↑ Contents

Lean sources, part 26 of 50 (lang/DH163.lean – lang/QuartSeven5.lean)

lang/DH163.lean

lean_all/lang/DH163.lean

/-
  lang/DH163 — T48/T50: **the Dedekind–Hasse step in ℤ[(1 + √−163)/2]**.  From a two-step remainder r (|r₂| = B, 2B ≤ N) a
  multiplier k ∈ {1, …, 7} with |kB − mN| small is found by a case tree on B/N (generated; each split |kB − mN|² · 163 < 3N² or a
  new linear bound on B/N, leaves closed by `contra`), and the exact points B/N = m/k are rescued by a table (resc_gen: if
  k r = (d₁, ±m) N then N(s r − e N) · k² = N² · N(s (d₁, ±m) − k e)).
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat; concrete norms by rfl), no axioms.
-/
import lang.Euc163
import lang.DH67

namespace Euc163

open FTAChain DetChain LibInt

theorem nrm_mulD (x y : Int × Int) : nrm ((Ring163.ring163 LibInt.intR).mul x y) = nrm x * nrm y :=
  Int.ofNat.inj (by rw [nrm_cast, en_mul, ← nrm_cast x, ← nrm_cast y]; rfl)

theorem nrm_scal (k : Nat) : nrm (Int.ofNat k, 0) = k * k := Int.ofNat.inj (by
  rw [nrm_cast]; show Int.ofNat k * Int.ofNat k + Int.ofNat k * 0 + 41 * (0 * 0) = Int.ofNat k * Int.ofNat k; lib_int)

/-- Natural residues by induction: n = K q + c with c < K. -/
theorem natRes (K : Nat) (hK : 0 < K) : ∀ n : Nat, ∃ q c : Nat, c < K ∧ n = K * q + c
  | 0 => ⟨0, 0, hK, by lib_nat⟩
  | n + 1 => by
    obtain ⟨q, c, hc, hn⟩ := natRes K hK n
    cases Nat.lt_or_ge (c + 1) K with
    | inl h => exact ⟨q, c + 1, h, by rw [hn]; lib_nat⟩
    | inr h =>
      have e : c + 1 = K := Nat.le_antisymm hc h
      exact ⟨q + 1, 0, hK, by rw [hn, show K * q + c + 1 = K * q + (c + 1) by lib_nat, e]; lib_nat⟩

/-- Residues: x = K q + c with 0 ≤ c < K. -/
theorem resN (K : Nat) (hK : 0 < K) (x : Int) : ∃ q : Int, ∃ c : Nat, c < K ∧ x = Int.ofNat K * q + Int.ofNat c := by
  cases x with
  | ofNat n =>
    obtain ⟨q, c, hc, hn⟩ := natRes K hK n
    exact ⟨Int.ofNat q, c, hc, by rw [hn]; rfl⟩
  | negSucc n =>
    obtain ⟨q, r, hr, hn⟩ := natRes K hK n
    obtain ⟨c, hc⟩ := EisEuclid.le_dest _ _ hr
    have hn' : Int.ofNat n = Int.ofNat K * Int.ofNat q + Int.ofNat r := congrArg Int.ofNat hn
    have hc' : Int.ofNat c + (Int.ofNat r + 1) = Int.ofNat K := congrArg Int.ofNat hc
    refine ⟨-Int.ofNat q + -1, c, Nat.lt_of_lt_of_le (Nat.lt_succ_of_le (Nat.le_add_right c _)) (Nat.le_of_eq hc), ?_⟩
    show -(Int.ofNat n + 1) = _
    rw [hn', ← hc']; lib_int

/-- The exact points: k r = (d₁, e₂) N and N(s (d₁, e₂) − k e) = n ∈ (0, k²) give a remainder of norm < N², nonzero. -/
theorem resc_gen (K N0 : Nat) (r1 r2 d1 e2 : Int) (h1 : Int.ofNat K * r1 = d1 * Int.ofNat (N0 + 1)) (h2 : Int.ofNat K * r2 = e2 * Int.ofNat (N0 + 1))
    (s e : Int × Int) (n : Nat) (hn : nrm ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (d1, e2)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul (Int.ofNat K, 0) e))) = n)
    (hn0 : 0 < n) (hnk : n < K * K) :
    nrm ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (r1, r2)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0)))) < (N0 + 1) * (N0 + 1) ∧
      (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (r1, r2)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0))) ≠ (Ring163.ring163 LibInt.intR).zero := by
  have hKr : (Ring163.ring163 LibInt.intR).mul (Int.ofNat K, 0) (r1, r2) = (Ring163.ring163 LibInt.intR).mul (Int.ofNat (N0 + 1), 0) (d1, e2) := by
    apply Prod.ext
    · show Int.ofNat K * r1 + -(0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2)   -- (+397 chars; full line in the Zenodo source)
      rw [h1]; lib_int
    · show Int.ofNat K * r2 + 0 * r1 + 0 * r2 = Int.ofNat (N0 + 1) * e2 + 0 * d1 + 0 * e2
      rw [h2]; lib_int
  have hid : (Ring163.ring163 LibInt.intR).mul (Int.ofNat K, 0) ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (r1, r2)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0)))) =
      (Ring163.ring163 LibInt.intR).mul (Int.ofNat (N0 + 1), 0) ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (d1, e2)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul (Int.ofNat K, 0) e))) := by
    calc _ = (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s ((Ring163.ring163 LibInt.intR).mul (Int.ofNat K, 0) (r1, r2))) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul (Int.ofNat (N0 + 1), 0) ((Ring163.ring163 LibInt.intR).mul (Int.ofNat K, 0) e))) := by lib_ring
      _ = _ := by rw [hKr]; lib_ring
  have hnn := congrArg nrm hid
  rw [nrm_mulD, nrm_mulD, nrm_scal, nrm_scal, hn] at hnn
  have hM : 0 < (N0 + 1) * (N0 + 1) := Nat.mul_pos (Nat.zero_lt_succ N0) (Nat.zero_lt_succ N0)
  refine ⟨?_, fun h0 => ?_⟩
  · cases Nat.lt_or_ge (nrm ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (r1, r2)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0))))) ((N0 + 1) * (N0 + 1)) with
    | inl h => exact h
    | inr h =>
      exfalso
      have a1 : K * K * ((N0 + 1) * (N0 + 1)) ≤ (N0 + 1) * (N0 + 1) * n := Nat.le_trans (Nat.mul_le_mul_left _ h) (Nat.le_of_eq hnn)
      have a2 : (N0 + 1) * (N0 + 1) * n < (N0 + 1) * (N0 + 1) * (K * K) := Nat.mul_lt_mul_of_pos_left hnk hM
      exact Nat.lt_irrefl _ (Nat.lt_of_le_of_lt a1 (Nat.lt_of_lt_of_le a2 (Nat.le_of_eq (by lib_nat))))
  · rw [h0] at hnn
    have z : (N0 + 1) * (N0 + 1) * n = 0 := hnn.symm.trans (Nat.mul_zero _)
    cases nat_mul_zero _ _ z with
    | inl e => exact Nat.lt_irrefl 0 (Nat.lt_of_lt_of_le hM (Nat.le_of_eq e))
    | inr e => exact Nat.lt_irrefl 0 (Nat.lt_of_lt_of_le hn0 (Nat.le_of_eq e))

theorem shift (K : Nat) (q c e2 : Int) (s e0 : Int × Int) :
    nrm ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (Int.ofNat K * q + c, e2)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul (Int.ofNat K, 0) ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (q, 0)) e0)))) =
      nrm ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (c, e2)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul (Int.ofNat K, 0) e0))) := by
  have hp : (Int.ofNat K * q + c, e2) = (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (Int.ofNat K, 0) (q, 0)) (c, e2) := by
    apply Prod.ext
    · show Int.ofNat K * q + c = Int.ofNat K * q + -(0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0) + c
      lib_int
    · show e2 = Int.ofNat K * 0 + 0 * q + 0 * 0 + e2
      lib_int
  rw [hp]
  exact congrArg nrm (by lib_ring)

/-- One branch: multiplier k + 1 with |(k+1) r₂ − d₂N| = w, 163w² < 3N² (d₂ = ±m; exact points rescued by the table). -/
theorem branchG (N0 : Nat) (r1 r2 d2 : Int) (k : Nat)
    (w : Nat) (hw : Int.natAbs (Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1))) = w) (hb : 163 * (w * w) < 3 * ((N0 + 1) * (N0 + 1)))
    (resc : ∀ d1 : Int, ∃ s e : Int × Int, ∃ n : Nat, nrm ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (d1, d2)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul (Int.ofNat (k + 1), 0) e))) = n ∧ 0 < n ∧ n < (k + 1) * (k + 1)) :
    ∃ s e : Int × Int, nrm ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (r1, r2)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0)))) < (N0 + 1) * (N0 + 1) ∧
      ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (r1, r2)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0))) = (Ring163.ring163 LibInt.intR).zero → s = (Ring163.ring163 LibInt.intR).one) := by
  have e2N : Int.ofNat (2 * N0 + 1 + 1) = 2 * Int.ofNat (N0 + 1) := by show 2 * Int.ofNat N0 + 1 + 1 = 2 * (Int.ofNat N0 + 1); lib_int
  obtain ⟨d1, t', hrd, hb1⟩ := GaussEuclid.rnd (Int.ofNat (k + 1) * (2 * r1 + r2) + -(d2 * Int.ofNat (N0 + 1))) (2 * N0 + 1)
  have hrd' : 2 * (Int.ofNat (k + 1) * r1 + -(d1 * Int.ofNat (N0 + 1))) + (Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1))) = t' := by
    calc 2 * (Int.ofNat (k + 1) * r1 + -(d1 * Int.ofNat (N0 + 1))) + (Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1))) = Int.ofNat (k + 1) * (2 * r1 + r2) + -(d2 * Int.ofNat (N0 + 1)) + -(d1 * (2 * Int.ofNat (N0 + 1))) := by lib_int
      _ = t' := by rw [hrd, e2N]; lib_int
  have hT' : Int.natAbs t' ≤ N0 + 1 := Nat.le_of_mul_le_mul_left (Nat.le_trans hb1 (Nat.le_of_eq (by lib_nat))) (Nat.zero_lt_succ 1)
  have hexpr : (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (Int.ofNat (k + 1), 0) (r1, r2)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul (d1, d2) (Int.ofNat (N0 + 1), 0))) = (Int.ofNat (k + 1) * r1 + -(d1 * Int.ofNat (N0 + 1)), Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1))) := by
    apply Prod.ext
    · show Int.ofNat (k + 1) * r1 + -(0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0   -- (+469 chars; full line in the Zenodo source)
    · show Int.ofNat (k + 1) * r2 + 0 * r1 + 0 * r2 + -(d1 * 0 + d2 * Int.ofNat (N0 + 1) + d2 * 0) = Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1)); lib_int
  by_cases hz : (Int.ofNat (k + 1) * r1 + -(d1 * Int.ofNat (N0 + 1)), Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1))) = (Ring163.ring163 LibInt.intR).zero
  · have z1 : Int.ofNat (k + 1) * r1 + -(d1 * Int.ofNat (N0 + 1)) = 0 := congrArg Prod.fst hz
    have z2 : Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1)) = 0 := congrArg Prod.snd hz
    have h1 : Int.ofNat (k + 1) * r1 = d1 * Int.ofNat (N0 + 1) := by
      rw [show Int.ofNat (k + 1) * r1 = Int.ofNat (k + 1) * r1 + -(d1 * Int.ofNat (N0 + 1)) + d1 * Int.ofNat (N0 + 1) by lib_int, z1]; lib_int
    have h2' : Int.ofNat (k + 1) * r2 = d2 * Int.ofNat (N0 + 1) := by
      rw [show Int.ofNat (k + 1) * r2 = Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1)) + d2 * Int.ofNat (N0 + 1) by lib_int, z2]; lib_int
    obtain ⟨sv, ev, n, hn, hn0, hnk⟩ := resc d1
    obtain ⟨hlt, hne⟩ := resc_gen (k + 1) N0 r1 r2 d1 d2 h1 h2' sv ev n hn hn0 hnk
    exact ⟨sv, ev, hlt, fun h0 => absurd h0 hne⟩
  · refine ⟨(Int.ofNat (k + 1), 0), (d1, d2), ?_, fun h0 => absurd (hexpr.symm.trans h0) hz⟩
    rw [hexpr]
    apply nrm_lt
    show Int.natAbs (2 * (Int.ofNat (k + 1) * r1 + -(d1 * Int.ofNat (N0 + 1))) + (Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1)))) * Int.natAbs (2 * (Int.ofNat (k + 1) * r1 + -(d1 * Int.ofNat (N0 + 1))) + (Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1))))
      + 163 * (Int.natAbs (Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1))) * Int.natAbs (Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1)))) < _
    rw [hrd', hw]
    calc Int.natAbs t' * Int.natAbs t' + 163 * (w * w) < (N0 + 1) * (N0 + 1) + 3 * ((N0 + 1) * (N0 + 1)) := Nat.add_lt_add_of_le_of_lt (Nat.mul_le_mul hT' hT') hb
      _ = 4 * ((N0 + 1) * (N0 + 1)) := by lib_nat


theorem lb_from (k m p q X B N : Nat) (hX : X + m * N = k * B) (h : q * N < p * X) : (p * m + q) * N < (p * k) * B := by
  have e : (p * k) * B = p * X + p * m * N := by rw [show (p * k) * B = p * (k * B) by lib_nat, ← hX]; lib_nat
  rw [e, show (p * m + q) * N = q * N + p * m * N by lib_nat]
  exact Nat.add_lt_add_right h _

theorem ub_from (k m p q X B N C : Nat) (hX : X + k * B = m * N) (h : q * N < p * X) (hC : C + q = p * m) : (p * k) * B < C * N := by
  apply Euc67.lt_cancel _ _ (q * N)
  calc (p * k) * B + q * N < (p * k) * B + p * X := Nat.add_lt_add_left h _
    _ = p * (m * N) := by rw [← hX]; lib_nat
    _ = C * N + q * N := by rw [show p * (m * N) = (p * m) * N by lib_nat, ← hC]; lib_nat

theorem contra (a b c d N B : Nat) (h1 : a * N < b * B) (h2 : c * B ≤ d * N) (hc : 0 < c) (h : b * d ≤ c * a) : False := by
  have s1 : c * (a * N) < c * (b * B) := Nat.mul_lt_mul_of_pos_left h1 hc
  have s2 : b * (c * B) ≤ b * (d * N) := Nat.mul_le_mul_left b h2
  have s3 : (b * d) * N ≤ (c * a) * N := Nat.mul_le_mul_right N h
  have : c * (a * N) < c * (a * N) :=
    Nat.lt_of_lt_of_le s1 (Nat.le_trans (Nat.le_of_eq (by lib_nat)) (Nat.le_trans s2 (Nat.le_trans (Nat.le_of_eq (by lib_nat)) (Nat.le_trans s3 (Nat.le_of_eq (by lib_nat))))))
  exact Nat.lt_irrefl _ this

theorem wpos (k m X B N0 : Nat) (d2 : Int) (hX : X + m * (N0 + 1) = k * B) :
    Int.ofNat k * (d2 * Int.ofNat B) + -(d2 * Int.ofNat m * Int.ofNat (N0 + 1)) = d2 * Int.ofNat X := by
  have h : Int.ofNat X + Int.ofNat m * Int.ofNat (N0 + 1) = Int.ofNat k * Int.ofNat B := congrArg Int.ofNat hX
  calc Int.ofNat k * (d2 * Int.ofNat B) + -(d2 * Int.ofNat m * Int.ofNat (N0 + 1)) = d2 * (Int.ofNat k * Int.ofNat B) + -(d2 * (Int.ofNat m * Int.ofNat (N0 + 1))) := by lib_int
    _ = d2 * Int.ofNat X := by rw [← h]; lib_int

theorem wneg (k m X B N0 : Nat) (d2 : Int) (hX : X + k * B = m * (N0 + 1)) :
    Int.ofNat k * (d2 * Int.ofNat B) + -(d2 * Int.ofNat m * Int.ofNat (N0 + 1)) = -(d2 * Int.ofNat X) := by
  have h : Int.ofNat X + Int.ofNat k * Int.ofNat B = Int.ofNat m * Int.ofNat (N0 + 1) := congrArg Int.ofNat hX
  calc Int.ofNat k * (d2 * Int.ofNat B) + -(d2 * Int.ofNat m * Int.ofNat (N0 + 1)) = d2 * (Int.ofNat k * Int.ofNat B) + -(d2 * (Int.ofNat m * Int.ofNat (N0 + 1))) := by lib_int
    _ = -(d2 * Int.ofNat X) := by rw [← h]; lib_int

theorem rs2_1 (d2 : Int) (hd2 : d2 = 1 ∨ d2 = -1) (d1 : Int) : ∃ s e : Int × Int, ∃ n : Nat,
    nrm ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (d1, d2 * Int.ofNat 1)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul (Int.ofNat (1 + 1), 0) e))) = n ∧ 0 < n ∧ n < (1 + 1) * (1 + 1) := by
  obtain ⟨q, c, hc, hd⟩ := resN (1 + 1) (Nat.zero_lt_succ 1) d1
  rw [hd]
  rcases hd2 with e | e <;> rw [e]
  · match c, hc with
    | 0, _ => exact ⟨((-1), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 1) (q, 0)) ((-21), 0), 1, (shift (1 + 1) q (Int.ofNat 0) (1 * Int.ofNat 1) ((-1), 1) ((-21), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 1, _ => exact ⟨(0, (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (0, (-1)) (q, 0)) (20, (-1)), 1, (shift (1 + 1) q (Int.ofNat 1) (1 * Int.ofNat 1) (0, (-1)) (20, (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | _ + 2, h => exact absurd h (Nat.not_lt_of_ge (Nat.le_add_left _ _))
  · match c, hc with
    | 0, _ => exact ⟨((-1), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 1) (q, 0)) (20, 0), 1, (shift (1 + 1) q (Int.ofNat 0) (-1 * Int.ofNat 1) ((-1), 1) (20, 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 1, _ => exact ⟨(0, (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (0, (-1)) (q, 0)) ((-21), 0), 1, (shift (1 + 1) q (Int.ofNat 1) (-1 * Int.ofNat 1) (0, (-1)) ((-21), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | _ + 2, h => exact absurd h (Nat.not_lt_of_ge (Nat.le_add_left _ _))

theorem rs3_1 (d2 : Int) (hd2 : d2 = 1 ∨ d2 = -1) (d1 : Int) : ∃ s e : Int × Int, ∃ n : Nat,
    nrm ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (d1, d2 * Int.ofNat 1)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul (Int.ofNat (2 + 1), 0) e))) = n ∧ 0 < n ∧ n < (2 + 1) * (2 + 1) := by
  obtain ⟨q, c, hc, hd⟩ := resN (2 + 1) (Nat.zero_lt_succ 2) d1
  rw [hd]
  rcases hd2 with e | e <;> rw [e]
  · match c, hc with
    | 0, _ => exact ⟨((-1), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 1) (q, 0)) ((-14), 0), 1, (shift (2 + 1) q (Int.ofNat 0) (1 * Int.ofNat 1) ((-1), 1) ((-14), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 1, _ => exact ⟨((-1), (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), (-1)) (q, 0)) (13, (-1)), 1, (shift (2 + 1) q (Int.ofNat 1) (1 * Int.ofNat 1) ((-1), (-1)) (13, (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 2, _ => exact ⟨(0, (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (0, (-1)) (q, 0)) (14, (-1)), 1, (shift (2 + 1) q (Int.ofNat 2) (1 * Int.ofNat 1) (0, (-1)) (14, (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | _ + 3, h => exact absurd h (Nat.not_lt_of_ge (Nat.le_add_left _ _))
  · match c, hc with
    | 0, _ => exact ⟨((-1), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 1) (q, 0)) (14, 0), 1, (shift (2 + 1) q (Int.ofNat 0) (-1 * Int.ofNat 1) ((-1), 1) (14, 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 1, _ => exact ⟨(0, (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (0, (-1)) (q, 0)) ((-14), 0), 1, (shift (2 + 1) q (Int.ofNat 1) (-1 * Int.ofNat 1) (0, (-1)) ((-14), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 2, _ => exact ⟨((-1), (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), (-1)) (q, 0)) ((-14), 0), 1, (shift (2 + 1) q (Int.ofNat 2) (-1 * Int.ofNat 1) ((-1), (-1)) ((-14), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | _ + 3, h => exact absurd h (Nat.not_lt_of_ge (Nat.le_add_left _ _))

theorem rs5_2 (d2 : Int) (hd2 : d2 = 1 ∨ d2 = -1) (d1 : Int) : ∃ s e : Int × Int, ∃ n : Nat,
    nrm ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (d1, d2 * Int.ofNat 2)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul (Int.ofNat (4 + 1), 0) e))) = n ∧ 0 < n ∧ n < (4 + 1) * (4 + 1) := by
  obtain ⟨q, c, hc, hd⟩ := resN (4 + 1) (Nat.zero_lt_succ 4) d1
  rw [hd]
  rcases hd2 with e | e <;> rw [e]
  · match c, hc with
    | 0, _ => exact ⟨((-1), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 1) (q, 0)) ((-16), 0), 4, (shift (4 + 1) q (Int.ofNat 0) (1 * Int.ofNat 2) ((-1), 1) ((-16), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 1, _ => exact ⟨((-1), (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), (-1)) (q, 0)) (16, (-1)), 1, (shift (4 + 1) q (Int.ofNat 1) (1 * Int.ofNat 2) ((-1), (-1)) (16, (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 2, _ => exact ⟨((-2), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), 1) (q, 0)) ((-17), 0), 1, (shift (4 + 1) q (Int.ofNat 2) (1 * Int.ofNat 2) ((-2), 1) ((-17), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 3, _ => exact ⟨(0, (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (0, (-1)) (q, 0)) (16, (-1)), 4, (shift (4 + 1) q (Int.ofNat 3) (1 * Int.ofNat 2) (0, (-1)) (16, (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 4, _ => exact ⟨((-2), (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), (-1)) (q, 0)) (15, (-2)), 1, (shift (4 + 1) q (Int.ofNat 4) (1 * Int.ofNat 2) ((-2), (-1)) (15, (-2))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | _ + 5, h => exact absurd h (Nat.not_lt_of_ge (Nat.le_add_left _ _))
  · match c, hc with
    | 0, _ => exact ⟨((-1), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 1) (q, 0)) (16, 0), 4, (shift (4 + 1) q (Int.ofNat 0) (-1 * Int.ofNat 2) ((-1), 1) (16, 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 1, _ => exact ⟨((-2), (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), (-1)) (q, 0)) ((-17), 1), 1, (shift (4 + 1) q (Int.ofNat 1) (-1 * Int.ofNat 2) ((-2), (-1)) ((-17), 1)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 2, _ => exact ⟨(0, (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (0, (-1)) (q, 0)) ((-16), 0), 4, (shift (4 + 1) q (Int.ofNat 2) (-1 * Int.ofNat 2) (0, (-1)) ((-16), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 3, _ => exact ⟨((-2), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), 1) (q, 0)) (15, 1), 1, (shift (4 + 1) q (Int.ofNat 3) (-1 * Int.ofNat 2) ((-2), 1) (15, 1)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 4, _ => exact ⟨((-1), (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), (-1)) (q, 0)) ((-17), 0), 1, (shift (4 + 1) q (Int.ofNat 4) (-1 * Int.ofNat 2) ((-1), (-1)) ((-17), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | _ + 5, h => exact absurd h (Nat.not_lt_of_ge (Nat.le_add_left _ _))

theorem rs7_3 (d2 : Int) (hd2 : d2 = 1 ∨ d2 = -1) (d1 : Int) : ∃ s e : Int × Int, ∃ n : Nat,
    nrm ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (d1, d2 * Int.ofNat 3)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul (Int.ofNat (6 + 1), 0) e))) = n ∧ 0 < n ∧ n < (6 + 1) * (6 + 1) := by
  obtain ⟨q, c, hc, hd⟩ := resN (6 + 1) (Nat.zero_lt_succ 6) d1
  rw [hd]
  rcases hd2 with e | e <;> rw [e]
  · match c, hc with
    | 0, _ => exact ⟨((-2), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), 0) (q, 0)) (0, (-1)), 41, (shift (6 + 1) q (Int.ofNat 0) (1 * Int.ofNat 3) ((-2), 0) (0, (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 1, _ => exact ⟨((-2), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), 0) (q, 0)) (0, (-1)), 43, (shift (6 + 1) q (Int.ofNat 1) (1 * Int.ofNat 3) ((-2), 0) (0, (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 2, _ => exact ⟨((-1), (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), (-1)) (q, 0)) (17, (-1)), 43, (shift (6 + 1) q (Int.ofNat 2) (1 * Int.ofNat 3) ((-1), (-1)) (17, (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 3, _ => exact ⟨((-2), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), 0) (q, 0)) ((-1), (-1)), 43, (shift (6 + 1) q (Int.ofNat 3) (1 * Int.ofNat 3) ((-2), 0) ((-1), (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 4, _ => exact ⟨((-2), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), 0) (q, 0)) ((-1), (-1)), 41, (shift (6 + 1) q (Int.ofNat 4) (1 * Int.ofNat 3) ((-2), 0) ((-1), (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 5, _ => exact ⟨((-2), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), 0) (q, 0)) ((-1), (-1)), 47, (shift (6 + 1) q (Int.ofNat 5) (1 * Int.ofNat 3) ((-2), 0) ((-1), (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 6, _ => exact ⟨((-2), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), 0) (q, 0)) ((-2), (-1)), 47, (shift (6 + 1) q (Int.ofNat 6) (1 * Int.ofNat 3) ((-2), 0) ((-2), (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | _ + 7, h => exact absurd h (Nat.not_lt_of_ge (Nat.le_add_left _ _))
  · match c, hc with
    | 0, _ => exact ⟨((-2), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), 0) (q, 0)) (0, 1), 41, (shift (6 + 1) q (Int.ofNat 0) (-1 * Int.ofNat 3) ((-2), 0) (0, 1)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 1, _ => exact ⟨((-2), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), 0) (q, 0)) (0, 1), 47, (shift (6 + 1) q (Int.ofNat 1) (-1 * Int.ofNat 3) ((-2), 0) (0, 1)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 2, _ => exact ⟨((-2), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), 0) (q, 0)) ((-1), 1), 47, (shift (6 + 1) q (Int.ofNat 2) (-1 * Int.ofNat 3) ((-2), 0) ((-1), 1)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 3, _ => exact ⟨((-2), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), 0) (q, 0)) ((-1), 1), 41, (shift (6 + 1) q (Int.ofNat 3) (-1 * Int.ofNat 3) ((-2), 0) ((-1), 1)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 4, _ => exact ⟨((-2), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), 0) (q, 0)) ((-1), 1), 43, (shift (6 + 1) q (Int.ofNat 4) (-1 * Int.ofNat 3) ((-2), 0) ((-1), 1)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 5, _ => exact ⟨((-1), (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), (-1)) (q, 0)) ((-18), 0), 43, (shift (6 + 1) q (Int.ofNat 5) (-1 * Int.ofNat 3) ((-1), (-1)) ((-18), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 6, _ => exact ⟨((-2), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), 0) (q, 0)) ((-2), 1), 43, (shift (6 + 1) q (Int.ofNat 6) (-1 * Int.ofNat 3) ((-2), 0) ((-2), 1)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | _ + 7, h => exact absurd h (Nat.not_lt_of_ge (Nat.le_add_left _ _))

theorem rs4_1 (d2 : Int) (hd2 : d2 = 1 ∨ d2 = -1) (d1 : Int) : ∃ s e : Int × Int, ∃ n : Nat,
    nrm ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (d1, d2 * Int.ofNat 1)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul (Int.ofNat (3 + 1), 0) e))) = n ∧ 0 < n ∧ n < (3 + 1) * (3 + 1) := by
  obtain ⟨q, c, hc, hd⟩ := resN (3 + 1) (Nat.zero_lt_succ 3) d1
  rw [hd]
  rcases hd2 with e | e <;> rw [e]
  · match c, hc with
    | 0, _ => exact ⟨((-1), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 1) (q, 0)) ((-10), 0), 1, (shift (3 + 1) q (Int.ofNat 0) (1 * Int.ofNat 1) ((-1), 1) ((-10), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 1, _ => exact ⟨((-2), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), 1) (q, 0)) ((-11), 0), 1, (shift (3 + 1) q (Int.ofNat 1) (1 * Int.ofNat 1) ((-2), 1) ((-11), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 2, _ => exact ⟨((-1), (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), (-1)) (q, 0)) (10, (-1)), 1, (shift (3 + 1) q (Int.ofNat 2) (1 * Int.ofNat 1) ((-1), (-1)) (10, (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 3, _ => exact ⟨(0, (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (0, (-1)) (q, 0)) (10, (-1)), 1, (shift (3 + 1) q (Int.ofNat 3) (1 * Int.ofNat 1) (0, (-1)) (10, (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | _ + 4, h => exact absurd h (Nat.not_lt_of_ge (Nat.le_add_left _ _))
  · match c, hc with
    | 0, _ => exact ⟨((-1), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 1) (q, 0)) (10, 0), 1, (shift (3 + 1) q (Int.ofNat 0) (-1 * Int.ofNat 1) ((-1), 1) (10, 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 1, _ => exact ⟨(0, (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (0, (-1)) (q, 0)) ((-10), 0), 1, (shift (3 + 1) q (Int.ofNat 1) (-1 * Int.ofNat 1) (0, (-1)) ((-10), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 2, _ => exact ⟨((-1), (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), (-1)) (q, 0)) ((-11), 0), 1, (shift (3 + 1) q (Int.ofNat 2) (-1 * Int.ofNat 1) ((-1), (-1)) ((-11), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 3, _ => exact ⟨((-2), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), 1) (q, 0)) (9, 1), 1, (shift (3 + 1) q (Int.ofNat 3) (-1 * Int.ofNat 1) ((-2), 1) (9, 1)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | _ + 4, h => exact absurd h (Nat.not_lt_of_ge (Nat.le_add_left _ _))

theorem rs7_2 (d2 : Int) (hd2 : d2 = 1 ∨ d2 = -1) (d1 : Int) : ∃ s e : Int × Int, ∃ n : Nat,
    nrm ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (d1, d2 * Int.ofNat 2)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul (Int.ofNat (6 + 1), 0) e))) = n ∧ 0 < n ∧ n < (6 + 1) * (6 + 1) := by
  obtain ⟨q, c, hc, hd⟩ := resN (6 + 1) (Nat.zero_lt_succ 6) d1
  rw [hd]
  rcases hd2 with e | e <;> rw [e]
  · match c, hc with
    | 0, _ => exact ⟨((-3), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-3), 0) (q, 0)) (0, (-1)), 41, (shift (6 + 1) q (Int.ofNat 0) (1 * Int.ofNat 2) ((-3), 0) (0, (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 1, _ => exact ⟨((-3), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-3), 0) (q, 0)) (0, (-1)), 47, (shift (6 + 1) q (Int.ofNat 1) (1 * Int.ofNat 2) ((-3), 0) (0, (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 2, _ => exact ⟨((-3), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-3), 0) (q, 0)) ((-1), (-1)), 43, (shift (6 + 1) q (Int.ofNat 2) (1 * Int.ofNat 2) ((-3), 0) ((-1), (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 3, _ => exact ⟨((-3), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-3), 0) (q, 0)) ((-1), (-1)), 43, (shift (6 + 1) q (Int.ofNat 3) (1 * Int.ofNat 2) ((-3), 0) ((-1), (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 4, _ => exact ⟨((-3), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-3), 0) (q, 0)) ((-2), (-1)), 47, (shift (6 + 1) q (Int.ofNat 4) (1 * Int.ofNat 2) ((-3), 0) ((-2), (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 5, _ => exact ⟨((-3), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-3), 0) (q, 0)) ((-2), (-1)), 41, (shift (6 + 1) q (Int.ofNat 5) (1 * Int.ofNat 2) ((-3), 0) ((-2), (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 6, _ => exact ⟨(0, (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (0, (-1)) (q, 0)) (12, (-1)), 47, (shift (6 + 1) q (Int.ofNat 6) (1 * Int.ofNat 2) (0, (-1)) (12, (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | _ + 7, h => exact absurd h (Nat.not_lt_of_ge (Nat.le_add_left _ _))
  · match c, hc with
    | 0, _ => exact ⟨((-3), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-3), 0) (q, 0)) (0, 1), 41, (shift (6 + 1) q (Int.ofNat 0) (-1 * Int.ofNat 2) ((-3), 0) (0, 1)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 1, _ => exact ⟨(0, (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (0, (-1)) (q, 0)) ((-12), 0), 47, (shift (6 + 1) q (Int.ofNat 1) (-1 * Int.ofNat 2) (0, (-1)) ((-12), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 2, _ => exact ⟨((-3), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-3), 0) (q, 0)) ((-1), 1), 41, (shift (6 + 1) q (Int.ofNat 2) (-1 * Int.ofNat 2) ((-3), 0) ((-1), 1)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 3, _ => exact ⟨((-3), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-3), 0) (q, 0)) ((-1), 1), 47, (shift (6 + 1) q (Int.ofNat 3) (-1 * Int.ofNat 2) ((-3), 0) ((-1), 1)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 4, _ => exact ⟨((-3), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-3), 0) (q, 0)) ((-2), 1), 43, (shift (6 + 1) q (Int.ofNat 4) (-1 * Int.ofNat 2) ((-3), 0) ((-2), 1)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 5, _ => exact ⟨((-3), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-3), 0) (q, 0)) ((-2), 1), 43, (shift (6 + 1) q (Int.ofNat 5) (-1 * Int.ofNat 2) ((-3), 0) ((-2), 1)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 6, _ => exact ⟨((-3), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-3), 0) (q, 0)) ((-3), 1), 47, (shift (6 + 1) q (Int.ofNat 6) (-1 * Int.ofNat 2) ((-3), 0) ((-3), 1)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | _ + 7, h => exact absurd h (Nat.not_lt_of_ge (Nat.le_add_left _ _))

theorem rs5_1 (d2 : Int) (hd2 : d2 = 1 ∨ d2 = -1) (d1 : Int) : ∃ s e : Int × Int, ∃ n : Nat,
    nrm ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (d1, d2 * Int.ofNat 1)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul (Int.ofNat (4 + 1), 0) e))) = n ∧ 0 < n ∧ n < (4 + 1) * (4 + 1) := by
  obtain ⟨q, c, hc, hd⟩ := resN (4 + 1) (Nat.zero_lt_succ 4) d1
  rw [hd]
  rcases hd2 with e | e <;> rw [e]
  · match c, hc with
    | 0, _ => exact ⟨((-1), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 1) (q, 0)) ((-8), 0), 1, (shift (4 + 1) q (Int.ofNat 0) (1 * Int.ofNat 1) ((-1), 1) ((-8), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 1, _ => exact ⟨((-2), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), 1) (q, 0)) ((-9), 0), 4, (shift (4 + 1) q (Int.ofNat 1) (1 * Int.ofNat 1) ((-2), 1) ((-9), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 2, _ => exact ⟨((-2), (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), (-1)) (q, 0)) (7, (-1)), 4, (shift (4 + 1) q (Int.ofNat 2) (1 * Int.ofNat 1) ((-2), (-1)) (7, (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 3, _ => exact ⟨((-1), (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), (-1)) (q, 0)) (8, (-1)), 4, (shift (4 + 1) q (Int.ofNat 3) (1 * Int.ofNat 1) ((-1), (-1)) (8, (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 4, _ => exact ⟨(0, (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (0, (-1)) (q, 0)) (8, (-1)), 1, (shift (4 + 1) q (Int.ofNat 4) (1 * Int.ofNat 1) (0, (-1)) (8, (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | _ + 5, h => exact absurd h (Nat.not_lt_of_ge (Nat.le_add_left _ _))
  · match c, hc with
    | 0, _ => exact ⟨((-1), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 1) (q, 0)) (8, 0), 1, (shift (4 + 1) q (Int.ofNat 0) (-1 * Int.ofNat 1) ((-1), 1) (8, 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 1, _ => exact ⟨(0, (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (0, (-1)) (q, 0)) ((-8), 0), 1, (shift (4 + 1) q (Int.ofNat 1) (-1 * Int.ofNat 1) (0, (-1)) ((-8), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 2, _ => exact ⟨((-1), (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), (-1)) (q, 0)) ((-9), 0), 4, (shift (4 + 1) q (Int.ofNat 2) (-1 * Int.ofNat 1) ((-1), (-1)) ((-9), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 3, _ => exact ⟨((-2), (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), (-1)) (q, 0)) ((-9), 0), 4, (shift (4 + 1) q (Int.ofNat 3) (-1 * Int.ofNat 1) ((-2), (-1)) ((-9), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 4, _ => exact ⟨((-2), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), 1) (q, 0)) (7, 1), 4, (shift (4 + 1) q (Int.ofNat 4) (-1 * Int.ofNat 1) ((-2), 1) (7, 1)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | _ + 5, h => exact absurd h (Nat.not_lt_of_ge (Nat.le_add_left _ _))

theorem rs6_1 (d2 : Int) (hd2 : d2 = 1 ∨ d2 = -1) (d1 : Int) : ∃ s e : Int × Int, ∃ n : Nat,
    nrm ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (d1, d2 * Int.ofNat 1)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul (Int.ofNat (5 + 1), 0) e))) = n ∧ 0 < n ∧ n < (5 + 1) * (5 + 1) := by
  obtain ⟨q, c, hc, hd⟩ := resN (5 + 1) (Nat.zero_lt_succ 5) d1
  rw [hd]
  rcases hd2 with e | e <;> rw [e]
  · match c, hc with
    | 0, _ => exact ⟨((-1), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 1) (q, 0)) ((-7), 0), 1, (shift (5 + 1) q (Int.ofNat 0) (1 * Int.ofNat 1) ((-1), 1) ((-7), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 1, _ => exact ⟨((-2), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), 1) (q, 0)) ((-7), 0), 1, (shift (5 + 1) q (Int.ofNat 1) (1 * Int.ofNat 1) ((-2), 1) ((-7), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 2, _ => exact ⟨((-3), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-3), 1) (q, 0)) ((-8), 0), 1, (shift (5 + 1) q (Int.ofNat 2) (1 * Int.ofNat 1) ((-3), 1) ((-8), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 3, _ => exact ⟨((-2), (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), (-1)) (q, 0)) (6, (-1)), 1, (shift (5 + 1) q (Int.ofNat 3) (1 * Int.ofNat 1) ((-2), (-1)) (6, (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 4, _ => exact ⟨((-1), (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), (-1)) (q, 0)) (6, (-1)), 1, (shift (5 + 1) q (Int.ofNat 4) (1 * Int.ofNat 1) ((-1), (-1)) (6, (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 5, _ => exact ⟨(0, (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (0, (-1)) (q, 0)) (7, (-1)), 1, (shift (5 + 1) q (Int.ofNat 5) (1 * Int.ofNat 1) (0, (-1)) (7, (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | _ + 6, h => exact absurd h (Nat.not_lt_of_ge (Nat.le_add_left _ _))
  · match c, hc with
    | 0, _ => exact ⟨((-1), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 1) (q, 0)) (7, 0), 1, (shift (5 + 1) q (Int.ofNat 0) (-1 * Int.ofNat 1) ((-1), 1) (7, 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 1, _ => exact ⟨(0, (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (0, (-1)) (q, 0)) ((-7), 0), 1, (shift (5 + 1) q (Int.ofNat 1) (-1 * Int.ofNat 1) (0, (-1)) ((-7), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 2, _ => exact ⟨((-1), (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), (-1)) (q, 0)) ((-7), 0), 1, (shift (5 + 1) q (Int.ofNat 2) (-1 * Int.ofNat 1) ((-1), (-1)) ((-7), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 3, _ => exact ⟨((-2), (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), (-1)) (q, 0)) ((-8), 0), 1, (shift (5 + 1) q (Int.ofNat 3) (-1 * Int.ofNat 1) ((-2), (-1)) ((-8), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 4, _ => exact ⟨((-3), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-3), 1) (q, 0)) (5, 1), 1, (shift (5 + 1) q (Int.ofNat 4) (-1 * Int.ofNat 1) ((-3), 1) (5, 1)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 5, _ => exact ⟨((-2), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), 1) (q, 0)) (5, 1), 1, (shift (5 + 1) q (Int.ofNat 5) (-1 * Int.ofNat 1) ((-2), 1) (5, 1)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | _ + 6, h => exact absurd h (Nat.not_lt_of_ge (Nat.le_add_left _ _))

theorem rs7_1 (d2 : Int) (hd2 : d2 = 1 ∨ d2 = -1) (d1 : Int) : ∃ s e : Int × Int, ∃ n : Nat,
    nrm ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (d1, d2 * Int.ofNat 1)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul (Int.ofNat (6 + 1), 0) e))) = n ∧ 0 < n ∧ n < (6 + 1) * (6 + 1) := by
  obtain ⟨q, c, hc, hd⟩ := resN (6 + 1) (Nat.zero_lt_succ 6) d1
  rw [hd]
  rcases hd2 with e | e <;> rw [e]
  · match c, hc with
    | 0, _ => exact ⟨((-1), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 0) (q, 0)) (0, 0), 41, (shift (6 + 1) q (Int.ofNat 0) (1 * Int.ofNat 1) ((-1), 0) (0, 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 1, _ => exact ⟨((-1), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 0) (q, 0)) (0, 0), 43, (shift (6 + 1) q (Int.ofNat 1) (1 * Int.ofNat 1) ((-1), 0) (0, 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 2, _ => exact ⟨((-1), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 0) (q, 0)) (0, 0), 47, (shift (6 + 1) q (Int.ofNat 2) (1 * Int.ofNat 1) ((-1), 0) (0, 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 3, _ => exact ⟨((-2), (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), (-1)) (q, 0)) (5, (-1)), 41, (shift (6 + 1) q (Int.ofNat 3) (1 * Int.ofNat 1) ((-2), (-1)) (5, (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 4, _ => exact ⟨((-1), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 0) (q, 0)) ((-1), 0), 47, (shift (6 + 1) q (Int.ofNat 4) (1 * Int.ofNat 1) ((-1), 0) ((-1), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 5, _ => exact ⟨((-1), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 0) (q, 0)) ((-1), 0), 43, (shift (6 + 1) q (Int.ofNat 5) (1 * Int.ofNat 1) ((-1), 0) ((-1), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 6, _ => exact ⟨((-1), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 0) (q, 0)) ((-1), 0), 41, (shift (6 + 1) q (Int.ofNat 6) (1 * Int.ofNat 1) ((-1), 0) ((-1), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | _ + 7, h => exact absurd h (Nat.not_lt_of_ge (Nat.le_add_left _ _))
  · match c, hc with
    | 0, _ => exact ⟨((-1), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 0) (q, 0)) (0, 0), 41, (shift (6 + 1) q (Int.ofNat 0) (-1 * Int.ofNat 1) ((-1), 0) (0, 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 1, _ => exact ⟨((-1), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 0) (q, 0)) (0, 0), 41, (shift (6 + 1) q (Int.ofNat 1) (-1 * Int.ofNat 1) ((-1), 0) (0, 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 2, _ => exact ⟨((-1), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 0) (q, 0)) (0, 0), 43, (shift (6 + 1) q (Int.ofNat 2) (-1 * Int.ofNat 1) ((-1), 0) (0, 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 3, _ => exact ⟨((-1), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 0) (q, 0)) (0, 0), 47, (shift (6 + 1) q (Int.ofNat 3) (-1 * Int.ofNat 1) ((-1), 0) (0, 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 4, _ => exact ⟨((-2), (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), (-1)) (q, 0)) ((-7), 0), 41, (shift (6 + 1) q (Int.ofNat 4) (-1 * Int.ofNat 1) ((-2), (-1)) ((-7), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 5, _ => exact ⟨((-1), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 0) (q, 0)) ((-1), 0), 47, (shift (6 + 1) q (Int.ofNat 5) (-1 * Int.ofNat 1) ((-1), 0) ((-1), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 6, _ => exact ⟨((-1), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 0) (q, 0)) ((-1), 0), 43, (shift (6 + 1) q (Int.ofNat 6) (-1 * Int.ofNat 1) ((-1), 0) ((-1), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | _ + 7, h => exact absurd h (Nat.not_lt_of_ge (Nat.le_add_left _ _))

set_option maxHeartbeats 0 in
/-- **The Dedekind–Hasse step on a remainder (d = 163).** -/
theorem dh163_r (N0 : Nat) (r1 r2 : Int) (h2 : 2 * Int.natAbs r2 ≤ N0 + 1) (ht : Int.natAbs (2 * r1 + r2) ≤ N0 + 1) :
    ∃ s e : Int × Int, nrm ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (r1, r2)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0)))) < (N0 + 1) * (N0 + 1) ∧
      ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (r1, r2)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0))) = (Ring163.ring163 LibInt.intR).zero → s = (Ring163.ring163 LibInt.intR).one) := by
  have h4 := four_nrm (r1, r2)
  generalize hT : Int.natAbs (2 * r1 + r2) = T at h4 ht
  generalize hB : Int.natAbs r2 = B at h4 h2
  have h4' : 4 * nrm (r1, r2) = T * T + 163 * (B * B) := h4
  cases Nat.lt_or_ge (T * T + 163 * (B * B)) (4 * ((N0 + 1) * (N0 + 1))) with
  | inl hl =>
    refine ⟨(Ring163.ring163 LibInt.intR).one, (Ring163.ring163 LibInt.intR).zero, ?_, fun _ => rfl⟩
    have e : (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (Ring163.ring163 LibInt.intR).one (r1, r2)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul (Ring163.ring163 LibInt.intR).zero (Int.ofNat (N0 + 1), 0))) = (r1, r2) := by lib_ring
    rw [e]
    exact nrm_lt (r1, r2) (N0 + 1) (by rw [hT, hB]; exact hl)
  | inr hg =>
    have h3 : 3 * ((N0 + 1) * (N0 + 1)) ≤ 163 * (B * B) := GaussEuclid.le_cancel _ _ ((N0 + 1) * (N0 + 1)) (by
      calc 3 * ((N0 + 1) * (N0 + 1)) + (N0 + 1) * (N0 + 1) = 4 * ((N0 + 1) * (N0 + 1)) := by lib_nat
        _ ≤ T * T + 163 * (B * B) := hg
        _ ≤ (N0 + 1) * (N0 + 1) + 163 * (B * B) := Nat.add_le_add_right (Nat.mul_le_mul ht ht) _
        _ = 163 * (B * B) + (N0 + 1) * (N0 + 1) := by lib_nat)
    have lb0 : 5 * (N0 + 1) < 37 * B := Euc67.lin_lb 163 3 37 5 B (N0 + 1) h3 (Nat.le_of_ble_eq_true rfl) (Nat.zero_lt_succ N0)
    have ub0 : 2 * B ≤ 1 * (N0 + 1) := Nat.le_trans h2 (Nat.le_of_eq (by lib_nat))
    have hsg : ∃ d2 : Int, (d2 = 1 ∨ d2 = -1) ∧ r2 = d2 * Int.ofNat B := by
      rcases EisEuclid.sgn r2 with e | e
      · exact ⟨1, Or.inl rfl, by rw [e, hB]; lib_int⟩
      · exact ⟨-1, Or.inr rfl, by rw [e, hB]; lib_int⟩
    obtain ⟨d2, hd2, hr2⟩ := hsg
    cases Nat.le_total (1 * (N0 + 1)) (2 * B) with
    | inl hge1 =>
      obtain ⟨X1, hX1⟩ := EisEuclid.le_dest _ _ hge1
      have hw1 : Int.natAbs (Int.ofNat (1 + 1) * r2 + -(d2 * Int.ofNat 1 * Int.ofNat (N0 + 1))) = X1 := Euc67.absw d2 hd2 _ X1 (Or.inl (by rw [hr2]; exact wpos (1 + 1) 1 X1 B N0 d2 hX1))
      cases Nat.lt_or_ge (163 * (X1 * X1)) (3 * ((N0 + 1) * (N0 + 1))) with
      | inl hb1 => exact branchG N0 r1 r2 (d2 * Int.ofNat 1) 1 X1 hw1 hb1 (rs2_1 d2 hd2)
      | inr hc1 =>
        have lo1 : (37 * 1 + 5) * (N0 + 1) < (37 * 2) * B := lb_from 2 1 37 5 X1 B (N0 + 1) hX1 (Euc67.lin_lb 163 3 37 5 X1 (N0 + 1) hc1 (Nat.le_of_ble_eq_true rfl) (Nat.zero_lt_succ N0))
        exact (contra (37 * 1 + 5) (37 * 2) 2 1 (N0 + 1) B lo1 ub0 (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)).elim
    | inr hle1 =>
      obtain ⟨Y1, hY1⟩ := EisEuclid.le_dest _ _ hle1
      have hv1 : Int.natAbs (Int.ofNat (1 + 1) * r2 + -(d2 * Int.ofNat 1 * Int.ofNat (N0 + 1))) = Y1 := Euc67.absw d2 hd2 _ Y1 (Or.inr (by rw [hr2]; exact wneg (1 + 1) 1 Y1 B N0 d2 hY1))
      cases Nat.lt_or_ge (163 * (Y1 * Y1)) (3 * ((N0 + 1) * (N0 + 1))) with
      | inl hf1 => exact branchG N0 r1 r2 (d2 * Int.ofNat 1) 1 Y1 hv1 hf1 (rs2_1 d2 hd2)
      | inr he1 =>
        have up1 : (37 * 2) * B ≤ 32 * (N0 + 1) := Nat.le_of_lt (ub_from 2 1 37 5 Y1 B (N0 + 1) 32 hY1 (Euc67.lin_lb 163 3 37 5 Y1 (N0 + 1) he1 (Nat.le_of_ble_eq_true rfl) (Nat.zero_lt_succ N0)) rfl)
        cases Nat.le_total (1 * (N0 + 1)) (3 * B) with
        | inl hge2 =>
          obtain ⟨X2, hX2⟩ := EisEuclid.le_dest _ _ hge2
          have hw2 : Int.natAbs (Int.ofNat (2 + 1) * r2 + -(d2 * Int.ofNat 1 * Int.ofNat (N0 + 1))) = X2 := Euc67.absw d2 hd2 _ X2 (Or.inl (by rw [hr2]; exact wpos (2 + 1) 1 X2 B N0 d2 hX2))
          cases Nat.lt_or_ge (163 * (X2 * X2)) (3 * ((N0 + 1) * (N0 + 1))) with
          | inl hb2 => exact branchG N0 r1 r2 (d2 * Int.ofNat 1) 2 X2 hw2 hb2 (rs3_1 d2 hd2)
          | inr hc2 =>
            have lo2 : (37 * 1 + 5) * (N0 + 1) < (37 * 3) * B := lb_from 3 1 37 5 X2 B (N0 + 1) hX2 (Euc67.lin_lb 163 3 37 5 X2 (N0 + 1) hc2 (Nat.le_of_ble_eq_true rfl) (Nat.zero_lt_succ N0))
            cases Nat.le_total (2 * (N0 + 1)) (5 * B) with
            | inl hge3 =>
              obtain ⟨X3, hX3⟩ := EisEuclid.le_dest _ _ hge3
              have hw3 : Int.natAbs (Int.ofNat (4 + 1) * r2 + -(d2 * Int.ofNat 2 * Int.ofNat (N0 + 1))) = X3 := Euc67.absw d2 hd2 _ X3 (Or.inl (by rw [hr2]; exact wpos (4 + 1) 2 X3 B N0 d2 hX3))
              cases Nat.lt_or_ge (163 * (X3 * X3)) (3 * ((N0 + 1) * (N0 + 1))) with
              | inl hb3 => exact branchG N0 r1 r2 (d2 * Int.ofNat 2) 4 X3 hw3 hb3 (rs5_2 d2 hd2)
              | inr hc3 =>
                have lo3 : (37 * 2 + 5) * (N0 + 1) < (37 * 5) * B := lb_from 5 2 37 5 X3 B (N0 + 1) hX3 (Euc67.lin_lb 163 3 37 5 X3 (N0 + 1) hc3 (Nat.le_of_ble_eq_true rfl) (Nat.zero_lt_succ N0))
                cases Nat.le_total (3 * (N0 + 1)) (7 * B) with
                | inl hge4 =>
                  obtain ⟨X4, hX4⟩ := EisEuclid.le_dest _ _ hge4
                  have hw4 : Int.natAbs (Int.ofNat (6 + 1) * r2 + -(d2 * Int.ofNat 3 * Int.ofNat (N0 + 1))) = X4 := Euc67.absw d2 hd2 _ X4 (Or.inl (by rw [hr2]; exact wpos (6 + 1) 3 X4 B N0 d2 hX4))
                  cases Nat.lt_or_ge (163 * (X4 * X4)) (3 * ((N0 + 1) * (N0 + 1))) with
                  | inl hb4 => exact branchG N0 r1 r2 (d2 * Int.ofNat 3) 6 X4 hw4 hb4 (rs7_3 d2 hd2)
                  | inr hc4 =>
                    have lo4 : (37 * 3 + 5) * (N0 + 1) < (37 * 7) * B := lb_from 7 3 37 5 X4 B (N0 + 1) hX4 (Euc67.lin_lb 163 3 37 5 X4 (N0 + 1) hc4 (Nat.le_of_ble_eq_true rfl) (Nat.zero_lt_succ N0))
                    exact (contra (37 * 3 + 5) (37 * 7) (37 * 2) 32 (N0 + 1) B lo4 up1 (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)).elim
                | inr hle4 =>
                  obtain ⟨Y4, hY4⟩ := EisEuclid.le_dest _ _ hle4
                  have hv4 : Int.natAbs (Int.ofNat (6 + 1) * r2 + -(d2 * Int.ofNat 3 * Int.ofNat (N0 + 1))) = Y4 := Euc67.absw d2 hd2 _ Y4 (Or.inr (by rw [hr2]; exact wneg (6 + 1) 3 Y4 B N0 d2 hY4))
                  cases Nat.lt_or_ge (163 * (Y4 * Y4)) (3 * ((N0 + 1) * (N0 + 1))) with
                  | inl hf4 => exact branchG N0 r1 r2 (d2 * Int.ofNat 3) 6 Y4 hv4 hf4 (rs7_3 d2 hd2)
                  | inr he4 =>
                    have up4 : (37 * 7) * B ≤ 106 * (N0 + 1) := Nat.le_of_lt (ub_from 7 3 37 5 Y4 B (N0 + 1) 106 hY4 (Euc67.lin_lb 163 3 37 5 Y4 (N0 + 1) he4 (Nat.le_of_ble_eq_true rfl) (Nat.zero_lt_succ N0)) rfl)
                    exact (contra (37 * 2 + 5) (37 * 5) (37 * 7) 106 (N0 + 1) B lo3 up4 (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)).elim
            | inr hle3 =>
              obtain ⟨Y3, hY3⟩ := EisEuclid.le_dest _ _ hle3
              have hv3 : Int.natAbs (Int.ofNat (4 + 1) * r2 + -(d2 * Int.ofNat 2 * Int.ofNat (N0 + 1))) = Y3 := Euc67.absw d2 hd2 _ Y3 (Or.inr (by rw [hr2]; exact wneg (4 + 1) 2 Y3 B N0 d2 hY3))
              cases Nat.lt_or_ge (163 * (Y3 * Y3)) (3 * ((N0 + 1) * (N0 + 1))) with
              | inl hf3 => exact branchG N0 r1 r2 (d2 * Int.ofNat 2) 4 Y3 hv3 hf3 (rs5_2 d2 hd2)
              | inr he3 =>
                have up3 : (37 * 5) * B ≤ 69 * (N0 + 1) := Nat.le_of_lt (ub_from 5 2 37 5 Y3 B (N0 + 1) 69 hY3 (Euc67.lin_lb 163 3 37 5 Y3 (N0 + 1) he3 (Nat.le_of_ble_eq_true rfl) (Nat.zero_lt_succ N0)) rfl)
                exact (contra (37 * 1 + 5) (37 * 3) (37 * 5) 69 (N0 + 1) B lo2 up3 (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)).elim
        | inr hle2 =>
          obtain ⟨Y2, hY2⟩ := EisEuclid.le_dest _ _ hle2
          have hv2 : Int.natAbs (Int.ofNat (2 + 1) * r2 + -(d2 * Int.ofNat 1 * Int.ofNat (N0 + 1))) = Y2 := Euc67.absw d2 hd2 _ Y2 (Or.inr (by rw [hr2]; exact wneg (2 + 1) 1 Y2 B N0 d2 hY2))
          cases Nat.lt_or_ge (163 * (Y2 * Y2)) (3 * ((N0 + 1) * (N0 + 1))) with
          | inl hf2 => exact branchG N0 r1 r2 (d2 * Int.ofNat 1) 2 Y2 hv2 hf2 (rs3_1 d2 hd2)
          | inr he2 =>
            have up2 : (37 * 3) * B ≤ 32 * (N0 + 1) := Nat.le_of_lt (ub_from 3 1 37 5 Y2 B (N0 + 1) 32 hY2 (Euc67.lin_lb 163 3 37 5 Y2 (N0 + 1) he2 (Nat.le_of_ble_eq_true rfl) (Nat.zero_lt_succ N0)) rfl)
            cases Nat.le_total (1 * (N0 + 1)) (4 * B) with
            | inl hge5 =>
              obtain ⟨X5, hX5⟩ := EisEuclid.le_dest _ _ hge5
              have hw5 : Int.natAbs (Int.ofNat (3 + 1) * r2 + -(d2 * Int.ofNat 1 * Int.ofNat (N0 + 1))) = X5 := Euc67.absw d2 hd2 _ X5 (Or.inl (by rw [hr2]; exact wpos (3 + 1) 1 X5 B N0 d2 hX5))
              cases Nat.lt_or_ge (163 * (X5 * X5)) (3 * ((N0 + 1) * (N0 + 1))) with
              | inl hb5 => exact branchG N0 r1 r2 (d2 * Int.ofNat 1) 3 X5 hw5 hb5 (rs4_1 d2 hd2)
              | inr hc5 =>
                have lo5 : (37 * 1 + 5) * (N0 + 1) < (37 * 4) * B := lb_from 4 1 37 5 X5 B (N0 + 1) hX5 (Euc67.lin_lb 163 3 37 5 X5 (N0 + 1) hc5 (Nat.le_of_ble_eq_true rfl) (Nat.zero_lt_succ N0))
                cases Nat.le_total (2 * (N0 + 1)) (7 * B) with
                | inl hge6 =>
                  obtain ⟨X6, hX6⟩ := EisEuclid.le_dest _ _ hge6
                  have hw6 : Int.natAbs (Int.ofNat (6 + 1) * r2 + -(d2 * Int.ofNat 2 * Int.ofNat (N0 + 1))) = X6 := Euc67.absw d2 hd2 _ X6 (Or.inl (by rw [hr2]; exact wpos (6 + 1) 2 X6 B N0 d2 hX6))
                  cases Nat.lt_or_ge (163 * (X6 * X6)) (3 * ((N0 + 1) * (N0 + 1))) with
                  | inl hb6 => exact branchG N0 r1 r2 (d2 * Int.ofNat 2) 6 X6 hw6 hb6 (rs7_2 d2 hd2)
                  | inr hc6 =>
                    have lo6 : (37 * 2 + 5) * (N0 + 1) < (37 * 7) * B := lb_from 7 2 37 5 X6 B (N0 + 1) hX6 (Euc67.lin_lb 163 3 37 5 X6 (N0 + 1) hc6 (Nat.le_of_ble_eq_true rfl) (Nat.zero_lt_succ N0))
                    exact (contra (37 * 2 + 5) (37 * 7) (37 * 3) 32 (N0 + 1) B lo6 up2 (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)).elim
                | inr hle6 =>
                  obtain ⟨Y6, hY6⟩ := EisEuclid.le_dest _ _ hle6
                  have hv6 : Int.natAbs (Int.ofNat (6 + 1) * r2 + -(d2 * Int.ofNat 2 * Int.ofNat (N0 + 1))) = Y6 := Euc67.absw d2 hd2 _ Y6 (Or.inr (by rw [hr2]; exact wneg (6 + 1) 2 Y6 B N0 d2 hY6))
                  cases Nat.lt_or_ge (163 * (Y6 * Y6)) (3 * ((N0 + 1) * (N0 + 1))) with
                  | inl hf6 => exact branchG N0 r1 r2 (d2 * Int.ofNat 2) 6 Y6 hv6 hf6 (rs7_2 d2 hd2)
                  | inr he6 =>
                    have up6 : (37 * 7) * B ≤ 69 * (N0 + 1) := Nat.le_of_lt (ub_from 7 2 37 5 Y6 B (N0 + 1) 69 hY6 (Euc67.lin_lb 163 3 37 5 Y6 (N0 + 1) he6 (Nat.le_of_ble_eq_true rfl) (Nat.zero_lt_succ N0)) rfl)
                    exact (contra (37 * 1 + 5) (37 * 4) (37 * 7) 69 (N0 + 1) B lo5 up6 (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)).elim
            | inr hle5 =>
              obtain ⟨Y5, hY5⟩ := EisEuclid.le_dest _ _ hle5
              have hv5 : Int.natAbs (Int.ofNat (3 + 1) * r2 + -(d2 * Int.ofNat 1 * Int.ofNat (N0 + 1))) = Y5 := Euc67.absw d2 hd2 _ Y5 (Or.inr (by rw [hr2]; exact wneg (3 + 1) 1 Y5 B N0 d2 hY5))
              cases Nat.lt_or_ge (163 * (Y5 * Y5)) (3 * ((N0 + 1) * (N0 + 1))) with
              | inl hf5 => exact branchG N0 r1 r2 (d2 * Int.ofNat 1) 3 Y5 hv5 hf5 (rs4_1 d2 hd2)
              | inr he5 =>
                have up5 : (37 * 4) * B ≤ 32 * (N0 + 1) := Nat.le_of_lt (ub_from 4 1 37 5 Y5 B (N0 + 1) 32 hY5 (Euc67.lin_lb 163 3 37 5 Y5 (N0 + 1) he5 (Nat.le_of_ble_eq_true rfl) (Nat.zero_lt_succ N0)) rfl)
                cases Nat.le_total (1 * (N0 + 1)) (5 * B) with
                | inl hge7 =>
                  obtain ⟨X7, hX7⟩ := EisEuclid.le_dest _ _ hge7
                  have hw7 : Int.natAbs (Int.ofNat (4 + 1) * r2 + -(d2 * Int.ofNat 1 * Int.ofNat (N0 + 1))) = X7 := Euc67.absw d2 hd2 _ X7 (Or.inl (by rw [hr2]; exact wpos (4 + 1) 1 X7 B N0 d2 hX7))
                  cases Nat.lt_or_ge (163 * (X7 * X7)) (3 * ((N0 + 1) * (N0 + 1))) with
                  | inl hb7 => exact branchG N0 r1 r2 (d2 * Int.ofNat 1) 4 X7 hw7 hb7 (rs5_1 d2 hd2)
                  | inr hc7 =>
                    have lo7 : (37 * 1 + 5) * (N0 + 1) < (37 * 5) * B := lb_from 5 1 37 5 X7 B (N0 + 1) hX7 (Euc67.lin_lb 163 3 37 5 X7 (N0 + 1) hc7 (Nat.le_of_ble_eq_true rfl) (Nat.zero_lt_succ N0))
                    exact (contra (37 * 1 + 5) (37 * 5) (37 * 4) 32 (N0 + 1) B lo7 up5 (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)).elim
                | inr hle7 =>
                  obtain ⟨Y7, hY7⟩ := EisEuclid.le_dest _ _ hle7
                  have hv7 : Int.natAbs (Int.ofNat (4 + 1) * r2 + -(d2 * Int.ofNat 1 * Int.ofNat (N0 + 1))) = Y7 := Euc67.absw d2 hd2 _ Y7 (Or.inr (by rw [hr2]; exact wneg (4 + 1) 1 Y7 B N0 d2 hY7))
                  cases Nat.lt_or_ge (163 * (Y7 * Y7)) (3 * ((N0 + 1) * (N0 + 1))) with
                  | inl hf7 => exact branchG N0 r1 r2 (d2 * Int.ofNat 1) 4 Y7 hv7 hf7 (rs5_1 d2 hd2)
                  | inr he7 =>
                    have up7 : (37 * 5) * B ≤ 32 * (N0 + 1) := Nat.le_of_lt (ub_from 5 1 37 5 Y7 B (N0 + 1) 32 hY7 (Euc67.lin_lb 163 3 37 5 Y7 (N0 + 1) he7 (Nat.le_of_ble_eq_true rfl) (Nat.zero_lt_succ N0)) rfl)
                    cases Nat.le_total (1 * (N0 + 1)) (6 * B) with
                    | inl hge8 =>
                      obtain ⟨X8, hX8⟩ := EisEuclid.le_dest _ _ hge8
                      have hw8 : Int.natAbs (Int.ofNat (5 + 1) * r2 + -(d2 * Int.ofNat 1 * Int.ofNat (N0 + 1))) = X8 := Euc67.absw d2 hd2 _ X8 (Or.inl (by rw [hr2]; exact wpos (5 + 1) 1 X8 B N0 d2 hX8))
                      cases Nat.lt_or_ge (163 * (X8 * X8)) (3 * ((N0 + 1) * (N0 + 1))) with
                      | inl hb8 => exact branchG N0 r1 r2 (d2 * Int.ofNat 1) 5 X8 hw8 hb8 (rs6_1 d2 hd2)
                      | inr hc8 =>
                        have lo8 : (37 * 1 + 5) * (N0 + 1) < (37 * 6) * B := lb_from 6 1 37 5 X8 B (N0 + 1) hX8 (Euc67.lin_lb 163 3 37 5 X8 (N0 + 1) hc8 (Nat.le_of_ble_eq_true rfl) (Nat.zero_lt_succ N0))
                        exact (contra (37 * 1 + 5) (37 * 6) (37 * 5) 32 (N0 + 1) B lo8 up7 (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)).elim
                    | inr hle8 =>
                      obtain ⟨Y8, hY8⟩ := EisEuclid.le_dest _ _ hle8
                      have hv8 : Int.natAbs (Int.ofNat (5 + 1) * r2 + -(d2 * Int.ofNat 1 * Int.ofNat (N0 + 1))) = Y8 := Euc67.absw d2 hd2 _ Y8 (Or.inr (by rw [hr2]; exact wneg (5 + 1) 1 Y8 B N0 d2 hY8))
                      cases Nat.lt_or_ge (163 * (Y8 * Y8)) (3 * ((N0 + 1) * (N0 + 1))) with
                      | inl hf8 => exact branchG N0 r1 r2 (d2 * Int.ofNat 1) 5 Y8 hv8 hf8 (rs6_1 d2 hd2)
                      | inr he8 =>
                        have up8 : (37 * 6) * B ≤ 32 * (N0 + 1) := Nat.le_of_lt (ub_from 6 1 37 5 Y8 B (N0 + 1) 32 hY8 (Euc67.lin_lb 163 3 37 5 Y8 (N0 + 1) he8 (Nat.le_of_ble_eq_true rfl) (Nat.zero_lt_succ N0)) rfl)
                        cases Nat.le_total (1 * (N0 + 1)) (7 * B) with
                        | inl hge9 =>
                          obtain ⟨X9, hX9⟩ := EisEuclid.le_dest _ _ hge9
                          have hw9 : Int.natAbs (Int.ofNat (6 + 1) * r2 + -(d2 * Int.ofNat 1 * Int.ofNat (N0 + 1))) = X9 := Euc67.absw d2 hd2 _ X9 (Or.inl (by rw [hr2]; exact wpos (6 + 1) 1 X9 B N0 d2 hX9))
                          cases Nat.lt_or_ge (163 * (X9 * X9)) (3 * ((N0 + 1) * (N0 + 1))) with
                          | inl hb9 => exact branchG N0 r1 r2 (d2 * Int.ofNat 1) 6 X9 hw9 hb9 (rs7_1 d2 hd2)
                          | inr hc9 =>
                            have lo9 : (37 * 1 + 5) * (N0 + 1) < (37 * 7) * B := lb_from 7 1 37 5 X9 B (N0 + 1) hX9 (Euc67.lin_lb 163 3 37 5 X9 (N0 + 1) hc9 (Nat.le_of_ble_eq_true rfl) (Nat.zero_lt_succ N0))
                            exact (contra (37 * 1 + 5) (37 * 7) (37 * 6) 32 (N0 + 1) B lo9 up8 (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)).elim
                        | inr hle9 =>
                          obtain ⟨Y9, hY9⟩ := EisEuclid.le_dest _ _ hle9
                          have hv9 : Int.natAbs (Int.ofNat (6 + 1) * r2 + -(d2 * Int.ofNat 1 * Int.ofNat (N0 + 1))) = Y9 := Euc67.absw d2 hd2 _ Y9 (Or.inr (by rw [hr2]; exact wneg (6 + 1) 1 Y9 B N0 d2 hY9))
                          cases Nat.lt_or_ge (163 * (Y9 * Y9)) (3 * ((N0 + 1) * (N0 + 1))) with
                          | inl hf9 => exact branchG N0 r1 r2 (d2 * Int.ofNat 1) 6 Y9 hv9 hf9 (rs7_1 d2 hd2)
                          | inr he9 =>
                            have up9 : (37 * 7) * B ≤ 32 * (N0 + 1) := Nat.le_of_lt (ub_from 7 1 37 5 Y9 B (N0 + 1) 32 hY9 (Euc67.lin_lb 163 3 37 5 Y9 (N0 + 1) he9 (Nat.le_of_ble_eq_true rfl) (Nat.zero_lt_succ N0)) rfl)
                            exact (contra 5 37 (37 * 7) 32 (N0 + 1) B lb0 up9 (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)).elim

end Euc163

#print axioms Euc163.nrm_mulD
#print axioms Euc163.nrm_scal
#print axioms Euc163.natRes
#print axioms Euc163.resN
#print axioms Euc163.resc_gen
#print axioms Euc163.shift
#print axioms Euc163.branchG
#print axioms Euc163.lb_from
#print axioms Euc163.ub_from
#print axioms Euc163.contra
#print axioms Euc163.wpos
#print axioms Euc163.wneg
#print axioms Euc163.rs2_1
#print axioms Euc163.rs3_1
#print axioms Euc163.rs5_2
#print axioms Euc163.rs7_3
#print axioms Euc163.rs4_1
#print axioms Euc163.rs7_2
#print axioms Euc163.rs5_1
#print axioms Euc163.rs6_1
#print axioms Euc163.rs7_1
#print axioms Euc163.dh163_r

lang/DH43.lean

lean_all/lang/DH43.lean

/-
  lang/DH43 — T48/T50: **the Dedekind–Hasse step in ℤ[(1 + √−43)/2]** (not norm-Euclidean): from a two-step remainder r
  (|2r₁ + r₂|, 2|r₂| ≤ N) there are s and e with N(sr − eN) < N², and sr − eN = 0 only for s = 1:
  s = 1 if 4N(r) < 4N²; else (3N² ≤ 43B², B = |r₂|, 2B + u = N) s = 2 if 43u² < 3N², s = 3 otherwise (43(B − u)² < 3N²);
  zero remainders at half and third points are rescued by θ − 1, −θ, θ − 2 (half43, third43; table computed numerically).
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Euc43
import lang.Euc19
import lang.EisPrim

namespace Euc43

open FTAChain DetChain LibInt

theorem sq_le' (T N : Nat) (h : T ≤ N) : T * T ≤ N * N := Nat.mul_le_mul h h

theorem sq_lt (n N : Nat) (h : n < N) : n * n < N * N := by
  cases Nat.lt_or_ge (n * n) (N * N) with
  | inl h' => exact h'
  | inr h' =>
    exfalso
    have : (n + 1) * (n + 1) ≤ N * N := Nat.mul_le_mul h h
    have : (2 * n + 1) + n * n ≤ 0 + n * n := Nat.le_trans (Nat.le_of_eq (by lib_nat)) (Nat.le_trans this (Nat.le_trans h' (Nat.le_of_eq (by lib_nat))))
    exact Nat.not_succ_le_zero _ (GaussEuclid.le_cancel _ _ _ this)

theorem nrm_pair (n : Nat) : nrm (Int.ofNat n, 0) = n * n := Int.ofNat.inj (by rw [nrm_cast]; show Int.ofNat n * Int.ofNat n + Int.ofNat n * 0 + 11 * (0 * 0) = Int.ofNat n * Int.ofNat n; lib_int)

theorem zero_sq (x : Nat) (k : Nat) (h : (k + 1) * (x * x) ≤ 0) : x = 0 := by
  have h1 : (k + 1) * (x * x) = 0 := Nat.le_antisymm h (Nat.zero_le _)
  have h2 : x * x = 0 := by cases nat_mul_zero _ _ h1 with | inl e => exact Nat.noConfusion e | inr e => exact e
  cases nat_mul_zero _ _ h2 with | inl e => exact e | inr e => exact e

/-- u ≤ B (B = u + v, N = 3u + 2v): 3N² ≤ 43u² ⇒ 43v² < 3N². -/
theorem ineq43c (u v : Nat) (hu : 3 * ((3 * u + 2 * v) * (3 * u + 2 * v)) ≤ 43 * (u * u)) (hpos : 0 < 3 * u + 2 * v) :
    43 * (v * v) < 3 * ((3 * u + 2 * v) * (3 * u + 2 * v)) := by
  cases Nat.lt_or_ge (43 * (v * v)) (3 * ((3 * u + 2 * v) * (3 * u + 2 * v))) with
  | inl h => exact h
  | inr hc =>
    exfalso
    have h1 : 36 * (u * v) + 12 * (v * v) ≤ 16 * (u * u) := GaussEuclid.le_cancel _ _ (27 * (u * u)) (by
      calc 36 * (u * v) + 12 * (v * v) + 27 * (u * u) = 3 * ((3 * u + 2 * v) * (3 * u + 2 * v)) := by lib_nat
        _ ≤ 43 * (u * u) := hu
        _ = 16 * (u * u) + 27 * (u * u) := by lib_nat)
    have h2 : 27 * (u * u) + 36 * (u * v) ≤ 31 * (v * v) := GaussEuclid.le_cancel _ _ (12 * (v * v)) (by
      calc 27 * (u * u) + 36 * (u * v) + 12 * (v * v) = 3 * ((3 * u + 2 * v) * (3 * u + 2 * v)) := by lib_nat
        _ ≤ 43 * (v * v) := hc
        _ = 31 * (v * v) + 12 * (v * v) := by lib_nat)
    have u0 : u = 0 := by
      cases Nat.le_total v u with
      | inl hvu =>
        have a : 31 * (v * v) ≤ 31 * (u * v) := Nat.mul_le_mul_left 31 (Nat.le_trans (Nat.mul_le_mul_right v hvu) (Nat.le_of_eq rfl))
        have b : 27 * (u * u) + 36 * (u * v) ≤ 0 + 31 * (u * v) := Nat.le_trans h2 (Nat.le_trans a (Nat.le_of_eq (by lib_nat)))
        have c : 27 * (u * u) + 5 * (u * v) + 31 * (u * v) ≤ 0 + 31 * (u * v) := Nat.le_trans (Nat.le_of_eq (by lib_nat)) b
        have d := GaussEuclid.le_cancel _ _ _ c
        exact zero_sq u 26 (Nat.le_trans (Nat.le_add_right _ _) d)
      | inr huv =>
        have a : 36 * (u * u) ≤ 36 * (u * v) := Nat.mul_le_mul_left 36 (Nat.mul_le_mul_left u huv)
        have b : 36 * (u * u) ≤ 16 * (u * u) := Nat.le_trans a (Nat.le_trans (Nat.le_add_right _ _) h1)
        have c : 20 * (u * u) + 16 * (u * u) ≤ 0 + 16 * (u * u) := Nat.le_trans (Nat.le_of_eq (by lib_nat)) (Nat.le_trans b (Nat.le_of_eq (by lib_nat)))
        exact zero_sq u 19 (GaussEuclid.le_cancel _ _ _ c)
    subst u0
    have v0 : v = 0 := zero_sq v 11 (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_trans h1 (Nat.le_of_eq (by lib_nat))))
    subst v0; exact Nat.lt_irrefl 0 hpos

/-- B ≤ u (u = B + v, N = 3B + v): 3N² ≤ 43B² ⇒ 43v² < 3N². -/
theorem ineq43d (B v : Nat) (hB : 3 * ((3 * B + v) * (3 * B + v)) ≤ 43 * (B * B)) (hpos : 0 < 3 * B + v) :
    43 * (v * v) < 3 * ((3 * B + v) * (3 * B + v)) := by
  cases Nat.lt_or_ge (43 * (v * v)) (3 * ((3 * B + v) * (3 * B + v))) with
  | inl h => exact h
  | inr hc =>
    exfalso
    have h1 : 18 * (B * v) + 3 * (v * v) ≤ 16 * (B * B) := GaussEuclid.le_cancel _ _ (27 * (B * B)) (by
      calc 18 * (B * v) + 3 * (v * v) + 27 * (B * B) = 3 * ((3 * B + v) * (3 * B + v)) := by lib_nat
        _ ≤ 43 * (B * B) := hB
        _ = 16 * (B * B) + 27 * (B * B) := by lib_nat)
    have h2 : 27 * (B * B) + 18 * (B * v) ≤ 40 * (v * v) := GaussEuclid.le_cancel _ _ (3 * (v * v)) (by
      calc 27 * (B * B) + 18 * (B * v) + 3 * (v * v) = 3 * ((3 * B + v) * (3 * B + v)) := by lib_nat
        _ ≤ 43 * (v * v) := hc
        _ = 40 * (v * v) + 3 * (v * v) := by lib_nat)
    have B0 : B = 0 := by
      cases Nat.le_total v B with
      | inl hvB =>
        have a : 40 * (v * v) ≤ 40 * (B * v) := Nat.mul_le_mul_left 40 (Nat.mul_le_mul_right v hvB)
        have b : 27 * (B * B) + 18 * (B * v) ≤ 0 + 40 * (B * v) := Nat.le_trans h2 (Nat.le_trans a (Nat.le_of_eq (by lib_nat)))
        have c : 27 * (B * B) + 18 * (B * v) ≤ 0 + 40 * (B * B) := Nat.le_trans b (Nat.add_le_add_left (Nat.mul_le_mul_left 40 (Nat.mul_le_mul_left B hvB)) _)
        -- 27B² + 18Bv ≤ 40 Bv  ⇒ 27 B² ≤ 22 Bv ≤ 22 B²
        have d : 27 * (B * B) + 18 * (B * v) ≤ 22 * (B * v) + 18 * (B * v) := Nat.le_trans b (Nat.le_of_eq (by lib_nat))
        have e := GaussEuclid.le_cancel _ _ _ d
        have f : 27 * (B * B) ≤ 22 * (B * B) := Nat.le_trans e (Nat.mul_le_mul_left 22 (Nat.mul_le_mul_left B hvB))
        have g : 5 * (B * B) + 22 * (B * B) ≤ 0 + 22 * (B * B) := Nat.le_trans (Nat.le_of_eq (by lib_nat)) (Nat.le_trans f (Nat.le_of_eq (by lib_nat)))
        exact zero_sq B 4 (GaussEuclid.le_cancel _ _ _ g)
      | inr hBv =>
        have a : 18 * (B * B) ≤ 18 * (B * v) := Nat.mul_le_mul_left 18 (Nat.mul_le_mul_left B hBv)
        have b : 18 * (B * B) ≤ 16 * (B * B) := Nat.le_trans a (Nat.le_trans (Nat.le_add_right _ _) h1)
        have c : 2 * (B * B) + 16 * (B * B) ≤ 0 + 16 * (B * B) := Nat.le_trans (Nat.le_of_eq (by lib_nat)) (Nat.le_trans b (Nat.le_of_eq (by lib_nat)))
        exact zero_sq B 1 (GaussEuclid.le_cancel _ _ _ c)
    subst B0
    have v0 : v = 0 := zero_sq v 2 (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_trans h1 (Nat.le_of_eq (by lib_nat))))
    subst v0; exact Nat.lt_irrefl 0 hpos

theorem half43 (B : Nat) (d1 d2 N : Int) (hNB : N = 2 * Int.ofNat B) (hd2 : d2 = 1 ∨ d2 = -1) :
    ∃ s e : Int × Int, (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s (d1 * Int.ofNat B, d2 * Int.ofNat B)) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul e (N, 0))) = (Int.ofNat B, 0) := by
  rcases GaussInt.int_even_odd d1 with ⟨k, hk⟩ | ⟨k, hk⟩ <;> rcases hd2 with e2 | e2
  · refine ⟨(-1, 1), (k * -1 + -6, k * 1 + 0), ?_⟩
    apply Prod.ext
    · show -1 * (d1 * Int.ofNat B) + -(1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B)) + -((k * -1 + -6) * N + -((k * 1 + 0) * 0 + (k * 1 + 0) * 0 + (k * 1 + 0) * 0 + (k * 1   -- (+150 chars; full line in the Zenodo source)
      rw [hk, e2, hNB]; lib_int
    · show -1 * (d2 * Int.ofNat B) + 1 * (d1 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + -((k * -1 + -6) * 0 + (k * 1 + 0) * N + (k * 1 + 0) * 0) = 0
      rw [hk, e2, hNB]; lib_int
  · refine ⟨(-1, 1), (k * -1 + 5, k * 1 + 0), ?_⟩
    apply Prod.ext
    · show -1 * (d1 * Int.ofNat B) + -(1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B)) + -((k * -1 + 5) * N + -((k * 1 + 0) * 0 + (k * 1 + 0) * 0 + (k * 1 + 0) * 0 + (k * 1 +  -- (+149 chars; full line in the Zenodo source)
      rw [hk, e2, hNB]; lib_int
    · show -1 * (d2 * Int.ofNat B) + 1 * (d1 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + -((k * -1 + 5) * 0 + (k * 1 + 0) * N + (k * 1 + 0) * 0) = 0
      rw [hk, e2, hNB]; lib_int
  · refine ⟨(0, -1), (k * 0 + 5, k * -1 + -1), ?_⟩
    apply Prod.ext
    · show 0 * (d1 * Int.ofNat B) + -(-1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B)) + -((k * 0 + 5) * N + -((k * -1 + -1) * 0 + (k * -1 + -1) * 0 + (k * -1 + -1)  -- (+180 chars; full line in the Zenodo source)
      rw [hk, e2, hNB]; lib_int
    · show 0 * (d2 * Int.ofNat B) + -1 * (d1 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -((k * 0 + 5) * 0 + (k * -1 + -1) * N + (k * -1 + -1) * 0) = 0
      rw [hk, e2, hNB]; lib_int
  · refine ⟨(0, -1), (k * 0 + -6, k * -1 + 0), ?_⟩
    apply Prod.ext
    · show 0 * (d1 * Int.ofNat B) + -(-1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B)) + -((k * 0 + -6) * N + -((k * -1 + 0) * 0 + (k * -1 + 0) * 0 + (k * -1 + 0) *  -- (+170 chars; full line in the Zenodo source)
      rw [hk, e2, hNB]; lib_int
    · show 0 * (d2 * Int.ofNat B) + -1 * (d1 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -((k * 0 + -6) * 0 + (k * -1 + 0) * N + (k * -1 + 0) * 0) = 0
      rw [hk, e2, hNB]; lib_int

theorem third43 (B : Nat) (d1 d2 N : Int) (hNB : N = 3 * Int.ofNat B) (hd2 : d2 = 1 ∨ d2 = -1) :
    ∃ s e : Int × Int, ∃ c : Nat, (c = 1 ∨ c = 2) ∧ (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s (d1 * Int.ofNat B, d2 * Int.ofNat B)) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul e (N, 0))) = (Int.ofNat (c * B), 0) := by
  obtain ⟨q, hq | hq | hq⟩ := EisPrim.res3 d1 <;> rcases hd2 with e2 | e2
  · refine ⟨(-1, 1), (q * -1 + -4, q * 1 + 0), 1, Or.inl rfl, ?_⟩
    apply Prod.ext
    · show -1 * (d1 * Int.ofNat B) + -(1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B)) + -((q * -1 + -4) * N + -((q * 1 + 0) * 0 + (q * 1 + 0) * 0 + (q * 1 + 0) * 0 + (q * 1   -- (+156 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 1 * Int.ofNat B; lib_int
    · show -1 * (d2 * Int.ofNat B) + 1 * (d1 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + -((q * -1 + -4) * 0 + (q * 1 + 0) * N + (q * 1 + 0) * 0) = 0
      rw [hq, e2, hNB]; lib_int
  · refine ⟨(-1, 1), (q * -1 + 3, q * 1 + 0), 2, Or.inr rfl, ?_⟩
    apply Prod.ext
    · show -1 * (d1 * Int.ofNat B) + -(1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B)) + -((q * -1 + 3) * N + -((q * 1 + 0) * 0 + (q * 1 + 0) * 0 + (q * 1 + 0) * 0 + (q * 1 +  -- (+155 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 2 * Int.ofNat B; lib_int
    · show -1 * (d2 * Int.ofNat B) + 1 * (d1 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + -((q * -1 + 3) * 0 + (q * 1 + 0) * N + (q * 1 + 0) * 0) = 0
      rw [hq, e2, hNB]; lib_int
  · refine ⟨(-2, 1), (q * -2 + -5, q * 1 + 0), 2, Or.inr rfl, ?_⟩
    apply Prod.ext
    · show -2 * (d1 * Int.ofNat B) + -(1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B)) + -((q * -2 + -5) * N + -((q * 1 + 0) * 0 + (q * 1 + 0) * 0 + (q * 1 + 0) * 0 + (q * 1   -- (+156 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 2 * Int.ofNat B; lib_int
    · show -2 * (d2 * Int.ofNat B) + 1 * (d1 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + -((q * -2 + -5) * 0 + (q * 1 + 0) * N + (q * 1 + 0) * 0) = 0
      rw [hq, e2, hNB]; lib_int
  · refine ⟨(0, -1), (q * 0 + -4, q * -1 + 0), 1, Or.inl rfl, ?_⟩
    apply Prod.ext
    · show 0 * (d1 * Int.ofNat B) + -(-1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B)) + -((q * 0 + -4) * N + -((q * -1 + 0) * 0 + (q * -1 + 0) * 0 + (q * -1 + 0) *  -- (+176 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 1 * Int.ofNat B; lib_int
    · show 0 * (d2 * Int.ofNat B) + -1 * (d1 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -((q * 0 + -4) * 0 + (q * -1 + 0) * N + (q * -1 + 0) * 0) = 0
      rw [hq, e2, hNB]; lib_int
  · refine ⟨(0, -1), ((q + -1) * 0 + 3, (q + -1) * -1 + -1), 2, Or.inr rfl, ?_⟩
    apply Prod.ext
    · show 0 * (d1 * Int.ofNat B) + -(-1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B)) + -(((q + -1) * 0 + 3) * N + -(((q + -1) * -1 + -1) * 0 + ((q + -1) * -1 + -1  -- (+270 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 2 * Int.ofNat B; lib_int
    · show 0 * (d2 * Int.ofNat B) + -1 * (d1 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -(((q + -1) * 0 + 3) * 0 + ((q + -1) * -1 + -1) * N + ((q + -1) * -1 + -1) * 0) = 0
      rw [hq, e2, hNB]; lib_int
  · refine ⟨(-2, 1), ((q + -1) * -2 + 2, (q + -1) * 1 + 1), 1, Or.inl rfl, ?_⟩
    apply Prod.ext
    · show -2 * (d1 * Int.ofNat B) + -(1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B)) + -(((q + -1) * -2 + 2) * N + -(((q + -1) * 1 + 1) * 0 + ((q + -1) * 1 + 1) * 0 + ((q +  -- (+239 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 1 * Int.ofNat B; lib_int
    · show -2 * (d2 * Int.ofNat B) + 1 * (d1 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + -(((q + -1) * -2 + 2) * 0 + ((q + -1) * 1 + 1) * N + ((q + -1) * 1 + 1) * 0) = 0
      rw [hq, e2, hNB]; lib_int

theorem three_cancel (a c : Int) (h : 3 * a = 3 * c) : a = c := by
  have h0 : 3 * (a + -c) = 0 := by rw [show 3 * (a + -c) = 3 * a + -(3 * c) by lib_int, h]; lib_int
  cases int_noZD _ _ h0 with
  | inl e => exact absurd e (fun e => Nat.noConfusion (Int.ofNat.inj e))
  | inr e => calc a = (a + -c) + c := by lib_int
      _ = c := by rw [e]; show (0 : Int) + c = c; lib_int

/-- **The Dedekind–Hasse step on a remainder (d = 43).** -/
theorem dh43_r (N0 : Nat) (r1 r2 : Int) (h2 : 2 * Int.natAbs r2 ≤ N0 + 1) (ht : Int.natAbs (2 * r1 + r2) ≤ N0 + 1) :
    ∃ s e : Int × Int, nrm ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s (r1, r2)) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0)))) < (N0 + 1) * (N0 + 1) ∧
      ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s (r1, r2)) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0))) = (Ring43.ring43 LibInt.intR).zero → s = (Ring43.ring43 LibInt.intR).one) := by
  have h4 := four_nrm (r1, r2)
  generalize hT : Int.natAbs (2 * r1 + r2) = T at h4 ht
  generalize hB : Int.natAbs r2 = B at h4 h2
  have h4' : 4 * nrm (r1, r2) = T * T + 43 * (B * B) := h4
  cases Nat.lt_or_ge (T * T + 43 * (B * B)) (4 * ((N0 + 1) * (N0 + 1))) with
  | inl hl =>
    refine ⟨(Ring43.ring43 LibInt.intR).one, (Ring43.ring43 LibInt.intR).zero, ?_, fun _ => rfl⟩
    have e : (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul (Ring43.ring43 LibInt.intR).one (r1, r2)) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul (Ring43.ring43 LibInt.intR).zero (Int.ofNat (N0 + 1), 0))) = (r1, r2) := by lib_ring
    rw [e]
    exact nrm_lt (r1, r2) (N0 + 1) (by rw [hT, hB]; exact hl)
  | inr hg =>
    have h3 : 3 * ((N0 + 1) * (N0 + 1)) ≤ 43 * (B * B) := GaussEuclid.le_cancel _ _ ((N0 + 1) * (N0 + 1)) (by
      calc 3 * ((N0 + 1) * (N0 + 1)) + (N0 + 1) * (N0 + 1) = 4 * ((N0 + 1) * (N0 + 1)) := by lib_nat
        _ ≤ T * T + 43 * (B * B) := hg
        _ ≤ (N0 + 1) * (N0 + 1) + 43 * (B * B) := Nat.add_le_add_right (sq_le' T _ ht) _
        _ = 43 * (B * B) + (N0 + 1) * (N0 + 1) := by lib_nat)
    obtain ⟨u, hu⟩ := EisEuclid.le_dest _ _ h2
    have hN : N0 + 1 = 2 * B + u := by rw [← hu]; lib_nat
    have hNi : Int.ofNat (N0 + 1) = 2 * Int.ofNat B + Int.ofNat u := congrArg Int.ofNat hN
    have hsg : ∃ d2 : Int, (d2 = 1 ∨ d2 = -1) ∧ r2 = d2 * Int.ofNat B := by
      rcases EisEuclid.sgn r2 with e | e
      · exact ⟨1, Or.inl rfl, by rw [e, hB]; lib_int⟩
      · exact ⟨-1, Or.inr rfl, by rw [e, hB]; lib_int⟩
    obtain ⟨d2, hd2, hr2⟩ := hsg
    have hdd : d2 * d2 = 1 := by rcases hd2 with e | e <;> rw [e] <;> rfl
    have e2N : Int.ofNat (2 * N0 + 1 + 1) = 2 * Int.ofNat (N0 + 1) := by show 2 * Int.ofNat N0 + 1 + 1 = 2 * (Int.ofNat N0 + 1); lib_int
    have hBpos : ∀ c : Nat, c * B = 0 → c = 0 ∨ B = 0 := fun c h => nat_mul_zero c B h
    -- finishing a special case: remainder (cB, 0) with cB < N, B ≠ 0
    have fin : ∀ (sv ev : Int × Int) (n : Nat), n < N0 + 1 → n ≠ 0 →
        (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul sv (r1, r2)) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul ev (Int.ofNat (N0 + 1), 0))) = (Int.ofNat n, 0) →
        ∃ s e : Int × Int, nrm ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s (r1, r2)) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0)))) < (N0 + 1) * (N0 + 1) ∧
          ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s (r1, r2)) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0))) = (Ring43.ring43 LibInt.intR).zero → s = (Ring43.ring43 LibInt.intR).one) :=
      fun sv ev n hn hn0 he => ⟨sv, ev, by rw [he, nrm_pair]; exact sq_lt n _ hn, fun h0 => by
        have : Int.ofNat n = 0 := congrArg Prod.fst (he.symm.trans h0)
        exact absurd (Int.ofNat.inj this) hn0⟩
    cases Nat.lt_or_ge (43 * (u * u)) (3 * ((N0 + 1) * (N0 + 1))) with
    | inl hu2 =>
      -- s = 2
      obtain ⟨d1, t', hrd, hb1⟩ := GaussEuclid.rnd (2 * (2 * r1 + r2) + -(d2 * Int.ofNat (N0 + 1))) (2 * N0 + 1)
      have hrd' : 2 * (2 * r1 + -(d1 * Int.ofNat (N0 + 1))) + (2 * r2 + -(d2 * Int.ofNat (N0 + 1))) = t' := by
        calc 2 * (2 * r1 + -(d1 * Int.ofNat (N0 + 1))) + (2 * r2 + -(d2 * Int.ofNat (N0 + 1))) = 2 * (2 * r1 + r2) + -(d2 * Int.ofNat (N0 + 1)) + -(d1 * (2 * Int.ofNat (N0 + 1))) := by lib_int
          _ = t' := by rw [hrd, e2N]; lib_int
      have hT' : Int.natAbs t' ≤ N0 + 1 := Nat.le_of_mul_le_mul_left (Nat.le_trans hb1 (Nat.le_of_eq (by lib_nat))) (Nat.zero_lt_succ 1)
      have hu' : Int.natAbs (2 * r2 + -(d2 * Int.ofNat (N0 + 1))) = u := by
        rw [hr2, hNi]
        rcases hd2 with e | e <;> rw [e]
        · rw [show 2 * (1 * Int.ofNat B) + -(1 * (2 * Int.ofNat B + Int.ofNat u)) = -Int.ofNat u by lib_int]; exact Int.natAbs_neg _
        · rw [show 2 * (-1 * Int.ofNat B) + -(-1 * (2 * Int.ofNat B + Int.ofNat u)) = Int.ofNat u by lib_int]; rfl
      have hexpr : (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul (2, 0) (r1, r2)) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul (d1, d2) (Int.ofNat (N0 + 1), 0))) = (2 * r1 + -(d1 * Int.ofNat (N0 + 1)), 2 * r2 + -(d2 * Int.ofNat (N0 + 1))) := by
        apply Prod.ext
        · show 2 * r1 + -(0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2) + -(d1 * Int.ofNat (N0 + 1) + -(d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0)) = 2 * r1 + -(d1 * Int.ofNat (N0 + 1)); lib_int
        · show 2 * r2 + 0 * r1 + 0 * r2 + -(d1 * 0 + d2 * Int.ofNat (N0 + 1) + d2 * 0) = 2 * r2 + -(d2 * Int.ofNat (N0 + 1)); lib_int
      by_cases hz : (2 * r1 + -(d1 * Int.ofNat (N0 + 1)), 2 * r2 + -(d2 * Int.ofNat (N0 + 1))) = (Ring43.ring43 LibInt.intR).zero
      · have z1 : 2 * r1 + -(d1 * Int.ofNat (N0 + 1)) = 0 := congrArg Prod.fst hz
        have z2 : 2 * r2 + -(d2 * Int.ofNat (N0 + 1)) = 0 := congrArg Prod.snd hz
        have u0 : u = 0 := by rw [← hu', z2]; rfl
        have hNB : Int.ofNat (N0 + 1) = 2 * Int.ofNat B := by rw [hNi, u0]; show _ + 0 = _; lib_int
        have hr1 : r1 = d1 * Int.ofNat B := W36Mul.two_cancel_int _ _ (by
          calc 2 * r1 = 2 * r1 + -(d1 * Int.ofNat (N0 + 1)) + d1 * Int.ofNat (N0 + 1) := by lib_int
            _ = 2 * (d1 * Int.ofNat B) := by rw [z1, hNB]; lib_int)
        have hB0 : B ≠ 0 := fun e => by rw [e, u0] at hN; exact Nat.noConfusion hN
        obtain ⟨sv, ev, he⟩ := half43 B d1 d2 (Int.ofNat (N0 + 1)) hNB hd2
        rw [← hr1, ← hr2] at he
        exact fin sv ev B (by rw [hN, u0]; show B < 2 * B + 0; rw [show 2 * B + 0 = B + B by lib_nat]; exact Nat.lt_add_of_pos_right (Nat.pos_of_ne_zero hB0)) hB0 he
      · refine ⟨(2, 0), (d1, d2), ?_, fun h0 => absurd (hexpr.symm.trans h0) hz⟩
        rw [hexpr]
        apply nrm_lt
        show Int.natAbs (2 * (2 * r1 + -(d1 * Int.ofNat (N0 + 1))) + (2 * r2 + -(d2 * Int.ofNat (N0 + 1)))) * Int.natAbs (2 * (2 * r1 + -(d1 * Int.ofNat (N0 + 1))) + (2 * r2 + -(d2 * Int.ofNat (N0 + 1))))
          + 43 * (Int.natAbs (2 * r2 + -(d2 * Int.ofNat (N0 + 1))) * Int.natAbs (2 * r2 + -(d2 * Int.ofNat (N0 + 1)))) < _
        rw [hrd', hu']
        calc Int.natAbs t' * Int.natAbs t' + 43 * (u * u) < (N0 + 1) * (N0 + 1) + 3 * ((N0 + 1) * (N0 + 1)) := Nat.add_lt_add_of_le_of_lt (sq_le' _ _ hT') hu2
          _ = 4 * ((N0 + 1) * (N0 + 1)) := by lib_nat
    | inr hu3 =>
      -- s = 3
      obtain ⟨d1, t', hrd, hb1⟩ := GaussEuclid.rnd (3 * (2 * r1 + r2) + -(d2 * Int.ofNat (N0 + 1))) (2 * N0 + 1)
      have hrd' : 2 * (3 * r1 + -(d1 * Int.ofNat (N0 + 1))) + (3 * r2 + -(d2 * Int.ofNat (N0 + 1))) = t' := by
        calc 2 * (3 * r1 + -(d1 * Int.ofNat (N0 + 1))) + (3 * r2 + -(d2 * Int.ofNat (N0 + 1))) = 3 * (2 * r1 + r2) + -(d2 * Int.ofNat (N0 + 1)) + -(d1 * (2 * Int.ofNat (N0 + 1))) := by lib_int
          _ = t' := by rw [hrd, e2N]; lib_int
      have hT' : Int.natAbs t' ≤ N0 + 1 := Nat.le_of_mul_le_mul_left (Nat.le_trans hb1 (Nat.le_of_eq (by lib_nat))) (Nat.zero_lt_succ 1)
      -- |3 r2 − d2 N| = |B − u|
      have hw : ∃ v : Nat, Int.natAbs (3 * r2 + -(d2 * Int.ofNat (N0 + 1))) = v ∧ 43 * (v * v) < 3 * ((N0 + 1) * (N0 + 1)) := by
        cases Nat.le_total u B with
        | inl huB =>
          obtain ⟨v, hv⟩ := EisEuclid.le_dest _ _ huB
          refine ⟨v, ?_, ?_⟩
          · rw [hr2, hNi, ← show Int.ofNat (v + u) = Int.ofNat B from congrArg Int.ofNat hv]
            show Int.natAbs (3 * (d2 * (Int.ofNat v + Int.ofNat u)) + -(d2 * (2 * (Int.ofNat v + Int.ofNat u) + Int.ofNat u))) = v
            rcases hd2 with e | e <;> rw [e]
            · rw [show 3 * (1 * (Int.ofNat v + Int.ofNat u)) + -(1 * (2 * (Int.ofNat v + Int.ofNat u) + Int.ofNat u)) = Int.ofNat v by lib_int]; rfl
            · rw [show 3 * (-1 * (Int.ofNat v + Int.ofNat u)) + -(-1 * (2 * (Int.ofNat v + Int.ofNat u) + Int.ofNat u)) = -Int.ofNat v by lib_int]; exact Int.natAbs_neg _
          · rw [hN, ← hv, show 2 * (v + u) + u = 3 * u + 2 * v by lib_nat]
            exact ineq43c u v (by rw [← show 2 * (v + u) + u = 3 * u + 2 * v by lib_nat, hv, ← hN]; exact hu3) (by rw [← show 2 * (v + u) + u = 3 * u + 2 * v by lib_nat, hv, ← hN]; exact Nat.zero_lt_succ N0)
        | inr hBu =>
          obtain ⟨v, hv⟩ := EisEuclid.le_dest _ _ hBu
          refine ⟨v, ?_, ?_⟩
          · rw [hr2, hNi, ← show Int.ofNat (v + B) = Int.ofNat u from congrArg Int.ofNat hv]
            show Int.natAbs (3 * (d2 * Int.ofNat B) + -(d2 * (2 * Int.ofNat B + (Int.ofNat v + Int.ofNat B)))) = v
            rcases hd2 with e | e <;> rw [e]
            · rw [show 3 * (1 * Int.ofNat B) + -(1 * (2 * Int.ofNat B + (Int.ofNat v + Int.ofNat B))) = -Int.ofNat v by lib_int]; exact Int.natAbs_neg _
            · rw [show 3 * (-1 * Int.ofNat B) + -(-1 * (2 * Int.ofNat B + (Int.ofNat v + Int.ofNat B))) = Int.ofNat v by lib_int]; rfl
          · rw [hN, ← hv, show 2 * B + (v + B) = 3 * B + v by lib_nat]
            exact ineq43d B v (by rw [← show 2 * B + (v + B) = 3 * B + v by lib_nat, hv, ← hN]; exact h3) (by rw [← show 2 * B + (v + B) = 3 * B + v by lib_nat, hv, ← hN]; exact Nat.zero_lt_succ N0)
      obtain ⟨v, hv1, hv2⟩ := hw
      have hexpr : (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul (3, 0) (r1, r2)) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul (d1, d2) (Int.ofNat (N0 + 1), 0))) = (3 * r1 + -(d1 * Int.ofNat (N0 + 1)), 3 * r2 + -(d2 * Int.ofNat (N0 + 1))) := by
        apply Prod.ext
        · show 3 * r1 + -(0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2) + -(d1 * Int.ofNat (N0 + 1) + -(d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0)) = 3 * r1 + -(d1 * Int.ofNat (N0 + 1)); lib_int
        · show 3 * r2 + 0 * r1 + 0 * r2 + -(d1 * 0 + d2 * Int.ofNat (N0 + 1) + d2 * 0) = 3 * r2 + -(d2 * Int.ofNat (N0 + 1)); lib_int
      by_cases hz : (3 * r1 + -(d1 * Int.ofNat (N0 + 1)), 3 * r2 + -(d2 * Int.ofNat (N0 + 1))) = (Ring43.ring43 LibInt.intR).zero
      · have z1 : 3 * r1 + -(d1 * Int.ofNat (N0 + 1)) = 0 := congrArg Prod.fst hz
        have z2 : 3 * r2 + -(d2 * Int.ofNat (N0 + 1)) = 0 := congrArg Prod.snd hz
        -- N = 3B, r1 = d1 B
        have hN3 : Int.ofNat (N0 + 1) = 3 * Int.ofNat B := by
          calc Int.ofNat (N0 + 1) = d2 * d2 * Int.ofNat (N0 + 1) := by rw [hdd]; lib_int
            _ = d2 * (3 * r2) := by rw [show 3 * r2 = d2 * Int.ofNat (N0 + 1) by rw [show 3 * r2 = 3 * r2 + -(d2 * Int.ofNat (N0 + 1)) + d2 * Int.ofNat (N0 + 1) by lib_int, z2]; lib_int]; lib_int
            _ = 3 * Int.ofNat B := by rw [hr2, show d2 * (3 * (d2 * Int.ofNat B)) = 3 * (d2 * d2) * Int.ofNat B by lib_int, hdd]; lib_int
        have hr1 : r1 = d1 * Int.ofNat B := three_cancel _ _ (by
          calc 3 * r1 = 3 * r1 + -(d1 * Int.ofNat (N0 + 1)) + d1 * Int.ofNat (N0 + 1) := by lib_int
            _ = 3 * (d1 * Int.ofNat B) := by rw [z1, hN3]; lib_int)
        have hB0 : B ≠ 0 := fun e => by
          have : Int.ofNat (N0 + 1) = 0 := by rw [hN3, e]; rfl
          exact Nat.noConfusion (Int.ofNat.inj this)
        have hN3n : N0 + 1 = 3 * B := Int.ofNat.inj (hN3.trans (by rfl))
        obtain ⟨sv, ev, c, hc, he⟩ := third43 B d1 d2 (Int.ofNat (N0 + 1)) hN3 hd2
        rw [← hr1, ← hr2] at he
        refine fin sv ev (c * B) ?_ ?_ he
        · rw [hN3n]; rcases hc with e | e <;> rw [e]
          · rw [show 3 * B = 1 * B + (B + B) by lib_nat]; exact Nat.lt_add_of_pos_right (Nat.lt_of_lt_of_le (Nat.pos_of_ne_zero hB0) (Nat.le_add_right _ _))
          · rw [show 3 * B = 2 * B + B by lib_nat]; exact Nat.lt_add_of_pos_right (Nat.pos_of_ne_zero hB0)
        · intro e0; rcases hBpos c e0 with e | e
          · rcases hc with h | h <;> rw [h] at e <;> exact Nat.noConfusion e
          · exact hB0 e
      · refine ⟨(3, 0), (d1, d2), ?_, fun h0 => absurd (hexpr.symm.trans h0) hz⟩
        rw [hexpr]
        apply nrm_lt
        show Int.natAbs (2 * (3 * r1 + -(d1 * Int.ofNat (N0 + 1))) + (3 * r2 + -(d2 * Int.ofNat (N0 + 1)))) * Int.natAbs (2 * (3 * r1 + -(d1 * Int.ofNat (N0 + 1))) + (3 * r2 + -(d2 * Int.ofNat (N0 + 1))))
          + 43 * (Int.natAbs (3 * r2 + -(d2 * Int.ofNat (N0 + 1))) * Int.natAbs (3 * r2 + -(d2 * Int.ofNat (N0 + 1)))) < _
        rw [hrd', hv1]
        calc Int.natAbs t' * Int.natAbs t' + 43 * (v * v) < (N0 + 1) * (N0 + 1) + 3 * ((N0 + 1) * (N0 + 1)) := Nat.add_lt_add_of_le_of_lt (sq_le' _ _ hT') hv2
          _ = 4 * ((N0 + 1) * (N0 + 1)) := by lib_nat

end Euc43

#print axioms Euc43.sq_le'
#print axioms Euc43.sq_lt
#print axioms Euc43.nrm_pair
#print axioms Euc43.zero_sq
#print axioms Euc43.ineq43c
#print axioms Euc43.ineq43d
#print axioms Euc43.half43
#print axioms Euc43.third43
#print axioms Euc43.three_cancel
#print axioms Euc43.dh43_r

lang/DH67.lean

lean_all/lang/DH67.lean

/-
  lang/DH67 — T48/T50: **the Dedekind–Hasse step in ℤ[(1 + √−67)/2]**: from a two-step remainder r there are s ∈ {1, 2, 3, 4}
  (by the size of |r₂| relative to N, via linear bounds 1000B > 211N etc. from 3N² ≤ 67B²) and rescues at the half, third and
  quarter points (resc2, resc3, resc4; table computed numerically) with N(sr − eN) < N², = 0 only for s = 1.
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Euc67
import lang.Euc19
import lang.EisPrim
import lang.Sq2Prim8
import lang.DH43

namespace Euc67

open FTAChain DetChain LibInt

theorem nrm_pair (n : Nat) : nrm (Int.ofNat n, 0) = n * n := Int.ofNat.inj (by rw [nrm_cast]; show Int.ofNat n * Int.ofNat n + Int.ofNat n * 0 + 17 * (0 * 0) = Int.ofNat n * Int.ofNat n; lib_int)

/-- b·Y² ≤ a·X², a q² < b p², Y > 0 ⇒ qY < pX. -/
theorem lin_lb (a b p q X Y : Nat) (h : b * (Y * Y) ≤ a * (X * X)) (hc : a * (q * q) < b * (p * p)) (hY : 0 < Y) : q * Y < p * X := by
  cases Nat.lt_or_ge (q * Y) (p * X) with
  | inl h' => exact h'
  | inr h' =>
    exfalso
    have s1 : (p * X) * (p * X) ≤ (q * Y) * (q * Y) := Nat.mul_le_mul h' h'
    have s2 : (p * p) * (b * (Y * Y)) ≤ (p * p) * (a * (X * X)) := Nat.mul_le_mul_left _ h
    have s3 : a * ((p * X) * (p * X)) ≤ a * ((q * Y) * (q * Y)) := Nat.mul_le_mul_left a s1
    have s4 : (a * (q * q)) * (Y * Y) < (b * (p * p)) * (Y * Y) := Nat.mul_lt_mul_of_pos_right hc (Nat.mul_pos hY hY)
    have : (b * (p * p)) * (Y * Y) < (b * (p * p)) * (Y * Y) :=
      Nat.lt_of_le_of_lt (Nat.le_trans (Nat.le_of_eq (by lib_nat)) (Nat.le_trans s2 (Nat.le_trans (Nat.le_of_eq (by lib_nat)) (Nat.le_trans s3 (Nat.le_of_eq (by lib_nat)))))) s4
    exact Nat.lt_irrefl _ this

/-- pW < qY, a q² < b p² ⇒ a W² < b Y². -/
theorem sq_ub (a b p q W Y : Nat) (h : p * W < q * Y) (hc : a * (q * q) < b * (p * p)) : a * (W * W) < b * (Y * Y) := by
  cases Nat.lt_or_ge (a * (W * W)) (b * (Y * Y)) with
  | inl h' => exact h'
  | inr h' =>
    exfalso
    have s1 : (p * W) * (p * W) ≤ (q * Y) * (q * Y) := Nat.mul_le_mul (Nat.le_of_lt h) (Nat.le_of_lt h)
    have hY : 0 < Y := by
      cases Y with
      | zero => exact absurd (Nat.lt_of_lt_of_le h (Nat.le_of_eq (Nat.mul_zero q))) (Nat.not_lt_zero _)
      | succ y => exact Nat.zero_lt_succ y
    have s2 : (p * p) * (b * (Y * Y)) ≤ (p * p) * (a * (W * W)) := Nat.mul_le_mul_left _ h'
    have s3 : a * ((p * W) * (p * W)) ≤ a * ((q * Y) * (q * Y)) := Nat.mul_le_mul_left a s1
    have s4 : (a * (q * q)) * (Y * Y) < (b * (p * p)) * (Y * Y) := Nat.mul_lt_mul_of_pos_right hc (Nat.mul_pos hY hY)
    have : (b * (p * p)) * (Y * Y) < (b * (p * p)) * (Y * Y) :=
      Nat.lt_of_le_of_lt (Nat.le_trans (Nat.le_of_eq (by lib_nat)) (Nat.le_trans s2 (Nat.le_trans (Nat.le_of_eq (by lib_nat)) (Nat.le_trans s3 (Nat.le_of_eq (by lib_nat)))))) s4
    exact Nat.lt_irrefl _ this

theorem resc2 (B : Nat) (d1 d2 N : Int) (hNB : N = 2 * Int.ofNat B) (hd2 : d2 = 1 ∨ d2 = -1) :
    ∃ s e : Int × Int, ∃ c : Nat, c < 2 ∧ 0 < c ∧ (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s (d1 * Int.ofNat B, d2 * Int.ofNat B)) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul e (N, 0))) = (Int.ofNat (c * B), 0) := by
  rcases GaussInt.int_even_odd d1 with ⟨q, hq⟩ | ⟨q, hq⟩ <;> rcases hd2 with e2 | e2
  · refine ⟨(-1, 1), (q * -1 + -9, q * 1 + 0), 1, (show 1 < 2 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 1) (Nat.le_add_left 2 0)), (show 0 < 1 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_left 1 0)), ?_⟩
    apply Prod.ext
    · show -1 * (d1 * Int.ofNat B) + -(1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * I  -- (+414 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 1 * Int.ofNat B; lib_int
    · show -1 * (d2 * Int.ofNat B) + 1 * (d1 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + -((q * -1 + -9) * 0 + (q * 1 + 0) * N + (q * 1 + 0) * 0) = 0
      rw [hq, e2, hNB]; lib_int
  · refine ⟨(-1, 1), (q * -1 + 8, q * 1 + 0), 1, (show 1 < 2 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 1) (Nat.le_add_left 2 0)), (show 0 < 1 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_left 1 0)), ?_⟩
    apply Prod.ext
    · show -1 * (d1 * Int.ofNat B) + -(1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * I  -- (+413 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 1 * Int.ofNat B; lib_int
    · show -1 * (d2 * Int.ofNat B) + 1 * (d1 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + -((q * -1 + 8) * 0 + (q * 1 + 0) * N + (q * 1 + 0) * 0) = 0
      rw [hq, e2, hNB]; lib_int
  · refine ⟨(0, -1), (q * 0 + 8, q * -1 + -1), 1, (show 1 < 2 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 1) (Nat.le_add_left 2 0)), (show 0 < 1 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_left 1 0)), ?_⟩
    apply Prod.ext
    · show 0 * (d1 * Int.ofNat B) + -(-1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B)   -- (+462 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 1 * Int.ofNat B; lib_int
    · show 0 * (d2 * Int.ofNat B) + -1 * (d1 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -((q * 0 + 8) * 0 + (q * -1 + -1) * N + (q * -1 + -1) * 0) = 0
      rw [hq, e2, hNB]; lib_int
  · refine ⟨(0, -1), (q * 0 + -9, q * -1 + 0), 1, (show 1 < 2 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 1) (Nat.le_add_left 2 0)), (show 0 < 1 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_left 1 0)), ?_⟩
    apply Prod.ext
    · show 0 * (d1 * Int.ofNat B) + -(-1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B)   -- (+446 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 1 * Int.ofNat B; lib_int
    · show 0 * (d2 * Int.ofNat B) + -1 * (d1 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -((q * 0 + -9) * 0 + (q * -1 + 0) * N + (q * -1 + 0) * 0) = 0
      rw [hq, e2, hNB]; lib_int

theorem resc3 (B : Nat) (d1 d2 N : Int) (hNB : N = 3 * Int.ofNat B) (hd2 : d2 = 1 ∨ d2 = -1) :
    ∃ s e : Int × Int, ∃ c : Nat, c < 3 ∧ 0 < c ∧ (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s (d1 * Int.ofNat B, d2 * Int.ofNat B)) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul e (N, 0))) = (Int.ofNat (c * B), 0) := by
  obtain ⟨q, hq | hq | hq⟩ := EisPrim.res3 d1 <;> rcases hd2 with e2 | e2
  · refine ⟨(-1, 1), (q * -1 + -6, q * 1 + 0), 1, (show 1 < 3 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 1) (Nat.le_add_left 2 1)), (show 0 < 1 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_left 1 0)), ?_⟩
    apply Prod.ext
    · show -1 * (d1 * Int.ofNat B) + -(1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * I  -- (+414 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 1 * Int.ofNat B; lib_int
    · show -1 * (d2 * Int.ofNat B) + 1 * (d1 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + -((q * -1 + -6) * 0 + (q * 1 + 0) * N + (q * 1 + 0) * 0) = 0
      rw [hq, e2, hNB]; lib_int
  · refine ⟨(-1, 1), (q * -1 + 5, q * 1 + 0), 2, (show 2 < 3 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 2) (Nat.le_add_left 3 0)), (show 0 < 2 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_left 1 1)), ?_⟩
    apply Prod.ext
    · show -1 * (d1 * Int.ofNat B) + -(1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * I  -- (+413 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 2 * Int.ofNat B; lib_int
    · show -1 * (d2 * Int.ofNat B) + 1 * (d1 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + -((q * -1 + 5) * 0 + (q * 1 + 0) * N + (q * 1 + 0) * 0) = 0
      rw [hq, e2, hNB]; lib_int
  · refine ⟨(-2, 1), (q * -2 + -7, q * 1 + 0), 2, (show 2 < 3 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 2) (Nat.le_add_left 3 0)), (show 0 < 2 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_left 1 1)), ?_⟩
    apply Prod.ext
    · show -2 * (d1 * Int.ofNat B) + -(1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * I  -- (+414 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 2 * Int.ofNat B; lib_int
    · show -2 * (d2 * Int.ofNat B) + 1 * (d1 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + -((q * -2 + -7) * 0 + (q * 1 + 0) * N + (q * 1 + 0) * 0) = 0
      rw [hq, e2, hNB]; lib_int
  · refine ⟨(0, -1), (q * 0 + -6, q * -1 + 0), 1, (show 1 < 3 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 1) (Nat.le_add_left 2 1)), (show 0 < 1 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_left 1 0)), ?_⟩
    apply Prod.ext
    · show 0 * (d1 * Int.ofNat B) + -(-1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B)   -- (+446 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 1 * Int.ofNat B; lib_int
    · show 0 * (d2 * Int.ofNat B) + -1 * (d1 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -((q * 0 + -6) * 0 + (q * -1 + 0) * N + (q * -1 + 0) * 0) = 0
      rw [hq, e2, hNB]; lib_int
  · refine ⟨(0, -1), ((q + -1) * 0 + 5, (q + -1) * -1 + -1), 2, (show 2 < 3 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 2) (Nat.le_add_left 3 0)), (show 0 < 2 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_left 1 1)), ?_⟩
    apply Prod.ext
    · show 0 * (d1 * Int.ofNat B) + -(-1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B)   -- (+588 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 2 * Int.ofNat B; lib_int
    · show 0 * (d2 * Int.ofNat B) + -1 * (d1 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -(((q + -1) * 0 + 5) * 0 + ((q + -1) * -1 + -1) * N + ((q + -1) * -1 + -1) * 0) = 0
      rw [hq, e2, hNB]; lib_int
  · refine ⟨(-2, 1), ((q + -1) * -2 + 4, (q + -1) * 1 + 1), 1, (show 1 < 3 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 1) (Nat.le_add_left 2 1)), (show 0 < 1 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_left 1 0)), ?_⟩
    apply Prod.ext
    · show -2 * (d1 * Int.ofNat B) + -(1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * I  -- (+539 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 1 * Int.ofNat B; lib_int
    · show -2 * (d2 * Int.ofNat B) + 1 * (d1 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + -(((q + -1) * -2 + 4) * 0 + ((q + -1) * 1 + 1) * N + ((q + -1) * 1 + 1) * 0) = 0
      rw [hq, e2, hNB]; lib_int

theorem resc4 (B : Nat) (d1 d2 N : Int) (hNB : N = 4 * Int.ofNat B) (hd2 : d2 = 1 ∨ d2 = -1) :
    ∃ s e : Int × Int, ∃ c : Nat, c < 4 ∧ 0 < c ∧ (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s (d1 * Int.ofNat B, d2 * Int.ofNat B)) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul e (N, 0))) = (Int.ofNat (c * B), 0) := by
  obtain ⟨q, hq | hq | hq | hq⟩ := Sq2Prim8.mod4 d1 <;> rcases hd2 with e2 | e2
  · refine ⟨(-1, 1), (q * -1 + -5, q * 1 + 0), 3, (show 3 < 4 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 3) (Nat.le_add_left 4 0)), (show 0 < 3 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_left 1 2)), ?_⟩
    apply Prod.ext
    · show -1 * (d1 * Int.ofNat B) + -(1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * I  -- (+414 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 3 * Int.ofNat B; lib_int
    · show -1 * (d2 * Int.ofNat B) + 1 * (d1 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + -((q * -1 + -5) * 0 + (q * 1 + 0) * N + (q * 1 + 0) * 0) = 0
      rw [hq, e2, hNB]; lib_int
  · refine ⟨(-1, 1), (q * -1 + 4, q * 1 + 0), 1, (show 1 < 4 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 1) (Nat.le_add_left 2 2)), (show 0 < 1 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_left 1 0)), ?_⟩
    apply Prod.ext
    · show -1 * (d1 * Int.ofNat B) + -(1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * I  -- (+413 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 1 * Int.ofNat B; lib_int
    · show -1 * (d2 * Int.ofNat B) + 1 * (d1 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + -((q * -1 + 4) * 0 + (q * 1 + 0) * N + (q * 1 + 0) * 0) = 0
      rw [hq, e2, hNB]; lib_int
  · refine ⟨(-2, 1), (q * -2 + -5, q * 1 + 0), 1, (show 1 < 4 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 1) (Nat.le_add_left 2 2)), (show 0 < 1 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_left 1 0)), ?_⟩
    apply Prod.ext
    · show -2 * (d1 * Int.ofNat B) + -(1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * I  -- (+414 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 1 * Int.ofNat B; lib_int
    · show -2 * (d2 * Int.ofNat B) + 1 * (d1 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + -((q * -2 + -5) * 0 + (q * 1 + 0) * N + (q * 1 + 0) * 0) = 0
      rw [hq, e2, hNB]; lib_int
  · refine ⟨(0, -1), (q * 0 + -5, q * -1 + 0), 3, (show 3 < 4 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 3) (Nat.le_add_left 4 0)), (show 0 < 3 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_left 1 2)), ?_⟩
    apply Prod.ext
    · show 0 * (d1 * Int.ofNat B) + -(-1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B)   -- (+446 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 3 * Int.ofNat B; lib_int
    · show 0 * (d2 * Int.ofNat B) + -1 * (d1 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -((q * 0 + -5) * 0 + (q * -1 + 0) * N + (q * -1 + 0) * 0) = 0
      rw [hq, e2, hNB]; lib_int
  · refine ⟨(-1, -1), (q * -1 + 3, q * -1 + -1), 3, (show 3 < 4 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 3) (Nat.le_add_left 4 0)), (show 0 < 3 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_left 1 2)), ?_⟩
    apply Prod.ext
    · show -1 * (d1 * Int.ofNat B) + -(-1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B)  -- (+464 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 3 * Int.ofNat B; lib_int
    · show -1 * (d2 * Int.ofNat B) + -1 * (d1 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -((q * -1 + 3) * 0 + (q * -1 + -1) * N + (q * -1 + -1) * 0) = 0
      rw [hq, e2, hNB]; lib_int
  · refine ⟨(-1, -1), (q * -1 + -5, q * -1 + 0), 1, (show 1 < 4 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 1) (Nat.le_add_left 2 2)), (show 0 < 1 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_left 1 0)), ?_⟩
    apply Prod.ext
    · show -1 * (d1 * Int.ofNat B) + -(-1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B)  -- (+448 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 1 * Int.ofNat B; lib_int
    · show -1 * (d2 * Int.ofNat B) + -1 * (d1 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -((q * -1 + -5) * 0 + (q * -1 + 0) * N + (q * -1 + 0) * 0) = 0
      rw [hq, e2, hNB]; lib_int
  · refine ⟨(0, -1), (q * 0 + 4, q * -1 + -1), 1, (show 1 < 4 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 1) (Nat.le_add_left 2 2)), (show 0 < 1 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_left 1 0)), ?_⟩
    apply Prod.ext
    · show 0 * (d1 * Int.ofNat B) + -(-1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B)   -- (+462 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 1 * Int.ofNat B; lib_int
    · show 0 * (d2 * Int.ofNat B) + -1 * (d1 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -((q * 0 + 4) * 0 + (q * -1 + -1) * N + (q * -1 + -1) * 0) = 0
      rw [hq, e2, hNB]; lib_int
  · refine ⟨(-2, 1), (q * -2 + 2, q * 1 + 1), 3, (show 3 < 4 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 3) (Nat.le_add_left 4 0)), (show 0 < 3 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_left 1 2)), ?_⟩
    apply Prod.ext
    · show -2 * (d1 * Int.ofNat B) + -(1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * I  -- (+413 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 3 * Int.ofNat B; lib_int
    · show -2 * (d2 * Int.ofNat B) + 1 * (d1 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + -((q * -2 + 2) * 0 + (q * 1 + 1) * N + (q * 1 + 1) * 0) = 0
      rw [hq, e2, hNB]; lib_int

theorem kcancel (k : Nat) (a c : Int) (h : Int.ofNat (k + 1) * a = Int.ofNat (k + 1) * c) : a = c := by
  have h0 : Int.ofNat (k + 1) * (a + -c) = 0 := by rw [show Int.ofNat (k + 1) * (a + -c) = Int.ofNat (k + 1) * a + -(Int.ofNat (k + 1) * c) by lib_int, h]; lib_int
  cases int_noZD _ _ h0 with
  | inl e => exact absurd (Int.ofNat.inj e) (fun e => Nat.noConfusion e)
  | inr e => calc a = (a + -c) + c := by lib_int
      _ = c := by rw [e]; show (0 : Int) + c = c; lib_int

/-- One branch: multiplier k + 1 with |(k+1) r₂ − d₂N| = w, 67w² < 3N². -/
theorem branch (N0 : Nat) (r1 r2 d2 : Int) (B k : Nat) (hr2 : r2 = d2 * Int.ofNat B) (hd2 : d2 = 1 ∨ d2 = -1)
    (w : Nat) (hw : Int.natAbs (Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1))) = w) (hb : 67 * (w * w) < 3 * ((N0 + 1) * (N0 + 1)))
    (resc : ∀ d1 : Int, Int.ofNat (N0 + 1) = Int.ofNat (k + 1) * Int.ofNat B → ∃ s e : Int × Int, ∃ c : Nat, c < k + 1 ∧ 0 < c ∧
      (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s (d1 * Int.ofNat B, d2 * Int.ofNat B)) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0))) = (Int.ofNat (c * B), 0)) :
    ∃ s e : Int × Int, nrm ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s (r1, r2)) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0)))) < (N0 + 1) * (N0 + 1) ∧
      ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s (r1, r2)) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0))) = (Ring67.ring67 LibInt.intR).zero → s = (Ring67.ring67 LibInt.intR).one) := by
  have hdd : d2 * d2 = 1 := by rcases hd2 with e | e <;> rw [e] <;> rfl
  have e2N : Int.ofNat (2 * N0 + 1 + 1) = 2 * Int.ofNat (N0 + 1) := by show 2 * Int.ofNat N0 + 1 + 1 = 2 * (Int.ofNat N0 + 1); lib_int
  obtain ⟨d1, t', hrd, hb1⟩ := GaussEuclid.rnd (Int.ofNat (k + 1) * (2 * r1 + r2) + -(d2 * Int.ofNat (N0 + 1))) (2 * N0 + 1)
  have hrd' : 2 * (Int.ofNat (k + 1) * r1 + -(d1 * Int.ofNat (N0 + 1))) + (Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1))) = t' := by
    calc 2 * (Int.ofNat (k + 1) * r1 + -(d1 * Int.ofNat (N0 + 1))) + (Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1))) = Int.ofNat (k + 1) * (2 * r1 + r2) + -(d2 * Int.ofNat (N0 + 1)) + -(d1 * (2 * Int.ofNat (N0 + 1))) := by lib_int
      _ = t' := by rw [hrd, e2N]; lib_int
  have hT' : Int.natAbs t' ≤ N0 + 1 := Nat.le_of_mul_le_mul_left (Nat.le_trans hb1 (Nat.le_of_eq (by lib_nat))) (Nat.zero_lt_succ 1)
  have hexpr : (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul (Int.ofNat (k + 1), 0) (r1, r2)) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul (d1, d2) (Int.ofNat (N0 + 1), 0))) = (Int.ofNat (k + 1) * r1 + -(d1 * Int.ofNat (N0 + 1)), Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1))) := by
    apply Prod.ext
    · show Int.ofNat (k + 1) * r1 + -(0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2) + -(d1 * Int.ofNat (N0 + 1) + -(d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0)) = Int.ofNat (k + 1) * r1   -- (+37 chars; full line in the Zenodo source)
    · show Int.ofNat (k + 1) * r2 + 0 * r1 + 0 * r2 + -(d1 * 0 + d2 * Int.ofNat (N0 + 1) + d2 * 0) = Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1)); lib_int
  by_cases hz : (Int.ofNat (k + 1) * r1 + -(d1 * Int.ofNat (N0 + 1)), Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1))) = (Ring67.ring67 LibInt.intR).zero
  · have z1 : Int.ofNat (k + 1) * r1 + -(d1 * Int.ofNat (N0 + 1)) = 0 := congrArg Prod.fst hz
    have z2 : Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1)) = 0 := congrArg Prod.snd hz
    have hNk : Int.ofNat (N0 + 1) = Int.ofNat (k + 1) * Int.ofNat B := by
      calc Int.ofNat (N0 + 1) = d2 * d2 * Int.ofNat (N0 + 1) := by rw [hdd]; lib_int
        _ = d2 * (Int.ofNat (k + 1) * r2) := by rw [show Int.ofNat (k + 1) * r2 = d2 * Int.ofNat (N0 + 1) by rw [show Int.ofNat (k + 1) * r2 = Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1)) + d2 * Int.ofNat (N0 + 1) by lib_int, z2]; lib_int]; lib_int
        _ = Int.ofNat (k + 1) * Int.ofNat B := by rw [hr2, show d2 * (Int.ofNat (k + 1) * (d2 * Int.ofNat B)) = Int.ofNat (k + 1) * (d2 * d2) * Int.ofNat B by lib_int, hdd]; lib_int
    have hr1 : r1 = d1 * Int.ofNat B := kcancel k _ _ (by
      calc Int.ofNat (k + 1) * r1 = Int.ofNat (k + 1) * r1 + -(d1 * Int.ofNat (N0 + 1)) + d1 * Int.ofNat (N0 + 1) := by lib_int
        _ = Int.ofNat (k + 1) * (d1 * Int.ofNat B) := by rw [z1, hNk]; lib_int)
    have hNn : N0 + 1 = (k + 1) * B := Int.ofNat.inj hNk
    have hB0 : B ≠ 0 := fun e => by rw [e] at hNn; exact Nat.noConfusion (hNn.trans (Nat.mul_zero _))
    obtain ⟨sv, ev, c, hck, hc0, he⟩ := resc d1 hNk
    rw [← hr1, ← hr2] at he
    refine ⟨sv, ev, ?_, fun h0 => ?_⟩
    · rw [he, nrm_pair, hNn]
      have : c * B < (k + 1) * B := Nat.mul_lt_mul_of_pos_right hck (Nat.pos_of_ne_zero hB0)
      exact Nat.mul_lt_mul_of_le_of_lt (Nat.le_of_lt this) this (Nat.lt_of_le_of_lt (Nat.zero_le _) this) |> fun h => Nat.lt_of_le_of_lt (Nat.mul_le_mul (Nat.le_of_lt this) (Nat.le_refl _)) (Nat.mul_lt_mul_of_pos_left this (Nat.lt_of_le_of_lt (Nat.zero_le _) this))
    · exfalso
      have : Int.ofNat (c * B) = 0 := congrArg Prod.fst (he.symm.trans h0)
      have h1 : c * B = 0 := Int.ofNat.inj this
      cases nat_mul_zero _ _ h1 with
      | inl e => rw [e] at hc0; exact Nat.lt_irrefl 0 hc0
      | inr e => exact hB0 e
  · refine ⟨(Int.ofNat (k + 1), 0), (d1, d2), ?_, fun h0 => absurd (hexpr.symm.trans h0) hz⟩
    rw [hexpr]
    apply nrm_lt
    show Int.natAbs (2 * (Int.ofNat (k + 1) * r1 + -(d1 * Int.ofNat (N0 + 1))) + (Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1)))) * Int.natAbs (2 * (Int.ofNat (k + 1) * r1 + -(d1 * Int.ofNat (N0 + 1))) + (Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1))))
      + 67 * (Int.natAbs (Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1))) * Int.natAbs (Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1)))) < _
    rw [hrd', hw]
    calc Int.natAbs t' * Int.natAbs t' + 67 * (w * w) < (N0 + 1) * (N0 + 1) + 3 * ((N0 + 1) * (N0 + 1)) := Nat.add_lt_add_of_le_of_lt (Nat.mul_le_mul hT' hT') hb
      _ = 4 * ((N0 + 1) * (N0 + 1)) := by lib_nat

theorem nlt (a k : Nat) : a < a + k + 1 := Nat.succ_le_succ (Nat.le_add_right a k)

theorem lt_cancel (a b c : Nat) (h : a + c < b + c) : a < b := by
  cases Nat.lt_or_ge a b with
  | inl h' => exact h'
  | inr h' => exact absurd h (Nat.not_lt_of_ge (Nat.add_le_add_right h' c))

theorem absw (d2 : Int) (hd2 : d2 = 1 ∨ d2 = -1) (X : Int) (w : Nat) (hX : X = d2 * Int.ofNat w ∨ X = -(d2 * Int.ofNat w)) : Int.natAbs X = w := by
  rcases hd2 with e | e <;> rcases hX with h | h <;> rw [h, e]
  · show Int.natAbs (1 * Int.ofNat w) = w; rw [show 1 * Int.ofNat w = Int.ofNat w by lib_int]; rfl
  · show Int.natAbs (-(1 * Int.ofNat w)) = w; rw [show -(1 * Int.ofNat w) = -Int.ofNat w by lib_int]; exact Int.natAbs_neg _
  · show Int.natAbs (-1 * Int.ofNat w) = w; rw [show -1 * Int.ofNat w = -Int.ofNat w by lib_int]; exact Int.natAbs_neg _
  · show Int.natAbs (-(-1 * Int.ofNat w)) = w; rw [show -(-1 * Int.ofNat w) = Int.ofNat w by lib_int]; rfl

set_option maxHeartbeats 0 in
/-- **The Dedekind–Hasse step on a remainder (d = 67).** -/
theorem dh67_r (N0 : Nat) (r1 r2 : Int) (h2 : 2 * Int.natAbs r2 ≤ N0 + 1) (ht : Int.natAbs (2 * r1 + r2) ≤ N0 + 1) :
    ∃ s e : Int × Int, nrm ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s (r1, r2)) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0)))) < (N0 + 1) * (N0 + 1) ∧
      ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s (r1, r2)) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0))) = (Ring67.ring67 LibInt.intR).zero → s = (Ring67.ring67 LibInt.intR).one) := by
  have h4 := four_nrm (r1, r2)
  generalize hT : Int.natAbs (2 * r1 + r2) = T at h4 ht
  generalize hB : Int.natAbs r2 = B at h4 h2
  have h4' : 4 * nrm (r1, r2) = T * T + 67 * (B * B) := h4
  cases Nat.lt_or_ge (T * T + 67 * (B * B)) (4 * ((N0 + 1) * (N0 + 1))) with
  | inl hl =>
    refine ⟨(Ring67.ring67 LibInt.intR).one, (Ring67.ring67 LibInt.intR).zero, ?_, fun _ => rfl⟩
    have e : (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul (Ring67.ring67 LibInt.intR).one (r1, r2)) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul (Ring67.ring67 LibInt.intR).zero (Int.ofNat (N0 + 1), 0))) = (r1, r2) := by lib_ring
    rw [e]
    exact nrm_lt (r1, r2) (N0 + 1) (by rw [hT, hB]; exact hl)
  | inr hg =>
    have h3 : 3 * ((N0 + 1) * (N0 + 1)) ≤ 67 * (B * B) := GaussEuclid.le_cancel _ _ ((N0 + 1) * (N0 + 1)) (by
      calc 3 * ((N0 + 1) * (N0 + 1)) + (N0 + 1) * (N0 + 1) = 4 * ((N0 + 1) * (N0 + 1)) := by lib_nat
        _ ≤ T * T + 67 * (B * B) := hg
        _ ≤ (N0 + 1) * (N0 + 1) + 67 * (B * B) := Nat.add_le_add_right (Nat.mul_le_mul ht ht) _
        _ = 67 * (B * B) + (N0 + 1) * (N0 + 1) := by lib_nat)
    have lb1 : 4 * (N0 + 1) < 19 * B := lin_lb 67 3 19 4 B (N0 + 1) h3 (nlt 1072 10) (Nat.zero_lt_succ N0)
    obtain ⟨u, hu⟩ := EisEuclid.le_dest _ _ h2
    have hN : N0 + 1 = 2 * B + u := by rw [← hu]; lib_nat
    have hNi : Int.ofNat (N0 + 1) = 2 * Int.ofNat B + Int.ofNat u := congrArg Int.ofNat hN
    have hsg : ∃ d2 : Int, (d2 = 1 ∨ d2 = -1) ∧ r2 = d2 * Int.ofNat B := by
      rcases EisEuclid.sgn r2 with e | e
      · exact ⟨1, Or.inl rfl, by rw [e, hB]; lib_int⟩
      · exact ⟨-1, Or.inr rfl, by rw [e, hB]; lib_int⟩
    obtain ⟨d2, hd2, hr2⟩ := hsg
    cases Nat.lt_or_ge (67 * (u * u)) (3 * ((N0 + 1) * (N0 + 1))) with
    | inl hu2 =>
      exact branch N0 r1 r2 d2 B 1 hr2 hd2 u (absw d2 hd2 _ u (Or.inr (by rw [hr2, hNi]; show 2 * (d2 * Int.ofNat B) + -(d2 * (2 * Int.ofNat B + Int.ofNat u)) = _; lib_int))) hu2
        (fun d1 hk => resc2 B d1 d2 _ (by rw [hk]; rfl) hd2)
    | inr hu3 =>
      have lb2 : 4 * (N0 + 1) < 19 * u := lin_lb 67 3 19 4 u (N0 + 1) hu3 (nlt 1072 10) (Nat.zero_lt_succ N0)
      cases Nat.le_total u B with
      | inl huB =>
        obtain ⟨v, hv⟩ := EisEuclid.le_dest _ _ huB
        -- B = v + u, N = 3u + 2v
        have l3 : 3 * (4 * (N0 + 1)) < 3 * (19 * u) := Nat.mul_lt_mul_of_pos_left lb2 (Nat.zero_lt_succ 2)
        have hlin : 38 * v < 7 * (N0 + 1) := lt_cancel _ _ (12 * (N0 + 1)) (by
          calc 38 * v + 12 * (N0 + 1) = 38 * v + 3 * (4 * (N0 + 1)) := by lib_nat
            _ < 38 * v + 3 * (19 * u) := Nat.add_lt_add_left l3 _
            _ = 7 * (N0 + 1) + 12 * (N0 + 1) := by rw [hN, ← hv]; lib_nat)
        have hb : 67 * (v * v) < 3 * ((N0 + 1) * (N0 + 1)) := sq_ub 67 3 38 7 v (N0 + 1) hlin (nlt 3283 1048)
        exact branch N0 r1 r2 d2 B 2 hr2 hd2 v (absw d2 hd2 _ v (Or.inl (by rw [hr2, hNi, ← hv]; show 3 * (d2 * (Int.ofNat v + Int.ofNat u)) + -(d2 * (2 * (Int.ofNat v + Int.ofNat u) + Int.ofNat u)) = _; lib_int))) hb
          (fun d1 hk => resc3 B d1 d2 _ (by rw [hk]; rfl) hd2)
      | inr hBu =>
        obtain ⟨v, hv⟩ := EisEuclid.le_dest _ _ hBu
        -- u = v + B, N = 3B + v
        have hw3 : Int.natAbs (Int.ofNat 3 * r2 + -(d2 * Int.ofNat (N0 + 1))) = v := absw d2 hd2 _ v (Or.inr (by
          rw [hr2, hNi, ← hv]; show 3 * (d2 * Int.ofNat B) + -(d2 * (2 * Int.ofNat B + (Int.ofNat v + Int.ofNat B))) = _; lib_int))
        cases Nat.lt_or_ge (67 * (v * v)) (3 * ((N0 + 1) * (N0 + 1))) with
        | inl hb => exact branch N0 r1 r2 d2 B 2 hr2 hd2 v hw3 hb (fun d1 hk => resc3 B d1 d2 _ (by rw [hk]; rfl) hd2)
        | inr hv3 =>
          have lb3 : 4 * (N0 + 1) < 19 * v := lin_lb 67 3 19 4 v (N0 + 1) hv3 (nlt 1072 10) (Nat.zero_lt_succ N0)
          cases Nat.le_total v B with
          | inl hvB =>
            obtain ⟨x, hx⟩ := EisEuclid.le_dest _ _ hvB
            have hw4 : Int.natAbs (Int.ofNat 4 * r2 + -(d2 * Int.ofNat (N0 + 1))) = x := absw d2 hd2 _ x (Or.inl (by
              rw [hr2, hNi, ← hv, ← hx]; show 4 * (d2 * (Int.ofNat x + Int.ofNat v)) + -(d2 * (2 * (Int.ofNat x + Int.ofNat v) + (Int.ofNat v + (Int.ofNat x + Int.ofNat v)))) = _; lib_int))
            have l4 : 4 * (4 * (N0 + 1)) < 4 * (19 * v) := Nat.mul_lt_mul_of_pos_left lb3 (Nat.zero_lt_succ 3)
            have hlin : 57 * x < 3 * (N0 + 1) := lt_cancel _ _ (16 * (N0 + 1)) (by
              calc 57 * x + 16 * (N0 + 1) = 57 * x + 4 * (4 * (N0 + 1)) := by lib_nat
                _ < 57 * x + 4 * (19 * v) := Nat.add_lt_add_left l4 _
                _ = 3 * (N0 + 1) + 16 * (N0 + 1) := by rw [hN, ← hv, ← hx]; lib_nat)
            exact branch N0 r1 r2 d2 B 3 hr2 hd2 x hw4 (sq_ub 67 3 57 3 x (N0 + 1) hlin (nlt 603 9143))
              (fun d1 hk => resc4 B d1 d2 _ (by rw [hk]; rfl) hd2)
          | inr hBv =>
            obtain ⟨x, hx⟩ := EisEuclid.le_dest _ _ hBv
            have hw4 : Int.natAbs (Int.ofNat 4 * r2 + -(d2 * Int.ofNat (N0 + 1))) = x := absw d2 hd2 _ x (Or.inr (by
              rw [hr2, hNi, ← hv, ← hx]; show 4 * (d2 * Int.ofNat B) + -(d2 * (2 * Int.ofNat B + ((Int.ofNat x + Int.ofNat B) + Int.ofNat B))) = _; lib_int))
            have l4 : 4 * (4 * (N0 + 1)) < 4 * (19 * B) := Nat.mul_lt_mul_of_pos_left lb1 (Nat.zero_lt_succ 3)
            have hlin : 19 * x < 3 * (N0 + 1) := lt_cancel _ _ (16 * (N0 + 1)) (by
              calc 19 * x + 16 * (N0 + 1) = 19 * x + 4 * (4 * (N0 + 1)) := by lib_nat
                _ < 19 * x + 4 * (19 * B) := Nat.add_lt_add_left l4 _
                _ = 3 * (N0 + 1) + 16 * (N0 + 1) := by rw [hN, ← hv, ← hx]; lib_nat)
            exact branch N0 r1 r2 d2 B 3 hr2 hd2 x hw4 (sq_ub 67 3 19 3 x (N0 + 1) hlin (nlt 603 479))
              (fun d1 hk => resc4 B d1 d2 _ (by rw [hk]; rfl) hd2)

end Euc67
#print axioms Euc67.nrm_pair
#print axioms Euc67.lin_lb
#print axioms Euc67.sq_ub
#print axioms Euc67.resc2
#print axioms Euc67.resc3
#print axioms Euc67.resc4
#print axioms Euc67.kcancel
#print axioms Euc67.branch
#print axioms Euc67.nlt
#print axioms Euc67.lt_cancel
#print axioms Euc67.absw
#print axioms Euc67.dh67_r

lang/DLog.lean

lean_all/lang/DLog.lean

/-
  lang/DLog — T43, cycle 52: discrete logarithms in 𝔽_p — every unit is a power of a primitive root, and sums over 𝔽_p
  split as F(0) + Σ_k F(g^k).

  p = J + 2, g of order p − 1 (PrimRoot.primitive_root), pows n = [g^(n−1), …, g^0].
    pw_ne_zero      g ≠ 0 ⇒ g^k ≠ 0
    pows_dist       g^0, …, g^(p−2) are pairwise distinct
    every_unit      a ≠ 0 ⇒ a = g^k for some k < p − 1          (pigeonhole: p + 1 distinct values below p otherwise)
    count_dist      Σ_{s ∈ l} δ(a, s) = 1 for a ∈ l, l distinct
    sum_split       Σ_a F(a) = F(0) + Σ_{s ∈ pows (p−1)} F(s)
  Used next: grouping the cubic Jacobsthal sums over the classes g^(3q + i) (i = 0, 1, 2).

  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.PrimRoot
import lang.TwoSquares
import lang.HasseCM
import lang.Eisen

namespace DLog

open FTAChain ZMod YMChain FiniteRecChain OrdMul Pigeon

variable (J : Nat)

def pows (g : Zp (J + 1)) : Nat → List (Zp (J + 1))
  | 0 => []
  | n + 1 => Frobenius.pw (ring (J + 1)) g n :: pows g n

theorem pw_ne_zero (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (g : Zp (J + 1)) (hg : g ≠ (ring (J + 1)).zero) :
    ∀ k, Frobenius.pw (ring (J + 1)) g k ≠ (ring (J + 1)).zero
  | 0 => PowerSum.one_ne_zero' J
  | k + 1 => fun e => by
    cases nozd J hpr _ _ e with
    | inl h => exact pw_ne_zero hpr g hg k h
    | inr h => exact hg h

theorem mem_pows (g : Zp (J + 1)) (s : Zp (J + 1)) : ∀ n, memL s (pows J g n) → ∃ k, k < n ∧ s = Frobenius.pw (ring (J + 1)) g k
  | 0, h => False.elim h
  | n + 1, h => by
    cases h with
    | inl e => exact ⟨n, Nat.lt_succ_self n, e⟩
    | inr e =>
      match mem_pows g s n e with
      | ⟨k, hk, hs⟩ => exact ⟨k, Nat.lt_succ_of_lt hk, hs⟩

theorem pw_inj (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (g : Zp (J + 1)) (hg : Ord (ring (J + 1)) g (J + 1))
    (k l : Nat) (hkl : k < l) (hl : l < J + 1) : Frobenius.pw (ring (J + 1)) g k ≠ Frobenius.pw (ring (J + 1)) g l := fun e => by
  have hg0 : g ≠ (ring (J + 1)).zero := fun h => by
    have := hg.1; rw [h, HasseCM.pw_zero_succ] at this; exact PowerSum.one_ne_zero' J this.symm
  match Nat.le.dest hkl with
  | ⟨d, hd⟩ =>
    have hl' : l = k + (d + 1) := by rw [← hd]; exact (Nat.succ_add k d)
    rw [hl', HasseCM.pw_add'] at e
    have h0 : (ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) g k) ((ring (J + 1)).add (Frobenius.pw (ring (J + 1)) g (d + 1)) ((ring (J + 1)).neg (ring (J + 1)).one)) = (ring (J + 1)).zero := by
      rw [(ring (J + 1)).distrib, ← e, mul_neg_r (ring (J + 1)), (ring (J + 1)).mul_comm _ (ring (J + 1)).one, (ring (J + 1)).one_mul, (ring (J + 1)).add_neg]
    cases nozd J hpr _ _ h0 with
    | inl h => exact pw_ne_zero J hpr g hg0 k h
    | inr h =>
      have h1 := hg.2 (d + 1) (Eisen.eq_of_sub_zero J _ _ h)
      match h1 with
      | ⟨c, hc⟩ =>
        cases c with
        | zero => rw [Nat.mul_zero] at hc; exact Nat.noConfusion hc
        | succ c =>
          have : J + 1 ≤ d + 1 := by rw [hc, Nat.mul_succ]; exact Nat.le_add_left _ _
          have hdl : d + 1 ≤ l := by rw [hl']; exact Nat.le_add_left _ _
          exact Nat.lt_irrefl l (Nat.lt_of_lt_of_le hl (Nat.le_trans this hdl))

theorem pows_dist (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (g : Zp (J + 1)) (hg : Ord (ring (J + 1)) g (J + 1)) :
    ∀ n, n ≤ J + 1 → Dist (pows J g n)
  | 0, _ => trivial
  | n + 1, hn => ⟨fun s hs => by
      match mem_pows J g s n hs with
      | ⟨k, hk, e⟩ => rw [e]; exact pw_inj J hpr g hg k n hk (Nat.lt_of_succ_le hn),
    pows_dist hpr g hg n (Nat.le_of_succ_le hn)⟩

theorem len_pows (g : Zp (J + 1)) : ∀ n, (pows J g n).length = n
  | 0 => rfl
  | n + 1 => congrArg Nat.succ (len_pows g n)

theorem find_mem (a : Zp (J + 1)) : ∀ l : List (Zp (J + 1)), memL a l ∨ ¬ memL a l
  | [] => Or.inr (fun h => h)
  | b :: l => by
    by_cases h : a = b
    · exact Or.inl (Or.inl h)
    · cases find_mem a l with
      | inl h' => exact Or.inl (Or.inr h')
      | inr h' => exact Or.inr (fun hh => by cases hh with
        | inl e => exact h e
        | inr e => exact h' e)

/-- **Every unit is a power of g.** -/
theorem every_unit (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (g : Zp (J + 1)) (hg : Ord (ring (J + 1)) g (J + 1))
    (a : Zp (J + 1)) (ha : a ≠ (ring (J + 1)).zero) : memL a (pows J g (J + 1)) := by
  have hg0 : g ≠ (ring (J + 1)).zero := fun h => by
    have := hg.1; rw [h, HasseCM.pw_zero_succ] at this; exact PowerSum.one_ne_zero' J this.symm
  cases find_mem J a (pows J g (J + 1)) with
  | inl h => exact h
  | inr h =>
    exfalso
    have hz : ∀ s, memL s (pows J g (J + 1)) → s ≠ (ring (J + 1)).zero := fun s hs => by
      match mem_pows J g s _ hs with
      | ⟨k, _, e⟩ => rw [e]; exact pw_ne_zero J hpr g hg0 k
    have hd : Dist ((ring (J + 1)).zero :: a :: pows J g (J + 1)) :=
      ⟨fun s hs => by
          cases hs with
          | inl e => rw [e]; exact ha
          | inr e => exact hz s e,
       ⟨fun s hs e => h (e ▸ hs), pows_dist J hpr g hg (J + 1) (Nat.le_refl _)⟩⟩
    have hv := TwoSquares.dist_map (fun x : Zp (J + 1) => x.val) (fun x y e => Subtype.ext e) _ hd
    have := pigeonhole (J + 1 + 1) _ hv (fun x hx => match TwoSquares.mem_map _ x _ hx with
      | ⟨y, _, ey⟩ => ey ▸ y.property)
    rw [TwoSquares.len_map] at this
    show False
    have hl : ((ring (J + 1)).zero :: a :: pows J g (J + 1)).length = J + 1 + 1 + 1 := by
      show Nat.succ (Nat.succ (pows J g (J + 1)).length) = _
      rw [len_pows]
    rw [hl] at this
    exact Nat.not_succ_le_self _ this

section Sums
variable {S : Type} (Rg : DetChain.CRing S)

theorem count_out (a : Zp (J + 1)) : ∀ l : List (Zp (J + 1)), ¬ memL a l → sumK Rg (fun s => delta Rg a s) l = Rg.zero
  | [], _ => rfl
  | b :: l, h => by
    show Rg.add (delta Rg a b) (sumK Rg (fun s => delta Rg a s) l) = Rg.zero
    rw [show delta Rg a b = Rg.zero from ite_neg' _ _ (fun e => h (Or.inl e)), count_out a l (fun h' => h (Or.inr h')),
      Rg.zero_add]

theorem count_in (a : Zp (J + 1)) : ∀ l : List (Zp (J + 1)), Dist l → memL a l → sumK Rg (fun s => delta Rg a s) l = Rg.one
  | [], _, h => False.elim h
  | b :: l, hd, h => by
    show Rg.add (delta Rg a b) (sumK Rg (fun s => delta Rg a s) l) = Rg.one
    by_cases e : a = b
    · rw [show delta Rg a b = Rg.one from ite_pos' _ _ e, count_out J Rg a l (fun h' => hd.1 a h' e), DetChain.add_zero']
    · cases h with
      | inl e' => exact absurd e' e
      | inr h' => rw [show delta Rg a b = Rg.zero from ite_neg' _ _ e, count_in a l hd.2 h', Rg.zero_add]

/-- **Σ_a F(a) = F(0) + Σ_k F(g^k).** -/
theorem sum_split (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (g : Zp (J + 1)) (hg : Ord (ring (J + 1)) g (J + 1))
    (F : Zp (J + 1) → S) :
    sumK Rg F (CMInert.allZ (J + 1) (J + 1 + 1))
      = Rg.add (F (ring (J + 1)).zero) (sumK Rg F (pows J g (J + 1))) := by
  have hc := CMInert.complete_allZ (J + 1) Rg
  have hg0 : g ≠ (ring (J + 1)).zero := fun h => by
    have := hg.1; rw [h, HasseCM.pw_zero_succ] at this; exact PowerSum.one_ne_zero' J this.symm
  have hd := pows_dist J hpr g hg (J + 1) (Nat.le_refl _)
  have pt : ∀ a, F a = Rg.add (Rg.mul (delta Rg a (ring (J + 1)).zero) (F a))
      (sumK Rg (fun s => Rg.mul (delta Rg a s) (F a)) (pows J g (J + 1))) := fun a => by
    rw [← sumK_mul, ← DetChain.right_distrib]
    by_cases ha : a = (ring (J + 1)).zero
    · rw [show delta Rg a (ring (J + 1)).zero = Rg.one from ite_pos' _ _ ha,
        count_out J Rg a _ (fun h => by
          match mem_pows J g a _ h with
          | ⟨k, _, e⟩ => exact pw_ne_zero J hpr g hg0 k (e.symm.trans ha)),
        DetChain.add_zero', Rg.one_mul]
    · rw [show delta Rg a (ring (J + 1)).zero = Rg.zero from ite_neg' _ _ ha, count_in J Rg a _ hd (every_unit J hpr g hg a ha),
        Rg.zero_add, Rg.one_mul]
  rw [sumK_ext Rg _ _ pt, sumK_add, pick Rg _ hc F (ring (J + 1)).zero, sumK_swap]
  congr 1
  exact sumK_ext Rg _ _ (fun s => pick Rg _ hc F s) _

end Sums

end DLog

#print axioms DLog.pw_ne_zero
#print axioms DLog.mem_pows
#print axioms DLog.pw_inj
#print axioms DLog.pows_dist
#print axioms DLog.len_pows
#print axioms DLog.find_mem
#print axioms DLog.every_unit
#print axioms DLog.count_out
#print axioms DLog.count_in
#print axioms DLog.sum_split

lang/DLogF.lean

lean_all/lang/DLogF.lean

/-
  lang/DLogF — T49 (general ℓ), cycle 829: **the discrete logarithm as a function** (DLog proves only existence):
    dlogF      search g^k = a for k = k0, …, k0 + n − 1
    dlogF_spec a hit in the window ⇒ g^(dlogF) = a and dlogF < k0 + n
    dlog       dlog a = dlogF (p − 1) 0;   a ≠ 0 ⇒ g^(dlog a) = a, dlog a < p − 1     (DLog.every_unit)
    dlog_pw    k < p − 1 ⇒ dlog (g^k) = k                                          (DLog.pw_inj, RootAll.tri)
  So χ(a) = ζ^(dlog a) is an honest function on 𝔽_p^×.
  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.DLog
import lang.RootAll
import lib.Semi

namespace DLogF

open FTAChain ZMod YMChain FiniteRecChain Frobenius

variable (J : Nat)

def dlogF (g a : Zp (J + 1)) : Nat → Nat → Nat
  | 0, _ => 0
  | n + 1, k => if pw (ring (J + 1)) g k = a then k else dlogF g a n (k + 1)

theorem succ_le_of_ne (k i : Nat) (h : Nat.le k i) (hne : i ≠ k) : Nat.le (k + 1) i := by
  cases h with
  | refl => exact absurd rfl hne
  | step h' => exact Nat.succ_le_succ h'

theorem dlogF_spec (g a : Zp (J + 1)) : ∀ n k, (∃ i, k ≤ i ∧ i < k + n ∧ pw (ring (J + 1)) g i = a) →
    pw (ring (J + 1)) g (dlogF J g a n k) = a ∧ dlogF J g a n k < k + n
  | 0, k, ⟨i, h1, h2, _⟩ => absurd (Nat.lt_of_le_of_lt h1 h2) (Nat.lt_irrefl k)
  | n + 1, k, ⟨i, h1, h2, h3⟩ => by
    by_cases h : pw (ring (J + 1)) g k = a
    · have e : dlogF J g a (n + 1) k = k := by
        show (if pw (ring (J + 1)) g k = a then k else dlogF J g a n (k + 1)) = k
        rw [ite_pos' _ _ h]
      rw [e]
      exact ⟨h, Nat.lt_of_lt_of_le (Nat.lt_succ_self k) (Nat.succ_le_succ (Nat.le_add_right k n))⟩
    · have e : dlogF J g a (n + 1) k = dlogF J g a n (k + 1) := by
        show (if pw (ring (J + 1)) g k = a then k else dlogF J g a n (k + 1)) = _
        rw [ite_neg' _ _ h]
      rw [e]
      have hi : k + 1 ≤ i := succ_le_of_ne k i h1 (fun hik => h (hik ▸ h3))
      have hi2 : i < k + 1 + n := by
        rw [show k + 1 + n = k + (n + 1) by lib_nat]; exact h2
      obtain ⟨r1, r2⟩ := dlogF_spec g a n (k + 1) ⟨i, hi, hi2, h3⟩
      exact ⟨r1, by rw [show k + (n + 1) = k + 1 + n by lib_nat]; exact r2⟩

/-- the discrete logarithm base g. -/
def dlog (g a : Zp (J + 1)) : Nat := dlogF J g a (J + 1) 0

theorem dlog_spec (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (g : Zp (J + 1)) (hg : OrdMul.Ord (ring (J + 1)) g (J + 1))
    (a : Zp (J + 1)) (ha : a ≠ (ring (J + 1)).zero) :
    pw (ring (J + 1)) g (dlog J g a) = a ∧ dlog J g a < J + 1 := by
  obtain ⟨k, hk, e⟩ := DLog.mem_pows J g a (J + 1) (DLog.every_unit J hpr g hg a ha)
  have r := dlogF_spec J g a (J + 1) 0 ⟨k, Nat.zero_le k, by rw [Nat.zero_add]; exact hk, e.symm⟩
  rw [Nat.zero_add] at r
  exact r

theorem dlog_pw (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (g : Zp (J + 1)) (hg : OrdMul.Ord (ring (J + 1)) g (J + 1))
    (hg0 : g ≠ (ring (J + 1)).zero) (k : Nat) (hk : k < J + 1) : dlog J g (pw (ring (J + 1)) g k) = k := by
  have hne : pw (ring (J + 1)) g k ≠ (ring (J + 1)).zero := DLog.pw_ne_zero J hpr g hg0 k
  obtain ⟨e, hd⟩ := dlog_spec J hpr g hg _ hne
  match RootAll.tri (dlog J g (pw (ring (J + 1)) g k)) k with
  | Or.inl h => exact h
  | Or.inr (Or.inl h) => exact absurd e (DLog.pw_inj J hpr g hg _ k h hk)
  | Or.inr (Or.inr h) => exact absurd e.symm (DLog.pw_inj J hpr g hg k _ h hd)

end DLogF

#print axioms DLogF.succ_le_of_ne
#print axioms DLogF.dlogF_spec
#print axioms DLogF.dlog_spec
#print axioms DLogF.dlog_pw

lang/Dbl27w.lean

lean_all/lang/Dbl27w.lean

/-
  lang/Dbl27w — T47, cycle 827 (27a weight-one factor): **[q^(n+1)] η(6z)η(18z) over ℤ as an explicit double sum**
    [q^(n+1)] = Σ_{j ∈ zl(2M)} (−1)^|j| Σ_{k ∈ zl(2M)} (−1)^|k| δ(18G(k) + 6G(j), n),   G = Gj g36,  n + 1 < M,
  from Trunc.coef27w (Coef27w) by unfolding the shifts (Hecke32.shn_add/shn_smul/shn_shn, Coef36.shn_delta, TruncQ.nth_pw).
    shn_sumK   a shift commutes with a finite sum
    dbl27      the double sum
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Coef27w
import lang.Coef36
import lib.RingTac

namespace Hecke36

open DetChain YMChain FiniteRecChain Hecke32

theorem shn_sumK (E k : Nat) (f : Int → Nat → Int) : ∀ L : List Int,
    Trunc.shn Z E (fun i => sumK Z (fun j => f j i) L) k = sumK Z (fun j => Trunc.shn Z E (f j) k) L
  | [] => shn_zero E k
  | a :: L => by
    show Trunc.shn Z E (fun i => Z.add (f a i) (sumK Z (fun j => f j i) L)) k = Z.add (Trunc.shn Z E (f a) k) (sumK Z (fun j => Trunc.shn Z E (f j) k) L)
    rw [shn_add, shn_sumK E k f L]

/-- the coefficient as an explicit signed double count. -/
def dbl (M n : Nat) : Int :=
  sumK Z (fun j => Z.mul (Frobenius.pw Z (Z.neg Z.one) (Int.natAbs j))
    (sumK Z (fun k => Z.mul (Frobenius.pw Z (Z.neg Z.one) (Int.natAbs k))
      (delta Z (18 * QBinom.Gj g36 k + 6 * QBinom.Gj g36 j) n)) (HeckeMult.zl (2 * M)))) (HeckeMult.zl (2 * M))

/-- **[q^(n+1)] η(6z)η(18z) = dbl M n** (n + 1 < M). -/
theorem dbl27 (M n : Nat) (hn : n + 1 < M) :
    Trunc.nth Z M ((Trunc.TR Z M).mul (Trunc.qe Z M) ((Trunc.TR Z M).mul (QBinom.pp (Trunc.TR Z M) (Frobenius.pw (Trunc.TR Z M) (Trunc.qe Z M) 6) M)
      (QBinom.pp (Trunc.TR Z M) (Frobenius.pw (Trunc.TR Z M) (Trunc.qe Z M) 18) M))) (n + 1) = dbl M n := by
  rw [Trunc.coef27w Z M n hn]
  apply sumK_ext
  intro j
  show Z.mul (Frobenius.pw Z (Z.neg Z.one) (Int.natAbs j))
      (Trunc.shn Z (6 * QBinom.Gj g36 j) (fun i => sumK Z (fun k => Z.mul (Frobenius.pw Z (Z.neg Z.one) (Int.natAbs k))
        (Trunc.shn Z (18 * QBinom.Gj g36 k) (Trunc.nth Z M (Trunc.TR Z M).one) i)) (HeckeMult.zl (2 * M))) n) = _
  have hnM : n < M := Nat.lt_of_succ_lt hn
  rw [Trunc.shn_congr Z (6 * QBinom.Gj g36 j) _
      (fun i => sumK Z (fun k => Z.mul (Frobenius.pw Z (Z.neg Z.one) (Int.natAbs k))
        (Trunc.shn Z (18 * QBinom.Gj g36 k) (GLnLocal.delta Z) i)) (HeckeMult.zl (2 * M))) n (fun i hi => by
      apply sumK_ext
      intro k
      rw [Trunc.shn_congr Z (18 * QBinom.Gj g36 k) (Trunc.nth Z M (Trunc.TR Z M).one) (GLnLocal.delta Z) i (fun i' hi' =>
        Trunc.nth_pw Z M 0 i' (Nat.lt_of_le_of_lt hi' (Nat.lt_of_le_of_lt hi hnM)))]),
    shn_sumK]
  apply congrArg
  apply sumK_ext
  intro k
  rw [shn_smul, shn_shn, shn_delta]

end Hecke36

#print axioms Hecke36.shn_sumK
#print axioms Hecke36.dbl27

lang/DetBlock.lean

lean_all/lang/DetBlock.lean

/-
  lang/DetBlock — T52 (tool for T21), cycle 802: **block-triangular determinants, every size**.
  Columns A (k of them) followed by columns B (m of them) that vanish in the first k rows:
      det (m + k) (A ++ B) = det k A · det m (B with its first k rows dropped).
    lapS_mul     a Laplace walk whose minors factor as D(zs ++ B) = D'(zs) · X factors as (walk of D') · X
    ZR k B       every column of B vanishes in rows 0 … k − 1;   shN k B   B with rows 0 … k − 1 dropped
    det_block    the block formula (induction on k; DetNChain.lapS_split, DetTri.lapS_z0)
  This is the inductive step behind Koszul/Bianchi determinants of general size (T21).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.DetTri
import lib.RingTac

namespace DetBlock

open DetChain DetN DetTri

variable {R : Type} (K : CRing R)

def ZR : Nat → List (Nat → R) → Prop
  | 0, _ => True
  | k + 1, B => Z0 K B ∧ ZR k (B.map shc)

def shN : Nat → List (Nat → R) → List (Nat → R)
  | 0, B => B
  | k + 1, B => shN k (B.map shc)

theorem sgn_fac (j : Nat) (a d X : R) : sgn K j (K.mul a (K.mul d X)) = K.mul (sgn K j (K.mul a d)) X := by
  have e : K.mul a (K.mul d X) = K.mul X (K.mul a d) := by lib_ring
  rw [e, sgn_mul]
  lib_ring

theorem lapS_mul (D D' : List (Nat → R) → R) (X : R) (B : List (Nat → R)) (k : Nat)
    (H : ∀ zs : List (Nat → R), zs.length = k → D ((zs ++ B).map shc) = K.mul (D' (zs.map shc)) X) :
    ∀ (xs pre : List (Nat → R)), (pre ++ xs).length = k + 1 → lapS K D pre xs B = K.mul (lapS K D' pre xs []) X
  | [], _, _ => by
    show K.zero = K.mul K.zero X
    rw [zero_mul' K]
  | x :: xs, pre, hl => by
    have h2 : (pre ++ x :: xs).length = pre.length + (xs.length + 1) := len_app' pre (x :: xs)
    have h3 : (pre ++ xs).length + 1 = k + 1 := by
      rw [len_app']
      exact h2.symm.trans hl
    have hk : (pre ++ xs).length = k := Nat.succ.inj h3
    have hl' : ((pre ++ [x]) ++ xs).length = k + 1 := by
      rw [app_assoc' pre [x] xs]
      exact hl
    show K.add (sgn K pre.length (K.mul (x 0) (D ((pre ++ (xs ++ B)).map shc)))) (lapS K D (pre ++ [x]) xs B)
      = K.mul (K.add (sgn K pre.length (K.mul (x 0) (D' ((pre ++ (xs ++ [])).map shc)))) (lapS K D' (pre ++ [x]) xs [])) X
    rw [← app_assoc' pre xs B, H (pre ++ xs) hk, lapS_mul D D' X B k H xs (pre ++ [x]) hl', app_nil', sgn_fac]
    lib_ring

/-- **Block-triangular determinant.** -/
theorem det_block (m : Nat) : ∀ (k : Nat) (A B : List (Nat → R)), A.length = k → B.length = m → ZR K k B →
    det K (m + k) (A ++ B) = K.mul (det K k A) (det K m (shN k B))
  | 0, [], B, _, _, _ => (K.one_mul _).symm
  | 0, _ :: _, _, hA, _, _ => Nat.noConfusion hA
  | k + 1, A, B, hA, hB, ⟨h0, hz⟩ => by
    have H : ∀ zs : List (Nat → R), zs.length = k →
        det K (m + k) ((zs ++ B).map shc) = K.mul (det K k (zs.map shc)) (det K m (shN k (B.map shc))) := fun zs hz' => by
      rw [map_app']
      exact det_block m k (zs.map shc) (B.map shc) ((len_map' shc zs).trans hz') ((len_map' shc B).trans hB) hz
    show lapS K (det K (m + k)) [] (A ++ B) [] = K.mul (lapS K (det K k) [] A []) (det K m (shN k (B.map shc)))
    rw [lapS_split K (det K (m + k)) [] A B [], app_nil', lapS_z0 K (det K (m + k)) B ([] ++ A) [] h0, add_zero' K]
    exact lapS_mul K (det K (m + k)) (det K k) _ B k H A [] hA

end DetBlock

#print axioms DetBlock.sgn_fac
#print axioms DetBlock.lapS_mul
#print axioms DetBlock.det_block

lang/DetColOp.lean

lean_all/lang/DetColOp.lean

/-
  lang/DetColOp — T21 retry (T52 tool), cycle 805: **elementary column operations keep the determinant, every size n**.
  Adding c times another column v to the column u (clin 1 u c v = u + c·v) does not change det, whether v stands to the left or
  to the right of u (DetNChain.det_lin, det_eq_cols). With DetNChain.det_swap and DetTri.det_tri this turns an integer
  elimination certificate into a proof of det M = ± (product of pivots) ≠ 0 — the form needed for the Bianchi relation matrices of T21.
    colop_right   det (P ++ (u + c v) :: (Q₁ ++ v :: Q₂)) = det (P ++ u :: (Q₁ ++ v :: Q₂))
    colop_left    det ((P₁ ++ v :: P₂) ++ (u + c v) :: Q) = det ((P₁ ++ v :: P₂) ++ u :: Q)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import DetNChain
import lib.RingTac

namespace DetColOp

open DetChain DetN

variable {R : Type} (K : CRing R)

theorem lin1 (a c : R) : K.add (K.mul K.one a) (K.mul c K.zero) = a := by lib_ring

theorem colop_right (n : Nat) (P Q1 Q2 : List (Nat → R)) (u v : Nat → R) (c : R)
    (hl : (P ++ u :: (Q1 ++ v :: Q2)).length = n) :
    det K n (P ++ clin K K.one u c v :: (Q1 ++ v :: Q2)) = det K n (P ++ u :: (Q1 ++ v :: Q2)) := by
  have hl' : (P ++ v :: (Q1 ++ v :: Q2)).length = n := by
    rw [len_app'] at hl ⊢
    exact hl
  rw [det_lin K n P (Q1 ++ v :: Q2) u v K.one c hl, det_eq_cols K n Q2 v Q1 P hl', lin1]

theorem colop_left (n : Nat) (P1 P2 Q : List (Nat → R)) (u v : Nat → R) (c : R)
    (hl : ((P1 ++ v :: P2) ++ u :: Q).length = n) :
    det K n ((P1 ++ v :: P2) ++ clin K K.one u c v :: Q) = det K n ((P1 ++ v :: P2) ++ u :: Q) := by
  have e : (P1 ++ v :: P2) ++ v :: Q = P1 ++ v :: (P2 ++ v :: Q) := app_assoc' P1 (v :: P2) (v :: Q)
  have hl' : (P1 ++ v :: (P2 ++ v :: Q)).length = n := by
    rw [← e, len_app']
    rw [len_app'] at hl
    exact hl
  rw [det_lin K n (P1 ++ v :: P2) Q u v K.one c hl, e, det_eq_cols K n Q v P2 P1 hl', lin1]

end DetColOp

#print axioms DetColOp.lin1
#print axioms DetColOp.colop_right
#print axioms DetColOp.colop_left

lang/DetTri.lean

lean_all/lang/DetTri.lean

/-
  lang/DetTri — T52 (tool for T21), cycle 801: **the determinant of a triangular matrix, every size n**.
  DetNChain.det is the Laplace expansion along row 0 of a list of columns. Here:
    Z0 cs          every column of cs vanishes in row 0
    lapS_z0        a Laplace walk over columns vanishing in row 0 is 0
    det_first      Z0 cs ⇒ det (n+1) (c :: cs) = c(0) · det n (cs with row 0 dropped)        (block step)
    Tri n, dg n    lower triangular of size n (Z0 of the later columns, then the same for the minor) and the diagonal product
    det_tri        Tri n cs, |cs| = n ⇒ det n cs = Π diagonal                                (induction on n)
  Lean 4 core only, proofs by hand, no axioms.
-/
import DetNChain

namespace DetTri

open DetChain DetN

variable {R : Type} (K : CRing R)

/-- every column vanishes in row 0. -/
def Z0 : List (Nat → R) → Prop
  | [] => True
  | d :: ds => d 0 = K.zero ∧ Z0 ds

/-- lower triangular of size n (fuel n). -/
def Tri : Nat → List (Nat → R) → Prop
  | 0, _ => True
  | _ + 1, [] => True
  | n + 1, _ :: cs => Z0 K cs ∧ Tri n (cs.map shc)

/-- the diagonal product (fuel n). -/
def dg : Nat → List (Nat → R) → R
  | 0, _ => K.one
  | _ + 1, [] => K.one
  | n + 1, c :: cs => K.mul (c 0) (dg n (cs.map shc))

theorem lapS_z0 (D : List (Nat → R) → R) : ∀ (xs pre suf : List (Nat → R)), Z0 K xs → lapS K D pre xs suf = K.zero
  | [], _, _, _ => rfl
  | x :: xs, pre, suf, ⟨h0, h⟩ => by
    show K.add (sgn K pre.length (K.mul (x 0) (D ((pre ++ (xs ++ suf)).map shc)))) (lapS K D (pre ++ [x]) xs suf) = K.zero
    rw [h0, zero_mul' K, sgn_zero, lapS_z0 D xs (pre ++ [x]) suf h, K.zero_add]

/-- **Block step**: a first row (c(0), 0, …, 0) splits off. -/
theorem det_first (n : Nat) (c : Nat → R) (cs : List (Nat → R)) (h : Z0 K cs) :
    det K (n + 1) (c :: cs) = K.mul (c 0) (det K n (cs.map shc)) := by
  show K.add (K.mul (c 0) (det K n ((cs ++ []).map shc))) (lapS K (det K n) [c] cs []) = _
  rw [lapS_z0 K (det K n) cs [c] [] h, app_nil', add_zero' K]

/-- **Triangular determinant**: det = product of the diagonal, every size. -/
theorem det_tri : ∀ (n : Nat) (cs : List (Nat → R)), cs.length = n → Tri K n cs → det K n cs = dg K n cs
  | 0, _, _, _ => rfl
  | n + 1, [], hl, _ => Nat.noConfusion hl
  | n + 1, c :: cs, hl, ⟨h0, ht⟩ => by
    have hl' : (cs.map shc).length = n := (len_map' shc cs).trans (Nat.succ.inj hl)
    show det K (n + 1) (c :: cs) = K.mul (c 0) (dg K n (cs.map shc))
    rw [det_first K n c cs h0, det_tri n (cs.map shc) hl' ht]

end DetTri

#print axioms DetTri.lapS_z0
#print axioms DetTri.det_first
#print axioms DetTri.det_tri

lang/Deuring.lean

lean_all/lang/Deuring.lean

/-
  lang/Deuring — T43, cycle 108: supersingular ⇔ inert for the two CM curves (Deuring's criterion, both directions).

  p = 2k + 3 prime, a_p = −S with S = Σ_{𝔽_p} χ(x³ − x), resp. ψ(1) = Σ_{𝔽_p} χ(x³ + 1).
    big_sq, not_div   a ≠ 0, a² + c b² = p ⇒ p ∤ 2a          (|a| < p)
    deuring_i         p | S  ⇔  p ≡ 3 (mod 4)                 (y² = x³ − x; split: S = −2a, a odd, a² + b² = p)
    deuring_omega     p | ψ(1)  ⇔  p ≡ 2 (mod 3), p > 3        (y² = x³ + 1; split: ψ(1) = −2a, a ≡ 1 (mod 3), a² + 3b² = p)
  The reduction is supersingular (a_p ≡ 0 mod p) exactly at the primes inert in the CM field — the GL2 statement whose
  GL1 side is the splitting law of ℚ(i), ℚ(√−3) (GaussSplit, Eisen).

  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.EveryPrime3

namespace Deuring

open FTAChain ZMod YMChain FiniteRecChain LibInt

theorem succ_add_ne (a X : Nat) (h : a + (X + 1) = a) : False :=
  Nat.not_succ_le_self (a + X) (by
    show Nat.succ (a + X) ≤ a + X
    rw [show Nat.succ (a + X) = a + (X + 1) from rfl, h]; exact Nat.le_add_right a X)

theorem big_sq (k n m : Nat) (h : (2 * k + 1 + 1 + 1) * (2 * k + 1 + 1 + 1) * (n + 1) + m = 2 * k + 1 + 1 + 1) : False :=
  succ_add_ne (2 * k + 1 + 1 + 1) (4 * k * k + 10 * k + 5 + (2 * k + 1 + 1 + 1) * (2 * k + 1 + 1 + 1) * n + m) (by
    rw [show (2 * k + 1 + 1 + 1) + (4 * k * k + 10 * k + 5 + (2 * k + 1 + 1 + 1) * (2 * k + 1 + 1 + 1) * n + m + 1)
        = (2 * k + 1 + 1 + 1) * (2 * k + 1 + 1 + 1) * (n + 1) + m by lib_nat]
    exact h)

/-- **p ∤ 2a when a ≠ 0 and a² + c b² = p.** -/
theorem not_div (k c : Nat) (a b q : Int) (ha : a ≠ 0) (hab : a * a + Int.ofNat c * (b * b) = Int.ofNat (2 * k + 1 + 1 + 1))
    (hq : -(2 * a) = Int.ofNat (2 * k + 1 + 1 + 1) * q) : False := by
  have hP : Int.ofNat (2 * k + 1 + 1 + 1) = 2 * (Int.ofNat k + 1) + 1 := by show 2 * Int.ofNat k + 1 + 1 + 1 = _; lib_int
  match GaussInt.int_even_odd q with
  | Or.inr ⟨q', hq'⟩ =>
    exact GaussInt.int_odd_ne_even (2 * (Int.ofNat k + 1) * q' + Int.ofNat k + 1 + q') (-a)
      (by rw [show 2 * -a = -(2 * a) by lib_int, hq, hq', hP]; lib_int)
  | Or.inl ⟨q', hq'⟩ =>
    have ha' : a = -(Int.ofNat (2 * k + 1 + 1 + 1) * q') :=
      FinPrimary.two_cancel _ _ (by
        calc 2 * a = -(-(2 * a)) := by lib_int
          _ = 2 * -(Int.ofNat (2 * k + 1 + 1 + 1) * q') := by rw [hq, hq']; lib_int)
    match FinCubicSign.sq_nn q', FinCubicSign.sq_nn b with
    | ⟨0, h0⟩, _ => exact ha (by rw [ha', HasseDavenport.sq_zero q' h0]; lib_int)
    | ⟨n + 1, hn⟩, ⟨nb, hnb⟩ =>
      apply big_sq k n (c * nb)
      apply Int.ofNat.inj
      show Int.ofNat (2 * k + 1 + 1 + 1) * Int.ofNat (2 * k + 1 + 1 + 1) * (Int.ofNat n + 1) + Int.ofNat c * Int.ofNat nb = _
      have : q' * q' = Int.ofNat n + 1 := hn
      calc Int.ofNat (2 * k + 1 + 1 + 1) * Int.ofNat (2 * k + 1 + 1 + 1) * (Int.ofNat n + 1) + Int.ofNat c * Int.ofNat nb
          = Int.ofNat (2 * k + 1 + 1 + 1) * Int.ofNat (2 * k + 1 + 1 + 1) * (q' * q') + Int.ofNat c * (b * b) := by rw [this, hnb]
        _ = a * a + Int.ofNat c * (b * b) := by rw [ha']; lib_int
        _ = _ := hab


/-- **Deuring for y² = x³ − x: p | a_p ⇔ p ≡ 3 (mod 4).** -/
theorem deuring_i (k : Nat) (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = 2 * k + 1 + 1 + 1) :
    (∃ q : Int, sumK LibInt.intR (fun x => QChar.chi k (CMInert.cub (ring (2 * k + 1 + 1)) x)) (ConicSum.L k) = Int.ofNat (2 * k + 1 + 1 + 1) * q)
      ↔ ∃ j, k = 2 * j := by
  constructor
  · intro ⟨q, hq⟩
    cases EveryPrime.nat_even_odd k with
    | inl h => exact h
    | inr h =>
      exfalso
      match h with
      | ⟨j, hj⟩ =>
        subst hj
        match SignCM3.ap_primary j hpr with
        | ⟨a, b, hab, ⟨⟨e, he⟩, ⟨t, ht⟩⟩, hS⟩ =>
          have he' : b = 2 * e := he
          have ht' : a + b - 1 = 4 * t := ht
          have hS' : -sumK LibInt.intR (fun x => QChar.chi (2 * j + 1) (CMInert.cub (ring (2 * (2 * j + 1) + 1 + 1)) x)) (ConicSum.L (2 * j + 1)) = 2 * a := hS
          have ha : a ≠ 0 := fun h0 => GaussInt.int_odd_ne_even (2 * t - e) 0 (by
            calc 2 * (2 * t - e) + 1 = (a + b - 1) - b + 1 := by rw [ht', he']; lib_int
              _ = a := by lib_int
              _ = 2 * 0 := by rw [h0]; lib_int)
          generalize sumK LibInt.intR (fun x => QChar.chi (2 * j + 1) (CMInert.cub (ring (2 * (2 * j + 1) + 1 + 1)) x)) (ConicSum.L (2 * j + 1)) = S at hq hS'
          exact not_div (2 * j + 1) 1 a b q ha (by show a * a + 1 * (b * b) = _; rw [← hab]; lib_int) (by rw [← hS', hq]; lib_int)
  · intro ⟨j, hj⟩
    subst hj
    exact ⟨0, by rw [InertFq.s1_inert j hpr]; lib_int⟩

/-- **Deuring for y² = x³ + 1: p | a_p ⇔ p ≡ 2 (mod 3)** (p = 2k + 3 > 3). -/
theorem deuring_omega (k : Nat) (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = 2 * k + 1 + 1 + 1) (hk : 1 ≤ k) :
    (∃ q : Int, CubicJacob.psi k (ring (2 * k + 1 + 1)).one = Int.ofNat (2 * k + 1 + 1 + 1) * q) ↔ ∃ t, k = 3 * t + 1 := by
  constructor
  · intro ⟨q, hq⟩
    cases EveryPrime3.nat_mod3 k with
    | inl h =>
      exfalso
      match h with
      | ⟨t, ht⟩ =>
        cases hpr 3 ⟨2 * t + 1, by rw [ht]; lib_nat⟩ with
        | inl e => exact Nat.noConfusion (Nat.succ.inj (show (2 : Nat) + 1 = 0 + 1 from e))
        | inr e =>
          have e' : 2 * k = 0 := Nat.succ.inj (Nat.succ.inj (Nat.succ.inj e.symm))
          cases k with
          | zero => exact Nat.not_succ_le_zero 0 hk
          | succ k' => exact Nat.noConfusion (show 2 * k' + 1 + 1 = 0 from e')
    | inr h =>
      cases h with
      | inl h => exact h
      | inr h =>
        exfalso
        match h with
        | ⟨t, ht⟩ =>
          subst ht
          match CubicSign.cubic_exact (3 * t + 2) hpr (2 * t + 2) (by lib_nat) with
          | ⟨a, b, hab, ⟨u, hu⟩, hS⟩ =>
            have hS' : -CubicJacob.psi (3 * t + 2) (ring (2 * (3 * t + 2) + 1 + 1)).one = 2 * a := hS
            have ha : a ≠ 0 := fun h0 => CubicSuper.three_ne_one (-u) (by
              calc 3 * -u = -(3 * u) := by lib_int
                _ = -(a - 1) := by rw [hu]
                _ = 1 := by rw [h0]; lib_int)
            generalize CubicJacob.psi (3 * t + 2) (ring (2 * (3 * t + 2) + 1 + 1)).one = S at hq hS'
            exact not_div (3 * t + 2) 3 a b q ha hab (by rw [← hS', hq]; lib_int)
  · intro ⟨t, ht⟩
    subst ht
    exact ⟨0, by rw [SexticFamily.psi_inert (3 * t + 1) hpr (4 * t + 3) (by lib_nat)]; lib_int⟩

end Deuring

#print axioms Deuring.succ_add_ne
#print axioms Deuring.big_sq
#print axioms Deuring.not_div
#print axioms Deuring.deuring_i
#print axioms Deuring.deuring_omega

lang/DeuringTwist.lean

lean_all/lang/DeuringTwist.lean

/-
  lang/DeuringTwist — T43, cycle 132: Deuring's criterion for every twist y² = x³ + Dx and y² = x³ + D (D ≠ 0).

    deuring_quartic   p | φ_p(D)  ⇔  p ≡ 3 (mod 4)          (split: φ_p(D) = ±2w, w² + w'² = p, w ≠ 0)
    deuring_sextic    p | ψ_p(D)  ⇔  p ≡ 2 (mod 3), p > 3   (split: ψ_p(D)² + 3B² = 4p, so ψ ≠ 0 and |ψ| < p)
  Every CM curve over ℚ with j = 1728 or j = 0 has supersingular reduction exactly at the primes inert in its CM field.

  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.FamilyBound
import lang.Deuring

namespace DeuringTwist

open FTAChain ZMod YMChain FiniteRecChain LibInt

theorem deuring_quartic (k : Nat) (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = 2 * k + 1 + 1 + 1)
    (D : Zp (2 * k + 1 + 1)) (hD : D ≠ (ring (2 * k + 1 + 1)).zero) :
    (∃ q : Int, Jacobsthal.phi k D = Int.ofNat (2 * k + 1 + 1 + 1) * q) ↔ ∃ j, k = 2 * j := by
  constructor
  · intro ⟨q, hq⟩
    cases EveryPrime.nat_even_odd k with
    | inl h => exact h
    | inr h =>
      exfalso
      match h with
      | ⟨j, hj⟩ =>
        subst hj
        match QuarticFamily.family_trace j hpr D hD with
        | ⟨u, v, huv, hc⟩ =>
          have hP : Int.ofNat u * Int.ofNat u + 1 * (Int.ofNat v * Int.ofNat v) = Int.ofNat (2 * (2 * j + 1) + 1 + 1 + 1) := by
            rw [show (1 : Int) * (Int.ofNat v * Int.ofNat v) = Int.ofNat v * Int.ofNat v by lib_int]; exact congrArg Int.ofNat huv
          have hP' : Int.ofNat v * Int.ofNat v + 1 * (Int.ofNat u * Int.ofNat u) = Int.ofNat (2 * (2 * j + 1) + 1 + 1 + 1) := by
            rw [← hP]; lib_int
          have hu0 : ∀ w w' : Nat, w * w + w' * w' = 2 * (2 * j + 1) + 1 + 1 + 1 → Int.ofNat w ≠ 0 := fun w w' hw e => by
            have hw0 : w = 0 := Int.ofNat.inj e
            rw [hw0] at hw
            match w' with
            | 0 => exact Nat.noConfusion hw
            | w'' + 1 =>
              exact TwoSquares.not_square (2 * (2 * j + 1) + 1 + 1) w'' (Nat.le_add_left 1 _) (fun d hd => hpr d hd)
                ((show (w'' + 1) * (w'' + 1) = 0 * 0 + (w'' + 1) * (w'' + 1) by lib_nat).trans hw)
          have huv' : v * v + u * u = 2 * (2 * j + 1) + 1 + 1 + 1 := by rw [← huv]; lib_nat
          generalize Jacobsthal.phi (2 * j + 1) D = S at hq hc
          rcases hc with e | e | e | e
          · exact Deuring.not_div (2 * j + 1) 1 (-Int.ofNat u) (Int.ofNat v) q
              (fun h0 => hu0 u v huv (by rw [show Int.ofNat u = -(-Int.ofNat u) by lib_int, h0]; rfl))
              (by show (-Int.ofNat u) * (-Int.ofNat u) + 1 * (Int.ofNat v * Int.ofNat v) = _; rw [← hP]; lib_int) (by rw [← hq, e]; show -(2 * -Int.ofNat u) = 2 * Int.ofNat u; lib_int)
          · exact Deuring.not_div (2 * j + 1) 1 (Int.ofNat u) (Int.ofNat v) q (hu0 u v huv)
              hP (by rw [← hq, e]; show -(2 * Int.ofNat u) = -(2 * Int.ofNat u); rfl)
          · exact Deuring.not_div (2 * j + 1) 1 (-Int.ofNat v) (Int.ofNat u) q
              (fun h0 => hu0 v u huv' (by rw [show Int.ofNat v = -(-Int.ofNat v) by lib_int, h0]; rfl))
              (by show (-Int.ofNat v) * (-Int.ofNat v) + 1 * (Int.ofNat u * Int.ofNat u) = _; rw [← hP']; lib_int) (by rw [← hq, e]; show -(2 * -Int.ofNat v) = 2 * Int.ofNat v; lib_int)
          · exact Deuring.not_div (2 * j + 1) 1 (Int.ofNat v) (Int.ofNat u) q (hu0 v u huv')
              hP' (by rw [← hq, e]; show -(2 * Int.ofNat v) = -(2 * Int.ofNat v); rfl)
  · intro ⟨j, hj⟩
    subst hj
    exact ⟨0, by rw [QuarticFamily.phi_inert (2 * j) hpr (BaseChange.chi_negone j (TwoPower.negone_odd' (2 * (2 * j) + 1) j)) D]; lib_int⟩

theorem deuring_sextic (k : Nat) (hk : 1 ≤ k) (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = 2 * k + 1 + 1 + 1)
    (D : Zp (2 * k + 1 + 1)) (hD : D ≠ (ring (2 * k + 1 + 1)).zero) :
    (∃ q : Int, FinCubic.psi (FinField.ffp k hpr) D = Int.ofNat (2 * k + 1 + 1 + 1) * q) ↔ ∃ t, k = 3 * t + 1 := by
  constructor
  · intro ⟨q, hq⟩
    cases EveryPrime3.nat_mod3 k with
    | inl h =>
      exfalso
      match h with
      | ⟨t, ht⟩ =>
        cases hpr 3 ⟨2 * t + 1, by rw [ht]; lib_nat⟩ with
        | inl e => exact Nat.noConfusion (Nat.succ.inj (show (2 : Nat) + 1 = 0 + 1 from e))
        | inr e =>
          have e' : 2 * k = 0 := Nat.succ.inj (Nat.succ.inj (Nat.succ.inj e.symm))
          cases k with
          | zero => exact Nat.not_succ_le_zero 0 hk
          | succ k' => exact Nat.noConfusion (show 2 * k' + 1 + 1 = 0 from e')
    | inr h =>
      cases h with
      | inl h => exact h
      | inr h =>
        exfalso
        match h with
        | ⟨t, ht⟩ =>
          subst ht
          match FinCubicNorm.norm_twist (FinField.ffp (3 * t + 2) hpr) (2 * t + 2) (by show 2 * (3 * t + 2 + 1) = _; lib_nat) D hD with
          | ⟨B, hB⟩ =>
            have hB' : FinCubic.psi (FinField.ffp (3 * t + 2) hpr) D * FinCubic.psi (FinField.ffp (3 * t + 2) hpr) D + 3 * (B * B)
                = 4 * Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) := hB
            rw [hq] at hB'
            match FinCubicSign.sq_nn q, FinCubicSign.sq_nn B with
            | ⟨0, h0⟩, ⟨nb, hnb⟩ =>
              rw [HasseDavenport.sq_zero q h0] at hB'
              exact CubicSuper.three_ne_one (B * B - 8 * Int.ofNat t - 9) (by
                have hP : Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) = 6 * Int.ofNat t + 7 := by show 2 * (3 * Int.ofNat t + 2) + 1 + 1 + 1 = _; lib_int
                rw [hP] at hB'
                calc 3 * (B * B - 8 * Int.ofNat t - 9) = ((6 * Int.ofNat t + 7) * 0 * ((6 * Int.ofNat t + 7) * 0) + 3 * (B * B)) - 24 * Int.ofNat t - 27 := by lib_int
                  _ = 1 := by rw [hB']; lib_int)
            | ⟨n + 1, hn⟩, ⟨nb, hnb⟩ =>
              apply Deuring.succ_add_ne (4 * (6 * t + 7)) ((36 * t * t + 60 * t + 20) + (6 * t + 7) * (6 * t + 7) * n + 3 * nb)
              apply Int.ofNat.inj
              show 4 * (6 * Int.ofNat t + 7) + ((36 * Int.ofNat t * Int.ofNat t + 60 * Int.ofNat t + 20) + (6 * Int.ofNat t + 7) * (6 * Int.ofNat t + 7) * Int.ofNat n + 3 * Int.ofNat nb + 1)
                = 4 * (6 * Int.ofNat t + 7)
              have hP : Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) = 6 * Int.ofNat t + 7 := by show 2 * (3 * Int.ofNat t + 2) + 1 + 1 + 1 = _; lib_int
              rw [hP] at hB'
              have hqq : q * q = Int.ofNat n + 1 := hn
              calc 4 * (6 * Int.ofNat t + 7) + ((36 * Int.ofNat t * Int.ofNat t + 60 * Int.ofNat t + 20) + (6 * Int.ofNat t + 7) * (6 * Int.ofNat t + 7) * Int.ofNat n + 3 * Int.ofNat nb + 1)
                  = (6 * Int.ofNat t + 7) * (6 * Int.ofNat t + 7) * (q * q) + 3 * (B * B) := by rw [hqq, hnb]; lib_int
                _ = 4 * (6 * Int.ofNat t + 7) := by rw [← hB']; lib_int
  · intro ⟨t, ht⟩
    subst ht
    exact ⟨0, by
      rw [show FinCubic.psi (FinField.ffp (3 * t + 1) hpr) D = CubicJacob.psi (3 * t + 1) D from rfl,
        SexticFamily.psi_inert (3 * t + 1) hpr (4 * t + 3) (by lib_nat) D]; lib_int⟩

end DeuringTwist

#print axioms DeuringTwist.deuring_quartic
#print axioms DeuringTwist.deuring_sextic

lang/Dihedral.lean

lean_all/lang/Dihedral.lean

/-
  lang/Dihedral — T43, cycle 5 (GL2 over ℚ, CM / dihedral case from GL1 over K).

  G = Gal(L/ℚ), H = Gal(L/K) of index 2 (K imaginary quadratic), σ ∈ G \ H. For a character ψ of H
  (a Hecke character of K, through GL1 over K), the induced representation Ind_H^G ψ has trace
      split p  (Frob_p ∈ H):   ψ(Frob) + ψ(σ Frob σ⁻¹)      = ψ(p) + ψ(p̄)
      inert p  (Frob_p ∉ H):   0
  which is the theta series a(p) of ψ. In the normalisation of FiniteRecChain (Ind = |H| × trace):
    ind_split    x ∈ H  ⇒  ind ψ x = |H| ψ(x) + |H| ψ(σ x σ⁻¹)
    ind_inert    x ∉ H  ⇒  ind ψ x = 0
  Hypotheses: G = H ∪ σH as lists (hsplit), H normal and closed under conjugation, ψ a class
  function on H. Checked first on y² = x³ − x (CM by ℚ(i)): a_p = π + π̄ (π ≡ 1 mod 2+2i) for
  p ≡ 1 (4) and a_p = 0 for p ≡ 3 (4), all p < 400, exactly.

  Lean 4 core only, proofs by hand, no axioms.
-/
import FiniteRecChain

namespace Dihedral

open YMChain FiniteRecChain

variable {R : Type} (Rg : DetChain.CRing R) {G : Type} [DecidableEq G] (K : Grp G)

def conjG (a y : G) : G := K.mul (K.mul a y) (K.inv a)

omit [DecidableEq G] in
theorem sum_const {A : Type} (c : R) (f : A → R) : ∀ l : List A, (∀ a, memL a l → f a = c) →
    sumK Rg f l = Rg.mul (sumK Rg (fun _ => Rg.one) l) c
  | [], _ => (DetChain.zero_mul' Rg c).symm
  | a :: l, h => by
    show Rg.add (f a) (sumK Rg f l) = Rg.mul (Rg.add Rg.one (sumK Rg (fun _ => Rg.one) l)) c
    rw [h a (Or.inl rfl), sum_const c f l (fun b hb => h b (Or.inr hb)), DetChain.right_distrib, Rg.one_mul]

variable (inH : G → Bool)

omit [DecidableEq G] in
/-- **Split primes.** -/
theorem ind_split (els Hl : List G) (σ : G) (ψ : G → R)
    (hsplit : ∀ F : G → R, sumK Rg F els = Rg.add (sumK Rg F Hl) (sumK Rg (fun h => F (K.mul σ h)) Hl))
    (hHl : ∀ h, memL h Hl → inH h = true)
    (hcl : ∀ h y, inH h = true → inH y = true → inH (conjG K h y) = true)
    (hnorm : ∀ y, inH y = true → inH (conjG K σ y) = true)
    (hcls : ∀ h y, inH h = true → inH y = true → ψ (conjG K h y) = ψ y)
    (x : G) (hx : inH x = true) :
    ind Rg K els inH ψ x
      = Rg.add (Rg.mul (sumK Rg (fun _ => Rg.one) Hl) (ψ x))
               (Rg.mul (sumK Rg (fun _ => Rg.one) Hl) (ψ (conjG K σ x))) := by
  unfold ind
  rw [hsplit]
  have e1 : sumK Rg (fun g => ext0 Rg inH ψ (K.mul (K.mul g x) (K.inv g))) Hl
      = Rg.mul (sumK Rg (fun _ => Rg.one) Hl) (ψ x) :=
    sum_const Rg (ψ x) _ Hl (fun h hh => by
      show (if inH (conjG K h x) = true then ψ (conjG K h x) else Rg.zero) = ψ x
      rw [ite_pos' _ _ (hcl h x (hHl h hh) hx)]
      exact hcls h x (hHl h hh) hx)
  have e2 : sumK Rg (fun h => ext0 Rg inH ψ (K.mul (K.mul (K.mul σ h) x) (K.inv (K.mul σ h)))) Hl
      = Rg.mul (sumK Rg (fun _ => Rg.one) Hl) (ψ (conjG K σ x)) :=
    sum_const Rg (ψ (conjG K σ x)) _ Hl (fun h hh => by
      -- (σh) x (σh)⁻¹ = (σ h σ⁻¹) (σ x σ⁻¹) (σ h σ⁻¹)⁻¹
      have hc : K.mul (K.mul (K.mul σ h) x) (K.inv (K.mul σ h)) = conjG K (conjG K σ h) (conjG K σ x) := by
        have cc := conj_conj K (conjG K σ h) σ x
        have e : K.mul (conjG K σ h) σ = K.mul σ h := by
          show K.mul (K.mul (K.mul σ h) (K.inv σ)) σ = K.mul σ h
          rw [K.assoc, K.inv_mul, K.mul_one]
        rw [e] at cc
        exact cc.symm
      show ext0 Rg inH ψ (K.mul (K.mul (K.mul σ h) x) (K.inv (K.mul σ h))) = ψ (conjG K σ x)
      rw [hc]
      have hh' := hnorm h (hHl h hh)
      have hx' := hnorm x hx
      show (if inH (conjG K (conjG K σ h) (conjG K σ x)) = true then ψ (conjG K (conjG K σ h) (conjG K σ x))
        else Rg.zero) = ψ (conjG K σ x)
      rw [ite_pos' _ _ (hcl _ _ hh' hx')]
      exact hcls _ _ hh' hx')
  rw [e1, e2]

omit [DecidableEq G] in
/-- **Inert primes.** -/
theorem ind_inert (els : List G) (ψ : G → R)
    (hout : ∀ g y, inH y = false → inH (conjG K g y) = false) (x : G) (hx : inH x = false) :
    ind Rg K els inH ψ x = Rg.zero := by
  unfold ind
  have : ∀ l : List G, sumK Rg (fun g => ext0 Rg inH ψ (K.mul (K.mul g x) (K.inv g))) l = Rg.zero := by
    intro l
    induction l with
    | nil => rfl
    | cons g l ih =>
      have hf : ¬ inH (conjG K g x) = true := by rw [hout g x hx]; exact Bool.noConfusion
      have hz : ext0 Rg inH ψ (conjG K g x) = Rg.zero := by
        show (if inH (conjG K g x) = true then ψ (conjG K g x) else Rg.zero) = Rg.zero
        rw [ite_neg' _ _ hf]
      show Rg.add (ext0 Rg inH ψ (conjG K g x)) _ = Rg.zero
      rw [hz, ih, Rg.zero_add]
  exact this els

end Dihedral

#print axioms Dihedral.sum_const
#print axioms Dihedral.ind_split
#print axioms Dihedral.ind_inert

lang/DualL.lean

lean_all/lang/DualL.lean

/-
  lang/DualL — T49 (primary elements, step 1): **dual numbers R[π]/(π²) over a commutative ring R**, and the
  ring maps ψ_c : (a, b) ↦ (a, c·b) (on R[ζ]/(1 − ζ)² this is σ_c, since (1 + π)^c = 1 + cπ).
    D, dext, dring       the ring structure on R × R, (a, b)(a', b') = (aa', ab' + ba')
    pi, pi_sq            π = (0, 1), π² = 0
    psi, psi_add, psi_mul, psi_one, psi_pi      ψ_c is a ring map with ψ_c(1 + π) = 1 + cπ
    snd_mul_zero         b = b' = 0 ⇒ the π-part of the product is 0
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import DetChain
import lib.RingTac

namespace DualL

open DetChain

variable {R : Type} (K : CRing R)

theorem dext {a b c d : R} (h1 : a = c) (h2 : b = d) : (a, b) = (c, d) := by rw [h1, h2]

def dadd (x y : R × R) : R × R := (K.add x.1 y.1, K.add x.2 y.2)
def dmul (x y : R × R) : R × R := (K.mul x.1 y.1, K.add (K.mul x.1 y.2) (K.mul x.2 y.1))
def dneg (x : R × R) : R × R := (K.neg x.1, K.neg x.2)

/-- **R[π]/(π²)**. -/
def dring : CRing (R × R) where
  add := dadd K
  mul := dmul K
  neg := dneg K
  zero := (K.zero, K.zero)
  one := (K.one, K.zero)
  add_assoc := fun a b c => by
    show (K.add (K.add a.1 b.1) c.1, K.add (K.add a.2 b.2) c.2) = (K.add a.1 (K.add b.1 c.1), K.add a.2 (K.add b.2 c.2))
    exact dext (by lib_ring) (by lib_ring)
  add_comm := fun a b => by
    show (K.add a.1 b.1, K.add a.2 b.2) = (K.add b.1 a.1, K.add b.2 a.2)
    exact dext (by lib_ring) (by lib_ring)
  zero_add := fun a => by
    show (K.add K.zero a.1, K.add K.zero a.2) = a
    exact dext (by lib_ring) (by lib_ring)
  add_neg := fun a => by
    show (K.add a.1 (K.neg a.1), K.add a.2 (K.neg a.2)) = (K.zero, K.zero)
    exact dext (by lib_ring) (by lib_ring)
  mul_assoc := fun a b c => by
    show (K.mul (K.mul a.1 b.1) c.1, K.add (K.mul (K.mul a.1 b.1) c.2) (K.mul (K.add (K.mul a.1 b.2) (K.mul a.2 b.1)) c.1))
      = (K.mul a.1 (K.mul b.1 c.1), K.add (K.mul a.1 (K.add (K.mul b.1 c.2) (K.mul b.2 c.1))) (K.mul a.2 (K.mul b.1 c.1)))
    exact dext (by lib_ring) (by lib_ring)
  mul_comm := fun a b => by
    show (K.mul a.1 b.1, K.add (K.mul a.1 b.2) (K.mul a.2 b.1)) = (K.mul b.1 a.1, K.add (K.mul b.1 a.2) (K.mul b.2 a.1))
    exact dext (by lib_ring) (by lib_ring)
  one_mul := fun a => by
    show (K.mul K.one a.1, K.add (K.mul K.one a.2) (K.mul K.zero a.1)) = a
    exact dext (by lib_ring) (by lib_ring)
  distrib := fun a b c => by
    show (K.mul a.1 (K.add b.1 c.1), K.add (K.mul a.1 (K.add b.2 c.2)) (K.mul a.2 (K.add b.1 c.1)))
      = (K.add (K.mul a.1 b.1) (K.mul a.1 c.1),
          K.add (K.add (K.mul a.1 b.2) (K.mul a.2 b.1)) (K.add (K.mul a.1 c.2) (K.mul a.2 c.1)))
    exact dext (by lib_ring) (by lib_ring)

def pi : R × R := (K.zero, K.one)

theorem pi_sq : (dring K).mul (pi K) (pi K) = (dring K).zero := by
  show (K.mul K.zero K.zero, K.add (K.mul K.zero K.one) (K.mul K.one K.zero)) = (K.zero, K.zero)
  exact dext (by lib_ring) (by lib_ring)

/-- ψ_c(a, b) = (a, c·b). -/
def psi (c : R) (x : R × R) : R × R := (x.1, K.mul c x.2)

theorem psi_add (c : R) (x y : R × R) : psi K c ((dring K).add x y) = (dring K).add (psi K c x) (psi K c y) := by
  show (K.add x.1 y.1, K.mul c (K.add x.2 y.2)) = (K.add x.1 y.1, K.add (K.mul c x.2) (K.mul c y.2))
  exact dext rfl (by lib_ring)

theorem psi_mul (c : R) (x y : R × R) : psi K c ((dring K).mul x y) = (dring K).mul (psi K c x) (psi K c y) := by
  show (K.mul x.1 y.1, K.mul c (K.add (K.mul x.1 y.2) (K.mul x.2 y.1)))
    = (K.mul x.1 y.1, K.add (K.mul x.1 (K.mul c y.2)) (K.mul (K.mul c x.2) y.1))
  exact dext rfl (by lib_ring)

theorem psi_one (c : R) : psi K c (dring K).one = (dring K).one := by
  show (K.one, K.mul c K.zero) = (K.one, K.zero)
  exact dext rfl (by lib_ring)

theorem psi_pi (c : R) : psi K c ((dring K).add (dring K).one (pi K)) = (dring K).add (dring K).one ((dring K).mul (c, K.zero) (pi K)) := by
  show (K.add K.one K.zero, K.mul c (K.add K.zero K.one))
    = (K.add K.one (K.mul c K.zero), K.add K.zero (K.add (K.mul c K.one) (K.mul K.zero K.zero)))
  exact dext (by lib_ring) (by lib_ring)

theorem snd_mul_zero (x y : R × R) (hx : x.2 = K.zero) (hy : y.2 = K.zero) : ((dring K).mul x y).2 = K.zero := by
  show K.add (K.mul x.1 y.2) (K.mul x.2 y.1) = K.zero
  rw [hx, hy]
  lib_ring

end DualL

#print axioms DualL.dext
#print axioms DualL.dring
#print axioms DualL.pi_sq
#print axioms DualL.psi_add
#print axioms DualL.psi_mul
#print axioms DualL.psi_one
#print axioms DualL.psi_pi
#print axioms DualL.snd_mul_zero

lang/Eig11.lean

lean_all/lang/Eig11.lean

/-
  lang/Eig11 — T50: **121b's Hecke theta series is a Hecke eigenform at every prime p ≠ 11**: for m prime to p and 11,
    eig11_split   p = ππ̄ split:  c(p^(j+2) m) = (W(π) + W(π̄)) c(p^(j+1) m) − W(π)W(π̄) c(p^j m)
    eig11_inert   p inert:       c(p^(j+2) m) = W(−p) c(p^j m)   (and c(p^(2k+1) m) = 0)
  (with Ram11: c(11n) = 0).  From HeckeMult11, HeckePrime11, Inert11 through HeckeEig.lift2.
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Inert11
import lang.HeckeEig

namespace Eig11

open FTAChain DetChain LibInt YMChain Frobenius Euc11 Prim11 HeckeMult11

variable (P : Nat)

theorem parity : ∀ j : Nat, (∃ k, j = 2 * k) ∨ (∃ k, j = 2 * k + 1)
  | 0 => Or.inl ⟨0, rfl⟩
  | j + 1 => by
    rcases parity j with ⟨k, hk⟩ | ⟨k, hk⟩
    · exact Or.inr ⟨k, by rw [hk]⟩
    · exact Or.inl ⟨k + 1, by rw [hk]; lib_nat⟩

/-- c(p^j m) = c(p^j) c(m) for m prime to p and 11. -/
theorem mul_pow (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring11.ring11 LibInt.intR).mul α β) = (Ring11.ring11 LibInt.intR).mul (W α) (W β))
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 11 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 11 * k) (j : Nat) :
    cw11 W ((P + 1 + 1) ^ j * (m + 1)) = (Ring11.ring11 LibInt.intR).mul (cw11 W ((P + 1 + 1) ^ j)) (cw11 W (m + 1)) := by
  obtain ⟨a, ha⟩ := HeckeEig.succ_pow (P + 1) j
  obtain ⟨s', t', h'⟩ := HeckeEig.bez_pow (P + 1 + 1) _ s t hst j
  rw [ha] at h'
  rw [ha]
  exact hecke_mult11 W hW a m s' t' h'
    (fun K e => HeckeEig.not_div (P + 1 + 1) 11 u v huv (Nat.le_of_ble_eq_true rfl) j K (by rw [ha]; exact e)) hm

/-- **Split p: the Hecke relation at every p^j m.** -/
theorem eig11_split (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring11.ring11 LibInt.intR).mul α β) = (Ring11.ring11 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring11.ring11 LibInt.intR).one = (Ring11.ring11 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim11 π)
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 11 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 11 * k) (j : Nat) :
    cw11 W ((P + 1 + 1) ^ (j + 2) * (m + 1)) = (Ring11.ring11 LibInt.intR).add
      ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).add (W π) (W (cj π))) (cw11 W ((P + 1 + 1) ^ (j + 1) * (m + 1))))
      ((Ring11.ring11 LibInt.intR).neg ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul (W π) (W (cj π))) (cw11 W ((P + 1 + 1) ^ j * (m + 1))))) :=
  HeckeEig.lift2 _ (cw11 W) (P + 1 + 1) (m + 1) (mul_pow P W hW u v huv m s t hst hm) _ _ (HeckePrime11.cw3_rec P W hW hW1 hp π hπ pπ) j

/-- Inert p: c(p^(j+2)) = W(−p) c(p^j). -/
theorem inert_rec (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring11.ring11 LibInt.intR).mul α β) = (Ring11.ring11 LibInt.intR).mul (W α) (W β))
    (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1)
    (hc : ∃ K, Int.ofNat (P + 1 + 1) = 11 * K + 2 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 6 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 7 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 8 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 10) (j : Nat) :
    cw11 W ((P + 1 + 1) ^ (j + 2)) = (Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul (Ring11.ring11 LibInt.intR).zero (cw11 W ((P + 1 + 1) ^ (j + 1))))
      ((Ring11.ring11 LibInt.intR).neg ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).neg (W (Inert11.nPi P))) (cw11 W ((P + 1 + 1) ^ j)))) := by
  rcases parity j with ⟨k, hk⟩ | ⟨k, hk⟩
  · rw [hk, show 2 * k + 2 = 2 * (k + 1) by lib_nat, Inert11.cw11_even P W hpr hc (k + 1), Inert11.cw11_even P W hpr hc k]
    show W ((Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) (Inert11.nPi P) k) (Inert11.nPi P)) = _
    rw [hW]; lib_ring
  · rw [hk, show 2 * k + 1 + 2 = 2 * (k + 1) + 1 by lib_nat, Inert11.cw11_odd P W hc (k + 1), Inert11.cw11_odd P W hc k]; lib_ring

/-- **Inert p: the Hecke relation at every p^j m.** -/
theorem eig11_inert (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring11.ring11 LibInt.intR).mul α β) = (Ring11.ring11 LibInt.intR).mul (W α) (W β))
    (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1)
    (hc : ∃ K, Int.ofNat (P + 1 + 1) = 11 * K + 2 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 6 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 7 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 8 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 10)
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 11 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 11 * k) (j : Nat) :
    cw11 W ((P + 1 + 1) ^ (j + 2) * (m + 1)) = (Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul (Ring11.ring11 LibInt.intR).zero (cw11 W ((P + 1 + 1) ^ (j + 1) * (m + 1))))
      ((Ring11.ring11 LibInt.intR).neg ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).neg (W (Inert11.nPi P))) (cw11 W ((P + 1 + 1) ^ j * (m + 1))))) :=
  HeckeEig.lift2 _ (cw11 W) (P + 1 + 1) (m + 1) (mul_pow P W hW u v huv m s t hst hm) _ _ (inert_rec P W hW hpr hc) j

end Eig11

#print axioms Eig11.parity
#print axioms Eig11.mul_pow
#print axioms Eig11.eig11_split
#print axioms Eig11.inert_rec
#print axioms Eig11.eig11_inert

lang/Eig163.lean

lean_all/lang/Eig163.lean

/-
  lang/Eig163 — T50: **26569a's Hecke theta series is a Hecke eigenform at every prime p ≠ 163**: for m prime to p and 163,
    eig163_split   p = ππ̄ split:  c(p^(j+2) m) = (W(π) + W(π̄)) c(p^(j+1) m) − W(π)W(π̄) c(p^j m)
    eig163_inert   p inert:       c(p^(j+2) m) = W(−p) c(p^j m)   (and c(p^(2k+1) m) = 0)
  (with Ram163: c(163n) = 0).  From HeckeMult163, HeckePrime163, Inert163 through HeckeEig.lift2.
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Inert163
import lang.HeckeEig

namespace Eig163

open FTAChain DetChain LibInt YMChain Frobenius Euc163 Prim163 HeckeMult163

variable (P : Nat)

theorem parity : ∀ j : Nat, (∃ k, j = 2 * k) ∨ (∃ k, j = 2 * k + 1)
  | 0 => Or.inl ⟨0, rfl⟩
  | j + 1 => by
    rcases parity j with ⟨k, hk⟩ | ⟨k, hk⟩
    · exact Or.inr ⟨k, by rw [hk]⟩
    · exact Or.inl ⟨k + 1, by rw [hk]; lib_nat⟩

/-- c(p^j m) = c(p^j) c(m) for m prime to p and 163. -/
theorem mul_pow (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring163.ring163 LibInt.intR).mul α β) = (Ring163.ring163 LibInt.intR).mul (W α) (W β))
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 163 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 163 * k) (j : Nat) :
    cw163 W ((P + 1 + 1) ^ j * (m + 1)) = (Ring163.ring163 LibInt.intR).mul (cw163 W ((P + 1 + 1) ^ j)) (cw163 W (m + 1)) := by
  obtain ⟨a, ha⟩ := HeckeEig.succ_pow (P + 1) j
  obtain ⟨s', t', h'⟩ := HeckeEig.bez_pow (P + 1 + 1) _ s t hst j
  rw [ha] at h'
  rw [ha]
  exact hecke_mult163 W hW a m s' t' h'
    (fun K e => HeckeEig.not_div (P + 1 + 1) 163 u v huv (Nat.le_of_ble_eq_true rfl) j K (by rw [ha]; exact e)) hm

/-- **Split p: the Hecke relation at every p^j m.** -/
theorem eig163_split (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring163.ring163 LibInt.intR).mul α β) = (Ring163.ring163 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring163.ring163 LibInt.intR).one = (Ring163.ring163 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim163 π)
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 163 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 163 * k) (j : Nat) :
    cw163 W ((P + 1 + 1) ^ (j + 2) * (m + 1)) = (Ring163.ring163 LibInt.intR).add
      ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).add (W π) (W (cj π))) (cw163 W ((P + 1 + 1) ^ (j + 1) * (m + 1))))
      ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul (W π) (W (cj π))) (cw163 W ((P + 1 + 1) ^ j * (m + 1))))) :=
  HeckeEig.lift2 _ (cw163 W) (P + 1 + 1) (m + 1) (mul_pow P W hW u v huv m s t hst hm) _ _ (HeckePrime163.cw3_rec P W hW hW1 hp π hπ pπ) j

/-- Inert p: c(p^(j+2)) = W(−p) c(p^j). -/
theorem inert_rec (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring163.ring163 LibInt.intR).mul α β) = (Ring163.ring163 LibInt.intR).mul (W α) (W β))
    (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1)
    (hc : QChar.chi 80 (LibRing.castZ (ZMod.ring (2 * 80 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) (j : Nat) :
    cw163 W ((P + 1 + 1) ^ (j + 2)) = (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (Ring163.ring163 LibInt.intR).zero (cw163 W ((P + 1 + 1) ^ (j + 1))))
      ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).neg (W (Inert163.nPi P))) (cw163 W ((P + 1 + 1) ^ j)))) := by
  rcases parity j with ⟨k, hk⟩ | ⟨k, hk⟩
  · rw [hk, show 2 * k + 2 = 2 * (k + 1) by lib_nat, Inert163.cw163_even P W hpr hc (k + 1), Inert163.cw163_even P W hpr hc k]
    show W ((Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) (Inert163.nPi P) k) (Inert163.nPi P)) = _
    rw [hW]; lib_ring
  · rw [hk, show 2 * k + 1 + 2 = 2 * (k + 1) + 1 by lib_nat, Inert163.cw163_odd P W hc (k + 1), Inert163.cw163_odd P W hc k]; lib_ring

/-- **Inert p: the Hecke relation at every p^j m.** -/
theorem eig163_inert (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring163.ring163 LibInt.intR).mul α β) = (Ring163.ring163 LibInt.intR).mul (W α) (W β))
    (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1)
    (hc : QChar.chi 80 (LibRing.castZ (ZMod.ring (2 * 80 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1)
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 163 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 163 * k) (j : Nat) :
    cw163 W ((P + 1 + 1) ^ (j + 2) * (m + 1)) = (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (Ring163.ring163 LibInt.intR).zero (cw163 W ((P + 1 + 1) ^ (j + 1) * (m + 1))))
      ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).neg (W (Inert163.nPi P))) (cw163 W ((P + 1 + 1) ^ j * (m + 1))))) :=
  HeckeEig.lift2 _ (cw163 W) (P + 1 + 1) (m + 1) (mul_pow P W hW u v huv m s t hst hm) _ _ (inert_rec P W hW hpr hc) j

end Eig163

#print axioms Eig163.parity
#print axioms Eig163.mul_pow
#print axioms Eig163.eig163_split
#print axioms Eig163.inert_rec
#print axioms Eig163.eig163_inert

lang/Eig19.lean

lean_all/lang/Eig19.lean

/-
  lang/Eig19 — T50: **361a's Hecke theta series is a Hecke eigenform at every prime p ≠ 19**: for m prime to p and 19,
    eig19_split   p = ππ̄ split:  c(p^(j+2) m) = (W(π) + W(π̄)) c(p^(j+1) m) − W(π)W(π̄) c(p^j m)
    eig19_inert   p inert:       c(p^(j+2) m) = W(−p) c(p^j m)   (and c(p^(2k+1) m) = 0)
  (with Ram19: c(19n) = 0).  From HeckeMult19, HeckePrime19, Inert19 through HeckeEig.lift2.
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Inert19
import lang.HeckeEig

namespace Eig19

open FTAChain DetChain LibInt YMChain Frobenius Euc19 Prim19 HeckeMult19

variable (P : Nat)

theorem parity : ∀ j : Nat, (∃ k, j = 2 * k) ∨ (∃ k, j = 2 * k + 1)
  | 0 => Or.inl ⟨0, rfl⟩
  | j + 1 => by
    rcases parity j with ⟨k, hk⟩ | ⟨k, hk⟩
    · exact Or.inr ⟨k, by rw [hk]⟩
    · exact Or.inl ⟨k + 1, by rw [hk]; lib_nat⟩

/-- c(p^j m) = c(p^j) c(m) for m prime to p and 19. -/
theorem mul_pow (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring19.ring19 LibInt.intR).mul α β) = (Ring19.ring19 LibInt.intR).mul (W α) (W β))
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 19 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 19 * k) (j : Nat) :
    cw19 W ((P + 1 + 1) ^ j * (m + 1)) = (Ring19.ring19 LibInt.intR).mul (cw19 W ((P + 1 + 1) ^ j)) (cw19 W (m + 1)) := by
  obtain ⟨a, ha⟩ := HeckeEig.succ_pow (P + 1) j
  obtain ⟨s', t', h'⟩ := HeckeEig.bez_pow (P + 1 + 1) _ s t hst j
  rw [ha] at h'
  rw [ha]
  exact hecke_mult19 W hW a m s' t' h'
    (fun K e => HeckeEig.not_div (P + 1 + 1) 19 u v huv (Nat.le_of_ble_eq_true rfl) j K (by rw [ha]; exact e)) hm

/-- **Split p: the Hecke relation at every p^j m.** -/
theorem eig19_split (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring19.ring19 LibInt.intR).mul α β) = (Ring19.ring19 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring19.ring19 LibInt.intR).one = (Ring19.ring19 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim19 π)
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 19 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 19 * k) (j : Nat) :
    cw19 W ((P + 1 + 1) ^ (j + 2) * (m + 1)) = (Ring19.ring19 LibInt.intR).add
      ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).add (W π) (W (cj π))) (cw19 W ((P + 1 + 1) ^ (j + 1) * (m + 1))))
      ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul (W π) (W (cj π))) (cw19 W ((P + 1 + 1) ^ j * (m + 1))))) :=
  HeckeEig.lift2 _ (cw19 W) (P + 1 + 1) (m + 1) (mul_pow P W hW u v huv m s t hst hm) _ _ (HeckePrime19.cw3_rec P W hW hW1 hp π hπ pπ) j

/-- Inert p: c(p^(j+2)) = W(−p) c(p^j). -/
theorem inert_rec (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring19.ring19 LibInt.intR).mul α β) = (Ring19.ring19 LibInt.intR).mul (W α) (W β))
    (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1)
    (hc : ∃ K, Int.ofNat (P + 1 + 1) = 19 * K + 2 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 3 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 8 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 10 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 12 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 13 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 14 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 15 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 18) (j : Nat) :
    cw19 W ((P + 1 + 1) ^ (j + 2)) = (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul (Ring19.ring19 LibInt.intR).zero (cw19 W ((P + 1 + 1) ^ (j + 1))))
      ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).neg (W (Inert19.nPi P))) (cw19 W ((P + 1 + 1) ^ j)))) := by
  rcases parity j with ⟨k, hk⟩ | ⟨k, hk⟩
  · rw [hk, show 2 * k + 2 = 2 * (k + 1) by lib_nat, Inert19.cw19_even P W hpr hc (k + 1), Inert19.cw19_even P W hpr hc k]
    show W ((Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) (Inert19.nPi P) k) (Inert19.nPi P)) = _
    rw [hW]; lib_ring
  · rw [hk, show 2 * k + 1 + 2 = 2 * (k + 1) + 1 by lib_nat, Inert19.cw19_odd P W hc (k + 1), Inert19.cw19_odd P W hc k]; lib_ring

/-- **Inert p: the Hecke relation at every p^j m.** -/
theorem eig19_inert (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring19.ring19 LibInt.intR).mul α β) = (Ring19.ring19 LibInt.intR).mul (W α) (W β))
    (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1)
    (hc : ∃ K, Int.ofNat (P + 1 + 1) = 19 * K + 2 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 3 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 8 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 10 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 12 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 13 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 14 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 15 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 18)
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 19 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 19 * k) (j : Nat) :
    cw19 W ((P + 1 + 1) ^ (j + 2) * (m + 1)) = (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul (Ring19.ring19 LibInt.intR).zero (cw19 W ((P + 1 + 1) ^ (j + 1) * (m + 1))))
      ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).neg (W (Inert19.nPi P))) (cw19 W ((P + 1 + 1) ^ j * (m + 1))))) :=
  HeckeEig.lift2 _ (cw19 W) (P + 1 + 1) (m + 1) (mul_pow P W hW u v huv m s t hst hm) _ _ (inert_rec P W hW hpr hc) j

end Eig19

#print axioms Eig19.parity
#print axioms Eig19.mul_pow
#print axioms Eig19.eig19_split
#print axioms Eig19.inert_rec
#print axioms Eig19.eig19_inert

lang/Eig3.lean

lean_all/lang/Eig3.lean

/-
  lang/Eig3 — T50: **27a/36a (ℤ[ω])'s Hecke theta series is a Hecke eigenform at every prime p ≠ 3**: for m prime to p and 3,
    eig3_split   p = ππ̄ split:  c(p^(j+2) m) = (W(π) + W(π̄)) c(p^(j+1) m) − W(π)W(π̄) c(p^j m)
    eig3_inert   p inert:       c(p^(j+2) m) = W(−p) c(p^j m)   (and c(p^(2k+1) m) = 0)
  (inert p = 3t + 2).  From HeckeMult3, HeckePrime3, EisInert3 through HeckeEig.lift2.
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.EisInert3
import lang.HeckePrime3
import lang.HeckeEig

namespace Eig3

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius EisEuclid EisPrim EisUnique3 EisPrime3 HeckeMult3

variable (P : Nat)

theorem parity : ∀ j : Nat, (∃ k, j = 2 * k) ∨ (∃ k, j = 2 * k + 1)
  | 0 => Or.inl ⟨0, rfl⟩
  | j + 1 => by
    rcases parity j with ⟨k, hk⟩ | ⟨k, hk⟩
    · exact Or.inr ⟨k, by rw [hk]⟩
    · exact Or.inl ⟨k + 1, by rw [hk]; lib_nat⟩

/-- p^j prime to 3 from u p + 3 v = 1. -/
theorem cond_of (p : Nat) (u v : Int) (huv : u * Int.ofNat p + v * 3 = 1) (j a : Nat) (ha : p ^ j = a + 1) : C3 (Int.ofNat (a + 1)) := by
  obtain ⟨q, hq | hq | hq⟩ := EisPrim.res3 (Int.ofNat (a + 1))
  · exact absurd (by rw [ha]; exact hq) (HeckeEig.not_div p 3 u v huv (Nat.le_of_ble_eq_true rfl) j q)
  · exact ⟨q, Or.inl hq⟩
  · exact ⟨q + -1, Or.inr (by rw [hq]; lib_int)⟩

/-- c(p^j m) = c(p^j) c(m) for m prime to p and 3. -/
theorem mul_pow (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Cyclo3.cyc3 LibInt.intR).mul α β) = (Cyclo3.cyc3 LibInt.intR).mul (W α) (W β))
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 3 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : C3 (Int.ofNat (m + 1))) (j : Nat) :
    cw3 W ((P + 1 + 1) ^ j * (m + 1)) = (Cyclo3.cyc3 LibInt.intR).mul (cw3 W ((P + 1 + 1) ^ j)) (cw3 W (m + 1)) := by
  obtain ⟨a, ha⟩ := HeckeEig.succ_pow (P + 1) j
  obtain ⟨s', t', h'⟩ := HeckeEig.bez_pow (P + 1 + 1) _ s t hst j
  rw [ha] at h'
  rw [ha]
  exact hecke_mult3 W hW a m s' t' h' (cond_of (P + 1 + 1) u v huv j a ha) hm

/-- **Split p: the Hecke relation at every p^j m.** -/
theorem eig3_split (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Cyclo3.cyc3 LibInt.intR).mul α β) = (Cyclo3.cyc3 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Cyclo3.cyc3 LibInt.intR).one = (Cyclo3.cyc3 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim3 π)
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 3 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : C3 (Int.ofNat (m + 1))) (j : Nat) :
    cw3 W ((P + 1 + 1) ^ (j + 2) * (m + 1)) = (Cyclo3.cyc3 LibInt.intR).add
      ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).add (W π) (W (cj π))) (cw3 W ((P + 1 + 1) ^ (j + 1) * (m + 1))))
      ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (W π) (W (cj π))) (cw3 W ((P + 1 + 1) ^ j * (m + 1))))) :=
  HeckeEig.lift2 _ (cw3 W) (P + 1 + 1) (m + 1) (mul_pow P W hW u v huv m s t hst hm) _ _ (HeckePrime3.cw3_rec P W hW hW1 hp π hπ pπ) j

/-- Inert p: c(p^(j+2)) = W(−p) c(p^j). -/
theorem inert_rec (t : Nat) (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Cyclo3.cyc3 LibInt.intR).mul α β) = (Cyclo3.cyc3 LibInt.intR).mul (W α) (W β))
    (hpr : ∀ d, Dv d (3 * t + 1 + 1) → d = 1 ∨ d = 3 * t + 1 + 1) (j : Nat) :
    cw3 W ((3 * t + 1 + 1) ^ (j + 2)) = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).zero (cw3 W ((3 * t + 1 + 1) ^ (j + 1))))
      ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).neg (W (EisInert3.nPi t))) (cw3 W ((3 * t + 1 + 1) ^ j)))) := by
  rcases parity j with ⟨k, hk⟩ | ⟨k, hk⟩
  · rw [hk, show 2 * k + 2 = 2 * (k + 1) by lib_nat, EisInert3.cw3_even t W hpr (k + 1), EisInert3.cw3_even t W hpr k]
    show W ((Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) (EisInert3.nPi t) k) (EisInert3.nPi t)) = _
    rw [hW]; lib_ring
  · rw [hk, show 2 * k + 1 + 2 = 2 * (k + 1) + 1 by lib_nat, EisInert3.cw3_odd t W (k + 1), EisInert3.cw3_odd t W k]; lib_ring

/-- **Inert p: the Hecke relation at every p^j m.** -/
theorem eig3_inert (t : Nat) (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Cyclo3.cyc3 LibInt.intR).mul α β) = (Cyclo3.cyc3 LibInt.intR).mul (W α) (W β))
    (hpr : ∀ d, Dv d (3 * t + 1 + 1) → d = 1 ∨ d = 3 * t + 1 + 1)
    (u v : Int) (huv : u * Int.ofNat (3 * t + 1 + 1) + v * 3 = 1) (m : Nat) (s t' : Int) (hst : s * Int.ofNat (3 * t + 1 + 1) + t' * Int.ofNat (m + 1) = 1)
    (hm : C3 (Int.ofNat (m + 1))) (j : Nat) :
    cw3 W ((3 * t + 1 + 1) ^ (j + 2) * (m + 1)) = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).zero (cw3 W ((3 * t + 1 + 1) ^ (j + 1) * (m + 1))))
      ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).neg (W (EisInert3.nPi t))) (cw3 W ((3 * t + 1 + 1) ^ j * (m + 1))))) :=
  HeckeEig.lift2 _ (cw3 W) (3 * t + 1 + 1) (m + 1) (mul_pow (3 * t) W hW u v huv m s t' hst hm) _ _ (inert_rec t W hW hpr) j

end Eig3

#print axioms Eig3.parity
#print axioms Eig3.cond_of
#print axioms Eig3.mul_pow
#print axioms Eig3.eig3_split
#print axioms Eig3.inert_rec
#print axioms Eig3.eig3_inert

lang/Eig43.lean

lean_all/lang/Eig43.lean

/-
  lang/Eig43 — T50: **1849a's Hecke theta series is a Hecke eigenform at every prime p ≠ 43**: for m prime to p and 43,
    eig43_split   p = ππ̄ split:  c(p^(j+2) m) = (W(π) + W(π̄)) c(p^(j+1) m) − W(π)W(π̄) c(p^j m)
    eig43_inert   p inert:       c(p^(j+2) m) = W(−p) c(p^j m)   (and c(p^(2k+1) m) = 0)
  (with Ram43: c(43n) = 0).  From HeckeMult43, HeckePrime43, Inert43 through HeckeEig.lift2.
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Inert43
import lang.HeckeEig

namespace Eig43

open FTAChain DetChain LibInt YMChain Frobenius Euc43 Prim43 HeckeMult43

variable (P : Nat)

theorem parity : ∀ j : Nat, (∃ k, j = 2 * k) ∨ (∃ k, j = 2 * k + 1)
  | 0 => Or.inl ⟨0, rfl⟩
  | j + 1 => by
    rcases parity j with ⟨k, hk⟩ | ⟨k, hk⟩
    · exact Or.inr ⟨k, by rw [hk]⟩
    · exact Or.inl ⟨k + 1, by rw [hk]; lib_nat⟩

/-- c(p^j m) = c(p^j) c(m) for m prime to p and 43. -/
theorem mul_pow (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring43.ring43 LibInt.intR).mul α β) = (Ring43.ring43 LibInt.intR).mul (W α) (W β))
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 43 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 43 * k) (j : Nat) :
    cw43 W ((P + 1 + 1) ^ j * (m + 1)) = (Ring43.ring43 LibInt.intR).mul (cw43 W ((P + 1 + 1) ^ j)) (cw43 W (m + 1)) := by
  obtain ⟨a, ha⟩ := HeckeEig.succ_pow (P + 1) j
  obtain ⟨s', t', h'⟩ := HeckeEig.bez_pow (P + 1 + 1) _ s t hst j
  rw [ha] at h'
  rw [ha]
  exact hecke_mult43 W hW a m s' t' h'
    (fun K e => HeckeEig.not_div (P + 1 + 1) 43 u v huv (Nat.le_of_ble_eq_true rfl) j K (by rw [ha]; exact e)) hm

/-- **Split p: the Hecke relation at every p^j m.** -/
theorem eig43_split (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring43.ring43 LibInt.intR).mul α β) = (Ring43.ring43 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring43.ring43 LibInt.intR).one = (Ring43.ring43 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim43 π)
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 43 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 43 * k) (j : Nat) :
    cw43 W ((P + 1 + 1) ^ (j + 2) * (m + 1)) = (Ring43.ring43 LibInt.intR).add
      ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).add (W π) (W (cj π))) (cw43 W ((P + 1 + 1) ^ (j + 1) * (m + 1))))
      ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul (W π) (W (cj π))) (cw43 W ((P + 1 + 1) ^ j * (m + 1))))) :=
  HeckeEig.lift2 _ (cw43 W) (P + 1 + 1) (m + 1) (mul_pow P W hW u v huv m s t hst hm) _ _ (HeckePrime43.cw3_rec P W hW hW1 hp π hπ pπ) j

/-- Inert p: c(p^(j+2)) = W(−p) c(p^j). -/
theorem inert_rec (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring43.ring43 LibInt.intR).mul α β) = (Ring43.ring43 LibInt.intR).mul (W α) (W β))
    (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1)
    (hc : QChar.chi 20 (LibRing.castZ (ZMod.ring (2 * 20 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) (j : Nat) :
    cw43 W ((P + 1 + 1) ^ (j + 2)) = (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul (Ring43.ring43 LibInt.intR).zero (cw43 W ((P + 1 + 1) ^ (j + 1))))
      ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).neg (W (Inert43.nPi P))) (cw43 W ((P + 1 + 1) ^ j)))) := by
  rcases parity j with ⟨k, hk⟩ | ⟨k, hk⟩
  · rw [hk, show 2 * k + 2 = 2 * (k + 1) by lib_nat, Inert43.cw43_even P W hpr hc (k + 1), Inert43.cw43_even P W hpr hc k]
    show W ((Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) (Inert43.nPi P) k) (Inert43.nPi P)) = _
    rw [hW]; lib_ring
  · rw [hk, show 2 * k + 1 + 2 = 2 * (k + 1) + 1 by lib_nat, Inert43.cw43_odd P W hc (k + 1), Inert43.cw43_odd P W hc k]; lib_ring

/-- **Inert p: the Hecke relation at every p^j m.** -/
theorem eig43_inert (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring43.ring43 LibInt.intR).mul α β) = (Ring43.ring43 LibInt.intR).mul (W α) (W β))
    (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1)
    (hc : QChar.chi 20 (LibRing.castZ (ZMod.ring (2 * 20 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1)
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 43 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 43 * k) (j : Nat) :
    cw43 W ((P + 1 + 1) ^ (j + 2) * (m + 1)) = (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul (Ring43.ring43 LibInt.intR).zero (cw43 W ((P + 1 + 1) ^ (j + 1) * (m + 1))))
      ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).neg (W (Inert43.nPi P))) (cw43 W ((P + 1 + 1) ^ j * (m + 1))))) :=
  HeckeEig.lift2 _ (cw43 W) (P + 1 + 1) (m + 1) (mul_pow P W hW u v huv m s t hst hm) _ _ (inert_rec P W hW hpr hc) j

end Eig43

#print axioms Eig43.parity
#print axioms Eig43.mul_pow
#print axioms Eig43.eig43_split
#print axioms Eig43.inert_rec
#print axioms Eig43.eig43_inert

lang/Eig67.lean

lean_all/lang/Eig67.lean

/-
  lang/Eig67 — T50: **4489a's Hecke theta series is a Hecke eigenform at every prime p ≠ 67**: for m prime to p and 67,
    eig67_split   p = ππ̄ split:  c(p^(j+2) m) = (W(π) + W(π̄)) c(p^(j+1) m) − W(π)W(π̄) c(p^j m)
    eig67_inert   p inert:       c(p^(j+2) m) = W(−p) c(p^j m)   (and c(p^(2k+1) m) = 0)
  (with Ram67: c(67n) = 0).  From HeckeMult67, HeckePrime67, Inert67 through HeckeEig.lift2.
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Inert67
import lang.HeckeEig

namespace Eig67

open FTAChain DetChain LibInt YMChain Frobenius Euc67 Prim67 HeckeMult67

variable (P : Nat)

theorem parity : ∀ j : Nat, (∃ k, j = 2 * k) ∨ (∃ k, j = 2 * k + 1)
  | 0 => Or.inl ⟨0, rfl⟩
  | j + 1 => by
    rcases parity j with ⟨k, hk⟩ | ⟨k, hk⟩
    · exact Or.inr ⟨k, by rw [hk]⟩
    · exact Or.inl ⟨k + 1, by rw [hk]; lib_nat⟩

/-- c(p^j m) = c(p^j) c(m) for m prime to p and 67. -/
theorem mul_pow (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring67.ring67 LibInt.intR).mul α β) = (Ring67.ring67 LibInt.intR).mul (W α) (W β))
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 67 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 67 * k) (j : Nat) :
    cw67 W ((P + 1 + 1) ^ j * (m + 1)) = (Ring67.ring67 LibInt.intR).mul (cw67 W ((P + 1 + 1) ^ j)) (cw67 W (m + 1)) := by
  obtain ⟨a, ha⟩ := HeckeEig.succ_pow (P + 1) j
  obtain ⟨s', t', h'⟩ := HeckeEig.bez_pow (P + 1 + 1) _ s t hst j
  rw [ha] at h'
  rw [ha]
  exact hecke_mult67 W hW a m s' t' h'
    (fun K e => HeckeEig.not_div (P + 1 + 1) 67 u v huv (Nat.le_of_ble_eq_true rfl) j K (by rw [ha]; exact e)) hm

/-- **Split p: the Hecke relation at every p^j m.** -/
theorem eig67_split (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring67.ring67 LibInt.intR).mul α β) = (Ring67.ring67 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring67.ring67 LibInt.intR).one = (Ring67.ring67 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim67 π)
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 67 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 67 * k) (j : Nat) :
    cw67 W ((P + 1 + 1) ^ (j + 2) * (m + 1)) = (Ring67.ring67 LibInt.intR).add
      ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).add (W π) (W (cj π))) (cw67 W ((P + 1 + 1) ^ (j + 1) * (m + 1))))
      ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul (W π) (W (cj π))) (cw67 W ((P + 1 + 1) ^ j * (m + 1))))) :=
  HeckeEig.lift2 _ (cw67 W) (P + 1 + 1) (m + 1) (mul_pow P W hW u v huv m s t hst hm) _ _ (HeckePrime67.cw3_rec P W hW hW1 hp π hπ pπ) j

/-- Inert p: c(p^(j+2)) = W(−p) c(p^j). -/
theorem inert_rec (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring67.ring67 LibInt.intR).mul α β) = (Ring67.ring67 LibInt.intR).mul (W α) (W β))
    (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1)
    (hc : QChar.chi 32 (LibRing.castZ (ZMod.ring (2 * 32 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) (j : Nat) :
    cw67 W ((P + 1 + 1) ^ (j + 2)) = (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul (Ring67.ring67 LibInt.intR).zero (cw67 W ((P + 1 + 1) ^ (j + 1))))
      ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).neg (W (Inert67.nPi P))) (cw67 W ((P + 1 + 1) ^ j)))) := by
  rcases parity j with ⟨k, hk⟩ | ⟨k, hk⟩
  · rw [hk, show 2 * k + 2 = 2 * (k + 1) by lib_nat, Inert67.cw67_even P W hpr hc (k + 1), Inert67.cw67_even P W hpr hc k]
    show W ((Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) (Inert67.nPi P) k) (Inert67.nPi P)) = _
    rw [hW]; lib_ring
  · rw [hk, show 2 * k + 1 + 2 = 2 * (k + 1) + 1 by lib_nat, Inert67.cw67_odd P W hc (k + 1), Inert67.cw67_odd P W hc k]; lib_ring

/-- **Inert p: the Hecke relation at every p^j m.** -/
theorem eig67_inert (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring67.ring67 LibInt.intR).mul α β) = (Ring67.ring67 LibInt.intR).mul (W α) (W β))
    (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1)
    (hc : QChar.chi 32 (LibRing.castZ (ZMod.ring (2 * 32 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1)
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 67 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 67 * k) (j : Nat) :
    cw67 W ((P + 1 + 1) ^ (j + 2) * (m + 1)) = (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul (Ring67.ring67 LibInt.intR).zero (cw67 W ((P + 1 + 1) ^ (j + 1) * (m + 1))))
      ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).neg (W (Inert67.nPi P))) (cw67 W ((P + 1 + 1) ^ j * (m + 1))))) :=
  HeckeEig.lift2 _ (cw67 W) (P + 1 + 1) (m + 1) (mul_pow P W hW u v huv m s t hst hm) _ _ (inert_rec P W hW hpr hc) j

end Eig67

#print axioms Eig67.parity
#print axioms Eig67.mul_pow
#print axioms Eig67.eig67_split
#print axioms Eig67.inert_rec
#print axioms Eig67.eig67_inert

lang/Eig7.lean

lean_all/lang/Eig7.lean

/-
  lang/Eig7 — T50: **49a's Hecke theta series is a Hecke eigenform at every prime p ≠ 7**: for m prime to p and 7,
    eig7_split   p = ππ̄ split:  c(p^(j+2) m) = (W(π) + W(π̄)) c(p^(j+1) m) − W(π)W(π̄) c(p^j m)
    eig7_inert   p inert:       c(p^(j+2) m) = W(−p) c(p^j m)   (and c(p^(2k+1) m) = 0)
  (with Ram7: c(7n) = 0).  From HeckeMult7, HeckePrime7, Inert7 through HeckeEig.lift2.
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Inert7
import lang.HeckeEig

namespace Eig7

open FTAChain DetChain LibInt YMChain Frobenius Euc7 Prim7 HeckeMult7

variable (P : Nat)

theorem parity : ∀ j : Nat, (∃ k, j = 2 * k) ∨ (∃ k, j = 2 * k + 1)
  | 0 => Or.inl ⟨0, rfl⟩
  | j + 1 => by
    rcases parity j with ⟨k, hk⟩ | ⟨k, hk⟩
    · exact Or.inr ⟨k, by rw [hk]⟩
    · exact Or.inl ⟨k + 1, by rw [hk]; lib_nat⟩

/-- c(p^j m) = c(p^j) c(m) for m prime to p and 7. -/
theorem mul_pow (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring7.ring7 LibInt.intR).mul α β) = (Ring7.ring7 LibInt.intR).mul (W α) (W β))
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 7 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 7 * k) (j : Nat) :
    cw7 W ((P + 1 + 1) ^ j * (m + 1)) = (Ring7.ring7 LibInt.intR).mul (cw7 W ((P + 1 + 1) ^ j)) (cw7 W (m + 1)) := by
  obtain ⟨a, ha⟩ := HeckeEig.succ_pow (P + 1) j
  obtain ⟨s', t', h'⟩ := HeckeEig.bez_pow (P + 1 + 1) _ s t hst j
  rw [ha] at h'
  rw [ha]
  exact hecke_mult7 W hW a m s' t' h'
    (fun K e => HeckeEig.not_div (P + 1 + 1) 7 u v huv (Nat.le_of_ble_eq_true rfl) j K (by rw [ha]; exact e)) hm

/-- **Split p: the Hecke relation at every p^j m.** -/
theorem eig7_split (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring7.ring7 LibInt.intR).mul α β) = (Ring7.ring7 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring7.ring7 LibInt.intR).one = (Ring7.ring7 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim7 π)
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 7 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 7 * k) (j : Nat) :
    cw7 W ((P + 1 + 1) ^ (j + 2) * (m + 1)) = (Ring7.ring7 LibInt.intR).add
      ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).add (W π) (W (cj π))) (cw7 W ((P + 1 + 1) ^ (j + 1) * (m + 1))))
      ((Ring7.ring7 LibInt.intR).neg ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul (W π) (W (cj π))) (cw7 W ((P + 1 + 1) ^ j * (m + 1))))) :=
  HeckeEig.lift2 _ (cw7 W) (P + 1 + 1) (m + 1) (mul_pow P W hW u v huv m s t hst hm) _ _ (HeckePrime7.cw3_rec P W hW hW1 hp π hπ pπ) j

/-- Inert p: c(p^(j+2)) = W(−p) c(p^j). -/
theorem inert_rec (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring7.ring7 LibInt.intR).mul α β) = (Ring7.ring7 LibInt.intR).mul (W α) (W β))
    (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1)
    (hc : ∃ K, Int.ofNat (P + 1 + 1) = 7 * K + 3 ∨ Int.ofNat (P + 1 + 1) = 7 * K + 5 ∨ Int.ofNat (P + 1 + 1) = 7 * K + 6) (j : Nat) :
    cw7 W ((P + 1 + 1) ^ (j + 2)) = (Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul (Ring7.ring7 LibInt.intR).zero (cw7 W ((P + 1 + 1) ^ (j + 1))))
      ((Ring7.ring7 LibInt.intR).neg ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).neg (W (Inert7.nPi P))) (cw7 W ((P + 1 + 1) ^ j)))) := by
  rcases parity j with ⟨k, hk⟩ | ⟨k, hk⟩
  · rw [hk, show 2 * k + 2 = 2 * (k + 1) by lib_nat, Inert7.cw7_even P W hpr hc (k + 1), Inert7.cw7_even P W hpr hc k]
    show W ((Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) (Inert7.nPi P) k) (Inert7.nPi P)) = _
    rw [hW]; lib_ring
  · rw [hk, show 2 * k + 1 + 2 = 2 * (k + 1) + 1 by lib_nat, Inert7.cw7_odd P W hc (k + 1), Inert7.cw7_odd P W hc k]; lib_ring

/-- **Inert p: the Hecke relation at every p^j m.** -/
theorem eig7_inert (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring7.ring7 LibInt.intR).mul α β) = (Ring7.ring7 LibInt.intR).mul (W α) (W β))
    (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1)
    (hc : ∃ K, Int.ofNat (P + 1 + 1) = 7 * K + 3 ∨ Int.ofNat (P + 1 + 1) = 7 * K + 5 ∨ Int.ofNat (P + 1 + 1) = 7 * K + 6)
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 7 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 7 * k) (j : Nat) :
    cw7 W ((P + 1 + 1) ^ (j + 2) * (m + 1)) = (Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul (Ring7.ring7 LibInt.intR).zero (cw7 W ((P + 1 + 1) ^ (j + 1) * (m + 1))))
      ((Ring7.ring7 LibInt.intR).neg ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).neg (W (Inert7.nPi P))) (cw7 W ((P + 1 + 1) ^ j * (m + 1))))) :=
  HeckeEig.lift2 _ (cw7 W) (P + 1 + 1) (m + 1) (mul_pow P W hW u v huv m s t hst hm) _ _ (inert_rec P W hW hpr hc) j

end Eig7

#print axioms Eig7.parity
#print axioms Eig7.mul_pow
#print axioms Eig7.eig7_split
#print axioms Eig7.inert_rec
#print axioms Eig7.eig7_inert

lang/Eig8.lean

lean_all/lang/Eig8.lean

/-
  lang/Eig8 — T50: **256a (ℚ(√−2))'s Hecke theta series is a Hecke eigenform at every odd prime p**: for m prime to p and 2,
    eig8_split   p = ππ̄ split:  c(p^(j+2) m) = (W(π) + W(π̄)) c(p^(j+1) m) − W(π)W(π̄) c(p^j m)
    eig8_inert   p inert:       c(p^(j+2) m) = W(−p) c(p^j m)   (and c(p^(2k+1) m) = 0)
  (inert p ≡ 5, 7 mod 8).  From HeckeMult8, HeckePrime8, Sq2Inert8 through HeckeEig.lift2.
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Sq2Inert8
import lang.HeckeEig

namespace Eig8

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius Sq2Euclid Sq2Prim8 Sq2Unique8 Sq2Prime8 HeckeMult8

variable (P : Nat)

theorem parity : ∀ j : Nat, (∃ k, j = 2 * k) ∨ (∃ k, j = 2 * k + 1)
  | 0 => Or.inl ⟨0, rfl⟩
  | j + 1 => by
    rcases parity j with ⟨k, hk⟩ | ⟨k, hk⟩
    · exact Or.inr ⟨k, by rw [hk]⟩
    · exact Or.inl ⟨k + 1, by rw [hk]; lib_nat⟩

/-- p^j odd from u p + 2 v = 1. -/
theorem cond_of (p : Nat) (u v : Int) (huv : u * Int.ofNat p + v * 2 = 1) (j a : Nat) (ha : p ^ j = a + 1) : ∃ k, Int.ofNat (a + 1) = 2 * k + 1 := by
  rcases GaussInt.int_even_odd (Int.ofNat (a + 1)) with ⟨q, hq⟩ | ⟨q, hq⟩
  · exact absurd (by rw [ha]; exact hq) (HeckeEig.not_div p 2 u v huv (Nat.le_of_ble_eq_true rfl) j q)
  · exact ⟨q, hq⟩

/-- c(p^j m) = c(p^j) c(m) for m prime to p and 2. -/
theorem mul_pow (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Quad.quad LibInt.intR (-2)).mul α β) = (Quad.quad LibInt.intR (-2)).mul (W α) (W β))
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 2 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∃ k, Int.ofNat (m + 1) = 2 * k + 1) (j : Nat) :
    cw8 W ((P + 1 + 1) ^ j * (m + 1)) = (Quad.quad LibInt.intR (-2)).mul (cw8 W ((P + 1 + 1) ^ j)) (cw8 W (m + 1)) := by
  obtain ⟨a, ha⟩ := HeckeEig.succ_pow (P + 1) j
  obtain ⟨s', t', h'⟩ := HeckeEig.bez_pow (P + 1 + 1) _ s t hst j
  rw [ha] at h'
  rw [ha]
  exact hecke_mult8 W hW a m s' t' h' (cond_of (P + 1 + 1) u v huv j a ha) hm

/-- **Split p: the Hecke relation at every p^j m.** -/
theorem eig8_split (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Quad.quad LibInt.intR (-2)).mul α β) = (Quad.quad LibInt.intR (-2)).mul (W α) (W β))
    (hW1 : W (Quad.quad LibInt.intR (-2)).one = (Quad.quad LibInt.intR (-2)).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim8 π)
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 2 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∃ k, Int.ofNat (m + 1) = 2 * k + 1) (j : Nat) :
    cw8 W ((P + 1 + 1) ^ (j + 2) * (m + 1)) = (Quad.quad LibInt.intR (-2)).add
      ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).add (W π) (W (cj π))) (cw8 W ((P + 1 + 1) ^ (j + 1) * (m + 1))))
      ((Quad.quad LibInt.intR (-2)).neg ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (W π) (W (cj π))) (cw8 W ((P + 1 + 1) ^ j * (m + 1))))) :=
  HeckeEig.lift2 _ (cw8 W) (P + 1 + 1) (m + 1) (mul_pow P W hW u v huv m s t hst hm) _ _ (HeckePrime8.cw3_rec P W hW hW1 hp π hπ pπ) j

/-- Inert p: c(p^(j+2)) = W(−p) c(p^j). -/
theorem inert_rec (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Quad.quad LibInt.intR (-2)).mul α β) = (Quad.quad LibInt.intR (-2)).mul (W α) (W β))
    (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1)
    (hc : ∃ K, Int.ofNat (P + 1 + 1) = 8 * K + 5 ∨ Int.ofNat (P + 1 + 1) = 8 * K + 7) (j : Nat) :
    cw8 W ((P + 1 + 1) ^ (j + 2)) = (Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul (Quad.quad LibInt.intR (-2)).zero (cw8 W ((P + 1 + 1) ^ (j + 1))))
      ((Quad.quad LibInt.intR (-2)).neg ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).neg (W (Sq2Inert8.nPi P))) (cw8 W ((P + 1 + 1) ^ j)))) := by
  rcases parity j with ⟨k, hk⟩ | ⟨k, hk⟩
  · rw [hk, show 2 * k + 2 = 2 * (k + 1) by lib_nat, Sq2Inert8.cw8_even P W hpr hc (k + 1), Sq2Inert8.cw8_even P W hpr hc k]
    show W ((Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) (Sq2Inert8.nPi P) k) (Sq2Inert8.nPi P)) = _
    rw [hW]; lib_ring
  · rw [hk, show 2 * k + 1 + 2 = 2 * (k + 1) + 1 by lib_nat, Sq2Inert8.cw8_odd P W hc (k + 1), Sq2Inert8.cw8_odd P W hc k]; lib_ring

/-- **Inert p: the Hecke relation at every p^j m.** -/
theorem eig8_inert (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Quad.quad LibInt.intR (-2)).mul α β) = (Quad.quad LibInt.intR (-2)).mul (W α) (W β))
    (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1)
    (hc : ∃ K, Int.ofNat (P + 1 + 1) = 8 * K + 5 ∨ Int.ofNat (P + 1 + 1) = 8 * K + 7)
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 2 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∃ k, Int.ofNat (m + 1) = 2 * k + 1) (j : Nat) :
    cw8 W ((P + 1 + 1) ^ (j + 2) * (m + 1)) = (Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul (Quad.quad LibInt.intR (-2)).zero (cw8 W ((P + 1 + 1) ^ (j + 1) * (m + 1))))
      ((Quad.quad LibInt.intR (-2)).neg ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).neg (W (Sq2Inert8.nPi P))) (cw8 W ((P + 1 + 1) ^ j * (m + 1))))) :=
  HeckeEig.lift2 _ (cw8 W) (P + 1 + 1) (m + 1) (mul_pow P W hW u v huv m s t hst hm) _ _ (inert_rec P W hW hpr hc) j

end Eig8

#print axioms Eig8.parity
#print axioms Eig8.cond_of
#print axioms Eig8.mul_pow
#print axioms Eig8.eig8_split
#print axioms Eig8.inert_rec
#print axioms Eig8.eig8_inert

lang/EigG.lean

lean_all/lang/EigG.lean

/-
  lang/EigG — T50: **32a (ℤ[i])'s Hecke theta series is a Hecke eigenform at every odd prime**: for odd m prime to p,
    eigG_split   p = a² + b²:   c(p^(j+2) m) = (W(π) + W(π̄)) c(p^(j+1) m) − W(π)W(π̄) c(p^j m)
    eigG_inert   p = 4r + 3:    c(p^(j+2) m) = W(−p) c(p^j m)   (and c(p^(2k+1) m) = 0)
  From HeckeMultW, HeckePrimeW, HeckeInertW through HeckeEig.lift2.
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.HeckeInertW
import lang.HeckeEig

namespace EigG

open FTAChain DetChain LibInt GaussEuclid GaussInt GaussFactor GaussUnique HeckeMult YMChain FiniteRecChain Frobenius LibGcd

theorem parity : ∀ j : Nat, (∃ k, j = 2 * k) ∨ (∃ k, j = 2 * k + 1)
  | 0 => Or.inl ⟨0, rfl⟩
  | j + 1 => by
    rcases parity j with ⟨k, hk⟩ | ⟨k, hk⟩
    · exact Or.inr ⟨k, by rw [hk]⟩
    · exact Or.inl ⟨k + 1, by rw [hk]; lib_nat⟩

/-- p^j odd from u p + 2 v = 1. -/
theorem cond_of (p : Nat) (u v : Int) (huv : u * Int.ofNat p + v * 2 = 1) (j a : Nat) (ha : p ^ j = a + 1) : ∃ k, Int.ofNat (a + 1) = 2 * k + 1 := by
  rcases GaussInt.int_even_odd (Int.ofNat (a + 1)) with ⟨q, hq⟩ | ⟨q, hq⟩
  · exact absurd (by rw [ha]; exact hq) (HeckeEig.not_div p 2 u v huv (Nat.le_of_ble_eq_true rfl) j q)
  · exact ⟨q, hq⟩

/-- c(p^j m) = c(p^j) c(m) for odd m prime to p. -/
theorem mul_pow (P : Nat) (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Cyclo4.cyc4 LibInt.intR).mul α β) = (Cyclo4.cyc4 LibInt.intR).mul (W α) (W β))
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 2 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∃ k, Int.ofNat (m + 1) = 2 * k + 1) (j : Nat) :
    cw W ((P + 1 + 1) ^ j * (m + 1)) = (Cyclo4.cyc4 LibInt.intR).mul (cw W ((P + 1 + 1) ^ j)) (cw W (m + 1)) := by
  obtain ⟨a, ha⟩ := HeckeEig.succ_pow (P + 1) j
  obtain ⟨s', t', h'⟩ := HeckeEig.bez_pow (P + 1 + 1) _ s t hst j
  rw [ha] at h'
  rw [ha]
  obtain ⟨km, hkm⟩ := cond_of (P + 1 + 1) u v huv j a ha
  obtain ⟨kn, hkn⟩ := hm
  exact hecke_mult_w W hW a m s' t' km kn h' hkm hkn

/-- **Split p: the Hecke relation at every p^j m.** -/
theorem eigG_split (P : Nat) (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Cyclo4.cyc4 LibInt.intR).mul α β) = (Cyclo4.cyc4 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Cyclo4.cyc4 LibInt.intR).one = (Cyclo4.cyc4 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (a b : Int) (hab : a * a + b * b = Int.ofNat (P + 1 + 1)) (hπ : Prim (a, b))
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 2 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∃ k, Int.ofNat (m + 1) = 2 * k + 1) (j : Nat) :
    cw W ((P + 1 + 1) ^ (j + 2) * (m + 1)) = (Cyclo4.cyc4 LibInt.intR).add
      ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add (W (a, b)) (W (a, -b))) (cw W ((P + 1 + 1) ^ (j + 1) * (m + 1))))
      ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (W (a, b)) (W (a, -b))) (cw W ((P + 1 + 1) ^ j * (m + 1))))) :=
  HeckeEig.lift2 _ (cw W) (P + 1 + 1) (m + 1) (mul_pow P W hW u v huv m s t hst hm) _ _ (HeckePrime.cw_rec P W hW hW1 hp a b hab hπ) j

/-- Inert p = 4r + 3: c(p^(j+2)) = W(−p) c(p^j). -/
theorem inert_rec (r : Nat) (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Cyclo4.cyc4 LibInt.intR).mul α β) = (Cyclo4.cyc4 LibInt.intR).mul (W α) (W β))
    (hq : ∀ d, Dv d (2 * (2 * r) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * r) + 1 + 1 + 1)) (j : Nat) :
    cw W ((2 * (2 * r) + 1 + 1 + 1) ^ (j + 2)) = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (Cyclo4.cyc4 LibInt.intR).zero (cw W ((2 * (2 * r) + 1 + 1 + 1) ^ (j + 1))))
      ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).neg (W (HeckeInert.mq r))) (cw W ((2 * (2 * r) + 1 + 1 + 1) ^ j)))) := by
  rcases parity j with ⟨k, hk⟩ | ⟨k, hk⟩
  · rw [hk, show 2 * k + 2 = 2 * (k + 1) by lib_nat, HeckeInert.cw_even r W hq (k + 1), HeckeInert.cw_even r W hq k]
    show W ((Cyclo4.cyc4 LibInt.intR).mul (pw (Cyclo4.cyc4 LibInt.intR) (HeckeInert.mq r) k) (HeckeInert.mq r)) = _
    rw [hW]; lib_ring
  · rw [hk, show 2 * k + 1 + 2 = 2 * (k + 1) + 1 by lib_nat, HeckeInert.cw_odd r W hq (k + 1), HeckeInert.cw_odd r W hq k]; lib_ring

/-- **Inert p: the Hecke relation at every p^j m.** -/
theorem eigG_inert (r : Nat) (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Cyclo4.cyc4 LibInt.intR).mul α β) = (Cyclo4.cyc4 LibInt.intR).mul (W α) (W β))
    (hq : ∀ d, Dv d (2 * (2 * r) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * r) + 1 + 1 + 1))
    (u v : Int) (huv : u * Int.ofNat (2 * (2 * r) + 1 + 1 + 1) + v * 2 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (2 * (2 * r) + 1 + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∃ k, Int.ofNat (m + 1) = 2 * k + 1) (j : Nat) :
    cw W ((2 * (2 * r) + 1 + 1 + 1) ^ (j + 2) * (m + 1)) = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (Cyclo4.cyc4 LibInt.intR).zero (cw W ((2 * (2 * r) + 1 + 1 + 1) ^ (j + 1) * (m + 1))))
      ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).neg (W (HeckeInert.mq r))) (cw W ((2 * (2 * r) + 1 + 1 + 1) ^ j * (m + 1))))) :=
  HeckeEig.lift2 _ (cw W) (2 * (2 * r) + 1 + 1 + 1) (m + 1) (mul_pow (2 * (2 * r) + 1) W hW u v huv m s t hst hm) _ _ (inert_rec r W hW hq) j

end EigG

#print axioms EigG.parity
#print axioms EigG.cond_of
#print axioms EigG.mul_pow
#print axioms EigG.eigG_split
#print axioms EigG.inert_rec
#print axioms EigG.eigG_inert

lang/Eis3.lean

lean_all/lang/Eis3.lean

/-
  lang/Eis3 — T49, cycle 744: **Eisenstein reciprocity for ℓ = 3 at an inert prime, in symbol form**: for q = 3k + 2 prime and J the
  primary prime of ℤ[ω] above p = 6n + 7,
      χ_p(q) ≡ J^((q² − 1)/3)  (mod q)   in 𝔽_q[ω],
  i.e. (q/J)₃ = (J/q)₃ (the right side is the cubic residue symbol of J modulo the inert prime q, by Euler's criterion in 𝔽_{q²}).
    sc_mul, sc_pow   𝔽_q ⊂ 𝔽_q[ω]
    frobC            (a + bω)^q = a + bω² = conj(a + bω)       (frob_add, Fermat, ω^q = ω²)
    ph_cconj         reduction mod q commutes with conjugation
    eis3_inert       from CubicRecip.cubic_recip_inert: χ_p(q) = J^(k+1) p^k, p = J J̄ (JacobiNorm), J̄ ≡ J^q
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.CubicRecip

namespace Eis3

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius CubicChar JacobiSum JacobiNorm CubicRecip
open LibRing (castZ castN)

variable (n k : Nat)

def sc (a : Zp (3 * k + 1)) : Zp (3 * k + 1) × Zp (3 * k + 1) := (a, (ring (3 * k + 1)).zero)

theorem sc_mul (a b : Zp (3 * k + 1)) : (Cyclo3.cyc3 (ring (3 * k + 1))).mul (sc k a) (sc k b) = sc k ((ring (3 * k + 1)).mul a b) := by
  apply Prod.ext
  · show (ring (3 * k + 1)).add ((ring (3 * k + 1)).mul a b) ((ring (3 * k + 1)).neg ((ring (3 * k + 1)).mul (ring (3 * k + 1)).zero (ring (3 * k + 1)).zero)) = (ring (3 * k + 1)).mul a b; lib_ring
  · show (ring (3 * k + 1)).add ((ring (3 * k + 1)).add ((ring (3 * k + 1)).mul a (ring (3 * k + 1)).zero) ((ring (3 * k + 1)).mul (ring (3 * k + 1)).zero b)) ((ring (3 * k + 1)).neg ((ring (3 * k + 1)).mul (ring (3 * k + 1)).zero (ring (3 * k + 1)).zero)) = (ring (3 * k + 1)).zero; lib_ring

theorem sc_pow (a : Zp (3 * k + 1)) : ∀ m, pw (Cyclo3.cyc3 (ring (3 * k + 1))) (sc k a) m = sc k (pw (ring (3 * k + 1)) a m)
  | 0 => rfl
  | m + 1 => by
    show (Cyclo3.cyc3 (ring (3 * k + 1))).mul (pw (Cyclo3.cyc3 (ring (3 * k + 1))) (sc k a) m) (sc k a) = sc k ((ring (3 * k + 1)).mul (pw (ring (3 * k + 1)) a m) a)
    rw [sc_pow a m, sc_mul]

def om : Zp (3 * k + 1) × Zp (3 * k + 1) := ((ring (3 * k + 1)).zero, (ring (3 * k + 1)).one)

theorem om3 : pw (Cyclo3.cyc3 (ring (3 * k + 1))) (om k) 3 = (Cyclo3.cyc3 (ring (3 * k + 1))).one := by
  apply Prod.ext
  · show ((ring (3 * k + 1)).add ((ring (3 * k + 1)).mul ((ring (3 * k + 1)).add ((ring (3 * k + 1)).mul ((ring (3 * k + 1)).add ((ring (3 * k + 1)).mul (ring (3 * k + 1)).one (ring (3 * k + 1)).zero) ((ring (3 * k + 1)).neg ((ring (3 * k + 1)).mul (ring (3 * k + 1)).zero (ring (3 * k + 1)).one))) (ring (3 * k + 1)).zero) ((ring (3 * k + 1)).neg ((ring (3 * k + 1)).mul ((ring (3 * k + 1)).add ((ri  -- (+1408 chars; full line in the Zenodo source)
    lib_ring
  · show ((ring (3 * k + 1)).add ((ring (3 * k + 1)).add ((ring (3 * k + 1)).mul ((ring (3 * k + 1)).add ((ring (3 * k + 1)).mul ((ring (3 * k + 1)).add ((ring (3 * k + 1)).mul (ring (3 * k + 1)).one (ring (3 * k + 1)).zero) ((ring (3 * k + 1)).neg ((ring (3 * k + 1)).mul (ring (3 * k + 1)).zero (ring (3 * k + 1)).one))) (ring (3 * k + 1)).zero) ((ring (3 * k + 1)).neg ((ring (3 * k + 1)).mul ((ri  -- (+2478 chars; full line in the Zenodo source)
    lib_ring

theorem decomp (a b : Zp (3 * k + 1)) : (a, b) = (Cyclo3.cyc3 (ring (3 * k + 1))).add (sc k a) ((Cyclo3.cyc3 (ring (3 * k + 1))).mul (sc k b) (om k)) := by
  apply Prod.ext
  · show a = ((ring (3 * k + 1)).add a ((ring (3 * k + 1)).add ((ring (3 * k + 1)).mul b (ring (3 * k + 1)).zero) ((ring (3 * k + 1)).neg ((ring (3 * k + 1)).mul (ring (3 * k + 1)).zero (ring (3 * k + 1)).one))))
    lib_ring
  · show b = ((ring (3 * k + 1)).add (ring (3 * k + 1)).zero ((ring (3 * k + 1)).add ((ring (3 * k + 1)).add ((ring (3 * k + 1)).mul b (ring (3 * k + 1)).one) ((ring (3 * k + 1)).mul (ring (3 * k + 1)).zero (ring (3 * k + 1)).zero)) ((ring (3 * k + 1)).neg ((ring (3 * k + 1)).mul (ring (3 * k + 1)).zero (ring (3 * k + 1)).one))))
    lib_ring

theorem frobC (hq : ∀ d, Dv d (3 * k + 1 + 1) → d = 1 ∨ d = 3 * k + 1 + 1) (a b : Zp (3 * k + 1)) :
    pw (Cyclo3.cyc3 (ring (3 * k + 1))) (a, b) (3 * k + 1 + 1) = ((ring (3 * k + 1)).add a ((ring (3 * k + 1)).neg b), (ring (3 * k + 1)).neg b) := by
  rw [decomp k a b, Frobenius.frob_add (Cyclo3.cyc3 (ring (3 * k + 1))) (3 * k) hq (CubicRecip.charq k), Frobenius.pw_mul, sc_pow, sc_pow, ZMod.fermat (3 * k) hq a, ZMod.fermat (3 * k) hq b,
    show (3 * k + 1 + 1) = 3 * k + 2 from rfl, CubicRecip.om_pow k (om k) (om3 k) k]
  apply Prod.ext
  · show ((ring (3 * k + 1)).add a ((ring (3 * k + 1)).add ((ring (3 * k + 1)).mul b ((ring (3 * k + 1)).add ((ring (3 * k + 1)).mul (ring (3 * k + 1)).zero (ring (3 * k + 1)).zero) ((ring (3 * k + 1)).neg ((ring (3 * k + 1)).mul (ring (3 * k + 1)).one (ring (3 * k + 1)).one)))) ((ring (3 * k + 1)).neg ((ring (3 * k + 1)).mul (ring (3 * k + 1)).zero ((ring (3 * k + 1)).add ((ring (3 * k + 1)).add   -- (+299 chars; full line in the Zenodo source)
    lib_ring
  · show ((ring (3 * k + 1)).add (ring (3 * k + 1)).zero ((ring (3 * k + 1)).add ((ring (3 * k + 1)).add ((ring (3 * k + 1)).mul b ((ring (3 * k + 1)).add ((ring (3 * k + 1)).add ((ring (3 * k + 1)).mul (ring (3 * k + 1)).zero (ring (3 * k + 1)).one) ((ring (3 * k + 1)).mul (ring (3 * k + 1)).one (ring (3 * k + 1)).zero)) ((ring (3 * k + 1)).neg ((ring (3 * k + 1)).mul (ring (3 * k + 1)).one (ring  -- (+658 chars; full line in the Zenodo source)
    lib_ring


theorem ph_cconj (z : Int × Int) : ph (ring (3 * k + 1)) (cconj z) = ((ring (3 * k + 1)).add (ph (ring (3 * k + 1)) z).1 ((ring (3 * k + 1)).neg (ph (ring (3 * k + 1)) z).2), (ring (3 * k + 1)).neg (ph (ring (3 * k + 1)) z).2) := by
  show (castZ (ring (3 * k + 1)) (z.1 + -z.2), castZ (ring (3 * k + 1)) (-z.2)) = _
  rw [IntCast.castZ_add, HasseDavenport.castZ_neg]; rfl

theorem frob_ph (hq : ∀ d, Dv d (3 * k + 1 + 1) → d = 1 ∨ d = 3 * k + 1 + 1) (z : Int × Int) : pw (Cyclo3.cyc3 (ring (3 * k + 1))) (ph (ring (3 * k + 1)) z) (3 * k + 1 + 1) = ph (ring (3 * k + 1)) (cconj z) := by
  rw [ph_cconj]
  exact frobC k hq (ph (ring (3 * k + 1)) z).1 (ph (ring (3 * k + 1)) z).2

theorem pe_ph (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : ph (ring (3 * k + 1)) (Pe n) = (Cyclo3.cyc3 (ring (3 * k + 1))).mul (ph (ring (3 * k + 1)) (J n w)) (ph (ring (3 * k + 1)) (cconj (J n w))) := by
  rw [← ph_mul]
  congr 1
  have e : (J n w) = ((J n w).1, (J n w).2) := rfl
  rw [e, norm_form]
  show (Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int)) = _
  rw [← jacobi_norm n hpr w hw1 hw3]

/-- **Eisenstein reciprocity, ℓ = 3, inert q: χ_p(q) ≡ J^((q² − 1)/3) (mod q).** -/
theorem eis3_inert (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (hq : ∀ d, Dv d (3 * k + 1 + 1) → d = 1 ∨ d = 3 * k + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (hQ : nsm (ring (6 * n + 5 + 1)) (3 * k + 1 + 1) (ring (6 * n + 5 + 1)).one ≠ (ring (6 * n + 5 + 1)).zero) :
    ph (ring (3 * k + 1)) (chi3 n w (nsm (ring (6 * n + 5 + 1)) (3 * k + 1 + 1) (ring (6 * n + 5 + 1)).one)) = pw (Cyclo3.cyc3 (ring (3 * k + 1))) (ph (ring (3 * k + 1)) (J n w)) ((3 * k + 1) * (k + 1)) := by
  rw [cubic_recip_inert n k hpr hq w hw1 hw3 hQ, pe_ph n k hpr w hw1 hw3, ← frob_ph k hq, Frobenius.pw_mul, TwoPower.pwpow,
    ← HasseCM.pw_add', ← HasseCM.pw_add']
  congr 1
  lib_nat

end Eis3

#print axioms Eis3.sc
#print axioms Eis3.sc_mul
#print axioms Eis3.sc_pow
#print axioms Eis3.om
#print axioms Eis3.om3
#print axioms Eis3.decomp
#print axioms Eis3.frobC
#print axioms Eis3.ph_cconj
#print axioms Eis3.frob_ph
#print axioms Eis3.pe_ph
#print axioms Eis3.eis3_inert

lang/Eis3s.lean

lean_all/lang/Eis3s.lean

/-
  lang/Eis3s — T49, cycle 745: **Eisenstein reciprocity for ℓ = 3 at a split prime q ≡ 1 (mod 3), symbol form**: with u ∈ 𝔽_q a cube
  root of unity (u² + u + 1 = 0) the evaluation ev_u : 𝔽_q[ω] → 𝔽_q, a + bω ↦ a + bu, is reduction modulo a prime π | q, and
      χ̄_p(q) ≡ (J/π)₃² (J̄/π)₃  (mod π),   (X/π)₃ = X(u)^((q−1)/3),
  i.e. (q/J)₃ = (J/π)(J/π̄) = (J/q)₃ (since (J/π̄) ≡ (J̄/π)² mod π).
    ev_mul, ev_pow   ev_u is multiplicative when u² + u + 1 = 0
    eis3_split       from CubicRecipSplit.cubic_recip_split and p = J J̄
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.CubicRecipSplit
import lang.Eis3

namespace Eis3s

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius CubicChar JacobiSum JacobiNorm CubicRecip CubicRecipSplit
open LibRing (castZ castN)

variable (n m : Nat)

def ev (u : Zp (3 * m + 2 + 1)) (z : Zp (3 * m + 2 + 1) × Zp (3 * m + 2 + 1)) : Zp (3 * m + 2 + 1) := (ring (3 * m + 2 + 1)).add z.1 ((ring (3 * m + 2 + 1)).mul z.2 u)

theorem ev_mul (u : Zp (3 * m + 2 + 1)) (hu : (ring (3 * m + 2 + 1)).add ((ring (3 * m + 2 + 1)).add ((ring (3 * m + 2 + 1)).mul u u) u) (ring (3 * m + 2 + 1)).one = (ring (3 * m + 2 + 1)).zero) (x y : Zp (3 * m + 2 + 1) × Zp (3 * m + 2 + 1)) :
    ev m u ((Cyclo3.cyc3 (ring (3 * m + 2 + 1))).mul x y) = (ring (3 * m + 2 + 1)).mul (ev m u x) (ev m u y) := by
  show (ring (3 * m + 2 + 1)).add ((ring (3 * m + 2 + 1)).add ((ring (3 * m + 2 + 1)).mul x.1 y.1) ((ring (3 * m + 2 + 1)).neg ((ring (3 * m + 2 + 1)).mul x.2 y.2))) ((ring (3 * m + 2 + 1)).mul ((ring (3 * m + 2 + 1)).add ((ring (3 * m + 2 + 1)).add ((ring (3 * m + 2 + 1)).mul x.1 y.2) ((ring (3 * m + 2 + 1)).mul x.2 y.1)) ((ring (3 * m + 2 + 1)).neg ((ring (3 * m + 2 + 1)).mul x.2 y.2))) u) = _
  have e : (ring (3 * m + 2 + 1)).mul (ev m u x) (ev m u y) = (ring (3 * m + 2 + 1)).add ((ring (3 * m + 2 + 1)).add ((ring (3 * m + 2 + 1)).add ((ring (3 * m + 2 + 1)).mul x.1 y.1) ((ring (3 * m + 2 + 1)).neg ((ring (3 * m + 2 + 1)).mul x.2 y.2))) ((ring (3 * m + 2 + 1)).mul ((ring (3 * m + 2 + 1)).add ((ring (3 * m + 2 + 1)).add ((ring (3 * m + 2 + 1)).mul x.1 y.2) ((ring (3 * m + 2 + 1)).mul x.  -- (+78 chars; full line in the Zenodo source)
      ((ring (3 * m + 2 + 1)).mul ((ring (3 * m + 2 + 1)).mul x.2 y.2) ((ring (3 * m + 2 + 1)).add ((ring (3 * m + 2 + 1)).add ((ring (3 * m + 2 + 1)).mul u u) u) (ring (3 * m + 2 + 1)).one)) := by unfold ev; lib_ring
  rw [e, hu]; lib_ring

theorem ev_one (u : Zp (3 * m + 2 + 1)) : ev m u (Cyclo3.cyc3 (ring (3 * m + 2 + 1))).one = (ring (3 * m + 2 + 1)).one := by
  show (ring (3 * m + 2 + 1)).add (ring (3 * m + 2 + 1)).one ((ring (3 * m + 2 + 1)).mul (ring (3 * m + 2 + 1)).zero u) = (ring (3 * m + 2 + 1)).one; lib_ring

theorem ev_pow (u : Zp (3 * m + 2 + 1)) (hu : (ring (3 * m + 2 + 1)).add ((ring (3 * m + 2 + 1)).add ((ring (3 * m + 2 + 1)).mul u u) u) (ring (3 * m + 2 + 1)).one = (ring (3 * m + 2 + 1)).zero) (x : Zp (3 * m + 2 + 1) × Zp (3 * m + 2 + 1)) : ∀ j, ev m u (pw (Cyclo3.cyc3 (ring (3 * m + 2 + 1))) x j) = pw (ring (3 * m + 2 + 1)) (ev m u x) j
  | 0 => ev_one m u
  | j + 1 => by
    show ev m u ((Cyclo3.cyc3 (ring (3 * m + 2 + 1))).mul (pw (Cyclo3.cyc3 (ring (3 * m + 2 + 1))) x j) x) = (ring (3 * m + 2 + 1)).mul (pw (ring (3 * m + 2 + 1)) (ev m u x) j) (ev m u x)
    rw [ev_mul m u hu, ev_pow u hu x j]

theorem pe_ph' (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : ph (ring (3 * m + 2 + 1)) (Pe n) = (Cyclo3.cyc3 (ring (3 * m + 2 + 1))).mul (ph (ring (3 * m + 2 + 1)) (J n w)) (ph (ring (3 * m + 2 + 1)) (cconj (J n w))) := by
  rw [← ph_mul]
  congr 1
  have e : (J n w) = ((J n w).1, (J n w).2) := rfl
  rw [e, norm_form]
  show (Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int)) = _
  rw [← jacobi_norm n hpr w hw1 hw3]

/-- **Eisenstein reciprocity, ℓ = 3, split q: χ̄_p(q) ≡ (J/π)²(J̄/π) (mod π).** -/
theorem eis3_split (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (hq : ∀ d, Dv d (3 * m + 2 + 1 + 1) → d = 1 ∨ d = 3 * m + 2 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (hQ : nsm (ring (6 * n + 5 + 1)) (3 * m + 2 + 1 + 1) (ring (6 * n + 5 + 1)).one ≠ (ring (6 * n + 5 + 1)).zero)   -- (+181 chars; full line in the Zenodo source)
    ev m u (ph (ring (3 * m + 2 + 1)) (cb n w (nsm (ring (6 * n + 5 + 1)) (3 * m + 2 + 1 + 1) (ring (6 * n + 5 + 1)).one)))
      = (ring (3 * m + 2 + 1)).mul (pw (ring (3 * m + 2 + 1)) (ev m u (ph (ring (3 * m + 2 + 1)) (J n w))) (2 * (m + 1))) (pw (ring (3 * m + 2 + 1)) (ev m u (ph (ring (3 * m + 2 + 1)) (cconj (J n w)))) (m + 1)) := by
  rw [cubic_recip_split n m hpr hq w hw1 hw3 hQ, ev_mul m u hu, ev_pow m u hu, ev_pow m u hu, pe_ph' n m hpr w hw1 hw3,
    ev_mul m u hu, Frobenius.pw_mul, show 2 * (m + 1) = (m + 1) + (m + 1) by lib_nat, HasseCM.pw_add' (ring (3 * m + 2 + 1)) (ev m u (ph (ring (3 * m + 2 + 1)) (J n w))) (m + 1) (m + 1)]
  lib_ring

end Eis3s

#print axioms Eis3s.ev
#print axioms Eis3s.ev_mul
#print axioms Eis3s.ev_one
#print axioms Eis3s.ev_pow
#print axioms Eis3s.pe_ph'
#print axioms Eis3s.eis3_split

lang/Eis4.lean

lean_all/lang/Eis4.lean

/-
  lang/Eis4 — T49, cycle 746: **quartic Eisenstein reciprocity at an inert prime, symbol form**: for q = 4k + 3 prime and J the
  Jacobi sum of the quartic character mod p = 4n + 5 (N(J) = p),
      χ(q)·χ̄(−1) ≡ J^((q² − 1)/4)  (mod q)   in 𝔽_q[i]         ((q² − 1)/4 = (4k + 2)(k + 1)),
  from QuartRecip.quart_recip_inert (= J^(2k+2) p^k) with Frobenius of 𝔽_q[i] = conjugation and p = J J̄ (QuartJacobi.quart_norm).
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.QuartRecip

namespace Eis4

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius QuartChar QuartJacobi QuartRecip
open LibRing (castZ castN)

variable (n k : Nat)

def sc (a : Zp (4 * k + 1 + 1)) : Zp (4 * k + 1 + 1) × Zp (4 * k + 1 + 1) := (a, (ring (4 * k + 1 + 1)).zero)

theorem sc_mul (a b : Zp (4 * k + 1 + 1)) : (Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul (sc k a) (sc k b) = sc k ((ring (4 * k + 1 + 1)).mul a b) := by
  apply Prod.ext
  · show (ring (4 * k + 1 + 1)).add ((ring (4 * k + 1 + 1)).mul a b) ((ring (4 * k + 1 + 1)).neg ((ring (4 * k + 1 + 1)).mul (ring (4 * k + 1 + 1)).zero (ring (4 * k + 1 + 1)).zero)) = (ring (4 * k + 1 + 1)).mul a b; lib_ring
  · show (ring (4 * k + 1 + 1)).add ((ring (4 * k + 1 + 1)).mul a (ring (4 * k + 1 + 1)).zero) ((ring (4 * k + 1 + 1)).mul (ring (4 * k + 1 + 1)).zero b) = (ring (4 * k + 1 + 1)).zero; lib_ring

theorem sc_pow (a : Zp (4 * k + 1 + 1)) : ∀ m, pw (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (sc k a) m = sc k (pw (ring (4 * k + 1 + 1)) a m)
  | 0 => rfl
  | m + 1 => by
    show (Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul (pw (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (sc k a) m) (sc k a) = sc k ((ring (4 * k + 1 + 1)).mul (pw (ring (4 * k + 1 + 1)) a m) a)
    rw [sc_pow a m, sc_mul]

def ii : Zp (4 * k + 1 + 1) × Zp (4 * k + 1 + 1) := ((ring (4 * k + 1 + 1)).zero, (ring (4 * k + 1 + 1)).one)

theorem ii4 : pw (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (ii k) 4 = (Cyclo4.cyc4 (ring (4 * k + 1 + 1))).one := by
  apply Prod.ext
  · show ((ring (4 * k + 1 + 1)).add ((ring (4 * k + 1 + 1)).mul ((ring (4 * k + 1 + 1)).add ((ring (4 * k + 1 + 1)).mul ((ring (4 * k + 1 + 1)).add ((ring (4 * k + 1 + 1)).mul ((ring (4 * k + 1 + 1)).add ((ring (4 * k + 1 + 1)).mul (ring (4 * k + 1 + 1)).one (ring (4 * k + 1 + 1)).zero) ((ring (4 * k + 1 + 1)).neg ((ring (4 * k + 1 + 1)).mul (ring (4 * k + 1 + 1)).zero (ring (4 * k + 1 + 1)).one)  -- (+2439 chars; full line in the Zenodo source)
    lib_ring
  · show ((ring (4 * k + 1 + 1)).add ((ring (4 * k + 1 + 1)).mul ((ring (4 * k + 1 + 1)).add ((ring (4 * k + 1 + 1)).mul ((ring (4 * k + 1 + 1)).add ((ring (4 * k + 1 + 1)).mul ((ring (4 * k + 1 + 1)).add ((ring (4 * k + 1 + 1)).mul (ring (4 * k + 1 + 1)).one (ring (4 * k + 1 + 1)).zero) ((ring (4 * k + 1 + 1)).neg ((ring (4 * k + 1 + 1)).mul (ring (4 * k + 1 + 1)).zero (ring (4 * k + 1 + 1)).one)  -- (+2411 chars; full line in the Zenodo source)
    lib_ring

theorem ii3 : pw (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (ii k) 3 = (Cyclo4.cyc4 (ring (4 * k + 1 + 1))).neg (ii k) := by
  apply Prod.ext
  · show ((ring (4 * k + 1 + 1)).add ((ring (4 * k + 1 + 1)).mul ((ring (4 * k + 1 + 1)).add ((ring (4 * k + 1 + 1)).mul ((ring (4 * k + 1 + 1)).add ((ring (4 * k + 1 + 1)).mul (ring (4 * k + 1 + 1)).one (ring (4 * k + 1 + 1)).zero) ((ring (4 * k + 1 + 1)).neg ((ring (4 * k + 1 + 1)).mul (ring (4 * k + 1 + 1)).zero (ring (4 * k + 1 + 1)).one))) (ring (4 * k + 1 + 1)).zero) ((ring (4 * k + 1 + 1)).  -- (+997 chars; full line in the Zenodo source)
    lib_ring
  · show ((ring (4 * k + 1 + 1)).add ((ring (4 * k + 1 + 1)).mul ((ring (4 * k + 1 + 1)).add ((ring (4 * k + 1 + 1)).mul ((ring (4 * k + 1 + 1)).add ((ring (4 * k + 1 + 1)).mul (ring (4 * k + 1 + 1)).one (ring (4 * k + 1 + 1)).zero) ((ring (4 * k + 1 + 1)).neg ((ring (4 * k + 1 + 1)).mul (ring (4 * k + 1 + 1)).zero (ring (4 * k + 1 + 1)).one))) (ring (4 * k + 1 + 1)).zero) ((ring (4 * k + 1 + 1)).  -- (+967 chars; full line in the Zenodo source)
    lib_ring

theorem ii_q : ∀ j, pw (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (ii k) (4 * j + 3) = (Cyclo4.cyc4 (ring (4 * k + 1 + 1))).neg (ii k)
  | 0 => ii3 k
  | j + 1 => by
    rw [show 4 * (j + 1) + 3 = (4 * j + 3) + 4 by lib_nat, HasseCM.pw_add', ii_q j, ii4]; lib_ring

theorem decomp (a b : Zp (4 * k + 1 + 1)) : (a, b) = (Cyclo4.cyc4 (ring (4 * k + 1 + 1))).add (sc k a) ((Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul (sc k b) (ii k)) := by
  apply Prod.ext
  · show a = ((ring (4 * k + 1 + 1)).add a ((ring (4 * k + 1 + 1)).add ((ring (4 * k + 1 + 1)).mul b (ring (4 * k + 1 + 1)).zero) ((ring (4 * k + 1 + 1)).neg ((ring (4 * k + 1 + 1)).mul (ring (4 * k + 1 + 1)).zero (ring (4 * k + 1 + 1)).one))))
    lib_ring
  · show b = ((ring (4 * k + 1 + 1)).add (ring (4 * k + 1 + 1)).zero ((ring (4 * k + 1 + 1)).add ((ring (4 * k + 1 + 1)).mul b (ring (4 * k + 1 + 1)).one) ((ring (4 * k + 1 + 1)).mul (ring (4 * k + 1 + 1)).zero (ring (4 * k + 1 + 1)).zero)))
    lib_ring

theorem frobC4 (hq : ∀ d, Dv d (4 * k + 1 + 1 + 1) → d = 1 ∨ d = 4 * k + 1 + 1 + 1) (a b : Zp (4 * k + 1 + 1)) : pw (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (a, b) (4 * k + 1 + 1 + 1) = (a, (ring (4 * k + 1 + 1)).neg b) := by
  rw [decomp k a b, Frobenius.frob_add (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * k + 1) hq (QuartRecip.charq4 (ring (4 * k + 1 + 1)) (4 * k + 1 + 1 + 1) (ZMod.char_p (4 * k + 1 + 1))), Frobenius.pw_mul, sc_pow, sc_pow,
    ZMod.fermat (4 * k + 1) hq a, ZMod.fermat (4 * k + 1) hq b, show 4 * k + 1 + 1 + 1 = 4 * k + 3 from rfl, ii_q k k]
  apply Prod.ext
  · show ((ring (4 * k + 1 + 1)).add a ((ring (4 * k + 1 + 1)).add ((ring (4 * k + 1 + 1)).mul b ((ring (4 * k + 1 + 1)).neg (ring (4 * k + 1 + 1)).zero)) ((ring (4 * k + 1 + 1)).neg ((ring (4 * k + 1 + 1)).mul (ring (4 * k + 1 + 1)).zero ((ring (4 * k + 1 + 1)).neg (ring (4 * k + 1 + 1)).one))))) = a
    lib_ring
  · show ((ring (4 * k + 1 + 1)).add (ring (4 * k + 1 + 1)).zero ((ring (4 * k + 1 + 1)).add ((ring (4 * k + 1 + 1)).mul b ((ring (4 * k + 1 + 1)).neg (ring (4 * k + 1 + 1)).one)) ((ring (4 * k + 1 + 1)).mul (ring (4 * k + 1 + 1)).zero ((ring (4 * k + 1 + 1)).neg (ring (4 * k + 1 + 1)).zero)))) = (ring (4 * k + 1 + 1)).neg b
    lib_ring

theorem frob_ph4 (hq : ∀ d, Dv d (4 * k + 1 + 1 + 1) → d = 1 ∨ d = 4 * k + 1 + 1 + 1) (z : Int × Int) : pw (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (ph4 (ring (4 * k + 1 + 1)) z) (4 * k + 1 + 1 + 1) = ph4 (ring (4 * k + 1 + 1)) (gconj z) := by
  show pw (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (castZ (ring (4 * k + 1 + 1)) z.1, castZ (ring (4 * k + 1 + 1)) z.2) _ = (castZ (ring (4 * k + 1 + 1)) z.1, castZ (ring (4 * k + 1 + 1)) (-z.2))
  rw [frobC4 k hq, HasseDavenport.castZ_neg]

theorem pe4 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : ph4 (ring (4 * k + 1 + 1)) (Ppq n) = (Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul (ph4 (ring (4 * k + 1 + 1)) (Jq n (chi4 n ι))) (ph4 (ring (4 * k + 1 + 1)) (gconj (Jq n (chi4 n ι)))) := by
  rw [← ph4_mul (ring (4 * k + 1 + 1))]
  congr 1
  rw [show Ppq n = (Int.ofNat (4 * n + 3 + 1 + 1), 0) from sum_oneq n _, ← quart_norm n hpr ι hi]
  apply Prod.ext
  · show (Jq n (chi4 n ι)).1 * (Jq n (chi4 n ι)).1 + (Jq n (chi4 n ι)).2 * (Jq n (chi4 n ι)).2 = (Jq n (chi4 n ι)).1 * (Jq n (chi4 n ι)).1 + -((Jq n (chi4 n ι)).2 * -(Jq n (chi4 n ι)).2); lib_int
  · show (0 : Int) = (Jq n (chi4 n ι)).1 * -(Jq n (chi4 n ι)).2 + (Jq n (chi4 n ι)).2 * (Jq n (chi4 n ι)).1; lib_int

theorem pow_comb {R : Type} (K : CRing R) (x : R) (k : Nat) :
    K.mul (pw K (K.mul (K.mul x x) K.one) (k + 1)) (pw K (K.mul x (pw K x (4 * k + 1 + 1 + 1))) k) = pw K x ((4 * k + 2) * (k + 1)) := by
  rw [show K.mul (K.mul x x) K.one = pw K x 2 by show _ = K.mul (K.mul K.one x) x; lib_ring, TwoPower.pwpow, Frobenius.pw_mul, TwoPower.pwpow,
    ← HasseCM.pw_add', ← HasseCM.pw_add']
  congr 1
  lib_nat

/-- **Quartic Eisenstein reciprocity, inert q: χ(q)χ̄(−1) ≡ J^((q²−1)/4) (mod q).** -/
theorem eis4_inert (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (hq : ∀ d, Dv d (4 * k + 1 + 1 + 1) → d = 1 ∨ d = 4 * k + 1 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (hQ : nsm (ring (4 * n + 3 + 1)) (4 * k + 1 + 1 + 1) (ring (4 * n + 3 + 1)).one ≠ (ring (4 * n + 3 + 1)).zero) :
    (Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul (ph4 (ring (4 * k + 1 + 1)) (chi4 n ι (nsm (ring (4 * n + 3 + 1)) (4 * k + 1 + 1 + 1) (ring (4 * n + 3 + 1)).one))) (ph4 (ring (4 * k + 1 + 1)) (chib n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)))
      = pw (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (ph4 (ring (4 * k + 1 + 1)) (Jq n (chi4 n ι))) ((4 * k + 2) * (k + 1)) := by
  rw [quart_recip_inert n k hpr hq ι hi hQ, pe4 n k hpr ι hi, ← frob_ph4 k hq, ph4_one (ring (4 * k + 1 + 1))]
  exact pow_comb _ _ k

end Eis4
#print axioms Eis4.sc
#print axioms Eis4.sc_mul
#print axioms Eis4.sc_pow
#print axioms Eis4.ii
#print axioms Eis4.ii4
#print axioms Eis4.ii3
#print axioms Eis4.ii_q
#print axioms Eis4.decomp
#print axioms Eis4.frobC4
#print axioms Eis4.frob_ph4
#print axioms Eis4.pe4
#print axioms Eis4.pow_comb
#print axioms Eis4.eis4_inert

lang/Eis4s.lean

lean_all/lang/Eis4s.lean

/-
  lang/Eis4s — T49, cycle 747: **quartic Eisenstein reciprocity at a split prime q = 4k + 5, symbol form**: with u ∈ 𝔽_q, u² = −1,
  the evaluation ev_u(a + bi) = a + bu is reduction modulo a prime π | q, and
      χ̄(q) ≡ (J/π)₄³ (J̄/π)₄   (mod π),    (X/π)₄ = X(u)^((q−1)/4)
  (from QuartRecip.quart_recip_split: χ̄(q) = J^(2k+2) p^(k+1), p = J J̄).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Eis4

namespace Eis4s

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius QuartChar QuartJacobi QuartRecip
open LibRing (castZ castN)

variable (n k : Nat)

def ev (u : Zp (4 * k + 3 + 1)) (z : Zp (4 * k + 3 + 1) × Zp (4 * k + 3 + 1)) : Zp (4 * k + 3 + 1) := (ring (4 * k + 3 + 1)).add z.1 ((ring (4 * k + 3 + 1)).mul z.2 u)

theorem ev_mul (u : Zp (4 * k + 3 + 1)) (hu : (ring (4 * k + 3 + 1)).add ((ring (4 * k + 3 + 1)).mul u u) (ring (4 * k + 3 + 1)).one = (ring (4 * k + 3 + 1)).zero) (x y : Zp (4 * k + 3 + 1) × Zp (4 * k + 3 + 1)) :
    ev k u ((Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul x y) = (ring (4 * k + 3 + 1)).mul (ev k u x) (ev k u y) := by
  show (ring (4 * k + 3 + 1)).add ((ring (4 * k + 3 + 1)).add ((ring (4 * k + 3 + 1)).mul x.1 y.1) ((ring (4 * k + 3 + 1)).neg ((ring (4 * k + 3 + 1)).mul x.2 y.2))) ((ring (4 * k + 3 + 1)).mul ((ring (4 * k + 3 + 1)).add ((ring (4 * k + 3 + 1)).mul x.1 y.2) ((ring (4 * k + 3 + 1)).mul x.2 y.1)) u) = _
  have e : (ring (4 * k + 3 + 1)).mul (ev k u x) (ev k u y) = (ring (4 * k + 3 + 1)).add ((ring (4 * k + 3 + 1)).add ((ring (4 * k + 3 + 1)).add ((ring (4 * k + 3 + 1)).mul x.1 y.1) ((ring (4 * k + 3 + 1)).neg ((ring (4 * k + 3 + 1)).mul x.2 y.2))) ((ring (4 * k + 3 + 1)).mul ((ring (4 * k + 3 + 1)).add ((ring (4 * k + 3 + 1)).mul x.1 y.2) ((ring (4 * k + 3 + 1)).mul x.2 y.1)) u))
      ((ring (4 * k + 3 + 1)).mul ((ring (4 * k + 3 + 1)).mul x.2 y.2) ((ring (4 * k + 3 + 1)).add ((ring (4 * k + 3 + 1)).mul u u) (ring (4 * k + 3 + 1)).one)) := by unfold ev; lib_ring
  rw [e, hu]; lib_ring

theorem ev_one (u : Zp (4 * k + 3 + 1)) : ev k u (Cyclo4.cyc4 (ring (4 * k + 3 + 1))).one = (ring (4 * k + 3 + 1)).one := by
  show (ring (4 * k + 3 + 1)).add (ring (4 * k + 3 + 1)).one ((ring (4 * k + 3 + 1)).mul (ring (4 * k + 3 + 1)).zero u) = (ring (4 * k + 3 + 1)).one; lib_ring

theorem ev_pow (u : Zp (4 * k + 3 + 1)) (hu : (ring (4 * k + 3 + 1)).add ((ring (4 * k + 3 + 1)).mul u u) (ring (4 * k + 3 + 1)).one = (ring (4 * k + 3 + 1)).zero) (x : Zp (4 * k + 3 + 1) × Zp (4 * k + 3 + 1)) : ∀ j, ev k u (pw (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) x j) = pw (ring (4 * k + 3 + 1)) (ev k u x) j
  | 0 => ev_one k u
  | j + 1 => by
    show ev k u ((Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul (pw (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) x j) x) = (ring (4 * k + 3 + 1)).mul (pw (ring (4 * k + 3 + 1)) (ev k u x) j) (ev k u x)
    rw [ev_mul k u hu, ev_pow u hu x j]

theorem pe4' (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : ph4 (ring (4 * k + 3 + 1)) (Ppq n) = (Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul (ph4 (ring (4 * k + 3 + 1)) (Jq n (chi4 n ι))) (ph4 (ring (4 * k + 3 + 1)) (gconj (Jq n (chi4 n ι)))) := by
  rw [← ph4_mul (ring (4 * k + 3 + 1))]
  congr 1
  rw [show Ppq n = (Int.ofNat (4 * n + 3 + 1 + 1), 0) from sum_oneq n _, ← quart_norm n hpr ι hi]
  apply Prod.ext
  · show (Jq n (chi4 n ι)).1 * (Jq n (chi4 n ι)).1 + (Jq n (chi4 n ι)).2 * (Jq n (chi4 n ι)).2 = (Jq n (chi4 n ι)).1 * (Jq n (chi4 n ι)).1 + -((Jq n (chi4 n ι)).2 * -(Jq n (chi4 n ι)).2); lib_int
  · show (0 : Int) = (Jq n (chi4 n ι)).1 * -(Jq n (chi4 n ι)).2 + (Jq n (chi4 n ι)).2 * (Jq n (chi4 n ι)).1; lib_int

/-- **Quartic Eisenstein reciprocity, split q: χ̄(q) ≡ (J/π)³(J̄/π) (mod π).** -/
theorem eis4_split (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (hq : ∀ d, Dv d (4 * k + 3 + 1 + 1) → d = 1 ∨ d = 4 * k + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (hQ : nsm (ring (4 * n + 3 + 1)) (4 * k + 3 + 1 + 1) (ring (4 * n + 3 + 1)).one ≠ (ring (4 * n + 3 + 1)).zero) (u : Zp (4 *  -- (+138 chars; full line in the Zenodo source)
    ev k u (ph4 (ring (4 * k + 3 + 1)) (chib n ι (nsm (ring (4 * n + 3 + 1)) (4 * k + 3 + 1 + 1) (ring (4 * n + 3 + 1)).one)))
      = (ring (4 * k + 3 + 1)).mul (pw (ring (4 * k + 3 + 1)) (ev k u (ph4 (ring (4 * k + 3 + 1)) (Jq n (chi4 n ι)))) (3 * (k + 1))) (pw (ring (4 * k + 3 + 1)) (ev k u (ph4 (ring (4 * k + 3 + 1)) (gconj (Jq n (chi4 n ι))))) (k + 1)) := by
  rw [quart_recip_split n k hpr hq ι hi hQ, pe4' n k hpr ι hi, ph4_one (ring (4 * k + 3 + 1)), ev_mul k u hu, ev_pow k u hu, ev_pow k u hu, ev_mul k u hu,
    ev_mul k u hu, ev_mul k u hu, ev_one, Frobenius.pw_mul, Frobenius.pw_mul, Frobenius.pw_mul,
    show 3 * (k + 1) = (k + 1) + ((k + 1) + (k + 1)) by lib_nat, HasseCM.pw_add' (ring (4 * k + 3 + 1)) (ev k u (ph4 (ring (4 * k + 3 + 1)) (Jq n (chi4 n ι)))) (k + 1) ((k + 1) + (k + 1)),
    HasseCM.pw_add' (ring (4 * k + 3 + 1)) (ev k u (ph4 (ring (4 * k + 3 + 1)) (Jq n (chi4 n ι)))) (k + 1) (k + 1)]
  generalize Frobenius.pw (ring (4 * k + 3 + 1)) (ev k u (ph4 (ring (4 * k + 3 + 1)) (Jq n (chi4 n ι)))) (k + 1) = A
  generalize Frobenius.pw (ring (4 * k + 3 + 1)) (ev k u (ph4 (ring (4 * k + 3 + 1)) (gconj (Jq n (chi4 n ι))))) (k + 1) = B
  rw [TwoPower.pwone]
  lib_ring

end Eis4s

#print axioms Eis4s.ev
#print axioms Eis4s.ev_mul
#print axioms Eis4s.ev_one
#print axioms Eis4s.ev_pow
#print axioms Eis4s.pe4'
#print axioms Eis4s.eis4_split

lang/EisAlpha.lean

lean_all/lang/EisAlpha.lean

/-
  lang/EisAlpha — T49 (general ℓ ≥ 5): **Eisenstein's reciprocity transported from A to a prime element α of
  norm p**, in global-symbol form.
  ℓ = ms + 4 and p = j + 3 primes, p ≡ 1 (mod ℓ), α ∈ ℤ[C_ℓ] with N(α) = p and α ∈ P_1, q = k' + 2 a prime with
  q^e = ℓM + 1, (q − 1) | M, M even; E = 𝔽_q[C_ℓ]/(N), u = [σ], g_ℓ a generator of 𝔽_ℓ^×,
  G(W) = Π_i τ(g_ℓ^i)(W)^(g_ℓ^i) (lang/SymG), Y = φ_u(α)^M. Then, with j₀ from A ≡ ±σ^(j₀) B (lang/StickInst),
    (u^(j₀ M))^(ℓ−1) · G(Y) = (u^T)^(ℓ−1),     T = Σ_c k_c (ℓ − 1 − c)          (eis_alpha)
  where u^(k_c) is the symbol of q^(−e) at P_c (EisRecA). For g_ℓ a generator, G(Y) is u^(f·s) with ζ^s = (α/q)_ℓ
  (product over all primes above q; checked numerically in work/data/t49/globsym_test.py), so for primary α
  (j₀ ≡ 0) this is (α/q)_ℓ expressed through the q-symbols at the primes P_c — Eisenstein's reciprocity for α.
    nsm_pow       (a^e)·1 = (a·1)^e in 𝔽_ℓ
    q_order       q^e = ℓM + 1 ⇒ (q·1)^e = 1 in 𝔽_ℓ
    G_eps         ε = ±1, M even ⇒ G((ε·1)^M) = 1
    G_Bsym        G(φ_u(B)^M) = G(Y)
    eis_alpha     the reciprocity relation above
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.SymB2
import lang.SymY
import lang.PhzModN
import lang.StickInst
import lang.EisRecV
import lib.RingTac
import lib.Semi
import lib.IntRing

namespace EisAlpha

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob
open LibRing (castZ castN)

theorem nsm_pow (n a : Nat) : ∀ e, nsm (ring (n + 1)) (a ^ e) (ring (n + 1)).one = pw (ring (n + 1)) (nsm (ring (n + 1)) a (ring (n + 1)).one) e
  | 0 => by
    show nsm (ring (n + 1)) 1 (ring (n + 1)).one = (ring (n + 1)).one
    show (ring (n + 1)).add (ring (n + 1)).zero (ring (n + 1)).one = _
    lib_ring
  | e + 1 => by
    show nsm (ring (n + 1)) (a ^ e * a) (ring (n + 1)).one = (ring (n + 1)).mul (pw (ring (n + 1)) (nsm (ring (n + 1)) a (ring (n + 1)).one) e) (nsm (ring (n + 1)) a (ring (n + 1)).one)
    rw [SymB2.nu_mul, nsm_pow n a e]

/-- **(q·1)^e = 1 in 𝔽_ℓ** when q^e = ℓM + 1. -/
theorem q_order (n q e M : Nat) (h : q ^ e = (n + 1 + 1) * M + 1) :
    pw (ring (n + 1)) (nsm (ring (n + 1)) q (ring (n + 1)).one) e = (ring (n + 1)).one := by
  rw [← nsm_pow, h]
  show (ring (n + 1)).add (nsm (ring (n + 1)) ((n + 1 + 1) * M) (ring (n + 1)).one) (ring (n + 1)).one = _
  rw [SymB2.nu_mul, ZMod.char_p (n + 1)]
  lib_ring

/-- **φ_u(α) is a unit in E** when N(α) = p (p ≢ 0 mod q): the first factor of the norm list is σ(1)α = α. -/
theorem unit_alpha (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (n p : Nat)
    (hp : nsm (ring (k' + 1)) p (ring (k' + 1)).one ≠ (ring (k' + 1)).zero)
    (α : V Int (n + 1 + 1)) (iv : Zp (n + 1) → Zp (n + 1)) (h : Nat)
    (hN : NormEval.nrm (n + 1) α (BbarVal.nlist (n + 1) iv (StickQs.qsL n 0 n (h + 1)))
      = nsm (gring LibInt.intR (n + 1)) p (gring LibInt.intR (n + 1)).one) :
    ∃ v, (CycRing.cyc (ring (k' + 1)) (n + 1)).mul
        (CycEis.phz (CycRing.cyc (ring (k' + 1)) (n + 1)) (n + 1) (CycUnit.u (ring (k' + 1)) (n + 1)) α) v
        = (CycRing.cyc (ring (k' + 1)) (n + 1)).one := by
  obtain ⟨rest, hr⟩ : ∃ rest, NormEval.nrm (n + 1) α (BbarVal.nlist (n + 1) iv (StickQs.qsL n 0 n (h + 1)))
      = (gring LibInt.intR (n + 1)).mul (CycGal.gsig LibInt.intR (n + 1) (ring (n + 1)).one α) rest := ⟨_, rfl⟩
  rw [hr, CycFrob.gsig_id] at hN
  have h2 := congrArg (CycEis.phz (CycRing.cyc (ring (k' + 1)) (n + 1)) (n + 1) (CycUnit.u (ring (k' + 1)) (n + 1))) hN
  rw [CycEis.phz_mul _ (n + 1) _ (CycUnit.u_ell (ring (k' + 1)) (n + 1)), NormEval.phz_nsm] at h2
  exact SymY.unit_of_mul (n + 1) k' hq p hp _ _ h2

variable {R : Type} (K : CRing R) (n : Nat) (gl : Zp (n + 1)) (hgl : OrdMul.Ord (ring (n + 1)) gl (n + 1))

include hgl in
/-- **G((ε·1)^M) = 1** for ε = ±1 and M even. -/
theorem G_eps (ε : Int) (hε : ε = 1 ∨ ε = -1) (M' : Nat) :
    SymG.G K n gl (pw (CycRing.cyc K (n + 1)) (castZ (CycRing.cyc K (n + 1)) ε) (M' + M')) = (CycRing.cyc K (n + 1)).one := by
  have hsq : (CycRing.cyc K (n + 1)).mul (castZ (CycRing.cyc K (n + 1)) ε) (castZ (CycRing.cyc K (n + 1)) ε)
      = (CycRing.cyc K (n + 1)).one := by
    rw [← HasseDavenport.castZ_mul]
    have e1 : ε * ε = 1 := by
      cases hε with
      | inl h => rw [h]; rfl
      | inr h => rw [h]; rfl
    rw [e1]
    show (CycRing.cyc K (n + 1)).add (CycRing.cyc K (n + 1)).one (CycRing.cyc K (n + 1)).zero = _
    lib_ring
  rw [SymG.pw_add, ← Frobenius.pw_mul, hsq, SymG.one_pw]
  exact SymB1.G_one K n gl hgl

variable (hℓ : ∀ d, Dv d (n + 1 + 1) → d = 1 ∨ d = n + 1 + 1)

include hgl hℓ in
/-- **G(φ_u(B)^M) = G(Y)** for the Stickelberger product B (ℓ − 1 = 2h), Y = φ_u(α)^M, Y^ℓ = 1. -/
theorem G_Bsym (h : Nat) (hh : n + 1 = h + h) (α : V Int (n + 1 + 1)) (M : Nat)
    (hY : pw (CycRing.cyc K (n + 1)) (pw (CycRing.cyc K (n + 1)) (CycEis.phz (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1)) α) M) (n + 1 + 1)
      = (CycRing.cyc K (n + 1)).one) :
    SymG.G K n gl (pw (CycRing.cyc K (n + 1))
        (CycEis.phz (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1))
          (AbsPair.Bp (gring LibInt.intR (n + 1)) (StickBB.ents (n + 1) α (StickQs.qsL n 0 n h)))) M)
      = SymG.G K n gl (pw (CycRing.cyc K (n + 1)) (CycEis.phz (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1)) α) M) := by
  rw [PhzPw.phz_Bp (CycRing.cyc K (n + 1)) (n + 1) _ (CycUnit.u_ell K (n + 1))]
  rw [SymB1.G_evB K n gl hgl hℓ α M hY _ (SymB2.nz_qsL n h 0 n (SymB2.h_le n h hh) (Nat.zero_add n))]
  have hG : pw (CycRing.cyc K (n + 1)) (SymG.G K n gl (pw (CycRing.cyc K (n + 1)) (CycEis.phz (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1)) α) M))
      (n + 1 + 1) = (CycRing.cyc K (n + 1)).one := by
    rw [← SymB1.G_pw K n gl hgl, hY]
    exact SymB1.G_one K n gl hgl
  rw [← GEval.pw_md (CycRing.cyc K (n + 1)) (n + 1) _ hG]
  have e1 : md (n + 1) (SymB1.expo n (StickQs.qsL n 0 n h)) = 1 :=
    (ZMod.nsm_one_val (n + 1) _).symm.trans ((congrArg Subtype.val (SymB2.expo_one n hℓ h hh)).trans
      (md_small (n + 1) 1 (Nat.succ_lt_succ (Nat.zero_lt_succ n))))
  rw [e1]
  show (CycRing.cyc K (n + 1)).mul (CycRing.cyc K (n + 1)).one _ = _
  exact (CycRing.cyc K (n + 1)).one_mul _

end EisAlpha

namespace EisAlpha

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob
open LibRing (castZ castN)

/-- **Eisenstein's reciprocity for α (global-symbol form)**: A ≡ ±σ^(j₀)B (mod N) and φ_u(A)^M = u^T give
(u^(j₀M))^(ℓ−1)·G(φ_u(α)^M) = (u^T)^(ℓ−1) in E = 𝔽_q[C_ℓ]/(N). -/
theorem eis_alpha (j Mp ms t h' : Nat) (hM : Mp + 1 = (j + 1 + 1 + 1) ^ (ms + 1 + 1))
    (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (g : Zp (j + 1 + 1)) (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (hℓ : ∀ d, Dv d (ms + 1 + 1 + 1 + 1) → d = 1 ∨ d = ms + 1 + 1 + 1 + 1)
    (hN : j + 1 + 1 = (ms + 1 + 1 + 1 + 1) * (t + 1)) (hh : ms + 1 + 1 + 1 = (h' + 1) + (h' + 1))
    (α : V Int (ms + 1 + 1 + 1 + 1)) (iv : Zp (ms + 1 + 1 + 1) → Zp (ms + 1 + 1 + 1))
    (hNα : NormEval.nrm (ms + 1 + 1 + 1) α
        (BbarVal.nlist (ms + 1 + 1 + 1) iv (StickQs.qsL (ms + 1 + 1) 0 (ms + 1 + 1) (h' + 1)))
      = nsm (gring LibInt.intR (ms + 1 + 1 + 1)) (j + 1 + 1 + 1) (gring LibInt.intR (ms + 1 + 1 + 1)).one)
    (h0 : RedP2.red (j + 1) Mp (ConjUnit.Y Mp (ms + 1 + 1 + 1) (TeichN.psiN (j + 1) Mp (ms + 1) (t + 1) g) α
        (ring (ms + 1 + 1 + 1)).one) = (ring (j + 1 + 1)).zero)
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (e : Nat)
    (hQ : nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (M : Nat) (hqn : (k' + 1 + 1) ^ e = (ms + 1 + 1 + 1 + 1) * M + 1)
    (hp : nsm (ring (k' + 1)) (j + 1 + 1 + 1) (ring (k' + 1)).one ≠ (ring (k' + 1)).zero) (s : Nat) (hs : M = (k' + 1) * s)
    (k : Nat → Nat)
    (hk : ∀ c, 1 ≤ c → c < ms + 1 + 1 + 1 + 1 →
      pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g (t + 1)) c) (k c)
        = pw (ring (j + 1 + 1))
            (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one) (j + 1)) (t + 1))
    (gl : Zp (ms + 1 + 1 + 1)) (hgl : OrdMul.Ord (ring (ms + 1 + 1 + 1)) gl (ms + 1 + 1 + 1))
    (hc0 : nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one ≠ (ring (ms + 1 + 1 + 1)).zero)
    (M' : Nat) (hM' : M = M' + M') :
    ∃ jj : Zp (ms + 1 + 1 + 1),
      (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)).mul
          (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
            (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) (jj.val * M)) (ms + 1 + 1 + 1))
          (SymG.G (ring (k' + 1)) (ms + 1 + 1) gl
            (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
              (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) α) M))
        = pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
            (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1))
              (SymProdW.T k (EisRecV.vS (ms + 1 + 1)) (ms + 1 + 1 + 1))) (ms + 1 + 1 + 1) := by
  obtain ⟨jj, ε, hε, hmod⟩ := StickInst.stick_inst j Mp ms t h' hM hpr g hg hℓ hN hh α iv hNα h0
  refine ⟨jj, ?_⟩
  have hU := unit_alpha k' hq (ms + 1 + 1) (j + 1 + 1 + 1) hp α iv h' hNα
  have hA := EisRecA.eis_recip_A j g hpr hg (ms + 1) t hℓ hN k' hq e hQ M hqn hp s hs k (EisRecV.vS (ms + 1 + 1)) hk
    (EisRecV.vS_spec (ms + 1 + 1))
  have hA' : pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
      (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1))
        (StickElem.A j g (ms + 1))) M
      = pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1))
          (SymProdW.T k (EisRecV.vS (ms + 1 + 1)) (ms + 1 + 1 + 1)) := hA
  have hAB := PhzModN.phz_A_B (ring (k' + 1)) (ms + 1 + 1) hℓ _ _ jj ε hmod
  -- Y^ℓ = 1
  have hcd : (ring (ms + 1 + 1 + 1)).mul (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one)
      (pw (ring (ms + 1 + 1 + 1)) (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one) (ms + 1 + 1))
      = (ring (ms + 1 + 1 + 1)).one := by
    have hf := NthEis.fermat_unit (ms + 1 + 1) hℓ _ hc0
    have h2 : (ring (ms + 1 + 1 + 1)).mul (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one)
        (pw (ring (ms + 1 + 1 + 1)) (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one) (ms + 1 + 1))
        = pw (ring (ms + 1 + 1 + 1)) (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one) (ms + 1 + 1 + 1) := by
      show _ = (ring (ms + 1 + 1 + 1)).mul (pw (ring (ms + 1 + 1 + 1)) (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one) (ms + 1 + 1))
        (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one)
      lib_ring
    exact h2.trans hf
  have hY := SymY.Y_ell (ms + 1 + 1 + 1) k' hq _ _ hcd rfl e (q_order (ms + 1 + 1) (k' + 1 + 1) e M hqn) M hqn _ hU
  -- the three factors
  have hC1 : SymG.G (ring (k' + 1)) (ms + 1 + 1) gl (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
      (castZ (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) ε) M) = (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)).one := by
    rw [hM']
    exact G_eps (ring (k' + 1)) (ms + 1 + 1) gl hgl ε hε M'
  have hU1 : SymG.G (ring (k' + 1)) (ms + 1 + 1) gl (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
      (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) jj.val) M)
      = pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
          (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) (jj.val * M)) (ms + 1 + 1 + 1) := by
    rw [TwoPower.pwpow]
    exact SymG.G_u (ring (k' + 1)) (ms + 1 + 1) gl hgl.1 (jj.val * M)
  have hB1 := G_Bsym (ring (k' + 1)) (ms + 1 + 1) gl hgl hℓ (h' + 1) hh α M hY
  have hGA : SymG.G (ring (k' + 1)) (ms + 1 + 1) gl (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
      (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1))
        (StickElem.A j g (ms + 1))) M)
      = pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
          (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1))
            (SymProdW.T k (EisRecV.vS (ms + 1 + 1)) (ms + 1 + 1 + 1))) (ms + 1 + 1 + 1) := by
    rw [hA']
    exact SymG.G_u (ring (k' + 1)) (ms + 1 + 1) gl hgl.1 _
  rw [hAB, Frobenius.pw_mul, Frobenius.pw_mul, SymG.G_mul (ring (k' + 1)) (ms + 1 + 1) gl hgl.1,
    SymG.G_mul (ring (k' + 1)) (ms + 1 + 1) gl hgl.1, hC1, hU1] at hGA
  have hB2 : SymG.G (ring (k' + 1)) (ms + 1 + 1) gl (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
      (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1))
        (StickElem.B (ms + 1) h' α)) M)
      = SymG.G (ring (k' + 1)) (ms + 1 + 1) gl (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
          (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) α) M) := hB1
  rw [hB2] at hGA
  rw [← hGA]
  lib_ring

end EisAlpha

#print axioms EisAlpha.nsm_pow
#print axioms EisAlpha.q_order
#print axioms EisAlpha.unit_alpha
#print axioms EisAlpha.G_eps
#print axioms EisAlpha.G_Bsym
#print axioms EisAlpha.eis_alpha

lang/EisAlphaFin.lean

lean_all/lang/EisAlphaFin.lean

/-
  lang/EisAlphaFin — T49 (general ℓ ≥ 5): **Eisenstein's reciprocity for a primary prime element α of norm p**,
  with the parity hypotheses of lang/EisAlphaK1 derived from primality: p − 1 = ℓ(t + 1) has t + 1 even (p odd),
  and M = (q^e − 1)/ℓ is even (q odd).
    odd_ne_even    a + a + 1 ≠ b + b
    parity         every n is a + a or a + a + 1
    odd_mul        (2c + 1)(2a + 1) is odd
    odd_pow        (2b + 1)^e is odd
    t_even         t + 1 is even
    M_even         M is even (q ≠ 2)
    eis_alpha_fin  G(φ_u(α)^M) = (u^(k_1))^(ℓ−1)
  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.EisAlphaK1
import lib.Semi

namespace EisAlphaFin

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

theorem odd_ne_even : ∀ a b : Nat, a + a + 1 ≠ b + b
  | 0, 0, h => Nat.noConfusion h
  | 0, b + 1, h => by
    have h' : 0 + 0 + 1 = b + b + 1 + 1 := h.trans (by lib_nat)
    exact Nat.noConfusion (Nat.succ.inj h')
  | a + 1, 0, h => Nat.noConfusion h
  | a + 1, b + 1, h => by
    have h' : a + a + 1 + 1 + 1 = b + b + 1 + 1 := (show a + a + 1 + 1 + 1 = a + 1 + (a + 1) + 1 by lib_nat).trans
      (h.trans (by lib_nat))
    exact odd_ne_even a b (Nat.succ.inj (Nat.succ.inj h'))

theorem parity : ∀ n : Nat, (∃ a, n = a + a) ∨ (∃ a, n = a + a + 1)
  | 0 => Or.inl ⟨0, rfl⟩
  | n + 1 => by
    cases parity n with
    | inl h =>
      obtain ⟨a, ha⟩ := h
      exact Or.inr ⟨a, by rw [ha]⟩
    | inr h =>
      obtain ⟨a, ha⟩ := h
      exact Or.inl ⟨a + 1, by rw [ha]; lib_nat⟩

theorem odd_mul (c a : Nat) : (c + c + 1) * (a + a + 1) = (2 * c * a + c + a) + (2 * c * a + c + a) + 1 := by lib_nat

theorem odd_pow (b : Nat) : ∀ e, ∃ c, (b + b + 1) ^ e = c + c + 1
  | 0 => ⟨0, rfl⟩
  | e + 1 => by
    obtain ⟨c, hc⟩ := odd_pow b e
    refine ⟨2 * c * b + c + b, ?_⟩
    show (b + b + 1) ^ e * (b + b + 1) = _
    rw [hc]
    exact odd_mul c b

/-- a prime P ≠ 2 is odd. -/
theorem prime_odd (P : Nat) (hP : ∀ d, Dv d P → d = 1 ∨ d = P) (h2 : P ≠ 2) : ∃ b, P = b + b + 1 := by
  cases parity P with
  | inr h => exact h
  | inl h =>
    obtain ⟨a, ha⟩ := h
    cases hP 2 ⟨a, by rw [ha]; lib_nat⟩ with
    | inl h1 => exact absurd h1 (fun e => Nat.noConfusion (Nat.succ.inj e))
    | inr h1 => exact absurd h1.symm h2

/-- **t + 1 is even**: p = j + 3 prime, p − 1 = ℓ(t + 1), ℓ = 2(h' + 1) + 1. -/
theorem t_even (j ms t h' : Nat) (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (hN : j + 1 + 1 = (ms + 1 + 1 + 1 + 1) * (t + 1)) (hh : ms + 1 + 1 + 1 = (h' + 1) + (h' + 1)) :
    ∃ a, t + 1 = 2 * a := by
  cases parity (t + 1) with
  | inl h =>
    obtain ⟨a, ha⟩ := h
    exact ⟨a, by rw [ha]; lib_nat⟩
  | inr h =>
    obtain ⟨a, ha⟩ := h
    exfalso
    have hl : ms + 1 + 1 + 1 + 1 = (h' + 1) + (h' + 1) + 1 := by rw [← hh]
    have e : j + 1 + 1 = (2 * (h' + 1) * a + (h' + 1) + a) + (2 * (h' + 1) * a + (h' + 1) + a) + 1 := by
      rw [hN, hl, ha]; exact odd_mul (h' + 1) a
    have hodd := prime_odd (j + 1 + 1 + 1) hpr (fun e2 => Nat.noConfusion (Nat.succ.inj (Nat.succ.inj e2)))
    obtain ⟨b, hb⟩ := hodd
    apply odd_ne_even b (2 * (h' + 1) * a + (h' + 1) + a + 1)
    rw [← hb, e]
    lib_nat

/-- **M is even**: q = k' + 2 an odd prime, q^e = ℓM + 1, ℓ odd. -/
theorem M_even (ms h' k' e M : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (hq2 : k' + 1 + 1 ≠ 2)
    (hh : ms + 1 + 1 + 1 = (h' + 1) + (h' + 1)) (hqn : (k' + 1 + 1) ^ e = (ms + 1 + 1 + 1 + 1) * M + 1) :
    ∃ M', M = M' + M' := by
  cases parity M with
  | inl h => exact h
  | inr h =>
    obtain ⟨a, ha⟩ := h
    exfalso
    obtain ⟨b, hb⟩ := prime_odd (k' + 1 + 1) hq hq2
    obtain ⟨c, hc⟩ := odd_pow b e
    have hl : ms + 1 + 1 + 1 + 1 = (h' + 1) + (h' + 1) + 1 := by rw [← hh]
    apply odd_ne_even c (2 * (h' + 1) * a + (h' + 1) + a + 1)
    rw [← hc, ← hb, hqn, hl, ha, odd_mul (h' + 1) a]
    lib_nat

/-- **Eisenstein's reciprocity for a primary prime element α of norm p**: G(φ_u(α)^M) = (u^(k_1))^(ℓ−1),
u^(k_1) = (q^(−e)/α)_ℓ; q an odd prime, ℓ ≥ 5, p ≡ 1 (mod ℓ). -/
theorem eis_alpha_fin (j Mp ms t h' : Nat) (hM : Mp + 1 = (j + 1 + 1 + 1) ^ (ms + 1 + 1))
    (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (g : Zp (j + 1 + 1)) (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (hℓ : ∀ d, Dv d (ms + 1 + 1 + 1 + 1) → d = 1 ∨ d = ms + 1 + 1 + 1 + 1)
    (hN : j + 1 + 1 = (ms + 1 + 1 + 1 + 1) * (t + 1)) (hh : ms + 1 + 1 + 1 = (h' + 1) + (h' + 1))
    (α : V Int (ms + 1 + 1 + 1 + 1)) (iv : Zp (ms + 1 + 1 + 1) → Zp (ms + 1 + 1 + 1))
    (hNα : NormEval.nrm (ms + 1 + 1 + 1) α
        (BbarVal.nlist (ms + 1 + 1 + 1) iv (StickQs.qsL (ms + 1 + 1) 0 (ms + 1 + 1) (h' + 1)))
      = nsm (gring LibInt.intR (ms + 1 + 1 + 1)) (j + 1 + 1 + 1) (gring LibInt.intR (ms + 1 + 1 + 1)).one)
    (h0 : RedP2.red (j + 1) Mp (ConjUnit.Y Mp (ms + 1 + 1 + 1) (TeichN.psiN (j + 1) Mp (ms + 1) (t + 1) g) α
        (ring (ms + 1 + 1 + 1)).one) = (ring (j + 1 + 1)).zero)
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (hq2 : k' + 1 + 1 ≠ 2) (e : Nat)
    (hQ : nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (M : Nat) (hqn : (k' + 1 + 1) ^ e = (ms + 1 + 1 + 1 + 1) * M + 1)
    (hp : nsm (ring (k' + 1)) (j + 1 + 1 + 1) (ring (k' + 1)).one ≠ (ring (k' + 1)).zero) (s : Nat) (hs : M = (k' + 1) * s)
    (k : Nat → Nat)
    (hk : ∀ c, 1 ≤ c → c < ms + 1 + 1 + 1 + 1 →
      pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g (t + 1)) c) (k c)
        = pw (ring (j + 1 + 1))
            (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one) (j + 1)) (t + 1))
    (gl : Zp (ms + 1 + 1 + 1)) (hgl : OrdMul.Ord (ring (ms + 1 + 1 + 1)) gl (ms + 1 + 1 + 1))
    (hc0 : nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one ≠ (ring (ms + 1 + 1 + 1)).zero)
    (hprim : PrimAlpha1.Prim (ms + 1 + 1) α) :
    SymG.G (ring (k' + 1)) (ms + 1 + 1) gl
        (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
          (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) α) M)
      = pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
          (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) (1 * k 1)) (ms + 1 + 1 + 1) := by
  obtain ⟨h2, ht⟩ := t_even j ms t h' hpr hN hh
  obtain ⟨M', hM'⟩ := M_even ms h' k' e M hq hq2 hh hqn
  exact EisAlphaK1.eis_alpha_k1 j Mp ms t h' hM hpr g hg hℓ hN hh α iv hNα h0 k' hq e hQ M hqn hp s hs k hk gl hgl hc0
    M' hM' hprim h2 ht

end EisAlphaFin

#print axioms EisAlphaFin.odd_ne_even
#print axioms EisAlphaFin.parity
#print axioms EisAlphaFin.odd_mul
#print axioms EisAlphaFin.odd_pow
#print axioms EisAlphaFin.prime_odd
#print axioms EisAlphaFin.t_even
#print axioms EisAlphaFin.M_even
#print axioms EisAlphaFin.eis_alpha_fin

lang/EisAlphaK1.lean

lean_all/lang/EisAlphaK1.lean

/-
  lang/EisAlphaK1 — T49 (general ℓ ≥ 5): **Eisenstein's reciprocity for a primary prime element α of norm p,
  with the P_1 symbol on the right**: α ∈ P_1, so the Stickelberger-weighted product of the q-symbols at the P_c
  collapses to the q-symbol at P_1 (lang/SymProdZ):
    G(φ_u(α)^M) = (u^(k_1))^(ℓ−1)       in E = 𝔽_q[C_ℓ]/(N),
  where u^(k_1) is (q^(−e)/P_1)_ℓ = (q^(−e)/α)_ℓ (z^(k_1) = (q^(−e))^((p−1)/ℓ)) and G(φ_u(α)^M) is the product over
  all primes above q of (α/𝔮)_ℓ (lang/SymG; work/data/t49/globsym_test.py).
    eis_alpha_k1
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.EisAlphaP

namespace EisAlphaK1

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

/-- **(α/q) = (q^(−e)/α)** in global-symbol form, α primary of norm p in P_1. -/
theorem eis_alpha_k1 (j Mp ms t h' : Nat) (hM : Mp + 1 = (j + 1 + 1 + 1) ^ (ms + 1 + 1))
    (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (g : Zp (j + 1 + 1)) (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (hℓ : ∀ d, Dv d (ms + 1 + 1 + 1 + 1) → d = 1 ∨ d = ms + 1 + 1 + 1 + 1)
    (hN : j + 1 + 1 = (ms + 1 + 1 + 1 + 1) * (t + 1)) (hh : ms + 1 + 1 + 1 = (h' + 1) + (h' + 1))
    (α : V Int (ms + 1 + 1 + 1 + 1)) (iv : Zp (ms + 1 + 1 + 1) → Zp (ms + 1 + 1 + 1))
    (hNα : NormEval.nrm (ms + 1 + 1 + 1) α
        (BbarVal.nlist (ms + 1 + 1 + 1) iv (StickQs.qsL (ms + 1 + 1) 0 (ms + 1 + 1) (h' + 1)))
      = nsm (gring LibInt.intR (ms + 1 + 1 + 1)) (j + 1 + 1 + 1) (gring LibInt.intR (ms + 1 + 1 + 1)).one)
    (h0 : RedP2.red (j + 1) Mp (ConjUnit.Y Mp (ms + 1 + 1 + 1) (TeichN.psiN (j + 1) Mp (ms + 1) (t + 1) g) α
        (ring (ms + 1 + 1 + 1)).one) = (ring (j + 1 + 1)).zero)
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (e : Nat)
    (hQ : nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (M : Nat) (hqn : (k' + 1 + 1) ^ e = (ms + 1 + 1 + 1 + 1) * M + 1)
    (hp : nsm (ring (k' + 1)) (j + 1 + 1 + 1) (ring (k' + 1)).one ≠ (ring (k' + 1)).zero) (s : Nat) (hs : M = (k' + 1) * s)
    (k : Nat → Nat)
    (hk : ∀ c, 1 ≤ c → c < ms + 1 + 1 + 1 + 1 →
      pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g (t + 1)) c) (k c)
        = pw (ring (j + 1 + 1))
            (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one) (j + 1)) (t + 1))
    (gl : Zp (ms + 1 + 1 + 1)) (hgl : OrdMul.Ord (ring (ms + 1 + 1 + 1)) gl (ms + 1 + 1 + 1))
    (hc0 : nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one ≠ (ring (ms + 1 + 1 + 1)).zero)
    (M' : Nat) (hM' : M = M' + M') (hprim : PrimAlpha1.Prim (ms + 1 + 1) α) (h2 : Nat) (ht : t + 1 = 2 * h2) :
          (SymG.G (ring (k' + 1)) (ms + 1 + 1) gl
            (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
              (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) α) M))
        = pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
            (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) (1 * k 1)) (ms + 1 + 1 + 1) := by
  rw [EisAlphaP.eis_alpha_prim j Mp ms t h' hM hpr g hg hℓ hN hh α iv hNα h0 k' hq e hQ M hqn hp s hs k hk gl hgl hc0 M' hM' hprim h2 ht]
  have hj : pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g (t + 1)) (1 * k 1)
      = pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one) (j + 1)) (t + 1) :=
    (SymProdZ.pw_pw (ring (j + 1 + 1)) _ 1 (k 1)).symm.trans (hk 1 (Nat.le_refl 1) (Nat.succ_lt_succ (Nat.succ_pos _)))
  rw [SymProdZ.sym_prod_z (ms + 1) hℓ (ring (j + 1 + 1)) _ _ (SymPsi.z_ord (j + 1) (ms + 1) t g hg hN) (1 * k 1) hj k
    (EisRecV.vS (ms + 1 + 1)) hk (EisRecV.vS_spec (ms + 1 + 1)) (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
    (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) (CycUnit.u_ell (ring (k' + 1)) (ms + 1 + 1 + 1))]

end EisAlphaK1

#print axioms EisAlphaK1.eis_alpha_k1

lang/EisAlphaMul.lean

lean_all/lang/EisAlphaMul.lean

/-
  lang/EisAlphaMul — T49 (general ℓ): **the global symbol is multiplicative in α**, so Eisenstein's reciprocity
  for primary prime elements of norm p (lang/EisAlphaK1) extends to their products:
    G_sym_mul    G(φ_u(αβ)^M) = G(φ_u(α)^M)·G(φ_u(β)^M)
    prodV        Π of a list of group-ring elements
    G_sym_rec    if each G(φ_u(α_i)^M) = (u^(k_i))^(ℓ−1) then G(φ_u(Π α_i)^M) = (u^(Σ k_i))^(ℓ−1)
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.SymB1
import lib.RingTac
import lib.Semi

namespace EisAlphaMul

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

variable {R : Type} (K : CRing R) (n : Nat) (gl : Zp (n + 1)) (hgl : OrdMul.Ord (ring (n + 1)) gl (n + 1))

include hgl in
/-- **G(φ_u(αβ)^M) = G(φ_u(α)^M)·G(φ_u(β)^M)**. -/
theorem G_sym_mul (α β : V Int (n + 1 + 1)) (M : Nat) :
    SymG.G K n gl (pw (CycRing.cyc K (n + 1))
        (CycEis.phz (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1)) ((gring LibInt.intR (n + 1)).mul α β)) M)
      = (CycRing.cyc K (n + 1)).mul
          (SymG.G K n gl (pw (CycRing.cyc K (n + 1)) (CycEis.phz (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1)) α) M))
          (SymG.G K n gl (pw (CycRing.cyc K (n + 1)) (CycEis.phz (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1)) β) M)) := by
  rw [CycEis.phz_mul (CycRing.cyc K (n + 1)) (n + 1) _ (CycUnit.u_ell K (n + 1)), Frobenius.pw_mul, SymG.G_mul K n gl hgl.1]

/-- Π of a list of group-ring elements. -/
def prodV : List (V Int (n + 1 + 1)) → V Int (n + 1 + 1)
  | [] => (gring LibInt.intR (n + 1)).one
  | a :: as => (gring LibInt.intR (n + 1)).mul a (prodV as)

include hgl in
/-- **G(φ_u(Π α_i)^M) = Π_i G(φ_u(α_i)^M)** (as a fold). -/
theorem G_sym_rec (M : Nat) (k : V Int (n + 1 + 1) → Nat) : ∀ αs : List (V Int (n + 1 + 1)),
    (∀ a, a ∈ αs → SymG.G K n gl (pw (CycRing.cyc K (n + 1)) (CycEis.phz (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1)) a) M)
      = pw (CycRing.cyc K (n + 1)) (pw (CycRing.cyc K (n + 1)) (CycUnit.u K (n + 1)) (k a)) (n + 1)) →
    SymG.G K n gl (pw (CycRing.cyc K (n + 1)) (CycEis.phz (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1)) (prodV n αs)) M)
      = pw (CycRing.cyc K (n + 1)) (pw (CycRing.cyc K (n + 1)) (CycUnit.u K (n + 1)) ((αs.map k).foldr (· + ·) 0)) (n + 1)
  | [], _ => by
    show SymG.G K n gl (pw (CycRing.cyc K (n + 1)) (CycEis.phz (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1))
        (gring LibInt.intR (n + 1)).one) M) = pw (CycRing.cyc K (n + 1)) (CycRing.cyc K (n + 1)).one (n + 1)
    rw [CycEis.phz_one, SymG.one_pw, SymG.one_pw]
    exact SymB1.G_one K n gl hgl
  | a :: as, h => by
    show SymG.G K n gl (pw (CycRing.cyc K (n + 1))
        (CycEis.phz (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1)) ((gring LibInt.intR (n + 1)).mul a (prodV n as))) M)
      = pw (CycRing.cyc K (n + 1)) (pw (CycRing.cyc K (n + 1)) (CycUnit.u K (n + 1)) (k a + (as.map k).foldr (· + ·) 0)) (n + 1)
    rw [G_sym_mul K n gl hgl, h a (List.Mem.head _), G_sym_rec M k as (fun b hb => h b (List.Mem.tail _ hb))]
    rw [SymG.pw_add (CycRing.cyc K (n + 1)) (CycUnit.u K (n + 1)) (k a) ((as.map k).foldr (· + ·) 0),
      Frobenius.pw_mul (CycRing.cyc K (n + 1)) (pw (CycRing.cyc K (n + 1)) (CycUnit.u K (n + 1)) (k a))
        (pw (CycRing.cyc K (n + 1)) (CycUnit.u K (n + 1)) ((as.map k).foldr (· + ·) 0)) (n + 1)]

end EisAlphaMul

#print axioms EisAlphaMul.G_sym_mul
#print axioms EisAlphaMul.G_sym_rec

lang/EisAlphaP.lean

lean_all/lang/EisAlphaP.lean

/-
  lang/EisAlphaP — T49 (general ℓ ≥ 5): **Eisenstein's reciprocity for a primary prime element α of norm p**.
  For α primary (α ≡ rational integer mod (1 − ζ)², i.e. Σ_i i·α(i) ≡ 0 mod ℓ), the root of unity in A ≡ ±σ^(j₀)B is ±1
  (lang/PrimAlpha2), so the relation of lang/EisAlpha becomes
    G(φ_u(α)^M) = (u^T)^(ℓ−1),   T = Σ_c k_c (ℓ − 1 − c)        in E = 𝔽_q[C_ℓ]/(N),
  where u^(k_c) is the symbol of q^(−e) at P_c and G is the global-symbol operator (lang/SymG).
    eis_alpha_of     the relation of EisAlpha for a given congruence A ≡ ε σ^(j₀) B (mod N)
    eis_alpha_prim   the primary case: j₀ = 0
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.EisAlpha
import lang.PrimAlpha2
import lib.RingTac
import lib.Semi

namespace EisAlphaP

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob
open LibRing (castZ castN)
open EisAlpha

/-- **the reciprocity relation from a given congruence A ≡ ε σ^(j₀) B (mod N)**. -/
theorem eis_alpha_of (j ms t h' : Nat)
    (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (g : Zp (j + 1 + 1)) (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (hℓ : ∀ d, Dv d (ms + 1 + 1 + 1 + 1) → d = 1 ∨ d = ms + 1 + 1 + 1 + 1)
    (hN : j + 1 + 1 = (ms + 1 + 1 + 1 + 1) * (t + 1)) (hh : ms + 1 + 1 + 1 = (h' + 1) + (h' + 1))
    (α : V Int (ms + 1 + 1 + 1 + 1)) (iv : Zp (ms + 1 + 1 + 1) → Zp (ms + 1 + 1 + 1))
    (hNα : NormEval.nrm (ms + 1 + 1 + 1) α
        (BbarVal.nlist (ms + 1 + 1 + 1) iv (StickQs.qsL (ms + 1 + 1) 0 (ms + 1 + 1) (h' + 1)))
      = nsm (gring LibInt.intR (ms + 1 + 1 + 1)) (j + 1 + 1 + 1) (gring LibInt.intR (ms + 1 + 1 + 1)).one)
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (e : Nat)
    (hQ : nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (M : Nat) (hqn : (k' + 1 + 1) ^ e = (ms + 1 + 1 + 1 + 1) * M + 1)
    (hp : nsm (ring (k' + 1)) (j + 1 + 1 + 1) (ring (k' + 1)).one ≠ (ring (k' + 1)).zero) (s : Nat) (hs : M = (k' + 1) * s)
    (k : Nat → Nat)
    (hk : ∀ c, 1 ≤ c → c < ms + 1 + 1 + 1 + 1 →
      pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g (t + 1)) c) (k c)
        = pw (ring (j + 1 + 1))
            (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one) (j + 1)) (t + 1))
    (gl : Zp (ms + 1 + 1 + 1)) (hgl : OrdMul.Ord (ring (ms + 1 + 1 + 1)) gl (ms + 1 + 1 + 1))
    (hc0 : nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one ≠ (ring (ms + 1 + 1 + 1)).zero)
    (M' : Nat) (hM' : M = M' + M') (jj : Zp (ms + 1 + 1 + 1)) (ε : Int) (hε : ε = 1 ∨ ε = -1)
    (hmod : ModN.ModN (ms + 1 + 1 + 1) (StickElem.A j g (ms + 1))
      (gmul LibInt.intR (ms + 1 + 1 + 1) (mono LibInt.intR (ms + 1 + 1 + 1) jj ε) (StickElem.B (ms + 1) h' α))) :
      (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)).mul
          (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
            (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) (jj.val * M)) (ms + 1 + 1 + 1))
          (SymG.G (ring (k' + 1)) (ms + 1 + 1) gl
            (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
              (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) α) M))
        = pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
            (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1))
              (SymProdW.T k (EisRecV.vS (ms + 1 + 1)) (ms + 1 + 1 + 1))) (ms + 1 + 1 + 1) := by
  have hU := unit_alpha k' hq (ms + 1 + 1) (j + 1 + 1 + 1) hp α iv h' hNα
  have hA := EisRecA.eis_recip_A j g hpr hg (ms + 1) t hℓ hN k' hq e hQ M hqn hp s hs k (EisRecV.vS (ms + 1 + 1)) hk
    (EisRecV.vS_spec (ms + 1 + 1))
  have hA' : pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
      (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1))
        (StickElem.A j g (ms + 1))) M
      = pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1))
          (SymProdW.T k (EisRecV.vS (ms + 1 + 1)) (ms + 1 + 1 + 1)) := hA
  have hAB := PhzModN.phz_A_B (ring (k' + 1)) (ms + 1 + 1) hℓ _ _ jj ε hmod
  -- Y^ℓ = 1
  have hcd : (ring (ms + 1 + 1 + 1)).mul (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one)
      (pw (ring (ms + 1 + 1 + 1)) (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one) (ms + 1 + 1))
      = (ring (ms + 1 + 1 + 1)).one := by
    have hf := NthEis.fermat_unit (ms + 1 + 1) hℓ _ hc0
    have h2 : (ring (ms + 1 + 1 + 1)).mul (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one)
        (pw (ring (ms + 1 + 1 + 1)) (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one) (ms + 1 + 1))
        = pw (ring (ms + 1 + 1 + 1)) (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one) (ms + 1 + 1 + 1) := by
      show _ = (ring (ms + 1 + 1 + 1)).mul (pw (ring (ms + 1 + 1 + 1)) (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one) (ms + 1 + 1))
        (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one)
      lib_ring
    exact h2.trans hf
  have hY := SymY.Y_ell (ms + 1 + 1 + 1) k' hq _ _ hcd rfl e (q_order (ms + 1 + 1) (k' + 1 + 1) e M hqn) M hqn _ hU
  -- the three factors
  have hC1 : SymG.G (ring (k' + 1)) (ms + 1 + 1) gl (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
      (castZ (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) ε) M) = (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)).one := by
    rw [hM']
    exact G_eps (ring (k' + 1)) (ms + 1 + 1) gl hgl ε hε M'
  have hU1 : SymG.G (ring (k' + 1)) (ms + 1 + 1) gl (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
      (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) jj.val) M)
      = pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
          (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) (jj.val * M)) (ms + 1 + 1 + 1) := by
    rw [TwoPower.pwpow]
    exact SymG.G_u (ring (k' + 1)) (ms + 1 + 1) gl hgl.1 (jj.val * M)
  have hB1 := G_Bsym (ring (k' + 1)) (ms + 1 + 1) gl hgl hℓ (h' + 1) hh α M hY
  have hGA : SymG.G (ring (k' + 1)) (ms + 1 + 1) gl (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
      (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1))
        (StickElem.A j g (ms + 1))) M)
      = pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
          (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1))
            (SymProdW.T k (EisRecV.vS (ms + 1 + 1)) (ms + 1 + 1 + 1))) (ms + 1 + 1 + 1) := by
    rw [hA']
    exact SymG.G_u (ring (k' + 1)) (ms + 1 + 1) gl hgl.1 _
  rw [hAB, Frobenius.pw_mul, Frobenius.pw_mul, SymG.G_mul (ring (k' + 1)) (ms + 1 + 1) gl hgl.1,
    SymG.G_mul (ring (k' + 1)) (ms + 1 + 1) gl hgl.1, hC1, hU1] at hGA
  have hB2 : SymG.G (ring (k' + 1)) (ms + 1 + 1) gl (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
      (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1))
        (StickElem.B (ms + 1) h' α)) M)
      = SymG.G (ring (k' + 1)) (ms + 1 + 1) gl (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
          (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) α) M) := hB1
  rw [hB2] at hGA
  rw [← hGA]
  lib_ring

/-- **Eisenstein's reciprocity for a primary prime element α of norm p (global-symbol form)**:
G(φ_u(α)^M) = (u^T)^(ℓ−1) in E = 𝔽_q[C_ℓ]/(N), T = Σ_c k_c(ℓ−1−c). -/
theorem eis_alpha_prim (j Mp ms t h' : Nat) (hM : Mp + 1 = (j + 1 + 1 + 1) ^ (ms + 1 + 1))
    (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (g : Zp (j + 1 + 1)) (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (hℓ : ∀ d, Dv d (ms + 1 + 1 + 1 + 1) → d = 1 ∨ d = ms + 1 + 1 + 1 + 1)
    (hN : j + 1 + 1 = (ms + 1 + 1 + 1 + 1) * (t + 1)) (hh : ms + 1 + 1 + 1 = (h' + 1) + (h' + 1))
    (α : V Int (ms + 1 + 1 + 1 + 1)) (iv : Zp (ms + 1 + 1 + 1) → Zp (ms + 1 + 1 + 1))
    (hNα : NormEval.nrm (ms + 1 + 1 + 1) α
        (BbarVal.nlist (ms + 1 + 1 + 1) iv (StickQs.qsL (ms + 1 + 1) 0 (ms + 1 + 1) (h' + 1)))
      = nsm (gring LibInt.intR (ms + 1 + 1 + 1)) (j + 1 + 1 + 1) (gring LibInt.intR (ms + 1 + 1 + 1)).one)
    (h0 : RedP2.red (j + 1) Mp (ConjUnit.Y Mp (ms + 1 + 1 + 1) (TeichN.psiN (j + 1) Mp (ms + 1) (t + 1) g) α
        (ring (ms + 1 + 1 + 1)).one) = (ring (j + 1 + 1)).zero)
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (e : Nat)
    (hQ : nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (M : Nat) (hqn : (k' + 1 + 1) ^ e = (ms + 1 + 1 + 1 + 1) * M + 1)
    (hp : nsm (ring (k' + 1)) (j + 1 + 1 + 1) (ring (k' + 1)).one ≠ (ring (k' + 1)).zero) (s : Nat) (hs : M = (k' + 1) * s)
    (k : Nat → Nat)
    (hk : ∀ c, 1 ≤ c → c < ms + 1 + 1 + 1 + 1 →
      pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g (t + 1)) c) (k c)
        = pw (ring (j + 1 + 1))
            (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one) (j + 1)) (t + 1))
    (gl : Zp (ms + 1 + 1 + 1)) (hgl : OrdMul.Ord (ring (ms + 1 + 1 + 1)) gl (ms + 1 + 1 + 1))
    (hc0 : nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one ≠ (ring (ms + 1 + 1 + 1)).zero)
    (M' : Nat) (hM' : M = M' + M') (hprim : PrimAlpha1.Prim (ms + 1 + 1) α) (h2 : Nat) (ht : t + 1 = 2 * h2) :
          (SymG.G (ring (k' + 1)) (ms + 1 + 1) gl
            (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
              (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) α) M))
        = pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
            (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1))
              (SymProdW.T k (EisRecV.vS (ms + 1 + 1)) (ms + 1 + 1 + 1))) (ms + 1 + 1 + 1) := by
  obtain ⟨jj, ε, hε, hmod⟩ := StickInst.stick_inst j Mp ms t h' hM hpr g hg hℓ hN hh α iv hNα h0
  have hrel := eis_alpha_of j ms t h' hpr g hg hℓ hN hh α iv hNα k' hq e hQ M hqn hp s hs k hk gl hgl hc0 M' hM' jj ε hε hmod
  have hN2 : j + 1 + 1 = (ms + 1 + 1 + 1 + 1) * (2 * h2) := by rw [hN, ht]
  have hk2 : ms + 1 + 1 = 2 * h' + 1 :=
    Nat.succ.inj (hh.trans (show h' + 1 + (h' + 1) = 2 * h' + 1 + 1 by lib_nat))
  have hA := PrimAlpha2.A_val (ms + 1 + 1) j g hpr hg h2 hN2 (ms + 1 + 1) h' hk2
  have hB : (CycEis.phz (PrimAlpha1.D (ms + 1 + 1)) (ms + 1 + 1 + 1) (PrimAlpha1.z (ms + 1 + 1))
      (AbsPair.Bp (gring LibInt.intR (ms + 1 + 1 + 1)) (StickBB.ents (ms + 1 + 1 + 1) α (StickQs.qsL (ms + 1 + 1) 0 (ms + 1 + 1) (h' + 1))))).2
      = (ring (ms + 1 + 1 + 1)).zero := by
    rw [PhzPw.phz_Bp (PrimAlpha1.D (ms + 1 + 1)) (ms + 1 + 1 + 1) _ (PrimAlpha1.z_ell (ms + 1 + 1))]
    exact PrimAlpha2.B_snd (ms + 1 + 1) hℓ α hprim _
      (SymB2.nz_qsL (ms + 1 + 1) (h' + 1) 0 (ms + 1 + 1) (SymB2.h_le (ms + 1 + 1) (h' + 1) hh) (Nat.zero_add _))
  have hj := PrimAlpha2.j0_zero (ms + 1 + 1) (h' + 1) hh _ _ hA hB jj ε hmod
  have hv : jj.val = 0 := by
    rw [hj]
    exact md_small (ms + 1 + 1 + 1) 0 (Nat.zero_lt_succ _)
  rw [hv, show 0 * M = 0 by lib_nat] at hrel
  rw [← hrel]
  show _ = (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)).mul
    (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)).one (ms + 1 + 1 + 1)) _
  rw [SymG.one_pw]
  exact ((CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)).one_mul _).symm

end EisAlphaP

#print axioms EisAlphaP.eis_alpha_of
#print axioms EisAlphaP.eis_alpha_prim

lang/EisBridge.lean

lean_all/lang/EisBridge.lean

/-
  lang/EisBridge — T49 (general ℓ): **Eisenstein's relation for a primary element**, every odd ℓ.
  Let χ : 𝔽_p → R be a character with values in any ring R (e.g. ℤ[C_ℓ] or ℤ[ζ_ℓ]) and A = ∏_(i ≤ k) J(χ^i, χ)·χ(ι(−1)).
  For ring maps φ₁ : R → Q (ζ ↦ 1 + π, π² = 0, ℓ·1 = 0) and φ₂ : R → E (characteristic q) through which χ becomes the
  characters of PrimA and NthEis:
    jpr_congr     pointwise equal characters have equal Jacobi products (no function extensionality)
    a_primary     φ₁(A)·p = −1, i.e. A·p ≡ −1 (mod (1 − ζ)²)                          (PrimA, k = ℓ − 2)
    a_eis         ψ(q^(−e)) = φ₂(A)^m when q^e = ℓm + 1, (q − 1) | m, p ≢ 0 (mod q)  (NthEis)
  so one element A of R is primary and satisfies χ_p(q)^(−e) ≡ A^((q^e − 1)/ℓ) (mod q).
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.JprHom
import lang.NthEis
import lang.PrimA

namespace EisBridge

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob CharSumGen

theorem j2_congr {S : Type} (E : CRing S) (j : Nat) (χ χ' μ μ' : Zp (j + 1 + 1) → S) (hχ : ∀ x, χ x = χ' x)
    (hμ : ∀ x, μ x = μ' x) : J2 E j χ μ = J2 E j χ' μ' :=
  JprHom.sumK_congr E _ _ (fun t => by
    show E.mul (χ t) (μ ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg t))) =
      E.mul (χ' t) (μ' ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg t)))
    rw [hχ, hμ]) (LL j)

/-- **pointwise equal characters have equal Jacobi products**. -/
theorem jpr_congr {S : Type} (E : CRing S) (j : Nat) (ψ₁ ψ₂ : Zp (j + 1 + 1) → S) (h : ∀ x, ψ₁ x = ψ₂ x) :
    ∀ k, jpr E j ψ₁ k = jpr E j ψ₂ k
  | 0 => rfl
  | k + 1 => by
    show E.mul (jpr E j ψ₁ k) (J2 E j (psp E j ψ₁ (k + 1)) ψ₁) = E.mul (jpr E j ψ₂ k) (J2 E j (psp E j ψ₂ (k + 1)) ψ₂)
    rw [jpr_congr E j ψ₁ ψ₂ h k,
      j2_congr E j (psp E j ψ₁ (k + 1)) (psp E j ψ₂ (k + 1)) ψ₁ ψ₂
        (fun x => by show pw E (ψ₁ x) (k + 1) = pw E (ψ₂ x) (k + 1); rw [h x]) h]

/-- the element A = ∏_(i ≤ k) J(χ^i, χ)·χ(ι(−1)) of R. -/
def AA {R : Type} (T : CRing R) (j : Nat) (χ : Zp (j + 1 + 1) → R) (k : Nat) : R :=
  T.mul (jpr T j χ k) (χ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))

/-- the image of A under a ring map is A of the image character. -/
theorem aa_map {R S : Type} (T : CRing R) (E : CRing S) (φ : R → S) (hadd : ∀ a b, φ (T.add a b) = E.add (φ a) (φ b))
    (hzero : φ T.zero = E.zero) (hmul : ∀ a b, φ (T.mul a b) = E.mul (φ a) (φ b)) (hone : φ T.one = E.one) (j : Nat)
    (χ : Zp (j + 1 + 1) → R) (ψ : Zp (j + 1 + 1) → S) (hχ : ∀ x, φ (χ x) = ψ x) (k : Nat) :
    φ (AA T j χ k) = AA E j ψ k := by
  show φ (T.mul (jpr T j χ k) (χ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) =
    E.mul (jpr E j ψ k) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))
  rw [JprHom.a_map T E φ j hadd hzero hmul hone χ k, jpr_congr E j _ ψ hχ k, hχ]

/-- **A is primary**: φ₁(A)·p = −1 in Q, i.e. A·p ≡ −1 (mod (1 − ζ)²), ℓ = 2r + 3. -/
theorem a_primary {R Q : Type} (T : CRing R) (K : CRing Q) (φ : R → Q) (hadd : ∀ a b, φ (T.add a b) = K.add (φ a) (φ b))
    (hzero : φ T.zero = K.zero) (hmul : ∀ a b, φ (T.mul a b) = K.mul (φ a) (φ b)) (hone : φ T.one = K.one)
    (π : Q) (hπ : K.mul π π = K.zero) (j : Nat) (g : Zp (j + 1 + 1)) (χ : Zp (j + 1 + 1) → R)
    (hχ : ∀ x, φ (χ x) = PrimJC.u K π (j + 1) g x)
    (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1)) (l h : Nat) (hl : nsm K l K.one = K.zero)
    (hN : j + 1 + 1 = l * (2 * h)) (r : Nat) :
    K.mul (φ (AA T j χ (2 * r + 1))) (Pp K j) = K.neg K.one := by
  rw [aa_map T K φ hadd hzero hmul hone j χ _ hχ (2 * r + 1)]
  exact PrimA.gauss_prim K π hπ j g hpr hg l h hl hN r

/-- **Eisenstein's relation for A**: ψ(q^(−e)) = φ₂(A)^m. -/
theorem a_eis {R S : Type} (T : CRing R) (E : CRing S) (φ : R → S) (hadd : ∀ a b, φ (T.add a b) = E.add (φ a) (φ b))
    (hzero : φ T.zero = E.zero) (hmul : ∀ a b, φ (T.mul a b) = E.mul (φ a) (φ b)) (hone : φ T.one = E.one)
    (j : Nat) (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (χ : Zp (j + 1 + 1) → R)
    (ψ : Zp (j + 1 + 1) → S) (hχ : ∀ x, φ (χ x) = ψ x)
    (hm : ∀ x y, ψ ((ring (j + 1 + 1)).mul x y) = E.mul (ψ x) (ψ y)) (h1 : ψ (ring (j + 1 + 1)).one = E.one)
    (h0 : ψ (ring (j + 1 + 1)).zero = E.zero) (hs : sumK E ψ (LL j) = E.zero) (k : Nat)
    (hnt : ∀ i, i < k → sumK E (fun a => pw E (ψ a) (i + 2)) (LL j) = E.zero) (hinv : ∀ x, pw E (ψ x) (k + 1) = ψ (iv j x))
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (hE : nsm E (k' + 1 + 1) E.one = E.zero) (e : Nat)
    (hfix : ∀ a, pw E (ψ a) ((k' + 1 + 1) ^ e) = ψ a)
    (hQ : nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (m : Nat) (hqn : (k' + 1 + 1) ^ e = (k + 2) * m + 1)
    (ι : Zp (k' + 1) → S) (hιa : ∀ a b, ι ((ring (k' + 1)).add a b) = E.add (ι a) (ι b))
    (hιm : ∀ a b, ι ((ring (k' + 1)).mul a b) = E.mul (ι a) (ι b)) (hι1 : ι (ring (k' + 1)).one = E.one)
    (hι0 : ι (ring (k' + 1)).zero = E.zero)
    (hp : nsm (ring (k' + 1)) (j + 1 + 1 + 1) (ring (k' + 1)).one ≠ (ring (k' + 1)).zero) (t : Nat) (ht : m = (k' + 1) * t) :
    ψ (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one) (j + 1)) =
      pw E (φ (AA T j χ k)) m := by
  rw [aa_map T E φ hadd hzero hmul hone j χ ψ hχ k]
  exact NthEis.nth_eis E j hpr ψ hm h1 h0 hs k hnt hinv k' hq hE e hfix hQ m hqn ι hιa hιm hι1 hι0 hp t ht

end EisBridge

#print axioms EisBridge.j2_congr
#print axioms EisBridge.jpr_congr
#print axioms EisBridge.aa_map
#print axioms EisBridge.a_primary
#print axioms EisBridge.a_eis

lang/EisCoprime.lean

lean_all/lang/EisCoprime.lean

/-
  lang/EisCoprime — T50, cycle 612: splitting by coprime norms in ℤ[ω].

  If N(γ) = mn with sm + tn = 1 (m, n ≥ 1), then γ = αβ with N(α) = m, N(β) = n (N(a + bω) = a² − ab + b²):
  α = gcd(γ, m), e = gcd(γ, n) (EisEuclid.egcd_exists), αe | γ by Euclid's lemma, γ | αe since mn = γγ̄, so γ = αe·v with v a unit;
  then N(α) | m, N(e) | n and N(α)N(e) = mn give N(α) = m. (Port of GaussCoprime.coprime_split.)
    en_emb, emb_mul, en_dvd, emb_add, en_zero_imp, cancel_left, en_nonneg_one, ecoprime_split
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.EisEuclid
import lang.GaussCoprime

namespace EisCoprime

open FTAChain DetChain LibInt EisEuclid

def emb (m : Int) : Int × Int := (m, 0)

theorem en_emb (m : Int) : en (emb m) = m * m := by show m * m + -(m * 0) + 0 * 0 = m * m; lib_int

theorem emb_mul (a b : Int) : (Cyclo3.cyc3 LibInt.intR).mul (emb a) (emb b) = emb (a * b) := by
  apply Prod.ext
  · show a * b + -(0 * 0) = a * b; lib_int
  · show a * 0 + 0 * b + -(0 * 0) = 0; lib_int

theorem en_dvd (d γ : Int × Int) (h : Dvd' d γ) : ∃ k, en γ = en d * k :=
  match h with
  | ⟨c, hc⟩ => ⟨en c, by rw [hc, en_mul]⟩

theorem emb_add (a b : Int) : emb (a + b) = (Cyclo3.cyc3 LibInt.intR).add (emb a) (emb b) := by
  apply Prod.ext
  · rfl
  · show (0 : Int) = 0 + 0; rfl

theorem en_zero_imp (z : Int × Int) (h : en z = 0) : z = (Cyclo3.cyc3 LibInt.intR).zero := by
  apply nrm_zero
  apply Int.ofNat.inj
  rw [nrm_cast]; exact h

theorem cancel_left (γ z : Int × Int) (hγ : en γ ≠ 0) (h : (Cyclo3.cyc3 LibInt.intR).mul γ z = (Cyclo3.cyc3 LibInt.intR).zero) : z = (Cyclo3.cyc3 LibInt.intR).zero := by
  have : en γ * en z = 0 := by rw [← en_mul, h]; rfl
  cases int_noZD _ _ this with
  | inl e => exact absurd e hγ
  | inr e => exact en_zero_imp z e

theorem en_nonneg_one (z : Int × Int) (h : en z = -1) : False := by
  rw [← nrm_cast] at h
  exact Int.noConfusion h

/-- **Coprime splitting**: N(γ) = mn, sm + tn = 1 ⇒ γ = αβ with N(α) = m, N(β) = n. -/
theorem ecoprime_split (γ : Int × Int) (m n : Nat) (s t : Int) (hγ : en γ = Int.ofNat ((m + 1) * (n + 1)))
    (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1) :
    ∃ α β, γ = (Cyclo3.cyc3 LibInt.intR).mul α β ∧ en α = Int.ofNat (m + 1) ∧ en β = Int.ofNat (n + 1) := by
  match egcd_exists _ γ (emb (Int.ofNat (m + 1))) (Nat.le_refl _), egcd_exists _ γ (emb (Int.ofNat (n + 1))) (Nat.le_refl _) with
  | ⟨d, x, y, hd, ⟨u, hu⟩, ⟨c1, hc1⟩⟩, ⟨e, x', y', he, ⟨u', hu'⟩, ⟨c2, hc2⟩⟩ =>
    have hco : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (emb t) c2) e) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (emb s) c1) d) = (Cyclo3.cyc3 LibInt.intR).one := by
      have h1 : (Cyclo3.cyc3 LibInt.intR).one = emb (s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1)) := by rw [hst]; rfl
      rw [h1, emb_add, ← emb_mul, ← emb_mul, hc1, hc2]; lib_ring
    -- e | u, so γ = d e v
    match eeuclid e d u _ _ hco ⟨u', by rw [← hu, hu']⟩ with
    | ⟨v, hv⟩ =>
    have hγdev : γ = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul d e) v := by rw [hu, hv]; lib_ring
    -- γ | d e
    have hmn : (Cyclo3.cyc3 LibInt.intR).mul γ (cj γ) = emb (Int.ofNat (m + 1) * Int.ofNat (n + 1)) := by
      rw [mul_cj, hγ]; rfl
    have hW : (Cyclo3.cyc3 LibInt.intR).mul d e = (Cyclo3.cyc3 LibInt.intR).mul γ ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul x x') γ) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul x y') (emb (Int.ofNat (n + 1))))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).  -- (+120 chars; full line in the Zenodo source)
      rw [hd, he]
      have : (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul y y') ((Cyclo3.cyc3 LibInt.intR).mul γ (cj γ)) = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul y y') ((Cyclo3.cyc3 LibInt.intR).mul (emb (Int.ofNat (m + 1))) (emb (Int.ofNat (n + 1)))) := by rw [hmn, emb_mul]
      calc (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul x γ) ((Cyclo3.cyc3 LibInt.intR).mul y (emb (Int.ofNat (m + 1))))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul x' γ) ((Cyclo3.cyc3 LibInt.intR).mul y' (emb (Int.ofNat (n + 1)))))
          = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul γ ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul x x') γ) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul x y') (emb (Int.ofNat (n + 1))))) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul y x') (emb (Int.ofNat (m + 1))))))
              ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul y y') ((Cyclo3.cyc3 LibInt.intR).mul (emb (Int.ofNat (m + 1))) (emb (Int.ofNat (n + 1))))) := by lib_ring
        _ = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul γ ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul x x') γ) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul x y') (emb (Int.ofNat (n + 1))))) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul y x') (emb (Int.ofNat (m + 1))))))
              ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul y y') ((Cyclo3.cyc3 LibInt.intR).mul γ (cj γ))) := by rw [this]
        _ = _ := by lib_ring
    generalize (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul x x') γ) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul x y') (emb (Int.ofNat (n + 1))))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul y x') (emb (Int.ofNat (m + 1)))) ((Cyclo3.cyc3 LibInt.intR).mul   -- (+55 chars; full line in the Zenodo source)
    have hγ0 : en γ ≠ 0 := by rw [hγ]; exact fun e => Nat.noConfusion (Int.ofNat.inj e)
    have hWv : (Cyclo3.cyc3 LibInt.intR).mul W v = (Cyclo3.cyc3 LibInt.intR).one := by
      have h0 : (Cyclo3.cyc3 LibInt.intR).mul γ ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul W v) ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one)) = (Cyclo3.cyc3 LibInt.intR).zero := by
        rw [show (Cyclo3.cyc3 LibInt.intR).mul γ ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul W v) ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one)) = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul γ W) v) ((Cyclo3.cyc3 LibInt.intR).neg γ) by lib_ring, ← hW, ← hγdev]; lib_ring
      have := cancel_left γ _ hγ0 h0
      calc (Cyclo3.cyc3 LibInt.intR).mul W v = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul W v) ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one)) (Cyclo3.cyc3 LibInt.intR).one := by lib_ring
        _ = (Cyclo3.cyc3 LibInt.intR).one := by rw [this]; lib_ring
    have hv1 : en v = 1 := by
      have : en W * en v = 1 := by rw [← en_mul, hWv]; rfl
      cases GaussCoprime.int_unit _ _ (by rw [show en v * en W = en W * en v by lib_int]; exact this) with
      | inl h => exact h
      | inr h => exact absurd h (en_nonneg_one v)
    -- norms
    have hN : en d * en e = Int.ofNat (m + 1) * Int.ofNat (n + 1) := by
      have := congrArg en hγdev
      rw [en_mul, en_mul, hv1, hγ] at this
      rw [show en d * en e = en d * en e * 1 by lib_int, ← this]; rfl
    have hdm : ∃ a, Int.ofNat (m + 1) = en d * a := by
      have k1 : Int.ofNat (m + 1) * Int.ofNat (m + 1) = en d * en c1 := by rw [← en_emb, hc1, en_mul]
      have k2 : Int.ofNat (m + 1) * Int.ofNat (n + 1) = en d * en u := by show Int.ofNat ((m + 1) * (n + 1)) = _; rw [← hγ, hu, en_mul]
      exact ⟨s * en c1 + t * en u, by
        calc Int.ofNat (m + 1) = Int.ofNat (m + 1) * (s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1)) := by rw [hst]; lib_int
          _ = s * (Int.ofNat (m + 1) * Int.ofNat (m + 1)) + t * (Int.ofNat (m + 1) * Int.ofNat (n + 1)) := by lib_int
          _ = en d * (s * en c1 + t * en u) := by rw [k1, k2]; lib_int⟩
    have hen' : ∃ b, Int.ofNat (n + 1) = en e * b := by
      have k1 : Int.ofNat (n + 1) * Int.ofNat (n + 1) = en e * en c2 := by rw [← en_emb, hc2, en_mul]
      have k2 : Int.ofNat (m + 1) * Int.ofNat (n + 1) = en e * en u' := by show Int.ofNat ((m + 1) * (n + 1)) = _; rw [← hγ, hu', en_mul]
      exact ⟨t * en c2 + s * en u', by
        calc Int.ofNat (n + 1) = Int.ofNat (n + 1) * (s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1)) := by rw [hst]; lib_int
          _ = t * (Int.ofNat (n + 1) * Int.ofNat (n + 1)) + s * (Int.ofNat (m + 1) * Int.ofNat (n + 1)) := by lib_int
          _ = en e * (t * en c2 + s * en u') := by rw [k1, k2]; lib_int⟩
    match hdm, hen' with
    | ⟨a, ha⟩, ⟨b, hb⟩ =>
      have hab : a * b = 1 := by
        have h0 : (en d * en e) * (a * b + -1) = 0 := by
          rw [show (en d * en e) * (a * b + -1) = (en d * a) * (en e * b) + -(en d * en e) by lib_int, ← ha, ← hb, hN]; lib_int
        cases int_noZD _ _ h0 with
        | inl e0 => exact absurd (by rw [hN] at e0; exact e0) (fun e1 : Int.ofNat (m + 1) * Int.ofNat (n + 1) = 0 => Nat.noConfusion (Int.ofNat.inj e1))
        | inr e0 => calc a * b = (a * b + -1) + 1 := by lib_int
            _ = 1 := by rw [e0]; rfl
      have pos : ∀ (k : Nat) (z : Int × Int) (c : Int), Int.ofNat (k + 1) = en z * c → c = 1 ∨ c = -1 → c = 1 := fun k z c h hc => by
        cases hc with
        | inl h1 => exact h1
        | inr h1 =>
          exfalso
          rw [h1, ← nrm_cast] at h
          have : Int.ofNat (k + 1) + Int.ofNat (nrm z) = 0 := by rw [h]; lib_int
          have h2 : k + nrm z + 1 = 0 := by rw [show k + nrm z + 1 = k + 1 + nrm z by lib_nat]; exact Int.ofNat.inj (show Int.ofNat (k + 1 + nrm z) = Int.ofNat 0 from this)
          exact Nat.noConfusion h2
      have a1 := pos m d a ha (GaussCoprime.int_unit a b hab)
      have b1 := pos n e b hb (GaussCoprime.int_unit b a (by rw [show b * a = a * b by lib_int]; exact hab))
      refine ⟨d, (Cyclo3.cyc3 LibInt.intR).mul e v, by rw [hγdev]; lib_ring, by rw [ha, a1]; lib_int, by rw [en_mul, hv1, hb, b1]⟩

end EisCoprime

#print axioms EisCoprime.en_emb
#print axioms EisCoprime.emb_mul
#print axioms EisCoprime.en_dvd
#print axioms EisCoprime.emb_add
#print axioms EisCoprime.en_zero_imp
#print axioms EisCoprime.cancel_left
#print axioms EisCoprime.en_nonneg_one
#print axioms EisCoprime.ecoprime_split

lang/EisEuclid.lean

lean_all/lang/EisEuclid.lean

/-
  lang/EisEuclid — T50, cycle 611: ℤ[ω] is Euclidean: division with remainder, gcd as a combination, and Euclid's lemma.

  ℤ[ω] = Cyclo3.cyc3 ℤ (pairs a + bω, ω² = −1 − ω), N(a + bω) = a² − ab + b², conjugate (a − b) − bω:
    en_rep         N(z) = c ∈ ℕ with c ≤ A² + B² + AB   (A = |a|, B = |b|; sign cases, no natAbs_mul)
    nrm_cast       (nrm z : ℤ) = N(z);   en_mul   N(xy) = N(x)N(y)
    edivr          β ≠ 0:  α = qβ + ρ with nrm ρ < nrm β   (round αβ̄ / N(β) componentwise; 4N(r) ≤ 3N(β)²)
    egcd_exists    d = xα + yβ dividing α and β
    eeuclid        xα + yβ = 1 and α | βγ ⇒ α | γ
  The ℤ[ω] analogue of GaussEuclid: the input for unique splitting by coprime norms and the multiplicativity of the
  Hecke theta series of ℚ(√−3).
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Cyclo3
import lang.GaussEuclid

namespace EisEuclid

open FTAChain DetChain LibInt


def en (z : Int × Int) : Int := z.1 * z.1 + -(z.1 * z.2) + z.2 * z.2
def nrm (z : Int × Int) : Nat := Int.natAbs (en z)
def cj (z : Int × Int) : Int × Int := (z.1 + -z.2, -z.2)
def Dvd' (a b : Int × Int) : Prop := ∃ c, b = (Cyclo3.cyc3 LibInt.intR).mul a c

theorem sgn (x : Int) : x = Int.ofNat (Int.natAbs x) ∨ x = -Int.ofNat (Int.natAbs x) := by
  cases x with
  | ofNat n => exact Or.inl rfl
  | negSucc n => exact Or.inr rfl

theorem le_dest (a b : Nat) (h : a ≤ b) : ∃ c, c + a = b := by
  induction h with
  | refl => exact ⟨0, Nat.zero_add a⟩
  | step _ ih =>
    obtain ⟨c, hc⟩ := ih
    exact ⟨c + 1, by rw [← hc]; lib_nat⟩

theorem ab_le (A B : Nat) : A * B ≤ A * A + B * B := by
  cases Nat.le_total A B with
  | inl h => exact Nat.le_trans (Nat.mul_le_mul_right B h) (Nat.le_add_left _ _)
  | inr h => exact Nat.le_trans (Nat.mul_le_mul_left A h) (Nat.le_add_right _ _)

theorem en_rep (z : Int × Int) : ∃ c : Nat, en z = Int.ofNat c ∧
    c ≤ Int.natAbs z.1 * Int.natAbs z.1 + Int.natAbs z.2 * Int.natAbs z.2 + Int.natAbs z.1 * Int.natAbs z.2 := by
  generalize hA : Int.natAbs z.1 = A
  generalize hB : Int.natAbs z.2 = B
  obtain ⟨c, hc⟩ := le_dest _ _ (ab_le A B)
  have e : Int.ofNat c + Int.ofNat A * Int.ofNat B = Int.ofNat A * Int.ofNat A + Int.ofNat B * Int.ofNat B := congrArg Int.ofNat hc
  have same : en z = Int.ofNat A * Int.ofNat A + Int.ofNat B * Int.ofNat B + -(Int.ofNat A * Int.ofNat B) → ∃ c : Nat, en z = Int.ofNat c ∧ c ≤ A * A + B * B + A * B :=
    fun h => ⟨c, by rw [h, ← e]; lib_int, Nat.le_trans (Nat.le_trans (Nat.le_add_right c (A * B)) (Nat.le_of_eq hc)) (Nat.le_add_right _ _)⟩
  have opp : en z = Int.ofNat A * Int.ofNat A + Int.ofNat B * Int.ofNat B + Int.ofNat A * Int.ofNat B → ∃ c : Nat, en z = Int.ofNat c ∧ c ≤ A * A + B * B + A * B :=
    fun h => ⟨A * A + B * B + A * B, h, Nat.le_refl _⟩
  unfold en
  rcases sgn z.1 with h1 | h1 <;> rcases sgn z.2 with h2 | h2 <;> rw [hA] at h1 <;> rw [hB] at h2
  · exact same (by unfold en; rw [h1, h2]; lib_int)
  · exact opp (by unfold en; rw [h1, h2]; lib_int)
  · exact opp (by unfold en; rw [h1, h2]; lib_int)
  · exact same (by unfold en; rw [h1, h2]; lib_int)

theorem nrm_cast (z : Int × Int) : Int.ofNat (nrm z) = en z := by
  obtain ⟨c, hc, _⟩ := en_rep z
  unfold nrm; rw [hc]; rfl

theorem nrm_le (z : Int × Int) : nrm z ≤ Int.natAbs z.1 * Int.natAbs z.1 + Int.natAbs z.2 * Int.natAbs z.2 + Int.natAbs z.1 * Int.natAbs z.2 := by
  obtain ⟨c, hc, hle⟩ := en_rep z
  unfold nrm; rw [hc]; exact hle

theorem mul_cj (z : Int × Int) : (Cyclo3.cyc3 LibInt.intR).mul z (cj z) = (en z, 0) := by
  apply Prod.ext
  · show z.1 * (z.1 + -z.2) + -(z.2 * -z.2) = z.1 * z.1 + -(z.1 * z.2) + z.2 * z.2
    lib_int
  · show z.1 * -z.2 + z.2 * (z.1 + -z.2) + -(z.2 * -z.2) = 0
    lib_int

theorem en_mul (x y : Int × Int) : en ((Cyclo3.cyc3 LibInt.intR).mul x y) = en x * en y := by
  show (x.1 * y.1 + -(x.2 * y.2)) * (x.1 * y.1 + -(x.2 * y.2)) + -((x.1 * y.1 + -(x.2 * y.2)) * (x.1 * y.2 + x.2 * y.1 + -(x.2 * y.2)))
      + (x.1 * y.2 + x.2 * y.1 + -(x.2 * y.2)) * (x.1 * y.2 + x.2 * y.1 + -(x.2 * y.2)) =
    (x.1 * x.1 + -(x.1 * x.2) + x.2 * x.2) * (y.1 * y.1 + -(y.1 * y.2) + y.2 * y.2)
  lib_int

theorem en_cj (z : Int × Int) : en (cj z) = en z := by
  show (z.1 + -z.2) * (z.1 + -z.2) + -((z.1 + -z.2) * -z.2) + -z.2 * -z.2 = z.1 * z.1 + -(z.1 * z.2) + z.2 * z.2
  lib_int

/-- **Division with remainder in ℤ[ω].** -/
theorem edivr (α β : Int × Int) (M : Nat) (hβ : nrm β = M + 1) :
    ∃ q, nrm ((Cyclo3.cyc3 LibInt.intR).add α ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).mul q β))) < M + 1 := by
  have hN : en β = Int.ofNat (M + 1) := by rw [← nrm_cast, hβ]
  match GaussEuclid.rnd ((Cyclo3.cyc3 LibInt.intR).mul α (cj β)).1 M, GaussEuclid.rnd ((Cyclo3.cyc3 LibInt.intR).mul α (cj β)).2 M with
  | ⟨q1, r1, h1, b1⟩, ⟨q2, r2, h2, b2⟩ =>
    refine ⟨(q1, q2), ?_⟩
    generalize hρ : (Cyclo3.cyc3 LibInt.intR).add α ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).mul (q1, q2) β)) = ρ
    have hρc : (Cyclo3.cyc3 LibInt.intR).mul ρ (cj β) = (r1, r2) := by
      rw [← hρ]
      have e : (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).add α ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).mul (q1, q2) β))) (cj β) = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul α (cj β)) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).mul (q1, q2) ((Cyclo3.cyc3 LibInt.intR).mul β (cj β)))) := by lib_ring
      rw [e, mul_cj, hN]
      apply Prod.ext
      · show ((Cyclo3.cyc3 LibInt.intR).mul α (cj β)).1 + -(q1 * Int.ofNat (M + 1) + -(q2 * 0)) = r1
        rw [h1]; lib_int
      · show ((Cyclo3.cyc3 LibInt.intR).mul α (cj β)).2 + -(q1 * 0 + q2 * Int.ofNat (M + 1) + -(q2 * 0)) = r2
        rw [h2]; lib_int
    have hn : en ρ * Int.ofNat (M + 1) = en (r1, r2) := by
      rw [← hN, ← en_cj β, ← en_mul, hρc]
    have hnN : nrm ρ * (M + 1) = nrm (r1, r2) := by
      apply Int.ofNat.inj
      show Int.ofNat (nrm ρ) * Int.ofNat (M + 1) = Int.ofNat (nrm (r1, r2))
      rw [nrm_cast, nrm_cast]; exact hn
    have hb := nrm_le (r1, r2)
    generalize Int.natAbs r1 = A at hb b1
    generalize Int.natAbs r2 = B at hb b2
    have hle : (M + 1) * (4 * nrm ρ) ≤ (M + 1) * (3 * (M + 1)) := by
      calc (M + 1) * (4 * nrm ρ) = 4 * nrm (r1, r2) := by rw [← hnN]; lib_nat
        _ ≤ 4 * (A * A + B * B + A * B) := Nat.mul_le_mul_left 4 hb
        _ = (2 * A) * (2 * A) + (2 * B) * (2 * B) + (2 * A) * (2 * B) := by lib_nat
        _ ≤ (M + 1) * (M + 1) + (M + 1) * (M + 1) + (M + 1) * (M + 1) :=
          Nat.add_le_add (Nat.add_le_add (Nat.mul_le_mul b1 b1) (Nat.mul_le_mul b2 b2)) (Nat.mul_le_mul b1 b2)
        _ = (M + 1) * (3 * (M + 1)) := by lib_nat
    have h4 := Nat.le_of_mul_le_mul_left hle (Nat.zero_lt_succ M)
    cases Nat.lt_or_ge (nrm ρ) (M + 1) with
    | inl h => exact h
    | inr h =>
      exfalso
      have : 4 * (M + 1) ≤ 3 * (M + 1) := Nat.le_trans (Nat.mul_le_mul_left 4 h) h4
      exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (show 3 * (M + 1) < 4 * (M + 1) by
        rw [show 4 * (M + 1) = 3 * (M + 1) + (M + 1) by lib_nat]; exact Nat.lt_add_of_pos_right (Nat.zero_lt_succ _)) this)

theorem nrm_zero (z : Int × Int) (h : nrm z = 0) : z = (Cyclo3.cyc3 LibInt.intR).zero := by
  have hen : en z = 0 := by rw [← nrm_cast, h]; rfl
  have h4 : Int.ofNat (Int.natAbs (2 * z.1 + -z.2) * Int.natAbs (2 * z.1 + -z.2) + (Int.natAbs z.2 * Int.natAbs z.2 + (Int.natAbs z.2 * Int.natAbs z.2 + Int.natAbs z.2 * Int.natAbs z.2))) = Int.ofNat 0 := by
    show Int.ofNat (Int.natAbs (2 * z.1 + -z.2) * Int.natAbs (2 * z.1 + -z.2)) + (Int.ofNat (Int.natAbs z.2 * Int.natAbs z.2) + (Int.ofNat (Int.natAbs z.2 * Int.natAbs z.2) + Int.ofNat (Int.natAbs z.2 * Int.natAbs z.2))) = 0
    rw [GaussEuclid.nas, GaussEuclid.nas]
    calc _ = 4 * en z := by unfold en; lib_int
      _ = 0 := by rw [hen]; lib_int
  have h4' := Int.ofNat.inj h4
  have hB : Int.natAbs z.2 * Int.natAbs z.2 = 0 :=
    Nat.le_antisymm (Nat.le_trans (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_add_left _ _)) (Nat.le_of_eq h4')) (Nat.zero_le _)
  have b0 : z.2 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ hB with | inl e => exact e | inr e => exact e)
  have hA : Int.ofNat (Int.natAbs z.1 * Int.natAbs z.1) = Int.ofNat 0 := by
    rw [GaussEuclid.nas]
    calc z.1 * z.1 = en z := by unfold en; rw [b0]; lib_int
      _ = _ := hen
  have hA' := Int.ofNat.inj hA
  have a0 : z.1 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ hA' with | inl e => exact e | inr e => exact e)
  exact Prod.ext a0 b0

theorem egcd_base (α β : Int × Int) (hb : β = (Cyclo3.cyc3 LibInt.intR).zero) :
    ∃ d x y, d = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul x α) ((Cyclo3.cyc3 LibInt.intR).mul y β) ∧ Dvd' d α ∧ Dvd' d β :=
  ⟨α, (Cyclo3.cyc3 LibInt.intR).one, (Cyclo3.cyc3 LibInt.intR).zero, by rw [hb]; lib_ring, ⟨(Cyclo3.cyc3 LibInt.intR).one, by lib_ring⟩, ⟨(Cyclo3.cyc3 LibInt.intR).zero, by rw [hb]; lib_ring⟩⟩

/-- **gcd as a combination in ℤ[ω].** -/
theorem egcd_exists : ∀ G (α β : Int × Int), nrm β ≤ G →
    ∃ d x y, d = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul x α) ((Cyclo3.cyc3 LibInt.intR).mul y β) ∧ Dvd' d α ∧ Dvd' d β
  | 0, α, β, hG => egcd_base α β (nrm_zero β (Nat.le_antisymm hG (Nat.zero_le _)))
  | G + 1, α, β, hG => by
    cases h : nrm β with
    | zero => exact egcd_base α β (nrm_zero β h)
    | succ M =>
        match edivr α β M h with
        | ⟨q, hq⟩ =>
          have hρ : nrm ((Cyclo3.cyc3 LibInt.intR).add α ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).mul q β))) ≤ G :=
            Nat.le_of_lt_succ (Nat.lt_of_lt_of_le hq (Nat.le_trans (Nat.le_of_eq h.symm) hG))
          match egcd_exists G β _ hρ with
          | ⟨d, x, y, hd, ⟨c1, hc1⟩, ⟨c2, hc2⟩⟩ =>
            refine ⟨d, y, (Cyclo3.cyc3 LibInt.intR).add x ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).mul y q)), by rw [hd]; lib_ring, ⟨(Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul q c1) c2, ?_⟩, ⟨c1, hc1⟩⟩
            have e : α = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul q β) ((Cyclo3.cyc3 LibInt.intR).add α ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).mul q β))) := by lib_ring
            rw [e, hc2, hc1]
            lib_ring

/-- **Euclid's lemma in ℤ[ω].** -/
theorem eeuclid (α β γ x y : Int × Int) (h1 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul x α) ((Cyclo3.cyc3 LibInt.intR).mul y β) = (Cyclo3.cyc3 LibInt.intR).one) (h : Dvd' α ((Cyclo3.cyc3 LibInt.intR).mul β γ)) : Dvd' α γ :=
  match h with
  | ⟨c, hc⟩ => ⟨(Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul x γ) ((Cyclo3.cyc3 LibInt.intR).mul y c), by
      calc γ = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul x α) ((Cyclo3.cyc3 LibInt.intR).mul y β)) γ := by rw [h1]; lib_ring
        _ = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul x α) γ) ((Cyclo3.cyc3 LibInt.intR).mul y ((Cyclo3.cyc3 LibInt.intR).mul β γ)) := by lib_ring
        _ = (Cyclo3.cyc3 LibInt.intR).mul α ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul x γ) ((Cyclo3.cyc3 LibInt.intR).mul y c)) := by rw [hc]; lib_ring⟩

end EisEuclid

#print axioms EisEuclid.sgn
#print axioms EisEuclid.le_dest
#print axioms EisEuclid.ab_le
#print axioms EisEuclid.en_rep
#print axioms EisEuclid.nrm_cast
#print axioms EisEuclid.nrm_le
#print axioms EisEuclid.mul_cj
#print axioms EisEuclid.en_mul
#print axioms EisEuclid.en_cj
#print axioms EisEuclid.edivr
#print axioms EisEuclid.nrm_zero
#print axioms EisEuclid.egcd_base
#print axioms EisEuclid.egcd_exists
#print axioms EisEuclid.eeuclid

lang/EisFProd.lean

lean_all/lang/EisFProd.lean

/-
  lang/EisFProd — T49: **reciprocity for products in any target field**. For F without zero divisors of
  characteristic q and z ∈ F of order ℓ (e.g. the corner field of lang/EisFqAll, any residue degree), the computed
  symbols at ρ_z = evI add under products: if each factor α_i satisfies z^(Σ s(φ_u(α_i)^M)) = z^(c_i) and
  φ_u(α_i)^M is an ℓ-th root of unity (lang/EisFqY.y_ell_alpha when N(α_i) is prime to q), then
  z^(Σ s(φ_u(Π α_i)^M)) = z^(Σ c_i). (lang/EisFqMul is the case F = 𝔽_q.)
    eis_F_prod
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.EisFqF
import lang.EisFqMul

namespace EisFProd

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

/-- **Products, in any target field.** -/
theorem eis_F_prod (k' : Nat) (ms : Nat)
    (hℓ : ∀ d, Dv d (ms + 1 + 1 + 1 + 1) → d = 1 ∨ d = ms + 1 + 1 + 1 + 1)
    (gl : Zp (ms + 1 + 1 + 1)) (hgl : OrdMul.Ord (ring (ms + 1 + 1 + 1)) gl (ms + 1 + 1 + 1))
    {T : Type} (F : CRing T) (hF : NoZD F) (h10 : F.one ≠ F.zero) (dec : DecidableEq T)
    (hpF : nsm F (k' + 1 + 1) F.one = F.zero)
    (z : T) (hz : pw F z (ms + 1 + 1 + 1 + 1) = F.one) (hz1 : z ≠ F.one)
    (M : Nat) (c : V Int (ms + 1 + 1 + 1 + 1) → Nat) (αs : List (V Int (ms + 1 + 1 + 1 + 1)))
    (hY : ∀ a, a ∈ αs → pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
        (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
          (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) a) M)
        (ms + 1 + 1 + 1 + 1) = (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)).one)
    (hrec : ∀ a, a ∈ αs → pw F z
        (SymRho.sumN (EisRhoF.syF (ring (k' + 1)) (ms + 1 + 1) gl F
          (CycBase.evI (ring (k' + 1)) F (FpEmb.emb F (k' + 1)) (ms + 1 + 1 + 1) z) dec
          (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
            (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) a) M))
          (ms + 1 + 1 + 1))
        = pw F z (c a)) :
    pw F z
        (SymRho.sumN (EisRhoF.syF (ring (k' + 1)) (ms + 1 + 1) gl F
          (CycBase.evI (ring (k' + 1)) F (FpEmb.emb F (k' + 1)) (ms + 1 + 1 + 1) z) dec
          (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
            (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1))
              (EisAlphaMul.prodV (ms + 1 + 1) αs)) M))
          (ms + 1 + 1 + 1))
      = pw F z ((αs.map c).foldr (· + ·) 0) := by
  have hs : CycChar.gs F z (ms + 1 + 1 + 1 + 1) = F.zero := CycEis.gs_zero F hF z hz1 (ms + 1 + 1 + 1 + 1) hz
  have hadd := FpEmb.emb_add F (k' + 1) hpF
  have hmul := FpEmb.emb_mul F (k' + 1) hpF
  have hone := FpEmb.emb_one F (k' + 1) hpF
  have hu := CycBase.evI_u (ring (k' + 1)) F (FpEmb.emb F (k' + 1)) hadd hmul hone (ms + 1 + 1 + 1) z hz hs
  have hu1 : CycBase.evI (ring (k' + 1)) F (FpEmb.emb F (k' + 1)) (ms + 1 + 1 + 1) z (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1))
      ≠ F.one := by
    rw [hu]
    exact hz1
  rw [EisFqMul.phz_prodV (ring (k' + 1)) (ms + 1 + 1) M αs]
  have h := EisFqMul.rho_sum_list (ring (k' + 1)) (ms + 1 + 1) gl hgl.1 F
    (CycBase.evI (ring (k' + 1)) F (FpEmb.emb F (k' + 1)) (ms + 1 + 1 + 1) z)
    (CycBase.evI_mul (ring (k' + 1)) F (FpEmb.emb F (k' + 1)) hadd hmul (ms + 1 + 1 + 1) z hz hs)
    (CycBase.evI_one (ring (k' + 1)) F (FpEmb.emb F (k' + 1)) hadd hone (ms + 1 + 1 + 1) z hz hs)
    hF h10 dec hℓ hu1
    (fun a => pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
      (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) a) M)
    c αs hY (fun a ha => by rw [hu]; exact hrec a ha)
  rw [hu] at h
  exact h

end EisFProd

#print axioms EisFProd.eis_F_prod

lang/EisFq.lean

lean_all/lang/EisFq.lean

/-
  lang/EisFq — T49 (symbols at a prime, step 5): **Eisenstein's reciprocity at a prime of degree one above q**.
  For z ∈ 𝔽_q of order ℓ (so ℓ | q − 1), evaluation at z is a ring map ρ_z : 𝔽_q[C_ℓ]/(N) → 𝔽_q (CycEvalK), and
  ρ_z∘τ(g^i) runs over the primes above q. With the symbols s_i of φ_u(α)^M at these primes computed by a decidable
  search (EisRhoF, no choice), for a primary prime α of norm p
    z^(Σ_(i<ℓ−1) s_i) = z^(k_1 (ℓ−1)),      u^(k_1) = (q^(−e)/α)_ℓ.
    one_ne, decZ, gs_z, eis_fq
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.EisRhoF
import lang.CycEvalK

namespace EisFq

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

theorem one_ne (k : Nat) : (ring (k + 1)).one ≠ (ring (k + 1)).zero := fun e => by
  have e0 : md (k + 1) 1 = md (k + 1) 0 := congrArg Subtype.val e
  rw [md_small (k + 1) 1 (Nat.succ_lt_succ (Nat.zero_lt_succ k))] at e0
  exact Nat.noConfusion e0

/-- decidable equality on 𝔽_p. -/
def decZ (k : Nat) : DecidableEq (Zp k) := fun a b =>
  match Nat.decEq a.val b.val with
  | isTrue h => isTrue (Subtype.ext h)
  | isFalse h => isFalse (fun e => h (congrArg Subtype.val e))

/-- **Σ_(a<ℓ) z^a = 0** for z ≠ 1 of order dividing ℓ in 𝔽_q. -/
theorem gs_z (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (n : Nat) (z : Zp (k' + 1))
    (hz : pw (ring (k' + 1)) z (n + 1) = (ring (k' + 1)).one) (hz1 : z ≠ (ring (k' + 1)).one) : CycChar.gs (ring (k' + 1)) z (n + 1) = (ring (k' + 1)).zero :=
  CycEis.gs_zero (ring (k' + 1)) (nozd k' hq) z hz1 (n + 1) hz

/-- **Eisenstein's reciprocity at the primes of degree one above q**. -/
theorem eis_fq (j Mp ms t h' : Nat) (hM : Mp + 1 = (j + 1 + 1 + 1) ^ (ms + 1 + 1))
    (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (g : Zp (j + 1 + 1)) (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (hℓ : ∀ d, Dv d (ms + 1 + 1 + 1 + 1) → d = 1 ∨ d = ms + 1 + 1 + 1 + 1)
    (hN : j + 1 + 1 = (ms + 1 + 1 + 1 + 1) * (t + 1)) (hh : ms + 1 + 1 + 1 = (h' + 1) + (h' + 1))
    (α : V Int (ms + 1 + 1 + 1 + 1)) (iv : Zp (ms + 1 + 1 + 1) → Zp (ms + 1 + 1 + 1))
    (hNα : NormEval.nrm (ms + 1 + 1 + 1) α
        (BbarVal.nlist (ms + 1 + 1 + 1) iv (StickQs.qsL (ms + 1 + 1) 0 (ms + 1 + 1) (h' + 1)))
      = nsm (gring LibInt.intR (ms + 1 + 1 + 1)) (j + 1 + 1 + 1) (gring LibInt.intR (ms + 1 + 1 + 1)).one)
    (h0 : RedP2.red (j + 1) Mp (ConjUnit.Y Mp (ms + 1 + 1 + 1) (TeichN.psiN (j + 1) Mp (ms + 1) (t + 1) g) α
        (ring (ms + 1 + 1 + 1)).one) = (ring (j + 1 + 1)).zero)
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (hq2 : k' + 1 + 1 ≠ 2) (e : Nat)
    (hQ : nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (M : Nat) (hqn : (k' + 1 + 1) ^ e = (ms + 1 + 1 + 1 + 1) * M + 1)
    (hp : nsm (ring (k' + 1)) (j + 1 + 1 + 1) (ring (k' + 1)).one ≠ (ring (k' + 1)).zero) (s : Nat) (hs' : M = (k' + 1) * s)
    (k : Nat → Nat)
    (hk : ∀ c, 1 ≤ c → c < ms + 1 + 1 + 1 + 1 →
      pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g (t + 1)) c) (k c)
        = pw (ring (j + 1 + 1))
            (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one) (j + 1)) (t + 1))
    (gl : Zp (ms + 1 + 1 + 1)) (hgl : OrdMul.Ord (ring (ms + 1 + 1 + 1)) gl (ms + 1 + 1 + 1))
    (hc0 : nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one ≠ (ring (ms + 1 + 1 + 1)).zero)
    (hprim : PrimAlpha1.Prim (ms + 1 + 1) α)
    (z : Zp (k' + 1)) (hz : pw (ring (k' + 1)) z (ms + 1 + 1 + 1 + 1) = (ring (k' + 1)).one) (hz1 : z ≠ (ring (k' + 1)).one) :
    pw (ring (k' + 1)) z
        (SymRho.sumN (EisRhoF.syF (ring (k' + 1)) (ms + 1 + 1) gl (ring (k' + 1)) (CycEvalK.evK (ring (k' + 1)) (ms + 1 + 1 + 1) z) (decZ (k' + 1))
          (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
            (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) α) M))
          (ms + 1 + 1 + 1))
      = pw (ring (k' + 1)) z ((1 * k 1) * (ms + 1 + 1 + 1)) := by
  have hs := gs_z k' hq (ms + 1 + 1 + 1) z hz hz1
  have hu := CycEvalK.evK_u (ring (k' + 1)) (ms + 1 + 1 + 1) z hz hs
  have h := EisRhoF.eis_rho_field j Mp ms t h' hM hpr g hg hℓ hN hh α iv hNα h0 k' hq hq2 e hQ M hqn hp s hs' k hk gl hgl hc0
    hprim (ring (k' + 1)) (CycEvalK.evK (ring (k' + 1)) (ms + 1 + 1 + 1) z) (CycEvalK.evK_mul (ring (k' + 1)) (ms + 1 + 1 + 1) z hz hs)
    (CycEvalK.evK_one (ring (k' + 1)) (ms + 1 + 1 + 1) z hz hs) (nozd k' hq) (one_ne k') (decZ (k' + 1)) (by rw [hu]; exact hz1)
  rw [hu] at h
  exact h

end EisFq

#print axioms EisFq.one_ne
#print axioms EisFq.gs_z
#print axioms EisFq.eis_fq

lang/EisFqF.lean

lean_all/lang/EisFqF.lean

/-
  lang/EisFqF — T49 (residue fields of degree f > 1): **Eisenstein's reciprocity at every prime above q**.
  For any ring F ⊇ 𝔽_q (characteristic q, no zero divisors) and z ∈ F of order ℓ, the base change
  𝔽_q[C_ℓ]/(N) → F[C_ℓ]/(N) followed by evaluation at z (lang/CycBase, lang/FpEmb) is a ring map ρ_z with ρ_z(u) = z;
  ρ_z∘τ(g^i) runs over the primes above q of residue degree f = ord_ℓ(q) (F = 𝔽_(q^f)), and for a primary prime α
  of norm p the computed symbols satisfy z^(Σ_(i<ℓ−1) s_i) = z^(k_1(ℓ−1)), u^(k_1) = (q^(−e)/α)_ℓ.
    eis_fqF
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.EisFq
import lang.CycBase
import lang.FpEmb

namespace EisFqF

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

/-- **Eisenstein's reciprocity at the primes of any degree above q**: F ⊇ 𝔽_q without zero divisors, z ∈ F of order ℓ. -/
theorem eis_fqF (j Mp ms t h' : Nat) (hM : Mp + 1 = (j + 1 + 1 + 1) ^ (ms + 1 + 1))
    (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (g : Zp (j + 1 + 1)) (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (hℓ : ∀ d, Dv d (ms + 1 + 1 + 1 + 1) → d = 1 ∨ d = ms + 1 + 1 + 1 + 1)
    (hN : j + 1 + 1 = (ms + 1 + 1 + 1 + 1) * (t + 1)) (hh : ms + 1 + 1 + 1 = (h' + 1) + (h' + 1))
    (α : V Int (ms + 1 + 1 + 1 + 1)) (iv : Zp (ms + 1 + 1 + 1) → Zp (ms + 1 + 1 + 1))
    (hNα : NormEval.nrm (ms + 1 + 1 + 1) α
        (BbarVal.nlist (ms + 1 + 1 + 1) iv (StickQs.qsL (ms + 1 + 1) 0 (ms + 1 + 1) (h' + 1)))
      = nsm (gring LibInt.intR (ms + 1 + 1 + 1)) (j + 1 + 1 + 1) (gring LibInt.intR (ms + 1 + 1 + 1)).one)
    (h0 : RedP2.red (j + 1) Mp (ConjUnit.Y Mp (ms + 1 + 1 + 1) (TeichN.psiN (j + 1) Mp (ms + 1) (t + 1) g) α
        (ring (ms + 1 + 1 + 1)).one) = (ring (j + 1 + 1)).zero)
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (hq2 : k' + 1 + 1 ≠ 2) (e : Nat)
    (hQ : nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (M : Nat) (hqn : (k' + 1 + 1) ^ e = (ms + 1 + 1 + 1 + 1) * M + 1)
    (hp : nsm (ring (k' + 1)) (j + 1 + 1 + 1) (ring (k' + 1)).one ≠ (ring (k' + 1)).zero) (s : Nat) (hs' : M = (k' + 1) * s)
    (k : Nat → Nat)
    (hk : ∀ c, 1 ≤ c → c < ms + 1 + 1 + 1 + 1 →
      pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g (t + 1)) c) (k c)
        = pw (ring (j + 1 + 1))
            (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one) (j + 1)) (t + 1))
    (gl : Zp (ms + 1 + 1 + 1)) (hgl : OrdMul.Ord (ring (ms + 1 + 1 + 1)) gl (ms + 1 + 1 + 1))
    (hc0 : nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one ≠ (ring (ms + 1 + 1 + 1)).zero)
    (hprim : PrimAlpha1.Prim (ms + 1 + 1) α)
    {T : Type} (F : CRing T) (hF : NoZD F) (h10 : F.one ≠ F.zero) (dec : DecidableEq T)
    (hpF : nsm F (k' + 1 + 1) F.one = F.zero)
    (z : T) (hz : pw F z (ms + 1 + 1 + 1 + 1) = F.one) (hz1 : z ≠ F.one) :
    pw F z
        (SymRho.sumN (EisRhoF.syF (ring (k' + 1)) (ms + 1 + 1) gl F (CycBase.evI (ring (k' + 1)) F (FpEmb.emb F (k' + 1)) (ms + 1 + 1 + 1) z) dec
          (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
            (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) α) M))
          (ms + 1 + 1 + 1))
      = pw F z ((1 * k 1) * (ms + 1 + 1 + 1)) := by
  have hs : CycChar.gs F z (ms + 1 + 1 + 1 + 1) = F.zero := CycEis.gs_zero F hF z hz1 (ms + 1 + 1 + 1 + 1) hz
  have hadd := FpEmb.emb_add F (k' + 1) hpF
  have hmul := FpEmb.emb_mul F (k' + 1) hpF
  have hone := FpEmb.emb_one F (k' + 1) hpF
  have hu := CycBase.evI_u (ring (k' + 1)) F (FpEmb.emb F (k' + 1)) hadd hmul hone (ms + 1 + 1 + 1) z hz hs
  have h := EisRhoF.eis_rho_field j Mp ms t h' hM hpr g hg hℓ hN hh α iv hNα h0 k' hq hq2 e hQ M hqn hp s hs' k hk gl hgl hc0
    hprim F (CycBase.evI (ring (k' + 1)) F (FpEmb.emb F (k' + 1)) (ms + 1 + 1 + 1) z)
    (CycBase.evI_mul (ring (k' + 1)) F (FpEmb.emb F (k' + 1)) hadd hmul (ms + 1 + 1 + 1) z hz hs)
    (CycBase.evI_one (ring (k' + 1)) F (FpEmb.emb F (k' + 1)) hadd hone (ms + 1 + 1 + 1) z hz hs) hF h10 dec (by rw [hu]; exact hz1)
  rw [hu] at h
  exact h

end EisFqF

#print axioms EisFqF.eis_fqF

lang/EisFqFF.lean

lean_all/lang/EisFqFF.lean

/-
  lang/EisFqFF — T49 (residue fields of degree f > 1): **Eisenstein's reciprocity in every finite field of the
  interface of order q^(N+1) with ℓ | q^(N+1) − 1**: such a field has characteristic q (lang/FFChar), an element z of
  order ℓ (FinCyclic.split_pow_ff), and the residue map ρ_z of lang/EisFqF. With the tower 𝔽_(q^(2^k)) of
  lang/FieldTower (order lang/TowerCard) this covers every prime above q whose residue degree divides 2^k.
    eis_ff
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.EisFqF
import lang.FFChar
import lib.RingTac

namespace EisFqFF

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob FinField

/-- **Eisenstein's reciprocity in a finite field of order q^(N+1) with ℓ | q^(N+1) − 1**: an element z of order ℓ
    exists, and the computed symbols at the primes ρ_z∘τ(g^i) satisfy z^(Σ s_i) = z^(k_1(ℓ−1)). -/
theorem eis_ff (j Mp ms t h' : Nat) (hM : Mp + 1 = (j + 1 + 1 + 1) ^ (ms + 1 + 1))
    (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (g : Zp (j + 1 + 1)) (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (hℓ : ∀ d, Dv d (ms + 1 + 1 + 1 + 1) → d = 1 ∨ d = ms + 1 + 1 + 1 + 1)
    (hN : j + 1 + 1 = (ms + 1 + 1 + 1 + 1) * (t + 1)) (hh : ms + 1 + 1 + 1 = (h' + 1) + (h' + 1))
    (α : V Int (ms + 1 + 1 + 1 + 1)) (iv : Zp (ms + 1 + 1 + 1) → Zp (ms + 1 + 1 + 1))
    (hNα : NormEval.nrm (ms + 1 + 1 + 1) α
        (BbarVal.nlist (ms + 1 + 1 + 1) iv (StickQs.qsL (ms + 1 + 1) 0 (ms + 1 + 1) (h' + 1)))
      = nsm (gring LibInt.intR (ms + 1 + 1 + 1)) (j + 1 + 1 + 1) (gring LibInt.intR (ms + 1 + 1 + 1)).one)
    (h0 : RedP2.red (j + 1) Mp (ConjUnit.Y Mp (ms + 1 + 1 + 1) (TeichN.psiN (j + 1) Mp (ms + 1) (t + 1) g) α
        (ring (ms + 1 + 1 + 1)).one) = (ring (j + 1 + 1)).zero)
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (hq2 : k' + 1 + 1 ≠ 2) (e : Nat)
    (hQ : nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (M : Nat) (hqn : (k' + 1 + 1) ^ e = (ms + 1 + 1 + 1 + 1) * M + 1)
    (hp : nsm (ring (k' + 1)) (j + 1 + 1 + 1) (ring (k' + 1)).one ≠ (ring (k' + 1)).zero) (s : Nat) (hs' : M = (k' + 1) * s)
    (k : Nat → Nat)
    (hk : ∀ c, 1 ≤ c → c < ms + 1 + 1 + 1 + 1 →
      pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g (t + 1)) c) (k c)
        = pw (ring (j + 1 + 1))
            (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one) (j + 1)) (t + 1))
    (gl : Zp (ms + 1 + 1 + 1)) (hgl : OrdMul.Ord (ring (ms + 1 + 1 + 1)) gl (ms + 1 + 1 + 1))
    (hc0 : nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one ≠ (ring (ms + 1 + 1 + 1)).zero)
    (hprim : PrimAlpha1.Prim (ms + 1 + 1) α)
    {R : Type} [dec : DecidableEq R] (F : FF R) (N : Nat) (hcard : 2 * F.h + 1 = (k' + 1 + 1) ^ (N + 1))
    (hdv : Dv (ms + 1 + 1 + 1 + 1) (2 * F.h)) :
    ∃ z, pw F.K z (ms + 1 + 1 + 1 + 1) = F.K.one ∧ z ≠ F.K.one ∧ pw F.K z
        (SymRho.sumN (EisRhoF.syF (ring (k' + 1)) (ms + 1 + 1) gl F.K (CycBase.evI (ring (k' + 1)) F.K (FpEmb.emb F.K (k' + 1)) (ms + 1 + 1 + 1) z) dec
          (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
            (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) α) M))
          (ms + 1 + 1 + 1))
      = pw F.K z ((1 * k 1) * (ms + 1 + 1 + 1)) := by
  have h10 : F.K.one ≠ F.K.zero := fun e => F.ne_pm (by
    rw [e]
    show F.K.neg F.K.zero = F.K.zero
    lib_ring)
  have hpF := FFChar.char_q F (k' + 1 + 1) N hcard
  have hd : Dv ((ms + 1 + 1 + 1 + 1) ^ (0 + 1)) (2 * F.h) := by
    rw [show (ms + 1 + 1 + 1 + 1) ^ (0 + 1) = ms + 1 + 1 + 1 + 1 by
      show 1 * (ms + 1 + 1 + 1 + 1) = ms + 1 + 1 + 1 + 1
      lib_nat]
    exact hdv
  obtain ⟨y, hy1, hy0⟩ := FinCyclic.split_pow_ff F (ms + 1 + 1) 0 hd
  have hz : pw F.K y (ms + 1 + 1 + 1 + 1) = F.K.one := by
    rw [show ms + 1 + 1 + 1 + 1 = (ms + 1 + 1 + 1 + 1) ^ (0 + 1) by
      show ms + 1 + 1 + 1 + 1 = 1 * (ms + 1 + 1 + 1 + 1)
      lib_nat]
    exact hy1
  have hz1 : y ≠ F.K.one := fun e => hy0 (by
    show F.K.mul F.K.one y = F.K.one
    rw [e]
    exact F.K.one_mul _)
  exact ⟨y, hz, hz1, EisFqF.eis_fqF j Mp ms t h' hM hpr g hg hℓ hN hh α iv hNα h0 k' hq hq2 e hQ M hqn hp s hs' k hk gl hgl hc0
    hprim F.K F.nozd h10 dec hpF y hz hz1⟩

end EisFqFF

#print axioms EisFqFF.eis_ff

lang/EisFqMixed.lean

lean_all/lang/EisFqMixed.lean

/-
  lang/EisFqMixed — T49 (residue fields of degree f > 1): **Eisenstein's reciprocity at every prime above q whose
  residue degree f has only the prime factors 2 and 3** (q = 2m + 3, q not 0 mod 3): the mixed tower field of order
  q^(2^(L+1)·3^b) (lang/MixedCard) with f | 2^(L+1)·3^b satisfies ℓ | |F^×|, so lang/EisFqFF applies.
    pow_one_mul      q^e = ℓM + 1 ⇒ q^(ec) = ℓM' + 1
    pow_succ_ex      (x+1)^n = N + 1
    mixed_pos        2^(L+1)·3^b = N + 1
    eis_mixed
  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.EisFqFF
import lang.MixedCard

namespace EisFqMixed

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob FinField MixedTower

theorem pow_one_mul (q l e M : Nat) (h : q ^ e = l * M + 1) : ∀ c, ∃ M', q ^ (e * c) = l * M' + 1
  | 0 => ⟨0, by
    show 1 = l * 0 + 1
    lib_nat⟩
  | c + 1 => by
    match pow_one_mul q l e M h c with
    | ⟨M', hM'⟩ =>
      exact ⟨M' * l * M + M' + M, by
        show q ^ (e * c + e) = _
        rw [TowerCard.pow_add', hM', h]
        lib_nat⟩

theorem pow_succ_ex (x : Nat) : ∀ n, ∃ N, (x + 1) ^ n = N + 1
  | 0 => ⟨0, rfl⟩
  | n + 1 => by
    match pow_succ_ex x n with
    | ⟨N, h⟩ =>
      exact ⟨N * x + N + x, by
        show (x + 1) ^ n * (x + 1) = _
        rw [h]
        lib_nat⟩

theorem mixed_pos (L b : Nat) : ∃ N, 2 ^ (L + 1) * 3 ^ b = N + 1 := by
  match pow_succ_ex 1 (L + 1), pow_succ_ex 2 b with
  | ⟨A, hA⟩, ⟨B, hB⟩ =>
    exact ⟨A * B + A + B, by
      show (1 + 1) ^ (L + 1) * (2 + 1) ^ b = _
      rw [hA, hB]
      lib_nat⟩

/-- **Eisenstein's reciprocity at primes of residue degree f | 2^(L+1)·3^b**: in the mixed tower field of order
    q^(2^(L+1)·3^b) an element z of order ℓ exists and the computed symbols satisfy z^(Σ s_i) = z^(k_1(ℓ−1)). -/
theorem eis_mixed (j Mp ms t h' : Nat) (hM : Mp + 1 = (j + 1 + 1 + 1) ^ (ms + 1 + 1))
    (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (g : Zp (j + 1 + 1)) (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (hℓ : ∀ d, Dv d (ms + 1 + 1 + 1 + 1) → d = 1 ∨ d = ms + 1 + 1 + 1 + 1)
    (hNj : j + 1 + 1 = (ms + 1 + 1 + 1 + 1) * (t + 1)) (hh : ms + 1 + 1 + 1 = (h' + 1) + (h' + 1))
    (α : V Int (ms + 1 + 1 + 1 + 1)) (iv : Zp (ms + 1 + 1 + 1) → Zp (ms + 1 + 1 + 1))
    (hNα : NormEval.nrm (ms + 1 + 1 + 1) α
        (BbarVal.nlist (ms + 1 + 1 + 1) iv (StickQs.qsL (ms + 1 + 1) 0 (ms + 1 + 1) (h' + 1)))
      = nsm (gring LibInt.intR (ms + 1 + 1 + 1)) (j + 1 + 1 + 1) (gring LibInt.intR (ms + 1 + 1 + 1)).one)
    (h0 : RedP2.red (j + 1) Mp (ConjUnit.Y Mp (ms + 1 + 1 + 1) (TeichN.psiN (j + 1) Mp (ms + 1) (t + 1) g) α
        (ring (ms + 1 + 1 + 1)).one) = (ring (j + 1 + 1)).zero)
    (m : Nat) (hq : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (hq2 : 2 * m + 1 + 1 + 1 ≠ 2) (e : Nat)
    (hQ : nsm (ring (j + 1 + 1)) ((2 * m + 1 + 1 + 1) ^ e) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (M : Nat) (hqn : (2 * m + 1 + 1 + 1) ^ e = (ms + 1 + 1 + 1 + 1) * M + 1)
    (hp : nsm (ring (2 * m + 1 + 1)) (j + 1 + 1 + 1) (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero) (s : Nat) (hs' : M = (2 * m + 1 + 1) * s)
    (k : Nat → Nat)
    (hk : ∀ c, 1 ≤ c → c < ms + 1 + 1 + 1 + 1 →
      pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g (t + 1)) c) (k c)
        = pw (ring (j + 1 + 1))
            (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((2 * m + 1 + 1 + 1) ^ e) (ring (j + 1 + 1)).one) (j + 1)) (t + 1))
    (gl : Zp (ms + 1 + 1 + 1)) (hgl : OrdMul.Ord (ring (ms + 1 + 1 + 1)) gl (ms + 1 + 1 + 1))
    (hc0 : nsm (ring (ms + 1 + 1 + 1)) (2 * m + 1 + 1 + 1) (ring (ms + 1 + 1 + 1)).one ≠ (ring (ms + 1 + 1 + 1)).zero)
    (hprim : PrimAlpha1.Prim (ms + 1 + 1) α)
    (h3 : (∃ t, m = 3 * t + 1) ∨ (∃ t, m = 3 * t + 2)) (L b c : Nat) (hf : 2 ^ (L + 1) * 3 ^ b = e * c) :
    ∃ G : @FF (C m L b) (instDecC m L b),
      ∃ z, pw G.K z (ms + 1 + 1 + 1 + 1) = G.K.one ∧ z ≠ G.K.one ∧ pw G.K z
        (SymRho.sumN (EisRhoF.syF (ring (2 * m + 1 + 1)) (ms + 1 + 1) gl G.K (CycBase.evI (ring (2 * m + 1 + 1)) G.K (FpEmb.emb G.K (2 * m + 1 + 1)) (ms + 1 + 1 + 1) z) (instDecC m L b)
          (pw (CycRing.cyc (ring (2 * m + 1 + 1)) (ms + 1 + 1 + 1))
            (CycEis.phz (CycRing.cyc (ring (2 * m + 1 + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (2 * m + 1 + 1)) (ms + 1 + 1 + 1)) α) M))
          (ms + 1 + 1 + 1))
      = pw G.K z ((1 * k 1) * (ms + 1 + 1 + 1))  := by
  match MixedCard.mixed_card m hq h3 L b, mixed_pos L b with
  | ⟨G, _, hc⟩, ⟨N, hNp⟩ =>
    have hcard : 2 * G.h + 1 = (2 * m + 1 + 1 + 1) ^ (N + 1) := by rw [hc, hNp]
    match pow_one_mul (2 * m + 1 + 1 + 1) (ms + 1 + 1 + 1 + 1) e M hqn c with
    | ⟨M', hM'⟩ =>
      have e1 : 2 * G.h + 1 = (ms + 1 + 1 + 1 + 1) * M' + 1 := by rw [hc, hf, hM']
      have hdv : Dv (ms + 1 + 1 + 1 + 1) (2 * G.h) := ⟨M', by
        apply FermatUnique.add_cancel 1
        rw [show 1 + 2 * G.h = 2 * G.h + 1 by lib_nat, e1]
        lib_nat⟩
      exact ⟨G, @EisFqFF.eis_ff j Mp ms t h' hM hpr g hg hℓ hNj hh α iv hNα h0 (2 * m + 1) hq hq2 e hQ M hqn hp s hs' k hk
        gl hgl hc0 hprim _ (instDecC m L b) G N hcard hdv⟩

end EisFqMixed

#print axioms EisFqMixed.pow_one_mul
#print axioms EisFqMixed.pow_succ_ex
#print axioms EisFqMixed.mixed_pos
#print axioms EisFqMixed.eis_mixed

lang/EisFqMul.lean

lean_all/lang/EisFqMul.lean

/-
  lang/EisFqMul — T49 (general ℓ): **the computed symbols add under products**, so Eisenstein's reciprocity at the
  primes of degree one above q (lang/EisFq) passes from primary primes of norm p to their products.
    rho_sum_mul   ρ(u)^(Σ s(Y Y')) = ρ(u)^(Σ s(Y) + Σ s(Y'))           (Y^ℓ = Y'^ℓ = 1)
    rho_sum_one   ρ(u)^(Σ s(1)) = 1
    prodY         Π of a list in K[C_ℓ]/(N)
    prodY_ell     Y_i^ℓ = 1 for all i ⇒ (Π Y_i)^ℓ = 1
    rho_sum_list  ρ(u)^(Σ s(Y_i)) = ρ(u)^(c_i) for all i ⇒ ρ(u)^(Σ s(Π Y_i)) = ρ(u)^(Σ c_i)
    phz_prodV     φ_u(Π α_i)^M = Π φ_u(α_i)^M
    eis_fq_mul    z^(Σ s(φ_u(α_i)^M)) = z^(c_i) for all i ⇒ z^(Σ s(φ_u(Π α_i)^M)) = z^(Σ c_i)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.EisFq
import lang.EisAlphaMul

namespace EisFqMul

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

section Gen

variable {R : Type} (K : CRing R) (n : Nat) (g : Zp (n + 1)) (hg : pw (ring (n + 1)) g (n + 1) = (ring (n + 1)).one)
  {T : Type} (F : CRing T) (ρ : CycRing.C K (n + 1) → T)
  (hmul : ∀ x y, ρ ((CycRing.cyc K (n + 1)).mul x y) = F.mul (ρ x) (ρ y)) (hone : ρ (CycRing.cyc K (n + 1)).one = F.one)
  (hK : NoZD F) (h10 : F.one ≠ F.zero) (dec : DecidableEq T)
  (hpr : ∀ d, Dv d (n + 1 + 1) → d = 1 ∨ d = n + 1 + 1) (hu1 : ρ (CycUnit.u K (n + 1)) ≠ F.one)

include hg hmul hone hK h10 hpr hu1 in
/-- **ρ(u)^(Σ s(Y Y')) = ρ(u)^(Σ s(Y) + Σ s(Y'))**. -/
theorem rho_sum_mul (Y Y' : CycRing.C K (n + 1))
    (hY : pw (CycRing.cyc K (n + 1)) Y (n + 1 + 1) = (CycRing.cyc K (n + 1)).one)
    (hY' : pw (CycRing.cyc K (n + 1)) Y' (n + 1 + 1) = (CycRing.cyc K (n + 1)).one) :
    pw F (ρ (CycUnit.u K (n + 1))) (SymRho.sumN (EisRhoF.syF K n g F ρ dec ((CycRing.cyc K (n + 1)).mul Y Y')) (n + 1))
      = pw F (ρ (CycUnit.u K (n + 1)))
          (SymRho.sumN (EisRhoF.syF K n g F ρ dec Y) (n + 1) + SymRho.sumN (EisRhoF.syF K n g F ρ dec Y') (n + 1)) := by
  have hYY : pw (CycRing.cyc K (n + 1)) ((CycRing.cyc K (n + 1)).mul Y Y') (n + 1 + 1) = (CycRing.cyc K (n + 1)).one := by
    rw [Frobenius.pw_mul, hY, hY']
    exact (CycRing.cyc K (n + 1)).one_mul _
  rw [← EisRhoF.rho_G_field K n g hg F ρ hmul hone hK h10 dec hpr hu1 _ hYY, SymG.G_mul K n g hg, hmul,
    EisRhoF.rho_G_field K n g hg F ρ hmul hone hK h10 dec hpr hu1 _ hY,
    EisRhoF.rho_G_field K n g hg F ρ hmul hone hK h10 dec hpr hu1 _ hY']
  exact (SymG.pw_add F _ _ _).symm

include hg hmul hone hK h10 hpr hu1 in
/-- **ρ(u)^(Σ s(1)) = 1**. -/
theorem rho_sum_one :
    pw F (ρ (CycUnit.u K (n + 1))) (SymRho.sumN (EisRhoF.syF K n g F ρ dec (CycRing.cyc K (n + 1)).one) (n + 1)) = F.one := by
  rw [← EisRhoF.rho_G_field K n g hg F ρ hmul hone hK h10 dec hpr hu1 _ (TwoPower.pwone _ _)]
  have h := SymG.G_u K n g hg 0
  have h1 : SymG.G K n g (CycRing.cyc K (n + 1)).one = pw (CycRing.cyc K (n + 1)) (CycRing.cyc K (n + 1)).one (n + 1) := h
  rw [h1, TwoPower.pwone, hone]

/-- Π of a list in K[C_ℓ]/(N). -/
def prodY : List (CycRing.C K (n + 1)) → CycRing.C K (n + 1)
  | [] => (CycRing.cyc K (n + 1)).one
  | a :: as => (CycRing.cyc K (n + 1)).mul a (prodY as)

/-- **Y_i^ℓ = 1 for all i ⇒ (Π Y_i)^ℓ = 1**. -/
theorem prodY_ell {ι : Type} (f : ι → CycRing.C K (n + 1)) : ∀ xs : List ι,
    (∀ x, x ∈ xs → pw (CycRing.cyc K (n + 1)) (f x) (n + 1 + 1) = (CycRing.cyc K (n + 1)).one) →
    pw (CycRing.cyc K (n + 1)) (prodY K n (xs.map f)) (n + 1 + 1) = (CycRing.cyc K (n + 1)).one
  | [], _ => TwoPower.pwone _ _
  | a :: as, h => by
    show pw (CycRing.cyc K (n + 1)) ((CycRing.cyc K (n + 1)).mul (f a) (prodY K n (as.map f))) (n + 1 + 1) = _
    rw [Frobenius.pw_mul, h a (List.Mem.head as), prodY_ell f as (fun x hx => h x (List.Mem.tail a hx))]
    exact (CycRing.cyc K (n + 1)).one_mul _

include hg hmul hone hK h10 hpr hu1 in
/-- **ρ(u)^(Σ s(Y_i)) = ρ(u)^(c_i) for all i ⇒ ρ(u)^(Σ s(Π Y_i)) = ρ(u)^(Σ c_i)**. -/
theorem rho_sum_list {ι : Type} (f : ι → CycRing.C K (n + 1)) (c : ι → Nat) : ∀ xs : List ι,
    (∀ x, x ∈ xs → pw (CycRing.cyc K (n + 1)) (f x) (n + 1 + 1) = (CycRing.cyc K (n + 1)).one) →
    (∀ x, x ∈ xs → pw F (ρ (CycUnit.u K (n + 1))) (SymRho.sumN (EisRhoF.syF K n g F ρ dec (f x)) (n + 1))
      = pw F (ρ (CycUnit.u K (n + 1))) (c x)) →
    pw F (ρ (CycUnit.u K (n + 1))) (SymRho.sumN (EisRhoF.syF K n g F ρ dec (prodY K n (xs.map f))) (n + 1))
      = pw F (ρ (CycUnit.u K (n + 1))) ((xs.map c).foldr (· + ·) 0)
  | [], _, _ => rho_sum_one K n g hg F ρ hmul hone hK h10 dec hpr hu1
  | a :: as, h1, h2 => by
    show pw F _ (SymRho.sumN (EisRhoF.syF K n g F ρ dec ((CycRing.cyc K (n + 1)).mul (f a) (prodY K n (as.map f)))) (n + 1))
      = pw F _ (c a + (as.map c).foldr (· + ·) 0)
    rw [rho_sum_mul K n g hg F ρ hmul hone hK h10 dec hpr hu1 _ _ (h1 a (List.Mem.head as)) (prodY_ell K n f as (fun x hx => h1 x (List.Mem.tail a hx))),
      SymG.pw_add F, SymG.pw_add F, h2 a (List.Mem.head as),
      rho_sum_list f c as (fun x hx => h1 x (List.Mem.tail a hx)) (fun x hx => h2 x (List.Mem.tail a hx))]

/-- **φ_u(Π α_i)^M = Π φ_u(α_i)^M**. -/
theorem phz_prodV (M : Nat) : ∀ αs : List (V Int (n + 1 + 1)),
    pw (CycRing.cyc K (n + 1))
        (CycEis.phz (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1)) (EisAlphaMul.prodV n αs)) M
      = prodY K n (αs.map (fun a => pw (CycRing.cyc K (n + 1))
          (CycEis.phz (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1)) a) M))
  | [] => by
    show pw (CycRing.cyc K (n + 1))
        (CycEis.phz (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1)) (gring LibInt.intR (n + 1)).one) M
      = (CycRing.cyc K (n + 1)).one
    rw [CycEis.phz_one]
    exact TwoPower.pwone _ _
  | a :: as => by
    show pw (CycRing.cyc K (n + 1)) (CycEis.phz (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1))
        ((gring LibInt.intR (n + 1)).mul a (EisAlphaMul.prodV n as))) M
      = (CycRing.cyc K (n + 1)).mul _ (prodY K n (as.map _))
    rw [CycEis.phz_mul (CycRing.cyc K (n + 1)) (n + 1) _ (CycUnit.u_ell K (n + 1)), Frobenius.pw_mul, phz_prodV M as]

end Gen

/-- **Eisenstein's reciprocity at the primes of degree one above q, for products**: if each factor α_i satisfies
    z^(Σ s(φ_u(α_i)^M)) = z^(c_i) (lang/EisFq gives this with c_i = k_1(α_i)(ℓ−1) for primary primes of norm p),
    then z^(Σ s(φ_u(Π α_i)^M)) = z^(Σ c_i). -/
theorem eis_fq_mul (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (ms : Nat)
    (hℓ : ∀ d, Dv d (ms + 1 + 1 + 1 + 1) → d = 1 ∨ d = ms + 1 + 1 + 1 + 1)
    (gl : Zp (ms + 1 + 1 + 1)) (hgl : OrdMul.Ord (ring (ms + 1 + 1 + 1)) gl (ms + 1 + 1 + 1))
    (z : Zp (k' + 1)) (hz : pw (ring (k' + 1)) z (ms + 1 + 1 + 1 + 1) = (ring (k' + 1)).one) (hz1 : z ≠ (ring (k' + 1)).one)
    (M : Nat) (c : V Int (ms + 1 + 1 + 1 + 1) → Nat) (αs : List (V Int (ms + 1 + 1 + 1 + 1)))
    (hY : ∀ a, a ∈ αs → pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
        (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
          (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) a) M)
        (ms + 1 + 1 + 1 + 1) = (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)).one)
    (hrec : ∀ a, a ∈ αs → pw (ring (k' + 1)) z
        (SymRho.sumN (EisRhoF.syF (ring (k' + 1)) (ms + 1 + 1) gl (ring (k' + 1)) (CycEvalK.evK (ring (k' + 1)) (ms + 1 + 1 + 1) z)
          (EisFq.decZ (k' + 1))
          (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
            (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) a) M))
          (ms + 1 + 1 + 1))
        = pw (ring (k' + 1)) z (c a)) :
    pw (ring (k' + 1)) z
        (SymRho.sumN (EisRhoF.syF (ring (k' + 1)) (ms + 1 + 1) gl (ring (k' + 1)) (CycEvalK.evK (ring (k' + 1)) (ms + 1 + 1 + 1) z)
          (EisFq.decZ (k' + 1))
          (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
            (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1))
              (EisAlphaMul.prodV (ms + 1 + 1) αs)) M))
          (ms + 1 + 1 + 1))
      = pw (ring (k' + 1)) z ((αs.map c).foldr (· + ·) 0) := by
  have hs := EisFq.gs_z k' hq (ms + 1 + 1 + 1) z hz hz1
  have hu := CycEvalK.evK_u (ring (k' + 1)) (ms + 1 + 1 + 1) z hz hs
  have hu1 : CycEvalK.evK (ring (k' + 1)) (ms + 1 + 1 + 1) z (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) ≠ (ring (k' + 1)).one := by
    rw [hu]
    exact hz1
  rw [phz_prodV (ring (k' + 1)) (ms + 1 + 1) M αs]
  have h := rho_sum_list (ring (k' + 1)) (ms + 1 + 1) gl hgl.1 (ring (k' + 1)) (CycEvalK.evK (ring (k' + 1)) (ms + 1 + 1 + 1) z)
    (CycEvalK.evK_mul (ring (k' + 1)) (ms + 1 + 1 + 1) z hz hs) (CycEvalK.evK_one (ring (k' + 1)) (ms + 1 + 1 + 1) z hz hs)
    (nozd k' hq) (EisFq.one_ne k') (EisFq.decZ (k' + 1)) hℓ hu1
    (fun a => pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
      (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) a) M)
    c αs hY (fun a ha => by rw [hu]; exact hrec a ha)
  rw [hu] at h
  exact h

end EisFqMul

#print axioms EisFqMul.rho_sum_mul
#print axioms EisFqMul.rho_sum_one
#print axioms EisFqMul.prodY_ell
#print axioms EisFqMul.rho_sum_list
#print axioms EisFqMul.phz_prodV
#print axioms EisFqMul.eis_fq_mul

lang/EisFqY.lean

lean_all/lang/EisFqY.lean

/-
  lang/EisFqY — T49 (general ℓ): **φ_u(α)^M is an ℓ-th root of unity** in 𝔽_q[C_ℓ]/(N) whenever the norm of α is
  prime to q and q^e = ℓM + 1, so the hypothesis hY of lang/EisFqMul holds for every factor; with it the
  reciprocity for products of primary primes of norm p_i needs only the per-prime statements of lang/EisFq.
    q_inv        q · q^(ℓ−2) = 1 in 𝔽_ℓ
    y_ell_alpha  (φ_u(α)^M)^ℓ = 1
    eis_fq_prod  z^(Σ s(φ_u(Π α_i)^M)) = z^(Σ c_i) from the per-prime reciprocity alone
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.EisFqMul
import lib.RingTac
import lib.Semi

namespace EisFqY

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

/-- **q · q^(ℓ−2) = 1 in 𝔽_ℓ** (q ≢ 0 mod ℓ). -/
theorem q_inv (ms k' : Nat) (hℓ : ∀ d, Dv d (ms + 1 + 1 + 1 + 1) → d = 1 ∨ d = ms + 1 + 1 + 1 + 1)
    (hc0 : nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one ≠ (ring (ms + 1 + 1 + 1)).zero) :
    (ring (ms + 1 + 1 + 1)).mul (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one)
      (pw (ring (ms + 1 + 1 + 1)) (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one) (ms + 1 + 1))
      = (ring (ms + 1 + 1 + 1)).one := by
  have hf := NthEis.fermat_unit (ms + 1 + 1) hℓ _ hc0
  have h2 : (ring (ms + 1 + 1 + 1)).mul (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one)
      (pw (ring (ms + 1 + 1 + 1)) (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one) (ms + 1 + 1))
      = pw (ring (ms + 1 + 1 + 1)) (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one) (ms + 1 + 1 + 1) := by
    show _ = (ring (ms + 1 + 1 + 1)).mul (pw (ring (ms + 1 + 1 + 1)) (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one) (ms + 1 + 1))
      (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one)
    lib_ring
  exact h2.trans hf

/-- **(φ_u(α)^M)^ℓ = 1** for N(α) = P prime to q and q^e = ℓM + 1. -/
theorem y_ell_alpha (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (ms : Nat)
    (hℓ : ∀ d, Dv d (ms + 1 + 1 + 1 + 1) → d = 1 ∨ d = ms + 1 + 1 + 1 + 1)
    (hc0 : nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one ≠ (ring (ms + 1 + 1 + 1)).zero)
    (P : Nat) (hp : nsm (ring (k' + 1)) P (ring (k' + 1)).one ≠ (ring (k' + 1)).zero)
    (α : V Int (ms + 1 + 1 + 1 + 1)) (iv : Zp (ms + 1 + 1 + 1) → Zp (ms + 1 + 1 + 1)) (h' : Nat)
    (hNα : NormEval.nrm (ms + 1 + 1 + 1) α
        (BbarVal.nlist (ms + 1 + 1 + 1) iv (StickQs.qsL (ms + 1 + 1) 0 (ms + 1 + 1) (h' + 1)))
      = nsm (gring LibInt.intR (ms + 1 + 1 + 1)) P (gring LibInt.intR (ms + 1 + 1 + 1)).one)
    (e M : Nat) (hqn : (k' + 1 + 1) ^ e = (ms + 1 + 1 + 1 + 1) * M + 1) :
    pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
        (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
          (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) α) M)
        (ms + 1 + 1 + 1 + 1) = (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)).one :=
  SymY.Y_ell (ms + 1 + 1 + 1) k' hq _ _ (q_inv ms k' hℓ hc0) rfl e (EisAlpha.q_order (ms + 1 + 1) (k' + 1 + 1) e M hqn) M hqn _
    (EisAlpha.unit_alpha k' hq (ms + 1 + 1) P hp α iv h' hNα)

/-- **Eisenstein's reciprocity at the primes of degree one above q, for products of elements of norm prime to q**:
    the per-prime statements z^(Σ s(φ_u(α_i)^M)) = z^(c_i) give z^(Σ s(φ_u(Π α_i)^M)) = z^(Σ c_i). -/
theorem eis_fq_prod (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (ms : Nat)
    (hℓ : ∀ d, Dv d (ms + 1 + 1 + 1 + 1) → d = 1 ∨ d = ms + 1 + 1 + 1 + 1)
    (hc0 : nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one ≠ (ring (ms + 1 + 1 + 1)).zero)
    (gl : Zp (ms + 1 + 1 + 1)) (hgl : OrdMul.Ord (ring (ms + 1 + 1 + 1)) gl (ms + 1 + 1 + 1))
    (z : Zp (k' + 1)) (hz : pw (ring (k' + 1)) z (ms + 1 + 1 + 1 + 1) = (ring (k' + 1)).one) (hz1 : z ≠ (ring (k' + 1)).one)
    (iv : Zp (ms + 1 + 1 + 1) → Zp (ms + 1 + 1 + 1)) (h' : Nat)
    (e M : Nat) (hqn : (k' + 1 + 1) ^ e = (ms + 1 + 1 + 1 + 1) * M + 1)
    (P c : V Int (ms + 1 + 1 + 1 + 1) → Nat) (αs : List (V Int (ms + 1 + 1 + 1 + 1)))
    (hp : ∀ a, a ∈ αs → nsm (ring (k' + 1)) (P a) (ring (k' + 1)).one ≠ (ring (k' + 1)).zero)
    (hN : ∀ a, a ∈ αs → NormEval.nrm (ms + 1 + 1 + 1) a
        (BbarVal.nlist (ms + 1 + 1 + 1) iv (StickQs.qsL (ms + 1 + 1) 0 (ms + 1 + 1) (h' + 1)))
      = nsm (gring LibInt.intR (ms + 1 + 1 + 1)) (P a) (gring LibInt.intR (ms + 1 + 1 + 1)).one)
    (hrec : ∀ a, a ∈ αs → pw (ring (k' + 1)) z
        (SymRho.sumN (EisRhoF.syF (ring (k' + 1)) (ms + 1 + 1) gl (ring (k' + 1)) (CycEvalK.evK (ring (k' + 1)) (ms + 1 + 1 + 1) z)
          (EisFq.decZ (k' + 1))
          (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
            (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) a) M))
          (ms + 1 + 1 + 1))
        = pw (ring (k' + 1)) z (c a)) :
    pw (ring (k' + 1)) z
        (SymRho.sumN (EisRhoF.syF (ring (k' + 1)) (ms + 1 + 1) gl (ring (k' + 1)) (CycEvalK.evK (ring (k' + 1)) (ms + 1 + 1 + 1) z)
          (EisFq.decZ (k' + 1))
          (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
            (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1))
              (EisAlphaMul.prodV (ms + 1 + 1) αs)) M))
          (ms + 1 + 1 + 1))
      = pw (ring (k' + 1)) z ((αs.map c).foldr (· + ·) 0) :=
  EisFqMul.eis_fq_mul k' hq ms hℓ gl hgl z hz hz1 M c αs
    (fun a ha => y_ell_alpha k' hq ms hℓ hc0 (P a) (hp a ha) a iv h' (hN a ha) e M hqn) hrec

end EisFqY

#print axioms EisFqY.q_inv
#print axioms EisFqY.y_ell_alpha
#print axioms EisFqY.eis_fq_prod

lang/EisInert3.lean

lean_all/lang/EisInert3.lean

/-
  lang/EisInert3 — T50, cycle 620: **Hecke characters of ℚ(√−3) at inert primes**. p = 3t + 2 prime:
    inert_div      p | xy in ℤ[ω] ⇒ p | x ∨ p | y          (gcd(p, x) has norm 1, p or p²; norm p ≡ 2 mod 3 is impossible, en_mod3)
    pow_odd        p^(2k+1) ≡ 2 (mod 3)
    factor_inert   N(α) = p^(2k) ⇒ α = u p^k, N(u) = 1
    prim_inert     α ≡ 1 (mod 3), N(α) = p^(2k) ⇒ α = (−p)^k
    cw3_even       c_W(p^(2k)) = W((−p)^k)
    cw3_odd        c_W(p^(2k+1)) = 0
  (for W(α) = α: c(p²) = −p, so the p-part of L(c, s) is 1/(1 + p^(2−2s)) = 1/(1 − a_p p^(−s) + p^(1−2s)) with a_p = 0.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.FermatInert3

namespace EisInert3

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius EisEuclid EisPrim EisUnique3 EisPrime3 HeckeMult3

variable (t : Nat)

def Pi : Int × Int := (Int.ofNat (3 * t + 1 + 1), 0)

theorem en_Pi : en (Pi t) = Int.ofNat ((3 * t + 1 + 1) * (3 * t + 1 + 1)) := by
  show Int.ofNat (3 * t + 1 + 1) * Int.ofNat (3 * t + 1 + 1) + -(Int.ofNat (3 * t + 1 + 1) * 0) + 0 * 0 = Int.ofNat (3 * t + 1 + 1) * Int.ofNat (3 * t + 1 + 1)
  lib_int

theorem nrm_Pi : nrm (Pi t) = (3 * t + 1 + 1) * (3 * t + 1 + 1) := Int.ofNat.inj (by rw [nrm_cast, en_Pi])

theorem cj_Pi : cj (Pi t) = Pi t := Prod.ext (by show Int.ofNat (3 * t + 1 + 1) + -0 = Int.ofNat (3 * t + 1 + 1); lib_int) (by show -(0 : Int) = 0; rfl)

theorem not_p (z : Int × Int) (h : nrm z = 3 * t + 1 + 1) : False :=
  FermatInert3.en_mod3 z (Int.ofNat t) (by rw [← nrm_cast, h]; rfl)

theorem dvd_conv (hpr : ∀ d, Dv d (3 * t + 1 + 1) → d = 1 ∨ d = 3 * t + 1 + 1) : ∀ d, d ∣ (3 * t + 1 + 1) → d = 1 ∨ d = 3 * t + 1 + 1 :=
  fun d ⟨c, hc⟩ => hpr d ⟨c, by rw [hc]; lib_nat⟩

/-- the two factor case: a b = p², p ∤ … ⇒ a = 1 or b = 1 (a = p is excluded separately). -/
theorem split_sq (hpr : ∀ d, Dv d (3 * t + 1 + 1) → d = 1 ∨ d = 3 * t + 1 + 1) (a b : Nat) (h : a * b = (3 * t + 1 + 1) * (3 * t + 1 + 1)) :
    a = 1 ∨ b = 1 ∨ a = 3 * t + 1 + 1 := by
  cases euclid_prime (dvd_conv t hpr) (show (3 * t + 1 + 1) ∣ a * b from ⟨3 * t + 1 + 1, h⟩) with
  | inl ha =>
    obtain ⟨s, hs⟩ := ha
    have e : s * b = 3 * t + 1 + 1 := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (3 * t + 1)) (by rw [show (3 * t + 1 + 1) * (s * b) = (3 * t + 1 + 1) * s * b by lib_nat, ← hs, h])
    cases hpr s ⟨b, by rw [← e]; lib_nat⟩ with
    | inl s1 => exact Or.inr (Or.inr (by rw [hs, s1]; lib_nat))
    | inr sp =>
      refine Or.inr (Or.inl ?_)
      rw [sp] at e
      exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (3 * t + 1)) (e.trans (Nat.mul_one _).symm)
  | inr hb =>
    obtain ⟨s, hs⟩ := hb
    have e : a * s = 3 * t + 1 + 1 := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (3 * t + 1)) (by
      rw [show (3 * t + 1 + 1) * (a * s) = a * ((3 * t + 1 + 1) * s) by lib_nat, ← hs, h])
    cases hpr a ⟨s, by rw [← e]; lib_nat⟩ with
    | inl a1 => exact Or.inl a1
    | inr ap => exact Or.inr (Or.inr ap)

/-- **p ≡ 2 (mod 3) stays prime in ℤ[ω].** -/
theorem inert_div (hpr : ∀ d, Dv d (3 * t + 1 + 1) → d = 1 ∨ d = 3 * t + 1 + 1) (x y : Int × Int)
    (h : Dvd' (Pi t) ((Cyclo3.cyc3 LibInt.intR).mul x y)) : Dvd' (Pi t) x ∨ Dvd' (Pi t) y := by
  match egcd_exists _ (Pi t) x (Nat.le_refl _) with
  | ⟨d, X, Y, hd, ⟨c1, hc1⟩, ⟨c2, hc2⟩⟩ =>
    have hdc : nrm d * nrm c1 = (3 * t + 1 + 1) * (3 * t + 1 + 1) := by rw [← nrm_mul, ← hc1, nrm_Pi]
    rcases split_sq t hpr _ _ hdc with h1 | h1 | h1
    · refine Or.inr (eeuclid (Pi t) x y ((Cyclo3.cyc3 LibInt.intR).mul (cj d) X) ((Cyclo3.cyc3 LibInt.intR).mul (cj d) Y) ?_ h)
      rw [show (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (cj d) X) (Pi t)) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (cj d) Y) x)
          = (Cyclo3.cyc3 LibInt.intR).mul (cj d) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul X (Pi t)) ((Cyclo3.cyc3 LibInt.intR).mul Y x)) by lib_ring,
        ← hd, (Cyclo3.cyc3 LibInt.intR).mul_comm, unit_inv d h1]
    · refine Or.inl ⟨(Cyclo3.cyc3 LibInt.intR).mul (cj c1) c2, ?_⟩
      have hd' : d = (Cyclo3.cyc3 LibInt.intR).mul (Pi t) (cj c1) := by
        rw [hc1]
        calc d = (Cyclo3.cyc3 LibInt.intR).mul d (Cyclo3.cyc3 LibInt.intR).one := by lib_ring
          _ = _ := by rw [← unit_inv c1 h1]; lib_ring
      rw [hc2, hd']; lib_ring
    · exact (not_p t d h1).elim

theorem nat_pow2 (k : Nat) : (3 * t + 1 + 1) ^ (2 * (k + 1)) = (3 * t + 1 + 1) * (3 * t + 1 + 1) * (3 * t + 1 + 1) ^ (2 * k) := by
  rw [show 2 * (k + 1) = 2 * k + 1 + 1 by lib_nat]
  show (3 * t + 1 + 1) ^ (2 * k) * (3 * t + 1 + 1) * (3 * t + 1 + 1) = _
  lib_nat

/-- **Factorisation above an inert prime.** -/
theorem factor_inert (hpr : ∀ d, Dv d (3 * t + 1 + 1) → d = 1 ∨ d = 3 * t + 1 + 1) : ∀ k (α : Int × Int),
    nrm α = (3 * t + 1 + 1) ^ (2 * k) → ∃ u, nrm u = 1 ∧ α = (Cyclo3.cyc3 LibInt.intR).mul u (pw (Cyclo3.cyc3 LibInt.intR) (Pi t) k)
  | 0, α, h => ⟨α, h, by show α = (Cyclo3.cyc3 LibInt.intR).mul α (Cyclo3.cyc3 LibInt.intR).one; lib_ring⟩
  | k + 1, α, h => by
    have hdiv : Dvd' (Pi t) ((Cyclo3.cyc3 LibInt.intR).mul α (cj α)) := by
      refine ⟨(Int.ofNat ((3 * t + 1 + 1) * (3 * t + 1 + 1) ^ (2 * k)), 0), ?_⟩
      rw [mul_cj, ← nrm_cast, h, nat_pow2]
      apply Prod.ext
      · show Int.ofNat ((3 * t + 1 + 1) * (3 * t + 1 + 1) * (3 * t + 1 + 1) ^ (2 * k)) = Int.ofNat (3 * t + 1 + 1) * Int.ofNat ((3 * t + 1 + 1) * (3 * t + 1 + 1) ^ (2 * k)) + -(0 * 0)
        show Int.ofNat (3 * t + 1 + 1) * Int.ofNat (3 * t + 1 + 1) * Int.ofNat ((3 * t + 1 + 1) ^ (2 * k)) = _
        show _ = Int.ofNat (3 * t + 1 + 1) * (Int.ofNat (3 * t + 1 + 1) * Int.ofNat ((3 * t + 1 + 1) ^ (2 * k))) + -(0 * 0)
        lib_int
      · show (0 : Int) = Int.ofNat (3 * t + 1 + 1) * 0 + 0 * Int.ofNat ((3 * t + 1 + 1) * (3 * t + 1 + 1) ^ (2 * k)) + -(0 * 0); lib_int
    have cancel : ∀ β : Int × Int, nrm ((Cyclo3.cyc3 LibInt.intR).mul (Pi t) β) = (3 * t + 1 + 1) ^ (2 * (k + 1)) → nrm β = (3 * t + 1 + 1) ^ (2 * k) := fun β hb => by
      rw [nrm_mul, nrm_Pi, nat_pow2] at hb
      exact Nat.eq_of_mul_eq_mul_left (Nat.mul_pos (Nat.zero_lt_succ _) (Nat.zero_lt_succ _)) hb
    have hβ : ∃ β, α = (Cyclo3.cyc3 LibInt.intR).mul (Pi t) β := by
      cases inert_div t hpr α (cj α) hdiv with
      | inl hα => exact hα
      | inr hα =>
        obtain ⟨β, hβ⟩ := hα
        exact ⟨cj β, by rw [← cj_Pi t, ← cj_mul, ← hβ, cj_cj]⟩
    obtain ⟨β, hβ⟩ := hβ
    obtain ⟨u, hu, he⟩ := factor_inert hpr k β (cancel β (by rw [← hβ]; exact h))
    refine ⟨u, hu, ?_⟩
    rw [hβ, he]; show _ = (Cyclo3.cyc3 LibInt.intR).mul u ((Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) (Pi t) k) (Pi t)); lib_ring

def nPi : Int × Int := (-Int.ofNat (3 * t + 1 + 1), 0)

theorem prim_nPi : Prim3 (nPi t) := ⟨-Int.ofNat t + -1, 0, by show -Int.ofNat (3 * t + 1 + 1) = _; show -(3 * Int.ofNat t + 1 + 1) = _; lib_int, rfl⟩

theorem pw_neg (k : Nat) : pw (Cyclo3.cyc3 LibInt.intR) (Pi t) k =
    (Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one) k) (pw (Cyclo3.cyc3 LibInt.intR) (nPi t) k) := by
  rw [← Frobenius.pw_mul]
  congr 1
  apply Prod.ext
  · show Int.ofNat (3 * t + 1 + 1) = -1 * -Int.ofNat (3 * t + 1 + 1) + -(-0 * 0); lib_int
  · show (0 : Int) = -1 * 0 + -0 * -Int.ofNat (3 * t + 1 + 1) + -(-0 * 0); lib_int

theorem nrm_neg_one_pw : ∀ k, nrm (pw (Cyclo3.cyc3 LibInt.intR) ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one) k) = 1
  | 0 => rfl
  | k + 1 => by
    show nrm ((Cyclo3.cyc3 LibInt.intR).mul _ _) = 1
    rw [nrm_mul, nrm_neg_one_pw k]; rfl

/-- **The primary elements of norm p^(2k) are (−p)^k.** -/
theorem prim_inert (hpr : ∀ d, Dv d (3 * t + 1 + 1) → d = 1 ∨ d = 3 * t + 1 + 1) (k : Nat) (α : Int × Int)
    (hα : nrm α = (3 * t + 1 + 1) ^ (2 * k)) (pα : Prim3 α) : α = pw (Cyclo3.cyc3 LibInt.intR) (nPi t) k := by
  obtain ⟨u, hu, he⟩ := factor_inert t hpr k α hα
  rw [pw_neg] at he
  generalize hv : (Cyclo3.cyc3 LibInt.intR).mul u (pw (Cyclo3.cyc3 LibInt.intR) ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one) k) = v
  have he' : α = (Cyclo3.cyc3 LibInt.intR).mul v (pw (Cyclo3.cyc3 LibInt.intR) (nPi t) k) := by
    rw [he, ← hv]; lib_ring
  have hv1 : nrm v = 1 := by rw [← hv, nrm_mul, hu, nrm_neg_one_pw]
  have v1 := unit_one3 v _ (nrm_one_eq v hv1) (prim3_pow _ (prim_nPi t) k) (by rw [← he']; exact pα)
  rw [he', v1]; lib_ring

theorem nrm_nPi : nrm (nPi t) = (3 * t + 1 + 1) * (3 * t + 1 + 1) := by
  apply Int.ofNat.inj; rw [nrm_cast]
  show -Int.ofNat (3 * t + 1 + 1) * -Int.ofNat (3 * t + 1 + 1) + -(-Int.ofNat (3 * t + 1 + 1) * 0) + 0 * 0 = Int.ofNat (3 * t + 1 + 1) * Int.ofNat (3 * t + 1 + 1)
  lib_int

theorem nrm_nPi_pw (k : Nat) : nrm (pw (Cyclo3.cyc3 LibInt.intR) (nPi t) k) = (3 * t + 1 + 1) ^ (2 * k) := by
  rw [HeckePrime3.nrm_pw, nrm_nPi]
  induction k with
  | zero => rfl
  | succ k ih =>
    show ((3 * t + 1 + 1) * (3 * t + 1 + 1)) ^ k * ((3 * t + 1 + 1) * (3 * t + 1 + 1)) = _
    rw [ih, nat_pow2]; lib_nat

/-- **c_W(p^(2k)) = W((−p)^k)** at an inert prime. -/
theorem cw3_even (W : Int × Int → Int × Int) (hpr : ∀ d, Dv d (3 * t + 1 + 1) → d = 1 ∨ d = 3 * t + 1 + 1) (k : Nat) :
    cw3 W ((3 * t + 1 + 1) ^ (2 * k)) = W (pw (Cyclo3.cyc3 LibInt.intR) (nPi t) k) := by
  have hPD : PD3 ((3 * t + 1 + 1) ^ (2 * k)) (pw (Cyclo3.cyc3 LibInt.intR) (nPi t) k) :=
    ⟨by rw [← nrm_cast, nrm_nPi_pw], prim3_pow _ (prim_nPi t) k⟩
  unfold cw3
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun z => (Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) z (pw (Cyclo3.cyc3 LibInt.intR) (nPi t) k)) (W z)) (fun z => by
    by_cases hz : PD3 ((3 * t + 1 + 1) ^ (2 * k)) z
    · have e := prim_inert t hpr k z (by unfold nrm; rw [hz.1]; rfl) hz.2
      rw [I3_pos _ _ hz]; unfold delta; rw [ite_pos' _ _ e]
    · rw [I3_neg _ _ hz]; unfold delta
      rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact hPD))])]
  have hb := abs_le3 _ _ hPD.1
  exact HeckeMult.pick_box (Cyclo3.cyc3 LibInt.intR) W _ _ hb.1 hb.2

theorem pow_odd : ∀ k, ∃ K : Int, Int.ofNat ((3 * t + 1 + 1) ^ (2 * k + 1)) = 3 * K + 2
  | 0 => ⟨Int.ofNat t, by show Int.ofNat (1 * (3 * t + 1 + 1)) = _; rw [Nat.one_mul]; show 3 * Int.ofNat t + 1 + 1 = _; lib_int⟩
  | k + 1 => by
    obtain ⟨K, hK⟩ := pow_odd k
    refine ⟨3 * K * (3 * Int.ofNat t * Int.ofNat t + 4 * Int.ofNat t + 1) + K + 2 * (3 * Int.ofNat t * Int.ofNat t + 4 * Int.ofNat t + 1), ?_⟩
    rw [show 2 * (k + 1) + 1 = 2 * k + 1 + 1 + 1 by lib_nat]
    show Int.ofNat ((3 * t + 1 + 1) ^ (2 * k + 1)) * Int.ofNat (3 * t + 1 + 1) * Int.ofNat (3 * t + 1 + 1) = _
    rw [hK]; show (3 * K + 2) * (3 * Int.ofNat t + 1 + 1) * (3 * Int.ofNat t + 1 + 1) = _; lib_int

/-- **c_W(p^(2k+1)) = 0** at an inert prime. -/
theorem cw3_odd (W : Int × Int → Int × Int) (k : Nat) : cw3 W ((3 * t + 1 + 1) ^ (2 * k + 1)) = (Cyclo3.cyc3 LibInt.intR).zero := by
  obtain ⟨K, hK⟩ := pow_odd t k
  exact FermatInert3.cw3_zero W _ K hK

end EisInert3

#print axioms EisInert3.en_Pi
#print axioms EisInert3.nrm_Pi
#print axioms EisInert3.cj_Pi
#print axioms EisInert3.not_p
#print axioms EisInert3.dvd_conv
#print axioms EisInert3.split_sq
#print axioms EisInert3.inert_div
#print axioms EisInert3.nat_pow2
#print axioms EisInert3.factor_inert
#print axioms EisInert3.prim_nPi
#print axioms EisInert3.pw_neg
#print axioms EisInert3.nrm_neg_one_pw
#print axioms EisInert3.prim_inert
#print axioms EisInert3.nrm_nPi
#print axioms EisInert3.nrm_nPi_pw
#print axioms EisInert3.cw3_even
#print axioms EisInert3.pow_odd
#print axioms EisInert3.cw3_odd

lang/EisPrim.lean

lean_all/lang/EisPrim.lean

/-
  lang/EisPrim — T50, cycle 613: primary normalisation in ℤ[ω].

  Prim3 z :⇔ z ≡ 1 (mod 3), i.e. a ≡ 1, b ≡ 0 (mod 3) for z = a + bω. This class is closed under products, and for 3 ∤ N(α)
  exactly one of the six units u (±1, ±ω, ±ω²) makes uα primary (existence here):
    no3            3x ∉ {1, 2}
    res3           every integer is 3q, 3q + 1 or 3q − 1                       (GaussEuclid.rnd with M = 2)
    prim3_mul      Prim3 x, Prim3 y ⇒ Prim3 (xy)
    prim3_c3       Prim3 x ⇒ N(x) ≡ 1 (mod 3)
    normalize3     N(α) ≢ 0 (mod 3) ⇒ ∃ u, N(u) = 1 and Prim3 (uα)            (the nine residue classes of (a, b) mod 3)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.EisCoprime

namespace EisPrim

open FTAChain DetChain LibInt EisEuclid

def Prim3 (z : Int × Int) : Prop := ∃ e f : Int, z.1 = 3 * e + 1 ∧ z.2 = 3 * f
def C3 (x : Int) : Prop := ∃ k : Int, x = 3 * k + 1 ∨ x = 3 * k + 2

theorem no3_nat (n : Nat) : 3 * n ≠ 1 ∧ 3 * n ≠ 2 := by
  cases n with
  | zero => exact ⟨fun h => Nat.noConfusion h, fun h => Nat.noConfusion h⟩
  | succ n =>
    rw [show 3 * (n + 1) = 3 * n + 2 + 1 by lib_nat]
    exact ⟨fun h => Nat.noConfusion (Nat.succ.inj h), fun h => Nat.noConfusion (Nat.succ.inj (Nat.succ.inj h))⟩

theorem no3 (x : Int) : 3 * x ≠ 1 ∧ 3 * x ≠ 2 := by
  cases x with
  | ofNat n =>
    exact ⟨fun h => (no3_nat n).1 (Int.ofNat.inj h), fun h => (no3_nat n).2 (Int.ofNat.inj h)⟩
  | negSucc n => exact ⟨fun h => Int.noConfusion h, fun h => Int.noConfusion h⟩

theorem c3_ne (x K : Int) (hc : C3 x) (h : x = 3 * K) : False := by
  obtain ⟨k, hk | hk⟩ := hc
  · exact (no3 (K + -k)).1 (by rw [show 3 * (K + -k) = 3 * K + -(3 * k) by lib_int, ← h, hk]; lib_int)
  · exact (no3 (K + -k)).2 (by rw [show 3 * (K + -k) = 3 * K + -(3 * k) by lib_int, ← h, hk]; lib_int)

theorem res3 (a : Int) : ∃ q, a = 3 * q ∨ a = 3 * q + 1 ∨ a = 3 * q + -1 := by
  obtain ⟨q, r, h, hr⟩ := GaussEuclid.rnd a 2
  refine ⟨q, ?_⟩
  have e : a = 3 * q + r := by rw [h]; show q * 3 + r = 3 * q + r; lib_int
  cases r with
  | ofNat t =>
    cases t with
    | zero => exact Or.inl (by rw [e]; show 3 * q + 0 = 3 * q; lib_int)
    | succ t =>
      cases t with
      | zero => exact Or.inr (Or.inl e)
      | succ t =>
        exfalso
        have : 2 * (t + 2) ≤ 3 := hr
        rw [show 2 * (t + 2) = 2 * t + 1 + 3 by lib_nat] at this
        exact Nat.not_succ_le_self 3 (Nat.le_trans (Nat.add_le_add_right (Nat.zero_le (2 * t)) 4) (Nat.le_trans (Nat.le_of_eq (by lib_nat)) this))
  | negSucc t =>
    cases t with
    | zero => exact Or.inr (Or.inr e)
    | succ t =>
      exfalso
      have : 2 * (t + 2) ≤ 3 := hr
      rw [show 2 * (t + 2) = 2 * t + 1 + 3 by lib_nat] at this
      exact Nat.not_succ_le_self 3 (Nat.le_trans (Nat.add_le_add_right (Nat.zero_le (2 * t)) 4) (Nat.le_trans (Nat.le_of_eq (by lib_nat)) this))

theorem prim3_mul (x y : Int × Int) (hx : Prim3 x) (hy : Prim3 y) : Prim3 ((Cyclo3.cyc3 LibInt.intR).mul x y) := by
  obtain ⟨e, f, h1, h2⟩ := hx
  obtain ⟨g, h, h3, h4⟩ := hy
  refine ⟨3 * e * g + e + g + -(3 * f * h), (3 * e + 1) * h + f * (3 * g + 1) + -(3 * f * h), ?_, ?_⟩
  · show x.1 * y.1 + -(x.2 * y.2) = _; rw [h1, h2, h3, h4]; lib_int
  · show x.1 * y.2 + x.2 * y.1 + -(x.2 * y.2) = _; rw [h1, h2, h3, h4]; lib_int


theorem prim3_c3 (x : Int × Int) (hx : Prim3 x) : C3 (en x) := by
  obtain ⟨e, f, h1, h2⟩ := hx
  refine ⟨3 * e * e + 2 * e + -(3 * e * f) + -f + 3 * f * f, Or.inl ?_⟩
  unfold en; rw [h1, h2]; lib_int

/-- the six units. -/
theorem unit_mul1 (u1 u2 a b : Int) : ((Cyclo3.cyc3 LibInt.intR).mul (u1, u2) (a, b)).1 = u1 * a + -(u2 * b) := rfl
theorem unit_mul2 (u1 u2 a b : Int) : ((Cyclo3.cyc3 LibInt.intR).mul (u1, u2) (a, b)).2 = u1 * b + u2 * a + -(u2 * b) := rfl

/-- **Primary normalisation in ℤ[ω].** -/
theorem normalize3 (α : Int × Int) (hα : C3 (en α)) : ∃ u, en u = 1 ∧ Prim3 ((Cyclo3.cyc3 LibInt.intR).mul u α) := by
  obtain ⟨a, b⟩ := α
  obtain ⟨e, he⟩ := res3 a
  obtain ⟨f, hf⟩ := res3 b
  have hn : en (a, b) = a * a + -(a * b) + b * b := rfl
  rw [hn] at hα
  rcases he with ha | ha | ha <;> rcases hf with hb | hb | hb
  · exact (c3_ne _ (3 * (e * e + -(e * f) + f * f)) hα (by rw [ha, hb]; lib_int)).elim
  · -- (0, 1): u = ω² = (−1, −1)
    exact ⟨(-1, -1), rfl, f + -e, -e, by rw [unit_mul1, ha, hb]; lib_int, by rw [unit_mul2, ha, hb]; lib_int⟩
  · -- (0, −1): u = −ω² = (1, 1)
    exact ⟨(1, 1), rfl, e + -f, e, by rw [unit_mul1, ha, hb]; lib_int, by rw [unit_mul2, ha, hb]; lib_int⟩
  · -- (1, 0): u = 1
    exact ⟨(1, 0), rfl, e, f, by rw [unit_mul1, ha, hb]; lib_int, by rw [unit_mul2, ha, hb]; lib_int⟩
  · -- (1, 1): u = −ω = (0, −1)
    exact ⟨(0, -1), rfl, f, f + -e, by rw [unit_mul1, ha, hb]; lib_int, by rw [unit_mul2, ha, hb]; lib_int⟩
  · exact (c3_ne _ (3 * (e * e + -(e * f) + f * f) + 3 * e + -(3 * f) + 1) hα (by rw [ha, hb]; lib_int)).elim
  · -- (−1, 0): u = −1
    exact ⟨(-1, 0), rfl, -e, -f, by rw [unit_mul1, ha, hb]; lib_int, by rw [unit_mul2, ha, hb]; lib_int⟩
  · exact (c3_ne _ (3 * (e * e + -(e * f) + f * f) + -(3 * e) + 3 * f + 1) hα (by rw [ha, hb]; lib_int)).elim
  · -- (−1, −1): u = ω = (0, 1)
    exact ⟨(0, 1), rfl, -f, e + -f, by rw [unit_mul1, ha, hb]; lib_int, by rw [unit_mul2, ha, hb]; lib_int⟩

end EisPrim

#print axioms EisPrim.no3_nat
#print axioms EisPrim.no3
#print axioms EisPrim.c3_ne
#print axioms EisPrim.res3
#print axioms EisPrim.prim3_mul
#print axioms EisPrim.prim3_c3
#print axioms EisPrim.unit_mul1
#print axioms EisPrim.unit_mul2
#print axioms EisPrim.normalize3

lang/EisPrime3.lean

lean_all/lang/EisPrime3.lean

/-
  lang/EisPrime3 — T50, cycle 616: elements of prime norm are prime in ℤ[ω], and the factorisation above a split prime.

  π ∈ ℤ[ω] with N(π) = p prime:
    eprime_div    π | xy ⇒ π | x ∨ π | y                  (gcd(π, x) has norm 1 or p; EisEuclid.eeuclid)
    cj_mul, cj_cj conjugation is a ring involution
    factor3       N(α) = p^k ⇒ α = u π^j π̄^l, N(u) = 1, j + l = k      (p = ππ̄ | αᾱ, induction on k)
    primary_only3 π, α ≡ 1 (mod 3), N(α) = p^k ⇒ α = π^j π̄^l, j + l = k     (EisUnique3.unit_one3)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.HeckeMult3

namespace EisPrime3

open FTAChain DetChain LibInt EisEuclid EisPrim EisUnique3 Frobenius

variable (P : Nat)

theorem nrm_mul (x y : Int × Int) : nrm ((Cyclo3.cyc3 LibInt.intR).mul x y) = nrm x * nrm y :=
  Int.ofNat.inj (by show Int.ofNat _ = Int.ofNat (nrm x) * Int.ofNat (nrm y); rw [nrm_cast, nrm_cast, nrm_cast, en_mul])

theorem unit_inv (u : Int × Int) (h : nrm u = 1) : (Cyclo3.cyc3 LibInt.intR).mul u (cj u) = (Cyclo3.cyc3 LibInt.intR).one := by
  rw [mul_cj, ← nrm_cast, h]; rfl

/-- **An element of prime norm is prime.** -/
theorem eprime_div (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (x y : Int × Int)
    (h : Dvd' π ((Cyclo3.cyc3 LibInt.intR).mul x y)) : Dvd' π x ∨ Dvd' π y := by
  match egcd_exists _ π x (Nat.le_refl _) with
  | ⟨d, X, Y, hd, ⟨c1, hc1⟩, ⟨c2, hc2⟩⟩ =>
    have hdc : nrm d * nrm c1 = P + 1 + 1 := by rw [← nrm_mul, ← hc1, hπ]
    cases hp (nrm d) ⟨nrm c1, by rw [← hdc]; lib_nat⟩ with
    | inl h1 =>
      refine Or.inr (eeuclid π x y ((Cyclo3.cyc3 LibInt.intR).mul (cj d) X) ((Cyclo3.cyc3 LibInt.intR).mul (cj d) Y) ?_ h)
      rw [show (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (cj d) X) π) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (cj d) Y) x)
          = (Cyclo3.cyc3 LibInt.intR).mul (cj d) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul X π) ((Cyclo3.cyc3 LibInt.intR).mul Y x)) by lib_ring,
        ← hd, (Cyclo3.cyc3 LibInt.intR).mul_comm, unit_inv d h1]
    | inr h1 =>
      have nc : nrm c1 = 1 := by
        have e : (P + 1 + 1) * nrm c1 = (P + 1 + 1) * 1 := by rw [← h1, hdc, h1]; lib_nat
        exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) e
      refine Or.inl ⟨(Cyclo3.cyc3 LibInt.intR).mul (cj c1) c2, ?_⟩
      have hd' : d = (Cyclo3.cyc3 LibInt.intR).mul π (cj c1) := by
        rw [hc1]
        calc d = (Cyclo3.cyc3 LibInt.intR).mul d (Cyclo3.cyc3 LibInt.intR).one := by lib_ring
          _ = _ := by rw [← unit_inv c1 nc]; lib_ring
      rw [hc2, hd']; lib_ring

theorem cj_mul (x y : Int × Int) : cj ((Cyclo3.cyc3 LibInt.intR).mul x y) = (Cyclo3.cyc3 LibInt.intR).mul (cj x) (cj y) := by
  apply Prod.ext
  · show (x.1 * y.1 + -(x.2 * y.2)) + -(x.1 * y.2 + x.2 * y.1 + -(x.2 * y.2)) = (x.1 + -x.2) * (y.1 + -y.2) + -(-x.2 * -y.2)
    lib_int
  · show -(x.1 * y.2 + x.2 * y.1 + -(x.2 * y.2)) = (x.1 + -x.2) * -y.2 + -x.2 * (y.1 + -y.2) + -(-x.2 * -y.2)
    lib_int

theorem cj_cj (z : Int × Int) : cj (cj z) = z := by
  apply Prod.ext
  · show (z.1 + -z.2) + - -z.2 = z.1; lib_int
  · show - -z.2 = z.2; lib_int

theorem nrm_cj (z : Int × Int) : nrm (cj z) = nrm z := by unfold nrm; rw [en_cj]

theorem nrm_one_eq (z : Int × Int) (h : nrm z = 1) : en z = 1 := by rw [← nrm_cast, h]; rfl

/-- **Factorisation above a split prime.** -/
theorem factor3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) : ∀ k (α : Int × Int),
    nrm α = (P + 1 + 1) ^ k → ∃ u j l, nrm u = 1 ∧ j + l = k ∧
      α = (Cyclo3.cyc3 LibInt.intR).mul u ((Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) π j) (pw (Cyclo3.cyc3 LibInt.intR) (cj π) l))
  | 0, α, h => ⟨α, 0, 0, h, rfl, by show α = (Cyclo3.cyc3 LibInt.intR).mul α ((Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).one (Cyclo3.cyc3 LibInt.intR).one); lib_ring⟩
  | k + 1, α, h => by
    -- π | α ᾱ
    have hdiv : Dvd' π ((Cyclo3.cyc3 LibInt.intR).mul α (cj α)) := by
      refine ⟨(Cyclo3.cyc3 LibInt.intR).mul (cj π) (Int.ofNat ((P + 1 + 1) ^ k), 0), ?_⟩
      rw [mul_cj, ← nrm_cast, h]
      rw [show (Cyclo3.cyc3 LibInt.intR).mul π ((Cyclo3.cyc3 LibInt.intR).mul (cj π) (Int.ofNat ((P + 1 + 1) ^ k), 0)) =
        (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul π (cj π)) (Int.ofNat ((P + 1 + 1) ^ k), 0) by lib_ring, mul_cj, ← nrm_cast, hπ]
      apply Prod.ext
      · show Int.ofNat ((P + 1 + 1) ^ k * (P + 1 + 1)) = Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ k) + -(0 * 0)
        show Int.ofNat ((P + 1 + 1) ^ k) * Int.ofNat (P + 1 + 1) = _; lib_int
      · show (0 : Int) = Int.ofNat (P + 1 + 1) * 0 + 0 * Int.ofNat ((P + 1 + 1) ^ k) + -(0 * 0); lib_int
    have cancel : ∀ β : Int × Int, nrm ((Cyclo3.cyc3 LibInt.intR).mul π β) = (P + 1 + 1) ^ (k + 1) → nrm β = (P + 1 + 1) ^ k := fun β hb => by
      rw [nrm_mul, hπ] at hb
      exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) (hb.trans (Nat.mul_comm ((P + 1 + 1) ^ k) (P + 1 + 1)))
    cases eprime_div P hp π hπ α (cj α) hdiv with
    | inl hα =>
      obtain ⟨β, hβ⟩ := hα
      obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 hp π hπ k β (cancel β (by rw [← hβ]; exact h))
      refine ⟨u, j + 1, l, hu, by rw [← hjl]; lib_nat, ?_⟩
      rw [hβ, he]; show _ = (Cyclo3.cyc3 LibInt.intR).mul u ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) π j) π) _); lib_ring
    | inr hα =>
      obtain ⟨β, hβ⟩ := hα
      have hα' : α = (Cyclo3.cyc3 LibInt.intR).mul (cj π) (cj β) := by rw [← cj_mul, ← hβ, cj_cj]
      have hb : nrm (cj β) = (P + 1 + 1) ^ k := by
        rw [nrm_cj]; apply cancel
        rw [← hβ, nrm_cj]; exact h
      obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 hp π hπ k (cj β) hb
      refine ⟨u, j, l + 1, hu, by rw [← hjl]; lib_nat, ?_⟩
      rw [hα', he]; show _ = (Cyclo3.cyc3 LibInt.intR).mul u ((Cyclo3.cyc3 LibInt.intR).mul _ ((Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) (cj π) l) (cj π))); lib_ring

theorem prim3_cj (z : Int × Int) (h : Prim3 z) : Prim3 (cj z) := by
  obtain ⟨e, f, h1, h2⟩ := h
  exact ⟨e + -f, -f, by show z.1 + -z.2 = _; rw [h1, h2]; lib_int, by show -z.2 = _; rw [h2]; lib_int⟩

theorem prim3_one : Prim3 (Cyclo3.cyc3 LibInt.intR).one := ⟨0, 0, rfl, rfl⟩

theorem prim3_pow (z : Int × Int) (h : Prim3 z) : ∀ j, Prim3 (pw (Cyclo3.cyc3 LibInt.intR) z j)
  | 0 => prim3_one
  | j + 1 => prim3_mul _ _ (prim3_pow z h j) h

/-- **The primary elements of norm p^k are the π^j π̄^l.** -/
theorem primary_only3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim3 π)
    (k : Nat) (α : Int × Int) (hα : nrm α = (P + 1 + 1) ^ k) (pα : Prim3 α) :
    ∃ j l, j + l = k ∧ α = (Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) π j) (pw (Cyclo3.cyc3 LibInt.intR) (cj π) l) := by
  obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 P hp π hπ k α hα
  have pw' := prim3_mul _ _ (prim3_pow π pπ j) (prim3_pow (cj π) (prim3_cj π pπ) l)
  have u1 := unit_one3 u _ (nrm_one_eq u hu) pw' (by rw [← he]; exact pα)
  exact ⟨j, l, hjl, by rw [he, u1]; lib_ring⟩

end EisPrime3

#print axioms EisPrime3.nrm_mul
#print axioms EisPrime3.unit_inv
#print axioms EisPrime3.eprime_div
#print axioms EisPrime3.cj_mul
#print axioms EisPrime3.cj_cj
#print axioms EisPrime3.nrm_cj
#print axioms EisPrime3.nrm_one_eq
#print axioms EisPrime3.factor3
#print axioms EisPrime3.prim3_cj
#print axioms EisPrime3.prim3_one
#print axioms EisPrime3.prim3_pow
#print axioms EisPrime3.primary_only3

lang/EisRecA.lean

lean_all/lang/EisRecA.lean

/-
  lang/EisRecA — T49 (every odd prime ℓ, symbol form): **Eisenstein's reciprocity for the primary element A**.
  A = Π_(i ≤ ℓ−2) J(χ^i, χ)·χ(−1) ∈ ℤ[C_ℓ] (primary, lang/CycPrim), E = 𝔽_q[C_ℓ]/(N), u = [σ].
  x = q^(−e) ∈ 𝔽_p. If the symbol of x at P_c is u^(k_c), i.e. (z^c)^(k_c) = x^((p−1)/ℓ) (z = g^((p−1)/ℓ)),
  and v_c + c + 1 = ℓ (v_c = v_(P_c)(A), lang/ValC5.A_val_all), then
    eis_recip_A   φ_u(A)^M = u^(Σ_c k_c v_c) = Π_c (u^(k_c))^(v_c)        (q^e = ℓM + 1, (q − 1) | M)
  i.e. (A/𝔮)_ℓ = (x/A)_ℓ with (x/A)_ℓ := Π_c (x/P_c)^(v_(P_c)(A)): lang/CycFq.cyc_eis_fq + lang/SymPsi.psi_stick.
  Lean 4 core only, no axioms.
-/
import lang.SymPsi
import lang.CycFq

namespace EisRecA

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob CharSumGen

/-- **Eisenstein's reciprocity for A (symbol form)**: φ_u(A)^M = u^(Σ_c k_c v_(P_c)(A)). -/
theorem eis_recip_A (j : Nat) (g : Zp (j + 1 + 1)) (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (m t : Nat) (hℓ : ∀ d, Dv d (m + 1 + 1 + 1) → d = 1 ∨ d = m + 1 + 1 + 1)
    (hN : j + 1 + 1 = (m + 1 + 1 + 1) * (t + 1))
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (e : Nat)
    (hQ : nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (M : Nat) (hqn : (k' + 1 + 1) ^ e = (m + 1 + 1 + 1) * M + 1)
    (hp : nsm (ring (k' + 1)) (j + 1 + 1 + 1) (ring (k' + 1)).one ≠ (ring (k' + 1)).zero) (s : Nat) (hs : M = (k' + 1) * s)
    (k v : Nat → Nat)
    (hk : ∀ c, 1 ≤ c → c < m + 1 + 1 + 1 →
      pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g (t + 1)) c) (k c)
        = pw (ring (j + 1 + 1))
            (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one) (j + 1)) (t + 1))
    (hv : ∀ c, 1 ≤ c → c < m + 1 + 1 + 1 → v c + c + 1 = m + 1 + 1 + 1) :
    pw (CycRing.cyc (ring (k' + 1)) (m + 1 + 1))
        (CycEis.phz (CycRing.cyc (ring (k' + 1)) (m + 1 + 1)) (m + 1 + 1) (CycUnit.u (ring (k' + 1)) (m + 1 + 1))
          (EisBridge.AA (gring LibInt.intR (m + 1 + 1)) j (CycPrim.chi (m + 1 + 1) j g) (m + 1))) M
      = pw (CycRing.cyc (ring (k' + 1)) (m + 1 + 1)) (CycUnit.u (ring (k' + 1)) (m + 1 + 1)) (SymProdW.T k v (m + 1 + 1)) := by
  rw [← CycFq.cyc_eis_fq j g hpr hg (m + 1) t hℓ hN k' hq e hQ M hqn hp s hs]
  exact SymPsi.psi_stick (j + 1) m t g hg hN hpr hℓ (CycRing.cyc (ring (k' + 1)) (m + 1 + 1))
    (CycUnit.u (ring (k' + 1)) (m + 1 + 1)) (CycUnit.u_ell (ring (k' + 1)) (m + 1 + 1)) _
    (DLog.pw_ne_zero (j + 1) hpr _ hQ (j + 1)) k v hk hv

end EisRecA

#print axioms EisRecA.eis_recip_A

lang/EisRecJ.lean

lean_all/lang/EisRecJ.lean

/-
  lang/EisRecJ — T49 (every odd prime ℓ = m + 3, symbol form, no condition on M): **Eisenstein's reciprocity
  for A with the p-factor kept**. Over E = 𝔽_q[C_ℓ]/(N), u = [σ], q^f = ℓM + 1 (no (q − 1) | M):
    eis_recip_J   (φ_u(A)·J(ψ, ψ)·J(ψ̄, ψ̄))^M = u^(Σ_c k_c v_c)
  where (z^c)^(k_c) = (q^(−f))^((p−1)/ℓ) and v_c + c + 1 = ℓ: (A/𝔮)(J/𝔮)(J̄/𝔮) = (q^(−f)/A)_ℓ
  (lang/CycSym2.cyc_recip_J + lang/SymPsi.psi_stick). Test: work/data/t49/eisreca_test.py.
  Lean 4 core only, no axioms.
-/
import lang.SymPsi
import lang.CycSym2

namespace EisRecJ

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob CharSumGen

/-- **Eisenstein's reciprocity for A, p kept as J J̄**. -/
theorem eis_recip_J (j : Nat) (g : Zp (j + 1 + 1)) (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (m t : Nat) (hℓ : ∀ d, Dv d (m + 1 + 1 + 1) → d = 1 ∨ d = m + 1 + 1 + 1)
    (hN : j + 1 + 1 = (m + 1 + 1 + 1) * (t + 1))
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (f : Nat)
    (hQ : nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ f) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (M : Nat) (hqn : (k' + 1 + 1) ^ f = (m + 1 + 1 + 1) * M + 1)
    (k v : Nat → Nat)
    (hk : ∀ c, 1 ≤ c → c < m + 1 + 1 + 1 →
      pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g (t + 1)) c) (k c)
        = pw (ring (j + 1 + 1))
            (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ f) (ring (j + 1 + 1)).one) (j + 1)) (t + 1))
    (hv : ∀ c, 1 ≤ c → c < m + 1 + 1 + 1 → v c + c + 1 = m + 1 + 1 + 1) :
    pw (CycRing.cyc (ring (k' + 1)) (m + 1 + 1))
        ((CycRing.cyc (ring (k' + 1)) (m + 1 + 1)).mul
          (CycEis.phz (CycRing.cyc (ring (k' + 1)) (m + 1 + 1)) (m + 1 + 1) (CycUnit.u (ring (k' + 1)) (m + 1 + 1))
            (EisBridge.AA (gring LibInt.intR (m + 1 + 1)) j (CycPrim.chi (m + 1 + 1) j g) (m + 1)))
          ((CycRing.cyc (ring (k' + 1)) (m + 1 + 1)).mul
            (J2 (CycRing.cyc (ring (k' + 1)) (m + 1 + 1)) j
              (CycChar.psi (j + 1) g (CycRing.cyc (ring (k' + 1)) (m + 1 + 1)) (CycUnit.u (ring (k' + 1)) (m + 1 + 1)))
              (CycChar.psi (j + 1) g (CycRing.cyc (ring (k' + 1)) (m + 1 + 1)) (CycUnit.u (ring (k' + 1)) (m + 1 + 1))))
            (J2 (CycRing.cyc (ring (k' + 1)) (m + 1 + 1)) j
              (fun x => CycChar.psi (j + 1) g (CycRing.cyc (ring (k' + 1)) (m + 1 + 1)) (CycUnit.u (ring (k' + 1)) (m + 1 + 1)) (iv j x))
              (fun x => CycChar.psi (j + 1) g (CycRing.cyc (ring (k' + 1)) (m + 1 + 1)) (CycUnit.u (ring (k' + 1)) (m + 1 + 1)) (iv j x))))) M
      = pw (CycRing.cyc (ring (k' + 1)) (m + 1 + 1)) (CycUnit.u (ring (k' + 1)) (m + 1 + 1)) (SymProdW.T k v (m + 1 + 1)) := by
  rw [← CycSym2.cyc_recip_J (m + 1) j g hpr hg t (Nat.succ_le_succ (Nat.zero_le m)) hℓ hN k' hq f hQ M hqn]
  exact SymPsi.psi_stick (j + 1) m t g hg hN hpr hℓ (CycRing.cyc (ring (k' + 1)) (m + 1 + 1))
    (CycUnit.u (ring (k' + 1)) (m + 1 + 1)) (CycUnit.u_ell (ring (k' + 1)) (m + 1 + 1)) _
    (DLog.pw_ne_zero (j + 1) hpr _ hQ (j + 1)) k v hk hv

end EisRecJ

#print axioms EisRecJ.eis_recip_J

lang/EisRecV.lean

lean_all/lang/EisRecV.lean

/-
  lang/EisRecV — T49 (every odd prime ℓ = m + 3, symbol form): **Eisenstein's reciprocity for A with the
  Stickelberger valuations in one statement**.
    subN, subN_add     truncated subtraction by structural recursion; (b + d) − b = d
    vS_spec            v_c := ℓ − 1 − c satisfies v_c + c + 1 = ℓ (1 ≤ c ≤ ℓ − 1)
    eis_recip_val      (1) ι_(N,c)(A) = P^(v_c)·U, U a unit mod p, for every 1 ≤ c ≤ ℓ − 1   (lang/ValC5)
                       (2) φ_u(A)^M = u^(Σ_c k_c v_c) = Π_c (u^(k_c))^(v_c)              (lang/EisRecA)
  i.e. (A) = Π_c P_c^(v_c) at every P_c above p, and (A/𝔮)_ℓ = (q^(−e)/A)_ℓ with the same exponents v_c.
  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.EisRecA
import lang.ValC5

namespace EisRecV

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob CharSumGen

def subN : Nat → Nat → Nat
  | a, 0 => a
  | 0, _ + 1 => 0
  | a + 1, b + 1 => subN a b

theorem subN_zero : ∀ a, subN a 0 = a
  | 0 => rfl
  | _ + 1 => rfl

theorem subN_add : ∀ b d, subN (b + d) b = d
  | 0, d => by
    have e : 0 + d = d := by lib_nat
    rw [e, subN_zero]
  | b + 1, d => by
    have e : b + 1 + d = (b + d) + 1 := by lib_nat
    rw [e]
    exact subN_add b d

theorem add_rc (a b : Nat) : ∀ c, a + c = b + c → a = b
  | 0, h => h
  | c + 1, h => add_rc a b c (Nat.succ.inj h)

/-- v_c := ℓ − 1 − c (ℓ = kk + 2). -/
def vS (kk c : Nat) : Nat := subN (kk + 1) c

/-- the Stickelberger exponent below ℓ − 1: c' + d = kk ⇒ v_(c'+1) = d. -/
theorem vS_eq (kk c' d : Nat) (h : c' + d = kk) : vS kk (c' + 1) = d := by
  show subN kk c' = d
  rw [← h]; exact subN_add c' d

theorem vS_spec (kk : Nat) : ∀ c, 1 ≤ c → c < kk + 1 + 1 → vS kk c + c + 1 = kk + 1 + 1
  | 0, h1, _ => nomatch h1
  | c' + 1, _, h2 => by
    obtain ⟨d, hd⟩ := SymProdZ.le_ex (Nat.le_of_succ_le_succ (Nat.le_of_succ_le_succ h2))
    rw [vS_eq kk c' d hd, ← hd]
    lib_nat

/-- **Eisenstein's reciprocity for A with its valuations**: (1) v_(P_c)(A) = ℓ − 1 − c for every c, and
  (2) φ_u(A)^Me = u^(Σ_c k_c (ℓ − 1 − c)). -/
theorem eis_recip_val (j Mp kp J₂ k₂ N'' : Nat) (hk : Mp + 1 = (j + 1 + 1 + 1) * kp) (hk₂ : Mp + 1 = (J₂ + 1 + 1) * k₂)
    (hM₂ : J₂ + 1 + 1 = (j + 1 + 1 + 1) * (j + 1 + 1 + 1))
    (hN0 : pw (ring Mp) (nsm (ring Mp) (j + 1 + 1 + 1) (ring Mp).one) (N'' + 1 + 1) = (ring Mp).zero)
    (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (g : Zp (j + 1 + 1)) (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (m t : Nat) (hℓ : ∀ d, Dv d (m + 1 + 1 + 1) → d = 1 ∨ d = m + 1 + 1 + 1)
    (hN : j + 1 + 1 = (m + 1 + 1 + 1) * (t + 1))
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (e : Nat)
    (hQ : nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (Me : Nat) (hqn : (k' + 1 + 1) ^ e = (m + 1 + 1 + 1) * Me + 1)
    (hp : nsm (ring (k' + 1)) (j + 1 + 1 + 1) (ring (k' + 1)).one ≠ (ring (k' + 1)).zero) (s : Nat) (hs : Me = (k' + 1) * s)
    (k : Nat → Nat)
    (hkc : ∀ c, 1 ≤ c → c < m + 1 + 1 + 1 →
      pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g (t + 1)) c) (k c)
        = pw (ring (j + 1 + 1))
            (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one) (j + 1)) (t + 1)) :
    (∀ c' f, f + 1 = (t + 1) * (c' + 1) → c' + 1 + 1 ≤ m + 1 + 1 + 1 →
      ∃ U, CycEis.phz (ring Mp) (m + 1 + 1) (TeichN.psiN (j + 1) Mp (N'' + 1) (f + 1) g)
          (EisBridge.AA (gring LibInt.intR (m + 1 + 1)) j (CycPrim.chi (m + 1 + 1) j g) (m + 1)) =
          (ring Mp).mul (pw (ring Mp) (nsm (ring Mp) (j + 1 + 1 + 1) (ring Mp).one) (vS (m + 1) (c' + 1))) U ∧
        RedP2.red (j + 1) Mp U ≠ (ring (j + 1 + 1)).zero) ∧
    pw (CycRing.cyc (ring (k' + 1)) (m + 1 + 1))
        (CycEis.phz (CycRing.cyc (ring (k' + 1)) (m + 1 + 1)) (m + 1 + 1) (CycUnit.u (ring (k' + 1)) (m + 1 + 1))
          (EisBridge.AA (gring LibInt.intR (m + 1 + 1)) j (CycPrim.chi (m + 1 + 1) j g) (m + 1))) Me
      = pw (CycRing.cyc (ring (k' + 1)) (m + 1 + 1)) (CycUnit.u (ring (k' + 1)) (m + 1 + 1))
          (SymProdW.T k (vS (m + 1)) (m + 1 + 1)) := by
  refine ⟨fun c' f hf hcl => ?_, EisRecA.eis_recip_A j g hpr hg m t hℓ hN k' hq e hQ Me hqn hp s hs k (vS (m + 1)) hkc
    (vS_spec (m + 1))⟩
  obtain ⟨v, U, hv, hU, hU0⟩ := ValC5.A_val_all j Mp kp J₂ k₂ N'' hk hk₂ hM₂ hN0 hpr g hg (m + 1) t c' f hN hf hcl hℓ
  obtain ⟨d, hd⟩ := SymProdZ.le_ex (Nat.le_of_succ_le_succ (Nat.le_of_succ_le_succ hcl))
  have hvd : v = d := add_rc v d c' (by rw [hv, ← hd]; lib_nat)
  refine ⟨U, ?_, hU0⟩
  rw [vS_eq (m + 1) c' d hd, ← hvd]
  exact hU

end EisRecV

#print axioms EisRecV.subN_zero
#print axioms EisRecV.subN_add
#print axioms EisRecV.add_rc
#print axioms EisRecV.vS_eq
#print axioms EisRecV.vS_spec
#print axioms EisRecV.eis_recip_val

lang/EisRho.lean

lean_all/lang/EisRho.lean

/-
  lang/EisRho — T49 (general ℓ ≥ 5): **Eisenstein's reciprocity read in a residue field**. For a primary prime
  element α of norm p and any ring map ρ : 𝔽_q[C_ℓ]/(N) → F, if s_i is the symbol of φ_u(α)^M at the conjugate
  ρ∘τ(g^i) (i.e. ρ(τ(g^i)(φ_u(α)^M)) = ρ(τ(g^i) u)^(s_i)), then
    ρ(u)^(Σ_(i<ℓ−1) s_i) = ρ(u)^(k_1 (ℓ−1)),      u^(k_1) = (q^(−e)/α)_ℓ:
  the product of the ℓ-th power symbols of α at all the primes above q (each counted f times) is (q^(−e)/α)^(ℓ−1).
    eis_rho
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.EisAlphaFin
import lang.SymRho

namespace EisRho

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

/-- **the symbols of α at the conjugate primes multiply to (q^(−e)/α)^(ℓ−1)**. -/
theorem eis_rho (j Mp ms t h' : Nat) (hM : Mp + 1 = (j + 1 + 1 + 1) ^ (ms + 1 + 1))
    (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (g : Zp (j + 1 + 1)) (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (hℓ : ∀ d, Dv d (ms + 1 + 1 + 1 + 1) → d = 1 ∨ d = ms + 1 + 1 + 1 + 1)
    (hN : j + 1 + 1 = (ms + 1 + 1 + 1 + 1) * (t + 1)) (hh : ms + 1 + 1 + 1 = (h' + 1) + (h' + 1))
    (α : V Int (ms + 1 + 1 + 1 + 1)) (iv : Zp (ms + 1 + 1 + 1) → Zp (ms + 1 + 1 + 1))
    (hNα : NormEval.nrm (ms + 1 + 1 + 1) α
        (BbarVal.nlist (ms + 1 + 1 + 1) iv (StickQs.qsL (ms + 1 + 1) 0 (ms + 1 + 1) (h' + 1)))
      = nsm (gring LibInt.intR (ms + 1 + 1 + 1)) (j + 1 + 1 + 1) (gring LibInt.intR (ms + 1 + 1 + 1)).one)
    (h0 : RedP2.red (j + 1) Mp (ConjUnit.Y Mp (ms + 1 + 1 + 1) (TeichN.psiN (j + 1) Mp (ms + 1) (t + 1) g) α
        (ring (ms + 1 + 1 + 1)).one) = (ring (j + 1 + 1)).zero)
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (hq2 : k' + 1 + 1 ≠ 2) (e : Nat)
    (hQ : nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (M : Nat) (hqn : (k' + 1 + 1) ^ e = (ms + 1 + 1 + 1 + 1) * M + 1)
    (hp : nsm (ring (k' + 1)) (j + 1 + 1 + 1) (ring (k' + 1)).one ≠ (ring (k' + 1)).zero) (s : Nat) (hs : M = (k' + 1) * s)
    (k : Nat → Nat)
    (hk : ∀ c, 1 ≤ c → c < ms + 1 + 1 + 1 + 1 →
      pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g (t + 1)) c) (k c)
        = pw (ring (j + 1 + 1))
            (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one) (j + 1)) (t + 1))
    (gl : Zp (ms + 1 + 1 + 1)) (hgl : OrdMul.Ord (ring (ms + 1 + 1 + 1)) gl (ms + 1 + 1 + 1))
    (hc0 : nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one ≠ (ring (ms + 1 + 1 + 1)).zero)
    (hprim : PrimAlpha1.Prim (ms + 1 + 1) α)
    {T : Type} (F : CRing T) (ρ : CycRing.C (ring (k' + 1)) (ms + 1 + 1 + 1) → T)
    (hmul : ∀ x y, ρ ((CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)).mul x y) = F.mul (ρ x) (ρ y))
    (hone : ρ (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)).one = F.one) (sy : Nat → Nat)
    (hsy : ∀ i, ρ (CycTau.tau (ring (k' + 1)) (ms + 1 + 1 + 1) (SymG.dI (ms + 1 + 1) gl i)
        (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
          (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) α) M))
      = pw F (ρ (CycTau.tau (ring (k' + 1)) (ms + 1 + 1 + 1) (SymG.dI (ms + 1 + 1) gl i) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)))) (sy i)) :
    pw F (ρ (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1))) (SymRho.sumN sy (ms + 1 + 1 + 1))
      = pw F (ρ (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1))) ((1 * k 1) * (ms + 1 + 1 + 1)) := by
  have h1 := SymRho.rho_G (ring (k' + 1)) (ms + 1 + 1) gl hgl.1 F ρ hmul hone _ sy hsy
  rw [EisAlphaFin.eis_alpha_fin j Mp ms t h' hM hpr g hg hℓ hN hh α iv hNα h0 k' hq hq2 e hQ M hqn hp s hs k hk gl hgl hc0 hprim] at h1
  rw [CycTau.hom_pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) F ρ hmul hone,
    CycTau.hom_pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) F ρ hmul hone, TwoPower.pwpow] at h1
  exact h1.symm

end EisRho

#print axioms EisRho.eis_rho

lang/EisRhoF.lean

lean_all/lang/EisRhoF.lean

/-
  lang/EisRhoF — T49 (symbols at a prime, step 2): **Eisenstein's reciprocity at the primes above q, with the
  symbols computed**. For a ring map ρ : E → F into a ring without zero divisors with decidable equality and
  ρ(u) ≠ 1 (F the residue field of a prime 𝔮 above q), every ℓ-th root of unity ρ(τ(g^i) Y) is a power
  ρ(τ(g^i) u)^(s_i), with s_i found by a decidable search (no choice), and for a primary prime α of norm p
    ρ(u)^(Σ_(i<ℓ−1) s_i) = ρ(u)^(k_1 (ℓ−1)),      u^(k_1) = (q^(−e)/α)_ℓ.
    tau_pw_one   Y^ℓ = 1 ⇒ ρ(τ(d) Y)^ℓ = 1
    tau_u_ne     ρ(u) ≠ 1 ⇒ ρ(τ(d) u) ≠ 1
    syF, sy_spec the symbol s_i of Y at ρ∘τ(g^i), and ρ(τ(g^i) Y) = ρ(τ(g^i) u)^(s_i)
    rho_G_field  ρ(G(Y)) = ρ(u)^(Σ_i s_i)
    eis_rho_field
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.EisRho
import lang.RootPick
import lib.RingTac

namespace EisRhoF

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

section Field

variable {R : Type} (K : CRing R) (n : Nat) (g : Zp (n + 1)) (hg : pw (ring (n + 1)) g (n + 1) = (ring (n + 1)).one)
  {T : Type} (F : CRing T) (ρ : CycRing.C K (n + 1) → T)
  (hmul : ∀ x y, ρ ((CycRing.cyc K (n + 1)).mul x y) = F.mul (ρ x) (ρ y)) (hone : ρ (CycRing.cyc K (n + 1)).one = F.one)
  (hK : NoZD F) (h10 : F.one ≠ F.zero) (dec : DecidableEq T)
  (hpr : ∀ d, Dv d (n + 1 + 1) → d = 1 ∨ d = n + 1 + 1) (hu1 : ρ (CycUnit.u K (n + 1)) ≠ F.one)

include hmul hone in
/-- **Y^ℓ = 1 ⇒ ρ(τ(d) Y)^ℓ = 1**. -/
theorem tau_pw_one (c d : Zp (n + 1)) (hcd : (ring (n + 1)).mul c d = (ring (n + 1)).one) (Y : CycRing.C K (n + 1))
    (hY : pw (CycRing.cyc K (n + 1)) Y (n + 1 + 1) = (CycRing.cyc K (n + 1)).one) :
    pw F (ρ (CycTau.tau K (n + 1) d Y)) (n + 1 + 1) = F.one := by
  rw [← CycTau.hom_pw (CycRing.cyc K (n + 1)) F ρ hmul hone,
    ← CycTau.hom_pw (CycRing.cyc K (n + 1)) (CycRing.cyc K (n + 1)) (CycTau.tau K (n + 1) d)
      (CycTau.tau_mul K (n + 1) c d hcd) (CycTau.tau_one K (n + 1) c d hcd),
    hY, CycTau.tau_one K (n + 1) c d hcd, hone]

include hmul hone hu1 in
/-- **ρ(u) ≠ 1 ⇒ ρ(τ(d) u) ≠ 1**. -/
theorem tau_u_ne (c d : Zp (n + 1)) (hcd : (ring (n + 1)).mul c d = (ring (n + 1)).one) :
    ρ (CycTau.tau K (n + 1) d (CycUnit.u K (n + 1))) ≠ F.one := fun e => by
  have h := SymRho.rho_tau_u K n F ρ hmul hone c d hcd 1
  rw [e, TwoPower.pwone, TwoPower.pwone] at h
  exact hu1 ((F.one_mul _).symm.trans h.symm)

/-- the symbol of Y at ρ∘τ(g^i). -/
def syF (Y : CycRing.C K (n + 1)) (i : Nat) : Nat :=
  RootPick.fp F dec (ρ (CycTau.tau K (n + 1) (SymG.dI n g i) Y))
    (ρ (CycTau.tau K (n + 1) (SymG.dI n g i) (CycUnit.u K (n + 1)))) (n + 1 + 1)

include hg hmul hone hK h10 hpr hu1 in
/-- **ρ(τ(g^i) Y) = ρ(τ(g^i) u)^(s_i)** for Y^ℓ = 1. -/
theorem sy_spec (Y : CycRing.C K (n + 1))
    (hY : pw (CycRing.cyc K (n + 1)) Y (n + 1 + 1) = (CycRing.cyc K (n + 1)).one) (i : Nat) :
    ρ (CycTau.tau K (n + 1) (SymG.dI n g i) Y)
      = pw F (ρ (CycTau.tau K (n + 1) (SymG.dI n g i) (CycUnit.u K (n + 1)))) (syF K n g F ρ dec Y i) :=
  RootPick.root_pick F dec hK h10 n hpr _
    (tau_pw_one K n F ρ hmul hone _ _ (SymG.cd_one n g hg i) _ (CycUnit.u_ell K (n + 1)))
    (tau_u_ne K n F ρ hmul hone hu1 _ _ (SymG.cd_one n g hg i))
    _ (tau_pw_one K n F ρ hmul hone _ _ (SymG.cd_one n g hg i) Y hY)

include hg hmul hone hK h10 hpr hu1 in
/-- **ρ(G(Y)) = ρ(u)^(Σ_i s_i)** with the computed symbols. -/
theorem rho_G_field (Y : CycRing.C K (n + 1))
    (hY : pw (CycRing.cyc K (n + 1)) Y (n + 1 + 1) = (CycRing.cyc K (n + 1)).one) :
    ρ (SymG.G K n g Y) = pw F (ρ (CycUnit.u K (n + 1))) (SymRho.sumN (syF K n g F ρ dec Y) (n + 1)) :=
  SymRho.rho_G K n g hg F ρ hmul hone Y _ (sy_spec K n g hg F ρ hmul hone hK h10 dec hpr hu1 Y hY)

end Field

/-- **Eisenstein's reciprocity at the primes above q**: the computed symbols s_i of a primary prime α of norm p
    satisfy ρ(u)^(Σ s_i) = ρ(u)^(k_1(ℓ−1)). -/
theorem eis_rho_field (j Mp ms t h' : Nat) (hM : Mp + 1 = (j + 1 + 1 + 1) ^ (ms + 1 + 1))
    (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (g : Zp (j + 1 + 1)) (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (hℓ : ∀ d, Dv d (ms + 1 + 1 + 1 + 1) → d = 1 ∨ d = ms + 1 + 1 + 1 + 1)
    (hN : j + 1 + 1 = (ms + 1 + 1 + 1 + 1) * (t + 1)) (hh : ms + 1 + 1 + 1 = (h' + 1) + (h' + 1))
    (α : V Int (ms + 1 + 1 + 1 + 1)) (iv : Zp (ms + 1 + 1 + 1) → Zp (ms + 1 + 1 + 1))
    (hNα : NormEval.nrm (ms + 1 + 1 + 1) α
        (BbarVal.nlist (ms + 1 + 1 + 1) iv (StickQs.qsL (ms + 1 + 1) 0 (ms + 1 + 1) (h' + 1)))
      = nsm (gring LibInt.intR (ms + 1 + 1 + 1)) (j + 1 + 1 + 1) (gring LibInt.intR (ms + 1 + 1 + 1)).one)
    (h0 : RedP2.red (j + 1) Mp (ConjUnit.Y Mp (ms + 1 + 1 + 1) (TeichN.psiN (j + 1) Mp (ms + 1) (t + 1) g) α
        (ring (ms + 1 + 1 + 1)).one) = (ring (j + 1 + 1)).zero)
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (hq2 : k' + 1 + 1 ≠ 2) (e : Nat)
    (hQ : nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (M : Nat) (hqn : (k' + 1 + 1) ^ e = (ms + 1 + 1 + 1 + 1) * M + 1)
    (hp : nsm (ring (k' + 1)) (j + 1 + 1 + 1) (ring (k' + 1)).one ≠ (ring (k' + 1)).zero) (s : Nat) (hs : M = (k' + 1) * s)
    (k : Nat → Nat)
    (hk : ∀ c, 1 ≤ c → c < ms + 1 + 1 + 1 + 1 →
      pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g (t + 1)) c) (k c)
        = pw (ring (j + 1 + 1))
            (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one) (j + 1)) (t + 1))
    (gl : Zp (ms + 1 + 1 + 1)) (hgl : OrdMul.Ord (ring (ms + 1 + 1 + 1)) gl (ms + 1 + 1 + 1))
    (hc0 : nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one ≠ (ring (ms + 1 + 1 + 1)).zero)
    (hprim : PrimAlpha1.Prim (ms + 1 + 1) α)
    {T : Type} (F : CRing T) (ρ : CycRing.C (ring (k' + 1)) (ms + 1 + 1 + 1) → T)
    (hmul : ∀ x y, ρ ((CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)).mul x y) = F.mul (ρ x) (ρ y))
    (hone : ρ (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)).one = F.one)
    (hK : NoZD F) (h10 : F.one ≠ F.zero) (dec : DecidableEq T)
    (hu1 : ρ (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) ≠ F.one) :
    pw F (ρ (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)))
        (SymRho.sumN (syF (ring (k' + 1)) (ms + 1 + 1) gl F ρ dec
          (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
            (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) α) M))
          (ms + 1 + 1 + 1))
      = pw F (ρ (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1))) ((1 * k 1) * (ms + 1 + 1 + 1)) := by
  have hU := EisAlpha.unit_alpha k' hq (ms + 1 + 1) (j + 1 + 1 + 1) hp α iv h' hNα
  have hcd : (ring (ms + 1 + 1 + 1)).mul (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one)
      (pw (ring (ms + 1 + 1 + 1)) (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one) (ms + 1 + 1))
      = (ring (ms + 1 + 1 + 1)).one := by
    have hf := NthEis.fermat_unit (ms + 1 + 1) hℓ _ hc0
    have h2 : (ring (ms + 1 + 1 + 1)).mul (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one)
        (pw (ring (ms + 1 + 1 + 1)) (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one) (ms + 1 + 1))
        = pw (ring (ms + 1 + 1 + 1)) (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one) (ms + 1 + 1 + 1) := by
      show _ = (ring (ms + 1 + 1 + 1)).mul (pw (ring (ms + 1 + 1 + 1)) (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one) (ms + 1 + 1))
        (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one)
      lib_ring
    exact h2.trans hf
  have hY := SymY.Y_ell (ms + 1 + 1 + 1) k' hq _ _ hcd rfl e (EisAlpha.q_order (ms + 1 + 1) (k' + 1 + 1) e M hqn) M hqn _ hU
  exact EisRho.eis_rho j Mp ms t h' hM hpr g hg hℓ hN hh α iv hNα h0 k' hq hq2 e hQ M hqn hp s hs k hk gl hgl hc0 hprim
    F ρ hmul hone _ (sy_spec (ring (k' + 1)) (ms + 1 + 1) gl hgl.1 F ρ hmul hone hK h10 dec hℓ hu1 _ hY)

end EisRhoF

#print axioms EisRhoF.tau_pw_one
#print axioms EisRhoF.tau_u_ne
#print axioms EisRhoF.sy_spec
#print axioms EisRhoF.rho_G_field
#print axioms EisRhoF.eis_rho_field

lang/EisUnique3.lean

lean_all/lang/EisUnique3.lean

/-
  lang/EisUnique3 — T50, cycle 614: uniqueness and existence of the primary splitting in ℤ[ω].

    unit_one3     N(u) = 1, α and uα ≡ 1 (mod 3) ⇒ u = 1      (u ≡ 1 mod 3 via u·N(α) = (uα)ᾱ; then T = Tr u = 2 − 9N(w) for
                                                            u = 1 + 3w, and T² ≤ 4N(u) = 4 forces N(w) = 0)
    prim3_quot    x, xy ≡ 1 (mod 3), 3 ∤ N(y) ⇒ y ≡ 1 (mod 3)
    uniq_split3   αβ = α'β', α, α' ≡ 1 (mod 3), N(α) = N(α') = M, N(β') = N, sM + tN = 1 ⇒ α = α', β = β'
    prim3_split   γ ≡ 1 (mod 3), N(γ) = mn coprime, 3 ∤ m, n ⇒ γ = αβ, α, β ≡ 1 (mod 3), N(α) = m, N(β) = n
  The ℤ[ω] analogue of GaussUnique (with EisCoprime, EisPrim).
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.EisPrim

namespace EisUnique3

open FTAChain DetChain LibInt EisEuclid EisPrim

theorem big81 (c : Nat) (T : Nat) (hT : T ≤ 4) (h : T + 36 * (c + 1) = 4 + 81 * ((c + 1) * (c + 1))) : False := by
  have h1 : T + 36 * (c + 1) ≤ 4 + 36 * (c + 1) := Nat.add_le_add_right hT _
  have h2 : 4 + 81 * ((c + 1) * (c + 1)) = (4 + 36 * (c + 1)) + (45 + (81 * (c * c) + 126 * c)) := by lib_nat
  rw [h, h2] at h1
  exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (Nat.lt_add_of_pos_right (Nat.lt_of_lt_of_le (Nat.zero_lt_succ 44) (Nat.le_add_right 45 _))) h1)

/-- a unit ≡ 1 (mod 3) is 1. -/
theorem unit_three (w1 w2 : Int) (hu : en (3 * w1 + 1, 3 * w2) = 1) : w1 = 0 ∧ w2 = 0 := by
  obtain ⟨c, hc, _⟩ := en_rep (w1, w2)
  have hT : (2 * (3 * w1 + 1) + -(3 * w2)) = 2 + -(9 * Int.ofNat c) := by
    rw [← hc]
    have e1 : en (3 * w1 + 1, 3 * w2) = 1 + (6 * w1 + -(3 * w2)) + 9 * en (w1, w2) := by
      show (3 * w1 + 1) * (3 * w1 + 1) + -((3 * w1 + 1) * (3 * w2)) + 3 * w2 * (3 * w2) = 1 + (6 * w1 + -(3 * w2)) + 9 * (w1 * w1 + -(w1 * w2) + w2 * w2)
      lib_int
    rw [hu] at e1
    calc (2 * (3 * w1 + 1) + -(3 * w2)) = 2 + (6 * w1 + -(3 * w2)) := by lib_int
      _ = 2 + ((1 + (6 * w1 + -(3 * w2)) + 9 * en (w1, w2)) + -(1 + 9 * en (w1, w2))) := by lib_int
      _ = 2 + -(9 * en (w1, w2)) := by rw [← e1]; lib_int
  have h4 : Int.ofNat (Int.natAbs (2 * (3 * w1 + 1) + -(3 * w2)) * Int.natAbs (2 * (3 * w1 + 1) + -(3 * w2)) + 3 * (Int.natAbs (3 * w2) * Int.natAbs (3 * w2))) = Int.ofNat 4 := by
    show Int.ofNat (Int.natAbs (2 * (3 * w1 + 1) + -(3 * w2)) * Int.natAbs (2 * (3 * w1 + 1) + -(3 * w2))) + 3 * Int.ofNat (Int.natAbs (3 * w2) * Int.natAbs (3 * w2)) = _
    rw [GaussEuclid.nas, GaussEuclid.nas]
    calc _ = 4 * en (3 * w1 + 1, 3 * w2) := by
          show _ = 4 * ((3 * w1 + 1) * (3 * w1 + 1) + -((3 * w1 + 1) * (3 * w2)) + 3 * w2 * (3 * w2))
          lib_int
      _ = Int.ofNat 4 := by rw [hu]; rfl
  have hle : Int.natAbs (2 * (3 * w1 + 1) + -(3 * w2)) * Int.natAbs (2 * (3 * w1 + 1) + -(3 * w2)) ≤ 4 := Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq (Int.ofNat.inj h4))
  have hsq : Int.ofNat (Int.natAbs (2 * (3 * w1 + 1) + -(3 * w2)) * Int.natAbs (2 * (3 * w1 + 1) + -(3 * w2)) + 36 * c) = Int.ofNat (4 + 81 * (c * c)) := by
    show Int.ofNat (Int.natAbs (2 * (3 * w1 + 1) + -(3 * w2)) * Int.natAbs (2 * (3 * w1 + 1) + -(3 * w2))) + Int.ofNat 36 * Int.ofNat c = Int.ofNat 4 + Int.ofNat 81 * (Int.ofNat c * Int.ofNat c)
    rw [GaussEuclid.nas, hT]
    show (2 + -(9 * Int.ofNat c)) * (2 + -(9 * Int.ofNat c)) + 36 * Int.ofNat c = 4 + 81 * (Int.ofNat c * Int.ofNat c)
    lib_int
  cases c with
  | succ c => exact (big81 c _ hle (Int.ofNat.inj hsq)).elim
  | zero =>
    have hz := nrm_zero (w1, w2) (by unfold nrm; rw [hc]; rfl)
    exact ⟨congrArg Prod.fst hz, congrArg Prod.snd hz⟩

/-- **N(u) = 1 and α, uα ≡ 1 (mod 3) ⇒ u = 1.** -/
theorem unit_one3 (u α : Int × Int) (hu : en u = 1) (pα : Prim3 α) (pu : Prim3 ((Cyclo3.cyc3 LibInt.intR).mul u α)) :
    u = (Cyclo3.cyc3 LibInt.intR).one := by
  obtain ⟨u1, u2⟩ := u
  obtain ⟨a, b⟩ := α
  obtain ⟨e, f, ha, hb⟩ := pα
  obtain ⟨g, h, hg, hh⟩ := pu
  have hg' : u1 * a + -(u2 * b) = 3 * g + 1 := hg
  have hh' : u1 * b + u2 * a + -(u2 * b) = 3 * h := hh
  have ha' : a = 3 * e + 1 := ha
  have hb' : b = 3 * f := hb
  have k1 : u1 * (a * a + -(a * b) + b * b) = (u1 * a + -(u2 * b)) * (a + -b) + (u1 * b + u2 * a + -(u2 * b)) * b := by lib_int
  have k2 : u2 * (a * a + -(a * b) + b * b) = (u1 * b + u2 * a + -(u2 * b)) * a + -((u1 * a + -(u2 * b)) * b) := by lib_int
  rw [hg', hh'] at k1 k2
  rw [ha', hb'] at k1 k2
  generalize hK : 3 * e * e + 2 * e + -(3 * e * f) + -f + 3 * f * f = K
  have hN : (3 * e + 1) * (3 * e + 1) + -((3 * e + 1) * (3 * f)) + 3 * f * (3 * f) = 3 * K + 1 := by rw [← hK]; lib_int
  rw [hN] at k1 k2
  have hu1 : u1 = 3 * ((3 * g + 1) * (e + -f) + g + 3 * h * f + -(K * u1)) + 1 := by
    calc u1 = u1 * (3 * K + 1) + -(3 * (K * u1)) := by lib_int
      _ = _ := by rw [k1]; lib_int
  have hu2 : u2 = 3 * (h * (3 * e + 1) + -((3 * g + 1) * f) + -(K * u2)) := by
    calc u2 = u2 * (3 * K + 1) + -(3 * (K * u2)) := by lib_int
      _ = _ := by rw [k2]; lib_int
  have hu' : en (3 * ((3 * g + 1) * (e + -f) + g + 3 * h * f + -(K * u1)) + 1, 3 * (h * (3 * e + 1) + -((3 * g + 1) * f) + -(K * u2))) = 1 := by
    rw [← hu1, ← hu2]; exact hu
  obtain ⟨z1, z2⟩ := unit_three _ _ hu'
  apply Prod.ext
  · show u1 = 1; rw [hu1, z1]; rfl
  · show u2 = 0; rw [hu2, z2]; rfl

/-- y ≡ 1 (mod 3) when x and xy are (3 ∤ N(y)). -/
theorem prim3_quot (x y : Int × Int) (hy : C3 (en y)) (hx : Prim3 x) (hxy : Prim3 ((Cyclo3.cyc3 LibInt.intR).mul x y)) : Prim3 y := by
  obtain ⟨u, hu, hpu⟩ := normalize3 y hy
  have h1 : Prim3 ((Cyclo3.cyc3 LibInt.intR).mul u ((Cyclo3.cyc3 LibInt.intR).mul x y)) := by
    rw [show (Cyclo3.cyc3 LibInt.intR).mul u ((Cyclo3.cyc3 LibInt.intR).mul x y) = (Cyclo3.cyc3 LibInt.intR).mul x ((Cyclo3.cyc3 LibInt.intR).mul u y) by lib_ring]
    exact prim3_mul _ _ hx hpu
  have hu1 := unit_one3 u _ hu hxy h1
  rw [hu1, (Cyclo3.cyc3 LibInt.intR).one_mul] at hpu
  exact hpu

/-- **Uniqueness of the primary splitting.** -/
theorem uniq_split3 (α β α' β' : Int × Int) (M N s t : Int) (hα : en α = M) (hα' : en α' = M) (hβ' : en β' = N)
    (hst : s * M + t * N = 1) (hM : M ≠ 0) (pα : Prim3 α) (pα' : Prim3 α') (h : (Cyclo3.cyc3 LibInt.intR).mul α β = (Cyclo3.cyc3 LibInt.intR).mul α' β') :
    α = α' ∧ β = β' := by
  have hco : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (EisCoprime.emb s) (cj α)) α) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (EisCoprime.emb t) (cj β')) β') = (Cyclo3.cyc3 LibInt.intR).one := by
    rw [show (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (EisCoprime.emb s) (cj α)) α) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (EisCoprime.emb t) (cj β')) β') = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (EisCoprime.emb s) ((Cyclo3.cyc3 LibInt.intR).mul α (cj α))) ((Cyclo3.cyc3 LibInt.intR).mul (EisCoprime.emb   -- (+59 chars; full line in the Zenodo source)
      mul_cj, mul_cj, hα, hβ']
    show (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (EisCoprime.emb s) (EisCoprime.emb M)) ((Cyclo3.cyc3 LibInt.intR).mul (EisCoprime.emb t) (EisCoprime.emb N)) = _
    rw [EisCoprime.emb_mul, EisCoprime.emb_mul, ← EisCoprime.emb_add, hst]; rfl
  match eeuclid α β' α' _ _ hco ⟨β, by rw [show (Cyclo3.cyc3 LibInt.intR).mul β' α' = (Cyclo3.cyc3 LibInt.intR).mul α' β' by lib_ring, ← h]⟩ with
  | ⟨w, hw⟩ =>
    have hgw : en w = 1 := by
      have e : M * en w = M * 1 := by
        calc M * en w = en α * en w := by rw [hα]
          _ = en α' := by rw [← en_mul, ← hw]
          _ = M * 1 := by rw [hα']; lib_int
      have e0 : M * (en w + -1) = 0 := by rw [show M * (en w + -1) = M * en w + -(M * 1) by lib_int, e]; lib_int
      cases int_noZD _ _ e0 with
      | inl h0 => exact absurd h0 hM
      | inr h0 => calc en w = (en w + -1) + 1 := by lib_int
          _ = 1 := by rw [h0]; rfl
    have hw1 : w = (Cyclo3.cyc3 LibInt.intR).one :=
      unit_one3 w α hgw pα (by rw [show (Cyclo3.cyc3 LibInt.intR).mul w α = α' by rw [hw]; lib_ring]; exact pα')
    have hαα : α = α' := by rw [hw, hw1]; lib_ring
    refine ⟨hαα, ?_⟩
    have h0 : (Cyclo3.cyc3 LibInt.intR).mul α ((Cyclo3.cyc3 LibInt.intR).add β ((Cyclo3.cyc3 LibInt.intR).neg β')) = (Cyclo3.cyc3 LibInt.intR).zero := by
      rw [show (Cyclo3.cyc3 LibInt.intR).mul α ((Cyclo3.cyc3 LibInt.intR).add β ((Cyclo3.cyc3 LibInt.intR).neg β')) = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul α β) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).mul α β')) by lib_ring, h, ← hαα]; lib_ring
    have := EisCoprime.cancel_left α _ (by rw [hα]; exact hM) h0
    calc β = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add β ((Cyclo3.cyc3 LibInt.intR).neg β')) β' := by lib_ring
      _ = β' := by rw [this]; lib_ring

/-- **Existence of the primary splitting.** -/
theorem prim3_split (γ : Int × Int) (m n : Nat) (s t : Int) (hγ : en γ = Int.ofNat ((m + 1) * (n + 1)))
    (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1) (hm : C3 (Int.ofNat (m + 1))) (hn : C3 (Int.ofNat (n + 1)))
    (pγ : Prim3 γ) : ∃ α β, γ = (Cyclo3.cyc3 LibInt.intR).mul α β ∧ Prim3 α ∧ Prim3 β ∧ en α = Int.ofNat (m + 1) ∧ en β = Int.ofNat (n + 1) := by
  match EisCoprime.ecoprime_split γ m n s t hγ hst with
  | ⟨α0, β0, h0, hα0, hβ0⟩ =>
    match normalize3 α0 (by rw [hα0]; exact hm) with
    | ⟨u, gu, pu⟩ =>
      have uc : (Cyclo3.cyc3 LibInt.intR).mul u (cj u) = (Cyclo3.cyc3 LibInt.intR).one := by rw [mul_cj, gu]; rfl
      have hβn : en ((Cyclo3.cyc3 LibInt.intR).mul (cj u) β0) = Int.ofNat (n + 1) := by
        rw [en_mul, show en (cj u) = en u from en_cj u, gu, hβ0]; lib_int
      have hsplit : γ = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul u α0) ((Cyclo3.cyc3 LibInt.intR).mul (cj u) β0) := by
        rw [h0, show (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul u α0) ((Cyclo3.cyc3 LibInt.intR).mul (cj u) β0) = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul u (cj u)) ((Cyclo3.cyc3 LibInt.intR).mul α0 β0) by lib_ring, uc]; lib_ring
      refine ⟨(Cyclo3.cyc3 LibInt.intR).mul u α0, (Cyclo3.cyc3 LibInt.intR).mul (cj u) β0, hsplit, pu,
        prim3_quot _ _ (by rw [hβn]; exact hn) pu (by rw [← hsplit]; exact pγ),
        by rw [en_mul, gu, hα0]; lib_int, hβn⟩

end EisUnique3

#print axioms EisUnique3.big81
#print axioms EisUnique3.unit_three
#print axioms EisUnique3.unit_one3
#print axioms EisUnique3.prim3_quot
#print axioms EisUnique3.uniq_split3
#print axioms EisUnique3.prim3_split

lang/Eisen.lean

lean_all/lang/Eisen.lean

/-
  lang/Eisen — T43, cycle 37 (GL1 for ℚ(√−3) = ℚ(ζ₃)): −3 is a square mod p ⇔ p ≡ 1 (mod 3).

  p = J + 2 prime, p > 3, ℤ/p = ring (J + 1), 3 = 1 + 1 + 1.
    cube_factor     y³ − 1 = (y − 1)(y² + y + 1)
    split_neg3      3 | p − 1 ⇒ ∃ z, z² = −3          (ζ₃ ∈ 𝔽_p by CycloSplit, z = 2ζ₃ + 1)
    inert_neg3      z² = −3 ⇒ 3 | p − 1               (y = (z − 1)/2 has y² + y + 1 = 0, so order 3)
    neg3_iff        the equivalence
  The splitting of p in ℤ[ω] (Eisenstein integers) is the Dirichlet character mod 3 — the Hecke-character input of
  the CM forms of discriminant −3 (y² = x³ + 1, y² = x³ − 432 …), as GaussSplit was for ℤ[i].

  Lean 4 core only, proofs by hand (lib_ring from lib_shared for ring identities), no axioms.
-/
import lang.CycloSplit
import lib.RingTac

namespace Eisen

open FTAChain ZMod

variable (J : Nat)

def three : Zp (J + 1) := (ring (J + 1)).add (ring (J + 1)).one ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one)

theorem cube_factor (y : Zp (J + 1)) :
    (ring (J + 1)).mul ((ring (J + 1)).add y ((ring (J + 1)).neg (ring (J + 1)).one)) ((ring (J + 1)).add ((ring (J + 1)).add ((ring (J + 1)).mul y y) y) (ring (J + 1)).one)
      = (ring (J + 1)).add ((ring (J + 1)).mul ((ring (J + 1)).mul y y) y) ((ring (J + 1)).neg (ring (J + 1)).one) := by lib_ring

theorem pw3 (y : Zp (J + 1)) : Frobenius.pw (ring (J + 1)) y 3 = (ring (J + 1)).mul ((ring (J + 1)).mul y y) y := by
  show (ring (J + 1)).mul ((ring (J + 1)).mul ((ring (J + 1)).mul (ring (J + 1)).one y) y) y = _
  rw [(ring (J + 1)).one_mul]

theorem eq_of_sub_zero (a b : Zp (J + 1)) (h : (ring (J + 1)).add a ((ring (J + 1)).neg b) = (ring (J + 1)).zero) : a = b := by
  have := congrArg (fun z => (ring (J + 1)).add z b) h
  have e : (ring (J + 1)).add ((ring (J + 1)).add a ((ring (J + 1)).neg b)) b = a := by
    rw [(ring (J + 1)).add_assoc, DetChain.add_neg', DetChain.add_zero']
  rw [← e]
  exact this.trans ((ring (J + 1)).zero_add b)

theorem lt3 (h3 : 2 ≤ J) (a : Nat) (ha : a ≤ 3) : a < J + 1 + 1 :=
  Nat.lt_succ_of_le (Nat.le_trans ha (Nat.succ_le_succ h3))

theorem three_val (h3 : 2 ≤ J) : (three J).val = 3 := by
  show md (J + 1) (md (J + 1) 1 + md (J + 1) (md (J + 1) 1 + md (J + 1) 1)) = 3
  have h1 : md (J + 1) 1 = 1 := md_small _ 1 (lt3 J h3 1 (Nat.le_succ_of_le (Nat.le_succ 1)))
  have h2 : md (J + 1) 2 = 2 := md_small _ 2 (lt3 J h3 2 (Nat.le_succ 2))
  rw [h1, h2]
  exact md_small _ 3 (lt3 J h3 3 (Nat.le_refl 3))

theorem three_ne_zero (h3 : 2 ≤ J) : three J ≠ (ring (J + 1)).zero := fun e => by
  have := congrArg Subtype.val e
  rw [three_val J h3] at this
  exact Nat.noConfusion this

theorem two_ne_zero' (h3 : 2 ≤ J) : (ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one ≠ (ring (J + 1)).zero := fun e => by
  have := congrArg Subtype.val e
  have h1 : md (J + 1) 1 = 1 := md_small _ 1 (lt3 J h3 1 (Nat.le_succ_of_le (Nat.le_succ 1)))
  have v : ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one).val = 2 := by
    show md (J + 1) (md (J + 1) 1 + md (J + 1) 1) = 2
    rw [h1]
    exact md_small _ 2 (lt3 J h3 2 (Nat.le_succ 2))
  rw [v] at this
  exact Nat.noConfusion this

theorem prime3 : ∀ d, Dv d (1 + 2) → d = 1 ∨ d = 1 + 2
  | 0, ⟨q, e⟩ => absurd (e.trans (Nat.mul_zero q)) (fun e' => Nat.noConfusion e')
  | 1, _ => Or.inl rfl
  | 2, ⟨q, e⟩ => by
    exfalso
    cases q with
    | zero => exact Nat.noConfusion e
    | succ q =>
      cases q with
      | zero => exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj e))
      | succ q =>
        have a : 2 * 2 ≤ (q + 1 + 1) * 2 := Nat.mul_le_mul_right 2 (Nat.le_add_left 2 q)
        exact Nat.not_succ_le_self 3 (Nat.le_trans a (Nat.le_of_eq e.symm))
  | 3, _ => Or.inr rfl
  | d + 4, ⟨q, e⟩ => by
    exfalso
    cases q with
    | zero => exact Nat.noConfusion (e.trans (Nat.zero_mul _))
    | succ q =>
      have a : d + 4 ≤ (q + 1) * (d + 4) := by rw [Nat.succ_mul]; exact Nat.le_add_left _ _
      have this : d + 4 ≤ 3 := Nat.le_trans a (Nat.le_of_eq e.symm)
      exact Nat.not_succ_le_self 3 (Nat.le_trans (Nat.succ_le_succ (Nat.succ_le_succ (Nat.succ_le_succ (Nat.succ_le_succ (Nat.zero_le d))))) this)

/-- **Split**: 3 | p − 1 ⇒ −3 is a square. -/
theorem split_neg3 (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hd : Dv (1 + 2) (J + 1)) :
    ∃ z : Zp (J + 1), (ring (J + 1)).mul z z = (ring (J + 1)).neg (three J) := by
  match CycloSplit.split_of_dvd J hpr 1 hd with
  | ⟨y, hy1, hy3⟩ =>
    have hc := cube_factor J y
    rw [← pw3, hy3, (ring (J + 1)).add_neg] at hc
    cases nozd J hpr _ _ hc with
    | inl h => exact absurd (eq_of_sub_zero J y (ring (J + 1)).one h) hy1
    | inr h =>
      refine ⟨(ring (J + 1)).add ((ring (J + 1)).add y y) (ring (J + 1)).one, ?_⟩
      apply DetChain.neg_unique
      have e : (ring (J + 1)).add ((ring (J + 1)).mul ((ring (J + 1)).add ((ring (J + 1)).add y y) (ring (J + 1)).one) ((ring (J + 1)).add ((ring (J + 1)).add y y) (ring (J + 1)).one)) (three J)
          = (ring (J + 1)).mul ((ring (J + 1)).add ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one) ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one)) ((ring (J + 1)).add ((ring (J + 1)).add ((ring (J + 1)).mul y y) y) (ring (J + 1)).one) := by
        unfold three; lib_ring
      rw [e, h, DetChain.mul_zero]

/-- **Inert direction**: −3 a square ⇒ 3 | p − 1. -/
theorem inert_neg3 (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (h3 : 2 ≤ J) (z : Zp (J + 1))
    (hz : (ring (J + 1)).mul z z = (ring (J + 1)).neg (three J)) : Dv (1 + 2) (J + 1) := by
  have h2 := two_ne_zero' J h3
  let w := Frobenius.pw (ring (J + 1)) ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one) J
  have hw : (ring (J + 1)).mul w ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one) = (ring (J + 1)).one := inv_exists J hpr _ h2
  let u := (ring (J + 1)).add z ((ring (J + 1)).neg (ring (J + 1)).one)
  let y := (ring (J + 1)).mul u w
  -- u² + 2u + 4 = z² + 3 = 0
  have hu : (ring (J + 1)).add ((ring (J + 1)).add ((ring (J + 1)).mul u u) ((ring (J + 1)).mul ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one) u)) ((ring (J + 1)).add ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one) ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one))
      = (ring (J + 1)).zero := by
    have e : (ring (J + 1)).add ((ring (J + 1)).add ((ring (J + 1)).mul u u) ((ring (J + 1)).mul ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one) u)) ((ring (J + 1)).add ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one) ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one))
        = (ring (J + 1)).add ((ring (J + 1)).mul z z) (three J) := by
      show (ring (J + 1)).add ((ring (J + 1)).add ((ring (J + 1)).mul ((ring (J + 1)).add z ((ring (J + 1)).neg (ring (J + 1)).one)) ((ring (J + 1)).add z ((ring (J + 1)).neg (ring (J + 1)).one)))
        ((ring (J + 1)).mul ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one) ((ring (J + 1)).add z ((ring (J + 1)).neg (ring (J + 1)).one)))) ((ring (J + 1)).add ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one) ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one)) = _
      unfold three; lib_ring
    rw [e, hz, DetChain.add_neg']
  -- y² + y + 1 = w² (u² + 2u + 4)
  have hy0 : (ring (J + 1)).add ((ring (J + 1)).add ((ring (J + 1)).mul y y) y) (ring (J + 1)).one = (ring (J + 1)).zero := by
    have e : (ring (J + 1)).mul ((ring (J + 1)).mul w w) ((ring (J + 1)).add ((ring (J + 1)).add ((ring (J + 1)).mul u u) ((ring (J + 1)).mul ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one) u))
          ((ring (J + 1)).add ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one) ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one)))
        = (ring (J + 1)).add ((ring (J + 1)).add ((ring (J + 1)).mul ((ring (J + 1)).mul u w) ((ring (J + 1)).mul u w)) ((ring (J + 1)).mul ((ring (J + 1)).mul u w) ((ring (J + 1)).mul w ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one))))
            ((ring (J + 1)).mul ((ring (J + 1)).mul w ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one)) ((ring (J + 1)).mul w ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one))) := by lib_ring
    rw [hu, DetChain.mul_zero, hw, (ring (J + 1)).mul_comm ((ring (J + 1)).mul u w) (ring (J + 1)).one, (ring (J + 1)).one_mul, (ring (J + 1)).one_mul] at e
    exact e.symm
  have hy1 : y ≠ (ring (J + 1)).one := fun e => by
    rw [e] at hy0
    apply three_ne_zero J h3
    have : (ring (J + 1)).add ((ring (J + 1)).add ((ring (J + 1)).mul (ring (J + 1)).one (ring (J + 1)).one) (ring (J + 1)).one) (ring (J + 1)).one = three J := by unfold three; lib_ring
    rw [← this]; exact hy0
  have hy3 : Frobenius.pw (ring (J + 1)) y (1 + 2) = (ring (J + 1)).one := by
    rw [pw3]
    apply eq_of_sub_zero
    rw [← cube_factor, hy0, DetChain.mul_zero]
  exact CycloSplit.dvd_of_split J hpr 1 prime3 y hy1 hy3

/-- **−3 is a square mod p ⇔ p ≡ 1 (mod 3)** (p > 3). -/
theorem neg3_iff (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (h3 : 2 ≤ J) :
    (∃ z : Zp (J + 1), (ring (J + 1)).mul z z = (ring (J + 1)).neg (three J)) ↔ Dv (1 + 2) (J + 1) :=
  ⟨fun ⟨z, hz⟩ => inert_neg3 J hpr h3 z hz, split_neg3 J hpr⟩

end Eisen

#print axioms Eisen.cube_factor
#print axioms Eisen.pw3
#print axioms Eisen.eq_of_sub_zero
#print axioms Eisen.lt3
#print axioms Eisen.three_val
#print axioms Eisen.three_ne_zero
#print axioms Eisen.two_ne_zero'
#print axioms Eisen.prime3
#print axioms Eisen.split_neg3
#print axioms Eisen.inert_neg3
#print axioms Eisen.neg3_iff

lang/EisenAp.lean

lean_all/lang/EisenAp.lean

/-
  lang/EisenAp — T43, cycle 155: the exact formula for y² = x³ + 1 at every split p (p ≡ 1 mod 3).

  p = 2m + 3 prime, 3 | p − 1. For ANY x, y with x² + 3y² = p and x ≡ 1 (mod 3):
    three_ne_r, p_ne_three        3k ≠ 1, 2;  3 | p − 1 ⇒ p ≠ 3
    eisen_ap                      Σ_{t ∈ 𝔽_p} χ(t³ + 1) = −2x,   i.e. a_p = 2x
    eisen_exists                  such an x exists
    count_mod6                    6 | #E(𝔽_p) = p + 1 − a_p
  (FinCubicExact: ψ(1) = ±2u with u² + 3w² = p; EisenUnique: x = ±u; FinCubicTrace: ψ(1) ≡ 1 mod 3 fixes the sign.)
  This is a_p = ψ(π) + ψ(π̄) for π = x + y√−3 ≡ 1 mod 3, the Hecke character of ℚ(√−3) attached to y² = x³ + 1,
  with no choice left — the Eisenstein counterpart of GaussAp.

  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.EisenUnique
import lang.FinCubicExact

namespace EisenAp

open FTAChain ZMod YMChain FiniteRecChain

theorem three_ne_r (k : Int) (r : Nat) (hr0 : r ≠ 0) (hr : r < 3) (h : 3 * k = Int.ofNat r) : False := by
  cases k with
  | ofNat q =>
    have e : 3 * q = r := Int.ofNat.inj h
    cases q with
    | zero => exact hr0 e.symm
    | succ q =>
      have : 3 * (q + 1) ≥ 3 := Nat.le_add_left 3 (3 * q)
      rw [e] at this; exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le hr this)
  | negSucc q =>
    have e : (3 : Int) * Int.negSucc q = Int.negSucc (3 * q + 2) := by
      show -(3 * Int.ofNat (q + 1)) = Int.negSucc (3 * q + 2)
      rw [show (3 : Int) * Int.ofNat (q + 1) = Int.ofNat (3 * q + 2 + 1) by
        show Int.ofNat (3 * (q + 1)) = Int.ofNat (3 * q + 2 + 1); rw [show 3 * (q + 1) = 3 * q + 2 + 1 by lib_nat]]
      rfl
    rw [e] at h
    exact Int.noConfusion h

theorem p_ne_three (m N : Nat) (hN : 2 * (m + 1) = 3 * N) : 2 * m + 1 + 1 + 1 ≠ 3 := fun e => by
  have hm : m = 0 := by
    cases m with
    | zero => rfl
    | succ k => exact absurd (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj ((show 2 * (k + 1) + 1 + 1 + 1 = 2 * k + 2 + 1 + 1 + 1 by lib_nat).symm.trans e)))) (fun h => Nat.noConfusion h)
  rw [hm] at hN
  exact three_ne_r (Int.ofNat N) 2 (fun h => Nat.noConfusion h) (Nat.lt_succ_self 2)
    (congrArg Int.ofNat hN.symm)

/-- **y² = x³ + 1: Σ_t χ(t³ + 1) = −2x for every x ≡ 1 (mod 3) with x² + 3y² = p.** -/
theorem eisen_ap (m : Nat) (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (N : Nat)
    (hN : 2 * (m + 1) = 3 * N) (x y : Int) (hxy : x * x + 3 * (y * y) = Int.ofNat (2 * m + 1 + 1 + 1))
    (hx : ∃ s, x = 3 * s + 1) :
    FinCubic.psi (FinField.ffp m hpr) (FinField.ffp m hpr).K.one = -2 * x := by
  match FinCubicExact.fin_cubic_trace (FinField.ffp m hpr) N hN, FinCubicTrace.psi_one_mod3_ff (FinField.ffp m hpr) N hN, hx with
  | ⟨u, w, huw, hpsi⟩, ⟨T, hT⟩, ⟨s, hs⟩ =>
    have huw' : Int.ofNat u * Int.ofNat u + 3 * (Int.ofNat w * Int.ofNat w) = Int.ofNat (2 * m + 1 + 1 + 1) :=
      congrArg Int.ofNat (huw.trans (show 2 * (m + 1) + 1 = 2 * m + 1 + 1 + 1 by lib_nat))
    have hsq := EisenUnique.eisen_unique (2 * m + 1) hpr (p_ne_three m N hN) x y (Int.ofNat u) (Int.ofNat w) hxy huw'
    have hpm : FinCubic.psi (FinField.ffp m hpr) (FinField.ffp m hpr).K.one = 2 * x ∨
        FinCubic.psi (FinField.ffp m hpr) (FinField.ffp m hpr).K.one = -2 * x := by
      have h2u : Int.ofNat (2 * u) = 2 * Int.ofNat u := rfl
      cases QuarticFamily.sq_eq _ _ hsq with
      | inl e =>
        cases hpsi with
        | inl h => left; rw [h, h2u, e]
        | inr h => right; rw [h, h2u, e]; lib_int
      | inr e =>
        cases hpsi with
        | inl h => right; rw [h, h2u, e]; lib_int
        | inr h => left; rw [h, h2u, e]; lib_int
    cases hpm with
    | inl h =>
      exfalso
      apply three_ne_r (T - 2 * s) 1 (fun h => Nat.noConfusion h) (Nat.lt_succ_of_lt (Nat.lt_succ_self 1))
      have : 2 * x = 1 + 3 * T := h.symm.trans hT
      rw [hs] at this
      show _ = 1
      rw [show 3 * (T - 2 * s) = 1 + 3 * T + -(2 * (3 * s + 1)) + 1 by lib_int, ← this]; lib_int
    | inr h => exact h

/-- **Existence: there is an x ≡ 1 (mod 3) with x² + 3y² = p, and then a_p = 2x.** -/
theorem eisen_exists (m : Nat) (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (N : Nat)
    (hN : 2 * (m + 1) = 3 * N) :
    ∃ x y : Int, x * x + 3 * (y * y) = Int.ofNat (2 * m + 1 + 1 + 1) ∧ (∃ s, x = 3 * s + 1) ∧
      FinCubic.psi (FinField.ffp m hpr) (FinField.ffp m hpr).K.one = -2 * x := by
  match FinCubicExact.fin_cubic_trace (FinField.ffp m hpr) N hN, FinCubicTrace.psi_one_mod3_ff (FinField.ffp m hpr) N hN with
  | ⟨u, w, huw, hpsi⟩, ⟨T, hT⟩ =>
    have huw' : Int.ofNat u * Int.ofNat u + 3 * (Int.ofNat w * Int.ofNat w) = Int.ofNat (2 * m + 1 + 1 + 1) :=
      congrArg Int.ofNat (huw.trans (show 2 * (m + 1) + 1 = 2 * m + 1 + 1 + 1 by lib_nat))
    have h2u : Int.ofNat (2 * u) = 2 * Int.ofNat u := rfl
    generalize FinCubic.psi (FinField.ffp m hpr) (FinField.ffp m hpr).K.one = S at hpsi hT ⊢
    have fin : ∀ x : Int, x * x = Int.ofNat u * Int.ofNat u → S = -2 * x →
        ∃ x y : Int, x * x + 3 * (y * y) = Int.ofNat (2 * m + 1 + 1 + 1) ∧ (∃ s, x = 3 * s + 1) ∧ S = -2 * x :=
      fun x hx hS => ⟨x, Int.ofNat w, by rw [hx]; exact huw', ⟨x + T, by
        have : -2 * x = 1 + 3 * T := hS.symm.trans hT
        rw [show 3 * (x + T) + 1 = 3 * x + (1 + 3 * T) by lib_int, ← this]; lib_int⟩, hS⟩
    cases hpsi with
    | inl h => exact fin (-Int.ofNat u) (by lib_int) (by rw [h, h2u]; lib_int)
    | inr h => exact fin (Int.ofNat u) rfl (by rw [h, h2u]; lib_int)

/-- **6 | #E(𝔽_p) = p + 1 + Σ_t χ(t³ + 1)** for y² = x³ + 1 at every p ≡ 1 (mod 3). -/
theorem count_mod6 (m : Nat) (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (N : Nat)
    (hN : 2 * (m + 1) = 3 * N) :
    ∃ k, Int.ofNat (2 * m + 1 + 1 + 1) + 1 + FinCubic.psi (FinField.ffp m hpr) (FinField.ffp m hpr).K.one = 6 * k := by
  match eisen_exists m hpr N hN with
  | ⟨x, y, hxy, ⟨s, hs⟩, hpsi⟩ =>
    rw [hpsi]
    have hm : Int.ofNat (2 * m + 1 + 1 + 1) = 2 * Int.ofNat m + 3 := rfl
    have e3 : 2 * (Int.ofNat m + 2 + -x) = 3 * (3 * (s * s) + y * y) := by
      rw [show 2 * (Int.ofNat m + 2 + -x) = (2 * Int.ofNat m + 3) + 1 + -2 * x by lib_int, ← hm, ← hxy, hs]; lib_int
    cases FermatUnique.int_euclid 1 Eisen.prime3 2 _ _ (e3.trans rfl) with
    | inl h =>
      match h with
      | ⟨c, hc⟩ => exact absurd (hc.symm.trans rfl) (fun e => three_ne_r c 2 (fun h => Nat.noConfusion h) (Nat.lt_succ_self 2) e)
    | inr h =>
      match h with
      | ⟨c, hc⟩ =>
        exact ⟨c, by
          have : Int.ofNat m + 2 + -x = 3 * c := hc
          rw [hm, show 2 * Int.ofNat m + 3 + 1 + -2 * x = 2 * (Int.ofNat m + 2 + -x) by lib_int, this]; lib_int⟩

end EisenAp

#print axioms EisenAp.three_ne_r
#print axioms EisenAp.p_ne_three
#print axioms EisenAp.eisen_ap
#print axioms EisenAp.eisen_exists
#print axioms EisenAp.count_mod6

lang/EisenCube.lean

lean_all/lang/EisenCube.lean

/-
  lang/EisenCube — T43, cycle 167: elements of norm p³ in ℤ[√−3] prime to p.

  π = x + y√−3, x² + 3y² = p ≠ 3, x ≡ 1 (mod 3). If U² + 3W² = p³, p ∤ U and U ≡ 1 (mod 3), then U = x³ − 9xy² = Re π³:
    ecube_re, e12, e21      Re π³, π π̄² = p π̄, π² π̄ = p π
    eisen_cube              EisenFactor gives ±π^j π̄^(3−j); j = 1, 2 give p | U; the sign − gives U ≡ −1 (mod 3).
  With ψ(1) ≡ 1 (mod 3) over 𝔽_{p³}: a(𝔽_{p³}) = 2 Re π³ = a_p³ − 3p a_p for y² = x³ + 1.

  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.EisenFactor
import lang.EisenAp

namespace EisenCube

open FTAChain LibInt FermatUnique GaussInt GaussFactor EisenFactor

theorem ecube_re (x : GI) : (epow x 3).re = x.re * x.re * x.re - 9 * (x.re * (x.im * x.im)) := by
  show ((1 * x.re - 3 * (0 * x.im)) * x.re - 3 * ((1 * x.im + 0 * x.re) * x.im)) * x.re
    - 3 * (((1 * x.re - 3 * (0 * x.im)) * x.im + (1 * x.im + 0 * x.re) * x.re) * x.im) = _
  lib_int

theorem e12 (a b : Int) : emul (epow ⟨a, b⟩ 1) (epow ⟨a, -b⟩ 2) = ⟨(a * a + 3 * (b * b)) * a, -((a * a + 3 * (b * b)) * b)⟩ := by
  apply gi_ext
  · show (1 * a - 3 * (0 * b)) * ((1 * a - 3 * (0 * -b)) * a - 3 * ((1 * -b + 0 * a) * -b))
      - 3 * ((1 * b + 0 * a) * ((1 * a - 3 * (0 * -b)) * -b + (1 * -b + 0 * a) * a)) = (a * a + 3 * (b * b)) * a
    lib_int
  · show (1 * a - 3 * (0 * b)) * ((1 * a - 3 * (0 * -b)) * -b + (1 * -b + 0 * a) * a)
      + (1 * b + 0 * a) * ((1 * a - 3 * (0 * -b)) * a - 3 * ((1 * -b + 0 * a) * -b)) = -((a * a + 3 * (b * b)) * b)
    lib_int

theorem e21 (a b : Int) : emul (epow ⟨a, b⟩ 2) (epow ⟨a, -b⟩ 1) = ⟨(a * a + 3 * (b * b)) * a, (a * a + 3 * (b * b)) * b⟩ := by
  apply gi_ext
  · show ((1 * a - 3 * (0 * b)) * a - 3 * ((1 * b + 0 * a) * b)) * (1 * a - 3 * (0 * -b))
      - 3 * (((1 * a - 3 * (0 * b)) * b + (1 * b + 0 * a) * a) * (1 * -b + 0 * a)) = (a * a + 3 * (b * b)) * a
    lib_int
  · show ((1 * a - 3 * (0 * b)) * a - 3 * ((1 * b + 0 * a) * b)) * (1 * -b + 0 * a)
      + ((1 * a - 3 * (0 * b)) * b + (1 * b + 0 * a) * a) * (1 * a - 3 * (0 * -b)) = (a * a + 3 * (b * b)) * b
    lib_int

theorem sign_re (u z : GI) (hu : IsUnit3 u) : (emul u z).re = z.re ∨ (emul u z).re = -z.re := by
  cases hu with
  | inl h => subst h; left; show 1 * z.re - 3 * (0 * z.im) = z.re; lib_int
  | inr h => subst h; right; show -1 * z.re - 3 * (0 * z.im) = -z.re; lib_int

/-- **U = Re π³.** -/
theorem eisen_cube (p : Nat) (hp : ∀ d, Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (h3 : p + 1 + 1 ≠ 3) (a b : Int)
    (hab : a * a + 3 * (b * b) = Int.ofNat (p + 1 + 1)) (ha : ∃ s, a = 3 * s + 1) (z : GI)
    (hN : enorm z = ipow (Int.ofNat (p + 1 + 1)) 3) (hU : ¬ ∃ w, z.re = Int.ofNat (p + 1 + 1) * w)
    (hU1 : ∃ t, z.re = 3 * t + 1) : z.re = a * a * a - 9 * (a * (b * b)) := by
  match efactor p hp h3 a b hab 3 z hN with
  | ⟨u, j, l, hu, hjl, hz⟩ =>
    have pm : z.re = a * a * a - 9 * (a * (b * b)) ∨ z.re = -(a * a * a - 9 * (a * (b * b))) → z.re = a * a * a - 9 * (a * (b * b)) :=
      fun h => by
        cases h with
        | inl h => exact h
        | inr h =>
          exfalso
          match ha, hU1 with
          | ⟨s, hs⟩, ⟨t, ht⟩ =>
            apply EisenAp.three_ne_r (t + 9 * s * s * s + 9 * s * s + 3 * s - 3 * ((3 * s + 1) * (b * b)) + 1) 1
              (fun e => Nat.noConfusion e) (Nat.lt_succ_of_lt (Nat.lt_succ_self 1))
            show _ = 1
            have e : 3 * t + 1 = -(a * a * a - 9 * (a * (b * b))) := ht.symm.trans h
            rw [hs] at e
            rw [show 3 * (t + 9 * s * s * s + 9 * s * s + 3 * s - 3 * ((3 * s + 1) * (b * b)) + 1)
              = (3 * t + 1) + 27 * s * s * s + 27 * s * s + 9 * s - 9 * ((3 * s + 1) * (b * b)) + 2 by lib_int, e]; lib_int
    match j with
    | 0 =>
      have hl : l = 3 := (Nat.zero_add l).symm.trans hjl
      subst hl
      apply pm
      rw [hz, show emul (epow ⟨a, b⟩ 0) (epow ⟨a, -b⟩ 3) = epow ⟨a, -b⟩ 3 from gone_emul _]
      have hc : (epow ⟨a, -b⟩ 3).re = a * a * a - 9 * (a * (b * b)) := by
        rw [ecube_re]; show a * a * a - 9 * (a * (-b * -b)) = _; lib_int
      rw [← hc]; exact sign_re u _ hu
    | 3 =>
      have hl : l = 0 := add_cancel 3 l 0 (hjl.trans rfl)
      subst hl
      apply pm
      rw [hz, show emul (epow ⟨a, b⟩ 3) (epow ⟨a, -b⟩ 0) = epow ⟨a, b⟩ 3 by
        show emul (epow ⟨a, b⟩ 3) gone = _; rw [emul_comm, gone_emul]]
      rw [← ecube_re ⟨a, b⟩]; exact sign_re u _ hu
    | 1 =>
      have hl : l = 2 := Nat.succ.inj ((Nat.add_comm l 1).trans hjl)
      subst hl
      exfalso; apply hU
      rw [hz, e12, ← hab]
      cases sign_re u ⟨(a * a + 3 * (b * b)) * a, -((a * a + 3 * (b * b)) * b)⟩ hu with
      | inl h => exact ⟨a, by rw [h]⟩
      | inr h => exact ⟨-a, by rw [h]; show -((a * a + 3 * (b * b)) * a) = _; lib_int⟩
    | 2 =>
      have hl : l = 1 := Nat.succ.inj (Nat.succ.inj ((show l + 1 + 1 = 2 + l by lib_nat).trans hjl))
      subst hl
      exfalso; apply hU
      rw [hz, e21, ← hab]
      cases sign_re u ⟨(a * a + 3 * (b * b)) * a, (a * a + 3 * (b * b)) * b⟩ hu with
      | inl h => exact ⟨a, by rw [h]⟩
      | inr h => exact ⟨-a, by rw [h]; show -((a * a + 3 * (b * b)) * a) = _; lib_int⟩
    | j + 4 =>
      exfalso
      exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj ((show j + l + 1 + 1 + 1 + 1 = j + 4 + l by lib_nat).trans hjl))))

end EisenCube

#print axioms EisenCube.ecube_re
#print axioms EisenCube.e12
#print axioms EisenCube.e21
#print axioms EisenCube.sign_re
#print axioms EisenCube.eisen_cube

lang/EisenDiv.lean

lean_all/lang/EisenDiv.lean

/-
  lang/EisenDiv — T43, cycle 165: π = x + y√−3 of prime norm p ≠ 3 is prime in ℤ[√−3] above p.

  If x² + 3y² = p and p | X² + 3Y², then X + Y√−3 = π γ or π̄ γ:
    quot_exact3    p | Xx + 3Yy and p | Yx − Xy ⇒ X = x u − 3 y v, Y = x v + y u
    div_plus3      p | Xx + 3Yy ⇒ π | X + Y√−3          ((Xx+3Yy)² + 3(Yx−Xy)² = p(X² + 3Y²), p ≠ 3)
    eisen_div      the dichotomy                         ((Xx+3Yy)(Xx−3Yy) = x²(X²+3Y²) − 3Y² p)
  The ℤ[√−3]-analogue of GaussDiv (p is prime to the conductor 2 of ℤ[√−3] ⊂ ℤ[ω]).

  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.EisenUnique
import lang.GaussDiv

namespace EisenDiv

open FTAChain LibInt FermatUnique

theorem quot_exact3 (p : Nat) (x y X Y u v : Int) (hxy : x * x + 3 * (y * y) = Int.ofNat (p + 1 + 1))
    (hu : X * x + 3 * (Y * y) = Int.ofNat (p + 1 + 1) * u) (hv : Y * x - X * y = Int.ofNat (p + 1 + 1) * v) :
    X = x * u - 3 * (y * v) ∧ Y = x * v + y * u := by
  constructor
  · apply cancelP p
    rw [show Int.ofNat (p + 1 + 1) * (x * u - 3 * (y * v)) = x * (Int.ofNat (p + 1 + 1) * u) - 3 * (y * (Int.ofNat (p + 1 + 1) * v)) by lib_int,
      ← hu, ← hv, ← hxy]; lib_int
  · apply cancelP p
    rw [show Int.ofNat (p + 1 + 1) * (x * v + y * u) = x * (Int.ofNat (p + 1 + 1) * v) + y * (Int.ofNat (p + 1 + 1) * u) by lib_int,
      ← hu, ← hv, ← hxy]; lib_int

/-- P | n as integers with n > 0 gives a natural quotient. -/
theorem nat_dvd (p n : Nat) (w : Int) (h : Int.ofNat (n + 1) = Int.ofNat (p + 1 + 1) * w) : ∃ q : Nat, n + 1 = q * (p + 1 + 1) := by
  cases w with
  | ofNat q => exact ⟨q, Int.ofNat.inj (h.trans (by show Int.ofNat ((p + 1 + 1) * q) = Int.ofNat (q * (p + 1 + 1)); rw [Nat.mul_comm]))⟩
  | negSucc q =>
    exfalso
    have e : Int.ofNat (p + 1 + 1) * Int.negSucc q = Int.negSucc ((p + 1 + 1) * q + p + 1) := by
      show -(Int.ofNat (p + 1 + 1) * Int.ofNat (q + 1)) = _
      rw [show Int.ofNat (p + 1 + 1) * Int.ofNat (q + 1) = Int.ofNat ((p + 1 + 1) * q + p + 1 + 1) by
        show Int.ofNat ((p + 1 + 1) * (q + 1)) = _; rw [show (p + 1 + 1) * (q + 1) = (p + 1 + 1) * q + p + 1 + 1 by lib_nat]]
      rfl
    rw [e] at h
    exact Int.noConfusion h

theorem div_plus3 (p : Nat) (hp : ∀ d, Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (h3 : p + 1 + 1 ≠ 3) (x y X Y n u : Int)
    (hxy : x * x + 3 * (y * y) = Int.ofNat (p + 1 + 1)) (hn : X * X + 3 * (Y * Y) = Int.ofNat (p + 1 + 1) * n)
    (hu : X * x + 3 * (Y * y) = Int.ofNat (p + 1 + 1) * u) :
    ∃ a b : Int, X = x * a - 3 * (y * b) ∧ Y = x * b + y * a := by
  have e : (X * x + 3 * (Y * y)) * (X * x + 3 * (Y * y)) + 3 * ((Y * x - X * y) * (Y * x - X * y))
      = (X * X + 3 * (Y * Y)) * (x * x + 3 * (y * y)) := by lib_int
  rw [hu, hn, hxy] at e
  have h3sq : 3 * ((Y * x - X * y) * (Y * x - X * y)) = Int.ofNat (p + 1 + 1) * (Int.ofNat (p + 1 + 1) * n + -(Int.ofNat (p + 1 + 1) * u * u)) := by
    rw [show 3 * ((Y * x - X * y) * (Y * x - X * y)) = (Int.ofNat (p + 1 + 1) * u * (Int.ofNat (p + 1 + 1) * u) + 3 * ((Y * x - X * y) * (Y * x - X * y)))
      + -(Int.ofNat (p + 1 + 1) * u * (Int.ofNat (p + 1 + 1) * u)) by lib_int, e]; lib_int
  have hz : ∃ v, Y * x - X * y = Int.ofNat (p + 1 + 1) * v := by
    cases int_euclid p hp 3 _ _ h3sq with
    | inl h =>
      exfalso
      match h with
      | ⟨w, hw⟩ =>
        match nat_dvd p 2 w hw with
        | ⟨q, hq⟩ =>
          cases Eisen.prime3 (p + 1 + 1) ⟨q, hq⟩ with
          | inl e => exact Nat.noConfusion (Nat.succ.inj e)
          | inr e => exact h3 e
    | inr h => match h with | ⟨w, hw⟩ => exact GaussDiv.sq_div p hp _ w hw
  match hz with
  | ⟨v, hv⟩ =>
    match quot_exact3 p x y X Y u v hxy hu hv with
    | ⟨h1, h2⟩ => exact ⟨u, v, h1, h2⟩

/-- **π or π̄ divides every X + Y√−3 with p | X² + 3Y².** -/
theorem eisen_div (p : Nat) (hp : ∀ d, Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (h3 : p + 1 + 1 ≠ 3) (x y X Y n : Int)
    (hxy : x * x + 3 * (y * y) = Int.ofNat (p + 1 + 1)) (hn : X * X + 3 * (Y * Y) = Int.ofNat (p + 1 + 1) * n) :
    (∃ a b : Int, X = x * a - 3 * (y * b) ∧ Y = x * b + y * a) ∨ (∃ a b : Int, X = x * a + 3 * (y * b) ∧ Y = x * b - y * a) := by
  have key : (X * x + 3 * (Y * y)) * (X * x + 3 * (Y * (-y))) = Int.ofNat (p + 1 + 1) * (x * x * n + -3 * (Y * Y)) := by
    rw [show (X * x + 3 * (Y * y)) * (X * x + 3 * (Y * (-y))) = x * x * (X * X + 3 * (Y * Y)) + -3 * (Y * Y) * (x * x + 3 * (y * y)) by lib_int, hn, hxy]
    lib_int
  cases int_euclid p hp _ _ _ key with
  | inl h => match h with | ⟨u, hu⟩ => exact Or.inl (div_plus3 p hp h3 x y X Y n u hxy hn hu)
  | inr h =>
    match h with
    | ⟨u, hu⟩ =>
      match div_plus3 p hp h3 x (-y) X Y n u (by rw [← hxy]; lib_int) hn hu with
      | ⟨a, b, h1, h2⟩ => exact Or.inr ⟨a, b, by rw [h1]; lib_int, by rw [h2]; lib_int⟩

end EisenDiv

#print axioms EisenDiv.quot_exact3
#print axioms EisenDiv.nat_dvd
#print axioms EisenDiv.div_plus3
#print axioms EisenDiv.eisen_div

lang/EisenFactor.lean

lean_all/lang/EisenFactor.lean

/-
  lang/EisenFactor — T43, cycle 166: factorisation in ℤ[√−3] above a split prime p ≠ 3.

  π = x + y√−3 with x² + 3y² = p prime. Every α ∈ ℤ[√−3] with N(α) = p^k is ±π^j π̄^l, j + l = k:
    emul, enorm, epow, e_ext, emul_comm, emul_assoc, enorm_mul     ℤ[√−3] on pairs (re, im)
    enorm_one                                                        N(α) = 1 ⇒ α = ±1
    efactor                                                          induction on k with EisenDiv.eisen_div
  The input for listing the elements of norm p³ (y² = x³ + 1 over 𝔽_{p³}).

  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.EisenDiv
import lang.GaussFactor

namespace EisenFactor

open FTAChain LibInt FermatUnique GaussInt GaussFactor

def emul (x y : GI) : GI := ⟨x.re * y.re - 3 * (x.im * y.im), x.re * y.im + x.im * y.re⟩
def enorm (x : GI) : Int := x.re * x.re + 3 * (x.im * x.im)

def epow (x : GI) : Nat → GI
  | 0 => gone
  | n + 1 => emul (epow x n) x

theorem emul_comm (x y : GI) : emul x y = emul y x :=
  gi_ext _ _ (by show x.re * y.re - 3 * (x.im * y.im) = y.re * x.re - 3 * (y.im * x.im); lib_int)
    (by show x.re * y.im + x.im * y.re = y.re * x.im + y.im * x.re; lib_int)

theorem emul_assoc (x y z : GI) : emul (emul x y) z = emul x (emul y z) :=
  gi_ext _ _ (by show (x.re * y.re - 3 * (x.im * y.im)) * z.re - 3 * ((x.re * y.im + x.im * y.re) * z.im)
      = x.re * (y.re * z.re - 3 * (y.im * z.im)) - 3 * (x.im * (y.re * z.im + y.im * z.re)); lib_int)
    (by show (x.re * y.re - 3 * (x.im * y.im)) * z.im + (x.re * y.im + x.im * y.re) * z.re
      = x.re * (y.re * z.im + y.im * z.re) + x.im * (y.re * z.re - 3 * (y.im * z.im)); lib_int)

theorem enorm_mul (x y : GI) : enorm (emul x y) = enorm x * enorm y := by
  show (x.re * y.re - 3 * (x.im * y.im)) * (x.re * y.re - 3 * (x.im * y.im)) + 3 * ((x.re * y.im + x.im * y.re) * (x.re * y.im + x.im * y.re))
    = (x.re * x.re + 3 * (x.im * x.im)) * (y.re * y.re + 3 * (y.im * y.im))
  lib_int

theorem gone_emul (x : GI) : emul gone x = x :=
  gi_ext _ _ (by show 1 * x.re - 3 * (0 * x.im) = x.re; lib_int) (by show 1 * x.im + 0 * x.re = x.im; lib_int)

def IsUnit3 (u : GI) : Prop := u = ⟨1, 0⟩ ∨ u = ⟨-1, 0⟩

theorem enorm_one (x : GI) (h : enorm x = 1) : IsUnit3 x := by
  match StrictHasse.sq_nat x.re, StrictHasse.sq_nat x.im with
  | ⟨n, hn⟩, ⟨m, hm⟩ =>
    have e : n * n + 3 * (m * m) = 1 := Int.ofNat.inj (show Int.ofNat (n * n) + 3 * Int.ofNat (m * m) = 1 by rw [← hn, ← hm]; exact h)
    have hm0 : m * m = 0 := by
      cases hmm : m * m with
      | zero => rfl
      | succ t =>
        rw [hmm] at e
        exact Nat.noConfusion (Nat.succ.inj ((show n * n + 3 * (t + 1) = (n * n + 3 * t + 1) + 1 + 1 by lib_nat).symm.trans e))
    have hi : x.im = 0 := HasseDavenport.sq_zero _ (by rw [hm, hm0]; rfl)
    have hn1 : n * n = 1 := (show n * n = n * n + 3 * (m * m) by rw [hm0]; rfl).trans e
    cases QuarticFamily.sq_eq x.re 1 (by rw [hn, hn1]; rfl) with
    | inl q => exact Or.inl (gi_ext _ _ q hi)
    | inr q => exact Or.inr (gi_ext _ _ q hi)

/-- **Factorisation above p in ℤ[√−3].** -/
theorem efactor (p : Nat) (hp : ∀ d, Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (h3 : p + 1 + 1 ≠ 3) (a b : Int)
    (hab : a * a + 3 * (b * b) = Int.ofNat (p + 1 + 1)) : ∀ (k : Nat) (x : GI), enorm x = ipow (Int.ofNat (p + 1 + 1)) k →
    ∃ u j l, IsUnit3 u ∧ j + l = k ∧ x = emul u (emul (epow ⟨a, b⟩ j) (epow ⟨a, -b⟩ l))
  | 0, x, h => ⟨x, 0, 0, enorm_one x h, rfl, by
      show x = emul x (emul gone gone)
      rw [gone_emul, emul_comm, gone_emul]⟩
  | k + 1, x, h => by
    have hn : x.re * x.re + 3 * (x.im * x.im) = Int.ofNat (p + 1 + 1) * ipow (Int.ofNat (p + 1 + 1)) k := by
      rw [show Int.ofNat (p + 1 + 1) * ipow (Int.ofNat (p + 1 + 1)) k = ipow (Int.ofNat (p + 1 + 1)) (k + 1) by
        show _ = ipow _ k * _; lib_int]; exact h
    have normq : ∀ (q : GI) (x' y' : Int), enorm q = Int.ofNat (p + 1 + 1) → x = emul q ⟨x', y'⟩ →
        enorm ⟨x', y'⟩ = ipow (Int.ofNat (p + 1 + 1)) k := fun q x' y' hq e => by
      apply cancelP p
      rw [← hn, ← hq, ← enorm_mul, ← e]; rfl
    cases EisenDiv.eisen_div p hp h3 a b x.re x.im _ hab hn with
    | inl h1 =>
      match h1 with
      | ⟨x', y', e1, e2⟩ =>
        have ex : x = emul ⟨a, b⟩ ⟨x', y'⟩ := gi_ext _ _ e1 e2
        match efactor p hp h3 a b hab k ⟨x', y'⟩ (normq ⟨a, b⟩ x' y' hab ex) with
        | ⟨u, j, l, hu, hjl, hg⟩ =>
          refine ⟨u, j + 1, l, hu, by rw [← hjl]; lib_nat, ?_⟩
          rw [ex, hg]
          show emul ⟨a, b⟩ (emul u (emul (epow ⟨a, b⟩ j) (epow ⟨a, -b⟩ l))) = emul u (emul (emul (epow ⟨a, b⟩ j) ⟨a, b⟩) (epow ⟨a, -b⟩ l))
          rw [← emul_assoc, emul_comm ⟨a, b⟩ u, emul_assoc, emul_assoc, ← emul_assoc ⟨a, b⟩, emul_comm ⟨a, b⟩ (epow ⟨a, b⟩ j), emul_assoc]
    | inr h1 =>
      match h1 with
      | ⟨x', y', e1, e2⟩ =>
        have ex : x = emul ⟨a, -b⟩ ⟨x', y'⟩ :=
          gi_ext _ _ (by rw [e1]; show _ = a * x' - 3 * (-b * y'); lib_int) (by rw [e2]; show _ = a * y' + -b * x'; lib_int)
        match efactor p hp h3 a b hab k ⟨x', y'⟩ (normq ⟨a, -b⟩ x' y' (by rw [← hab]; show a * a + 3 * (-b * -b) = _; lib_int) ex) with
        | ⟨u, j, l, hu, hjl, hg⟩ =>
          refine ⟨u, j, l + 1, hu, by rw [← hjl]; lib_nat, ?_⟩
          rw [ex, hg]
          show emul ⟨a, -b⟩ (emul u (emul (epow ⟨a, b⟩ j) (epow ⟨a, -b⟩ l))) = emul u (emul (epow ⟨a, b⟩ j) (emul (epow ⟨a, -b⟩ l) ⟨a, -b⟩))
          rw [← emul_assoc, emul_comm ⟨a, -b⟩ u, emul_assoc, ← emul_assoc ⟨a, -b⟩, emul_comm ⟨a, -b⟩ (epow ⟨a, b⟩ j),
            emul_assoc, emul_comm ⟨a, -b⟩ (epow ⟨a, -b⟩ l)]

end EisenFactor

#print axioms EisenFactor.emul
#print axioms EisenFactor.enorm
#print axioms EisenFactor.epow
#print axioms EisenFactor.emul_comm
#print axioms EisenFactor.emul_assoc
#print axioms EisenFactor.enorm_mul
#print axioms EisenFactor.gone_emul
#print axioms EisenFactor.enorm_one
#print axioms EisenFactor.efactor

lang/EisenNorm.lean

lean_all/lang/EisenNorm.lean

/-
  lang/EisenNorm — T43, cycle 38: every prime p ≡ 1 (mod 3) is u² + 3v² (the norm form of ℤ[√−3] ⊂ ℤ[ω]).

  p = J + 2 prime, 3 | p − 1, z² = −3 in 𝔽_p (Eisen.split_neg3). Thue's grid (TwoSquares, Pigeon) gives two points with
  a + z b ≡ a' + z b'; with u = |a − a'|, v = |b − b'|:
    sq3_zero      X = zY, z² = −3 ⇒ X² + 3Y² = 0
    core3         p | u² + 3v², 0 < u² + 3v² < 4p
    mult_cases    p | n, 0 < n < 4p ⇒ n ∈ {p, 2p, 3p}
    case3         u² + 3v² = 3p ⇒ 3 | u and v² + 3(u/3)² = p
    case2_absurd  u² + 3v² = 2p is impossible for odd p (u ≡ v mod 2 and then 4 | u² + 3v²)
    eisen_norm    ∃ u v, u² + 3v² = p
  With Eisen: an odd prime p > 3 is u² + 3v² iff p ≡ 1 (mod 3) — the split primes of ℤ[ω] are norms, the input of
  the Hecke character of ℚ(√−3) at split primes.

  Lean 4 core only, proofs by hand (lib_nat, lib_ring, LibGcd from lib_shared), no axioms.
-/
import lang.Eisen
import lang.TwoSquares
import lib.Gcd
import lang.PrimRoot
import QRChain

namespace EisenNorm

open FTAChain ZMod YMChain Pigeon QRChain

variable (m : Nat)

theorem sq3_zero (X Y z : Zp m) (hz : (ring m).mul z z = (ring m).neg ((ring m).add (ring m).one ((ring m).add (ring m).one (ring m).one)))
    (h : X = (ring m).mul z Y) : (ring m).add ((ring m).mul X X) ((ring m).mul ((ring m).add (ring m).one ((ring m).add (ring m).one (ring m).one)) ((ring m).mul Y Y)) = (ring m).zero := by
  rw [h]
  have e : (ring m).add ((ring m).mul ((ring m).mul z Y) ((ring m).mul z Y)) ((ring m).mul ((ring m).add (ring m).one ((ring m).add (ring m).one (ring m).one)) ((ring m).mul Y Y))
      = (ring m).mul ((ring m).add ((ring m).mul z z) ((ring m).add (ring m).one ((ring m).add (ring m).one (ring m).one))) ((ring m).mul Y Y) := by lib_ring
  rw [e, hz, DetChain.add_neg', DetChain.zero_mul']

theorem three_mk : (ring m).add (ring m).one ((ring m).add (ring m).one (ring m).one) = mk m 3 := by
  show (ring m).add (mk m 1) ((ring m).add (mk m 1) (mk m 1)) = mk m 3
  rw [PrimeOneModQ.mk_add, PrimeOneModQ.mk_add]

theorem core3 (z : Zp m) (hz : (ring m).mul z z = (ring m).neg ((ring m).add (ring m).one ((ring m).add (ring m).one (ring m).one))) (k a b a' b' : Nat)
    (hk : k * k < m + 1) (ha : a ≤ k) (hb : b ≤ k) (ha' : a' ≤ k) (hb' : b' ≤ k) (hne : (a, b) ≠ (a', b'))
    (hc : (ring m).add (mk m a) ((ring m).mul z (mk m b)) = (ring m).add (mk m a') ((ring m).mul z (mk m b'))) :
    ∃ u v, Dv (m + 1) (u * u + 3 * (v * v)) ∧ 0 < u * u + 3 * (v * v) ∧ u * u + 3 * (v * v) < 4 * (m + 1) := by
  match Thue.dist_exists a a', Thue.dist_exists b' b with
  | ⟨u, hu⟩, ⟨v, hv⟩ =>
    refine ⟨u, v, ?_, ?_, ?_⟩
    · have hX := Thue.shift_eq (ring m) _ _ _ _ z hc
      have hs := sq3_zero m _ _ z hz hX
      rw [Thue.diff_sq m a a' u hu, Thue.diff_sq m b' b v hv, three_mk, TwoPower.mk_mul, TwoPower.mk_mul,
        TwoPower.mk_mul, PrimeOneModQ.mk_add] at hs
      exact md_zero_dv m _ (congrArg Subtype.val hs)
    · cases Nat.eq_zero_or_pos (u * u + 3 * (v * v)) with
      | inr h => exact h
      | inl h =>
        exfalso
        have hu0 : u * u = 0 := Nat.eq_zero_of_le_zero (h ▸ Nat.le_add_right (u * u) (3 * (v * v)))
        have hv3 : 3 * (v * v) = 0 := Nat.eq_zero_of_le_zero (h ▸ Nat.le_add_left (3 * (v * v)) (u * u))
        have hv0 : v * v = 0 := by
          cases hvv : v * v with
          | zero => rfl
          | succ w => rw [hvv] at hv3; exact Nat.noConfusion hv3
        have u0 := Thue.zero_of_sq u hu0
        have v0 := Thue.zero_of_sq v hv0
        apply hne
        have ea : a = a' := by
          cases hu with
          | inl e => rw [e, u0]; rfl
          | inr e => rw [e, u0]; rfl
        have eb : b = b' := by
          cases hv with
          | inl e => rw [e, v0]; rfl
          | inr e => rw [e, v0]; rfl
        rw [ea, eb]
    · have uk := Thue.dist_le a a' u k ha ha' hu
      have vk := Thue.dist_le b' b v k hb' hb hv
      have hu2 : u * u < m + 1 := Nat.lt_of_le_of_lt (Nat.mul_le_mul uk uk) hk
      have hv2 : v * v < m + 1 := Nat.lt_of_le_of_lt (Nat.mul_le_mul vk vk) hk
      have e1 : u * u + 3 * (v * v) = u * u + v * v + v * v + v * v := by lib_nat
      have e2 : 4 * (m + 1) = (m + 1) + (m + 1) + (m + 1) + (m + 1) := by lib_nat
      rw [e1, e2]
      exact Nat.add_lt_add (Nat.add_lt_add (Nat.add_lt_add hu2 hv2) hv2) hv2

theorem mult_cases (n : Nat) (h : Dv (m + 1) n) (h0 : 0 < n) (h4 : n < 4 * (m + 1)) :
    n = 1 * (m + 1) ∨ n = 2 * (m + 1) ∨ n = 3 * (m + 1) := by
  match h with
  | ⟨0, e⟩ => rw [e, Nat.zero_mul] at h0; exact absurd h0 (Nat.lt_irrefl 0)
  | ⟨1, e⟩ => exact Or.inl e
  | ⟨2, e⟩ => exact Or.inr (Or.inl e)
  | ⟨3, e⟩ => exact Or.inr (Or.inr e)
  | ⟨c + 4, e⟩ =>
    exfalso
    have : 4 * (m + 1) ≤ n := by
      rw [e, show (c + 4) * (m + 1) = c * (m + 1) + 4 * (m + 1) by lib_nat]
      exact Nat.le_add_left _ _
    exact Nat.lt_irrefl n (Nat.lt_of_lt_of_le h4 this)

theorem odd_of_prime (hpr : ∀ d, Dv d (m + 1) → d = 1 ∨ d = m + 1) (hm : 2 ≤ m) (x : Nat)
    (h : m + 1 = 2 * x) : False := by
  cases hpr 2 ⟨x, by rw [h]; lib_nat⟩ with
  | inl e => exact Nat.noConfusion (Nat.succ.inj e)
  | inr e =>
    have : m = 1 := Nat.succ.inj e.symm
    rw [this] at hm
    exact Nat.not_succ_le_self 1 hm

theorem par_sq (u : Nat) : par (u * u) = par u := by
  rw [par_mul]; cases par u <;> rfl

theorem case2_absurd (hpr : ∀ d, Dv d (m + 1) → d = 1 ∨ d = m + 1) (hm : 2 ≤ m) (u v : Nat)
    (h : u * u + 3 * (v * v) = 2 * (m + 1)) : False := by
  have hp : par (u * u + 3 * (v * v)) = false := by
    rw [h, par_mul]; rfl
  rw [par_add, par_sq, par_mul 3, par_sq] at hp
  have heq : par u = par v := by
    revert hp
    show bx (par u) (true && par v) = false → par u = par v
    cases par u <;> cases par v <;> intro hh <;> first | rfl | exact Bool.noConfusion hh
  cases hu : par u with
  | false =>
    have hv : par v = false := heq ▸ hu
    match (par_split u).1 hu, (par_split v).1 hv with
    | ⟨a, ea⟩, ⟨b, eb⟩ =>
      apply odd_of_prime m hpr hm (a * a + 3 * (b * b))
      apply Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ 1)
      rw [← h, ea, eb]; lib_nat
  | true =>
    have hv : par v = true := heq ▸ hu
    match (par_split u).2 hu, (par_split v).2 hv with
    | ⟨a, ea⟩, ⟨b, eb⟩ =>
      apply odd_of_prime m hpr hm (a * a + a + 3 * (b * b) + 3 * b + 1)
      apply Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ 1)
      rw [← h, ea, eb]; lib_nat

theorem prime3' : ∀ d, d ∣ 3 → d = 1 ∨ d = 3 := fun d hd => Eisen.prime3 d (PrimRoot.dv_of_dvd hd)

theorem case3 (u v : Nat) (h : u * u + 3 * (v * v) = 3 * (m + 1)) : ∃ x y, x * x + 3 * (y * y) = m + 1 := by
  have h3 : Dv (2 + 1) (3 * (v * v) + u * u) := ⟨m + 1, by rw [Nat.add_comm, h]; lib_nat⟩
  have h3v : Dv (2 + 1) (3 * (v * v)) := ⟨v * v, by lib_nat⟩
  have hu : 3 ∣ u * u := PrimRoot.dvd_of_dv (dv_sub 2 _ _ h3v h3)
  have hu' : 3 ∣ u := by
    cases LibGcd.euclid_prime prime3' hu with
    | inl x => exact x
    | inr x => exact x
  match hu' with
  | ⟨w, ew⟩ =>
    refine ⟨v, w, Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ 2) ?_⟩
    rw [← h, ew]; lib_nat

/-- **p ≡ 1 (mod 3) ⇒ p = u² + 3v².** -/
theorem eisen_norm (J : Nat) (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (h3 : 2 ≤ J)
    (hd : Dv (1 + 2) (J + 1)) : ∃ u v, u * u + 3 * (v * v) = J + 1 + 1 := by
  match Eisen.split_neg3 J hpr hd, TwoSquares.isqrt (J + 1) with
  | ⟨z, hz⟩, ⟨k, hk1, hk2⟩ =>
    have hk : k * k < J + 1 + 1 := Nat.lt_succ_of_le hk1
    have hlen : J + 1 + 1 < (TwoSquares.grid (k + 1) (k + 1)).length := by
      rw [TwoSquares.len_grid]
      exact Nat.lt_of_le_of_ne (Nat.succ_le_of_lt hk2) (fun e =>
        TwoSquares.not_square (J + 1) k (Nat.succ_le_succ (Nat.zero_le _)) hpr e.symm)
    match collision (fun x => md (J + 1) (x.1 + z.val * x.2)) (J + 1 + 1)
        (TwoSquares.grid (k + 1) (k + 1)) (TwoSquares.dist_grid (k + 1) (k + 1)) (fun x => md_lt _ _) hlen with
    | ⟨(a, b), (a', b'), hx, hx', hne, hf⟩ =>
      have ma := TwoSquares.mem_grid (k + 1) (a, b) (k + 1) hx
      have ma' := TwoSquares.mem_grid (k + 1) (a', b') (k + 1) hx'
      have hc : (ring (J + 1)).add (mk _ a) ((ring _).mul z (mk _ b))
          = (ring (J + 1)).add (mk _ a') ((ring _).mul z (mk _ b')) := by
        rw [← mk_val _ z, TwoPower.mk_mul, TwoPower.mk_mul, PrimeOneModQ.mk_add, PrimeOneModQ.mk_add]
        exact Subtype.ext hf
      match core3 (J + 1) z hz k a b a' b' hk (Nat.le_of_lt_succ ma.1) (Nat.le_of_lt_succ ma.2)
          (Nat.le_of_lt_succ ma'.1) (Nat.le_of_lt_succ ma'.2) hne hc with
      | ⟨u, v, hdv, h0, h4⟩ =>
        cases mult_cases (J + 1) _ hdv h0 h4 with
        | inl e => exact ⟨u, v, e.trans (Nat.one_mul _)⟩
        | inr e =>
          cases e with
          | inl e2 => exact absurd (case2_absurd (J + 1) hpr (Nat.le_succ_of_le h3) u v e2) (fun x => x)
          | inr e3 => exact case3 (J + 1) u v e3

end EisenNorm

#print axioms EisenNorm.sq3_zero
#print axioms EisenNorm.three_mk
#print axioms EisenNorm.core3
#print axioms EisenNorm.mult_cases
#print axioms EisenNorm.odd_of_prime
#print axioms EisenNorm.par_sq
#print axioms EisenNorm.case2_absurd
#print axioms EisenNorm.prime3'
#print axioms EisenNorm.case3
#print axioms EisenNorm.eisen_norm

lang/EisenPow.lean

lean_all/lang/EisenPow.lean

/-
  lang/EisenPow — T43, cycle 175: elements of norm p^N in ℤ[√−3] prime to p, ≡ 1 (mod 3), are π^N or π̄^N.

  π = x + y√−3, x² + 3y² = p ≠ 3, x ≡ 1 (mod 3). If U² + 3W² = p^N, p ∤ U and U ≡ 1 (mod 3), then U = Re π^N:
    econj_mul, econj_pow     conjugation in ℤ[√−3]
    emixed                   π^(j+1) π̄^(l+1) = p · π^j π̄^l
    re_mod3                  Re π^n ≡ 1 (mod 3)
    epow_cube                EisenFactor: ±π^j π̄^l; mixed terms give p | U; −Re π^N ≡ −1 (mod 3).

  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.EisenCube
import lang.GaussPow

namespace EisenPow

open FTAChain LibInt FermatUnique GaussInt GaussFactor EisenFactor

theorem econj_mul (x y : GI) : gconj (emul x y) = emul (gconj x) (gconj y) :=
  gi_ext _ _ (by show x.re * y.re - 3 * (x.im * y.im) = x.re * y.re - 3 * (-x.im * -y.im); lib_int)
    (by show -(x.re * y.im + x.im * y.re) = x.re * -y.im + -x.im * y.re; lib_int)

theorem econj_pow (a b : Int) : ∀ n, epow ⟨a, -b⟩ n = gconj (epow ⟨a, b⟩ n)
  | 0 => gi_ext _ _ rfl (by show (0 : Int) = -0; rfl)
  | n + 1 => by
    show emul (epow ⟨a, -b⟩ n) ⟨a, -b⟩ = gconj (emul (epow ⟨a, b⟩ n) ⟨a, b⟩)
    rw [econj_mul, econj_pow a b n]; rfl

theorem ereal_re (P : Int) (z : GI) : (emul (GaussPow.real P) z).re = P * z.re := by
  show P * z.re - 3 * (0 * z.im) = P * z.re; lib_int

theorem emixed (a b : Int) (j l : Nat) :
    emul (epow ⟨a, b⟩ (j + 1)) (epow ⟨a, -b⟩ (l + 1)) = emul (GaussPow.real (a * a + 3 * (b * b))) (emul (epow ⟨a, b⟩ j) (epow ⟨a, -b⟩ l)) := by
  have hpp : emul ⟨a, b⟩ ⟨a, -b⟩ = GaussPow.real (a * a + 3 * (b * b)) :=
    gi_ext _ _ (by show a * a - 3 * (b * -b) = a * a + 3 * (b * b); lib_int) (by show a * -b + b * a = 0; lib_int)
  show emul (emul (epow ⟨a, b⟩ j) ⟨a, b⟩) (emul (epow ⟨a, -b⟩ l) ⟨a, -b⟩) = _
  rw [emul_assoc, ← emul_assoc ⟨a, b⟩, emul_comm ⟨a, b⟩ (epow ⟨a, -b⟩ l), emul_assoc, hpp,
    ← emul_assoc, emul_comm _ (GaussPow.real _)]

theorem re_mod3 (a b s : Int) (hs : a = 3 * s + 1) : ∀ n, ∃ t, (epow ⟨a, b⟩ n).re = 3 * t + 1
  | 0 => ⟨0, rfl⟩
  | n + 1 =>
    match re_mod3 a b s hs n with
    | ⟨t, ht⟩ => ⟨t * a + s - (epow ⟨a, b⟩ n).im * b, by
        show (epow ⟨a, b⟩ n).re * a - 3 * ((epow ⟨a, b⟩ n).im * b) = _
        rw [ht, hs]; lib_int⟩

theorem sign_ok (U V : Int) (hU : ∃ t, U = 3 * t + 1) (hV : ∃ t, V = 3 * t + 1) (h : U = V ∨ U = -V) : U = V := by
  cases h with
  | inl h => exact h
  | inr h =>
    exfalso
    match hU, hV with
    | ⟨t, ht⟩, ⟨t', ht'⟩ =>
      apply EisenAp.three_ne_r (t + t' + 1) 1 (fun e => Nat.noConfusion e) (Nat.lt_succ_of_lt (Nat.lt_succ_self 1))
      show _ = 1
      rw [show 3 * (t + t' + 1) = (3 * t + 1) + (3 * t' + 1) + 1 by lib_int, ← ht, ← ht', h]; lib_int

/-- **U = Re π^N.** -/
theorem epow_primary (p : Nat) (hp : ∀ d, Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (h3 : p + 1 + 1 ≠ 3) (a b : Int)
    (hab : a * a + 3 * (b * b) = Int.ofNat (p + 1 + 1)) (ha : ∃ s, a = 3 * s + 1) (N : Nat) (z : GI)
    (hN : enorm z = ipow (Int.ofNat (p + 1 + 1)) N) (hU : ¬ ∃ w, z.re = Int.ofNat (p + 1 + 1) * w)
    (hU1 : ∃ t, z.re = 3 * t + 1) : z.re = (epow ⟨a, b⟩ N).re := by
  match efactor p hp h3 a b hab N z hN, ha with
  | ⟨u, j, l, hu, hjl, hz⟩, ⟨s, hs⟩ =>
    match j, l with
    | j + 1, l + 1 =>
      exfalso; apply hU
      cases EisenCube.sign_re u (emul (GaussPow.real (a * a + 3 * (b * b))) (emul (epow ⟨a, b⟩ j) (epow ⟨a, -b⟩ l))) hu with
      | inl h => exact ⟨(emul (epow ⟨a, b⟩ j) (epow ⟨a, -b⟩ l)).re, by rw [hz, emixed, h, ereal_re, hab]⟩
      | inr h => exact ⟨-(emul (epow ⟨a, b⟩ j) (epow ⟨a, -b⟩ l)).re, by rw [hz, emixed, h, ereal_re, hab]; lib_int⟩
    | 0, l =>
      have hl : l = N := (Nat.zero_add l).symm.trans hjl
      subst hl
      apply sign_ok _ _ hU1 (re_mod3 a b s hs l)
      rw [hz, show emul (epow ⟨a, b⟩ 0) (epow ⟨a, -b⟩ l) = epow ⟨a, -b⟩ l from gone_emul _, econj_pow]
      exact EisenCube.sign_re u _ hu
    | j + 1, 0 =>
      have hl : j + 1 = N := hjl
      subst hl
      apply sign_ok _ _ hU1 (re_mod3 a b s hs (j + 1))
      rw [hz, show emul (epow ⟨a, b⟩ (j + 1)) (epow ⟨a, -b⟩ 0) = epow ⟨a, b⟩ (j + 1) by
        show emul (epow ⟨a, b⟩ (j + 1)) gone = _; rw [emul_comm, gone_emul]]
      exact EisenCube.sign_re u _ hu

end EisenPow

#print axioms EisenPow.econj_mul
#print axioms EisenPow.econj_pow
#print axioms EisenPow.ereal_re
#print axioms EisenPow.emixed
#print axioms EisenPow.re_mod3
#print axioms EisenPow.sign_ok
#print axioms EisenPow.epow_primary

lang/EisenUnique.lean

lean_all/lang/EisenUnique.lean

/-
  lang/EisenUnique — T43, cycle 154: uniqueness of p = x² + 3y² (p prime, p ≠ 3).

    lagrange3      (xu + 3yv)² + 3(xv − yu)² = (x² + 3y²)(u² + 3v²)
    no_three_sq    3Z² ≠ P² for P prime ≠ 3                            (Euclid in ℤ/3, then 3 | P)
    case_plus3     P | xu + 3yv ⇒ u = ±x
    eisen_unique   x² + 3y² = p = u² + 3v² ⇒ u² = x², v² = y²          ((xu + 3yv)(xu − 3yv) = p(x² + u² − p))
  The Eisenstein analogue of FermatUnique: the prime of ℤ[ω] above a split p is unique up to units and conjugation,
  so the Hecke character of ℚ(√−3) has a well-defined value at p up to the sextic units.

  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.FermatUnique
import lang.Eisen

namespace EisenUnique

open FTAChain LibInt FermatUnique

theorem lagrange3 (x y u v : Int) :
    (x * u + 3 * (y * v)) * (x * u + 3 * (y * v)) + 3 * ((x * v - y * u) * (x * v - y * u))
      = (x * x + 3 * (y * y)) * (u * u + 3 * (v * v)) := by lib_int

theorem nat_of_three (n : Nat) (k : Int) (h : Int.ofNat n = 3 * k) : ∃ q : Nat, n = q * 3 := by
  cases k with
  | ofNat q => exact ⟨q, Int.ofNat.inj (h.trans (by show Int.ofNat (3 * q) = Int.ofNat (q * 3); rw [Nat.mul_comm]))⟩
  | negSucc q =>
    exfalso
    have e : (3 : Int) * Int.negSucc q = Int.negSucc (3 * q + 2) := by
      show -(3 * Int.ofNat (q + 1)) = Int.negSucc (3 * q + 2)
      rw [show (3 : Int) * Int.ofNat (q + 1) = Int.ofNat (3 * q + 2 + 1) by
        show Int.ofNat (3 * (q + 1)) = Int.ofNat (3 * q + 2 + 1); rw [show 3 * (q + 1) = 3 * q + 2 + 1 by lib_nat]]
      rfl
    rw [e] at h
    exact Int.noConfusion h

theorem no_three_sq (p : Nat) (hp : ∀ d, Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (h3 : p + 1 + 1 ≠ 3) (Z : Int)
    (h : 3 * (Z * Z) = Int.ofNat (p + 1 + 1) * Int.ofNat (p + 1 + 1)) : False := by
  have bad : ∀ k, Int.ofNat (p + 1 + 1) = 3 * k → False := fun k hk => by
    match nat_of_three _ k hk with
    | ⟨q, hq⟩ =>
      cases hp 3 ⟨q, hq⟩ with
      | inl e => exact Nat.noConfusion (Nat.succ.inj e)
      | inr e => exact h3 e.symm
  cases int_euclid 1 Eisen.prime3 _ _ (Z * Z) h.symm with
  | inl hu => match hu with | ⟨k, hk⟩ => exact bad k hk
  | inr hu => match hu with | ⟨k, hk⟩ => exact bad k hk

theorem case_plus3 (p : Nat) (hp : ∀ d, Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (h3 : p + 1 + 1 ≠ 3)
    (x y u v w : Int) (h1 : x * x + 3 * (y * y) = Int.ofNat (p + 1 + 1))
    (h2 : u * u + 3 * (v * v) = Int.ofNat (p + 1 + 1)) (hw : x * u + 3 * (y * v) = Int.ofNat (p + 1 + 1) * w) :
    u = x ∨ u = -x := by
  have hLag := lagrange3 x y u v
  have hu : (x * x + 3 * (y * y)) * u = x * (x * u + 3 * (y * v)) - 3 * (y * (x * v - y * u)) := by lib_int
  rw [h1, h2, hw] at hLag
  rw [h1, hw] at hu
  generalize x * v - y * u = Zz at hLag hu
  have hthree := no_three_sq p hp h3 Zz
  generalize hP : Int.ofNat (p + 1 + 1) = P at hLag hu hthree
  match FinCubicSign.sq_nn w, FinCubicSign.sq_nn Zz, FinCubicSign.sq_nn P with
  | ⟨nw, hnw⟩, ⟨nz, hnz⟩, ⟨pp, hpp⟩ =>
    have key : Int.ofNat (pp * nw + 3 * nz) = Int.ofNat (pp + 0) := by
      show Int.ofNat pp * Int.ofNat nw + 3 * Int.ofNat nz = Int.ofNat pp
      rw [← hpp, ← hnw, ← hnz, show P * P * (w * w) + 3 * (Zz * Zz) = P * w * (P * w) + 3 * (Zz * Zz) by lib_int]; exact hLag
    have hk := Int.ofNat.inj key
    have hpp0 : pp ≠ 0 := fun e => by
      have h0 : P * P = 0 := by rw [hpp, e]; rfl
      have hP0 := HasseDavenport.sq_zero P h0
      rw [← hP] at hP0; exact Nat.noConfusion (Int.ofNat.inj hP0)
    match nw with
    | 0 =>
      exfalso
      apply hthree
      have e : 3 * nz = pp := by rw [show pp * 0 + 3 * nz = 3 * nz by lib_nat] at hk; exact hk
      rw [hnz, hpp, ← e]; rfl
    | 1 =>
      have hz0 : 3 * nz = 0 := add_cancel pp _ _ ((show pp + 3 * nz = pp * 1 + 3 * nz by lib_nat).trans hk)
      have hz0' : nz = 0 := (HasseDavenport.nat_sum_zero nz (nz + nz) ((show nz + (nz + nz) = 3 * nz by lib_nat).trans hz0)).1
      have z0 : Zz = 0 := HasseDavenport.sq_zero Zz (by rw [hnz, hz0']; rfl)
      rw [z0] at hu
      cases QuarticFamily.sq_eq w 1 (by rw [hnw]; rfl) with
      | inl e =>
        left; rw [e] at hu
        exact cancelQ p P hP u x (by rw [hu]; lib_int)
      | inr e =>
        right; rw [e] at hu
        exact cancelQ p P hP u (-x) (by rw [hu]; lib_int)
    | n + 2 =>
      exfalso
      have h3' : pp * (n + 1) + 3 * nz = 0 := add_cancel pp _ _ ((show pp + (pp * (n + 1) + 3 * nz) = pp * (n + 2) + 3 * nz by lib_nat).trans hk)
      have h4 := (HasseDavenport.nat_sum_zero _ _ h3').1
      have h5 : pp + pp * n = 0 := (show pp + pp * n = pp * (n + 1) by lib_nat).trans h4
      exact hpp0 (HasseDavenport.nat_sum_zero _ _ h5).1

/-- **Uniqueness of p = x² + 3y².** -/
theorem eisen_unique (p : Nat) (hp : ∀ d, Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (h3 : p + 1 + 1 ≠ 3) (x y u v : Int)
    (h1 : x * x + 3 * (y * y) = Int.ofNat (p + 1 + 1)) (h2 : u * u + 3 * (v * v) = Int.ofNat (p + 1 + 1)) :
    u * u = x * x := by
  have key : (x * u + 3 * (y * v)) * (x * u + 3 * (y * (-v))) = Int.ofNat (p + 1 + 1) * (x * x + u * u + -1 * Int.ofNat (p + 1 + 1)) := by
    have hy : 3 * (y * y) = Int.ofNat (p + 1 + 1) + -1 * (x * x) := by rw [← h1]; lib_int
    have hv : 3 * (v * v) = Int.ofNat (p + 1 + 1) + -1 * (u * u) := by rw [← h2]; lib_int
    rw [show (x * u + 3 * (y * v)) * (x * u + 3 * (y * (-v))) = x * x * (u * u) + -1 * (3 * (y * y) * (3 * (v * v))) by lib_int, hy, hv]
    lib_int
  have fin : u = x ∨ u = -x → u * u = x * x := fun h => by
    cases h with
    | inl e => rw [e]
    | inr e => rw [e]; lib_int
  cases int_euclid p hp _ _ _ key with
  | inl h => match h with | ⟨w, hw⟩ => exact fin (case_plus3 p hp h3 x y u v w h1 h2 hw)
  | inr h => match h with | ⟨w, hw⟩ => exact fin (case_plus3 p hp h3 x y u (-v) w h1 (by rw [← h2]; lib_int) hw)

end EisenUnique

#print axioms EisenUnique.lagrange3
#print axioms EisenUnique.nat_of_three
#print axioms EisenUnique.no_three_sq
#print axioms EisenUnique.case_plus3
#print axioms EisenUnique.eisen_unique

lang/Ell11.lean

lean_all/lang/Ell11.lean

/-
  lang/Ell11 — T43: Dirichlet for p ≡ −1 (mod 11), part 1 (ring-theoretic tools), generated.
    phi        z·w = 1, f = minimal polynomial of 2cos(2π/11)  ⟹  z^5 f(z + w) = Φ_11(z)
    ellth      Φ_11(z) = 0  ⟹  z^11 = 1
    at_one     Φ_11(z) = 0, z = 1  ⟹  11 = 0
    one_of     z^11 = 1, z^r = 1, r·a = 11b + 1  ⟹  z = 1
    key        Φ_11(z) = 0, z^N = 1  ⟹  11 | N or 11 = 0
  f(X) = 1·X^5 + 1·X^4 + -4·X^3 + -3·X^2 + 3·X^1 + 1·X^0.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Order
import lang.TwoPower
import lib.RingTac

namespace Ell11

open FTAChain

variable {R : Type} (K : DetChain.CRing R)

theorem phi (u v : R) (huv : K.mul u v = K.one) :
    K.mul (K.mul (K.mul (K.mul (K.mul (u) (u)) (u)) (u)) (u)) (K.add (K.add (K.add (K.add (K.add (K.mul (K.mul (K.mul (K.mul (K.add (u) (v)) (K.add (u) (v))) (K.add (u) (v))) (K.add (u) (v))) (K.add (u) (v))) (K.mul (K.mul (K.mul (K.add (u) (v)) (K.add (u) (v))) (K.add (u) (v))) (K.add (u) (v)))) (K.neg (K.mul (K.add (K.add (K.add (K.one) (K.one)) (K.one)) (K.one)) (K.mul (K.mul (K.add (u) (v)) (K  -- (+879 chars; full line in the Zenodo source)
  calc _ = K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.one) (u)) (K.mul (u) (u))) (K.mul (K.mul (u) (u)) (u))) (K.mul (K.mul (K.mul (u) (u)) (u)) (u))) (K.mul (K.mul (K.mul (K.mul (u) (u)) (u)) (u)) (u))) (K.mul (K.mul (K.mul (K.mul (K.mul (u) (u)) (u)) (u)) (u)) (u))) (K.mul (K.mul (K.mul (K.mul (K.mul (K.mul (u) (u)) (u)) (u)) (u)) (u)) (u))) (K.mul (K.mul (K.m  -- (+2235 chars; full line in the Zenodo source)
    _ = _ := by rw [huv]; lib_ring

theorem ellth (u : R) (h : K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.one) (u)) (K.mul (u) (u))) (K.mul (K.mul (u) (u)) (u))) (K.mul (K.mul (K.mul (u) (u)) (u)) (u))) (K.mul (K.mul (K.mul (K.mul (u) (u)) (u)) (u)) (u))) (K.mul (K.mul (K.mul (K.mul (K.mul (u) (u)) (u)) (u)) (u)) (u))) (K.mul (K.mul (K.mul (K.mul (K.mul (K.mul (u) (u)) (u)) (u)) (u)) (u)) (u))) (K.mul (K  -- (+338 chars; full line in the Zenodo source)
  show K.mul (K.mul (K.mul (K.mul (K.mul (K.mul (K.mul (K.mul (K.mul (K.mul (K.mul (K.one) u) u) u) u) u) u) u) u) u) u) u = K.one
  calc _ = K.add (K.mul (K.add u (K.neg K.one)) (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.one) (u)) (K.mul (u) (u))) (K.mul (K.mul (u) (u)) (u))) (K.mul (K.mul (K.mul (u) (u)) (u)) (u))) (K.mul (K.mul (K.mul (K.mul (u) (u)) (u)) (u)) (u))) (K.mul (K.mul (K.mul (K.mul (K.mul (u) (u)) (u)) (u)) (u)) (u))) (K.mul (K.mul (K.mul (K.mul (K.mul (K.mul (u) (u)) (u)) (u)) (u)  -- (+337 chars; full line in the Zenodo source)
    _ = K.one := by rw [h]; lib_ring

theorem at_one (u : R) (h : K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.one) (u)) (K.mul (u) (u))) (K.mul (K.mul (u) (u)) (u))) (K.mul (K.mul (K.mul (u) (u)) (u)) (u))) (K.mul (K.mul (K.mul (K.mul (u) (u)) (u)) (u)) (u))) (K.mul (K.mul (K.mul (K.mul (K.mul (u) (u)) (u)) (u)) (u)) (u))) (K.mul (K.mul (K.mul (K.mul (K.mul (K.mul (u) (u)) (u)) (u)) (u)) (u)) (u))) (K.mul (  -- (+498 chars; full line in the Zenodo source)
  rw [hu] at h
  calc _ = K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.one) (K.one)) (K.mul (K.one) (K.one))) (K.mul (K.mul (K.one) (K.one)) (K.one))) (K.mul (K.mul (K.mul (K.one) (K.one)) (K.one)) (K.one))) (K.mul (K.mul (K.mul (K.mul (K.one) (K.one)) (K.one)) (K.one)) (K.one))) (K.mul (K.mul (K.mul (K.mul (K.mul (K.one) (K.one)) (K.one)) (K.one)) (K.one)) (K.one))) (K.mul (K.mul (K.m  -- (+511 chars; full line in the Zenodo source)
    _ = K.zero := h

theorem one_of (u : R) (hL : Frobenius.pw K u 11 = K.one) (r a b : Nat) (hr : Frobenius.pw K u r = K.one) (hab : r * a = 11 * b + 1) :
    u = K.one := by
  calc u = K.mul (Frobenius.pw K (Frobenius.pw K u 11) b) u := by rw [hL, TwoPower.pwone]; lib_ring
    _ = Frobenius.pw K u (11 * b + 1) := by rw [TwoPower.pwpow]; rfl
    _ = Frobenius.pw K (Frobenius.pw K u r) a := by rw [TwoPower.pwpow, hab]
    _ = K.one := by rw [hr, TwoPower.pwone]

theorem key (u : R) (h : K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.one) (u)) (K.mul (u) (u))) (K.mul (K.mul (u) (u)) (u))) (K.mul (K.mul (K.mul (u) (u)) (u)) (u))) (K.mul (K.mul (K.mul (K.mul (u) (u)) (u)) (u)) (u))) (K.mul (K.mul (K.mul (K.mul (K.mul (u) (u)) (u)) (u)) (u)) (u))) (K.mul (K.mul (K.mul (K.mul (K.mul (K.mul (u) (u)) (u)) (u)) (u)) (u)) (u))) (K.mul (K.m  -- (+346 chars; full line in the Zenodo source)
    Dv 11 N ∨ K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.zero) K.one) K.one) K.one) K.one) K.one) K.one) K.one) K.one) K.one) K.one) K.one = K.zero := by
  have hL := ellth K u h
  obtain ⟨q, r, hqr, hr⟩ := Order.divmod 10 N
  have hxr : Frobenius.pw K u r = K.one := by
    rw [← Order.pw_qe K u 11 r hL q]; rw [← hqr]; exact hN
  match r, hr, hxr, hqr with
  | 0, _, _, hqr => exact Or.inl ⟨q, by rw [hqr]; rfl⟩
  | 1, _, hxr, _ => exact Or.inr (at_one K u h (one_of K u hL 1 1 0 hxr rfl))
  | 2, _, hxr, _ => exact Or.inr (at_one K u h (one_of K u hL 2 6 1 hxr rfl))
  | 3, _, hxr, _ => exact Or.inr (at_one K u h (one_of K u hL 3 4 1 hxr rfl))
  | 4, _, hxr, _ => exact Or.inr (at_one K u h (one_of K u hL 4 3 1 hxr rfl))
  | 5, _, hxr, _ => exact Or.inr (at_one K u h (one_of K u hL 5 9 4 hxr rfl))
  | 6, _, hxr, _ => exact Or.inr (at_one K u h (one_of K u hL 6 2 1 hxr rfl))
  | 7, _, hxr, _ => exact Or.inr (at_one K u h (one_of K u hL 7 8 5 hxr rfl))
  | 8, _, hxr, _ => exact Or.inr (at_one K u h (one_of K u hL 8 7 5 hxr rfl))
  | 9, _, hxr, _ => exact Or.inr (at_one K u h (one_of K u hL 9 5 4 hxr rfl))
  | 10, _, hxr, _ => exact Or.inr (at_one K u h (one_of K u hL 10 10 9 hxr rfl))
  | r + 11, hr, _, _ => exact absurd hr (fun h => Nat.lt_irrefl 11 (Nat.lt_of_le_of_lt (Nat.le_add_left 11 r) h))

end Ell11

#print axioms Ell11.phi
#print axioms Ell11.ellth
#print axioms Ell11.at_one
#print axioms Ell11.one_of
#print axioms Ell11.key

lang/Ell11b.lean

lean_all/lang/Ell11b.lean

/-
  lang/Ell11b — T43: Dirichlet for p ≡ −1 (mod 11), part 2: a root of f mod q (q = 2m + 3) forces 11 | q − 1, 11 | q + 1
  or 11 = 0 in 𝔽_q (cases D = r² − 4 zero / nonzero square / non-square, as in Seven7b). Generated.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Ell11
import lang.EulerHard
import lang.Euler
import lang.G4Map
import lang.Fq2

namespace Ell11

open FTAChain ZMod YMChain ConicSum G4Gen G4Map

variable (m : Nat)

def fr (x : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (x) (x)) (x)) (x)) (x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m  -- (+815 chars; full line in the Zenodo source)
def lnum : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).zero) (ring (2 * m + 1 + 1)).one) (r  -- (+276 chars; full line in the Zenodo source)

theorem root_D0 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x : Zp (2 * m + 1 + 1)) (hf : fr m x = (ring (2 * m + 1 + 1)).zero) (hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ri  -- (+132 chars; full line in the Zenodo source)
  unfold fr at hf
  have h := mul_eq_zero m hpr ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) (by rw [← hD]; lib_ring)
  rcases h with e | e
  · have hx : x = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one) := by
      calc x = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) := by lib_ring
        _ = _ := by rw [e]; lib_ring
    rw [hx] at hf
    unfold lnum
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1  -- (+2032 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := hf
  · have hx : x = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) := by
      calc x = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) := by lib_ring
        _ = _ := by rw [e]; lib_ring
    rw [hx] at hf
    exact absurd (show (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).zero by
      calc (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).o  -- (+2523 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [hf]; lib_ring) (one_ne m)

theorem root_sq (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h2 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one) ≠ (ring (2 * m + 1 + 1)).zero) (x : Zp (2 * m + 1 + 1)) (hf : fr m x = (ring (2 * m + 1 + 1)).zero)
    (s : Zp (2 * m + 1 + 1)) (hs : (ring (2 * m + 1 + 1)).mul s s = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))))) : Dv 11 (2 * m + 1 + 1) ∨ lnum m =   -- (+33 chars; full line in the Zenodo source)
  have ei : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ h2
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) = i at ei
  obtain ⟨u, hu⟩ : ∃ u : Zp (2 * m + 1 + 1), u = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x s) i := ⟨_, rfl⟩
  obtain ⟨v, hv⟩ : ∃ v : Zp (2 * m + 1 + 1), v = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg s)) i := ⟨_, rfl⟩
  have huv : (ring (2 * m + 1 + 1)).mul u v = (ring (2 * m + 1 + 1)).one := by
    rw [hu, hv]
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul i i) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul s s)))) (ring (2 * m + 1 + 1)).zero := by lib_ring
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) := by rw [hs]; lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [ei]; lib_ring
  have hsum : (ring (2 * m + 1 + 1)).add u v = x := by
    rw [hu, hv]
    calc _ = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) := by lib_ring
      _ = x := by rw [ei]; lib_ring
  have hp := phi (ring (2 * m + 1 + 1)) u v huv
  rw [hsum] at hp
  unfold fr at hf
  rw [hf] at hp
  have hu0 : u ≠ (ring (2 * m + 1 + 1)).zero := fun e => one_ne m (by rw [← huv, e]; lib_ring)
  have hferm : Frobenius.pw (ring (2 * m + 1 + 1)) u (2 * m + 1 + 1) = (ring (2 * m + 1 + 1)).one := by
    have hF := ZMod.fermat (2 * m + 1) hpr u
    have hz : (ring (2 * m + 1 + 1)).mul u ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) u (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) u (2 * m + 1 + 1)) u) ((ring (2 * m + 1 + 1)).neg u) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [show (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) u (2 * m + 1 + 1)) u = u from hF]; lib_ring
    rcases mul_eq_zero m hpr _ _ hz with e | e
    · exact absurd e hu0
    · calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) u (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
        _ = (ring (2 * m + 1 + 1)).one := by rw [e]; lib_ring
  exact key (ring (2 * m + 1 + 1)) u (by rw [← hp]; lib_ring) _ hferm

theorem e_add (a b : Zp (2 * m + 1 + 1)) (n : Zp (2 * m + 1 + 1)) : (Fq2.G2 (2 * m + 1) n).add (a, (ring (2 * m + 1 + 1)).zero) (b, (ring (2 * m + 1 + 1)).zero) = ((ring (2 * m + 1 + 1)).add a b, (ring (2 * m + 1 + 1)).zero) :=
  Prod.ext rfl (by show (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero = (ring (2 * m + 1 + 1)).zero; lib_ring)
theorem e_neg (a : Zp (2 * m + 1 + 1)) (n : Zp (2 * m + 1 + 1)) : (Fq2.G2 (2 * m + 1) n).neg (a, (ring (2 * m + 1 + 1)).zero) = ((ring (2 * m + 1 + 1)).neg a, (ring (2 * m + 1 + 1)).zero) :=
  Prod.ext rfl (by show (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).zero = (ring (2 * m + 1 + 1)).zero; lib_ring)

theorem root_nsq (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h2 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one) ≠ (ring (2 * m + 1 + 1)).zero) (x : Zp (2 * m + 1 + 1)) (hf : fr m x = (ring (2 * m + 1 + 1)).zero)
    (hn : Frobenius.pw (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))))) (m + 1) = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 +   -- (+85 chars; full line in the Zenodo source)
  have ei : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ h2
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) = i at ei
  have hzw : (Fq2.G2 (2 * m + 1) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)))))).mul ((ring (2 * m + 1 + 1)).mul x i, i) ((ring (2 * m + 1 + 1)).mul   -- (+365 chars; full line in the Zenodo source)
    apply Prod.ext
    · show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x i) ((ring (2 * m + 1 + 1)).mul x i)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((r  -- (+150 chars; full line in the Zenodo source)
      calc _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).one := by rw [ei]; lib_ring
    · show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x i) ((ring (2 * m + 1 + 1)).neg i)) ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul x i)) = (ring (2 * m + 1 + 1)).zero
      lib_ring
  have hsum : (Fq2.G2 (2 * m + 1) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)))))).add ((ring (2 * m + 1 + 1)).mul x i, i) ((ring (2 * m + 1 + 1)).mul  -- (+76 chars; full line in the Zenodo source)
    apply Prod.ext
    · show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x i) ((ring (2 * m + 1 + 1)).mul x i) = x
      calc _ = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) := by lib_ring
        _ = x := by rw [ei]; lib_ring
    · show (ring (2 * m + 1 + 1)).add i ((ring (2 * m + 1 + 1)).neg i) = (ring (2 * m + 1 + 1)).zero
      lib_ring
  have hp := phi (Fq2.G2 (2 * m + 1) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)))))) _ _ hzw
  rw [hsum] at hp
  have hfe : (Fq2.G2 (2 * m + 1) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)))))).add ((Fq2.G2 (2 * m + 1) ((ring (2 * m + 1 + 1)).add ((ring (2 * m +  -- (+12817 chars; full line in the Zenodo source)
    rw [show (Fq2.G2 (2 * m + 1) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)))))).one = ((ring (2 * m + 1 + 1)).one, (ring (2 * m + 1 + 1)).zero) from  -- (+5 chars; full line in the Zenodo source)
    repeat (first | rw [Fq2.embed_mul] | rw [e_add] | rw [e_neg])
    apply Prod.ext
    · show _ = (ring (2 * m + 1 + 1)).zero
      rw [← hf]; unfold fr; rfl
    · rfl
  rw [hfe, DetChain.mul_zero] at hp
  have hq := Fq2.pw_p1 (2 * m + 1) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))))) (m + 1) hpr rfl hn ((ring (2 * m + 1 + 1)).mul x i, i)
  have hN : Frobenius.pw (Fq2.G2 (2 * m + 1) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)))))) ((ring (2 * m + 1 + 1)).mul x i, i) (2 * m + 1 + 1 + 1 +  -- (+334 chars; full line in the Zenodo source)
    rw [hq]
    apply Prod.ext
    · show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x i) ((ring (2 * m + 1 + 1)).mul x i)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((r  -- (+150 chars; full line in the Zenodo source)
      calc _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).one := by rw [ei]; lib_ring
    · rfl
  rcases key (Fq2.G2 (2 * m + 1) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)))))) _ hp.symm _ hN with h | h
  · exact Or.inl h
  · have e : (Fq2.G2 (2 * m + 1) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)))))).add ((Fq2.G2 (2 * m + 1) ((ring (2 * m + 1 + 1)).add ((ring (2 * m +  -- (+7747 chars; full line in the Zenodo source)
      rw [show (Fq2.G2 (2 * m + 1) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)))))).one = ((ring (2 * m + 1 + 1)).one, (ring (2 * m + 1 + 1)).zero) fr  -- (+407 chars; full line in the Zenodo source)
      repeat rw [e_add]
    rw [e, show (Fq2.G2 (2 * m + 1) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)))))).zero = ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).zero)  -- (+15 chars; full line in the Zenodo source)
    have h' := congrArg Prod.fst h
    exact Or.inr h'

theorem root (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h2 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one) ≠ (ring (2 * m + 1 + 1)).zero) (x : Zp (2 * m + 1 + 1)) (hf : fr m x = (ring (2 * m + 1 + 1)).zero) :
    Dv 11 (2 * m + 1 + 1) ∨ Dv 11 (2 * m + 1 + 1 + 1 + 1) ∨ lnum m = (ring (2 * m + 1 + 1)).zero := by
  by_cases hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)))) = (ring (2 * m + 1 + 1)).zero
  · exact Or.inr (Or.inr (root_D0 m hpr x hf hD))
  · rcases Euler.euler_pm m hpr _ hD with h | h
    · obtain ⟨s, hs⟩ := EulerHard.euler_hard m hpr _ h
      rcases root_sq m hpr h2 x hf s hs with h' | h'
      · exact Or.inl h'
      · exact Or.inr (Or.inr h')
    · rcases root_nsq m hpr h2 x hf h with h' | h'
      · exact Or.inr (Or.inl h')
      · exact Or.inr (Or.inr h')

end Ell11

#print axioms Ell11.root_D0
#print axioms Ell11.root_sq
#print axioms Ell11.e_add
#print axioms Ell11.e_neg
#print axioms Ell11.root_nsq
#print axioms Ell11.root

lang/Ell11c.lean

lean_all/lang/Ell11c.lean

/-
  lang/Ell11c — T43: **infinitely many primes p ≡ 10 (mod 11)** (Euclid-type, f = minimal polynomial of 2cos(2π/11)).
  g(n) = −f(−n) = Q(n) − P(n) with Q = 1n^5 + 3n^2 + 3n^1,
  P = 1n^4 + 4n^3 + 1. For n = 11·B!, N₀ = g(n) ≡ −1 (mod 11); every prime factor q is odd, f has a
  root mod q, so (Ell11b.root) q ≡ ±1 (mod 11) or q = 11, and q = 11 is impossible; some factor is ≡ 10 and it does not divide n.
    mod11, cofactor, rem_minus, l_zero, allowed, has, inf    (generated)
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Ell11b
import lang.Fp2Sqrt
import lang.PrimeOneModQ
import lang.TwoPower
import lang.Nine9c

namespace Ell11

open FTAChain ZMod InfSplit

theorem add_cancel (a b : Nat) : ∀ c, a + c = b + c → a = b
  | 0, h => h
  | c + 1, h => add_cancel a b c (Nat.succ.inj h)

theorem le_dest (a b : Nat) (h : a ≤ b) : ∃ c, c + a = b := by
  induction h with
  | refl => exact ⟨0, Nat.zero_add a⟩
  | step _ ih =>
    obtain ⟨c, hc⟩ := ih
    exact ⟨c + 1, by rw [← hc]; lib_nat⟩

theorem le_mul_self (a n : Nat) (hn : 1 ≤ n) : a ≤ a * n :=
  match n, hn with
  | n' + 1, _ => Nat.le_trans (Nat.le_add_left a (a * n')) (Nat.le_of_eq (by lib_nat))

theorem modL (n : Nat) : ∃ a r, n = a * 11 + r ∧ r < 11 :=
  match div_exists 10 n with
  | ⟨a, r, k, hn, hr⟩ => ⟨a, r, hn, Nat.lt_of_lt_of_le (Nat.lt_succ_self r) (by rw [show (11 : Nat) = 10 + 1 from rfl, ← hr]; exact Nat.succ_le_succ (Nat.le_add_right r k))⟩

theorem cofactor (b N' a : Nat) (h : (b * 11 + 1) * N' = a * 11 + 10) : ∃ c, N' = c * 11 + 10 :=
  match modL N' with
  | ⟨c, r, hc, hr⟩ => by
    have e : (b * 11 + 1) * N' = (b * 11 * c + b * r + c) * 11 + r := by rw [hc]; lib_nat
    rw [e] at h
    exact ⟨c, by rw [hc, Nine.uniq_rem 11 _ _ r 10 hr (Nat.lt_succ_self 10) h]⟩

theorem rem_minus (N A B : Nat) (h : N + (A * 11 + 1) = B * 11) : ∃ c, N = c * 11 + 10 := by
  obtain ⟨c, r, hc, hr⟩ := modL N
  cases Nat.lt_or_ge (r + 1) 11 with
  | inl hlt =>
    have e : (c + A) * 11 + (r + 1) = B * 11 + 0 := by rw [Nat.add_zero, ← h, hc]; lib_nat
    exact absurd (Nine.uniq_rem 11 _ _ _ _ hlt (Nat.zero_lt_succ 10) e) (fun h' => Nat.noConfusion h')
  | inr hge =>
    have e : r = 10 := Nat.le_antisymm (Nat.le_of_lt_succ hr) (Nat.le_of_succ_le_succ hge)
    exact ⟨c, by rw [hc, e]⟩

theorem l_zero (m : Nat) (h : lnum m = (ring (2 * m + 1 + 1)).zero) : 2 * m + 1 + 1 + 1 = 11 := by
  have e : lnum m = mk (2 * m + 1 + 1) 11 := by
    unfold lnum
    rw [show (ring (2 * m + 1 + 1)).zero = mk (2 * m + 1 + 1) 0 from rfl, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one]
  rw [e, show (ring (2 * m + 1 + 1)).zero = mk (2 * m + 1 + 1) 0 from rfl] at h
  unfold mk at h
  have h0 : md (2 * m + 1 + 1) 11 = md (2 * m + 1 + 1) 0 := Subtype.mk.inj h
  match m, h0 with
  | 0, h0 => exact absurd h0 (fun h' => Nat.noConfusion h')
  | 1, h0 => exact absurd h0 (fun h' => Nat.noConfusion h')
  | 2, h0 => exact absurd h0 (fun h' => Nat.noConfusion h')
  | 3, h0 => exact absurd h0 (fun h' => Nat.noConfusion h')
  | 4, _ => rfl
  | k + 5, h0 =>
    rw [md_small _ 11 (Nat.lt_of_lt_of_le (Nat.lt_succ_self 11) (Nat.le_trans (Nat.le_add_left 12 (2 * k + 1)) (Nat.le_of_eq (by lib_nat))))] at h0
    exact Nat.noConfusion h0

theorem allowed (n N l : Nat) (hq : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1)
    (hN : N + (n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n + 1) = n * n * n * n * n + n * n + n * n + n * n + n + n + n) (hdv : Dv (l + 1 + 1) N) :
    (∃ a, l + 1 + 1 = a * 11 + 1) ∨ (∃ a, l + 1 + 1 = a * 11 + 10) ∨ l + 1 + 1 = 11 := by
  rcases EveryPrime.nat_even_odd l with ⟨j, hj⟩ | ⟨m, hm⟩
  · exfalso
    have h2 : l + 1 + 1 = 2 := by
      cases hq 2 ⟨j + 1, by rw [hj]; lib_nat⟩ with
      | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
      | inr e => exact e.symm
    rw [h2] at hdv
    obtain ⟨r, hr⟩ := hdv
    rw [hr] at hN
    rcases EveryPrime.nat_even_odd n with ⟨i, hi⟩ | ⟨i, hi⟩
    · have hQe : n * n * n * n * n + n * n + n * n + n * n + n + n + n = (n * n * n * n * i + n * i + n * i + n * i + i + i + i) * 2 := by
        calc _ = n * n * n * n * (2 * i) + n * (2 * i) + n * (2 * i) + n * (2 * i) + (2 * i) + (2 * i) + (2 * i) := by rw [← hi]
          _ = _ := by lib_nat
      have hPe : n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n = (n * n * n * i + n * n * i + n * n * i + n * n * i + n * n * i) * 2 := by
        calc _ = n * n * n * (2 * i) + n * n * (2 * i) + n * n * (2 * i) + n * n * (2 * i) + n * n * (2 * i) := by rw [← hi]
          _ = _ := by lib_nat
      exact odd_not_dv2 (r + (n * n * n * i + n * n * i + n * n * i + n * n * i + n * n * i)) (n * n * n * n * i + n * i + n * i + n * i + i + i + i) (by
        calc _ = r * 2 + (n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n + 1) := by rw [hPe]; lib_nat
          _ = _ := hN
          _ = _ := hQe)
    · have hQo : n * n * n * n * n + n * n + n * n + n * n + n + n + n = 7 + (i * ((1 + n + n * n + n * n * n + n * n * n * n) + (1 + n) + (1 + n) + (1 + n) + (1) + (1) + (1))) * 2 := add_cancel _ _ ((1 + n + n * n + n * n * n + n * n * n * n) + (1 + n) + (1 + n) + (1 + n) + (1) + (1) + (1)) (by
        calc n * n * n * n * n + n * n + n * n + n * n + n + n + n + ((1 + n + n * n + n * n * n + n * n * n * n) + (1 + n) + (1 + n) + (1 + n) + (1) + (1) + (1)) = 7 + n * ((1 + n + n * n + n * n * n + n * n * n * n) + (1 + n) + (1 + n) + (1 + n) + (1) + (1) + (1)) := by lib_nat
          _ = 7 + (2 * i + 1) * ((1 + n + n * n + n * n * n + n * n * n * n) + (1 + n) + (1 + n) + (1 + n) + (1) + (1) + (1)) := by rw [hi]
          _ = _ := by lib_nat)
      have hPo : n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n = 5 + (i * ((1 + n + n * n + n * n * n) + (1 + n + n * n) + (1 + n + n * n) + (1 + n + n * n) + (1 + n + n * n))) * 2 := add_cancel _ _ ((1 + n + n * n + n * n * n) + (1 + n + n * n) + (1 + n + n * n) + (1 + n + n * n) + (1 + n + n * n)) (by
        calc n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n + ((1 + n + n * n + n * n * n) + (1 + n + n * n) + (1 + n + n * n) + (1 + n + n * n) + (1 + n + n * n)) = 5 + n * ((1 + n + n * n + n * n * n) + (1 + n + n * n) + (1 + n + n * n) + (1 + n + n * n) + (1 + n + n * n)) := by lib_nat
          _ = 5 + (2 * i + 1) * ((1 + n + n * n + n * n * n) + (1 + n + n * n) + (1 + n + n * n) + (1 + n + n * n) + (1 + n + n * n)) := by rw [hi]
          _ = _ := by lib_nat)
      exact odd_not_dv2 (3 + i * ((1 + n + n * n + n * n * n + n * n * n * n) + (1 + n) + (1 + n) + (1 + n) + (1) + (1) + (1))) (r + i * ((1 + n + n * n + n * n * n) + (1 + n + n * n) + (1 + n + n * n) + (1 + n + n * n) + (1 + n + n * n)) + 3) (by
        calc _ = 7 + (i * ((1 + n + n * n + n * n * n + n * n * n * n) + (1 + n) + (1 + n) + (1 + n) + (1) + (1) + (1))) * 2 := by lib_nat
          _ = n * n * n * n * n + n * n + n * n + n * n + n + n + n := hQo.symm
          _ = r * 2 + (n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n + 1) := hN.symm
          _ = _ := by rw [hPo]; lib_nat)
  · subst hm
    have hx : fr m ((ring (2 * m + 1 + 1)).neg (mk (2 * m + 1 + 1) n)) = (ring (2 * m + 1 + 1)).zero := by
      have h1 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((mk (2 * m + 1 + 1) n)) (mk (2 * m + 1 + 1) n)) (mk (2 * m + 1 + 1) n)) (mk (2 * m + 1 + 1) n)) (mk (2 *  -- (+409 chars; full line in the Zenodo source)
        repeat (first | rw [TwoPower.mk_mul] | rw [PrimeOneModQ.mk_add] | rw [TwoPower.mk_add_one])
      have h2 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((mk (2 * m + 1 + 1) n)) (mk (2 * m + 1 + 1) n)) (mk (2 * m + 1 + 1) n)) (mk (2 * m + 1 + 1) n)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((mk (2   -- (+577 chars; full line in the Zenodo source)
        repeat (first | rw [TwoPower.mk_mul] | rw [PrimeOneModQ.mk_add] | rw [TwoPower.mk_add_one])
      have h0 : mk (2 * m + 1 + 1) N = (ring (2 * m + 1 + 1)).zero := Subtype.ext (by
        show md (2 * m + 1 + 1) N = md (2 * m + 1 + 1) 0
        rw [dv_md_zero _ N hdv]; rfl)
      have h3 : (ring (2 * m + 1 + 1)).add (mk (2 * m + 1 + 1) N) (mk (2 * m + 1 + 1) (n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n + 1)) = mk (2 * m + 1 + 1) (n * n * n * n * n + n * n + n * n + n * n + n + n + n) := by
        rw [PrimeOneModQ.mk_add, hN]
      rw [h0, ← h1, ← h2] at h3
      unfold fr
      generalize mk (2 * m + 1 + 1) n = x at h3
      calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (x) x) x) x) x) ((ring (2 * m + 1 + 1)).mul (x) x)  -- (+666 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (x) x) x) x) ((ring (2 * m + 1 + 1)).mul ((ring (2 *   -- (+833 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by lib_ring
    rcases root m hq (Fp2Sqrt.two_ne m) _ hx with ⟨a, ha⟩ | ⟨a, ha⟩ | hl
    · exact Or.inl ⟨a, by rw [← ha]⟩
    · match a, ha with
      | 0, ha => exact absurd ha (fun h' => Nat.noConfusion h')
      | a + 1, ha => exact Or.inr (Or.inl ⟨a, Nat.succ.inj (by show 2 * m + 1 + 1 + 1 + 1 = a * 11 + 10 + 1; rw [ha, Nat.succ_mul])⟩)
    · exact Or.inr (Or.inr (l_zero m hl))

theorem has (n N0 : Nat) (hN : N0 + (n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n + 1) = n * n * n * n * n + n * n + n * n + n * n + n + n + n) (hc0 : ∃ c, N0 = c * 11 + 10) :
    ∀ G N, N ≤ G → Dv N N0 → (∃ a, N = a * 11 + 10) →
    ∃ q, (∀ d, Dv d q → d = 1 ∨ d = q) ∧ (∃ b, q = b * 11 + 10) ∧ Dv q N
  | 0, N, hN', _, ⟨a, ha⟩ => absurd (ha ▸ hN') (fun h => Nat.not_succ_le_zero _ (Nat.le_trans (Nat.le_add_left _ _) h))
  | G + 1, N, hNG, hN0, ⟨a, ha⟩ => by
    match lp_exists (a * 11 + 8) with
    | ⟨l, hl, hmin⟩ =>
    have hq := lp_prime _ l hl hmin
    have hlN : Dv (l + 1 + 1) N := by rw [ha]; exact hl
    have hlN0 := dv_trans _ _ _ hlN hN0
    rcases allowed n N0 l hq hN hlN0 with ⟨b, hb⟩ | ⟨b, hb⟩ | hL
    · match hlN with
      | ⟨N', hN'⟩ =>
      match cofactor b N' a (by rw [← hb, ← ha, hN']; lib_nat) with
      | ⟨c, hc⟩ =>
      have hlt : N' ≤ G := by
        have e : N = N' + (N' * (l + 1)) := by rw [hN']; lib_nat
        have h1' : 1 ≤ N' * (l + 1) := by rw [hc]; exact Nat.mul_le_mul (Nat.le_add_left 1 _) (Nat.le_add_left 1 _)
        exact Nat.le_of_succ_le_succ (Nat.le_trans (Nat.add_le_add_left h1' N') (by rw [← e]; exact hNG))
      have hN'0 : Dv N' N0 := dv_trans _ _ _ ⟨l + 1 + 1, by rw [hN']; lib_nat⟩ hN0
      match has n N0 hN hc0 G N' hlt hN'0 ⟨c, hc⟩ with
      | ⟨q, hq', hqL, hqN⟩ => exact ⟨q, hq', hqL, dv_trans q N' N hqN ⟨l + 1 + 1, by rw [hN']; lib_nat⟩⟩
    · exact ⟨l + 1 + 1, hq, ⟨b, hb⟩, hlN⟩
    · exfalso
      obtain ⟨c, hc⟩ := hc0
      obtain ⟨r, hr⟩ := hlN0
      rw [hL] at hr
      exact absurd (Nine.uniq_rem 11 c r 10 0 (Nat.lt_succ_self 10) (Nat.zero_lt_succ 10)
        (by rw [← hc, hr]; rfl)) (fun h' => Nat.noConfusion h')

/-- **Infinitely many primes p ≡ 10 (mod 11).** -/
theorem inf (B : Nat) : ∃ b, (∀ d, Dv d (b * 11 + 10) → d = 1 ∨ d = b * 11 + 10) ∧ B < b * 11 + 10 := by
  obtain ⟨f, hf⟩ := fac_pos B
  obtain ⟨n, hk⟩ : ∃ n, n = (f + 1) * 11 := ⟨_, rfl⟩
  have hlen : 11 ≤ n := by rw [hk]; exact Nat.le_trans (Nat.le_add_left 11 (f * 11)) (Nat.le_of_eq (by lib_nat))
  have hM : 6 ≤ n := Nat.le_trans (Nat.le_add_left 6 5) hlen
  have hn : 1 ≤ n := Nat.le_trans (Nat.le_add_left 1 5) hM
  have hle : n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n + 1 ≤ n * n * n * n * n + n * n + n * n + n * n + n + n + n := by
    have h1 : n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n + 1 ≤ (n * n * n * n) + (n * n * n * n) + (n * n * n * n) + (n * n * n * n) + (n * n * n * n) + (n * n * n * n) := Nat.add_le_add (Nat.add_le_add (Nat.add_le_add (Nat.add_le_add (Nat.add_le_add (Nat.le_refl _) (Nat.le_trans (le_mul_self (n * n * n) n hn) (Nat.le_refl _))) (Nat.le_trans (le_mul_self (n * n * n) n hn) (Nat.l  -- (+297 chars; full line in the Zenodo source)
    calc _ ≤ (n * n * n * n) + (n * n * n * n) + (n * n * n * n) + (n * n * n * n) + (n * n * n * n) + (n * n * n * n) := h1
      _ = 6 * (n * n * n * n) := by lib_nat
      _ ≤ n * (n * n * n * n) := Nat.mul_le_mul_right _ hM
      _ = n * n * n * n * n := by lib_nat
      _ ≤ _ := by rw [show n * n * n * n * n + n * n + n * n + n * n + n + n + n = n * n * n * n * n + (n * n + n * n + n * n + n + n + n) by lib_nat]; exact Nat.le_add_right _ _
  obtain ⟨N0, hN⟩ := le_dest _ _ hle
  generalize f + 1 = k at hf hk
  have hA : n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n = (n * n * n * k + n * n * k + n * n * k + n * n * k + n * n * k) * 11 := by
    calc _ = n * n * n * (k * 11) + n * n * (k * 11) + n * n * (k * 11) + n * n * (k * 11) + n * n * (k * 11) := by rw [← hk]
      _ = _ := by lib_nat
  have hB : n * n * n * n * n + n * n + n * n + n * n + n + n + n = (n * n * n * n * k + n * k + n * k + n * k + k + k + k) * 11 := by
    calc _ = n * n * n * n * (k * 11) + n * (k * 11) + n * (k * 11) + n * (k * 11) + (k * 11) + (k * 11) + (k * 11) := by rw [← hk]
      _ = _ := by lib_nat
  obtain ⟨c, hc⟩ := rem_minus N0 (n * n * n * k + n * n * k + n * n * k + n * n * k + n * n * k) (n * n * n * n * k + n * k + n * k + n * k + k + k + k) (by rw [← hA, ← hB]; exact hN)
  obtain ⟨q, hq, ⟨b, hb⟩, hqN⟩ := has n N0 hN ⟨c, hc⟩ N0 N0 (Nat.le_refl _) (dv_refl _) ⟨c, hc⟩
  refine ⟨b, by rw [← hb]; exact hq, ?_⟩
  rw [← hb]
  cases Nat.lt_or_ge B q with
  | inl h => exact h
  | inr h =>
    exfalso
    obtain ⟨r, hr⟩ := dv_fac B q (by rw [hb]; exact Nat.le_add_left 1 _) h
    obtain ⟨s, hs⟩ : ∃ s, n = s * q := ⟨r * 11, by rw [hk, ← hf, hr]; lib_nat⟩
    have hQ : n * n * n * n * n + n * n + n * n + n * n + n + n + n = (n * n * n * n * s + n * s + n * s + n * s + s + s + s) * q := by
      calc _ = n * n * n * n * (s * q) + n * (s * q) + n * (s * q) + n * (s * q) + (s * q) + (s * q) + (s * q) := by rw [← hs]
        _ = _ := by lib_nat
    have hP : n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n = (n * n * n * s + n * n * s + n * n * s + n * n * s + n * n * s) * q := by
      calc _ = n * n * n * (s * q) + n * n * (s * q) + n * n * (s * q) + n * n * (s * q) + n * n * (s * q) := by rw [← hs]
        _ = _ := by lib_nat
    have hsum : Dv q (N0 + (n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n + 1)) := ⟨_, by rw [hN, hQ]⟩
    have h2 : Dv q (n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n) := ⟨_, hP⟩
    have hq1 : q = (b * 11 + 9) + 1 := hb
    rw [hq1] at hqN hsum h2
    exact dv_one_absurd (b * 11 + 8) (dv_sub (b * 11 + 9) _ 1 h2 (dv_sub (b * 11 + 9) _ _ hqN hsum))

end Ell11

#print axioms Ell11.add_cancel
#print axioms Ell11.le_dest
#print axioms Ell11.le_mul_self
#print axioms Ell11.modL
#print axioms Ell11.cofactor
#print axioms Ell11.rem_minus
#print axioms Ell11.l_zero
#print axioms Ell11.allowed
#print axioms Ell11.has
#print axioms Ell11.inf

lang/Ell13.lean

lean_all/lang/Ell13.lean

/-
  lang/Ell13 — T43: Dirichlet for p ≡ −1 (mod 13), part 1 (ring-theoretic tools), generated.
    phi        z·w = 1, f = minimal polynomial of 2cos(2π/13)  ⟹  z^6 f(z + w) = Φ_13(z)
    ellth      Φ_13(z) = 0  ⟹  z^13 = 1
    at_one     Φ_13(z) = 0, z = 1  ⟹  13 = 0
    one_of     z^13 = 1, z^r = 1, r·a = 13b + 1  ⟹  z = 1
    key        Φ_13(z) = 0, z^N = 1  ⟹  13 | N or 13 = 0
  f(X) = 1·X^6 + 1·X^5 + -5·X^4 + -4·X^3 + 6·X^2 + 3·X^1 + -1·X^0.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Order
import lang.TwoPower
import lib.RingTac

namespace Ell13

open FTAChain

variable {R : Type} (K : DetChain.CRing R)

theorem phi (u v : R) (huv : K.mul u v = K.one) :
    K.mul (K.mul (K.mul (K.mul (K.mul (K.mul (u) (u)) (u)) (u)) (u)) (u)) (K.add (K.add (K.add (K.add (K.add (K.add (K.mul (K.mul (K.mul (K.mul (K.mul (K.add (u) (v)) (K.add (u) (v))) (K.add (u) (v))) (K.add (u) (v))) (K.add (u) (v))) (K.add (u) (v))) (K.mul (K.mul (K.mul (K.mul (K.add (u) (v)) (K.add (u) (v))) (K.add (u) (v))) (K.add (u) (v))) (K.add (u) (v)))) (K.neg (K.mul (K.add (K.add (K.add   -- (+1447 chars; full line in the Zenodo source)
  calc _ = K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.one) (u)) (K.mul (u) (u))) (K.mul (K.mul (u) (u)) (u))) (K.mul (K.mul (K.mul (u) (u)) (u)) (u))) (K.mul (K.mul (K.mul (K.mul (u) (u)) (u)) (u)) (u))) (K.mul (K.mul (K.mul (K.mul (K.mul (u) (u)) (u)) (u)) (u)) (u))) (K.mul (K.mul (K.mul (K.mul (K.mul (K.mul (u) (u)) (u)) (u)) (u)) (u)) (u))) (K.m  -- (+4261 chars; full line in the Zenodo source)
    _ = _ := by rw [huv]; lib_ring

theorem ellth (u : R) (h : K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.one) (u)) (K.mul (u) (u))) (K.mul (K.mul (u) (u)) (u))) (K.mul (K.mul (K.mul (u) (u)) (u)) (u))) (K.mul (K.mul (K.mul (K.mul (u) (u)) (u)) (u)) (u))) (K.mul (K.mul (K.mul (K.mul (K.mul (u) (u)) (u)) (u)) (u)) (u))) (K.mul (K.mul (K.mul (K.mul (K.mul (K.mul (u) (u)) (u)) (u)) (u)) (u)) (  -- (+614 chars; full line in the Zenodo source)
  show K.mul (K.mul (K.mul (K.mul (K.mul (K.mul (K.mul (K.mul (K.mul (K.mul (K.mul (K.mul (K.mul (K.one) u) u) u) u) u) u) u) u) u) u) u) u) u = K.one
  calc _ = K.add (K.mul (K.add u (K.neg K.one)) (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.one) (u)) (K.mul (u) (u))) (K.mul (K.mul (u) (u)) (u))) (K.mul (K.mul (K.mul (u) (u)) (u)) (u))) (K.mul (K.mul (K.mul (K.mul (u) (u)) (u)) (u)) (u))) (K.mul (K.mul (K.mul (K.mul (K.mul (u) (u)) (u)) (u)) (u)) (u))) (K.mul (K.mul (K.mul (K.mul (K.mul (K.mul (u) (u))  -- (+613 chars; full line in the Zenodo source)
    _ = K.one := by rw [h]; lib_ring

theorem at_one (u : R) (h : K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.one) (u)) (K.mul (u) (u))) (K.mul (K.mul (u) (u)) (u))) (K.mul (K.mul (K.mul (u) (u)) (u)) (u))) (K.mul (K.mul (K.mul (K.mul (u) (u)) (u)) (u)) (u))) (K.mul (K.mul (K.mul (K.mul (K.mul (u) (u)) (u)) (u)) (u)) (u))) (K.mul (K.mul (K.mul (K.mul (K.mul (K.mul (u) (u)) (u)) (u)) (u)) (u))   -- (+802 chars; full line in the Zenodo source)
  rw [hu] at h
  calc _ = K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.one) (K.one)) (K.mul (K.one) (K.one))) (K.mul (K.mul (K.one) (K.one)) (K.one))) (K.mul (K.mul (K.mul (K.one) (K.one)) (K.one)) (K.one))) (K.mul (K.mul (K.mul (K.mul (K.one) (K.one)) (K.one)) (K.one)) (K.one))) (K.mul (K.mul (K.mul (K.mul (K.mul (K.one) (K.one)) (K.one)) (K.one)) (K.one)) (K.one))) (K.m  -- (+879 chars; full line in the Zenodo source)
    _ = K.zero := h

theorem one_of (u : R) (hL : Frobenius.pw K u 13 = K.one) (r a b : Nat) (hr : Frobenius.pw K u r = K.one) (hab : r * a = 13 * b + 1) :
    u = K.one := by
  calc u = K.mul (Frobenius.pw K (Frobenius.pw K u 13) b) u := by rw [hL, TwoPower.pwone]; lib_ring
    _ = Frobenius.pw K u (13 * b + 1) := by rw [TwoPower.pwpow]; rfl
    _ = Frobenius.pw K (Frobenius.pw K u r) a := by rw [TwoPower.pwpow, hab]
    _ = K.one := by rw [hr, TwoPower.pwone]

theorem key (u : R) (h : K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.one) (u)) (K.mul (u) (u))) (K.mul (K.mul (u) (u)) (u))) (K.mul (K.mul (K.mul (u) (u)) (u)) (u))) (K.mul (K.mul (K.mul (K.mul (u) (u)) (u)) (u)) (u))) (K.mul (K.mul (K.mul (K.mul (K.mul (u) (u)) (u)) (u)) (u)) (u))) (K.mul (K.mul (K.mul (K.mul (K.mul (K.mul (u) (u)) (u)) (u)) (u)) (u)) (u)  -- (+622 chars; full line in the Zenodo source)
    Dv 13 N ∨ K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.zero) K.one) K.one) K.one) K.one) K.one) K.one) K.one) K.one) K.one) K.one) K.one) K.one) K.one = K.zero := by
  have hL := ellth K u h
  obtain ⟨q, r, hqr, hr⟩ := Order.divmod 12 N
  have hxr : Frobenius.pw K u r = K.one := by
    rw [← Order.pw_qe K u 13 r hL q]; rw [← hqr]; exact hN
  match r, hr, hxr, hqr with
  | 0, _, _, hqr => exact Or.inl ⟨q, by rw [hqr]; rfl⟩
  | 1, _, hxr, _ => exact Or.inr (at_one K u h (one_of K u hL 1 1 0 hxr rfl))
  | 2, _, hxr, _ => exact Or.inr (at_one K u h (one_of K u hL 2 7 1 hxr rfl))
  | 3, _, hxr, _ => exact Or.inr (at_one K u h (one_of K u hL 3 9 2 hxr rfl))
  | 4, _, hxr, _ => exact Or.inr (at_one K u h (one_of K u hL 4 10 3 hxr rfl))
  | 5, _, hxr, _ => exact Or.inr (at_one K u h (one_of K u hL 5 8 3 hxr rfl))
  | 6, _, hxr, _ => exact Or.inr (at_one K u h (one_of K u hL 6 11 5 hxr rfl))
  | 7, _, hxr, _ => exact Or.inr (at_one K u h (one_of K u hL 7 2 1 hxr rfl))
  | 8, _, hxr, _ => exact Or.inr (at_one K u h (one_of K u hL 8 5 3 hxr rfl))
  | 9, _, hxr, _ => exact Or.inr (at_one K u h (one_of K u hL 9 3 2 hxr rfl))
  | 10, _, hxr, _ => exact Or.inr (at_one K u h (one_of K u hL 10 4 3 hxr rfl))
  | 11, _, hxr, _ => exact Or.inr (at_one K u h (one_of K u hL 11 6 5 hxr rfl))
  | 12, _, hxr, _ => exact Or.inr (at_one K u h (one_of K u hL 12 12 11 hxr rfl))
  | r + 13, hr, _, _ => exact absurd hr (fun h => Nat.lt_irrefl 13 (Nat.lt_of_le_of_lt (Nat.le_add_left 13 r) h))

end Ell13

#print axioms Ell13.phi
#print axioms Ell13.ellth
#print axioms Ell13.at_one
#print axioms Ell13.one_of
#print axioms Ell13.key

lang/Ell13b.lean

lean_all/lang/Ell13b.lean

/-
  lang/Ell13b — T43: Dirichlet for p ≡ −1 (mod 13), part 2: a root of f mod q (q = 2m + 3) forces 13 | q − 1, 13 | q + 1
  or 13 = 0 in 𝔽_q (cases D = r² − 4 zero / nonzero square / non-square, as in Seven7b). Generated.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Ell13
import lang.EulerHard
import lang.Euler
import lang.G4Map
import lang.Fq2

namespace Ell13

open FTAChain ZMod YMChain ConicSum G4Gen G4Map

variable (m : Nat)

def fr (x : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (x) (x)) (x)) (x)) (x)) (x)) ((ring (  -- (+1506 chars; full line in the Zenodo source)
def lnum : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ri  -- (+388 chars; full line in the Zenodo source)

theorem root_D0 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x : Zp (2 * m + 1 + 1)) (hf : fr m x = (ring (2 * m + 1 + 1)).zero) (hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ri  -- (+132 chars; full line in the Zenodo source)
  unfold fr at hf
  have h := mul_eq_zero m hpr ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) (by rw [← hD]; lib_ring)
  rcases h with e | e
  · have hx : x = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one) := by
      calc x = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) := by lib_ring
        _ = _ := by rw [e]; lib_ring
    rw [hx] at hf
    unfold lnum
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1))  -- (+3221 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := hf
  · have hx : x = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) := by
      calc x = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) := by lib_ring
        _ = _ := by rw [e]; lib_ring
    rw [hx] at hf
    exact absurd (show (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).zero by
      calc (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).a  -- (+3857 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := hf) (one_ne m)

theorem root_sq (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h2 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one) ≠ (ring (2 * m + 1 + 1)).zero) (x : Zp (2 * m + 1 + 1)) (hf : fr m x = (ring (2 * m + 1 + 1)).zero)
    (s : Zp (2 * m + 1 + 1)) (hs : (ring (2 * m + 1 + 1)).mul s s = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))))) : Dv 13 (2 * m + 1 + 1) ∨ lnum m =   -- (+33 chars; full line in the Zenodo source)
  have ei : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ h2
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) = i at ei
  obtain ⟨u, hu⟩ : ∃ u : Zp (2 * m + 1 + 1), u = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x s) i := ⟨_, rfl⟩
  obtain ⟨v, hv⟩ : ∃ v : Zp (2 * m + 1 + 1), v = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg s)) i := ⟨_, rfl⟩
  have huv : (ring (2 * m + 1 + 1)).mul u v = (ring (2 * m + 1 + 1)).one := by
    rw [hu, hv]
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul i i) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul s s)))) (ring (2 * m + 1 + 1)).zero := by lib_ring
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) := by rw [hs]; lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [ei]; lib_ring
  have hsum : (ring (2 * m + 1 + 1)).add u v = x := by
    rw [hu, hv]
    calc _ = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) := by lib_ring
      _ = x := by rw [ei]; lib_ring
  have hp := phi (ring (2 * m + 1 + 1)) u v huv
  rw [hsum] at hp
  unfold fr at hf
  rw [hf] at hp
  have hu0 : u ≠ (ring (2 * m + 1 + 1)).zero := fun e => one_ne m (by rw [← huv, e]; lib_ring)
  have hferm : Frobenius.pw (ring (2 * m + 1 + 1)) u (2 * m + 1 + 1) = (ring (2 * m + 1 + 1)).one := by
    have hF := ZMod.fermat (2 * m + 1) hpr u
    have hz : (ring (2 * m + 1 + 1)).mul u ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) u (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) u (2 * m + 1 + 1)) u) ((ring (2 * m + 1 + 1)).neg u) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [show (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) u (2 * m + 1 + 1)) u = u from hF]; lib_ring
    rcases mul_eq_zero m hpr _ _ hz with e | e
    · exact absurd e hu0
    · calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) u (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
        _ = (ring (2 * m + 1 + 1)).one := by rw [e]; lib_ring
  exact key (ring (2 * m + 1 + 1)) u (by rw [← hp]; lib_ring) _ hferm

theorem e_add (a b : Zp (2 * m + 1 + 1)) (n : Zp (2 * m + 1 + 1)) : (Fq2.G2 (2 * m + 1) n).add (a, (ring (2 * m + 1 + 1)).zero) (b, (ring (2 * m + 1 + 1)).zero) = ((ring (2 * m + 1 + 1)).add a b, (ring (2 * m + 1 + 1)).zero) :=
  Prod.ext rfl (by show (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero = (ring (2 * m + 1 + 1)).zero; lib_ring)
theorem e_neg (a : Zp (2 * m + 1 + 1)) (n : Zp (2 * m + 1 + 1)) : (Fq2.G2 (2 * m + 1) n).neg (a, (ring (2 * m + 1 + 1)).zero) = ((ring (2 * m + 1 + 1)).neg a, (ring (2 * m + 1 + 1)).zero) :=
  Prod.ext rfl (by show (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).zero = (ring (2 * m + 1 + 1)).zero; lib_ring)

theorem root_nsq (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h2 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one) ≠ (ring (2 * m + 1 + 1)).zero) (x : Zp (2 * m + 1 + 1)) (hf : fr m x = (ring (2 * m + 1 + 1)).zero)
    (hn : Frobenius.pw (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))))) (m + 1) = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 +   -- (+85 chars; full line in the Zenodo source)
  have ei : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ h2
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) = i at ei
  have hzw : (Fq2.G2 (2 * m + 1) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)))))).mul ((ring (2 * m + 1 + 1)).mul x i, i) ((ring (2 * m + 1 + 1)).mul   -- (+365 chars; full line in the Zenodo source)
    apply Prod.ext
    · show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x i) ((ring (2 * m + 1 + 1)).mul x i)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((r  -- (+150 chars; full line in the Zenodo source)
      calc _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).one := by rw [ei]; lib_ring
    · show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x i) ((ring (2 * m + 1 + 1)).neg i)) ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul x i)) = (ring (2 * m + 1 + 1)).zero
      lib_ring
  have hsum : (Fq2.G2 (2 * m + 1) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)))))).add ((ring (2 * m + 1 + 1)).mul x i, i) ((ring (2 * m + 1 + 1)).mul  -- (+76 chars; full line in the Zenodo source)
    apply Prod.ext
    · show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x i) ((ring (2 * m + 1 + 1)).mul x i) = x
      calc _ = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) := by lib_ring
        _ = x := by rw [ei]; lib_ring
    · show (ring (2 * m + 1 + 1)).add i ((ring (2 * m + 1 + 1)).neg i) = (ring (2 * m + 1 + 1)).zero
      lib_ring
  have hp := phi (Fq2.G2 (2 * m + 1) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)))))) _ _ hzw
  rw [hsum] at hp
  have hfe : (Fq2.G2 (2 * m + 1) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)))))).add ((Fq2.G2 (2 * m + 1) ((ring (2 * m + 1 + 1)).add ((ring (2 * m +  -- (+20502 chars; full line in the Zenodo source)
    rw [show (Fq2.G2 (2 * m + 1) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)))))).one = ((ring (2 * m + 1 + 1)).one, (ring (2 * m + 1 + 1)).zero) from  -- (+5 chars; full line in the Zenodo source)
    repeat (first | rw [Fq2.embed_mul] | rw [e_add] | rw [e_neg])
    apply Prod.ext
    · show _ = (ring (2 * m + 1 + 1)).zero
      rw [← hf]; unfold fr; rfl
    · rfl
  rw [hfe, DetChain.mul_zero] at hp
  have hq := Fq2.pw_p1 (2 * m + 1) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))))) (m + 1) hpr rfl hn ((ring (2 * m + 1 + 1)).mul x i, i)
  have hN : Frobenius.pw (Fq2.G2 (2 * m + 1) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)))))) ((ring (2 * m + 1 + 1)).mul x i, i) (2 * m + 1 + 1 + 1 +  -- (+334 chars; full line in the Zenodo source)
    rw [hq]
    apply Prod.ext
    · show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x i) ((ring (2 * m + 1 + 1)).mul x i)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((r  -- (+150 chars; full line in the Zenodo source)
      calc _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).one := by rw [ei]; lib_ring
    · rfl
  rcases key (Fq2.G2 (2 * m + 1) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)))))) _ hp.symm _ hN with h | h
  · exact Or.inl h
  · have e : (Fq2.G2 (2 * m + 1) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)))))).add ((Fq2.G2 (2 * m + 1) ((ring (2 * m + 1 + 1)).add ((ring (2 * m +  -- (+9155 chars; full line in the Zenodo source)
      rw [show (Fq2.G2 (2 * m + 1) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)))))).one = ((ring (2 * m + 1 + 1)).one, (ring (2 * m + 1 + 1)).zero) fr  -- (+407 chars; full line in the Zenodo source)
      repeat rw [e_add]
    rw [e, show (Fq2.G2 (2 * m + 1) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)))))).zero = ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).zero)  -- (+15 chars; full line in the Zenodo source)
    have h' := congrArg Prod.fst h
    exact Or.inr h'

theorem root (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h2 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one) ≠ (ring (2 * m + 1 + 1)).zero) (x : Zp (2 * m + 1 + 1)) (hf : fr m x = (ring (2 * m + 1 + 1)).zero) :
    Dv 13 (2 * m + 1 + 1) ∨ Dv 13 (2 * m + 1 + 1 + 1 + 1) ∨ lnum m = (ring (2 * m + 1 + 1)).zero := by
  by_cases hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)))) = (ring (2 * m + 1 + 1)).zero
  · exact Or.inr (Or.inr (root_D0 m hpr x hf hD))
  · rcases Euler.euler_pm m hpr _ hD with h | h
    · obtain ⟨s, hs⟩ := EulerHard.euler_hard m hpr _ h
      rcases root_sq m hpr h2 x hf s hs with h' | h'
      · exact Or.inl h'
      · exact Or.inr (Or.inr h')
    · rcases root_nsq m hpr h2 x hf h with h' | h'
      · exact Or.inr (Or.inl h')
      · exact Or.inr (Or.inr h')

end Ell13

#print axioms Ell13.root_D0
#print axioms Ell13.root_sq
#print axioms Ell13.e_add
#print axioms Ell13.e_neg
#print axioms Ell13.root_nsq
#print axioms Ell13.root

lang/Ell13c.lean

lean_all/lang/Ell13c.lean

/-
  lang/Ell13c — T43: **infinitely many primes p ≡ 12 (mod 13)** (Euclid-type, f = minimal polynomial of 2cos(2π/13)).
  g(n) = f(n) = Q(n) − P(n) with Q = 1n^6 + 1n^5 + 6n^2 + 3n^1,
  P = 5n^4 + 4n^3 + 1. For n = 13·B!, N₀ = g(n) ≡ −1 (mod 13); every prime factor q is odd, f has a
  root mod q, so (Ell13b.root) q ≡ ±1 (mod 13) or q = 13, and q = 13 is impossible; some factor is ≡ 12 and it does not divide n.
    mod13, cofactor, rem_minus, l_zero, allowed, has, inf    (generated)
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Ell13b
import lang.Fp2Sqrt
import lang.PrimeOneModQ
import lang.TwoPower
import lang.Nine9c

namespace Ell13

open FTAChain ZMod InfSplit

theorem add_cancel (a b : Nat) : ∀ c, a + c = b + c → a = b
  | 0, h => h
  | c + 1, h => add_cancel a b c (Nat.succ.inj h)

theorem le_dest (a b : Nat) (h : a ≤ b) : ∃ c, c + a = b := by
  induction h with
  | refl => exact ⟨0, Nat.zero_add a⟩
  | step _ ih =>
    obtain ⟨c, hc⟩ := ih
    exact ⟨c + 1, by rw [← hc]; lib_nat⟩

theorem le_mul_self (a n : Nat) (hn : 1 ≤ n) : a ≤ a * n :=
  match n, hn with
  | n' + 1, _ => Nat.le_trans (Nat.le_add_left a (a * n')) (Nat.le_of_eq (by lib_nat))

theorem modL (n : Nat) : ∃ a r, n = a * 13 + r ∧ r < 13 :=
  match div_exists 12 n with
  | ⟨a, r, k, hn, hr⟩ => ⟨a, r, hn, Nat.lt_of_lt_of_le (Nat.lt_succ_self r) (by rw [show (13 : Nat) = 12 + 1 from rfl, ← hr]; exact Nat.succ_le_succ (Nat.le_add_right r k))⟩

theorem cofactor (b N' a : Nat) (h : (b * 13 + 1) * N' = a * 13 + 12) : ∃ c, N' = c * 13 + 12 :=
  match modL N' with
  | ⟨c, r, hc, hr⟩ => by
    have e : (b * 13 + 1) * N' = (b * 13 * c + b * r + c) * 13 + r := by rw [hc]; lib_nat
    rw [e] at h
    exact ⟨c, by rw [hc, Nine.uniq_rem 13 _ _ r 12 hr (Nat.lt_succ_self 12) h]⟩

theorem rem_minus (N A B : Nat) (h : N + (A * 13 + 1) = B * 13) : ∃ c, N = c * 13 + 12 := by
  obtain ⟨c, r, hc, hr⟩ := modL N
  cases Nat.lt_or_ge (r + 1) 13 with
  | inl hlt =>
    have e : (c + A) * 13 + (r + 1) = B * 13 + 0 := by rw [Nat.add_zero, ← h, hc]; lib_nat
    exact absurd (Nine.uniq_rem 13 _ _ _ _ hlt (Nat.zero_lt_succ 12) e) (fun h' => Nat.noConfusion h')
  | inr hge =>
    have e : r = 12 := Nat.le_antisymm (Nat.le_of_lt_succ hr) (Nat.le_of_succ_le_succ hge)
    exact ⟨c, by rw [hc, e]⟩

theorem l_zero (m : Nat) (h : lnum m = (ring (2 * m + 1 + 1)).zero) : 2 * m + 1 + 1 + 1 = 13 := by
  have e : lnum m = mk (2 * m + 1 + 1) 13 := by
    unfold lnum
    rw [show (ring (2 * m + 1 + 1)).zero = mk (2 * m + 1 + 1) 0 from rfl, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one]
  rw [e, show (ring (2 * m + 1 + 1)).zero = mk (2 * m + 1 + 1) 0 from rfl] at h
  unfold mk at h
  have h0 : md (2 * m + 1 + 1) 13 = md (2 * m + 1 + 1) 0 := Subtype.mk.inj h
  match m, h0 with
  | 0, h0 => exact absurd h0 (fun h' => Nat.noConfusion h')
  | 1, h0 => exact absurd h0 (fun h' => Nat.noConfusion h')
  | 2, h0 => exact absurd h0 (fun h' => Nat.noConfusion h')
  | 3, h0 => exact absurd h0 (fun h' => Nat.noConfusion h')
  | 4, h0 => exact absurd h0 (fun h' => Nat.noConfusion h')
  | 5, _ => rfl
  | k + 6, h0 =>
    rw [md_small _ 13 (Nat.lt_of_lt_of_le (Nat.lt_succ_self 13) (Nat.le_trans (Nat.le_add_left 14 (2 * k + 1)) (Nat.le_of_eq (by lib_nat))))] at h0
    exact Nat.noConfusion h0

theorem allowed (n N l : Nat) (hq : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1)
    (hN : N + (n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n + 1) = n * n * n * n * n * n + n * n * n * n * n + n * n + n * n + n * n + n * n + n * n + n * n + n + n + n) (hdv : Dv (l + 1 + 1) N) :
    (∃ a, l + 1 + 1 = a * 13 + 1) ∨ (∃ a, l + 1 + 1 = a * 13 + 12) ∨ l + 1 + 1 = 13 := by
  rcases EveryPrime.nat_even_odd l with ⟨j, hj⟩ | ⟨m, hm⟩
  · exfalso
    have h2 : l + 1 + 1 = 2 := by
      cases hq 2 ⟨j + 1, by rw [hj]; lib_nat⟩ with
      | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
      | inr e => exact e.symm
    rw [h2] at hdv
    obtain ⟨r, hr⟩ := hdv
    rw [hr] at hN
    rcases EveryPrime.nat_even_odd n with ⟨i, hi⟩ | ⟨i, hi⟩
    · have hQe : n * n * n * n * n * n + n * n * n * n * n + n * n + n * n + n * n + n * n + n * n + n * n + n + n + n = (n * n * n * n * n * i + n * n * n * n * i + n * i + n * i + n * i + n * i + n * i + n * i + i + i + i) * 2 := by
        calc _ = n * n * n * n * n * (2 * i) + n * n * n * n * (2 * i) + n * (2 * i) + n * (2 * i) + n * (2 * i) + n * (2 * i) + n * (2 * i) + n * (2 * i) + (2 * i) + (2 * i) + (2 * i) := by rw [← hi]
          _ = _ := by lib_nat
      have hPe : n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n = (n * n * n * i + n * n * n * i + n * n * n * i + n * n * n * i + n * n * n * i + n * n * i + n * n * i + n * n * i + n * n * i) * 2 := by
        calc _ = n * n * n * (2 * i) + n * n * n * (2 * i) + n * n * n * (2 * i) + n * n * n * (2 * i) + n * n * n * (2 * i) + n * n * (2 * i) + n * n * (2 * i) + n * n * (2 * i) + n * n * (2 * i) := by rw [← hi]
          _ = _ := by lib_nat
      exact odd_not_dv2 (r + (n * n * n * i + n * n * n * i + n * n * n * i + n * n * n * i + n * n * n * i + n * n * i + n * n * i + n * n * i + n * n * i)) (n * n * n * n * n * i + n * n * n * n * i + n * i + n * i + n * i + n * i + n * i + n * i + i + i + i) (by
        calc _ = r * 2 + (n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n + 1) := by rw [hPe]; lib_nat
          _ = _ := hN
          _ = _ := hQe)
    · have hQo : n * n * n * n * n * n + n * n * n * n * n + n * n + n * n + n * n + n * n + n * n + n * n + n + n + n = 11 + (i * ((1 + n + n * n + n * n * n + n * n * n * n + n * n * n * n * n) + (1 + n + n * n + n * n * n + n * n * n * n) + (1 + n) + (1 + n) + (1 + n) + (1 + n) + (1 + n) + (1 + n) + (1) + (1) + (1))) * 2 := add_cancel _ _ ((1 + n + n * n + n * n * n + n * n * n * n + n * n * n   -- (+137 chars; full line in the Zenodo source)
        calc n * n * n * n * n * n + n * n * n * n * n + n * n + n * n + n * n + n * n + n * n + n * n + n + n + n + ((1 + n + n * n + n * n * n + n * n * n * n + n * n * n * n * n) + (1 + n + n * n + n * n * n + n * n * n * n) + (1 + n) + (1 + n) + (1 + n) + (1 + n) + (1 + n) + (1 + n) + (1) + (1) + (1)) = 11 + n * ((1 + n + n * n + n * n * n + n * n * n * n + n * n * n * n * n) + (1 + n + n * n   -- (+121 chars; full line in the Zenodo source)
          _ = 11 + (2 * i + 1) * ((1 + n + n * n + n * n * n + n * n * n * n + n * n * n * n * n) + (1 + n + n * n + n * n * n + n * n * n * n) + (1 + n) + (1 + n) + (1 + n) + (1 + n) + (1 + n) + (1 + n) + (1) + (1) + (1)) := by rw [hi]
          _ = _ := by lib_nat)
      have hPo : n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n = 9 + (i * ((1 + n + n * n + n * n * n) + (1 + n + n * n + n * n * n) + (1 + n + n * n + n * n * n) + (1 + n + n * n + n * n * n) + (1 + n + n * n + n * n * n) + (1 + n + n * n) + (1 + n + n * n) + (1 + n + n * n) + (1 + n + n * n))) * 2 := add_cancel _ _ (  -- (+224 chars; full line in the Zenodo source)
        calc n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n + ((1 + n + n * n + n * n * n) + (1 + n + n * n + n * n * n) + (1 + n + n * n + n * n * n) + (1 + n + n * n + n * n * n) + (1 + n + n * n + n * n * n) + (1 + n + n * n) + (1 + n + n * n) + (1 + n + n * n) + (1 + n + n * n)) = 9 + n * ((1 + n + n * n + n * n * n  -- (+208 chars; full line in the Zenodo source)
          _ = 9 + (2 * i + 1) * ((1 + n + n * n + n * n * n) + (1 + n + n * n + n * n * n) + (1 + n + n * n + n * n * n) + (1 + n + n * n + n * n * n) + (1 + n + n * n + n * n * n) + (1 + n + n * n) + (1 + n + n * n) + (1 + n + n * n) + (1 + n + n * n)) := by rw [hi]
          _ = _ := by lib_nat)
      exact odd_not_dv2 (5 + i * ((1 + n + n * n + n * n * n + n * n * n * n + n * n * n * n * n) + (1 + n + n * n + n * n * n + n * n * n * n) + (1 + n) + (1 + n) + (1 + n) + (1 + n) + (1 + n) + (1 + n) + (1) + (1) + (1))) (r + i * ((1 + n + n * n + n * n * n) + (1 + n + n * n + n * n * n) + (1 + n + n * n + n * n * n) + (1 + n + n * n + n * n * n) + (1 + n + n * n + n * n * n) + (1 + n + n * n)   -- (+63 chars; full line in the Zenodo source)
        calc _ = 11 + (i * ((1 + n + n * n + n * n * n + n * n * n * n + n * n * n * n * n) + (1 + n + n * n + n * n * n + n * n * n * n) + (1 + n) + (1 + n) + (1 + n) + (1 + n) + (1 + n) + (1 + n) + (1) + (1) + (1))) * 2 := by lib_nat
          _ = n * n * n * n * n * n + n * n * n * n * n + n * n + n * n + n * n + n * n + n * n + n * n + n + n + n := hQo.symm
          _ = r * 2 + (n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n + 1) := hN.symm
          _ = _ := by rw [hPo]; lib_nat)
  · subst hm
    have hx : fr m (mk (2 * m + 1 + 1) n) = (ring (2 * m + 1 + 1)).zero := by
      have h1 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1  -- (+1089 chars; full line in the Zenodo source)
        repeat (first | rw [TwoPower.mk_mul] | rw [PrimeOneModQ.mk_add] | rw [TwoPower.mk_add_one])
      have h2 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((mk (2 * m + 1 + 1) n)) (mk (2 * m + 1 + 1) n))   -- (+1481 chars; full line in the Zenodo source)
        repeat (first | rw [TwoPower.mk_mul] | rw [PrimeOneModQ.mk_add] | rw [TwoPower.mk_add_one])
      have h0 : mk (2 * m + 1 + 1) N = (ring (2 * m + 1 + 1)).zero := Subtype.ext (by
        show md (2 * m + 1 + 1) N = md (2 * m + 1 + 1) 0
        rw [dv_md_zero _ N hdv]; rfl)
      have h3 : (ring (2 * m + 1 + 1)).add (mk (2 * m + 1 + 1) N) (mk (2 * m + 1 + 1) (n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n + 1)) = mk (2 * m + 1 + 1) (n * n * n * n * n * n + n * n * n * n * n + n * n + n * n + n * n + n * n + n * n + n * n + n + n + n) := by
        rw [PrimeOneModQ.mk_add, hN]
      rw [h0, ← h1, ← h2] at h3
      unfold fr
      generalize mk (2 * m + 1 + 1) n = x at h3
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)  -- (+1521 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).m  -- (+1812 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by lib_ring
    rcases root m hq (Fp2Sqrt.two_ne m) _ hx with ⟨a, ha⟩ | ⟨a, ha⟩ | hl
    · exact Or.inl ⟨a, by rw [← ha]⟩
    · match a, ha with
      | 0, ha => exact absurd ha (fun h' => Nat.noConfusion h')
      | a + 1, ha => exact Or.inr (Or.inl ⟨a, Nat.succ.inj (by show 2 * m + 1 + 1 + 1 + 1 = a * 13 + 12 + 1; rw [ha, Nat.succ_mul])⟩)
    · exact Or.inr (Or.inr (l_zero m hl))

theorem has (n N0 : Nat) (hN : N0 + (n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n + 1) = n * n * n * n * n * n + n * n * n * n * n + n * n + n * n + n * n + n * n + n * n + n * n + n + n + n) (hc0 : ∃ c, N0 = c * 13 + 12) :
    ∀ G N, N ≤ G → Dv N N0 → (∃ a, N = a * 13 + 12) →
    ∃ q, (∀ d, Dv d q → d = 1 ∨ d = q) ∧ (∃ b, q = b * 13 + 12) ∧ Dv q N
  | 0, N, hN', _, ⟨a, ha⟩ => absurd (ha ▸ hN') (fun h => Nat.not_succ_le_zero _ (Nat.le_trans (Nat.le_add_left _ _) h))
  | G + 1, N, hNG, hN0, ⟨a, ha⟩ => by
    match lp_exists (a * 13 + 10) with
    | ⟨l, hl, hmin⟩ =>
    have hq := lp_prime _ l hl hmin
    have hlN : Dv (l + 1 + 1) N := by rw [ha]; exact hl
    have hlN0 := dv_trans _ _ _ hlN hN0
    rcases allowed n N0 l hq hN hlN0 with ⟨b, hb⟩ | ⟨b, hb⟩ | hL
    · match hlN with
      | ⟨N', hN'⟩ =>
      match cofactor b N' a (by rw [← hb, ← ha, hN']; lib_nat) with
      | ⟨c, hc⟩ =>
      have hlt : N' ≤ G := by
        have e : N = N' + (N' * (l + 1)) := by rw [hN']; lib_nat
        have h1' : 1 ≤ N' * (l + 1) := by rw [hc]; exact Nat.mul_le_mul (Nat.le_add_left 1 _) (Nat.le_add_left 1 _)
        exact Nat.le_of_succ_le_succ (Nat.le_trans (Nat.add_le_add_left h1' N') (by rw [← e]; exact hNG))
      have hN'0 : Dv N' N0 := dv_trans _ _ _ ⟨l + 1 + 1, by rw [hN']; lib_nat⟩ hN0
      match has n N0 hN hc0 G N' hlt hN'0 ⟨c, hc⟩ with
      | ⟨q, hq', hqL, hqN⟩ => exact ⟨q, hq', hqL, dv_trans q N' N hqN ⟨l + 1 + 1, by rw [hN']; lib_nat⟩⟩
    · exact ⟨l + 1 + 1, hq, ⟨b, hb⟩, hlN⟩
    · exfalso
      obtain ⟨c, hc⟩ := hc0
      obtain ⟨r, hr⟩ := hlN0
      rw [hL] at hr
      exact absurd (Nine.uniq_rem 13 c r 12 0 (Nat.lt_succ_self 12) (Nat.zero_lt_succ 12)
        (by rw [← hc, hr]; rfl)) (fun h' => Nat.noConfusion h')

/-- **Infinitely many primes p ≡ 12 (mod 13).** -/
theorem inf (B : Nat) : ∃ b, (∀ d, Dv d (b * 13 + 12) → d = 1 ∨ d = b * 13 + 12) ∧ B < b * 13 + 12 := by
  obtain ⟨f, hf⟩ := fac_pos B
  obtain ⟨n, hk⟩ : ∃ n, n = (f + 1) * 13 := ⟨_, rfl⟩
  have hlen : 13 ≤ n := by rw [hk]; exact Nat.le_trans (Nat.le_add_left 13 (f * 13)) (Nat.le_of_eq (by lib_nat))
  have hM : 10 ≤ n := Nat.le_trans (Nat.le_add_left 10 3) hlen
  have hn : 1 ≤ n := Nat.le_trans (Nat.le_add_left 1 9) hM
  have hle : n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n + 1 ≤ n * n * n * n * n * n + n * n * n * n * n + n * n + n * n + n * n + n * n + n * n + n * n + n + n + n := by
    have h1 : n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n + 1 ≤ (n * n * n * n * n) + (n * n * n * n * n) + (n * n * n * n * n) + (n * n * n * n * n) + (n * n * n * n * n) + (n * n * n * n * n) + (n * n * n * n * n) + (n * n * n * n * n) + (n * n * n * n * n) + (n * n * n * n * n) := Nat.add_le_add (Nat.add_le_add (N  -- (+1105 chars; full line in the Zenodo source)
    calc _ ≤ (n * n * n * n * n) + (n * n * n * n * n) + (n * n * n * n * n) + (n * n * n * n * n) + (n * n * n * n * n) + (n * n * n * n * n) + (n * n * n * n * n) + (n * n * n * n * n) + (n * n * n * n * n) + (n * n * n * n * n) := h1
      _ = 10 * (n * n * n * n * n) := by lib_nat
      _ ≤ n * (n * n * n * n * n) := Nat.mul_le_mul_right _ hM
      _ = n * n * n * n * n * n := by lib_nat
      _ ≤ _ := by rw [show n * n * n * n * n * n + n * n * n * n * n + n * n + n * n + n * n + n * n + n * n + n * n + n + n + n = n * n * n * n * n * n + (n * n * n * n * n + n * n + n * n + n * n + n * n + n * n + n * n + n + n + n) by lib_nat]; exact Nat.le_add_right _ _
  obtain ⟨N0, hN⟩ := le_dest _ _ hle
  generalize f + 1 = k at hf hk
  have hA : n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n = (n * n * n * k + n * n * n * k + n * n * n * k + n * n * n * k + n * n * n * k + n * n * k + n * n * k + n * n * k + n * n * k) * 13 := by
    calc _ = n * n * n * (k * 13) + n * n * n * (k * 13) + n * n * n * (k * 13) + n * n * n * (k * 13) + n * n * n * (k * 13) + n * n * (k * 13) + n * n * (k * 13) + n * n * (k * 13) + n * n * (k * 13) := by rw [← hk]
      _ = _ := by lib_nat
  have hB : n * n * n * n * n * n + n * n * n * n * n + n * n + n * n + n * n + n * n + n * n + n * n + n + n + n = (n * n * n * n * n * k + n * n * n * n * k + n * k + n * k + n * k + n * k + n * k + n * k + k + k + k) * 13 := by
    calc _ = n * n * n * n * n * (k * 13) + n * n * n * n * (k * 13) + n * (k * 13) + n * (k * 13) + n * (k * 13) + n * (k * 13) + n * (k * 13) + n * (k * 13) + (k * 13) + (k * 13) + (k * 13) := by rw [← hk]
      _ = _ := by lib_nat
  obtain ⟨c, hc⟩ := rem_minus N0 (n * n * n * k + n * n * n * k + n * n * n * k + n * n * n * k + n * n * n * k + n * n * k + n * n * k + n * n * k + n * n * k) (n * n * n * n * n * k + n * n * n * n * k + n * k + n * k + n * k + n * k + n * k + n * k + k + k + k) (by rw [← hA, ← hB]; exact hN)
  obtain ⟨q, hq, ⟨b, hb⟩, hqN⟩ := has n N0 hN ⟨c, hc⟩ N0 N0 (Nat.le_refl _) (dv_refl _) ⟨c, hc⟩
  refine ⟨b, by rw [← hb]; exact hq, ?_⟩
  rw [← hb]
  cases Nat.lt_or_ge B q with
  | inl h => exact h
  | inr h =>
    exfalso
    obtain ⟨r, hr⟩ := dv_fac B q (by rw [hb]; exact Nat.le_add_left 1 _) h
    obtain ⟨s, hs⟩ : ∃ s, n = s * q := ⟨r * 13, by rw [hk, ← hf, hr]; lib_nat⟩
    have hQ : n * n * n * n * n * n + n * n * n * n * n + n * n + n * n + n * n + n * n + n * n + n * n + n + n + n = (n * n * n * n * n * s + n * n * n * n * s + n * s + n * s + n * s + n * s + n * s + n * s + s + s + s) * q := by
      calc _ = n * n * n * n * n * (s * q) + n * n * n * n * (s * q) + n * (s * q) + n * (s * q) + n * (s * q) + n * (s * q) + n * (s * q) + n * (s * q) + (s * q) + (s * q) + (s * q) := by rw [← hs]
        _ = _ := by lib_nat
    have hP : n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n = (n * n * n * s + n * n * n * s + n * n * n * s + n * n * n * s + n * n * n * s + n * n * s + n * n * s + n * n * s + n * n * s) * q := by
      calc _ = n * n * n * (s * q) + n * n * n * (s * q) + n * n * n * (s * q) + n * n * n * (s * q) + n * n * n * (s * q) + n * n * (s * q) + n * n * (s * q) + n * n * (s * q) + n * n * (s * q) := by rw [← hs]
        _ = _ := by lib_nat
    have hsum : Dv q (N0 + (n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n + 1)) := ⟨_, by rw [hN, hQ]⟩
    have h2 : Dv q (n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n) := ⟨_, hP⟩
    have hq1 : q = (b * 13 + 11) + 1 := hb
    rw [hq1] at hqN hsum h2
    exact dv_one_absurd (b * 13 + 10) (dv_sub (b * 13 + 11) _ 1 h2 (dv_sub (b * 13 + 11) _ _ hqN hsum))

end Ell13

#print axioms Ell13.add_cancel
#print axioms Ell13.le_dest
#print axioms Ell13.le_mul_self
#print axioms Ell13.modL
#print axioms Ell13.cofactor
#print axioms Ell13.rem_minus
#print axioms Ell13.l_zero
#print axioms Ell13.allowed
#print axioms Ell13.has
#print axioms Ell13.inf

lang/Eta27wC.lean

lean_all/lang/Eta27wC.lean

/-
  lang/Eta27wC — T47, cycle 826: **the coefficient of q^(n+1) in η(6z)η(18z) in R[q]/(q^M) as a double sum**
  (the Eta36C step for 27a's weight-one factor):
    [q^(n+1)] = Σ_{m ≤ 4M} (−1)^m Σ_{m' ≤ 4M} (−1)^m' [6E(m) + 18E(m') = n],   E(m) = (2Ta + Tb)(2M, m),
  written as es(fun m => shn (6E m) (fun i => es(fun m' => shn (18E m') (nth 1) i))) — QBinom.wt27 and Trunc.nth_es_mul twice.
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.Eta36C
import lang.Wt27a

namespace Trunc

open GLnLocal

variable {R : Type} (K : DetChain.CRing R) (M : Nat)

/-- **[q^(n+1)] η(6z)η(18z) as a double pentagonal sum.** -/
theorem eta27w_coef (n : Nat) (hn : n + 1 < M) :
    nth K M ((TR K M).mul (qe K M) ((TR K M).mul (QBinom.pp (TR K M) (Frobenius.pw (TR K M) (qe K M) 6) M)
      (QBinom.pp (TR K M) (Frobenius.pw (TR K M) (qe K M) 18) M))) (n + 1) =
    QBinom.es K (fun m => shn K (6 * (2 * QBinom.Ta (2 * M) m + QBinom.Tb (2 * M) m))
      (fun i => QBinom.es K (fun m' => shn K (18 * (2 * QBinom.Ta (2 * M) m' + QBinom.Tb (2 * M) m')) (nth K M (TR K M).one) i)
        (K.neg K.one) (2 * (2 * M) + 1)) n) (K.neg K.one) (2 * (2 * M) + 1) := by
  rw [QBinom.wt27 (TR K M) (qe K M) M (qe_nil K M), nth_qmul K M _ _ hn]
  show nth K M _ n = _
  rw [nth_es_mul K M 6 _ _ n (Nat.lt_of_succ_lt hn)]
  apply QBinom.es_congr
  intro m
  apply shn_congr K _ _ _ n
  intro i hi
  have hiM : i < M := Nat.lt_of_le_of_lt hi (Nat.lt_of_succ_lt hn)
  rw [← nth_es_mul K M 18 (fun m' => 2 * QBinom.Ta (2 * M) m' + QBinom.Tb (2 * M) m') (TR K M).one i hiM (2 * (2 * M) + 1),
    (TR K M).mul_comm, (TR K M).one_mul]

end Trunc

#print axioms Trunc.eta27w_coef

lang/Eta32.lean

lean_all/lang/Eta32.lean

/-
  lang/Eta32 — T47, cycle 557: **the 32a eta product as a product of two theta series, at every degree**.
  In every commutative ring with q^M = 0, with x = q^4:
    q·(x; x)_M²·(x²; x²)_M² = q · G(x) · T(x),
    G(x) = Σ_{m ≤ 4M} (−1)^m x^((m − 2M)²)  (Gauss),   T(x) = Σ_{j < 2M+2} (−1)^j (2j+1) x^(j² + j)  (Jacobi),
  i.e. η(4z)²η(8z)² = Σ_{n ∈ ℤ, j ≥ 0} (−1)^(n+j) (2j+1) q^(4n² + (2j+1)²) coefficientwise.

    od x T          ∏_{k<T} (1 − x^(2k+1))
    pp_split        (x; x)_(2T) = (x²; x²)_T · od x T
    jf_neg          the Gauss product ∏_{k<N}(q^(2k+1) − (1 + q^(4k+2)) + q^(2k+1)) = (−1)^N od q N²
    gauss_prod      G(q)·(q²; q²)_M = (q; q)_M²  when q^M = 0
    eta32
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.JacobiP

namespace QBinom

open FTAChain

variable {R : Type} (K : DetChain.CRing R)

def od (x : R) : Nat → R
  | 0 => K.one
  | T + 1 => K.mul (od x T) (K.add K.one (K.neg (Frobenius.pw K x (2 * T + 1))))

theorem pp_split (x : R) : ∀ T, pp K x (2 * T) = K.mul (pp K (K.mul x x) T) (od K x T)
  | 0 => by show K.one = K.mul K.one K.one; lib_ring
  | T + 1 => by
    rw [show 2 * (T + 1) = 2 * T + 1 + 1 by lib_nat]
    show K.mul (K.mul (pp K x (2 * T)) (K.add K.one (K.neg (Frobenius.pw K x (2 * T + 1))))) (K.add K.one (K.neg (Frobenius.pw K x (2 * T + 1 + 1)))) =
      K.mul (K.mul (pp K (K.mul x x) T) (K.add K.one (K.neg (Frobenius.pw K (K.mul x x) (T + 1)))))
        (K.mul (od K x T) (K.add K.one (K.neg (Frobenius.pw K x (2 * T + 1)))))
    rw [pp_split x T, pwQ, show 2 * (T + 1) = 2 * T + 1 + 1 by lib_nat]
    lib_ring

theorem jf_neg (q : R) : ∀ N, jf K q (K.neg K.one) N = K.mul (Frobenius.pw K (K.neg K.one) N) (K.mul (od K q N) (od K q N))
  | 0 => by show K.one = K.mul K.one (K.mul K.one K.one); lib_ring
  | N + 1 => by
    show K.mul (jf K q (K.neg K.one) N) (K.add (K.add (Frobenius.pw K q (2 * N + 1)) (K.mul (K.add K.one (Frobenius.pw K q (4 * N + 2))) (K.neg K.one)))
        (K.mul (Frobenius.pw K q (2 * N + 1)) (K.mul (K.neg K.one) (K.neg K.one)))) =
      K.mul (K.mul (Frobenius.pw K (K.neg K.one) N) (K.neg K.one))
        (K.mul (K.mul (od K q N) (K.add K.one (K.neg (Frobenius.pw K q (2 * N + 1))))) (K.mul (od K q N) (K.add K.one (K.neg (Frobenius.pw K q (2 * N + 1))))))
    have e : Frobenius.pw K q (4 * N + 2) = K.mul (Frobenius.pw K q (2 * N + 1)) (Frobenius.pw K q (2 * N + 1)) :=
      (pw_join K q _ _ _ (by lib_nat)).symm
    rw [jf_neg q N, e]
    generalize Frobenius.pw K q (2 * N + 1) = A
    lib_ring

theorem gauss_prod (q : R) (M : Nat) (hq : Frobenius.pw K q M = K.zero) :
    K.mul (es K (fun m => Frobenius.pw K q (Ex (2 * M) m)) (K.neg K.one) (2 * (2 * M) + 1)) (pp K (K.mul q q) M) =
      K.mul (pp K q M) (pp K q M) := by
  have hQ : Frobenius.pw K (K.mul q q) M = K.zero := by
    rw [pwQ]; exact pw_zero_of_le K q M _ hq (Nat.le_trans (Nat.le_add_left M M) (Nat.le_of_eq (by lib_nat)))
  have s1 := pp_split K q (2 * M)
  have s2 : pp K (K.mul q q) (2 * M) = pp K (K.mul q q) M := by rw [show 2 * M = M + M by lib_nat, pp_stable K _ M hQ M]
  have s3 : pp K q (2 * (2 * M)) = pp K q M := by rw [show 2 * (2 * M) = M + 3 * M by lib_nat, pp_stable K q M hq]
  rw [s2, s3] at s1
  rw [gauss_mod K q M hq, jf_neg, pm_even, s1]
  lib_ring

/-- **η(4z)²η(8z)² = Gauss(q⁴)·Jacobi(q⁴)·q at every coefficient** (x = q⁴, x² = q⁸). -/
theorem eta32 (q : R) (M : Nat) (hq : Frobenius.pw K q M = K.zero) :
    K.mul q (K.mul (K.mul (pp K (Frobenius.pw K q 4) M) (pp K (Frobenius.pw K q 4) M))
      (K.mul (pp K (K.mul (Frobenius.pw K q 4) (Frobenius.pw K q 4)) M) (pp K (K.mul (Frobenius.pw K q 4) (Frobenius.pw K q 4)) M))) =
    K.mul q (K.mul (es K (fun m => Frobenius.pw K (Frobenius.pw K q 4) (Ex (2 * M) m)) (K.neg K.one) (2 * (2 * M) + 1))
      (T K (Frobenius.pw K q 4) (2 * M + 2))) := by
  have hx : Frobenius.pw K (Frobenius.pw K q 4) M = K.zero := by
    rw [TwoPower.pwpow]; exact pw_zero_of_le K q M _ hq (Nat.le_trans (Nat.le_add_left M (3 * M)) (Nat.le_of_eq (by lib_nat)))
  generalize Frobenius.pw K q 4 = x at hx ⊢
  have hX : Frobenius.pw K (K.mul x x) M = K.zero := by
    rw [pwQ]; exact pw_zero_of_le K x M _ hx (Nat.le_trans (Nat.le_add_left M M) (Nat.le_of_eq (by lib_nat)))
  have gp := gauss_prod K x M hx
  rw [jacobi_eta3 K x M hx, pp_stable K (K.mul x x) M hX 1]
  generalize es K (fun m => Frobenius.pw K x (Ex (2 * M) m)) (K.neg K.one) (2 * (2 * M) + 1) = G at gp ⊢
  generalize pp K (K.mul x x) M = P at gp ⊢
  calc _ = K.mul q (K.mul (K.mul (pp K x M) (pp K x M)) (K.mul P P)) := rfl
    _ = K.mul q (K.mul (K.mul G P) (K.mul P P)) := by rw [gp]
    _ = _ := by lib_ring

end QBinom

#print axioms QBinom.pp_split
#print axioms QBinom.jf_neg
#print axioms QBinom.gauss_prod
#print axioms QBinom.eta32

lang/Eta32C.lean

lean_all/lang/Eta32C.lean

/-
  lang/Eta32C — T47, cycle 562: the n-th coefficient of η(4z)²η(8z)² (truncated product in R[q]/(q^M), n < M) as an explicit double sum:
    nth (q·(q⁴;q⁴)_M²(q⁸;q⁸)_M²) n = Σ_{e ∈ zl (2M)} (−1)^|e| · tc (2M+2) (n − 4e² − 1),
    tc J i = Σ_{j<J} (−1)^j (2j+1) [4(j² + j) = i].

    shn_congr      shn E f n only depends on f at ≤ n
    nth_T          coefficients of the Jacobi sum T(q⁴)
    eta32_coef
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.TruncC

namespace Trunc

open GLnLocal

variable {R : Type} (K : DetChain.CRing R)

theorem shn_congr : ∀ E (f g : Nat → R) n, (∀ i, i ≤ n → f i = g i) → shn K E f n = shn K E g n
  | 0, _, _, n, h => h n (Nat.le_refl n)
  | _ + 1, _, _, 0, _ => rfl
  | E + 1, f, g, n + 1, h => shn_congr E f g n (fun i hi => h i (Nat.le_succ_of_le hi))

def tc : Nat → Nat → R
  | 0, _ => K.zero
  | J + 1, i => K.add (tc J i) (K.mul (Frobenius.pw K (K.neg K.one) J) (K.mul (Cheb.emb K (2 * J + 1)) (shn K (4 * (J * J + J)) (delta K) i)))

variable (M : Nat)

theorem nth_T : ∀ J i, i < M → nth K M (QBinom.T (TR K M) (Frobenius.pw (TR K M) (qe K M) 4) J) i = tc K J i
  | 0, i, hi => nth_zero K M i hi
  | J + 1, i, hi => by
    show nth K M ((TR K M).add (QBinom.T (TR K M) (Frobenius.pw (TR K M) (qe K M) 4) J)
      ((TR K M).mul (Frobenius.pw (TR K M) ((TR K M).neg (TR K M).one) J) ((TR K M).mul (Cheb.emb (TR K M) (2 * J + 1))
        (Frobenius.pw (TR K M) (Frobenius.pw (TR K M) (qe K M) 4) (J * J + J))))) i = _
    rw [nth_add K M _ _ i hi, nth_T J i hi, sc_mul K M _ _ (sc_pw K M _ _ (sc_negone K M) J) _ i hi,
      sc_mul K M _ _ (sc_emb K M (2 * J + 1)) _ i hi, TwoPower.pwpow, nth_pw K M _ i hi]
    rfl

theorem eta32_coef (n : Nat) (hn : n < M) :
    nth K M ((TR K M).mul (qe K M) ((TR K M).mul
      ((TR K M).mul (QBinom.pp (TR K M) (Frobenius.pw (TR K M) (qe K M) 4) M) (QBinom.pp (TR K M) (Frobenius.pw (TR K M) (qe K M) 4) M))
      ((TR K M).mul (QBinom.pp (TR K M) ((TR K M).mul (Frobenius.pw (TR K M) (qe K M) 4) (Frobenius.pw (TR K M) (qe K M) 4)) M)
        (QBinom.pp (TR K M) ((TR K M).mul (Frobenius.pw (TR K M) (qe K M) 4) (Frobenius.pw (TR K M) (qe K M) 4)) M)))) n =
    YMChain.sumK K (fun e => K.mul (Frobenius.pw K (K.neg K.one) (Int.natAbs e))
      (shn K (4 * (Int.natAbs e * Int.natAbs e) + 1) (tc K (2 * M + 2)) n)) (HeckeMult.zl (2 * M)) := by
  rw [QBinom.eta32 (TR K M) (qe K M) M (qe_nil K M), QBinom.gz_even]
  generalize hx : Frobenius.pw (TR K M) (qe K M) 4 = x
  generalize hT : QBinom.T (TR K M) x (2 * M + 2) = Tx
  have e1 : (TR K M).mul (qe K M) ((TR K M).mul (QBinom.Bz (TR K M) x (2 * M)) Tx) =
      YMChain.sumK (TR K M) (fun e => (TR K M).mul (Frobenius.pw (TR K M) ((TR K M).neg (TR K M).one) (Int.natAbs e))
        ((TR K M).mul (Frobenius.pw (TR K M) (qe K M) (4 * (Int.natAbs e * Int.natAbs e) + 1)) Tx)) (HeckeMult.zl (2 * M)) := by
    show (TR K M).mul (qe K M) ((TR K M).mul (YMChain.sumK (TR K M) (QBinom.gfz (TR K M) x) (HeckeMult.zl (2 * M))) Tx) = _
    rw [YMChain.sumK_mul, YMChain.mul_sumK]
    apply YMChain.sumK_ext
    intro e
    show (TR K M).mul (qe K M) ((TR K M).mul ((TR K M).mul (Frobenius.pw (TR K M) ((TR K M).neg (TR K M).one) (Int.natAbs e))
      (Frobenius.pw (TR K M) x (Int.natAbs e * Int.natAbs e))) Tx) = _
    rw [← hx, TwoPower.pwpow]
    show _ = (TR K M).mul (Frobenius.pw (TR K M) ((TR K M).neg (TR K M).one) (Int.natAbs e))
      ((TR K M).mul ((TR K M).mul (Frobenius.pw (TR K M) (qe K M) (4 * (Int.natAbs e * Int.natAbs e))) (qe K M)) Tx)
    lib_ring
  rw [e1, nth_sumK K M _ n hn]
  apply YMChain.sumK_ext
  intro e
  rw [sc_mul K M _ _ (sc_pw K M _ _ (sc_negone K M) _) _ n hn, nth_pwmul K M _ _ n hn,
    shn_congr K _ (nth K M Tx) (tc K (2 * M + 2)) n (fun i hi => by rw [← hT, ← hx]; exact nth_T K M _ i (Nat.lt_of_le_of_lt hi hn))]

end Trunc

#print axioms Trunc.shn_congr
#print axioms Trunc.nth_T
#print axioms Trunc.eta32_coef

lang/Eta36.lean

lean_all/lang/Eta36.lean

/-
  lang/Eta36 — T47, cycle 572: **the 36a eta product as Euler × Jacobi at every degree**. In every commutative ring with q^M = 0,
  x = q⁶, s = q³:
    q·(x; x)_M⁴ = q · Σ_{m ≤ 4M} (−1)^m x^((3j²+j)/2) · Σ_{i < 2M+2} (−1)^i (2i+1) s^(i² + i)      (j = m − 2M),
  i.e. η(6z)⁴ = Σ_{j ∈ ℤ, i ≥ 0} (−1)^(j+i) (2i+1) q^(1 + 3j(3j+1) + 3i(i+1)) = Σ (−1)^(j+i)(2i+1) q^(((6j+1)² + 3(2i+1)²)/4).
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Pent

namespace QBinom

open FTAChain

variable {R : Type} (K : DetChain.CRing R)

theorem eta36 (q : R) (M : Nat) (hq : Frobenius.pw K q M = K.zero) :
    K.mul q (K.mul (K.mul (pp K (Frobenius.pw K q 6) M) (pp K (Frobenius.pw K q 6) M))
      (K.mul (pp K (Frobenius.pw K q 6) M) (pp K (Frobenius.pw K q 6) M))) =
    K.mul q (K.mul (es K (fun m => Frobenius.pw K (Frobenius.pw K q 6) (2 * Ta (2 * M) m + Tb (2 * M) m)) (K.neg K.one) (2 * (2 * M) + 1))
      (T K (Frobenius.pw K q 3) (2 * M + 2))) := by
  have hx : Frobenius.pw K (Frobenius.pw K q 6) M = K.zero := by
    rw [TwoPower.pwpow]; exact pw_zero_of_le K q M _ hq (Nat.le_trans (Nat.le_add_left M (5 * M)) (Nat.le_of_eq (by lib_nat)))
  have hs : Frobenius.pw K (Frobenius.pw K q 3) M = K.zero := by
    rw [TwoPower.pwpow]; exact pw_zero_of_le K q M _ hq (Nat.le_trans (Nat.le_add_left M (2 * M)) (Nat.le_of_eq (by lib_nat)))
  have e6 : K.mul (Frobenius.pw K q 3) (Frobenius.pw K q 3) = Frobenius.pw K q 6 := pw_sum K q 3 3 6 rfl
  rw [pent K _ M hx, jacobi_eta3 K _ M hs, e6, pp_stable K _ M hx 1]
  lib_ring

end QBinom

#print axioms QBinom.eta36

lang/Eta36All.lean

lean_all/lang/Eta36All.lean

/-
  lang/Eta36All — T47, cycle 593: **36a: η(6z)⁴ = Σ_{x ≡ 1 (3)} x q^(x² + 3y²) at every coefficient**:
    etaC36 n = c3 n   for every n
  (odd n: All36; even n ≥ 2: both 0, Even36 and Orb36; n = 0: both 0). The n < 200 evaluation row theta-eta for 36a, now at every n.
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.Orb36

namespace Theta36

open DetChain YMChain FiniteRecChain HeckeMult Hecke32 Hecke36

theorem nat_parity : ∀ n : Nat, (∃ s, n = 2 * s + 1) ∨ (∃ t, n = 2 * t)
  | 0 => Or.inr ⟨0, rfl⟩
  | n + 1 => by
    rcases nat_parity n with ⟨s, hs⟩ | ⟨t, ht⟩
    · exact Or.inr ⟨s + 1, by rw [hs]; lib_nat⟩
    · exact Or.inl ⟨t, by rw [ht]⟩

theorem etaC36_zero : etaC36 0 = 0 := by
  show Trunc.nth Z (2 * 0 + 1) ((Trunc.TR Z (2 * 0 + 1)).mul (Trunc.qe Z (2 * 0 + 1)) _) 0 = 0
  rw [Trunc.nth_qmul Z (2 * 0 + 1) _ 0 (Nat.zero_lt_succ 0)]; rfl

theorem c3_zero : c3 0 = 0 := by
  show Z.add (Z.add (F3 0 (0, 0)) Z.zero) Z.zero = 0
  unfold F3
  by_cases h : P3 0 (0, 0)
  · rw [ite_pos' _ _ h]; rfl
  · rw [ite_neg' _ _ h]; rfl

/-- **η(6z)⁴ = Σ_{x≡1 (3)} x q^(x²+3y²) at every n.** -/
theorem eta36_all (n : Nat) : etaC36 n = c3 n := by
  rcases nat_parity n with ⟨s, hs⟩ | ⟨t, ht⟩
  · rw [hs]; exact eta36_odd s
  · cases t with
    | zero => rw [ht]; exact etaC36_zero.trans c3_zero.symm
    | succ t =>
      rw [ht, show 2 * (t + 1) = 2 * t + 1 + 1 by lib_nat, etaC36_even, show 2 * t + 1 + 1 = 2 * (t + 1) by lib_nat, c3_even]

end Theta36

#print axioms Theta36.nat_parity
#print axioms Theta36.etaC36_zero
#print axioms Theta36.c3_zero
#print axioms Theta36.eta36_all

lang/Eta36C.lean

lean_all/lang/Eta36C.lean

/-
  lang/Eta36C — T47, cycle 573: the coefficient of qⁿ (1 ≤ n < M) of η(6z)⁴ in R[q]/(q^M) as an explicit double sum:
    [qⁿ] = Σ_{m ≤ 4M} (−1)^m · tcd 3 (2M+2) (n − 1 − 6·(2Ta + Tb)(2M, m)),
    tcd d J i = Σ_{i' < J} (−1)^i' (2i'+1) [d(i'² + i') = i].

    tcd, nth_Td             coefficients of the Jacobi sum T(q^d)
    nth_es_mul              coefficients of (Σ_m (−1)^m x^(E m))·b for x = q^d
    eta36_coef
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Eta36
import lang.Eta32C

namespace Trunc

open GLnLocal

variable {R : Type} (K : DetChain.CRing R)

def tcd (d : Nat) : Nat → Nat → R
  | 0, _ => K.zero
  | J + 1, i => K.add (tcd d J i) (K.mul (Frobenius.pw K (K.neg K.one) J) (K.mul (Cheb.emb K (2 * J + 1)) (shn K (d * (J * J + J)) (delta K) i)))

variable (M : Nat)

theorem nth_Td (d : Nat) : ∀ J i, i < M → nth K M (QBinom.T (TR K M) (Frobenius.pw (TR K M) (qe K M) d) J) i = tcd K d J i
  | 0, i, hi => nth_zero K M i hi
  | J + 1, i, hi => by
    show nth K M ((TR K M).add (QBinom.T (TR K M) (Frobenius.pw (TR K M) (qe K M) d) J)
      ((TR K M).mul (Frobenius.pw (TR K M) ((TR K M).neg (TR K M).one) J) ((TR K M).mul (Cheb.emb (TR K M) (2 * J + 1))
        (Frobenius.pw (TR K M) (Frobenius.pw (TR K M) (qe K M) d) (J * J + J))))) i = _
    rw [nth_add K M _ _ i hi, nth_Td d J i hi, sc_mul K M _ _ (sc_pw K M _ _ (sc_negone K M) J) _ i hi,
      sc_mul K M _ _ (sc_emb K M (2 * J + 1)) _ i hi, TwoPower.pwpow, nth_pw K M _ i hi]
    rfl

theorem nth_es_mul (d : Nat) (E : Nat → Nat) (b : Tup R M) (n : Nat) (hn : n < M) : ∀ L,
    nth K M ((TR K M).mul (QBinom.es (TR K M) (fun m => Frobenius.pw (TR K M) (Frobenius.pw (TR K M) (qe K M) d) (E m))
      ((TR K M).neg (TR K M).one) L) b) n =
    QBinom.es K (fun m => shn K (d * E m) (nth K M b) n) (K.neg K.one) L
  | 0 => by
    show nth K M ((TR K M).mul (TR K M).zero b) n = K.zero
    rw [DetChain.zero_mul', nth_zero K M n hn]
  | L + 1 => by
    show nth K M ((TR K M).mul ((TR K M).add (QBinom.es (TR K M) _ _ L)
      ((TR K M).mul (Frobenius.pw (TR K M) (Frobenius.pw (TR K M) (qe K M) d) (E L)) (Frobenius.pw (TR K M) ((TR K M).neg (TR K M).one) L))) b) n =
      K.add (QBinom.es K _ (K.neg K.one) L) (K.mul (shn K (d * E L) (nth K M b) n) (Frobenius.pw K (K.neg K.one) L))
    rw [DetChain.right_distrib, nth_add K M _ _ n hn, nth_es_mul d E b n hn L]
    have e : (TR K M).mul ((TR K M).mul (Frobenius.pw (TR K M) (Frobenius.pw (TR K M) (qe K M) d) (E L)) (Frobenius.pw (TR K M) ((TR K M).neg (TR K M).one) L)) b =
        (TR K M).mul (Frobenius.pw (TR K M) ((TR K M).neg (TR K M).one) L) ((TR K M).mul (Frobenius.pw (TR K M) (qe K M) (d * E L)) b) := by
      rw [TwoPower.pwpow]; lib_ring
    rw [e, sc_mul K M _ _ (sc_pw K M _ _ (sc_negone K M) L) _ n hn, nth_pwmul K M _ _ n hn]
    lib_ring

theorem eta36_coef (n : Nat) (hn : n + 1 < M) :
    nth K M ((TR K M).mul (qe K M) ((TR K M).mul
      ((TR K M).mul (QBinom.pp (TR K M) (Frobenius.pw (TR K M) (qe K M) 6) M) (QBinom.pp (TR K M) (Frobenius.pw (TR K M) (qe K M) 6) M))
      ((TR K M).mul (QBinom.pp (TR K M) (Frobenius.pw (TR K M) (qe K M) 6) M) (QBinom.pp (TR K M) (Frobenius.pw (TR K M) (qe K M) 6) M)))) (n + 1) =
    QBinom.es K (fun m => shn K (6 * (2 * QBinom.Ta (2 * M) m + QBinom.Tb (2 * M) m)) (tcd K 3 (2 * M + 2)) n) (K.neg K.one) (2 * (2 * M) + 1) := by
  rw [QBinom.eta36 (TR K M) (qe K M) M (qe_nil K M), nth_qmul K M _ _ hn]
  show nth K M _ n = _
  rw [nth_es_mul K M 6 _ _ n (Nat.lt_of_succ_lt hn)]
  apply QBinom.es_congr
  intro m
  exact shn_congr K _ _ _ n (fun i hi => nth_Td K M 3 _ i (Nat.lt_of_le_of_lt hi (Nat.lt_of_succ_lt hn)))

end Trunc

#print axioms Trunc.nth_Td
#print axioms Trunc.nth_es_mul
#print axioms Trunc.eta36_coef

lang/Euc11.lean

lean_all/lang/Euc11.lean

/-
  lang/Euc11 — T48/T50, cycle 647: ℤ[θ], θ = (1 + √−11)/2, is Euclidean.
  N(a + bθ) = a² + ab + 3b², 4N = (2a + b)² + 11b², conjugate (a + b) − bθ:
    en_rep, nrm_cast, en_mul, en_cj, four_nrm      norm facts (4·nrm = |2a + b|² + 11|b|² in ℕ)
    edivr          β ≠ 0: α = qβ + ρ, nrm ρ < nrm β  — two-step rounding: r₂ by N, then 2r₁ + r₂ by 2N, 16N(r) ≤ 15N²
    egcd_exists, eeuclid
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Ring11
import lang.GaussEuclid
import lang.W36Mul

namespace Euc11

open FTAChain DetChain LibInt


def en (z : Int × Int) : Int := z.1 * z.1 + z.1 * z.2 + 3 * (z.2 * z.2)
def nrm (z : Int × Int) : Nat := Int.natAbs (en z)
def cj (z : Int × Int) : Int × Int := (z.1 + z.2, -z.2)
def Dvd' (a b : Int × Int) : Prop := ∃ c, b = (Ring11.ring11 LibInt.intR).mul a c

theorem sgn (x : Int) : x = Int.ofNat (Int.natAbs x) ∨ x = -Int.ofNat (Int.natAbs x) := by
  cases x with
  | ofNat n => exact Or.inl rfl
  | negSucc n => exact Or.inr rfl

theorem le_dest (a b : Nat) (h : a ≤ b) : ∃ c, c + a = b := by
  induction h with
  | refl => exact ⟨0, Nat.zero_add a⟩
  | step _ ih =>
    obtain ⟨c, hc⟩ := ih
    exact ⟨c + 1, by rw [← hc]; lib_nat⟩

theorem ab_le (A B : Nat) : A * B ≤ A * A + 3 * (B * B) := by
  cases Nat.le_total A B with
  | inl h => exact Nat.le_trans (Nat.mul_le_mul_right B h) (Nat.le_trans (Nat.le_add_left _ (A * A)) (Nat.add_le_add_left (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ (Nat.le_succ_of_le (Nat.le_succ 1)))) _))
  | inr h => exact Nat.le_trans (Nat.mul_le_mul_left A h) (Nat.le_add_right _ _)

theorem en_rep (z : Int × Int) : ∃ c : Nat, en z = Int.ofNat c := by
  generalize hA : Int.natAbs z.1 = A
  generalize hB : Int.natAbs z.2 = B
  obtain ⟨c, hc⟩ := le_dest _ _ (ab_le A B)
  have e : Int.ofNat c + Int.ofNat A * Int.ofNat B = Int.ofNat A * Int.ofNat A + 3 * (Int.ofNat B * Int.ofNat B) := congrArg Int.ofNat hc
  have opp : en z = Int.ofNat A * Int.ofNat A + 3 * (Int.ofNat B * Int.ofNat B) + -(Int.ofNat A * Int.ofNat B) → ∃ c : Nat, en z = Int.ofNat c :=
    fun h => ⟨c, by rw [h, ← e]; lib_int⟩
  have same : en z = Int.ofNat A * Int.ofNat A + 3 * (Int.ofNat B * Int.ofNat B) + Int.ofNat A * Int.ofNat B → ∃ c : Nat, en z = Int.ofNat c :=
    fun h => ⟨A * A + 3 * (B * B) + A * B, h⟩
  unfold en
  rcases sgn z.1 with h1 | h1 <;> rcases sgn z.2 with h2 | h2 <;> rw [hA] at h1 <;> rw [hB] at h2
  · exact same (by unfold en; rw [h1, h2]; lib_int)
  · exact opp (by unfold en; rw [h1, h2]; lib_int)
  · exact opp (by unfold en; rw [h1, h2]; lib_int)
  · exact same (by unfold en; rw [h1, h2]; lib_int)

theorem nrm_cast (z : Int × Int) : Int.ofNat (nrm z) = en z := by
  obtain ⟨c, hc⟩ := en_rep z
  unfold nrm; rw [hc]; rfl

theorem four_nrm (z : Int × Int) : 4 * nrm z = Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2) + 11 * (Int.natAbs z.2 * Int.natAbs z.2) := by
  apply Int.ofNat.inj
  show 4 * Int.ofNat (nrm z) = Int.ofNat (Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2)) + 11 * Int.ofNat (Int.natAbs z.2 * Int.natAbs z.2)
  rw [nrm_cast, GaussEuclid.nas, GaussEuclid.nas]; unfold en; lib_int

theorem mul_cj (z : Int × Int) : (Ring11.ring11 LibInt.intR).mul z (cj z) = (en z, 0) := by
  apply Prod.ext
  · show z.1 * (z.1 + z.2) + -(z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2) = z.1 * z.1 + z.1 * z.2 + 3 * (z.2 * z.2)
    lib_int
  · show z.1 * -z.2 + z.2 * (z.1 + z.2) + z.2 * -z.2 = 0
    lib_int

theorem en_mul (x y : Int × Int) : en ((Ring11.ring11 LibInt.intR).mul x y) = en x * en y := by
  show (x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2)) * (x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2)) + (x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2)) * (x.1 * y.2 + x.2 * y.1 + x.2 * y.2)
      + 3 * ((x.1 * y.2 + x.2 * y.1 + x.2 * y.2) * (x.1 * y.2 + x.2 * y.1 + x.2 * y.2)) =
    (x.1 * x.1 + x.1 * x.2 + 3 * (x.2 * x.2)) * (y.1 * y.1 + y.1 * y.2 + 3 * (y.2 * y.2))
  lib_int

theorem en_cj (z : Int × Int) : en (cj z) = en z := by
  show (z.1 + z.2) * (z.1 + z.2) + (z.1 + z.2) * -z.2 + 3 * (-z.2 * -z.2) = z.1 * z.1 + z.1 * z.2 + 3 * (z.2 * z.2)
  lib_int

/-- **Division with remainder in ℤ[θ].** -/
theorem edivr (α β : Int × Int) (M : Nat) (hβ : nrm β = M + 1) :
    ∃ q, nrm ((Ring11.ring11 LibInt.intR).add α ((Ring11.ring11 LibInt.intR).neg ((Ring11.ring11 LibInt.intR).mul q β))) < M + 1 := by
  have hN : en β = Int.ofNat (M + 1) := by rw [← nrm_cast, hβ]
  match GaussEuclid.rnd ((Ring11.ring11 LibInt.intR).mul α (cj β)).2 M with
  | ⟨q2, r2, h2, b2⟩ =>
  match GaussEuclid.rnd (2 * ((Ring11.ring11 LibInt.intR).mul α (cj β)).1 + r2) (2 * M + 1) with
  | ⟨q1, t, h1, b1⟩ =>
    refine ⟨(q1, q2), ?_⟩
    generalize hρ : (Ring11.ring11 LibInt.intR).add α ((Ring11.ring11 LibInt.intR).neg ((Ring11.ring11 LibInt.intR).mul (q1, q2) β)) = ρ
    have hρc : (Ring11.ring11 LibInt.intR).mul ρ (cj β) = (((Ring11.ring11 LibInt.intR).mul α (cj β)).1 + -(q1 * Int.ofNat (M + 1)), r2) := by
      rw [← hρ]
      have e : (Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).add α ((Ring11.ring11 LibInt.intR).neg ((Ring11.ring11 LibInt.intR).mul (q1, q2) β))) (cj β) = (Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul α (cj β)) ((Ring11.ring11 LibInt.intR).neg ((Ring11.ring11 LibInt.intR).mul (q1, q2) ((Ring11.ring11 LibInt.intR).mul β (cj β)))) := by lib_ring
      rw [e, mul_cj, hN]
      apply Prod.ext
      · show ((Ring11.ring11 LibInt.intR).mul α (cj β)).1 + -(q1 * Int.ofNat (M + 1) + -(q2 * 0 + q2 * 0 + q2 * 0)) = ((Ring11.ring11 LibInt.intR).mul α (cj β)).1 + -(q1 * Int.ofNat (M + 1))
        lib_int
      · show ((Ring11.ring11 LibInt.intR).mul α (cj β)).2 + -(q1 * 0 + q2 * Int.ofNat (M + 1) + q2 * 0) = r2
        rw [h2]; lib_int
    generalize hr1 : ((Ring11.ring11 LibInt.intR).mul α (cj β)).1 + -(q1 * Int.ofNat (M + 1)) = r1 at hρc
    have ht : 2 * r1 + r2 = t := by
      rw [← hr1]
      calc 2 * (((Ring11.ring11 LibInt.intR).mul α (cj β)).1 + -(q1 * Int.ofNat (M + 1))) + r2 = (2 * ((Ring11.ring11 LibInt.intR).mul α (cj β)).1 + r2) + -(q1 * Int.ofNat (2 * M + 1 + 1)) := by
            show _ = _ + -(q1 * (2 * Int.ofNat M + 1 + 1)); show 2 * (_ + -(q1 * (Int.ofNat M + 1))) + r2 = _; lib_int
        _ = t := by rw [h1]; lib_int
    have hn : en ρ * Int.ofNat (M + 1) = en (r1, r2) := by
      rw [← hN, ← en_cj β, ← en_mul, hρc]
    have hnN : nrm ρ * (M + 1) = nrm (r1, r2) := by
      apply Int.ofNat.inj
      show Int.ofNat (nrm ρ) * Int.ofNat (M + 1) = Int.ofNat (nrm (r1, r2))
      rw [nrm_cast, nrm_cast]; exact hn
    have h4 := four_nrm (r1, r2)
    have hT : Int.natAbs (2 * r1 + r2) ≤ M + 1 := by
      rw [ht]
      apply Nat.le_of_mul_le_mul_left (show 2 * Int.natAbs t ≤ 2 * (M + 1) from Nat.le_trans b1 (Nat.le_of_eq (by lib_nat))) (Nat.zero_lt_succ 1)
    generalize Int.natAbs (2 * r1 + r2) = T at h4 hT
    have h4' : 4 * nrm (r1, r2) = T * T + 11 * (Int.natAbs r2 * Int.natAbs r2) := h4
    generalize Int.natAbs r2 = B at h4' b2
    have hle : (M + 1) * (16 * nrm ρ) ≤ (M + 1) * (15 * (M + 1)) := by
      calc (M + 1) * (16 * nrm ρ) = 4 * (4 * nrm (r1, r2)) := by rw [← hnN]; lib_nat
        _ = 4 * (T * T) + 11 * ((2 * B) * (2 * B)) := by rw [h4']; lib_nat
        _ ≤ 4 * ((M + 1) * (M + 1)) + 11 * ((M + 1) * (M + 1)) := Nat.add_le_add (Nat.mul_le_mul_left 4 (Nat.mul_le_mul hT hT)) (Nat.mul_le_mul_left 11 (Nat.mul_le_mul b2 b2))
        _ = (M + 1) * (15 * (M + 1)) := by lib_nat
    have h16 := Nat.le_of_mul_le_mul_left hle (Nat.zero_lt_succ M)
    cases Nat.lt_or_ge (nrm ρ) (M + 1) with
    | inl h => exact h
    | inr h =>
      exfalso
      have : 16 * (M + 1) ≤ 15 * (M + 1) := Nat.le_trans (Nat.mul_le_mul_left 16 h) h16
      exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (show 15 * (M + 1) < 16 * (M + 1) by
        rw [show 16 * (M + 1) = 15 * (M + 1) + (M + 1) by lib_nat]; exact Nat.lt_add_of_pos_right (Nat.zero_lt_succ _)) this)

theorem nrm_zero (z : Int × Int) (h : nrm z = 0) : z = (Ring11.ring11 LibInt.intR).zero := by
  have h4 := four_nrm z
  rw [h] at h4
  have hB : Int.natAbs z.2 * Int.natAbs z.2 = 0 := by
    have : 11 * (Int.natAbs z.2 * Int.natAbs z.2) = 0 := Nat.le_antisymm (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_of_eq h4.symm)) (Nat.zero_le _)
    cases nat_mul_zero _ _ this with
    | inl e => exact Nat.noConfusion e
    | inr e => exact e
  have hT : Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2) = 0 := Nat.le_antisymm (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq h4.symm)) (Nat.zero_le _)
  have b0 : z.2 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ hB with | inl e => exact e | inr e => exact e)
  have t0 : 2 * z.1 + z.2 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ hT with | inl e => exact e | inr e => exact e)
  have a0 : z.1 = 0 := W36Mul.two_cancel_int z.1 0 (by rw [show 2 * z.1 = 2 * z.1 + z.2 by rw [b0]; lib_int, t0]; rfl)
  exact Prod.ext a0 b0

theorem egcd_base (α β : Int × Int) (hb : β = (Ring11.ring11 LibInt.intR).zero) :
    ∃ d x y, d = (Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul x α) ((Ring11.ring11 LibInt.intR).mul y β) ∧ Dvd' d α ∧ Dvd' d β :=
  ⟨α, (Ring11.ring11 LibInt.intR).one, (Ring11.ring11 LibInt.intR).zero, by rw [hb]; lib_ring, ⟨(Ring11.ring11 LibInt.intR).one, by lib_ring⟩, ⟨(Ring11.ring11 LibInt.intR).zero, by rw [hb]; lib_ring⟩⟩

/-- **gcd as a combination in ℤ[ω].** -/
theorem egcd_exists : ∀ G (α β : Int × Int), nrm β ≤ G →
    ∃ d x y, d = (Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul x α) ((Ring11.ring11 LibInt.intR).mul y β) ∧ Dvd' d α ∧ Dvd' d β
  | 0, α, β, hG => egcd_base α β (nrm_zero β (Nat.le_antisymm hG (Nat.zero_le _)))
  | G + 1, α, β, hG => by
    cases h : nrm β with
    | zero => exact egcd_base α β (nrm_zero β h)
    | succ M =>
        match edivr α β M h with
        | ⟨q, hq⟩ =>
          have hρ : nrm ((Ring11.ring11 LibInt.intR).add α ((Ring11.ring11 LibInt.intR).neg ((Ring11.ring11 LibInt.intR).mul q β))) ≤ G :=
            Nat.le_of_lt_succ (Nat.lt_of_lt_of_le hq (Nat.le_trans (Nat.le_of_eq h.symm) hG))
          match egcd_exists G β _ hρ with
          | ⟨d, x, y, hd, ⟨c1, hc1⟩, ⟨c2, hc2⟩⟩ =>
            refine ⟨d, y, (Ring11.ring11 LibInt.intR).add x ((Ring11.ring11 LibInt.intR).neg ((Ring11.ring11 LibInt.intR).mul y q)), by rw [hd]; lib_ring, ⟨(Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul q c1) c2, ?_⟩, ⟨c1, hc1⟩⟩
            have e : α = (Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul q β) ((Ring11.ring11 LibInt.intR).add α ((Ring11.ring11 LibInt.intR).neg ((Ring11.ring11 LibInt.intR).mul q β))) := by lib_ring
            rw [e, hc2, hc1]
            lib_ring

/-- **Euclid's lemma in ℤ[ω].** -/
theorem eeuclid (α β γ x y : Int × Int) (h1 : (Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul x α) ((Ring11.ring11 LibInt.intR).mul y β) = (Ring11.ring11 LibInt.intR).one) (h : Dvd' α ((Ring11.ring11 LibInt.intR).mul β γ)) : Dvd' α γ :=
  match h with
  | ⟨c, hc⟩ => ⟨(Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul x γ) ((Ring11.ring11 LibInt.intR).mul y c), by
      calc γ = (Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul x α) ((Ring11.ring11 LibInt.intR).mul y β)) γ := by rw [h1]; lib_ring
        _ = (Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul x α) γ) ((Ring11.ring11 LibInt.intR).mul y ((Ring11.ring11 LibInt.intR).mul β γ)) := by lib_ring
        _ = (Ring11.ring11 LibInt.intR).mul α ((Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul x γ) ((Ring11.ring11 LibInt.intR).mul y c)) := by rw [hc]; lib_ring⟩

end Euc11

#print axioms Euc11.sgn
#print axioms Euc11.le_dest
#print axioms Euc11.ab_le
#print axioms Euc11.en_rep
#print axioms Euc11.nrm_cast
#print axioms Euc11.four_nrm
#print axioms Euc11.mul_cj
#print axioms Euc11.en_mul
#print axioms Euc11.en_cj
#print axioms Euc11.edivr
#print axioms Euc11.nrm_zero
#print axioms Euc11.egcd_base
#print axioms Euc11.egcd_exists
#print axioms Euc11.eeuclid

lang/Euc163.lean

lean_all/lang/Euc163.lean

/-
  lang/Euc163 — T48/T50: ℤ[θ], θ = (1 + √−163)/2, N(a + bθ) = a² + ab + 41b², 4N = (2a + b)² + 163b²: norm facts and Euclid's lemma.
  (The Dedekind–Hasse step is in DH163.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Ring163
import lang.GaussEuclid
import lang.W36Mul

namespace Euc163

open FTAChain DetChain LibInt


def en (z : Int × Int) : Int := z.1 * z.1 + z.1 * z.2 + 41 * (z.2 * z.2)
def nrm (z : Int × Int) : Nat := Int.natAbs (en z)
def cj (z : Int × Int) : Int × Int := (z.1 + z.2, -z.2)
def Dvd' (a b : Int × Int) : Prop := ∃ c, b = (Ring163.ring163 LibInt.intR).mul a c

theorem sgn (x : Int) : x = Int.ofNat (Int.natAbs x) ∨ x = -Int.ofNat (Int.natAbs x) := by
  cases x with
  | ofNat n => exact Or.inl rfl
  | negSucc n => exact Or.inr rfl

theorem le_dest (a b : Nat) (h : a ≤ b) : ∃ c, c + a = b := by
  induction h with
  | refl => exact ⟨0, Nat.zero_add a⟩
  | step _ ih =>
    obtain ⟨c, hc⟩ := ih
    exact ⟨c + 1, by rw [← hc]; lib_nat⟩

theorem ab_le (A B : Nat) : A * B ≤ A * A + 41 * (B * B) := by
  cases Nat.le_total A B with
  | inl h => exact Nat.le_trans (Nat.mul_le_mul_right B h) (Nat.le_trans (Nat.le_add_left _ (A * A)) (Nat.add_le_add_left (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ (Nat.le_add_left 1 40))) _))
  | inr h => exact Nat.le_trans (Nat.mul_le_mul_left A h) (Nat.le_add_right _ _)

theorem en_rep (z : Int × Int) : ∃ c : Nat, en z = Int.ofNat c := by
  generalize hA : Int.natAbs z.1 = A
  generalize hB : Int.natAbs z.2 = B
  obtain ⟨c, hc⟩ := le_dest _ _ (ab_le A B)
  have e : Int.ofNat c + Int.ofNat A * Int.ofNat B = Int.ofNat A * Int.ofNat A + 41 * (Int.ofNat B * Int.ofNat B) := congrArg Int.ofNat hc
  have opp : en z = Int.ofNat A * Int.ofNat A + 41 * (Int.ofNat B * Int.ofNat B) + -(Int.ofNat A * Int.ofNat B) → ∃ c : Nat, en z = Int.ofNat c :=
    fun h => ⟨c, by rw [h, ← e]; lib_int⟩
  have same : en z = Int.ofNat A * Int.ofNat A + 41 * (Int.ofNat B * Int.ofNat B) + Int.ofNat A * Int.ofNat B → ∃ c : Nat, en z = Int.ofNat c :=
    fun h => ⟨A * A + 41 * (B * B) + A * B, h⟩
  unfold en
  rcases sgn z.1 with h1 | h1 <;> rcases sgn z.2 with h2 | h2 <;> rw [hA] at h1 <;> rw [hB] at h2
  · exact same (by unfold en; rw [h1, h2]; lib_int)
  · exact opp (by unfold en; rw [h1, h2]; lib_int)
  · exact opp (by unfold en; rw [h1, h2]; lib_int)
  · exact same (by unfold en; rw [h1, h2]; lib_int)

theorem nrm_cast (z : Int × Int) : Int.ofNat (nrm z) = en z := by
  obtain ⟨c, hc⟩ := en_rep z
  unfold nrm; rw [hc]; rfl

theorem four_nrm (z : Int × Int) : 4 * nrm z = Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2) + 163 * (Int.natAbs z.2 * Int.natAbs z.2) := by
  apply Int.ofNat.inj
  show 4 * Int.ofNat (nrm z) = Int.ofNat (Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2)) + 163 * Int.ofNat (Int.natAbs z.2 * Int.natAbs z.2)
  rw [nrm_cast, GaussEuclid.nas, GaussEuclid.nas]; unfold en; lib_int

theorem mul_cj (z : Int × Int) : (Ring163.ring163 LibInt.intR).mul z (cj z) = (en z, 0) := by
  apply Prod.ext
  · show z.1 * (z.1 + z.2) + -(z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 *  -- (+205 chars; full line in the Zenodo source)
    lib_int
  · show z.1 * -z.2 + z.2 * (z.1 + z.2) + z.2 * -z.2 = 0
    lib_int

theorem en_mul (x y : Int × Int) : en ((Ring163.ring163 LibInt.intR).mul x y) = en x * en y := by
  show (x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 *   -- (+1169 chars; full line in the Zenodo source)
      + 41 * ((x.1 * y.2 + x.2 * y.1 + x.2 * y.2) * (x.1 * y.2 + x.2 * y.1 + x.2 * y.2)) =
    (x.1 * x.1 + x.1 * x.2 + 41 * (x.2 * x.2)) * (y.1 * y.1 + y.1 * y.2 + 41 * (y.2 * y.2))
  lib_int

theorem en_cj (z : Int × Int) : en (cj z) = en z := by
  show (z.1 + z.2) * (z.1 + z.2) + (z.1 + z.2) * -z.2 + 41 * (-z.2 * -z.2) = z.1 * z.1 + z.1 * z.2 + 41 * (z.2 * z.2)
  lib_int

theorem nrm_zero (z : Int × Int) (h : nrm z = 0) : z = (Ring163.ring163 LibInt.intR).zero := by
  have h4 := four_nrm z
  rw [h] at h4
  have hB : Int.natAbs z.2 * Int.natAbs z.2 = 0 := by
    have : 163 * (Int.natAbs z.2 * Int.natAbs z.2) = 0 := Nat.le_antisymm (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_of_eq h4.symm)) (Nat.zero_le _)
    cases nat_mul_zero _ _ this with
    | inl e => exact Nat.noConfusion e
    | inr e => exact e
  have hT : Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2) = 0 := Nat.le_antisymm (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq h4.symm)) (Nat.zero_le _)
  have b0 : z.2 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ hB with | inl e => exact e | inr e => exact e)
  have t0 : 2 * z.1 + z.2 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ hT with | inl e => exact e | inr e => exact e)
  have a0 : z.1 = 0 := W36Mul.two_cancel_int z.1 0 (by rw [show 2 * z.1 = 2 * z.1 + z.2 by rw [b0]; lib_int, t0]; rfl)
  exact Prod.ext a0 b0

theorem egcd_base (α β : Int × Int) (hb : β = (Ring163.ring163 LibInt.intR).zero) :
    ∃ d x y, d = (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul x α) ((Ring163.ring163 LibInt.intR).mul y β) ∧ Dvd' d α ∧ Dvd' d β :=
  ⟨α, (Ring163.ring163 LibInt.intR).one, (Ring163.ring163 LibInt.intR).zero, by rw [hb]; lib_ring, ⟨(Ring163.ring163 LibInt.intR).one, by lib_ring⟩, ⟨(Ring163.ring163 LibInt.intR).zero, by rw [hb]; lib_ring⟩⟩

/-- **Euclid's lemma in ℤ[ω].** -/
theorem eeuclid (α β γ x y : Int × Int) (h1 : (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul x α) ((Ring163.ring163 LibInt.intR).mul y β) = (Ring163.ring163 LibInt.intR).one) (h : Dvd' α ((Ring163.ring163 LibInt.intR).mul β γ)) : Dvd' α γ :=
  match h with
  | ⟨c, hc⟩ => ⟨(Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul x γ) ((Ring163.ring163 LibInt.intR).mul y c), by
      calc γ = (Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul x α) ((Ring163.ring163 LibInt.intR).mul y β)) γ := by rw [h1]; lib_ring
        _ = (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul x α) γ) ((Ring163.ring163 LibInt.intR).mul y ((Ring163.ring163 LibInt.intR).mul β γ)) := by lib_ring
        _ = (Ring163.ring163 LibInt.intR).mul α ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul x γ) ((Ring163.ring163 LibInt.intR).mul y c)) := by rw [hc]; lib_ring⟩

theorem nrm_lt (z : Int × Int) (N : Nat) (h : Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2) + 163 * (Int.natAbs z.2 * Int.natAbs z.2) < 4 * (N * N)) :
    nrm z < N * N := by
  cases Nat.lt_or_ge (nrm z) (N * N) with
  | inl h' => exact h'
  | inr h' =>
    exfalso
    rw [← four_nrm] at h
    exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le h (Nat.mul_le_mul_left 4 h'))

theorem sq_le (T N : Nat) (h : T ≤ N) : T * T ≤ N * N := Nat.mul_le_mul h h


end Euc163

#print axioms Euc163.sgn
#print axioms Euc163.le_dest
#print axioms Euc163.ab_le
#print axioms Euc163.en_rep
#print axioms Euc163.nrm_cast
#print axioms Euc163.four_nrm
#print axioms Euc163.mul_cj
#print axioms Euc163.en_mul
#print axioms Euc163.en_cj
#print axioms Euc163.nrm_zero
#print axioms Euc163.egcd_base
#print axioms Euc163.nrm_lt
#print axioms Euc163.sq_le
#print axioms Euc163.eeuclid

lang/Euc19.lean

lean_all/lang/Euc19.lean

/-
  lang/Euc19 — T48/T50, cycle 657: ℤ[θ], θ = (1 + √−19)/2: norm facts and Euclid's lemma, and the gcd as a combination via a
  **Dedekind–Hasse step** (ℤ[θ] is not norm-Euclidean): for β ≠ 0 there are s ∈ {1, 2, θ, θ̄} and q with
    N(sα − qβ) < N(β),  and sα − qβ = 0 only for s = 1.
  N(a + bθ) = a² + ab + 5b², 4N = (2a + b)² + 19b².
    norm facts (en_rep, nrm_cast, four_nrm, mul_cj, en_mul, en_cj, nrm_zero)
    dh_r        the step on a two-step remainder r (|2r₁ + r₂|, 2|r₂| ≤ N): s = 1 if 4N(r) < 4N², else s = 2 (19(N − 2|r₂|)² < 3N²),
                and at the half points (0, ±N/2), (∓N/2, ±N/2) s = θ̄, θ with remainder (±N/2, 0)
    edh, egcd_exists, eeuclid
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Ring19
import lang.GaussEuclid
import lang.W36Mul

namespace Euc19

open FTAChain DetChain LibInt


def en (z : Int × Int) : Int := z.1 * z.1 + z.1 * z.2 + 5 * (z.2 * z.2)
def nrm (z : Int × Int) : Nat := Int.natAbs (en z)
def cj (z : Int × Int) : Int × Int := (z.1 + z.2, -z.2)
def Dvd' (a b : Int × Int) : Prop := ∃ c, b = (Ring19.ring19 LibInt.intR).mul a c

theorem sgn (x : Int) : x = Int.ofNat (Int.natAbs x) ∨ x = -Int.ofNat (Int.natAbs x) := by
  cases x with
  | ofNat n => exact Or.inl rfl
  | negSucc n => exact Or.inr rfl

theorem le_dest (a b : Nat) (h : a ≤ b) : ∃ c, c + a = b := by
  induction h with
  | refl => exact ⟨0, Nat.zero_add a⟩
  | step _ ih =>
    obtain ⟨c, hc⟩ := ih
    exact ⟨c + 1, by rw [← hc]; lib_nat⟩

theorem ab_le (A B : Nat) : A * B ≤ A * A + 5 * (B * B) := by
  cases Nat.le_total A B with
  | inl h => exact Nat.le_trans (Nat.mul_le_mul_right B h) (Nat.le_trans (Nat.le_add_left _ (A * A)) (Nat.add_le_add_left (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ (Nat.le_add_left 1 4))) _))
  | inr h => exact Nat.le_trans (Nat.mul_le_mul_left A h) (Nat.le_add_right _ _)

theorem en_rep (z : Int × Int) : ∃ c : Nat, en z = Int.ofNat c := by
  generalize hA : Int.natAbs z.1 = A
  generalize hB : Int.natAbs z.2 = B
  obtain ⟨c, hc⟩ := le_dest _ _ (ab_le A B)
  have e : Int.ofNat c + Int.ofNat A * Int.ofNat B = Int.ofNat A * Int.ofNat A + 5 * (Int.ofNat B * Int.ofNat B) := congrArg Int.ofNat hc
  have opp : en z = Int.ofNat A * Int.ofNat A + 5 * (Int.ofNat B * Int.ofNat B) + -(Int.ofNat A * Int.ofNat B) → ∃ c : Nat, en z = Int.ofNat c :=
    fun h => ⟨c, by rw [h, ← e]; lib_int⟩
  have same : en z = Int.ofNat A * Int.ofNat A + 5 * (Int.ofNat B * Int.ofNat B) + Int.ofNat A * Int.ofNat B → ∃ c : Nat, en z = Int.ofNat c :=
    fun h => ⟨A * A + 5 * (B * B) + A * B, h⟩
  unfold en
  rcases sgn z.1 with h1 | h1 <;> rcases sgn z.2 with h2 | h2 <;> rw [hA] at h1 <;> rw [hB] at h2
  · exact same (by unfold en; rw [h1, h2]; lib_int)
  · exact opp (by unfold en; rw [h1, h2]; lib_int)
  · exact opp (by unfold en; rw [h1, h2]; lib_int)
  · exact same (by unfold en; rw [h1, h2]; lib_int)

theorem nrm_cast (z : Int × Int) : Int.ofNat (nrm z) = en z := by
  obtain ⟨c, hc⟩ := en_rep z
  unfold nrm; rw [hc]; rfl

theorem four_nrm (z : Int × Int) : 4 * nrm z = Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2) + 19 * (Int.natAbs z.2 * Int.natAbs z.2) := by
  apply Int.ofNat.inj
  show 4 * Int.ofNat (nrm z) = Int.ofNat (Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2)) + 19 * Int.ofNat (Int.natAbs z.2 * Int.natAbs z.2)
  rw [nrm_cast, GaussEuclid.nas, GaussEuclid.nas]; unfold en; lib_int

theorem mul_cj (z : Int × Int) : (Ring19.ring19 LibInt.intR).mul z (cj z) = (en z, 0) := by
  apply Prod.ext
  · show z.1 * (z.1 + z.2) + -(z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2) = z.1 * z.1 + z.1 * z.2 + 5 * (z.2 * z.2)
    lib_int
  · show z.1 * -z.2 + z.2 * (z.1 + z.2) + z.2 * -z.2 = 0
    lib_int

theorem en_mul (x y : Int × Int) : en ((Ring19.ring19 LibInt.intR).mul x y) = en x * en y := by
  show (x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2)) * (x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2)) + (x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2)) * (x.1 * y.2 + x.2 * y.1 + x.2 * y.2)
      + 5 * ((x.1 * y.2 + x.2 * y.1 + x.2 * y.2) * (x.1 * y.2 + x.2 * y.1 + x.2 * y.2)) =
    (x.1 * x.1 + x.1 * x.2 + 5 * (x.2 * x.2)) * (y.1 * y.1 + y.1 * y.2 + 5 * (y.2 * y.2))
  lib_int

theorem en_cj (z : Int × Int) : en (cj z) = en z := by
  show (z.1 + z.2) * (z.1 + z.2) + (z.1 + z.2) * -z.2 + 5 * (-z.2 * -z.2) = z.1 * z.1 + z.1 * z.2 + 5 * (z.2 * z.2)
  lib_int

theorem nrm_zero (z : Int × Int) (h : nrm z = 0) : z = (Ring19.ring19 LibInt.intR).zero := by
  have h4 := four_nrm z
  rw [h] at h4
  have hB : Int.natAbs z.2 * Int.natAbs z.2 = 0 := by
    have : 19 * (Int.natAbs z.2 * Int.natAbs z.2) = 0 := Nat.le_antisymm (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_of_eq h4.symm)) (Nat.zero_le _)
    cases nat_mul_zero _ _ this with
    | inl e => exact Nat.noConfusion e
    | inr e => exact e
  have hT : Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2) = 0 := Nat.le_antisymm (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq h4.symm)) (Nat.zero_le _)
  have b0 : z.2 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ hB with | inl e => exact e | inr e => exact e)
  have t0 : 2 * z.1 + z.2 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ hT with | inl e => exact e | inr e => exact e)
  have a0 : z.1 = 0 := W36Mul.two_cancel_int z.1 0 (by rw [show 2 * z.1 = 2 * z.1 + z.2 by rw [b0]; lib_int, t0]; rfl)
  exact Prod.ext a0 b0

theorem egcd_base (α β : Int × Int) (hb : β = (Ring19.ring19 LibInt.intR).zero) :
    ∃ d x y, d = (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul x α) ((Ring19.ring19 LibInt.intR).mul y β) ∧ Dvd' d α ∧ Dvd' d β :=
  ⟨α, (Ring19.ring19 LibInt.intR).one, (Ring19.ring19 LibInt.intR).zero, by rw [hb]; lib_ring, ⟨(Ring19.ring19 LibInt.intR).one, by lib_ring⟩, ⟨(Ring19.ring19 LibInt.intR).zero, by rw [hb]; lib_ring⟩⟩

/-- **Euclid's lemma in ℤ[ω].** -/
theorem eeuclid (α β γ x y : Int × Int) (h1 : (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul x α) ((Ring19.ring19 LibInt.intR).mul y β) = (Ring19.ring19 LibInt.intR).one) (h : Dvd' α ((Ring19.ring19 LibInt.intR).mul β γ)) : Dvd' α γ :=
  match h with
  | ⟨c, hc⟩ => ⟨(Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul x γ) ((Ring19.ring19 LibInt.intR).mul y c), by
      calc γ = (Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul x α) ((Ring19.ring19 LibInt.intR).mul y β)) γ := by rw [h1]; lib_ring
        _ = (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul x α) γ) ((Ring19.ring19 LibInt.intR).mul y ((Ring19.ring19 LibInt.intR).mul β γ)) := by lib_ring
        _ = (Ring19.ring19 LibInt.intR).mul α ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul x γ) ((Ring19.ring19 LibInt.intR).mul y c)) := by rw [hc]; lib_ring⟩

set_option maxHeartbeats 0 in
/-- 2B + u = N, 3N² ≤ 19B², N > 0 ⇒ 19u² < 3N². -/
theorem ineq19 (N B u : Nat) (hN : N = 2 * B + u) (h : 3 * (N * N) ≤ 19 * (B * B)) (hpos : 0 < N) : 19 * (u * u) < 3 * (N * N) := by
  cases Nat.lt_or_ge (19 * (u * u)) (3 * (N * N)) with
  | inl h' => exact h'
  | inr hc =>
    exfalso
    subst hN
    have hA : 12 * (B * u) + 3 * (u * u) ≤ 7 * (B * B) := GaussEuclid.le_cancel _ _ (12 * (B * B)) (by
      calc 12 * (B * u) + 3 * (u * u) + 12 * (B * B) = 3 * ((2 * B + u) * (2 * B + u)) := by lib_nat
        _ ≤ 19 * (B * B) := h
        _ = 7 * (B * B) + 12 * (B * B) := by lib_nat)
    have hB : 12 * (B * B) + 12 * (B * u) ≤ 16 * (u * u) := GaussEuclid.le_cancel _ _ (3 * (u * u)) (by
      calc 12 * (B * B) + 12 * (B * u) + 3 * (u * u) = 3 * ((2 * B + u) * (2 * B + u)) := by lib_nat
        _ ≤ 19 * (u * u) := hc
        _ = 16 * (u * u) + 3 * (u * u) := by lib_nat)
    have h3 : 228 * (B * u) + 36 * (B * B) ≤ 112 * (B * B) := by
      calc 228 * (B * u) + 36 * (B * B) = 192 * (B * u) + 3 * (12 * (B * B) + 12 * (B * u)) := by lib_nat
        _ ≤ 192 * (B * u) + 3 * (16 * (u * u)) := Nat.add_le_add_left (Nat.mul_le_mul_left 3 hB) _
        _ = 16 * (12 * (B * u) + 3 * (u * u)) := by lib_nat
        _ ≤ 16 * (7 * (B * B)) := Nat.mul_le_mul_left 16 hA
        _ = 112 * (B * B) := by lib_nat
    have h4 : 228 * (B * u) ≤ 76 * (B * B) := GaussEuclid.le_cancel _ _ (36 * (B * B)) (Nat.le_trans h3 (Nat.le_of_eq (by lib_nat)))
    cases B with
    | zero =>
      have : 3 * ((2 * 0 + u) * (2 * 0 + u)) = 0 := Nat.le_antisymm (Nat.le_trans h (Nat.le_of_eq (by lib_nat))) (Nat.zero_le _)
      have hu : u * u = 0 := by
        rw [show 3 * ((2 * 0 + u) * (2 * 0 + u)) = 3 * (u * u) by lib_nat] at this
        cases nat_mul_zero _ _ this with
        | inl e => exact Nat.noConfusion e
        | inr e => exact e
      have u0 : u = 0 := by cases nat_mul_zero _ _ hu with | inl e => exact e | inr e => exact e
      rw [u0] at hpos; exact Nat.lt_irrefl 0 hpos
    | succ B' =>
      have h5 : 3 * u ≤ B' + 1 := by
        apply Nat.le_of_mul_le_mul_left (show (B' + 1) * (3 * u) ≤ (B' + 1) * (B' + 1) from
          Nat.le_of_mul_le_mul_left (show 76 * ((B' + 1) * (3 * u)) ≤ 76 * ((B' + 1) * (B' + 1)) from Nat.le_trans (Nat.le_of_eq (by lib_nat)) (Nat.le_trans h4 (Nat.le_of_eq (by lib_nat)))) (Nat.zero_lt_succ 75)) (Nat.zero_lt_succ B')
      have h6 : 9 * (u * u) ≤ (B' + 1) * (B' + 1) := Nat.le_trans (Nat.le_of_eq (by lib_nat)) (Nat.mul_le_mul h5 h5)
      have h7 : 108 * ((B' + 1) * (B' + 1)) ≤ 16 * ((B' + 1) * (B' + 1)) := by
        calc 108 * ((B' + 1) * (B' + 1)) ≤ 9 * (12 * ((B' + 1) * (B' + 1)) + 12 * ((B' + 1) * u)) := by
              rw [show 9 * (12 * ((B' + 1) * (B' + 1)) + 12 * ((B' + 1) * u)) = 108 * ((B' + 1) * (B' + 1)) + 108 * ((B' + 1) * u) by lib_nat]; exact Nat.le_add_right _ _
          _ ≤ 9 * (16 * (u * u)) := Nat.mul_le_mul_left 9 hB
          _ = 16 * (9 * (u * u)) := by lib_nat
          _ ≤ 16 * ((B' + 1) * (B' + 1)) := Nat.mul_le_mul_left 16 h6
      have : 92 * ((B' + 1) * (B' + 1)) + 16 * ((B' + 1) * (B' + 1)) ≤ 0 + 16 * ((B' + 1) * (B' + 1)) := Nat.le_trans (Nat.le_of_eq (by lib_nat)) (Nat.le_trans h7 (Nat.le_of_eq (by lib_nat)))
      have h8 := GaussEuclid.le_cancel _ _ _ this
      rw [show 92 * ((B' + 1) * (B' + 1)) = (92 * (B' * B' + 2 * B') + 91) + 1 by lib_nat] at h8
      exact Nat.not_succ_le_zero _ h8

theorem nrm_lt (z : Int × Int) (N : Nat) (h : Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2) + 19 * (Int.natAbs z.2 * Int.natAbs z.2) < 4 * (N * N)) :
    nrm z < N * N := by
  cases Nat.lt_or_ge (nrm z) (N * N) with
  | inl h' => exact h'
  | inr h' =>
    exfalso
    rw [← four_nrm] at h
    exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le h (Nat.mul_le_mul_left 4 h'))

theorem sq_le (T N : Nat) (h : T ≤ N) : T * T ≤ N * N := Nat.mul_le_mul h h

/-- **The Dedekind–Hasse step on a remainder.** -/
theorem dh_r (N0 : Nat) (r1 r2 : Int) (h2 : 2 * Int.natAbs r2 ≤ N0 + 1) (ht : Int.natAbs (2 * r1 + r2) ≤ N0 + 1) :
    ∃ s e : Int × Int, nrm ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s (r1, r2)) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0)))) < (N0 + 1) * (N0 + 1) ∧
      ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s (r1, r2)) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0))) = (Ring19.ring19 LibInt.intR).zero → s = (Ring19.ring19 LibInt.intR).one) := by
  have h4 := four_nrm (r1, r2)
  generalize hT : Int.natAbs (2 * r1 + r2) = T at h4 ht
  generalize hB : Int.natAbs r2 = B at h4 h2
  have h4' : 4 * nrm (r1, r2) = T * T + 19 * (B * B) := h4
  cases Nat.lt_or_ge (T * T + 19 * (B * B)) (4 * ((N0 + 1) * (N0 + 1))) with
  | inl hl =>
    refine ⟨(Ring19.ring19 LibInt.intR).one, (Ring19.ring19 LibInt.intR).zero, ?_, fun _ => rfl⟩
    have e : (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul (Ring19.ring19 LibInt.intR).one (r1, r2)) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul (Ring19.ring19 LibInt.intR).zero (Int.ofNat (N0 + 1), 0))) = (r1, r2) := by lib_ring
    rw [e]
    exact nrm_lt (r1, r2) (N0 + 1) (by rw [hT, hB]; exact hl)
  | inr hg =>
    have h3 : 3 * ((N0 + 1) * (N0 + 1)) ≤ 19 * (B * B) := GaussEuclid.le_cancel _ _ ((N0 + 1) * (N0 + 1)) (by
      calc 3 * ((N0 + 1) * (N0 + 1)) + (N0 + 1) * (N0 + 1) = 4 * ((N0 + 1) * (N0 + 1)) := by lib_nat
        _ ≤ T * T + 19 * (B * B) := hg
        _ ≤ (N0 + 1) * (N0 + 1) + 19 * (B * B) := Nat.add_le_add_right (sq_le T _ ht) _
        _ = 19 * (B * B) + (N0 + 1) * (N0 + 1) := by lib_nat)
    obtain ⟨u, hu⟩ := EisEuclid.le_dest _ _ h2
    have hN : N0 + 1 = 2 * B + u := by rw [← hu]; lib_nat
    have hineq := ineq19 (N0 + 1) B u hN h3 (Nat.zero_lt_succ N0)
    -- the sign of r2
    have hsg : ∃ d2 : Int, (d2 = 1 ∨ d2 = -1) ∧ r2 = d2 * Int.ofNat B := by
      rcases EisEuclid.sgn r2 with e | e
      · exact ⟨1, Or.inl rfl, by rw [e, hB]; lib_int⟩
      · exact ⟨-1, Or.inr rfl, by rw [e, hB]; lib_int⟩
    obtain ⟨d2, hd2, hr2⟩ := hsg
    obtain ⟨d1, t', hrd, hb1⟩ := GaussEuclid.rnd (2 * (2 * r1 + r2) + -(d2 * Int.ofNat (N0 + 1))) (2 * N0 + 1)
    have hr2' : 2 * r2 + -(d2 * Int.ofNat (N0 + 1)) = -(d2 * Int.ofNat u) := by
      rw [hr2, show Int.ofNat (N0 + 1) = 2 * Int.ofNat B + Int.ofNat u from congrArg Int.ofNat hN]; lib_int
    have hrd' : 2 * (2 * r1 + -(d1 * Int.ofNat (N0 + 1))) + (2 * r2 + -(d2 * Int.ofNat (N0 + 1))) = t' := by
      have e2 : Int.ofNat (2 * N0 + 1 + 1) = 2 * Int.ofNat (N0 + 1) := by show Int.ofNat (2 * N0 + 1 + 1) = 2 * (Int.ofNat N0 + 1); show 2 * Int.ofNat N0 + 1 + 1 = _; lib_int
      calc 2 * (2 * r1 + -(d1 * Int.ofNat (N0 + 1))) + (2 * r2 + -(d2 * Int.ofNat (N0 + 1))) = 2 * (2 * r1 + r2) + -(d2 * Int.ofNat (N0 + 1)) + -(d1 * (2 * Int.ofNat (N0 + 1))) := by lib_int
        _ = t' := by rw [hrd, ← e2]; lib_int
    have hT' : Int.natAbs t' ≤ N0 + 1 := Nat.le_of_mul_le_mul_left (Nat.le_trans hb1 (Nat.le_of_eq (by lib_nat))) (Nat.zero_lt_succ 1)
    have hu' : Int.natAbs (2 * r2 + -(d2 * Int.ofNat (N0 + 1))) = u := by
      rw [hr2']; rcases hd2 with e | e <;> rw [e]
      · show Int.natAbs (-(1 * Int.ofNat u)) = u; rw [show -(1 * Int.ofNat u) = -Int.ofNat u by lib_int]; exact Int.natAbs_neg _
      · show Int.natAbs (-(-1 * Int.ofNat u)) = u; rw [show -(-1 * Int.ofNat u) = Int.ofNat u by lib_int]; rfl
    generalize hrr : (2 * r1 + -(d1 * Int.ofNat (N0 + 1)), 2 * r2 + -(d2 * Int.ofNat (N0 + 1))) = r'
    have hexpr : (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul (2, 0) (r1, r2)) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul (d1, d2) (Int.ofNat (N0 + 1), 0))) = r' := by
      rw [← hrr]
      apply Prod.ext
      · show 2 * r1 + -(0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2) + -(d1 * Int.ofNat (N0 + 1) + -(d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0)) = 2 * r1 + -(d1 * Int.ofNat (N0 + 1)); lib_int
      · show 2 * r2 + 0 * r1 + 0 * r2 + -(d1 * 0 + d2 * Int.ofNat (N0 + 1) + d2 * 0) = 2 * r2 + -(d2 * Int.ofNat (N0 + 1)); lib_int
    by_cases hz : r' = (Ring19.ring19 LibInt.intR).zero
    · -- half points: N = 2B, r1 = d1 B
      have z1 : 2 * r1 + -(d1 * Int.ofNat (N0 + 1)) = 0 := by rw [← hrr] at hz; exact congrArg Prod.fst hz
      have z2 : 2 * r2 + -(d2 * Int.ofNat (N0 + 1)) = 0 := by rw [← hrr] at hz; exact congrArg Prod.snd hz
      have u0 : u = 0 := by rw [← hu', z2]; rfl
      have hNB : Int.ofNat (N0 + 1) = 2 * Int.ofNat B := by rw [show Int.ofNat (N0 + 1) = 2 * Int.ofNat B + Int.ofNat u from congrArg Int.ofNat hN, u0]; show _ + 0 = _; lib_int
      have hr1 : r1 = d1 * Int.ofNat B := W36Mul.two_cancel_int _ _ (by
        calc 2 * r1 = 2 * r1 + -(d1 * Int.ofNat (N0 + 1)) + d1 * Int.ofNat (N0 + 1) := by lib_int
          _ = 2 * (d1 * Int.ofNat B) := by rw [z1, hNB]; lib_int)
      have hBpos : B ≠ 0 := fun e => by rw [e] at hN; rw [u0] at hN; exact Nat.noConfusion hN
      have nB : nrm (d2 * Int.ofNat B, 0) < (N0 + 1) * (N0 + 1) := by
        apply nrm_lt
        have e1 : Int.natAbs (2 * (d2 * Int.ofNat B) + 0) = 2 * B := by
          rcases hd2 with e | e <;> rw [e]
          · show Int.natAbs (2 * (1 * Int.ofNat B) + 0) = 2 * B; rw [show 2 * (1 * Int.ofNat B) + 0 = Int.ofNat (2 * B) by show _ = 2 * Int.ofNat B; lib_int]; rfl
          · show Int.natAbs (2 * (-1 * Int.ofNat B) + 0) = 2 * B; rw [show 2 * (-1 * Int.ofNat B) + 0 = -Int.ofNat (2 * B) by show _ = -(2 * Int.ofNat B); lib_int]; exact Int.natAbs_neg _
        show Int.natAbs (2 * (d2 * Int.ofNat B) + 0) * Int.natAbs (2 * (d2 * Int.ofNat B) + 0) + 19 * (Int.natAbs 0 * Int.natAbs 0) < _
        rw [e1, hN, u0]
        have hBs : ∃ B', B = B' + 1 := by
          cases B with
          | zero => exact absurd rfl hBpos
          | succ b => exact ⟨b, rfl⟩
        obtain ⟨B', hB'⟩ := hBs
        rw [hB']
        show (2 * (B' + 1)) * (2 * (B' + 1)) + 19 * 0 < 4 * ((2 * (B' + 1) + 0) * (2 * (B' + 1) + 0))
        rw [show 4 * ((2 * (B' + 1) + 0) * (2 * (B' + 1) + 0)) = (2 * (B' + 1)) * (2 * (B' + 1)) + 19 * 0 + (12 * (B' * B' + 2 * B') + 11 + 1) by lib_nat]
        exact Nat.lt_add_of_pos_right (Nat.zero_lt_succ _)
      have nz : (d2 * Int.ofNat B, (0 : Int)) ≠ (Ring19.ring19 LibInt.intR).zero := fun e => by
        have : d2 * Int.ofNat B = 0 := congrArg Prod.fst e
        rcases hd2 with e2 | e2 <;> rw [e2] at this
        · exact hBpos (Int.ofNat.inj (show Int.ofNat B = 0 from by calc Int.ofNat B = 1 * Int.ofNat B := by lib_int
            _ = 0 := this))
        · exact hBpos (Int.ofNat.inj (show Int.ofNat B = 0 from by calc Int.ofNat B = -(-1 * Int.ofNat B) := by lib_int
            _ = -0 := by rw [this]
            _ = 0 := rfl))
      have fin : ∀ sv ev : Int × Int, (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul sv (r1, r2)) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul ev (Int.ofNat (N0 + 1), 0))) = (d2 * Int.ofNat B, 0) →
          ∃ s e : Int × Int, nrm ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s (r1, r2)) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0)))) < (N0 + 1) * (N0 + 1) ∧
            ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s (r1, r2)) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0))) = (Ring19.ring19 LibInt.intR).zero → s = (Ring19.ring19 LibInt.intR).one) :=
        fun sv ev he => ⟨sv, ev, by rw [he]; exact nB, fun h0 => absurd (he.symm.trans h0) nz⟩
      rcases GaussInt.int_even_odd d1 with ⟨k, hk⟩ | ⟨k, hk⟩
      · -- s = θ̄ = (1, −1), e = (k + 2 d2, −k)
        apply fin (1, -1) (k + 2 * d2, -k)
        apply Prod.ext
        · show 1 * r1 + -(-1 * r2 + -1 * r2 + -1 * r2 + -1 * r2 + -1 * r2) + -((k + 2 * d2) * Int.ofNat (N0 + 1) + -(-k * 0 + -k * 0 + -k * 0 + -k * 0 + -k * 0)) = d2 * Int.ofNat B
          rw [hr1, hr2, hk, hNB]; lib_int
        · show 1 * r2 + -1 * r1 + -1 * r2 + -((k + 2 * d2) * 0 + -k * Int.ofNat (N0 + 1) + -k * 0) = 0
          rw [hr1, hk, hNB]; lib_int
      · -- s = θ = (0, 1), e = (−3 d2, (d1 + d2)/2)
        rcases hd2 with e2 | e2
        · apply fin (0, 1) (-(3 * d2), k + 1)
          apply Prod.ext
          · show 0 * r1 + -(1 * r2 + 1 * r2 + 1 * r2 + 1 * r2 + 1 * r2) + -(-(3 * d2) * Int.ofNat (N0 + 1) + -((k + 1) * 0 + (k + 1) * 0 + (k + 1) * 0 + (k + 1) * 0 + (k + 1) * 0)) = d2 * Int.ofNat B
            rw [hr2, hNB, e2]; lib_int
          · show 0 * r2 + 1 * r1 + 1 * r2 + -(-(3 * d2) * 0 + (k + 1) * Int.ofNat (N0 + 1) + (k + 1) * 0) = 0
            rw [hr1, hr2, hk, hNB, e2]; lib_int
        · apply fin (0, 1) (-(3 * d2), k)
          apply Prod.ext
          · show 0 * r1 + -(1 * r2 + 1 * r2 + 1 * r2 + 1 * r2 + 1 * r2) + -(-(3 * d2) * Int.ofNat (N0 + 1) + -(k * 0 + k * 0 + k * 0 + k * 0 + k * 0)) = d2 * Int.ofNat B
            rw [hr2, hNB, e2]; lib_int
          · show 0 * r2 + 1 * r1 + 1 * r2 + -(-(3 * d2) * 0 + k * Int.ofNat (N0 + 1) + k * 0) = 0
            rw [hr1, hr2, hk, hNB, e2]; lib_int
    · -- s = 2
      refine ⟨(2, 0), (d1, d2), ?_, fun h0 => absurd (hexpr.symm.trans h0) hz⟩
      rw [hexpr, ← hrr]
      apply nrm_lt
      show Int.natAbs (2 * (2 * r1 + -(d1 * Int.ofNat (N0 + 1))) + (2 * r2 + -(d2 * Int.ofNat (N0 + 1)))) * Int.natAbs (2 * (2 * r1 + -(d1 * Int.ofNat (N0 + 1))) + (2 * r2 + -(d2 * Int.ofNat (N0 + 1))))
        + 19 * (Int.natAbs (2 * r2 + -(d2 * Int.ofNat (N0 + 1))) * Int.natAbs (2 * r2 + -(d2 * Int.ofNat (N0 + 1)))) < _
      rw [hrd', hu']
      calc Int.natAbs t' * Int.natAbs t' + 19 * (u * u) < (N0 + 1) * (N0 + 1) + 3 * ((N0 + 1) * (N0 + 1)) := Nat.add_lt_add_of_le_of_lt (sq_le _ _ hT') hineq
        _ = 4 * ((N0 + 1) * (N0 + 1)) := by lib_nat

end Euc19

#print axioms Euc19.sgn
#print axioms Euc19.le_dest
#print axioms Euc19.ab_le
#print axioms Euc19.en_rep
#print axioms Euc19.nrm_cast
#print axioms Euc19.four_nrm
#print axioms Euc19.mul_cj
#print axioms Euc19.en_mul
#print axioms Euc19.en_cj
#print axioms Euc19.nrm_zero
#print axioms Euc19.egcd_base
#print axioms Euc19.ineq19
#print axioms Euc19.nrm_lt
#print axioms Euc19.sq_le
#print axioms Euc19.dh_r
#print axioms Euc19.eeuclid

lang/Euc43.lean

lean_all/lang/Euc43.lean

/-
  lang/Euc43 — T48/T50: ℤ[θ], θ = (1 + √−43)/2, N(a + bθ) = a² + ab + 11b², 4N = (2a + b)² + 43b²: norm facts and Euclid's lemma.
  (The Dedekind–Hasse step is in DH43.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Ring43
import lang.GaussEuclid
import lang.W36Mul

namespace Euc43

open FTAChain DetChain LibInt


def en (z : Int × Int) : Int := z.1 * z.1 + z.1 * z.2 + 11 * (z.2 * z.2)
def nrm (z : Int × Int) : Nat := Int.natAbs (en z)
def cj (z : Int × Int) : Int × Int := (z.1 + z.2, -z.2)
def Dvd' (a b : Int × Int) : Prop := ∃ c, b = (Ring43.ring43 LibInt.intR).mul a c

theorem sgn (x : Int) : x = Int.ofNat (Int.natAbs x) ∨ x = -Int.ofNat (Int.natAbs x) := by
  cases x with
  | ofNat n => exact Or.inl rfl
  | negSucc n => exact Or.inr rfl

theorem le_dest (a b : Nat) (h : a ≤ b) : ∃ c, c + a = b := by
  induction h with
  | refl => exact ⟨0, Nat.zero_add a⟩
  | step _ ih =>
    obtain ⟨c, hc⟩ := ih
    exact ⟨c + 1, by rw [← hc]; lib_nat⟩

theorem ab_le (A B : Nat) : A * B ≤ A * A + 11 * (B * B) := by
  cases Nat.le_total A B with
  | inl h => exact Nat.le_trans (Nat.mul_le_mul_right B h) (Nat.le_trans (Nat.le_add_left _ (A * A)) (Nat.add_le_add_left (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ (Nat.le_add_left 1 10))) _))
  | inr h => exact Nat.le_trans (Nat.mul_le_mul_left A h) (Nat.le_add_right _ _)

theorem en_rep (z : Int × Int) : ∃ c : Nat, en z = Int.ofNat c := by
  generalize hA : Int.natAbs z.1 = A
  generalize hB : Int.natAbs z.2 = B
  obtain ⟨c, hc⟩ := le_dest _ _ (ab_le A B)
  have e : Int.ofNat c + Int.ofNat A * Int.ofNat B = Int.ofNat A * Int.ofNat A + 11 * (Int.ofNat B * Int.ofNat B) := congrArg Int.ofNat hc
  have opp : en z = Int.ofNat A * Int.ofNat A + 11 * (Int.ofNat B * Int.ofNat B) + -(Int.ofNat A * Int.ofNat B) → ∃ c : Nat, en z = Int.ofNat c :=
    fun h => ⟨c, by rw [h, ← e]; lib_int⟩
  have same : en z = Int.ofNat A * Int.ofNat A + 11 * (Int.ofNat B * Int.ofNat B) + Int.ofNat A * Int.ofNat B → ∃ c : Nat, en z = Int.ofNat c :=
    fun h => ⟨A * A + 11 * (B * B) + A * B, h⟩
  unfold en
  rcases sgn z.1 with h1 | h1 <;> rcases sgn z.2 with h2 | h2 <;> rw [hA] at h1 <;> rw [hB] at h2
  · exact same (by unfold en; rw [h1, h2]; lib_int)
  · exact opp (by unfold en; rw [h1, h2]; lib_int)
  · exact opp (by unfold en; rw [h1, h2]; lib_int)
  · exact same (by unfold en; rw [h1, h2]; lib_int)

theorem nrm_cast (z : Int × Int) : Int.ofNat (nrm z) = en z := by
  obtain ⟨c, hc⟩ := en_rep z
  unfold nrm; rw [hc]; rfl

theorem four_nrm (z : Int × Int) : 4 * nrm z = Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2) + 43 * (Int.natAbs z.2 * Int.natAbs z.2) := by
  apply Int.ofNat.inj
  show 4 * Int.ofNat (nrm z) = Int.ofNat (Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2)) + 43 * Int.ofNat (Int.natAbs z.2 * Int.natAbs z.2)
  rw [nrm_cast, GaussEuclid.nas, GaussEuclid.nas]; unfold en; lib_int

theorem mul_cj (z : Int × Int) : (Ring43.ring43 LibInt.intR).mul z (cj z) = (en z, 0) := by
  apply Prod.ext
  · show z.1 * (z.1 + z.2) + -(z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2) = z.1 * z.1 + z.1 * z.2 + 11 * (z.2 * z.2)
    lib_int
  · show z.1 * -z.2 + z.2 * (z.1 + z.2) + z.2 * -z.2 = 0
    lib_int

theorem en_mul (x y : Int × Int) : en ((Ring43.ring43 LibInt.intR).mul x y) = en x * en y := by
  show (x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2)) * (x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2)) + (x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.  -- (+89 chars; full line in the Zenodo source)
      + 11 * ((x.1 * y.2 + x.2 * y.1 + x.2 * y.2) * (x.1 * y.2 + x.2 * y.1 + x.2 * y.2)) =
    (x.1 * x.1 + x.1 * x.2 + 11 * (x.2 * x.2)) * (y.1 * y.1 + y.1 * y.2 + 11 * (y.2 * y.2))
  lib_int

theorem en_cj (z : Int × Int) : en (cj z) = en z := by
  show (z.1 + z.2) * (z.1 + z.2) + (z.1 + z.2) * -z.2 + 11 * (-z.2 * -z.2) = z.1 * z.1 + z.1 * z.2 + 11 * (z.2 * z.2)
  lib_int

theorem nrm_zero (z : Int × Int) (h : nrm z = 0) : z = (Ring43.ring43 LibInt.intR).zero := by
  have h4 := four_nrm z
  rw [h] at h4
  have hB : Int.natAbs z.2 * Int.natAbs z.2 = 0 := by
    have : 43 * (Int.natAbs z.2 * Int.natAbs z.2) = 0 := Nat.le_antisymm (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_of_eq h4.symm)) (Nat.zero_le _)
    cases nat_mul_zero _ _ this with
    | inl e => exact Nat.noConfusion e
    | inr e => exact e
  have hT : Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2) = 0 := Nat.le_antisymm (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq h4.symm)) (Nat.zero_le _)
  have b0 : z.2 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ hB with | inl e => exact e | inr e => exact e)
  have t0 : 2 * z.1 + z.2 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ hT with | inl e => exact e | inr e => exact e)
  have a0 : z.1 = 0 := W36Mul.two_cancel_int z.1 0 (by rw [show 2 * z.1 = 2 * z.1 + z.2 by rw [b0]; lib_int, t0]; rfl)
  exact Prod.ext a0 b0

theorem egcd_base (α β : Int × Int) (hb : β = (Ring43.ring43 LibInt.intR).zero) :
    ∃ d x y, d = (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul x α) ((Ring43.ring43 LibInt.intR).mul y β) ∧ Dvd' d α ∧ Dvd' d β :=
  ⟨α, (Ring43.ring43 LibInt.intR).one, (Ring43.ring43 LibInt.intR).zero, by rw [hb]; lib_ring, ⟨(Ring43.ring43 LibInt.intR).one, by lib_ring⟩, ⟨(Ring43.ring43 LibInt.intR).zero, by rw [hb]; lib_ring⟩⟩

/-- **Euclid's lemma in ℤ[ω].** -/
theorem eeuclid (α β γ x y : Int × Int) (h1 : (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul x α) ((Ring43.ring43 LibInt.intR).mul y β) = (Ring43.ring43 LibInt.intR).one) (h : Dvd' α ((Ring43.ring43 LibInt.intR).mul β γ)) : Dvd' α γ :=
  match h with
  | ⟨c, hc⟩ => ⟨(Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul x γ) ((Ring43.ring43 LibInt.intR).mul y c), by
      calc γ = (Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul x α) ((Ring43.ring43 LibInt.intR).mul y β)) γ := by rw [h1]; lib_ring
        _ = (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul x α) γ) ((Ring43.ring43 LibInt.intR).mul y ((Ring43.ring43 LibInt.intR).mul β γ)) := by lib_ring
        _ = (Ring43.ring43 LibInt.intR).mul α ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul x γ) ((Ring43.ring43 LibInt.intR).mul y c)) := by rw [hc]; lib_ring⟩

theorem nrm_lt (z : Int × Int) (N : Nat) (h : Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2) + 43 * (Int.natAbs z.2 * Int.natAbs z.2) < 4 * (N * N)) :
    nrm z < N * N := by
  cases Nat.lt_or_ge (nrm z) (N * N) with
  | inl h' => exact h'
  | inr h' =>
    exfalso
    rw [← four_nrm] at h
    exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le h (Nat.mul_le_mul_left 4 h'))

theorem sq_le (T N : Nat) (h : T ≤ N) : T * T ≤ N * N := Nat.mul_le_mul h h


end Euc43

#print axioms Euc43.sgn
#print axioms Euc43.le_dest
#print axioms Euc43.ab_le
#print axioms Euc43.en_rep
#print axioms Euc43.nrm_cast
#print axioms Euc43.four_nrm
#print axioms Euc43.mul_cj
#print axioms Euc43.en_mul
#print axioms Euc43.en_cj
#print axioms Euc43.nrm_zero
#print axioms Euc43.egcd_base
#print axioms Euc43.nrm_lt
#print axioms Euc43.sq_le
#print axioms Euc43.eeuclid

lang/Euc5Arc.lean

lean_all/lang/Euc5Arc.lean

/-
  lang/Euc5Arc — T49 (general α at ℓ = 5, step 3): **five points in an interval of length L**: with
  u_i + d_i = L (u_i, d_i ≥ 0) and s = Σu_i, 20Σu_i² ≤ 20Ls ≤ 25L² + 4s², i.e. 4(5Σu_i² − s²) ≤ 25L²
  (u_i² ≤ L u_i and (5L − 2s)² ≥ 0), all as natural-number identities with witnesses.
    sq_le, amgm, arc_bound
  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lib.Semi

namespace Euc5Arc

theorem sq_le (u d L : Nat) (h : u + d = L) : u * u + u * d = L * u := by
  rw [← h]; lib_nat

theorem amgm (L s : Nat) : 20 * (L * s) ≤ 25 * (L * L) + 4 * (s * s) :=
  match Nat.le_total (2 * s) (5 * L) with
  | Or.inl h =>
    match Nat.le.dest h with
    | ⟨k, hk⟩ => by
      have e : 25 * (L * L) + 4 * (s * s) = 20 * (L * s) + k * k := by
        have h5 : 5 * L = 2 * s + k := hk.symm
        have e1 : 25 * (L * L) = (5 * L) * (5 * L) := by lib_nat
        have e2 : 20 * (L * s) = 2 * (5 * L) * (2 * s) := by lib_nat
        rw [e1, e2, h5]; lib_nat
      rw [e]; exact Nat.le_add_right _ _
  | Or.inr h =>
    match Nat.le.dest h with
    | ⟨k, hk⟩ => by
      have e : 25 * (L * L) + 4 * (s * s) = 20 * (L * s) + k * k := by
        have h2 : 2 * s = 5 * L + k := hk.symm
        have e1 : 4 * (s * s) = (2 * s) * (2 * s) := by lib_nat
        have e2 : 20 * (L * s) = 2 * (5 * L) * (2 * s) := by lib_nat
        rw [e1, e2, h2]; lib_nat
      rw [e]; exact Nat.le_add_right _ _

/-- **4(5Σu² − s²) ≤ 25L²**, as 20Σu² ≤ 25L² + 4s². -/
theorem arc_bound (L u1 u2 u3 u4 u5 d1 d2 d3 d4 d5 : Nat) (h1 : u1 + d1 = L) (h2 : u2 + d2 = L) (h3 : u3 + d3 = L)
    (h4 : u4 + d4 = L) (h5 : u5 + d5 = L) :
    20 * (u1 * u1 + u2 * u2 + u3 * u3 + u4 * u4 + u5 * u5)
      ≤ 25 * (L * L) + 4 * ((u1 + u2 + u3 + u4 + u5) * (u1 + u2 + u3 + u4 + u5)) := by
  have e : 20 * (u1 * u1 + u2 * u2 + u3 * u3 + u4 * u4 + u5 * u5) + 20 * (u1 * d1 + u2 * d2 + u3 * d3 + u4 * d4 + u5 * d5)
      = 20 * (L * (u1 + u2 + u3 + u4 + u5)) := by
    have k1 := sq_le u1 d1 L h1
    have k2 := sq_le u2 d2 L h2
    have k3 := sq_le u3 d3 L h3
    have k4 := sq_le u4 d4 L h4
    have k5 := sq_le u5 d5 L h5
    calc 20 * (u1 * u1 + u2 * u2 + u3 * u3 + u4 * u4 + u5 * u5) + 20 * (u1 * d1 + u2 * d2 + u3 * d3 + u4 * d4 + u5 * d5)
        = 20 * ((u1 * u1 + u1 * d1) + (u2 * u2 + u2 * d2) + (u3 * u3 + u3 * d3) + (u4 * u4 + u4 * d4) + (u5 * u5 + u5 * d5)) := by
          lib_nat
      _ = 20 * (L * u1 + L * u2 + L * u3 + L * u4 + L * u5) := by rw [k1, k2, k3, k4, k5]
      _ = 20 * (L * (u1 + u2 + u3 + u4 + u5)) := by lib_nat
  exact Nat.le_trans (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq e)) (amgm L _)

end Euc5Arc

#print axioms Euc5Arc.sq_le
#print axioms Euc5Arc.amgm
#print axioms Euc5Arc.arc_bound

lang/Euc5Form.lean

lean_all/lang/Euc5Form.lean

/-
  lang/Euc5Form — T49 (general α at ℓ = 5, step 1): **the norm and trace forms of ℤ[ζ_5]** in the basis 1, ζ, ζ², ζ³:
    N5        N(a + bζ + cζ² + dζ³), the quartic norm form (computed in data/t49/euc5_forms.py)
    T5, R5    T = |σ_1|² + |σ_2|² = 2Σa² − Σ_{i<j} a_i a_j,  R = (|σ_1|² − |σ_2|²)/√5
    n5_ident  T² − 4N = 5R², so 4N ≤ T²
    t5_pairs  2T = Σ_{i<j} (y_i − y_j)² over the five points (a, b, c, d, 0)
  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lib.IntRing

namespace Euc5Form

def N5 (a b c d : Int) : Int := (1) * d*d*d*d + (-1) * c*d*d*d + (1) * c*c*d*d + (-1) * c*c*c*d + (1) * c*c*c*c + (-1) * b*d*d*d + (2) * b*c*d*d + (-3) * b*c*c*d + (-1) * b*c*c*c + (1) * b*b*d*d + (2) * b*b*c*d + (1) * b*b*c*c + (-1) * b*b*b*d + (-1) * b*b*b*c + (1) * b*b*b*b + (-1) * a*d*d*d + (2) * a*c*d*d + (2) * a*c*c*d + (-1) * a*c*c*c + (-3) * a*b*d*d + (-1) * a*b*c*d + (2) * a*b*c*c + (2) *  -- (+206 chars; full line in the Zenodo source)

def T5 (a b c d : Int) : Int := 2 * (a * a + b * b + c * c + d * d) - (a * b + a * c + a * d + b * c + b * d + c * d)

def R5 (a b c d : Int) : Int := a * c + a * d + b * d - a * b - b * c - c * d

theorem n5_ident (a b c d : Int) : T5 a b c d * T5 a b c d - 4 * N5 a b c d = 5 * (R5 a b c d * R5 a b c d) := by
  unfold T5 N5 R5
  lib_int

theorem t5_pairs (a b c d : Int) : 2 * T5 a b c d
    = (a - b) * (a - b) + (a - c) * (a - c) + (a - d) * (a - d) + a * a + (b - c) * (b - c) + (b - d) * (b - d) + b * b
      + (c - d) * (c - d) + c * c + d * d := by
  unfold T5
  lib_int

end Euc5Form

#print axioms Euc5Form.N5
#print axioms Euc5Form.T5
#print axioms Euc5Form.R5
#print axioms Euc5Form.n5_ident
#print axioms Euc5Form.t5_pairs

lang/Euc5Gap.lean

lean_all/lang/Euc5Gap.lean

/-
  lang/Euc5Gap — T49 (general α at ℓ = 5, step 2): **five sorted points with consecutive gaps g_1..g_4 have
  Σ_{i<j} (u_j − u_i)² ≤ 6 (g_1 + g_2 + g_3 + g_4)²** (a pair crosses gap k in at most k(5 − k) ≤ 6 ways), as an exact
  identity with a nonnegative remainder.
    gap_ident, gap_bound
  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lib.Semi

namespace Euc5Gap

theorem gap_ident (g1 g2 g3 g4 : Nat) :
    (g1 * g1 + (g1 + g2) * (g1 + g2) + (g1 + g2 + g3) * (g1 + g2 + g3) + (g1 + g2 + g3 + g4) * (g1 + g2 + g3 + g4)
      + g2 * g2 + (g2 + g3) * (g2 + g3) + (g2 + g3 + g4) * (g2 + g3 + g4) + g3 * g3 + (g3 + g4) * (g3 + g4) + g4 * g4)
    + (2 * g1 * g1 + 2 * g4 * g4 + 6 * g1 * g2 + 8 * g1 * g3 + 10 * g1 * g4 + 4 * g2 * g3 + 8 * g2 * g4 + 6 * g3 * g4)
    = 6 * ((g1 + g2 + g3 + g4) * (g1 + g2 + g3 + g4)) := by
  lib_nat

theorem gap_bound (g1 g2 g3 g4 : Nat) :
    g1 * g1 + (g1 + g2) * (g1 + g2) + (g1 + g2 + g3) * (g1 + g2 + g3) + (g1 + g2 + g3 + g4) * (g1 + g2 + g3 + g4)
      + g2 * g2 + (g2 + g3) * (g2 + g3) + (g2 + g3 + g4) * (g2 + g3 + g4) + g3 * g3 + (g3 + g4) * (g3 + g4) + g4 * g4
    ≤ 6 * ((g1 + g2 + g3 + g4) * (g1 + g2 + g3 + g4)) := by
  rw [← gap_ident g1 g2 g3 g4]
  exact Nat.le_add_right _ _

end Euc5Gap

#print axioms Euc5Gap.gap_ident
#print axioms Euc5Gap.gap_bound

lang/Euc5Perm.lean

lean_all/lang/Euc5Perm.lean

/-
  lang/Euc5Perm — T49 (general α at ℓ = 5, step 5): **five points, one in each fifth**: if W_a = a·n + e_a with
  0 ≤ e_a < n (a = 0..4) and W_a + D_ab = W_b (a < b), then Σ_{a<b} D_ab² < 100 n² (each D_ab < (b − a + 1)n, and
  Σ (5 − d)(d + 1)² = 100).
    sq_lt, pair_lt, perm_sum
  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lib.Semi
import lang.FermatUnique

namespace Euc5Perm

theorem sq_lt (D M : Nat) (h : D < M) : D * D < M * M :=
  match Nat.le.dest h with
  | ⟨k, hk⟩ => by
    rw [← hk, show (D + 1 + k) * (D + 1 + k) = D * D + (k + 1) * (D + D + k + 1) by lib_nat]
    exact Nat.lt_add_of_pos_right (Nat.mul_pos (Nat.zero_lt_succ k) (Nat.zero_lt_succ _))

theorem pair_lt (n a g ea eb D : Nat) (heb : eb < n) (hD : a * n + ea + D = (a + g) * n + eb) : D < (g + 1) * n := by
  have h1 : ea + D = g * n + eb := FermatUnique.add_cancel (a * n) _ _ (by
    rw [show a * n + (ea + D) = a * n + ea + D by lib_nat, hD]; lib_nat)
  have h2 : D ≤ g * n + eb := by rw [← h1]; exact Nat.le_add_left D ea
  have h3 : g * n + eb < (g + 1) * n := by
    rw [show (g + 1) * n = g * n + n by lib_nat]
    exact Nat.add_lt_add_left heb (g * n)
  exact Nat.lt_of_le_of_lt h2 h3

theorem perm_sum (n e0 e1 e2 e3 e4 d01 d02 d03 d04 d12 d13 d14 d23 d24 d34 : Nat) (he1 : e1 < n) (he2 : e2 < n) (he3 : e3 < n) (he4 : e4 < n)
    (h01 : 0 * n + e0 + d01 = (0 + 1) * n + e1) (h02 : 0 * n + e0 + d02 = (0 + 2) * n + e2) (h03 : 0 * n + e0 + d03 = (0 + 3) * n + e3) (h04 : 0 * n + e0 + d04 = (0 + 4) * n + e4) (h12 : 1 * n + e1 + d12 = (1 + 1) * n + e2) (h13 : 1 * n + e1 + d13 = (1 + 2) * n + e3) (h14 : 1 * n + e1 + d14 = (1 + 3) * n + e4) (h23 : 2 * n + e2 + d23 = (2 + 1) * n + e3) (h24 : 2 * n + e2 + d24 = (2 + 2) * n + e4)   -- (+45 chars; full line in the Zenodo source)
    d01 * d01 + d02 * d02 + d03 * d03 + d04 * d04 + d12 * d12 + d13 * d13 + d14 * d14 + d23 * d23 + d24 * d24 + d34 * d34 < 100 * (n * n) := by
  have key : d01 * d01 + d02 * d02 + d03 * d03 + d04 * d04 + d12 * d12 + d13 * d13 + d14 * d14 + d23 * d23 + d24 * d24 + d34 * d34 < (1 + 1) * n * ((1 + 1) * n) + (2 + 1) * n * ((2 + 1) * n) + (3 + 1) * n * ((3 + 1) * n) + (4 + 1) * n * ((4 + 1) * n) + (1 + 1) * n * ((1 + 1) * n) + (2 + 1) * n * ((2 + 1) * n) + (3 + 1) * n * ((3 + 1) * n) + (1 + 1) * n * ((1 + 1) * n) + (2 + 1) * n * ((2 + 1) * n)  -- (+36 chars; full line in the Zenodo source)
    exact (Nat.add_lt_add (Nat.add_lt_add (Nat.add_lt_add (Nat.add_lt_add (Nat.add_lt_add (Nat.add_lt_add (Nat.add_lt_add (Nat.add_lt_add (Nat.add_lt_add (sq_lt _ _ (pair_lt n 0 1 e0 e1 d01 he1 h01)) (sq_lt _ _ (pair_lt n 0 2 e0 e2 d02 he2 h02))) (sq_lt _ _ (pair_lt n 0 3 e0 e3 d03 he3 h03))) (sq_lt _ _ (pair_lt n 0 4 e0 e4 d04 he4 h04))) (sq_lt _ _ (pair_lt n 1 1 e1 e2 d12 he2 h12))) (sq_lt _ _ (  -- (+222 chars; full line in the Zenodo source)
  rw [show (1 + 1) * n * ((1 + 1) * n) + (2 + 1) * n * ((2 + 1) * n) + (3 + 1) * n * ((3 + 1) * n) + (4 + 1) * n * ((4 + 1) * n) + (1 + 1) * n * ((1 + 1) * n) + (2 + 1) * n * ((2 + 1) * n) + (3 + 1) * n * ((3 + 1) * n) + (1 + 1) * n * ((1 + 1) * n) + (2 + 1) * n * ((2 + 1) * n) + (1 + 1) * n * ((1 + 1) * n) = 100 * (n * n) by lib_nat] at key
  exact key

end Euc5Perm

#print axioms Euc5Perm.sq_lt
#print axioms Euc5Perm.pair_lt
#print axioms Euc5Perm.perm_sum

lang/Euc5Rep.lean

lean_all/lang/Euc5Rep.lean

/-
  lang/Euc5Rep — T49 (general α at ℓ = 5, step 4): **representatives after cutting at an empty fifth**. If no residue
  v < n lies in the fifth [jn/5, (j+1)n/5), the representative r = v (when 5v ≥ (j+1)n) or r = v + n (when
  5v < jn) satisfies 5r = (j+1)n + U with 0 ≤ U < 4n: all representatives lie in an interval of length < 4n/5.
    rep_right, rep_left
  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lib.Semi

namespace Euc5Rep

theorem rep_right (n j v : Nat) (hv : v < n) (h : (j + 1) * n ≤ 5 * v) : ∃ U, 5 * v = (j + 1) * n + U ∧ U < 4 * n :=
  match Nat.le.dest h with
  | ⟨U, hU⟩ => ⟨U, hU.symm, by
    have h5 : 5 * v + 5 ≤ 5 * n := Nat.mul_le_mul_left 5 hv
    have h1 : (j + 1) * n + U < 5 * n := by
      rw [hU]
      exact Nat.lt_of_lt_of_le (Nat.lt_add_of_pos_right (Nat.zero_lt_succ 4)) h5
    have h2 : n + U ≤ (j + 1) * n + U := Nat.add_le_add_right (by
      rw [show (j + 1) * n = j * n + n by lib_nat]
      exact Nat.le_add_left n (j * n)) U
    have h3 : n + U < n + 4 * n := by
      rw [show n + 4 * n = 5 * n by lib_nat]
      exact Nat.lt_of_le_of_lt h2 h1
    exact Nat.lt_of_add_lt_add_left h3⟩

theorem rep_left (n j v : Nat) (hj : j + 1 ≤ 5) (h : 5 * v < j * n) : ∃ U, 5 * (v + n) = (j + 1) * n + U ∧ U < 4 * n :=
  match Nat.le.dest hj with
  | ⟨d, hd⟩ => ⟨5 * v + d * n, by
      calc 5 * (v + n) = 5 * v + 5 * n := by lib_nat
        _ = 5 * v + (j + 1 + d) * n := by rw [hd]
        _ = (j + 1) * n + (5 * v + d * n) := by lib_nat, by
      have h4 : j + d = 4 := Nat.succ.inj ((show j + 1 + d = (j + d) + 1 by lib_nat).symm.trans hd)
      have h1 : 5 * v + d * n < j * n + d * n := Nat.add_lt_add_right h (d * n)
      rw [show j * n + d * n = (j + d) * n by lib_nat, h4] at h1
      exact h1⟩

end Euc5Rep

#print axioms Euc5Rep.rep_right
#print axioms Euc5Rep.rep_left

lang/Euc67.lean

lean_all/lang/Euc67.lean

/-
  lang/Euc67 — T48/T50: ℤ[θ], θ = (1 + √−67)/2, N(a + bθ) = a² + ab + 17b², 4N = (2a + b)² + 67b²: norm facts and Euclid's lemma.
  (The Dedekind–Hasse step is in DH67.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Ring67
import lang.GaussEuclid
import lang.W36Mul

namespace Euc67

open FTAChain DetChain LibInt


def en (z : Int × Int) : Int := z.1 * z.1 + z.1 * z.2 + 17 * (z.2 * z.2)
def nrm (z : Int × Int) : Nat := Int.natAbs (en z)
def cj (z : Int × Int) : Int × Int := (z.1 + z.2, -z.2)
def Dvd' (a b : Int × Int) : Prop := ∃ c, b = (Ring67.ring67 LibInt.intR).mul a c

theorem sgn (x : Int) : x = Int.ofNat (Int.natAbs x) ∨ x = -Int.ofNat (Int.natAbs x) := by
  cases x with
  | ofNat n => exact Or.inl rfl
  | negSucc n => exact Or.inr rfl

theorem le_dest (a b : Nat) (h : a ≤ b) : ∃ c, c + a = b := by
  induction h with
  | refl => exact ⟨0, Nat.zero_add a⟩
  | step _ ih =>
    obtain ⟨c, hc⟩ := ih
    exact ⟨c + 1, by rw [← hc]; lib_nat⟩

theorem ab_le (A B : Nat) : A * B ≤ A * A + 17 * (B * B) := by
  cases Nat.le_total A B with
  | inl h => exact Nat.le_trans (Nat.mul_le_mul_right B h) (Nat.le_trans (Nat.le_add_left _ (A * A)) (Nat.add_le_add_left (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ (Nat.le_add_left 1 16))) _))
  | inr h => exact Nat.le_trans (Nat.mul_le_mul_left A h) (Nat.le_add_right _ _)

theorem en_rep (z : Int × Int) : ∃ c : Nat, en z = Int.ofNat c := by
  generalize hA : Int.natAbs z.1 = A
  generalize hB : Int.natAbs z.2 = B
  obtain ⟨c, hc⟩ := le_dest _ _ (ab_le A B)
  have e : Int.ofNat c + Int.ofNat A * Int.ofNat B = Int.ofNat A * Int.ofNat A + 17 * (Int.ofNat B * Int.ofNat B) := congrArg Int.ofNat hc
  have opp : en z = Int.ofNat A * Int.ofNat A + 17 * (Int.ofNat B * Int.ofNat B) + -(Int.ofNat A * Int.ofNat B) → ∃ c : Nat, en z = Int.ofNat c :=
    fun h => ⟨c, by rw [h, ← e]; lib_int⟩
  have same : en z = Int.ofNat A * Int.ofNat A + 17 * (Int.ofNat B * Int.ofNat B) + Int.ofNat A * Int.ofNat B → ∃ c : Nat, en z = Int.ofNat c :=
    fun h => ⟨A * A + 17 * (B * B) + A * B, h⟩
  unfold en
  rcases sgn z.1 with h1 | h1 <;> rcases sgn z.2 with h2 | h2 <;> rw [hA] at h1 <;> rw [hB] at h2
  · exact same (by unfold en; rw [h1, h2]; lib_int)
  · exact opp (by unfold en; rw [h1, h2]; lib_int)
  · exact opp (by unfold en; rw [h1, h2]; lib_int)
  · exact same (by unfold en; rw [h1, h2]; lib_int)

theorem nrm_cast (z : Int × Int) : Int.ofNat (nrm z) = en z := by
  obtain ⟨c, hc⟩ := en_rep z
  unfold nrm; rw [hc]; rfl

theorem four_nrm (z : Int × Int) : 4 * nrm z = Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2) + 67 * (Int.natAbs z.2 * Int.natAbs z.2) := by
  apply Int.ofNat.inj
  show 4 * Int.ofNat (nrm z) = Int.ofNat (Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2)) + 67 * Int.ofNat (Int.natAbs z.2 * Int.natAbs z.2)
  rw [nrm_cast, GaussEuclid.nas, GaussEuclid.nas]; unfold en; lib_int

theorem mul_cj (z : Int × Int) : (Ring67.ring67 LibInt.intR).mul z (cj z) = (en z, 0) := by
  apply Prod.ext
  · show z.1 * (z.1 + z.2) + -(z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2) = z.1 * z.1 + z.1 * z.2 + 17 * (z.2 * z.2)
    lib_int
  · show z.1 * -z.2 + z.2 * (z.1 + z.2) + z.2 * -z.2 = 0
    lib_int

theorem en_mul (x y : Int × Int) : en ((Ring67.ring67 LibInt.intR).mul x y) = en x * en y := by
  show (x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2)) * (x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x  -- (+305 chars; full line in the Zenodo source)
      + 17 * ((x.1 * y.2 + x.2 * y.1 + x.2 * y.2) * (x.1 * y.2 + x.2 * y.1 + x.2 * y.2)) =
    (x.1 * x.1 + x.1 * x.2 + 17 * (x.2 * x.2)) * (y.1 * y.1 + y.1 * y.2 + 17 * (y.2 * y.2))
  lib_int

theorem en_cj (z : Int × Int) : en (cj z) = en z := by
  show (z.1 + z.2) * (z.1 + z.2) + (z.1 + z.2) * -z.2 + 17 * (-z.2 * -z.2) = z.1 * z.1 + z.1 * z.2 + 17 * (z.2 * z.2)
  lib_int

theorem nrm_zero (z : Int × Int) (h : nrm z = 0) : z = (Ring67.ring67 LibInt.intR).zero := by
  have h4 := four_nrm z
  rw [h] at h4
  have hB : Int.natAbs z.2 * Int.natAbs z.2 = 0 := by
    have : 67 * (Int.natAbs z.2 * Int.natAbs z.2) = 0 := Nat.le_antisymm (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_of_eq h4.symm)) (Nat.zero_le _)
    cases nat_mul_zero _ _ this with
    | inl e => exact Nat.noConfusion e
    | inr e => exact e
  have hT : Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2) = 0 := Nat.le_antisymm (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq h4.symm)) (Nat.zero_le _)
  have b0 : z.2 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ hB with | inl e => exact e | inr e => exact e)
  have t0 : 2 * z.1 + z.2 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ hT with | inl e => exact e | inr e => exact e)
  have a0 : z.1 = 0 := W36Mul.two_cancel_int z.1 0 (by rw [show 2 * z.1 = 2 * z.1 + z.2 by rw [b0]; lib_int, t0]; rfl)
  exact Prod.ext a0 b0

theorem egcd_base (α β : Int × Int) (hb : β = (Ring67.ring67 LibInt.intR).zero) :
    ∃ d x y, d = (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul x α) ((Ring67.ring67 LibInt.intR).mul y β) ∧ Dvd' d α ∧ Dvd' d β :=
  ⟨α, (Ring67.ring67 LibInt.intR).one, (Ring67.ring67 LibInt.intR).zero, by rw [hb]; lib_ring, ⟨(Ring67.ring67 LibInt.intR).one, by lib_ring⟩, ⟨(Ring67.ring67 LibInt.intR).zero, by rw [hb]; lib_ring⟩⟩

/-- **Euclid's lemma in ℤ[ω].** -/
theorem eeuclid (α β γ x y : Int × Int) (h1 : (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul x α) ((Ring67.ring67 LibInt.intR).mul y β) = (Ring67.ring67 LibInt.intR).one) (h : Dvd' α ((Ring67.ring67 LibInt.intR).mul β γ)) : Dvd' α γ :=
  match h with
  | ⟨c, hc⟩ => ⟨(Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul x γ) ((Ring67.ring67 LibInt.intR).mul y c), by
      calc γ = (Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul x α) ((Ring67.ring67 LibInt.intR).mul y β)) γ := by rw [h1]; lib_ring
        _ = (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul x α) γ) ((Ring67.ring67 LibInt.intR).mul y ((Ring67.ring67 LibInt.intR).mul β γ)) := by lib_ring
        _ = (Ring67.ring67 LibInt.intR).mul α ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul x γ) ((Ring67.ring67 LibInt.intR).mul y c)) := by rw [hc]; lib_ring⟩

theorem nrm_lt (z : Int × Int) (N : Nat) (h : Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2) + 67 * (Int.natAbs z.2 * Int.natAbs z.2) < 4 * (N * N)) :
    nrm z < N * N := by
  cases Nat.lt_or_ge (nrm z) (N * N) with
  | inl h' => exact h'
  | inr h' =>
    exfalso
    rw [← four_nrm] at h
    exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le h (Nat.mul_le_mul_left 4 h'))

theorem sq_le (T N : Nat) (h : T ≤ N) : T * T ≤ N * N := Nat.mul_le_mul h h


end Euc67

#print axioms Euc67.sgn
#print axioms Euc67.le_dest
#print axioms Euc67.ab_le
#print axioms Euc67.en_rep
#print axioms Euc67.nrm_cast
#print axioms Euc67.four_nrm
#print axioms Euc67.mul_cj
#print axioms Euc67.en_mul
#print axioms Euc67.en_cj
#print axioms Euc67.nrm_zero
#print axioms Euc67.egcd_base
#print axioms Euc67.nrm_lt
#print axioms Euc67.sq_le
#print axioms Euc67.eeuclid

lang/Euc7.lean

lean_all/lang/Euc7.lean

/-
  lang/Euc7 — T48/T50, cycle 638: ℤ[θ], θ = (1 + √−7)/2, is Euclidean.
  N(a + bθ) = a² + ab + 2b², 4N = (2a + b)² + 7b², conjugate (a + b) − bθ:
    en_rep, nrm_cast, en_mul, en_cj, four_nrm      norm facts (4·nrm = |2a + b|² + 7|b|² in ℕ)
    edivr          β ≠ 0: α = qβ + ρ, nrm ρ < nrm β  — two-step rounding: r₂ by N, then 2r₁ + r₂ by 2N, 16N(r) ≤ 11N²
    egcd_exists, eeuclid
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Ring7
import lang.GaussEuclid
import lang.W36Mul

namespace Euc7

open FTAChain DetChain LibInt


def en (z : Int × Int) : Int := z.1 * z.1 + z.1 * z.2 + 2 * (z.2 * z.2)
def nrm (z : Int × Int) : Nat := Int.natAbs (en z)
def cj (z : Int × Int) : Int × Int := (z.1 + z.2, -z.2)
def Dvd' (a b : Int × Int) : Prop := ∃ c, b = (Ring7.ring7 LibInt.intR).mul a c

theorem sgn (x : Int) : x = Int.ofNat (Int.natAbs x) ∨ x = -Int.ofNat (Int.natAbs x) := by
  cases x with
  | ofNat n => exact Or.inl rfl
  | negSucc n => exact Or.inr rfl

theorem le_dest (a b : Nat) (h : a ≤ b) : ∃ c, c + a = b := by
  induction h with
  | refl => exact ⟨0, Nat.zero_add a⟩
  | step _ ih =>
    obtain ⟨c, hc⟩ := ih
    exact ⟨c + 1, by rw [← hc]; lib_nat⟩

theorem ab_le (A B : Nat) : A * B ≤ A * A + 2 * (B * B) := by
  cases Nat.le_total A B with
  | inl h => exact Nat.le_trans (Nat.mul_le_mul_right B h) (Nat.le_trans (Nat.le_add_left _ (A * A)) (Nat.add_le_add_left (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ (Nat.le_succ 1))) _))
  | inr h => exact Nat.le_trans (Nat.mul_le_mul_left A h) (Nat.le_add_right _ _)

theorem en_rep (z : Int × Int) : ∃ c : Nat, en z = Int.ofNat c := by
  generalize hA : Int.natAbs z.1 = A
  generalize hB : Int.natAbs z.2 = B
  obtain ⟨c, hc⟩ := le_dest _ _ (ab_le A B)
  have e : Int.ofNat c + Int.ofNat A * Int.ofNat B = Int.ofNat A * Int.ofNat A + 2 * (Int.ofNat B * Int.ofNat B) := congrArg Int.ofNat hc
  have opp : en z = Int.ofNat A * Int.ofNat A + 2 * (Int.ofNat B * Int.ofNat B) + -(Int.ofNat A * Int.ofNat B) → ∃ c : Nat, en z = Int.ofNat c :=
    fun h => ⟨c, by rw [h, ← e]; lib_int⟩
  have same : en z = Int.ofNat A * Int.ofNat A + 2 * (Int.ofNat B * Int.ofNat B) + Int.ofNat A * Int.ofNat B → ∃ c : Nat, en z = Int.ofNat c :=
    fun h => ⟨A * A + 2 * (B * B) + A * B, h⟩
  unfold en
  rcases sgn z.1 with h1 | h1 <;> rcases sgn z.2 with h2 | h2 <;> rw [hA] at h1 <;> rw [hB] at h2
  · exact same (by unfold en; rw [h1, h2]; lib_int)
  · exact opp (by unfold en; rw [h1, h2]; lib_int)
  · exact opp (by unfold en; rw [h1, h2]; lib_int)
  · exact same (by unfold en; rw [h1, h2]; lib_int)

theorem nrm_cast (z : Int × Int) : Int.ofNat (nrm z) = en z := by
  obtain ⟨c, hc⟩ := en_rep z
  unfold nrm; rw [hc]; rfl

theorem four_nrm (z : Int × Int) : 4 * nrm z = Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2) + 7 * (Int.natAbs z.2 * Int.natAbs z.2) := by
  apply Int.ofNat.inj
  show 4 * Int.ofNat (nrm z) = Int.ofNat (Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2)) + 7 * Int.ofNat (Int.natAbs z.2 * Int.natAbs z.2)
  rw [nrm_cast, GaussEuclid.nas, GaussEuclid.nas]; unfold en; lib_int

theorem mul_cj (z : Int × Int) : (Ring7.ring7 LibInt.intR).mul z (cj z) = (en z, 0) := by
  apply Prod.ext
  · show z.1 * (z.1 + z.2) + -(z.2 * -z.2 + z.2 * -z.2) = z.1 * z.1 + z.1 * z.2 + 2 * (z.2 * z.2)
    lib_int
  · show z.1 * -z.2 + z.2 * (z.1 + z.2) + z.2 * -z.2 = 0
    lib_int

theorem en_mul (x y : Int × Int) : en ((Ring7.ring7 LibInt.intR).mul x y) = en x * en y := by
  show (x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2)) * (x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2)) + (x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2)) * (x.1 * y.2 + x.2 * y.1 + x.2 * y.2)
      + 2 * ((x.1 * y.2 + x.2 * y.1 + x.2 * y.2) * (x.1 * y.2 + x.2 * y.1 + x.2 * y.2)) =
    (x.1 * x.1 + x.1 * x.2 + 2 * (x.2 * x.2)) * (y.1 * y.1 + y.1 * y.2 + 2 * (y.2 * y.2))
  lib_int

theorem en_cj (z : Int × Int) : en (cj z) = en z := by
  show (z.1 + z.2) * (z.1 + z.2) + (z.1 + z.2) * -z.2 + 2 * (-z.2 * -z.2) = z.1 * z.1 + z.1 * z.2 + 2 * (z.2 * z.2)
  lib_int

/-- **Division with remainder in ℤ[θ].** -/
theorem edivr (α β : Int × Int) (M : Nat) (hβ : nrm β = M + 1) :
    ∃ q, nrm ((Ring7.ring7 LibInt.intR).add α ((Ring7.ring7 LibInt.intR).neg ((Ring7.ring7 LibInt.intR).mul q β))) < M + 1 := by
  have hN : en β = Int.ofNat (M + 1) := by rw [← nrm_cast, hβ]
  match GaussEuclid.rnd ((Ring7.ring7 LibInt.intR).mul α (cj β)).2 M with
  | ⟨q2, r2, h2, b2⟩ =>
  match GaussEuclid.rnd (2 * ((Ring7.ring7 LibInt.intR).mul α (cj β)).1 + r2) (2 * M + 1) with
  | ⟨q1, t, h1, b1⟩ =>
    refine ⟨(q1, q2), ?_⟩
    generalize hρ : (Ring7.ring7 LibInt.intR).add α ((Ring7.ring7 LibInt.intR).neg ((Ring7.ring7 LibInt.intR).mul (q1, q2) β)) = ρ
    have hρc : (Ring7.ring7 LibInt.intR).mul ρ (cj β) = (((Ring7.ring7 LibInt.intR).mul α (cj β)).1 + -(q1 * Int.ofNat (M + 1)), r2) := by
      rw [← hρ]
      have e : (Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).add α ((Ring7.ring7 LibInt.intR).neg ((Ring7.ring7 LibInt.intR).mul (q1, q2) β))) (cj β) = (Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul α (cj β)) ((Ring7.ring7 LibInt.intR).neg ((Ring7.ring7 LibInt.intR).mul (q1, q2) ((Ring7.ring7 LibInt.intR).mul β (cj β)))) := by lib_ring
      rw [e, mul_cj, hN]
      apply Prod.ext
      · show ((Ring7.ring7 LibInt.intR).mul α (cj β)).1 + -(q1 * Int.ofNat (M + 1) + -(q2 * 0 + q2 * 0)) = ((Ring7.ring7 LibInt.intR).mul α (cj β)).1 + -(q1 * Int.ofNat (M + 1))
        lib_int
      · show ((Ring7.ring7 LibInt.intR).mul α (cj β)).2 + -(q1 * 0 + q2 * Int.ofNat (M + 1) + q2 * 0) = r2
        rw [h2]; lib_int
    generalize hr1 : ((Ring7.ring7 LibInt.intR).mul α (cj β)).1 + -(q1 * Int.ofNat (M + 1)) = r1 at hρc
    have ht : 2 * r1 + r2 = t := by
      rw [← hr1]
      calc 2 * (((Ring7.ring7 LibInt.intR).mul α (cj β)).1 + -(q1 * Int.ofNat (M + 1))) + r2 = (2 * ((Ring7.ring7 LibInt.intR).mul α (cj β)).1 + r2) + -(q1 * Int.ofNat (2 * M + 1 + 1)) := by
            show _ = _ + -(q1 * (2 * Int.ofNat M + 1 + 1)); show 2 * (_ + -(q1 * (Int.ofNat M + 1))) + r2 = _; lib_int
        _ = t := by rw [h1]; lib_int
    have hn : en ρ * Int.ofNat (M + 1) = en (r1, r2) := by
      rw [← hN, ← en_cj β, ← en_mul, hρc]
    have hnN : nrm ρ * (M + 1) = nrm (r1, r2) := by
      apply Int.ofNat.inj
      show Int.ofNat (nrm ρ) * Int.ofNat (M + 1) = Int.ofNat (nrm (r1, r2))
      rw [nrm_cast, nrm_cast]; exact hn
    have h4 := four_nrm (r1, r2)
    have hT : Int.natAbs (2 * r1 + r2) ≤ M + 1 := by
      rw [ht]
      apply Nat.le_of_mul_le_mul_left (show 2 * Int.natAbs t ≤ 2 * (M + 1) from Nat.le_trans b1 (Nat.le_of_eq (by lib_nat))) (Nat.zero_lt_succ 1)
    generalize Int.natAbs (2 * r1 + r2) = T at h4 hT
    have h4' : 4 * nrm (r1, r2) = T * T + 7 * (Int.natAbs r2 * Int.natAbs r2) := h4
    generalize Int.natAbs r2 = B at h4' b2
    have hle : (M + 1) * (16 * nrm ρ) ≤ (M + 1) * (11 * (M + 1)) := by
      calc (M + 1) * (16 * nrm ρ) = 4 * (4 * nrm (r1, r2)) := by rw [← hnN]; lib_nat
        _ = 4 * (T * T) + 7 * ((2 * B) * (2 * B)) := by rw [h4']; lib_nat
        _ ≤ 4 * ((M + 1) * (M + 1)) + 7 * ((M + 1) * (M + 1)) := Nat.add_le_add (Nat.mul_le_mul_left 4 (Nat.mul_le_mul hT hT)) (Nat.mul_le_mul_left 7 (Nat.mul_le_mul b2 b2))
        _ = (M + 1) * (11 * (M + 1)) := by lib_nat
    have h16 := Nat.le_of_mul_le_mul_left hle (Nat.zero_lt_succ M)
    cases Nat.lt_or_ge (nrm ρ) (M + 1) with
    | inl h => exact h
    | inr h =>
      exfalso
      have : 16 * (M + 1) ≤ 11 * (M + 1) := Nat.le_trans (Nat.mul_le_mul_left 16 h) h16
      exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (show 11 * (M + 1) < 16 * (M + 1) by
        rw [show 16 * (M + 1) = 11 * (M + 1) + (5 * M + 5) by lib_nat]; exact Nat.lt_add_of_pos_right (Nat.zero_lt_succ _)) this)

theorem nrm_zero (z : Int × Int) (h : nrm z = 0) : z = (Ring7.ring7 LibInt.intR).zero := by
  have h4 := four_nrm z
  rw [h] at h4
  have hB : Int.natAbs z.2 * Int.natAbs z.2 = 0 := by
    have : 7 * (Int.natAbs z.2 * Int.natAbs z.2) = 0 := Nat.le_antisymm (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_of_eq h4.symm)) (Nat.zero_le _)
    cases nat_mul_zero _ _ this with
    | inl e => exact Nat.noConfusion e
    | inr e => exact e
  have hT : Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2) = 0 := Nat.le_antisymm (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq h4.symm)) (Nat.zero_le _)
  have b0 : z.2 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ hB with | inl e => exact e | inr e => exact e)
  have t0 : 2 * z.1 + z.2 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ hT with | inl e => exact e | inr e => exact e)
  have a0 : z.1 = 0 := W36Mul.two_cancel_int z.1 0 (by rw [show 2 * z.1 = 2 * z.1 + z.2 by rw [b0]; lib_int, t0]; rfl)
  exact Prod.ext a0 b0

theorem egcd_base (α β : Int × Int) (hb : β = (Ring7.ring7 LibInt.intR).zero) :
    ∃ d x y, d = (Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul x α) ((Ring7.ring7 LibInt.intR).mul y β) ∧ Dvd' d α ∧ Dvd' d β :=
  ⟨α, (Ring7.ring7 LibInt.intR).one, (Ring7.ring7 LibInt.intR).zero, by rw [hb]; lib_ring, ⟨(Ring7.ring7 LibInt.intR).one, by lib_ring⟩, ⟨(Ring7.ring7 LibInt.intR).zero, by rw [hb]; lib_ring⟩⟩

/-- **gcd as a combination in ℤ[ω].** -/
theorem egcd_exists : ∀ G (α β : Int × Int), nrm β ≤ G →
    ∃ d x y, d = (Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul x α) ((Ring7.ring7 LibInt.intR).mul y β) ∧ Dvd' d α ∧ Dvd' d β
  | 0, α, β, hG => egcd_base α β (nrm_zero β (Nat.le_antisymm hG (Nat.zero_le _)))
  | G + 1, α, β, hG => by
    cases h : nrm β with
    | zero => exact egcd_base α β (nrm_zero β h)
    | succ M =>
        match edivr α β M h with
        | ⟨q, hq⟩ =>
          have hρ : nrm ((Ring7.ring7 LibInt.intR).add α ((Ring7.ring7 LibInt.intR).neg ((Ring7.ring7 LibInt.intR).mul q β))) ≤ G :=
            Nat.le_of_lt_succ (Nat.lt_of_lt_of_le hq (Nat.le_trans (Nat.le_of_eq h.symm) hG))
          match egcd_exists G β _ hρ with
          | ⟨d, x, y, hd, ⟨c1, hc1⟩, ⟨c2, hc2⟩⟩ =>
            refine ⟨d, y, (Ring7.ring7 LibInt.intR).add x ((Ring7.ring7 LibInt.intR).neg ((Ring7.ring7 LibInt.intR).mul y q)), by rw [hd]; lib_ring, ⟨(Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul q c1) c2, ?_⟩, ⟨c1, hc1⟩⟩
            have e : α = (Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul q β) ((Ring7.ring7 LibInt.intR).add α ((Ring7.ring7 LibInt.intR).neg ((Ring7.ring7 LibInt.intR).mul q β))) := by lib_ring
            rw [e, hc2, hc1]
            lib_ring

/-- **Euclid's lemma in ℤ[ω].** -/
theorem eeuclid (α β γ x y : Int × Int) (h1 : (Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul x α) ((Ring7.ring7 LibInt.intR).mul y β) = (Ring7.ring7 LibInt.intR).one) (h : Dvd' α ((Ring7.ring7 LibInt.intR).mul β γ)) : Dvd' α γ :=
  match h with
  | ⟨c, hc⟩ => ⟨(Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul x γ) ((Ring7.ring7 LibInt.intR).mul y c), by
      calc γ = (Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul x α) ((Ring7.ring7 LibInt.intR).mul y β)) γ := by rw [h1]; lib_ring
        _ = (Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul x α) γ) ((Ring7.ring7 LibInt.intR).mul y ((Ring7.ring7 LibInt.intR).mul β γ)) := by lib_ring
        _ = (Ring7.ring7 LibInt.intR).mul α ((Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul x γ) ((Ring7.ring7 LibInt.intR).mul y c)) := by rw [hc]; lib_ring⟩

end Euc7

#print axioms Euc7.sgn
#print axioms Euc7.le_dest
#print axioms Euc7.ab_le
#print axioms Euc7.en_rep
#print axioms Euc7.nrm_cast
#print axioms Euc7.four_nrm
#print axioms Euc7.mul_cj
#print axioms Euc7.en_mul
#print axioms Euc7.en_cj
#print axioms Euc7.edivr
#print axioms Euc7.nrm_zero
#print axioms Euc7.egcd_base
#print axioms Euc7.egcd_exists
#print axioms Euc7.eeuclid

lang/Euler.lean

lean_all/lang/Euler.lean

/-
  lang/Euler — T43, cycle 17 (GL1 over ℚ, quadratic characters): Euler's criterion, the easy half.

  p = 2m + 3 odd prime, ℤ/p (lang/ZMod), x^(p−1) = 1 for x ≠ 0.
    sq_one          y² = 1 ⇒ y = 1 ∨ y = −1   (no zero divisors)
    euler_pm        x ≠ 0 ⇒ x^((p−1)/2) = 1 ∨ x^((p−1)/2) = −1
    euler_square    x = y², y ≠ 0 ⇒ x^((p−1)/2) = 1
    legendre_mul    (xy)^((p−1)/2) = x^((p−1)/2) y^((p−1)/2)
  So χ(x) = x^((p−1)/2) is a ±1-valued character of (ℤ/p)^×, trivial on squares — the Hecke character of
  the quadratic field ℚ(√(±p)) read on ℤ/p.

  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.ZMod
import lang.TwoPower

namespace Euler

open FTAChain ZMod TwoPower

variable {R : Type} (K : DetChain.CRing R)

theorem sq_one (hK : DetChain.NoZD K) (y : R) (h : K.mul y y = K.one) : y = K.one ∨ y = K.neg K.one := by
  -- (y − 1)(y + 1) = y² − 1 = 0
  have e : K.mul (K.add y (K.neg K.one)) (K.add y K.one) = K.zero := by
    rw [K.distrib, DetChain.right_distrib, DetChain.right_distrib, h, K.mul_comm y K.one, K.one_mul,
      DetChain.neg_mul_left, DetChain.neg_mul_left, K.one_mul, K.one_mul]
    rw [K.add_assoc, ← K.add_assoc (K.neg y) y (K.neg K.one), DetChain.add_neg' K y, K.zero_add, K.add_neg]
  cases hK _ _ e with
  | inl h1 =>
    left
    have := congrArg (fun z => K.add z K.one) h1
    have h' : K.add (K.add y (K.neg K.one)) K.one = K.add K.zero K.one := this
    rw [K.add_assoc, DetChain.add_neg' K K.one, DetChain.add_zero', K.zero_add] at h'
    exact h'
  | inr h2 =>
    right
    exact DetChain.neg_unique K _ _ h2

variable (m : Nat)

/-- **Euler, ±1.** `p = 2m + 3`; `x^(m+1)` squares to `x^(p−1) = 1`. -/
theorem euler_pm (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1)
    (x : Zp (2 * m + 1 + 1)) (hx : x ≠ (ZMod.ring (2 * m + 1 + 1)).zero) :
    Frobenius.pw (ZMod.ring (2 * m + 1 + 1)) x (m + 1) = (ZMod.ring (2 * m + 1 + 1)).one ∨
    Frobenius.pw (ZMod.ring (2 * m + 1 + 1)) x (m + 1) = (ZMod.ring (2 * m + 1 + 1)).neg (ZMod.ring (2 * m + 1 + 1)).one := by
  apply sq_one (ZMod.ring (2 * m + 1 + 1)) (nozd (2 * m + 1) hpr)
  have hu := unit_pow (2 * m + 1) hpr x hx
  rw [← LinkIntegralZN.pw_add (ZMod.ring (2 * m + 1 + 1)) x (m + 1) (m + 1)]
  have e : m + 1 + (m + 1) = 2 * m + 1 + 1 := by
    have h2 : 2 * m = m + m := by rw [nmul_comm 2 m]; show m * 1 + m = m + m; rw [nmul_one]
    rw [h2, ← Nat.add_assoc (m + 1) m 1, Nat.add_right_comm m 1 m]
  rw [e]
  exact hu

/-- **Squares are quadratic residues.** -/
theorem euler_square (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1)
    (y : Zp (2 * m + 1 + 1)) (hy : y ≠ (ZMod.ring (2 * m + 1 + 1)).zero) :
    Frobenius.pw (ZMod.ring (2 * m + 1 + 1)) ((ZMod.ring (2 * m + 1 + 1)).mul y y) (m + 1) = (ZMod.ring (2 * m + 1 + 1)).one := by
  rw [Frobenius.pw_mul, ← LinkIntegralZN.pw_add (ZMod.ring (2 * m + 1 + 1)) y (m + 1) (m + 1)]
  have e : m + 1 + (m + 1) = 2 * m + 1 + 1 := by
    have h2 : 2 * m = m + m := by rw [nmul_comm 2 m]; show m * 1 + m = m + m; rw [nmul_one]
    rw [h2, ← Nat.add_assoc (m + 1) m 1, Nat.add_right_comm m 1 m]
  rw [e]
  exact unit_pow (2 * m + 1) hpr y hy

/-- **The Legendre symbol is multiplicative.** -/
theorem legendre_mul (x y : Zp (2 * m + 1 + 1)) :
    Frobenius.pw (ZMod.ring (2 * m + 1 + 1)) ((ZMod.ring (2 * m + 1 + 1)).mul x y) (m + 1)
      = (ZMod.ring (2 * m + 1 + 1)).mul (Frobenius.pw (ZMod.ring (2 * m + 1 + 1)) x (m + 1))
          (Frobenius.pw (ZMod.ring (2 * m + 1 + 1)) y (m + 1)) :=
  Frobenius.pw_mul _ x y (m + 1)

end Euler

#print axioms Euler.sq_one
#print axioms Euler.euler_pm
#print axioms Euler.euler_square
#print axioms Euler.legendre_mul

lang/EulerHard.lean

lean_all/lang/EulerHard.lean

/-
  lang/EulerHard — T43, cycle 19: Euler's criterion, the hard half.  p = 2m + 3 prime, ℤ/p.

    ev_binom        the coefficient list [1, 0, …, 0, −1] (length m + 2) evaluates to x^(m+1) − 1
    mk_cancel       mk a + mk d = mk a ⇒ p | d
    squares_dist    the residues of 1², 2², …, (m+1)² are pairwise distinct
    euler_hard      x^(m+1) = 1 ⇒ x is a square
  Proof: the m + 1 distinct squares are roots of X^(m+1) − 1 (Euler.euler_square); a non-square root would give
  m + 2 distinct roots of a polynomial of degree m + 1 (RootBound.root_bound).

  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.Euler
import lang.RootBound
import lang.PrimeOneModQ

namespace EulerHard

open FTAChain ZMod TwoPower RootBound

/-- `[1, 0, …, 0, −1]` with `k` zeros. -/
def binomL {R : Type} (K : DetChain.CRing R) (k : Nat) : List R := K.one :: zerosThen K k

where
  zerosThen {R : Type} (K : DetChain.CRing R) : Nat → List R
    | 0 => [K.neg K.one]
    | k + 1 => K.zero :: zerosThen K k

theorem len_zerosThen {R : Type} (K : DetChain.CRing R) : ∀ k, (binomL.zerosThen K k).length = k + 1
  | 0 => rfl
  | k + 1 => congrArg Nat.succ (len_zerosThen K k)

theorem ev_zerosThen {R : Type} (K : DetChain.CRing R) (x : R) : ∀ k, evH K x (binomL.zerosThen K k) = K.neg K.one
  | 0 => by
    show K.add (K.mul (K.neg K.one) K.one) K.zero = K.neg K.one
    rw [DetChain.add_zero', K.mul_comm, K.one_mul]
  | k + 1 => by
    show K.add (K.mul K.zero _) (evH K x (binomL.zerosThen K k)) = K.neg K.one
    rw [DetChain.zero_mul', K.zero_add, ev_zerosThen K x k]

/-- `evH [1, 0, …, 0, −1] x = x^(k+1) − 1`. -/
theorem ev_binom {R : Type} (K : DetChain.CRing R) (x : R) (k : Nat) :
    evH K x (binomL K k) = K.add (Frobenius.pw K x (k + 1)) (K.neg K.one) := by
  show K.add (K.mul K.one (Frobenius.pw K x (binomL.zerosThen K k).length)) (evH K x (binomL.zerosThen K k)) = _
  rw [len_zerosThen, K.one_mul, ev_zerosThen]

theorem mk_cancel (m a d : Nat) (h : (ZMod.ring m).add (mk m a) (mk m d) = mk m a) : Dv (m + 1) d := by
  have e : (ZMod.ring m).add (mk m a) (mk m d) = (ZMod.ring m).add (mk m a) (ZMod.ring m).zero := by
    rw [h, DetChain.add_zero']
  have := DetChain.add_cancel_left (ZMod.ring m) _ _ _ e
  exact md_zero_dv m d (congrArg Subtype.val this)

theorem sq_expand (y e : Nat) : (y + e) * (y + e) = y * y + e * (y + (y + e)) := by
  rw [nleft_distrib, nright_distrib, nright_distrib, nleft_distrib e y (y + e), nleft_distrib e y e,
    nmul_comm y e, nadd_assoc]

variable (m : Nat)

theorem no_dvd (P n : Nat) (h0 : 0 < n) (hlt : n < P) : ¬ Dv P n := by
  intro hd
  cases n with
  | zero => exact Nat.lt_irrefl 0 h0
  | succ j =>
    obtain ⟨k, hk⟩ := dv_le P j hd
    exact Nat.not_le_of_lt hlt (Nat.le.intro hk)

theorem two_m (m : Nat) : 2 * m = m + m := by
  rw [nmul_comm 2 m]; show m * 1 + m = m + m; rw [nmul_one]

/-- **Distinct half-squares.** `1 ≤ y`, `1 ≤ e`, `y + e ≤ m + 1` ⇒ `y² ≢ (y+e)² (mod 2m + 3)`. -/
theorem sq_ne (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1)
    (y e : Nat) (hy : 1 ≤ y) (he : 1 ≤ e) (hz : y + e ≤ m + 1) :
    mk (2 * m + 1 + 1) (y * y) ≠ mk (2 * m + 1 + 1) ((y + e) * (y + e)) := by
  intro h
  rw [sq_expand, ← PrimeOneModQ.mk_add] at h
  have hd := mk_cancel (2 * m + 1 + 1) (y * y) (e * (y + (y + e))) h.symm
  have hP : 2 * m + 1 + 1 + 1 = m + (m + 1) + 1 + 1 := by rw [two_m]; rfl
  -- e ≤ m
  have hem : e < 2 * m + 1 + 1 + 1 := by
    have h1 : e + 1 ≤ m + 1 := Nat.le_trans (by rw [Nat.add_comm e 1]; exact Nat.add_le_add_right hy e) hz
    rw [hP]
    exact Nat.lt_of_le_of_lt (Nat.le_of_succ_le_succ h1)
      (Nat.lt_succ_of_le (Nat.le_succ_of_le (Nat.le_add_right m (m + 1))))
  -- y + (y + e) ≤ m + (m + 1)
  have hyz : y + (y + e) < 2 * m + 1 + 1 + 1 := by
    have h2 : y + 1 ≤ m + 1 := Nat.le_trans (Nat.add_le_add_left he y) hz
    have h3 : y + (y + e) ≤ m + (m + 1) := Nat.add_le_add (Nat.le_of_succ_le_succ h2) hz
    rw [hP]
    exact Nat.lt_succ_of_le (Nat.le_succ_of_le h3)
  obtain ⟨e', he'⟩ := Nat.le.dest he
  have he2 : e = e' + 1 := by rw [← he', Nat.add_comm]
  rw [he2] at hd
  cases euclid_core (2 * m + 1) e' (y + (y + (e' + 1))) hpr hd with
  | inl h1 => exact no_dvd _ (e' + 1) (Nat.zero_lt_succ e') (he2 ▸ hem) h1
  | inr h2 =>
    exact no_dvd _ (y + (y + (e' + 1))) (Nat.lt_of_lt_of_le (Nat.zero_lt_succ e')
      (Nat.le_trans (Nat.le_add_left (e' + 1) y) (Nat.le_add_left (y + (e' + 1)) y)))
      (he2 ▸ hyz) h2

/-- The residues of `1², …, k²`. -/
def sqs : Nat → List (Zp (2 * m + 1 + 1))
  | 0 => []
  | k + 1 => mk (2 * m + 1 + 1) ((k + 1) * (k + 1)) :: sqs k

theorem len_sqs : ∀ k, (sqs m k).length = k
  | 0 => rfl
  | k + 1 => congrArg Nat.succ (len_sqs k)

theorem mem_sqs : ∀ k s, YMChain.memL s (sqs m k) → ∃ y, 1 ≤ y ∧ y ≤ k ∧ s = mk (2 * m + 1 + 1) (y * y)
  | 0, _, h => absurd h (fun z => z)
  | k + 1, s, h => by
    cases h with
    | inl e => exact ⟨k + 1, Nat.succ_le_succ (Nat.zero_le k), Nat.le_refl _, e⟩
    | inr h' =>
      obtain ⟨y, h1, h2, h3⟩ := mem_sqs k s h'
      exact ⟨y, h1, Nat.le_succ_of_le h2, h3⟩

theorem dist_sqs (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) :
    ∀ k, k ≤ m + 1 → Dist (sqs m k)
  | 0, _ => trivial
  | k + 1, hk => by
    refine ⟨fun s hs => ?_, dist_sqs hpr k (Nat.le_of_succ_le hk)⟩
    obtain ⟨y, hy1, hyk, hs'⟩ := mem_sqs m k s hs
    rw [hs']
    obtain ⟨e, he⟩ := Nat.le.dest (Nat.succ_le_succ hyk)
    -- y + 1 + e = k + 1, so (k+1) = y + (e + 1)
    have hz : y + (e + 1) = k + 1 := by
      have he' : y + 1 + e = k + 1 := he
      rw [← he', Nat.add_right_comm, Nat.add_assoc]
    have hne := sq_ne m hpr y (e + 1) hy1 (Nat.succ_le_succ (Nat.zero_le e)) (by rw [hz]; exact hk)
    rw [hz] at hne
    exact hne

/-- `mk y ≠ 0` for `1 ≤ y ≤ m + 1`. -/
theorem mk_ne_zero (y : Nat) (h1 : 1 ≤ y) (h2 : y ≤ m + 1) : mk (2 * m + 1 + 1) y ≠ (ZMod.ring (2 * m + 1 + 1)).zero := by
  intro e
  have := congrArg Subtype.val e
  have hlt : y < 2 * m + 1 + 1 + 1 := by
    rw [two_m]
    exact Nat.lt_of_le_of_lt h2 (Nat.succ_lt_succ (Nat.lt_succ_of_le (Nat.le_succ_of_le (Nat.le_add_left m m))))
  rw [show (mk (2 * m + 1 + 1) y).val = y from md_small (2 * m + 1 + 1) y hlt] at this
  rw [this] at h1
  exact Nat.not_succ_le_zero 0 h1

/-- **Euler's criterion, hard half**: `x^((p−1)/2) = 1 ⇒ x` is a square (p = 2m + 3). -/
theorem euler_hard (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1)
    (x : Zp (2 * m + 1 + 1)) (hx : Frobenius.pw (ZMod.ring (2 * m + 1 + 1)) x (m + 1) = (ZMod.ring (2 * m + 1 + 1)).one) :
    ∃ y : Zp (2 * m + 1 + 1), (ZMod.ring (2 * m + 1 + 1)).mul y y = x := by
  -- is x among the half-squares?
  have decide_mem : ∀ l : List (Zp (2 * m + 1 + 1)), (∃ s, YMChain.memL s l ∧ s = x) ∨ (∀ s, YMChain.memL s l → s ≠ x) := by
    intro l
    induction l with
    | nil => exact Or.inr (fun s h => absurd h (fun z => z))
    | cons a l ih =>
      by_cases ha : a = x
      · exact Or.inl ⟨a, Or.inl rfl, ha⟩
      · cases ih with
        | inl h => obtain ⟨s, hs, e⟩ := h; exact Or.inl ⟨s, Or.inr hs, e⟩
        | inr h => exact Or.inr (fun s hs => by cases hs with
            | inl e => rw [e]; exact ha
            | inr h' => exact h s h')
  have root : ∀ s : Zp (2 * m + 1 + 1), Frobenius.pw (ZMod.ring (2 * m + 1 + 1)) s (m + 1) = (ZMod.ring (2 * m + 1 + 1)).one →
      evH (ZMod.ring (2 * m + 1 + 1)) s (binomL (ZMod.ring (2 * m + 1 + 1)) m) = (ZMod.ring (2 * m + 1 + 1)).zero := fun s hs => by
    rw [ev_binom, hs, (ZMod.ring (2 * m + 1 + 1)).add_neg]
  cases decide_mem (sqs m (m + 1)) with
  | inl h =>
    obtain ⟨s, hs, e⟩ := h
    obtain ⟨y, _, _, hy⟩ := mem_sqs m (m + 1) s hs
    exact ⟨mk (2 * m + 1 + 1) y, by rw [TwoPower.mk_mul, ← hy, e]⟩
  | inr h =>
    exfalso
    have hd : Dist (x :: sqs m (m + 1)) :=
      ⟨fun s hs => h s hs, dist_sqs m hpr (m + 1) (Nat.le_refl _)⟩
    have hroots : ∀ r, YMChain.memL r (x :: sqs m (m + 1)) →
        evH (ZMod.ring (2 * m + 1 + 1)) r (binomL (ZMod.ring (2 * m + 1 + 1)) m) = (ZMod.ring (2 * m + 1 + 1)).zero := by
      intro r hr
      cases hr with
      | inl e => rw [e]; exact root x hx
      | inr hr' =>
        obtain ⟨y, h1, h2, hy⟩ := mem_sqs m (m + 1) r hr'
        rw [hy, ← TwoPower.mk_mul]
        exact root _ (Euler.euler_square m hpr (mk (2 * m + 1 + 1) y) (mk_ne_zero m y h1 h2))
    have hone : (ZMod.ring (2 * m + 1 + 1)).one ≠ (ZMod.ring (2 * m + 1 + 1)).zero := fun e => by
      have := congrArg Subtype.val e
      rw [TwoPower.one_val] at this
      exact Nat.noConfusion this
    have := root_bound (ZMod.ring (2 * m + 1 + 1)) (nozd (2 * m + 1) hpr) (m + 1) (ZMod.ring (2 * m + 1 + 1)).one
      (binomL.zerosThen (ZMod.ring (2 * m + 1 + 1)) m) hone (len_zerosThen _ m) (x :: sqs m (m + 1)) hd hroots
    rw [show (x :: sqs m (m + 1)).length = m + 1 + 1 from congrArg Nat.succ (len_sqs m (m + 1))] at this
    exact Nat.not_succ_le_self (m + 1) this

end EulerHard

#print axioms EulerHard.len_zerosThen
#print axioms EulerHard.ev_zerosThen
#print axioms EulerHard.ev_binom
#print axioms EulerHard.mk_cancel
#print axioms EulerHard.sq_expand
#print axioms EulerHard.no_dvd
#print axioms EulerHard.two_m
#print axioms EulerHard.sq_ne
#print axioms EulerHard.len_sqs
#print axioms EulerHard.mem_sqs
#print axioms EulerHard.dist_sqs
#print axioms EulerHard.mk_ne_zero
#print axioms EulerHard.euler_hard

lang/EvalZero.lean

lean_all/lang/EvalZero.lean

/-
  lang/EvalZero — T49 (O3') (v4): **φ_z(A·σ_(−1)B) = 0 in ℤ/p^k when the valuations add up to k**:
  φ_z(A) = P^a U, φ_z(σ_(−1)B) = P^b U', a + b = k, P^k = 0 ⇒ φ_z(A·σ_(−1)B) = 0
  (CycEis.phz_mul, ValArith). With a = ℓ − 1 − c (ValC5) and b = c − 1 (bbar_val_test) this is hypothesis hW of StickCond.
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.ValArith
import lang.StickBB
import lang.CycEis

namespace EvalZero

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing

theorem eval_zero {S : Type} (E : CRing S) (m : Nat) (z : S) (hz : pw E z (m + 1) = E.one)
    (A B : V Int (m + 1)) (P U U' : S) (a b k : Nat)
    (hA : CycEis.phz E m z A = E.mul (pw E P a) U) (hB : CycEis.phz E m z (StickBB.J m B) = E.mul (pw E P b) U')
    (hk : a + b = k) (hP : pw E P k = E.zero) :
    CycEis.phz E m z (gmul LibInt.intR m A (StickBB.J m B)) = E.zero := by
  show CycEis.phz E m z ((gring LibInt.intR m).mul A (StickBB.J m B)) = E.zero
  rw [CycEis.phz_mul E m z hz, hA, hB, ValArith.val_mul, hk]
  exact ValArith.val_zero E P _ k k hP (Nat.le_refl k)

end EvalZero

#print axioms EvalZero.eval_zero

lang/Even32.lean

lean_all/lang/Even32.lean

/-
  lang/Even32 — T47, cycle 586: **the even coefficients of η(4z)²η(8z)² vanish**: [q^(2n)] = 0 for every n
  (a primary a + bi has a odd, b even, so its norm is odd; hence c_(2n) = 0). With Mult32/Rec32 the eigenvalue of T_2 is 0.
  Lean 4 core only, proofs by hand (lib_int, lib_ring), no axioms.
-/
import lang.Mult32

namespace Hecke32

open DetChain YMChain FiniteRecChain HeckeMult GaussEuclid GaussUnique GaussInt

theorem not_pd_even (n : Nat) (z : Int × Int) : ¬ PD (2 * n) z := fun h => by
  obtain ⟨w, c', ha, hb⟩ := HeckePrime.odd_a z.1 z.2 h.2
  have hn : z.1 * z.1 + z.2 * z.2 = Int.ofNat (2 * n) := h.1
  rw [ha, hb] at hn
  exact int_odd_ne_even (2 * w * w + 2 * w + 2 * c' * c') (Int.ofNat n) (by
    calc 2 * (2 * w * w + 2 * w + 2 * c' * c') + 1 = (2 * w + 1) * (2 * w + 1) + 2 * c' * (2 * c') := by lib_int
      _ = 2 * Int.ofNat n := by rw [hn]; rfl)

theorem c_even (n : Nat) : c (2 * n) = (Cyclo4.cyc4 Z).zero := by
  show sumK (Cyclo4.cyc4 Z) (fun z => (Cyclo4.cyc4 Z).mul (I (2 * n) z) z) (box (2 * n)) = _
  rw [sumK_ext (Cyclo4.cyc4 Z) _ (fun _ => (Cyclo4.cyc4 Z).zero) (fun z => by
    rw [I_neg _ z (not_pd_even n z)]; exact zero_mul' _ z)]
  exact HeckePrime.zsumR _ _

theorem etaC_even (n : Nat) : etaC (2 * n) = 0 := by
  rw [etaC_c, c_even]; rfl

end Hecke32

#print axioms Hecke32.not_pd_even
#print axioms Hecke32.c_even
#print axioms Hecke32.etaC_even

lang/Even36.lean

lean_all/lang/Even36.lean

/-
  lang/Even36 — T47, cycle 590: the even coefficients of η(6z)⁴ vanish: [q^(2t+2)]η(6z)⁴ = 0
  (every exponent 6E + 3(i² + i) is even, so none equals 2t + 1).
  Lean 4 core only, proofs by hand (lib_nat, lib_ring), no axioms.
-/
import lang.All36

namespace Theta36

open DetChain YMChain FiniteRecChain Hecke32 Hecke36

theorem even_ne_odd : ∀ a b : Nat, 2 * a ≠ 2 * b + 1
  | 0, _, h => Nat.noConfusion h
  | a + 1, 0, h => Nat.noConfusion (Nat.succ.inj ((show 2 * a + 1 + 1 = 2 * (a + 1) by lib_nat).trans h))
  | a + 1, b + 1, h => even_ne_odd a b (Nat.succ.inj (Nat.succ.inj ((by lib_nat : 2 * a + 1 + 1 = 2 * (a + 1)).trans (h.trans (by lib_nat)))))

theorem etaC36_even (t : Nat) : etaC36 (2 * t + 1 + 1) = 0 := by
  rw [eta36_sum]
  have hz : ∀ j i, T36 (2 * t + 1) j i = Z.zero := fun j i => by
    unfold T36
    rw [show delta Z (6 * QBinom.Gj g36 j + 3 * (i * i + i)) (2 * t + 1) = Z.zero by
      unfold delta; apply ite_neg'; intro h
      exact even_ne_odd (3 * QBinom.Gj g36 j + 3 * QBinom.tr2 i) t
        ((show 2 * (3 * QBinom.Gj g36 j + 3 * QBinom.tr2 i) = 6 * QBinom.Gj g36 j + 3 * (2 * QBinom.tr2 i) by lib_nat).trans
          (by rw [tr2_two]; exact h))]
    lib_ring
  rw [sumK_ext Z _ (fun _ => Z.zero) (fun j => by rw [sumK_ext Z _ (fun _ => Z.zero) (fun i => hz j i)]; exact HeckePrime.zsumR Z _),
    HeckePrime.zsumR Z]
  rfl

end Theta36

#print axioms Theta36.even_ne_odd
#print axioms Theta36.etaC36_even

lang/EveryPrime.lean

lean_all/lang/EveryPrime.lean

/-
  lang/EveryPrime — T43, cycle 106: a(𝔽_{p²}) = a_p² − 2p for y² = x³ − x at every odd prime p.

  One construction 𝔽_{p²} = 𝔽_p[√n] (any non-residue n, Fq2Inst.ffq) for every p = 2k + 3:
    nat_even_odd       k = 2j or k = 2j + 1
    every_prime        −Σ_{𝔽_{p²}} χ(z³ − z) = (Σ_{𝔽_p} χ(x³ − x))² − 2p
  (p ≡ 3 mod 4: InertFq; p ≡ 1 mod 4: HasseDavenport). With α + β = a_p, αβ = p this is a(𝔽_{p²}) = α² + β²: the second
  coefficient of the local zeta function of the CM curve at every odd prime is the one of its Hecke L-factor.

  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.InertFq

namespace EveryPrime

open FTAChain ZMod YMChain FiniteRecChain

theorem nat_even_odd : ∀ k : Nat, (∃ j, k = 2 * j) ∨ (∃ j, k = 2 * j + 1)
  | 0 => Or.inl ⟨0, rfl⟩
  | k + 1 => by
    cases nat_even_odd k with
    | inl h => match h with | ⟨j, hj⟩ => exact Or.inr ⟨j, by rw [hj]⟩
    | inr h => match h with | ⟨j, hj⟩ => exact Or.inl ⟨j + 1, by rw [hj]; lib_nat⟩

/-- **a(𝔽_{p²}) = a_p² − 2p for y² = x³ − x at every odd prime.** -/
theorem every_prime (k : Nat) (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = (2 * k + 1 + 1 + 1)) (n : Zp (2 * k + 1 + 1))
    (hn : Frobenius.pw (ring (2 * k + 1 + 1)) n (k + 1) = (ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) :
    -sumK LibInt.intR (fun x => FinField.chi (Fq2Inst.ffq k hpr n hn) (FinSign.f (Fq2Inst.ffq k hpr n hn) x)) (Fq2Inst.ffq k hpr n hn).els = (-sumK LibInt.intR (fun x => QChar.chi k (CMInert.cub (ring (2 * k + 1 + 1)) x)) (ConicSum.L k)) * (-sumK LibInt.intR (fun x => QChar.chi k (CMInert.cub (ring (2 * k + 1 + 1)) x)) (ConicSum.L k)) + -2 * Int.ofNat (2 * k + 1 + 1 + 1) := by
  cases nat_even_odd k with
  | inl h =>
    match h with
    | ⟨j, hj⟩ => subst hj; exact InertFq.inert_fq j hpr n hn
  | inr h =>
    match h with
    | ⟨j, hj⟩ => subst hj; exact HasseDavenport.hasse_davenport_split j hpr n hn

end EveryPrime

#print axioms EveryPrime.nat_even_odd
#print axioms EveryPrime.every_prime

lang/EveryPrime3.lean

lean_all/lang/EveryPrime3.lean

/-
  lang/EveryPrime3 — T43, cycle 107: a(𝔽_{p²}) = a_p² − 2p for y² = x³ + 1 at every prime p > 3.

  One model 𝔽_{p²} = 𝔽_p[√n] for p = 2k + 3, k ≥ 1:
    nat_mod3        k = 3t, 3t + 1 or 3t + 2
    every_prime3    −ψ_{𝔽_{p²}}(1) = ψ_{𝔽_p}(1)² − 2p
  (k = 3t: p = 3(2t + 1) is not prime for t ≥ 1; k = 3t + 1: p ≡ 2 (mod 3), CubicSuper2; k = 3t + 2: p ≡ 1 (mod 3),
  HasseDavenport3.)

  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.HasseDavenport3
import lang.EveryPrime

namespace EveryPrime3

open FTAChain ZMod YMChain FiniteRecChain

theorem nat_mod3 : ∀ k : Nat, (∃ t, k = 3 * t) ∨ (∃ t, k = 3 * t + 1) ∨ (∃ t, k = 3 * t + 2)
  | 0 => Or.inl ⟨0, rfl⟩
  | k + 1 => by
    cases nat_mod3 k with
    | inl h => match h with | ⟨t, ht⟩ => exact Or.inr (Or.inl ⟨t, by rw [ht]⟩)
    | inr h =>
      cases h with
      | inl h => match h with | ⟨t, ht⟩ => exact Or.inr (Or.inr ⟨t, by rw [ht]⟩)
      | inr h => match h with | ⟨t, ht⟩ => exact Or.inl ⟨t + 1, by rw [ht]; lib_nat⟩

/-- **a(𝔽_{p²}) = a_p² − 2p for y² = x³ + 1 at every prime p > 3.** -/
theorem every_prime3 (k : Nat) (hk : 1 ≤ k) (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = (2 * k + 1 + 1 + 1)) (n : Zp (2 * k + 1 + 1))
    (hn : Frobenius.pw (ring (2 * k + 1 + 1)) n (k + 1) = (ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) :
    -FinCubic.psi (Fq2Inst.ffq k hpr n hn) (Fq2Inst.ffq k hpr n hn).K.one = (-CubicJacob.psi k (ring (2 * k + 1 + 1)).one) * (-CubicJacob.psi k (ring (2 * k + 1 + 1)).one) + -2 * Int.ofNat (2 * k + 1 + 1 + 1) := by
  cases nat_mod3 k with
  | inl h =>
    exfalso
    match h with
    | ⟨t, ht⟩ =>
      cases hpr 3 ⟨2 * t + 1, by rw [ht]; lib_nat⟩ with
      | inl e => exact Nat.noConfusion (Nat.succ.inj (show (2 : Nat) + 1 = 0 + 1 from e))
      | inr e =>
        have e' : 2 * k = 0 := Nat.succ.inj (Nat.succ.inj (Nat.succ.inj e.symm))
        cases k with
        | zero => exact Nat.not_succ_le_zero 0 hk
        | succ k' => exact Nat.noConfusion (show 2 * k' + 1 + 1 = 0 from e')
  | inr h =>
    cases h with
    | inl h =>
      match h with
      | ⟨t, ht⟩ =>
        subst ht
        exact CubicSuper2.base_change2 (3 * t + 1) (2 * t + 2) (by lib_nat) hpr n hn
    | inr h =>
      match h with
      | ⟨t, ht⟩ => subst ht; exact HasseDavenport3.hasse_davenport_split3 t hpr n hn

end EveryPrime3

#print axioms EveryPrime3.nat_mod3
#print axioms EveryPrime3.every_prime3

lang/ExactOne.lean

lean_all/lang/ExactOne.lean

/-
  lang/ExactOne — T49 (general primary α, step (O1')): **a product equal to P with P² = 0 has exactly one factor
  divisible by P**. Abstract form over any commutative ring R with P·P = 0, P ≠ 0 and P not a unit, for a list of
  factors each flagged (d x = true ⇒ x ∈ P·R, d x = false ⇒ x a unit):
    pr, cnt          product and number of flagged factors
    pr_form          pr xs = P^(cnt xs)·w
    pr_unit          cnt xs = 0 ⇒ pr xs is a unit
    exact_one        pr xs = P ⇒ cnt xs = 1
  Use: R = ℤ/p², P = p, xs = (ι_c(α))_c with Π_c ι_c(α) = ι(N(α)) = p — exactly one P_c divides α, once
  (test: work/data/t49/normp_test.py).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.SymProdW

namespace ExactOne

open FTAChain YMChain FiniteRecChain

variable {R : Type} (K : DetChain.CRing R)

def pr : List R → R
  | [] => K.one
  | x :: xs => K.mul x (pr xs)

def cnt (d : R → Bool) : List R → Nat
  | [] => 0
  | x :: xs => (if d x = true then 1 else 0) + cnt d xs

variable (P : R) (d : R → Bool) (hP : ∀ x, d x = true → ∃ y, x = K.mul P y)
  (hU : ∀ x, d x = false → ∃ w, K.mul x w = K.one)

include hP in
theorem pr_form : ∀ xs : List R, ∃ w, pr K xs = K.mul (Frobenius.pw K P (cnt d xs)) w
  | [] => ⟨K.one, by show K.one = K.mul (Frobenius.pw K P 0) K.one; show K.one = K.mul K.one K.one; lib_ring⟩
  | x :: xs => by
    obtain ⟨w, hw⟩ := pr_form xs
    show ∃ w', K.mul x (pr K xs) = K.mul (Frobenius.pw K P ((if d x = true then 1 else 0) + cnt d xs)) w'
    rw [hw]
    cases hx : d x with
    | true =>
      obtain ⟨y, hy⟩ := hP x hx
      refine ⟨K.mul y w, ?_⟩
      have e : 1 + cnt d xs = cnt d xs + 1 := by lib_nat
      rw [ite_pos' _ _ rfl, e, SymProdW.pw_add, hy]
      show K.mul (K.mul P y) (K.mul (Frobenius.pw K P (cnt d xs)) w)
        = K.mul (K.mul (Frobenius.pw K P (cnt d xs)) (K.mul K.one P)) (K.mul y w)
      lib_ring
    | false =>
      refine ⟨K.mul x w, ?_⟩
      have e : 0 + cnt d xs = cnt d xs := by lib_nat
      rw [ite_neg' _ _ (fun h => Bool.noConfusion h), e]
      lib_ring

include hU in
theorem pr_unit : ∀ xs : List R, cnt d xs = 0 → ∃ w, K.mul (pr K xs) w = K.one
  | [], _ => ⟨K.one, by show K.mul K.one K.one = K.one; lib_ring⟩
  | x :: xs, h => by
    cases hx : d x with
    | true =>
      have h' : (if d x = true then 1 else 0) + cnt d xs = 0 := h
      rw [hx, ite_pos' _ _ rfl] at h'
      have e : cnt d xs + 1 = 0 := (show cnt d xs + 1 = 1 + cnt d xs by lib_nat).trans h'
      exact Nat.noConfusion e
    | false =>
      have h' : (if d x = true then 1 else 0) + cnt d xs = 0 := h
      have e : 0 + cnt d xs = cnt d xs := by lib_nat
      rw [hx, ite_neg' _ _ (fun h => Bool.noConfusion h), e] at h'
      obtain ⟨w, hw⟩ := pr_unit xs h'
      obtain ⟨u, hu⟩ := hU x hx
      refine ⟨K.mul w u, ?_⟩
      show K.mul (K.mul x (pr K xs)) (K.mul w u) = K.one
      calc K.mul (K.mul x (pr K xs)) (K.mul w u) = K.mul (K.mul (pr K xs) w) (K.mul x u) := by lib_ring
        _ = K.one := by rw [hw, hu]; lib_ring

include hP hU in
/-- **exactly one factor is divisible by P** when the product is P, P² = 0, P ≠ 0, P not a unit. -/
theorem exact_one (hPP : K.mul P P = K.zero) (hP0 : P ≠ K.zero) (hPu : ∀ w, K.mul P w ≠ K.one)
    (xs : List R) (hx : pr K xs = P) : cnt d xs = 1 := by
  obtain ⟨w, hw⟩ := pr_form K P d hP xs
  match hc : cnt d xs with
  | 0 =>
    obtain ⟨u, hu⟩ := pr_unit K d hU xs hc
    rw [hx] at hu
    exact absurd hu (hPu u)
  | 1 => rfl
  | c + 2 =>
    rw [hc] at hw
    have h0 : Frobenius.pw K P (c + 2) = K.zero := by
      show K.mul (K.mul (Frobenius.pw K P c) P) P = K.zero
      rw [K.mul_assoc, hPP]; lib_ring
    rw [h0, hx] at hw
    exact (hP0 (hw.trans (show K.mul K.zero w = K.zero by lib_ring))).elim

end ExactOne

#print axioms ExactOne.pr_form
#print axioms ExactOne.pr_unit
#print axioms ExactOne.exact_one

lang/ExactOneK.lean

lean_all/lang/ExactOneK.lean

/-
  lang/ExactOneK — T49 (O3') (v3): **a product equal to P has exactly one factor divisible by P** when every
  1 − P·t is a unit (instead of P² = 0 in lang/ExactOne), so the count also holds in ℤ/p^n, n ≥ 2:
    exact_one_k    pr xs = P, P ≠ 0, P not a unit, (1 − P t) a unit for all t ⇒ cnt xs = 1
    others_unit    pr (x :: xs) = P with x flagged and cnt = 1 ⇒ pr xs is a unit and x = P·U, U a unit
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.ExactOne

namespace ExactOneK

open FTAChain YMChain FiniteRecChain ExactOne

variable {R : Type} (K : DetChain.CRing R) (P : R) (d : R → Bool) (hP : ∀ x, d x = true → ∃ y, x = K.mul P y)
  (hU : ∀ x, d x = false → ∃ w, K.mul x w = K.one)

include hP hU in
/-- **exactly one factor is divisible by P** when the product is P and every 1 − P t is a unit. -/
theorem exact_one_k (hT : ∀ t, ∃ v, K.mul (K.add K.one (K.neg (K.mul P t))) v = K.one) (hP0 : P ≠ K.zero)
    (hPu : ∀ w, K.mul P w ≠ K.one) (xs : List R) (hx : pr K xs = P) : cnt d xs = 1 := by
  obtain ⟨w, hw⟩ := pr_form K P d hP xs
  match hc : cnt d xs with
  | 0 =>
    obtain ⟨u, hu⟩ := pr_unit K d hU xs hc
    rw [hx] at hu
    exact absurd hu (hPu u)
  | 1 => rfl
  | c + 2 =>
    rw [hc, hx] at hw
    have hpw : Frobenius.pw K P (c + 2) = K.mul (K.mul (Frobenius.pw K P c) P) P := rfl
    rw [hpw] at hw
    obtain ⟨v, hv⟩ := hT (K.mul (Frobenius.pw K P c) w)
    refine (hP0 ?_).elim
    generalize Frobenius.pw K P c = Q at hw hv
    have h1 : K.mul P (K.add K.one (K.neg (K.mul P (K.mul Q w)))) = K.zero := by
      calc K.mul P (K.add K.one (K.neg (K.mul P (K.mul Q w))))
          = K.add P (K.neg (K.mul (K.mul (K.mul Q P) P) w)) := by lib_ring
        _ = K.add P (K.neg P) := by rw [← hw]
        _ = K.zero := by lib_ring
    calc P = K.mul P K.one := by lib_ring
      _ = K.mul P (K.mul (K.add K.one (K.neg (K.mul P (K.mul Q w)))) v) := by rw [hv]
      _ = K.mul (K.mul P (K.add K.one (K.neg (K.mul P (K.mul Q w))))) v := by lib_ring
      _ = K.mul K.zero v := by rw [h1]
      _ = K.zero := by lib_ring

include hU in
/-- **the flagged factor is P times a unit** when it is the only flagged one. -/
theorem others_unit (x : R) (xs : List R) (hx : pr K (x :: xs) = P) (hd : d x = true)
    (hc : cnt d (x :: xs) = 1) : ∃ U w, x = K.mul P U ∧ K.mul U w = K.one := by
  have h0 : cnt d xs = 0 := by
    have h' : (if d x = true then 1 else 0) + cnt d xs = 1 := hc
    rw [hd, ite_pos' _ _ rfl] at h'
    exact Nat.succ.inj ((show cnt d xs + 1 = 1 + cnt d xs by lib_nat).trans h')
  obtain ⟨w, hw⟩ := pr_unit K d hU xs h0
  have hx' : K.mul x (pr K xs) = P := hx
  refine ⟨w, pr K xs, ?_, ?_⟩
  · calc x = K.mul (K.mul x (pr K xs)) w := by rw [show K.mul (K.mul x (pr K xs)) w = K.mul x (K.mul (pr K xs) w) by lib_ring, hw]; lib_ring
      _ = K.mul P w := by rw [hx']
  · rw [show K.mul w (pr K xs) = K.mul (pr K xs) w by lib_ring]
    exact hw

end ExactOneK

#print axioms ExactOneK.exact_one_k
#print axioms ExactOneK.others_unit

lang/ExactP2.lean

lean_all/lang/ExactP2.lean

/-
  lang/ExactP2 — T49 (general primary α, step (O1')): **in ℤ/p², a product of factors equal to p has exactly one
  factor divisible by p** (lang/ExactOne over R = ℤ/p², P = p·1). M + 1 = p², p = J + 2 prime.
    red_P        red(p·1) = 0 in 𝔽_p
    unit_p2      red(x) ≠ 0 ⇒ x is a unit in ℤ/p²    (x·y = 1 + P t with y a lift of red(x)^(−1); then (1 + Pt)(1 − Pt) = 1)
    P_ne         p·1 ≠ 0 in ℤ/p²
    P_nunit      p·w ≠ 1 in ℤ/p²
    exact_one_p2 Π xs = p ⇒ #{x ∈ xs : red(x) = 0} = 1
  With xs = (ι_c(α))_c and Π_c ι_c(α) = ι(N(α)) = p: exactly one P_c divides α (test: work/data/t49/normp_test.py).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.ExactOne
import lang.RedKer
import lang.TeichChar2
import lang.StickVal

namespace ExactP2

open FTAChain ZMod YMChain FiniteRecChain Frobenius

variable (J M : Nat) (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1)
  (hM : M + 1 = (J + 1 + 1) * (J + 1 + 1))

/-- P = p·1 in ℤ/p². -/
def P : Zp M := nsm (ring M) (J + 1 + 1) (ring M).one

include hM in
theorem red_P : RedP2.red J M (P J M) = (ring (J + 1)).zero := Subtype.ext (by
  show md (J + 1) (nsm (ring M) (J + 1 + 1) (ring M).one).val = md (J + 1) 0
  rw [nsm_one_val, RedP2.mdmd J M (J + 1 + 1) hM, md_self]
  rfl)

include hpr hM in
/-- **red(x) ≠ 0 ⇒ x is a unit in ℤ/p²**. -/
theorem unit_p2 (x : Zp M) (hx : RedP2.red J M x ≠ (ring (J + 1)).zero) :
    ∃ w, (ring M).mul x w = (ring M).one := by
  have hinv := inv_exists J hpr (RedP2.red J M x) hx
  let y := TeichChar.lift J M (pw (ring (J + 1)) (RedP2.red J M x) J)
  have hy : RedP2.red J M y = pw (ring (J + 1)) (RedP2.red J M x) J := TeichChar.red_lift J M hM _
  have h0 : RedP2.red J M ((ring M).add ((ring M).mul x y) ((ring M).neg (ring M).one)) = (ring (J + 1)).zero := by
    rw [RedP2.red_add J M (J + 1 + 1) hM, RedP2.red_mul J M (J + 1 + 1) hM, TeichChar.red_neg J M hM,
      RedP2.red_one J M (J + 1 + 1) hM, hy]
    calc (ring (J + 1)).add ((ring (J + 1)).mul (RedP2.red J M x) (pw (ring (J + 1)) (RedP2.red J M x) J))
          ((ring (J + 1)).neg (ring (J + 1)).one)
        = (ring (J + 1)).add ((ring (J + 1)).mul (pw (ring (J + 1)) (RedP2.red J M x) J) (RedP2.red J M x))
          ((ring (J + 1)).neg (ring (J + 1)).one) := by lib_ring
      _ = (ring (J + 1)).zero := by rw [hinv]; lib_ring
  obtain ⟨t, ht⟩ := RedP2.red_ker J M _ h0
  have hPP := RedP2.P_sq J M hM
  refine ⟨(ring M).mul y ((ring M).add (ring M).one ((ring M).neg ((ring M).mul (P J M) t))), ?_⟩
  calc (ring M).mul x ((ring M).mul y ((ring M).add (ring M).one ((ring M).neg ((ring M).mul (P J M) t))))
      = (ring M).mul ((ring M).add ((ring M).add ((ring M).mul x y) ((ring M).neg (ring M).one)) (ring M).one)
          ((ring M).add (ring M).one ((ring M).neg ((ring M).mul (P J M) t))) := by lib_ring
    _ = (ring M).mul ((ring M).add ((ring M).mul (P J M) t) (ring M).one)
          ((ring M).add (ring M).one ((ring M).neg ((ring M).mul (P J M) t))) :=
          congrArg (fun z => (ring M).mul ((ring M).add z (ring M).one)
            ((ring M).add (ring M).one ((ring M).neg ((ring M).mul (P J M) t)))) ht
    _ = (ring M).add (ring M).one ((ring M).neg ((ring M).mul ((ring M).mul (P J M) (P J M)) ((ring M).mul t t))) := by
          lib_ring
    _ = (ring M).one := by
          show (ring M).add (ring M).one ((ring M).neg ((ring M).mul
            ((ring M).mul (nsm (ring M) (J + 1 + 1) (ring M).one) (nsm (ring M) (J + 1 + 1) (ring M).one))
            ((ring M).mul t t))) = (ring M).one
          rw [hPP]; lib_ring

include hM in
theorem P_ne : P J M ≠ (ring M).zero :=
  StickVal.small_ne_zero M (J + 1) (by
    have e : (J + 1 + 1) * (J + 1 + 1) = (J + 1 + 1 + 1) + (J * J + 3 * J + 1) := by lib_nat
    have h3 : J + 1 + 1 + 1 ≤ (J + 1 + 1) * (J + 1 + 1) := by rw [e]; exact Nat.le_add_right _ _
    rw [hM]; exact h3)

include hM in
theorem P_nunit (w : Zp M) : (ring M).mul (P J M) w ≠ (ring M).one := fun h => by
  have h1 := congrArg (RedP2.red J M) h
  rw [RedP2.red_mul J M (J + 1 + 1) hM, red_P J M hM, RedP2.red_one J M (J + 1 + 1) hM] at h1
  exact PowerSum.one_ne_zero' J (h1.symm.trans
    (show (ring (J + 1)).mul (ring (J + 1)).zero (RedP2.red J M w) = (ring (J + 1)).zero by lib_ring))

/-- the flag: red(x) = 0. -/
def d (x : Zp M) : Bool := decide (RedP2.red J M x = (ring (J + 1)).zero)

include hpr hM in
/-- **exactly one factor is divisible by p** when the product in ℤ/p² is p. -/
theorem exact_one_p2 (xs : List (Zp M)) (hx : ExactOne.pr (ring M) xs = P J M) : ExactOne.cnt (d J M) xs = 1 :=
  ExactOne.exact_one (ring M) (P J M) (d J M)
    (fun x h => RedP2.red_ker J M x (of_decide_eq_true h))
    (fun x h => unit_p2 J M hpr hM x (of_decide_eq_false h))
    (RedP2.P_sq J M hM) (P_ne J M hM) (P_nunit J M hM) xs hx

end ExactP2

#print axioms ExactP2.red_P
#print axioms ExactP2.unit_p2
#print axioms ExactP2.P_ne
#print axioms ExactP2.P_nunit
#print axioms ExactP2.exact_one_p2

lang/ExtSquareLocal.lean

lean_all/lang/ExtSquareLocal.lean

/-
  lang/ExtSquareLocal — T43, cycle 95 (functoriality, local, unramified): the exterior square GL4 → GL6.

  ∧² sends Satake data (a, b, c, d) of GL4 to the six products {ab, ac, ad, bc, bd, cd}. With e₁, …, e₄ the elementary
  symmetric functions (the GL4 eigen-system at p, GLnLocal), its Satake polynomial is
      1 − e₂X + (e₁e₃ − e₄)X² − (e₁²e₄ + e₃² − 2e₂e₄)X³ + e₄(e₁e₃ − e₄)X⁴ − e₂e₄²X⁵ + e₄³X⁶,
  a function of the GL4 data alone:
    cE              the polynomial in (e₁, e₂, e₃, e₄)
    ext2_poly       cf [ab, ac, ad, bc, bd, cd] = cE (e₁, e₂, e₃, e₄)
    ext2_lift       the GL6 eigen-system is the unique solution of conv (cE) h = δ — the local form of Kim's ∧² lift
  Checked first on 300 random integer quadruples.

  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.TensorLocal

namespace ExtSquareLocal

open GLnLocal

variable {R : Type} (K : DetChain.CRing R)

def cE (e₁ e₂ e₃ e₄ : R) : Nat → R
  | 0 => K.one
  | 1 => K.neg e₂
  | 2 => K.add (K.mul e₁ e₃) (K.neg e₄)
  | 3 => K.neg (K.add (K.add (K.mul (K.mul e₁ e₁) e₄) (K.mul e₃ e₃)) (K.neg (K.add (K.mul e₂ e₄) (K.mul e₂ e₄))))
  | 4 => K.mul e₄ (K.add (K.mul e₁ e₃) (K.neg e₄))
  | 5 => K.neg (K.mul e₂ (K.mul e₄ e₄))
  | 6 => K.mul e₄ (K.mul e₄ e₄)
  | _ + 7 => K.zero

/-- **The ∧² Satake polynomial depends only on the GL4 data.** -/
theorem ext2_poly (a b c d : R) : ∀ k,
    cf K [K.mul a b, K.mul a c, K.mul a d, K.mul b c, K.mul b d, K.mul c d] k = cE K (K.add (K.add (K.add a b) c) d) (K.add (K.add (K.add (K.add (K.add (K.mul a b) (K.mul a c)) (K.mul a d)) (K.mul b c)) (K.mul b d)) (K.mul c d)) (K.add (K.add (K.add (K.mul (K.mul a b) c) (K.mul (K.mul a b) d)) (K.mul (K.mul a c) d)) (K.mul (K.mul b c) d)) (K.mul (K.mul (K.mul a b) c) d) k
  | 0 => by
    show K.one = K.one
    lib_ring
  | 1 => by
    show K.add (K.add (K.add (K.add (K.add (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.one)))) (K.neg (K.mul (K.mul b d) (K.one)))) (K.neg (K.mul (K.mul b c) (K.one)))) (K.neg (K.mul (K.mul a d) (K.one)))) (K.neg (K.mul (K.mul a c) (K.one)))) (K.neg (K.mul (K.mul a b) (K.one))) = K.neg (K.add (K.add (K.add (K.add (K.add (K.mul a b) (K.mul a c)) (K.mul a d)) (K.mul b c)) (K.mul b d)) (K.mul c d))
    lib_ring
  | 2 => by
    show K.add (K.add (K.add (K.add (K.add (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.zero)))) (K.neg (K.mul (K.mul b d) (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.one))))))) (K.neg (K.mul (K.mul b c) (K.add (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.one)))) (K.neg (K.mul (K.mul b d) (K.one))))))) (K.neg (K.mul (K.mul a d) (K.add (K.add (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.one)))) (K  -- (+745 chars; full line in the Zenodo source)
    lib_ring
  | 3 => by
    show K.add (K.add (K.add (K.add (K.add (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.zero)))) (K.neg (K.mul (K.mul b d) (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.zero))))))) (K.neg (K.mul (K.mul b c) (K.add (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.zero)))) (K.neg (K.mul (K.mul b d) (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.one)))))))))) (K.neg (K.mul (K.mul a d) (K.add (K.add (K.add (  -- (+2128 chars; full line in the Zenodo source)
    lib_ring
  | 4 => by
    show K.add (K.add (K.add (K.add (K.add (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.zero)))) (K.neg (K.mul (K.mul b d) (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.zero))))))) (K.neg (K.mul (K.mul b c) (K.add (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.zero)))) (K.neg (K.mul (K.mul b d) (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.zero)))))))))) (K.neg (K.mul (K.mul a d) (K.add (K.add (K.add   -- (+2360 chars; full line in the Zenodo source)
    lib_ring
  | 5 => by
    show K.add (K.add (K.add (K.add (K.add (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.zero)))) (K.neg (K.mul (K.mul b d) (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.zero))))))) (K.neg (K.mul (K.mul b c) (K.add (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.zero)))) (K.neg (K.mul (K.mul b d) (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.zero)))))))))) (K.neg (K.mul (K.mul a d) (K.add (K.add (K.add   -- (+2599 chars; full line in the Zenodo source)
    lib_ring
  | 6 => by
    show K.add (K.add (K.add (K.add (K.add (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.zero)))) (K.neg (K.mul (K.mul b d) (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.zero))))))) (K.neg (K.mul (K.mul b c) (K.add (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.zero)))) (K.neg (K.mul (K.mul b d) (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.zero)))))))))) (K.neg (K.mul (K.mul a d) (K.add (K.add (K.add   -- (+2561 chars; full line in the Zenodo source)
    lib_ring
  | k + 7 => by
    show K.add (K.add (K.add (K.add (K.add (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.zero)))) (K.neg (K.mul (K.mul b d) (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.zero))))))) (K.neg (K.mul (K.mul b c) (K.add (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.zero)))) (K.neg (K.mul (K.mul b d) (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.zero)))))))))) (K.neg (K.mul (K.mul a d) (K.add (K.add (K.add   -- (+2459 chars; full line in the Zenodo source)
    lib_ring

/-- **Local ∧² : GL4 → GL6 functoriality.** -/
theorem ext2_lift (a b c d : R) (g : Nat → R)
    (hg : ∀ k, conv K (cE K (K.add (K.add (K.add a b) c) d) (K.add (K.add (K.add (K.add (K.add (K.mul a b) (K.mul a c)) (K.mul a d)) (K.mul b c)) (K.mul b d)) (K.mul c d)) (K.add (K.add (K.add (K.mul (K.mul a b) c) (K.mul (K.mul a b) d)) (K.mul (K.mul a c) d)) (K.mul (K.mul b c) d)) (K.mul (K.mul (K.mul a b) c) d)) g k = delta K k) :
    ∀ k, hk K [K.mul a b, K.mul a c, K.mul a d, K.mul b c, K.mul b d, K.mul c d] k = g k := fun k =>
  conv_unique K (cE K (K.add (K.add (K.add a b) c) d) (K.add (K.add (K.add (K.add (K.add (K.mul a b) (K.mul a c)) (K.mul a d)) (K.mul b c)) (K.mul b d)) (K.mul c d)) (K.add (K.add (K.add (K.mul (K.mul a b) c) (K.mul (K.mul a b) d)) (K.mul (K.mul a c) d)) (K.mul (K.mul b c) d)) (K.mul (K.mul (K.mul a b) c) d)) _ g rfl
    (fun j => (conv_ext_left K _ _ _ (fun i => (ext2_poly K a b c d i).symm) j).trans
      (conv_cf_hk K [K.mul a b, K.mul a c, K.mul a d, K.mul b c, K.mul b d, K.mul c d] j)) hg k k (Nat.le_refl k)

end ExtSquareLocal

#print axioms ExtSquareLocal.ext2_poly
#print axioms ExtSquareLocal.ext2_lift

lang/FFChar.lean

lean_all/lang/FFChar.lean

/-
  lang/FFChar — T49 (residue fields of degree f > 1): **the characteristic of a finite field of the interface**.
  Translation x ↦ x + 1 permutes the element list, so |F|·1 = 0; if |F| = q^(N+1) then (q·1)^(N+1) = 0 and, without
  zero divisors, q·1 = 0. This is the hypothesis p·1 = 0 of lang/FpEmb for the fields 𝔽_(q^(2^k)) of lang/FieldTower.
    len_count     Σ_(x∈l) 1 = |l| in ℤ
    sum_one       Σ_(x∈l) 1 = |l|·1 in any ring
    nsm_pow_any   (a^e)·1 = (a·1)^e
    pw_zero_eq    x^(N+1) = 0 ⇒ x = 0          (no zero divisors)
    char_card     |F|·1 = 0
    char_q        |F| = q^(N+1) ⇒ q·1 = 0
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.FinPowerSum
import lib.RingTac

namespace FFChar

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius FinField

theorem len_count {A : Type} : ∀ l : List A, sumK LibInt.intR (fun _ => (1 : Int)) l = Int.ofNat l.length
  | [] => rfl
  | _ :: as => by
    show LibInt.intR.add 1 (sumK LibInt.intR (fun _ => (1 : Int)) as) = Int.ofNat (as.length + 1)
    rw [len_count as]
    exact LibInt.intR.add_comm _ _

theorem sum_one {R : Type} (K : CRing R) {A : Type} : ∀ l : List A, sumK K (fun _ => K.one) l = nsm K l.length K.one
  | [] => rfl
  | _ :: as => by
    show K.add K.one (sumK K (fun _ => K.one) as) = K.add (nsm K as.length K.one) K.one
    rw [sum_one K as]
    exact K.add_comm _ _

theorem nsm_pow_any {R : Type} (K : CRing R) (a : Nat) : ∀ e, nsm K (a ^ e) K.one = pw K (nsm K a K.one) e
  | 0 => by
    show nsm K 1 K.one = K.one
    exact nsm_one K K.one
  | e + 1 => by
    show nsm K (a ^ e * a) K.one = K.mul (pw K (nsm K a K.one) e) (nsm K a K.one)
    rw [nsm_mul_nat K (a ^ e) K.one a, nsm_eq_mul K a (nsm K (a ^ e) K.one), nsm_pow_any K a e]
    exact K.mul_comm _ _

theorem pw_zero_eq {R : Type} (K : CRing R) (hK : NoZD K) (x : R) : ∀ N, pw K x (N + 1) = K.zero → x = K.zero
  | 0, h => by
    have e : pw K x 1 = K.mul K.one x := rfl
    rw [e, K.one_mul] at h
    exact h
  | N + 1, h => by
    cases hK (pw K x (N + 1)) x h with
    | inl h1 => exact pw_zero_eq K hK x N h1
    | inr h2 => exact h2

variable {R : Type} [DecidableEq R] (F : FF R)

/-- **|F|·1 = 0**. -/
theorem char_card : nsm F.K (2 * F.h + 1) F.K.one = F.K.zero := by
  have hlen : F.els.length = 2 * F.h + 1 := Int.ofNat.inj ((len_count F.els).symm.trans F.card)
  have hr := reindex F.K F.els (FinPowerSum.compK F) (fun x => F.K.add x F.K.one) (fun y => F.K.add y (F.K.neg F.K.one))
    (fun x => by
      show F.K.add (F.K.add x F.K.one) (F.K.neg F.K.one) = x
      lib_ring) (fun y => by
      show F.K.add (F.K.add y (F.K.neg F.K.one)) F.K.one = y
      lib_ring) (fun x => x)
  have hs : sumK F.K (fun x => F.K.add x F.K.one) F.els = F.K.add (sumK F.K (fun x => x) F.els) (sumK F.K (fun _ => F.K.one) F.els) :=
    sumK_add F.K (fun x => x) (fun _ => F.K.one) F.els
  rw [sum_one F.K F.els, hlen] at hs
  have h2 : F.K.add (sumK F.K (fun x => x) F.els) (nsm F.K (2 * F.h + 1) F.K.one) = sumK F.K (fun x => x) F.els :=
    hs.symm.trans hr
  generalize sumK F.K (fun x => x) F.els = S at h2
  generalize nsm F.K (2 * F.h + 1) F.K.one = c at h2 ⊢
  calc c = F.K.add (F.K.add S c) (F.K.neg S) := by lib_ring
    _ = F.K.add S (F.K.neg S) := by rw [h2]
    _ = F.K.zero := F.K.add_neg _

/-- **|F| = q^(N+1) ⇒ q·1 = 0**. -/
theorem char_q (q N : Nat) (hq : 2 * F.h + 1 = q ^ (N + 1)) : nsm F.K q F.K.one = F.K.zero := by
  have h := char_card F
  rw [hq, nsm_pow_any F.K q (N + 1)] at h
  exact pw_zero_eq F.K F.nozd _ N h

end FFChar

#print axioms FFChar.len_count
#print axioms FFChar.sum_one
#print axioms FFChar.nsm_pow_any
#print axioms FFChar.pw_zero_eq
#print axioms FFChar.char_card
#print axioms FFChar.char_q

lang/FamilyBound.lean

lean_all/lang/FamilyBound.lean

/-
  lang/FamilyBound — T43, cycle 121: Hasse's bound for every quartic and every sextic twist over 𝔽_p.

    quartic_bound   φ_p(D)² + s = 4p   (y² = x³ + Dx, every odd p, D ≠ 0)          (QuarticFamily.family_trace; inert: 0)
    sextic_bound    ψ_p(D)² + s = 4p   (y² = x³ + D, every p > 3, D ≠ 0)           (FinCubicNorm over 𝔽_p; inert: 0)
  |a_p| ≤ 2√p for all CM curves with j = 1728 or j = 0 over ℚ at every good prime — Ramanujan–Petersson for the
  corresponding CM newforms, as integer statements.

  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.SexticHD
import lang.HasseBoundP

namespace FamilyBound

open FTAChain ZMod YMChain FiniteRecChain LibInt

theorem quartic_bound (k : Nat) (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = 2 * k + 1 + 1 + 1)
    (D : Zp (2 * k + 1 + 1)) (hD : D ≠ (ring (2 * k + 1 + 1)).zero) :
    ∃ s : Nat, Jacobsthal.phi k D * Jacobsthal.phi k D + Int.ofNat s = 4 * Int.ofNat (2 * k + 1 + 1 + 1) := by
  cases EveryPrime.nat_even_odd k with
  | inl h =>
    match h with
    | ⟨j, hj⟩ =>
      subst hj
      refine ⟨4 * (2 * (2 * j) + 1 + 1 + 1), ?_⟩
      rw [QuarticFamily.phi_inert (2 * j) hpr (BaseChange.chi_negone j (TwoPower.negone_odd' (2 * (2 * j) + 1) j)) D,
        show Int.ofNat (4 * (2 * (2 * j) + 1 + 1 + 1)) = 4 * Int.ofNat (2 * (2 * j) + 1 + 1 + 1) from rfl]
      lib_int
  | inr h =>
    match h with
    | ⟨j, hj⟩ =>
      subst hj
      match QuarticFamily.family_trace j hpr D hD with
      | ⟨u, v, huv, hc⟩ =>
        have hP : Int.ofNat u * Int.ofNat u + Int.ofNat v * Int.ofNat v = Int.ofNat (2 * (2 * j + 1) + 1 + 1 + 1) :=
          congrArg Int.ofNat huv
        generalize Jacobsthal.phi (2 * j + 1) D = S at hc ⊢
        have h4 : Int.ofNat (4 * (v * v)) = 4 * (Int.ofNat v * Int.ofNat v) := rfl
        have h4' : Int.ofNat (4 * (u * u)) = 4 * (Int.ofNat u * Int.ofNat u) := rfl
        cases hc with
        | inl e => exact ⟨4 * (v * v), by rw [e, h4, ← hP]; show (2 * Int.ofNat u) * (2 * Int.ofNat u) + _ = _; lib_int⟩
        | inr hc =>
          cases hc with
          | inl e => exact ⟨4 * (v * v), by rw [e, h4, ← hP]; show -(2 * Int.ofNat u) * -(2 * Int.ofNat u) + _ = _; lib_int⟩
          | inr hc =>
            cases hc with
            | inl e => exact ⟨4 * (u * u), by rw [e, h4', ← hP]; show (2 * Int.ofNat v) * (2 * Int.ofNat v) + _ = _; lib_int⟩
            | inr e => exact ⟨4 * (u * u), by rw [e, h4', ← hP]; show -(2 * Int.ofNat v) * -(2 * Int.ofNat v) + _ = _; lib_int⟩

theorem sextic_bound (k : Nat) (hk : 1 ≤ k) (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = 2 * k + 1 + 1 + 1)
    (D : Zp (2 * k + 1 + 1)) (hD : D ≠ (ring (2 * k + 1 + 1)).zero) :
    ∃ s : Nat, FinCubic.psi (FinField.ffp k hpr) D * FinCubic.psi (FinField.ffp k hpr) D + Int.ofNat s
      = 4 * Int.ofNat (2 * k + 1 + 1 + 1) := by
  cases EveryPrime3.nat_mod3 k with
  | inl h =>
    exfalso
    match h with
    | ⟨t, ht⟩ =>
      cases hpr 3 ⟨2 * t + 1, by rw [ht]; lib_nat⟩ with
      | inl e => exact Nat.noConfusion (Nat.succ.inj (show (2 : Nat) + 1 = 0 + 1 from e))
      | inr e =>
        have e' : 2 * k = 0 := Nat.succ.inj (Nat.succ.inj (Nat.succ.inj e.symm))
        cases k with
        | zero => exact Nat.not_succ_le_zero 0 hk
        | succ k' => exact Nat.noConfusion (show 2 * k' + 1 + 1 = 0 from e')
  | inr h =>
    cases h with
    | inl h =>
      match h with
      | ⟨t, ht⟩ =>
        subst ht
        refine ⟨4 * (2 * (3 * t + 1) + 1 + 1 + 1), ?_⟩
        rw [show FinCubic.psi (FinField.ffp (3 * t + 1) hpr) D = CubicJacob.psi (3 * t + 1) D from rfl,
          SexticFamily.psi_inert (3 * t + 1) hpr (4 * t + 3) (by lib_nat) D,
          show Int.ofNat (4 * (2 * (3 * t + 1) + 1 + 1 + 1)) = 4 * Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) from rfl]
        lib_int
    | inr h =>
      match h with
      | ⟨t, ht⟩ =>
        subst ht
        match FinCubicNorm.norm_twist (FinField.ffp (3 * t + 2) hpr) (2 * t + 2) (by show 2 * (3 * t + 2 + 1) = _; lib_nat) D hD with
        | ⟨b, hb⟩ =>
          match FinCubicSign.sq_nn b with
          | ⟨nb, hnb⟩ =>
            refine ⟨3 * nb, ?_⟩
            rw [show Int.ofNat (3 * nb) = 3 * Int.ofNat nb from rfl, ← hnb]
            exact hb

end FamilyBound

#print axioms FamilyBound.quartic_bound
#print axioms FamilyBound.sextic_bound

lang/FamilyFq.lean

lean_all/lang/FamilyFq.lean

/-
  lang/FamilyFq — T43, cycle 110: the quartic twists y² = x³ + Dx (D ∈ 𝔽_p^×) over 𝔽_{p²} = 𝔽_p[√n].

  φ_q(a) = Σ_{x ∈ 𝔽_q} χ(x(x² + a)). In 𝔽_{p²}, −D is a square τ² (every element of 𝔽_p is), so φ(D) = χ(τ) φ(−1)
  (FinConic.phi_scale), and τ^((p²−1)/2) = ((−D)^((p−1)/2))^((p+1)/2):
    family_scale     ∃ τ ≠ 0, φ_{p²}(D) = χ(τ) φ_{p²}(−1),  τ^h = ((−D)^((p−1)/2))^((p+1)/2)
    family_inert     p ≡ 3 (mod 4):  φ_{p²}(D) = φ_{p²}(−1)              ((p + 1)/2 even)
    family_split     p ≡ 1 (mod 4):  φ_{p²}(D) = χ_p(−D) φ_{p²}(−1)       ((p + 1)/2 odd)
  The twist by D over 𝔽_{p²} is the twist by the quartic-residue sign of D, the base change of the quartic character.

  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.EveryPrime
import lang.SplitHasse
import lang.CubicSuper2

namespace FamilyFq

open FTAChain ZMod YMChain FiniteRecChain Quad

theorem h_two (k : Nat) (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = (2 * k + 1 + 1 + 1)) (n : Zp (2 * k + 1 + 1)) (hn : Frobenius.pw (ring (2 * k + 1 + 1)) n (k + 1) = (ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) : (Fq2Inst.ffq k hpr n hn).h = 2 * ((k + 1) * (k + 2)) := by
  rw [CubicSuper2.h_eq k hpr n hn]; lib_nat

theorem family_scale (k : Nat) (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = (2 * k + 1 + 1 + 1)) (n : Zp (2 * k + 1 + 1)) (hn : Frobenius.pw (ring (2 * k + 1 + 1)) n (k + 1) = (ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) (D : Zp (2 * k + 1 + 1)) (hD : D ≠ (ring (2 * k + 1 + 1)).zero) :
    ∃ τ, τ ≠ (Fq2.G2 (2 * k + 1) n).zero ∧
      FinConic.phi (Fq2Inst.ffq k hpr n hn) (D, (ring (2 * k + 1 + 1)).zero) = FinField.chi (Fq2Inst.ffq k hpr n hn) τ * FinConic.phi (Fq2Inst.ffq k hpr n hn) ((Fq2.G2 (2 * k + 1) n).neg (Fq2.G2 (2 * k + 1) n).one) ∧
      Frobenius.pw (Fq2.G2 (2 * k + 1) n) τ (Fq2Inst.ffq k hpr n hn).h = (Frobenius.pw (ring (2 * k + 1 + 1)) (Frobenius.pw (ring (2 * k + 1 + 1)) ((ring (2 * k + 1 + 1)).neg D) (k + 1)) (k + 2), (ring (2 * k + 1 + 1)).zero) := by
  have hnD : (ring (2 * k + 1 + 1)).neg D ≠ (ring (2 * k + 1 + 1)).zero := fun e => hD (by rw [← DetChain.neg_neg' (ring (2 * k + 1 + 1)) D, e]; exact (DetChain.neg_unique (ring (2 * k + 1 + 1)) _ _ ((ring (2 * k + 1 + 1)).zero_add _)).symm)
  have hc0 : ((ring (2 * k + 1 + 1)).neg D, (ring (2 * k + 1 + 1)).zero) ≠ (Fq2.G2 (2 * k + 1) n).zero := fun e => hnD (congrArg Prod.fst e)
  have hch : Frobenius.pw (Fq2Inst.ffq k hpr n hn).K ((ring (2 * k + 1 + 1)).neg D, (ring (2 * k + 1 + 1)).zero) (Fq2Inst.ffq k hpr n hn).h = (Fq2Inst.ffq k hpr n hn).K.one := by
    show Frobenius.pw (Fq2.G2 (2 * k + 1) n) ((ring (2 * k + 1 + 1)).neg D, (ring (2 * k + 1 + 1)).zero) (Fq2Inst.ffq k hpr n hn).h = _
    rw [CubicSuper2.h_eq k hpr n hn, QuadFrob.pw_embed (2 * k + 1) n,
      show (2 * k + 1 + 1 + 1 + 1) * (k + 1) = (2 * k + 1 + 1) * (k + 2) by lib_nat, ← TwoPower.pwpow,
      ZMod.unit_pow (2 * k + 1) hpr _ hnD, TwoPower.pwone]
    rfl
  match (Fq2Inst.ffq k hpr n hn).euler_hard _ hch with
  | ⟨τ, hτ⟩ =>
    have hτ' : (Fq2.G2 (2 * k + 1) n).mul τ τ = ((ring (2 * k + 1 + 1)).neg D, (ring (2 * k + 1 + 1)).zero) := hτ
    have hτ0 : τ ≠ (Fq2.G2 (2 * k + 1) n).zero := fun e => hc0 (by rw [← hτ', e]; exact DetChain.mul_zero (Fq2.G2 (2 * k + 1) n) _)
    refine ⟨τ, hτ0, ?_, ?_⟩
    · have e : (D, (ring (2 * k + 1 + 1)).zero) = (Fq2Inst.ffq k hpr n hn).K.mul ((Fq2.G2 (2 * k + 1) n).neg (Fq2.G2 (2 * k + 1) n).one) ((Fq2Inst.ffq k hpr n hn).K.mul τ τ) := by
        show (D, (ring (2 * k + 1 + 1)).zero) = (Fq2.G2 (2 * k + 1) n).mul ((Fq2.G2 (2 * k + 1) n).neg (Fq2.G2 (2 * k + 1) n).one) ((Fq2.G2 (2 * k + 1) n).mul τ τ)
        rw [hτ']
        apply Prod.ext
        · show D = (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) ((ring (2 * k + 1 + 1)).neg D)) ((ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).zero) (ring (2 * k + 1 + 1)).zero)); lib_ring
        · show (ring (2 * k + 1 + 1)).zero = (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) (ring (2 * k + 1 + 1)).zero) ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).zero) ((ring (2 * k + 1 + 1)).neg D)); lib_ring
      rw [e]
      exact FinConic.phi_scale (Fq2Inst.ffq k hpr n hn) _ τ hτ0
    · rw [h_two k hpr n hn, ← TwoPower.pwpow,
        show Frobenius.pw (Fq2.G2 (2 * k + 1) n) τ 2 = (Fq2.G2 (2 * k + 1) n).mul τ τ by show (Fq2.G2 (2 * k + 1) n).mul ((Fq2.G2 (2 * k + 1) n).mul (Fq2.G2 (2 * k + 1) n).one τ) τ = _; rw [(Fq2.G2 (2 * k + 1) n).one_mul],
        hτ', QuadFrob.pw_embed (2 * k + 1) n, ← TwoPower.pwpow]

theorem family_inert (j : Nat) (hpr : ∀ d, Dv d (2 * (2 * j) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * j) + 1 + 1 + 1)) (n : Zp (2 * (2 * j) + 1 + 1))
    (hn : Frobenius.pw (ring (2 * (2 * j) + 1 + 1)) n (2 * j + 1) = (ring (2 * (2 * j) + 1 + 1)).neg (ring (2 * (2 * j) + 1 + 1)).one) (D : Zp (2 * (2 * j) + 1 + 1)) (hD : D ≠ (ring (2 * (2 * j) + 1 + 1)).zero) :
    FinConic.phi (Fq2Inst.ffq (2 * j) hpr n hn) (D, (ring (2 * (2 * j) + 1 + 1)).zero) = FinConic.phi (Fq2Inst.ffq (2 * j) hpr n hn) ((Fq2.G2 (2 * (2 * j) + 1) n).neg (Fq2.G2 (2 * (2 * j) + 1) n).one) := by
  have hnD : (ring (2 * (2 * j) + 1 + 1)).neg D ≠ (ring (2 * (2 * j) + 1 + 1)).zero := fun e => hD (by rw [← DetChain.neg_neg' (ring (2 * (2 * j) + 1 + 1)) D, e]; exact (DetChain.neg_unique (ring (2 * (2 * j) + 1 + 1)) _ _ ((ring (2 * (2 * j) + 1 + 1)).zero_add _)).symm)
  match family_scale (2 * j) hpr n hn D hD with
  | ⟨τ, hτ0, hphi, hpow⟩ =>
    have he : Frobenius.pw (ring (2 * (2 * j) + 1 + 1)) (Frobenius.pw (ring (2 * (2 * j) + 1 + 1)) ((ring (2 * (2 * j) + 1 + 1)).neg D) (2 * j + 1)) (2 * j + 2) = (ring (2 * (2 * j) + 1 + 1)).one := by
      cases Euler.euler_pm (2 * j) hpr _ hnD with
      | inl h => rw [h, TwoPower.pwone]
      | inr h => rw [h, show 2 * j + 2 = 2 * (j + 1) by lib_nat, SplitHasse.negone_even]
    rw [he] at hpow
    rw [hphi, FinField.chi_one_of _ τ hτ0 hpow]
    lib_int

theorem family_split (j : Nat) (hpr : ∀ d, Dv d (2 * (2 * j + 1) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * j + 1) + 1 + 1 + 1)) (n : Zp (2 * (2 * j + 1) + 1 + 1))
    (hn : Frobenius.pw (ring (2 * (2 * j + 1) + 1 + 1)) n (2 * j + 1 + 1) = (ring (2 * (2 * j + 1) + 1 + 1)).neg (ring (2 * (2 * j + 1) + 1 + 1)).one) (D : Zp (2 * (2 * j + 1) + 1 + 1)) (hD : D ≠ (ring (2 * (2 * j + 1) + 1 + 1)).zero) :
    FinConic.phi (Fq2Inst.ffq (2 * j + 1) hpr n hn) (D, (ring (2 * (2 * j + 1) + 1 + 1)).zero) = QChar.chi (2 * j + 1) ((ring (2 * (2 * j + 1) + 1 + 1)).neg D) * FinConic.phi (Fq2Inst.ffq (2 * j + 1) hpr n hn) ((Fq2.G2 (2 * (2 * j + 1) + 1) n).neg (Fq2.G2 (2 * (2 * j + 1) + 1) n).one) := by
  have hnD : (ring (2 * (2 * j + 1) + 1 + 1)).neg D ≠ (ring (2 * (2 * j + 1) + 1 + 1)).zero := fun e => hD (by rw [← DetChain.neg_neg' (ring (2 * (2 * j + 1) + 1 + 1)) D, e]; exact (DetChain.neg_unique (ring (2 * (2 * j + 1) + 1 + 1)) _ _ ((ring (2 * (2 * j + 1) + 1 + 1)).zero_add _)).symm)
  match family_scale (2 * j + 1) hpr n hn D hD with
  | ⟨τ, hτ0, hphi, hpow⟩ =>
    rw [hphi]
    cases Euler.euler_pm (2 * j + 1) hpr _ hnD with
    | inl h =>
      rw [h, TwoPower.pwone] at hpow
      rw [FinField.chi_one_of _ τ hτ0 hpow, QChar.chi_one_of (2 * j + 1) _ hnD h]
    | inr h =>
      rw [h, show 2 * j + 1 + 2 = 2 * (j + 1) + 1 by lib_nat, TwoPower.negone_odd' (2 * (2 * j + 1) + 1) (j + 1)] at hpow
      have hne : Frobenius.pw (Fq2Inst.ffq (2 * j + 1) hpr n hn).K τ (Fq2Inst.ffq (2 * j + 1) hpr n hn).h ≠ (Fq2Inst.ffq (2 * j + 1) hpr n hn).K.one := fun e =>
        QChar.ne_pm (2 * j + 1) (congrArg Prod.fst (hpow.symm.trans e))
      have hne' : Frobenius.pw (ring (2 * (2 * j + 1) + 1 + 1)) ((ring (2 * (2 * j + 1) + 1 + 1)).neg D) (2 * j + 1 + 1) ≠ (ring (2 * (2 * j + 1) + 1 + 1)).one := fun e => QChar.ne_pm (2 * j + 1) (h.symm.trans e)
      rw [FinField.chi_neg_of _ τ hτ0 hne, QChar.chi_neg_of (2 * j + 1) _ hnD hne']

end FamilyFq

#print axioms FamilyFq.h_two
#print axioms FamilyFq.family_scale
#print axioms FamilyFq.family_inert
#print axioms FamilyFq.family_split

lang/FamilyHD.lean

lean_all/lang/FamilyHD.lean

/-
  lang/FamilyHD — T43, cycle 111: a(𝔽_{p²}) = a_p² − 2p for every quartic twist y² = x³ + Dx, D ∈ 𝔽_p^×, at every odd p.

  φ_q(D) = Σ_{x ∈ 𝔽_q} χ(x(x² + D)), a_q(E_D) = −φ_q(D).
    four_p          φ_p(1)² + φ_p(n)² = 4p at p ≡ 1 (mod 4)                   (JacobMoment, JacobClass; as in CMModular)
    phi_sq_class    φ_p(D)² = φ_p(1)² if χ(D) = 1,  φ_p(n)² if χ(D) = −1        (JacobClass.phi_class)
    family_hd_split p ≡ 1 (mod 4): −φ_{p²}(D) = φ_p(D)² − 2p  (χ(−D) = χ(D); FamilyFq.family_split + HasseDavenport)
    family_hd_inert p ≡ 3 (mod 4): −φ_{p²}(D) = φ_p(D)² − 2p = −2p
    family_hd       every odd p
  So every curve y² = x³ + Dx has the Frobenius-square identity of a CM form at every good prime: the quartic twists of the
  Hecke character of ℚ(i), base-changed to 𝔽_{p²}.

  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.FamilyFq
import lang.CMModular

namespace FamilyHD

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum Jacobsthal JacobMoment JacobClass LibInt

theorem four_p (j : Nat) (hpr : ∀ d, Dv d (2 * (2 * j + 1) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * j + 1) + 1 + 1 + 1)) (n : Zp (2 * (2 * j + 1) + 1 + 1))
    (hn0 : n ≠ (ring (2 * (2 * j + 1) + 1 + 1)).zero) (hn : chi (2 * j + 1) n = -1) :
    phi (2 * j + 1) (ring (2 * (2 * j + 1) + 1 + 1)).one * phi (2 * j + 1) (ring (2 * (2 * j + 1) + 1 + 1)).one + phi (2 * j + 1) n * phi (2 * j + 1) n = 4 * P (2 * j + 1) := by
  match GaussSplit.split_one_mod_four j hpr with
  | ⟨i, hi⟩ =>
    have hi0 : i ≠ (ring (2 * (2 * j + 1) + 1 + 1)).zero := fun e => by
      rw [e, DetChain.zero_mul'] at hi
      apply PowerSum.one_ne_zero' (2 * (2 * j + 1) + 1)
      have := congrArg (fun z => (ring (2 * (2 * j + 1) + 1 + 1)).add z (ring (2 * (2 * j + 1) + 1 + 1)).one) hi
      exact (((ring (2 * (2 * j + 1) + 1 + 1)).zero_add (ring (2 * (2 * j + 1) + 1 + 1)).one).symm.trans (this.trans (DetChain.add_neg' (ring (2 * (2 * j + 1) + 1 + 1)) (ring (2 * (2 * j + 1) + 1 + 1)).one)))
    have hchi : chi (2 * j + 1) ((ring (2 * (2 * j + 1) + 1 + 1)).neg (ring (2 * (2 * j + 1) + 1 + 1)).one) = 1 := by rw [← hi]; exact chi_sq (2 * j + 1) hpr i hi0
    have hM := moment (2 * j + 1) hpr
    have hG := grouped (2 * j + 1) hpr n hn0 hn
    rw [hchi] at hM
    rw [hM] at hG
    have hz : (P (2 * j + 1) + -1) * (phi (2 * j + 1) (ring (2 * (2 * j + 1) + 1 + 1)).one * phi (2 * j + 1) (ring (2 * (2 * j + 1) + 1 + 1)).one + phi (2 * j + 1) n * phi (2 * j + 1) n + -1 * (4 * P (2 * j + 1))) = 0 := by
      have : (P (2 * j + 1) + -1) * (phi (2 * j + 1) (ring (2 * (2 * j + 1) + 1 + 1)).one * phi (2 * j + 1) (ring (2 * (2 * j + 1) + 1 + 1)).one + phi (2 * j + 1) n * phi (2 * j + 1) n + -1 * (4 * P (2 * j + 1)))
          = (P (2 * j + 1) + -1) * (phi (2 * j + 1) (ring (2 * (2 * j + 1) + 1 + 1)).one * phi (2 * j + 1) (ring (2 * (2 * j + 1) + 1 + 1)).one + phi (2 * j + 1) n * phi (2 * j + 1) n) + -1 * (2 * (P (2 * j + 1) * ((1 + 1) * (P (2 * j + 1) + -1)))) := by
        lib_int
      rw [this, ← hG]; lib_int
    have hP1 : P (2 * j + 1) + -1 ≠ 0 := fun e => by
      have e1 : P (2 * j + 1) = 1 := by
        have : P (2 * j + 1) = (P (2 * j + 1) + -1) + 1 := by lib_int
        rw [this, e]; rfl
      have e3 := Int.ofNat.inj (show Int.ofNat (2 * (2 * j + 1) + 1 + 1 + 1) = Int.ofNat 1 from e1)
      exact Nat.noConfusion (Nat.succ.inj e3)
    cases LibInt.int_noZD _ _ hz with
    | inl h => exact absurd h hP1
    | inr h =>
      calc phi (2 * j + 1) (ring (2 * (2 * j + 1) + 1 + 1)).one * phi (2 * j + 1) (ring (2 * (2 * j + 1) + 1 + 1)).one + phi (2 * j + 1) n * phi (2 * j + 1) n
          = (phi (2 * j + 1) (ring (2 * (2 * j + 1) + 1 + 1)).one * phi (2 * j + 1) (ring (2 * (2 * j + 1) + 1 + 1)).one + phi (2 * j + 1) n * phi (2 * j + 1) n + -1 * (4 * P (2 * j + 1))) + 4 * P (2 * j + 1) := by lib_int
        _ = 4 * P (2 * j + 1) := by rw [h]; exact LibInt.intR.zero_add _

theorem phi_sq_one (mm : Nat) (hpr : ∀ d, Dv d (2 * mm + 1 + 1 + 1) → d = 1 ∨ d = 2 * mm + 1 + 1 + 1) (n : Zp (2 * mm + 1 + 1))
    (hn0 : n ≠ (ring (2 * mm + 1 + 1)).zero) (hn : chi mm n = -1) (D : Zp (2 * mm + 1 + 1)) (hD : chi mm D = 1) :
    phi mm D * phi mm D = phi mm (ring (2 * mm + 1 + 1)).one * phi mm (ring (2 * mm + 1 + 1)).one := by
  have hc := phi_class mm hpr n hn0 hn D
  rw [hD] at hc
  exact FinPrimary.two_cancel _ _ (hc.trans (by lib_int))

theorem phi_sq_neg (mm : Nat) (hpr : ∀ d, Dv d (2 * mm + 1 + 1 + 1) → d = 1 ∨ d = 2 * mm + 1 + 1 + 1) (n : Zp (2 * mm + 1 + 1))
    (hn0 : n ≠ (ring (2 * mm + 1 + 1)).zero) (hn : chi mm n = -1) (D : Zp (2 * mm + 1 + 1)) (hD : chi mm D = -1) :
    phi mm D * phi mm D = phi mm n * phi mm n := by
  have hc := phi_class mm hpr n hn0 hn D
  rw [hD] at hc
  exact FinPrimary.two_cancel _ _ (hc.trans (by lib_int))

theorem s2_phi {R : Type} [DecidableEq R] (F : FinField.FF R) :
    sumK LibInt.intR (fun x => FinField.chi F (FinSign.f F x)) F.els = FinConic.phi F (F.K.neg F.K.one) := by
  apply sumK_ext
  intro x
  show FinField.chi F (CMInert.cub F.K x) = FinField.chi F (F.K.mul x (F.K.add (F.K.mul x x) (F.K.neg F.K.one)))
  rw [show CMInert.cub F.K x = F.K.mul x (F.K.add (F.K.mul x x) (F.K.neg F.K.one)) by
    show F.K.add (F.K.mul (F.K.mul x x) x) (F.K.neg x) = _; lib_ring]

theorem s1_phi (mm : Nat) :
    sumK LibInt.intR (fun x => chi mm (CMInert.cub (ring (2 * mm + 1 + 1)) x)) (L mm) = phi mm ((ring (2 * mm + 1 + 1)).neg (ring (2 * mm + 1 + 1)).one) := by
  apply sumK_ext
  intro x
  show chi mm (CMInert.cub (ring (2 * mm + 1 + 1)) x) = chi mm ((ring (2 * mm + 1 + 1)).mul x ((ring (2 * mm + 1 + 1)).add ((ring (2 * mm + 1 + 1)).mul x x) ((ring (2 * mm + 1 + 1)).neg (ring (2 * mm + 1 + 1)).one)))
  rw [show CMInert.cub (ring (2 * mm + 1 + 1)) x = (ring (2 * mm + 1 + 1)).mul x ((ring (2 * mm + 1 + 1)).add ((ring (2 * mm + 1 + 1)).mul x x) ((ring (2 * mm + 1 + 1)).neg (ring (2 * mm + 1 + 1)).one)) by
    show (ring (2 * mm + 1 + 1)).add ((ring (2 * mm + 1 + 1)).mul ((ring (2 * mm + 1 + 1)).mul x x) x) ((ring (2 * mm + 1 + 1)).neg x) = _; lib_ring]

/-- **p ≡ 1 (mod 4).** -/
theorem family_hd_split (j : Nat) (hpr : ∀ d, Dv d (2 * (2 * j + 1) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * j + 1) + 1 + 1 + 1)) (n : Zp (2 * (2 * j + 1) + 1 + 1))
    (hn : Frobenius.pw (ring (2 * (2 * j + 1) + 1 + 1)) n (2 * j + 1 + 1) = (ring (2 * (2 * j + 1) + 1 + 1)).neg (ring (2 * (2 * j + 1) + 1 + 1)).one) (D : Zp (2 * (2 * j + 1) + 1 + 1)) (hD : D ≠ (ring (2 * (2 * j + 1) + 1 + 1)).zero) :
    -FinConic.phi (Fq2Inst.ffq (2 * j + 1) hpr n hn) (D, (ring (2 * (2 * j + 1) + 1 + 1)).zero) = phi (2 * j + 1) D * phi (2 * j + 1) D + -2 * Int.ofNat (2 * (2 * j + 1) + 1 + 1 + 1) := by
  have hn0 : n ≠ (ring (2 * (2 * j + 1) + 1 + 1)).zero := fun e => by
    rw [e, HasseCM.pw_zero_succ] at hn
    apply PowerSum.one_ne_zero' (2 * (2 * j + 1) + 1)
    have := congrArg (fun z => (ring (2 * (2 * j + 1) + 1 + 1)).add z (ring (2 * (2 * j + 1) + 1 + 1)).one) hn
    exact (((ring (2 * (2 * j + 1) + 1 + 1)).zero_add (ring (2 * (2 * j + 1) + 1 + 1)).one).symm.trans (this.trans (DetChain.add_neg' (ring (2 * (2 * j + 1) + 1 + 1)) (ring (2 * (2 * j + 1) + 1 + 1)).one)))
  have hnc : chi (2 * j + 1) n = -1 := chi_neg_of (2 * j + 1) n hn0 (fun e => ne_pm (2 * j + 1) (hn.symm.trans e))
  match GaussSplit.split_one_mod_four j hpr with
  | ⟨i, hi⟩ =>
    have hi0 : i ≠ (ring (2 * (2 * j + 1) + 1 + 1)).zero := fun e => by
      rw [e, DetChain.zero_mul'] at hi
      apply PowerSum.one_ne_zero' (2 * (2 * j + 1) + 1)
      have := congrArg (fun z => (ring (2 * (2 * j + 1) + 1 + 1)).add z (ring (2 * (2 * j + 1) + 1 + 1)).one) hi
      exact (((ring (2 * (2 * j + 1) + 1 + 1)).zero_add (ring (2 * (2 * j + 1) + 1 + 1)).one).symm.trans (this.trans (DetChain.add_neg' (ring (2 * (2 * j + 1) + 1 + 1)) (ring (2 * (2 * j + 1) + 1 + 1)).one)))
    have hm1 : chi (2 * j + 1) ((ring (2 * (2 * j + 1) + 1 + 1)).neg (ring (2 * (2 * j + 1) + 1 + 1)).one) = 1 := by rw [← hi]; exact chi_sq (2 * j + 1) hpr i hi0
    have hHD := HasseDavenport.hasse_davenport_split j hpr n hn
    rw [s2_phi, s1_phi] at hHD
    have h4 := four_p j hpr n hn0 hnc
    have hP : P (2 * j + 1) = Int.ofNat (2 * (2 * j + 1) + 1 + 1 + 1) := rfl
    have hm1sq := phi_sq_one (2 * j + 1) hpr n hn0 hnc _ hm1
    rw [FamilyFq.family_split j hpr n hn D hD]
    have hcD : chi (2 * j + 1) ((ring (2 * (2 * j + 1) + 1 + 1)).neg D) = chi (2 * j + 1) D := by
      rw [show (ring (2 * (2 * j + 1) + 1 + 1)).neg D = (ring (2 * (2 * j + 1) + 1 + 1)).mul ((ring (2 * (2 * j + 1) + 1 + 1)).neg (ring (2 * (2 * j + 1) + 1 + 1)).one) D by lib_ring, chi_mul (2 * j + 1) hpr, hm1]; lib_int
    rw [hcD]
    have hneg : (Fq2.G2 (2 * (2 * j + 1) + 1) n).neg (Fq2.G2 (2 * (2 * j + 1) + 1) n).one = (Fq2Inst.ffq (2 * j + 1) hpr n hn).K.neg (Fq2Inst.ffq (2 * j + 1) hpr n hn).K.one := rfl
    rw [hneg]
    generalize FinConic.phi (Fq2Inst.ffq (2 * j + 1) hpr n hn) ((Fq2Inst.ffq (2 * j + 1) hpr n hn).K.neg (Fq2Inst.ffq (2 * j + 1) hpr n hn).K.one) = S2 at hHD ⊢
    cases JacobClass.chi_cases (2 * j + 1) D with
    | inl h0 => exact absurd h0 (chi_ne_zero (2 * j + 1) D hD)
    | inr h =>
      cases h with
      | inl h1 =>
        rw [h1, phi_sq_one (2 * j + 1) hpr n hn0 hnc D h1, ← hm1sq]
        rw [show -(1 * S2) = -S2 by lib_int, hHD]; lib_int
      | inr h1 =>
        rw [h1, phi_sq_neg (2 * j + 1) hpr n hn0 hnc D h1]
        have e : phi (2 * j + 1) n * phi (2 * j + 1) n = 4 * Int.ofNat (2 * (2 * j + 1) + 1 + 1 + 1) - phi (2 * j + 1) ((ring (2 * (2 * j + 1) + 1 + 1)).neg (ring (2 * (2 * j + 1) + 1 + 1)).one) * phi (2 * j + 1) ((ring (2 * (2 * j + 1) + 1 + 1)).neg (ring (2 * (2 * j + 1) + 1 + 1)).one) := by
          rw [hm1sq, ← hP, ← h4]; lib_int
        rw [e]
        calc -(-1 * S2) = -(-S2) := by lib_int
          _ = -((-phi (2 * j + 1) ((ring (2 * (2 * j + 1) + 1 + 1)).neg (ring (2 * (2 * j + 1) + 1 + 1)).one)) * (-phi (2 * j + 1) ((ring (2 * (2 * j + 1) + 1 + 1)).neg (ring (2 * (2 * j + 1) + 1 + 1)).one)) + -2 * Int.ofNat (2 * (2 * j + 1) + 1 + 1 + 1)) := by rw [hHD]
          _ = _ := by lib_int

/-- **p ≡ 3 (mod 4).** -/
theorem family_hd_inert (j : Nat) (hpr : ∀ d, Dv d (2 * (2 * j) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * j) + 1 + 1 + 1)) (n : Zp (2 * (2 * j) + 1 + 1))
    (hn : Frobenius.pw (ring (2 * (2 * j) + 1 + 1)) n (2 * j + 1) = (ring (2 * (2 * j) + 1 + 1)).neg (ring (2 * (2 * j) + 1 + 1)).one) (D : Zp (2 * (2 * j) + 1 + 1)) (hD : D ≠ (ring (2 * (2 * j) + 1 + 1)).zero) :
    -FinConic.phi (Fq2Inst.ffq (2 * j) hpr n hn) (D, (ring (2 * (2 * j) + 1 + 1)).zero) = phi (2 * j) D * phi (2 * j) D + -2 * Int.ofNat (2 * (2 * j) + 1 + 1 + 1) := by
  rw [QuarticFamily.phi_inert (2 * j) hpr (BaseChange.chi_negone j (TwoPower.negone_odd' (2 * (2 * j) + 1) j)) D,
    FamilyFq.family_inert j hpr n hn D hD]
  have h := InertFq.inert_fq j hpr n hn
  rw [s2_phi, InertFq.s1_inert j hpr] at h
  exact h

/-- **a(𝔽_{p²}) = a_p² − 2p for y² = x³ + Dx, every D ∈ 𝔽_p^×, every odd prime p.** -/
theorem family_hd (k : Nat) (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = (2 * k + 1 + 1 + 1)) (n : Zp (2 * k + 1 + 1))
    (hn : Frobenius.pw (ring (2 * k + 1 + 1)) n (k + 1) = (ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) (D : Zp (2 * k + 1 + 1)) (hD : D ≠ (ring (2 * k + 1 + 1)).zero) :
    -FinConic.phi (Fq2Inst.ffq k hpr n hn) (D, (ring (2 * k + 1 + 1)).zero) = phi k D * phi k D + -2 * Int.ofNat (2 * k + 1 + 1 + 1) := by
  cases EveryPrime.nat_even_odd k with
  | inl h => match h with | ⟨j, hj⟩ => subst hj; exact family_hd_inert j hpr n hn D hD
  | inr h => match h with | ⟨j, hj⟩ => subst hj; exact family_hd_split j hpr n hn D hD

end FamilyHD

#print axioms FamilyHD.four_p
#print axioms FamilyHD.phi_sq_one
#print axioms FamilyHD.phi_sq_neg
#print axioms FamilyHD.s2_phi
#print axioms FamilyHD.s1_phi
#print axioms FamilyHD.family_hd_split
#print axioms FamilyHD.family_hd_inert
#print axioms FamilyHD.family_hd

lang/Fermat27a.lean

lean_all/lang/Fermat27a.lean

/-
  lang/Fermat27a — T43, cycle 271: modularity check for the Fermat cubic (the curve 27a, conductor 27).

  f = η(3z)²η(9z)² = q Π_{n≥1} (1 − q^{3n})² (1 − q^{9n})², the weight-2 newform of level 27.
  For every prime p < 200, p ≠ 3, the coefficient of q^p equals p + 1 − #X(𝔽_p) for the projective Fermat cubic
  X : x³ + y³ = z³ (affine points of x³ + y³ = 1 plus the points [x : 1 : 0] with x³ = −1):
      modular_27a     checked by evaluation (rfl), as ESChain.modular_check_fast does for 11a.
  Together with FermatCubic / CubicTwistCount (#X = p + 1 + Tr J for p ≡ 1 mod 3), this identifies the coefficients of f with
  −Tr J at those primes.
  Lean 4 core only, evaluation by rfl, no axioms.
-/
import ESChain

namespace Fermat27a

open FTAChain FrobChain QRChain ESChain

def affF (p : Nat) : Nat := sumTo p (fun x => sumTo p (fun y => b2n (Nat.beq ((x * x * x + y * y * y) % p) (1 % p))))
def infF (p : Nat) : Nat := sumTo p (fun x => b2n (Nat.beq ((x * x * x + 1) % p) 0))

/-- a_p = p + 1 − #X(𝔽_p). -/
def apF (p : Nat) : Int := Int.ofNat (p + 1) - Int.ofNat (affF p + infF p)

def prod3 : Nat → List Int → List Int
  | 0, l => l
  | n + 1, l => prod3 n (mul1m (3 * (n + 1)) (mul1m (3 * (n + 1)) l))
def prod9 : Nat → List Int → List Int
  | 0, l => l
  | n + 1, l => prod9 n (mul1m (9 * (n + 1)) (mul1m (9 * (n + 1)) l))

/-- coefficients of q⁰ … q^{N−1} of Π (1 − q^{3n})²(1 − q^{9n})². -/
def eta27 (N : Nat) : List Int := prod9 N (prod3 N (oneS N))

def coef27 (p : Nat) : Int := nth (eta27 200) (p - 1)

def primes200' : List Nat :=
  [2, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83,
   89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179,
   181, 191, 193, 197, 199]

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
/-- **Modularity of the Fermat cubic, checked at every prime p < 200, p ≠ 3.** -/
theorem modular_27a : primes200'.map coef27 = primes200'.map apF := by rfl

end Fermat27a

#print axioms Fermat27a.modular_27a

lang/FermatCubic.lean

lean_all/lang/FermatCubic.lean

/-
  lang/FermatCubic — T43, cycle 218: points of the Fermat cubic x³ + y³ = 1 over 𝔽_p through the cubic Jacobi sum.

  p = 6n + 7, χ the cubic character, J = a + bω (N(J) = p). N(a) = #{x : x³ = a} = 1 + χ(a) + χ̄(a), so
      #{(x, y) : x³ + y³ = 1} = Σ_{a+b=1} N(a)N(b) = p − 2 + J + J̄ = p − 2 + 2a − b,
  and (2a − b)² + 3b² = 4p: the trace of Frobenius on the Fermat cubic is J + J̄ with |J + J̄| ≤ 2√p.
    cube_root, three_roots, cube_count, step1, step2, fermat_count, fermat_hasse
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.JacobiNorm
import lang.DLog
import lang.PrimRoot
import lang.HeckePrime

namespace FermatCubic

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius CubicChar JacobiSum JacobiNorm

variable (n : Nat)

/-- a^N = 1, a ≠ 0 ⇒ a is a cube. -/
theorem cube_root (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (a : Zp (6 * n + 5 + 1)) (ha0 : a ≠ (ring (6 * n + 5 + 1)).zero) (ha : pw (ring (6 * n + 5 + 1)) a (2 * n + 2) = (ring (6 * n + 5 + 1)).one) : ∃ c, (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul c c) c = a := by
  match PrimRoot.primitive_root (6 * n + 5) hpr with
  | ⟨g, hg⟩ =>
    match DLog.mem_pows (6 * n + 5) g a _ (DLog.every_unit (6 * n + 5) hpr g hg a ha0) with
    | ⟨k, _, hk⟩ =>
      have h1 : pw (ring (6 * n + 5 + 1)) g (k * (2 * n + 2)) = (ring (6 * n + 5 + 1)).one := by rw [← TwoPower.pwpow, ← hk, ha]
      match hg.2 _ h1 with
      | ⟨c, hc⟩ =>
        have h3 : k = 3 * c := by
          apply Nat.eq_of_mul_eq_mul_right (Nat.zero_lt_succ (2 * n + 1))
          rw [hc]; lib_nat
        refine ⟨pw (ring (6 * n + 5 + 1)) g c, ?_⟩
        rw [hk, h3, show 3 * c = c * 3 by lib_nat, ← TwoPower.pwpow]
        show _ = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (ring (6 * n + 5 + 1)).one _) _) _
        lib_ring

def cube (x : Zp (6 * n + 5 + 1)) : Zp (6 * n + 5 + 1) := (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul x x) x

theorem cube_mul (x y : Zp (6 * n + 5 + 1)) : cube n ((ring (6 * n + 5 + 1)).mul x y) = (ring (6 * n + 5 + 1)).mul (cube n x) (cube n y) := by unfold cube; lib_ring

theorem w3' (w : Zp (6 * n + 5 + 1)) (_hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : cube n w = (ring (6 * n + 5 + 1)).one := by
  have : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (ring (6 * n + 5 + 1)).one w) w) w := rfl
  rw [this, (ring (6 * n + 5 + 1)).one_mul] at hw3; exact hw3

theorem cube_zero (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (x : Zp (6 * n + 5 + 1)) (h : cube n x = (ring (6 * n + 5 + 1)).zero) : x = (ring (6 * n + 5 + 1)).zero := by
  by_cases hx : x = (ring (6 * n + 5 + 1)).zero
  · exact hx
  · exfalso
    have h1 : (ring (6 * n + 5 + 1)).mul x x ≠ (ring (6 * n + 5 + 1)).zero := fun e => hx (cancel n hpr x x hx e)
    exact hx (cancel n hpr _ _ h1 (by unfold cube at h; exact h))

/-- the three cube roots of c³. -/
theorem three_roots (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (c : Zp (6 * n + 5 + 1)) (hc : c ≠ (ring (6 * n + 5 + 1)).zero) (x : Zp (6 * n + 5 + 1)) :
    delta (Cyclo3.cyc3 LibInt.intR) (cube n x) (cube n c) = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (delta (Cyclo3.cyc3 LibInt.intR) x c) (delta (Cyclo3.cyc3 LibInt.intR) x ((ring (6 * n + 5 + 1)).mul c w))) (delta (Cyclo3.cyc3 LibInt.intR) x ((ring (6 * n + 5 + 1)).mul c ((ring (6 * n + 5 + 1)).mul w w))) := by
  have hw0 : w ≠ (ring (6 * n + 5 + 1)).zero := w_ne_zero n w hw1 hw3
  have d1 : c ≠ (ring (6 * n + 5 + 1)).mul c w := fun e => hw1 (by
    have := cancel n hpr c ((ring (6 * n + 5 + 1)).add w ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one)) hc (by rw [show (ring (6 * n + 5 + 1)).mul c ((ring (6 * n + 5 + 1)).add w ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one)) = (ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).mul c w) ((ring (6 * n + 5 + 1)).neg c) by lib_ring, ← e]; lib_ring)
    exact eq_of_sub n _ _ this)
  have d2 : c ≠ (ring (6 * n + 5 + 1)).mul c ((ring (6 * n + 5 + 1)).mul w w) := fun e => ww_ne_one n w hw1 hw3 (by
    have := cancel n hpr c ((ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).mul w w) ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one)) hc (by rw [show (ring (6 * n + 5 + 1)).mul c ((ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).mul w w) ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one)) = (ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).mul c ((ring (6 * n + 5 + 1)).mul w w))  -- (+60 chars; full line in the Zenodo source)
    exact eq_of_sub n _ _ this)
  have d3 : (ring (6 * n + 5 + 1)).mul c w ≠ (ring (6 * n + 5 + 1)).mul c ((ring (6 * n + 5 + 1)).mul w w) := fun e => ww_ne_w n hpr w hw1 hw3 (by
    have := cancel n hpr c ((ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).mul w w) ((ring (6 * n + 5 + 1)).neg w)) hc (by rw [show (ring (6 * n + 5 + 1)).mul c ((ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).mul w w) ((ring (6 * n + 5 + 1)).neg w)) = (ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).mul c ((ring (6 * n + 5 + 1)).mul w w)) ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1  -- (+41 chars; full line in the Zenodo source)
    exact eq_of_sub n _ _ this)
  have hcw : cube n ((ring (6 * n + 5 + 1)).mul c w) = cube n c := by rw [cube_mul, w3' n w hw1 hw3]; lib_ring
  have hcww : cube n ((ring (6 * n + 5 + 1)).mul c ((ring (6 * n + 5 + 1)).mul w w)) = cube n c := by
    rw [cube_mul, cube_mul, w3' n w hw1 hw3]; lib_ring
  unfold delta
  by_cases hx : cube n x = cube n c
  · rw [ite_pos' _ _ hx]
    have hv := inv_exists (6 * n + 5) hpr c hc
    have ht : (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul x (pw (ring (6 * n + 5 + 1)) c (6 * n + 5))) ((ring (6 * n + 5 + 1)).mul x (pw (ring (6 * n + 5 + 1)) c (6 * n + 5)))) ((ring (6 * n + 5 + 1)).mul x (pw (ring (6 * n + 5 + 1)) c (6 * n + 5))) = (ring (6 * n + 5 + 1)).one := by
      have : (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul x (pw (ring (6 * n + 5 + 1)) c (6 * n + 5))) ((ring (6 * n + 5 + 1)).mul x (pw (ring (6 * n + 5 + 1)) c (6 * n + 5)))) ((ring (6 * n + 5 + 1)).mul x (pw (ring (6 * n + 5 + 1)) c (6 * n + 5)))
          = (ring (6 * n + 5 + 1)).mul (cube n x) (cube n (pw (ring (6 * n + 5 + 1)) c (6 * n + 5))) := by unfold cube; lib_ring
      rw [this, hx, ← cube_mul, (ring (6 * n + 5 + 1)).mul_comm, hv]; unfold cube; lib_ring
    have back : ∀ t, (ring (6 * n + 5 + 1)).mul x (pw (ring (6 * n + 5 + 1)) c (6 * n + 5)) = t → x = (ring (6 * n + 5 + 1)).mul c t := fun t e => by
      rw [← e, show (ring (6 * n + 5 + 1)).mul c ((ring (6 * n + 5 + 1)).mul x (pw (ring (6 * n + 5 + 1)) c (6 * n + 5))) = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (pw (ring (6 * n + 5 + 1)) c (6 * n + 5)) c) x by lib_ring, hv]; lib_ring
    rcases cube_cases n hpr w hw1 hw3 _ ht with h | h | h
    · have e := back _ h; rw [show (ring (6 * n + 5 + 1)).mul c (ring (6 * n + 5 + 1)).one = c by lib_ring] at e
      rw [ite_pos' _ _ e, ite_neg' _ _ (by rw [e]; exact d1), ite_neg' _ _ (by rw [e]; exact d2)]; lib_ring
    · have e := back _ h
      rw [ite_neg' _ _ (by rw [e]; exact fun e' => d1 e'.symm), ite_pos' _ _ e, ite_neg' _ _ (by rw [e]; exact d3)]; lib_ring
    · have e := back _ h
      rw [ite_neg' _ _ (by rw [e]; exact fun e' => d2 e'.symm), ite_neg' _ _ (by rw [e]; exact fun e' => d3 e'.symm), ite_pos' _ _ e]; lib_ring
  · rw [ite_neg' _ _ hx, ite_neg' _ _ (fun e => hx (by rw [e])), ite_neg' _ _ (fun e => hx (by rw [e, hcw])),
      ite_neg' _ _ (fun e => hx (by rw [e, hcww]))]; lib_ring

theorem sum_delta1 (c : Zp (6 * n + 5 + 1)) : sumK (Cyclo3.cyc3 LibInt.intR) (fun x => delta (Cyclo3.cyc3 LibInt.intR) x c) (LL n) = (Cyclo3.cyc3 LibInt.intR).one := CMInert.complete_allZ (6 * n + 5 + 1) (Cyclo3.cyc3 LibInt.intR) c

/-- **N(a) = #{x : x³ = a} = 1 + χ(a) + χ̄(a).** -/
theorem cube_count (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (a : Zp (6 * n + 5 + 1)) :
    sumK (Cyclo3.cyc3 LibInt.intR) (fun x => delta (Cyclo3.cyc3 LibInt.intR) (cube n x) a) (LL n) = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one (chi3 n w a)) (cb n w a) := by
  by_cases ha : a = (ring (6 * n + 5 + 1)).zero
  · rw [ha]
    rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun x => delta (Cyclo3.cyc3 LibInt.intR) x (ring (6 * n + 5 + 1)).zero) (fun x => by
      unfold delta
      by_cases hx : x = (ring (6 * n + 5 + 1)).zero
      · rw [ite_pos' _ _ hx, ite_pos' _ _ (by rw [hx]; unfold cube; lib_ring)]
      · rw [ite_neg' _ _ hx, ite_neg' _ _ (fun e => hx (cube_zero n hpr x e))]), sum_delta1]
    unfold cb; rw [chi3_zero]; show _ = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one (Cyclo3.cyc3 LibInt.intR).zero) ((Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).zero (Cyclo3.cyc3 LibInt.intR).zero); lib_ring
  · by_cases hN : pw (ring (6 * n + 5 + 1)) a (2 * n + 2) = (ring (6 * n + 5 + 1)).one
    · match cube_root n hpr a ha hN with
      | ⟨c, hc⟩ =>
        have hc0 : c ≠ (ring (6 * n + 5 + 1)).zero := fun e => ha (by rw [← hc, e]; lib_ring)
        have hc' : cube n c = a := hc
        rw [← hc']
        rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun x => three_roots n hpr w hw1 hw3 c hc0 x), sumK_add, sumK_add, sum_delta1, sum_delta1, sum_delta1]
        have h1 : chi3 n w (cube n c) = (Cyclo3.cyc3 LibInt.intR).one := by
          rw [← hc'] at hN; exact val_of n hpr w hw1 hw3 _ (by rw [hc']; exact ha) 0 (Nat.zero_lt_succ 2) hN
        unfold cb; rw [h1]; lib_ring
    · have hz : ∀ x, delta (Cyclo3.cyc3 LibInt.intR) (cube n x) a = (Cyclo3.cyc3 LibInt.intR).zero := fun x => by
        unfold delta
        rw [ite_neg' _ _ (fun e => hN (by
          have hx0 : x ≠ (ring (6 * n + 5 + 1)).zero := fun e0 => ha (by rw [← e, e0]; unfold cube; lib_ring)
          rw [← e, show cube n x = pw (ring (6 * n + 5 + 1)) x 3 by unfold cube; show _ = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (ring (6 * n + 5 + 1)).one x) x) x; lib_ring,
            TwoPower.pwpow, show 3 * (2 * n + 2) = 6 * n + 5 + 1 by lib_nat]
          exact unit_pow (6 * n + 5) hpr x hx0))]
      rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ hz, HeckePrime.zsumR (Cyclo3.cyc3 LibInt.intR)]
      match exists_i n hpr w hw1 hw3 a ha with
      | ⟨i, hi, ei⟩ =>
        unfold cb; rw [val_of n hpr w hw1 hw3 a ha i hi ei]
        match i, hi with
        | 0, _ => exact absurd ei hN
        | 1, _ => rfl
        | 2, _ => rfl
        | k + 3, hk => exact absurd hk (fun h => Nat.lt_irrefl 3 (Nat.lt_of_le_of_lt (Nat.le_add_left 3 k) h))

def g3 (w a : Zp (6 * n + 5 + 1)) : Int × Int := (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one (chi3 n w a)) (cb n w a)
def om1 (a : Zp (6 * n + 5 + 1)) : Zp (6 * n + 5 + 1) := (ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).neg a)

/-- the number of points on x³ + y³ = 1 (in ℤ[ω], first component). -/
def Fc : Int × Int := sumK (Cyclo3.cyc3 LibInt.intR) (fun x => sumK (Cyclo3.cyc3 LibInt.intR) (fun y => delta (Cyclo3.cyc3 LibInt.intR) ((ring (6 * n + 5 + 1)).add (cube n x) (cube n y)) (ring (6 * n + 5 + 1)).one) (LL n)) (LL n)

theorem refl1 (G : Zp (6 * n + 5 + 1) → Int × Int) : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => G (om1 n a)) (LL n) = sumK (Cyclo3.cyc3 LibInt.intR) G (LL n) :=
  reindex (Cyclo3.cyc3 LibInt.intR) _ (CMInert.complete_allZ (6 * n + 5 + 1) (Cyclo3.cyc3 LibInt.intR)) (om1 n) (om1 n) (fun x => by unfold om1; lib_ring) (fun y => by unfold om1; lib_ring) G

theorem step1 (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (x : Zp (6 * n + 5 + 1)) :
    sumK (Cyclo3.cyc3 LibInt.intR) (fun y => delta (Cyclo3.cyc3 LibInt.intR) ((ring (6 * n + 5 + 1)).add (cube n x) (cube n y)) (ring (6 * n + 5 + 1)).one) (LL n) = g3 n w (om1 n (cube n x)) := by
  unfold g3; rw [← cube_count n hpr w hw1 hw3]
  exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun y => by
    unfold delta om1
    by_cases h : (ring (6 * n + 5 + 1)).add (cube n x) (cube n y) = (ring (6 * n + 5 + 1)).one
    · rw [ite_pos' _ _ h, ite_pos' _ _ (by rw [← h]; lib_ring)]
    · rw [ite_neg' _ _ h, ite_neg' _ _ (fun e => h (by rw [e]; lib_ring))]) _

theorem step2 (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (h : Zp (6 * n + 5 + 1) → Int × Int) :
    sumK (Cyclo3.cyc3 LibInt.intR) (fun x => h (cube n x)) (LL n) = sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (g3 n w a) (h a)) (LL n) := by
  have e1 : sumK (Cyclo3.cyc3 LibInt.intR) (fun x => h (cube n x)) (LL n) = sumK (Cyclo3.cyc3 LibInt.intR) (fun x => sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) a (cube n x)) (h a)) (LL n)) (LL n) :=
    sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun x => (pick (Cyclo3.cyc3 LibInt.intR) _ (CMInert.complete_allZ (6 * n + 5 + 1) (Cyclo3.cyc3 LibInt.intR)) h (cube n x)).symm) _
  rw [e1, sumK_swap]
  exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun a => by
    rw [← sumK_mul]
    unfold g3; rw [← cube_count n hpr w hw1 hw3 a]
    exact congrArg (fun z => (Cyclo3.cyc3 LibInt.intR).mul z (h a)) (sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun x => delta_symm (Cyclo3.cyc3 LibInt.intR) a (cube n x)) _)) _

/-- **#(x³ + y³ = 1) = p − 2 + J + J̄**, in ℤ[ω]. -/
theorem fermat_count (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) :
    Fc n = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1), 0) ((Cyclo3.cyc3 LibInt.intR).add (J n w) (cconj (J n w)))) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one (Cyclo3.cyc3 LibInt.intR).one)) := by
  unfold Fc
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun x => step1 n hpr w hw1 hw3 x), step2 n hpr w hw1 hw3 (fun a => g3 n w (om1 n a))]
  -- expand (1 + A + B)(1 + A' + B')
  have hexp : ∀ a, (Cyclo3.cyc3 LibInt.intR).mul (g3 n w a) (g3 n w (om1 n a)) =
      (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one ((Cyclo3.cyc3 LibInt.intR).add (chi3 n w (om1 n a)) (cb n w (om1 n a)))) ((Cyclo3.cyc3 LibInt.intR).add (chi3 n w a) (cb n w a)))
        ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (chi3 n w (om1 n a))) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w a) (cb n w (om1 n a)))))
        ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (cb n w (om1 n a))) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w a) (chi3 n w (om1 n a)))) := fun a => by unfold g3; lib_ring
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ hexp]
  rw [sumK_add, sumK_add, sumK_add, sumK_add, sumK_add, sumK_add, sumK_add, sumK_add]
  have S1 : sumK (Cyclo3.cyc3 LibInt.intR) (fun _ : Zp (6 * n + 5 + 1) => (Cyclo3.cyc3 LibInt.intR).one) (LL n) = (Int.ofNat (6 * n + 5 + 1 + 1), 0) := JacobiSum.sum_one n _
  have SA' : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => chi3 n w (om1 n a)) (LL n) = (Cyclo3.cyc3 LibInt.intR).zero := (refl1 n (chi3 n w)).trans (chi3_sum n hpr w hw1 hw3)
  have SB' : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => cb n w (om1 n a)) (LL n) = (Cyclo3.cyc3 LibInt.intR).zero := (refl1 n (cb n w)).trans (cb_sum n hpr w hw1 hw3)
  have SAA : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (chi3 n w (om1 n a))) (LL n) = J n w := rfl
  have SBB : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (cb n w a) (cb n w (om1 n a))) (LL n) = cconj (J n w) := by
    rw [← J2_conj n hpr w hw1 hw3]
    exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun a => by rw [(cb_eq n hpr w hw1 hw3 a).1, (cb_eq n hpr w hw1 hw3 _).1]; rfl) _
  have SAB : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (cb n w (om1 n a))) (LL n) = (Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one := by
    rw [← JacobiSum.shift1 n (fun a => (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (cb n w (om1 n a)))]
    rw [← D_ne n hpr w hw1 hw3 (ring (6 * n + 5 + 1)).one (PowerSum.one_ne_zero' _)]
    exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun y => by
      show (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one y)) (cb n w ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one y)))) = _
      rw [show (ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one y)) = (ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).zero ((ring (6 * n + 5 + 1)).neg y) by lib_ring, neg_b n hpr w hw1 hw3]) _
  have SBA : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (cb n w a) (chi3 n w (om1 n a))) (LL n) = (Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one := by
    rw [← refl1 n (fun a => (Cyclo3.cyc3 LibInt.intR).mul (cb n w a) (chi3 n w (om1 n a))), ← SAB]
    exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun a => by
      show (Cyclo3.cyc3 LibInt.intR).mul (cb n w (om1 n a)) (chi3 n w (om1 n (om1 n a))) = _
      rw [show om1 n (om1 n a) = a by unfold om1; lib_ring, (Cyclo3.cyc3 LibInt.intR).mul_comm]) _
  have SA : sumK (Cyclo3.cyc3 LibInt.intR) (chi3 n w) (LL n) = (Cyclo3.cyc3 LibInt.intR).zero := chi3_sum n hpr w hw1 hw3
  rw [S1, SA', SB', SA, cb_sum n hpr w hw1 hw3, SAA, SBB, SAB, SBA]
  lib_ring

/-- **The Hasse bound for the Fermat cubic**: with J = a + bω, #(x³ + y³ = 1) = p − 2 + (2a − b) and (2a − b)² + 3b² = 4p. -/
theorem fermat_hasse (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) :
    (Fc n).1 = Int.ofNat (6 * n + 5 + 1 + 1) + (2 * (J n w).1 - (J n w).2) - 2 ∧
    (2 * (J n w).1 - (J n w).2) * (2 * (J n w).1 - (J n w).2) + 3 * ((J n w).2 * (J n w).2) = 4 * Int.ofNat (6 * n + 5 + 1 + 1) := by
  refine ⟨?_, ?_⟩
  · rw [fermat_count n hpr w hw1 hw3]
    show Int.ofNat (6 * n + 5 + 1 + 1) + ((J n w).1 + ((J n w).1 - (J n w).2)) + -(1 + 1) = _
    lib_int
  · rw [← jacobi_norm n hpr w hw1 hw3]; lib_int

end FermatCubic

#print axioms FermatCubic.cube_root
#print axioms FermatCubic.cube_mul
#print axioms FermatCubic.w3'
#print axioms FermatCubic.cube_zero
#print axioms FermatCubic.three_roots
#print axioms FermatCubic.sum_delta1
#print axioms FermatCubic.cube_count
#print axioms FermatCubic.refl1
#print axioms FermatCubic.step1
#print axioms FermatCubic.step2
#print axioms FermatCubic.fermat_count
#print axioms FermatCubic.fermat_hasse

lang/FermatHecke.lean

lean_all/lang/FermatHecke.lean

/-
  lang/FermatHecke — T47/T50, cycle 618: **27a (the Fermat cubic x³ + y³ = z³) at every prime p ≡ 1 (mod 3): a_p = c(p)**,
  where c(n) = Σ_{α ∈ ℤ[ω], α ≡ 1 (mod 3), N(α) = n} α is the theta series of the Hecke character α ↦ α of ℚ(√−3) (HeckeMult3).

  p = 6n + 7, χ the cubic character, J = J(χ, χ) (JacobiNorm: N(J) = p; JacobiPrimary: J ≡ −1 mod 3). π := −J is primary of norm p,
  so c(p) = π + π̄ (HeckePrime3.cw3_pk' at k = 1) = J̄-trace with sign: c(p) = −(2a − b) for J = a + bω. FermatCubic.fermat_hasse:
      #{x³ + y³ = 1}(𝔽_p) = p − 2 + (2a − b) = p − 2 − c(p),
  and with the three points at infinity, #{x³ + y³ = z³}(ℙ²(𝔽_p)) = p + 1 − c(p).
    neg_prim, tg_one, cp_eq, fermat_hecke
  With HeckeMult3 (c multiplicative) and HeckePrime3 (Hecke recursion at p^k) this gives the Hecke side of 27a at every split prime.
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.HeckePrime3
import lang.FermatCubic
import lang.CubicTwo

namespace FermatHecke

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius CubicChar JacobiSum JacobiNorm EisEuclid EisPrim EisPrime3 HeckeMult3
open LibRing (castZ castN)

variable (n : Nat)

theorem tg_one {R : Type} (K : CRing R) (A B : R) : HeckePrime.Tg K A B 1 = K.add A B := by
  rw [HeckePrime.Tg_succ]
  show K.add (K.mul A (K.add (K.mul K.one K.one) K.zero)) (K.mul K.one B) = K.add A B
  lib_ring

/-- −J is primary of norm p. -/
theorem neg_prim (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1))
    (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) :
    Prim3 (-(J n w).1, -(J n w).2) ∧ nrm (-(J n w).1, -(J n w).2) = 6 * n + 5 + 1 + 1 := by
  have hp := JacobiPrimary.jacobi_primary n hpr w hw1 hw3
  have h1 : castZ (ring 2) ((J n w).1 + 1) = (ring 2).zero := by
    rw [IntCast.castZ_add, hp.1, show castZ (ring 2) 1 = (ring 2).one from add_zero' (ring 2) (ring 2).one]; lib_ring
  obtain ⟨u, hu⟩ := CubicTwo.cast_zero_gen 2 _ h1
  obtain ⟨v, hv⟩ := CubicTwo.cast_zero_gen 2 _ hp.2
  refine ⟨⟨-u, -v, ?_, ?_⟩, ?_⟩
  · show -(J n w).1 = 3 * -u + 1
    have e : (J n w).1 + 1 = 3 * u := hu
    calc -(J n w).1 = -((J n w).1 + 1) + 1 := by lib_int
      _ = _ := by rw [e]; lib_int
  · show -(J n w).2 = 3 * -v
    have e : (J n w).2 = 3 * v := hv
    rw [e]; lib_int
  · apply Int.ofNat.inj
    rw [nrm_cast, ← jacobi_norm n hpr w hw1 hw3]
    show -(J n w).1 * -(J n w).1 + -(-(J n w).1 * -(J n w).2) + -(J n w).2 * -(J n w).2 = _
    lib_int

/-- **c(p) = −(2a − b)** for J = a + bω. -/
theorem cp_eq (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1))
    (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) :
    cw3 (fun z => z) (6 * n + 5 + 1 + 1) = (-(2 * (J n w).1 - (J n w).2), 0) := by
  have np := neg_prim n hpr w hw1 hw3
  have h := HeckePrime3.cw3_pk' (6 * n + 5) (fun z => z) (fun _ _ => rfl) rfl hpr _ np.2 np.1 1
  rw [show (6 * n + 5 + 1 + 1) ^ 1 = 6 * n + 5 + 1 + 1 from Nat.one_mul _, tg_one] at h
  rw [h]
  apply Prod.ext
  · show -(J n w).1 + (-(J n w).1 + -(-(J n w).2)) = -(2 * (J n w).1 - (J n w).2); lib_int
  · show -(J n w).2 + -(-(J n w).2) = 0; lib_int

/-- **#{x³ + y³ = 1}(𝔽_p) = p − 2 − c(p)**, i.e. the projective Fermat cubic (27a) has p + 1 − c(p) points. -/
theorem fermat_hecke (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1))
    (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) :
    (FermatCubic.Fc n).1 + 3 = Int.ofNat (6 * n + 5 + 1 + 1) + 1 - (cw3 (fun z => z) (6 * n + 5 + 1 + 1)).1 ∧
      (cw3 (fun z => z) (6 * n + 5 + 1 + 1)).2 = 0 := by
  rw [cp_eq n hpr w hw1 hw3]
  refine ⟨?_, rfl⟩
  rw [(FermatCubic.fermat_hasse n hpr w hw1 hw3).1]
  show _ = Int.ofNat (6 * n + 5 + 1 + 1) + 1 - -(2 * (J n w).1 - (J n w).2)
  lib_int

end FermatHecke

#print axioms FermatHecke.tg_one
#print axioms FermatHecke.neg_prim
#print axioms FermatHecke.cp_eq
#print axioms FermatHecke.fermat_hecke

lang/FermatInert3.lean

lean_all/lang/FermatInert3.lean

/-
  lang/FermatInert3 — T47/T50, cycle 619: **27a at the primes p ≡ 2 (mod 3)**: a_p = 0 = c(p).

    en_mod3        N(a + bω) = a² − ab + b² is never ≡ 2 (mod 3)                      (residues of a, b mod 3)
    cw3_zero       c_W(n) = 0 for every n ≡ 2 (mod 3) and every weight W               (no α of norm n)
    ones_allZ      Σ_{x ∈ 𝔽_p} 1 = p
    fermat_inert   p = 3t + 2 prime: #{x³ + y³ = 1}(𝔽_p) = p        (x ↦ x³ is a bijection, CMEisen.cube_inv/inv_cube)
    fermat27_inert #{x³ + y³ = z³}(ℙ²(𝔽_p)) = p + 1 = p + 1 − c(p)    (one point at infinity: x = −y)
  With FermatHecke (p ≡ 1 mod 3) this gives a_p(27a) = c(p) at every prime p ≠ 3.
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.FermatHecke
import lang.CMEisen

namespace FermatInert3

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius EisEuclid EisPrim HeckeMult3

/-- N(z) ≢ 2 (mod 3). -/
theorem en_mod3 (z : Int × Int) (k : Int) (h : en z = 3 * k + 2) : False := by
  obtain ⟨a, b⟩ := z
  have hn : a * a + -(a * b) + b * b = 3 * k + 2 := h
  obtain ⟨e, he⟩ := res3 a
  obtain ⟨f, hf⟩ := res3 b
  have r0 : ∀ K : Int, a * a + -(a * b) + b * b = 3 * K → False := fun K hK =>
    (no3 (K + -k)).2 (by
      calc 3 * (K + -k) = 3 * K + -(3 * k + 2) + 2 := by lib_int
        _ = 2 := by rw [← hK, ← hn]; lib_int)
  have r1 : ∀ K : Int, a * a + -(a * b) + b * b = 3 * K + 1 → False := fun K hK =>
    (no3 (K + -k)).1 (by
      calc 3 * (K + -k) = (3 * K + 1) + -(3 * k + 2) + 1 := by lib_int
        _ = 1 := by rw [← hK, ← hn]; lib_int)
  rcases he with ha | ha | ha <;> rcases hf with hb | hb | hb
  · exact r0 (3 * (e * e + -(e * f) + f * f)) (by rw [ha, hb]; lib_int)
  · exact r1 (3 * (e * e + -(e * f) + f * f) + -e + 2 * f) (by rw [ha, hb]; lib_int)
  · exact r1 (3 * (e * e + -(e * f) + f * f) + e + -(2 * f)) (by rw [ha, hb]; lib_int)
  · exact r1 (3 * (e * e + -(e * f) + f * f) + 2 * e + -f) (by rw [ha, hb]; lib_int)
  · exact r1 (3 * (e * e + -(e * f) + f * f) + e + f) (by rw [ha, hb]; lib_int)
  · exact r0 (3 * (e * e + -(e * f) + f * f) + 3 * e + -(3 * f) + 1) (by rw [ha, hb]; lib_int)
  · exact r1 (3 * (e * e + -(e * f) + f * f) + -(2 * e) + f) (by rw [ha, hb]; lib_int)
  · exact r0 (3 * (e * e + -(e * f) + f * f) + -(3 * e) + 3 * f + 1) (by rw [ha, hb]; lib_int)
  · exact r1 (3 * (e * e + -(e * f) + f * f) + -e + -f) (by rw [ha, hb]; lib_int)

/-- **c_W(n) = 0 for n ≡ 2 (mod 3).** -/
theorem cw3_zero (W : Int × Int → Int × Int) (n : Nat) (k : Int) (hn : Int.ofNat n = 3 * k + 2) : cw3 W n = (Cyclo3.cyc3 LibInt.intR).zero := by
  unfold cw3
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun _ => (Cyclo3.cyc3 LibInt.intR).zero) (fun z => by
    rw [I3_neg _ _ (fun h => en_mod3 z k (h.1.trans hn))]; lib_ring)]
  exact zsum3 _

variable (t : Nat)

theorem ones_allZ (m : Nat) : ∀ k, sumK LibInt.intR (fun _ => (1 : Int)) (CMInert.allZ m k) = Int.ofNat k
  | 0 => rfl
  | k + 1 => by
    show sumK LibInt.intR (fun _ => (1 : Int)) (CMInert.allZ m k ++ [mk m k]) = _
    rw [sumK_append, ones_allZ m k]
    show Int.ofNat k + (1 + 0) = Int.ofNat k + 1; lib_int

/-- **#{x³ + y³ = 1}(𝔽_p) = p for p = 3t + 2.** -/
theorem fermat_inert (hpr : ∀ d, Dv d (3 * t + 1 + 1) → d = 1 ∨ d = 3 * t + 1 + 1) :
    sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR ((ring (3 * t + 1)).add (pw (ring (3 * t + 1)) x 3) (pw (ring (3 * t + 1)) y 3)) (ring (3 * t + 1)).one)
      (CMInert.allZ (3 * t + 1) (3 * t + 1 + 1))) (CMInert.allZ (3 * t + 1) (3 * t + 1 + 1)) = Int.ofNat (3 * t + 1 + 1) := by
  have he : 3 * (2 * t + 1) = 2 * (3 * t + 1) + 1 := by lib_nat
  have cube : ∀ G : Zp (3 * t + 1) → Int, sumK LibInt.intR (fun x => G (pw (ring (3 * t + 1)) x 3)) (CMInert.allZ (3 * t + 1) (3 * t + 1 + 1)) =
      sumK LibInt.intR G (CMInert.allZ (3 * t + 1) (3 * t + 1 + 1)) := fun G =>
    reindex LibInt.intR _ (CMInert.complete_allZ (3 * t + 1) LibInt.intR) (fun x => pw (ring (3 * t + 1)) x 3) (fun x => pw (ring (3 * t + 1)) x (2 * t + 1))
      (CMEisen.cube_inv (3 * t) hpr _ he) (CMEisen.inv_cube (3 * t) hpr _ he) G
  rw [sumK_ext LibInt.intR _ _ (fun x => cube (fun y => delta LibInt.intR ((ring (3 * t + 1)).add (pw (ring (3 * t + 1)) x 3) y) (ring (3 * t + 1)).one))]
  rw [cube (fun x => sumK LibInt.intR (fun y => delta LibInt.intR ((ring (3 * t + 1)).add x y) (ring (3 * t + 1)).one) (CMInert.allZ (3 * t + 1) (3 * t + 1 + 1)))]
  rw [sumK_ext LibInt.intR _ (fun _ => (1 : Int)) (fun x => by
    rw [sumK_ext LibInt.intR _ (fun y => delta LibInt.intR y ((ring (3 * t + 1)).add (ring (3 * t + 1)).one ((ring (3 * t + 1)).neg x))) (fun y => by
      unfold delta
      by_cases hy : (ring (3 * t + 1)).add x y = (ring (3 * t + 1)).one
      · rw [ite_pos' _ _ hy, ite_pos' _ _ (by rw [← hy]; lib_ring)]
      · rw [ite_neg' _ _ hy, ite_neg' _ _ (fun e => hy (by rw [e]; lib_ring))])]
    exact CMInert.complete_allZ (3 * t + 1) LibInt.intR _)]
  exact ones_allZ (3 * t + 1) _

/-- **27a at p ≡ 2 (mod 3)**: #E(𝔽_p) = (affine count) + 1 = p + 1 − c(p), c(p) = 0. -/
theorem fermat27_inert (hpr : ∀ d, Dv d (3 * t + 1 + 1) → d = 1 ∨ d = 3 * t + 1 + 1) :
    sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR ((ring (3 * t + 1)).add (pw (ring (3 * t + 1)) x 3) (pw (ring (3 * t + 1)) y 3)) (ring (3 * t + 1)).one)
      (CMInert.allZ (3 * t + 1) (3 * t + 1 + 1))) (CMInert.allZ (3 * t + 1) (3 * t + 1 + 1)) + 1 =
      Int.ofNat (3 * t + 1 + 1) + 1 - (cw3 (fun z => z) (3 * t + 1 + 1)).1 := by
  rw [fermat_inert t hpr, cw3_zero _ _ (Int.ofNat t) rfl]
  show _ = Int.ofNat (3 * t + 1 + 1) + 1 - 0; lib_int

end FermatInert3

#print axioms FermatInert3.en_mod3
#print axioms FermatInert3.cw3_zero
#print axioms FermatInert3.ones_allZ
#print axioms FermatInert3.fermat_inert
#print axioms FermatInert3.fermat27_inert

lang/FermatQuartic.lean

lean_all/lang/FermatQuartic.lean

/-
  lang/FermatQuartic — T43, cycle 232: points on the Fermat quartic x⁴ + y⁴ = 1 over 𝔽_p, p ≡ 1 (mod 4).

  N(a) = #{x : x⁴ = a} = 1 + χ(a) + ρ(a) + χ̄(a) (fourth roots c, cι, −c, −cι), and Σ_{a+b=1} N(a)N(b) is a sum of Jacobi sums:
  J(χ,χ) + J(χ̄,χ̄) + J(ρ,ρ) + 2J(χ,χ̄) + 2J(ρ,χ) + 2J(ρ,χ̄), with J(ρ,ρ) = −1, J(χ,χ̄) = −χ(−1) and (QuartDup) J(ρ,χ) = χ(−1)J:
      fermat_quartic   #{x⁴ + y⁴ = 1} = p − 1 − 2χ(−1) + (2 + 4χ(−1)) a,   J = a + bi
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.QuartDup

namespace FermatQuartic

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius QuartChar QuartJacobi QuartPrimary QuartDup
open LibRing (castZ castN)

variable (n : Nat)

def q4 (x : Zp (4 * n + 3 + 1)) : Zp (4 * n + 3 + 1) := (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul x x) ((ring (4 * n + 3 + 1)).mul x x)

theorem q4_mul (x y : Zp (4 * n + 3 + 1)) : q4 n ((ring (4 * n + 3 + 1)).mul x y) = (ring (4 * n + 3 + 1)).mul (q4 n x) (q4 n y) := by unfold q4; lib_ring

/-- a^{(p−1)/4} = 1, a ≠ 0 ⇒ a is a fourth power. -/
theorem q4_root (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (a : Zp (4 * n + 3 + 1)) (ha0 : a ≠ (ring (4 * n + 3 + 1)).zero) (ha : pw (ring (4 * n + 3 + 1)) a (n + 1) = (ring (4 * n + 3 + 1)).one) : ∃ c, q4 n c = a := by
  match PrimRoot.primitive_root (4 * n + 3) hpr with
  | ⟨g, hg⟩ =>
    match DLog.mem_pows (4 * n + 3) g a _ (DLog.every_unit (4 * n + 3) hpr g hg a ha0) with
    | ⟨k, _, hk⟩ =>
      have h1 : pw (ring (4 * n + 3 + 1)) g (k * (n + 1)) = (ring (4 * n + 3 + 1)).one := by rw [← TwoPower.pwpow, ← hk, ha]
      match hg.2 _ h1 with
      | ⟨c, hc⟩ =>
        have h3 : k = 4 * c := by
          apply Nat.eq_of_mul_eq_mul_right (Nat.zero_lt_succ n)
          rw [hc]; lib_nat
        refine ⟨pw (ring (4 * n + 3 + 1)) g c, ?_⟩
        rw [hk, h3, show 4 * c = c * 4 by lib_nat, ← TwoPower.pwpow]
        show _ = (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul (ring (4 * n + 3 + 1)).one _) _) _) _
        unfold q4; lib_ring

theorem cancelK (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (a b : Zp (4 * n + 3 + 1)) (ha : a ≠ (ring (4 * n + 3 + 1)).zero) (h : (ring (4 * n + 3 + 1)).mul a b = (ring (4 * n + 3 + 1)).zero) : b = (ring (4 * n + 3 + 1)).zero := by
  cases nozd (4 * n + 3) hpr _ _ h with
  | inl e => exact absurd e ha
  | inr e => exact e

theorem eq_of_sub' (x y : Zp (4 * n + 3 + 1)) (h : (ring (4 * n + 3 + 1)).add x ((ring (4 * n + 3 + 1)).neg y) = (ring (4 * n + 3 + 1)).zero) : x = y := by
  calc x = (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).add x ((ring (4 * n + 3 + 1)).neg y)) y := by lib_ring
    _ = y := by rw [h]; lib_ring

/-- the four fourth roots of c⁴. -/
theorem four_roots (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (c : Zp (4 * n + 3 + 1)) (hc : c ≠ (ring (4 * n + 3 + 1)).zero) (x : Zp (4 * n + 3 + 1)) :
    delta (Cyclo4.cyc4 LibInt.intR) (q4 n x) (q4 n c) = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (delta (Cyclo4.cyc4 LibInt.intR) x c) (delta (Cyclo4.cyc4 LibInt.intR) x ((ring (4 * n + 3 + 1)).mul c ι))) (delta (Cyclo4.cyc4 LibInt.intR) x ((ring (4 * n + 3 + 1)).neg c))) (delta (Cyclo4.cyc4 LibInt.intR) x ((ring (4 * n + 3 + 1)).neg ((ring (4 * n  -- (+27 chars; full line in the Zenodo source)
  have two : (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one ≠ (ring (4 * n + 3 + 1)).zero := mtwo_ne n
  have hi0 : ι ≠ (ring (4 * n + 3 + 1)).zero := fun e => by
    have : (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).zero := by rw [← hi, e]; lib_ring
    exact PowerSum.one_ne_zero' (4 * n + 3) (by calc (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by lib_ring
                                                  _ = (ring (4 * n + 3 + 1)).zero := by rw [this]; lib_ring)
  -- distinctness: u ≠ v whenever u − v = c·(unit)
  have dist : ∀ u : Zp (4 * n + 3 + 1), u ≠ (ring (4 * n + 3 + 1)).zero → ∀ y z : Zp (4 * n + 3 + 1), (ring (4 * n + 3 + 1)).add y ((ring (4 * n + 3 + 1)).neg z) = (ring (4 * n + 3 + 1)).mul c u → y ≠ z := fun u hu y z e eq => by
    rw [eq, show (ring (4 * n + 3 + 1)).add z ((ring (4 * n + 3 + 1)).neg z) = (ring (4 * n + 3 + 1)).zero by lib_ring] at e
    exact hu (cancelK n hpr c u hc e.symm)
  have u1 : (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one ((ring (4 * n + 3 + 1)).neg ι) ≠ (ring (4 * n + 3 + 1)).zero := fun e => by
    have : ι = (ring (4 * n + 3 + 1)).one := (eq_of_sub' n _ _ e).symm
    rw [this] at hi
    exact two (by calc (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).mul (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one) (ring (4 * n + 3 + 1)).one := by lib_ring
                  _ = (ring (4 * n + 3 + 1)).zero := by rw [hi]; lib_ring)
  have u2 : (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one ι ≠ (ring (4 * n + 3 + 1)).zero := fun e => by
    have : ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one := by calc ι = (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one ι) ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by lib_ring
                                          _ = _ := by rw [e]; lib_ring
    rw [this] at hi
    exact two (by calc (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (ring (4 * n + 3 + 1)).one := by lib_ring
                  _ = (ring (4 * n + 3 + 1)).zero := by rw [hi]; lib_ring)
  have d12 : c ≠ (ring (4 * n + 3 + 1)).mul c ι := dist _ u1 _ _ (by lib_ring)
  have d13 : c ≠ (ring (4 * n + 3 + 1)).neg c := dist _ two _ _ (by lib_ring)
  have d14 : c ≠ (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul c ι) := dist _ u2 _ _ (by lib_ring)
  have d23 : (ring (4 * n + 3 + 1)).mul c ι ≠ (ring (4 * n + 3 + 1)).neg c := dist _ u2 _ _ (by lib_ring)
  have d24 : (ring (4 * n + 3 + 1)).mul c ι ≠ (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul c ι) := dist ((ring (4 * n + 3 + 1)).add ι ι) (fun e => hi0 (cancelK n hpr _ ι two (by rw [show (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one) ι = (ring (4 * n + 3 + 1)).add ι ι by lib_ring]; exact e))) _ _ (by lib_ring)
  have u3 : (ring (4 * n + 3 + 1)).add ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) ≠ (ring (4 * n + 3 + 1)).zero := fun (e : (ring (4 * n + 3 + 1)).add ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) = (ring (4 * n + 3 + 1)).zero) => u1 (by
    calc (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one ((ring (4 * n + 3 + 1)).neg ι) = (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).add ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) := by lib_ring
      _ = (ring (4 * n + 3 + 1)).zero := by rw [e]; lib_ring)
  have d34 : (ring (4 * n + 3 + 1)).neg c ≠ (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul c ι) := dist _ u3 _ _ (by lib_ring)
  have r2 : q4 n ((ring (4 * n + 3 + 1)).mul c ι) = q4 n c := by
    unfold q4; rw [show (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul c ι) ((ring (4 * n + 3 + 1)).mul c ι)) ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul c ι) ((ring (4 * n + 3 + 1)).mul c ι)) = (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul c c) ((ring (4 * n + 3 + 1)).mul c c)) ((ring (4 * n + 3 + 1)).mul ((ring   -- (+86 chars; full line in the Zenodo source)
  have r3 : q4 n ((ring (4 * n + 3 + 1)).neg c) = q4 n c := by unfold q4; lib_ring
  have r4 : q4 n ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul c ι)) = q4 n c := by
    rw [show q4 n ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul c ι)) = q4 n ((ring (4 * n + 3 + 1)).mul c ι) by unfold q4; lib_ring, r2]
  unfold delta
  by_cases hx : q4 n x = q4 n c
  · rw [ite_pos' _ _ hx]
    -- (x − c)(x + c)(x − cι)(x + cι) = x⁴ − c⁴ = 0
    have e : (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).add x ((ring (4 * n + 3 + 1)).neg c)) ((ring (4 * n + 3 + 1)).add x c)) ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).add x ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul c ι))) ((ring (4 * n + 3 + 1)).add x ((ring (4 * n + 3 + 1)).mul c ι))) = (ring (4 * n + 3 + 1)).zero := by
      calc _ = (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).add (q4 n x) ((ring (4 * n + 3 + 1)).neg (q4 n c))) ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul c c) ((ring (4 * n + 3 + 1)).mul c c)) ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul x x) ((ring (4 * n + 3 + 1)).mul c c)))) ((ring  -- (+109 chars; full line in the Zenodo source)
        _ = (ring (4 * n + 3 + 1)).zero := by rw [hx, hi]; lib_ring
    cases nozd (4 * n + 3) hpr _ _ e with
    | inl e1 =>
      cases nozd (4 * n + 3) hpr _ _ e1 with
      | inl e2 =>
        have ex := eq_of_sub' n _ _ e2
        rw [ite_pos' _ _ ex, ite_neg' _ _ (by rw [ex]; exact d12), ite_neg' _ _ (by rw [ex]; exact d13), ite_neg' _ _ (by rw [ex]; exact d14)]; lib_ring
      | inr e2 =>
        have ex : x = (ring (4 * n + 3 + 1)).neg c := eq_of_sub' n _ _ (by rw [show (ring (4 * n + 3 + 1)).add x ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).neg c)) = (ring (4 * n + 3 + 1)).add x c by lib_ring]; exact e2)
        rw [ite_neg' _ _ (by rw [ex]; exact fun e => d13 e.symm), ite_neg' _ _ (by rw [ex]; exact fun e => d23 e.symm), ite_pos' _ _ ex, ite_neg' _ _ (by rw [ex]; exact d34)]; lib_ring
    | inr e1 =>
      cases nozd (4 * n + 3) hpr _ _ e1 with
      | inl e2 =>
        have ex := eq_of_sub' n _ _ e2
        rw [ite_neg' _ _ (by rw [ex]; exact fun e => d12 e.symm), ite_pos' _ _ ex, ite_neg' _ _ (by rw [ex]; exact d23), ite_neg' _ _ (by rw [ex]; exact d24)]; lib_ring
      | inr e2 =>
        have ex : x = (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul c ι) := eq_of_sub' n _ _ (by rw [show (ring (4 * n + 3 + 1)).add x ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul c ι))) = (ring (4 * n + 3 + 1)).add x ((ring (4 * n + 3 + 1)).mul c ι) by lib_ring]; exact e2)
        rw [ite_neg' _ _ (by rw [ex]; exact fun e => d14 e.symm), ite_neg' _ _ (by rw [ex]; exact fun e => d24 e.symm), ite_neg' _ _ (by rw [ex]; exact fun e => d34 e.symm), ite_pos' _ _ ex]; lib_ring
  · rw [ite_neg' _ _ hx, ite_neg' _ _ (fun e => hx (by rw [e])), ite_neg' _ _ (fun e => hx (by rw [e, r2])),
      ite_neg' _ _ (fun e => hx (by rw [e, r3])), ite_neg' _ _ (fun e => hx (by rw [e, r4]))]; lib_ring

/-! ## N(a) = 1 + χ(a) + ρ(a) + χ̄(a) -/

def g4 (ι a : Zp (4 * n + 3 + 1)) : Int × Int := (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one (chi4 n ι a)) (rho n ι a)) (chib n ι a)

theorem chib_eq (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (a : Zp (4 * n + 3 + 1)) : chib n ι a = gconj (chi4 n ι a) := by
  unfold chib; exact chi_ivq n hpr ι hi a

theorem chi_q4 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (x : Zp (4 * n + 3 + 1)) (hx : x ≠ (ring (4 * n + 3 + 1)).zero) : chi4 n ι (q4 n x) = (1, 0) := by
  unfold q4; rw [chi4_mul n hpr ι hi, chi4_mul n hpr ι hi]
  rcases ResidueCriterion.chi4_cases n ι x with e | e | e | e | e
  · exact absurd e (chi4_ne n hpr ι hi x hx)
  all_goals (rw [e]; rfl)

theorem q4_zero (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (x : Zp (4 * n + 3 + 1)) (h : q4 n x = (ring (4 * n + 3 + 1)).zero) : x = (ring (4 * n + 3 + 1)).zero := by
  unfold q4 at h
  cases nozd (4 * n + 3) hpr _ _ h with
  | inl e => cases nozd (4 * n + 3) hpr _ _ e with
    | inl e' => exact e'
    | inr e' => exact e'
  | inr e => cases nozd (4 * n + 3) hpr _ _ e with
    | inl e' => exact e'
    | inr e' => exact e'

theorem q4_count (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (a : Zp (4 * n + 3 + 1)) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun x => delta (Cyclo4.cyc4 LibInt.intR) (q4 n x) a) (LL n) = g4 n ι a := by
  unfold g4
  by_cases ha : a = (ring (4 * n + 3 + 1)).zero
  · rw [ha, sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun x => delta (Cyclo4.cyc4 LibInt.intR) x (ring (4 * n + 3 + 1)).zero) (fun x => by
      unfold delta
      by_cases hx : x = (ring (4 * n + 3 + 1)).zero
      · rw [ite_pos' _ _ hx, ite_pos' _ _ (by rw [hx]; unfold q4; lib_ring)]
      · rw [ite_neg' _ _ hx, ite_neg' _ _ (fun e => hx (q4_zero n hpr x e))]),
      show sumK (Cyclo4.cyc4 LibInt.intR) (fun x => delta (Cyclo4.cyc4 LibInt.intR) x (ring (4 * n + 3 + 1)).zero) (LL n) = (Cyclo4.cyc4 LibInt.intR).one from CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR) (ring (4 * n + 3 + 1)).zero,
      chi4_zero, rho_zero, chib_zero]; rfl
  · rw [chib_eq n hpr ι hi]; unfold rho
    rcases ResidueCriterion.chi4_cases n ι a with e | e | e | e | e
    · exact absurd e (chi4_ne n hpr ι hi a ha)
    · match q4_root n hpr a ha (QuartDup.chi4_one_pw n ι a e) with
      | ⟨c, hc⟩ =>
        have hc0 : c ≠ (ring (4 * n + 3 + 1)).zero := fun e0 => ha (by rw [← hc, e0]; unfold q4; lib_ring)
        have hcomp : ∀ z, sumK (Cyclo4.cyc4 LibInt.intR) (fun x => delta (Cyclo4.cyc4 LibInt.intR) x z) (LL n) = (Cyclo4.cyc4 LibInt.intR).one := fun z => CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR) z
        rw [← hc, sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun x => four_roots n hpr ι hi c hc0 x), sumK_add, sumK_add, sumK_add,
          hcomp c, hcomp ((ring (4 * n + 3 + 1)).mul c ι), hcomp ((ring (4 * n + 3 + 1)).neg c), hcomp ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul c ι)), hc, e]; rfl
    all_goals
      have hz : ∀ x, delta (Cyclo4.cyc4 LibInt.intR) (q4 n x) a = (Cyclo4.cyc4 LibInt.intR).zero := fun x => by
        unfold delta
        rw [ite_neg' _ _ (fun ex => by
          have hx0 : x ≠ (ring (4 * n + 3 + 1)).zero := fun e0 => ha (by rw [← ex, e0]; unfold q4; lib_ring)
          have h1 := chi_q4 n hpr ι hi x hx0
          rw [ex, e] at h1
          first
          | exact absurd (congrArg Prod.fst h1) (fun e' => Nat.noConfusion (Int.ofNat.inj e'))
          | exact absurd (congrArg Prod.fst h1) (fun e' => Int.noConfusion e'))]
      rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ hz, HeckePrime.zsumR (Cyclo4.cyc4 LibInt.intR), e]; rfl

/-! ## the double sum -/

def Fq : Int × Int := sumK (Cyclo4.cyc4 LibInt.intR) (fun x => sumK (Cyclo4.cyc4 LibInt.intR) (fun y => delta (Cyclo4.cyc4 LibInt.intR) ((ring (4 * n + 3 + 1)).add (q4 n x) (q4 n y)) (ring (4 * n + 3 + 1)).one) (LL n)) (LL n)

theorem step1 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (x : Zp (4 * n + 3 + 1)) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun y => delta (Cyclo4.cyc4 LibInt.intR) ((ring (4 * n + 3 + 1)).add (q4 n x) (q4 n y)) (ring (4 * n + 3 + 1)).one) (LL n) = g4 n ι (om n (q4 n x)) := by
  rw [← q4_count n hpr ι hi]
  exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun y => by
    unfold delta om
    by_cases h : (ring (4 * n + 3 + 1)).add (q4 n x) (q4 n y) = (ring (4 * n + 3 + 1)).one
    · rw [ite_pos' _ _ h, ite_pos' _ _ (by rw [← h]; lib_ring)]
    · rw [ite_neg' _ _ h, ite_neg' _ _ (fun e => h (by rw [e]; lib_ring))]) _

theorem step2 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (hh : Zp (4 * n + 3 + 1) → Int × Int) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun x => hh (q4 n x)) (LL n) = sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (hh a)) (LL n) := by
  have e1 : sumK (Cyclo4.cyc4 LibInt.intR) (fun x => hh (q4 n x)) (LL n) = sumK (Cyclo4.cyc4 LibInt.intR) (fun x => sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) a (q4 n x)) (hh a)) (LL n)) (LL n) :=
    sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun x => (FiniteRecChain.pick (Cyclo4.cyc4 LibInt.intR) _ (CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR)) hh (q4 n x)).symm) _
  rw [e1, sumK_swap]
  exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by
    rw [← sumK_mul, ← q4_count n hpr ι hi a]
    exact congrArg (fun z => (Cyclo4.cyc4 LibInt.intR).mul z (hh a)) (sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun x => delta_symm (Cyclo4.cyc4 LibInt.intR) a (q4 n x)) _)) _

theorem refl_om (G : Zp (4 * n + 3 + 1) → Int × Int) : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => G (om n a)) (LL n) = sumK (Cyclo4.cyc4 LibInt.intR) G (LL n) :=
  FiniteRecChain.reindex (Cyclo4.cyc4 LibInt.intR) (LL n) (CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR)) (om n) (om n) (om_om n) (om_om n) G

theorem Fq_sum (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) :
    Fq n = sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (g4 n ι (om n a))) (LL n) := by
  unfold Fq
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun x => step1 n hpr ι hi x)]
  exact step2 n hpr ι hi (fun a => g4 n ι (om n a))

/-! ## the sixteen character sums -/

theorem m1_real (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) :
    chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) = (1, 0) ∨ chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) = (-1, 0) := by
  have hs : (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) = (Cyclo4.cyc4 LibInt.intR).one := by
    rw [← chi4_mul n hpr ι hi, show (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) = (ring (4 * n + 3 + 1)).one by lib_ring]; exact chi_one n hpr ι hi
  rcases ResidueCriterion.chi4_cases n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) with e | e | e | e | e
  · rw [e] at hs; exact absurd (congrArg Prod.fst hs) (fun e' => Nat.noConfusion (Int.ofNat.inj e'))
  · exact Or.inl e
  · rw [e] at hs; exact absurd (congrArg Prod.fst hs) (fun e' => Int.noConfusion e')
  · exact Or.inr e
  · rw [e] at hs; exact absurd (congrArg Prod.fst hs) (fun e' => Int.noConfusion e')

theorem gconj_rho (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (x : Zp (4 * n + 3 + 1)) : gconj (rho n ι x) = rho n ι x := by
  by_cases hx : x = (ring (4 * n + 3 + 1)).zero
  · rw [hx, rho_zero]; rfl
  · rcases QuartPrimary.rho_real n hpr ι hi x hx with e | e <;> rw [e] <;> rfl

/-- J(χ, χ̄) = −χ(−1). -/
theorem T13 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (chib n ι (om n a))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one) := by
  rw [← FiniteRecChain.reindex (Cyclo4.cyc4 LibInt.intR) (LL n) (CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR)) (fun b => (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one b) (fun b => (ring (4 * n + 3 + 1)).add b ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (fun x => by lib_ring) (fun y => by lib_ring)]
  rw [← D_ne' n hpr (chi4 n ι) (chi4_mul n hpr ι hi) (chi_one n hpr ι hi) (chi4_zero n ι) (chi4_sum n hpr ι hi) (ring (4 * n + 3 + 1)).one (fun e => PowerSum.one_ne_zero' (4 * n + 3) e), mul_sumK]
  exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun b => by
    show (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one b)) (chib n ι (om n ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one b))) = _
    have hreal : gconj (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) = chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by
      rcases m1_real n hpr ι hi with e | e <;> rw [e] <;> rfl
    rw [show om n ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one b) = (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) b by unfold om; lib_ring, chib_mul n hpr ι hi,
      chib_eq n hpr ι hi ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one), hreal]
    unfold chib; lib_ring) _

/-- **Fq as a combination of Jacobi sums.** -/
theorem Fq_C (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) :
    Fq n = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (Int.ofNat (4 * n + 3 + 1 + 1), 0) (Jq n (chi4 n ι))) (gconj (Jq n (chi4 n ι)))) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one))
      ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one)))
        ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (J2 n ι) (J2 n ι)) ((Cyclo4.cyc4 LibInt.intR).add (gconj (J2 n ι)) (gconj (J2 n ι))))) := by
  rw [Fq_sum n hpr ι hi]
  have hexp : ∀ a, (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (g4 n ι (om n a)) =
      (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one
        ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (chi4 n ι (om n a)) (rho n ι (om n a))) (chib n ι (om n a))))
        ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (chi4 n ι a) (rho n ι a)) (chib n ι a)))
        ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (chi4 n ι (om n a))))
        ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (rho n ι (om n a))))
        ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (chib n ι (om n a))))
        ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (chib n ι (om n a))) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (chi4 n ι (om n a)))))
        ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (rho n ι (om n a))) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (chi4 n ι (om n a)))))
        ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (chib n ι (om n a))) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (rho n ι (om n a)))) := fun a => by unfold g4; lib_ring
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ hexp]
  repeat rw [sumK_add]
  have S1 : sumK (Cyclo4.cyc4 LibInt.intR) (fun _ : Zp (4 * n + 3 + 1) => (Cyclo4.cyc4 LibInt.intR).one) (LL n) = (Int.ofNat (4 * n + 3 + 1 + 1), 0) := sum_oneq n _
  have SA : sumK (Cyclo4.cyc4 LibInt.intR) (chi4 n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := chi4_sum n hpr ι hi
  have SR : sumK (Cyclo4.cyc4 LibInt.intR) (rho n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := rho_sum n hpr ι hi
  have SB : sumK (Cyclo4.cyc4 LibInt.intR) (chib n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := chib_sum n hpr ι hi
  have SA' : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => chi4 n ι (om n a)) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := (refl_om n _).trans SA
  have SR' : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => rho n ι (om n a)) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := (refl_om n _).trans SR
  have SB' : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => chib n ι (om n a)) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := (refl_om n _).trans SB
  have SAA : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (chi4 n ι (om n a))) (LL n) = Jq n (chi4 n ι) := rfl
  have SRR : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (rho n ι (om n a))) (LL n) = (Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one := J_rho n hpr ι hi
  have SBB : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (chib n ι (om n a))) (LL n) = gconj (Jq n (chi4 n ι)) := Jb_conj n hpr ι hi
  have SAB := T13 n hpr ι hi
  have SBA : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (chi4 n ι (om n a))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one) := by
    rw [← SAB, ← refl_om n]; exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by rw [om_om]; exact (Cyclo4.cyc4 LibInt.intR).mul_comm _ _) _
  have SRA : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (chi4 n ι (om n a))) (LL n) = J2 n ι := rfl
  have SAR : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (rho n ι (om n a))) (LL n) = J2 n ι := by
    rw [← SRA, ← refl_om n]; exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by rw [om_om]; exact (Cyclo4.cyc4 LibInt.intR).mul_comm _ _) _
  have SRB : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (chib n ι (om n a))) (LL n) = gconj (J2 n ι) := by
    unfold J2; rw [gconj_sum]
    exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by rw [gconj_mul, gconj_rho n hpr ι hi, chib_eq n hpr ι hi]) _
  have SBR : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (rho n ι (om n a))) (LL n) = gconj (J2 n ι) := by
    rw [← SRB, ← refl_om n]; exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by rw [om_om]; exact (Cyclo4.cyc4 LibInt.intR).mul_comm _ _) _
  rw [S1, SA, SR, SB, SA', SR', SB', SAA, SRR, SBB, SAB, SBA, SAR, SRA, SRB, SBR]
  lib_ring

/-- **Points on the Fermat quartic**: #{x⁴ + y⁴ = 1} = p − 1 − 2s + (2 + 4s) a, s = χ(−1), J = a + bi. -/
theorem fermat_quartic (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) :
    (Fq n).1 = Int.ofNat (4 * n + 3 + 1 + 1) - 1 - 2 * (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)).1 + (2 + 4 * (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)).1) * (Jq n (chi4 n ι)).1 := by
  have hd := QuartDup.quart_dup n hpr ι hi
  have hs : (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) = (Cyclo4.cyc4 LibInt.intR).one := by
    rw [← chi4_mul n hpr ι hi, show (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) = (ring (4 * n + 3 + 1)).one by lib_ring]; exact chi_one n hpr ι hi
  have hJ2 : J2 n ι = (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (Jq n (chi4 n ι)) := by
    rw [hd]; calc J2 n ι = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one))) (J2 n ι) := by rw [hs]; lib_ring
      _ = _ := by lib_ring
  rw [Fq_C n hpr ι hi, hJ2]
  generalize Jq n (chi4 n ι) = Jv
  have fa : ∀ u v : Int × Int, ((Cyclo4.cyc4 LibInt.intR).add u v).1 = u.1 + v.1 := fun _ _ => rfl
  have fm : ∀ u v : Int × Int, ((Cyclo4.cyc4 LibInt.intR).mul u v).1 = u.1 * v.1 + -(u.2 * v.2) := fun _ _ => rfl
  have fg : ∀ u : Int × Int, (gconj u).1 = u.1 := fun _ => rfl
  have fn : ∀ u : Int × Int, ((Cyclo4.cyc4 LibInt.intR).neg u).1 = -u.1 := fun _ => rfl
  have fn2 : ∀ u : Int × Int, ((Cyclo4.cyc4 LibInt.intR).neg u).2 = -u.2 := fun _ => rfl
  have o1 : ((Cyclo4.cyc4 LibInt.intR).one).1 = 1 := rfl
  have o2 : ((Cyclo4.cyc4 LibInt.intR).one).2 = 0 := rfl
  repeat rw [fa]
  repeat rw [fm]
  repeat rw [fg]
  rw [fm, fn, fn2, o1, o2]
  rcases m1_real n hpr ι hi with e | e <;> rw [e]
  · show Int.ofNat (4 * n + 3 + 1 + 1) + Jv.1 + Jv.1 + -1 + ((1 * -1 + -(0 * -0) + (1 * -1 + -(0 * -0))) + (1 * Jv.1 + -(0 * Jv.2) + (1 * Jv.1 + -(0 * Jv.2)) + (1 * Jv.1 + -(0 * Jv.2) + (1 * Jv.1 + -(0 * Jv.2))))) = Int.ofNat (4 * n + 3 + 1 + 1) - 1 - 2 * 1 + (2 + 4 * 1) * Jv.1
    lib_int
  · show Int.ofNat (4 * n + 3 + 1 + 1) + Jv.1 + Jv.1 + -1 + ((-1 * -1 + -(0 * -0) + (-1 * -1 + -(0 * -0))) + (-1 * Jv.1 + -(0 * Jv.2) + (-1 * Jv.1 + -(0 * Jv.2)) + (-1 * Jv.1 + -(0 * Jv.2) + (-1 * Jv.1 + -(0 * Jv.2))))) = Int.ofNat (4 * n + 3 + 1 + 1) - 1 - 2 * -1 + (2 + 4 * -1) * Jv.1
    lib_int

end FermatQuartic

#print axioms FermatQuartic.q4_mul
#print axioms FermatQuartic.q4_root
#print axioms FermatQuartic.cancelK
#print axioms FermatQuartic.eq_of_sub'
#print axioms FermatQuartic.four_roots
#print axioms FermatQuartic.chib_eq
#print axioms FermatQuartic.chi_q4
#print axioms FermatQuartic.q4_zero
#print axioms FermatQuartic.q4_count
#print axioms FermatQuartic.step1
#print axioms FermatQuartic.step2
#print axioms FermatQuartic.refl_om
#print axioms FermatQuartic.Fq_sum
#print axioms FermatQuartic.m1_real
#print axioms FermatQuartic.gconj_rho
#print axioms FermatQuartic.T13
#print axioms FermatQuartic.Fq_C
#print axioms FermatQuartic.fermat_quartic

lang/FermatUnique.lean

lean_all/lang/FermatUnique.lean

/-
  lang/FermatUnique — T43, cycle 148: uniqueness in Fermat's two-squares theorem, and of the primary π above p.

  If a² + b² = p = c² + d² with p prime, then (c, d) = (±a, ±b) or (±b, ∓a) up to the sign of d:
    lagrange       (ac + bd)² + (ad − bc)² = (a² + b²)(c² + d²) = p²
    case_plus      p | ac + bd ⇒ (ac+bd)/p = ±1 and ad − bc = 0, or ac + bd = 0 and ad − bc = ±p
    fermat_unique  (ac + bd)(ac − bd) = p(a² + c² − p), so one factor is divisible by p: the eight possibilities
    squares_unique {c², d²} = {a², b²}
  So the Gaussian prime above a split p is unique up to units and conjugation, and the Hecke character value ψ(𝔭) = π
  (π primary) is determined up to conjugation: a_p = 2 Re π has a unique value.

  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.HasseDavenport
import lang.QuarticFamily
import lang.CubicTrace
import lang.FinCubicSign

namespace FermatUnique

open FTAChain LibInt LibRing

/-- p | xy with p prime (as Int, P = ofNat p) ⇒ p | x or p | y. -/
theorem int_euclid (p : Nat) (hp : ∀ d, Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (x y q : Int)
    (h : x * y = Int.ofNat (p + 1 + 1) * q) :
    (∃ u, x = Int.ofNat (p + 1 + 1) * u) ∨ (∃ u, y = Int.ofNat (p + 1 + 1) * u) := by
  have h0 := HasseDavenport.castZ_mul (ZMod.ring (p + 1)) x y
  rw [h, HasseDavenport.castZ_mul] at h0
  have hP : castZ (ZMod.ring (p + 1)) (Int.ofNat (p + 1 + 1)) = (ZMod.ring (p + 1)).zero := by
    show castN (ZMod.ring (p + 1)) (p + 1 + 1) = _
    rw [IntCast.castN_mk]; exact Subtype.ext (ZMod.dv_md_zero (p + 1) _ ⟨1, by lib_nat⟩)
  rw [hP, DetChain.zero_mul'] at h0
  cases ZMod.nozd p hp _ _ h0.symm with
  | inl h1 => exact Or.inl (HasseDavenport.castZ_zero_dvd (p + 1) x h1)
  | inr h1 => exact Or.inr (HasseDavenport.castZ_zero_dvd (p + 1) y h1)

theorem cancelP (p : Nat) (x y : Int) (h : Int.ofNat (p + 1 + 1) * x = Int.ofNat (p + 1 + 1) * y) : x = y :=
  CubicTrace.cancel_N (p + 1 + 1) (fun e => Nat.noConfusion e) x y h

theorem add_cancel (a : Nat) : ∀ b c : Nat, a + b = a + c → b = c := by
  induction a with
  | zero => intro b c h; rw [Nat.zero_add, Nat.zero_add] at h; exact h
  | succ k ih => intro b c h; apply ih; rw [Nat.succ_add, Nat.succ_add] at h; exact Nat.succ.inj h

theorem lagrange (a b c d : Int) :
    (a * c + b * d) * (a * c + b * d) + (a * d - b * c) * (a * d - b * c) = (a * a + b * b) * (c * c + d * d) := by lib_int

theorem cancelQ (p : Nat) (P : Int) (hP : Int.ofNat (p + 1 + 1) = P) (x y : Int) (h : P * x = P * y) : x = y := by
  rw [← hP] at h; exact cancelP p x y h

/-- the case p | ac + bd: (c, d) ∈ {(a, b), (−a, −b), (−b, a), (b, −a)}. -/
theorem case_plus (p : Nat) (a b c d u : Int) (h1 : a * a + b * b = Int.ofNat (p + 1 + 1))
    (h2 : c * c + d * d = Int.ofNat (p + 1 + 1)) (hu : a * c + b * d = Int.ofNat (p + 1 + 1) * u) :
    (c = a ∧ d = b) ∨ (c = -a ∧ d = -b) ∨ (c = -b ∧ d = a) ∨ (c = b ∧ d = -a) := by
  have hLag := lagrange a b c d
  have hc : (a * a + b * b) * c = a * (a * c + b * d) - b * (a * d - b * c) := by lib_int
  have hd : (a * a + b * b) * d = b * (a * c + b * d) + a * (a * d - b * c) := by lib_int
  rw [h1, h2, hu] at hLag
  rw [h1, hu] at hc hd
  generalize a * d - b * c = Zz at hLag hc hd
  generalize hP : Int.ofNat (p + 1 + 1) = P at hLag hc hd
  match FinCubicSign.sq_nn u, FinCubicSign.sq_nn Zz, FinCubicSign.sq_nn P with
  | ⟨nu, hnu⟩, ⟨nz, hnz⟩, ⟨pp, hpp⟩ =>
    have key : Int.ofNat (pp * nu + nz) = Int.ofNat (pp + 0) := by
      show Int.ofNat pp * Int.ofNat nu + Int.ofNat nz = Int.ofNat pp
      rw [← hpp, ← hnu, ← hnz, show P * P * (u * u) + Zz * Zz = P * u * (P * u) + Zz * Zz by lib_int]; exact hLag
    have hk := Int.ofNat.inj key
    have hpp0 : pp ≠ 0 := fun e => by
      have h0 : P * P = 0 := by rw [hpp, e]; rfl
      have hP0 := HasseDavenport.sq_zero P h0
      rw [← hP] at hP0; exact Nat.noConfusion (Int.ofNat.inj hP0)
    match nu with
    | 0 =>
      have u0 : u = 0 := HasseDavenport.sq_zero u hnu
      have hnzp : nz = pp := by
        have e : pp * 0 + nz = nz := by lib_nat
        rw [e] at hk; exact hk
      have hz : Zz * Zz = P * P := by rw [hnz, hpp, hnzp]
      rw [u0] at hc hd
      cases QuarticFamily.sq_eq Zz P hz with
      | inl e =>
        right; right; left
        rw [e] at hc hd
        exact ⟨cancelQ p P hP c (-b) (by rw [hc]; lib_int), cancelQ p P hP d a (by rw [hd]; lib_int)⟩
      | inr e =>
        right; right; right
        rw [e] at hc hd
        exact ⟨cancelQ p P hP c b (by rw [hc]; lib_int), cancelQ p P hP d (-a) (by rw [hd]; lib_int)⟩
    | 1 =>
      have hz0 : nz = 0 := add_cancel pp _ _ ((show pp + nz = pp * 1 + nz by lib_nat).trans hk)
      have z0 : Zz = 0 := HasseDavenport.sq_zero Zz (by rw [hnz, hz0]; rfl)
      rw [z0] at hc hd
      cases QuarticFamily.sq_eq u 1 (by rw [hnu]; rfl) with
      | inl e =>
        left
        rw [e] at hc hd
        exact ⟨cancelQ p P hP c a (by rw [hc]; lib_int), cancelQ p P hP d b (by rw [hd]; lib_int)⟩
      | inr e =>
        right; left
        rw [e] at hc hd
        exact ⟨cancelQ p P hP c (-a) (by rw [hc]; lib_int), cancelQ p P hP d (-b) (by rw [hd]; lib_int)⟩
    | n + 2 =>
      exfalso
      have h3 : pp * (n + 1) + nz = 0 := add_cancel pp _ _ ((show pp + (pp * (n + 1) + nz) = pp * (n + 2) + nz by lib_nat).trans hk)
      have h4 := (HasseDavenport.nat_sum_zero _ _ h3).1
      have h5 : pp + pp * n = 0 := (show pp + pp * n = pp * (n + 1) by lib_nat).trans h4
      exact hpp0 (HasseDavenport.nat_sum_zero _ _ h5).1

def Four (a b c d : Int) : Prop := (c = a ∧ d = b) ∨ (c = -a ∧ d = -b) ∨ (c = -b ∧ d = a) ∨ (c = b ∧ d = -a)

/-- **Uniqueness in Fermat's two-squares theorem**: a² + b² = p = c² + d² (p prime) ⇒ (c, ±d) ∈ {(a, b), (−a, −b), (−b, a), (b, −a)}. -/
theorem fermat_unique (p : Nat) (hp : ∀ d, Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (a b c d : Int)
    (h1 : a * a + b * b = Int.ofNat (p + 1 + 1)) (h2 : c * c + d * d = Int.ofNat (p + 1 + 1)) :
    Four a b c d ∨ Four a b c (-d) := by
  have key : (a * c + b * d) * (a * c + -1 * (b * d)) = Int.ofNat (p + 1 + 1) * (a * a + c * c + -1 * Int.ofNat (p + 1 + 1)) := by
    have hb : b * b = Int.ofNat (p + 1 + 1) + -1 * (a * a) := by rw [← h1]; lib_int
    have hd : d * d = Int.ofNat (p + 1 + 1) + -1 * (c * c) := by rw [← h2]; lib_int
    rw [show (a * c + b * d) * (a * c + -1 * (b * d)) = a * a * (c * c) + -1 * ((b * b) * (d * d)) by lib_int, hb, hd]
    lib_int
  cases int_euclid p hp _ _ _ key with
  | inl h =>
    match h with
    | ⟨u, hu⟩ => exact Or.inl (case_plus p a b c d u h1 h2 hu)
  | inr h =>
    match h with
    | ⟨u, hu⟩ =>
      exact Or.inr (case_plus p a b c (-d) u h1 (by rw [← h2]; lib_int) (by rw [← hu]; lib_int))

/-- the squares {a², b²} are determined by p. -/
theorem squares_unique (p : Nat) (hp : ∀ d, Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (a b c d : Int)
    (h1 : a * a + b * b = Int.ofNat (p + 1 + 1)) (h2 : c * c + d * d = Int.ofNat (p + 1 + 1)) :
    c * c = a * a ∨ c * c = b * b := by
  have aux : ∀ dd : Int, Four a b c dd → c * c = a * a ∨ c * c = b * b := fun dd h => by
    rcases h with ⟨e, _⟩ | ⟨e, _⟩ | ⟨e, _⟩ | ⟨e, _⟩
    · left; rw [e]
    · left; rw [e]; lib_int
    · right; rw [e]; lib_int
    · right; rw [e]
  cases fermat_unique p hp a b c d h1 h2 with
  | inl h => exact aux d h
  | inr h => exact aux (-d) h

end FermatUnique

#print axioms FermatUnique.int_euclid
#print axioms FermatUnique.cancelP
#print axioms FermatUnique.add_cancel
#print axioms FermatUnique.lagrange
#print axioms FermatUnique.cancelQ
#print axioms FermatUnique.case_plus
#print axioms FermatUnique.Four
#print axioms FermatUnique.fermat_unique
#print axioms FermatUnique.squares_unique

lang/FibSum.lean

lean_all/lang/FibSum.lean

/-
  lang/FibSum — T48, cycle 722: **the 2-to-1 fibre sum for X = x + c/x** (OPEN 256a-split, step (ii-a)).
  In 𝔽_p (p = 2m + 3 prime), c ≠ 0, 2h = 1, for every g : 𝔽_p → ℤ:
    key        [(2x − X)² = X² − 4c] = [x ≠ 0] · [X = x + c/x]
    fib_sum    Σ_{x ≠ 0} g(x + c/x) = Σ_X g(X) · (1 + χ(X² − 4c))
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.SqCount
import lang.CharSumGen
import lang.G4Gen

namespace FibSum

open FTAChain ZMod YMChain FiniteRecChain DetChain QChar SqCount

variable (m : Nat)

theorem four_cancel (h a : Zp (2 * m + 1 + 1))
    (hh : (K m).add h h = (K m).one) (ha : (K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) a = (K m).zero) :
    a = (K m).zero := by
  have e : a = (K m).mul ((K m).mul h h) ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) a) := by
    calc a = (K m).mul ((K m).mul ((K m).add h h) ((K m).add h h)) a := by rw [hh]; lib_ring
      _ = _ := by lib_ring
  rw [e, ha]; lib_ring

/-- [(2x − X)² = X² − 4c] ⇔ x ≠ 0 ∧ X = x + c/x. -/
theorem key (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h c : Zp (2 * m + 1 + 1))
    (hh : (K m).add h h = (K m).one) (hc : c ≠ (K m).zero) (x X : Zp (2 * m + 1 + 1)) :
    delta LibInt.intR ((K m).mul ((K m).add ((K m).add x x) ((K m).neg X)) ((K m).add ((K m).add x x) ((K m).neg X)))
        ((K m).add ((K m).mul X X) ((K m).neg ((K m).add ((K m).add c c) ((K m).add c c))))
      = if x = (K m).zero then 0 else delta LibInt.intR X ((K m).add x ((K m).mul c (CharSumGen.iv (2 * m) x))) := by
  by_cases hx : x = (K m).zero
  · rw [ite_pos' _ _ hx]
    unfold delta
    refine ite_neg' _ _ (fun e => hc ?_)
    apply four_cancel m h c hh
    have e2 : (K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) c =
        (K m).add ((K m).mul ((K m).add ((K m).add x x) ((K m).neg X)) ((K m).add ((K m).add x x) ((K m).neg X)))
          ((K m).neg ((K m).add ((K m).mul X X) ((K m).neg ((K m).add ((K m).add c c) ((K m).add c c))))) := by
      rw [hx]; lib_ring
    rw [e2, e]; lib_ring
  · rw [ite_neg' _ _ hx]
    have hw := CharSumGen.iv_mul (2 * m) hpr x hx
    generalize CharSumGen.iv (2 * m) x = w at hw ⊢
    unfold delta
    by_cases hX : X = (K m).add x ((K m).mul c w)
    · rw [ite_pos' _ _ hX, ite_pos' _ _ (by
        rw [hX]
        calc _ = (K m).add ((K m).mul ((K m).add x ((K m).mul c w)) ((K m).add x ((K m).mul c w)))
                ((K m).add ((K m).neg ((K m).add ((K m).add c c) ((K m).add c c)))
                  ((K m).mul ((K m).add ((K m).add c c) ((K m).add c c)) ((K m).add (K m).one ((K m).neg ((K m).mul w x))))) := by lib_ring
          _ = _ := by rw [hw]; lib_ring)]
    · rw [ite_neg' _ _ hX, ite_neg' _ _ (fun e => hX ?_)]
      have e4 : (K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one))
          ((K m).mul x ((K m).add ((K m).add x ((K m).neg X)) ((K m).mul c w))) = (K m).zero := by
        calc _ = (K m).add ((K m).add ((K m).mul ((K m).add ((K m).add x x) ((K m).neg X)) ((K m).add ((K m).add x x) ((K m).neg X)))
                  ((K m).neg ((K m).add ((K m).mul X X) ((K m).neg ((K m).add ((K m).add c c) ((K m).add c c))))))
                ((K m).mul ((K m).add ((K m).add c c) ((K m).add c c)) ((K m).add ((K m).mul w x) ((K m).neg (K m).one))) := by lib_ring
          _ = (K m).zero := by rw [e, hw]; lib_ring
      have e5 := four_cancel m h _ hh e4
      rcases G4Gen.mul_eq_zero m hpr _ _ e5 with e6 | e6
      · exact absurd e6 hx
      · calc X = (K m).add ((K m).add x ((K m).mul c w)) ((K m).neg ((K m).add ((K m).add x ((K m).neg X)) ((K m).mul c w))) := by lib_ring
          _ = _ := by rw [e6]; lib_ring

/-- **Σ_{x ≠ 0} g(x + c/x) = Σ_X g(X)(1 + χ(X² − 4c)).** -/
theorem fib_sum (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h c : Zp (2 * m + 1 + 1))
    (hh : (K m).add h h = (K m).one) (hc : c ≠ (K m).zero) (g : Zp (2 * m + 1 + 1) → Int) :
    sumK LibInt.intR (fun x => if x = (K m).zero then 0 else g ((K m).add x ((K m).mul c (CharSumGen.iv (2 * m) x)))) (ConicSum.L m)
      = sumK LibInt.intR (fun X => g X * (1 + chi m ((K m).add ((K m).mul X X) ((K m).neg ((K m).add ((K m).add c c) ((K m).add c c)))))) (ConicSum.L m) := by
  have hcomp : Complete LibInt.intR (ConicSum.L m) := CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR
  -- 1 + χ(D) = Σ_u [u² = D] = Σ_x [(2x − X)² = D]
  have h1 : ∀ X, g X * (1 + chi m ((K m).add ((K m).mul X X) ((K m).neg ((K m).add ((K m).add c c) ((K m).add c c)))))
      = sumK LibInt.intR (fun x => LibInt.intR.mul (g X) (if x = (K m).zero then 0 else delta LibInt.intR X ((K m).add x ((K m).mul c (CharSumGen.iv (2 * m) x))))) (ConicSum.L m) := fun X => by
    rw [← sq_count m hpr]
    rw [← reindex LibInt.intR _ hcomp (fun x => (K m).add ((K m).add x x) ((K m).neg X)) (fun u => (K m).mul h ((K m).add u X))
      (fun x => by
        calc (K m).mul h ((K m).add ((K m).add ((K m).add x x) ((K m).neg X)) X) = (K m).mul ((K m).add h h) x := by lib_ring
          _ = x := by rw [hh]; lib_ring)
      (fun u => by
        calc (K m).add ((K m).add ((K m).mul h ((K m).add u X)) ((K m).mul h ((K m).add u X))) ((K m).neg X)
            = (K m).add ((K m).mul ((K m).add h h) ((K m).add u X)) ((K m).neg X) := by lib_ring
          _ = u := by rw [hh]; lib_ring)]
    show LibInt.intR.mul (g X) _ = _
    rw [mul_sumK]
    exact sumK_ext LibInt.intR _ _ (fun x => by rw [key m hpr h c hh hc x X]) _
  rw [sumK_ext LibInt.intR _ _ h1, sumK_swap]
  refine sumK_ext LibInt.intR _ _ (fun x => ?_) _
  by_cases hx : x = (K m).zero
  · rw [ite_pos' _ _ hx]
    rw [sumK_ext LibInt.intR _ (fun _ => LibInt.intR.zero) (fun X => by rw [ite_pos' _ _ hx]; show LibInt.intR.mul (g X) LibInt.intR.zero = LibInt.intR.zero; exact DetChain.mul_zero LibInt.intR _)]
    exact (zsum0 _).symm
  · rw [ite_neg' _ _ hx]
    rw [sumK_ext LibInt.intR _ (fun X => LibInt.intR.mul (delta LibInt.intR X ((K m).add x ((K m).mul c (CharSumGen.iv (2 * m) x)))) (g X))
      (fun X => by rw [ite_neg' _ _ hx]; exact LibInt.intR.mul_comm _ _)]
    exact (pick LibInt.intR _ hcomp g _).symm

end FibSum

#print axioms FibSum.four_cancel
#print axioms FibSum.key
#print axioms FibSum.fib_sum

lang/FieldTower.lean

lean_all/lang/FieldTower.lean

/-
  lang/FieldTower — T43, cycle 140: the tower 𝔽_p ⊂ 𝔽_{p²} ⊂ 𝔽_{p⁴} ⊂ … ⊂ 𝔽_{p^(2^k)} as finite fields of the interface.

    T k            the carrier: Zp for k = 0, pairs of the previous level
    findNR         a non-residue found by search in the element list (no choice principle)
    tower k        an FF on T k with h = j + 1                     (QuadExt at each step)
    tower_card     the order of level k + 1 is the square of the order of level k
  So for every odd prime p and every k, a field of order p^(2^k) exists, carrying all FF theorems.

  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.QuadExt

namespace FieldTower

open FTAChain ZMod YMChain FinField

section Find
variable {R : Type} [DecidableEq R] (F : FF R)

def findNR : List R → Option R
  | [] => none
  | a :: l => if a ≠ F.K.zero ∧ Frobenius.pw F.K a F.h ≠ F.K.one then some a else findNR l

theorem findNR_spec : ∀ (l : List R) (y : R), findNR F l = some y → y ≠ F.K.zero ∧ Frobenius.pw F.K y F.h ≠ F.K.one
  | [], _, h => nomatch h
  | a :: l, y, h => by
    change (if a ≠ F.K.zero ∧ Frobenius.pw F.K a F.h ≠ F.K.one then some a else findNR F l) = some y at h
    by_cases e : a ≠ F.K.zero ∧ Frobenius.pw F.K a F.h ≠ F.K.one
    · rw [FiniteRecChain.ite_pos' _ _ e] at h
      have : a = y := Option.some.inj h
      rw [← this]; exact e
    · rw [FiniteRecChain.ite_neg' _ _ e] at h; exact findNR_spec l y h

theorem findNR_some : ∀ (l : List R) (x : R), memL x l → x ≠ F.K.zero → Frobenius.pw F.K x F.h ≠ F.K.one →
    ∃ y, findNR F l = some y
  | [], _, h, _, _ => False.elim h
  | a :: l, x, hm, h0, h1 => by
    show ∃ y, (if a ≠ F.K.zero ∧ Frobenius.pw F.K a F.h ≠ F.K.one then some a else findNR F l) = some y
    by_cases e : a ≠ F.K.zero ∧ Frobenius.pw F.K a F.h ≠ F.K.one
    · rw [FiniteRecChain.ite_pos' _ _ e]; exact ⟨a, rfl⟩
    · rw [FiniteRecChain.ite_neg' _ _ e]
      cases hm with
      | inl ex => exact absurd ⟨ex ▸ h0, ex ▸ h1⟩ e
      | inr hl => exact findNR_some l x hl h0 h1

end Find

def T (m : Nat) : Nat → Type
  | 0 => Zp (2 * m + 1 + 1)
  | k + 1 => T m k × T m k

instance instDec (m : Nat) : ∀ k, DecidableEq (T m k)
  | 0 => inferInstanceAs (DecidableEq (Zp (2 * m + 1 + 1)))
  | k + 1 => @instDecidableEqProd _ _ (instDec m k) (instDec m k)

structure Level (m k : Nat) where
  F : @FF (T m k) (instDec m k)
  j : Nat
  hj : F.h = j + 1

def step {R : Type} [DecidableEq R] (F : FF R) (j : Nat) (hj : F.h = j + 1) :
    (G : FF (R × R)) ×' (G.h = (2 * j * j + 6 * j + 3) + 1) :=
  match hf : findNR F F.els with
  | some y =>
    have hy := findNR_spec F F.els y hf
    ⟨QuadExt.quadExt F y j hj hy.1 hy.2, rfl⟩
  | none => False.elim (by
      match F.nonres with
      | ⟨x, hx0, hx1⟩ =>
        match findNR_some F F.els x (FinList.mem_els F x) hx0 hx1 with
        | ⟨y, hy⟩ => rw [hf] at hy; exact nomatch hy)

def tower (m : Nat) (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : ∀ k, Level m k
  | 0 => ⟨ffp m hpr, m, rfl⟩
  | k + 1 =>
    match tower m hpr k with
    | ⟨F, j, hj⟩ =>
      match @step _ (instDec m k) F j hj with
      | ⟨G, hG⟩ => ⟨G, 2 * j * j + 6 * j + 3, hG⟩

/-- **The order squares at each level**: 2h' + 1 = (2h + 1)². -/
theorem order_sq (j : Nat) : 2 * ((2 * j * j + 6 * j + 3) + 1) + 1 = (2 * (j + 1) + 1) * (2 * (j + 1) + 1) := by lib_nat

end FieldTower

#print axioms FieldTower.findNR_spec
#print axioms FieldTower.findNR_some
#print axioms FieldTower.step
#print axioms FieldTower.tower
#print axioms FieldTower.order_sq

lang/FinCast.lean

lean_all/lang/FinCast.lean

/-
  lang/FinCast — T43, cycle 99: integer character sums reduced into the field, over every finite field.

    cast_chi_ff     castZ(χ(z)) = z^h                                     (Euler's criterion in FF)
    cast_sum_ff     castZ(Σ_x χ(g(x))) = Σ_x g(x)^h
    castN_quad      in 𝔽_p[√n], m·1 = (m·1, 0)                             (the prime field sits in the first coordinate)
    castZ_quad      castZ z = (castZ z, 0)
  So the Frobenius trace over 𝔽_q reduces mod p to the Hasse sum (FinHasse) — over 𝔽_{p²} as well, whose prime field is 𝔽_p.

  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.FinCyclic
import lang.IntCast
import lang.Fq2

namespace FinCast

open FTAChain YMChain FiniteRecChain FinField LibRing LibInt

variable {R : Type} [DecidableEq R] (F : FF R)

theorem cast_chi_ff (z : R) : castZ F.K (chi F z) = Frobenius.pw F.K z F.h := by
  have hpos := FinCyclic.h_pos F
  by_cases hz : z = F.K.zero
  · rw [hz, chi_zero]
    cases hh : F.h with
    | zero => rw [hh] at hpos; exact absurd hpos (Nat.not_succ_le_zero 0)
    | succ r => exact (HasseCM.pw_zero_succ F.K r).symm
  · cases F.euler_pm z hz with
    | inl h =>
      rw [chi_one_of F z hz h, h]
      show F.K.add F.K.one F.K.zero = F.K.one
      exact DetChain.add_zero' F.K _
    | inr h =>
      have h' : Frobenius.pw F.K z F.h ≠ F.K.one := fun e => F.ne_pm (h.symm.trans e)
      rw [chi_neg_of F z hz h', h]
      exact IntCast.castZ_neg_nat F.K 1 |>.trans (by show F.K.neg (F.K.add F.K.one F.K.zero) = _; rw [DetChain.add_zero'])

theorem cast_sum_ff (g : R → R) :
    castZ F.K (sumK intR (fun x => chi F (g x)) F.els) = sumK F.K (fun x => Frobenius.pw F.K (g x) F.h) F.els := by
  rw [IntCast.castZ_sum]
  exact sumK_ext F.K _ _ (fun x => cast_chi_ff F (g x)) _

section Quad
variable {S : Type} (K : DetChain.CRing S) (n : S)

theorem castN_quad : ∀ m, castN (Quad.quad K n) m = (castN K m, K.zero)
  | 0 => rfl
  | m + 1 => by
    show (Quad.quad K n).add (Quad.quad K n).one (castN (Quad.quad K n) m) = (K.add K.one (castN K m), K.zero)
    rw [castN_quad m]
    apply Prod.ext
    · rfl
    · show K.add K.zero K.zero = K.zero; exact K.zero_add _

theorem castZ_quad (z : Int) : castZ (Quad.quad K n) z = (castZ K z, K.zero) := by
  rw [IntCast.castZ_repr, IntCast.castZ_repr K z, castN_quad, castN_quad]
  apply Prod.ext
  · rfl
  · show K.add K.zero (K.neg K.zero) = K.zero; lib_ring

end Quad

end FinCast

#print axioms FinCast.cast_chi_ff
#print axioms FinCast.cast_sum_ff
#print axioms FinCast.castN_quad
#print axioms FinCast.castZ_quad

lang/FinChoice.lean

lean_all/lang/FinChoice.lean

/-
  lang/FinChoice — T49 (O3') (2): **finite choice without the choice axiom**: per-index existence below n gives
  one function (induction on n, the new value patched in with `if i = n`).
    fin_choice   (∀ i < n, ∃ q, P i q) ⇒ ∃ g, ∀ i < n, P i (g i)
  Used to assemble u(b) with W(b) = W(b₀) + p^k·u(b) from CastConst.cast_eq at every b.
  Lean 4 core only, proofs by hand, no axioms.
-/

namespace FinChoice

theorem ite_t {α : Type} {c : Prop} [d : Decidable c] (a b : α) (h : c) : @ite α c d a b = a := by
  cases d with
  | isTrue _ => rfl
  | isFalse hc => exact absurd h hc

theorem ite_f {α : Type} {c : Prop} [d : Decidable c] (a b : α) (h : ¬ c) : @ite α c d a b = b := by
  cases d with
  | isTrue hc => exact absurd hc h
  | isFalse _ => rfl

theorem fin_choice {A : Type} (P : Nat → A → Prop) (a0 : A) :
    ∀ n, (∀ i, i < n → ∃ q, P i q) → ∃ g : Nat → A, ∀ i, i < n → P i (g i)
  | 0, _ => ⟨fun _ => a0, fun i hi => absurd hi (Nat.not_lt_zero i)⟩
  | n + 1, h => by
    obtain ⟨g, hg⟩ := fin_choice P a0 n (fun i hi => h i (Nat.lt_trans hi (Nat.lt_succ_self n)))
    obtain ⟨q, hq⟩ := h n (Nat.lt_succ_self n)
    refine ⟨fun i => if i = n then q else g i, fun i hi => ?_⟩
    cases Nat.lt_or_ge i n with
    | inl hlt =>
      have hne : i ≠ n := fun e => Nat.lt_irrefl n (e ▸ hlt)
      show P i (if i = n then q else g i)
      rw [ite_f _ _ hne]; exact hg i hlt
    | inr hge =>
      have heq : i = n := Nat.le_antisymm (Nat.le_of_lt_succ hi) hge
      show P i (if i = n then q else g i)
      rw [ite_t _ _ heq, heq]; exact hq

end FinChoice

#print axioms FinChoice.ite_t
#print axioms FinChoice.ite_f
#print axioms FinChoice.fin_choice

lang/FinClass.lean

lean_all/lang/FinClass.lean

/-
  lang/FinClass — T43, cycle 67: Jacobsthal sums on square classes over any finite field (port of JacobClass):
    2 Σ_a φ(a)² = (q − 1)(φ(1)² + φ(n)²).
  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.FinMoment

namespace FinClass

open YMChain FiniteRecChain FinField FinConic FinMoment

variable {R : Type} [DecidableEq R] (F : FF R)

theorem z01 : (0 : Int) ≠ 1 := fun e => Nat.noConfusion (Int.ofNat.inj e)

theorem chi_cases (x : R) : chi F x = 0 ∨ chi F x = 1 ∨ chi F x = -1 := by
  unfold chi
  by_cases h0 : x = F.K.zero
  · rw [ite_pos' _ _ h0]; exact Or.inl rfl
  · rw [ite_neg' _ _ h0]
    by_cases h1 : Frobenius.pw F.K x F.h = F.K.one
    · rw [ite_pos' _ _ h1]; exact Or.inr (Or.inl rfl)
    · rw [ite_neg' _ _ h1]; exact Or.inr (Or.inr rfl)

theorem chi_pw_one (x : R) (h : chi F x = 1) : Frobenius.pw F.K x F.h = F.K.one := by
  unfold chi at h
  by_cases h0 : x = F.K.zero
  · rw [ite_pos' _ _ h0] at h; exact absurd h z01
  · rw [ite_neg' _ _ h0] at h
    by_cases h1 : Frobenius.pw F.K x F.h = F.K.one
    · exact h1
    · rw [ite_neg' _ _ h1] at h; exact absurd h (fun e => Int.noConfusion e)

theorem chi_ne_zero (x : R) (h : x ≠ F.K.zero) : chi F x ≠ 0 := by
  unfold chi
  rw [ite_neg' _ _ h]
  by_cases h1 : Frobenius.pw F.K x F.h = F.K.one
  · rw [ite_pos' _ _ h1]; exact fun e => z01 e.symm
  · rw [ite_neg' _ _ h1]; exact fun e => Int.noConfusion e

theorem chi_sq_val (t : R)
    (ht : t ≠ F.K.zero) : chi F t * chi F t = 1 := by
  rw [← chi_mul F]; exact chi_sq F t ht

theorem chi_iv (n : R)
    (hn : n ≠ F.K.zero) : chi F (F.iv n) = chi F n := by
  have h1 : chi F n * chi F (F.iv n) = 1 := by
    rw [← chi_mul F, F.K.mul_comm, F.iv_mul n hn, FinConic.chi_one F]
  have h2 := chi_sq_val F n hn
  -- c i = 1 and c c = 1 ⇒ i = c
  calc chi F (F.iv n) = (chi F n * chi F n) * chi F (F.iv n) := by rw [h2]; lib_int
    _ = chi F n * (chi F n * chi F (F.iv n)) := by lib_int
    _ = chi F n := by rw [h1]; lib_int

theorem square_of (a : R)
    (h : chi F a = 1) : ∃ t, t ≠ F.K.zero ∧ F.K.mul t t = a := by
  match F.euler_hard a (chi_pw_one F a h) with
  | ⟨t, ht⟩ =>
    refine ⟨t, fun e => ?_, ht⟩
    rw [e, DetChain.zero_mul'] at ht
    rw [← ht, chi_zero] at h
    exact z01 h

/-- **φ² on square classes.** -/
theorem phi_class (n : R)
    (hn0 : n ≠ F.K.zero) (hn : chi F n = -1) (a : R) :
    2 * (phi F a * phi F a) = phi F F.K.one * phi F F.K.one * (chi F a * chi F a + chi F a)
      + phi F n * phi F n * (chi F a * chi F a - chi F a) := by
  by_cases ha : a = F.K.zero
  · rw [ha, FinConic.phi_zero F, chi_zero]; lib_int
  cases chi_cases F a with
  | inl h0 => exact absurd h0 (chi_ne_zero F a ha)
  | inr h =>
    cases h with
    | inl h1 =>
      match square_of F a h1 with
      | ⟨t, ht0, ht⟩ =>
        have e : phi F a = chi F t * phi F F.K.one := by
          rw [← ht, ← FinConic.phi_scale F F.K.one t ht0, F.K.one_mul]
        rw [e, h1]
        have := chi_sq_val F t ht0
        calc 2 * (chi F t * phi F F.K.one * (chi F t * phi F F.K.one))
            = 2 * ((chi F t * chi F t) * (phi F F.K.one * phi F F.K.one)) := by lib_int
          _ = _ := by rw [this]; lib_int
    | inr hm =>
      have hq : chi F (F.K.mul a (F.iv n)) = 1 := by
        rw [chi_mul F, chi_iv F n hn0, hm, hn]; rfl
      match square_of F _ hq with
      | ⟨t, ht0, ht⟩ =>
        have ha' : a = F.K.mul n (F.K.mul t t) := by
          rw [ht, F.K.mul_comm a, ← F.K.mul_assoc, F.K.mul_comm n, F.iv_mul n hn0, F.K.one_mul]
        have e : phi F a = chi F t * phi F n := by rw [ha']; exact FinConic.phi_scale F n t ht0
        rw [e, hm]
        have := chi_sq_val F t ht0
        calc 2 * (chi F t * phi F n * (chi F t * phi F n))
            = 2 * ((chi F t * chi F t) * (phi F n * phi F n)) := by lib_int
          _ = _ := by rw [this]; lib_int

theorem sum_chi_sq :
    sumK Z (fun a => chi F a * chi F a) F.els = PP F + -1 := by
  have pt : ∀ a, chi F a * chi F a = Z.add 1 (Z.mul (-1) (delta Z a F.K.zero)) := fun a => by
    by_cases ha : a = F.K.zero
    · rw [ha, chi_zero, delta_one _ _ rfl]; rfl
    · rw [chi_sq_val F a ha, delta_zero _ _ ha]; rfl
  rw [sumK_ext Z _ _ pt, sumK_add, ← mul_sumK, sum_one, show sumK Z (fun x => delta Z x F.K.zero) F.els = 1 from F.comp F.K.zero]
  show PP F + -1 * 1 = PP F + -1
  lib_int

/-- **2 Σ_a φ(a)² = (p − 1)(φ(1)² + φ(n)²).** -/
theorem grouped (n : R)
    (hn0 : n ≠ F.K.zero) (hn : chi F n = -1) :
    2 * sumK Z (fun a => phi F a * phi F a) F.els
      = (PP F + -1) * (phi F F.K.one * phi F F.K.one + phi F n * phi F n) := by
  have pt : ∀ a, Z.mul 2 (phi F a * phi F a)
      = Z.add (Z.mul (phi F F.K.one * phi F F.K.one + phi F n * phi F n) (chi F a * chi F a))
          (Z.mul (phi F F.K.one * phi F F.K.one + -1 * (phi F n * phi F n)) (chi F a)) := fun a => by
    show 2 * (phi F a * phi F a) = _
    rw [phi_class F n hn0 hn a]
    show _ = (phi F F.K.one * phi F F.K.one + phi F n * phi F n) * (chi F a * chi F a)
      + (phi F F.K.one * phi F F.K.one + -1 * (phi F n * phi F n)) * chi F a
    lib_int
  show Z.mul 2 (sumK Z _ F.els) = _
  rw [mul_sumK, sumK_ext Z _ _ pt, sumK_add, ← mul_sumK, ← mul_sumK, sum_chi_sq F,
    show sumK Z (chi F) F.els = 0 from chi_sum F]
  show (phi F F.K.one * phi F F.K.one + phi F n * phi F n) * (PP F + -1)
    + (phi F F.K.one * phi F F.K.one + -1 * (phi F n * phi F n)) * 0 = _
  lib_int

end FinClass

#print axioms FinClass.z01
#print axioms FinClass.chi_cases
#print axioms FinClass.chi_pw_one
#print axioms FinClass.chi_ne_zero
#print axioms FinClass.chi_sq_val
#print axioms FinClass.chi_iv
#print axioms FinClass.square_of
#print axioms FinClass.phi_class
#print axioms FinClass.sum_chi_sq
#print axioms FinClass.grouped

lang/FinConic.lean

lean_all/lang/FinConic.lean

/-
  lang/FinConic — T43, cycle 66: the conic sum and Jacobsthal scaling over any finite field (FinField.FF).

    inv_inv, one_ne_zero, shift_sum, reindex_mul
    conic_sum      Σ_x χ((x + b)(x + c)) = −1 for b ≠ c
    phi_scale      φ(a t²) = χ(t) φ(a),  φ(a) = Σ_x χ(x (x² + a))
    phi_zero       φ(0) = 0
  Ports of ConicSum and Jacobsthal (𝔽_p) to every finite field of odd order, in particular 𝔽_{p²}.

  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.FinField

namespace FinConic

open YMChain FiniteRecChain FinField

abbrev Z := LibInt.intR

variable {R : Type} [DecidableEq R] (F : FF R)

theorem one_ne_zero : F.K.one ≠ F.K.zero := fun e => F.ne_pm (by
  rw [e]; exact (DetChain.neg_unique F.K _ _ (F.K.zero_add F.K.zero)).symm)

theorem iv_ne_zero (x : R) (hx : x ≠ F.K.zero) : F.iv x ≠ F.K.zero := fun e => by
  have := F.iv_mul x hx
  rw [e, DetChain.zero_mul'] at this
  exact one_ne_zero F this.symm

theorem inv_inv (x : R) : F.iv (F.iv x) = x := by
  by_cases hx : x = F.K.zero
  · rw [hx, F.iv_zero, F.iv_zero]
  · have h1 := F.iv_mul (F.iv x) (iv_ne_zero F x hx)
    have h2 := F.iv_mul x hx
    calc F.iv (F.iv x) = F.K.mul (F.iv (F.iv x)) (F.K.mul (F.iv x) x) := by rw [h2, F.K.mul_comm, F.K.one_mul]
      _ = F.K.mul (F.K.mul (F.iv (F.iv x)) (F.iv x)) x := by rw [F.K.mul_assoc]
      _ = x := by rw [h1, F.K.one_mul]

theorem shift_sum (G : R → Int) (c : R) : sumK Z (fun x => G (F.K.add x c)) F.els = sumK Z G F.els :=
  reindex Z _ F.comp (fun x => F.K.add x c) (fun x => F.K.add x (F.K.neg c))
    (fun x => by rw [F.K.add_assoc, F.K.add_neg, DetChain.add_zero'])
    (fun y => by rw [F.K.add_assoc, DetChain.add_neg', DetChain.add_zero']) G

theorem reindex_mul (t : R) (ht : t ≠ F.K.zero) (G : R → Int) :
    sumK Z (fun x => G (F.K.mul t x)) F.els = sumK Z G F.els :=
  reindex Z _ F.comp (F.K.mul t) (F.K.mul (F.iv t))
    (fun x => by rw [← F.K.mul_assoc, F.iv_mul t ht, F.K.one_mul])
    (fun y => by rw [← F.K.mul_assoc, F.K.mul_comm t, F.iv_mul t ht, F.K.one_mul]) G

theorem chi_one : chi F F.K.one = 1 :=
  chi_one_of F F.K.one (one_ne_zero F) (TwoPower.pwone F.K _)

theorem sum_recip (d : R) (hd : d ≠ F.K.zero) :
    sumK Z (fun x => chi F (F.K.add F.K.one (F.K.mul d (F.iv x)))) F.els = 0 := by
  have hdd : F.K.mul d (F.iv d) = F.K.one := by rw [F.K.mul_comm]; exact F.iv_mul d hd
  have inv1 : ∀ x, F.K.mul d (F.iv (F.K.mul d (F.iv x))) = x := fun x => by
    rw [F.iv_hom, inv_inv F, ← F.K.mul_assoc, hdd, F.K.one_mul]
  have hre := reindex Z F.els F.comp (fun x => F.K.mul d (F.iv x)) (fun x => F.K.mul d (F.iv x)) inv1 inv1
    (fun y => chi F (F.K.add F.K.one y))
  rw [hre]
  have h1 := shift_sum F (chi F) F.K.one
  have h2 : sumK Z (fun y => chi F (F.K.add F.K.one y)) F.els = sumK Z (fun x => chi F (F.K.add x F.K.one)) F.els :=
    sumK_ext Z _ _ (fun y => by rw [F.K.add_comm]) _
  rw [h2, h1]
  exact chi_sum F

theorem chi_pair (d x : R) (hx : x ≠ F.K.zero) :
    chi F (F.K.mul x (F.K.add x d)) = chi F (F.K.add F.K.one (F.K.mul d (F.iv x))) := by
  have e1 : F.K.mul (F.K.mul x x) (F.K.add F.K.one (F.K.mul d (F.iv x)))
      = F.K.add (F.K.mul x x) (F.K.mul (F.K.mul x d) (F.K.mul (F.iv x) x)) := by lib_ring
  rw [F.iv_mul x hx] at e1
  have e2 : F.K.add (F.K.mul x x) (F.K.mul (F.K.mul x d) F.K.one) = F.K.mul x (F.K.add x d) := by lib_ring
  rw [e2] at e1
  rw [← e1, chi_mul F, chi_sq F x hx]
  lib_int

theorem sum_xxd (d : R) (hd : d ≠ F.K.zero) : sumK Z (fun x => chi F (F.K.mul x (F.K.add x d))) F.els = -1 := by
  have pt : ∀ x, chi F (F.K.mul x (F.K.add x d))
      = Z.add (chi F (F.K.add F.K.one (F.K.mul d (F.iv x)))) (Z.mul (-1) (delta Z x F.K.zero)) := fun x => by
    by_cases hx : x = F.K.zero
    · rw [hx, DetChain.zero_mul', chi_zero, F.iv_zero, DetChain.mul_zero, DetChain.add_zero', chi_one]
      show (0 : Int) = 1 + -1 * (if F.K.zero = F.K.zero then 1 else 0)
      rw [ite_pos' _ _ rfl]; rfl
    · rw [chi_pair F d x hx]
      show _ = chi F _ + -1 * (if x = F.K.zero then 1 else 0)
      rw [ite_neg' _ _ hx]; lib_int
  rw [sumK_ext Z _ _ pt, sumK_add, sum_recip F d hd, ← mul_sumK]
  show (0 : Int) + -1 * sumK Z (fun y => delta Z y F.K.zero) F.els = -1
  rw [show sumK Z (fun y => delta Z y F.K.zero) F.els = 1 from F.comp F.K.zero]
  rfl

/-- **The conic sum over any finite field.** -/
theorem conic_sum (b c : R) (hbc : b ≠ c) :
    sumK Z (fun x => chi F (F.K.mul (F.K.add x b) (F.K.add x c))) F.els = -1 := by
  have hd : F.K.add b (F.K.neg c) ≠ F.K.zero := fun e => by
    apply hbc
    have := congrArg (fun z => F.K.add z c) e
    have e2 : F.K.add (F.K.add b (F.K.neg c)) c = b := by rw [F.K.add_assoc, DetChain.add_neg', DetChain.add_zero']
    rw [← e2]; exact this.trans (F.K.zero_add c)
  have e : sumK Z (fun x => chi F (F.K.mul (F.K.add x b) (F.K.add x c))) F.els
      = sumK Z (fun x => (fun y => chi F (F.K.mul y (F.K.add y (F.K.add b (F.K.neg c))))) (F.K.add x c)) F.els :=
    sumK_ext Z _ _ (fun x => by
      show chi F _ = chi F _
      have : F.K.mul (F.K.add x b) (F.K.add x c) = F.K.mul (F.K.add x c) (F.K.add (F.K.add x c) (F.K.add b (F.K.neg c))) := by
        lib_ring
      rw [this]) _
  rw [e, shift_sum F (fun y => chi F (F.K.mul y (F.K.add y (F.K.add b (F.K.neg c))))) c]
  exact sum_xxd F _ hd

def phi (a : R) : Int := sumK Z (fun x => chi F (F.K.mul x (F.K.add (F.K.mul x x) a))) F.els

theorem chi_cube (t : R) : chi F (F.K.mul t (F.K.mul t t)) = chi F t := by
  by_cases ht : t = F.K.zero
  · rw [ht, DetChain.zero_mul']
  · rw [chi_mul F, chi_sq F t ht]; lib_int

theorem phi_scale (a t : R) (ht : t ≠ F.K.zero) : phi F (F.K.mul a (F.K.mul t t)) = chi F t * phi F a := by
  have h := reindex_mul F t ht (fun x => chi F (F.K.mul x (F.K.add (F.K.mul x x) (F.K.mul a (F.K.mul t t)))))
  show sumK Z _ F.els = _
  rw [← h]
  have pt : ∀ x, chi F (F.K.mul (F.K.mul t x) (F.K.add (F.K.mul (F.K.mul t x) (F.K.mul t x)) (F.K.mul a (F.K.mul t t))))
      = Z.mul (chi F t) (chi F (F.K.mul x (F.K.add (F.K.mul x x) a))) := fun x => by
    have e : F.K.mul (F.K.mul t x) (F.K.add (F.K.mul (F.K.mul t x) (F.K.mul t x)) (F.K.mul a (F.K.mul t t)))
        = F.K.mul (F.K.mul t (F.K.mul t t)) (F.K.mul x (F.K.add (F.K.mul x x) a)) := by lib_ring
    rw [e, chi_mul F, chi_cube F]
    rfl
  rw [sumK_ext Z _ _ pt, ← mul_sumK]
  rfl

theorem phi_zero : phi F F.K.zero = 0 := by
  show sumK Z _ F.els = 0
  have pt : ∀ x, chi F (F.K.mul x (F.K.add (F.K.mul x x) F.K.zero)) = chi F x := fun x => by
    rw [DetChain.add_zero', ← F.K.mul_assoc]
    have : F.K.mul (F.K.mul x x) x = F.K.mul x (F.K.mul x x) := by lib_ring
    rw [this, chi_cube F]
  rw [sumK_ext Z _ _ pt]
  exact chi_sum F

end FinConic

#print axioms FinConic.one_ne_zero
#print axioms FinConic.iv_ne_zero
#print axioms FinConic.inv_inv
#print axioms FinConic.shift_sum
#print axioms FinConic.reindex_mul
#print axioms FinConic.chi_one
#print axioms FinConic.sum_recip
#print axioms FinConic.chi_pair
#print axioms FinConic.sum_xxd
#print axioms FinConic.conic_sum
#print axioms FinConic.chi_cube
#print axioms FinConic.phi_scale
#print axioms FinConic.phi_zero

lang/FinCubic.lean

lean_all/lang/FinCubic.lean

/-
  lang/FinCubic — T43, cycle 81: cubic Jacobsthal sums ψ(a) = Σ_x χ(x³ + a) over any finite field (ports of CubicJacob
  and CubicMoment): ψ(a t³) = χ(t) ψ(a), Σ χψ = 0, and Σ ψ² = 2q(q − 1) when F has a primitive cube root of unity.

  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.FinDLog
import lang.FinClass

namespace FinCubic

open YMChain FiniteRecChain FinField FinConic FinMoment

theorem cube_factor {R : Type} [DecidableEq R] (F : FF R) (y : R) :
    F.K.mul (F.K.add y (F.K.neg F.K.one)) (F.K.add (F.K.add (F.K.mul y y) y) F.K.one)
      = F.K.add (F.K.mul (F.K.mul y y) y) (F.K.neg F.K.one) := by lib_ring

variable {R : Type} [DecidableEq R] (F : FF R)

def cube (x : R) : R := F.K.mul x (F.K.mul x x)

def psi (a : R) : Int := sumK Z (fun x => chi F (F.K.add (cube F x) a)) F.els

theorem cube_mul (t x : R) : cube F (F.K.mul t x) = F.K.mul (cube F t) (cube F x) := by
  unfold cube; lib_ring

/-- **ψ(a t³) = χ(t) ψ(a).** -/
theorem psi_scale (a t : R)
    (ht : t ≠ F.K.zero) : psi F (F.K.mul a (cube F t)) = chi F t * psi F a := by
  have h := FinConic.reindex_mul F t ht (fun x => chi F (F.K.add (cube F x) (F.K.mul a (cube F t))))
  show sumK Z _ F.els = _
  rw [← h]
  have pt : ∀ x, chi F (F.K.add (cube F (F.K.mul t x)) (F.K.mul a (cube F t)))
      = Z.mul (chi F t) (chi F (F.K.add (cube F x) a)) := fun x => by
    have e : F.K.add (cube F (F.K.mul t x)) (F.K.mul a (cube F t)) = F.K.mul (cube F t) (F.K.add (cube F x) a) := by
      unfold cube; lib_ring
    rw [e, chi_mul F, show cube F t = F.K.mul t (F.K.mul t t) from rfl, FinConic.chi_cube F]
    rfl
  rw [sumK_ext Z _ _ pt, ← mul_sumK]
  rfl

theorem chi_psi_const (a t : R)
    (ht : t ≠ F.K.zero) : chi F (F.K.mul a (cube F t)) * psi F (F.K.mul a (cube F t)) = chi F a * psi F a := by
  rw [psi_scale F a t ht, chi_mul F, show cube F t = F.K.mul t (F.K.mul t t) from rfl, FinConic.chi_cube F]
  have := FinClass.chi_sq_val F t ht
  calc chi F a * chi F t * (chi F t * psi F a) = (chi F t * chi F t) * (chi F a * psi F a) := by lib_int
    _ = chi F a * psi F a := by rw [this]; lib_int

theorem psi_sq_const (a t : R)
    (ht : t ≠ F.K.zero) : psi F (F.K.mul a (cube F t)) * psi F (F.K.mul a (cube F t)) = psi F a * psi F a := by
  rw [psi_scale F a t ht]
  have := FinClass.chi_sq_val F t ht
  calc chi F t * psi F a * (chi F t * psi F a) = (chi F t * chi F t) * (psi F a * psi F a) := by lib_int
    _ = psi F a * psi F a := by rw [this]; lib_int

/-- **Σ_a χ(a) ψ(a) = 0.** -/
theorem twisted :
    sumK Z (fun a => chi F a * psi F a) F.els = 0 := by
  have e1 : ∀ a, chi F a * psi F a = sumK Z (fun x => chi F (F.K.mul (F.K.add a F.K.zero) (F.K.add a (cube F x)))) F.els :=
    fun a => by
      show Z.mul (chi F a) (sumK Z _ F.els) = _
      rw [mul_sumK]
      apply sumK_ext
      intro x
      show chi F a * chi F (F.K.add (cube F x) a) = _
      rw [← chi_mul F]
      have : F.K.mul a (F.K.add (cube F x) a) = F.K.mul (F.K.add a F.K.zero) (F.K.add a (cube F x)) := by lib_ring
      rw [this]
  rw [sumK_ext Z _ _ e1, sumK_swap, sumK_ext Z _ _ (fun x => FinMoment.inner F F.K.zero (cube F x))]
  -- Σ_x (−1 + p δ(0, x³)) = −p + p · 1
  have hd : ∀ x, delta Z F.K.zero (cube F x) = delta Z x F.K.zero := fun x => by
    by_cases hx : x = F.K.zero
    · rw [hx, show cube F F.K.zero = F.K.zero by unfold cube; rw [DetChain.zero_mul'], FinMoment.delta_one _ _ rfl]
    · have hc : F.K.zero ≠ cube F x := fun e => by
        have h0 : F.K.mul x (F.K.mul x x) = F.K.zero := e.symm
        cases F.nozd _ _ h0 with
        | inl h => exact hx h
        | inr h =>
          cases F.nozd _ _ h with
          | inl h' => exact hx h'
          | inr h' => exact hx h'
      rw [FinMoment.delta_zero _ _ hc, FinMoment.delta_zero _ _ hx]
  have pt : ∀ x, -1 + PP F * delta Z F.K.zero (cube F x) = Z.add (-1) (Z.mul (PP F) (delta Z x F.K.zero)) := fun x => by
    rw [hd]; rfl
  rw [sumK_ext Z _ _ pt, sumK_add, ← mul_sumK, show sumK Z (fun x => delta Z x F.K.zero) F.els = 1 from F.comp F.K.zero]
  have : sumK Z (fun _ => (-1 : Int)) F.els = -1 * PP F := by
    rw [← FinMoment.sum_one F]
    show _ = Z.mul (-1) (sumK Z (fun _ => (1 : Int)) F.els)
    rw [mul_sumK]
    rfl
  rw [this]
  show -1 * PP F + PP F * 1 = 0
  lib_int



theorem omega_rel (w : R)
    (hw1 : w ≠ F.K.one) (hw3 : cube F w = F.K.one) : F.K.add (F.K.add (F.K.mul w w) w) F.K.one = F.K.zero := by
  have hc := cube_factor F w
  have : F.K.mul (F.K.mul w w) w = cube F w := by unfold cube; lib_ring
  rw [this, hw3, F.K.add_neg] at hc
  cases F.nozd _ _ hc with
  | inl h => exact absurd (FinMoment.eq_of_sub_zero F w F.K.one h) hw1
  | inr h => exact h

theorem factor3 (z w : R) (hs : F.K.add (F.K.add (F.K.mul w w) w) F.K.one = F.K.zero)
    (hw3 : cube F w = F.K.one) :
    F.K.mul (F.K.mul (F.K.add z (F.K.neg F.K.one)) (F.K.add z (F.K.neg w))) (F.K.add z (F.K.neg (F.K.mul w w)))
      = F.K.add (cube F z) (F.K.neg F.K.one) := by
  have e : F.K.mul (F.K.mul (F.K.add z (F.K.neg F.K.one)) (F.K.add z (F.K.neg w))) (F.K.add z (F.K.neg (F.K.mul w w)))
      = F.K.add (F.K.add (F.K.add (cube F z) (F.K.neg (F.K.mul (F.K.add (F.K.add (F.K.mul w w) w) F.K.one) (F.K.mul z z))))
          (F.K.mul (F.K.mul w (F.K.add (F.K.add (F.K.mul w w) w) F.K.one)) z)) (F.K.neg (cube F w)) := by
    unfold cube; lib_ring
  rw [e, hs, hw3, DetChain.zero_mul', DetChain.mul_zero, DetChain.zero_mul']
  have : F.K.neg F.K.zero = F.K.zero := (DetChain.neg_unique F.K _ _ (F.K.zero_add F.K.zero)).symm
  rw [this, DetChain.add_zero', DetChain.add_zero']

theorem cube_zero : cube F F.K.zero = F.K.zero := by unfold cube; rw [DetChain.zero_mul']

theorem roots (z w : R)
    (hs : F.K.add (F.K.add (F.K.mul w w) w) F.K.one = F.K.zero) (hw3 : cube F w = F.K.one) (hz : cube F z = F.K.one) :
    z = F.K.one ∨ z = w ∨ z = F.K.mul w w := by
  have h := factor3 F z w hs hw3
  rw [hz, F.K.add_neg] at h
  cases F.nozd _ _ h with
  | inl h1 =>
    cases F.nozd _ _ h1 with
    | inl h2 => exact Or.inl (FinMoment.eq_of_sub_zero F _ _ h2)
    | inr h2 => exact Or.inr (Or.inl (FinMoment.eq_of_sub_zero F _ _ h2))
  | inr h1 => exact Or.inr (Or.inr (FinMoment.eq_of_sub_zero F _ _ h1))

theorem w_ne_ww (w : R)
    (hw1 : w ≠ F.K.one) (hw3 : cube F w = F.K.one) : w ≠ F.K.mul w w := fun e => by
  have h : F.K.mul w (F.K.add w (F.K.neg F.K.one)) = F.K.zero := by
    have : F.K.mul w (F.K.add w (F.K.neg F.K.one)) = F.K.add (F.K.mul w w) (F.K.neg w) := by lib_ring
    rw [this, ← e, F.K.add_neg]
  cases F.nozd _ _ h with
  | inl h0 => rw [h0, cube_zero] at hw3; exact FinConic.one_ne_zero F hw3.symm
  | inr h1 => exact hw1 (FinMoment.eq_of_sub_zero F _ _ h1)

theorem one_ne_ww (w : R) (hw1 : w ≠ F.K.one) (hw3 : cube F w = F.K.one) : F.K.one ≠ F.K.mul w w :=
  fun e => hw1 (by
    have : cube F w = F.K.mul w (F.K.mul w w) := rfl
    rw [← e, F.K.mul_comm, F.K.one_mul] at this
    exact this.symm.trans hw3)

theorem cube_ww (w : R) (hw3 : cube F w = F.K.one) : cube F (F.K.mul w w) = F.K.one := by
  have : cube F (F.K.mul w w) = F.K.mul (cube F w) (cube F w) := by unfold cube; lib_ring
  rw [this, hw3, F.K.one_mul]

theorem cube_one : cube F F.K.one = F.K.one := by unfold cube; rw [F.K.one_mul, F.K.one_mul]

/-- **Exactly three cube roots of unity.** -/
theorem count_roots (w : R)
    (hw1 : w ≠ F.K.one) (hw3 : cube F w = F.K.one) :
    sumK Z (fun z => delta Z (cube F z) F.K.one) F.els = 3 := by
  have hs := omega_rel F w hw1 hw3
  have d1 := w_ne_ww F w hw1 hw3
  have d2 := one_ne_ww F w hw1 hw3
  have pt : ∀ z, delta Z (cube F z) F.K.one
      = Z.add (delta Z z F.K.one) (Z.add (delta Z z w) (delta Z z (F.K.mul w w))) := fun z => by
    by_cases h1 : z = F.K.one
    · rw [h1, cube_one, FinMoment.delta_one _ _ rfl, FinMoment.delta_zero _ _ (fun e => hw1 e.symm),
        FinMoment.delta_zero _ _ d2]; rfl
    by_cases h2 : z = w
    · rw [h2, hw3, FinMoment.delta_one _ _ rfl, FinMoment.delta_zero _ _ hw1, FinMoment.delta_one _ _ rfl, FinMoment.delta_zero _ _ d1]; rfl
    by_cases h3 : z = F.K.mul w w
    · rw [h3, cube_ww F w hw3, FinMoment.delta_one _ _ rfl, FinMoment.delta_zero _ _ (fun e => d2 e.symm),
        FinMoment.delta_zero _ _ (fun e => d1 e.symm), FinMoment.delta_one _ _ rfl]; rfl
    · have hc : cube F z ≠ F.K.one := fun e => by
        cases roots F z w hs hw3 e with
        | inl h => exact h1 h
        | inr h => cases h with
          | inl h => exact h2 h
          | inr h => exact h3 h
      rw [FinMoment.delta_zero _ _ hc, FinMoment.delta_zero _ _ h1, FinMoment.delta_zero _ _ h2, FinMoment.delta_zero _ _ h3]; rfl
  rw [sumK_ext Z _ _ pt, sumK_add, sumK_add,
    show sumK Z (fun z => delta Z z F.K.one) F.els = 1 from F.comp F.K.one,
    show sumK Z (fun z => delta Z z w) F.els = 1 from F.comp w,
    show sumK Z (fun z => delta Z z (F.K.mul w w)) F.els = 1 from F.comp _]
  rfl

theorem cube_ne_zero (x : R)
    (hx : x ≠ F.K.zero) : cube F x ≠ F.K.zero := fun e => by
  cases F.nozd _ _ e with
  | inl h => exact hx h
  | inr h =>
    cases F.nozd _ _ h with
    | inl h' => exact hx h'
    | inr h' => exact hx h'

theorem count_x (w : R)
    (hw1 : w ≠ F.K.one) (hw3 : cube F w = F.K.one) (x : R) :
    sumK Z (fun y => delta Z (cube F x) (cube F y)) F.els = 3 + -2 * delta Z x F.K.zero := by
  by_cases hx : x = F.K.zero
  · rw [hx, cube_zero, FinMoment.delta_one _ _ rfl]
    have pt : ∀ y, delta Z F.K.zero (cube F y) = delta Z y F.K.zero := fun y => by
      by_cases hy : y = F.K.zero
      · rw [hy, cube_zero, FinMoment.delta_one _ _ rfl]
      · rw [FinMoment.delta_zero _ _ (fun e => cube_ne_zero F y hy e.symm), FinMoment.delta_zero _ _ hy]
    rw [sumK_ext Z _ _ pt, show sumK Z (fun y => delta Z y F.K.zero) F.els = 1 from F.comp F.K.zero]
    rfl
  · rw [FinMoment.delta_zero _ _ hx, ← FinConic.reindex_mul F x hx (fun y => delta Z (cube F x) (cube F y))]
    have pt : ∀ z, delta Z (cube F x) (cube F (F.K.mul x z)) = delta Z (cube F z) F.K.one := fun z => by
      rw [cube_mul]
      by_cases hz : cube F z = F.K.one
      · rw [hz, F.K.mul_comm, F.K.one_mul, FinMoment.delta_one _ _ rfl, FinMoment.delta_one _ _ rfl]
      · have hne : cube F x ≠ F.K.mul (cube F x) (cube F z) := fun e => by
          have h0 : F.K.mul (cube F x) (F.K.add (cube F z) (F.K.neg F.K.one)) = F.K.zero := by
            have : F.K.mul (cube F x) (F.K.add (cube F z) (F.K.neg F.K.one))
                = F.K.add (F.K.mul (cube F x) (cube F z)) (F.K.neg (cube F x)) := by lib_ring
            rw [this, ← e, F.K.add_neg]
          cases F.nozd _ _ h0 with
          | inl h => exact cube_ne_zero F x hx h
          | inr h => exact hz (FinMoment.eq_of_sub_zero F _ _ h)
        rw [FinMoment.delta_zero _ _ hne, FinMoment.delta_zero _ _ hz]
    rw [sumK_ext Z _ _ pt, count_roots F w hw1 hw3]
    rfl

/-- **Σ_a ψ(a)² = 2p(p − 1)** for p ≡ 1 (mod 3). -/
theorem moment_c (w : R)
    (hw1 : w ≠ F.K.one) (hw3 : cube F w = F.K.one) :
    sumK Z (fun a => psi F a * psi F a) F.els = 2 * PP F * (PP F + -1) := by
  have e1 : ∀ a, psi F a * psi F a = sumK Z (fun x => sumK Z (fun y =>
      chi F (F.K.add (cube F x) a) * chi F (F.K.add (cube F y) a)) F.els) F.els := fun a => by
    show Z.mul (sumK Z _ F.els) (sumK Z _ F.els) = _
    rw [sumK_mul]
    exact sumK_ext Z _ _ (fun x => mul_sumK Z _ _ F.els) _
  rw [sumK_ext Z _ _ e1, sumK_swap]
  have e2 : ∀ x, sumK Z (fun a => sumK Z (fun y =>
      chi F (F.K.add (cube F x) a) * chi F (F.K.add (cube F y) a)) F.els) F.els
      = Z.add (Z.mul (-1) (PP F)) (Z.mul (PP F) (3 + -2 * delta Z x F.K.zero)) := fun x => by
    rw [sumK_swap]
    have e3 : ∀ y, sumK Z (fun a => chi F (F.K.add (cube F x) a) * chi F (F.K.add (cube F y) a)) F.els
        = Z.add (-1) (Z.mul (PP F) (delta Z (cube F x) (cube F y))) := fun y => by
      have pt : ∀ a, chi F (F.K.add (cube F x) a) * chi F (F.K.add (cube F y) a)
          = chi F (F.K.mul (F.K.add a (cube F x)) (F.K.add a (cube F y))) := fun a => by
        rw [← chi_mul F, F.K.add_comm (cube F x), F.K.add_comm (cube F y)]
      rw [sumK_ext Z _ _ pt, FinMoment.inner F]
      rfl
    rw [sumK_ext Z _ _ e3, sumK_add, ← mul_sumK, count_x F w hw1 hw3 x]
    have : sumK Z (fun _ => (-1 : Int)) F.els = Z.mul (-1) (PP F) := by
      rw [← FinMoment.sum_one F, mul_sumK]; rfl
    rw [this]
  rw [sumK_ext Z _ _ e2, sumK_add, ← mul_sumK]
  have hs : sumK Z (fun x => Z.mul (PP F) (3 + -2 * delta Z x F.K.zero)) F.els = PP F * (3 * PP F + -2) := by
    rw [← mul_sumK]
    have pt : ∀ x, (3 : Int) + -2 * delta Z x F.K.zero = Z.add 3 (Z.mul (-2) (delta Z x F.K.zero)) := fun _ => rfl
    rw [sumK_ext Z _ _ pt, sumK_add, ← mul_sumK, show sumK Z (fun x => delta Z x F.K.zero) F.els = 1 from F.comp F.K.zero]
    have : sumK Z (fun _ => (3 : Int)) F.els = 3 * PP F := by
      rw [← FinMoment.sum_one F]
      show _ = Z.mul 3 (sumK Z (fun _ => (1 : Int)) F.els)
      rw [mul_sumK]; rfl
    rw [this]
    show PP F * (3 * PP F + -2 * 1) = _
    lib_int
  rw [hs]
  have h1 : sumK Z (fun _ => PP F) F.els = Z.mul (PP F) (sumK Z (fun _ => (1 : Int)) F.els) := by
    rw [mul_sumK]
    exact sumK_ext Z _ _ (fun _ => by show PP F = PP F * 1; lib_int) _
  rw [h1, sum_one]
  show -1 * (PP F * PP F) + PP F * (3 * PP F + -2) = _
  lib_int

end FinCubic

#print axioms FinCubic.cube_factor
#print axioms FinCubic.cube_mul
#print axioms FinCubic.psi_scale
#print axioms FinCubic.chi_psi_const
#print axioms FinCubic.psi_sq_const
#print axioms FinCubic.twisted
#print axioms FinCubic.omega_rel
#print axioms FinCubic.factor3
#print axioms FinCubic.cube_zero
#print axioms FinCubic.roots
#print axioms FinCubic.w_ne_ww
#print axioms FinCubic.one_ne_ww
#print axioms FinCubic.cube_ww
#print axioms FinCubic.cube_one
#print axioms FinCubic.count_roots
#print axioms FinCubic.cube_ne_zero
#print axioms FinCubic.count_x
#print axioms FinCubic.moment_c

lang/FinCubicExact.lean

lean_all/lang/FinCubicExact.lean

/-
  lang/FinCubicExact — T43, cycle 83: y² = x³ + 1 over every finite field 𝔽_q with q ≡ 1 (mod 3).

    psi_zero, chi_g, chi_gg     ψ(0) = 0, χ(g) = −1 for a primitive root, χ(g²) = 1
    fin_cubic_trace             ∃ u w, u² + 3w² = q ∧ ψ(1) = ±2u
  Port of CubicTrace to the interface (FinCubic, FinCubicTrace, FinCyclic, FinDLog).

  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.FinCubicTrace
import lang.CMModular
import lang.EisenNorm

namespace FinCubicExact

open FTAChain YMChain FiniteRecChain FinField FinConic FinMoment FinCubic FinDLog OrdMul

variable {R : Type} [DecidableEq R] (F : FF R)

theorem cancel_N (N : Nat) (hN : N ≠ 0) (X Y : Int) (h : Int.ofNat N * X = Int.ofNat N * Y) : X = Y :=
  CubicTrace.cancel_N N hN X Y h

theorem psi_zero : psi F F.K.zero = 0 := by
  show sumK Z (fun x => chi F (F.K.add (cube F x) F.K.zero)) F.els = 0
  rw [sumK_ext Z _ (chi F) (fun x => by rw [DetChain.add_zero']; exact FinConic.chi_cube F x)]
  exact chi_sum F

theorem chi_g (g : R) (hg : Ord F.K g (2 * F.h)) : chi F g = -1 := by
  have hg0 := FinDLog.g_ne_zero F g hg
  cases FinClass.chi_cases F g with
  | inl h0 => exact absurd h0 (FinClass.chi_ne_zero F g hg0)
  | inr h =>
    cases h with
    | inr h1 => exact h1
    | inl h1 =>
      exfalso
      have hp := FinClass.chi_pw_one F g h1
      match hg.2 F.h hp with
      | ⟨c, hc⟩ =>
        have hpos := FinCyclic.h_pos F
        cases c with
        | zero => rw [Nat.mul_zero] at hc; rw [hc] at hpos; exact Nat.not_succ_le_zero 0 hpos
        | succ c =>
          have : 2 * F.h ≤ F.h := by
            calc 2 * F.h ≤ 2 * F.h * c + 2 * F.h := Nat.le_add_left _ _
              _ = F.h := hc.symm
          have h2 : F.h < 2 * F.h := by rw [Nat.two_mul]; exact Nat.lt_add_of_pos_right hpos
          exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le h2 this)

theorem chi_gg (g : R) (hg0 : g ≠ F.K.zero) : chi F (F.K.mul g g) = 1 := chi_sq F g hg0

theorem fin_cubic_trace (N : Nat) (hN : 2 * F.h = 3 * N) :
    ∃ u w : Nat, u * u + 3 * (w * w) = 2 * F.h + 1 ∧
      (psi F F.K.one = Int.ofNat (2 * u) ∨ psi F F.K.one = -Int.ofNat (2 * u)) := by
  have hN0 : N ≠ 0 := fun e => by
    rw [e] at hN
    have := FinCyclic.h_pos F
    have h2 : 2 * 1 ≤ 2 * F.h := Nat.mul_le_mul_left 2 this
    rw [hN] at h2; exact Nat.not_succ_le_zero 1 h2
  match FinCyclic.primitive_root_ff F, FinCyclic.split_pow_ff F 1 0 ⟨N, by rw [hN]; lib_nat⟩ with
  | ⟨g, hg⟩, ⟨w, hw3', hw1'⟩ =>
    have hw1 : w ≠ F.K.one := fun e => hw1' (by rw [e]; exact TwoPower.pwone F.K _)
    have hw3 : cube F w = F.K.one := by
      rw [← hw3']; show _ = F.K.mul (F.K.mul (F.K.mul F.K.one w) w) w; unfold cube; lib_ring
    have hg0 : g ≠ F.K.zero := FinDLog.g_ne_zero F g hg
    have hP : PP F = 3 * Int.ofNat N + 1 := by
      show Int.ofNat (2 * F.h + 1) = _
      rw [hN]; rfl
    -- second moment, grouped
    have hpw : pows F g (2 * F.h) = pows F g (3 * N) := congrArg (pows F g) hN
    have M := moment_c F w hw1 hw3
    rw [sum_split_ff F Z g hg _ F.comp, hpw,
      FinCubicTrace.group3_ff F g hg0 (fun a => psi F a * psi F a) (fun a t ht => psi_sq_const F a t ht) N,
      psi_zero F] at M
    have S2 : psi F F.K.one * psi F F.K.one + psi F g * psi F g + psi F (F.K.mul g g) * psi F (F.K.mul g g)
        = 6 * PP F := by
      apply cancel_N N hN0
      have : Int.ofNat N * (6 * PP F) = 2 * PP F * (PP F + -1) := by rw [hP]; lib_int
      rw [this, ← M]
      show Int.ofNat N * _ = (0 * 0 : Int) + Int.ofNat N * _
      lib_int
    -- twisted sum, grouped
    have T := twisted F
    rw [sum_split_ff F Z g hg _ F.comp, hpw,
      FinCubicTrace.group3_ff F g hg0 (fun a => chi F a * psi F a) (fun a t ht => chi_psi_const F a t ht) N,
      chi_zero, FinConic.chi_one F, chi_g F g hg, chi_gg F g hg0] at T
    have S1 : psi F F.K.one + -1 * psi F g + psi F (F.K.mul g g) = 0 := by
      apply cancel_N N hN0
      rw [show Int.ofNat N * 0 = 0 by lib_int, ← T]
      show _ = 0 * psi F F.K.zero + _
      lib_int
    -- ψ(1) = 2c
    have hE := FinCubicTrace.psi_one_even F w hw1 hw3
    generalize hc : Int.ofNat F.h + -1 + -1 * sumK Z (fun x => FinCubicTrace.nq F (F.K.add (cube F x) F.K.one)) F.els = c at hE
    -- (A₂ + c)² + 3c² = 3p
    have key : (psi F (F.K.mul g g) + c) * (psi F (F.K.mul g g) + c) + 3 * (c * c) = 3 * PP F := by
      have h1 : psi F g = psi F F.K.one + psi F (F.K.mul g g) := by
        have e : psi F F.K.one + psi F (F.K.mul g g)
            = (psi F F.K.one + -1 * psi F g + psi F (F.K.mul g g)) + psi F g := by lib_int
        rw [S1] at e
        rw [e]; lib_int
      rw [h1, hE] at S2
      apply cancel_N 2 (fun e => Nat.noConfusion e)
      show (2 : Int) * _ = 2 * _
      rw [show (2 : Int) * (3 * PP F) = 6 * PP F by lib_int, ← S2]
      lib_int
    match CMModular.nat_sq (psi F (F.K.mul g g) + c), CMModular.nat_sq c with
    | ⟨n, hn, _⟩, ⟨u, hu, hsu⟩ =>
      rw [hn, hu] at key
      have key' : Int.ofNat (n * n + 3 * (u * u)) = Int.ofNat (3 * (2 * F.h + 1)) := key
      have hnat := Int.ofNat.inj key'
      have h3 : 3 ∣ n * n := by
        have a : Dv (2 + 1) (3 * (u * u) + n * n) := ⟨2 * F.h + 1, by rw [Nat.add_comm, hnat]; lib_nat⟩
        have b : Dv (2 + 1) (3 * (u * u)) := ⟨u * u, by lib_nat⟩
        exact PrimRoot.dvd_of_dv (dv_sub 2 _ _ b a)
      have h3' : 3 ∣ n := by
        cases LibGcd.euclid_prime EisenNorm.prime3' h3 with
        | inl x => exact x
        | inr x => exact x
      match h3' with
      | ⟨wv, ewv⟩ =>
        refine ⟨u, wv, ?_, ?_⟩
        · apply Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ 2)
          rw [← hnat, ewv]; lib_nat
        · rw [hE]
          cases hsu with
          | inl e => left; rw [e]; show 2 * Int.ofNat u = Int.ofNat (2 * u); rfl
          | inr e => right; rw [e]; show 2 * -Int.ofNat u = -Int.ofNat (2 * u)
                     show 2 * -Int.ofNat u = -(2 * Int.ofNat u); lib_int


end FinCubicExact

#print axioms FinCubicExact.cancel_N
#print axioms FinCubicExact.psi_zero
#print axioms FinCubicExact.chi_g
#print axioms FinCubicExact.chi_gg
#print axioms FinCubicExact.fin_cubic_trace

lang/FinCubicNorm.lean

lean_all/lang/FinCubicNorm.lean

/-
  lang/FinCubicNorm — T43, cycle 115: every sextic twist over any 𝔽_q with q ≡ 1 (mod 3): ψ(D) ≡ χ(D) (mod 3) and
  ψ(D)² + 3B² = 4q.

  ψ(D) = Σ_x χ(x³ + D), g a primitive root, A_i = ψ(g^i):
    psi_mod3        ψ(D) = χ(D) + 3T                                   (cube_sum_ff with G(y) = χ(y + D))
    rels            A₀ − A₁ + A₂ = 0,  A₀² + A₁² + A₂² = 6q              (as in FinCubicExact)
    class_sq        D ≠ 0 ⇒ ψ(D)² ∈ {A₀², A₁², A₂²}                       (D = g^i t³, psi_sq_const)
    norm_twist      D ≠ 0 ⇒ ∃ B, ψ(D)² + 3B² = 4q                         (3A_i² + (A_j ± 2A_k)² = 12q, A_i ≡ χ(g^i) mod 3)
  So the Frobenius trace of every curve y² = x³ + D over 𝔽_q is the trace of an Eisenstein integer of norm q, and
  ψ(D)² ≤ 4q (Hasse's bound for the whole sextic family over every such field).

  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.FinCubicExact
import lang.EveryPrime3

namespace FinCubicNorm

open FTAChain YMChain FiniteRecChain FinField FinConic FinMoment FinCubic FinDLog OrdMul LibInt

variable {R : Type} [DecidableEq R] (F : FF R)

theorem psi_mod3 (N : Nat) (hN : 2 * F.h = 3 * N) (a : R) : ∃ T : Int, psi F a = chi F a + 3 * T := by
  match FinCyclic.primitive_root_ff F with
  | ⟨g, hg⟩ =>
    refine ⟨CubicSign.sumR (fun k => chi F (F.K.add (FinCubicTrace.cb F (Frobenius.pw F.K g k)) a)) N, ?_⟩
    have h := FinCubicTrace.cube_sum_ff F g hg N hN (fun y => chi F (F.K.add y a))
    show sumK Z (fun x => chi F (F.K.add (cube F x) a)) F.els = _
    rw [show (fun x => chi F (F.K.add (cube F x) a)) = (fun x => chi F (F.K.add (FinCubicTrace.cb F x) a)) from rfl, h,
      F.K.zero_add]

theorem rels (N : Nat) (hN : 2 * F.h = 3 * N) (g : R) (hg : Ord F.K g (2 * F.h)) :
    psi F F.K.one + -1 * psi F g + psi F (F.K.mul g g) = 0 ∧
    psi F F.K.one * psi F F.K.one + psi F g * psi F g + psi F (F.K.mul g g) * psi F (F.K.mul g g) = 6 * PP F := by
  have hN0 : N ≠ 0 := fun e => by
    rw [e] at hN
    have := FinCyclic.h_pos F
    have h2 : 2 * 1 ≤ 2 * F.h := Nat.mul_le_mul_left 2 this
    rw [hN] at h2; exact Nat.not_succ_le_zero 1 h2
  match FinCyclic.split_pow_ff F 1 0 ⟨N, by rw [hN]; lib_nat⟩ with
  | ⟨w, hw3', hw1'⟩ =>
    have hw1 : w ≠ F.K.one := fun e => hw1' (by rw [e]; exact TwoPower.pwone F.K _)
    have hw3 : cube F w = F.K.one := by
      rw [← hw3']; show _ = F.K.mul (F.K.mul (F.K.mul F.K.one w) w) w; unfold cube; lib_ring
    have hg0 : g ≠ F.K.zero := FinDLog.g_ne_zero F g hg
    have hP : PP F = 3 * Int.ofNat N + 1 := by
      show Int.ofNat (2 * F.h + 1) = _
      rw [hN]; rfl
    have hpw : pows F g (2 * F.h) = pows F g (3 * N) := congrArg (pows F g) hN
    have M := moment_c F w hw1 hw3
    rw [sum_split_ff F Z g hg _ F.comp, hpw,
      FinCubicTrace.group3_ff F g hg0 (fun a => psi F a * psi F a) (fun a t ht => psi_sq_const F a t ht) N,
      FinCubicExact.psi_zero F] at M
    have T := twisted F
    rw [sum_split_ff F Z g hg _ F.comp, hpw,
      FinCubicTrace.group3_ff F g hg0 (fun a => chi F a * psi F a) (fun a t ht => chi_psi_const F a t ht) N,
      chi_zero, FinConic.chi_one F, FinCubicExact.chi_g F g hg, FinCubicExact.chi_gg F g hg0] at T
    constructor
    · apply FinCubicExact.cancel_N N hN0
      rw [show Int.ofNat N * 0 = 0 by lib_int, ← T]
      show _ = 0 * psi F F.K.zero + _
      lib_int
    · apply FinCubicExact.cancel_N N hN0
      have : Int.ofNat N * (6 * PP F) = 2 * PP F * (PP F + -1) := by rw [hP]; lib_int
      rw [this, ← M]
      show Int.ofNat N * _ = (0 * 0 : Int) + Int.ofNat N * _
      lib_int

theorem class_sq (g : R) (hg : Ord F.K g (2 * F.h)) (D : R) (hD : D ≠ F.K.zero) :
    psi F D * psi F D = psi F F.K.one * psi F F.K.one ∨ psi F D * psi F D = psi F g * psi F g ∨
      psi F D * psi F D = psi F (F.K.mul g g) * psi F (F.K.mul g g) := by
  have hg0 := FinDLog.g_ne_zero F g hg
  match mem_pows F g D _ (every_unit_ff F g hg D hD) with
  | ⟨k, _, hk⟩ =>
    have step : ∀ i j, k = 3 * j + i → psi F D * psi F D = psi F (Frobenius.pw F.K g i) * psi F (Frobenius.pw F.K g i) := by
      intro i j e
      rw [hk, e, FinCubicTrace.pw_split_ff F g j i]
      exact psi_sq_const F _ _ (pw_ne_zero F g hg0 j)
    cases EveryPrime3.nat_mod3 k with
    | inl h => match h with
      | ⟨j, hj⟩ => exact Or.inl ((step 0 j hj).trans (by rfl))
    | inr h =>
      cases h with
      | inl h => match h with
        | ⟨j, hj⟩ => exact Or.inr (Or.inl ((step 1 j hj).trans (by rw [show Frobenius.pw F.K g 1 = g from F.K.one_mul g])))
      | inr h => match h with
        | ⟨j, hj⟩ =>
          exact Or.inr (Or.inr ((step 2 j hj).trans (by
            rw [show Frobenius.pw F.K g 2 = F.K.mul g g by show F.K.mul (F.K.mul F.K.one g) g = _; rw [F.K.one_mul]])))

theorem norm3 (X B A0 A2 P : Int)
    (key : A0 * A0 + A0 * A2 + A2 * A2 = 3 * P) (hX : 3 * (X * X + 3 * (B * B)) = 4 * (A0 * A0 + A0 * A2 + A2 * A2)) :
    X * X + 3 * (B * B) = 4 * P := by
  apply FinCubicExact.cancel_N 3 (fun e => Nat.noConfusion e)
  show (3 : Int) * _ = 3 * _
  rw [hX, key]; lib_int

theorem norm_twist (N : Nat) (hN : 2 * F.h = 3 * N) (D : R) (hD : D ≠ F.K.zero) :
    ∃ B : Int, psi F D * psi F D + 3 * (B * B) = 4 * PP F := by
  match FinCyclic.primitive_root_ff F with
  | ⟨g, hg⟩ =>
    have hg0 := FinDLog.g_ne_zero F g hg
    have hcl := class_sq F g hg D hD
    match rels F N hN g hg, psi_mod3 F N hN F.K.one, psi_mod3 F N hN (F.K.mul g g) with
    | ⟨S1, S2⟩, ⟨T0, h0⟩, ⟨T2, h2⟩ =>
      rw [FinConic.chi_one F] at h0
      rw [FinCubicExact.chi_gg F g hg0] at h2
      have h1 : psi F g = psi F F.K.one + psi F (F.K.mul g g) := by
        calc psi F g = psi F g + (psi F F.K.one + -1 * psi F g + psi F (F.K.mul g g)) := by rw [S1]; lib_int
          _ = _ := by lib_int
      rw [h1] at S2 hcl
      generalize psi F D = X at hcl ⊢
      generalize psi F F.K.one = A0 at h0 S2 hcl
      generalize psi F (F.K.mul g g) = A2 at h2 S2 hcl
      have key : A0 * A0 + A0 * A2 + A2 * A2 = 3 * PP F := by
        apply FinPrimary.two_cancel
        rw [show 2 * (3 * PP F) = 6 * PP F by lib_int, ← S2]; lib_int
      cases hcl with
      | inl e =>
        exact ⟨1 + T0 + 2 * T2, norm3 X _ A0 A2 (PP F) key (by rw [e, h0, h2]; lib_int)⟩
      | inr e =>
        cases e with
        | inl e => exact ⟨T0 - T2, norm3 X _ A0 A2 (PP F) key (by rw [e, h0, h2]; lib_int)⟩
        | inr e => exact ⟨1 + 2 * T0 + T2, norm3 X _ A0 A2 (PP F) key (by rw [e, h0, h2]; lib_int)⟩

end FinCubicNorm

#print axioms FinCubicNorm.psi_mod3
#print axioms FinCubicNorm.rels
#print axioms FinCubicNorm.class_sq
#print axioms FinCubicNorm.norm3
#print axioms FinCubicNorm.norm_twist

lang/FinCubicSign.lean

lean_all/lang/FinCubicSign.lean

/-
  lang/FinCubicSign — T43, cycle 85: the exact sign and Hasse's bound for y² = x³ + 1 over every 𝔽_q with q ≡ 1 (mod 3).

    fin_cubic_exact   ∃ a b ∈ ℤ, a² + 3b² = q, a ≡ 1 (mod 3), −Σ_x χ(x³ + 1) = 2a
    fin_hasse3        (Σ_x χ(x³ + 1))² ≤ 4q                    (Hasse's bound for this curve over every such field)
  Port of CubicSign.cubic_exact to the interface: the Frobenius trace of y² = x³ + 1 over 𝔽_q is the trace 2a of the
  primary Eisenstein integer a + b√−3 of norm q (q = p, p², … — the Hecke character of ℚ(√−3) at every prime power).

  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.FinCubicExact
import lang.CubicSign

namespace FinCubicSign

open FTAChain YMChain FiniteRecChain FinField FinConic FinMoment FinCubic

variable {R : Type} [DecidableEq R] (F : FF R)

theorem fin_cubic_exact (N : Nat) (hN : 2 * F.h = 3 * N) :
    ∃ a b : Int, a * a + 3 * (b * b) = Int.ofNat (2 * F.h + 1) ∧ LibGcd.IDv 3 (a - 1) ∧ -psi F F.K.one = 2 * a := by
  match FinCubicExact.fin_cubic_trace F N hN, FinCubicTrace.psi_one_mod3_ff F N hN with
  | ⟨u, w, huw, hs⟩, ⟨T, hT⟩ =>
    have hs' : psi F F.K.one = 2 * Int.ofNat u ∨ psi F F.K.one = 2 * -Int.ofNat u := by
      cases hs with
      | inl e => left; rw [e]; rfl
      | inr e => right; rw [e]; show -(2 * Int.ofNat u) = 2 * -Int.ofNat u; lib_int
    -- 1 + T is even
    have hev : ∃ s, 1 + T = 2 * s := by
      match GaussInt.int_even_odd (1 + T) with
      | Or.inl h => exact h
      | Or.inr ⟨s0, h0⟩ =>
        exfalso
        have hodd : psi F F.K.one = 2 * (s0 + T) + 1 := by
          rw [hT]
          calc 1 + 3 * T = (1 + T) + 2 * T := by lib_int
            _ = 2 * (s0 + T) + 1 := by rw [h0]; lib_int
        cases hs' with
        | inl e => exact GaussInt.int_odd_ne_even (s0 + T) (Int.ofNat u) (hodd.symm.trans e)
        | inr e => exact GaussInt.int_odd_ne_even (s0 + T) (-Int.ofNat u) (hodd.symm.trans e)
    match hev with
    | ⟨s0, hs2⟩ =>
      have hTs : T = 2 * s0 + -1 := by
        calc T = (1 + T) + -1 := by lib_int
          _ = 2 * s0 + -1 := by rw [hs2]
      have hpsi : psi F F.K.one = 2 * (3 * s0 + -1) := by rw [hT, hTs]; lib_int
      have hu : 3 * s0 + -1 = Int.ofNat u ∨ 3 * s0 + -1 = -Int.ofNat u := by
        cases hs' with
        | inl e => left; exact CubicTrace.cancel_N 2 (fun e => Nat.noConfusion e) _ _ (hpsi.symm.trans e)
        | inr e => right; exact CubicTrace.cancel_N 2 (fun e => Nat.noConfusion e) _ _ (hpsi.symm.trans e)
      refine ⟨1 + -3 * s0, Int.ofNat w, ?_, ⟨-s0, by lib_int⟩, ?_⟩
      · have hsq : (1 + -3 * s0) * (1 + -3 * s0) = Int.ofNat u * Int.ofNat u := by
          cases hu with
          | inl e => rw [← e]; lib_int
          | inr e =>
            have : Int.ofNat u = -(3 * s0 + -1) := by rw [e]; lib_int
            rw [this]; lib_int
        rw [hsq]
        show Int.ofNat (u * u + 3 * (w * w)) = _
        rw [huw]
      · rw [hpsi]; lib_int


theorem sq_nn : ∀ x : Int, ∃ n : Nat, x * x = Int.ofNat n
  | Int.ofNat n => ⟨n * n, rfl⟩
  | Int.negSucc n => ⟨(n + 1) * (n + 1), rfl⟩

/-- **Hasse's bound**: a² ≤ 4q for a = −Σ_x χ(x³ + 1). -/
theorem fin_hasse3 (N : Nat) (hN : 2 * F.h = 3 * N) :
    ∃ s : Nat, psi F F.K.one * psi F F.K.one + Int.ofNat s = 4 * Int.ofNat (2 * F.h + 1) := by
  match fin_cubic_exact F N hN with
  | ⟨a, b, hab, _, hs⟩ =>
    match sq_nn b with
    | ⟨n, hn⟩ =>
      refine ⟨12 * n, ?_⟩
      have e : psi F F.K.one = -(2 * a) := by rw [← hs]; lib_int
      rw [e, ← hab, show Int.ofNat (12 * n) = 12 * Int.ofNat n from rfl, ← hn]
      lib_int

end FinCubicSign

#print axioms FinCubicSign.fin_cubic_exact
#print axioms FinCubicSign.sq_nn
#print axioms FinCubicSign.fin_hasse3

lang/FinCubicTrace.lean

lean_all/lang/FinCubicTrace.lean

/-
  lang/FinCubicTrace — T43, cycle 82: y² = x³ + 1 over any finite field 𝔽_q with q ≡ 1 (mod 3).

  Ports of CubicClass, CubicTrace and CubicSign to the interface:
    nq, chi_split, delta_cube_neg, psi_one_even     ψ(1) = 2((h − 1) − Σ nq)  (q = 2h + 1)
    cb, pw_split_ff, group3_ff                      sums over the cubic classes of 𝔽_q^×
    cube_sum_ff, psi_one_mod3_ff                    ψ(1) ≡ 1 (mod 3)
  (the trace theorem itself is assembled in the next file).

  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.FinCubic
import lang.CubicSign

namespace FinCubicTrace

open FTAChain YMChain FiniteRecChain FinField FinConic FinMoment FinCubic FinDLog OrdMul

variable {R : Type} [DecidableEq R] (F : FF R)

def nq (z : R) : Int := if chi F z = -1 then 1 else 0

theorem chi_split (z : R) : chi F z = 1 + -1 * delta Z z F.K.zero + -2 * nq F z := by
  by_cases hz : z = F.K.zero
  · rw [hz, chi_zero, FinMoment.delta_one _ _ rfl]
    unfold nq
    rw [chi_zero, ite_neg' _ _ (fun e => Int.noConfusion e)]; rfl
  · rw [FinMoment.delta_zero _ _ hz]
    unfold nq
    cases FinClass.chi_cases F z with
    | inl h0 => exact absurd h0 (FinClass.chi_ne_zero F z hz)
    | inr h =>
      cases h with
      | inl h1 => rw [h1, ite_neg' _ _ (fun e => Int.noConfusion e)]; rfl
      | inr h1 => rw [h1, ite_pos' _ _ rfl]; rfl

theorem delta_cube_neg (z : R) :
    delta Z (F.K.add (cube F z) F.K.one) F.K.zero = delta Z (cube F (F.K.neg z)) F.K.one := by
  have ec : cube F (F.K.neg z) = F.K.neg (cube F z) := by unfold cube; lib_ring
  rw [ec]
  by_cases h : F.K.add (cube F z) F.K.one = F.K.zero
  · have h' : F.K.neg (cube F z) = F.K.one := by
      have := DetChain.neg_unique F.K _ _ h
      rw [this, DetChain.neg_neg']
    rw [FinMoment.delta_one _ _ h, FinMoment.delta_one _ _ h']
  · have h' : F.K.neg (cube F z) ≠ F.K.one := fun e => h (by rw [← e]; exact F.K.add_neg _)
    rw [FinMoment.delta_zero _ _ h, FinMoment.delta_zero _ _ h']

theorem psi_one_even (w : R) (hw1 : w ≠ F.K.one) (hw3 : cube F w = F.K.one) :
    psi F F.K.one = 2 * (Int.ofNat F.h + -1 + -1 * sumK Z (fun x => nq F (F.K.add (cube F x) F.K.one)) F.els) := by
  show sumK Z (fun x => chi F (F.K.add (cube F x) F.K.one)) F.els = _
  have pt : ∀ x, chi F (F.K.add (cube F x) F.K.one) = Z.add (Z.add 1 (Z.mul (-1) (delta Z (cube F (F.K.neg x)) F.K.one)))
      (Z.mul (-2) (nq F (F.K.add (cube F x) F.K.one))) := fun x => by
    rw [chi_split F, delta_cube_neg F]; rfl
  rw [sumK_ext Z _ _ pt, sumK_add, sumK_add, ← mul_sumK, ← mul_sumK, FinMoment.sum_one F]
  have hneg : sumK Z (fun x => delta Z (cube F (F.K.neg x)) F.K.one) F.els = 3 := by
    have hr := reindex Z F.els F.comp F.K.neg F.K.neg (fun x => DetChain.neg_neg' F.K x)
      (fun x => DetChain.neg_neg' F.K x) (fun y => delta Z (cube F y) F.K.one)
    rw [hr]
    exact count_roots F w hw1 hw3
  rw [hneg]
  show Int.ofNat (2 * F.h + 1) + -1 * 3 + -2 * _ = _
  show 2 * Int.ofNat F.h + 1 + -1 * 3 + -2 * _ = _
  lib_int

def cb (t : R) : R := F.K.mul t (F.K.mul t t)

theorem pw_split_ff (g : R) (N i : Nat) :
    Frobenius.pw F.K g (3 * N + i) = F.K.mul (Frobenius.pw F.K g i) (cb F (Frobenius.pw F.K g N)) := by
  rw [show 3 * N + i = i + N * 3 by lib_nat, HasseCM.pw_add', ← TwoPower.pwpow]
  show F.K.mul (Frobenius.pw F.K g i) (F.K.mul (F.K.mul (F.K.mul F.K.one (Frobenius.pw F.K g N)) (Frobenius.pw F.K g N))
    (Frobenius.pw F.K g N)) = _
  unfold cb; lib_ring

theorem group3_ff (g : R) (hg0 : g ≠ F.K.zero) (G : R → Int) (hG : ∀ a t, t ≠ F.K.zero → G (F.K.mul a (cb F t)) = G a) :
    ∀ N, sumK LibInt.intR G (pows F g (3 * N)) = Int.ofNat N * (G F.K.one + G g + G (F.K.mul g g))
  | 0 => by show (0 : Int) = 0 * _; lib_int
  | N + 1 => by
    show G (Frobenius.pw F.K g (3 * N + 2)) + (G (Frobenius.pw F.K g (3 * N + 1)) + (G (Frobenius.pw F.K g (3 * N + 0))
      + sumK LibInt.intR G (pows F g (3 * N)))) = _
    have hgN := pw_ne_zero F g hg0 N
    rw [pw_split_ff F g N 2, pw_split_ff F g N 1, pw_split_ff F g N 0, hG _ _ hgN, hG _ _ hgN, hG _ _ hgN, group3_ff g hg0 G hG N]
    have e2 : Frobenius.pw F.K g 2 = F.K.mul g g := by show F.K.mul (F.K.mul F.K.one g) g = _; rw [F.K.one_mul]
    have e1 : Frobenius.pw F.K g 1 = g := F.K.one_mul g
    rw [e2, e1]
    show G (F.K.mul g g) + (G g + (G F.K.one + Int.ofNat N * (G F.K.one + G g + G (F.K.mul g g))))
      = (Int.ofNat N + 1) * (G F.K.one + G g + G (F.K.mul g g))
    lib_int

theorem sumR_pows_ff (g : R) (G : R → Int) : ∀ n,
    sumK LibInt.intR G (pows F g n) = CubicSign.sumR (fun k => G (Frobenius.pw F.K g k)) n
  | 0 => rfl
  | n + 1 => by
    show G (Frobenius.pw F.K g n) + sumK LibInt.intR G (pows F g n) = _
    rw [sumR_pows_ff g G n]; rfl

theorem cube_sum_ff (g : R) (hg : Ord F.K g (2 * F.h)) (N : Nat) (hN : 2 * F.h = 3 * N) (G : R → Int) :
    sumK LibInt.intR (fun x => G (cb F x)) F.els
      = G F.K.zero + 3 * CubicSign.sumR (fun k => G (cb F (Frobenius.pw F.K g k))) N := by
  rw [sum_split_ff F LibInt.intR g hg _ F.comp, sumR_pows_ff, congrArg (CubicSign.sumR _) hN, CubicSign.sumR_triple]
  · have : cb F F.K.zero = F.K.zero := by unfold cb; rw [DetChain.zero_mul']
    rw [this]; rfl
  · intro k
    show G (cb F (Frobenius.pw F.K g (k + N))) = G (cb F (Frobenius.pw F.K g k))
    have : cb F (Frobenius.pw F.K g (k + N)) = cb F (Frobenius.pw F.K g k) := by
      unfold cb
      rw [HasseCM.pw_add']
      have h3 : F.K.mul (Frobenius.pw F.K g N) (F.K.mul (Frobenius.pw F.K g N) (Frobenius.pw F.K g N)) = F.K.one := by
        have this : Frobenius.pw F.K g (3 * N) = F.K.one := (congrArg (Frobenius.pw F.K g) hN).symm.trans hg.1
        rw [show 3 * N = N * 3 by lib_nat, ← TwoPower.pwpow] at this
        rw [← this]
        show _ = F.K.mul (F.K.mul (F.K.mul F.K.one (Frobenius.pw F.K g N)) (Frobenius.pw F.K g N)) (Frobenius.pw F.K g N)
        lib_ring
      have e : F.K.mul (F.K.mul (Frobenius.pw F.K g k) (Frobenius.pw F.K g N))
          (F.K.mul (F.K.mul (Frobenius.pw F.K g k) (Frobenius.pw F.K g N)) (F.K.mul (Frobenius.pw F.K g k) (Frobenius.pw F.K g N)))
          = F.K.mul (F.K.mul (Frobenius.pw F.K g k) (F.K.mul (Frobenius.pw F.K g k) (Frobenius.pw F.K g k)))
            (F.K.mul (Frobenius.pw F.K g N) (F.K.mul (Frobenius.pw F.K g N) (Frobenius.pw F.K g N))) := by lib_ring
      rw [e, h3, F.K.mul_comm, F.K.one_mul]
    rw [this]

theorem psi_one_mod3_ff (N : Nat) (hN : 2 * F.h = 3 * N) : ∃ T : Int, psi F F.K.one = 1 + 3 * T := by
  match FinCyclic.primitive_root_ff F with
  | ⟨g, hg⟩ =>
    refine ⟨CubicSign.sumR (fun k => chi F (F.K.add (cb F (Frobenius.pw F.K g k)) F.K.one)) N, ?_⟩
    have h := cube_sum_ff F g hg N hN (fun y => chi F (F.K.add y F.K.one))
    show sumK Z (fun x => chi F (F.K.add (cube F x) F.K.one)) F.els = _
    rw [show (fun x => chi F (F.K.add (cube F x) F.K.one)) = (fun x => chi F (F.K.add (cb F x) F.K.one)) from rfl, h,
      F.K.zero_add, FinConic.chi_one F]

end FinCubicTrace

#print axioms FinCubicTrace.chi_split
#print axioms FinCubicTrace.delta_cube_neg
#print axioms FinCubicTrace.psi_one_even
#print axioms FinCubicTrace.pw_split_ff
#print axioms FinCubicTrace.group3_ff
#print axioms FinCubicTrace.sumR_pows_ff
#print axioms FinCubicTrace.cube_sum_ff
#print axioms FinCubicTrace.psi_one_mod3_ff

lang/FinCyclic.lean

lean_all/lang/FinCyclic.lean

/-
  lang/FinCyclic — T43, cycle 79: every finite field of the interface has a primitive root (𝔽_q^× is cyclic).

  For an FF of order q = 2h + 1:
    fermat_ff          x ≠ 0 ⇒ x^(q−1) = 1                       (Euler: x^h = ±1)
    exists_nonroot_ff  d + 2 ≤ q − 1 ⇒ some g ≠ 0 has g^(d+1) ≠ 1  (RootBound with the q − 1 distinct units, FinList)
    split_pow_ff       ℓ^(s+1) | q − 1 ⇒ an element y with y^(ℓ^(s+1)) = 1, y^(ℓ^s) ≠ 1
    ord_prime_pow_ff   … of exact order ℓ^(s+1)
    ord_of_dvd_ff      every divisor of q − 1 is an order
    primitive_root_ff  some g has order q − 1
  Ports of PowerSum/CycloPow/OrdMul/PrimRoot from ℤ/p to every FF instance (in particular 𝔽_{p²}).

  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.FinList
import lang.FinConic
import lang.PrimRoot

namespace FinCyclic

open FTAChain YMChain FiniteRecChain FinField OrdMul TwoPower Order PrimeOneModQ PrimePowerOne PrimRoot

variable {R : Type} [DecidableEq R] (F : FF R)

theorem h_pos : 1 ≤ F.h := by
  cases hh : F.h with
  | zero => exact absurd (by match F.nonres with | ⟨n, _, hnr⟩ => exact absurd (by rw [hh]; rfl) hnr) (fun x => x)
  | succ h' => exact Nat.succ_le_succ (Nat.zero_le _)

theorem fermat_ff (x : R) (hx : x ≠ F.K.zero) : Frobenius.pw F.K x (2 * F.h) = F.K.one := by
  rw [Nat.two_mul, HasseCM.pw_add']
  cases F.euler_pm x hx with
  | inl h => rw [h, F.K.one_mul]
  | inr h => rw [h, DetChain.neg_mul_left, F.K.one_mul, DetChain.neg_neg']

theorem find_nonroot (k : Nat) : ∀ l : List R,
    (∃ g, memL g l ∧ Frobenius.pw F.K g (k + 1) ≠ F.K.one) ∨ (∀ g, memL g l → Frobenius.pw F.K g (k + 1) = F.K.one)
  | [] => Or.inr (fun _ h => False.elim h)
  | a :: l => by
    by_cases ha : Frobenius.pw F.K a (k + 1) = F.K.one
    · cases find_nonroot k l with
      | inl h => match h with | ⟨g, hg, e⟩ => exact Or.inl ⟨g, Or.inr hg, e⟩
      | inr h => exact Or.inr (fun g hg => by cases hg with
          | inl e => rw [e]; exact ha
          | inr e => exact h g e)
    · exact Or.inl ⟨a, Or.inl rfl, ha⟩

theorem exists_nonroot_ff (k : Nat) (hk : k + 1 + 1 ≤ 2 * F.h) :
    ∃ g, g ≠ F.K.zero ∧ Frobenius.pw F.K g (k + 1) ≠ F.K.one := by
  have hnz := FinList.nz_list F
  cases find_nonroot F k (FinList.nzL F.K.zero F.els) with
  | inl h => match h with | ⟨g, hg, e⟩ => exact ⟨g, hnz.2.2 g hg, e⟩
  | inr h =>
    exfalso
    have := RootBound.root_bound F.K F.nozd (k + 1) F.K.one (EulerHard.binomL.zerosThen F.K k)
      (FinConic.one_ne_zero F) (EulerHard.len_zerosThen _ k) _ hnz.1
      (fun r hr => by
        show RootBound.evH F.K r (EulerHard.binomL F.K k) = _
        rw [EulerHard.ev_binom, h r hr, F.K.add_neg])
    rw [hnz.2.1] at this
    exact Nat.not_succ_le_self _ (Nat.le_trans hk this)

theorem f_le' (N f t : Nat) (h : N = f * (t + 2)) (hf : 1 ≤ f) : f + 1 ≤ N := by
  rw [h, show f * (t + 2) = f * t + f + f by lib_nat]
  exact Nat.add_le_add (Nat.le_add_left f (f * t)) hf

theorem split_pow_ff (t s : Nat) (hd : Dv ((t + 1 + 1) ^ (s + 1)) (2 * F.h)) :
    ∃ y, Frobenius.pw F.K y ((t + 1 + 1) ^ (s + 1)) = F.K.one ∧ Frobenius.pw F.K y ((t + 1 + 1) ^ s) ≠ F.K.one := by
  match hd, pow_succ_form (t + 1) s with
  | ⟨0, h⟩, _ =>
    exfalso
    have : 2 * 1 ≤ 2 * F.h := Nat.mul_le_mul_left 2 (h_pos F)
    rw [h, Nat.zero_mul] at this
    exact Nat.not_succ_le_zero 1 this
  | ⟨f' + 1, h⟩, ⟨n, hn⟩ =>
    have hF : 2 * F.h = ((f' + 1) * (t + 1 + 1) ^ s) * (t + 1 + 1) := by rw [h, pow_succ', nmul_assoc]
    have hF1 : (f' + 1) * (t + 1 + 1) ^ s = ((f' + 1) * n + f') + 1 := by rw [hn]; rfl
    have hFJ := f_le' (2 * F.h) ((f' + 1) * (t + 1 + 1) ^ s) t hF (by rw [hF1]; exact Nat.succ_le_succ (Nat.zero_le _))
    rw [hF1] at hFJ
    match exists_nonroot_ff F ((f' + 1) * n + f') hFJ with
    | ⟨g, hg0, hg⟩ =>
      refine ⟨Frobenius.pw F.K g (f' + 1), ?_, ?_⟩
      · rw [pwpow, ← h]; exact fermat_ff F g hg0
      · rw [pwpow, hF1]; exact hg

theorem ord_prime_pow_ff (t s : Nat) (hq : ∀ d, Dv d (t + 1 + 1) → d = 1 ∨ d = t + 1 + 1)
    (hd : Dv ((t + 1 + 1) ^ (s + 1)) (2 * F.h)) : ∃ y, Ord F.K y ((t + 1 + 1) ^ (s + 1)) := by
  match split_pow_ff F t s hd with
  | ⟨x, hxfull, hX1⟩ =>
    obtain ⟨n, hn⟩ := pow_succ_form (t + 1) (s + 1)
    rw [hn] at hxfull
    obtain ⟨d, hd1, hdmin⟩ := ord_exists F.K x n hxfull
    obtain ⟨c, hc⟩ := ord_dvd F.K x d hd1 hdmin (n + 1) hxfull
    have hdq : d + 1 = (t + 1 + 1) ^ (s + 1) := by
      cases c with
      | zero => rw [nzero_mul] at hc; exact absurd hc (Nat.noConfusion)
      | succ c =>
        cases c with
        | zero => rw [hn, hc, nmul_comm, nmul_one]
        | succ c =>
          exfalso
          apply hX1
          have hpd := proper_div t s (d + 1) c hq (by rw [hn, hc, nmul_comm])
          obtain ⟨m', hm'⟩ := hpd
          rw [hm']
          exact pw_qe F.K x (d + 1) 0 hd1 m'
    exact ⟨x, hdq ▸ ord_of_min F.K x d hd1 hdmin⟩

theorem ord_one_ff : Ord F.K F.K.one 1 := ⟨pwone F.K 1, fun r _ => LibGcd.one_dvd' r⟩

theorem ord_of_dvd_ff : ∀ N n, n ≤ N → n ∣ 2 * F.h → ∃ g, Ord F.K g n
  | _, 0, _, h => by
    match h with
    | ⟨c, e⟩ =>
      exfalso
      have := h_pos F
      rw [Nat.zero_mul] at e
      cases hh : F.h with
      | zero => rw [hh] at this; exact Nat.not_succ_le_zero 0 this
      | succ h' => rw [hh] at e; exact Nat.noConfusion (show Nat.succ (2 * h' + 1) = 0 from by rw [← e]; lib_nat)
  | _, 1, _, _ => ⟨F.K.one, ord_one_ff F⟩
  | 0, k + 2, hN, _ => absurd hN (Nat.not_succ_le_zero _)
  | N + 1, k + 2, hN, hd => by
    match lp_exists k with
    | ⟨l, hl, hmin⟩ =>
      have hlp := lp_prime _ l hl hmin
      match extract l (k + 2) (k + 2) (Nat.le_refl _) (Nat.succ_le_succ (Nat.zero_le _)) with
      | ⟨0, n', he, hn'⟩ =>
        exfalso; apply hn'
        rw [Nat.pow_zero, Nat.one_mul] at he
        rw [← he]; exact dvd_of_dv hl
      | ⟨s + 1, n', he, hn'⟩ =>
        have hn1 : 1 ≤ n' := by
          cases n' with
          | zero => rw [Nat.mul_zero] at he; exact absurd he (fun e => Nat.noConfusion e)
          | succ _ => exact Nat.succ_le_succ (Nat.zero_le _)
        have hP2 : 2 ≤ (l + 1 + 1) ^ (s + 1) := by
          rw [Nat.pow_succ]
          have hp1 : 1 ≤ (l + 1 + 1) ^ s := match pow_succ_form (l + 1) s with
            | ⟨r, hr⟩ => by rw [hr]; exact Nat.succ_le_succ (Nat.zero_le r)
          have := Nat.mul_le_mul hp1 (Nat.succ_le_succ (Nat.succ_le_succ (Nat.zero_le l)))
          rw [Nat.one_mul] at this
          exact this
        have hlt : n' < k + 2 := by
          rw [he]
          have a : 2 * n' ≤ (l + 1 + 1) ^ (s + 1) * n' := Nat.mul_le_mul_right n' hP2
          have b : n' < 2 * n' := by rw [Nat.two_mul]; exact Nat.lt_add_of_pos_right hn1
          exact Nat.lt_of_lt_of_le b a
        have hPd : (l + 1 + 1) ^ (s + 1) ∣ 2 * F.h := LibGcd.dvd_trans' ⟨n', he⟩ hd
        have hnd : n' ∣ 2 * F.h := LibGcd.dvd_trans' ⟨(l + 1 + 1) ^ (s + 1), by rw [he, Nat.mul_comm]⟩ hd
        match ord_prime_pow_ff F l s hlp (dv_of_dvd hPd),
          ord_of_dvd_ff N n' (Nat.le_of_lt_succ (Nat.lt_of_lt_of_le hlt hN)) hnd with
        | ⟨y, hy⟩, ⟨z, hz⟩ =>
          refine ⟨F.K.mul y z, ?_⟩
          rw [he]
          exact ord_mul_coprime F.K y z _ _ hy hz (coprime_pow l s n' hlp hn1 hn')

/-- **𝔽_q^× is cyclic** for every FF. -/
theorem primitive_root_ff : ∃ g, Ord F.K g (2 * F.h) :=
  ord_of_dvd_ff F (2 * F.h) (2 * F.h) (Nat.le_refl _) (LibGcd.dvd_refl' _)

end FinCyclic

#print axioms FinCyclic.h_pos
#print axioms FinCyclic.fermat_ff
#print axioms FinCyclic.find_nonroot
#print axioms FinCyclic.exists_nonroot_ff
#print axioms FinCyclic.f_le'
#print axioms FinCyclic.split_pow_ff
#print axioms FinCyclic.ord_prime_pow_ff
#print axioms FinCyclic.ord_one_ff
#print axioms FinCyclic.ord_of_dvd_ff
#print axioms FinCyclic.primitive_root_ff

lang/FinCyclo.lean

lean_all/lang/FinCyclo.lean

/-
  lang/FinCyclo — T43, cycle 93: roots of unity in every finite field, and residue degree 2 in ℚ(ζ_N).

    cyclo_ff     for every FF of order q:  𝔽_q has an element of order N + 1  ⇔  N + 1 | q − 1
    cyclo_p2     for every odd prime p:    𝔽_{p²} has an element of order N + 1  ⇔  N + 1 | p² − 1
  So a prime p ∤ N has residue degree f ≤ 2 in ℚ(ζ_N) (𝔽_{p²} ⊇ ℤ[ζ_N]/𝔭) exactly when p² ≡ 1 (mod N): the Frobenius of p
  in (ℤ/N)^× has order dividing 2 — GL1 reciprocity for ℚ(ζ_N) at the level of residue degrees (PrimRoot.cyclo_N is f = 1).

  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.FinCyclic
import lang.Fq2Inst

namespace FinCyclo

open FTAChain ZMod YMChain FinField OrdMul

theorem cyclo_ff {R : Type} [DecidableEq R] (F : FF R) (N : Nat) :
    (∃ y, Ord F.K y (N + 1)) ↔ (N + 1) ∣ 2 * F.h :=
  ⟨fun ⟨y, hy⟩ => by
    have hy0 : y ≠ F.K.zero := fun e => by
      have h := hy.1
      rw [e] at h
      exact FinConic.one_ne_zero F (h.symm.trans (HasseCM.pw_zero_succ F.K N))
    exact hy.2 _ (FinCyclic.fermat_ff F y hy0),
   fun hd => FinCyclic.ord_of_dvd_ff F (N + 1) (N + 1) (Nat.le_refl _) hd⟩

theorem two_h (k : Nat) : 2 * ((2 * k + 1 + 1 + 1 + 1) * (k + 1)) = (2 * k + 1 + 1 + 1) * (2 * k + 1 + 1 + 1) - 1 := by
  rw [show (2 * k + 1 + 1 + 1) * (2 * k + 1 + 1 + 1) = 2 * ((2 * k + 1 + 1 + 1 + 1) * (k + 1)) + 1 by lib_nat]
  rfl

/-- **Residue degree ≤ 2 in ℚ(ζ_{N+1})**: 𝔽_{p²} contains an element of order N + 1 iff N + 1 | p² − 1. -/
theorem cyclo_p2 (k : Nat) (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = 2 * k + 1 + 1 + 1) (n : Zp (2 * k + 1 + 1))
    (hn : Frobenius.pw (ring (2 * k + 1 + 1)) n (k + 1) = (ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) (N : Nat) :
    (∃ y, Ord (Fq2.G2 (2 * k + 1) n) y (N + 1)) ↔ (N + 1) ∣ (2 * k + 1 + 1 + 1) * (2 * k + 1 + 1 + 1) - 1 := by
  rw [← two_h k]
  exact cyclo_ff (Fq2Inst.ffq k hpr n hn) N

end FinCyclo

#print axioms FinCyclo.cyclo_ff
#print axioms FinCyclo.two_h
#print axioms FinCyclo.cyclo_p2

lang/FinDLog.lean

lean_all/lang/FinDLog.lean

/-
  lang/FinDLog — T43, cycle 80: discrete logarithms in any finite field of the interface.

  g a primitive root (FinCyclic), q − 1 = 2h.
    sumN, sumN_cnt     Σ_{z ∈ 𝔽_q} cnt z l = |l|            (each element of a list is counted once)
    len_le             a distinct list of nonzero elements has at most q − 1 entries
    pows, pows_dist    g⁰, …, g^(q−2) are distinct
    every_unit_ff      every unit is a power of g
    sum_split_ff       Σ_a F(a) = F(0) + Σ_k F(g^k)
  Port of lang/DLog to FinField.FF (used for the cubic chain over 𝔽_{p²}).

  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.FinCyclic
import lang.FinMoment

namespace FinDLog

open FTAChain YMChain FiniteRecChain FinField OrdMul FinList

variable {R : Type} [DecidableEq R] (F : FF R)

def sumN {A : Type} (f : A → Nat) : List A → Nat
  | [] => 0
  | a :: l => f a + sumN f l

theorem sumN_add {A : Type} (f g : A → Nat) : ∀ l : List A, sumN (fun a => f a + g a) l = sumN f l + sumN g l
  | [] => rfl
  | a :: l => by show f a + g a + sumN _ l = (f a + sumN f l) + (g a + sumN g l); rw [sumN_add f g l]; lib_nat

theorem sumN_le {A : Type} (f g : A → Nat) (h : ∀ a, f a ≤ g a) : ∀ l : List A, sumN f l ≤ sumN g l
  | [] => Nat.le_refl 0
  | a :: l => Nat.add_le_add (h a) (sumN_le f g h l)

theorem cnt_eq_sumN (z : R) : ∀ l : List R, cnt z l = sumN (fun y => if y = z then 1 else 0) l
  | [] => rfl
  | a :: l => by show _ + cnt z l = _ + sumN _ l; rw [cnt_eq_sumN z l]

theorem cnt_els (a : R) : sumN (fun z => if a = z then 1 else 0) F.els = 1 := by
  have h1 : cnt a F.els = 1 := Int.ofNat.inj ((cnt_int a F.els).symm.trans (F.comp a))
  rw [cnt_eq_sumN] at h1
  have : ∀ l : List R, sumN (fun z => if a = z then 1 else 0) l = sumN (fun y => if y = a then 1 else 0) l := by
    intro l; induction l with
    | nil => rfl
    | cons b l ih =>
      show (if a = b then 1 else 0) + sumN (fun z => if a = z then 1 else 0) l
        = (if b = a then 1 else 0) + sumN (fun y => if y = a then 1 else 0) l
      rw [ih]
      by_cases e : a = b
      · rw [ite_pos' _ _ e, ite_pos' _ _ e.symm]
      · rw [ite_neg' _ _ e, ite_neg' _ _ (fun e' => e e'.symm)]
  exact (this _).trans h1

theorem sumN_cnt : ∀ l : List R, sumN (fun z => cnt z l) F.els = l.length
  | [] => by
    show sumN (fun _ => 0) F.els = 0
    have : ∀ l : List R, sumN (fun _ => (0 : Nat)) l = 0 := by
      intro l; induction l with
      | nil => rfl
      | cons _ l ih => show 0 + _ = 0; rw [ih]
    exact this _
  | a :: l => by
    show sumN (fun z => (if a = z then 1 else 0) + cnt z l) F.els = l.length + 1
    rw [sumN_add, cnt_els F a, sumN_cnt l]; lib_nat

theorem cnt_le_one_of_dist (z : R) : ∀ l : List R, RootBound.Dist l → cnt z l ≤ 1
  | [], _ => Nat.zero_le 1
  | a :: l, hd => by
    show (if a = z then 1 else 0) + cnt z l ≤ 1
    by_cases e : a = z
    · rw [ite_pos' _ _ e]
      have : cnt z l = 0 := by
        cases hc : cnt z l with
        | zero => rfl
        | succ c =>
          exfalso
          have hm : memL z l := by
            have : ∀ l' : List R, 1 ≤ cnt z l' → memL z l' := by
              intro l'; induction l' with
              | nil => intro h; exact absurd h (Nat.not_succ_le_zero 0)
              | cons b l' ih =>
                intro h
                by_cases eb : b = z
                · exact Or.inl eb.symm
                · right; apply ih
                  have : cnt z (b :: l') = cnt z l' := by show (if b = z then 1 else 0) + cnt z l' = _; rw [ite_neg' _ _ eb, Nat.zero_add]
                  rw [← this]; exact h
            exact this l (by rw [hc]; exact Nat.succ_le_succ (Nat.zero_le c))
          exact hd.1 z hm e.symm
      rw [this]; exact Nat.le_refl _
    · rw [ite_neg' _ _ e, Nat.zero_add]; exact cnt_le_one_of_dist z l hd.2

/-- **A distinct list of nonzero elements has at most q − 1 entries.** -/
theorem len_le (l : List R) (hd : RootBound.Dist l) (hz : ∀ s, memL s l → s ≠ F.K.zero) : l.length ≤ 2 * F.h := by
  rw [← sumN_cnt F l]
  have h0 : cnt F.K.zero l = 0 := by
    have : ∀ l' : List R, (∀ s, memL s l' → s ≠ F.K.zero) → cnt F.K.zero l' = 0 := by
      intro l'; induction l' with
      | nil => intro _; rfl
      | cons b l' ih =>
        intro h
        show (if b = F.K.zero then 1 else 0) + cnt F.K.zero l' = 0
        rw [ite_neg' _ _ (h b (Or.inl rfl)), ih (fun s hs => h s (Or.inr hs))]
    exact this l hz
  have hb : ∀ z, cnt z l ≤ (if z = F.K.zero then 0 else 1) := fun z => by
    by_cases e : z = F.K.zero
    · rw [ite_pos' _ _ e, e, h0]; exact Nat.le_refl 0
    · rw [ite_neg' _ _ e]; exact cnt_le_one_of_dist z l hd
  have hs := sumN_le _ _ hb F.els
  have hcount : sumN (fun z => if z = F.K.zero then 0 else 1) F.els + cnt F.K.zero F.els = F.els.length := by
    have : ∀ l' : List R, sumN (fun z => if z = F.K.zero then 0 else 1) l' + cnt F.K.zero l' = l'.length := by
      intro l'; induction l' with
      | nil => rfl
      | cons b l' ih =>
        show ((if b = F.K.zero then 0 else 1) + sumN (fun z => if z = F.K.zero then 0 else 1) l') + ((if b = F.K.zero then 1 else 0) + cnt F.K.zero l') = l'.length + 1
        by_cases e : b = F.K.zero
        · rw [ite_pos' _ _ e, ite_pos' _ _ e, ← ih]; lib_nat
        · rw [ite_neg' _ _ e, ite_neg' _ _ e, ← ih]; lib_nat
    exact this _
  have h1 : cnt F.K.zero F.els = 1 := Int.ofNat.inj ((cnt_int F.K.zero F.els).symm.trans (F.comp F.K.zero))
  rw [h1, len_els F] at hcount
  have : sumN (fun z => if z = F.K.zero then 0 else 1) F.els = 2 * F.h := Nat.succ.inj hcount
  rw [this] at hs
  exact hs

def pows (g : R) : Nat → List R
  | 0 => []
  | n + 1 => Frobenius.pw F.K g n :: pows g n

theorem pw_ne_zero (g : R) (hg : g ≠ F.K.zero) : ∀ k, Frobenius.pw F.K g k ≠ F.K.zero
  | 0 => FinConic.one_ne_zero F
  | k + 1 => fun e => by
    cases F.nozd _ _ e with
    | inl h => exact pw_ne_zero g hg k h
    | inr h => exact hg h

theorem mem_pows (g s : R) : ∀ n, memL s (pows F g n) → ∃ k, k < n ∧ s = Frobenius.pw F.K g k
  | 0, h => False.elim h
  | n + 1, h => by
    cases h with
    | inl e => exact ⟨n, Nat.lt_succ_self n, e⟩
    | inr e => match mem_pows g s n e with | ⟨k, hk, hs⟩ => exact ⟨k, Nat.lt_succ_of_lt hk, hs⟩

theorem g_ne_zero (g : R) (hg : Ord F.K g (2 * F.h)) : g ≠ F.K.zero := fun h => by
  have := hg.1
  have hh := FinCyclic.h_pos F
  cases hq : F.h with
  | zero => rw [hq] at hh; exact Nat.not_succ_le_zero 0 hh
  | succ h' =>
    rw [h, hq, show 2 * (h' + 1) = (2 * h' + 1) + 1 by lib_nat, HasseCM.pw_zero_succ] at this
    exact FinConic.one_ne_zero F this.symm

theorem pw_inj (g : R) (hg : Ord F.K g (2 * F.h)) (k l : Nat) (hkl : k < l) (hl : l < 2 * F.h) :
    Frobenius.pw F.K g k ≠ Frobenius.pw F.K g l := fun e => by
  have hg0 := g_ne_zero F g hg
  match Nat.le.dest hkl with
  | ⟨d, hd⟩ =>
    have hl' : l = k + (d + 1) := by rw [← hd]; exact (Nat.succ_add k d)
    rw [hl', HasseCM.pw_add'] at e
    have h0 : F.K.mul (Frobenius.pw F.K g k) (F.K.add (Frobenius.pw F.K g (d + 1)) (F.K.neg F.K.one)) = F.K.zero := by
      rw [F.K.distrib, ← e, mul_neg_r F.K, F.K.mul_comm _ F.K.one, F.K.one_mul, F.K.add_neg]
    cases F.nozd _ _ h0 with
    | inl h => exact pw_ne_zero F g hg0 k h
    | inr h =>
      match hg.2 (d + 1) (FinMoment.eq_of_sub_zero F _ _ h) with
      | ⟨c, hc⟩ =>
        cases c with
        | zero => rw [Nat.mul_zero] at hc; exact Nat.noConfusion hc
        | succ c =>
          have : 2 * F.h ≤ d + 1 := by rw [hc, Nat.mul_succ]; exact Nat.le_add_left _ _
          have hdl : d + 1 ≤ l := by rw [hl']; exact Nat.le_add_left _ _
          exact Nat.lt_irrefl l (Nat.lt_of_lt_of_le hl (Nat.le_trans this hdl))

theorem pows_dist (g : R) (hg : Ord F.K g (2 * F.h)) : ∀ n, n ≤ 2 * F.h → RootBound.Dist (pows F g n)
  | 0, _ => trivial
  | n + 1, hn => ⟨fun s hs => by
      match mem_pows F g s n hs with
      | ⟨k, hk, e⟩ => rw [e]; exact pw_inj F g hg k n hk (Nat.lt_of_succ_le hn),
    pows_dist g hg n (Nat.le_of_succ_le hn)⟩

theorem len_pows (g : R) : ∀ n, (pows F g n).length = n
  | 0 => rfl
  | n + 1 => congrArg Nat.succ (len_pows g n)

theorem find_mem (a : R) : ∀ l : List R, memL a l ∨ ¬ memL a l
  | [] => Or.inr (fun h => h)
  | b :: l => by
    by_cases h : a = b
    · exact Or.inl (Or.inl h)
    · cases find_mem a l with
      | inl h' => exact Or.inl (Or.inr h')
      | inr h' => exact Or.inr (fun hh => by cases hh with
        | inl e => exact h e
        | inr e => exact h' e)

/-- **Every unit is a power of the primitive root.** -/
theorem every_unit_ff (g : R) (hg : Ord F.K g (2 * F.h)) (a : R) (ha : a ≠ F.K.zero) : memL a (pows F g (2 * F.h)) := by
  have hg0 := g_ne_zero F g hg
  cases find_mem a (pows F g (2 * F.h)) with
  | inl h => exact h
  | inr h =>
    exfalso
    have hd : RootBound.Dist (a :: pows F g (2 * F.h)) := ⟨fun s hs e => h (e ▸ hs), pows_dist F g hg _ (Nat.le_refl _)⟩
    have hz : ∀ s, memL s (a :: pows F g (2 * F.h)) → s ≠ F.K.zero := fun s hs => by
      cases hs with
      | inl e => rw [e]; exact ha
      | inr e => match mem_pows F g s _ e with | ⟨k, _, e'⟩ => rw [e']; exact pw_ne_zero F g hg0 k
    have := len_le F _ hd hz
    rw [show (a :: pows F g (2 * F.h)).length = 2 * F.h + 1 from congrArg Nat.succ (len_pows F g _)] at this
    exact Nat.not_succ_le_self _ this

section Sums
variable {S : Type} (Rg : DetChain.CRing S)

theorem count_out (a : R) : ∀ l : List R, ¬ memL a l → sumK Rg (fun s => delta Rg a s) l = Rg.zero
  | [], _ => rfl
  | b :: l, h => by
    show Rg.add (delta Rg a b) (sumK Rg (fun s => delta Rg a s) l) = Rg.zero
    rw [show delta Rg a b = Rg.zero from ite_neg' _ _ (fun e => h (Or.inl e)), count_out a l (fun h' => h (Or.inr h')),
      Rg.zero_add]

theorem count_in (a : R) : ∀ l : List R, RootBound.Dist l → memL a l → sumK Rg (fun s => delta Rg a s) l = Rg.one
  | [], _, h => False.elim h
  | b :: l, hd, h => by
    show Rg.add (delta Rg a b) (sumK Rg (fun s => delta Rg a s) l) = Rg.one
    by_cases e : a = b
    · rw [show delta Rg a b = Rg.one from ite_pos' _ _ e, count_out Rg a l (fun h' => hd.1 a h' e), DetChain.add_zero']
    · cases h with
      | inl e' => exact absurd e' e
      | inr h' => rw [show delta Rg a b = Rg.zero from ite_neg' _ _ e, count_in a l hd.2 h', Rg.zero_add]

/-- **Σ_a F(a) = F(0) + Σ_k F(g^k).** -/
theorem sum_split_ff (g : R) (hg : Ord F.K g (2 * F.h)) (Fn : R → S) (hc : Complete Rg F.els) :
    sumK Rg Fn F.els = Rg.add (Fn F.K.zero) (sumK Rg Fn (pows F g (2 * F.h))) := by
  have hg0 := g_ne_zero F g hg
  have hd := pows_dist F g hg (2 * F.h) (Nat.le_refl _)
  have pt : ∀ a, Fn a = Rg.add (Rg.mul (delta Rg a F.K.zero) (Fn a))
      (sumK Rg (fun s => Rg.mul (delta Rg a s) (Fn a)) (pows F g (2 * F.h))) := fun a => by
    rw [← sumK_mul, ← DetChain.right_distrib]
    by_cases ha : a = F.K.zero
    · rw [show delta Rg a F.K.zero = Rg.one from ite_pos' _ _ ha,
        count_out Rg a _ (fun h => by
          match mem_pows F g a _ h with
          | ⟨k, _, e⟩ => exact pw_ne_zero F g hg0 k (e.symm.trans ha)),
        DetChain.add_zero', Rg.one_mul]
    · rw [show delta Rg a F.K.zero = Rg.zero from ite_neg' _ _ ha, count_in Rg a _ hd (every_unit_ff F g hg a ha),
        Rg.zero_add, Rg.one_mul]
  rw [sumK_ext Rg _ _ pt, sumK_add, pick Rg _ hc Fn F.K.zero, sumK_swap]
  congr 1
  exact sumK_ext Rg _ _ (fun s => pick Rg _ hc Fn s) _

end Sums

end FinDLog

#print axioms FinDLog.sumN_add
#print axioms FinDLog.sumN_le
#print axioms FinDLog.cnt_eq_sumN
#print axioms FinDLog.cnt_els
#print axioms FinDLog.sumN_cnt
#print axioms FinDLog.cnt_le_one_of_dist
#print axioms FinDLog.len_le
#print axioms FinDLog.pw_ne_zero
#print axioms FinDLog.mem_pows
#print axioms FinDLog.g_ne_zero
#print axioms FinDLog.pw_inj
#print axioms FinDLog.pows_dist
#print axioms FinDLog.len_pows
#print axioms FinDLog.find_mem
#print axioms FinDLog.every_unit_ff
#print axioms FinDLog.count_out
#print axioms FinDLog.count_in
#print axioms FinDLog.sum_split_ff

lang/FinField.lean

lean_all/lang/FinField.lean

/-
  lang/FinField — T43, cycle 65: an interface for finite fields of odd order and their integer-valued quadratic character.

  FF R bundles a commutative ring K on R with a complete list of its q = 2h + 1 elements and the facts used by character
  sums: no zero divisors, inverses, Euler's criterion (x^h = ±1, squares give 1, x^h = 1 ⇒ square), a non-residue,
  −1 ≠ 1. 𝔽_p (lang/ZMod, Euler, EulerHard) and 𝔽_{p²} (lang/QuadFrob) are instances (next cycles).
    chi                 χ(x) ∈ {0, ±1}
    chi_mul, chi_sq     χ(xy) = χ(x)χ(y), χ(y²) = 1 (y ≠ 0)
    chi_sum             Σ_x χ(x) = 0
  The aim: the Jacobsthal chain over any finite field, then the base change of the CM form of y² = x³ − x to ℚ(i) at the
  inert primes (point counts over 𝔽_{p²}) — GL2 over the CM field ℚ(i).

  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.QChar
import lang.JacobMoment
import lang.EulerHard
import lib.Semi

namespace FinField

open YMChain FiniteRecChain

structure FF (R : Type) [DecidableEq R] where
  K : DetChain.CRing R
  els : List R
  comp : Complete LibInt.intR els
  h : Nat
  card : sumK LibInt.intR (fun _ => (1 : Int)) els = Int.ofNat (2 * h + 1)
  nozd : DetChain.NoZD K
  inv : ∀ x, x ≠ K.zero → ∃ y, K.mul x y = K.one
  euler_pm : ∀ x, x ≠ K.zero → Frobenius.pw K x h = K.one ∨ Frobenius.pw K x h = K.neg K.one
  euler_sq : ∀ y, y ≠ K.zero → Frobenius.pw K (K.mul y y) h = K.one
  euler_hard : ∀ x, Frobenius.pw K x h = K.one → ∃ y, K.mul y y = x
  ne_pm : K.neg K.one ≠ K.one
  nonres : ∃ n, n ≠ K.zero ∧ Frobenius.pw K n h ≠ K.one
  iv : R → R
  iv_mul : ∀ x, x ≠ K.zero → K.mul (iv x) x = K.one
  iv_zero : iv K.zero = K.zero
  iv_hom : ∀ x y, iv (K.mul x y) = K.mul (iv x) (iv y)

variable {R : Type} [DecidableEq R] (F : FF R)

def chi (x : R) : Int := if x = F.K.zero then 0 else if Frobenius.pw F.K x F.h = F.K.one then 1 else -1

theorem chi_zero : chi F F.K.zero = 0 := ite_pos' _ _ rfl

theorem chi_one_of (x : R) (hx : x ≠ F.K.zero) (h : Frobenius.pw F.K x F.h = F.K.one) : chi F x = 1 := by
  unfold chi; rw [ite_neg' _ _ hx, ite_pos' _ _ h]

theorem chi_neg_of (x : R) (hx : x ≠ F.K.zero) (h : Frobenius.pw F.K x F.h ≠ F.K.one) : chi F x = -1 := by
  unfold chi; rw [ite_neg' _ _ hx, ite_neg' _ _ h]

theorem chi_mul (x y : R) : chi F (F.K.mul x y) = chi F x * chi F y := by
  by_cases hx : x = F.K.zero
  · rw [hx, DetChain.zero_mul', chi_zero]; lib_int
  by_cases hy : y = F.K.zero
  · rw [hy, DetChain.mul_zero, chi_zero]; lib_int
  have hxy : F.K.mul x y ≠ F.K.zero := fun e => by
    cases F.nozd _ _ e with
    | inl h => exact hx h
    | inr h => exact hy h
  have hp := Frobenius.pw_mul F.K x y F.h
  cases F.euler_pm x hx with
  | inl ex =>
    cases F.euler_pm y hy with
    | inl ey => rw [chi_one_of F x hx ex, chi_one_of F y hy ey, chi_one_of F _ hxy (by rw [hp, ex, ey, F.K.one_mul])]; rfl
    | inr ey =>
      have ey' : Frobenius.pw F.K y F.h ≠ F.K.one := fun e => F.ne_pm (ey.symm.trans e)
      rw [chi_one_of F x hx ex, chi_neg_of F y hy ey', chi_neg_of F _ hxy (by rw [hp, ex, ey, F.K.one_mul]; exact F.ne_pm)]; rfl
  | inr ex =>
    have ex' : Frobenius.pw F.K x F.h ≠ F.K.one := fun e => F.ne_pm (ex.symm.trans e)
    cases F.euler_pm y hy with
    | inl ey =>
      rw [chi_neg_of F x hx ex', chi_one_of F y hy ey,
        chi_neg_of F _ hxy (by rw [hp, ex, ey, F.K.mul_comm, F.K.one_mul]; exact F.ne_pm)]; rfl
    | inr ey =>
      have ey' : Frobenius.pw F.K y F.h ≠ F.K.one := fun e => F.ne_pm (ey.symm.trans e)
      have nn : F.K.mul (F.K.neg F.K.one) (F.K.neg F.K.one) = F.K.one := by
        rw [DetChain.neg_mul_left, F.K.one_mul, DetChain.neg_neg']
      rw [chi_neg_of F x hx ex', chi_neg_of F y hy ey', chi_one_of F _ hxy (by rw [hp, ex, ey]; exact nn)]; rfl

theorem chi_sq (y : R) (hy : y ≠ F.K.zero) : chi F (F.K.mul y y) = 1 := by
  have hyy : F.K.mul y y ≠ F.K.zero := fun e => by
    cases F.nozd _ _ e with
    | inl h => exact hy h
    | inr h => exact hy h
  exact chi_one_of F _ hyy (F.euler_sq y hy)

theorem chi_sum : sumK LibInt.intR (chi F) F.els = 0 := by
  match F.nonres with
  | ⟨n, hn0, hn⟩ =>
    match F.inv n hn0 with
    | ⟨ni, hni⟩ =>
      have hre := reindex LibInt.intR F.els F.comp (F.K.mul n) (F.K.mul ni)
        (fun x => by rw [← F.K.mul_assoc, F.K.mul_comm ni, hni, F.K.one_mul])
        (fun y => by rw [← F.K.mul_assoc, hni, F.K.one_mul]) (chi F)
      have hc : chi F n = -1 := chi_neg_of F n hn0 hn
      have e : sumK LibInt.intR (fun x => chi F (F.K.mul n x)) F.els
          = LibInt.intR.mul (-1) (sumK LibInt.intR (chi F) F.els) := by
        rw [mul_sumK]
        exact sumK_ext LibInt.intR _ _ (fun x => by rw [chi_mul F, hc]; rfl) _
      rw [e] at hre
      apply QChar.int_two_zero
      have : LibInt.intR.mul (-1) (sumK LibInt.intR (chi F) F.els) + sumK LibInt.intR (chi F) F.els = 0 := by
        show -1 * sumK LibInt.intR (chi F) F.els + sumK LibInt.intR (chi F) F.els = 0
        lib_int
      rw [hre] at this
      exact this

/-- **𝔽_p is an instance** (p = 2m + 3). -/
def ffp (m : Nat) (hpr : ∀ d, FTAChain.Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) :
    FF (ZMod.Zp (2 * m + 1 + 1)) where
  K := (ZMod.ring (2 * m + 1 + 1))
  els := ConicSum.L m
  comp := JacobMoment.compL m
  h := m + 1
  card := by
    rw [show 2 * (m + 1) + 1 = 2 * m + 1 + 1 + 1 by lib_nat]
    exact JacobMoment.sum_one m
  nozd := ZMod.nozd (2 * m + 1) hpr
  inv := fun x hx => ⟨Frobenius.pw (ZMod.ring (2 * m + 1 + 1)) x (2 * m + 1), by rw [(ZMod.ring (2 * m + 1 + 1)).mul_comm]; exact ZMod.inv_exists (2 * m + 1) hpr x hx⟩
  euler_pm := fun x hx => Euler.euler_pm m hpr x hx
  euler_sq := fun y hy => Euler.euler_square m hpr y hy
  euler_hard := fun x hx => EulerHard.euler_hard m hpr x hx
  ne_pm := QChar.ne_pm m
  nonres := match PowerSum.exists_nonroot (2 * m + 1) hpr m (by rw [Nat.two_mul]; exact Nat.succ_le_succ (Nat.le_add_left m m)) with
    | ⟨n, hn0, hn⟩ => ⟨n, hn0, hn⟩
  iv := ConicSum.iv m
  iv_mul := fun x hx => ConicSum.iv_mul_self m hpr x hx
  iv_zero := ConicSum.iv_zero m
  iv_hom := fun x y => ConicSum.inv_mul m x y

end FinField

#print axioms FinField.ffp
#print axioms FinField.chi_zero
#print axioms FinField.chi_one_of
#print axioms FinField.chi_neg_of
#print axioms FinField.chi_mul
#print axioms FinField.chi_sq
#print axioms FinField.chi_sum

lang/FinHasse.lean

lean_all/lang/FinHasse.lean

/-
  lang/FinHasse — T43, cycle 97: the Hasse invariant of y² = x³ − x over every finite field with q ≡ 1 (mod 4).

  For an FF with h = (q − 1)/2 = 2k + 2:
    psF, ps_low, ps_top, ps_high   Σ_x x^e = 0 (1 ≤ e ≤ q − 2), −1 (e = q − 1), 0 (q ≤ e ≤ 2q − 3)    (FinPowerSum)
    TF, T_cons, T_zero, T_skip, skip_many   one binomial coefficient at a time (port of HasseCM)
    fin_hasse      Σ_x (x³ − x)^h = −C(h, h/2)·(−1)^(h/2)          in the field
  So the Frobenius trace over 𝔽_q is ≡ ±C((q−1)/2, (q−1)/4) (mod p) for every q ≡ 1 (mod 4) — for 𝔽_{p²} this is the step
  towards a(𝔽_{p²}) ≢ 0 (mod p) at split primes (ordinarity), with Lucas' theorem next.

  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.FinPowerSum
import lang.HasseCM

namespace FinHasse

open FTAChain YMChain Frobenius FinField HasseCM

variable {R : Type} [DecidableEq R] (F : FF R)

def psF (e : Nat) : R := sumK F.K (fun x => pw F.K x e) F.els

theorem two_h (k : Nat) (hk : F.h = 2 * k + 2) : 2 * F.h = 2 * (2 * k + 1) + 1 + 1 := by rw [hk]; lib_nat

theorem ps_low (k : Nat) (hk : F.h = 2 * k + 2) (e : Nat) (h1 : 1 ≤ e) (h2 : e ≤ 2 * (2 * k + 1) + 1) : psF F e = F.K.zero := by
  cases e with
  | zero => exact absurd h1 (Nat.not_succ_le_zero 0)
  | succ e' => exact FinPowerSum.ps_zero_ff F e' (by rw [two_h F k hk]; exact Nat.succ_le_succ h2)

theorem ps_top (k : Nat) (hk : F.h = 2 * k + 2) : psF F (2 * (2 * k + 1) + 1 + 1) = F.K.neg F.K.one := by
  rw [← two_h F k hk]; exact FinPowerSum.ps_top_ff F

theorem ps_high (k : Nat) (hk : F.h = 2 * k + 2) (r : Nat) (h1 : 1 ≤ r) (h2 : r ≤ 2 * (2 * k + 1) + 1) :
    psF F (2 * (2 * k + 1) + 1 + 1 + r) = F.K.zero := by
  have e : psF F (2 * (2 * k + 1) + 1 + 1 + r) = psF F r := sumK_ext F.K _ _ (fun x => by
    show pw F.K x (2 * (2 * k + 1) + 1 + 1 + r) = pw F.K x r
    by_cases hx : x = F.K.zero
    · rw [hx]
      cases r with
      | zero => exact absurd h1 (Nat.not_succ_le_zero 0)
      | succ r' => rw [pw_zero_succ, ← Nat.add_assoc, pw_zero_succ]
    · rw [pw_add' F.K x _ r, ← two_h F k hk, FinCyclic.fermat_ff F x hx, F.K.one_mul]) _
  rw [e]
  exact ps_low F k hk r h1 h2

theorem sumK_zero' {A : Type} : ∀ l : List A, sumK F.K (fun _ => F.K.zero) l = F.K.zero
  | [] => rfl
  | _ :: l => by
    show F.K.add F.K.zero (sumK F.K (fun _ => F.K.zero) l) = F.K.zero
    rw [sumK_zero' l, F.K.zero_add]

def evsF (x : R) (cs : List Nat) : R := ev F.K (F.K.mul x x) (F.K.neg F.K.one) cs

def TF (e : Nat) (cs : List Nat) : R :=
  sumK F.K (fun x => F.K.mul (pw F.K x e) (evsF F x cs)) F.els

theorem T_cons (e c : Nat) (cs : List Nat) :
    TF F e (c :: cs) = F.K.add (F.K.mul (nsm F.K c (pw F.K (F.K.neg F.K.one) cs.length)) (psF F e)) (TF F (e + 2) cs) := by
  have pt : ∀ x : R, F.K.mul (pw F.K x e) (evsF F x (c :: cs))
      = F.K.add (F.K.mul (nsm F.K c (pw F.K (F.K.neg F.K.one) cs.length)) (pw F.K x e)) (F.K.mul (pw F.K x (e + 2)) (evsF F x cs)) :=
    fun x => by
      show F.K.mul (pw F.K x e) (F.K.add (nsm F.K c (pw F.K (F.K.neg F.K.one) cs.length)) (F.K.mul (F.K.mul x x) (evsF F x cs))) = _
      rw [F.K.distrib, F.K.mul_comm (pw F.K x e) (nsm F.K c _)]
      show _ = F.K.add _ (F.K.mul (F.K.mul (F.K.mul (pw F.K x e) x) x) (evsF F x cs))
      rw [F.K.mul_assoc (F.K.mul (pw F.K x e) x) x, F.K.mul_assoc (pw F.K x e) x, ← F.K.mul_assoc x x]
  show sumK F.K _ _ = _
  rw [sumK_ext F.K _ _ pt, sumK_add, ← mul_sumK]
  rfl

theorem idx2' (e i : Nat) : e + 2 + 2 * i = e + 2 * (i + 1) := by
  rw [Nat.add_assoc, Nat.add_comm 2 (2 * i)]
  rfl

theorem T_nil (e : Nat) : TF F e [] = F.K.zero := by
  show sumK F.K (fun x => F.K.mul (pw F.K x e) F.K.zero) _ = _
  rw [sumK_ext F.K _ (fun _ => F.K.zero) (fun x => DetChain.mul_zero F.K _), sumK_zero' F]

theorem T_zero : ∀ (cs : List Nat) (e : Nat), (∀ i, i < cs.length → psF F (e + 2 * i) = F.K.zero) → TF F e cs = F.K.zero
  | [], e, _ => T_nil F e
  | c :: cs, e, h => by
    have h0 : psF F e = F.K.zero := h 0 (Nat.zero_lt_succ _)
    rw [T_cons, h0, DetChain.mul_zero, F.K.zero_add]
    exact T_zero cs (e + 2) (fun i hi => by rw [idx2']; exact h (i + 1) (Nat.succ_lt_succ hi))

theorem T_skip (e c : Nat) (cs : List Nat) (h : psF F e = F.K.zero) : TF F e (c :: cs) = TF F (e + 2) cs := by
  rw [T_cons, h, DetChain.mul_zero, F.K.zero_add]

theorem skip_many (n : Nat) (m : Nat) : ∀ (i j e : Nat), (∀ i', i' < i → psF F (e + 2 * i') = F.K.zero) →
    TF F e (Frobenius.listC n j (i + m)) = TF F (e + 2 * i) (Frobenius.listC n (j + i) m)
  | 0, j, e, _ => by rw [Nat.zero_add]; rfl
  | i + 1, j, e, h => by
    rw [Nat.succ_add]
    show TF F e (Frobenius.C n j :: Frobenius.listC n (j + 1) (i + m)) = _
    rw [T_skip F e _ _ (h 0 (Nat.zero_lt_succ _) : psF F e = _),
      skip_many n m i (j + 1) (e + 2) (fun i' hi' => by rw [idx2']; exact h (i' + 1) (Nat.succ_lt_succ hi')),
      idx2', Nat.add_assoc j 1 i, Nat.add_comm 1 i]

theorem cub_factor (x : R) :
    CMInert.cub F.K x = F.K.mul x (F.K.add (F.K.mul x x) (F.K.neg F.K.one)) := by
  show F.K.add (F.K.mul (F.K.mul x x) x) (F.K.neg x) = _
  rw [F.K.distrib, mul_neg_r F.K, F.K.mul_comm x F.K.one, F.K.one_mul, F.K.mul_comm (F.K.mul x x) x]

theorem cub_factorF (x : R) :
    CMInert.cub F.K x = F.K.mul x (F.K.add (F.K.mul x x) (F.K.neg F.K.one)) := by
  show F.K.add (F.K.mul (F.K.mul x x) x) (F.K.neg x) = _
  lib_ring

theorem fin_hasse (k : Nat) (hk : F.h = 2 * k + 2) :
    sumK F.K (fun x => pw F.K (CMInert.cub F.K x) (2 * k + 2)) F.els
      = F.K.neg (nsm F.K (Frobenius.C (2 * k + 2) (k + 1)) (pw F.K (F.K.neg F.K.one) (k + 1))) := by
  have eA : sumK F.K (fun x => pw F.K (CMInert.cub F.K x) (2 * k + 2)) F.els
      = TF F (2 * k + 2) (Frobenius.row (2 * k + 2)) :=
    sumK_ext F.K _ _ (fun x => by
      rw [cub_factorF F x, pw_mul, ← ev_row]
      rfl) _
  rw [eA, Frobenius.row_eq, ar1 k,
    skip_many F (2 * k + 2) (k + 2) (k + 1) 0 (2 * k + 2) (fun i' hi' =>
      ps_low F k hk _ (Nat.le_trans (Nat.succ_le_succ (Nat.zero_le _)) (Nat.le_add_right _ _))
        (ar3 k i' (Nat.le_of_lt_succ hi'))),
    Nat.zero_add]
  show TF F (2 * k + 2 + 2 * (k + 1))
    (Frobenius.C (2 * k + 2) (k + 1) :: Frobenius.listC (2 * k + 2) (k + 1 + 1) (k + 1)) = _
  rw [T_cons, Frobenius.len_listC, ar2 k, ps_top F k hk,
    T_zero F _ _ (fun i hi => by
      rw [Frobenius.len_listC] at hi
      rw [Nat.add_assoc (2 * (2 * k + 1) + 1 + 1) 2 (2 * i)]
      exact ps_high F k hk _ (Nat.le_trans (Nat.succ_le_succ (Nat.zero_le 1)) (Nat.le_add_right 2 _))
        (ar4 k i (Nat.le_of_lt_succ hi))),
    DetChain.add_zero', mul_neg_r F.K, F.K.mul_comm _ F.K.one, F.K.one_mul]

end FinHasse

#print axioms FinHasse.two_h
#print axioms FinHasse.ps_low
#print axioms FinHasse.ps_top
#print axioms FinHasse.ps_high
#print axioms FinHasse.sumK_zero'
#print axioms FinHasse.T_cons
#print axioms FinHasse.idx2'
#print axioms FinHasse.T_nil
#print axioms FinHasse.T_zero
#print axioms FinHasse.T_skip
#print axioms FinHasse.skip_many
#print axioms FinHasse.cub_factor
#print axioms FinHasse.cub_factorF
#print axioms FinHasse.fin_hasse

lang/FinHasse3.lean

lean_all/lang/FinHasse3.lean

/-
  lang/FinHasse3 — T43, cycle 102: the Hasse invariant of y² = x³ + 1 over every finite field with q ≡ 1 (mod 6).

  For an FF with h = (q − 1)/2 = 3k + 3:
    ps_zero, ps_low3, ps_top3, ps_high3   Σ_x x^e = 0 (e = 0 and 1 ≤ e ≤ q − 2), −1 (e = q − 1), 0 (q ≤ e ≤ 2q − 3)
    T3F, T3_cons, T3_zero, skip3_many     one binomial coefficient at a time (port of HasseEisen)
    fin_hasse3     Σ_x (x³ + 1)^h = −C(3k + 3, 2k + 2)   in the field
  Over 𝔽_{p²} (p ≡ 1 mod 3) this is the input, with Lucas, for a(𝔽_{p²}) = a_p² − 2p for y² = x³ + 1.

  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.FinHasse
import lang.HasseEisen

namespace FinHasse3

open FTAChain YMChain Frobenius FinField HasseCM

variable {R : Type} [DecidableEq R] (F : FF R)

theorem two_h3 (k : Nat) (hk : F.h = 3 * k + 3) : 2 * F.h = (6 * k + 5) + 1 := by rw [hk]; lib_nat

theorem ps_zero : FinHasse.psF F 0 = F.K.zero := by
  show sumK F.K (fun _ => F.K.one) F.els = F.K.zero
  rw [FinPowerSum.sum_one_K, FinList.len_els F, FinPowerSum.char_q]

theorem ps_low3 (k : Nat) (hk : F.h = 3 * k + 3) (e : Nat) (h1 : 1 ≤ e) (h2 : e ≤ 6 * k + 5) : FinHasse.psF F e = F.K.zero := by
  cases e with
  | zero => exact absurd h1 (Nat.not_succ_le_zero 0)
  | succ e' => exact FinPowerSum.ps_zero_ff F e' (by rw [two_h3 F k hk]; exact Nat.succ_le_succ h2)

theorem ps_top3 (k : Nat) (hk : F.h = 3 * k + 3) : FinHasse.psF F ((6 * k + 5) + 1) = F.K.neg F.K.one := by
  rw [← two_h3 F k hk]; exact FinPowerSum.ps_top_ff F

theorem ps_high3 (k : Nat) (hk : F.h = 3 * k + 3) (r : Nat) (h1 : 1 ≤ r) (h2 : r ≤ 6 * k + 5) :
    FinHasse.psF F ((6 * k + 5) + 1 + r) = F.K.zero := by
  have e : FinHasse.psF F ((6 * k + 5) + 1 + r) = FinHasse.psF F r := sumK_ext F.K _ _ (fun x => by
    show pw F.K x ((6 * k + 5) + 1 + r) = pw F.K x r
    by_cases hx : x = F.K.zero
    · rw [hx]
      cases r with
      | zero => exact absurd h1 (Nat.not_succ_le_zero 0)
      | succ r' => rw [pw_zero_succ, ← Nat.add_assoc, pw_zero_succ]
    · rw [pw_add' F.K x _ r, ← two_h3 F k hk, FinCyclic.fermat_ff F x hx, F.K.one_mul]) _
  rw [e]
  exact ps_low3 F k hk r h1 h2

def ev3F (x : R) (cs : List Nat) : R := ev F.K (pw F.K x 3) F.K.one cs

def T3F (e : Nat) (cs : List Nat) : R :=
  sumK F.K (fun x => F.K.mul (pw F.K x e) (ev3F F x cs)) F.els

theorem T3_cons (e c : Nat) (cs : List Nat) :
    T3F F e (c :: cs) = F.K.add (F.K.mul (nsm F.K c F.K.one) (FinHasse.psF F e)) (T3F F (e + 3) cs) := by
  have pt : ∀ x : R, F.K.mul (pw F.K x e) (ev3F F x (c :: cs))
      = F.K.add (F.K.mul (nsm F.K c F.K.one) (pw F.K x e)) (F.K.mul (pw F.K x (e + 3)) (ev3F F x cs)) := fun x => by
    show F.K.mul (pw F.K x e) (F.K.add (nsm F.K c (pw F.K F.K.one cs.length)) (F.K.mul (pw F.K x 3) (ev3F F x cs))) = _
    rw [TwoPower.pwone, F.K.distrib, F.K.mul_comm (pw F.K x e) (nsm F.K c _), ← F.K.mul_assoc, ← pw_add' F.K x e 3]
  show sumK F.K _ _ = _
  rw [sumK_ext F.K _ _ pt, sumK_add, ← mul_sumK]
  rfl

theorem T3_nil (e : Nat) : T3F F e [] = F.K.zero := by
  show sumK F.K (fun x => F.K.mul (pw F.K x e) F.K.zero) _ = _
  rw [sumK_ext F.K _ (fun _ => F.K.zero) (fun x => DetChain.mul_zero F.K _), FinHasse.sumK_zero' F]

theorem idx3' (e i : Nat) : e + 3 + 3 * i = e + 3 * (i + 1) := by lib_nat

theorem T3_zero : ∀ (cs : List Nat) (e : Nat), (∀ i, i < cs.length → FinHasse.psF F (e + 3 * i) = F.K.zero) → T3F F e cs = F.K.zero
  | [], e, _ => T3_nil F e
  | c :: cs, e, h => by
    have h0 : FinHasse.psF F e = F.K.zero := h 0 (Nat.zero_lt_succ _)
    rw [T3_cons, h0, DetChain.mul_zero, F.K.zero_add]
    exact T3_zero cs (e + 3) (fun i hi => by rw [idx3']; exact h (i + 1) (Nat.succ_lt_succ hi))

theorem skip3_many (n m : Nat) : ∀ (i j e : Nat), (∀ i', i' < i → FinHasse.psF F (e + 3 * i') = F.K.zero) →
    T3F F e (Frobenius.listC n j (i + m)) = T3F F (e + 3 * i) (Frobenius.listC n (j + i) m)
  | 0, j, e, _ => by rw [Nat.zero_add]; rfl
  | i + 1, j, e, h => by
    rw [Nat.succ_add]
    show T3F F e (Frobenius.C n j :: Frobenius.listC n (j + 1) (i + m)) = _
    have h0 : FinHasse.psF F e = F.K.zero := h 0 (Nat.zero_lt_succ _)
    rw [T3_cons, h0, DetChain.mul_zero, F.K.zero_add,
      skip3_many n m i (j + 1) (e + 3) (fun i' hi' => by rw [idx3']; exact h (i' + 1) (Nat.succ_lt_succ hi')),
      idx3', Nat.add_assoc j 1 i, Nat.add_comm 1 i]

theorem fin_hasse3 (k : Nat) (hk : F.h = 3 * k + 3) :
    sumK F.K (fun x => pw F.K (F.K.add (pw F.K x 3) F.K.one) (3 * k + 3)) F.els
      = F.K.neg (nsm F.K (Frobenius.C (3 * k + 3) (2 * k + 2)) F.K.one) := by
  have eA : sumK F.K (fun x => pw F.K (F.K.add (pw F.K x 3) F.K.one) (3 * k + 3)) F.els
      = T3F F 0 (Frobenius.row (3 * k + 3)) :=
    sumK_ext F.K _ _ (fun x => by
      show _ = F.K.mul F.K.one (ev3F F x (Frobenius.row (3 * k + 3)))
      rw [F.K.one_mul, ev3F, ev_row]) _
  rw [eA, Frobenius.row_eq, show 3 * k + 3 + 1 = (2 * k + 2) + (k + 2) by lib_nat,
    skip3_many F (3 * k + 3) (k + 2) (2 * k + 2) 0 0 (fun i' hi' => by
      cases i' with
      | zero => exact ps_zero F
      | succ i'' =>
        apply ps_low3 F k hk
        · rw [show 0 + 3 * (i'' + 1) = 1 + (3 * i'' + 2) by lib_nat]; exact Nat.le_add_right 1 _
        · have hi : i'' ≤ 2 * k := Nat.le_of_succ_le_succ (Nat.le_of_lt_succ hi')
          have a : 0 + 3 * (i'' + 1) ≤ 0 + 3 * (2 * k + 1) := Nat.add_le_add_left (Nat.mul_le_mul_left 3 (Nat.succ_le_succ hi)) 0
          exact Nat.le_trans a (by rw [show 0 + 3 * (2 * k + 1) = 6 * k + 3 by lib_nat]; exact Nat.le_succ_of_le (Nat.le_succ _)))]
  rw [show 0 + (2 * k + 2) = 2 * k + 2 by lib_nat]
  show T3F F (0 + 3 * (2 * k + 2)) (Frobenius.C (3 * k + 3) (2 * k + 2) :: Frobenius.listC (3 * k + 3) (2 * k + 2 + 1) (k + 1)) = _
  rw [T3_cons, show 0 + 3 * (2 * k + 2) = (6 * k + 5) + 1 by lib_nat, ps_top3 F k hk,
    T3_zero F _ _ (fun i hi => by
      rw [Frobenius.len_listC] at hi
      rw [show (6 * k + 5) + 1 + 3 + 3 * i = (6 * k + 5) + 1 + (3 * i + 3) by lib_nat]
      apply ps_high3 F k hk
      · exact Nat.le_add_left 1 _
      · have a : 3 * i + 3 ≤ 3 * k + 3 := Nat.add_le_add_right (Nat.mul_le_mul_left 3 (Nat.le_of_lt_succ hi)) 3
        exact Nat.le_trans a (by rw [show 6 * k + 5 = (3 * k + 3) + (3 * k + 2) by lib_nat]; exact Nat.le_add_right _ _)),
    DetChain.add_zero', mul_neg_r F.K, F.K.mul_comm _ F.K.one, F.K.one_mul]

end FinHasse3

#print axioms FinHasse3.two_h3
#print axioms FinHasse3.ps_zero
#print axioms FinHasse3.ps_low3
#print axioms FinHasse3.ps_top3
#print axioms FinHasse3.ps_high3
#print axioms FinHasse3.T3_cons
#print axioms FinHasse3.T3_nil
#print axioms FinHasse3.idx3'
#print axioms FinHasse3.T3_zero
#print axioms FinHasse3.skip3_many
#print axioms FinHasse3.fin_hasse3

lang/FinHasseD.lean

lean_all/lang/FinHasseD.lean

/-
  lang/FinHasseD — T43, cycle 116: the Hasse invariant of every sextic twist y² = x³ + D over 𝔽_q, q ≡ 1 (mod 6).

  For an FF with h = (q − 1)/2 = 3k + 3:
    ps_zero, ps_low3, ps_top3, ps_high3   Σ_x x^e = 0 (e = 0 and 1 ≤ e ≤ q − 2), −1 (e = q − 1), 0 (q ≤ e ≤ 2q − 3)
    T3F, T3_cons, T3_zero, skip3_many     one binomial coefficient at a time (port of HasseEisen)
    fin_hasseD     Σ_x (x³ + D)^h = −C(3k + 3, 2k + 2) D^(k+1)   in the field  (D^((q−1)/6))
  Over 𝔽_{p²} (p ≡ 1 mod 3) this is the input, with Lucas, for a(𝔽_{p²}) = a_p² − 2p for y² = x³ + 1.

  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.FinHasse3

namespace FinHasseD

open FTAChain YMChain Frobenius FinField HasseCM

variable {R : Type} [DecidableEq R] (F : FF R)

def ev3F (D x : R) (cs : List Nat) : R := ev F.K (pw F.K x 3) D cs

def T3F (D : R) (e : Nat) (cs : List Nat) : R :=
  sumK F.K (fun x => F.K.mul (pw F.K x e) (ev3F F D x cs)) F.els

theorem T3_cons (e c : Nat) (cs : List Nat) :
    T3F F D e (c :: cs) = F.K.add (F.K.mul (nsm F.K c (pw F.K D cs.length)) (FinHasse.psF F e)) (T3F F D (e + 3) cs) := by
  have pt : ∀ x : R, F.K.mul (pw F.K x e) (ev3F F D x (c :: cs))
      = F.K.add (F.K.mul (nsm F.K c (pw F.K D cs.length)) (pw F.K x e)) (F.K.mul (pw F.K x (e + 3)) (ev3F F D x cs)) := fun x => by
    show F.K.mul (pw F.K x e) (F.K.add (nsm F.K c (pw F.K D cs.length)) (F.K.mul (pw F.K x 3) (ev3F F D x cs))) = _
    rw [F.K.distrib, F.K.mul_comm (pw F.K x e) (nsm F.K c _), ← F.K.mul_assoc, ← pw_add' F.K x e 3]
  show sumK F.K _ _ = _
  rw [sumK_ext F.K _ _ pt, sumK_add, ← mul_sumK]
  rfl

theorem T3_nil (e : Nat) : T3F F D e [] = F.K.zero := by
  show sumK F.K (fun x => F.K.mul (pw F.K x e) F.K.zero) _ = _
  rw [sumK_ext F.K _ (fun _ => F.K.zero) (fun x => DetChain.mul_zero F.K _), FinHasse.sumK_zero' F]

theorem idx3' (e i : Nat) : e + 3 + 3 * i = e + 3 * (i + 1) := by lib_nat

theorem T3_zero : ∀ (cs : List Nat) (e : Nat), (∀ i, i < cs.length → FinHasse.psF F (e + 3 * i) = F.K.zero) → T3F F D e cs = F.K.zero
  | [], e, _ => T3_nil F e
  | c :: cs, e, h => by
    have h0 : FinHasse.psF F e = F.K.zero := h 0 (Nat.zero_lt_succ _)
    rw [T3_cons, h0, DetChain.mul_zero, F.K.zero_add]
    exact T3_zero cs (e + 3) (fun i hi => by rw [idx3']; exact h (i + 1) (Nat.succ_lt_succ hi))

theorem skip3_many (n m : Nat) : ∀ (i j e : Nat), (∀ i', i' < i → FinHasse.psF F (e + 3 * i') = F.K.zero) →
    T3F F D e (Frobenius.listC n j (i + m)) = T3F F D (e + 3 * i) (Frobenius.listC n (j + i) m)
  | 0, j, e, _ => by rw [Nat.zero_add]; rfl
  | i + 1, j, e, h => by
    rw [Nat.succ_add]
    show T3F F D e (Frobenius.C n j :: Frobenius.listC n (j + 1) (i + m)) = _
    have h0 : FinHasse.psF F e = F.K.zero := h 0 (Nat.zero_lt_succ _)
    rw [T3_cons, h0, DetChain.mul_zero, F.K.zero_add,
      skip3_many n m i (j + 1) (e + 3) (fun i' hi' => by rw [idx3']; exact h (i' + 1) (Nat.succ_lt_succ hi')),
      idx3', Nat.add_assoc j 1 i, Nat.add_comm 1 i]

theorem fin_hasseD (D : R) (k : Nat) (hk : F.h = 3 * k + 3) :
    sumK F.K (fun x => pw F.K (F.K.add (pw F.K x 3) D) (3 * k + 3)) F.els
      = F.K.neg (nsm F.K (Frobenius.C (3 * k + 3) (2 * k + 2)) (pw F.K D (k + 1))) := by
  have eA : sumK F.K (fun x => pw F.K (F.K.add (pw F.K x 3) D) (3 * k + 3)) F.els
      = T3F F D 0 (Frobenius.row (3 * k + 3)) :=
    sumK_ext F.K _ _ (fun x => by
      show _ = F.K.mul F.K.one (ev3F F D x (Frobenius.row (3 * k + 3)))
      rw [F.K.one_mul, ev3F, ev_row]) _
  rw [eA, Frobenius.row_eq, show 3 * k + 3 + 1 = (2 * k + 2) + (k + 2) by lib_nat,
    skip3_many F (3 * k + 3) (k + 2) (2 * k + 2) 0 0 (fun i' hi' => by
      cases i' with
      | zero => exact FinHasse3.ps_zero F
      | succ i'' =>
        apply FinHasse3.ps_low3 F k hk
        · rw [show 0 + 3 * (i'' + 1) = 1 + (3 * i'' + 2) by lib_nat]; exact Nat.le_add_right 1 _
        · have hi : i'' ≤ 2 * k := Nat.le_of_succ_le_succ (Nat.le_of_lt_succ hi')
          have a : 0 + 3 * (i'' + 1) ≤ 0 + 3 * (2 * k + 1) := Nat.add_le_add_left (Nat.mul_le_mul_left 3 (Nat.succ_le_succ hi)) 0
          exact Nat.le_trans a (by rw [show 0 + 3 * (2 * k + 1) = 6 * k + 3 by lib_nat]; exact Nat.le_succ_of_le (Nat.le_succ _)))]
  rw [show 0 + (2 * k + 2) = 2 * k + 2 by lib_nat]
  show T3F F D (0 + 3 * (2 * k + 2)) (Frobenius.C (3 * k + 3) (2 * k + 2) :: Frobenius.listC (3 * k + 3) (2 * k + 2 + 1) (k + 1)) = _
  rw [T3_cons, show 0 + 3 * (2 * k + 2) = (6 * k + 5) + 1 by lib_nat, FinHasse3.ps_top3 F k hk,
    T3_zero F _ _ (fun i hi => by
      rw [Frobenius.len_listC] at hi
      rw [show (6 * k + 5) + 1 + 3 + 3 * i = (6 * k + 5) + 1 + (3 * i + 3) by lib_nat]
      apply FinHasse3.ps_high3 F k hk
      · exact Nat.le_add_left 1 _
      · have a : 3 * i + 3 ≤ 3 * k + 3 := Nat.add_le_add_right (Nat.mul_le_mul_left 3 (Nat.le_of_lt_succ hi)) 3
        exact Nat.le_trans a (by rw [show 6 * k + 5 = (3 * k + 3) + (3 * k + 2) by lib_nat]; exact Nat.le_add_right _ _)),
    DetChain.add_zero', mul_neg_r F.K, F.K.mul_comm _ F.K.one, F.K.one_mul, Frobenius.len_listC]

end FinHasseD

#print axioms FinHasseD.T3_cons
#print axioms FinHasseD.T3_nil
#print axioms FinHasseD.idx3'
#print axioms FinHasseD.T3_zero
#print axioms FinHasseD.skip3_many
#print axioms FinHasseD.fin_hasseD

lang/FinJTP.lean

lean_all/lang/FinJTP.lean

/-
  lang/FinJTP — T47, cycle 545: exponent bookkeeping for the finite Jacobi triple product, E(N, m) = (m − N)² in ℕ.

    Ex N m = (m − N)² + (N − m)²   (one of the two is 0)
    ex_up, ex_dn          Ex N (N + t) = t², Ex (N + t) N = t²
    ex_0, ex_a1, ex_b1    the boundary cases m = 0, 1 of the recursion
    ex_a, ex_b, ex_c      the three exponent identities behind the double Pascal step:
       Ex(N+1, m+2) + 2(m+2) = Ex(N, m+2) + 2N + 1,  Ex(N+1, m+2) = Ex(N, m+1),
       Ex(N+1, m+2) + 2(2N − m) = Ex(N, m) + 2N + 1   (m ≤ 2N)
  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.QBinom2
import lang.LucMul

namespace QBinom

open FTAChain

def Ex (N m : Nat) : Nat := (m - N) * (m - N) + (N - m) * (N - m)

theorem sub_add_l (N t : Nat) : N + t - N = t := by
  have h := LibInt.nsub_add_add t 0 N
  rw [Nat.zero_add, show t + N = N + t by lib_nat] at h; exact h

theorem sub_add_r (N t : Nat) : N - (N + t) = 0 := by
  have h := LibInt.nsub_add_add 0 t N
  rw [Nat.zero_add, show t + N = N + t by lib_nat, LibInt.nzero_sub] at h; exact h

theorem ex_up (N t : Nat) : Ex N (N + t) = t * t := by
  show (N + t - N) * (N + t - N) + (N - (N + t)) * (N - (N + t)) = _
  rw [sub_add_l, sub_add_r]; lib_nat

theorem ex_dn (N t : Nat) : Ex (N + t) N = t * t := by
  show (N - (N + t)) * (N - (N + t)) + (N + t - N) * (N + t - N) = _
  rw [sub_add_l, sub_add_r]; lib_nat

theorem ex_0 (N : Nat) : Ex (N + 1) 0 = Ex N 0 + (2 * N + 1) := by
  rw [show N + 1 = 0 + (N + 1) by lib_nat, ex_dn, show N = 0 + N by lib_nat, ex_dn]
  rw [show 0 + N = N by lib_nat]; lib_nat

theorem ex_b1 (N : Nat) : Ex (N + 1) 1 = Ex N 0 := by
  rw [show N + 1 = 1 + N by lib_nat, ex_dn, show N = 0 + N by lib_nat, ex_dn]; lib_nat

theorem ex_a1 (N : Nat) : Ex N 1 + (2 * N + 1) = Ex (N + 1) 1 + 2 := by
  rw [ex_b1]
  cases N with
  | zero => rfl
  | succ N =>
    rw [show N + 1 = 1 + N by lib_nat, ex_dn, show 1 + N = 0 + (1 + N) by lib_nat, ex_dn]; lib_nat

/-- Case split m + 1 versus N. -/
theorem ex_a (N m : Nat) : Ex (N + 1) (m + 2) + 2 * (m + 2) = Ex N (m + 2) + (2 * N + 1) := by
  cases Nat.lt_or_ge (m + 1) N with
  | inl h =>
    obtain ⟨t, ht⟩ := Cheb.le_dest2 (m + 2) N h
    rw [← ht, show t + (m + 2) + 1 = (m + 2) + (t + 1) by lib_nat, ex_dn, show t + (m + 2) = (m + 2) + t by lib_nat, ex_dn]; lib_nat
  | inr h =>
    obtain ⟨t, ht⟩ := Cheb.le_dest2 N (m + 1) h
    rw [show m + 2 = (N + 1) + t by rw [show m + 2 = (m + 1) + 1 from rfl, ← ht]; lib_nat, ex_up, show N + 1 + t = N + (t + 1) by lib_nat, ex_up]; lib_nat

theorem ex_b (N m : Nat) : Ex (N + 1) (m + 2) = Ex N (m + 1) := by
  cases Nat.lt_or_ge (m + 1) N with
  | inl h =>
    obtain ⟨t, ht⟩ := Cheb.le_dest2 (m + 2) N h
    rw [← ht, show t + (m + 2) + 1 = (m + 2) + (t + 1) by lib_nat, ex_dn, show t + (m + 2) = (m + 1) + (t + 1) by lib_nat, ex_dn]
  | inr h =>
    obtain ⟨t, ht⟩ := Cheb.le_dest2 N (m + 1) h
    rw [show m + 2 = (N + 1) + t by rw [show m + 2 = (m + 1) + 1 from rfl, ← ht]; lib_nat, ex_up, show m + 1 = N + t by rw [← ht]; lib_nat, ex_up]

theorem ex_c (N m : Nat) (hm : m ≤ 2 * N) : Ex (N + 1) (m + 2) + 2 * (2 * N - m) = Ex N m + (2 * N + 1) := by
  obtain ⟨s, hs⟩ := Cheb.le_dest2 m (2 * N) hm
  have h2 : 2 * N - m = s := by rw [← hs]; exact sub_add_l' s m
  rw [h2]
  cases Nat.lt_or_ge m N with
  | inl h =>
    obtain ⟨t, ht⟩ := Cheb.le_dest2 (m + 1) N h
    -- N = m + 1 + t, s = N + N − m = 2t + m + 2
    have hs' : s = m + 2 * t + 2 := by
      have := hs; rw [← ht] at this
      exact Cheb.add_rc m _ _ (this.trans (by lib_nat))
    rw [hs', ← ht]
    cases t with
    | zero =>
      rw [show 0 + (m + 1) + 1 = (m + 2) + 0 by lib_nat, ex_dn, show 0 + (m + 1) = m + 1 by lib_nat, ex_dn]; lib_nat
    | succ t =>
      rw [show t + 1 + (m + 1) + 1 = (m + 2) + (t + 1) by lib_nat, ex_dn, show t + 1 + (m + 1) = m + (t + 2) by lib_nat, ex_dn]; lib_nat
  | inr h =>
    obtain ⟨t, ht⟩ := Cheb.le_dest2 N m h
    -- m = N + t, s = N − t
    have hs' : s + t = N := Cheb.add_rc N _ _ ((show s + t + N = s + (t + N) by lib_nat).trans (by rw [ht]; exact hs.trans (by lib_nat)))
    rw [← ht]
    cases t with
    | zero => rw [show 0 + N + 2 = (N + 1) + 1 by lib_nat, ex_up, show 0 + N = N + 0 by lib_nat, ex_up]; rw [← hs']; lib_nat
    | succ t =>
      rw [show t + 1 + N + 2 = (N + 1) + (t + 2) by lib_nat, ex_up, show t + 1 + N = N + (t + 1) by lib_nat, ex_up]
      rw [← hs']; lib_nat
where
  sub_add_l' (s m : Nat) : s + m - m = s := by
    have h := LibInt.nsub_add_add s 0 m
    rw [Nat.zero_add] at h; exact h

end QBinom

#print axioms QBinom.sub_add_l
#print axioms QBinom.sub_add_r
#print axioms QBinom.ex_up
#print axioms QBinom.ex_dn
#print axioms QBinom.ex_0
#print axioms QBinom.ex_b1
#print axioms QBinom.ex_a1
#print axioms QBinom.ex_a
#print axioms QBinom.ex_b
#print axioms QBinom.ex_c

lang/FinJTP2.lean

lean_all/lang/FinJTP2.lean

/-
  lang/FinJTP2 — T47, cycle 546: **the finite Jacobi triple product** in any commutative ring:
  the coefficient of z^m in ∏_{k=1}^{N} (1 + zq^(2k−1))(z + q^(2k−1)) is [2N; m]_{q²}·q^((m−N)²).

    bj q N m     the coefficients, by the recursion of multiplying with q^(2N+1) + (1 + q^(4N+2))z + q^(2N+1)z²
    pwQ          (q²)^k = q^(2k)
    fjtp         bj q N m = [2N; m]_{q²}·q^(Ex N m)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.FinJTP

namespace QBinom

open FTAChain

variable {R : Type} (K : DetChain.CRing R) (q : R)

def bj : Nat → Nat → R
  | 0, 0 => K.one
  | 0, _ + 1 => K.zero
  | N + 1, 0 => K.mul (Frobenius.pw K q (2 * N + 1)) (bj N 0)
  | N + 1, 1 => K.add (K.mul (Frobenius.pw K q (2 * N + 1)) (bj N 1)) (K.mul (K.add K.one (Frobenius.pw K q (4 * N + 2))) (bj N 0))
  | N + 1, m + 2 => K.add (K.add (K.mul (Frobenius.pw K q (2 * N + 1)) (bj N (m + 2)))
      (K.mul (K.add K.one (Frobenius.pw K q (4 * N + 2))) (bj N (m + 1)))) (K.mul (Frobenius.pw K q (2 * N + 1)) (bj N m))

theorem pwQ (k : Nat) : Frobenius.pw K (K.mul q q) k = Frobenius.pw K q (2 * k) := by
  rw [Frobenius.pw_mul, ← HasseCM.pw_add']; exact congrArg (Frobenius.pw K q) (by lib_nat)

theorem pw_join (a b c : Nat) (h : a + b = c) : K.mul (Frobenius.pw K q a) (Frobenius.pw K q b) = Frobenius.pw K q c := by
  rw [← HasseCM.pw_add', h]

theorem fjtp : ∀ N m, bj K q N m = K.mul (gb K (K.mul q q) (2 * N) m) (Frobenius.pw K q (Ex N m))
  | 0, 0 => by show K.one = K.mul K.one (Frobenius.pw K q 0); show K.one = K.mul K.one K.one; lib_ring
  | 0, m + 1 => by show K.zero = K.mul K.zero _; lib_ring
  | N + 1, 0 => by
    show K.mul (Frobenius.pw K q (2 * N + 1)) (bj K q N 0) = _
    rw [fjtp N 0, gb_zero, gb_zero, ← pw_join K q (Ex N 0) (2 * N + 1) _ (ex_0 N).symm]
    lib_ring
  | N + 1, 1 => by
    show K.add (K.mul (Frobenius.pw K q (2 * N + 1)) (bj K q N 1)) (K.mul (K.add K.one (Frobenius.pw K q (4 * N + 2))) (bj K q N 0)) = _
    rw [fjtp N 1, fjtp N 0, gb_zero, ex_b1]
    -- [2N+2; 1] = Q([2N; 1] + Q^(2N)) + 1
    rw [show 2 * (N + 1) = (2 * N + 1) + 1 by lib_nat, show (1 : Nat) = 0 + 1 from rfl, pascalA, gb_zero]
    show _ = K.mul (K.add (K.mul (Frobenius.pw K (K.mul q q) (0 + 1)) (K.add (gb K (K.mul q q) (2 * N) (0 + 1))
      (K.mul (Frobenius.pw K (K.mul q q) (2 * N - 0)) (gb K (K.mul q q) (2 * N) 0)))) K.one) (Frobenius.pw K q (Ex N 0))
    rw [gb_zero, pwQ, pwQ, show 2 * N - 0 = 2 * N from rfl, show (0 : Nat) + 1 = 1 from rfl]
    have t1 : K.mul (Frobenius.pw K q (2 * N + 1)) (Frobenius.pw K q (Ex N 1)) =
        K.mul (Frobenius.pw K q (2 * 1)) (Frobenius.pw K q (Ex N 0)) := by
      rw [pw_join K q _ _ _ rfl, pw_join K q _ _ _ rfl]
      exact congrArg (Frobenius.pw K q) ((Nat.add_comm _ _).trans ((ex_a1 N).trans (by rw [ex_b1]; lib_nat)))
    have t2 : K.mul (Frobenius.pw K q (2 * 1)) (Frobenius.pw K q (2 * (2 * N))) = Frobenius.pw K q (4 * N + 2) :=
      pw_join K q _ _ _ (by lib_nat)
    generalize gb K (K.mul q q) (2 * N) 1 = G at *
    calc _ = K.add (K.mul (K.mul (Frobenius.pw K q (2 * N + 1)) (Frobenius.pw K q (Ex N 1))) G)
          (K.mul (K.add K.one (Frobenius.pw K q (4 * N + 2))) (Frobenius.pw K q (Ex N 0))) := by lib_ring
      _ = _ := by rw [t1, ← t2]; lib_ring
  | N + 1, m + 2 => by
    show K.add (K.add (K.mul (Frobenius.pw K q (2 * N + 1)) (bj K q N (m + 2)))
      (K.mul (K.add K.one (Frobenius.pw K q (4 * N + 2))) (bj K q N (m + 1)))) (K.mul (Frobenius.pw K q (2 * N + 1)) (bj K q N m)) = _
    rw [fjtp N (m + 2), fjtp N (m + 1), fjtp N m]
    cases Nat.lt_or_ge (2 * N) m with
    | inl hbig =>
      rw [gb_big K _ (2 * (N + 1)) (m + 2) (by rw [show 2 * (N + 1) = 2 * N + 2 by lib_nat]; exact Nat.succ_lt_succ (Nat.succ_lt_succ hbig)),
        gb_big K _ (2 * N) (m + 2) (Nat.lt_trans hbig (Nat.lt_trans (Nat.lt_succ_self m) (Nat.lt_succ_self _))),
        gb_big K _ (2 * N) (m + 1) (Nat.lt_trans hbig (Nat.lt_succ_self m)), gb_big K _ (2 * N) m hbig]
      lib_ring
    | inr hle =>
      rw [show 2 * (N + 1) = 2 * N + 2 by lib_nat, dpascal K _ (2 * N) m hle, pwQ, pwQ, pwQ]
      have t1 : K.mul (Frobenius.pw K q (2 * N + 1)) (Frobenius.pw K q (Ex N (m + 2))) =
          K.mul (Frobenius.pw K q (2 * (m + 2))) (Frobenius.pw K q (Ex (N + 1) (m + 2))) := by
        rw [pw_join K q _ _ _ rfl, pw_join K q _ _ _ rfl]
        exact congrArg (Frobenius.pw K q) ((Nat.add_comm _ _).trans ((ex_a N m).symm.trans (Nat.add_comm _ _)))
      have t2 : Frobenius.pw K q (Ex N (m + 1)) = Frobenius.pw K q (Ex (N + 1) (m + 2)) := congrArg _ (ex_b N m).symm
      have t3 : K.mul (Frobenius.pw K q (2 * N + 1)) (Frobenius.pw K q (Ex N m)) =
          K.mul (Frobenius.pw K q (2 * (2 * N - m))) (Frobenius.pw K q (Ex (N + 1) (m + 2))) := by
        rw [pw_join K q _ _ _ rfl, pw_join K q _ _ _ rfl]
        exact congrArg (Frobenius.pw K q) ((Nat.add_comm _ _).trans ((ex_c N m hle).symm.trans (Nat.add_comm _ _)))
      have t4 : Frobenius.pw K q (2 * (2 * N + 1)) = Frobenius.pw K q (4 * N + 2) := congrArg _ (by lib_nat)
      rw [t4]
      generalize gb K (K.mul q q) (2 * N) (m + 2) = G2 at *
      generalize gb K (K.mul q q) (2 * N) (m + 1) = G1 at *
      generalize gb K (K.mul q q) (2 * N) m = G0 at *
      calc _ = K.add (K.add (K.mul (K.mul (Frobenius.pw K q (2 * N + 1)) (Frobenius.pw K q (Ex N (m + 2)))) G2)
            (K.mul (K.add K.one (Frobenius.pw K q (4 * N + 2))) (K.mul G1 (Frobenius.pw K q (Ex N (m + 1))))))
            (K.mul (K.mul (Frobenius.pw K q (2 * N + 1)) (Frobenius.pw K q (Ex N m))) G0) := by lib_ring
        _ = _ := by rw [t1, t2, t3]; lib_ring

end QBinom

#print axioms QBinom.pwQ
#print axioms QBinom.pw_join
#print axioms QBinom.fjtp

lang/FinJTP3.lean

lean_all/lang/FinJTP3.lean

/-
  lang/FinJTP3 — T47, cycle 548: the value of ∏_{k=1}^{N}(1 + zq^(2k−1))(z + q^(2k−1)) at a ring element z.

    es f z L        Σ_{m<L} f(m) z^m
    es_rec          for b′ given by the three-term recursion with A, C:  es b′ (L+2) = A·es b (L+2) + Cz·es b (L+1) + Az²·es b L
    bj_big          bj N m = 0 for m > 2N
    es_ext          es b z (L + t) = es b z L when b vanishes from L on
    ev_jtp          es (bj N) z (2N+1) = ∏_{k<N} (q^(2k+1) + (1 + q^(4k+2))z + q^(2k+1)z²)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.QBinom3

namespace QBinom

open FTAChain

variable {R : Type} (K : DetChain.CRing R)

def es (f : Nat → R) (z : R) : Nat → R
  | 0 => K.zero
  | L + 1 => K.add (es f z L) (K.mul (f L) (Frobenius.pw K z L))

theorem es_rec (b b' : Nat → R) (A C z : R) (h0 : b' 0 = K.mul A (b 0)) (h1 : b' 1 = K.add (K.mul A (b 1)) (K.mul C (b 0)))
    (h2 : ∀ m, b' (m + 2) = K.add (K.add (K.mul A (b (m + 2))) (K.mul C (b (m + 1)))) (K.mul A (b m))) : ∀ L,
    es K b' z (L + 2) = K.add (K.add (K.mul A (es K b z (L + 2))) (K.mul (K.mul C z) (es K b z (L + 1))))
      (K.mul (K.mul A (K.mul z z)) (es K b z L))
  | 0 => by
    show K.add (K.add K.zero (K.mul (b' 0) (Frobenius.pw K z 0))) (K.mul (b' 1) (Frobenius.pw K z 1)) =
      K.add (K.add (K.mul A (K.add (K.add K.zero (K.mul (b 0) (Frobenius.pw K z 0))) (K.mul (b 1) (Frobenius.pw K z 1))))
        (K.mul (K.mul C z) (K.add K.zero (K.mul (b 0) (Frobenius.pw K z 0))))) (K.mul (K.mul A (K.mul z z)) K.zero)
    rw [h0, h1]
    show K.add (K.add K.zero (K.mul (K.mul A (b 0)) K.one)) (K.mul (K.add (K.mul A (b 1)) (K.mul C (b 0))) (K.mul K.one z)) =
      K.add (K.add (K.mul A (K.add (K.add K.zero (K.mul (b 0) K.one)) (K.mul (b 1) (K.mul K.one z))))
        (K.mul (K.mul C z) (K.add K.zero (K.mul (b 0) K.one)))) (K.mul (K.mul A (K.mul z z)) K.zero)
    lib_ring
  | L + 1 => by
    show K.add (es K b' z (L + 2)) (K.mul (b' (L + 2)) (Frobenius.pw K z (L + 2))) =
      K.add (K.add (K.mul A (K.add (es K b z (L + 2)) (K.mul (b (L + 2)) (Frobenius.pw K z (L + 2)))))
        (K.mul (K.mul C z) (K.add (es K b z (L + 1)) (K.mul (b (L + 1)) (Frobenius.pw K z (L + 1))))))
        (K.mul (K.mul A (K.mul z z)) (K.add (es K b z L) (K.mul (b L) (Frobenius.pw K z L))))
    rw [es_rec b b' A C z h0 h1 h2 L, h2 L]
    have e2 : Frobenius.pw K z (L + 2) = K.mul (K.mul (Frobenius.pw K z L) z) z := rfl
    have e1 : Frobenius.pw K z (L + 1) = K.mul (Frobenius.pw K z L) z := rfl
    rw [e2, e1]
    generalize Frobenius.pw K z L = P
    lib_ring

theorem es_ext (b : Nat → R) (z : R) (L : Nat) (hb : ∀ t, b (L + t) = K.zero) : ∀ t, es K b z (L + t) = es K b z L
  | 0 => rfl
  | t + 1 => by
    show K.add (es K b z (L + t)) (K.mul (b (L + t)) (Frobenius.pw K z (L + t))) = _
    rw [es_ext b z L hb t, hb t]; lib_ring

variable (q : R)

theorem bj_big (N m : Nat) (h : 2 * N < m) : bj K q N m = K.zero := by
  rw [fjtp, gb_big K _ _ _ h]; lib_ring

def jf (z : R) : Nat → R
  | 0 => K.one
  | N + 1 => K.mul (jf z N) (K.add (K.add (Frobenius.pw K q (2 * N + 1)) (K.mul (K.add K.one (Frobenius.pw K q (4 * N + 2))) z))
      (K.mul (Frobenius.pw K q (2 * N + 1)) (K.mul z z)))

theorem ev_jtp (z : R) : ∀ N, es K (bj K q N) z (2 * N + 1) = jf K q z N
  | 0 => by show K.add K.zero (K.mul K.one K.one) = K.one; lib_ring
  | N + 1 => by
    have hr := es_rec K (bj K q N) (bj K q (N + 1)) (Frobenius.pw K q (2 * N + 1)) (K.add K.one (Frobenius.pw K q (4 * N + 2))) z
      rfl rfl (fun m => rfl) (2 * N + 1)
    rw [show 2 * (N + 1) + 1 = 2 * N + 1 + 2 by lib_nat, hr]
    have hz : ∀ t, bj K q N (2 * N + 1 + t) = K.zero := fun t => bj_big K q N _ (Nat.lt_of_lt_of_le (Nat.lt_succ_self _) (Nat.le_add_right _ t))
    rw [show 2 * N + 1 + 2 = (2 * N + 1) + 2 from rfl, es_ext K _ z _ hz 2, es_ext K _ z _ hz 1, ev_jtp z N]
    show _ = K.mul (jf K q z N) _
    lib_ring

end QBinom

#print axioms QBinom.es_rec
#print axioms QBinom.es_ext
#print axioms QBinom.bj_big
#print axioms QBinom.ev_jtp

lang/FinList.lean

lean_all/lang/FinList.lean

/-
  lang/FinList — T43, cycle 78: from a complete element list to a distinct list of the nonzero elements.

  For an FF of order q = 2h + 1:
    cnt, cnt_int         the natural-number count of z in a list, and Σ δ(y, z) = cnt
    dist_of_complete     a complete list has no repetitions (RootBound.Dist)
    len_els              its length is q
    nzL, dist_nzL, len_nzL, mem_nzL   the nonzero elements, distinct, q − 1 of them
  Input for the root bound in a general finite field (non-roots of x^d − 1, cyclicity of 𝔽_q^×).

  Lean 4 core only, proofs by hand (lib_nat, lib_int), no axioms.
-/
import lang.FinField
import lang.RootBound
import lib.Semi

namespace FinList

open YMChain FiniteRecChain FinField

variable {R : Type} [DecidableEq R]

def cnt (z : R) : List R → Nat
  | [] => 0
  | a :: l => (if a = z then 1 else 0) + cnt z l

theorem cnt_int (z : R) : ∀ l : List R, sumK LibInt.intR (fun y => delta LibInt.intR y z) l = Int.ofNat (cnt z l)
  | [] => rfl
  | a :: l => by
    show delta LibInt.intR a z + sumK LibInt.intR _ l = Int.ofNat ((if a = z then 1 else 0) + cnt z l)
    rw [cnt_int z l]
    unfold delta
    by_cases h : a = z
    · rw [ite_pos' _ _ h, ite_pos' _ _ h]; rfl
    · rw [ite_neg' _ _ h, ite_neg' _ _ h]; show (0 : Int) + Int.ofNat (cnt z l) = Int.ofNat (0 + cnt z l); rw [Nat.zero_add]; lib_int

theorem cnt_pos (z : R) : ∀ l : List R, memL z l → 1 ≤ cnt z l
  | [], h => False.elim h
  | a :: l, h => by
    show 1 ≤ (if a = z then 1 else 0) + cnt z l
    cases h with
    | inl e => rw [ite_pos' _ _ e.symm]; exact Nat.le_add_right 1 _
    | inr e => exact Nat.le_trans (cnt_pos z l e) (Nat.le_add_left _ _)

theorem dist_of_le_one : ∀ l : List R, (∀ z, cnt z l ≤ 1) → RootBound.Dist l
  | [], _ => trivial
  | a :: l, h => by
    refine ⟨fun s hs e => ?_, dist_of_le_one l (fun z => Nat.le_trans (Nat.le_add_left _ _) (h z))⟩
    have h1 := h a
    have h2 := cnt_pos a l (e ▸ hs)
    have : 2 ≤ cnt a (a :: l) := by
      show 2 ≤ (if a = a then 1 else 0) + cnt a l
      rw [ite_pos' _ _ rfl, Nat.add_comm]; exact Nat.succ_le_succ h2
    exact Nat.not_succ_le_self 1 (Nat.le_trans this h1)

theorem dist_of_complete (l : List R) (hc : Complete LibInt.intR l) : RootBound.Dist l :=
  dist_of_le_one l (fun z => by
    have := (cnt_int z l).symm.trans (hc z)
    have e : cnt z l = 1 := Int.ofNat.inj this
    rw [e]; exact Nat.le_refl 1)

omit [DecidableEq R] in
theorem len_int : ∀ l : List R, sumK LibInt.intR (fun _ => (1 : Int)) l = Int.ofNat l.length
  | [] => rfl
  | _ :: l => by
    show 1 + sumK LibInt.intR (fun _ => (1 : Int)) l = Int.ofNat (l.length + 1)
    rw [len_int l]; show 1 + Int.ofNat l.length = Int.ofNat l.length + 1; lib_int

variable (F : FF R)

theorem len_els : F.els.length = 2 * F.h + 1 := Int.ofNat.inj ((len_int F.els).symm.trans F.card)

def nzL (z : R) : List R → List R
  | [] => []
  | a :: l => if a = z then nzL z l else a :: nzL z l

theorem mem_nzL (z s : R) : ∀ l : List R, memL s (nzL z l) → memL s l ∧ s ≠ z
  | [], h => False.elim h
  | a :: l, h => by
    unfold nzL at h
    by_cases e : a = z
    · rw [ite_pos' _ _ e] at h
      have := mem_nzL z s l h
      exact ⟨Or.inr this.1, this.2⟩
    · rw [ite_neg' _ _ e] at h
      cases h with
      | inl e' => exact ⟨Or.inl e', fun hz => e (e' ▸ hz)⟩
      | inr e' =>
        have := mem_nzL z s l e'
        exact ⟨Or.inr this.1, this.2⟩

theorem dist_nzL (z : R) : ∀ l : List R, RootBound.Dist l → RootBound.Dist (nzL z l)
  | [], _ => trivial
  | a :: l, hd => by
    unfold nzL
    by_cases e : a = z
    · rw [ite_pos' _ _ e]; exact dist_nzL z l hd.2
    · rw [ite_neg' _ _ e]
      exact ⟨fun s hs => hd.1 s (mem_nzL z s l hs).1, dist_nzL z l hd.2⟩

theorem len_nzL (z : R) : ∀ l : List R, (nzL z l).length + cnt z l = l.length
  | [] => rfl
  | a :: l => by
    unfold nzL cnt
    by_cases e : a = z
    · rw [ite_pos' _ _ e, ite_pos' _ _ e]
      show (nzL z l).length + (1 + cnt z l) = l.length + 1
      rw [← len_nzL z l]; lib_nat
    · rw [ite_neg' _ _ e, ite_neg' _ _ e]
      show (nzL z l).length + 1 + (0 + cnt z l) = l.length + 1
      rw [← len_nzL z l]; lib_nat

/-- **The nonzero elements**: a distinct list of length q − 1 = 2h. -/
theorem nz_list : RootBound.Dist (nzL F.K.zero F.els) ∧ (nzL F.K.zero F.els).length = 2 * F.h ∧
    (∀ s, memL s (nzL F.K.zero F.els) → s ≠ F.K.zero) := by
  have hc1 : cnt F.K.zero F.els = 1 := Int.ofNat.inj ((cnt_int F.K.zero F.els).symm.trans (F.comp F.K.zero))
  have hl := len_nzL F.K.zero F.els
  rw [hc1, len_els F] at hl
  exact ⟨dist_nzL _ _ (dist_of_complete _ F.comp), Nat.succ.inj hl, fun s hs => (mem_nzL _ s _ hs).2⟩

theorem mem_els (z : R) : memL z F.els := by
  have hc1 : cnt z F.els = 1 := Int.ofNat.inj ((cnt_int z F.els).symm.trans (F.comp z))
  have : ∀ l : List R, 1 ≤ cnt z l → memL z l := by
    intro l
    induction l with
    | nil => intro h; exact absurd h (Nat.not_succ_le_zero 0)
    | cons a l ih =>
      intro h
      by_cases e : a = z
      · exact Or.inl e.symm
      · right; apply ih
        have : cnt z (a :: l) = cnt z l := by show (if a = z then 1 else 0) + cnt z l = _; rw [ite_neg' _ _ e, Nat.zero_add]
        rw [← this]; exact h
  exact this _ (by rw [hc1]; exact Nat.le_refl 1)

end FinList

#print axioms FinList.cnt_int
#print axioms FinList.cnt_pos
#print axioms FinList.dist_of_le_one
#print axioms FinList.dist_of_complete
#print axioms FinList.len_int
#print axioms FinList.len_els
#print axioms FinList.mem_nzL
#print axioms FinList.dist_nzL
#print axioms FinList.len_nzL
#print axioms FinList.nz_list
#print axioms FinList.mem_els

lang/FinMoment.lean

lean_all/lang/FinMoment.lean

/-
  lang/FinMoment — T43, cycle 67: the second moment of Jacobsthal sums over any finite field (port of JacobMoment):
    Σ_a φ(a)² = q (q − 1)(1 + χ(−1)),  q = |F|.
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.FinConic
import lang.Thue

namespace FinMoment

open YMChain FiniteRecChain FinField FinConic

variable {R : Type} [DecidableEq R] (F : FF R)


def PP : Int := Int.ofNat (2 * F.h + 1)

theorem eq_of_sub_zero (a b : R) (h : F.K.add a (F.K.neg b) = F.K.zero) : a = b := by
  have := congrArg (fun z => F.K.add z b) h
  have e : F.K.add (F.K.add a (F.K.neg b)) b = a := by rw [F.K.add_assoc, DetChain.add_neg', DetChain.add_zero']
  rw [← e]
  exact this.trans (F.K.zero_add b)

theorem sum_one : sumK Z (fun _ => (1 : Int)) F.els = PP F := F.card

omit F in
theorem delta_one (y z : R) (h : y = z) : delta Z y z = (1 : Int) := ite_pos' _ _ h
omit F in
theorem delta_zero (y z : R) (h : y ≠ z) : delta Z y z = (0 : Int) := ite_neg' _ _ h

theorem sq_delta (z : R) :
    chi F (F.K.mul z z) = 1 + -1 * delta Z z F.K.zero := by
  by_cases hz : z = F.K.zero
  · rw [hz, DetChain.zero_mul', chi_zero, delta_one _ _ rfl]; rfl
  · rw [chi_sq F z hz, delta_zero _ _ hz]; rfl

theorem sum_delta_shift (b : R) : sumK Z (fun a => delta Z (F.K.add a b) F.K.zero) F.els = 1 := by
  rw [FinConic.shift_sum F (fun y => delta Z y F.K.zero) b]
  exact F.comp F.K.zero

/-- **Σ_a χ((a + b)(a + c)) = −1 + p δ(b, c).** -/
theorem inner (b c : R) :
    sumK Z (fun a => chi F (F.K.mul (F.K.add a b) (F.K.add a c))) F.els = -1 + PP F * delta Z b c := by
  by_cases hbc : b = c
  · rw [← hbc, delta_one _ _ rfl]
    rw [sumK_ext Z _ _ (fun a => sq_delta F (F.K.add a b))]
    show sumK Z (fun a => Z.add 1 (Z.mul (-1) (delta Z (F.K.add a b) F.K.zero))) F.els = _
    rw [sumK_add, ← mul_sumK, sum_delta_shift, sum_one]
    show PP F + -1 * 1 = -1 + PP F * 1
    lib_int
  · rw [FinConic.conic_sum F b c hbc, delta_zero _ _ hbc]
    lib_int

theorem pair (x y a : R) :
    chi F (F.K.mul x (F.K.add (F.K.mul x x) a)) * chi F (F.K.mul y (F.K.add (F.K.mul y y) a))
      = chi F (F.K.mul x y) * chi F (F.K.mul (F.K.add a (F.K.mul x x)) (F.K.add a (F.K.mul y y))) := by
  rw [← chi_mul F, ← chi_mul F]
  have e : F.K.mul (F.K.mul x (F.K.add (F.K.mul x x) a)) (F.K.mul y (F.K.add (F.K.mul y y) a))
      = F.K.mul (F.K.mul x y) (F.K.mul (F.K.add a (F.K.mul x x)) (F.K.add a (F.K.mul y y))) := by lib_ring
  rw [e]

theorem zsum {A : Type} : ∀ l : List A, sumK Z (fun _ => (0 : Int)) l = 0
  | [] => rfl
  | _ :: l => by
    show (0 : Int) + sumK Z (fun _ => (0 : Int)) l = 0
    rw [zsum l]; rfl

theorem compL : Complete Z F.els := F.comp

theorem two_ne (x : R)
    (hx : x ≠ F.K.zero) : x ≠ F.K.neg x := fun e => by
  have h2 : F.K.mul (F.K.add F.K.one F.K.one) x = F.K.zero := by
    have : F.K.mul (F.K.add F.K.one F.K.one) x = F.K.add x x := by lib_ring
    rw [this]; conv => lhs; rhs; rw [e]
    exact F.K.add_neg x
  cases F.nozd _ _ h2 with
  | inl h =>
    exact F.ne_pm (DetChain.neg_unique F.K _ _ h).symm
  | inr h => exact hx h

theorem roots_sq (x y : R)
    (hx : x ≠ F.K.zero) : delta Z (F.K.mul x x) (F.K.mul y y) = delta Z y x + delta Z y (F.K.neg x) := by
  have hxn := two_ne F x hx
  by_cases h1 : y = x
  · rw [h1, delta_one _ _ rfl, delta_one _ _ rfl, delta_zero _ _ hxn]; rfl
  by_cases h2 : y = F.K.neg x
  · rw [h2, Thue.negsq F.K, delta_one _ _ rfl, delta_one _ _ rfl, delta_zero _ _ (fun e => hxn e.symm)]; rfl
  · have hne : F.K.mul x x ≠ F.K.mul y y := fun e => by
      have f : F.K.mul (F.K.add y (F.K.neg x)) (F.K.add y x) = F.K.add (F.K.mul y y) (F.K.neg (F.K.mul x x)) := by lib_ring
      rw [← e, F.K.add_neg] at f
      cases F.nozd _ _ f with
      | inl h => exact h1 (eq_of_sub_zero F y x h)
      | inr h =>
        apply h2
        have := congrArg (fun z => F.K.add z (F.K.neg x)) h
        have e2 : F.K.add (F.K.add y x) (F.K.neg x) = y := by rw [F.K.add_assoc, F.K.add_neg, DetChain.add_zero']
        rw [← e2]
        exact this.trans (F.K.zero_add _)
    rw [delta_zero _ _ hne, delta_zero _ _ h1, delta_zero _ _ h2]; rfl

theorem chi_neg_sq (x : R)
    (hx : x ≠ F.K.zero) : chi F (F.K.mul x (F.K.neg x)) = chi F (F.K.neg F.K.one) := by
  have e : F.K.mul x (F.K.neg x) = F.K.mul (F.K.neg F.K.one) (F.K.mul x x) := by lib_ring
  rw [e, chi_mul F, chi_sq F x hx]; lib_int

theorem diag_x (x : R) :
    sumK Z (fun y => chi F (F.K.mul x y) * delta Z (F.K.mul x x) (F.K.mul y y)) F.els
      = (1 + chi F (F.K.neg F.K.one)) * (1 + -1 * delta Z x F.K.zero) := by
  by_cases hx : x = F.K.zero
  · rw [hx, delta_one _ _ rfl]
    have : sumK Z (fun y => chi F (F.K.mul F.K.zero y) * delta Z (F.K.mul F.K.zero F.K.zero) (F.K.mul y y)) F.els = 0 := by
      rw [sumK_ext Z _ (fun _ => (0 : Int)) (fun y => by rw [DetChain.zero_mul', chi_zero]; lib_int)]
      exact zsum F.els
    rw [this]; lib_int
  · rw [delta_zero _ _ hx]
    have pt : ∀ y, chi F (F.K.mul x y) * delta Z (F.K.mul x x) (F.K.mul y y)
        = Z.add (Z.mul (delta Z y x) (chi F (F.K.mul x y))) (Z.mul (delta Z y (F.K.neg x)) (chi F (F.K.mul x y))) := fun y => by
      rw [roots_sq F x y hx]
      show _ = delta Z y x * chi F _ + delta Z y (F.K.neg x) * chi F _
      lib_int
    rw [sumK_ext Z _ _ pt, sumK_add, pick Z F.els (F.comp) (fun y => chi F (F.K.mul x y)) x,
      pick Z F.els (F.comp) (fun y => chi F (F.K.mul x y)) (F.K.neg x), chi_sq F x hx,
      chi_neg_sq F x hx]
    show 1 + chi F (F.K.neg F.K.one) = _
    lib_int

theorem diag :
    sumK Z (fun x => sumK Z (fun y => chi F (F.K.mul x y) * delta Z (F.K.mul x x) (F.K.mul y y)) F.els) F.els
      = (1 + chi F (F.K.neg F.K.one)) * (PP F + -1) := by
  rw [sumK_ext Z _ _ (diag_x F)]
  show sumK Z (fun x => Z.mul (1 + chi F (F.K.neg F.K.one)) (Z.add 1 (Z.mul (-1) (delta Z x F.K.zero)))) F.els = _
  rw [← mul_sumK, sumK_add, ← mul_sumK, sum_one, show sumK Z (fun x => delta Z x F.K.zero) F.els = 1 from F.comp F.K.zero]
  show (1 + chi F (F.K.neg F.K.one)) * (PP F + -1 * 1) = _
  lib_int

theorem sum_chi_xy (x : R) :
    sumK Z (fun y => chi F (F.K.mul x y)) F.els = 0 := by
  rw [sumK_ext Z _ (fun y => Z.mul (chi F x) (chi F y)) (fun y => chi_mul F x y), ← mul_sumK,
    show sumK Z (chi F) F.els = 0 from chi_sum F]
  show chi F x * 0 = 0
  lib_int

/-- **Σ_a φ(a)² = p (p − 1)(1 + χ(−1)).** -/
theorem moment :
    sumK Z (fun a => phi F a * phi F a) F.els = PP F * ((1 + chi F (F.K.neg F.K.one)) * (PP F + -1)) := by
  -- expand the square
  have e1 : ∀ a, phi F a * phi F a = sumK Z (fun x => sumK Z (fun y =>
      chi F (F.K.mul x (F.K.add (F.K.mul x x) a)) * chi F (F.K.mul y (F.K.add (F.K.mul y y) a))) F.els) F.els := fun a => by
    show Z.mul (sumK Z _ F.els) (sumK Z _ F.els) = _
    rw [sumK_mul]
    exact sumK_ext Z _ _ (fun x => mul_sumK Z _ _ F.els) _
  rw [sumK_ext Z _ _ e1, sumK_swap]
  -- swap a and y for each x, then use pair and inner
  have e2 : ∀ x, sumK Z (fun a => sumK Z (fun y =>
      chi F (F.K.mul x (F.K.add (F.K.mul x x) a)) * chi F (F.K.mul y (F.K.add (F.K.mul y y) a))) F.els) F.els
      = sumK Z (fun y => Z.add (Z.mul (-1) (chi F (F.K.mul x y)))
          (Z.mul (PP F) (chi F (F.K.mul x y) * delta Z (F.K.mul x x) (F.K.mul y y)))) F.els := fun x => by
    rw [sumK_swap]
    apply sumK_ext
    intro y
    rw [sumK_ext Z _ (fun a => Z.mul (chi F (F.K.mul x y))
      (chi F (F.K.mul (F.K.add a (F.K.mul x x)) (F.K.add a (F.K.mul y y))))) (fun a => pair F x y a),
      ← mul_sumK, inner F]
    show chi F (F.K.mul x y) * (-1 + PP F * delta Z (F.K.mul x x) (F.K.mul y y)) = _
    show _ = -1 * chi F (F.K.mul x y) + PP F * (chi F (F.K.mul x y) * delta Z (F.K.mul x x) (F.K.mul y y))
    lib_int
  rw [sumK_ext Z _ _ e2]
  have e3 : ∀ x, sumK Z (fun y => Z.add (Z.mul (-1) (chi F (F.K.mul x y)))
      (Z.mul (PP F) (chi F (F.K.mul x y) * delta Z (F.K.mul x x) (F.K.mul y y)))) F.els
      = Z.mul (PP F) (sumK Z (fun y => chi F (F.K.mul x y) * delta Z (F.K.mul x x) (F.K.mul y y)) F.els) := fun x => by
    rw [sumK_add, ← mul_sumK, ← mul_sumK, sum_chi_xy F x]
    show -1 * 0 + _ = _
    lib_int
  rw [sumK_ext Z _ _ e3, ← mul_sumK, diag F]
  rfl

end FinMoment

#print axioms FinMoment.eq_of_sub_zero
#print axioms FinMoment.sum_one
#print axioms FinMoment.delta_one
#print axioms FinMoment.delta_zero
#print axioms FinMoment.sq_delta
#print axioms FinMoment.sum_delta_shift
#print axioms FinMoment.inner
#print axioms FinMoment.pair
#print axioms FinMoment.zsum
#print axioms FinMoment.compL
#print axioms FinMoment.two_ne
#print axioms FinMoment.roots_sq
#print axioms FinMoment.chi_neg_sq
#print axioms FinMoment.diag_x
#print axioms FinMoment.diag
#print axioms FinMoment.sum_chi_xy
#print axioms FinMoment.moment

lang/FinPairing.lean

lean_all/lang/FinPairing.lean

/-
  lang/FinPairing — T43, cycle 73: the involution pairing lemma over any finite field, ordered by an injective key.

  key : R → ℕ injective (for 𝔽_{p²}: key(a, b) = a·p + b). Then for an involution σ and σ-invariant F:
    Σ_x F(x) = Σ_x [σx = x] F(x) + 2 Σ_x [key x < key σx] F(x).
  Port of lang/Pairing to FinField.FF.

  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.FinClass

namespace FinPairing

open YMChain FiniteRecChain FinField

abbrev Z := LibInt.intR

variable {R : Type} [DecidableEq R] (key : R → Nat)

def ltI (x y : R) : Int := if key x < key y then 1 else 0
def eqI (x y : R) : Int := if x = y then 1 else 0

theorem lt_split (hkey : ∀ x y, key x = key y → x = y) (x y : R) : eqI y x + ltI key x y + ltI key y x = 1 := by
  unfold eqI ltI
  by_cases h : y = x
  · rw [ite_pos' _ _ h, h, ite_neg' _ _ (Nat.lt_irrefl _)]; rfl
  · rw [ite_neg' _ _ h]
    cases Nat.lt_or_ge (key x) (key y) with
    | inl hl => rw [ite_pos' _ _ hl, ite_neg' _ _ (Nat.not_lt_of_le (Nat.le_of_lt hl))]; rfl
    | inr hg =>
      have hl : key y < key x := Nat.lt_of_le_of_ne hg (fun e => h (hkey _ _ e))
      rw [ite_neg' _ _ (Nat.not_lt_of_le hg), ite_pos' _ _ hl]; rfl

theorem pairing (hkey : ∀ x y, key x = key y → x = y) (σ : R → R) (hσ : ∀ x, σ (σ x) = x) (F : R → Int) (hF : ∀ x, F (σ x) = F x)
    (l : List R) (hl : Complete Z l) :
    sumK Z F l = sumK Z (fun x => eqI (σ x) x * F x) l + 2 * sumK Z (fun x => ltI key x (σ x) * F x) l := by
  have pt : ∀ x, F x = Z.add (Z.add (eqI (σ x) x * F x) (ltI key x (σ x) * F x)) (ltI key (σ x) x * F x) := fun x => by
    have := lt_split key hkey x (σ x)
    show F x = eqI (σ x) x * F x + ltI key x (σ x) * F x + ltI key (σ x) x * F x
    calc F x = (eqI (σ x) x + ltI key x (σ x) + ltI key (σ x) x) * F x := by rw [this]; lib_int
      _ = _ := by lib_int
  rw [sumK_ext Z _ _ pt, sumK_add, sumK_add]
  have hr := reindex Z l hl σ σ hσ hσ (fun x => ltI key x (σ x) * F x)
  have e : sumK Z (fun x => ltI key (σ x) x * F x) l = sumK Z (fun x => ltI key x (σ x) * F x) l := by
    rw [← hr]
    exact sumK_ext Z _ _ (fun x => by show _ = ltI key (σ x) (σ (σ x)) * F (σ x); rw [hσ, hF]) _
  rw [e]
  show sumK Z _ l + sumK Z _ l + sumK Z _ l = _
  lib_int

end FinPairing

#print axioms FinPairing.lt_split
#print axioms FinPairing.pairing

lang/FinPow.lean

lean_all/lang/FinPow.lean

/-
  lang/FinPow — T43, cycle 172: a(𝔽_q) = 2 Re π^N = α^N + β^N for y² = x³ − x over every finite field of order q = p^N
  containing i, at every split prime p = 4k + 5.

  Hypotheses on the field F (any FF): characteristic p (p·1 = 0), ℤ → F has kernel pℤ, h = (q − 1)/2 = rep (2k + 2) N, an
  injective key and i² = −1. Then
    rep_two, rep_pow       2·rep(k+1) = rep(2k+2);  2 rep(2k+2) N + 1 = p^N
    castN_nsm, pw_ne_zero   ℤ → F on naturals; x ≠ 0 ⇒ x^n ≠ 0
    fin_pow                −Σ_F χ(z³ − z) = 2 Re π^N, π = a + bi primary of norm p, a_p = 2a
  Proof: FinHasse + LucasIter give S_F ≡ ±C(2k+2, k+1)^N (mod p), nonzero since p ∤ a_p; FinPrimary gives S_F = −2A with
  A + Bi primary of norm q; GaussPow gives A = Re π^N.

  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.GaussPow
import lang.LucasIter
import lang.CubeAp

namespace FinPow

open FTAChain ZMod YMChain FiniteRecChain Frobenius GaussInt GaussFactor LucasIter
open LibRing (castZ castN)

theorem rep_two (P c : Nat) : ∀ N, 2 * rep P c N = rep P (2 * c) N
  | 0 => rfl
  | N + 1 => by
    show 2 * (c + rep P c N * P) = 2 * c + rep P (2 * c) N * P
    rw [← rep_two P c N]; lib_nat

theorem rep_pow (k : Nat) : ∀ N, Int.ofNat (2 * rep (4 * k + 5) (2 * k + 2) N + 1) = ipow (Int.ofNat (4 * k + 5)) N
  | 0 => rfl
  | N + 1 => by
    show Int.ofNat (2 * ((2 * k + 2) + rep (4 * k + 5) (2 * k + 2) N * (4 * k + 5)) + 1) = ipow (Int.ofNat (4 * k + 5)) N * Int.ofNat (4 * k + 5)
    rw [← rep_pow k N]
    show Int.ofNat (2 * ((2 * k + 2) + rep (4 * k + 5) (2 * k + 2) N * (4 * k + 5)) + 1)
      = Int.ofNat ((2 * rep (4 * k + 5) (2 * k + 2) N + 1) * (4 * k + 5))
    exact congrArg Int.ofNat (by lib_nat)

theorem castN_nsm {R : Type} (K : DetChain.CRing R) : ∀ n, castN K n = nsm K n K.one
  | 0 => rfl
  | n + 1 => by
    show K.add K.one (castN K n) = K.add (nsm K n K.one) K.one
    rw [castN_nsm K n, K.add_comm]

theorem pw_ne_zero {R : Type} (K : DetChain.CRing R) (hK : DetChain.NoZD K) (x : R) (hx : x ≠ K.zero) : ∀ n, pw K x n ≠ K.zero
  | 0 => fun e => by
    have h1 : K.one = K.zero := e
    exact hx (by rw [← K.one_mul x, h1, DetChain.zero_mul'])
  | n + 1 => fun e => by
    cases hK _ _ e with
    | inl h => exact pw_ne_zero K hK x hx n h
    | inr h => exact hx h

theorem hr (k N : Nat) : rep (2 * (2 * k + 1) + 1 + 1 + 1) (2 * k + 2) N = 2 * rep (2 * (2 * k + 1) + 1 + 1 + 1) (k + 1) N := by
  rw [rep_two, show 2 * (k + 1) = 2 * k + 2 by lib_nat]

theorem P_eq (k : Nat) : (2 * (2 * k + 1) + 1 + 1 + 1) = 4 * k + 5 := by lib_nat

variable {R : Type} [DecidableEq R]

/-- **a(F) = 2 Re π^(N+1)** for y² = x³ − x over every F of order p^(N+1) ∋ i (p = 4k + 5 split). -/
theorem fin_pow (k : Nat) (hpr : ∀ d, Dv d (2 * (2 * k + 1) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * k + 1) + 1 + 1 + 1)) (F : FinField.FF R)
    (hchar : nsm F.K (2 * (2 * k + 1) + 1 + 1 + 1) F.K.one = F.K.zero)
    (hcast : ∀ z : Int, castZ F.K z = F.K.zero → ∃ w, z = Int.ofNat (2 * (2 * k + 1) + 1 + 1 + 1) * w)
    (N : Nat) (hh : F.h = rep (2 * (2 * k + 1) + 1 + 1 + 1) (2 * k + 2) (N + 1))
    (key : R → Nat) (hkey : ∀ x y, key x = key y → x = y) (i : R) (hi : F.K.mul i i = F.K.neg F.K.one) :
    ∃ a b : Int, a * a + b * b = Int.ofNat (2 * (2 * k + 1) + 1 + 1 + 1) ∧ Primary ⟨a, b⟩ ∧ -(sumK LibInt.intR (fun x => QChar.chi (2 * k + 1) (CMInert.cub (ring (2 * (2 * k + 1) + 1 + 1)) x)) (ConicSum.L (2 * k + 1))) = 2 * a ∧
      -(sumK LibInt.intR (fun x => FinField.chi F (FinSign.f F x)) F.els) = 2 * (gpow ⟨a, b⟩ (N + 1)).re := by
  match SignCM3.ap_primary k hpr, FinPrimary.fin_ap_primary F key hkey i hi with
  | ⟨a, b, hab, hPa, hS1⟩, ⟨A, B, hAB, hPA, hSF⟩ =>
    refine ⟨a, b, hab, hPa, hS1, ?_⟩
    -- the Hasse congruence over F
    have hK : F.h = 2 * (k + rep (2 * (2 * k + 1) + 1 + 1 + 1) (k + 1) N * (2 * (2 * k + 1) + 1 + 1 + 1)) + 2 := by
      rw [hh]; show (2 * k + 2) + rep (2 * (2 * k + 1) + 1 + 1 + 1) (2 * k + 2) N * (2 * (2 * k + 1) + 1 + 1 + 1) = _; rw [hr]; lib_nat
    have hc1 : castZ F.K (sumK LibInt.intR (fun x => FinField.chi F (FinSign.f F x)) F.els) = F.K.neg (nsm F.K (C (rep (2 * (2 * k + 1) + 1 + 1 + 1) (2 * k + 2) (N + 1)) (rep (2 * (2 * k + 1) + 1 + 1 + 1) (k + 1) (N + 1)))
        (pw F.K (F.K.neg F.K.one) (k + rep (2 * (2 * k + 1) + 1 + 1 + 1) (k + 1) N * (2 * (2 * k + 1) + 1 + 1 + 1) + 1))) := by
      rw [FinCast.cast_sum_ff, hK]
      refine (FinHasse.fin_hasse F _ hK).trans ?_
      rw [show 2 * (k + rep (2 * (2 * k + 1) + 1 + 1 + 1) (k + 1) N * (2 * (2 * k + 1) + 1 + 1 + 1)) + 2 = rep (2 * (2 * k + 1) + 1 + 1 + 1) (2 * k + 2) (N + 1) by
        show _ = (2 * k + 2) + rep (2 * (2 * k + 1) + 1 + 1 + 1) (2 * k + 2) N * (2 * (2 * k + 1) + 1 + 1 + 1); rw [hr]; lib_nat]
      rw [show k + rep (2 * (2 * k + 1) + 1 + 1 + 1) (k + 1) N * (2 * (2 * k + 1) + 1 + 1 + 1) + 1 = rep (2 * (2 * k + 1) + 1 + 1 + 1) (k + 1) (N + 1) by
        show _ = (k + 1) + rep (2 * (2 * k + 1) + 1 + 1 + 1) (k + 1) N * (2 * (2 * k + 1) + 1 + 1 + 1); lib_nat]
    rw [Frobenius.nsm_eq_mul, LucasIter.lucas_rep F.K (2 * (2 * k + 1) + 1) hpr hchar (2 * k + 2) (k + 1)
      (SplitHasse.lt_p k) (SplitHasse.lt_p' k) (N + 1)] at hc1
    -- the base congruence: C(2k+2, k+1) ≢ 0 mod p
    have hX : nsm F.K (Frobenius.C (2 * k + 2) (k + 1)) F.K.one ≠ F.K.zero := fun e => by
      have e1 : castZ F.K (Int.ofNat (Frobenius.C (2 * k + 2) (k + 1))) = F.K.zero := by
        show castN F.K _ = _; rw [castN_nsm]; exact e
      match hcast _ e1 with
      | ⟨w, hw⟩ =>
        have e2 : nsm (ring (2 * (2 * k + 1) + 1 + 1)) (Frobenius.C (2 * k + 2) (k + 1)) (ring (2 * (2 * k + 1) + 1 + 1)).one = (ring (2 * (2 * k + 1) + 1 + 1)).zero := by
          rw [← castN_nsm]; show castZ (ring (2 * (2 * k + 1) + 1 + 1)) (Int.ofNat (Frobenius.C (2 * k + 2) (k + 1))) = _
          rw [hw, HasseDavenport.castZ_mul, CubeAp.castP, DetChain.zero_mul']
        have e3 := SplitHasse.cast_S1 k hpr
        rw [Frobenius.nsm_eq_mul, e2, DetChain.zero_mul'] at e3
        have e4 : castZ (ring (2 * (2 * k + 1) + 1 + 1)) (sumK LibInt.intR (fun x => QChar.chi (2 * k + 1) (CMInert.cub (ring (2 * (2 * k + 1) + 1 + 1)) x)) (ConicSum.L (2 * k + 1))) = (ring (2 * (2 * k + 1) + 1 + 1)).zero := e3.trans (DetChain.neg_unique (ring (2 * (2 * k + 1) + 1 + 1)) _ _ ((ring (2 * (2 * k + 1) + 1 + 1)).zero_add _)).symm
        generalize (sumK LibInt.intR (fun x => QChar.chi (2 * k + 1) (CMInert.cub (ring (2 * (2 * k + 1) + 1 + 1)) x)) (ConicSum.L (2 * k + 1))) = s1 at e4 hS1
        have ha : castZ (ring (2 * (2 * k + 1) + 1 + 1)) a = (ring (2 * (2 * k + 1) + 1 + 1)).zero := by
          rw [show s1 = 2 * -a by rw [show s1 = -(-s1) by lib_int, hS1]; lib_int, HasseDavenport.castZ_mul, CubeAp.castTwo,
            HasseDavenport.castZ_neg] at e4
          cases ZMod.nozd (2 * (2 * k + 1) + 1) hpr _ _ e4 with
          | inl h => exact absurd h (Eisen.two_ne_zero' (2 * (2 * k + 1) + 1) (Nat.succ_le_succ (Nat.succ_le_succ (Nat.zero_le _))))
          | inr h =>
            rw [← DetChain.neg_neg' (ring (2 * (2 * k + 1) + 1 + 1)) (castZ (ring (2 * (2 * k + 1) + 1 + 1)) a), h]
            exact (DetChain.neg_unique (ring (2 * (2 * k + 1) + 1 + 1)) _ _ ((ring (2 * (2 * k + 1) + 1 + 1)).zero_add _)).symm
        match HasseDavenport.castZ_zero_dvd _ a ha, hPa with
        | ⟨q, hq⟩, ⟨⟨e, he⟩, ⟨t, ht⟩⟩ =>
          have q0 : q = 0 := HasseDavenport.sq_zero q (CubeAp.small (2 * (2 * k + 1) + 1) a b q hab hq)
          rw [q0] at hq
          have he' : b = 2 * e := he
          have ht' : a + b - 1 = 4 * t := ht
          exact GaussInt.two_ne_one (e - 2 * t) (by rw [show 2 * (e - 2 * t) = 2 * e - 4 * t by lib_int, ← he', ← ht', hq]; lib_int)
    -- p ∤ A
    have hA : ¬ ∃ w, A = Int.ofNat (2 * (2 * k + 1) + 1 + 1 + 1) * w := fun hex => match hex with
      | ⟨w, hw⟩ => by
        have z0 : castZ F.K (sumK LibInt.intR (fun x => FinField.chi F (FinSign.f F x)) F.els) = F.K.zero := by
          rw [show (sumK LibInt.intR (fun x => FinField.chi F (FinSign.f F x)) F.els) = Int.ofNat (2 * (2 * k + 1) + 1 + 1 + 1) * (-2 * w) by rw [show (sumK LibInt.intR (fun x => FinField.chi F (FinSign.f F x)) F.els) = -(-(sumK LibInt.intR (fun x => FinField.chi F (FinSign.f F x)) F.els)) by lib_int, hSF, hw]; lib_int,
            HasseDavenport.castZ_mul, show castZ F.K (Int.ofNat (2 * (2 * k + 1) + 1 + 1 + 1)) = F.K.zero by
              show castN F.K _ = _; rw [castN_nsm]; exact hchar, DetChain.zero_mul']
        rw [z0] at hc1
        have hs : F.K.mul (pw F.K (F.K.neg F.K.one) (k + rep (2 * (2 * k + 1) + 1 + 1 + 1) (k + 1) N * (2 * (2 * k + 1) + 1 + 1 + 1) + 1)) (pw F.K (F.K.neg F.K.one) (k + rep (2 * (2 * k + 1) + 1 + 1 + 1) (k + 1) N * (2 * (2 * k + 1) + 1 + 1 + 1) + 1)) = F.K.one := by
          rw [← Frobenius.pw_mul, show F.K.mul (F.K.neg F.K.one) (F.K.neg F.K.one) = F.K.one by lib_ring, TwoPower.pwone]
        generalize pw F.K (F.K.neg F.K.one) (k + rep (2 * (2 * k + 1) + 1 + 1 + 1) (k + 1) N * (2 * (2 * k + 1) + 1 + 1 + 1) + 1) = s at hc1 hs
        have hY : pw F.K (nsm F.K (Frobenius.C (2 * k + 2) (k + 1)) F.K.one) (N + 1) = F.K.zero := by
          have h0 : F.K.mul (pw F.K (nsm F.K (Frobenius.C (2 * k + 2) (k + 1)) F.K.one) (N + 1)) s = F.K.zero := by
            rw [← DetChain.neg_neg' F.K (F.K.mul _ s), ← hc1]; exact (DetChain.neg_unique F.K _ _ (F.K.zero_add _)).symm
          generalize Frobenius.pw F.K (nsm F.K (C (2 * k + 2) (k + 1)) F.K.one) (N + 1) = Y at h0 ⊢
          calc Y = F.K.mul (F.K.mul Y s) s := by rw [F.K.mul_assoc, hs, F.K.mul_comm, F.K.one_mul]
            _ = F.K.zero := by rw [h0, DetChain.zero_mul']
        exact pw_ne_zero F.K F.nozd _ hX (N + 1) hY
    have hN : gnorm ⟨A, B⟩ = ipow (Int.ofNat (2 * (2 * k + 1) + 1 + 1 + 1)) (N + 1) := by
      show A * A + B * B = _
      rw [hAB, hh, P_eq k]; exact rep_pow k (N + 1)
    have hre : A = (gpow ⟨a, b⟩ (N + 1)).re := GaussPow.pow_primary (2 * (2 * k + 1) + 1) hpr a b hab hPa (N + 1) ⟨A, B⟩ hPA hN hA
    rw [hSF, hre]

end FinPow

#print axioms FinPow.rep_two
#print axioms FinPow.rep_pow
#print axioms FinPow.castN_nsm
#print axioms FinPow.pw_ne_zero
#print axioms FinPow.hr
#print axioms FinPow.P_eq
#print axioms FinPow.fin_pow

lang/FinPow2.lean

lean_all/lang/FinPow2.lean

/-
  lang/FinPow2 — T43, cycle 173: FinPow with |F| = p^N as hypothesis, and the quadratic step keeps all hypotheses.

    fin_pow'      as FinPow.fin_pow with 2h + 1 = p^(N+1) in place of h = rep (2k+2) (N+1)
    nsm_quadG     n·(a, b) = (n·a, n·b) in F[√n]
    quad_char, quad_cast, quad_size     F[√n] has characteristic p, ker(ℤ → F[√n]) = pℤ, |F[√n]| = |F|²
  So the tower 𝔽_p ⊂ 𝔽_{p²} ⊂ 𝔽_{p⁴} ⊂ … satisfies fin_pow' at every level.

  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.FinPow
import lang.FieldTower
import lang.CubeHasse3

namespace FinPow2

open FTAChain ZMod YMChain FiniteRecChain Frobenius GaussInt GaussFactor LucasIter
open LibRing (castZ castN)

theorem ipow_add (P : Int) (a : Nat) : ∀ b, ipow P (a + b) = ipow P a * ipow P b
  | 0 => by show ipow P a = ipow P a * 1; lib_int
  | b + 1 => by
    show ipow P (a + b) * P = ipow P a * (ipow P b * P)
    rw [ipow_add P a b]; lib_int

variable {R : Type} [DecidableEq R]

theorem fin_pow' (k : Nat) (hpr : ∀ d, Dv d (2 * (2 * k + 1) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * k + 1) + 1 + 1 + 1)) (F : FinField.FF R)
    (hchar : nsm F.K (2 * (2 * k + 1) + 1 + 1 + 1) F.K.one = F.K.zero)
    (hcast : ∀ z : Int, castZ F.K z = F.K.zero → ∃ w, z = Int.ofNat (2 * (2 * k + 1) + 1 + 1 + 1) * w)
    (N : Nat) (hq : Int.ofNat (2 * F.h + 1) = ipow (Int.ofNat (2 * (2 * k + 1) + 1 + 1 + 1)) (N + 1))
    (key : R → Nat) (hkey : ∀ x y, key x = key y → x = y) (i : R) (hi : F.K.mul i i = F.K.neg F.K.one) :
    ∃ a b : Int, a * a + b * b = Int.ofNat (2 * (2 * k + 1) + 1 + 1 + 1) ∧ Primary ⟨a, b⟩ ∧ -(sumK LibInt.intR (fun x => QChar.chi (2 * k + 1) (CMInert.cub (ring (2 * (2 * k + 1) + 1 + 1)) x)) (ConicSum.L (2 * k + 1))) = 2 * a ∧
      -(sumK LibInt.intR (fun x => FinField.chi F (FinSign.f F x)) F.els) = 2 * (gpow ⟨a, b⟩ (N + 1)).re := by
  have hh : F.h = rep (2 * (2 * k + 1) + 1 + 1 + 1) (2 * k + 2) (N + 1) := by
    apply CubeHasse3.two_cancel
    apply Int.ofNat.inj
    rw [hq, FinPow.P_eq k, ← FinPow.rep_pow k (N + 1)]
  exact FinPow.fin_pow k hpr F hchar hcast N hh key hkey i hi

section Step
variable {S : Type} (K : DetChain.CRing S) (n : S)

theorem nsm_quadG (a b : S) : ∀ m, nsm (Quad.quad K n) m (a, b) = (nsm K m a, nsm K m b)
  | 0 => rfl
  | m + 1 => by
    show (Quad.quad K n).add (nsm (Quad.quad K n) m (a, b)) (a, b) = _
    rw [nsm_quadG a b m]; rfl

theorem nsm_zeroG : ∀ m, nsm K m K.zero = K.zero
  | 0 => rfl
  | m + 1 => by show K.add (nsm K m K.zero) K.zero = K.zero; rw [nsm_zeroG m]; exact K.zero_add _

end Step

theorem quad_char (F : FinField.FF R) (n : R) (j : Nat) (hj : F.h = j + 1) (hn0 : n ≠ F.K.zero)
    (hnr : Frobenius.pw F.K n F.h ≠ F.K.one) (P : Nat) (hchar : nsm F.K P F.K.one = F.K.zero) :
    nsm (QuadExt.quadExt F n j hj hn0 hnr).K P (QuadExt.quadExt F n j hj hn0 hnr).K.one = (QuadExt.quadExt F n j hj hn0 hnr).K.zero := by
  show nsm (Quad.quad F.K n) P (F.K.one, F.K.zero) = (F.K.zero, F.K.zero)
  rw [nsm_quadG, hchar, nsm_zeroG]

theorem quad_cast (F : FinField.FF R) (n : R) (j : Nat) (hj : F.h = j + 1) (hn0 : n ≠ F.K.zero)
    (hnr : Frobenius.pw F.K n F.h ≠ F.K.one) (P : Nat)
    (hcast : ∀ z : Int, castZ F.K z = F.K.zero → ∃ w, z = Int.ofNat P * w) :
    ∀ z : Int, castZ (QuadExt.quadExt F n j hj hn0 hnr).K z = (QuadExt.quadExt F n j hj hn0 hnr).K.zero → ∃ w, z = Int.ofNat P * w :=
  fun z h => hcast z (by
    have h1 := congrArg Prod.fst h
    rw [show castZ (QuadExt.quadExt F n j hj hn0 hnr).K z = castZ (Quad.quad F.K n) z from rfl, FinCast.castZ_quad] at h1
    exact h1)

theorem quad_size (F : FinField.FF R) (n : R) (j : Nat) (hj : F.h = j + 1) (hn0 : n ≠ F.K.zero)
    (hnr : Frobenius.pw F.K n F.h ≠ F.K.one) (P : Int) (N : Nat) (hq : Int.ofNat (2 * F.h + 1) = ipow P N) :
    Int.ofNat (2 * (QuadExt.quadExt F n j hj hn0 hnr).h + 1) = ipow P (N + N) := by
  rw [ipow_add, ← hq, hj]
  show Int.ofNat (2 * ((2 * j * j + 6 * j + 3) + 1) + 1) = Int.ofNat ((2 * (j + 1) + 1) * (2 * (j + 1) + 1))
  rw [FieldTower.order_sq]

end FinPow2

#print axioms FinPow2.ipow_add
#print axioms FinPow2.fin_pow'
#print axioms FinPow2.nsm_quadG
#print axioms FinPow2.nsm_zeroG
#print axioms FinPow2.quad_char
#print axioms FinPow2.quad_cast
#print axioms FinPow2.quad_size

lang/FinPow3.lean

lean_all/lang/FinPow3.lean

/-
  lang/FinPow3 — T43, cycle 176: a(𝔽_q) = 2 Re π^N = α^N + β^N for y² = x³ + 1 over every finite field of order q = p^N,
  p = 6t + 7.

  Hypotheses on F: characteristic p, ker(ℤ → F) = pℤ, 2h + 1 = p^(N+1).
    rep_mul, rep_pow_gen     rep (c·a) = c·rep a;  2a + 1 = P ⇒ 2 rep a n + 1 = P^n
    fin_pow3                 ψ_F(1) = −2 Re π^(N+1), π = x + y√−3, x ≡ 1 (3), ψ_p(1) = −2x
  Proof: FinHasse3 + LucasIter: ψ_F ≡ −C(3t+3, 2t+2)^(N+1) ≢ 0 (mod p); FinCubicExact + mod 3: ψ_F = −2Z, Z ≡ 1 (3),
  Z² + 3W² = p^(N+1); EisenPow: Z = Re π^(N+1).

  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.FinPow2
import lang.EisenPow
import lang.CubeAp3

namespace FinPow3

open FTAChain ZMod YMChain FiniteRecChain Frobenius GaussInt GaussFactor EisenFactor LucasIter
open LibRing (castZ castN)

theorem rep_mul (P c a : Nat) : ∀ n, c * rep P a n = rep P (c * a) n
  | 0 => rfl
  | n + 1 => by
    show c * (a + rep P a n * P) = c * a + rep P (c * a) n * P
    rw [← rep_mul P c a n]; lib_nat

theorem rep_pow_gen (P a : Nat) (h : 2 * a + 1 = P) : ∀ n, Int.ofNat (2 * rep P a n + 1) = ipow (Int.ofNat P) n
  | 0 => rfl
  | n + 1 => by
    show Int.ofNat (2 * (a + rep P a n * P) + 1) = ipow (Int.ofNat P) n * Int.ofNat P
    rw [← rep_pow_gen P a h n]
    show _ = Int.ofNat ((2 * rep P a n + 1) * P)
    exact congrArg Int.ofNat (by rw [show 2 * (a + rep P a n * P) + 1 = (2 * a + 1) + 2 * rep P a n * P by lib_nat, h]; lib_nat)

theorem rep3 (P t : Nat) (n : Nat) : rep P (3 * t + 3) n = 3 * rep P (t + 1) n := by
  rw [rep_mul, show 3 * (t + 1) = 3 * t + 3 by lib_nat]

theorem rep2 (P t : Nat) (n : Nat) : rep P (2 * t + 2) n = 2 * rep P (t + 1) n := by
  rw [rep_mul, show 2 * (t + 1) = 2 * t + 2 by lib_nat]

variable {R : Type} [DecidableEq R]

theorem fin_pow3 (t : Nat) (hpr : ∀ d, Dv d (2 * (3 * t + 2) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (3 * t + 2) + 1 + 1 + 1)) (F : FinField.FF R)
    (hchar : nsm F.K (2 * (3 * t + 2) + 1 + 1 + 1) F.K.one = F.K.zero)
    (hcast : ∀ z : Int, castZ F.K z = F.K.zero → ∃ w, z = Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) * w)
    (N : Nat) (hq : Int.ofNat (2 * F.h + 1) = ipow (Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1)) (N + 1)) :
    ∃ x y : Int, x * x + 3 * (y * y) = Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) ∧ (∃ s, x = 3 * s + 1) ∧ (FinCubic.psi (FinField.ffp (3 * t + 2) hpr) (FinField.ffp (3 * t + 2) hpr).K.one) = -2 * x ∧
      (FinCubic.psi F F.K.one) = -2 * (epow ⟨x, y⟩ (N + 1)).re := by
  have hN1 : 2 * ((3 * t + 2) + 1) = 3 * (2 * t + 2) := by lib_nat
  have h3 := EisenAp.p_ne_three (3 * t + 2) (2 * t + 2) hN1
  have hh : F.h = rep (2 * (3 * t + 2) + 1 + 1 + 1) (3 * t + 3) (N + 1) := by
    apply CubeHasse3.two_cancel
    apply Int.ofNat.inj
    rw [hq, ← rep_pow_gen (2 * (3 * t + 2) + 1 + 1 + 1) (3 * t + 3) (by lib_nat) (N + 1)]
  have hN3 : 2 * F.h = 3 * (2 * rep (2 * (3 * t + 2) + 1 + 1 + 1) (t + 1) (N + 1)) := by rw [hh, rep3]; lib_nat
  match EisenAp.eisen_exists (3 * t + 2) hpr (2 * t + 2) hN1,
    FinCubicExact.fin_cubic_trace F _ hN3, FinCubicTrace.psi_one_mod3_ff F _ hN3 with
  | ⟨x, y, hxy, hx, hS1⟩, ⟨U, W, hUW, hpsi⟩, ⟨T, hT⟩ =>
    refine ⟨x, y, hxy, hx, hS1, ?_⟩
    -- Hasse over F
    have hk : F.h = 3 * (t + rep (2 * (3 * t + 2) + 1 + 1 + 1) (t + 1) N * (2 * (3 * t + 2) + 1 + 1 + 1)) + 3 := by
      rw [hh]; show (3 * t + 3) + rep (2 * (3 * t + 2) + 1 + 1 + 1) (3 * t + 3) N * (2 * (3 * t + 2) + 1 + 1 + 1) = _; rw [rep3]; lib_nat
    have e1 : castZ F.K (FinCubic.psi F F.K.one) = F.K.neg (nsm F.K (Frobenius.C (3 * (t + rep (2 * (3 * t + 2) + 1 + 1 + 1) (t + 1) N * (2 * (3 * t + 2) + 1 + 1 + 1)) + 3) (2 * (t + rep (2 * (3 * t + 2) + 1 + 1 + 1) (t + 1) N * (2 * (3 * t + 2) + 1 + 1 + 1)) + 2)) F.K.one) := by
      show castZ F.K (sumK LibInt.intR (fun x => FinField.chi F (F.K.add (FinCubic.cube F x) F.K.one)) F.els) = _
      rw [FinCast.cast_sum_ff, hk]
      rw [sumK_ext F.K _ (fun x => Frobenius.pw F.K (F.K.add (Frobenius.pw F.K x 3) F.K.one) (3 * (t + rep (2 * (3 * t + 2) + 1 + 1 + 1) (t + 1) N * (2 * (3 * t + 2) + 1 + 1 + 1)) + 3)) (fun x => by
        rw [show FinCubic.cube F x = Frobenius.pw F.K x 3 by
          show F.K.mul x (F.K.mul x x) = F.K.mul (F.K.mul (F.K.mul F.K.one x) x) x; lib_ring])]
      exact FinHasse3.fin_hasse3 F (t + rep (2 * (3 * t + 2) + 1 + 1 + 1) (t + 1) N * (2 * (3 * t + 2) + 1 + 1 + 1)) hk
    rw [show 3 * (t + rep (2 * (3 * t + 2) + 1 + 1 + 1) (t + 1) N * (2 * (3 * t + 2) + 1 + 1 + 1)) + 3 = rep (2 * (3 * t + 2) + 1 + 1 + 1) (3 * t + 3) (N + 1) by
        show _ = (3 * t + 3) + rep (2 * (3 * t + 2) + 1 + 1 + 1) (3 * t + 3) N * (2 * (3 * t + 2) + 1 + 1 + 1); rw [rep3]; lib_nat,
      show 2 * (t + rep (2 * (3 * t + 2) + 1 + 1 + 1) (t + 1) N * (2 * (3 * t + 2) + 1 + 1 + 1)) + 2 = rep (2 * (3 * t + 2) + 1 + 1 + 1) (2 * t + 2) (N + 1) by
        show _ = (2 * t + 2) + rep (2 * (3 * t + 2) + 1 + 1 + 1) (2 * t + 2) N * (2 * (3 * t + 2) + 1 + 1 + 1); rw [rep2]; lib_nat,
      lucas_rep F.K (2 * (3 * t + 2) + 1) hpr hchar (3 * t + 3) (2 * t + 2) (SplitHasse3.lt3 t) (SplitHasse3.lt3' t) (N + 1)] at e1
    have hcP : castZ F.K (Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1)) = F.K.zero := by
      show castN F.K _ = _; rw [FinPow.castN_nsm]; exact hchar
    have hX : nsm F.K (Frobenius.C (3 * t + 3) (2 * t + 2)) F.K.one ≠ F.K.zero := fun e => by
      have e1' : castZ F.K (Int.ofNat (Frobenius.C (3 * t + 3) (2 * t + 2))) = F.K.zero := by
        show castN F.K _ = _; rw [FinPow.castN_nsm]; exact e
      match hcast _ e1' with
      | ⟨w, hw⟩ =>
        have e2 : nsm (ring (2 * (3 * t + 2) + 1 + 1)) (Frobenius.C (3 * t + 3) (2 * t + 2)) (ring (2 * (3 * t + 2) + 1 + 1)).one = (ring (2 * (3 * t + 2) + 1 + 1)).zero := by
          rw [← FinPow.castN_nsm]; show castZ (ring (2 * (3 * t + 2) + 1 + 1)) (Int.ofNat (Frobenius.C (3 * t + 3) (2 * t + 2))) = _
          rw [hw, HasseDavenport.castZ_mul, CubeAp.castP, DetChain.zero_mul']
        have e3 := SplitHasse3.cast_S1_3 t hpr
        rw [e2, ← CubeAp3.psi_same (3 * t + 2) hpr, hS1] at e3
        have e4 : castZ (ring (2 * (3 * t + 2) + 1 + 1)) (-2 * x) = (ring (2 * (3 * t + 2) + 1 + 1)).zero := e3.trans (DetChain.neg_unique (ring (2 * (3 * t + 2) + 1 + 1)) _ _ ((ring (2 * (3 * t + 2) + 1 + 1)).zero_add _)).symm
        have hx0 : castZ (ring (2 * (3 * t + 2) + 1 + 1)) x = (ring (2 * (3 * t + 2) + 1 + 1)).zero := by
          rw [show -2 * x = 2 * -x by lib_int, HasseDavenport.castZ_mul, CubeAp.castTwo, HasseDavenport.castZ_neg] at e4
          cases ZMod.nozd (2 * (3 * t + 2) + 1) hpr _ _ e4 with
          | inl h => exact absurd h (Eisen.two_ne_zero' (2 * (3 * t + 2) + 1) (Nat.succ_le_succ (Nat.succ_le_succ (Nat.zero_le _))))
          | inr h =>
            rw [← DetChain.neg_neg' (ring (2 * (3 * t + 2) + 1 + 1)) (castZ (ring (2 * (3 * t + 2) + 1 + 1)) x), h]
            exact (DetChain.neg_unique (ring (2 * (3 * t + 2) + 1 + 1)) _ _ ((ring (2 * (3 * t + 2) + 1 + 1)).zero_add _)).symm
        match HasseDavenport.castZ_zero_dvd _ x hx0, hx with
        | ⟨q, hq⟩, ⟨s, hs⟩ =>
          have q0 : q = 0 := HasseDavenport.sq_zero q (CubeAp3.small3 (2 * (3 * t + 2) + 1) x y q hxy hq)
          rw [q0] at hq
          exact EisenAp.three_ne_r (-s) 1 (fun e => Nat.noConfusion e) (Nat.lt_succ_of_lt (Nat.lt_succ_self 1))
            (by show _ = 1; rw [show 3 * -s = -(3 * s + 1) + 1 by lib_int, ← hs, hq]; lib_int)
    generalize (FinCubic.psi F F.K.one) = sF at e1 hpsi hT ⊢
    have hZ : ∃ Z : Int, sF = -2 * Z ∧ Z * Z = Int.ofNat U * Int.ofNat U := by
      cases hpsi with
      | inl h => exact ⟨-Int.ofNat U, by rw [h]; show 2 * Int.ofNat U = _; lib_int, by lib_int⟩
      | inr h => exact ⟨Int.ofNat U, by rw [h]; show -(2 * Int.ofNat U) = _; lib_int, rfl⟩
    match hZ with
    | ⟨Z, hZs, hZZ⟩ =>
    have hZ1 : ∃ s, Z = 3 * s + 1 := ⟨Z + T, by
      have : -2 * Z = 1 + 3 * T := hZs.symm.trans hT
      rw [show 3 * (Z + T) + 1 = 3 * Z + (1 + 3 * T) by lib_int, ← this]; lib_int⟩
    have hNorm : enorm ⟨Z, Int.ofNat W⟩ = ipow (Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1)) (N + 1) := by
      show Z * Z + 3 * (Int.ofNat W * Int.ofNat W) = _
      rw [hZZ, ← hq]
      exact congrArg Int.ofNat hUW
    have hU : ¬ ∃ w, Z = Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) * w := fun hex => match hex with
      | ⟨w, hw⟩ => by
        have h0 : castZ F.K sF = F.K.zero := by
          rw [hZs, hw, show -2 * (Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) * w) = Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) * (-2 * w) by lib_int,
            HasseDavenport.castZ_mul, hcP, DetChain.zero_mul']
        rw [h0] at e1
        have hY : Frobenius.pw F.K (nsm F.K (Frobenius.C (3 * t + 3) (2 * t + 2)) F.K.one) (N + 1) = F.K.zero := by
          rw [← DetChain.neg_neg' F.K (Frobenius.pw F.K _ _), ← e1]
          exact (DetChain.neg_unique F.K _ _ (F.K.zero_add _)).symm
        exact FinPow.pw_ne_zero F.K F.nozd _ hX (N + 1) hY
    have hre := EisenPow.epow_primary (2 * (3 * t + 2) + 1) hpr h3 x y hxy hx (N + 1) ⟨Z, Int.ofNat W⟩ hNorm hU hZ1
    have hre' : Z = (epow ⟨x, y⟩ (N + 1)).re := hre
    rw [hZs, hre']

end FinPow3

#print axioms FinPow3.rep_mul
#print axioms FinPow3.rep_pow_gen
#print axioms FinPow3.rep3
#print axioms FinPow3.rep2
#print axioms FinPow3.fin_pow3

lang/FinPowerSum.lean

lean_all/lang/FinPowerSum.lean

/-
  lang/FinPowerSum — T43, cycle 96: power sums over any finite field of the interface.

  For an FF of order q = 2h + 1, sums with values in the field itself:
    compK           the element list is complete over K (from the integer count)
    sum_one_K       Σ_x 1 = q·1
    char_q          q·1 = 0                                  (Σ (x + 1) = Σ x: the shift is a bijection)
    ps_zero_ff      Σ_x x^e = 0 for 1 ≤ e ≤ q − 2            (x ↦ g x with g^e ≠ 1: root bound)
    ps_top_ff       Σ_x x^(q−1) = −1                          (Fermat on the q − 1 units)
  The input of the Hasse invariant over every 𝔽_q (𝔽_{p²} included): Σ_x f(x)^h ≡ −(coefficient of x^(q−1)).

  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.FinCyclic
import lang.IntCast
import lang.FinMoment

namespace FinPowerSum

open FTAChain YMChain FiniteRecChain FinField FinList LibRing

variable {R : Type} [DecidableEq R] (F : FF R)

theorem delta_count (z : R) : ∀ l : List R, sumK F.K (fun y => delta F.K y z) l = castN F.K (cnt z l)
  | [] => rfl
  | a :: l => by
    show F.K.add (delta F.K a z) (sumK F.K (fun y => delta F.K y z) l) = castN F.K ((if a = z then 1 else 0) + cnt z l)
    rw [delta_count z l, IntCast.castN_add]
    by_cases e : a = z
    · rw [show delta F.K a z = F.K.one from ite_pos' _ _ e, ite_pos' _ _ e]
      show _ = F.K.add (F.K.add F.K.one F.K.zero) _
      rw [DetChain.add_zero']
    · rw [show delta F.K a z = F.K.zero from ite_neg' _ _ e, ite_neg' _ _ e]
      rfl

theorem compK : Complete F.K F.els := fun z => by
  rw [delta_count F z]
  rw [show cnt z F.els = 1 from Int.ofNat.inj ((cnt_int z F.els).symm.trans (F.comp z))]
  show F.K.add F.K.one F.K.zero = F.K.one
  exact DetChain.add_zero' F.K _

theorem sum_one_K : ∀ l : List R, sumK F.K (fun _ => F.K.one) l = castN F.K l.length
  | [] => rfl
  | _ :: l => by
    show F.K.add F.K.one (sumK F.K (fun _ => F.K.one) l) = F.K.add F.K.one (castN F.K l.length)
    rw [sum_one_K l]

/-- **q · 1 = 0.** -/
theorem char_q : castN F.K (2 * F.h + 1) = F.K.zero := by
  have hre := reindex F.K F.els (compK F) (fun x => F.K.add x F.K.one) (fun x => F.K.add x (F.K.neg F.K.one))
    (fun x => by lib_ring) (fun x => by lib_ring) (fun x => x)
  have e : sumK F.K (fun x => F.K.add x F.K.one) F.els = F.K.add (sumK F.K (fun x => x) F.els) (sumK F.K (fun _ => F.K.one) F.els) :=
    sumK_add F.K _ _ _
  rw [e, sum_one_K, len_els F] at hre
  have := congrArg (fun z => F.K.add z (F.K.neg (sumK F.K (fun x => x) F.els))) hre
  rw [show F.K.add (F.K.add (sumK F.K (fun x => x) F.els) (castN F.K (2 * F.h + 1))) (F.K.neg (sumK F.K (fun x => x) F.els))
      = castN F.K (2 * F.h + 1) by lib_ring, F.K.add_neg] at this
  exact this

theorem ps_zero_ff (k : Nat) (hk : k + 1 + 1 ≤ 2 * F.h) :
    sumK F.K (fun x => Frobenius.pw F.K x (k + 1)) F.els = F.K.zero := by
  match FinCyclic.exists_nonroot_ff F k hk with
  | ⟨g, hg0, hg⟩ =>
    have hre := reindex F.K F.els (compK F) (F.K.mul g) (F.K.mul (F.iv g))
      (fun x => by rw [← F.K.mul_assoc, F.iv_mul g hg0, F.K.one_mul])
      (fun y => by rw [← F.K.mul_assoc, F.K.mul_comm g, F.iv_mul g hg0, F.K.one_mul])
      (fun x => Frobenius.pw F.K x (k + 1))
    have e : sumK F.K (fun x => Frobenius.pw F.K (F.K.mul g x) (k + 1)) F.els
        = F.K.mul (Frobenius.pw F.K g (k + 1)) (sumK F.K (fun x => Frobenius.pw F.K x (k + 1)) F.els) := by
      rw [mul_sumK]
      exact sumK_ext _ _ _ (fun x => Frobenius.pw_mul _ g x (k + 1)) _
    rw [e] at hre
    have e2 : F.K.mul (F.K.add (Frobenius.pw F.K g (k + 1)) (F.K.neg F.K.one)) (sumK F.K (fun x => Frobenius.pw F.K x (k + 1)) F.els)
        = F.K.zero := by
      rw [DetChain.right_distrib, hre, DetChain.neg_mul_left, F.K.one_mul, F.K.add_neg]
    cases F.nozd _ _ e2 with
    | inl h => exact absurd (FinMoment.eq_of_sub_zero F _ _ h) hg
    | inr h => exact h

/-- **Σ_x x^(q−1) = −1.** -/
theorem ps_top_ff : sumK F.K (fun x => Frobenius.pw F.K x (2 * F.h)) F.els = F.K.neg F.K.one := by
  have hpos := FinCyclic.h_pos F
  have pt : ∀ x, Frobenius.pw F.K x (2 * F.h) = F.K.add F.K.one (F.K.neg (delta F.K x F.K.zero)) := fun x => by
    by_cases hx : x = F.K.zero
    · rw [show delta F.K x F.K.zero = F.K.one from ite_pos' _ _ hx, F.K.add_neg, hx]
      cases hh : 2 * F.h with
      | zero =>
        have h2 : 2 * 1 ≤ 2 * F.h := Nat.mul_le_mul_left 2 hpos
        rw [hh] at h2; exact absurd h2 (Nat.not_succ_le_zero 1)
      | succ r => exact HasseCM.pw_zero_succ F.K r
    · rw [show delta F.K x F.K.zero = F.K.zero from ite_neg' _ _ hx, FinCyclic.fermat_ff F x hx]
      lib_ring
  rw [sumK_ext F.K _ _ pt, sumK_add, sum_one_K, len_els F, char_q, F.K.zero_add]
  have := compK F F.K.zero
  rw [show sumK F.K (fun x => F.K.neg (delta F.K x F.K.zero)) F.els = F.K.neg (sumK F.K (fun x => delta F.K x F.K.zero) F.els)
    from CMInert.sum_neg_fun F.K _ _, this]

end FinPowerSum

#print axioms FinPowerSum.delta_count
#print axioms FinPowerSum.compK
#print axioms FinPowerSum.sum_one_K
#print axioms FinPowerSum.char_q
#print axioms FinPowerSum.ps_zero_ff
#print axioms FinPowerSum.ps_top_ff

lang/FinPrimary.lean

lean_all/lang/FinPrimary.lean

/-
  lang/FinPrimary — T43, cycle 86: the exact sign for y² = x³ − x over every finite field 𝔽_q containing i.

    primary_of_mod8   a² + v² = 7 + 2a + 8X  ⇒  v even and a + v ≡ 1 (mod 4)   (pure integer lemma)
    fin_ap_primary    ∃ a b ∈ ℤ, a² + b² = q, a + b i primary, −Σ_x χ(x³ − x) = 2a
  With FinTrace (Σ = ±2u, q = u² + v², Σ ≡ 7 − q mod 8): the Frobenius trace of y² = x³ − x over 𝔽_q is ψ(π) + ψ(π̄) = 2 Re π
  for the primary Gaussian integer π of norm q — the Hecke character of ℚ(i) at every prime power q ≡ 1 (mod 4)
  (q = p, p², …), generalising SignCM3.ap_primary from 𝔽_p.

  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.FinTrace
import lang.SignCM3

namespace FinPrimary

open YMChain FiniteRecChain FinField FinConic FinMoment

theorem two_cancel (x y : Int) (h : 2 * x = 2 * y) : x = y := CubicTrace.cancel_N 2 (fun e => Nat.noConfusion e) x y h

theorem primary_of_mod8 (a v X : Int) (h : a * a + v * v = 7 + 2 * a + 8 * X) :
    (∃ c, v = 2 * c) ∧ ∃ t, a + v - 1 = 4 * t := by
  match GaussInt.int_even_odd a, GaussInt.int_even_odd v with
  | Or.inl ⟨d, hd⟩, Or.inl ⟨c, hc⟩ =>
    exfalso; rw [hd, hc] at h
    exact GaussInt.int_odd_ne_even (3 + 2 * d + 4 * X) (2 * d * d + 2 * c * c) (by rw [show 2 * (2 * d * d + 2 * c * c) = 2 * d * (2 * d) + 2 * c * (2 * c) by lib_int, h]; lib_int)
  | Or.inl ⟨d, hd⟩, Or.inr ⟨c, hc⟩ =>
    exfalso; rw [hd, hc] at h
    -- 4(d² + c² + c − d − 2X) = 6
    have h2 : 2 * (2 * (d * d + c * c + c + -1 * d + -2 * X)) = 2 * (2 * 1 + 1) := by
      rw [show 2 * (2 * (d * d + c * c + c + -1 * d + -2 * X)) = 2 * d * (2 * d) + (2 * c + 1) * (2 * c + 1) + -1 * (7 + 2 * (2 * d) + 8 * X) + 6 by lib_int, h]
      lib_int
    exact GaussInt.int_odd_ne_even 1 _ (two_cancel _ _ h2).symm
  | Or.inr ⟨d, hd⟩, Or.inr ⟨c, hc⟩ =>
    exfalso; rw [hd, hc] at h
    exact GaussInt.int_odd_ne_even (4 * X + 4 + 2 * d) (2 * d * d + 2 * d + 2 * c * c + 2 * c + 1)
      (by rw [show 2 * (2 * d * d + 2 * d + 2 * c * c + 2 * c + 1) = (2 * d + 1) * (2 * d + 1) + (2 * c + 1) * (2 * c + 1) by lib_int, h]; lib_int)
  | Or.inr ⟨d, hd⟩, Or.inl ⟨c, hc⟩ =>
    refine ⟨⟨c, hc⟩, ?_⟩
    rw [hd, hc] at h
    -- d² + c² = 2 + 2X
    have hdc : 2 * (d * d + c * c) = 2 * (2 + 2 * X) := by
      apply two_cancel
      rw [show 2 * (2 * (d * d + c * c)) = (2 * d + 1) * (2 * d + 1) + 2 * c * (2 * c) + -1 * (4 * d + 1) by lib_int, h]
      lib_int
    have hdc' := two_cancel _ _ hdc
    rw [hd, hc]
    match GaussInt.int_even_odd d, GaussInt.int_even_odd c with
    | Or.inl ⟨e, he⟩, Or.inl ⟨f, hf⟩ => exact ⟨e + f, by rw [he, hf]; lib_int⟩
    | Or.inr ⟨e, he⟩, Or.inr ⟨f, hf⟩ => exact ⟨e + f + 1, by rw [he, hf]; lib_int⟩
    | Or.inl ⟨e, he⟩, Or.inr ⟨f, hf⟩ =>
      exfalso; rw [he, hf] at hdc'
      exact GaussInt.int_odd_ne_even (2 * e * e + 2 * f * f + 2 * f) (1 + X)
        (by rw [show 2 * (2 * e * e + 2 * f * f + 2 * f) + 1 = 2 * e * (2 * e) + (2 * f + 1) * (2 * f + 1) by lib_int, hdc']; lib_int)
    | Or.inr ⟨e, he⟩, Or.inl ⟨f, hf⟩ =>
      exfalso; rw [he, hf] at hdc'
      exact GaussInt.int_odd_ne_even (2 * e * e + 2 * e + 2 * f * f) (1 + X)
        (by rw [show 2 * (2 * e * e + 2 * e + 2 * f * f) + 1 = (2 * e + 1) * (2 * e + 1) + 2 * f * (2 * f) by lib_int, hdc']; lib_int)

variable {R : Type} [DecidableEq R] (F : FF R)

/-- **a = 2 Re π for the primary π of norm q** (y² = x³ − x over every 𝔽_q ∋ i). -/
theorem fin_ap_primary (key : R → Nat) (hkey : ∀ x y, key x = key y → x = y) (i : R) (hi : F.K.mul i i = F.K.neg F.K.one) :
    ∃ a b : Int, a * a + b * b = Int.ofNat (2 * F.h + 1) ∧ GaussInt.Primary ⟨a, b⟩ ∧
      -sumK Z (fun x => chi F (FinSign.f F x)) F.els = 2 * a := by
  match FinTrace.fin_trace F key hkey i hi with
  | ⟨u, v, huv, hs, X, hX⟩ =>
    generalize hS : sumK Z (fun x => chi F (FinSign.f F x)) F.els = S at hs hX ⊢
    have hq : Int.ofNat u * Int.ofNat u + Int.ofNat v * Int.ofNat v = Int.ofNat (2 * F.h + 1) := congrArg Int.ofNat huv
    -- a = −S/2 = ∓u
    have ha : ∃ a : Int, S = -(2 * a) ∧ a * a = Int.ofNat u * Int.ofNat u := by
      cases hs with
      | inl e => exact ⟨-Int.ofNat u, by rw [e]; show 2 * Int.ofNat u = _; lib_int, by lib_int⟩
      | inr e => exact ⟨Int.ofNat u, by rw [e]; rfl, rfl⟩
    match ha with
    | ⟨a, hSa, haa⟩ =>
      have hab : a * a + Int.ofNat v * Int.ofNat v = Int.ofNat (2 * F.h + 1) := by rw [haa]; exact hq
      have h8 : a * a + Int.ofNat v * Int.ofNat v = 7 + 2 * a + 8 * X := by
        rw [hab]
        have : -(2 * a) = 7 + -1 * Int.ofNat (2 * F.h + 1) + 8 * X := hSa.symm.trans hX
        calc Int.ofNat (2 * F.h + 1) = -1 * (7 + -1 * Int.ofNat (2 * F.h + 1) + 8 * X) + 7 + 8 * X := by lib_int
          _ = 7 + 2 * a + 8 * X := by rw [← this]; lib_int
      match primary_of_mod8 a (Int.ofNat v) X h8 with
      | ⟨⟨c, hc⟩, ⟨t, ht⟩⟩ =>
        exact ⟨a, Int.ofNat v, hab, ⟨⟨c, hc⟩, ⟨t, ht⟩⟩, by rw [hSa]; lib_int⟩

end FinPrimary

#print axioms FinPrimary.two_cancel
#print axioms FinPrimary.primary_of_mod8
#print axioms FinPrimary.fin_ap_primary

lang/FinSign.lean

lean_all/lang/FinSign.lean

/-
  lang/FinSign — T43, cycle 73: the mod-8 count Σ_x χ(x³ − x) = 3 − q + 4 I(i) + 8 H₂ over any finite field containing
  i (i² = −1), ordered by an injective key — port of SignCM/SignCM2 to FinField.FF.

  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.FinPairing
import lang.Thue
import lang.CMInert

namespace FinSign

open YMChain FiniteRecChain FinField FinConic FinMoment

variable {R : Type} [DecidableEq R] (F : FF R) (key : R → Nat)

def f (x : R) : R := CMInert.cub F.K x

def I (x : R) : Int := if chi F (f F x) = 1 then 1 else 0

def H : Int := sumK Z (fun x => FinPairing.ltI key x (F.K.neg x) * I F x) F.els

theorem chi_f_split (x : R) :
    chi F (f F x) = 2 * I F x + -1 + delta Z (f F x) F.K.zero := by
  unfold I
  by_cases h0 : f F x = F.K.zero
  · rw [h0, chi_zero, FinMoment.delta_one _ _ rfl, ite_neg' _ _ (fun e => FinClass.z01 e)]; rfl
  · rw [FinMoment.delta_zero _ _ h0]
    cases FinClass.chi_cases F (f F x) with
    | inl h => exact absurd h (FinClass.chi_ne_zero F _ h0)
    | inr h =>
      cases h with
      | inl h1 => rw [h1, ite_pos' _ _ rfl]; rfl
      | inr h1 => rw [h1, ite_neg' _ _ (fun e => Int.noConfusion e)]; rfl

theorem f_neg (x : R) : f F (F.K.neg x) = F.K.neg (f F x) := by
  unfold f; show F.K.add (F.K.mul (F.K.mul (F.K.neg x) (F.K.neg x)) (F.K.neg x)) (F.K.neg (F.K.neg x))
    = F.K.neg (F.K.add (F.K.mul (F.K.mul x x) x) (F.K.neg x)); lib_ring

theorem I_neg (hm1 : chi F (F.K.neg F.K.one) = 1) (x : R) : I F (F.K.neg x) = I F x := by
  unfold I
  have : chi F (f F (F.K.neg x)) = chi F (f F x) := by
    rw [f_neg, show F.K.neg (f F x) = F.K.mul (F.K.neg F.K.one) (f F x) by lib_ring, chi_mul F, hm1]; lib_int
  rw [this]

theorem f_zero_iff (x : R)
    (h : f F x = F.K.zero) : x = F.K.zero ∨ x = F.K.one ∨ x = F.K.neg F.K.one := by
  have e : f F x = F.K.mul x (F.K.mul (F.K.add x (F.K.neg F.K.one)) (F.K.add x F.K.one)) := by
    unfold f; show F.K.add (F.K.mul (F.K.mul x x) x) (F.K.neg x) = _; lib_ring
  rw [e] at h
  cases F.nozd _ _ h with
  | inl h0 => exact Or.inl h0
  | inr h1 =>
    cases F.nozd _ _ h1 with
    | inl h2 => exact Or.inr (Or.inl (FinMoment.eq_of_sub_zero F _ _ h2))
    | inr h2 =>
      right; right
      exact DetChain.neg_unique F.K _ _ h2

theorem I_zero : I F F.K.zero = 0 := by
  unfold I
  have : f F F.K.zero = F.K.zero := by unfold f; show F.K.add (F.K.mul (F.K.mul F.K.zero F.K.zero) F.K.zero) (F.K.neg F.K.zero) = _; lib_ring
  rw [this, chi_zero, ite_neg' _ _ (fun e => FinClass.z01 e)]

theorem roots_f :
    sumK Z (fun x => delta Z (f F x) F.K.zero) F.els = 3 := by
  have n01 : F.K.zero ≠ F.K.one := fun e => FinConic.one_ne_zero F e.symm
  have n0m : F.K.zero ≠ F.K.neg F.K.one := fun e => n01 (by
    have := congrArg F.K.neg e; rw [DetChain.neg_neg'] at this
    rw [← this]; exact (DetChain.neg_unique F.K _ _ (F.K.zero_add F.K.zero)))
  have n1m : F.K.one ≠ F.K.neg F.K.one := fun e => F.ne_pm e.symm
  have pt : ∀ x, delta Z (f F x) F.K.zero = Z.add (delta Z x F.K.zero) (Z.add (delta Z x F.K.one) (delta Z x (F.K.neg F.K.one))) :=
    fun x => by
      by_cases h : f F x = F.K.zero
      · rw [FinMoment.delta_one _ _ h]
        cases f_zero_iff F x h with
        | inl h0 => rw [h0, FinMoment.delta_one _ _ rfl, FinMoment.delta_zero _ _ n01, FinMoment.delta_zero _ _ n0m]; rfl
        | inr h1 =>
          cases h1 with
          | inl h1 => rw [h1, FinMoment.delta_zero _ _ (fun e => n01 e.symm), FinMoment.delta_one _ _ rfl, FinMoment.delta_zero _ _ n1m]; rfl
          | inr h1 => rw [h1, FinMoment.delta_zero _ _ (fun e => n0m e.symm), FinMoment.delta_zero _ _ (fun e => n1m e.symm),
              FinMoment.delta_one _ _ rfl]; rfl
      · have hfx : ∀ c, x = c → f F c = F.K.zero → False := fun c e hc => h (e ▸ hc)
        have f0 : f F F.K.zero = F.K.zero := by unfold f; show F.K.add (F.K.mul (F.K.mul F.K.zero F.K.zero) F.K.zero) (F.K.neg F.K.zero) = _; lib_ring
        have f1 : f F F.K.one = F.K.zero := by unfold f; show F.K.add (F.K.mul (F.K.mul F.K.one F.K.one) F.K.one) (F.K.neg F.K.one) = _; lib_ring
        have fm : f F (F.K.neg F.K.one) = F.K.zero := by
          unfold f; show F.K.add (F.K.mul (F.K.mul (F.K.neg F.K.one) (F.K.neg F.K.one)) (F.K.neg F.K.one)) (F.K.neg (F.K.neg F.K.one)) = _; lib_ring
        rw [FinMoment.delta_zero _ _ h, FinMoment.delta_zero _ _ (fun e => hfx _ e f0), FinMoment.delta_zero _ _ (fun e => hfx _ e f1),
          FinMoment.delta_zero _ _ (fun e => hfx _ e fm)]; rfl
  rw [sumK_ext Z _ _ pt, sumK_add, sumK_add, show sumK Z (fun x => delta Z x F.K.zero) F.els = 1 from F.comp F.K.zero,
    show sumK Z (fun x => delta Z x F.K.one) F.els = 1 from F.comp F.K.one,
    show sumK Z (fun x => delta Z x (F.K.neg F.K.one)) F.els = 1 from F.comp _]
  rfl

theorem sum_I (hkey : ∀ x y, key x = key y → x = y) (hm1 : chi F (F.K.neg F.K.one) = 1) : sumK Z (I F) F.els = 2 * H F key := by
  rw [FinPairing.pairing key hkey F.K.neg (DetChain.neg_neg' F.K) (I F) (I_neg F hm1) F.els (F.comp)]
  have pt : ∀ x, FinPairing.eqI (F.K.neg x) x * I F x = 0 := fun x => by
    unfold FinPairing.eqI
    by_cases h : F.K.neg x = x
    · have hx : x = F.K.zero := by
        by_cases hx : x = F.K.zero
        · exact hx
        · exact absurd h.symm (FinMoment.two_ne F x hx)
      rw [hx, I_zero]; lib_int
    · rw [ite_neg' _ _ h]; lib_int
  rw [sumK_ext Z _ (fun _ => (0 : Int)) pt, zsum]
  show 0 + 2 * H F key = 2 * H F key
  lib_int

/-- **Σ_x χ(x³ − x) = 4H − p + 3.** -/
theorem S_eq (hkey : ∀ x y, key x = key y → x = y) (hm1 : chi F (F.K.neg F.K.one) = 1) : sumK Z (fun x => chi F (f F x)) F.els = 4 * H F key + -1 * PP F + 3 := by
  have pt : ∀ x, chi F (f F x) = Z.add (Z.add (Z.mul 2 (I F x)) (-1)) (delta Z (f F x) F.K.zero) := fun x => by
    rw [chi_f_split]; rfl
  rw [sumK_ext Z _ _ pt, sumK_add, sumK_add, ← mul_sumK, sum_I F key hkey hm1, roots_f F]
  have : sumK Z (fun _ => (-1 : Int)) F.els = -1 * PP F := by
    rw [← FinMoment.sum_one F]
    show _ = Z.mul (-1) (sumK Z (fun _ => (1 : Int)) F.els)
    rw [mul_sumK]; rfl
  rw [this]
  show 2 * (2 * H F key) + -1 * PP F + 3 = _
  lib_int



abbrev half (x : R) : Prop := key x < key (F.K.neg x)

theorem iv_neg_one : F.iv (F.K.neg F.K.one) = F.K.neg F.K.one := by
  have h0 : F.K.neg F.K.one ≠ F.K.zero := fun e => FinConic.one_ne_zero F (by
    rw [← DetChain.neg_neg' F.K F.K.one, e]; exact (DetChain.neg_unique F.K _ _ (F.K.zero_add F.K.zero)).symm)
  have h1 := F.iv_mul _ h0
  have h2 : F.K.mul (F.K.neg F.K.one) (F.K.neg F.K.one) = F.K.one := by lib_ring
  calc F.iv (F.K.neg F.K.one) = F.K.mul (F.iv (F.K.neg F.K.one)) (F.K.mul (F.K.neg F.K.one) (F.K.neg F.K.one)) := by rw [h2]; lib_ring
    _ = F.K.mul (F.K.mul (F.iv (F.K.neg F.K.one)) (F.K.neg F.K.one)) (F.K.neg F.K.one) := by lib_ring
    _ = F.K.neg F.K.one := by rw [h1]; lib_ring

theorem iv_neg (x : R) : F.iv (F.K.neg x) = F.K.neg (F.iv x) := by
  rw [show F.K.neg x = F.K.mul (F.K.neg F.K.one) x by lib_ring, F.iv_hom, iv_neg_one F]
  lib_ring

theorem I_inv (hm1 : chi F (F.K.neg F.K.one) = 1) (x : R) (hx : x ≠ F.K.zero) : I F (F.iv x) = I F x := by
  unfold I
  have hxy : F.K.mul x (F.iv x) = F.K.one := by rw [F.K.mul_comm]; exact F.iv_mul x hx
  have hy0 : F.iv x ≠ F.K.zero := fun e => by rw [e, DetChain.mul_zero] at hxy; exact FinConic.one_ne_zero F hxy.symm
  have e : f F (F.iv x) = F.K.mul (F.K.mul (F.K.mul (F.iv x) (F.iv x)) (F.K.mul (F.iv x) (F.iv x))) (F.K.mul (F.K.neg F.K.one) (f F x)) := by
    have id : f F (F.iv x) = F.K.add (F.K.mul (F.K.mul (F.K.mul (F.iv x) (F.iv x)) (F.K.mul (F.iv x) (F.iv x))) (F.K.mul (F.K.neg F.K.one) (f F x)))
        (F.K.add (F.K.mul (F.K.mul (F.K.mul (F.iv x) (F.iv x)) (F.iv x)) (F.K.add F.K.one (F.K.neg (F.K.mul x (F.iv x)))))
          (F.K.neg (F.K.mul (F.iv x) (F.K.add F.K.one (F.K.neg (F.K.mul (F.K.mul x (F.iv x)) (F.K.mul (F.K.mul x (F.iv x)) (F.K.mul x (F.iv x))))))))) := by
      unfold f; show F.K.add (F.K.mul (F.K.mul (F.iv x) (F.iv x)) (F.iv x)) (F.K.neg (F.iv x)) = _
      show _ = F.K.add _ (F.K.add _ (F.K.neg (F.K.mul (F.iv x) (F.K.add F.K.one (F.K.neg (F.K.mul (F.K.mul x (F.iv x))
        (F.K.mul (F.K.mul x (F.iv x)) (F.K.mul x (F.iv x))))))))) 
      unfold CMInert.cub
      lib_ring
    rw [id, hxy, F.K.add_neg, F.K.one_mul, F.K.one_mul, F.K.add_neg, DetChain.mul_zero, DetChain.mul_zero]
    have : F.K.neg F.K.zero = F.K.zero := (DetChain.neg_unique F.K _ _ (F.K.zero_add F.K.zero)).symm
    rw [this, DetChain.add_zero', DetChain.add_zero']
  have hc : chi F (f F (F.iv x)) = chi F (f F x) := by
    rw [e, chi_mul F, chi_mul F, chi_sq F _ hy0, chi_mul F, hm1]
    lib_int
  rw [hc]

theorem neg_neg (x : R) : F.K.neg (F.K.neg x) = x := DetChain.neg_neg' F.K x

theorem half_or (hkey : ∀ x y, key x = key y → x = y) (x : R)
    (hx : x ≠ F.K.zero) : half F key x ∨ half F key (F.K.neg x) := by
  unfold half
  rw [neg_neg]
  cases Nat.lt_or_ge (key x) (key (F.K.neg x)) with
  | inl h => exact Or.inl h
  | inr h => exact Or.inr (Nat.lt_of_le_of_ne h (fun e => FinMoment.two_ne F x hx (hkey _ _ e.symm)))

theorem half_not_both (x : R) (h1 : half F key x) (h2 : half F key (F.K.neg x)) : False := by
  unfold half at h1 h2
  rw [neg_neg] at h2
  exact Nat.lt_irrefl _ (Nat.lt_trans h1 h2)

def r (x : R) : R := if half F key (F.iv x) then F.iv x else F.K.neg (F.iv x)

theorem iv_ne_zero (x : R)
    (hx : x ≠ F.K.zero) : F.iv x ≠ F.K.zero := fun e => by
  have := F.iv_mul x hx
  rw [e, DetChain.zero_mul'] at this
  exact FinConic.one_ne_zero F this.symm

theorem r_half (hkey : ∀ x y, key x = key y → x = y) (x : R)
    (hx : x ≠ F.K.zero) : half F key (r F key x) := by
  unfold r
  by_cases h : half F key (F.iv x)
  · rw [ite_pos' _ _ h]; exact h
  · rw [ite_neg' _ _ h]
    cases half_or F key hkey (F.iv x) (FinConic.iv_ne_zero F x hx) with
    | inl h' => exact absurd h' h
    | inr h' => exact h'

theorem r_ne_zero (x : R)
    (hx : x ≠ F.K.zero) : r F key x ≠ F.K.zero := by
  unfold r
  by_cases h : half F key (F.iv x)
  · rw [ite_pos' _ _ h]; exact FinConic.iv_ne_zero F x hx
  · rw [ite_neg' _ _ h]
    intro e
    apply FinConic.iv_ne_zero F x hx
    rw [← neg_neg F (F.iv x), e]
    exact (DetChain.neg_unique F.K _ _ (F.K.zero_add F.K.zero)).symm

theorem iv_r (x : R) :
    F.iv (r F key x) = x ∨ F.iv (r F key x) = F.K.neg x := by
  unfold r
  by_cases h : half F key (F.iv x)
  · rw [ite_pos' _ _ h, FinConic.inv_inv F]; exact Or.inl rfl
  · rw [ite_neg' _ _ h, iv_neg, FinConic.inv_inv F]; exact Or.inr rfl

theorem r_r (x : R)
    (hhx : half F key x) : r F key (r F key x) = x := by
  show (if half F key (F.iv (r F key x)) then F.iv (r F key x) else F.K.neg (F.iv (r F key x))) = x
  cases iv_r F key x with
  | inl e => rw [e, ite_pos' _ _ hhx]
  | inr e =>
    rw [e, ite_neg' _ _ (fun h => half_not_both F key x hhx h), neg_neg]

def rho (x : R) : R := if (x ≠ F.K.zero ∧ half F key x) then r F key x else x

theorem rho_invol (hkey : ∀ x y, key x = key y → x = y) (x : R) :
    rho F key (rho F key x) = x := by
  by_cases h : x ≠ F.K.zero ∧ half F key x
  · have e1 : rho F key x = r F key x := ite_pos' _ _ h
    have h' : r F key x ≠ F.K.zero ∧ half F key (r F key x) := ⟨r_ne_zero F key x h.1, r_half F key hkey x h.1⟩
    rw [e1]
    show (if (r F key x ≠ F.K.zero ∧ half F key (r F key x)) then r F key (r F key x) else r F key x) = x
    rw [ite_pos' _ _ h', r_r F key x h.2]
  · have e1 : rho F key x = x := ite_neg' _ _ h
    rw [e1, e1]

def J (x : R) : Int := FinPairing.ltI key x (F.K.neg x) * I F x

theorem ltI_half (x : R) (h : half F key x) : FinPairing.ltI key x (F.K.neg x) = 1 := ite_pos' _ _ h
theorem ltI_nhalf (x : R) (h : ¬ half F key x) : FinPairing.ltI key x (F.K.neg x) = 0 := ite_neg' _ _ h

theorem I_r (hm1 : chi F (F.K.neg F.K.one) = 1)
    (x : R) (hx : x ≠ F.K.zero) : I F (r F key x) = I F x := by
  unfold r
  by_cases h : half F key (F.iv x)
  · rw [ite_pos' _ _ h]; exact I_inv F hm1 x hx
  · rw [ite_neg' _ _ h, I_neg F hm1]; exact I_inv F hm1 x hx

theorem J_rho (hkey : ∀ x y, key x = key y → x = y) (hm1 : chi F (F.K.neg F.K.one) = 1)
    (x : R) : J F key (rho F key x) = J F key x := by
  by_cases h : x ≠ F.K.zero ∧ half F key x
  · have e1 : rho F key x = r F key x := ite_pos' _ _ h
    rw [e1]
    unfold J
    rw [ltI_half F key _ (r_half F key hkey x h.1), ltI_half F key _ h.2, I_r F key hm1 x h.1]
  · have e1 : rho F key x = x := ite_neg' _ _ h
    rw [e1]

theorem iv_unique (y z : R)
    (hy : y ≠ F.K.zero) (h : F.K.mul y z = F.K.one) : F.iv y = z := by
  have := F.iv_mul y hy
  calc F.iv y = F.K.mul (F.iv y) (F.K.mul y z) := by rw [h, F.K.mul_comm, F.K.one_mul]
    _ = F.K.mul (F.K.mul (F.iv y) y) z := by lib_ring
    _ = z := by rw [this, F.K.one_mul]

theorem sq_roots (i x : R)
    (hi : F.K.mul i i = F.K.neg F.K.one) (hx : F.K.mul x x = F.K.neg F.K.one) : x = i ∨ x = F.K.neg i := by
  have e : F.K.mul (F.K.add x (F.K.neg i)) (F.K.add x i) = F.K.add (F.K.mul x x) (F.K.neg (F.K.mul i i)) := by lib_ring
  rw [hx, hi, F.K.add_neg] at e
  cases F.nozd _ _ e with
  | inl h => exact Or.inl (FinMoment.eq_of_sub_zero F _ _ h)
  | inr h => exact Or.inr (DetChain.neg_unique F.K _ _ h)

theorem I_pm_one (x : R)
    (hx : F.K.mul x x = F.K.one) : I F x = 0 := by
  have hf : f F x = F.K.zero := by
    have : f F x = F.K.mul x (F.K.add (F.K.mul x x) (F.K.neg F.K.one)) := by
      unfold f; show F.K.add (F.K.mul (F.K.mul x x) x) (F.K.neg x) = _; lib_ring
    rw [this, hx, F.K.add_neg, DetChain.mul_zero]
  unfold I
  rw [hf, chi_zero, ite_neg' _ _ (fun e => FinClass.z01 e)]

theorem i_ne_zero (i : R) (hi : F.K.mul i i = F.K.neg F.K.one) : i ≠ F.K.zero := fun e => by
  rw [e, DetChain.zero_mul'] at hi
  apply FinConic.one_ne_zero F
  have := congrArg (fun z => F.K.add z F.K.one) hi
  exact (F.K.zero_add F.K.one).symm.trans (this.trans (DetChain.add_neg' F.K F.K.one))

theorem fixed_pt (i : R)
    (hi : F.K.mul i i = F.K.neg F.K.one) (x : R) :
    FinPairing.eqI (rho F key x) x * J F key x = delta Z x i * J F key x + delta Z x (F.K.neg i) * J F key x := by
  have hi0 := i_ne_zero F i hi
  by_cases h : x ≠ F.K.zero ∧ half F key x
  · have e1 : rho F key x = r F key x := ite_pos' _ _ h
    have hJ : J F key x = I F x := by unfold J; rw [ltI_half F key x h.2]; lib_int
    rw [e1, hJ]
    by_cases hx2 : F.K.mul x x = F.K.neg F.K.one
    · have hivx : F.iv x = F.K.neg x := iv_unique F x _ h.1 (by
        have : F.K.mul x (F.K.neg x) = F.K.neg (F.K.mul x x) := by lib_ring
        rw [this, hx2, neg_neg])
      have hrx : r F key x = x := by
        unfold r
        rw [hivx, ite_neg' _ _ (fun hh => half_not_both F key x h.2 hh), neg_neg]
      have hq : FinPairing.eqI (r F key x) x = 1 := by unfold FinPairing.eqI; rw [ite_pos' _ _ hrx]
      rw [hq]
      have hin : i ≠ F.K.neg i := FinMoment.two_ne F i hi0
      cases sq_roots F i x hi hx2 with
      | inl e => rw [e, FinMoment.delta_one _ _ rfl, FinMoment.delta_zero _ _ hin]; lib_int
      | inr e => rw [e, FinMoment.delta_zero _ _ (fun e' => hin e'.symm), FinMoment.delta_one _ _ rfl]; lib_int
    · have hxi : x ≠ i := fun e => hx2 (by rw [e]; exact hi)
      have hxi' : x ≠ F.K.neg i := fun e => hx2 (by rw [e, Thue.negsq F.K]; exact hi)
      rw [FinMoment.delta_zero _ _ hxi, FinMoment.delta_zero _ _ hxi']
      by_cases hr : r F key x = x
      · have hI : I F x = 0 := by
          unfold r at hr
          by_cases hh : half F key (F.iv x)
          · rw [ite_pos' _ _ hh] at hr
            apply I_pm_one F x
            have := F.iv_mul x h.1
            rw [hr] at this; exact this
          · rw [ite_neg' _ _ hh] at hr
            exfalso; apply hx2
            have h1 := F.iv_mul x h.1
            have h2 : F.iv x = F.K.neg x := by
              have := congrArg F.K.neg hr; rw [neg_neg] at this; exact this
            rw [h2] at h1
            have : F.K.mul x x = F.K.neg (F.K.mul (F.K.neg x) x) := by lib_ring
            rw [this, h1]
        rw [hI]; lib_int
      · have hq : FinPairing.eqI (r F key x) x = 0 := by unfold FinPairing.eqI; rw [ite_neg' _ _ hr]
        rw [hq]; lib_int
  · have hJ : J F key x = 0 := by
      unfold J
      by_cases hx0 : x = F.K.zero
      · rw [hx0, I_zero]; lib_int
      · have hnh : ¬ half F key x := fun hh => h ⟨hx0, hh⟩
        rw [ltI_nhalf F key x hnh]; lib_int
    rw [hJ]; lib_int

/-- **H ≡ I(i) (mod 2)**: H = I(i) + 2 H₂. -/
theorem H_mod (hkey : ∀ x y, key x = key y → x = y) (i : R)
    (hi : F.K.mul i i = F.K.neg F.K.one) :
    H F key = I F i + 2 * sumK Z (fun x => FinPairing.ltI key x (rho F key x) * J F key x) F.els := by
  have hm1 : chi F (F.K.neg F.K.one) = 1 := by
    have hi0 := i_ne_zero F i hi
    rw [← hi]; exact chi_sq F i hi0
  have hp := FinPairing.pairing key hkey (rho F key) (rho_invol F key hkey) (J F key) (J_rho F key hkey hm1) F.els (F.comp)
  show sumK Z (J F key) F.els = _
  rw [hp, sumK_ext Z _ (fun x => Z.add (Z.mul (delta Z x i) (J F key x)) (Z.mul (delta Z x (F.K.neg i)) (J F key x))) (fixed_pt F key i hi), sumK_add, pick Z F.els (F.comp) (J F key) i,
    pick Z F.els (F.comp) (J F key) (F.K.neg i)]
  have hJm : J F key (F.K.neg i) = FinPairing.ltI key (F.K.neg i) i * I F i := by
    unfold J; rw [neg_neg, I_neg F hm1]
  have hi0 := i_ne_zero F i hi
  have hs := FinPairing.lt_split key hkey i (F.K.neg i)
  have he : FinPairing.eqI (F.K.neg i) i = 0 := ite_neg' _ _ (fun e => FinMoment.two_ne F i hi0 e.symm)
  rw [he] at hs
  rw [hJm]
  show FinPairing.ltI key i (F.K.neg i) * I F i + FinPairing.ltI key (F.K.neg i) i * I F i + 2 * _ = _
  calc FinPairing.ltI key i (F.K.neg i) * I F i + FinPairing.ltI key (F.K.neg i) i * I F i + 2 * sumK Z (fun x => FinPairing.ltI key x (rho F key x) * J F key x) F.els
      = (0 + FinPairing.ltI key i (F.K.neg i) + FinPairing.ltI key (F.K.neg i) i) * I F i + 2 * sumK Z (fun x => FinPairing.ltI key x (rho F key x) * J F key x) F.els := by lib_int
    _ = _ := by rw [hs]; lib_int

/-- **The mod-8 congruence**: Σ_x χ(x³ − x) = 3 − p + 4 I(i) + 8 H₂. -/
theorem sign_mod8 (hkey : ∀ x y, key x = key y → x = y) (i : R)
    (hi : F.K.mul i i = F.K.neg F.K.one) :
    sumK Z (fun x => chi F (f F x)) F.els
      = 3 + -1 * PP F + 4 * I F i + 8 * sumK Z (fun x => FinPairing.ltI key x (rho F key x) * J F key x) F.els := by
  have hi0 := i_ne_zero F i hi
  have hm1 : chi F (F.K.neg F.K.one) = 1 := by rw [← hi]; exact chi_sq F i hi0
  rw [S_eq F key hkey hm1, H_mod F key hkey i hi]
  lib_int

end FinSign

#print axioms FinSign.chi_f_split
#print axioms FinSign.f_neg
#print axioms FinSign.I_neg
#print axioms FinSign.f_zero_iff
#print axioms FinSign.I_zero
#print axioms FinSign.roots_f
#print axioms FinSign.sum_I
#print axioms FinSign.S_eq
#print axioms FinSign.iv_neg_one
#print axioms FinSign.iv_neg
#print axioms FinSign.I_inv
#print axioms FinSign.neg_neg
#print axioms FinSign.half_or
#print axioms FinSign.half_not_both
#print axioms FinSign.iv_ne_zero
#print axioms FinSign.r_half
#print axioms FinSign.r_ne_zero
#print axioms FinSign.iv_r
#print axioms FinSign.r_r
#print axioms FinSign.rho_invol
#print axioms FinSign.ltI_half
#print axioms FinSign.ltI_nhalf
#print axioms FinSign.I_r
#print axioms FinSign.J_rho
#print axioms FinSign.iv_unique
#print axioms FinSign.sq_roots
#print axioms FinSign.I_pm_one
#print axioms FinSign.i_ne_zero
#print axioms FinSign.fixed_pt
#print axioms FinSign.H_mod
#print axioms FinSign.sign_mod8

lang/FinTrace.lean

lean_all/lang/FinTrace.lean

/-
  lang/FinTrace — T43, cycle 76: y² = x³ − x over any finite field 𝔽_q containing i (i² = −1).

  For every FF of order q with an injective key and i² = −1:
    fin_trace     ∃ u v, u² + v² = q, Σ_x χ(x³ − x) = ±2u, and Σ_x χ(x³ − x) = 7 − q + 8X for an integer X
  i.e. a = −Σ satisfies a² ≤ 4q (Hasse's bound for this curve over every such field) and a ≡ q + 1 (mod 8) — the Frobenius
  trace of the CM curve over 𝔽_q is the trace of a Gaussian integer of norm q, for every q (𝔽_p, 𝔽_{p²}, …).

  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.FinSign
import lang.CMModular

namespace FinTrace

open YMChain FiniteRecChain FinField FinConic FinMoment

variable {R : Type} [DecidableEq R] (F : FF R)

theorem fin_trace (key : R → Nat) (hkey : ∀ x y, key x = key y → x = y) (i : R) (hi : F.K.mul i i = F.K.neg F.K.one) :
    ∃ u v : Nat, u * u + v * v = 2 * F.h + 1 ∧
      (sumK Z (fun x => chi F (FinSign.f F x)) F.els = Int.ofNat (2 * u) ∨
        sumK Z (fun x => chi F (FinSign.f F x)) F.els = -Int.ofNat (2 * u)) ∧
      ∃ X : Int, sumK Z (fun x => chi F (FinSign.f F x)) F.els = 7 + -1 * Int.ofNat (2 * F.h + 1) + 8 * X := by
  have hi0 := FinSign.i_ne_zero F i hi
  have hm1 : chi F (F.K.neg F.K.one) = 1 := by rw [← hi]; exact chi_sq F i hi0
  have hIi : FinSign.I F i = 1 := by
    have h1 : F.K.add F.K.one (F.K.neg i) ≠ F.K.zero := fun e => by
      have := FinMoment.eq_of_sub_zero F _ _ e
      rw [← this, F.K.one_mul] at hi
      exact F.ne_pm hi.symm
    have e : FinSign.f F i = F.K.mul (F.K.add F.K.one (F.K.neg i)) (F.K.add F.K.one (F.K.neg i)) := by
      have id : FinSign.f F i = F.K.add (F.K.mul (F.K.add F.K.one (F.K.neg i)) (F.K.add F.K.one (F.K.neg i)))
          (F.K.mul (F.K.add i (F.K.neg F.K.one)) (F.K.add (F.K.mul i i) F.K.one)) := by
        show F.K.add (F.K.mul (F.K.mul i i) i) (F.K.neg i) = _
        lib_ring
      rw [id, hi, DetChain.add_neg' F.K F.K.one, DetChain.mul_zero, DetChain.add_zero']
    unfold FinSign.I
    rw [e, chi_sq F _ h1, ite_pos' _ _ rfl]
  have hM8 := FinSign.sign_mod8 F key hkey i hi
  rw [hIi] at hM8
  generalize hH : sumK Z (fun x => FinPairing.ltI key x (FinSign.rho F key x) * FinSign.J F key x) F.els = H2 at hM8
  match F.nonres with
  | ⟨n, hn0, hnr⟩ =>
    have hh0 : F.h ≠ 0 := fun e => hnr (by rw [e]; rfl)
    have hchin : chi F n = -1 := chi_neg_of F n hn0 hnr
    have hM := FinMoment.moment F
    have hG := FinClass.grouped F n hn0 hchin
    rw [hm1] at hM
    rw [hM] at hG
    have hP1 : PP F + -1 ≠ 0 := fun e => by
      have e2 : Int.ofNat (2 * F.h + 1) = Int.ofNat 1 := by
        have : PP F = (PP F + -1) + 1 := by lib_int
        show PP F = _; rw [this, e]; rfl
      have e3 : 2 * F.h = 0 := Nat.succ.inj (Int.ofNat.inj e2)
      apply hh0
      cases hh : F.h with
      | zero => rfl
      | succ h' => rw [hh] at e3; exact absurd e3 (fun e => Nat.noConfusion (show Nat.succ (2 * h' + 1) = 0 from by rw [← e]; lib_nat))
    have hAB : phi F F.K.one * phi F F.K.one + phi F n * phi F n = 4 * PP F := by
      have hz : (PP F + -1) * (phi F F.K.one * phi F F.K.one + phi F n * phi F n + -1 * (4 * PP F)) = 0 := by
        have : (PP F + -1) * (phi F F.K.one * phi F F.K.one + phi F n * phi F n + -1 * (4 * PP F))
            = (PP F + -1) * (phi F F.K.one * phi F F.K.one + phi F n * phi F n)
              + -1 * (2 * (PP F * ((1 + 1) * (PP F + -1)))) := by lib_int
        rw [this, ← hG]; lib_int
      cases LibInt.int_noZD _ _ hz with
      | inl h => exact absurd h hP1
      | inr h =>
        have h' : phi F F.K.one * phi F F.K.one + phi F n * phi F n + -1 * (4 * PP F) = 0 := h
        have : phi F F.K.one * phi F F.K.one + phi F n * phi F n
            = (phi F F.K.one * phi F F.K.one + phi F n * phi F n + -1 * (4 * PP F)) + 4 * PP F := by lib_int
        rw [this, h']; lib_int
    match CMModular.nat_sq (phi F F.K.one), CMModular.nat_sq (phi F n) with
    | ⟨a, ha, hsa⟩, ⟨b, hb, _⟩ =>
      rw [ha, hb] at hAB
      have hnat : a * a + b * b = 4 * (2 * F.h + 1) := Int.ofNat.inj hAB
      match CMModular.halves a b _ hnat with
      | ⟨u, v, hau, _, huv⟩ =>
        have hS : sumK Z (fun x => chi F (FinSign.f F x)) F.els = chi F i * phi F F.K.one := by
          rw [← FinConic.phi_scale F F.K.one _ hi0, F.K.one_mul, hi]
          exact sumK_ext Z _ _ (fun x => by
            show chi F (F.K.add (F.K.mul (F.K.mul x x) x) (F.K.neg x)) = chi F (F.K.mul x (F.K.add (F.K.mul x x) (F.K.neg F.K.one)))
            rw [show F.K.add (F.K.mul (F.K.mul x x) x) (F.K.neg x) = F.K.mul x (F.K.add (F.K.mul x x) (F.K.neg F.K.one)) by lib_ring]) _
        refine ⟨u, v, huv, ?_, ⟨H2, ?_⟩⟩
        · rw [hS]
          have hchi : chi F i = 1 ∨ chi F i = -1 := by
            cases FinClass.chi_cases F i with
            | inl h0 => exact absurd h0 (FinClass.chi_ne_zero F _ hi0)
            | inr h => exact h
          rw [← hau]
          cases hchi with
          | inl e => cases hsa with
            | inl e' => left; rw [e, e']; lib_int
            | inr e' => right; rw [e, e']; lib_int
          | inr e => cases hsa with
            | inl e' => right; rw [e, e']; lib_int
            | inr e' => left; rw [e, e']; lib_int
        · rw [hM8]
          show 3 + -1 * PP F + 4 * 1 + 8 * H2 = 7 + -1 * PP F + 8 * H2
          lib_int

end FinTrace

#print axioms FinTrace.fin_trace

namespace FinTrace

open YMChain FiniteRecChain FinField

/-- **Hasse's bound** for y² = x³ − x over every 𝔽_q containing i: (Σ_x χ(x³ − x))² ≤ 4q. -/
theorem hasse_bound {R : Type} [DecidableEq R] (F : FF R) (key : R → Nat) (hkey : ∀ x y, key x = key y → x = y)
    (i : R) (hi : F.K.mul i i = F.K.neg F.K.one) :
    ∃ s : Nat, (sumK FinConic.Z (fun x => chi F (FinSign.f F x)) F.els = Int.ofNat s ∨
      sumK FinConic.Z (fun x => chi F (FinSign.f F x)) F.els = -Int.ofNat s) ∧ s * s ≤ 4 * (2 * F.h + 1) := by
  match fin_trace F key hkey i hi with
  | ⟨u, v, huv, hS, _⟩ =>
    refine ⟨2 * u, hS, ?_⟩
    rw [← huv, show 2 * u * (2 * u) = 4 * (u * u) by lib_nat]
    exact Nat.mul_le_mul_left 4 (Nat.le_add_right _ _)

/-- the value order on 𝔽_p is an injective key -/
theorem key_p (m : Nat) (x y : ZMod.Zp (2 * m + 1 + 1)) (h : x.val = y.val) : x = y := Subtype.ext h

end FinTrace

#print axioms FinTrace.hasse_bound
#print axioms FinTrace.key_p

lang/FiniteField.lean

lean_all/lang/FiniteField.lean

/-
  lang/FiniteField — T43, cycle 3 (GL1 over a general number field): x^q = x in every finite field.

  For a residue field O_F/p with q = Np elements, Frob_p = (x ↦ x^q) fixes every element; with
  Frobenius.frob_add this makes Frob_p a field automorphism of O_L/P over O_F/p.

    prodL_perm      products over two lists with the same multiplicities are equal
    cnt_map         multiplicities of a list mapped by a bijection
    unit_pow        for a unit x of a finite field whose nonzero elements are listed once each
                    (n of them): x^n = 1  (Lagrange, by permuting the product of all units)
    fermat_field    x^(n+1) = x for every x, i.e. x^q = x with q = n + 1

  Lean 4 core only, proofs by hand, no axioms.
-/
import DetChain
import lang.Frobenius

namespace FiniteField

open Frobenius

variable {R : Type} [DecidableEq R] (K : DetChain.CRing R)

def prodL : List R → R
  | [] => K.one
  | a :: l => K.mul a (prodL l)

def cnt (z : R) : List R → Nat
  | [] => 0
  | a :: l => (if a = z then 1 else 0) + cnt z l

/-- Remove the first occurrence. -/
def erase1 (z : R) : List R → List R
  | [] => []
  | a :: l => if a = z then l else a :: erase1 z l

theorem ite_t {α : Type} {c : Prop} [d : Decidable c] (a b : α) (h : c) : @ite α c d a b = a := by
  cases d with
  | isTrue _ => rfl
  | isFalse hn => exact absurd h hn

theorem ite_f {α : Type} {c : Prop} [d : Decidable c] (a b : α) (h : ¬ c) : @ite α c d a b = b := by
  cases d with
  | isTrue hc => exact absurd hc h
  | isFalse _ => rfl

/-- If `z` occurs in `l`, then `prod l = z · prod (erase1 z l)`. -/
theorem prod_erase (z : R) : ∀ l : List R, 0 < cnt z l → prodL K l = K.mul z (prodL K (erase1 z l))
  | [], h => absurd h (Nat.lt_irrefl 0)
  | a :: l, h => by
    by_cases ha : a = z
    · show K.mul a (prodL K l) = K.mul z (prodL K (erase1 z (a :: l)))
      show _ = K.mul z (prodL K (if a = z then l else a :: erase1 z l))
      rw [ite_t _ _ ha, ha]
    · have h' : 0 < cnt z l := by
        have e : cnt z (a :: l) = 0 + cnt z l := by
          show (if a = z then 1 else 0) + cnt z l = 0 + cnt z l
          rw [ite_f _ _ ha]
        rw [e, Nat.zero_add] at h
        exact h
      show K.mul a (prodL K l) = K.mul z (prodL K (if a = z then l else a :: erase1 z l))
      rw [ite_f _ _ ha]
      show K.mul a (prodL K l) = K.mul z (K.mul a (prodL K (erase1 z l)))
      rw [prod_erase z l h', ← K.mul_assoc, K.mul_comm a z, K.mul_assoc]

theorem cnt_erase_same (z : R) : ∀ l : List R, 0 < cnt z l → cnt z (erase1 z l) + 1 = cnt z l
  | [], h => absurd h (Nat.lt_irrefl 0)
  | a :: l, h => by
    by_cases ha : a = z
    · show cnt z (if a = z then l else a :: erase1 z l) + 1 = (if a = z then 1 else 0) + cnt z l
      rw [ite_t _ _ ha, ite_t (1 : Nat) 0 ha, Nat.add_comm]
    · have h' : 0 < cnt z l := by
        have e : cnt z (a :: l) = 0 + cnt z l := by
          show (if a = z then 1 else 0) + cnt z l = 0 + cnt z l
          rw [ite_f _ _ ha]
        rw [e, Nat.zero_add] at h
        exact h
      show cnt z (if a = z then l else a :: erase1 z l) + 1 = (if a = z then 1 else 0) + cnt z l
      rw [ite_f _ _ ha, ite_f (1 : Nat) 0 ha]
      show (if a = z then 1 else 0) + cnt z (erase1 z l) + 1 = 0 + cnt z l
      rw [ite_f (1 : Nat) 0 ha, Nat.zero_add, Nat.zero_add, cnt_erase_same z l h']

theorem cnt_erase_other (z w : R) (hzw : w ≠ z) : ∀ l : List R, cnt w (erase1 z l) = cnt w l
  | [] => rfl
  | a :: l => by
    by_cases ha : a = z
    · show cnt w (if a = z then l else a :: erase1 z l) = (if a = w then 1 else 0) + cnt w l
      rw [ite_t _ _ ha]
      have : ¬ a = w := fun h => hzw (h ▸ ha)
      rw [ite_f (1 : Nat) 0 this, Nat.zero_add]
    · show cnt w (if a = z then l else a :: erase1 z l) = (if a = w then 1 else 0) + cnt w l
      rw [ite_f _ _ ha]
      show (if a = w then 1 else 0) + cnt w (erase1 z l) = _
      rw [cnt_erase_other z w hzw l]

/-- **Products are invariant under permutation** (equal multiplicities). -/
theorem prodL_perm : ∀ (l l' : List R), (∀ z, cnt z l = cnt z l') → prodL K l = prodL K l'
  | [], l', h => by
    cases l' with
    | nil => rfl
    | cons b l' =>
      have := h b
      have e : cnt b (b :: l') = 1 + cnt b l' := by
        show (if b = b then 1 else 0) + cnt b l' = 1 + cnt b l'
        rw [ite_t (1 : Nat) 0 rfl]
      rw [e] at this
      exact absurd this (fun h0 => Nat.noConfusion (h0.trans (Nat.add_comm 1 _)))
  | a :: l, l', h => by
    have hpos : 0 < cnt a l' := by
      rw [← h a]
      show 0 < (if a = a then 1 else 0) + cnt a l
      rw [ite_t (1 : Nat) 0 rfl, Nat.add_comm]
      exact Nat.succ_pos _
    rw [prod_erase K a l' hpos]
    show K.mul a (prodL K l) = _
    have hrest : ∀ z, cnt z l = cnt z (erase1 a l') := fun z => by
      by_cases hz : z = a
      · rw [hz]
        have e1 := cnt_erase_same a l' hpos
        have e2 : cnt a (a :: l) = 1 + cnt a l := by
          show (if a = a then 1 else 0) + cnt a l = 1 + cnt a l
          rw [ite_t (1 : Nat) 0 rfl]
        rw [← h a, e2, Nat.add_comm] at e1
        have e3 : cnt a (erase1 a l') + 1 = cnt a l + 1 := by
          rw [Nat.add_comm (cnt a (erase1 a l')) 1, Nat.add_comm (cnt a l) 1]; exact e1
        exact (Nat.succ.inj e3).symm
      · rw [cnt_erase_other a z hz l']
        have e2 : cnt z (a :: l) = 0 + cnt z l := by
          show (if a = z then 1 else 0) + cnt z l = 0 + cnt z l
          rw [ite_f (1 : Nat) 0 (fun e => hz e.symm)]
        rw [← h z, e2, Nat.zero_add]
    rw [prodL_perm l (erase1 a l') hrest]

/-- Multiplicities of a mapped list, for a bijection `f` with inverse `g`. -/
theorem cnt_map (f g : R → R) (hgf : ∀ x, g (f x) = x) (hfg : ∀ y, f (g y) = y) (z : R) :
    ∀ l : List R, cnt z (l.map f) = cnt (g z) l
  | [] => rfl
  | a :: l => by
    show (if f a = z then 1 else 0) + cnt z (l.map f) = (if a = g z then 1 else 0) + cnt (g z) l
    rw [cnt_map f g hgf hfg z l]
    by_cases ha : a = g z
    · rw [ite_t (1 : Nat) 0 ha, ite_t (1 : Nat) 0 (by rw [ha, hfg])]
    · rw [ite_f (1 : Nat) 0 ha, ite_f (1 : Nat) 0 (fun e => ha (by rw [← e, hgf]))]

omit [DecidableEq R] in
theorem prod_map_mul (x : R) : ∀ l : List R, prodL K (l.map (K.mul x)) = K.mul (pw K x l.length) (prodL K l)
  | [] => (K.one_mul K.one).symm
  | a :: l => by
    show K.mul (K.mul x a) (prodL K (l.map (K.mul x))) = K.mul (K.mul (pw K x l.length) x) (K.mul a (prodL K l))
    rw [prod_map_mul x l, K.mul_assoc, ← K.mul_assoc a, K.mul_comm a (pw K x l.length), K.mul_assoc,
      ← K.mul_assoc x, K.mul_comm x (pw K x l.length), K.mul_assoc]

/-- **Lagrange for the units of a finite field.** `us` lists every nonzero element exactly once;
    `inv` is a two-sided inverse on nonzero elements; the ring has no zero divisors. -/
theorem unit_pow (hK : DetChain.NoZD K) (inv : R → R) (hinv : ∀ x, x ≠ K.zero → K.mul (inv x) x = K.one)
    (us : List R) (hus : ∀ z, cnt z us = if z = K.zero then 0 else 1)
    (hnz : ∀ z, 0 < cnt z us → z ≠ K.zero) (hone : K.one ≠ K.zero)
    (x : R) (hx : x ≠ K.zero) : pw K x us.length = K.one := by
  have hxinv : ∀ y, K.mul x (K.mul (inv x) y) = y := fun y => by
    rw [← K.mul_assoc, K.mul_comm x (inv x), hinv x hx, K.one_mul]
  have hinvx : ∀ y, K.mul (inv x) (K.mul x y) = y := fun y => by
    rw [← K.mul_assoc, hinv x hx, K.one_mul]
  -- x·u ranges over the units as u does
  have hperm : ∀ z, cnt z (us.map (K.mul x)) = cnt z us := fun z => by
    rw [cnt_map (K.mul x) (K.mul (inv x)) hinvx hxinv z us, hus, hus]
    by_cases hz : z = K.zero
    · rw [ite_t (0 : Nat) 1 hz]
      have : K.mul (inv x) z = K.zero := by rw [hz]; exact DetChain.mul_zero K _
      rw [ite_t (0 : Nat) 1 this]
    · rw [ite_f (0 : Nat) 1 hz]
      have : ¬ K.mul (inv x) z = K.zero := fun h0 => by
        apply hz
        have := congrArg (K.mul x) h0
        rw [hxinv, DetChain.mul_zero] at this
        exact this
      rw [ite_f (0 : Nat) 1 this]
  have e := prodL_perm K _ _ hperm
  rw [prod_map_mul] at e
  -- P = prod us ≠ 0
  have hP : ∀ l : List R, (∀ z, 0 < cnt z l → z ≠ K.zero) → prodL K l ≠ K.zero := by
    intro l
    induction l with
    | nil => intro _; exact hone
    | cons a l ih =>
      intro hl h0
      have ha : a ≠ K.zero := hl a (by
        show 0 < (if a = a then 1 else 0) + cnt a l
        rw [ite_t (1 : Nat) 0 rfl, Nat.add_comm]; exact Nat.succ_pos _)
      have hl' : ∀ z, 0 < cnt z l → z ≠ K.zero := fun z hz => hl z (by
        show 0 < (if a = z then 1 else 0) + cnt z l
        exact Nat.lt_of_lt_of_le hz (Nat.le_add_left _ _))
      cases hK a (prodL K l) h0 with
      | inl h => exact ha h
      | inr h => exact ih hl' h
  -- x^n P = P, so (x^n - 1) P = 0
  have e2 : K.mul (K.add (pw K x us.length) (K.neg K.one)) (prodL K us) = K.zero := by
    rw [DetChain.right_distrib, e, DetChain.neg_mul_left, K.one_mul, K.add_neg]
  cases hK _ _ e2 with
  | inl h =>
    have this : K.add (K.add (pw K x us.length) (K.neg K.one)) K.one = K.add K.zero K.one :=
      congrArg (fun t => K.add t K.one) h
    show pw K x us.length = K.one
    rw [K.add_assoc, DetChain.add_neg' K K.one, DetChain.add_zero', K.zero_add] at this
    exact this
  | inr h => exact absurd h (hP us hnz)

/-- **x^q = x in a finite field** (q = n + 1 elements, `us` the n nonzero ones): the Frobenius
    x ↦ x^q of the residue field O_F/p (q = Np) is the identity. -/
theorem fermat_field (hK : DetChain.NoZD K) (inv : R → R) (hinv : ∀ x, x ≠ K.zero → K.mul (inv x) x = K.one)
    (us : List R) (hus : ∀ z, cnt z us = if z = K.zero then 0 else 1)
    (hnz : ∀ z, 0 < cnt z us → z ≠ K.zero) (hone : K.one ≠ K.zero) (x : R) :
    pw K x (us.length + 1) = x := by
  by_cases hx : x = K.zero
  · rw [hx]; exact DetChain.mul_zero K _
  · show K.mul (pw K x us.length) x = x
    rw [unit_pow K hK inv hinv us hus hnz hone x hx, K.one_mul]

end FiniteField

#print axioms FiniteField.ite_t
#print axioms FiniteField.ite_f
#print axioms FiniteField.prod_erase
#print axioms FiniteField.cnt_erase_same
#print axioms FiniteField.cnt_erase_other
#print axioms FiniteField.prodL_perm
#print axioms FiniteField.cnt_map
#print axioms FiniteField.prod_map_mul
#print axioms FiniteField.unit_pow
#print axioms FiniteField.fermat_field

lang/FlagPos.lean

lean_all/lang/FlagPos.lean

/-
  lang/FlagPos — T49 (O3') (v3): **two different indices cannot both be flagged when exactly one factor is**.
  For a list of indices Fs and factors Y f (f ∈ Fs), with ExactOne.cnt counting the flagged factors:
    one_flag    f ∈ Fs, Y f flagged ⇒ 1 ≤ cnt (Fs.map Y)
    two_flag    f ≠ g in Fs, both flagged ⇒ 2 ≤ cnt (Fs.map Y)
    unflag      cnt (Fs.map Y) = 1, Y f flagged, g ≠ f in Fs ⇒ Y g not flagged
  Use: Y c = φ_z(σ_c α); α ∈ P_1 flags c = 1, so every other conjugate is a unit.
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.ExactOne

namespace FlagPos

open FTAChain YMChain FiniteRecChain ExactOne

variable {R A : Type} (d : R → Bool) (Y : A → R)

theorem one_flag (f : A) (hf : d (Y f) = true) : ∀ Fs : List A, f ∈ Fs → 1 ≤ cnt d (Fs.map Y)
  | [], h => nomatch h
  | _ :: t, h => by
    cases h with
    | head =>
      show 1 ≤ (if d (Y f) = true then 1 else 0) + cnt d (t.map Y)
      rw [hf, ite_pos' _ _ rfl]
      exact Nat.le_add_right 1 _
    | tail _ h' => exact Nat.le_trans (one_flag f hf t h') (Nat.le_add_left _ _)

theorem two_flag (f g : A) (hfg : f ≠ g) (hf : d (Y f) = true) (hg : d (Y g) = true) :
    ∀ Fs : List A, f ∈ Fs → g ∈ Fs → 2 ≤ cnt d (Fs.map Y)
  | [], h, _ => nomatch h
  | _ :: t, hf', hg' => by
    cases hf' with
    | head =>
      cases hg' with
      | head => exact absurd rfl hfg
      | tail _ hgt =>
        show 2 ≤ (if d (Y f) = true then 1 else 0) + cnt d (t.map Y)
        rw [hf, ite_pos' _ _ rfl]
        exact Nat.add_le_add_left (one_flag d Y g hg t hgt) 1
    | tail _ hft =>
      cases hg' with
      | head =>
        show 2 ≤ (if d (Y g) = true then 1 else 0) + cnt d (t.map Y)
        rw [hg, ite_pos' _ _ rfl]
        exact Nat.add_le_add_left (one_flag d Y f hf t hft) 1
      | tail _ hgt => exact Nat.le_trans (two_flag f g hfg hf hg t hft hgt) (Nat.le_add_left _ _)

/-- **only one flagged index**: the others are unflagged. -/
theorem unflag (Fs : List A) (hc : cnt d (Fs.map Y) = 1) (f g : A) (hfm : f ∈ Fs) (hgm : g ∈ Fs) (hfg : f ≠ g)
    (hf : d (Y f) = true) : d (Y g) = false := by
  cases hgd : d (Y g) with
  | false => rfl
  | true =>
    have h2 := two_flag d Y f g hfg hf hgd Fs hfm hgm
    rw [hc] at h2
    exact absurd h2 (Nat.not_succ_le_self 1)

end FlagPos

#print axioms FlagPos.one_flag
#print axioms FlagPos.two_flag
#print axioms FlagPos.unflag

lang/Fp2.lean

lean_all/lang/Fp2.lean

/-
  lang/Fp2 — T43, cycle 68: 𝔽_{p²} = 𝔽_p[i] for p ≡ 3 (mod 4) — the element list, its count, inverses.

  p = J + 2 with −1 a non-residue ((−1)^m = −1, p = 2m + 1), F = quad K (−1).
    pairs, comp_pairs    the p² pairs (a, b), each once (Complete over ℤ)
    card_pairs           Σ 1 = p²
    nozd2                no zero divisors (QuadFrob.quad_inv)
    iv2, iv2_mul, iv2_zero, iv2_hom    x⁻¹ = x̄ · N(x)⁻¹
  Part of the FF instance for 𝔽_{p²} (next: Euler's criterion through the norm).

  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.QuadFrob
import lang.FinField

namespace Fp2

open FTAChain ZMod YMChain FiniteRecChain Quad QuadFrob

variable (J : Nat)

abbrev Z := LibInt.intR

def row (a : Zp (J + 1)) : List (Zp (J + 1) × Zp (J + 1)) := (CMInert.allZ (J + 1) (J + 1 + 1)).map (fun b => (a, b))

def pairsL : List (Zp (J + 1)) → List (Zp (J + 1) × Zp (J + 1))
  | [] => []
  | a :: l => row J a ++ pairsL l

def pairs : List (Zp (J + 1) × Zp (J + 1)) := pairsL J (CMInert.allZ (J + 1) (J + 1 + 1))

theorem sumK_map {A B : Type} (f : B → Int) (g : A → B) : ∀ l : List A, sumK Z f (l.map g) = sumK Z (fun a => f (g a)) l
  | [] => rfl
  | a :: l => by show f (g a) + sumK Z f (l.map g) = _; rw [sumK_map f g l]; rfl

theorem sum_pairsL (F : Zp (J + 1) × Zp (J + 1) → Int) : ∀ l : List (Zp (J + 1)),
    sumK Z F (pairsL J l) = sumK Z (fun a => sumK Z (fun b => F (a, b)) (CMInert.allZ (J + 1) (J + 1 + 1))) l
  | [] => rfl
  | a :: l => by
    show sumK Z F (row J a ++ pairsL J l) = _
    rw [CMInert.sumK_app', sum_pairsL F l, row, sumK_map]
    rfl

theorem delta_pair (y z : Zp (J + 1) × Zp (J + 1)) : delta Z y z = delta Z y.1 z.1 * delta Z y.2 z.2 := by
  unfold delta
  by_cases h1 : y.1 = z.1
  · by_cases h2 : y.2 = z.2
    · rw [ite_pos' _ _ (Prod.ext h1 h2), ite_pos' _ _ h1, ite_pos' _ _ h2]; rfl
    · rw [ite_neg' _ _ (fun e => h2 (congrArg Prod.snd e)), ite_pos' _ _ h1, ite_neg' _ _ h2]; rfl
  · rw [ite_neg' _ _ (fun e => h1 (congrArg Prod.fst e)), ite_neg' _ _ h1]; exact (DetChain.zero_mul' Z _).symm

theorem comp_pairs : Complete Z (pairs J) := fun z => by
  show sumK Z (fun y => delta Z y z) (pairsL J _) = 1
  rw [sum_pairsL]
  have pt : ∀ a, sumK Z (fun b => delta Z (a, b) z) (CMInert.allZ (J + 1) (J + 1 + 1)) = delta Z a z.1 := fun a => by
    rw [sumK_ext Z _ (fun b => Z.mul (delta Z a z.1) (delta Z b z.2)) (fun b => delta_pair J (a, b) z), ← mul_sumK,
      show sumK Z (fun b => delta Z b z.2) (CMInert.allZ (J + 1) (J + 1 + 1)) = 1 from CMInert.complete_allZ (J + 1) Z z.2]
    show delta Z a z.1 * 1 = _; lib_int
  rw [sumK_ext Z _ _ pt]
  exact CMInert.complete_allZ (J + 1) Z z.1

theorem sum_one_n : ∀ n, sumK Z (fun _ => (1 : Int)) (CMInert.allZ (J + 1) n) = Int.ofNat n
  | 0 => rfl
  | n + 1 => by
    show sumK Z _ (CMInert.allZ (J + 1) n ++ [mk _ n]) = _
    rw [CMInert.sumK_app', sum_one_n n]
    rfl

theorem card_pairs : sumK Z (fun _ => (1 : Int)) (pairs J) = Int.ofNat (J + 1 + 1) * Int.ofNat (J + 1 + 1) := by
  show sumK Z _ (pairsL J _) = _
  rw [sum_pairsL]
  have h1 := sum_one_n J (J + 1 + 1)
  rw [sumK_ext Z _ (fun _ => Int.ofNat (J + 1 + 1)) (fun _ => h1)]
  rw [show sumK Z (fun _ => Int.ofNat (J + 1 + 1)) (CMInert.allZ (J + 1) (J + 1 + 1))
      = Z.mul (Int.ofNat (J + 1 + 1)) (sumK Z (fun _ => (1 : Int)) (CMInert.allZ (J + 1) (J + 1 + 1))) by
        rw [mul_sumK]; exact sumK_ext Z _ _ (fun _ => by show _ = Int.ofNat (J + 1 + 1) * 1; lib_int) _, h1]
  rfl

variable (m : Nat)

abbrev F2 : DetChain.CRing (Zp (J + 1) × Zp (J + 1)) := quad (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one)

def iv2 (x : Zp (J + 1) × Zp (J + 1)) : Zp (J + 1) × Zp (J + 1) :=
  (F2 J).mul (qconj (ring (J + 1)) x) (Frobenius.pw (ring (J + 1)) (qnorm (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one) x) J, (ring (J + 1)).zero)

theorem embed_mul (a b : Zp (J + 1)) : (F2 J).mul (a, (ring (J + 1)).zero) (b, (ring (J + 1)).zero) = ((ring (J + 1)).mul a b, (ring (J + 1)).zero) := by
  apply Prod.ext
  · show (ring (J + 1)).add ((ring (J + 1)).mul a b) ((ring (J + 1)).mul ((ring (J + 1)).neg (ring (J + 1)).one) ((ring (J + 1)).mul (ring (J + 1)).zero (ring (J + 1)).zero)) = (ring (J + 1)).mul a b
    lib_ring
  · show (ring (J + 1)).add ((ring (J + 1)).mul a (ring (J + 1)).zero) ((ring (J + 1)).mul (ring (J + 1)).zero b) = (ring (J + 1)).zero
    lib_ring

theorem iv2_zero : iv2 J (F2 J).zero = (F2 J).zero := by
  apply Prod.ext
  · show (ring (J + 1)).add ((ring (J + 1)).mul (ring (J + 1)).zero (Frobenius.pw (ring (J + 1)) (qnorm (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one) ((ring (J + 1)).zero, (ring (J + 1)).zero)) J))
      ((ring (J + 1)).mul ((ring (J + 1)).neg (ring (J + 1)).one) ((ring (J + 1)).mul ((ring (J + 1)).neg (ring (J + 1)).zero) (ring (J + 1)).zero)) = (ring (J + 1)).zero
    lib_ring
  · show (ring (J + 1)).add ((ring (J + 1)).mul (ring (J + 1)).zero (ring (J + 1)).zero) ((ring (J + 1)).mul ((ring (J + 1)).neg (ring (J + 1)).zero) (Frobenius.pw (ring (J + 1)) (qnorm (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one) ((ring (J + 1)).zero, (ring (J + 1)).zero)) J)) = (ring (J + 1)).zero
    lib_ring

theorem iv2_mul (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hJ : J + 1 = 2 * m) (h1 : 1 ≤ J)
    (hn : Frobenius.pw (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one) m = (ring (J + 1)).neg (ring (J + 1)).one) (x : Zp (J + 1) × Zp (J + 1)) (hx : x ≠ (F2 J).zero) :
    (F2 J).mul (iv2 J x) x = (F2 J).one := by
  rw [(F2 J).mul_comm]
  exact quad_inv J ((ring (J + 1)).neg (ring (J + 1)).one) hpr m hJ h1 hn x hx

theorem iv2_hom (x y : Zp (J + 1) × Zp (J + 1)) : iv2 J ((F2 J).mul x y) = (F2 J).mul (iv2 J x) (iv2 J y) := by
  unfold iv2
  rw [Quad.conj_mul, Quad.norm_mul, Frobenius.pw_mul, ← embed_mul]
  show qmul (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one) (qmul (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one) (qconj (ring (J + 1)) x) (qconj (ring (J + 1)) y))
      (qmul (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one) (Frobenius.pw (ring (J + 1)) (qnorm (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one) x) J, (ring (J + 1)).zero) (Frobenius.pw (ring (J + 1)) (qnorm (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one) y) J, (ring (J + 1)).zero))
    = qmul (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one) (qmul (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one) (qconj (ring (J + 1)) x) (Frobenius.pw (ring (J + 1)) (qnorm (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one) x) J, (ring (J + 1)).zero))
      (qmul (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one) (qconj (ring (J + 1)) y) (Frobenius.pw (ring (J + 1)) (qnorm (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one) y) J, (ring (J + 1)).zero))
  rw [qmul_assoc, ← qmul_assoc (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one) (qconj (ring (J + 1)) y), qmul_comm (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one) (qconj (ring (J + 1)) y),
    qmul_assoc, ← qmul_assoc]

theorem nozd2 (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hJ : J + 1 = 2 * m) (h1 : 1 ≤ J)
    (hn : Frobenius.pw (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one) m = (ring (J + 1)).neg (ring (J + 1)).one) : DetChain.NoZD (F2 J) := fun x y e => by
  by_cases hx : x = (F2 J).zero
  · exact Or.inl hx
  · right
    have hi := iv2_mul J m hpr hJ h1 hn x hx
    calc y = (F2 J).mul (F2 J).one y := ((F2 J).one_mul y).symm
      _ = (F2 J).mul (F2 J).one y := rfl
      _ = (F2 J).mul ((F2 J).mul (iv2 J x) x) y := by rw [hi]
      _ = (F2 J).mul (iv2 J x) ((F2 J).mul x y) := (F2 J).mul_assoc _ _ _
      _ = (F2 J).zero := by rw [e]; exact DetChain.mul_zero (F2 J) _

end Fp2

#print axioms Fp2.sumK_map
#print axioms Fp2.sum_pairsL
#print axioms Fp2.delta_pair
#print axioms Fp2.comp_pairs
#print axioms Fp2.sum_one_n
#print axioms Fp2.card_pairs
#print axioms Fp2.embed_mul
#print axioms Fp2.iv2_zero
#print axioms Fp2.iv2_mul
#print axioms Fp2.iv2_hom
#print axioms Fp2.nozd2

lang/Fp2Count.lean

lean_all/lang/Fp2Count.lean

/-
  lang/Fp2Count — T43, cycle 74: tools for the supersingular count over 𝔽_{p²}, p = 4j + 3.

    key2, key2_inj    an injective order key on 𝔽_p × 𝔽_p: (a, b) ↦ a·p + b
    i2, hi2           i = (0, 1) has i² = −1 in 𝔽_p[i]
    inert_norm        u² + v² = p² ⇒ {u, v} = {p, 0}            (p inert in ℤ[i]: −1 is not a square mod p)

  Lean 4 core only, proofs by hand (lib_nat, lib_ring), no axioms.
-/
import lang.Fp2Inst
import lang.FinSign
import lang.GaussSplit
import lang.CMModular

namespace Fp2Count

open FTAChain ZMod Fp2

variable (j : Nat)

def key2 (x : Zp (2 * (2 * j) + 1 + 1) × Zp (2 * (2 * j) + 1 + 1)) : Nat := x.1.val * (2 * (2 * j) + 1 + 1 + 1) + x.2.val

theorem divmod_unique (P x y x' y' : Nat) (hy : y < P) (hy' : y' < P) (h : x * P + y = x' * P + y') : x = x' ∧ y = y' := by
  have hxx : x = x' := by
    cases Nat.lt_or_ge x x' with
    | inl hl =>
      exfalso
      have a : x * P + y < (x + 1) * P := by rw [Nat.succ_mul]; exact Nat.add_lt_add_left hy _
      have b : (x + 1) * P ≤ x' * P := Nat.mul_le_mul_right P hl
      exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (h ▸ a) (Nat.le_trans b (Nat.le_add_right _ _)))
    | inr hg =>
      cases Nat.lt_or_ge x' x with
      | inl hl =>
        exfalso
        have a : x' * P + y' < (x' + 1) * P := by rw [Nat.succ_mul]; exact Nat.add_lt_add_left hy' _
        have b : (x' + 1) * P ≤ x * P := Nat.mul_le_mul_right P hl
        exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (h ▸ a) (Nat.le_trans b (Nat.le_add_right _ _)))
      | inr hg' => exact Nat.le_antisymm hg' hg
  rw [hxx] at h
  exact ⟨hxx, QRChain.add_left_cancel' _ _ _ h⟩

theorem key2_inj (x y : Zp (2 * (2 * j) + 1 + 1) × Zp (2 * (2 * j) + 1 + 1)) (h : key2 j x = key2 j y) : x = y :=
  match divmod_unique _ _ _ _ _ x.2.property y.2.property h with
  | ⟨h1, h2⟩ => Prod.ext (Subtype.ext h1) (Subtype.ext h2)

def i2 : Zp (2 * (2 * j) + 1 + 1) × Zp (2 * (2 * j) + 1 + 1) := ((ring (2 * (2 * j) + 1 + 1)).zero, (ring (2 * (2 * j) + 1 + 1)).one)

theorem hi2 : (F2 (2 * (2 * j) + 1)).mul (i2 j) (i2 j) = (F2 (2 * (2 * j) + 1)).neg (F2 (2 * (2 * j) + 1)).one := by
  apply Prod.ext
  · show (ring (2 * (2 * j) + 1 + 1)).add ((ring (2 * (2 * j) + 1 + 1)).mul (ring (2 * (2 * j) + 1 + 1)).zero (ring (2 * (2 * j) + 1 + 1)).zero) ((ring (2 * (2 * j) + 1 + 1)).mul ((ring (2 * (2 * j) + 1 + 1)).neg (ring (2 * (2 * j) + 1 + 1)).one) ((ring (2 * (2 * j) + 1 + 1)).mul (ring (2 * (2 * j) + 1 + 1)).one (ring (2 * (2 * j) + 1 + 1)).one)) = (ring (2 * (2 * j) + 1 + 1)).neg (ring (2 * (2 *   -- (+16 chars; full line in the Zenodo source)
    lib_ring
  · show (ring (2 * (2 * j) + 1 + 1)).add ((ring (2 * (2 * j) + 1 + 1)).mul (ring (2 * (2 * j) + 1 + 1)).zero (ring (2 * (2 * j) + 1 + 1)).one) ((ring (2 * (2 * j) + 1 + 1)).mul (ring (2 * (2 * j) + 1 + 1)).one (ring (2 * (2 * j) + 1 + 1)).zero) = (ring (2 * (2 * j) + 1 + 1)).neg (ring (2 * (2 * j) + 1 + 1)).zero
    lib_ring

/-- **p ≡ 3 (mod 4) is inert in ℤ[i]**: u² + v² = p² forces {u, v} = {p, 0}. -/
theorem inert_norm (hpr : ∀ d, Dv d (2 * (2 * j) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * j) + 1 + 1 + 1) (u v : Nat)
    (h : u * u + v * v = (2 * (2 * j) + 1 + 1 + 1) * (2 * (2 * j) + 1 + 1 + 1)) :
    (u = 2 * (2 * j) + 1 + 1 + 1 ∧ v = 0) ∨ (u = 0 ∧ v = 2 * (2 * j) + 1 + 1 + 1) := by
  have hz : (ring (2 * (2 * j) + 1 + 1)).add ((ring (2 * (2 * j) + 1 + 1)).mul (mk _ u) (mk _ u)) ((ring (2 * (2 * j) + 1 + 1)).mul (mk _ v) (mk _ v)) = (ring (2 * (2 * j) + 1 + 1)).zero := by
    rw [TwoPower.mk_mul, TwoPower.mk_mul, PrimeOneModQ.mk_add, h]
    exact Subtype.ext (dv_md_zero _ _ ⟨2 * (2 * j) + 1 + 1 + 1, rfl⟩)
  have hv : mk (2 * (2 * j) + 1 + 1) v = (ring (2 * (2 * j) + 1 + 1)).zero := by
    by_cases hv0 : mk (2 * (2 * j) + 1 + 1) v = (ring (2 * (2 * j) + 1 + 1)).zero
    · exact hv0
    · exfalso
      let w := (ring (2 * (2 * j) + 1 + 1)).mul (mk _ u) (ConicSum.iv (2 * j) (mk _ v))
      have hvi := ConicSum.iv_mul_self (2 * j) hpr _ hv0
      apply GaussSplit.inert_three_mod_four j hpr w
      have hu2 : (ring (2 * (2 * j) + 1 + 1)).mul (mk _ u) (mk _ u) = (ring (2 * (2 * j) + 1 + 1)).neg ((ring (2 * (2 * j) + 1 + 1)).mul (mk _ v) (mk _ v)) := DetChain.neg_unique (ring (2 * (2 * j) + 1 + 1)) _ _ hz
      show (ring (2 * (2 * j) + 1 + 1)).mul ((ring (2 * (2 * j) + 1 + 1)).mul (mk _ u) (ConicSum.iv (2 * j) (mk _ v))) ((ring (2 * (2 * j) + 1 + 1)).mul (mk _ u) (ConicSum.iv (2 * j) (mk _ v))) = _
      rw [show (ring (2 * (2 * j) + 1 + 1)).mul ((ring (2 * (2 * j) + 1 + 1)).mul (mk _ u) (ConicSum.iv (2 * j) (mk _ v))) ((ring (2 * (2 * j) + 1 + 1)).mul (mk _ u) (ConicSum.iv (2 * j) (mk _ v)))
          = (ring (2 * (2 * j) + 1 + 1)).mul ((ring (2 * (2 * j) + 1 + 1)).mul (mk _ u) (mk _ u)) ((ring (2 * (2 * j) + 1 + 1)).mul (ConicSum.iv (2 * j) (mk _ v)) (ConicSum.iv (2 * j) (mk _ v))) by lib_ring, hu2]
      rw [show (ring (2 * (2 * j) + 1 + 1)).mul ((ring (2 * (2 * j) + 1 + 1)).neg ((ring (2 * (2 * j) + 1 + 1)).mul (mk _ v) (mk _ v))) ((ring (2 * (2 * j) + 1 + 1)).mul (ConicSum.iv (2 * j) (mk _ v)) (ConicSum.iv (2 * j) (mk _ v)))
          = (ring (2 * (2 * j) + 1 + 1)).neg ((ring (2 * (2 * j) + 1 + 1)).mul ((ring (2 * (2 * j) + 1 + 1)).mul (ConicSum.iv (2 * j) (mk _ v)) (mk _ v)) ((ring (2 * (2 * j) + 1 + 1)).mul (ConicSum.iv (2 * j) (mk _ v)) (mk _ v))) by lib_ring, hvi]
      lib_ring
  have hu : mk (2 * (2 * j) + 1 + 1) u = (ring (2 * (2 * j) + 1 + 1)).zero := by
    rw [hv, DetChain.mul_zero, DetChain.add_zero'] at hz
    cases nozd _ hpr _ _ hz with
    | inl e => exact e
    | inr e => exact e
  match md_zero_dv _ _ (congrArg Subtype.val hu), md_zero_dv _ _ (congrArg Subtype.val hv) with
  | ⟨u1, hu1⟩, ⟨v1, hv1⟩ =>
    have h1 : (2 * (2 * j) + 1 + 1 + 1) * (2 * (2 * j) + 1 + 1 + 1) * (u1 * u1 + v1 * v1)
        = (2 * (2 * j) + 1 + 1 + 1) * (2 * (2 * j) + 1 + 1 + 1) * 1 := by
      have e1 : (2 * (2 * j) + 1 + 1 + 1) * (2 * (2 * j) + 1 + 1 + 1) * (u1 * u1 + v1 * v1) = u * u + v * v := by
        rw [hu1, hv1]; lib_nat
      rw [e1, h, Nat.mul_one]
    have h2 := Nat.eq_of_mul_eq_mul_left (Nat.mul_pos (Nat.zero_lt_succ _) (Nat.zero_lt_succ _)) h1
    cases u1 with
    | zero =>
      right
      have : v1 * v1 = 1 := by rw [← h2]; lib_nat
      have hv1' : v1 = 1 := by
        cases v1 with
        | zero => exact absurd this (fun e => Nat.noConfusion e)
        | succ v2 =>
          cases v2 with
          | zero => rfl
          | succ v3 => exact absurd this (fun e => by
              have : 4 ≤ (v3 + 1 + 1) * (v3 + 1 + 1) := Nat.mul_le_mul (Nat.le_add_left 2 v3) (Nat.le_add_left 2 v3)
              rw [e] at this; exact Nat.not_succ_le_self 1 (Nat.le_trans (Nat.le_add_left 2 2) this))
      rw [hu1, hv1, hv1']; exact ⟨by lib_nat, by lib_nat⟩
    | succ u2 =>
      left
      have hv0 : v1 = 0 := by
        cases v1 with
        | zero => rfl
        | succ v2 =>
          exfalso
          have : 2 ≤ (u2 + 1) * (u2 + 1) + (v2 + 1) * (v2 + 1) :=
            Nat.add_le_add (Nat.mul_le_mul (Nat.succ_le_succ (Nat.zero_le _)) (Nat.succ_le_succ (Nat.zero_le _)))
              (Nat.mul_le_mul (Nat.succ_le_succ (Nat.zero_le _)) (Nat.succ_le_succ (Nat.zero_le _)))
          rw [h2] at this; exact Nat.not_succ_le_self 1 this
      have hu2 : u2 = 0 := by
        rw [hv0] at h2
        cases u2 with
        | zero => rfl
        | succ u3 =>
          exfalso
          have : 4 ≤ (u3 + 1 + 1) * (u3 + 1 + 1) := Nat.mul_le_mul (Nat.le_add_left 2 u3) (Nat.le_add_left 2 u3)
          rw [show (u3 + 1 + 1) * (u3 + 1 + 1) = (u3 + 1 + 1) * (u3 + 1 + 1) + 0 * 0 by lib_nat, h2] at this
          exact Nat.not_succ_le_self 1 (Nat.le_trans (Nat.le_add_left 2 2) this)
      rw [hu1, hv1, hu2, hv0]; exact ⟨by lib_nat, by lib_nat⟩

end Fp2Count

#print axioms Fp2Count.divmod_unique
#print axioms Fp2Count.key2_inj
#print axioms Fp2Count.hi2
#print axioms Fp2Count.inert_norm

lang/Fp2Euler.lean

lean_all/lang/Fp2Euler.lean

/-
  lang/Fp2Euler — T43, cycle 69: Euler's criterion in 𝔽_{p²} through the norm.

  p = J + 2 = 2m + 1 with −1 a non-residue, F2 = 𝔽_p[i], N(a + bi) = a² + b², h₂ = (p + 1) m = (p² − 1)/2.
    pw_p1         x^(p+1) = (N x, 0)                       (Frobenius = conjugation, QuadFrob.frob_conj)
    pw_h2         x^h₂ = (N(x)^m, 0)
    euler_pm2     x ≠ 0 ⇒ x^h₂ = ±1
    euler_sq2     y ≠ 0 ⇒ (y²)^h₂ = 1
  So the quadratic character of 𝔽_{p²} is χ_p ∘ N — the restriction of the GL1 data of 𝔽_{p²} to 𝔽_p through the norm.

  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Fp2
import lang.Euler

namespace Fp2Euler

open FTAChain ZMod Quad QuadFrob Fp2

variable (J m : Nat)

theorem neg_embed (a : Zp (J + 1)) : (F2 J).neg (a, (ring (J + 1)).zero) = ((ring (J + 1)).neg a, (ring (J + 1)).zero) := by
  apply Prod.ext
  · rfl
  · show (ring (J + 1)).neg (ring (J + 1)).zero = (ring (J + 1)).zero
    exact (DetChain.neg_unique (ring (J + 1)) _ _ ((ring (J + 1)).zero_add (ring (J + 1)).zero)).symm

theorem pw_p1 (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hJ : J + 1 + 1 = 2 * m + 1)
    (hn : Frobenius.pw (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one) m = (ring (J + 1)).neg (ring (J + 1)).one) (x : Zp (J + 1) × Zp (J + 1)) :
    Frobenius.pw (F2 J) x (J + 1 + 1 + 1) = (qnorm (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one) x, (ring (J + 1)).zero) := by
  show (F2 J).mul (Frobenius.pw (F2 J) x (J + 1 + 1)) x = _
  rw [show Frobenius.pw (F2 J) x (J + 1 + 1) = Frobenius.pw (quad (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one)) (x.1, x.2) (J + 1 + 1) from rfl,
    frob_conj J ((ring (J + 1)).neg (ring (J + 1)).one) hpr m hJ hn x.1 x.2, (F2 J).mul_comm]
  exact mul_self_norm (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one) x

theorem pw_h2 (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hJ : J + 1 + 1 = 2 * m + 1)
    (hn : Frobenius.pw (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one) m = (ring (J + 1)).neg (ring (J + 1)).one) (x : Zp (J + 1) × Zp (J + 1)) :
    Frobenius.pw (F2 J) x ((J + 1 + 1 + 1) * m) = (Frobenius.pw (ring (J + 1)) (qnorm (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one) x) m, (ring (J + 1)).zero) := by
  rw [← TwoPower.pwpow, pw_p1 J m hpr hJ hn]
  exact pw_embed J ((ring (J + 1)).neg (ring (J + 1)).one) _ m

theorem euler_pm2 (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hJ : J + 1 + 1 = 2 * m + 1) (h1 : 1 ≤ J)
    (hn : Frobenius.pw (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one) m = (ring (J + 1)).neg (ring (J + 1)).one) (x : Zp (J + 1) × Zp (J + 1)) (hx : x ≠ (F2 J).zero) :
    Frobenius.pw (F2 J) x ((J + 1 + 1 + 1) * m) = (F2 J).one ∨
      Frobenius.pw (F2 J) x ((J + 1 + 1 + 1) * m) = (F2 J).neg (F2 J).one := by
  have hJ' : J + 1 = 2 * m := Nat.succ.inj (hJ.trans rfl)
  have hN := norm_ne_zero J ((ring (J + 1)).neg (ring (J + 1)).one) hpr m hJ' h1 hn x.1 x.2 hx
  have hsq : (ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) (qnorm (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one) x) m) (Frobenius.pw (ring (J + 1)) (qnorm (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one) x) m) = (ring (J + 1)).one := by
    rw [← HasseCM.pw_add', show m + m = J + 1 by rw [hJ']; lib_nat]
    exact unit_pow J hpr _ hN
  rw [pw_h2 J m hpr hJ hn]
  cases Euler.sq_one (ring (J + 1)) (nozd J hpr) _ hsq with
  | inl h => left; rw [h]; rfl
  | inr h => right; rw [h]; show ((ring (J + 1)).neg (ring (J + 1)).one, (ring (J + 1)).zero) = (F2 J).neg ((ring (J + 1)).one, (ring (J + 1)).zero); rw [neg_embed]

theorem euler_sq2 (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hJ : J + 1 + 1 = 2 * m + 1) (h1 : 1 ≤ J)
    (hn : Frobenius.pw (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one) m = (ring (J + 1)).neg (ring (J + 1)).one) (y : Zp (J + 1) × Zp (J + 1)) (hy : y ≠ (F2 J).zero) :
    Frobenius.pw (F2 J) ((F2 J).mul y y) ((J + 1 + 1 + 1) * m) = (F2 J).one := by
  rw [Frobenius.pw_mul]
  cases euler_pm2 J m hpr hJ h1 hn y hy with
  | inl h => rw [h, (F2 J).one_mul]
  | inr h => rw [h, DetChain.neg_mul_left, (F2 J).one_mul, DetChain.neg_neg']

end Fp2Euler

#print axioms Fp2Euler.neg_embed
#print axioms Fp2Euler.pw_p1
#print axioms Fp2Euler.pw_h2
#print axioms Fp2Euler.euler_pm2
#print axioms Fp2Euler.euler_sq2

lang/Fp2Inst.lean

lean_all/lang/Fp2Inst.lean

/-
  lang/Fp2Inst — T43, cycle 72: 𝔽_{p²} = 𝔽_p[i] (p ≡ 3 mod 4) as an instance of the finite-field interface FinField.FF.

  p = 2k + 3 with (−1)^(k+1) = −1; q = p², h = (q − 1)/2 = (2k + 4)(k + 1).
    ff2     the FF structure: the p² pairs (Fp2), no zero divisors and inverses (Fp2), Euler's criterion through the
            norm (Fp2Euler), square roots (Fp2Sqrt), a non-residue 1 + b i (Fp2Roots)
  So every character-sum theorem proved for FF (FinConic, FinMoment, FinClass) holds over 𝔽_{p²}.

  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.Fp2Sqrt
import lang.FinClass

namespace Fp2Inst

open FTAChain ZMod Quad Fp2 Fp2Euler

def ff2 (k : Nat) (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = 2 * k + 1 + 1 + 1)
    (hn : Frobenius.pw (ring (2 * k + 1 + 1)) ((ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) (k + 1) = (ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) :
    FinField.FF (Zp (2 * k + 1 + 1) × Zp (2 * k + 1 + 1)) where
  K := F2 (2 * k + 1)
  els := pairs (2 * k + 1)
  comp := comp_pairs (2 * k + 1)
  h := (2 * k + 1 + 1 + 1 + 1) * (k + 1)
  card := by
    rw [card_pairs]
    show Int.ofNat ((2 * k + 1 + 1 + 1) * (2 * k + 1 + 1 + 1)) = Int.ofNat (2 * ((2 * k + 1 + 1 + 1 + 1) * (k + 1)) + 1)
    rw [show (2 * k + 1 + 1 + 1) * (2 * k + 1 + 1 + 1) = 2 * ((2 * k + 1 + 1 + 1 + 1) * (k + 1)) + 1 by lib_nat]
  nozd := nozd2 (2 * k + 1) (k + 1) hpr (by lib_nat) (Nat.le_add_left 1 _) hn
  inv := fun x hx => ⟨iv2 (2 * k + 1) x, by
    rw [(F2 (2 * k + 1)).mul_comm]; exact iv2_mul (2 * k + 1) (k + 1) hpr (by lib_nat) (Nat.le_add_left 1 _) hn x hx⟩
  euler_pm := fun x hx => euler_pm2 (2 * k + 1) (k + 1) hpr (by lib_nat) (Nat.le_add_left 1 _) hn x hx
  euler_sq := fun y hy => euler_sq2 (2 * k + 1) (k + 1) hpr (by lib_nat) (Nat.le_add_left 1 _) hn y hy
  euler_hard := fun x hx => Fp2Sqrt.euler_hard2 k hpr hn x hx
  ne_pm := fun e => QChar.ne_pm k (congrArg Prod.fst e)
  nonres := Fp2Roots.nonres2 k hpr hn
  iv := iv2 (2 * k + 1)
  iv_mul := fun x hx => iv2_mul (2 * k + 1) (k + 1) hpr (by lib_nat) (Nat.le_add_left 1 _) hn x hx
  iv_zero := iv2_zero (2 * k + 1)
  iv_hom := iv2_hom (2 * k + 1)

end Fp2Inst

#print axioms Fp2Inst.ff2

lang/Fp2Roots.lean

lean_all/lang/Fp2Roots.lean

/-
  lang/Fp2Roots — T43, cycle 70: a non-residue of 𝔽_{p²}.

  p = 2k + 3, −1 a non-residue ((−1)^(k+1) = −1), F2 = 𝔽_p[i], N(1 + b i) = 1 + b².
    search          for every j: some b has 1 + b² a non-residue, or mk j is a square   (0, 1, 2, … step by y² ↦ 1 + y²)
    one_plus_sq     some b has 1 + b² a non-residue
    nonres2         x = 1 + b i is a non-residue of 𝔽_{p²}: x^((p²−1)/2) ≠ 1
  Next: square roots in 𝔽_p[i] and the FF instance.

  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Fp2Euler
import lang.EulerHard
import lang.QChar

namespace Fp2Roots

open FTAChain ZMod YMChain Quad QuadFrob Fp2 Fp2Euler

variable (k : Nat)

abbrev NR (b : Zp (2 * k + 1 + 1)) : Prop :=
  Frobenius.pw (ring (2 * k + 1 + 1)) ((ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one ((ring (2 * k + 1 + 1)).mul b b)) (k + 1) ≠ (ring (2 * k + 1 + 1)).one ∧ (ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one ((ring (2 * k + 1 + 1)).mul b b) ≠ (ring (2 * k + 1 + 1)).zero

theorem search (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = 2 * k + 1 + 1 + 1) :
    ∀ j, (∃ b, NR k b) ∨ (∃ y, (ring (2 * k + 1 + 1)).mul y y = mk (2 * k + 1 + 1) j)
  | 0 => Or.inr ⟨(ring (2 * k + 1 + 1)).zero, DetChain.mul_zero (ring (2 * k + 1 + 1)) _⟩
  | j + 1 => by
    cases search hpr j with
    | inl h => exact Or.inl h
    | inr h =>
      match h with
      | ⟨y, hy⟩ =>
        by_cases hP : NR k y
        · exact Or.inl ⟨y, hP⟩
        · right
          have hsq : ∃ z, (ring (2 * k + 1 + 1)).mul z z = (ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one ((ring (2 * k + 1 + 1)).mul y y) := by
            by_cases h0 : (ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one ((ring (2 * k + 1 + 1)).mul y y) = (ring (2 * k + 1 + 1)).zero
            · exact ⟨(ring (2 * k + 1 + 1)).zero, by rw [h0]; exact DetChain.mul_zero (ring (2 * k + 1 + 1)) _⟩
            · have h1 : Frobenius.pw (ring (2 * k + 1 + 1)) ((ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one ((ring (2 * k + 1 + 1)).mul y y)) (k + 1) = (ring (2 * k + 1 + 1)).one := by
                by_cases hh : Frobenius.pw (ring (2 * k + 1 + 1)) ((ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one ((ring (2 * k + 1 + 1)).mul y y)) (k + 1) = (ring (2 * k + 1 + 1)).one
                · exact hh
                · exact absurd ⟨hh, h0⟩ hP
              exact EulerHard.euler_hard k hpr _ h1
          match hsq with
          | ⟨z, hz⟩ =>
            refine ⟨z, ?_⟩
            rw [hz, ← TwoPower.mk_add_one, ← hy, (ring (2 * k + 1 + 1)).add_comm]

theorem one_plus_sq (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = 2 * k + 1 + 1 + 1) : ∃ b, NR k b := by
  match PowerSum.exists_nonroot (2 * k + 1) hpr k (by rw [Nat.two_mul]; exact Nat.succ_le_succ (Nat.le_add_left k k)) with
  | ⟨n, hn0, hn⟩ =>
    cases search k hpr n.val with
    | inl h => exact h
    | inr h =>
      exfalso
      match h with
      | ⟨y, hy⟩ =>
        rw [mk_val] at hy
        have hy0 : y ≠ (ring (2 * k + 1 + 1)).zero := fun e => hn0 (by rw [← hy, e]; exact DetChain.mul_zero (ring (2 * k + 1 + 1)) _)
        exact hn (by rw [← hy]; exact Euler.euler_square k hpr y hy0)

/-- **A non-residue of 𝔽_{p²}.** -/
theorem nonres2 (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = 2 * k + 1 + 1 + 1)
    (hn : Frobenius.pw (ring (2 * k + 1 + 1)) ((ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) (k + 1) = (ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) :
    ∃ x, x ≠ (F2 (2 * k + 1)).zero ∧ Frobenius.pw (F2 (2 * k + 1)) x ((2 * k + 1 + 1 + 1 + 1) * (k + 1)) ≠ (F2 (2 * k + 1)).one := by
  match one_plus_sq k hpr with
  | ⟨b, hb1, _⟩ =>
    refine ⟨((ring (2 * k + 1 + 1)).one, b), fun e => PowerSum.one_ne_zero' (2 * k + 1) (congrArg Prod.fst e), ?_⟩
    rw [pw_h2 (2 * k + 1) (k + 1) hpr (by lib_nat) hn]
    intro e
    apply hb1
    have := congrArg Prod.fst e
    have hN : qnorm (ring (2 * k + 1 + 1)) ((ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) ((ring (2 * k + 1 + 1)).one, b) = (ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one ((ring (2 * k + 1 + 1)).mul b b) := by
      show (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul (ring (2 * k + 1 + 1)).one (ring (2 * k + 1 + 1)).one) ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) ((ring (2 * k + 1 + 1)).mul b b))) = _
      lib_ring
    rw [hN] at this
    exact this

end Fp2Roots

#print axioms Fp2Roots.search
#print axioms Fp2Roots.one_plus_sq
#print axioms Fp2Roots.nonres2

lang/Fp2S.lean

lean_all/lang/Fp2S.lean

/-
  lang/Fp2S — T48, cycle 826 (step S2 of 256a at p ≡ 3 mod 8): **the 8th root of unity s in 𝔽_p[i]**.
  Over any commutative ring K, in K[i] = Quad.quad K (−1):
    i_sq     i² + 1 = 0 for i = (0, 1)
    s_sq     if r² + 2 = 0 and 2h = 1 in K, then s = (rh, rh) satisfies s² + i = 0
    frob_s   s^(8k+3) = −i·s      (Frob8.frob3: the Frobenius of 𝔽_(p²) at p = 8k + 3)
    frob_i   i^(8k+3) = −i        (Frob8.frobi3)
  For p ≡ 3 (mod 8), −2 is a square in 𝔽_p, so r exists and 𝔽_(p²) = 𝔽_p[i] contains s with s² = −i.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Frob8
import lang.Quad

namespace Fp2S

open Quad

variable {R : Type} (K : DetChain.CRing R)

/-- K[i] with i² = −1. -/
abbrev Ki : DetChain.CRing (R × R) := quad K (K.neg K.one)

/-- **i² + 1 = 0** for i = (0, 1). -/
theorem i_sq : (Ki K).add ((Ki K).mul (K.zero, K.one) (K.zero, K.one)) (Ki K).one = (Ki K).zero := by
  apply Prod.ext
  · show K.add (K.add (K.mul K.zero K.zero) (K.mul (K.neg K.one) (K.mul K.one K.one))) K.one = K.zero
    lib_ring
  · show K.add (K.add (K.mul K.zero K.one) (K.mul K.one K.zero)) K.zero = K.zero
    lib_ring

/-- **s² + i = 0** for s = (rh, rh), when r² + 2 = 0 and 2h = 1. -/
theorem s_sq (r h : R) (hr : K.add (K.mul r r) (K.add K.one K.one) = K.zero) (hh : K.add h h = K.one) :
    (Ki K).add ((Ki K).mul (K.mul r h, K.mul r h) (K.mul r h, K.mul r h)) (K.zero, K.one) = (Ki K).zero := by
  have hr' : K.mul r r = K.neg (K.add K.one K.one) := DetChain.neg_unique K _ _ hr
  apply Prod.ext
  · show K.add (K.add (K.mul (K.mul r h) (K.mul r h)) (K.mul (K.neg K.one) (K.mul (K.mul r h) (K.mul r h)))) K.zero = K.zero
    lib_ring
  · show K.add (K.add (K.mul (K.mul r h) (K.mul r h)) (K.mul (K.mul r h) (K.mul r h))) K.one = K.zero
    calc K.add (K.add (K.mul (K.mul r h) (K.mul r h)) (K.mul (K.mul r h) (K.mul r h))) K.one
        = K.add (K.mul (K.mul r r) (K.add (K.mul h h) (K.mul h h))) K.one := by lib_ring
      _ = K.add (K.mul (K.neg (K.add K.one K.one)) (K.add (K.mul h h) (K.mul h h))) K.one := by rw [hr']
      _ = K.add (K.neg (K.mul (K.add h h) (K.add h h))) K.one := by lib_ring
      _ = K.add (K.neg (K.mul K.one K.one)) K.one := by rw [hh]
      _ = K.zero := by lib_ring

/-- **the Frobenius at p = 8k + 3 sends s to −i·s** in K[i]. -/
theorem frob_s (r h : R) (hr : K.add (K.mul r r) (K.add K.one K.one) = K.zero) (hh : K.add h h = K.one) (k : Nat) :
    Frobenius.pw (Ki K) (K.mul r h, K.mul r h) (8 * k + 3) = (Ki K).neg ((Ki K).mul (K.zero, K.one) (K.mul r h, K.mul r h)) :=
  Frob8.frob3 (Ki K) (K.zero, K.one) (K.mul r h, K.mul r h) (i_sq K) (s_sq K r h hr hh) k

/-- **and i to −i**. -/
theorem frob_i (k : Nat) : Frobenius.pw (Ki K) (K.zero, K.one) (8 * k + 3) = (Ki K).neg (K.zero, K.one) :=
  Frob8.frobi3 (Ki K) (K.zero, K.one) (i_sq K) k

end Fp2S

#print axioms Fp2S.i_sq
#print axioms Fp2S.s_sq
#print axioms Fp2S.frob_s
#print axioms Fp2S.frob_i

lang/Fp2Sqrt.lean

lean_all/lang/Fp2Sqrt.lean

/-
  lang/Fp2Sqrt — T43, cycle 71: square roots in 𝔽_{p²} = 𝔽_p[i] (Euler's criterion, hard half).

  p = 2k + 3, −1 a non-residue. If x = a + bi has x^((p²−1)/2) = 1 then N(x) = a² + b² = s² in 𝔽_p and
    b = 0:  x = a = c² or x = a = (d i)² with −a = d²
    b ≠ 0:  u± = (a ± s)/2 have u₊u₋ = −b²/4, a non-residue, so one of them is u = c²; then y = c + (b/(2c)) i
            satisfies y² = (c² − b²/(4u)) + b i = a + b i  (u² − a u − b²/4 = 0)
    euler_hard2   ∃ y, y² = x
  Completes Euler's criterion in 𝔽_{p²} (with Fp2Euler); next the FF instance and the point count of y² = x³ − x.

  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Fp2Roots

namespace Fp2Sqrt

open FTAChain ZMod YMChain Quad QuadFrob Fp2 Fp2Euler

variable (k : Nat)

theorem two_ne : (ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one (ring (2 * k + 1 + 1)).one ≠ (ring (2 * k + 1 + 1)).zero := fun e =>
  QChar.ne_pm k (DetChain.neg_unique (ring (2 * k + 1 + 1)) _ _ e).symm

theorem sq_embed (c : Zp (2 * k + 1 + 1)) : (F2 (2 * k + 1)).mul (c, (ring (2 * k + 1 + 1)).zero) (c, (ring (2 * k + 1 + 1)).zero) = ((ring (2 * k + 1 + 1)).mul c c, (ring (2 * k + 1 + 1)).zero) :=
  embed_mul (2 * k + 1) c c

theorem sq_imag (d : Zp (2 * k + 1 + 1)) :
    (F2 (2 * k + 1)).mul ((ring (2 * k + 1 + 1)).zero, d) ((ring (2 * k + 1 + 1)).zero, d) = ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul d d), (ring (2 * k + 1 + 1)).zero) := by
  apply Prod.ext
  · show (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul (ring (2 * k + 1 + 1)).zero (ring (2 * k + 1 + 1)).zero) ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) ((ring (2 * k + 1 + 1)).mul d d)) = (ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul d d)
    lib_ring
  · show (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul (ring (2 * k + 1 + 1)).zero d) ((ring (2 * k + 1 + 1)).mul d (ring (2 * k + 1 + 1)).zero) = (ring (2 * k + 1 + 1)).zero
    lib_ring

theorem sq_gen (c d : Zp (2 * k + 1 + 1)) :
    (F2 (2 * k + 1)).mul (c, d) (c, d) = ((ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul c c) ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul d d)), (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one (ring (2 * k + 1 + 1)).one) ((ring (2 * k + 1 + 1)).mul c d)) := by
  apply Prod.ext
  · show (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul c c) ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) ((ring (2 * k + 1 + 1)).mul d d)) = (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul c c) ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul d d))
    lib_ring
  · show (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul c d) ((ring (2 * k + 1 + 1)).mul d c) = (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one (ring (2 * k + 1 + 1)).one) ((ring (2 * k + 1 + 1)).mul c d)
    lib_ring

/-- the key quadratic: with 2t = 1 and s² = a² + b², u = (a + s)t satisfies u² − a u − b² t² = 0. -/
theorem quad_rel (a b s t : Zp (2 * k + 1 + 1)) (ht : (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one (ring (2 * k + 1 + 1)).one) t = (ring (2 * k + 1 + 1)).one)
    (hs : (ring (2 * k + 1 + 1)).mul s s = (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul a a) ((ring (2 * k + 1 + 1)).mul b b)) :
    (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s) t) ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s) t)) ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul a ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s) t))))
      ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul b b) ((ring (2 * k + 1 + 1)).mul t t))) = (ring (2 * k + 1 + 1)).zero := by
  have e : (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s) t) ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s) t)) ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul a ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s) t))))
      ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul b b) ((ring (2 * k + 1 + 1)).mul t t)))
      = (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul t t) ((ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul s s) ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul a a) ((ring (2 * k + 1 + 1)).mul b b)))))
          ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul a ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s) t)) ((ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one (ring (2 * k + 1 + 1)).one) t) ((ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one))) := by
    lib_ring
  rw [e, hs, ht, (ring (2 * k + 1 + 1)).add_neg, (ring (2 * k + 1 + 1)).add_neg, DetChain.mul_zero, DetChain.mul_zero, (ring (2 * k + 1 + 1)).zero_add]

theorem neg_neg_one_pw (hn : Frobenius.pw (ring (2 * k + 1 + 1)) ((ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) (k + 1) = (ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) (a : Zp (2 * k + 1 + 1))
    (ha : Frobenius.pw (ring (2 * k + 1 + 1)) a (k + 1) = (ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) : Frobenius.pw (ring (2 * k + 1 + 1)) ((ring (2 * k + 1 + 1)).neg a) (k + 1) = (ring (2 * k + 1 + 1)).one := by
  rw [show (ring (2 * k + 1 + 1)).neg a = (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) a by lib_ring, Frobenius.pw_mul, hn, ha]
  lib_ring

/-- **Square roots in 𝔽_p[i].** -/
theorem euler_hard2 (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = 2 * k + 1 + 1 + 1)
    (hn : Frobenius.pw (ring (2 * k + 1 + 1)) ((ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) (k + 1) = (ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) (x : Zp (2 * k + 1 + 1) × Zp (2 * k + 1 + 1))
    (hx : Frobenius.pw (F2 (2 * k + 1)) x ((2 * k + 1 + 1 + 1 + 1) * (k + 1)) = (F2 (2 * k + 1)).one) :
    ∃ y, (F2 (2 * k + 1)).mul y y = x := by
  match x with
  | (a, b) =>
  rw [pw_h2 (2 * k + 1) (k + 1) hpr (by lib_nat) hn] at hx
  have hN : Frobenius.pw (ring (2 * k + 1 + 1)) ((ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul a a) ((ring (2 * k + 1 + 1)).mul b b)) (k + 1) = (ring (2 * k + 1 + 1)).one := by
    have := congrArg Prod.fst hx
    have e : qnorm (ring (2 * k + 1 + 1)) ((ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) (a, b) = (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul a a) ((ring (2 * k + 1 + 1)).mul b b) := by
      show (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul a a) ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) ((ring (2 * k + 1 + 1)).mul b b))) = _; lib_ring
    rw [e] at this; exact this
  by_cases hb : b = (ring (2 * k + 1 + 1)).zero
  · by_cases ha : a = (ring (2 * k + 1 + 1)).zero
    · exact ⟨((ring (2 * k + 1 + 1)).zero, (ring (2 * k + 1 + 1)).zero), by rw [ha, hb]; exact DetChain.mul_zero (F2 (2 * k + 1)) _⟩
    · cases Euler.euler_pm k hpr a ha with
      | inl h =>
        match EulerHard.euler_hard k hpr a h with
        | ⟨c, hc⟩ => exact ⟨(c, (ring (2 * k + 1 + 1)).zero), by rw [sq_embed, hc, hb]⟩
      | inr h =>
        match EulerHard.euler_hard k hpr _ (neg_neg_one_pw k hn a h) with
        | ⟨d, hd⟩ => exact ⟨((ring (2 * k + 1 + 1)).zero, d), by rw [sq_imag, hd, DetChain.neg_neg', hb]⟩
  · match EulerHard.euler_hard k hpr _ hN with
    | ⟨s, hs⟩ =>
      have h2 := two_ne k
      let t := ConicSum.iv k ((ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one (ring (2 * k + 1 + 1)).one)
      have ht : (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one (ring (2 * k + 1 + 1)).one) t = (ring (2 * k + 1 + 1)).one := by
        rw [(ring (2 * k + 1 + 1)).mul_comm]; exact ConicSum.iv_mul_self k hpr _ h2
      have ht0 : t ≠ (ring (2 * k + 1 + 1)).zero := fun e => by rw [e, DetChain.mul_zero] at ht; exact PowerSum.one_ne_zero' _ ht.symm
      -- choose s' = ±s with ((a + s') t)^(k+1) = 1
      have choose : ∃ s', (ring (2 * k + 1 + 1)).mul s' s' = (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul a a) ((ring (2 * k + 1 + 1)).mul b b) ∧
          Frobenius.pw (ring (2 * k + 1 + 1)) ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s') t) (k + 1) = (ring (2 * k + 1 + 1)).one := by
        have hprod : (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s) t) ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a ((ring (2 * k + 1 + 1)).neg s)) t)
            = (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul b t) ((ring (2 * k + 1 + 1)).mul b t)) := by
          have e : (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s) t) ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a ((ring (2 * k + 1 + 1)).neg s)) t)
              = (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul b t) ((ring (2 * k + 1 + 1)).mul b t)))
                  ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul t t) ((ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul a a) ((ring (2 * k + 1 + 1)).mul b b)) ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul s s)))) := by lib_ring
          rw [e, hs, (ring (2 * k + 1 + 1)).add_neg, DetChain.mul_zero, DetChain.add_zero']
        have hbt : (ring (2 * k + 1 + 1)).mul b t ≠ (ring (2 * k + 1 + 1)).zero := fun e => by
          cases nozd (2 * k + 1) hpr _ _ e with
          | inl h => exact hb h
          | inr h => exact ht0 h
        have hp : Frobenius.pw (ring (2 * k + 1 + 1)) ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s) t) ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a ((ring (2 * k + 1 + 1)).neg s)) t)) (k + 1) = (ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one := by
          rw [hprod, Frobenius.pw_mul, hn, Euler.euler_square k hpr _ hbt]; lib_ring
        rw [Frobenius.pw_mul] at hp
        have hu1 : (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s) t ≠ (ring (2 * k + 1 + 1)).zero := fun e => by
          rw [e, HasseCM.pw_zero_succ, DetChain.zero_mul'] at hp
          apply PowerSum.one_ne_zero' (2 * k + 1)
          have := congrArg (fun z => (ring (2 * k + 1 + 1)).add z (ring (2 * k + 1 + 1)).one) hp
          exact ((ring (2 * k + 1 + 1)).zero_add (ring (2 * k + 1 + 1)).one).symm.trans (this.trans (DetChain.add_neg' (ring (2 * k + 1 + 1)) (ring (2 * k + 1 + 1)).one))
        cases Euler.euler_pm k hpr _ hu1 with
        | inl h1 => exact ⟨s, hs, h1⟩
        | inr h1 =>
          refine ⟨(ring (2 * k + 1 + 1)).neg s, by rw [Thue.negsq (ring (2 * k + 1 + 1))]; exact hs, ?_⟩
          rw [h1] at hp
          have : Frobenius.pw (ring (2 * k + 1 + 1)) ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a ((ring (2 * k + 1 + 1)).neg s)) t) (k + 1)
              = (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) (Frobenius.pw (ring (2 * k + 1 + 1)) ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a ((ring (2 * k + 1 + 1)).neg s)) t) (k + 1))) := by lib_ring
          rw [this, hp]; lib_ring
      match choose with
      | ⟨s', hs', hu⟩ =>
        match EulerHard.euler_hard k hpr _ hu with
        | ⟨c, hc⟩ =>
          have hu0 : (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s') t ≠ (ring (2 * k + 1 + 1)).zero := fun e => by
            rw [e, HasseCM.pw_zero_succ] at hu; exact PowerSum.one_ne_zero' _ hu.symm
          have hc0 : c ≠ (ring (2 * k + 1 + 1)).zero := fun e => hu0 (by rw [← hc, e]; exact DetChain.mul_zero (ring (2 * k + 1 + 1)) _)
          have hci := ConicSum.iv_mul_self k hpr c hc0
          refine ⟨(c, (ring (2 * k + 1 + 1)).mul b ((ring (2 * k + 1 + 1)).mul t (ConicSum.iv k c))), ?_⟩
          rw [sq_gen]
          apply Prod.ext
          · show (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul c c) ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul b ((ring (2 * k + 1 + 1)).mul t (ConicSum.iv k c))) ((ring (2 * k + 1 + 1)).mul b ((ring (2 * k + 1 + 1)).mul t (ConicSum.iv k c))))) = a
            have hq := quad_rel k a b s' t ht hs'
            generalize hU : (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s') t = U at hq hc
            have hiu : (ring (2 * k + 1 + 1)).mul (ConicSum.iv k c) (ConicSum.iv k c) = ConicSum.iv k U := by
              rw [← hc, ConicSum.inv_mul]
            have hUi : (ring (2 * k + 1 + 1)).mul U (ConicSum.iv k U) = (ring (2 * k + 1 + 1)).one := by
              rw [← hiu, ← hc]
              rw [show (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul c c) ((ring (2 * k + 1 + 1)).mul (ConicSum.iv k c) (ConicSum.iv k c))
                  = (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul (ConicSum.iv k c) c) ((ring (2 * k + 1 + 1)).mul (ConicSum.iv k c) c) by lib_ring, hci, (ring (2 * k + 1 + 1)).one_mul]
            rw [hc]
            have e : (ring (2 * k + 1 + 1)).add U ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul b ((ring (2 * k + 1 + 1)).mul t (ConicSum.iv k c))) ((ring (2 * k + 1 + 1)).mul b ((ring (2 * k + 1 + 1)).mul t (ConicSum.iv k c)))))
                = (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul U U) ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul a U))) ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul b b) ((ring (2 * k + 1 + 1)).mul t t)))) (ConicSum.iv k U))
                    ((ring (2 * k + 1 + 1)).mul a ((ring (2 * k + 1 + 1)).mul U (ConicSum.iv k U))))
                    ((ring (2 * k + 1 + 1)).mul U ((ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul U (ConicSum.iv k U))))) := by
              rw [show (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul b ((ring (2 * k + 1 + 1)).mul t (ConicSum.iv k c))) ((ring (2 * k + 1 + 1)).mul b ((ring (2 * k + 1 + 1)).mul t (ConicSum.iv k c)))
                  = (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul b b) ((ring (2 * k + 1 + 1)).mul t t)) ((ring (2 * k + 1 + 1)).mul (ConicSum.iv k c) (ConicSum.iv k c)) by lib_ring, hiu]
              lib_ring
            rw [e, show (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul U U) ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul a U))) ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul b b) ((ring (2 * k + 1 + 1)).mul t t))) = (ring (2 * k + 1 + 1)).zero by
                  rw [← hq], hUi, (ring (2 * k + 1 + 1)).add_neg]
            lib_ring
          · show (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one (ring (2 * k + 1 + 1)).one) ((ring (2 * k + 1 + 1)).mul c ((ring (2 * k + 1 + 1)).mul b ((ring (2 * k + 1 + 1)).mul t (ConicSum.iv k c)))) = b
            have e : (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one (ring (2 * k + 1 + 1)).one) ((ring (2 * k + 1 + 1)).mul c ((ring (2 * k + 1 + 1)).mul b ((ring (2 * k + 1 + 1)).mul t (ConicSum.iv k c))))
                = (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul b ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one (ring (2 * k + 1 + 1)).one) t)) ((ring (2 * k + 1 + 1)).mul (ConicSum.iv k c) c) := by lib_ring
            rw [e, ht, hci]; lib_ring

end Fp2Sqrt

#print axioms Fp2Sqrt.two_ne
#print axioms Fp2Sqrt.sq_embed
#print axioms Fp2Sqrt.sq_imag
#print axioms Fp2Sqrt.sq_gen
#print axioms Fp2Sqrt.quad_rel
#print axioms Fp2Sqrt.neg_neg_one_pw
#print axioms Fp2Sqrt.euler_hard2

lang/FpEmb.lean

lean_all/lang/FpEmb.lean

/-
  lang/FpEmb — T49 (residue fields of degree f > 1): **the embedding 𝔽_p → F into any ring of characteristic p**,
  a ↦ a·1. With lang/CycBase it carries 𝔽_p[C_ℓ]/(N) into F[C_ℓ]/(N), so a field F ⊇ 𝔽_p containing an element of
  order ℓ gives the residue map at a prime above p of any degree.
    emb_md     (md n)·1 = n·1          (p·1 = 0)
    emb_add, emb_mul, emb_one          a ↦ a·1 is a ring map 𝔽_p → F
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.Frobenius
import lang.ZMod

namespace FpEmb

open FTAChain ZMod DetChain Frobenius

variable {T : Type} (F : CRing T) (m : Nat) (hp : nsm F (m + 1) F.one = F.zero)

/-- a ↦ a·1. -/
def emb (a : Zp m) : T := nsm F a.val F.one

include hp in
theorem emb_md (n : Nat) : nsm F (md m n) F.one = nsm F n F.one := by
  obtain ⟨q, hq⟩ := md_spec m n
  calc nsm F (md m n) F.one = F.add (nsm F (md m n) F.one) F.zero := by
        rw [F.add_comm]
        exact (F.zero_add _).symm
    _ = F.add (nsm F (md m n) F.one) (nsm F (q * (m + 1)) F.one) := by rw [nsm_char F (m + 1) q hp F.one]
    _ = nsm F (md m n + q * (m + 1)) F.one := (nsm_add F _ _ _).symm
    _ = nsm F n F.one := by rw [← hq]

include hp in
theorem emb_add (a b : Zp m) : emb F m ((ring m).add a b) = F.add (emb F m a) (emb F m b) := by
  show nsm F (md m (a.val + b.val)) F.one = _
  rw [emb_md F m hp]
  exact nsm_add F a.val b.val F.one

include hp in
theorem emb_mul (a b : Zp m) : emb F m ((ring m).mul a b) = F.mul (emb F m a) (emb F m b) := by
  show nsm F (md m (a.val * b.val)) F.one = _
  rw [emb_md F m hp, nsm_mul_nat F a.val F.one b.val, nsm_eq_mul F b.val]
  exact F.mul_comm _ _

include hp in
theorem emb_one : emb F m (ring m).one = F.one := by
  show nsm F (md m 1) F.one = F.one
  rw [emb_md F m hp]
  exact nsm_one F F.one

end FpEmb

#print axioms FpEmb.emb_md
#print axioms FpEmb.emb_add
#print axioms FpEmb.emb_mul
#print axioms FpEmb.emb_one

lang/Fq2.lean

lean_all/lang/Fq2.lean

/-
  lang/Fq2 — T43, cycle 88: 𝔽_{p²} = 𝔽_p[√n] for any non-residue n (generalising Fp2/Fp2Euler from n = −1).

  p = J + 2 = 2m + 1, n^m = −1, G2 = quad 𝔽_p n, N(a + b√n) = a² − n b², h₂ = (p + 1) m.
    iv, iv_zero, iv_mul, iv_hom, nozd     inverses x⁻¹ = x̄ · N(x)^(p−2), no zero divisors
    pw_p1, pw_h2                          x^(p+1) = N(x),  x^h₂ = N(x)^m
    euler_pm, euler_sq                    x^h₂ = ±1 (x ≠ 0),  (y²)^h₂ = 1
  The element list is Fp2.pairs (independent of n). This removes the restriction p ≡ 3 (mod 4): with n a non-residue
  (e.g. found by Euler's criterion) every 𝔽_{p²} is available, the input for point counts over 𝔽_{p²} at all p.

  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Fp2Euler

namespace Fq2

open FTAChain ZMod Quad QuadFrob Fp2

variable (J : Nat) (n : Zp (J + 1)) (m : Nat)

abbrev G2 : DetChain.CRing (Zp (J + 1) × Zp (J + 1)) := quad (ring (J + 1)) n

def iv (x : Zp (J + 1) × Zp (J + 1)) : Zp (J + 1) × Zp (J + 1) :=
  (G2 J n).mul (qconj (ring (J + 1)) x) (Frobenius.pw (ring (J + 1)) (qnorm (ring (J + 1)) n x) J, (ring (J + 1)).zero)

theorem embed_mul (a b : Zp (J + 1)) : (G2 J n).mul (a, (ring (J + 1)).zero) (b, (ring (J + 1)).zero) = ((ring (J + 1)).mul a b, (ring (J + 1)).zero) := by
  apply Prod.ext
  · show (ring (J + 1)).add ((ring (J + 1)).mul a b) ((ring (J + 1)).mul n ((ring (J + 1)).mul (ring (J + 1)).zero (ring (J + 1)).zero)) = (ring (J + 1)).mul a b
    lib_ring
  · show (ring (J + 1)).add ((ring (J + 1)).mul a (ring (J + 1)).zero) ((ring (J + 1)).mul (ring (J + 1)).zero b) = (ring (J + 1)).zero
    lib_ring

theorem iv_zero : iv J n (G2 J n).zero = (G2 J n).zero := by
  apply Prod.ext
  · show (ring (J + 1)).add ((ring (J + 1)).mul (ring (J + 1)).zero (Frobenius.pw (ring (J + 1)) (qnorm (ring (J + 1)) n ((ring (J + 1)).zero, (ring (J + 1)).zero)) J))
      ((ring (J + 1)).mul n ((ring (J + 1)).mul ((ring (J + 1)).neg (ring (J + 1)).zero) (ring (J + 1)).zero)) = (ring (J + 1)).zero
    lib_ring
  · show (ring (J + 1)).add ((ring (J + 1)).mul (ring (J + 1)).zero (ring (J + 1)).zero) ((ring (J + 1)).mul ((ring (J + 1)).neg (ring (J + 1)).zero) (Frobenius.pw (ring (J + 1)) (qnorm (ring (J + 1)) n ((ring (J + 1)).zero, (ring (J + 1)).zero)) J)) = (ring (J + 1)).zero
    lib_ring

theorem iv_mul (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hJ : J + 1 = 2 * m) (h1 : 1 ≤ J)
    (hn : Frobenius.pw (ring (J + 1)) n m = (ring (J + 1)).neg (ring (J + 1)).one) (x : Zp (J + 1) × Zp (J + 1)) (hx : x ≠ (G2 J n).zero) :
    (G2 J n).mul (iv J n x) x = (G2 J n).one := by
  rw [(G2 J n).mul_comm]
  exact quad_inv J n hpr m hJ h1 hn x hx

theorem iv_hom (x y : Zp (J + 1) × Zp (J + 1)) : iv J n ((G2 J n).mul x y) = (G2 J n).mul (iv J n x) (iv J n y) := by
  unfold iv
  rw [Quad.conj_mul, Quad.norm_mul, Frobenius.pw_mul, ← embed_mul]
  show qmul (ring (J + 1)) n (qmul (ring (J + 1)) n (qconj (ring (J + 1)) x) (qconj (ring (J + 1)) y))
      (qmul (ring (J + 1)) n (Frobenius.pw (ring (J + 1)) (qnorm (ring (J + 1)) n x) J, (ring (J + 1)).zero) (Frobenius.pw (ring (J + 1)) (qnorm (ring (J + 1)) n y) J, (ring (J + 1)).zero))
    = qmul (ring (J + 1)) n (qmul (ring (J + 1)) n (qconj (ring (J + 1)) x) (Frobenius.pw (ring (J + 1)) (qnorm (ring (J + 1)) n x) J, (ring (J + 1)).zero))
      (qmul (ring (J + 1)) n (qconj (ring (J + 1)) y) (Frobenius.pw (ring (J + 1)) (qnorm (ring (J + 1)) n y) J, (ring (J + 1)).zero))
  rw [qmul_assoc, ← qmul_assoc (ring (J + 1)) n (qconj (ring (J + 1)) y), qmul_comm (ring (J + 1)) n (qconj (ring (J + 1)) y),
    qmul_assoc, ← qmul_assoc]

theorem nozd (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hJ : J + 1 = 2 * m) (h1 : 1 ≤ J)
    (hn : Frobenius.pw (ring (J + 1)) n m = (ring (J + 1)).neg (ring (J + 1)).one) : DetChain.NoZD (G2 J n) := fun x y e => by
  by_cases hx : x = (G2 J n).zero
  · exact Or.inl hx
  · right
    have hi := iv_mul J n m hpr hJ h1 hn x hx
    calc y = (G2 J n).mul (G2 J n).one y := ((G2 J n).one_mul y).symm
      _ = (G2 J n).mul (G2 J n).one y := rfl
      _ = (G2 J n).mul ((G2 J n).mul (iv J n x) x) y := by rw [hi]
      _ = (G2 J n).mul (iv J n x) ((G2 J n).mul x y) := (G2 J n).mul_assoc _ _ _
      _ = (G2 J n).zero := by rw [e]; exact DetChain.mul_zero (G2 J n) _


theorem neg_embed (a : Zp (J + 1)) : (G2 J n).neg (a, (ring (J + 1)).zero) = ((ring (J + 1)).neg a, (ring (J + 1)).zero) := by
  apply Prod.ext
  · rfl
  · show (ring (J + 1)).neg (ring (J + 1)).zero = (ring (J + 1)).zero
    exact (DetChain.neg_unique (ring (J + 1)) _ _ ((ring (J + 1)).zero_add (ring (J + 1)).zero)).symm

theorem pw_p1 (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hJ : J + 1 + 1 = 2 * m + 1)
    (hn : Frobenius.pw (ring (J + 1)) n m = (ring (J + 1)).neg (ring (J + 1)).one) (x : Zp (J + 1) × Zp (J + 1)) :
    Frobenius.pw (G2 J n) x (J + 1 + 1 + 1) = (qnorm (ring (J + 1)) n x, (ring (J + 1)).zero) := by
  show (G2 J n).mul (Frobenius.pw (G2 J n) x (J + 1 + 1)) x = _
  rw [show Frobenius.pw (G2 J n) x (J + 1 + 1) = Frobenius.pw (quad (ring (J + 1)) n) (x.1, x.2) (J + 1 + 1) from rfl,
    frob_conj J n hpr m hJ hn x.1 x.2, (G2 J n).mul_comm]
  exact mul_self_norm (ring (J + 1)) n x

theorem pw_h2 (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hJ : J + 1 + 1 = 2 * m + 1)
    (hn : Frobenius.pw (ring (J + 1)) n m = (ring (J + 1)).neg (ring (J + 1)).one) (x : Zp (J + 1) × Zp (J + 1)) :
    Frobenius.pw (G2 J n) x ((J + 1 + 1 + 1) * m) = (Frobenius.pw (ring (J + 1)) (qnorm (ring (J + 1)) n x) m, (ring (J + 1)).zero) := by
  rw [← TwoPower.pwpow, pw_p1 J n m hpr hJ hn]
  exact pw_embed J n _ m

theorem euler_pm (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hJ : J + 1 + 1 = 2 * m + 1) (h1 : 1 ≤ J)
    (hn : Frobenius.pw (ring (J + 1)) n m = (ring (J + 1)).neg (ring (J + 1)).one) (x : Zp (J + 1) × Zp (J + 1)) (hx : x ≠ (G2 J n).zero) :
    Frobenius.pw (G2 J n) x ((J + 1 + 1 + 1) * m) = (G2 J n).one ∨
      Frobenius.pw (G2 J n) x ((J + 1 + 1 + 1) * m) = (G2 J n).neg (G2 J n).one := by
  have hJ' : J + 1 = 2 * m := Nat.succ.inj (hJ.trans rfl)
  have hN := norm_ne_zero J n hpr m hJ' h1 hn x.1 x.2 hx
  have hsq : (ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) (qnorm (ring (J + 1)) n x) m) (Frobenius.pw (ring (J + 1)) (qnorm (ring (J + 1)) n x) m) = (ring (J + 1)).one := by
    rw [← HasseCM.pw_add', show m + m = J + 1 by rw [hJ']; lib_nat]
    exact unit_pow J hpr _ hN
  rw [pw_h2 J n m hpr hJ hn]
  cases Euler.sq_one (ring (J + 1)) (ZMod.nozd J hpr) _ hsq with
  | inl h => left; rw [h]; rfl
  | inr h => right; rw [h]; show ((ring (J + 1)).neg (ring (J + 1)).one, (ring (J + 1)).zero) = (G2 J n).neg ((ring (J + 1)).one, (ring (J + 1)).zero); rw [neg_embed]

theorem euler_sq (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hJ : J + 1 + 1 = 2 * m + 1) (h1 : 1 ≤ J)
    (hn : Frobenius.pw (ring (J + 1)) n m = (ring (J + 1)).neg (ring (J + 1)).one) (y : Zp (J + 1) × Zp (J + 1)) (hy : y ≠ (G2 J n).zero) :
    Frobenius.pw (G2 J n) ((G2 J n).mul y y) ((J + 1 + 1 + 1) * m) = (G2 J n).one := by
  rw [Frobenius.pw_mul]
  cases euler_pm J n m hpr hJ h1 hn y hy with
  | inl h => rw [h, (G2 J n).one_mul]
  | inr h => rw [h, DetChain.neg_mul_left, (G2 J n).one_mul, DetChain.neg_neg']

end Fq2

#print axioms Fq2.embed_mul
#print axioms Fq2.iv_zero
#print axioms Fq2.iv_mul
#print axioms Fq2.iv_hom
#print axioms Fq2.nozd
#print axioms Fq2.neg_embed
#print axioms Fq2.pw_p1
#print axioms Fq2.pw_h2
#print axioms Fq2.euler_pm
#print axioms Fq2.euler_sq

lang/Fq2Inst.lean

lean_all/lang/Fq2Inst.lean

/-
  lang/Fq2Inst — T43, cycle 90: 𝔽_{p²} = 𝔽_p[√n] as an instance of FinField.FF for every odd prime p.

  p = 2k + 3, n any non-residue (n^(k+1) = −1), q = p², h = (2k + 4)(k + 1).
    nonroot_gen   in a ring without zero divisors whose elements are listed once each (q = 2h + 1 of them),
                  some unit has x^(k+1) ≠ 1 whenever k + 2 ≤ 2h      (root bound; no FF structure needed)
    ffq           the FF structure: pairs (Fp2), inverses and Euler (Fq2), square roots (Fq2Sqrt), a non-residue
                  (nonroot_gen at k + 1 = h)
  Unlike Fp2Inst (p ≡ 3 mod 4 only) this covers every odd prime: every character-sum theorem for FF holds over every 𝔽_{p²}.

  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.Fq2Sqrt
import lang.FinCyclic

namespace Fq2Inst

open FTAChain ZMod YMChain FiniteRecChain Quad Fp2 Fq2 FinList

theorem nonroot_gen {R : Type} [DecidableEq R] (K : DetChain.CRing R) (hK : DetChain.NoZD K) (els : List R)
    (comp : Complete LibInt.intR els) (h : Nat) (card : sumK LibInt.intR (fun _ => (1 : Int)) els = Int.ofNat (2 * h + 1))
    (k : Nat) (hk : k + 1 + 1 ≤ 2 * h) : ∃ g, g ≠ K.zero ∧ Frobenius.pw K g (k + 1) ≠ K.one := by
  have hlen : els.length = 2 * h + 1 := Int.ofNat.inj ((len_int els).symm.trans card)
  have hc1 : cnt K.zero els = 1 := Int.ofNat.inj ((cnt_int K.zero els).symm.trans (comp K.zero))
  have hl := len_nzL K.zero els
  rw [hc1, hlen] at hl
  have hlen' : (nzL K.zero els).length = 2 * h := Nat.succ.inj hl
  have hd := dist_nzL K.zero els (dist_of_complete els comp)
  have hfind : ∀ l : List R, (∃ g, memL g l ∧ Frobenius.pw K g (k + 1) ≠ K.one) ∨ (∀ g, memL g l → Frobenius.pw K g (k + 1) = K.one) := by
    intro l
    induction l with
    | nil => exact Or.inr (fun _ h => False.elim h)
    | cons a l ih =>
      by_cases ha : Frobenius.pw K a (k + 1) = K.one
      · cases ih with
        | inl h => match h with | ⟨g, hg, e⟩ => exact Or.inl ⟨g, Or.inr hg, e⟩
        | inr h => exact Or.inr (fun g hg => by cases hg with
            | inl e => rw [e]; exact ha
            | inr e => exact h g e)
      · exact Or.inl ⟨a, Or.inl rfl, ha⟩
  cases hfind (nzL K.zero els) with
  | inl hh => match hh with | ⟨g, hg, e⟩ => exact ⟨g, (mem_nzL K.zero g els hg).2, e⟩
  | inr hh =>
    exfalso
    have one_ne : K.one ≠ K.zero := fun e => by
      have : ∀ x, x = K.zero := fun x => by rw [← K.one_mul x, e, DetChain.zero_mul']
      -- then every element equals zero, so els has at most one distinct element
      have h2 : 2 ≤ 2 * h := Nat.le_trans (Nat.succ_le_succ (Nat.succ_le_succ (Nat.zero_le k))) hk
      match hn : nzL K.zero els with
      | [] => rw [hn] at hlen'; rw [← hlen'] at h2; exact Nat.not_succ_le_zero 1 h2
      | a :: _ => exact (mem_nzL K.zero a els (by rw [hn]; exact Or.inl rfl)).2 (this a)
    have := RootBound.root_bound K hK (k + 1) K.one (EulerHard.binomL.zerosThen K k)
      one_ne (EulerHard.len_zerosThen _ k) _ hd
      (fun r hr => by
        show RootBound.evH K r (EulerHard.binomL K k) = _
        rw [EulerHard.ev_binom, hh r hr, K.add_neg])
    rw [hlen'] at this
    exact Nat.not_succ_le_self _ (Nat.le_trans hk this)

theorem h_form (k : Nat) : (2 * k + 1 + 1 + 1 + 1) * (k + 1) = (2 * k * k + 6 * k + 3) + 1 := by lib_nat

/-- **𝔽_{p²} = 𝔽_p[√n] for every odd prime p.** -/
def ffq (k : Nat) (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = 2 * k + 1 + 1 + 1) (n : Zp (2 * k + 1 + 1))
    (hn : Frobenius.pw (ring (2 * k + 1 + 1)) n (k + 1) = (ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) :
    FinField.FF (Zp (2 * k + 1 + 1) × Zp (2 * k + 1 + 1)) where
  K := G2 (2 * k + 1) n
  els := pairs (2 * k + 1)
  comp := comp_pairs (2 * k + 1)
  h := (2 * k + 1 + 1 + 1 + 1) * (k + 1)
  card := by
    rw [card_pairs]
    show Int.ofNat ((2 * k + 1 + 1 + 1) * (2 * k + 1 + 1 + 1)) = Int.ofNat (2 * ((2 * k + 1 + 1 + 1 + 1) * (k + 1)) + 1)
    rw [show (2 * k + 1 + 1 + 1) * (2 * k + 1 + 1 + 1) = 2 * ((2 * k + 1 + 1 + 1 + 1) * (k + 1)) + 1 by lib_nat]
  nozd := Fq2.nozd (2 * k + 1) n (k + 1) hpr (by lib_nat) (Nat.le_add_left 1 _) hn
  inv := fun x hx => ⟨Fq2.iv (2 * k + 1) n x, by
    rw [(G2 (2 * k + 1) n).mul_comm]; exact iv_mul (2 * k + 1) n (k + 1) hpr (by lib_nat) (Nat.le_add_left 1 _) hn x hx⟩
  euler_pm := fun x hx => Fq2.euler_pm (2 * k + 1) n (k + 1) hpr (by lib_nat) (Nat.le_add_left 1 _) hn x hx
  euler_sq := fun y hy => Fq2.euler_sq (2 * k + 1) n (k + 1) hpr (by lib_nat) (Nat.le_add_left 1 _) hn y hy
  euler_hard := fun x hx => Fq2Sqrt.fq_sqrt k n hpr hn x hx
  ne_pm := fun e => QChar.ne_pm k (congrArg Prod.fst e)
  nonres := by
    have := nonroot_gen (G2 (2 * k + 1) n) (Fq2.nozd (2 * k + 1) n (k + 1) hpr (by lib_nat) (Nat.le_add_left 1 _) hn)
      (pairs (2 * k + 1)) (comp_pairs (2 * k + 1)) ((2 * k + 1 + 1 + 1 + 1) * (k + 1)) (by
        rw [card_pairs]
        show Int.ofNat ((2 * k + 1 + 1 + 1) * (2 * k + 1 + 1 + 1)) = Int.ofNat (2 * ((2 * k + 1 + 1 + 1 + 1) * (k + 1)) + 1)
        rw [show (2 * k + 1 + 1 + 1) * (2 * k + 1 + 1 + 1) = 2 * ((2 * k + 1 + 1 + 1 + 1) * (k + 1)) + 1 by lib_nat])
      (2 * k * k + 6 * k + 3) (Nat.le.intro (k := 2 * k * k + 6 * k + 3) (by lib_nat))
    rw [← h_form] at this
    exact this
  iv := Fq2.iv (2 * k + 1) n
  iv_mul := fun x hx => iv_mul (2 * k + 1) n (k + 1) hpr (by lib_nat) (Nat.le_add_left 1 _) hn x hx
  iv_zero := iv_zero (2 * k + 1) n
  iv_hom := iv_hom (2 * k + 1) n

end Fq2Inst

#print axioms Fq2Inst.nonroot_gen
#print axioms Fq2Inst.h_form
#print axioms Fq2Inst.ffq

lang/Fq2Sqrt.lean

lean_all/lang/Fq2Sqrt.lean

/-
  lang/Fq2Sqrt — T43, cycle 89: square roots in 𝔽_p[√n] for any non-residue n (Euler's criterion, hard half).

  p = 2k + 3, n^(k+1) = −1. If x = a + b√n has x^((p²−1)/2) = 1 then N(x) = a² − n b² = s² in 𝔽_p and
    b = 0:  a = c², or a n = d² and then a = n (d/n)² = (d/n · √n)²
    b ≠ 0:  u± = (a ± s)/2 have u₊u₋ = n b²/4, a non-residue, so one of them is u = c²; then y = c + (b/(2c)) √n
            has y² = (c² + n b²/(4u)) + b √n = a + b√n   (u² − a u + n b²/4 = 0)
    fq_sqrt   ∃ y, y² = x
  Generalises Fp2Sqrt (n = −1) to every non-residue.

  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Fq2
import lang.Fp2Sqrt

namespace Fq2Sqrt

open FTAChain ZMod YMChain Quad QuadFrob Fq2

variable (k : Nat) (n : Zp (2 * k + 1 + 1))

theorem sq_gen (c d : Zp (2 * k + 1 + 1)) :
    (G2 (2 * k + 1) n).mul (c, d) (c, d) = ((ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul c c) ((ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul d d)), (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one (ring (2 * k + 1 + 1)).one) ((ring (2 * k + 1 + 1)).mul c d)) := by
  apply Prod.ext
  · show (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul c c) ((ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul d d)) = _; rfl
  · show (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul c d) ((ring (2 * k + 1 + 1)).mul d c) = (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one (ring (2 * k + 1 + 1)).one) ((ring (2 * k + 1 + 1)).mul c d); lib_ring

/-- with 2t = 1 and s² = a² − n b², U = (a + s)t satisfies U² − a U + n b² t² = 0. -/
theorem quad_rel (a b s t : Zp (2 * k + 1 + 1)) (ht : (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one (ring (2 * k + 1 + 1)).one) t = (ring (2 * k + 1 + 1)).one)
    (hs : (ring (2 * k + 1 + 1)).mul s s = (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul a a) ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul b b)))) :
    (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s) t) ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s) t)) ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul a ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s) t))))
      ((ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul b b) ((ring (2 * k + 1 + 1)).mul t t))) = (ring (2 * k + 1 + 1)).zero := by
  have e : (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s) t) ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s) t)) ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul a ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s) t))))
      ((ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul b b) ((ring (2 * k + 1 + 1)).mul t t)))
      = (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul t t) ((ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul s s) ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul a a) ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul b b)))))))
          ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul a ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s) t)) ((ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one (ring (2 * k + 1 + 1)).one) t) ((ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one))) := by
    lib_ring
  rw [e, hs, (ring (2 * k + 1 + 1)).add_neg, ht, (ring (2 * k + 1 + 1)).add_neg, DetChain.mul_zero, DetChain.mul_zero, (ring (2 * k + 1 + 1)).zero_add]

/-- **Square roots in 𝔽_p[√n].** -/
theorem fq_sqrt (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = 2 * k + 1 + 1 + 1)
    (hn : Frobenius.pw (ring (2 * k + 1 + 1)) n (k + 1) = (ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) (x : Zp (2 * k + 1 + 1) × Zp (2 * k + 1 + 1))
    (hx : Frobenius.pw (G2 (2 * k + 1) n) x ((2 * k + 1 + 1 + 1 + 1) * (k + 1)) = (G2 (2 * k + 1) n).one) :
    ∃ y, (G2 (2 * k + 1) n).mul y y = x := by
  match x with
  | (a, b) =>
  rw [pw_h2 (2 * k + 1) n (k + 1) hpr (by lib_nat) hn] at hx
  have hN : Frobenius.pw (ring (2 * k + 1 + 1)) ((ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul a a) ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul b b)))) (k + 1) = (ring (2 * k + 1 + 1)).one := by
    have := congrArg Prod.fst hx
    exact this
  have hn0 : n ≠ (ring (2 * k + 1 + 1)).zero := fun e => by
    rw [e, HasseCM.pw_zero_succ] at hn
    exact PowerSum.one_ne_zero' _ (by
      have := congrArg (fun z => (ring (2 * k + 1 + 1)).add z (ring (2 * k + 1 + 1)).one) hn
      exact (((ring (2 * k + 1 + 1)).zero_add (ring (2 * k + 1 + 1)).one).symm.trans (this.trans (DetChain.add_neg' (ring (2 * k + 1 + 1)) (ring (2 * k + 1 + 1)).one))))
  by_cases hb : b = (ring (2 * k + 1 + 1)).zero
  · by_cases ha : a = (ring (2 * k + 1 + 1)).zero
    · exact ⟨((ring (2 * k + 1 + 1)).zero, (ring (2 * k + 1 + 1)).zero), by rw [ha, hb]; exact DetChain.mul_zero (G2 (2 * k + 1) n) _⟩
    · cases Euler.euler_pm k hpr a ha with
      | inl h =>
        match EulerHard.euler_hard k hpr a h with
        | ⟨c, hc⟩ =>
          refine ⟨(c, (ring (2 * k + 1 + 1)).zero), ?_⟩
          rw [sq_gen, hc, hb]
          apply Prod.ext
          · show (ring (2 * k + 1 + 1)).add a ((ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul (ring (2 * k + 1 + 1)).zero (ring (2 * k + 1 + 1)).zero)) = a; lib_ring
          · show (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one (ring (2 * k + 1 + 1)).one) ((ring (2 * k + 1 + 1)).mul c (ring (2 * k + 1 + 1)).zero) = (ring (2 * k + 1 + 1)).zero; lib_ring
      | inr h =>
        have han : Frobenius.pw (ring (2 * k + 1 + 1)) ((ring (2 * k + 1 + 1)).mul a n) (k + 1) = (ring (2 * k + 1 + 1)).one := by rw [Frobenius.pw_mul, h, hn]; lib_ring
        match EulerHard.euler_hard k hpr _ han with
        | ⟨d, hd⟩ =>
          have hni := ConicSum.iv_mul_self k hpr n hn0
          refine ⟨((ring (2 * k + 1 + 1)).zero, (ring (2 * k + 1 + 1)).mul d (ConicSum.iv k n)), ?_⟩
          rw [sq_gen, hb]
          apply Prod.ext
          · show (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul (ring (2 * k + 1 + 1)).zero (ring (2 * k + 1 + 1)).zero) ((ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul d (ConicSum.iv k n)) ((ring (2 * k + 1 + 1)).mul d (ConicSum.iv k n)))) = a
            rw [show (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul (ring (2 * k + 1 + 1)).zero (ring (2 * k + 1 + 1)).zero) ((ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul d (ConicSum.iv k n)) ((ring (2 * k + 1 + 1)).mul d (ConicSum.iv k n))))
                = (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul (ConicSum.iv k n) n) ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul d d) (ConicSum.iv k n)) by lib_ring, hni, hd]
            rw [show (ring (2 * k + 1 + 1)).mul (ring (2 * k + 1 + 1)).one ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul a n) (ConicSum.iv k n)) = (ring (2 * k + 1 + 1)).mul a ((ring (2 * k + 1 + 1)).mul (ConicSum.iv k n) n) by lib_ring, hni]
            lib_ring
          · show (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one (ring (2 * k + 1 + 1)).one) ((ring (2 * k + 1 + 1)).mul (ring (2 * k + 1 + 1)).zero ((ring (2 * k + 1 + 1)).mul d (ConicSum.iv k n))) = (ring (2 * k + 1 + 1)).zero
            lib_ring
  · match EulerHard.euler_hard k hpr _ hN with
    | ⟨s, hs⟩ =>
      have h2 := Fp2Sqrt.two_ne k
      let t := ConicSum.iv k ((ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one (ring (2 * k + 1 + 1)).one)
      have ht : (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one (ring (2 * k + 1 + 1)).one) t = (ring (2 * k + 1 + 1)).one := by
        rw [(ring (2 * k + 1 + 1)).mul_comm]; exact ConicSum.iv_mul_self k hpr _ h2
      have ht0 : t ≠ (ring (2 * k + 1 + 1)).zero := fun e => by rw [e, DetChain.mul_zero] at ht; exact PowerSum.one_ne_zero' _ ht.symm
      have choose : ∃ s', (ring (2 * k + 1 + 1)).mul s' s' = (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul a a) ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul b b))) ∧
          Frobenius.pw (ring (2 * k + 1 + 1)) ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s') t) (k + 1) = (ring (2 * k + 1 + 1)).one := by
        have hprod : (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s) t) ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a ((ring (2 * k + 1 + 1)).neg s)) t) = (ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul b t) ((ring (2 * k + 1 + 1)).mul b t)) := by
          have e : (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s) t) ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a ((ring (2 * k + 1 + 1)).neg s)) t)
              = (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul b t) ((ring (2 * k + 1 + 1)).mul b t)))
                  ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul t t) ((ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul a a) ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul b b)))) ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul s s)))) := by lib_ring
          rw [e, hs, (ring (2 * k + 1 + 1)).add_neg, DetChain.mul_zero, DetChain.add_zero']
        have hbt : (ring (2 * k + 1 + 1)).mul b t ≠ (ring (2 * k + 1 + 1)).zero := fun e => by
          cases nozd (2 * k + 1) hpr _ _ e with
          | inl h => exact hb h
          | inr h => exact ht0 h
        have hp : Frobenius.pw (ring (2 * k + 1 + 1)) ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s) t) ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a ((ring (2 * k + 1 + 1)).neg s)) t)) (k + 1) = (ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one := by
          rw [hprod, Frobenius.pw_mul, hn, Euler.euler_square k hpr _ hbt]; lib_ring
        rw [Frobenius.pw_mul] at hp
        have hu1 : (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s) t ≠ (ring (2 * k + 1 + 1)).zero := fun e => by
          rw [e, HasseCM.pw_zero_succ, DetChain.zero_mul'] at hp
          apply PowerSum.one_ne_zero' (2 * k + 1)
          have := congrArg (fun z => (ring (2 * k + 1 + 1)).add z (ring (2 * k + 1 + 1)).one) hp
          exact (((ring (2 * k + 1 + 1)).zero_add (ring (2 * k + 1 + 1)).one).symm.trans (this.trans (DetChain.add_neg' (ring (2 * k + 1 + 1)) (ring (2 * k + 1 + 1)).one)))
        cases Euler.euler_pm k hpr _ hu1 with
        | inl h1 => exact ⟨s, hs, h1⟩
        | inr h1 =>
          refine ⟨(ring (2 * k + 1 + 1)).neg s, by rw [Thue.negsq (ring (2 * k + 1 + 1))]; exact hs, ?_⟩
          rw [h1] at hp
          have : Frobenius.pw (ring (2 * k + 1 + 1)) ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a ((ring (2 * k + 1 + 1)).neg s)) t) (k + 1)
              = (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) (Frobenius.pw (ring (2 * k + 1 + 1)) ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a ((ring (2 * k + 1 + 1)).neg s)) t) (k + 1))) := by lib_ring
          rw [this, hp]; lib_ring
      match choose with
      | ⟨s', hs', hu⟩ =>
        match EulerHard.euler_hard k hpr _ hu with
        | ⟨c, hc⟩ =>
          have hu0 : (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s') t ≠ (ring (2 * k + 1 + 1)).zero := fun e => by
            rw [e, HasseCM.pw_zero_succ] at hu; exact PowerSum.one_ne_zero' _ hu.symm
          have hc0 : c ≠ (ring (2 * k + 1 + 1)).zero := fun e => hu0 (by rw [← hc, e]; exact DetChain.mul_zero (ring (2 * k + 1 + 1)) _)
          have hci := ConicSum.iv_mul_self k hpr c hc0
          refine ⟨(c, (ring (2 * k + 1 + 1)).mul b ((ring (2 * k + 1 + 1)).mul t (ConicSum.iv k c))), ?_⟩
          rw [sq_gen]
          apply Prod.ext
          · show (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul c c) ((ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul b ((ring (2 * k + 1 + 1)).mul t (ConicSum.iv k c))) ((ring (2 * k + 1 + 1)).mul b ((ring (2 * k + 1 + 1)).mul t (ConicSum.iv k c))))) = a
            have hq := quad_rel k n a b s' t ht hs'
            generalize hU : (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s') t = U at hq hc
            have hiu : (ring (2 * k + 1 + 1)).mul (ConicSum.iv k c) (ConicSum.iv k c) = ConicSum.iv k U := by
              rw [← hc, ConicSum.inv_mul]
            have hUi : (ring (2 * k + 1 + 1)).mul U (ConicSum.iv k U) = (ring (2 * k + 1 + 1)).one := by
              rw [← hiu, ← hc]
              rw [show (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul c c) ((ring (2 * k + 1 + 1)).mul (ConicSum.iv k c) (ConicSum.iv k c))
                  = (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul (ConicSum.iv k c) c) ((ring (2 * k + 1 + 1)).mul (ConicSum.iv k c) c) by lib_ring, hci, (ring (2 * k + 1 + 1)).one_mul]
            rw [hc]
            have e : (ring (2 * k + 1 + 1)).add U ((ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul b ((ring (2 * k + 1 + 1)).mul t (ConicSum.iv k c))) ((ring (2 * k + 1 + 1)).mul b ((ring (2 * k + 1 + 1)).mul t (ConicSum.iv k c)))))
                = (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul U U) ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul a U))) ((ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul b b) ((ring (2 * k + 1 + 1)).mul t t)))) (ConicSum.iv k U))
                    ((ring (2 * k + 1 + 1)).mul a ((ring (2 * k + 1 + 1)).mul U (ConicSum.iv k U))))
                    ((ring (2 * k + 1 + 1)).mul U ((ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul U (ConicSum.iv k U))))) := by
              rw [show (ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul b ((ring (2 * k + 1 + 1)).mul t (ConicSum.iv k c))) ((ring (2 * k + 1 + 1)).mul b ((ring (2 * k + 1 + 1)).mul t (ConicSum.iv k c))))
                  = (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul b b) ((ring (2 * k + 1 + 1)).mul t t))) ((ring (2 * k + 1 + 1)).mul (ConicSum.iv k c) (ConicSum.iv k c)) by lib_ring, hiu]
              lib_ring
            rw [e, hq, hUi, (ring (2 * k + 1 + 1)).add_neg]
            lib_ring
          · show (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one (ring (2 * k + 1 + 1)).one) ((ring (2 * k + 1 + 1)).mul c ((ring (2 * k + 1 + 1)).mul b ((ring (2 * k + 1 + 1)).mul t (ConicSum.iv k c)))) = b
            have e : (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one (ring (2 * k + 1 + 1)).one) ((ring (2 * k + 1 + 1)).mul c ((ring (2 * k + 1 + 1)).mul b ((ring (2 * k + 1 + 1)).mul t (ConicSum.iv k c))))
                = (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul b ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one (ring (2 * k + 1 + 1)).one) t)) ((ring (2 * k + 1 + 1)).mul (ConicSum.iv k c) c) := by lib_ring
            rw [e, ht, hci]; lib_ring

end Fq2Sqrt

#print axioms Fq2Sqrt.sq_gen
#print axioms Fq2Sqrt.quad_rel
#print axioms Fq2Sqrt.fq_sqrt

lang/Fq2Trace.lean

lean_all/lang/Fq2Trace.lean

/-
  lang/Fq2Trace — T43, cycle 92: y² = x³ − x over 𝔽_{p²} for every odd prime p.

  p = 2k + 3, 𝔽_{p²} = 𝔽_p[√n] (Fq2Inst.ffq).
    keyq, keyq_inj    an injective order key (a, b) ↦ a·p + b
    iq, iq_sq         i = (√n)^((p−1)/2) satisfies i² = n^((p−1)/2) = −1: every 𝔽_{p²} contains i
    fq_ap_primary     −Σ_{z ∈ 𝔽_{p²}} χ(z³ − z) = 2A with p² = A² + B², A + Bi primary      (FinPrimary)
    fq_hasse          (Σ_{z ∈ 𝔽_{p²}} χ(z³ − z))² ≤ 4p²                                     (Hasse's bound)
  So over 𝔽_{p²} the Frobenius trace of the CM curve is 2 Re of a primary Gaussian integer of norm p², at every p — split
  (p ≡ 1 mod 4) as well as inert.

  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.Fq2Inst
import lang.FinPrimary
import lang.Fp2Count
import lang.CubicSuper
import lang.FinCubicSign

namespace Fq2Trace

open FTAChain ZMod YMChain FiniteRecChain Quad Fq2

variable (k : Nat)

def keyq (x : Zp (2 * k + 1 + 1) × Zp (2 * k + 1 + 1)) : Nat := x.1.val * (2 * k + 1 + 1 + 1) + x.2.val

theorem keyq_inj (x y : Zp (2 * k + 1 + 1) × Zp (2 * k + 1 + 1)) (h : keyq k x = keyq k y) : x = y :=
  match Fp2Count.divmod_unique _ _ _ _ _ x.2.property y.2.property h with
  | ⟨h1, h2⟩ => Prod.ext (Subtype.ext h1) (Subtype.ext h2)

def iq (n : Zp (2 * k + 1 + 1)) : Zp (2 * k + 1 + 1) × Zp (2 * k + 1 + 1) :=
  Frobenius.pw (G2 (2 * k + 1) n) ((ring (2 * k + 1 + 1)).zero, (ring (2 * k + 1 + 1)).one) (k + 1)

theorem iq_sq (n : Zp (2 * k + 1 + 1)) (hn : Frobenius.pw (ring (2 * k + 1 + 1)) n (k + 1) = (ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) :
    (G2 (2 * k + 1) n).mul (iq k n) (iq k n) = (G2 (2 * k + 1) n).neg (G2 (2 * k + 1) n).one := by
  unfold iq
  rw [← Frobenius.pw_mul]
  have e : (G2 (2 * k + 1) n).mul ((ring (2 * k + 1 + 1)).zero, (ring (2 * k + 1 + 1)).one) ((ring (2 * k + 1 + 1)).zero, (ring (2 * k + 1 + 1)).one)
      = (n, (ring (2 * k + 1 + 1)).zero) := by
    apply Prod.ext
    · show (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul (ring (2 * k + 1 + 1)).zero (ring (2 * k + 1 + 1)).zero)
        ((ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul (ring (2 * k + 1 + 1)).one (ring (2 * k + 1 + 1)).one)) = n
      lib_ring
    · show (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul (ring (2 * k + 1 + 1)).zero (ring (2 * k + 1 + 1)).one)
        ((ring (2 * k + 1 + 1)).mul (ring (2 * k + 1 + 1)).one (ring (2 * k + 1 + 1)).zero) = (ring (2 * k + 1 + 1)).zero
      lib_ring
  rw [e, QuadFrob.pw_embed (2 * k + 1) n n (k + 1), hn, ← Fq2.neg_embed]
  rfl

variable (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = 2 * k + 1 + 1 + 1) (n : Zp (2 * k + 1 + 1))
  (hn : Frobenius.pw (ring (2 * k + 1 + 1)) n (k + 1) = (ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one)

/-- **y² = x³ − x over every 𝔽_{p²}: a = 2 Re π, π primary of norm p².** -/
theorem fq_ap_primary :
    ∃ A B : Int, A * A + B * B = Int.ofNat (2 * k + 1 + 1 + 1) * Int.ofNat (2 * k + 1 + 1 + 1) ∧ GaussInt.Primary ⟨A, B⟩ ∧
      -sumK LibInt.intR (fun x => FinField.chi (Fq2Inst.ffq k hpr n hn) (FinSign.f (Fq2Inst.ffq k hpr n hn) x)) (Fq2Inst.ffq k hpr n hn).els = 2 * A := by
  match FinPrimary.fin_ap_primary (Fq2Inst.ffq k hpr n hn) (keyq k) (keyq_inj k) (iq k n) (iq_sq k n hn) with
  | ⟨A, B, hAB, hP, hS⟩ =>
    refine ⟨A, B, ?_, hP, hS⟩
    rw [hAB]
    show Int.ofNat (2 * ((2 * k + 1 + 1 + 1 + 1) * (k + 1)) + 1) = Int.ofNat ((2 * k + 1 + 1 + 1) * (2 * k + 1 + 1 + 1))
    rw [CubicSuper.sq_id k]

/-- **Hasse's bound over every 𝔽_{p²}**: a² + 4B² = 4p². -/
theorem fq_hasse : ∃ s : Nat,
    sumK LibInt.intR (fun x => FinField.chi (Fq2Inst.ffq k hpr n hn) (FinSign.f (Fq2Inst.ffq k hpr n hn) x)) (Fq2Inst.ffq k hpr n hn).els
      * sumK LibInt.intR (fun x => FinField.chi (Fq2Inst.ffq k hpr n hn) (FinSign.f (Fq2Inst.ffq k hpr n hn) x)) (Fq2Inst.ffq k hpr n hn).els
      + Int.ofNat s = 4 * (Int.ofNat (2 * k + 1 + 1 + 1) * Int.ofNat (2 * k + 1 + 1 + 1)) := by
  match fq_ap_primary k hpr n hn with
  | ⟨A, B, hAB, _, hS⟩ =>
    match FinCubicSign.sq_nn B with
    | ⟨m, hm⟩ =>
      refine ⟨4 * m, ?_⟩
      generalize sumK LibInt.intR (fun x => FinField.chi (Fq2Inst.ffq k hpr n hn) (FinSign.f (Fq2Inst.ffq k hpr n hn) x)) (Fq2Inst.ffq k hpr n hn).els = S at hS ⊢
      have e : S = -(2 * A) := by rw [← hS]; lib_int
      rw [e, ← hAB, show Int.ofNat (4 * m) = 4 * Int.ofNat m from rfl, ← hm]
      lib_int

end Fq2Trace

#print axioms Fq2Trace.keyq
#print axioms Fq2Trace.keyq_inj
#print axioms Fq2Trace.iq
#print axioms Fq2Trace.iq_sq
#print axioms Fq2Trace.fq_ap_primary
#print axioms Fq2Trace.fq_hasse

lang/Frob8.lean

lean_all/lang/Frob8.lean

/-
  lang/Frob8 — T48, cycle 824 (step S1 of 256a at p ≡ 3 mod 8): **the Frobenius on a primitive 8th root of unity**.
  In any commutative ring with i² = −1 and s² = −i:
    s8      s⁸ = 1
    frob3   s^(8k+3) = −i·s      (p ≡ 3 mod 8: the Frobenius sends s to −i s = s³)
    frobi3  i^(8k+3) = −i        (and i to −i)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.HasseCM
import lang.TwoPower
import lib.RingTac

namespace Frob8

open Frobenius

variable {R : Type} (K : DetChain.CRing R)

theorem pw_one : ∀ k : Nat, pw K K.one k = K.one
  | 0 => rfl
  | k + 1 => by
    show K.mul (pw K K.one k) K.one = K.one
    rw [pw_one k]
    exact K.one_mul K.one

/-- **s⁸ = 1** from s² = −i and i² = −1. -/
theorem s8 (i s : R) (hi : K.add (K.mul i i) K.one = K.zero) (hs : K.add (K.mul s s) i = K.zero) : pw K s 8 = K.one := by
  have hss : K.mul s s = K.neg i := DetChain.neg_unique K _ _ hs
  have hii : K.mul i i = K.neg K.one := DetChain.neg_unique K _ _ hi
  calc pw K s 8 = K.mul (K.mul (K.mul s s) (K.mul s s)) (K.mul (K.mul s s) (K.mul s s)) := by
          show K.mul (K.mul (K.mul (K.mul (K.mul (K.mul (K.mul (K.mul K.one s) s) s) s) s) s) s) s = _
          lib_ring
    _ = K.mul (K.mul (K.neg i) (K.neg i)) (K.mul (K.neg i) (K.neg i)) := by rw [hss]
    _ = K.mul (K.mul i i) (K.mul i i) := by lib_ring
    _ = K.mul (K.neg K.one) (K.neg K.one) := by rw [hii]
    _ = K.one := by lib_ring

/-- **s^(8k+3) = −i·s**: for p = 8k + 3 the Frobenius x ↦ x^p sends s to −i s. -/
theorem frob3 (i s : R) (hi : K.add (K.mul i i) K.one = K.zero) (hs : K.add (K.mul s s) i = K.zero) (k : Nat) :
    pw K s (8 * k + 3) = K.neg (K.mul i s) := by
  have hss : K.mul s s = K.neg i := DetChain.neg_unique K _ _ hs
  rw [HasseCM.pw_add', ← TwoPower.pwpow, s8 K i s hi hs, pw_one]
  calc K.mul K.one (pw K s 3) = K.mul (K.mul s s) s := by
          show K.mul K.one (K.mul (K.mul (K.mul K.one s) s) s) = _
          lib_ring
    _ = K.mul (K.neg i) s := by rw [hss]
    _ = K.neg (K.mul i s) := by lib_ring

/-- **i^(8k+3) = −i**. -/
theorem frobi3 (i : R) (hi : K.add (K.mul i i) K.one = K.zero) (k : Nat) : pw K i (8 * k + 3) = K.neg i := by
  have hii : K.mul i i = K.neg K.one := DetChain.neg_unique K _ _ hi
  have h4 : pw K i 4 = K.one := by
    calc pw K i 4 = K.mul (K.mul i i) (K.mul i i) := by
            show K.mul (K.mul (K.mul (K.mul K.one i) i) i) i = _
            lib_ring
      _ = K.mul (K.neg K.one) (K.neg K.one) := by rw [hii]
      _ = K.one := by lib_ring
  have h8 : pw K i 8 = K.one := by
    rw [show (8 : Nat) = 4 * 2 from rfl, ← TwoPower.pwpow, h4, pw_one]
  rw [HasseCM.pw_add', ← TwoPower.pwpow, h8, pw_one]
  calc K.mul K.one (pw K i 3) = K.mul (K.mul i i) i := by
          show K.mul K.one (K.mul (K.mul (K.mul K.one i) i) i) = _
          lib_ring
    _ = K.mul (K.neg K.one) i := by rw [hii]
    _ = K.neg i := by lib_ring

end Frob8

#print axioms Frob8.pw_one
#print axioms Frob8.s8
#print axioms Frob8.frob3
#print axioms Frob8.frobi3

lang/FrobIter.lean

lean_all/lang/FrobIter.lean

/-
  lang/FrobIter — T43, cycle 185: x ↦ x^(p^N) is a ring map in every commutative ring of characteristic p.

    npow                 p^N on ℕ
    frob_iter_add        (a + b)^(p^N) = a^(p^N) + b^(p^N)       (Frobenius.frob_add, iterated)
    frob_iter_mul        (ab)^(p^N) = a^(p^N) b^(p^N)
    npow_int             ℤ-cast of p^N is ipow
  The input for the Frobenius of F[∛c] over any finite field F of order q = p^N (next: 𝔽_{q³} for every q ≡ 1 mod 3).

  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.Frobenius
import lang.TwoPower
import lang.GaussFactor

namespace FrobIter

open FTAChain Frobenius

def npow (P : Nat) : Nat → Nat
  | 0 => 1
  | N + 1 => npow P N * P

variable {R : Type} (K : DetChain.CRing R)

theorem frob_iter_add (j : Nat) (hpr : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1)
    (hp : nsm K (j + 1 + 1) K.one = K.zero) : ∀ N (a b : R),
    pw K (K.add a b) (npow (j + 1 + 1) N) = K.add (pw K a (npow (j + 1 + 1) N)) (pw K b (npow (j + 1 + 1) N))
  | 0, a, b => by
    show K.mul K.one (K.add a b) = K.add (K.mul K.one a) (K.mul K.one b)
    rw [K.one_mul, K.one_mul, K.one_mul]
  | N + 1, a, b => by
    show pw K (K.add a b) (npow (j + 1 + 1) N * (j + 1 + 1)) = K.add (pw K a (npow (j + 1 + 1) N * (j + 1 + 1))) (pw K b (npow (j + 1 + 1) N * (j + 1 + 1)))
    rw [← TwoPower.pwpow, ← TwoPower.pwpow, ← TwoPower.pwpow, frob_iter_add j hpr hp N a b, frob_add K j hpr hp]

theorem frob_iter_mul (n : Nat) (a b : R) : pw K (K.mul a b) n = K.mul (pw K a n) (pw K b n) := pw_mul K a b n

theorem npow_int (P : Nat) : ∀ N, Int.ofNat (npow P N) = GaussFactor.ipow (Int.ofNat P) N
  | 0 => rfl
  | N + 1 => by
    show Int.ofNat (npow P N * P) = GaussFactor.ipow (Int.ofNat P) N * Int.ofNat P
    rw [← npow_int P N]; rfl

end FrobIter

#print axioms FrobIter.npow
#print axioms FrobIter.frob_iter_add
#print axioms FrobIter.frob_iter_mul
#print axioms FrobIter.npow_int

lang/Frobenius.lean

lean_all/lang/Frobenius.lean

/-
  lang/Frobenius — T43, cycle 2: GL1 over a general number field, the general pieces.

  (a) The reciprocity shell, for any group G and any set of primes P with a Frobenius map
      frob : P → G and a Chebotarev section sec : G → P (frob (sec g) = g):
        shell          λ factors through frob  ↔  λ = λ ∘ sec ∘ frob
        shell_unique   the factor is λ ∘ sec
      Over a number field F with abelian L/F, G = Gal(L/F) and frob p = Artin symbol; the arithmetic
      inputs are the Frobenius map itself and the section (Chebotarev). For non-abelian G the same
      shell applies with frob landing in conjugacy classes.
  (b) Frobenius is additive in characteristic p, in every commutative ring (every residue field
      O_F/p of every number field):
        ev_nxt         ev (nxt p r) = (a + b) · ev r + p • b^|r|   (Pascal rows, no subtraction)
        ev_row         (a + b)^n = ev (row n)                       (binomial theorem)
        C_absorb       (k + 1) C(n+1, k+1) = (n + 1) C(n, k)
        dv_choose      p | C(p, k+1) for k + 1 < p, p prime          (Euclid: FTAChain.euclid_core)
        row_eq         row n = [C(n,0), …, C(n,n)]
        nsm_char       p • 1 = 0  ⇒  (q p) • y = 0
        frob_add       p prime, p • 1 = 0  ⇒  (a + b)^p = a^p + b^p
        pw_mul         (a b)^n = a^n b^n
      So x ↦ x^p is a ring endomorphism of every commutative ring of characteristic p.

  Lean 4 core only, proofs by hand, no axioms.
-/
import DetChain
import FTAChain

namespace Frobenius

/-! ## (a) The shell -/

section Shell
variable {P G R : Type} (frob : P → G) (sec : G → P) (hs : ∀ g, frob (sec g) = g)

theorem shell (lam : P → R) (hs : ∀ g, frob (sec g) = g) :
    (∃ χ : G → R, ∀ p, lam p = χ (frob p)) ↔ ∀ p, lam p = lam (sec (frob p)) :=
  ⟨fun ⟨χ, hχ⟩ p => by rw [hχ p, hχ (sec (frob p)), hs (frob p)],
   fun h => ⟨fun g => lam (sec g), h⟩⟩

theorem shell_unique (lam : P → R) (χ : G → R) (hs : ∀ g, frob (sec g) = g)
    (hχ : ∀ p, lam p = χ (frob p)) (g : G) : χ g = lam (sec g) := by
  rw [hχ (sec g), hs g]

end Shell

/-! ## (b) Pascal rows and the binomial theorem in a commutative ring -/

section Binom
variable {R : Type} (K : DetChain.CRing R)

/-- `n • x`. -/
def nsm : Nat → R → R
  | 0, _ => K.zero
  | n + 1, x => K.add (nsm n x) x

def pw (x : R) : Nat → R
  | 0 => K.one
  | k + 1 => K.mul (pw x k) x

/-- Homogeneous evaluation of a coefficient list `c_0, …, c_d`: `Σ c_k a^k b^(d-k)`. -/
def ev (a b : R) : List Nat → R
  | [] => K.zero
  | c :: cs => K.add (nsm K c (pw K b cs.length)) (K.mul a (ev a b cs))

/-- Next Pascal row: `n_k = r_k + r_(k-1)`. -/
def nxt : Nat → List Nat → List Nat
  | p, [] => [p]
  | p, c :: cs => (c + p) :: nxt c cs

def row : Nat → List Nat
  | 0 => [1]
  | n + 1 => nxt 0 (row n)

theorem nsm_add (m n : Nat) (x : R) : nsm K (m + n) x = K.add (nsm K m x) (nsm K n x) := by
  induction n with
  | zero => exact (DetChain.add_zero' K _).symm
  | succ n ih =>
    show K.add (nsm K (m + n) x) x = K.add (nsm K m x) (K.add (nsm K n x) x)
    rw [ih, K.add_assoc]

theorem mul_nsm (a : R) (n : Nat) (x : R) : K.mul a (nsm K n x) = nsm K n (K.mul a x) := by
  induction n with
  | zero => exact DetChain.mul_zero K a
  | succ n ih =>
    show K.mul a (K.add (nsm K n x) x) = K.add (nsm K n (K.mul a x)) (K.mul a x)
    rw [K.distrib, ih]

theorem len_nxt : ∀ (p : Nat) (r : List Nat), (nxt p r).length = r.length + 1
  | _, [] => rfl
  | _, c :: cs => congrArg Nat.succ (len_nxt c cs)

theorem add4 (u v w z : R) : K.add (K.add u v) (K.add w z) = K.add (K.add u w) (K.add v z) :=
  DetChain.add_mid4 K u v w z

theorem nsm_eq_mul (n : Nat) (x : R) : nsm K n x = K.mul (nsm K n K.one) x := by
  induction n with
  | zero => exact (DetChain.zero_mul' K x).symm
  | succ n ih =>
    show K.add (nsm K n x) x = K.mul (K.add (nsm K n K.one) K.one) x
    rw [ih, DetChain.right_distrib, K.one_mul]

theorem perm4 (X Y Z U : R) : K.add (K.add X Y) (K.add Z U) = K.add (K.add (K.add U X) Z) Y := by
  rw [K.add_comm Z U, add4 K, K.add_comm X U, K.add_assoc (K.add U X) Z Y, K.add_comm Z Y,
    ← K.add_assoc (K.add U X) Y Z, K.add_assoc U X Y, K.add_comm (K.add U (K.add X Y)) Z,
    K.add_comm Z, K.add_assoc U (K.add X Y) Z, K.add_comm (K.add X Y) Z, ← K.add_assoc U Z]

theorem swap3' (a s E : R) : K.mul a (K.mul s E) = K.mul s (K.mul a E) := by
  rw [← K.mul_assoc, K.mul_comm a s, K.mul_assoc]

theorem ring_step (a b C P E B : R) :
    K.add (K.add (K.mul C (K.mul B b)) (K.mul P (K.mul B b))) (K.mul a (K.add (K.mul (K.add a b) E) (K.mul C B)))
      = K.add (K.mul (K.add a b) (K.add (K.mul C B) (K.mul a E))) (K.mul P (K.mul B b)) := by
  have l1 : K.mul a (K.add (K.mul (K.add a b) E) (K.mul C B))
      = K.add (K.mul (K.add a b) (K.mul a E)) (K.mul a (K.mul C B)) := by
    rw [K.distrib, swap3' K a (K.add a b) E]
  have r1 : K.mul (K.add a b) (K.add (K.mul C B) (K.mul a E))
      = K.add (K.add (K.mul a (K.mul C B)) (K.mul C (K.mul B b))) (K.mul (K.add a b) (K.mul a E)) := by
    rw [K.distrib, DetChain.right_distrib, K.mul_comm b (K.mul C B), K.mul_assoc C B b]
  rw [l1, r1]
  exact perm4 K _ _ _ _

/-- **Pascal step**: `ev (nxt p r) = (a + b) · ev r + p • b^|r|`. -/
theorem ev_nxt (a b : R) : ∀ (p : Nat) (r : List Nat),
    ev K a b (nxt p r) = K.add (K.mul (K.add a b) (ev K a b r)) (nsm K p (pw K b r.length))
  | p, [] => by
    show K.add (nsm K p (pw K b 0)) (K.mul a K.zero) = K.add (K.mul (K.add a b) K.zero) (nsm K p (pw K b 0))
    rw [DetChain.mul_zero, DetChain.mul_zero, DetChain.add_zero', K.zero_add]
  | p, c :: cs => by
    show K.add (nsm K (c + p) (pw K b (nxt c cs).length)) (K.mul a (ev K a b (nxt c cs)))
      = K.add (K.mul (K.add a b) (K.add (nsm K c (pw K b cs.length)) (K.mul a (ev K a b cs))))
          (nsm K p (pw K b (cs.length + 1)))
    rw [ev_nxt a b c cs, len_nxt, nsm_add]
    have hB' : pw K b (cs.length + 1) = K.mul (pw K b cs.length) b := rfl
    rw [hB', nsm_eq_mul K c (K.mul (pw K b cs.length) b), nsm_eq_mul K p (K.mul (pw K b cs.length) b),
      nsm_eq_mul K c (pw K b cs.length)]
    exact ring_step K a b _ _ _ _

theorem ev_row (a b : R) : ∀ n, ev K a b (row n) = pw K (K.add a b) n
  | 0 => by
    show K.add (nsm K 1 (pw K b 0)) (K.mul a K.zero) = K.one
    rw [DetChain.mul_zero, DetChain.add_zero']
    show K.add K.zero K.one = K.one
    exact K.zero_add _
  | n + 1 => by
    show ev K a b (nxt 0 (row n)) = K.mul (pw K (K.add a b) n) (K.add a b)
    rw [ev_nxt, ev_row a b n]
    show K.add (K.mul (K.add a b) (pw K (K.add a b) n)) K.zero = _
    rw [DetChain.add_zero', K.mul_comm]

end Binom

/-! ## Binomial coefficients, and p | C(p, k) -/

section Choose
open FTAChain

def C : Nat → Nat → Nat
  | 0, 0 => 1
  | 0, _ + 1 => 0
  | _ + 1, 0 => 1
  | n + 1, k + 1 => C n k + C n (k + 1)

theorem C_zero_right : ∀ n, C n 0 = 1
  | 0 => rfl
  | _ + 1 => rfl

theorem C_above : ∀ n k, C n (k + n + 1) = 0
  | 0, _ => rfl
  | n + 1, k => by
    show C n (k + n + 1) + C n (k + n + 1 + 1) = 0
    have e : k + n + 1 + 1 = (k + 1) + n + 1 := by rw [nadd_right_comm k n 1]
    rw [C_above n k, e, C_above n (k + 1)]

theorem C_above' (n : Nat) : C n (n + 1) = 0 := by
  have h := C_above n 0
  rw [nzero_add] at h
  exact h

theorem C_self : ∀ n, C n n = 1
  | 0 => rfl
  | n + 1 => by
    show C n n + C n (n + 1) = 1
    rw [C_self n, C_above' n]

theorem C_one : ∀ n, C (n + 1) 1 = n + 1
  | 0 => rfl
  | n + 1 => by
    show C (n + 1) 0 + C (n + 1) 1 = n + 1 + 1
    rw [C_zero_right, C_one n, nadd_comm]

/-- `(k + 1) · C(n+1, k+1) = (n + 1) · C(n, k)`. -/
theorem C_absorb : ∀ n k, (k + 1) * C (n + 1) (k + 1) = (n + 1) * C n k
  | 0, 0 => rfl
  | 0, k + 1 => by
    show (k + 1 + 1) * (C 0 (k + 1) + C 0 (k + 1 + 1)) = 1 * C 0 (k + 1)
    rfl
  | n + 1, 0 => by
    show 1 * (C (n + 1) 0 + C (n + 1) 1) = (n + 1 + 1) * C (n + 1) 0
    rw [C_zero_right, C_one, nmul_one (n + 1 + 1), nmul_comm 1, nmul_one, nadd_comm]
  | n + 1, j + 1 => by
    show (j + 1 + 1) * (C (n + 1) (j + 1) + C (n + 1) (j + 1 + 1)) = (n + 1 + 1) * C (n + 1) (j + 1)
    rw [nleft_distrib, C_absorb n (j + 1)]
    have e1 : (j + 1 + 1) * C (n + 1) (j + 1) = (n + 1) * C n j + C (n + 1) (j + 1) := by
      rw [nsucc_mul, C_absorb n j]
    rw [e1]
    show (n + 1) * C n j + C (n + 1) (j + 1) + (n + 1) * C n (j + 1) = (n + 1 + 1) * (C n j + C n (j + 1))
    rw [nsucc_mul (n + 1), nleft_distrib (n + 1), nadd_right_comm]
    rfl

theorem le_succ_self_absurd : ∀ n, Le (n + 1) n → False
  | 0, h => le_zero_absurd 0 h
  | n + 1, h => le_succ_self_absurd n (le_pred (n + 1) n h)

/-- **p divides C(p, k+1) for k + 1 < p** (p = j + 2 prime). -/
theorem dv_choose (j k : Nat) (hpr : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1)
    (hk : Le (k + 1 + 1) (j + 1 + 1)) : Dv (j + 1 + 1) (C (j + 1 + 1) (k + 1)) := by
  have h1 : Dv (j + 1 + 1) ((k + 1) * C (j + 1 + 1) (k + 1)) :=
    ⟨C (j + 1) k, by rw [C_absorb (j + 1) k, nmul_comm]⟩
  cases euclid_core j k (C (j + 1 + 1) (k + 1)) hpr h1 with
  | inl h => exact absurd (le_trans' _ _ _ hk (dv_le (j + 1 + 1) k h)) (le_succ_self_absurd (k + 1))
  | inr h => exact h

/-- `[C n k, C n (k+1), …]`, `m` entries. -/
def listC (n : Nat) : Nat → Nat → List Nat
  | _, 0 => []
  | k, m + 1 => C n k :: listC n (k + 1) m

theorem nxt_listC (n : Nat) : ∀ (m k : Nat), C n (k + 1 + m) = 0 →
    nxt (C n k) (listC n (k + 1) m) = listC (n + 1) (k + 1) (m + 1)
  | 0, k, h => by
    show [C n k] = [C n k + C n (k + 1)]
    rw [show C n (k + 1) = 0 from h]
    exact rfl
  | m + 1, k, h => by
    show (C n (k + 1) + C n k) :: nxt (C n (k + 1)) (listC n (k + 1 + 1) m)
      = (C n k + C n (k + 1)) :: listC (n + 1) (k + 1 + 1) (m + 1)
    have h' : C n (k + 1 + 1 + m) = 0 := by
      rw [nadd_assoc (k + 1) 1 m, nadd_comm 1 m]; exact h
    rw [nxt_listC n m (k + 1) h', nadd_comm (C n (k + 1))]

theorem row_eq : ∀ n, row n = listC n 0 (n + 1)
  | 0 => rfl
  | n + 1 => by
    show nxt 0 (row n) = C (n + 1) 0 :: listC (n + 1) 1 (n + 1)
    rw [row_eq n]
    show (C n 0 + 0) :: nxt (C n 0) (listC n 1 n) = C (n + 1) 0 :: listC (n + 1) 1 (n + 1)
    have h : C n (0 + 1 + n) = 0 := by rw [nadd_comm (0 + 1) n]; exact C_above' n
    rw [nxt_listC n n 0 h, C_zero_right n, C_zero_right (n + 1)]

end Choose

/-! ## Frobenius in characteristic p -/

section Frob
open FTAChain
variable {R : Type} (K : DetChain.CRing R)

theorem len_listC (n : Nat) : ∀ k m, (listC n k m).length = m
  | _, 0 => rfl
  | k, m + 1 => congrArg Nat.succ (len_listC n (k + 1) m)

theorem nsm_one (x : R) : nsm K 1 x = x := K.zero_add x

theorem nsm_mul_nat (q : Nat) (x : R) : ∀ n, nsm K (q * n) x = nsm K n (nsm K q x)
  | 0 => rfl
  | n + 1 => by
    show nsm K (q * n + q) x = K.add (nsm K n (nsm K q x)) (nsm K q x)
    rw [nsm_add, nsm_mul_nat q x n]

/-- In characteristic `p` (p • 1 = 0) every multiple of `p` kills every element. -/
theorem nsm_char (p q : Nat) (hp : nsm K p K.one = K.zero) (y : R) : nsm K (q * p) y = K.zero := by
  rw [nsm_mul_nat, nsm_eq_mul, hp, DetChain.zero_mul']

theorem ev_tail (a b : R) (n : Nat) : ∀ m k, (∀ i y, Le (i + 1) m → nsm K (C n (k + i)) y = K.zero) →
    ev K a b (listC n k (m + 1)) = K.mul (pw K a m) (nsm K (C n (k + m)) K.one)
  | 0, k, _ => by
    show K.add (nsm K (C n k) K.one) (K.mul a K.zero) = K.mul K.one (nsm K (C n k) K.one)
    rw [DetChain.mul_zero, DetChain.add_zero', K.one_mul]
  | m + 1, k, h => by
    show K.add (nsm K (C n k) (pw K b (listC n (k + 1) (m + 1)).length)) (K.mul a (ev K a b (listC n (k + 1) (m + 1))))
      = K.mul (K.mul (pw K a m) a) (nsm K (C n (k + (m + 1))) K.one)
    have h0 : nsm K (C n k) (pw K b (listC n (k + 1) (m + 1)).length) = K.zero :=
      h 0 _ ⟨m, nadd_comm 1 m⟩
    have h' : ∀ i y, Le (i + 1) m → nsm K (C n (k + 1 + i)) y = K.zero := fun i y hi => by
      rw [nadd_assoc k 1 i, nadd_comm 1 i]
      exact h (i + 1) y (le_succ (i + 1) m hi)
    rw [h0, K.zero_add, ev_tail a b n m (k + 1) h', ← K.mul_assoc, K.mul_comm a (pw K a m),
      nadd_assoc k 1 m, nadd_comm 1 m]

/-- **Frobenius is additive in characteristic p** (p = j + 2 prime, p • 1 = 0 in the ring):
    `(a + b)^p = a^p + b^p`. This holds in every residue ring `O_F/p` of every number field. -/
theorem frob_add (j : Nat) (hpr : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1)
    (hp : nsm K (j + 1 + 1) K.one = K.zero) (a b : R) :
    pw K (K.add a b) (j + 1 + 1) = K.add (pw K a (j + 1 + 1)) (pw K b (j + 1 + 1)) := by
  rw [← ev_row K a b, row_eq]
  show K.add (nsm K (C (j + 1 + 1) 0) (pw K b (listC (j + 1 + 1) 1 (j + 1 + 1)).length))
      (K.mul a (ev K a b (listC (j + 1 + 1) 1 (j + 1 + 1)))) = _
  have hv : ∀ i y, Le (i + 1) (j + 1) → nsm K (C (j + 1 + 1) (1 + i)) y = K.zero := fun i y hi => by
    rw [nadd_comm 1 i]
    obtain ⟨q, hq⟩ := dv_choose j i hpr (le_succ (i + 1) (j + 1) hi)
    rw [hq]
    exact nsm_char K (j + 1 + 1) q hp y
  rw [ev_tail K a b (j + 1 + 1) (j + 1) 1 hv, len_listC, C_zero_right, nsm_one]
  have hc : C (j + 1 + 1) (1 + (j + 1)) = 1 := by rw [nadd_comm 1 (j + 1)]; exact C_self (j + 1 + 1)
  rw [hc, nsm_one, ← K.mul_assoc, K.mul_comm a (pw K a (j + 1)), K.add_comm]
  show K.add (K.mul (K.mul (pw K a (j + 1)) a) K.one) (pw K b (j + 1 + 1)) = _
  rw [K.mul_comm _ K.one, K.one_mul]
  rfl

theorem pw_mul (a b : R) : ∀ n, pw K (K.mul a b) n = K.mul (pw K a n) (pw K b n)
  | 0 => (K.one_mul K.one).symm
  | n + 1 => by
    show K.mul (pw K (K.mul a b) n) (K.mul a b) = K.mul (K.mul (pw K a n) a) (K.mul (pw K b n) b)
    rw [pw_mul a b n, K.mul_assoc, ← K.mul_assoc (pw K b n) a b, K.mul_comm (pw K b n) a,
      K.mul_assoc a (pw K b n) b, ← K.mul_assoc (pw K a n) a]

end Frob

end Frobenius

#print axioms Frobenius.shell
#print axioms Frobenius.shell_unique
#print axioms Frobenius.nsm_add
#print axioms Frobenius.mul_nsm
#print axioms Frobenius.len_nxt
#print axioms Frobenius.add4
#print axioms Frobenius.nsm_eq_mul
#print axioms Frobenius.perm4
#print axioms Frobenius.swap3'
#print axioms Frobenius.ring_step
#print axioms Frobenius.ev_nxt
#print axioms Frobenius.ev_row
#print axioms Frobenius.C_zero_right
#print axioms Frobenius.C_above
#print axioms Frobenius.C_above'
#print axioms Frobenius.C_self
#print axioms Frobenius.C_one
#print axioms Frobenius.C_absorb
#print axioms Frobenius.le_succ_self_absurd
#print axioms Frobenius.dv_choose
#print axioms Frobenius.nxt_listC
#print axioms Frobenius.row_eq
#print axioms Frobenius.len_listC
#print axioms Frobenius.nsm_one
#print axioms Frobenius.nsm_mul_nat
#print axioms Frobenius.nsm_char
#print axioms Frobenius.ev_tail
#print axioms Frobenius.frob_add
#print axioms Frobenius.pw_mul

lang/FullTwo.lean

lean_all/lang/FullTwo.lean

/-
  lang/FullTwo — T43, cycle 394: translations by the 2-torsion points of y² = x(x − a)(x − b) (4 | #E(𝔽_p) by the Klein four group of translations by 2-torsion points).

  For y² = x³ + Ax² + Bx and a shift d with x³ + Ax² + Bx = X³ + A'X² + B'X (X = x − d), the involution of lang/G5Two on the
  shifted curve, conjugated back, is translation by (d, 0):  TT d A' B' = (shift by +d) ∘ G5Two.tau A' B' ∘ (shift by −d).
    sh, ush, sh_ush, ush_sh     shifting points of {O} ∪ 𝔽_p²
    c2_shift, onE_shift         the curve and its indicator are compatible with the shift
    TT2, onE_TT, onE_TTfix      TT is an involution preserving E ∪ {O}, without fixed points on it when A'² ≠ 4B'
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G5Two
import lang.OrbitV4

namespace FullTwo

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Step G4Gen G4Map Pairs G5Two
open FiniteRecChain (ite_pos' ite_neg')

variable (m : Nat)

def sh (d : Zp (2 * m + 1 + 1)) : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) → Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))
  | none => none
  | some p => some ((ring (2 * m + 1 + 1)).add p.1 ((ring (2 * m + 1 + 1)).neg d), p.2)

def ush (d : Zp (2 * m + 1 + 1)) : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) → Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))
  | none => none
  | some p => some ((ring (2 * m + 1 + 1)).add p.1 d, p.2)

theorem sh_ush (d : Zp (2 * m + 1 + 1)) (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) : sh m d (ush m d P) = P := by
  cases P with
  | none => rfl
  | some p => exact congrArg some (Prod.ext (show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add p.1 d) ((ring (2 * m + 1 + 1)).neg d) = p.1 by lib_ring) rfl)

theorem ush_sh (d : Zp (2 * m + 1 + 1)) (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) : ush m d (sh m d P) = P := by
  cases P with
  | none => rfl
  | some p => exact congrArg some (Prod.ext (show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add p.1 ((ring (2 * m + 1 + 1)).neg d)) d = p.1 by lib_ring) rfl)

section Shift
variable (A B A' B' d : Zp (2 * m + 1 + 1))
  (hp : ∀ x : Zp (2 * m + 1 + 1), (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul A ((ring (2 * m + 1 + 1)).mul x x))) ((ring (2 * m + 1 + 1)).mul B x) =
    (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg d)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg d)) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg d))))
      ((ring (2 * m + 1 + 1)).mul A' ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg d)) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg d))))) ((ring (2 * m + 1 + 1)).mul B' ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg d))))

def TT (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) :=
  ush m d (tau m A' B' (sh m d P))

include hp in
theorem c2_shift (x y : Zp (2 * m + 1 + 1)) : c2 m A B x y ↔ c2 m A' B' ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg d)) y := by
  unfold c2; rw [hp x]

include hp in
theorem onE_shift (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) : onE m A B P = onE m A' B' (sh m d P) := by
  cases P with
  | none => rfl
  | some p =>
    obtain ⟨x, y⟩ := p
    by_cases hc : (ring (2 * m + 1 + 1)).mul y y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul A ((ring (2 * m + 1 + 1)).mul x x))) ((ring (2 * m + 1 + 1)).mul B x)
    · rw [onE_on m A B x y hc]
      exact (onE_on m A' B' _ _ ((c2_shift m A B A' B' d hp x y).mp hc)).symm
    · rw [onE_off m A B x y hc]
      exact (onE_off m A' B' _ _ (fun h' => hc ((c2_shift m A B A' B' d hp x y).mpr h'))).symm

theorem TT2 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (hb : B' ≠ (ring (2 * m + 1 + 1)).zero) (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) :
    TT m A' B' d (TT m A' B' d P) = P := by
  unfold TT
  rw [sh_ush, tau2 m hpr A' B' hb, ush_sh]

include hp in
theorem onE_TT (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) :
    onE m A B (TT m A' B' d P) = onE m A B P := by
  unfold TT
  rw [onE_shift m A B A' B' d hp, sh_ush, onE_tau m hpr A' B', ← onE_shift m A B A' B' d hp]

include hp in
theorem onE_TTfix (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h2 : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero)
    (hd : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul A' A') ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) B')) ≠ (ring (2 * m + 1 + 1)).zero)
    (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) (h : TT m A' B' d P = P) : onE m A B P = 0 := by
  rw [onE_shift m A B A' B' d hp]
  apply onE_fix m hpr A' B' h2 hd
  unfold TT at h
  calc tau m A' B' (sh m d P) = sh m d (ush m d (tau m A' B' (sh m d P))) := (sh_ush m d _).symm
    _ = sh m d P := by rw [h]

end Shift

section Legendre
variable (a b : Zp (2 * m + 1 + 1))

/-- σ: translation by (0, 0); τa: translation by (a, 0); τb: translation by (b, 0) on y² = x(x − a)(x − b). -/
def sig : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) → Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) := tau m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (a) (b))) ((ring (2 * m + 1 + 1)).mul (a) (b))
def ta : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) → Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) := TT m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) (a)) ((ring (2 * m + 1 + 1)).neg (b))) ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg (b)))) a
def tb : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) → Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) := TT m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) (b)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).add (b) ((ring (2 * m + 1 + 1)).neg (a)))) b

theorem hpa : ∀ x : Zp (2 * m + 1 + 1), (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (a) (b))) ((ring (2 * m + 1 + 1)).mul x x))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (b)) x) = (ring (2 * m + 1 + 1)).add ((ring (2 * m +  -- (+835 chars; full line in the Zenodo source)
theorem hpb : ∀ x : Zp (2 * m + 1 + 1), (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (a) (b))) ((ring (2 * m + 1 + 1)).mul x x))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (b)) x) = (ring (2 * m + 1 + 1)).add ((ring (2 * m +  -- (+835 chars; full line in the Zenodo source)

theorem pt (p q p' q' : Zp (2 * m + 1 + 1)) (h1 : p = p') (h2 : q = q') :
    (some (p, q) : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) = some (p', q') := by rw [h1, h2]

theorem iv_neg' (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (z : Zp (2 * m + 1 + 1)) (hz : z ≠ (ring (2 * m + 1 + 1)).zero) : iv m ((ring (2 * m + 1 + 1)).neg z) = (ring (2 * m + 1 + 1)).neg (iv m z) := by
  have e := iv_mul_self m hpr z hz
  exact iv_eq m hpr _ _ (by rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg z) ((ring (2 * m + 1 + 1)).neg (iv m z)) = (ring (2 * m + 1 + 1)).mul (iv m z) z by lib_ring, e])

end Legendre

section Key
variable (a b : Zp (2 * m + 1 + 1))

theorem hba (hab : (ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg b) ≠ (ring (2 * m + 1 + 1)).zero) : (ring (2 * m + 1 + 1)).add b ((ring (2 * m + 1 + 1)).neg a) ≠ (ring (2 * m + 1 + 1)).zero := fun e => hab (by
  calc (ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg b) = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add b ((ring (2 * m + 1 + 1)).neg a)) := by lib_ring
    _ = (ring (2 * m + 1 + 1)).zero := by rw [e]; lib_ring)

/-- generic shifted translation: for x ≠ d on the curve, TT d (x, y) = (B'/(x − d) + d, …). -/
theorem TT_gen (A B A' B' d : Zp (2 * m + 1 + 1))
    (hp : ∀ x : Zp (2 * m + 1 + 1), (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul A ((ring (2 * m + 1 + 1)).mul x x))) ((ring (2 * m + 1 + 1)).mul B x) =
      (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg d)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg d)) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg d))))
        ((ring (2 * m + 1 + 1)).mul A' ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg d)) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg d))))) ((ring (2 * m + 1 + 1)).mul B' ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg d))))
    (x y : Zp (2 * m + 1 + 1)) (hc : c2 m A B x y) (hx : (ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg d) ≠ (ring (2 * m + 1 + 1)).zero) :
    TT m A' B' d (some (x, y)) = some ((ring (2 * m + 1 + 1)).add (t2x m B' ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg d))) d, t2y m B' ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg d)) y) := by
  unfold TT
  show ush m d (tau m A' B' (some ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg d), y))) = _
  rw [tau_gen m A' B' _ _ ((c2_shift m A B A' B' d hp x y).mp hc) hx]; rfl

theorem TT_T (A' B' d : Zp (2 * m + 1 + 1)) : TT m A' B' d (some (d, (ring (2 * m + 1 + 1)).zero)) = none := by
  unfold TT
  rw [show sh m d (some (d, (ring (2 * m + 1 + 1)).zero)) = some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).zero) from pt m ((ring (2 * m + 1 + 1)).add d ((ring (2 * m + 1 + 1)).neg d)) (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero (by lib_ring) rfl, G5Two.tau_T]; rfl

theorem TT_O (A' B' d : Zp (2 * m + 1 + 1)) : TT m A' B' d none = some (d, (ring (2 * m + 1 + 1)).zero) := by
  unfold TT
  show ush m d (some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).zero)) = _
  exact pt m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero d) (ring (2 * m + 1 + 1)).zero d (ring (2 * m + 1 + 1)).zero (by lib_ring) rfl

theorem sig_O : sig m a b none = some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).zero) := rfl

theorem sig_T : sig m a b (some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).zero)) = none := G5Two.tau_T m _ _

theorem sig_pt (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (c d' : Zp (2 * m + 1 + 1)) (hcd : (ring (2 * m + 1 + 1)).mul c d' = (ring (2 * m + 1 + 1)).mul a b) (hc0 : c ≠ (ring (2 * m + 1 + 1)).zero)
    (hcurve : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).zero) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (c) ((ring (2 * m + 1 + 1)).mul (c) (c))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (a) (b))) ((ring (2 * m + 1 + 1)).mul (c) (c)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 *  -- (+130 chars; full line in the Zenodo source)
  unfold sig
  rw [tau_gen m _ _ _ _ hcurve hc0]
  unfold t2x t2y
  have e := iv_mul_self m hpr c hc0
  exact pt m _ _ _ _ (by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (b)) (iv m c) = (ring (2 * m + 1 + 1)).mul d' ((ring (2 * m + 1 + 1)).mul (iv m c) c) := by rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (b)) (iv m c) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul c d') (iv m c) by rw [hcd]]; lib_ring
      _ = d' := by rw [e]; lib_ring) (by lib_ring)

theorem TT_pt (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (A B A' B' dd : Zp (2 * m + 1 + 1))
    (hp : ∀ x : Zp (2 * m + 1 + 1), (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul A ((ring (2 * m + 1 + 1)).mul x x))) ((ring (2 * m + 1 + 1)).mul B x) =
      (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg dd)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg dd)) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg dd))))
        ((ring (2 * m + 1 + 1)).mul A' ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg dd)) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg dd))))) ((ring (2 * m + 1 + 1)).mul B' ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg dd))))
    (c d' : Zp (2 * m + 1 + 1)) (hc : c2 m A B c (ring (2 * m + 1 + 1)).zero) (hx : (ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg dd) ≠ (ring (2 * m + 1 + 1)).zero)
    (hv : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add d' ((ring (2 * m + 1 + 1)).neg dd)) ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg dd)) = B') :
    TT m A' B' dd (some (c, (ring (2 * m + 1 + 1)).zero)) = some (d', (ring (2 * m + 1 + 1)).zero) := by
  rw [TT_gen m A B A' B' dd hp c _ hc hx]
  unfold t2x t2y
  have e := iv_mul_self m hpr _ hx
  exact pt m _ _ _ _ (by
    calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul B' (iv m ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg dd)))) dd
        = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add d' ((ring (2 * m + 1 + 1)).neg dd)) ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg dd))) (iv m ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg dd)))) dd := by rw [hv]
      _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add d' ((ring (2 * m + 1 + 1)).neg dd)) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg dd))) ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg dd)))) dd := by lib_ring
      _ = d' := by rw [e]; lib_ring) (by lib_ring)

end Key

section Key2
variable (a b : Zp (2 * m + 1 + 1))

theorem key_gen (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) (x y : Zp (2 * m + 1 + 1)) (hc : c2 m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (a) (b))) ((ring (2 * m + 1 + 1)).mul (a) (b)) x y)
    (hxa : (ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg a) ≠ (ring (2 * m + 1 + 1)).zero) (hxb : (ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg b) ≠ (ring (2 * m + 1 + 1)).zero) :
    sig m a b (ta m a b (some (x, y))) = tb m a b (some (x, y)) := by
  unfold ta tb
  rw [TT_gen m _ _ _ _ a (hpa m a b) x y hc hxa, TT_gen m _ _ _ _ b (hpb m a b) x y hc hxb]
  have hcs : c2 m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) (a)) ((ring (2 * m + 1 + 1)).neg (b))) ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg (b)))) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg a)) y := (c2_shift m _ _ _ _ a (hpa m a b)   -- (+10 chars; full line in the Zenodo source)
  have hon := on2 m hpr _ _ _ _ hcs hxa
  have hcX : c2 m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (a) (b))) ((ring (2 * m + 1 + 1)).mul (a) (b)) ((ring (2 * m + 1 + 1)).add (t2x m ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg (b)))) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg a))) a) (t2y m ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).add (a) ((ring (  -- (+97 chars; full line in the Zenodo source)
    apply (c2_shift m _ _ _ _ a (hpa m a b) _ _).mpr
    rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (t2x m ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg (b)))) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg a))) a) ((ring (2 * m + 1 + 1)).neg a) = t2x m ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg (b)))) ((ring (2 * m + 1 + 1)  -- (+63 chars; full line in the Zenodo source)
  have e1 := iv_mul_self m hpr _ hxa
  have e2 := iv_mul_self m hpr _ hxb
  have ea := iv_mul_self m hpr _ ha
  have hXv : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (t2x m ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg (b)))) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg a))) a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg a)) ((ring (2 * m + 1 + 1)).mul (iv m a) (iv m ((ring (2 * m + 1 + 1)).add  -- (+72 chars; full line in the Zenodo source)
    unfold t2x
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg (b)))) ((ring (2 * m + 1 + 1)).mul (iv m a) (iv m ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg b))))) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg a)))   -- (+307 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m a) a) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg b))) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg b))) := by rw [e1]; lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [ea, e2]; lib_ring
  have hX0 : (ring (2 * m + 1 + 1)).add (t2x m ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg (b)))) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg a))) a ≠ (ring (2 * m + 1 + 1)).zero := fun e => by
    rw [e] at hXv
    exact PowerSum.one_ne_zero' (2 * m + 1) (by rw [← hXv]; lib_ring)
  have hiX := iv_eq m hpr _ _ hXv
  unfold sig
  rw [tau_gen m _ _ _ _ hcX hX0]
  unfold t2x t2y
  unfold t2x at hiX
  rw [hiX]
  generalize iv m ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg a)) = i1 at e1 ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg b)) = i2 at e2 ⊢
  generalize iv m a = ia at ea ⊢
  exact pt m _ _ _ _
    (by calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (b)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg a)) ((ring (2 * m + 1 + 1)).mul ia i2)) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg a)) i2)) ((ring (2 * m + 1 + 1)).mul ia a) := by lib  -- (+5 chars; full line in the Zenodo source)
          _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).add (b) ((ring (2 * m + 1 + 1)).neg (a)))) i2) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul i2 ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg b)))) := by rw [ea]; lib_ring
          _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).add (b) ((ring (2 * m + 1 + 1)).neg (a)))) i2) b := by rw [e2]; lib_ring)
    (by calc _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg b)) ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul i2 i2)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ia a) ((ring (2 * m + 1 + 1)).mul ia a)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m +  -- (+189 chars; full line in the Zenodo source)
          _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).add (b) ((ring (2 * m + 1 + 1)).neg (a)))) ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul i2 i2))) := by rw [ea, e1]; lib_ring)

end Key2

section Key3
variable (a b : Zp (2 * m + 1 + 1))

theorem key (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) (hab : (ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg b) ≠ (ring (2 * m + 1 + 1)).zero)
    (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) : sig m a b (ta m a b P) = tb m a b P := by
  have hca : c2 m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (a) (b))) ((ring (2 * m + 1 + 1)).mul (a) (b)) a (ring (2 * m + 1 + 1)).zero := by unfold c2; lib_ring
  have hcb : c2 m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (a) (b))) ((ring (2 * m + 1 + 1)).mul (a) (b)) b (ring (2 * m + 1 + 1)).zero := by unfold c2; lib_ring
  have hc0 : c2 m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (a) (b))) ((ring (2 * m + 1 + 1)).mul (a) (b)) (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero := by unfold c2; lib_ring
  have na : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).neg a) ≠ (ring (2 * m + 1 + 1)).zero := fun e => ha (by
    calc a = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).neg a)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).zero := by rw [e]; lib_ring)
  have nb : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).neg b) ≠ (ring (2 * m + 1 + 1)).zero := fun e => hb (by
    calc b = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).neg b)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).zero := by rw [e]; lib_ring)
  have hba' := hba m a b hab
  -- the three points of order 2 and O
  have taO : ta m a b none = some (a, (ring (2 * m + 1 + 1)).zero) := TT_O m _ _ a
  have tbO : tb m a b none = some (b, (ring (2 * m + 1 + 1)).zero) := TT_O m _ _ b
  have ta0 : ta m a b (some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).zero)) = some (b, (ring (2 * m + 1 + 1)).zero) :=
    TT_pt m hpr _ _ _ _ a (hpa m a b) (ring (2 * m + 1 + 1)).zero b hc0 na (by lib_ring)
  have tb0 : tb m a b (some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).zero)) = some (a, (ring (2 * m + 1 + 1)).zero) :=
    TT_pt m hpr _ _ _ _ b (hpb m a b) (ring (2 * m + 1 + 1)).zero a hc0 nb (by lib_ring)
  have taa : ta m a b (some (a, (ring (2 * m + 1 + 1)).zero)) = none := TT_T m _ _ a
  have tbb : tb m a b (some (b, (ring (2 * m + 1 + 1)).zero)) = none := TT_T m _ _ b
  have tab : ta m a b (some (b, (ring (2 * m + 1 + 1)).zero)) = some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).zero) :=
    TT_pt m hpr _ _ _ _ a (hpa m a b) b (ring (2 * m + 1 + 1)).zero hcb hba' (by lib_ring)
  have tba : tb m a b (some (a, (ring (2 * m + 1 + 1)).zero)) = some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).zero) :=
    TT_pt m hpr _ _ _ _ b (hpb m a b) a (ring (2 * m + 1 + 1)).zero hca hab (by lib_ring)
  have sa : sig m a b (some (a, (ring (2 * m + 1 + 1)).zero)) = some (b, (ring (2 * m + 1 + 1)).zero) := sig_pt m a b hpr a b (by lib_ring) ha hca
  have sb : sig m a b (some (b, (ring (2 * m + 1 + 1)).zero)) = some (a, (ring (2 * m + 1 + 1)).zero) := sig_pt m a b hpr b a (by lib_ring) hb hcb
  cases P with
  | none => rw [taO, sa, tbO]
  | some p =>
    obtain ⟨x, y⟩ := p
    by_cases hc : (ring (2 * m + 1 + 1)).mul y y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (a) (b))) ((ring (2 * m + 1 + 1)).mul (x) (x)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (b)) (x))
    · by_cases h0 : x = (ring (2 * m + 1 + 1)).zero
      · rw [h0] at hc ⊢
        have hy := y0 m hpr y (by rw [hc]; lib_ring)
        rw [hy, ta0, sb, tb0]
      · by_cases hxa : x = a
        · rw [hxa] at hc ⊢
          have hy := y0 m hpr y (by rw [hc]; lib_ring)
          rw [hy, taa, sig_O, tba]
        · by_cases hxb : x = b
          · rw [hxb] at hc ⊢
            have hy := y0 m hpr y (by rw [hc]; lib_ring)
            rw [hy, tab, sig_T, tbb]
          · exact key_gen m a b hpr ha x y hc (G7LF.sub_ne m x a hxa) (G7LF.sub_ne m x b hxb)
    · have hoff : ¬ c2 m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (a) (b))) ((ring (2 * m + 1 + 1)).mul (a) (b)) x y := hc
      have ta_off : ta m a b (some (x, y)) = some (x, y) := by
        unfold ta TT
        show ush m a (tau m _ _ (some ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg a), y))) = _
        rw [tau_off m _ _ _ _ (fun h' => hoff ((c2_shift m _ _ _ _ a (hpa m a b) x y).mpr h'))]
        exact pt m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg a)) a) y x y (by lib_ring) rfl
      have tb_off : tb m a b (some (x, y)) = some (x, y) := by
        unfold tb TT
        show ush m b (tau m _ _ (some ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg b), y))) = _
        rw [tau_off m _ _ _ _ (fun h' => hoff ((c2_shift m _ _ _ _ b (hpb m a b) x y).mpr h'))]
        exact pt m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg b)) b) y x y (by lib_ring) rfl
      rw [ta_off, tb_off]
      unfold sig
      exact tau_off m _ _ x y hoff

end Key3

section Count
variable (a b : Zp (2 * m + 1 + 1))

theorem comm (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) (hab : (ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg b) ≠ (ring (2 * m + 1 + 1)).zero)
    (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) : sig m a b (ta m a b P) = ta m a b (sig m a b P) := by
  have hB : (ring (2 * m + 1 + 1)).mul (a) (b) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ ha hb
  have hBa : (ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg (b))) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ ha hab
  have hBb : (ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).add (b) ((ring (2 * m + 1 + 1)).neg (a))) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ hb (hba m a b hab)
  have k1 := key m a b hpr ha hb hab (ta m a b (sig m a b P))
  unfold ta at k1
  rw [TT2 m _ _ a hpr hBa] at k1
  have s2 : sig m a b (sig m a b P) = P := tau2 m hpr _ _ hB P
  rw [s2] at k1
  -- k1 : P = tb (ta (sig P))
  rw [key m a b hpr ha hb hab P]
  calc tb m a b P = tb m a b (tb m a b (ta m a b (sig m a b P))) := by
        show tb m a b P = tb m a b (tb m a b (TT m _ _ a (sig m a b P))); rw [← k1]
    _ = ta m a b (sig m a b P) := TT2 m _ _ b hpr hBb _

/-- **4 | #E(𝔽_p)** for y² = x(x − a)(x − b), a, b ≠ 0, a ≠ b, p odd. -/
theorem g4full (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) (hab : (ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg b) ≠ (ring (2 * m + 1 + 1)).zero)
    (h2 : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero) :
    ∃ k : Int, 1 + sumK LibInt.intR (cnt2 m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (a) (b))) ((ring (2 * m + 1 + 1)).mul (a) (b))) (pL m) = 4 * k := by
  have hB : (ring (2 * m + 1 + 1)).mul (a) (b) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ ha hb
  have hBa : (ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg (b))) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ ha hab
  have hd0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (a) (b))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (a) (b)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (rin  -- (+126 chars; full line in the Zenodo source)
    rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (a) (b))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (a) (b)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (rin  -- (+256 chars; full line in the Zenodo source)
    exact mul_ne m hpr _ _ hab hab
  have hda : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) (a)) ((ring (2 * m + 1 + 1)).neg (b))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) (a)) ((  -- (+420 chars; full line in the Zenodo source)
    rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) (a)) ((ring (2 * m + 1 + 1)).neg (b))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) (a)) ((  -- (+460 chars; full line in the Zenodo source)
  have hdb : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) (b)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) (b)) ((  -- (+420 chars; full line in the Zenodo source)
    rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) (b)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) (b)) ((  -- (+460 chars; full line in the Zenodo source)
  have e : sumK LibInt.intR (onE m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (a) (b))) ((ring (2 * m + 1 + 1)).mul (a) (b))) (ptsL m) = 1 + sumK LibInt.intR (cnt2 m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (a) (b))) ((ring (2 * m + 1 + 1)).mul (a) (b))) (pL m) := by
    show LibInt.intR.add 1 (sumK LibInt.intR (onE m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (a) (b))) ((ring (2 * m + 1 + 1)).mul (a) (b))) ((pL m).map some)) = _
    rw [sum_map]; rfl
  rw [← e]
  exact ⟨_, OrbitV4.orbitV4 (ptsL m) (complete_pts m) (code m) (code_inj m) (sig m a b) (ta m a b)
    (fun P => tau2 m hpr _ _ hB P) (fun P => TT2 m _ _ a hpr hBa P) (fun P => comm m a b hpr ha hb hab P)
    (onE m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (a) (b))) ((ring (2 * m + 1 + 1)).mul (a) (b))) (fun P => onE_tau m hpr _ _ P) (fun P => onE_TT m _ _ _ _ a (hpa m a b) hpr P)
    (fun P h => by
      rcases h with h | h | h
      · exact onE_fix m hpr _ _ h2 hd0 P h
      · exact onE_TTfix m _ _ _ _ a (hpa m a b) hpr h2 hda P h
      · rw [key m a b hpr ha hb hab P] at h
        exact onE_TTfix m _ _ _ _ b (hpb m a b) hpr h2 hdb P h)⟩

end Count

end FullTwo

#print axioms FullTwo.sh_ush
#print axioms FullTwo.ush_sh
#print axioms FullTwo.c2_shift
#print axioms FullTwo.onE_shift
#print axioms FullTwo.TT2
#print axioms FullTwo.onE_TT
#print axioms FullTwo.onE_TTfix
#print axioms FullTwo.hpa
#print axioms FullTwo.hpb
#print axioms FullTwo.pt
#print axioms FullTwo.iv_neg'
#print axioms FullTwo.hba
#print axioms FullTwo.TT_gen
#print axioms FullTwo.TT_T
#print axioms FullTwo.TT_O
#print axioms FullTwo.sig_O
#print axioms FullTwo.sig_T
#print axioms FullTwo.sig_pt
#print axioms FullTwo.TT_pt
#print axioms FullTwo.key_gen
#print axioms FullTwo.key
#print axioms FullTwo.comm
#print axioms FullTwo.g4full

lang/G4Count.lean

lean_all/lang/G4Count.lean

/-
  lang/G4Count — T43, cycle 365: 5 | #E(𝔽_p) for 11a: y² + y = x³ − x², every odd prime p ≠ 11 (G4).

    g4_points   Σ_{P ∈ {O} ∪ 𝔽_p²} [P ∈ E ∪ {O}] = 5k
    g4          1 + #{(x, y) ∈ 𝔽_p² : y² + y = x³ − x²} = 5k
  The translation τ by the 5-torsion point T = (0, 0) (G4Map: τ⁵ = id; G4Fix: τ preserves E ∪ {O} and has no fixed point there
  for p ≠ 11) and the orbit count Orbit5.orbit5 on the completely listed set {O} ∪ 𝔽_p² (Pairs).
  Since #E(𝔽_p) = p + 1 − a_p, this is a_p ≡ p + 1 (mod 5) for the newform 11a.
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.G4Fix

namespace G4Count

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Trans G4Map G4Fix Pairs

variable (m : Nat)

/-- **5 | #(E(𝔽_p) ∪ {O})** for 11a, p ≠ 11. -/
theorem g4_points (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h11 : eleven m ≠ (ring (2 * m + 1 + 1)).zero) : ∃ k : Int, sumK LibInt.intR (onE m) (ptsL m) = 5 * k :=
  ⟨_, Orbit5.orbit5 (ptsL m) (complete_pts m) (code m) (code_inj m) (tau m) (tau5 m hpr) (onE m) (onE_tau m hpr) (onE_fix m hpr h11)⟩

def cnt (q : Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) : Int :=
  if (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul q.2 q.2) q.2 = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul q.1 ((ring (2 * m + 1 + 1)).mul q.1 q.1)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul q.1 q.1)) then 1 else 0

theorem pts_split : sumK LibInt.intR (onE m) (ptsL m) = 1 + sumK LibInt.intR (cnt m) (pL m) := by
  show LibInt.intR.add 1 (sumK LibInt.intR (onE m) ((pL m).map some)) = _
  rw [sum_map]; rfl

/-- **G4: 5 | 1 + #E_aff(𝔽_p)** for y² + y = x³ − x² and every odd prime p ≠ 11. -/
theorem g4 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h11 : eleven m ≠ (ring (2 * m + 1 + 1)).zero) : ∃ k : Int, 1 + sumK LibInt.intR (cnt m) (pL m) = 5 * k := by
  rw [← pts_split]; exact g4_points m hpr h11

end G4Count

#print axioms G4Count.g4_points
#print axioms G4Count.pts_split
#print axioms G4Count.g4

lang/G4Fix.lean

lean_all/lang/G4Fix.lean

/-
  lang/G4Fix — T43, cycle 364: τ has no fixed point on E ∪ {O} for 11a when p ≠ 11, and τ preserves E ∪ {O} (G4, part 9).

    fixed_alg    x ≠ 0, ix = 1, τ(x, y) = (x, y), (x, y) ∈ E ⟹ 11 = 0
                 (−y/x² = x gives y = −x³; then y(2y + 1) = 0, x² = −y² − 2y, x(y + 2) = 1, so 4x² = 3, 3x = 2 and
                  11 = −9(4x² − 3) + 4(3x + 2)(3x − 2))
    no_fix       τP ≠ P for P ∈ E ∪ {O}
    onE, onE_tau the indicator of E ∪ {O} is τ-invariant
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G4Map

namespace G4Fix

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Trans G4Step G4Gen G4Map
open FiniteRecChain (ite_pos' ite_neg')

variable (m : Nat)

def eleven : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2  -- (+221 chars; full line in the Zenodo source)

theorem fixed_alg (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y i : Zp (2 * m + 1 + 1)) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (ei : (ring (2 * m + 1 + 1)).mul i x = (ring (2 * m + 1 + 1)).one)
    (fx : (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul i i)) = x) (fy : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul i i))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = y)
    (hc : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x))) : eleven m = (ring (2 * m + 1 + 1)).zero := by
  have gi : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have gX : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul (i) (i)))) ((ring (2 * m + 1 + 1)).neg (x)) = (ring (2 * m + 1 + 1)).zero := by rw [fx]; lib_ring
  have gY : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul (i) ((ring (2 * m + 1 + 1)).mul (i) (i)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg (y)) = (ring (2 * m + 1 + 1)).zero := by rw [fy]; lib_ring
  have gc : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) (y)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x))))) ((ring (2 * m + 1 + 1)).mul (x) (x)) = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
  have g1 : (ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x))) = (ring (2 * m + 1 + 1)).zero := by
    calc (ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x))) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul (i) (i)))) ((ring (2 * m + 1 + 1)).neg (x))))) ((ring (2  -- (+313 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [gX, gi]; lib_ring
  have g2 : (ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).one)) (y)) ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by
    calc (ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).one)) (y)) ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring   -- (+441 chars; full line in the Zenodo source)
          ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).mul (i) (x))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1   -- (+95 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [gY, g1, gi]; lib_ring
  have hy : y ≠ (ring (2 * m + 1 + 1)).zero := fun e => hx (by
    rw [e] at g1
    rcases mul_eq_zero m hpr _ _ (show (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x) = (ring (2 * m + 1 + 1)).zero by rw [← g1]; lib_ring) with h | h
    · exact h
    · rcases mul_eq_zero m hpr _ _ h with h' | h'
      · exact h'
      · exact h')
  have g2y : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).one)) (y)) ((ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by
    rcases mul_eq_zero m hpr _ _ g2 with h | h
    · exact absurd h hy
    · exact h
  have g3 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).mul (y) (y))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).one)) (y)) = (ring (2 * m + 1 + 1)).zero := by
    calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).mul (y) (y))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).one)) (y)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y  -- (+267 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [gc, g1]; lib_ring
  have g4 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by
    have h : (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) = (ring (2 * m + 1 + 1)).zero := by
      calc (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring   -- (+334 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [g3, g1]; lib_ring
    rcases mul_eq_zero m hpr _ _ h with h' | h'
    · exact absurd h' hy
    · exact h'
  have gA : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).mul (x) (x))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m +  -- (+107 chars; full line in the Zenodo source)
    calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).mul (x) (x))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1   -- (+1110 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [g3, g2y]; lib_ring
  have gB : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).one))) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).one))) = (ring (2 * m + 1 + 1)).zero := by
    calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).one))) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).one))) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 +  -- (+555 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [g4, g2y]; lib_ring
  calc eleven m = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).  -- (+1507 chars; full line in the Zenodo source)
        unfold eleven; lib_ring
    _ = (ring (2 * m + 1 + 1)).zero := by rw [gA, gB]; lib_ring

theorem some_ne_none' (u : Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) : (some u : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) ≠ none :=
  fun h => by cases h

/-- **No fixed points** of τ on E ∪ {O} when p ≠ 11. -/
theorem no_fix (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h11 : eleven m ≠ (ring (2 * m + 1 + 1)).zero) (x y : Zp (2 * m + 1 + 1)) (hc : cE m x y) :
    tau m (some (x, y)) ≠ some (x, y) := fun e => by
  by_cases h0 : x = (ring (2 * m + 1 + 1)).zero
  · have hc' := hc
    unfold cE at hc'
    rw [tau_some, ite_pos' _ _ hc', ite_pos' _ _ h0] at e
    by_cases hy : y = (ring (2 * m + 1 + 1)).zero
    · rw [ite_pos' _ _ hy] at e
      exact one_ne m ((congrArg Prod.fst (Option.some.inj e)).trans h0)
    · rw [ite_neg' _ _ hy] at e
      exact some_ne_none' m _ e.symm
  · rw [tau_gen m x y hc h0] at e
    have e' := Option.some.inj e
    have fx : tx m x y = x := congrArg Prod.fst e'
    have fy : ty m x y = y := congrArg Prod.snd e'
    unfold tx at fx; unfold ty at fy
    exact h11 (fixed_alg m hpr x y (iv m x) h0 (iv_mul_self m hpr x h0) fx fy hc)

theorem no_fix_O : tau m none ≠ none := fun h => by cases h

def onE : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) → Int
  | none => 1
  | some p => if (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul p.2 p.2) p.2 = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul p.1 ((ring (2 * m + 1 + 1)).mul p.1 p.1)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul p.1 p.1)) then 1 else 0

theorem onE_on (x y : Zp (2 * m + 1 + 1)) (hc : cE m x y) : onE m (some (x, y)) = 1 := by
  unfold cE at hc; show (if _ then _ else _) = _; rw [ite_pos' _ _ hc]

theorem onE_off (x y : Zp (2 * m + 1 + 1)) (hc : ¬ cE m x y) : onE m (some (x, y)) = 0 := by
  unfold cE at hc; show (if _ then _ else _) = _; rw [ite_neg' _ _ hc]

/-- **τ preserves E ∪ {O}.** -/
theorem onE_tau (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) : onE m (tau m P) = onE m P := by
  cases P with
  | none => rw [tau_O]; exact onE_on m _ _ (by unfold cE; lib_ring)
  | some p =>
    obtain ⟨x, y⟩ := p
    by_cases hc : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x))
    · rw [onE_on m x y hc]
      by_cases h0 : x = (ring (2 * m + 1 + 1)).zero
      · rw [tau_some, ite_pos' _ _ hc, ite_pos' _ _ h0]
        by_cases hy : y = (ring (2 * m + 1 + 1)).zero
        · rw [ite_pos' _ _ hy]; exact onE_on m _ _ (by unfold cE; lib_ring)
        · rw [ite_neg' _ _ hy]; rfl
      · rw [tau_gen m x y hc h0]; exact onE_on m _ _ (on_tx m hpr x y hc h0)
    · rw [tau_off m x y hc]

/-- H = 0 at the fixed points of τ. -/
theorem onE_fix (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h11 : eleven m ≠ (ring (2 * m + 1 + 1)).zero) (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) (h : tau m P = P) :
    onE m P = 0 := by
  cases P with
  | none => exact absurd h (no_fix_O m)
  | some p =>
    obtain ⟨x, y⟩ := p
    by_cases hc : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x))
    · exact absurd h (no_fix m hpr h11 x y hc)
    · exact onE_off m x y hc

end G4Fix

#print axioms G4Fix.fixed_alg
#print axioms G4Fix.some_ne_none'
#print axioms G4Fix.no_fix
#print axioms G4Fix.no_fix_O
#print axioms G4Fix.onE_on
#print axioms G4Fix.onE_off
#print axioms G4Fix.onE_tau
#print axioms G4Fix.onE_fix

lang/G4Gen.lean

lean_all/lang/G4Gen.lean

/-
  lang/G4Gen — T43, cycle 362: the generic points of 11a: y² + y = x³ − x² (G4, part 7).

    mul_eq_zero   a·b = 0 ⟹ a = 0 ∨ b = 0 in 𝔽_p
    x01, x01_2, x01_3   x^k(x − 1) = 0 ⟹ x = 0 ∨ x = 1 (k = 1, 2, 3)
    gen_y, gen_y1, gen_s, gen_w
                  for (x, y) ∈ E with x ≠ 0, 1: y ≠ 0, y + 1 ≠ 0, x² + y ≠ 0, x² − y − 1 ≠ 0
  So every point with x ∉ {0, 1} satisfies all the side conditions of G4Trans … G4Step4.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G4Step4

namespace G4Gen

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Trans

variable (m : Nat)

theorem mul_eq_zero (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b : Zp (2 * m + 1 + 1)) (h : (ring (2 * m + 1 + 1)).mul a b = (ring (2 * m + 1 + 1)).zero) : a = (ring (2 * m + 1 + 1)).zero ∨ b = (ring (2 * m + 1 + 1)).zero := by
  by_cases ha : a = (ring (2 * m + 1 + 1)).zero
  · exact Or.inl ha
  · refine Or.inr ?_
    have e := iv_mul_self m hpr a ha
    generalize iv m a = c at e
    calc b = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul c a) b := by rw [e]; lib_ring
      _ = (ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul a b) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).zero := by rw [h]; lib_ring

theorem x01 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x : Zp (2 * m + 1 + 1)) (h : (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero) :
    x = (ring (2 * m + 1 + 1)).zero ∨ x = (ring (2 * m + 1 + 1)).one := by
  rcases mul_eq_zero m hpr _ _ h with e | e
  · exact Or.inl e
  · exact Or.inr (by calc x = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [e]; lib_ring)

theorem x01_2 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x : Zp (2 * m + 1 + 1)) (h : (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))) = (ring (2 * m + 1 + 1)).zero) :
    x = (ring (2 * m + 1 + 1)).zero ∨ x = (ring (2 * m + 1 + 1)).one := by
  rcases mul_eq_zero m hpr _ _ h with e | e
  · exact Or.inl e
  · exact x01 m hpr x e

theorem x01_3 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x : Zp (2 * m + 1 + 1)) (h : (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)))) = (ring (2 * m + 1 + 1)).zero) :
    x = (ring (2 * m + 1 + 1)).zero ∨ x = (ring (2 * m + 1 + 1)).one := by
  rcases mul_eq_zero m hpr _ _ h with e | e
  · exact Or.inl e
  · exact x01_2 m hpr x e

theorem gen_y (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1)) (hc : cE m x y) (h0 : x ≠ (ring (2 * m + 1 + 1)).zero) (h1 : x ≠ (ring (2 * m + 1 + 1)).one) : y ≠ (ring (2 * m + 1 + 1)).zero := fun e => by
  unfold cE at hc; rw [e] at hc
  rcases x01_2 m hpr x (by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x))) (ring (2 * m + 1 + 1)).zero := by lib_ring
      _ = (ring (2 * m + 1 + 1)).zero := by rw [← hc]; lib_ring) with e' | e'
  · exact h0 e'
  · exact h1 e'

theorem gen_y1 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1)) (hc : cE m x y) (h0 : x ≠ (ring (2 * m + 1 + 1)).zero) (h1 : x ≠ (ring (2 * m + 1 + 1)).one) : (ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero := fun e => by
  unfold cE at hc
  have hy : y = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one := by
    calc y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
      _ = _ := by rw [e]; lib_ring
  rw [hy] at hc
  rcases x01_2 m hpr x (by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x)))
          ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).zero := by rw [← hc]; lib_ring) with e' | e'
  · exact h0 e'
  · exact h1 e'

theorem gen_s (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1)) (hc : cE m x y) (h0 : x ≠ (ring (2 * m + 1 + 1)).zero) (h1 : x ≠ (ring (2 * m + 1 + 1)).one) : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y ≠ (ring (2 * m + 1 + 1)).zero := fun e => by
  unfold cE at hc
  have hy : y = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x) := by
    calc y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x)) := by lib_ring
      _ = _ := by rw [e]; lib_ring
  rw [hy] at hc
  rcases x01_3 m hpr x (by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x)))
          ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x)))) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring) with e' | e'
  · exact h0 e'
  · exact h1 e'

theorem gen_w (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1)) (hc : cE m x y) (h0 : x ≠ (ring (2 * m + 1 + 1)).zero) (h1 : x ≠ (ring (2 * m + 1 + 1)).one) :
    (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg y)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ≠ (ring (2 * m + 1 + 1)).zero := fun e => by
  unfold cE at hc
  have hy : y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by
    calc y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg y)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))) := by lib_ring
      _ = _ := by rw [e]; lib_ring
  rw [hy] at hc
  rcases x01_3 m hpr x (by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring  -- (+51 chars; full line in the Zenodo source)
          ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x)))) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring) with e' | e'
  · exact h0 e'
  · exact h1 e'

end G4Gen

#print axioms G4Gen.mul_eq_zero
#print axioms G4Gen.x01
#print axioms G4Gen.x01_2
#print axioms G4Gen.x01_3
#print axioms G4Gen.gen_y
#print axioms G4Gen.gen_y1
#print axioms G4Gen.gen_s
#print axioms G4Gen.gen_w

lang/G4Map.lean

lean_all/lang/G4Map.lean

/-
  lang/G4Map — T43, cycle 363: the translation τ = (· + T) on {O} ∪ 𝔽_p² for 11a: y² + y = x³ − x², and τ⁵ = id (G4, part 8).

    tau          O ↦ (0, 0); (0, 0) ↦ (1, −1); (0, y ≠ 0) ↦ O on E; (x ≠ 0, y) ↦ (−y/x², y²/x³ − 1) on E; identity off E
    tau_tors     O → (0, 0) → (1, −1) → (1, 0) → (0, −1) → O
    tau_gen5     τ⁵ P = P for P ∈ E with x ∉ {0, 1} (the chain G4Trans → G4Step → G4Step3 → G4Step4)
    tau5         τ⁵ = id on all of {O} ∪ 𝔽_p²
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G4Gen

namespace G4Map

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Trans G4Step G4Step3 G4Step4 G4Gen
open FiniteRecChain (ite_pos' ite_neg')

variable (m : Nat)

abbrev Pt := Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))

def tau : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) → Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))
  | none => some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).zero)
  | some p => if (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul p.2 p.2) p.2 = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul p.1 ((ring (2 * m + 1 + 1)).mul p.1 p.1)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul p.1 p.1)) then
      (if p.1 = (ring (2 * m + 1 + 1)).zero then (if p.2 = (ring (2 * m + 1 + 1)).zero then some ((ring (2 * m + 1 + 1)).one, (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) else none) else some (tx m p.1 p.2, ty m p.1 p.2))
    else some p

theorem tau_some (x y : Zp (2 * m + 1 + 1)) : tau m (some (x, y)) =
    if (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x)) then
      (if x = (ring (2 * m + 1 + 1)).zero then (if y = (ring (2 * m + 1 + 1)).zero then some ((ring (2 * m + 1 + 1)).one, (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) else none) else some (tx m x y, ty m x y))
    else some (x, y) := rfl

theorem tau_gen (x y : Zp (2 * m + 1 + 1)) (hc : cE m x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) : tau m (some (x, y)) = some (tx m x y, ty m x y) := by
  unfold cE at hc
  rw [tau_some, ite_pos' _ _ hc, ite_neg' _ _ hx]

theorem tau_off (x y : Zp (2 * m + 1 + 1)) (hc : ¬ cE m x y) : tau m (some (x, y)) = some (x, y) := by
  unfold cE at hc
  rw [tau_some, ite_neg' _ _ hc]

theorem on_tx (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1)) (hc : cE m x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) : cE m (tx m x y) (ty m x y) :=
  tau_on m hpr x y hc hx

theorem mul_ne (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b : Zp (2 * m + 1 + 1)) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) : (ring (2 * m + 1 + 1)).mul a b ≠ (ring (2 * m + 1 + 1)).zero := fun e => by
  rcases mul_eq_zero m hpr a b e with h | h
  · exact ha h
  · exact hb h

theorem neg_ne (a : Zp (2 * m + 1 + 1)) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) : (ring (2 * m + 1 + 1)).neg a ≠ (ring (2 * m + 1 + 1)).zero := fun e => ha (by
  calc a = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).neg a) := by lib_ring
    _ = (ring (2 * m + 1 + 1)).zero := by rw [e]; lib_ring)

theorem one_ne : (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero := PowerSum.one_ne_zero' (2 * m + 1)

theorem iv_one (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : iv m (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).one := iv_eq m hpr _ _ (by lib_ring)

theorem tau_O : tau m none = some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).zero) := rfl

theorem tau_T : tau m (some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).zero)) = some ((ring (2 * m + 1 + 1)).one, (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by
  rw [tau_some, ite_pos' _ _ (show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero) (ring (2 * m + 1 + 1)).zero = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mu  -- (+109 chars; full line in the Zenodo source)

theorem tau_2T (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : tau m (some ((ring (2 * m + 1 + 1)).one, (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) = some ((ring (2 * m + 1 + 1)).one, (ring (2 * m + 1 + 1)).zero) := by
  rw [tau_gen m _ _ (by unfold cE; lib_ring) (one_ne m)]
  unfold tx ty; rw [iv_one m hpr]
  exact congrArg some (Prod.ext (by show (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).one; lib_ring)
    (by show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).  -- (+46 chars; full line in the Zenodo source)

theorem tau_3T (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : tau m (some ((ring (2 * m + 1 + 1)).one, (ring (2 * m + 1 + 1)).zero)) = some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by
  rw [tau_gen m _ _ (by unfold cE; lib_ring) (one_ne m)]
  unfold tx ty; rw [iv_one m hpr]
  exact congrArg some (Prod.ext (by show (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero; lib_ring)
    (by show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1))  -- (+16 chars; full line in the Zenodo source)

theorem tau_4T : tau m (some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) = none := by
  rw [tau_some, ite_pos' _ _ (show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero   -- (+213 chars; full line in the Zenodo source)

theorem tau_tors (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : tau m (tau m (tau m (tau m (tau m none)))) = none := by
  rw [tau_O, tau_T, tau_2T m hpr, tau_3T m hpr, tau_4T]

theorem tau_gen5 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1)) (hc : cE m x y) (h0 : x ≠ (ring (2 * m + 1 + 1)).zero) (h1 : x ≠ (ring (2 * m + 1 + 1)).one) :
    tau m (tau m (tau m (tau m (tau m (some (x, y)))))) = some (x, y) := by
  have hy := gen_y m hpr x y hc h0 h1
  have hy1 := gen_y1 m hpr x y hc h0 h1
  have hs := gen_s m hpr x y hc h0 h1
  have hw := gen_w m hpr x y hc h0 h1
  have ix := iv_nz m hpr x h0
  have iy := iv_nz m hpr y hy
  have iy1 := iv_nz m hpr _ hy1
  -- step 1
  have c1 := on_tx m hpr x y hc h0
  have n1 : tx m x y ≠ (ring (2 * m + 1 + 1)).zero := neg_ne m _ (mul_ne m hpr _ _ hy (mul_ne m hpr _ _ ix ix))
  -- step 2
  have t2 := tau2 m hpr x y hc h0 hy
  have c2 := on_tx m hpr _ _ c1 n1
  rw [t2.1, t2.2] at c2
  have n2 := mul_ne m hpr _ _ h0 (mul_ne m hpr _ _ hs (mul_ne m hpr _ _ iy iy))
  -- step 3
  have t3 := tau3 m hpr x y hc h0 hy hy1 hs
  have c3 := on_tx m hpr _ _ c2 n2
  rw [t3.1, t3.2] at c3
  have n3 := mul_ne m hpr _ _ h0 (mul_ne m hpr _ _ hw (mul_ne m hpr _ _ iy1 iy1))
  -- step 4
  have t4 := tau4 m hpr x y hc h0 hy1 hw
  have c4 := on_tx m hpr _ _ c3 n3
  rw [t4.1, t4.2] at c4
  have n4 := mul_ne m hpr _ _ hy1 (mul_ne m hpr _ _ ix ix)
  have t5 := tau5 m hpr x y hc h0 hy1
  rw [tau_gen m x y hc h0, tau_gen m _ _ c1 n1, t2.1, t2.2, tau_gen m _ _ c2 n2, t3.1, t3.2, tau_gen m _ _ c3 n3, t4.1, t4.2,
    tau_gen m _ _ c4 n4, t5.1, t5.2]

theorem y_tors (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (y : Zp (2 * m + 1 + 1)) (h : (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero) :
    y = (ring (2 * m + 1 + 1)).zero ∨ y = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one := by
  rcases mul_eq_zero m hpr _ _ h with e | e
  · exact Or.inl e
  · exact Or.inr (by calc y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
      _ = _ := by rw [e]; lib_ring)

/-- **τ⁵ = id** on {O} ∪ 𝔽_p². -/
theorem tau5 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) :
    tau m (tau m (tau m (tau m (tau m P)))) = P := by
  cases P with
  | none => exact tau_tors m hpr
  | some p =>
    obtain ⟨x, y⟩ := p
    by_cases hc : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x))
    · by_cases h0 : x = (ring (2 * m + 1 + 1)).zero
      · have hy : (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by
          unfold cE at hc; rw [h0] at hc
          calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) y := by lib_ring
            _ = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
        rw [h0]
        rcases y_tors m hpr y hy with e | e <;> rw [e]
        · rw [tau_T, tau_2T m hpr, tau_3T m hpr, tau_4T, tau_O]
        · rw [tau_4T, tau_O, tau_T, tau_2T m hpr, tau_3T m hpr]
      · by_cases h1 : x = (ring (2 * m + 1 + 1)).one
        · have hy : (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by
            unfold cE at hc; rw [h1] at hc
            calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) y := by lib_ring
              _ = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
          rw [h1]
          rcases y_tors m hpr y hy with e | e <;> rw [e]
          · rw [tau_3T m hpr, tau_4T, tau_O, tau_T, tau_2T m hpr]
          · rw [tau_2T m hpr, tau_3T m hpr, tau_4T, tau_O, tau_T]
        · exact tau_gen5 m hpr x y hc h0 h1
    · rw [tau_off m x y hc, tau_off m x y hc, tau_off m x y hc, tau_off m x y hc, tau_off m x y hc]

end G4Map

#print axioms G4Map.tau_some
#print axioms G4Map.tau_gen
#print axioms G4Map.tau_off
#print axioms G4Map.on_tx
#print axioms G4Map.mul_ne
#print axioms G4Map.neg_ne
#print axioms G4Map.one_ne
#print axioms G4Map.iv_one
#print axioms G4Map.tau_O
#print axioms G4Map.tau_T
#print axioms G4Map.tau_2T
#print axioms G4Map.tau_3T
#print axioms G4Map.tau_4T
#print axioms G4Map.tau_tors
#print axioms G4Map.tau_gen5
#print axioms G4Map.y_tors
#print axioms G4Map.tau5

lang/G4Step.lean

lean_all/lang/G4Step.lean

/-
  lang/G4Step — T43, cycle 359: the step P + T ↦ P + 2T on 11a: y² + y = x³ − x² (G4, part 4).

    iv_eq     a·b = 1 ⟹ 1/a = b
    iv_tx     1/x(P + T) = −x²/y
    tau2      for P = (x, y) ∈ E, x ≠ 0, y ≠ 0: τ(τ P) = (x(x² + y)/y², −(x² + y)²/y³ − 1)
  Polynomial certificates (multipliers of i·x − 1, j·y − 1 and the curve equation) found by a std-lib Python reduction.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G4Trans

namespace G4Step

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Trans

variable (m : Nat)

def tx (x y : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul (iv m x) (iv m x)))
def ty (x y : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul (iv m x) ((ring (2 * m + 1 + 1)).mul (iv m x) (iv m x)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)

theorem iv_eq (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b : Zp (2 * m + 1 + 1)) (h : (ring (2 * m + 1 + 1)).mul a b = (ring (2 * m + 1 + 1)).one) : iv m a = b := by
  by_cases ha : a = (ring (2 * m + 1 + 1)).zero
  · rw [ha, ConicSum.iv_zero]
    rw [ha] at h
    calc (ring (2 * m + 1 + 1)).zero = (ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero b) := by lib_ring
      _ = b := by rw [h]; lib_ring
  · have e := iv_mul_self m hpr a ha
    generalize iv m a = c at e ⊢
    calc c = (ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul a b) := by rw [h]; lib_ring
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul c a) b := by lib_ring
      _ = b := by rw [e]; lib_ring

theorem iv_tx (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1)) (hc : cE m x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hy : y ≠ (ring (2 * m + 1 + 1)).zero) :
    iv m (tx m x y) = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) (iv m y)) := by
  apply iv_eq m hpr
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  have ej : (ring (2 * m + 1 + 1)).mul (iv m y) y = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr y hy
  unfold cE at hc
  unfold tx
  generalize iv m x = i at ei ⊢
  generalize iv m y = j at ej ⊢
  have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i x) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have gjy : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j y) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x)))) = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
  calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul i i))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) j)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j (y)) ((r  -- (+405 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).one := by rw [gix, gjy]; lib_ring

/-- **P + 2T.** -/
theorem tau2 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1)) (hc : cE m x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hy : y ≠ (ring (2 * m + 1 + 1)).zero) :
    tx m (tx m x y) (ty m x y) = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y) ((ring (2 * m + 1 + 1)).mul (iv m y) (iv m y))) ∧
    ty m (tx m x y) (ty m x y) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y)) ((ring (2 * m + 1 + 1)).mul (iv m y) ((ring (2 * m + 1 + 1)).mul (iv m y) (iv m y))))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m  -- (+21 chars; full line in the Zenodo source)
  have hX := iv_tx m hpr x y hc hx hy
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  have ej : (ring (2 * m + 1 + 1)).mul (iv m y) y = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr y hy
  unfold cE at hc
  rw [show tx m (tx m x y) (ty m x y) = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (ty m x y) ((ring (2 * m + 1 + 1)).mul (iv m (tx m x y)) (iv m (tx m x y)))) from rfl,
    show ty m (tx m x y) (ty m x y) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ty m x y) (ty m x y)) ((ring (2 * m + 1 + 1)).mul (iv m (tx m x y)) ((ring (2 * m + 1 + 1)).mul (iv m (tx m x y)) (iv m (tx m x y))))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) from rfl, hX]
  unfold ty
  generalize iv m x = i at ei ⊢
  generalize iv m y = j at ej ⊢
  have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i x) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have gjy : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j y) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x)))) = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
  constructor
  · calc (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul i i))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) j)) ((ri  -- (+3284 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y) ((ring (2 * m + 1 + 1)).mul j j)) := by rw [gix, gjy, gcv]; lib_ring
  · calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul i i))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (  -- (+13604 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y)) ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).mul j j)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by rw [gix, gjy, gcv];   -- (+8 chars; full line in the Zenodo source)

end G4Step

#print axioms G4Step.iv_eq
#print axioms G4Step.iv_tx
#print axioms G4Step.tau2

lang/G4Step3.lean

lean_all/lang/G4Step3.lean

/-
  lang/G4Step3 — T43, cycle 360: the step P + 2T ↦ P + 3T = P − 2T on 11a: y² + y = x³ − x² (G4, part 5).

    ty_tx     X·b = 1 ⟹ y(τQ) = x(τQ)²·X − 1 (for Q = (X, Y))
    mul_x2, iv_x2   x(P + 2T) · y²/(x(x² + y)) = 1
    tau3      for P = (x, y) ∈ E with x, y, y + 1, x² + y ≠ 0:
              τ(P + 2T) = (x(x² − y − 1)/(y + 1)², −(x² − y − 1)²/(y + 1)³) = P − 2T
  Certificates (multipliers of i·x − 1, j·y − 1, k(y + 1) − 1, l(x² + y) − 1 and the curve) from a std-lib Python reduction.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G4Step

namespace G4Step3

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Trans G4Step

variable (m : Nat)

theorem ty_tx (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (X Y b : Zp (2 * m + 1 + 1)) (h : (ring (2 * m + 1 + 1)).mul X b = (ring (2 * m + 1 + 1)).one) :
    ty m X Y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (tx m X Y) (tx m X Y)) X) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by
  unfold tx ty
  rw [iv_eq m hpr X b h]
  have e : (ring (2 * m + 1 + 1)).mul b X = (ring (2 * m + 1 + 1)).one := by rw [← h]; lib_ring
  calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul Y Y) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul b b))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)
      = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul Y ((ring (2 * m + 1 + 1)).mul b b))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul Y ((ring (2 * m + 1 + 1)).mul b b)))) X) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))
          ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul Y Y) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul b b))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b X)))) := by lib_ring
    _ = _ := by rw [e]; lib_ring

theorem mul_x2 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1)) (hc : cE m x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hy : y ≠ (ring (2 * m + 1 + 1)).zero) (hy1 : (ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero) (hs : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y ≠ (ring (2 * m + 1 + 1)).zero) :
    (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y) ((ring (2 * m + 1 + 1)).mul (iv m y) (iv m y)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul (iv m x) (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y)))) = (ring (2 * m + 1 + 1)).on  -- (+7 chars; full line in the Zenodo source)
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  have ej : (ring (2 * m + 1 + 1)).mul (iv m y) y = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr y hy
  have ek : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hy1
  have el : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hs
  unfold cE at hc
  generalize iv m x = i at ei ⊢
  generalize iv m y = j at ej ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = k at ek ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y) = l at el ⊢
  have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i x) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have gjy : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j y) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring
  have gky : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by rw [ek]; lib_ring
  have gls : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul l ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by rw [el]; lib_ring
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x)))) = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
  calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y) ((ring (2 * m + 1 + 1)).mul (j) (j)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul (i) (l))) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m +  -- (+1353 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).one := by rw [gix, gjy, gls]; lib_ring

theorem iv_x2 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1)) (hc : cE m x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hy : y ≠ (ring (2 * m + 1 + 1)).zero) (hy1 : (ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero) (hs : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y ≠ (ring (2 * m + 1 + 1)).zero) :
    iv m ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y) ((ring (2 * m + 1 + 1)).mul (iv m y) (iv m y)))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul (iv m x) (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y))) := iv_eq m hpr _ _ (mul_x2 m hpr x y hc hx hy hy1  -- (+4 chars; full line in the Zenodo source)

/-- **P + 3T = P − 2T.** -/
theorem tau3 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1)) (hc : cE m x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hy : y ≠ (ring (2 * m + 1 + 1)).zero) (hy1 : (ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero) (hs : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y ≠ (ring (2 * m + 1 + 1)).zero) :
    tx m ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y) ((ring (2 * m + 1 + 1)).mul (iv m y) (iv m y)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y) ((ring (2 * m + 1 + 1)).add ((ring (2 * m +   -- (+563 chars; full line in the Zenodo source)
    ty m ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y) ((ring (2 * m + 1 + 1)).mul (iv m y) (iv m y)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y) ((ring (2 * m + 1 + 1)).add ((ring (2 * m +   -- (+866 chars; full line in the Zenodo source)
  have hX := iv_x2 m hpr x y hc hx hy hy1 hs
  have h1 : tx m ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y) ((ring (2 * m + 1 + 1)).mul (iv m y) (iv m y)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y) ((ring (2 * m + 1 + 1)).add ((ring (  -- (+575 chars; full line in the Zenodo source)
    unfold tx
    rw [hX]
    have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
    have ej : (ring (2 * m + 1 + 1)).mul (iv m y) y = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr y hy
    have ek : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hy1
    have el : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hs
    unfold cE at hc
    generalize iv m x = i at ei ⊢
    generalize iv m y = j at ej ⊢
    generalize iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = k at ek ⊢
    generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y) = l at el ⊢
    have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i x) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
    have gjy : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j y) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring
    have gky : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by rw [ek]; lib_ring
    have gls : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul l ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by rw [el]; lib_ring
    have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x)))) = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
    calc (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y)) ((ring (2 * m + 1 + 1)).mul (j) ((ring (2 * m + 1 + 1)).mul (j) (j))))) ((ring (2 * m + 1 + 1))  -- (+38884 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg y)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (k) (k))) := by rw [gix, gjy, gky, gls, gcv]; lib_ring
  refine ⟨h1, ?_⟩
  rw [ty_tx m hpr _ _ _ (mul_x2 m hpr x y hc hx hy hy1 hs), h1]
  have ej : (ring (2 * m + 1 + 1)).mul (iv m y) y = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr y hy
  have ek : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hy1
  unfold cE at hc
  generalize iv m y = j at ej ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = k at ek ⊢
  obtain ⟨w, hw⟩ : ∃ w, w = (ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one := ⟨_, rfl⟩
  have gjy : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j y) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring
  have gkz : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k w) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by rw [hw, ek]; lib_ring
  have gzz : (ring (2 * m + 1 + 1)).add w ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [hw]; lib_ring
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x)))) = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
  calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg y)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (k) (k)))) ((ring (2 * m + 1 + 1)).mul (x) (  -- (+59141 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg y)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg y)) ((ring (2   -- (+158 chars; full line in the Zenodo source)

end G4Step3

#print axioms G4Step3.ty_tx
#print axioms G4Step3.mul_x2
#print axioms G4Step3.iv_x2
#print axioms G4Step3.tau3

lang/G4Step4.lean

lean_all/lang/G4Step4.lean

/-
  lang/G4Step4 — T43, cycle 361: the last two steps P − 2T ↦ P − T ↦ P on 11a: y² + y = x³ − x² (G4, part 6).

    mul_x3          x(P − 2T)·(y + 1)²/(x(x² − y − 1)) = 1, i.e. 1/x(P − 2T) = (y + 1)²/(x(x² − y − 1))
    tau4            τ(P − 2T) = P − T = ((y + 1)/x², −(y + 1)²/x³)        (x, y + 1, x² − y − 1 ≠ 0)
    mul_x4          x(P − T)·x²/(y + 1) = 1
    tau5            τ(P − T) = P                                             (x, y + 1 ≠ 0)
  Certificates from the phased std-lib Python reduction (clear i, j, k; w = y + 1; then the curve).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G4Step3

namespace G4Step4

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Trans G4Step G4Step3

variable (m : Nat)

theorem mul_x3 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1)) (hc : cE m x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hy1 : (ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero) (hw0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring  -- (+84 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one)) (iv m ((ring (2 * m + 1 + 1)).  -- (+485 chars; full line in the Zenodo source)
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  have ek : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hy1
  have ej : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m  -- (+73 chars; full line in the Zenodo source)
  unfold cE at hc
  generalize iv m x = i at ei ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = k at ek ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) = j at ej ⊢
  obtain ⟨w, hw⟩ : ∃ w, w = (ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one := ⟨_, rfl⟩
  have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have gkz : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (k) (w)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [hw, ek]; lib_ring
  have gzz : (ring (2 * m + 1 + 1)).add (w) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one))) = (ring (2 * m + 1 + 1)).zero := by rw [hw]; lib_ring
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) (y)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (x) (x))))) = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
  have gjw : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (j) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring
  calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).mul (k) (k)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).a  -- (+3537 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).one := by rw [gix, gkz, gjw, gzz]; lib_ring

/-- **P + 4T = P − T.** -/
theorem tau4 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1)) (hc : cE m x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hy1 : (ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero) (hw0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (  -- (+82 chars; full line in the Zenodo source)
    tx m ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one)) (iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1  -- (+2293 chars; full line in the Zenodo source)
  have hX := iv_eq m hpr _ _ (mul_x3 m hpr x y hc hx hy1 hw0)
  have h1 : tx m ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one)) (iv m ((ring (2 * m + 1 + 1)).add y (ring (2  -- (+879 chars; full line in the Zenodo source)
    unfold tx
    rw [hX]
    have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
    have ek : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hy1
    have ej : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 *  -- (+75 chars; full line in the Zenodo source)
    unfold cE at hc
    generalize iv m x = i at ei ⊢
    generalize iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = k at ek ⊢
    generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) = j at ej ⊢
    obtain ⟨w, hw⟩ : ∃ w, w = (ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one := ⟨_, rfl⟩
    have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
    have gkz : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (k) (w)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [hw, ek]; lib_ring
    have gzz : (ring (2 * m + 1 + 1)).add (w) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one))) = (ring (2 * m + 1 + 1)).zero := by rw [hw]; lib_ring
    have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) (y)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (x) (x))))) = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
    have gjw : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (j) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring
    calc (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2   -- (+1649 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (i) (i)) := by rw [ej, ek]; lib_ring
  refine ⟨h1, ?_⟩
  rw [ty_tx m hpr _ _ _ (mul_x3 m hpr x y hc hx hy1 hw0), h1]
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  have ek : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hy1
  have ej : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m  -- (+73 chars; full line in the Zenodo source)
  unfold cE at hc
  generalize iv m x = i at ei ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = k at ek ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) = j at ej ⊢
  obtain ⟨w, hw⟩ : ∃ w, w = (ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one := ⟨_, rfl⟩
  have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have gkz : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (k) (w)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [hw, ek]; lib_ring
  have gzz : (ring (2 * m + 1 + 1)).add (w) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one))) = (ring (2 * m + 1 + 1)).zero := by rw [hw]; lib_ring
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) (y)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (x) (x))))) = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
  have gjw : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (j) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring
  calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (i) (i))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (i) (i)))) ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1  -- (+15549 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).mul (i) ((ring (2 * m + 1 + 1)).mul (i) (i)))) := by rw [gix, gkz, gzz, gcv]; lib_ring

theorem mul_x4 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1)) (hc : cE m x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hy1 : (ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero) :
    (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (iv m x) (iv m x))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (x) (x)) (iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one))) = (ring (2 * m + 1 + 1)).one := by
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  have ek : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hy1
  unfold cE at hc
  generalize iv m x = i at ei ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = k at ek ⊢
  obtain ⟨w, hw⟩ : ∃ w, w = (ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one := ⟨_, rfl⟩
  have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have gkz : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (k) (w)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [hw, ek]; lib_ring
  have gzz : (ring (2 * m + 1 + 1)).add (w) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one))) = (ring (2 * m + 1 + 1)).zero := by rw [hw]; lib_ring
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) (y)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (x) (x))))) = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
  calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (i) (i))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (x) (x)) (k)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).ad  -- (+772 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).one := by rw [gix, gkz, gzz]; lib_ring

/-- **P + 5T = P.** -/
theorem tau5 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1)) (hc : cE m x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hy1 : (ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero) :
    tx m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (iv m x) (iv m x))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).mul (iv m x) ((ring   -- (+497 chars; full line in the Zenodo source)
  have hX := iv_eq m hpr _ _ (mul_x4 m hpr x y hc hx hy1)
  have h1 : tx m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (iv m x) (iv m x))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).mul (iv m x)  -- (+60 chars; full line in the Zenodo source)
    unfold tx
    rw [hX]
    have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
    have ek : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hy1
    unfold cE at hc
    generalize iv m x = i at ei ⊢
    generalize iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = k at ek ⊢
    obtain ⟨w, hw⟩ : ∃ w, w = (ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one := ⟨_, rfl⟩
    have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
    have gkz : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (k) (w)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [hw, ek]; lib_ring
    have gzz : (ring (2 * m + 1 + 1)).add (w) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one))) = (ring (2 * m + 1 + 1)).zero := by rw [hw]; lib_ring
    have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) (y)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (x) (x))))) = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
    calc (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).mul (i) ((ring (2 * m + 1 + 1)).mul (i) (i))))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul  -- (+3188 chars; full line in the Zenodo source)
      _ = x := by rw [gix, gkz, gzz]; lib_ring
  refine ⟨h1, ?_⟩
  rw [ty_tx m hpr _ _ _ (mul_x4 m hpr x y hc hx hy1), h1]
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  have ek : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hy1
  unfold cE at hc
  generalize iv m x = i at ei ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = k at ek ⊢
  obtain ⟨w, hw⟩ : ∃ w, w = (ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one := ⟨_, rfl⟩
  have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have gkz : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (k) (w)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [hw, ek]; lib_ring
  have gzz : (ring (2 * m + 1 + 1)).add (w) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one))) = (ring (2 * m + 1 + 1)).zero := by rw [hw]; lib_ring
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) (y)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (x) (x))))) = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
  calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (i) (i)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).o  -- (+307 chars; full line in the Zenodo source)
    _ = y := by rw [gix]; lib_ring

end G4Step4

#print axioms G4Step4.mul_x3
#print axioms G4Step4.tau4
#print axioms G4Step4.mul_x4
#print axioms G4Step4.tau5

lang/G4Trans.lean

lean_all/lang/G4Trans.lean

/-
  lang/G4Trans — T43, cycle 358: the translation by the 5-torsion point T = (0, 0) on 11a: E: y² + y = x³ − x² (G4, part 3).

  For P = (x, y) ∈ E with x ≠ 0 the chord through P and T gives
      P + T = (−y/x², y²/x³ − 1),        P − T = ((y + 1)/x², −(y + 1)²/x³),
      P + 2T = (x(x² + y)/y², −(x² + y)²/y³ − 1),   P − 2T = (x(x² − y − 1)/(y + 1)², −(x² − y − 1)²/(y + 1)³),
  and on the torsion O → T → (1, −1) → (1, 0) → (0, −1) → O.
    cE             the curve equation as a predicate
    tau_on         P ∈ E, x ≠ 0 ⟹ P + T ∈ E
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Pairs

namespace G4Trans

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed

variable (m : Nat)

def cE (x y : Zp (2 * m + 1 + 1)) : Prop := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x))

/-- **P + T stays on E**: (−y/x², y²/x³ − 1) satisfies the curve equation. -/
theorem tau_on (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1)) (hc : cE m x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) :
    cE m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul (iv m x) (iv m x)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul (iv m x) ((ring (2 * m + 1 + 1)).mul (iv m x) (iv m x)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by
  have e : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  unfold cE at hc ⊢
  generalize hi : iv m x = i at e ⊢
  calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul i i))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul i ((ring  -- (+2356 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul i i))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul i i))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul i i))))) ((ring (2 * m + 1 + 1)  -- (+252 chars; full line in the Zenodo source)

end G4Trans

#print axioms G4Trans.tau_on

lang/G4b1.lean

lean_all/lang/G4b1.lean

/-
  lang/G4b1 — G4 (b1), the arithmetic half: **an L-series whose Euler factors at p ≠ 11 are 1 − a_p X + p X² with
  a_p ≡ 1 + p (mod 5) is ≡ σ(n) (mod 5) at every n prime to 11**.
  The Euler factor's characteristic polynomial x² − a_p x + p ≡ (x − 1)(x − p) (mod 5), whose power sums are σ(p^k) = 1 + p + … + p^k:
    geo, geo_rec       G_k = 1 + p + … + p^k satisfies G_(k+2) = (1 + p) G_(k+1) − p G_k
    pp_cong            c_0 = 1, c_1 = a_p ≡ 1 + p, c_(k+2) = a_p c_(k+1) − p c_k  ⇒  c_k ≡ G_k (mod 5) for all k
    pull               n ≥ 2 with p | n (p prime) ⇒ n = p^(k+1)·m, p ∤ m, m < n
    factor             n ≥ 2 ⇒ n = p^(k+1)·m with p prime, p ∤ m, m < n
    g4b1               a, s multiplicative (on p^(k+1)·m, p ∤ m), a(1) = s(1) = 1, the Euler recursion and a_p ≡ 1 + p at p ≠ 11,
                       s(p^k) = 1 + … + p^k  ⇒  a(n) ≡ s(n) = σ(n) (mod 5) for every n ≥ 1 with 11 ∤ n
  (The curve input a_p ≡ 1 + p (mod 5) for 11a is G4Count.g4; see G4b1c.) Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.G4Count

namespace G4b1

open FTAChain Frobenius

def geo (p : Int) : Nat → Int
  | 0 => 1
  | k + 1 => geo p k + pw LibInt.intR p (k + 1)

theorem geo_rec (p : Int) (k : Nat) : geo p (k + 2) = (1 + p) * geo p (k + 1) - p * geo p k := by
  show geo p k + pw LibInt.intR p (k + 1) + LibInt.intR.mul (pw LibInt.intR p (k + 1)) p = (1 + p) * (geo p k + pw LibInt.intR p (k + 1)) - p * geo p k
  generalize geo p k = g
  generalize pw LibInt.intR p (k + 1) = q
  show g + q + q * p = (1 + p) * (g + q) - p * g
  lib_int

/-- **c_k ≡ G_k (mod 5)** for the Euler-factor recursion with a_p ≡ 1 + p. -/
theorem pp_cong (A p : Int) (hA : ∃ t, A - (1 + p) = 5 * t) (c : Nat → Int) (h0 : c 0 = 1) (h1 : c 1 = A)
    (hrec : ∀ k, c (k + 2) = A * c (k + 1) - p * c k) : ∀ k, (∃ t, c k - geo p k = 5 * t) ∧ (∃ t, c (k + 1) - geo p (k + 1) = 5 * t)
  | 0 => by
    match hA with
    | ⟨t, ht⟩ =>
      refine ⟨⟨0, by rw [h0]; show (1 : Int) - 1 = 5 * 0; rfl⟩, ⟨t, ?_⟩⟩
      rw [h1, ← ht]; show A - (1 + LibInt.intR.mul LibInt.intR.one p) = A - (1 + p); rw [LibInt.intR.one_mul]
  | k + 1 => by
    match pp_cong A p hA c h0 h1 hrec k, hA with
    | ⟨_, ⟨t1, e1⟩⟩, ⟨t, ht⟩ =>
      match (pp_cong A p hA c h0 h1 hrec k).1 with
      | ⟨t0, e0⟩ =>
        refine ⟨⟨t1, e1⟩, ⟨A * t1 + geo p (k + 1) * t - p * t0, ?_⟩⟩
        rw [hrec, geo_rec]
        have ec1 : c (k + 1) = geo p (k + 1) + 5 * t1 := by rw [← e1]; lib_int
        have ec0 : c k = geo p k + 5 * t0 := by rw [← e0]; lib_int
        have eA : A = 1 + p + 5 * t := by rw [← ht]; lib_int
        rw [ec1, ec0, eA]; lib_int

theorem az : ∀ a b : Nat, a + b = 0 → a = 0
  | _, 0, h => h
  | _, _ + 1, h => Nat.noConfusion h

/-- extraction of the full power of a prime p = l + 2 dividing x + 1. -/
theorem pull (l : Nat) : ∀ fuel x, Le x fuel → Dv (l + 2) (x + 1) →
    ∃ k m, x + 1 = (l + 2) ^ (k + 1) * (m + 1) ∧ (Dv (l + 2) (m + 1) → False) ∧ Lt m x
  | 0, x, hle, ⟨q, hq⟩ => by
    cases q with
    | zero => exact absurd (hq.trans (nzero_mul _)) (fun e => Nat.noConfusion e)
    | succ q' =>
      have hlt : Lt q' x := ⟨q' * l + q' + l, Nat.succ.inj (show q' + (q' * l + q' + l + 1) + 1 = x + 1 by rw [hq]; lib_nat)⟩
      cases dv_dec (l + 1) (q' + 1) with
      | inr nd => exact ⟨0, q', by show x + 1 = 1 * (l + 2) * (q' + 1); rw [hq]; lib_nat, nd, hlt⟩
      | inl _ =>
        match hle, hlt with
        | ⟨c, hc⟩, ⟨c', hc'⟩ =>
          rw [az x c hc] at hc'
          exact Nat.noConfusion hc'
  | f + 1, x, hle, ⟨q, hq⟩ => by
    cases q with
    | zero => exact absurd (hq.trans (nzero_mul _)) (fun e => Nat.noConfusion e)
    | succ q' =>
      have hlt : Lt q' x := ⟨q' * l + q' + l, Nat.succ.inj (show q' + (q' * l + q' + l + 1) + 1 = x + 1 by rw [hq]; lib_nat)⟩
      cases dv_dec (l + 1) (q' + 1) with
      | inr nd => exact ⟨0, q', by show x + 1 = 1 * (l + 2) * (q' + 1); rw [hq]; lib_nat, nd, hlt⟩
      | inl d =>
        match hle, hlt with
        | ⟨c, hc⟩, ⟨c', hc'⟩ =>
          match pull l f q' ⟨c' + c, Nat.succ.inj (show q' + (c' + c) + 1 = f + 1 by rw [← hc, ← hc']; lib_nat)⟩ d with
          | ⟨k, m, hk, hnd, ⟨c'', hc''⟩⟩ =>
            refine ⟨k + 1, m, ?_, hnd, ⟨c'' + c' + 1, by rw [← hc', ← hc'']; lib_nat⟩⟩
            show x + 1 = (l + 2) ^ (k + 1) * (l + 2) * (m + 1)
            rw [hq]
            show (q' + 1) * (l + 2) = (l + 2) ^ (k + 1) * (l + 2) * (m + 1)
            rw [hk]
            generalize (l + 2) ^ (k + 1) = P
            lib_nat

/-- every n = i + 2 is p^(k+1)·(m+1) with p = l + 2 prime, p ∤ m + 1, m < i + 1. -/
theorem factor (i : Nat) : ∃ l k m, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) ∧ i + 2 = (l + 2) ^ (k + 1) * (m + 1) ∧
    (Dv (l + 2) (m + 1) → False) ∧ Lt m (i + 1) := by
  match lp_exists i with
  | ⟨l, hl, hmin⟩ =>
    match pull l (i + 1) (i + 1) ⟨0, rfl⟩ hl with
    | ⟨k, m, hk, hnd, hlt⟩ => exact ⟨l, k, m, lp_prime (i + 1 + 1) l hl hmin, hk, hnd, hlt⟩

/-- **G4 (b1)**: a(n) ≡ σ(n) (mod 5) at every n prime to 11. -/
theorem g4b1 (a s : Nat → Int) (ha1 : a 1 = 1) (hs1 : s 1 = 1)
    (hmA : ∀ l k m, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) → (Dv (l + 2) (m + 1) → False) →
      a ((l + 2) ^ (k + 1) * (m + 1)) = a ((l + 2) ^ (k + 1)) * a (m + 1))
    (hmS : ∀ l k m, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) → (Dv (l + 2) (m + 1) → False) →
      s ((l + 2) ^ (k + 1) * (m + 1)) = s ((l + 2) ^ (k + 1)) * s (m + 1))
    (hrec : ∀ l k, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) → (l + 2 = 11 → False) →
      a ((l + 2) ^ (k + 2)) = a (l + 2) * a ((l + 2) ^ (k + 1)) - Int.ofNat (l + 2) * a ((l + 2) ^ k))
    (hap : ∀ l, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) → (l + 2 = 11 → False) → ∃ t, a (l + 2) - (1 + Int.ofNat (l + 2)) = 5 * t)
    (hsp : ∀ l k, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) → s ((l + 2) ^ k) = geo (Int.ofNat (l + 2)) k) :
    ∀ i, (Dv 11 (i + 1) → False) → ∃ t, a (i + 1) - s (i + 1) = 5 * t := by
  have hpp : ∀ l k, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) → (l + 2 = 11 → False) →
      ∃ t, a ((l + 2) ^ (k + 1)) - s ((l + 2) ^ (k + 1)) = 5 * t := fun l k hp h11 => by
    have h1 : (l + 2) ^ 1 = l + 2 := by show 1 * (l + 2) = l + 2; exact (nmul_comm 1 (l + 2)).trans (nmul_one _)
    have hc := pp_cong (a (l + 2)) (Int.ofNat (l + 2)) (hap l hp h11) (fun k => a ((l + 2) ^ k)) ha1
      (by show a ((l + 2) ^ 1) = a (l + 2); rw [h1]) (fun k => hrec l k hp h11) k
    rw [hsp l (k + 1) hp]
    exact hc.2
  have main : ∀ fuel i, Le i fuel → (Dv 11 (i + 1) → False) → ∃ t, a (i + 1) - s (i + 1) = 5 * t := by
    intro fuel
    induction fuel with
    | zero =>
      intro i hle _
      match hle with
      | ⟨c, hc⟩ =>
        rw [az i c hc, ha1, hs1]; exact ⟨0, rfl⟩
    | succ f ih =>
      intro i hle h11
      cases i with
      | zero => rw [ha1, hs1]; exact ⟨0, rfl⟩
      | succ i' =>
        match factor i' with
        | ⟨l, k, m, hp, hn, hnd, ⟨c', hc'⟩⟩ =>
          match hle with
          | ⟨c, hc⟩ =>
            have hm11 : Dv 11 (m + 1) → False := fun h => h11 (by
              show Dv 11 (i' + 2); rw [hn]; exact dv_mul_left _ 11 _ h)
            have hp11 : l + 2 = 11 → False := fun e => h11 (by
              show Dv 11 (i' + 2)
              exact ⟨(l + 2) ^ k * (m + 1), by
                rw [hn, ← e]; show (l + 2) ^ k * (l + 2) * (m + 1) = (l + 2) ^ k * (m + 1) * (l + 2)
                generalize (l + 2) ^ k = P; lib_nat⟩)
            match ih m ⟨c' + c, Nat.succ.inj (show m + (c' + c) + 1 = f + 1 by rw [← hc, ← hc']; lib_nat)⟩ hm11, hpp l k hp hp11 with
            | ⟨t2, e2⟩, ⟨t1, e1⟩ =>
              refine ⟨t1 * s (m + 1) + s ((l + 2) ^ (k + 1)) * t2 + 5 * t1 * t2, ?_⟩
              show a (i' + 2) - s (i' + 2) = _
              rw [hn, hmA l k m hp hnd, hmS l k m hp hnd]
              have ea1 : a ((l + 2) ^ (k + 1)) = s ((l + 2) ^ (k + 1)) + 5 * t1 := by rw [← e1]; lib_int
              have ea2 : a (m + 1) = s (m + 1) + 5 * t2 := by rw [← e2]; lib_int
              rw [ea1, ea2]; lib_int
  exact fun i h => main i i ⟨0, rfl⟩ h

end G4b1

#print axioms G4b1.geo_rec
#print axioms G4b1.az
#print axioms G4b1.pp_cong
#print axioms G4b1.pull
#print axioms G4b1.factor
#print axioms G4b1.g4b1

lang/G4b1c.lean

lean_all/lang/G4b1c.lean

/-
  lang/G4b1c — G4 (b1) for the curve 11a: y² + y = x³ − x². **The L-series coefficients a(n) of 11a satisfy a(n) ≡ σ(n) (mod 5)
  for every n ≥ 1 prime to 11.**
    cnt2_val      #E_aff(𝔽₂) = 4 (direct count), so a(2) = 2 − 4 = −2 ≡ 1 + 2 (mod 5)
    eleven_ne     11 ≠ 0 in 𝔽_p for every odd prime p ≠ 11 (p = 3, 5, 7, 9 by computation, p > 11 since 11 < p)
    hap_11a       a(p) = p − #E_aff(𝔽_p) ≡ 1 + p (mod 5) at every prime p ≠ 11 (G4Count.g4 for odd p)
    g4b1_11a      with the Euler-product structure of the L-series (a multiplicative on p^(k+1)·m, p ∤ m; a(p^(k+2)) = a(p)a(p^(k+1)) − p a(p^k)
                  for p ≠ 11; a(1) = 1) and σ (s multiplicative, s(p^k) = 1 + … + p^k): 5 | a(n) − σ(n) whenever 11 ∤ n   (G4b1.g4b1)
  The identification of a(n) with the q-expansion coefficients of η(z)²η(11z)² (modularity of 11a at every n) is G4 (b2), left open.
  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.G4b1
import lang.Quint16

namespace G4b1c

open FTAChain ZMod Frobenius YMChain
open LibRing (castZ castN)

/-- #{(x, y) ∈ 𝔽₂² : y² + y = x³ − x²} (in 𝔽₂: y² + y + x³ + x² = 0). -/
def cnt2 : Nat := (([(0, 0), (0, 1), (1, 0), (1, 1)] : List (Nat × Nat)).filter (fun q => (q.2 * q.2 + q.2 + q.1 * q.1 * q.1 + q.1 * q.1) % 2 == 0)).length

theorem cnt2_val : cnt2 = 4 := rfl

theorem mk_val' (q a : Nat) : (ZMod.mk q a).val = md q a := rfl
theorem zero_val' (q : Nat) : (ring q).zero.val = 0 := rfl

theorem eleven_ne (m : Nat) (h : 2 * m + 1 + 1 + 1 = 11 → False) : G4Fix.eleven m ≠ (ring (2 * m + 1 + 1)).zero := fun e => by
  have e0 : castN (ring (2 * m + 1 + 1)) 11 = G4Fix.eleven m := by
    show ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring  -- (+287 chars; full line in the Zenodo source)
  have e1 : castN (ring (2 * m + 1 + 1)) 11 = (ring (2 * m + 1 + 1)).zero := e0.trans e
  rw [IntCast.castN_mk] at e1
  have hv := congrArg Subtype.val e1
  match m with
  | 0 => exact Nat.noConfusion hv
  | 1 => exact Nat.noConfusion hv
  | 2 => exact Nat.noConfusion hv
  | 3 => exact Nat.noConfusion hv
  | 4 => exact h rfl
  | m' + 5 =>
    have : md (2 * (m' + 5) + 1 + 1) 11 = 11 := md_small _ 11 (by
      show 11 < 2 * (m' + 5) + 1 + 1 + 1
      rw [show 2 * (m' + 5) + 1 + 1 + 1 = 11 + (2 * m' + 1) + 1 by lib_nat]
      exact Nat.lt_succ_of_le (Nat.le_add_right 11 _))
    rw [mk_val', this, zero_val'] at hv
    exact Nat.noConfusion hv

/-- **a_p ≡ 1 + p (mod 5) at every prime p ≠ 11** for 11a, with a(p) = p − #E_aff(𝔽_p). -/
theorem hap_11a (a : Nat → Int) (ha2 : a 2 = 2 - Int.ofNat cnt2)
    (hodd : ∀ m, a (2 * m + 1 + 1 + 1) = Int.ofNat (2 * m + 1 + 1 + 1) - sumK LibInt.intR (G4Count.cnt m) (Pairs.pL m)) :
    ∀ l, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) → (l + 2 = 11 → False) → ∃ t, a (l + 2) - (1 + Int.ofNat (l + 2)) = 5 * t
  | 0, _, _ => ⟨-1, by rw [ha2, cnt2_val]; rfl⟩
  | l' + 1, hp, h11 => by
    match Quint16.par l' with
    | ⟨t, Or.inr ht⟩ =>
      exfalso
      cases hp 2 ⟨t + 2, by rw [ht]; lib_nat⟩ with
      | inl e => exact Nat.noConfusion (Nat.succ.inj e)
      | inr e =>
        rw [ht] at e
        exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj e)).symm
    | ⟨t, Or.inl ht⟩ =>
      subst ht
      have hp' : ∀ d, Dv d (2 * t + 1 + 1 + 1) → d = 1 ∨ d = 2 * t + 1 + 1 + 1 := hp
      match G4Count.g4 t hp' (eleven_ne t h11) with
      | ⟨k, hk⟩ =>
        refine ⟨-k, ?_⟩
        show a (2 * t + 1 + 1 + 1) - (1 + Int.ofNat (2 * t + 1 + 1 + 1)) = 5 * -k
        rw [hodd t]
        have e : sumK LibInt.intR (G4Count.cnt t) (Pairs.pL t) = 5 * k - 1 := by rw [← hk]; lib_int
        rw [e]; lib_int

/-- **G4 (b1)**: for 11a, a(n) ≡ σ(n) (mod 5) at every n ≥ 1 with 11 ∤ n. -/
theorem g4b1_11a (a s : Nat → Int) (ha1 : a 1 = 1) (hs1 : s 1 = 1)
    (hmA : ∀ l k m, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) → (Dv (l + 2) (m + 1) → False) →
      a ((l + 2) ^ (k + 1) * (m + 1)) = a ((l + 2) ^ (k + 1)) * a (m + 1))
    (hmS : ∀ l k m, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) → (Dv (l + 2) (m + 1) → False) →
      s ((l + 2) ^ (k + 1) * (m + 1)) = s ((l + 2) ^ (k + 1)) * s (m + 1))
    (hrec : ∀ l k, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) → (l + 2 = 11 → False) →
      a ((l + 2) ^ (k + 2)) = a (l + 2) * a ((l + 2) ^ (k + 1)) - Int.ofNat (l + 2) * a ((l + 2) ^ k))
    (ha2 : a 2 = 2 - Int.ofNat cnt2)
    (hodd : ∀ m, a (2 * m + 1 + 1 + 1) = Int.ofNat (2 * m + 1 + 1 + 1) - sumK LibInt.intR (G4Count.cnt m) (Pairs.pL m))
    (hsp : ∀ l k, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) → s ((l + 2) ^ k) = G4b1.geo (Int.ofNat (l + 2)) k) :
    ∀ i, (Dv 11 (i + 1) → False) → ∃ t, a (i + 1) - s (i + 1) = 5 * t :=
  G4b1.g4b1 a s ha1 hs1 hmA hmS hrec (hap_11a a ha2 hodd) hsp

end G4b1c

#print axioms G4b1c.cnt2_val
#print axioms G4b1c.mk_val'
#print axioms G4b1c.zero_val'
#print axioms G4b1c.eleven_ne
#print axioms G4b1c.hap_11a
#print axioms G4b1c.g4b1_11a

lang/G5Tate3.lean

lean_all/lang/G5Tate3.lean

/-
  lang/G5Tate3 — T43, cycle 366: 3 | #E(𝔽_p) for every curve y² + axy + by = x³ with b ≠ 0, a³ ≠ 27b (G5, order 3).

  The point T = (0, 0) has order 3 (Tate normal form); translation by T is
      τ(x, y) = (−by/x², −b²y/x³)  (x ≠ 0),   (0, 0) ↦ (0, −b) ↦ O ↦ (0, 0),
  with τ²(x, y) = (bx/y, −bx³/y²) and τ³ = id; a fixed point forces b²(a³ − 27b) = 0.
    one_min3, m3_rot, orbit3g   orbit counting for an order-3 map on any listed type with an injective code
    on3, step2, step3, tau3     the translation and τ³ = id on {O} ∪ 𝔽_p²
    fixed_alg3, onT_fix, onT_tau
    g5                          1 + #{(x, y) : y² + axy + by = x³} = 3k
  Certificates from the std-lib Python reduction. Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G4Count

namespace G5Tate3

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Step G4Gen G4Map Pairs Orbit5
open FiniteRecChain (ite_pos' ite_neg')

variable (m : Nat)

def m3 (a b c : Nat) : Nat := mn a (mn b c)

theorem m3_mem (a b c : Nat) : m3 a b c = a ∨ m3 a b c = b ∨ m3 a b c = c := by
  unfold m3
  rcases mn_or a (mn b c) with h | h
  · exact Or.inl h
  rw [h]
  rcases mn_or b c with h | h
  · exact Or.inr (Or.inl h)
  · exact Or.inr (Or.inr h)

theorem m3_le (a b c : Nat) : m3 a b c ≤ a ∧ m3 a b c ≤ b ∧ m3 a b c ≤ c :=
  ⟨mn_le_l _ _, Nat.le_trans (mn_le_r _ _) (mn_le_l _ _), Nat.le_trans (mn_le_r _ _) (mn_le_r _ _)⟩

theorem m3_rot (a b c : Nat) : m3 b c a = m3 a b c := by
  have l1 := m3_le a b c
  have l2 := m3_le b c a
  apply Nat.le_antisymm
  · rcases m3_mem a b c with h | h | h <;> rw [h]
    · exact l2.2.2
    · exact l2.1
    · exact l2.2.1
  · rcases m3_mem b c a with h | h | h <;> rw [h]
    · exact l1.2.1
    · exact l1.2.2
    · exact l1.1

theorem one_min3 (a b c : Nat) (hab : a ≠ b) (hac : a ≠ c) (hbc : b ≠ c) :
    eqI a (m3 a b c) + eqI b (m3 a b c) + eqI c (m3 a b c) = 1 := by
  unfold eqI
  rcases m3_mem a b c with h | h | h <;> rw [h]
  · rw [ite_pos' _ _ rfl, ite_neg' _ _ (fun e' => hab e'.symm), ite_neg' _ _ (fun e' => hac e'.symm)]; rfl
  · rw [ite_neg' _ _ hab, ite_pos' _ _ rfl, ite_neg' _ _ (fun e' => hbc e'.symm)]; rfl
  · rw [ite_neg' _ _ hac, ite_neg' _ _ hbc, ite_pos' _ _ rfl]; rfl

section General
variable {G : Type} [DecidableEq G]

/-- **Orbits of an order-3 map** on any completely listed type with an injective code. -/
theorem orbit3g (els : List G) (hc : Complete LibInt.intR els) (code : G → Nat) (hinj : ∀ u v, code u = code v → u = v)
    (sg : G → G) (h3 : ∀ x, sg (sg (sg (x))) = x) (H : G → Int) (hH : ∀ x, H (sg x) = H x) (hfix : ∀ x, sg x = x → H x = 0) :
    sumK LibInt.intR H els = 3 * sumK LibInt.intR (fun x => H x * eqI (code x) (m3 (code x) (code (sg (x))) (code (sg (sg (x)))))) els := by
  let Ls : G → Int := fun x => eqI (code x) (m3 (code x) (code (sg (x))) (code (sg (sg (x)))))
  have hpt : ∀ x, H x = H x * Ls x + H x * Ls (sg (x)) + H x * Ls (sg (sg (x))) := fun x => by
    by_cases hx : sg x = x
    · rw [hfix x hx]; lib_int
    · have ne : ∀ u v : G, u ≠ v → code u ≠ code v := fun u v h e => h (hinj u v e)
      have d01 : sg (x) ≠ x := hx
      have d02 : sg (sg (x)) ≠ x := fun e => hx (by
        calc sg (x) = sg (sg (sg (sg (x)))) := by rw [h3]
          _ = x := by rw [e, e])
      have d12 : sg (sg (x)) ≠ sg (x) := fun e => hx (by
        calc sg (x) = sg (sg (x)) := e.symm
          _ = sg (sg (sg (x))) := congrArg sg e.symm
          _ = x := h3 x)
      have hm := one_min3 (code x) (code (sg (x))) (code (sg (sg (x))))
        (ne _ _ (fun e => d01 e.symm)) (ne _ _ (fun e => d02 e.symm)) (ne _ _ (fun e => d12 e.symm))
      have r1 : Ls (sg (x)) = eqI (code (sg (x))) (m3 (code x) (code (sg (x))) (code (sg (sg (x))))) := by
        show eqI _ (m3 _ _ (code (sg (sg (sg (x)))))) = _; rw [h3, m3_rot]
      have r2 : Ls (sg (sg (x))) = eqI (code (sg (sg (x)))) (m3 (code x) (code (sg (x))) (code (sg (sg (x))))) := by
        show eqI _ (m3 _ (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x))))))) = _; rw [h3, m3_rot, m3_rot]
      rw [r1, r2]
      show H x = H x * eqI (code x) _ + _ + _
      generalize m3 (code x) (code (sg (x))) (code (sg (sg (x)))) = μ at hm ⊢
      generalize eqI (code x) μ = u0 at hm ⊢; generalize eqI (code (sg (x))) μ = u1 at hm ⊢
      generalize eqI (code (sg (sg (x)))) μ = u2 at hm ⊢
      calc H x = H x * (u0 + u1 + u2) := by rw [hm]; lib_int
        _ = _ := by lib_int
  have q1 : sumK LibInt.intR (fun x => H x * Ls (sg (x))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (x)) (fun x => sg (sg (x))) (fun x => h3 x) (fun y => by show sg (sg (sg y)) = y; exact h3 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (x)) = H (sg (x)) * Ls (sg (x)); rw [hH]) _
  have q2 : sumK LibInt.intR (fun x => H x * Ls (sg (sg (x)))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (sg (x))) (fun x => sg (x)) (fun x => h3 x) (fun y => by show sg (sg (sg y)) = y; exact h3 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (sg (x))) = H (sg (sg (x))) * Ls (sg (sg (x))); rw [hH, hH]) _
  rw [sumK_ext LibInt.intR _ _ hpt, QuadDiagonal.sI_add, QuadDiagonal.sI_add, q1, q2]
  generalize sumK LibInt.intR (fun x => H x * Ls x) els = T
  lib_int

end General

def cT (a b x y : Zp (2 * m + 1 + 1)) : Prop := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x y))) ((ring (2 * m + 1 + 1)).mul b y) = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)

def tx3 (_a b x y : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul (iv m x) (iv m x))))
def ty3 (_a b x y : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (b) (b)) ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul (iv m x) ((ring (2 * m + 1 + 1)).mul (iv m x) (iv m x)))))

/-- **P + T stays on the curve.** -/
theorem on3 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b x y : Zp (2 * m + 1 + 1)) (hc : cT m a b x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) : cT m a b (tx3 m a b x y) (ty3 m a b x y) := by
  unfold tx3 ty3
  show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (b) (b)) ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul (iv m x) ((ring (2 * m + 1 + 1)).mul (iv m x) (iv m x)))))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (b) (b)) ((ri  -- (+784 chars; full line in the Zenodo source)
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  unfold cT at hc
  generalize iv m x = i at ei ⊢
  have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m + 1 + 1)).mul (b) (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x)))) = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
  calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (b) (b)) ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul (i) ((ring (2 * m + 1 + 1)).mul (i) (i)))))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (b) (b)) ((ring (2 * m + 1 +  -- (+3218 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul (i) (i))))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul (i) (i))))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m  -- (+89 chars; full line in the Zenodo source)

theorem mul1 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b x y : Zp (2 * m + 1 + 1)) (hc : cT m a b x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hy : y ≠ (ring (2 * m + 1 + 1)).zero) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) : (ring (2 * m + 1 + 1)).mul (tx3 m a b x y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 *  -- (+72 chars; full line in the Zenodo source)
  unfold tx3
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  have ej : (ring (2 * m + 1 + 1)).mul (iv m y) y = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr y hy
  have ec : (ring (2 * m + 1 + 1)).mul (iv m b) b = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr b hb
  unfold cT at hc
  generalize iv m x = i at ei ⊢
  generalize iv m y = j at ej ⊢
  generalize iv m b = c at ec ⊢
  have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have gjy : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (j) (y)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring
  have gcb : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (c) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ec]; lib_ring
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m + 1 + 1)).mul (b) (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x)))) = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
  calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul (i) (i))))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).mul (j) (c)))) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1  -- (+836 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).one := by rw [gix, gjy, gcb]; lib_ring

/-- **τ²P = (bx/y, −bx³/y²).** -/
theorem step2 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b x y : Zp (2 * m + 1 + 1)) (hc : cT m a b x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hy : y ≠ (ring (2 * m + 1 + 1)).zero) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) : tx3 m a b (tx3 m a b x y) (ty3 m a b x y) = (ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (x) (iv m y)) ∧ ty3 m a b (tx3 m a b x y) (  -- (+228 chars; full line in the Zenodo source)
  have hX := iv_eq m hpr _ _ (mul1 m hpr a b x y hc hx hy hb)
  rw [show tx3 m a b (tx3 m a b x y) (ty3 m a b x y) = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul (ty3 m a b x y) ((ring (2 * m + 1 + 1)).mul (iv m (tx3 m a b x y)) (iv m (tx3 m a b x y))))) from rfl,
    show ty3 m a b (tx3 m a b x y) (ty3 m a b x y) = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b b) ((ring (2 * m + 1 + 1)).mul (ty3 m a b x y) ((ring (2 * m + 1 + 1)).mul (iv m (tx3 m a b x y)) ((ring (2 * m + 1 + 1)).mul (iv m (tx3 m a b x y)) (iv m (tx3 m a b x y)))))) from rfl, hX]
  unfold ty3
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  have ej : (ring (2 * m + 1 + 1)).mul (iv m y) y = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr y hy
  have ec : (ring (2 * m + 1 + 1)).mul (iv m b) b = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr b hb
  unfold cT at hc
  generalize iv m x = i at ei ⊢
  generalize iv m y = j at ej ⊢
  generalize iv m b = c at ec ⊢
  have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have gjy : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (j) (y)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring
  have gcb : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (c) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ec]; lib_ring
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m + 1 + 1)).mul (b) (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x)))) = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
  constructor
  · calc (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (b) (b)) ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul (i) ((ring (2 * m + 1 + 1)).mul (i) (i)))))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)  -- (+2232 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (x) (j)) := by rw [gix, gjy, gcb]; lib_ring
  · calc (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (b) (b)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (b) (b)) ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul (i) ((ring (2 * m + 1 + 1)).mul (i) (i)))))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1  -- (+3875 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x))) ((ring (2 * m + 1 + 1)).mul (j) (j)))) := by rw [gix, gjy, gcb]; lib_ring

theorem mul2 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b x y : Zp (2 * m + 1 + 1)) (hc : cT m a b x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hy : y ≠ (ring (2 * m + 1 + 1)).zero) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (x) (iv m y))) ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m  -- (+69 chars; full line in the Zenodo source)
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  have ej : (ring (2 * m + 1 + 1)).mul (iv m y) y = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr y hy
  have ec : (ring (2 * m + 1 + 1)).mul (iv m b) b = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr b hb
  unfold cT at hc
  generalize iv m x = i at ei ⊢
  generalize iv m y = j at ej ⊢
  generalize iv m b = c at ec ⊢
  have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have gjy : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (j) (y)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring
  have gcb : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (c) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ec]; lib_ring
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m + 1 + 1)).mul (b) (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x)))) = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
  calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (x) (j))) ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul (i) (c))) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).  -- (+524 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).one := by rw [gix, gjy, gcb]; lib_ring

/-- **τ³P = P.** -/
theorem step3 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b x y : Zp (2 * m + 1 + 1)) (hc : cT m a b x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hy : y ≠ (ring (2 * m + 1 + 1)).zero) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) : tx3 m a b ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (x) (iv m y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (  -- (+455 chars; full line in the Zenodo source)
  have hX := iv_eq m hpr _ _ (mul2 m hpr a b x y hc hx hy hb)
  unfold tx3 ty3
  rw [hX]
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  have ej : (ring (2 * m + 1 + 1)).mul (iv m y) y = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr y hy
  have ec : (ring (2 * m + 1 + 1)).mul (iv m b) b = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr b hb
  unfold cT at hc
  generalize iv m x = i at ei ⊢
  generalize iv m y = j at ej ⊢
  generalize iv m b = c at ec ⊢
  have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have gjy : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (j) (y)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring
  have gcb : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (c) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ec]; lib_ring
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m + 1 + 1)).mul (b) (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x)))) = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
  constructor
  · calc (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x))) ((ring (2 * m + 1 + 1)).mul (j) (j))))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul (i) (c))) ((ring (  -- (+1670 chars; full line in the Zenodo source)
      _ = x := by rw [gix, gjy, gcb]; lib_ring
  · calc (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (b) (b)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x))) ((ring (2 * m + 1 + 1)).mul (j) (j))))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m  -- (+2674 chars; full line in the Zenodo source)
      _ = y := by rw [gix, gjy, gcb]; lib_ring

def tau (a b : Zp (2 * m + 1 + 1)) : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) → Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))
  | none => some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).zero)
  | some p => if (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul p.2 p.2) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul p.1 p.2))) ((ring (2 * m + 1 + 1)).mul b p.2) = (ring (2 * m + 1 + 1)).mul p.1 ((ring (2 * m + 1 + 1)).mul p.1 p.1) then
      (if p.1 = (ring (2 * m + 1 + 1)).zero then (if p.2 = (ring (2 * m + 1 + 1)).zero then some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).neg b) else none) else some (tx3 m a b p.1 p.2, ty3 m a b p.1 p.2))
    else some p

theorem tau_some (a b x y : Zp (2 * m + 1 + 1)) : tau m a b (some (x, y)) =
    if (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x y))) ((ring (2 * m + 1 + 1)).mul b y) = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x) then
      (if x = (ring (2 * m + 1 + 1)).zero then (if y = (ring (2 * m + 1 + 1)).zero then some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).neg b) else none) else some (tx3 m a b x y, ty3 m a b x y))
    else some (x, y) := rfl

theorem tau_gen (a b x y : Zp (2 * m + 1 + 1)) (hc : cT m a b x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) : tau m a b (some (x, y)) = some (tx3 m a b x y, ty3 m a b x y) := by
  unfold cT at hc
  rw [tau_some, ite_pos' _ _ hc, ite_neg' _ _ hx]

theorem tau_off (a b x y : Zp (2 * m + 1 + 1)) (hc : ¬ cT m a b x y) : tau m a b (some (x, y)) = some (x, y) := by
  unfold cT at hc
  rw [tau_some, ite_neg' _ _ hc]

theorem tau_T (a b : Zp (2 * m + 1 + 1)) : tau m a b (some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).zero)) = some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).neg b) := by
  rw [tau_some, ite_pos' _ _ (show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero))) ((ring (2 * m + 1 + 1)).mul b (ring (2 * m + 1 + 1)).zero) = (ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1  -- (+106 chars; full line in the Zenodo source)
    ite_pos' _ _ rfl, ite_pos' _ _ rfl]

theorem tau_2T (a b : Zp (2 * m + 1 + 1)) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) : tau m a b (some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).neg b)) = none := by
  rw [tau_some, ite_pos' _ _ (show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg b) ((ring (2 * m + 1 + 1)).neg b)) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).neg b)))) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).neg b)) = (ring (2 * m + 1 + 1)).mul (ring (2  -- (+118 chars; full line in the Zenodo source)
    ite_pos' _ _ rfl, ite_neg' _ _ (neg_ne m _ hb)]

theorem gen_y (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b x y : Zp (2 * m + 1 + 1)) (hc : cT m a b x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) : y ≠ (ring (2 * m + 1 + 1)).zero := fun e => hx (by
  unfold cT at hc; rw [e] at hc
  rcases mul_eq_zero m hpr _ _ (show (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x) = (ring (2 * m + 1 + 1)).zero by rw [← hc]; lib_ring) with h | h
  · exact h
  · rcases mul_eq_zero m hpr _ _ h with h' | h'
    · exact h'
    · exact h')

theorem tau_gen3 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b x y : Zp (2 * m + 1 + 1)) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) (hc : cT m a b x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) :
    tau m a b (tau m a b (tau m a b (some (x, y)))) = some (x, y) := by
  have hy := gen_y m hpr a b x y hc hx
  have ix := iv_nz m hpr x hx
  have iy := iv_nz m hpr y hy
  have c1 := on3 m hpr a b x y hc hx
  have n1 : tx3 m a b x y ≠ (ring (2 * m + 1 + 1)).zero := neg_ne m _ (mul_ne m hpr _ _ hb (mul_ne m hpr _ _ hy (mul_ne m hpr _ _ ix ix)))
  have s2 := step2 m hpr a b x y hc hx hy hb
  have c2 := on3 m hpr a b _ _ c1 n1
  rw [s2.1, s2.2] at c2
  have n2 := mul_ne m hpr _ _ hb (mul_ne m hpr _ _ hx iy)
  have s3 := step3 m hpr a b x y hc hx hy hb
  rw [tau_gen m a b x y hc hx, tau_gen m a b _ _ c1 n1, s2.1, s2.2, tau_gen m a b _ _ c2 n2, s3.1, s3.2]

theorem y_tors3 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (b y : Zp (2 * m + 1 + 1)) (h : (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).add y b) = (ring (2 * m + 1 + 1)).zero) :
    y = (ring (2 * m + 1 + 1)).zero ∨ y = (ring (2 * m + 1 + 1)).neg b := by
  rcases mul_eq_zero m hpr _ _ h with e | e
  · exact Or.inl e
  · exact Or.inr (by calc y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add y b) ((ring (2 * m + 1 + 1)).neg b) := by lib_ring
      _ = _ := by rw [e]; lib_ring)

/-- **τ³ = id** on {O} ∪ 𝔽_p². -/
theorem tau3 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b : Zp (2 * m + 1 + 1)) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) :
    tau m a b (tau m a b (tau m a b P)) = P := by
  cases P with
  | none => show tau m a b (tau m a b (some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).zero))) = none; rw [tau_T, tau_2T m a b hb]
  | some p =>
    obtain ⟨x, y⟩ := p
    by_cases hc : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x y))) ((ring (2 * m + 1 + 1)).mul b y) = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)
    · by_cases h0 : x = (ring (2 * m + 1 + 1)).zero
      · have hy : (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).add y b) = (ring (2 * m + 1 + 1)).zero := by
          rw [h0] at hc
          calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero y))) ((ring (2 * m + 1 + 1)).mul b y) := by lib_ring
            _ = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
        rw [h0]
        rcases y_tors3 m hpr b y hy with e | e <;> rw [e]
        · rw [tau_T, tau_2T m a b hb]; rfl
        · rw [tau_2T m a b hb]; show tau m a b (some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).zero)) = _; rw [tau_T]
      · exact tau_gen3 m hpr a b x y hb hc h0
    · rw [tau_off m a b x y hc, tau_off m a b x y hc, tau_off m a b x y hc]

def d27 (a b : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (  -- (+1294 chars; full line in the Zenodo source)

/-- a fixed point with x ≠ 0 forces b²(a³ − 27b) = 0. -/
theorem fixed_alg3 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b x y : Zp (2 * m + 1 + 1)) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) (hc : cT m a b x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero)
    (fx : tx3 m a b x y = x) (fy : ty3 m a b x y = y) : d27 m a b = (ring (2 * m + 1 + 1)).zero := by
  have hy := gen_y m hpr a b x y hc hx
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  unfold tx3 at fx; unfold ty3 at fy; unfold cT at hc
  generalize iv m x = i at ei fx fy
  have gi : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i x) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have gX : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul i i)))) ((ring (2 * m + 1 + 1)).neg x) = (ring (2 * m + 1 + 1)).zero := by rw [fx]; lib_ring
  have gY : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b b) ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul i i))))) ((ring (2 * m + 1 + 1)).neg y) = (ring (2 * m + 1 + 1)).zero := by rw [fy]; lib_ring
  have g1 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b y) = (ring (2 * m + 1 + 1)).zero := by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul i i)))) ((ring (2 * m + 1 + 1)).neg x))))
          ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i x) (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i x) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))))) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).zero := by rw [gX, gi]; lib_ring
  have g2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b y) ((ring (2 * m + 1 + 1)).add b ((ring (2 * m + 1 + 1)).neg y)) = (ring (2 * m + 1 + 1)).zero := by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b b) ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul i i))))  -- (+35 chars; full line in the Zenodo source)
          ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b b) y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul i x) ((ring (2 * m + 1 + 1)).mul i x)) ((ring (2 * m + 1 + 1)).mul i x)) (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (  -- (+92 chars; full line in the Zenodo source)
          ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b y)))) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).zero := by rw [gY, gi, g1]; lib_ring
  have hyb : y = b := by
    rcases mul_eq_zero m hpr _ _ g2 with h | h
    · exact absurd h (mul_ne m hpr _ _ hb hy)
    · calc y = (ring (2 * m + 1 + 1)).add b ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add b ((ring (2 * m + 1 + 1)).neg y))) := by lib_ring
        _ = b := by rw [h]; lib_ring
  rw [hyb] at g1 hc
  have g3 : (ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).one))) b)) = (ring (2 * m + 1 + 1)).zero := by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b b) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x b))) ((ring (2 * m + 1 + 1)).mul b b)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1))  -- (+90 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [hc, g1]; lib_ring
  have g4 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).one))) b) = (ring (2 * m + 1 + 1)).zero := by
    rcases mul_eq_zero m hpr _ _ g3 with h | h
    · exact absurd h hb
    · exact h
  have g5 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b b) (d27 m a b) = (ring (2 * m + 1 + 1)).zero := by
    unfold d27
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b b)))
          ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).one))) b))
            ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a x) ((ring (2 * m + 1 + 1)).mul a x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2   -- (+417 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [g1, g4]; lib_ring
  rcases mul_eq_zero m hpr _ _ g5 with h | h
  · exact absurd h (mul_ne m hpr _ _ hb hb)
  · exact h

def onT (a b : Zp (2 * m + 1 + 1)) : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) → Int
  | none => 1
  | some p => if (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul p.2 p.2) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul p.1 p.2))) ((ring (2 * m + 1 + 1)).mul b p.2) = (ring (2 * m + 1 + 1)).mul p.1 ((ring (2 * m + 1 + 1)).mul p.1 p.1) then 1 else 0

theorem onT_on (a b x y : Zp (2 * m + 1 + 1)) (hc : cT m a b x y) : onT m a b (some (x, y)) = 1 := by
  unfold cT at hc; show (if _ then _ else _) = _; rw [ite_pos' _ _ hc]

theorem onT_off (a b x y : Zp (2 * m + 1 + 1)) (hc : ¬ cT m a b x y) : onT m a b (some (x, y)) = 0 := by
  unfold cT at hc; show (if _ then _ else _) = _; rw [ite_neg' _ _ hc]

theorem onT_tau (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b : Zp (2 * m + 1 + 1)) (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) : onT m a b (tau m a b P) = onT m a b P := by
  cases P with
  | none => exact onT_on m a b _ _ (by unfold cT; lib_ring)
  | some p =>
    obtain ⟨x, y⟩ := p
    by_cases hc : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x y))) ((ring (2 * m + 1 + 1)).mul b y) = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)
    · rw [onT_on m a b x y hc]
      by_cases h0 : x = (ring (2 * m + 1 + 1)).zero
      · rw [tau_some, ite_pos' _ _ hc, ite_pos' _ _ h0]
        by_cases hy : y = (ring (2 * m + 1 + 1)).zero
        · rw [ite_pos' _ _ hy]; exact onT_on m a b _ _ (by unfold cT; lib_ring)
        · rw [ite_neg' _ _ hy]; rfl
      · rw [tau_gen m a b x y hc h0]; exact onT_on m a b _ _ (on3 m hpr a b x y hc h0)
    · rw [tau_off m a b x y hc]

theorem onT_fix (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b : Zp (2 * m + 1 + 1)) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) (hd : d27 m a b ≠ (ring (2 * m + 1 + 1)).zero)
    (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) (h : tau m a b P = P) : onT m a b P = 0 := by
  cases P with
  | none => exact absurd h (fun e => by cases e)
  | some p =>
    obtain ⟨x, y⟩ := p
    by_cases hc : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x y))) ((ring (2 * m + 1 + 1)).mul b y) = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)
    · exfalso
      by_cases h0 : x = (ring (2 * m + 1 + 1)).zero
      · rw [tau_some, ite_pos' _ _ hc, ite_pos' _ _ h0] at h
        by_cases hy : y = (ring (2 * m + 1 + 1)).zero
        · rw [ite_pos' _ _ hy] at h
          exact neg_ne m b hb ((congrArg Prod.snd (Option.some.inj h)).trans hy)
        · rw [ite_neg' _ _ hy] at h
          cases h
      · rw [tau_gen m a b x y hc h0] at h
        have e' := Option.some.inj h
        exact hd (fixed_alg3 m hpr a b x y hb hc h0 (congrArg Prod.fst e') (congrArg Prod.snd e'))
    · exact onT_off m a b x y hc

def cnt3 (a b : Zp (2 * m + 1 + 1)) (q : Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) : Int :=
  if (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul q.2 q.2) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul q.1 q.2))) ((ring (2 * m + 1 + 1)).mul b q.2) = (ring (2 * m + 1 + 1)).mul q.1 ((ring (2 * m + 1 + 1)).mul q.1 q.1) then 1 else 0

/-- **G5 (order 3): 3 | 1 + #E_aff(𝔽_p)** for y² + axy + by = x³, b ≠ 0, a³ ≠ 27b. -/
theorem g5 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b : Zp (2 * m + 1 + 1)) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) (hd : d27 m a b ≠ (ring (2 * m + 1 + 1)).zero) :
    ∃ k : Int, 1 + sumK LibInt.intR (cnt3 m a b) (pL m) = 3 * k := by
  have e : sumK LibInt.intR (onT m a b) (ptsL m) = 1 + sumK LibInt.intR (cnt3 m a b) (pL m) := by
    show LibInt.intR.add 1 (sumK LibInt.intR (onT m a b) ((pL m).map some)) = _
    rw [sum_map]; rfl
  rw [← e]
  exact ⟨_, orbit3g (ptsL m) (complete_pts m) (code m) (code_inj m) (tau m a b) (tau3 m hpr a b hb) (onT m a b) (onT_tau m hpr a b) (onT_fix m hpr a b hb hd)⟩

end G5Tate3

#print axioms G5Tate3.m3_mem
#print axioms G5Tate3.m3_le
#print axioms G5Tate3.m3_rot
#print axioms G5Tate3.one_min3
#print axioms G5Tate3.orbit3g
#print axioms G5Tate3.on3
#print axioms G5Tate3.mul1
#print axioms G5Tate3.step2
#print axioms G5Tate3.mul2
#print axioms G5Tate3.step3
#print axioms G5Tate3.tau_some
#print axioms G5Tate3.tau_gen
#print axioms G5Tate3.tau_off
#print axioms G5Tate3.tau_T
#print axioms G5Tate3.tau_2T
#print axioms G5Tate3.gen_y
#print axioms G5Tate3.tau_gen3
#print axioms G5Tate3.y_tors3
#print axioms G5Tate3.tau3
#print axioms G5Tate3.fixed_alg3
#print axioms G5Tate3.onT_on
#print axioms G5Tate3.onT_off
#print axioms G5Tate3.onT_tau
#print axioms G5Tate3.onT_fix
#print axioms G5Tate3.g5

lang/G5Two.lean

lean_all/lang/G5Two.lean

/-
  lang/G5Two — T43, cycle 375: 2 | #E(𝔽_p) for every curve y² = x³ + ax² + bx with b ≠ 0, a² ≠ 4b (G5, order 2).

  Translation by the 2-torsion point T = (0, 0): (x, y) ↦ (b/x, −by/x²) (x ≠ 0), (0, 0) ↦ O ↦ (0, 0); identity off the curve.
    one_min2, orbit2g   orbit counting for an involution on any listed type with an injective code
    on2, inv2, tau2     the translation stays on E and is an involution on {O} ∪ 𝔽_p²
    fix2                a fixed point (x ≠ 0) forces y = 0, b = x², 2x + a = 0, hence a² = 4b
    g2                  1 + #{(x, y) : y² = x³ + ax² + bx} = 2k        (p odd, b ≠ 0, a² ≠ 4b)
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.G7Count

namespace G5Two

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Step G4Gen G4Map Pairs Orbit5
open FiniteRecChain (ite_pos' ite_neg')

variable (m : Nat)

theorem one_min2 (u v : Nat) (h : u ≠ v) : eqI u (mn u v) + eqI v (mn u v) = 1 := by
  unfold eqI
  rcases mn_or u v with e | e <;> rw [e]
  · rw [ite_pos' _ _ rfl, ite_neg' _ _ (fun e' => h e'.symm)]; rfl
  · rw [ite_neg' _ _ h, ite_pos' _ _ rfl]; rfl

theorem mn_comm (u v : Nat) : mn v u = mn u v := by
  apply Nat.le_antisymm
  · rcases mn_or u v with e | e <;> rw [e]
    · exact mn_le_r _ _
    · exact mn_le_l _ _
  · rcases mn_or v u with e | e <;> rw [e]
    · exact mn_le_r _ _
    · exact mn_le_l _ _

section General
variable {G : Type} [DecidableEq G]

/-- **Orbits of an involution** on any completely listed type with an injective code. -/
theorem orbit2g (els : List G) (hc : Complete LibInt.intR els) (code : G → Nat) (hinj : ∀ u v, code u = code v → u = v)
    (sg : G → G) (h2 : ∀ x, sg (sg x) = x) (H : G → Int) (hH : ∀ x, H (sg x) = H x) (hfix : ∀ x, sg x = x → H x = 0) :
    sumK LibInt.intR H els = 2 * sumK LibInt.intR (fun x => H x * eqI (code x) (mn (code x) (code (sg x)))) els := by
  let Ls : G → Int := fun x => eqI (code x) (mn (code x) (code (sg x)))
  have hpt : ∀ x, H x = H x * Ls x + H x * Ls (sg x) := fun x => by
    by_cases hx : sg x = x
    · rw [hfix x hx]; lib_int
    · have hm := one_min2 (code x) (code (sg x)) (fun e => hx (hinj _ _ e).symm)
      have r1 : Ls (sg x) = eqI (code (sg x)) (mn (code x) (code (sg x))) := by
        show eqI _ (mn _ (code (sg (sg x)))) = _; rw [h2, mn_comm]
      rw [r1]
      show H x = H x * eqI (code x) _ + _
      generalize mn (code x) (code (sg x)) = μ at hm ⊢
      generalize eqI (code x) μ = u0 at hm ⊢; generalize eqI (code (sg x)) μ = u1 at hm ⊢
      calc H x = H x * (u0 + u1) := by rw [hm]; lib_int
        _ = _ := by lib_int
  have q1 : sumK LibInt.intR (fun x => H x * Ls (sg x)) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc sg sg (fun x => h2 x) (fun y => h2 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg x) = H (sg x) * Ls (sg x); rw [hH]) _
  rw [sumK_ext LibInt.intR _ _ hpt, QuadDiagonal.sI_add, q1]
  generalize sumK LibInt.intR (fun x => H x * Ls x) els = T
  lib_int

end General

def c2 (a b x y : Zp (2 * m + 1 + 1)) : Prop := (ring (2 * m + 1 + 1)).mul y y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x))) ((ring (2 * m + 1 + 1)).mul b x)

def t2x (b x : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).mul b (iv m x)
def t2y (b x y : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul (iv m x) (iv m x))))

theorem on2 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b x y : Zp (2 * m + 1 + 1)) (hc : c2 m a b x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) :
    c2 m a b (t2x m b x) (t2y m b x y) := by
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  unfold c2 at hc ⊢; unfold t2x t2y
  generalize iv m x = i at ei
  calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul i i)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul i i))))
      = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b b) ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul i i)))) ((ring (2 * m + 1 + 1)).mul y y) := by lib_ring
    _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b b) ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul i i)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x))) ((ring (2 * m + 1 + 1))  -- (+24 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b b) i) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul i x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul i x) ((ring (2 * m + 1 + 1)).mul i x))))
          ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul b b)) ((ring (2 * m + 1 + 1)).mul i i)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul i x) ((ring (2 * m + 1 + 1)).mul i x))))
          ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul i i))))) ((ring (2 * m + 1 + 1)).mul i x)) := by lib_ring
    _ = _ := by rw [ei]; lib_ring

theorem inv2 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (b x y : Zp (2 * m + 1 + 1)) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) :
    t2x m b (t2x m b x) = x ∧ t2y m b (t2x m b x) (t2y m b x y) = y := by
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  have el : (ring (2 * m + 1 + 1)).mul (iv m b) b = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr b hb
  have hX : iv m (t2x m b x) = (ring (2 * m + 1 + 1)).mul x (iv m b) := iv_eq m hpr _ _ (by
    unfold t2x
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b (iv m x)) ((ring (2 * m + 1 + 1)).mul x (iv m b)) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m x) x) ((ring (2 * m + 1 + 1)).mul (iv m b) b) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [ei, el]; lib_ring)
  rw [show t2x m b (t2x m b x) = (ring (2 * m + 1 + 1)).mul b (iv m (t2x m b x)) from rfl,
    show t2y m b (t2x m b x) (t2y m b x y) = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul (t2y m b x y) ((ring (2 * m + 1 + 1)).mul (iv m (t2x m b x)) (iv m (t2x m b x))))) from rfl, hX]
  unfold t2y
  generalize iv m x = i at ei
  generalize iv m b = l at el
  constructor
  · calc (ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul x l) = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul l b) := by lib_ring
      _ = x := by rw [el]; lib_ring
  · calc (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul i i)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x l) ((ring (2 * m + 1 + 1)).mul x l))))
        = (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul l b) ((ring (2 * m + 1 + 1)).mul l b)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul i x) ((ring (2 * m + 1 + 1)).mul i x))) := by lib_ring
      _ = y := by rw [el, ei]; lib_ring

def tau (a b : Zp (2 * m + 1 + 1)) : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) → Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))
  | none => some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).zero)
  | some p => if (ring (2 * m + 1 + 1)).mul p.2 p.2 = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul p.1 ((ring (2 * m + 1 + 1)).mul p.1 p.1)) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul p.1 p.1))) ((ring (2 * m + 1 + 1)).mul b p.1) then
      (if p.1 = (ring (2 * m + 1 + 1)).zero then none else some (t2x m b p.1, t2y m b p.1 p.2))
    else some p

theorem tau_some (a b x y : Zp (2 * m + 1 + 1)) : tau m a b (some (x, y)) =
    if (ring (2 * m + 1 + 1)).mul y y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x))) ((ring (2 * m + 1 + 1)).mul b x) then
      (if x = (ring (2 * m + 1 + 1)).zero then none else some (t2x m b x, t2y m b x y))
    else some (x, y) := rfl

theorem tau_gen (a b x y : Zp (2 * m + 1 + 1)) (hc : c2 m a b x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) : tau m a b (some (x, y)) = some (t2x m b x, t2y m b x y) := by
  unfold c2 at hc; rw [tau_some, ite_pos' _ _ hc, ite_neg' _ _ hx]

theorem tau_off (a b x y : Zp (2 * m + 1 + 1)) (hc : ¬ c2 m a b x y) : tau m a b (some (x, y)) = some (x, y) := by
  unfold c2 at hc; rw [tau_some, ite_neg' _ _ hc]

theorem y0 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (y : Zp (2 * m + 1 + 1)) (h : (ring (2 * m + 1 + 1)).mul y y = (ring (2 * m + 1 + 1)).zero) : y = (ring (2 * m + 1 + 1)).zero := by
  rcases mul_eq_zero m hpr y y h with e | e <;> exact e

theorem tau_T (a b : Zp (2 * m + 1 + 1)) : tau m a b (some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).zero)) = none := by
  rw [tau_some, ite_pos' _ _ (show (ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero)) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).ze  -- (+124 chars; full line in the Zenodo source)

/-- **τ² = id.** -/
theorem tau2 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b : Zp (2 * m + 1 + 1)) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) :
    tau m a b (tau m a b P) = P := by
  cases P with
  | none => exact tau_T m a b
  | some p =>
    obtain ⟨x, y⟩ := p
    by_cases hc : (ring (2 * m + 1 + 1)).mul y y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x))) ((ring (2 * m + 1 + 1)).mul b x)
    · by_cases h0 : x = (ring (2 * m + 1 + 1)).zero
      · rw [h0] at hc ⊢
        have hy := y0 m hpr y (by rw [hc]; lib_ring)
        rw [hy, tau_T]; rfl
      · rw [tau_gen m a b x y hc h0, tau_gen m a b _ _ (on2 m hpr a b x y hc h0) (fun e => hb (by
            have ei := iv_mul_self m hpr x h0
            unfold t2x at e
            calc b = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b (iv m x)) x := by rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b (iv m x)) x = (ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul (iv m x) x) by lib_ring, ei]; lib_ring
              _ = (ring (2 * m + 1 + 1)).zero := by rw [e]; lib_ring)), (inv2 m hpr b x y h0 hb).1, (inv2 m hpr b x y h0 hb).2]
    · rw [tau_off m a b x y hc, tau_off m a b x y hc]

def onE (a b : Zp (2 * m + 1 + 1)) : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) → Int
  | none => 1
  | some p => if (ring (2 * m + 1 + 1)).mul p.2 p.2 = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul p.1 ((ring (2 * m + 1 + 1)).mul p.1 p.1)) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul p.1 p.1))) ((ring (2 * m + 1 + 1)).mul b p.1) then 1 else 0

theorem onE_on (a b x y : Zp (2 * m + 1 + 1)) (hc : c2 m a b x y) : onE m a b (some (x, y)) = 1 := by
  unfold c2 at hc; show (if _ then _ else _) = _; rw [ite_pos' _ _ hc]

theorem onE_off (a b x y : Zp (2 * m + 1 + 1)) (hc : ¬ c2 m a b x y) : onE m a b (some (x, y)) = 0 := by
  unfold c2 at hc; show (if _ then _ else _) = _; rw [ite_neg' _ _ hc]

theorem onE_tau (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b : Zp (2 * m + 1 + 1)) (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) :
    onE m a b (tau m a b P) = onE m a b P := by
  cases P with
  | none => exact onE_on m a b _ _ (by unfold c2; lib_ring)
  | some p =>
    obtain ⟨x, y⟩ := p
    by_cases hc : (ring (2 * m + 1 + 1)).mul y y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x))) ((ring (2 * m + 1 + 1)).mul b x)
    · rw [onE_on m a b x y hc]
      by_cases h0 : x = (ring (2 * m + 1 + 1)).zero
      · rw [tau_some, ite_pos' _ _ hc, ite_pos' _ _ h0]; rfl
      · rw [tau_gen m a b x y hc h0]; exact onE_on m a b _ _ (on2 m hpr a b x y hc h0)
    · rw [tau_off m a b x y hc]

/-- a fixed point with x ≠ 0 forces a² = 4b. -/
theorem fix2 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b x y : Zp (2 * m + 1 + 1)) (hc : c2 m a b x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero)
    (h2 : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero) (fx : t2x m b x = x) (fy : t2y m b x y = y) :
    (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) b)) = (ring (2 * m + 1 + 1)).zero := by
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  unfold c2 at hc; unfold t2x at fx; unfold t2y at fy
  generalize iv m x = i at ei fx fy
  have hb : b = (ring (2 * m + 1 + 1)).mul x x := by
    calc b = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b i) x := by rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b i) x = (ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul i x) by lib_ring, ei]; lib_ring
      _ = (ring (2 * m + 1 + 1)).mul x x := by rw [fx]
  have e2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) y = (ring (2 * m + 1 + 1)).zero := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul i i)))) y := by rw [fy]; lib_ring
      _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul i i)))) y := by rw [hb]
      _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul i x) ((ring (2 * m + 1 + 1)).mul i x)))) y := by lib_ring
      _ = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have hy : y = (ring (2 * m + 1 + 1)).zero := by
    rcases mul_eq_zero m hpr _ _ e2 with e | e
    · exact absurd e h2
    · exact e
  rw [hy, hb] at hc
  have e3 : (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) x) a)) = (ring (2 * m + 1 + 1)).zero := by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zer  -- (+18 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [← hc]; lib_ring
  have e4 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) x) a = (ring (2 * m + 1 + 1)).zero := by
    rcases mul_eq_zero m hpr _ _ e3 with e | e
    · exact absurd e hx
    · rcases mul_eq_zero m hpr _ _ e with e' | e'
      · exact absurd e' hx
      · exact e'
  rw [hb]
  calc _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) x))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) x) a) := by lib_ring
    _ = (ring (2 * m + 1 + 1)).zero := by rw [e4]; lib_ring

def cnt2 (a b : Zp (2 * m + 1 + 1)) (q : Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) : Int :=
  if (ring (2 * m + 1 + 1)).mul q.2 q.2 = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul q.1 ((ring (2 * m + 1 + 1)).mul q.1 q.1)) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul q.1 q.1))) ((ring (2 * m + 1 + 1)).mul b q.1) then 1 else 0

theorem onE_fix (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b : Zp (2 * m + 1 + 1)) (h2 : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero)
    (hd : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) b)) ≠ (ring (2 * m + 1 + 1)).zero)
    (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) (h : tau m a b P = P) : onE m a b P = 0 := by
  cases P with
  | none => exact absurd h (fun e => by cases e)
  | some p =>
    obtain ⟨x, y⟩ := p
    by_cases hc : (ring (2 * m + 1 + 1)).mul y y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x))) ((ring (2 * m + 1 + 1)).mul b x)
    · exfalso
      by_cases h0 : x = (ring (2 * m + 1 + 1)).zero
      · rw [tau_some, ite_pos' _ _ hc, ite_pos' _ _ h0] at h
        cases h
      · rw [tau_gen m a b x y hc h0] at h
        have e' := Option.some.inj h
        exact hd (fix2 m hpr a b x y hc h0 h2 (congrArg Prod.fst e') (congrArg Prod.snd e'))
    · exact onE_off m a b x y hc

/-- **G5, order 2: 2 | #E(𝔽_p)** for y² = x³ + ax² + bx, b ≠ 0, a² ≠ 4b, p odd. -/
theorem g2 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b : Zp (2 * m + 1 + 1)) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) (h2 : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero)
    (hd : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) b)) ≠ (ring (2 * m + 1 + 1)).zero) :
    ∃ k : Int, 1 + sumK LibInt.intR (cnt2 m a b) (pL m) = 2 * k := by
  have e : sumK LibInt.intR (onE m a b) (ptsL m) = 1 + sumK LibInt.intR (cnt2 m a b) (pL m) := by
    show LibInt.intR.add 1 (sumK LibInt.intR (onE m a b) ((pL m).map some)) = _
    rw [sum_map]; rfl
  rw [← e]
  exact ⟨_, orbit2g (ptsL m) (complete_pts m) (code m) (code_inj m) (tau m a b) (tau2 m hpr a b hb)
    (onE m a b) (onE_tau m hpr a b) (onE_fix m hpr a b h2 hd)⟩

end G5Two

#print axioms G5Two.one_min2
#print axioms G5Two.mn_comm
#print axioms G5Two.orbit2g
#print axioms G5Two.on2
#print axioms G5Two.inv2
#print axioms G5Two.tau_some
#print axioms G5Two.tau_gen
#print axioms G5Two.tau_off
#print axioms G5Two.y0
#print axioms G5Two.tau_T
#print axioms G5Two.tau2
#print axioms G5Two.onE_on
#print axioms G5Two.onE_off
#print axioms G5Two.onE_tau
#print axioms G5Two.fix2
#print axioms G5Two.onE_fix
#print axioms G5Two.g2

lang/G7C1l.lean

lean_all/lang/G7C1l.lean

/-
  lang/G7C1l — T43, cycle 387: the rescaled step identity 1l of the order-7 family, with all denominators cleared: the
  cleared identity is a multiple of the rescaled curve (a five-term quotient), and the inverse relations put the denominators back.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G7LF

namespace G7LF

open FTAChain ZMod YMChain

variable (m : Nat)

theorem cstep1l (a u v i j k : Zp (2 * m + 1 + 1)) (gri : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1  -- (+2045 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) (v))) (i)) = (ring (2  -- (+896 chars; full line in the Zenodo source)
  have ri : (ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (a)) (v)))) = (ring (2 * m + 1 + 1)).one := by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (a))  -- (+107 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).one := by rw [gri]; lib_ring
  have rj : (ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) (v))) = (ring (2 * m + 1 + 1)).one := by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) (v)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1   -- (+24 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).one := by rw [grj]; lib_ring
  have rk : (ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1  -- (+242 chars; full line in the Zenodo source)
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m   -- (+364 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).one := by rw [grk]; lib_ring
  calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) (v))) (i)) = (ring  -- (+1322 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m   -- (+5002 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (  -- (+3492 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (  -- (+538 chars; full line in the Zenodo source)

end G7LF

#print axioms G7LF.cstep1l

lang/G7C1x.lean

lean_all/lang/G7C1x.lean

/-
  lang/G7C1x — T43, cycle 385: the rescaled step identity 1x of the order-7 family (x = t(t − 1)u, y = t²(t − 1)²v), a polynomial
  identity in u, v, t and the inverses of three small linear forms, modulo the rescaled curve. Certificate from the std-lib
  Python reduction. Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G7LF

namespace G7LF

open FTAChain ZMod YMChain

variable (m : Nat)

set_option maxRecDepth 100000 in
theorem cstep1x (a u v i j k : Zp (2 * m + 1 + 1)) (gri : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul u (i))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a (i))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul v ((ring (2 * m +  -- (+1759 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u))) (i)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((rin  -- (+930 chars; full line in the Zenodo source)
  calc (ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u))) (i)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((  -- (+64414 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1))  -- (+245 chars; full line in the Zenodo source)

end G7LF

#print axioms G7LF.cstep1x

lang/G7C2l.lean

lean_all/lang/G7C2l.lean

/-
  lang/G7C2l — T43, cycle 387: the rescaled step identity 2l of the order-7 family, with all denominators cleared: the
  cleared identity is a multiple of the rescaled curve (a five-term quotient), and the inverse relations put the denominators back.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G7LF

namespace G7LF

open FTAChain ZMod YMChain

variable (m : Nat)

theorem cstep2l (a u v i j k : Zp (2 * m + 1 + 1)) (gri : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero) (grj : (ring (2  -- (+1660 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) (i)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a  -- (+1128 chars; full line in the Zenodo source)
  have ri : (ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u))) = (ring (2 * m + 1 + 1)).one := by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [gri]; lib_ring
  have rj : (ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (a)) (v)))) = (ring (2 * m + 1 + 1)).one := by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (a))  -- (+107 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).one := by rw [grj]; lib_ring
  have rk : (ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) (v))) = (ring (2 * m + 1 + 1)).one := by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) (v)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1   -- (+24 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).one := by rw [grk]; lib_ring
  calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) (i)) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 *   -- (+743 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m   -- (+4228 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ri  -- (+2987 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ri  -- (+931 chars; full line in the Zenodo source)

end G7LF

#print axioms G7LF.cstep2l

lang/G7C2x.lean

lean_all/lang/G7C2x.lean

/-
  lang/G7C2x — T43, cycle 387: the rescaled step identity 2x of the order-7 family, with all denominators cleared: the
  cleared identity is a multiple of the rescaled curve (a five-term quotient), and the inverse relations put the denominators back.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G7LF

namespace G7LF

open FTAChain ZMod YMChain

variable (m : Nat)

theorem cstep2x (a u v i j k : Zp (2 * m + 1 + 1)) (gri : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1  -- (+2385 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) (v))) (i)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1  -- (+441 chars; full line in the Zenodo source)
  have ri : (ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (a)) (v)))) = (ring (2 * m + 1 + 1)).one := by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (a))  -- (+107 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).one := by rw [gri]; lib_ring
  have rj : (ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1  -- (+242 chars; full line in the Zenodo source)
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m   -- (+364 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).one := by rw [grj]; lib_ring
  have rk : (ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1  -- (+242 chars; full line in the Zenodo source)
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m   -- (+364 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).one := by rw [grk]; lib_ring
  calc (ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) (v))) (i)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1   -- (+1403 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) (v))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mu  -- (+1789 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) (v))) k) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1  -- (+885 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) (v))) (k) := by rw [ri, rj]; lib_ring

end G7LF

#print axioms G7LF.cstep2x

lang/G7C3l.lean

lean_all/lang/G7C3l.lean

/-
  lang/G7C3l — T43, cycle 385: the rescaled step identity 3l of the order-7 family (x = t(t − 1)u, y = t²(t − 1)²v), a polynomial
  identity in u, v, t and the inverses of three small linear forms, modulo the rescaled curve. Certificate from the std-lib
  Python reduction. Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G7LF

namespace G7LF

open FTAChain ZMod YMChain

variable (m : Nat)

set_option maxRecDepth 100000 in
theorem cstep3l (a u v i j k : Zp (2 * m + 1 + 1)) (gri : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul u (i))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a (i))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul v ((ring (2 * m + 1 + 1)).mul a (i)))) ((ring   -- (+1542 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v))) (i)) = (ring (2 * m + 1 + 1  -- (+706 chars; full line in the Zenodo source)
  calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v))) (i)) = (ring (2 * m + 1   -- (+37326 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) (j))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).n  -- (+368 chars; full line in the Zenodo source)

end G7LF

#print axioms G7LF.cstep3l

lang/G7C3x.lean

lean_all/lang/G7C3x.lean

/-
  lang/G7C3x — T43, cycle 385: the rescaled step identity 3x of the order-7 family (x = t(t − 1)u, y = t²(t − 1)²v), a polynomial
  identity in u, v, t and the inverses of three small linear forms, modulo the rescaled curve. Certificate from the std-lib
  Python reduction. Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G7LF

namespace G7LF

open FTAChain ZMod YMChain

variable (m : Nat)

set_option maxRecDepth 100000 in
theorem cstep3x (a u v i j k : Zp (2 * m + 1 + 1)) (gri : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a (i))) ((ring (2 * m + 1 + 1)).mul u ((ring (2 * m + 1 + 1)).mul a (i)))) = (ring (2 * m + 1 + 1)).zero) (grj : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((rin  -- (+1600 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) (i)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring  -- (+616 chars; full line in the Zenodo source)
  calc (ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) (i)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((r  -- (+49335 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v))) (k) := by rw [gri, grj, grk, gcv]; lib_ring

end G7LF

#print axioms G7LF.cstep3x

lang/G7C4l.lean

lean_all/lang/G7C4l.lean

/-
  lang/G7C4l — T43, cycle 387: the rescaled step identity 4l of the order-7 family, with all denominators cleared: the
  cleared identity is a multiple of the rescaled curve (a five-term quotient), and the inverse relations put the denominators back.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G7LF

namespace G7LF

open FTAChain ZMod YMChain

variable (m : Nat)

theorem cstep4l (a u v i j k : Zp (2 * m + 1 + 1)) (gri : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).  -- (+1743 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u))) (i)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((r  -- (+884 chars; full line in the Zenodo source)
  have ri : (ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v))) = (ring (2 * m + 1 + 1)).one := by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v))))  -- (+99 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).one := by rw [gri]; lib_ring
  have rj : (ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (a))) (u))) ((ring (2 * m + 1 + 1)).mul (a) (v))) = (ring (2 * m + 1 + 1)).one := by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (a))) (u))) ((ring (2 * m + 1 + 1)).mul (a) (v)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)  -- (+49 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).one := by rw [grj]; lib_ring
  have rk : (ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) ((ring (2 * m + 1 + 1)).mul (a) (v)))) = (ring (2 * m + 1 + 1)).one := by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) ((ring (2 * m + 1 + 1)).mul (a) (v))))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).on  -- (+45 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).one := by rw [grk]; lib_ring
  calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u))) (i)) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add   -- (+872 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m   -- (+3756 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (  -- (+2946 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (  -- (+604 chars; full line in the Zenodo source)

end G7LF

#print axioms G7LF.cstep4l

lang/G7C4x.lean

lean_all/lang/G7C4x.lean

/-
  lang/G7C4x — T43, cycle 387: the rescaled step identity 4x of the order-7 family, with all denominators cleared: the
  cleared identity is a multiple of the rescaled curve (a five-term quotient), and the inverse relations put the denominators back.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G7LF

namespace G7LF

open FTAChain ZMod YMChain

variable (m : Nat)

theorem cstep4x (a u v i j k : Zp (2 * m + 1 + 1)) (gri : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (a))) (u))) ((ring (2 * m + 1 + 1)).mul (a) (v)))) ((ring (2 * m + 1 + 1)).neg (rin  -- (+1660 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v))) (i)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((rin  -- (+388 chars; full line in the Zenodo source)
  have ri : (ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (a))) (u))) ((ring (2 * m + 1 + 1)).mul (a) (v))) = (ring (2 * m + 1 + 1)).one := by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (a))) (u))) ((ring (2 * m + 1 + 1)).mul (a) (v)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)  -- (+49 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).one := by rw [gri]; lib_ring
  have rj : (ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v))) = (ring (2 * m + 1 + 1)).one := by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one :=  -- (+12 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).one := by rw [grj]; lib_ring
  have rk : (ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (a))) (u))) ((ring (2 * m + 1 + 1)).mul (a) (v))) = (ring (2 * m + 1 + 1)).one := by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (a))) (u))) ((ring (2 * m + 1 + 1)).mul (a) (v)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)  -- (+49 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).one := by rw [grk]; lib_ring
  calc (ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v))) (i)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((  -- (+940 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) ((ring (2 * m + 1 + 1)).mul (a) (v)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((rin  -- (+1400 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) ((ring (2 * m + 1 + 1)).mul (a) (v)))) k) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add   -- (+496 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) ((ring (2 * m + 1 + 1)).mul (a) (v)))) (k) := by rw [ri, rj]; lib_ring

end G7LF

#print axioms G7LF.cstep4x

lang/G7C5l.lean

lean_all/lang/G7C5l.lean

/-
  lang/G7C5l — T43, cycle 387: the rescaled step identity 5l of the order-7 family, with all denominators cleared: the
  cleared identity is a multiple of the rescaled curve (a five-term quotient), and the inverse relations put the denominators back.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G7LF

namespace G7LF

open FTAChain ZMod YMChain

variable (m : Nat)

theorem cstep5l (a u v i j k : Zp (2 * m + 1 + 1)) (gri : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) (u))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 *   -- (+1582 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring  -- (+1012 chars; full line in the Zenodo source)
  have ri : (ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) (u)) = (ring (2 * m + 1 + 1)).one := by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) (u))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one :=  -- (+12 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).one := by rw [gri]; lib_ring
  have rj : (ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v))) = (ring (2 * m + 1 + 1)).one := by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v))))  -- (+99 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).one := by rw [grj]; lib_ring
  have rk : (ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u))) = (ring (2 * m + 1 + 1)).one := by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [grk]; lib_ring
  calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (a))) ((r  -- (+1326 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m  -- (+3335 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m  -- (+2401 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u))) (j)))  -- (+468 chars; full line in the Zenodo source)

end G7LF

#print axioms G7LF.cstep5l

lang/G7C5x.lean

lean_all/lang/G7C5x.lean

/-
  lang/G7C5x — T43, cycle 387: the rescaled step identity 5x of the order-7 family, with all denominators cleared: the
  cleared identity is a multiple of the rescaled curve (a five-term quotient), and the inverse relations put the denominators back.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G7LF

namespace G7LF

open FTAChain ZMod YMChain

variable (m : Nat)

theorem cstep5x (a u v i j k : Zp (2 * m + 1 + 1)) (gri : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).  -- (+1673 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u))) (i)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 *  -- (+293 chars; full line in the Zenodo source)
  have ri : (ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v))) = (ring (2 * m + 1 + 1)).one := by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v))))  -- (+99 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).one := by rw [gri]; lib_ring
  have rj : (ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) ((ring (2 * m + 1 + 1)).mul (a) (v)))) = (ring (2 * m + 1 + 1)).one := by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) ((ring (2 * m + 1 + 1)).mul (a) (v))))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).on  -- (+45 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).one := by rw [grj]; lib_ring
  have rk : (ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) (v)) = (ring (2 * m + 1 + 1)).one := by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) (v))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [grk]; lib_ring
  calc (ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u))) (i)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (  -- (+928 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 *   -- (+1426 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u))) k) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring   -- (+455 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u))) (k) := by rw [ri, rj]; lib_ring

end G7LF

#print axioms G7LF.cstep5x

lang/G7Count.lean

lean_all/lang/G7Count.lean

/-
  lang/G7Count — T43, cycle 373: assembly of G5 order 7 — 7 | #E_t(𝔽_p) for E_t: y² + (1 − c)xy − by = x³ − bx²,
  b = t³ − t², c = t² − t, modulo the step identities and the fixed-point lemma.

    tauG                 translation by T = (0, 0) on {O} ∪ 𝔽_p² (identity off the curve)
    glue                 one step P_k = (X_k, λ_k X_k) ↦ P_{k+1} from the two step identities
    Steps7, Fix7         the statements of lang/G7S0 … G7S6 and lang/G7Fix (proved there; G7S1–G7S5, G7Fix on Kamiak)
    chain7               τ⁷P = P for P ∈ E_t with x ∉ {0, b, c}
    tors_*, tau7         the torsion cycle O → (0,0) → (b, bc) → (c, b − c) → (c, c²) → (b, 0) → (0, b) → O, and τ⁷ = id
    onG_tau, onG_fix     the indicator of E_t ∪ {O} is τ-invariant and vanishes at fixed points
    g7_of                1 + #{(x, y) ∈ 𝔽_p² on E_t} = 7k, given Steps7 and Fix7 (t ≠ 0, 1, 2 ≠ 0, t³ − 8t² + 5t + 1 ≠ 0)
  The unconditional statement is lang/G7Final (Kamiak). Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G7Gen
import lang.Orbit7
import lang.Pairs

namespace G7LF

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Step G4Gen G4Map G7Tate1 Pairs Orbit5
open FiniteRecChain (ite_pos' ite_neg')

variable (m : Nat)

def tauG (b c : Zp (2 * m + 1 + 1)) : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) → Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))
  | none => some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).zero)
  | some p => if (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul p.2 p.2) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg c)) ((ring (2 * m + 1 + 1)).mul p.1 p.2))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b p.2)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul p.1 ((ring (2 *   -- (+127 chars; full line in the Zenodo source)
      (if p.1 = (ring (2 * m + 1 + 1)).zero then (if p.2 = (ring (2 * m + 1 + 1)).zero then some (b, (ring (2 * m + 1 + 1)).mul b c) else none) else some (tx7 m b c p.1 p.2, ty7 m b c p.1 p.2))
    else some p

theorem tauG_some (b c x y : Zp (2 * m + 1 + 1)) : tauG m b c (some (x, y)) =
    if (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg c)) ((ring (2 * m + 1 + 1)).mul x y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b y)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x))   -- (+97 chars; full line in the Zenodo source)
      (if x = (ring (2 * m + 1 + 1)).zero then (if y = (ring (2 * m + 1 + 1)).zero then some (b, (ring (2 * m + 1 + 1)).mul b c) else none) else some (tx7 m b c x y, ty7 m b c x y))
    else some (x, y) := rfl

theorem tauG_gen (b c x y : Zp (2 * m + 1 + 1)) (hc : cB m b c x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) : tauG m b c (some (x, y)) = some (tx7 m b c x y, ty7 m b c x y) := by
  unfold cB at hc
  rw [tauG_some, ite_pos' _ _ hc, ite_neg' _ _ hx]

theorem tauG_off (b c x y : Zp (2 * m + 1 + 1)) (hc : ¬ cB m b c x y) : tauG m b c (some (x, y)) = some (x, y) := by
  unfold cB at hc
  rw [tauG_some, ite_neg' _ _ hc]

/-- **One step** (X, λX) ↦ (X', λ'X') of the translation, from the two step identities. -/
theorem glue (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (b c X L J X' L' J' : Zp (2 * m + 1 + 1)) (hc : cB m b c X ((ring (2 * m + 1 + 1)).mul L X)) (hX : X ≠ (ring (2 * m + 1 + 1)).zero)
    (hXJ : (ring (2 * m + 1 + 1)).mul X J = (ring (2 * m + 1 + 1)).one) (hsx : (ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul L J) = X') (hsl : L' = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add L ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg c)))) ((ring (2 * m + 1 + 1)).mul b J'))
    (hJ' : (ring (2 * m + 1 + 1)).mul X' J' = (ring (2 * m + 1 + 1)).one) :
    tauG m b c (some (X, (ring (2 * m + 1 + 1)).mul L X)) = some (X', (ring (2 * m + 1 + 1)).mul L' X') ∧ cB m b c X' ((ring (2 * m + 1 + 1)).mul L' X') := by
  have hJ : iv m X = J := iv_eq m hpr X J hXJ
  have e1 : tx7 m b c X ((ring (2 * m + 1 + 1)).mul L X) = X' := by
    unfold tx7; rw [hJ]
    calc (ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul L X) ((ring (2 * m + 1 + 1)).mul J J)) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul L J)) ((ring (2 * m + 1 + 1)).mul X J) := by lib_ring
      _ = X' := by rw [hXJ, hsx]; lib_ring
  have e2 : ty7 m b c X ((ring (2 * m + 1 + 1)).mul L X) = (ring (2 * m + 1 + 1)).mul L' X' := by
    unfold ty7; rw [hJ]
    calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul L X) J) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg c))) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul L X) ((ring (2 * m + 1 + 1)).mul J J)))))   -- (+1 chars; full line in the Zenodo source)
        = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul L ((ring (2 * m + 1 + 1)).mul X J)) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg c))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul L J)) ((ring (2 * m + 1 + 1)).mul X J))))  -- (+17 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add L ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg c))) X')) b := by rw [hXJ, hsx]; lib_ring
      _ = (ring (2 * m + 1 + 1)).mul L' X' := by
        rw [hsl]
        calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add L ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg c))) X')) b = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add L ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2  -- (+119 chars; full line in the Zenodo source)
          _ = _ := by lib_ring
  have hon := on7 m hpr b c X ((ring (2 * m + 1 + 1)).mul L X) hc hX
  rw [e1, e2] at hon
  exact ⟨by rw [tauG_gen m b c X _ hc hX, e1, e2], hon⟩

/-- the statements of the fourteen step identities (lang/G7S0 … G7S6). -/
def Steps7 : Prop :=
    (∀ (a x y : Zp (2 * m + 1 + 1)), cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y → x ≠ (ring (2 * m + 1 + 1)).zero → X1d m a x y ≠ (ring (2 * m + 1 + 1)).zero → (ring (2 *  -- (+390 chars; full line in the Zenodo source)
    (∀ (a x y : Zp (2 * m + 1 + 1)), cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y → L1d m a x y ≠ (ring (2 * m + 1 + 1)).zero → x ≠ (ring (2 * m + 1 + 1)).zero → X1n m a x   -- (+671 chars; full line in the Zenodo source)
    (∀ (a x y : Zp (2 * m + 1 + 1)), cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y → L1d m a x y ≠ (ring (2 * m + 1 + 1)).zero → X1n m a x y ≠ (ring (2 * m + 1 + 1)).zero →   -- (+459 chars; full line in the Zenodo source)
    (∀ (a x y : Zp (2 * m + 1 + 1)), cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y → L2d m a x y ≠ (ring (2 * m + 1 + 1)).zero → L1d m a x y ≠ (ring (2 * m + 1 + 1)).zero →   -- (+701 chars; full line in the Zenodo source)
    (∀ (a x y : Zp (2 * m + 1 + 1)), cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y → L2d m a x y ≠ (ring (2 * m + 1 + 1)).zero → X2n m a x y ≠ (ring (2 * m + 1 + 1)).zero →   -- (+459 chars; full line in the Zenodo source)
    (∀ (a x y : Zp (2 * m + 1 + 1)), cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y → L3d m a x y ≠ (ring (2 * m + 1 + 1)).zero → L2d m a x y ≠ (ring (2 * m + 1 + 1)).zero →   -- (+701 chars; full line in the Zenodo source)
    (∀ (a x y : Zp (2 * m + 1 + 1)), cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y → L3d m a x y ≠ (ring (2 * m + 1 + 1)).zero → X3n m a x y ≠ (ring (2 * m + 1 + 1)).zero →   -- (+459 chars; full line in the Zenodo source)
    (∀ (a x y : Zp (2 * m + 1 + 1)), cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y → L4d m a x y ≠ (ring (2 * m + 1 + 1)).zero → L3d m a x y ≠ (ring (2 * m + 1 + 1)).zero →   -- (+701 chars; full line in the Zenodo source)
    (∀ (a x y : Zp (2 * m + 1 + 1)), cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y → L4d m a x y ≠ (ring (2 * m + 1 + 1)).zero → X4n m a x y ≠ (ring (2 * m + 1 + 1)).zero →   -- (+415 chars; full line in the Zenodo source)
    (∀ (a x y : Zp (2 * m + 1 + 1)), cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y → L5d m a x y ≠ (ring (2 * m + 1 + 1)).zero → L4d m a x y ≠ (ring (2 * m + 1 + 1)).zero →   -- (+701 chars; full line in the Zenodo source)
    (∀ (a x y : Zp (2 * m + 1 + 1)), cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y → L5d m a x y ≠ (ring (2 * m + 1 + 1)).zero → X5n m a x y ≠ (ring (2 * m + 1 + 1)).zero →   -- (+459 chars; full line in the Zenodo source)
    (∀ (a x y : Zp (2 * m + 1 + 1)), cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y → L6d m a x y ≠ (ring (2 * m + 1 + 1)).zero → L5d m a x y ≠ (ring (2 * m + 1 + 1)).zero →   -- (+701 chars; full line in the Zenodo source)
    (∀ (a x y : Zp (2 * m + 1 + 1)), cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y → L6d m a x y ≠ (ring (2 * m + 1 + 1)).zero → X6n m a x y ≠ (ring (2 * m + 1 + 1)).zero →   -- (+413 chars; full line in the Zenodo source)
    (∀ (a x y : Zp (2 * m + 1 + 1)), cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y → x ≠ (ring (2 * m + 1 + 1)).zero → (ring (2 * m + 1 + 1)).mul (y) (iv m (x)) = (ring (2 *  -- (+586 chars; full line in the Zenodo source)

/-- the statement of lang/G7Fix.fixed7. -/
def Fix7 : Prop := ∀ (a x y : Zp (2 * m + 1 + 1)), cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) x y → x ≠ (ring (2 * m + 1 + 1)).zero → tx7 m ((ring (2 * m + 1 + 1)).add ((ring  -- (+519 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero → a ≠ (ring (2 * m + 1 + 1)).zero → a ≠ (ring (2 * m + 1 + 1)).one → (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring   -- (+691 chars; full line in the Zenodo source)

theorem bg_eq (a : Zp (2 * m + 1 + 1)) : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) := by lib_ring
theorem cg_eq (a : Zp (2 * m + 1 + 1)) : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a)) := by lib_ring

/-- **τ⁷P = P** at the generic points of E_t. -/
theorem chain7 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (hS : Steps7 m) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (  -- (+88 chars; full line in the Zenodo source)
    (h0 : x ≠ (ring (2 * m + 1 + 1)).zero) (hb : x ≠ (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) (hc0 : x ≠ (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) :
    tauG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) (tauG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 +  -- (+1546 chars; full line in the Zenodo source)
  obtain ⟨s0x, s0l, s1x, s1l, s2x, s2l, s3x, s3l, s4x, s4l, s5x, s5l, s6x, s6l⟩ := hS
  have hcg : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) x y := by rw [← bg_eq m a, ← cg_eq m a]; exact hc
  have hbg : x ≠ (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) := by rw [← bg_eq m a]; exact hb
  have hcg0 : x ≠ (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a)) := by rw [← cg_eq m a]; exact hc0
  have n_L1d : L1d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_L1d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_L2d : L2d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_L2d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_L3d : L3d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_L3d m hpr a x y ht0 ht1 h0 hbg hcg0
  have n_L4d : L4d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_L4d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_L5d : L5d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_L5d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_L6d : L6d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_L6d m hpr a x y ht0 ht1 h0 hbg hcg0
  have n_X1d : X1d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X1d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_X1n : X1n m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X1n m hpr a x y ht0 ht1 h0 hbg hcg0
  have n_X2d : X2d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X2d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_X2n : X2n m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X2n m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_X3d : X3d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X3d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_X3n : X3n m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X3n m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_X4d : X4d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X4d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_X4n : X4n m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X4n m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_X5d : X5d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X5d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_X5n : X5n m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X5n m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_X6d : X6d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X6d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_X6n : X6n m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X6n m hpr a x y ht0 ht1 h0 hbg hcg0
  have ix : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x h0
  have xj0 : (ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (iv m (x))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (iv m (x))) = (ring (2 * m + 1 + 1)).mul (iv m x) x := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := ix
  have xj1 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y))) ((ring (2 * m + 1 + 1)).mul (X1d m a x y) (iv m (X1n m a x y))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y))) ((ring (2 * m + 1 + 1)).mul (X1d m a x y) (iv m (X1n m a x y))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m (X1d m a x y)) (X1d m a x y)) ((ring (2 * m + 1 + 1)).mul (iv m (X1n m a x y)) (X1n m a x y)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [iv_mul_self m hpr _ n_X1d, iv_mul_self m hpr _ n_X1n]; lib_ring
  have nx1 : (ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y)) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ n_X1n (iv_nz m hpr _ n_X1d)
  have xj2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a x y))) ((ring (2 * m + 1 + 1)).mul (X2d m a x y) (iv m (X2n m a x y))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a x y))) ((ring (2 * m + 1 + 1)).mul (X2d m a x y) (iv m (X2n m a x y))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m (X2d m a x y)) (X2d m a x y)) ((ring (2 * m + 1 + 1)).mul (iv m (X2n m a x y)) (X2n m a x y)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [iv_mul_self m hpr _ n_X2d, iv_mul_self m hpr _ n_X2n]; lib_ring
  have nx2 : (ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a x y)) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ n_X2n (iv_nz m hpr _ n_X2d)
  have xj3 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X3n m a x y) (iv m (X3d m a x y))) ((ring (2 * m + 1 + 1)).mul (X3d m a x y) (iv m (X3n m a x y))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X3n m a x y) (iv m (X3d m a x y))) ((ring (2 * m + 1 + 1)).mul (X3d m a x y) (iv m (X3n m a x y))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m (X3d m a x y)) (X3d m a x y)) ((ring (2 * m + 1 + 1)).mul (iv m (X3n m a x y)) (X3n m a x y)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [iv_mul_self m hpr _ n_X3d, iv_mul_self m hpr _ n_X3n]; lib_ring
  have nx3 : (ring (2 * m + 1 + 1)).mul (X3n m a x y) (iv m (X3d m a x y)) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ n_X3n (iv_nz m hpr _ n_X3d)
  have xj4 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X4n m a x y) (iv m (X4d m a x y))) ((ring (2 * m + 1 + 1)).mul (X4d m a x y) (iv m (X4n m a x y))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X4n m a x y) (iv m (X4d m a x y))) ((ring (2 * m + 1 + 1)).mul (X4d m a x y) (iv m (X4n m a x y))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m (X4d m a x y)) (X4d m a x y)) ((ring (2 * m + 1 + 1)).mul (iv m (X4n m a x y)) (X4n m a x y)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [iv_mul_self m hpr _ n_X4d, iv_mul_self m hpr _ n_X4n]; lib_ring
  have nx4 : (ring (2 * m + 1 + 1)).mul (X4n m a x y) (iv m (X4d m a x y)) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ n_X4n (iv_nz m hpr _ n_X4d)
  have xj5 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X5n m a x y) (iv m (X5d m a x y))) ((ring (2 * m + 1 + 1)).mul (X5d m a x y) (iv m (X5n m a x y))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X5n m a x y) (iv m (X5d m a x y))) ((ring (2 * m + 1 + 1)).mul (X5d m a x y) (iv m (X5n m a x y))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m (X5d m a x y)) (X5d m a x y)) ((ring (2 * m + 1 + 1)).mul (iv m (X5n m a x y)) (X5n m a x y)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [iv_mul_self m hpr _ n_X5d, iv_mul_self m hpr _ n_X5n]; lib_ring
  have nx5 : (ring (2 * m + 1 + 1)).mul (X5n m a x y) (iv m (X5d m a x y)) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ n_X5n (iv_nz m hpr _ n_X5d)
  have xj6 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X6n m a x y) (iv m (X6d m a x y))) ((ring (2 * m + 1 + 1)).mul (X6d m a x y) (iv m (X6n m a x y))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X6n m a x y) (iv m (X6d m a x y))) ((ring (2 * m + 1 + 1)).mul (X6d m a x y) (iv m (X6n m a x y))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m (X6d m a x y)) (X6d m a x y)) ((ring (2 * m + 1 + 1)).mul (iv m (X6n m a x y)) (X6n m a x y)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [iv_mul_self m hpr _ n_X6d, iv_mul_self m hpr _ n_X6n]; lib_ring
  have nx6 : (ring (2 * m + 1 + 1)).mul (X6n m a x y) (iv m (X6d m a x y)) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ n_X6n (iv_nz m hpr _ n_X6d)
  have xj7 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (iv m (x))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (iv m (x))) = (ring (2 * m + 1 + 1)).mul (iv m x) x := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := ix
  have e0 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) x = y := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) x = (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul (iv m x) x) := by lib_ring
      _ = y := by rw [ix]; lib_ring
  have c0 : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) x) := by rw [e0]; exact hc
  have g0 := glue m hpr ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) (x) ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (iv m (  -- (+264 chars; full line in the Zenodo source)
  have g1 := glue m hpr ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y))) ((ring (2 * m + 1 + 1)).mul (L1n m a x y) (iv m   -- (+354 chars; full line in the Zenodo source)
  have g2 := glue m hpr ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a x y))) ((ring (2 * m + 1 + 1)).mul (L2n m a x y) (iv m   -- (+354 chars; full line in the Zenodo source)
  have g3 := glue m hpr ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (X3n m a x y) (iv m (X3d m a x y))) ((ring (2 * m + 1 + 1)).mul (L3n m a x y) (iv m   -- (+354 chars; full line in the Zenodo source)
  have g4 := glue m hpr ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (X4n m a x y) (iv m (X4d m a x y))) ((ring (2 * m + 1 + 1)).mul (L4n m a x y) (iv m   -- (+348 chars; full line in the Zenodo source)
  have g5 := glue m hpr ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (X5n m a x y) (iv m (X5d m a x y))) ((ring (2 * m + 1 + 1)).mul (L5n m a x y) (iv m   -- (+354 chars; full line in the Zenodo source)
  have g6 := glue m hpr ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (X6n m a x y) (iv m (X6d m a x y))) ((ring (2 * m + 1 + 1)).mul (L6n m a x y) (iv m   -- (+316 chars; full line in the Zenodo source)
  have key : tauG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) (tauG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2   -- (+1815 chars; full line in the Zenodo source)
    rw [g0.1, g1.1, g2.1, g3.1, g4.1, g5.1, g6.1]
  rw [e0] at key
  rw [key]
  exact congrArg some (Prod.ext (show (ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).one) = x by lib_ring) (show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).one)) = y by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul (iv m x) x) := by lib_ring
      _ = y := by rw [ix]; lib_ring))


theorem nzB (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a : Zp (2 * m + 1 + 1)) (ht0 : a ≠ (ring (2 * m + 1 + 1)).zero) (ht1 : a ≠ (ring (2 * m + 1 + 1)).one) : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ≠ (ring (2 * m + 1 + 1)).zero := by
  have e : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) = (ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))) := by lib_ring
  rw [e]; exact mul_ne m hpr _ _ ht0 (mul_ne m hpr _ _ ht0 (sub_ne m a _ ht1))

theorem nzC (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a : Zp (2 * m + 1 + 1)) (ht0 : a ≠ (ring (2 * m + 1 + 1)).zero) (ht1 : a ≠ (ring (2 * m + 1 + 1)).one) : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)) ≠ (ring (2 * m + 1 + 1)).zero := by
  have e : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)) = (ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by lib_ring
  rw [e]; exact mul_ne m hpr _ _ ht0 (sub_ne m a _ ht1)

theorem nzBC (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a : Zp (2 * m + 1 + 1)) (ht0 : a ≠ (ring (2 * m + 1 + 1)).zero) (ht1 : a ≠ (ring (2 * m + 1 + 1)).one) : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 +  -- (+144 chars; full line in the Zenodo source)
  have e : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) = (ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul ((ring (2   -- (+179 chars; full line in the Zenodo source)
  rw [e]; exact mul_ne m hpr _ _ ht0 (mul_ne m hpr _ _ (sub_ne m a _ ht1) (sub_ne m a _ ht1))

theorem nzBCC (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a : Zp (2 * m + 1 + 1)) (ht0 : a ≠ (ring (2 * m + 1 + 1)).zero) (ht1 : a ≠ (ring (2 * m + 1 + 1)).one) :
    (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 +  -- (+244 chars; full line in the Zenodo source)
  have e : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) ((ring (2 * m + 1 + 1)).neg ((ring (2 *   -- (+217 chars; full line in the Zenodo source)
      (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))))) := by lib_ring
  rw [e]; exact neg_ne m _ (mul_ne m hpr _ _ ht0 (mul_ne m hpr _ _ (sub_ne m a _ ht1) (mul_ne m hpr _ _ (sub_ne m a _ ht1) (sub_ne m a _ ht1))))

theorem tO (b c : Zp (2 * m + 1 + 1)) : tauG m b c none = some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).zero) := rfl

theorem tT1 (b c : Zp (2 * m + 1 + 1)) : tauG m b c (some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).zero)) = some (b, (ring (2 * m + 1 + 1)).mul b c) := by
  rw [tauG_some, ite_pos' _ _ (show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg c)) ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero))) ((ring (2 * m + 1 + 1)  -- (+431 chars; full line in the Zenodo source)

theorem tT6 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a : Zp (2 * m + 1 + 1)) (ht0 : a ≠ (ring (2 * m + 1 + 1)).zero) (ht1 : a ≠ (ring (2 * m + 1 + 1)).one) :
    tauG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) (some ((ring (2 * m + 1 + 1)).zero, ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (  -- (+90 chars; full line in the Zenodo source)
  rw [tauG_some, ite_pos' _ _ (show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1   -- (+1420 chars; full line in the Zenodo source)

theorem tT2 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a : Zp (2 * m + 1 + 1)) (ht0 : a ≠ (ring (2 * m + 1 + 1)).zero) (ht1 : a ≠ (ring (2 * m + 1 + 1)).one) :
    tauG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) (some ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1  -- (+771 chars; full line in the Zenodo source)
  rw [tauG_gen m _ _ _ _ (by unfold cB; lib_ring) (nzB m hpr a ht0 ht1)]
  have hl : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) = (ring (2 * m + 1 + 1))  -- (+49 chars; full line in the Zenodo source)
  have e1 : tx7 m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 +   -- (+444 chars; full line in the Zenodo source)
    unfold tx7
    generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) = l at hl ⊢
    have gl : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul l ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by rw [hl]; lib_ring
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul a (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 +   -- (+772 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)) := by rw [gl]; lib_ring
  have e2 : ty7 m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 +   -- (+667 chars; full line in the Zenodo source)
    unfold ty7
    generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) = l at hl ⊢
    have gl : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul l ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by rw [hl]; lib_ring
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) ((ring (2 * m + 1 + 1)).mul ((ring (2   -- (+3399 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) := by rw [gl]; lib_ring
  rw [e1, e2]

theorem tT3 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a : Zp (2 * m + 1 + 1)) (ht0 : a ≠ (ring (2 * m + 1 + 1)).zero) (ht1 : a ≠ (ring (2 * m + 1 + 1)).one) :
    tauG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) (some ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)), (ring (2 * m + 1 + 1  -- (+639 chars; full line in the Zenodo source)
  rw [tauG_gen m _ _ _ _ (by unfold cB; lib_ring) (nzC m hpr a ht0 ht1)]
  have hl : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ (nzC m hpr a ht0 ht1)
  have e1 : tx7 m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 +  -- (+407 chars; full line in the Zenodo source)
    unfold tx7
    generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) = l at hl ⊢
    have gl : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul l ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by rw [hl]; lib_ring
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul a (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a (l))) ((ring (2 * m   -- (+613 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)) := by rw [gl]; lib_ring
  have e2 : ty7 m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 +  -- (+535 chars; full line in the Zenodo source)
    unfold ty7
    generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) = l at hl ⊢
    have gl : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul l ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by rw [hl]; lib_ring
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1  -- (+3964 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) := by rw [gl]; lib_ring
  rw [e1, e2]

theorem tT4 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a : Zp (2 * m + 1 + 1)) (ht0 : a ≠ (ring (2 * m + 1 + 1)).zero) (ht1 : a ≠ (ring (2 * m + 1 + 1)).one) :
    tauG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) (some ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)), (ring (2 * m + 1 + 1  -- (+413 chars; full line in the Zenodo source)
  rw [tauG_gen m _ _ _ _ (by unfold cB; lib_ring) (nzC m hpr a ht0 ht1)]
  have hl : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ (nzC m hpr a ht0 ht1)
  have e1 : tx7 m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 +  -- (+378 chars; full line in the Zenodo source)
    unfold tx7
    generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) = l at hl ⊢
    have gl : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul l ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by rw [hl]; lib_ring
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a (a))) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1  -- (+834 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) := by rw [gl]; lib_ring
  have e2 : ty7 m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 +  -- (+243 chars; full line in the Zenodo source)
    unfold ty7
    generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) = l at hl ⊢
    have gl : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul l ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by rw [hl]; lib_ring
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a (a))))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a (  -- (+2948 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [gl]; lib_ring
  rw [e1, e2]

theorem tT5 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a : Zp (2 * m + 1 + 1)) (ht0 : a ≠ (ring (2 * m + 1 + 1)).zero) (ht1 : a ≠ (ring (2 * m + 1 + 1)).one) :
    tauG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) (some ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1  -- (+282 chars; full line in the Zenodo source)
  rw [tauG_gen m _ _ _ _ (by unfold cB; lib_ring) (nzB m hpr a ht0 ht1)]
  have hl : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) = (ring (2 * m + 1 + 1))  -- (+49 chars; full line in the Zenodo source)
  have e1 : tx7 m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 +   -- (+112 chars; full line in the Zenodo source)
    unfold tx7
    generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) = l at hl ⊢
    have gl : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul l ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by rw [hl]; lib_ring
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul l ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1  -- (+24 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [gl]; lib_ring
  have e2 : ty7 m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 +   -- (+247 chars; full line in the Zenodo source)
    unfold ty7
    generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) = l at hl ⊢
    have gl : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul l ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by rw [hl]; lib_ring
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul l ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 +  -- (+159 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) := by rw [gl]; lib_ring
  rw [e1, e2]

theorem yroot (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (y r s : Zp (2 * m + 1 + 1)) (h : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg r)) ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg s)) = (ring (2 * m + 1 + 1)).zero) : y = r ∨ y = s := by
  rcases mul_eq_zero m hpr _ _ h with e | e
  · exact Or.inl (by calc y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg r)) r := by lib_ring
      _ = r := by rw [e]; lib_ring)
  · exact Or.inr (by calc y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg s)) s := by lib_ring
      _ = s := by rw [e]; lib_ring)

/-- **τ⁷ = id** on {O} ∪ 𝔽_p² for E_t. -/
theorem tau7 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (hS : Steps7 m) (a : Zp (2 * m + 1 + 1)) (ht0 : a ≠ (ring (2 * m + 1 + 1)).zero) (ht1 : a ≠ (ring (2 * m + 1 + 1)).one)
    (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) : tauG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) (tauG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a)   -- (+1583 chars; full line in the Zenodo source)
  cases P with
  | none => rw [tO, tT1, tT2 m hpr a ht0 ht1, tT3 m hpr a ht0 ht1, tT4 m hpr a ht0 ht1, tT5 m hpr a ht0 ht1, tT6 m hpr a ht0 ht1]
  | some p =>
    obtain ⟨x, y⟩ := p
    by_cases hc : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 *  -- (+546 chars; full line in the Zenodo source)
    · by_cases h0 : x = (ring (2 * m + 1 + 1)).zero
      · rw [h0] at hc ⊢
        rcases yroot m hpr y (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) (by
          calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).ze  -- (+756 chars; full line in the Zenodo source)
            _ = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring) with e | e <;> rw [e]
        · rw [tT1, tT2 m hpr a ht0 ht1, tT3 m hpr a ht0 ht1, tT4 m hpr a ht0 ht1, tT5 m hpr a ht0 ht1, tT6 m hpr a ht0 ht1, tO]
        · rw [tT6 m hpr a ht0 ht1, tO, tT1, tT2 m hpr a ht0 ht1, tT3 m hpr a ht0 ht1, tT4 m hpr a ht0 ht1, tT5 m hpr a ht0 ht1]
      · by_cases hb : x = ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))))
        · rw [hb] at hc ⊢
          rcases yroot m hpr y (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) (by
            calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1))  -- (+1580 chars; full line in the Zenodo source)
              _ = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring) with e | e <;> rw [e]
          · rw [tT5 m hpr a ht0 ht1, tT6 m hpr a ht0 ht1, tO, tT1, tT2 m hpr a ht0 ht1, tT3 m hpr a ht0 ht1, tT4 m hpr a ht0 ht1]
          · rw [tT2 m hpr a ht0 ht1, tT3 m hpr a ht0 ht1, tT4 m hpr a ht0 ht1, tT5 m hpr a ht0 ht1, tT6 m hpr a ht0 ht1, tO, tT1]
        · by_cases hcc : x = ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))
          · rw [hcc] at hc ⊢
            rcases yroot m hpr y ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m   -- (+185 chars; full line in the Zenodo source)
              calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1  -- (+1186 chars; full line in the Zenodo source)
                _ = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring) with e | e <;> rw [e]
            · rw [tT4 m hpr a ht0 ht1, tT5 m hpr a ht0 ht1, tT6 m hpr a ht0 ht1, tO, tT1, tT2 m hpr a ht0 ht1, tT3 m hpr a ht0 ht1]
            · rw [tT3 m hpr a ht0 ht1, tT4 m hpr a ht0 ht1, tT5 m hpr a ht0 ht1, tT6 m hpr a ht0 ht1, tO, tT1, tT2 m hpr a ht0 ht1]
          · exact chain7 m hpr hS a x y hc ht0 ht1 h0 hb hcc
    · rw [tauG_off m _ _ x y hc, tauG_off m _ _ x y hc, tauG_off m _ _ x y hc, tauG_off m _ _ x y hc, tauG_off m _ _ x y hc, tauG_off m _ _ x y hc, tauG_off m _ _ x y hc]

def onG (b c : Zp (2 * m + 1 + 1)) : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) → Int
  | none => 1
  | some p => if (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul p.2 p.2) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg c)) ((ring (2 * m + 1 + 1)).mul p.1 p.2))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b p.2)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul p.1 ((ring (2 *   -- (+136 chars; full line in the Zenodo source)

theorem onG_on (b c x y : Zp (2 * m + 1 + 1)) (hc : cB m b c x y) : onG m b c (some (x, y)) = 1 := by
  unfold cB at hc; show (if _ then _ else _) = _; rw [ite_pos' _ _ hc]

theorem onG_off (b c x y : Zp (2 * m + 1 + 1)) (hc : ¬ cB m b c x y) : onG m b c (some (x, y)) = 0 := by
  unfold cB at hc; show (if _ then _ else _) = _; rw [ite_neg' _ _ hc]

theorem onG_tau (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (b c : Zp (2 * m + 1 + 1)) (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) :
    onG m b c (tauG m b c P) = onG m b c P := by
  cases P with
  | none => exact onG_on m b c _ _ (by unfold cB; lib_ring)
  | some p =>
    obtain ⟨x, y⟩ := p
    by_cases hc : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg c)) ((ring (2 * m + 1 + 1)).mul x y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b y)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1))  -- (+103 chars; full line in the Zenodo source)
    · rw [onG_on m b c x y hc]
      by_cases h0 : x = (ring (2 * m + 1 + 1)).zero
      · rw [tauG_some, ite_pos' _ _ hc, ite_pos' _ _ h0]
        by_cases hy : y = (ring (2 * m + 1 + 1)).zero
        · rw [ite_pos' _ _ hy]; exact onG_on m b c _ _ (by unfold cB; lib_ring)
        · rw [ite_neg' _ _ hy]; rfl
      · rw [tauG_gen m b c x y hc h0]; exact onG_on m b c _ _ (on7 m hpr b c x y hc h0)
    · rw [tauG_off m b c x y hc]

theorem fst_ne (u v : Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) (h : u.1 ≠ v.1) : (some u : Option _) ≠ some v :=
  fun e => h (congrArg Prod.fst (Option.some.inj e))

theorem snd_ne (u v : Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) (h : u.2 ≠ v.2) : (some u : Option _) ≠ some v :=
  fun e => h (congrArg Prod.snd (Option.some.inj e))

theorem ne_of_sub (u v : Zp (2 * m + 1 + 1)) (h : (ring (2 * m + 1 + 1)).add u ((ring (2 * m + 1 + 1)).neg v) ≠ (ring (2 * m + 1 + 1)).zero) : u ≠ v := fun e => h (by rw [e]; lib_ring)

theorem onG_fix (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (hF : Fix7 m) (a : Zp (2 * m + 1 + 1)) (ht0 : a ≠ (ring (2 * m + 1 + 1)).zero) (ht1 : a ≠ (ring (2 * m + 1 + 1)).one)
    (h2 : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero) (hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (  -- (+628 chars; full line in the Zenodo source)
    (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) (h : tauG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) P = P) : onG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)  -- (+225 chars; full line in the Zenodo source)
  cases P with
  | none => exact absurd h (fun e => by cases e)
  | some p =>
    obtain ⟨x, y⟩ := p
    by_cases hc : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 *  -- (+546 chars; full line in the Zenodo source)
    · exfalso
      by_cases h0 : x = (ring (2 * m + 1 + 1)).zero
      · rw [tauG_some, ite_pos' _ _ hc, ite_pos' _ _ h0] at h
        by_cases hy : y = (ring (2 * m + 1 + 1)).zero
        · rw [ite_pos' _ _ hy, h0] at h
          exact fst_ne m _ _ (nzB m hpr a ht0 ht1) h
        · rw [ite_neg' _ _ hy] at h
          cases h
      · by_cases hb : x = ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))))
        · rw [hb] at hc h
          rcases yroot m hpr y (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) (by
            calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1))  -- (+1580 chars; full line in the Zenodo source)
              _ = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring) with e | e <;> rw [e] at h
          · rw [tT5 m hpr a ht0 ht1] at h
            exact fst_ne m _ _ (fun e' => nzB m hpr a ht0 ht1 e'.symm) h
          · rw [tT2 m hpr a ht0 ht1] at h
            exact fst_ne m _ _ (fun e' => nzBC m hpr a ht0 ht1 (by rw [show ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) = ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) from e']; lib_ring)) h
        · by_cases hcc : x = ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))
          · rw [hcc] at hc h
            rcases yroot m hpr y ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m   -- (+185 chars; full line in the Zenodo source)
              calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1  -- (+1186 chars; full line in the Zenodo source)
                _ = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring) with e | e <;> rw [e] at h
            · rw [tT4 m hpr a ht0 ht1] at h
              exact fst_ne m _ _ (fun e' => nzBC m hpr a ht0 ht1 (by rw [show ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) = ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) from e']; lib_ring)) h
            · rw [tT3 m hpr a ht0 ht1] at h
              exact snd_ne m _ _ (fun e' => nzBCC m hpr a ht0 ht1 (by rw [← show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((rin  -- (+250 chars; full line in the Zenodo source)
          · rw [tauG_gen m _ _ x y hc h0] at h
            have e' := Option.some.inj h
            have fx : tx7 m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) x y = x := by rw [← bg_eq m a, ← cg_eq m a]; exact congrArg Prod.fst e'
            have fy : ty7 m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) x y = y := by rw [← bg_eq m a, ← cg_eq m a]; exact congrArg Prod.snd e'
            have hcg : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) x y := by rw [← bg_eq m a, ← cg_eq m a]; exact hc
            exact hF a x y hcg h0 fx fy h2 ht0 ht1 hD
    · exact onG_off m _ _ x y hc

def cnt7 (a : Zp (2 * m + 1 + 1)) (q : Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) : Int :=
  if (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul q.2 q.2) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) ((ring (2 * m + 1 + 1)).mul q.1 q.2))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).  -- (+547 chars; full line in the Zenodo source)

/-- **7 | 1 + #E_t(𝔽_p)**, given the step identities and the fixed-point lemma. -/
theorem g7_of (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (hS : Steps7 m) (hF : Fix7 m) (a : Zp (2 * m + 1 + 1)) (ht0 : a ≠ (ring (2 * m + 1 + 1)).zero) (ht1 : a ≠ (ring (2 * m + 1 + 1)).one)
    (h2 : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero) (hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (  -- (+630 chars; full line in the Zenodo source)
    ∃ k : Int, 1 + sumK LibInt.intR (cnt7 m a) (pL m) = 7 * k := by
  have e : sumK LibInt.intR (onG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) (ptsL m) = 1 + sumK LibInt.intR (cnt7 m a) (pL m) := by
    show LibInt.intR.add 1 (sumK LibInt.intR (onG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) ((pL m).map some)) = _
    rw [sum_map]; rfl
  rw [← e]
  exact ⟨_, Orbit7.orbit7 (ptsL m) (complete_pts m) (code m) (code_inj m) (tauG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) (tau7 m hpr hS a ht0 ht1)
    (onG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) (onG_tau m hpr _ _) (onG_fix m hpr hF a ht0 ht1 h2 hD)⟩

end G7LF

#print axioms G7LF.tauG_some
#print axioms G7LF.tauG_gen
#print axioms G7LF.tauG_off
#print axioms G7LF.glue
#print axioms G7LF.bg_eq
#print axioms G7LF.cg_eq
#print axioms G7LF.chain7
#print axioms G7LF.nzB
#print axioms G7LF.nzC
#print axioms G7LF.nzBC
#print axioms G7LF.nzBCC
#print axioms G7LF.tO
#print axioms G7LF.tT1
#print axioms G7LF.tT2
#print axioms G7LF.tT3
#print axioms G7LF.tT4
#print axioms G7LF.tT5
#print axioms G7LF.tT6
#print axioms G7LF.yroot
#print axioms G7LF.tau7
#print axioms G7LF.onG_on
#print axioms G7LF.onG_off
#print axioms G7LF.onG_tau
#print axioms G7LF.fst_ne
#print axioms G7LF.snd_ne
#print axioms G7LF.ne_of_sub
#print axioms G7LF.onG_fix
#print axioms G7LF.g7_of

lang/G7Done.lean

lean_all/lang/G7Done.lean

/-
  lang/G7Done — T43, cycle 382: the fixed-point lemma of the order-7 family by small equations (replacing lang/G7Fix).

  At a generic fixed point τP = P also τ⁴P = τ⁵P = τ⁶P = P, and the linear-fractional closed forms give
  z_k: X_kn − x·X_kd = 0 (k = 1, 4, 5, 6). Then q1 = z1 − z6 and q2 = t(z4 − z6) − (z5 − z6) are quadratics in x alone, and
  A·q1 + B·q2 = −t⁸(t − 1)⁷(t³ − 8t² + 5t + 1) with |coefficients| ≤ 126 (std-lib Python elimination and interpolation).
    hk1, hk4, hk5, hk6   τᵏP in closed form at the generic points (from the step identities Steps7)
    fixg7                no fixed point at a generic point when t ≠ 0, 1 and t³ − 8t² + 5t + 1 ≠ 0
    onG_fixg7, g7t_of    7 | 1 + #E_t(𝔽_p), given only Steps7 (lang/G7Final instantiates it with G7S0 … G7S6)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G7Count

namespace G7LF

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Step G4Gen G4Map G7Tate1 Pairs Orbit5
open FiniteRecChain (ite_pos' ite_neg')

variable (m : Nat)

theorem hk1 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (hS : Steps7 m) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))  -- (+85 chars; full line in the Zenodo source)
    (h0 : x ≠ (ring (2 * m + 1 + 1)).zero) (hb : x ≠ (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) (hc0 : x ≠ (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) :
    tauG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) (some (x, y)) = some ((ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y)), (ring (2 * m + 1 + 1)).mul ((ring (2 * m  -- (+121 chars; full line in the Zenodo source)
  obtain ⟨s0x, s0l, s1x, s1l, s2x, s2l, s3x, s3l, s4x, s4l, s5x, s5l, s6x, s6l⟩ := hS
  have hcg : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) x y := by rw [← bg_eq m a, ← cg_eq m a]; exact hc
  have hbg : x ≠ (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) := by rw [← bg_eq m a]; exact hb
  have hcg0 : x ≠ (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a)) := by rw [← cg_eq m a]; exact hc0
  have n_L1d : L1d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_L1d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_X1d : X1d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X1d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_X1n : X1n m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X1n m hpr a x y ht0 ht1 h0 hbg hcg0
  have ix : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x h0
  have xj0 : (ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (iv m (x))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (iv m (x))) = (ring (2 * m + 1 + 1)).mul (iv m x) x := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := ix
  have xj1 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y))) ((ring (2 * m + 1 + 1)).mul (X1d m a x y) (iv m (X1n m a x y))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y))) ((ring (2 * m + 1 + 1)).mul (X1d m a x y) (iv m (X1n m a x y))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m (X1d m a x y)) (X1d m a x y)) ((ring (2 * m + 1 + 1)).mul (iv m (X1n m a x y)) (X1n m a x y)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [iv_mul_self m hpr _ n_X1d, iv_mul_self m hpr _ n_X1n]; lib_ring
  have nx1 : (ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y)) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ n_X1n (iv_nz m hpr _ n_X1d)
  have e0 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) x = y := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) x = (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul (iv m x) x) := by lib_ring
      _ = y := by rw [ix]; lib_ring
  have c0 : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) x) := by rw [e0]; exact hc
  have g0 := glue m hpr ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) (x) ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (iv m (  -- (+264 chars; full line in the Zenodo source)
  have key : tauG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) (some (x, (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) x)) = some ((ring (2 * m + 1 + 1)).  -- (+201 chars; full line in the Zenodo source)
    rw [g0.1]
  rw [e0] at key
  exact key

theorem hk4 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (hS : Steps7 m) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))  -- (+85 chars; full line in the Zenodo source)
    (h0 : x ≠ (ring (2 * m + 1 + 1)).zero) (hb : x ≠ (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) (hc0 : x ≠ (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) :
    tauG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) (tauG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 +  -- (+940 chars; full line in the Zenodo source)
  obtain ⟨s0x, s0l, s1x, s1l, s2x, s2l, s3x, s3l, s4x, s4l, s5x, s5l, s6x, s6l⟩ := hS
  have hcg : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) x y := by rw [← bg_eq m a, ← cg_eq m a]; exact hc
  have hbg : x ≠ (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) := by rw [← bg_eq m a]; exact hb
  have hcg0 : x ≠ (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a)) := by rw [← cg_eq m a]; exact hc0
  have n_L1d : L1d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_L1d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_L2d : L2d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_L2d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_L3d : L3d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_L3d m hpr a x y ht0 ht1 h0 hbg hcg0
  have n_L4d : L4d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_L4d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_X1d : X1d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X1d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_X1n : X1n m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X1n m hpr a x y ht0 ht1 h0 hbg hcg0
  have n_X2d : X2d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X2d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_X2n : X2n m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X2n m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_X3d : X3d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X3d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_X3n : X3n m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X3n m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_X4d : X4d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X4d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_X4n : X4n m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X4n m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have ix : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x h0
  have xj0 : (ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (iv m (x))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (iv m (x))) = (ring (2 * m + 1 + 1)).mul (iv m x) x := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := ix
  have xj1 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y))) ((ring (2 * m + 1 + 1)).mul (X1d m a x y) (iv m (X1n m a x y))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y))) ((ring (2 * m + 1 + 1)).mul (X1d m a x y) (iv m (X1n m a x y))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m (X1d m a x y)) (X1d m a x y)) ((ring (2 * m + 1 + 1)).mul (iv m (X1n m a x y)) (X1n m a x y)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [iv_mul_self m hpr _ n_X1d, iv_mul_self m hpr _ n_X1n]; lib_ring
  have nx1 : (ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y)) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ n_X1n (iv_nz m hpr _ n_X1d)
  have xj2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a x y))) ((ring (2 * m + 1 + 1)).mul (X2d m a x y) (iv m (X2n m a x y))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a x y))) ((ring (2 * m + 1 + 1)).mul (X2d m a x y) (iv m (X2n m a x y))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m (X2d m a x y)) (X2d m a x y)) ((ring (2 * m + 1 + 1)).mul (iv m (X2n m a x y)) (X2n m a x y)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [iv_mul_self m hpr _ n_X2d, iv_mul_self m hpr _ n_X2n]; lib_ring
  have nx2 : (ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a x y)) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ n_X2n (iv_nz m hpr _ n_X2d)
  have xj3 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X3n m a x y) (iv m (X3d m a x y))) ((ring (2 * m + 1 + 1)).mul (X3d m a x y) (iv m (X3n m a x y))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X3n m a x y) (iv m (X3d m a x y))) ((ring (2 * m + 1 + 1)).mul (X3d m a x y) (iv m (X3n m a x y))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m (X3d m a x y)) (X3d m a x y)) ((ring (2 * m + 1 + 1)).mul (iv m (X3n m a x y)) (X3n m a x y)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [iv_mul_self m hpr _ n_X3d, iv_mul_self m hpr _ n_X3n]; lib_ring
  have nx3 : (ring (2 * m + 1 + 1)).mul (X3n m a x y) (iv m (X3d m a x y)) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ n_X3n (iv_nz m hpr _ n_X3d)
  have xj4 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X4n m a x y) (iv m (X4d m a x y))) ((ring (2 * m + 1 + 1)).mul (X4d m a x y) (iv m (X4n m a x y))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X4n m a x y) (iv m (X4d m a x y))) ((ring (2 * m + 1 + 1)).mul (X4d m a x y) (iv m (X4n m a x y))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m (X4d m a x y)) (X4d m a x y)) ((ring (2 * m + 1 + 1)).mul (iv m (X4n m a x y)) (X4n m a x y)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [iv_mul_self m hpr _ n_X4d, iv_mul_self m hpr _ n_X4n]; lib_ring
  have nx4 : (ring (2 * m + 1 + 1)).mul (X4n m a x y) (iv m (X4d m a x y)) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ n_X4n (iv_nz m hpr _ n_X4d)
  have e0 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) x = y := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) x = (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul (iv m x) x) := by lib_ring
      _ = y := by rw [ix]; lib_ring
  have c0 : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) x) := by rw [e0]; exact hc
  have g0 := glue m hpr ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) (x) ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (iv m (  -- (+264 chars; full line in the Zenodo source)
  have g1 := glue m hpr ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y))) ((ring (2 * m + 1 + 1)).mul (L1n m a x y) (iv m   -- (+354 chars; full line in the Zenodo source)
  have g2 := glue m hpr ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a x y))) ((ring (2 * m + 1 + 1)).mul (L2n m a x y) (iv m   -- (+354 chars; full line in the Zenodo source)
  have g3 := glue m hpr ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (X3n m a x y) (iv m (X3d m a x y))) ((ring (2 * m + 1 + 1)).mul (L3n m a x y) (iv m   -- (+354 chars; full line in the Zenodo source)
  have key : tauG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) (tauG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2   -- (+1020 chars; full line in the Zenodo source)
    rw [g0.1, g1.1, g2.1, g3.1]
  rw [e0] at key
  exact key

theorem hk5 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (hS : Steps7 m) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))  -- (+85 chars; full line in the Zenodo source)
    (h0 : x ≠ (ring (2 * m + 1 + 1)).zero) (hb : x ≠ (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) (hc0 : x ≠ (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) :
    tauG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) (tauG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 +  -- (+1213 chars; full line in the Zenodo source)
  obtain ⟨s0x, s0l, s1x, s1l, s2x, s2l, s3x, s3l, s4x, s4l, s5x, s5l, s6x, s6l⟩ := hS
  have hcg : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) x y := by rw [← bg_eq m a, ← cg_eq m a]; exact hc
  have hbg : x ≠ (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) := by rw [← bg_eq m a]; exact hb
  have hcg0 : x ≠ (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a)) := by rw [← cg_eq m a]; exact hc0
  have n_L1d : L1d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_L1d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_L2d : L2d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_L2d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_L3d : L3d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_L3d m hpr a x y ht0 ht1 h0 hbg hcg0
  have n_L4d : L4d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_L4d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_L5d : L5d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_L5d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_X1d : X1d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X1d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_X1n : X1n m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X1n m hpr a x y ht0 ht1 h0 hbg hcg0
  have n_X2d : X2d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X2d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_X2n : X2n m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X2n m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_X3d : X3d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X3d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_X3n : X3n m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X3n m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_X4d : X4d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X4d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_X4n : X4n m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X4n m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_X5d : X5d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X5d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_X5n : X5n m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X5n m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have ix : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x h0
  have xj0 : (ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (iv m (x))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (iv m (x))) = (ring (2 * m + 1 + 1)).mul (iv m x) x := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := ix
  have xj1 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y))) ((ring (2 * m + 1 + 1)).mul (X1d m a x y) (iv m (X1n m a x y))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y))) ((ring (2 * m + 1 + 1)).mul (X1d m a x y) (iv m (X1n m a x y))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m (X1d m a x y)) (X1d m a x y)) ((ring (2 * m + 1 + 1)).mul (iv m (X1n m a x y)) (X1n m a x y)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [iv_mul_self m hpr _ n_X1d, iv_mul_self m hpr _ n_X1n]; lib_ring
  have nx1 : (ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y)) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ n_X1n (iv_nz m hpr _ n_X1d)
  have xj2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a x y))) ((ring (2 * m + 1 + 1)).mul (X2d m a x y) (iv m (X2n m a x y))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a x y))) ((ring (2 * m + 1 + 1)).mul (X2d m a x y) (iv m (X2n m a x y))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m (X2d m a x y)) (X2d m a x y)) ((ring (2 * m + 1 + 1)).mul (iv m (X2n m a x y)) (X2n m a x y)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [iv_mul_self m hpr _ n_X2d, iv_mul_self m hpr _ n_X2n]; lib_ring
  have nx2 : (ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a x y)) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ n_X2n (iv_nz m hpr _ n_X2d)
  have xj3 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X3n m a x y) (iv m (X3d m a x y))) ((ring (2 * m + 1 + 1)).mul (X3d m a x y) (iv m (X3n m a x y))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X3n m a x y) (iv m (X3d m a x y))) ((ring (2 * m + 1 + 1)).mul (X3d m a x y) (iv m (X3n m a x y))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m (X3d m a x y)) (X3d m a x y)) ((ring (2 * m + 1 + 1)).mul (iv m (X3n m a x y)) (X3n m a x y)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [iv_mul_self m hpr _ n_X3d, iv_mul_self m hpr _ n_X3n]; lib_ring
  have nx3 : (ring (2 * m + 1 + 1)).mul (X3n m a x y) (iv m (X3d m a x y)) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ n_X3n (iv_nz m hpr _ n_X3d)
  have xj4 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X4n m a x y) (iv m (X4d m a x y))) ((ring (2 * m + 1 + 1)).mul (X4d m a x y) (iv m (X4n m a x y))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X4n m a x y) (iv m (X4d m a x y))) ((ring (2 * m + 1 + 1)).mul (X4d m a x y) (iv m (X4n m a x y))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m (X4d m a x y)) (X4d m a x y)) ((ring (2 * m + 1 + 1)).mul (iv m (X4n m a x y)) (X4n m a x y)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [iv_mul_self m hpr _ n_X4d, iv_mul_self m hpr _ n_X4n]; lib_ring
  have nx4 : (ring (2 * m + 1 + 1)).mul (X4n m a x y) (iv m (X4d m a x y)) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ n_X4n (iv_nz m hpr _ n_X4d)
  have xj5 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X5n m a x y) (iv m (X5d m a x y))) ((ring (2 * m + 1 + 1)).mul (X5d m a x y) (iv m (X5n m a x y))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X5n m a x y) (iv m (X5d m a x y))) ((ring (2 * m + 1 + 1)).mul (X5d m a x y) (iv m (X5n m a x y))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m (X5d m a x y)) (X5d m a x y)) ((ring (2 * m + 1 + 1)).mul (iv m (X5n m a x y)) (X5n m a x y)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [iv_mul_self m hpr _ n_X5d, iv_mul_self m hpr _ n_X5n]; lib_ring
  have nx5 : (ring (2 * m + 1 + 1)).mul (X5n m a x y) (iv m (X5d m a x y)) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ n_X5n (iv_nz m hpr _ n_X5d)
  have e0 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) x = y := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) x = (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul (iv m x) x) := by lib_ring
      _ = y := by rw [ix]; lib_ring
  have c0 : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) x) := by rw [e0]; exact hc
  have g0 := glue m hpr ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) (x) ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (iv m (  -- (+264 chars; full line in the Zenodo source)
  have g1 := glue m hpr ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y))) ((ring (2 * m + 1 + 1)).mul (L1n m a x y) (iv m   -- (+354 chars; full line in the Zenodo source)
  have g2 := glue m hpr ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a x y))) ((ring (2 * m + 1 + 1)).mul (L2n m a x y) (iv m   -- (+354 chars; full line in the Zenodo source)
  have g3 := glue m hpr ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (X3n m a x y) (iv m (X3d m a x y))) ((ring (2 * m + 1 + 1)).mul (L3n m a x y) (iv m   -- (+354 chars; full line in the Zenodo source)
  have g4 := glue m hpr ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (X4n m a x y) (iv m (X4d m a x y))) ((ring (2 * m + 1 + 1)).mul (L4n m a x y) (iv m   -- (+348 chars; full line in the Zenodo source)
  have key : tauG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) (tauG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2   -- (+1293 chars; full line in the Zenodo source)
    rw [g0.1, g1.1, g2.1, g3.1, g4.1]
  rw [e0] at key
  exact key

theorem hk6 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (hS : Steps7 m) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))  -- (+85 chars; full line in the Zenodo source)
    (h0 : x ≠ (ring (2 * m + 1 + 1)).zero) (hb : x ≠ (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) (hc0 : x ≠ (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) :
    tauG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) (tauG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 +  -- (+1486 chars; full line in the Zenodo source)
  obtain ⟨s0x, s0l, s1x, s1l, s2x, s2l, s3x, s3l, s4x, s4l, s5x, s5l, s6x, s6l⟩ := hS
  have hcg : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) x y := by rw [← bg_eq m a, ← cg_eq m a]; exact hc
  have hbg : x ≠ (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) := by rw [← bg_eq m a]; exact hb
  have hcg0 : x ≠ (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a)) := by rw [← cg_eq m a]; exact hc0
  have n_L1d : L1d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_L1d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_L2d : L2d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_L2d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_L3d : L3d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_L3d m hpr a x y ht0 ht1 h0 hbg hcg0
  have n_L4d : L4d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_L4d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_L5d : L5d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_L5d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_L6d : L6d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_L6d m hpr a x y ht0 ht1 h0 hbg hcg0
  have n_X1d : X1d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X1d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_X1n : X1n m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X1n m hpr a x y ht0 ht1 h0 hbg hcg0
  have n_X2d : X2d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X2d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_X2n : X2n m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X2n m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_X3d : X3d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X3d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_X3n : X3n m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X3n m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_X4d : X4d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X4d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_X4n : X4n m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X4n m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_X5d : X5d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X5d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_X5n : X5n m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X5n m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_X6d : X6d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X6d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have n_X6n : X6n m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X6n m hpr a x y ht0 ht1 h0 hbg hcg0
  have ix : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x h0
  have xj0 : (ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (iv m (x))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (iv m (x))) = (ring (2 * m + 1 + 1)).mul (iv m x) x := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := ix
  have xj1 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y))) ((ring (2 * m + 1 + 1)).mul (X1d m a x y) (iv m (X1n m a x y))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y))) ((ring (2 * m + 1 + 1)).mul (X1d m a x y) (iv m (X1n m a x y))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m (X1d m a x y)) (X1d m a x y)) ((ring (2 * m + 1 + 1)).mul (iv m (X1n m a x y)) (X1n m a x y)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [iv_mul_self m hpr _ n_X1d, iv_mul_self m hpr _ n_X1n]; lib_ring
  have nx1 : (ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y)) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ n_X1n (iv_nz m hpr _ n_X1d)
  have xj2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a x y))) ((ring (2 * m + 1 + 1)).mul (X2d m a x y) (iv m (X2n m a x y))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a x y))) ((ring (2 * m + 1 + 1)).mul (X2d m a x y) (iv m (X2n m a x y))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m (X2d m a x y)) (X2d m a x y)) ((ring (2 * m + 1 + 1)).mul (iv m (X2n m a x y)) (X2n m a x y)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [iv_mul_self m hpr _ n_X2d, iv_mul_self m hpr _ n_X2n]; lib_ring
  have nx2 : (ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a x y)) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ n_X2n (iv_nz m hpr _ n_X2d)
  have xj3 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X3n m a x y) (iv m (X3d m a x y))) ((ring (2 * m + 1 + 1)).mul (X3d m a x y) (iv m (X3n m a x y))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X3n m a x y) (iv m (X3d m a x y))) ((ring (2 * m + 1 + 1)).mul (X3d m a x y) (iv m (X3n m a x y))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m (X3d m a x y)) (X3d m a x y)) ((ring (2 * m + 1 + 1)).mul (iv m (X3n m a x y)) (X3n m a x y)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [iv_mul_self m hpr _ n_X3d, iv_mul_self m hpr _ n_X3n]; lib_ring
  have nx3 : (ring (2 * m + 1 + 1)).mul (X3n m a x y) (iv m (X3d m a x y)) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ n_X3n (iv_nz m hpr _ n_X3d)
  have xj4 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X4n m a x y) (iv m (X4d m a x y))) ((ring (2 * m + 1 + 1)).mul (X4d m a x y) (iv m (X4n m a x y))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X4n m a x y) (iv m (X4d m a x y))) ((ring (2 * m + 1 + 1)).mul (X4d m a x y) (iv m (X4n m a x y))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m (X4d m a x y)) (X4d m a x y)) ((ring (2 * m + 1 + 1)).mul (iv m (X4n m a x y)) (X4n m a x y)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [iv_mul_self m hpr _ n_X4d, iv_mul_self m hpr _ n_X4n]; lib_ring
  have nx4 : (ring (2 * m + 1 + 1)).mul (X4n m a x y) (iv m (X4d m a x y)) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ n_X4n (iv_nz m hpr _ n_X4d)
  have xj5 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X5n m a x y) (iv m (X5d m a x y))) ((ring (2 * m + 1 + 1)).mul (X5d m a x y) (iv m (X5n m a x y))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X5n m a x y) (iv m (X5d m a x y))) ((ring (2 * m + 1 + 1)).mul (X5d m a x y) (iv m (X5n m a x y))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m (X5d m a x y)) (X5d m a x y)) ((ring (2 * m + 1 + 1)).mul (iv m (X5n m a x y)) (X5n m a x y)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [iv_mul_self m hpr _ n_X5d, iv_mul_self m hpr _ n_X5n]; lib_ring
  have nx5 : (ring (2 * m + 1 + 1)).mul (X5n m a x y) (iv m (X5d m a x y)) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ n_X5n (iv_nz m hpr _ n_X5d)
  have xj6 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X6n m a x y) (iv m (X6d m a x y))) ((ring (2 * m + 1 + 1)).mul (X6d m a x y) (iv m (X6n m a x y))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X6n m a x y) (iv m (X6d m a x y))) ((ring (2 * m + 1 + 1)).mul (X6d m a x y) (iv m (X6n m a x y))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m (X6d m a x y)) (X6d m a x y)) ((ring (2 * m + 1 + 1)).mul (iv m (X6n m a x y)) (X6n m a x y)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [iv_mul_self m hpr _ n_X6d, iv_mul_self m hpr _ n_X6n]; lib_ring
  have nx6 : (ring (2 * m + 1 + 1)).mul (X6n m a x y) (iv m (X6d m a x y)) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ n_X6n (iv_nz m hpr _ n_X6d)
  have e0 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) x = y := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) x = (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul (iv m x) x) := by lib_ring
      _ = y := by rw [ix]; lib_ring
  have c0 : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) x) := by rw [e0]; exact hc
  have g0 := glue m hpr ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) (x) ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (iv m (  -- (+264 chars; full line in the Zenodo source)
  have g1 := glue m hpr ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y))) ((ring (2 * m + 1 + 1)).mul (L1n m a x y) (iv m   -- (+354 chars; full line in the Zenodo source)
  have g2 := glue m hpr ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a x y))) ((ring (2 * m + 1 + 1)).mul (L2n m a x y) (iv m   -- (+354 chars; full line in the Zenodo source)
  have g3 := glue m hpr ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (X3n m a x y) (iv m (X3d m a x y))) ((ring (2 * m + 1 + 1)).mul (L3n m a x y) (iv m   -- (+354 chars; full line in the Zenodo source)
  have g4 := glue m hpr ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (X4n m a x y) (iv m (X4d m a x y))) ((ring (2 * m + 1 + 1)).mul (L4n m a x y) (iv m   -- (+348 chars; full line in the Zenodo source)
  have g5 := glue m hpr ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (X5n m a x y) (iv m (X5d m a x y))) ((ring (2 * m + 1 + 1)).mul (L5n m a x y) (iv m   -- (+354 chars; full line in the Zenodo source)
  have key : tauG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) (tauG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2   -- (+1566 chars; full line in the Zenodo source)
    rw [g0.1, g1.1, g2.1, g3.1, g4.1, g5.1]
  rw [e0] at key
  exact key

/-- a fixed point of τ at a generic point of E_t forces t⁸(t − 1)⁷(t³ − 8t² + 5t + 1) = 0. -/
theorem fixg7 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (hS : Steps7 m) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a  -- (+87 chars; full line in the Zenodo source)
    (h0 : x ≠ (ring (2 * m + 1 + 1)).zero) (hb : x ≠ (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) (hc0 : x ≠ (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) (hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m  -- (+832 chars; full line in the Zenodo source)
    (h : tauG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) (some (x, y)) = some (x, y)) : False := by
  have hcg : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) x y := by rw [← bg_eq m a, ← cg_eq m a]; exact hc
  have hbg : x ≠ (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) := by rw [← bg_eq m a]; exact hb
  have hcg0 : x ≠ (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a)) := by rw [← cg_eq m a]; exact hc0
  have f1 : (ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y)) = x := by
    have h' : tauG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) (some (x, y)) = some (x, y) := by rw [h]
    rw [hk1 m hpr hS a x y hc ht0 ht1 h0 hb hc0] at h'
    exact congrArg Prod.fst (Option.some.inj h')
  have nd1 : X1d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X1d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have ew1 : (ring (2 * m + 1 + 1)).mul (iv m (X1d m a x y)) (X1d m a x y) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ nd1
  have z1 : (ring (2 * m + 1 + 1)).add (X1n m a x y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x (X1d m a x y))) = (ring (2 * m + 1 + 1)).zero := by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y))) ((ring (2 * m + 1 + 1)).neg x)) (X1d m a x y)) ((ring (2 * m + 1 + 1)).mul (X1n m a x y) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (iv m (X1d m a x y)) (X1d m a x y))))) :  -- (+13 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [f1, ew1]; lib_ring
  have f4 : (ring (2 * m + 1 + 1)).mul (X4n m a x y) (iv m (X4d m a x y)) = x := by
    have h' : tauG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) (tauG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2  -- (+753 chars; full line in the Zenodo source)
    rw [hk4 m hpr hS a x y hc ht0 ht1 h0 hb hc0] at h'
    exact congrArg Prod.fst (Option.some.inj h')
  have nd4 : X4d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X4d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have ew4 : (ring (2 * m + 1 + 1)).mul (iv m (X4d m a x y)) (X4d m a x y) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ nd4
  have z4 : (ring (2 * m + 1 + 1)).add (X4n m a x y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x (X4d m a x y))) = (ring (2 * m + 1 + 1)).zero := by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (X4n m a x y) (iv m (X4d m a x y))) ((ring (2 * m + 1 + 1)).neg x)) (X4d m a x y)) ((ring (2 * m + 1 + 1)).mul (X4n m a x y) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (iv m (X4d m a x y)) (X4d m a x y))))) :  -- (+13 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [f4, ew4]; lib_ring
  have f5 : (ring (2 * m + 1 + 1)).mul (X5n m a x y) (iv m (X5d m a x y)) = x := by
    have h' : tauG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) (tauG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2  -- (+1029 chars; full line in the Zenodo source)
    rw [hk5 m hpr hS a x y hc ht0 ht1 h0 hb hc0] at h'
    exact congrArg Prod.fst (Option.some.inj h')
  have nd5 : X5d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X5d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have ew5 : (ring (2 * m + 1 + 1)).mul (iv m (X5d m a x y)) (X5d m a x y) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ nd5
  have z5 : (ring (2 * m + 1 + 1)).add (X5n m a x y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x (X5d m a x y))) = (ring (2 * m + 1 + 1)).zero := by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (X5n m a x y) (iv m (X5d m a x y))) ((ring (2 * m + 1 + 1)).neg x)) (X5d m a x y)) ((ring (2 * m + 1 + 1)).mul (X5n m a x y) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (iv m (X5d m a x y)) (X5d m a x y))))) :  -- (+13 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [f5, ew5]; lib_ring
  have f6 : (ring (2 * m + 1 + 1)).mul (X6n m a x y) (iv m (X6d m a x y)) = x := by
    have h' : tauG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) (tauG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2  -- (+1305 chars; full line in the Zenodo source)
    rw [hk6 m hpr hS a x y hc ht0 ht1 h0 hb hc0] at h'
    exact congrArg Prod.fst (Option.some.inj h')
  have nd6 : X6d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X6d m hpr a x y hcg ht0 ht1 h0 hbg hcg0
  have ew6 : (ring (2 * m + 1 + 1)).mul (iv m (X6d m a x y)) (X6d m a x y) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ nd6
  have z6 : (ring (2 * m + 1 + 1)).add (X6n m a x y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x (X6d m a x y))) = (ring (2 * m + 1 + 1)).zero := by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (X6n m a x y) (iv m (X6d m a x y))) ((ring (2 * m + 1 + 1)).neg x)) (X6d m a x y)) ((ring (2 * m + 1 + 1)).mul (X6n m a x y) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (iv m (X6d m a x y)) (X6d m a x y))))) :  -- (+13 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [f6, ew6]; lib_ring
  have hq1 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) (a)))) ((ring (2 * m + 1 + 1)).mul ((ri  -- (+1462 chars; full line in the Zenodo source)
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (X1n m a x y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x (X1d m a x y)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (X6n m a x y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x (X6d m a x y))))) := by unfold X1n X1d X6n X6d; lib_ring
      _ = (ring (2 * m + 1 + 1)).zero := by rw [z1, z6]; lib_ring
  have hq2 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring   -- (+2260 chars; full line in the Zenodo source)
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (X4n m a x y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x (X4d m a x y)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (X6n m a x y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x (X6d m a x y))))))) ((ring (2 * m + 1 + 1)).neg ((ring (2  -- (+330 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [z4, z5, z6]; lib_ring
  have ht1' := G7LF.sub_ne m a (ring (2 * m + 1 + 1)).one ht1
  have nc : (ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ ht0 ht1'
  have ec : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))))) ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ nc
  generalize iv m ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) = ci at ec
  obtain ⟨w, hw⟩ : ∃ w : Zp (2 * m + 1 + 1), w = (ring (2 * m + 1 + 1)).mul x ci := ⟨_, rfl⟩
  have hxw : x = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) w := by
    rw [hw]
    calc x = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul ci ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))))) := by rw [ec]; lib_ring
      _ = _ := by lib_ring
  rw [hxw] at hq1 hq2
  have k1 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg   -- (+644 chars; full line in the Zenodo source)
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) (a)))) ((ring (2 * m + 1 + 1)).mul ((ri  -- (+1894 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := hq1
  have k2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((  -- (+615 chars; full line in the Zenodo source)
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring   -- (+2692 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := hq2
  have hQ1 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m +   -- (+339 chars; full line in the Zenodo source)
    rcases mul_eq_zero m hpr _ _ k1 with e | e
    · exact absurd e (mul_ne m hpr _ _ nc nc)
    · exact e
  have hQ2 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2   -- (+194 chars; full line in the Zenodo source)
    rcases mul_eq_zero m hpr _ _ k2 with e | e
    · exact absurd e (mul_ne m hpr _ _ ht1' (mul_ne m hpr _ _ nc nc))
    · exact e
  have key : (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))))))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).m  -- (+799 chars; full line in the Zenodo source)
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m +  -- (+6662 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [hQ1, hQ2]; lib_ring
  exact neg_ne m _ (mul_ne m hpr _ _ (mul_ne m hpr _ _ ht0 (mul_ne m hpr _ _ ht0 (mul_ne m hpr _ _ ht0 (mul_ne m hpr _ _ ht0 ht1')))) hD) key

theorem onG_fixg7 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (hS : Steps7 m) (a : Zp (2 * m + 1 + 1)) (ht0 : a ≠ (ring (2 * m + 1 + 1)).zero) (ht1 : a ≠ (ring (2 * m + 1 + 1)).one)
    (hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m +  -- (+510 chars; full line in the Zenodo source)
    (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) (h : tauG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) P = P) : onG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)  -- (+225 chars; full line in the Zenodo source)
  cases P with
  | none => exact absurd h (fun e => by cases e)
  | some p =>
    obtain ⟨x, y⟩ := p
    by_cases hc : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 *  -- (+546 chars; full line in the Zenodo source)
    · exfalso
      by_cases h0 : x = (ring (2 * m + 1 + 1)).zero
      · rw [tauG_some, ite_pos' _ _ hc, ite_pos' _ _ h0] at h
        by_cases hy : y = (ring (2 * m + 1 + 1)).zero
        · rw [ite_pos' _ _ hy, h0] at h
          exact fst_ne m _ _ (nzB m hpr a ht0 ht1) h
        · rw [ite_neg' _ _ hy] at h
          cases h
      · by_cases hb : x = ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))))
        · rw [hb] at hc h
          rcases yroot m hpr y (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) (by
            calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1))  -- (+1580 chars; full line in the Zenodo source)
              _ = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring) with e | e <;> rw [e] at h
          · rw [tT5 m hpr a ht0 ht1] at h
            exact fst_ne m _ _ (fun e' => nzB m hpr a ht0 ht1 e'.symm) h
          · rw [tT2 m hpr a ht0 ht1] at h
            exact fst_ne m _ _ (fun e' => nzBC m hpr a ht0 ht1 (by rw [show ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) = ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) from e']; lib_ring)) h
        · by_cases hcc : x = ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))
          · rw [hcc] at hc h
            rcases yroot m hpr y ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m   -- (+185 chars; full line in the Zenodo source)
              calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1  -- (+1186 chars; full line in the Zenodo source)
                _ = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring) with e | e <;> rw [e] at h
            · rw [tT4 m hpr a ht0 ht1] at h
              exact fst_ne m _ _ (fun e' => nzBC m hpr a ht0 ht1 (by rw [show ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) = ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) from e']; lib_ring)) h
            · rw [tT3 m hpr a ht0 ht1] at h
              exact snd_ne m _ _ (fun e' => nzBCC m hpr a ht0 ht1 (by rw [← show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((rin  -- (+250 chars; full line in the Zenodo source)
          · exact fixg7 m hpr hS a x y hc ht0 ht1 h0 hb hcc hD h
    · exact onG_off m _ _ x y hc

theorem g7t_of (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (hS : Steps7 m) (a : Zp (2 * m + 1 + 1)) (ht0 : a ≠ (ring (2 * m + 1 + 1)).zero) (ht1 : a ≠ (ring (2 * m + 1 + 1)).one)
    (hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m +  -- (+512 chars; full line in the Zenodo source)
    ∃ k : Int, 1 + sumK LibInt.intR (cnt7 m a) (pL m) = 7 * k := by
  have e : sumK LibInt.intR (onG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) (ptsL m) = 1 + sumK LibInt.intR (cnt7 m a) (pL m) := by
    show LibInt.intR.add 1 (sumK LibInt.intR (onG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) ((pL m).map some)) = _
    rw [sum_map]; rfl
  rw [← e]
  exact ⟨_, Orbit7.orbit7 (ptsL m) (complete_pts m) (code m) (code_inj m) (tauG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) (tau7 m hpr hS a ht0 ht1)
    (onG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) (onG_tau m hpr _ _) (onG_fixg7 m hpr hS a ht0 ht1 hD)⟩

end G7LF
#print axioms G7LF.hk1
#print axioms G7LF.hk4
#print axioms G7LF.hk5
#print axioms G7LF.hk6
#print axioms G7LF.fixg7
#print axioms G7LF.onG_fixg7
#print axioms G7LF.g7t_of

lang/G7Final.lean

lean_all/lang/G7Final.lean

/-
  lang/G7Final — T43, cycle 388: G5 order 7 — 7 | #E_t(𝔽_p) for the Tate normal form with a point of order 7.

    steps7     the fourteen step identities (lang/G7S0, G7R1 … G7R5, G7S6) as Steps7
    g7         for E_t: y² + (1 − c)xy − by = x³ − bx², b = t³ − t², c = t² − t, with t ≠ 0, 1 and
               t³ − 8t² + 5t + 1 ≠ 0 (E_t nonsingular mod p):  1 + #{(x, y) ∈ 𝔽_p² on E_t} = 7k
  Steps 1–5 via the rescaled cores lang/G7C*, G7R* (all on the Mac); the fixed-point lemma is lang/G7Done.
  Lean 4 core only, no axioms.
-/
import lang.G7Done
import lang.G7S0
import lang.G7R1
import lang.G7R2
import lang.G7R3
import lang.G7R4
import lang.G7R5
import lang.G7S6

namespace G7LF

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Step G4Gen G4Map G7Tate1 Pairs

variable (m : Nat)

theorem steps7 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : Steps7 m :=
  ⟨step0x m hpr, step0l m hpr, step1x m hpr, step1l m hpr, step2x m hpr, step2l m hpr, step3x m hpr, step3l m hpr, step4x m hpr, step4l m hpr, step5x m hpr, step5l m hpr, step6x m hpr, step6l m hpr⟩

/-- **G5, order 7: 7 | #E_t(𝔽_p).** -/
theorem g7 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a : Zp (2 * m + 1 + 1)) (ht0 : a ≠ (ring (2 * m + 1 + 1)).zero) (ht1 : a ≠ (ring (2 * m + 1 + 1)).one)
    (hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m +  -- (+512 chars; full line in the Zenodo source)
    ∃ k : Int, 1 + sumK LibInt.intR (cnt7 m a) (pL m) = 7 * k :=
  g7t_of m hpr (steps7 m hpr) a ht0 ht1 hD

end G7LF

#print axioms G7LF.steps7
#print axioms G7LF.g7

lang/G7Gen.lean

lean_all/lang/G7Gen.lean

/-
  lang/G7Gen — T43, cycle 371: the generic points of E_t (G5 order 7, part 5).

    sub_ne       x ≠ r ⟹ x − r ≠ 0
    nz_Xkn, nz_Xkd, nz_Lkn, nz_Lkd
                 for (x, y) on E_t with t ≠ 0, 1 and x ∉ {0, b, c} (b = t³ − t², c = t² − t, the x-coordinates of the
                 torsion points), every linear form of lang/G7LF is nonzero: on the curve each one reduces to
                 ±tᵖ(t − 1)^q·(x − r₁)(x − r₂)(x − r₃) with rᵢ ∈ {0, b, c} (ℓ_y²·C = res + ℓ·(ℓ − 2L + ℓ_y(1 − c)x − ℓ_y b)).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G7LF

namespace G7LF

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Step G4Gen G4Map G7Tate1

variable (m : Nat)

theorem sub_ne (x r : Zp (2 * m + 1 + 1)) (h : x ≠ r) : (ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg r) ≠ (ring (2 * m + 1 + 1)).zero := fun e => h (by
  calc x = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg r)) r := by lib_ring
    _ = r := by rw [e]; lib_ring)

theorem nz_L1n (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (ht0 : a ≠ (ring (2 * m + 1 + 1)).zero) (ht1 : a ≠ (ring (2 * m + 1 + 1)).one) (_h0 : x ≠ (ring (2 * m + 1 + 1)).zero) (_hb : x ≠ (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)  -- (+118 chars; full line in the Zenodo source)
    L1n m a x y ≠ (ring (2 * m + 1 + 1)).zero := by
  have ht1' := sub_ne m a (ring (2 * m + 1 + 1)).one ht1
  unfold L1n
  intro e
  have key : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))))) = (ring (2 * m + 1 + 1)).zero := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))))) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1  -- (+732 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := e
  exact mul_ne m hpr _ _ (mul_ne m hpr _ _ ((mul_ne m hpr _ _ (ht0) ht0)) ht1') (sub_ne m x _ hc0) key

theorem nz_L1d (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) x y) (_ht0   -- (+398 chars; full line in the Zenodo source)
    L1d m a x y ≠ (ring (2 * m + 1 + 1)).zero := by
  unfold L1d
  intro e
  unfold cB at hc
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m   -- (+655 chars; full line in the Zenodo source)
  have key : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero)))) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))  -- (+271 chars; full line in the Zenodo source)
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero)))) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((r  -- (+2972 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [gcv, e]; lib_ring
  exact (mul_ne m hpr _ _ (mul_ne m hpr _ _ (mul_ne m hpr _ _ (neg_ne m _ (one_ne m)) (sub_ne m x _ h0)) (sub_ne m x _ hb)) (sub_ne m x _ hc0)) key

theorem nz_L2n (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) x y) (ht0 :  -- (+396 chars; full line in the Zenodo source)
    L2n m a x y ≠ (ring (2 * m + 1 + 1)).zero := by
  have ht1' := sub_ne m a (ring (2 * m + 1 + 1)).one ht1
  unfold L2n
  intro e
  unfold cB at hc
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m   -- (+655 chars; full line in the Zenodo source)
  have key : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))))  -- (+519 chars; full line in the Zenodo source)
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))))) ((  -- (+5756 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [gcv, e]; lib_ring
  exact (mul_ne m hpr _ _ (mul_ne m hpr _ _ (mul_ne m hpr _ _ (neg_ne m _ (mul_ne m hpr _ _ ((mul_ne m hpr _ _ ((mul_ne m hpr _ _ (ht0) ht0)) ht1')) ht1')) (sub_ne m x _ h0)) (sub_ne m x _ hb)) (sub_ne m x _ hc0)) key

theorem nz_L2d (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) x y) (_ht0   -- (+399 chars; full line in the Zenodo source)
    L2d m a x y ≠ (ring (2 * m + 1 + 1)).zero := by
  unfold L2d
  intro e
  unfold cB at hc
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m   -- (+655 chars; full line in the Zenodo source)
  have key : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)))))) ((ring (2 * m + 1   -- (+406 chars; full line in the Zenodo source)
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)))))) ((ring (2 * m + 1 + 1)  -- (+3503 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [gcv, e]; lib_ring
  exact (mul_ne m hpr _ _ (mul_ne m hpr _ _ (mul_ne m hpr _ _ (neg_ne m _ (one_ne m)) (sub_ne m x _ hb)) (sub_ne m x _ hb)) (sub_ne m x _ hc0)) key

theorem nz_L3n (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (_ht0 : a ≠ (ring (2 * m + 1 + 1)).zero) (ht1 : a ≠ (ring (2 * m + 1 + 1)).one) (_h0 : x ≠ (ring (2 * m + 1 + 1)).zero) (hb : x ≠ (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)  -- (+119 chars; full line in the Zenodo source)
    L3n m a x y ≠ (ring (2 * m + 1 + 1)).zero := by
  have ht1' := sub_ne m a (ring (2 * m + 1 + 1)).one ht1
  unfold L3n
  intro e
  have key : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))))) = (ring (2 * m + 1 + 1)).zero := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))))) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1  -- (+559 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := e
  exact mul_ne m hpr _ _ ht1' (sub_ne m x _ hb) key

theorem nz_L3d (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (_ht0 : a ≠ (ring (2 * m + 1 + 1)).zero) (_ht1 : a ≠ (ring (2 * m + 1 + 1)).one) (_h0 : x ≠ (ring (2 * m + 1 + 1)).zero) (_hb : x ≠ (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (  -- (+120 chars; full line in the Zenodo source)
    L3d m a x y ≠ (ring (2 * m + 1 + 1)).zero := by
  unfold L3d
  intro e
  have key : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))))) = (ring (2 * m + 1 + 1)).zero := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))))) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) (x) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).zero := e
  exact mul_ne m hpr _ _ (one_ne m) (sub_ne m x _ hc0) key

theorem nz_L4n (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) x y) (ht0 :  -- (+396 chars; full line in the Zenodo source)
    L4n m a x y ≠ (ring (2 * m + 1 + 1)).zero := by
  have ht1' := sub_ne m a (ring (2 * m + 1 + 1)).one ht1
  unfold L4n
  intro e
  unfold cB at hc
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m   -- (+655 chars; full line in the Zenodo source)
  have key : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))))  -- (+519 chars; full line in the Zenodo source)
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))))) ((  -- (+4848 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [gcv, e]; lib_ring
  exact (mul_ne m hpr _ _ (mul_ne m hpr _ _ (mul_ne m hpr _ _ (neg_ne m _ (mul_ne m hpr _ _ ((mul_ne m hpr _ _ ((mul_ne m hpr _ _ (ht0) ht0)) ht1')) ht1')) (sub_ne m x _ h0)) (sub_ne m x _ hb)) (sub_ne m x _ hc0)) key

theorem nz_L4d (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) x y) (_ht0   -- (+399 chars; full line in the Zenodo source)
    L4d m a x y ≠ (ring (2 * m + 1 + 1)).zero := by
  unfold L4d
  intro e
  unfold cB at hc
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m   -- (+655 chars; full line in the Zenodo source)
  have key : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)))))) ((ring (2 * m + 1   -- (+406 chars; full line in the Zenodo source)
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)))))) ((ring (2 * m + 1 + 1)  -- (+4695 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [gcv, e]; lib_ring
  exact (mul_ne m hpr _ _ (mul_ne m hpr _ _ (mul_ne m hpr _ _ (neg_ne m _ (one_ne m)) (sub_ne m x _ hb)) (sub_ne m x _ hb)) (sub_ne m x _ hc0)) key

theorem nz_L5n (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (ht0 : a ≠ (ring (2 * m + 1 + 1)).zero) (ht1 : a ≠ (ring (2 * m + 1 + 1)).one) (_h0 : x ≠ (ring (2 * m + 1 + 1)).zero) (_hb : x ≠ (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)  -- (+118 chars; full line in the Zenodo source)
    L5n m a x y ≠ (ring (2 * m + 1 + 1)).zero := by
  have ht1' := sub_ne m a (ring (2 * m + 1 + 1)).one ht1
  unfold L5n
  intro e
  have key : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))))) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))))) = (ring (2 *   -- (+22 chars; full line in the Zenodo source)
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))))) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))))) = (ring (2 * m +   -- (+732 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := e
  exact mul_ne m hpr _ _ (neg_ne m _ (mul_ne m hpr _ _ ((mul_ne m hpr _ _ (ht0) ht0)) ht1')) (sub_ne m x _ hc0) key

theorem nz_L5d (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) x y) (_ht0   -- (+398 chars; full line in the Zenodo source)
    L5d m a x y ≠ (ring (2 * m + 1 + 1)).zero := by
  unfold L5d
  intro e
  unfold cB at hc
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m   -- (+655 chars; full line in the Zenodo source)
  have key : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero)))) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))  -- (+271 chars; full line in the Zenodo source)
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero)))) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((r  -- (+2708 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [gcv, e]; lib_ring
  exact (mul_ne m hpr _ _ (mul_ne m hpr _ _ (mul_ne m hpr _ _ (neg_ne m _ (one_ne m)) (sub_ne m x _ h0)) (sub_ne m x _ hb)) (sub_ne m x _ hc0)) key

theorem nz_L6n (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) x y) (_ht0   -- (+399 chars; full line in the Zenodo source)
    L6n m a x y ≠ (ring (2 * m + 1 + 1)).zero := by
  unfold L6n
  intro e
  unfold cB at hc
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m   -- (+655 chars; full line in the Zenodo source)
  have key : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero)))) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero)))) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg   -- (+202 chars; full line in the Zenodo source)
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero)))) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero)))) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ri  -- (+4053 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [gcv, e]; lib_ring
  exact (mul_ne m hpr _ _ (mul_ne m hpr _ _ (mul_ne m hpr _ _ (neg_ne m _ (one_ne m)) (sub_ne m x _ h0)) (sub_ne m x _ h0)) (sub_ne m x _ hb)) key

theorem nz_L6d (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (_ht0 : a ≠ (ring (2 * m + 1 + 1)).zero) (_ht1 : a ≠ (ring (2 * m + 1 + 1)).one) (h0 : x ≠ (ring (2 * m + 1 + 1)).zero) (_hb : x ≠ (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a  -- (+120 chars; full line in the Zenodo source)
    L6d m a x y ≠ (ring (2 * m + 1 + 1)).zero := by
  unfold L6d
  intro e
  have key : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero))) = (ring (2 * m + 1 + 1)).zero := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero))) = x := by lib_ring
      _ = (ring (2 * m + 1 + 1)).zero := e
  exact mul_ne m hpr _ _ (one_ne m) (sub_ne m x _ h0) key

theorem nz_X1n (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (ht0 : a ≠ (ring (2 * m + 1 + 1)).zero) (ht1 : a ≠ (ring (2 * m + 1 + 1)).one) (_h0 : x ≠ (ring (2 * m + 1 + 1)).zero) (hb : x ≠ (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a))  -- (+118 chars; full line in the Zenodo source)
    X1n m a x y ≠ (ring (2 * m + 1 + 1)).zero := by
  have ht1' := sub_ne m a (ring (2 * m + 1 + 1)).one ht1
  unfold X1n
  intro e
  have key : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1  -- (+59 chars; full line in the Zenodo source)
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).m  -- (+897 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := e
  exact mul_ne m hpr _ _ (mul_ne m hpr _ _ ((mul_ne m hpr _ _ (ht0) ht0)) ht1') (sub_ne m x _ hb) key

theorem nz_X1d (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) x y) (_ht0   -- (+399 chars; full line in the Zenodo source)
    X1d m a x y ≠ (ring (2 * m + 1 + 1)).zero := by
  unfold X1d
  intro e
  unfold cB at hc
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m   -- (+655 chars; full line in the Zenodo source)
  have key : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero)))) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero)))) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg   -- (+202 chars; full line in the Zenodo source)
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero)))) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero)))) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ri  -- (+3647 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [gcv, e]; lib_ring
  exact (mul_ne m hpr _ _ (mul_ne m hpr _ _ (mul_ne m hpr _ _ (neg_ne m _ (one_ne m)) (sub_ne m x _ h0)) (sub_ne m x _ h0)) (sub_ne m x _ hb)) key

theorem nz_X2n (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) x y) (ht0 :  -- (+397 chars; full line in the Zenodo source)
    X2n m a x y ≠ (ring (2 * m + 1 + 1)).zero := by
  have ht1' := sub_ne m a (ring (2 * m + 1 + 1)).one ht1
  unfold X2n
  intro e
  unfold cB at hc
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m   -- (+655 chars; full line in the Zenodo source)
  have key : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) (a)) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).a  -- (+519 chars; full line in the Zenodo source)
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) (a)) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add (  -- (+8972 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [gcv, e]; lib_ring
  exact (mul_ne m hpr _ _ (mul_ne m hpr _ _ (mul_ne m hpr _ _ (neg_ne m _ (mul_ne m hpr _ _ ((mul_ne m hpr _ _ ((mul_ne m hpr _ _ ((mul_ne m hpr _ _ ((mul_ne m hpr _ _ (ht0) ht0)) ht0)) ht0)) ht1')) ht1')) (sub_ne m x _ h0)) (sub_ne m x _ hc0)) (sub_ne m x _ hc0)) key

theorem nz_X2d (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) x y) (_ht0   -- (+399 chars; full line in the Zenodo source)
    X2d m a x y ≠ (ring (2 * m + 1 + 1)).zero := by
  unfold X2d
  intro e
  unfold cB at hc
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m   -- (+655 chars; full line in the Zenodo source)
  have key : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)))))) ((ring (2 * m + 1   -- (+406 chars; full line in the Zenodo source)
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)))))) ((ring (2 * m + 1 + 1)  -- (+3503 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [gcv, e]; lib_ring
  exact (mul_ne m hpr _ _ (mul_ne m hpr _ _ (mul_ne m hpr _ _ (neg_ne m _ (one_ne m)) (sub_ne m x _ hb)) (sub_ne m x _ hb)) (sub_ne m x _ hc0)) key

theorem nz_X3n (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) x y) (ht0 :  -- (+396 chars; full line in the Zenodo source)
    X3n m a x y ≠ (ring (2 * m + 1 + 1)).zero := by
  have ht1' := sub_ne m a (ring (2 * m + 1 + 1)).one ht1
  unfold X3n
  intro e
  unfold cB at hc
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m   -- (+655 chars; full line in the Zenodo source)
  have key : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))))  -- (+519 chars; full line in the Zenodo source)
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))))) ((  -- (+5756 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [gcv, e]; lib_ring
  exact (mul_ne m hpr _ _ (mul_ne m hpr _ _ (mul_ne m hpr _ _ (neg_ne m _ (mul_ne m hpr _ _ ((mul_ne m hpr _ _ ((mul_ne m hpr _ _ (ht0) ht0)) ht1')) ht1')) (sub_ne m x _ h0)) (sub_ne m x _ hb)) (sub_ne m x _ hc0)) key

theorem nz_X3d (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) x y) (_ht0   -- (+399 chars; full line in the Zenodo source)
    X3d m a x y ≠ (ring (2 * m + 1 + 1)).zero := by
  unfold X3d
  intro e
  unfold cB at hc
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m   -- (+655 chars; full line in the Zenodo source)
  have key : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero)))) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a)   -- (+205 chars; full line in the Zenodo source)
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero)))) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))  -- (+3866 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [gcv, e]; lib_ring
  exact (mul_ne m hpr _ _ (mul_ne m hpr _ _ (mul_ne m hpr _ _ (neg_ne m _ (one_ne m)) (sub_ne m x _ h0)) (sub_ne m x _ hc0)) (sub_ne m x _ hc0)) key

theorem nz_X4n (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) x y) (ht0 :  -- (+396 chars; full line in the Zenodo source)
    X4n m a x y ≠ (ring (2 * m + 1 + 1)).zero := by
  have ht1' := sub_ne m a (ring (2 * m + 1 + 1)).one ht1
  unfold X4n
  intro e
  unfold cB at hc
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m   -- (+655 chars; full line in the Zenodo source)
  have key : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))))  -- (+519 chars; full line in the Zenodo source)
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))))) ((  -- (+4848 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [gcv, e]; lib_ring
  exact (mul_ne m hpr _ _ (mul_ne m hpr _ _ (mul_ne m hpr _ _ (neg_ne m _ (mul_ne m hpr _ _ ((mul_ne m hpr _ _ ((mul_ne m hpr _ _ (ht0) ht0)) ht1')) ht1')) (sub_ne m x _ h0)) (sub_ne m x _ hb)) (sub_ne m x _ hc0)) key

theorem nz_X4d (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) x y) (_ht0   -- (+399 chars; full line in the Zenodo source)
    X4d m a x y ≠ (ring (2 * m + 1 + 1)).zero := by
  unfold X4d
  intro e
  unfold cB at hc
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m   -- (+655 chars; full line in the Zenodo source)
  have key : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero)))) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a)   -- (+205 chars; full line in the Zenodo source)
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero)))) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))  -- (+2610 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [gcv, e]; lib_ring
  exact (mul_ne m hpr _ _ (mul_ne m hpr _ _ (mul_ne m hpr _ _ (neg_ne m _ (one_ne m)) (sub_ne m x _ h0)) (sub_ne m x _ hc0)) (sub_ne m x _ hc0)) key

theorem nz_X5n (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) x y) (ht0 :  -- (+397 chars; full line in the Zenodo source)
    X5n m a x y ≠ (ring (2 * m + 1 + 1)).zero := by
  have ht1' := sub_ne m a (ring (2 * m + 1 + 1)).one ht1
  unfold X5n
  intro e
  unfold cB at hc
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m   -- (+655 chars; full line in the Zenodo source)
  have key : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) (a)) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).a  -- (+519 chars; full line in the Zenodo source)
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) (a)) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add (  -- (+5244 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [gcv, e]; lib_ring
  exact (mul_ne m hpr _ _ (mul_ne m hpr _ _ (mul_ne m hpr _ _ (neg_ne m _ (mul_ne m hpr _ _ ((mul_ne m hpr _ _ ((mul_ne m hpr _ _ ((mul_ne m hpr _ _ ((mul_ne m hpr _ _ (ht0) ht0)) ht0)) ht0)) ht1')) ht1')) (sub_ne m x _ h0)) (sub_ne m x _ hc0)) (sub_ne m x _ hc0)) key

theorem nz_X5d (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) x y) (_ht0   -- (+399 chars; full line in the Zenodo source)
    X5d m a x y ≠ (ring (2 * m + 1 + 1)).zero := by
  unfold X5d
  intro e
  unfold cB at hc
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m   -- (+655 chars; full line in the Zenodo source)
  have key : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)))))) ((ring (2 * m + 1   -- (+406 chars; full line in the Zenodo source)
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)))))) ((ring (2 * m + 1 + 1)  -- (+4695 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [gcv, e]; lib_ring
  exact (mul_ne m hpr _ _ (mul_ne m hpr _ _ (mul_ne m hpr _ _ (neg_ne m _ (one_ne m)) (sub_ne m x _ hb)) (sub_ne m x _ hb)) (sub_ne m x _ hc0)) key

theorem nz_X6n (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (ht0 : a ≠ (ring (2 * m + 1 + 1)).zero) (ht1 : a ≠ (ring (2 * m + 1 + 1)).one) (_h0 : x ≠ (ring (2 * m + 1 + 1)).zero) (hb : x ≠ (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a))  -- (+118 chars; full line in the Zenodo source)
    X6n m a x y ≠ (ring (2 * m + 1 + 1)).zero := by
  have ht1' := sub_ne m a (ring (2 * m + 1 + 1)).one ht1
  unfold X6n
  intro e
  have key : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))))) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 +   -- (+88 chars; full line in the Zenodo source)
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))))) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).  -- (+897 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := e
  exact mul_ne m hpr _ _ (neg_ne m _ (mul_ne m hpr _ _ ((mul_ne m hpr _ _ (ht0) ht0)) ht1')) (sub_ne m x _ hb) key

theorem nz_X6d (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) x y) (_ht0   -- (+399 chars; full line in the Zenodo source)
    X6d m a x y ≠ (ring (2 * m + 1 + 1)).zero := by
  unfold X6d
  intro e
  unfold cB at hc
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m   -- (+655 chars; full line in the Zenodo source)
  have key : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero)))) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero)))) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg   -- (+202 chars; full line in the Zenodo source)
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero)))) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero)))) ((ring (2 * m + 1 + 1)).add (x) ((ring (2 * m + 1 + 1)).neg ((ri  -- (+2113 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [gcv, e]; lib_ring
  exact (mul_ne m hpr _ _ (mul_ne m hpr _ _ (mul_ne m hpr _ _ (neg_ne m _ (one_ne m)) (sub_ne m x _ h0)) (sub_ne m x _ h0)) (sub_ne m x _ hb)) key

end G7LF

#print axioms G7LF.sub_ne
#print axioms G7LF.nz_L1n
#print axioms G7LF.nz_L1d
#print axioms G7LF.nz_L2n
#print axioms G7LF.nz_L2d
#print axioms G7LF.nz_L3n
#print axioms G7LF.nz_L3d
#print axioms G7LF.nz_L4n
#print axioms G7LF.nz_L4d
#print axioms G7LF.nz_L5n
#print axioms G7LF.nz_L5d
#print axioms G7LF.nz_L6n
#print axioms G7LF.nz_L6d
#print axioms G7LF.nz_X1n
#print axioms G7LF.nz_X1d
#print axioms G7LF.nz_X2n
#print axioms G7LF.nz_X2d
#print axioms G7LF.nz_X3n
#print axioms G7LF.nz_X3d
#print axioms G7LF.nz_X4n
#print axioms G7LF.nz_X4d
#print axioms G7LF.nz_X5n
#print axioms G7LF.nz_X5d
#print axioms G7LF.nz_X6n
#print axioms G7LF.nz_X6d

lang/G7LF.lean

lean_all/lang/G7LF.lean

/-
  lang/G7LF — T43, cycle 370: linear-fractional forms of P + kT (k = 1..6) on E_t = E(t³ − t², t² − t) (G5 order 7, part 4).

  For the Tate normal form with b = t³ − t², c = t² − t the point T = (0, 0) has order 7, and for P = (x, y) on E_t
      x(P + kT) = Xkn/Xkd,  y(P + kT)/x(P + kT) = Lkn/Lkd,
  where all six forms are linear in x, y with polynomial coefficients in t (found by std-lib Python interpolation over
  𝔽_1000003 and then proved step by step in lang/G7S0 … G7S6). This file only holds the definitions.
-/
import lang.G7Tate1

namespace G7LF

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Step G4Gen G4Map G7Tate1

variable (m : Nat)

def L1n (a x _y : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1  -- (+423 chars; full line in the Zenodo source)
def L1d (a x y : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)))) (x)) (y)
def L2n (a x y : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1   -- (+517 chars; full line in the Zenodo source)
def L2d (a x y : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) (a)))) ((ring (2 * m + 1 + 1)).mul (a) (x))) (y)
def L3n (a x _y : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)))) ((ring  -- (+250 chars; full line in the Zenodo source)
def L3d (a x _y : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) (x)
def L4n (a x y : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1))  -- (+290 chars; full line in the Zenodo source)
def L4d (a x y : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).  -- (+286 chars; full line in the Zenodo source)
def L5n (a x _y : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1   -- (+394 chars; full line in the Zenodo source)
def L5d (a x y : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) (x)) (y)
def L6n (a x y : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring   -- (+191 chars; full line in the Zenodo source)
def L6d (_a x _y : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := x
def X1n (a x _y : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 *   -- (+522 chars; full line in the Zenodo source)
def X1d (a x y : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) (a)) (  -- (+75 chars; full line in the Zenodo source)
def X2n (a x y : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m   -- (+1288 chars; full line in the Zenodo source)
def X2d (a x y : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) (a)))) ((ring (2 * m + 1 + 1)).mul (a) (x))) (y)
def X3n (a x y : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1   -- (+517 chars; full line in the Zenodo source)
def X3d (a x y : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (  -- (+129 chars; full line in the Zenodo source)
def X4n (a x y : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1))  -- (+290 chars; full line in the Zenodo source)
def X4d (a x y : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (a))) (x)) (y)
def X5n (a x y : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1))  -- (+356 chars; full line in the Zenodo source)
def X5d (a x y : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).  -- (+286 chars; full line in the Zenodo source)
def X6n (a x _y : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m  -- (+493 chars; full line in the Zenodo source)
def X6d (_a _x y : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := y

end G7LF

lang/G7R1.lean

lean_all/lang/G7R1.lean

/-
  lang/G7R1 — T43, cycle 387: the order-7 steps 1x, 1l in the original form at the points with t ≠ 0, 1, from the rescaled
  core identities lang/G7C1x, G7C1l (x = t(t − 1)u, y = t²(t − 1)²v; each original form is a t-monomial times a small form,
  and numerator/denominator ratios pick up exactly the factor t(t − 1)).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G7Gen
import lang.G7C1x
import lang.G7C1l

namespace G7LF

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Step G4Gen G4Map G7Tate1

variable (m : Nat)

theorem rmain1x (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y) (h0 :  -- (+222 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (L1n m a x y) (iv m (L1d m a x y))) ((ring (2 * m + 1 + 1)).mul (X1d m a x y) (iv m (X1n m a x y)))) = (ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv   -- (+22 chars; full line in the Zenodo source)
  have ht1' := G7LF.sub_ne m a (ring (2 * m + 1 + 1)).one ht1
  have nc : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)) ≠ (ring (2 * m + 1 + 1)).zero := by
    have e0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)) = (ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by lib_ring
    rw [e0]; exact mul_ne m hpr _ _ ht0 ht1'
  have ec : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ nc
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) = e at ec
  obtain ⟨u, hu⟩ : ∃ u : Zp (2 * m + 1 + 1), u = (ring (2 * m + 1 + 1)).mul x e := ⟨_, rfl⟩
  obtain ⟨v, hv⟩ : ∃ v : Zp (2 * m + 1 + 1), v = (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul e e) := ⟨_, rfl⟩
  have hx : x = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u := by
    rw [hu]
    calc x = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul e ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) := by rw [ec]; lib_ring
      _ = _ := by lib_ring
  have hy : y = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) v := by
    rw [hv]
    calc y = (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul e ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) ((ring (2 * m + 1 + 1)).mul e ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) := by rw [ec]; lib_ring
      _ = _ := by lib_ring
  rw [hx, hy] at hc h0 h1 h2 ⊢
  unfold cB at hc
  have hcv' : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) ((ri  -- (+828 chars; full line in the Zenodo source)
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) v) ((ring (2 * m + 1   -- (+2386 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
  have gcv0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).a  -- (+444 chars; full line in the Zenodo source)
    rcases mul_eq_zero m hpr _ _ hcv' with e0 | e0
    · exact absurd e0 (mul_ne m hpr _ _ nc (mul_ne m hpr _ _ nc nc))
    · exact e0
  have nzdL1 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) (v)) ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => h0 (by
    calc L1d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))  -- (+469 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [e0]; lib_ring)
  have crL1 : (ring (2 * m + 1 + 1)).mul (L1n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a))   -- (+1033 chars; full line in the Zenodo source)
  have qL1 : (ring (2 * m + 1 + 1)).mul (L1n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) (  -- (+1021 chars; full line in the Zenodo source)
    have e1 := iv_mul_self m hpr _ nzdL1
    have e2 := iv_mul_self m hpr _ h0
    calc (ring (2 * m + 1 + 1)).mul (L1n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((rin  -- (+1813 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (L1n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m  -- (+973 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u))  -- (+1057 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u))) (iv m ((ring (2 * m + 1 +   -- (+1056 chars; full line in the Zenodo source)
      _ = _ := by rw [e2]; lib_ring
  have nznX1 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => h1 (by
    calc X1n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))  -- (+552 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [e0]; lib_ring)
  have crX1 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (X1d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2  -- (+1194 chars; full line in the Zenodo source)
  have qX1 : (ring (2 * m + 1 + 1)).mul (X1d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) (  -- (+1085 chars; full line in the Zenodo source)
    have e1 := iv_mul_self m hpr _ nznX1
    have e2 := iv_mul_self m hpr _ h1
    calc (ring (2 * m + 1 + 1)).mul (X1d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((rin  -- (+1836 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (X1d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m  -- (+992 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) (u))) ((ring (2 * m + 1 + 1)).mul  -- (+1121 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul e ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) (u))) ((ring (2 * m + 1 + 1)).m  -- (+1120 chars; full line in the Zenodo source)
      _ = _ := by rw [e2]; lib_ring
  have nzdX2 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (a)) (v)) ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => h2 (by
    calc X2d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))  -- (+494 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [e0]; lib_ring)
  have crX2 : (ring (2 * m + 1 + 1)).mul (X2n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a))   -- (+1410 chars; full line in the Zenodo source)
  have qX2 : (ring (2 * m + 1 + 1)).mul (X2n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) (  -- (+1398 chars; full line in the Zenodo source)
    have e1 := iv_mul_self m hpr _ nzdX2
    have e2 := iv_mul_self m hpr _ h2
    calc (ring (2 * m + 1 + 1)).mul (X2n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((rin  -- (+1797 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X2n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m  -- (+957 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring  -- (+1434 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ri  -- (+1433 chars; full line in the Zenodo source)
      _ = _ := by rw [e2]; lib_ring
  have hbE : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) = (ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) := by lib_ring
  rw [qL1, qX1, qX2, hbE]
  have ri : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) (v)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((rin  -- (+157 chars; full line in the Zenodo source)
  have rj : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u))) = (ring (2 * m + 1 + 1)).one := iv_mul_s  -- (+17 chars; full line in the Zenodo source)
  have rk : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (a)) (v)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m  -- (+141 chars; full line in the Zenodo source)
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) (v))) = i at ri ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u))) = j at rj ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (a)) (v))) = k at rk ⊢
  have gri : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul u (i))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a (i))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul v ((ring (2 * m + 1 + 1)).mul a (i)))) ((ring (2 * m + 1 + 1))  -- (+111 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) (v)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ri]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) (v)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ri]; lib_ring)
  have grj : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul u ((ring (2 * m + 1 + 1)).mul a (j)))) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a (j)))) = (ring (2 * m + 1 + 1)).zero := by
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [rj]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [rj]; lib_ring)
  have grk : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul v (k)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul u (k)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a (k))) ((ring (2 * m + 1 + 1)).mul v ((ring (2 * m + 1 + 1)).  -- (+49 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (a)) (v)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [rk]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (a)) (v)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [rk]; lib_ring)
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul v (u))) ((ring (2 * m + 1 + 1)).mul u ((ring (2 * m + 1 + 1)).mul u (u)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul v (a)) ((ring (2 * m + 1 + 1)).mul v ((ring (2 * m + 1 + 1)).mul v (a))))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m   -- (+493 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1))  -- (+425 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := gcv0)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1))  -- (+454 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [gcv0]; lib_ring)
  have core := cstep1x m a u v i j k gri grj grk gcv
  calc _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (  -- (+633 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring  -- (+363 chars; full line in the Zenodo source)
    _ = _ := by lib_ring

theorem rmain1l (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y) (h0 :  -- (+222 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul (L2n m a x y) (iv m (L2d m a x y)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (L1n m a x y) (iv m (L1d m a x y))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))))))  -- (+264 chars; full line in the Zenodo source)
  have ht1' := G7LF.sub_ne m a (ring (2 * m + 1 + 1)).one ht1
  have nc : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)) ≠ (ring (2 * m + 1 + 1)).zero := by
    have e0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)) = (ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by lib_ring
    rw [e0]; exact mul_ne m hpr _ _ ht0 ht1'
  have ec : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ nc
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) = e at ec
  obtain ⟨u, hu⟩ : ∃ u : Zp (2 * m + 1 + 1), u = (ring (2 * m + 1 + 1)).mul x e := ⟨_, rfl⟩
  obtain ⟨v, hv⟩ : ∃ v : Zp (2 * m + 1 + 1), v = (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul e e) := ⟨_, rfl⟩
  have hx : x = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u := by
    rw [hu]
    calc x = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul e ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) := by rw [ec]; lib_ring
      _ = _ := by lib_ring
  have hy : y = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) v := by
    rw [hv]
    calc y = (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul e ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) ((ring (2 * m + 1 + 1)).mul e ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) := by rw [ec]; lib_ring
      _ = _ := by lib_ring
  rw [hx, hy] at hc h0 h1 h2 ⊢
  unfold cB at hc
  have hcv' : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) ((ri  -- (+828 chars; full line in the Zenodo source)
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) v) ((ring (2 * m + 1   -- (+2386 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
  have gcv0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).a  -- (+444 chars; full line in the Zenodo source)
    rcases mul_eq_zero m hpr _ _ hcv' with e0 | e0
    · exact absurd e0 (mul_ne m hpr _ _ nc (mul_ne m hpr _ _ nc nc))
    · exact e0
  have nzdL2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (a)) (v))) ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => h0 (by
    calc L2d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))  -- (+552 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [e0]; lib_ring)
  have crL2 : (ring (2 * m + 1 + 1)).mul (L2n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a))   -- (+1161 chars; full line in the Zenodo source)
  have qL2 : (ring (2 * m + 1 + 1)).mul (L2n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) (  -- (+1149 chars; full line in the Zenodo source)
    have e1 := iv_mul_self m hpr _ nzdL2
    have e2 := iv_mul_self m hpr _ h0
    calc (ring (2 * m + 1 + 1)).mul (L2n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((rin  -- (+1979 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (L2n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m  -- (+1139 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a  -- (+1185 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).m  -- (+1184 chars; full line in the Zenodo source)
      _ = _ := by rw [e2]; lib_ring
  have nzdL1 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) (v)) ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => h1 (by
    calc L1d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))  -- (+469 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [e0]; lib_ring)
  have crL1 : (ring (2 * m + 1 + 1)).mul (L1n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a))   -- (+1033 chars; full line in the Zenodo source)
  have qL1 : (ring (2 * m + 1 + 1)).mul (L1n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) (  -- (+1021 chars; full line in the Zenodo source)
    have e1 := iv_mul_self m hpr _ nzdL1
    have e2 := iv_mul_self m hpr _ h1
    calc (ring (2 * m + 1 + 1)).mul (L1n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((rin  -- (+1813 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (L1n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m  -- (+973 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u))  -- (+1057 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u))) (iv m ((ring (2 * m + 1 +   -- (+1056 chars; full line in the Zenodo source)
      _ = _ := by rw [e2]; lib_ring
  have nznX2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m +  -- (+229 chars; full line in the Zenodo source)
    calc X2n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))  -- (+995 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [e0]; lib_ring)
  have crX2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (X2d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2  -- (+1410 chars; full line in the Zenodo source)
  have qX2 : (ring (2 * m + 1 + 1)).mul (X2d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) (  -- (+1301 chars; full line in the Zenodo source)
    have e1 := iv_mul_self m hpr _ nznX2
    have e2 := iv_mul_self m hpr _ h2
    calc (ring (2 * m + 1 + 1)).mul (X2d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((rin  -- (+2656 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (X2d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m  -- (+1812 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (a)) (v))) (X2n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1))  -- (+1337 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul e ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (a)) (v))) (iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).  -- (+1336 chars; full line in the Zenodo source)
      _ = _ := by rw [e2]; lib_ring
  have hbE : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) = (ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) := by lib_ring
  rw [qL2, qL1, qX2, hbE]
  have ri : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (a)) (v))))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m +   -- (+323 chars; full line in the Zenodo source)
  have rj : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) (v)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((rin  -- (+157 chars; full line in the Zenodo source)
  have rk : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m  -- (+837 chars; full line in the Zenodo source)
  generalize iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (a)) (v)))) = i at ri ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) (v))) = j at rj ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 *  -- (+196 chars; full line in the Zenodo source)
  have gri : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (a)) (v))))) ((ring (2 * m + 1 +  -- (+72 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (a)) (v))))) ((ring (2 * m +   -- (+54 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ri]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (  -- (+83 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ri]; lib_ring)
  have grj : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) (v)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) (v)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [rj]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) (v)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [rj]; lib_ring)
  have grk : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m +  -- (+329 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 *   -- (+311 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [rk]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 *  -- (+340 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [rk]; lib_ring)
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).ad  -- (+443 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1))  -- (+425 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := gcv0)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1))  -- (+454 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [gcv0]; lib_ring)
  have core := cstep1l m a u v i j k gri grj grk gcv
  calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).  -- (+596 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (  -- (+532 chars; full line in the Zenodo source)
    _ = _ := by
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add (  -- (+778 chars; full line in the Zenodo source)
        _ = _ := by lib_ring

/-- **step 1x** (the statement of lang/G7S1). -/
theorem step1x (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y) (h0 :   -- (+142 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (L1n m a x y) (iv m (L1d m a x y))) ((ring (2 * m + 1 + 1)).mul (X1d m a x y) (iv m (X1n m a x y)))) = (ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv   -- (+22 chars; full line in the Zenodo source)
  by_cases ht0 : a = (ring (2 * m + 1 + 1)).zero
  · exact absurd (by rw [ht0]; unfold X1n; lib_ring) h1
  by_cases ht1 : a = (ring (2 * m + 1 + 1)).one
  · exact absurd (by rw [ht1]; unfold X1n; lib_ring) h1
  exact rmain1x m hpr a x y hc h0 h1 h2 ht0 ht1

/-- **step 1l** (the statement of lang/G7S1). -/
theorem step1l (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y) (h0 :   -- (+142 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul (L2n m a x y) (iv m (L2d m a x y)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (L1n m a x y) (iv m (L1d m a x y))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))))))  -- (+264 chars; full line in the Zenodo source)
  by_cases ht0 : a = (ring (2 * m + 1 + 1)).zero
  · exact absurd (by rw [ht0]; unfold X2n; lib_ring) h2
  by_cases ht1 : a = (ring (2 * m + 1 + 1)).one
  · exact absurd (by rw [ht1]; unfold X2n; lib_ring) h2
  exact rmain1l m hpr a x y hc h0 h1 h2 ht0 ht1

end G7LF

#print axioms G7LF.rmain1x
#print axioms G7LF.rmain1l
#print axioms G7LF.step1x
#print axioms G7LF.step1l

lang/G7R2.lean

lean_all/lang/G7R2.lean

/-
  lang/G7R2 — T43, cycle 388: the order-7 steps 2x, 2l in the original form at the points with t ≠ 0, 1, from the rescaled
  core identities lang/G7C2x, G7C2l (x = t(t − 1)u, y = t²(t − 1)²v; each original form is a t-monomial times a small form,
  and numerator/denominator ratios pick up exactly the factor t(t − 1)).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G7Gen
import lang.G7C2x
import lang.G7C2l

namespace G7LF

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Step G4Gen G4Map G7Tate1

variable (m : Nat)

theorem rmain2x (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y) (h0 :  -- (+222 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (L2n m a x y) (iv m (L2d m a x y))) ((ring (2 * m + 1 + 1)).mul (X2d m a x y) (iv m (X2n m a x y)))) = (ring (2 * m + 1 + 1)).mul (X3n m a x y) (iv   -- (+22 chars; full line in the Zenodo source)
  have ht1' := G7LF.sub_ne m a (ring (2 * m + 1 + 1)).one ht1
  have nc : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)) ≠ (ring (2 * m + 1 + 1)).zero := by
    have e0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)) = (ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by lib_ring
    rw [e0]; exact mul_ne m hpr _ _ ht0 ht1'
  have ec : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ nc
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) = e at ec
  obtain ⟨u, hu⟩ : ∃ u : Zp (2 * m + 1 + 1), u = (ring (2 * m + 1 + 1)).mul x e := ⟨_, rfl⟩
  obtain ⟨v, hv⟩ : ∃ v : Zp (2 * m + 1 + 1), v = (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul e e) := ⟨_, rfl⟩
  have hx : x = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u := by
    rw [hu]
    calc x = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul e ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) := by rw [ec]; lib_ring
      _ = _ := by lib_ring
  have hy : y = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) v := by
    rw [hv]
    calc y = (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul e ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) ((ring (2 * m + 1 + 1)).mul e ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) := by rw [ec]; lib_ring
      _ = _ := by lib_ring
  rw [hx, hy] at hc h0 h1 h2 ⊢
  unfold cB at hc
  have hcv' : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) ((ri  -- (+828 chars; full line in the Zenodo source)
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) v) ((ring (2 * m + 1   -- (+2386 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
  have gcv0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).a  -- (+444 chars; full line in the Zenodo source)
    rcases mul_eq_zero m hpr _ _ hcv' with e0 | e0
    · exact absurd e0 (mul_ne m hpr _ _ nc (mul_ne m hpr _ _ nc nc))
    · exact e0
  have nzdL2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (a)) (v))) ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => h0 (by
    calc L2d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))  -- (+552 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [e0]; lib_ring)
  have crL2 : (ring (2 * m + 1 + 1)).mul (L2n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a))   -- (+1161 chars; full line in the Zenodo source)
  have qL2 : (ring (2 * m + 1 + 1)).mul (L2n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) (  -- (+1149 chars; full line in the Zenodo source)
    have e1 := iv_mul_self m hpr _ nzdL2
    have e2 := iv_mul_self m hpr _ h0
    calc (ring (2 * m + 1 + 1)).mul (L2n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((rin  -- (+1979 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (L2n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m  -- (+1139 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a  -- (+1185 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).m  -- (+1184 chars; full line in the Zenodo source)
      _ = _ := by rw [e2]; lib_ring
  have nznX2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m +  -- (+229 chars; full line in the Zenodo source)
    calc X2n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))  -- (+995 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [e0]; lib_ring)
  have crX2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (X2d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2  -- (+1410 chars; full line in the Zenodo source)
  have qX2 : (ring (2 * m + 1 + 1)).mul (X2d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) (  -- (+1301 chars; full line in the Zenodo source)
    have e1 := iv_mul_self m hpr _ nznX2
    have e2 := iv_mul_self m hpr _ h1
    calc (ring (2 * m + 1 + 1)).mul (X2d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((rin  -- (+2656 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (X2d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m  -- (+1812 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (a)) (v))) (X2n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1))  -- (+1337 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul e ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (a)) (v))) (iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).  -- (+1336 chars; full line in the Zenodo source)
      _ = _ := by rw [e2]; lib_ring
  have nzdX3 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m +  -- (+229 chars; full line in the Zenodo source)
    calc X3d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))  -- (+809 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [e0]; lib_ring)
  have crX3 : (ring (2 * m + 1 + 1)).mul (X3n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a))   -- (+1418 chars; full line in the Zenodo source)
  have qX3 : (ring (2 * m + 1 + 1)).mul (X3n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) (  -- (+1406 chars; full line in the Zenodo source)
    have e1 := iv_mul_self m hpr _ nzdX3
    have e2 := iv_mul_self m hpr _ h2
    calc (ring (2 * m + 1 + 1)).mul (X3n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((rin  -- (+2493 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X3n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m  -- (+1653 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a  -- (+1442 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).m  -- (+1441 chars; full line in the Zenodo source)
      _ = _ := by rw [e2]; lib_ring
  have hbE : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) = (ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) := by lib_ring
  rw [qL2, qX2, qX3, hbE]
  have ri : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (a)) (v))))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m +   -- (+323 chars; full line in the Zenodo source)
  have rj : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m  -- (+837 chars; full line in the Zenodo source)
  have rk : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m  -- (+837 chars; full line in the Zenodo source)
  generalize iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (a)) (v)))) = i at ri ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 *  -- (+196 chars; full line in the Zenodo source)
  obtain ⟨k, hkk⟩ : ∃ k : Zp (2 * m + 1 + 1), k = j := ⟨_, rfl⟩
  have rk : (ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1  -- (+261 chars; full line in the Zenodo source)
  have gri : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (a)) (v))))) ((ring (2 * m + 1 +  -- (+72 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (a)) (v))))) ((ring (2 * m +   -- (+54 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ri]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (  -- (+83 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ri]; lib_ring)
  have grj : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m +  -- (+329 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 *   -- (+311 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [rj]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 *  -- (+340 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [rj]; lib_ring)
  have grk : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m +  -- (+329 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 *   -- (+311 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [rk]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 *  -- (+340 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [rk]; lib_ring)
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).ad  -- (+443 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1))  -- (+425 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := gcv0)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1))  -- (+454 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [gcv0]; lib_ring)
  have core := cstep2x m a u v i j k gri grj grk gcv
  calc _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).a  -- (+484 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) (v))) (k)) := by   -- (+23 chars; full line in the Zenodo source)
    _ = _ := by subst hkk; lib_ring

theorem rmain2l (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y) (h0 :  -- (+222 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul (L3n m a x y) (iv m (L3d m a x y)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (L2n m a x y) (iv m (L2d m a x y))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))))))  -- (+264 chars; full line in the Zenodo source)
  have ht1' := G7LF.sub_ne m a (ring (2 * m + 1 + 1)).one ht1
  have nc : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)) ≠ (ring (2 * m + 1 + 1)).zero := by
    have e0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)) = (ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by lib_ring
    rw [e0]; exact mul_ne m hpr _ _ ht0 ht1'
  have ec : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ nc
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) = e at ec
  obtain ⟨u, hu⟩ : ∃ u : Zp (2 * m + 1 + 1), u = (ring (2 * m + 1 + 1)).mul x e := ⟨_, rfl⟩
  obtain ⟨v, hv⟩ : ∃ v : Zp (2 * m + 1 + 1), v = (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul e e) := ⟨_, rfl⟩
  have hx : x = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u := by
    rw [hu]
    calc x = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul e ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) := by rw [ec]; lib_ring
      _ = _ := by lib_ring
  have hy : y = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) v := by
    rw [hv]
    calc y = (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul e ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) ((ring (2 * m + 1 + 1)).mul e ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) := by rw [ec]; lib_ring
      _ = _ := by lib_ring
  rw [hx, hy] at hc h0 h1 h2 ⊢
  unfold cB at hc
  have hcv' : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) ((ri  -- (+828 chars; full line in the Zenodo source)
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) v) ((ring (2 * m + 1   -- (+2386 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
  have gcv0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).a  -- (+444 chars; full line in the Zenodo source)
    rcases mul_eq_zero m hpr _ _ hcv' with e0 | e0
    · exact absurd e0 (mul_ne m hpr _ _ nc (mul_ne m hpr _ _ nc nc))
    · exact e0
  have nzdL3 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => h0 (by
    calc L3d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))  -- (+391 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [e0]; lib_ring)
  have crL3 : (ring (2 * m + 1 + 1)).mul (L3n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a))   -- (+872 chars; full line in the Zenodo source)
  have qL3 : (ring (2 * m + 1 + 1)).mul (L3n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) (  -- (+860 chars; full line in the Zenodo source)
    have e1 := iv_mul_self m hpr _ nzdL3
    have e2 := iv_mul_self m hpr _ h0
    calc (ring (2 * m + 1 + 1)).mul (L3n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((rin  -- (+1723 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (L3n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m  -- (+883 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) (L3d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 *   -- (+896 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) (iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1   -- (+895 chars; full line in the Zenodo source)
      _ = _ := by rw [e2]; lib_ring
  have nzdL2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (a)) (v))) ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => h1 (by
    calc L2d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))  -- (+552 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [e0]; lib_ring)
  have crL2 : (ring (2 * m + 1 + 1)).mul (L2n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a))   -- (+1161 chars; full line in the Zenodo source)
  have qL2 : (ring (2 * m + 1 + 1)).mul (L2n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) (  -- (+1149 chars; full line in the Zenodo source)
    have e1 := iv_mul_self m hpr _ nzdL2
    have e2 := iv_mul_self m hpr _ h1
    calc (ring (2 * m + 1 + 1)).mul (L2n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((rin  -- (+1979 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (L2n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m  -- (+1139 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a  -- (+1185 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).m  -- (+1184 chars; full line in the Zenodo source)
      _ = _ := by rw [e2]; lib_ring
  have nznX3 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) (v)) ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => h2 (by
    calc X3n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))  -- (+622 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [e0]; lib_ring)
  have crX3 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (X3d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2  -- (+1418 chars; full line in the Zenodo source)
  have qX3 : (ring (2 * m + 1 + 1)).mul (X3d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) (  -- (+1309 chars; full line in the Zenodo source)
    have e1 := iv_mul_self m hpr _ nznX3
    have e2 := iv_mul_self m hpr _ h2
    calc (ring (2 * m + 1 + 1)).mul (X3d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((rin  -- (+1976 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (X3d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m  -- (+1132 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 +  -- (+1345 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul e ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1  -- (+1344 chars; full line in the Zenodo source)
      _ = _ := by rw [e2]; lib_ring
  have hbE : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) = (ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) := by lib_ring
  rw [qL3, qL2, qX3, hbE]
  have ri : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).on  -- (+67 chars; full line in the Zenodo source)
  have rj : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (a)) (v))))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m +   -- (+323 chars; full line in the Zenodo source)
  have rk : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) (v)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((rin  -- (+157 chars; full line in the Zenodo source)
  generalize iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u))) = i at ri ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (a)) (v)))) = j at rj ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) (v))) = k at rk ⊢
  have gri : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ri]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ri]; lib_ring)
  have grj : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (a)) (v))))) ((ring (2 * m + 1 +  -- (+72 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (a)) (v))))) ((ring (2 * m +   -- (+54 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [rj]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (  -- (+83 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [rj]; lib_ring)
  have grk : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) (v)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) (v)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [rk]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) (v)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [rk]; lib_ring)
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).ad  -- (+443 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1))  -- (+425 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := gcv0)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1))  -- (+454 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [gcv0]; lib_ring)
  have core := cstep2l m a u v i j k gri grj grk gcv
  calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) (i))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m   -- (+274 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ri  -- (+925 chars; full line in the Zenodo source)
    _ = _ := by
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).m  -- (+1171 chars; full line in the Zenodo source)
        _ = _ := by lib_ring

/-- **step 2x** (the statement of lang/G7S2). -/
theorem step2x (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y) (h0 :   -- (+142 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (L2n m a x y) (iv m (L2d m a x y))) ((ring (2 * m + 1 + 1)).mul (X2d m a x y) (iv m (X2n m a x y)))) = (ring (2 * m + 1 + 1)).mul (X3n m a x y) (iv   -- (+22 chars; full line in the Zenodo source)
  by_cases ht0 : a = (ring (2 * m + 1 + 1)).zero
  · exact absurd (by rw [ht0]; unfold X2n; lib_ring) h1
  by_cases ht1 : a = (ring (2 * m + 1 + 1)).one
  · exact absurd (by rw [ht1]; unfold X2n; lib_ring) h1
  exact rmain2x m hpr a x y hc h0 h1 h2 ht0 ht1

/-- **step 2l** (the statement of lang/G7S2). -/
theorem step2l (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y) (h0 :   -- (+142 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul (L3n m a x y) (iv m (L3d m a x y)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (L2n m a x y) (iv m (L2d m a x y))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))))))  -- (+264 chars; full line in the Zenodo source)
  by_cases ht0 : a = (ring (2 * m + 1 + 1)).zero
  · exact absurd (by rw [ht0]; unfold X3n; lib_ring) h2
  by_cases ht1 : a = (ring (2 * m + 1 + 1)).one
  · exact absurd (by rw [ht1]; unfold X3n; lib_ring) h2
  exact rmain2l m hpr a x y hc h0 h1 h2 ht0 ht1

end G7LF

#print axioms G7LF.rmain2x
#print axioms G7LF.rmain2l
#print axioms G7LF.step2x
#print axioms G7LF.step2l

lang/G7R3.lean

lean_all/lang/G7R3.lean

/-
  lang/G7R3 — T43, cycle 386: the order-7 steps 3x, 3l in the original form at the points with t ≠ 0, 1, from the rescaled
  core identities lang/G7C3x, G7C3l (x = t(t − 1)u, y = t²(t − 1)²v; each original form is a t-monomial times a small form,
  and numerator/denominator ratios pick up exactly the factor t(t − 1)).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G7Gen
import lang.G7C3x
import lang.G7C3l

namespace G7LF

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Step G4Gen G4Map G7Tate1

variable (m : Nat)

theorem rmain3x (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y) (h0 :  -- (+222 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (L3n m a x y) (iv m (L3d m a x y))) ((ring (2 * m + 1 + 1)).mul (X3d m a x y) (iv m (X3n m a x y)))) = (ring (2 * m + 1 + 1)).mul (X4n m a x y) (iv   -- (+22 chars; full line in the Zenodo source)
  have ht1' := G7LF.sub_ne m a (ring (2 * m + 1 + 1)).one ht1
  have nc : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)) ≠ (ring (2 * m + 1 + 1)).zero := by
    have e0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)) = (ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by lib_ring
    rw [e0]; exact mul_ne m hpr _ _ ht0 ht1'
  have ec : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ nc
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) = e at ec
  obtain ⟨u, hu⟩ : ∃ u : Zp (2 * m + 1 + 1), u = (ring (2 * m + 1 + 1)).mul x e := ⟨_, rfl⟩
  obtain ⟨v, hv⟩ : ∃ v : Zp (2 * m + 1 + 1), v = (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul e e) := ⟨_, rfl⟩
  have hx : x = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u := by
    rw [hu]
    calc x = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul e ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) := by rw [ec]; lib_ring
      _ = _ := by lib_ring
  have hy : y = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) v := by
    rw [hv]
    calc y = (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul e ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) ((ring (2 * m + 1 + 1)).mul e ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) := by rw [ec]; lib_ring
      _ = _ := by lib_ring
  rw [hx, hy] at hc h0 h1 h2 ⊢
  unfold cB at hc
  have hcv' : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) ((ri  -- (+828 chars; full line in the Zenodo source)
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) v) ((ring (2 * m + 1   -- (+2386 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
  have gcv0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).a  -- (+444 chars; full line in the Zenodo source)
    rcases mul_eq_zero m hpr _ _ hcv' with e0 | e0
    · exact absurd e0 (mul_ne m hpr _ _ nc (mul_ne m hpr _ _ nc nc))
    · exact e0
  have nzdL3 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => h0 (by
    calc L3d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))  -- (+391 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [e0]; lib_ring)
  have crL3 : (ring (2 * m + 1 + 1)).mul (L3n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a))   -- (+872 chars; full line in the Zenodo source)
  have qL3 : (ring (2 * m + 1 + 1)).mul (L3n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) (  -- (+860 chars; full line in the Zenodo source)
    have e1 := iv_mul_self m hpr _ nzdL3
    have e2 := iv_mul_self m hpr _ h0
    calc (ring (2 * m + 1 + 1)).mul (L3n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((rin  -- (+1723 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (L3n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m  -- (+883 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) (L3d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 *   -- (+896 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) (iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1   -- (+895 chars; full line in the Zenodo source)
      _ = _ := by rw [e2]; lib_ring
  have nznX3 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) (v)) ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => h1 (by
    calc X3n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))  -- (+622 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [e0]; lib_ring)
  have crX3 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (X3d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2  -- (+1418 chars; full line in the Zenodo source)
  have qX3 : (ring (2 * m + 1 + 1)).mul (X3d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) (  -- (+1309 chars; full line in the Zenodo source)
    have e1 := iv_mul_self m hpr _ nznX3
    have e2 := iv_mul_self m hpr _ h1
    calc (ring (2 * m + 1 + 1)).mul (X3d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((rin  -- (+1976 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (X3d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m  -- (+1132 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 +  -- (+1345 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul e ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1  -- (+1344 chars; full line in the Zenodo source)
      _ = _ := by rw [e2]; lib_ring
  have nzdX4 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) ((ring (2 * m + 1 + 1)).mul (a) (v)) ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => h2 (by
    calc X4d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))  -- (+519 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [e0]; lib_ring)
  have crX4 : (ring (2 * m + 1 + 1)).mul (X4n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a))   -- (+996 chars; full line in the Zenodo source)
  have qX4 : (ring (2 * m + 1 + 1)).mul (X4n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) (  -- (+984 chars; full line in the Zenodo source)
    have e1 := iv_mul_self m hpr _ nzdX4
    have e2 := iv_mul_self m hpr _ h2
    calc (ring (2 * m + 1 + 1)).mul (X4n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((rin  -- (+1673 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X4n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m  -- (+833 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring  -- (+1020 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u))  -- (+1019 chars; full line in the Zenodo source)
      _ = _ := by rw [e2]; lib_ring
  have hbE : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) = (ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) := by lib_ring
  rw [qL3, qX3, qX4, hbE]
  have ri : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).on  -- (+67 chars; full line in the Zenodo source)
  have rj : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) (v)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((rin  -- (+157 chars; full line in the Zenodo source)
  have rk : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) ((ring (2 * m + 1 + 1)).mul (a) (v)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) ((ring (2 * m + 1 + 1)).mul (a) (v))) = (ring (2 * m + 1 + 1)).one := iv_mul_s  -- (+17 chars; full line in the Zenodo source)
  generalize iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u))) = i at ri ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) (v))) = j at rj ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) ((ring (2 * m + 1 + 1)).mul (a) (v))) = k at rk ⊢
  have gri : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a (i))) ((ring (2 * m + 1 + 1)).mul u ((ring (2 * m + 1 + 1)).mul a (i)))) = (ring (2 * m + 1 + 1)).zero := by
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ri]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ri]; lib_ring)
  have grj : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul u (j))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a (j))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul v ((ring (2 * m + 1 + 1)).mul a (j)))) ((ring (2 * m + 1 + 1))  -- (+111 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) (v)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [rj]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul (a) (a))) (v)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [rj]; lib_ring)
  have grk : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul u (k))) ((ring (2 * m + 1 + 1)).mul v ((ring (2 * m + 1 + 1)).mul a (k)))) = (ring (2 * m + 1 + 1)).zero := by
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) ((ring (2 * m + 1 + 1)).mul (a) (v)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [rk]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) ((ring (2 * m + 1 + 1)).mul (a) (v)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [rk]; lib_ring)
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul v (u))) ((ring (2 * m + 1 + 1)).mul u ((ring (2 * m + 1 + 1)).mul u (u)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul v (a)) ((ring (2 * m + 1 + 1)).mul v ((ring (2 * m + 1 + 1)).mul v (a))))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m   -- (+493 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1))  -- (+425 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := gcv0)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1))  -- (+454 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [gcv0]; lib_ring)
  have core := cstep3x m a u v i j k gri grj grk gcv
  calc _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) (i)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 +   -- (+671 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v))) (k)) := by rw [ec, core  -- (+11 chars; full line in the Zenodo source)
    _ = _ := by lib_ring

theorem rmain3l (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y) (h0 :  -- (+222 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul (L4n m a x y) (iv m (L4d m a x y)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (L3n m a x y) (iv m (L3d m a x y))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))))))  -- (+264 chars; full line in the Zenodo source)
  have ht1' := G7LF.sub_ne m a (ring (2 * m + 1 + 1)).one ht1
  have nc : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)) ≠ (ring (2 * m + 1 + 1)).zero := by
    have e0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)) = (ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by lib_ring
    rw [e0]; exact mul_ne m hpr _ _ ht0 ht1'
  have ec : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ nc
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) = e at ec
  obtain ⟨u, hu⟩ : ∃ u : Zp (2 * m + 1 + 1), u = (ring (2 * m + 1 + 1)).mul x e := ⟨_, rfl⟩
  obtain ⟨v, hv⟩ : ∃ v : Zp (2 * m + 1 + 1), v = (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul e e) := ⟨_, rfl⟩
  have hx : x = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u := by
    rw [hu]
    calc x = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul e ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) := by rw [ec]; lib_ring
      _ = _ := by lib_ring
  have hy : y = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) v := by
    rw [hv]
    calc y = (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul e ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) ((ring (2 * m + 1 + 1)).mul e ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) := by rw [ec]; lib_ring
      _ = _ := by lib_ring
  rw [hx, hy] at hc h0 h1 h2 ⊢
  unfold cB at hc
  have hcv' : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) ((ri  -- (+828 chars; full line in the Zenodo source)
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) v) ((ring (2 * m + 1   -- (+2386 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
  have gcv0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).a  -- (+444 chars; full line in the Zenodo source)
    rcases mul_eq_zero m hpr _ _ hcv' with e0 | e0
    · exact absurd e0 (mul_ne m hpr _ _ nc (mul_ne m hpr _ _ nc nc))
    · exact e0
  have nzdL4 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (a))) (u))) ((ring (2 * m + 1 + 1)).mul (a) (v)) ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => h0 (by
    calc L4d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))  -- (+614 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [e0]; lib_ring)
  have crL4 : (ring (2 * m + 1 + 1)).mul (L4n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a))   -- (+1091 chars; full line in the Zenodo source)
  have qL4 : (ring (2 * m + 1 + 1)).mul (L4n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) (  -- (+1079 chars; full line in the Zenodo source)
    have e1 := iv_mul_self m hpr _ nzdL4
    have e2 := iv_mul_self m hpr _ h0
    calc (ring (2 * m + 1 + 1)).mul (L4n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((rin  -- (+1863 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (L4n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m  -- (+1023 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring  -- (+1115 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u))  -- (+1114 chars; full line in the Zenodo source)
      _ = _ := by rw [e2]; lib_ring
  have nzdL3 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => h1 (by
    calc L3d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))  -- (+391 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [e0]; lib_ring)
  have crL3 : (ring (2 * m + 1 + 1)).mul (L3n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a))   -- (+872 chars; full line in the Zenodo source)
  have qL3 : (ring (2 * m + 1 + 1)).mul (L3n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) (  -- (+860 chars; full line in the Zenodo source)
    have e1 := iv_mul_self m hpr _ nzdL3
    have e2 := iv_mul_self m hpr _ h1
    calc (ring (2 * m + 1 + 1)).mul (L3n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((rin  -- (+1723 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (L3n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m  -- (+883 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) (L3d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 *   -- (+896 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) (iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1   -- (+895 chars; full line in the Zenodo source)
      _ = _ := by rw [e2]; lib_ring
  have nznX4 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v)) ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => h2 (by
    calc X4n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))  -- (+730 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [e0]; lib_ring)
  have crX4 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (X4d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2  -- (+996 chars; full line in the Zenodo source)
  have qX4 : (ring (2 * m + 1 + 1)).mul (X4d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) (  -- (+887 chars; full line in the Zenodo source)
    have e1 := iv_mul_self m hpr _ nznX4
    have e2 := iv_mul_self m hpr _ h2
    calc (ring (2 * m + 1 + 1)).mul (X4d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((rin  -- (+1952 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (X4d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m  -- (+1108 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) ((ring (2 * m + 1 + 1)).mul (a) (v))) (X4n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 *  -- (+923 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul e ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) ((ring (2 * m + 1 + 1)).mul (a) (v))) (iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).  -- (+922 chars; full line in the Zenodo source)
      _ = _ := by rw [e2]; lib_ring
  have hbE : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) = (ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) := by lib_ring
  rw [qL4, qL3, qX4, hbE]
  have ri : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (a))) (u))) ((ring (2 * m + 1 + 1)).mul (a) (v)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m   -- (+207 chars; full line in the Zenodo source)
  have rj : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).on  -- (+67 chars; full line in the Zenodo source)
  have rk : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1  -- (+133 chars; full line in the Zenodo source)
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (a))) (u))) ((ring (2 * m + 1 + 1)).mul (a) (v))) = i at ri ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u))) = j at rj ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v))) = k at rk ⊢
  have gri : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul u (i))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a (i))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul v ((ring (2 * m + 1 + 1)).mul a (i)))) ((ring (2 * m + 1 + 1)).mul u ((ring (2 * m + 1 + 1)  -- (+51 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (a))) (u))) ((ring (2 * m + 1 + 1)).mul (a) (v)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ri]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (a))) (u))) ((ring (2 * m + 1 + 1)).mul (a) (v)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 +  -- (+25 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ri]; lib_ring)
  have grj : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a (j))) ((ring (2 * m + 1 + 1)).mul u ((ring (2 * m + 1 + 1)).mul a (j)))) = (ring (2 * m + 1 + 1)).zero := by
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [rj]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [rj]; lib_ring)
  have grk : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul v ((ring (2 * m + 1 + 1)).mul a (k)))) ((ring (2 * m + 1 + 1)).mul u ((ring (2 * m + 1 + 1)).mul a (k)))) = (ring (2 * m + 1 + 1)).zero := by
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [rk]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [rk]; lib_ring)
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul v (u))) ((ring (2 * m + 1 + 1)).mul u ((ring (2 * m + 1 + 1)).mul u (u)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul v (a)) ((ring (2 * m + 1 + 1)).mul v ((ring (2 * m + 1 + 1)).mul v (a))))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m   -- (+493 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1))  -- (+425 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := gcv0)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1))  -- (+454 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [gcv0]; lib_ring)
  have core := cstep3l m a u v i j k gri grj grk gcv
  calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)  -- (+572 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) (j))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).n  -- (+354 chars; full line in the Zenodo source)
    _ = _ := by
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) (u)) j)) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)  -- (+600 chars; full line in the Zenodo source)
        _ = _ := by lib_ring

/-- **step 3x** (the statement of lang/G7S3). -/
theorem step3x (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y) (h0 :   -- (+142 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (L3n m a x y) (iv m (L3d m a x y))) ((ring (2 * m + 1 + 1)).mul (X3d m a x y) (iv m (X3n m a x y)))) = (ring (2 * m + 1 + 1)).mul (X4n m a x y) (iv   -- (+22 chars; full line in the Zenodo source)
  by_cases ht0 : a = (ring (2 * m + 1 + 1)).zero
  · exact absurd (by rw [ht0]; unfold X3n; lib_ring) h1
  by_cases ht1 : a = (ring (2 * m + 1 + 1)).one
  · exact absurd (by rw [ht1]; unfold X3n; lib_ring) h1
  exact rmain3x m hpr a x y hc h0 h1 h2 ht0 ht1

/-- **step 3l** (the statement of lang/G7S3). -/
theorem step3l (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y) (h0 :   -- (+142 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul (L4n m a x y) (iv m (L4d m a x y)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (L3n m a x y) (iv m (L3d m a x y))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))))))  -- (+264 chars; full line in the Zenodo source)
  by_cases ht0 : a = (ring (2 * m + 1 + 1)).zero
  · exact absurd (by rw [ht0]; unfold X4n; lib_ring) h2
  by_cases ht1 : a = (ring (2 * m + 1 + 1)).one
  · exact absurd (by rw [ht1]; unfold X4n; lib_ring) h2
  exact rmain3l m hpr a x y hc h0 h1 h2 ht0 ht1

end G7LF

#print axioms G7LF.rmain3x
#print axioms G7LF.rmain3l
#print axioms G7LF.step3x
#print axioms G7LF.step3l

lang/G7R4.lean

lean_all/lang/G7R4.lean

/-
  lang/G7R4 — T43, cycle 388: the order-7 steps 4x, 4l in the original form at the points with t ≠ 0, 1, from the rescaled
  core identities lang/G7C4x, G7C4l (x = t(t − 1)u, y = t²(t − 1)²v; each original form is a t-monomial times a small form,
  and numerator/denominator ratios pick up exactly the factor t(t − 1)).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G7Gen
import lang.G7C4x
import lang.G7C4l

namespace G7LF

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Step G4Gen G4Map G7Tate1

variable (m : Nat)

theorem rmain4x (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y) (h0 :  -- (+173 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (L4n m a x y) (iv m (L4d m a x y))) ((ring (2 * m + 1 + 1)).mul (X4d m a x y) (iv m (X4n m a x y)))) = (ring (2 * m + 1 + 1)).mul (X5n m a x y) (iv   -- (+22 chars; full line in the Zenodo source)
  have h2 : X5d m a x y ≠ (ring (2 * m + 1 + 1)).zero := by
    unfold X5d; unfold L4d at h0; exact h0
  have ht1' := G7LF.sub_ne m a (ring (2 * m + 1 + 1)).one ht1
  have nc : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)) ≠ (ring (2 * m + 1 + 1)).zero := by
    have e0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)) = (ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by lib_ring
    rw [e0]; exact mul_ne m hpr _ _ ht0 ht1'
  have ec : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ nc
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) = e at ec
  obtain ⟨u, hu⟩ : ∃ u : Zp (2 * m + 1 + 1), u = (ring (2 * m + 1 + 1)).mul x e := ⟨_, rfl⟩
  obtain ⟨v, hv⟩ : ∃ v : Zp (2 * m + 1 + 1), v = (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul e e) := ⟨_, rfl⟩
  have hx : x = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u := by
    rw [hu]
    calc x = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul e ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) := by rw [ec]; lib_ring
      _ = _ := by lib_ring
  have hy : y = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) v := by
    rw [hv]
    calc y = (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul e ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) ((ring (2 * m + 1 + 1)).mul e ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) := by rw [ec]; lib_ring
      _ = _ := by lib_ring
  rw [hx, hy] at hc h0 h1 h2 ⊢
  unfold cB at hc
  have hcv' : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) ((ri  -- (+828 chars; full line in the Zenodo source)
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) v) ((ring (2 * m + 1   -- (+2386 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
  have gcv0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).a  -- (+444 chars; full line in the Zenodo source)
    rcases mul_eq_zero m hpr _ _ hcv' with e0 | e0
    · exact absurd e0 (mul_ne m hpr _ _ nc (mul_ne m hpr _ _ nc nc))
    · exact e0
  have nzdL4 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (a))) (u))) ((ring (2 * m + 1 + 1)).mul (a) (v)) ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => h0 (by
    calc L4d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))  -- (+614 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [e0]; lib_ring)
  have crL4 : (ring (2 * m + 1 + 1)).mul (L4n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a))   -- (+1091 chars; full line in the Zenodo source)
  have qL4 : (ring (2 * m + 1 + 1)).mul (L4n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) (  -- (+1079 chars; full line in the Zenodo source)
    have e1 := iv_mul_self m hpr _ nzdL4
    have e2 := iv_mul_self m hpr _ h0
    calc (ring (2 * m + 1 + 1)).mul (L4n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((rin  -- (+1863 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (L4n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m  -- (+1023 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring  -- (+1115 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u))  -- (+1114 chars; full line in the Zenodo source)
      _ = _ := by rw [e2]; lib_ring
  have nznX4 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v)) ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => h1 (by
    calc X4n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))  -- (+730 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [e0]; lib_ring)
  have crX4 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (X4d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2  -- (+996 chars; full line in the Zenodo source)
  have qX4 : (ring (2 * m + 1 + 1)).mul (X4d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) (  -- (+887 chars; full line in the Zenodo source)
    have e1 := iv_mul_self m hpr _ nznX4
    have e2 := iv_mul_self m hpr _ h1
    calc (ring (2 * m + 1 + 1)).mul (X4d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((rin  -- (+1952 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (X4d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m  -- (+1108 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) ((ring (2 * m + 1 + 1)).mul (a) (v))) (X4n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 *  -- (+923 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul e ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) ((ring (2 * m + 1 + 1)).mul (a) (v))) (iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).  -- (+922 chars; full line in the Zenodo source)
      _ = _ := by rw [e2]; lib_ring
  have nzdX5 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (a))) (u))) ((ring (2 * m + 1 + 1)).mul (a) (v)) ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => h2 (by
    calc X5d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))  -- (+614 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [e0]; lib_ring)
  have crX5 : (ring (2 * m + 1 + 1)).mul (X5n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a))   -- (+1124 chars; full line in the Zenodo source)
  have qX5 : (ring (2 * m + 1 + 1)).mul (X5n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) (  -- (+1112 chars; full line in the Zenodo source)
    have e1 := iv_mul_self m hpr _ nzdX5
    have e2 := iv_mul_self m hpr _ h2
    calc (ring (2 * m + 1 + 1)).mul (X5n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((rin  -- (+1863 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X5n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m  -- (+1023 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring  -- (+1148 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u))  -- (+1147 chars; full line in the Zenodo source)
      _ = _ := by rw [e2]; lib_ring
  have hbE : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) = (ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) := by lib_ring
  rw [qL4, qX4, qX5, hbE]
  have ri : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (a))) (u))) ((ring (2 * m + 1 + 1)).mul (a) (v)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m   -- (+207 chars; full line in the Zenodo source)
  have rj : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1  -- (+133 chars; full line in the Zenodo source)
  have rk : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (a))) (u))) ((ring (2 * m + 1 + 1)).mul (a) (v)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m   -- (+207 chars; full line in the Zenodo source)
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (a))) (u))) ((ring (2 * m + 1 + 1)).mul (a) (v))) = i at ri ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v))) = j at rj ⊢
  obtain ⟨k, hkk⟩ : ∃ k : Zp (2 * m + 1 + 1), k = i := ⟨_, rfl⟩
  have rk : (ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (a))) (u))) ((ring (2 * m + 1 + 1)).mul (a) (v))) = (ring (2 * m + 1 + 1)).one := by rw [hkk]; exact ri
  have gri : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (a))) (u))) ((ring (2 * m + 1 + 1)).mul (a) (v)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 +   -- (+14 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (a))) (u))) ((ring (2 * m + 1 + 1)).mul (a) (v)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ri]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (a))) (u))) ((ring (2 * m + 1 + 1)).mul (a) (v)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 +  -- (+25 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ri]; lib_ring)
  have grj : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [rj]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [rj]; lib_ring)
  have grk : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (a))) (u))) ((ring (2 * m + 1 + 1)).mul (a) (v)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 +   -- (+14 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (a))) (u))) ((ring (2 * m + 1 + 1)).mul (a) (v)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [rk]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (a))) (u))) ((ring (2 * m + 1 + 1)).mul (a) (v)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 +  -- (+25 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [rk]; lib_ring)
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).ad  -- (+443 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1))  -- (+425 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := gcv0)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1))  -- (+454 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [gcv0]; lib_ring)
  have core := cstep4x m a u v i j k gri grj grk gcv
  calc _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (  -- (+410 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) ((ring (2 * m + 1 + 1)).mul (  -- (+44 chars; full line in the Zenodo source)
    _ = _ := by subst hkk; lib_ring

theorem rmain4l (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y) (h0 :  -- (+222 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul (L5n m a x y) (iv m (L5d m a x y)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (L4n m a x y) (iv m (L4d m a x y))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))))))  -- (+264 chars; full line in the Zenodo source)
  have ht1' := G7LF.sub_ne m a (ring (2 * m + 1 + 1)).one ht1
  have nc : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)) ≠ (ring (2 * m + 1 + 1)).zero := by
    have e0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)) = (ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by lib_ring
    rw [e0]; exact mul_ne m hpr _ _ ht0 ht1'
  have ec : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ nc
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) = e at ec
  obtain ⟨u, hu⟩ : ∃ u : Zp (2 * m + 1 + 1), u = (ring (2 * m + 1 + 1)).mul x e := ⟨_, rfl⟩
  obtain ⟨v, hv⟩ : ∃ v : Zp (2 * m + 1 + 1), v = (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul e e) := ⟨_, rfl⟩
  have hx : x = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u := by
    rw [hu]
    calc x = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul e ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) := by rw [ec]; lib_ring
      _ = _ := by lib_ring
  have hy : y = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) v := by
    rw [hv]
    calc y = (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul e ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) ((ring (2 * m + 1 + 1)).mul e ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) := by rw [ec]; lib_ring
      _ = _ := by lib_ring
  rw [hx, hy] at hc h0 h1 h2 ⊢
  unfold cB at hc
  have hcv' : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) ((ri  -- (+828 chars; full line in the Zenodo source)
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) v) ((ring (2 * m + 1   -- (+2386 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
  have gcv0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).a  -- (+444 chars; full line in the Zenodo source)
    rcases mul_eq_zero m hpr _ _ hcv' with e0 | e0
    · exact absurd e0 (mul_ne m hpr _ _ nc (mul_ne m hpr _ _ nc nc))
    · exact e0
  have nzdL5 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v)) ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => h0 (by
    calc L5d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))  -- (+577 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [e0]; lib_ring)
  have crL5 : (ring (2 * m + 1 + 1)).mul (L5n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a))   -- (+1075 chars; full line in the Zenodo source)
  have qL5 : (ring (2 * m + 1 + 1)).mul (L5n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) (  -- (+1063 chars; full line in the Zenodo source)
    have e1 := iv_mul_self m hpr _ nzdL5
    have e2 := iv_mul_self m hpr _ h0
    calc (ring (2 * m + 1 + 1)).mul (L5n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((rin  -- (+1963 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (L5n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m  -- (+1123 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u))) (L5d m a ((ring (2 * m + 1 + 1)  -- (+1099 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u))) (iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).m  -- (+1098 chars; full line in the Zenodo source)
      _ = _ := by rw [e2]; lib_ring
  have nzdL4 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (a))) (u))) ((ring (2 * m + 1 + 1)).mul (a) (v)) ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => h1 (by
    calc L4d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))  -- (+614 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [e0]; lib_ring)
  have crL4 : (ring (2 * m + 1 + 1)).mul (L4n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a))   -- (+1091 chars; full line in the Zenodo source)
  have qL4 : (ring (2 * m + 1 + 1)).mul (L4n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) (  -- (+1079 chars; full line in the Zenodo source)
    have e1 := iv_mul_self m hpr _ nzdL4
    have e2 := iv_mul_self m hpr _ h1
    calc (ring (2 * m + 1 + 1)).mul (L4n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((rin  -- (+1863 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (L4n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m  -- (+1023 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring  -- (+1115 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u))  -- (+1114 chars; full line in the Zenodo source)
      _ = _ := by rw [e2]; lib_ring
  have nznX5 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) ((ring (2 * m + 1 + 1)).mul (a) (v))) ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => h2 (by
    calc X5n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))  -- (+763 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [e0]; lib_ring)
  have crX5 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (X5d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2  -- (+1124 chars; full line in the Zenodo source)
  have qX5 : (ring (2 * m + 1 + 1)).mul (X5d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) (  -- (+1015 chars; full line in the Zenodo source)
    have e1 := iv_mul_self m hpr _ nznX5
    have e2 := iv_mul_self m hpr _ h2
    calc (ring (2 * m + 1 + 1)).mul (X5d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((rin  -- (+2018 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (X5d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m  -- (+1174 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (a))) (u))) ((ring (2 * m + 1 + 1)).mul (a) (v))) (X5n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1   -- (+1051 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul e ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (a))) (u))) ((ring (2 * m + 1 + 1)).mul (a) (v))) (iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 +  -- (+1050 chars; full line in the Zenodo source)
      _ = _ := by rw [e2]; lib_ring
  have hbE : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) = (ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) := by lib_ring
  rw [qL5, qL4, qX5, hbE]
  have ri : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).  -- (+307 chars; full line in the Zenodo source)
  have rj : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (a))) (u))) ((ring (2 * m + 1 + 1)).mul (a) (v)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m   -- (+207 chars; full line in the Zenodo source)
  have rk : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) ((ring (2 * m + 1 + 1)).mul (a) (v))))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m +   -- (+199 chars; full line in the Zenodo source)
  generalize iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v))) = i at ri ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (a))) (u))) ((ring (2 * m + 1 + 1)).mul (a) (v))) = j at rj ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) ((ring (2 * m + 1 + 1)).mul (a) (v)))) = k at rk ⊢
  have gri : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v)))) ((ring (2 * m + 1 + 1)).neg  -- (+64 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v)))) ((ring (2 * m + 1 + 1)).  -- (+46 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ri]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v)  -- (+75 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ri]; lib_ring)
  have grj : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (a))) (u))) ((ring (2 * m + 1 + 1)).mul (a) (v)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 +   -- (+14 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (a))) (u))) ((ring (2 * m + 1 + 1)).mul (a) (v)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [rj]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (a))) (u))) ((ring (2 * m + 1 + 1)).mul (a) (v)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 +  -- (+25 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [rj]; lib_ring)
  have grk : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) ((ring (2 * m + 1 + 1)).mul (a) (v))))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).  -- (+10 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) ((ring (2 * m + 1 + 1)).mul (a) (v))))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [rk]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) ((ring (2 * m + 1 + 1)).mul (a) (v))))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1))  -- (+21 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [rk]; lib_ring)
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).ad  -- (+443 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1))  -- (+425 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := gcv0)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1))  -- (+454 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [gcv0]; lib_ring)
  have core := cstep4l m a u v i j k gri grj grk gcv
  calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u))) (i))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).  -- (+440 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (  -- (+598 chars; full line in the Zenodo source)
    _ = _ := by
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add (  -- (+844 chars; full line in the Zenodo source)
        _ = _ := by lib_ring

/-- **step 4x** (the statement of lang/G7S4). -/
theorem step4x (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y) (h0 :   -- (+93 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (L4n m a x y) (iv m (L4d m a x y))) ((ring (2 * m + 1 + 1)).mul (X4d m a x y) (iv m (X4n m a x y)))) = (ring (2 * m + 1 + 1)).mul (X5n m a x y) (iv   -- (+22 chars; full line in the Zenodo source)
  by_cases ht0 : a = (ring (2 * m + 1 + 1)).zero
  · exact absurd (by rw [ht0]; unfold X4n; lib_ring) h1
  by_cases ht1 : a = (ring (2 * m + 1 + 1)).one
  · exact absurd (by rw [ht1]; unfold X4n; lib_ring) h1
  exact rmain4x m hpr a x y hc h0 h1 ht0 ht1

/-- **step 4l** (the statement of lang/G7S4). -/
theorem step4l (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y) (h0 :   -- (+142 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul (L5n m a x y) (iv m (L5d m a x y)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (L4n m a x y) (iv m (L4d m a x y))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))))))  -- (+264 chars; full line in the Zenodo source)
  by_cases ht0 : a = (ring (2 * m + 1 + 1)).zero
  · exact absurd (by rw [ht0]; unfold X5n; lib_ring) h2
  by_cases ht1 : a = (ring (2 * m + 1 + 1)).one
  · exact absurd (by rw [ht1]; unfold X5n; lib_ring) h2
  exact rmain4l m hpr a x y hc h0 h1 h2 ht0 ht1

end G7LF

#print axioms G7LF.rmain4x
#print axioms G7LF.rmain4l
#print axioms G7LF.step4x
#print axioms G7LF.step4l

lang/G7R5.lean

lean_all/lang/G7R5.lean

/-
  lang/G7R5 — T43, cycle 388: the order-7 steps 5x, 5l in the original form at the points with t ≠ 0, 1, from the rescaled
  core identities lang/G7C5x, G7C5l (x = t(t − 1)u, y = t²(t − 1)²v; each original form is a t-monomial times a small form,
  and numerator/denominator ratios pick up exactly the factor t(t − 1)).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G7Gen
import lang.G7C5x
import lang.G7C5l

namespace G7LF

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Step G4Gen G4Map G7Tate1

variable (m : Nat)

theorem rmain5x (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y) (h0 :  -- (+222 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (L5n m a x y) (iv m (L5d m a x y))) ((ring (2 * m + 1 + 1)).mul (X5d m a x y) (iv m (X5n m a x y)))) = (ring (2 * m + 1 + 1)).mul (X6n m a x y) (iv   -- (+22 chars; full line in the Zenodo source)
  have ht1' := G7LF.sub_ne m a (ring (2 * m + 1 + 1)).one ht1
  have nc : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)) ≠ (ring (2 * m + 1 + 1)).zero := by
    have e0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)) = (ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by lib_ring
    rw [e0]; exact mul_ne m hpr _ _ ht0 ht1'
  have ec : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ nc
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) = e at ec
  obtain ⟨u, hu⟩ : ∃ u : Zp (2 * m + 1 + 1), u = (ring (2 * m + 1 + 1)).mul x e := ⟨_, rfl⟩
  obtain ⟨v, hv⟩ : ∃ v : Zp (2 * m + 1 + 1), v = (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul e e) := ⟨_, rfl⟩
  have hx : x = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u := by
    rw [hu]
    calc x = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul e ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) := by rw [ec]; lib_ring
      _ = _ := by lib_ring
  have hy : y = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) v := by
    rw [hv]
    calc y = (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul e ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) ((ring (2 * m + 1 + 1)).mul e ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) := by rw [ec]; lib_ring
      _ = _ := by lib_ring
  rw [hx, hy] at hc h0 h1 h2 ⊢
  unfold cB at hc
  have hcv' : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) ((ri  -- (+828 chars; full line in the Zenodo source)
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) v) ((ring (2 * m + 1   -- (+2386 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
  have gcv0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).a  -- (+444 chars; full line in the Zenodo source)
    rcases mul_eq_zero m hpr _ _ hcv' with e0 | e0
    · exact absurd e0 (mul_ne m hpr _ _ nc (mul_ne m hpr _ _ nc nc))
    · exact e0
  have nzdL5 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v)) ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => h0 (by
    calc L5d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))  -- (+577 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [e0]; lib_ring)
  have crL5 : (ring (2 * m + 1 + 1)).mul (L5n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a))   -- (+1075 chars; full line in the Zenodo source)
  have qL5 : (ring (2 * m + 1 + 1)).mul (L5n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) (  -- (+1063 chars; full line in the Zenodo source)
    have e1 := iv_mul_self m hpr _ nzdL5
    have e2 := iv_mul_self m hpr _ h0
    calc (ring (2 * m + 1 + 1)).mul (L5n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((rin  -- (+1963 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (L5n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m  -- (+1123 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u))) (L5d m a ((ring (2 * m + 1 + 1)  -- (+1099 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u))) (iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).m  -- (+1098 chars; full line in the Zenodo source)
      _ = _ := by rw [e2]; lib_ring
  have nznX5 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) ((ring (2 * m + 1 + 1)).mul (a) (v))) ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => h1 (by
    calc X5n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))  -- (+763 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [e0]; lib_ring)
  have crX5 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (X5d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2  -- (+1124 chars; full line in the Zenodo source)
  have qX5 : (ring (2 * m + 1 + 1)).mul (X5d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) (  -- (+1015 chars; full line in the Zenodo source)
    have e1 := iv_mul_self m hpr _ nznX5
    have e2 := iv_mul_self m hpr _ h1
    calc (ring (2 * m + 1 + 1)).mul (X5d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((rin  -- (+2018 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (X5d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m  -- (+1174 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (a))) (u))) ((ring (2 * m + 1 + 1)).mul (a) (v))) (X5n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1   -- (+1051 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul e ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (a))) (u))) ((ring (2 * m + 1 + 1)).mul (a) (v))) (iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 +  -- (+1050 chars; full line in the Zenodo source)
      _ = _ := by rw [e2]; lib_ring
  have nzdX6 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) (v) ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => h2 (by
    calc X6d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))  -- (+457 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [e0]; lib_ring)
  have crX6 : (ring (2 * m + 1 + 1)).mul (X6n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a))   -- (+930 chars; full line in the Zenodo source)
  have qX6 : (ring (2 * m + 1 + 1)).mul (X6n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) (  -- (+918 chars; full line in the Zenodo source)
    have e1 := iv_mul_self m hpr _ nzdX6
    have e2 := iv_mul_self m hpr _ h2
    calc (ring (2 * m + 1 + 1)).mul (X6n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((rin  -- (+1723 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X6n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m  -- (+883 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u))) (X6d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 +  -- (+954 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u))) (iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)  -- (+953 chars; full line in the Zenodo source)
      _ = _ := by rw [e2]; lib_ring
  have hbE : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) = (ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) := by lib_ring
  rw [qL5, qX5, qX6, hbE]
  have ri : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).  -- (+307 chars; full line in the Zenodo source)
  have rj : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) ((ring (2 * m + 1 + 1)).mul (a) (v))))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m +   -- (+199 chars; full line in the Zenodo source)
  have rk : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) (v))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one  -- (+67 chars; full line in the Zenodo source)
  generalize iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v))) = i at ri ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) ((ring (2 * m + 1 + 1)).mul (a) (v)))) = j at rj ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) (v)) = k at rk ⊢
  have gri : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v)))) ((ring (2 * m + 1 + 1)).neg  -- (+64 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v)))) ((ring (2 * m + 1 + 1)).  -- (+46 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ri]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v)  -- (+75 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ri]; lib_ring)
  have grj : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) ((ring (2 * m + 1 + 1)).mul (a) (v))))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).  -- (+10 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) ((ring (2 * m + 1 + 1)).mul (a) (v))))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [rj]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) ((ring (2 * m + 1 + 1)).mul (a) (v))))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1))  -- (+21 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [rj]; lib_ring)
  have grk : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) (v))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) (v))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [rk]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) (v))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [rk]; lib_ring)
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).ad  -- (+443 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1))  -- (+425 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := gcv0)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1))  -- (+454 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [gcv0]; lib_ring)
  have core := cstep5x m a u v i j k gri grj grk gcv
  calc _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u))  -- (+439 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u))) (k)) := by rw [ec, core]; lib_ring
    _ = _ := by lib_ring

theorem rmain5l (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y) (h0 :  -- (+222 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul (L6n m a x y) (iv m (L6d m a x y)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (L5n m a x y) (iv m (L5d m a x y))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))))))  -- (+264 chars; full line in the Zenodo source)
  have ht1' := G7LF.sub_ne m a (ring (2 * m + 1 + 1)).one ht1
  have nc : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)) ≠ (ring (2 * m + 1 + 1)).zero := by
    have e0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)) = (ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by lib_ring
    rw [e0]; exact mul_ne m hpr _ _ ht0 ht1'
  have ec : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ nc
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) = e at ec
  obtain ⟨u, hu⟩ : ∃ u : Zp (2 * m + 1 + 1), u = (ring (2 * m + 1 + 1)).mul x e := ⟨_, rfl⟩
  obtain ⟨v, hv⟩ : ∃ v : Zp (2 * m + 1 + 1), v = (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul e e) := ⟨_, rfl⟩
  have hx : x = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u := by
    rw [hu]
    calc x = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul e ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) := by rw [ec]; lib_ring
      _ = _ := by lib_ring
  have hy : y = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) v := by
    rw [hv]
    calc y = (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul e ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) ((ring (2 * m + 1 + 1)).mul e ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) := by rw [ec]; lib_ring
      _ = _ := by lib_ring
  rw [hx, hy] at hc h0 h1 h2 ⊢
  unfold cB at hc
  have hcv' : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) ((ri  -- (+828 chars; full line in the Zenodo source)
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))) v) ((ring (2 * m + 1   -- (+2386 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
  have gcv0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).a  -- (+444 chars; full line in the Zenodo source)
    rcases mul_eq_zero m hpr _ _ hcv' with e0 | e0
    · exact absurd e0 (mul_ne m hpr _ _ nc (mul_ne m hpr _ _ nc nc))
    · exact e0
  have nzdL6 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) (u) ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => h0 (by
    calc L6d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))  -- (+304 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [e0]; lib_ring)
  have crL6 : (ring (2 * m + 1 + 1)).mul (L6n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a))   -- (+1252 chars; full line in the Zenodo source)
  have qL6 : (ring (2 * m + 1 + 1)).mul (L6n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) (  -- (+1240 chars; full line in the Zenodo source)
    have e1 := iv_mul_self m hpr _ nzdL6
    have e2 := iv_mul_self m hpr _ h0
    calc (ring (2 * m + 1 + 1)).mul (L6n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((rin  -- (+1789 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (L6n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m  -- (+949 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (  -- (+1276 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring  -- (+1275 chars; full line in the Zenodo source)
      _ = _ := by rw [e2]; lib_ring
  have nzdL5 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v)) ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => h1 (by
    calc L5d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))  -- (+577 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [e0]; lib_ring)
  have crL5 : (ring (2 * m + 1 + 1)).mul (L5n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a))   -- (+1075 chars; full line in the Zenodo source)
  have qL5 : (ring (2 * m + 1 + 1)).mul (L5n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) (  -- (+1063 chars; full line in the Zenodo source)
    have e1 := iv_mul_self m hpr _ nzdL5
    have e2 := iv_mul_self m hpr _ h1
    calc (ring (2 * m + 1 + 1)).mul (L5n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((rin  -- (+1963 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (L5n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m  -- (+1123 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u))) (L5d m a ((ring (2 * m + 1 + 1)  -- (+1099 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u))) (iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).m  -- (+1098 chars; full line in the Zenodo source)
      _ = _ := by rw [e2]; lib_ring
  have nznX6 : (ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => h2 (by
    calc X6n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))  -- (+552 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [e0]; lib_ring)
  have crX6 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (X6d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2  -- (+930 chars; full line in the Zenodo source)
  have qX6 : (ring (2 * m + 1 + 1)).mul (X6d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) (  -- (+821 chars; full line in the Zenodo source)
    have e1 := iv_mul_self m hpr _ nznX6
    have e2 := iv_mul_self m hpr _ h2
    calc (ring (2 * m + 1 + 1)).mul (X6d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((rin  -- (+1770 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (X6d m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) u) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m  -- (+926 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) (v)) (X6n m a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1))  -- (+857 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul e ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) (v)) (iv m ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))  -- (+856 chars; full line in the Zenodo source)
      _ = _ := by rw [e2]; lib_ring
  have hbE : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) = (ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) := by lib_ring
  rw [qL6, qL5, qX6, hbE]
  have ri : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) (u))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1  -- (+133 chars; full line in the Zenodo source)
  have rj : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).  -- (+307 chars; full line in the Zenodo source)
  have rk : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)))) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u))) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ nznX6
  generalize iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) (u)) = i at ri ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v))) = j at rj ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u))) = k at rk ⊢
  have gri : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) (u))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) (u))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ri]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) (u))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ri]; lib_ring)
  have grj : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v)))) ((ring (2 * m + 1 + 1)).neg  -- (+64 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v)))) ((ring (2 * m + 1 + 1)).  -- (+46 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [rj]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)) (v)  -- (+75 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [rj]; lib_ring)
  have grk : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [rk]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [rk]; lib_ring)
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).ad  -- (+443 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1))  -- (+425 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := gcv0)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1))  -- (+454 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [gcv0]; lib_ring)
  have core := cstep5l m a u v i j k gri grj grk gcv
  calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((rin  -- (+968 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (u))) (j)))  -- (+462 chars; full line in the Zenodo source)
    _ = _ := by
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring  -- (+708 chars; full line in the Zenodo source)
        _ = _ := by lib_ring

/-- **step 5x** (the statement of lang/G7S5). -/
theorem step5x (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y) (h0 :   -- (+142 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (L5n m a x y) (iv m (L5d m a x y))) ((ring (2 * m + 1 + 1)).mul (X5d m a x y) (iv m (X5n m a x y)))) = (ring (2 * m + 1 + 1)).mul (X6n m a x y) (iv   -- (+22 chars; full line in the Zenodo source)
  by_cases ht0 : a = (ring (2 * m + 1 + 1)).zero
  · exact absurd (by rw [ht0]; unfold X5n; lib_ring) h1
  by_cases ht1 : a = (ring (2 * m + 1 + 1)).one
  · exact absurd (by rw [ht1]; unfold X5n; lib_ring) h1
  exact rmain5x m hpr a x y hc h0 h1 h2 ht0 ht1

/-- **step 5l** (the statement of lang/G7S5). -/
theorem step5l (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y) (h0 :   -- (+142 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul (L6n m a x y) (iv m (L6d m a x y)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (L5n m a x y) (iv m (L5d m a x y))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))))))  -- (+264 chars; full line in the Zenodo source)
  by_cases ht0 : a = (ring (2 * m + 1 + 1)).zero
  · exact absurd (by rw [ht0]; unfold X6n; lib_ring) h2
  by_cases ht1 : a = (ring (2 * m + 1 + 1)).one
  · exact absurd (by rw [ht1]; unfold X6n; lib_ring) h2
  exact rmain5l m hpr a x y hc h0 h1 h2 ht0 ht1

end G7LF

#print axioms G7LF.rmain5x
#print axioms G7LF.rmain5l
#print axioms G7LF.step5x
#print axioms G7LF.step5l

lang/G7S0.lean

lean_all/lang/G7S0.lean

/-
  lang/G7S0 — T43, cycle 370: step 0 → 1 of the order-7 orbit on E_t (G5 order 7, part 4).

    step0x   b·λ_0/x_0 = x_1                     (x-coordinate of τ(P + 0T))
    step0l   λ_1 = −(λ_0 + 1 − c) + b/x_1        (slope of P + 1T through T)
  with x_k = x(P + kT), λ_k = y(P + kT)/x(P + kT) the linear-fractional forms of lang/G7LF (x₀ = x, λ₀ = y/x, x₇ = x, λ₇ = y/x).
  Certificates (multipliers of the inverse relations and of the curve) from the std-lib Python reduction.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G7LF

namespace G7LF

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Step G4Gen G4Map G7Tate1

variable (m : Nat)

set_option maxRecDepth 100000 in
theorem step0x (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y) (hi :   -- (+83 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (iv m (x)))) = (ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y)  -- (+7 chars; full line in the Zenodo source)
  have ei : (ring (2 * m + 1 + 1)).mul (iv m (x)) (x) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hi
  have ek : (ring (2 * m + 1 + 1)).mul (iv m (X1d m a x y)) (X1d m a x y) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hk
  unfold cB at hc
  unfold X1d X1n at *
  generalize iv m (x) = i at ei ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m +  -- (+46 chars; full line in the Zenodo source)
  have gri : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul i (x)) = (ring (2 * m + 1 + 1)).zero := by
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i (x)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i (x)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring)
  have grk : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a (k)))) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a (k)))))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).n  -- (+361 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)  -- (+161 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ek]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)  -- (+190 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ek]; lib_ring)
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m   -- (+655 chars; full line in the Zenodo source)
  calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (i)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (i))) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m +   -- (+12655 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2   -- (+536 chars; full line in the Zenodo source)

set_option maxRecDepth 100000 in
theorem step0l (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y) (hi :   -- (+132 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul (L1n m a x y) (iv m (L1d m a x y)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))))) ((ring (2 * m + 1 +  -- (+244 chars; full line in the Zenodo source)
  have ei : (ring (2 * m + 1 + 1)).mul (iv m (L1d m a x y)) (L1d m a x y) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hi
  have ej : (ring (2 * m + 1 + 1)).mul (iv m (x)) (x) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hj
  have ek : (ring (2 * m + 1 + 1)).mul (iv m (X1n m a x y)) (X1n m a x y) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hk
  unfold cB at hc
  unfold L1d L1n X1d X1n at *
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)))) (x)) (y)) = i at ei ⊢
  generalize iv m (x) = j at ej ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul  -- (+492 chars; full line in the Zenodo source)
  have gri : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a (i))))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a (i)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mu  -- (+111 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)))) (x)) (y))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)))) (x)) (y))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by lib_  -- (+4 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring)
  have grj : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul j (x)) = (ring (2 * m + 1 + 1)).zero := by
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j (x)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j (x)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring)
  have grk : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a (k))))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (  -- (+692 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)   -- (+607 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ek]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one   -- (+636 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ek]; lib_ring)
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m   -- (+655 chars; full line in the Zenodo source)
  calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m +   -- (+40351 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (j)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul   -- (+599 chars; full line in the Zenodo source)

end G7LF

#print axioms G7LF.step0x
#print axioms G7LF.step0l

lang/G7S2.lean

lean_all/lang/G7S2.lean

/-
  lang/G7S2 — T43, cycle 370: step 2 → 3 of the order-7 orbit on E_t (G5 order 7, part 4).

    step2x   b·λ_2/x_2 = x_3                     (x-coordinate of τ(P + 2T))
    step2l   λ_3 = −(λ_2 + 1 − c) + b/x_3        (slope of P + 3T through T)
  with x_k = x(P + kT), λ_k = y(P + kT)/x(P + kT) the linear-fractional forms of lang/G7LF (x₀ = x, λ₀ = y/x, x₇ = x, λ₇ = y/x).
  Certificates (multipliers of the inverse relations and of the curve) from the std-lib Python reduction.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G7LF

namespace G7LF

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Step G4Gen G4Map G7Tate1

variable (m : Nat)

set_option maxRecDepth 100000 in
set_option maxHeartbeats 10000000 in
theorem step2x (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y) (hi :   -- (+142 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (L2n m a x y) (iv m (L2d m a x y))) ((ring (2 * m + 1 + 1)).mul (X2d m a x y) (iv m (X2n m a x y)))) = (ring (2 * m + 1 + 1)).mul (X3n m a x y) (iv   -- (+22 chars; full line in the Zenodo source)
  have ei : (ring (2 * m + 1 + 1)).mul (iv m (L2d m a x y)) (L2d m a x y) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hi
  have ej : (ring (2 * m + 1 + 1)).mul (iv m (X2n m a x y)) (X2n m a x y) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hj
  have ek : (ring (2 * m + 1 + 1)).mul (iv m (X3d m a x y)) (X3d m a x y) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hk
  unfold cB at hc
  unfold L2d L2n X2d X2n X3d X3n at *
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) (a)))) ((ring (2 * m + 1 + 1)).mul (a) (x))) (y)) = i at ei ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul  -- (+1259 chars; full line in the Zenodo source)
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1))  -- (+100 chars; full line in the Zenodo source)
  have gri : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a (i)))))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a (i))))) ((ring (2 * m + 1 +   -- (+204 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) (a)))) ((ring (2 * m + 1 + 1)).mul (a) (x))) (y  -- (+74 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) (a)))) ((ring (2 *   -- (+103 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring)
  have grj : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a (j)))))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (  -- (+1565 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one   -- (+1374 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add  -- (+1403 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring)
  have grk : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a (k)))) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a (k)))))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).m  -- (+380 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)  -- (+215 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ek]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)  -- (+244 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ek]; lib_ring)
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m   -- (+655 chars; full line in the Zenodo source)
  calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((rin  -- (+27623 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m +  -- (+532 chars; full line in the Zenodo source)

set_option maxRecDepth 100000 in
set_option maxHeartbeats 10000000 in
theorem step2l (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y) (hi :   -- (+142 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul (L3n m a x y) (iv m (L3d m a x y)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (L2n m a x y) (iv m (L2d m a x y))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))))))  -- (+264 chars; full line in the Zenodo source)
  have ei : (ring (2 * m + 1 + 1)).mul (iv m (L3d m a x y)) (L3d m a x y) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hi
  have ej : (ring (2 * m + 1 + 1)).mul (iv m (L2d m a x y)) (L2d m a x y) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hj
  have ek : (ring (2 * m + 1 + 1)).mul (iv m (X3n m a x y)) (X3n m a x y) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hk
  unfold cB at hc
  unfold L2d L2n L3d L3n X3d X3n at *
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) (x)) = i at ei ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) (a)))) ((ring (2 * m + 1 + 1)).mul (a) (x))) (y)) = j at ej ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ri  -- (+488 chars; full line in the Zenodo source)
  have gri : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a (i)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a (i))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul i (x)))) = (ring (2 * m + 1 + 1)).zero := by
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) (x))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) (x))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring)
  have grj : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a (j)))))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a (j))))) ((ring (2 * m + 1 +   -- (+204 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) (a)))) ((ring (2 * m + 1 + 1)).mul (a) (x))) (y  -- (+74 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) (a)))) ((ring (2 *   -- (+103 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring)
  have grk : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a (k))))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2   -- (+787 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((rin  -- (+603 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ek]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring  -- (+632 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ek]; lib_ring)
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m   -- (+655 chars; full line in the Zenodo source)
  calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)))) ((ring (2 * m + 1 + 1)).neg ((rin  -- (+87430 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m +  -- (+1508 chars; full line in the Zenodo source)

end G7LF

#print axioms G7LF.step2x
#print axioms G7LF.step2l

lang/G7S4.lean

lean_all/lang/G7S4.lean

/-
  lang/G7S4 — T43, cycle 370: step 4 → 5 of the order-7 orbit on E_t (G5 order 7, part 4).

    step4x   b·λ_4/x_4 = x_5                     (x-coordinate of τ(P + 4T))
    step4l   λ_5 = −(λ_4 + 1 − c) + b/x_5        (slope of P + 5T through T)
  with x_k = x(P + kT), λ_k = y(P + kT)/x(P + kT) the linear-fractional forms of lang/G7LF (x₀ = x, λ₀ = y/x, x₇ = x, λ₇ = y/x).
  Certificates (multipliers of the inverse relations and of the curve) from the std-lib Python reduction.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G7LF

namespace G7LF

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Step G4Gen G4Map G7Tate1

variable (m : Nat)

set_option maxRecDepth 100000 in
set_option maxHeartbeats 10000000 in
theorem step4x (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y) (hi :   -- (+93 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (L4n m a x y) (iv m (L4d m a x y))) ((ring (2 * m + 1 + 1)).mul (X4d m a x y) (iv m (X4n m a x y)))) = (ring (2 * m + 1 + 1)).mul (X5n m a x y) (iv   -- (+22 chars; full line in the Zenodo source)
  have ei : (ring (2 * m + 1 + 1)).mul (iv m (L4d m a x y)) (L4d m a x y) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hi
  have ej : (ring (2 * m + 1 + 1)).mul (iv m (X4n m a x y)) (X4n m a x y) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hj
  unfold cB at hc
  unfold L4d L4n X4d X4n X5d X5n at *
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))))) ((ring (2 * m + 1 + 1  -- (+257 chars; full line in the Zenodo source)
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)))) ((ring (2 * m + 1 + 1  -- (+261 chars; full line in the Zenodo source)
  have gri : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a (i))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul a ((ring (2 *  -- (+506 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 *  -- (+372 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 *  -- (+401 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring)
  have grj : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul j (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul j (y))))))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1))  -- (+537 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2   -- (+376 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2   -- (+405 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring)
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m   -- (+655 chars; full line in the Zenodo source)
  calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((rin  -- (+14394 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)))) ((ri  -- (+366 chars; full line in the Zenodo source)

set_option maxRecDepth 100000 in
set_option maxHeartbeats 10000000 in
theorem step4l (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y) (hi :   -- (+142 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul (L5n m a x y) (iv m (L5d m a x y)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (L4n m a x y) (iv m (L4d m a x y))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))))))  -- (+264 chars; full line in the Zenodo source)
  have ei : (ring (2 * m + 1 + 1)).mul (iv m (L5d m a x y)) (L5d m a x y) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hi
  have ej : (ring (2 * m + 1 + 1)).mul (iv m (L4d m a x y)) (L4d m a x y) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hj
  have ek : (ring (2 * m + 1 + 1)).mul (iv m (X5n m a x y)) (X5n m a x y) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hk
  unfold cB at hc
  unfold L4d L4n L5d L5n X5d X5n at *
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) (x)) (y)) = i at ei ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))))) ((ring (2 * m + 1 + 1  -- (+257 chars; full line in the Zenodo source)
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)))) ((ring (2 * m + 1 + 1  -- (+327 chars; full line in the Zenodo source)
  have gri : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul i (y)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul i (x)))) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul i (x))))) = (ring (2 * m +   -- (+18 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) (x)) (y))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) (x)) (y))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring)
  have grj : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a (j))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul a ((ring (2 *  -- (+506 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 *  -- (+372 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 *  -- (+401 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring)
  have grk : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul k (y)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul k (y)))))))) ((ring (2 * m + 1 +  -- (+599 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2   -- (+442 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ek]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2   -- (+471 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ek]; lib_ring)
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m   -- (+655 chars; full line in the Zenodo source)
  calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1  -- (+102704 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1  -- (+1438 chars; full line in the Zenodo source)

end G7LF

#print axioms G7LF.step4x
#print axioms G7LF.step4l

lang/G7S5.lean

lean_all/lang/G7S5.lean

/-
  lang/G7S5 — T43, cycle 370: step 5 → 6 of the order-7 orbit on E_t (G5 order 7, part 4).

    step5x   b·λ_5/x_5 = x_6                     (x-coordinate of τ(P + 5T))
    step5l   λ_6 = −(λ_5 + 1 − c) + b/x_6        (slope of P + 6T through T)
  with x_k = x(P + kT), λ_k = y(P + kT)/x(P + kT) the linear-fractional forms of lang/G7LF (x₀ = x, λ₀ = y/x, x₇ = x, λ₇ = y/x).
  Certificates (multipliers of the inverse relations and of the curve) from the std-lib Python reduction.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G7LF

namespace G7LF

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Step G4Gen G4Map G7Tate1

variable (m : Nat)

set_option maxRecDepth 100000 in
set_option maxHeartbeats 10000000 in
theorem step5x (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y) (hi :   -- (+142 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (L5n m a x y) (iv m (L5d m a x y))) ((ring (2 * m + 1 + 1)).mul (X5d m a x y) (iv m (X5n m a x y)))) = (ring (2 * m + 1 + 1)).mul (X6n m a x y) (iv   -- (+22 chars; full line in the Zenodo source)
  have ei : (ring (2 * m + 1 + 1)).mul (iv m (L5d m a x y)) (L5d m a x y) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hi
  have ej : (ring (2 * m + 1 + 1)).mul (iv m (X5n m a x y)) (X5n m a x y) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hj
  have ek : (ring (2 * m + 1 + 1)).mul (iv m (X6d m a x y)) (X6d m a x y) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hk
  unfold cB at hc
  unfold L5d L5n X5d X5n X6d X6n at *
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) (x)) (y)) = i at ei ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)))) ((ring (2 * m + 1 + 1  -- (+327 chars; full line in the Zenodo source)
  generalize iv m (y) = k at ek ⊢
  have gri : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul i (y)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul i (x)))) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul i (x))))) = (ring (2 * m +   -- (+18 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) (x)) (y))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) (x)) (y))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring)
  have grj : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul j (y)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul j (y)))))))) ((ring (2 * m + 1 +  -- (+599 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2   -- (+442 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2   -- (+471 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring)
  have grk : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul k (y)) = (ring (2 * m + 1 + 1)).zero := by
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k (y)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ek]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k (y)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ek]; lib_ring)
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m   -- (+655 chars; full line in the Zenodo source)
  calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (  -- (+169928 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 *  -- (+512 chars; full line in the Zenodo source)

set_option maxRecDepth 100000 in
set_option maxHeartbeats 10000000 in
theorem step5l (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y) (hi :   -- (+142 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul (L6n m a x y) (iv m (L6d m a x y)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (L5n m a x y) (iv m (L5d m a x y))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a)))))))  -- (+264 chars; full line in the Zenodo source)
  have ei : (ring (2 * m + 1 + 1)).mul (iv m (L6d m a x y)) (L6d m a x y) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hi
  have ej : (ring (2 * m + 1 + 1)).mul (iv m (L5d m a x y)) (L5d m a x y) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hj
  have ek : (ring (2 * m + 1 + 1)).mul (iv m (X6n m a x y)) (X6n m a x y) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hk
  unfold cB at hc
  unfold L5d L5n L6d L6n X6d X6n at *
  generalize iv m (x) = i at ei ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) (x)) (y)) = j at ej ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul   -- (+463 chars; full line in the Zenodo source)
  have gri : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul i (x)) = (ring (2 * m + 1 + 1)).zero := by
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i (x)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i (x)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring)
  have grj : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul j (y)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul j (x)))) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul j (x))))) = (ring (2 * m +   -- (+18 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) (x)) (y))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) (x)) (y))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring)
  have grk : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a (k))))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (  -- (+721 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) (  -- (+578 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ek]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (  -- (+607 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ek]; lib_ring)
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m   -- (+655 chars; full line in the Zenodo source)
  calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((r  -- (+41273 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1  -- (+917 chars; full line in the Zenodo source)

end G7LF

#print axioms G7LF.step5x
#print axioms G7LF.step5l

lang/G7S6.lean

lean_all/lang/G7S6.lean

/-
  lang/G7S6 — T43, cycle 370: step 6 → 7 of the order-7 orbit on E_t (G5 order 7, part 4).

    step6x   b·λ_6/x_6 = x_7                     (x-coordinate of τ(P + 6T))
    step6l   λ_7 = −(λ_6 + 1 − c) + b/x_7        (slope of P + 7T through T)
  with x_k = x(P + kT), λ_k = y(P + kT)/x(P + kT) the linear-fractional forms of lang/G7LF (x₀ = x, λ₀ = y/x, x₇ = x, λ₇ = y/x).
  Certificates (multipliers of the inverse relations and of the curve) from the std-lib Python reduction.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G7LF

namespace G7LF

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Step G4Gen G4Map G7Tate1

variable (m : Nat)

set_option maxRecDepth 100000 in
theorem step6x (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y) (hi :   -- (+93 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (L6n m a x y) (iv m (L6d m a x y))) ((ring (2 * m + 1 + 1)).mul (X6d m a x y) (iv m (X6n m a x y)))) = (ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m   -- (+20 chars; full line in the Zenodo source)
  have ei : (ring (2 * m + 1 + 1)).mul (iv m (L6d m a x y)) (L6d m a x y) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hi
  have ej : (ring (2 * m + 1 + 1)).mul (iv m (X6n m a x y)) (X6n m a x y) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hj
  unfold cB at hc
  unfold L6d L6n X6d X6n at *
  generalize iv m (x) = i at ei ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul   -- (+463 chars; full line in the Zenodo source)
  have gri : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul i (x)) = (ring (2 * m + 1 + 1)).zero := by
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i (x)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i (x)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring)
  have grj : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a (j))))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (  -- (+721 chars; full line in the Zenodo source)
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) (  -- (+578 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a)) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (  -- (+607 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring)
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m   -- (+655 chars; full line in the Zenodo source)
  calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (  -- (+22253 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).one) := by rw [gri, grj, gcv]; lib_ring

set_option maxRecDepth 100000 in
theorem step6l (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) x y) (hi :   -- (+34 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul (y) (iv m (x)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (L6n m a x y) (iv m (L6d m a x y))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))))) ((ring (2 * m + 1 +  -- (+249 chars; full line in the Zenodo source)
  have ei : (ring (2 * m + 1 + 1)).mul (iv m (x)) (x) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hi
  unfold cB at hc
  unfold L6d L6n at *
  generalize iv m (x) = i at ei ⊢
  have gri : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul i (x)) = (ring (2 * m + 1 + 1)).zero := by
    first
    | (calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i (x)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring)
    | (calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i (x)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring)
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m   -- (+655 chars; full line in the Zenodo source)
  calc (ring (2 * m + 1 + 1)).mul (y) (i) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 *  -- (+2888 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ri  -- (+725 chars; full line in the Zenodo source)

end G7LF

#print axioms G7LF.step6x
#print axioms G7LF.step6l

lang/G7Tate1.lean

lean_all/lang/G7Tate1.lean

/-
  lang/G7Tate1 — T43, cycle 367: Tate normal form E(b, c): y² + (1 − c)xy − by = x³ − bx², translation by T = (0, 0) (G5 order 7, part 1).

    tx7, ty7   P + T = (by/x², −(y/x + 1 − c)·by/x² + b)        (x ≠ 0)
    on7        P + T stays on E(b, c)
    ny, neg7   −P = (x, −y − (1 − c)x + b) is on E(b, c)
    mu7, inv7  τ(−τ(−Q)) = Q: translation by T has the explicit inverse Q ↦ −(−Q + T)
  For b = t³ − t², c = t² − t the point T has order 7; the later parts use P − kT = −((−P) + kT).
  Certificates from the std-lib Python reduction. Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G5Tate3

namespace G7Tate1

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Step G4Gen G4Map

variable (m : Nat)

def cB (b c x y : Zp (2 * m + 1 + 1)) : Prop := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg c)) ((ring (2 * m + 1 + 1)).mul x y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b y)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1))  -- (+133 chars; full line in the Zenodo source)

def tx7 (b _c x y : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul (iv m x) (iv m x)))
def ty7 (b c x y : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (iv m x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (c)))) ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul   -- (+26 chars; full line in the Zenodo source)
def ny (b c x y : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg c)) x))) b

/-- **P + T stays on E(b, c).** -/
theorem on7 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (b c x y : Zp (2 * m + 1 + 1)) (hc : cB m b c x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) :
    cB m b c (tx7 m b c x y) (ty7 m b c x y) := by
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  unfold cB at hc ⊢; unfold tx7 ty7
  generalize iv m x = i at ei ⊢
  have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (c))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (y)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m   -- (+238 chars; full line in the Zenodo source)
  calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (i)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (c)))) ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (y) ((rin  -- (+5394 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul (i) (i)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul (i) (i)))) ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul (i) (i)))))  -- (+328 chars; full line in the Zenodo source)

/-- **−P is on E(b, c).** -/
theorem neg7 (b c x y : Zp (2 * m + 1 + 1)) (hc : cB m b c x y) : cB m b c x (ny m b c x y) := by
  unfold cB at hc ⊢; unfold ny
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (c))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (y)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m   -- (+238 chars; full line in the Zenodo source)
  calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (y)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (c))) (x)))) (b)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring  -- (+1772 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (x) (x)))) := by rw [gcv]; lib_ring

theorem mu7 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (b c x w : Zp (2 * m + 1 + 1)) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hw : w ≠ (ring (2 * m + 1 + 1)).zero) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) :
    (ring (2 * m + 1 + 1)).mul (tx7 m b c x w) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).mul (iv m w) (iv m b))) = (ring (2 * m + 1 + 1)).one := by
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  have ej : (ring (2 * m + 1 + 1)).mul (iv m w) w = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr w hw
  have el : (ring (2 * m + 1 + 1)).mul (iv m b) b = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr b hb
  unfold tx7
  generalize iv m x = i at ei ⊢
  generalize iv m w = j at ej ⊢
  generalize iv m b = l at el ⊢
  have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have gjw : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (j) (w)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring
  have glb : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (l) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [el]; lib_ring
  calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (w) ((ring (2 * m + 1 + 1)).mul (i) (i)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).mul (j) (l))) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1   -- (+778 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).one := by rw [gix, gjw, glb]; lib_ring

/-- **τ(−τ(−Q)) = Q.** -/
theorem inv7 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (b c x y : Zp (2 * m + 1 + 1)) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hw : ny m b c x y ≠ (ring (2 * m + 1 + 1)).zero) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) :
    tx7 m b c (tx7 m b c x (ny m b c x y)) (ny m b c (tx7 m b c x (ny m b c x y)) (ty7 m b c x (ny m b c x y))) = x ∧
    ty7 m b c (tx7 m b c x (ny m b c x y)) (ny m b c (tx7 m b c x (ny m b c x y)) (ty7 m b c x (ny m b c x y))) = y := by
  have hyw : y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (ny m b c x y)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg c)) x))) b := by unfold ny; lib_ring
  generalize ny m b c x y = w at hw hyw ⊢
  have hU := iv_eq m hpr _ _ (mu7 m hpr b c x w hx hw hb)
  rw [show tx7 m b c (tx7 m b c x w) (ny m b c (tx7 m b c x w) (ty7 m b c x w)) =
      (ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul (ny m b c (tx7 m b c x w) (ty7 m b c x w)) ((ring (2 * m + 1 + 1)).mul (iv m (tx7 m b c x w)) (iv m (tx7 m b c x w)))) from rfl,
    show ty7 m b c (tx7 m b c x w) (ny m b c (tx7 m b c x w) (ty7 m b c x w)) =
      (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ny m b c (tx7 m b c x w) (ty7 m b c x w)) (iv m (tx7 m b c x w))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg c)))
        ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul (ny m b c (tx7 m b c x w) (ty7 m b c x w)) ((ring (2 * m + 1 + 1)).mul (iv m (tx7 m b c x w)) (iv m (tx7 m b c x w))))))) b from rfl, hU]
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  have ej : (ring (2 * m + 1 + 1)).mul (iv m w) w = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr w hw
  have el : (ring (2 * m + 1 + 1)).mul (iv m b) b = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr b hb
  unfold ny ty7 tx7
  generalize iv m x = i at ei ⊢
  generalize iv m w = j at ej ⊢
  generalize iv m b = l at el ⊢
  have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have gjw : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (j) (w)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring
  have glb : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (l) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [el]; lib_ring
  constructor
  · calc (ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (w) (i)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (c)))) ((ring   -- (+2608 chars; full line in the Zenodo source)
    _ = x := by rw [gix, gjw, glb]; lib_ring
  · rw [hyw]
    calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (w) (i)) ((ring (2 * m + 1 +  -- (+10678 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (w)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (c))) (x)))) (b) := by rw [gix, gjw, glb]; lib_ring

end G7Tate1

#print axioms G7Tate1.on7
#print axioms G7Tate1.neg7
#print axioms G7Tate1.mu7
#print axioms G7Tate1.inv7

lang/G7Tate2.lean

lean_all/lang/G7Tate2.lean

/-
  lang/G7Tate2 — T43, cycle 368: P + 2T on the Tate normal form E(b, c) (G5 order 7, part 2).

    step72   for P = (x, y) ∈ E(b, c), x, y, b ≠ 0:
             τ(τP) = P + 2T = (bx(x² − y)/y², −b(x³ − y²)(x² − y)/y³ + b)
  Certificate from the std-lib Python reduction. Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G7Tate1

namespace G7Tate2

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Step G4Gen G4Map G7Tate1

variable (m : Nat)

/-- **P + 2T.** -/
theorem step72 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (b c x y : Zp (2 * m + 1 + 1)) (hc : cB m b c x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hy : y ≠ (ring (2 * m + 1 + 1)).zero) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) :
    tx7 m b c (tx7 m b c x y) (ty7 m b c x y) = (ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).mul (iv m y) (iv m y)))) ∧ ty7 m b c (tx7 m b c x y) (ty7 m b c x y) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 *   -- (+438 chars; full line in the Zenodo source)
  have hU := iv_eq m hpr _ _ (mu7 m hpr b c x y hx hy hb)
  rw [show tx7 m b c (tx7 m b c x y) (ty7 m b c x y) = (ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul (ty7 m b c x y) ((ring (2 * m + 1 + 1)).mul (iv m (tx7 m b c x y)) (iv m (tx7 m b c x y)))) from rfl,
    show ty7 m b c (tx7 m b c x y) (ty7 m b c x y) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ty7 m b c x y) (iv m (tx7 m b c x y))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg c)))
      ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul (ty7 m b c x y) ((ring (2 * m + 1 + 1)).mul (iv m (tx7 m b c x y)) (iv m (tx7 m b c x y))))))) b from rfl, hU]
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  have ej : (ring (2 * m + 1 + 1)).mul (iv m y) y = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr y hy
  have el : (ring (2 * m + 1 + 1)).mul (iv m b) b = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr b hb
  unfold cB at hc
  unfold ty7
  generalize iv m x = i at ei ⊢
  generalize iv m y = j at ej ⊢
  generalize iv m b = l at el ⊢
  have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have gjy : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (j) (y)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring
  have glb : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (l) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [el]; lib_ring
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (c))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (y)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m   -- (+238 chars; full line in the Zenodo source)
  constructor
  · calc (ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (i)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (c)))) ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul  -- (+5761 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).mul (j) (j)))) := by rw [gix, glb, gcv]; lib_ring
  · calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (i)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (c)))) ((ring (2 *  -- (+40454 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (y) (y)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1  -- (+134 chars; full line in the Zenodo source)

end G7Tate2

#print axioms G7Tate2.step72

lang/G7Tate3.lean

lean_all/lang/G7Tate3.lean

/-
  lang/G7Tate3 — T43, cycle 369: P + 3T on the Tate normal form E(b, c) (G5 order 7, part 3).

    mu73     x(P + 2T) · y²/(x(x² − y)b) = 1
    step73   for P = (x, y) ∈ E(b, c) with x, y, b, x² − y ≠ 0, τ(P + 2T) = P + 3T =
               (yM/(x² − y)², b − KM/(x² − y)³),  M = by + cxy − bx²,  K = −bx³ + bxy + x²y − (1 − c)y²
  The y-coordinate goes through y(τQ) = −(Y/X + 1 − c)·x(τQ) + b, so its certificate is small.
  Certificates from the std-lib Python reduction. Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G7Tate2

namespace G7Tate3

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Step G4Gen G4Map G7Tate1

variable (m : Nat)

theorem mu73 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (b c x y : Zp (2 * m + 1 + 1)) (hc : cB m b c x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hy : y ≠ (ring (2 * m + 1 + 1)).zero) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) (hw : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg y) ≠ (ring (2 * m + 1 + 1)).zero) : (ring (2 * m + 1 +   -- (+525 chars; full line in the Zenodo source)
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  have ej : (ring (2 * m + 1 + 1)).mul (iv m y) y = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr y hy
  have el : (ring (2 * m + 1 + 1)).mul (iv m b) b = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr b hb
  have ek : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg y))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg y)) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hw
  unfold cB at hc
  generalize iv m x = i at ei ⊢
  generalize iv m y = j at ej ⊢
  generalize iv m b = l at el ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg y)) = k at ek ⊢
  have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have gjy : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (j) (y)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring
  have glb : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (l) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [el]; lib_ring
  have gkw : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (k) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ek]; lib_ring
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (c))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (y)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m   -- (+238 chars; full line in the Zenodo source)
  calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).mul (j) (j))))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul (i) ((ring (2 * m + 1 + 1)).mul (k) (l))))  -- (+1995 chars; full line in the Zenodo source)
  _ = (ring (2 * m + 1 + 1)).one := by rw [gix, gjy, glb, gkw]; lib_ring

/-- **P + 3T.** -/
theorem step73 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (b c x y : Zp (2 * m + 1 + 1)) (hc : cB m b c x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hy : y ≠ (ring (2 * m + 1 + 1)).zero) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) (hw : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg y) ≠ (ring (2 * m + 1 + 1)).zero) : tx7 m b c ((ring   -- (+3338 chars; full line in the Zenodo source)
  have hX := iv_eq m hpr _ _ (mu73 m hpr b c x y hc hx hy hb hw)
  have h1 : tx7 m b c ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).mul (iv m y) (iv m y))))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1  -- (+925 chars; full line in the Zenodo source)
    rw [show tx7 m b c ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).mul (iv m y) (iv m y))))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 +   -- (+1466 chars; full line in the Zenodo source)
    have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
    have ej : (ring (2 * m + 1 + 1)).mul (iv m y) y = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr y hy
    have el : (ring (2 * m + 1 + 1)).mul (iv m b) b = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr b hb
    have ek : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg y))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg y)) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hw
    unfold cB at hc
    generalize iv m x = i at ei ⊢
    generalize iv m y = j at ej ⊢
    generalize iv m b = l at el ⊢
    generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg y)) = k at ek ⊢
    have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
    have gjy : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (j) (y)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring
    have glb : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (l) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [el]; lib_ring
    have gkw : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (k) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ek]; lib_ring
    have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (c))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (y)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 *   -- (+240 chars; full line in the Zenodo source)
    calc (ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (y) (y)))) ((ring (2 * m + 1 + 1))  -- (+11009 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (b) (y)) ((ring (2 * m + 1 + 1)).mul (c) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (x) (x))))) ((ring (2 * m + 1 + 1)).mul (k) (k))) := by rw [gix, gjy, glb, gcv]  -- (+10 chars; full line in the Zenodo source)
  refine ⟨h1, ?_⟩
  rw [show ty7 m b c ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).mul (iv m y) (iv m y))))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)  -- (+2120 chars; full line in the Zenodo source)
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  have ej : (ring (2 * m + 1 + 1)).mul (iv m y) y = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr y hy
  have el : (ring (2 * m + 1 + 1)).mul (iv m b) b = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr b hb
  have ek : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg y))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg y)) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hw
  unfold cB at hc
  generalize iv m x = i at ei ⊢
  generalize iv m y = j at ej ⊢
  generalize iv m b = l at el ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg y)) = k at ek ⊢
  have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have gjy : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (j) (y)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring
  have glb : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (l) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [el]; lib_ring
  have gkw : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (k) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ek]; lib_ring
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (c))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (y)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m   -- (+238 chars; full line in the Zenodo source)
  calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x))) ((ring (2 *   -- (+26523 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x))))) ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m + 1 + 1)  -- (+649 chars; full line in the Zenodo source)

end G7Tate3

#print axioms G7Tate3.mu73
#print axioms G7Tate3.step73

lang/GEval.lean

lean_all/lang/GEval.lean

/-
  lang/GEval — T54, cycle 828 (step 3): **evaluation of the group ring K[C_(m+1)] at an (m+1)-th root of unity is a ring map**:
    gev z u = Σ_a u(a) z^a       (z^(m+1) = 1)
    pw_md       z^(n mod (m+1)) = z^n
    pw_val_add  z^((a + b).val) = z^a z^b
    gev_add, gev_mul, gev_mono
  With K = ℤ/p², z = the Teichmüller root of order ℓ, and ℤ[C_ℓ] → (ℤ/p²)[C_ℓ] coefficientwise, this is the map ι₂ of StickVal.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.GroupRingFrob
import lang.HasseCM
import lib.RingTac
import lib.Semi

namespace GEval

open FTAChain ZMod YMChain Frobenius GroupRing GroupRingFrob

variable {R : Type} (K : DetChain.CRing R) (m : Nat)

def gev (z : R) (u : V R (m + 1)) : R := sumK K (fun a => K.mul (gt K m u a) (pw K z a.val)) (L m)

theorem pw_mulq (z : R) (hz : pw K z (m + 1) = K.one) (r : Nat) : ∀ q : Nat, pw K z (r + q * (m + 1)) = pw K z r
  | 0 => by rw [Nat.zero_mul, Nat.add_zero]
  | q + 1 => by
    rw [show r + (q + 1) * (m + 1) = (r + q * (m + 1)) + (m + 1) by lib_nat, HasseCM.pw_add' K z _ (m + 1), hz, pw_mulq z hz r q]
    lib_ring

theorem pw_md (z : R) (hz : pw K z (m + 1) = K.one) (n : Nat) : pw K z (md m n) = pw K z n := by
  obtain ⟨q, hq⟩ := md_spec m n
  calc pw K z (md m n) = pw K z (md m n + q * (m + 1)) := (pw_mulq K m z hz (md m n) q).symm
    _ = pw K z n := by rw [← hq]

theorem pw_val_add (z : R) (hz : pw K z (m + 1) = K.one) (a b : Zp m) :
    pw K z ((ring m).add a b).val = K.mul (pw K z a.val) (pw K z b.val) := by
  show pw K z (md m (a.val + b.val)) = _
  rw [pw_md K m z hz, HasseCM.pw_add']

/-- **gev is multiplicative.** -/
theorem gev_mul (z : R) (hz : pw K z (m + 1) = K.one) (u v : V R (m + 1)) :
    gev K m z (gmul K m u v) = K.mul (gev K m z u) (gev K m z v) := by
  unfold gev
  have h1 : ∀ c : Zp m, K.mul (gt K m (gmul K m u v) c) (pw K z c.val) =
      sumK K (fun a => K.mul (gt K m u a) (K.mul (gt K m v ((ring m).add c ((ring m).neg a))) (pw K z c.val))) (L m) := fun c => by
    rw [gt_mul, sumK_mul]
    exact sumK_ext K _ _ (fun a => by lib_ring) _
  rw [sumK_ext K _ _ h1, sumK_swap, sumK_mul]
  apply sumK_ext
  intro a
  rw [← mul_sumK]
  have h2 : sumK K (fun c => K.mul (gt K m v ((ring m).add c ((ring m).neg a))) (pw K z c.val)) (L m) =
      K.mul (sumK K (fun b => K.mul (gt K m v b) (pw K z b.val)) (L m)) (pw K z a.val) := by
    rw [← shift K m (fun c => K.mul (gt K m v ((ring m).add c ((ring m).neg a))) (pw K z c.val)) a, sumK_mul]
    apply sumK_ext
    intro b
    show K.mul (gt K m v ((ring m).add ((ring m).add b a) ((ring m).neg a))) (pw K z ((ring m).add b a).val) = _
    rw [show (ring m).add ((ring m).add b a) ((ring m).neg a) = b by lib_ring, pw_val_add K m z hz]
    lib_ring
  rw [h2]
  lib_ring

theorem gev_add (z : R) (u v : V R (m + 1)) : gev K m z (gadd K m u v) = K.add (gev K m z u) (gev K m z v) := by
  unfold gev
  rw [← sumK_add]
  apply sumK_ext
  intro a
  unfold gadd
  rw [gt_tb]
  lib_ring

theorem map_sumK {S A : Type} (Ks : DetChain.CRing S) (f : R → S) (hadd : ∀ a b, f (K.add a b) = Ks.add (f a) (f b))
    (hzero : f K.zero = Ks.zero) (g : A → R) : ∀ l : List A, f (sumK K g l) = sumK Ks (fun a => f (g a)) l
  | [] => hzero
  | a :: l => by
    show f (K.add (g a) (sumK K g l)) = Ks.add (f (g a)) (sumK Ks (fun a => f (g a)) l)
    rw [hadd, map_sumK Ks f hadd hzero g l]

/-- **change of coefficients** along a ring map f : R → S, coefficientwise. -/
def gmap {S : Type} (f : R → S) (u : V R (m + 1)) : V S (m + 1) := tb m (fun a => f (gt K m u a))

theorem gmap_mul {S : Type} (Ks : DetChain.CRing S) (f : R → S) (hadd : ∀ a b, f (K.add a b) = Ks.add (f a) (f b))
    (hmul : ∀ a b, f (K.mul a b) = Ks.mul (f a) (f b)) (hzero : f K.zero = Ks.zero) (u v : V R (m + 1)) :
    gmap K m f (gmul K m u v) = gmul Ks m (gmap K m f u) (gmap K m f v) := ext Ks m _ _ (fun c => by
  unfold gmap
  rw [gt_tb, gt_mul, gt_mul, map_sumK K Ks f hadd hzero]
  apply sumK_ext
  intro a
  rw [hmul, gt_tb, gt_tb])

theorem gmap_add {S : Type} (Ks : DetChain.CRing S) (f : R → S) (hadd : ∀ a b, f (K.add a b) = Ks.add (f a) (f b))
    (u v : V R (m + 1)) : gmap K m f (gadd K m u v) = gadd Ks m (gmap K m f u) (gmap K m f v) := ext Ks m _ _ (fun c => by
  unfold gmap gadd
  repeat rw [gt_tb]
  exact hadd _ _)

end GEval

#print axioms GEval.pw_mulq
#print axioms GEval.pw_md
#print axioms GEval.pw_val_add
#print axioms GEval.gev_mul
#print axioms GEval.map_sumK
#print axioms GEval.gev_add
#print axioms GEval.gmap_mul
#print axioms GEval.gmap_add

lang/GL2Local.lean

lean_all/lang/GL2Local.lean

/-
  lang/GL2Local — T43, cycle 4 (GL2 over ℚ): the place/read split at one prime.

  Automorphic side at p: sequences a(p^n) obeying the Hecke recursion
      a(p^(n+2)) = t · a(p^(n+1)) − d · a(p^n),   a(1) = 1,  a(p) = t,     (d = p^(k−1) χ(p))
  Galois side at p: the characteristic data (t, d) = (tr, det) of ρ(Frob_p), with eigenvalues α, β.
    place (t, d)          = the sequence h t d  (h 0 = 1, h 1 = t, Hecke recursion)
    read  a               = (a(p), a(p)^2 − a(p^2))
    read_place            read (place (t, d)) = (t, d)                       (unit iso)
    fix_iff               a = place (read a)  ↔  a obeys the Hecke recursion  (counit iso on Fix)
    h_symm                with t = α + β, d = α β:  h t d n = Σ_{i ≤ n} α^i β^(n−i) = tr Sym^n ρ(Frob_p)
  So at each unramified p "Hecke eigenvalues a(p^n)" and "traces of Sym^n of Frobenius" are the same
  data, in every commutative ring. Checked exactly on Δ (τ(p^n), p ≤ 7) before the proof.

  Lean 4 core only, proofs by hand, no axioms.
-/
import DetChain
import lang.Frobenius

namespace GL2Local

open Frobenius

variable {R : Type} (K : DetChain.CRing R)

def sub (x y : R) : R := K.add x (K.neg y)

/-- `place (t, d)`: the Hecke-recursive sequence. -/
def h (t d : R) : Nat → R
  | 0 => K.one
  | 1 => t
  | n + 2 => sub K (K.mul t (h t d (n + 1))) (K.mul d (h t d n))

/-- `read a = (a 1, a 1 ^ 2 − a 2)`. -/
def readTD (a : Nat → R) : R × R := (a 1, sub K (K.mul (a 1) (a 1)) (a 2))

def Hecke (a : Nat → R) : Prop :=
  a 0 = K.one ∧ ∀ n, a (n + 2) = sub K (K.mul (a 1) (a (n + 1))) (K.mul (sub K (K.mul (a 1) (a 1)) (a 2)) (a n))

theorem sub_sub_self (x y : R) : sub K x (sub K x y) = y := by
  show K.add x (K.neg (K.add x (K.neg y))) = y
  have e : K.neg (K.add x (K.neg y)) = K.add (K.neg x) y := by
    apply (DetChain.neg_unique K _ _ _).symm
    rw [K.add_assoc, ← K.add_assoc y x (K.neg y), K.add_comm y x, K.add_assoc x y, K.add_neg y,
      DetChain.add_zero', DetChain.add_neg']
  rw [e, ← K.add_assoc, K.add_neg, K.zero_add]

theorem read_place (t d : R) : readTD K (h K t d) = (t, d) := by
  show (t, sub K (K.mul t t) (h K t d 2)) = (t, d)
  have h2 : h K t d 2 = sub K (K.mul t t) (K.mul d K.one) := rfl
  rw [h2, K.mul_comm d K.one, K.one_mul, sub_sub_self]

theorem place_hecke (t d : R) : Hecke K (h K t d) := by
  refine ⟨rfl, fun n => ?_⟩
  have e := congrArg Prod.snd (read_place K t d)
  show h K t d (n + 2) = sub K (K.mul t (h K t d (n + 1))) (K.mul (sub K (K.mul t t) (h K t d 2)) (h K t d n))
  have e' : sub K (K.mul t t) (h K t d 2) = d := e
  rw [e']
  rfl

theorem hecke_eq_place (a : Nat → R) (ha : Hecke K a) :
    ∀ n, a n = h K (readTD K a).1 (readTD K a).2 n
  | 0 => ha.1
  | 1 => rfl
  | n + 2 => by
    rw [ha.2 n, hecke_eq_place a ha (n + 1), hecke_eq_place a ha n]
    rfl

/-- **Counit iso on the fixed points**: `a` is `place (read a)` iff it obeys the Hecke recursion. -/
theorem fix_iff (a : Nat → R) : (∀ n, a n = h K (readTD K a).1 (readTD K a).2 n) ↔ Hecke K a := by
  constructor
  · intro hf
    have hp := place_hecke K (readTD K a).1 (readTD K a).2
    refine ⟨(hf 0).trans hp.1, fun n => ?_⟩
    rw [hf (n + 2), hf (n + 1), hf n, hp.2 n]
    have r := read_place K (readTD K a).1 (readTD K a).2
    have r1 : h K (readTD K a).1 (readTD K a).2 1 = (readTD K a).1 := rfl
    have r2 : sub K (K.mul (h K (readTD K a).1 (readTD K a).2 1) (h K (readTD K a).1 (readTD K a).2 1))
        (h K (readTD K a).1 (readTD K a).2 2) = (readTD K a).2 := congrArg Prod.snd r
    rw [r1] at r2 |-
    rw [r2]
    show sub K (K.mul (a 1) (h K (readTD K a).1 (readTD K a).2 (n + 1))) (K.mul (readTD K a).2 (h K (readTD K a).1 (readTD K a).2 n))
      = sub K (K.mul (a 1) (h K (readTD K a).1 (readTD K a).2 (n + 1))) (K.mul (sub K (K.mul (a 1) (a 1)) (a 2)) (h K (readTD K a).1 (readTD K a).2 n))
    rfl
  · exact hecke_eq_place K a

/-- `Σ_{i ≤ n} α^i β^(n−i)`, by `s (n+1) = α^(n+1) + β · s n`. -/
def symTr (α β : R) : Nat → R
  | 0 => K.one
  | n + 1 => K.add (pw K α (n + 1)) (K.mul β (symTr α β n))

/-- **Hecke eigenvalues are symmetric-power traces of Frobenius.** -/
theorem h_symm (α β : R) : ∀ n, h K (K.add α β) (K.mul α β) n = symTr K α β n
  | 0 => rfl
  | 1 => by
    show K.add α β = K.add (K.mul K.one α) (K.mul β K.one)
    rw [K.one_mul, K.mul_comm β K.one, K.one_mul]
  | n + 2 => by
    show sub K (K.mul (K.add α β) (h K (K.add α β) (K.mul α β) (n + 1)))
        (K.mul (K.mul α β) (h K (K.add α β) (K.mul α β) n))
      = K.add (pw K α (n + 2)) (K.mul β (symTr K α β (n + 1)))
    rw [h_symm α β (n + 1), h_symm α β n]
    -- α s(n+1) = α^(n+2) + αβ s(n)
    have e1 : K.mul α (symTr K α β (n + 1)) = K.add (pw K α (n + 2)) (K.mul (K.mul α β) (symTr K α β n)) := by
      show K.mul α (K.add (pw K α (n + 1)) (K.mul β (symTr K α β n))) = _
      rw [K.distrib, K.mul_comm α (pw K α (n + 1)), ← K.mul_assoc]
      rfl
    show K.add (K.mul (K.add α β) (symTr K α β (n + 1))) (K.neg (K.mul (K.mul α β) (symTr K α β n))) = _
    rw [DetChain.right_distrib, e1, K.add_assoc, K.add_assoc, K.add_comm (K.mul β (symTr K α β (n + 1))),
      ← K.add_assoc (K.mul (K.mul α β) (symTr K α β n)), K.add_neg, K.zero_add]

end GL2Local

#print axioms GL2Local.sub_sub_self
#print axioms GL2Local.read_place
#print axioms GL2Local.place_hecke
#print axioms GL2Local.hecke_eq_place
#print axioms GL2Local.fix_iff
#print axioms GL2Local.h_symm

lang/GLnLocal.lean

lean_all/lang/GLnLocal.lean

/-
  lang/GLnLocal — T43, cycle 6 (GL_n at an unramified place, every n).

  Satake / Frobenius data α = (α_1, …, α_n) (a list in any commutative ring).
    cf α     coefficients of Π (1 − α_i X)        (cf (a :: α) (k+1) = cf α (k+1) − a · cf α k)
    hk α     complete homogeneous h_k(α)          (hk (a :: α) (k+1) = hk α (k+1) + a · hk (a :: α) k)
             = tr Sym^k of diag(α) = the Hecke eigenvalue of T_(p^k) for GL_n
    conv     Cauchy product of sequences, by conv c h (k+1) = c 0 · h (k+1) + conv (c ∘ succ) h k
  Proved:
    conv_cf_hk     conv (cf α) (hk α) = δ_0, i.e.  Σ_k h_k X^k = 1 / Π (1 − α_i X)  (the local L-factor)
    conv_unique    if c 0 = 1, the sequence h with conv c h = δ_0 is unique
  So at an unramified p the Hecke eigen-system (a(p^k))_k and the Satake polynomial Π (1 − α_i X)
  determine each other (place = solve conv c h = δ, read = the polynomial), for every n.

  Lean 4 core only, proofs by hand, no axioms.
-/
import DetChain

namespace GLnLocal

variable {R : Type} (K : DetChain.CRing R)

def cf : List R → Nat → R
  | [], 0 => K.one
  | [], _ + 1 => K.zero
  | _ :: α, 0 => cf α 0
  | a :: α, k + 1 => K.add (cf α (k + 1)) (K.neg (K.mul a (cf α k)))

/-- One more variable: `hkc a H 0 = H 0`, `hkc a H (k+1) = H (k+1) + a · hkc a H k`. -/
def hkc (a : R) (H : Nat → R) : Nat → R
  | 0 => H 0
  | k + 1 => K.add (H (k + 1)) (K.mul a (hkc a H k))

def hk : List R → Nat → R
  | [] => fun k => match k with
    | 0 => K.one
    | _ + 1 => K.zero
  | a :: α => hkc K a (hk α)

/-- Cauchy product. -/
def conv : (Nat → R) → (Nat → R) → Nat → R
  | c, h, 0 => K.mul (c 0) (h 0)
  | c, h, k + 1 => K.add (K.mul (c 0) (h (k + 1))) (conv (fun i => c (i + 1)) h k)

/-- Shift right: `(sR h) 0 = 0`, `(sR h) (k+1) = h k`. -/
def sR (h : Nat → R) : Nat → R
  | 0 => K.zero
  | k + 1 => h k

def delta : Nat → R
  | 0 => K.one
  | _ + 1 => K.zero

theorem conv_ext (c h g : Nat → R) (hg : ∀ i, h i = g i) : ∀ k, conv K c h k = conv K c g k := by
  intro k
  induction k generalizing c with
  | zero => show K.mul (c 0) (h 0) = K.mul (c 0) (g 0); rw [hg 0]
  | succ k ih =>
    show K.add (K.mul (c 0) (h (k + 1))) (conv K (fun i => c (i + 1)) h k)
      = K.add (K.mul (c 0) (g (k + 1))) (conv K (fun i => c (i + 1)) g k)
    rw [hg (k + 1), ih]

theorem conv_ext_left (c d h : Nat → R) (hcd : ∀ i, c i = d i) : ∀ k, conv K c h k = conv K d h k := by
  intro k
  induction k generalizing c d with
  | zero => show K.mul (c 0) (h 0) = K.mul (d 0) (h 0); rw [hcd 0]
  | succ k ih =>
    show K.add (K.mul (c 0) (h (k + 1))) (conv K (fun i => c (i + 1)) h k)
      = K.add (K.mul (d 0) (h (k + 1))) (conv K (fun i => d (i + 1)) h k)
    rw [hcd 0, ih (fun i => c (i + 1)) (fun i => d (i + 1)) (fun i => hcd (i + 1))]

theorem conv_add (c h g : Nat → R) : ∀ k, conv K c (fun i => K.add (h i) (g i)) k = K.add (conv K c h k) (conv K c g k) := by
  intro k
  induction k generalizing c with
  | zero => exact K.distrib _ _ _
  | succ k ih =>
    show K.add (K.mul (c 0) (K.add (h (k + 1)) (g (k + 1)))) (conv K (fun i => c (i + 1)) (fun i => K.add (h i) (g i)) k)
      = K.add (K.add (K.mul (c 0) (h (k + 1))) (conv K (fun i => c (i + 1)) h k))
          (K.add (K.mul (c 0) (g (k + 1))) (conv K (fun i => c (i + 1)) g k))
    rw [ih, K.distrib, DetChain.add_mid4]

theorem conv_add_left (c d h : Nat → R) : ∀ k, conv K (fun i => K.add (c i) (d i)) h k = K.add (conv K c h k) (conv K d h k) := by
  intro k
  induction k generalizing c d with
  | zero => exact DetChain.right_distrib K _ _ _
  | succ k ih =>
    show K.add (K.mul (K.add (c 0) (d 0)) (h (k + 1))) (conv K (fun i => K.add (c (i + 1)) (d (i + 1))) h k)
      = K.add (K.add (K.mul (c 0) (h (k + 1))) (conv K (fun i => c (i + 1)) h k))
          (K.add (K.mul (d 0) (h (k + 1))) (conv K (fun i => d (i + 1)) h k))
    rw [ih (fun i => c (i + 1)) (fun i => d (i + 1)), DetChain.right_distrib, DetChain.add_mid4]

theorem conv_smul_left (c h : Nat → R) (a : R) : ∀ k, conv K (fun i => K.mul a (c i)) h k = K.mul a (conv K c h k) := by
  intro k
  induction k generalizing c with
  | zero => show K.mul (K.mul a (c 0)) (h 0) = K.mul a (K.mul (c 0) (h 0)); exact K.mul_assoc _ _ _
  | succ k ih =>
    show K.add (K.mul (K.mul a (c 0)) (h (k + 1))) (conv K (fun i => K.mul a (c (i + 1))) h k)
      = K.mul a (K.add (K.mul (c 0) (h (k + 1))) (conv K (fun i => c (i + 1)) h k))
    rw [ih (fun i => c (i + 1)), K.distrib, K.mul_assoc]

theorem conv_smul (c h : Nat → R) (a : R) : ∀ k, conv K c (fun i => K.mul a (h i)) k = K.mul a (conv K c h k) := by
  intro k
  induction k generalizing c with
  | zero =>
    show K.mul (c 0) (K.mul a (h 0)) = K.mul a (K.mul (c 0) (h 0))
    rw [← K.mul_assoc, K.mul_comm (c 0) a, K.mul_assoc]
  | succ k ih =>
    show K.add (K.mul (c 0) (K.mul a (h (k + 1)))) (conv K (fun i => c (i + 1)) (fun i => K.mul a (h i)) k)
      = K.mul a (K.add (K.mul (c 0) (h (k + 1))) (conv K (fun i => c (i + 1)) h k))
    rw [ih, K.distrib, ← K.mul_assoc, K.mul_comm (c 0) a, K.mul_assoc]

theorem conv_zero_left (c h : Nat → R) (hc : ∀ i, c i = K.zero) : ∀ k, conv K c h k = K.zero := by
  intro k
  induction k generalizing c with
  | zero => show K.mul (c 0) (h 0) = K.zero; rw [hc 0, DetChain.zero_mul']
  | succ k ih =>
    show K.add (K.mul (c 0) (h (k + 1))) (conv K (fun i => c (i + 1)) h k) = K.zero
    rw [hc 0, DetChain.zero_mul', ih (fun i => c (i + 1)) (fun i => hc (i + 1)), K.zero_add]

theorem conv_sR_right (c h : Nat → R) : ∀ k, conv K c (sR K h) (k + 1) = conv K c h k := by
  intro k
  induction k generalizing c with
  | zero =>
    show K.add (K.mul (c 0) (h 0)) (K.mul (c 1) K.zero) = K.mul (c 0) (h 0)
    rw [DetChain.mul_zero, DetChain.add_zero']
  | succ k ih =>
    show K.add (K.mul (c 0) (h (k + 1))) (conv K (fun i => c (i + 1)) (sR K h) (k + 1))
      = K.add (K.mul (c 0) (h (k + 1))) (conv K (fun i => c (i + 1)) h k)
    rw [ih]

theorem conv_sR_right0 (c h : Nat → R) : conv K c (sR K h) 0 = K.zero := DetChain.mul_zero K _

/-- `conv c (sR h) = sR (conv c h)` pointwise. -/
theorem conv_sR (c h : Nat → R) : ∀ k, conv K c (sR K h) k = sR K (conv K c h) k
  | 0 => conv_sR_right0 K c h
  | k + 1 => conv_sR_right K c h k

theorem cf_cons (a : R) (α : List R) (k : Nat) :
    cf K (a :: α) k = K.add (cf K α k) (K.neg (K.mul a (sR K (cf K α) k))) := by
  cases k with
  | zero =>
    show cf K α 0 = K.add (cf K α 0) (K.neg (K.mul a K.zero))
    rw [DetChain.mul_zero]
    have : K.neg K.zero = K.zero := (DetChain.neg_unique K _ _ (K.zero_add K.zero)).symm
    rw [this, DetChain.add_zero']
  | succ k => rfl

theorem hk_cons (a : R) (α : List R) (k : Nat) :
    hk K (a :: α) k = K.add (hk K α k) (K.mul a (sR K (hk K (a :: α)) k)) := by
  cases k with
  | zero =>
    show hk K α 0 = K.add (hk K α 0) (K.mul a K.zero)
    rw [DetChain.mul_zero, DetChain.add_zero']
  | succ k => rfl

theorem neg_as_mul (x : R) : K.neg x = K.mul (K.neg K.one) x := by
  rw [DetChain.neg_mul_left, K.one_mul]

/-- **The local L-factor**: `Σ h_k(α) X^k · Π (1 − α_i X) = 1`. -/
theorem conv_cf_hk : ∀ (α : List R) (k : Nat), conv K (cf K α) (hk K α) k = delta K k
  | [], 0 => K.one_mul K.one
  | [], k + 1 => by
    show K.add (K.mul K.one K.zero) (conv K (fun i => cf K [] (i + 1)) (hk K []) k) = K.zero
    rw [DetChain.mul_zero, K.zero_add]
    exact conv_zero_left K _ _ (fun i => rfl) k
  | a :: α, k => by
    -- D = conv (cf α) (hk (a::α)) satisfies D = δ + a sR D
    have hD : ∀ j, conv K (cf K α) (hk K (a :: α)) j
        = K.add (delta K j) (K.mul a (sR K (conv K (cf K α) (hk K (a :: α))) j)) := fun j => by
      rw [conv_ext K _ _ _ (hk_cons K a α) j, conv_add, conv_smul, conv_cf_hk α j, conv_sR]
    -- conv (cf (a::α)) H' = D − a sR D
    rw [conv_ext_left K _ _ _ (cf_cons K a α) k, conv_add_left,
      conv_ext_left K (fun i => K.neg (K.mul a (sR K (cf K α) i))) (fun i => K.mul (K.neg a) (sR K (cf K α) i)) _
        (fun i => by rw [neg_as_mul K, ← K.mul_assoc, ← neg_as_mul K]) k,
      conv_smul_left]
    -- conv (sR c) h = sR (conv c h)
    have hsl : ∀ j, conv K (sR K (cf K α)) (hk K (a :: α)) j = sR K (conv K (cf K α) (hk K (a :: α))) j := by
      intro j
      cases j with
      | zero => show K.mul K.zero _ = K.zero; exact DetChain.zero_mul' K _
      | succ j =>
        show K.add (K.mul K.zero _) (conv K (fun i => sR K (cf K α) (i + 1)) (hk K (a :: α)) j) = _
        rw [DetChain.zero_mul', K.zero_add]
        exact conv_ext_left K _ _ _ (fun i => rfl) j
    rw [hsl k, hD k, K.add_assoc, DetChain.neg_mul_left K a, K.add_neg, DetChain.add_zero']

/-- `conv d h m` depends only on `h 0, …, h m`. -/
theorem conv_dep (d h g : Nat → R) : ∀ m, (∀ j, j ≤ m → h j = g j) → conv K d h m = conv K d g m := by
  intro m
  induction m generalizing d with
  | zero => intro hj; show K.mul (d 0) (h 0) = K.mul (d 0) (g 0); rw [hj 0 (Nat.le_refl 0)]
  | succ m ih =>
    intro hj
    show K.add (K.mul (d 0) (h (m + 1))) (conv K (fun i => d (i + 1)) h m)
      = K.add (K.mul (d 0) (g (m + 1))) (conv K (fun i => d (i + 1)) g m)
    rw [hj (m + 1) (Nat.le_refl _), ih (fun i => d (i + 1)) (fun j hjm => hj j (Nat.le_succ_of_le hjm))]

/-- **Uniqueness**: with `c 0 = 1`, `conv c h = δ` determines `h`. -/
theorem conv_unique (c h g : Nat → R) (hc : c 0 = K.one)
    (hh : ∀ k, conv K c h k = delta K k) (hg : ∀ k, conv K c g k = delta K k) : ∀ k, ∀ j, j ≤ k → h j = g j := by
  intro k
  induction k with
  | zero =>
    intro j hj
    have hj0 : j = 0 := Nat.le_zero.mp hj
    rw [hj0]
    have e := (hh 0).trans (hg 0).symm
    show h 0 = g 0
    have e' : K.mul (c 0) (h 0) = K.mul (c 0) (g 0) := e
    rw [hc, K.one_mul, K.one_mul] at e'
    exact e'
  | succ k ih =>
    intro j hj
    cases Nat.lt_or_eq_of_le hj with
    | inl hlt => exact ih j (Nat.le_of_lt_succ hlt)
    | inr heq =>
      rw [heq]
      have e := (hh (k + 1)).trans (hg (k + 1)).symm
      have e' : K.add (K.mul (c 0) (h (k + 1))) (conv K (fun i => c (i + 1)) h k)
          = K.add (K.mul (c 0) (g (k + 1))) (conv K (fun i => c (i + 1)) g k) := e
      rw [conv_dep K _ h g k ih, hc, K.one_mul, K.one_mul] at e'
      have := congrArg (fun t => K.add t (K.neg (conv K (fun i => c (i + 1)) g k))) e'
      show h (k + 1) = g (k + 1)
      have t1 : K.add (K.add (h (k + 1)) (conv K (fun i => c (i + 1)) g k)) (K.neg (conv K (fun i => c (i + 1)) g k))
          = K.add (K.add (g (k + 1)) (conv K (fun i => c (i + 1)) g k)) (K.neg (conv K (fun i => c (i + 1)) g k)) := this
      rw [K.add_assoc, K.add_neg, DetChain.add_zero', K.add_assoc, K.add_neg, DetChain.add_zero'] at t1
      exact t1

end GLnLocal

#print axioms GLnLocal.conv_ext
#print axioms GLnLocal.conv_ext_left
#print axioms GLnLocal.conv_add
#print axioms GLnLocal.conv_add_left
#print axioms GLnLocal.conv_smul_left
#print axioms GLnLocal.conv_smul
#print axioms GLnLocal.conv_zero_left
#print axioms GLnLocal.conv_sR_right
#print axioms GLnLocal.conv_sR_right0
#print axioms GLnLocal.conv_sR
#print axioms GLnLocal.cf_cons
#print axioms GLnLocal.hk_cons
#print axioms GLnLocal.neg_as_mul
#print axioms GLnLocal.conv_cf_hk
#print axioms GLnLocal.conv_dep
#print axioms GLnLocal.conv_unique

lang/Gauss5.lean

lean_all/lang/Gauss5.lean

/-
  lang/Gauss5 — T43, cycle 198: the Gauss sum of conductor 5 in 𝔽_q[ζ₅]: 5 a square mod q ⇒ q ≡ ±1 (mod 5).

  In C = 𝔽_q[ζ]/(Φ₅) (q = 2r + 3 prime): ζ⁵ = 1, g = ζ − ζ² − ζ³ + ζ⁴ = (−1, 0, −2, −2) has g² = 5, and in characteristic q
    g^q = ζ^q − ζ^(2q) − ζ^(3q) + ζ^(4q) = ±g          (Frobenius is additive; ζ^q depends on q mod 5)
  with the sign − when q ≡ 2, 3 (mod 5). If u² = 5 then g^(q−1) = 5^((q−1)/2) = 1, so g^q = g; g = −g is impossible (2 ≠ 0).
    sq5_mod5     q prime ≠ 5, u² = 5 in 𝔽_q ⇒ q ≡ 1 or 4 (mod 5)

  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Cyclo5
import lang.CubeInert
import lang.FinPow2
import lang.EisenDiv

namespace Gauss5

open FTAChain ZMod Frobenius
open LibRing (castZ castN)

variable (r : Nat)

theorem nsm_c5 (x : Zp (2 * r + 1 + 1) × Zp (2 * r + 1 + 1) × Zp (2 * r + 1 + 1) × Zp (2 * r + 1 + 1)) : ∀ k,
    nsm (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) k x = (nsm (ring (2 * r + 1 + 1)) k x.1, nsm (ring (2 * r + 1 + 1)) k x.2.1, nsm (ring (2 * r + 1 + 1)) k x.2.2.1, nsm (ring (2 * r + 1 + 1)) k x.2.2.2)
  | 0 => rfl
  | k + 1 => by
    show (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).add (nsm (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) k x) x = _
    rw [nsm_c5 x k]; rfl

theorem char_c5 : nsm (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (2 * r + 1 + 1 + 1) (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).one = (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).zero := by
  show nsm (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (2 * r + 1 + 1 + 1) ((ring (2 * r + 1 + 1)).one, (ring (2 * r + 1 + 1)).zero, (ring (2 * r + 1 + 1)).zero, (ring (2 * r + 1 + 1)).zero) = ((ring (2 * r + 1 + 1)).zero, (ring (2 * r + 1 + 1)).zero, (ring (2 * r + 1 + 1)).zero, (ring (2 * r + 1 + 1)).zero)
  rw [nsm_c5]
  show (nsm (ring (2 * r + 1 + 1)) (2 * r + 1 + 1 + 1) (ring (2 * r + 1 + 1)).one, nsm (ring (2 * r + 1 + 1)) (2 * r + 1 + 1 + 1) (ring (2 * r + 1 + 1)).zero, nsm (ring (2 * r + 1 + 1)) (2 * r + 1 + 1 + 1) (ring (2 * r + 1 + 1)).zero, nsm (ring (2 * r + 1 + 1)) (2 * r + 1 + 1 + 1) (ring (2 * r + 1 + 1)).zero) = _
  rw [char_p, FinPow2.nsm_zeroG]

def zz : Zp (2 * r + 1 + 1) × Zp (2 * r + 1 + 1) × Zp (2 * r + 1 + 1) × Zp (2 * r + 1 + 1) := ((ring (2 * r + 1 + 1)).zero, (ring (2 * r + 1 + 1)).one, (ring (2 * r + 1 + 1)).zero, (ring (2 * r + 1 + 1)).zero)

theorem zmul (x : Zp (2 * r + 1 + 1) × Zp (2 * r + 1 + 1) × Zp (2 * r + 1 + 1) × Zp (2 * r + 1 + 1)) : (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).mul (zz r) x = ((ring (2 * r + 1 + 1)).neg (x.2.2.2), (ring (2 * r + 1 + 1)).add (x.1) ((ring (2 * r + 1 + 1)).neg (x.2.2.2)), (ring (2 * r + 1 + 1)).add (x.2.1) ((ring (2 * r + 1 + 1)).neg (x.2.2.2)), (ring (2 * r + 1 + 1)).add (x.2.2.1) ((ring (2 * r + 1 + 1  -- (+24 chars; full line in the Zenodo source)
  apply Prod.ext
  · show (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1 + 1)).zero) (x.1)) ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1 + 1)).one) (x.2.2.2)) ((ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1 + 1)).zero) (x.2.2.1))) ((ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1 + 1  -- (+236 chars; full line in the Zenodo source)
  · apply Prod.ext
    · show (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1 + 1)).zero) (x.2.1)) ((ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1 + 1)).one) (x.1))) ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1 + 1)).one) (x.2.2.2)) ((ring (2 * r + 1 + 1)  -- (+270 chars; full line in the Zenodo source)
    · apply Prod.ext
      · show (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1 + 1)).zero) (x.2.2.1)) ((ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1 + 1)).one) (x.2.1))) ((ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1 + 1)).zero) (x.1))) ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1   -- (+274 chars; full line in the Zenodo source)
      · show (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1 + 1)).zero) (x.2.2.2)) ((ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1 + 1)).one) (x.2.2.1))) ((ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1 + 1)).zero) (x.2.1))) ((ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1 + 1)).zero) (x.1))) ((ri  -- (+374 chars; full line in the Zenodo source)

theorem pw_z_succ (n : Nat) : pw (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (zz r) (n + 1) = (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).mul (zz r) (pw (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (zz r) n) := (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).mul_comm _ _

theorem zp1 : pw (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (zz r) 1 = ((ring (2 * r + 1 + 1)).zero, (ring (2 * r + 1 + 1)).one, (ring (2 * r + 1 + 1)).zero, (ring (2 * r + 1 + 1)).zero) := (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).one_mul _

theorem zp2 : pw (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (zz r) 2 = ((ring (2 * r + 1 + 1)).zero, (ring (2 * r + 1 + 1)).zero, (ring (2 * r + 1 + 1)).one, (ring (2 * r + 1 + 1)).zero) := by
  rw [pw_z_succ r 1, zp1, zmul]
  apply Prod.ext
  · show (ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).zero) = (ring (2 * r + 1 + 1)).zero; lib_ring
  · apply Prod.ext
    · show (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).zero) ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).zero)) = (ring (2 * r + 1 + 1)).zero; lib_ring
    · apply Prod.ext
      · show (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).one) ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).zero)) = (ring (2 * r + 1 + 1)).one; lib_ring
      · show (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).zero) ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).zero)) = (ring (2 * r + 1 + 1)).zero; lib_ring

theorem zp3 : pw (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (zz r) 3 = ((ring (2 * r + 1 + 1)).zero, (ring (2 * r + 1 + 1)).zero, (ring (2 * r + 1 + 1)).zero, (ring (2 * r + 1 + 1)).one) := by
  rw [pw_z_succ r 2, zp2, zmul]
  apply Prod.ext
  · show (ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).zero) = (ring (2 * r + 1 + 1)).zero; lib_ring
  · apply Prod.ext
    · show (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).zero) ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).zero)) = (ring (2 * r + 1 + 1)).zero; lib_ring
    · apply Prod.ext
      · show (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).zero) ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).zero)) = (ring (2 * r + 1 + 1)).zero; lib_ring
      · show (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).one) ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).zero)) = (ring (2 * r + 1 + 1)).one; lib_ring

theorem zp4 : pw (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (zz r) 4 = ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one), (ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one), (ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one), (ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one)) := by
  rw [pw_z_succ r 3, zp3, zmul]
  apply Prod.ext
  · show (ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one) = (ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one); lib_ring
  · apply Prod.ext
    · show (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).zero) ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one)) = (ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one); lib_ring
    · apply Prod.ext
      · show (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).zero) ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one)) = (ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one); lib_ring
      · show (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).zero) ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one)) = (ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one); lib_ring

theorem zp5 : pw (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (zz r) 5 = ((ring (2 * r + 1 + 1)).one, (ring (2 * r + 1 + 1)).zero, (ring (2 * r + 1 + 1)).zero, (ring (2 * r + 1 + 1)).zero) := by
  rw [pw_z_succ r 4, zp4, zmul]
  apply Prod.ext
  · show (ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one)) = (ring (2 * r + 1 + 1)).one; lib_ring
  · apply Prod.ext
    · show (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one)) ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one))) = (ring (2 * r + 1 + 1)).zero; lib_ring
    · apply Prod.ext
      · show (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one)) ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one))) = (ring (2 * r + 1 + 1)).zero; lib_ring
      · show (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one)) ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one))) = (ring (2 * r + 1 + 1)).zero; lib_ring

theorem z_mod (s t : Nat) : pw (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (zz r) (5 * s + t) = pw (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (zz r) t := by
  rw [HasseCM.pw_add', ← TwoPower.pwpow, zp5]
  show (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).mul (pw (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).one s) _ = _
  rw [TwoPower.pwone, (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).one_mul]

def gg : Zp (2 * r + 1 + 1) × Zp (2 * r + 1 + 1) × Zp (2 * r + 1 + 1) × Zp (2 * r + 1 + 1) := ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one), (ring (2 * r + 1 + 1)).zero, (ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).one) ((ring (2 * r + 1 + 1)).one)), (ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).one) ((ring (2 * r + 1 +   -- (+10 chars; full line in the Zenodo source)

theorem g_sq : (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).mul (gg r) (gg r) = ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).one) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).one) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).one) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).one) ((ring (2 * r + 1 + 1)).one)))), (ring (2 * r + 1 + 1)).zero, (ring (2 * r + 1 + 1)).zero, (ring (2   -- (+25 chars; full line in the Zenodo source)
  apply Prod.ext
  · show (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one)) ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one))) ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1 + 1)).zero) ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1  -- (+1329 chars; full line in the Zenodo source)
  · apply Prod.ext
    · show (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one)) ((ring (2 * r + 1 + 1)).zero)) ((ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1 + 1)).zero) ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one)))) ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).add ((ring (2 * r +  -- (+928 chars; full line in the Zenodo source)
    · apply Prod.ext
      · show (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one)) ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).one) ((ring (2 * r + 1 + 1)).one)))) ((ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1 + 1)).zero) ((ring (2 * r + 1 + 1)).zero))) ((ring (2 *  -- (+930 chars; full line in the Zenodo source)
      · show (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one)) ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).one) ((ring (2 * r + 1 + 1)).one)))) ((ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1 + 1)).zero) ((ring (2 * r +  -- (+1220 chars; full line in the Zenodo source)

theorem scal_mul (s : Zp (2 * r + 1 + 1)) (x : Zp (2 * r + 1 + 1) × Zp (2 * r + 1 + 1) × Zp (2 * r + 1 + 1) × Zp (2 * r + 1 + 1)) : (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).mul (s, (ring (2 * r + 1 + 1)).zero, (ring (2 * r + 1 + 1)).zero, (ring (2 * r + 1 + 1)).zero) x = ((ring (2 * r + 1 + 1)).mul s x.1, (ring (2 * r + 1 + 1)).mul s x.2.1, (ring (2 * r + 1 + 1)).mul s x.2.2.1, (ring (2 * r + 1 + 1)).  -- (+20 chars; full line in the Zenodo source)
  apply Prod.ext
  · show (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).mul (s) (x.1)) ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1 + 1)).zero) (x.2.2.2)) ((ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1 + 1)).zero) (x.2.2.1))) ((ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1 + 1)).zero) (x.2.1))))) ((ri  -- (+211 chars; full line in the Zenodo source)
  · apply Prod.ext
    · show (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).mul (s) (x.2.1)) ((ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1 + 1)).zero) (x.1))) ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1 + 1)).zero) (x.2.2.2)) ((ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1   -- (+213 chars; full line in the Zenodo source)
    · apply Prod.ext
      · show (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).mul (s) (x.2.2.1)) ((ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1 + 1)).zero) (x.2.1))) ((ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1 + 1)).zero) (x.1))) ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).mul ((ring (2 * r +  -- (+217 chars; full line in the Zenodo source)
      · show (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).mul (s) (x.2.2.2)) ((ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1 + 1)).zero) (x.2.2.1))) ((ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1 + 1)).zero) (x.2.1))) ((ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1 + 1)).zero) (x.1))) ((ring (2 * r + 1 + 1)).neg (  -- (+315 chars; full line in the Zenodo source)

theorem g_expr : gg r = (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).add (pw (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (zz r) 1) ((Cyclo5.cyc5 (ring (2 * r + 1 + 1))).add ((Cyclo5.cyc5 (ring (2 * r + 1 + 1))).neg (pw (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (zz r) 2)) ((Cyclo5.cyc5 (ring (2 * r + 1 + 1))).add ((Cyclo5.cyc5 (ring (2 * r + 1 + 1))).neg (pw (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (zz r) 3)) (pw (Cyclo5.  -- (+46 chars; full line in the Zenodo source)
  rw [zp1, zp2, zp3, zp4]
  apply Prod.ext
  · show (ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one) = (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).zero) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).zero)) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).zero)) ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one)))); lib_ring
  · apply Prod.ext
    · show (ring (2 * r + 1 + 1)).zero = (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).one) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).zero)) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).zero)) ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one)))); lib_ring
    · apply Prod.ext
      · show (ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).one) ((ring (2 * r + 1 + 1)).one)) = (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).zero) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one)) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).zero)) ((ring (2 * r + 1 + 1)).neg ((ring (2 * r  -- (+28 chars; full line in the Zenodo source)
      · show (ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).one) ((ring (2 * r + 1 + 1)).one)) = (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).zero) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).zero)) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one)) ((ring (2 * r + 1 + 1)).neg ((ring (2 * r  -- (+28 chars; full line in the Zenodo source)

theorem pw_neg_odd (x : Zp (2 * r + 1 + 1) × Zp (2 * r + 1 + 1) × Zp (2 * r + 1 + 1) × Zp (2 * r + 1 + 1)) :
    pw (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) ((Cyclo5.cyc5 (ring (2 * r + 1 + 1))).neg x) (2 * r + 1 + 1 + 1) = (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).neg (pw (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) x (2 * r + 1 + 1 + 1)) := by
  rw [show (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).neg x = (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).mul ((Cyclo5.cyc5 (ring (2 * r + 1 + 1))).neg (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).one) x by rw [DetChain.neg_mul_left, (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).one_mul], pw_mul,
    show (2 * r + 1 + 1 + 1) = 2 * (r + 1) + 1 by lib_nat, HasseCM.pw_add', SplitHasse.negone_even, (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).one_mul,
    show pw (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) ((Cyclo5.cyc5 (ring (2 * r + 1 + 1))).neg (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).one) 1 = (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).neg (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).one from (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).one_mul _, DetChain.neg_mul_left, (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).one_mul]

theorem frob_g (hpr : ∀ d, Dv d (2 * r + 1 + 1 + 1) → d = 1 ∨ d = (2 * r + 1 + 1 + 1)) :
    pw (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (gg r) (2 * r + 1 + 1 + 1) = (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).add (pw (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (zz r) (1 * (2 * r + 1 + 1 + 1))) ((Cyclo5.cyc5 (ring (2 * r + 1 + 1))).add ((Cyclo5.cyc5 (ring (2 * r + 1 + 1))).neg (pw (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (zz r) (2 * (2 * r + 1 + 1 + 1)))) ((Cyclo5.cyc5 (ring (2 * r + 1 + 1))).add ((Cyclo5.  -- (+191 chars; full line in the Zenodo source)
  rw [g_expr, frob_add (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (2 * r + 1) hpr (char_c5 r), frob_add (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (2 * r + 1) hpr (char_c5 r), frob_add (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (2 * r + 1) hpr (char_c5 r),
    pw_neg_odd, pw_neg_odd, TwoPower.pwpow, TwoPower.pwpow, TwoPower.pwpow, TwoPower.pwpow]

theorem combo1 : (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).add (pw (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (zz r) 1) ((Cyclo5.cyc5 (ring (2 * r + 1 + 1))).add ((Cyclo5.cyc5 (ring (2 * r + 1 + 1))).neg (pw (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (zz r) 2)) ((Cyclo5.cyc5 (ring (2 * r + 1 + 1))).add ((Cyclo5.cyc5 (ring (2 * r + 1 + 1))).neg (pw (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (zz r) 3)) (pw (Cyclo5.cyc5 (r  -- (+46 chars; full line in the Zenodo source)
  rw [zp1, zp2, zp3, zp4]
  apply Prod.ext
  · show (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).zero) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).zero)) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).zero)) ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one)))) = (ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one); lib_ring
  · apply Prod.ext
    · show (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).one) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).zero)) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).zero)) ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one)))) = (ring (2 * r + 1 + 1)).zero; lib_ring
    · apply Prod.ext
      · show (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).zero) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one)) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).zero)) ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one)))) = (ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).one) ((ring (2 *  -- (+28 chars; full line in the Zenodo source)
      · show (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).zero) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).zero)) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one)) ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one)))) = (ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).one) ((ring (2 *  -- (+28 chars; full line in the Zenodo source)

theorem combo2 : (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).add (pw (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (zz r) 2) ((Cyclo5.cyc5 (ring (2 * r + 1 + 1))).add ((Cyclo5.cyc5 (ring (2 * r + 1 + 1))).neg (pw (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (zz r) 4)) ((Cyclo5.cyc5 (ring (2 * r + 1 + 1))).add ((Cyclo5.cyc5 (ring (2 * r + 1 + 1))).neg (pw (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (zz r) 1)) (pw (Cyclo5.cyc5 (r  -- (+89 chars; full line in the Zenodo source)
  rw [zp2, zp4, zp1, zp3]
  apply Prod.ext
  · show (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).zero) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one))) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).zero)) ((ring (2 * r + 1 + 1)).zero))) = (ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one)); lib_ring
  · apply Prod.ext
    · show (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).zero) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one))) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one)) ((ring (2 * r + 1 + 1)).zero))) = (ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).zero); lib_ring
    · apply Prod.ext
      · show (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).one) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one))) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).zero)) ((ring (2 * r + 1 + 1)).zero))) = (ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).add ((ring (2 *   -- (+57 chars; full line in the Zenodo source)
      · show (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).zero) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one))) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).zero)) ((ring (2 * r + 1 + 1)).one))) = (ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).add ((ring (2 *   -- (+57 chars; full line in the Zenodo source)

theorem combo3 : (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).add (pw (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (zz r) 3) ((Cyclo5.cyc5 (ring (2 * r + 1 + 1))).add ((Cyclo5.cyc5 (ring (2 * r + 1 + 1))).neg (pw (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (zz r) 1)) ((Cyclo5.cyc5 (ring (2 * r + 1 + 1))).add ((Cyclo5.cyc5 (ring (2 * r + 1 + 1))).neg (pw (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (zz r) 4)) (pw (Cyclo5.cyc5 (r  -- (+89 chars; full line in the Zenodo source)
  rw [zp3, zp1, zp4, zp2]
  apply Prod.ext
  · show (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).zero) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).zero)) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one))) ((ring (2 * r + 1 + 1)).zero))) = (ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one)); lib_ring
  · apply Prod.ext
    · show (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).zero) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one)) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one))) ((ring (2 * r + 1 + 1)).zero))) = (ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).zero); lib_ring
    · apply Prod.ext
      · show (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).zero) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).zero)) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one))) ((ring (2 * r + 1 + 1)).one))) = (ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).add ((ring (2 *   -- (+57 chars; full line in the Zenodo source)
      · show (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).one) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).zero)) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one))) ((ring (2 * r + 1 + 1)).zero))) = (ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).add ((ring (2 *   -- (+57 chars; full line in the Zenodo source)

theorem combo4 : (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).add (pw (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (zz r) 4) ((Cyclo5.cyc5 (ring (2 * r + 1 + 1))).add ((Cyclo5.cyc5 (ring (2 * r + 1 + 1))).neg (pw (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (zz r) 3)) ((Cyclo5.cyc5 (ring (2 * r + 1 + 1))).add ((Cyclo5.cyc5 (ring (2 * r + 1 + 1))).neg (pw (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (zz r) 2)) (pw (Cyclo5.cyc5 (r  -- (+46 chars; full line in the Zenodo source)
  rw [zp4, zp3, zp2, zp1]
  apply Prod.ext
  · show (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one)) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).zero)) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).zero)) ((ring (2 * r + 1 + 1)).zero))) = (ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one); lib_ring
  · apply Prod.ext
    · show (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one)) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).zero)) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).zero)) ((ring (2 * r + 1 + 1)).one))) = (ring (2 * r + 1 + 1)).zero; lib_ring
    · apply Prod.ext
      · show (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one)) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).zero)) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one)) ((ring (2 * r + 1 + 1)).zero))) = (ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).one) ((ring (2 *  -- (+28 chars; full line in the Zenodo source)
      · show (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one)) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).one)) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).zero)) ((ring (2 * r + 1 + 1)).zero))) = (ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).one) ((ring (2 *  -- (+28 chars; full line in the Zenodo source)


theorem pw_embed5 (a : Zp (2 * r + 1 + 1)) : ∀ k, pw (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (a, (ring (2 * r + 1 + 1)).zero, (ring (2 * r + 1 + 1)).zero, (ring (2 * r + 1 + 1)).zero) k = (pw (ring (2 * r + 1 + 1)) a k, (ring (2 * r + 1 + 1)).zero, (ring (2 * r + 1 + 1)).zero, (ring (2 * r + 1 + 1)).zero)
  | 0 => rfl
  | k + 1 => by
    show (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).mul (pw (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (a, (ring (2 * r + 1 + 1)).zero, (ring (2 * r + 1 + 1)).zero, (ring (2 * r + 1 + 1)).zero) k) (a, (ring (2 * r + 1 + 1)).zero, (ring (2 * r + 1 + 1)).zero, (ring (2 * r + 1 + 1)).zero) = _
    rw [pw_embed5 a k, scal_mul]
    show ((ring (2 * r + 1 + 1)).mul (pw (ring (2 * r + 1 + 1)) a k) a, (ring (2 * r + 1 + 1)).mul (pw (ring (2 * r + 1 + 1)) a k) (ring (2 * r + 1 + 1)).zero, (ring (2 * r + 1 + 1)).mul (pw (ring (2 * r + 1 + 1)) a k) (ring (2 * r + 1 + 1)).zero, (ring (2 * r + 1 + 1)).mul (pw (ring (2 * r + 1 + 1)) a k) (ring (2 * r + 1 + 1)).zero) = _
    rw [DetChain.mul_zero]; rfl

theorem mod5_cases : ∀ q : Nat, ∃ s, q = 5 * s ∨ q = 5 * s + 1 ∨ q = 5 * s + 2 ∨ q = 5 * s + 3 ∨ q = 5 * s + 4
  | 0 => ⟨0, Or.inl rfl⟩
  | q + 1 => by
    match mod5_cases q with
    | ⟨s, h⟩ =>
      rcases h with h | h | h | h | h
      · exact ⟨s, Or.inr (Or.inl (by rw [h]))⟩
      · exact ⟨s, Or.inr (Or.inr (Or.inl (by rw [h])))⟩
      · exact ⟨s, Or.inr (Or.inr (Or.inr (Or.inl (by rw [h]))))⟩
      · exact ⟨s, Or.inr (Or.inr (Or.inr (Or.inr (by rw [h]))))⟩
      · exact ⟨s + 1, Or.inl (by rw [h]; lib_nat)⟩

theorem five_ne_zero (h5 : (2 * r + 1 + 1 + 1) ≠ 5) : (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).one) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).one) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).one) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).one) ((ring (2 * r + 1 + 1)).one)))) ≠ (ring (2 * r + 1 + 1)).zero := fun e => by
  have e1 : castZ (ring (2 * r + 1 + 1)) 5 = (ring (2 * r + 1 + 1)).zero := by
    show (ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one (ring (2 * r + 1 + 1)).zero)))) = _
    rw [DetChain.add_zero']; exact e
  match HasseDavenport.castZ_zero_dvd _ 5 e1 with
  | ⟨w, hw⟩ =>
    match EisenDiv.nat_dvd (2 * r + 1) 4 w hw with
    | ⟨q, hq⟩ =>
      cases q with
      | zero => exact Nat.noConfusion (hq.trans (Nat.zero_mul _))
      | succ q =>
        cases q with
        | zero => exact h5 ((Nat.one_mul _).symm.trans hq.symm)
        | succ q =>
          exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj
            (hq.trans (show (q + 1 + 1) * (2 * r + 1 + 1 + 1) = (2 * r * q + 4 * r + 3 * q) + 1 + 1 + 1 + 1 + 1 + 1 by lib_nat)))))))

theorem g_fixed (hpr : ∀ d, Dv d (2 * r + 1 + 1 + 1) → d = 1 ∨ d = (2 * r + 1 + 1 + 1)) (h5 : (2 * r + 1 + 1 + 1) ≠ 5) (u : Zp (2 * r + 1 + 1)) (hu : (ring (2 * r + 1 + 1)).mul u u = (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).one) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).one) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).one) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 +  -- (+44 chars; full line in the Zenodo source)
    pw (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (gg r) (2 * r + 1 + 1 + 1) = gg r := by
  have hu0 : u ≠ (ring (2 * r + 1 + 1)).zero := fun e => five_ne_zero r h5 (by rw [← hu, e, DetChain.mul_zero])
  rw [show (2 * r + 1 + 1 + 1) = 2 * (r + 1) + 1 by lib_nat]
  show (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).mul (pw (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (gg r) (2 * (r + 1))) (gg r) = gg r
  rw [← TwoPower.pwpow, show pw (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (gg r) 2 = (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).mul (gg r) (gg r) from by
      show (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).mul ((Cyclo5.cyc5 (ring (2 * r + 1 + 1))).mul (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).one (gg r)) (gg r) = _; rw [(Cyclo5.cyc5 (ring (2 * r + 1 + 1))).one_mul], g_sq, pw_embed5, ← hu,
    Euler.euler_square r hpr u hu0]
  exact (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).one_mul _

/-- **5 a square mod q ⇒ q ≡ ±1 (mod 5).** -/
theorem sq5_mod5 (hpr : ∀ d, Dv d (2 * r + 1 + 1 + 1) → d = 1 ∨ d = (2 * r + 1 + 1 + 1)) (h5 : (2 * r + 1 + 1 + 1) ≠ 5) (u : Zp (2 * r + 1 + 1)) (hu : (ring (2 * r + 1 + 1)).mul u u = (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).one) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).one) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).one) ((ring (2 * r + 1 + 1)).add ((ring (2 * r + 1   -- (+45 chars; full line in the Zenodo source)
    ∃ s, (2 * r + 1 + 1 + 1) = 5 * s + 1 ∨ (2 * r + 1 + 1 + 1) = 5 * s + 4 := by
  have hG := g_fixed r hpr h5 u hu
  rw [frob_g r hpr] at hG
  have bad : (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).neg (gg r) = gg r → False := fun e => by
    have e1 : (ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).neg (ring (2 * r + 1 + 1)).one) = (ring (2 * r + 1 + 1)).neg (ring (2 * r + 1 + 1)).one := congrArg Prod.fst e
    rw [DetChain.neg_neg'] at e1
    exact QChar.ne_pm r e1.symm
  match mod5_cases (2 * r + 1 + 1 + 1) with
  | ⟨s, h⟩ =>
    rcases h with h | h | h | h | h
    · exfalso
      cases hpr 5 ⟨s, by rw [h]; lib_nat⟩ with
      | inl e => exact Nat.noConfusion (Nat.succ.inj e)
      | inr e => exact h5 e.symm
    · exact ⟨s, Or.inl h⟩
    · exfalso; apply bad
      rw [show 1 * (2 * r + 1 + 1 + 1) = 5 * s + 2 by rw [h]; lib_nat, show 2 * (2 * r + 1 + 1 + 1) = 5 * (2 * s) + 4 by rw [h]; lib_nat,
        show 3 * (2 * r + 1 + 1 + 1) = 5 * (3 * s + 1) + 1 by rw [h]; lib_nat, show 4 * (2 * r + 1 + 1 + 1) = 5 * (4 * s + 1) + 3 by rw [h]; lib_nat,
        z_mod, z_mod, z_mod, z_mod, combo2] at hG
      exact hG
    · exfalso; apply bad
      rw [show 1 * (2 * r + 1 + 1 + 1) = 5 * s + 3 by rw [h]; lib_nat, show 2 * (2 * r + 1 + 1 + 1) = 5 * (2 * s + 1) + 1 by rw [h]; lib_nat,
        show 3 * (2 * r + 1 + 1 + 1) = 5 * (3 * s + 1) + 4 by rw [h]; lib_nat, show 4 * (2 * r + 1 + 1 + 1) = 5 * (4 * s + 2) + 2 by rw [h]; lib_nat,
        z_mod, z_mod, z_mod, z_mod, combo3] at hG
      exact hG
    · exact ⟨s, Or.inr h⟩

end Gauss5

#print axioms Gauss5.nsm_c5
#print axioms Gauss5.char_c5
#print axioms Gauss5.zz
#print axioms Gauss5.zmul
#print axioms Gauss5.pw_z_succ
#print axioms Gauss5.zp1
#print axioms Gauss5.zp2
#print axioms Gauss5.zp3
#print axioms Gauss5.zp4
#print axioms Gauss5.zp5
#print axioms Gauss5.z_mod
#print axioms Gauss5.gg
#print axioms Gauss5.g_sq
#print axioms Gauss5.scal_mul
#print axioms Gauss5.g_expr
#print axioms Gauss5.pw_neg_odd
#print axioms Gauss5.frob_g
#print axioms Gauss5.combo1
#print axioms Gauss5.combo2
#print axioms Gauss5.combo3
#print axioms Gauss5.combo4
#print axioms Gauss5.pw_embed5
#print axioms Gauss5.mod5_cases
#print axioms Gauss5.five_ne_zero
#print axioms Gauss5.g_fixed
#print axioms Gauss5.sq5_mod5

lang/Gauss5Conv.lean

lean_all/lang/Gauss5Conv.lean

/-
  lang/Gauss5Conv — T43, cycle 347: q ≡ 4 (mod 5) ⟹ 5 is a square mod q (the missing direction of the law for 5).

  In C = 𝔽_q[ζ]/(Φ₅) (Gauss5): g = ζ − ζ² − ζ³ + ζ⁴ has g² = 5, and g^q = ζ^q − ζ^(2q) − ζ^(3q) + ζ^(4q). For q = 5s + 4 the exponents
  are 4, 3, 2, 1 mod 5, so g^q = g (combo4). Writing g^q = (g²)^((q−1)/2)·g = (5^((q−1)/2), 0, 0, 0)·g and comparing first
  coordinates (g₁ = −1) gives 5^((q−1)/2) = 1, i.e. χ(5) = 1.
    sq5_conv     q = 5s + 4 ⟹ χ(5) = 1
  With Gauss5.sq5_mod5 (χ(5) = 1 ⟹ q ≡ ±1) and LegendreMoment.five_div_conv (q ≡ 1 ⟹ χ(5) = 1): χ(5) = 1 ⟺ q ≡ ±1 (mod 5).
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Gauss5
import lang.Inf2e1

namespace Gauss5Conv

open FTAChain ZMod Frobenius Gauss5

variable (r : Nat)

theorem sq5_conv (hpr : ∀ d, Dv d (2 * r + 1 + 1 + 1) → d = 1 ∨ d = (2 * r + 1 + 1 + 1)) (s : Nat) (h : 2 * r + 1 + 1 + 1 = 5 * s + 4) :
    QChar.chi r ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one (ring (2 * r + 1 + 1)).one)))) = 1 := by
  have h5 : (2 * r + 1 + 1 + 1) ≠ 5 := fun e => by
    have : s * 5 + 4 = 1 * 5 + 0 := by rw [show s * 5 + 4 = 5 * s + 4 by lib_nat, ← h, e]
    exact Nat.noConfusion (Inf2e1.uniq_remG 5 _ _ 4 0 (Nat.le_add_left 5 0) (Nat.le_add_left 1 4) this)
  have hG := frob_g r hpr
  rw [show 1 * (2 * r + 1 + 1 + 1) = 5 * s + 4 by rw [h]; lib_nat, show 2 * (2 * r + 1 + 1 + 1) = 5 * (2 * s + 1) + 3 by rw [h]; lib_nat,
    show 3 * (2 * r + 1 + 1 + 1) = 5 * (3 * s + 2) + 2 by rw [h]; lib_nat, show 4 * (2 * r + 1 + 1 + 1) = 5 * (4 * s + 3) + 1 by rw [h]; lib_nat,
    z_mod, z_mod, z_mod, z_mod, combo4] at hG
  -- g^q = (g²)^(r+1)·g = (5^(r+1), 0, 0, 0)·g
  rw [show (2 * r + 1 + 1 + 1) = 2 * (r + 1) + 1 by lib_nat] at hG
  have hG' : (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).mul (pw (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (gg r) (2 * (r + 1))) (gg r) = gg r := hG
  rw [← TwoPower.pwpow, show pw (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (gg r) 2 = (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).mul (gg r) (gg r) from by
      show (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).mul ((Cyclo5.cyc5 (ring (2 * r + 1 + 1))).mul (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).one (gg r)) (gg r) = _
      rw [(Cyclo5.cyc5 (ring (2 * r + 1 + 1))).one_mul],
    g_sq, pw_embed5, scal_mul] at hG'
  have e1 : (ring (2 * r + 1 + 1)).mul (pw (ring (2 * r + 1 + 1)) ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one (ring (2 * r + 1 + 1)).one)))) (r + 1)) ((ring (2 * r + 1 + 1)).neg (ring (2 * r + 1 + 1)).one)
      = (ring (2 * r + 1 + 1)).neg (ring (2 * r + 1 + 1)).one := congrArg Prod.fst hG'
  apply QChar.chi_one_of r _ (five_ne_zero r h5)
  generalize pw (ring (2 * r + 1 + 1)) ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one (ring (2 * r + 1 + 1)).one)))) (r + 1) = t at e1 ⊢
  calc t = (ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).mul t ((ring (2 * r + 1 + 1)).neg (ring (2 * r + 1 + 1)).one)) := by lib_ring
    _ = (ring (2 * r + 1 + 1)).one := by rw [e1]; lib_ring

end Gauss5Conv

#print axioms Gauss5Conv.sq5_conv

lang/GaussAp.lean

lean_all/lang/GaussAp.lean

/-
  lang/GaussAp — T43, cycle 150: Gauss's formula for y² = x³ − x over 𝔽_p, p ≡ 1 (mod 4), for every choice of π.

  For p = 2m + 3 prime with i² = −1 in 𝔽_p and ANY primary c + di with c² + d² = p:
    gauss_ap      −Σ_{x ∈ 𝔽_p} χ(x³ − x) = 2c
    exists_i      i exists for p = 4j + 5 (FF.euler_hard: (−1)^((p−1)/2) = 1)
    gauss_ap4     the formula at every p ≡ 1 (mod 4), no i assumed
  (FinPrimary gives one primary π with a_p = 2 Re π; PrimaryUnique says every primary of norm p has the same real part.)
  So a_p = ψ(π) + ψ(π̄) for the Hecke character ψ(α) = α on primary α, at every split prime, with no choice left —
  the GL1 → GL2 identity a_p(E) = a_p(θ_ψ) at split p as a statement about p alone.

  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.PrimaryUnique
import lang.FinPrimary

namespace GaussAp

open FTAChain ZMod YMChain FiniteRecChain

theorem val_inj (m : Nat) (x y : Zp (2 * m + 1 + 1)) (h : x.val = y.val) : x = y := Subtype.ext h

/-- **Gauss: a_p = 2c for every primary c + di of norm p.** -/
theorem gauss_ap (m : Nat) (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1)
    (i : Zp (2 * m + 1 + 1)) (hi : (ring (2 * m + 1 + 1)).mul i i = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)
    (c d : Int) (hP : GaussInt.Primary ⟨c, d⟩) (hcd : c * c + d * d = Int.ofNat (2 * m + 1 + 1 + 1)) :
    -sumK LibInt.intR (fun x => FinField.chi (FinField.ffp m hpr) (FinSign.f (FinField.ffp m hpr) x)) (FinField.ffp m hpr).els = 2 * c := by
  match FinPrimary.fin_ap_primary (FinField.ffp m hpr) (fun x => x.val) (val_inj m) i hi with
  | ⟨a, b, hab, hPa, hS⟩ =>
    have hab' : a * a + b * b = Int.ofNat (2 * m + 1 + 1 + 1) := by
      rw [hab]; exact congrArg Int.ofNat (show 2 * (m + 1) + 1 = 2 * m + 1 + 1 + 1 by lib_nat)
    rw [hS, (PrimaryUnique.primary_unique (2 * m + 1) hpr a b c d hPa hP hab' hcd).1]

/-- −1 is a square in 𝔽_p for p = 4j + 5 (Euler's criterion, the hard half). -/
theorem exists_i (j : Nat) (hpr : ∀ d, Dv d (2 * (2 * j + 1) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * j + 1) + 1 + 1 + 1) :
    ∃ i, (ring (2 * (2 * j + 1) + 1 + 1)).mul i i = (ring (2 * (2 * j + 1) + 1 + 1)).neg (ring (2 * (2 * j + 1) + 1 + 1)).one := by
  apply (FinField.ffp (2 * j + 1) hpr).euler_hard
  show Frobenius.pw (ring (2 * (2 * j + 1) + 1 + 1)) ((ring (2 * (2 * j + 1) + 1 + 1)).neg (ring (2 * (2 * j + 1) + 1 + 1)).one) (2 * j + 1 + 1) = (ring (2 * (2 * j + 1) + 1 + 1)).one
  rw [show 2 * j + 1 + 1 = 2 * (j + 1) by lib_nat, ← TwoPower.pwpow,
    show Frobenius.pw (ring (2 * (2 * j + 1) + 1 + 1)) ((ring (2 * (2 * j + 1) + 1 + 1)).neg (ring (2 * (2 * j + 1) + 1 + 1)).one) 2 = (ring (2 * (2 * j + 1) + 1 + 1)).one from
      ((ring (2 * (2 * j + 1) + 1 + 1)).one_mul _ |> fun e => by show (ring (2 * (2 * j + 1) + 1 + 1)).mul ((ring (2 * (2 * j + 1) + 1 + 1)).mul (ring (2 * (2 * j + 1) + 1 + 1)).one _) _ = _; rw [e]; exact TwoPower.negone_sq' _),
    TwoPower.pwone]

/-- **Gauss's formula at every p ≡ 1 (mod 4)**, p = 4j + 5: a_p = 2c for every primary c + di of norm p. -/
theorem gauss_ap4 (j : Nat) (hpr : ∀ d, Dv d (2 * (2 * j + 1) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * j + 1) + 1 + 1 + 1)
    (c d : Int) (hP : GaussInt.Primary ⟨c, d⟩) (hcd : c * c + d * d = Int.ofNat (2 * (2 * j + 1) + 1 + 1 + 1)) :
    -sumK LibInt.intR (fun x => FinField.chi (FinField.ffp (2 * j + 1) hpr) (FinSign.f (FinField.ffp (2 * j + 1) hpr) x))
      (FinField.ffp (2 * j + 1) hpr).els = 2 * c :=
  match exists_i j hpr with
  | ⟨i, hi⟩ => gauss_ap (2 * j + 1) hpr i hi c d hP hcd

end GaussAp

#print axioms GaussAp.val_inj
#print axioms GaussAp.gauss_ap
#print axioms GaussAp.exists_i
#print axioms GaussAp.gauss_ap4

lang/GaussCong.lean

lean_all/lang/GaussCong.lean

/-
  lang/GaussCong — T43, cycle 48: Gauss's congruence (1825). For p = 4k + 5 = u² + v² (u, v from CMModular):
      C(2k + 2, k + 1) ≡ ±2u  (mod p).

  The integer a_p-sum S = Σ_x χ(x³ − x) = ±2u (CMModular.cm_trace) is reduced mod p (IntCast): castZ(χ(z)) = z^((p−1)/2)
  (Euler), so castZ(S) = Σ_x (x³ − x)^((p−1)/2) = −(−1)^(k+1) C(2k + 2, k + 1) (HasseCM.hasse_cm). Hence
    cast_chi        castZ(χ(z)) = z^((p−1)/2)
    negone_pow      (−1)^n = ±1
    gauss_cong      mk C(2k+2, k+1) = mk (2u) or = −mk (2u), with u² + v² = p
  The Hasse invariant (a_p mod p) and the Hecke-character trace (a_p in ℤ) agree: the two halves of the CM
  correspondence for y² = x³ − x meet in Gauss's binomial congruence.

  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.CMModular
import lang.IntCast
import lang.HasseCM

namespace GaussCong

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum JacobMoment LibRing

variable (k : Nat)

theorem cast_chi (hpr : ∀ d, Dv d (2 * (2 * k + 1) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * k + 1) + 1 + 1 + 1) (z : Zp (2 * (2 * k + 1) + 1 + 1)) :
    castZ (ring (2 * (2 * k + 1) + 1 + 1)) (chi (2 * k + 1) z) = Frobenius.pw (ring (2 * (2 * k + 1) + 1 + 1)) z ((2 * k + 1) + 1) := by
  by_cases hz : z = (ring (2 * (2 * k + 1) + 1 + 1)).zero
  · rw [hz, chi_zero]
    exact (HasseCM.pw_zero_succ (ring (2 * (2 * k + 1) + 1 + 1)) (2 * k + 1)).symm
  · cases Euler.euler_pm (2 * k + 1) hpr z hz with
    | inl h =>
      rw [chi_one_of (2 * k + 1) z hz h, h]
      show (ring (2 * (2 * k + 1) + 1 + 1)).add (ring (2 * (2 * k + 1) + 1 + 1)).one (ring (2 * (2 * k + 1) + 1 + 1)).zero = (ring (2 * (2 * k + 1) + 1 + 1)).one
      exact DetChain.add_zero' (ring (2 * (2 * k + 1) + 1 + 1)) _
    | inr h =>
      have h' : Frobenius.pw (ring (2 * (2 * k + 1) + 1 + 1)) z ((2 * k + 1) + 1) ≠ (ring (2 * (2 * k + 1) + 1 + 1)).one := fun e => ne_pm (2 * k + 1) (h.symm.trans e)
      rw [chi_neg_of (2 * k + 1) z hz h', h]
      show (ring (2 * (2 * k + 1) + 1 + 1)).neg ((ring (2 * (2 * k + 1) + 1 + 1)).add (ring (2 * (2 * k + 1) + 1 + 1)).one (ring (2 * (2 * k + 1) + 1 + 1)).zero) = (ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one
      rw [DetChain.add_zero']

theorem negone_pow : ∀ n, Frobenius.pw (ring (2 * (2 * k + 1) + 1 + 1)) ((ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one) n = (ring (2 * (2 * k + 1) + 1 + 1)).one ∨
    Frobenius.pw (ring (2 * (2 * k + 1) + 1 + 1)) ((ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one) n = (ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one
  | 0 => Or.inl rfl
  | n + 1 => by
    cases negone_pow n with
    | inl h =>
      right
      show (ring (2 * (2 * k + 1) + 1 + 1)).mul (Frobenius.pw (ring (2 * (2 * k + 1) + 1 + 1)) ((ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one) n) ((ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one) = (ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one
      rw [h, (ring (2 * (2 * k + 1) + 1 + 1)).one_mul]
    | inr h =>
      left
      show (ring (2 * (2 * k + 1) + 1 + 1)).mul (Frobenius.pw (ring (2 * (2 * k + 1) + 1 + 1)) ((ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one) n) ((ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one) = (ring (2 * (2 * k + 1) + 1 + 1)).one
      rw [h]; exact TwoPower.negone_sq' _

theorem nsm_mk (n : Nat) : Frobenius.nsm (ring (2 * (2 * k + 1) + 1 + 1)) n (ring (2 * (2 * k + 1) + 1 + 1)).one = mk _ n :=
  Subtype.ext (nsm_one_val _ n)

/-- **Gauss's congruence.** -/
theorem gauss_cong (hpr : ∀ d, Dv d (2 * (2 * k + 1) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * k + 1) + 1 + 1 + 1) :
    ∃ u v : Nat, u * u + v * v = 2 * (2 * k + 1) + 1 + 1 + 1 ∧
      (mk (2 * (2 * k + 1) + 1 + 1) (Frobenius.C (2 * k + 2) (k + 1)) = mk (2 * (2 * k + 1) + 1 + 1) (2 * u) ∨
       mk (2 * (2 * k + 1) + 1 + 1) (Frobenius.C (2 * k + 2) (k + 1)) = (ring (2 * (2 * k + 1) + 1 + 1)).neg (mk (2 * (2 * k + 1) + 1 + 1) (2 * u))) := by
  match CMModular.cm_trace k hpr with
  | ⟨u, v, huv, hS⟩ =>
    refine ⟨u, v, huv, ?_⟩
    have hc : castZ (ring (2 * (2 * k + 1) + 1 + 1)) (sumK LibInt.intR (fun x => chi (2 * k + 1) (CMInert.cub (ring (2 * (2 * k + 1) + 1 + 1)) x)) (L (2 * k + 1)))
        = (ring (2 * (2 * k + 1) + 1 + 1)).neg (Frobenius.nsm (ring (2 * (2 * k + 1) + 1 + 1)) (Frobenius.C (2 * k + 2) (k + 1)) (Frobenius.pw (ring (2 * (2 * k + 1) + 1 + 1)) ((ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one) (k + 1))) := by
      rw [IntCast.castZ_sum]
      rw [sumK_ext (ring (2 * (2 * k + 1) + 1 + 1)) _ _ (fun x => cast_chi k hpr (CMInert.cub (ring (2 * (2 * k + 1) + 1 + 1)) x))]
      exact HasseCM.hasse_cm k hpr
    rw [Frobenius.nsm_eq_mul, nsm_mk] at hc
    have key : ∀ X s : Zp (2 * (2 * k + 1) + 1 + 1), (s = (ring (2 * (2 * k + 1) + 1 + 1)).one ∨ s = (ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one) →
        (X = mk (2 * (2 * k + 1) + 1 + 1) (2 * u) ∨ X = (ring (2 * (2 * k + 1) + 1 + 1)).neg (mk (2 * (2 * k + 1) + 1 + 1) (2 * u))) →
        X = (ring (2 * (2 * k + 1) + 1 + 1)).neg ((ring (2 * (2 * k + 1) + 1 + 1)).mul (mk (2 * (2 * k + 1) + 1 + 1) (Frobenius.C (2 * k + 2) (k + 1))) s) →
        (mk (2 * (2 * k + 1) + 1 + 1) (Frobenius.C (2 * k + 2) (k + 1)) = mk (2 * (2 * k + 1) + 1 + 1) (2 * u) ∨
         mk (2 * (2 * k + 1) + 1 + 1) (Frobenius.C (2 * k + 2) (k + 1)) = (ring (2 * (2 * k + 1) + 1 + 1)).neg (mk (2 * (2 * k + 1) + 1 + 1) (2 * u))) := by
      intro X s hs hX e
      have nn : ∀ y : Zp (2 * (2 * k + 1) + 1 + 1), (ring (2 * (2 * k + 1) + 1 + 1)).neg ((ring (2 * (2 * k + 1) + 1 + 1)).neg y) = y := DetChain.neg_neg' (ring (2 * (2 * k + 1) + 1 + 1))
      cases hs with
      | inl hs1 =>
        rw [hs1, (ring (2 * (2 * k + 1) + 1 + 1)).mul_comm, (ring (2 * (2 * k + 1) + 1 + 1)).one_mul] at e
        cases hX with
        | inl h => right; rw [← nn (mk (2 * (2 * k + 1) + 1 + 1) (Frobenius.C (2 * k + 2) (k + 1))), ← e, h]
        | inr h => left; rw [← nn (mk (2 * (2 * k + 1) + 1 + 1) (Frobenius.C (2 * k + 2) (k + 1))), ← e, h, nn]
      | inr hs1 =>
        rw [hs1, mul_neg_r (ring (2 * (2 * k + 1) + 1 + 1)), (ring (2 * (2 * k + 1) + 1 + 1)).mul_comm, (ring (2 * (2 * k + 1) + 1 + 1)).one_mul, nn] at e
        cases hX with
        | inl h => left; rw [← e, h]
        | inr h => right; rw [← e, h]
    apply key _ _ (negone_pow k (k + 1)) _ hc
    cases hS with
    | inl h => left; rw [h]; exact IntCast.castN_mk _ _
    | inr h => right; rw [h, IntCast.castZ_neg_nat, IntCast.castN_mk]

end GaussCong

#print axioms GaussCong.cast_chi
#print axioms GaussCong.negone_pow
#print axioms GaussCong.nsm_mk
#print axioms GaussCong.gauss_cong

lang/GaussCoprime.lean

lean_all/lang/GaussCoprime.lean

/-
  lang/GaussCoprime — G3 (T43): splitting by coprime norms in ℤ[i].

  If N(γ) = mn with sm + tn = 1 (m, n ≥ 1), then γ = αβ with N(α) = m, N(β) = n:
  α = gcd(γ, m), e = gcd(γ, n) (GaussEuclid.gcd_exists); αe | γ (Euclid's lemma, α and e coprime since sm + tn = 1);
  γ | αe (αe = γ(…) + yy'mn and mn = γγ̄); so γ = αe v with v a unit; N(α) | m (N(α) | m² and N(α) | mn, Bezout), N(e) | n,
  N(α)N(e) = mn, hence N(α) = m.
    nrm_div, unit_of, int_unit, coprime_split
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.GaussEuclid

namespace GaussCoprime

open FTAChain DetChain LibInt GaussEuclid

def emb (m : Int) : Int × Int := (m, 0)

theorem gn_emb (m : Int) : gn (emb m) = m * m := by show m * m + 0 * 0 = m * m; lib_int

theorem emb_mul (a b : Int) : (Cyclo4.cyc4 LibInt.intR).mul (emb a) (emb b) = emb (a * b) := by
  apply Prod.ext
  · show a * b + -(0 * 0) = a * b; lib_int
  · show a * 0 + 0 * b = 0; lib_int

theorem gn_dvd (d γ : Int × Int) (h : Dvd' d γ) : ∃ k, gn γ = gn d * k :=
  match h with
  | ⟨c, hc⟩ => ⟨gn c, by rw [hc, norm_mul]⟩

/-- a b = 1 in ℤ ⇒ a = 1 ∨ a = −1. -/
theorem int_unit (a b : Int) (h : a * b = 1) : a = 1 ∨ a = -1 := by
  have hn : Int.natAbs a * Int.natAbs b = 1 := by
    apply Int.ofNat.inj
    show Int.ofNat (Int.natAbs a * Int.natAbs b) = 1
    have e1 : Int.ofNat (Int.natAbs a * Int.natAbs b) * Int.ofNat (Int.natAbs a * Int.natAbs b) = (a * b) * (a * b) := by
      rw [show Int.ofNat (Int.natAbs a * Int.natAbs b) * Int.ofNat (Int.natAbs a * Int.natAbs b)
          = Int.ofNat (Int.natAbs a * Int.natAbs a) * Int.ofNat (Int.natAbs b * Int.natAbs b) by
            show Int.ofNat (Int.natAbs a * Int.natAbs b * (Int.natAbs a * Int.natAbs b)) = Int.ofNat (Int.natAbs a * Int.natAbs a * (Int.natAbs b * Int.natAbs b))
            exact congrArg Int.ofNat (by lib_nat), nas, nas]; lib_int
    rw [h] at e1
    have e2 : Int.natAbs a * Int.natAbs b * (Int.natAbs a * Int.natAbs b) = 1 * 1 := Int.ofNat.inj e1
    cases Nat.lt_or_ge (Int.natAbs a * Int.natAbs b) 1 with
    | inl hl =>
      have : Int.natAbs a * Int.natAbs b = 0 := Nat.le_antisymm (Nat.le_of_lt_succ hl) (Nat.zero_le _)
      rw [this] at e2; exact absurd e2 (fun e => Nat.noConfusion e)
    | inr hg =>
      cases Nat.lt_or_ge 1 (Int.natAbs a * Int.natAbs b) with
      | inl hl2 =>
        exfalso
        have : 2 * 2 ≤ Int.natAbs a * Int.natAbs b * (Int.natAbs a * Int.natAbs b) := Nat.mul_le_mul hl2 hl2
        rw [e2] at this
        exact absurd this (Nat.not_succ_le_self 1 |> fun h' => fun h2 => h' (Nat.le_trans (Nat.le_succ_of_le (Nat.le_succ 2)) h2))
      | inr hg2 => exact congrArg Int.ofNat (Nat.le_antisymm hg2 hg)
  have ha : Int.natAbs a = 1 := by
    cases Nat.lt_or_ge (Int.natAbs a) 1 with
    | inl hl => rw [Nat.le_antisymm (Nat.le_of_lt_succ hl) (Nat.zero_le _), Nat.zero_mul] at hn; exact absurd hn (fun e => Nat.noConfusion e)
    | inr hg =>
      cases Nat.lt_or_ge 1 (Int.natAbs a) with
      | inl hl2 =>
        exfalso
        cases Nat.lt_or_ge (Int.natAbs b) 1 with
        | inl hb => rw [Nat.le_antisymm (Nat.le_of_lt_succ hb) (Nat.zero_le _), Nat.mul_zero] at hn; exact absurd hn (fun e => Nat.noConfusion e)
        | inr hb =>
          have : 2 * 1 ≤ Int.natAbs a * Int.natAbs b := Nat.mul_le_mul hl2 hb
          rw [hn] at this; exact absurd this (fun h' => Nat.lt_irrefl 1 (Nat.lt_of_lt_of_le (Nat.lt_succ_self 1) h'))
      | inr hg2 => exact Nat.le_antisymm hg2 hg
  cases a with
  | ofNat t => exact Or.inl (congrArg Int.ofNat ha)
  | negSucc t =>
    have : t = 0 := Nat.succ.inj ha
    rw [this]; exact Or.inr rfl

theorem emb_add (a b : Int) : emb (a + b) = (Cyclo4.cyc4 LibInt.intR).add (emb a) (emb b) := by
  apply Prod.ext
  · rfl
  · show (0 : Int) = 0 + 0; rfl

theorem gn_zero_imp (z : Int × Int) (h : gn z = 0) : z = (Cyclo4.cyc4 LibInt.intR).zero := by
  apply nrm_zero
  apply Int.ofNat.inj
  rw [nrm_cast]; exact h

theorem cancel_left (γ z : Int × Int) (hγ : gn γ ≠ 0) (h : (Cyclo4.cyc4 LibInt.intR).mul γ z = (Cyclo4.cyc4 LibInt.intR).zero) : z = (Cyclo4.cyc4 LibInt.intR).zero := by
  have : gn γ * gn z = 0 := by rw [← norm_mul, h]; rfl
  cases int_noZD _ _ this with
  | inl e => exact absurd e hγ
  | inr e => exact gn_zero_imp z e

theorem gn_nonneg_one (z : Int × Int) (h : gn z = -1) : False := by
  rw [← nrm_cast] at h
  exact Int.noConfusion h

/-- **Coprime splitting**: N(γ) = mn, sm + tn = 1 ⇒ γ = αβ with N(α) = m, N(β) = n. -/
theorem coprime_split (γ : Int × Int) (m n : Nat) (s t : Int) (hγ : gn γ = Int.ofNat ((m + 1) * (n + 1)))
    (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1) :
    ∃ α β, γ = (Cyclo4.cyc4 LibInt.intR).mul α β ∧ gn α = Int.ofNat (m + 1) ∧ gn β = Int.ofNat (n + 1) := by
  match gcd_exists _ γ (emb (Int.ofNat (m + 1))) (Nat.le_refl _), gcd_exists _ γ (emb (Int.ofNat (n + 1))) (Nat.le_refl _) with
  | ⟨d, x, y, hd, ⟨u, hu⟩, ⟨c1, hc1⟩⟩, ⟨e, x', y', he, ⟨u', hu'⟩, ⟨c2, hc2⟩⟩ =>
    have hco : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (emb t) c2) e) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (emb s) c1) d) = (Cyclo4.cyc4 LibInt.intR).one := by
      have h1 : (Cyclo4.cyc4 LibInt.intR).one = emb (s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1)) := by rw [hst]; rfl
      rw [h1, emb_add, ← emb_mul, ← emb_mul, hc1, hc2]; lib_ring
    -- e | u, so γ = d e v
    match euclid_coprime e d u _ _ hco ⟨u', by rw [← hu, hu']⟩ with
    | ⟨v, hv⟩ =>
    have hγdev : γ = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul d e) v := by rw [hu, hv]; lib_ring
    -- γ | d e
    have hmn : (Cyclo4.cyc4 LibInt.intR).mul γ (cj γ) = emb (Int.ofNat (m + 1) * Int.ofNat (n + 1)) := by
      rw [mul_cj, hγ]; rfl
    have hW : (Cyclo4.cyc4 LibInt.intR).mul d e = (Cyclo4.cyc4 LibInt.intR).mul γ ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul x x') γ) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul x y') (emb (Int.ofNat (n + 1))))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).  -- (+120 chars; full line in the Zenodo source)
      rw [hd, he]
      have : (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul y y') ((Cyclo4.cyc4 LibInt.intR).mul γ (cj γ)) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul y y') ((Cyclo4.cyc4 LibInt.intR).mul (emb (Int.ofNat (m + 1))) (emb (Int.ofNat (n + 1)))) := by rw [hmn, emb_mul]
      calc (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul x γ) ((Cyclo4.cyc4 LibInt.intR).mul y (emb (Int.ofNat (m + 1))))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul x' γ) ((Cyclo4.cyc4 LibInt.intR).mul y' (emb (Int.ofNat (n + 1)))))
          = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul γ ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul x x') γ) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul x y') (emb (Int.ofNat (n + 1))))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul y x') (emb (Int.ofNat (m + 1))))))
              ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul y y') ((Cyclo4.cyc4 LibInt.intR).mul (emb (Int.ofNat (m + 1))) (emb (Int.ofNat (n + 1))))) := by lib_ring
        _ = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul γ ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul x x') γ) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul x y') (emb (Int.ofNat (n + 1))))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul y x') (emb (Int.ofNat (m + 1))))))
              ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul y y') ((Cyclo4.cyc4 LibInt.intR).mul γ (cj γ))) := by rw [this]
        _ = _ := by lib_ring
    generalize (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul x x') γ) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul x y') (emb (Int.ofNat (n + 1))))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul y x') (emb (Int.ofNat (m + 1)))) ((Cyclo4.cyc4 LibInt.intR).mul   -- (+55 chars; full line in the Zenodo source)
    have hγ0 : gn γ ≠ 0 := by rw [hγ]; exact fun e => Nat.noConfusion (Int.ofNat.inj e)
    have hWv : (Cyclo4.cyc4 LibInt.intR).mul W v = (Cyclo4.cyc4 LibInt.intR).one := by
      have h0 : (Cyclo4.cyc4 LibInt.intR).mul γ ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul W v) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one)) = (Cyclo4.cyc4 LibInt.intR).zero := by
        rw [show (Cyclo4.cyc4 LibInt.intR).mul γ ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul W v) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one)) = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul γ W) v) ((Cyclo4.cyc4 LibInt.intR).neg γ) by lib_ring, ← hW, ← hγdev]; lib_ring
      have := cancel_left γ _ hγ0 h0
      calc (Cyclo4.cyc4 LibInt.intR).mul W v = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul W v) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one)) (Cyclo4.cyc4 LibInt.intR).one := by lib_ring
        _ = (Cyclo4.cyc4 LibInt.intR).one := by rw [this]; lib_ring
    have hv1 : gn v = 1 := by
      have : gn W * gn v = 1 := by rw [← norm_mul, hWv]; rfl
      cases int_unit _ _ (by rw [show gn v * gn W = gn W * gn v by lib_int]; exact this) with
      | inl h => exact h
      | inr h => exact absurd h (gn_nonneg_one v)
    -- norms
    have hN : gn d * gn e = Int.ofNat (m + 1) * Int.ofNat (n + 1) := by
      have := congrArg gn hγdev
      rw [norm_mul, norm_mul, hv1, hγ] at this
      rw [show gn d * gn e = gn d * gn e * 1 by lib_int, ← this]; rfl
    have hdm : ∃ a, Int.ofNat (m + 1) = gn d * a := by
      have k1 : Int.ofNat (m + 1) * Int.ofNat (m + 1) = gn d * gn c1 := by rw [← gn_emb, hc1, norm_mul]
      have k2 : Int.ofNat (m + 1) * Int.ofNat (n + 1) = gn d * gn u := by show Int.ofNat ((m + 1) * (n + 1)) = _; rw [← hγ, hu, norm_mul]
      exact ⟨s * gn c1 + t * gn u, by
        calc Int.ofNat (m + 1) = Int.ofNat (m + 1) * (s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1)) := by rw [hst]; lib_int
          _ = s * (Int.ofNat (m + 1) * Int.ofNat (m + 1)) + t * (Int.ofNat (m + 1) * Int.ofNat (n + 1)) := by lib_int
          _ = gn d * (s * gn c1 + t * gn u) := by rw [k1, k2]; lib_int⟩
    have hen' : ∃ b, Int.ofNat (n + 1) = gn e * b := by
      have k1 : Int.ofNat (n + 1) * Int.ofNat (n + 1) = gn e * gn c2 := by rw [← gn_emb, hc2, norm_mul]
      have k2 : Int.ofNat (m + 1) * Int.ofNat (n + 1) = gn e * gn u' := by show Int.ofNat ((m + 1) * (n + 1)) = _; rw [← hγ, hu', norm_mul]
      exact ⟨t * gn c2 + s * gn u', by
        calc Int.ofNat (n + 1) = Int.ofNat (n + 1) * (s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1)) := by rw [hst]; lib_int
          _ = t * (Int.ofNat (n + 1) * Int.ofNat (n + 1)) + s * (Int.ofNat (m + 1) * Int.ofNat (n + 1)) := by lib_int
          _ = gn e * (t * gn c2 + s * gn u') := by rw [k1, k2]; lib_int⟩
    match hdm, hen' with
    | ⟨a, ha⟩, ⟨b, hb⟩ =>
      have hab : a * b = 1 := by
        have h0 : (gn d * gn e) * (a * b + -1) = 0 := by
          rw [show (gn d * gn e) * (a * b + -1) = (gn d * a) * (gn e * b) + -(gn d * gn e) by lib_int, ← ha, ← hb, hN]; lib_int
        cases int_noZD _ _ h0 with
        | inl e0 => exact absurd (by rw [hN] at e0; exact e0) (fun e1 : Int.ofNat (m + 1) * Int.ofNat (n + 1) = 0 => Nat.noConfusion (Int.ofNat.inj e1))
        | inr e0 => calc a * b = (a * b + -1) + 1 := by lib_int
            _ = 1 := by rw [e0]; rfl
      have pos : ∀ (k : Nat) (z : Int × Int) (c : Int), Int.ofNat (k + 1) = gn z * c → c = 1 ∨ c = -1 → c = 1 := fun k z c h hc => by
        cases hc with
        | inl h1 => exact h1
        | inr h1 =>
          exfalso
          rw [h1, ← nrm_cast] at h
          have : Int.ofNat (k + 1) + Int.ofNat (nrm z) = 0 := by rw [h]; lib_int
          have h2 : k + nrm z + 1 = 0 := by rw [show k + nrm z + 1 = k + 1 + nrm z by lib_nat]; exact Int.ofNat.inj (show Int.ofNat (k + 1 + nrm z) = Int.ofNat 0 from this)
          exact Nat.noConfusion h2
      have a1 := pos m d a ha (int_unit a b hab)
      have b1 := pos n e b hb (int_unit b a (by rw [show b * a = a * b by lib_int]; exact hab))
      refine ⟨d, (Cyclo4.cyc4 LibInt.intR).mul e v, by rw [hγdev]; lib_ring, by rw [ha, a1]; lib_int, by rw [norm_mul, hv1, hb, b1]⟩

end GaussCoprime

#print axioms GaussCoprime.gn_emb
#print axioms GaussCoprime.emb_mul
#print axioms GaussCoprime.gn_dvd
#print axioms GaussCoprime.int_unit
#print axioms GaussCoprime.emb_add
#print axioms GaussCoprime.gn_zero_imp
#print axioms GaussCoprime.cancel_left
#print axioms GaussCoprime.coprime_split

lang/GaussCritN.lean

lean_all/lang/GaussCritN.lean

/-
  lang/GaussCritN — T49, cycle 606: the pieces of the n-th power residue criterion in E[C_p]:
    gt_mono0     (s ζ⁰ · u)(c) = s u(c)
    gauss_Gv     Σ_a ψ(a) ζ^a (GaussFrobN.gauss) = Gv ψ
    XG           (pζ⁰ − N) G_ψ = p G_ψ                      (Σψ = 0)
    gpow_n       G_ψ^n = Jpr_(n−2) ψ(ι(−1)) (pζ⁰ − N)       (ψ^(n−1) = ψ∘ι, the intermediate powers nontrivial)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.GaussPowN
import lang.GaussFrobN

namespace CharSumGen

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

variable {S : Type} (E : CRing S) (j : Nat)

theorem gt_mono0 (s : S) (u : V S (j + 1 + 1 + 1)) (c : Zp (j + 1 + 1)) :
    gt E (j + 1 + 1) (gmul E (j + 1 + 1) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero s) u) c = E.mul s (gt E (j + 1 + 1) u c) := by
  rw [gt_mul]
  have e : sumK E (fun a => E.mul (gt E (j + 1 + 1) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero s) a) (gt E (j + 1 + 1) u ((ring (j + 1 + 1)).add c ((ring (j + 1 + 1)).neg a)))) (L (j + 1 + 1)) =
      sumK E (fun a => E.mul (delta E a (ring (j + 1 + 1)).zero) (E.mul s (gt E (j + 1 + 1) u ((ring (j + 1 + 1)).add c ((ring (j + 1 + 1)).neg a))))) (L (j + 1 + 1)) :=
    sumK_ext E _ _ (fun a => by rw [gt_mono]; lib_ring) _
  rw [e, delta_sum, show sumK E (fun y => delta E y (ring (j + 1 + 1)).zero) (L (j + 1 + 1)) = E.one from CMInert.complete_allZ (j + 1 + 1) E _]
  show E.mul E.one (E.mul s (gt E (j + 1 + 1) u ((ring (j + 1 + 1)).add c ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).zero)))) = _
  rw [show (ring (j + 1 + 1)).add c ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).zero) = c by lib_ring]; lib_ring

theorem gauss_Gv (ψ : Zp (j + 1 + 1) → S) : gauss E (j + 1) ψ = Gv j ψ :=
  ext E (j + 1 + 1) _ _ (fun c => by
    unfold gauss Gv
    rw [gt_sum, gt_tb]
    have e : sumK E (fun a => gt E (j + 1 + 1) (mono E (j + 1 + 1) a (ψ a)) c) (L (j + 1 + 1)) = sumK E (fun a => E.mul (delta E a c) (ψ a)) (L (j + 1 + 1)) :=
      sumK_ext E _ _ (fun a => by rw [gt_mono, delta_symm, E.mul_comm]) _
    rw [e, delta_sum, show sumK E (fun y => delta E y c) (L (j + 1 + 1)) = E.one from CMInert.complete_allZ (j + 1 + 1) E _]; lib_ring)

theorem XG (ψ : Zp (j + 1 + 1) → S) (hs : sumK E ψ (LL j) = E.zero) :
    gmul E (j + 1 + 1) (Xv E j) (Gv j ψ) = gmul E (j + 1 + 1) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (Pp E j)) (Gv j ψ) :=
  ext E (j + 1 + 1) _ _ (fun c => by
    rw [gt_mono0, gt_mul]
    have e : ∀ a, E.mul (gt E (j + 1 + 1) (Xv E j) a) (gt E (j + 1 + 1) (Gv j ψ) ((ring (j + 1 + 1)).add c ((ring (j + 1 + 1)).neg a))) =
        E.add (E.mul (delta E a (ring (j + 1 + 1)).zero) (E.mul (Pp E j) (ψ ((ring (j + 1 + 1)).add c ((ring (j + 1 + 1)).neg a)))))
          (E.neg (ψ ((ring (j + 1 + 1)).add c ((ring (j + 1 + 1)).neg a)))) := fun a => by
      rw [gt_X]; unfold Gv; rw [gt_tb]; lib_ring
    rw [sumK_ext E _ _ e, sumK_add, delta_sum, show sumK E (fun y => delta E y (ring (j + 1 + 1)).zero) (L (j + 1 + 1)) = E.one from CMInert.complete_allZ (j + 1 + 1) E _,
      CMInert.sum_neg_fun]
    have hre := reindex E (L (j + 1 + 1)) (CMInert.complete_allZ (j + 1 + 1) E) (fun a => (ring (j + 1 + 1)).add c ((ring (j + 1 + 1)).neg a))
      (fun a => (ring (j + 1 + 1)).add c ((ring (j + 1 + 1)).neg a)) (fun x => by lib_ring) (fun y => by lib_ring) ψ
    rw [hre, show sumK E ψ (L (j + 1 + 1)) = E.zero from hs]
    unfold Gv; rw [gt_tb]
    show E.add (E.mul E.one (E.mul (Pp E j) (ψ ((ring (j + 1 + 1)).add c ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).zero))))) (E.neg E.zero) = _
    rw [show (ring (j + 1 + 1)).add c ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).zero) = c by lib_ring]; lib_ring)

/-- **G_ψ^(k+2) = Jpr_k · ψ(ι(−1)) · (pζ⁰ − N)** when ψ^(k+1) = ψ∘ι. -/
theorem gpow_n (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (ψ : Zp (j + 1 + 1) → S)
    (hm : ∀ x y, ψ ((ring (j + 1 + 1)).mul x y) = E.mul (ψ x) (ψ y)) (h1 : ψ (ring (j + 1 + 1)).one = E.one)
    (h0 : ψ (ring (j + 1 + 1)).zero = E.zero) (hs : sumK E ψ (LL j) = E.zero) (k : Nat)
    (hnt : ∀ i, i < k → sumK E (fun a => pw E (ψ a) (i + 2)) (LL j) = E.zero) (hinv : ∀ x, pw E (ψ x) (k + 1) = ψ (iv j x)) :
    pw (gring E (j + 1 + 1)) (Gv j ψ) (k + 2) =
      gmul E (j + 1 + 1) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (E.mul (jpr E j ψ k) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))))) (Xv E j) := by
  show gmul E (j + 1 + 1) (pw (gring E (j + 1 + 1)) (Gv j ψ) (k + 1)) (Gv j ψ) = _
  rw [gpow E j hpr ψ hm h0 k hnt]
  apply ext; intro c
  rw [gt_mono0, E.mul_assoc, ← gauss_norm E j hpr ψ hm h1 h0 hs c, gt_mul, gt_mul, mul_sumK]
  -- Σ_a Jpr ψ(ι a) ψ(c − a) = Jpr Σ_a ψ(a) ψ(ι(c − a))  (a ↦ c − a)
  have hre := reindex E (L (j + 1 + 1)) (CMInert.complete_allZ (j + 1 + 1) E) (fun a => (ring (j + 1 + 1)).add c ((ring (j + 1 + 1)).neg a))
    (fun a => (ring (j + 1 + 1)).add c ((ring (j + 1 + 1)).neg a)) (fun x => by lib_ring) (fun y => by lib_ring)
    (fun a => E.mul (E.mul (jpr E j ψ k) (pw E (ψ a) (k + 1))) (ψ ((ring (j + 1 + 1)).add c ((ring (j + 1 + 1)).neg a))))
  unfold Gv
  refine Eq.trans (sumK_ext E _ (fun a => E.mul (E.mul (jpr E j ψ k) (pw E (ψ a) (k + 1))) (ψ ((ring (j + 1 + 1)).add c ((ring (j + 1 + 1)).neg a)))) (fun a => ?_) _)
    (hre.symm.trans (sumK_ext E _ _ (fun x => ?_) _))
  · rw [gt_tb, gt_tb]
  · rw [gt_tb, gt_tb, hinv, show (ring (j + 1 + 1)).add c ((ring (j + 1 + 1)).neg ((ring (j + 1 + 1)).add c ((ring (j + 1 + 1)).neg x))) = x by lib_ring]
    lib_ring

end CharSumGen

#print axioms CharSumGen.gt_mono0
#print axioms CharSumGen.gauss_Gv
#print axioms CharSumGen.XG
#print axioms CharSumGen.gpow_n

lang/GaussCube.lean

lean_all/lang/GaussCube.lean

/-
  lang/GaussCube — T43, cycle 161: the primary elements of norm p³ prime to p are π³ and π̄³.

  π = a + bi primary, a² + b² = p prime. If α = A + Bi is primary, N(α) = p³ and p ∤ A, then A = a³ − 3ab² = Re π³:
    unit_primary     u unit, z and u z primary ⇒ u = 1
    prim_pow         π^j primary
    cube_primary     the statement — GaussFactor gives α = u π^j π̄^(3−j); j = 1, 2 give p | A; j = 0, 3 give u = 1.
  With a(𝔽_{p³}) = 2A (CubeTrace) and p ∤ a(𝔽_{p³}) (ordinarity), this is a(𝔽_{p³}) = 2 Re π³ = a_p³ − 3p a_p.

  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.GaussFactor
import lang.PrimaryUnique

namespace GaussCube

open FTAChain LibInt LibGcd FermatUnique GaussInt GaussFactor

theorem unit_primary (u z : GI) (hu : IsUnit u) (hz : Primary z) (huz : Primary (gmul u z)) : u = gone := by
  match hz, huz with
  | ⟨⟨e, he⟩, ⟨t, ht⟩⟩, ⟨⟨e', he'⟩, ⟨t', ht'⟩⟩ =>
    have hre : z.re = 2 * (2 * t - e) + 1 := by rw [show 2 * (2 * t - e) + 1 = 4 * t + 1 - 2 * e by lib_int, ← ht, he]; lib_int
    rcases hu with h | h | h | h
    · exact h
    · exfalso; subst h
      have h1 : -1 * z.re - 0 * z.im + (-1 * z.im + 0 * z.re) - 1 = 4 * t' := ht'
      apply PrimaryUnique.four_bad (-(t + t'))
      rw [show 4 * -(t + t') = -(4 * t) + -(4 * t') by lib_int, ← ht, ← h1]; lib_int
    · exfalso; subst h
      have h1 : 0 * z.im + 1 * z.re = 2 * e' := he'
      exact int_odd_ne_even _ e' (by rw [← hre, ← h1]; lib_int)
    · exfalso; subst h
      have h1 : 0 * z.im + -1 * z.re = 2 * e' := he'
      exact int_odd_ne_even _ (-e') (by rw [← hre, show 2 * -e' = -(2 * e') by lib_int, ← h1]; lib_int)

theorem prim_one : Primary gone := ⟨⟨0, rfl⟩, ⟨0, rfl⟩⟩

theorem prim_pow (x : GI) (hx : Primary x) : ∀ j, Primary (gpow x j)
  | 0 => prim_one
  | j + 1 => prim_mul _ _ (prim_pow x hx j) hx

theorem cube_re (x : GI) : (gpow x 3).re = x.re * x.re * x.re - 3 * (x.re * (x.im * x.im)) := by
  show ((1 * x.re - 0 * x.im) * x.re - (1 * x.im + 0 * x.re) * x.im) * x.re
    - ((1 * x.re - 0 * x.im) * x.im + (1 * x.im + 0 * x.re) * x.re) * x.im = _
  lib_int

theorem one_two (a b : Int) : gmul (gpow ⟨a, b⟩ 1) (gpow ⟨a, -b⟩ 2) = ⟨(a * a + b * b) * a, -((a * a + b * b) * b)⟩ := by
  apply gi_ext
  · show (1 * a - 0 * b) * ((1 * a - 0 * -b) * a - (1 * -b + 0 * a) * -b) - (1 * b + 0 * a) * ((1 * a - 0 * -b) * -b + (1 * -b + 0 * a) * a) = (a * a + b * b) * a
    lib_int
  · show (1 * a - 0 * b) * ((1 * a - 0 * -b) * -b + (1 * -b + 0 * a) * a) + (1 * b + 0 * a) * ((1 * a - 0 * -b) * a - (1 * -b + 0 * a) * -b) = -((a * a + b * b) * b)
    lib_int

theorem two_one (a b : Int) : gmul (gpow ⟨a, b⟩ 2) (gpow ⟨a, -b⟩ 1) = ⟨(a * a + b * b) * a, (a * a + b * b) * b⟩ := by
  apply gi_ext
  · show ((1 * a - 0 * b) * a - (1 * b + 0 * a) * b) * (1 * a - 0 * -b) - ((1 * a - 0 * b) * b + (1 * b + 0 * a) * a) * (1 * -b + 0 * a) = (a * a + b * b) * a
    lib_int
  · show ((1 * a - 0 * b) * a - (1 * b + 0 * a) * b) * (1 * -b + 0 * a) + ((1 * a - 0 * b) * b + (1 * b + 0 * a) * a) * (1 * a - 0 * -b) = (a * a + b * b) * b
    lib_int

/-- **Primary α of norm p³ with p ∤ Re α has Re α = Re π³.** -/
theorem cube_primary (p : Nat) (hp : ∀ d, Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (a b : Int)
    (hab : a * a + b * b = Int.ofNat (p + 1 + 1)) (hπ : Primary ⟨a, b⟩) (x : GI) (hx : Primary x)
    (hN : gnorm x = ipow (Int.ofNat (p + 1 + 1)) 3) (hA : ¬ ∃ w, x.re = Int.ofNat (p + 1 + 1) * w) :
    x.re = a * a * a - 3 * (a * (b * b)) := by
  match factor p hp a b hab 3 x hN with
  | ⟨u, j, l, hu, hjl, hx'⟩ =>
    match j with
    | 0 =>
      have hl : l = 3 := (Nat.zero_add l).symm.trans hjl
      subst hl
      have hc : Primary (gmul (gpow ⟨a, b⟩ 0) (gpow ⟨a, -b⟩ 3)) :=
        prim_mul _ _ prim_one (prim_pow _ (prim_conj ⟨a, b⟩ hπ) 3)
      have h1 := unit_primary u _ hu hc (by rw [← hx']; exact hx)
      rw [hx', h1, gone_mul, show gpow ⟨a, b⟩ 0 = gone from rfl, gone_mul, cube_re]
      show a * a * a - 3 * (a * (-b * -b)) = _; lib_int
    | 3 =>
      have hl : l = 0 := add_cancel 3 l 0 (hjl.trans rfl)
      subst hl
      have hc : Primary (gmul (gpow ⟨a, b⟩ 3) (gpow ⟨a, -b⟩ 0)) := prim_mul _ _ (prim_pow _ hπ 3) prim_one
      have h1 := unit_primary u _ hu hc (by rw [← hx']; exact hx)
      rw [hx', h1, gone_mul, show gpow ⟨a, -b⟩ 0 = gone from rfl, gmul_comm, gone_mul, cube_re]
    | 1 =>
      have hl : l = 2 := Nat.succ.inj ((Nat.add_comm l 1).trans hjl)
      subst hl
      exfalso; apply hA
      refine ⟨u.re * a + u.im * b, ?_⟩
      rw [hx', one_two, ← hab]
      show u.re * ((a * a + b * b) * a) - u.im * -((a * a + b * b) * b) = _; lib_int
    | 2 =>
      have hl : l = 1 := Nat.succ.inj (Nat.succ.inj ((show l + 1 + 1 = 2 + l by lib_nat).trans hjl))
      subst hl
      exfalso; apply hA
      refine ⟨u.re * a - u.im * b, ?_⟩
      rw [hx', two_one, ← hab]
      show u.re * ((a * a + b * b) * a) - u.im * ((a * a + b * b) * b) = _; lib_int
    | j + 4 =>
      exfalso
      exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj ((show j + l + 1 + 1 + 1 + 1 = j + 4 + l by lib_nat).trans hjl))))

end GaussCube

#print axioms GaussCube.unit_primary
#print axioms GaussCube.prim_one
#print axioms GaussCube.prim_pow
#print axioms GaussCube.cube_re
#print axioms GaussCube.one_two
#print axioms GaussCube.two_one
#print axioms GaussCube.cube_primary

lang/GaussDiv.lean

lean_all/lang/GaussDiv.lean

/-
  lang/GaussDiv — T43, cycle 159: π = a + bi of prime norm p is a Gaussian prime: p | N(α) ⇒ π | α or π̄ | α.

    quot_exact     p | ca + db and p | da − cb ⇒ c + di = (a + bi)(u + vi)       (multiply out, cancel p)
    sq_div         p | z² ⇒ p | z                                                 (Euclid in ℤ/p)
    gauss_div      p | c² + d² ⇒ c + di = π γ or c + di = π̄ γ, and then N(c + di) = p · N(γ)
  Proof: (ca + db)(ca − db) = a²(c² + d²) − d² p, and (ca + db)² + (da − cb)² = p (c² + d²).
  The step of unique factorisation in ℤ[i] needed to list the primary elements of norm p^k (next: a(𝔽_{p³}) = a_p³ − 3p a_p).

  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.FermatUnique
import lang.GaussInt

namespace GaussDiv

open FTAChain LibInt FermatUnique GaussInt

theorem quot_exact (p : Nat) (a b c d u v : Int) (hab : a * a + b * b = Int.ofNat (p + 1 + 1))
    (hu : c * a + d * b = Int.ofNat (p + 1 + 1) * u) (hv : d * a - c * b = Int.ofNat (p + 1 + 1) * v) :
    c = a * u - b * v ∧ d = a * v + b * u := by
  constructor
  · apply cancelP p
    rw [show Int.ofNat (p + 1 + 1) * (a * u - b * v) = a * (Int.ofNat (p + 1 + 1) * u) - b * (Int.ofNat (p + 1 + 1) * v) by lib_int,
      ← hu, ← hv, ← hab]; lib_int
  · apply cancelP p
    rw [show Int.ofNat (p + 1 + 1) * (a * v + b * u) = a * (Int.ofNat (p + 1 + 1) * v) + b * (Int.ofNat (p + 1 + 1) * u) by lib_int,
      ← hu, ← hv, ← hab]; lib_int

theorem sq_div (p : Nat) (hp : ∀ d, Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (z q : Int)
    (h : z * z = Int.ofNat (p + 1 + 1) * q) : ∃ u, z = Int.ofNat (p + 1 + 1) * u := by
  cases int_euclid p hp z z q h with
  | inl h => exact h
  | inr h => exact h

/-- one direction: p | ca + db ⇒ π | c + di. -/
theorem div_plus (p : Nat) (hp : ∀ d, Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (a b c d n u : Int)
    (hab : a * a + b * b = Int.ofNat (p + 1 + 1)) (hn : c * c + d * d = Int.ofNat (p + 1 + 1) * n)
    (hu : c * a + d * b = Int.ofNat (p + 1 + 1) * u) :
    ∃ x y : Int, c = a * x - b * y ∧ d = a * y + b * x := by
  have hsq : (d * a - c * b) * (d * a - c * b) = Int.ofNat (p + 1 + 1) * (Int.ofNat (p + 1 + 1) * n + -(Int.ofNat (p + 1 + 1) * u * u)) := by
    have e : (c * a + d * b) * (c * a + d * b) + (d * a - c * b) * (d * a - c * b) = (c * c + d * d) * (a * a + b * b) := by lib_int
    rw [hu, hn, hab] at e
    rw [show (d * a - c * b) * (d * a - c * b) = (Int.ofNat (p + 1 + 1) * u * (Int.ofNat (p + 1 + 1) * u)
      + (d * a - c * b) * (d * a - c * b)) + -(Int.ofNat (p + 1 + 1) * u * (Int.ofNat (p + 1 + 1) * u)) by lib_int, e]; lib_int
  match sq_div p hp _ _ hsq with
  | ⟨v, hv⟩ =>
    match quot_exact p a b c d u v hab hu hv with
    | ⟨h1, h2⟩ => exact ⟨u, v, h1, h2⟩

/-- **π or π̄ divides every α with p | N(α).** -/
theorem gauss_div (p : Nat) (hp : ∀ d, Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (a b c d n : Int)
    (hab : a * a + b * b = Int.ofNat (p + 1 + 1)) (hn : c * c + d * d = Int.ofNat (p + 1 + 1) * n) :
    (∃ x y : Int, c = a * x - b * y ∧ d = a * y + b * x) ∨ (∃ x y : Int, c = a * x + b * y ∧ d = a * y - b * x) := by
  have key : (c * a + d * b) * (c * a + -1 * (d * b)) = Int.ofNat (p + 1 + 1) * (a * a * n + -1 * (d * d)) := by
    have hb : b * b = Int.ofNat (p + 1 + 1) + -1 * (a * a) := by rw [← hab]; lib_int
    rw [show (c * a + d * b) * (c * a + -1 * (d * b)) = a * a * (c * c + d * d) + -1 * (d * d * (a * a + b * b)) by lib_int, hn, hab]
    lib_int
  cases int_euclid p hp _ _ _ key with
  | inl h =>
    match h with
    | ⟨u, hu⟩ => exact Or.inl (div_plus p hp a b c d n u hab hn hu)
  | inr h =>
    match h with
    | ⟨u, hu⟩ =>
      match div_plus p hp a (-b) c d n u (by rw [← hab]; lib_int) hn (by rw [← hu]; lib_int) with
      | ⟨x, y, h1, h2⟩ => exact Or.inr ⟨x, y, by rw [h1]; lib_int, by rw [h2]; lib_int⟩

end GaussDiv

#print axioms GaussDiv.quot_exact
#print axioms GaussDiv.sq_div
#print axioms GaussDiv.div_plus
#print axioms GaussDiv.gauss_div

lang/GaussEuclid.lean

lean_all/lang/GaussEuclid.lean

/-
  lang/GaussEuclid — G3 (T43): ℤ[i] is Euclidean: division with remainder, gcd as a combination, and Euclid's lemma.

  ℤ[i] = Cyclo4.cyc4 ℤ (pairs), |z|² = nrm z ∈ ℕ (natAbs² + natAbs², so that order arguments stay in ℕ):
    rnd            a = q(M + 1) + r with 2|r| ≤ M + 1                    (FTAChain.div_exists, both signs)
    nrm_cast       (nrm z : ℤ) = z.1² + z.2²;   norm_mul   N(xy) = N(x)N(y)
    gdiv           β ≠ 0:  α = qβ + ρ with nrm ρ < nrm β                 (round αβ̄ / N(β) componentwise; 4|ρ|²N ≤ 2N²)
    gcd_exists     d = xα + yβ dividing α and β                          (strong induction on nrm β)
    euclid_coprime xα + yβ = 1 and α | βγ ⇒ α | γ
  The input for unique factorisation in ℤ[i] and the multiplicativity of the Hecke coefficients c_n (G3).
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Cyclo4
import lib.IntRing

namespace GaussEuclid

open FTAChain DetChain LibInt

def nrm (z : Int × Int) : Nat := Int.natAbs z.1 * Int.natAbs z.1 + Int.natAbs z.2 * Int.natAbs z.2
def gn (z : Int × Int) : Int := z.1 * z.1 + z.2 * z.2
def cj (z : Int × Int) : Int × Int := (z.1, -z.2)
def Dvd' (a b : Int × Int) : Prop := ∃ c, b = (Cyclo4.cyc4 LibInt.intR).mul a c

theorem nas (a : Int) : Int.ofNat (Int.natAbs a * Int.natAbs a) = a * a := Int.natAbs_mul_self

theorem nrm_cast (z : Int × Int) : Int.ofNat (nrm z) = gn z := by
  show Int.ofNat (Int.natAbs z.1 * Int.natAbs z.1) + Int.ofNat (Int.natAbs z.2 * Int.natAbs z.2) = _
  rw [nas, nas]; rfl

theorem mul_cj (z : Int × Int) : (Cyclo4.cyc4 LibInt.intR).mul z (cj z) = (gn z, 0) := by
  apply Prod.ext
  · show z.1 * z.1 + -(z.2 * -z.2) = z.1 * z.1 + z.2 * z.2
    lib_int
  · show z.1 * -z.2 + z.2 * z.1 = 0
    lib_int

theorem norm_mul (x y : Int × Int) : gn ((Cyclo4.cyc4 LibInt.intR).mul x y) = gn x * gn y := by
  show (x.1 * y.1 + -(x.2 * y.2)) * (x.1 * y.1 + -(x.2 * y.2)) + (x.1 * y.2 + x.2 * y.1) * (x.1 * y.2 + x.2 * y.1) = (x.1 * x.1 + x.2 * x.2) * (y.1 * y.1 + y.2 * y.2)
  lib_int

theorem gn_cj (z : Int × Int) : gn (cj z) = gn z := by
  show z.1 * z.1 + -z.2 * -z.2 = z.1 * z.1 + z.2 * z.2; lib_int

theorem le_cancel (a b : Nat) : ∀ c, a + c ≤ b + c → a ≤ b
  | 0, h => h
  | c + 1, h => le_cancel a b c (Nat.le_of_succ_le_succ h)

theorem half_split (r k M : Nat) (h : r + (k + 1) = M + 1) : 2 * r ≤ M + 1 ∨ 2 * (k + 1) ≤ M + 1 := by
  cases Nat.le_total (2 * r) (M + 1) with
  | inl h1 => exact Or.inl h1
  | inr h2 =>
    apply Or.inr
    apply le_cancel _ _ (M + 1)
    calc 2 * (k + 1) + (M + 1) ≤ 2 * (k + 1) + 2 * r := Nat.add_le_add_left h2 _
      _ = (M + 1) + (M + 1) := by rw [show 2 * (k + 1) + 2 * r = 2 * (r + (k + 1)) by lib_nat, h]; lib_nat

/-- **rounding division**: a = q(M + 1) + r with 2|r| ≤ M + 1. -/
theorem rnd (a : Int) (M : Nat) : ∃ q r : Int, a = q * Int.ofNat (M + 1) + r ∧ 2 * Int.natAbs r ≤ M + 1 := by
  cases a with
  | ofNat t =>
    match div_exists M t with
    | ⟨q, r, k, ht, hr⟩ =>
      cases half_split r k M hr with
      | inl h => exact ⟨Int.ofNat q, Int.ofNat r, congrArg Int.ofNat ht, h⟩
      | inr h =>
        refine ⟨Int.ofNat (q + 1), Int.negSucc k, ?_, h⟩
        have e : t + (k + 1) = (q + 1) * (M + 1) := by rw [ht, show q * (M + 1) + r + (k + 1) = q * (M + 1) + (r + (k + 1)) by lib_nat, hr]; lib_nat
        have eI : Int.ofNat t + Int.ofNat (k + 1) = Int.ofNat (q + 1) * Int.ofNat (M + 1) := congrArg Int.ofNat e
        show Int.ofNat t = Int.ofNat (q + 1) * Int.ofNat (M + 1) + -Int.ofNat (k + 1)
        rw [← eI]; lib_int
  | negSucc t =>
    match div_exists M (t + 1) with
    | ⟨q, r, k, ht, hr⟩ =>
      cases half_split r k M hr with
      | inl h =>
        refine ⟨-Int.ofNat q, -Int.ofNat r, ?_, by rw [Int.natAbs_neg]; exact h⟩
        have eI : Int.ofNat (t + 1) = Int.ofNat q * Int.ofNat (M + 1) + Int.ofNat r := congrArg Int.ofNat ht
        show -Int.ofNat (t + 1) = _
        rw [eI]; lib_int
      | inr h =>
        refine ⟨-Int.ofNat (q + 1), Int.ofNat (k + 1), ?_, h⟩
        have e : (t + 1) + (k + 1) = (q + 1) * (M + 1) := by rw [ht, show q * (M + 1) + r + (k + 1) = q * (M + 1) + (r + (k + 1)) by lib_nat, hr]; lib_nat
        have eI : Int.ofNat (t + 1) + Int.ofNat (k + 1) = Int.ofNat (q + 1) * Int.ofNat (M + 1) := congrArg Int.ofNat e
        show -Int.ofNat (t + 1) = _
        rw [show -Int.ofNat (t + 1) = -(Int.ofNat (t + 1) + Int.ofNat (k + 1)) + Int.ofNat (k + 1) by lib_int, eI]; lib_int

theorem sq_bound (r : Int) (M : Nat) (h : 2 * Int.natAbs r ≤ M + 1) : 4 * (Int.natAbs r * Int.natAbs r) ≤ (M + 1) * (M + 1) := by
  rw [show 4 * (Int.natAbs r * Int.natAbs r) = (2 * Int.natAbs r) * (2 * Int.natAbs r) by lib_nat]
  exact Nat.mul_le_mul h h

/-- **Division with remainder in ℤ[i].** -/
theorem gdiv (α β : Int × Int) (M : Nat) (hβ : nrm β = M + 1) :
    ∃ q, nrm ((Cyclo4.cyc4 LibInt.intR).add α ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).mul q β))) < M + 1 := by
  have hN : gn β = Int.ofNat (M + 1) := by rw [← nrm_cast, hβ]
  match rnd ((Cyclo4.cyc4 LibInt.intR).mul α (cj β)).1 M, rnd ((Cyclo4.cyc4 LibInt.intR).mul α (cj β)).2 M with
  | ⟨q1, r1, h1, b1⟩, ⟨q2, r2, h2, b2⟩ =>
    refine ⟨(q1, q2), ?_⟩
    generalize hρ : (Cyclo4.cyc4 LibInt.intR).add α ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).mul (q1, q2) β)) = ρ
    have hρc : (Cyclo4.cyc4 LibInt.intR).mul ρ (cj β) = (r1, r2) := by
      rw [← hρ]
      have e : (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add α ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).mul (q1, q2) β))) (cj β) = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul α (cj β)) ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).mul (q1, q2) ((Cyclo4.cyc4 LibInt.intR).mul β (cj β)))) := by lib_ring
      rw [e, mul_cj, hN]
      apply Prod.ext
      · show ((Cyclo4.cyc4 LibInt.intR).mul α (cj β)).1 + -(q1 * Int.ofNat (M + 1) + -(q2 * 0)) = r1
        rw [h1]; lib_int
      · show ((Cyclo4.cyc4 LibInt.intR).mul α (cj β)).2 + -(q1 * 0 + q2 * Int.ofNat (M + 1)) = r2
        rw [h2]; lib_int
    have hn : gn ρ * Int.ofNat (M + 1) = r1 * r1 + r2 * r2 := by
      rw [← hN, ← gn_cj β, ← norm_mul, hρc]; rfl
    have hnN : nrm ρ * (M + 1) = Int.natAbs r1 * Int.natAbs r1 + Int.natAbs r2 * Int.natAbs r2 := by
      apply Int.ofNat.inj
      show Int.ofNat (nrm ρ) * Int.ofNat (M + 1) = Int.ofNat (Int.natAbs r1 * Int.natAbs r1) + Int.ofNat (Int.natAbs r2 * Int.natAbs r2)
      rw [nrm_cast, nas, nas]; exact hn
    have hle : (M + 1) * (4 * nrm ρ) ≤ (M + 1) * (2 * (M + 1)) := by
      calc (M + 1) * (4 * nrm ρ) = 4 * (Int.natAbs r1 * Int.natAbs r1) + 4 * (Int.natAbs r2 * Int.natAbs r2) := by rw [← show 4 * (nrm ρ * (M + 1)) = (M + 1) * (4 * nrm ρ) by lib_nat, hnN]; lib_nat
        _ ≤ (M + 1) * (M + 1) + (M + 1) * (M + 1) := Nat.add_le_add (sq_bound r1 M b1) (sq_bound r2 M b2)
        _ = (M + 1) * (2 * (M + 1)) := by lib_nat
    have h4 := Nat.le_of_mul_le_mul_left hle (Nat.zero_lt_succ M)
    cases Nat.lt_or_ge (nrm ρ) (M + 1) with
    | inl h => exact h
    | inr h =>
      exfalso
      have : 4 * (M + 1) ≤ 2 * (M + 1) := Nat.le_trans (Nat.mul_le_mul_left 4 h) h4
      exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (show 2 * (M + 1) < 4 * (M + 1) by
        rw [show 4 * (M + 1) = 2 * (M + 1) + (2 * M + 2) by lib_nat]; exact Nat.lt_add_of_pos_right (Nat.zero_lt_succ _)) this)

theorem natAbs_zero (a : Int) (h : Int.natAbs a = 0) : a = 0 := by
  cases a with
  | ofNat t => exact congrArg Int.ofNat h
  | negSucc t => exact absurd h (fun e => Nat.noConfusion e)

theorem nrm_zero (z : Int × Int) (h : nrm z = 0) : z = (Cyclo4.cyc4 LibInt.intR).zero := by
  have h' : Int.natAbs z.1 * Int.natAbs z.1 + Int.natAbs z.2 * Int.natAbs z.2 = 0 := h
  have h1 : Int.natAbs z.1 * Int.natAbs z.1 = 0 := Nat.le_antisymm (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq h')) (Nat.zero_le _)
  have h2 : Int.natAbs z.2 * Int.natAbs z.2 = 0 := Nat.le_antisymm (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_of_eq h')) (Nat.zero_le _)
  have a1 : z.1 = 0 := natAbs_zero _ (by cases nat_mul_zero _ _ h1 with | inl e => exact e | inr e => exact e)
  have a2 : z.2 = 0 := natAbs_zero _ (by cases nat_mul_zero _ _ h2 with | inl e => exact e | inr e => exact e)
  exact Prod.ext a1 a2

/-- **gcd as a combination.** -/
theorem gcd_base (α β : Int × Int) (hb : β = (Cyclo4.cyc4 LibInt.intR).zero) :
    ∃ d x y, d = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul x α) ((Cyclo4.cyc4 LibInt.intR).mul y β) ∧ Dvd' d α ∧ Dvd' d β :=
  ⟨α, (Cyclo4.cyc4 LibInt.intR).one, (Cyclo4.cyc4 LibInt.intR).zero, by rw [hb]; lib_ring, ⟨(Cyclo4.cyc4 LibInt.intR).one, by lib_ring⟩, ⟨(Cyclo4.cyc4 LibInt.intR).zero, by rw [hb]; lib_ring⟩⟩

theorem gcd_exists : ∀ G (α β : Int × Int), nrm β ≤ G →
    ∃ d x y, d = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul x α) ((Cyclo4.cyc4 LibInt.intR).mul y β) ∧ Dvd' d α ∧ Dvd' d β
  | 0, α, β, hG => gcd_base α β (nrm_zero β (Nat.le_antisymm hG (Nat.zero_le _)))
  | G + 1, α, β, hG => by
    cases h : nrm β with
    | zero => exact gcd_base α β (nrm_zero β h)
    | succ M =>
        match gdiv α β M h with
        | ⟨q, hq⟩ =>
          have hρ : nrm ((Cyclo4.cyc4 LibInt.intR).add α ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).mul q β))) ≤ G :=
            Nat.le_of_lt_succ (Nat.lt_of_lt_of_le hq (Nat.le_trans (Nat.le_of_eq h.symm) hG))
          match gcd_exists G β _ hρ with
          | ⟨d, x, y, hd, ⟨c1, hc1⟩, ⟨c2, hc2⟩⟩ =>
            refine ⟨d, y, (Cyclo4.cyc4 LibInt.intR).add x ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).mul y q)), by rw [hd]; lib_ring, ⟨(Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul q c1) c2, ?_⟩, ⟨c1, hc1⟩⟩
            have e : α = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul q β) ((Cyclo4.cyc4 LibInt.intR).add α ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).mul q β))) := by lib_ring
            rw [e, hc2, hc1]
            lib_ring

/-- **Euclid's lemma in ℤ[i].** -/
theorem euclid_coprime (α β γ x y : Int × Int) (h1 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul x α) ((Cyclo4.cyc4 LibInt.intR).mul y β) = (Cyclo4.cyc4 LibInt.intR).one) (h : Dvd' α ((Cyclo4.cyc4 LibInt.intR).mul β γ)) : Dvd' α γ :=
  match h with
  | ⟨c, hc⟩ => ⟨(Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul x γ) ((Cyclo4.cyc4 LibInt.intR).mul y c), by
      calc γ = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul x α) ((Cyclo4.cyc4 LibInt.intR).mul y β)) γ := by rw [h1]; lib_ring
        _ = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul x α) γ) ((Cyclo4.cyc4 LibInt.intR).mul y ((Cyclo4.cyc4 LibInt.intR).mul β γ)) := by lib_ring
        _ = (Cyclo4.cyc4 LibInt.intR).mul α ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul x γ) ((Cyclo4.cyc4 LibInt.intR).mul y c)) := by rw [hc]; lib_ring⟩

end GaussEuclid

#print axioms GaussEuclid.nrm_cast
#print axioms GaussEuclid.mul_cj
#print axioms GaussEuclid.norm_mul
#print axioms GaussEuclid.half_split
#print axioms GaussEuclid.rnd
#print axioms GaussEuclid.gdiv
#print axioms GaussEuclid.nrm_zero
#print axioms GaussEuclid.gcd_base
#print axioms GaussEuclid.gcd_exists
#print axioms GaussEuclid.euclid_coprime

lang/GaussFactor.lean

lean_all/lang/GaussFactor.lean

/-
  lang/GaussFactor — T43, cycle 160: unique factorisation in ℤ[i] above a split p.

  π = a + bi with a² + b² = p prime. Every α ∈ ℤ[i] with N(α) = p^k is u · π^j · π̄^l, j + l = k, u ∈ {±1, ±i}:
    gi_ext, gmul_comm, gmul_assoc, gpow     ℤ[i] arithmetic on GaussInt.GI
    norm_one                                N(α) = 1 ⇒ α a unit
    factor                                  the classification, by induction on k with GaussDiv.gauss_div
  The primary elements of norm p^k are therefore among the π^j π̄^(k−j) up to units — the input for a(𝔽_{p^k}).

  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.GaussDiv
import lang.StrictHasse

namespace GaussFactor

open FTAChain LibInt FermatUnique GaussInt

theorem gi_ext (x y : GI) (h1 : x.re = y.re) (h2 : x.im = y.im) : x = y := by
  cases x; cases y; cases h1; cases h2; rfl

theorem gmul_comm (x y : GI) : gmul x y = gmul y x :=
  gi_ext _ _ (by show x.re * y.re - x.im * y.im = y.re * x.re - y.im * x.im; lib_int)
    (by show x.re * y.im + x.im * y.re = y.re * x.im + y.im * x.re; lib_int)

theorem gmul_assoc (x y z : GI) : gmul (gmul x y) z = gmul x (gmul y z) :=
  gi_ext _ _ (by show (x.re * y.re - x.im * y.im) * z.re - (x.re * y.im + x.im * y.re) * z.im
      = x.re * (y.re * z.re - y.im * z.im) - x.im * (y.re * z.im + y.im * z.re); lib_int)
    (by show (x.re * y.re - x.im * y.im) * z.im + (x.re * y.im + x.im * y.re) * z.re
      = x.re * (y.re * z.im + y.im * z.re) + x.im * (y.re * z.re - y.im * z.im); lib_int)

def gone : GI := ⟨1, 0⟩

def gpow (x : GI) : Nat → GI
  | 0 => gone
  | n + 1 => gmul (gpow x n) x

def ipow (P : Int) : Nat → Int
  | 0 => 1
  | n + 1 => ipow P n * P

def IsUnit (u : GI) : Prop := u = ⟨1, 0⟩ ∨ u = ⟨-1, 0⟩ ∨ u = ⟨0, 1⟩ ∨ u = ⟨0, -1⟩

theorem nat_sq_one (n m : Nat) (h : n * n + m * m = 1) : (n = 1 ∧ m = 0) ∨ (n = 0 ∧ m = 1) := by
  match n, m with
  | 0, 0 => exact Nat.noConfusion h
  | 1, 0 => exact Or.inl ⟨rfl, rfl⟩
  | 0, 1 => exact Or.inr ⟨rfl, rfl⟩
  | 0, k + 2 => exact Nat.noConfusion (Nat.succ.inj ((show 0 * 0 + (k + 2) * (k + 2) = (k * k + 4 * k + 2) + 1 + 1 by lib_nat).symm.trans h))
  | k + 2, m => exact Nat.noConfusion (Nat.succ.inj ((show (k + 2) * (k + 2) + m * m = (k * k + 4 * k + 2 + m * m) + 1 + 1 by lib_nat).symm.trans h))
  | 1, k + 1 => exact Nat.noConfusion (Nat.succ.inj ((show 1 * 1 + (k + 1) * (k + 1) = (k * k + 2 * k) + 1 + 1 by lib_nat).symm.trans h))

theorem norm_one (x : GI) (h : gnorm x = 1) : IsUnit x := by
  match StrictHasse.sq_nat x.re, StrictHasse.sq_nat x.im with
  | ⟨n, hn⟩, ⟨m, hm⟩ =>
    have e : n * n + m * m = 1 := Int.ofNat.inj (show Int.ofNat (n * n) + Int.ofNat (m * m) = 1 by rw [← hn, ← hm]; exact h)
    cases nat_sq_one n m e with
    | inl h1 =>
      have hi : x.im = 0 := HasseDavenport.sq_zero _ (by rw [hm, h1.2]; rfl)
      cases QuarticFamily.sq_eq x.re 1 (by rw [hn, h1.1]; rfl) with
      | inl q => exact Or.inl (gi_ext _ _ q hi)
      | inr q => exact Or.inr (Or.inl (gi_ext _ _ q hi))
    | inr h1 =>
      have hr : x.re = 0 := HasseDavenport.sq_zero _ (by rw [hn, h1.1]; rfl)
      cases QuarticFamily.sq_eq x.im 1 (by rw [hm, h1.2]; rfl) with
      | inl q => exact Or.inr (Or.inr (Or.inl (gi_ext _ _ hr q)))
      | inr q => exact Or.inr (Or.inr (Or.inr (gi_ext _ _ hr q)))

theorem gone_mul (x : GI) : gmul gone x = x :=
  gi_ext _ _ (by show 1 * x.re - 0 * x.im = x.re; lib_int) (by show 1 * x.im + 0 * x.re = x.im; lib_int)

/-- **Factorisation above p.** -/
theorem factor (p : Nat) (hp : ∀ d, Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (a b : Int)
    (hab : a * a + b * b = Int.ofNat (p + 1 + 1)) : ∀ (k : Nat) (x : GI), gnorm x = ipow (Int.ofNat (p + 1 + 1)) k →
    ∃ u j l, IsUnit u ∧ j + l = k ∧ x = gmul u (gmul (gpow ⟨a, b⟩ j) (gpow ⟨a, -b⟩ l))
  | 0, x, h => ⟨x, 0, 0, norm_one x h, rfl, by
      show x = gmul x (gmul gone gone)
      rw [gone_mul, gmul_comm, gone_mul]⟩
  | k + 1, x, h => by
    have hn : x.re * x.re + x.im * x.im = Int.ofNat (p + 1 + 1) * ipow (Int.ofNat (p + 1 + 1)) k := by
      rw [show Int.ofNat (p + 1 + 1) * ipow (Int.ofNat (p + 1 + 1)) k = ipow (Int.ofNat (p + 1 + 1)) (k + 1) by
        show _ = ipow _ k * _; lib_int]; exact h
    have normq : ∀ (q : GI) (x' y' : Int), gnorm q = Int.ofNat (p + 1 + 1) → x = gmul q ⟨x', y'⟩ →
        gnorm ⟨x', y'⟩ = ipow (Int.ofNat (p + 1 + 1)) k := fun q x' y' hq e => by
      apply cancelP p
      rw [← hn, ← hq, ← norm_mul, ← e]; rfl
    cases GaussDiv.gauss_div p hp a b x.re x.im _ hab hn with
    | inl h1 =>
      match h1 with
      | ⟨x', y', e1, e2⟩ =>
        have ex : x = gmul ⟨a, b⟩ ⟨x', y'⟩ := gi_ext _ _ e1 e2
        match factor p hp a b hab k ⟨x', y'⟩ (normq ⟨a, b⟩ x' y' hab ex) with
        | ⟨u, j, l, hu, hjl, hg⟩ =>
          refine ⟨u, j + 1, l, hu, by rw [← hjl]; lib_nat, ?_⟩
          rw [ex, hg]
          show gmul ⟨a, b⟩ (gmul u (gmul (gpow ⟨a, b⟩ j) (gpow ⟨a, -b⟩ l))) = gmul u (gmul (gmul (gpow ⟨a, b⟩ j) ⟨a, b⟩) (gpow ⟨a, -b⟩ l))
          rw [← gmul_assoc, gmul_comm ⟨a, b⟩ u, gmul_assoc, gmul_assoc, ← gmul_assoc ⟨a, b⟩, gmul_comm ⟨a, b⟩ (gpow ⟨a, b⟩ j), gmul_assoc]
    | inr h1 =>
      match h1 with
      | ⟨x', y', e1, e2⟩ =>
        have ex : x = gmul ⟨a, -b⟩ ⟨x', y'⟩ :=
          gi_ext _ _ (by rw [e1]; show _ = a * x' - -b * y'; lib_int) (by rw [e2]; show _ = a * y' + -b * x'; lib_int)
        match factor p hp a b hab k ⟨x', y'⟩ (normq _ x' y' (by rw [← hab]; show a * a + -b * -b = _; lib_int) ex) with
        | ⟨u, j, l, hu, hjl, hg⟩ =>
          refine ⟨u, j, l + 1, hu, by rw [← hjl]; lib_nat, ?_⟩
          rw [ex, hg]
          show gmul ⟨a, -b⟩ (gmul u (gmul (gpow ⟨a, b⟩ j) (gpow ⟨a, -b⟩ l))) = gmul u (gmul (gpow ⟨a, b⟩ j) (gmul (gpow ⟨a, -b⟩ l) ⟨a, -b⟩))
          rw [← gmul_assoc, gmul_comm ⟨a, -b⟩ u, gmul_assoc, ← gmul_assoc ⟨a, -b⟩, gmul_comm ⟨a, -b⟩ (gpow ⟨a, b⟩ j),
            gmul_assoc, gmul_comm ⟨a, -b⟩ (gpow ⟨a, -b⟩ l)]

end GaussFactor

#print axioms GaussFactor.gi_ext
#print axioms GaussFactor.gmul_comm
#print axioms GaussFactor.gmul_assoc
#print axioms GaussFactor.gone
#print axioms GaussFactor.gpow
#print axioms GaussFactor.ipow
#print axioms GaussFactor.nat_sq_one
#print axioms GaussFactor.norm_one
#print axioms GaussFactor.gone_mul
#print axioms GaussFactor.factor

lang/GaussFrob.lean

lean_all/lang/GaussFrob.lean

/-
  lang/GaussFrob — G1 (T43): Frobenius on the Gauss sum.  g^q = (q/p)·g  in 𝔽_q[C_p].

  p = 2m + 3, q = 2r + 3 distinct primes, χ = (·/p) (QChar), g = Σ_a χ(a) ζ^a ∈ 𝔽_q[C_p] (lang/GroupRing). In characteristic q
    g^q = Σ_a χ(a)^q ζ^{qa} = Σ_a χ(a) ζ^{qa}                      (GroupRingFrob.frob_sum, mono_pow, Fermat t^q = t)
        = Σ_b χ(q⁻¹ b) ζ^b = χ(q) g                                 (reindex a ↦ q a;  χ(q⁻¹) = χ(q))
    gq            the Gauss sum in 𝔽_q[C_p]
    chi_inv       χ(x⁻¹) = χ(x) for x ≠ 0
    coef_frob     the ζ^c coefficient of Σ_a s_a ζ^{qa} is s_{q⁻¹ c}
    gauss_frob    (g^q)(c) = χ(q)·g(c)
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.GroupRingFrob
import lang.GaussSum
import lang.HasseDavenport

namespace GaussFrob

open FTAChain ZMod YMChain FiniteRecChain DetChain GroupRing GroupRingFrob Frobenius QChar ConicSum
open LibRing (castZ castN)

variable (m r : Nat)

/-- the Gauss sum Σ_a χ(a) ζ^a in 𝔽_q[C_p]. -/
def gq : V (Zp (2 * r + 1 + 1)) ((2 * m + 1 + 1) + 1) := tb (2 * m + 1 + 1) (fun a => castZ (ring (2 * r + 1 + 1)) (chi m a))

theorem chi_inv (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x : Zp (2 * m + 1 + 1)) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) :
    chi m (iv m x) = chi m x := by
  have h1 : chi m (iv m x) * chi m x = 1 := by
    rw [← chi_mul m hpr, iv_mul_self m hpr x hx]; exact chi_one m
  have h2 : chi m x * chi m x = 1 := by rw [← chi_mul m hpr]; exact chi_sq m hpr x hx
  calc chi m (iv m x) = chi m (iv m x) * (chi m x * chi m x) := by rw [h2]; lib_int
    _ = (chi m (iv m x) * chi m x) * chi m x := by lib_int
    _ = chi m x := by rw [h1]; lib_int

theorem coef_frob (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (Q : Zp (2 * m + 1 + 1)) (hQ : Q ≠ (ring (2 * m + 1 + 1)).zero)
    (s : Zp (2 * m + 1 + 1) → Zp (2 * r + 1 + 1)) (c : Zp (2 * m + 1 + 1)) :
    sumK (ring (2 * r + 1 + 1)) (fun a => (ring (2 * r + 1 + 1)).mul (s a) (delta (ring (2 * r + 1 + 1)) c ((ring (2 * m + 1 + 1)).mul Q a))) (L (2 * m + 1 + 1)) = s ((ring (2 * m + 1 + 1)).mul (iv m Q) c) := by
  have hQQ := iv_mul_self m hpr Q hQ
  have e := reindex (ring (2 * r + 1 + 1)) _ (CMInert.complete_allZ (2 * m + 1 + 1) (ring (2 * r + 1 + 1))) (fun a => (ring (2 * m + 1 + 1)).mul Q a) (fun b => (ring (2 * m + 1 + 1)).mul (iv m Q) b)
    (fun x => by rw [show (ring (2 * m + 1 + 1)).mul (iv m Q) ((ring (2 * m + 1 + 1)).mul Q x) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m Q) Q) x by lib_ring, hQQ]; lib_ring)
    (fun y => by rw [show (ring (2 * m + 1 + 1)).mul Q ((ring (2 * m + 1 + 1)).mul (iv m Q) y) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m Q) Q) y by lib_ring, hQQ]; lib_ring)
    (fun b => (ring (2 * r + 1 + 1)).mul (delta (ring (2 * r + 1 + 1)) b c) (s ((ring (2 * m + 1 + 1)).mul (iv m Q) b)))
  have e2 : sumK (ring (2 * r + 1 + 1)) (fun a => (ring (2 * r + 1 + 1)).mul (s a) (delta (ring (2 * r + 1 + 1)) c ((ring (2 * m + 1 + 1)).mul Q a))) (L (2 * m + 1 + 1))
      = sumK (ring (2 * r + 1 + 1)) (fun a => (fun b => (ring (2 * r + 1 + 1)).mul (delta (ring (2 * r + 1 + 1)) b c) (s ((ring (2 * m + 1 + 1)).mul (iv m Q) b))) ((ring (2 * m + 1 + 1)).mul Q a)) (L (2 * m + 1 + 1)) :=
    sumK_ext (ring (2 * r + 1 + 1)) _ _ (fun a => by
      show _ = (ring (2 * r + 1 + 1)).mul (delta (ring (2 * r + 1 + 1)) ((ring (2 * m + 1 + 1)).mul Q a) c) (s ((ring (2 * m + 1 + 1)).mul (iv m Q) ((ring (2 * m + 1 + 1)).mul Q a)))
      rw [show (ring (2 * m + 1 + 1)).mul (iv m Q) ((ring (2 * m + 1 + 1)).mul Q a) = a by rw [show (ring (2 * m + 1 + 1)).mul (iv m Q) ((ring (2 * m + 1 + 1)).mul Q a) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m Q) Q) a by lib_ring, hQQ]; lib_ring,
        delta_symm, (ring (2 * r + 1 + 1)).mul_comm]) _
  rw [e2]
  exact e.trans (pick (ring (2 * r + 1 + 1)) _ (CMInert.complete_allZ (2 * m + 1 + 1) (ring (2 * r + 1 + 1))) _ c)

theorem fermat_q (hq : ∀ d, Dv d (2 * r + 1 + 1 + 1) → d = 1 ∨ d = 2 * r + 1 + 1 + 1) (x : Zp (2 * r + 1 + 1)) :
    pw (ring (2 * r + 1 + 1)) x (2 * r + 1 + 1 + 1) = x := fermat (2 * r + 1) hq x

/-- **g^q = χ(q)·g** in 𝔽_q[C_p], coefficientwise. -/
theorem gauss_frob (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1)
    (hq : ∀ d, Dv d (2 * r + 1 + 1 + 1) → d = 1 ∨ d = 2 * r + 1 + 1 + 1)
    (hQ : nsm (ring (2 * m + 1 + 1)) (2 * r + 1 + 1 + 1) (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero) (c : Zp (2 * m + 1 + 1)) :
    gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (pw (gring (ring (2 * r + 1 + 1)) (2 * m + 1 + 1)) (gq m r) (2 * r + 1 + 1 + 1)) c
      = (ring (2 * r + 1 + 1)).mul (castZ (ring (2 * r + 1 + 1)) (chi m (nsm (ring (2 * m + 1 + 1)) (2 * r + 1 + 1 + 1) (ring (2 * m + 1 + 1)).one))) (gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (gq m r) c) := by
  have hd := decomp (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (gq m r)
  have e1 : gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (pw (gring (ring (2 * r + 1 + 1)) (2 * m + 1 + 1)) (gq m r) (2 * r + 1 + 1 + 1)) c
      = gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (pw (gring (ring (2 * r + 1 + 1)) (2 * m + 1 + 1)) (sumK (gring (ring (2 * r + 1 + 1)) (2 * m + 1 + 1)) (fun a => mono (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) a (gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (gq m r) a)) (L (2 * m + 1 + 1))) (2 * r + 1 + 1 + 1)) c :=
    congrArg (fun u => gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (pw (gring (ring (2 * r + 1 + 1)) (2 * m + 1 + 1)) u (2 * r + 1 + 1 + 1)) c) hd
  rw [e1, frob_sum (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (2 * r + 1) hq (char_p (2 * r + 1 + 1)), gt_sum]
  have e2 : sumK (ring (2 * r + 1 + 1)) (fun a => gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (pw (gring (ring (2 * r + 1 + 1)) (2 * m + 1 + 1)) (mono (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) a (gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (gq m r) a)) (2 * r + 1 + 1 + 1)) c) (L (2 * m + 1 + 1))
      = sumK (ring (2 * r + 1 + 1)) (fun a => (ring (2 * r + 1 + 1)).mul (castZ (ring (2 * r + 1 + 1)) (chi m a)) (delta (ring (2 * r + 1 + 1)) c ((ring (2 * m + 1 + 1)).mul (nsm (ring (2 * m + 1 + 1)) (2 * r + 1 + 1 + 1) (ring (2 * m + 1 + 1)).one) a))) (L (2 * m + 1 + 1)) :=
    sumK_ext (ring (2 * r + 1 + 1)) _ _ (fun a => by
      rw [mono_pow, gt_mono, fermat_q r hq, nsm_eq_mul]
      unfold gq; rw [gt_tb]) _
  rw [e2, coef_frob m r hpr _ hQ (fun a => castZ (ring (2 * r + 1 + 1)) (chi m a)) c, chi_mul m hpr, chi_inv m hpr _ hQ, HasseDavenport.castZ_mul]
  unfold gq; rw [gt_tb]

end GaussFrob

#print axioms GaussFrob.gq
#print axioms GaussFrob.chi_inv
#print axioms GaussFrob.coef_frob
#print axioms GaussFrob.fermat_q
#print axioms GaussFrob.gauss_frob

lang/GaussFrobN.lean

lean_all/lang/GaussFrobN.lean

/-
  lang/GaussFrobN — T49, cycle 602: **the Frobenius of a Gauss sum for an arbitrary multiplicative character** (any order n):
  E a commutative ring of prime characteristic q, ψ : 𝔽_p → E multiplicative with ψ^q = ψ (e.g. values in μ_n ⊂ E, q ≡ 1 mod n),
  G_ψ = Σ_a ψ(a) ζ^a ∈ E[C_p]:
    G_ψ^q = ψ(q⁻¹) G_ψ          (Frobenius + reindex a ↦ q⁻¹a)
  The common first step of the cubic and quartic reciprocity proofs (CubicRecipSplit, QuartRecip; GaussFrob for the quadratic character), now for every n.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.GroupRingFrob

namespace GroupRingFrob

open FTAChain ZMod YMChain FiniteRecChain DetChain GroupRing Frobenius

variable {S : Type} (E : CRing S) (j : Nat)

def gauss (ψ : Zp (j + 1) → S) : V S (j + 1 + 1) := sumK (gring E (j + 1)) (fun a => mono E (j + 1) a (ψ a)) (L (j + 1))

theorem gauss_frob (hpr : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1) (k : Nat) (hq : ∀ d, Dv d (k + 1 + 1) → d = 1 ∨ d = k + 1 + 1)
    (hE : nsm E (k + 1 + 1) E.one = E.zero) (ψ : Zp (j + 1) → S)
    (hmul : ∀ a b, ψ ((ring (j + 1)).mul a b) = E.mul (ψ a) (ψ b)) (hfix : ∀ a, pw E (ψ a) (k + 1 + 1) = ψ a)
    (hQ : nsm (ring (j + 1)) (k + 1 + 1) (ring (j + 1)).one ≠ (ring (j + 1)).zero) :
    pw (gring E (j + 1)) (gauss E j ψ) (k + 1 + 1) =
      gmul E (j + 1) (mono E (j + 1) (ring (j + 1)).zero (ψ (pw (ring (j + 1)) (nsm (ring (j + 1)) (k + 1 + 1) (ring (j + 1)).one) j))) (gauss E j ψ) := by
  generalize hQd : nsm (ring (j + 1)) (k + 1 + 1) (ring (j + 1)).one = Q at hQ
  have hinv := inv_exists j hpr Q hQ
  generalize hQi : pw (ring (j + 1)) Q j = Qi at hinv
  -- LHS = Σ_a mono (Q a) (ψ a)
  have hL : pw (gring E (j + 1)) (gauss E j ψ) (k + 1 + 1) = sumK (gring E (j + 1)) (fun a => mono E (j + 1) ((ring (j + 1)).mul Q a) (ψ a)) (L (j + 1)) := by
    unfold gauss
    rw [frob_sum E (j + 1) k hq hE]
    apply sumK_ext; intro a
    rw [mono_pow, hfix, nsm_eq_mul, hQd]
  -- reindex a = Qi b
  have hc := CMInert.complete_allZ (j + 1) (gring E (j + 1))
  have hre := reindex (gring E (j + 1)) (L (j + 1)) hc (fun b => (ring (j + 1)).mul Qi b) (fun a => (ring (j + 1)).mul Q a)
    (fun b => by show (ring (j + 1)).mul Q ((ring (j + 1)).mul Qi b) = b
                 calc _ = (ring (j + 1)).mul ((ring (j + 1)).mul Qi Q) b := by lib_ring
                   _ = b := by rw [hinv]; lib_ring)
    (fun a => by show (ring (j + 1)).mul Qi ((ring (j + 1)).mul Q a) = a
                 calc _ = (ring (j + 1)).mul ((ring (j + 1)).mul Qi Q) a := by lib_ring
                   _ = a := by rw [hinv]; lib_ring)
    (fun a => mono E (j + 1) ((ring (j + 1)).mul Q a) (ψ a))
  rw [hL, ← hre]
  unfold gauss
  show _ = (gring E (j + 1)).mul (mono E (j + 1) (ring (j + 1)).zero (ψ Qi)) (sumK (gring E (j + 1)) (fun a => mono E (j + 1) a (ψ a)) (L (j + 1)))
  rw [mul_sumK]
  apply sumK_ext; intro b
  show _ = gmul E (j + 1) (mono E (j + 1) (ring (j + 1)).zero (ψ Qi)) (mono E (j + 1) b (ψ b))
  rw [mono_mul, ← hmul]
  have e1 : (ring (j + 1)).mul Q ((ring (j + 1)).mul Qi b) = (ring (j + 1)).add (ring (j + 1)).zero b := by
    calc _ = (ring (j + 1)).mul ((ring (j + 1)).mul Qi Q) b := by lib_ring
      _ = _ := by rw [hinv]; lib_ring
  rw [e1]

end GroupRingFrob

#print axioms GroupRingFrob.gauss_frob

lang/GaussInt.lean

lean_all/lang/GaussInt.lean

/-
  lang/GaussInt — T43, cycle 49: Gaussian integers and the Hecke character of ℚ(i) on primary elements.

  α = a + b i ∈ ℤ[i] as a pair of integers; α is primary when b ≡ 0 (mod 2) and a + b ≡ 1 (mod 4) (α ≡ 1 mod (2 + 2i)).
    gmul, gnorm, gconj
    norm_mul        N(αβ) = N(α) N(β)
    prim_mul        primary · primary is primary            (the Hecke character ψ(α) = α on primary α is multiplicative)
    prim_conj       ᾱ is primary when α is
    prim_choice     u odd, v even ⇒ exactly one of u + vi, −u + vi is primary
    hecke_trace     for primary π = a + bi: ψ(π) + ψ(π̄) = 2a
  With CMModular (a_p(y² = x³ − x) = ±2u) and TwoSquares (p = u² + v²): |a_p| = |ψ(π) + ψ(π̄)| at every split prime, π the
  primary prime above p; the sign is the remaining statement (a_p = ψ(π) + ψ(π̄) exactly, a mod-8 count on the curve).

  Lean 4 core only, proofs by hand (lib_int from lib_shared), no axioms.
-/
import lib.IntRing
import lib.Gcd
import QRChain

namespace GaussInt

open LibGcd QRChain

structure GI where
  re : Int
  im : Int

def gmul (x y : GI) : GI := ⟨x.re * y.re - x.im * y.im, x.re * y.im + x.im * y.re⟩
def gconj (x : GI) : GI := ⟨x.re, -x.im⟩
def gnorm (x : GI) : Int := x.re * x.re + x.im * x.im

def Primary (x : GI) : Prop := IDv 2 x.im ∧ IDv 4 (x.re + x.im - 1)

theorem int_even_odd (z : Int) : (∃ e, z = 2 * e) ∨ (∃ e, z = 2 * e + 1) := by
  cases z with
  | ofNat n =>
    cases hp : par n with
    | false => match (par_split n).1 hp with
      | ⟨k, hk⟩ => exact Or.inl ⟨Int.ofNat k, by
          rw [hk]; show Int.ofNat k + Int.ofNat k = 2 * Int.ofNat k; lib_int⟩
    | true => match (par_split n).2 hp with
      | ⟨k, hk⟩ => exact Or.inr ⟨Int.ofNat k, by
          rw [hk]; show Int.ofNat k + Int.ofNat k + 1 = 2 * Int.ofNat k + 1; lib_int⟩
  | negSucc n =>
    have e : Int.negSucc n = -Int.ofNat (n + 1) := rfl
    rw [e]
    cases hp : par (n + 1) with
    | false => match (par_split (n + 1)).1 hp with
      | ⟨k, hk⟩ => exact Or.inl ⟨-Int.ofNat k, by
          rw [hk]; show -(Int.ofNat k + Int.ofNat k) = 2 * -Int.ofNat k; lib_int⟩
    | true => match (par_split (n + 1)).2 hp with
      | ⟨k, hk⟩ => exact Or.inr ⟨-Int.ofNat k - 1, by
          rw [hk]; show -(Int.ofNat k + Int.ofNat k + 1) = 2 * (-Int.ofNat k - 1) + 1; lib_int⟩

theorem two_ne_one (e : Int) (h : 2 * e = 1) : False := by
  cases e with
  | ofNat n =>
    have h' : Int.ofNat (2 * n) = Int.ofNat 1 := h
    have := congrArg par (Int.ofNat.inj h')
    rw [show 2 * n = n + n by lib_nat, par_double] at this
    exact Bool.noConfusion this
  | negSucc n =>
    have h' : Int.negSucc (2 * n + 1) = Int.ofNat 1 := by
      have : (2 : Int) * Int.negSucc n = Int.negSucc (2 * n + 1) := by
        show -(2 * Int.ofNat (n + 1)) = Int.negSucc (2 * n + 1)
        rw [show (2 : Int) * Int.ofNat (n + 1) = Int.ofNat (2 * n + 1 + 1) by
          show Int.ofNat (2 * (n + 1)) = Int.ofNat (2 * n + 1 + 1); rw [show 2 * (n + 1) = 2 * n + 1 + 1 by lib_nat]]
        rfl
      rw [← this]; exact h
    exact Int.noConfusion h'

theorem int_odd_ne_even (w t : Int) (h : 2 * w + 1 = 2 * t) : False :=
  two_ne_one (t - w) (by rw [show 2 * (t - w) = 2 * t - 2 * w by lib_int, ← h]; lib_int)

theorem norm_mul (x y : GI) : gnorm (gmul x y) = gnorm x * gnorm y := by
  show (x.re * y.re - x.im * y.im) * (x.re * y.re - x.im * y.im) + (x.re * y.im + x.im * y.re) * (x.re * y.im + x.im * y.re)
    = (x.re * x.re + x.im * x.im) * (y.re * y.re + y.im * y.im)
  lib_int

theorem prim_mul (x y : GI) (hx : Primary x) (hy : Primary y) : Primary (gmul x y) := by
  match hx, hy with
  | ⟨⟨b, hb⟩, ⟨s, hs⟩⟩, ⟨⟨d, hd⟩, ⟨t, ht⟩⟩ =>
    refine ⟨⟨x.re * d + b * y.re, ?_⟩, ⟨s + t + 4 * s * t - 2 * b * d, ?_⟩⟩
    · show x.re * y.im + x.im * y.re = 2 * (x.re * d + b * y.re)
      rw [hb, hd]; lib_int
    · show x.re * y.re - x.im * y.im + (x.re * y.im + x.im * y.re) - 1 = 4 * (s + t + 4 * s * t - 2 * b * d)
      have ex : x.re = 4 * s + 1 - x.im := by rw [← hs]; lib_int
      have ey : y.re = 4 * t + 1 - y.im := by rw [← ht]; lib_int
      rw [ex, ey, hb, hd]; lib_int

theorem prim_conj (x : GI) (hx : Primary x) : Primary (gconj x) := by
  match hx with
  | ⟨⟨b, hb⟩, ⟨s, hs⟩⟩ =>
    refine ⟨⟨-b, ?_⟩, ⟨s - b, ?_⟩⟩
    · show -x.im = 2 * -b
      rw [hb]; lib_int
    · show x.re + -x.im - 1 = 4 * (s - b)
      have ex : x.re = 4 * s + 1 - x.im := by rw [← hs]; lib_int
      rw [ex, hb]; lib_int

/-- **Exactly one sign of the real part is primary** (u odd, v even). -/
theorem prim_choice (u v w c : Int) (hu : u = 2 * w + 1) (hv : v = 2 * c) :
    (Primary ⟨u, v⟩ ∨ Primary ⟨-u, v⟩) ∧ ¬ (Primary ⟨u, v⟩ ∧ Primary ⟨-u, v⟩) := by
  constructor
  · -- u + v − 1 = 2(w + c); if w + c is even the first, else −u + v − 1 = 2(c − w − 1) has the other parity
    match int_even_odd (w + c) with
    | Or.inl ⟨e, he⟩ =>
      left
      exact ⟨⟨c, hv⟩, ⟨e, by show u + v - 1 = 4 * e; rw [hu, hv, show 2 * w + 1 + 2 * c - 1 = 2 * (w + c) by lib_int, he]; lib_int⟩⟩
    | Or.inr ⟨e, he⟩ =>
      right
      exact ⟨⟨c, hv⟩, ⟨c - e - 1, by
        show -u + v - 1 = 4 * (c - e - 1)
        have : w = 2 * e + 1 - c := by rw [← he]; lib_int
        rw [hu, hv, this]; lib_int⟩⟩
  · intro ⟨⟨_, ⟨s, hs⟩⟩, ⟨_, ⟨t, ht⟩⟩⟩
    -- adding: 2v − 2 = 4(s + t) and subtracting: 2u = 4(s − t), so u even
    have h2 : (2 : Int) * u = 4 * (s - t) := by
      have h1 : u + v - 1 = 4 * s := hs
      have h1' : -u + v - 1 = 4 * t := ht
      have : 2 * u = (u + v - 1) - (-u + v - 1) := by lib_int
      rw [this, h1, h1']; lib_int
    have hu2 : u = 2 * (s - t) := by
      have : (2 : Int) * (u - 2 * (s - t)) = 0 := by rw [show (2 : Int) * (u - 2 * (s - t)) = 2 * u - 4 * (s - t) by lib_int, h2]; lib_int
      cases LibInt.int_noZD _ _ this with
      | inl h => exact absurd h (fun e => Nat.noConfusion (Int.ofNat.inj e))
      | inr h =>
        have h0 : u - 2 * (s - t) = 0 := h
        have e1 : u = (u - 2 * (s - t)) + 2 * (s - t) := by lib_int
        rw [h0] at e1
        rw [e1]; lib_int
    rw [hu] at hu2
    exact int_odd_ne_even w (s - t) hu2

/-- **The Hecke trace**: ψ(π) + ψ(π̄) = 2 Re π. -/
theorem hecke_trace (x : GI) : x.re + (gconj x).re = 2 * x.re := by
  show x.re + x.re = 2 * x.re
  lib_int

end GaussInt

#print axioms GaussInt.int_even_odd
#print axioms GaussInt.two_ne_one
#print axioms GaussInt.int_odd_ne_even
#print axioms GaussInt.norm_mul
#print axioms GaussInt.prim_mul
#print axioms GaussInt.prim_conj
#print axioms GaussInt.prim_choice
#print axioms GaussInt.hecke_trace

lang/GaussMod.lean

lean_all/lang/GaussMod.lean

/-
  lang/GaussMod — T47, cycle 549: **Gauss's identity modulo q^M**: in any commutative ring with q^M = 0,
    Σ_{m ≤ 4M} (−1)^m q^((m − 2M)²) = (q²; q²)_M · ∏_{k<2M} (−(1 − q^(2k+1))²),
  i.e. Σ_j (−1)^j q^(j²) = ∏(1 − q^(2k))(1 − q^(2k−1))² = ∏(1 − q^n)²/(1 − q^(2n)) coefficientwise below degree M.

    pw_ge, pp_stable, unit_sub, pp_unit    nilpotent bookkeeping: q^(M+t) = 0, (Q;Q)_(T+t) = (Q;Q)_T, 1 − x and (Q;Q)_n are units
    ex_ge                                   Ex N m ≥ N − m and ≥ m − N
    es_scale                                c·Σ f = Σ g if c·f = g termwise
    gauss_mod
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.FinJTP3
import lang.ChebKey

namespace QBinom

open FTAChain

variable {R : Type} (K : DetChain.CRing R)

theorem pw_ge (x : R) (M : Nat) (h : Frobenius.pw K x M = K.zero) : ∀ t, Frobenius.pw K x (M + t) = K.zero
  | 0 => h
  | t + 1 => by show K.mul (Frobenius.pw K x (M + t)) x = K.zero; rw [pw_ge x M h t]; lib_ring

theorem pp_stable (Q : R) (T : Nat) (h : Frobenius.pw K Q T = K.zero) : ∀ t, pp K Q (T + t) = pp K Q T
  | 0 => rfl
  | t + 1 => by
    show K.mul (pp K Q (T + t)) (K.add K.one (K.neg (Frobenius.pw K Q (T + t + 1)))) = _
    rw [pp_stable Q T h t, show T + t + 1 = T + (t + 1) from rfl, pw_ge K Q T h]; lib_ring

theorem unit_sub (x : R) (M : Nat) (h : Frobenius.pw K x M = K.zero) : ∃ y, K.mul (K.add K.one (K.neg x)) y = K.one :=
  ⟨Cheb.geo K x M, by
    have g := Cheb.geo_mul K x M
    rw [h] at g
    calc _ = K.neg (K.mul (K.add x (K.neg K.one)) (Cheb.geo K x M)) := by lib_ring
      _ = _ := by rw [g]; lib_ring⟩

theorem pp_unit (Q : R) (T : Nat) (h : Frobenius.pw K Q T = K.zero) : ∀ n, ∃ y, K.mul (pp K Q n) y = K.one
  | 0 => ⟨K.one, by show K.mul K.one K.one = K.one; lib_ring⟩
  | n + 1 => by
    obtain ⟨y, hy⟩ := pp_unit Q T h n
    have hx : Frobenius.pw K (Frobenius.pw K Q (n + 1)) T = K.zero := by
      rw [TwoPower.pwpow, show (n + 1) * T = T + n * T by lib_nat, pw_ge K Q T h]
    obtain ⟨u, hu⟩ := unit_sub K _ T hx
    refine ⟨K.mul y u, ?_⟩
    show K.mul (K.mul (pp K Q n) (K.add K.one (K.neg (Frobenius.pw K Q (n + 1))))) (K.mul y u) = K.one
    calc _ = K.mul (K.mul (pp K Q n) y) (K.mul (K.add K.one (K.neg (Frobenius.pw K Q (n + 1)))) u) := by lib_ring
      _ = _ := by rw [hy, hu]; lib_ring

theorem le_sq' (a : Nat) : a ≤ a * a := by
  cases a with
  | zero => exact Nat.le_refl 0
  | succ k => exact Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ (Nat.succ_le_succ (Nat.zero_le k)))

theorem ex_ge_l (N m : Nat) : N - m ≤ Ex N m := Nat.le_trans (le_sq' _) (Nat.le_add_left _ _)
theorem ex_ge_r (N m : Nat) : m - N ≤ Ex N m := Nat.le_trans (le_sq' _) (Nat.le_add_right _ _)

theorem es_scale (c : R) (f g : Nat → R) (z : R) : ∀ L, (∀ m, m < L → K.mul c (f m) = g m) → K.mul c (es K f z L) = es K g z L
  | 0, _ => by show K.mul c K.zero = K.zero; lib_ring
  | L + 1, h => by
    show K.mul c (K.add (es K f z L) (K.mul (f L) (Frobenius.pw K z L))) = K.add (es K g z L) (K.mul (g L) (Frobenius.pw K z L))
    rw [← es_scale c f g z L (fun m hm => h m (Nat.lt_succ_of_lt hm)), ← h L (Nat.lt_succ_self L)]; lib_ring

theorem pw_zero_of_le (x : R) (M E : Nat) (h : Frobenius.pw K x M = K.zero) (hE : M ≤ E) : Frobenius.pw K x E = K.zero := by
  obtain ⟨t, ht⟩ := Cheb.le_dest2 M E hE
  rw [← ht, Nat.add_comm]; exact pw_ge K x M h t

/-- **Gauss's identity mod q^M** (N = 2M, Q = q², P = (Q; Q)_M). -/
theorem gauss_mod (q : R) (M : Nat) (hq : Frobenius.pw K q M = K.zero) :
    es K (fun m => Frobenius.pw K q (Ex (2 * M) m)) (K.neg K.one) (2 * (2 * M) + 1) =
      K.mul (pp K (K.mul q q) M) (jf K q (K.neg K.one) (2 * M)) := by
  have hQ : Frobenius.pw K (K.mul q q) M = K.zero := by
    rw [pwQ]; exact pw_zero_of_le K q M _ hq (Nat.le_trans (Nat.le_add_left M M) (Nat.le_of_eq (by lib_nat)))
  have hst := pp_stable K (K.mul q q) M hQ
  generalize hP : pp K (K.mul q q) M = P at hst
  -- termwise: P·P·bj N m = P·q^(Ex N m)
  have hterm : ∀ m, m < 2 * (2 * M) + 1 → K.mul (K.mul P P) (bj K q (2 * M) m) = K.mul P (Frobenius.pw K q (Ex (2 * M) m)) := by
    intro m hm
    rw [fjtp]
    cases Nat.lt_or_ge m M with
    | inl h1 =>
      have hz : Frobenius.pw K q (Ex (2 * M) m) = K.zero := by
        apply pw_zero_of_le K q M _ hq
        apply Nat.le_trans _ (ex_ge_l (2 * M) m)
        obtain ⟨d, hd⟩ := Cheb.le_dest2 (m + 1) M h1
        rw [show 2 * M = m + (d + 1 + M) by rw [← hd]; lib_nat, sub_add_l]
        exact Nat.le_add_left M (d + 1)
      rw [hz]; lib_ring
    | inr h1 =>
      cases Nat.lt_or_ge (3 * M) m with
      | inl h2 =>
        have hz : Frobenius.pw K q (Ex (2 * M) m) = K.zero := by
          apply pw_zero_of_le K q M _ hq
          apply Nat.le_trans _ (ex_ge_r (2 * M) m)
          obtain ⟨d, hd⟩ := Cheb.le_dest2 (3 * M + 1) m h2
          rw [show m = 2 * M + (d + M + 1) by rw [← hd]; lib_nat, sub_add_l]
          exact Nat.le_trans (Nat.le_add_left M d) (Nat.le_succ _)
        rw [hz]; lib_ring
      | inr h2 =>
        have hm2 : m ≤ 2 * (2 * M) := Nat.le_trans h2 (Nat.le_trans (Nat.le_add_right (3 * M) M) (Nat.le_of_eq (by lib_nat)))
        have hg := gb_pp K (K.mul q q) (2 * (2 * M)) m hm2
        obtain ⟨a, ha⟩ := Cheb.le_dest2 M m h1
        obtain ⟨c, hc⟩ := Cheb.le_dest2 m (3 * M) h2
        have h4 : 2 * (2 * M) = m + (M + c) := by
          rw [show 2 * (2 * M) = (c + m) + M by rw [hc]; lib_nat]; lib_nat
        have hs : 2 * (2 * M) - m = M + c := by rw [h4, sub_add_l]
        have e1 : pp K (K.mul q q) m = P := by rw [← ha, Nat.add_comm, hst]
        have e2 : pp K (K.mul q q) (2 * (2 * M) - m) = P := by rw [hs, hst]
        have e3 : pp K (K.mul q q) (2 * (2 * M)) = P := by rw [show 2 * (2 * M) = M + 3 * M by lib_nat, hst]
        rw [e1, e2, e3] at hg
        generalize gb K (K.mul q q) (2 * (2 * M)) m = G at hg ⊢
        calc _ = K.mul (K.mul (K.mul G P) P) (Frobenius.pw K q (Ex (2 * M) m)) := by lib_ring
          _ = _ := by rw [hg]
  have hes := es_scale K (K.mul P P) (bj K q (2 * M)) _ (K.neg K.one) _ hterm
  have hes2 := es_scale K P (fun m => Frobenius.pw K q (Ex (2 * M) m)) _ (K.neg K.one) (2 * (2 * M) + 1) (fun m _ => rfl)
  rw [ev_jtp, ← hes2] at hes
  -- cancel P
  obtain ⟨y, hy⟩ := pp_unit K (K.mul q q) M hQ M
  rw [hP] at hy
  calc es K (fun m => Frobenius.pw K q (Ex (2 * M) m)) (K.neg K.one) (2 * (2 * M) + 1)
      = K.mul (K.mul P y) (es K (fun m => Frobenius.pw K q (Ex (2 * M) m)) (K.neg K.one) (2 * (2 * M) + 1)) := by rw [hy]; lib_ring
    _ = K.mul y (K.mul P (es K (fun m => Frobenius.pw K q (Ex (2 * M) m)) (K.neg K.one) (2 * (2 * M) + 1))) := by lib_ring
    _ = K.mul y (K.mul (K.mul P P) (jf K q (K.neg K.one) (2 * M))) := by rw [← hes]
    _ = K.mul (K.mul P y) (K.mul P (jf K q (K.neg K.one) (2 * M))) := by lib_ring
    _ = _ := by rw [hy]; lib_ring

end QBinom

#print axioms QBinom.pw_ge
#print axioms QBinom.pp_stable
#print axioms QBinom.unit_sub
#print axioms QBinom.pp_unit
#print axioms QBinom.le_sq'
#print axioms QBinom.ex_ge_l
#print axioms QBinom.ex_ge_r
#print axioms QBinom.es_scale
#print axioms QBinom.pw_zero_of_le
#print axioms QBinom.gauss_mod

lang/GaussNormN.lean

lean_all/lang/GaussNormN.lean

/-
  lang/GaussNormN — T49, cycle 603: **the norm of a Gauss sum of any order**, coefficientwise in E[C_p]:
  for multiplicative ψ : 𝔽_p → E with ψ(1) = 1, ψ(0) = 0, Σψ = 0, and ψ' = ψ ∘ ι (ι(x) = x^(p−2), the inverse),
    (G_ψ · G_ψ')(c) = ψ'(−1) · (p·δ_(c,0) − 1),   i.e.  G_ψ G_ψ̄ = ψ̄(−1)·(p ζ⁰ − N).
  (CharSumGen.conv_D, D_ne, D_zero; the cubic and quartic cases are JacobiNorm.gg and its quartic analogue.)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.CharSumGen

namespace CharSumGen

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

variable {S : Type} (E : CRing S) (j : Nat)

theorem gauss_norm (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (ψ : Zp (j + 1 + 1) → S)
    (hm : ∀ x y, ψ ((ring (j + 1 + 1)).mul x y) = E.mul (ψ x) (ψ y)) (h1 : ψ (ring (j + 1 + 1)).one = E.one)
    (h0 : ψ (ring (j + 1 + 1)).zero = E.zero) (hs : sumK E ψ (LL j) = E.zero) (c : Zp (j + 1 + 1)) :
    gt E (j + 1 + 1) (gmul E (j + 1 + 1) (Gv j ψ) (Gv j (fun x => ψ (iv j x)))) c =
      E.mul (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))) (gt E (j + 1 + 1) (Xv E j) c) := by
  have hm' : ∀ x y, (fun x => ψ (iv j x)) ((ring (j + 1 + 1)).mul x y) = E.mul ((fun x => ψ (iv j x)) x) ((fun x => ψ (iv j x)) y) :=
    fun x y => by show ψ (iv j ((ring (j + 1 + 1)).mul x y)) = _; rw [iv_mulmul, hm]
  rw [conv_D E j ψ _ hm' c, gt_X]
  by_cases hc : c = (ring (j + 1 + 1)).zero
  · rw [hc]
    have e : sumK E (fun b => E.mul (ψ ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).zero b)) (ψ (iv j b))) (LL j) =
        sumK E (fun b => E.mul (ψ b) (ψ (iv j b))) (LL j) := sumK_ext E _ _ (fun b => by rw [(ring (j + 1 + 1)).zero_add]) _
    show E.mul _ (sumK E (fun b => E.mul (ψ ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).zero b)) (ψ (iv j b))) (LL j)) = _
    rw [e, D_zero E j hpr ψ hm h1 h0]
    unfold delta; rw [ite_pos' _ _ rfl]; lib_ring
  · show E.mul _ (sumK E (fun b => E.mul (ψ ((ring (j + 1 + 1)).add c b)) (ψ (iv j b))) (LL j)) = _
    rw [D_ne E j hpr ψ hm h1 h0 hs c hc]
    unfold delta; rw [ite_neg' _ _ hc]; lib_ring

end CharSumGen

#print axioms CharSumGen.gauss_norm

lang/GaussPow.lean

lean_all/lang/GaussPow.lean

/-
  lang/GaussPow — T43, cycle 171: primary elements of norm p^N prime to p are π^N and π̄^N.

  π = a + bi primary, a² + b² = p prime. If α is primary, N(α) = p^N and p ∤ Re α, then Re α = Re π^N:
    gconj_mul, conj_pow     conjugation is multiplicative; π̄^n = conj(π^n)
    mixed                   π^(j+1) π̄^(l+1) = p · π^j π̄^l
    pow_primary             GaussFactor: α = u π^j π̄^l; j, l ≥ 1 give p | Re α; else u = 1 and Re α = Re π^N.

  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.GaussCube

namespace GaussPow

open FTAChain LibInt FermatUnique GaussInt GaussFactor GaussCube

theorem gconj_mul (x y : GI) : gconj (gmul x y) = gmul (gconj x) (gconj y) :=
  gi_ext _ _ (by show x.re * y.re - x.im * y.im = x.re * y.re - -x.im * -y.im; lib_int)
    (by show -(x.re * y.im + x.im * y.re) = x.re * -y.im + -x.im * y.re; lib_int)

theorem conj_pow (a b : Int) : ∀ n, gpow ⟨a, -b⟩ n = gconj (gpow ⟨a, b⟩ n)
  | 0 => gi_ext _ _ rfl (by show (0 : Int) = -0; rfl)
  | n + 1 => by
    show gmul (gpow ⟨a, -b⟩ n) ⟨a, -b⟩ = gconj (gmul (gpow ⟨a, b⟩ n) ⟨a, b⟩)
    rw [gconj_mul, conj_pow a b n]; rfl

def real (P : Int) : GI := ⟨P, 0⟩

theorem real_re (P : Int) (z : GI) : (gmul (real P) z).re = P * z.re := by
  show P * z.re - 0 * z.im = P * z.re; lib_int

theorem real_comm_u (P : Int) (u z : GI) : gmul u (gmul (real P) z) = gmul (real P) (gmul u z) := by
  rw [← gmul_assoc, gmul_comm u, gmul_assoc]

theorem mixed (a b : Int) (j l : Nat) :
    gmul (gpow ⟨a, b⟩ (j + 1)) (gpow ⟨a, -b⟩ (l + 1)) = gmul (real (a * a + b * b)) (gmul (gpow ⟨a, b⟩ j) (gpow ⟨a, -b⟩ l)) := by
  have hpp : gmul ⟨a, b⟩ ⟨a, -b⟩ = real (a * a + b * b) :=
    gi_ext _ _ (by show a * a - b * -b = a * a + b * b; lib_int) (by show a * -b + b * a = 0; lib_int)
  show gmul (gmul (gpow ⟨a, b⟩ j) ⟨a, b⟩) (gmul (gpow ⟨a, -b⟩ l) ⟨a, -b⟩) = _
  rw [gmul_assoc, ← gmul_assoc ⟨a, b⟩, gmul_comm ⟨a, b⟩ (gpow ⟨a, -b⟩ l), gmul_assoc, hpp,
    ← gmul_assoc, gmul_comm _ (real _)]

/-- **Primary α of norm p^N with p ∤ Re α has Re α = Re π^N.** -/
theorem pow_primary (p : Nat) (hp : ∀ d, Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (a b : Int)
    (hab : a * a + b * b = Int.ofNat (p + 1 + 1)) (hπ : Primary ⟨a, b⟩) (N : Nat) (x : GI) (hx : Primary x)
    (hN : gnorm x = ipow (Int.ofNat (p + 1 + 1)) N) (hA : ¬ ∃ w, x.re = Int.ofNat (p + 1 + 1) * w) :
    x.re = (gpow ⟨a, b⟩ N).re := by
  match factor p hp a b hab N x hN with
  | ⟨u, j, l, hu, hjl, hx'⟩ =>
    match j, l with
    | j + 1, l + 1 =>
      exfalso; apply hA
      refine ⟨(gmul u (gmul (gpow ⟨a, b⟩ j) (gpow ⟨a, -b⟩ l))).re, ?_⟩
      rw [hx', mixed, real_comm_u, real_re, hab]
    | 0, l =>
      have hl : l = N := (Nat.zero_add l).symm.trans hjl
      subst hl
      have hc : Primary (gmul (gpow ⟨a, b⟩ 0) (gpow ⟨a, -b⟩ l)) :=
        prim_mul _ _ prim_one (prim_pow _ (prim_conj ⟨a, b⟩ hπ) l)
      have h1 := unit_primary u _ hu hc (by rw [← hx']; exact hx)
      rw [hx', h1, gone_mul, show gpow ⟨a, b⟩ 0 = gone from rfl, gone_mul, conj_pow]
      rfl
    | j + 1, 0 =>
      have hl : j + 1 = N := hjl
      subst hl
      have hc : Primary (gmul (gpow ⟨a, b⟩ (j + 1)) (gpow ⟨a, -b⟩ 0)) := prim_mul _ _ (prim_pow _ hπ _) prim_one
      have h1 := unit_primary u _ hu hc (by rw [← hx']; exact hx)
      rw [hx', h1, gone_mul, show gpow ⟨a, -b⟩ 0 = gone from rfl, gmul_comm, gone_mul]

end GaussPow

#print axioms GaussPow.gconj_mul
#print axioms GaussPow.conj_pow
#print axioms GaussPow.real
#print axioms GaussPow.real_re
#print axioms GaussPow.real_comm_u
#print axioms GaussPow.mixed
#print axioms GaussPow.pow_primary

lang/GaussPowN.lean

lean_all/lang/GaussPowN.lean

/-
  lang/GaussPowN — T49, cycle 605: **powers of a Gauss sum through Jacobi sums**, for a character ψ of any order:
  if Σ_a ψ(a)^i = 0 for 2 ≤ i ≤ k + 1 (ψ^2, …, ψ^(k+1) nontrivial), then in E[C_p]
    G_ψ^(k+1) = Jpr_k · G_(ψ^(k+1)),   Jpr_k = ∏_{i=1}^{k} J(ψ^i, ψ)
  (coefficientwise: (G_ψ^(k+1))(c) = Jpr_k ψ(c)^(k+1)).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.JacobiGauss

namespace CharSumGen

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

variable {S : Type} (E : CRing S) (j : Nat)

def psp (ψ : Zp (j + 1 + 1) → S) (k : Nat) : Zp (j + 1 + 1) → S := fun x => pw E (ψ x) k

def jpr (ψ : Zp (j + 1 + 1) → S) : Nat → S
  | 0 => E.one
  | k + 1 => E.mul (jpr ψ k) (J2 E j (psp E j ψ (k + 1)) ψ)

theorem gpow (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (ψ : Zp (j + 1 + 1) → S)
    (hm : ∀ x y, ψ ((ring (j + 1 + 1)).mul x y) = E.mul (ψ x) (ψ y)) (h0 : ψ (ring (j + 1 + 1)).zero = E.zero) :
    ∀ k, (∀ i, i < k → sumK E (fun a => pw E (ψ a) (i + 2)) (LL j) = E.zero) →
      pw (gring E (j + 1 + 1)) (Gv j ψ) (k + 1) = Gv j (fun x => E.mul (jpr E j ψ k) (pw E (ψ x) (k + 1)))
  | 0, _ => ext E (j + 1 + 1) _ _ (fun c => by
      show gt E (j + 1 + 1) (gmul E (j + 1 + 1) (gone E (j + 1 + 1)) (Gv j ψ)) c = _
      rw [gt_mul]
      unfold Gv gone; rw [gt_tb]
      have e : sumK E (fun a => E.mul (gt E (j + 1 + 1) (tb (j + 1 + 1) (fun c => delta E c (ring (j + 1 + 1)).zero)) a)
          (gt E (j + 1 + 1) (tb (j + 1 + 1) ψ) ((ring (j + 1 + 1)).add c ((ring (j + 1 + 1)).neg a)))) (L (j + 1 + 1)) =
          sumK E (fun a => E.mul (delta E a (ring (j + 1 + 1)).zero) ((fun a => ψ ((ring (j + 1 + 1)).add c ((ring (j + 1 + 1)).neg a))) a)) (L (j + 1 + 1)) :=
        sumK_ext E _ _ (fun a => by rw [gt_tb, gt_tb]) _
      rw [e, delta_sum, show sumK E (fun y => delta E y (ring (j + 1 + 1)).zero) (L (j + 1 + 1)) = E.one from CMInert.complete_allZ (j + 1 + 1) E _]
      show E.mul E.one (ψ ((ring (j + 1 + 1)).add c ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).zero))) = E.mul E.one (E.mul E.one (ψ c))
      rw [show (ring (j + 1 + 1)).add c ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).zero) = c by lib_ring]; lib_ring)
  | k + 1, hnt => by
    show gmul E (j + 1 + 1) (pw (gring E (j + 1 + 1)) (Gv j ψ) (k + 1)) (Gv j ψ) = _
    rw [gpow hpr ψ hm h0 k (fun i hi => hnt i (Nat.lt_succ_of_lt hi))]
    apply ext; intro c
    have hjg := jacobi_gauss E j hpr (psp E j ψ (k + 1)) ψ (fun x y => by unfold psp; rw [hm]; exact Frobenius.pw_mul E _ _ _) hm
      (by show pw E (ψ (ring (j + 1 + 1)).zero) (k + 1) = E.zero; rw [h0]; exact GroupRingFrob.pw_zero_succ' E k)
      (by have := hnt k (Nat.lt_succ_self k)
          rw [show (k + 2) = (k + 1) + 1 from rfl] at this
          exact (sumK_ext E _ _ (fun a => rfl) _).trans this) c
    rw [gt_mul] at hjg ⊢
    unfold Gv at hjg ⊢
    rw [gt_tb]
    have e : ∀ a, E.mul (gt E (j + 1 + 1) (tb (j + 1 + 1) (fun x => E.mul (jpr E j ψ k) (pw E (ψ x) (k + 1)))) a)
        (gt E (j + 1 + 1) (tb (j + 1 + 1) ψ) ((ring (j + 1 + 1)).add c ((ring (j + 1 + 1)).neg a))) =
        E.mul (jpr E j ψ k) (E.mul (gt E (j + 1 + 1) (tb (j + 1 + 1) (psp E j ψ (k + 1))) a)
          (gt E (j + 1 + 1) (tb (j + 1 + 1) ψ) ((ring (j + 1 + 1)).add c ((ring (j + 1 + 1)).neg a)))) := fun a => by
      rw [gt_tb, gt_tb, gt_tb]; unfold psp; lib_ring
    rw [sumK_ext E _ _ e, ← mul_sumK, hjg]
    show _ = E.mul (E.mul (jpr E j ψ k) (J2 E j (psp E j ψ (k + 1)) ψ)) (E.mul (pw E (ψ c) (k + 1)) (ψ c))
    unfold psp; lib_ring

end CharSumGen

#print axioms CharSumGen.gpow

lang/GaussQR.lean

lean_all/lang/GaussQR.lean

/-
  lang/GaussQR — G1 (T43): quadratic reciprocity from the Gauss sum.

  p = 2m + 3, q = 2r + 3 distinct odd primes, p* = (−1/p)·p, g = Σ χ_p(a) ζ^a ∈ 𝔽_q[C_p]. In 𝔽_q[C_p]:
    g ⋆ g = p*·ζ⁰ − (−1/p)·N   (GaussSum.gauss_sq, cast to 𝔽_q),   g ⋆ N = 0   (Σ χ_p = 0)
  so g^q = (g ⋆ g)^((q−1)/2) ⋆ g = (p*)^((q−1)/2) g, while g^q = (q/p) g (GaussFrob.gauss_frob). The ζ¹ coefficient of g is 1, so
  (q/p) ≡ (p*)^((q−1)/2) ≡ (p*/q) (mod q) (Euler), and both are ±1:
      qr_gauss     (q/p) = (p*/q)
  This is the law of quadratic reciprocity, proved through ℚ(√p*) ⊂ ℚ(ζ_p): the Frobenius of q acts on √p* = g as ζ ↦ ζ^q.
    sq_coef, gB_coef, sq_split, pow_kill, pw_odd, nsm_zero', mono0_mul, castZ_one', cast_chi_q, chi_pm, qr_gauss
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.GaussFrob

namespace GaussQR

open FTAChain ZMod YMChain FiniteRecChain DetChain GroupRing GroupRingFrob Frobenius QChar ConicSum GaussSum GaussFrob
open LibRing (castZ castN)

variable (m r : Nat)

/-- p* as an element of 𝔽_q. -/
def P : Zp (2 * r + 1 + 1) := castZ (ring (2 * r + 1 + 1)) (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * Int.ofNat (2 * m + 1 + 1 + 1))
/-- (−1/p) in 𝔽_q. -/
def E : Zp (2 * r + 1 + 1) := castZ (ring (2 * r + 1 + 1)) (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))
/-- (−1/p)·N. -/
def Bv : V (Zp (2 * r + 1 + 1)) ((2 * m + 1 + 1) + 1) := tb (2 * m + 1 + 1) (fun _ => E m r)
/-- p*·ζ⁰. -/
def Av : V (Zp (2 * r + 1 + 1)) ((2 * m + 1 + 1) + 1) := mono (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (ring (2 * m + 1 + 1)).zero (P m r)

theorem castZ_one' {S : Type} (Kr : CRing S) : castZ Kr 1 = Kr.one := add_zero' Kr Kr.one

theorem gq_coef (a : Zp (2 * m + 1 + 1)) : gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (gq m r) a = castZ (ring (2 * r + 1 + 1)) (chi m a) := by unfold gq; rw [gt_tb]

theorem sq_coef (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (c : Zp (2 * m + 1 + 1)) :
    gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (gmul (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (gq m r) (gq m r)) c = (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).mul (P m r) (delta (ring (2 * r + 1 + 1)) c (ring (2 * m + 1 + 1)).zero)) ((ring (2 * r + 1 + 1)).neg (E m r)) := by
  rw [gt_mul]
  have e : sumK (ring (2 * r + 1 + 1)) (fun a => (ring (2 * r + 1 + 1)).mul (gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (gq m r) a) (gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (gq m r) ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a)))) (L (2 * m + 1 + 1))
      = castZ (ring (2 * r + 1 + 1)) (conv m (chi m) (chi m) c) := by
    rw [show conv m (chi m) (chi m) c = sumK LibInt.intR (fun a => chi m a * chi m ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a))) (ConicSum.L m) from rfl,
      IntCast.castZ_sum]
    exact sumK_ext (ring (2 * r + 1 + 1)) _ _ (fun a => by rw [gq_coef, gq_coef, HasseDavenport.castZ_mul]) _
  rw [e]
  by_cases hc : c = (ring (2 * m + 1 + 1)).zero
  · rw [hc, gauss_zero m hpr]
    unfold delta; rw [ite_pos' _ _ rfl]
    unfold P E
    rw [show chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * (Int.ofNat (2 * m + 1 + 1 + 1) - 1) = chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * Int.ofNat (2 * m + 1 + 1 + 1) + -chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) by lib_int,
      IntCast.castZ_add, HasseDavenport.castZ_neg]
    lib_ring
  · rw [gauss_ne m hpr c hc]
    unfold delta; rw [ite_neg' _ _ hc]
    unfold E
    rw [HasseDavenport.castZ_neg]
    lib_ring

theorem gB_coef (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (c : Zp (2 * m + 1 + 1)) : gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (gmul (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (gq m r) (Bv m r)) c = (ring (2 * r + 1 + 1)).zero := by
  rw [gt_mul]
  have e : sumK (ring (2 * r + 1 + 1)) (fun a => (ring (2 * r + 1 + 1)).mul (gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (gq m r) a) (gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (Bv m r) ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a)))) (L (2 * m + 1 + 1))
      = (ring (2 * r + 1 + 1)).mul (castZ (ring (2 * r + 1 + 1)) (sumK LibInt.intR (chi m) (ConicSum.L m))) (E m r) := by
    rw [IntCast.castZ_sum, sumK_mul]
    exact sumK_ext (ring (2 * r + 1 + 1)) _ _ (fun a => by unfold Bv; rw [gq_coef, gt_tb]) _
  rw [e, show sumK LibInt.intR (chi m) (ConicSum.L m) = 0 from chi_sum m hpr]
  show (ring (2 * r + 1 + 1)).mul (ring (2 * r + 1 + 1)).zero (E m r) = (ring (2 * r + 1 + 1)).zero
  exact zero_mul' (ring (2 * r + 1 + 1)) _

theorem sq_split (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : gmul (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (gq m r) (gq m r) = gadd (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (Av m r) (gneg (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (Bv m r)) :=
  ext (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) _ _ (fun c => by
    rw [sq_coef m r hpr]
    unfold gadd gneg Av Bv
    rw [gt_tb, gt_mono, gt_tb, gt_tb])

theorem pow_kill {S : Type} (G : CRing S) (A B g : S) (h : G.mul B g = G.zero) : ∀ k,
    G.mul (pw G (G.add A (G.neg B)) k) g = G.mul (pw G A k) g
  | 0 => rfl
  | k + 1 => by
    show G.mul (G.mul (pw G (G.add A (G.neg B)) k) (G.add A (G.neg B))) g = G.mul (G.mul (pw G A k) A) g
    have e1 : G.mul (G.mul (pw G (G.add A (G.neg B)) k) (G.add A (G.neg B))) g
        = G.add (G.mul A (G.mul (pw G (G.add A (G.neg B)) k) g)) (G.neg (G.mul (pw G (G.add A (G.neg B)) k) (G.mul B g))) := by lib_ring
    rw [e1, h, pow_kill G A B g h k]
    lib_ring

theorem pw_odd {S : Type} (G : CRing S) (x : S) : ∀ k, pw G x (2 * k + 1) = G.mul (pw G (G.mul x x) k) x
  | 0 => rfl
  | k + 1 => by
    show G.mul (G.mul (pw G x (2 * k + 1)) x) x = G.mul (G.mul (pw G (G.mul x x) k) (G.mul x x)) x
    rw [pw_odd G x k]; lib_ring

theorem nsm_zero' {S : Type} (G : CRing S) : ∀ k, nsm G k G.zero = G.zero
  | 0 => rfl
  | k + 1 => by show G.add (nsm G k G.zero) G.zero = G.zero; rw [nsm_zero' G k]; exact add_zero' G _

theorem mono0_mul (s : Zp (2 * r + 1 + 1)) (u : V (Zp (2 * r + 1 + 1)) ((2 * m + 1 + 1) + 1)) (c : Zp (2 * m + 1 + 1)) :
    gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (gmul (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (mono (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (ring (2 * m + 1 + 1)).zero s) u) c = (ring (2 * r + 1 + 1)).mul s (gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) u c) := by
  rw [gt_mul]
  have e : sumK (ring (2 * r + 1 + 1)) (fun a => (ring (2 * r + 1 + 1)).mul (gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (mono (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (ring (2 * m + 1 + 1)).zero s) a) (gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) u ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a)))) (L (2 * m + 1 + 1))
      = sumK (ring (2 * r + 1 + 1)) (fun a => (ring (2 * r + 1 + 1)).mul (delta (ring (2 * r + 1 + 1)) a (ring (2 * m + 1 + 1)).zero) ((fun b => (ring (2 * r + 1 + 1)).mul s (gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) u ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg b)))) a)) (L (2 * m + 1 + 1)) :=
    sumK_ext (ring (2 * r + 1 + 1)) _ _ (fun a => by
      rw [gt_mono]
      show (ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1 + 1)).mul s (delta (ring (2 * r + 1 + 1)) a (ring (2 * m + 1 + 1)).zero)) (gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) u ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a))) = (ring (2 * r + 1 + 1)).mul (delta (ring (2 * r + 1 + 1)) a (ring (2 * m + 1 + 1)).zero) ((ring (2 * r + 1 + 1)).mul s (gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1)  -- (+66 chars; full line in the Zenodo source)
      lib_ring) _
  rw [e]
  refine (pick (ring (2 * r + 1 + 1)) _ (CMInert.complete_allZ (2 * m + 1 + 1) (ring (2 * r + 1 + 1))) _ (ring (2 * m + 1 + 1)).zero).trans ?_
  show (ring (2 * r + 1 + 1)).mul s (gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) u ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).zero))) = _
  rw [show (ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).zero) = c by lib_ring]

theorem cast_chi_q (hq : ∀ d, Dv d (2 * r + 1 + 1 + 1) → d = 1 ∨ d = 2 * r + 1 + 1 + 1) (x : Zp (2 * r + 1 + 1)) (hx : x ≠ (ring (2 * r + 1 + 1)).zero) : castZ (ring (2 * r + 1 + 1)) (chi r x) = pw (ring (2 * r + 1 + 1)) x (r + 1) := by
  cases Euler.euler_pm r hq x hx with
  | inl h => rw [chi_one_of r x hx h, h]; exact castZ_one' (ring (2 * r + 1 + 1))
  | inr h =>
    rw [chi_neg_of r x hx (by rw [h]; exact ne_pm r), h]
    show (ring (2 * r + 1 + 1)).neg (castN (ring (2 * r + 1 + 1)) 1) = _
    rw [show castN (ring (2 * r + 1 + 1)) 1 = (ring (2 * r + 1 + 1)).one from add_zero' (ring (2 * r + 1 + 1)) (ring (2 * r + 1 + 1)).one]

theorem chi_pm (n : Nat) (x : Zp (2 * n + 1 + 1)) (hx : x ≠ (ring (2 * n + 1 + 1)).zero) : chi n x = 1 ∨ chi n x = -1 := by
  by_cases h : pw (ring (2 * n + 1 + 1)) x (n + 1) = (ring (2 * n + 1 + 1)).one
  · exact Or.inl (chi_one_of n x hx h)
  · exact Or.inr (chi_neg_of n x hx h)

/-- **Quadratic reciprocity**: (q/p) = (p*/q), p* = (−1/p)·p, through the Gauss sum √p* ∈ ℤ[ζ_p]. -/
theorem qr_gauss (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (hq : ∀ d, Dv d (2 * r + 1 + 1 + 1) → d = 1 ∨ d = 2 * r + 1 + 1 + 1) (hQ : nsm (ring (2 * m + 1 + 1)) (2 * r + 1 + 1 + 1) (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero) (hP : P m r ≠ (ring (2 * r + 1 + 1)).zero) :
    chi m (nsm (ring (2 * m + 1 + 1)) (2 * r + 1 + 1 + 1) (ring (2 * m + 1 + 1)).one) = chi r (P m r) := by
  have hB : (gring (ring (2 * r + 1 + 1)) (2 * m + 1 + 1)).mul (Bv m r) (gq m r) = (gring (ring (2 * r + 1 + 1)) (2 * m + 1 + 1)).zero :=
    ext (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) _ _ (fun c => by
      show gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (gmul (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (Bv m r) (gq m r)) c = gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (tb (2 * m + 1 + 1) (fun _ => (ring (2 * r + 1 + 1)).zero)) c
      rw [gm_comm, gB_coef m r hpr, gt_tb])
  have key : gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (pw (gring (ring (2 * r + 1 + 1)) (2 * m + 1 + 1)) (gq m r) (2 * r + 1 + 1 + 1)) (ring (2 * m + 1 + 1)).one = (ring (2 * r + 1 + 1)).mul (pw (ring (2 * r + 1 + 1)) (P m r) (r + 1)) (ring (2 * r + 1 + 1)).one := by
    rw [show 2 * r + 1 + 1 + 1 = 2 * (r + 1) + 1 by lib_nat, pw_odd (gring (ring (2 * r + 1 + 1)) (2 * m + 1 + 1))]
    show gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) ((gring (ring (2 * r + 1 + 1)) (2 * m + 1 + 1)).mul (pw (gring (ring (2 * r + 1 + 1)) (2 * m + 1 + 1)) (gmul (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (gq m r) (gq m r)) (r + 1)) (gq m r)) (ring (2 * m + 1 + 1)).one = _
    rw [sq_split m r hpr]
    show gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) ((gring (ring (2 * r + 1 + 1)) (2 * m + 1 + 1)).mul (pw (gring (ring (2 * r + 1 + 1)) (2 * m + 1 + 1)) ((gring (ring (2 * r + 1 + 1)) (2 * m + 1 + 1)).add (Av m r) ((gring (ring (2 * r + 1 + 1)) (2 * m + 1 + 1)).neg (Bv m r))) (r + 1)) (gq m r)) (ring (2 * m + 1 + 1)).one = _
    rw [pow_kill (gring (ring (2 * r + 1 + 1)) (2 * m + 1 + 1)) _ _ _ hB]
    unfold Av
    rw [mono_pow, nsm_zero']
    show gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (gmul (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (mono (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (ring (2 * m + 1 + 1)).zero (pw (ring (2 * r + 1 + 1)) (P m r) (r + 1))) (gq m r)) (ring (2 * m + 1 + 1)).one = _
    rw [mono0_mul, gq_coef, chi_one m, castZ_one']
  rw [gauss_frob m r hpr hq hQ, gq_coef, chi_one m, castZ_one', (ring (2 * r + 1 + 1)).mul_comm, (ring (2 * r + 1 + 1)).one_mul, (ring (2 * r + 1 + 1)).mul_comm, (ring (2 * r + 1 + 1)).one_mul,
    ← cast_chi_q r hq _ hP] at key
  cases chi_pm m _ (fun e => hQ e) with
  | inl h1 =>
    cases chi_pm r _ hP with
    | inl h2 => rw [h1, h2]
    | inr h2 =>
      rw [h1, h2, castZ_one'] at key
      exact absurd key (fun e => GaussSplit.one_ne_negone (2 * r) (e.trans (by show (ring (2 * r + 1 + 1)).neg (castN (ring (2 * r + 1 + 1)) 1) = _; rw [show castN (ring (2 * r + 1 + 1)) 1 = (ring (2 * r + 1 + 1)).one from add_zero' (ring (2 * r + 1 + 1)) (ring (2 * r + 1 + 1)).one])))
  | inr h1 =>
    cases chi_pm r _ hP with
    | inr h2 => rw [h1, h2]
    | inl h2 =>
      rw [h1, h2, castZ_one'] at key
      exact absurd key.symm (fun e => GaussSplit.one_ne_negone (2 * r) (e.trans (by show (ring (2 * r + 1 + 1)).neg (castN (ring (2 * r + 1 + 1)) 1) = _; rw [show castN (ring (2 * r + 1 + 1)) 1 = (ring (2 * r + 1 + 1)).one from add_zero' (ring (2 * r + 1 + 1)) (ring (2 * r + 1 + 1)).one])))

end GaussQR

#print axioms GaussQR.castZ_one'
#print axioms GaussQR.gq_coef
#print axioms GaussQR.sq_coef
#print axioms GaussQR.gB_coef
#print axioms GaussQR.sq_split
#print axioms GaussQR.pow_kill
#print axioms GaussQR.pw_odd
#print axioms GaussQR.nsm_zero'
#print axioms GaussQR.mono0_mul
#print axioms GaussQR.cast_chi_q
#print axioms GaussQR.chi_pm
#print axioms GaussQR.qr_gauss

lang/GaussSplit.lean

lean_all/lang/GaussSplit.lean

/-
  lang/GaussSplit — T43, cycle 23 (GL1 for ℚ(i)): the splitting law of ℤ[i].

  For every odd prime p, −1 is a square in ℤ/p exactly when p ≡ 1 (mod 4):
    two_ne_zero          1 + 1 ≠ 0 in ℤ/p (p odd)
    one_ne_negone        1 ≠ −1 in ℤ/p
    split_one_mod_four   p = 4k + 5  ⇒  ∃ y, y² = −1          (Euler's criterion, EulerHard.euler_hard)
    inert_three_mod_four p = 4k + 3  ⇒  ∀ y, y² ≠ −1          (y^(p−1) = (y²)^(2k+1) = −1 ≠ 1, ZMod.unit_pow)
  So the Artin symbol of p in ℚ(i)/ℚ (split ⇔ x² + 1 has a root mod p) is the Dirichlet character χ₋₄(p) of
  conductor 4: GL1 reciprocity for ℚ(i), the Hecke-character input of the CM form of y² = x³ − x (lang/CMInert).
  The case p = 4k + 1 is written p = 4k + 5 together with p = 5 at k = 0.

  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.EulerHard
import lang.TwoPower

namespace GaussSplit

open FTAChain ZMod

/-- `1 + 1 ≠ 0` in ℤ/(j + 2), j ≥ 1. -/
theorem two_ne_zero (j : Nat) :
    (ring (j + 1 + 1)).add (ring (j + 1 + 1)).one (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero := fun h => by
  have hv := congrArg Subtype.val h
  have h2 : ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one (ring (j + 1 + 1)).one).val = 2 := by
    show md (j + 1 + 1) (md (j + 1 + 1) 1 + md (j + 1 + 1) 1) = 2
    rw [md_small _ 1 (Nat.succ_lt_succ (Nat.zero_lt_succ _))]
    exact md_small _ 2 (Nat.succ_lt_succ (Nat.succ_lt_succ (Nat.zero_lt_succ _)))
  rw [h2] at hv
  exact Nat.noConfusion hv

theorem one_ne_negone (j : Nat) : (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one := fun h => by
  apply two_ne_zero j
  have := congrArg (fun z => (ring (j + 1 + 1)).add z (ring (j + 1 + 1)).one) h
  exact this.trans (DetChain.add_neg' (ring (j + 1 + 1)) _)

/-- **Split primes**: p = 4k + 5 ⇒ −1 is a square mod p. -/
theorem split_one_mod_four (k : Nat)
    (hpr : ∀ d, Dv d (2 * (2 * k + 1) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * k + 1) + 1 + 1 + 1) :
    ∃ y : Zp (2 * (2 * k + 1) + 1 + 1),
      (ring (2 * (2 * k + 1) + 1 + 1)).mul y y = (ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one := by
  apply EulerHard.euler_hard (2 * k + 1) hpr
  show Frobenius.pw (ring (2 * (2 * k + 1) + 1 + 1)) _ (2 * (k + 1)) = _
  rw [← TwoPower.pwpow]
  have e2 : Frobenius.pw (ring (2 * (2 * k + 1) + 1 + 1)) ((ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one) 2
      = (ring (2 * (2 * k + 1) + 1 + 1)).one := by
    show (ring _).mul ((ring _).mul (ring _).one _) _ = _
    rw [(ring _).one_mul]
    exact TwoPower.negone_sq' (2 * (2 * k + 1) + 1)
  rw [e2, TwoPower.pwone]

/-- **Inert primes**: p = 4k + 3 ⇒ −1 is not a square mod p. -/
theorem inert_three_mod_four (k : Nat)
    (hpr : ∀ d, Dv d (2 * (2 * k) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * k) + 1 + 1 + 1)
    (y : Zp (2 * (2 * k) + 1 + 1)) :
    (ring (2 * (2 * k) + 1 + 1)).mul y y ≠ (ring (2 * (2 * k) + 1 + 1)).neg (ring (2 * (2 * k) + 1 + 1)).one := fun h => by
  have hy : y ≠ (ring (2 * (2 * k) + 1 + 1)).zero := fun e => by
    rw [e, DetChain.mul_zero] at h
    have := congrArg (fun z => (ring (2 * (2 * k) + 1 + 1)).add z (ring (2 * (2 * k) + 1 + 1)).one) h
    have h1 : (ring (2 * (2 * k) + 1 + 1)).one = (ring (2 * (2 * k) + 1 + 1)).zero :=
      ((ring _).zero_add _).symm.trans (this.trans (DetChain.add_neg' (ring _) _))
    exact one_ne_negone (2 * (2 * k)) (h1.trans h)
  have hu := unit_pow (2 * (2 * k) + 1) hpr y hy
  have e : Frobenius.pw (ring (2 * (2 * k) + 1 + 1)) y (2 * (2 * k) + 1 + 1)
      = Frobenius.pw (ring (2 * (2 * k) + 1 + 1)) (Frobenius.pw (ring (2 * (2 * k) + 1 + 1)) y 2) (2 * k + 1) :=
    (TwoPower.pwpow _ y 2 (2 * k + 1)).symm
  have e2 : Frobenius.pw (ring (2 * (2 * k) + 1 + 1)) y 2 = (ring (2 * (2 * k) + 1 + 1)).mul y y := by
    show (ring _).mul ((ring _).mul (ring _).one y) y = _
    rw [(ring _).one_mul]
  rw [e, e2, h, TwoPower.negone_odd' (2 * (2 * k) + 1) k] at hu
  exact one_ne_negone (2 * (2 * k)) hu.symm

end GaussSplit

#print axioms GaussSplit.two_ne_zero
#print axioms GaussSplit.one_ne_negone
#print axioms GaussSplit.split_one_mod_four
#print axioms GaussSplit.inert_three_mod_four

lang/GaussSum.lean

lean_all/lang/GaussSum.lean

/-
  lang/GaussSum — G1 (T43): the quadratic Gauss sum over ℤ[ζ_p], algebraically.

  p = 2m + 3 prime, χ the quadratic character of 𝔽_p (QChar). The group ring ℤ[C_p] is the ring of functions 𝔽_p → ℤ with the
  convolution (f ⋆ h)(c) = Σ_a f(a) h(c − a); ζ^a is the point mass at a, and ℤ[ζ_p] = ℤ[C_p]/(N) with N = Σ_a ζ^a (the constant
  function 1). The Gauss sum is g = Σ_a χ(a) ζ^a, i.e. the function χ itself.
    term          χ(a) χ(c − a) = χ(−1) χ((a + 0)(a − c))
    gauss_ne      c ≠ 0:  (g ⋆ g)(c) = −χ(−1)                    (ConicSum.conic_sum)
    gauss_zero    (g ⋆ g)(0) = χ(−1)(p − 1)                      (χ(a²) = 1 for a ≠ 0)
    gauss_sq      g ⋆ g = χ(−1)·(p·ζ⁰ − N)                       in ℤ[C_p]
    gauss_N       g ⋆ N = 0                                       (QChar.chi_sum)
    gauss_star    g ⋆ g + χ(−1)·N = p*·ζ⁰ with p* = χ(−1)·p:  g² = p* in ℤ[ζ_p] = ℤ[C_p]/(N)
  So √p* = g ∈ ℤ[ζ_p]: the quadratic field ℚ(√p*) lies in the cyclotomic field ℚ(ζ_p) (Kronecker–Weber for the quadratic
  character of conductor p).

  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.ConicSum

namespace GaussSum

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum

variable (m : Nat)

/-- convolution in ℤ[C_p]. -/
def conv (f h : Zp (2 * m + 1 + 1) → Int) (c : Zp (2 * m + 1 + 1)) : Int :=
  sumK LibInt.intR (fun a => f a * h ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a))) (L m)

/-- the norm element N = Σ_a ζ^a. -/
def N : Zp (2 * m + 1 + 1) → Int := fun _ => 1

/-- ζ⁰ = 1. -/
def d0 (c : Zp (2 * m + 1 + 1)) : Int := delta LibInt.intR c (ring (2 * m + 1 + 1)).zero

theorem sum_one : ∀ k, sumK LibInt.intR (fun _ : Zp (2 * m + 1 + 1) => (1 : Int)) (CMInert.allZ (2 * m + 1 + 1) k) = Int.ofNat k
  | 0 => rfl
  | k + 1 => by
    show sumK LibInt.intR (fun _ : Zp (2 * m + 1 + 1) => (1 : Int)) (CMInert.allZ (2 * m + 1 + 1) k ++ [mk (2 * m + 1 + 1) k]) = _
    rw [sumK_append, sum_one k]
    show Int.ofNat k + (1 + 0) = Int.ofNat (k + 1)
    rfl

theorem term (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (c a : Zp (2 * m + 1 + 1)) :
    chi m a * chi m ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a)) = chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add a (ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg c))) := by
  rw [← chi_mul m hpr, ← chi_mul m hpr]
  congr 1
  lib_ring

theorem neg_ne (c : Zp (2 * m + 1 + 1)) (hc : c ≠ (ring (2 * m + 1 + 1)).zero) : (ring (2 * m + 1 + 1)).zero ≠ (ring (2 * m + 1 + 1)).neg c := fun e => by
  apply hc
  calc c = (ring (2 * m + 1 + 1)).add c (ring (2 * m + 1 + 1)).zero := (DetChain.add_zero' (ring (2 * m + 1 + 1)) c).symm
    _ = (ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg c) := by rw [e]
    _ = (ring (2 * m + 1 + 1)).zero := (ring (2 * m + 1 + 1)).add_neg c

/-- **c ≠ 0: (g ⋆ g)(c) = −χ(−1).** -/
theorem gauss_ne (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (c : Zp (2 * m + 1 + 1)) (hc : c ≠ (ring (2 * m + 1 + 1)).zero) :
    conv m (chi m) (chi m) c = - chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by
  show sumK LibInt.intR (fun a => chi m a * chi m ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a))) (L m) = _
  rw [sumK_ext LibInt.intR _ _ (term m hpr c)]
  refine (mul_sumK LibInt.intR (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) _ (L m)).symm.trans ?_
  rw [conic_sum m hpr _ _ (neg_ne m c hc)]
  show chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * -1 = _
  lib_int

/-- **(g ⋆ g)(0) = χ(−1)(p − 1).** -/
theorem gauss_zero (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : conv m (chi m) (chi m) (ring (2 * m + 1 + 1)).zero = chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * (Int.ofNat (2 * m + 1 + 1 + 1) - 1) := by
  have pt : ∀ a, chi m a * chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).neg a)) = chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * (1 + -1 * d0 m a) := fun a => by
    rw [show (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).neg a) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) a by lib_ring, chi_mul m hpr]
    by_cases ha : a = (ring (2 * m + 1 + 1)).zero
    · rw [ha, chi_zero]
      show (0 : Int) * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * 0) = chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * (1 + -1 * (if (ring (2 * m + 1 + 1)).zero = (ring (2 * m + 1 + 1)).zero then 1 else 0))
      rw [ite_pos' _ _ rfl]; lib_int
    · have h1 : chi m a * chi m a = 1 := by rw [← chi_mul m hpr]; exact chi_sq m hpr a ha
      show chi m a * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * chi m a) = chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * (1 + -1 * (if a = (ring (2 * m + 1 + 1)).zero then 1 else 0))
      rw [ite_neg' _ _ ha, show chi m a * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * chi m a) = chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * (chi m a * chi m a) by lib_int, h1]
      lib_int
  show sumK LibInt.intR (fun a => chi m a * chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).neg a))) (L m) = _
  rw [sumK_ext LibInt.intR _ _ pt]
  refine (mul_sumK LibInt.intR (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (fun a => LibInt.intR.add 1 (LibInt.intR.mul (-1) (d0 m a))) (L m)).symm.trans ?_
  rw [sumK_add, ← mul_sumK]
  have hc := CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR (ring (2 * m + 1 + 1)).zero
  rw [show sumK LibInt.intR (fun a => d0 m a) (L m) = 1 from hc]
  show chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * (sumK LibInt.intR (fun _ : Zp (2 * m + 1 + 1) => (1 : Int)) (CMInert.allZ (2 * m + 1 + 1) (2 * m + 1 + 1 + 1)) + -1 * 1) = _
  rw [sum_one]
  lib_int

/-- **g² = χ(−1)·(p − N) in ℤ[C_p].** -/
theorem gauss_sq (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (c : Zp (2 * m + 1 + 1)) :
    conv m (chi m) (chi m) c = chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * (Int.ofNat (2 * m + 1 + 1 + 1) * d0 m c - N m c) := by
  by_cases hc : c = (ring (2 * m + 1 + 1)).zero
  · rw [hc, gauss_zero m hpr]
    show _ = chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * (Int.ofNat (2 * m + 1 + 1 + 1) * (if (ring (2 * m + 1 + 1)).zero = (ring (2 * m + 1 + 1)).zero then 1 else 0) - 1)
    rw [ite_pos' _ _ rfl]; lib_int
  · rw [gauss_ne m hpr c hc]
    show _ = chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * (Int.ofNat (2 * m + 1 + 1 + 1) * (if c = (ring (2 * m + 1 + 1)).zero then 1 else 0) - 1)
    rw [ite_neg' _ _ hc]; lib_int

/-- **g ⋆ N = 0**: g lies in the augmentation ideal. -/
theorem gauss_N (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (c : Zp (2 * m + 1 + 1)) : conv m (chi m) (N m) c = 0 := by
  show sumK LibInt.intR (fun a => chi m a * 1) (L m) = 0
  rw [sumK_ext LibInt.intR _ (chi m) (fun a => by lib_int)]
  exact chi_sum m hpr

/-- **g² = p* in ℤ[ζ_p] = ℤ[C_p]/(N)**, p* = χ(−1)·p: (g ⋆ g)(c) + χ(−1)·N(c) = p*·ζ⁰(c) for every c. -/
theorem gauss_star (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (c : Zp (2 * m + 1 + 1)) :
    conv m (chi m) (chi m) c + chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * N m c = (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * Int.ofNat (2 * m + 1 + 1 + 1)) * d0 m c := by
  rw [gauss_sq m hpr c]; lib_int

end GaussSum

#print axioms GaussSum.conv
#print axioms GaussSum.sum_one
#print axioms GaussSum.term
#print axioms GaussSum.neg_ne
#print axioms GaussSum.gauss_ne
#print axioms GaussSum.gauss_zero
#print axioms GaussSum.gauss_sq
#print axioms GaussSum.gauss_N
#print axioms GaussSum.gauss_star

lang/GaussUnique.lean

lean_all/lang/GaussUnique.lean

/-
  lang/GaussUnique — G3 (T43): primary normalisation and uniqueness of the primary splitting in ℤ[i].

  For pairs z = (a, b) (Cyclo4.cyc4 ℤ), Prim z means a + bi primary (GaussInt.Primary: 2 | b, 4 | a + b − 1).
    normalize       N(α) odd ⇒ uα primary for some unit u                       (a odd: ±α;  b odd: ±iα)
    prim_quot       N(xy) odd, x and xy primary ⇒ y primary                        (GaussCube.unit_primary)
    uniq_split      αβ = α'β', all primary, N(α) = N(α') = m, N(β) = N(β') = n, sm + tn = 1 ⇒ α = α', β = β'
    prim_split      γ primary, N(γ) = mn, sm + tn = 1 ⇒ γ = αβ, α, β primary with N(α) = m, N(β) = n
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.GaussCoprime
import lang.GaussCube

namespace GaussUnique

open FTAChain DetChain LibInt GaussEuclid GaussCoprime GaussInt GaussFactor LibGcd

def toGI (z : Int × Int) : GI := ⟨z.1, z.2⟩
def Prim (z : Int × Int) : Prop := Primary (toGI z)

theorem toGI_mul (x y : Int × Int) : toGI ((Cyclo4.cyc4 LibInt.intR).mul x y) = gmul (toGI x) (toGI y) := by
  apply gi_ext
  · show x.1 * y.1 + -(x.2 * y.2) = x.1 * y.1 - x.2 * y.2; lib_int
  · show x.1 * y.2 + x.2 * y.1 = x.1 * y.2 + x.2 * y.1; rfl

theorem gnorm_toGI (z : Int × Int) : gnorm (toGI z) = gn z := rfl

def IsUnitP (u : Int × Int) : Prop := IsUnit (toGI u)

/-- odd x is 4e + 1 or 4e + 3. -/
theorem odd4 (w : Int) : (∃ e, 2 * w + 1 = 4 * e + 1) ∨ (∃ e, 2 * w + 1 = 4 * e + 3) := by
  match int_even_odd w with
  | Or.inl ⟨e, he⟩ => exact Or.inl ⟨e, by rw [he]; lib_int⟩
  | Or.inr ⟨e, he⟩ => exact Or.inr ⟨e, by rw [he]; lib_int⟩

/-- a odd, b even: (a, b) or (−a, −b) is primary. -/
theorem norm_ab (a b w c : Int) (ha : a = 2 * w + 1) (hb : b = 2 * c) : Prim (a, b) ∨ Prim (-a, -b) := by
  match odd4 (w + c) with
  | Or.inl ⟨e, he⟩ => exact Or.inl ⟨⟨c, hb⟩, ⟨e, by show a + b - 1 = 4 * e; rw [ha, hb, show 2 * w + 1 + 2 * c - 1 = (2 * (w + c) + 1) - 1 by lib_int, he]; lib_int⟩⟩
  | Or.inr ⟨e, he⟩ => exact Or.inr ⟨⟨-c, by show -b = 2 * -c; rw [hb]; lib_int⟩, ⟨-e - 1, by
      show -a + -b - 1 = 4 * (-e - 1); rw [ha, hb, show -(2 * w + 1) + -(2 * c) - 1 = -((2 * (w + c) + 1)) - 1 by lib_int, he]; lib_int⟩⟩

theorem parity_cases (a b : Int) (k : Int) (h : a * a + b * b = 2 * k + 1) :
    (∃ w c, a = 2 * w + 1 ∧ b = 2 * c) ∨ (∃ w c, b = 2 * w + 1 ∧ a = 2 * c) := by
  match int_even_odd a, int_even_odd b with
  | Or.inr ⟨w, hw⟩, Or.inl ⟨c, hc⟩ => exact Or.inl ⟨w, c, hw, hc⟩
  | Or.inl ⟨c, hc⟩, Or.inr ⟨w, hw⟩ => exact Or.inr ⟨w, c, hw, hc⟩
  | Or.inl ⟨c, hc⟩, Or.inl ⟨d, hd⟩ =>
    exfalso; exact int_odd_ne_even k (2 * c * c + 2 * d * d) (by rw [← h, hc, hd]; lib_int)
  | Or.inr ⟨w, hw⟩, Or.inr ⟨v, hv⟩ =>
    exfalso; exact int_odd_ne_even k (2 * w * w + 2 * w + 2 * v * v + 2 * v + 1) (by rw [← h, hw, hv]; lib_int)

/-- **Primary normalisation.** -/
theorem normalize (α : Int × Int) (k : Int) (h : gn α = 2 * k + 1) : ∃ u, IsUnitP u ∧ Prim ((Cyclo4.cyc4 LibInt.intR).mul u α) := by
  cases parity_cases α.1 α.2 k h with
  | inl hab =>
    match hab with
    | ⟨w, c, hw, hc⟩ =>
      cases norm_ab α.1 α.2 w c hw hc with
      | inl hp => exact ⟨(1, 0), Or.inl rfl, by rw [show (Cyclo4.cyc4 LibInt.intR).mul (1, 0) α = (α.1, α.2) from (Cyclo4.cyc4 LibInt.intR).one_mul α]; exact hp⟩
      | inr hp => exact ⟨(-1, 0), Or.inr (Or.inl rfl), by
          rw [show (Cyclo4.cyc4 LibInt.intR).mul (-1, 0) α = (-α.1, -α.2) from Prod.ext (by show -1 * α.1 + -(0 * α.2) = -α.1; lib_int) (by show -1 * α.2 + 0 * α.1 = -α.2; lib_int)]; exact hp⟩
  | inr hab =>
    match hab with
    | ⟨w, c, hw, hc⟩ =>
      have hb : -α.2 = 2 * (-w - 1) + 1 := by rw [hw]; lib_int
      cases norm_ab (-α.2) α.1 (-w - 1) c hb hc with
      | inl hp => exact ⟨(0, 1), Or.inr (Or.inr (Or.inl rfl)), by
          rw [show (Cyclo4.cyc4 LibInt.intR).mul (0, 1) α = (-α.2, α.1) from Prod.ext (by show 0 * α.1 + -(1 * α.2) = -α.2; lib_int) (by show 0 * α.2 + 1 * α.1 = α.1; lib_int)]; exact hp⟩
      | inr hp => exact ⟨(0, -1), Or.inr (Or.inr (Or.inr rfl)), by
          rw [show (Cyclo4.cyc4 LibInt.intR).mul (0, -1) α = (- -α.2, -α.1) from Prod.ext (by show 0 * α.1 + -(-1 * α.2) = - -α.2; lib_int) (by show 0 * α.2 + -1 * α.1 = -α.1; lib_int)]; exact hp⟩

theorem toGI_inj (x y : Int × Int) (h : toGI x = toGI y) : x = y :=
  Prod.ext (congrArg GI.re h) (congrArg GI.im h)

theorem unit_gn (u : Int × Int) (hu : IsUnitP u) : gn u = 1 ∧ (Cyclo4.cyc4 LibInt.intR).mul u (cj u) = (Cyclo4.cyc4 LibInt.intR).one := by
  have hu' : u = (u.1, u.2) := rfl
  rcases hu with h | h | h | h <;>
  · have e := toGI_inj u _ (h.trans (show _ = toGI (_ , _) from rfl))
    rw [e]; exact ⟨rfl, rfl⟩

theorem prim_mulP (x y : Int × Int) (hx : Prim x) (hy : Prim y) : Prim ((Cyclo4.cyc4 LibInt.intR).mul x y) := by
  unfold Prim; rw [toGI_mul]; exact prim_mul _ _ hx hy

/-- y is primary when x and xy are (N(y) odd). -/
theorem prim_quot (x y : Int × Int) (k : Int) (hk : gn y = 2 * k + 1) (hx : Prim x) (hxy : Prim ((Cyclo4.cyc4 LibInt.intR).mul x y)) : Prim y := by
  match normalize y k hk with
  | ⟨u, hu, hpu⟩ =>
    have h1 : Prim ((Cyclo4.cyc4 LibInt.intR).mul u ((Cyclo4.cyc4 LibInt.intR).mul x y)) := by
      rw [show (Cyclo4.cyc4 LibInt.intR).mul u ((Cyclo4.cyc4 LibInt.intR).mul x y) = (Cyclo4.cyc4 LibInt.intR).mul x ((Cyclo4.cyc4 LibInt.intR).mul u y) by lib_ring]; exact prim_mulP _ _ hx hpu
    have h2 : Primary (gmul (toGI u) (toGI ((Cyclo4.cyc4 LibInt.intR).mul x y))) := by rw [← toGI_mul]; exact h1
    have hu1 := GaussCube.unit_primary _ _ hu hxy h2
    have : u = (Cyclo4.cyc4 LibInt.intR).one := toGI_inj u (Cyclo4.cyc4 LibInt.intR).one hu1
    rw [this, (Cyclo4.cyc4 LibInt.intR).one_mul] at hpu
    exact hpu

/-- **Uniqueness of the primary splitting.** -/
theorem uniq_split (α β α' β' : Int × Int) (M N s t : Int) (hα : gn α = M) (hα' : gn α' = M) (hβ' : gn β' = N)
    (hst : s * M + t * N = 1) (hM : M ≠ 0) (pα : Prim α) (pα' : Prim α') (h : (Cyclo4.cyc4 LibInt.intR).mul α β = (Cyclo4.cyc4 LibInt.intR).mul α' β') :
    α = α' ∧ β = β' := by
  have hco : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (emb s) (cj α)) α) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (emb t) (cj β')) β') = (Cyclo4.cyc4 LibInt.intR).one := by
    rw [show (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (emb s) (cj α)) α) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (emb t) (cj β')) β') = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (emb s) ((Cyclo4.cyc4 LibInt.intR).mul α (cj α))) ((Cyclo4.cyc4 LibInt.intR).mul (emb t) ((Cyclo4.cyc4 LibInt.intR).mul β' (cj β')  -- (+15 chars; full line in the Zenodo source)
      mul_cj, mul_cj, hα, hβ']
    show (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (emb s) (emb M)) ((Cyclo4.cyc4 LibInt.intR).mul (emb t) (emb N)) = _
    rw [emb_mul, emb_mul, ← emb_add, hst]; rfl
  match euclid_coprime α β' α' _ _ hco ⟨β, by rw [show (Cyclo4.cyc4 LibInt.intR).mul β' α' = (Cyclo4.cyc4 LibInt.intR).mul α' β' by lib_ring, ← h]⟩ with
  | ⟨w, hw⟩ =>
    have hgw : gn w = 1 := by
      have e : M * gn w = M * 1 := by
        calc M * gn w = gn α * gn w := by rw [hα]
          _ = gn α' := by rw [← GaussEuclid.norm_mul, ← hw]
          _ = M * 1 := by rw [hα']; lib_int
      have e0 : M * (gn w + -1) = 0 := by rw [show M * (gn w + -1) = M * gn w + -(M * 1) by lib_int, e]; lib_int
      cases int_noZD _ _ e0 with
      | inl h0 => exact absurd h0 hM
      | inr h0 => calc gn w = (gn w + -1) + 1 := by lib_int
          _ = 1 := by rw [h0]; rfl
    have hu : IsUnit (toGI w) := norm_one _ hgw
    have h2 : Primary (gmul (toGI w) (toGI α)) := by
      rw [← toGI_mul, show (Cyclo4.cyc4 LibInt.intR).mul w α = α' by rw [hw]; lib_ring]; exact pα'
    have hw1 : w = (Cyclo4.cyc4 LibInt.intR).one := toGI_inj w (Cyclo4.cyc4 LibInt.intR).one (GaussCube.unit_primary _ _ hu pα h2)
    have hαα : α = α' := by rw [hw, hw1]; lib_ring
    refine ⟨hαα, ?_⟩
    have h0 : (Cyclo4.cyc4 LibInt.intR).mul α ((Cyclo4.cyc4 LibInt.intR).add β ((Cyclo4.cyc4 LibInt.intR).neg β')) = (Cyclo4.cyc4 LibInt.intR).zero := by
      rw [show (Cyclo4.cyc4 LibInt.intR).mul α ((Cyclo4.cyc4 LibInt.intR).add β ((Cyclo4.cyc4 LibInt.intR).neg β')) = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul α β) ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).mul α β')) by lib_ring, h, ← hαα]; lib_ring
    have := cancel_left α _ (by rw [hα]; exact hM) h0
    calc β = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add β ((Cyclo4.cyc4 LibInt.intR).neg β')) β' := by lib_ring
      _ = β' := by rw [this]; lib_ring

/-- **Existence of the primary splitting.** -/
theorem prim_split (γ : Int × Int) (m n : Nat) (s t km kn : Int) (hγ : gn γ = Int.ofNat ((m + 1) * (n + 1)))
    (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1) (hm : Int.ofNat (m + 1) = 2 * km + 1) (hn : Int.ofNat (n + 1) = 2 * kn + 1)
    (pγ : Prim γ) : ∃ α β, γ = (Cyclo4.cyc4 LibInt.intR).mul α β ∧ Prim α ∧ Prim β ∧ gn α = Int.ofNat (m + 1) ∧ gn β = Int.ofNat (n + 1) := by
  match coprime_split γ m n s t hγ hst with
  | ⟨α0, β0, h0, hα0, hβ0⟩ =>
    match normalize α0 km (by rw [hα0, hm]) with
    | ⟨u, hu, pu⟩ =>
      have ⟨gu, uc⟩ := unit_gn u hu
      have hβn : gn ((Cyclo4.cyc4 LibInt.intR).mul (cj u) β0) = Int.ofNat (n + 1) := by
        rw [GaussEuclid.norm_mul, show gn (cj u) = gn u from gn_cj u, gu, hβ0]; lib_int
      have hsplit : γ = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul u α0) ((Cyclo4.cyc4 LibInt.intR).mul (cj u) β0) := by
        rw [h0, show (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul u α0) ((Cyclo4.cyc4 LibInt.intR).mul (cj u) β0) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul u (cj u)) ((Cyclo4.cyc4 LibInt.intR).mul α0 β0) by lib_ring, uc]; lib_ring
      refine ⟨(Cyclo4.cyc4 LibInt.intR).mul u α0, (Cyclo4.cyc4 LibInt.intR).mul (cj u) β0, hsplit, pu,
        prim_quot _ _ kn (by rw [hβn, hn]) pu (by rw [← hsplit]; exact pγ),
        by rw [GaussEuclid.norm_mul, gu, hα0]; lib_int, hβn⟩

end GaussUnique

#print axioms GaussUnique.toGI_mul
#print axioms GaussUnique.odd4
#print axioms GaussUnique.norm_ab
#print axioms GaussUnique.parity_cases
#print axioms GaussUnique.normalize
#print axioms GaussUnique.toGI_inj
#print axioms GaussUnique.unit_gn
#print axioms GaussUnique.prim_mulP
#print axioms GaussUnique.prim_quot
#print axioms GaussUnique.uniq_split
#print axioms GaussUnique.prim_split

lang/Gcd163.lean

lean_all/lang/Gcd163.lean

/-
  lang/Gcd163 — T48/T50, T48: **gcd as a combination in ℤ[(1 + √−163)/2]** (a PID that is not norm-Euclidean).
    edh           for β ≠ 0: sα − qβ with N < N(β), and = 0 only for s = 1          (Euc163.dh_r on the two-step remainder)
    gaux          from any nonzero d = xα + yβ: either d | α, d | β, or a nonzero element of smaller norm in the ideal
    egcd_exists   d = xα + yβ dividing α and β
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.DH163

namespace Euc163

open FTAChain DetChain LibInt

theorem lt_of_mul (a N : Nat) (h : a * (N + 1) < (N + 1) * (N + 1)) : a < N + 1 := by
  cases Nat.lt_or_ge a (N + 1) with
  | inl h' => exact h'
  | inr h' => exact absurd (Nat.lt_of_lt_of_le h (Nat.mul_le_mul_right _ h')) (Nat.lt_irrefl _)

/-- **The Dedekind–Hasse step.** -/
theorem edh (α β : Int × Int) (N0 : Nat) (hβ : nrm β = N0 + 1) :
    ∃ s q : Int × Int, nrm ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s α) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul q β))) < N0 + 1 ∧
      (((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s α) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul q β))) = (Ring163.ring163 LibInt.intR).zero → s = (Ring163.ring163 LibInt.intR).one) := by
  have hN : en β = Int.ofNat (N0 + 1) := by rw [← nrm_cast, hβ]
  generalize hS : (Ring163.ring163 LibInt.intR).mul α (cj β) = S
  obtain ⟨q2, r2, h2, b2⟩ := GaussEuclid.rnd S.2 N0
  obtain ⟨q1, t, h1, b1⟩ := GaussEuclid.rnd (2 * S.1 + r2) (2 * N0 + 1)
  have hT : Int.natAbs (2 * (S.1 + -(q1 * Int.ofNat (N0 + 1))) + r2) ≤ N0 + 1 := by
    have e : 2 * (S.1 + -(q1 * Int.ofNat (N0 + 1))) + r2 = t := by
      have e2 : Int.ofNat (2 * N0 + 1 + 1) = 2 * Int.ofNat (N0 + 1) := by show 2 * Int.ofNat N0 + 1 + 1 = 2 * (Int.ofNat N0 + 1); lib_int
      calc 2 * (S.1 + -(q1 * Int.ofNat (N0 + 1))) + r2 = (2 * S.1 + r2) + -(q1 * (2 * Int.ofNat (N0 + 1))) := by lib_int
        _ = t := by rw [h1, ← e2]; lib_int
    rw [e]; exact Nat.le_of_mul_le_mul_left (Nat.le_trans b1 (Nat.le_of_eq (by lib_nat))) (Nat.zero_lt_succ 1)
  obtain ⟨s, e, hlt, hzero⟩ := dh163_r N0 (S.1 + -(q1 * Int.ofNat (N0 + 1))) r2 b2 hT
  have hSr : S = (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (q1, q2) (Int.ofNat (N0 + 1), 0)) (S.1 + -(q1 * Int.ofNat (N0 + 1)), r2) := by
    apply Prod.ext
    · show S.1 = q1 * Int.ofNat (N0 + 1) + -(q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 *  -- (+58 chars; full line in the Zenodo source)
    · show S.2 = q1 * 0 + q2 * Int.ofNat (N0 + 1) + q2 * 0 + r2; rw [h2]; show q2 * Int.ofNat (N0 + 1) + r2 = _; lib_int
  generalize hr : (S.1 + -(q1 * Int.ofNat (N0 + 1)), r2) = r at hSr hlt hzero
  refine ⟨s, (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (q1, q2)) e, ?_, ?_⟩
  all_goals
    have key : (Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s α) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (q1, q2)) e) β))) (cj β)
        = (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s r) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0))) := by
      rw [show (Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s α) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (q1, q2)) e) β))) (cj β)
          = (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s ((Ring163.ring163 LibInt.intR).mul α (cj β))) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (q1, q2)) e) ((Ring163.ring163 LibInt.intR).mul β (cj β)))) by lib_ring,
        hS, mul_cj, hN, hSr]
      lib_ring
  · -- norm bound
    have hn : en ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s α) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (q1, q2)) e) β))) * Int.ofNat (N0 + 1)
        = en ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s r) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0)))) := by
      rw [← key, en_mul, en_cj, hN]
    apply lt_of_mul
    have : nrm ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s α) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (q1, q2)) e) β))) * (N0 + 1)
        = nrm ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s r) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0)))) := by
      apply Int.ofNat.inj
      show Int.ofNat (nrm _) * Int.ofNat (N0 + 1) = Int.ofNat (nrm _)
      rw [nrm_cast, nrm_cast]; exact hn
    rw [this]; exact hlt
  · intro h0
    apply hzero
    rw [← key, h0]; lib_ring

theorem nrm_succ (d : Int × Int) (hd : d ≠ (Ring163.ring163 LibInt.intR).zero) : ∃ N0, nrm d = N0 + 1 := by
  cases h : nrm d with
  | zero => exact absurd (nrm_zero d h) hd
  | succ N0 => exact ⟨N0, rfl⟩

theorem gaux (α β : Int × Int) : ∀ G (d x y : Int × Int), d = (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul x α) ((Ring163.ring163 LibInt.intR).mul y β) →
    nrm d ≤ G → d ≠ (Ring163.ring163 LibInt.intR).zero →
    ∃ d' x' y', d' = (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul x' α) ((Ring163.ring163 LibInt.intR).mul y' β) ∧ Dvd' d' α ∧ Dvd' d' β
  | 0, d, _, _, _, hG, hd => absurd (nrm_zero d (Nat.le_antisymm hG (Nat.zero_le _))) hd
  | G + 1, d, x, y, hdef, hG, hd => by
    obtain ⟨N0, hN0⟩ := nrm_succ d hd
    obtain ⟨s, q, hlt, hz⟩ := edh α d N0 hN0
    have hle : ∀ ρ, nrm ρ < N0 + 1 → nrm ρ ≤ G := fun ρ h => Nat.le_of_lt_succ (Nat.lt_of_lt_of_le h (Nat.le_trans (Nat.le_of_eq hN0.symm) hG))
    by_cases hρ : (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s α) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul q d)) = (Ring163.ring163 LibInt.intR).zero
    · have s1 := hz hρ
      rw [s1] at hρ
      have hα : Dvd' d α := ⟨q, by
        calc α = (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (Ring163.ring163 LibInt.intR).one α) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul q d))) ((Ring163.ring163 LibInt.intR).mul d q) := by lib_ring
          _ = _ := by rw [hρ]; lib_ring⟩
      obtain ⟨s', q', hlt', hz'⟩ := edh β d N0 hN0
      by_cases hρ' : (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s' β) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul q' d)) = (Ring163.ring163 LibInt.intR).zero
      · have s1' := hz' hρ'
        rw [s1'] at hρ'
        exact ⟨d, x, y, hdef, hα, ⟨q', by
          calc β = (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (Ring163.ring163 LibInt.intR).one β) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul q' d))) ((Ring163.ring163 LibInt.intR).mul d q') := by lib_ring
            _ = _ := by rw [hρ']; lib_ring⟩⟩
      · exact gaux α β G _ ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul q' x)) ((Ring163.ring163 LibInt.intR).add s' ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul q' y)))
          (by rw [hdef]; lib_ring) (hle _ hlt') hρ'
    · exact gaux α β G _ ((Ring163.ring163 LibInt.intR).add s ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul q x))) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul q y))
        (by rw [hdef]; lib_ring) (hle _ hlt) hρ

/-- **gcd as a combination in ℤ[(1 + √−163)/2].** -/
theorem egcd_exists (α β : Int × Int) :
    ∃ d x y, d = (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul x α) ((Ring163.ring163 LibInt.intR).mul y β) ∧ Dvd' d α ∧ Dvd' d β := by
  by_cases hb : β = (Ring163.ring163 LibInt.intR).zero
  · exact egcd_base α β hb
  · exact gaux α β (nrm β) β (Ring163.ring163 LibInt.intR).zero (Ring163.ring163 LibInt.intR).one (by lib_ring) (Nat.le_refl _) hb

end Euc163

#print axioms Euc163.lt_of_mul
#print axioms Euc163.edh
#print axioms Euc163.nrm_succ
#print axioms Euc163.gaux
#print axioms Euc163.egcd_exists

lang/Gcd19.lean

lean_all/lang/Gcd19.lean

/-
  lang/Gcd19 — T48/T50, cycle 658: **gcd as a combination in ℤ[(1 + √−19)/2]** (a PID that is not norm-Euclidean).
    edh           for β ≠ 0: sα − qβ with N < N(β), and = 0 only for s = 1          (Euc19.dh_r on the two-step remainder)
    gaux          from any nonzero d = xα + yβ: either d | α, d | β, or a nonzero element of smaller norm in the ideal
    egcd_exists   d = xα + yβ dividing α and β
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Euc19

namespace Euc19

open FTAChain DetChain LibInt

theorem lt_of_mul (a N : Nat) (h : a * (N + 1) < (N + 1) * (N + 1)) : a < N + 1 := by
  cases Nat.lt_or_ge a (N + 1) with
  | inl h' => exact h'
  | inr h' => exact absurd (Nat.lt_of_lt_of_le h (Nat.mul_le_mul_right _ h')) (Nat.lt_irrefl _)

/-- **The Dedekind–Hasse step.** -/
theorem edh (α β : Int × Int) (N0 : Nat) (hβ : nrm β = N0 + 1) :
    ∃ s q : Int × Int, nrm ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s α) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul q β))) < N0 + 1 ∧
      (((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s α) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul q β))) = (Ring19.ring19 LibInt.intR).zero → s = (Ring19.ring19 LibInt.intR).one) := by
  have hN : en β = Int.ofNat (N0 + 1) := by rw [← nrm_cast, hβ]
  generalize hS : (Ring19.ring19 LibInt.intR).mul α (cj β) = S
  obtain ⟨q2, r2, h2, b2⟩ := GaussEuclid.rnd S.2 N0
  obtain ⟨q1, t, h1, b1⟩ := GaussEuclid.rnd (2 * S.1 + r2) (2 * N0 + 1)
  have hT : Int.natAbs (2 * (S.1 + -(q1 * Int.ofNat (N0 + 1))) + r2) ≤ N0 + 1 := by
    have e : 2 * (S.1 + -(q1 * Int.ofNat (N0 + 1))) + r2 = t := by
      have e2 : Int.ofNat (2 * N0 + 1 + 1) = 2 * Int.ofNat (N0 + 1) := by show 2 * Int.ofNat N0 + 1 + 1 = 2 * (Int.ofNat N0 + 1); lib_int
      calc 2 * (S.1 + -(q1 * Int.ofNat (N0 + 1))) + r2 = (2 * S.1 + r2) + -(q1 * (2 * Int.ofNat (N0 + 1))) := by lib_int
        _ = t := by rw [h1, ← e2]; lib_int
    rw [e]; exact Nat.le_of_mul_le_mul_left (Nat.le_trans b1 (Nat.le_of_eq (by lib_nat))) (Nat.zero_lt_succ 1)
  obtain ⟨s, e, hlt, hzero⟩ := dh_r N0 (S.1 + -(q1 * Int.ofNat (N0 + 1))) r2 b2 hT
  have hSr : S = (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul (q1, q2) (Int.ofNat (N0 + 1), 0)) (S.1 + -(q1 * Int.ofNat (N0 + 1)), r2) := by
    apply Prod.ext
    · show S.1 = q1 * Int.ofNat (N0 + 1) + -(q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0) + (S.1 + -(q1 * Int.ofNat (N0 + 1))); lib_int
    · show S.2 = q1 * 0 + q2 * Int.ofNat (N0 + 1) + q2 * 0 + r2; rw [h2]; show q2 * Int.ofNat (N0 + 1) + r2 = _; lib_int
  generalize hr : (S.1 + -(q1 * Int.ofNat (N0 + 1)), r2) = r at hSr hlt hzero
  refine ⟨s, (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s (q1, q2)) e, ?_, ?_⟩
  all_goals
    have key : (Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s α) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s (q1, q2)) e) β))) (cj β)
        = (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s r) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0))) := by
      rw [show (Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s α) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s (q1, q2)) e) β))) (cj β)
          = (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s ((Ring19.ring19 LibInt.intR).mul α (cj β))) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s (q1, q2)) e) ((Ring19.ring19 LibInt.intR).mul β (cj β)))) by lib_ring,
        hS, mul_cj, hN, hSr]
      lib_ring
  · -- norm bound
    have hn : en ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s α) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s (q1, q2)) e) β))) * Int.ofNat (N0 + 1)
        = en ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s r) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0)))) := by
      rw [← key, en_mul, en_cj, hN]
    apply lt_of_mul
    have : nrm ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s α) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s (q1, q2)) e) β))) * (N0 + 1)
        = nrm ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s r) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0)))) := by
      apply Int.ofNat.inj
      show Int.ofNat (nrm _) * Int.ofNat (N0 + 1) = Int.ofNat (nrm _)
      rw [nrm_cast, nrm_cast]; exact hn
    rw [this]; exact hlt
  · intro h0
    apply hzero
    rw [← key, h0]; lib_ring

theorem nrm_succ (d : Int × Int) (hd : d ≠ (Ring19.ring19 LibInt.intR).zero) : ∃ N0, nrm d = N0 + 1 := by
  cases h : nrm d with
  | zero => exact absurd (nrm_zero d h) hd
  | succ N0 => exact ⟨N0, rfl⟩

theorem gaux (α β : Int × Int) : ∀ G (d x y : Int × Int), d = (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul x α) ((Ring19.ring19 LibInt.intR).mul y β) →
    nrm d ≤ G → d ≠ (Ring19.ring19 LibInt.intR).zero →
    ∃ d' x' y', d' = (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul x' α) ((Ring19.ring19 LibInt.intR).mul y' β) ∧ Dvd' d' α ∧ Dvd' d' β
  | 0, d, _, _, _, hG, hd => absurd (nrm_zero d (Nat.le_antisymm hG (Nat.zero_le _))) hd
  | G + 1, d, x, y, hdef, hG, hd => by
    obtain ⟨N0, hN0⟩ := nrm_succ d hd
    obtain ⟨s, q, hlt, hz⟩ := edh α d N0 hN0
    have hle : ∀ ρ, nrm ρ < N0 + 1 → nrm ρ ≤ G := fun ρ h => Nat.le_of_lt_succ (Nat.lt_of_lt_of_le h (Nat.le_trans (Nat.le_of_eq hN0.symm) hG))
    by_cases hρ : (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s α) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul q d)) = (Ring19.ring19 LibInt.intR).zero
    · have s1 := hz hρ
      rw [s1] at hρ
      have hα : Dvd' d α := ⟨q, by
        calc α = (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul (Ring19.ring19 LibInt.intR).one α) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul q d))) ((Ring19.ring19 LibInt.intR).mul d q) := by lib_ring
          _ = _ := by rw [hρ]; lib_ring⟩
      obtain ⟨s', q', hlt', hz'⟩ := edh β d N0 hN0
      by_cases hρ' : (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s' β) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul q' d)) = (Ring19.ring19 LibInt.intR).zero
      · have s1' := hz' hρ'
        rw [s1'] at hρ'
        exact ⟨d, x, y, hdef, hα, ⟨q', by
          calc β = (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul (Ring19.ring19 LibInt.intR).one β) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul q' d))) ((Ring19.ring19 LibInt.intR).mul d q') := by lib_ring
            _ = _ := by rw [hρ']; lib_ring⟩⟩
      · exact gaux α β G _ ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul q' x)) ((Ring19.ring19 LibInt.intR).add s' ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul q' y)))
          (by rw [hdef]; lib_ring) (hle _ hlt') hρ'
    · exact gaux α β G _ ((Ring19.ring19 LibInt.intR).add s ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul q x))) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul q y))
        (by rw [hdef]; lib_ring) (hle _ hlt) hρ

/-- **gcd as a combination in ℤ[(1 + √−19)/2].** -/
theorem egcd_exists (α β : Int × Int) :
    ∃ d x y, d = (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul x α) ((Ring19.ring19 LibInt.intR).mul y β) ∧ Dvd' d α ∧ Dvd' d β := by
  by_cases hb : β = (Ring19.ring19 LibInt.intR).zero
  · exact egcd_base α β hb
  · exact gaux α β (nrm β) β (Ring19.ring19 LibInt.intR).zero (Ring19.ring19 LibInt.intR).one (by lib_ring) (Nat.le_refl _) hb

end Euc19

#print axioms Euc19.lt_of_mul
#print axioms Euc19.edh
#print axioms Euc19.nrm_succ
#print axioms Euc19.gaux
#print axioms Euc19.egcd_exists

lang/Gcd43.lean

lean_all/lang/Gcd43.lean

/-
  lang/Gcd43 — T48/T50, T48: **gcd as a combination in ℤ[(1 + √−43)/2]** (a PID that is not norm-Euclidean).
    edh           for β ≠ 0: sα − qβ with N < N(β), and = 0 only for s = 1          (Euc43.dh_r on the two-step remainder)
    gaux          from any nonzero d = xα + yβ: either d | α, d | β, or a nonzero element of smaller norm in the ideal
    egcd_exists   d = xα + yβ dividing α and β
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.DH43

namespace Euc43

open FTAChain DetChain LibInt

theorem lt_of_mul (a N : Nat) (h : a * (N + 1) < (N + 1) * (N + 1)) : a < N + 1 := by
  cases Nat.lt_or_ge a (N + 1) with
  | inl h' => exact h'
  | inr h' => exact absurd (Nat.lt_of_lt_of_le h (Nat.mul_le_mul_right _ h')) (Nat.lt_irrefl _)

/-- **The Dedekind–Hasse step.** -/
theorem edh (α β : Int × Int) (N0 : Nat) (hβ : nrm β = N0 + 1) :
    ∃ s q : Int × Int, nrm ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s α) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul q β))) < N0 + 1 ∧
      (((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s α) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul q β))) = (Ring43.ring43 LibInt.intR).zero → s = (Ring43.ring43 LibInt.intR).one) := by
  have hN : en β = Int.ofNat (N0 + 1) := by rw [← nrm_cast, hβ]
  generalize hS : (Ring43.ring43 LibInt.intR).mul α (cj β) = S
  obtain ⟨q2, r2, h2, b2⟩ := GaussEuclid.rnd S.2 N0
  obtain ⟨q1, t, h1, b1⟩ := GaussEuclid.rnd (2 * S.1 + r2) (2 * N0 + 1)
  have hT : Int.natAbs (2 * (S.1 + -(q1 * Int.ofNat (N0 + 1))) + r2) ≤ N0 + 1 := by
    have e : 2 * (S.1 + -(q1 * Int.ofNat (N0 + 1))) + r2 = t := by
      have e2 : Int.ofNat (2 * N0 + 1 + 1) = 2 * Int.ofNat (N0 + 1) := by show 2 * Int.ofNat N0 + 1 + 1 = 2 * (Int.ofNat N0 + 1); lib_int
      calc 2 * (S.1 + -(q1 * Int.ofNat (N0 + 1))) + r2 = (2 * S.1 + r2) + -(q1 * (2 * Int.ofNat (N0 + 1))) := by lib_int
        _ = t := by rw [h1, ← e2]; lib_int
    rw [e]; exact Nat.le_of_mul_le_mul_left (Nat.le_trans b1 (Nat.le_of_eq (by lib_nat))) (Nat.zero_lt_succ 1)
  obtain ⟨s, e, hlt, hzero⟩ := dh43_r N0 (S.1 + -(q1 * Int.ofNat (N0 + 1))) r2 b2 hT
  have hSr : S = (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul (q1, q2) (Int.ofNat (N0 + 1), 0)) (S.1 + -(q1 * Int.ofNat (N0 + 1)), r2) := by
    apply Prod.ext
    · show S.1 = q1 * Int.ofNat (N0 + 1) + -(q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0) + (S.1 + -(q1 * Int.ofNat (N0 + 1))); lib_int
    · show S.2 = q1 * 0 + q2 * Int.ofNat (N0 + 1) + q2 * 0 + r2; rw [h2]; show q2 * Int.ofNat (N0 + 1) + r2 = _; lib_int
  generalize hr : (S.1 + -(q1 * Int.ofNat (N0 + 1)), r2) = r at hSr hlt hzero
  refine ⟨s, (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s (q1, q2)) e, ?_, ?_⟩
  all_goals
    have key : (Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s α) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s (q1, q2)) e) β))) (cj β)
        = (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s r) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0))) := by
      rw [show (Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s α) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s (q1, q2)) e) β))) (cj β)
          = (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s ((Ring43.ring43 LibInt.intR).mul α (cj β))) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s (q1, q2)) e) ((Ring43.ring43 LibInt.intR).mul β (cj β)))) by lib_ring,
        hS, mul_cj, hN, hSr]
      lib_ring
  · -- norm bound
    have hn : en ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s α) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s (q1, q2)) e) β))) * Int.ofNat (N0 + 1)
        = en ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s r) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0)))) := by
      rw [← key, en_mul, en_cj, hN]
    apply lt_of_mul
    have : nrm ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s α) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s (q1, q2)) e) β))) * (N0 + 1)
        = nrm ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s r) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0)))) := by
      apply Int.ofNat.inj
      show Int.ofNat (nrm _) * Int.ofNat (N0 + 1) = Int.ofNat (nrm _)
      rw [nrm_cast, nrm_cast]; exact hn
    rw [this]; exact hlt
  · intro h0
    apply hzero
    rw [← key, h0]; lib_ring

theorem nrm_succ (d : Int × Int) (hd : d ≠ (Ring43.ring43 LibInt.intR).zero) : ∃ N0, nrm d = N0 + 1 := by
  cases h : nrm d with
  | zero => exact absurd (nrm_zero d h) hd
  | succ N0 => exact ⟨N0, rfl⟩

theorem gaux (α β : Int × Int) : ∀ G (d x y : Int × Int), d = (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul x α) ((Ring43.ring43 LibInt.intR).mul y β) →
    nrm d ≤ G → d ≠ (Ring43.ring43 LibInt.intR).zero →
    ∃ d' x' y', d' = (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul x' α) ((Ring43.ring43 LibInt.intR).mul y' β) ∧ Dvd' d' α ∧ Dvd' d' β
  | 0, d, _, _, _, hG, hd => absurd (nrm_zero d (Nat.le_antisymm hG (Nat.zero_le _))) hd
  | G + 1, d, x, y, hdef, hG, hd => by
    obtain ⟨N0, hN0⟩ := nrm_succ d hd
    obtain ⟨s, q, hlt, hz⟩ := edh α d N0 hN0
    have hle : ∀ ρ, nrm ρ < N0 + 1 → nrm ρ ≤ G := fun ρ h => Nat.le_of_lt_succ (Nat.lt_of_lt_of_le h (Nat.le_trans (Nat.le_of_eq hN0.symm) hG))
    by_cases hρ : (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s α) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul q d)) = (Ring43.ring43 LibInt.intR).zero
    · have s1 := hz hρ
      rw [s1] at hρ
      have hα : Dvd' d α := ⟨q, by
        calc α = (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul (Ring43.ring43 LibInt.intR).one α) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul q d))) ((Ring43.ring43 LibInt.intR).mul d q) := by lib_ring
          _ = _ := by rw [hρ]; lib_ring⟩
      obtain ⟨s', q', hlt', hz'⟩ := edh β d N0 hN0
      by_cases hρ' : (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s' β) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul q' d)) = (Ring43.ring43 LibInt.intR).zero
      · have s1' := hz' hρ'
        rw [s1'] at hρ'
        exact ⟨d, x, y, hdef, hα, ⟨q', by
          calc β = (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul (Ring43.ring43 LibInt.intR).one β) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul q' d))) ((Ring43.ring43 LibInt.intR).mul d q') := by lib_ring
            _ = _ := by rw [hρ']; lib_ring⟩⟩
      · exact gaux α β G _ ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul q' x)) ((Ring43.ring43 LibInt.intR).add s' ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul q' y)))
          (by rw [hdef]; lib_ring) (hle _ hlt') hρ'
    · exact gaux α β G _ ((Ring43.ring43 LibInt.intR).add s ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul q x))) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul q y))
        (by rw [hdef]; lib_ring) (hle _ hlt) hρ

/-- **gcd as a combination in ℤ[(1 + √−43)/2].** -/
theorem egcd_exists (α β : Int × Int) :
    ∃ d x y, d = (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul x α) ((Ring43.ring43 LibInt.intR).mul y β) ∧ Dvd' d α ∧ Dvd' d β := by
  by_cases hb : β = (Ring43.ring43 LibInt.intR).zero
  · exact egcd_base α β hb
  · exact gaux α β (nrm β) β (Ring43.ring43 LibInt.intR).zero (Ring43.ring43 LibInt.intR).one (by lib_ring) (Nat.le_refl _) hb

end Euc43

#print axioms Euc43.lt_of_mul
#print axioms Euc43.edh
#print axioms Euc43.nrm_succ
#print axioms Euc43.gaux
#print axioms Euc43.egcd_exists

lang/Gcd67.lean

lean_all/lang/Gcd67.lean

/-
  lang/Gcd67 — T48/T50, T48: **gcd as a combination in ℤ[(1 + √−67)/2]** (a PID that is not norm-Euclidean).
    edh           for β ≠ 0: sα − qβ with N < N(β), and = 0 only for s = 1          (Euc67.dh_r on the two-step remainder)
    gaux          from any nonzero d = xα + yβ: either d | α, d | β, or a nonzero element of smaller norm in the ideal
    egcd_exists   d = xα + yβ dividing α and β
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.DH67

namespace Euc67

open FTAChain DetChain LibInt

theorem lt_of_mul (a N : Nat) (h : a * (N + 1) < (N + 1) * (N + 1)) : a < N + 1 := by
  cases Nat.lt_or_ge a (N + 1) with
  | inl h' => exact h'
  | inr h' => exact absurd (Nat.lt_of_lt_of_le h (Nat.mul_le_mul_right _ h')) (Nat.lt_irrefl _)

/-- **The Dedekind–Hasse step.** -/
theorem edh (α β : Int × Int) (N0 : Nat) (hβ : nrm β = N0 + 1) :
    ∃ s q : Int × Int, nrm ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s α) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul q β))) < N0 + 1 ∧
      (((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s α) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul q β))) = (Ring67.ring67 LibInt.intR).zero → s = (Ring67.ring67 LibInt.intR).one) := by
  have hN : en β = Int.ofNat (N0 + 1) := by rw [← nrm_cast, hβ]
  generalize hS : (Ring67.ring67 LibInt.intR).mul α (cj β) = S
  obtain ⟨q2, r2, h2, b2⟩ := GaussEuclid.rnd S.2 N0
  obtain ⟨q1, t, h1, b1⟩ := GaussEuclid.rnd (2 * S.1 + r2) (2 * N0 + 1)
  have hT : Int.natAbs (2 * (S.1 + -(q1 * Int.ofNat (N0 + 1))) + r2) ≤ N0 + 1 := by
    have e : 2 * (S.1 + -(q1 * Int.ofNat (N0 + 1))) + r2 = t := by
      have e2 : Int.ofNat (2 * N0 + 1 + 1) = 2 * Int.ofNat (N0 + 1) := by show 2 * Int.ofNat N0 + 1 + 1 = 2 * (Int.ofNat N0 + 1); lib_int
      calc 2 * (S.1 + -(q1 * Int.ofNat (N0 + 1))) + r2 = (2 * S.1 + r2) + -(q1 * (2 * Int.ofNat (N0 + 1))) := by lib_int
        _ = t := by rw [h1, ← e2]; lib_int
    rw [e]; exact Nat.le_of_mul_le_mul_left (Nat.le_trans b1 (Nat.le_of_eq (by lib_nat))) (Nat.zero_lt_succ 1)
  obtain ⟨s, e, hlt, hzero⟩ := dh67_r N0 (S.1 + -(q1 * Int.ofNat (N0 + 1))) r2 b2 hT
  have hSr : S = (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul (q1, q2) (Int.ofNat (N0 + 1), 0)) (S.1 + -(q1 * Int.ofNat (N0 + 1)), r2) := by
    apply Prod.ext
    · show S.1 = q1 * Int.ofNat (N0 + 1) + -(q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0) + (S.1 + -(q1 * Int.ofNat (N0 + 1))); lib_int
    · show S.2 = q1 * 0 + q2 * Int.ofNat (N0 + 1) + q2 * 0 + r2; rw [h2]; show q2 * Int.ofNat (N0 + 1) + r2 = _; lib_int
  generalize hr : (S.1 + -(q1 * Int.ofNat (N0 + 1)), r2) = r at hSr hlt hzero
  refine ⟨s, (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s (q1, q2)) e, ?_, ?_⟩
  all_goals
    have key : (Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s α) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s (q1, q2)) e) β))) (cj β)
        = (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s r) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0))) := by
      rw [show (Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s α) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s (q1, q2)) e) β))) (cj β)
          = (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s ((Ring67.ring67 LibInt.intR).mul α (cj β))) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s (q1, q2)) e) ((Ring67.ring67 LibInt.intR).mul β (cj β)))) by lib_ring,
        hS, mul_cj, hN, hSr]
      lib_ring
  · -- norm bound
    have hn : en ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s α) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s (q1, q2)) e) β))) * Int.ofNat (N0 + 1)
        = en ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s r) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0)))) := by
      rw [← key, en_mul, en_cj, hN]
    apply lt_of_mul
    have : nrm ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s α) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s (q1, q2)) e) β))) * (N0 + 1)
        = nrm ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s r) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0)))) := by
      apply Int.ofNat.inj
      show Int.ofNat (nrm _) * Int.ofNat (N0 + 1) = Int.ofNat (nrm _)
      rw [nrm_cast, nrm_cast]; exact hn
    rw [this]; exact hlt
  · intro h0
    apply hzero
    rw [← key, h0]; lib_ring

theorem nrm_succ (d : Int × Int) (hd : d ≠ (Ring67.ring67 LibInt.intR).zero) : ∃ N0, nrm d = N0 + 1 := by
  cases h : nrm d with
  | zero => exact absurd (nrm_zero d h) hd
  | succ N0 => exact ⟨N0, rfl⟩

theorem gaux (α β : Int × Int) : ∀ G (d x y : Int × Int), d = (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul x α) ((Ring67.ring67 LibInt.intR).mul y β) →
    nrm d ≤ G → d ≠ (Ring67.ring67 LibInt.intR).zero →
    ∃ d' x' y', d' = (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul x' α) ((Ring67.ring67 LibInt.intR).mul y' β) ∧ Dvd' d' α ∧ Dvd' d' β
  | 0, d, _, _, _, hG, hd => absurd (nrm_zero d (Nat.le_antisymm hG (Nat.zero_le _))) hd
  | G + 1, d, x, y, hdef, hG, hd => by
    obtain ⟨N0, hN0⟩ := nrm_succ d hd
    obtain ⟨s, q, hlt, hz⟩ := edh α d N0 hN0
    have hle : ∀ ρ, nrm ρ < N0 + 1 → nrm ρ ≤ G := fun ρ h => Nat.le_of_lt_succ (Nat.lt_of_lt_of_le h (Nat.le_trans (Nat.le_of_eq hN0.symm) hG))
    by_cases hρ : (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s α) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul q d)) = (Ring67.ring67 LibInt.intR).zero
    · have s1 := hz hρ
      rw [s1] at hρ
      have hα : Dvd' d α := ⟨q, by
        calc α = (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul (Ring67.ring67 LibInt.intR).one α) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul q d))) ((Ring67.ring67 LibInt.intR).mul d q) := by lib_ring
          _ = _ := by rw [hρ]; lib_ring⟩
      obtain ⟨s', q', hlt', hz'⟩ := edh β d N0 hN0
      by_cases hρ' : (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s' β) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul q' d)) = (Ring67.ring67 LibInt.intR).zero
      · have s1' := hz' hρ'
        rw [s1'] at hρ'
        exact ⟨d, x, y, hdef, hα, ⟨q', by
          calc β = (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul (Ring67.ring67 LibInt.intR).one β) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul q' d))) ((Ring67.ring67 LibInt.intR).mul d q') := by lib_ring
            _ = _ := by rw [hρ']; lib_ring⟩⟩
      · exact gaux α β G _ ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul q' x)) ((Ring67.ring67 LibInt.intR).add s' ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul q' y)))
          (by rw [hdef]; lib_ring) (hle _ hlt') hρ'
    · exact gaux α β G _ ((Ring67.ring67 LibInt.intR).add s ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul q x))) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul q y))
        (by rw [hdef]; lib_ring) (hle _ hlt) hρ

/-- **gcd as a combination in ℤ[(1 + √−67)/2].** -/
theorem egcd_exists (α β : Int × Int) :
    ∃ d x y, d = (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul x α) ((Ring67.ring67 LibInt.intR).mul y β) ∧ Dvd' d α ∧ Dvd' d β := by
  by_cases hb : β = (Ring67.ring67 LibInt.intR).zero
  · exact egcd_base α β hb
  · exact gaux α β (nrm β) β (Ring67.ring67 LibInt.intR).zero (Ring67.ring67 LibInt.intR).one (by lib_ring) (Nat.le_refl _) hb

end Euc67

#print axioms Euc67.lt_of_mul
#print axioms Euc67.edh
#print axioms Euc67.nrm_succ
#print axioms Euc67.gaux
#print axioms Euc67.egcd_exists

lang/GenCubic.lean

lean_all/lang/GenCubic.lean

/-
  lang/GenCubic — T43, cycle 186: F[∛c] over any base field F: the cubic ring K[t]/(t³ − c) and its Frobenius.

  Port of CubicFrob to an arbitrary commutative ring K (the base field 𝔽_q):
    nsm_cubic, char_cubic, scal_mul, pw_embed3, tt, tmul, t_cube, t_pow, t_pow1, decomp
    frob3G     q = p^N = 3m + 1, a^q = a on K:  (a + bt + dt²)^q = a + ωbt + ω²dt², ω = c^m       (FrobIter)

  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Cubic
import lang.FrobIter
import lang.FinPow2

namespace GenCubic

open FTAChain Frobenius

abbrev R3 {S : Type} (K : DetChain.CRing S) (c : S) := Cubic.cubic K c

variable {S : Type} (K : DetChain.CRing S) (c : S)

theorem nsm_cubic (x : S × S × S) : ∀ k,
    nsm (R3 K c) k x = (nsm K k x.1, nsm K k x.2.1, nsm K k x.2.2)
  | 0 => rfl
  | k + 1 => by
    show (R3 K c).add (nsm (R3 K c) k x) x = _
    rw [nsm_cubic x k]; rfl

theorem char_cubic (P : Nat) (hc : nsm K P K.one = K.zero) : nsm (R3 K c) P (R3 K c).one = (R3 K c).zero := by
  show nsm (R3 K c) P (K.one, K.zero, K.zero) = (K.zero, K.zero, K.zero)
  rw [nsm_cubic]
  show (nsm K P K.one, nsm K P K.zero, nsm K P K.zero) = _
  rw [hc, FinPow2.nsm_zeroG]

theorem scal_mul (s : S) (x : S × S × S) :
    (R3 K c).mul (s, K.zero, K.zero) x = (K.mul s x.1, K.mul s x.2.1, K.mul s x.2.2) := by
  apply Prod.ext
  · show K.add (K.mul s x.1) (K.mul c (K.add (K.mul K.zero x.2.2) (K.mul K.zero x.2.1))) = K.mul s x.1; lib_ring
  · apply Prod.ext
    · show K.add (K.add (K.mul s x.2.1) (K.mul K.zero x.1)) (K.mul c (K.mul K.zero x.2.2)) = K.mul s x.2.1; lib_ring
    · show K.add (K.add (K.mul s x.2.2) (K.mul K.zero x.2.1)) (K.mul K.zero x.1) = K.mul s x.2.2; lib_ring

theorem pw_embed3 (a : S) : ∀ k, pw (R3 K c) (a, K.zero, K.zero) k = (pw K a k, K.zero, K.zero)
  | 0 => rfl
  | k + 1 => by
    show (R3 K c).mul (pw (R3 K c) (a, K.zero, K.zero) k) (a, K.zero, K.zero) = _
    rw [pw_embed3 a k, scal_mul]
    show ((K.mul (pw K a k) a), K.mul (pw K a k) K.zero, K.mul (pw K a k) K.zero) = _
    rw [DetChain.mul_zero]; rfl

def tt : S × S × S := (K.zero, K.one, K.zero)

theorem tmul (x : S × S × S) :
    (R3 K c).mul (tt K) x = (K.mul c x.2.2, x.1, x.2.1) := by
  apply Prod.ext
  · show K.add (K.mul K.zero x.1) (K.mul c (K.add (K.mul K.one x.2.2) (K.mul K.zero x.2.1))) = K.mul c x.2.2; lib_ring
  · apply Prod.ext
    · show K.add (K.add (K.mul K.zero x.2.1) (K.mul K.one x.1)) (K.mul c (K.mul K.zero x.2.2)) = x.1; lib_ring
    · show K.add (K.add (K.mul K.zero x.2.2) (K.mul K.one x.2.1)) (K.mul K.zero x.1) = x.2.1; lib_ring

theorem pw_t_succ (k : Nat) : pw (R3 K c) (tt K) (k + 1) = (R3 K c).mul (tt K) (pw (R3 K c) (tt K) k) := (R3 K c).mul_comm _ _

theorem t_cube : pw (R3 K c) (tt K) 3 = (c, K.zero, K.zero) := by
  rw [pw_t_succ, pw_t_succ, pw_t_succ, show pw (R3 K c) (tt K) 0 = (K.one, K.zero, K.zero) from rfl, tmul, tmul, tmul]
  apply Prod.ext
  · show K.mul c K.one = c; rw [K.mul_comm, K.one_mul]
  · apply Prod.ext
    · show K.mul c K.zero = K.zero; exact DetChain.mul_zero K c
    · show K.mul c K.zero = K.zero; exact DetChain.mul_zero K c


theorem t_pow (m : Nat) : Frobenius.pw (R3 K c) (tt K) (3 * m) = (Frobenius.pw K c m, K.zero, K.zero) := by
  rw [← TwoPower.pwpow, t_cube, pw_embed3]

theorem t_pow1 (m : Nat) : Frobenius.pw (R3 K c) (tt K) (3 * m + 1) = (K.zero, Frobenius.pw K c m, K.zero) := by
  rw [pw_t_succ, t_pow, tmul]
  apply Prod.ext
  · show K.mul c K.zero = K.zero; exact DetChain.mul_zero K c
  · rfl

/-- x = a + b t + d t². -/
theorem decomp (x : S × S × S) :
    x = (R3 K c).add (x.1, K.zero, K.zero) ((R3 K c).add ((R3 K c).mul (x.2.1, K.zero, K.zero) (tt K))
      ((R3 K c).mul (x.2.2, K.zero, K.zero) ((R3 K c).mul (tt K) (tt K)))) := by
  apply Prod.ext
  · show x.1 = K.add (x.1) (K.add (K.add (K.mul (x.2.1) (K.zero)) (K.mul c (K.add (K.mul (K.zero) (K.zero)) (K.mul (K.zero) (K.one))))) (K.add (K.mul (x.2.2) (K.add (K.mul (K.zero) (K.zero)) (K.mul c (K.add (K.mul (K.one) (K.zero)) (K.mul (K.zero) (K.one)))))) (K.mul c (K.add (K.mul (K.zero) (K.add (K.add (K.mul (K.zero) (K.zero)) (K.mul (K.one) (K.one))) (K.mul (K.zero) (K.zero)))) (K.mul (K.zero  -- (+118 chars; full line in the Zenodo source)
  · apply Prod.ext
    · show x.2.1 = K.add (K.zero) (K.add (K.add (K.add (K.mul (x.2.1) (K.one)) (K.mul (K.zero) (K.zero))) (K.mul c (K.mul (K.zero) (K.zero)))) (K.add (K.add (K.mul (x.2.2) (K.add (K.add (K.mul (K.zero) (K.one)) (K.mul (K.one) (K.zero))) (K.mul c (K.mul (K.zero) (K.zero))))) (K.mul (K.zero) (K.add (K.mul (K.zero) (K.zero)) (K.mul c (K.add (K.mul (K.one) (K.zero)) (K.mul (K.zero) (K.one))))))) (K.mu  -- (+125 chars; full line in the Zenodo source)
    · show x.2.2 = K.add (K.zero) (K.add (K.add (K.add (K.mul (x.2.1) (K.zero)) (K.mul (K.zero) (K.one))) (K.mul (K.zero) (K.zero))) (K.add (K.add (K.mul (x.2.2) (K.add (K.add (K.mul (K.zero) (K.zero)) (K.mul (K.one) (K.one))) (K.mul (K.zero) (K.zero)))) (K.mul (K.zero) (K.add (K.add (K.mul (K.zero) (K.one)) (K.mul (K.one) (K.zero))) (K.mul c (K.mul (K.zero) (K.zero)))))) (K.mul (K.zero) (K.add (K  -- (+105 chars; full line in the Zenodo source)

/-- **Frobenius over any base: (a + bt + dt²)^q = a + ωbt + ω²dt², ω = c^m, q = p^N = 3m + 1, a^q = a on K.** -/
theorem frob3G (j : Nat) (hpr : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1) (hch : nsm K (j + 1 + 1) K.one = K.zero)
    (N m : Nat) (hq : FrobIter.npow (j + 1 + 1) N = 3 * m + 1) (hf : ∀ a : S, Frobenius.pw K a (FrobIter.npow (j + 1 + 1) N) = a)
    (x : S × S × S) :
    Frobenius.pw (R3 K c) x (FrobIter.npow (j + 1 + 1) N) = (x.1, K.mul (Frobenius.pw K c m) x.2.1, K.mul (K.mul (Frobenius.pw K c m) (Frobenius.pw K c m)) x.2.2) := by
  have hc := char_cubic K c (j + 1 + 1) hch
  conv => lhs; rw [decomp K c x]
  rw [FrobIter.frob_iter_add (R3 K c) j hpr hc, FrobIter.frob_iter_add (R3 K c) j hpr hc, Frobenius.pw_mul, Frobenius.pw_mul, Frobenius.pw_mul,
    pw_embed3, pw_embed3, pw_embed3, hf, hf, hf, hq, t_pow1]
  apply Prod.ext
  · show K.add (x.1) (K.add (K.add (K.mul (x.2.1) (K.zero)) (K.mul c (K.add (K.mul (K.zero) (K.zero)) (K.mul (K.zero) ((Frobenius.pw K c m)))))) (K.add (K.mul (x.2.2) (K.add (K.mul (K.zero) (K.zero)) (K.mul c (K.add (K.mul ((Frobenius.pw K c m)) (K.zero)) (K.mul (K.zero) ((Frobenius.pw K c m))))))) (K.mul c (K.add (K.mul (K.zero) (K.add (K.add (K.mul (K.zero) (K.zero)) (K.mul ((Frobenius.pw K c m)  -- (+223 chars; full line in the Zenodo source)
  · apply Prod.ext
    · show K.add (K.zero) (K.add (K.add (K.add (K.mul (x.2.1) ((Frobenius.pw K c m))) (K.mul (K.zero) (K.zero))) (K.mul c (K.mul (K.zero) (K.zero)))) (K.add (K.add (K.mul (x.2.2) (K.add (K.add (K.mul (K.zero) ((Frobenius.pw K c m))) (K.mul ((Frobenius.pw K c m)) (K.zero))) (K.mul c (K.mul (K.zero) (K.zero))))) (K.mul (K.zero) (K.add (K.mul (K.zero) (K.zero)) (K.mul c (K.add (K.mul ((Frobenius.pw K  -- (+257 chars; full line in the Zenodo source)
    · show K.add (K.zero) (K.add (K.add (K.add (K.mul (x.2.1) (K.zero)) (K.mul (K.zero) ((Frobenius.pw K c m)))) (K.mul (K.zero) (K.zero))) (K.add (K.add (K.mul (x.2.2) (K.add (K.add (K.mul (K.zero) (K.zero)) (K.mul ((Frobenius.pw K c m)) ((Frobenius.pw K c m)))) (K.mul (K.zero) (K.zero)))) (K.mul (K.zero) (K.add (K.add (K.mul (K.zero) ((Frobenius.pw K c m))) (K.mul ((Frobenius.pw K c m)) (K.zero)  -- (+266 chars; full line in the Zenodo source)

end GenCubic

#print axioms GenCubic.nsm_cubic
#print axioms GenCubic.char_cubic
#print axioms GenCubic.scal_mul
#print axioms GenCubic.pw_embed3
#print axioms GenCubic.tt
#print axioms GenCubic.tmul
#print axioms GenCubic.pw_t_succ
#print axioms GenCubic.t_cube
#print axioms GenCubic.t_pow
#print axioms GenCubic.t_pow1
#print axioms GenCubic.decomp
#print axioms GenCubic.frob3G

lang/GenCubicExt.lean

lean_all/lang/GenCubicExt.lean

/-
  lang/GenCubicExt — T43, cycle 189: every finite field F of the interface with characteristic p, |F| = q = p^N ≡ 1 (mod 3)
  has a cubic extension of order q³ (of the interface), with characteristic p and ℤ → F[∛c] landing in F.

    fermat_q       a^q = a in F
    noncube        a primitive root g has g^m ≠ 1 (2h = 3m)
    cubic_ext      ∃ G : FF (R × R × R), 2h_G + 1 = q³, p·1 = 0 in G, castZ G z = 0 ⇒ castZ F z = 0

  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.GenCubicInst
import lang.FinCyclic
import lang.CubeHasse

namespace GenCubicExt

open FTAChain YMChain FiniteRecChain Frobenius
open LibRing (castZ castN)

variable {R : Type} [DecidableEq R] (F : FinField.FF R)

theorem fermat_q (a : R) : pw F.K a (2 * F.h + 1) = a := by
  by_cases ha : a = F.K.zero
  · rw [ha]; exact HasseCM.pw_zero_succ F.K _
  · show F.K.mul (pw F.K a (2 * F.h)) a = a
    rw [FinCyclic.fermat_ff F a ha, F.K.one_mul]

theorem h_succ : ∃ k, F.h = k + 1 := by
  have hh := FinCyclic.h_pos F
  cases e : F.h with
  | zero => rw [e] at hh; exact absurd hh (Nat.not_succ_le_zero 0)
  | succ k => exact ⟨k, rfl⟩

theorem noncube (m : Nat) (hm : 2 * F.h = 3 * m) : ∃ g, g ≠ F.K.zero ∧ pw F.K g m ≠ F.K.one := by
  match FinCyclic.primitive_root_ff F, h_succ F with
  | ⟨g, hg1, hg2⟩, ⟨h', hh'⟩ =>
    have m0 : m ≠ 0 := fun e => by
      rw [e, hh'] at hm
      exact Nat.noConfusion ((show 2 * (h' + 1) = (2 * h' + 1) + 1 by lib_nat).symm.trans hm)
    refine ⟨g, fun e => ?_, fun e => ?_⟩
    · rw [e, hh', show 2 * (h' + 1) = (2 * h' + 1) + 1 by lib_nat, HasseCM.pw_zero_succ] at hg1
      exact GenCubicInst.one_ne_zero3 F F.K.zero (Prod.ext hg1.symm rfl)
    · match hg2 m e with
      | ⟨k, hk⟩ =>
        cases k with
        | zero => exact m0 (hk.trans (Nat.mul_zero _))
        | succ k =>
          rw [hm] at hk
          have h0 : m + 0 = m + (2 * m + 3 * m * k) := by rw [Nat.add_zero]; exact hk.trans (by lib_nat)
          have h1 := FermatUnique.add_cancel m 0 _ h0
          have h2 := (HasseDavenport.nat_sum_zero (2 * m) (3 * m * k) h1.symm).1
          exact m0 (HasseDavenport.nat_sum_zero m m ((show m + m = 2 * m by lib_nat).trans h2)).1

/-- **Every F of order q = p^N ≡ 1 (mod 3) has a cubic extension of order q³.** -/
theorem cubic_ext (j : Nat) (hpr : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1) (hch : nsm F.K (j + 1 + 1) F.K.one = F.K.zero)
    (N : Nat) (hq : FrobIter.npow (j + 1 + 1) N = 2 * F.h + 1) (m : Nat) (hm : 2 * F.h = 3 * m) :
    ∃ G : FinField.FF (R × R × R), 2 * G.h + 1 = (2 * F.h + 1) * ((2 * F.h + 1) * (2 * F.h + 1)) ∧
      nsm G.K (j + 1 + 1) G.K.one = G.K.zero ∧ (∀ z : Int, castZ G.K z = G.K.zero → castZ F.K z = F.K.zero) := by
  match h_succ F, noncube F m hm with
  | ⟨h', hh'⟩, ⟨c, hc0, hω⟩ =>
    have hQ : FrobIter.npow (j + 1 + 1) N = 2 * h' + 1 + 1 + 1 := by rw [hq, hh']; lib_nat
    have hfrob : ∀ x : R × R × R, Frobenius.pw (GenCubic.R3 F.K c) x (2 * h' + 1 + 1 + 1) = (x.1, F.K.mul (pw F.K c m) x.2.1, F.K.mul (F.K.mul (pw F.K c m) (pw F.K c m)) x.2.2) := by
      intro x
      rw [← hQ]
      exact GenCubic.frob3G F.K c j hpr hch N m (by rw [hq, hm]) (fun a => by rw [hq]; exact fermat_q F a) x
    have hw3 : F.K.mul (pw F.K c m) (F.K.mul (pw F.K c m) (pw F.K c m)) = F.K.one := by
      rw [show F.K.mul (pw F.K c m) (F.K.mul (pw F.K c m) (pw F.K c m)) = pw F.K (pw F.K c m) 3 by
        show _ = F.K.mul (F.K.mul (F.K.mul F.K.one _) _) _; lib_ring, TwoPower.pwpow, Nat.mul_comm, ← hm]
      exact FinCyclic.fermat_ff F c hc0
    have hR : (2 * h' + 1 + 1 + 1) * (2 * h' + 1 + 1 + 1) * (2 * h' + 1 + 1 + 1)
        = (1 + (2 * h' + 1 + 1 + 1) + (2 * h' + 1 + 1 + 1) * (2 * h' + 1 + 1 + 1)) + (8 * h' * h' * h' + 32 * h' * h' + 40 * h' + 14) := by lib_nat
    refine ⟨GenCubicInst.ffG F c h' (pw F.K c m) hh' hfrob hw3 _ hR hω, ?_, ?_, ?_⟩
    · show 2 * ((4 * h' * h' * h' + 18 * h' * h' + 27 * h' + 12) + 1) + 1 = _
      rw [hh', show 2 * (h' + 1) + 1 = 2 * h' + 1 + 1 + 1 by lib_nat, ← CubicInst.cube_form h']
    · exact GenCubic.char_cubic F.K c (j + 1 + 1) hch
    · intro z hz
      have h1 := congrArg Prod.fst hz
      rw [show castZ (GenCubicInst.ffG F c h' (pw F.K c m) hh' hfrob hw3 _ hR hω).K z = castZ (GenCubic.R3 F.K c) z from rfl,
        CubeHasse.castZ_cubic] at h1
      exact h1

end GenCubicExt

#print axioms GenCubicExt.fermat_q
#print axioms GenCubicExt.h_succ
#print axioms GenCubicExt.noncube
#print axioms GenCubicExt.cubic_ext

lang/GenCubicField.lean

lean_all/lang/GenCubicField.lean

/-
  lang/GenCubicField — T43, cycle 187: F[∛c] has inverses and no zero divisors over any finite field F, given the Frobenius
  σ(x) = x^Q = (a, ωb, ω²d) with ω³ = 1, ω ≠ 1 (GenCubic.frob3G supplies it).

  Port of CubicField with the base field abstract: cancel, omega2_ne, fixed_scalar, Nm, sigma3, norm_fixed, norm_pw,
  norm_zero, norm_scalar, inverse3 (inverse via F.iv), nozd3.

  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.GenCubic
import lang.FinField

namespace GenCubicField

open FTAChain Frobenius

variable {R : Type} [DecidableEq R] (F : FinField.FF R) (c : R) (Q : Nat) (w : R)

theorem omega2_ne (w : R) (hw3 : F.K.mul w (F.K.mul w w) = F.K.one) (hω : w ≠ F.K.one) : F.K.mul w w ≠ F.K.one := fun e => hω (by
  rw [e, F.K.mul_comm, F.K.one_mul] at hw3; exact hw3)

theorem cancel (u y : R) (hu : u ≠ F.K.one)
    (h : F.K.mul u y = y) : y = F.K.zero := by
  have e : F.K.mul (F.K.add u (F.K.neg F.K.one)) y = F.K.zero := by
    rw [DetChain.right_distrib, h, DetChain.neg_mul_left, F.K.one_mul, F.K.add_neg]
  cases F.nozd _ _ e with
  | inl h1 =>
    exfalso; apply hu
    have := congrArg (fun z => F.K.add z F.K.one) h1
    show u = F.K.one
    have h2 : F.K.add (F.K.add u (F.K.neg F.K.one)) F.K.one = F.K.add F.K.zero F.K.one := this
    rw [F.K.add_assoc, DetChain.add_neg' F.K F.K.one, DetChain.add_zero', F.K.zero_add] at h2
    exact h2
  | inr h2 => exact h2

/-- **σ-fixed elements are scalars.** -/
theorem fixed_scalar (hfrob : ∀ x : R × R × R, pw (GenCubic.R3 F.K c) x Q = (x.1, F.K.mul w x.2.1, F.K.mul (F.K.mul w w) x.2.2)) (hw3 : F.K.mul w (F.K.mul w w) = F.K.one)
    (hω : w ≠ F.K.one) (x : R × R × R)
    (h : pw (GenCubic.R3 F.K c) x Q = x) : x = (x.1, F.K.zero, F.K.zero) := by
  rw [hfrob] at h
  have h2 : F.K.mul w x.2.1 = x.2.1 := congrArg (fun z => z.2.1) h
  have h3 : F.K.mul (F.K.mul w w) x.2.2 = x.2.2 := congrArg (fun z => z.2.2) h
  apply Prod.ext
  · rfl
  · apply Prod.ext
    · exact cancel F _ _ hω h2
    · exact cancel F _ _ (omega2_ne F w hw3 hω) h3

def Nm (x : R × R × R) : R × R × R :=
  (GenCubic.R3 F.K c).mul x ((GenCubic.R3 F.K c).mul (pw (GenCubic.R3 F.K c) x Q) (pw (GenCubic.R3 F.K c) x (Q * Q)))

theorem sigma3 (hfrob : ∀ x : R × R × R, pw (GenCubic.R3 F.K c) x Q = (x.1, F.K.mul w x.2.1, F.K.mul (F.K.mul w w) x.2.2)) (hw3 : F.K.mul w (F.K.mul w w) = F.K.one) (x : R × R × R) :
    pw (GenCubic.R3 F.K c) x (Q * Q * Q) = x := by
  rw [← TwoPower.pwpow, ← TwoPower.pwpow, hfrob, hfrob, hfrob]
  have h3 := hw3
  apply Prod.ext
  · rfl
  · apply Prod.ext
    · show F.K.mul w (F.K.mul w (F.K.mul w x.2.1)) = x.2.1
      rw [show F.K.mul w (F.K.mul w (F.K.mul w x.2.1)) = F.K.mul (F.K.mul w (F.K.mul w w)) x.2.1 by lib_ring, h3, F.K.one_mul]
    · show F.K.mul (F.K.mul w w) (F.K.mul (F.K.mul w w) (F.K.mul (F.K.mul w w) x.2.2)) = x.2.2
      rw [show F.K.mul (F.K.mul w w) (F.K.mul (F.K.mul w w) (F.K.mul (F.K.mul w w) x.2.2))
          = F.K.mul (F.K.mul (F.K.mul w (F.K.mul w w)) (F.K.mul w (F.K.mul w w))) x.2.2 by lib_ring, h3]
      rw [F.K.one_mul, F.K.one_mul]

theorem norm_fixed (hfrob : ∀ x : R × R × R, pw (GenCubic.R3 F.K c) x Q = (x.1, F.K.mul w x.2.1, F.K.mul (F.K.mul w w) x.2.2)) (hw3 : F.K.mul w (F.K.mul w w) = F.K.one) (x : R × R × R) : pw (GenCubic.R3 F.K c) (Nm F c Q x) Q = Nm F c Q x := by
  unfold Nm
  rw [pw_mul, pw_mul, TwoPower.pwpow, TwoPower.pwpow, show Q * Q * Q = Q * Q * Q from rfl, sigma3 F c Q w hfrob hw3]
  lib_ring

theorem norm_pw (x : R × R × R) : pw (GenCubic.R3 F.K c) x (1 + Q + Q * Q) = Nm F c Q x := by
  rw [HasseCM.pw_add', HasseCM.pw_add']
  unfold Nm
  rw [show pw (GenCubic.R3 F.K c) x 1 = x from (GenCubic.R3 F.K c).one_mul x]
  lib_ring

theorem norm_zero (hfrob : ∀ x : R × R × R, pw (GenCubic.R3 F.K c) x Q = (x.1, F.K.mul w x.2.1, F.K.mul (F.K.mul w w) x.2.2)) (hw3 : F.K.mul w (F.K.mul w w) = F.K.one) (Rq : Nat) (hR : Q * Q * Q = (1 + Q + Q * Q) + Rq) (x : R × R × R) (h : Nm F c Q x = (GenCubic.R3 F.K c).zero) : x = (GenCubic.R3 F.K c).zero := by
  rw [← sigma3 F c Q w hfrob hw3 x, hR,
    HasseCM.pw_add', norm_pw, h]
  exact DetChain.zero_mul' (GenCubic.R3 F.K c) _

theorem norm_scalar (hfrob : ∀ x : R × R × R, pw (GenCubic.R3 F.K c) x Q = (x.1, F.K.mul w x.2.1, F.K.mul (F.K.mul w w) x.2.2)) (hw3 : F.K.mul w (F.K.mul w w) = F.K.one) (hω : w ≠ F.K.one) (x : R × R × R) :
    Nm F c Q x = ((Nm F c Q x).1, F.K.zero, F.K.zero) :=
  fixed_scalar F c Q w hfrob hw3 hω _ (norm_fixed F c Q w hfrob hw3 x)

/-- **Inverses in 𝔽_p[∛c].** -/
theorem inverse3 (hfrob : ∀ x : R × R × R, pw (GenCubic.R3 F.K c) x Q = (x.1, F.K.mul w x.2.1, F.K.mul (F.K.mul w w) x.2.2)) (hw3 : F.K.mul w (F.K.mul w w) = F.K.one) (Rq : Nat) (hR : Q * Q * Q = (1 + Q + Q * Q) + Rq) (hω : w ≠ F.K.one) (x : R × R × R) (hx : x ≠ (GenCubic.R3 F.K c).zero) :
    (GenCubic.R3 F.K c).mul x ((GenCubic.R3 F.K c).mul ((GenCubic.R3 F.K c).mul (pw (GenCubic.R3 F.K c) x Q) (pw (GenCubic.R3 F.K c) x (Q * Q))) (F.iv (Nm F c Q x).1, F.K.zero, F.K.zero)) = (GenCubic.R3 F.K c).one := by
  have hs := norm_scalar F c Q w hfrob hw3 hω x
  have hn : (Nm F c Q x).1 ≠ F.K.zero := fun e => hx (norm_zero F c Q w hfrob hw3 Rq hR x (by rw [hs, e]; rfl))
  have hinv := F.iv_mul _ hn
  rw [← (GenCubic.R3 F.K c).mul_assoc, show (GenCubic.R3 F.K c).mul x ((GenCubic.R3 F.K c).mul (pw (GenCubic.R3 F.K c) x Q) (pw (GenCubic.R3 F.K c) x (Q * Q))) = Nm F c Q x from rfl, hs, GenCubic.scal_mul]
  apply Prod.ext
  · show F.K.mul (Nm F c Q x).1 (F.iv (Nm F c Q x).1) = F.K.one; rw [F.K.mul_comm]; exact hinv
  · apply Prod.ext
    · show F.K.mul (Nm F c Q x).1 F.K.zero = F.K.zero; exact DetChain.mul_zero F.K _
    · show F.K.mul (Nm F c Q x).1 F.K.zero = F.K.zero; exact DetChain.mul_zero F.K _

theorem nozd3 (hfrob : ∀ x : R × R × R, pw (GenCubic.R3 F.K c) x Q = (x.1, F.K.mul w x.2.1, F.K.mul (F.K.mul w w) x.2.2)) (hw3 : F.K.mul w (F.K.mul w w) = F.K.one) (Rq : Nat) (hR : Q * Q * Q = (1 + Q + Q * Q) + Rq) (hω : w ≠ F.K.one) : DetChain.NoZD (GenCubic.R3 F.K c) := fun x y e => by
  by_cases hx : x = (GenCubic.R3 F.K c).zero
  · exact Or.inl hx
  · right
    have hi := inverse3 F c Q w hfrob hw3 Rq hR hω x hx
    generalize (GenCubic.R3 F.K c).mul ((GenCubic.R3 F.K c).mul (pw (GenCubic.R3 F.K c) x Q) (pw (GenCubic.R3 F.K c) x (Q * Q))) (F.iv (Nm F c Q x).1, F.K.zero, F.K.zero) = v at hi
    calc y = (GenCubic.R3 F.K c).mul (GenCubic.R3 F.K c).one y := ((GenCubic.R3 F.K c).one_mul y).symm
      _ = (GenCubic.R3 F.K c).mul ((GenCubic.R3 F.K c).mul x v) y := by rw [hi]
      _ = (GenCubic.R3 F.K c).mul v ((GenCubic.R3 F.K c).mul x y) := by lib_ring
      _ = (GenCubic.R3 F.K c).zero := by rw [e]; exact DetChain.mul_zero (GenCubic.R3 F.K c) _

end GenCubicField

#print axioms GenCubicField.omega2_ne
#print axioms GenCubicField.cancel
#print axioms GenCubicField.fixed_scalar
#print axioms GenCubicField.Nm
#print axioms GenCubicField.sigma3
#print axioms GenCubicField.norm_fixed
#print axioms GenCubicField.norm_pw
#print axioms GenCubicField.norm_zero
#print axioms GenCubicField.norm_scalar
#print axioms GenCubicField.inverse3
#print axioms GenCubicField.nozd3

lang/GenCubicInst.lean

lean_all/lang/GenCubicInst.lean

/-
  lang/GenCubicInst — T43, cycle 188: 𝔽_{q³} = F[∛c] as a finite field of the interface, for every F of the interface with
  |F| = q = 2j + 3, given the Frobenius data of GenCubic (q ≡ 1 mod 3, c a non-cube).

    compG, cardG     the q³ triples, listed once each
    ivG, fermatG     inverses (GenCubicField.inverse3), Fermat by FiniteField.unit_pow
    ffG              the FF structure (MkFF)
  Next: discharge the Frobenius data from GenCubic.frob3G at the levels of TowerPow.tower2 (fields of order p^(3·2^L)).

  Lean 4 core only, proofs by hand (lib_nat, lib_int), no axioms.
-/
import lang.GenCubicField
import lang.CubicInst

namespace GenCubicInst

open FTAChain YMChain FiniteRecChain Frobenius

variable {R : Type} [DecidableEq R] (F : FinField.FF R) (c : R) (j : Nat) (w : R)

theorem compG : Complete LibInt.intR (CubicInst.prodL (CubicInst.prodL F.els F.els) F.els) :=
  CubicInst.comp_prodL F.els (CubicInst.prodL F.els F.els) F.comp (CubicInst.comp_prodL F.els F.els F.comp F.comp)

theorem cardG (hj : F.h = j + 1) : sumK LibInt.intR (fun _ => (1 : Int)) (CubicInst.prodL (CubicInst.prodL F.els F.els) F.els) = Int.ofNat (2 * ((4 * j * j * j + 18 * j * j + 27 * j + 12) + 1) + 1) := by
  have h1 : sumK LibInt.intR (fun _ => (1 : Int)) F.els = Int.ofNat (2 * j + 1 + 1 + 1) := by
    rw [F.card, hj]; exact congrArg Int.ofNat (by lib_nat)
  rw [CubicInst.card_prodL F.els (CubicInst.prodL F.els F.els) _ _ h1 (CubicInst.card_prodL F.els F.els _ _ h1 h1), ← CubicInst.cube_form j]
  rfl

def ivG (x : R × R × R) : R × R × R :=
  (GenCubic.R3 F.K c).mul ((GenCubic.R3 F.K c).mul (pw (GenCubic.R3 F.K c) x (2 * j + 1 + 1 + 1)) (pw (GenCubic.R3 F.K c) x ((2 * j + 1 + 1 + 1) * (2 * j + 1 + 1 + 1)))) (F.iv (GenCubicField.Nm F c (2 * j + 1 + 1 + 1) x).1, F.K.zero, F.K.zero)

theorem one_ne_zero3 : (GenCubic.R3 F.K c).one ≠ (GenCubic.R3 F.K c).zero := fun e => by
  have h1 : F.K.one = F.K.zero := congrArg Prod.fst e
  apply F.ne_pm
  rw [h1]
  exact (DetChain.neg_unique F.K _ _ (F.K.zero_add _)).symm

theorem fermatG (hj : F.h = j + 1) (hfrob : ∀ x : R × R × R, Frobenius.pw (GenCubic.R3 F.K c) x (2 * j + 1 + 1 + 1) = (x.1, F.K.mul w x.2.1, F.K.mul (F.K.mul w w) x.2.2)) (hw3 : F.K.mul w (F.K.mul w w) = F.K.one) (Rq : Nat) (hR : (2 * j + 1 + 1 + 1) * (2 * j + 1 + 1 + 1) * (2 * j + 1 + 1 + 1) = (1 + (2 * j + 1 + 1 + 1) + (2 * j + 1 + 1 + 1) * (2 * j + 1 + 1 + 1)) + Rq) (hω : w ≠ F.K.one) (x : R ×   -- (+156 chars; full line in the Zenodo source)
  have hc : ∀ z, FinList.cnt z (CubicInst.prodL (CubicInst.prodL F.els F.els) F.els) = 1 := fun z => Int.ofNat.inj ((FinList.cnt_int z _).symm.trans (compG F z))
  have hl := FinList.len_nzL (GenCubic.R3 F.K c).zero (CubicInst.prodL (CubicInst.prodL F.els F.els) F.els)
  have hlen : ((CubicInst.prodL (CubicInst.prodL F.els F.els) F.els)).length = 2 * ((4 * j * j * j + 18 * j * j + 27 * j + 12) + 1) + 1 := Int.ofNat.inj ((FinList.len_int _).symm.trans (cardG F j hj))
  rw [hc, hlen] at hl
  rw [← Nat.succ.inj hl]
  apply FiniteField.unit_pow (GenCubic.R3 F.K c) (GenCubicField.nozd3 F c (2 * j + 1 + 1 + 1) w hfrob hw3 Rq hR hω) (ivG F c j)
    (fun y hy => by unfold ivG; rw [(GenCubic.R3 F.K c).mul_comm]; exact GenCubicField.inverse3 F c (2 * j + 1 + 1 + 1) w hfrob hw3 Rq hR hω y hy)
  · intro z
    rw [QuadExt.cnt_eq, QuadExt.cnt_nzL]
    by_cases e : z = (GenCubic.R3 F.K c).zero
    · rw [ite_pos' _ _ e, ite_pos' _ _ e]
    · rw [ite_neg' _ _ e, ite_neg' _ _ e, hc]
  · intro z hz e
    rw [QuadExt.cnt_eq, QuadExt.cnt_nzL, ite_pos' _ _ e] at hz
    exact Nat.lt_irrefl 0 hz
  · exact one_ne_zero3 F c
  · exact hx

/-- **F[∛c] as a finite field of the interface, |F[∛c]| = |F|³.** -/
def ffG (hj : F.h = j + 1) (hfrob : ∀ x : R × R × R, Frobenius.pw (GenCubic.R3 F.K c) x (2 * j + 1 + 1 + 1) = (x.1, F.K.mul w x.2.1, F.K.mul (F.K.mul w w) x.2.2)) (hw3 : F.K.mul w (F.K.mul w w) = F.K.one) (Rq : Nat) (hR : (2 * j + 1 + 1 + 1) * (2 * j + 1 + 1 + 1) * (2 * j + 1 + 1 + 1) = (1 + (2 * j + 1 + 1 + 1) + (2 * j + 1 + 1 + 1) * (2 * j + 1 + 1 + 1)) + Rq) (hω : w ≠ F.K.one) : FinField.FF (R   -- (+11 chars; full line in the Zenodo source)
  MkFF.mkFF (GenCubic.R3 F.K c) (GenCubicField.nozd3 F c (2 * j + 1 + 1 + 1) w hfrob hw3 Rq hR hω) (CubicInst.prodL (CubicInst.prodL F.els F.els) F.els) (compG F) (4 * j * j * j + 18 * j * j + 27 * j + 12) (cardG F j hj)
    (fun e => F.ne_pm (congrArg Prod.fst e)) (fermatG F c j w hj hfrob hw3 Rq hR hω)

theorem ffG_h (hj : F.h = j + 1) (hfrob : ∀ x : R × R × R, Frobenius.pw (GenCubic.R3 F.K c) x (2 * j + 1 + 1 + 1) = (x.1, F.K.mul w x.2.1, F.K.mul (F.K.mul w w) x.2.2)) (hw3 : F.K.mul w (F.K.mul w w) = F.K.one) (Rq : Nat) (hR : (2 * j + 1 + 1 + 1) * (2 * j + 1 + 1 + 1) * (2 * j + 1 + 1 + 1) = (1 + (2 * j + 1 + 1 + 1) + (2 * j + 1 + 1 + 1) * (2 * j + 1 + 1 + 1)) + Rq) (hω : w ≠ F.K.one) : (ffG F c   -- (+84 chars; full line in the Zenodo source)

end GenCubicInst

#print axioms GenCubicInst.compG
#print axioms GenCubicInst.cardG
#print axioms GenCubicInst.ivG
#print axioms GenCubicInst.one_ne_zero3
#print axioms GenCubicInst.fermatG
#print axioms GenCubicInst.ffG
#print axioms GenCubicInst.ffG_h

lang/GenericEuler.lean

lean_all/lang/GenericEuler.lean

/-
  lang/GenericEuler — T43, cycle 137: Euler's criterion (hard half) in any finite field, from Fermat and counting alone.

  K a commutative ring without zero divisors whose q = 2h + 1 elements are listed once each, with −1 ≠ 1 and x^(2h) = 1 for
  x ≠ 0. Then x^h = 1 ⇒ x is a square:
    memD, sqs                the distinct squares of the units (built by insertion)
    sq_two                   a distinct list of square roots of one element has length ≤ 2 (y² = y₀² ⇒ y = ±y₀)
    sum_fiber                Σ_{s ∈ S} #{y ∈ U : y² = s} = |U|
    h_le_sqs                 h ≤ |S|
    generic_euler_hard       x^h = 1 ⇒ ∃ y, y² = x       (S ∪ {x} would be h + 1 roots of X^h − 1: RootBound)
  No explicit square roots are needed, so every field built from a list (towers of quadratic extensions, cubic
  extensions) gets Euler's criterion for free.

  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.FinDLog
import lang.FinList

namespace GenericEuler

open FTAChain YMChain FiniteRecChain FinList RootBound FinDLog

variable {R : Type} [DecidableEq R] (K : DetChain.CRing R)

def memD (a : R) : List R → Bool
  | [] => false
  | b :: l => if a = b then true else memD a l

theorem memD_true (a : R) : ∀ l : List R, memD a l = true → memL a l
  | [], h => Bool.noConfusion h
  | b :: l, h => by
    unfold memD at h
    by_cases e : a = b
    · exact Or.inl e
    · rw [ite_neg' _ _ e] at h; exact Or.inr (memD_true a l h)

theorem memD_false (a : R) : ∀ l : List R, memD a l = false → ¬ memL a l
  | [], _, h => h
  | b :: l, h, hm => by
    unfold memD at h
    by_cases e : a = b
    · rw [ite_pos' _ _ e] at h; exact Bool.noConfusion h
    · rw [ite_neg' _ _ e] at h
      cases hm with
      | inl e' => exact e e'
      | inr e' => exact memD_false a l h e'

def sqs : List R → List R
  | [] => []
  | y :: l => if memD (K.mul y y) (sqs l) = true then sqs l else K.mul y y :: sqs l

theorem sqs_dist : ∀ l : List R, Dist (sqs K l)
  | [] => trivial
  | y :: l => by
    unfold sqs
    cases hm : memD (K.mul y y) (sqs K l) with
    | true => rw [ite_pos' _ _ rfl]; exact sqs_dist l
    | false =>
      rw [ite_neg' _ _ (fun e => Bool.noConfusion e)]
      exact ⟨fun s hs e => memD_false _ _ hm (e ▸ hs), sqs_dist l⟩

theorem sqs_cover : ∀ (l : List R) (y : R), memL y l → memL (K.mul y y) (sqs K l)
  | [], _, h => False.elim h
  | b :: l, y, h => by
    unfold sqs
    cases hm : memD (K.mul b b) (sqs K l) with
    | true =>
      rw [ite_pos' _ _ rfl]
      cases h with
      | inl e => rw [e]; exact memD_true _ _ hm
      | inr e => exact sqs_cover l y e
    | false =>
      rw [ite_neg' _ _ (fun e => Bool.noConfusion e)]
      cases h with
      | inl e => rw [e]; exact Or.inl rfl
      | inr e => exact Or.inr (sqs_cover l y e)

theorem sqs_from : ∀ (l : List R) (s : R), memL s (sqs K l) → ∃ y, memL y l ∧ K.mul y y = s
  | [], _, h => False.elim h
  | b :: l, s, h => by
    unfold sqs at h
    cases hm : memD (K.mul b b) (sqs K l) with
    | true =>
      rw [hm, ite_pos' _ _ rfl] at h
      match sqs_from l s h with | ⟨y, hy, e⟩ => exact ⟨y, Or.inr hy, e⟩
    | false =>
      rw [hm, ite_neg' _ _ (fun e => Bool.noConfusion e)] at h
      cases h with
      | inl e => exact ⟨b, Or.inl rfl, e.symm⟩
      | inr e => match sqs_from l s e with | ⟨y, hy, e'⟩ => exact ⟨y, Or.inr hy, e'⟩

/-- number of y in l with y² = s. -/
def fib (s : R) : List R → Nat
  | [] => 0
  | y :: l => (if K.mul y y = s then 1 else 0) + fib s l

omit [DecidableEq R] in
theorem sq_eq (hK : DetChain.NoZD K) (a b : R) (h : K.mul a a = K.mul b b) : a = b ∨ a = K.neg b := by
  have e : K.mul (K.add a (K.neg b)) (K.add a b) = K.zero := by
    rw [show K.mul (K.add a (K.neg b)) (K.add a b) = K.add (K.mul a a) (K.neg (K.mul b b)) by lib_ring, h, K.add_neg]
  cases hK _ _ e with
  | inl h1 =>
    left
    have h2 : K.add (K.add a (K.neg b)) b = K.add K.zero b := congrArg (fun z => K.add z b) h1
    rw [show K.add (K.add a (K.neg b)) b = a by lib_ring, K.zero_add] at h2
    exact h2
  | inr h1 =>
    right
    exact DetChain.neg_unique K _ _ h1

def flt (s : R) : List R → List R
  | [] => []
  | y :: l => if K.mul y y = s then y :: flt s l else flt s l

theorem fib_len (s : R) : ∀ l : List R, fib K s l = (flt K s l).length
  | [] => rfl
  | y :: l => by
    unfold fib flt
    by_cases e : K.mul y y = s
    · rw [ite_pos' _ _ e, ite_pos' _ _ e, fib_len s l]; show 1 + _ = _ + 1; lib_nat
    · rw [ite_neg' _ _ e, ite_neg' _ _ e, fib_len s l, Nat.zero_add]

theorem flt_mem (s : R) : ∀ (l : List R) (y : R), memL y (flt K s l) → memL y l ∧ K.mul y y = s
  | [], _, h => False.elim h
  | b :: l, y, h => by
    unfold flt at h
    by_cases e : K.mul b b = s
    · rw [ite_pos' _ _ e] at h
      cases h with
      | inl e' => exact ⟨Or.inl e', by rw [e']; exact e⟩
      | inr e' => have := flt_mem s l y e'; exact ⟨Or.inr this.1, this.2⟩
    · rw [ite_neg' _ _ e] at h
      have := flt_mem s l y h; exact ⟨Or.inr this.1, this.2⟩

theorem flt_dist (s : R) : ∀ l : List R, Dist l → Dist (flt K s l)
  | [], _ => trivial
  | b :: l, hd => by
    unfold flt
    by_cases e : K.mul b b = s
    · rw [ite_pos' _ _ e]; exact ⟨fun t ht => hd.1 t (flt_mem K s l t ht).1, flt_dist s l hd.2⟩
    · rw [ite_neg' _ _ e]; exact flt_dist s l hd.2

omit [DecidableEq R] in
/-- **At most two square roots.** -/
theorem len_le2 (hK : DetChain.NoZD K) (s : R) :
    ∀ L : List R, Dist L → (∀ y, memL y L → K.mul y y = s) → L.length ≤ 2
  | [], _, _ => Nat.zero_le 2
  | [_], _, _ => Nat.le_succ 1
  | [_, _], _, _ => Nat.le_refl 2
  | a :: b :: c :: rest, hd, hs => by
    exfalso
    have hab := sq_eq K hK b a ((hs b (Or.inr (Or.inl rfl))).trans (hs a (Or.inl rfl)).symm)
    have hac := sq_eq K hK c a ((hs c (Or.inr (Or.inr (Or.inl rfl)))).trans (hs a (Or.inl rfl)).symm)
    have nba : b ≠ a := hd.1 b (Or.inl rfl)
    have nca : c ≠ a := hd.1 c (Or.inr (Or.inl rfl))
    have ncb : c ≠ b := hd.2.1 c (Or.inl rfl)
    cases hab with
    | inl e => exact nba e
    | inr eb =>
      cases hac with
      | inl e => exact nca e
      | inr ec => exact ncb (ec.trans eb.symm)

theorem fib_le2 (hK : DetChain.NoZD K) (s : R) (l : List R) (hd : Dist l) :
    fib K s l ≤ 2 := by
  rw [fib_len]
  exact len_le2 K hK s _ (flt_dist K s l hd) (fun y hy => (flt_mem K s l y hy).2)

theorem ind_cnt (y : R) : ∀ S : List R, sumN (fun s => if K.mul y y = s then 1 else 0) S = cnt (K.mul y y) S
  | [] => rfl
  | a :: S => by
    show (if K.mul y y = a then 1 else 0) + _ = (if a = K.mul y y then 1 else 0) + _
    rw [ind_cnt y S]
    by_cases e : K.mul y y = a
    · rw [ite_pos' _ _ e, ite_pos' _ _ e.symm]
    · rw [ite_neg' _ _ e, ite_neg' _ _ (fun e' => e e'.symm)]

/-- **Σ_{s ∈ S} #{y ∈ U : y² = s} = |U|.** -/
theorem sum_fiber (S : List R) (hS : Dist S) : ∀ U : List R, (∀ y, memL y U → memL (K.mul y y) S) →
    sumN (fun s => fib K s U) S = U.length
  | [], _ => by
    show sumN (fun _ => 0) S = 0
    have : ∀ l : List R, sumN (fun _ => (0 : Nat)) l = 0 := by
      intro l; induction l with
      | nil => rfl
      | cons _ l ih => show 0 + _ = 0; rw [ih]
    exact this S
  | y :: U, hU => by
    show sumN (fun s => (if K.mul y y = s then 1 else 0) + fib K s U) S = U.length + 1
    rw [sumN_add, ind_cnt, sum_fiber S hS U (fun z hz => hU z (Or.inr hz))]
    have h1 : cnt (K.mul y y) S = 1 :=
      Nat.le_antisymm (cnt_le_one_of_dist _ S hS) (cnt_pos _ S (hU y (Or.inl rfl)))
    rw [h1, Nat.add_comm]

omit [DecidableEq R] in
theorem sumN_const2 : ∀ S : List R, sumN (fun _ => (2 : Nat)) S = 2 * S.length
  | [] => rfl
  | _ :: S => by show 2 + sumN _ S = 2 * (S.length + 1); rw [sumN_const2 S]; lib_nat

/-- **Euler's criterion, hard half, in any finite field.** -/
theorem generic_euler_hard (hK : DetChain.NoZD K) (els : List R) (comp : Complete LibInt.intR els) (h : Nat) (hh : 1 ≤ h)
    (card : sumK LibInt.intR (fun _ => (1 : Int)) els = Int.ofNat (2 * h + 1))
    (hferm : ∀ x, x ≠ K.zero → Frobenius.pw K x (2 * h) = K.one) (hne1 : K.neg K.one ≠ K.one)
    (x : R) (hx : Frobenius.pw K x h = K.one) : ∃ y, K.mul y y = x := by
  have hlen : els.length = 2 * h + 1 := Int.ofNat.inj ((len_int els).symm.trans card)
  have hc1 : cnt K.zero els = 1 := Int.ofNat.inj ((cnt_int K.zero els).symm.trans (comp K.zero))
  have hl := len_nzL K.zero els
  rw [hc1, hlen] at hl
  have hU : (nzL K.zero els).length = 2 * h := Nat.succ.inj hl
  have hUd := dist_nzL K.zero els (dist_of_complete els comp)
  have hS := sqs_dist K (nzL K.zero els)
  have one0 : K.one ≠ K.zero := fun e => hne1 (by
    rw [e]; exact (DetChain.neg_unique K _ _ (K.zero_add K.zero)).symm)
  have hcount : 2 * h ≤ 2 * (sqs K (nzL K.zero els)).length := by
    rw [← hU, ← sum_fiber K _ hS _ (sqs_cover K _), ← sumN_const2]
    exact sumN_le _ _ (fun s => fib_le2 K hK s _ hUd) _
  have hle : h ≤ (sqs K (nzL K.zero els)).length := Nat.le_of_mul_le_mul_left hcount (Nat.zero_lt_succ 1)
  have hroots : ∀ s, memL s (sqs K (nzL K.zero els)) → Frobenius.pw K s h = K.one := fun s hs => by
    match sqs_from K _ s hs with
    | ⟨y, hy, e⟩ =>
      rw [← e, Frobenius.pw_mul, ← HasseCM.pw_add', show h + h = 2 * h by lib_nat]
      exact hferm y (mem_nzL K.zero y els hy).2
  cases hm : memD x (sqs K (nzL K.zero els)) with
  | true =>
    match sqs_from K _ x (memD_true _ _ hm) with
    | ⟨y, _, e⟩ => exact ⟨y, e⟩
  | false =>
    exfalso
    cases h with
    | zero => exact Nat.not_succ_le_zero 0 hh
    | succ k =>
      have hb := root_bound K hK (k + 1) K.one (EulerHard.binomL.zerosThen K k) one0 (EulerHard.len_zerosThen _ k)
        (x :: sqs K (nzL K.zero els)) ⟨fun s hs e => memD_false _ _ hm (e ▸ hs), hS⟩
        (fun r hr => by
          show evH K r (EulerHard.binomL K k) = _
          rw [EulerHard.ev_binom]
          cases hr with
          | inl e => rw [e, hx, K.add_neg]
          | inr e => rw [hroots r e, K.add_neg])
      exact Nat.not_succ_le_self _ (Nat.le_trans (Nat.succ_le_succ hle) hb)

end GenericEuler

#print axioms GenericEuler.memD_true
#print axioms GenericEuler.memD_false
#print axioms GenericEuler.sqs_dist
#print axioms GenericEuler.sqs_cover
#print axioms GenericEuler.sqs_from
#print axioms GenericEuler.sq_eq
#print axioms GenericEuler.fib_len
#print axioms GenericEuler.flt_mem
#print axioms GenericEuler.flt_dist
#print axioms GenericEuler.len_le2
#print axioms GenericEuler.fib_le2
#print axioms GenericEuler.ind_cnt
#print axioms GenericEuler.sum_fiber
#print axioms GenericEuler.sumN_const2
#print axioms GenericEuler.generic_euler_hard

lang/Genus2.lean

lean_all/lang/Genus2.lean

/-
  lang/Genus2 — T48, cycle 723: **the genus-2 curve y² = x⁵ − x splits into two twists of 256a** (OPEN 256a-split, step (ii)).
  In 𝔽_p with i² = −1, s² = −i, 2h = 1, σ = 2si, F_σ(X) = (2X² − σ²)(2X − 2σ):
    d1, c5 … c7  polynomial identities modulo i² + 1, s² + i (generated certificates)
    g2_split    Σ_x χ(x⁵ − x) = Σ_X χ(F_σ(X)) + Σ_X χ(F_{−σ}(X))
  (involution (x, y) ↦ (i/x, sy/x³), X = x + i/x: x⁴F_σ(X) = (2(x − si))²(x⁵ − x); FibSum for the fibres; boundary x = si, X = σ).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.FibSum
import lang.Q256

namespace Genus2

open FTAChain ZMod YMChain FiniteRecChain DetChain QChar SqCount

variable (m : Nat)

theorem d1 (x i s : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero) :
    ((K m).mul x ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add ((K m).mul x x) i) ((K m).add ((K m).mul x x) i))) ((K m).neg ((K m).mul ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)) ((K m).mul x x)))) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).add ((K m).mul x x) i)) ((K m).neg ((K m).mul ((K m).add (K m).one (K m  -- (+363 chars; full line in the Zenodo source)
  have e : ((K m).mul x ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add ((K m).mul x x) i) ((K m).add ((K m).mul x x) i))) ((K m).neg ((K m).mul ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)) ((K m).mul x x)))) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).add ((K m).mul x x) i)) ((K m).neg ((K m).mul ((K m).add (K m).o  -- (+3329 chars; full line in the Zenodo source)
  rw [e, hi, hs]; lib_ring

theorem c5 (i s : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero) :
    ((K m).mul ((K m).mul s i) ((K m).mul ((K m).add s s) i)) = ((K m).add i i) := by
  have e : ((K m).mul ((K m).mul s i) ((K m).mul ((K m).add s s) i)) = ((K m).add ((K m).add ((K m).add i i) ((K m).mul ((K m).add ((K m).mul i i) (K m).one) ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul s s)))) ((K m).mul ((K m).add ((K m).mul s s) i) ((K m).neg ((K m).add (K m).one (K m).one)))) := by lib_ring
  rw [e, hi, hs]; lib_ring

theorem c5b (i s : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero) :
    ((K m).mul ((K m).mul s i) ((K m).mul s i)) = i := by
  have e : ((K m).mul ((K m).mul s i) ((K m).mul s i)) = ((K m).add ((K m).add i ((K m).mul ((K m).add ((K m).mul i i) (K m).one) ((K m).mul s s))) ((K m).mul ((K m).add ((K m).mul s s) i) ((K m).neg (K m).one))) := by lib_ring
  rw [e, hi, hs]; lib_ring

theorem c6 (i s : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero) :
    ((K m).add ((K m).mul ((K m).mul ((K m).mul ((K m).mul ((K m).mul s i) ((K m).mul s i)) ((K m).mul s i)) ((K m).mul s i)) ((K m).mul s i)) ((K m).neg ((K m).mul s i))) = ((K m).neg ((K m).mul ((K m).add s s) i)) := by
  have e : ((K m).add ((K m).mul ((K m).mul ((K m).mul ((K m).mul ((K m).mul s i) ((K m).mul s i)) ((K m).mul s i)) ((K m).mul s i)) ((K m).mul s i)) ((K m).neg ((K m).mul s i))) = ((K m).add ((K m).add ((K m).neg ((K m).mul ((K m).add s s) i)) ((K m).mul ((K m).add ((K m).mul i i) (K m).one) ((K m).add ((K m).add ((K m).neg ((K m).mul ((K m).mul s s) s)) ((K m).neg ((K m).mul ((K m).mul ((K m).mu  -- (+261 chars; full line in the Zenodo source)
  rw [e, hi, hs]; lib_ring

theorem c7 (i s X : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero) :
    ((K m).mul ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul X X)) ((K m).neg ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)))) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) X) ((K m).neg ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add s s) i))))) ((K m).add ((K m).mul X X) ((K m).neg ((K m).add ((K m).add i i) ((K m).a  -- (+503 chars; full line in the Zenodo source)
  have e : ((K m).mul ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul X X)) ((K m).neg ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)))) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) X) ((K m).neg ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add s s) i))))) ((K m).add ((K m).mul X X) ((K m).neg ((K m).add ((K m).add i i) (  -- (+12083 chars; full line in the Zenodo source)
  rw [e, hi, hs]; lib_ring


theorem addI (a b : Int) : LibInt.intR.add a b = a + b := rfl
theorem mulI (a b : Int) : LibInt.intR.mul a b = a * b := rfl
theorem negI (a : Int) : LibInt.intR.neg a = -a := rfl
theorem oneI : LibInt.intR.one = (1 : Int) := rfl
theorem zeroI : LibInt.intR.zero = (0 : Int) := rfl

theorem fin3 (a b c : Int) : LibInt.intR.add (LibInt.intR.add a (LibInt.intR.add b (-c))) c = a + b := by
  rw [addI, addI, addI]; lib_int

theorem sum_delta_L (z : Zp (2 * m + 1 + 1)) : sumK LibInt.intR (fun X => delta LibInt.intR X z) (ConicSum.L m) = 1 :=
  CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR z

theorem chi_sq_mul (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (y b : Zp (2 * m + 1 + 1)) (hy : y ≠ (K m).zero) : chi m ((K m).mul ((K m).mul y y) b) = chi m b := by
  rw [chi_mul m hpr, QChar.chi_sq m hpr y hy]; lib_int

theorem nz (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h : Zp (2 * m + 1 + 1)) (hh : (K m).add h h = (K m).one) (i s : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero) :
    i ≠ (K m).zero ∧ s ≠ (K m).zero ∧ ((K m).mul s i) ≠ (K m).zero ∧ ((K m).add (K m).one (K m).one) ≠ (K m).zero := by
  have one_nz := PowerSum.one_ne_zero' (2 * m + 1)
  have hi0 : i ≠ (K m).zero := fun e => one_nz (by rw [← hi, e]; lib_ring)
  have hs0 : s ≠ (K m).zero := fun e => hi0 (by rw [← hs, e]; lib_ring)
  refine ⟨hi0, hs0, fun e => ?_, fun e => one_nz (by rw [← hh, show (K m).add h h = (K m).mul h ((K m).add (K m).one (K m).one) by lib_ring, e]; lib_ring)⟩
  rcases G4Gen.mul_eq_zero m hpr s i e with e1 | e1
  · exact hs0 e1
  · exact hi0 e1

/-- For x ∉ {0, si}: χ(F_σ(x + i/x)) = χ(x⁵ − x). -/
theorem pt (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h : Zp (2 * m + 1 + 1)) (hh : (K m).add h h = (K m).one) (i s : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero)
    (x : Zp (2 * m + 1 + 1)) (hx : x ≠ (K m).zero) (hxs : x ≠ ((K m).mul s i)) : chi m ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add x ((K m).mul i (CharSumGen.iv (2 * m) x))) ((K m).add x ((K m).mul i (CharSumGen.iv (2 * m) x))))) ((K m).neg ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)))) ((K m).add ((K m).mul ((K m).add (K m).one  -- (+252 chars; full line in the Zenodo source)
  obtain ⟨_, _, _, h2⟩ := nz m hpr h hh i s hi hs
  have hw := CharSumGen.iv_mul (2 * m) hpr x hx
  generalize CharSumGen.iv (2 * m) x = w at hw ⊢
  have hZ : (K m).mul x ((K m).add x ((K m).mul i w)) = ((K m).add ((K m).mul x x) i) := by
    calc _ = (K m).add ((K m).mul x x) ((K m).mul i ((K m).mul w x)) := by lib_ring
      _ = _ := by rw [hw]; lib_ring
  have e : (K m).mul ((K m).mul ((K m).mul x x) ((K m).mul x x)) (((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add x ((K m).mul i w)) ((K m).add x ((K m).mul i w)))) ((K m).neg ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)))) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).add x ((K m).mul i w))) ((K m).neg ((K m).mul ((K   -- (+62 chars; full line in the Zenodo source)
      = (K m).mul ((K m).mul ((K m).add ((K m).add x ((K m).neg ((K m).mul s i))) ((K m).add x ((K m).neg ((K m).mul s i)))) ((K m).add ((K m).add x ((K m).neg ((K m).mul s i))) ((K m).add x ((K m).neg ((K m).mul s i))))) ((K m).add ((K m).mul ((K m).mul ((K m).mul ((K m).mul x x) x) x) x) ((K m).neg x)) := by
    calc _ = (K m).mul x ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).mul x ((K m).add x ((K m).mul i w))) ((K m).mul x ((K m).add x ((K m).mul i w))))) ((K m).neg ((K m).mul ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)) ((K m).mul x x))))
          ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul x ((K m).add x ((K m).mul i w)))) ((K m).neg ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).mul ((K m).add s s) i) x))))) := by lib_ring
      _ = _ := by rw [hZ]; exact d1 m x i s hi hs
  have hxx : (K m).mul x x ≠ (K m).zero := fun e0 => by
    rcases G4Gen.mul_eq_zero m hpr x x e0 with e1 | e1 <;> exact hx e1
  have hd : (K m).add ((K m).add x ((K m).neg ((K m).mul s i))) ((K m).add x ((K m).neg ((K m).mul s i))) ≠ (K m).zero := fun e0 => by
    have : (K m).mul ((K m).add (K m).one (K m).one) ((K m).add x ((K m).neg ((K m).mul s i))) = (K m).zero := by rw [← e0]; lib_ring
    rcases G4Gen.mul_eq_zero m hpr _ _ this with e1 | e1
    · exact h2 e1
    · exact hxs (by rw [show x = (K m).add ((K m).add x ((K m).neg ((K m).mul s i))) ((K m).mul s i) by lib_ring, e1]; lib_ring)
  have := congrArg (chi m) e
  rw [chi_sq_mul m hpr _ _ hxx, chi_sq_mul m hpr _ _ hd] at this
  exact this

/-- X(si) = σ, so F_σ vanishes there. -/
theorem at_si (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h : Zp (2 * m + 1 + 1)) (hh : (K m).add h h = (K m).one) (i s : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero) :
    ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add ((K m).mul s i) ((K m).mul i (CharSumGen.iv (2 * m) ((K m).mul s i)))) ((K m).add ((K m).mul s i) ((K m).mul i (CharSumGen.iv (2 * m) ((K m).mul s i)))))) ((K m).neg ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)))) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).add ((K   -- (+174 chars; full line in the Zenodo source)
  obtain ⟨_, _, hsi, _⟩ := nz m hpr h hh i s hi hs
  have hw := CharSumGen.iv_mul (2 * m) hpr _ hsi
  generalize CharSumGen.iv (2 * m) ((K m).mul s i) = w at hw ⊢
  have hX : (K m).add ((K m).mul s i) ((K m).mul i w) = ((K m).mul ((K m).add s s) i) := by
    have e0 : (K m).mul ((K m).mul s i) ((K m).add ((K m).add ((K m).mul s i) ((K m).mul i w)) ((K m).neg ((K m).mul ((K m).add s s) i))) = (K m).zero := by
      calc _ = (K m).add ((K m).add ((K m).mul ((K m).mul s i) ((K m).mul s i)) ((K m).mul i ((K m).mul w ((K m).mul s i)))) ((K m).neg ((K m).mul ((K m).mul s i) ((K m).mul ((K m).add s s) i))) := by lib_ring
        _ = _ := by rw [hw, c5b m i s hi hs, c5 m i s hi hs]; lib_ring
    rcases G4Gen.mul_eq_zero m hpr _ _ e0 with e1 | e1
    · exact absurd e1 hsi
    · calc _ = (K m).add ((K m).add ((K m).add ((K m).mul s i) ((K m).mul i w)) ((K m).neg ((K m).mul ((K m).add s s) i))) ((K m).mul ((K m).add s s) i) := by lib_ring
        _ = _ := by rw [e1]; lib_ring
  rw [hX]; lib_ring

/-- **y² = x⁵ − x splits: Σχ(x⁵ − x) = Σχ(F_σ) + Σχ(F_{−σ}).** -/
theorem g2_split (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h : Zp (2 * m + 1 + 1)) (hh : (K m).add h h = (K m).one) (i s : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero) :
    sumK LibInt.intR (fun x => chi m ((K m).add ((K m).mul ((K m).mul ((K m).mul ((K m).mul x x) x) x) x) ((K m).neg x))) (ConicSum.L m)
      = sumK LibInt.intR (fun X => chi m ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul X X)) ((K m).neg ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)))) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) X) ((K m).neg ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add s s) i)))))) (ConicSum.L m) + sumK LibInt.intR (fun X => ch  -- (+373 chars; full line in the Zenodo source)
  obtain ⟨hi0, hs0, hsi, h2⟩ := nz m hpr h hh i s hi hs
  have hcomp : Complete LibInt.intR (ConicSum.L m) := CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR
  have hm1 : chi m ((K m).neg (K m).one) = 1 := by
    rw [show (K m).neg (K m).one = (K m).mul i i by rw [show (K m).mul i i = (K m).add ((K m).add ((K m).mul i i) (K m).one) ((K m).neg (K m).one) by lib_ring, hi]; lib_ring]
    exact QChar.chi_sq m hpr i hi0
  have hsg : ((K m).mul ((K m).add s s) i) ≠ (K m).zero := fun e0 => hsi (by
    have : (K m).mul ((K m).add (K m).one (K m).one) ((K m).mul s i) = (K m).zero := by rw [← e0]; lib_ring
    rcases G4Gen.mul_eq_zero m hpr _ _ this with e1 | e1
    · exact absurd e1 h2
    · exact e1)
  -- step 1: pointwise split
  have p1 : ∀ x, chi m ((K m).add ((K m).mul ((K m).mul ((K m).mul ((K m).mul x x) x) x) x) ((K m).neg x)) = LibInt.intR.add (if x = (K m).zero then 0 else chi m ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add x ((K m).mul i (CharSumGen.iv (2 * m) x))) ((K m).add x ((K m).mul i (CharSumGen.iv (2 * m) x))))) ((K m).neg ((K m).mul ((K m).mul ((K m).add s s) i)   -- (+230 chars; full line in the Zenodo source)
      (LibInt.intR.mul (delta LibInt.intR x ((K m).mul s i)) (chi m ((K m).add ((K m).mul ((K m).mul ((K m).mul ((K m).mul x x) x) x) x) ((K m).neg x)))) := fun x => by
    by_cases hx : x = (K m).zero
    · rw [ite_pos' _ _ hx, hx, show ((K m).add ((K m).mul ((K m).mul ((K m).mul ((K m).mul (K m).zero (K m).zero) (K m).zero) (K m).zero) (K m).zero) ((K m).neg (K m).zero)) = (K m).zero by lib_ring] ; rw [chi_zero, addI, mulI]; lib_int
    · rw [ite_neg' _ _ hx]
      by_cases hxs : x = ((K m).mul s i)
      · rw [hxs, at_si m hpr h hh i s hi hs, chi_zero]
        unfold delta; rw [ite_pos' _ _ rfl, addI, mulI, oneI]; lib_int
      · rw [pt m hpr h hh i s hi hs x hx hxs]
        unfold delta; rw [ite_neg' _ _ hxs, addI, mulI, zeroI]; lib_int
  rw [sumK_ext LibInt.intR _ _ p1, sumK_add, pick LibInt.intR _ hcomp (fun x => chi m ((K m).add ((K m).mul ((K m).mul ((K m).mul ((K m).mul x x) x) x) x) ((K m).neg x))),
    FibSum.fib_sum m hpr h i hh hi0 (fun X => chi m ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul X X)) ((K m).neg ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)))) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) X) ((K m).neg ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add s s) i))))))]
  -- step 2: Σ g(1 + χ(D)) = Σ g + Σ g χ(D)
  have p2 : ∀ X, chi m ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul X X)) ((K m).neg ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)))) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) X) ((K m).neg ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add s s) i))))) * (1 + chi m ((K m).add ((K m).mul X X) ((K m).neg ((K m).add ((K  -- (+32 chars; full line in the Zenodo source)
      = LibInt.intR.add (chi m ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul X X)) ((K m).neg ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)))) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) X) ((K m).neg ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add s s) i)))))) (LibInt.intR.add (chi m ((K m).mul ((K m).add ((K m).mul  -- (+316 chars; full line in the Zenodo source)
          (LibInt.intR.neg (LibInt.intR.mul (delta LibInt.intR X ((K m).mul ((K m).add s s) i)) (chi m ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i))) ((K m).neg ((K m).mul ((K m).neg ((K m).mul ((K m).add s s) i)) ((K m).neg ((K m).mul ((K m).add s s) i))))) ((K m).add ((K m).mul ((K m).add (K m).one (K m).o  -- (+153 chars; full line in the Zenodo source)
    have c := c7 m i s X hi hs
    by_cases hX : X = ((K m).mul ((K m).add s s) i)
    · rw [hX, show ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i))) ((K m).neg ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)))) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add s s) i)) ((K m).neg ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).a  -- (+50 chars; full line in the Zenodo source)
      unfold delta; rw [ite_pos' _ _ rfl, addI, addI, negI, mulI, oneI]; lib_int
    · have hXm : (K m).add X ((K m).neg ((K m).mul ((K m).add s s) i)) ≠ (K m).zero := fun e0 => hX (by
        rw [show X = (K m).add ((K m).add X ((K m).neg ((K m).mul ((K m).add s s) i))) ((K m).mul ((K m).add s s) i) by lib_ring, e0]; lib_ring)
      have e1 : chi m ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul X X)) ((K m).neg ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)))) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) X) ((K m).neg ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add s s) i))))) * chi m ((K m).add ((K m).mul X X) ((K m).neg ((K m).add ((K m).ad  -- (+387 chars; full line in the Zenodo source)
        rw [← chi_mul m hpr, c, chi_sq_mul m hpr _ _ hXm]
      unfold delta; rw [ite_neg' _ _ hX, addI, addI, negI, mulI, zeroI]
      rw [show chi m ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul X X)) ((K m).neg ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)))) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) X) ((K m).neg ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add s s) i))))) * (1 + chi m ((K m).add ((K m).mul X X) ((K m).neg ((K m).add ((K m  -- (+30 chars; full line in the Zenodo source)
        = chi m ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul X X)) ((K m).neg ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)))) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) X) ((K m).neg ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add s s) i))))) + chi m ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one)  -- (+355 chars; full line in the Zenodo source)
      lib_int
  rw [sumK_ext LibInt.intR _ _ p2, sumK_add, sumK_add]
  -- step 3: the δ-sum and the boundary values
  have p3 : sumK LibInt.intR (fun X => LibInt.intR.neg (LibInt.intR.mul (delta LibInt.intR X ((K m).mul ((K m).add s s) i)) (chi m ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i))) ((K m).neg ((K m).mul ((K m).neg ((K m).mul ((K m).add s s) i)) ((K m).neg ((K m).mul ((K m).add s s) i))))) ((K m).add ((K m).mul   -- (+180 chars; full line in the Zenodo source)
      = -(chi m ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i))) ((K m).neg ((K m).mul ((K m).neg ((K m).mul ((K m).add s s) i)) ((K m).neg ((K m).mul ((K m).add s s) i))))) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add s s) i)) ((K m).neg ((K m).mul ((K m).add (K m).one (K m).one  -- (+54 chars; full line in the Zenodo source)
    rw [sumK_ext LibInt.intR _ (fun X => LibInt.intR.mul (LibInt.intR.neg (chi m ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i))) ((K m).neg ((K m).mul ((K m).neg ((K m).mul ((K m).add s s) i)) ((K m).neg ((K m).mul ((K m).add s s) i))))) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).  -- (+180 chars; full line in the Zenodo source)
      rw [negI, mulI, mulI, negI]; lib_int), ← mul_sumK, sum_delta_L m, mulI, negI]; lib_int
  have p4 : chi m ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i))) ((K m).neg ((K m).mul ((K m).neg ((K m).mul ((K m).add s s) i)) ((K m).neg ((K m).mul ((K m).add s s) i))))) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add s s) i)) ((K m).neg ((K m).mul ((K m).add (K m).one (K m).o  -- (+231 chars; full line in the Zenodo source)
    have hss : (K m).add ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i) ≠ (K m).zero := fun e0 => hsg (by
      have : (K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add s s) i) = (K m).zero := by rw [← e0]; lib_ring
      rcases G4Gen.mul_eq_zero m hpr _ _ this with e1 | e1
      · exact absurd e1 h2
      · exact e1)
    rw [show ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i))) ((K m).neg ((K m).mul ((K m).neg ((K m).mul ((K m).add s s) i)) ((K m).neg ((K m).mul ((K m).add s s) i))))) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add s s) i)) ((K m).neg ((K m).mul ((K m).add (K m).one (K m).one) (  -- (+255 chars; full line in the Zenodo source)
      c6 m i s hi hs, show (K m).neg ((K m).mul ((K m).add s s) i) = (K m).mul ((K m).neg (K m).one) ((K m).mul ((K m).add s s) i) by lib_ring, chi_mul m hpr ((K m).neg (K m).one), hm1, chi_sq_mul m hpr _ _ hss]
    lib_int

  rw [p3, p4]
  exact fin3 _ _ _

end Genus2
#print axioms Genus2.d1
#print axioms Genus2.c5
#print axioms Genus2.c5b
#print axioms Genus2.c6
#print axioms Genus2.c7
#print axioms Genus2.addI
#print axioms Genus2.mulI
#print axioms Genus2.negI
#print axioms Genus2.oneI
#print axioms Genus2.zeroI
#print axioms Genus2.fin3
#print axioms Genus2.sum_delta_L
#print axioms Genus2.chi_sq_mul
#print axioms Genus2.nz
#print axioms Genus2.pt
#print axioms Genus2.at_si
#print axioms Genus2.g2_split

lang/GrFrobPer.lean

lean_all/lang/GrFrobPer.lean

/-
  lang/GrFrobPer — T49 (residue fields without extensions): **in 𝔽_p[C_n] every y satisfies y^(p^e) = y when
  p^e ≡ 1 (mod n)**: y = Σ_a y(a) σ^a, the Frobenius is additive, (s σ^a)^(p^e) = s^(p^e) σ^(p^e a) = s σ^a.
    zp_fermat_pow     s^(p^e) = s in 𝔽_p
    nsm_q             ((n M + 1)·a = a in ℤ/n
    gr_per            y^(p^e) = y
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.GroupRingFrob
import lang.TwistPhi

namespace GrFrobPer

open FTAChain ZMod YMChain FiniteRecChain DetChain GroupRing Frobenius

theorem zp_fermat_pow (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) :
    ∀ e (s : Zp (k' + 1)), pw (ring (k' + 1)) s ((k' + 1 + 1) ^ e) = s
  | 0, s => (ring (k' + 1)).one_mul s
  | e + 1, s => by
    show pw (ring (k' + 1)) s ((k' + 1 + 1) ^ e * (k' + 1 + 1)) = s
    rw [← TwoPower.pwpow, zp_fermat_pow k' hq e s, ZMod.fermat k' hq s]

theorem nsm_q (m M : Nat) (a : Zp m) : nsm (ring m) ((m + 1) * M + 1) a = a := by
  have h1 : nsm (ring m) ((m + 1) * M + 1) (ring m).one = (ring m).one := Subtype.ext (by
    rw [nsm_one_val, show (m + 1) * M + 1 = 1 + M * (m + 1) by lib_nat, md_add_mul]
    rfl)
  rw [nsm_eq_mul, h1, (ring m).one_mul]

/-- **y^(p^e) = y in 𝔽_p[C_n].** -/
theorem gr_per (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (m e M : Nat)
    (hqe : (k' + 1 + 1) ^ e = (m + 1) * M + 1) (y : V (Zp (k' + 1)) (m + 1)) :
    pw (gring (ring (k' + 1)) m) y ((k' + 1 + 1) ^ e) = y := by
  have hd := GroupRingFrob.decomp (ring (k' + 1)) m y
  have hp : nsm (ring (k' + 1)) (k' + 1 + 1) (ring (k' + 1)).one = (ring (k' + 1)).zero := ZMod.char_p (k' + 1)
  calc pw (gring (ring (k' + 1)) m) y ((k' + 1 + 1) ^ e)
      = pw (gring (ring (k' + 1)) m)
          (sumK (gring (ring (k' + 1)) m) (fun a => GroupRingFrob.mono (ring (k' + 1)) m a (gt (ring (k' + 1)) m y a)) (GroupRing.L m))
          ((k' + 1 + 1) ^ e) := by rw [← hd]
    _ = sumK (gring (ring (k' + 1)) m)
          (fun a => pw (gring (ring (k' + 1)) m) (GroupRingFrob.mono (ring (k' + 1)) m a (gt (ring (k' + 1)) m y a)) ((k' + 1 + 1) ^ e))
          (GroupRing.L m) :=
        TwistPhi.frob_pk_sum (gring (ring (k' + 1)) m) k' hq (GroupRingFrob.char_G (ring (k' + 1)) m _ hp) _ e (GroupRing.L m)
    _ = sumK (gring (ring (k' + 1)) m) (fun a => GroupRingFrob.mono (ring (k' + 1)) m a (gt (ring (k' + 1)) m y a)) (GroupRing.L m) :=
        sumK_ext (gring (ring (k' + 1)) m) _ _ (fun a => by
          rw [GroupRingFrob.mono_pow, zp_fermat_pow k' hq e, hqe, nsm_q]) _
    _ = y := hd.symm

end GrFrobPer

#print axioms GrFrobPer.zp_fermat_pow
#print axioms GrFrobPer.nsm_q
#print axioms GrFrobPer.gr_per

lang/GroupRing.lean

lean_all/lang/GroupRing.lean

/-
  lang/GroupRing — G1 (T43): the group ring K[C_p] of the cyclic group 𝔽_p = Zp m (p = m + 1) over any commutative ring K,
  as a commutative ring (no function extensionality: elements are length-p tuples).
    V, getN, tabN, getN_tabN, extN       tuples R × (R × … ()) with index access, tabulation and extensionality
    gt, tb, gt_tb, ext                    the same, indexed by Zp m
    gmul                                  (u ⋆ v)(c) = Σ_a u(a) v(c − a)
    gm_comm, gm_one, gm_dist, gm_assoc    the ring laws (reindexing a ↦ c − a and b ↦ b + a)
    gring                                 the commutative ring K[C_p]
  For K = 𝔽_q this is the ring in which the Gauss sum of lang/GaussSum satisfies g^q = (q/p) g (next step: quadratic reciprocity).

  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.ConicSum

namespace GroupRing

open FTAChain ZMod YMChain FiniteRecChain DetChain

def V (R : Type) : Nat → Type
  | 0 => Unit
  | n + 1 => R × V R n

section
variable {R : Type}

def getN (d : R) : (n : Nat) → V R n → Nat → R
  | 0, _, _ => d
  | _ + 1, u, 0 => u.1
  | n + 1, u, i + 1 => getN d n u.2 i

def tabN (f : Nat → R) : (n : Nat) → V R n
  | 0 => ()
  | n + 1 => (f 0, tabN (fun i => f (i + 1)) n)

theorem getN_tabN (d : R) : ∀ n (f : Nat → R) i, i < n → getN d n (tabN f n) i = f i
  | 0, _, _, h => absurd h (Nat.not_lt_zero _)
  | _ + 1, _, 0, _ => rfl
  | n + 1, f, i + 1, h => getN_tabN d n (fun i => f (i + 1)) i (Nat.lt_of_succ_lt_succ h)

theorem extN (d : R) : ∀ n (u v : V R n), (∀ i, i < n → getN d n u i = getN d n v i) → u = v
  | 0, (), (), _ => rfl
  | n + 1, (a, u), (b, v), h => by
    have e0 : a = b := h 0 (Nat.zero_lt_succ n)
    have e1 : u = v := extN d n u v (fun i hi => h (i + 1) (Nat.succ_lt_succ hi))
    rw [e0, e1]
end

variable {R : Type} (K : CRing R) (m : Nat)

def gt (u : V R (m + 1)) (x : Zp m) : R := getN K.zero (m + 1) u x.val

def tb (f : Zp m → R) : V R (m + 1) := tabN (fun i => f (mk m i)) (m + 1)

theorem gt_tb (f : Zp m → R) (x : Zp m) : gt K m (tb m f) x = f x := by
  show getN K.zero (m + 1) (tabN (fun i => f (mk m i)) (m + 1)) x.val = f x
  rw [getN_tabN K.zero (m + 1) _ x.val x.property, mk_val]

theorem ext (u v : V R (m + 1)) (h : ∀ x, gt K m u x = gt K m v x) : u = v :=
  extN K.zero (m + 1) u v (fun i hi => by
    have e : (mk m i).val = i := md_small m i hi
    have := h (mk m i)
    unfold gt at this
    rw [e] at this
    exact this)

def L : List (Zp m) := CMInert.allZ m (m + 1)

def gadd (u v : V R (m + 1)) : V R (m + 1) := tb m (fun x => K.add (gt K m u x) (gt K m v x))
def gneg (u : V R (m + 1)) : V R (m + 1) := tb m (fun x => K.neg (gt K m u x))
def gzero : V R (m + 1) := tb m (fun _ => K.zero)
def gone : V R (m + 1) := tb m (fun x => delta K x (ring m).zero)
def gmul (u v : V R (m + 1)) : V R (m + 1) :=
  tb m (fun c => sumK K (fun a => K.mul (gt K m u a) (gt K m v ((ring m).add c ((ring m).neg a)))) (L m))

theorem gt_mul (u v : V R (m + 1)) (c : Zp m) :
    gt K m (gmul K m u v) c = sumK K (fun a => K.mul (gt K m u a) (gt K m v ((ring m).add c ((ring m).neg a)))) (L m) :=
  gt_tb K m _ c

theorem shift (F : Zp m → R) (a : Zp m) : sumK K (fun b => F ((ring m).add b a)) (L m) = sumK K F (L m) :=
  reindex K _ (CMInert.complete_allZ m K) (fun b => (ring m).add b a) (fun b => (ring m).add b ((ring m).neg a))
    (fun x => by lib_ring) (fun y => by lib_ring) F

theorem flip (F : Zp m → R) (c : Zp m) : sumK K (fun a => F ((ring m).add c ((ring m).neg a))) (L m) = sumK K F (L m) :=
  reindex K _ (CMInert.complete_allZ m K) (fun a => (ring m).add c ((ring m).neg a)) (fun a => (ring m).add c ((ring m).neg a))
    (fun x => by lib_ring) (fun y => by lib_ring) F

theorem gm_comm (u v : V R (m + 1)) : gmul K m u v = gmul K m v u := ext K m _ _ (fun c => by
  rw [gt_mul, gt_mul]
  have e : sumK K (fun a => K.mul (gt K m u a) (gt K m v ((ring m).add c ((ring m).neg a)))) (L m)
      = sumK K (fun a => (fun b => K.mul (gt K m v b) (gt K m u ((ring m).add c ((ring m).neg b)))) ((ring m).add c ((ring m).neg a))) (L m) :=
    sumK_ext K _ _ (fun a => by
      show _ = K.mul (gt K m v _) (gt K m u ((ring m).add c ((ring m).neg ((ring m).add c ((ring m).neg a)))))
      rw [show (ring m).add c ((ring m).neg ((ring m).add c ((ring m).neg a))) = a by lib_ring, K.mul_comm]) _
  rw [e]
  exact flip K m (fun b => K.mul (gt K m v b) (gt K m u ((ring m).add c ((ring m).neg b)))) c)

theorem gm_one (v : V R (m + 1)) : gmul K m (gone K m) v = v := ext K m _ _ (fun c => by
  rw [gt_mul]
  have e : sumK K (fun a => K.mul (gt K m (gone K m) a) (gt K m v ((ring m).add c ((ring m).neg a)))) (L m)
      = sumK K (fun a => K.mul (delta K a (ring m).zero) (gt K m v ((ring m).add c ((ring m).neg a)))) (L m) :=
    sumK_ext K _ _ (fun a => by unfold gone; rw [gt_tb]) _
  rw [e]
  refine (pick K _ (CMInert.complete_allZ m K) (fun a => gt K m v ((ring m).add c ((ring m).neg a))) (ring m).zero).trans ?_
  show gt K m v ((ring m).add c ((ring m).neg (ring m).zero)) = _
  rw [show (ring m).add c ((ring m).neg (ring m).zero) = c by lib_ring])

theorem gm_dist (u v w : V R (m + 1)) : gmul K m u (gadd K m v w) = gadd K m (gmul K m u v) (gmul K m u w) := ext K m _ _ (fun c => by
  unfold gadd
  rw [gt_mul, gt_tb, gt_mul, gt_mul]
  rw [← sumK_add]
  exact sumK_ext K _ _ (fun a => by rw [gt_tb, K.distrib]) _)

theorem gm_assoc (u v w : V R (m + 1)) : gmul K m (gmul K m u v) w = gmul K m u (gmul K m v w) := ext K m _ _ (fun c => by
  rw [gt_mul, gt_mul]
  have e1 : sumK K (fun b => K.mul (gt K m (gmul K m u v) b) (gt K m w ((ring m).add c ((ring m).neg b)))) (L m)
      = sumK K (fun b => sumK K (fun a => K.mul (K.mul (gt K m u a) (gt K m v ((ring m).add b ((ring m).neg a)))) (gt K m w ((ring m).add c ((ring m).neg b)))) (L m)) (L m) :=
    sumK_ext K _ _ (fun b => by rw [gt_mul, sumK_mul]) _
  have e2 : sumK K (fun a => K.mul (gt K m u a) (gt K m (gmul K m v w) ((ring m).add c ((ring m).neg a)))) (L m)
      = sumK K (fun a => sumK K (fun b => K.mul (K.mul (gt K m u a) (gt K m v ((ring m).add b ((ring m).neg a)))) (gt K m w ((ring m).add c ((ring m).neg b)))) (L m)) (L m) :=
    sumK_ext K _ _ (fun a => by
      rw [gt_mul, mul_sumK]
      have e3 := shift K m (fun b => K.mul (K.mul (gt K m u a) (gt K m v ((ring m).add b ((ring m).neg a)))) (gt K m w ((ring m).add c ((ring m).neg b)))) a
      rw [← e3]
      exact sumK_ext K _ _ (fun b => by
        show _ = K.mul (K.mul (gt K m u a) (gt K m v ((ring m).add ((ring m).add b a) ((ring m).neg a)))) (gt K m w ((ring m).add c ((ring m).neg ((ring m).add b a))))
        rw [show (ring m).add ((ring m).add b a) ((ring m).neg a) = b by lib_ring,
          show (ring m).add c ((ring m).neg ((ring m).add b a)) = (ring m).add ((ring m).add c ((ring m).neg a)) ((ring m).neg b) by lib_ring,
          K.mul_assoc]) _) _
  rw [e1, e2, sumK_swap])

/-- **The group ring K[C_p] as a commutative ring.** -/
def gring : CRing (V R (m + 1)) where
  add := gadd K m
  mul := gmul K m
  neg := gneg K m
  zero := gzero K m
  one := gone K m
  add_assoc := fun a b c => ext K m _ _ (fun x => by unfold gadd; rw [gt_tb, gt_tb, gt_tb, gt_tb, K.add_assoc])
  add_comm := fun a b => ext K m _ _ (fun x => by unfold gadd; rw [gt_tb, gt_tb, K.add_comm])
  zero_add := fun a => ext K m _ _ (fun x => by unfold gadd gzero; rw [gt_tb, gt_tb, K.zero_add])
  add_neg := fun a => ext K m _ _ (fun x => by unfold gadd gneg gzero; rw [gt_tb, gt_tb, gt_tb, K.add_neg])
  mul_assoc := gm_assoc K m
  mul_comm := gm_comm K m
  one_mul := gm_one K m
  distrib := gm_dist K m

end GroupRing

#print axioms GroupRing.getN_tabN
#print axioms GroupRing.extN
#print axioms GroupRing.gt_tb
#print axioms GroupRing.ext
#print axioms GroupRing.gt_mul
#print axioms GroupRing.shift
#print axioms GroupRing.flip
#print axioms GroupRing.gm_comm
#print axioms GroupRing.gm_one
#print axioms GroupRing.gm_dist
#print axioms GroupRing.gm_assoc
#print axioms GroupRing.gring

lang/GroupRingFrob.lean

lean_all/lang/GroupRingFrob.lean

/-
  lang/GroupRingFrob — G1 (T43): monomials and Frobenius in the group ring K[C_p] (lang/GroupRing).
    gt_nsm, gt_sum        coefficients of n·x and of a finite sum
    char_G                q·1 = 0 in K ⇒ q·1 = 0 in K[C_p]
    frob_sum              (Σ_a x_a)^q = Σ_a x_a^q in K[C_p] when K has characteristic q (Frobenius.frob_add)
    mono, gt_mono         s ζ^a
    mono_mul, mono_pow    (s ζ^a)(t ζ^b) = st ζ^(a+b),  (s ζ^a)^k = s^k ζ^(k a)
    decomp                every u is Σ_a u(a) ζ^a
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.GroupRing

namespace GroupRingFrob

open FTAChain ZMod YMChain FiniteRecChain DetChain GroupRing Frobenius

variable {R : Type} (K : CRing R) (m : Nat)

theorem gt_nsm (x : V R (m + 1)) (c : Zp m) : ∀ k, gt K m (nsm (gring K m) k x) c = nsm K k (gt K m x c)
  | 0 => gt_tb K m (fun _ => K.zero) c
  | k + 1 => by
    show gt K m (gadd K m (nsm (gring K m) k x) x) c = K.add (nsm K k (gt K m x c)) (gt K m x c)
    unfold gadd; rw [gt_tb, gt_nsm x c k]

theorem gt_sum {A : Type} (h : A → V R (m + 1)) (c : Zp m) : ∀ l : List A,
    gt K m (sumK (gring K m) h l) c = sumK K (fun a => gt K m (h a) c) l
  | [] => gt_tb K m (fun _ => K.zero) c
  | a :: l => by
    show gt K m (gadd K m (h a) (sumK (gring K m) h l)) c = K.add (gt K m (h a) c) (sumK K (fun a => gt K m (h a) c) l)
    unfold gadd; rw [gt_tb, gt_sum h c l]

theorem char_G (q : Nat) (hq : nsm K q K.one = K.zero) : nsm (gring K m) q (gring K m).one = (gring K m).zero :=
  ext K m _ _ (fun c => by
    rw [gt_nsm, nsm_eq_mul, hq, zero_mul']
    exact (gt_tb K m (fun _ => K.zero) c).symm)

theorem pw_zero_succ' {S : Type} (Kr : CRing S) (k : Nat) : pw Kr Kr.zero (k + 1) = Kr.zero := mul_zero Kr _

theorem frob_sum (j : Nat) (hpr : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1)
    (hp : nsm K (j + 1 + 1) K.one = K.zero) {A : Type} (f : A → V R (m + 1)) : ∀ l : List A,
    pw (gring K m) (sumK (gring K m) f l) (j + 1 + 1) = sumK (gring K m) (fun a => pw (gring K m) (f a) (j + 1 + 1)) l
  | [] => pw_zero_succ' (gring K m) (j + 1)
  | a :: l => by
    show pw (gring K m) ((gring K m).add (f a) (sumK (gring K m) f l)) (j + 1 + 1) = (gring K m).add _ _
    rw [frob_add (gring K m) j hpr (char_G K m _ hp), frob_sum j hpr hp f l]

def mono (a : Zp m) (s : R) : V R (m + 1) := tb m (fun c => K.mul s (delta K c a))

theorem gt_mono (a : Zp m) (s : R) (c : Zp m) : gt K m (mono K m a s) c = K.mul s (delta K c a) := gt_tb K m _ c

theorem delta_shift (a b c : Zp m) : delta K ((ring m).add c ((ring m).neg a)) b = delta K c ((ring m).add a b) := by
  unfold delta
  by_cases h : c = (ring m).add a b
  · rw [ite_pos' _ _ h, ite_pos' _ _ (by rw [h]; lib_ring)]
  · rw [ite_neg' _ _ h, ite_neg' _ _ (fun e => h (by rw [← e]; lib_ring))]

theorem mono_mul (a b : Zp m) (s t : R) : gmul K m (mono K m a s) (mono K m b t) = mono K m ((ring m).add a b) (K.mul s t) :=
  ext K m _ _ (fun c => by
    rw [gt_mul, gt_mono]
    have e : sumK K (fun x => K.mul (gt K m (mono K m a s) x) (gt K m (mono K m b t) ((ring m).add c ((ring m).neg x)))) (L m)
        = sumK K (fun x => K.mul (delta K x a) ((fun y => K.mul (K.mul s t) (delta K ((ring m).add c ((ring m).neg y)) b)) x)) (L m) :=
      sumK_ext K _ _ (fun x => by
        rw [gt_mono, gt_mono]
        show K.mul (K.mul s (delta K x a)) (K.mul t (delta K ((ring m).add c ((ring m).neg x)) b)) = K.mul (delta K x a) (K.mul (K.mul s t) (delta K ((ring m).add c ((ring m).neg x)) b))
        lib_ring) _
    rw [e]
    refine (pick K _ (CMInert.complete_allZ m K) _ a).trans ?_
    show K.mul (K.mul s t) (delta K ((ring m).add c ((ring m).neg a)) b) = _
    rw [delta_shift])

theorem mono_pow (a : Zp m) (s : R) : ∀ k, pw (gring K m) (mono K m a s) k = mono K m (nsm (ring m) k a) (pw K s k)
  | 0 => ext K m _ _ (fun c => by
      show gt K m (gone K m) c = _
      unfold gone; rw [gt_tb, gt_mono]
      show _ = K.mul K.one (delta K c (ring m).zero)
      rw [K.one_mul])
  | k + 1 => by
    show gmul K m (pw (gring K m) (mono K m a s) k) (mono K m a s) = mono K m ((ring m).add (nsm (ring m) k a) a) (K.mul (pw K s k) s)
    rw [mono_pow a s k, mono_mul]

theorem decomp (u : V R (m + 1)) : u = sumK (gring K m) (fun a => mono K m a (gt K m u a)) (L m) :=
  ext K m _ _ (fun c => by
    rw [gt_sum]
    have e : sumK K (fun a => gt K m (mono K m a (gt K m u a)) c) (L m) = sumK K (fun a => K.mul (delta K a c) (gt K m u a)) (L m) :=
      sumK_ext K _ _ (fun a => by rw [gt_mono, delta_symm, K.mul_comm]) _
    rw [e]
    exact (pick K _ (CMInert.complete_allZ m K) _ c).symm)

end GroupRingFrob

#print axioms GroupRingFrob.gt_nsm
#print axioms GroupRingFrob.gt_sum
#print axioms GroupRingFrob.char_G
#print axioms GroupRingFrob.pw_zero_succ'
#print axioms GroupRingFrob.frob_sum
#print axioms GroupRingFrob.gt_mono
#print axioms GroupRingFrob.delta_shift
#print axioms GroupRingFrob.mono_mul
#print axioms GroupRingFrob.mono_pow
#print axioms GroupRingFrob.decomp

lang/GsUnit.lean

lean_all/lang/GsUnit.lean

/-
  lang/GsUnit — T49 (O3') (i') (a): **Σ_(i<n) w^i = 0 when w^n = 1 and w − 1 is a unit** — in any commutative ring
  (no zero-divisor hypothesis), e.g. ℤ/p^k at w = z^c with red(z^c) ≠ 1 (CycEis.gs_mul: (w − 1)·Σ w^i = w^n − 1).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.CycEis

namespace GsUnit

open DetChain Frobenius

theorem gs_unit {S : Type} (E : CRing S) (w u : S) (hu : E.mul (E.add w (E.neg E.one)) u = E.one) (n : Nat)
    (hwn : pw E w n = E.one) : CycChar.gs E w n = E.zero := by
  have h := CycEis.gs_mul E w n
  rw [hwn] at h
  calc CycChar.gs E w n = E.mul (E.mul (E.add w (E.neg E.one)) u) (CycChar.gs E w n) := by rw [hu]; lib_ring
    _ = E.mul u (E.mul (E.add w (E.neg E.one)) (CycChar.gs E w n)) := by lib_ring
    _ = E.mul u (E.add E.one (E.neg E.one)) := by rw [h]
    _ = E.zero := by lib_ring

end GsUnit

#print axioms GsUnit.gs_unit

lang/GsigPow.lean

lean_all/lang/GsigPow.lean

/-
  lang/GsigPow — T49 (O3') (v2): **φ_(z^e)(σ_d α) = φ_(z^(e·c))(α)** for c·d = 1 in 𝔽_ℓ, z^ℓ = 1
  (CycGalEv.phz_gsig at the root z^e, SymProdZ.pw_pw).
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.CycGalEv
import lang.SymProdZ

namespace GsigPow

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing

theorem phz_gsig_pow {S : Type} (E : CRing S) (m : Nat) (z : S) (hz : pw E z (m + 1) = E.one) (e : Nat)
    (c d : Zp m) (hcd : (ring m).mul c d = (ring m).one) (v : V Int (m + 1)) :
    CycEis.phz E m (pw E z e) (CycGal.gsig LibInt.intR m d v) = CycEis.phz E m (pw E z (e * c.val)) v := by
  have hze : pw E (pw E z e) (m + 1) = E.one := by
    rw [SymProdZ.pw_pw]
    exact SymProdW.pw_mulE E z (m + 1) hz e
  rw [CycGalEv.phz_gsig E m (pw E z e) hze c d hcd v, SymProdZ.pw_pw]

end GsigPow

#print axioms GsigPow.phz_gsig_pow

lang/Gz.lean

lean_all/lang/Gz.lean

/-
  lang/Gz — T47, cycle 560: Gauss's theta sum as a sum over e ∈ ℤ (the list HeckeMult.zl K = [0, 1, −1, …, K, −K]):
    Σ_{m ≤ 2K} (−1)^m x^((m − K)²) = (−1)^K · Σ_{e ∈ zl K} (−1)^|e| x^(e²).

    gf e            (−1)^|e| x^(|e|²)
    ex_shift        Ex (N+1) (m+1) = Ex N m
    gz              the identity above (induction on K through es_shift, like JacobiP.w_zero)
    gz_even         for K = 2M the sign is +1
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.JacobiP
import lang.HeckeMult

namespace QBinom

open FTAChain

variable {R : Type} (K : DetChain.CRing R) (x : R)

def gfz (e : Int) : R := K.mul (Frobenius.pw K (K.neg K.one) (Int.natAbs e)) (Frobenius.pw K x (Int.natAbs e * Int.natAbs e))

def Bz (N : Nat) : R := YMChain.sumK K (gfz K x) (HeckeMult.zl N)

theorem Bz_succ (N : Nat) : Bz K x (N + 1) =
    K.add (Bz K x N) (K.add (gfz K x (Int.ofNat (N + 1))) (K.add (gfz K x (Int.negSucc N)) K.zero)) := by
  show YMChain.sumK K (gfz K x) (HeckeMult.zl N ++ [Int.ofNat (N + 1), Int.negSucc N]) = _
  rw [YMChain.sumK_append]; rfl

theorem ex_shift (N m : Nat) : Ex (N + 1) (m + 1) = Ex N m := by
  show (m + 1 - (N + 1)) * (m + 1 - (N + 1)) + (N + 1 - (m + 1)) * (N + 1 - (m + 1)) = (m - N) * (m - N) + (N - m) * (N - m)
  rw [LibInt.nsub_succ_succ, LibInt.nsub_succ_succ]

theorem gz : ∀ N, es K (fun m => Frobenius.pw K x (Ex N m)) (K.neg K.one) (2 * N + 1) =
    K.mul (Frobenius.pw K (K.neg K.one) N) (Bz K x N)
  | 0 => by
    show K.add K.zero (K.mul K.one K.one) = K.mul K.one (K.add (K.mul K.one K.one) K.zero); lib_ring
  | N + 1 => by
    have hc := es_congr K (fun m => Frobenius.pw K x (Ex (N + 1) (m + 1))) (fun m => Frobenius.pw K x (Ex N m)) (K.neg K.one)
      (fun m => congrArg (Frobenius.pw K x) (ex_shift N m)) (2 * N + 1)
    rw [show 2 * (N + 1) + 1 = (2 * N + 1 + 1) + 1 by lib_nat, es_shift]
    show K.add (Frobenius.pw K x (Ex (N + 1) 0)) (K.mul (K.neg K.one)
      (K.add (es K (fun m => Frobenius.pw K x (Ex (N + 1) (m + 1))) (K.neg K.one) (2 * N + 1))
        (K.mul (Frobenius.pw K x (Ex (N + 1) (2 * N + 1 + 1))) (Frobenius.pw K (K.neg K.one) (2 * N + 1))))) = _
    rw [hc, gz N, Bz_succ]
    have e0 : Ex (N + 1) 0 = (N + 1) * (N + 1) := (congrArg (fun t => Ex t 0) (show N + 1 = 0 + (N + 1) by lib_nat)).trans (ex_dn 0 (N + 1))
    have e1 : Ex (N + 1) (2 * N + 1 + 1) = (N + 1) * (N + 1) := by rw [show 2 * N + 1 + 1 = (N + 1) + (N + 1) by lib_nat, ex_up]
    rw [e0, e1, show Frobenius.pw K (K.neg K.one) (2 * N + 1) = K.mul (Frobenius.pw K (K.neg K.one) (2 * N)) (K.neg K.one) from rfl, pm_even]
    show _ = K.mul (K.mul (Frobenius.pw K (K.neg K.one) N) (K.neg K.one))
      (K.add (Bz K x N) (K.add (K.mul (K.mul (Frobenius.pw K (K.neg K.one) N) (K.neg K.one)) (Frobenius.pw K x ((N + 1) * (N + 1))))
        (K.add (K.mul (K.mul (Frobenius.pw K (K.neg K.one) N) (K.neg K.one)) (Frobenius.pw K x ((N + 1) * (N + 1)))) K.zero)))
    have hs := pm_sq K N
    generalize Frobenius.pw K (K.neg K.one) N = s at hs ⊢
    generalize Frobenius.pw K x ((N + 1) * (N + 1)) = A
    calc _ = K.add (K.add A A) (K.neg (K.mul s (Bz K x N))) := by lib_ring
      _ = K.add (K.mul (K.add A A) (K.mul s s)) (K.neg (K.mul s (Bz K x N))) := by rw [hs]; lib_ring
      _ = _ := by lib_ring

theorem gz_even (M : Nat) : es K (fun m => Frobenius.pw K x (Ex (2 * M) m)) (K.neg K.one) (2 * (2 * M) + 1) = Bz K x (2 * M) := by
  rw [gz, pm_even]; exact K.one_mul _

end QBinom

#print axioms QBinom.Bz_succ
#print axioms QBinom.ex_shift
#print axioms QBinom.gz
#print axioms QBinom.gz_even

lang/GzGen.lean

lean_all/lang/GzGen.lean

/-
  lang/GzGen — T47, cycle 576: a centered alternating sum as a sum over j ∈ ℤ (HeckeMult.zl), for any shift-invariant g:
    g (N+1) (m+1) = g N m  ⇒  Σ_{m ≤ 2N} (−1)^m F(g N m) = (−1)^N Σ_{j ∈ zl N} (−1)^|j| F(G j),
  G (ofNat k) = g 0 k, G (negSucc k) = g (k+1) 0   (so G (m − N) = g N m).

    Gj, g_up, g_dn, Bg, gzg
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Gz

namespace QBinom

open FTAChain

variable {R : Type} (K : DetChain.CRing R) (F : Nat → R) (g : Nat → Nat → Nat) (hg : ∀ N m, g (N + 1) (m + 1) = g N m)

def Gj (j : Int) : Nat :=
  match j with
  | Int.ofNat k => g 0 k
  | Int.negSucc k => g (k + 1) 0

include hg in
theorem g_up : ∀ N k, g N (N + k) = g 0 k
  | 0, k => by rw [Nat.zero_add]
  | N + 1, k => by rw [show N + 1 + k = (N + k) + 1 by lib_nat, hg, g_up N k]

include hg in
theorem g_dn : ∀ N k, g (N + k + 1) N = g (k + 1) 0
  | 0, k => by rw [Nat.zero_add]
  | N + 1, k => by rw [show N + 1 + k + 1 = (N + k + 1) + 1 by lib_nat, hg, g_dn N k]

def Bg (N : Nat) : R := YMChain.sumK K (fun j => K.mul (Frobenius.pw K (K.neg K.one) (Int.natAbs j)) (F (Gj g j))) (HeckeMult.zl N)

theorem Bg_succ (N : Nat) : Bg K F g (N + 1) =
    K.add (Bg K F g N) (K.add (K.mul (Frobenius.pw K (K.neg K.one) (N + 1)) (F (g 0 (N + 1))))
      (K.add (K.mul (Frobenius.pw K (K.neg K.one) (N + 1)) (F (g (N + 1) 0))) K.zero)) := by
  show YMChain.sumK K _ (HeckeMult.zl N ++ [Int.ofNat (N + 1), Int.negSucc N]) = _
  rw [YMChain.sumK_append]; rfl

include hg in
theorem gzg : ∀ N, es K (fun m => F (g N m)) (K.neg K.one) (2 * N + 1) = K.mul (Frobenius.pw K (K.neg K.one) N) (Bg K F g N)
  | 0 => by
    show K.add K.zero (K.mul (F (g 0 0)) K.one) = K.mul K.one (K.add (K.mul K.one (F (g 0 0))) K.zero); lib_ring
  | N + 1 => by
    have hc := es_congr K (fun m => F (g (N + 1) (m + 1))) (fun m => F (g N m)) (K.neg K.one)
      (fun m => congrArg F (hg N m)) (2 * N + 1)
    rw [show 2 * (N + 1) + 1 = (2 * N + 1 + 1) + 1 by lib_nat, es_shift]
    show K.add (F (g (N + 1) 0)) (K.mul (K.neg K.one)
      (K.add (es K (fun m => F (g (N + 1) (m + 1))) (K.neg K.one) (2 * N + 1))
        (K.mul (F (g (N + 1) (2 * N + 1 + 1))) (Frobenius.pw K (K.neg K.one) (2 * N + 1))))) = _
    rw [hc, gzg N, Bg_succ]
    have e0 : g (N + 1) 0 = g (N + 1) 0 := rfl
    have e1 : g (N + 1) (2 * N + 1 + 1) = g 0 (N + 1) := by rw [show 2 * N + 1 + 1 = (N + 1) + (N + 1) by lib_nat, g_up g hg]
    have e2 : g (N + 1) 0 = Gj g (Int.negSucc N) := by show g (N + 1) 0 = g (N + 1) 0; rfl
    rw [e1, show Frobenius.pw K (K.neg K.one) (2 * N + 1) = K.mul (Frobenius.pw K (K.neg K.one) (2 * N)) (K.neg K.one) from rfl, pm_even]
    show _ = K.mul (K.mul (Frobenius.pw K (K.neg K.one) N) (K.neg K.one))
      (K.add (Bg K F g N) (K.add (K.mul (K.mul (Frobenius.pw K (K.neg K.one) N) (K.neg K.one)) (F (g 0 (N + 1))))
        (K.add (K.mul (K.mul (Frobenius.pw K (K.neg K.one) N) (K.neg K.one)) (F (g (N + 1) 0))) K.zero)))
    have hs := pm_sq K N
    generalize Frobenius.pw K (K.neg K.one) N = s at hs ⊢
    generalize F (g 0 (N + 1)) = A
    generalize F (g (N + 1) 0) = B
    calc _ = K.add (K.add A B) (K.neg (K.mul s (Bg K F g N))) := by lib_ring
      _ = K.add (K.mul (K.add A B) (K.mul s s)) (K.neg (K.mul s (Bg K F g N))) := by rw [hs]; lib_ring
      _ = _ := by lib_ring

end QBinom

#print axioms QBinom.g_up
#print axioms QBinom.g_dn
#print axioms QBinom.Bg_succ
#print axioms QBinom.gzg

lang/HasseBoundP.lean

lean_all/lang/HasseBoundP.lean

/-
  lang/HasseBoundP — T43, cycle 109: Hasse's bound a_p² ≤ 4p (Ramanujan for the CM forms) at every prime, both curves.

    hasse_i       ∃ s, S² + s = 4p,  S = Σ_{𝔽_p} χ(x³ − x),  every odd p        (inert: S = 0; split: S = −2a, s = 4b²)
    hasse_omega   ∃ s, ψ(1)² + s = 4p, ψ(1) = Σ_{𝔽_p} χ(x³ + 1), every p > 3    (inert: 0; split: ψ(1) = −2a, s = 12b²)
  |a_p| ≤ 2√p, i.e. the Satake parameters of the CM newforms have absolute value √p (Ramanujan–Petersson for these
  forms), at every good prime, as integer statements.

  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.Deuring

namespace HasseBoundP

open FTAChain ZMod YMChain FiniteRecChain LibInt

theorem hasse_i (k : Nat) (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = 2 * k + 1 + 1 + 1) :
    ∃ s : Nat, sumK LibInt.intR (fun x => QChar.chi k (CMInert.cub (ring (2 * k + 1 + 1)) x)) (ConicSum.L k)
      * sumK LibInt.intR (fun x => QChar.chi k (CMInert.cub (ring (2 * k + 1 + 1)) x)) (ConicSum.L k)
      + Int.ofNat s = 4 * Int.ofNat (2 * k + 1 + 1 + 1) := by
  cases EveryPrime.nat_even_odd k with
  | inl h =>
    match h with
    | ⟨j, hj⟩ => subst hj; exact ⟨4 * (2 * (2 * j) + 1 + 1 + 1), by rw [InertFq.s1_inert j hpr, show Int.ofNat (4 * (2 * (2 * j) + 1 + 1 + 1)) = 4 * Int.ofNat (2 * (2 * j) + 1 + 1 + 1) from rfl]; lib_int⟩
  | inr h =>
    match h with
    | ⟨j, hj⟩ =>
      subst hj
      match SignCM3.ap_primary j hpr with
      | ⟨a, b, hab, _, hS⟩ =>
        have hS' : -sumK LibInt.intR (fun x => QChar.chi (2 * j + 1) (CMInert.cub (ring (2 * (2 * j + 1) + 1 + 1)) x)) (ConicSum.L (2 * j + 1)) = 2 * a := hS
        generalize sumK LibInt.intR (fun x => QChar.chi (2 * j + 1) (CMInert.cub (ring (2 * (2 * j + 1) + 1 + 1)) x)) (ConicSum.L (2 * j + 1)) = S at hS' ⊢
        match FinCubicSign.sq_nn b with
        | ⟨nb, hnb⟩ =>
          refine ⟨4 * nb, ?_⟩
          have e : S = -(2 * a) := by rw [← hS']; lib_int
          rw [e, ← hab, show Int.ofNat (4 * nb) = 4 * Int.ofNat nb from rfl, ← hnb]; lib_int

theorem hasse_omega (k : Nat) (hk : 1 ≤ k) (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = 2 * k + 1 + 1 + 1) :
    ∃ s : Nat, CubicJacob.psi k (ring (2 * k + 1 + 1)).one * CubicJacob.psi k (ring (2 * k + 1 + 1)).one
      + Int.ofNat s = 4 * Int.ofNat (2 * k + 1 + 1 + 1) := by
  cases EveryPrime3.nat_mod3 k with
  | inl h =>
    exfalso
    match h with
    | ⟨t, ht⟩ =>
      cases hpr 3 ⟨2 * t + 1, by rw [ht]; lib_nat⟩ with
      | inl e => exact Nat.noConfusion (Nat.succ.inj (show (2 : Nat) + 1 = 0 + 1 from e))
      | inr e =>
        have e' : 2 * k = 0 := Nat.succ.inj (Nat.succ.inj (Nat.succ.inj e.symm))
        cases k with
        | zero => exact Nat.not_succ_le_zero 0 hk
        | succ k' => exact Nat.noConfusion (show 2 * k' + 1 + 1 = 0 from e')
  | inr h =>
    cases h with
    | inl h =>
      match h with
      | ⟨t, ht⟩ =>
        subst ht
        exact ⟨4 * (2 * (3 * t + 1) + 1 + 1 + 1), by
          rw [SexticFamily.psi_inert (3 * t + 1) hpr (4 * t + 3) (by lib_nat), show Int.ofNat (4 * (2 * (3 * t + 1) + 1 + 1 + 1)) = 4 * Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) from rfl]; lib_int⟩
    | inr h =>
      match h with
      | ⟨t, ht⟩ =>
        subst ht
        match CubicSign.cubic_exact (3 * t + 2) hpr (2 * t + 2) (by lib_nat) with
        | ⟨a, b, hab, _, hS⟩ =>
          have hS' : -CubicJacob.psi (3 * t + 2) (ring (2 * (3 * t + 2) + 1 + 1)).one = 2 * a := hS
          generalize CubicJacob.psi (3 * t + 2) (ring (2 * (3 * t + 2) + 1 + 1)).one = S at hS' ⊢
          match FinCubicSign.sq_nn b with
          | ⟨nb, hnb⟩ =>
            refine ⟨12 * nb, ?_⟩
            have e : S = -(2 * a) := by rw [← hS']; lib_int
            rw [e, ← hab, show Int.ofNat (12 * nb) = 12 * Int.ofNat nb from rfl, ← hnb]; lib_int

end HasseBoundP

#print axioms HasseBoundP.hasse_i
#print axioms HasseBoundP.hasse_omega

lang/HasseCM.lean

lean_all/lang/HasseCM.lean

/-
  lang/HasseCM — T43, cycle 28: the Hasse invariant of y² = x³ − x at p = 4k + 5.

  p = J + 2 prime, h = (p − 1)/2. Writing (x³ − x)^h = x^h (x² − 1)^h = x^h Σ_i C(h, i) x^(2i) (−1)^(h−i)
  (Frobenius.ev_row) and summing over x with the power sums of lang/PowerSum:
    ps_low, ps_top, ps_high   Σ_x x^e = 0 (1 ≤ e ≤ p−2), = −1 (e = p−1), = 0 (p ≤ e ≤ 2p−3)
    T, T_cons, T_zero, T_skip, skip_many   Σ_x x^e · ev(x², −1, cs), one coefficient at a time
    cub_factor                x³ − x = x (x² − 1)
    hasse_cm                  for p = 4k + 5:  Σ_x (x³ − x)^((p−1)/2) = −(−1)^(k+1) · C(2k + 2, k + 1)
  Since #E(𝔽_p) = p + 1 + Σ_x χ(x³ − x), this gives a_p ≡ (−1)^(k+1) C((p−1)/2, (p−1)/4) (mod p) at every split
  prime — the Hasse invariant; with |a_p| < 2√p it fixes a_p (next: C((p−1)/2, (p−1)/4) ≡ ±2u (mod p),
  p = u² + v², Gauss).

  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.PowerSum

namespace HasseCM

open FTAChain ZMod YMChain Frobenius

variable (J : Nat)

def ps (e : Nat) : Zp (J + 1) :=
  sumK (ring (J + 1)) (fun x => pw (ring (J + 1)) x e) (CMInert.allZ (J + 1) (J + 1 + 1))

theorem pw_add' {R : Type} (K : DetChain.CRing R) (x : R) (a : Nat) : ∀ b, pw K x (a + b) = K.mul (pw K x a) (pw K x b)
  | 0 => by show pw K x a = K.mul (pw K x a) K.one; rw [K.mul_comm, K.one_mul]
  | b + 1 => by
    show K.mul (pw K x (a + b)) x = K.mul (pw K x a) (K.mul (pw K x b) x)
    rw [pw_add' K x a b, K.mul_assoc]

theorem pw_zero_succ {R : Type} (K : DetChain.CRing R) (r : Nat) : pw K K.zero (r + 1) = K.zero :=
  DetChain.mul_zero K _

theorem ps_low (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (e : Nat) (h1 : 1 ≤ e) (h2 : e ≤ J) :
    ps J e = (ring (J + 1)).zero := by
  cases e with
  | zero => exact absurd h1 (Nat.not_succ_le_zero 0)
  | succ e' => exact PowerSum.power_sum_zero J hpr e' h2

theorem ps_top (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) : ps J (J + 1) = (ring (J + 1)).neg (ring (J + 1)).one :=
  PowerSum.power_sum_top J hpr

theorem ps_high (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (r : Nat) (h1 : 1 ≤ r) (h2 : r ≤ J) :
    ps J (J + 1 + r) = (ring (J + 1)).zero := by
  have e : ps J (J + 1 + r) = ps J r := sumK_ext (ring (J + 1)) _ _ (fun x => by
    show pw (ring (J + 1)) x (J + 1 + r) = pw (ring (J + 1)) x r
    by_cases hx : x = (ring (J + 1)).zero
    · rw [hx]
      cases r with
      | zero => exact absurd h1 (Nat.not_succ_le_zero 0)
      | succ r' => rw [pw_zero_succ, ← Nat.add_assoc, pw_zero_succ]
    · rw [pw_add' (ring (J + 1)) x (J + 1) r, unit_pow J hpr x hx, (ring (J + 1)).one_mul]) _
  rw [e]
  exact ps_low J hpr r h1 h2

def evs (x : Zp (J + 1)) (cs : List Nat) : Zp (J + 1) := ev (ring (J + 1)) ((ring (J + 1)).mul x x) ((ring (J + 1)).neg (ring (J + 1)).one) cs

def T (e : Nat) (cs : List Nat) : Zp (J + 1) :=
  sumK (ring (J + 1)) (fun x => (ring (J + 1)).mul (pw (ring (J + 1)) x e) (evs J x cs)) (CMInert.allZ (J + 1) (J + 1 + 1))

theorem T_cons (e c : Nat) (cs : List Nat) :
    T J e (c :: cs) = (ring (J + 1)).add ((ring (J + 1)).mul (nsm (ring (J + 1)) c (pw (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one) cs.length)) (ps J e)) (T J (e + 2) cs) := by
  have pt : ∀ x : Zp (J + 1), (ring (J + 1)).mul (pw (ring (J + 1)) x e) (evs J x (c :: cs))
      = (ring (J + 1)).add ((ring (J + 1)).mul (nsm (ring (J + 1)) c (pw (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one) cs.length)) (pw (ring (J + 1)) x e)) ((ring (J + 1)).mul (pw (ring (J + 1)) x (e + 2)) (evs J x cs)) :=
    fun x => by
      show (ring (J + 1)).mul (pw (ring (J + 1)) x e) ((ring (J + 1)).add (nsm (ring (J + 1)) c (pw (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one) cs.length)) ((ring (J + 1)).mul ((ring (J + 1)).mul x x) (evs J x cs))) = _
      rw [(ring (J + 1)).distrib, (ring (J + 1)).mul_comm (pw (ring (J + 1)) x e) (nsm (ring (J + 1)) c _)]
      show _ = (ring (J + 1)).add _ ((ring (J + 1)).mul ((ring (J + 1)).mul ((ring (J + 1)).mul (pw (ring (J + 1)) x e) x) x) (evs J x cs))
      rw [(ring (J + 1)).mul_assoc ((ring (J + 1)).mul (pw (ring (J + 1)) x e) x) x, (ring (J + 1)).mul_assoc (pw (ring (J + 1)) x e) x, ← (ring (J + 1)).mul_assoc x x]
  show sumK (ring (J + 1)) _ _ = _
  rw [sumK_ext (ring (J + 1)) _ _ pt, sumK_add, ← mul_sumK]
  rfl

theorem idx2 (e i : Nat) : e + 2 + 2 * i = e + 2 * (i + 1) := by
  rw [Nat.add_assoc, Nat.add_comm 2 (2 * i)]
  rfl

theorem sumK_zero' {A : Type} : ∀ l : List A, sumK (ring (J + 1)) (fun _ => (ring (J + 1)).zero) l = (ring (J + 1)).zero
  | [] => rfl
  | _ :: l => by
    show (ring (J + 1)).add (ring (J + 1)).zero (sumK (ring (J + 1)) (fun _ => (ring (J + 1)).zero) l) = (ring (J + 1)).zero
    rw [sumK_zero' l, (ring (J + 1)).zero_add]

theorem T_nil (e : Nat) : T J e [] = (ring (J + 1)).zero := by
  show sumK (ring (J + 1)) (fun x => (ring (J + 1)).mul (pw (ring (J + 1)) x e) (ring (J + 1)).zero) _ = _
  rw [sumK_ext (ring (J + 1)) _ (fun _ => (ring (J + 1)).zero) (fun x => DetChain.mul_zero (ring (J + 1)) _), sumK_zero']

theorem T_zero : ∀ (cs : List Nat) (e : Nat), (∀ i, i < cs.length → ps J (e + 2 * i) = (ring (J + 1)).zero) → T J e cs = (ring (J + 1)).zero
  | [], e, _ => T_nil J e
  | c :: cs, e, h => by
    have h0 : ps J e = (ring (J + 1)).zero := h 0 (Nat.zero_lt_succ _)
    rw [T_cons, h0, DetChain.mul_zero, (ring (J + 1)).zero_add]
    exact T_zero cs (e + 2) (fun i hi => by rw [idx2]; exact h (i + 1) (Nat.succ_lt_succ hi))

theorem T_skip (e c : Nat) (cs : List Nat) (h : ps J e = (ring (J + 1)).zero) : T J e (c :: cs) = T J (e + 2) cs := by
  rw [T_cons, h, DetChain.mul_zero, (ring (J + 1)).zero_add]

theorem skip_many (n : Nat) (m : Nat) : ∀ (i j e : Nat), (∀ i', i' < i → ps J (e + 2 * i') = (ring (J + 1)).zero) →
    T J e (Frobenius.listC n j (i + m)) = T J (e + 2 * i) (Frobenius.listC n (j + i) m)
  | 0, j, e, _ => by rw [Nat.zero_add]; rfl
  | i + 1, j, e, h => by
    rw [Nat.succ_add]
    show T J e (Frobenius.C n j :: Frobenius.listC n (j + 1) (i + m)) = _
    rw [T_skip J e _ _ (h 0 (Nat.zero_lt_succ _) : ps J e = _),
      skip_many n m i (j + 1) (e + 2) (fun i' hi' => by rw [idx2]; exact h (i' + 1) (Nat.succ_lt_succ hi')),
      idx2, Nat.add_assoc j 1 i, Nat.add_comm 1 i]

theorem cub_factor (x : Zp (J + 1)) :
    CMInert.cub (ring (J + 1)) x = (ring (J + 1)).mul x ((ring (J + 1)).add ((ring (J + 1)).mul x x) ((ring (J + 1)).neg (ring (J + 1)).one)) := by
  show (ring (J + 1)).add ((ring (J + 1)).mul ((ring (J + 1)).mul x x) x) ((ring (J + 1)).neg x) = _
  rw [(ring (J + 1)).distrib, mul_neg_r (ring (J + 1)), (ring (J + 1)).mul_comm x (ring (J + 1)).one, (ring (J + 1)).one_mul, (ring (J + 1)).mul_comm ((ring (J + 1)).mul x x) x]

theorem ar1 (k : Nat) : 2 * k + 2 + 1 = k + 1 + (k + 2) := by
  rw [Nat.two_mul, Nat.add_assoc k k 2, Nat.add_assoc k (k + 2) 1, Nat.add_assoc k 1 (k + 2), Nat.add_comm 1 (k + 2)]

theorem ar2 (k : Nat) : 2 * k + 2 + 2 * (k + 1) = 2 * (2 * k + 1) + 1 + 1 := by
  show 2 * k + 2 + (2 * k + 2) = 2 * (2 * k) + 2 + 1 + 1
  rw [Nat.two_mul (2 * k), ← Nat.add_assoc, Nat.add_right_comm (2 * k) 2 (2 * k)]

theorem ar3 (k i : Nat) (hi : i ≤ k) : 2 * k + 2 + 2 * i ≤ 2 * (2 * k + 1) + 1 := by
  have a : 2 * k + 2 + 2 * i ≤ 2 * k + 2 + 2 * k := Nat.add_le_add_left (Nat.mul_le_mul_left 2 hi) _
  have b : 2 * k + 2 + 2 * k = 2 * (2 * k + 1) := by
    show 2 * k + 2 + 2 * k = 2 * (2 * k) + 2
    rw [Nat.two_mul (2 * k), Nat.add_right_comm (2 * k) 2 (2 * k)]
  rw [b] at a
  exact Nat.le_succ_of_le a

theorem ar4 (k i : Nat) (hi : i ≤ k) : 2 + 2 * i ≤ 2 * (2 * k + 1) + 1 :=
  Nat.le_trans (Nat.add_le_add_right (Nat.le_add_left 2 (2 * k)) (2 * i)) (ar3 k i hi)

/-- **The Hasse invariant of y² = x³ − x** at p = 4k + 5:
    `Σ_x (x³ − x)^((p−1)/2) = −(−1)^(k+1) C(2k+2, k+1)`. -/
theorem hasse_cm (k : Nat) (hpr : ∀ d, Dv d ((2 * (2 * k + 1) + 1) + 1 + 1) → d = 1 ∨ d = (2 * (2 * k + 1) + 1) + 1 + 1) :
    sumK (ring (2 * (2 * k + 1) + 1 + 1)) (fun x => pw (ring (2 * (2 * k + 1) + 1 + 1)) (CMInert.cub (ring (2 * (2 * k + 1) + 1 + 1)) x) (2 * k + 2)) (CMInert.allZ ((2 * (2 * k + 1) + 1) + 1) ((2 * (2 * k + 1) + 1) + 1 + 1))
      = (ring (2 * (2 * k + 1) + 1 + 1)).neg (nsm (ring (2 * (2 * k + 1) + 1 + 1)) (Frobenius.C (2 * k + 2) (k + 1)) (pw (ring (2 * (2 * k + 1) + 1 + 1)) ((ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one) (k + 1))) := by
  have eA : sumK (ring (2 * (2 * k + 1) + 1 + 1)) (fun x => pw (ring (2 * (2 * k + 1) + 1 + 1)) (CMInert.cub (ring (2 * (2 * k + 1) + 1 + 1)) x) (2 * k + 2)) (CMInert.allZ ((2 * (2 * k + 1) + 1) + 1) ((2 * (2 * k + 1) + 1) + 1 + 1))
      = T (2 * (2 * k + 1) + 1) (2 * k + 2) (Frobenius.row (2 * k + 2)) :=
    sumK_ext (ring (2 * (2 * k + 1) + 1 + 1)) _ _ (fun x => by
      rw [cub_factor (2 * (2 * k + 1) + 1) x, pw_mul, ← ev_row]
      rfl) _
  rw [eA, Frobenius.row_eq, ar1 k,
    skip_many (2 * (2 * k + 1) + 1) (2 * k + 2) (k + 2) (k + 1) 0 (2 * k + 2) (fun i' hi' =>
      ps_low (2 * (2 * k + 1) + 1) hpr _ (Nat.le_trans (Nat.succ_le_succ (Nat.zero_le _)) (Nat.le_add_right _ _))
        (ar3 k i' (Nat.le_of_lt_succ hi'))),
    Nat.zero_add]
  show T (2 * (2 * k + 1) + 1) (2 * k + 2 + 2 * (k + 1))
    (Frobenius.C (2 * k + 2) (k + 1) :: Frobenius.listC (2 * k + 2) (k + 1 + 1) (k + 1)) = _
  rw [T_cons, Frobenius.len_listC, ar2 k, ps_top (2 * (2 * k + 1) + 1) hpr,
    T_zero (2 * (2 * k + 1) + 1) _ _ (fun i hi => by
      rw [Frobenius.len_listC] at hi
      rw [Nat.add_assoc (2 * (2 * k + 1) + 1 + 1) 2 (2 * i)]
      exact ps_high (2 * (2 * k + 1) + 1) hpr _ (Nat.le_trans (Nat.succ_le_succ (Nat.zero_le 1)) (Nat.le_add_right 2 _))
        (ar4 k i (Nat.le_of_lt_succ hi))),
    DetChain.add_zero', mul_neg_r (ring (2 * (2 * k + 1) + 1 + 1)), (ring (2 * (2 * k + 1) + 1 + 1)).mul_comm _ (ring (2 * (2 * k + 1) + 1 + 1)).one, (ring (2 * (2 * k + 1) + 1 + 1)).one_mul]

end HasseCM

#print axioms HasseCM.pw_add'
#print axioms HasseCM.pw_zero_succ
#print axioms HasseCM.ps_low
#print axioms HasseCM.ps_top
#print axioms HasseCM.ps_high
#print axioms HasseCM.T_cons
#print axioms HasseCM.idx2
#print axioms HasseCM.sumK_zero'
#print axioms HasseCM.T_nil
#print axioms HasseCM.T_zero
#print axioms HasseCM.T_skip
#print axioms HasseCM.skip_many
#print axioms HasseCM.cub_factor
#print axioms HasseCM.ar1
#print axioms HasseCM.ar2
#print axioms HasseCM.ar3
#print axioms HasseCM.ar4
#print axioms HasseCM.hasse_cm

lang/HasseDavenport.lean

lean_all/lang/HasseDavenport.lean

/-
  lang/HasseDavenport — T43, cycle 101: a(𝔽_{p²}) = a_p² − 2p for y² = x³ − x at every split prime p ≡ 1 (mod 4).

  S₁ = Σ_{𝔽_p} χ(x³ − x) = −2a (a + bi primary, a² + b² = p: SignCM3), S₂ = Σ_{𝔽_{p²}} χ(z³ − z) = −2A (A + Bi primary,
  A² + B² = p²: Fq2Trace), and S₂ ≡ −S₁² (mod p) (SplitHasse). Then A ≡ 2a² ≡ a² − b² (mod p); A and a² − b² are odd, so
  they differ by 2p·s, and |A|, |a² − b²| ≤ p leaves s = 0 unless A = −p with B = 0, which is not primary. Hence A = a² − b²:
    castZ_neg, castZ_mul       the cast ℤ → K is a ring map
    castZ_zero_dvd             castZ_p z = 0 ⇒ p | z
    finish                     the integer argument above
    hasse_davenport_split      −S₂ = S₁² − 2p,  i.e.  a(𝔽_{p²}) = a_p² − 2p = α² + β²  (α + β = a_p, αβ = p)
  With BaseChange (inert primes) the Frobenius-square identity holds at every odd prime: the zeta function of the CM curve
  over 𝔽_{p²} is the one predicted by the Hecke character (π² and π̄²) — Hasse–Davenport in this case, without Jacobi sums.

  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.SplitHasse
import lang.Fq2Trace
import lang.SignCM3

namespace HasseDavenport

open FTAChain ZMod YMChain FiniteRecChain LibRing LibInt

section Cast
variable {R : Type} (K : DetChain.CRing R)

theorem castZ_neg (z : Int) : castZ K (-z) = K.neg (castZ K z) := by
  apply DetChain.neg_unique
  rw [← IntCast.castZ_add, show -z + z = 0 by lib_int]
  rfl

theorem castZ_mul_nat (x : Int) : ∀ n : Nat, castZ K (x * Int.ofNat n) = K.mul (castZ K x) (castN K n)
  | 0 => by rw [show x * Int.ofNat 0 = 0 from Int.mul_zero x]; show K.zero = K.mul _ K.zero; rw [DetChain.mul_zero]
  | n + 1 => by
    rw [show x * Int.ofNat (n + 1) = x * Int.ofNat n + x by show x * (Int.ofNat n + 1) = x * Int.ofNat n + x; lib_int, IntCast.castZ_add,
      castZ_mul_nat x n]
    show _ = K.mul (castZ K x) (K.add K.one (castN K n))
    lib_ring

theorem castZ_mul (x y : Int) : castZ K (x * y) = K.mul (castZ K x) (castZ K y) := by
  cases y with
  | ofNat n => exact castZ_mul_nat K x n
  | negSucc n =>
    rw [show x * Int.negSucc n = -(x * Int.ofNat (n + 1)) by show x * -(Int.ofNat (n + 1)) = -(x * Int.ofNat (n + 1)); lib_int, castZ_neg,
      castZ_mul_nat]
    show _ = K.mul (castZ K x) (K.neg (castN K (n + 1)))
    lib_ring

end Cast

theorem castZ_zero_dvd (m : Nat) (z : Int) (h : castZ (ring m) z = (ring m).zero) : ∃ q : Int, z = Int.ofNat (m + 1) * q := by
  cases z with
  | ofNat a =>
    have h' : mk m a = (ring m).zero := (IntCast.castN_mk m a).symm.trans h
    match md_zero_dv m a (congrArg Subtype.val h') with
    | ⟨q, hq⟩ => exact ⟨Int.ofNat q, by rw [hq]; show Int.ofNat q * Int.ofNat (m + 1) = _; lib_int⟩
  | negSucc b =>
    have h1 : castN (ring m) (b + 1) = (ring m).zero := by
      have := congrArg (ring m).neg h
      rw [show (ring m).neg (castZ (ring m) (Int.negSucc b)) = castN (ring m) (b + 1) from DetChain.neg_neg' _ _] at this
      rw [this]; exact (DetChain.neg_unique (ring m) _ _ ((ring m).zero_add _)).symm
    have h' : mk m (b + 1) = (ring m).zero := (IntCast.castN_mk m (b + 1)).symm.trans h1
    match md_zero_dv m (b + 1) (congrArg Subtype.val h') with
    | ⟨q, hq⟩ =>
      refine ⟨-Int.ofNat q, ?_⟩
      show -(Int.ofNat (b + 1)) = _
      rw [hq]; show -(Int.ofNat q * Int.ofNat (m + 1)) = _; lib_int

theorem two_mul_zero (n : Nat) (h : 2 * n = 0) : n = 0 := by
  cases n with
  | zero => rfl
  | succ n => exact Nat.noConfusion h

theorem nat_sum_zero (a b : Nat) (h : a + b = 0) : a = 0 ∧ b = 0 := by
  cases b with
  | zero => exact ⟨h, rfl⟩
  | succ b => exact Nat.noConfusion h

theorem sq_zero (x : Int) (h : x * x = 0) : x = 0 := by
  cases int_noZD x x h with
  | inl h => exact h
  | inr h => exact h

/-- **The integer finish.** -/
theorem finish (P a b A B r c : Int) (hP4 : P = 4 * c + 1) (hab : a * a + b * b = P) (hb : ∃ e, b = 2 * e)
    (ha4 : ∃ t, a + b - 1 = 4 * t) (hAB : A * A + B * B = P * P) (hB : ∃ g, B = 2 * g) (hA4 : ∃ t, A + B - 1 = 4 * t)
    (hr : A = 2 * a * a - P * r) : A = a * a - b * b := by
  match hb, ha4, hB, hA4 with
  | ⟨e, he⟩, ⟨t, ht⟩, ⟨g, hg⟩, ⟨t', ht'⟩ =>
    have haodd : a = 2 * (2 * t - e) + 1 := by
      calc a = (a + b - 1) - b + 1 := by lib_int
        _ = 2 * (2 * t - e) + 1 := by rw [ht, he]; lib_int
    have hAodd : A = 2 * (2 * t' - g) + 1 := by
      calc A = (A + B - 1) - B + 1 := by lib_int
        _ = 2 * (2 * t' - g) + 1 := by rw [ht', hg]; lib_int
    have hdiff : A - (a * a - b * b) = P * (1 - r) := by
      calc A - (a * a - b * b) = (2 * a * a - P * r) - (a * a - b * b) := by rw [hr]
        _ = (a * a + b * b) - P * r := by lib_int
        _ = P * (1 - r) := by rw [hab]; lib_int
    match GaussInt.int_even_odd (1 - r) with
    | Or.inr ⟨s, hs⟩ =>
      exfalso
      apply GaussInt.int_odd_ne_even (4 * c * s + 2 * c + s) ((2 * t' - g) - (2 * (2 * t - e) * (2 * t - e) + 2 * (2 * t - e) - 2 * e * e))
      rw [show 2 * ((2 * t' - g) - (2 * (2 * t - e) * (2 * t - e) + 2 * (2 * t - e) - 2 * e * e))
          = A - (a * a - b * b) by rw [hAodd, haodd, he]; lib_int, hdiff, hs, hP4]
      lib_int
    | Or.inl ⟨s, hs⟩ =>
      match FinCubicSign.sq_nn s with
      | ⟨0, h0⟩ =>
        have : s = 0 := sq_zero s h0
        calc A = (A - (a * a - b * b)) + (a * a - b * b) := by lib_int
          _ = a * a - b * b := by rw [hdiff, hs, this]; lib_int
      | ⟨n' + 1, hn⟩ =>
        exfalso
        match FinCubicSign.sq_nn B, FinCubicSign.sq_nn (2 * a * b), FinCubicSign.sq_nn (A + (a * a - b * b)), FinCubicSign.sq_nn P with
        | ⟨nB, hnB⟩, ⟨n2, hn2⟩, ⟨m, hm⟩, ⟨pp, hpp⟩ =>
          have key : Int.ofNat (4 * pp * n' + m + 2 * nB + 2 * n2) = 0 := by
            show 4 * Int.ofNat pp * Int.ofNat n' + Int.ofNat m + 2 * Int.ofNat nB + 2 * Int.ofNat n2 = 0
            have e1 : (A - (a * a - b * b)) * (A - (a * a - b * b)) = 4 * (P * P) * (s * s) := by rw [hdiff, hs]; lib_int
            have e2 : (a * a - b * b) * (a * a - b * b) + (2 * a * b) * (2 * a * b) = P * P := by
              rw [← hab]; lib_int
            rw [← hpp, ← hm, ← hnB, ← hn2]
            have : s * s = Int.ofNat n' + 1 := hn
            calc 4 * (P * P) * Int.ofNat n' + (A + (a * a - b * b)) * (A + (a * a - b * b)) + 2 * (B * B) + 2 * (2 * a * b * (2 * a * b))
                = ((A - (a * a - b * b)) * (A - (a * a - b * b)) + (A + (a * a - b * b)) * (A + (a * a - b * b)))
                  + 2 * (B * B) + 2 * (2 * a * b * (2 * a * b)) - 4 * (P * P) * (s * s) + 4 * (P * P) * Int.ofNat n' := by rw [e1]; lib_int
              _ = 2 * (A * A + B * B) + 2 * ((a * a - b * b) * (a * a - b * b) + (2 * a * b) * (2 * a * b)) - 4 * (P * P) * (s * s) + 4 * (P * P) * Int.ofNat n' := by lib_int
              _ = 0 := by rw [hAB, e2, this]; lib_int
          have hk := Int.ofNat.inj key
          match nat_sum_zero _ _ hk with
          | ⟨h123, h4⟩ =>
            match nat_sum_zero _ _ h123 with
            | ⟨h12, h3⟩ =>
              match nat_sum_zero _ _ h12 with
              | ⟨_, h2⟩ =>
                have hB0 : B = 0 := sq_zero B (hnB.trans (by rw [two_mul_zero nB h3]; rfl))
                have hab0 : 2 * a * b = 0 := sq_zero _ (hn2.trans (by rw [two_mul_zero n2 h4]; rfl))
                have hm0 : A + (a * a - b * b) = 0 := sq_zero _ (hm.trans (by rw [h2]; rfl))
                have hb0 : b = 0 := by
                  cases int_noZD (2 * a) b hab0 with
                  | inr h => exact h
                  | inl h =>
                    exfalso
                    have ha0 : a = 0 := FinPrimary.two_cancel a 0 (h.trans (show (0 : Int) = 2 * 0 by lib_int))
                    exact GaussInt.int_odd_ne_even (2 * t - e) 0 ((haodd.symm.trans ha0).trans (by lib_int))
                have hAP : A = -P := by
                  calc A = (A + (a * a - b * b)) - (a * a + b * b) + 2 * (b * b) := by lib_int
                    _ = -P := by rw [hm0, hab, hb0]; lib_int
                have h4 : A + B - 1 = 4 * t' := ht'
                rw [hAP, hB0, hP4] at h4
                exact GaussInt.int_odd_ne_even (t' + c) 0 (FinPrimary.two_cancel _ _ (by
                  calc 2 * (2 * (t' + c) + 1) = 4 * t' + (4 * c + 2) := by lib_int
                    _ = (-(4 * c + 1) + 0 - 1) + (4 * c + 2) := by rw [h4]
                    _ = 2 * (2 * 0) := by lib_int))

/-- **a(𝔽_{p²}) = a_p² − 2p at every split prime p = 4k + 5** (y² = x³ − x). -/
theorem hasse_davenport_split (k : Nat) (hpr : ∀ d, Dv d (2 * (2 * k + 1) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * k + 1) + 1 + 1 + 1)) (n : Zp (2 * (2 * k + 1) + 1 + 1))
    (hn : Frobenius.pw (ring (2 * (2 * k + 1) + 1 + 1)) n (2 * k + 1 + 1) = (ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one) :
    -sumK LibInt.intR (fun x => FinField.chi (Fq2Inst.ffq (2 * k + 1) hpr n hn) (FinSign.f (Fq2Inst.ffq (2 * k + 1) hpr n hn) x)) (Fq2Inst.ffq (2 * k + 1) hpr n hn).els = (-sumK LibInt.intR (fun x => QChar.chi (2 * k + 1) (CMInert.cub (ring (2 * (2 * k + 1) + 1 + 1)) x)) (ConicSum.L (2 * k + 1))) * (-sumK LibInt.intR (fun x => QChar.chi (2 * k + 1) (CMInert.cub (ring (2 * (2 * k + 1) + 1 + 1)) x))  -- (+79 chars; full line in the Zenodo source)
  match Fq2Trace.fq_ap_primary (2 * k + 1) hpr n hn, SignCM3.ap_primary k hpr with
  | ⟨A, B, hAB, ⟨hB, hA4⟩, hS2⟩, ⟨a, b, hab, ⟨hb, ha4⟩, hS1⟩ =>
    have hsp := SplitHasse.split_hasse k hpr n hn
    generalize sumK LibInt.intR (fun x => FinField.chi (Fq2Inst.ffq (2 * k + 1) hpr n hn) (FinSign.f (Fq2Inst.ffq (2 * k + 1) hpr n hn) x)) (Fq2Inst.ffq (2 * k + 1) hpr n hn).els = T2 at hS2 hsp ⊢
    generalize sumK LibInt.intR (fun x => QChar.chi (2 * k + 1) (CMInert.cub (ring (2 * (2 * k + 1) + 1 + 1)) x)) (ConicSum.L (2 * k + 1)) = T1 at hS1 hsp ⊢
    have h0 : castZ (ring (2 * (2 * k + 1) + 1 + 1)) (T2 + T1 * T1) = (ring (2 * (2 * k + 1) + 1 + 1)).zero := by
      rw [IntCast.castZ_add, castZ_mul, hsp]; lib_ring
    match castZ_zero_dvd (2 * (2 * k + 1) + 1 + 1) _ h0 with
    | ⟨q, hq⟩ =>
      have hP : Int.ofNat (2 * (2 * k + 1) + 1 + 1 + 1) = 4 * (Int.ofNat k + 1) + 1 := by
        show 2 * (2 * Int.ofNat k + 1) + 1 + 1 + 1 = _; lib_int
      have e2 : T2 = -(2 * A) := by rw [← hS2]; lib_int
      have e1 : T1 = -(2 * a) := by rw [← hS1]; lib_int
      have hX : 2 * (2 * a * a - A) = Int.ofNat (2 * (2 * k + 1) + 1 + 1 + 1) * q := by rw [← hq, e2, e1]; lib_int
      have hr : A = 2 * a * a - Int.ofNat (2 * (2 * k + 1) + 1 + 1 + 1) * ((2 * Int.ofNat k + 3) * q - (2 * a * a - A)) := by
        calc A = 2 * a * a - ((2 * Int.ofNat k + 3) * (2 * (2 * a * a - A)) - (4 * Int.ofNat k + 5) * (2 * a * a - A)) := by lib_int
          _ = _ := by rw [hX, hP]; lib_int
      have hfin := finish (Int.ofNat (2 * (2 * k + 1) + 1 + 1 + 1)) a b A B _ (Int.ofNat k + 1) hP hab hb ha4 hAB hB hA4 hr
      rw [e2, e1, hfin, ← hab]
      lib_int

end HasseDavenport

#print axioms HasseDavenport.castZ_neg
#print axioms HasseDavenport.castZ_mul_nat
#print axioms HasseDavenport.castZ_mul
#print axioms HasseDavenport.castZ_zero_dvd
#print axioms HasseDavenport.two_mul_zero
#print axioms HasseDavenport.nat_sum_zero
#print axioms HasseDavenport.sq_zero
#print axioms HasseDavenport.finish
#print axioms HasseDavenport.hasse_davenport_split

lang/HasseDavenport3.lean

lean_all/lang/HasseDavenport3.lean

/-
  lang/HasseDavenport3 — T43, cycle 104: a(𝔽_{p²}) = a_p² − 2p for y² = x³ + 1 at every split prime p ≡ 1 (mod 3).

  −ψ_p(1) = 2a with p = a² + 3b², a ≡ 1 (mod 3) (CubicSign); −ψ_{p²}(1) = 2A with p² = A² + 3B², A ≡ 1 (mod 3) (FinCubicSign
  over 𝔽_{p²}); ψ_{p²} ≡ −ψ_p² (mod p) (SplitHasse3). Then A ≡ 2a² ≡ a² − 3b² (mod p); both are ≡ 1 (mod 3) and p ≡ 1 (mod 3),
  so they differ by 3ps, and (A − A')² + (A + A')² + 6B² + 6(2ab)² = 4p² leaves s = 0:
    finish3                  the integer argument
    hasse_davenport_split3   −ψ_{p²}(1) = ψ_p(1)² − 2p, i.e. a(𝔽_{p²}) = a_p² − 2p
  With CubicSuper2 (inert p) this holds at every prime p > 3: the Frobenius-square identity for the CM curve of ℚ(√−3).

  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.SplitHasse3
import lang.HasseDavenport
import lang.FinCubicSign
import lang.CubicSuper2

namespace HasseDavenport3

open FTAChain ZMod YMChain FiniteRecChain LibRing LibInt HasseDavenport

theorem finish3 (P a b A B r t : Int) (hP : P = 6 * t + 7) (hP0 : P ≠ 0) (hab : a * a + 3 * (b * b) = P)
    (ha3 : ∃ u, a - 1 = 3 * u) (hAB : A * A + 3 * (B * B) = P * P) (hA3 : ∃ v, A - 1 = 3 * v)
    (hr : A = 2 * a * a - P * r) : A = a * a - 3 * (b * b) := by
  match ha3, hA3 with
  | ⟨u, hu⟩, ⟨v, hv⟩ =>
    have hdiff : A - (a * a - 3 * (b * b)) = P * (1 - r) := by
      calc A - (a * a - 3 * (b * b)) = (2 * a * a - P * r) - (a * a - 3 * (b * b)) := by rw [hr]
        _ = (a * a + 3 * (b * b)) - P * r := by lib_int
        _ = P * (1 - r) := by rw [hab]; lib_int
    have ha' : a = 3 * u + 1 := by calc a = (a - 1) + 1 := by lib_int
                                         _ = 3 * u + 1 := by rw [hu]
    have hA' : A = 3 * v + 1 := by calc A = (A - 1) + 1 := by lib_int
                                        _ = 3 * v + 1 := by rw [hv]
    -- 1 − r = 3 s'
    have hs : 1 - r = 3 * (v - 3 * u * u - 2 * u + b * b - (2 * t + 2) * (1 - r)) := by
      calc 1 - r = P * (1 - r) - (6 * t + 6) * (1 - r) := by rw [hP]; lib_int
        _ = (A - (a * a - 3 * (b * b))) - (6 * t + 6) * (1 - r) := by rw [hdiff]
        _ = _ := by rw [hA', ha']; lib_int
    generalize v - 3 * u * u - 2 * u + b * b - (2 * t + 2) * (1 - r) = s at hs
    match FinCubicSign.sq_nn s with
    | ⟨0, h0⟩ =>
      have : s = 0 := sq_zero s h0
      calc A = (A - (a * a - 3 * (b * b))) + (a * a - 3 * (b * b)) := by lib_int
        _ = a * a - 3 * (b * b) := by rw [hdiff, hs, this]; lib_int
    | ⟨n' + 1, hn⟩ =>
      exfalso
      match FinCubicSign.sq_nn B, FinCubicSign.sq_nn (2 * a * b), FinCubicSign.sq_nn (A + (a * a - 3 * (b * b))), FinCubicSign.sq_nn P with
      | ⟨nB, hnB⟩, ⟨n2, hn2⟩, ⟨m, hm⟩, ⟨pp, hpp⟩ =>
        have key : Int.ofNat (5 * pp + 9 * pp * n' + m + 6 * nB + 6 * n2) = 0 := by
          show 5 * Int.ofNat pp + 9 * Int.ofNat pp * Int.ofNat n' + Int.ofNat m + 6 * Int.ofNat nB + 6 * Int.ofNat n2 = 0
          have e1 : (A - (a * a - 3 * (b * b))) * (A - (a * a - 3 * (b * b))) = 9 * (P * P) * (s * s) := by rw [hdiff, hs]; lib_int
          have e2 : (a * a - 3 * (b * b)) * (a * a - 3 * (b * b)) + 3 * ((2 * a * b) * (2 * a * b)) = P * P := by
            rw [← hab]; lib_int
          have hss : s * s = Int.ofNat n' + 1 := hn
          rw [← hpp, ← hm, ← hnB, ← hn2]
          calc 5 * (P * P) + 9 * (P * P) * Int.ofNat n' + (A + (a * a - 3 * (b * b))) * (A + (a * a - 3 * (b * b))) + 6 * (B * B) + 6 * (2 * a * b * (2 * a * b))
              = ((A - (a * a - 3 * (b * b))) * (A - (a * a - 3 * (b * b))) + (A + (a * a - 3 * (b * b))) * (A + (a * a - 3 * (b * b))))
                + 6 * (B * B) + 6 * (2 * a * b * (2 * a * b)) - 9 * (P * P) * (s * s) + 9 * (P * P) * Int.ofNat n' + 5 * (P * P) := by rw [e1]; lib_int
            _ = 2 * (A * A + 3 * (B * B)) + 2 * ((a * a - 3 * (b * b)) * (a * a - 3 * (b * b)) + 3 * ((2 * a * b) * (2 * a * b)))
                - 9 * (P * P) * (s * s) + 9 * (P * P) * Int.ofNat n' + 5 * (P * P) := by lib_int
            _ = 0 := by rw [hAB, e2, hss]; lib_int
        have hk := Int.ofNat.inj key
        match nat_sum_zero _ _ hk with
        | ⟨h1, _⟩ =>
          match nat_sum_zero _ _ h1 with
          | ⟨h2, _⟩ =>
            match nat_sum_zero _ _ h2 with
            | ⟨h3, _⟩ =>
              match nat_sum_zero _ _ h3 with
              | ⟨h4, _⟩ =>
                have hpp0 : pp = 0 := by
                  cases pp with
                  | zero => rfl
                  | succ q => exact Nat.noConfusion h4
                exact hP0 (sq_zero P (hpp.trans (by rw [hpp0]; rfl)))

/-- **a(𝔽_{p²}) = a_p² − 2p for y² = x³ + 1 at every split prime p = 6t + 7.** -/
theorem hasse_davenport_split3 (t : Nat) (hpr : ∀ d, Dv d (2 * (3 * t + 2) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (3 * t + 2) + 1 + 1 + 1)) (n : Zp (2 * (3 * t + 2) + 1 + 1))
    (hn : Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) n (3 * t + 2 + 1) = (ring (2 * (3 * t + 2) + 1 + 1)).neg (ring (2 * (3 * t + 2) + 1 + 1)).one) :
    -FinCubic.psi (Fq2Inst.ffq (3 * t + 2) hpr n hn) (Fq2Inst.ffq (3 * t + 2) hpr n hn).K.one = (-CubicJacob.psi (3 * t + 2) (ring (2 * (3 * t + 2) + 1 + 1)).one) * (-CubicJacob.psi (3 * t + 2) (ring (2 * (3 * t + 2) + 1 + 1)).one) + -2 * Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) := by
  have hN' : 2 * (Fq2Inst.ffq (3 * t + 2) hpr n hn).h = 3 * (2 * (6 * t * t + 14 * t + 7) + 2) := by
    rw [CubicSuper2.h_eq (3 * t + 2) hpr n hn, SplitHasse3.hF3]; lib_nat
  match FinCubicSign.fin_cubic_exact (Fq2Inst.ffq (3 * t + 2) hpr n hn) _ hN', CubicSign.cubic_exact (3 * t + 2) hpr (2 * t + 2) (by lib_nat) with
  | ⟨A, B, hAB, hA3, hS2⟩, ⟨a, b, hab, ha3, hS1⟩ =>
    have hAB' : A * A + 3 * (B * B) = Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) * Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) := by
      rw [hAB, CubicSuper2.h_eq (3 * t + 2) hpr n hn, CubicSuper.sq_id]; rfl
    have hsp := SplitHasse3.split_hasse3 t hpr n hn
    generalize FinCubic.psi (Fq2Inst.ffq (3 * t + 2) hpr n hn) (Fq2Inst.ffq (3 * t + 2) hpr n hn).K.one = T2 at hS2 hsp ⊢
    generalize CubicJacob.psi (3 * t + 2) (ring (2 * (3 * t + 2) + 1 + 1)).one = T1 at hS1 hsp ⊢
    have h0 : castZ (ring (2 * (3 * t + 2) + 1 + 1)) (T2 + T1 * T1) = (ring (2 * (3 * t + 2) + 1 + 1)).zero := by
      rw [IntCast.castZ_add, castZ_mul, hsp]; lib_ring
    match castZ_zero_dvd (2 * (3 * t + 2) + 1 + 1) _ h0 with
    | ⟨q, hq⟩ =>
      have hP : Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) = 6 * Int.ofNat t + 7 := by
        show 2 * (3 * Int.ofNat t + 2) + 1 + 1 + 1 = _; lib_int
      have e2 : T2 = -(2 * A) := by rw [← hS2]; lib_int
      have e1 : T1 = -(2 * a) := by rw [← hS1]; lib_int
      have hX : 2 * (2 * a * a - A) = Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) * q := by rw [← hq, e2, e1]; lib_int
      have hr : A = 2 * a * a - Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) * ((3 * Int.ofNat t + 4) * q - (2 * a * a - A)) := by
        calc A = 2 * a * a - ((3 * Int.ofNat t + 4) * (2 * (2 * a * a - A)) - (6 * Int.ofNat t + 7) * (2 * a * a - A)) := by lib_int
          _ = _ := by rw [hX, hP]; lib_int
      have hP0 : Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) ≠ 0 := fun e => Nat.noConfusion (Int.ofNat.inj e)
      have hfin := finish3 (Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1)) a b A B _ (Int.ofNat t) hP hP0 hab ha3 hAB' hA3 hr
      rw [e2, e1, hfin, ← hab]
      lib_int

end HasseDavenport3

#print axioms HasseDavenport3.finish3
#print axioms HasseDavenport3.hasse_davenport_split3

lang/HasseEisen.lean

lean_all/lang/HasseEisen.lean

/-
  lang/HasseEisen — T43, cycle 40: the Hasse invariant of y² = x³ + 1 at split primes p = 6k + 7.

  p = J + 2, h = (p − 1)/2 = 3k + 3. (x³ + 1)^h = Σ_i C(h, i) x^(3i) (Frobenius.ev_row with a = x³, b = 1); summing over
  x with the power sums (PowerSum, HasseCM.ps): the exponent 0 gives p · 1 = 0, the exponents 3i ∈ [3, p − 2] and
  [p, 2p − 3] give 0, and 3i = p − 1 (i = 2k + 2) gives −1:
    ps_zero          Σ_x 1 = 0
    T3, T3_cons, T3_zero, T3_skip, skip3_many    one coefficient at a time
    hasse_eisen      Σ_x (x³ + 1)^((p−1)/2) = −C(3k + 3, 2k + 2)
  So a_p(y² = x³ + 1) ≡ C((p−1)/2, (p−1)/3) (mod p) at every split prime of ℤ[ω] — the companion of CMEisen
  (inert primes) and of HasseCM for ℤ[i].

  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.HasseCM
import lib.Semi

namespace HasseEisen

open FTAChain ZMod YMChain Frobenius HasseCM

variable (J : Nat)

theorem sum_one_partial : ∀ n, n ≤ J + 1 →
    sumK (ring (J + 1)) (fun x => pw (ring (J + 1)) x 0) (CMInert.allZ (J + 1) (n + 1)) = mk (J + 1) (n + 1)
  | 0, _ => by
    show (ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).zero = mk (J + 1) 1
    rw [DetChain.add_zero']; rfl
  | n + 1, hn => by
    show sumK (ring (J + 1)) _ (CMInert.allZ (J + 1) (n + 1) ++ [mk (J + 1) (n + 1)]) = _
    rw [CMInert.sumK_app', sum_one_partial n (Nat.le_of_succ_le hn)]
    show (ring (J + 1)).add (mk (J + 1) (n + 1)) ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).zero) = _
    rw [DetChain.add_zero', TwoPower.mk_add_one]

theorem ps_zero : ps J 0 = (ring (J + 1)).zero := by
  show sumK (ring (J + 1)) (fun x => pw (ring (J + 1)) x 0) (CMInert.allZ (J + 1) (J + 1 + 1)) = _
  rw [sum_one_partial J (J + 1) (Nat.le_refl _)]
  exact Subtype.ext (md_self (J + 1))

def ev3 (x : Zp (J + 1)) (cs : List Nat) : Zp (J + 1) := ev (ring (J + 1)) (pw (ring (J + 1)) x 3) (ring (J + 1)).one cs

def T3 (e : Nat) (cs : List Nat) : Zp (J + 1) :=
  sumK (ring (J + 1)) (fun x => (ring (J + 1)).mul (pw (ring (J + 1)) x e) (ev3 J x cs)) (CMInert.allZ (J + 1) (J + 1 + 1))

theorem T3_cons (e c : Nat) (cs : List Nat) :
    T3 J e (c :: cs) = (ring (J + 1)).add ((ring (J + 1)).mul (nsm (ring (J + 1)) c (ring (J + 1)).one) (ps J e)) (T3 J (e + 3) cs) := by
  have pt : ∀ x : Zp (J + 1), (ring (J + 1)).mul (pw (ring (J + 1)) x e) (ev3 J x (c :: cs))
      = (ring (J + 1)).add ((ring (J + 1)).mul (nsm (ring (J + 1)) c (ring (J + 1)).one) (pw (ring (J + 1)) x e)) ((ring (J + 1)).mul (pw (ring (J + 1)) x (e + 3)) (ev3 J x cs)) := fun x => by
    show (ring (J + 1)).mul (pw (ring (J + 1)) x e) ((ring (J + 1)).add (nsm (ring (J + 1)) c (pw (ring (J + 1)) (ring (J + 1)).one cs.length)) ((ring (J + 1)).mul (pw (ring (J + 1)) x 3) (ev3 J x cs))) = _
    rw [TwoPower.pwone, (ring (J + 1)).distrib, (ring (J + 1)).mul_comm (pw (ring (J + 1)) x e) (nsm (ring (J + 1)) c _), ← (ring (J + 1)).mul_assoc, ← pw_add' (ring (J + 1)) x e 3]
  show sumK (ring (J + 1)) _ _ = _
  rw [sumK_ext (ring (J + 1)) _ _ pt, sumK_add, ← mul_sumK]
  rfl

theorem T3_nil (e : Nat) : T3 J e [] = (ring (J + 1)).zero := by
  show sumK (ring (J + 1)) (fun x => (ring (J + 1)).mul (pw (ring (J + 1)) x e) (ring (J + 1)).zero) _ = _
  rw [sumK_ext (ring (J + 1)) _ (fun _ => (ring (J + 1)).zero) (fun x => DetChain.mul_zero (ring (J + 1)) _), sumK_zero']

theorem idx3 (e i : Nat) : e + 3 + 3 * i = e + 3 * (i + 1) := by lib_nat

theorem T3_zero : ∀ (cs : List Nat) (e : Nat), (∀ i, i < cs.length → ps J (e + 3 * i) = (ring (J + 1)).zero) → T3 J e cs = (ring (J + 1)).zero
  | [], e, _ => T3_nil J e
  | c :: cs, e, h => by
    have h0 : ps J e = (ring (J + 1)).zero := h 0 (Nat.zero_lt_succ _)
    rw [T3_cons, h0, DetChain.mul_zero, (ring (J + 1)).zero_add]
    exact T3_zero cs (e + 3) (fun i hi => by rw [idx3]; exact h (i + 1) (Nat.succ_lt_succ hi))

theorem skip3_many (n m : Nat) : ∀ (i j e : Nat), (∀ i', i' < i → ps J (e + 3 * i') = (ring (J + 1)).zero) →
    T3 J e (Frobenius.listC n j (i + m)) = T3 J (e + 3 * i) (Frobenius.listC n (j + i) m)
  | 0, j, e, _ => by rw [Nat.zero_add]; rfl
  | i + 1, j, e, h => by
    rw [Nat.succ_add]
    show T3 J e (Frobenius.C n j :: Frobenius.listC n (j + 1) (i + m)) = _
    have h0 : ps J e = (ring (J + 1)).zero := h 0 (Nat.zero_lt_succ _)
    rw [T3_cons, h0, DetChain.mul_zero, (ring (J + 1)).zero_add,
      skip3_many n m i (j + 1) (e + 3) (fun i' hi' => by rw [idx3]; exact h (i' + 1) (Nat.succ_lt_succ hi')),
      idx3, Nat.add_assoc j 1 i, Nat.add_comm 1 i]

/-- **The Hasse invariant of y² = x³ + 1** at p = 6k + 7:
    `Σ_x (x³ + 1)^((p−1)/2) = −C(3k + 3, 2k + 2)`. -/
theorem hasse_eisen (k : Nat) (hpr : ∀ d, Dv d ((6 * k + 5) + 1 + 1) → d = 1 ∨ d = (6 * k + 5) + 1 + 1) :
    sumK (ring ((6 * k + 5) + 1)) (fun x => pw (ring ((6 * k + 5) + 1)) ((ring ((6 * k + 5) + 1)).add (pw (ring ((6 * k + 5) + 1)) x 3) (ring ((6 * k + 5) + 1)).one) (3 * k + 3)) (CMInert.allZ ((6 * k + 5) + 1) ((6 * k + 5) + 1 + 1))
      = (ring ((6 * k + 5) + 1)).neg (nsm (ring ((6 * k + 5) + 1)) (Frobenius.C (3 * k + 3) (2 * k + 2)) (ring ((6 * k + 5) + 1)).one) := by
  have eA : sumK (ring ((6 * k + 5) + 1)) (fun x => pw (ring ((6 * k + 5) + 1)) ((ring ((6 * k + 5) + 1)).add (pw (ring ((6 * k + 5) + 1)) x 3) (ring ((6 * k + 5) + 1)).one) (3 * k + 3)) (CMInert.allZ ((6 * k + 5) + 1) ((6 * k + 5) + 1 + 1))
      = T3 (6 * k + 5) 0 (Frobenius.row (3 * k + 3)) :=
    sumK_ext (ring ((6 * k + 5) + 1)) _ _ (fun x => by
      show _ = (ring ((6 * k + 5) + 1)).mul (ring ((6 * k + 5) + 1)).one (ev3 (6 * k + 5) x (Frobenius.row (3 * k + 3)))
      rw [(ring ((6 * k + 5) + 1)).one_mul, ev3, ev_row]) _
  rw [eA, Frobenius.row_eq, show 3 * k + 3 + 1 = (2 * k + 2) + (k + 2) by lib_nat,
    skip3_many (6 * k + 5) (3 * k + 3) (k + 2) (2 * k + 2) 0 0 (fun i' hi' => by
      cases i' with
      | zero => exact ps_zero (6 * k + 5)
      | succ i'' =>
        apply ps_low (6 * k + 5) hpr
        · rw [show 0 + 3 * (i'' + 1) = 1 + (3 * i'' + 2) by lib_nat]; exact Nat.le_add_right 1 _
        · have hi : i'' ≤ 2 * k := Nat.le_of_succ_le_succ (Nat.le_of_lt_succ hi')
          have a : 0 + 3 * (i'' + 1) ≤ 0 + 3 * (2 * k + 1) := Nat.add_le_add_left (Nat.mul_le_mul_left 3 (Nat.succ_le_succ hi)) 0
          exact Nat.le_trans a (by rw [show 0 + 3 * (2 * k + 1) = 6 * k + 3 by lib_nat]; exact Nat.le_succ_of_le (Nat.le_succ _)))]
  rw [show 0 + (2 * k + 2) = 2 * k + 2 by lib_nat]
  show T3 (6 * k + 5) (0 + 3 * (2 * k + 2)) (Frobenius.C (3 * k + 3) (2 * k + 2) :: Frobenius.listC (3 * k + 3) (2 * k + 2 + 1) (k + 1)) = _
  rw [T3_cons, show 0 + 3 * (2 * k + 2) = (6 * k + 5) + 1 by lib_nat, ps_top (6 * k + 5) hpr,
    T3_zero (6 * k + 5) _ _ (fun i hi => by
      rw [Frobenius.len_listC] at hi
      rw [show (6 * k + 5) + 1 + 3 + 3 * i = (6 * k + 5) + 1 + (3 * i + 3) by lib_nat]
      apply ps_high (6 * k + 5) hpr
      · exact Nat.le_add_left 1 _
      · have a : 3 * i + 3 ≤ 3 * k + 3 := Nat.add_le_add_right (Nat.mul_le_mul_left 3 (Nat.le_of_lt_succ hi)) 3
        exact Nat.le_trans a (by rw [show 6 * k + 5 = (3 * k + 3) + (3 * k + 2) by lib_nat]; exact Nat.le_add_right _ _)),
    DetChain.add_zero', mul_neg_r (ring ((6 * k + 5) + 1)), (ring ((6 * k + 5) + 1)).mul_comm _ (ring ((6 * k + 5) + 1)).one, (ring ((6 * k + 5) + 1)).one_mul]

end HasseEisen

#print axioms HasseEisen.sum_one_partial
#print axioms HasseEisen.ps_zero
#print axioms HasseEisen.T3_cons
#print axioms HasseEisen.T3_nil
#print axioms HasseEisen.idx3
#print axioms HasseEisen.T3_zero
#print axioms HasseEisen.skip3_many
#print axioms HasseEisen.hasse_eisen

lang/Hecke32.lean

lean_all/lang/Hecke32.lean

/-
  lang/Hecke32 — T47, cycle 565: **η(4z)²η(8z)² = Σ_n Re(c_n) qⁿ at every n**: the coefficient of qⁿ in the eta product of 32a
  (computed in ℤ[q]/(q^(n+1))) equals the real part of the ℚ(i) Hecke coefficient c_n = Σ_{α primary, N(α) = n} α (HeckeMult.c).

    shn_add, shn_smul, shn_zero, shn_shn, shn_delta1, shn_delta0     shifts are linear, compose, and pick one index
    pw_neg_one, emb_int, sq_natAbs, gn_phi_nat                        bookkeeping in ℤ
    term, inner                                                       one e at a time
    eta32_hecke
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Re32
import lang.Eta32C

namespace Hecke32

open DetChain YMChain HeckeMult GaussEuclid GaussUnique

theorem shn_add : ∀ E (f g : Nat → Int) k, Trunc.shn Z E (fun i => Z.add (f i) (g i)) k = Z.add (Trunc.shn Z E f k) (Trunc.shn Z E g k)
  | 0, _, _, _ => rfl
  | _ + 1, _, _, 0 => by show Z.zero = Z.add Z.zero Z.zero; lib_ring
  | E + 1, f, g, k + 1 => shn_add E f g k

theorem shn_smul (c : Int) : ∀ E (g : Nat → Int) k, Trunc.shn Z E (fun i => Z.mul c (g i)) k = Z.mul c (Trunc.shn Z E g k)
  | 0, _, _ => rfl
  | _ + 1, _, 0 => by show Z.zero = Z.mul c Z.zero; lib_ring
  | E + 1, g, k + 1 => shn_smul c E g k

theorem shn_zero : ∀ E k, Trunc.shn Z E (fun _ => Z.zero) k = Z.zero
  | 0, _ => rfl
  | _ + 1, 0 => rfl
  | E + 1, k + 1 => shn_zero E k

theorem shn_shn (b : Nat) (f : Nat → Int) : ∀ a k, Trunc.shn Z a (Trunc.shn Z b f) k = Trunc.shn Z (b + a) f k
  | 0, _ => rfl
  | _ + 1, 0 => rfl
  | a + 1, k + 1 => shn_shn b f a k

theorem shn_delta1 (D : Nat) : Trunc.shn Z D (GLnLocal.delta Z) D = Z.one := Trunc.shn_ge Z D _ 0

theorem shn_delta0 (D n : Nat) (h : ¬ D = n) : Trunc.shn Z D (GLnLocal.delta Z) n = Z.zero := by
  cases Nat.lt_or_ge n D with
  | inl hl => exact Trunc.shn_lt Z D _ n hl
  | inr hg =>
    obtain ⟨t, ht⟩ := Cheb.le_dest2 D n hg
    rw [← ht, Nat.add_comm, Trunc.shn_ge]
    cases t with
    | zero => exact absurd (show D = 0 + D by lib_nat) (fun e => h (e.trans ht))
    | succ t => rfl

theorem pw_neg_one : ∀ k, Frobenius.pw Z (Z.neg Z.one) k = sg2 k
  | 0 => rfl
  | k + 1 => by
    show Frobenius.pw Z (Z.neg Z.one) k * (-1) = -sg2 k
    rw [pw_neg_one k]; lib_int

theorem emb_int : ∀ m, Cheb.emb Z m = Int.ofNat m
  | 0 => rfl
  | m + 1 => by show Cheb.emb Z m + 1 = Int.ofNat m + 1; rw [emb_int m]

theorem sq_natAbs (e : Int) : e * e = Int.ofNat (Int.natAbs e) * Int.ofNat (Int.natAbs e) := by
  cases e with
  | ofNat k => rfl
  | negSucc k => show (-(Int.ofNat k + 1)) * (-(Int.ofNat k + 1)) = (Int.ofNat k + 1) * (Int.ofNat k + 1); lib_int

theorem gn_phi_nat (e : Int) (j : Nat) :
    gn (phi e j) = Int.ofNat (4 * (Int.natAbs e * Int.natAbs e) + 1 + 4 * (j * j + j)) := by
  rw [phi_norm, sq_natAbs]
  show _ = 4 * (Int.ofNat (Int.natAbs e) * Int.ofNat (Int.natAbs e)) + 1 + 4 * (Int.ofNat j * Int.ofNat j + Int.ofNat j)
  lib_int

theorem term (n : Nat) (e : Int) (J : Nat) :
    Z.mul (Frobenius.pw Z (Z.neg Z.one) (Int.natAbs e)) (Z.mul (Frobenius.pw Z (Z.neg Z.one) J) (Z.mul (Cheb.emb Z (2 * J + 1))
      (Trunc.shn Z (4 * (Int.natAbs e * Int.natAbs e) + 1 + 4 * (J * J + J)) (GLnLocal.delta Z) n))) = F n (phi e J) := by
  rw [pw_neg_one, pw_neg_one, emb_int]
  by_cases hD : 4 * (Int.natAbs e * Int.natAbs e) + 1 + 4 * (J * J + J) = n
  · have hp : PD n (phi e J) := ⟨by rw [gn_phi_nat, hD], phi_prim e J⟩
    rw [F_pos n _ hp, hD, shn_delta1]
    show sg2 (Int.natAbs e) * (sg2 J * ((2 * Int.ofNat J + 1) * 1)) = sg2 (Int.natAbs e + J) * (2 * Int.ofNat J + 1)
    rw [sg2_add]; lib_int
  · have hp : ¬ PD n (phi e J) := fun h => hD (Int.ofNat.inj (by rw [← gn_phi_nat]; exact h.1))
    rw [F_neg n _ hp, shn_delta0 _ n hD]; lib_ring

theorem inner (n : Nat) (e : Int) : ∀ J,
    Z.mul (Frobenius.pw Z (Z.neg Z.one) (Int.natAbs e)) (Trunc.shn Z (4 * (Int.natAbs e * Int.natAbs e) + 1) (Trunc.tc Z J) n) =
      sumK Z (fun j => F n (phi e j)) (jl J)
  | 0 => by
    show Z.mul _ (Trunc.shn Z _ (fun _ => Z.zero) n) = Z.zero
    rw [shn_zero]; lib_ring
  | J + 1 => by
    rw [show jl (J + 1) = jl J ++ [J] from rfl, sumK_append, ← inner n e J]
    show _ = Z.add _ (Z.add (F n (phi e J)) Z.zero)
    rw [← term n e J]
    show Z.mul _ (Trunc.shn Z _ (fun i => Z.add (Trunc.tc Z J i) (Z.mul (Frobenius.pw Z (Z.neg Z.one) J)
      (Z.mul (Cheb.emb Z (2 * J + 1)) (Trunc.shn Z (4 * (J * J + J)) (GLnLocal.delta Z) i)))) n) = _
    rw [shn_add, shn_smul, shn_smul, shn_shn, show 4 * (J * J + J) + (4 * (Int.natAbs e * Int.natAbs e) + 1) =
      4 * (Int.natAbs e * Int.natAbs e) + 1 + 4 * (J * J + J) by lib_nat]
    lib_ring

/-- **The 32a eta product is the ℚ(i) Hecke theta series at every coefficient.** -/
theorem eta32_hecke (n : Nat) :
    Trunc.nth Z (n + 1) ((Trunc.TR Z (n + 1)).mul (Trunc.qe Z (n + 1)) ((Trunc.TR Z (n + 1)).mul
      ((Trunc.TR Z (n + 1)).mul (QBinom.pp (Trunc.TR Z (n + 1)) (Frobenius.pw (Trunc.TR Z (n + 1)) (Trunc.qe Z (n + 1)) 4) (n + 1))
        (QBinom.pp (Trunc.TR Z (n + 1)) (Frobenius.pw (Trunc.TR Z (n + 1)) (Trunc.qe Z (n + 1)) 4) (n + 1)))
      ((Trunc.TR Z (n + 1)).mul
        (QBinom.pp (Trunc.TR Z (n + 1)) ((Trunc.TR Z (n + 1)).mul (Frobenius.pw (Trunc.TR Z (n + 1)) (Trunc.qe Z (n + 1)) 4)
          (Frobenius.pw (Trunc.TR Z (n + 1)) (Trunc.qe Z (n + 1)) 4)) (n + 1))
        (QBinom.pp (Trunc.TR Z (n + 1)) ((Trunc.TR Z (n + 1)).mul (Frobenius.pw (Trunc.TR Z (n + 1)) (Trunc.qe Z (n + 1)) 4)
          (Frobenius.pw (Trunc.TR Z (n + 1)) (Trunc.qe Z (n + 1)) 4)) (n + 1))))) n = (c n).1 := by
  rw [Trunc.eta32_coef Z (n + 1) n (Nat.lt_succ_self n),
    re_c n (2 * (n + 1)) (2 * (n + 1) + 2) (Nat.le_trans (Nat.le_add_right n (n + 2)) (Nat.le_of_eq (by lib_nat)))
      (Nat.le_trans (Nat.le_add_right n (n + 4)) (Nat.le_of_eq (by lib_nat)))]
  exact sumK_ext Z _ _ (fun e => inner n e _) _

end Hecke32

#print axioms Hecke32.shn_add
#print axioms Hecke32.shn_smul
#print axioms Hecke32.shn_zero
#print axioms Hecke32.shn_shn
#print axioms Hecke32.shn_delta1
#print axioms Hecke32.shn_delta0
#print axioms Hecke32.pw_neg_one
#print axioms Hecke32.emb_int
#print axioms Hecke32.sq_natAbs
#print axioms Hecke32.gn_phi_nat
#print axioms Hecke32.term
#print axioms Hecke32.inner
#print axioms Hecke32.eta32_hecke

lang/Hecke36Rec.lean

lean_all/lang/Hecke36Rec.lean

/-
  lang/Hecke36Rec — T47/T50, cycle 624: **36a: the Hecke recursion at every prime p ≠ 2, 3** for c3 n = Σ_{x≡1 (3), x²+3y²=n} x
  (= the coefficients of η(6z)⁴, Eta36All):
    w36_one       W36(1) = 1
    ins_cj        S is closed under conjugation;  w36_cj   W36(π̄) = conj W36(π)
    c3_rec        p ≡ 1 (mod 3), W36(π) = a + bω:  c3(p^(k+2)) = (2a − b) c3(p^(k+1)) − p c3(p^k)
    c3_inert_odd  p ≡ 2 (mod 3): c3(p^(2k+1)) = 0
    c3_inert_even p ≡ 2 (mod 3): c3(p^(2k)) = ((−p)^k)            (W36(−p) = −p)
  So the p-part of Σ c3(n) n^(−s) is 1/(1 − a_p p^(−s) + p^(1−2s)) at every p ∤ 6 (a_p = 2a − b = 2x, resp. 0).
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Mult36

namespace Hecke36Rec

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius EisEuclid EisPrim EisUnique3 EisPrime3 HeckeMult3 W36Mul Theta36W Mult36 Theta36

theorem w36_one : W36 (Cyclo3.cyc3 LibInt.intR).one = (Cyclo3.cyc3 LibInt.intR).one := by
  have h : InS (Cyclo3.cyc3 LibInt.intR).one := ⟨⟨0, rfl⟩, ⟨0, rfl⟩, ⟨0, rfl⟩⟩
  unfold W36; rw [ite_pos' _ _ h]

theorem ins_cj (z : Int × Int) (h : InS z) : InS (cj z) := by
  obtain ⟨a, b⟩ := z
  obtain ⟨⟨s, hs⟩, ⟨r, hr⟩, ⟨k, hk⟩⟩ := h
  have hs0 : b = 2 * s := hs
  have hr0 : a - 1 = 2 * r := hr
  have hk0 : 2 * a - b - 2 = 3 * k := hk
  refine ⟨⟨-s, ?_⟩, ⟨r + -s, ?_⟩, ⟨k, ?_⟩⟩
  · show -b = 2 * -s; rw [hs0]; lib_int
  · show a + -b - 1 = 2 * (r + -s)
    calc a + -b - 1 = (a - 1) + -b := by lib_int
      _ = _ := by rw [hr0, hs0]; lib_int
  · show 2 * (a + -b) - -b - 2 = 3 * k
    rw [← hk0]; lib_int

theorem w36_cj (π : Int × Int) (u : Int × Int) (hu : en u = 1) (h : InS ((Cyclo3.cyc3 LibInt.intR).mul u π)) :
    W36 (cj π) = cj (W36 π) := by
  rw [w36_eq π u hu h, w36_eq (cj π) (cj u) (by rw [en_cj]; exact hu) (by rw [← cj_mul]; exact ins_cj _ h), cj_mul]

theorem pow_odd (p : Nat) (hp : ∃ k, Int.ofNat p = 2 * k + 1) : ∀ j, ∃ k, Int.ofNat (p ^ j) = 2 * k + 1
  | 0 => ⟨0, rfl⟩
  | j + 1 => by
    obtain ⟨k, hk⟩ := pow_odd p hp j
    obtain ⟨l, hl⟩ := hp
    exact ⟨2 * k * l + k + l, by show Int.ofNat (p ^ j) * Int.ofNat p = _; rw [hk, hl]; lib_int⟩

theorem pow_c3 (p : Nat) (hp : ∃ k, Int.ofNat p = 3 * k + 1) : ∀ j, C3 (Int.ofNat (p ^ j))
  | 0 => ⟨0, Or.inl rfl⟩
  | j + 1 => by
    show C3 (Int.ofNat (p ^ j) * Int.ofNat p)
    obtain ⟨l, hl⟩ := hp
    exact c3_mul _ _ (pow_c3 p ⟨l, hl⟩ j) ⟨l, Or.inl hl⟩

/-- **The 36a Hecke recursion at a split prime.** -/
theorem c3_rec (P : Nat) (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1)
    (hodd : ∃ k, Int.ofNat (P + 1 + 1) = 2 * k + 1) (h1 : ∃ k, Int.ofNat (P + 1 + 1) = 3 * k + 1)
    (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim3 π) (k : Nat) :
    c3 ((P + 1 + 1) ^ (k + 2)) = (2 * (W36 π).1 - (W36 π).2) * c3 ((P + 1 + 1) ^ (k + 1)) - Int.ofNat (P + 1 + 1) * c3 ((P + 1 + 1) ^ k) := by
  have hen : en π = Int.ofNat (P + 1 + 1) := by rw [← nrm_cast, hπ]
  obtain ⟨K, hK⟩ := hodd
  obtain ⟨L, hL⟩ := h1
  obtain ⟨u, hu, iu⟩ := ins_exists π ⟨K, by rw [hen, hK]⟩ ⟨L, Or.inl (by rw [hen, hL])⟩
  have hr := HeckePrime3.cw3_rec (P) W36 w36_mul w36_one hp π hπ pπ k
  rw [w36_cj π u hu iu] at hr
  have hW : en (W36 π) = Int.ofNat (P + 1 + 1) := by rw [w36_eq π u hu iu, en_mul, hu, hen]; lib_int
  have c3n : ∀ j, cw3 W36 ((P + 1 + 1) ^ j) = (c3 ((P + 1 + 1) ^ j), 0) := fun j =>
    cw3_c3 _ (pow_odd _ ⟨K, hK⟩ j) (pow_c3 _ ⟨L, hL⟩ j)
  rw [c3n, c3n, c3n] at hr
  have h1' : c3 ((P + 1 + 1) ^ (k + 2)) = ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).add (W36 π) (cj (W36 π))) (c3 ((P + 1 + 1) ^ (k + 1)), 0))
      ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (W36 π) (cj (W36 π))) (c3 ((P + 1 + 1) ^ k), 0)))).1 := congrArg Prod.fst hr
  rw [h1', mul_cj, hW]
  generalize W36 π = w
  show ((w.1 + (w.1 + -w.2)) * c3 ((P + 1 + 1) ^ (k + 1)) + -((w.2 + -w.2) * 0)) + -(Int.ofNat (P + 1 + 1) * c3 ((P + 1 + 1) ^ k) + -(0 * 0)) = _
  lib_int

/-- **36a at an inert prime, odd powers: c3(p^(2k+1)) = 0.** -/
theorem c3_inert_odd (t : Nat) (hodd : ∃ k, Int.ofNat (3 * t + 1 + 1) = 2 * k + 1) (k : Nat) :
    c3 ((3 * t + 1 + 1) ^ (2 * k + 1)) = 0 := by
  obtain ⟨K, hK⟩ := EisInert3.pow_odd t k
  have h := cw3_c3 _ (pow_odd _ hodd _) ⟨K, Or.inr hK⟩
  rw [EisInert3.cw3_odd t W36 k] at h
  exact (congrArg Prod.fst h).symm

theorem w36_nPi (t : Nat) (hodd : ∃ k, Int.ofNat (3 * t + 1 + 1) = 2 * k + 1) : W36 (EisInert3.nPi t) = EisInert3.nPi t := by
  obtain ⟨K, hK⟩ := hodd
  have h : InS (EisInert3.nPi t) := ⟨⟨0, rfl⟩, ⟨-K + -1, by show -Int.ofNat (3 * t + 1 + 1) - 1 = _; rw [hK]; lib_int⟩,
    ⟨-(2 * Int.ofNat t) + -2, by show 2 * -Int.ofNat (3 * t + 1 + 1) - 0 - 2 = _; show 2 * -(3 * Int.ofNat t + 1 + 1) - 0 - 2 = _; lib_int⟩⟩
  unfold W36; rw [ite_pos' _ _ h]

/-- **36a at an inert prime, even powers: c3(p^(2k)) = (−p)^k.** -/
theorem c3_inert_even (t : Nat) (hpr : ∀ d, Dv d (3 * t + 1 + 1) → d = 1 ∨ d = 3 * t + 1 + 1)
    (hodd : ∃ k, Int.ofNat (3 * t + 1 + 1) = 2 * k + 1) (k : Nat) :
    c3 ((3 * t + 1 + 1) ^ (2 * k)) = (pw (Cyclo3.cyc3 LibInt.intR) (EisInert3.nPi t) k).1 := by
  obtain ⟨K, hK⟩ := hodd
  have hc : ∀ j, C3 (Int.ofNat ((3 * t + 1 + 1) ^ (2 * j))) := by
    intro j
    induction j with
    | zero => exact ⟨0, Or.inl rfl⟩
    | succ j ih =>
      rw [EisInert3.nat_pow2]
      show C3 (Int.ofNat (3 * t + 1 + 1) * Int.ofNat (3 * t + 1 + 1) * Int.ofNat ((3 * t + 1 + 1) ^ (2 * j)))
      exact c3_mul _ _ ⟨3 * Int.ofNat t * Int.ofNat t + 4 * Int.ofNat t + 1, Or.inl (by
        show (3 * Int.ofNat t + 1 + 1) * (3 * Int.ofNat t + 1 + 1) = _; lib_int)⟩ ih
  have h := cw3_c3 _ (pow_odd _ ⟨K, hK⟩ _) (hc k)
  rw [EisInert3.cw3_even t W36 hpr k, HeckePrime3.W_pow3 W36 w36_mul w36_one, w36_nPi t ⟨K, hK⟩] at h
  exact (congrArg Prod.fst h).symm

end Hecke36Rec

#print axioms Hecke36Rec.w36_one
#print axioms Hecke36Rec.ins_cj
#print axioms Hecke36Rec.w36_cj
#print axioms Hecke36Rec.pow_odd
#print axioms Hecke36Rec.pow_c3
#print axioms Hecke36Rec.c3_rec
#print axioms Hecke36Rec.c3_inert_odd
#print axioms Hecke36Rec.w36_nPi
#print axioms Hecke36Rec.c3_inert_even

lang/HeckeCheck.lean

lean_all/lang/HeckeCheck.lean

/-
  lang/HeckeCheck — T43, cycle 331: the η-products of 32a, 27a, 36a are Hecke eigenforms, coefficientwise for n < 200.

  For f = η(4z)²η(8z)², η(3z)²η(9z)², η(6z)⁴ with coefficients a(n) (n < 200):
    cNN_eq      the coefficient list equals an explicit literal list (one evaluation of the η-product, rfl)
    multNN      a(mn) = a(m)a(n) for every coprime pair m, n ≥ 2 with mn < 200
    heckeNN     a(p²) = a(p)² − p for every good prime p ≤ 13
    heckeNN_3   a(p³) = a(p)a(p²) − p·a(p) for the good primes with p³ < 200
  These are the Hecke relations T_m T_n = T_mn and T_p² = T_p² − p of a weight-2 newform with trivial character.
  Lean 4 core only, evaluation by rfl, no axioms. (Kamiak: the cNN_eq evaluations are slow.)
-/
import lang.CMModularCheck

namespace HeckeCheck

open FTAChain FrobChain QRChain ESChain Fermat27a CMModularCheck

def get (l : List Int) (n : Nat) : Int := nth l (n - 1)

def c32 : List Int := [1,0,0,0,-2,0,0,0,-3,0,0,0,6,0,0,0,2,0,0,0,0,0,0,0,-1,0,0,0,-10,0,0,0,0,0,0,0,-2,0,0,0,10,0,0,0,6,0,0,0,-7,0,0,0,14,0,0,0,0,0,0,0,-10,0,0,0,-12,0,0,0,0,0,0,0,-6,0,0,0,0,0,0,0,9,0,0,0,-4,0,0,0,10,0,0,0,0,0,0,0,18,0,0,0,-2,0,0,0,0,0,0,0,6,0,0,0,-14,0,0,0,-18,0,0,0,-11,0,0,0,12,0,0,0,0,0,0,0,0,0,0,0,-22,0,0,0,0,0,0,0,20,0,0,0,14,0,0,0,-6,0,0,0,22,0,0,0,0,0,0,0,0,0,0,0,23,0,0,0,-  -- (+60 chars; full line in the Zenodo source)

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
theorem c32_eq : (List.range 199).map (fun n => nth (eta32 200) n) = c32 := by rfl

def pairs32 : List (Nat × Nat) := [(2, 3), (2, 5), (2, 7), (2, 9), (2, 11), (2, 13), (2, 15), (2, 17), (2, 19), (2, 21), (2, 23), (2, 25), (2, 27), (2, 29), (2, 31), (2, 33), (2, 35), (2, 37), (2, 39), (2, 41), (2, 43), (2, 45), (2, 47), (2, 49), (2, 51), (2, 53), (2, 55), (2, 57), (2, 59), (2, 61), (2, 63), (2, 65), (2, 67), (2, 69), (2, 71), (2, 73), (2, 75), (2, 77), (2, 79), (2, 81), (2, 83),   -- (+1429 chars; full line in the Zenodo source)

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
/-- **Multiplicativity**: a(mn) = a(m)a(n) for coprime m, n, mn < 200. -/
theorem mult32 : pairs32.map (fun q => get c32 (q.1 * q.2)) = pairs32.map (fun q => get c32 q.1 * get c32 q.2) := by rfl

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
/-- **Hecke at p²**: a(p²) = a(p)² − p for the good primes p ≤ 13. -/
theorem hecke32 : ([3, 5, 7, 11, 13] : List Nat).map (fun p => get c32 (p * p))
    = ([3, 5, 7, 11, 13] : List Nat).map (fun p => get c32 p * get c32 p - Int.ofNat p) := by rfl

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
/-- **Hecke at p³**: a(p³) = a(p)a(p²) − p·a(p) for the good primes with p³ < 200. -/
theorem hecke32_3 : ([3, 5] : List Nat).map (fun p => get c32 (p * p * p))
    = ([3, 5] : List Nat).map (fun p => get c32 p * get c32 (p * p) - Int.ofNat p * get c32 p) := by rfl

def c27 : List Int := [1,0,0,-2,0,0,-1,0,0,0,0,0,5,0,0,4,0,0,-7,0,0,0,0,0,-5,0,0,2,0,0,-4,0,0,0,0,0,11,0,0,0,0,0,8,0,0,0,0,0,-6,0,0,-10,0,0,0,0,0,0,0,0,-1,0,0,-8,0,0,5,0,0,0,0,0,-7,0,0,14,0,0,17,0,0,0,0,0,0,0,0,0,0,0,-5,0,0,0,0,0,-19,0,0,10,0,0,-13,0,0,0,0,0,2,0,0,-4,0,0,0,0,0,0,0,0,-11,0,0,8,0,0,20,0,0,0,0,0,7,0,0,0,0,0,23,0,0,0,0,0,0,0,0,-22,0,0,-19,0,0,0,0,0,14,0,0,0,0,0,-25,0,0,0,0,0,12,0,0,-1  -- (+60 chars; full line in the Zenodo source)

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
theorem c27_eq : (List.range 199).map (fun n => nth (eta27 200) n) = c27 := by rfl

def pairs27 : List (Nat × Nat) := [(2, 3), (2, 5), (2, 7), (2, 9), (2, 11), (2, 13), (2, 15), (2, 17), (2, 19), (2, 21), (2, 23), (2, 25), (2, 27), (2, 29), (2, 31), (2, 33), (2, 35), (2, 37), (2, 39), (2, 41), (2, 43), (2, 45), (2, 47), (2, 49), (2, 51), (2, 53), (2, 55), (2, 57), (2, 59), (2, 61), (2, 63), (2, 65), (2, 67), (2, 69), (2, 71), (2, 73), (2, 75), (2, 77), (2, 79), (2, 81), (2, 83),   -- (+1429 chars; full line in the Zenodo source)

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
/-- **Multiplicativity**: a(mn) = a(m)a(n) for coprime m, n, mn < 200. -/
theorem mult27 : pairs27.map (fun q => get c27 (q.1 * q.2)) = pairs27.map (fun q => get c27 q.1 * get c27 q.2) := by rfl

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
/-- **Hecke at p²**: a(p²) = a(p)² − p for the good primes p ≤ 13. -/
theorem hecke27 : ([2, 5, 7, 11, 13] : List Nat).map (fun p => get c27 (p * p))
    = ([2, 5, 7, 11, 13] : List Nat).map (fun p => get c27 p * get c27 p - Int.ofNat p) := by rfl

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
/-- **Hecke at p³**: a(p³) = a(p)a(p²) − p·a(p) for the good primes with p³ < 200. -/
theorem hecke27_3 : ([2, 5] : List Nat).map (fun p => get c27 (p * p * p))
    = ([2, 5] : List Nat).map (fun p => get c27 p * get c27 (p * p) - Int.ofNat p * get c27 p) := by rfl

def c36 : List Int := [1,0,0,0,0,0,-4,0,0,0,0,0,2,0,0,0,0,0,8,0,0,0,0,0,-5,0,0,0,0,0,-4,0,0,0,0,0,-10,0,0,0,0,0,8,0,0,0,0,0,9,0,0,0,0,0,0,0,0,0,0,0,14,0,0,0,0,0,-16,0,0,0,0,0,-10,0,0,0,0,0,-4,0,0,0,0,0,0,0,0,0,0,0,-8,0,0,0,0,0,14,0,0,0,0,0,20,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,-11,0,0,0,0,0,20,0,0,0,0,0,-32,0,0,0,0,0,-16,0,0,0,0,0,0,0,0,0,0,0,-4,0,0,0,0,0,14,0,0,0,0,0,8,0,0,0,0,0,-9,0,0,0,0,0,20,0,  -- (+49 chars; full line in the Zenodo source)

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
theorem c36_eq : (List.range 199).map (fun n => nth (eta36 200) n) = c36 := by rfl

def pairs36 : List (Nat × Nat) := [(2, 3), (2, 5), (2, 7), (2, 9), (2, 11), (2, 13), (2, 15), (2, 17), (2, 19), (2, 21), (2, 23), (2, 25), (2, 27), (2, 29), (2, 31), (2, 33), (2, 35), (2, 37), (2, 39), (2, 41), (2, 43), (2, 45), (2, 47), (2, 49), (2, 51), (2, 53), (2, 55), (2, 57), (2, 59), (2, 61), (2, 63), (2, 65), (2, 67), (2, 69), (2, 71), (2, 73), (2, 75), (2, 77), (2, 79), (2, 81), (2, 83),   -- (+1429 chars; full line in the Zenodo source)

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
/-- **Multiplicativity**: a(mn) = a(m)a(n) for coprime m, n, mn < 200. -/
theorem mult36 : pairs36.map (fun q => get c36 (q.1 * q.2)) = pairs36.map (fun q => get c36 q.1 * get c36 q.2) := by rfl

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
/-- **Hecke at p²**: a(p²) = a(p)² − p for the good primes p ≤ 13. -/
theorem hecke36 : ([5, 7, 11, 13] : List Nat).map (fun p => get c36 (p * p))
    = ([5, 7, 11, 13] : List Nat).map (fun p => get c36 p * get c36 p - Int.ofNat p) := by rfl

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
/-- **Hecke at p³**: a(p³) = a(p)a(p²) − p·a(p) for the good primes with p³ < 200. -/
theorem hecke36_3 : ([5] : List Nat).map (fun p => get c36 (p * p * p))
    = ([5] : List Nat).map (fun p => get c36 p * get c36 (p * p) - Int.ofNat p * get c36 p) := by rfl

end HeckeCheck

#print axioms HeckeCheck.c32_eq
#print axioms HeckeCheck.mult32
#print axioms HeckeCheck.hecke32
#print axioms HeckeCheck.hecke32_3
#print axioms HeckeCheck.c27_eq
#print axioms HeckeCheck.mult27
#print axioms HeckeCheck.hecke27
#print axioms HeckeCheck.hecke27_3
#print axioms HeckeCheck.c36_eq
#print axioms HeckeCheck.mult36
#print axioms HeckeCheck.hecke36
#print axioms HeckeCheck.hecke36_3

lang/HeckeCheck11.lean

lean_all/lang/HeckeCheck11.lean

/-
  lang/HeckeCheck11 — T43, cycle 348: the η-product of 11a is a Hecke eigenform, coefficientwise for n < 200 (the first non-CM case).

  f = η(z)²η(11z)² = q Π (1 − qⁿ)²(1 − q¹¹ⁿ)² with coefficients a(n), n < 200 (ESChain.etaProd):
    c11_eq     the coefficient list equals an explicit literal list (one evaluation, rfl)
    mult11     a(mn) = a(m)a(n) for all 200 coprime pairs m, n ≥ 2, mn < 200
    hecke11    a(p²) = a(p)² − p for the good primes p ≤ 13
    hecke11_3  a(p³) = a(p)a(p²) − p·a(p) for p = 2, 3, 5
    bad11      a(121) = a(11)² (the bad prime: a(11ᵏ) = a(11)ᵏ, here a(11) = 1)
  With ESChain.modular_check_fast (a(p) = p + 1 − #E(𝔽_p)) these are the Hecke relations of the newform attached to 11a.
  Lean 4 core only, evaluation by rfl, no axioms. (Kamiak: c11_eq is slow.)
-/
import ESChain

namespace HeckeCheck11

open FTAChain FrobChain QRChain ESChain

def get (l : List Int) (n : Nat) : Int := nth l (n - 1)

def c11 : List Int := [1,-2,-1,2,1,2,-2,0,-2,-2,1,-2,4,4,-1,-4,-2,4,0,2,2,-2,-1,0,-4,-8,5,-4,0,2,7,8,-1,4,-2,-4,3,0,-4,0,-8,-4,-6,2,-2,2,8,4,-3,8,2,8,-6,-10,1,0,0,0,5,-2,12,-14,4,-8,4,2,-7,-4,1,4,-3,0,4,-6,4,0,-2,8,-10,-4,1,16,-6,4,-2,12,0,0,15,4,-8,-2,-7,-16,0,-8,-7,6,-2,-8,2,-4,-16,0,2,12,18,10,10,-2,-3,8,9,0,-1,0,-8,-10,4,0,1,-24,8,14,-9,-8,8,0,6,-8,-18,-2,0,14,5,0,-7,-2,10,-4,-8,6,4,8,0,-8,3,6  -- (+139 chars; full line in the Zenodo source)

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
theorem c11_eq : (List.range 199).map (fun n => nth (etaProd 200) n) = c11 := by rfl

def pairs11 : List (Nat × Nat) := [(2, 3), (2, 5), (2, 7), (2, 9), (2, 11), (2, 13), (2, 15), (2, 17), (2, 19), (2, 21), (2, 23), (2, 25), (2, 27), (2, 29), (2, 31), (2, 33), (2, 35), (2, 37), (2, 39), (2, 41), (2, 43), (2, 45), (2, 47), (2, 49), (2, 51), (2, 53), (2, 55), (2, 57), (2, 59), (2, 61), (2, 63), (2, 65), (2, 67), (2, 69), (2, 71), (2, 73), (2, 75), (2, 77), (2, 79), (2, 81), (2, 83),   -- (+1429 chars; full line in the Zenodo source)

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
theorem mult11 : pairs11.map (fun q => get c11 (q.1 * q.2)) = pairs11.map (fun q => get c11 q.1 * get c11 q.2) := by rfl

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
theorem hecke11 : ([2, 3, 5, 7, 13] : List Nat).map (fun p => get c11 (p * p))
    = ([2, 3, 5, 7, 13] : List Nat).map (fun p => get c11 p * get c11 p - Int.ofNat p) := by rfl

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
theorem hecke11_3 : ([2, 3, 5] : List Nat).map (fun p => get c11 (p * p * p))
    = ([2, 3, 5] : List Nat).map (fun p => get c11 p * get c11 (p * p) - Int.ofNat p * get c11 p) := by rfl

theorem bad11 : get c11 121 = get c11 11 * get c11 11 := by rfl

end HeckeCheck11

#print axioms HeckeCheck11.c11_eq
#print axioms HeckeCheck11.mult11
#print axioms HeckeCheck11.hecke11
#print axioms HeckeCheck11.hecke11_3
#print axioms HeckeCheck11.bad11

lang/HeckeEig.lean

lean_all/lang/HeckeEig.lean

/-
  lang/HeckeEig — T50: generic pieces for the Hecke-eigenform statement at every n prime to the level.
    bez_pow     s p + t m = 1 ⇒ s' p^k + t' m = 1
    nd, not_div  p^k is never a multiple of d when u p + v d = 1 (d > 1)
    succ_pow    (q + 1)^k = a + 1
    lift2       a prime-power recursion c(q^(j+2)) = A c(q^(j+1)) − B c(q^j) lifts to c(q^j m) when c(q^j m) = c(q^j) c(m)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.HeckePrime

namespace HeckeEig

open DetChain LibInt

theorem bez_pow (p : Nat) (m s t : Int) (h : s * Int.ofNat p + t * m = 1) :
    ∀ k : Nat, ∃ s' t' : Int, s' * Int.ofNat (p ^ k) + t' * m = 1
  | 0 => ⟨1, 0, by show (1 : Int) * 1 + 0 * m = 1; lib_int⟩
  | k + 1 => by
    obtain ⟨a, b, hab⟩ := bez_pow p m s t h k
    refine ⟨a * s, a * t * Int.ofNat (p ^ k) + b * s * Int.ofNat p + b * t * m, ?_⟩
    show a * s * (Int.ofNat (p ^ k) * Int.ofNat p) + (a * t * Int.ofNat (p ^ k) + b * s * Int.ofNat p + b * t * m) * m = 1
    calc a * s * (Int.ofNat (p ^ k) * Int.ofNat p) + (a * t * Int.ofNat (p ^ k) + b * s * Int.ofNat p + b * t * m) * m
        = (a * Int.ofNat (p ^ k) + b * m) * (s * Int.ofNat p + t * m) := by lib_int
      _ = 1 := by rw [hab, h]; rfl

/-- D·K = r with 0 < r < D is impossible. -/
theorem nd (D r : Nat) (K : Int) (h : Int.ofNat D * K = Int.ofNat r) (h0 : 0 < r) (hr : r < D) : False := by
  cases D with
  | zero => exact Nat.not_lt_zero _ hr
  | succ D' =>
    cases K with
    | ofNat k =>
      have hk : (D' + 1) * k = r := Int.ofNat.inj h
      cases k with
      | zero => exact Nat.lt_irrefl 0 (Nat.lt_of_lt_of_le h0 (Nat.le_of_eq (hk.symm.trans (Nat.mul_zero _))))
      | succ j => exact Nat.lt_irrefl r (Nat.lt_of_lt_of_le hr (Nat.le_trans (Nat.le_add_left (D' + 1) ((D' + 1) * j)) (Nat.le_of_eq hk)))
    | negSucc j =>
      have : Int.ofNat (D' + 1) * Int.negSucc j = Int.negSucc ((D' + 1) * j + D') := by
        show Int.negOfNat ((D' + 1) * (j + 1)) = _
        show Int.negOfNat ((D' + 1) * j + D' + 1) = _
        rfl
      rw [this] at h; exact Int.noConfusion h

theorem not_div (p d : Nat) (u v : Int) (huv : u * Int.ofNat p + v * Int.ofNat d = 1) (hd : 1 < d) (k : Nat) (K : Int) :
    Int.ofNat (p ^ k) ≠ Int.ofNat d * K := fun e => by
  obtain ⟨a, b, hab⟩ := bez_pow p (Int.ofNat d) u v huv k
  rw [e] at hab
  exact nd d 1 (a * K + b) ((show Int.ofNat d * (a * K + b) = a * (Int.ofNat d * K) + b * Int.ofNat d by lib_int).trans hab) (Nat.zero_lt_succ 0) hd

theorem succ_pow (q : Nat) : ∀ k : Nat, ∃ a : Nat, (q + 1) ^ k = a + 1
  | 0 => ⟨0, rfl⟩
  | k + 1 => by
    obtain ⟨a, ha⟩ := succ_pow q k
    exact ⟨a * (q + 1) + q, by show (q + 1) ^ k * (q + 1) = _; rw [ha]; lib_nat⟩

variable {R : Type} (Rg : CRing R)

/-- **Lifting a prime-power recursion through multiplicativity.** -/
theorem lift2 (c : Nat → R) (q m : Nat) (hmul : ∀ j, c (q ^ j * m) = Rg.mul (c (q ^ j)) (c m)) (A B : R)
    (hrec : ∀ j, c (q ^ (j + 2)) = Rg.add (Rg.mul A (c (q ^ (j + 1)))) (Rg.neg (Rg.mul B (c (q ^ j))))) (j : Nat) :
    c (q ^ (j + 2) * m) = Rg.add (Rg.mul A (c (q ^ (j + 1) * m))) (Rg.neg (Rg.mul B (c (q ^ j * m)))) := by
  rw [hmul, hmul, hmul, hrec]; lib_ring

end HeckeEig

#print axioms HeckeEig.bez_pow
#print axioms HeckeEig.nd
#print axioms HeckeEig.not_div
#print axioms HeckeEig.succ_pow
#print axioms HeckeEig.lift2

lang/HeckeEigSeq.lean

lean_all/lang/HeckeEigSeq.lean

/-
  lang/HeckeEigSeq — T51 (tool for G4 (b2)), cycle 800: **T_p-eigen sequences are multiplicative and satisfy the Euler recursion**.
  For a coefficient sequence a : ℕ → ℤ with a(1) = 1, the eigen-equation T_p a = a(p)·a of the weight-2 Hecke operator
  T_p = U_p + w V_p reads, coefficientwise,
    (h1)  a(p·N) = a(p) a(N)                         for p ∤ N,
    (h2)  a(p·(p·n)) = a(p) a(p·n) − w a(n)           for every n.
  From these (no primality used in the algebra):
    rec_of      a(p^(k+2)) = a(p) a(p^(k+1)) − w a(p^k)
    mul_of      a(p^(k+1)·N) = a(p^(k+1)) a(N)   whenever a(p·N) = a(p) a(N)
    eigen_uniq  two sequences that are T_p-eigen at every prime p (same weight w) with equal a(p) are equal (HeckeUniq.uniq)
  So the q-series identity of G4 (b2) reduces to: both sides are T_p-eigen sequences, and their a(p) agree.
  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.HeckeUniq

namespace HeckeEigSeq

open FTAChain Frobenius

theorem pw1 (P : Nat) : P ^ 1 = P := by show 1 * P = P; exact (nmul_comm 1 P).trans (nmul_one _)

theorem pw2 (P k : Nat) : P ^ (k + 2) = P * (P * P ^ k) := by
  show P ^ k * P * P = P * (P * P ^ k)
  generalize P ^ k = Q
  lib_nat

theorem pw1' (P k : Nat) : P ^ (k + 1) = P * P ^ k := by
  show P ^ k * P = P * P ^ k
  generalize P ^ k = Q
  lib_nat

theorem rec_of (a : Nat → Int) (w : Int) (P : Nat) (h2 : ∀ n, a (P * (P * n)) = a P * a (P * n) - w * a n) (k : Nat) :
    a (P ^ (k + 2)) = a P * a (P ^ (k + 1)) - w * a (P ^ k) := by
  rw [pw2, pw1' P k]
  exact h2 (P ^ k)

theorem mul_of (a : Nat → Int) (w : Int) (P : Nat) (ha1 : a 1 = 1) (h2 : ∀ n, a (P * (P * n)) = a P * a (P * n) - w * a n)
    (N : Nat) (hN : a (P * N) = a P * a N) :
    ∀ k, a (P ^ (k + 1) * N) = a (P ^ (k + 1)) * a N ∧ a (P ^ (k + 2) * N) = a (P ^ (k + 2)) * a N
  | 0 => by
    have e1 : P ^ (0 + 1) * N = P * N := by rw [pw1' P 0]; show P * 1 * N = P * N; rw [nmul_one]
    have e2 : P ^ (0 + 2) * N = P * (P * N) := by rw [pw2 P 0]; show P * (P * 1) * N = P * (P * N); rw [nmul_one]; lib_nat
    have hp2 : a (P ^ (0 + 2)) = a P * a P - w := by
      rw [pw2 P 0, show P * (P * P ^ 0) = P * (P * 1) from rfl, h2 1, nmul_one, ha1]; lib_int
    refine ⟨?_, ?_⟩
    · rw [e1, show P ^ (0 + 1) = P from pw1 P]; exact hN
    · rw [e2, h2 N, hN, hp2]; lib_int
  | k + 1 => by
    match mul_of a w P ha1 h2 N hN k with
    | ⟨e0, e1⟩ =>
      refine ⟨e1, ?_⟩
      have es : P ^ (k + 1 + 2) * N = P * (P * (P ^ (k + 1) * N)) := by
        show P ^ (k + 1) * P * P * N = _
        generalize P ^ (k + 1) = Q
        lib_nat
      have es1 : P * (P ^ (k + 1) * N) = P ^ (k + 2) * N := by
        show _ = P ^ (k + 1) * P * N
        generalize P ^ (k + 1) = Q
        lib_nat
      have hr : a (P ^ (k + 1 + 2)) = a P * a (P ^ (k + 2)) - w * a (P ^ (k + 1)) := rec_of a w P h2 (k + 1)
      rw [es, h2, es1, e1, e0, hr]
      lib_int

/-- **Eigen-uniqueness**: T_p-eigen sequences (all primes p, same weights) with equal prime coefficients are equal. -/
theorem eigen_uniq (a b w : Nat → Int) (ha1 : a 1 = 1) (hb1 : b 1 = 1)
    (h1A : ∀ l m, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) → (Dv (l + 2) (m + 1) → False) →
      a ((l + 2) * (m + 1)) = a (l + 2) * a (m + 1))
    (h1B : ∀ l m, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) → (Dv (l + 2) (m + 1) → False) →
      b ((l + 2) * (m + 1)) = b (l + 2) * b (m + 1))
    (h2A : ∀ l n, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) →
      a ((l + 2) * ((l + 2) * n)) = a (l + 2) * a ((l + 2) * n) - w (l + 2) * a n)
    (h2B : ∀ l n, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) →
      b ((l + 2) * ((l + 2) * n)) = b (l + 2) * b ((l + 2) * n) - w (l + 2) * b n)
    (hab : ∀ l, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) → a (l + 2) = b (l + 2)) :
    ∀ i, a (i + 1) = b (i + 1) :=
  HeckeUniq.uniq a b w ha1 hb1
    (fun l k m hp hnd => (mul_of a (w (l + 2)) (l + 2) ha1 (h2A l · hp) (m + 1) (h1A l m hp hnd) k).1)
    (fun l k m hp hnd => (mul_of b (w (l + 2)) (l + 2) hb1 (h2B l · hp) (m + 1) (h1B l m hp hnd) k).1)
    (fun l k hp => rec_of a (w (l + 2)) (l + 2) (h2A l · hp) k)
    (fun l k hp => rec_of b (w (l + 2)) (l + 2) (h2B l · hp) k)
    hab

end HeckeEigSeq

#print axioms HeckeEigSeq.pw1
#print axioms HeckeEigSeq.pw2
#print axioms HeckeEigSeq.pw1'
#print axioms HeckeEigSeq.rec_of
#print axioms HeckeEigSeq.mul_of
#print axioms HeckeEigSeq.eigen_uniq

lang/HeckeEuler.lean

lean_all/lang/HeckeEuler.lean

/-
  lang/HeckeEuler — G3 (T43): the Euler product of the theta series of the Hecke character of ℚ(i), coefficientwise for all odd n.

    c_one       c_1 = 1
    mult2       c_{mn} = c_m c_n for coprime odd m, n ≥ 1                     (HeckeMult.hecke_mult with Bezout from lib/Gcd)
    euler       for odd, pairwise coprime n₁, …, n_r:  c_{n₁⋯n_r} = c_{n₁} ⋯ c_{n_r}
  Taking the nᵢ to be the prime powers of n, c_n = ∏ c_{p^e}, with c_{p^e} from HeckePrime (split p: Σ_{j+l=e} π^j π̄^l, the
  recursion c_{p^{e+2}} = 2a c_{p^{e+1}} − p c_{p^e}) and HeckeInert (inert q: (−q)^{e/2} or 0): the Euler product
  L(θ_ψ, s) = ∏_{p split} (1 − 2a_p p^{−s} + p^{1−2s})^{−1} ∏_{q inert} (1 + q^{1−2s})^{−1} over odd n.
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.HeckeInert

namespace HeckeEuler

open FTAChain DetChain LibInt GaussEuclid GaussInt GaussFactor GaussUnique HeckeMult YMChain FiniteRecChain LibGcd

theorem c_one : c 1 = (Cyclo4.cyc4 LibInt.intR).one := by
  have hI : ∀ z, I 1 z = delta (Cyclo4.cyc4 LibInt.intR) z (Cyclo4.cyc4 LibInt.intR).one := fun z => by
    by_cases hz : PD 1 z
    · rw [I_pos _ _ hz]
      have hu : IsUnit (toGI z) := norm_one _ hz.1
      have h1 := GaussCube.unit_primary (toGI z) gone hu GaussCube.prim_one (by rw [HeckeInert.gone_right]; exact hz.2)
      unfold delta; rw [ite_pos' _ _ (toGI_inj z (Cyclo4.cyc4 LibInt.intR).one h1)]
    · rw [I_neg _ _ hz]
      unfold delta; rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact ⟨rfl, GaussCube.prim_one⟩))]
  unfold c
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun z => by rw [hI z])]
  exact pick_box (Cyclo4.cyc4 LibInt.intR) (fun z => z) (Cyclo4.cyc4 LibInt.intR).one 1 (Nat.le_refl 1) (Nat.zero_le 1)

def Cop (m n : Nat) : Prop := ∀ d, d ∣ m → d ∣ n → d = 1
def Odd' (n : Nat) : Prop := ∃ k, Int.ofNat n = 2 * k + 1

/-- **c_{mn} = c_m c_n** for coprime odd m, n. -/
theorem mult2 (m n : Nat) (hmo : Odd' m) (hno : Odd' n) (hc : Cop m n) : c (m * n) = (Cyclo4.cyc4 LibInt.intR).mul (c m) (c n) := by
  match m, n, hmo, hno with
  | 0, _, ⟨k, hk⟩, _ => exact absurd (show (0 : Int) = 2 * k + 1 from hk) (fun e => GaussInt.int_odd_ne_even k 0 (e.symm.trans rfl))
  | _, 0, _, ⟨k, hk⟩ => exact absurd (show (0 : Int) = 2 * k + 1 from hk) (fun e => GaussInt.int_odd_ne_even k 0 (e.symm.trans rfl))
  | m' + 1, n' + 1, ⟨km, hm⟩, ⟨kn, hn⟩ =>
    match coprime_bezout hc with
    | ⟨s, t, hst⟩ => exact hecke_mult m' n' s t km kn hst hm hn

def prodN : List Nat → Nat
  | [] => 1
  | n :: ns => n * prodN ns

def prodE : List (Int × Int) → Int × Int
  | [] => (Cyclo4.cyc4 LibInt.intR).one
  | z :: zs => (Cyclo4.cyc4 LibInt.intR).mul z (prodE zs)

def Good : List Nat → Prop
  | [] => True
  | n :: ns => Odd' n ∧ Cop n (prodN ns) ∧ Good ns

theorem odd_mul (m n : Nat) (hm : Odd' m) (hn : Odd' n) : Odd' (m * n) :=
  match hm, hn with
  | ⟨a, ha⟩, ⟨b, hb⟩ => ⟨2 * a * b + a + b, by
      show Int.ofNat m * Int.ofNat n = _; rw [ha, hb]; lib_int⟩

theorem good_odd : ∀ ns : List Nat, Good ns → Odd' (prodN ns)
  | [], _ => ⟨0, rfl⟩
  | _ :: ns, ⟨hn, _, hg⟩ => odd_mul _ _ hn (good_odd ns hg)

/-- **The Euler product, coefficientwise**: c_{n₁⋯n_r} = c_{n₁}⋯c_{n_r} for odd pairwise coprime nᵢ. -/
theorem euler : ∀ ns : List Nat, Good ns → c (prodN ns) = prodE (ns.map c)
  | [], _ => c_one
  | n :: ns, ⟨hn, hc, hg⟩ => by
    show c (n * prodN ns) = (Cyclo4.cyc4 LibInt.intR).mul (c n) (prodE (ns.map c))
    rw [mult2 n _ hn (good_odd ns hg) hc, euler ns hg]

end HeckeEuler

#print axioms HeckeEuler.c_one
#print axioms HeckeEuler.mult2
#print axioms HeckeEuler.odd_mul
#print axioms HeckeEuler.good_odd
#print axioms HeckeEuler.euler

lang/HeckeEulerW.lean

lean_all/lang/HeckeEulerW.lean

/-
  lang/HeckeEulerW — T50, cycle 597: **the Euler product for every Hecke character of ℚ(i)** (multiplicative weight W, W(1) = 1):
    c_W(1) = 1,   c_W(mn) = c_W(m) c_W(n) (coprime odd),   c_W(n₁⋯n_r) = c_W(n₁)⋯c_W(n_r) (pairwise coprime odd).
  With HeckePrimeW/HeckeInertW this determines c_W at every odd n from W(π) at the split primes and W(−q) at the inert ones.
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.HeckeInertW
import lang.HeckeEuler

namespace HeckeEuler

open FTAChain DetChain LibInt GaussEuclid GaussInt GaussFactor GaussUnique HeckeMult YMChain FiniteRecChain LibGcd

theorem cw_one (W : Int × Int → Int × Int) (hW1 : W (Cyclo4.cyc4 LibInt.intR).one = (Cyclo4.cyc4 LibInt.intR).one) :
    cw W 1 = (Cyclo4.cyc4 LibInt.intR).one := by
  have hI : ∀ z, I 1 z = delta (Cyclo4.cyc4 LibInt.intR) z (Cyclo4.cyc4 LibInt.intR).one := fun z => by
    by_cases hz : PD 1 z
    · rw [I_pos _ _ hz]
      have hu : IsUnit (toGI z) := norm_one _ hz.1
      have h1 := GaussCube.unit_primary (toGI z) gone hu GaussCube.prim_one (by rw [HeckeInert.gone_right]; exact hz.2)
      unfold delta; rw [ite_pos' _ _ (toGI_inj z (Cyclo4.cyc4 LibInt.intR).one h1)]
    · rw [I_neg _ _ hz]
      unfold delta; rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact ⟨rfl, GaussCube.prim_one⟩))]
  unfold cw
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun z => by rw [hI z])]
  rw [pick_box (Cyclo4.cyc4 LibInt.intR) (fun z => W z) (Cyclo4.cyc4 LibInt.intR).one 1 (Nat.le_refl 1) (Nat.zero_le 1)]
  exact hW1

theorem mult2_w (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Cyclo4.cyc4 LibInt.intR).mul α β) = (Cyclo4.cyc4 LibInt.intR).mul (W α) (W β))
    (m n : Nat) (hmo : Odd' m) (hno : Odd' n) (hc : Cop m n) : cw W (m * n) = (Cyclo4.cyc4 LibInt.intR).mul (cw W m) (cw W n) := by
  match m, n, hmo, hno with
  | 0, _, ⟨k, hk⟩, _ => exact absurd (show (0 : Int) = 2 * k + 1 from hk) (fun e => GaussInt.int_odd_ne_even k 0 (e.symm.trans rfl))
  | _, 0, _, ⟨k, hk⟩ => exact absurd (show (0 : Int) = 2 * k + 1 from hk) (fun e => GaussInt.int_odd_ne_even k 0 (e.symm.trans rfl))
  | m' + 1, n' + 1, ⟨km, hm⟩, ⟨kn, hn⟩ =>
    match coprime_bezout hc with
    | ⟨s, t, hst⟩ => exact hecke_mult_w W hW m' n' s t km kn hst hm hn

/-- **The Euler product for c_W.** -/
theorem euler_w (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Cyclo4.cyc4 LibInt.intR).mul α β) = (Cyclo4.cyc4 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Cyclo4.cyc4 LibInt.intR).one = (Cyclo4.cyc4 LibInt.intR).one) : ∀ ns : List Nat, Good ns → cw W (prodN ns) = prodE (ns.map (cw W))
  | [], _ => cw_one W hW1
  | n :: ns, ⟨hn, hc, hg⟩ => by
    show cw W (n * prodN ns) = (Cyclo4.cyc4 LibInt.intR).mul (cw W n) (prodE (ns.map (cw W)))
    rw [mult2_w W hW n _ hn (good_odd ns hg) hc, euler_w W hW hW1 ns hg]

end HeckeEuler

#print axioms HeckeEuler.cw_one
#print axioms HeckeEuler.mult2_w
#print axioms HeckeEuler.euler_w

lang/HeckeInert.lean

lean_all/lang/HeckeInert.lean

/-
  lang/HeckeInert — G3 (T43): the Hecke coefficients of ℚ(i) at an inert prime q = 4r + 3: c_{q^{2j}} = (−q)^j, c_{q^{2j+1}} = 0.

    cast_zero         castZ x = 0 in 𝔽_q ⇒ q | x
    inert_div         q | a² + b² ⇒ q | a and q | b            (−1 is not a square mod q, GaussSplit.inert_three_mod_four)
    inert_all         z primary with N(z) = q^k ⇒ k = 2j and z = (−q)^j
    c_inert_even, c_inert_odd, c_inert_rec    c_{q^{2j}} = (−q)^j, c_{q^{2j+1}} = 0, c_{q^{k+2}} = −q c_{q^k}
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.HeckePrime
import lang.GaussSplit
import lang.IntCast

namespace HeckeInert

open FTAChain DetChain LibInt GaussEuclid GaussCoprime GaussInt GaussFactor GaussUnique HeckeMult HeckePrime YMChain FiniteRecChain Frobenius LibGcd ZMod
open LibRing (castZ castN)

variable (r : Nat)

theorem cast_zero (x : Int) (h : castZ (ring (2 * (2 * r) + 1 + 1)) x = (ring (2 * (2 * r) + 1 + 1)).zero) : IDv (Int.ofNat (2 * (2 * r) + 1 + 1 + 1)) x := by
  apply (idv_iff (2 * (2 * r) + 1 + 1) x).mpr
  cases x with
  | ofNat t =>
    have : mk (2 * (2 * r) + 1 + 1) t = (ring (2 * (2 * r) + 1 + 1)).zero := (IntCast.castN_mk _ t).symm.trans h
    exact congrArg Subtype.val this
  | negSucc t =>
    have h1 : (ring (2 * (2 * r) + 1 + 1)).neg (mk (2 * (2 * r) + 1 + 1) (t + 1)) = (ring (2 * (2 * r) + 1 + 1)).zero := by
      rw [← IntCast.castN_mk]; exact h
    have h2 : mk (2 * (2 * r) + 1 + 1) (t + 1) = (ring (2 * (2 * r) + 1 + 1)).zero := by
      calc mk (2 * (2 * r) + 1 + 1) (t + 1) = (ring (2 * (2 * r) + 1 + 1)).neg ((ring (2 * (2 * r) + 1 + 1)).neg (mk (2 * (2 * r) + 1 + 1) (t + 1))) := by lib_ring
        _ = (ring (2 * (2 * r) + 1 + 1)).zero := by rw [h1]; lib_ring
    exact congrArg Subtype.val h2

theorem sqz (hq : ∀ d, Dv d (2 * (2 * r) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * r) + 1 + 1 + 1)) (A : Zp (2 * (2 * r) + 1 + 1)) (h : (ring (2 * (2 * r) + 1 + 1)).mul A A = (ring (2 * (2 * r) + 1 + 1)).zero) : A = (ring (2 * (2 * r) + 1 + 1)).zero := by
  cases Nat.decEq A.val (ring (2 * (2 * r) + 1 + 1)).zero.val with
  | isTrue h0 => exact Subtype.ext h0
  | isFalse h0 =>
    exfalso
    have hne : A ≠ (ring (2 * (2 * r) + 1 + 1)).zero := fun e => h0 (congrArg Subtype.val e)
    have hv := ZMod.inv_exists (2 * (2 * r) + 1) hq A hne
    apply hne
    calc A = (ring (2 * (2 * r) + 1 + 1)).mul (ring (2 * (2 * r) + 1 + 1)).one A := ((ring (2 * (2 * r) + 1 + 1)).one_mul A).symm
      _ = (ring (2 * (2 * r) + 1 + 1)).mul ((ring (2 * (2 * r) + 1 + 1)).mul (pw (ring (2 * (2 * r) + 1 + 1)) A (2 * (2 * r) + 1)) A) A := by rw [hv]
      _ = (ring (2 * (2 * r) + 1 + 1)).mul (pw (ring (2 * (2 * r) + 1 + 1)) A (2 * (2 * r) + 1)) ((ring (2 * (2 * r) + 1 + 1)).mul A A) := by lib_ring
      _ = (ring (2 * (2 * r) + 1 + 1)).zero := by rw [h, mul_zero]

/-- **q | a² + b² ⇒ q | a, q | b** for q ≡ 3 (mod 4). -/
theorem inert_div (hq : ∀ d, Dv d (2 * (2 * r) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * r) + 1 + 1 + 1)) (a b c : Int) (h : a * a + b * b = Int.ofNat (2 * (2 * r) + 1 + 1 + 1) * c) : IDv (Int.ofNat (2 * (2 * r) + 1 + 1 + 1)) a ∧ IDv (Int.ofNat (2 * (2 * r) + 1 + 1 + 1)) b := by
  have hc : (ring (2 * (2 * r) + 1 + 1)).add ((ring (2 * (2 * r) + 1 + 1)).mul (castZ (ring (2 * (2 * r) + 1 + 1)) a) (castZ (ring (2 * (2 * r) + 1 + 1)) a)) ((ring (2 * (2 * r) + 1 + 1)).mul (castZ (ring (2 * (2 * r) + 1 + 1)) b) (castZ (ring (2 * (2 * r) + 1 + 1)) b)) = (ring (2 * (2 * r) + 1 + 1)).zero := by
    rw [← HasseDavenport.castZ_mul, ← HasseDavenport.castZ_mul, ← IntCast.castZ_add, h, HasseDavenport.castZ_mul, CubeAp.castP, zero_mul']
  have hB : castZ (ring (2 * (2 * r) + 1 + 1)) b = (ring (2 * (2 * r) + 1 + 1)).zero := by
    cases Nat.decEq (castZ (ring (2 * (2 * r) + 1 + 1)) b).val (ring (2 * (2 * r) + 1 + 1)).zero.val with
    | isTrue h0 => exact Subtype.ext h0
    | isFalse h0 =>
      exfalso
      have hne : castZ (ring (2 * (2 * r) + 1 + 1)) b ≠ (ring (2 * (2 * r) + 1 + 1)).zero := fun e => h0 (congrArg Subtype.val e)
      have hv := ZMod.inv_exists (2 * (2 * r) + 1) hq _ hne
      apply GaussSplit.inert_three_mod_four r hq ((ring (2 * (2 * r) + 1 + 1)).mul (castZ (ring (2 * (2 * r) + 1 + 1)) a) (pw (ring (2 * (2 * r) + 1 + 1)) (castZ (ring (2 * (2 * r) + 1 + 1)) b) (2 * (2 * r) + 1)))
      have e : (ring (2 * (2 * r) + 1 + 1)).mul (castZ (ring (2 * (2 * r) + 1 + 1)) a) (castZ (ring (2 * (2 * r) + 1 + 1)) a) = (ring (2 * (2 * r) + 1 + 1)).neg ((ring (2 * (2 * r) + 1 + 1)).mul (castZ (ring (2 * (2 * r) + 1 + 1)) b) (castZ (ring (2 * (2 * r) + 1 + 1)) b)) := DetChain.neg_unique (ring (2 * (2 * r) + 1 + 1)) _ _ hc
      rw [show (ring (2 * (2 * r) + 1 + 1)).mul ((ring (2 * (2 * r) + 1 + 1)).mul (castZ (ring (2 * (2 * r) + 1 + 1)) a) (pw (ring (2 * (2 * r) + 1 + 1)) (castZ (ring (2 * (2 * r) + 1 + 1)) b) (2 * (2 * r) + 1))) ((ring (2 * (2 * r) + 1 + 1)).mul (castZ (ring (2 * (2 * r) + 1 + 1)) a) (pw (ring (2 * (2 * r) + 1 + 1)) (castZ (ring (2 * (2 * r) + 1 + 1)) b) (2 * (2 * r) + 1)))
          = (ring (2 * (2 * r) + 1 + 1)).mul ((ring (2 * (2 * r) + 1 + 1)).mul (castZ (ring (2 * (2 * r) + 1 + 1)) a) (castZ (ring (2 * (2 * r) + 1 + 1)) a)) ((ring (2 * (2 * r) + 1 + 1)).mul (pw (ring (2 * (2 * r) + 1 + 1)) (castZ (ring (2 * (2 * r) + 1 + 1)) b) (2 * (2 * r) + 1)) (pw (ring (2 * (2 * r) + 1 + 1)) (castZ (ring (2 * (2 * r) + 1 + 1)) b) (2 * (2 * r) + 1))) by lib_ring, e,
        show (ring (2 * (2 * r) + 1 + 1)).mul ((ring (2 * (2 * r) + 1 + 1)).neg ((ring (2 * (2 * r) + 1 + 1)).mul (castZ (ring (2 * (2 * r) + 1 + 1)) b) (castZ (ring (2 * (2 * r) + 1 + 1)) b))) ((ring (2 * (2 * r) + 1 + 1)).mul (pw (ring (2 * (2 * r) + 1 + 1)) (castZ (ring (2 * (2 * r) + 1 + 1)) b) (2 * (2 * r) + 1)) (pw (ring (2 * (2 * r) + 1 + 1)) (castZ (ring (2 * (2 * r) + 1 + 1)) b) (2 * (2 *  -- (+10 chars; full line in the Zenodo source)
          = (ring (2 * (2 * r) + 1 + 1)).neg ((ring (2 * (2 * r) + 1 + 1)).mul ((ring (2 * (2 * r) + 1 + 1)).mul (pw (ring (2 * (2 * r) + 1 + 1)) (castZ (ring (2 * (2 * r) + 1 + 1)) b) (2 * (2 * r) + 1)) (castZ (ring (2 * (2 * r) + 1 + 1)) b)) ((ring (2 * (2 * r) + 1 + 1)).mul (pw (ring (2 * (2 * r) + 1 + 1)) (castZ (ring (2 * (2 * r) + 1 + 1)) b) (2 * (2 * r) + 1)) (castZ (ring (2 * (2 * r) + 1 +  -- (+26 chars; full line in the Zenodo source)
      lib_ring
  have hA : castZ (ring (2 * (2 * r) + 1 + 1)) a = (ring (2 * (2 * r) + 1 + 1)).zero := sqz r hq _ (by rw [hB, mul_zero, add_zero'] at hc; exact hc)
  exact ⟨cast_zero r a hA, cast_zero r b hB⟩

theorem int_cancel' (u v : Int) (k : Nat) (h : u * Int.ofNat (k + 1) = v * Int.ofNat (k + 1)) : u = v := by
  have h0 : (u - v) * Int.ofNat (k + 1) = 0 := by rw [show (u - v) * Int.ofNat (k + 1) = u * Int.ofNat (k + 1) - v * Int.ofNat (k + 1) by lib_int, h]; lib_int
  cases int_noZD _ _ h0 with
  | inl h1 => calc u = (u - v) + v := by lib_int
      _ = 0 + v := by rw [h1]; rfl
      _ = v := by lib_int
  | inr h1 => exact absurd (Int.ofNat.inj h1) (fun e => Nat.noConfusion e)

def mq : Int × Int := (-Int.ofNat (2 * (2 * r) + 1 + 1 + 1), 0)

theorem prim_mq : Prim (mq r) := ⟨⟨0, rfl⟩, ⟨-Int.ofNat r - 1, by
  show -Int.ofNat (2 * (2 * r) + 1 + 1 + 1) + 0 - 1 = 4 * (-Int.ofNat r - 1)
  show -(2 * (2 * Int.ofNat r) + 1 + 1 + 1) + 0 - 1 = 4 * (-Int.ofNat r - 1)
  lib_int⟩⟩

theorem gn_mq : gn (mq r) = Int.ofNat (2 * (2 * r) + 1 + 1 + 1) * Int.ofNat (2 * (2 * r) + 1 + 1 + 1) := by
  show -Int.ofNat (2 * (2 * r) + 1 + 1 + 1) * -Int.ofNat (2 * (2 * r) + 1 + 1 + 1) + 0 * 0 = _; lib_int

theorem ipow_two (x : Int) : ∀ j, ipow x (2 * j) = ipow (x * x) j
  | 0 => rfl
  | j + 1 => by
    show ipow x (2 * j + 1 + 1) = ipow (x * x) j * (x * x)
    show ipow x (2 * j) * x * x = _
    rw [ipow_two x j]; lib_int

theorem q_odd : ∀ k, ∃ t, Int.ofNat ((2 * (2 * r) + 1 + 1 + 1) ^ k) = 2 * t + 1
  | 0 => ⟨0, rfl⟩
  | k + 1 => by
    match q_odd k with
    | ⟨t, ht⟩ =>
      refine ⟨t * (2 * (2 * Int.ofNat r) + 1 + 1 + 1) + (2 * Int.ofNat r + 1), ?_⟩
      show Int.ofNat ((2 * (2 * r) + 1 + 1 + 1) ^ k) * Int.ofNat (2 * (2 * r) + 1 + 1 + 1) = _
      rw [ht]; show (2 * t + 1) * (2 * (2 * Int.ofNat r) + 1 + 1 + 1) = _; lib_int

theorem gone_right (u : GI) : gmul u gone = u := gi_ext _ _ (by show u.re * 1 - u.im * 0 = u.re; lib_int) (by show u.re * 0 + u.im * 1 = u.im; lib_int)

/-- **Primary elements of norm q^k**: k is even and z = (−q)^{k/2}. -/
theorem inert_all (hq : ∀ d, Dv d (2 * (2 * r) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * r) + 1 + 1 + 1)) : ∀ k (z : Int × Int), Prim z → gn z = Int.ofNat ((2 * (2 * r) + 1 + 1 + 1) ^ k) → ∃ j, k = 2 * j ∧ z = pw (Cyclo4.cyc4 LibInt.intR) (mq r) j
  | 0, z, pz, hz => by
    have hu : IsUnit (toGI z) := norm_one _ hz
    have h1 := GaussCube.unit_primary (toGI z) gone hu GaussCube.prim_one (by rw [gone_right]; exact pz)
    exact ⟨0, rfl, toGI_inj z (Cyclo4.cyc4 LibInt.intR).one h1⟩
  | 1, z, _, hz => by
    exfalso
    have h1 : z.1 * z.1 + z.2 * z.2 = Int.ofNat (2 * (2 * r) + 1 + 1 + 1) * 1 := by rw [show z.1 * z.1 + z.2 * z.2 = gn z from rfl, hz]; show 1 * Int.ofNat (2 * (2 * r) + 1 + 1 + 1) = _; lib_int
    match inert_div r hq z.1 z.2 1 h1 with
    | ⟨⟨a', ha⟩, ⟨b', hb⟩⟩ =>
      have : Int.ofNat (2 * (2 * r) + 1 + 1 + 1) * (Int.ofNat (2 * (2 * r) + 1 + 1 + 1) * (a' * a' + b' * b')) = Int.ofNat (2 * (2 * r) + 1 + 1 + 1) * 1 := by rw [← h1, ha, hb]; lib_int
      have h2 : Int.ofNat (2 * (2 * r) + 1 + 1 + 1) * (a' * a' + b' * b') = 1 := int_cancel' _ _ (2 * (2 * r) + 1 + 1) (by
        rw [show Int.ofNat (2 * (2 * r) + 1 + 1 + 1) * (a' * a' + b' * b') * Int.ofNat (2 * (2 * r) + 1 + 1 + 1) = Int.ofNat (2 * (2 * r) + 1 + 1 + 1) * (Int.ofNat (2 * (2 * r) + 1 + 1 + 1) * (a' * a' + b' * b')) by lib_int, this]; lib_int)
      cases int_unit _ _ h2 with
      | inl e => exact Nat.noConfusion (Nat.succ.inj (Int.ofNat.inj e))
      | inr e => exact Int.noConfusion e
  | k + 2, z, pz, hz => by
    have h1 : z.1 * z.1 + z.2 * z.2 = Int.ofNat (2 * (2 * r) + 1 + 1 + 1) * (Int.ofNat (2 * (2 * r) + 1 + 1 + 1) * Int.ofNat ((2 * (2 * r) + 1 + 1 + 1) ^ k)) := by
      rw [show z.1 * z.1 + z.2 * z.2 = gn z from rfl, hz]
      show Int.ofNat ((2 * (2 * r) + 1 + 1 + 1) ^ k * (2 * (2 * r) + 1 + 1 + 1) * (2 * (2 * r) + 1 + 1 + 1)) = _; show Int.ofNat ((2 * (2 * r) + 1 + 1 + 1) ^ k) * Int.ofNat (2 * (2 * r) + 1 + 1 + 1) * Int.ofNat (2 * (2 * r) + 1 + 1 + 1) = _; lib_int
    match inert_div r hq z.1 z.2 _ h1 with
    | ⟨⟨a', ha⟩, ⟨b', hb⟩⟩ =>
      have hz' : z = (Cyclo4.cyc4 LibInt.intR).mul (mq r) (-a', -b') := Prod.ext (by show z.1 = -Int.ofNat (2 * (2 * r) + 1 + 1 + 1) * -a' + -(0 * -b'); rw [ha]; lib_int)
        (by show z.2 = -Int.ofNat (2 * (2 * r) + 1 + 1 + 1) * -b' + 0 * -a'; rw [hb]; lib_int)
      have hg : gn ((-a', -b') : Int × Int) = Int.ofNat ((2 * (2 * r) + 1 + 1 + 1) ^ k) := by
        have e : Int.ofNat (2 * (2 * r) + 1 + 1 + 1) * Int.ofNat (2 * (2 * r) + 1 + 1 + 1) * gn ((-a', -b') : Int × Int) = Int.ofNat (2 * (2 * r) + 1 + 1 + 1) * Int.ofNat (2 * (2 * r) + 1 + 1 + 1) * Int.ofNat ((2 * (2 * r) + 1 + 1 + 1) ^ k) := by
          calc Int.ofNat (2 * (2 * r) + 1 + 1 + 1) * Int.ofNat (2 * (2 * r) + 1 + 1 + 1) * gn ((-a', -b') : Int × Int) = gn (mq r) * gn ((-a', -b') : Int × Int) := by rw [gn_mq]
            _ = gn z := by rw [← GaussEuclid.norm_mul, ← hz']
            _ = _ := by rw [hz]; show Int.ofNat ((2 * (2 * r) + 1 + 1 + 1) ^ k) * Int.ofNat (2 * (2 * r) + 1 + 1 + 1) * Int.ofNat (2 * (2 * r) + 1 + 1 + 1) = _; lib_int
        have e1 : Int.ofNat (2 * (2 * r) + 1 + 1 + 1) * gn ((-a', -b') : Int × Int) = Int.ofNat (2 * (2 * r) + 1 + 1 + 1) * Int.ofNat ((2 * (2 * r) + 1 + 1 + 1) ^ k) :=
          int_cancel' _ _ (2 * (2 * r) + 1 + 1) (by
            show Int.ofNat (2 * (2 * r) + 1 + 1 + 1) * gn ((-a', -b') : Int × Int) * Int.ofNat (2 * (2 * r) + 1 + 1 + 1) = Int.ofNat (2 * (2 * r) + 1 + 1 + 1) * Int.ofNat ((2 * (2 * r) + 1 + 1 + 1) ^ k) * Int.ofNat (2 * (2 * r) + 1 + 1 + 1)
            rw [show Int.ofNat (2 * (2 * r) + 1 + 1 + 1) * gn ((-a', -b') : Int × Int) * Int.ofNat (2 * (2 * r) + 1 + 1 + 1) = Int.ofNat (2 * (2 * r) + 1 + 1 + 1) * Int.ofNat (2 * (2 * r) + 1 + 1 + 1) * gn ((-a', -b') : Int × Int) by lib_int, e]; lib_int)
        exact int_cancel' _ _ (2 * (2 * r) + 1 + 1) (by
          show gn ((-a', -b') : Int × Int) * Int.ofNat (2 * (2 * r) + 1 + 1 + 1) = Int.ofNat ((2 * (2 * r) + 1 + 1 + 1) ^ k) * Int.ofNat (2 * (2 * r) + 1 + 1 + 1)
          rw [show gn ((-a', -b') : Int × Int) * Int.ofNat (2 * (2 * r) + 1 + 1 + 1) = Int.ofNat (2 * (2 * r) + 1 + 1 + 1) * gn ((-a', -b') : Int × Int) by lib_int, e1]; lib_int)
      have py : Prim ((-a', -b') : Int × Int) := match q_odd r k with
        | ⟨t, ht⟩ => prim_quot (mq r) _ t (by rw [hg, ht]) (prim_mq r) (by rw [← hz']; exact pz)
      match inert_all hq k _ py hg with
      | ⟨j, hj, hy⟩ =>
        exact ⟨j + 1, by rw [hj]; lib_nat, by rw [hz', hy]; show _ = (Cyclo4.cyc4 LibInt.intR).mul _ _; lib_ring⟩

theorem mq_PD (j : Nat) : PD ((2 * (2 * r) + 1 + 1 + 1) ^ (2 * j)) (pw (Cyclo4.cyc4 LibInt.intR) (mq r) j) := by
  refine ⟨?_, ?_⟩
  · rw [HeckePrime.gn_pw, gn_mq, ← ipow_two, ipow_nat]
  · unfold Prim; rw [HeckePrime.toGI_pow]; exact GaussCube.prim_pow _ (prim_mq r) _

/-- **c_{q^{2j}} = (−q)^j.** -/
theorem c_even (hq : ∀ d, Dv d (2 * (2 * r) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * r) + 1 + 1 + 1)) (j : Nat) : c ((2 * (2 * r) + 1 + 1 + 1) ^ (2 * j)) = pw (Cyclo4.cyc4 LibInt.intR) (mq r) j := by
  have hI : ∀ z, I ((2 * (2 * r) + 1 + 1 + 1) ^ (2 * j)) z = delta (Cyclo4.cyc4 LibInt.intR) z (pw (Cyclo4.cyc4 LibInt.intR) (mq r) j) := fun z => by
    by_cases hz : PD ((2 * (2 * r) + 1 + 1 + 1) ^ (2 * j)) z
    · rw [I_pos _ _ hz]
      match inert_all r hq _ z hz.2 hz.1 with
      | ⟨j', hj, hz'⟩ =>
        have e : j' = j := (Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ 1) hj).symm
        unfold delta; rw [ite_pos' _ _ (by rw [hz', e])]
    · rw [I_neg _ _ hz]
      unfold delta; rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact mq_PD r j))]
  unfold c
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun z => by rw [hI z])]
  have hb := abs_le _ _ (mq_PD r j).1
  exact pick_box (Cyclo4.cyc4 LibInt.intR) (fun z => z) _ _ hb.1 hb.2

/-- **c_{q^{2j+1}} = 0.** -/
theorem c_odd (hq : ∀ d, Dv d (2 * (2 * r) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * r) + 1 + 1 + 1)) (j : Nat) : c ((2 * (2 * r) + 1 + 1 + 1) ^ (2 * j + 1)) = (Cyclo4.cyc4 LibInt.intR).zero := by
  have hI : ∀ z, (Cyclo4.cyc4 LibInt.intR).mul (I ((2 * (2 * r) + 1 + 1 + 1) ^ (2 * j + 1)) z) z = (Cyclo4.cyc4 LibInt.intR).zero := fun z => by
    by_cases hz : PD ((2 * (2 * r) + 1 + 1 + 1) ^ (2 * j + 1)) z
    · exfalso
      match inert_all r hq _ z hz.2 hz.1 with
      | ⟨j', hj, _⟩ => exact GaussInt.int_odd_ne_even (Int.ofNat j) (Int.ofNat j') (congrArg Int.ofNat hj)
    · rw [I_neg _ _ hz]; lib_ring
  unfold c
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ hI]
  exact HeckePrime.zsumR (Cyclo4.cyc4 LibInt.intR) _

/-- **Hecke recursion at an inert prime**: c_{q^{k+2}} = −q c_{q^k}, c_q = 0. -/
theorem c_inert_rec (hq : ∀ d, Dv d (2 * (2 * r) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * r) + 1 + 1 + 1)) (k : Nat) : c ((2 * (2 * r) + 1 + 1 + 1) ^ (k + 2)) = (Cyclo4.cyc4 LibInt.intR).mul (mq r) (c ((2 * (2 * r) + 1 + 1 + 1) ^ k)) := by
  match EveryPrime.nat_even_odd k with
  | Or.inl ⟨j, hj⟩ =>
    rw [hj, show 2 * j + 2 = 2 * (j + 1) by lib_nat, c_even r hq, c_even r hq]
    show (Cyclo4.cyc4 LibInt.intR).mul (pw (Cyclo4.cyc4 LibInt.intR) (mq r) j) (mq r) = _; lib_ring
  | Or.inr ⟨j, hj⟩ =>
    rw [hj, show 2 * j + 1 + 2 = 2 * (j + 1) + 1 by lib_nat, c_odd r hq, c_odd r hq]; lib_ring

end HeckeInert

#print axioms HeckeInert.cast_zero
#print axioms HeckeInert.sqz
#print axioms HeckeInert.inert_div
#print axioms HeckeInert.prim_mq
#print axioms HeckeInert.gn_mq
#print axioms HeckeInert.ipow_two
#print axioms HeckeInert.q_odd
#print axioms HeckeInert.inert_all
#print axioms HeckeInert.mq_PD
#print axioms HeckeInert.c_even
#print axioms HeckeInert.c_odd
#print axioms HeckeInert.c_inert_rec

lang/HeckeInertW.lean

lean_all/lang/HeckeInertW.lean

/-
  lang/HeckeInertW — T50, cycle 596: **Hecke characters of ℚ(i) at inert primes q = 4r + 3**, every weight W:
    c_W(q^(2j)) = W((−q)^j),   c_W(q^(2j+1)) = 0,
  and for multiplicative W with W(1) = 1: c_W(q^(2j)) = W(−q)^j (W(α) = α^k: (−q)^(kj)).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.HeckePrimeW
import lang.HeckeInert

namespace HeckeInert

open FTAChain DetChain LibInt GaussEuclid GaussInt GaussFactor GaussUnique HeckeMult YMChain FiniteRecChain Frobenius LibGcd

variable (r : Nat)

theorem cw_even (W : Int × Int → Int × Int) (hq : ∀ d, Dv d (2 * (2 * r) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * r) + 1 + 1 + 1)) (j : Nat) :
    cw W ((2 * (2 * r) + 1 + 1 + 1) ^ (2 * j)) = W (pw (Cyclo4.cyc4 LibInt.intR) (mq r) j) := by
  have hI : ∀ z, I ((2 * (2 * r) + 1 + 1 + 1) ^ (2 * j)) z = delta (Cyclo4.cyc4 LibInt.intR) z (pw (Cyclo4.cyc4 LibInt.intR) (mq r) j) := fun z => by
    by_cases hz : PD ((2 * (2 * r) + 1 + 1 + 1) ^ (2 * j)) z
    · rw [I_pos _ _ hz]
      match inert_all r hq _ z hz.2 hz.1 with
      | ⟨j', hj, hz'⟩ =>
        have e : j' = j := (Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ 1) hj).symm
        unfold delta; rw [ite_pos' _ _ (by rw [hz', e])]
    · rw [I_neg _ _ hz]
      unfold delta; rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact mq_PD r j))]
  unfold cw
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun z => by rw [hI z])]
  have hb := abs_le _ _ (mq_PD r j).1
  exact pick_box (Cyclo4.cyc4 LibInt.intR) (fun z => W z) _ _ hb.1 hb.2

theorem cw_odd (W : Int × Int → Int × Int) (hq : ∀ d, Dv d (2 * (2 * r) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * r) + 1 + 1 + 1)) (j : Nat) :
    cw W ((2 * (2 * r) + 1 + 1 + 1) ^ (2 * j + 1)) = (Cyclo4.cyc4 LibInt.intR).zero := by
  have hI : ∀ z, (Cyclo4.cyc4 LibInt.intR).mul (I ((2 * (2 * r) + 1 + 1 + 1) ^ (2 * j + 1)) z) (W z) = (Cyclo4.cyc4 LibInt.intR).zero := fun z => by
    by_cases hz : PD ((2 * (2 * r) + 1 + 1 + 1) ^ (2 * j + 1)) z
    · exfalso
      match inert_all r hq _ z hz.2 hz.1 with
      | ⟨j', hj, _⟩ => exact GaussInt.int_odd_ne_even (Int.ofNat j) (Int.ofNat j') (congrArg Int.ofNat hj)
    · rw [I_neg _ _ hz]; lib_ring
  unfold cw
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ hI]
  exact HeckePrime.zsumR (Cyclo4.cyc4 LibInt.intR) _

theorem cw_even' (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Cyclo4.cyc4 LibInt.intR).mul α β) = (Cyclo4.cyc4 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Cyclo4.cyc4 LibInt.intR).one = (Cyclo4.cyc4 LibInt.intR).one)
    (hq : ∀ d, Dv d (2 * (2 * r) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * r) + 1 + 1 + 1)) (j : Nat) :
    cw W ((2 * (2 * r) + 1 + 1 + 1) ^ (2 * j)) = pw (Cyclo4.cyc4 LibInt.intR) (W (mq r)) j := by
  rw [cw_even r W hq j, HeckePrime.W_pow W hW hW1]

end HeckeInert

#print axioms HeckeInert.cw_even
#print axioms HeckeInert.cw_odd
#print axioms HeckeInert.cw_even'

lang/HeckeMult.lean

lean_all/lang/HeckeMult.lean

/-
  lang/HeckeMult — G3 (T43): the coefficients c_n = Σ_{α primary, N(α) = n} α of the theta series of the Hecke character of ℚ(i)
  are multiplicative: c_{mn} = c_m c_n for coprime odd m, n.

  Finite sums: zl n = [0, 1, −1, …, n, −n], box n = zl n × zl n (every z with |z.1|, |z.2| ≤ n exactly once), P n z ⇔ N(z) = n ∧ z primary
  (decidable through md: IDv (k + 1) x ⇔ md k |x| = 0).
    count_zl, sumK_pairs, count_box, abs_le     the counting facts
    idv_iff, primDec                            primary is decidable
    c, hecke_mult                               c_{mn} = c_m c_n            (GaussUnique.prim_split, uniq_split)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.GaussUnique
import lang.ZMod

namespace HeckeMult

open FTAChain DetChain LibInt GaussEuclid GaussCoprime GaussInt GaussFactor GaussUnique LibGcd YMChain FiniteRecChain

def zl : Nat → List Int
  | 0 => [0]
  | k + 1 => zl k ++ [Int.ofNat (k + 1), Int.negSucc k]

theorem count_zl {R : Type} (Rg : CRing R) (a : Int) : ∀ n, Int.natAbs a ≤ n → sumK Rg (fun x => delta Rg x a) (zl n) = Rg.one
  | 0, h => by
    have : a = 0 := GaussEuclid.natAbs_zero a (Nat.le_antisymm h (Nat.zero_le _))
    show Rg.add (delta Rg 0 a) Rg.zero = Rg.one
    rw [this]; unfold delta; rw [ite_pos' _ _ rfl, add_zero']
  | k + 1, h => by
    show sumK Rg (fun x => delta Rg x a) (zl k ++ [Int.ofNat (k + 1), Int.negSucc k]) = Rg.one
    rw [sumK_append]
    show Rg.add _ (Rg.add (delta Rg (Int.ofNat (k + 1)) a) (Rg.add (delta Rg (Int.negSucc k) a) Rg.zero)) = Rg.one
    cases Nat.lt_or_ge (Int.natAbs a) (k + 1) with
    | inl hl =>
      rw [count_zl Rg a k (Nat.le_of_lt_succ hl)]
      have h1 : ¬ Int.ofNat (k + 1) = a := fun e => by rw [← e] at hl; exact Nat.lt_irrefl _ hl
      have h2 : ¬ Int.negSucc k = a := fun e => by rw [← e] at hl; exact Nat.lt_irrefl _ hl
      unfold delta; rw [ite_neg' _ _ h1, ite_neg' _ _ h2]; lib_ring
    | inr hg =>
      have he : Int.natAbs a = k + 1 := Nat.le_antisymm h hg
      have h0 : sumK Rg (fun x => delta Rg x a) (zl k) = Rg.zero := zero_count Rg a k (by rw [he]; exact Nat.lt_succ_self k)
      rw [h0]
      cases a with
      | ofNat t =>
        have ht : t = k + 1 := he
        rw [ht]; unfold delta; rw [ite_pos' _ _ rfl, ite_neg' _ _ (fun e => Int.noConfusion e)]; lib_ring
      | negSucc t =>
        have ht : t = k := Nat.succ.inj he
        rw [ht]; unfold delta; rw [ite_neg' _ _ (fun e => Int.noConfusion e), ite_pos' _ _ rfl]; lib_ring
where
  zero_count {R : Type} (Rg : CRing R) (a : Int) : ∀ n, n < Int.natAbs a → sumK Rg (fun x => delta Rg x a) (zl n) = Rg.zero
  | 0, h => by
    show Rg.add (delta Rg 0 a) Rg.zero = Rg.zero
    have : ¬ (0 : Int) = a := fun e => by rw [← e] at h; exact Nat.lt_irrefl 0 h
    unfold delta; rw [ite_neg' _ _ this]; lib_ring
  | k + 1, h => by
    show sumK Rg (fun x => delta Rg x a) (zl k ++ [Int.ofNat (k + 1), Int.negSucc k]) = Rg.zero
    rw [sumK_append, zero_count Rg a k (Nat.lt_of_succ_lt h)]
    show Rg.add Rg.zero (Rg.add (delta Rg (Int.ofNat (k + 1)) a) (Rg.add (delta Rg (Int.negSucc k) a) Rg.zero)) = Rg.zero
    have h1 : ¬ Int.ofNat (k + 1) = a := fun e => by rw [← e] at h; exact Nat.lt_irrefl _ h
    have h2 : ¬ Int.negSucc k = a := fun e => by rw [← e] at h; exact Nat.lt_irrefl _ h
    unfold delta; rw [ite_neg' _ _ h1, ite_neg' _ _ h2]; lib_ring

def pairs : List Int → List Int → List (Int × Int)
  | [], _ => []
  | a :: as, bs => bs.map (fun b => (a, b)) ++ pairs as bs

theorem sumK_pairs {R : Type} (Rg : CRing R) (f : Int × Int → R) (bs : List Int) : ∀ as : List Int,
    sumK Rg f (pairs as bs) = sumK Rg (fun a => sumK Rg (fun b => f (a, b)) bs) as
  | [] => rfl
  | a :: as => by
    show sumK Rg f (bs.map (fun b => (a, b)) ++ pairs as bs) = Rg.add _ _
    rw [sumK_append, sumK_map, sumK_pairs Rg f bs as]

def box (n : Nat) : List (Int × Int) := pairs (zl n) (zl n)

theorem delta_pair {R : Type} (Rg : CRing R) (x y : Int × Int) : delta Rg x y = Rg.mul (delta Rg x.1 y.1) (delta Rg x.2 y.2) := by
  unfold delta
  by_cases h1 : x.1 = y.1
  · by_cases h2 : x.2 = y.2
    · rw [ite_pos' _ _ (Prod.ext h1 h2), ite_pos' _ _ h1, ite_pos' _ _ h2]; lib_ring
    · rw [ite_neg' _ _ (fun e => h2 (congrArg Prod.snd e)), ite_pos' _ _ h1, ite_neg' _ _ h2]; lib_ring
  · rw [ite_neg' _ _ (fun e => h1 (congrArg Prod.fst e)), ite_neg' _ _ h1]; lib_ring

theorem count_box {R : Type} (Rg : CRing R) (z : Int × Int) (n : Nat) (h1 : Int.natAbs z.1 ≤ n) (h2 : Int.natAbs z.2 ≤ n) :
    sumK Rg (fun x => delta Rg x z) (box n) = Rg.one := by
  unfold box; rw [sumK_pairs]
  have e : sumK Rg (fun a => sumK Rg (fun b => delta Rg (a, b) z) (zl n)) (zl n)
      = sumK Rg (fun a => Rg.mul (delta Rg a z.1) (sumK Rg (fun b => delta Rg b z.2) (zl n))) (zl n) :=
    sumK_ext Rg _ _ (fun a => by rw [mul_sumK]; exact sumK_ext Rg _ _ (fun b => delta_pair Rg (a, b) z) _) _
  rw [e, sumK_ext Rg _ _ (fun a => by rw [count_zl Rg z.2 n h2]), ← sumK_mul, count_zl Rg z.1 n h1]; lib_ring

theorem pick_box {R : Type} (Rg : CRing R) (F : Int × Int → R) (z : Int × Int) (n : Nat) (h1 : Int.natAbs z.1 ≤ n) (h2 : Int.natAbs z.2 ≤ n) :
    sumK Rg (fun x => Rg.mul (delta Rg x z) (F x)) (box n) = F z := by
  rw [delta_sum, count_box Rg z n h1 h2, Rg.one_mul]

theorem le_sq (a : Nat) : a ≤ a * a := by
  cases a with
  | zero => exact Nat.le_refl 0
  | succ k => exact Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ (Nat.succ_le_succ (Nat.zero_le k)))

/-- N(z) = n ⇒ |z.1|, |z.2| ≤ n. -/
theorem abs_le (z : Int × Int) (n : Nat) (h : gn z = Int.ofNat n) : Int.natAbs z.1 ≤ n ∧ Int.natAbs z.2 ≤ n := by
  have hn : nrm z = n := Int.ofNat.inj (by rw [nrm_cast]; exact h)
  constructor
  · exact Nat.le_trans (le_sq _) (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq hn))
  · exact Nat.le_trans (le_sq _) (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_of_eq hn))

theorem natAbs_negOfNat (j : Nat) : Int.natAbs (Int.negOfNat j) = j := by
  cases j with
  | zero => rfl
  | succ j => rfl

theorem idv_iff (k : Nat) (x : Int) : IDv (Int.ofNat (k + 1)) x ↔ ZMod.md k (Int.natAbs x) = 0 := by
  constructor
  · intro ⟨c, hc⟩
    apply ZMod.dv_md_zero k
    refine ⟨Int.natAbs c, ?_⟩
    rw [hc]
    cases c with
    | ofNat t => show (k + 1) * t = t * (k + 1); lib_nat
    | negSucc t => show Int.natAbs (Int.negOfNat ((k + 1) * (t + 1))) = (t + 1) * (k + 1); rw [natAbs_negOfNat]; lib_nat
  · intro h
    match ZMod.md_zero_dv k _ h with
    | ⟨c, hc⟩ =>
      cases x with
      | ofNat t => exact ⟨Int.ofNat c, by show Int.ofNat t = Int.ofNat ((k + 1) * c); exact congrArg Int.ofNat (hc.trans (by lib_nat))⟩
      | negSucc t =>
        refine ⟨-Int.ofNat c, ?_⟩
        have e : Int.ofNat (t + 1) = Int.ofNat (k + 1) * Int.ofNat c := congrArg Int.ofNat (hc.trans (by lib_nat))
        show -Int.ofNat (t + 1) = _
        rw [e]; lib_int

def PD (n : Nat) (z : Int × Int) : Prop := gn z = Int.ofNat n ∧ Prim z

theorem pd_iff (n : Nat) (z : Int × Int) :
    (gn z = Int.ofNat n ∧ (ZMod.md 1 (Int.natAbs z.2) = 0 ∧ ZMod.md 3 (Int.natAbs (z.1 + z.2 - 1)) = 0)) ↔ PD n z := by
  constructor
  · intro ⟨h1, h2, h3⟩
    exact ⟨h1, (idv_iff 1 z.2).mpr h2, (idv_iff 3 (z.1 + z.2 - 1)).mpr h3⟩
  · intro ⟨h1, h2, h3⟩
    exact ⟨h1, (idv_iff 1 z.2).mp h2, (idv_iff 3 (z.1 + z.2 - 1)).mp h3⟩

instance (n : Nat) (z : Int × Int) : Decidable (PD n z) := decidable_of_iff _ (pd_iff n z)

theorem zsum {A : Type} : ∀ l : List A, sumK (Cyclo4.cyc4 LibInt.intR) (fun _ => (Cyclo4.cyc4 LibInt.intR).zero) l = (Cyclo4.cyc4 LibInt.intR).zero
  | [] => rfl
  | _ :: l => by show (Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).zero (sumK (Cyclo4.cyc4 LibInt.intR) (fun _ => (Cyclo4.cyc4 LibInt.intR).zero) l) = _; rw [zsum l]; exact (Cyclo4.cyc4 LibInt.intR).zero_add _

def I (n : Nat) (z : Int × Int) : Int × Int := if PD n z then (Cyclo4.cyc4 LibInt.intR).one else (Cyclo4.cyc4 LibInt.intR).zero

/-- c_n = Σ_{α primary, N(α) = n} α. -/
def c (n : Nat) : Int × Int := sumK (Cyclo4.cyc4 LibInt.intR) (fun z => (Cyclo4.cyc4 LibInt.intR).mul (I n z) z) (box n)

theorem I_pos (n : Nat) (z : Int × Int) (h : PD n z) : I n z = (Cyclo4.cyc4 LibInt.intR).one := ite_pos' _ _ h
theorem I_neg (n : Nat) (z : Int × Int) (h : ¬ PD n z) : I n z = (Cyclo4.cyc4 LibInt.intR).zero := ite_neg' _ _ h

/-- **c_{mn} = c_m c_n** for coprime odd m, n. -/
theorem hecke_mult (m n : Nat) (s t km kn : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : Int.ofNat (m + 1) = 2 * km + 1) (hn : Int.ofNat (n + 1) = 2 * kn + 1) :
    c ((m + 1) * (n + 1)) = (Cyclo4.cyc4 LibInt.intR).mul (c (m + 1)) (c (n + 1)) := by
  have hM0 : Int.ofNat (m + 1) ≠ 0 := fun e => Nat.noConfusion (Int.ofNat.inj e)
  -- product of primaries of norms m, n is primary of norm mn
  have hprod : ∀ α β, PD (m + 1) α → PD (n + 1) β → PD ((m + 1) * (n + 1)) ((Cyclo4.cyc4 LibInt.intR).mul α β) := fun α β ⟨h1, p1⟩ ⟨h2, p2⟩ =>
    ⟨by rw [GaussEuclid.norm_mul, h1, h2]; rfl, prim_mulP _ _ p1 p2⟩
  -- Step A: expand c_m c_n with a δ over γ
  have stepA : (Cyclo4.cyc4 LibInt.intR).mul (c (m + 1)) (c (n + 1))
      = sumK (Cyclo4.cyc4 LibInt.intR) (fun α => sumK (Cyclo4.cyc4 LibInt.intR) (fun β => sumK (Cyclo4.cyc4 LibInt.intR) (fun γ => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (I (m + 1) α) (I (n + 1) β)) (delta (Cyclo4.cyc4 LibInt.intR) γ ((Cyclo4.cyc4 LibInt.intR).mul α β))) γ) (box ((m + 1) * (n + 1)))) (box (n + 1))) (box (m + 1)) := by
    unfold c; rw [sumK_mul]
    refine sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun α => ?_) _
    rw [mul_sumK]
    refine sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun β => ?_) _
    by_cases hα : PD (m + 1) α
    · by_cases hβ : PD (n + 1) β
      · rw [I_pos _ _ hα, I_pos _ _ hβ]
        have hb := abs_le _ _ (hprod α β hα hβ).1
        have e := pick_box (Cyclo4.cyc4 LibInt.intR) (fun γ => γ) ((Cyclo4.cyc4 LibInt.intR).mul α β) _ hb.1 hb.2
        rw [show sumK (Cyclo4.cyc4 LibInt.intR) (fun γ => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (Cyclo4.cyc4 LibInt.intR).one (Cyclo4.cyc4 LibInt.intR).one) (delta (Cyclo4.cyc4 LibInt.intR) γ ((Cyclo4.cyc4 LibInt.intR).mul α β))) γ) (box ((m + 1) * (n + 1)))
            = sumK (Cyclo4.cyc4 LibInt.intR) (fun γ => (Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) γ ((Cyclo4.cyc4 LibInt.intR).mul α β)) γ) (box ((m + 1) * (n + 1))) from sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun γ => by lib_ring) _, e]
        lib_ring
      · rw [I_neg _ _ hβ]
        rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun γ => show (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (I (m + 1) α) (Cyclo4.cyc4 LibInt.intR).zero) (delta (Cyclo4.cyc4 LibInt.intR) γ ((Cyclo4.cyc4 LibInt.intR).mul α β))) γ = (Cyclo4.cyc4 LibInt.intR).zero by lib_ring)]
        rw [zsum]; lib_ring
    · rw [I_neg _ _ hα]
      rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun γ => show (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (Cyclo4.cyc4 LibInt.intR).zero (I (n + 1) β)) (delta (Cyclo4.cyc4 LibInt.intR) γ ((Cyclo4.cyc4 LibInt.intR).mul α β))) γ = (Cyclo4.cyc4 LibInt.intR).zero by lib_ring)]
      rw [zsum]; lib_ring

  rw [stepA]
  -- swap to Σ_γ Σ_α Σ_β
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun α => sumK_swap (Cyclo4.cyc4 LibInt.intR) _ _ _)]
  rw [sumK_swap (Cyclo4.cyc4 LibInt.intR)]
  unfold c
  refine sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun γ => ?_) _
  have e1 : sumK (Cyclo4.cyc4 LibInt.intR) (fun α => sumK (Cyclo4.cyc4 LibInt.intR) (fun β => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (I (m + 1) α) (I (n + 1) β)) (delta (Cyclo4.cyc4 LibInt.intR) γ ((Cyclo4.cyc4 LibInt.intR).mul α β))) γ) (box (n + 1))) (box (m + 1))
      = (Cyclo4.cyc4 LibInt.intR).mul (sumK (Cyclo4.cyc4 LibInt.intR) (fun α => sumK (Cyclo4.cyc4 LibInt.intR) (fun β => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (I (m + 1) α) (I (n + 1) β)) (delta (Cyclo4.cyc4 LibInt.intR) γ ((Cyclo4.cyc4 LibInt.intR).mul α β))) (box (n + 1))) (box (m + 1))) γ := by
    rw [sumK_mul]; exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun α => by rw [sumK_mul]) _
  rw [e1]
  by_cases hγ : PD ((m + 1) * (n + 1)) γ
  · rw [I_pos _ _ hγ]
    match prim_split γ m n s t km kn hγ.1 hst hm hn hγ.2 with
    | ⟨α0, β0, h0, pα0, pβ0, nα0, nβ0⟩ =>
      have pt : ∀ α β, (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (I (m + 1) α) (I (n + 1) β)) (delta (Cyclo4.cyc4 LibInt.intR) γ ((Cyclo4.cyc4 LibInt.intR).mul α β)) = (Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) α α0) (delta (Cyclo4.cyc4 LibInt.intR) β β0) := by
        intro α β
        by_cases hαβ : PD (m + 1) α ∧ PD (n + 1) β ∧ γ = (Cyclo4.cyc4 LibInt.intR).mul α β
        · match hαβ with
          | ⟨hα, hβ, hg⟩ =>
            have u := uniq_split α β α0 β0 _ _ s t hα.1 nα0 nβ0 hst hM0 hα.2 pα0 (hg.symm.trans h0)
            rw [I_pos _ _ hα, I_pos _ _ hβ, ← u.1, ← u.2]
            unfold delta; rw [ite_pos' _ _ hg, ite_pos' _ _ rfl, ite_pos' _ _ rfl]; lib_ring
        · by_cases he : α = α0 ∧ β = β0
          · exfalso; apply hαβ
            rw [he.1, he.2]; exact ⟨⟨nα0, pα0⟩, ⟨nβ0, pβ0⟩, h0⟩
          · have rhs : (Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) α α0) (delta (Cyclo4.cyc4 LibInt.intR) β β0) = (Cyclo4.cyc4 LibInt.intR).zero := by
              unfold delta
              by_cases h1 : α = α0
              · rw [ite_neg' _ _ (fun h2 => he ⟨h1, h2⟩), ite_pos' _ _ h1]; lib_ring
              · rw [ite_neg' _ _ h1]; lib_ring
            rw [rhs]
            by_cases hα : PD (m + 1) α
            · by_cases hβ : PD (n + 1) β
              · have hg : ¬ γ = (Cyclo4.cyc4 LibInt.intR).mul α β := fun hg => hαβ ⟨hα, hβ, hg⟩
                unfold delta; rw [ite_neg' _ _ hg]; lib_ring
              · rw [I_neg _ _ hβ]; lib_ring
            · rw [I_neg _ _ hα]; lib_ring
      rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun α => sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun β => pt α β) _)]
      have ba := abs_le α0 _ nα0
      have bb := abs_le β0 _ nβ0
      rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun α => by rw [← mul_sumK, count_box (Cyclo4.cyc4 LibInt.intR) β0 _ bb.1 bb.2])]
      rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun α => show (Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) α α0) (Cyclo4.cyc4 LibInt.intR).one = (Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) α α0) (Cyclo4.cyc4 LibInt.intR).one from rfl), ← sumK_mul,
        count_box (Cyclo4.cyc4 LibInt.intR) α0 _ ba.1 ba.2]
      lib_ring
  · rw [I_neg _ _ hγ]
    have pt : ∀ α β, (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (I (m + 1) α) (I (n + 1) β)) (delta (Cyclo4.cyc4 LibInt.intR) γ ((Cyclo4.cyc4 LibInt.intR).mul α β)) = (Cyclo4.cyc4 LibInt.intR).zero := by
      intro α β
      by_cases hα : PD (m + 1) α
      · by_cases hβ : PD (n + 1) β
        · have hg : ¬ γ = (Cyclo4.cyc4 LibInt.intR).mul α β := fun hg => hγ (by rw [hg]; exact hprod α β hα hβ)
          unfold delta; rw [ite_neg' _ _ hg]; lib_ring
        · rw [I_neg _ _ hβ]; lib_ring
      · rw [I_neg _ _ hα]; lib_ring
    rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun α => sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun β => pt α β) _)]
    rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun α => zsum _), zsum]

end HeckeMult

#print axioms HeckeMult.count_zl
#print axioms HeckeMult.sumK_pairs
#print axioms HeckeMult.delta_pair
#print axioms HeckeMult.count_box
#print axioms HeckeMult.pick_box
#print axioms HeckeMult.abs_le
#print axioms HeckeMult.idv_iff
#print axioms HeckeMult.pd_iff
#print axioms HeckeMult.c
#print axioms HeckeMult.hecke_mult

lang/HeckeMult11.lean

lean_all/lang/HeckeMult11.lean

/-
  lang/HeckeMult11 — T48/T50, cycle 651: **the theta series of 121b's Hecke character class is multiplicative**: for every
  multiplicative w on ℤ[θ], θ = (1 + √−11)/2,  c_w(n) = Σ_{α ∈ H11, N(α) = n} w(α)  satisfies c_w(mn) = c_w(m)c_w(n) for coprime m, n
  prime to 11.  Enumeration box11 n = HeckeMult.box (4n) (4N = (2a + b)² + 7b²).
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Uniq11
import lang.HeckeMultW

namespace HeckeMult11

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Euc11 Prim11 Uniq11

abbrev box11 (n : Nat) : List (Int × Int) := HeckeMult.box (12 * n)

/-- N(z) = n ⇒ |z.1|, |z.2| ≤ 12n  (11a² + (a + 6b)² = 12N, (2a + b)² + 11b² = 4N). -/
theorem abs_le11 (z : Int × Int) (n : Nat) (h : en z = Int.ofNat n) : Int.natAbs z.1 ≤ 12 * n ∧ Int.natAbs z.2 ≤ 12 * n := by
  have hn : nrm z = n := Int.ofNat.inj (by rw [nrm_cast]; exact h)
  have h4 := four_nrm z
  rw [hn] at h4
  have h8 : 11 * (Int.natAbs z.1 * Int.natAbs z.1) + Int.natAbs (z.1 + 6 * z.2) * Int.natAbs (z.1 + 6 * z.2) = 12 * n := Int.ofNat.inj (by
    show 11 * Int.ofNat (Int.natAbs z.1 * Int.natAbs z.1) + Int.ofNat (Int.natAbs (z.1 + 6 * z.2) * Int.natAbs (z.1 + 6 * z.2)) = 12 * Int.ofNat n
    rw [GaussEuclid.nas, GaussEuclid.nas, ← h]; unfold en; lib_int)
  have seven : ∀ X : Nat, X ≤ 11 * (X * X) := fun X => Nat.le_trans (HeckeMult.le_sq X) (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ (Nat.le_add_left 1 10)))
  constructor
  · exact Nat.le_trans (seven _) (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq h8))
  · exact Nat.le_trans (seven _) (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_trans (Nat.le_of_eq h4.symm) (Nat.mul_le_mul_right n (Nat.le_add_right 4 8))))

def PD11 (n : Nat) (z : Int × Int) : Prop := en z = Int.ofNat n ∧ Prim11 z

theorem prim11_iff (z : Int × Int) :
    (ZMod.md 10 (Int.natAbs (rho z - 1)) = 0 ∨ ZMod.md 10 (Int.natAbs (rho z - 3)) = 0 ∨ ZMod.md 10 (Int.natAbs (rho z - 4)) = 0 ∨ ZMod.md 10 (Int.natAbs (rho z - 5)) = 0 ∨ ZMod.md 10 (Int.natAbs (rho z - 9)) = 0) ↔ Prim11 z := by
  constructor
  · intro h
    rcases h with h | h | h | h | h
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 10 _).mpr h
      exact ⟨e, (Or.inl (by calc rho z = (rho z - 1) + 1 := by lib_int
          _ = _ := by rw [he]; rfl))⟩
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 10 _).mpr h
      exact ⟨e, (Or.inr (Or.inl (by calc rho z = (rho z - 3) + 3 := by lib_int
          _ = _ := by rw [he]; rfl)))⟩
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 10 _).mpr h
      exact ⟨e, (Or.inr (Or.inr (Or.inl (by calc rho z = (rho z - 4) + 4 := by lib_int
          _ = _ := by rw [he]; rfl))))⟩
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 10 _).mpr h
      exact ⟨e, (Or.inr (Or.inr (Or.inr (Or.inl (by calc rho z = (rho z - 5) + 5 := by lib_int
          _ = _ := by rw [he]; rfl)))))⟩
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 10 _).mpr h
      exact ⟨e, (Or.inr (Or.inr (Or.inr (Or.inr (by calc rho z = (rho z - 9) + 9 := by lib_int
          _ = _ := by rw [he]; rfl)))))⟩
  · intro ⟨e, h⟩
    rcases h with h | h | h | h | h
    · exact (Or.inl ((HeckeMult.idv_iff 10 _).mp ⟨e, by rw [h]; show 11 * e + 1 - 1 = 11 * e; lib_int⟩))
    · exact (Or.inr (Or.inl ((HeckeMult.idv_iff 10 _).mp ⟨e, by rw [h]; show 11 * e + 3 - 3 = 11 * e; lib_int⟩)))
    · exact (Or.inr (Or.inr (Or.inl ((HeckeMult.idv_iff 10 _).mp ⟨e, by rw [h]; show 11 * e + 4 - 4 = 11 * e; lib_int⟩))))
    · exact (Or.inr (Or.inr (Or.inr (Or.inl ((HeckeMult.idv_iff 10 _).mp ⟨e, by rw [h]; show 11 * e + 5 - 5 = 11 * e; lib_int⟩)))))
    · exact (Or.inr (Or.inr (Or.inr (Or.inr ((HeckeMult.idv_iff 10 _).mp ⟨e, by rw [h]; show 11 * e + 9 - 9 = 11 * e; lib_int⟩)))))

instance (z : Int × Int) : Decidable (Prim11 z) := decidable_of_iff _ (prim11_iff z)

instance (n : Nat) (z : Int × Int) : Decidable (PD11 n z) := inferInstanceAs (Decidable (_ ∧ _))

theorem zsum11 {A : Type} : ∀ l : List A, sumK (Ring11.ring11 LibInt.intR) (fun _ => (Ring11.ring11 LibInt.intR).zero) l = (Ring11.ring11 LibInt.intR).zero
  | [] => rfl
  | _ :: l => by show (Ring11.ring11 LibInt.intR).add (Ring11.ring11 LibInt.intR).zero (sumK (Ring11.ring11 LibInt.intR) (fun _ => (Ring11.ring11 LibInt.intR).zero) l) = _; rw [zsum11 l]; exact (Ring11.ring11 LibInt.intR).zero_add _

def I11 (n : Nat) (z : Int × Int) : Int × Int := if PD11 n z then (Ring11.ring11 LibInt.intR).one else (Ring11.ring11 LibInt.intR).zero

theorem I11_pos (n : Nat) (z : Int × Int) (h : PD11 n z) : I11 n z = (Ring11.ring11 LibInt.intR).one := ite_pos' _ _ h
theorem I11_neg (n : Nat) (z : Int × Int) (h : ¬ PD11 n z) : I11 n z = (Ring11.ring11 LibInt.intR).zero := ite_neg' _ _ h

/-- c_w(n) = Σ_{α ≡ 1 (3), N(α) = n} w(α). -/
def cw11 (w : Int × Int → Int × Int) (n : Nat) : Int × Int := sumK (Ring11.ring11 LibInt.intR) (fun z => (Ring11.ring11 LibInt.intR).mul (I11 n z) (w z)) (box11 n)

/-- **c_w(mn) = c_w(m) c_w(n)** for coprime m, n prime to 3 and multiplicative w on ℤ[ω]. -/
theorem hecke_mult11 (w : Int × Int → Int × Int) (hw : ∀ α β, w ((Ring11.ring11 LibInt.intR).mul α β) = (Ring11.ring11 LibInt.intR).mul (w α) (w β)) (m n : Nat) (s t : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 11 * k) (hn : ∀ k, Int.ofNat (n + 1) ≠ 11 * k) :
    cw11 w ((m + 1) * (n + 1)) = (Ring11.ring11 LibInt.intR).mul (cw11 w (m + 1)) (cw11 w (n + 1)) := by
  have hM0 : Int.ofNat (m + 1) ≠ 0 := fun e => Nat.noConfusion (Int.ofNat.inj e)
  -- product of primaries of norms m, n is primary of norm mn
  have hprod : ∀ α β, PD11 (m + 1) α → PD11 (n + 1) β → PD11 ((m + 1) * (n + 1)) ((Ring11.ring11 LibInt.intR).mul α β) := fun α β ⟨h1, p1⟩ ⟨h2, p2⟩ =>
    ⟨by rw [en_mul, h1, h2]; rfl, prim11_mul _ _ p1 p2⟩
  -- Step A: expand c_m c_n with a δ over γ
  have stepA : (Ring11.ring11 LibInt.intR).mul (cw11 w (m + 1)) (cw11 w (n + 1))
      = sumK (Ring11.ring11 LibInt.intR) (fun α => sumK (Ring11.ring11 LibInt.intR) (fun β => sumK (Ring11.ring11 LibInt.intR) (fun γ => (Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul (I11 (m + 1) α) (I11 (n + 1) β)) (delta (Ring11.ring11 LibInt.intR) γ ((Ring11.ring11 LibInt.intR).mul α β))) (w γ)) (box11 ((m + 1) * (n + 1)))) (box11 (n + 1))) (b  -- (+19 chars; full line in the Zenodo source)
    unfold cw11; rw [sumK_mul]
    refine sumK_ext (Ring11.ring11 LibInt.intR) _ _ (fun α => ?_) _
    rw [mul_sumK]
    refine sumK_ext (Ring11.ring11 LibInt.intR) _ _ (fun β => ?_) _
    by_cases hα : PD11 (m + 1) α
    · by_cases hβ : PD11 (n + 1) β
      · rw [I11_pos _ _ hα, I11_pos _ _ hβ]
        have hb := abs_le11 _ _ (hprod α β hα hβ).1
        have e := HeckeMult.pick_box (Ring11.ring11 LibInt.intR) (fun γ => w γ) ((Ring11.ring11 LibInt.intR).mul α β) _ hb.1 hb.2
        rw [show sumK (Ring11.ring11 LibInt.intR) (fun γ => (Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul (Ring11.ring11 LibInt.intR).one (Ring11.ring11 LibInt.intR).one) (delta (Ring11.ring11 LibInt.intR) γ ((Ring11.ring11 LibInt.intR).mul α β))) (w γ)) (box11 ((m + 1) * (n + 1)))
            = sumK (Ring11.ring11 LibInt.intR) (fun γ => (Ring11.ring11 LibInt.intR).mul (delta (Ring11.ring11 LibInt.intR) γ ((Ring11.ring11 LibInt.intR).mul α β)) (w γ)) (box11 ((m + 1) * (n + 1))) from sumK_ext (Ring11.ring11 LibInt.intR) _ _ (fun γ => by lib_ring) _, e, hw]
        lib_ring
      · rw [I11_neg _ _ hβ]
        rw [sumK_ext (Ring11.ring11 LibInt.intR) _ _ (fun γ => show (Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul (I11 (m + 1) α) (Ring11.ring11 LibInt.intR).zero) (delta (Ring11.ring11 LibInt.intR) γ ((Ring11.ring11 LibInt.intR).mul α β))) (w γ) = (Ring11.ring11 LibInt.intR).zero by lib_ring)]
        rw [zsum11]; lib_ring
    · rw [I11_neg _ _ hα]
      rw [sumK_ext (Ring11.ring11 LibInt.intR) _ _ (fun γ => show (Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul (Ring11.ring11 LibInt.intR).zero (I11 (n + 1) β)) (delta (Ring11.ring11 LibInt.intR) γ ((Ring11.ring11 LibInt.intR).mul α β))) (w γ) = (Ring11.ring11 LibInt.intR).zero by lib_ring)]
      rw [zsum11]; lib_ring

  rw [stepA]
  -- swap to Σ_γ Σ_α Σ_β
  rw [sumK_ext (Ring11.ring11 LibInt.intR) _ _ (fun α => sumK_swap (Ring11.ring11 LibInt.intR) _ _ _)]
  rw [sumK_swap (Ring11.ring11 LibInt.intR)]
  unfold cw11
  refine sumK_ext (Ring11.ring11 LibInt.intR) _ _ (fun γ => ?_) _
  have e1 : sumK (Ring11.ring11 LibInt.intR) (fun α => sumK (Ring11.ring11 LibInt.intR) (fun β => (Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul (I11 (m + 1) α) (I11 (n + 1) β)) (delta (Ring11.ring11 LibInt.intR) γ ((Ring11.ring11 LibInt.intR).mul α β))) (w γ)) (box11 (n + 1))) (box11 (m + 1))
      = (Ring11.ring11 LibInt.intR).mul (sumK (Ring11.ring11 LibInt.intR) (fun α => sumK (Ring11.ring11 LibInt.intR) (fun β => (Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul (I11 (m + 1) α) (I11 (n + 1) β)) (delta (Ring11.ring11 LibInt.intR) γ ((Ring11.ring11 LibInt.intR).mul α β))) (box11 (n + 1))) (box11 (m + 1))) (w γ) := by
    rw [sumK_mul]; exact sumK_ext (Ring11.ring11 LibInt.intR) _ _ (fun α => by rw [sumK_mul]) _
  rw [e1]
  by_cases hγ : PD11 ((m + 1) * (n + 1)) γ
  · rw [I11_pos _ _ hγ]
    match prim11_split γ m n s t hγ.1 hst hm hn hγ.2 with
    | ⟨α0, β0, h0, pα0, pβ0, nα0, nβ0⟩ =>
      have pt : ∀ α β, (Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul (I11 (m + 1) α) (I11 (n + 1) β)) (delta (Ring11.ring11 LibInt.intR) γ ((Ring11.ring11 LibInt.intR).mul α β)) = (Ring11.ring11 LibInt.intR).mul (delta (Ring11.ring11 LibInt.intR) α α0) (delta (Ring11.ring11 LibInt.intR) β β0) := by
        intro α β
        by_cases hαβ : PD11 (m + 1) α ∧ PD11 (n + 1) β ∧ γ = (Ring11.ring11 LibInt.intR).mul α β
        · match hαβ with
          | ⟨hα, hβ, hg⟩ =>
            have u := uniq_split11 α β α0 β0 _ _ s t hα.1 nα0 nβ0 hst hM0 hα.2 pα0 (hg.symm.trans h0)
            rw [I11_pos _ _ hα, I11_pos _ _ hβ, ← u.1, ← u.2]
            unfold delta; rw [ite_pos' _ _ hg, ite_pos' _ _ rfl, ite_pos' _ _ rfl]; lib_ring
        · by_cases he : α = α0 ∧ β = β0
          · exfalso; apply hαβ
            rw [he.1, he.2]; exact ⟨⟨nα0, pα0⟩, ⟨nβ0, pβ0⟩, h0⟩
          · have rhs : (Ring11.ring11 LibInt.intR).mul (delta (Ring11.ring11 LibInt.intR) α α0) (delta (Ring11.ring11 LibInt.intR) β β0) = (Ring11.ring11 LibInt.intR).zero := by
              unfold delta
              by_cases h1 : α = α0
              · rw [ite_neg' _ _ (fun h2 => he ⟨h1, h2⟩), ite_pos' _ _ h1]; lib_ring
              · rw [ite_neg' _ _ h1]; lib_ring
            rw [rhs]
            by_cases hα : PD11 (m + 1) α
            · by_cases hβ : PD11 (n + 1) β
              · have hg : ¬ γ = (Ring11.ring11 LibInt.intR).mul α β := fun hg => hαβ ⟨hα, hβ, hg⟩
                unfold delta; rw [ite_neg' _ _ hg]; lib_ring
              · rw [I11_neg _ _ hβ]; lib_ring
            · rw [I11_neg _ _ hα]; lib_ring
      rw [sumK_ext (Ring11.ring11 LibInt.intR) _ _ (fun α => sumK_ext (Ring11.ring11 LibInt.intR) _ _ (fun β => pt α β) _)]
      have ba := abs_le11 α0 _ nα0
      have bb := abs_le11 β0 _ nβ0
      rw [sumK_ext (Ring11.ring11 LibInt.intR) _ _ (fun α => by rw [← mul_sumK, HeckeMult.count_box (Ring11.ring11 LibInt.intR) β0 _ bb.1 bb.2])]
      rw [sumK_ext (Ring11.ring11 LibInt.intR) _ _ (fun α => show (Ring11.ring11 LibInt.intR).mul (delta (Ring11.ring11 LibInt.intR) α α0) (Ring11.ring11 LibInt.intR).one = (Ring11.ring11 LibInt.intR).mul (delta (Ring11.ring11 LibInt.intR) α α0) (Ring11.ring11 LibInt.intR).one from rfl), ← sumK_mul,
        HeckeMult.count_box (Ring11.ring11 LibInt.intR) α0 _ ba.1 ba.2]
      lib_ring
  · rw [I11_neg _ _ hγ]
    have pt : ∀ α β, (Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul (I11 (m + 1) α) (I11 (n + 1) β)) (delta (Ring11.ring11 LibInt.intR) γ ((Ring11.ring11 LibInt.intR).mul α β)) = (Ring11.ring11 LibInt.intR).zero := by
      intro α β
      by_cases hα : PD11 (m + 1) α
      · by_cases hβ : PD11 (n + 1) β
        · have hg : ¬ γ = (Ring11.ring11 LibInt.intR).mul α β := fun hg => hγ (by rw [hg]; exact hprod α β hα hβ)
          unfold delta; rw [ite_neg' _ _ hg]; lib_ring
        · rw [I11_neg _ _ hβ]; lib_ring
      · rw [I11_neg _ _ hα]; lib_ring
    rw [sumK_ext (Ring11.ring11 LibInt.intR) _ _ (fun α => sumK_ext (Ring11.ring11 LibInt.intR) _ _ (fun β => pt α β) _)]
    rw [sumK_ext (Ring11.ring11 LibInt.intR) _ _ (fun α => zsum11 _), zsum11]

/-- **The Hecke characters α ↦ α^k of ℚ(√−3)**: c_k(mn) = c_k(m) c_k(n). -/
theorem hecke_mult11_pow (k : Nat) (m n : Nat) (s t : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 11 * k) (hn : ∀ k, Int.ofNat (n + 1) ≠ 11 * k) :
    cw11 (fun z => Frobenius.pw (Ring11.ring11 LibInt.intR) z k) ((m + 1) * (n + 1)) =
      (Ring11.ring11 LibInt.intR).mul (cw11 (fun z => Frobenius.pw (Ring11.ring11 LibInt.intR) z k) (m + 1)) (cw11 (fun z => Frobenius.pw (Ring11.ring11 LibInt.intR) z k) (n + 1)) :=
  hecke_mult11 _ (fun α β => Frobenius.pw_mul _ α β k) m n s t hst hm hn

end HeckeMult11

#print axioms HeckeMult11.abs_le11
#print axioms HeckeMult11.prim11_iff
#print axioms HeckeMult11.zsum11
#print axioms HeckeMult11.I11_pos
#print axioms HeckeMult11.I11_neg
#print axioms HeckeMult11.hecke_mult11
#print axioms HeckeMult11.hecke_mult11_pow
#print axioms HeckeMult11.cw11

lang/HeckeMult163.lean

lean_all/lang/HeckeMult163.lean

/-
  lang/HeckeMult163 — T48/T50, T48: **the theta series of ℚ(√−163)'s Hecke character class is multiplicative**: for every
  multiplicative w on ℤ[θ], θ = (1 + √−163)/2,  c_w(n) = Σ_{α ∈ H163, N(α) = n} w(α)  satisfies c_w(mn) = c_w(m)c_w(n) for coprime m, n
  prime to 163.  Enumeration box163 n = HeckeMult.box (164n).
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Uniq163
import lang.HeckeMultW

namespace HeckeMult163

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Euc163 Prim163 Uniq163

abbrev box163 (n : Nat) : List (Int × Int) := HeckeMult.box (164 * n)

/-- D a² + (a + c b)² = E (a² + ab + t b²) when 2c = E, c² = E t, D + 1 = E. -/
theorem sqgen (a b c D E t : Int) (h1 : 2 * c = E) (h2 : c * c = E * t) (h3 : D + 1 = E) :
    D * (a * a) + (a + c * b) * (a + c * b) = E * (a * a + a * b + t * (b * b)) := by
  calc D * (a * a) + (a + c * b) * (a + c * b) = (D + 1) * (a * a) + (2 * c) * (a * b) + (c * c) * (b * b) := by lib_int
    _ = E * (a * a) + E * (a * b) + (E * t) * (b * b) := by rw [h3, h1, h2]
    _ = _ := by lib_int

/-- N(z) = n ⇒ |z.1|, |z.2| ≤ 164n  (163a² + (a + 82b)² = 164N, (2a + b)² + 163b² = 4N). -/
theorem abs_le163 (z : Int × Int) (n : Nat) (h : en z = Int.ofNat n) : Int.natAbs z.1 ≤ 164 * n ∧ Int.natAbs z.2 ≤ 164 * n := by
  have hn : nrm z = n := Int.ofNat.inj (by rw [nrm_cast]; exact h)
  have h4 := four_nrm z
  rw [hn] at h4
  have h8 : 163 * (Int.natAbs z.1 * Int.natAbs z.1) + Int.natAbs (z.1 + 82 * z.2) * Int.natAbs (z.1 + 82 * z.2) = 164 * n := Int.ofNat.inj (by
    show 163 * Int.ofNat (Int.natAbs z.1 * Int.natAbs z.1) + Int.ofNat (Int.natAbs (z.1 + 82 * z.2) * Int.natAbs (z.1 + 82 * z.2)) = 164 * Int.ofNat n
    rw [GaussEuclid.nas, GaussEuclid.nas, ← h]; unfold en; exact sqgen _ _ 82 163 164 41 rfl rfl rfl)
  have seven : ∀ X : Nat, X ≤ 163 * (X * X) := fun X => Nat.le_trans (HeckeMult.le_sq X) (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ (Nat.le_add_left 1 162)))
  constructor
  · exact Nat.le_trans (seven _) (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq h8))
  · exact Nat.le_trans (seven _) (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_trans (Nat.le_of_eq h4.symm) (Nat.mul_le_mul_right n (Nat.le_add_right 4 160))))

def PD163 (n : Nat) (z : Int × Int) : Prop := en z = Int.ofNat n ∧ Prim163 z

instance (n : Nat) (z : Int × Int) : Decidable (PD163 n z) := inferInstanceAs (Decidable (_ ∧ _))

theorem zsum163 {A : Type} : ∀ l : List A, sumK (Ring163.ring163 LibInt.intR) (fun _ => (Ring163.ring163 LibInt.intR).zero) l = (Ring163.ring163 LibInt.intR).zero
  | [] => rfl
  | _ :: l => by show (Ring163.ring163 LibInt.intR).add (Ring163.ring163 LibInt.intR).zero (sumK (Ring163.ring163 LibInt.intR) (fun _ => (Ring163.ring163 LibInt.intR).zero) l) = _; rw [zsum163 l]; exact (Ring163.ring163 LibInt.intR).zero_add _

def I163 (n : Nat) (z : Int × Int) : Int × Int := if PD163 n z then (Ring163.ring163 LibInt.intR).one else (Ring163.ring163 LibInt.intR).zero

theorem I163_pos (n : Nat) (z : Int × Int) (h : PD163 n z) : I163 n z = (Ring163.ring163 LibInt.intR).one := ite_pos' _ _ h
theorem I163_neg (n : Nat) (z : Int × Int) (h : ¬ PD163 n z) : I163 n z = (Ring163.ring163 LibInt.intR).zero := ite_neg' _ _ h

/-- c_w(n) = Σ_{α ≡ 1 (3), N(α) = n} w(α). -/
def cw163 (w : Int × Int → Int × Int) (n : Nat) : Int × Int := sumK (Ring163.ring163 LibInt.intR) (fun z => (Ring163.ring163 LibInt.intR).mul (I163 n z) (w z)) (box163 n)

/-- **c_w(mn) = c_w(m) c_w(n)** for coprime m, n prime to 3 and multiplicative w on ℤ[ω]. -/
theorem hecke_mult163 (w : Int × Int → Int × Int) (hw : ∀ α β, w ((Ring163.ring163 LibInt.intR).mul α β) = (Ring163.ring163 LibInt.intR).mul (w α) (w β)) (m n : Nat) (s t : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 163 * k) (hn : ∀ k, Int.ofNat (n + 1) ≠ 163 * k) :
    cw163 w ((m + 1) * (n + 1)) = (Ring163.ring163 LibInt.intR).mul (cw163 w (m + 1)) (cw163 w (n + 1)) := by
  have hM0 : Int.ofNat (m + 1) ≠ 0 := fun e => Nat.noConfusion (Int.ofNat.inj e)
  -- product of primaries of norms m, n is primary of norm mn
  have hprod : ∀ α β, PD163 (m + 1) α → PD163 (n + 1) β → PD163 ((m + 1) * (n + 1)) ((Ring163.ring163 LibInt.intR).mul α β) := fun α β ⟨h1, p1⟩ ⟨h2, p2⟩ =>
    ⟨by rw [en_mul, h1, h2]; rfl, prim_mul _ _ p1 p2⟩
  -- Step A: expand c_m c_n with a δ over γ
  have stepA : (Ring163.ring163 LibInt.intR).mul (cw163 w (m + 1)) (cw163 w (n + 1))
      = sumK (Ring163.ring163 LibInt.intR) (fun α => sumK (Ring163.ring163 LibInt.intR) (fun β => sumK (Ring163.ring163 LibInt.intR) (fun γ => (Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul (I163 (m + 1) α) (I163 (n + 1) β)) (delta (Ring163.ring163 LibInt.intR) γ ((Ring163.ring163 LibInt.intR).mul α β))) (w γ)) (box163 ((m + 1) * (n + 1))))   -- (+40 chars; full line in the Zenodo source)
    unfold cw163; rw [sumK_mul]
    refine sumK_ext (Ring163.ring163 LibInt.intR) _ _ (fun α => ?_) _
    rw [mul_sumK]
    refine sumK_ext (Ring163.ring163 LibInt.intR) _ _ (fun β => ?_) _
    by_cases hα : PD163 (m + 1) α
    · by_cases hβ : PD163 (n + 1) β
      · rw [I163_pos _ _ hα, I163_pos _ _ hβ]
        have hb := abs_le163 _ _ (hprod α β hα hβ).1
        have e := HeckeMult.pick_box (Ring163.ring163 LibInt.intR) (fun γ => w γ) ((Ring163.ring163 LibInt.intR).mul α β) _ hb.1 hb.2
        rw [show sumK (Ring163.ring163 LibInt.intR) (fun γ => (Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul (Ring163.ring163 LibInt.intR).one (Ring163.ring163 LibInt.intR).one) (delta (Ring163.ring163 LibInt.intR) γ ((Ring163.ring163 LibInt.intR).mul α β))) (w γ)) (box163 ((m + 1) * (n + 1)))
            = sumK (Ring163.ring163 LibInt.intR) (fun γ => (Ring163.ring163 LibInt.intR).mul (delta (Ring163.ring163 LibInt.intR) γ ((Ring163.ring163 LibInt.intR).mul α β)) (w γ)) (box163 ((m + 1) * (n + 1))) from sumK_ext (Ring163.ring163 LibInt.intR) _ _ (fun γ => by lib_ring) _, e, hw]
        lib_ring
      · rw [I163_neg _ _ hβ]
        rw [sumK_ext (Ring163.ring163 LibInt.intR) _ _ (fun γ => show (Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul (I163 (m + 1) α) (Ring163.ring163 LibInt.intR).zero) (delta (Ring163.ring163 LibInt.intR) γ ((Ring163.ring163 LibInt.intR).mul α β))) (w γ) = (Ring163.ring163 LibInt.intR).zero by lib_ring)]
        rw [zsum163]; lib_ring
    · rw [I163_neg _ _ hα]
      rw [sumK_ext (Ring163.ring163 LibInt.intR) _ _ (fun γ => show (Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul (Ring163.ring163 LibInt.intR).zero (I163 (n + 1) β)) (delta (Ring163.ring163 LibInt.intR) γ ((Ring163.ring163 LibInt.intR).mul α β))) (w γ) = (Ring163.ring163 LibInt.intR).zero by lib_ring)]
      rw [zsum163]; lib_ring

  rw [stepA]
  -- swap to Σ_γ Σ_α Σ_β
  rw [sumK_ext (Ring163.ring163 LibInt.intR) _ _ (fun α => sumK_swap (Ring163.ring163 LibInt.intR) _ _ _)]
  rw [sumK_swap (Ring163.ring163 LibInt.intR)]
  unfold cw163
  refine sumK_ext (Ring163.ring163 LibInt.intR) _ _ (fun γ => ?_) _
  have e1 : sumK (Ring163.ring163 LibInt.intR) (fun α => sumK (Ring163.ring163 LibInt.intR) (fun β => (Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul (I163 (m + 1) α) (I163 (n + 1) β)) (delta (Ring163.ring163 LibInt.intR) γ ((Ring163.ring163 LibInt.intR).mul α β))) (w γ)) (box163 (n + 1))) (box163 (m + 1))
      = (Ring163.ring163 LibInt.intR).mul (sumK (Ring163.ring163 LibInt.intR) (fun α => sumK (Ring163.ring163 LibInt.intR) (fun β => (Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul (I163 (m + 1) α) (I163 (n + 1) β)) (delta (Ring163.ring163 LibInt.intR) γ ((Ring163.ring163 LibInt.intR).mul α β))) (box163 (n + 1))) (box163 (m + 1))) (w γ) := by
    rw [sumK_mul]; exact sumK_ext (Ring163.ring163 LibInt.intR) _ _ (fun α => by rw [sumK_mul]) _
  rw [e1]
  by_cases hγ : PD163 ((m + 1) * (n + 1)) γ
  · rw [I163_pos _ _ hγ]
    match prim163_split γ m n s t hγ.1 hst hm hn hγ.2 with
    | ⟨α0, β0, h0, pα0, pβ0, nα0, nβ0⟩ =>
      have pt : ∀ α β, (Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul (I163 (m + 1) α) (I163 (n + 1) β)) (delta (Ring163.ring163 LibInt.intR) γ ((Ring163.ring163 LibInt.intR).mul α β)) = (Ring163.ring163 LibInt.intR).mul (delta (Ring163.ring163 LibInt.intR) α α0) (delta (Ring163.ring163 LibInt.intR) β β0) := by
        intro α β
        by_cases hαβ : PD163 (m + 1) α ∧ PD163 (n + 1) β ∧ γ = (Ring163.ring163 LibInt.intR).mul α β
        · match hαβ with
          | ⟨hα, hβ, hg⟩ =>
            have u := uniq_split163 α β α0 β0 _ _ s t hα.1 nα0 nβ0 hst hM0 hα.2 pα0 (hg.symm.trans h0)
            rw [I163_pos _ _ hα, I163_pos _ _ hβ, ← u.1, ← u.2]
            unfold delta; rw [ite_pos' _ _ hg, ite_pos' _ _ rfl, ite_pos' _ _ rfl]; lib_ring
        · by_cases he : α = α0 ∧ β = β0
          · exfalso; apply hαβ
            rw [he.1, he.2]; exact ⟨⟨nα0, pα0⟩, ⟨nβ0, pβ0⟩, h0⟩
          · have rhs : (Ring163.ring163 LibInt.intR).mul (delta (Ring163.ring163 LibInt.intR) α α0) (delta (Ring163.ring163 LibInt.intR) β β0) = (Ring163.ring163 LibInt.intR).zero := by
              unfold delta
              by_cases h1 : α = α0
              · rw [ite_neg' _ _ (fun h2 => he ⟨h1, h2⟩), ite_pos' _ _ h1]; lib_ring
              · rw [ite_neg' _ _ h1]; lib_ring
            rw [rhs]
            by_cases hα : PD163 (m + 1) α
            · by_cases hβ : PD163 (n + 1) β
              · have hg : ¬ γ = (Ring163.ring163 LibInt.intR).mul α β := fun hg => hαβ ⟨hα, hβ, hg⟩
                unfold delta; rw [ite_neg' _ _ hg]; lib_ring
              · rw [I163_neg _ _ hβ]; lib_ring
            · rw [I163_neg _ _ hα]; lib_ring
      rw [sumK_ext (Ring163.ring163 LibInt.intR) _ _ (fun α => sumK_ext (Ring163.ring163 LibInt.intR) _ _ (fun β => pt α β) _)]
      have ba := abs_le163 α0 _ nα0
      have bb := abs_le163 β0 _ nβ0
      rw [sumK_ext (Ring163.ring163 LibInt.intR) _ _ (fun α => by rw [← mul_sumK, HeckeMult.count_box (Ring163.ring163 LibInt.intR) β0 _ bb.1 bb.2])]
      rw [sumK_ext (Ring163.ring163 LibInt.intR) _ _ (fun α => show (Ring163.ring163 LibInt.intR).mul (delta (Ring163.ring163 LibInt.intR) α α0) (Ring163.ring163 LibInt.intR).one = (Ring163.ring163 LibInt.intR).mul (delta (Ring163.ring163 LibInt.intR) α α0) (Ring163.ring163 LibInt.intR).one from rfl), ← sumK_mul,
        HeckeMult.count_box (Ring163.ring163 LibInt.intR) α0 _ ba.1 ba.2]
      lib_ring
  · rw [I163_neg _ _ hγ]
    have pt : ∀ α β, (Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul (I163 (m + 1) α) (I163 (n + 1) β)) (delta (Ring163.ring163 LibInt.intR) γ ((Ring163.ring163 LibInt.intR).mul α β)) = (Ring163.ring163 LibInt.intR).zero := by
      intro α β
      by_cases hα : PD163 (m + 1) α
      · by_cases hβ : PD163 (n + 1) β
        · have hg : ¬ γ = (Ring163.ring163 LibInt.intR).mul α β := fun hg => hγ (by rw [hg]; exact hprod α β hα hβ)
          unfold delta; rw [ite_neg' _ _ hg]; lib_ring
        · rw [I163_neg _ _ hβ]; lib_ring
      · rw [I163_neg _ _ hα]; lib_ring
    rw [sumK_ext (Ring163.ring163 LibInt.intR) _ _ (fun α => sumK_ext (Ring163.ring163 LibInt.intR) _ _ (fun β => pt α β) _)]
    rw [sumK_ext (Ring163.ring163 LibInt.intR) _ _ (fun α => zsum163 _), zsum163]

/-- **The Hecke characters α ↦ α^k of ℚ(√−3)**: c_k(mn) = c_k(m) c_k(n). -/
theorem hecke_mult163_pow (k : Nat) (m n : Nat) (s t : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 163 * k) (hn : ∀ k, Int.ofNat (n + 1) ≠ 163 * k) :
    cw163 (fun z => Frobenius.pw (Ring163.ring163 LibInt.intR) z k) ((m + 1) * (n + 1)) =
      (Ring163.ring163 LibInt.intR).mul (cw163 (fun z => Frobenius.pw (Ring163.ring163 LibInt.intR) z k) (m + 1)) (cw163 (fun z => Frobenius.pw (Ring163.ring163 LibInt.intR) z k) (n + 1)) :=
  hecke_mult163 _ (fun α β => Frobenius.pw_mul _ α β k) m n s t hst hm hn

end HeckeMult163

#print axioms HeckeMult163.sqgen
#print axioms HeckeMult163.abs_le163
#print axioms HeckeMult163.zsum163
#print axioms HeckeMult163.I163_pos
#print axioms HeckeMult163.I163_neg
#print axioms HeckeMult163.hecke_mult163
#print axioms HeckeMult163.hecke_mult163_pow
#print axioms HeckeMult163.cw163

lang/HeckeMult19.lean

lean_all/lang/HeckeMult19.lean

/-
  lang/HeckeMult19 — T48/T50, cycle 662: **the theta series of 361a's Hecke character class is multiplicative**: for every
  multiplicative w on ℤ[θ], θ = (1 + √−19)/2,  c_w(n) = Σ_{α ∈ H19, N(α) = n} w(α)  satisfies c_w(mn) = c_w(m)c_w(n) for coprime m, n
  prime to 19.  Enumeration box19 n = HeckeMult.box (20n).
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Uniq19
import lang.HeckeMultW

namespace HeckeMult19

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Euc19 Prim19 Uniq19

abbrev box19 (n : Nat) : List (Int × Int) := HeckeMult.box (20 * n)

/-- N(z) = n ⇒ |z.1|, |z.2| ≤ 20n  (19a² + (a + 10b)² = 20N, (2a + b)² + 19b² = 4N). -/
theorem abs_le19 (z : Int × Int) (n : Nat) (h : en z = Int.ofNat n) : Int.natAbs z.1 ≤ 20 * n ∧ Int.natAbs z.2 ≤ 20 * n := by
  have hn : nrm z = n := Int.ofNat.inj (by rw [nrm_cast]; exact h)
  have h4 := four_nrm z
  rw [hn] at h4
  have h8 : 19 * (Int.natAbs z.1 * Int.natAbs z.1) + Int.natAbs (z.1 + 10 * z.2) * Int.natAbs (z.1 + 10 * z.2) = 20 * n := Int.ofNat.inj (by
    show 19 * Int.ofNat (Int.natAbs z.1 * Int.natAbs z.1) + Int.ofNat (Int.natAbs (z.1 + 10 * z.2) * Int.natAbs (z.1 + 10 * z.2)) = 20 * Int.ofNat n
    rw [GaussEuclid.nas, GaussEuclid.nas, ← h]; unfold en; lib_int)
  have seven : ∀ X : Nat, X ≤ 19 * (X * X) := fun X => Nat.le_trans (HeckeMult.le_sq X) (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ (Nat.le_add_left 1 18)))
  constructor
  · exact Nat.le_trans (seven _) (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq h8))
  · exact Nat.le_trans (seven _) (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_trans (Nat.le_of_eq h4.symm) (Nat.mul_le_mul_right n (Nat.le_add_right 4 16))))

def PD19 (n : Nat) (z : Int × Int) : Prop := en z = Int.ofNat n ∧ Prim19 z

theorem prim19_iff (z : Int × Int) :
    (ZMod.md 18 (Int.natAbs (rho z - 1)) = 0 ∨ ZMod.md 18 (Int.natAbs (rho z - 4)) = 0 ∨ ZMod.md 18 (Int.natAbs (rho z - 5)) = 0 ∨ ZMod.md 18 (Int.natAbs (rho z - 6)) = 0 ∨ ZMod.md 18 (Int.natAbs (rho z - 7)) = 0 ∨ ZMod.md 18 (Int.natAbs (rho z - 9)) = 0 ∨ ZMod.md 18 (Int.natAbs (rho z - 11)) = 0 ∨ ZMod.md 18 (Int.natAbs (rho z - 16)) = 0 ∨ ZMod.md 18 (Int.natAbs (rho z - 17)) = 0) ↔ Prim19 z := b  -- (+1 chars; full line in the Zenodo source)
  constructor
  · intro h
    rcases h with h | h | h | h | h | h | h | h | h
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 18 _).mpr h
      exact ⟨e, (Or.inl (by calc rho z = (rho z - 1) + 1 := by lib_int
          _ = _ := by rw [he]; rfl))⟩
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 18 _).mpr h
      exact ⟨e, (Or.inr (Or.inl (by calc rho z = (rho z - 4) + 4 := by lib_int
          _ = _ := by rw [he]; rfl)))⟩
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 18 _).mpr h
      exact ⟨e, (Or.inr (Or.inr (Or.inl (by calc rho z = (rho z - 5) + 5 := by lib_int
          _ = _ := by rw [he]; rfl))))⟩
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 18 _).mpr h
      exact ⟨e, (Or.inr (Or.inr (Or.inr (Or.inl (by calc rho z = (rho z - 6) + 6 := by lib_int
          _ = _ := by rw [he]; rfl)))))⟩
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 18 _).mpr h
      exact ⟨e, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by calc rho z = (rho z - 7) + 7 := by lib_int
          _ = _ := by rw [he]; rfl))))))⟩
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 18 _).mpr h
      exact ⟨e, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by calc rho z = (rho z - 9) + 9 := by lib_int
          _ = _ := by rw [he]; rfl)))))))⟩
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 18 _).mpr h
      exact ⟨e, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by calc rho z = (rho z - 11) + 11 := by lib_int
          _ = _ := by rw [he]; rfl))))))))⟩
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 18 _).mpr h
      exact ⟨e, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by calc rho z = (rho z - 16) + 16 := by lib_int
          _ = _ := by rw [he]; rfl)))))))))⟩
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 18 _).mpr h
      exact ⟨e, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by calc rho z = (rho z - 17) + 17 := by lib_int
          _ = _ := by rw [he]; rfl)))))))))⟩
  · intro ⟨e, h⟩
    rcases h with h | h | h | h | h | h | h | h | h
    · exact (Or.inl ((HeckeMult.idv_iff 18 _).mp ⟨e, by rw [h]; show 19 * e + 1 - 1 = 19 * e; lib_int⟩))
    · exact (Or.inr (Or.inl ((HeckeMult.idv_iff 18 _).mp ⟨e, by rw [h]; show 19 * e + 4 - 4 = 19 * e; lib_int⟩)))
    · exact (Or.inr (Or.inr (Or.inl ((HeckeMult.idv_iff 18 _).mp ⟨e, by rw [h]; show 19 * e + 5 - 5 = 19 * e; lib_int⟩))))
    · exact (Or.inr (Or.inr (Or.inr (Or.inl ((HeckeMult.idv_iff 18 _).mp ⟨e, by rw [h]; show 19 * e + 6 - 6 = 19 * e; lib_int⟩)))))
    · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ((HeckeMult.idv_iff 18 _).mp ⟨e, by rw [h]; show 19 * e + 7 - 7 = 19 * e; lib_int⟩))))))
    · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ((HeckeMult.idv_iff 18 _).mp ⟨e, by rw [h]; show 19 * e + 9 - 9 = 19 * e; lib_int⟩)))))))
    · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ((HeckeMult.idv_iff 18 _).mp ⟨e, by rw [h]; show 19 * e + 11 - 11 = 19 * e; lib_int⟩))))))))
    · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ((HeckeMult.idv_iff 18 _).mp ⟨e, by rw [h]; show 19 * e + 16 - 16 = 19 * e; lib_int⟩)))))))))
    · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ((HeckeMult.idv_iff 18 _).mp ⟨e, by rw [h]; show 19 * e + 17 - 17 = 19 * e; lib_int⟩)))))))))

instance (z : Int × Int) : Decidable (Prim19 z) := decidable_of_iff _ (prim19_iff z)

instance (n : Nat) (z : Int × Int) : Decidable (PD19 n z) := inferInstanceAs (Decidable (_ ∧ _))

theorem zsum19 {A : Type} : ∀ l : List A, sumK (Ring19.ring19 LibInt.intR) (fun _ => (Ring19.ring19 LibInt.intR).zero) l = (Ring19.ring19 LibInt.intR).zero
  | [] => rfl
  | _ :: l => by show (Ring19.ring19 LibInt.intR).add (Ring19.ring19 LibInt.intR).zero (sumK (Ring19.ring19 LibInt.intR) (fun _ => (Ring19.ring19 LibInt.intR).zero) l) = _; rw [zsum19 l]; exact (Ring19.ring19 LibInt.intR).zero_add _

def I19 (n : Nat) (z : Int × Int) : Int × Int := if PD19 n z then (Ring19.ring19 LibInt.intR).one else (Ring19.ring19 LibInt.intR).zero

theorem I19_pos (n : Nat) (z : Int × Int) (h : PD19 n z) : I19 n z = (Ring19.ring19 LibInt.intR).one := ite_pos' _ _ h
theorem I19_neg (n : Nat) (z : Int × Int) (h : ¬ PD19 n z) : I19 n z = (Ring19.ring19 LibInt.intR).zero := ite_neg' _ _ h

/-- c_w(n) = Σ_{α ≡ 1 (3), N(α) = n} w(α). -/
def cw19 (w : Int × Int → Int × Int) (n : Nat) : Int × Int := sumK (Ring19.ring19 LibInt.intR) (fun z => (Ring19.ring19 LibInt.intR).mul (I19 n z) (w z)) (box19 n)

/-- **c_w(mn) = c_w(m) c_w(n)** for coprime m, n prime to 3 and multiplicative w on ℤ[ω]. -/
theorem hecke_mult19 (w : Int × Int → Int × Int) (hw : ∀ α β, w ((Ring19.ring19 LibInt.intR).mul α β) = (Ring19.ring19 LibInt.intR).mul (w α) (w β)) (m n : Nat) (s t : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 19 * k) (hn : ∀ k, Int.ofNat (n + 1) ≠ 19 * k) :
    cw19 w ((m + 1) * (n + 1)) = (Ring19.ring19 LibInt.intR).mul (cw19 w (m + 1)) (cw19 w (n + 1)) := by
  have hM0 : Int.ofNat (m + 1) ≠ 0 := fun e => Nat.noConfusion (Int.ofNat.inj e)
  -- product of primaries of norms m, n is primary of norm mn
  have hprod : ∀ α β, PD19 (m + 1) α → PD19 (n + 1) β → PD19 ((m + 1) * (n + 1)) ((Ring19.ring19 LibInt.intR).mul α β) := fun α β ⟨h1, p1⟩ ⟨h2, p2⟩ =>
    ⟨by rw [en_mul, h1, h2]; rfl, prim_mul _ _ p1 p2⟩
  -- Step A: expand c_m c_n with a δ over γ
  have stepA : (Ring19.ring19 LibInt.intR).mul (cw19 w (m + 1)) (cw19 w (n + 1))
      = sumK (Ring19.ring19 LibInt.intR) (fun α => sumK (Ring19.ring19 LibInt.intR) (fun β => sumK (Ring19.ring19 LibInt.intR) (fun γ => (Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul (I19 (m + 1) α) (I19 (n + 1) β)) (delta (Ring19.ring19 LibInt.intR) γ ((Ring19.ring19 LibInt.intR).mul α β))) (w γ)) (box19 ((m + 1) * (n + 1)))) (box19 (n + 1))) (b  -- (+19 chars; full line in the Zenodo source)
    unfold cw19; rw [sumK_mul]
    refine sumK_ext (Ring19.ring19 LibInt.intR) _ _ (fun α => ?_) _
    rw [mul_sumK]
    refine sumK_ext (Ring19.ring19 LibInt.intR) _ _ (fun β => ?_) _
    by_cases hα : PD19 (m + 1) α
    · by_cases hβ : PD19 (n + 1) β
      · rw [I19_pos _ _ hα, I19_pos _ _ hβ]
        have hb := abs_le19 _ _ (hprod α β hα hβ).1
        have e := HeckeMult.pick_box (Ring19.ring19 LibInt.intR) (fun γ => w γ) ((Ring19.ring19 LibInt.intR).mul α β) _ hb.1 hb.2
        rw [show sumK (Ring19.ring19 LibInt.intR) (fun γ => (Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul (Ring19.ring19 LibInt.intR).one (Ring19.ring19 LibInt.intR).one) (delta (Ring19.ring19 LibInt.intR) γ ((Ring19.ring19 LibInt.intR).mul α β))) (w γ)) (box19 ((m + 1) * (n + 1)))
            = sumK (Ring19.ring19 LibInt.intR) (fun γ => (Ring19.ring19 LibInt.intR).mul (delta (Ring19.ring19 LibInt.intR) γ ((Ring19.ring19 LibInt.intR).mul α β)) (w γ)) (box19 ((m + 1) * (n + 1))) from sumK_ext (Ring19.ring19 LibInt.intR) _ _ (fun γ => by lib_ring) _, e, hw]
        lib_ring
      · rw [I19_neg _ _ hβ]
        rw [sumK_ext (Ring19.ring19 LibInt.intR) _ _ (fun γ => show (Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul (I19 (m + 1) α) (Ring19.ring19 LibInt.intR).zero) (delta (Ring19.ring19 LibInt.intR) γ ((Ring19.ring19 LibInt.intR).mul α β))) (w γ) = (Ring19.ring19 LibInt.intR).zero by lib_ring)]
        rw [zsum19]; lib_ring
    · rw [I19_neg _ _ hα]
      rw [sumK_ext (Ring19.ring19 LibInt.intR) _ _ (fun γ => show (Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul (Ring19.ring19 LibInt.intR).zero (I19 (n + 1) β)) (delta (Ring19.ring19 LibInt.intR) γ ((Ring19.ring19 LibInt.intR).mul α β))) (w γ) = (Ring19.ring19 LibInt.intR).zero by lib_ring)]
      rw [zsum19]; lib_ring

  rw [stepA]
  -- swap to Σ_γ Σ_α Σ_β
  rw [sumK_ext (Ring19.ring19 LibInt.intR) _ _ (fun α => sumK_swap (Ring19.ring19 LibInt.intR) _ _ _)]
  rw [sumK_swap (Ring19.ring19 LibInt.intR)]
  unfold cw19
  refine sumK_ext (Ring19.ring19 LibInt.intR) _ _ (fun γ => ?_) _
  have e1 : sumK (Ring19.ring19 LibInt.intR) (fun α => sumK (Ring19.ring19 LibInt.intR) (fun β => (Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul (I19 (m + 1) α) (I19 (n + 1) β)) (delta (Ring19.ring19 LibInt.intR) γ ((Ring19.ring19 LibInt.intR).mul α β))) (w γ)) (box19 (n + 1))) (box19 (m + 1))
      = (Ring19.ring19 LibInt.intR).mul (sumK (Ring19.ring19 LibInt.intR) (fun α => sumK (Ring19.ring19 LibInt.intR) (fun β => (Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul (I19 (m + 1) α) (I19 (n + 1) β)) (delta (Ring19.ring19 LibInt.intR) γ ((Ring19.ring19 LibInt.intR).mul α β))) (box19 (n + 1))) (box19 (m + 1))) (w γ) := by
    rw [sumK_mul]; exact sumK_ext (Ring19.ring19 LibInt.intR) _ _ (fun α => by rw [sumK_mul]) _
  rw [e1]
  by_cases hγ : PD19 ((m + 1) * (n + 1)) γ
  · rw [I19_pos _ _ hγ]
    match prim19_split γ m n s t hγ.1 hst hm hn hγ.2 with
    | ⟨α0, β0, h0, pα0, pβ0, nα0, nβ0⟩ =>
      have pt : ∀ α β, (Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul (I19 (m + 1) α) (I19 (n + 1) β)) (delta (Ring19.ring19 LibInt.intR) γ ((Ring19.ring19 LibInt.intR).mul α β)) = (Ring19.ring19 LibInt.intR).mul (delta (Ring19.ring19 LibInt.intR) α α0) (delta (Ring19.ring19 LibInt.intR) β β0) := by
        intro α β
        by_cases hαβ : PD19 (m + 1) α ∧ PD19 (n + 1) β ∧ γ = (Ring19.ring19 LibInt.intR).mul α β
        · match hαβ with
          | ⟨hα, hβ, hg⟩ =>
            have u := uniq_split19 α β α0 β0 _ _ s t hα.1 nα0 nβ0 hst hM0 hα.2 pα0 (hg.symm.trans h0)
            rw [I19_pos _ _ hα, I19_pos _ _ hβ, ← u.1, ← u.2]
            unfold delta; rw [ite_pos' _ _ hg, ite_pos' _ _ rfl, ite_pos' _ _ rfl]; lib_ring
        · by_cases he : α = α0 ∧ β = β0
          · exfalso; apply hαβ
            rw [he.1, he.2]; exact ⟨⟨nα0, pα0⟩, ⟨nβ0, pβ0⟩, h0⟩
          · have rhs : (Ring19.ring19 LibInt.intR).mul (delta (Ring19.ring19 LibInt.intR) α α0) (delta (Ring19.ring19 LibInt.intR) β β0) = (Ring19.ring19 LibInt.intR).zero := by
              unfold delta
              by_cases h1 : α = α0
              · rw [ite_neg' _ _ (fun h2 => he ⟨h1, h2⟩), ite_pos' _ _ h1]; lib_ring
              · rw [ite_neg' _ _ h1]; lib_ring
            rw [rhs]
            by_cases hα : PD19 (m + 1) α
            · by_cases hβ : PD19 (n + 1) β
              · have hg : ¬ γ = (Ring19.ring19 LibInt.intR).mul α β := fun hg => hαβ ⟨hα, hβ, hg⟩
                unfold delta; rw [ite_neg' _ _ hg]; lib_ring
              · rw [I19_neg _ _ hβ]; lib_ring
            · rw [I19_neg _ _ hα]; lib_ring
      rw [sumK_ext (Ring19.ring19 LibInt.intR) _ _ (fun α => sumK_ext (Ring19.ring19 LibInt.intR) _ _ (fun β => pt α β) _)]
      have ba := abs_le19 α0 _ nα0
      have bb := abs_le19 β0 _ nβ0
      rw [sumK_ext (Ring19.ring19 LibInt.intR) _ _ (fun α => by rw [← mul_sumK, HeckeMult.count_box (Ring19.ring19 LibInt.intR) β0 _ bb.1 bb.2])]
      rw [sumK_ext (Ring19.ring19 LibInt.intR) _ _ (fun α => show (Ring19.ring19 LibInt.intR).mul (delta (Ring19.ring19 LibInt.intR) α α0) (Ring19.ring19 LibInt.intR).one = (Ring19.ring19 LibInt.intR).mul (delta (Ring19.ring19 LibInt.intR) α α0) (Ring19.ring19 LibInt.intR).one from rfl), ← sumK_mul,
        HeckeMult.count_box (Ring19.ring19 LibInt.intR) α0 _ ba.1 ba.2]
      lib_ring
  · rw [I19_neg _ _ hγ]
    have pt : ∀ α β, (Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul (I19 (m + 1) α) (I19 (n + 1) β)) (delta (Ring19.ring19 LibInt.intR) γ ((Ring19.ring19 LibInt.intR).mul α β)) = (Ring19.ring19 LibInt.intR).zero := by
      intro α β
      by_cases hα : PD19 (m + 1) α
      · by_cases hβ : PD19 (n + 1) β
        · have hg : ¬ γ = (Ring19.ring19 LibInt.intR).mul α β := fun hg => hγ (by rw [hg]; exact hprod α β hα hβ)
          unfold delta; rw [ite_neg' _ _ hg]; lib_ring
        · rw [I19_neg _ _ hβ]; lib_ring
      · rw [I19_neg _ _ hα]; lib_ring
    rw [sumK_ext (Ring19.ring19 LibInt.intR) _ _ (fun α => sumK_ext (Ring19.ring19 LibInt.intR) _ _ (fun β => pt α β) _)]
    rw [sumK_ext (Ring19.ring19 LibInt.intR) _ _ (fun α => zsum19 _), zsum19]

/-- **The Hecke characters α ↦ α^k of ℚ(√−3)**: c_k(mn) = c_k(m) c_k(n). -/
theorem hecke_mult19_pow (k : Nat) (m n : Nat) (s t : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 19 * k) (hn : ∀ k, Int.ofNat (n + 1) ≠ 19 * k) :
    cw19 (fun z => Frobenius.pw (Ring19.ring19 LibInt.intR) z k) ((m + 1) * (n + 1)) =
      (Ring19.ring19 LibInt.intR).mul (cw19 (fun z => Frobenius.pw (Ring19.ring19 LibInt.intR) z k) (m + 1)) (cw19 (fun z => Frobenius.pw (Ring19.ring19 LibInt.intR) z k) (n + 1)) :=
  hecke_mult19 _ (fun α β => Frobenius.pw_mul _ α β k) m n s t hst hm hn

end HeckeMult19

#print axioms HeckeMult19.abs_le19
#print axioms HeckeMult19.prim19_iff
#print axioms HeckeMult19.zsum19
#print axioms HeckeMult19.I19_pos
#print axioms HeckeMult19.I19_neg
#print axioms HeckeMult19.hecke_mult19
#print axioms HeckeMult19.hecke_mult19_pow
#print axioms HeckeMult19.cw19

lang/HeckeMult2.lean

lean_all/lang/HeckeMult2.lean

/-
  lang/HeckeMult2 — T48/T50, cycle 629: **the theta series of every multiplicative weight on primary elements of ℤ[√−2] is
  multiplicative**: for w : ℤ[√−2] → ℤ[√−2] with w(αβ) = w(α)w(β) (e.g. w(α) = α^k, the Hecke characters of ℚ(√−2) of ∞-type k),
    c_w(n) = Σ_{α primary, N(α) = n} w(α)   satisfies   c_w(mn) = c_w(m) c_w(n)  for coprime odd m, n.
  Enumeration box2 n = HeckeMult.box n (|a|, |b| ≤ N). (Port of HeckeMult3 with Sq2Unique.)
    abs_le2, pd2_iff, cw2, hecke_mult2, hecke_mult2_pow
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Sq2Unique
import lang.HeckeMultW

namespace HeckeMult2

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Sq2Euclid Sq2Unique

abbrev box2 (n : Nat) : List (Int × Int) := HeckeMult.box n

/-- N(z) = n ⇒ |z.1|, |z.2| ≤ n. -/
theorem abs_le2 (z : Int × Int) (n : Nat) (h : en z = Int.ofNat n) : Int.natAbs z.1 ≤ n ∧ Int.natAbs z.2 ≤ n := by
  have hn : nrm z = n := Int.ofNat.inj (by rw [nrm_cast]; exact h)
  have hn' : Int.natAbs z.1 * Int.natAbs z.1 + 2 * (Int.natAbs z.2 * Int.natAbs z.2) = n := hn
  constructor
  · exact Nat.le_trans (HeckeMult.le_sq _) (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq hn'))
  · exact Nat.le_trans (HeckeMult.le_sq _) (Nat.le_trans (Nat.le_trans (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ (Nat.le_succ 1))) (Nat.le_add_left _ _)) (Nat.le_of_eq hn'))

def PD2 (n : Nat) (z : Int × Int) : Prop := en z = Int.ofNat n ∧ Prim2 z

theorem prim2_iff (z : Int × Int) :
    ((ZMod.md 1 (Int.natAbs z.2) = 0 ∧ ZMod.md 3 (Int.natAbs (z.1 + z.2 - 1)) = 0) ∨ (ZMod.md 3 (Int.natAbs (z.2 - 1)) = 0 ∧ ZMod.md 1 (Int.natAbs (z.1 - 1)) = 0)) ↔ Prim2 z := by
  constructor
  · intro h
    rcases h with ⟨h1, h2⟩ | ⟨h1, h2⟩
    · obtain ⟨s, hs⟩ := (HeckeMult.idv_iff 1 _).mpr h1
      obtain ⟨e, he⟩ := (HeckeMult.idv_iff 3 _).mpr h2
      exact Or.inl ⟨s, e, hs, by calc z.1 = (z.1 + z.2 - 1) + 1 - z.2 := by lib_int
                                  _ = _ := by rw [he, hs]; rfl⟩
    · obtain ⟨f, hf⟩ := (HeckeMult.idv_iff 3 _).mpr h1
      obtain ⟨r, hr⟩ := (HeckeMult.idv_iff 1 _).mpr h2
      exact Or.inr ⟨f, r, by calc z.2 = (z.2 - 1) + 1 := by lib_int
                               _ = _ := by rw [hf]; rfl, by calc z.1 = (z.1 - 1) + 1 := by lib_int
                                                              _ = _ := by rw [hr]; rfl⟩
  · intro h
    rcases h with ⟨s, e, hs, he⟩ | ⟨f, r, hf, hr⟩
    · exact Or.inl ⟨(HeckeMult.idv_iff 1 _).mp ⟨s, hs⟩, (HeckeMult.idv_iff 3 _).mp ⟨e, by rw [he, hs]; show 4 * e + 1 - 2 * s + 2 * s - 1 = 4 * e; lib_int⟩⟩
    · exact Or.inr ⟨(HeckeMult.idv_iff 3 _).mp ⟨f, by rw [hf]; show 4 * f + 1 - 1 = 4 * f; lib_int⟩, (HeckeMult.idv_iff 1 _).mp ⟨r, by rw [hr]; show 2 * r + 1 - 1 = 2 * r; lib_int⟩⟩

instance (z : Int × Int) : Decidable (Prim2 z) := decidable_of_iff _ (prim2_iff z)

instance (n : Nat) (z : Int × Int) : Decidable (PD2 n z) := inferInstanceAs (Decidable (_ ∧ _))

theorem zsum2 {A : Type} : ∀ l : List A, sumK (Quad.quad LibInt.intR (-2)) (fun _ => (Quad.quad LibInt.intR (-2)).zero) l = (Quad.quad LibInt.intR (-2)).zero
  | [] => rfl
  | _ :: l => by show (Quad.quad LibInt.intR (-2)).add (Quad.quad LibInt.intR (-2)).zero (sumK (Quad.quad LibInt.intR (-2)) (fun _ => (Quad.quad LibInt.intR (-2)).zero) l) = _; rw [zsum2 l]; exact (Quad.quad LibInt.intR (-2)).zero_add _

def I2 (n : Nat) (z : Int × Int) : Int × Int := if PD2 n z then (Quad.quad LibInt.intR (-2)).one else (Quad.quad LibInt.intR (-2)).zero

theorem I2_pos (n : Nat) (z : Int × Int) (h : PD2 n z) : I2 n z = (Quad.quad LibInt.intR (-2)).one := ite_pos' _ _ h
theorem I2_neg (n : Nat) (z : Int × Int) (h : ¬ PD2 n z) : I2 n z = (Quad.quad LibInt.intR (-2)).zero := ite_neg' _ _ h

/-- c_w(n) = Σ_{α ≡ 1 (3), N(α) = n} w(α). -/
def cw2 (w : Int × Int → Int × Int) (n : Nat) : Int × Int := sumK (Quad.quad LibInt.intR (-2)) (fun z => (Quad.quad LibInt.intR (-2)).mul (I2 n z) (w z)) (box2 n)

/-- **c_w(mn) = c_w(m) c_w(n)** for coprime m, n prime to 3 and multiplicative w on ℤ[ω]. -/
theorem hecke_mult2 (w : Int × Int → Int × Int) (hw : ∀ α β, w ((Quad.quad LibInt.intR (-2)).mul α β) = (Quad.quad LibInt.intR (-2)).mul (w α) (w β)) (m n : Nat) (s t : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : ∃ k, Int.ofNat (m + 1) = 2 * k + 1) (hn : ∃ k, Int.ofNat (n + 1) = 2 * k + 1) :
    cw2 w ((m + 1) * (n + 1)) = (Quad.quad LibInt.intR (-2)).mul (cw2 w (m + 1)) (cw2 w (n + 1)) := by
  have hM0 : Int.ofNat (m + 1) ≠ 0 := fun e => Nat.noConfusion (Int.ofNat.inj e)
  -- product of primaries of norms m, n is primary of norm mn
  have hprod : ∀ α β, PD2 (m + 1) α → PD2 (n + 1) β → PD2 ((m + 1) * (n + 1)) ((Quad.quad LibInt.intR (-2)).mul α β) := fun α β ⟨h1, p1⟩ ⟨h2, p2⟩ =>
    ⟨by rw [en_mul, h1, h2]; rfl, prim2_mul _ _ p1 p2⟩
  -- Step A: expand c_m c_n with a δ over γ
  have stepA : (Quad.quad LibInt.intR (-2)).mul (cw2 w (m + 1)) (cw2 w (n + 1))
      = sumK (Quad.quad LibInt.intR (-2)) (fun α => sumK (Quad.quad LibInt.intR (-2)) (fun β => sumK (Quad.quad LibInt.intR (-2)) (fun γ => (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (I2 (m + 1) α) (I2 (n + 1) β)) (delta (Quad.quad LibInt.intR (-2)) γ ((Quad.quad LibInt.intR (-2)).mul α β))) (w γ)) (box2 ((m + 1) * (n + 1)))) (box2 (n + 1))  -- (+22 chars; full line in the Zenodo source)
    unfold cw2; rw [sumK_mul]
    refine sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun α => ?_) _
    rw [mul_sumK]
    refine sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun β => ?_) _
    by_cases hα : PD2 (m + 1) α
    · by_cases hβ : PD2 (n + 1) β
      · rw [I2_pos _ _ hα, I2_pos _ _ hβ]
        have hb := abs_le2 _ _ (hprod α β hα hβ).1
        have e := HeckeMult.pick_box (Quad.quad LibInt.intR (-2)) (fun γ => w γ) ((Quad.quad LibInt.intR (-2)).mul α β) _ hb.1 hb.2
        rw [show sumK (Quad.quad LibInt.intR (-2)) (fun γ => (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (Quad.quad LibInt.intR (-2)).one (Quad.quad LibInt.intR (-2)).one) (delta (Quad.quad LibInt.intR (-2)) γ ((Quad.quad LibInt.intR (-2)).mul α β))) (w γ)) (box2 ((m + 1) * (n + 1)))
            = sumK (Quad.quad LibInt.intR (-2)) (fun γ => (Quad.quad LibInt.intR (-2)).mul (delta (Quad.quad LibInt.intR (-2)) γ ((Quad.quad LibInt.intR (-2)).mul α β)) (w γ)) (box2 ((m + 1) * (n + 1))) from sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun γ => by lib_ring) _, e, hw]
        lib_ring
      · rw [I2_neg _ _ hβ]
        rw [sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun γ => show (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (I2 (m + 1) α) (Quad.quad LibInt.intR (-2)).zero) (delta (Quad.quad LibInt.intR (-2)) γ ((Quad.quad LibInt.intR (-2)).mul α β))) (w γ) = (Quad.quad LibInt.intR (-2)).zero by lib_ring)]
        rw [zsum2]; lib_ring
    · rw [I2_neg _ _ hα]
      rw [sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun γ => show (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (Quad.quad LibInt.intR (-2)).zero (I2 (n + 1) β)) (delta (Quad.quad LibInt.intR (-2)) γ ((Quad.quad LibInt.intR (-2)).mul α β))) (w γ) = (Quad.quad LibInt.intR (-2)).zero by lib_ring)]
      rw [zsum2]; lib_ring

  rw [stepA]
  -- swap to Σ_γ Σ_α Σ_β
  rw [sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun α => sumK_swap (Quad.quad LibInt.intR (-2)) _ _ _)]
  rw [sumK_swap (Quad.quad LibInt.intR (-2))]
  unfold cw2
  refine sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun γ => ?_) _
  have e1 : sumK (Quad.quad LibInt.intR (-2)) (fun α => sumK (Quad.quad LibInt.intR (-2)) (fun β => (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (I2 (m + 1) α) (I2 (n + 1) β)) (delta (Quad.quad LibInt.intR (-2)) γ ((Quad.quad LibInt.intR (-2)).mul α β))) (w γ)) (box2 (n + 1))) (box2 (m + 1))
      = (Quad.quad LibInt.intR (-2)).mul (sumK (Quad.quad LibInt.intR (-2)) (fun α => sumK (Quad.quad LibInt.intR (-2)) (fun β => (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (I2 (m + 1) α) (I2 (n + 1) β)) (delta (Quad.quad LibInt.intR (-2)) γ ((Quad.quad LibInt.intR (-2)).mul α β))) (box2 (n + 1))) (box2 (m + 1))) (w γ) := by
    rw [sumK_mul]; exact sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun α => by rw [sumK_mul]) _
  rw [e1]
  by_cases hγ : PD2 ((m + 1) * (n + 1)) γ
  · rw [I2_pos _ _ hγ]
    match prim2_split γ m n s t hγ.1 hst hm hn hγ.2 with
    | ⟨α0, β0, h0, pα0, pβ0, nα0, nβ0⟩ =>
      have pt : ∀ α β, (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (I2 (m + 1) α) (I2 (n + 1) β)) (delta (Quad.quad LibInt.intR (-2)) γ ((Quad.quad LibInt.intR (-2)).mul α β)) = (Quad.quad LibInt.intR (-2)).mul (delta (Quad.quad LibInt.intR (-2)) α α0) (delta (Quad.quad LibInt.intR (-2)) β β0) := by
        intro α β
        by_cases hαβ : PD2 (m + 1) α ∧ PD2 (n + 1) β ∧ γ = (Quad.quad LibInt.intR (-2)).mul α β
        · match hαβ with
          | ⟨hα, hβ, hg⟩ =>
            have u := uniq_split2 α β α0 β0 _ _ s t hα.1 nα0 nβ0 hst hM0 hα.2 pα0 (hg.symm.trans h0)
            rw [I2_pos _ _ hα, I2_pos _ _ hβ, ← u.1, ← u.2]
            unfold delta; rw [ite_pos' _ _ hg, ite_pos' _ _ rfl, ite_pos' _ _ rfl]; lib_ring
        · by_cases he : α = α0 ∧ β = β0
          · exfalso; apply hαβ
            rw [he.1, he.2]; exact ⟨⟨nα0, pα0⟩, ⟨nβ0, pβ0⟩, h0⟩
          · have rhs : (Quad.quad LibInt.intR (-2)).mul (delta (Quad.quad LibInt.intR (-2)) α α0) (delta (Quad.quad LibInt.intR (-2)) β β0) = (Quad.quad LibInt.intR (-2)).zero := by
              unfold delta
              by_cases h1 : α = α0
              · rw [ite_neg' _ _ (fun h2 => he ⟨h1, h2⟩), ite_pos' _ _ h1]; lib_ring
              · rw [ite_neg' _ _ h1]; lib_ring
            rw [rhs]
            by_cases hα : PD2 (m + 1) α
            · by_cases hβ : PD2 (n + 1) β
              · have hg : ¬ γ = (Quad.quad LibInt.intR (-2)).mul α β := fun hg => hαβ ⟨hα, hβ, hg⟩
                unfold delta; rw [ite_neg' _ _ hg]; lib_ring
              · rw [I2_neg _ _ hβ]; lib_ring
            · rw [I2_neg _ _ hα]; lib_ring
      rw [sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun α => sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun β => pt α β) _)]
      have ba := abs_le2 α0 _ nα0
      have bb := abs_le2 β0 _ nβ0
      rw [sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun α => by rw [← mul_sumK, HeckeMult.count_box (Quad.quad LibInt.intR (-2)) β0 _ bb.1 bb.2])]
      rw [sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun α => show (Quad.quad LibInt.intR (-2)).mul (delta (Quad.quad LibInt.intR (-2)) α α0) (Quad.quad LibInt.intR (-2)).one = (Quad.quad LibInt.intR (-2)).mul (delta (Quad.quad LibInt.intR (-2)) α α0) (Quad.quad LibInt.intR (-2)).one from rfl), ← sumK_mul,
        HeckeMult.count_box (Quad.quad LibInt.intR (-2)) α0 _ ba.1 ba.2]
      lib_ring
  · rw [I2_neg _ _ hγ]
    have pt : ∀ α β, (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (I2 (m + 1) α) (I2 (n + 1) β)) (delta (Quad.quad LibInt.intR (-2)) γ ((Quad.quad LibInt.intR (-2)).mul α β)) = (Quad.quad LibInt.intR (-2)).zero := by
      intro α β
      by_cases hα : PD2 (m + 1) α
      · by_cases hβ : PD2 (n + 1) β
        · have hg : ¬ γ = (Quad.quad LibInt.intR (-2)).mul α β := fun hg => hγ (by rw [hg]; exact hprod α β hα hβ)
          unfold delta; rw [ite_neg' _ _ hg]; lib_ring
        · rw [I2_neg _ _ hβ]; lib_ring
      · rw [I2_neg _ _ hα]; lib_ring
    rw [sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun α => sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun β => pt α β) _)]
    rw [sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun α => zsum2 _), zsum2]

/-- **The Hecke characters α ↦ α^k of ℚ(√−3)**: c_k(mn) = c_k(m) c_k(n). -/
theorem hecke_mult2_pow (k : Nat) (m n : Nat) (s t : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : ∃ k, Int.ofNat (m + 1) = 2 * k + 1) (hn : ∃ k, Int.ofNat (n + 1) = 2 * k + 1) :
    cw2 (fun z => Frobenius.pw (Quad.quad LibInt.intR (-2)) z k) ((m + 1) * (n + 1)) =
      (Quad.quad LibInt.intR (-2)).mul (cw2 (fun z => Frobenius.pw (Quad.quad LibInt.intR (-2)) z k) (m + 1)) (cw2 (fun z => Frobenius.pw (Quad.quad LibInt.intR (-2)) z k) (n + 1)) :=
  hecke_mult2 _ (fun α β => Frobenius.pw_mul _ α β k) m n s t hst hm hn

end HeckeMult2

#print axioms HeckeMult2.abs_le2
#print axioms HeckeMult2.prim2_iff
#print axioms HeckeMult2.zsum2
#print axioms HeckeMult2.I2_pos
#print axioms HeckeMult2.I2_neg
#print axioms HeckeMult2.hecke_mult2
#print axioms HeckeMult2.hecke_mult2_pow
#print axioms HeckeMult2.cw2

lang/HeckeMult3.lean

lean_all/lang/HeckeMult3.lean

/-
  lang/HeckeMult3 — T50, cycle 615: **the theta series of every multiplicative weight on primary Eisenstein integers is multiplicative**:
  for w : ℤ[ω] → ℤ[ω] with w(αβ) = w(α)w(β) (e.g. w(α) = α^k, the Hecke characters of ℚ(√−3) of ∞-type k, conductor 3),
    c_w(n) = Σ_{α ≡ 1 (mod 3), N(α) = n} w(α)   satisfies   c_w(mn) = c_w(m) c_w(n)  for coprime m, n prime to 3.
  The finite enumeration is box3 n = HeckeMult.box (4n) (|a|, |b| ≤ 4N since 4N = (2a − b)² + 3b² = (2b − a)² + 3a²).
    abs_le3, idv3_iff, pd3_iff, cw3, hecke_mult3, hecke_mult3_pow
  (Port of HeckeMultW with EisUnique3.prim3_split/uniq3_split.) For k = 1 the c_w(p) are the a_p of 27a (y² + y = x³).
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.EisUnique3
import lang.HeckeMultW

namespace HeckeMult3

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain EisEuclid EisPrim EisUnique3

abbrev box3 (n : Nat) : List (Int × Int) := HeckeMult.box (4 * n)

theorem abs_one3 (a b : Int) (n : Nat) (h : a * a + -(a * b) + b * b = Int.ofNat n) : Int.natAbs b ≤ 4 * n := by
  have h4 : Int.ofNat (Int.natAbs (2 * a + -b) * Int.natAbs (2 * a + -b) + 3 * (Int.natAbs b * Int.natAbs b)) = Int.ofNat (4 * n) := by
    show Int.ofNat (Int.natAbs (2 * a + -b) * Int.natAbs (2 * a + -b)) + 3 * Int.ofNat (Int.natAbs b * Int.natAbs b) = 4 * Int.ofNat n
    rw [GaussEuclid.nas, GaussEuclid.nas, ← h]; lib_int
  have h4' := Int.ofNat.inj h4
  calc Int.natAbs b ≤ Int.natAbs b * Int.natAbs b := HeckeMult.le_sq _
    _ ≤ 3 * (Int.natAbs b * Int.natAbs b) := Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ (Nat.le_succ_of_le (Nat.le_succ 1)))
    _ ≤ _ := Nat.le_trans (Nat.le_add_left _ _) (Nat.le_of_eq h4')

/-- N(z) = n ⇒ |z.1|, |z.2| ≤ 4n. -/
theorem abs_le3 (z : Int × Int) (n : Nat) (h : en z = Int.ofNat n) : Int.natAbs z.1 ≤ 4 * n ∧ Int.natAbs z.2 ≤ 4 * n :=
  ⟨abs_one3 z.2 z.1 n (by rw [← h]; unfold en; lib_int), abs_one3 z.1 z.2 n h⟩

def PD3 (n : Nat) (z : Int × Int) : Prop := en z = Int.ofNat n ∧ Prim3 z

theorem pd3_iff (n : Nat) (z : Int × Int) :
    (en z = Int.ofNat n ∧ (ZMod.md 2 (Int.natAbs (z.1 + -1)) = 0 ∧ ZMod.md 2 (Int.natAbs z.2) = 0)) ↔ PD3 n z := by
  constructor
  · intro ⟨h1, h2, h3⟩
    obtain ⟨e, he⟩ := (HeckeMult.idv_iff 2 _).mpr h2
    obtain ⟨f, hf⟩ := (HeckeMult.idv_iff 2 _).mpr h3
    exact ⟨h1, e, f, by calc z.1 = (z.1 + -1) + 1 := by lib_int
                              _ = 3 * e + 1 := by rw [he]; rfl, hf⟩
  · intro ⟨h1, e, f, he, hf⟩
    exact ⟨h1, (HeckeMult.idv_iff 2 _).mp ⟨e, by rw [he]; show 3 * e + 1 + -1 = 3 * e; lib_int⟩, (HeckeMult.idv_iff 2 _).mp ⟨f, hf⟩⟩

instance (n : Nat) (z : Int × Int) : Decidable (PD3 n z) := decidable_of_iff _ (pd3_iff n z)

theorem zsum3 {A : Type} : ∀ l : List A, sumK (Cyclo3.cyc3 LibInt.intR) (fun _ => (Cyclo3.cyc3 LibInt.intR).zero) l = (Cyclo3.cyc3 LibInt.intR).zero
  | [] => rfl
  | _ :: l => by show (Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).zero (sumK (Cyclo3.cyc3 LibInt.intR) (fun _ => (Cyclo3.cyc3 LibInt.intR).zero) l) = _; rw [zsum3 l]; exact (Cyclo3.cyc3 LibInt.intR).zero_add _

def I3 (n : Nat) (z : Int × Int) : Int × Int := if PD3 n z then (Cyclo3.cyc3 LibInt.intR).one else (Cyclo3.cyc3 LibInt.intR).zero

theorem I3_pos (n : Nat) (z : Int × Int) (h : PD3 n z) : I3 n z = (Cyclo3.cyc3 LibInt.intR).one := ite_pos' _ _ h
theorem I3_neg (n : Nat) (z : Int × Int) (h : ¬ PD3 n z) : I3 n z = (Cyclo3.cyc3 LibInt.intR).zero := ite_neg' _ _ h

/-- c_w(n) = Σ_{α ≡ 1 (3), N(α) = n} w(α). -/
def cw3 (w : Int × Int → Int × Int) (n : Nat) : Int × Int := sumK (Cyclo3.cyc3 LibInt.intR) (fun z => (Cyclo3.cyc3 LibInt.intR).mul (I3 n z) (w z)) (box3 n)

/-- **c_w(mn) = c_w(m) c_w(n)** for coprime m, n prime to 3 and multiplicative w on ℤ[ω]. -/
theorem hecke_mult3 (w : Int × Int → Int × Int) (hw : ∀ α β, w ((Cyclo3.cyc3 LibInt.intR).mul α β) = (Cyclo3.cyc3 LibInt.intR).mul (w α) (w β)) (m n : Nat) (s t : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : C3 (Int.ofNat (m + 1))) (hn : C3 (Int.ofNat (n + 1))) :
    cw3 w ((m + 1) * (n + 1)) = (Cyclo3.cyc3 LibInt.intR).mul (cw3 w (m + 1)) (cw3 w (n + 1)) := by
  have hM0 : Int.ofNat (m + 1) ≠ 0 := fun e => Nat.noConfusion (Int.ofNat.inj e)
  -- product of primaries of norms m, n is primary of norm mn
  have hprod : ∀ α β, PD3 (m + 1) α → PD3 (n + 1) β → PD3 ((m + 1) * (n + 1)) ((Cyclo3.cyc3 LibInt.intR).mul α β) := fun α β ⟨h1, p1⟩ ⟨h2, p2⟩ =>
    ⟨by rw [en_mul, h1, h2]; rfl, prim3_mul _ _ p1 p2⟩
  -- Step A: expand c_m c_n with a δ over γ
  have stepA : (Cyclo3.cyc3 LibInt.intR).mul (cw3 w (m + 1)) (cw3 w (n + 1))
      = sumK (Cyclo3.cyc3 LibInt.intR) (fun α => sumK (Cyclo3.cyc3 LibInt.intR) (fun β => sumK (Cyclo3.cyc3 LibInt.intR) (fun γ => (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (I3 (m + 1) α) (I3 (n + 1) β)) (delta (Cyclo3.cyc3 LibInt.intR) γ ((Cyclo3.cyc3 LibInt.intR).mul α β))) (w γ)) (box3 ((m + 1) * (n + 1)))) (box3 (n + 1))) (box3 (m + 1)) := by
    unfold cw3; rw [sumK_mul]
    refine sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun α => ?_) _
    rw [mul_sumK]
    refine sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun β => ?_) _
    by_cases hα : PD3 (m + 1) α
    · by_cases hβ : PD3 (n + 1) β
      · rw [I3_pos _ _ hα, I3_pos _ _ hβ]
        have hb := abs_le3 _ _ (hprod α β hα hβ).1
        have e := HeckeMult.pick_box (Cyclo3.cyc3 LibInt.intR) (fun γ => w γ) ((Cyclo3.cyc3 LibInt.intR).mul α β) _ hb.1 hb.2
        rw [show sumK (Cyclo3.cyc3 LibInt.intR) (fun γ => (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).one (Cyclo3.cyc3 LibInt.intR).one) (delta (Cyclo3.cyc3 LibInt.intR) γ ((Cyclo3.cyc3 LibInt.intR).mul α β))) (w γ)) (box3 ((m + 1) * (n + 1)))
            = sumK (Cyclo3.cyc3 LibInt.intR) (fun γ => (Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) γ ((Cyclo3.cyc3 LibInt.intR).mul α β)) (w γ)) (box3 ((m + 1) * (n + 1))) from sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun γ => by lib_ring) _, e, hw]
        lib_ring
      · rw [I3_neg _ _ hβ]
        rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun γ => show (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (I3 (m + 1) α) (Cyclo3.cyc3 LibInt.intR).zero) (delta (Cyclo3.cyc3 LibInt.intR) γ ((Cyclo3.cyc3 LibInt.intR).mul α β))) (w γ) = (Cyclo3.cyc3 LibInt.intR).zero by lib_ring)]
        rw [zsum3]; lib_ring
    · rw [I3_neg _ _ hα]
      rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun γ => show (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).zero (I3 (n + 1) β)) (delta (Cyclo3.cyc3 LibInt.intR) γ ((Cyclo3.cyc3 LibInt.intR).mul α β))) (w γ) = (Cyclo3.cyc3 LibInt.intR).zero by lib_ring)]
      rw [zsum3]; lib_ring

  rw [stepA]
  -- swap to Σ_γ Σ_α Σ_β
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun α => sumK_swap (Cyclo3.cyc3 LibInt.intR) _ _ _)]
  rw [sumK_swap (Cyclo3.cyc3 LibInt.intR)]
  unfold cw3
  refine sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun γ => ?_) _
  have e1 : sumK (Cyclo3.cyc3 LibInt.intR) (fun α => sumK (Cyclo3.cyc3 LibInt.intR) (fun β => (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (I3 (m + 1) α) (I3 (n + 1) β)) (delta (Cyclo3.cyc3 LibInt.intR) γ ((Cyclo3.cyc3 LibInt.intR).mul α β))) (w γ)) (box3 (n + 1))) (box3 (m + 1))
      = (Cyclo3.cyc3 LibInt.intR).mul (sumK (Cyclo3.cyc3 LibInt.intR) (fun α => sumK (Cyclo3.cyc3 LibInt.intR) (fun β => (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (I3 (m + 1) α) (I3 (n + 1) β)) (delta (Cyclo3.cyc3 LibInt.intR) γ ((Cyclo3.cyc3 LibInt.intR).mul α β))) (box3 (n + 1))) (box3 (m + 1))) (w γ) := by
    rw [sumK_mul]; exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun α => by rw [sumK_mul]) _
  rw [e1]
  by_cases hγ : PD3 ((m + 1) * (n + 1)) γ
  · rw [I3_pos _ _ hγ]
    match prim3_split γ m n s t hγ.1 hst hm hn hγ.2 with
    | ⟨α0, β0, h0, pα0, pβ0, nα0, nβ0⟩ =>
      have pt : ∀ α β, (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (I3 (m + 1) α) (I3 (n + 1) β)) (delta (Cyclo3.cyc3 LibInt.intR) γ ((Cyclo3.cyc3 LibInt.intR).mul α β)) = (Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) α α0) (delta (Cyclo3.cyc3 LibInt.intR) β β0) := by
        intro α β
        by_cases hαβ : PD3 (m + 1) α ∧ PD3 (n + 1) β ∧ γ = (Cyclo3.cyc3 LibInt.intR).mul α β
        · match hαβ with
          | ⟨hα, hβ, hg⟩ =>
            have u := uniq_split3 α β α0 β0 _ _ s t hα.1 nα0 nβ0 hst hM0 hα.2 pα0 (hg.symm.trans h0)
            rw [I3_pos _ _ hα, I3_pos _ _ hβ, ← u.1, ← u.2]
            unfold delta; rw [ite_pos' _ _ hg, ite_pos' _ _ rfl, ite_pos' _ _ rfl]; lib_ring
        · by_cases he : α = α0 ∧ β = β0
          · exfalso; apply hαβ
            rw [he.1, he.2]; exact ⟨⟨nα0, pα0⟩, ⟨nβ0, pβ0⟩, h0⟩
          · have rhs : (Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) α α0) (delta (Cyclo3.cyc3 LibInt.intR) β β0) = (Cyclo3.cyc3 LibInt.intR).zero := by
              unfold delta
              by_cases h1 : α = α0
              · rw [ite_neg' _ _ (fun h2 => he ⟨h1, h2⟩), ite_pos' _ _ h1]; lib_ring
              · rw [ite_neg' _ _ h1]; lib_ring
            rw [rhs]
            by_cases hα : PD3 (m + 1) α
            · by_cases hβ : PD3 (n + 1) β
              · have hg : ¬ γ = (Cyclo3.cyc3 LibInt.intR).mul α β := fun hg => hαβ ⟨hα, hβ, hg⟩
                unfold delta; rw [ite_neg' _ _ hg]; lib_ring
              · rw [I3_neg _ _ hβ]; lib_ring
            · rw [I3_neg _ _ hα]; lib_ring
      rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun α => sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun β => pt α β) _)]
      have ba := abs_le3 α0 _ nα0
      have bb := abs_le3 β0 _ nβ0
      rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun α => by rw [← mul_sumK, HeckeMult.count_box (Cyclo3.cyc3 LibInt.intR) β0 _ bb.1 bb.2])]
      rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun α => show (Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) α α0) (Cyclo3.cyc3 LibInt.intR).one = (Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) α α0) (Cyclo3.cyc3 LibInt.intR).one from rfl), ← sumK_mul,
        HeckeMult.count_box (Cyclo3.cyc3 LibInt.intR) α0 _ ba.1 ba.2]
      lib_ring
  · rw [I3_neg _ _ hγ]
    have pt : ∀ α β, (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (I3 (m + 1) α) (I3 (n + 1) β)) (delta (Cyclo3.cyc3 LibInt.intR) γ ((Cyclo3.cyc3 LibInt.intR).mul α β)) = (Cyclo3.cyc3 LibInt.intR).zero := by
      intro α β
      by_cases hα : PD3 (m + 1) α
      · by_cases hβ : PD3 (n + 1) β
        · have hg : ¬ γ = (Cyclo3.cyc3 LibInt.intR).mul α β := fun hg => hγ (by rw [hg]; exact hprod α β hα hβ)
          unfold delta; rw [ite_neg' _ _ hg]; lib_ring
        · rw [I3_neg _ _ hβ]; lib_ring
      · rw [I3_neg _ _ hα]; lib_ring
    rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun α => sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun β => pt α β) _)]
    rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun α => zsum3 _), zsum3]

/-- **The Hecke characters α ↦ α^k of ℚ(√−3)**: c_k(mn) = c_k(m) c_k(n). -/
theorem hecke_mult3_pow (k : Nat) (m n : Nat) (s t : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : C3 (Int.ofNat (m + 1))) (hn : C3 (Int.ofNat (n + 1))) :
    cw3 (fun z => Frobenius.pw (Cyclo3.cyc3 LibInt.intR) z k) ((m + 1) * (n + 1)) =
      (Cyclo3.cyc3 LibInt.intR).mul (cw3 (fun z => Frobenius.pw (Cyclo3.cyc3 LibInt.intR) z k) (m + 1)) (cw3 (fun z => Frobenius.pw (Cyclo3.cyc3 LibInt.intR) z k) (n + 1)) :=
  hecke_mult3 _ (fun α β => Frobenius.pw_mul _ α β k) m n s t hst hm hn

end HeckeMult3

#print axioms HeckeMult3.abs_one3
#print axioms HeckeMult3.abs_le3
#print axioms HeckeMult3.pd3_iff
#print axioms HeckeMult3.zsum3
#print axioms HeckeMult3.I3_pos
#print axioms HeckeMult3.I3_neg
#print axioms HeckeMult3.hecke_mult3
#print axioms HeckeMult3.hecke_mult3_pow
#print axioms HeckeMult3.cw3

lang/HeckeMult43.lean

lean_all/lang/HeckeMult43.lean

/-
  lang/HeckeMult43 — T48/T50, T48: **the theta series of ℚ(√−43)'s Hecke character class is multiplicative**: for every
  multiplicative w on ℤ[θ], θ = (1 + √−43)/2,  c_w(n) = Σ_{α ∈ H43, N(α) = n} w(α)  satisfies c_w(mn) = c_w(m)c_w(n) for coprime m, n
  prime to 43.  Enumeration box43 n = HeckeMult.box (44n).
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Uniq43
import lang.HeckeMultW

namespace HeckeMult43

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Euc43 Prim43 Uniq43

abbrev box43 (n : Nat) : List (Int × Int) := HeckeMult.box (44 * n)

/-- N(z) = n ⇒ |z.1|, |z.2| ≤ 44n  (43a² + (a + 22b)² = 44N, (2a + b)² + 43b² = 4N). -/
theorem abs_le43 (z : Int × Int) (n : Nat) (h : en z = Int.ofNat n) : Int.natAbs z.1 ≤ 44 * n ∧ Int.natAbs z.2 ≤ 44 * n := by
  have hn : nrm z = n := Int.ofNat.inj (by rw [nrm_cast]; exact h)
  have h4 := four_nrm z
  rw [hn] at h4
  have h8 : 43 * (Int.natAbs z.1 * Int.natAbs z.1) + Int.natAbs (z.1 + 22 * z.2) * Int.natAbs (z.1 + 22 * z.2) = 44 * n := Int.ofNat.inj (by
    show 43 * Int.ofNat (Int.natAbs z.1 * Int.natAbs z.1) + Int.ofNat (Int.natAbs (z.1 + 22 * z.2) * Int.natAbs (z.1 + 22 * z.2)) = 44 * Int.ofNat n
    rw [GaussEuclid.nas, GaussEuclid.nas, ← h]; unfold en; lib_int)
  have seven : ∀ X : Nat, X ≤ 43 * (X * X) := fun X => Nat.le_trans (HeckeMult.le_sq X) (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ (Nat.le_add_left 1 42)))
  constructor
  · exact Nat.le_trans (seven _) (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq h8))
  · exact Nat.le_trans (seven _) (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_trans (Nat.le_of_eq h4.symm) (Nat.mul_le_mul_right n (Nat.le_add_right 4 40))))

def PD43 (n : Nat) (z : Int × Int) : Prop := en z = Int.ofNat n ∧ Prim43 z

instance (n : Nat) (z : Int × Int) : Decidable (PD43 n z) := inferInstanceAs (Decidable (_ ∧ _))

theorem zsum43 {A : Type} : ∀ l : List A, sumK (Ring43.ring43 LibInt.intR) (fun _ => (Ring43.ring43 LibInt.intR).zero) l = (Ring43.ring43 LibInt.intR).zero
  | [] => rfl
  | _ :: l => by show (Ring43.ring43 LibInt.intR).add (Ring43.ring43 LibInt.intR).zero (sumK (Ring43.ring43 LibInt.intR) (fun _ => (Ring43.ring43 LibInt.intR).zero) l) = _; rw [zsum43 l]; exact (Ring43.ring43 LibInt.intR).zero_add _

def I43 (n : Nat) (z : Int × Int) : Int × Int := if PD43 n z then (Ring43.ring43 LibInt.intR).one else (Ring43.ring43 LibInt.intR).zero

theorem I43_pos (n : Nat) (z : Int × Int) (h : PD43 n z) : I43 n z = (Ring43.ring43 LibInt.intR).one := ite_pos' _ _ h
theorem I43_neg (n : Nat) (z : Int × Int) (h : ¬ PD43 n z) : I43 n z = (Ring43.ring43 LibInt.intR).zero := ite_neg' _ _ h

/-- c_w(n) = Σ_{α ≡ 1 (3), N(α) = n} w(α). -/
def cw43 (w : Int × Int → Int × Int) (n : Nat) : Int × Int := sumK (Ring43.ring43 LibInt.intR) (fun z => (Ring43.ring43 LibInt.intR).mul (I43 n z) (w z)) (box43 n)

/-- **c_w(mn) = c_w(m) c_w(n)** for coprime m, n prime to 3 and multiplicative w on ℤ[ω]. -/
theorem hecke_mult43 (w : Int × Int → Int × Int) (hw : ∀ α β, w ((Ring43.ring43 LibInt.intR).mul α β) = (Ring43.ring43 LibInt.intR).mul (w α) (w β)) (m n : Nat) (s t : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 43 * k) (hn : ∀ k, Int.ofNat (n + 1) ≠ 43 * k) :
    cw43 w ((m + 1) * (n + 1)) = (Ring43.ring43 LibInt.intR).mul (cw43 w (m + 1)) (cw43 w (n + 1)) := by
  have hM0 : Int.ofNat (m + 1) ≠ 0 := fun e => Nat.noConfusion (Int.ofNat.inj e)
  -- product of primaries of norms m, n is primary of norm mn
  have hprod : ∀ α β, PD43 (m + 1) α → PD43 (n + 1) β → PD43 ((m + 1) * (n + 1)) ((Ring43.ring43 LibInt.intR).mul α β) := fun α β ⟨h1, p1⟩ ⟨h2, p2⟩ =>
    ⟨by rw [en_mul, h1, h2]; rfl, prim_mul _ _ p1 p2⟩
  -- Step A: expand c_m c_n with a δ over γ
  have stepA : (Ring43.ring43 LibInt.intR).mul (cw43 w (m + 1)) (cw43 w (n + 1))
      = sumK (Ring43.ring43 LibInt.intR) (fun α => sumK (Ring43.ring43 LibInt.intR) (fun β => sumK (Ring43.ring43 LibInt.intR) (fun γ => (Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul (I43 (m + 1) α) (I43 (n + 1) β)) (delta (Ring43.ring43 LibInt.intR) γ ((Ring43.ring43 LibInt.intR).mul α β))) (w γ)) (box43 ((m + 1) * (n + 1)))) (box43 (n + 1))) (b  -- (+19 chars; full line in the Zenodo source)
    unfold cw43; rw [sumK_mul]
    refine sumK_ext (Ring43.ring43 LibInt.intR) _ _ (fun α => ?_) _
    rw [mul_sumK]
    refine sumK_ext (Ring43.ring43 LibInt.intR) _ _ (fun β => ?_) _
    by_cases hα : PD43 (m + 1) α
    · by_cases hβ : PD43 (n + 1) β
      · rw [I43_pos _ _ hα, I43_pos _ _ hβ]
        have hb := abs_le43 _ _ (hprod α β hα hβ).1
        have e := HeckeMult.pick_box (Ring43.ring43 LibInt.intR) (fun γ => w γ) ((Ring43.ring43 LibInt.intR).mul α β) _ hb.1 hb.2
        rw [show sumK (Ring43.ring43 LibInt.intR) (fun γ => (Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul (Ring43.ring43 LibInt.intR).one (Ring43.ring43 LibInt.intR).one) (delta (Ring43.ring43 LibInt.intR) γ ((Ring43.ring43 LibInt.intR).mul α β))) (w γ)) (box43 ((m + 1) * (n + 1)))
            = sumK (Ring43.ring43 LibInt.intR) (fun γ => (Ring43.ring43 LibInt.intR).mul (delta (Ring43.ring43 LibInt.intR) γ ((Ring43.ring43 LibInt.intR).mul α β)) (w γ)) (box43 ((m + 1) * (n + 1))) from sumK_ext (Ring43.ring43 LibInt.intR) _ _ (fun γ => by lib_ring) _, e, hw]
        lib_ring
      · rw [I43_neg _ _ hβ]
        rw [sumK_ext (Ring43.ring43 LibInt.intR) _ _ (fun γ => show (Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul (I43 (m + 1) α) (Ring43.ring43 LibInt.intR).zero) (delta (Ring43.ring43 LibInt.intR) γ ((Ring43.ring43 LibInt.intR).mul α β))) (w γ) = (Ring43.ring43 LibInt.intR).zero by lib_ring)]
        rw [zsum43]; lib_ring
    · rw [I43_neg _ _ hα]
      rw [sumK_ext (Ring43.ring43 LibInt.intR) _ _ (fun γ => show (Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul (Ring43.ring43 LibInt.intR).zero (I43 (n + 1) β)) (delta (Ring43.ring43 LibInt.intR) γ ((Ring43.ring43 LibInt.intR).mul α β))) (w γ) = (Ring43.ring43 LibInt.intR).zero by lib_ring)]
      rw [zsum43]; lib_ring

  rw [stepA]
  -- swap to Σ_γ Σ_α Σ_β
  rw [sumK_ext (Ring43.ring43 LibInt.intR) _ _ (fun α => sumK_swap (Ring43.ring43 LibInt.intR) _ _ _)]
  rw [sumK_swap (Ring43.ring43 LibInt.intR)]
  unfold cw43
  refine sumK_ext (Ring43.ring43 LibInt.intR) _ _ (fun γ => ?_) _
  have e1 : sumK (Ring43.ring43 LibInt.intR) (fun α => sumK (Ring43.ring43 LibInt.intR) (fun β => (Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul (I43 (m + 1) α) (I43 (n + 1) β)) (delta (Ring43.ring43 LibInt.intR) γ ((Ring43.ring43 LibInt.intR).mul α β))) (w γ)) (box43 (n + 1))) (box43 (m + 1))
      = (Ring43.ring43 LibInt.intR).mul (sumK (Ring43.ring43 LibInt.intR) (fun α => sumK (Ring43.ring43 LibInt.intR) (fun β => (Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul (I43 (m + 1) α) (I43 (n + 1) β)) (delta (Ring43.ring43 LibInt.intR) γ ((Ring43.ring43 LibInt.intR).mul α β))) (box43 (n + 1))) (box43 (m + 1))) (w γ) := by
    rw [sumK_mul]; exact sumK_ext (Ring43.ring43 LibInt.intR) _ _ (fun α => by rw [sumK_mul]) _
  rw [e1]
  by_cases hγ : PD43 ((m + 1) * (n + 1)) γ
  · rw [I43_pos _ _ hγ]
    match prim43_split γ m n s t hγ.1 hst hm hn hγ.2 with
    | ⟨α0, β0, h0, pα0, pβ0, nα0, nβ0⟩ =>
      have pt : ∀ α β, (Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul (I43 (m + 1) α) (I43 (n + 1) β)) (delta (Ring43.ring43 LibInt.intR) γ ((Ring43.ring43 LibInt.intR).mul α β)) = (Ring43.ring43 LibInt.intR).mul (delta (Ring43.ring43 LibInt.intR) α α0) (delta (Ring43.ring43 LibInt.intR) β β0) := by
        intro α β
        by_cases hαβ : PD43 (m + 1) α ∧ PD43 (n + 1) β ∧ γ = (Ring43.ring43 LibInt.intR).mul α β
        · match hαβ with
          | ⟨hα, hβ, hg⟩ =>
            have u := uniq_split43 α β α0 β0 _ _ s t hα.1 nα0 nβ0 hst hM0 hα.2 pα0 (hg.symm.trans h0)
            rw [I43_pos _ _ hα, I43_pos _ _ hβ, ← u.1, ← u.2]
            unfold delta; rw [ite_pos' _ _ hg, ite_pos' _ _ rfl, ite_pos' _ _ rfl]; lib_ring
        · by_cases he : α = α0 ∧ β = β0
          · exfalso; apply hαβ
            rw [he.1, he.2]; exact ⟨⟨nα0, pα0⟩, ⟨nβ0, pβ0⟩, h0⟩
          · have rhs : (Ring43.ring43 LibInt.intR).mul (delta (Ring43.ring43 LibInt.intR) α α0) (delta (Ring43.ring43 LibInt.intR) β β0) = (Ring43.ring43 LibInt.intR).zero := by
              unfold delta
              by_cases h1 : α = α0
              · rw [ite_neg' _ _ (fun h2 => he ⟨h1, h2⟩), ite_pos' _ _ h1]; lib_ring
              · rw [ite_neg' _ _ h1]; lib_ring
            rw [rhs]
            by_cases hα : PD43 (m + 1) α
            · by_cases hβ : PD43 (n + 1) β
              · have hg : ¬ γ = (Ring43.ring43 LibInt.intR).mul α β := fun hg => hαβ ⟨hα, hβ, hg⟩
                unfold delta; rw [ite_neg' _ _ hg]; lib_ring
              · rw [I43_neg _ _ hβ]; lib_ring
            · rw [I43_neg _ _ hα]; lib_ring
      rw [sumK_ext (Ring43.ring43 LibInt.intR) _ _ (fun α => sumK_ext (Ring43.ring43 LibInt.intR) _ _ (fun β => pt α β) _)]
      have ba := abs_le43 α0 _ nα0
      have bb := abs_le43 β0 _ nβ0
      rw [sumK_ext (Ring43.ring43 LibInt.intR) _ _ (fun α => by rw [← mul_sumK, HeckeMult.count_box (Ring43.ring43 LibInt.intR) β0 _ bb.1 bb.2])]
      rw [sumK_ext (Ring43.ring43 LibInt.intR) _ _ (fun α => show (Ring43.ring43 LibInt.intR).mul (delta (Ring43.ring43 LibInt.intR) α α0) (Ring43.ring43 LibInt.intR).one = (Ring43.ring43 LibInt.intR).mul (delta (Ring43.ring43 LibInt.intR) α α0) (Ring43.ring43 LibInt.intR).one from rfl), ← sumK_mul,
        HeckeMult.count_box (Ring43.ring43 LibInt.intR) α0 _ ba.1 ba.2]
      lib_ring
  · rw [I43_neg _ _ hγ]
    have pt : ∀ α β, (Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul (I43 (m + 1) α) (I43 (n + 1) β)) (delta (Ring43.ring43 LibInt.intR) γ ((Ring43.ring43 LibInt.intR).mul α β)) = (Ring43.ring43 LibInt.intR).zero := by
      intro α β
      by_cases hα : PD43 (m + 1) α
      · by_cases hβ : PD43 (n + 1) β
        · have hg : ¬ γ = (Ring43.ring43 LibInt.intR).mul α β := fun hg => hγ (by rw [hg]; exact hprod α β hα hβ)
          unfold delta; rw [ite_neg' _ _ hg]; lib_ring
        · rw [I43_neg _ _ hβ]; lib_ring
      · rw [I43_neg _ _ hα]; lib_ring
    rw [sumK_ext (Ring43.ring43 LibInt.intR) _ _ (fun α => sumK_ext (Ring43.ring43 LibInt.intR) _ _ (fun β => pt α β) _)]
    rw [sumK_ext (Ring43.ring43 LibInt.intR) _ _ (fun α => zsum43 _), zsum43]

/-- **The Hecke characters α ↦ α^k of ℚ(√−3)**: c_k(mn) = c_k(m) c_k(n). -/
theorem hecke_mult43_pow (k : Nat) (m n : Nat) (s t : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 43 * k) (hn : ∀ k, Int.ofNat (n + 1) ≠ 43 * k) :
    cw43 (fun z => Frobenius.pw (Ring43.ring43 LibInt.intR) z k) ((m + 1) * (n + 1)) =
      (Ring43.ring43 LibInt.intR).mul (cw43 (fun z => Frobenius.pw (Ring43.ring43 LibInt.intR) z k) (m + 1)) (cw43 (fun z => Frobenius.pw (Ring43.ring43 LibInt.intR) z k) (n + 1)) :=
  hecke_mult43 _ (fun α β => Frobenius.pw_mul _ α β k) m n s t hst hm hn

end HeckeMult43

#print axioms HeckeMult43.abs_le43
#print axioms HeckeMult43.zsum43
#print axioms HeckeMult43.I43_pos
#print axioms HeckeMult43.I43_neg
#print axioms HeckeMult43.hecke_mult43
#print axioms HeckeMult43.hecke_mult43_pow
#print axioms HeckeMult43.cw43

lang/HeckeMult67.lean

lean_all/lang/HeckeMult67.lean

/-
  lang/HeckeMult67 — T48/T50, T48: **the theta series of ℚ(√−67)'s Hecke character class is multiplicative**: for every
  multiplicative w on ℤ[θ], θ = (1 + √−67)/2,  c_w(n) = Σ_{α ∈ H67, N(α) = n} w(α)  satisfies c_w(mn) = c_w(m)c_w(n) for coprime m, n
  prime to 67.  Enumeration box67 n = HeckeMult.box (68n).
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Uniq67
import lang.HeckeMultW

namespace HeckeMult67

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Euc67 Prim67 Uniq67

abbrev box67 (n : Nat) : List (Int × Int) := HeckeMult.box (68 * n)

/-- N(z) = n ⇒ |z.1|, |z.2| ≤ 68n  (67a² + (a + 34b)² = 68N, (2a + b)² + 67b² = 4N). -/
theorem abs_le67 (z : Int × Int) (n : Nat) (h : en z = Int.ofNat n) : Int.natAbs z.1 ≤ 68 * n ∧ Int.natAbs z.2 ≤ 68 * n := by
  have hn : nrm z = n := Int.ofNat.inj (by rw [nrm_cast]; exact h)
  have h4 := four_nrm z
  rw [hn] at h4
  have h8 : 67 * (Int.natAbs z.1 * Int.natAbs z.1) + Int.natAbs (z.1 + 34 * z.2) * Int.natAbs (z.1 + 34 * z.2) = 68 * n := Int.ofNat.inj (by
    show 67 * Int.ofNat (Int.natAbs z.1 * Int.natAbs z.1) + Int.ofNat (Int.natAbs (z.1 + 34 * z.2) * Int.natAbs (z.1 + 34 * z.2)) = 68 * Int.ofNat n
    rw [GaussEuclid.nas, GaussEuclid.nas, ← h]; unfold en; lib_int)
  have seven : ∀ X : Nat, X ≤ 67 * (X * X) := fun X => Nat.le_trans (HeckeMult.le_sq X) (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ (Nat.le_add_left 1 66)))
  constructor
  · exact Nat.le_trans (seven _) (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq h8))
  · exact Nat.le_trans (seven _) (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_trans (Nat.le_of_eq h4.symm) (Nat.mul_le_mul_right n (Nat.le_add_right 4 64))))

def PD67 (n : Nat) (z : Int × Int) : Prop := en z = Int.ofNat n ∧ Prim67 z

instance (n : Nat) (z : Int × Int) : Decidable (PD67 n z) := inferInstanceAs (Decidable (_ ∧ _))

theorem zsum67 {A : Type} : ∀ l : List A, sumK (Ring67.ring67 LibInt.intR) (fun _ => (Ring67.ring67 LibInt.intR).zero) l = (Ring67.ring67 LibInt.intR).zero
  | [] => rfl
  | _ :: l => by show (Ring67.ring67 LibInt.intR).add (Ring67.ring67 LibInt.intR).zero (sumK (Ring67.ring67 LibInt.intR) (fun _ => (Ring67.ring67 LibInt.intR).zero) l) = _; rw [zsum67 l]; exact (Ring67.ring67 LibInt.intR).zero_add _

def I67 (n : Nat) (z : Int × Int) : Int × Int := if PD67 n z then (Ring67.ring67 LibInt.intR).one else (Ring67.ring67 LibInt.intR).zero

theorem I67_pos (n : Nat) (z : Int × Int) (h : PD67 n z) : I67 n z = (Ring67.ring67 LibInt.intR).one := ite_pos' _ _ h
theorem I67_neg (n : Nat) (z : Int × Int) (h : ¬ PD67 n z) : I67 n z = (Ring67.ring67 LibInt.intR).zero := ite_neg' _ _ h

/-- c_w(n) = Σ_{α ≡ 1 (3), N(α) = n} w(α). -/
def cw67 (w : Int × Int → Int × Int) (n : Nat) : Int × Int := sumK (Ring67.ring67 LibInt.intR) (fun z => (Ring67.ring67 LibInt.intR).mul (I67 n z) (w z)) (box67 n)

/-- **c_w(mn) = c_w(m) c_w(n)** for coprime m, n prime to 3 and multiplicative w on ℤ[ω]. -/
theorem hecke_mult67 (w : Int × Int → Int × Int) (hw : ∀ α β, w ((Ring67.ring67 LibInt.intR).mul α β) = (Ring67.ring67 LibInt.intR).mul (w α) (w β)) (m n : Nat) (s t : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 67 * k) (hn : ∀ k, Int.ofNat (n + 1) ≠ 67 * k) :
    cw67 w ((m + 1) * (n + 1)) = (Ring67.ring67 LibInt.intR).mul (cw67 w (m + 1)) (cw67 w (n + 1)) := by
  have hM0 : Int.ofNat (m + 1) ≠ 0 := fun e => Nat.noConfusion (Int.ofNat.inj e)
  -- product of primaries of norms m, n is primary of norm mn
  have hprod : ∀ α β, PD67 (m + 1) α → PD67 (n + 1) β → PD67 ((m + 1) * (n + 1)) ((Ring67.ring67 LibInt.intR).mul α β) := fun α β ⟨h1, p1⟩ ⟨h2, p2⟩ =>
    ⟨by rw [en_mul, h1, h2]; rfl, prim_mul _ _ p1 p2⟩
  -- Step A: expand c_m c_n with a δ over γ
  have stepA : (Ring67.ring67 LibInt.intR).mul (cw67 w (m + 1)) (cw67 w (n + 1))
      = sumK (Ring67.ring67 LibInt.intR) (fun α => sumK (Ring67.ring67 LibInt.intR) (fun β => sumK (Ring67.ring67 LibInt.intR) (fun γ => (Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul (I67 (m + 1) α) (I67 (n + 1) β)) (delta (Ring67.ring67 LibInt.intR) γ ((Ring67.ring67 LibInt.intR).mul α β))) (w γ)) (box67 ((m + 1) * (n + 1)))) (box67 (n + 1))) (b  -- (+19 chars; full line in the Zenodo source)
    unfold cw67; rw [sumK_mul]
    refine sumK_ext (Ring67.ring67 LibInt.intR) _ _ (fun α => ?_) _
    rw [mul_sumK]
    refine sumK_ext (Ring67.ring67 LibInt.intR) _ _ (fun β => ?_) _
    by_cases hα : PD67 (m + 1) α
    · by_cases hβ : PD67 (n + 1) β
      · rw [I67_pos _ _ hα, I67_pos _ _ hβ]
        have hb := abs_le67 _ _ (hprod α β hα hβ).1
        have e := HeckeMult.pick_box (Ring67.ring67 LibInt.intR) (fun γ => w γ) ((Ring67.ring67 LibInt.intR).mul α β) _ hb.1 hb.2
        rw [show sumK (Ring67.ring67 LibInt.intR) (fun γ => (Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul (Ring67.ring67 LibInt.intR).one (Ring67.ring67 LibInt.intR).one) (delta (Ring67.ring67 LibInt.intR) γ ((Ring67.ring67 LibInt.intR).mul α β))) (w γ)) (box67 ((m + 1) * (n + 1)))
            = sumK (Ring67.ring67 LibInt.intR) (fun γ => (Ring67.ring67 LibInt.intR).mul (delta (Ring67.ring67 LibInt.intR) γ ((Ring67.ring67 LibInt.intR).mul α β)) (w γ)) (box67 ((m + 1) * (n + 1))) from sumK_ext (Ring67.ring67 LibInt.intR) _ _ (fun γ => by lib_ring) _, e, hw]
        lib_ring
      · rw [I67_neg _ _ hβ]
        rw [sumK_ext (Ring67.ring67 LibInt.intR) _ _ (fun γ => show (Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul (I67 (m + 1) α) (Ring67.ring67 LibInt.intR).zero) (delta (Ring67.ring67 LibInt.intR) γ ((Ring67.ring67 LibInt.intR).mul α β))) (w γ) = (Ring67.ring67 LibInt.intR).zero by lib_ring)]
        rw [zsum67]; lib_ring
    · rw [I67_neg _ _ hα]
      rw [sumK_ext (Ring67.ring67 LibInt.intR) _ _ (fun γ => show (Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul (Ring67.ring67 LibInt.intR).zero (I67 (n + 1) β)) (delta (Ring67.ring67 LibInt.intR) γ ((Ring67.ring67 LibInt.intR).mul α β))) (w γ) = (Ring67.ring67 LibInt.intR).zero by lib_ring)]
      rw [zsum67]; lib_ring

  rw [stepA]
  -- swap to Σ_γ Σ_α Σ_β
  rw [sumK_ext (Ring67.ring67 LibInt.intR) _ _ (fun α => sumK_swap (Ring67.ring67 LibInt.intR) _ _ _)]
  rw [sumK_swap (Ring67.ring67 LibInt.intR)]
  unfold cw67
  refine sumK_ext (Ring67.ring67 LibInt.intR) _ _ (fun γ => ?_) _
  have e1 : sumK (Ring67.ring67 LibInt.intR) (fun α => sumK (Ring67.ring67 LibInt.intR) (fun β => (Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul (I67 (m + 1) α) (I67 (n + 1) β)) (delta (Ring67.ring67 LibInt.intR) γ ((Ring67.ring67 LibInt.intR).mul α β))) (w γ)) (box67 (n + 1))) (box67 (m + 1))
      = (Ring67.ring67 LibInt.intR).mul (sumK (Ring67.ring67 LibInt.intR) (fun α => sumK (Ring67.ring67 LibInt.intR) (fun β => (Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul (I67 (m + 1) α) (I67 (n + 1) β)) (delta (Ring67.ring67 LibInt.intR) γ ((Ring67.ring67 LibInt.intR).mul α β))) (box67 (n + 1))) (box67 (m + 1))) (w γ) := by
    rw [sumK_mul]; exact sumK_ext (Ring67.ring67 LibInt.intR) _ _ (fun α => by rw [sumK_mul]) _
  rw [e1]
  by_cases hγ : PD67 ((m + 1) * (n + 1)) γ
  · rw [I67_pos _ _ hγ]
    match prim67_split γ m n s t hγ.1 hst hm hn hγ.2 with
    | ⟨α0, β0, h0, pα0, pβ0, nα0, nβ0⟩ =>
      have pt : ∀ α β, (Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul (I67 (m + 1) α) (I67 (n + 1) β)) (delta (Ring67.ring67 LibInt.intR) γ ((Ring67.ring67 LibInt.intR).mul α β)) = (Ring67.ring67 LibInt.intR).mul (delta (Ring67.ring67 LibInt.intR) α α0) (delta (Ring67.ring67 LibInt.intR) β β0) := by
        intro α β
        by_cases hαβ : PD67 (m + 1) α ∧ PD67 (n + 1) β ∧ γ = (Ring67.ring67 LibInt.intR).mul α β
        · match hαβ with
          | ⟨hα, hβ, hg⟩ =>
            have u := uniq_split67 α β α0 β0 _ _ s t hα.1 nα0 nβ0 hst hM0 hα.2 pα0 (hg.symm.trans h0)
            rw [I67_pos _ _ hα, I67_pos _ _ hβ, ← u.1, ← u.2]
            unfold delta; rw [ite_pos' _ _ hg, ite_pos' _ _ rfl, ite_pos' _ _ rfl]; lib_ring
        · by_cases he : α = α0 ∧ β = β0
          · exfalso; apply hαβ
            rw [he.1, he.2]; exact ⟨⟨nα0, pα0⟩, ⟨nβ0, pβ0⟩, h0⟩
          · have rhs : (Ring67.ring67 LibInt.intR).mul (delta (Ring67.ring67 LibInt.intR) α α0) (delta (Ring67.ring67 LibInt.intR) β β0) = (Ring67.ring67 LibInt.intR).zero := by
              unfold delta
              by_cases h1 : α = α0
              · rw [ite_neg' _ _ (fun h2 => he ⟨h1, h2⟩), ite_pos' _ _ h1]; lib_ring
              · rw [ite_neg' _ _ h1]; lib_ring
            rw [rhs]
            by_cases hα : PD67 (m + 1) α
            · by_cases hβ : PD67 (n + 1) β
              · have hg : ¬ γ = (Ring67.ring67 LibInt.intR).mul α β := fun hg => hαβ ⟨hα, hβ, hg⟩
                unfold delta; rw [ite_neg' _ _ hg]; lib_ring
              · rw [I67_neg _ _ hβ]; lib_ring
            · rw [I67_neg _ _ hα]; lib_ring
      rw [sumK_ext (Ring67.ring67 LibInt.intR) _ _ (fun α => sumK_ext (Ring67.ring67 LibInt.intR) _ _ (fun β => pt α β) _)]
      have ba := abs_le67 α0 _ nα0
      have bb := abs_le67 β0 _ nβ0
      rw [sumK_ext (Ring67.ring67 LibInt.intR) _ _ (fun α => by rw [← mul_sumK, HeckeMult.count_box (Ring67.ring67 LibInt.intR) β0 _ bb.1 bb.2])]
      rw [sumK_ext (Ring67.ring67 LibInt.intR) _ _ (fun α => show (Ring67.ring67 LibInt.intR).mul (delta (Ring67.ring67 LibInt.intR) α α0) (Ring67.ring67 LibInt.intR).one = (Ring67.ring67 LibInt.intR).mul (delta (Ring67.ring67 LibInt.intR) α α0) (Ring67.ring67 LibInt.intR).one from rfl), ← sumK_mul,
        HeckeMult.count_box (Ring67.ring67 LibInt.intR) α0 _ ba.1 ba.2]
      lib_ring
  · rw [I67_neg _ _ hγ]
    have pt : ∀ α β, (Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul (I67 (m + 1) α) (I67 (n + 1) β)) (delta (Ring67.ring67 LibInt.intR) γ ((Ring67.ring67 LibInt.intR).mul α β)) = (Ring67.ring67 LibInt.intR).zero := by
      intro α β
      by_cases hα : PD67 (m + 1) α
      · by_cases hβ : PD67 (n + 1) β
        · have hg : ¬ γ = (Ring67.ring67 LibInt.intR).mul α β := fun hg => hγ (by rw [hg]; exact hprod α β hα hβ)
          unfold delta; rw [ite_neg' _ _ hg]; lib_ring
        · rw [I67_neg _ _ hβ]; lib_ring
      · rw [I67_neg _ _ hα]; lib_ring
    rw [sumK_ext (Ring67.ring67 LibInt.intR) _ _ (fun α => sumK_ext (Ring67.ring67 LibInt.intR) _ _ (fun β => pt α β) _)]
    rw [sumK_ext (Ring67.ring67 LibInt.intR) _ _ (fun α => zsum67 _), zsum67]

/-- **The Hecke characters α ↦ α^k of ℚ(√−3)**: c_k(mn) = c_k(m) c_k(n). -/
theorem hecke_mult67_pow (k : Nat) (m n : Nat) (s t : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 67 * k) (hn : ∀ k, Int.ofNat (n + 1) ≠ 67 * k) :
    cw67 (fun z => Frobenius.pw (Ring67.ring67 LibInt.intR) z k) ((m + 1) * (n + 1)) =
      (Ring67.ring67 LibInt.intR).mul (cw67 (fun z => Frobenius.pw (Ring67.ring67 LibInt.intR) z k) (m + 1)) (cw67 (fun z => Frobenius.pw (Ring67.ring67 LibInt.intR) z k) (n + 1)) :=
  hecke_mult67 _ (fun α β => Frobenius.pw_mul _ α β k) m n s t hst hm hn

end HeckeMult67

#print axioms HeckeMult67.abs_le67
#print axioms HeckeMult67.zsum67
#print axioms HeckeMult67.I67_pos
#print axioms HeckeMult67.I67_neg
#print axioms HeckeMult67.hecke_mult67
#print axioms HeckeMult67.hecke_mult67_pow
#print axioms HeckeMult67.cw67

lang/HeckeMult7.lean

lean_all/lang/HeckeMult7.lean

/-
  lang/HeckeMult7 — T48/T50, cycle 642: **the theta series of 49a's Hecke character class is multiplicative**: for every
  multiplicative w on ℤ[θ], θ = (1 + √−7)/2,  c_w(n) = Σ_{α ∈ H7, N(α) = n} w(α)  satisfies c_w(mn) = c_w(m)c_w(n) for coprime m, n
  prime to 7.  Enumeration box7 n = HeckeMult.box (4n) (4N = (2a + b)² + 7b²).
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Uniq7
import lang.HeckeMultW

namespace HeckeMult7

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Euc7 Prim7 Uniq7

abbrev box7 (n : Nat) : List (Int × Int) := HeckeMult.box (8 * n)

/-- N(z) = n ⇒ |z.1|, |z.2| ≤ 8n  (7a² + (a + 4b)² = 8N, (2a + b)² + 7b² = 4N). -/
theorem abs_le7 (z : Int × Int) (n : Nat) (h : en z = Int.ofNat n) : Int.natAbs z.1 ≤ 8 * n ∧ Int.natAbs z.2 ≤ 8 * n := by
  have hn : nrm z = n := Int.ofNat.inj (by rw [nrm_cast]; exact h)
  have h4 := four_nrm z
  rw [hn] at h4
  have h8 : 7 * (Int.natAbs z.1 * Int.natAbs z.1) + Int.natAbs (z.1 + 4 * z.2) * Int.natAbs (z.1 + 4 * z.2) = 8 * n := Int.ofNat.inj (by
    show 7 * Int.ofNat (Int.natAbs z.1 * Int.natAbs z.1) + Int.ofNat (Int.natAbs (z.1 + 4 * z.2) * Int.natAbs (z.1 + 4 * z.2)) = 8 * Int.ofNat n
    rw [GaussEuclid.nas, GaussEuclid.nas, ← h]; unfold en; lib_int)
  have seven : ∀ X : Nat, X ≤ 7 * (X * X) := fun X => Nat.le_trans (HeckeMult.le_sq X) (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ (Nat.le_add_left 1 6)))
  constructor
  · exact Nat.le_trans (seven _) (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq h8))
  · exact Nat.le_trans (seven _) (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_trans (Nat.le_of_eq h4.symm) (Nat.mul_le_mul_right n (Nat.le_add_right 4 4))))

def PD7 (n : Nat) (z : Int × Int) : Prop := en z = Int.ofNat n ∧ Prim7 z

theorem prim7_iff (z : Int × Int) :
    (ZMod.md 6 (Int.natAbs (rho z - 1)) = 0 ∨ ZMod.md 6 (Int.natAbs (rho z - 2)) = 0 ∨ ZMod.md 6 (Int.natAbs (rho z - 4)) = 0) ↔ Prim7 z := by
  constructor
  · intro h
    rcases h with h | h | h
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 6 _).mpr h
      exact ⟨e, Or.inl (by calc rho z = (rho z - 1) + 1 := by lib_int
                               _ = _ := by rw [he]; rfl)⟩
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 6 _).mpr h
      exact ⟨e, Or.inr (Or.inl (by calc rho z = (rho z - 2) + 2 := by lib_int
                                        _ = _ := by rw [he]; rfl))⟩
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 6 _).mpr h
      exact ⟨e, Or.inr (Or.inr (by calc rho z = (rho z - 4) + 4 := by lib_int
                                        _ = _ := by rw [he]; rfl))⟩
  · intro ⟨e, h⟩
    rcases h with h | h | h
    · exact Or.inl ((HeckeMult.idv_iff 6 _).mp ⟨e, by rw [h]; show 7 * e + 1 - 1 = 7 * e; lib_int⟩)
    · exact Or.inr (Or.inl ((HeckeMult.idv_iff 6 _).mp ⟨e, by rw [h]; show 7 * e + 2 - 2 = 7 * e; lib_int⟩))
    · exact Or.inr (Or.inr ((HeckeMult.idv_iff 6 _).mp ⟨e, by rw [h]; show 7 * e + 4 - 4 = 7 * e; lib_int⟩))

instance (z : Int × Int) : Decidable (Prim7 z) := decidable_of_iff _ (prim7_iff z)

instance (n : Nat) (z : Int × Int) : Decidable (PD7 n z) := inferInstanceAs (Decidable (_ ∧ _))

theorem zsum7 {A : Type} : ∀ l : List A, sumK (Ring7.ring7 LibInt.intR) (fun _ => (Ring7.ring7 LibInt.intR).zero) l = (Ring7.ring7 LibInt.intR).zero
  | [] => rfl
  | _ :: l => by show (Ring7.ring7 LibInt.intR).add (Ring7.ring7 LibInt.intR).zero (sumK (Ring7.ring7 LibInt.intR) (fun _ => (Ring7.ring7 LibInt.intR).zero) l) = _; rw [zsum7 l]; exact (Ring7.ring7 LibInt.intR).zero_add _

def I7 (n : Nat) (z : Int × Int) : Int × Int := if PD7 n z then (Ring7.ring7 LibInt.intR).one else (Ring7.ring7 LibInt.intR).zero

theorem I7_pos (n : Nat) (z : Int × Int) (h : PD7 n z) : I7 n z = (Ring7.ring7 LibInt.intR).one := ite_pos' _ _ h
theorem I7_neg (n : Nat) (z : Int × Int) (h : ¬ PD7 n z) : I7 n z = (Ring7.ring7 LibInt.intR).zero := ite_neg' _ _ h

/-- c_w(n) = Σ_{α ≡ 1 (3), N(α) = n} w(α). -/
def cw7 (w : Int × Int → Int × Int) (n : Nat) : Int × Int := sumK (Ring7.ring7 LibInt.intR) (fun z => (Ring7.ring7 LibInt.intR).mul (I7 n z) (w z)) (box7 n)

/-- **c_w(mn) = c_w(m) c_w(n)** for coprime m, n prime to 3 and multiplicative w on ℤ[ω]. -/
theorem hecke_mult7 (w : Int × Int → Int × Int) (hw : ∀ α β, w ((Ring7.ring7 LibInt.intR).mul α β) = (Ring7.ring7 LibInt.intR).mul (w α) (w β)) (m n : Nat) (s t : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 7 * k) (hn : ∀ k, Int.ofNat (n + 1) ≠ 7 * k) :
    cw7 w ((m + 1) * (n + 1)) = (Ring7.ring7 LibInt.intR).mul (cw7 w (m + 1)) (cw7 w (n + 1)) := by
  have hM0 : Int.ofNat (m + 1) ≠ 0 := fun e => Nat.noConfusion (Int.ofNat.inj e)
  -- product of primaries of norms m, n is primary of norm mn
  have hprod : ∀ α β, PD7 (m + 1) α → PD7 (n + 1) β → PD7 ((m + 1) * (n + 1)) ((Ring7.ring7 LibInt.intR).mul α β) := fun α β ⟨h1, p1⟩ ⟨h2, p2⟩ =>
    ⟨by rw [en_mul, h1, h2]; rfl, prim7_mul _ _ p1 p2⟩
  -- Step A: expand c_m c_n with a δ over γ
  have stepA : (Ring7.ring7 LibInt.intR).mul (cw7 w (m + 1)) (cw7 w (n + 1))
      = sumK (Ring7.ring7 LibInt.intR) (fun α => sumK (Ring7.ring7 LibInt.intR) (fun β => sumK (Ring7.ring7 LibInt.intR) (fun γ => (Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul (I7 (m + 1) α) (I7 (n + 1) β)) (delta (Ring7.ring7 LibInt.intR) γ ((Ring7.ring7 LibInt.intR).mul α β))) (w γ)) (box7 ((m + 1) * (n + 1)))) (box7 (n + 1))) (box7 (m + 1)) := by
    unfold cw7; rw [sumK_mul]
    refine sumK_ext (Ring7.ring7 LibInt.intR) _ _ (fun α => ?_) _
    rw [mul_sumK]
    refine sumK_ext (Ring7.ring7 LibInt.intR) _ _ (fun β => ?_) _
    by_cases hα : PD7 (m + 1) α
    · by_cases hβ : PD7 (n + 1) β
      · rw [I7_pos _ _ hα, I7_pos _ _ hβ]
        have hb := abs_le7 _ _ (hprod α β hα hβ).1
        have e := HeckeMult.pick_box (Ring7.ring7 LibInt.intR) (fun γ => w γ) ((Ring7.ring7 LibInt.intR).mul α β) _ hb.1 hb.2
        rw [show sumK (Ring7.ring7 LibInt.intR) (fun γ => (Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul (Ring7.ring7 LibInt.intR).one (Ring7.ring7 LibInt.intR).one) (delta (Ring7.ring7 LibInt.intR) γ ((Ring7.ring7 LibInt.intR).mul α β))) (w γ)) (box7 ((m + 1) * (n + 1)))
            = sumK (Ring7.ring7 LibInt.intR) (fun γ => (Ring7.ring7 LibInt.intR).mul (delta (Ring7.ring7 LibInt.intR) γ ((Ring7.ring7 LibInt.intR).mul α β)) (w γ)) (box7 ((m + 1) * (n + 1))) from sumK_ext (Ring7.ring7 LibInt.intR) _ _ (fun γ => by lib_ring) _, e, hw]
        lib_ring
      · rw [I7_neg _ _ hβ]
        rw [sumK_ext (Ring7.ring7 LibInt.intR) _ _ (fun γ => show (Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul (I7 (m + 1) α) (Ring7.ring7 LibInt.intR).zero) (delta (Ring7.ring7 LibInt.intR) γ ((Ring7.ring7 LibInt.intR).mul α β))) (w γ) = (Ring7.ring7 LibInt.intR).zero by lib_ring)]
        rw [zsum7]; lib_ring
    · rw [I7_neg _ _ hα]
      rw [sumK_ext (Ring7.ring7 LibInt.intR) _ _ (fun γ => show (Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul (Ring7.ring7 LibInt.intR).zero (I7 (n + 1) β)) (delta (Ring7.ring7 LibInt.intR) γ ((Ring7.ring7 LibInt.intR).mul α β))) (w γ) = (Ring7.ring7 LibInt.intR).zero by lib_ring)]
      rw [zsum7]; lib_ring

  rw [stepA]
  -- swap to Σ_γ Σ_α Σ_β
  rw [sumK_ext (Ring7.ring7 LibInt.intR) _ _ (fun α => sumK_swap (Ring7.ring7 LibInt.intR) _ _ _)]
  rw [sumK_swap (Ring7.ring7 LibInt.intR)]
  unfold cw7
  refine sumK_ext (Ring7.ring7 LibInt.intR) _ _ (fun γ => ?_) _
  have e1 : sumK (Ring7.ring7 LibInt.intR) (fun α => sumK (Ring7.ring7 LibInt.intR) (fun β => (Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul (I7 (m + 1) α) (I7 (n + 1) β)) (delta (Ring7.ring7 LibInt.intR) γ ((Ring7.ring7 LibInt.intR).mul α β))) (w γ)) (box7 (n + 1))) (box7 (m + 1))
      = (Ring7.ring7 LibInt.intR).mul (sumK (Ring7.ring7 LibInt.intR) (fun α => sumK (Ring7.ring7 LibInt.intR) (fun β => (Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul (I7 (m + 1) α) (I7 (n + 1) β)) (delta (Ring7.ring7 LibInt.intR) γ ((Ring7.ring7 LibInt.intR).mul α β))) (box7 (n + 1))) (box7 (m + 1))) (w γ) := by
    rw [sumK_mul]; exact sumK_ext (Ring7.ring7 LibInt.intR) _ _ (fun α => by rw [sumK_mul]) _
  rw [e1]
  by_cases hγ : PD7 ((m + 1) * (n + 1)) γ
  · rw [I7_pos _ _ hγ]
    match prim7_split γ m n s t hγ.1 hst hm hn hγ.2 with
    | ⟨α0, β0, h0, pα0, pβ0, nα0, nβ0⟩ =>
      have pt : ∀ α β, (Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul (I7 (m + 1) α) (I7 (n + 1) β)) (delta (Ring7.ring7 LibInt.intR) γ ((Ring7.ring7 LibInt.intR).mul α β)) = (Ring7.ring7 LibInt.intR).mul (delta (Ring7.ring7 LibInt.intR) α α0) (delta (Ring7.ring7 LibInt.intR) β β0) := by
        intro α β
        by_cases hαβ : PD7 (m + 1) α ∧ PD7 (n + 1) β ∧ γ = (Ring7.ring7 LibInt.intR).mul α β
        · match hαβ with
          | ⟨hα, hβ, hg⟩ =>
            have u := uniq_split7 α β α0 β0 _ _ s t hα.1 nα0 nβ0 hst hM0 hα.2 pα0 (hg.symm.trans h0)
            rw [I7_pos _ _ hα, I7_pos _ _ hβ, ← u.1, ← u.2]
            unfold delta; rw [ite_pos' _ _ hg, ite_pos' _ _ rfl, ite_pos' _ _ rfl]; lib_ring
        · by_cases he : α = α0 ∧ β = β0
          · exfalso; apply hαβ
            rw [he.1, he.2]; exact ⟨⟨nα0, pα0⟩, ⟨nβ0, pβ0⟩, h0⟩
          · have rhs : (Ring7.ring7 LibInt.intR).mul (delta (Ring7.ring7 LibInt.intR) α α0) (delta (Ring7.ring7 LibInt.intR) β β0) = (Ring7.ring7 LibInt.intR).zero := by
              unfold delta
              by_cases h1 : α = α0
              · rw [ite_neg' _ _ (fun h2 => he ⟨h1, h2⟩), ite_pos' _ _ h1]; lib_ring
              · rw [ite_neg' _ _ h1]; lib_ring
            rw [rhs]
            by_cases hα : PD7 (m + 1) α
            · by_cases hβ : PD7 (n + 1) β
              · have hg : ¬ γ = (Ring7.ring7 LibInt.intR).mul α β := fun hg => hαβ ⟨hα, hβ, hg⟩
                unfold delta; rw [ite_neg' _ _ hg]; lib_ring
              · rw [I7_neg _ _ hβ]; lib_ring
            · rw [I7_neg _ _ hα]; lib_ring
      rw [sumK_ext (Ring7.ring7 LibInt.intR) _ _ (fun α => sumK_ext (Ring7.ring7 LibInt.intR) _ _ (fun β => pt α β) _)]
      have ba := abs_le7 α0 _ nα0
      have bb := abs_le7 β0 _ nβ0
      rw [sumK_ext (Ring7.ring7 LibInt.intR) _ _ (fun α => by rw [← mul_sumK, HeckeMult.count_box (Ring7.ring7 LibInt.intR) β0 _ bb.1 bb.2])]
      rw [sumK_ext (Ring7.ring7 LibInt.intR) _ _ (fun α => show (Ring7.ring7 LibInt.intR).mul (delta (Ring7.ring7 LibInt.intR) α α0) (Ring7.ring7 LibInt.intR).one = (Ring7.ring7 LibInt.intR).mul (delta (Ring7.ring7 LibInt.intR) α α0) (Ring7.ring7 LibInt.intR).one from rfl), ← sumK_mul,
        HeckeMult.count_box (Ring7.ring7 LibInt.intR) α0 _ ba.1 ba.2]
      lib_ring
  · rw [I7_neg _ _ hγ]
    have pt : ∀ α β, (Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul (I7 (m + 1) α) (I7 (n + 1) β)) (delta (Ring7.ring7 LibInt.intR) γ ((Ring7.ring7 LibInt.intR).mul α β)) = (Ring7.ring7 LibInt.intR).zero := by
      intro α β
      by_cases hα : PD7 (m + 1) α
      · by_cases hβ : PD7 (n + 1) β
        · have hg : ¬ γ = (Ring7.ring7 LibInt.intR).mul α β := fun hg => hγ (by rw [hg]; exact hprod α β hα hβ)
          unfold delta; rw [ite_neg' _ _ hg]; lib_ring
        · rw [I7_neg _ _ hβ]; lib_ring
      · rw [I7_neg _ _ hα]; lib_ring
    rw [sumK_ext (Ring7.ring7 LibInt.intR) _ _ (fun α => sumK_ext (Ring7.ring7 LibInt.intR) _ _ (fun β => pt α β) _)]
    rw [sumK_ext (Ring7.ring7 LibInt.intR) _ _ (fun α => zsum7 _), zsum7]

/-- **The Hecke characters α ↦ α^k of ℚ(√−3)**: c_k(mn) = c_k(m) c_k(n). -/
theorem hecke_mult7_pow (k : Nat) (m n : Nat) (s t : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 7 * k) (hn : ∀ k, Int.ofNat (n + 1) ≠ 7 * k) :
    cw7 (fun z => Frobenius.pw (Ring7.ring7 LibInt.intR) z k) ((m + 1) * (n + 1)) =
      (Ring7.ring7 LibInt.intR).mul (cw7 (fun z => Frobenius.pw (Ring7.ring7 LibInt.intR) z k) (m + 1)) (cw7 (fun z => Frobenius.pw (Ring7.ring7 LibInt.intR) z k) (n + 1)) :=
  hecke_mult7 _ (fun α β => Frobenius.pw_mul _ α β k) m n s t hst hm hn

end HeckeMult7

#print axioms HeckeMult7.abs_le7
#print axioms HeckeMult7.prim7_iff
#print axioms HeckeMult7.zsum7
#print axioms HeckeMult7.I7_pos
#print axioms HeckeMult7.I7_neg
#print axioms HeckeMult7.hecke_mult7
#print axioms HeckeMult7.hecke_mult7_pow
#print axioms HeckeMult7.cw7

lang/HeckeMult8.lean

lean_all/lang/HeckeMult8.lean

/-
  lang/HeckeMult8 — T48/T50, cycle 632: **the theta series of 256a's Hecke character class**: for every multiplicative w on ℤ[√−2],
    c_w(n) = Σ_{α ∈ H8, N(α) = n} w(α)  is multiplicative on coprime odd m, n  (HeckeMult2 with the class H8 of Sq2Prim8).
  For w = id, c(p) = 2a for p = a² + 2b² (π = a + b√−2 ∈ H8) — the a_p of y² = x³ + 4x² + 2x (checked numerically).
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Sq2Unique8
import lang.HeckeMultW

namespace HeckeMult8

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Sq2Euclid Sq2Prim8 Sq2Unique8

abbrev box8 (n : Nat) : List (Int × Int) := HeckeMult.box n

/-- N(z) = n ⇒ |z.1|, |z.2| ≤ n. -/
theorem abs_le8 (z : Int × Int) (n : Nat) (h : en z = Int.ofNat n) : Int.natAbs z.1 ≤ n ∧ Int.natAbs z.2 ≤ n := by
  have hn : nrm z = n := Int.ofNat.inj (by rw [nrm_cast]; exact h)
  have hn' : Int.natAbs z.1 * Int.natAbs z.1 + 2 * (Int.natAbs z.2 * Int.natAbs z.2) = n := hn
  constructor
  · exact Nat.le_trans (HeckeMult.le_sq _) (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq hn'))
  · exact Nat.le_trans (HeckeMult.le_sq _) (Nat.le_trans (Nat.le_trans (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ (Nat.le_succ 1))) (Nat.le_add_left _ _)) (Nat.le_of_eq hn'))

def PD8 (n : Nat) (z : Int × Int) : Prop := en z = Int.ofNat n ∧ Prim8 z

theorem prim8_iff (z : Int × Int) : ((ZMod.md 7 (Int.natAbs (z.1 - 1)) = 0 ∧ ZMod.md 3 (Int.natAbs (z.2 - 0)) = 0) ∨ (ZMod.md 7 (Int.natAbs (z.1 - 3)) = 0 ∧ ZMod.md 3 (Int.natAbs (z.2 - 0)) = 0) ∨ (ZMod.md 7 (Int.natAbs (z.1 - 5)) = 0 ∧ ZMod.md 3 (Int.natAbs (z.2 - 1)) = 0) ∨ (ZMod.md 7 (Int.natAbs (z.1 - 5)) = 0 ∧ ZMod.md 3 (Int.natAbs (z.2 - 2)) = 0) ∨ (ZMod.md 7 (Int.natAbs (z.1 - 5)) = 0 ∧ ZMo  -- (+291 chars; full line in the Zenodo source)
  constructor
  · intro h
    rcases h with ⟨h1, h2⟩ | ⟨h1, h2⟩ | ⟨h1, h2⟩ | ⟨h1, h2⟩ | ⟨h1, h2⟩ | ⟨h1, h2⟩ | ⟨h1, h2⟩ | ⟨h1, h2⟩
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 7 _).mpr h1
      obtain ⟨f, hf⟩ := (HeckeMult.idv_iff 3 _).mpr h2
      exact (Or.inl ⟨e, f, by calc z.1 = (z.1 - 1) + 1 := by lib_int
          _ = _ := by rw [he]; rfl, by calc z.2 = (z.2 - 0) + 0 := by lib_int
          _ = _ := by rw [hf]; rfl⟩)
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 7 _).mpr h1
      obtain ⟨f, hf⟩ := (HeckeMult.idv_iff 3 _).mpr h2
      exact (Or.inr (Or.inl ⟨e, f, by calc z.1 = (z.1 - 3) + 3 := by lib_int
          _ = _ := by rw [he]; rfl, by calc z.2 = (z.2 - 0) + 0 := by lib_int
          _ = _ := by rw [hf]; rfl⟩))
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 7 _).mpr h1
      obtain ⟨f, hf⟩ := (HeckeMult.idv_iff 3 _).mpr h2
      exact (Or.inr (Or.inr (Or.inl ⟨e, f, by calc z.1 = (z.1 - 5) + 5 := by lib_int
          _ = _ := by rw [he]; rfl, by calc z.2 = (z.2 - 1) + 1 := by lib_int
          _ = _ := by rw [hf]; rfl⟩)))
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 7 _).mpr h1
      obtain ⟨f, hf⟩ := (HeckeMult.idv_iff 3 _).mpr h2
      exact (Or.inr (Or.inr (Or.inr (Or.inl ⟨e, f, by calc z.1 = (z.1 - 5) + 5 := by lib_int
          _ = _ := by rw [he]; rfl, by calc z.2 = (z.2 - 2) + 2 := by lib_int
          _ = _ := by rw [hf]; rfl⟩))))
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 7 _).mpr h1
      obtain ⟨f, hf⟩ := (HeckeMult.idv_iff 3 _).mpr h2
      exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨e, f, by calc z.1 = (z.1 - 5) + 5 := by lib_int
          _ = _ := by rw [he]; rfl, by calc z.2 = (z.2 - 3) + 3 := by lib_int
          _ = _ := by rw [hf]; rfl⟩)))))
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 7 _).mpr h1
      obtain ⟨f, hf⟩ := (HeckeMult.idv_iff 3 _).mpr h2
      exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨e, f, by calc z.1 = (z.1 - 7) + 7 := by lib_int
          _ = _ := by rw [he]; rfl, by calc z.2 = (z.2 - 1) + 1 := by lib_int
          _ = _ := by rw [hf]; rfl⟩))))))
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 7 _).mpr h1
      obtain ⟨f, hf⟩ := (HeckeMult.idv_iff 3 _).mpr h2
      exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨e, f, by calc z.1 = (z.1 - 7) + 7 := by lib_int
          _ = _ := by rw [he]; rfl, by calc z.2 = (z.2 - 2) + 2 := by lib_int
          _ = _ := by rw [hf]; rfl⟩)))))))
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 7 _).mpr h1
      obtain ⟨f, hf⟩ := (HeckeMult.idv_iff 3 _).mpr h2
      exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨e, f, by calc z.1 = (z.1 - 7) + 7 := by lib_int
          _ = _ := by rw [he]; rfl, by calc z.2 = (z.2 - 3) + 3 := by lib_int
          _ = _ := by rw [hf]; rfl⟩)))))))
  · intro h
    rcases h with ⟨e, f, h1, h2⟩ | ⟨e, f, h1, h2⟩ | ⟨e, f, h1, h2⟩ | ⟨e, f, h1, h2⟩ | ⟨e, f, h1, h2⟩ | ⟨e, f, h1, h2⟩ | ⟨e, f, h1, h2⟩ | ⟨e, f, h1, h2⟩
    · exact (Or.inl ⟨(HeckeMult.idv_iff 7 _).mp ⟨e, by rw [h1]; show 8 * e + 1 - 1 = 8 * e; lib_int⟩, (HeckeMult.idv_iff 3 _).mp ⟨f, by rw [h2]; show 4 * f + 0 - 0 = 4 * f; lib_int⟩⟩)
    · exact (Or.inr (Or.inl ⟨(HeckeMult.idv_iff 7 _).mp ⟨e, by rw [h1]; show 8 * e + 3 - 3 = 8 * e; lib_int⟩, (HeckeMult.idv_iff 3 _).mp ⟨f, by rw [h2]; show 4 * f + 0 - 0 = 4 * f; lib_int⟩⟩))
    · exact (Or.inr (Or.inr (Or.inl ⟨(HeckeMult.idv_iff 7 _).mp ⟨e, by rw [h1]; show 8 * e + 5 - 5 = 8 * e; lib_int⟩, (HeckeMult.idv_iff 3 _).mp ⟨f, by rw [h2]; show 4 * f + 1 - 1 = 4 * f; lib_int⟩⟩)))
    · exact (Or.inr (Or.inr (Or.inr (Or.inl ⟨(HeckeMult.idv_iff 7 _).mp ⟨e, by rw [h1]; show 8 * e + 5 - 5 = 8 * e; lib_int⟩, (HeckeMult.idv_iff 3 _).mp ⟨f, by rw [h2]; show 4 * f + 2 - 2 = 4 * f; lib_int⟩⟩))))
    · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨(HeckeMult.idv_iff 7 _).mp ⟨e, by rw [h1]; show 8 * e + 5 - 5 = 8 * e; lib_int⟩, (HeckeMult.idv_iff 3 _).mp ⟨f, by rw [h2]; show 4 * f + 3 - 3 = 4 * f; lib_int⟩⟩)))))
    · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨(HeckeMult.idv_iff 7 _).mp ⟨e, by rw [h1]; show 8 * e + 7 - 7 = 8 * e; lib_int⟩, (HeckeMult.idv_iff 3 _).mp ⟨f, by rw [h2]; show 4 * f + 1 - 1 = 4 * f; lib_int⟩⟩))))))
    · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨(HeckeMult.idv_iff 7 _).mp ⟨e, by rw [h1]; show 8 * e + 7 - 7 = 8 * e; lib_int⟩, (HeckeMult.idv_iff 3 _).mp ⟨f, by rw [h2]; show 4 * f + 2 - 2 = 4 * f; lib_int⟩⟩)))))))
    · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨(HeckeMult.idv_iff 7 _).mp ⟨e, by rw [h1]; show 8 * e + 7 - 7 = 8 * e; lib_int⟩, (HeckeMult.idv_iff 3 _).mp ⟨f, by rw [h2]; show 4 * f + 3 - 3 = 4 * f; lib_int⟩⟩)))))))

instance (z : Int × Int) : Decidable (Prim8 z) := decidable_of_iff _ (prim8_iff z)

instance (n : Nat) (z : Int × Int) : Decidable (PD8 n z) := inferInstanceAs (Decidable (_ ∧ _))

theorem zsum8 {A : Type} : ∀ l : List A, sumK (Quad.quad LibInt.intR (-2)) (fun _ => (Quad.quad LibInt.intR (-2)).zero) l = (Quad.quad LibInt.intR (-2)).zero
  | [] => rfl
  | _ :: l => by show (Quad.quad LibInt.intR (-2)).add (Quad.quad LibInt.intR (-2)).zero (sumK (Quad.quad LibInt.intR (-2)) (fun _ => (Quad.quad LibInt.intR (-2)).zero) l) = _; rw [zsum8 l]; exact (Quad.quad LibInt.intR (-2)).zero_add _

def I8 (n : Nat) (z : Int × Int) : Int × Int := if PD8 n z then (Quad.quad LibInt.intR (-2)).one else (Quad.quad LibInt.intR (-2)).zero

theorem I8_pos (n : Nat) (z : Int × Int) (h : PD8 n z) : I8 n z = (Quad.quad LibInt.intR (-2)).one := ite_pos' _ _ h
theorem I8_neg (n : Nat) (z : Int × Int) (h : ¬ PD8 n z) : I8 n z = (Quad.quad LibInt.intR (-2)).zero := ite_neg' _ _ h

/-- c_w(n) = Σ_{α ≡ 1 (3), N(α) = n} w(α). -/
def cw8 (w : Int × Int → Int × Int) (n : Nat) : Int × Int := sumK (Quad.quad LibInt.intR (-2)) (fun z => (Quad.quad LibInt.intR (-2)).mul (I8 n z) (w z)) (box8 n)

/-- **c_w(mn) = c_w(m) c_w(n)** for coprime m, n prime to 3 and multiplicative w on ℤ[ω]. -/
theorem hecke_mult8 (w : Int × Int → Int × Int) (hw : ∀ α β, w ((Quad.quad LibInt.intR (-2)).mul α β) = (Quad.quad LibInt.intR (-2)).mul (w α) (w β)) (m n : Nat) (s t : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : ∃ k, Int.ofNat (m + 1) = 2 * k + 1) (hn : ∃ k, Int.ofNat (n + 1) = 2 * k + 1) :
    cw8 w ((m + 1) * (n + 1)) = (Quad.quad LibInt.intR (-2)).mul (cw8 w (m + 1)) (cw8 w (n + 1)) := by
  have hM0 : Int.ofNat (m + 1) ≠ 0 := fun e => Nat.noConfusion (Int.ofNat.inj e)
  -- product of primaries of norms m, n is primary of norm mn
  have hprod : ∀ α β, PD8 (m + 1) α → PD8 (n + 1) β → PD8 ((m + 1) * (n + 1)) ((Quad.quad LibInt.intR (-2)).mul α β) := fun α β ⟨h1, p1⟩ ⟨h2, p2⟩ =>
    ⟨by rw [en_mul, h1, h2]; rfl, prim8_mul _ _ p1 p2⟩
  -- Step A: expand c_m c_n with a δ over γ
  have stepA : (Quad.quad LibInt.intR (-2)).mul (cw8 w (m + 1)) (cw8 w (n + 1))
      = sumK (Quad.quad LibInt.intR (-2)) (fun α => sumK (Quad.quad LibInt.intR (-2)) (fun β => sumK (Quad.quad LibInt.intR (-2)) (fun γ => (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (I8 (m + 1) α) (I8 (n + 1) β)) (delta (Quad.quad LibInt.intR (-2)) γ ((Quad.quad LibInt.intR (-2)).mul α β))) (w γ)) (box8 ((m + 1) * (n + 1)))) (box8 (n + 1))  -- (+22 chars; full line in the Zenodo source)
    unfold cw8; rw [sumK_mul]
    refine sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun α => ?_) _
    rw [mul_sumK]
    refine sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun β => ?_) _
    by_cases hα : PD8 (m + 1) α
    · by_cases hβ : PD8 (n + 1) β
      · rw [I8_pos _ _ hα, I8_pos _ _ hβ]
        have hb := abs_le8 _ _ (hprod α β hα hβ).1
        have e := HeckeMult.pick_box (Quad.quad LibInt.intR (-2)) (fun γ => w γ) ((Quad.quad LibInt.intR (-2)).mul α β) _ hb.1 hb.2
        rw [show sumK (Quad.quad LibInt.intR (-2)) (fun γ => (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (Quad.quad LibInt.intR (-2)).one (Quad.quad LibInt.intR (-2)).one) (delta (Quad.quad LibInt.intR (-2)) γ ((Quad.quad LibInt.intR (-2)).mul α β))) (w γ)) (box8 ((m + 1) * (n + 1)))
            = sumK (Quad.quad LibInt.intR (-2)) (fun γ => (Quad.quad LibInt.intR (-2)).mul (delta (Quad.quad LibInt.intR (-2)) γ ((Quad.quad LibInt.intR (-2)).mul α β)) (w γ)) (box8 ((m + 1) * (n + 1))) from sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun γ => by lib_ring) _, e, hw]
        lib_ring
      · rw [I8_neg _ _ hβ]
        rw [sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun γ => show (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (I8 (m + 1) α) (Quad.quad LibInt.intR (-2)).zero) (delta (Quad.quad LibInt.intR (-2)) γ ((Quad.quad LibInt.intR (-2)).mul α β))) (w γ) = (Quad.quad LibInt.intR (-2)).zero by lib_ring)]
        rw [zsum8]; lib_ring
    · rw [I8_neg _ _ hα]
      rw [sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun γ => show (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (Quad.quad LibInt.intR (-2)).zero (I8 (n + 1) β)) (delta (Quad.quad LibInt.intR (-2)) γ ((Quad.quad LibInt.intR (-2)).mul α β))) (w γ) = (Quad.quad LibInt.intR (-2)).zero by lib_ring)]
      rw [zsum8]; lib_ring

  rw [stepA]
  -- swap to Σ_γ Σ_α Σ_β
  rw [sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun α => sumK_swap (Quad.quad LibInt.intR (-2)) _ _ _)]
  rw [sumK_swap (Quad.quad LibInt.intR (-2))]
  unfold cw8
  refine sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun γ => ?_) _
  have e1 : sumK (Quad.quad LibInt.intR (-2)) (fun α => sumK (Quad.quad LibInt.intR (-2)) (fun β => (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (I8 (m + 1) α) (I8 (n + 1) β)) (delta (Quad.quad LibInt.intR (-2)) γ ((Quad.quad LibInt.intR (-2)).mul α β))) (w γ)) (box8 (n + 1))) (box8 (m + 1))
      = (Quad.quad LibInt.intR (-2)).mul (sumK (Quad.quad LibInt.intR (-2)) (fun α => sumK (Quad.quad LibInt.intR (-2)) (fun β => (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (I8 (m + 1) α) (I8 (n + 1) β)) (delta (Quad.quad LibInt.intR (-2)) γ ((Quad.quad LibInt.intR (-2)).mul α β))) (box8 (n + 1))) (box8 (m + 1))) (w γ) := by
    rw [sumK_mul]; exact sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun α => by rw [sumK_mul]) _
  rw [e1]
  by_cases hγ : PD8 ((m + 1) * (n + 1)) γ
  · rw [I8_pos _ _ hγ]
    match prim8_split γ m n s t hγ.1 hst hm hn hγ.2 with
    | ⟨α0, β0, h0, pα0, pβ0, nα0, nβ0⟩ =>
      have pt : ∀ α β, (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (I8 (m + 1) α) (I8 (n + 1) β)) (delta (Quad.quad LibInt.intR (-2)) γ ((Quad.quad LibInt.intR (-2)).mul α β)) = (Quad.quad LibInt.intR (-2)).mul (delta (Quad.quad LibInt.intR (-2)) α α0) (delta (Quad.quad LibInt.intR (-2)) β β0) := by
        intro α β
        by_cases hαβ : PD8 (m + 1) α ∧ PD8 (n + 1) β ∧ γ = (Quad.quad LibInt.intR (-2)).mul α β
        · match hαβ with
          | ⟨hα, hβ, hg⟩ =>
            have u := uniq_split8 α β α0 β0 _ _ s t hα.1 nα0 nβ0 hst hM0 hα.2 pα0 (hg.symm.trans h0)
            rw [I8_pos _ _ hα, I8_pos _ _ hβ, ← u.1, ← u.2]
            unfold delta; rw [ite_pos' _ _ hg, ite_pos' _ _ rfl, ite_pos' _ _ rfl]; lib_ring
        · by_cases he : α = α0 ∧ β = β0
          · exfalso; apply hαβ
            rw [he.1, he.2]; exact ⟨⟨nα0, pα0⟩, ⟨nβ0, pβ0⟩, h0⟩
          · have rhs : (Quad.quad LibInt.intR (-2)).mul (delta (Quad.quad LibInt.intR (-2)) α α0) (delta (Quad.quad LibInt.intR (-2)) β β0) = (Quad.quad LibInt.intR (-2)).zero := by
              unfold delta
              by_cases h1 : α = α0
              · rw [ite_neg' _ _ (fun h2 => he ⟨h1, h2⟩), ite_pos' _ _ h1]; lib_ring
              · rw [ite_neg' _ _ h1]; lib_ring
            rw [rhs]
            by_cases hα : PD8 (m + 1) α
            · by_cases hβ : PD8 (n + 1) β
              · have hg : ¬ γ = (Quad.quad LibInt.intR (-2)).mul α β := fun hg => hαβ ⟨hα, hβ, hg⟩
                unfold delta; rw [ite_neg' _ _ hg]; lib_ring
              · rw [I8_neg _ _ hβ]; lib_ring
            · rw [I8_neg _ _ hα]; lib_ring
      rw [sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun α => sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun β => pt α β) _)]
      have ba := abs_le8 α0 _ nα0
      have bb := abs_le8 β0 _ nβ0
      rw [sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun α => by rw [← mul_sumK, HeckeMult.count_box (Quad.quad LibInt.intR (-2)) β0 _ bb.1 bb.2])]
      rw [sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun α => show (Quad.quad LibInt.intR (-2)).mul (delta (Quad.quad LibInt.intR (-2)) α α0) (Quad.quad LibInt.intR (-2)).one = (Quad.quad LibInt.intR (-2)).mul (delta (Quad.quad LibInt.intR (-2)) α α0) (Quad.quad LibInt.intR (-2)).one from rfl), ← sumK_mul,
        HeckeMult.count_box (Quad.quad LibInt.intR (-2)) α0 _ ba.1 ba.2]
      lib_ring
  · rw [I8_neg _ _ hγ]
    have pt : ∀ α β, (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (I8 (m + 1) α) (I8 (n + 1) β)) (delta (Quad.quad LibInt.intR (-2)) γ ((Quad.quad LibInt.intR (-2)).mul α β)) = (Quad.quad LibInt.intR (-2)).zero := by
      intro α β
      by_cases hα : PD8 (m + 1) α
      · by_cases hβ : PD8 (n + 1) β
        · have hg : ¬ γ = (Quad.quad LibInt.intR (-2)).mul α β := fun hg => hγ (by rw [hg]; exact hprod α β hα hβ)
          unfold delta; rw [ite_neg' _ _ hg]; lib_ring
        · rw [I8_neg _ _ hβ]; lib_ring
      · rw [I8_neg _ _ hα]; lib_ring
    rw [sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun α => sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun β => pt α β) _)]
    rw [sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun α => zsum8 _), zsum8]

/-- **The Hecke characters α ↦ α^k of ℚ(√−3)**: c_k(mn) = c_k(m) c_k(n). -/
theorem hecke_mult8_pow (k : Nat) (m n : Nat) (s t : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : ∃ k, Int.ofNat (m + 1) = 2 * k + 1) (hn : ∃ k, Int.ofNat (n + 1) = 2 * k + 1) :
    cw8 (fun z => Frobenius.pw (Quad.quad LibInt.intR (-2)) z k) ((m + 1) * (n + 1)) =
      (Quad.quad LibInt.intR (-2)).mul (cw8 (fun z => Frobenius.pw (Quad.quad LibInt.intR (-2)) z k) (m + 1)) (cw8 (fun z => Frobenius.pw (Quad.quad LibInt.intR (-2)) z k) (n + 1)) :=
  hecke_mult8 _ (fun α β => Frobenius.pw_mul _ α β k) m n s t hst hm hn

end HeckeMult8

#print axioms HeckeMult8.abs_le8
#print axioms HeckeMult8.prim8_iff
#print axioms HeckeMult8.zsum8
#print axioms HeckeMult8.I8_pos
#print axioms HeckeMult8.I8_neg
#print axioms HeckeMult8.hecke_mult8
#print axioms HeckeMult8.hecke_mult8_pow
#print axioms HeckeMult8.cw8

lang/HeckeMultW.lean

lean_all/lang/HeckeMultW.lean

/-
  lang/HeckeMultW — T50, cycle 594: **the theta series of every multiplicative weight on primary Gaussian integers is multiplicative**:
  for w : ℤ[i] → ℤ[i] with w(αβ) = w(α)w(β) (e.g. w(α) = α^k, the Hecke characters of ℚ(i) of ∞-type k),
    c_w(n) = Σ_{α primary, N(α) = n} w(α)   satisfies   c_w(mn) = c_w(m) c_w(n)  for coprime odd m, n.
  (HeckeMult.hecke_mult is w = id; the proof is the same bijection (β, γ) ↦ βγ of primaries, GaussUnique.prim_split/uniq_split.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.HeckeMult

namespace HeckeMult

open FTAChain DetChain LibInt GaussEuclid GaussCoprime GaussInt GaussFactor GaussUnique LibGcd YMChain FiniteRecChain

def cw (w : Int × Int → Int × Int) (n : Nat) : Int × Int := sumK (Cyclo4.cyc4 LibInt.intR) (fun z => (Cyclo4.cyc4 LibInt.intR).mul (I n z) (w z)) (box n)

/-- **c_w(mn) = c_w(m) c_w(n)** for coprime odd m, n and multiplicative w. -/
theorem hecke_mult_w (w : Int × Int → Int × Int) (hw : ∀ α β, w ((Cyclo4.cyc4 LibInt.intR).mul α β) = (Cyclo4.cyc4 LibInt.intR).mul (w α) (w β)) (m n : Nat) (s t km kn : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : Int.ofNat (m + 1) = 2 * km + 1) (hn : Int.ofNat (n + 1) = 2 * kn + 1) :
    cw w ((m + 1) * (n + 1)) = (Cyclo4.cyc4 LibInt.intR).mul (cw w (m + 1)) (cw w (n + 1)) := by
  have hM0 : Int.ofNat (m + 1) ≠ 0 := fun e => Nat.noConfusion (Int.ofNat.inj e)
  -- product of primaries of norms m, n is primary of norm mn
  have hprod : ∀ α β, PD (m + 1) α → PD (n + 1) β → PD ((m + 1) * (n + 1)) ((Cyclo4.cyc4 LibInt.intR).mul α β) := fun α β ⟨h1, p1⟩ ⟨h2, p2⟩ =>
    ⟨by rw [GaussEuclid.norm_mul, h1, h2]; rfl, prim_mulP _ _ p1 p2⟩
  -- Step A: expand c_m c_n with a δ over γ
  have stepA : (Cyclo4.cyc4 LibInt.intR).mul (cw w (m + 1)) (cw w (n + 1))
      = sumK (Cyclo4.cyc4 LibInt.intR) (fun α => sumK (Cyclo4.cyc4 LibInt.intR) (fun β => sumK (Cyclo4.cyc4 LibInt.intR) (fun γ => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (I (m + 1) α) (I (n + 1) β)) (delta (Cyclo4.cyc4 LibInt.intR) γ ((Cyclo4.cyc4 LibInt.intR).mul α β))) (w γ)) (box ((m + 1) * (n + 1)))) (box (n + 1))) (box (m + 1)) := by
    unfold cw; rw [sumK_mul]
    refine sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun α => ?_) _
    rw [mul_sumK]
    refine sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun β => ?_) _
    by_cases hα : PD (m + 1) α
    · by_cases hβ : PD (n + 1) β
      · rw [I_pos _ _ hα, I_pos _ _ hβ]
        have hb := abs_le _ _ (hprod α β hα hβ).1
        have e := pick_box (Cyclo4.cyc4 LibInt.intR) (fun γ => w γ) ((Cyclo4.cyc4 LibInt.intR).mul α β) _ hb.1 hb.2
        rw [show sumK (Cyclo4.cyc4 LibInt.intR) (fun γ => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (Cyclo4.cyc4 LibInt.intR).one (Cyclo4.cyc4 LibInt.intR).one) (delta (Cyclo4.cyc4 LibInt.intR) γ ((Cyclo4.cyc4 LibInt.intR).mul α β))) (w γ)) (box ((m + 1) * (n + 1)))
            = sumK (Cyclo4.cyc4 LibInt.intR) (fun γ => (Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) γ ((Cyclo4.cyc4 LibInt.intR).mul α β)) (w γ)) (box ((m + 1) * (n + 1))) from sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun γ => by lib_ring) _, e, hw]
        lib_ring
      · rw [I_neg _ _ hβ]
        rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun γ => show (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (I (m + 1) α) (Cyclo4.cyc4 LibInt.intR).zero) (delta (Cyclo4.cyc4 LibInt.intR) γ ((Cyclo4.cyc4 LibInt.intR).mul α β))) (w γ) = (Cyclo4.cyc4 LibInt.intR).zero by lib_ring)]
        rw [zsum]; lib_ring
    · rw [I_neg _ _ hα]
      rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun γ => show (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (Cyclo4.cyc4 LibInt.intR).zero (I (n + 1) β)) (delta (Cyclo4.cyc4 LibInt.intR) γ ((Cyclo4.cyc4 LibInt.intR).mul α β))) (w γ) = (Cyclo4.cyc4 LibInt.intR).zero by lib_ring)]
      rw [zsum]; lib_ring

  rw [stepA]
  -- swap to Σ_γ Σ_α Σ_β
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun α => sumK_swap (Cyclo4.cyc4 LibInt.intR) _ _ _)]
  rw [sumK_swap (Cyclo4.cyc4 LibInt.intR)]
  unfold cw
  refine sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun γ => ?_) _
  have e1 : sumK (Cyclo4.cyc4 LibInt.intR) (fun α => sumK (Cyclo4.cyc4 LibInt.intR) (fun β => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (I (m + 1) α) (I (n + 1) β)) (delta (Cyclo4.cyc4 LibInt.intR) γ ((Cyclo4.cyc4 LibInt.intR).mul α β))) (w γ)) (box (n + 1))) (box (m + 1))
      = (Cyclo4.cyc4 LibInt.intR).mul (sumK (Cyclo4.cyc4 LibInt.intR) (fun α => sumK (Cyclo4.cyc4 LibInt.intR) (fun β => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (I (m + 1) α) (I (n + 1) β)) (delta (Cyclo4.cyc4 LibInt.intR) γ ((Cyclo4.cyc4 LibInt.intR).mul α β))) (box (n + 1))) (box (m + 1))) (w γ) := by
    rw [sumK_mul]; exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun α => by rw [sumK_mul]) _
  rw [e1]
  by_cases hγ : PD ((m + 1) * (n + 1)) γ
  · rw [I_pos _ _ hγ]
    match prim_split γ m n s t km kn hγ.1 hst hm hn hγ.2 with
    | ⟨α0, β0, h0, pα0, pβ0, nα0, nβ0⟩ =>
      have pt : ∀ α β, (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (I (m + 1) α) (I (n + 1) β)) (delta (Cyclo4.cyc4 LibInt.intR) γ ((Cyclo4.cyc4 LibInt.intR).mul α β)) = (Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) α α0) (delta (Cyclo4.cyc4 LibInt.intR) β β0) := by
        intro α β
        by_cases hαβ : PD (m + 1) α ∧ PD (n + 1) β ∧ γ = (Cyclo4.cyc4 LibInt.intR).mul α β
        · match hαβ with
          | ⟨hα, hβ, hg⟩ =>
            have u := uniq_split α β α0 β0 _ _ s t hα.1 nα0 nβ0 hst hM0 hα.2 pα0 (hg.symm.trans h0)
            rw [I_pos _ _ hα, I_pos _ _ hβ, ← u.1, ← u.2]
            unfold delta; rw [ite_pos' _ _ hg, ite_pos' _ _ rfl, ite_pos' _ _ rfl]; lib_ring
        · by_cases he : α = α0 ∧ β = β0
          · exfalso; apply hαβ
            rw [he.1, he.2]; exact ⟨⟨nα0, pα0⟩, ⟨nβ0, pβ0⟩, h0⟩
          · have rhs : (Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) α α0) (delta (Cyclo4.cyc4 LibInt.intR) β β0) = (Cyclo4.cyc4 LibInt.intR).zero := by
              unfold delta
              by_cases h1 : α = α0
              · rw [ite_neg' _ _ (fun h2 => he ⟨h1, h2⟩), ite_pos' _ _ h1]; lib_ring
              · rw [ite_neg' _ _ h1]; lib_ring
            rw [rhs]
            by_cases hα : PD (m + 1) α
            · by_cases hβ : PD (n + 1) β
              · have hg : ¬ γ = (Cyclo4.cyc4 LibInt.intR).mul α β := fun hg => hαβ ⟨hα, hβ, hg⟩
                unfold delta; rw [ite_neg' _ _ hg]; lib_ring
              · rw [I_neg _ _ hβ]; lib_ring
            · rw [I_neg _ _ hα]; lib_ring
      rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun α => sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun β => pt α β) _)]
      have ba := abs_le α0 _ nα0
      have bb := abs_le β0 _ nβ0
      rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun α => by rw [← mul_sumK, count_box (Cyclo4.cyc4 LibInt.intR) β0 _ bb.1 bb.2])]
      rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun α => show (Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) α α0) (Cyclo4.cyc4 LibInt.intR).one = (Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) α α0) (Cyclo4.cyc4 LibInt.intR).one from rfl), ← sumK_mul,
        count_box (Cyclo4.cyc4 LibInt.intR) α0 _ ba.1 ba.2]
      lib_ring
  · rw [I_neg _ _ hγ]
    have pt : ∀ α β, (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (I (m + 1) α) (I (n + 1) β)) (delta (Cyclo4.cyc4 LibInt.intR) γ ((Cyclo4.cyc4 LibInt.intR).mul α β)) = (Cyclo4.cyc4 LibInt.intR).zero := by
      intro α β
      by_cases hα : PD (m + 1) α
      · by_cases hβ : PD (n + 1) β
        · have hg : ¬ γ = (Cyclo4.cyc4 LibInt.intR).mul α β := fun hg => hγ (by rw [hg]; exact hprod α β hα hβ)
          unfold delta; rw [ite_neg' _ _ hg]; lib_ring
        · rw [I_neg _ _ hβ]; lib_ring
      · rw [I_neg _ _ hα]; lib_ring
    rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun α => sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun β => pt α β) _)]
    rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun α => zsum _), zsum]

/-- **The Hecke characters α ↦ α^k (∞-type k)**: c_k(mn) = c_k(m) c_k(n). -/
theorem hecke_mult_pow (k : Nat) (m n : Nat) (s t km kn : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : Int.ofNat (m + 1) = 2 * km + 1) (hn : Int.ofNat (n + 1) = 2 * kn + 1) :
    cw (fun z => Frobenius.pw (Cyclo4.cyc4 LibInt.intR) z k) ((m + 1) * (n + 1)) =
      (Cyclo4.cyc4 LibInt.intR).mul (cw (fun z => Frobenius.pw (Cyclo4.cyc4 LibInt.intR) z k) (m + 1)) (cw (fun z => Frobenius.pw (Cyclo4.cyc4 LibInt.intR) z k) (n + 1)) :=
  hecke_mult_w _ (fun α β => Frobenius.pw_mul _ α β k) m n s t km kn hst hm hn

end HeckeMult

#print axioms HeckeMult.hecke_mult_w
#print axioms HeckeMult.hecke_mult_pow

lang/HeckePrime.lean

lean_all/lang/HeckePrime.lean

/-
  lang/HeckePrime — G3 (T43): the Hecke coefficients of ℚ(i) at prime powers of a split p, c_{p^k} = Σ_{j+l=k} π^j π̄^l.

  p = a² + b² prime, π = a + bi primary, π̄ = a − bi. In ℤ[i] (pairs, HeckeMult):
    toGI_pow, coprime_pi   π and π̄ are coprime (gcd of norm 1: norm p would make π̄ an associate of π, impossible for p odd prime)
    distinct               π^j π̄^l = π^{j'} π̄^{l'}, j + l = j' + l' ⇒ (j, l) = (j', l')     (Euclid's lemma)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.HeckeMult
import lang.ThetaCoef

namespace HeckePrime

open FTAChain DetChain LibInt GaussEuclid GaussCoprime GaussInt GaussFactor GaussUnique HeckeMult YMChain FiniteRecChain Frobenius LibGcd

variable (P : Nat)

theorem toGI_pow (x : Int × Int) : ∀ j, toGI (pw (Cyclo4.cyc4 LibInt.intR) x j) = gpow (toGI x) j
  | 0 => rfl
  | j + 1 => by
    show toGI ((Cyclo4.cyc4 LibInt.intR).mul (pw (Cyclo4.cyc4 LibInt.intR) x j) x) = gmul (gpow (toGI x) j) (toGI x)
    rw [toGI_mul, toGI_pow x j]

theorem odd_a (a b : Int) (hπ : Prim (a, b)) : ∃ w c, a = 2 * w + 1 ∧ b = 2 * c := by
  match hπ with
  | ⟨⟨c, hc⟩, ⟨e, he⟩⟩ =>
    have hb : b = 2 * c := hc
    have ha : a + b - 1 = 4 * e := he
    exact ⟨2 * e - c, c, by rw [show a = (a + b - 1) - b + 1 by lib_int, ha, hb]; lib_int, hb⟩

theorem gn_pos_ne (z : Int × Int) (N : Nat) (h : gn z = Int.ofNat (N + 1)) : gn z ≠ 0 := by
  rw [h]; exact fun e => Nat.noConfusion (Int.ofNat.inj e)

theorem nrm_of (z : Int × Int) (N : Nat) (h : gn z = Int.ofNat N) : nrm z = N := Int.ofNat.inj (by rw [nrm_cast]; exact h)

/-- **π and π̄ are coprime.** -/
theorem coprime_pi (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (a b : Int) (hab : a * a + b * b = Int.ofNat (P + 1 + 1)) (hπ : Prim (a, b)) : ∃ X Y, (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul X (a, b)) ((Cyclo4.cyc4 LibInt.intR).mul Y (a, -b)) = (Cyclo4.cyc4 LibInt.intR).one := by
  have gπ : gn (a, b) = Int.ofNat (P + 1 + 1) := hab
  have gπb : gn (a, -b) = Int.ofNat (P + 1 + 1) := by show a * a + -b * -b = _; rw [← hab]; lib_int
  match gcd_exists _ (a, b) (a, -b) (Nat.le_refl _) with
  | ⟨d, x, y, hd, ⟨c, hc⟩, ⟨c', hc'⟩⟩ =>
    have hdc : nrm (a, b) = nrm d * nrm c := Int.ofNat.inj (by
      show Int.ofNat (nrm (a, b)) = Int.ofNat (nrm d) * Int.ofNat (nrm c)
      rw [nrm_cast, nrm_cast, nrm_cast, ← GaussEuclid.norm_mul, ← hc])
    have hdc' : nrm (a, -b) = nrm d * nrm c' := Int.ofNat.inj (by
      show Int.ofNat (nrm (a, -b)) = Int.ofNat (nrm d) * Int.ofNat (nrm c')
      rw [nrm_cast, nrm_cast, nrm_cast, ← GaussEuclid.norm_mul, ← hc'])
    rw [nrm_of _ _ gπ] at hdc
    rw [nrm_of _ _ gπb] at hdc'
    cases hp (nrm d) ⟨nrm c, by rw [hdc]; lib_nat⟩ with
    | inl h1 =>
      have gd : gn d = 1 := by rw [← nrm_cast, h1]; rfl
      refine ⟨(Cyclo4.cyc4 LibInt.intR).mul (cj d) x, (Cyclo4.cyc4 LibInt.intR).mul (cj d) y, ?_⟩
      rw [show (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (cj d) x) (a, b)) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (cj d) y) (a, -b)) = (Cyclo4.cyc4 LibInt.intR).mul (cj d) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul x (a, b)) ((Cyclo4.cyc4 LibInt.intR).mul y (a, -b))) by lib_ring,
        ← hd, (Cyclo4.cyc4 LibInt.intR).mul_comm, mul_cj, gd]; rfl
    | inr h1 =>
      exfalso
      have nc : nrm c = 1 := by
        have : (P + 1 + 1) * nrm c = (P + 1 + 1) * 1 := by
          calc (P + 1 + 1) * nrm c = nrm d * nrm c := by rw [h1]
            _ = P + 1 + 1 := hdc.symm
            _ = (P + 1 + 1) * 1 := by lib_nat
        exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) this
      have nc' : nrm c' = 1 := by
        have : (P + 1 + 1) * nrm c' = (P + 1 + 1) * 1 := by
          calc (P + 1 + 1) * nrm c' = nrm d * nrm c' := by rw [h1]
            _ = P + 1 + 1 := hdc'.symm
            _ = (P + 1 + 1) * 1 := by lib_nat
        exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) this
      have gc : gn c = 1 := by rw [← nrm_cast, nc]; rfl
      have gc' : gn c' = 1 := by rw [← nrm_cast, nc']; rfl
      -- π̄ = π · u with u = c' c̄
      have hu : (a, -b) = (Cyclo4.cyc4 LibInt.intR).mul (a, b) ((Cyclo4.cyc4 LibInt.intR).mul c' (cj c)) := by
        have e1 : (Cyclo4.cyc4 LibInt.intR).mul c (cj c) = (Cyclo4.cyc4 LibInt.intR).one := by rw [mul_cj, gc]; rfl
        rw [hc', hc, show (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul d c) ((Cyclo4.cyc4 LibInt.intR).mul c' (cj c)) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul d c') ((Cyclo4.cyc4 LibInt.intR).mul c (cj c)) by lib_ring, e1]; lib_ring
      have hU : IsUnit (toGI ((Cyclo4.cyc4 LibInt.intR).mul c' (cj c))) := norm_one _ (by
        show gn ((Cyclo4.cyc4 LibInt.intR).mul c' (cj c)) = 1; rw [GaussEuclid.norm_mul, gn_cj, gc, gc']; rfl)
      match odd_a a b hπ with
      | ⟨w, cc, ha, hb⟩ =>
      generalize (Cyclo4.cyc4 LibInt.intR).mul c' (cj c) = u at hu hU
      have hu1 := congrArg Prod.fst hu
      have hu2 := congrArg Prod.snd hu
      rcases hU with h | h | h | h
      · have e := toGI_inj u (1, 0) h
        rw [e] at hu2
        change -b = a * 0 + b * 1 at hu2
        have hb0 : b = 0 := QChar.int_two_zero b (by rw [show b + b = b - -b by lib_int, hu2]; lib_int)
        rw [hb0] at hab
        have hsq : Int.natAbs a * Int.natAbs a = P + 1 + 1 := Int.ofNat.inj (by rw [nas, ← hab]; lib_int)
        cases hp (Int.natAbs a) ⟨Int.natAbs a, hsq.symm⟩ with
        | inl h1' => rw [h1'] at hsq; exact Nat.noConfusion (Nat.succ.inj hsq).symm
        | inr h1' =>
          rw [h1'] at hsq
          have : (P + 1 + 1) * (P + 1 + 1) = (P + 1 + 1) * 1 := hsq.trans (Nat.mul_one _).symm
          have := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) this
          exact Nat.noConfusion (Nat.succ.inj this)
      · have e := toGI_inj u (-1, 0) h
        rw [e] at hu1
        change a = a * -1 + -(b * 0) at hu1
        have h0 : a = 0 := QChar.int_two_zero a (by rw [show a + a = a - (a * -1 + -(b * 0)) by lib_int, ← hu1]; lib_int)
        rw [ha] at h0
        exact int_odd_ne_even w 0 (h0.trans rfl)
      · have e := toGI_inj u (0, 1) h
        rw [e] at hu1
        change a = a * 0 + -(b * 1) at hu1
        rw [ha, hb] at hu1
        exact int_odd_ne_even w (-cc) (by rw [hu1]; lib_int)
      · have e := toGI_inj u (0, -1) h
        rw [e] at hu1
        change a = a * 0 + -(b * -1) at hu1
        rw [ha, hb] at hu1
        exact int_odd_ne_even w cc (by rw [hu1]; lib_int)

theorem ipow_ne (x : Int) (hx : x ≠ 0) : ∀ j, ipow x j ≠ 0
  | 0 => fun e => Nat.noConfusion (Int.ofNat.inj (show Int.ofNat 1 = Int.ofNat 0 from e))
  | j + 1 => fun e => by
    cases int_noZD _ _ e with
    | inl h => exact ipow_ne x hx j h
    | inr h => exact hx h

theorem gn_pw (z : Int × Int) (j : Nat) : gn (pw (Cyclo4.cyc4 LibInt.intR) z j) = ipow (gn z) j := by
  rw [← gnorm_toGI, toGI_pow]; exact ThetaCoef.gnorm_pow _ j

theorem cancel_pw (z : Int × Int) (hz : gn z = Int.ofNat (P + 1 + 1)) (j : Nat) (A B : Int × Int)
    (h : (Cyclo4.cyc4 LibInt.intR).mul (pw (Cyclo4.cyc4 LibInt.intR) z j) A = (Cyclo4.cyc4 LibInt.intR).mul (pw (Cyclo4.cyc4 LibInt.intR) z j) B) : A = B := by
  have h0 : (Cyclo4.cyc4 LibInt.intR).mul (pw (Cyclo4.cyc4 LibInt.intR) z j) ((Cyclo4.cyc4 LibInt.intR).add A ((Cyclo4.cyc4 LibInt.intR).neg B)) = (Cyclo4.cyc4 LibInt.intR).zero := by
    rw [show (Cyclo4.cyc4 LibInt.intR).mul (pw (Cyclo4.cyc4 LibInt.intR) z j) ((Cyclo4.cyc4 LibInt.intR).add A ((Cyclo4.cyc4 LibInt.intR).neg B)) = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (pw (Cyclo4.cyc4 LibInt.intR) z j) A) ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).mul (pw (Cyclo4.cyc4 LibInt.intR) z j) B)) by lib_ring, h]; lib_ring
  have := cancel_left _ _ (by rw [gn_pw, hz]; exact ipow_ne _ (fun e => Nat.noConfusion (Int.ofNat.inj e)) j) h0
  calc A = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add A ((Cyclo4.cyc4 LibInt.intR).neg B)) B := by lib_ring
    _ = B := by rw [this]; lib_ring

theorem no_div (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (a b : Int) (hab : a * a + b * b = Int.ofNat (P + 1 + 1)) (hπ : Prim (a, b)) : ∀ d, Dvd' (a, b) (pw (Cyclo4.cyc4 LibInt.intR) (a, -b) d) → False
  | 0, ⟨c, hc⟩ => by
    have : gn (a, b) * gn c = 1 := by rw [← GaussEuclid.norm_mul, ← hc]; rfl
    cases int_unit _ _ this with
    | inl h => rw [show gn (a, b) = Int.ofNat (P + 1 + 1) from hab] at h; exact Nat.noConfusion (Nat.succ.inj (Int.ofNat.inj h))
    | inr h => rw [show gn (a, b) = Int.ofNat (P + 1 + 1) from hab] at h; exact Int.noConfusion h
  | d + 1, h => by
    match coprime_pi P hp a b hab hπ with
    | ⟨X, Y, hXY⟩ =>
      exact no_div hp a b hab hπ d (euclid_coprime _ _ _ X Y hXY (by
        rw [show (Cyclo4.cyc4 LibInt.intR).mul (a, -b) (pw (Cyclo4.cyc4 LibInt.intR) (a, -b) d) = pw (Cyclo4.cyc4 LibInt.intR) (a, -b) (d + 1) by show _ = (Cyclo4.cyc4 LibInt.intR).mul _ _; lib_ring]; exact h))

theorem distinct_lt (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (a b : Int) (hab : a * a + b * b = Int.ofNat (P + 1 + 1)) (hπ : Prim (a, b)) (j d l' : Nat) (h : (Cyclo4.cyc4 LibInt.intR).mul (pw (Cyclo4.cyc4 LibInt.intR) (a, b) j) (pw (Cyclo4.cyc4 LibInt.intR) (a, -b) (l' + 1 + d)) = (Cyclo4.cyc4 LibInt.intR).mul (pw (Cyclo4.cyc4 LibInt.intR) (a, b) (j + 1 + d)) (pw (Cyclo4.cyc4 LibInt.in  -- (+30 chars; full line in the Zenodo source)
  have gπb : gn (a, -b) = Int.ofNat (P + 1 + 1) := by show a * a + -b * -b = _; rw [← hab]; lib_int
  rw [show l' + 1 + d = l' + (1 + d) by lib_nat, HasseCM.pw_add' (Cyclo4.cyc4 LibInt.intR) (a, -b) l' (1 + d),
    show j + 1 + d = j + (1 + d) by lib_nat, HasseCM.pw_add' (Cyclo4.cyc4 LibInt.intR) (a, b) j (1 + d)] at h
  have h' : (Cyclo4.cyc4 LibInt.intR).mul (pw (Cyclo4.cyc4 LibInt.intR) (a, b) j) ((Cyclo4.cyc4 LibInt.intR).mul (pw (Cyclo4.cyc4 LibInt.intR) (a, -b) l') (pw (Cyclo4.cyc4 LibInt.intR) (a, -b) (1 + d)))
      = (Cyclo4.cyc4 LibInt.intR).mul (pw (Cyclo4.cyc4 LibInt.intR) (a, b) j) ((Cyclo4.cyc4 LibInt.intR).mul (pw (Cyclo4.cyc4 LibInt.intR) (a, -b) l') (pw (Cyclo4.cyc4 LibInt.intR) (a, b) (1 + d))) := by
    calc _ = (Cyclo4.cyc4 LibInt.intR).mul (pw (Cyclo4.cyc4 LibInt.intR) (a, b) j) ((Cyclo4.cyc4 LibInt.intR).mul (pw (Cyclo4.cyc4 LibInt.intR) (a, -b) l') (pw (Cyclo4.cyc4 LibInt.intR) (a, -b) (1 + d))) := rfl
      _ = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (pw (Cyclo4.cyc4 LibInt.intR) (a, b) j) (pw (Cyclo4.cyc4 LibInt.intR) (a, b) (1 + d))) (pw (Cyclo4.cyc4 LibInt.intR) (a, -b) l') := h
      _ = _ := by lib_ring
  have h1 := cancel_pw P (a, b) hab j _ _ h'
  have h2 := cancel_pw P (a, -b) gπb l' _ _ h1
  apply no_div P hp a b hab hπ (1 + d)
  refine ⟨pw (Cyclo4.cyc4 LibInt.intR) (a, b) d, ?_⟩
  rw [h2, show 1 + d = d + 1 by lib_nat]; show (Cyclo4.cyc4 LibInt.intR).mul (pw (Cyclo4.cyc4 LibInt.intR) (a, b) d) (a, b) = _; lib_ring

/-- **π^j π̄^l are distinct.** -/
theorem distinct (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (a b : Int) (hab : a * a + b * b = Int.ofNat (P + 1 + 1)) (hπ : Prim (a, b)) (j l j' l' : Nat) (hs : j + l = j' + l')
    (he : (Cyclo4.cyc4 LibInt.intR).mul (pw (Cyclo4.cyc4 LibInt.intR) (a, b) j) (pw (Cyclo4.cyc4 LibInt.intR) (a, -b) l) = (Cyclo4.cyc4 LibInt.intR).mul (pw (Cyclo4.cyc4 LibInt.intR) (a, b) j') (pw (Cyclo4.cyc4 LibInt.intR) (a, -b) l')) : j = j' ∧ l = l' := by
  cases Nat.lt_or_ge j j' with
  | inl hlt =>
    exfalso
    match le_dest hlt with
    | ⟨d, hd⟩ =>
      have hl : l = l' + 1 + d := LibInt.nadd_right_cancel j (by rw [show l + j = j + l by lib_nat, hs, hd]; lib_nat)
      rw [hd, hl] at he
      exact distinct_lt P hp a b hab hπ j d l' he
  | inr hge =>
    cases Nat.lt_or_ge j' j with
    | inl hlt =>
      exfalso
      match le_dest hlt with
      | ⟨d, hd⟩ =>
        have hl : l' = l + 1 + d := LibInt.nadd_right_cancel j' (by rw [show l' + j' = j' + l' by lib_nat, ← hs, hd]; lib_nat)
        rw [hd, hl] at he
        exact distinct_lt P hp a b hab hπ j' d l he.symm
    | inr hle =>
      have e : j = j' := Nat.le_antisymm hle hge
      exact ⟨e, LibInt.nadd_right_cancel j (by rw [show l + j = j + l by lib_nat, hs, e]; lib_nat)⟩

def Wl : Nat → List (Nat × Nat)
  | 0 => [(0, 0)]
  | k + 1 => (Wl k).map (fun x => (x.1 + 1, x.2)) ++ [(0, k + 1)]

def allk (k : Nat) : List (Nat × Nat) → Prop
  | [] => True
  | x :: xs => x.1 + x.2 = k ∧ allk k xs

theorem allk_append (k : Nat) : ∀ l1 l2 : List (Nat × Nat), allk k l1 → allk k l2 → allk k (l1 ++ l2)
  | [], _, _, h2 => h2
  | _ :: xs, l2, ⟨hx, h1⟩, h2 => ⟨hx, allk_append k xs l2 h1 h2⟩

theorem allk_map (k : Nat) : ∀ l : List (Nat × Nat), allk k l → allk (k + 1) (l.map (fun x => (x.1 + 1, x.2)))
  | [], _ => trivial
  | x :: xs, ⟨hx, h⟩ => ⟨by show x.1 + 1 + x.2 = k + 1; rw [← hx]; lib_nat, allk_map k xs h⟩

theorem allk_Wl : ∀ k, allk k (Wl k)
  | 0 => ⟨rfl, trivial⟩
  | k + 1 => allk_append (k + 1) _ _ (allk_map k _ (allk_Wl k)) ⟨by show 0 + (k + 1) = k + 1; lib_nat, trivial⟩

theorem sumK_ext_all {R : Type} (Rg : CRing R) (k : Nat) (f g : Nat × Nat → R) (h : ∀ x : Nat × Nat, x.1 + x.2 = k → f x = g x) :
    ∀ l : List (Nat × Nat), allk k l → sumK Rg f l = sumK Rg g l
  | [], _ => rfl
  | x :: xs, ⟨hx, hl⟩ => by
    show Rg.add (f x) (sumK Rg f xs) = Rg.add (g x) (sumK Rg g xs)
    rw [h x hx, sumK_ext_all Rg k f g h xs hl]

theorem zsumR {R : Type} (Rg : CRing R) {A : Type} : ∀ l : List A, sumK Rg (fun _ => Rg.zero) l = Rg.zero
  | [] => rfl
  | _ :: l => by show Rg.add Rg.zero _ = _; rw [zsumR Rg l]; exact Rg.zero_add _

theorem count_Wl {R : Type} (Rg : CRing R) : ∀ k (j l : Nat), j + l = k → sumK Rg (fun x => delta Rg x (j, l)) (Wl k) = Rg.one
  | 0, j, l, h => by
    have hj : j = 0 := Nat.le_antisymm (Nat.le_trans (Nat.le_add_right j l) (Nat.le_of_eq h)) (Nat.zero_le _)
    have hl : l = 0 := Nat.le_antisymm (Nat.le_trans (Nat.le_add_left l j) (Nat.le_of_eq h)) (Nat.zero_le _)
    rw [hj, hl]
    show Rg.add (delta Rg (0, 0) (0, 0)) Rg.zero = Rg.one
    unfold delta; rw [ite_pos' _ _ rfl]; exact add_zero' Rg _
  | k + 1, j, l, h => by
    show sumK Rg (fun x => delta Rg x (j, l)) ((Wl k).map (fun x => (x.1 + 1, x.2)) ++ [(0, k + 1)]) = Rg.one
    rw [sumK_append, sumK_map]
    show Rg.add _ (Rg.add (delta Rg (0, k + 1) (j, l)) Rg.zero) = Rg.one
    match j with
    | 0 =>
      have hl : l = k + 1 := by rw [← h]; lib_nat
      rw [hl]
      have e : sumK Rg (fun x => delta Rg ((x.1 + 1, x.2) : Nat × Nat) (0, k + 1)) (Wl k) = Rg.zero := by
        rw [sumK_ext Rg _ (fun _ => Rg.zero) (fun x => by
          unfold delta; rw [ite_neg' _ _ (fun e => Nat.noConfusion (congrArg Prod.fst e))])]
        exact zsumR Rg _
      rw [e]; unfold delta; rw [ite_pos' _ _ rfl]; lib_ring
    | j0 + 1 =>
      have e : sumK Rg (fun x => delta Rg ((x.1 + 1, x.2) : Nat × Nat) (j0 + 1, l)) (Wl k) = sumK Rg (fun x => delta Rg x (j0, l)) (Wl k) :=
        sumK_ext Rg _ _ (fun x => by
          unfold delta
          by_cases hx : x = (j0, l)
          · rw [ite_pos' _ _ (show ((x.1 + 1, x.2) : Nat × Nat) = (j0 + 1, l) by rw [hx]), ite_pos' _ _ hx]
          · rw [ite_neg' _ _ (fun (e : ((x.1 + 1, x.2) : Nat × Nat) = (j0 + 1, l)) => hx (Prod.ext (show x.1 = j0 from Nat.succ.inj (congrArg Prod.fst e)) (show x.2 = l from congrArg Prod.snd e))), ite_neg' _ _ hx]) _
      rw [e, count_Wl Rg k j0 l (Nat.succ.inj (show j0 + l + 1 = k + 1 by rw [← h]; lib_nat))]
      unfold delta; rw [ite_neg' _ _ (fun e => Nat.noConfusion (congrArg Prod.fst e))]; lib_ring

def w (a b : Int) (x : Nat × Nat) : Int × Int := (Cyclo4.cyc4 LibInt.intR).mul (pw (Cyclo4.cyc4 LibInt.intR) (a, b) x.1) (pw (Cyclo4.cyc4 LibInt.intR) (a, -b) x.2)
def S (a b : Int) (k : Nat) : Int × Int := sumK (Cyclo4.cyc4 LibInt.intR) (w a b) (Wl k)

theorem S_succ (a b : Int) (k : Nat) : S a b (k + 1) = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (a, b) (S a b k)) (pw (Cyclo4.cyc4 LibInt.intR) (a, -b) (k + 1)) := by
  show sumK (Cyclo4.cyc4 LibInt.intR) (w a b) ((Wl k).map (fun x => (x.1 + 1, x.2)) ++ [(0, k + 1)]) = _
  rw [sumK_append, sumK_map]
  unfold S; rw [mul_sumK]
  have e : sumK (Cyclo4.cyc4 LibInt.intR) (fun x => w a b ((x.1 + 1, x.2) : Nat × Nat)) (Wl k) = sumK (Cyclo4.cyc4 LibInt.intR) (fun x => (Cyclo4.cyc4 LibInt.intR).mul (a, b) (w a b x)) (Wl k) :=
    sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun x => by
      show (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (pw (Cyclo4.cyc4 LibInt.intR) (a, b) x.1) (a, b)) (pw (Cyclo4.cyc4 LibInt.intR) (a, -b) x.2) = (Cyclo4.cyc4 LibInt.intR).mul (a, b) ((Cyclo4.cyc4 LibInt.intR).mul (pw (Cyclo4.cyc4 LibInt.intR) (a, b) x.1) (pw (Cyclo4.cyc4 LibInt.intR) (a, -b) x.2))
      lib_ring) _
  rw [e]
  show (Cyclo4.cyc4 LibInt.intR).add _ ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (Cyclo4.cyc4 LibInt.intR).one (pw (Cyclo4.cyc4 LibInt.intR) (a, -b) (k + 1))) (Cyclo4.cyc4 LibInt.intR).zero) = _
  lib_ring

/-- **Hecke recursion for S**: S_{k+2} = (π + π̄) S_{k+1} − ππ̄ S_k. -/
theorem S_rec (a b : Int) (k : Nat) :
    S a b (k + 2) = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add (a, b) (a, -b)) (S a b (k + 1))) ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (a, b) (a, -b)) (S a b k))) := by
  have h1 := S_succ a b (k + 1)
  have h2 := S_succ a b k
  have h3 : pw (Cyclo4.cyc4 LibInt.intR) (a, -b) (k + 1 + 1) = (Cyclo4.cyc4 LibInt.intR).mul (a, -b) (pw (Cyclo4.cyc4 LibInt.intR) (a, -b) (k + 1)) := by show (Cyclo4.cyc4 LibInt.intR).mul _ _ = _; lib_ring
  have h4 : pw (Cyclo4.cyc4 LibInt.intR) (a, -b) (k + 1) = (Cyclo4.cyc4 LibInt.intR).add (S a b (k + 1)) ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).mul (a, b) (S a b k))) := by rw [h2]; lib_ring
  rw [show k + 2 = k + 1 + 1 from rfl, h1, h3, h4]; lib_ring

theorem ipow_nat (p : Nat) : ∀ k, Int.ofNat (p ^ k) = ipow (Int.ofNat p) k
  | 0 => rfl
  | k + 1 => by show Int.ofNat (p ^ k * p) = ipow (Int.ofNat p) k * Int.ofNat p; rw [← ipow_nat p k]; rfl

theorem w_PD (_hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (a b : Int) (hab : a * a + b * b = Int.ofNat (P + 1 + 1)) (hπ : Prim (a, b)) (k : Nat) (x : Nat × Nat) (hx : x.1 + x.2 = k) : PD ((P + 1 + 1) ^ k) (w a b x) := by
  have gπb : gn (a, -b) = Int.ofNat (P + 1 + 1) := by show a * a + -b * -b = _; rw [← hab]; lib_int
  refine ⟨?_, ?_⟩
  · unfold w; rw [GaussEuclid.norm_mul, gn_pw, gn_pw, show gn (a, b) = Int.ofNat (P + 1 + 1) from hab, gπb, ← FinPow2.ipow_add, hx, ipow_nat]
  · unfold w; apply prim_mulP
    · unfold Prim; rw [toGI_pow]; exact GaussCube.prim_pow _ hπ _
    · unfold Prim; rw [toGI_pow]; exact GaussCube.prim_pow _ (prim_conj _ hπ) _

/-- **c_{p^k} = Σ_{j+l=k} π^j π̄^l.** -/
theorem c_pk (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (a b : Int) (hab : a * a + b * b = Int.ofNat (P + 1 + 1)) (hπ : Prim (a, b)) (k : Nat) : c ((P + 1 + 1) ^ k) = S a b k := by
  have hI : ∀ z, I ((P + 1 + 1) ^ k) z = sumK (Cyclo4.cyc4 LibInt.intR) (fun x => delta (Cyclo4.cyc4 LibInt.intR) z (w a b x)) (Wl k) := fun z => by
    by_cases hz : PD ((P + 1 + 1) ^ k) z
    · rw [I_pos _ _ hz]
      have hN : gnorm (toGI z) = ipow (Int.ofNat (P + 1 + 1)) k := by rw [gnorm_toGI, hz.1, ipow_nat]
      match ThetaCoef.primary_only P hp a b hab hπ k (toGI z) hz.2 hN with
      | ⟨j, l, hjl, hx⟩ =>
        have hz' : z = w a b (j, l) := toGI_inj _ _ (by unfold w; rw [toGI_mul, toGI_pow, toGI_pow]; exact hx)
        rw [sumK_ext_all (Cyclo4.cyc4 LibInt.intR) k _ (fun x => delta (Cyclo4.cyc4 LibInt.intR) x (j, l)) (fun x hxk => by
          unfold delta
          by_cases hxe : x = (j, l)
          · rw [ite_pos' _ _ hxe, ite_pos' _ _ (by rw [hz', hxe])]
          · rw [ite_neg' _ _ hxe, ite_neg' _ _ (fun e => hxe (by
              have d := distinct P hp a b hab hπ j l x.1 x.2 (hjl.trans hxk.symm) (hz'.symm.trans e)
              exact Prod.ext d.1.symm d.2.symm))]) _ (allk_Wl k),
          count_Wl (Cyclo4.cyc4 LibInt.intR) k j l hjl]
    · rw [I_neg _ _ hz]
      rw [sumK_ext_all (Cyclo4.cyc4 LibInt.intR) k _ (fun _ => (Cyclo4.cyc4 LibInt.intR).zero) (fun x hxk => by
        unfold delta; rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact w_PD P hp a b hab hπ k x hxk))]) _ (allk_Wl k)]
      exact (zsumR (Cyclo4.cyc4 LibInt.intR) _).symm
  unfold c
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun z => by rw [hI z, sumK_mul])]
  rw [sumK_swap]
  unfold S
  refine sumK_ext_all (Cyclo4.cyc4 LibInt.intR) k _ _ (fun x hxk => ?_) _ (allk_Wl k)
  have hb := abs_le (w a b x) _ (w_PD P hp a b hab hπ k x hxk).1
  exact pick_box (Cyclo4.cyc4 LibInt.intR) (fun z => z) (w a b x) _ hb.1 hb.2

/-- **The Hecke recursion for c at a split prime**: c_{p^{k+2}} = 2a c_{p^{k+1}} − p c_{p^k}, c_1 = 1, c_p = 2a. -/
theorem hecke_rec (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (a b : Int) (hab : a * a + b * b = Int.ofNat (P + 1 + 1)) (hπ : Prim (a, b)) (k : Nat) :
    c ((P + 1 + 1) ^ (k + 2)) = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (2 * a, 0) (c ((P + 1 + 1) ^ (k + 1)))) ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).mul (Int.ofNat (P + 1 + 1), 0) (c ((P + 1 + 1) ^ k)))) := by
  rw [c_pk P hp a b hab hπ, c_pk P hp a b hab hπ, c_pk P hp a b hab hπ, S_rec]
  have e1 : (Cyclo4.cyc4 LibInt.intR).add (a, b) (a, -b) = (2 * a, 0) := Prod.ext (by show a + a = 2 * a; lib_int) (by show b + -b = 0; lib_int)
  have e2 : (Cyclo4.cyc4 LibInt.intR).mul (a, b) (a, -b) = (Int.ofNat (P + 1 + 1), 0) := Prod.ext (by show a * a + -(b * -b) = Int.ofNat (P + 1 + 1); rw [← hab]; lib_int) (by show a * -b + b * a = 0; lib_int)
  rw [e1, e2]

theorem c_p (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (a b : Int) (hab : a * a + b * b = Int.ofNat (P + 1 + 1)) (hπ : Prim (a, b)) : c (P + 1 + 1) = (2 * a, 0) := by
  have := c_pk P hp a b hab hπ 1
  rw [show (P + 1 + 1) ^ 1 = P + 1 + 1 from Nat.one_mul _] at this
  rw [this, S_succ]
  have hS0 : S a b 0 = (Cyclo4.cyc4 LibInt.intR).one := by show (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (Cyclo4.cyc4 LibInt.intR).one (Cyclo4.cyc4 LibInt.intR).one) (Cyclo4.cyc4 LibInt.intR).zero = _; lib_ring
  rw [hS0, show pw (Cyclo4.cyc4 LibInt.intR) (a, -b) (0 + 1) = (a, -b) by show (Cyclo4.cyc4 LibInt.intR).mul (Cyclo4.cyc4 LibInt.intR).one _ = _; lib_ring,
    show (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (a, b) (Cyclo4.cyc4 LibInt.intR).one) (a, -b) = (Cyclo4.cyc4 LibInt.intR).add (a, b) (a, -b) by lib_ring]
  exact Prod.ext (by show a + a = 2 * a; lib_int) (by show b + -b = 0; lib_int)

end HeckePrime

#print axioms HeckePrime.toGI_pow
#print axioms HeckePrime.odd_a
#print axioms HeckePrime.coprime_pi
#print axioms HeckePrime.ipow_ne
#print axioms HeckePrime.gn_pw
#print axioms HeckePrime.cancel_pw
#print axioms HeckePrime.no_div
#print axioms HeckePrime.distinct_lt
#print axioms HeckePrime.distinct
#print axioms HeckePrime.allk_Wl
#print axioms HeckePrime.count_Wl
#print axioms HeckePrime.S_succ
#print axioms HeckePrime.S_rec
#print axioms HeckePrime.ipow_nat
#print axioms HeckePrime.w_PD
#print axioms HeckePrime.c_pk
#print axioms HeckePrime.hecke_rec
#print axioms HeckePrime.c_p

lang/HeckePrime11.lean

lean_all/lang/HeckePrime11.lean

/-
  lang/HeckePrime11 — T48/T50, cycle 653: **121b's Hecke character at split prime powers**: π ∈ H11 of prime norm p, W multiplicative:
  c_W(p^k) = Σ_{j+l=k} W(π)^j W(π̄)^l and the Hecke recursion.  (Port of HeckePrime3.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Prime11
import lang.HeckePrimeW

namespace HeckePrime11

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius Euc11 Prim11 Uniq11 Prime11 HeckeMult11

variable (P : Nat)

theorem pow_ne0 : ∀ j, (P + 1 + 1) ^ j ≠ 0
  | 0 => fun e => Nat.noConfusion e
  | j + 1 => fun e => by
    cases nat_mul_zero _ _ e with
    | inl h => exact pow_ne0 j h
    | inr h => exact Nat.noConfusion h

theorem nrm_pw (z : Int × Int) : ∀ j, nrm (pw (Ring11.ring11 LibInt.intR) z j) = nrm z ^ j
  | 0 => rfl
  | j + 1 => by
    show nrm ((Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) z j) z) = nrm z ^ j * nrm z
    rw [nrm_mul, nrm_pw z j]

theorem npow_add (x : Nat) (a : Nat) : ∀ b, x ^ a * x ^ b = x ^ (a + b)
  | 0 => Nat.mul_one _
  | b + 1 => by
    show x ^ a * (x ^ b * x) = x ^ (a + b) * x
    rw [← npow_add x a b]; lib_nat

theorem en_ne (z : Int × Int) (N : Nat) (h : nrm z = N) (hN : N ≠ 0) : en z ≠ 0 := fun e => by
  apply hN; rw [← h]; unfold nrm; rw [e]; rfl

theorem cancel_pw3 (z : Int × Int) (hz : nrm z = P + 1 + 1) (j : Nat) (A B : Int × Int)
    (h : (Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) z j) A = (Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) z j) B) : A = B := by
  have h0 : (Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) z j) ((Ring11.ring11 LibInt.intR).add A ((Ring11.ring11 LibInt.intR).neg B)) = (Ring11.ring11 LibInt.intR).zero := by
    rw [show (Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) z j) ((Ring11.ring11 LibInt.intR).add A ((Ring11.ring11 LibInt.intR).neg B)) = (Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) z j) A) ((Ring11.ring11 LibInt.intR).neg ((Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) z j) B)) by lib_ring, h]; lib_ring
  have := Cop11.cancel_left _ _ (en_ne _ _ (by rw [nrm_pw, hz]) (pow_ne0 P j)) h0
  calc A = (Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).add A ((Ring11.ring11 LibInt.intR).neg B)) B := by lib_ring
    _ = B := by rw [this]; lib_ring

theorem no_assoc3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim11 π)
    (h : Dvd' π (cj π)) : False := by
  obtain ⟨c, hc⟩ := h
  have nc : nrm c = 1 := by
    have e : (P + 1 + 1) * nrm c = (P + 1 + 1) * 1 := by rw [← hπ, ← nrm_mul, ← hc, nrm_cj]; lib_nat
    exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) e
  have c1 := unit_one11 c π (nrm_one_eq c nc) pπ (by rw [(Ring11.ring11 LibInt.intR).mul_comm, ← hc]; exact prim11_cj π pπ)
  rw [c1] at hc
  have hc2 : cj π = π := by rw [hc]; lib_ring
  obtain ⟨a, b⟩ := π
  have hb2 : -b = b := congrArg Prod.snd hc2
  have hb0 : b = 0 := QChar.int_two_zero b (by rw [show b + b = b + -(-b) by lib_int, hb2]; lib_int)
  have hen : en (a, b) = Int.ofNat (P + 1 + 1) := by rw [← nrm_cast, hπ]
  have hsq : Int.natAbs a * Int.natAbs a = P + 1 + 1 := Int.ofNat.inj (by
    rw [GaussEuclid.nas, ← hen]; show a * a = a * a + a * b + 3 * (b * b); rw [hb0]; lib_int)
  cases hp (Int.natAbs a) ⟨Int.natAbs a, hsq.symm⟩ with
  | inl h1' => rw [h1'] at hsq; exact Nat.noConfusion (Nat.succ.inj hsq).symm
  | inr h1' =>
    rw [h1'] at hsq
    have : (P + 1 + 1) * (P + 1 + 1) = (P + 1 + 1) * 1 := hsq.trans (Nat.mul_one _).symm
    have := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) this
    exact Nat.noConfusion (Nat.succ.inj this)

theorem no_div3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim11 π) :
    ∀ d, Dvd' π (pw (Ring11.ring11 LibInt.intR) (cj π) d) → False
  | 0, ⟨c, hc⟩ => by
    have : (P + 1 + 1) * nrm c = 1 := by rw [← hπ, ← nrm_mul, ← hc]; rfl
    cases h' : nrm c with
    | zero => rw [h'] at this; exact Nat.noConfusion this
    | succ b => rw [h', show (P + 1 + 1) * (b + 1) = (P + 1 + 1) * b + P + 1 + 1 by lib_nat] at this; exact Nat.noConfusion (Nat.succ.inj this)
  | d + 1, h => by
    cases eprime_div P hp π hπ _ _ h with
    | inl h1 => exact no_div3 hp π hπ pπ d h1
    | inr h1 => exact no_assoc3 P hp π hπ pπ h1

theorem distinct_lt3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim11 π) (j d l' : Nat)
    (h : (Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) π j) (pw (Ring11.ring11 LibInt.intR) (cj π) (l' + 1 + d)) = (Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) π (j + 1 + d)) (pw (Ring11.ring11 LibInt.intR) (cj π) l')) : False := by
  rw [show l' + 1 + d = l' + (1 + d) by lib_nat, HasseCM.pw_add' (Ring11.ring11 LibInt.intR) (cj π) l' (1 + d),
    show j + 1 + d = j + (1 + d) by lib_nat, HasseCM.pw_add' (Ring11.ring11 LibInt.intR) π j (1 + d)] at h
  have h' : (Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) π j) ((Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) (cj π) l') (pw (Ring11.ring11 LibInt.intR) (cj π) (1 + d)))
      = (Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) π j) ((Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) (cj π) l') (pw (Ring11.ring11 LibInt.intR) π (1 + d))) := by
    calc _ = (Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) π j) (pw (Ring11.ring11 LibInt.intR) π (1 + d))) (pw (Ring11.ring11 LibInt.intR) (cj π) l') := h
      _ = _ := by lib_ring
  have h1 := cancel_pw3 P π hπ j _ _ h'
  have h2 := cancel_pw3 P (cj π) (by rw [nrm_cj, hπ]) l' _ _ h1
  apply no_div3 P hp π hπ pπ (1 + d)
  refine ⟨pw (Ring11.ring11 LibInt.intR) π d, ?_⟩
  rw [h2, show 1 + d = d + 1 by lib_nat]; show (Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) π d) π = _; lib_ring

/-- **π^j π̄^l are distinct.** -/
theorem distinct3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim11 π) (j l j' l' : Nat) (hs : j + l = j' + l')
    (he : (Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) π j) (pw (Ring11.ring11 LibInt.intR) (cj π) l) = (Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) π j') (pw (Ring11.ring11 LibInt.intR) (cj π) l')) : j = j' ∧ l = l' := by
  cases Nat.lt_or_ge j j' with
  | inl hlt =>
    exfalso
    match le_dest hlt with
    | ⟨d, hd⟩ =>
      have hl : l = l' + 1 + d := LibInt.nadd_right_cancel j (by rw [show l + j = j + l by lib_nat, hs, hd]; lib_nat)
      rw [hd, hl] at he
      exact distinct_lt3 P hp π hπ pπ j d l' he
  | inr hge =>
    cases Nat.lt_or_ge j' j with
    | inl hlt =>
      exfalso
      match le_dest hlt with
      | ⟨d, hd⟩ =>
        have hl : l' = l + 1 + d := LibInt.nadd_right_cancel j' (by rw [show l' + j' = j' + l' by lib_nat, ← hs, hd]; lib_nat)
        rw [hd, hl] at he
        exact distinct_lt3 P hp π hπ pπ j' d l he.symm
    | inr hle =>
      have e : j = j' := Nat.le_antisymm hle hge
      exact ⟨e, LibInt.nadd_right_cancel j (by rw [show l + j = j + l by lib_nat, hs, e]; lib_nat)⟩

def w3 (π : Int × Int) (x : Nat × Nat) : Int × Int := (Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) π x.1) (pw (Ring11.ring11 LibInt.intR) (cj π) x.2)

theorem w3_PD (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim11 π) (k : Nat) (x : Nat × Nat) (hx : x.1 + x.2 = k) :
    PD11 ((P + 1 + 1) ^ k) (w3 π x) := by
  refine ⟨?_, ?_⟩
  · rw [← nrm_cast]; unfold w3
    rw [nrm_mul, nrm_pw, nrm_pw, nrm_cj, hπ, npow_add, hx]
  · exact prim11_mul _ _ (prim11_pow π pπ _) (prim11_pow (cj π) (prim11_cj π pπ) _)

/-- **c_W(p^k) = Σ_{j+l=k} W(π^j π̄^l).** -/
theorem cw3_pk (W : Int × Int → Int × Int) (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int)
    (hπ : nrm π = P + 1 + 1) (pπ : Prim11 π) (k : Nat) :
    cw11 W ((P + 1 + 1) ^ k) = sumK (Ring11.ring11 LibInt.intR) (fun x => W (w3 π x)) (HeckePrime.Wl k) := by
  have hI : ∀ z, I11 ((P + 1 + 1) ^ k) z = sumK (Ring11.ring11 LibInt.intR) (fun x => delta (Ring11.ring11 LibInt.intR) z (w3 π x)) (HeckePrime.Wl k) := fun z => by
    by_cases hz : PD11 ((P + 1 + 1) ^ k) z
    · rw [I11_pos _ _ hz]
      have hN : nrm z = (P + 1 + 1) ^ k := by unfold nrm; rw [hz.1]; rfl
      match primary_only3 P hp π hπ pπ k z hN hz.2 with
      | ⟨j, l, hjl, hx⟩ =>
        have hz' : z = w3 π (j, l) := hx
        rw [HeckePrime.sumK_ext_all (Ring11.ring11 LibInt.intR) k _ (fun x => delta (Ring11.ring11 LibInt.intR) x (j, l)) (fun x hxk => by
          unfold delta
          by_cases hxe : x = (j, l)
          · rw [ite_pos' _ _ hxe, ite_pos' _ _ (by rw [hz', hxe])]
          · rw [ite_neg' _ _ hxe, ite_neg' _ _ (fun e => hxe (by
              have d := distinct3 P hp π hπ pπ j l x.1 x.2 (hjl.trans hxk.symm) (hz'.symm.trans e)
              exact Prod.ext d.1.symm d.2.symm))]) _ (HeckePrime.allk_Wl k),
          HeckePrime.count_Wl (Ring11.ring11 LibInt.intR) k j l hjl]
    · rw [I11_neg _ _ hz]
      rw [HeckePrime.sumK_ext_all (Ring11.ring11 LibInt.intR) k _ (fun _ => (Ring11.ring11 LibInt.intR).zero) (fun x hxk => by
        unfold delta; rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact w3_PD P π hπ pπ k x hxk))]) _ (HeckePrime.allk_Wl k)]
      exact (HeckePrime.zsumR (Ring11.ring11 LibInt.intR) _).symm
  unfold cw11
  rw [sumK_ext (Ring11.ring11 LibInt.intR) _ _ (fun z => by rw [hI z, sumK_mul])]
  rw [sumK_swap]
  refine HeckePrime.sumK_ext_all (Ring11.ring11 LibInt.intR) k _ _ (fun x hxk => ?_) _ (HeckePrime.allk_Wl k)
  have hb := abs_le11 (w3 π x) _ (w3_PD P π hπ pπ k x hxk).1
  exact HeckeMult.pick_box (Ring11.ring11 LibInt.intR) (fun z => W z) (w3 π x) _ hb.1 hb.2

theorem W_pow3 (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring11.ring11 LibInt.intR).mul α β) = (Ring11.ring11 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring11.ring11 LibInt.intR).one = (Ring11.ring11 LibInt.intR).one) (z : Int × Int) :
    ∀ j, W (pw (Ring11.ring11 LibInt.intR) z j) = pw (Ring11.ring11 LibInt.intR) (W z) j
  | 0 => hW1
  | j + 1 => by
    show W ((Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) z j) z) = (Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) (W z) j) (W z)
    rw [hW, W_pow3 W hW hW1 z j]

theorem cw3_pk' (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring11.ring11 LibInt.intR).mul α β) = (Ring11.ring11 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring11.ring11 LibInt.intR).one = (Ring11.ring11 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim11 π) (k : Nat) :
    cw11 W ((P + 1 + 1) ^ k) = HeckePrime.Tg (Ring11.ring11 LibInt.intR) (W π) (W (cj π)) k := by
  rw [cw3_pk P W hp π hπ pπ k]
  exact sumK_ext _ _ _ (fun x => by unfold w3; rw [hW, W_pow3 W hW hW1, W_pow3 W hW hW1]) _

/-- **The Hecke recursion for every multiplicative weight on ℤ[ω].** -/
theorem cw3_rec (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring11.ring11 LibInt.intR).mul α β) = (Ring11.ring11 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring11.ring11 LibInt.intR).one = (Ring11.ring11 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim11 π) (k : Nat) :
    cw11 W ((P + 1 + 1) ^ (k + 2)) = (Ring11.ring11 LibInt.intR).add
      ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).add (W π) (W (cj π))) (cw11 W ((P + 1 + 1) ^ (k + 1))))
      ((Ring11.ring11 LibInt.intR).neg ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul (W π) (W (cj π))) (cw11 W ((P + 1 + 1) ^ k)))) := by
  rw [cw3_pk' P W hW hW1 hp π hπ pπ, cw3_pk' P W hW hW1 hp π hπ pπ, cw3_pk' P W hW hW1 hp π hπ pπ, HeckePrime.Tg_rec]

end HeckePrime11

#print axioms HeckePrime11.pow_ne0
#print axioms HeckePrime11.nrm_pw
#print axioms HeckePrime11.npow_add
#print axioms HeckePrime11.en_ne
#print axioms HeckePrime11.cancel_pw3
#print axioms HeckePrime11.no_assoc3
#print axioms HeckePrime11.no_div3
#print axioms HeckePrime11.distinct_lt3
#print axioms HeckePrime11.distinct3
#print axioms HeckePrime11.w3_PD
#print axioms HeckePrime11.cw3_pk
#print axioms HeckePrime11.W_pow3
#print axioms HeckePrime11.cw3_pk'
#print axioms HeckePrime11.cw3_rec

lang/HeckePrime163.lean

lean_all/lang/HeckePrime163.lean

/-
  lang/HeckePrime163 — T48/T50, T48: **ℚ(√−163)'s Hecke character at split prime powers**: π ∈ H163 of prime norm p, W multiplicative:
  c_W(p^k) = Σ_{j+l=k} W(π)^j W(π̄)^l and the Hecke recursion.  (Port of HeckePrime3.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Prime163
import lang.HeckePrimeW

namespace HeckePrime163

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius Euc163 Prim163 Uniq163 Prime163 HeckeMult163

variable (P : Nat)

theorem pow_ne0 : ∀ j, (P + 1 + 1) ^ j ≠ 0
  | 0 => fun e => Nat.noConfusion e
  | j + 1 => fun e => by
    cases nat_mul_zero _ _ e with
    | inl h => exact pow_ne0 j h
    | inr h => exact Nat.noConfusion h

theorem nrm_pw (z : Int × Int) : ∀ j, nrm (pw (Ring163.ring163 LibInt.intR) z j) = nrm z ^ j
  | 0 => rfl
  | j + 1 => by
    show nrm ((Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) z j) z) = nrm z ^ j * nrm z
    rw [nrm_mul, nrm_pw z j]

theorem npow_add (x : Nat) (a : Nat) : ∀ b, x ^ a * x ^ b = x ^ (a + b)
  | 0 => Nat.mul_one _
  | b + 1 => by
    show x ^ a * (x ^ b * x) = x ^ (a + b) * x
    rw [← npow_add x a b]; lib_nat

theorem en_ne (z : Int × Int) (N : Nat) (h : nrm z = N) (hN : N ≠ 0) : en z ≠ 0 := fun e => by
  apply hN; rw [← h]; unfold nrm; rw [e]; rfl

theorem cancel_pw3 (z : Int × Int) (hz : nrm z = P + 1 + 1) (j : Nat) (A B : Int × Int)
    (h : (Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) z j) A = (Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) z j) B) : A = B := by
  have h0 : (Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) z j) ((Ring163.ring163 LibInt.intR).add A ((Ring163.ring163 LibInt.intR).neg B)) = (Ring163.ring163 LibInt.intR).zero := by
    rw [show (Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) z j) ((Ring163.ring163 LibInt.intR).add A ((Ring163.ring163 LibInt.intR).neg B)) = (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) z j) A) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) z j) B)) by lib_ring,  -- (+13 chars; full line in the Zenodo source)
  have := Cop163.cancel_left _ _ (en_ne _ _ (by rw [nrm_pw, hz]) (pow_ne0 P j)) h0
  calc A = (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).add A ((Ring163.ring163 LibInt.intR).neg B)) B := by lib_ring
    _ = B := by rw [this]; lib_ring

theorem no_assoc3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim163 π)
    (h : Dvd' π (cj π)) : False := by
  obtain ⟨c, hc⟩ := h
  have nc : nrm c = 1 := by
    have e : (P + 1 + 1) * nrm c = (P + 1 + 1) * 1 := by rw [← hπ, ← nrm_mul, ← hc, nrm_cj]; lib_nat
    exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) e
  have c1 := unit_one c π (nrm_one_eq c nc) pπ (by rw [(Ring163.ring163 LibInt.intR).mul_comm, ← hc]; exact prim_cj π pπ)
  rw [c1] at hc
  have hc2 : cj π = π := by rw [hc]; lib_ring
  obtain ⟨a, b⟩ := π
  have hb2 : -b = b := congrArg Prod.snd hc2
  have hb0 : b = 0 := QChar.int_two_zero b (by rw [show b + b = b + -(-b) by lib_int, hb2]; lib_int)
  have hen : en (a, b) = Int.ofNat (P + 1 + 1) := by rw [← nrm_cast, hπ]
  have hsq : Int.natAbs a * Int.natAbs a = P + 1 + 1 := Int.ofNat.inj (by
    rw [GaussEuclid.nas, ← hen]; show a * a = a * a + a * b + 41 * (b * b); rw [hb0]; lib_int)
  cases hp (Int.natAbs a) ⟨Int.natAbs a, hsq.symm⟩ with
  | inl h1' => rw [h1'] at hsq; exact Nat.noConfusion (Nat.succ.inj hsq).symm
  | inr h1' =>
    rw [h1'] at hsq
    have : (P + 1 + 1) * (P + 1 + 1) = (P + 1 + 1) * 1 := hsq.trans (Nat.mul_one _).symm
    have := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) this
    exact Nat.noConfusion (Nat.succ.inj this)

theorem no_div3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim163 π) :
    ∀ d, Dvd' π (pw (Ring163.ring163 LibInt.intR) (cj π) d) → False
  | 0, ⟨c, hc⟩ => by
    have : (P + 1 + 1) * nrm c = 1 := by rw [← hπ, ← nrm_mul, ← hc]; rfl
    cases h' : nrm c with
    | zero => rw [h'] at this; exact Nat.noConfusion this
    | succ b => rw [h', show (P + 1 + 1) * (b + 1) = (P + 1 + 1) * b + P + 1 + 1 by lib_nat] at this; exact Nat.noConfusion (Nat.succ.inj this)
  | d + 1, h => by
    cases eprime_div P hp π hπ _ _ h with
    | inl h1 => exact no_div3 hp π hπ pπ d h1
    | inr h1 => exact no_assoc3 P hp π hπ pπ h1

theorem distinct_lt3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim163 π) (j d l' : Nat)
    (h : (Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) π j) (pw (Ring163.ring163 LibInt.intR) (cj π) (l' + 1 + d)) = (Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) π (j + 1 + d)) (pw (Ring163.ring163 LibInt.intR) (cj π) l')) : False := by
  rw [show l' + 1 + d = l' + (1 + d) by lib_nat, HasseCM.pw_add' (Ring163.ring163 LibInt.intR) (cj π) l' (1 + d),
    show j + 1 + d = j + (1 + d) by lib_nat, HasseCM.pw_add' (Ring163.ring163 LibInt.intR) π j (1 + d)] at h
  have h' : (Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) π j) ((Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) (cj π) l') (pw (Ring163.ring163 LibInt.intR) (cj π) (1 + d)))
      = (Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) π j) ((Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) (cj π) l') (pw (Ring163.ring163 LibInt.intR) π (1 + d))) := by
    calc _ = (Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) π j) (pw (Ring163.ring163 LibInt.intR) π (1 + d))) (pw (Ring163.ring163 LibInt.intR) (cj π) l') := h
      _ = _ := by lib_ring
  have h1 := cancel_pw3 P π hπ j _ _ h'
  have h2 := cancel_pw3 P (cj π) (by rw [nrm_cj, hπ]) l' _ _ h1
  apply no_div3 P hp π hπ pπ (1 + d)
  refine ⟨pw (Ring163.ring163 LibInt.intR) π d, ?_⟩
  rw [h2, show 1 + d = d + 1 by lib_nat]; show (Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) π d) π = _; lib_ring

/-- **π^j π̄^l are distinct.** -/
theorem distinct3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim163 π) (j l j' l' : Nat) (hs : j + l = j' + l')
    (he : (Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) π j) (pw (Ring163.ring163 LibInt.intR) (cj π) l) = (Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) π j') (pw (Ring163.ring163 LibInt.intR) (cj π) l')) : j = j' ∧ l = l' := by
  cases Nat.lt_or_ge j j' with
  | inl hlt =>
    exfalso
    match le_dest hlt with
    | ⟨d, hd⟩ =>
      have hl : l = l' + 1 + d := LibInt.nadd_right_cancel j (by rw [show l + j = j + l by lib_nat, hs, hd]; lib_nat)
      rw [hd, hl] at he
      exact distinct_lt3 P hp π hπ pπ j d l' he
  | inr hge =>
    cases Nat.lt_or_ge j' j with
    | inl hlt =>
      exfalso
      match le_dest hlt with
      | ⟨d, hd⟩ =>
        have hl : l' = l + 1 + d := LibInt.nadd_right_cancel j' (by rw [show l' + j' = j' + l' by lib_nat, ← hs, hd]; lib_nat)
        rw [hd, hl] at he
        exact distinct_lt3 P hp π hπ pπ j' d l he.symm
    | inr hle =>
      have e : j = j' := Nat.le_antisymm hle hge
      exact ⟨e, LibInt.nadd_right_cancel j (by rw [show l + j = j + l by lib_nat, hs, e]; lib_nat)⟩

def w3 (π : Int × Int) (x : Nat × Nat) : Int × Int := (Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) π x.1) (pw (Ring163.ring163 LibInt.intR) (cj π) x.2)

theorem w3_PD (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim163 π) (k : Nat) (x : Nat × Nat) (hx : x.1 + x.2 = k) :
    PD163 ((P + 1 + 1) ^ k) (w3 π x) := by
  refine ⟨?_, ?_⟩
  · rw [← nrm_cast]; unfold w3
    rw [nrm_mul, nrm_pw, nrm_pw, nrm_cj, hπ, npow_add, hx]
  · exact prim_mul _ _ (prim163_pow π pπ _) (prim163_pow (cj π) (prim_cj π pπ) _)

/-- **c_W(p^k) = Σ_{j+l=k} W(π^j π̄^l).** -/
theorem cw3_pk (W : Int × Int → Int × Int) (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int)
    (hπ : nrm π = P + 1 + 1) (pπ : Prim163 π) (k : Nat) :
    cw163 W ((P + 1 + 1) ^ k) = sumK (Ring163.ring163 LibInt.intR) (fun x => W (w3 π x)) (HeckePrime.Wl k) := by
  have hI : ∀ z, I163 ((P + 1 + 1) ^ k) z = sumK (Ring163.ring163 LibInt.intR) (fun x => delta (Ring163.ring163 LibInt.intR) z (w3 π x)) (HeckePrime.Wl k) := fun z => by
    by_cases hz : PD163 ((P + 1 + 1) ^ k) z
    · rw [I163_pos _ _ hz]
      have hN : nrm z = (P + 1 + 1) ^ k := by unfold nrm; rw [hz.1]; rfl
      match primary_only3 P hp π hπ pπ k z hN hz.2 with
      | ⟨j, l, hjl, hx⟩ =>
        have hz' : z = w3 π (j, l) := hx
        rw [HeckePrime.sumK_ext_all (Ring163.ring163 LibInt.intR) k _ (fun x => delta (Ring163.ring163 LibInt.intR) x (j, l)) (fun x hxk => by
          unfold delta
          by_cases hxe : x = (j, l)
          · rw [ite_pos' _ _ hxe, ite_pos' _ _ (by rw [hz', hxe])]
          · rw [ite_neg' _ _ hxe, ite_neg' _ _ (fun e => hxe (by
              have d := distinct3 P hp π hπ pπ j l x.1 x.2 (hjl.trans hxk.symm) (hz'.symm.trans e)
              exact Prod.ext d.1.symm d.2.symm))]) _ (HeckePrime.allk_Wl k),
          HeckePrime.count_Wl (Ring163.ring163 LibInt.intR) k j l hjl]
    · rw [I163_neg _ _ hz]
      rw [HeckePrime.sumK_ext_all (Ring163.ring163 LibInt.intR) k _ (fun _ => (Ring163.ring163 LibInt.intR).zero) (fun x hxk => by
        unfold delta; rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact w3_PD P π hπ pπ k x hxk))]) _ (HeckePrime.allk_Wl k)]
      exact (HeckePrime.zsumR (Ring163.ring163 LibInt.intR) _).symm
  unfold cw163
  rw [sumK_ext (Ring163.ring163 LibInt.intR) _ _ (fun z => by rw [hI z, sumK_mul])]
  rw [sumK_swap]
  refine HeckePrime.sumK_ext_all (Ring163.ring163 LibInt.intR) k _ _ (fun x hxk => ?_) _ (HeckePrime.allk_Wl k)
  have hb := abs_le163 (w3 π x) _ (w3_PD P π hπ pπ k x hxk).1
  exact HeckeMult.pick_box (Ring163.ring163 LibInt.intR) (fun z => W z) (w3 π x) _ hb.1 hb.2

theorem W_pow3 (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring163.ring163 LibInt.intR).mul α β) = (Ring163.ring163 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring163.ring163 LibInt.intR).one = (Ring163.ring163 LibInt.intR).one) (z : Int × Int) :
    ∀ j, W (pw (Ring163.ring163 LibInt.intR) z j) = pw (Ring163.ring163 LibInt.intR) (W z) j
  | 0 => hW1
  | j + 1 => by
    show W ((Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) z j) z) = (Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) (W z) j) (W z)
    rw [hW, W_pow3 W hW hW1 z j]

theorem cw3_pk' (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring163.ring163 LibInt.intR).mul α β) = (Ring163.ring163 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring163.ring163 LibInt.intR).one = (Ring163.ring163 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim163 π) (k : Nat) :
    cw163 W ((P + 1 + 1) ^ k) = HeckePrime.Tg (Ring163.ring163 LibInt.intR) (W π) (W (cj π)) k := by
  rw [cw3_pk P W hp π hπ pπ k]
  exact sumK_ext _ _ _ (fun x => by unfold w3; rw [hW, W_pow3 W hW hW1, W_pow3 W hW hW1]) _

/-- **The Hecke recursion for every multiplicative weight on ℤ[ω].** -/
theorem cw3_rec (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring163.ring163 LibInt.intR).mul α β) = (Ring163.ring163 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring163.ring163 LibInt.intR).one = (Ring163.ring163 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim163 π) (k : Nat) :
    cw163 W ((P + 1 + 1) ^ (k + 2)) = (Ring163.ring163 LibInt.intR).add
      ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).add (W π) (W (cj π))) (cw163 W ((P + 1 + 1) ^ (k + 1))))
      ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul (W π) (W (cj π))) (cw163 W ((P + 1 + 1) ^ k)))) := by
  rw [cw3_pk' P W hW hW1 hp π hπ pπ, cw3_pk' P W hW hW1 hp π hπ pπ, cw3_pk' P W hW hW1 hp π hπ pπ, HeckePrime.Tg_rec]

end HeckePrime163

#print axioms HeckePrime163.pow_ne0
#print axioms HeckePrime163.nrm_pw
#print axioms HeckePrime163.npow_add
#print axioms HeckePrime163.en_ne
#print axioms HeckePrime163.cancel_pw3
#print axioms HeckePrime163.no_assoc3
#print axioms HeckePrime163.no_div3
#print axioms HeckePrime163.distinct_lt3
#print axioms HeckePrime163.distinct3
#print axioms HeckePrime163.w3_PD
#print axioms HeckePrime163.cw3_pk
#print axioms HeckePrime163.W_pow3
#print axioms HeckePrime163.cw3_pk'
#print axioms HeckePrime163.cw3_rec

lang/HeckePrime19.lean

lean_all/lang/HeckePrime19.lean

/-
  lang/HeckePrime19 — T48/T50, cycle 664: **361a's Hecke character at split prime powers**: π ∈ H19 of prime norm p, W multiplicative:
  c_W(p^k) = Σ_{j+l=k} W(π)^j W(π̄)^l and the Hecke recursion.  (Port of HeckePrime3.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Prime19
import lang.HeckePrimeW

namespace HeckePrime19

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius Euc19 Prim19 Uniq19 Prime19 HeckeMult19

variable (P : Nat)

theorem pow_ne0 : ∀ j, (P + 1 + 1) ^ j ≠ 0
  | 0 => fun e => Nat.noConfusion e
  | j + 1 => fun e => by
    cases nat_mul_zero _ _ e with
    | inl h => exact pow_ne0 j h
    | inr h => exact Nat.noConfusion h

theorem nrm_pw (z : Int × Int) : ∀ j, nrm (pw (Ring19.ring19 LibInt.intR) z j) = nrm z ^ j
  | 0 => rfl
  | j + 1 => by
    show nrm ((Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) z j) z) = nrm z ^ j * nrm z
    rw [nrm_mul, nrm_pw z j]

theorem npow_add (x : Nat) (a : Nat) : ∀ b, x ^ a * x ^ b = x ^ (a + b)
  | 0 => Nat.mul_one _
  | b + 1 => by
    show x ^ a * (x ^ b * x) = x ^ (a + b) * x
    rw [← npow_add x a b]; lib_nat

theorem en_ne (z : Int × Int) (N : Nat) (h : nrm z = N) (hN : N ≠ 0) : en z ≠ 0 := fun e => by
  apply hN; rw [← h]; unfold nrm; rw [e]; rfl

theorem cancel_pw3 (z : Int × Int) (hz : nrm z = P + 1 + 1) (j : Nat) (A B : Int × Int)
    (h : (Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) z j) A = (Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) z j) B) : A = B := by
  have h0 : (Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) z j) ((Ring19.ring19 LibInt.intR).add A ((Ring19.ring19 LibInt.intR).neg B)) = (Ring19.ring19 LibInt.intR).zero := by
    rw [show (Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) z j) ((Ring19.ring19 LibInt.intR).add A ((Ring19.ring19 LibInt.intR).neg B)) = (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) z j) A) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) z j) B)) by lib_ring, h]; lib_ring
  have := Cop19.cancel_left _ _ (en_ne _ _ (by rw [nrm_pw, hz]) (pow_ne0 P j)) h0
  calc A = (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).add A ((Ring19.ring19 LibInt.intR).neg B)) B := by lib_ring
    _ = B := by rw [this]; lib_ring

theorem no_assoc3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim19 π)
    (h : Dvd' π (cj π)) : False := by
  obtain ⟨c, hc⟩ := h
  have nc : nrm c = 1 := by
    have e : (P + 1 + 1) * nrm c = (P + 1 + 1) * 1 := by rw [← hπ, ← nrm_mul, ← hc, nrm_cj]; lib_nat
    exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) e
  have c1 := unit_one c π (nrm_one_eq c nc) pπ (by rw [(Ring19.ring19 LibInt.intR).mul_comm, ← hc]; exact prim_cj π pπ)
  rw [c1] at hc
  have hc2 : cj π = π := by rw [hc]; lib_ring
  obtain ⟨a, b⟩ := π
  have hb2 : -b = b := congrArg Prod.snd hc2
  have hb0 : b = 0 := QChar.int_two_zero b (by rw [show b + b = b + -(-b) by lib_int, hb2]; lib_int)
  have hen : en (a, b) = Int.ofNat (P + 1 + 1) := by rw [← nrm_cast, hπ]
  have hsq : Int.natAbs a * Int.natAbs a = P + 1 + 1 := Int.ofNat.inj (by
    rw [GaussEuclid.nas, ← hen]; show a * a = a * a + a * b + 5 * (b * b); rw [hb0]; lib_int)
  cases hp (Int.natAbs a) ⟨Int.natAbs a, hsq.symm⟩ with
  | inl h1' => rw [h1'] at hsq; exact Nat.noConfusion (Nat.succ.inj hsq).symm
  | inr h1' =>
    rw [h1'] at hsq
    have : (P + 1 + 1) * (P + 1 + 1) = (P + 1 + 1) * 1 := hsq.trans (Nat.mul_one _).symm
    have := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) this
    exact Nat.noConfusion (Nat.succ.inj this)

theorem no_div3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim19 π) :
    ∀ d, Dvd' π (pw (Ring19.ring19 LibInt.intR) (cj π) d) → False
  | 0, ⟨c, hc⟩ => by
    have : (P + 1 + 1) * nrm c = 1 := by rw [← hπ, ← nrm_mul, ← hc]; rfl
    cases h' : nrm c with
    | zero => rw [h'] at this; exact Nat.noConfusion this
    | succ b => rw [h', show (P + 1 + 1) * (b + 1) = (P + 1 + 1) * b + P + 1 + 1 by lib_nat] at this; exact Nat.noConfusion (Nat.succ.inj this)
  | d + 1, h => by
    cases eprime_div P hp π hπ _ _ h with
    | inl h1 => exact no_div3 hp π hπ pπ d h1
    | inr h1 => exact no_assoc3 P hp π hπ pπ h1

theorem distinct_lt3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim19 π) (j d l' : Nat)
    (h : (Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) π j) (pw (Ring19.ring19 LibInt.intR) (cj π) (l' + 1 + d)) = (Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) π (j + 1 + d)) (pw (Ring19.ring19 LibInt.intR) (cj π) l')) : False := by
  rw [show l' + 1 + d = l' + (1 + d) by lib_nat, HasseCM.pw_add' (Ring19.ring19 LibInt.intR) (cj π) l' (1 + d),
    show j + 1 + d = j + (1 + d) by lib_nat, HasseCM.pw_add' (Ring19.ring19 LibInt.intR) π j (1 + d)] at h
  have h' : (Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) π j) ((Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) (cj π) l') (pw (Ring19.ring19 LibInt.intR) (cj π) (1 + d)))
      = (Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) π j) ((Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) (cj π) l') (pw (Ring19.ring19 LibInt.intR) π (1 + d))) := by
    calc _ = (Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) π j) (pw (Ring19.ring19 LibInt.intR) π (1 + d))) (pw (Ring19.ring19 LibInt.intR) (cj π) l') := h
      _ = _ := by lib_ring
  have h1 := cancel_pw3 P π hπ j _ _ h'
  have h2 := cancel_pw3 P (cj π) (by rw [nrm_cj, hπ]) l' _ _ h1
  apply no_div3 P hp π hπ pπ (1 + d)
  refine ⟨pw (Ring19.ring19 LibInt.intR) π d, ?_⟩
  rw [h2, show 1 + d = d + 1 by lib_nat]; show (Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) π d) π = _; lib_ring

/-- **π^j π̄^l are distinct.** -/
theorem distinct3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim19 π) (j l j' l' : Nat) (hs : j + l = j' + l')
    (he : (Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) π j) (pw (Ring19.ring19 LibInt.intR) (cj π) l) = (Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) π j') (pw (Ring19.ring19 LibInt.intR) (cj π) l')) : j = j' ∧ l = l' := by
  cases Nat.lt_or_ge j j' with
  | inl hlt =>
    exfalso
    match le_dest hlt with
    | ⟨d, hd⟩ =>
      have hl : l = l' + 1 + d := LibInt.nadd_right_cancel j (by rw [show l + j = j + l by lib_nat, hs, hd]; lib_nat)
      rw [hd, hl] at he
      exact distinct_lt3 P hp π hπ pπ j d l' he
  | inr hge =>
    cases Nat.lt_or_ge j' j with
    | inl hlt =>
      exfalso
      match le_dest hlt with
      | ⟨d, hd⟩ =>
        have hl : l' = l + 1 + d := LibInt.nadd_right_cancel j' (by rw [show l' + j' = j' + l' by lib_nat, ← hs, hd]; lib_nat)
        rw [hd, hl] at he
        exact distinct_lt3 P hp π hπ pπ j' d l he.symm
    | inr hle =>
      have e : j = j' := Nat.le_antisymm hle hge
      exact ⟨e, LibInt.nadd_right_cancel j (by rw [show l + j = j + l by lib_nat, hs, e]; lib_nat)⟩

def w3 (π : Int × Int) (x : Nat × Nat) : Int × Int := (Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) π x.1) (pw (Ring19.ring19 LibInt.intR) (cj π) x.2)

theorem w3_PD (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim19 π) (k : Nat) (x : Nat × Nat) (hx : x.1 + x.2 = k) :
    PD19 ((P + 1 + 1) ^ k) (w3 π x) := by
  refine ⟨?_, ?_⟩
  · rw [← nrm_cast]; unfold w3
    rw [nrm_mul, nrm_pw, nrm_pw, nrm_cj, hπ, npow_add, hx]
  · exact prim_mul _ _ (prim19_pow π pπ _) (prim19_pow (cj π) (prim_cj π pπ) _)

/-- **c_W(p^k) = Σ_{j+l=k} W(π^j π̄^l).** -/
theorem cw3_pk (W : Int × Int → Int × Int) (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int)
    (hπ : nrm π = P + 1 + 1) (pπ : Prim19 π) (k : Nat) :
    cw19 W ((P + 1 + 1) ^ k) = sumK (Ring19.ring19 LibInt.intR) (fun x => W (w3 π x)) (HeckePrime.Wl k) := by
  have hI : ∀ z, I19 ((P + 1 + 1) ^ k) z = sumK (Ring19.ring19 LibInt.intR) (fun x => delta (Ring19.ring19 LibInt.intR) z (w3 π x)) (HeckePrime.Wl k) := fun z => by
    by_cases hz : PD19 ((P + 1 + 1) ^ k) z
    · rw [I19_pos _ _ hz]
      have hN : nrm z = (P + 1 + 1) ^ k := by unfold nrm; rw [hz.1]; rfl
      match primary_only3 P hp π hπ pπ k z hN hz.2 with
      | ⟨j, l, hjl, hx⟩ =>
        have hz' : z = w3 π (j, l) := hx
        rw [HeckePrime.sumK_ext_all (Ring19.ring19 LibInt.intR) k _ (fun x => delta (Ring19.ring19 LibInt.intR) x (j, l)) (fun x hxk => by
          unfold delta
          by_cases hxe : x = (j, l)
          · rw [ite_pos' _ _ hxe, ite_pos' _ _ (by rw [hz', hxe])]
          · rw [ite_neg' _ _ hxe, ite_neg' _ _ (fun e => hxe (by
              have d := distinct3 P hp π hπ pπ j l x.1 x.2 (hjl.trans hxk.symm) (hz'.symm.trans e)
              exact Prod.ext d.1.symm d.2.symm))]) _ (HeckePrime.allk_Wl k),
          HeckePrime.count_Wl (Ring19.ring19 LibInt.intR) k j l hjl]
    · rw [I19_neg _ _ hz]
      rw [HeckePrime.sumK_ext_all (Ring19.ring19 LibInt.intR) k _ (fun _ => (Ring19.ring19 LibInt.intR).zero) (fun x hxk => by
        unfold delta; rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact w3_PD P π hπ pπ k x hxk))]) _ (HeckePrime.allk_Wl k)]
      exact (HeckePrime.zsumR (Ring19.ring19 LibInt.intR) _).symm
  unfold cw19
  rw [sumK_ext (Ring19.ring19 LibInt.intR) _ _ (fun z => by rw [hI z, sumK_mul])]
  rw [sumK_swap]
  refine HeckePrime.sumK_ext_all (Ring19.ring19 LibInt.intR) k _ _ (fun x hxk => ?_) _ (HeckePrime.allk_Wl k)
  have hb := abs_le19 (w3 π x) _ (w3_PD P π hπ pπ k x hxk).1
  exact HeckeMult.pick_box (Ring19.ring19 LibInt.intR) (fun z => W z) (w3 π x) _ hb.1 hb.2

theorem W_pow3 (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring19.ring19 LibInt.intR).mul α β) = (Ring19.ring19 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring19.ring19 LibInt.intR).one = (Ring19.ring19 LibInt.intR).one) (z : Int × Int) :
    ∀ j, W (pw (Ring19.ring19 LibInt.intR) z j) = pw (Ring19.ring19 LibInt.intR) (W z) j
  | 0 => hW1
  | j + 1 => by
    show W ((Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) z j) z) = (Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) (W z) j) (W z)
    rw [hW, W_pow3 W hW hW1 z j]

theorem cw3_pk' (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring19.ring19 LibInt.intR).mul α β) = (Ring19.ring19 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring19.ring19 LibInt.intR).one = (Ring19.ring19 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim19 π) (k : Nat) :
    cw19 W ((P + 1 + 1) ^ k) = HeckePrime.Tg (Ring19.ring19 LibInt.intR) (W π) (W (cj π)) k := by
  rw [cw3_pk P W hp π hπ pπ k]
  exact sumK_ext _ _ _ (fun x => by unfold w3; rw [hW, W_pow3 W hW hW1, W_pow3 W hW hW1]) _

/-- **The Hecke recursion for every multiplicative weight on ℤ[ω].** -/
theorem cw3_rec (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring19.ring19 LibInt.intR).mul α β) = (Ring19.ring19 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring19.ring19 LibInt.intR).one = (Ring19.ring19 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim19 π) (k : Nat) :
    cw19 W ((P + 1 + 1) ^ (k + 2)) = (Ring19.ring19 LibInt.intR).add
      ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).add (W π) (W (cj π))) (cw19 W ((P + 1 + 1) ^ (k + 1))))
      ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul (W π) (W (cj π))) (cw19 W ((P + 1 + 1) ^ k)))) := by
  rw [cw3_pk' P W hW hW1 hp π hπ pπ, cw3_pk' P W hW hW1 hp π hπ pπ, cw3_pk' P W hW hW1 hp π hπ pπ, HeckePrime.Tg_rec]

end HeckePrime19

#print axioms HeckePrime19.pow_ne0
#print axioms HeckePrime19.nrm_pw
#print axioms HeckePrime19.npow_add
#print axioms HeckePrime19.en_ne
#print axioms HeckePrime19.cancel_pw3
#print axioms HeckePrime19.no_assoc3
#print axioms HeckePrime19.no_div3
#print axioms HeckePrime19.distinct_lt3
#print axioms HeckePrime19.distinct3
#print axioms HeckePrime19.w3_PD
#print axioms HeckePrime19.cw3_pk
#print axioms HeckePrime19.W_pow3
#print axioms HeckePrime19.cw3_pk'
#print axioms HeckePrime19.cw3_rec

lang/HeckePrime3.lean

lean_all/lang/HeckePrime3.lean

/-
  lang/HeckePrime3 — T50, cycle 617: **Hecke characters of ℚ(√−3): coefficients at split prime powers and the Hecke recursion**.
  p = P + 2 prime, π ∈ ℤ[ω] primary (≡ 1 mod 3) of norm p, W multiplicative on ℤ[ω] with W(1) = 1 (e.g. W(α) = α^k):
    no_assoc3      π ∤ π̄                                   (else π̄ = π, b = 0, p = a²)
    no_div3        π ∤ π̄^d
    distinct3      π^j π̄^l = π^j' π̄^l', j + l = j' + l' ⇒ (j, l) = (j', l')
    w3_PD          π^j π̄^l is primary of norm p^(j+l)
    cw3_pk         c_W(p^k) = Σ_{j+l=k} W(π^j π̄^l)            (EisPrime3.primary_only3)
    cw3_pk'        c_W(p^k) = Σ_{j+l=k} W(π)^j W(π̄)^l
    cw3_rec        c_W(p^(k+2)) = (W(π) + W(π̄)) c_W(p^(k+1)) − W(π)W(π̄) c_W(p^k)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.EisPrime3
import lang.HeckePrimeW

namespace HeckePrime3

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius EisEuclid EisPrim EisUnique3 EisPrime3 HeckeMult3

variable (P : Nat)

theorem pow_ne0 : ∀ j, (P + 1 + 1) ^ j ≠ 0
  | 0 => fun e => Nat.noConfusion e
  | j + 1 => fun e => by
    cases nat_mul_zero _ _ e with
    | inl h => exact pow_ne0 j h
    | inr h => exact Nat.noConfusion h

theorem nrm_pw (z : Int × Int) : ∀ j, nrm (pw (Cyclo3.cyc3 LibInt.intR) z j) = nrm z ^ j
  | 0 => rfl
  | j + 1 => by
    show nrm ((Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) z j) z) = nrm z ^ j * nrm z
    rw [nrm_mul, nrm_pw z j]

theorem npow_add (x : Nat) (a : Nat) : ∀ b, x ^ a * x ^ b = x ^ (a + b)
  | 0 => Nat.mul_one _
  | b + 1 => by
    show x ^ a * (x ^ b * x) = x ^ (a + b) * x
    rw [← npow_add x a b]; lib_nat

theorem en_ne (z : Int × Int) (N : Nat) (h : nrm z = N) (hN : N ≠ 0) : en z ≠ 0 := fun e => by
  apply hN; rw [← h]; unfold nrm; rw [e]; rfl

theorem cancel_pw3 (z : Int × Int) (hz : nrm z = P + 1 + 1) (j : Nat) (A B : Int × Int)
    (h : (Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) z j) A = (Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) z j) B) : A = B := by
  have h0 : (Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) z j) ((Cyclo3.cyc3 LibInt.intR).add A ((Cyclo3.cyc3 LibInt.intR).neg B)) = (Cyclo3.cyc3 LibInt.intR).zero := by
    rw [show (Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) z j) ((Cyclo3.cyc3 LibInt.intR).add A ((Cyclo3.cyc3 LibInt.intR).neg B)) = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) z j) A) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) z j) B)) by lib_ring, h]; lib_ring
  have := EisCoprime.cancel_left _ _ (en_ne _ _ (by rw [nrm_pw, hz]) (pow_ne0 P j)) h0
  calc A = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add A ((Cyclo3.cyc3 LibInt.intR).neg B)) B := by lib_ring
    _ = B := by rw [this]; lib_ring

theorem no_assoc3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim3 π)
    (h : Dvd' π (cj π)) : False := by
  obtain ⟨c, hc⟩ := h
  have nc : nrm c = 1 := by
    have e : (P + 1 + 1) * nrm c = (P + 1 + 1) * 1 := by rw [← hπ, ← nrm_mul, ← hc, nrm_cj]; lib_nat
    exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) e
  have c1 := unit_one3 c π (nrm_one_eq c nc) pπ (by rw [(Cyclo3.cyc3 LibInt.intR).mul_comm, ← hc]; exact prim3_cj π pπ)
  rw [c1] at hc
  have hc2 : cj π = π := by rw [hc]; lib_ring
  obtain ⟨a, b⟩ := π
  have hb2 : -b = b := congrArg Prod.snd hc2
  have hb0 : b = 0 := QChar.int_two_zero b (by rw [show b + b = b + -(-b) by lib_int, hb2]; lib_int)
  have hen : en (a, b) = Int.ofNat (P + 1 + 1) := by rw [← nrm_cast, hπ]
  have hsq : Int.natAbs a * Int.natAbs a = P + 1 + 1 := Int.ofNat.inj (by
    rw [GaussEuclid.nas, ← hen]; show a * a = a * a + -(a * b) + b * b; rw [hb0]; lib_int)
  cases hp (Int.natAbs a) ⟨Int.natAbs a, hsq.symm⟩ with
  | inl h1' => rw [h1'] at hsq; exact Nat.noConfusion (Nat.succ.inj hsq).symm
  | inr h1' =>
    rw [h1'] at hsq
    have : (P + 1 + 1) * (P + 1 + 1) = (P + 1 + 1) * 1 := hsq.trans (Nat.mul_one _).symm
    have := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) this
    exact Nat.noConfusion (Nat.succ.inj this)

theorem no_div3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim3 π) :
    ∀ d, Dvd' π (pw (Cyclo3.cyc3 LibInt.intR) (cj π) d) → False
  | 0, ⟨c, hc⟩ => by
    have : (P + 1 + 1) * nrm c = 1 := by rw [← hπ, ← nrm_mul, ← hc]; rfl
    cases h' : nrm c with
    | zero => rw [h'] at this; exact Nat.noConfusion this
    | succ b => rw [h', show (P + 1 + 1) * (b + 1) = (P + 1 + 1) * b + P + 1 + 1 by lib_nat] at this; exact Nat.noConfusion (Nat.succ.inj this)
  | d + 1, h => by
    cases eprime_div P hp π hπ _ _ h with
    | inl h1 => exact no_div3 hp π hπ pπ d h1
    | inr h1 => exact no_assoc3 P hp π hπ pπ h1

theorem distinct_lt3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim3 π) (j d l' : Nat)
    (h : (Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) π j) (pw (Cyclo3.cyc3 LibInt.intR) (cj π) (l' + 1 + d)) = (Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) π (j + 1 + d)) (pw (Cyclo3.cyc3 LibInt.intR) (cj π) l')) : False := by
  rw [show l' + 1 + d = l' + (1 + d) by lib_nat, HasseCM.pw_add' (Cyclo3.cyc3 LibInt.intR) (cj π) l' (1 + d),
    show j + 1 + d = j + (1 + d) by lib_nat, HasseCM.pw_add' (Cyclo3.cyc3 LibInt.intR) π j (1 + d)] at h
  have h' : (Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) π j) ((Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) (cj π) l') (pw (Cyclo3.cyc3 LibInt.intR) (cj π) (1 + d)))
      = (Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) π j) ((Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) (cj π) l') (pw (Cyclo3.cyc3 LibInt.intR) π (1 + d))) := by
    calc _ = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) π j) (pw (Cyclo3.cyc3 LibInt.intR) π (1 + d))) (pw (Cyclo3.cyc3 LibInt.intR) (cj π) l') := h
      _ = _ := by lib_ring
  have h1 := cancel_pw3 P π hπ j _ _ h'
  have h2 := cancel_pw3 P (cj π) (by rw [nrm_cj, hπ]) l' _ _ h1
  apply no_div3 P hp π hπ pπ (1 + d)
  refine ⟨pw (Cyclo3.cyc3 LibInt.intR) π d, ?_⟩
  rw [h2, show 1 + d = d + 1 by lib_nat]; show (Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) π d) π = _; lib_ring

/-- **π^j π̄^l are distinct.** -/
theorem distinct3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim3 π) (j l j' l' : Nat) (hs : j + l = j' + l')
    (he : (Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) π j) (pw (Cyclo3.cyc3 LibInt.intR) (cj π) l) = (Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) π j') (pw (Cyclo3.cyc3 LibInt.intR) (cj π) l')) : j = j' ∧ l = l' := by
  cases Nat.lt_or_ge j j' with
  | inl hlt =>
    exfalso
    match le_dest hlt with
    | ⟨d, hd⟩ =>
      have hl : l = l' + 1 + d := LibInt.nadd_right_cancel j (by rw [show l + j = j + l by lib_nat, hs, hd]; lib_nat)
      rw [hd, hl] at he
      exact distinct_lt3 P hp π hπ pπ j d l' he
  | inr hge =>
    cases Nat.lt_or_ge j' j with
    | inl hlt =>
      exfalso
      match le_dest hlt with
      | ⟨d, hd⟩ =>
        have hl : l' = l + 1 + d := LibInt.nadd_right_cancel j' (by rw [show l' + j' = j' + l' by lib_nat, ← hs, hd]; lib_nat)
        rw [hd, hl] at he
        exact distinct_lt3 P hp π hπ pπ j' d l he.symm
    | inr hle =>
      have e : j = j' := Nat.le_antisymm hle hge
      exact ⟨e, LibInt.nadd_right_cancel j (by rw [show l + j = j + l by lib_nat, hs, e]; lib_nat)⟩

def w3 (π : Int × Int) (x : Nat × Nat) : Int × Int := (Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) π x.1) (pw (Cyclo3.cyc3 LibInt.intR) (cj π) x.2)

theorem w3_PD (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim3 π) (k : Nat) (x : Nat × Nat) (hx : x.1 + x.2 = k) :
    PD3 ((P + 1 + 1) ^ k) (w3 π x) := by
  refine ⟨?_, ?_⟩
  · rw [← nrm_cast]; unfold w3
    rw [nrm_mul, nrm_pw, nrm_pw, nrm_cj, hπ, npow_add, hx]
  · exact prim3_mul _ _ (prim3_pow π pπ _) (prim3_pow (cj π) (prim3_cj π pπ) _)

/-- **c_W(p^k) = Σ_{j+l=k} W(π^j π̄^l).** -/
theorem cw3_pk (W : Int × Int → Int × Int) (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int)
    (hπ : nrm π = P + 1 + 1) (pπ : Prim3 π) (k : Nat) :
    cw3 W ((P + 1 + 1) ^ k) = sumK (Cyclo3.cyc3 LibInt.intR) (fun x => W (w3 π x)) (HeckePrime.Wl k) := by
  have hI : ∀ z, I3 ((P + 1 + 1) ^ k) z = sumK (Cyclo3.cyc3 LibInt.intR) (fun x => delta (Cyclo3.cyc3 LibInt.intR) z (w3 π x)) (HeckePrime.Wl k) := fun z => by
    by_cases hz : PD3 ((P + 1 + 1) ^ k) z
    · rw [I3_pos _ _ hz]
      have hN : nrm z = (P + 1 + 1) ^ k := by unfold nrm; rw [hz.1]; rfl
      match primary_only3 P hp π hπ pπ k z hN hz.2 with
      | ⟨j, l, hjl, hx⟩ =>
        have hz' : z = w3 π (j, l) := hx
        rw [HeckePrime.sumK_ext_all (Cyclo3.cyc3 LibInt.intR) k _ (fun x => delta (Cyclo3.cyc3 LibInt.intR) x (j, l)) (fun x hxk => by
          unfold delta
          by_cases hxe : x = (j, l)
          · rw [ite_pos' _ _ hxe, ite_pos' _ _ (by rw [hz', hxe])]
          · rw [ite_neg' _ _ hxe, ite_neg' _ _ (fun e => hxe (by
              have d := distinct3 P hp π hπ pπ j l x.1 x.2 (hjl.trans hxk.symm) (hz'.symm.trans e)
              exact Prod.ext d.1.symm d.2.symm))]) _ (HeckePrime.allk_Wl k),
          HeckePrime.count_Wl (Cyclo3.cyc3 LibInt.intR) k j l hjl]
    · rw [I3_neg _ _ hz]
      rw [HeckePrime.sumK_ext_all (Cyclo3.cyc3 LibInt.intR) k _ (fun _ => (Cyclo3.cyc3 LibInt.intR).zero) (fun x hxk => by
        unfold delta; rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact w3_PD P π hπ pπ k x hxk))]) _ (HeckePrime.allk_Wl k)]
      exact (HeckePrime.zsumR (Cyclo3.cyc3 LibInt.intR) _).symm
  unfold cw3
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun z => by rw [hI z, sumK_mul])]
  rw [sumK_swap]
  refine HeckePrime.sumK_ext_all (Cyclo3.cyc3 LibInt.intR) k _ _ (fun x hxk => ?_) _ (HeckePrime.allk_Wl k)
  have hb := abs_le3 (w3 π x) _ (w3_PD P π hπ pπ k x hxk).1
  exact HeckeMult.pick_box (Cyclo3.cyc3 LibInt.intR) (fun z => W z) (w3 π x) _ hb.1 hb.2

theorem W_pow3 (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Cyclo3.cyc3 LibInt.intR).mul α β) = (Cyclo3.cyc3 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Cyclo3.cyc3 LibInt.intR).one = (Cyclo3.cyc3 LibInt.intR).one) (z : Int × Int) :
    ∀ j, W (pw (Cyclo3.cyc3 LibInt.intR) z j) = pw (Cyclo3.cyc3 LibInt.intR) (W z) j
  | 0 => hW1
  | j + 1 => by
    show W ((Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) z j) z) = (Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) (W z) j) (W z)
    rw [hW, W_pow3 W hW hW1 z j]

theorem cw3_pk' (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Cyclo3.cyc3 LibInt.intR).mul α β) = (Cyclo3.cyc3 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Cyclo3.cyc3 LibInt.intR).one = (Cyclo3.cyc3 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim3 π) (k : Nat) :
    cw3 W ((P + 1 + 1) ^ k) = HeckePrime.Tg (Cyclo3.cyc3 LibInt.intR) (W π) (W (cj π)) k := by
  rw [cw3_pk P W hp π hπ pπ k]
  exact sumK_ext _ _ _ (fun x => by unfold w3; rw [hW, W_pow3 W hW hW1, W_pow3 W hW hW1]) _

/-- **The Hecke recursion for every multiplicative weight on ℤ[ω].** -/
theorem cw3_rec (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Cyclo3.cyc3 LibInt.intR).mul α β) = (Cyclo3.cyc3 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Cyclo3.cyc3 LibInt.intR).one = (Cyclo3.cyc3 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim3 π) (k : Nat) :
    cw3 W ((P + 1 + 1) ^ (k + 2)) = (Cyclo3.cyc3 LibInt.intR).add
      ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).add (W π) (W (cj π))) (cw3 W ((P + 1 + 1) ^ (k + 1))))
      ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (W π) (W (cj π))) (cw3 W ((P + 1 + 1) ^ k)))) := by
  rw [cw3_pk' P W hW hW1 hp π hπ pπ, cw3_pk' P W hW hW1 hp π hπ pπ, cw3_pk' P W hW hW1 hp π hπ pπ, HeckePrime.Tg_rec]

end HeckePrime3

#print axioms HeckePrime3.pow_ne0
#print axioms HeckePrime3.nrm_pw
#print axioms HeckePrime3.npow_add
#print axioms HeckePrime3.en_ne
#print axioms HeckePrime3.cancel_pw3
#print axioms HeckePrime3.no_assoc3
#print axioms HeckePrime3.no_div3
#print axioms HeckePrime3.distinct_lt3
#print axioms HeckePrime3.distinct3
#print axioms HeckePrime3.w3_PD
#print axioms HeckePrime3.cw3_pk
#print axioms HeckePrime3.W_pow3
#print axioms HeckePrime3.cw3_pk'
#print axioms HeckePrime3.cw3_rec

lang/HeckePrime43.lean

lean_all/lang/HeckePrime43.lean

/-
  lang/HeckePrime43 — T48/T50, T48: **ℚ(√−43)'s Hecke character at split prime powers**: π ∈ H43 of prime norm p, W multiplicative:
  c_W(p^k) = Σ_{j+l=k} W(π)^j W(π̄)^l and the Hecke recursion.  (Port of HeckePrime3.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Prime43
import lang.HeckePrimeW

namespace HeckePrime43

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius Euc43 Prim43 Uniq43 Prime43 HeckeMult43

variable (P : Nat)

theorem pow_ne0 : ∀ j, (P + 1 + 1) ^ j ≠ 0
  | 0 => fun e => Nat.noConfusion e
  | j + 1 => fun e => by
    cases nat_mul_zero _ _ e with
    | inl h => exact pow_ne0 j h
    | inr h => exact Nat.noConfusion h

theorem nrm_pw (z : Int × Int) : ∀ j, nrm (pw (Ring43.ring43 LibInt.intR) z j) = nrm z ^ j
  | 0 => rfl
  | j + 1 => by
    show nrm ((Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) z j) z) = nrm z ^ j * nrm z
    rw [nrm_mul, nrm_pw z j]

theorem npow_add (x : Nat) (a : Nat) : ∀ b, x ^ a * x ^ b = x ^ (a + b)
  | 0 => Nat.mul_one _
  | b + 1 => by
    show x ^ a * (x ^ b * x) = x ^ (a + b) * x
    rw [← npow_add x a b]; lib_nat

theorem en_ne (z : Int × Int) (N : Nat) (h : nrm z = N) (hN : N ≠ 0) : en z ≠ 0 := fun e => by
  apply hN; rw [← h]; unfold nrm; rw [e]; rfl

theorem cancel_pw3 (z : Int × Int) (hz : nrm z = P + 1 + 1) (j : Nat) (A B : Int × Int)
    (h : (Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) z j) A = (Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) z j) B) : A = B := by
  have h0 : (Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) z j) ((Ring43.ring43 LibInt.intR).add A ((Ring43.ring43 LibInt.intR).neg B)) = (Ring43.ring43 LibInt.intR).zero := by
    rw [show (Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) z j) ((Ring43.ring43 LibInt.intR).add A ((Ring43.ring43 LibInt.intR).neg B)) = (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) z j) A) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) z j) B)) by lib_ring, h]; lib_ring
  have := Cop43.cancel_left _ _ (en_ne _ _ (by rw [nrm_pw, hz]) (pow_ne0 P j)) h0
  calc A = (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).add A ((Ring43.ring43 LibInt.intR).neg B)) B := by lib_ring
    _ = B := by rw [this]; lib_ring

theorem no_assoc3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim43 π)
    (h : Dvd' π (cj π)) : False := by
  obtain ⟨c, hc⟩ := h
  have nc : nrm c = 1 := by
    have e : (P + 1 + 1) * nrm c = (P + 1 + 1) * 1 := by rw [← hπ, ← nrm_mul, ← hc, nrm_cj]; lib_nat
    exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) e
  have c1 := unit_one c π (nrm_one_eq c nc) pπ (by rw [(Ring43.ring43 LibInt.intR).mul_comm, ← hc]; exact prim_cj π pπ)
  rw [c1] at hc
  have hc2 : cj π = π := by rw [hc]; lib_ring
  obtain ⟨a, b⟩ := π
  have hb2 : -b = b := congrArg Prod.snd hc2
  have hb0 : b = 0 := QChar.int_two_zero b (by rw [show b + b = b + -(-b) by lib_int, hb2]; lib_int)
  have hen : en (a, b) = Int.ofNat (P + 1 + 1) := by rw [← nrm_cast, hπ]
  have hsq : Int.natAbs a * Int.natAbs a = P + 1 + 1 := Int.ofNat.inj (by
    rw [GaussEuclid.nas, ← hen]; show a * a = a * a + a * b + 11 * (b * b); rw [hb0]; lib_int)
  cases hp (Int.natAbs a) ⟨Int.natAbs a, hsq.symm⟩ with
  | inl h1' => rw [h1'] at hsq; exact Nat.noConfusion (Nat.succ.inj hsq).symm
  | inr h1' =>
    rw [h1'] at hsq
    have : (P + 1 + 1) * (P + 1 + 1) = (P + 1 + 1) * 1 := hsq.trans (Nat.mul_one _).symm
    have := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) this
    exact Nat.noConfusion (Nat.succ.inj this)

theorem no_div3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim43 π) :
    ∀ d, Dvd' π (pw (Ring43.ring43 LibInt.intR) (cj π) d) → False
  | 0, ⟨c, hc⟩ => by
    have : (P + 1 + 1) * nrm c = 1 := by rw [← hπ, ← nrm_mul, ← hc]; rfl
    cases h' : nrm c with
    | zero => rw [h'] at this; exact Nat.noConfusion this
    | succ b => rw [h', show (P + 1 + 1) * (b + 1) = (P + 1 + 1) * b + P + 1 + 1 by lib_nat] at this; exact Nat.noConfusion (Nat.succ.inj this)
  | d + 1, h => by
    cases eprime_div P hp π hπ _ _ h with
    | inl h1 => exact no_div3 hp π hπ pπ d h1
    | inr h1 => exact no_assoc3 P hp π hπ pπ h1

theorem distinct_lt3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim43 π) (j d l' : Nat)
    (h : (Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) π j) (pw (Ring43.ring43 LibInt.intR) (cj π) (l' + 1 + d)) = (Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) π (j + 1 + d)) (pw (Ring43.ring43 LibInt.intR) (cj π) l')) : False := by
  rw [show l' + 1 + d = l' + (1 + d) by lib_nat, HasseCM.pw_add' (Ring43.ring43 LibInt.intR) (cj π) l' (1 + d),
    show j + 1 + d = j + (1 + d) by lib_nat, HasseCM.pw_add' (Ring43.ring43 LibInt.intR) π j (1 + d)] at h
  have h' : (Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) π j) ((Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) (cj π) l') (pw (Ring43.ring43 LibInt.intR) (cj π) (1 + d)))
      = (Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) π j) ((Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) (cj π) l') (pw (Ring43.ring43 LibInt.intR) π (1 + d))) := by
    calc _ = (Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) π j) (pw (Ring43.ring43 LibInt.intR) π (1 + d))) (pw (Ring43.ring43 LibInt.intR) (cj π) l') := h
      _ = _ := by lib_ring
  have h1 := cancel_pw3 P π hπ j _ _ h'
  have h2 := cancel_pw3 P (cj π) (by rw [nrm_cj, hπ]) l' _ _ h1
  apply no_div3 P hp π hπ pπ (1 + d)
  refine ⟨pw (Ring43.ring43 LibInt.intR) π d, ?_⟩
  rw [h2, show 1 + d = d + 1 by lib_nat]; show (Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) π d) π = _; lib_ring

/-- **π^j π̄^l are distinct.** -/
theorem distinct3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim43 π) (j l j' l' : Nat) (hs : j + l = j' + l')
    (he : (Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) π j) (pw (Ring43.ring43 LibInt.intR) (cj π) l) = (Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) π j') (pw (Ring43.ring43 LibInt.intR) (cj π) l')) : j = j' ∧ l = l' := by
  cases Nat.lt_or_ge j j' with
  | inl hlt =>
    exfalso
    match le_dest hlt with
    | ⟨d, hd⟩ =>
      have hl : l = l' + 1 + d := LibInt.nadd_right_cancel j (by rw [show l + j = j + l by lib_nat, hs, hd]; lib_nat)
      rw [hd, hl] at he
      exact distinct_lt3 P hp π hπ pπ j d l' he
  | inr hge =>
    cases Nat.lt_or_ge j' j with
    | inl hlt =>
      exfalso
      match le_dest hlt with
      | ⟨d, hd⟩ =>
        have hl : l' = l + 1 + d := LibInt.nadd_right_cancel j' (by rw [show l' + j' = j' + l' by lib_nat, ← hs, hd]; lib_nat)
        rw [hd, hl] at he
        exact distinct_lt3 P hp π hπ pπ j' d l he.symm
    | inr hle =>
      have e : j = j' := Nat.le_antisymm hle hge
      exact ⟨e, LibInt.nadd_right_cancel j (by rw [show l + j = j + l by lib_nat, hs, e]; lib_nat)⟩

def w3 (π : Int × Int) (x : Nat × Nat) : Int × Int := (Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) π x.1) (pw (Ring43.ring43 LibInt.intR) (cj π) x.2)

theorem w3_PD (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim43 π) (k : Nat) (x : Nat × Nat) (hx : x.1 + x.2 = k) :
    PD43 ((P + 1 + 1) ^ k) (w3 π x) := by
  refine ⟨?_, ?_⟩
  · rw [← nrm_cast]; unfold w3
    rw [nrm_mul, nrm_pw, nrm_pw, nrm_cj, hπ, npow_add, hx]
  · exact prim_mul _ _ (prim43_pow π pπ _) (prim43_pow (cj π) (prim_cj π pπ) _)

/-- **c_W(p^k) = Σ_{j+l=k} W(π^j π̄^l).** -/
theorem cw3_pk (W : Int × Int → Int × Int) (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int)
    (hπ : nrm π = P + 1 + 1) (pπ : Prim43 π) (k : Nat) :
    cw43 W ((P + 1 + 1) ^ k) = sumK (Ring43.ring43 LibInt.intR) (fun x => W (w3 π x)) (HeckePrime.Wl k) := by
  have hI : ∀ z, I43 ((P + 1 + 1) ^ k) z = sumK (Ring43.ring43 LibInt.intR) (fun x => delta (Ring43.ring43 LibInt.intR) z (w3 π x)) (HeckePrime.Wl k) := fun z => by
    by_cases hz : PD43 ((P + 1 + 1) ^ k) z
    · rw [I43_pos _ _ hz]
      have hN : nrm z = (P + 1 + 1) ^ k := by unfold nrm; rw [hz.1]; rfl
      match primary_only3 P hp π hπ pπ k z hN hz.2 with
      | ⟨j, l, hjl, hx⟩ =>
        have hz' : z = w3 π (j, l) := hx
        rw [HeckePrime.sumK_ext_all (Ring43.ring43 LibInt.intR) k _ (fun x => delta (Ring43.ring43 LibInt.intR) x (j, l)) (fun x hxk => by
          unfold delta
          by_cases hxe : x = (j, l)
          · rw [ite_pos' _ _ hxe, ite_pos' _ _ (by rw [hz', hxe])]
          · rw [ite_neg' _ _ hxe, ite_neg' _ _ (fun e => hxe (by
              have d := distinct3 P hp π hπ pπ j l x.1 x.2 (hjl.trans hxk.symm) (hz'.symm.trans e)
              exact Prod.ext d.1.symm d.2.symm))]) _ (HeckePrime.allk_Wl k),
          HeckePrime.count_Wl (Ring43.ring43 LibInt.intR) k j l hjl]
    · rw [I43_neg _ _ hz]
      rw [HeckePrime.sumK_ext_all (Ring43.ring43 LibInt.intR) k _ (fun _ => (Ring43.ring43 LibInt.intR).zero) (fun x hxk => by
        unfold delta; rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact w3_PD P π hπ pπ k x hxk))]) _ (HeckePrime.allk_Wl k)]
      exact (HeckePrime.zsumR (Ring43.ring43 LibInt.intR) _).symm
  unfold cw43
  rw [sumK_ext (Ring43.ring43 LibInt.intR) _ _ (fun z => by rw [hI z, sumK_mul])]
  rw [sumK_swap]
  refine HeckePrime.sumK_ext_all (Ring43.ring43 LibInt.intR) k _ _ (fun x hxk => ?_) _ (HeckePrime.allk_Wl k)
  have hb := abs_le43 (w3 π x) _ (w3_PD P π hπ pπ k x hxk).1
  exact HeckeMult.pick_box (Ring43.ring43 LibInt.intR) (fun z => W z) (w3 π x) _ hb.1 hb.2

theorem W_pow3 (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring43.ring43 LibInt.intR).mul α β) = (Ring43.ring43 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring43.ring43 LibInt.intR).one = (Ring43.ring43 LibInt.intR).one) (z : Int × Int) :
    ∀ j, W (pw (Ring43.ring43 LibInt.intR) z j) = pw (Ring43.ring43 LibInt.intR) (W z) j
  | 0 => hW1
  | j + 1 => by
    show W ((Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) z j) z) = (Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) (W z) j) (W z)
    rw [hW, W_pow3 W hW hW1 z j]

theorem cw3_pk' (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring43.ring43 LibInt.intR).mul α β) = (Ring43.ring43 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring43.ring43 LibInt.intR).one = (Ring43.ring43 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim43 π) (k : Nat) :
    cw43 W ((P + 1 + 1) ^ k) = HeckePrime.Tg (Ring43.ring43 LibInt.intR) (W π) (W (cj π)) k := by
  rw [cw3_pk P W hp π hπ pπ k]
  exact sumK_ext _ _ _ (fun x => by unfold w3; rw [hW, W_pow3 W hW hW1, W_pow3 W hW hW1]) _

/-- **The Hecke recursion for every multiplicative weight on ℤ[ω].** -/
theorem cw3_rec (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring43.ring43 LibInt.intR).mul α β) = (Ring43.ring43 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring43.ring43 LibInt.intR).one = (Ring43.ring43 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim43 π) (k : Nat) :
    cw43 W ((P + 1 + 1) ^ (k + 2)) = (Ring43.ring43 LibInt.intR).add
      ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).add (W π) (W (cj π))) (cw43 W ((P + 1 + 1) ^ (k + 1))))
      ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul (W π) (W (cj π))) (cw43 W ((P + 1 + 1) ^ k)))) := by
  rw [cw3_pk' P W hW hW1 hp π hπ pπ, cw3_pk' P W hW hW1 hp π hπ pπ, cw3_pk' P W hW hW1 hp π hπ pπ, HeckePrime.Tg_rec]

end HeckePrime43

#print axioms HeckePrime43.pow_ne0
#print axioms HeckePrime43.nrm_pw
#print axioms HeckePrime43.npow_add
#print axioms HeckePrime43.en_ne
#print axioms HeckePrime43.cancel_pw3
#print axioms HeckePrime43.no_assoc3
#print axioms HeckePrime43.no_div3
#print axioms HeckePrime43.distinct_lt3
#print axioms HeckePrime43.distinct3
#print axioms HeckePrime43.w3_PD
#print axioms HeckePrime43.cw3_pk
#print axioms HeckePrime43.W_pow3
#print axioms HeckePrime43.cw3_pk'
#print axioms HeckePrime43.cw3_rec

lang/HeckePrime67.lean

lean_all/lang/HeckePrime67.lean

/-
  lang/HeckePrime67 — T48/T50, T48: **ℚ(√−67)'s Hecke character at split prime powers**: π ∈ H67 of prime norm p, W multiplicative:
  c_W(p^k) = Σ_{j+l=k} W(π)^j W(π̄)^l and the Hecke recursion.  (Port of HeckePrime3.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Prime67
import lang.HeckePrimeW

namespace HeckePrime67

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius Euc67 Prim67 Uniq67 Prime67 HeckeMult67

variable (P : Nat)

theorem pow_ne0 : ∀ j, (P + 1 + 1) ^ j ≠ 0
  | 0 => fun e => Nat.noConfusion e
  | j + 1 => fun e => by
    cases nat_mul_zero _ _ e with
    | inl h => exact pow_ne0 j h
    | inr h => exact Nat.noConfusion h

theorem nrm_pw (z : Int × Int) : ∀ j, nrm (pw (Ring67.ring67 LibInt.intR) z j) = nrm z ^ j
  | 0 => rfl
  | j + 1 => by
    show nrm ((Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) z j) z) = nrm z ^ j * nrm z
    rw [nrm_mul, nrm_pw z j]

theorem npow_add (x : Nat) (a : Nat) : ∀ b, x ^ a * x ^ b = x ^ (a + b)
  | 0 => Nat.mul_one _
  | b + 1 => by
    show x ^ a * (x ^ b * x) = x ^ (a + b) * x
    rw [← npow_add x a b]; lib_nat

theorem en_ne (z : Int × Int) (N : Nat) (h : nrm z = N) (hN : N ≠ 0) : en z ≠ 0 := fun e => by
  apply hN; rw [← h]; unfold nrm; rw [e]; rfl

theorem cancel_pw3 (z : Int × Int) (hz : nrm z = P + 1 + 1) (j : Nat) (A B : Int × Int)
    (h : (Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) z j) A = (Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) z j) B) : A = B := by
  have h0 : (Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) z j) ((Ring67.ring67 LibInt.intR).add A ((Ring67.ring67 LibInt.intR).neg B)) = (Ring67.ring67 LibInt.intR).zero := by
    rw [show (Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) z j) ((Ring67.ring67 LibInt.intR).add A ((Ring67.ring67 LibInt.intR).neg B)) = (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) z j) A) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) z j) B)) by lib_ring, h]; lib_ring
  have := Cop67.cancel_left _ _ (en_ne _ _ (by rw [nrm_pw, hz]) (pow_ne0 P j)) h0
  calc A = (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).add A ((Ring67.ring67 LibInt.intR).neg B)) B := by lib_ring
    _ = B := by rw [this]; lib_ring

theorem no_assoc3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim67 π)
    (h : Dvd' π (cj π)) : False := by
  obtain ⟨c, hc⟩ := h
  have nc : nrm c = 1 := by
    have e : (P + 1 + 1) * nrm c = (P + 1 + 1) * 1 := by rw [← hπ, ← nrm_mul, ← hc, nrm_cj]; lib_nat
    exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) e
  have c1 := unit_one c π (nrm_one_eq c nc) pπ (by rw [(Ring67.ring67 LibInt.intR).mul_comm, ← hc]; exact prim_cj π pπ)
  rw [c1] at hc
  have hc2 : cj π = π := by rw [hc]; lib_ring
  obtain ⟨a, b⟩ := π
  have hb2 : -b = b := congrArg Prod.snd hc2
  have hb0 : b = 0 := QChar.int_two_zero b (by rw [show b + b = b + -(-b) by lib_int, hb2]; lib_int)
  have hen : en (a, b) = Int.ofNat (P + 1 + 1) := by rw [← nrm_cast, hπ]
  have hsq : Int.natAbs a * Int.natAbs a = P + 1 + 1 := Int.ofNat.inj (by
    rw [GaussEuclid.nas, ← hen]; show a * a = a * a + a * b + 17 * (b * b); rw [hb0]; lib_int)
  cases hp (Int.natAbs a) ⟨Int.natAbs a, hsq.symm⟩ with
  | inl h1' => rw [h1'] at hsq; exact Nat.noConfusion (Nat.succ.inj hsq).symm
  | inr h1' =>
    rw [h1'] at hsq
    have : (P + 1 + 1) * (P + 1 + 1) = (P + 1 + 1) * 1 := hsq.trans (Nat.mul_one _).symm
    have := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) this
    exact Nat.noConfusion (Nat.succ.inj this)

theorem no_div3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim67 π) :
    ∀ d, Dvd' π (pw (Ring67.ring67 LibInt.intR) (cj π) d) → False
  | 0, ⟨c, hc⟩ => by
    have : (P + 1 + 1) * nrm c = 1 := by rw [← hπ, ← nrm_mul, ← hc]; rfl
    cases h' : nrm c with
    | zero => rw [h'] at this; exact Nat.noConfusion this
    | succ b => rw [h', show (P + 1 + 1) * (b + 1) = (P + 1 + 1) * b + P + 1 + 1 by lib_nat] at this; exact Nat.noConfusion (Nat.succ.inj this)
  | d + 1, h => by
    cases eprime_div P hp π hπ _ _ h with
    | inl h1 => exact no_div3 hp π hπ pπ d h1
    | inr h1 => exact no_assoc3 P hp π hπ pπ h1

theorem distinct_lt3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim67 π) (j d l' : Nat)
    (h : (Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) π j) (pw (Ring67.ring67 LibInt.intR) (cj π) (l' + 1 + d)) = (Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) π (j + 1 + d)) (pw (Ring67.ring67 LibInt.intR) (cj π) l')) : False := by
  rw [show l' + 1 + d = l' + (1 + d) by lib_nat, HasseCM.pw_add' (Ring67.ring67 LibInt.intR) (cj π) l' (1 + d),
    show j + 1 + d = j + (1 + d) by lib_nat, HasseCM.pw_add' (Ring67.ring67 LibInt.intR) π j (1 + d)] at h
  have h' : (Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) π j) ((Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) (cj π) l') (pw (Ring67.ring67 LibInt.intR) (cj π) (1 + d)))
      = (Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) π j) ((Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) (cj π) l') (pw (Ring67.ring67 LibInt.intR) π (1 + d))) := by
    calc _ = (Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) π j) (pw (Ring67.ring67 LibInt.intR) π (1 + d))) (pw (Ring67.ring67 LibInt.intR) (cj π) l') := h
      _ = _ := by lib_ring
  have h1 := cancel_pw3 P π hπ j _ _ h'
  have h2 := cancel_pw3 P (cj π) (by rw [nrm_cj, hπ]) l' _ _ h1
  apply no_div3 P hp π hπ pπ (1 + d)
  refine ⟨pw (Ring67.ring67 LibInt.intR) π d, ?_⟩
  rw [h2, show 1 + d = d + 1 by lib_nat]; show (Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) π d) π = _; lib_ring

/-- **π^j π̄^l are distinct.** -/
theorem distinct3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim67 π) (j l j' l' : Nat) (hs : j + l = j' + l')
    (he : (Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) π j) (pw (Ring67.ring67 LibInt.intR) (cj π) l) = (Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) π j') (pw (Ring67.ring67 LibInt.intR) (cj π) l')) : j = j' ∧ l = l' := by
  cases Nat.lt_or_ge j j' with
  | inl hlt =>
    exfalso
    match le_dest hlt with
    | ⟨d, hd⟩ =>
      have hl : l = l' + 1 + d := LibInt.nadd_right_cancel j (by rw [show l + j = j + l by lib_nat, hs, hd]; lib_nat)
      rw [hd, hl] at he
      exact distinct_lt3 P hp π hπ pπ j d l' he
  | inr hge =>
    cases Nat.lt_or_ge j' j with
    | inl hlt =>
      exfalso
      match le_dest hlt with
      | ⟨d, hd⟩ =>
        have hl : l' = l + 1 + d := LibInt.nadd_right_cancel j' (by rw [show l' + j' = j' + l' by lib_nat, ← hs, hd]; lib_nat)
        rw [hd, hl] at he
        exact distinct_lt3 P hp π hπ pπ j' d l he.symm
    | inr hle =>
      have e : j = j' := Nat.le_antisymm hle hge
      exact ⟨e, LibInt.nadd_right_cancel j (by rw [show l + j = j + l by lib_nat, hs, e]; lib_nat)⟩

def w3 (π : Int × Int) (x : Nat × Nat) : Int × Int := (Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) π x.1) (pw (Ring67.ring67 LibInt.intR) (cj π) x.2)

theorem w3_PD (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim67 π) (k : Nat) (x : Nat × Nat) (hx : x.1 + x.2 = k) :
    PD67 ((P + 1 + 1) ^ k) (w3 π x) := by
  refine ⟨?_, ?_⟩
  · rw [← nrm_cast]; unfold w3
    rw [nrm_mul, nrm_pw, nrm_pw, nrm_cj, hπ, npow_add, hx]
  · exact prim_mul _ _ (prim67_pow π pπ _) (prim67_pow (cj π) (prim_cj π pπ) _)

/-- **c_W(p^k) = Σ_{j+l=k} W(π^j π̄^l).** -/
theorem cw3_pk (W : Int × Int → Int × Int) (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int)
    (hπ : nrm π = P + 1 + 1) (pπ : Prim67 π) (k : Nat) :
    cw67 W ((P + 1 + 1) ^ k) = sumK (Ring67.ring67 LibInt.intR) (fun x => W (w3 π x)) (HeckePrime.Wl k) := by
  have hI : ∀ z, I67 ((P + 1 + 1) ^ k) z = sumK (Ring67.ring67 LibInt.intR) (fun x => delta (Ring67.ring67 LibInt.intR) z (w3 π x)) (HeckePrime.Wl k) := fun z => by
    by_cases hz : PD67 ((P + 1 + 1) ^ k) z
    · rw [I67_pos _ _ hz]
      have hN : nrm z = (P + 1 + 1) ^ k := by unfold nrm; rw [hz.1]; rfl
      match primary_only3 P hp π hπ pπ k z hN hz.2 with
      | ⟨j, l, hjl, hx⟩ =>
        have hz' : z = w3 π (j, l) := hx
        rw [HeckePrime.sumK_ext_all (Ring67.ring67 LibInt.intR) k _ (fun x => delta (Ring67.ring67 LibInt.intR) x (j, l)) (fun x hxk => by
          unfold delta
          by_cases hxe : x = (j, l)
          · rw [ite_pos' _ _ hxe, ite_pos' _ _ (by rw [hz', hxe])]
          · rw [ite_neg' _ _ hxe, ite_neg' _ _ (fun e => hxe (by
              have d := distinct3 P hp π hπ pπ j l x.1 x.2 (hjl.trans hxk.symm) (hz'.symm.trans e)
              exact Prod.ext d.1.symm d.2.symm))]) _ (HeckePrime.allk_Wl k),
          HeckePrime.count_Wl (Ring67.ring67 LibInt.intR) k j l hjl]
    · rw [I67_neg _ _ hz]
      rw [HeckePrime.sumK_ext_all (Ring67.ring67 LibInt.intR) k _ (fun _ => (Ring67.ring67 LibInt.intR).zero) (fun x hxk => by
        unfold delta; rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact w3_PD P π hπ pπ k x hxk))]) _ (HeckePrime.allk_Wl k)]
      exact (HeckePrime.zsumR (Ring67.ring67 LibInt.intR) _).symm
  unfold cw67
  rw [sumK_ext (Ring67.ring67 LibInt.intR) _ _ (fun z => by rw [hI z, sumK_mul])]
  rw [sumK_swap]
  refine HeckePrime.sumK_ext_all (Ring67.ring67 LibInt.intR) k _ _ (fun x hxk => ?_) _ (HeckePrime.allk_Wl k)
  have hb := abs_le67 (w3 π x) _ (w3_PD P π hπ pπ k x hxk).1
  exact HeckeMult.pick_box (Ring67.ring67 LibInt.intR) (fun z => W z) (w3 π x) _ hb.1 hb.2

theorem W_pow3 (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring67.ring67 LibInt.intR).mul α β) = (Ring67.ring67 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring67.ring67 LibInt.intR).one = (Ring67.ring67 LibInt.intR).one) (z : Int × Int) :
    ∀ j, W (pw (Ring67.ring67 LibInt.intR) z j) = pw (Ring67.ring67 LibInt.intR) (W z) j
  | 0 => hW1
  | j + 1 => by
    show W ((Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) z j) z) = (Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) (W z) j) (W z)
    rw [hW, W_pow3 W hW hW1 z j]

theorem cw3_pk' (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring67.ring67 LibInt.intR).mul α β) = (Ring67.ring67 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring67.ring67 LibInt.intR).one = (Ring67.ring67 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim67 π) (k : Nat) :
    cw67 W ((P + 1 + 1) ^ k) = HeckePrime.Tg (Ring67.ring67 LibInt.intR) (W π) (W (cj π)) k := by
  rw [cw3_pk P W hp π hπ pπ k]
  exact sumK_ext _ _ _ (fun x => by unfold w3; rw [hW, W_pow3 W hW hW1, W_pow3 W hW hW1]) _

/-- **The Hecke recursion for every multiplicative weight on ℤ[ω].** -/
theorem cw3_rec (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring67.ring67 LibInt.intR).mul α β) = (Ring67.ring67 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring67.ring67 LibInt.intR).one = (Ring67.ring67 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim67 π) (k : Nat) :
    cw67 W ((P + 1 + 1) ^ (k + 2)) = (Ring67.ring67 LibInt.intR).add
      ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).add (W π) (W (cj π))) (cw67 W ((P + 1 + 1) ^ (k + 1))))
      ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul (W π) (W (cj π))) (cw67 W ((P + 1 + 1) ^ k)))) := by
  rw [cw3_pk' P W hW hW1 hp π hπ pπ, cw3_pk' P W hW hW1 hp π hπ pπ, cw3_pk' P W hW hW1 hp π hπ pπ, HeckePrime.Tg_rec]

end HeckePrime67

#print axioms HeckePrime67.pow_ne0
#print axioms HeckePrime67.nrm_pw
#print axioms HeckePrime67.npow_add
#print axioms HeckePrime67.en_ne
#print axioms HeckePrime67.cancel_pw3
#print axioms HeckePrime67.no_assoc3
#print axioms HeckePrime67.no_div3
#print axioms HeckePrime67.distinct_lt3
#print axioms HeckePrime67.distinct3
#print axioms HeckePrime67.w3_PD
#print axioms HeckePrime67.cw3_pk
#print axioms HeckePrime67.W_pow3
#print axioms HeckePrime67.cw3_pk'
#print axioms HeckePrime67.cw3_rec

lang/HeckePrime7.lean

lean_all/lang/HeckePrime7.lean

/-
  lang/HeckePrime7 — T48/T50, cycle 644: **49a's Hecke character at split prime powers**: π ∈ H7 of prime norm p, W multiplicative:
  c_W(p^k) = Σ_{j+l=k} W(π)^j W(π̄)^l and the Hecke recursion.  (Port of HeckePrime3.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Prime7
import lang.HeckePrimeW

namespace HeckePrime7

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius Euc7 Prim7 Uniq7 Prime7 HeckeMult7

variable (P : Nat)

theorem pow_ne0 : ∀ j, (P + 1 + 1) ^ j ≠ 0
  | 0 => fun e => Nat.noConfusion e
  | j + 1 => fun e => by
    cases nat_mul_zero _ _ e with
    | inl h => exact pow_ne0 j h
    | inr h => exact Nat.noConfusion h

theorem nrm_pw (z : Int × Int) : ∀ j, nrm (pw (Ring7.ring7 LibInt.intR) z j) = nrm z ^ j
  | 0 => rfl
  | j + 1 => by
    show nrm ((Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) z j) z) = nrm z ^ j * nrm z
    rw [nrm_mul, nrm_pw z j]

theorem npow_add (x : Nat) (a : Nat) : ∀ b, x ^ a * x ^ b = x ^ (a + b)
  | 0 => Nat.mul_one _
  | b + 1 => by
    show x ^ a * (x ^ b * x) = x ^ (a + b) * x
    rw [← npow_add x a b]; lib_nat

theorem en_ne (z : Int × Int) (N : Nat) (h : nrm z = N) (hN : N ≠ 0) : en z ≠ 0 := fun e => by
  apply hN; rw [← h]; unfold nrm; rw [e]; rfl

theorem cancel_pw3 (z : Int × Int) (hz : nrm z = P + 1 + 1) (j : Nat) (A B : Int × Int)
    (h : (Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) z j) A = (Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) z j) B) : A = B := by
  have h0 : (Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) z j) ((Ring7.ring7 LibInt.intR).add A ((Ring7.ring7 LibInt.intR).neg B)) = (Ring7.ring7 LibInt.intR).zero := by
    rw [show (Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) z j) ((Ring7.ring7 LibInt.intR).add A ((Ring7.ring7 LibInt.intR).neg B)) = (Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) z j) A) ((Ring7.ring7 LibInt.intR).neg ((Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) z j) B)) by lib_ring, h]; lib_ring
  have := Cop7.cancel_left _ _ (en_ne _ _ (by rw [nrm_pw, hz]) (pow_ne0 P j)) h0
  calc A = (Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).add A ((Ring7.ring7 LibInt.intR).neg B)) B := by lib_ring
    _ = B := by rw [this]; lib_ring

theorem no_assoc3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim7 π)
    (h : Dvd' π (cj π)) : False := by
  obtain ⟨c, hc⟩ := h
  have nc : nrm c = 1 := by
    have e : (P + 1 + 1) * nrm c = (P + 1 + 1) * 1 := by rw [← hπ, ← nrm_mul, ← hc, nrm_cj]; lib_nat
    exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) e
  have c1 := unit_one7 c π (nrm_one_eq c nc) pπ (by rw [(Ring7.ring7 LibInt.intR).mul_comm, ← hc]; exact prim7_cj π pπ)
  rw [c1] at hc
  have hc2 : cj π = π := by rw [hc]; lib_ring
  obtain ⟨a, b⟩ := π
  have hb2 : -b = b := congrArg Prod.snd hc2
  have hb0 : b = 0 := QChar.int_two_zero b (by rw [show b + b = b + -(-b) by lib_int, hb2]; lib_int)
  have hen : en (a, b) = Int.ofNat (P + 1 + 1) := by rw [← nrm_cast, hπ]
  have hsq : Int.natAbs a * Int.natAbs a = P + 1 + 1 := Int.ofNat.inj (by
    rw [GaussEuclid.nas, ← hen]; show a * a = a * a + a * b + 2 * (b * b); rw [hb0]; lib_int)
  cases hp (Int.natAbs a) ⟨Int.natAbs a, hsq.symm⟩ with
  | inl h1' => rw [h1'] at hsq; exact Nat.noConfusion (Nat.succ.inj hsq).symm
  | inr h1' =>
    rw [h1'] at hsq
    have : (P + 1 + 1) * (P + 1 + 1) = (P + 1 + 1) * 1 := hsq.trans (Nat.mul_one _).symm
    have := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) this
    exact Nat.noConfusion (Nat.succ.inj this)

theorem no_div3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim7 π) :
    ∀ d, Dvd' π (pw (Ring7.ring7 LibInt.intR) (cj π) d) → False
  | 0, ⟨c, hc⟩ => by
    have : (P + 1 + 1) * nrm c = 1 := by rw [← hπ, ← nrm_mul, ← hc]; rfl
    cases h' : nrm c with
    | zero => rw [h'] at this; exact Nat.noConfusion this
    | succ b => rw [h', show (P + 1 + 1) * (b + 1) = (P + 1 + 1) * b + P + 1 + 1 by lib_nat] at this; exact Nat.noConfusion (Nat.succ.inj this)
  | d + 1, h => by
    cases eprime_div P hp π hπ _ _ h with
    | inl h1 => exact no_div3 hp π hπ pπ d h1
    | inr h1 => exact no_assoc3 P hp π hπ pπ h1

theorem distinct_lt3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim7 π) (j d l' : Nat)
    (h : (Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) π j) (pw (Ring7.ring7 LibInt.intR) (cj π) (l' + 1 + d)) = (Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) π (j + 1 + d)) (pw (Ring7.ring7 LibInt.intR) (cj π) l')) : False := by
  rw [show l' + 1 + d = l' + (1 + d) by lib_nat, HasseCM.pw_add' (Ring7.ring7 LibInt.intR) (cj π) l' (1 + d),
    show j + 1 + d = j + (1 + d) by lib_nat, HasseCM.pw_add' (Ring7.ring7 LibInt.intR) π j (1 + d)] at h
  have h' : (Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) π j) ((Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) (cj π) l') (pw (Ring7.ring7 LibInt.intR) (cj π) (1 + d)))
      = (Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) π j) ((Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) (cj π) l') (pw (Ring7.ring7 LibInt.intR) π (1 + d))) := by
    calc _ = (Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) π j) (pw (Ring7.ring7 LibInt.intR) π (1 + d))) (pw (Ring7.ring7 LibInt.intR) (cj π) l') := h
      _ = _ := by lib_ring
  have h1 := cancel_pw3 P π hπ j _ _ h'
  have h2 := cancel_pw3 P (cj π) (by rw [nrm_cj, hπ]) l' _ _ h1
  apply no_div3 P hp π hπ pπ (1 + d)
  refine ⟨pw (Ring7.ring7 LibInt.intR) π d, ?_⟩
  rw [h2, show 1 + d = d + 1 by lib_nat]; show (Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) π d) π = _; lib_ring

/-- **π^j π̄^l are distinct.** -/
theorem distinct3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim7 π) (j l j' l' : Nat) (hs : j + l = j' + l')
    (he : (Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) π j) (pw (Ring7.ring7 LibInt.intR) (cj π) l) = (Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) π j') (pw (Ring7.ring7 LibInt.intR) (cj π) l')) : j = j' ∧ l = l' := by
  cases Nat.lt_or_ge j j' with
  | inl hlt =>
    exfalso
    match le_dest hlt with
    | ⟨d, hd⟩ =>
      have hl : l = l' + 1 + d := LibInt.nadd_right_cancel j (by rw [show l + j = j + l by lib_nat, hs, hd]; lib_nat)
      rw [hd, hl] at he
      exact distinct_lt3 P hp π hπ pπ j d l' he
  | inr hge =>
    cases Nat.lt_or_ge j' j with
    | inl hlt =>
      exfalso
      match le_dest hlt with
      | ⟨d, hd⟩ =>
        have hl : l' = l + 1 + d := LibInt.nadd_right_cancel j' (by rw [show l' + j' = j' + l' by lib_nat, ← hs, hd]; lib_nat)
        rw [hd, hl] at he
        exact distinct_lt3 P hp π hπ pπ j' d l he.symm
    | inr hle =>
      have e : j = j' := Nat.le_antisymm hle hge
      exact ⟨e, LibInt.nadd_right_cancel j (by rw [show l + j = j + l by lib_nat, hs, e]; lib_nat)⟩

def w3 (π : Int × Int) (x : Nat × Nat) : Int × Int := (Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) π x.1) (pw (Ring7.ring7 LibInt.intR) (cj π) x.2)

theorem w3_PD (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim7 π) (k : Nat) (x : Nat × Nat) (hx : x.1 + x.2 = k) :
    PD7 ((P + 1 + 1) ^ k) (w3 π x) := by
  refine ⟨?_, ?_⟩
  · rw [← nrm_cast]; unfold w3
    rw [nrm_mul, nrm_pw, nrm_pw, nrm_cj, hπ, npow_add, hx]
  · exact prim7_mul _ _ (prim7_pow π pπ _) (prim7_pow (cj π) (prim7_cj π pπ) _)

/-- **c_W(p^k) = Σ_{j+l=k} W(π^j π̄^l).** -/
theorem cw3_pk (W : Int × Int → Int × Int) (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int)
    (hπ : nrm π = P + 1 + 1) (pπ : Prim7 π) (k : Nat) :
    cw7 W ((P + 1 + 1) ^ k) = sumK (Ring7.ring7 LibInt.intR) (fun x => W (w3 π x)) (HeckePrime.Wl k) := by
  have hI : ∀ z, I7 ((P + 1 + 1) ^ k) z = sumK (Ring7.ring7 LibInt.intR) (fun x => delta (Ring7.ring7 LibInt.intR) z (w3 π x)) (HeckePrime.Wl k) := fun z => by
    by_cases hz : PD7 ((P + 1 + 1) ^ k) z
    · rw [I7_pos _ _ hz]
      have hN : nrm z = (P + 1 + 1) ^ k := by unfold nrm; rw [hz.1]; rfl
      match primary_only3 P hp π hπ pπ k z hN hz.2 with
      | ⟨j, l, hjl, hx⟩ =>
        have hz' : z = w3 π (j, l) := hx
        rw [HeckePrime.sumK_ext_all (Ring7.ring7 LibInt.intR) k _ (fun x => delta (Ring7.ring7 LibInt.intR) x (j, l)) (fun x hxk => by
          unfold delta
          by_cases hxe : x = (j, l)
          · rw [ite_pos' _ _ hxe, ite_pos' _ _ (by rw [hz', hxe])]
          · rw [ite_neg' _ _ hxe, ite_neg' _ _ (fun e => hxe (by
              have d := distinct3 P hp π hπ pπ j l x.1 x.2 (hjl.trans hxk.symm) (hz'.symm.trans e)
              exact Prod.ext d.1.symm d.2.symm))]) _ (HeckePrime.allk_Wl k),
          HeckePrime.count_Wl (Ring7.ring7 LibInt.intR) k j l hjl]
    · rw [I7_neg _ _ hz]
      rw [HeckePrime.sumK_ext_all (Ring7.ring7 LibInt.intR) k _ (fun _ => (Ring7.ring7 LibInt.intR).zero) (fun x hxk => by
        unfold delta; rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact w3_PD P π hπ pπ k x hxk))]) _ (HeckePrime.allk_Wl k)]
      exact (HeckePrime.zsumR (Ring7.ring7 LibInt.intR) _).symm
  unfold cw7
  rw [sumK_ext (Ring7.ring7 LibInt.intR) _ _ (fun z => by rw [hI z, sumK_mul])]
  rw [sumK_swap]
  refine HeckePrime.sumK_ext_all (Ring7.ring7 LibInt.intR) k _ _ (fun x hxk => ?_) _ (HeckePrime.allk_Wl k)
  have hb := abs_le7 (w3 π x) _ (w3_PD P π hπ pπ k x hxk).1
  exact HeckeMult.pick_box (Ring7.ring7 LibInt.intR) (fun z => W z) (w3 π x) _ hb.1 hb.2

theorem W_pow3 (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring7.ring7 LibInt.intR).mul α β) = (Ring7.ring7 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring7.ring7 LibInt.intR).one = (Ring7.ring7 LibInt.intR).one) (z : Int × Int) :
    ∀ j, W (pw (Ring7.ring7 LibInt.intR) z j) = pw (Ring7.ring7 LibInt.intR) (W z) j
  | 0 => hW1
  | j + 1 => by
    show W ((Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) z j) z) = (Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) (W z) j) (W z)
    rw [hW, W_pow3 W hW hW1 z j]

theorem cw3_pk' (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring7.ring7 LibInt.intR).mul α β) = (Ring7.ring7 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring7.ring7 LibInt.intR).one = (Ring7.ring7 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim7 π) (k : Nat) :
    cw7 W ((P + 1 + 1) ^ k) = HeckePrime.Tg (Ring7.ring7 LibInt.intR) (W π) (W (cj π)) k := by
  rw [cw3_pk P W hp π hπ pπ k]
  exact sumK_ext _ _ _ (fun x => by unfold w3; rw [hW, W_pow3 W hW hW1, W_pow3 W hW hW1]) _

/-- **The Hecke recursion for every multiplicative weight on ℤ[ω].** -/
theorem cw3_rec (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring7.ring7 LibInt.intR).mul α β) = (Ring7.ring7 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring7.ring7 LibInt.intR).one = (Ring7.ring7 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim7 π) (k : Nat) :
    cw7 W ((P + 1 + 1) ^ (k + 2)) = (Ring7.ring7 LibInt.intR).add
      ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).add (W π) (W (cj π))) (cw7 W ((P + 1 + 1) ^ (k + 1))))
      ((Ring7.ring7 LibInt.intR).neg ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul (W π) (W (cj π))) (cw7 W ((P + 1 + 1) ^ k)))) := by
  rw [cw3_pk' P W hW hW1 hp π hπ pπ, cw3_pk' P W hW hW1 hp π hπ pπ, cw3_pk' P W hW hW1 hp π hπ pπ, HeckePrime.Tg_rec]

end HeckePrime7

#print axioms HeckePrime7.pow_ne0
#print axioms HeckePrime7.nrm_pw
#print axioms HeckePrime7.npow_add
#print axioms HeckePrime7.en_ne
#print axioms HeckePrime7.cancel_pw3
#print axioms HeckePrime7.no_assoc3
#print axioms HeckePrime7.no_div3
#print axioms HeckePrime7.distinct_lt3
#print axioms HeckePrime7.distinct3
#print axioms HeckePrime7.w3_PD
#print axioms HeckePrime7.cw3_pk
#print axioms HeckePrime7.W_pow3
#print axioms HeckePrime7.cw3_pk'
#print axioms HeckePrime7.cw3_rec

lang/HeckePrime8.lean

lean_all/lang/HeckePrime8.lean

/-
  lang/HeckePrime8 — T48/T50, cycle 634: **256a's Hecke character at split prime powers**: p = a² + 2b² prime, π ∈ H8 of norm p,
  W multiplicative with W(1) = 1:  c_W(p^k) = Σ_{j+l=k} W(π)^j W(π̄)^l  and the Hecke recursion
    c_W(p^(k+2)) = (W(π) + W(π̄)) c_W(p^(k+1)) − W(π)W(π̄) c_W(p^k).        (Port of HeckePrime3.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Sq2Prime8
import lang.HeckePrimeW

namespace HeckePrime8

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius Sq2Euclid Sq2Prim8 Sq2Unique8 Sq2Prime8 HeckeMult8

variable (P : Nat)

theorem pow_ne0 : ∀ j, (P + 1 + 1) ^ j ≠ 0
  | 0 => fun e => Nat.noConfusion e
  | j + 1 => fun e => by
    cases nat_mul_zero _ _ e with
    | inl h => exact pow_ne0 j h
    | inr h => exact Nat.noConfusion h

theorem nrm_pw (z : Int × Int) : ∀ j, nrm (pw (Quad.quad LibInt.intR (-2)) z j) = nrm z ^ j
  | 0 => rfl
  | j + 1 => by
    show nrm ((Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) z j) z) = nrm z ^ j * nrm z
    rw [nrm_mul, nrm_pw z j]

theorem npow_add (x : Nat) (a : Nat) : ∀ b, x ^ a * x ^ b = x ^ (a + b)
  | 0 => Nat.mul_one _
  | b + 1 => by
    show x ^ a * (x ^ b * x) = x ^ (a + b) * x
    rw [← npow_add x a b]; lib_nat

theorem en_ne (z : Int × Int) (N : Nat) (h : nrm z = N) (hN : N ≠ 0) : en z ≠ 0 := fun e => by
  apply hN; rw [← h]; exact Int.ofNat.inj (by rw [nrm_cast, e]; rfl)

theorem cancel_pw3 (z : Int × Int) (hz : nrm z = P + 1 + 1) (j : Nat) (A B : Int × Int)
    (h : (Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) z j) A = (Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) z j) B) : A = B := by
  have h0 : (Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) z j) ((Quad.quad LibInt.intR (-2)).add A ((Quad.quad LibInt.intR (-2)).neg B)) = (Quad.quad LibInt.intR (-2)).zero := by
    rw [show (Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) z j) ((Quad.quad LibInt.intR (-2)).add A ((Quad.quad LibInt.intR (-2)).neg B)) = (Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) z j) A) ((Quad.quad LibInt.intR (-2)).neg ((Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) z j) B)) by lib_ring, h]; lib_r  -- (+3 chars; full line in the Zenodo source)
  have := Sq2Coprime.cancel_left _ _ (en_ne _ _ (by rw [nrm_pw, hz]) (pow_ne0 P j)) h0
  calc A = (Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).add A ((Quad.quad LibInt.intR (-2)).neg B)) B := by lib_ring
    _ = B := by rw [this]; lib_ring

theorem no_assoc3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim8 π)
    (h : Dvd' π (cj π)) : False := by
  obtain ⟨c, hc⟩ := h
  have nc : nrm c = 1 := by
    have e : (P + 1 + 1) * nrm c = (P + 1 + 1) * 1 := by rw [← hπ, ← nrm_mul, ← hc, nrm_cj]; lib_nat
    exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) e
  have c1 := unit_one8 c π (nrm_one_eq c nc) pπ (by rw [(Quad.quad LibInt.intR (-2)).mul_comm, ← hc]; exact prim8_cj π pπ)
  rw [c1] at hc
  have hc2 : cj π = π := by rw [hc]; lib_ring
  obtain ⟨a, b⟩ := π
  have hb2 : -b = b := congrArg Prod.snd hc2
  have hb0 : b = 0 := QChar.int_two_zero b (by rw [show b + b = b + -(-b) by lib_int, hb2]; lib_int)
  have hen : en (a, b) = Int.ofNat (P + 1 + 1) := by rw [← nrm_cast, hπ]
  have hsq : Int.natAbs a * Int.natAbs a = P + 1 + 1 := Int.ofNat.inj (by
    rw [GaussEuclid.nas, ← hen]; show a * a = a * a + 2 * (b * b); rw [hb0]; lib_int)
  cases hp (Int.natAbs a) ⟨Int.natAbs a, hsq.symm⟩ with
  | inl h1' => rw [h1'] at hsq; exact Nat.noConfusion (Nat.succ.inj hsq).symm
  | inr h1' =>
    rw [h1'] at hsq
    have : (P + 1 + 1) * (P + 1 + 1) = (P + 1 + 1) * 1 := hsq.trans (Nat.mul_one _).symm
    have := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) this
    exact Nat.noConfusion (Nat.succ.inj this)

theorem no_div3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim8 π) :
    ∀ d, Dvd' π (pw (Quad.quad LibInt.intR (-2)) (cj π) d) → False
  | 0, ⟨c, hc⟩ => by
    have : (P + 1 + 1) * nrm c = 1 := by rw [← hπ, ← nrm_mul, ← hc]; rfl
    cases h' : nrm c with
    | zero => rw [h'] at this; exact Nat.noConfusion this
    | succ b => rw [h', show (P + 1 + 1) * (b + 1) = (P + 1 + 1) * b + P + 1 + 1 by lib_nat] at this; exact Nat.noConfusion (Nat.succ.inj this)
  | d + 1, h => by
    cases eprime_div P hp π hπ _ _ h with
    | inl h1 => exact no_div3 hp π hπ pπ d h1
    | inr h1 => exact no_assoc3 P hp π hπ pπ h1

theorem distinct_lt3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim8 π) (j d l' : Nat)
    (h : (Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) π j) (pw (Quad.quad LibInt.intR (-2)) (cj π) (l' + 1 + d)) = (Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) π (j + 1 + d)) (pw (Quad.quad LibInt.intR (-2)) (cj π) l')) : False := by
  rw [show l' + 1 + d = l' + (1 + d) by lib_nat, HasseCM.pw_add' (Quad.quad LibInt.intR (-2)) (cj π) l' (1 + d),
    show j + 1 + d = j + (1 + d) by lib_nat, HasseCM.pw_add' (Quad.quad LibInt.intR (-2)) π j (1 + d)] at h
  have h' : (Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) π j) ((Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) (cj π) l') (pw (Quad.quad LibInt.intR (-2)) (cj π) (1 + d)))
      = (Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) π j) ((Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) (cj π) l') (pw (Quad.quad LibInt.intR (-2)) π (1 + d))) := by
    calc _ = (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) π j) (pw (Quad.quad LibInt.intR (-2)) π (1 + d))) (pw (Quad.quad LibInt.intR (-2)) (cj π) l') := h
      _ = _ := by lib_ring
  have h1 := cancel_pw3 P π hπ j _ _ h'
  have h2 := cancel_pw3 P (cj π) (by rw [nrm_cj, hπ]) l' _ _ h1
  apply no_div3 P hp π hπ pπ (1 + d)
  refine ⟨pw (Quad.quad LibInt.intR (-2)) π d, ?_⟩
  rw [h2, show 1 + d = d + 1 by lib_nat]; show (Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) π d) π = _; lib_ring

/-- **π^j π̄^l are distinct.** -/
theorem distinct3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim8 π) (j l j' l' : Nat) (hs : j + l = j' + l')
    (he : (Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) π j) (pw (Quad.quad LibInt.intR (-2)) (cj π) l) = (Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) π j') (pw (Quad.quad LibInt.intR (-2)) (cj π) l')) : j = j' ∧ l = l' := by
  cases Nat.lt_or_ge j j' with
  | inl hlt =>
    exfalso
    match le_dest hlt with
    | ⟨d, hd⟩ =>
      have hl : l = l' + 1 + d := LibInt.nadd_right_cancel j (by rw [show l + j = j + l by lib_nat, hs, hd]; lib_nat)
      rw [hd, hl] at he
      exact distinct_lt3 P hp π hπ pπ j d l' he
  | inr hge =>
    cases Nat.lt_or_ge j' j with
    | inl hlt =>
      exfalso
      match le_dest hlt with
      | ⟨d, hd⟩ =>
        have hl : l' = l + 1 + d := LibInt.nadd_right_cancel j' (by rw [show l' + j' = j' + l' by lib_nat, ← hs, hd]; lib_nat)
        rw [hd, hl] at he
        exact distinct_lt3 P hp π hπ pπ j' d l he.symm
    | inr hle =>
      have e : j = j' := Nat.le_antisymm hle hge
      exact ⟨e, LibInt.nadd_right_cancel j (by rw [show l + j = j + l by lib_nat, hs, e]; lib_nat)⟩

def w3 (π : Int × Int) (x : Nat × Nat) : Int × Int := (Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) π x.1) (pw (Quad.quad LibInt.intR (-2)) (cj π) x.2)

theorem w3_PD (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim8 π) (k : Nat) (x : Nat × Nat) (hx : x.1 + x.2 = k) :
    PD8 ((P + 1 + 1) ^ k) (w3 π x) := by
  refine ⟨?_, ?_⟩
  · rw [← nrm_cast]; unfold w3
    rw [nrm_mul, nrm_pw, nrm_pw, nrm_cj, hπ, npow_add, hx]
  · exact prim8_mul _ _ (prim8_pow π pπ _) (prim8_pow (cj π) (prim8_cj π pπ) _)

/-- **c_W(p^k) = Σ_{j+l=k} W(π^j π̄^l).** -/
theorem cw3_pk (W : Int × Int → Int × Int) (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int)
    (hπ : nrm π = P + 1 + 1) (pπ : Prim8 π) (k : Nat) :
    cw8 W ((P + 1 + 1) ^ k) = sumK (Quad.quad LibInt.intR (-2)) (fun x => W (w3 π x)) (HeckePrime.Wl k) := by
  have hI : ∀ z, I8 ((P + 1 + 1) ^ k) z = sumK (Quad.quad LibInt.intR (-2)) (fun x => delta (Quad.quad LibInt.intR (-2)) z (w3 π x)) (HeckePrime.Wl k) := fun z => by
    by_cases hz : PD8 ((P + 1 + 1) ^ k) z
    · rw [I8_pos _ _ hz]
      have hN : nrm z = (P + 1 + 1) ^ k := Int.ofNat.inj (by rw [nrm_cast, hz.1])
      match primary_only3 P hp π hπ pπ k z hN hz.2 with
      | ⟨j, l, hjl, hx⟩ =>
        have hz' : z = w3 π (j, l) := hx
        rw [HeckePrime.sumK_ext_all (Quad.quad LibInt.intR (-2)) k _ (fun x => delta (Quad.quad LibInt.intR (-2)) x (j, l)) (fun x hxk => by
          unfold delta
          by_cases hxe : x = (j, l)
          · rw [ite_pos' _ _ hxe, ite_pos' _ _ (by rw [hz', hxe])]
          · rw [ite_neg' _ _ hxe, ite_neg' _ _ (fun e => hxe (by
              have d := distinct3 P hp π hπ pπ j l x.1 x.2 (hjl.trans hxk.symm) (hz'.symm.trans e)
              exact Prod.ext d.1.symm d.2.symm))]) _ (HeckePrime.allk_Wl k),
          HeckePrime.count_Wl (Quad.quad LibInt.intR (-2)) k j l hjl]
    · rw [I8_neg _ _ hz]
      rw [HeckePrime.sumK_ext_all (Quad.quad LibInt.intR (-2)) k _ (fun _ => (Quad.quad LibInt.intR (-2)).zero) (fun x hxk => by
        unfold delta; rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact w3_PD P π hπ pπ k x hxk))]) _ (HeckePrime.allk_Wl k)]
      exact (HeckePrime.zsumR (Quad.quad LibInt.intR (-2)) _).symm
  unfold cw8
  rw [sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun z => by rw [hI z, sumK_mul])]
  rw [sumK_swap]
  refine HeckePrime.sumK_ext_all (Quad.quad LibInt.intR (-2)) k _ _ (fun x hxk => ?_) _ (HeckePrime.allk_Wl k)
  have hb := abs_le8 (w3 π x) _ (w3_PD P π hπ pπ k x hxk).1
  exact HeckeMult.pick_box (Quad.quad LibInt.intR (-2)) (fun z => W z) (w3 π x) _ hb.1 hb.2

theorem W_pow3 (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Quad.quad LibInt.intR (-2)).mul α β) = (Quad.quad LibInt.intR (-2)).mul (W α) (W β))
    (hW1 : W (Quad.quad LibInt.intR (-2)).one = (Quad.quad LibInt.intR (-2)).one) (z : Int × Int) :
    ∀ j, W (pw (Quad.quad LibInt.intR (-2)) z j) = pw (Quad.quad LibInt.intR (-2)) (W z) j
  | 0 => hW1
  | j + 1 => by
    show W ((Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) z j) z) = (Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) (W z) j) (W z)
    rw [hW, W_pow3 W hW hW1 z j]

theorem cw3_pk' (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Quad.quad LibInt.intR (-2)).mul α β) = (Quad.quad LibInt.intR (-2)).mul (W α) (W β))
    (hW1 : W (Quad.quad LibInt.intR (-2)).one = (Quad.quad LibInt.intR (-2)).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim8 π) (k : Nat) :
    cw8 W ((P + 1 + 1) ^ k) = HeckePrime.Tg (Quad.quad LibInt.intR (-2)) (W π) (W (cj π)) k := by
  rw [cw3_pk P W hp π hπ pπ k]
  exact sumK_ext _ _ _ (fun x => by unfold w3; rw [hW, W_pow3 W hW hW1, W_pow3 W hW hW1]) _

/-- **The Hecke recursion for every multiplicative weight on ℤ[ω].** -/
theorem cw3_rec (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Quad.quad LibInt.intR (-2)).mul α β) = (Quad.quad LibInt.intR (-2)).mul (W α) (W β))
    (hW1 : W (Quad.quad LibInt.intR (-2)).one = (Quad.quad LibInt.intR (-2)).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim8 π) (k : Nat) :
    cw8 W ((P + 1 + 1) ^ (k + 2)) = (Quad.quad LibInt.intR (-2)).add
      ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).add (W π) (W (cj π))) (cw8 W ((P + 1 + 1) ^ (k + 1))))
      ((Quad.quad LibInt.intR (-2)).neg ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (W π) (W (cj π))) (cw8 W ((P + 1 + 1) ^ k)))) := by
  rw [cw3_pk' P W hW hW1 hp π hπ pπ, cw3_pk' P W hW hW1 hp π hπ pπ, cw3_pk' P W hW hW1 hp π hπ pπ, HeckePrime.Tg_rec]

end HeckePrime8

#print axioms HeckePrime8.pow_ne0
#print axioms HeckePrime8.nrm_pw
#print axioms HeckePrime8.npow_add
#print axioms HeckePrime8.en_ne
#print axioms HeckePrime8.cancel_pw3
#print axioms HeckePrime8.no_assoc3
#print axioms HeckePrime8.no_div3
#print axioms HeckePrime8.distinct_lt3
#print axioms HeckePrime8.distinct3
#print axioms HeckePrime8.w3_PD
#print axioms HeckePrime8.cw3_pk
#print axioms HeckePrime8.W_pow3
#print axioms HeckePrime8.cw3_pk'
#print axioms HeckePrime8.cw3_rec

lang/HeckePrimeW.lean

lean_all/lang/HeckePrimeW.lean

/-
  lang/HeckePrimeW — T50, cycle 595: **Hecke characters of ℚ(i): the coefficients at split prime powers and the Hecke recursion**,
  for every multiplicative weight W on ℤ[i] (W(αβ) = W(α)W(β), W(1) = 1; e.g. W(α) = α^k):
    cw_pk     c_W(p^k) = Σ_{(j,l) ∈ Wl k} W(π^j π̄^l)
    cw_pk'    c_W(p^k) = Σ_{j+l=k} W(π)^j W(π̄)^l
    cw_rec    c_W(p^(k+2)) = (W(π) + W(π̄)) c_W(p^(k+1)) − W(π)W(π̄) c_W(p^k)
  (for W(α) = α^k: eigenvalue π^k + π̄^k and W(π)W(π̄) = p^k — the weight k + 1 Hecke recursion).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.HeckeMultW
import lang.HeckePrime

namespace HeckePrime

open FTAChain DetChain LibInt GaussEuclid GaussCoprime GaussInt GaussFactor GaussUnique HeckeMult YMChain FiniteRecChain Frobenius LibGcd

def Tg {R : Type} (K : CRing R) (A B : R) (k : Nat) : R := sumK K (fun x => K.mul (pw K A x.1) (pw K B x.2)) (Wl k)

theorem Tg_succ {R : Type} (K : CRing R) (A B : R) (k : Nat) : Tg K A B (k + 1) = K.add (K.mul A (Tg K A B k)) (pw K B (k + 1)) := by
  show sumK K (fun x => K.mul (pw K A x.1) (pw K B x.2)) ((Wl k).map (fun x => (x.1 + 1, x.2)) ++ [(0, k + 1)]) = _
  rw [sumK_append, sumK_map]
  unfold Tg; rw [mul_sumK]
  have e : sumK K (fun x => K.mul (pw K A (((x.1 + 1, x.2) : Nat × Nat)).1) (pw K B (((x.1 + 1, x.2) : Nat × Nat)).2)) (Wl k) =
      sumK K (fun x => K.mul A (K.mul (pw K A x.1) (pw K B x.2))) (Wl k) :=
    sumK_ext K _ _ (fun x => by show K.mul (K.mul (pw K A x.1) A) (pw K B x.2) = _; lib_ring) _
  rw [e]
  show K.add _ (K.add (K.mul K.one (pw K B (k + 1))) K.zero) = _
  lib_ring

theorem Tg_rec {R : Type} (K : CRing R) (A B : R) (k : Nat) :
    Tg K A B (k + 2) = K.add (K.mul (K.add A B) (Tg K A B (k + 1))) (K.neg (K.mul (K.mul A B) (Tg K A B k))) := by
  have h1 := Tg_succ K A B (k + 1)
  have h2 := Tg_succ K A B k
  have h3 : pw K B (k + 1 + 1) = K.mul B (pw K B (k + 1)) := by show K.mul _ _ = _; lib_ring
  have h4 : pw K B (k + 1) = K.add (Tg K A B (k + 1)) (K.neg (K.mul A (Tg K A B k))) := by rw [h2]; lib_ring
  rw [show k + 2 = k + 1 + 1 from rfl, h1, h3, h4]; lib_ring

variable (P : Nat)

theorem cw_pk (W : Int × Int → Int × Int) (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (a b : Int)
    (hab : a * a + b * b = Int.ofNat (P + 1 + 1)) (hπ : Prim (a, b)) (k : Nat) :
    cw W ((P + 1 + 1) ^ k) = sumK (Cyclo4.cyc4 LibInt.intR) (fun x => W (w a b x)) (Wl k) := by
  have hI : ∀ z, I ((P + 1 + 1) ^ k) z = sumK (Cyclo4.cyc4 LibInt.intR) (fun x => delta (Cyclo4.cyc4 LibInt.intR) z (w a b x)) (Wl k) := fun z => by
    by_cases hz : PD ((P + 1 + 1) ^ k) z
    · rw [I_pos _ _ hz]
      have hN : gnorm (toGI z) = ipow (Int.ofNat (P + 1 + 1)) k := by rw [gnorm_toGI, hz.1, ipow_nat]
      match ThetaCoef.primary_only P hp a b hab hπ k (toGI z) hz.2 hN with
      | ⟨j, l, hjl, hx⟩ =>
        have hz' : z = w a b (j, l) := toGI_inj _ _ (by unfold w; rw [toGI_mul, toGI_pow, toGI_pow]; exact hx)
        rw [sumK_ext_all (Cyclo4.cyc4 LibInt.intR) k _ (fun x => delta (Cyclo4.cyc4 LibInt.intR) x (j, l)) (fun x hxk => by
          unfold delta
          by_cases hxe : x = (j, l)
          · rw [ite_pos' _ _ hxe, ite_pos' _ _ (by rw [hz', hxe])]
          · rw [ite_neg' _ _ hxe, ite_neg' _ _ (fun e => hxe (by
              have d := distinct P hp a b hab hπ j l x.1 x.2 (hjl.trans hxk.symm) (hz'.symm.trans e)
              exact Prod.ext d.1.symm d.2.symm))]) _ (allk_Wl k),
          count_Wl (Cyclo4.cyc4 LibInt.intR) k j l hjl]
    · rw [I_neg _ _ hz]
      rw [sumK_ext_all (Cyclo4.cyc4 LibInt.intR) k _ (fun _ => (Cyclo4.cyc4 LibInt.intR).zero) (fun x hxk => by
        unfold delta; rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact w_PD P hp a b hab hπ k x hxk))]) _ (allk_Wl k)]
      exact (zsumR (Cyclo4.cyc4 LibInt.intR) _).symm
  unfold cw
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun z => by rw [hI z, sumK_mul])]
  rw [sumK_swap]
  refine sumK_ext_all (Cyclo4.cyc4 LibInt.intR) k _ _ (fun x hxk => ?_) _ (allk_Wl k)
  have hb := abs_le (w a b x) _ (w_PD P hp a b hab hπ k x hxk).1
  exact pick_box (Cyclo4.cyc4 LibInt.intR) (fun z => W z) (w a b x) _ hb.1 hb.2

theorem W_pow (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Cyclo4.cyc4 LibInt.intR).mul α β) = (Cyclo4.cyc4 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Cyclo4.cyc4 LibInt.intR).one = (Cyclo4.cyc4 LibInt.intR).one) (z : Int × Int) :
    ∀ j, W (pw (Cyclo4.cyc4 LibInt.intR) z j) = pw (Cyclo4.cyc4 LibInt.intR) (W z) j
  | 0 => hW1
  | j + 1 => by
    show W ((Cyclo4.cyc4 LibInt.intR).mul (pw (Cyclo4.cyc4 LibInt.intR) z j) z) = (Cyclo4.cyc4 LibInt.intR).mul (pw (Cyclo4.cyc4 LibInt.intR) (W z) j) (W z)
    rw [hW, W_pow W hW hW1 z j]

theorem cw_pk' (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Cyclo4.cyc4 LibInt.intR).mul α β) = (Cyclo4.cyc4 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Cyclo4.cyc4 LibInt.intR).one = (Cyclo4.cyc4 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (a b : Int) (hab : a * a + b * b = Int.ofNat (P + 1 + 1)) (hπ : Prim (a, b)) (k : Nat) :
    cw W ((P + 1 + 1) ^ k) = Tg (Cyclo4.cyc4 LibInt.intR) (W (a, b)) (W (a, -b)) k := by
  rw [cw_pk P W hp a b hab hπ k]
  exact sumK_ext _ _ _ (fun x => by unfold w; rw [hW, W_pow W hW hW1, W_pow W hW hW1]) _

/-- **The Hecke recursion for every multiplicative weight.** -/
theorem cw_rec (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Cyclo4.cyc4 LibInt.intR).mul α β) = (Cyclo4.cyc4 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Cyclo4.cyc4 LibInt.intR).one = (Cyclo4.cyc4 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (a b : Int) (hab : a * a + b * b = Int.ofNat (P + 1 + 1)) (hπ : Prim (a, b)) (k : Nat) :
    cw W ((P + 1 + 1) ^ (k + 2)) = (Cyclo4.cyc4 LibInt.intR).add
      ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add (W (a, b)) (W (a, -b))) (cw W ((P + 1 + 1) ^ (k + 1))))
      ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (W (a, b)) (W (a, -b))) (cw W ((P + 1 + 1) ^ k)))) := by
  rw [cw_pk' P W hW hW1 hp a b hab hπ, cw_pk' P W hW hW1 hp a b hab hπ, cw_pk' P W hW hW1 hp a b hab hπ, Tg_rec]

end HeckePrime

#print axioms HeckePrime.Tg_succ
#print axioms HeckePrime.Tg_rec
#print axioms HeckePrime.cw_pk
#print axioms HeckePrime.W_pow
#print axioms HeckePrime.cw_pk'
#print axioms HeckePrime.cw_rec

lang/HeckeUniq.lean

lean_all/lang/HeckeUniq.lean

/-
  lang/HeckeUniq — T51 (tool for G4 (b2)), cycle 799: **normalized Hecke eigen-sequences are determined by their prime coefficients**.
  Two sequences a, b : ℕ → ℤ with a(1) = b(1) = 1, both multiplicative on p^(k+1)·m (p prime, p ∤ m), both satisfying the
  Euler recursion c(p^(k+2)) = c(p) c(p^(k+1)) − w(p) c(p^k) with the same weight w (w(p) = p at good primes, 0 at bad ones),
  and a(p) = b(p) at every prime p, agree at every n ≥ 1:
    pp_eq      a(p^k) = b(p^k) for all k
    uniq       a(n) = b(n) for all n ≥ 1                     (strong induction by fuel, G4b1.factor)
  So a q-series identity Σ a(n)qⁿ = Σ b(n)qⁿ reduces to the Hecke structure of both sides and the equality of a(p).
  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.G4b1

namespace HeckeUniq

open FTAChain Frobenius

theorem pp_eq (a b w : Nat → Int) (P : Nat) (ha1 : a 1 = 1) (hb1 : b 1 = 1) (h1 : P ^ 1 = P) (hab : a P = b P)
    (hrA : ∀ k, a (P ^ (k + 2)) = a P * a (P ^ (k + 1)) - w P * a (P ^ k))
    (hrB : ∀ k, b (P ^ (k + 2)) = b P * b (P ^ (k + 1)) - w P * b (P ^ k)) :
    ∀ k, a (P ^ k) = b (P ^ k) ∧ a (P ^ (k + 1)) = b (P ^ (k + 1))
  | 0 => ⟨by show a 1 = b 1; rw [ha1, hb1], by rw [h1]; exact hab⟩
  | k + 1 => by
    match pp_eq a b w P ha1 hb1 h1 hab hrA hrB k with
    | ⟨e0, e1⟩ => exact ⟨e1, by rw [hrA k, hrB k, e0, e1, hab]⟩

/-- **Uniqueness**: equal prime coefficients ⇒ equal sequences. -/
theorem uniq (a b w : Nat → Int) (ha1 : a 1 = 1) (hb1 : b 1 = 1)
    (hmA : ∀ l k m, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) → (Dv (l + 2) (m + 1) → False) →
      a ((l + 2) ^ (k + 1) * (m + 1)) = a ((l + 2) ^ (k + 1)) * a (m + 1))
    (hmB : ∀ l k m, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) → (Dv (l + 2) (m + 1) → False) →
      b ((l + 2) ^ (k + 1) * (m + 1)) = b ((l + 2) ^ (k + 1)) * b (m + 1))
    (hrA : ∀ l k, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) →
      a ((l + 2) ^ (k + 2)) = a (l + 2) * a ((l + 2) ^ (k + 1)) - w (l + 2) * a ((l + 2) ^ k))
    (hrB : ∀ l k, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) →
      b ((l + 2) ^ (k + 2)) = b (l + 2) * b ((l + 2) ^ (k + 1)) - w (l + 2) * b ((l + 2) ^ k))
    (hab : ∀ l, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) → a (l + 2) = b (l + 2)) :
    ∀ i, a (i + 1) = b (i + 1) := by
  have hpp : ∀ l k, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) → a ((l + 2) ^ (k + 1)) = b ((l + 2) ^ (k + 1)) := fun l k hp => by
    have h1 : (l + 2) ^ 1 = l + 2 := by show 1 * (l + 2) = l + 2; exact (nmul_comm 1 (l + 2)).trans (nmul_one _)
    exact (pp_eq a b w (l + 2) ha1 hb1 h1 (hab l hp) (hrA l · hp) (hrB l · hp) k).2
  have main : ∀ fuel i, Le i fuel → a (i + 1) = b (i + 1) := by
    intro fuel
    induction fuel with
    | zero =>
      intro i hle
      match hle with
      | ⟨c, hc⟩ => rw [G4b1.az i c hc, ha1, hb1]
    | succ f ih =>
      intro i hle
      cases i with
      | zero => rw [ha1, hb1]
      | succ i' =>
        match G4b1.factor i' with
        | ⟨l, k, m, hp, hn, hnd, ⟨c', hc'⟩⟩ =>
          match hle with
          | ⟨c, hc⟩ =>
            have e2 := ih m ⟨c' + c, Nat.succ.inj (show m + (c' + c) + 1 = f + 1 by rw [← hc, ← hc']; lib_nat)⟩
            show a (i' + 2) = b (i' + 2)
            rw [hn, hmA l k m hp hnd, hmB l k m hp hnd, hpp l k hp, e2]
  exact fun i => main i i ⟨0, rfl⟩

end HeckeUniq

#print axioms HeckeUniq.pp_eq
#print axioms HeckeUniq.uniq

lang/IdemAtom1.lean

lean_all/lang/IdemAtom1.lean

/-
  lang/IdemAtom1 — T49 (Kummer step, abstract): **iterates of a ring endomorphism and a greedy atom for an orbit of
  idempotents**. E a commutative ring with decidable equality, φ : E → E additive, multiplicative, φ(1) = 1.
    it, it_comp                φ^k, φ^(b+a) = φ^a ∘ φ^b
    phi_zero, it_add, it_mul, it_one, it_zero, it_neg, it_idem
    gr, gr_spec                u_0 = 1, u_(k+1) = u_k φ^k(e) if that is nonzero, else u_k (1 − φ^k(e)); every u_k is a
                               nonzero idempotent with u_k φ^i(e) ∈ {u_k, 0} for i < k
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import FiniteRecChain
import lib.RingTac

namespace IdemAtom1

open FTAChain DetChain FiniteRecChain

variable {S : Type} (E : CRing S) (phi : S → S)

structure Endo : Prop where
  hadd : ∀ a b, phi (E.add a b) = E.add (phi a) (phi b)
  hmul : ∀ a b, phi (E.mul a b) = E.mul (phi a) (phi b)
  hone : phi E.one = E.one

def it : Nat → S → S
  | 0, a => a
  | k + 1, a => phi (it k a)

theorem it_comp (b : Nat) (x : S) : ∀ a, it phi (b + a) x = it phi a (it phi b x)
  | 0 => rfl
  | a + 1 => by
    show phi (it phi (b + a) x) = phi (it phi a (it phi b x))
    rw [it_comp b x a]

theorem phi_zero (H : Endo E phi) : phi E.zero = E.zero := by
  have h := H.hadd E.zero E.zero
  rw [E.zero_add] at h
  calc phi E.zero = E.add (E.add (phi E.zero) (phi E.zero)) (E.neg (phi E.zero)) := by lib_ring
    _ = E.add (phi E.zero) (E.neg (phi E.zero)) := by rw [← h]
    _ = E.zero := by lib_ring

theorem it_add (H : Endo E phi) (a b : S) : ∀ k, it phi k (E.add a b) = E.add (it phi k a) (it phi k b)
  | 0 => rfl
  | k + 1 => by
    show phi (it phi k (E.add a b)) = E.add (phi (it phi k a)) (phi (it phi k b))
    rw [it_add H a b k, H.hadd]

theorem it_mul (H : Endo E phi) (a b : S) : ∀ k, it phi k (E.mul a b) = E.mul (it phi k a) (it phi k b)
  | 0 => rfl
  | k + 1 => by
    show phi (it phi k (E.mul a b)) = E.mul (phi (it phi k a)) (phi (it phi k b))
    rw [it_mul H a b k, H.hmul]

theorem it_one (H : Endo E phi) : ∀ k, it phi k E.one = E.one
  | 0 => rfl
  | k + 1 => by
    show phi (it phi k E.one) = E.one
    rw [it_one H k, H.hone]

theorem it_zero (H : Endo E phi) : ∀ k, it phi k E.zero = E.zero
  | 0 => rfl
  | k + 1 => by
    show phi (it phi k E.zero) = E.zero
    rw [it_zero H k, phi_zero E phi H]

theorem it_neg (H : Endo E phi) (a : S) (k : Nat) : it phi k (E.neg a) = E.neg (it phi k a) := by
  have h : E.add (it phi k a) (it phi k (E.neg a)) = E.zero := by
    rw [← it_add E phi H, E.add_neg, it_zero E phi H]
  calc it phi k (E.neg a) = E.add (E.add (it phi k a) (it phi k (E.neg a))) (E.neg (it phi k a)) := by lib_ring
    _ = E.add E.zero (E.neg (it phi k a)) := by rw [h]
    _ = E.neg (it phi k a) := by lib_ring

theorem it_idem (H : Endo E phi) (g : S) (hg : E.mul g g = g) (k : Nat) :
    E.mul (it phi k g) (it phi k g) = it phi k g := by
  rw [← it_mul E phi H, hg]

variable [DecidableEq S]

/-- The greedy atom. -/
def gr (e : S) : Nat → S
  | 0 => E.one
  | k + 1 =>
    if E.mul (gr e k) (it phi k e) = E.zero then E.mul (gr e k) (E.add E.one (E.neg (it phi k e)))
    else E.mul (gr e k) (it phi k e)

def Spec (e : S) (k : Nat) : Prop :=
  gr E phi e k ≠ E.zero ∧ E.mul (gr E phi e k) (gr E phi e k) = gr E phi e k ∧
    ∀ i, i < k → (E.mul (gr E phi e k) (it phi i e) = gr E phi e k ∨ E.mul (gr E phi e k) (it phi i e) = E.zero)

theorem lt_succ_cases (i k : Nat) (h : i < k + 1) : i < k ∨ i = k :=
  match Nat.lt_or_ge i k with
  | Or.inl hl => Or.inl hl
  | Or.inr hg => Or.inr (Nat.le_antisymm (Nat.le_of_lt_succ h) hg)

/-- **Every u_k is a nonzero idempotent, an atom for φ^i(e), i < k.** -/
theorem gr_spec (H : Endo E phi) (h10 : E.one ≠ E.zero) (e : S) (he : E.mul e e = e) : ∀ k, Spec E phi e k
  | 0 => ⟨h10, E.one_mul E.one, fun _ h => absurd h (Nat.not_lt_zero _)⟩
  | k + 1 => by
    match gr_spec H h10 e he k with
    | ⟨hv0, hvv, hat⟩ =>
      have hff := it_idem E phi H e he k
      by_cases hvf : E.mul (gr E phi e k) (it phi k e) = E.zero
      · have hg : gr E phi e (k + 1) = E.mul (gr E phi e k) (E.add E.one (E.neg (it phi k e))) := ite_pos' _ _ hvf
        have hgv : gr E phi e (k + 1) = gr E phi e k := by
          rw [hg]
          calc E.mul (gr E phi e k) (E.add E.one (E.neg (it phi k e)))
              = E.add (gr E phi e k) (E.neg (E.mul (gr E phi e k) (it phi k e))) := by lib_ring
            _ = E.add (gr E phi e k) (E.neg E.zero) := by rw [hvf]
            _ = gr E phi e k := by lib_ring
        refine ⟨by rw [hgv]; exact hv0, by rw [hgv]; exact hvv, fun i hi => ?_⟩
        rw [hgv]
        match lt_succ_cases i k hi with
        | Or.inl hl => exact hat i hl
        | Or.inr he' => rw [he']; exact Or.inr hvf
      · have hg : gr E phi e (k + 1) = E.mul (gr E phi e k) (it phi k e) := ite_neg' _ _ hvf
        refine ⟨by rw [hg]; exact hvf, ?_, fun i hi => ?_⟩
        · rw [hg]
          calc E.mul (E.mul (gr E phi e k) (it phi k e)) (E.mul (gr E phi e k) (it phi k e))
              = E.mul (E.mul (gr E phi e k) (gr E phi e k)) (E.mul (it phi k e) (it phi k e)) := by lib_ring
            _ = E.mul (gr E phi e k) (it phi k e) := by rw [hvv, hff]
        · rw [hg]
          match lt_succ_cases i k hi with
          | Or.inl hl =>
            match hat i hl with
            | Or.inl h1 =>
              left
              calc E.mul (E.mul (gr E phi e k) (it phi k e)) (it phi i e)
                  = E.mul (E.mul (gr E phi e k) (it phi i e)) (it phi k e) := by lib_ring
                _ = E.mul (gr E phi e k) (it phi k e) := by rw [h1]
            | Or.inr h0 =>
              right
              calc E.mul (E.mul (gr E phi e k) (it phi k e)) (it phi i e)
                  = E.mul (E.mul (gr E phi e k) (it phi i e)) (it phi k e) := by lib_ring
                _ = E.mul E.zero (it phi k e) := by rw [h0]
                _ = E.zero := by lib_ring
          | Or.inr he' =>
            rw [he']
            left
            calc E.mul (E.mul (gr E phi e k) (it phi k e)) (it phi k e)
                = E.mul (gr E phi e k) (E.mul (it phi k e) (it phi k e)) := by lib_ring
              _ = E.mul (gr E phi e k) (it phi k e) := by rw [hff]

end IdemAtom1

#print axioms IdemAtom1.it
#print axioms IdemAtom1.it_comp
#print axioms IdemAtom1.phi_zero
#print axioms IdemAtom1.it_add
#print axioms IdemAtom1.it_mul
#print axioms IdemAtom1.it_one
#print axioms IdemAtom1.it_zero
#print axioms IdemAtom1.it_neg
#print axioms IdemAtom1.it_idem
#print axioms IdemAtom1.gr
#print axioms IdemAtom1.gr_spec

lang/IdemAtom2.lean

lean_all/lang/IdemAtom2.lean

/-
  lang/IdemAtom2 — T49 (Kummer step, abstract): **an idempotent whose whole φ-orbit dominates a nonzero element is 1**,
  when φ has period n (φ^n = id) and the only φ-fixed idempotents are 0 and 1.
    prodI, prodQ          Π_{i<k} φ^i(g), Π_{i<k} φ^(i+1)(g)
    phi_prod, prod_succ   φ(Π_{i<k} φ^i g) = Π_{i<k} φ^(i+1) g;  Π_{i<k+1} φ^i g = g·Π_{i<k} φ^(i+1) g
    prod_idem, u_prod     products of idempotents are idempotent; u·φ^i g = u (i < N) ⇒ u·Π_{i<k} φ^i g = u (k ≤ N)
    g_one                 u ≠ 0, u·φ^i g = u for all i < n ⇒ g = 1
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.IdemAtom1

namespace IdemAtom2

open FTAChain DetChain FiniteRecChain IdemAtom1

variable {S : Type} (E : CRing S) (phi : S → S)

def prodI (g : S) : Nat → S
  | 0 => E.one
  | k + 1 => E.mul (prodI g k) (it phi k g)

def prodQ (g : S) : Nat → S
  | 0 => E.one
  | k + 1 => E.mul (prodQ g k) (it phi (k + 1) g)

theorem phi_prod (H : Endo E phi) (g : S) : ∀ k, phi (prodI E phi g k) = prodQ E phi g k
  | 0 => H.hone
  | k + 1 => by
    show phi (E.mul (prodI E phi g k) (it phi k g)) = E.mul (prodQ E phi g k) (it phi (k + 1) g)
    rw [H.hmul, phi_prod H g k]
    rfl

theorem prod_succ (g : S) : ∀ k, prodI E phi g (k + 1) = E.mul g (prodQ E phi g k)
  | 0 => by
    show E.mul E.one g = E.mul g E.one
    rw [E.mul_comm]
  | k + 1 => by
    show E.mul (prodI E phi g (k + 1)) (it phi (k + 1) g) = E.mul g (E.mul (prodQ E phi g k) (it phi (k + 1) g))
    rw [prod_succ g k, E.mul_assoc]

theorem prod_idem (H : Endo E phi) (g : S) (hg : E.mul g g = g) : ∀ k, E.mul (prodI E phi g k) (prodI E phi g k) = prodI E phi g k
  | 0 => E.one_mul E.one
  | k + 1 => by
    show E.mul (E.mul (prodI E phi g k) (it phi k g)) (E.mul (prodI E phi g k) (it phi k g)) = E.mul (prodI E phi g k) (it phi k g)
    calc E.mul (E.mul (prodI E phi g k) (it phi k g)) (E.mul (prodI E phi g k) (it phi k g))
        = E.mul (E.mul (prodI E phi g k) (prodI E phi g k)) (E.mul (it phi k g) (it phi k g)) := by lib_ring
      _ = E.mul (prodI E phi g k) (it phi k g) := by rw [prod_idem H g hg k, it_idem E phi H g hg k]

theorem u_prod (g u : S) (N : Nat) (hu : ∀ i, i < N → E.mul u (it phi i g) = u) : ∀ k, k ≤ N → E.mul u (prodI E phi g k) = u
  | 0, _ => by
    show E.mul u E.one = u
    rw [E.mul_comm, E.one_mul]
  | k + 1, h => by
    show E.mul u (E.mul (prodI E phi g k) (it phi k g)) = u
    rw [← E.mul_assoc, u_prod g u N hu k (Nat.le_of_succ_le h), hu k h]

/-- **g = 1.** -/
theorem g_one (H : Endo E phi) (j' : Nat) (hper : ∀ a, it phi (j' + 1 + 1) a = a)
    (hfixid : ∀ f, phi f = f → E.mul f f = f → f = E.zero ∨ f = E.one)
    (g : S) (hg : E.mul g g = g) (u : S) (hu0 : u ≠ E.zero)
    (hu : ∀ i, i < j' + 1 + 1 → E.mul u (it phi i g) = u) : g = E.one := by
  have hfix : phi (prodI E phi g (j' + 1 + 1)) = prodI E phi g (j' + 1 + 1) := by
    rw [phi_prod E phi H]
    show E.mul (prodQ E phi g (j' + 1)) (it phi (j' + 1 + 1) g) = prodI E phi g (j' + 1 + 1)
    rw [hper, E.mul_comm, ← prod_succ]
  rcases hfixid _ hfix (prod_idem E phi H g hg (j' + 1 + 1)) with h0 | h1
  · exact absurd (calc u = E.mul u (prodI E phi g (j' + 1 + 1)) := (u_prod E phi g u _ hu _ (Nat.le_refl _)).symm
        _ = E.mul u E.zero := by rw [h0]
        _ = E.zero := DetChain.mul_zero E u) hu0
  · calc g = E.mul g E.one := by rw [E.mul_comm, E.one_mul]
      _ = E.mul g (E.mul g (prodQ E phi g (j' + 1))) := by rw [← prod_succ, h1]
      _ = E.mul (E.mul g g) (prodQ E phi g (j' + 1)) := by lib_ring
      _ = E.mul g (prodQ E phi g (j' + 1)) := by rw [hg]
      _ = E.one := by rw [← prod_succ, h1]

end IdemAtom2

#print axioms IdemAtom2.prodI
#print axioms IdemAtom2.prodQ
#print axioms IdemAtom2.phi_prod
#print axioms IdemAtom2.prod_succ
#print axioms IdemAtom2.prod_idem
#print axioms IdemAtom2.u_prod
#print axioms IdemAtom2.g_one

lang/IdemAtom3.lean

lean_all/lang/IdemAtom3.lean

/-
  lang/IdemAtom3 — T49 (Kummer step, abstract): **a nonzero idempotent orthogonal to its φ-conjugates**. Let φ be a ring
  endomorphism of E with φ^n = id, n = j' + 2 prime, whose only fixed idempotents are 0 and 1. If e is an idempotent,
  e ≠ 0, 1, then the greedy atom u of lang/IdemAtom1 satisfies u·φ^k(u) = 0 for 0 < k < n: otherwise the pattern
  i ↦ [u φ^i(e) = u] is invariant under the shift by k, hence constant (k is invertible mod n), and lang/IdemAtom2
  gives e = 1 or 1 − e = 1.
    it_period, it_md, transport, transport0, shift_iff, shift_n, reach, atom_orth
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.IdemAtom2
import lang.ZMod
import lib.Semi

namespace IdemAtom3

open FTAChain ZMod DetChain FiniteRecChain IdemAtom1 IdemAtom2

variable {S : Type} (E : CRing S) (phi : S → S)

theorem it_period (j' : Nat) (hper : ∀ a, it phi (j' + 1 + 1) a = a) (a : Nat) (x : S) :
    ∀ q, it phi (a + q * (j' + 1 + 1)) x = it phi a x
  | 0 => by rw [Nat.zero_mul, Nat.add_zero]
  | q + 1 => by
    rw [show a + (q + 1) * (j' + 1 + 1) = (j' + 1 + 1) + (a + q * (j' + 1 + 1)) by lib_nat, it_comp, hper,
      it_period j' hper a x q]

theorem it_md (j' : Nat) (hper : ∀ a, it phi (j' + 1 + 1) a = a) (n : Nat) (x : S) :
    it phi n x = it phi (md (j' + 1) n) x := by
  match md_spec (j' + 1) n with
  | ⟨q, hq⟩ =>
    calc it phi n x = it phi (md (j' + 1) n + q * (j' + 1 + 1)) x := by rw [← hq]
      _ = it phi (md (j' + 1) n) x := it_period phi j' hper _ x q

theorem transport (H : Endo E phi) (j' : Nat) (hper : ∀ a, it phi (j' + 1 + 1) a = a) (u g : S) (i jj : Zp (j' + 1))
    (h : E.mul u (it phi i.val g) = u) :
    E.mul (it phi jj.val u) (it phi ((ring (j' + 1)).add i jj).val g) = it phi jj.val u := by
  have h2 := congrArg (it phi jj.val) h
  rw [it_mul E phi H, ← it_comp, it_md phi j' hper (i.val + jj.val)] at h2
  exact h2

theorem transport0 (H : Endo E phi) (j' : Nat) (hper : ∀ a, it phi (j' + 1 + 1) a = a) (u g : S) (i jj : Zp (j' + 1))
    (h : E.mul u (it phi i.val g) = E.zero) :
    E.mul (it phi jj.val u) (it phi ((ring (j' + 1)).add i jj).val g) = E.zero := by
  have h2 := congrArg (it phi jj.val) h
  rw [it_mul E phi H, ← it_comp, it_md phi j' hper (i.val + jj.val), it_zero E phi H] at h2
  exact h2

variable [DecidableEq S]

theorem shift_iff (H : Endo E phi) (j' : Nat) (hper : ∀ a, it phi (j' + 1 + 1) a = a) (e u : S)
    (hat : ∀ i, i < j' + 1 + 1 → (E.mul u (it phi i e) = u ∨ E.mul u (it phi i e) = E.zero))
    (jj : Zp (j' + 1)) (hne : E.mul u (it phi jj.val u) ≠ E.zero) (i : Zp (j' + 1)) :
    (E.mul u (it phi i.val e) = u) ↔ (E.mul u (it phi ((ring (j' + 1)).add i jj).val e) = u) := by
  constructor
  · intro hi
    by_cases h2 : E.mul u (it phi ((ring (j' + 1)).add i jj).val e) = u
    · exact h2
    · exfalso
      have hz := (hat _ ((ring (j' + 1)).add i jj).property).resolve_left h2
      have ht := transport E phi H j' hper u e i jj hi
      apply hne
      calc E.mul u (it phi jj.val u)
          = E.mul u (E.mul (it phi jj.val u) (it phi ((ring (j' + 1)).add i jj).val e)) := by rw [ht]
        _ = E.mul (E.mul u (it phi ((ring (j' + 1)).add i jj).val e)) (it phi jj.val u) := by lib_ring
        _ = E.zero := by rw [hz]; lib_ring
  · intro hi2
    by_cases h1 : E.mul u (it phi i.val e) = u
    · exact h1
    · exfalso
      have hz := (hat _ i.property).resolve_left h1
      have ht := transport0 E phi H j' hper u e i jj hz
      apply hne
      calc E.mul u (it phi jj.val u)
          = E.mul (E.mul u (it phi ((ring (j' + 1)).add i jj).val e)) (it phi jj.val u) := by rw [hi2]
        _ = E.mul u (E.mul (it phi jj.val u) (it phi ((ring (j' + 1)).add i jj).val e)) := by lib_ring
        _ = E.zero := by rw [ht]; lib_ring

theorem shift_n (H : Endo E phi) (j' : Nat) (hper : ∀ a, it phi (j' + 1 + 1) a = a) (e u : S)
    (hat : ∀ i, i < j' + 1 + 1 → (E.mul u (it phi i e) = u ∨ E.mul u (it phi i e) = E.zero))
    (jj : Zp (j' + 1)) (hne : E.mul u (it phi jj.val u) ≠ E.zero) (i : Zp (j' + 1)) :
    ∀ n, (E.mul u (it phi i.val e) = u) ↔
      (E.mul u (it phi ((ring (j' + 1)).add i (Frobenius.nsm (ring (j' + 1)) n jj)).val e) = u)
  | 0 => by
    have e0 : (ring (j' + 1)).add i (ring (j' + 1)).zero = i := by lib_ring
    exact Iff.of_eq (congrArg (fun z : Zp (j' + 1) => E.mul u (it phi z.val e) = u) e0.symm)
  | n + 1 => by
    have h := shift_iff E phi H j' hper e u hat jj hne ((ring (j' + 1)).add i (Frobenius.nsm (ring (j' + 1)) n jj))
    have e1 : (ring (j' + 1)).add ((ring (j' + 1)).add i (Frobenius.nsm (ring (j' + 1)) n jj)) jj
        = (ring (j' + 1)).add i ((ring (j' + 1)).add (Frobenius.nsm (ring (j' + 1)) n jj) jj) := by lib_ring
    exact (shift_n H j' hper e u hat jj hne i n).trans
      (h.trans (Iff.of_eq (congrArg (fun z : Zp (j' + 1) => E.mul u (it phi z.val e) = u) e1)))

theorem reach (j' : Nat) (hpr : ∀ d, Dv d (j' + 1 + 1) → d = 1 ∨ d = j' + 1 + 1) (jj : Zp (j' + 1))
    (hjj : jj ≠ (ring (j' + 1)).zero) (tgt : Zp (j' + 1)) :
    ∃ n, (ring (j' + 1)).add (ring (j' + 1)).zero (Frobenius.nsm (ring (j' + 1)) n jj) = tgt := by
  have hinv := ZMod.inv_exists j' hpr jj hjj
  refine ⟨((ring (j' + 1)).mul tgt (Frobenius.pw (ring (j' + 1)) jj j')).val, ?_⟩
  rw [Frobenius.nsm_eq_mul (ring (j' + 1)), every_elem]
  calc (ring (j' + 1)).add (ring (j' + 1)).zero
        ((ring (j' + 1)).mul ((ring (j' + 1)).mul tgt (Frobenius.pw (ring (j' + 1)) jj j')) jj)
      = (ring (j' + 1)).mul tgt ((ring (j' + 1)).mul (Frobenius.pw (ring (j' + 1)) jj j') jj) := by lib_ring
    _ = (ring (j' + 1)).mul tgt (ring (j' + 1)).one := by rw [hinv]
    _ = tgt := by lib_ring

/-- **A nonzero idempotent orthogonal to its conjugates.** -/
theorem atom_orth (H : Endo E phi) (h10 : E.one ≠ E.zero) (j' : Nat) (hper : ∀ a, it phi (j' + 1 + 1) a = a)
    (hfixid : ∀ f, phi f = f → E.mul f f = f → f = E.zero ∨ f = E.one)
    (hpr : ∀ d, Dv d (j' + 1 + 1) → d = 1 ∨ d = j' + 1 + 1)
    (e : S) (he : E.mul e e = e) (he0 : e ≠ E.zero) (he1 : e ≠ E.one) :
    ∃ u, u ≠ E.zero ∧ E.mul u u = u ∧ ∀ k, 0 < k → k < j' + 1 + 1 → E.mul u (it phi k u) = E.zero := by
  match gr_spec E phi H h10 e he (j' + 1 + 1) with
  | ⟨hu0, huu, hat⟩ =>
    refine ⟨gr E phi e (j' + 1 + 1), hu0, huu, fun k hk0 hk => ?_⟩
    by_cases hne : E.mul (gr E phi e (j' + 1 + 1)) (it phi k (gr E phi e (j' + 1 + 1))) = E.zero
    · exact hne
    · exfalso
      have hkv : (mk (j' + 1) k).val = k := md_small (j' + 1) k hk
      have hne' : E.mul (gr E phi e (j' + 1 + 1)) (it phi (mk (j' + 1) k).val (gr E phi e (j' + 1 + 1))) ≠ E.zero := by
        rw [hkv]; exact hne
      have hjj : mk (j' + 1) k ≠ (ring (j' + 1)).zero := fun h => by
        have h0 : (mk (j' + 1) k).val = 0 := congrArg Subtype.val h
        rw [hkv] at h0
        rw [h0] at hk0
        exact Nat.lt_irrefl 0 hk0
      have key : ∀ tgt : Zp (j' + 1),
          (E.mul (gr E phi e (j' + 1 + 1)) (it phi ((ring (j' + 1)).zero).val e) = gr E phi e (j' + 1 + 1)) ↔
          (E.mul (gr E phi e (j' + 1 + 1)) (it phi tgt.val e) = gr E phi e (j' + 1 + 1)) := fun tgt =>
        match reach j' hpr (mk (j' + 1) k) hjj tgt with
        | ⟨n, hn⟩ => by
          have h := shift_n E phi H j' hper e _ hat (mk (j' + 1) k) hne' (ring (j' + 1)).zero n
          rw [hn] at h
          exact h
      by_cases h0 : E.mul (gr E phi e (j' + 1 + 1)) (it phi ((ring (j' + 1)).zero).val e) = gr E phi e (j' + 1 + 1)
      · have hall : ∀ i, i < j' + 1 + 1 → E.mul (gr E phi e (j' + 1 + 1)) (it phi i e) = gr E phi e (j' + 1 + 1) :=
          fun i hi => by
            have h1 := (key (mk (j' + 1) i)).mp h0
            rw [show (mk (j' + 1) i).val = i from md_small (j' + 1) i hi] at h1
            exact h1
        exact he1 (g_one E phi H j' hper hfixid e he _ hu0 hall)
      · have hidem : E.mul (E.add E.one (E.neg e)) (E.add E.one (E.neg e)) = E.add E.one (E.neg e) := by
          calc E.mul (E.add E.one (E.neg e)) (E.add E.one (E.neg e))
              = E.add (E.add E.one (E.neg (E.add e e))) (E.mul e e) := by lib_ring
            _ = E.add (E.add E.one (E.neg (E.add e e))) e := by rw [he]
            _ = E.add E.one (E.neg e) := by lib_ring
        have hall : ∀ i, i < j' + 1 + 1 →
            E.mul (gr E phi e (j' + 1 + 1)) (it phi i (E.add E.one (E.neg e))) = gr E phi e (j' + 1 + 1) := fun i hi => by
          have hnf : ¬ (E.mul (gr E phi e (j' + 1 + 1)) (it phi i e) = gr E phi e (j' + 1 + 1)) := fun hf =>
            h0 ((key (mk (j' + 1) i)).mpr (by
              rw [show (mk (j' + 1) i).val = i from md_small (j' + 1) i hi]
              exact hf))
          have hz := (hat i hi).resolve_left hnf
          rw [it_add E phi H, it_one E phi H, it_neg E phi H]
          calc E.mul (gr E phi e (j' + 1 + 1)) (E.add E.one (E.neg (it phi i e)))
              = E.add (gr E phi e (j' + 1 + 1)) (E.neg (E.mul (gr E phi e (j' + 1 + 1)) (it phi i e))) := by lib_ring
            _ = gr E phi e (j' + 1 + 1) := by rw [hz]; lib_ring
        have hg1 := g_one E phi H j' hper hfixid (E.add E.one (E.neg e)) hidem _ hu0 hall
        apply he0
        calc e = E.add E.one (E.neg (E.add E.one (E.neg e))) := by lib_ring
          _ = E.add E.one (E.neg E.one) := by rw [hg1]
          _ = E.zero := by lib_ring

end IdemAtom3

#print axioms IdemAtom3.it_period
#print axioms IdemAtom3.it_md
#print axioms IdemAtom3.transport
#print axioms IdemAtom3.transport0
#print axioms IdemAtom3.shift_iff
#print axioms IdemAtom3.shift_n
#print axioms IdemAtom3.reach
#print axioms IdemAtom3.atom_orth

lang/Inert11.lean

lean_all/lang/Inert11.lean

/-
  lang/Inert11 — T48/T50, cycle 654: **121b's Hecke character at inert primes** p ≡ 2, 6, 7, 8, 10 (mod 11):
    en_mod11       N(a + bθ) ≡ (a + 6b)² (mod 11) (12N = (a + 6b)² + 11a²) is never a non-square mod 11
    cw11_zero      c_W(n) = 0 for n a non-square mod 11
    inert_div, factor_inert, prim_inert   p stays prime; the H11-elements of norm p^(2k) are (−p)^k
    cw11_even, cw11_odd                   c_W(p^(2k)) = W((−p)^k), c_W(p^(2k+1)) = 0
  (Port of Inert7.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.HeckePrime11

namespace Inert11

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius Euc11 Prim11 Uniq11 Prime11 HeckeMult11

variable (P : Nat)

theorem en_mod11 (z : Int × Int) (k : Int) (h : en z = 11 * k + 2 ∨ en z = 11 * k + 6 ∨ en z = 11 * k + 7 ∨ en z = 11 * k + 8 ∨ en z = 11 * k + 10) : False := by
  obtain ⟨a, b⟩ := z
  have hn : en (a, b) = a * a + a * b + 3 * (b * b) := rfl
  obtain ⟨q, r, hq, hr⟩ := GaussEuclid.rnd (a + 6 * b) 10
  have hq' : a + 6 * b = 11 * q + r := by rw [hq]; show q * 11 + r = 11 * q + r; lib_int
  have key : en (a, b) = r * r + 11 * (11 * q * q + 2 * q * r + a * a + -en (a, b)) := by
    have e12 : 12 * en (a, b) = (a + 6 * b) * (a + 6 * b) + 11 * (a * a) := by rw [hn]; lib_int
    rw [hq'] at e12
    calc en (a, b) = 12 * en (a, b) + -(11 * en (a, b)) := by lib_int
      _ = (11 * q + r) * (11 * q + r) + 11 * (a * a) + -(11 * en (a, b)) := by rw [e12]
      _ = _ := by lib_int
  generalize en (a, b) = N at key h
  generalize 11 * q * q + 2 * q * r + a * a + -N = X at key
  have cases_r : r = 0 ∨ r = 1 ∨ r = 2 ∨ r = 3 ∨ r = 4 ∨ r = 5 ∨ r = -1 ∨ r = -2 ∨ r = -3 ∨ r = -4 ∨ r = -5 := by
    cases r with
    | ofNat t =>
      match t, hr with
      | 0, _ => exact (Or.inl (rfl))
      | 1, _ => exact (Or.inr (Or.inl (rfl)))
      | 2, _ => exact (Or.inr (Or.inr (Or.inl (rfl))))
      | 3, _ => exact (Or.inr (Or.inr (Or.inr (Or.inl (rfl)))))
      | 4, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (rfl))))))
      | 5, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (rfl)))))))
      | t + 6, hr' =>
        exfalso
        have : 2 * (t + 6) ≤ 11 := hr'
        rw [show 2 * (t + 6) = 2 * t + 1 + 11 by lib_nat] at this
        exact Nat.not_succ_le_self 11 (Nat.le_trans (Nat.add_le_add_right (Nat.zero_le (2 * t)) 12) (Nat.le_trans (Nat.le_of_eq (by lib_nat)) this))
    | negSucc t =>
      match t, hr with
      | 0, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (rfl))))))))
      | 1, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (rfl)))))))))
      | 2, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (rfl))))))))))
      | 3, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (rfl)))))))))))
      | 4, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (rfl)))))))))))
      | t + 5, hr' =>
        exfalso
        have : 2 * (t + 6) ≤ 11 := hr'
        rw [show 2 * (t + 6) = 2 * t + 1 + 11 by lib_nat] at this
        exact Nat.not_succ_le_self 11 (Nat.le_trans (Nat.add_le_add_right (Nat.zero_le (2 * t)) 12) (Nat.le_trans (Nat.le_of_eq (by lib_nat)) this))
  rcases cases_r with e | e | e | e | e | e | e | e | e | e | e <;> rw [e] at key <;> rcases h with h | h | h | h | h
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 8 1 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 2) + -(0 * 0 + 11 * X) + 9 + -(-2) + (0 * 0 + -2) := by lib_int
        _ = N + -N + 9 := by rw [← h, ← key]; lib_int
        _ = 9 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 4 5 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 6) + -(0 * 0 + 11 * X) + 5 + -(-6) + (0 * 0 + -6) := by lib_int
        _ = N + -N + 5 := by rw [← h, ← key]; lib_int
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 3 6 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 7) + -(0 * 0 + 11 * X) + 4 + -(-7) + (0 * 0 + -7) := by lib_int
        _ = N + -N + 4 := by rw [← h, ← key]; lib_int
        _ = 4 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 2 7 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 8) + -(0 * 0 + 11 * X) + 3 + -(-8) + (0 * 0 + -8) := by lib_int
        _ = N + -N + 3 := by rw [← h, ← key]; lib_int
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 0 9 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 10) + -(0 * 0 + 11 * X) + 1 + -(-10) + (0 * 0 + -10) := by lib_int
        _ = N + -N + 1 := by rw [← h, ← key]; lib_int
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 9 0 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 2) + -(1 * 1 + 11 * X) + 10 + -(-1) + (1 * 1 + -2) := by lib_int
        _ = N + -N + 10 := by rw [← h, ← key]; lib_int
        _ = 10 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 5 4 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 6) + -(1 * 1 + 11 * X) + 6 + -(-5) + (1 * 1 + -6) := by lib_int
        _ = N + -N + 6 := by rw [← h, ← key]; lib_int
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 4 5 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 7) + -(1 * 1 + 11 * X) + 5 + -(-6) + (1 * 1 + -7) := by lib_int
        _ = N + -N + 5 := by rw [← h, ← key]; lib_int
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 3 6 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 8) + -(1 * 1 + 11 * X) + 4 + -(-7) + (1 * 1 + -8) := by lib_int
        _ = N + -N + 4 := by rw [← h, ← key]; lib_int
        _ = 4 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 1 8 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 10) + -(1 * 1 + 11 * X) + 2 + -(-9) + (1 * 1 + -10) := by lib_int
        _ = N + -N + 2 := by rw [← h, ← key]; lib_int
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(0)) 1 8 (by calc (11 : Int) * (k + -X + -(0)) = (11 * k + 2) + -(2 * 2 + 11 * X) + 2 + -(2) + (2 * 2 + -2) := by lib_int
        _ = N + -N + 2 := by rw [← h, ← key]; lib_int
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 8 1 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 6) + -(2 * 2 + 11 * X) + 9 + -(-2) + (2 * 2 + -6) := by lib_int
        _ = N + -N + 9 := by rw [← h, ← key]; lib_int
        _ = 9 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 7 2 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 7) + -(2 * 2 + 11 * X) + 8 + -(-3) + (2 * 2 + -7) := by lib_int
        _ = N + -N + 8 := by rw [← h, ← key]; lib_int
        _ = 8 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 6 3 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 8) + -(2 * 2 + 11 * X) + 7 + -(-4) + (2 * 2 + -8) := by lib_int
        _ = N + -N + 7 := by rw [← h, ← key]; lib_int
        _ = 7 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 4 5 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 10) + -(2 * 2 + 11 * X) + 5 + -(-6) + (2 * 2 + -10) := by lib_int
        _ = N + -N + 5 := by rw [← h, ← key]; lib_int
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(0)) 6 3 (by calc (11 : Int) * (k + -X + -(0)) = (11 * k + 2) + -(3 * 3 + 11 * X) + 7 + -(7) + (3 * 3 + -2) := by lib_int
        _ = N + -N + 7 := by rw [← h, ← key]; lib_int
        _ = 7 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(0)) 2 7 (by calc (11 : Int) * (k + -X + -(0)) = (11 * k + 6) + -(3 * 3 + 11 * X) + 3 + -(3) + (3 * 3 + -6) := by lib_int
        _ = N + -N + 3 := by rw [← h, ← key]; lib_int
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(0)) 1 8 (by calc (11 : Int) * (k + -X + -(0)) = (11 * k + 7) + -(3 * 3 + 11 * X) + 2 + -(2) + (3 * 3 + -7) := by lib_int
        _ = N + -N + 2 := by rw [← h, ← key]; lib_int
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(0)) 0 9 (by calc (11 : Int) * (k + -X + -(0)) = (11 * k + 8) + -(3 * 3 + 11 * X) + 1 + -(1) + (3 * 3 + -8) := by lib_int
        _ = N + -N + 1 := by rw [← h, ← key]; lib_int
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 9 0 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 10) + -(3 * 3 + 11 * X) + 10 + -(-1) + (3 * 3 + -10) := by lib_int
        _ = N + -N + 10 := by rw [← h, ← key]; lib_int
        _ = 10 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(1)) 2 7 (by calc (11 : Int) * (k + -X + -(1)) = (11 * k + 2) + -(4 * 4 + 11 * X) + 3 + -(14) + (4 * 4 + -2) := by lib_int
        _ = N + -N + 3 := by rw [← h, ← key]; lib_int
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(0)) 9 0 (by calc (11 : Int) * (k + -X + -(0)) = (11 * k + 6) + -(4 * 4 + 11 * X) + 10 + -(10) + (4 * 4 + -6) := by lib_int
        _ = N + -N + 10 := by rw [← h, ← key]; lib_int
        _ = 10 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(0)) 8 1 (by calc (11 : Int) * (k + -X + -(0)) = (11 * k + 7) + -(4 * 4 + 11 * X) + 9 + -(9) + (4 * 4 + -7) := by lib_int
        _ = N + -N + 9 := by rw [← h, ← key]; lib_int
        _ = 9 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(0)) 7 2 (by calc (11 : Int) * (k + -X + -(0)) = (11 * k + 8) + -(4 * 4 + 11 * X) + 8 + -(8) + (4 * 4 + -8) := by lib_int
        _ = N + -N + 8 := by rw [← h, ← key]; lib_int
        _ = 8 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(0)) 5 4 (by calc (11 : Int) * (k + -X + -(0)) = (11 * k + 10) + -(4 * 4 + 11 * X) + 6 + -(6) + (4 * 4 + -10) := by lib_int
        _ = N + -N + 6 := by rw [← h, ← key]; lib_int
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(2)) 0 9 (by calc (11 : Int) * (k + -X + -(2)) = (11 * k + 2) + -(5 * 5 + 11 * X) + 1 + -(23) + (5 * 5 + -2) := by lib_int
        _ = N + -N + 1 := by rw [← h, ← key]; lib_int
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(1)) 7 2 (by calc (11 : Int) * (k + -X + -(1)) = (11 * k + 6) + -(5 * 5 + 11 * X) + 8 + -(19) + (5 * 5 + -6) := by lib_int
        _ = N + -N + 8 := by rw [← h, ← key]; lib_int
        _ = 8 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(1)) 6 3 (by calc (11 : Int) * (k + -X + -(1)) = (11 * k + 7) + -(5 * 5 + 11 * X) + 7 + -(18) + (5 * 5 + -7) := by lib_int
        _ = N + -N + 7 := by rw [← h, ← key]; lib_int
        _ = 7 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(1)) 5 4 (by calc (11 : Int) * (k + -X + -(1)) = (11 * k + 8) + -(5 * 5 + 11 * X) + 6 + -(17) + (5 * 5 + -8) := by lib_int
        _ = N + -N + 6 := by rw [← h, ← key]; lib_int
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(1)) 3 6 (by calc (11 : Int) * (k + -X + -(1)) = (11 * k + 10) + -(5 * 5 + 11 * X) + 4 + -(15) + (5 * 5 + -10) := by lib_int
        _ = N + -N + 4 := by rw [← h, ← key]; lib_int
        _ = 4 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 9 0 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 2) + -(-1 * -1 + 11 * X) + 10 + -(-1) + (-1 * -1 + -2) := by lib_int
        _ = N + -N + 10 := by rw [← h, ← key]; lib_int
        _ = 10 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 5 4 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 6) + -(-1 * -1 + 11 * X) + 6 + -(-5) + (-1 * -1 + -6) := by lib_int
        _ = N + -N + 6 := by rw [← h, ← key]; lib_int
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 4 5 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 7) + -(-1 * -1 + 11 * X) + 5 + -(-6) + (-1 * -1 + -7) := by lib_int
        _ = N + -N + 5 := by rw [← h, ← key]; lib_int
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 3 6 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 8) + -(-1 * -1 + 11 * X) + 4 + -(-7) + (-1 * -1 + -8) := by lib_int
        _ = N + -N + 4 := by rw [← h, ← key]; lib_int
        _ = 4 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 1 8 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 10) + -(-1 * -1 + 11 * X) + 2 + -(-9) + (-1 * -1 + -10) := by lib_int
        _ = N + -N + 2 := by rw [← h, ← key]; lib_int
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(0)) 1 8 (by calc (11 : Int) * (k + -X + -(0)) = (11 * k + 2) + -(-2 * -2 + 11 * X) + 2 + -(2) + (-2 * -2 + -2) := by lib_int
        _ = N + -N + 2 := by rw [← h, ← key]; lib_int
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 8 1 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 6) + -(-2 * -2 + 11 * X) + 9 + -(-2) + (-2 * -2 + -6) := by lib_int
        _ = N + -N + 9 := by rw [← h, ← key]; lib_int
        _ = 9 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 7 2 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 7) + -(-2 * -2 + 11 * X) + 8 + -(-3) + (-2 * -2 + -7) := by lib_int
        _ = N + -N + 8 := by rw [← h, ← key]; lib_int
        _ = 8 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 6 3 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 8) + -(-2 * -2 + 11 * X) + 7 + -(-4) + (-2 * -2 + -8) := by lib_int
        _ = N + -N + 7 := by rw [← h, ← key]; lib_int
        _ = 7 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 4 5 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 10) + -(-2 * -2 + 11 * X) + 5 + -(-6) + (-2 * -2 + -10) := by lib_int
        _ = N + -N + 5 := by rw [← h, ← key]; lib_int
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(0)) 6 3 (by calc (11 : Int) * (k + -X + -(0)) = (11 * k + 2) + -(-3 * -3 + 11 * X) + 7 + -(7) + (-3 * -3 + -2) := by lib_int
        _ = N + -N + 7 := by rw [← h, ← key]; lib_int
        _ = 7 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(0)) 2 7 (by calc (11 : Int) * (k + -X + -(0)) = (11 * k + 6) + -(-3 * -3 + 11 * X) + 3 + -(3) + (-3 * -3 + -6) := by lib_int
        _ = N + -N + 3 := by rw [← h, ← key]; lib_int
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(0)) 1 8 (by calc (11 : Int) * (k + -X + -(0)) = (11 * k + 7) + -(-3 * -3 + 11 * X) + 2 + -(2) + (-3 * -3 + -7) := by lib_int
        _ = N + -N + 2 := by rw [← h, ← key]; lib_int
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(0)) 0 9 (by calc (11 : Int) * (k + -X + -(0)) = (11 * k + 8) + -(-3 * -3 + 11 * X) + 1 + -(1) + (-3 * -3 + -8) := by lib_int
        _ = N + -N + 1 := by rw [← h, ← key]; lib_int
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 9 0 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 10) + -(-3 * -3 + 11 * X) + 10 + -(-1) + (-3 * -3 + -10) := by lib_int
        _ = N + -N + 10 := by rw [← h, ← key]; lib_int
        _ = 10 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(1)) 2 7 (by calc (11 : Int) * (k + -X + -(1)) = (11 * k + 2) + -(-4 * -4 + 11 * X) + 3 + -(14) + (-4 * -4 + -2) := by lib_int
        _ = N + -N + 3 := by rw [← h, ← key]; lib_int
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(0)) 9 0 (by calc (11 : Int) * (k + -X + -(0)) = (11 * k + 6) + -(-4 * -4 + 11 * X) + 10 + -(10) + (-4 * -4 + -6) := by lib_int
        _ = N + -N + 10 := by rw [← h, ← key]; lib_int
        _ = 10 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(0)) 8 1 (by calc (11 : Int) * (k + -X + -(0)) = (11 * k + 7) + -(-4 * -4 + 11 * X) + 9 + -(9) + (-4 * -4 + -7) := by lib_int
        _ = N + -N + 9 := by rw [← h, ← key]; lib_int
        _ = 9 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(0)) 7 2 (by calc (11 : Int) * (k + -X + -(0)) = (11 * k + 8) + -(-4 * -4 + 11 * X) + 8 + -(8) + (-4 * -4 + -8) := by lib_int
        _ = N + -N + 8 := by rw [← h, ← key]; lib_int
        _ = 8 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(0)) 5 4 (by calc (11 : Int) * (k + -X + -(0)) = (11 * k + 10) + -(-4 * -4 + 11 * X) + 6 + -(6) + (-4 * -4 + -10) := by lib_int
        _ = N + -N + 6 := by rw [← h, ← key]; lib_int
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(2)) 0 9 (by calc (11 : Int) * (k + -X + -(2)) = (11 * k + 2) + -(-5 * -5 + 11 * X) + 1 + -(23) + (-5 * -5 + -2) := by lib_int
        _ = N + -N + 1 := by rw [← h, ← key]; lib_int
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(1)) 7 2 (by calc (11 : Int) * (k + -X + -(1)) = (11 * k + 6) + -(-5 * -5 + 11 * X) + 8 + -(19) + (-5 * -5 + -6) := by lib_int
        _ = N + -N + 8 := by rw [← h, ← key]; lib_int
        _ = 8 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(1)) 6 3 (by calc (11 : Int) * (k + -X + -(1)) = (11 * k + 7) + -(-5 * -5 + 11 * X) + 7 + -(18) + (-5 * -5 + -7) := by lib_int
        _ = N + -N + 7 := by rw [← h, ← key]; lib_int
        _ = 7 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(1)) 5 4 (by calc (11 : Int) * (k + -X + -(1)) = (11 * k + 8) + -(-5 * -5 + 11 * X) + 6 + -(17) + (-5 * -5 + -8) := by lib_int
        _ = N + -N + 6 := by rw [← h, ← key]; lib_int
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(1)) 3 6 (by calc (11 : Int) * (k + -X + -(1)) = (11 * k + 10) + -(-5 * -5 + 11 * X) + 4 + -(15) + (-5 * -5 + -10) := by lib_int
        _ = N + -N + 4 := by rw [← h, ← key]; lib_int
        _ = 4 := by lib_int) rfl

/-- **c_W(n) = 0 for n a non-square mod 11.** -/
theorem cw11_zero (W : Int × Int → Int × Int) (n : Nat) (k : Int) (hn : Int.ofNat n = 11 * k + 2 ∨ Int.ofNat n = 11 * k + 6 ∨ Int.ofNat n = 11 * k + 7 ∨ Int.ofNat n = 11 * k + 8 ∨ Int.ofNat n = 11 * k + 10) : cw11 W n = (Ring11.ring11 LibInt.intR).zero := by
  unfold cw11
  rw [sumK_ext (Ring11.ring11 LibInt.intR) _ (fun _ => (Ring11.ring11 LibInt.intR).zero) (fun z => by
    rw [I11_neg _ _ (fun h => en_mod11 z k (by rw [h.1]; exact hn))]; lib_ring)]
  exact zsum11 _

def Pi : Int × Int := (Int.ofNat (P + 1 + 1), 0)

theorem en_Pi : en (Pi P) = Int.ofNat ((P + 1 + 1) * (P + 1 + 1)) := by
  show Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1) + Int.ofNat (P + 1 + 1) * 0 + 3 * (0 * 0) = Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1)
  lib_int

theorem nrm_Pi : nrm (Pi P) = (P + 1 + 1) * (P + 1 + 1) := Int.ofNat.inj (by rw [nrm_cast, en_Pi])

theorem cj_Pi : cj (Pi P) = Pi P := Prod.ext (by show Int.ofNat (P + 1 + 1) + 0 = Int.ofNat (P + 1 + 1); lib_int) (by show -(0 : Int) = 0; rfl)

theorem not_p (hc : ∃ K, Int.ofNat (P + 1 + 1) = 11 * K + 2 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 6 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 7 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 8 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 10) (z : Int × Int) (h : nrm z = P + 1 + 1) : False := by
  obtain ⟨K, hK⟩ := hc
  exact en_mod11 z K (by rw [← nrm_cast, h]; exact hK)

theorem dvd_conv (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) : ∀ d, d ∣ (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1 :=
  fun d ⟨c, hc⟩ => hpr d ⟨c, by rw [hc]; lib_nat⟩

/-- the two factor case: a b = p², p ∤ … ⇒ a = 1 or b = 1 (a = p is excluded separately). -/
theorem split_sq (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (a b : Nat) (h : a * b = (P + 1 + 1) * (P + 1 + 1)) :
    a = 1 ∨ b = 1 ∨ a = P + 1 + 1 := by
  cases euclid_prime (dvd_conv P hpr) (show (P + 1 + 1) ∣ a * b from ⟨P + 1 + 1, h⟩) with
  | inl ha =>
    obtain ⟨s, hs⟩ := ha
    have e : s * b = P + 1 + 1 := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (P + 1)) (by rw [show (P + 1 + 1) * (s * b) = (P + 1 + 1) * s * b by lib_nat, ← hs, h])
    cases hpr s ⟨b, by rw [← e]; lib_nat⟩ with
    | inl s1 => exact Or.inr (Or.inr (by rw [hs, s1]; lib_nat))
    | inr sp =>
      refine Or.inr (Or.inl ?_)
      rw [sp] at e
      exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (P + 1)) (e.trans (Nat.mul_one _).symm)
  | inr hb =>
    obtain ⟨s, hs⟩ := hb
    have e : a * s = P + 1 + 1 := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (P + 1)) (by
      rw [show (P + 1 + 1) * (a * s) = a * ((P + 1 + 1) * s) by lib_nat, ← hs, h])
    cases hpr a ⟨s, by rw [← e]; lib_nat⟩ with
    | inl a1 => exact Or.inl a1
    | inr ap => exact Or.inr (Or.inr ap)

/-- **p ≡ 2 (mod 3) stays prime in ℤ[ω].** -/
theorem inert_div (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : ∃ K, Int.ofNat (P + 1 + 1) = 11 * K + 2 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 6 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 7 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 8 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 10) (x y : Int × Int)
    (h : Dvd' (Pi P) ((Ring11.ring11 LibInt.intR).mul x y)) : Dvd' (Pi P) x ∨ Dvd' (Pi P) y := by
  match egcd_exists _ (Pi P) x (Nat.le_refl _) with
  | ⟨d, X, Y, hd, ⟨c1, hc1⟩, ⟨c2, hc2⟩⟩ =>
    have hdc : nrm d * nrm c1 = (P + 1 + 1) * (P + 1 + 1) := by rw [← nrm_mul, ← hc1, nrm_Pi]
    rcases split_sq P hpr _ _ hdc with h1 | h1 | h1
    · refine Or.inr (eeuclid (Pi P) x y ((Ring11.ring11 LibInt.intR).mul (cj d) X) ((Ring11.ring11 LibInt.intR).mul (cj d) Y) ?_ h)
      rw [show (Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul (cj d) X) (Pi P)) ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul (cj d) Y) x)
          = (Ring11.ring11 LibInt.intR).mul (cj d) ((Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul X (Pi P)) ((Ring11.ring11 LibInt.intR).mul Y x)) by lib_ring,
        ← hd, (Ring11.ring11 LibInt.intR).mul_comm, unit_inv d h1]
    · refine Or.inl ⟨(Ring11.ring11 LibInt.intR).mul (cj c1) c2, ?_⟩
      have hd' : d = (Ring11.ring11 LibInt.intR).mul (Pi P) (cj c1) := by
        rw [hc1]
        calc d = (Ring11.ring11 LibInt.intR).mul d (Ring11.ring11 LibInt.intR).one := by lib_ring
          _ = _ := by rw [← unit_inv c1 h1]; lib_ring
      rw [hc2, hd']; lib_ring
    · exact (not_p P hc d h1).elim

theorem nat_pow2 (k : Nat) : (P + 1 + 1) ^ (2 * (k + 1)) = (P + 1 + 1) * (P + 1 + 1) * (P + 1 + 1) ^ (2 * k) := by
  rw [show 2 * (k + 1) = 2 * k + 1 + 1 by lib_nat]
  show (P + 1 + 1) ^ (2 * k) * (P + 1 + 1) * (P + 1 + 1) = _
  lib_nat

/-- **Factorisation above an inert prime.** -/
theorem factor_inert (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : ∃ K, Int.ofNat (P + 1 + 1) = 11 * K + 2 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 6 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 7 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 8 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 10) : ∀ k (α : Int × Int),
    nrm α = (P + 1 + 1) ^ (2 * k) → ∃ u, nrm u = 1 ∧ α = (Ring11.ring11 LibInt.intR).mul u (pw (Ring11.ring11 LibInt.intR) (Pi P) k)
  | 0, α, h => ⟨α, h, by show α = (Ring11.ring11 LibInt.intR).mul α (Ring11.ring11 LibInt.intR).one; lib_ring⟩
  | k + 1, α, h => by
    have hdiv : Dvd' (Pi P) ((Ring11.ring11 LibInt.intR).mul α (cj α)) := by
      refine ⟨(Int.ofNat ((P + 1 + 1) * (P + 1 + 1) ^ (2 * k)), 0), ?_⟩
      rw [mul_cj, ← nrm_cast, h, nat_pow2]
      apply Prod.ext
      · show Int.ofNat ((P + 1 + 1) * (P + 1 + 1) * (P + 1 + 1) ^ (2 * k)) = Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) * (P + 1 + 1) ^ (2 * k)) + -(0 * 0)
        show Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ (2 * k)) = _
        show _ = Int.ofNat (P + 1 + 1) * (Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ (2 * k))) + -(0 * 0)
        lib_int
      · show (0 : Int) = Int.ofNat (P + 1 + 1) * 0 + 0 * Int.ofNat ((P + 1 + 1) * (P + 1 + 1) ^ (2 * k)) + -(0 * 0); lib_int
    have cancel : ∀ β : Int × Int, nrm ((Ring11.ring11 LibInt.intR).mul (Pi P) β) = (P + 1 + 1) ^ (2 * (k + 1)) → nrm β = (P + 1 + 1) ^ (2 * k) := fun β hb => by
      rw [nrm_mul, nrm_Pi, nat_pow2] at hb
      exact Nat.eq_of_mul_eq_mul_left (Nat.mul_pos (Nat.zero_lt_succ _) (Nat.zero_lt_succ _)) hb
    have hβ : ∃ β, α = (Ring11.ring11 LibInt.intR).mul (Pi P) β := by
      cases inert_div P hpr hc α (cj α) hdiv with
      | inl hα => exact hα
      | inr hα =>
        obtain ⟨β, hβ⟩ := hα
        exact ⟨cj β, by rw [← cj_Pi P, ← cj_mul, ← hβ, cj_cj]⟩
    obtain ⟨β, hβ⟩ := hβ
    obtain ⟨u, hu, he⟩ := factor_inert hpr hc k β (cancel β (by rw [← hβ]; exact h))
    refine ⟨u, hu, ?_⟩
    rw [hβ, he]; show _ = (Ring11.ring11 LibInt.intR).mul u ((Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) (Pi P) k) (Pi P)); lib_ring

def nPi : Int × Int := (-Int.ofNat (P + 1 + 1), 0)

theorem prim_nPi (hc : ∃ K, Int.ofNat (P + 1 + 1) = 11 * K + 2 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 6 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 7 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 8 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 10) : Prim11 (nPi P) := by
  obtain ⟨K, hK | hK | hK | hK | hK⟩ := hc
  · exact ⟨-K + -1, (Or.inr (Or.inr (Or.inr (Or.inr (by show -Int.ofNat (P + 1 + 1) + 6 * 0 = _; rw [hK]; lib_int)))))⟩
  · exact ⟨-K + -1, (Or.inr (Or.inr (Or.inr (Or.inl (by show -Int.ofNat (P + 1 + 1) + 6 * 0 = _; rw [hK]; lib_int)))))⟩
  · exact ⟨-K + -1, (Or.inr (Or.inr (Or.inl (by show -Int.ofNat (P + 1 + 1) + 6 * 0 = _; rw [hK]; lib_int))))⟩
  · exact ⟨-K + -1, (Or.inr (Or.inl (by show -Int.ofNat (P + 1 + 1) + 6 * 0 = _; rw [hK]; lib_int)))⟩
  · exact ⟨-K + -1, (Or.inl (by show -Int.ofNat (P + 1 + 1) + 6 * 0 = _; rw [hK]; lib_int))⟩

theorem pw_neg (k : Nat) : pw (Ring11.ring11 LibInt.intR) (Pi P) k =
    (Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) ((Ring11.ring11 LibInt.intR).neg (Ring11.ring11 LibInt.intR).one) k) (pw (Ring11.ring11 LibInt.intR) (nPi P) k) := by
  rw [← Frobenius.pw_mul]
  congr 1
  apply Prod.ext
  · show Int.ofNat (P + 1 + 1) = -1 * -Int.ofNat (P + 1 + 1) + -(-0 * 0 + -0 * 0 + -0 * 0); lib_int
  · show (0 : Int) = -1 * 0 + -0 * -Int.ofNat (P + 1 + 1) + -0 * 0; lib_int

theorem nrm_neg_one_pw : ∀ k, nrm (pw (Ring11.ring11 LibInt.intR) ((Ring11.ring11 LibInt.intR).neg (Ring11.ring11 LibInt.intR).one) k) = 1
  | 0 => rfl
  | k + 1 => by
    show nrm ((Ring11.ring11 LibInt.intR).mul _ _) = 1
    rw [nrm_mul, nrm_neg_one_pw k]; rfl

/-- **The primary elements of norm p^(2k) are (−p)^k.** -/
theorem prim_inert (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : ∃ K, Int.ofNat (P + 1 + 1) = 11 * K + 2 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 6 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 7 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 8 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 10) (k : Nat) (α : Int × Int)
    (hα : nrm α = (P + 1 + 1) ^ (2 * k)) (pα : Prim11 α) : α = pw (Ring11.ring11 LibInt.intR) (nPi P) k := by
  obtain ⟨u, hu, he⟩ := factor_inert P hpr hc k α hα
  rw [pw_neg] at he
  generalize hv : (Ring11.ring11 LibInt.intR).mul u (pw (Ring11.ring11 LibInt.intR) ((Ring11.ring11 LibInt.intR).neg (Ring11.ring11 LibInt.intR).one) k) = v
  have he' : α = (Ring11.ring11 LibInt.intR).mul v (pw (Ring11.ring11 LibInt.intR) (nPi P) k) := by
    rw [he, ← hv]; lib_ring
  have hv1 : nrm v = 1 := by rw [← hv, nrm_mul, hu, nrm_neg_one_pw]
  have v1 := unit_one11 v _ (nrm_one_eq v hv1) (prim11_pow _ (prim_nPi P hc) k) (by rw [← he']; exact pα)
  rw [he', v1]; lib_ring

theorem nrm_nPi : nrm (nPi P) = (P + 1 + 1) * (P + 1 + 1) := by
  apply Int.ofNat.inj; rw [nrm_cast]
  show -Int.ofNat (P + 1 + 1) * -Int.ofNat (P + 1 + 1) + -Int.ofNat (P + 1 + 1) * 0 + 3 * (0 * 0) = Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1)
  lib_int

theorem nrm_nPi_pw (k : Nat) : nrm (pw (Ring11.ring11 LibInt.intR) (nPi P) k) = (P + 1 + 1) ^ (2 * k) := by
  rw [HeckePrime11.nrm_pw, nrm_nPi]
  induction k with
  | zero => rfl
  | succ k ih =>
    show ((P + 1 + 1) * (P + 1 + 1)) ^ k * ((P + 1 + 1) * (P + 1 + 1)) = _
    rw [ih, nat_pow2]; lib_nat

/-- **c_W(p^(2k)) = W((−p)^k)** at an inert prime. -/
theorem cw11_even (W : Int × Int → Int × Int) (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : ∃ K, Int.ofNat (P + 1 + 1) = 11 * K + 2 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 6 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 7 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 8 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 10) (k : Nat) :
    cw11 W ((P + 1 + 1) ^ (2 * k)) = W (pw (Ring11.ring11 LibInt.intR) (nPi P) k) := by
  have hPD : PD11 ((P + 1 + 1) ^ (2 * k)) (pw (Ring11.ring11 LibInt.intR) (nPi P) k) :=
    ⟨by rw [← nrm_cast, nrm_nPi_pw], prim11_pow _ (prim_nPi P hc) k⟩
  unfold cw11
  rw [sumK_ext (Ring11.ring11 LibInt.intR) _ (fun z => (Ring11.ring11 LibInt.intR).mul (delta (Ring11.ring11 LibInt.intR) z (pw (Ring11.ring11 LibInt.intR) (nPi P) k)) (W z)) (fun z => by
    by_cases hz : PD11 ((P + 1 + 1) ^ (2 * k)) z
    · have e := prim_inert P hpr hc k z (Int.ofNat.inj (by rw [nrm_cast, hz.1])) hz.2
      rw [I11_pos _ _ hz]; unfold delta; rw [ite_pos' _ _ e]
    · rw [I11_neg _ _ hz]; unfold delta
      rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact hPD))])]
  have hb := abs_le11 _ _ hPD.1
  exact HeckeMult.pick_box (Ring11.ring11 LibInt.intR) W _ _ hb.1 hb.2

theorem sq11 (Y L : Int) (hL : Y = 11 * L + 2 ∨ Y = 11 * L + 6 ∨ Y = 11 * L + 7 ∨ Y = 11 * L + 8 ∨ Y = 11 * L + 10) : ∃ M : Int, Y * Y = 11 * M + 1 ∨ Y * Y = 11 * M + 3 ∨ Y * Y = 11 * M + 4 ∨ Y * Y = 11 * M + 5 ∨ Y * Y = 11 * M + 9 := by
  rcases hL with hL | hL | hL | hL | hL
  · exact ⟨(4) * L + (11) * L * L, (Or.inr (Or.inr (Or.inl (by rw [hL]; lib_int))))⟩
  · exact ⟨(3) + (12) * L + (11) * L * L, (Or.inr (Or.inl (by rw [hL]; lib_int)))⟩
  · exact ⟨(4) + (14) * L + (11) * L * L, (Or.inr (Or.inr (Or.inr (Or.inl (by rw [hL]; lib_int)))))⟩
  · exact ⟨(5) + (16) * L + (11) * L * L, (Or.inr (Or.inr (Or.inr (Or.inr (by rw [hL]; lib_int)))))⟩
  · exact ⟨(9) + (20) * L + (11) * L * L, (Or.inl (by rw [hL]; lib_int))⟩

theorem step11_0 (X K M Z : Int) (hK : X = 11 * K + 2) (hM : Z = 11 * M + 1 ∨ Z = 11 * M + 3 ∨ Z = 11 * M + 4 ∨ Z = 11 * M + 5 ∨ Z = 11 * M + 9) :
    ∃ W : Int, X * Z = 11 * W + 2 ∨ X * Z = 11 * W + 6 ∨ X * Z = 11 * W + 7 ∨ X * Z = 11 * W + 8 ∨ X * Z = 11 * W + 10 := by
  rcases hM with hM | hM | hM | hM | hM <;> rw [hK, hM]
  · exact ⟨K + (11) * K * M + (2) * M, (Or.inl (by lib_int))⟩
  · exact ⟨(3) * K + (11) * K * M + (2) * M, (Or.inr (Or.inl (by lib_int)))⟩
  · exact ⟨(4) * K + (11) * K * M + (2) * M, (Or.inr (Or.inr (Or.inr (Or.inl (by lib_int)))))⟩
  · exact ⟨(5) * K + (11) * K * M + (2) * M, (Or.inr (Or.inr (Or.inr (Or.inr (by lib_int)))))⟩
  · exact ⟨(1) + (9) * K + (11) * K * M + (2) * M, (Or.inr (Or.inr (Or.inl (by lib_int))))⟩

theorem step11_1 (X K M Z : Int) (hK : X = 11 * K + 6) (hM : Z = 11 * M + 1 ∨ Z = 11 * M + 3 ∨ Z = 11 * M + 4 ∨ Z = 11 * M + 5 ∨ Z = 11 * M + 9) :
    ∃ W : Int, X * Z = 11 * W + 2 ∨ X * Z = 11 * W + 6 ∨ X * Z = 11 * W + 7 ∨ X * Z = 11 * W + 8 ∨ X * Z = 11 * W + 10 := by
  rcases hM with hM | hM | hM | hM | hM <;> rw [hK, hM]
  · exact ⟨K + (11) * K * M + (6) * M, (Or.inr (Or.inl (by lib_int)))⟩
  · exact ⟨(1) + (3) * K + (11) * K * M + (6) * M, (Or.inr (Or.inr (Or.inl (by lib_int))))⟩
  · exact ⟨(2) + (4) * K + (11) * K * M + (6) * M, (Or.inl (by lib_int))⟩
  · exact ⟨(2) + (5) * K + (11) * K * M + (6) * M, (Or.inr (Or.inr (Or.inr (Or.inl (by lib_int)))))⟩
  · exact ⟨(4) + (9) * K + (11) * K * M + (6) * M, (Or.inr (Or.inr (Or.inr (Or.inr (by lib_int)))))⟩

theorem step11_2 (X K M Z : Int) (hK : X = 11 * K + 7) (hM : Z = 11 * M + 1 ∨ Z = 11 * M + 3 ∨ Z = 11 * M + 4 ∨ Z = 11 * M + 5 ∨ Z = 11 * M + 9) :
    ∃ W : Int, X * Z = 11 * W + 2 ∨ X * Z = 11 * W + 6 ∨ X * Z = 11 * W + 7 ∨ X * Z = 11 * W + 8 ∨ X * Z = 11 * W + 10 := by
  rcases hM with hM | hM | hM | hM | hM <;> rw [hK, hM]
  · exact ⟨K + (11) * K * M + (7) * M, (Or.inr (Or.inr (Or.inl (by lib_int))))⟩
  · exact ⟨(1) + (3) * K + (11) * K * M + (7) * M, (Or.inr (Or.inr (Or.inr (Or.inr (by lib_int)))))⟩
  · exact ⟨(2) + (4) * K + (11) * K * M + (7) * M, (Or.inr (Or.inl (by lib_int)))⟩
  · exact ⟨(3) + (5) * K + (11) * K * M + (7) * M, (Or.inl (by lib_int))⟩
  · exact ⟨(5) + (9) * K + (11) * K * M + (7) * M, (Or.inr (Or.inr (Or.inr (Or.inl (by lib_int)))))⟩

theorem step11_3 (X K M Z : Int) (hK : X = 11 * K + 8) (hM : Z = 11 * M + 1 ∨ Z = 11 * M + 3 ∨ Z = 11 * M + 4 ∨ Z = 11 * M + 5 ∨ Z = 11 * M + 9) :
    ∃ W : Int, X * Z = 11 * W + 2 ∨ X * Z = 11 * W + 6 ∨ X * Z = 11 * W + 7 ∨ X * Z = 11 * W + 8 ∨ X * Z = 11 * W + 10 := by
  rcases hM with hM | hM | hM | hM | hM <;> rw [hK, hM]
  · exact ⟨K + (11) * K * M + (8) * M, (Or.inr (Or.inr (Or.inr (Or.inl (by lib_int)))))⟩
  · exact ⟨(2) + (3) * K + (11) * K * M + (8) * M, (Or.inl (by lib_int))⟩
  · exact ⟨(2) + (4) * K + (11) * K * M + (8) * M, (Or.inr (Or.inr (Or.inr (Or.inr (by lib_int)))))⟩
  · exact ⟨(3) + (5) * K + (11) * K * M + (8) * M, (Or.inr (Or.inr (Or.inl (by lib_int))))⟩
  · exact ⟨(6) + (9) * K + (11) * K * M + (8) * M, (Or.inr (Or.inl (by lib_int)))⟩

theorem step11_4 (X K M Z : Int) (hK : X = 11 * K + 10) (hM : Z = 11 * M + 1 ∨ Z = 11 * M + 3 ∨ Z = 11 * M + 4 ∨ Z = 11 * M + 5 ∨ Z = 11 * M + 9) :
    ∃ W : Int, X * Z = 11 * W + 2 ∨ X * Z = 11 * W + 6 ∨ X * Z = 11 * W + 7 ∨ X * Z = 11 * W + 8 ∨ X * Z = 11 * W + 10 := by
  rcases hM with hM | hM | hM | hM | hM <;> rw [hK, hM]
  · exact ⟨K + (11) * K * M + (10) * M, (Or.inr (Or.inr (Or.inr (Or.inr (by lib_int)))))⟩
  · exact ⟨(2) + (3) * K + (11) * K * M + (10) * M, (Or.inr (Or.inr (Or.inr (Or.inl (by lib_int)))))⟩
  · exact ⟨(3) + (4) * K + (11) * K * M + (10) * M, (Or.inr (Or.inr (Or.inl (by lib_int))))⟩
  · exact ⟨(4) + (5) * K + (11) * K * M + (10) * M, (Or.inr (Or.inl (by lib_int)))⟩
  · exact ⟨(8) + (9) * K + (11) * K * M + (10) * M, (Or.inl (by lib_int))⟩

theorem step11 (X Y K L : Int) (hK : X = 11 * K + 2 ∨ X = 11 * K + 6 ∨ X = 11 * K + 7 ∨ X = 11 * K + 8 ∨ X = 11 * K + 10) (hL : Y = 11 * L + 2 ∨ Y = 11 * L + 6 ∨ Y = 11 * L + 7 ∨ Y = 11 * L + 8 ∨ Y = 11 * L + 10) :
    ∃ W : Int, X * Y * Y = 11 * W + 2 ∨ X * Y * Y = 11 * W + 6 ∨ X * Y * Y = 11 * W + 7 ∨ X * Y * Y = 11 * W + 8 ∨ X * Y * Y = 11 * W + 10 := by
  obtain ⟨M, hM⟩ := sq11 Y L hL
  have e : X * Y * Y = X * (Y * Y) := by lib_int
  rw [e]
  rcases hK with hK | hK | hK | hK | hK
  · exact step11_0 _ K M _ hK hM
  · exact step11_1 _ K M _ hK hM
  · exact step11_2 _ K M _ hK hM
  · exact step11_3 _ K M _ hK hM
  · exact step11_4 _ K M _ hK hM

theorem pow_odd (hc : ∃ K, Int.ofNat (P + 1 + 1) = 11 * K + 2 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 6 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 7 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 8 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 10) (k : Nat) : ∃ K : Int, Int.ofNat ((P + 1 + 1) ^ (2 * k + 1)) = 11 * K + 2 ∨ Int.ofNat ((P + 1 + 1) ^ (2 * k + 1)) = 11 * K + 6 ∨ Int.ofNat ((P + 1 + 1) ^ (2 * k + 1)) = 11 * K + 7 ∨ Int.of  -- (+104 chars; full line in the Zenodo source)
  obtain ⟨L, hL⟩ := hc
  induction k with
  | zero => exact ⟨L, by rw [show (P + 1 + 1) ^ (2 * 0 + 1) = P + 1 + 1 from Nat.one_mul _]; exact hL⟩
  | succ k ih =>
    obtain ⟨K, hK⟩ := ih
    have e : Int.ofNat ((P + 1 + 1) ^ (2 * (k + 1) + 1)) = Int.ofNat ((P + 1 + 1) ^ (2 * k + 1)) * Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1) := by
      rw [show 2 * (k + 1) + 1 = 2 * k + 1 + 1 + 1 by lib_nat]; rfl
    rw [e]
    exact step11 _ _ K L hK hL

/-- **c_W(p^(2k+1)) = 0** at an inert prime. -/
theorem cw11_odd (W : Int × Int → Int × Int) (hc : ∃ K, Int.ofNat (P + 1 + 1) = 11 * K + 2 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 6 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 7 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 8 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 10) (k : Nat) : cw11 W ((P + 1 + 1) ^ (2 * k + 1)) = (Ring11.ring11 LibInt.intR).zero := by
  obtain ⟨K, hK⟩ := pow_odd P hc k
  exact cw11_zero W _ K hK

end Inert11

#print axioms Inert11.en_mod11
#print axioms Inert11.cw11_zero
#print axioms Inert11.en_Pi
#print axioms Inert11.nrm_Pi
#print axioms Inert11.cj_Pi
#print axioms Inert11.not_p
#print axioms Inert11.dvd_conv
#print axioms Inert11.split_sq
#print axioms Inert11.inert_div
#print axioms Inert11.nat_pow2
#print axioms Inert11.factor_inert
#print axioms Inert11.prim_nPi
#print axioms Inert11.pw_neg
#print axioms Inert11.nrm_neg_one_pw
#print axioms Inert11.prim_inert
#print axioms Inert11.nrm_nPi
#print axioms Inert11.nrm_nPi_pw
#print axioms Inert11.cw11_even
#print axioms Inert11.sq11
#print axioms Inert11.step11_0
#print axioms Inert11.step11_1
#print axioms Inert11.step11_2
#print axioms Inert11.step11_3
#print axioms Inert11.step11_4
#print axioms Inert11.step11
#print axioms Inert11.pow_odd
#print axioms Inert11.cw11_odd

lang/Inert163.lean

lean_all/lang/Inert163.lean

/-
  lang/Inert163 — T48/T50: **the ℚ(√−163) Hecke character at inert primes** (χ₁₆₃(p) = −1), via the Legendre symbol:
    cast_en, en_mod163   N(z) ≡ ρ(z)² (mod 163) (164N = ρ² + 163a²), so χ₁₆₃(N(z)) ≠ −1
    cw163_zero, inert_div, factor_inert, prim_inert, cw163_even, pow_odd, cw163_odd   (port of Inert19)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.HeckePrime163

namespace Inert163

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius Euc163 Prim163 Uniq163 Prime163 HeckeMult163

variable (P : Nat)

/-- E (a² + ab + t b²) = (a + c b)² + D a² when 2c = E, c² = E t, D + 1 = E. -/
theorem sqgen2 (a b c D E t : Int) (h1 : 2 * c = E) (h2 : c * c = E * t) (h3 : D + 1 = E) :
    E * (a * a + a * b + t * (b * b)) = (a + c * b) * (a + c * b) + D * (a * a) := by
  calc E * (a * a + a * b + t * (b * b)) = E * (a * a) + E * (a * b) + (E * t) * (b * b) := by lib_int
    _ = (D + 1) * (a * a) + (2 * c) * (a * b) + (c * c) * (b * b) := by rw [h3, h1, h2]
    _ = _ := by lib_int

theorem cast_en (z : Int × Int) : LibRing.castZ (ZMod.ring (2 * 80 + 1 + 1)) (en z) = (ZMod.ring (2 * 80 + 1 + 1)).mul (Prim163.cr z) (Prim163.cr z) := by
  have e44 : 164 * en z = Prim163.rho z * Prim163.rho z + 163 * (z.1 * z.1) :=
    sqgen2 z.1 z.2 82 163 164 41 rfl rfl rfl
  have h1 : LibRing.castZ (ZMod.ring (2 * 80 + 1 + 1)) (164 * en z) = LibRing.castZ (ZMod.ring (2 * 80 + 1 + 1)) (en z) := by
    rw [show (164 : Int) * en z = 163 * en z + en z by lib_int, IntCast.castZ_add, Prim163.cz163]; lib_ring
  rw [← h1, e44, IntCast.castZ_add, HasseDavenport.castZ_mul, Prim163.cz163]; unfold Prim163.cr; lib_ring

/-- N(z) is never a non-residue mod 163. -/
theorem en_mod163 (z : Int × Int) (n : Nat) (hn : QChar.chi 80 (LibRing.castZ (ZMod.ring (2 * 80 + 1 + 1)) (Int.ofNat n)) = -1) (h : en z = Int.ofNat n) : False := by
  rw [← h, cast_en] at hn
  by_cases h0 : Prim163.cr z = (ZMod.ring (2 * 80 + 1 + 1)).zero
  · rw [h0, show (ZMod.ring (2 * 80 + 1 + 1)).mul (ZMod.ring (2 * 80 + 1 + 1)).zero (ZMod.ring (2 * 80 + 1 + 1)).zero = (ZMod.ring (2 * 80 + 1 + 1)).zero by lib_ring, QChar.chi_zero] at hn; exact Int.noConfusion hn
  · rw [QChar.chi_sq 80 Prim163.prime163 _ h0] at hn; exact Int.noConfusion hn

/-- **c_W(n) = 0 for n a non-residue mod 163.** -/
theorem cw163_zero (W : Int × Int → Int × Int) (n : Nat) (hn : QChar.chi 80 (LibRing.castZ (ZMod.ring (2 * 80 + 1 + 1)) (Int.ofNat n)) = -1) : cw163 W n = (Ring163.ring163 LibInt.intR).zero := by
  unfold cw163
  rw [sumK_ext (Ring163.ring163 LibInt.intR) _ (fun _ => (Ring163.ring163 LibInt.intR).zero) (fun z => by
    rw [I163_neg _ _ (fun h => en_mod163 z n hn h.1)]; lib_ring)]
  exact zsum163 _

def Pi : Int × Int := (Int.ofNat (P + 1 + 1), 0)

theorem en_Pi : en (Pi P) = Int.ofNat ((P + 1 + 1) * (P + 1 + 1)) := by
  show Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1) + Int.ofNat (P + 1 + 1) * 0 + 41 * (0 * 0) = Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1)
  lib_int

theorem nrm_Pi : nrm (Pi P) = (P + 1 + 1) * (P + 1 + 1) := Int.ofNat.inj (by rw [nrm_cast, en_Pi])

theorem cj_Pi : cj (Pi P) = Pi P := Prod.ext (by show Int.ofNat (P + 1 + 1) + 0 = Int.ofNat (P + 1 + 1); lib_int) (by show -(0 : Int) = 0; rfl)

theorem not_p (hc : QChar.chi 80 (LibRing.castZ (ZMod.ring (2 * 80 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) (z : Int × Int) (h : nrm z = P + 1 + 1) : False :=
  en_mod163 z (P + 1 + 1) hc (by rw [← nrm_cast, h])

theorem dvd_conv (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) : ∀ d, d ∣ (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1 :=
  fun d ⟨c, hc⟩ => hpr d ⟨c, by rw [hc]; lib_nat⟩

/-- the two factor case: a b = p², p ∤ … ⇒ a = 1 or b = 1 (a = p is excluded separately). -/
theorem split_sq (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (a b : Nat) (h : a * b = (P + 1 + 1) * (P + 1 + 1)) :
    a = 1 ∨ b = 1 ∨ a = P + 1 + 1 := by
  cases euclid_prime (dvd_conv P hpr) (show (P + 1 + 1) ∣ a * b from ⟨P + 1 + 1, h⟩) with
  | inl ha =>
    obtain ⟨s, hs⟩ := ha
    have e : s * b = P + 1 + 1 := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (P + 1)) (by rw [show (P + 1 + 1) * (s * b) = (P + 1 + 1) * s * b by lib_nat, ← hs, h])
    cases hpr s ⟨b, by rw [← e]; lib_nat⟩ with
    | inl s1 => exact Or.inr (Or.inr (by rw [hs, s1]; lib_nat))
    | inr sp =>
      refine Or.inr (Or.inl ?_)
      rw [sp] at e
      exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (P + 1)) (e.trans (Nat.mul_one _).symm)
  | inr hb =>
    obtain ⟨s, hs⟩ := hb
    have e : a * s = P + 1 + 1 := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (P + 1)) (by
      rw [show (P + 1 + 1) * (a * s) = a * ((P + 1 + 1) * s) by lib_nat, ← hs, h])
    cases hpr a ⟨s, by rw [← e]; lib_nat⟩ with
    | inl a1 => exact Or.inl a1
    | inr ap => exact Or.inr (Or.inr ap)

/-- **p ≡ 2 (mod 3) stays prime in ℤ[ω].** -/
theorem inert_div (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : QChar.chi 80 (LibRing.castZ (ZMod.ring (2 * 80 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) (x y : Int × Int)
    (h : Dvd' (Pi P) ((Ring163.ring163 LibInt.intR).mul x y)) : Dvd' (Pi P) x ∨ Dvd' (Pi P) y := by
  match egcd_exists (Pi P) x with
  | ⟨d, X, Y, hd, ⟨c1, hc1⟩, ⟨c2, hc2⟩⟩ =>
    have hdc : nrm d * nrm c1 = (P + 1 + 1) * (P + 1 + 1) := by rw [← nrm_mul, ← hc1, nrm_Pi]
    rcases split_sq P hpr _ _ hdc with h1 | h1 | h1
    · refine Or.inr (eeuclid (Pi P) x y ((Ring163.ring163 LibInt.intR).mul (cj d) X) ((Ring163.ring163 LibInt.intR).mul (cj d) Y) ?_ h)
      rw [show (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul (cj d) X) (Pi P)) ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul (cj d) Y) x)
          = (Ring163.ring163 LibInt.intR).mul (cj d) ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul X (Pi P)) ((Ring163.ring163 LibInt.intR).mul Y x)) by lib_ring,
        ← hd, (Ring163.ring163 LibInt.intR).mul_comm, unit_inv d h1]
    · refine Or.inl ⟨(Ring163.ring163 LibInt.intR).mul (cj c1) c2, ?_⟩
      have hd' : d = (Ring163.ring163 LibInt.intR).mul (Pi P) (cj c1) := by
        rw [hc1]
        calc d = (Ring163.ring163 LibInt.intR).mul d (Ring163.ring163 LibInt.intR).one := by lib_ring
          _ = _ := by rw [← unit_inv c1 h1]; lib_ring
      rw [hc2, hd']; lib_ring
    · exact (not_p P hc d h1).elim

theorem nat_pow2 (k : Nat) : (P + 1 + 1) ^ (2 * (k + 1)) = (P + 1 + 1) * (P + 1 + 1) * (P + 1 + 1) ^ (2 * k) := by
  rw [show 2 * (k + 1) = 2 * k + 1 + 1 by lib_nat]
  show (P + 1 + 1) ^ (2 * k) * (P + 1 + 1) * (P + 1 + 1) = _
  lib_nat

/-- **Factorisation above an inert prime.** -/
theorem factor_inert (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : QChar.chi 80 (LibRing.castZ (ZMod.ring (2 * 80 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) : ∀ k (α : Int × Int),
    nrm α = (P + 1 + 1) ^ (2 * k) → ∃ u, nrm u = 1 ∧ α = (Ring163.ring163 LibInt.intR).mul u (pw (Ring163.ring163 LibInt.intR) (Pi P) k)
  | 0, α, h => ⟨α, h, by show α = (Ring163.ring163 LibInt.intR).mul α (Ring163.ring163 LibInt.intR).one; lib_ring⟩
  | k + 1, α, h => by
    have hdiv : Dvd' (Pi P) ((Ring163.ring163 LibInt.intR).mul α (cj α)) := by
      refine ⟨(Int.ofNat ((P + 1 + 1) * (P + 1 + 1) ^ (2 * k)), 0), ?_⟩
      rw [mul_cj, ← nrm_cast, h, nat_pow2]
      apply Prod.ext
      · show Int.ofNat ((P + 1 + 1) * (P + 1 + 1) * (P + 1 + 1) ^ (2 * k)) = Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) * (P + 1 + 1) ^ (2 * k)) + -(0 * 0)
        show Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ (2 * k)) = _
        show _ = Int.ofNat (P + 1 + 1) * (Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ (2 * k))) + -(0 * 0)
        lib_int
      · show (0 : Int) = Int.ofNat (P + 1 + 1) * 0 + 0 * Int.ofNat ((P + 1 + 1) * (P + 1 + 1) ^ (2 * k)) + -(0 * 0); lib_int
    have cancel : ∀ β : Int × Int, nrm ((Ring163.ring163 LibInt.intR).mul (Pi P) β) = (P + 1 + 1) ^ (2 * (k + 1)) → nrm β = (P + 1 + 1) ^ (2 * k) := fun β hb => by
      rw [nrm_mul, nrm_Pi, nat_pow2] at hb
      exact Nat.eq_of_mul_eq_mul_left (Nat.mul_pos (Nat.zero_lt_succ _) (Nat.zero_lt_succ _)) hb
    have hβ : ∃ β, α = (Ring163.ring163 LibInt.intR).mul (Pi P) β := by
      cases inert_div P hpr hc α (cj α) hdiv with
      | inl hα => exact hα
      | inr hα =>
        obtain ⟨β, hβ⟩ := hα
        exact ⟨cj β, by rw [← cj_Pi P, ← cj_mul, ← hβ, cj_cj]⟩
    obtain ⟨β, hβ⟩ := hβ
    obtain ⟨u, hu, he⟩ := factor_inert hpr hc k β (cancel β (by rw [← hβ]; exact h))
    refine ⟨u, hu, ?_⟩
    rw [hβ, he]; show _ = (Ring163.ring163 LibInt.intR).mul u ((Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) (Pi P) k) (Pi P)); lib_ring

def nPi : Int × Int := (-Int.ofNat (P + 1 + 1), 0)

theorem prim_nPi (hc : QChar.chi 80 (LibRing.castZ (ZMod.ring (2 * 80 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) : Prim163 (nPi P) := by
  show QChar.chi 80 (Prim163.cr (nPi P)) = 1
  have e : Prim163.cr (nPi P) = (ZMod.ring (2 * 80 + 1 + 1)).neg (LibRing.castZ (ZMod.ring (2 * 80 + 1 + 1)) (Int.ofNat (P + 1 + 1))) := by
    unfold Prim163.cr; rw [show Prim163.rho (nPi P) = -Int.ofNat (P + 1 + 1) by show -Int.ofNat (P + 1 + 1) + 82 * 0 = _; lib_int, HasseDavenport.castZ_neg]
  rw [e, Prim163.chi_neg, hc]; rfl

theorem pw_neg (k : Nat) : pw (Ring163.ring163 LibInt.intR) (Pi P) k =
    (Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) ((Ring163.ring163 LibInt.intR).neg (Ring163.ring163 LibInt.intR).one) k) (pw (Ring163.ring163 LibInt.intR) (nPi P) k) := by
  rw [← Frobenius.pw_mul]
  congr 1
  apply Prod.ext
  · show Int.ofNat (P + 1 + 1) = -1 * -Int.ofNat (P + 1 + 1) + -(-0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0  -- (+41 chars; full line in the Zenodo source)
  · show (0 : Int) = -1 * 0 + -0 * -Int.ofNat (P + 1 + 1) + -0 * 0; lib_int

theorem nrm_neg_one_pw : ∀ k, nrm (pw (Ring163.ring163 LibInt.intR) ((Ring163.ring163 LibInt.intR).neg (Ring163.ring163 LibInt.intR).one) k) = 1
  | 0 => rfl
  | k + 1 => by
    show nrm ((Ring163.ring163 LibInt.intR).mul _ _) = 1
    rw [nrm_mul, nrm_neg_one_pw k]; rfl

/-- **The primary elements of norm p^(2k) are (−p)^k.** -/
theorem prim_inert (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : QChar.chi 80 (LibRing.castZ (ZMod.ring (2 * 80 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) (k : Nat) (α : Int × Int)
    (hα : nrm α = (P + 1 + 1) ^ (2 * k)) (pα : Prim163 α) : α = pw (Ring163.ring163 LibInt.intR) (nPi P) k := by
  obtain ⟨u, hu, he⟩ := factor_inert P hpr hc k α hα
  rw [pw_neg] at he
  generalize hv : (Ring163.ring163 LibInt.intR).mul u (pw (Ring163.ring163 LibInt.intR) ((Ring163.ring163 LibInt.intR).neg (Ring163.ring163 LibInt.intR).one) k) = v
  have he' : α = (Ring163.ring163 LibInt.intR).mul v (pw (Ring163.ring163 LibInt.intR) (nPi P) k) := by
    rw [he, ← hv]; lib_ring
  have hv1 : nrm v = 1 := by rw [← hv, nrm_mul, hu, nrm_neg_one_pw]
  have v1 := unit_one v _ (nrm_one_eq v hv1) (prim163_pow _ (prim_nPi P hc) k) (by rw [← he']; exact pα)
  rw [he', v1]; lib_ring

theorem nrm_nPi : nrm (nPi P) = (P + 1 + 1) * (P + 1 + 1) := by
  apply Int.ofNat.inj; rw [nrm_cast]
  show -Int.ofNat (P + 1 + 1) * -Int.ofNat (P + 1 + 1) + -Int.ofNat (P + 1 + 1) * 0 + 41 * (0 * 0) = Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1)
  lib_int

theorem nrm_nPi_pw (k : Nat) : nrm (pw (Ring163.ring163 LibInt.intR) (nPi P) k) = (P + 1 + 1) ^ (2 * k) := by
  rw [HeckePrime163.nrm_pw, nrm_nPi]
  induction k with
  | zero => rfl
  | succ k ih =>
    show ((P + 1 + 1) * (P + 1 + 1)) ^ k * ((P + 1 + 1) * (P + 1 + 1)) = _
    rw [ih, nat_pow2]; lib_nat

/-- **c_W(p^(2k)) = W((−p)^k)** at an inert prime. -/
theorem cw163_even (W : Int × Int → Int × Int) (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : QChar.chi 80 (LibRing.castZ (ZMod.ring (2 * 80 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) (k : Nat) :
    cw163 W ((P + 1 + 1) ^ (2 * k)) = W (pw (Ring163.ring163 LibInt.intR) (nPi P) k) := by
  have hPD : PD163 ((P + 1 + 1) ^ (2 * k)) (pw (Ring163.ring163 LibInt.intR) (nPi P) k) :=
    ⟨by rw [← nrm_cast, nrm_nPi_pw], prim163_pow _ (prim_nPi P hc) k⟩
  unfold cw163
  rw [sumK_ext (Ring163.ring163 LibInt.intR) _ (fun z => (Ring163.ring163 LibInt.intR).mul (delta (Ring163.ring163 LibInt.intR) z (pw (Ring163.ring163 LibInt.intR) (nPi P) k)) (W z)) (fun z => by
    by_cases hz : PD163 ((P + 1 + 1) ^ (2 * k)) z
    · have e := prim_inert P hpr hc k z (Int.ofNat.inj (by rw [nrm_cast, hz.1])) hz.2
      rw [I163_pos _ _ hz]; unfold delta; rw [ite_pos' _ _ e]
    · rw [I163_neg _ _ hz]; unfold delta
      rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact hPD))])]
  have hb := abs_le163 _ _ hPD.1
  exact HeckeMult.pick_box (Ring163.ring163 LibInt.intR) W _ _ hb.1 hb.2

theorem pow_odd (hc : QChar.chi 80 (LibRing.castZ (ZMod.ring (2 * 80 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) : ∀ k, QChar.chi 80 (LibRing.castZ (ZMod.ring (2 * 80 + 1 + 1)) (Int.ofNat ((P + 1 + 1) ^ (2 * k + 1)))) = -1
  | 0 => by rw [show (P + 1 + 1) ^ (2 * 0 + 1) = P + 1 + 1 from Nat.one_mul _]; exact hc
  | k + 1 => by
    rw [show (P + 1 + 1) ^ (2 * (k + 1) + 1) = (P + 1 + 1) ^ (2 * k + 1) * ((P + 1 + 1) * (P + 1 + 1)) by
      rw [show 2 * (k + 1) + 1 = 2 * k + 1 + 1 + 1 by lib_nat]; show (P + 1 + 1) ^ (2 * k + 1) * (P + 1 + 1) * (P + 1 + 1) = _; lib_nat]
    show QChar.chi 80 (LibRing.castZ (ZMod.ring (2 * 80 + 1 + 1)) (Int.ofNat ((P + 1 + 1) ^ (2 * k + 1)) * (Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1)))) = -1
    rw [HasseDavenport.castZ_mul, HasseDavenport.castZ_mul, QChar.chi_mul 80 Prim163.prime163, QChar.chi_mul 80 Prim163.prime163, pow_odd hc k, hc]; rfl

/-- **c_W(p^(2k+1)) = 0** at an inert prime. -/
theorem cw163_odd (W : Int × Int → Int × Int) (hc : QChar.chi 80 (LibRing.castZ (ZMod.ring (2 * 80 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) (k : Nat) : cw163 W ((P + 1 + 1) ^ (2 * k + 1)) = (Ring163.ring163 LibInt.intR).zero := by
  exact cw163_zero W _ (pow_odd P hc k)

end Inert163

#print axioms Inert163.sqgen2
#print axioms Inert163.cast_en
#print axioms Inert163.en_mod163
#print axioms Inert163.cw163_zero
#print axioms Inert163.en_Pi
#print axioms Inert163.nrm_Pi
#print axioms Inert163.cj_Pi
#print axioms Inert163.not_p
#print axioms Inert163.dvd_conv
#print axioms Inert163.split_sq
#print axioms Inert163.inert_div
#print axioms Inert163.nat_pow2
#print axioms Inert163.factor_inert
#print axioms Inert163.prim_nPi
#print axioms Inert163.pw_neg
#print axioms Inert163.nrm_neg_one_pw
#print axioms Inert163.prim_inert
#print axioms Inert163.nrm_nPi
#print axioms Inert163.nrm_nPi_pw
#print axioms Inert163.cw163_even
#print axioms Inert163.pow_odd
#print axioms Inert163.cw163_odd

lang/Inert19.lean

lean_all/lang/Inert19.lean

/-
  lang/Inert19 — T48/T50, cycle 665: **the ℚ(√−19) Hecke character (361a) at inert primes** (p a non-square mod 19):
    en_mod19       N ≡ (a + 10b)² (mod 19) (20N = (a + 10b)² + 19a²) is never a non-square mod 19
    cw19_zero, inert_div, factor_inert, prim_inert, cw19_even, cw19_odd    (port of Inert11; residue cases generated)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.HeckePrime19

namespace Inert19

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius Euc19 Prim19 Uniq19 Prime19 HeckeMult19

variable (P : Nat)

theorem prodD (X Z K M c s D : Int) (hX : X = D * K + c) (hZ : Z = D * M + s) :
    X * Z = D * (D * K * M + K * s + c * M) + c * s := by
  rw [hX, hZ]; lib_int

theorem prod19 (X Z K M c s : Int) (hX : X = 19 * K + c) (hZ : Z = 19 * M + s) :
    X * Z = 19 * (19 * K * M + K * s + c * M) + c * s := prodD X Z K M c s 19 hX hZ

set_option maxHeartbeats 0 in
theorem nsq_0 (N k X : Int) (key : N = 0 + 19 * X) (h : N = 19 * k + 2 ∨ N = 19 * k + 3 ∨ N = 19 * k + 8 ∨ N = 19 * k + 10 ∨ N = 19 * k + 12 ∨ N = 19 * k + 13 ∨ N = 19 * k + 14 ∨ N = 19 * k + 15 ∨ N = 19 * k + 18) : False := by
  rcases h with h | h | h | h | h | h | h | h | h
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 16 1 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 2) + -(0 + 19 * X) + 17 + -(-2) + (0 + -2) := by lib_int
        _ = N + -N + 17 := by rw [← h, ← key]; lib_int
        _ = 17 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 15 2 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 3) + -(0 + 19 * X) + 16 + -(-3) + (0 + -3) := by lib_int
        _ = N + -N + 16 := by rw [← h, ← key]; lib_int
        _ = 16 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 10 7 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 8) + -(0 + 19 * X) + 11 + -(-8) + (0 + -8) := by lib_int
        _ = N + -N + 11 := by rw [← h, ← key]; lib_int
        _ = 11 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 8 9 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 10) + -(0 + 19 * X) + 9 + -(-10) + (0 + -10) := by lib_int
        _ = N + -N + 9 := by rw [← h, ← key]; lib_int
        _ = 9 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 6 11 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 12) + -(0 + 19 * X) + 7 + -(-12) + (0 + -12) := by lib_int
        _ = N + -N + 7 := by rw [← h, ← key]; lib_int
        _ = 7 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 5 12 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 13) + -(0 + 19 * X) + 6 + -(-13) + (0 + -13) := by lib_int
        _ = N + -N + 6 := by rw [← h, ← key]; lib_int
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 4 13 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 14) + -(0 + 19 * X) + 5 + -(-14) + (0 + -14) := by lib_int
        _ = N + -N + 5 := by rw [← h, ← key]; lib_int
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 3 14 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 15) + -(0 + 19 * X) + 4 + -(-15) + (0 + -15) := by lib_int
        _ = N + -N + 4 := by rw [← h, ← key]; lib_int
        _ = 4 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 0 17 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 18) + -(0 + 19 * X) + 1 + -(-18) + (0 + -18) := by lib_int
        _ = N + -N + 1 := by rw [← h, ← key]; lib_int
        _ = 1 := by lib_int) rfl

set_option maxHeartbeats 0 in
theorem nsq_1 (N k X : Int) (key : N = 1 + 19 * X) (h : N = 19 * k + 2 ∨ N = 19 * k + 3 ∨ N = 19 * k + 8 ∨ N = 19 * k + 10 ∨ N = 19 * k + 12 ∨ N = 19 * k + 13 ∨ N = 19 * k + 14 ∨ N = 19 * k + 15 ∨ N = 19 * k + 18) : False := by
  rcases h with h | h | h | h | h | h | h | h | h
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 17 0 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 2) + -(1 + 19 * X) + 18 + -(-1) + (1 + -2) := by lib_int
        _ = N + -N + 18 := by rw [← h, ← key]; lib_int
        _ = 18 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 16 1 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 3) + -(1 + 19 * X) + 17 + -(-2) + (1 + -3) := by lib_int
        _ = N + -N + 17 := by rw [← h, ← key]; lib_int
        _ = 17 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 11 6 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 8) + -(1 + 19 * X) + 12 + -(-7) + (1 + -8) := by lib_int
        _ = N + -N + 12 := by rw [← h, ← key]; lib_int
        _ = 12 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 9 8 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 10) + -(1 + 19 * X) + 10 + -(-9) + (1 + -10) := by lib_int
        _ = N + -N + 10 := by rw [← h, ← key]; lib_int
        _ = 10 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 7 10 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 12) + -(1 + 19 * X) + 8 + -(-11) + (1 + -12) := by lib_int
        _ = N + -N + 8 := by rw [← h, ← key]; lib_int
        _ = 8 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 6 11 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 13) + -(1 + 19 * X) + 7 + -(-12) + (1 + -13) := by lib_int
        _ = N + -N + 7 := by rw [← h, ← key]; lib_int
        _ = 7 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 5 12 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 14) + -(1 + 19 * X) + 6 + -(-13) + (1 + -14) := by lib_int
        _ = N + -N + 6 := by rw [← h, ← key]; lib_int
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 4 13 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 15) + -(1 + 19 * X) + 5 + -(-14) + (1 + -15) := by lib_int
        _ = N + -N + 5 := by rw [← h, ← key]; lib_int
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 1 16 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 18) + -(1 + 19 * X) + 2 + -(-17) + (1 + -18) := by lib_int
        _ = N + -N + 2 := by rw [← h, ← key]; lib_int
        _ = 2 := by lib_int) rfl

set_option maxHeartbeats 0 in
theorem nsq_2 (N k X : Int) (key : N = 4 + 19 * X) (h : N = 19 * k + 2 ∨ N = 19 * k + 3 ∨ N = 19 * k + 8 ∨ N = 19 * k + 10 ∨ N = 19 * k + 12 ∨ N = 19 * k + 13 ∨ N = 19 * k + 14 ∨ N = 19 * k + 15 ∨ N = 19 * k + 18) : False := by
  rcases h with h | h | h | h | h | h | h | h | h
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 1 16 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 2) + -(4 + 19 * X) + 2 + -(2) + (4 + -2) := by lib_int
        _ = N + -N + 2 := by rw [← h, ← key]; lib_int
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 0 17 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 3) + -(4 + 19 * X) + 1 + -(1) + (4 + -3) := by lib_int
        _ = N + -N + 1 := by rw [← h, ← key]; lib_int
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 14 3 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 8) + -(4 + 19 * X) + 15 + -(-4) + (4 + -8) := by lib_int
        _ = N + -N + 15 := by rw [← h, ← key]; lib_int
        _ = 15 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 12 5 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 10) + -(4 + 19 * X) + 13 + -(-6) + (4 + -10) := by lib_int
        _ = N + -N + 13 := by rw [← h, ← key]; lib_int
        _ = 13 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 10 7 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 12) + -(4 + 19 * X) + 11 + -(-8) + (4 + -12) := by lib_int
        _ = N + -N + 11 := by rw [← h, ← key]; lib_int
        _ = 11 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 9 8 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 13) + -(4 + 19 * X) + 10 + -(-9) + (4 + -13) := by lib_int
        _ = N + -N + 10 := by rw [← h, ← key]; lib_int
        _ = 10 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 8 9 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 14) + -(4 + 19 * X) + 9 + -(-10) + (4 + -14) := by lib_int
        _ = N + -N + 9 := by rw [← h, ← key]; lib_int
        _ = 9 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 7 10 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 15) + -(4 + 19 * X) + 8 + -(-11) + (4 + -15) := by lib_int
        _ = N + -N + 8 := by rw [← h, ← key]; lib_int
        _ = 8 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 4 13 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 18) + -(4 + 19 * X) + 5 + -(-14) + (4 + -18) := by lib_int
        _ = N + -N + 5 := by rw [← h, ← key]; lib_int
        _ = 5 := by lib_int) rfl

set_option maxHeartbeats 0 in
theorem nsq_3 (N k X : Int) (key : N = 9 + 19 * X) (h : N = 19 * k + 2 ∨ N = 19 * k + 3 ∨ N = 19 * k + 8 ∨ N = 19 * k + 10 ∨ N = 19 * k + 12 ∨ N = 19 * k + 13 ∨ N = 19 * k + 14 ∨ N = 19 * k + 15 ∨ N = 19 * k + 18) : False := by
  rcases h with h | h | h | h | h | h | h | h | h
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 6 11 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 2) + -(9 + 19 * X) + 7 + -(7) + (9 + -2) := by lib_int
        _ = N + -N + 7 := by rw [← h, ← key]; lib_int
        _ = 7 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 5 12 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 3) + -(9 + 19 * X) + 6 + -(6) + (9 + -3) := by lib_int
        _ = N + -N + 6 := by rw [← h, ← key]; lib_int
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 0 17 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 8) + -(9 + 19 * X) + 1 + -(1) + (9 + -8) := by lib_int
        _ = N + -N + 1 := by rw [← h, ← key]; lib_int
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 17 0 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 10) + -(9 + 19 * X) + 18 + -(-1) + (9 + -10) := by lib_int
        _ = N + -N + 18 := by rw [← h, ← key]; lib_int
        _ = 18 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 15 2 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 12) + -(9 + 19 * X) + 16 + -(-3) + (9 + -12) := by lib_int
        _ = N + -N + 16 := by rw [← h, ← key]; lib_int
        _ = 16 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 14 3 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 13) + -(9 + 19 * X) + 15 + -(-4) + (9 + -13) := by lib_int
        _ = N + -N + 15 := by rw [← h, ← key]; lib_int
        _ = 15 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 13 4 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 14) + -(9 + 19 * X) + 14 + -(-5) + (9 + -14) := by lib_int
        _ = N + -N + 14 := by rw [← h, ← key]; lib_int
        _ = 14 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 12 5 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 15) + -(9 + 19 * X) + 13 + -(-6) + (9 + -15) := by lib_int
        _ = N + -N + 13 := by rw [← h, ← key]; lib_int
        _ = 13 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 9 8 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 18) + -(9 + 19 * X) + 10 + -(-9) + (9 + -18) := by lib_int
        _ = N + -N + 10 := by rw [← h, ← key]; lib_int
        _ = 10 := by lib_int) rfl

set_option maxHeartbeats 0 in
theorem nsq_4 (N k X : Int) (key : N = 16 + 19 * X) (h : N = 19 * k + 2 ∨ N = 19 * k + 3 ∨ N = 19 * k + 8 ∨ N = 19 * k + 10 ∨ N = 19 * k + 12 ∨ N = 19 * k + 13 ∨ N = 19 * k + 14 ∨ N = 19 * k + 15 ∨ N = 19 * k + 18) : False := by
  rcases h with h | h | h | h | h | h | h | h | h
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 13 4 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 2) + -(16 + 19 * X) + 14 + -(14) + (16 + -2) := by lib_int
        _ = N + -N + 14 := by rw [← h, ← key]; lib_int
        _ = 14 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 12 5 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 3) + -(16 + 19 * X) + 13 + -(13) + (16 + -3) := by lib_int
        _ = N + -N + 13 := by rw [← h, ← key]; lib_int
        _ = 13 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 7 10 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 8) + -(16 + 19 * X) + 8 + -(8) + (16 + -8) := by lib_int
        _ = N + -N + 8 := by rw [← h, ← key]; lib_int
        _ = 8 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 5 12 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 10) + -(16 + 19 * X) + 6 + -(6) + (16 + -10) := by lib_int
        _ = N + -N + 6 := by rw [← h, ← key]; lib_int
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 3 14 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 12) + -(16 + 19 * X) + 4 + -(4) + (16 + -12) := by lib_int
        _ = N + -N + 4 := by rw [← h, ← key]; lib_int
        _ = 4 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 2 15 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 13) + -(16 + 19 * X) + 3 + -(3) + (16 + -13) := by lib_int
        _ = N + -N + 3 := by rw [← h, ← key]; lib_int
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 1 16 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 14) + -(16 + 19 * X) + 2 + -(2) + (16 + -14) := by lib_int
        _ = N + -N + 2 := by rw [← h, ← key]; lib_int
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 0 17 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 15) + -(16 + 19 * X) + 1 + -(1) + (16 + -15) := by lib_int
        _ = N + -N + 1 := by rw [← h, ← key]; lib_int
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 16 1 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 18) + -(16 + 19 * X) + 17 + -(-2) + (16 + -18) := by lib_int
        _ = N + -N + 17 := by rw [← h, ← key]; lib_int
        _ = 17 := by lib_int) rfl

set_option maxHeartbeats 0 in
theorem nsq_5 (N k X : Int) (key : N = 25 + 19 * X) (h : N = 19 * k + 2 ∨ N = 19 * k + 3 ∨ N = 19 * k + 8 ∨ N = 19 * k + 10 ∨ N = 19 * k + 12 ∨ N = 19 * k + 13 ∨ N = 19 * k + 14 ∨ N = 19 * k + 15 ∨ N = 19 * k + 18) : False := by
  rcases h with h | h | h | h | h | h | h | h | h
  · exact Sq2Prim8.noM 18 (k + -X + -(1)) 3 14 (by calc (19 : Int) * (k + -X + -(1)) = (19 * k + 2) + -(25 + 19 * X) + 4 + -(23) + (25 + -2) := by lib_int
        _ = N + -N + 4 := by rw [← h, ← key]; lib_int
        _ = 4 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(1)) 2 15 (by calc (19 : Int) * (k + -X + -(1)) = (19 * k + 3) + -(25 + 19 * X) + 3 + -(22) + (25 + -3) := by lib_int
        _ = N + -N + 3 := by rw [← h, ← key]; lib_int
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 16 1 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 8) + -(25 + 19 * X) + 17 + -(17) + (25 + -8) := by lib_int
        _ = N + -N + 17 := by rw [← h, ← key]; lib_int
        _ = 17 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 14 3 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 10) + -(25 + 19 * X) + 15 + -(15) + (25 + -10) := by lib_int
        _ = N + -N + 15 := by rw [← h, ← key]; lib_int
        _ = 15 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 12 5 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 12) + -(25 + 19 * X) + 13 + -(13) + (25 + -12) := by lib_int
        _ = N + -N + 13 := by rw [← h, ← key]; lib_int
        _ = 13 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 11 6 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 13) + -(25 + 19 * X) + 12 + -(12) + (25 + -13) := by lib_int
        _ = N + -N + 12 := by rw [← h, ← key]; lib_int
        _ = 12 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 10 7 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 14) + -(25 + 19 * X) + 11 + -(11) + (25 + -14) := by lib_int
        _ = N + -N + 11 := by rw [← h, ← key]; lib_int
        _ = 11 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 9 8 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 15) + -(25 + 19 * X) + 10 + -(10) + (25 + -15) := by lib_int
        _ = N + -N + 10 := by rw [← h, ← key]; lib_int
        _ = 10 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 6 11 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 18) + -(25 + 19 * X) + 7 + -(7) + (25 + -18) := by lib_int
        _ = N + -N + 7 := by rw [← h, ← key]; lib_int
        _ = 7 := by lib_int) rfl

set_option maxHeartbeats 0 in
theorem nsq_6 (N k X : Int) (key : N = 36 + 19 * X) (h : N = 19 * k + 2 ∨ N = 19 * k + 3 ∨ N = 19 * k + 8 ∨ N = 19 * k + 10 ∨ N = 19 * k + 12 ∨ N = 19 * k + 13 ∨ N = 19 * k + 14 ∨ N = 19 * k + 15 ∨ N = 19 * k + 18) : False := by
  rcases h with h | h | h | h | h | h | h | h | h
  · exact Sq2Prim8.noM 18 (k + -X + -(1)) 14 3 (by calc (19 : Int) * (k + -X + -(1)) = (19 * k + 2) + -(36 + 19 * X) + 15 + -(34) + (36 + -2) := by lib_int
        _ = N + -N + 15 := by rw [← h, ← key]; lib_int
        _ = 15 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(1)) 13 4 (by calc (19 : Int) * (k + -X + -(1)) = (19 * k + 3) + -(36 + 19 * X) + 14 + -(33) + (36 + -3) := by lib_int
        _ = N + -N + 14 := by rw [← h, ← key]; lib_int
        _ = 14 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(1)) 8 9 (by calc (19 : Int) * (k + -X + -(1)) = (19 * k + 8) + -(36 + 19 * X) + 9 + -(28) + (36 + -8) := by lib_int
        _ = N + -N + 9 := by rw [← h, ← key]; lib_int
        _ = 9 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(1)) 6 11 (by calc (19 : Int) * (k + -X + -(1)) = (19 * k + 10) + -(36 + 19 * X) + 7 + -(26) + (36 + -10) := by lib_int
        _ = N + -N + 7 := by rw [← h, ← key]; lib_int
        _ = 7 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(1)) 4 13 (by calc (19 : Int) * (k + -X + -(1)) = (19 * k + 12) + -(36 + 19 * X) + 5 + -(24) + (36 + -12) := by lib_int
        _ = N + -N + 5 := by rw [← h, ← key]; lib_int
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(1)) 3 14 (by calc (19 : Int) * (k + -X + -(1)) = (19 * k + 13) + -(36 + 19 * X) + 4 + -(23) + (36 + -13) := by lib_int
        _ = N + -N + 4 := by rw [← h, ← key]; lib_int
        _ = 4 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(1)) 2 15 (by calc (19 : Int) * (k + -X + -(1)) = (19 * k + 14) + -(36 + 19 * X) + 3 + -(22) + (36 + -14) := by lib_int
        _ = N + -N + 3 := by rw [← h, ← key]; lib_int
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(1)) 1 16 (by calc (19 : Int) * (k + -X + -(1)) = (19 * k + 15) + -(36 + 19 * X) + 2 + -(21) + (36 + -15) := by lib_int
        _ = N + -N + 2 := by rw [← h, ← key]; lib_int
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 17 0 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 18) + -(36 + 19 * X) + 18 + -(18) + (36 + -18) := by lib_int
        _ = N + -N + 18 := by rw [← h, ← key]; lib_int
        _ = 18 := by lib_int) rfl

set_option maxHeartbeats 0 in
theorem nsq_7 (N k X : Int) (key : N = 49 + 19 * X) (h : N = 19 * k + 2 ∨ N = 19 * k + 3 ∨ N = 19 * k + 8 ∨ N = 19 * k + 10 ∨ N = 19 * k + 12 ∨ N = 19 * k + 13 ∨ N = 19 * k + 14 ∨ N = 19 * k + 15 ∨ N = 19 * k + 18) : False := by
  rcases h with h | h | h | h | h | h | h | h | h
  · exact Sq2Prim8.noM 18 (k + -X + -(2)) 8 9 (by calc (19 : Int) * (k + -X + -(2)) = (19 * k + 2) + -(49 + 19 * X) + 9 + -(47) + (49 + -2) := by lib_int
        _ = N + -N + 9 := by rw [← h, ← key]; lib_int
        _ = 9 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(2)) 7 10 (by calc (19 : Int) * (k + -X + -(2)) = (19 * k + 3) + -(49 + 19 * X) + 8 + -(46) + (49 + -3) := by lib_int
        _ = N + -N + 8 := by rw [← h, ← key]; lib_int
        _ = 8 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(2)) 2 15 (by calc (19 : Int) * (k + -X + -(2)) = (19 * k + 8) + -(49 + 19 * X) + 3 + -(41) + (49 + -8) := by lib_int
        _ = N + -N + 3 := by rw [← h, ← key]; lib_int
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(2)) 0 17 (by calc (19 : Int) * (k + -X + -(2)) = (19 * k + 10) + -(49 + 19 * X) + 1 + -(39) + (49 + -10) := by lib_int
        _ = N + -N + 1 := by rw [← h, ← key]; lib_int
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(1)) 17 0 (by calc (19 : Int) * (k + -X + -(1)) = (19 * k + 12) + -(49 + 19 * X) + 18 + -(37) + (49 + -12) := by lib_int
        _ = N + -N + 18 := by rw [← h, ← key]; lib_int
        _ = 18 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(1)) 16 1 (by calc (19 : Int) * (k + -X + -(1)) = (19 * k + 13) + -(49 + 19 * X) + 17 + -(36) + (49 + -13) := by lib_int
        _ = N + -N + 17 := by rw [← h, ← key]; lib_int
        _ = 17 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(1)) 15 2 (by calc (19 : Int) * (k + -X + -(1)) = (19 * k + 14) + -(49 + 19 * X) + 16 + -(35) + (49 + -14) := by lib_int
        _ = N + -N + 16 := by rw [← h, ← key]; lib_int
        _ = 16 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(1)) 14 3 (by calc (19 : Int) * (k + -X + -(1)) = (19 * k + 15) + -(49 + 19 * X) + 15 + -(34) + (49 + -15) := by lib_int
        _ = N + -N + 15 := by rw [← h, ← key]; lib_int
        _ = 15 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(1)) 11 6 (by calc (19 : Int) * (k + -X + -(1)) = (19 * k + 18) + -(49 + 19 * X) + 12 + -(31) + (49 + -18) := by lib_int
        _ = N + -N + 12 := by rw [← h, ← key]; lib_int
        _ = 12 := by lib_int) rfl

set_option maxHeartbeats 0 in
theorem nsq_8 (N k X : Int) (key : N = 64 + 19 * X) (h : N = 19 * k + 2 ∨ N = 19 * k + 3 ∨ N = 19 * k + 8 ∨ N = 19 * k + 10 ∨ N = 19 * k + 12 ∨ N = 19 * k + 13 ∨ N = 19 * k + 14 ∨ N = 19 * k + 15 ∨ N = 19 * k + 18) : False := by
  rcases h with h | h | h | h | h | h | h | h | h
  · exact Sq2Prim8.noM 18 (k + -X + -(3)) 4 13 (by calc (19 : Int) * (k + -X + -(3)) = (19 * k + 2) + -(64 + 19 * X) + 5 + -(62) + (64 + -2) := by lib_int
        _ = N + -N + 5 := by rw [← h, ← key]; lib_int
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(3)) 3 14 (by calc (19 : Int) * (k + -X + -(3)) = (19 * k + 3) + -(64 + 19 * X) + 4 + -(61) + (64 + -3) := by lib_int
        _ = N + -N + 4 := by rw [← h, ← key]; lib_int
        _ = 4 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(2)) 17 0 (by calc (19 : Int) * (k + -X + -(2)) = (19 * k + 8) + -(64 + 19 * X) + 18 + -(56) + (64 + -8) := by lib_int
        _ = N + -N + 18 := by rw [← h, ← key]; lib_int
        _ = 18 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(2)) 15 2 (by calc (19 : Int) * (k + -X + -(2)) = (19 * k + 10) + -(64 + 19 * X) + 16 + -(54) + (64 + -10) := by lib_int
        _ = N + -N + 16 := by rw [← h, ← key]; lib_int
        _ = 16 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(2)) 13 4 (by calc (19 : Int) * (k + -X + -(2)) = (19 * k + 12) + -(64 + 19 * X) + 14 + -(52) + (64 + -12) := by lib_int
        _ = N + -N + 14 := by rw [← h, ← key]; lib_int
        _ = 14 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(2)) 12 5 (by calc (19 : Int) * (k + -X + -(2)) = (19 * k + 13) + -(64 + 19 * X) + 13 + -(51) + (64 + -13) := by lib_int
        _ = N + -N + 13 := by rw [← h, ← key]; lib_int
        _ = 13 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(2)) 11 6 (by calc (19 : Int) * (k + -X + -(2)) = (19 * k + 14) + -(64 + 19 * X) + 12 + -(50) + (64 + -14) := by lib_int
        _ = N + -N + 12 := by rw [← h, ← key]; lib_int
        _ = 12 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(2)) 10 7 (by calc (19 : Int) * (k + -X + -(2)) = (19 * k + 15) + -(64 + 19 * X) + 11 + -(49) + (64 + -15) := by lib_int
        _ = N + -N + 11 := by rw [← h, ← key]; lib_int
        _ = 11 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(2)) 7 10 (by calc (19 : Int) * (k + -X + -(2)) = (19 * k + 18) + -(64 + 19 * X) + 8 + -(46) + (64 + -18) := by lib_int
        _ = N + -N + 8 := by rw [← h, ← key]; lib_int
        _ = 8 := by lib_int) rfl

set_option maxHeartbeats 0 in
theorem nsq_9 (N k X : Int) (key : N = 81 + 19 * X) (h : N = 19 * k + 2 ∨ N = 19 * k + 3 ∨ N = 19 * k + 8 ∨ N = 19 * k + 10 ∨ N = 19 * k + 12 ∨ N = 19 * k + 13 ∨ N = 19 * k + 14 ∨ N = 19 * k + 15 ∨ N = 19 * k + 18) : False := by
  rcases h with h | h | h | h | h | h | h | h | h
  · exact Sq2Prim8.noM 18 (k + -X + -(4)) 2 15 (by calc (19 : Int) * (k + -X + -(4)) = (19 * k + 2) + -(81 + 19 * X) + 3 + -(79) + (81 + -2) := by lib_int
        _ = N + -N + 3 := by rw [← h, ← key]; lib_int
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(4)) 1 16 (by calc (19 : Int) * (k + -X + -(4)) = (19 * k + 3) + -(81 + 19 * X) + 2 + -(78) + (81 + -3) := by lib_int
        _ = N + -N + 2 := by rw [← h, ← key]; lib_int
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(3)) 15 2 (by calc (19 : Int) * (k + -X + -(3)) = (19 * k + 8) + -(81 + 19 * X) + 16 + -(73) + (81 + -8) := by lib_int
        _ = N + -N + 16 := by rw [← h, ← key]; lib_int
        _ = 16 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(3)) 13 4 (by calc (19 : Int) * (k + -X + -(3)) = (19 * k + 10) + -(81 + 19 * X) + 14 + -(71) + (81 + -10) := by lib_int
        _ = N + -N + 14 := by rw [← h, ← key]; lib_int
        _ = 14 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(3)) 11 6 (by calc (19 : Int) * (k + -X + -(3)) = (19 * k + 12) + -(81 + 19 * X) + 12 + -(69) + (81 + -12) := by lib_int
        _ = N + -N + 12 := by rw [← h, ← key]; lib_int
        _ = 12 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(3)) 10 7 (by calc (19 : Int) * (k + -X + -(3)) = (19 * k + 13) + -(81 + 19 * X) + 11 + -(68) + (81 + -13) := by lib_int
        _ = N + -N + 11 := by rw [← h, ← key]; lib_int
        _ = 11 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(3)) 9 8 (by calc (19 : Int) * (k + -X + -(3)) = (19 * k + 14) + -(81 + 19 * X) + 10 + -(67) + (81 + -14) := by lib_int
        _ = N + -N + 10 := by rw [← h, ← key]; lib_int
        _ = 10 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(3)) 8 9 (by calc (19 : Int) * (k + -X + -(3)) = (19 * k + 15) + -(81 + 19 * X) + 9 + -(66) + (81 + -15) := by lib_int
        _ = N + -N + 9 := by rw [← h, ← key]; lib_int
        _ = 9 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(3)) 5 12 (by calc (19 : Int) * (k + -X + -(3)) = (19 * k + 18) + -(81 + 19 * X) + 6 + -(63) + (81 + -18) := by lib_int
        _ = N + -N + 6 := by rw [← h, ← key]; lib_int
        _ = 6 := by lib_int) rfl

set_option maxHeartbeats 0 in
theorem en_mod19 (z : Int × Int) (k : Int) (h : en z = 19 * k + 2 ∨ en z = 19 * k + 3 ∨ en z = 19 * k + 8 ∨ en z = 19 * k + 10 ∨ en z = 19 * k + 12 ∨ en z = 19 * k + 13 ∨ en z = 19 * k + 14 ∨ en z = 19 * k + 15 ∨ en z = 19 * k + 18) : False := by
  obtain ⟨a, b⟩ := z
  have hn : en (a, b) = a * a + a * b + 5 * (b * b) := rfl
  obtain ⟨q, r, hq, hr⟩ := GaussEuclid.rnd (a + 10 * b) 18
  have hq' : a + 10 * b = 19 * q + r := by rw [hq]; show q * 19 + r = 19 * q + r; lib_int
  have key : en (a, b) = r * r + 19 * (19 * q * q + 2 * q * r + a * a + -en (a, b)) := by
    have e20 : 20 * en (a, b) = (a + 10 * b) * (a + 10 * b) + 19 * (a * a) := by rw [hn]; lib_int
    rw [hq'] at e20
    calc en (a, b) = 20 * en (a, b) + -(19 * en (a, b)) := by lib_int
      _ = (19 * q + r) * (19 * q + r) + 19 * (a * a) + -(19 * en (a, b)) := by rw [e20]
      _ = _ := by rw [prod19 (19 * q + r) (19 * q + r) q q r r rfl rfl]; generalize 19 * q * q = Q; lib_int
  generalize en (a, b) = N at key h
  generalize 19 * q * q + 2 * q * r + a * a + -N = X at key
  have cases_r : r = 0 ∨ r = 1 ∨ r = 2 ∨ r = 3 ∨ r = 4 ∨ r = 5 ∨ r = 6 ∨ r = 7 ∨ r = 8 ∨ r = 9 ∨ r = -1 ∨ r = -2 ∨ r = -3 ∨ r = -4 ∨ r = -5 ∨ r = -6 ∨ r = -7 ∨ r = -8 ∨ r = -9 := by
    cases r with
    | ofNat t =>
      match t, hr with
      | 0, _ => exact (Or.inl (rfl))
      | 1, _ => exact (Or.inr (Or.inl (rfl)))
      | 2, _ => exact (Or.inr (Or.inr (Or.inl (rfl))))
      | 3, _ => exact (Or.inr (Or.inr (Or.inr (Or.inl (rfl)))))
      | 4, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (rfl))))))
      | 5, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (rfl)))))))
      | 6, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (rfl))))))))
      | 7, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (rfl)))))))))
      | 8, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (rfl))))))))))
      | 9, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (rfl)))))))))))
      | t + 10, hr' =>
        exfalso
        have : 2 * (t + 10) ≤ 19 := hr'
        rw [show 2 * (t + 10) = 2 * t + 1 + 19 by lib_nat] at this
        exact Nat.not_succ_le_self 19 (Nat.le_trans (Nat.add_le_add_right (Nat.zero_le (2 * t)) 20) (Nat.le_trans (Nat.le_of_eq (by lib_nat)) this))
    | negSucc t =>
      match t, hr with
      | 0, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (rfl))))))))))))
      | 1, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (rfl)))))))))))))
      | 2, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (rfl))))))))))))))
      | 3, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (rfl)))))))))))))))
      | 4, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (rfl))))))))))))))))
      | 5, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (rfl)))))))))))))))))
      | 6, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (rfl))))))))))))))))))
      | 7, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (rfl)))))))))))))))))))
      | 8, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (rfl)))))))))))))))))))
      | t + 9, hr' =>
        exfalso
        have : 2 * (t + 10) ≤ 19 := hr'
        rw [show 2 * (t + 10) = 2 * t + 1 + 19 by lib_nat] at this
        exact Nat.not_succ_le_self 19 (Nat.le_trans (Nat.add_le_add_right (Nat.zero_le (2 * t)) 20) (Nat.le_trans (Nat.le_of_eq (by lib_nat)) this))
  rcases cases_r with e | e | e | e | e | e | e | e | e | e | e | e | e | e | e | e | e | e | e <;> rw [e] at key
  · exact nsq_0 N k X (by rw [key]; lib_int) h
  · exact nsq_1 N k X (by rw [key]; lib_int) h
  · exact nsq_2 N k X (by rw [key]; lib_int) h
  · exact nsq_3 N k X (by rw [key]; lib_int) h
  · exact nsq_4 N k X (by rw [key]; lib_int) h
  · exact nsq_5 N k X (by rw [key]; lib_int) h
  · exact nsq_6 N k X (by rw [key]; lib_int) h
  · exact nsq_7 N k X (by rw [key]; lib_int) h
  · exact nsq_8 N k X (by rw [key]; lib_int) h
  · exact nsq_9 N k X (by rw [key]; lib_int) h
  · exact nsq_1 N k X (by rw [key]; lib_int) h
  · exact nsq_2 N k X (by rw [key]; lib_int) h
  · exact nsq_3 N k X (by rw [key]; lib_int) h
  · exact nsq_4 N k X (by rw [key]; lib_int) h
  · exact nsq_5 N k X (by rw [key]; lib_int) h
  · exact nsq_6 N k X (by rw [key]; lib_int) h
  · exact nsq_7 N k X (by rw [key]; lib_int) h
  · exact nsq_8 N k X (by rw [key]; lib_int) h
  · exact nsq_9 N k X (by rw [key]; lib_int) h

/-- **c_W(n) = 0 for n a non-square mod 19.** -/
theorem cw19_zero (W : Int × Int → Int × Int) (n : Nat) (k : Int) (hn : Int.ofNat n = 19 * k + 2 ∨ Int.ofNat n = 19 * k + 3 ∨ Int.ofNat n = 19 * k + 8 ∨ Int.ofNat n = 19 * k + 10 ∨ Int.ofNat n = 19 * k + 12 ∨ Int.ofNat n = 19 * k + 13 ∨ Int.ofNat n = 19 * k + 14 ∨ Int.ofNat n = 19 * k + 15 ∨ Int.ofNat n = 19 * k + 18) : cw19 W n = (Ring19.ring19 LibInt.intR).zero := by
  unfold cw19
  rw [sumK_ext (Ring19.ring19 LibInt.intR) _ (fun _ => (Ring19.ring19 LibInt.intR).zero) (fun z => by
    rw [I19_neg _ _ (fun h => en_mod19 z k (by rw [h.1]; exact hn))]; lib_ring)]
  exact zsum19 _

def Pi : Int × Int := (Int.ofNat (P + 1 + 1), 0)

theorem en_Pi : en (Pi P) = Int.ofNat ((P + 1 + 1) * (P + 1 + 1)) := by
  show Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1) + Int.ofNat (P + 1 + 1) * 0 + 5 * (0 * 0) = Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1)
  lib_int

theorem nrm_Pi : nrm (Pi P) = (P + 1 + 1) * (P + 1 + 1) := Int.ofNat.inj (by rw [nrm_cast, en_Pi])

theorem cj_Pi : cj (Pi P) = Pi P := Prod.ext (by show Int.ofNat (P + 1 + 1) + 0 = Int.ofNat (P + 1 + 1); lib_int) (by show -(0 : Int) = 0; rfl)

theorem not_p (hc : ∃ K, Int.ofNat (P + 1 + 1) = 19 * K + 2 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 3 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 8 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 10 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 12 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 13 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 14 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 15 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 18) (z : Int × Int) (h : nrm z = P + 1 +   -- (+16 chars; full line in the Zenodo source)
  obtain ⟨K, hK⟩ := hc
  exact en_mod19 z K (by rw [← nrm_cast, h]; exact hK)

theorem dvd_conv (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) : ∀ d, d ∣ (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1 :=
  fun d ⟨c, hc⟩ => hpr d ⟨c, by rw [hc]; lib_nat⟩

/-- the two factor case: a b = p², p ∤ … ⇒ a = 1 or b = 1 (a = p is excluded separately). -/
theorem split_sq (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (a b : Nat) (h : a * b = (P + 1 + 1) * (P + 1 + 1)) :
    a = 1 ∨ b = 1 ∨ a = P + 1 + 1 := by
  cases euclid_prime (dvd_conv P hpr) (show (P + 1 + 1) ∣ a * b from ⟨P + 1 + 1, h⟩) with
  | inl ha =>
    obtain ⟨s, hs⟩ := ha
    have e : s * b = P + 1 + 1 := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (P + 1)) (by rw [show (P + 1 + 1) * (s * b) = (P + 1 + 1) * s * b by lib_nat, ← hs, h])
    cases hpr s ⟨b, by rw [← e]; lib_nat⟩ with
    | inl s1 => exact Or.inr (Or.inr (by rw [hs, s1]; lib_nat))
    | inr sp =>
      refine Or.inr (Or.inl ?_)
      rw [sp] at e
      exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (P + 1)) (e.trans (Nat.mul_one _).symm)
  | inr hb =>
    obtain ⟨s, hs⟩ := hb
    have e : a * s = P + 1 + 1 := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (P + 1)) (by
      rw [show (P + 1 + 1) * (a * s) = a * ((P + 1 + 1) * s) by lib_nat, ← hs, h])
    cases hpr a ⟨s, by rw [← e]; lib_nat⟩ with
    | inl a1 => exact Or.inl a1
    | inr ap => exact Or.inr (Or.inr ap)

/-- **p ≡ 2 (mod 3) stays prime in ℤ[ω].** -/
theorem inert_div (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : ∃ K, Int.ofNat (P + 1 + 1) = 19 * K + 2 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 3 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 8 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 10 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 12 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 13 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 14 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 15 ∨ Int.ofNat (P + 1  -- (+38 chars; full line in the Zenodo source)
    (h : Dvd' (Pi P) ((Ring19.ring19 LibInt.intR).mul x y)) : Dvd' (Pi P) x ∨ Dvd' (Pi P) y := by
  match egcd_exists (Pi P) x with
  | ⟨d, X, Y, hd, ⟨c1, hc1⟩, ⟨c2, hc2⟩⟩ =>
    have hdc : nrm d * nrm c1 = (P + 1 + 1) * (P + 1 + 1) := by rw [← nrm_mul, ← hc1, nrm_Pi]
    rcases split_sq P hpr _ _ hdc with h1 | h1 | h1
    · refine Or.inr (eeuclid (Pi P) x y ((Ring19.ring19 LibInt.intR).mul (cj d) X) ((Ring19.ring19 LibInt.intR).mul (cj d) Y) ?_ h)
      rw [show (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul (cj d) X) (Pi P)) ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul (cj d) Y) x)
          = (Ring19.ring19 LibInt.intR).mul (cj d) ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul X (Pi P)) ((Ring19.ring19 LibInt.intR).mul Y x)) by lib_ring,
        ← hd, (Ring19.ring19 LibInt.intR).mul_comm, unit_inv d h1]
    · refine Or.inl ⟨(Ring19.ring19 LibInt.intR).mul (cj c1) c2, ?_⟩
      have hd' : d = (Ring19.ring19 LibInt.intR).mul (Pi P) (cj c1) := by
        rw [hc1]
        calc d = (Ring19.ring19 LibInt.intR).mul d (Ring19.ring19 LibInt.intR).one := by lib_ring
          _ = _ := by rw [← unit_inv c1 h1]; lib_ring
      rw [hc2, hd']; lib_ring
    · exact (not_p P hc d h1).elim

theorem nat_pow2 (k : Nat) : (P + 1 + 1) ^ (2 * (k + 1)) = (P + 1 + 1) * (P + 1 + 1) * (P + 1 + 1) ^ (2 * k) := by
  rw [show 2 * (k + 1) = 2 * k + 1 + 1 by lib_nat]
  show (P + 1 + 1) ^ (2 * k) * (P + 1 + 1) * (P + 1 + 1) = _
  lib_nat

/-- **Factorisation above an inert prime.** -/
theorem factor_inert (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : ∃ K, Int.ofNat (P + 1 + 1) = 19 * K + 2 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 3 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 8 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 10 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 12 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 13 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 14 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 15 ∨ Int.ofNat (P   -- (+46 chars; full line in the Zenodo source)
    nrm α = (P + 1 + 1) ^ (2 * k) → ∃ u, nrm u = 1 ∧ α = (Ring19.ring19 LibInt.intR).mul u (pw (Ring19.ring19 LibInt.intR) (Pi P) k)
  | 0, α, h => ⟨α, h, by show α = (Ring19.ring19 LibInt.intR).mul α (Ring19.ring19 LibInt.intR).one; lib_ring⟩
  | k + 1, α, h => by
    have hdiv : Dvd' (Pi P) ((Ring19.ring19 LibInt.intR).mul α (cj α)) := by
      refine ⟨(Int.ofNat ((P + 1 + 1) * (P + 1 + 1) ^ (2 * k)), 0), ?_⟩
      rw [mul_cj, ← nrm_cast, h, nat_pow2]
      apply Prod.ext
      · show Int.ofNat ((P + 1 + 1) * (P + 1 + 1) * (P + 1 + 1) ^ (2 * k)) = Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) * (P + 1 + 1) ^ (2 * k)) + -(0 * 0)
        show Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ (2 * k)) = _
        show _ = Int.ofNat (P + 1 + 1) * (Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ (2 * k))) + -(0 * 0)
        lib_int
      · show (0 : Int) = Int.ofNat (P + 1 + 1) * 0 + 0 * Int.ofNat ((P + 1 + 1) * (P + 1 + 1) ^ (2 * k)) + -(0 * 0); lib_int
    have cancel : ∀ β : Int × Int, nrm ((Ring19.ring19 LibInt.intR).mul (Pi P) β) = (P + 1 + 1) ^ (2 * (k + 1)) → nrm β = (P + 1 + 1) ^ (2 * k) := fun β hb => by
      rw [nrm_mul, nrm_Pi, nat_pow2] at hb
      exact Nat.eq_of_mul_eq_mul_left (Nat.mul_pos (Nat.zero_lt_succ _) (Nat.zero_lt_succ _)) hb
    have hβ : ∃ β, α = (Ring19.ring19 LibInt.intR).mul (Pi P) β := by
      cases inert_div P hpr hc α (cj α) hdiv with
      | inl hα => exact hα
      | inr hα =>
        obtain ⟨β, hβ⟩ := hα
        exact ⟨cj β, by rw [← cj_Pi P, ← cj_mul, ← hβ, cj_cj]⟩
    obtain ⟨β, hβ⟩ := hβ
    obtain ⟨u, hu, he⟩ := factor_inert hpr hc k β (cancel β (by rw [← hβ]; exact h))
    refine ⟨u, hu, ?_⟩
    rw [hβ, he]; show _ = (Ring19.ring19 LibInt.intR).mul u ((Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) (Pi P) k) (Pi P)); lib_ring

def nPi : Int × Int := (-Int.ofNat (P + 1 + 1), 0)

theorem prim_nPi (hc : ∃ K, Int.ofNat (P + 1 + 1) = 19 * K + 2 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 3 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 8 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 10 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 12 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 13 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 14 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 15 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 18) : Prim19 (nPi P) := by
  obtain ⟨K, hK | hK | hK | hK | hK | hK | hK | hK | hK⟩ := hc
  · exact ⟨-K + -1, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by show -Int.ofNat (P + 1 + 1) + 10 * 0 = _; rw [hK]; lib_int)))))))))⟩
  · exact ⟨-K + -1, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by show -Int.ofNat (P + 1 + 1) + 10 * 0 = _; rw [hK]; lib_int)))))))))⟩
  · exact ⟨-K + -1, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by show -Int.ofNat (P + 1 + 1) + 10 * 0 = _; rw [hK]; lib_int))))))))⟩
  · exact ⟨-K + -1, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by show -Int.ofNat (P + 1 + 1) + 10 * 0 = _; rw [hK]; lib_int)))))))⟩
  · exact ⟨-K + -1, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by show -Int.ofNat (P + 1 + 1) + 10 * 0 = _; rw [hK]; lib_int))))))⟩
  · exact ⟨-K + -1, (Or.inr (Or.inr (Or.inr (Or.inl (by show -Int.ofNat (P + 1 + 1) + 10 * 0 = _; rw [hK]; lib_int)))))⟩
  · exact ⟨-K + -1, (Or.inr (Or.inr (Or.inl (by show -Int.ofNat (P + 1 + 1) + 10 * 0 = _; rw [hK]; lib_int))))⟩
  · exact ⟨-K + -1, (Or.inr (Or.inl (by show -Int.ofNat (P + 1 + 1) + 10 * 0 = _; rw [hK]; lib_int)))⟩
  · exact ⟨-K + -1, (Or.inl (by show -Int.ofNat (P + 1 + 1) + 10 * 0 = _; rw [hK]; lib_int))⟩

theorem pw_neg (k : Nat) : pw (Ring19.ring19 LibInt.intR) (Pi P) k =
    (Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) ((Ring19.ring19 LibInt.intR).neg (Ring19.ring19 LibInt.intR).one) k) (pw (Ring19.ring19 LibInt.intR) (nPi P) k) := by
  rw [← Frobenius.pw_mul]
  congr 1
  apply Prod.ext
  · show Int.ofNat (P + 1 + 1) = -1 * -Int.ofNat (P + 1 + 1) + -(-0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0); lib_int
  · show (0 : Int) = -1 * 0 + -0 * -Int.ofNat (P + 1 + 1) + -0 * 0; lib_int

theorem nrm_neg_one_pw : ∀ k, nrm (pw (Ring19.ring19 LibInt.intR) ((Ring19.ring19 LibInt.intR).neg (Ring19.ring19 LibInt.intR).one) k) = 1
  | 0 => rfl
  | k + 1 => by
    show nrm ((Ring19.ring19 LibInt.intR).mul _ _) = 1
    rw [nrm_mul, nrm_neg_one_pw k]; rfl

/-- **The primary elements of norm p^(2k) are (−p)^k.** -/
theorem prim_inert (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : ∃ K, Int.ofNat (P + 1 + 1) = 19 * K + 2 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 3 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 8 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 10 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 12 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 13 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 14 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 15 ∨ Int.ofNat (P +   -- (+47 chars; full line in the Zenodo source)
    (hα : nrm α = (P + 1 + 1) ^ (2 * k)) (pα : Prim19 α) : α = pw (Ring19.ring19 LibInt.intR) (nPi P) k := by
  obtain ⟨u, hu, he⟩ := factor_inert P hpr hc k α hα
  rw [pw_neg] at he
  generalize hv : (Ring19.ring19 LibInt.intR).mul u (pw (Ring19.ring19 LibInt.intR) ((Ring19.ring19 LibInt.intR).neg (Ring19.ring19 LibInt.intR).one) k) = v
  have he' : α = (Ring19.ring19 LibInt.intR).mul v (pw (Ring19.ring19 LibInt.intR) (nPi P) k) := by
    rw [he, ← hv]; lib_ring
  have hv1 : nrm v = 1 := by rw [← hv, nrm_mul, hu, nrm_neg_one_pw]
  have v1 := unit_one v _ (nrm_one_eq v hv1) (prim19_pow _ (prim_nPi P hc) k) (by rw [← he']; exact pα)
  rw [he', v1]; lib_ring

theorem nrm_nPi : nrm (nPi P) = (P + 1 + 1) * (P + 1 + 1) := by
  apply Int.ofNat.inj; rw [nrm_cast]
  show -Int.ofNat (P + 1 + 1) * -Int.ofNat (P + 1 + 1) + -Int.ofNat (P + 1 + 1) * 0 + 5 * (0 * 0) = Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1)
  lib_int

theorem nrm_nPi_pw (k : Nat) : nrm (pw (Ring19.ring19 LibInt.intR) (nPi P) k) = (P + 1 + 1) ^ (2 * k) := by
  rw [HeckePrime19.nrm_pw, nrm_nPi]
  induction k with
  | zero => rfl
  | succ k ih =>
    show ((P + 1 + 1) * (P + 1 + 1)) ^ k * ((P + 1 + 1) * (P + 1 + 1)) = _
    rw [ih, nat_pow2]; lib_nat

/-- **c_W(p^(2k)) = W((−p)^k)** at an inert prime. -/
theorem cw19_even (W : Int × Int → Int × Int) (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : ∃ K, Int.ofNat (P + 1 + 1) = 19 * K + 2 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 3 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 8 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 10 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 12 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 13 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 14 ∨ Int.ofNat (P + 1 + 1) = 19  -- (+60 chars; full line in the Zenodo source)
    cw19 W ((P + 1 + 1) ^ (2 * k)) = W (pw (Ring19.ring19 LibInt.intR) (nPi P) k) := by
  have hPD : PD19 ((P + 1 + 1) ^ (2 * k)) (pw (Ring19.ring19 LibInt.intR) (nPi P) k) :=
    ⟨by rw [← nrm_cast, nrm_nPi_pw], prim19_pow _ (prim_nPi P hc) k⟩
  unfold cw19
  rw [sumK_ext (Ring19.ring19 LibInt.intR) _ (fun z => (Ring19.ring19 LibInt.intR).mul (delta (Ring19.ring19 LibInt.intR) z (pw (Ring19.ring19 LibInt.intR) (nPi P) k)) (W z)) (fun z => by
    by_cases hz : PD19 ((P + 1 + 1) ^ (2 * k)) z
    · have e := prim_inert P hpr hc k z (Int.ofNat.inj (by rw [nrm_cast, hz.1])) hz.2
      rw [I19_pos _ _ hz]; unfold delta; rw [ite_pos' _ _ e]
    · rw [I19_neg _ _ hz]; unfold delta
      rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact hPD))])]
  have hb := abs_le19 _ _ hPD.1
  exact HeckeMult.pick_box (Ring19.ring19 LibInt.intR) W _ _ hb.1 hb.2

set_option maxHeartbeats 0 in
theorem sq19 (Y L : Int) (hL : Y = 19 * L + 2 ∨ Y = 19 * L + 3 ∨ Y = 19 * L + 8 ∨ Y = 19 * L + 10 ∨ Y = 19 * L + 12 ∨ Y = 19 * L + 13 ∨ Y = 19 * L + 14 ∨ Y = 19 * L + 15 ∨ Y = 19 * L + 18) : ∃ M : Int, Y * Y = 19 * M + 1 ∨ Y * Y = 19 * M + 4 ∨ Y * Y = 19 * M + 5 ∨ Y * Y = 19 * M + 6 ∨ Y * Y = 19 * M + 7 ∨ Y * Y = 19 * M + 9 ∨ Y * Y = 19 * M + 11 ∨ Y * Y = 19 * M + 16 ∨ Y * Y = 19 * M + 17 := by
  rcases hL with hL | hL | hL | hL | hL | hL | hL | hL | hL
  · exact ⟨19 * L * L + L * 2 + 2 * L + 0, (Or.inr (Or.inl (by rw [prod19 Y Y L L 2 2 hL hL]; generalize 19 * L * L + L * 2 + 2 * L = X; lib_int)))⟩
  · exact ⟨19 * L * L + L * 3 + 3 * L + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 Y Y L L 3 3 hL hL]; generalize 19 * L * L + L * 3 + 3 * L = X; lib_int)))))))⟩
  · exact ⟨19 * L * L + L * 8 + 8 * L + 3, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 Y Y L L 8 8 hL hL]; generalize 19 * L * L + L * 8 + 8 * L = X; lib_int))))))⟩
  · exact ⟨19 * L * L + L * 10 + 10 * L + 5, (Or.inr (Or.inr (Or.inl (by rw [prod19 Y Y L L 10 10 hL hL]; generalize 19 * L * L + L * 10 + 10 * L = X; lib_int))))⟩
  · exact ⟨19 * L * L + L * 12 + 12 * L + 7, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 Y Y L L 12 12 hL hL]; generalize 19 * L * L + L * 12 + 12 * L = X; lib_int))))))))⟩
  · exact ⟨19 * L * L + L * 13 + 13 * L + 8, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [prod19 Y Y L L 13 13 hL hL]; generalize 19 * L * L + L * 13 + 13 * L = X; lib_int)))))))))⟩
  · exact ⟨19 * L * L + L * 14 + 14 * L + 10, (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 Y Y L L 14 14 hL hL]; generalize 19 * L * L + L * 14 + 14 * L = X; lib_int)))))⟩
  · exact ⟨19 * L * L + L * 15 + 15 * L + 11, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 Y Y L L 15 15 hL hL]; generalize 19 * L * L + L * 15 + 15 * L = X; lib_int)))))))))⟩
  · exact ⟨19 * L * L + L * 18 + 18 * L + 17, (Or.inl (by rw [prod19 Y Y L L 18 18 hL hL]; generalize 19 * L * L + L * 18 + 18 * L = X; lib_int))⟩

set_option maxHeartbeats 0 in
theorem step19_0 (X K M Z : Int) (hK : X = 19 * K + 2) (hM : Z = 19 * M + 1 ∨ Z = 19 * M + 4 ∨ Z = 19 * M + 5 ∨ Z = 19 * M + 6 ∨ Z = 19 * M + 7 ∨ Z = 19 * M + 9 ∨ Z = 19 * M + 11 ∨ Z = 19 * M + 16 ∨ Z = 19 * M + 17) :
    ∃ W : Int, X * Z = 19 * W + 2 ∨ X * Z = 19 * W + 3 ∨ X * Z = 19 * W + 8 ∨ X * Z = 19 * W + 10 ∨ X * Z = 19 * W + 12 ∨ X * Z = 19 * W + 13 ∨ X * Z = 19 * W + 14 ∨ X * Z = 19 * W + 15 ∨ X * Z = 19 * W + 18 := by
  rcases hM with hM | hM | hM | hM | hM | hM | hM | hM | hM
  · exact ⟨19 * K * M + K * 1 + 2 * M + 0, (Or.inl (by rw [prod19 X Z K M 2 1 hK hM]; generalize 19 * K * M + K * 1 + 2 * M = Q; lib_int))⟩
  · exact ⟨19 * K * M + K * 4 + 2 * M + 0, (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 2 4 hK hM]; generalize 19 * K * M + K * 4 + 2 * M = Q; lib_int))))⟩
  · exact ⟨19 * K * M + K * 5 + 2 * M + 0, (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 2 5 hK hM]; generalize 19 * K * M + K * 5 + 2 * M = Q; lib_int)))))⟩
  · exact ⟨19 * K * M + K * 6 + 2 * M + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 2 6 hK hM]; generalize 19 * K * M + K * 6 + 2 * M = Q; lib_int))))))⟩
  · exact ⟨19 * K * M + K * 7 + 2 * M + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 2 7 hK hM]; generalize 19 * K * M + K * 7 + 2 * M = Q; lib_int))))))))⟩
  · exact ⟨19 * K * M + K * 9 + 2 * M + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [prod19 X Z K M 2 9 hK hM]; generalize 19 * K * M + K * 9 + 2 * M = Q; lib_int)))))))))⟩
  · exact ⟨19 * K * M + K * 11 + 2 * M + 1, (Or.inr (Or.inl (by rw [prod19 X Z K M 2 11 hK hM]; generalize 19 * K * M + K * 11 + 2 * M = Q; lib_int)))⟩
  · exact ⟨19 * K * M + K * 16 + 2 * M + 1, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 2 16 hK hM]; generalize 19 * K * M + K * 16 + 2 * M = Q; lib_int)))))))⟩
  · exact ⟨19 * K * M + K * 17 + 2 * M + 1, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 2 17 hK hM]; generalize 19 * K * M + K * 17 + 2 * M = Q; lib_int)))))))))⟩

set_option maxHeartbeats 0 in
theorem step19_1 (X K M Z : Int) (hK : X = 19 * K + 3) (hM : Z = 19 * M + 1 ∨ Z = 19 * M + 4 ∨ Z = 19 * M + 5 ∨ Z = 19 * M + 6 ∨ Z = 19 * M + 7 ∨ Z = 19 * M + 9 ∨ Z = 19 * M + 11 ∨ Z = 19 * M + 16 ∨ Z = 19 * M + 17) :
    ∃ W : Int, X * Z = 19 * W + 2 ∨ X * Z = 19 * W + 3 ∨ X * Z = 19 * W + 8 ∨ X * Z = 19 * W + 10 ∨ X * Z = 19 * W + 12 ∨ X * Z = 19 * W + 13 ∨ X * Z = 19 * W + 14 ∨ X * Z = 19 * W + 15 ∨ X * Z = 19 * W + 18 := by
  rcases hM with hM | hM | hM | hM | hM | hM | hM | hM | hM
  · exact ⟨19 * K * M + K * 1 + 3 * M + 0, (Or.inr (Or.inl (by rw [prod19 X Z K M 3 1 hK hM]; generalize 19 * K * M + K * 1 + 3 * M = Q; lib_int)))⟩
  · exact ⟨19 * K * M + K * 4 + 3 * M + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 3 4 hK hM]; generalize 19 * K * M + K * 4 + 3 * M = Q; lib_int))))))⟩
  · exact ⟨19 * K * M + K * 5 + 3 * M + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 3 5 hK hM]; generalize 19 * K * M + K * 5 + 3 * M = Q; lib_int)))))))))⟩
  · exact ⟨19 * K * M + K * 6 + 3 * M + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [prod19 X Z K M 3 6 hK hM]; generalize 19 * K * M + K * 6 + 3 * M = Q; lib_int)))))))))⟩
  · exact ⟨19 * K * M + K * 7 + 3 * M + 1, (Or.inl (by rw [prod19 X Z K M 3 7 hK hM]; generalize 19 * K * M + K * 7 + 3 * M = Q; lib_int))⟩
  · exact ⟨19 * K * M + K * 9 + 3 * M + 1, (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 3 9 hK hM]; generalize 19 * K * M + K * 9 + 3 * M = Q; lib_int))))⟩
  · exact ⟨19 * K * M + K * 11 + 3 * M + 1, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 3 11 hK hM]; generalize 19 * K * M + K * 11 + 3 * M = Q; lib_int))))))))⟩
  · exact ⟨19 * K * M + K * 16 + 3 * M + 2, (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 3 16 hK hM]; generalize 19 * K * M + K * 16 + 3 * M = Q; lib_int)))))⟩
  · exact ⟨19 * K * M + K * 17 + 3 * M + 2, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 3 17 hK hM]; generalize 19 * K * M + K * 17 + 3 * M = Q; lib_int)))))))⟩

set_option maxHeartbeats 0 in
theorem step19_2 (X K M Z : Int) (hK : X = 19 * K + 8) (hM : Z = 19 * M + 1 ∨ Z = 19 * M + 4 ∨ Z = 19 * M + 5 ∨ Z = 19 * M + 6 ∨ Z = 19 * M + 7 ∨ Z = 19 * M + 9 ∨ Z = 19 * M + 11 ∨ Z = 19 * M + 16 ∨ Z = 19 * M + 17) :
    ∃ W : Int, X * Z = 19 * W + 2 ∨ X * Z = 19 * W + 3 ∨ X * Z = 19 * W + 8 ∨ X * Z = 19 * W + 10 ∨ X * Z = 19 * W + 12 ∨ X * Z = 19 * W + 13 ∨ X * Z = 19 * W + 14 ∨ X * Z = 19 * W + 15 ∨ X * Z = 19 * W + 18 := by
  rcases hM with hM | hM | hM | hM | hM | hM | hM | hM | hM
  · exact ⟨19 * K * M + K * 1 + 8 * M + 0, (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 8 1 hK hM]; generalize 19 * K * M + K * 1 + 8 * M = Q; lib_int))))⟩
  · exact ⟨19 * K * M + K * 4 + 8 * M + 1, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 8 4 hK hM]; generalize 19 * K * M + K * 4 + 8 * M = Q; lib_int)))))))⟩
  · exact ⟨19 * K * M + K * 5 + 8 * M + 2, (Or.inl (by rw [prod19 X Z K M 8 5 hK hM]; generalize 19 * K * M + K * 5 + 8 * M = Q; lib_int))⟩
  · exact ⟨19 * K * M + K * 6 + 8 * M + 2, (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 8 6 hK hM]; generalize 19 * K * M + K * 6 + 8 * M = Q; lib_int)))))⟩
  · exact ⟨19 * K * M + K * 7 + 8 * M + 2, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [prod19 X Z K M 8 7 hK hM]; generalize 19 * K * M + K * 7 + 8 * M = Q; lib_int)))))))))⟩
  · exact ⟨19 * K * M + K * 9 + 8 * M + 3, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 8 9 hK hM]; generalize 19 * K * M + K * 9 + 8 * M = Q; lib_int)))))))))⟩
  · exact ⟨19 * K * M + K * 11 + 8 * M + 4, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 8 11 hK hM]; generalize 19 * K * M + K * 11 + 8 * M = Q; lib_int))))))⟩
  · exact ⟨19 * K * M + K * 16 + 8 * M + 6, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 8 16 hK hM]; generalize 19 * K * M + K * 16 + 8 * M = Q; lib_int))))))))⟩
  · exact ⟨19 * K * M + K * 17 + 8 * M + 7, (Or.inr (Or.inl (by rw [prod19 X Z K M 8 17 hK hM]; generalize 19 * K * M + K * 17 + 8 * M = Q; lib_int)))⟩

set_option maxHeartbeats 0 in
theorem step19_3 (X K M Z : Int) (hK : X = 19 * K + 10) (hM : Z = 19 * M + 1 ∨ Z = 19 * M + 4 ∨ Z = 19 * M + 5 ∨ Z = 19 * M + 6 ∨ Z = 19 * M + 7 ∨ Z = 19 * M + 9 ∨ Z = 19 * M + 11 ∨ Z = 19 * M + 16 ∨ Z = 19 * M + 17) :
    ∃ W : Int, X * Z = 19 * W + 2 ∨ X * Z = 19 * W + 3 ∨ X * Z = 19 * W + 8 ∨ X * Z = 19 * W + 10 ∨ X * Z = 19 * W + 12 ∨ X * Z = 19 * W + 13 ∨ X * Z = 19 * W + 14 ∨ X * Z = 19 * W + 15 ∨ X * Z = 19 * W + 18 := by
  rcases hM with hM | hM | hM | hM | hM | hM | hM | hM | hM
  · exact ⟨19 * K * M + K * 1 + 10 * M + 0, (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 10 1 hK hM]; generalize 19 * K * M + K * 1 + 10 * M = Q; lib_int)))))⟩
  · exact ⟨19 * K * M + K * 4 + 10 * M + 2, (Or.inl (by rw [prod19 X Z K M 10 4 hK hM]; generalize 19 * K * M + K * 4 + 10 * M = Q; lib_int))⟩
  · exact ⟨19 * K * M + K * 5 + 10 * M + 2, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 10 5 hK hM]; generalize 19 * K * M + K * 5 + 10 * M = Q; lib_int))))))⟩
  · exact ⟨19 * K * M + K * 6 + 10 * M + 3, (Or.inr (Or.inl (by rw [prod19 X Z K M 10 6 hK hM]; generalize 19 * K * M + K * 6 + 10 * M = Q; lib_int)))⟩
  · exact ⟨19 * K * M + K * 7 + 10 * M + 3, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 10 7 hK hM]; generalize 19 * K * M + K * 7 + 10 * M = Q; lib_int)))))))⟩
  · exact ⟨19 * K * M + K * 9 + 10 * M + 4, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 10 9 hK hM]; generalize 19 * K * M + K * 9 + 10 * M = Q; lib_int))))))))⟩
  · exact ⟨19 * K * M + K * 11 + 10 * M + 5, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 10 11 hK hM]; generalize 19 * K * M + K * 11 + 10 * M = Q; lib_int)))))))))⟩
  · exact ⟨19 * K * M + K * 16 + 10 * M + 8, (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 10 16 hK hM]; generalize 19 * K * M + K * 16 + 10 * M = Q; lib_int))))⟩
  · exact ⟨19 * K * M + K * 17 + 10 * M + 8, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [prod19 X Z K M 10 17 hK hM]; generalize 19 * K * M + K * 17 + 10 * M = Q; lib_int)))))))))⟩

set_option maxHeartbeats 0 in
theorem step19_4 (X K M Z : Int) (hK : X = 19 * K + 12) (hM : Z = 19 * M + 1 ∨ Z = 19 * M + 4 ∨ Z = 19 * M + 5 ∨ Z = 19 * M + 6 ∨ Z = 19 * M + 7 ∨ Z = 19 * M + 9 ∨ Z = 19 * M + 11 ∨ Z = 19 * M + 16 ∨ Z = 19 * M + 17) :
    ∃ W : Int, X * Z = 19 * W + 2 ∨ X * Z = 19 * W + 3 ∨ X * Z = 19 * W + 8 ∨ X * Z = 19 * W + 10 ∨ X * Z = 19 * W + 12 ∨ X * Z = 19 * W + 13 ∨ X * Z = 19 * W + 14 ∨ X * Z = 19 * W + 15 ∨ X * Z = 19 * W + 18 := by
  rcases hM with hM | hM | hM | hM | hM | hM | hM | hM | hM
  · exact ⟨19 * K * M + K * 1 + 12 * M + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 12 1 hK hM]; generalize 19 * K * M + K * 1 + 12 * M = Q; lib_int))))))⟩
  · exact ⟨19 * K * M + K * 4 + 12 * M + 2, (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 12 4 hK hM]; generalize 19 * K * M + K * 4 + 12 * M = Q; lib_int)))))⟩
  · exact ⟨19 * K * M + K * 5 + 12 * M + 3, (Or.inr (Or.inl (by rw [prod19 X Z K M 12 5 hK hM]; generalize 19 * K * M + K * 5 + 12 * M = Q; lib_int)))⟩
  · exact ⟨19 * K * M + K * 6 + 12 * M + 3, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 12 6 hK hM]; generalize 19 * K * M + K * 6 + 12 * M = Q; lib_int)))))))))⟩
  · exact ⟨19 * K * M + K * 7 + 12 * M + 4, (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 12 7 hK hM]; generalize 19 * K * M + K * 7 + 12 * M = Q; lib_int))))⟩
  · exact ⟨19 * K * M + K * 9 + 12 * M + 5, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 12 9 hK hM]; generalize 19 * K * M + K * 9 + 12 * M = Q; lib_int)))))))⟩
  · exact ⟨19 * K * M + K * 11 + 12 * M + 6, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [prod19 X Z K M 12 11 hK hM]; generalize 19 * K * M + K * 11 + 12 * M = Q; lib_int)))))))))⟩
  · exact ⟨19 * K * M + K * 16 + 12 * M + 10, (Or.inl (by rw [prod19 X Z K M 12 16 hK hM]; generalize 19 * K * M + K * 16 + 12 * M = Q; lib_int))⟩
  · exact ⟨19 * K * M + K * 17 + 12 * M + 10, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 12 17 hK hM]; generalize 19 * K * M + K * 17 + 12 * M = Q; lib_int))))))))⟩

set_option maxHeartbeats 0 in
theorem step19_5 (X K M Z : Int) (hK : X = 19 * K + 13) (hM : Z = 19 * M + 1 ∨ Z = 19 * M + 4 ∨ Z = 19 * M + 5 ∨ Z = 19 * M + 6 ∨ Z = 19 * M + 7 ∨ Z = 19 * M + 9 ∨ Z = 19 * M + 11 ∨ Z = 19 * M + 16 ∨ Z = 19 * M + 17) :
    ∃ W : Int, X * Z = 19 * W + 2 ∨ X * Z = 19 * W + 3 ∨ X * Z = 19 * W + 8 ∨ X * Z = 19 * W + 10 ∨ X * Z = 19 * W + 12 ∨ X * Z = 19 * W + 13 ∨ X * Z = 19 * W + 14 ∨ X * Z = 19 * W + 15 ∨ X * Z = 19 * W + 18 := by
  rcases hM with hM | hM | hM | hM | hM | hM | hM | hM | hM
  · exact ⟨19 * K * M + K * 1 + 13 * M + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 13 1 hK hM]; generalize 19 * K * M + K * 1 + 13 * M = Q; lib_int)))))))⟩
  · exact ⟨19 * K * M + K * 4 + 13 * M + 2, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 13 4 hK hM]; generalize 19 * K * M + K * 4 + 13 * M = Q; lib_int))))))))⟩
  · exact ⟨19 * K * M + K * 5 + 13 * M + 3, (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 13 5 hK hM]; generalize 19 * K * M + K * 5 + 13 * M = Q; lib_int))))⟩
  · exact ⟨19 * K * M + K * 6 + 13 * M + 4, (Or.inl (by rw [prod19 X Z K M 13 6 hK hM]; generalize 19 * K * M + K * 6 + 13 * M = Q; lib_int))⟩
  · exact ⟨19 * K * M + K * 7 + 13 * M + 4, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 13 7 hK hM]; generalize 19 * K * M + K * 7 + 13 * M = Q; lib_int)))))))))⟩
  · exact ⟨19 * K * M + K * 9 + 13 * M + 6, (Or.inr (Or.inl (by rw [prod19 X Z K M 13 9 hK hM]; generalize 19 * K * M + K * 9 + 13 * M = Q; lib_int)))⟩
  · exact ⟨19 * K * M + K * 11 + 13 * M + 7, (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 13 11 hK hM]; generalize 19 * K * M + K * 11 + 13 * M = Q; lib_int)))))⟩
  · exact ⟨19 * K * M + K * 16 + 13 * M + 10, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [prod19 X Z K M 13 16 hK hM]; generalize 19 * K * M + K * 16 + 13 * M = Q; lib_int)))))))))⟩
  · exact ⟨19 * K * M + K * 17 + 13 * M + 11, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 13 17 hK hM]; generalize 19 * K * M + K * 17 + 13 * M = Q; lib_int))))))⟩

set_option maxHeartbeats 0 in
theorem step19_6 (X K M Z : Int) (hK : X = 19 * K + 14) (hM : Z = 19 * M + 1 ∨ Z = 19 * M + 4 ∨ Z = 19 * M + 5 ∨ Z = 19 * M + 6 ∨ Z = 19 * M + 7 ∨ Z = 19 * M + 9 ∨ Z = 19 * M + 11 ∨ Z = 19 * M + 16 ∨ Z = 19 * M + 17) :
    ∃ W : Int, X * Z = 19 * W + 2 ∨ X * Z = 19 * W + 3 ∨ X * Z = 19 * W + 8 ∨ X * Z = 19 * W + 10 ∨ X * Z = 19 * W + 12 ∨ X * Z = 19 * W + 13 ∨ X * Z = 19 * W + 14 ∨ X * Z = 19 * W + 15 ∨ X * Z = 19 * W + 18 := by
  rcases hM with hM | hM | hM | hM | hM | hM | hM | hM | hM
  · exact ⟨19 * K * M + K * 1 + 14 * M + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 14 1 hK hM]; generalize 19 * K * M + K * 1 + 14 * M = Q; lib_int))))))))⟩
  · exact ⟨19 * K * M + K * 4 + 14 * M + 2, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [prod19 X Z K M 14 4 hK hM]; generalize 19 * K * M + K * 4 + 14 * M = Q; lib_int)))))))))⟩
  · exact ⟨19 * K * M + K * 5 + 14 * M + 3, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 14 5 hK hM]; generalize 19 * K * M + K * 5 + 14 * M = Q; lib_int)))))))⟩
  · exact ⟨19 * K * M + K * 6 + 14 * M + 4, (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 14 6 hK hM]; generalize 19 * K * M + K * 6 + 14 * M = Q; lib_int))))⟩
  · exact ⟨19 * K * M + K * 7 + 14 * M + 5, (Or.inr (Or.inl (by rw [prod19 X Z K M 14 7 hK hM]; generalize 19 * K * M + K * 7 + 14 * M = Q; lib_int)))⟩
  · exact ⟨19 * K * M + K * 9 + 14 * M + 6, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 14 9 hK hM]; generalize 19 * K * M + K * 9 + 14 * M = Q; lib_int))))))⟩
  · exact ⟨19 * K * M + K * 11 + 14 * M + 8, (Or.inl (by rw [prod19 X Z K M 14 11 hK hM]; generalize 19 * K * M + K * 11 + 14 * M = Q; lib_int))⟩
  · exact ⟨19 * K * M + K * 16 + 14 * M + 11, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 14 16 hK hM]; generalize 19 * K * M + K * 16 + 14 * M = Q; lib_int)))))))))⟩
  · exact ⟨19 * K * M + K * 17 + 14 * M + 12, (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 14 17 hK hM]; generalize 19 * K * M + K * 17 + 14 * M = Q; lib_int)))))⟩

set_option maxHeartbeats 0 in
theorem step19_7 (X K M Z : Int) (hK : X = 19 * K + 15) (hM : Z = 19 * M + 1 ∨ Z = 19 * M + 4 ∨ Z = 19 * M + 5 ∨ Z = 19 * M + 6 ∨ Z = 19 * M + 7 ∨ Z = 19 * M + 9 ∨ Z = 19 * M + 11 ∨ Z = 19 * M + 16 ∨ Z = 19 * M + 17) :
    ∃ W : Int, X * Z = 19 * W + 2 ∨ X * Z = 19 * W + 3 ∨ X * Z = 19 * W + 8 ∨ X * Z = 19 * W + 10 ∨ X * Z = 19 * W + 12 ∨ X * Z = 19 * W + 13 ∨ X * Z = 19 * W + 14 ∨ X * Z = 19 * W + 15 ∨ X * Z = 19 * W + 18 := by
  rcases hM with hM | hM | hM | hM | hM | hM | hM | hM | hM
  · exact ⟨19 * K * M + K * 1 + 15 * M + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 15 1 hK hM]; generalize 19 * K * M + K * 1 + 15 * M = Q; lib_int)))))))))⟩
  · exact ⟨19 * K * M + K * 4 + 15 * M + 3, (Or.inr (Or.inl (by rw [prod19 X Z K M 15 4 hK hM]; generalize 19 * K * M + K * 4 + 15 * M = Q; lib_int)))⟩
  · exact ⟨19 * K * M + K * 5 + 15 * M + 3, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [prod19 X Z K M 15 5 hK hM]; generalize 19 * K * M + K * 5 + 15 * M = Q; lib_int)))))))))⟩
  · exact ⟨19 * K * M + K * 6 + 15 * M + 4, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 15 6 hK hM]; generalize 19 * K * M + K * 6 + 15 * M = Q; lib_int))))))))⟩
  · exact ⟨19 * K * M + K * 7 + 15 * M + 5, (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 15 7 hK hM]; generalize 19 * K * M + K * 7 + 15 * M = Q; lib_int)))))⟩
  · exact ⟨19 * K * M + K * 9 + 15 * M + 7, (Or.inl (by rw [prod19 X Z K M 15 9 hK hM]; generalize 19 * K * M + K * 9 + 15 * M = Q; lib_int))⟩
  · exact ⟨19 * K * M + K * 11 + 15 * M + 8, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 15 11 hK hM]; generalize 19 * K * M + K * 11 + 15 * M = Q; lib_int)))))))⟩
  · exact ⟨19 * K * M + K * 16 + 15 * M + 12, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 15 16 hK hM]; generalize 19 * K * M + K * 16 + 15 * M = Q; lib_int))))))⟩
  · exact ⟨19 * K * M + K * 17 + 15 * M + 13, (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 15 17 hK hM]; generalize 19 * K * M + K * 17 + 15 * M = Q; lib_int))))⟩

set_option maxHeartbeats 0 in
theorem step19_8 (X K M Z : Int) (hK : X = 19 * K + 18) (hM : Z = 19 * M + 1 ∨ Z = 19 * M + 4 ∨ Z = 19 * M + 5 ∨ Z = 19 * M + 6 ∨ Z = 19 * M + 7 ∨ Z = 19 * M + 9 ∨ Z = 19 * M + 11 ∨ Z = 19 * M + 16 ∨ Z = 19 * M + 17) :
    ∃ W : Int, X * Z = 19 * W + 2 ∨ X * Z = 19 * W + 3 ∨ X * Z = 19 * W + 8 ∨ X * Z = 19 * W + 10 ∨ X * Z = 19 * W + 12 ∨ X * Z = 19 * W + 13 ∨ X * Z = 19 * W + 14 ∨ X * Z = 19 * W + 15 ∨ X * Z = 19 * W + 18 := by
  rcases hM with hM | hM | hM | hM | hM | hM | hM | hM | hM
  · exact ⟨19 * K * M + K * 1 + 18 * M + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [prod19 X Z K M 18 1 hK hM]; generalize 19 * K * M + K * 1 + 18 * M = Q; lib_int)))))))))⟩
  · exact ⟨19 * K * M + K * 4 + 18 * M + 3, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 18 4 hK hM]; generalize 19 * K * M + K * 4 + 18 * M = Q; lib_int)))))))))⟩
  · exact ⟨19 * K * M + K * 5 + 18 * M + 4, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 18 5 hK hM]; generalize 19 * K * M + K * 5 + 18 * M = Q; lib_int))))))))⟩
  · exact ⟨19 * K * M + K * 6 + 18 * M + 5, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 18 6 hK hM]; generalize 19 * K * M + K * 6 + 18 * M = Q; lib_int)))))))⟩
  · exact ⟨19 * K * M + K * 7 + 18 * M + 6, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 18 7 hK hM]; generalize 19 * K * M + K * 7 + 18 * M = Q; lib_int))))))⟩
  · exact ⟨19 * K * M + K * 9 + 18 * M + 8, (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 18 9 hK hM]; generalize 19 * K * M + K * 9 + 18 * M = Q; lib_int)))))⟩
  · exact ⟨19 * K * M + K * 11 + 18 * M + 10, (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 18 11 hK hM]; generalize 19 * K * M + K * 11 + 18 * M = Q; lib_int))))⟩
  · exact ⟨19 * K * M + K * 16 + 18 * M + 15, (Or.inr (Or.inl (by rw [prod19 X Z K M 18 16 hK hM]; generalize 19 * K * M + K * 16 + 18 * M = Q; lib_int)))⟩
  · exact ⟨19 * K * M + K * 17 + 18 * M + 16, (Or.inl (by rw [prod19 X Z K M 18 17 hK hM]; generalize 19 * K * M + K * 17 + 18 * M = Q; lib_int))⟩

theorem step19 (X Y K L : Int) (hK : X = 19 * K + 2 ∨ X = 19 * K + 3 ∨ X = 19 * K + 8 ∨ X = 19 * K + 10 ∨ X = 19 * K + 12 ∨ X = 19 * K + 13 ∨ X = 19 * K + 14 ∨ X = 19 * K + 15 ∨ X = 19 * K + 18) (hL : Y = 19 * L + 2 ∨ Y = 19 * L + 3 ∨ Y = 19 * L + 8 ∨ Y = 19 * L + 10 ∨ Y = 19 * L + 12 ∨ Y = 19 * L + 13 ∨ Y = 19 * L + 14 ∨ Y = 19 * L + 15 ∨ Y = 19 * L + 18) :
    ∃ W : Int, X * Y * Y = 19 * W + 2 ∨ X * Y * Y = 19 * W + 3 ∨ X * Y * Y = 19 * W + 8 ∨ X * Y * Y = 19 * W + 10 ∨ X * Y * Y = 19 * W + 12 ∨ X * Y * Y = 19 * W + 13 ∨ X * Y * Y = 19 * W + 14 ∨ X * Y * Y = 19 * W + 15 ∨ X * Y * Y = 19 * W + 18 := by
  obtain ⟨M, hM⟩ := sq19 Y L hL
  have e : X * Y * Y = X * (Y * Y) := by lib_int
  rw [e]
  rcases hK with hK | hK | hK | hK | hK | hK | hK | hK | hK
  · exact step19_0 _ K M _ hK hM
  · exact step19_1 _ K M _ hK hM
  · exact step19_2 _ K M _ hK hM
  · exact step19_3 _ K M _ hK hM
  · exact step19_4 _ K M _ hK hM
  · exact step19_5 _ K M _ hK hM
  · exact step19_6 _ K M _ hK hM
  · exact step19_7 _ K M _ hK hM
  · exact step19_8 _ K M _ hK hM

theorem pow_odd (hc : ∃ K, Int.ofNat (P + 1 + 1) = 19 * K + 2 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 3 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 8 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 10 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 12 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 13 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 14 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 15 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 18) (k : Nat) : ∃ K : Int, Int.ofNat ((  -- (+474 chars; full line in the Zenodo source)
  obtain ⟨L, hL⟩ := hc
  induction k with
  | zero => exact ⟨L, by rw [show (P + 1 + 1) ^ (2 * 0 + 1) = P + 1 + 1 from Nat.one_mul _]; exact hL⟩
  | succ k ih =>
    obtain ⟨K, hK⟩ := ih
    have e : Int.ofNat ((P + 1 + 1) ^ (2 * (k + 1) + 1)) = Int.ofNat ((P + 1 + 1) ^ (2 * k + 1)) * Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1) := by
      rw [show 2 * (k + 1) + 1 = 2 * k + 1 + 1 + 1 by lib_nat]; rfl
    rw [e]
    exact step19 _ _ K L hK hL

/-- **c_W(p^(2k+1)) = 0** at an inert prime. -/
theorem cw19_odd (W : Int × Int → Int × Int) (hc : ∃ K, Int.ofNat (P + 1 + 1) = 19 * K + 2 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 3 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 8 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 10 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 12 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 13 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 14 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 15 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 18) (k : N  -- (+81 chars; full line in the Zenodo source)
  obtain ⟨K, hK⟩ := pow_odd P hc k
  exact cw19_zero W _ K hK

end Inert19

#print axioms Inert19.nsq_0
#print axioms Inert19.nsq_1
#print axioms Inert19.nsq_2
#print axioms Inert19.nsq_3
#print axioms Inert19.nsq_4
#print axioms Inert19.nsq_5
#print axioms Inert19.nsq_6
#print axioms Inert19.nsq_7
#print axioms Inert19.nsq_8
#print axioms Inert19.nsq_9
#print axioms Inert19.en_mod19
#print axioms Inert19.cw19_zero
#print axioms Inert19.en_Pi
#print axioms Inert19.nrm_Pi
#print axioms Inert19.cj_Pi
#print axioms Inert19.not_p
#print axioms Inert19.dvd_conv
#print axioms Inert19.split_sq
#print axioms Inert19.inert_div
#print axioms Inert19.nat_pow2
#print axioms Inert19.factor_inert
#print axioms Inert19.prim_nPi
#print axioms Inert19.pw_neg
#print axioms Inert19.nrm_neg_one_pw
#print axioms Inert19.prim_inert
#print axioms Inert19.nrm_nPi
#print axioms Inert19.nrm_nPi_pw
#print axioms Inert19.cw19_even
#print axioms Inert19.prodD
#print axioms Inert19.prod19
#print axioms Inert19.sq19
#print axioms Inert19.step19_0
#print axioms Inert19.step19_1
#print axioms Inert19.step19_2
#print axioms Inert19.step19_3
#print axioms Inert19.step19_4
#print axioms Inert19.step19_5
#print axioms Inert19.step19_6
#print axioms Inert19.step19_7
#print axioms Inert19.step19_8
#print axioms Inert19.step19
#print axioms Inert19.pow_odd
#print axioms Inert19.cw19_odd

lang/Inert43.lean

lean_all/lang/Inert43.lean

/-
  lang/Inert43 — T48/T50: **the ℚ(√−43) Hecke character at inert primes** (χ₄₃(p) = −1), via the Legendre symbol:
    cast_en, en_mod43   N(z) ≡ ρ(z)² (mod 43) (44N = ρ² + 43a²), so χ₄₃(N(z)) ≠ −1
    cw43_zero, inert_div, factor_inert, prim_inert, cw43_even, pow_odd, cw43_odd   (port of Inert19)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.HeckePrime43

namespace Inert43

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius Euc43 Prim43 Uniq43 Prime43 HeckeMult43

variable (P : Nat)

theorem cast_en (z : Int × Int) : LibRing.castZ (ZMod.ring (2 * 20 + 1 + 1)) (en z) = (ZMod.ring (2 * 20 + 1 + 1)).mul (Prim43.cr z) (Prim43.cr z) := by
  have e44 : 44 * en z = Prim43.rho z * Prim43.rho z + 43 * (z.1 * z.1) := by
    show 44 * (z.1 * z.1 + z.1 * z.2 + 11 * (z.2 * z.2)) = (z.1 + 22 * z.2) * (z.1 + 22 * z.2) + 43 * (z.1 * z.1); lib_int
  have h1 : LibRing.castZ (ZMod.ring (2 * 20 + 1 + 1)) (44 * en z) = LibRing.castZ (ZMod.ring (2 * 20 + 1 + 1)) (en z) := by
    rw [HasseDavenport.castZ_mul, show LibRing.castZ (ZMod.ring (2 * 20 + 1 + 1)) 44 = (ZMod.ring (2 * 20 + 1 + 1)).one from rfl]; lib_ring
  rw [← h1, e44, IntCast.castZ_add, HasseDavenport.castZ_mul, Prim43.cz43]; unfold Prim43.cr; lib_ring

/-- N(z) is never a non-residue mod 43. -/
theorem en_mod43 (z : Int × Int) (n : Nat) (hn : QChar.chi 20 (LibRing.castZ (ZMod.ring (2 * 20 + 1 + 1)) (Int.ofNat n)) = -1) (h : en z = Int.ofNat n) : False := by
  rw [← h, cast_en] at hn
  by_cases h0 : Prim43.cr z = (ZMod.ring (2 * 20 + 1 + 1)).zero
  · rw [h0, show (ZMod.ring (2 * 20 + 1 + 1)).mul (ZMod.ring (2 * 20 + 1 + 1)).zero (ZMod.ring (2 * 20 + 1 + 1)).zero = (ZMod.ring (2 * 20 + 1 + 1)).zero by lib_ring, QChar.chi_zero] at hn; exact Int.noConfusion hn
  · rw [QChar.chi_sq 20 Prim43.prime43 _ h0] at hn; exact Int.noConfusion hn

/-- **c_W(n) = 0 for n a non-residue mod 43.** -/
theorem cw43_zero (W : Int × Int → Int × Int) (n : Nat) (hn : QChar.chi 20 (LibRing.castZ (ZMod.ring (2 * 20 + 1 + 1)) (Int.ofNat n)) = -1) : cw43 W n = (Ring43.ring43 LibInt.intR).zero := by
  unfold cw43
  rw [sumK_ext (Ring43.ring43 LibInt.intR) _ (fun _ => (Ring43.ring43 LibInt.intR).zero) (fun z => by
    rw [I43_neg _ _ (fun h => en_mod43 z n hn h.1)]; lib_ring)]
  exact zsum43 _

def Pi : Int × Int := (Int.ofNat (P + 1 + 1), 0)

theorem en_Pi : en (Pi P) = Int.ofNat ((P + 1 + 1) * (P + 1 + 1)) := by
  show Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1) + Int.ofNat (P + 1 + 1) * 0 + 11 * (0 * 0) = Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1)
  lib_int

theorem nrm_Pi : nrm (Pi P) = (P + 1 + 1) * (P + 1 + 1) := Int.ofNat.inj (by rw [nrm_cast, en_Pi])

theorem cj_Pi : cj (Pi P) = Pi P := Prod.ext (by show Int.ofNat (P + 1 + 1) + 0 = Int.ofNat (P + 1 + 1); lib_int) (by show -(0 : Int) = 0; rfl)

theorem not_p (hc : QChar.chi 20 (LibRing.castZ (ZMod.ring (2 * 20 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) (z : Int × Int) (h : nrm z = P + 1 + 1) : False :=
  en_mod43 z (P + 1 + 1) hc (by rw [← nrm_cast, h])

theorem dvd_conv (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) : ∀ d, d ∣ (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1 :=
  fun d ⟨c, hc⟩ => hpr d ⟨c, by rw [hc]; lib_nat⟩

/-- the two factor case: a b = p², p ∤ … ⇒ a = 1 or b = 1 (a = p is excluded separately). -/
theorem split_sq (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (a b : Nat) (h : a * b = (P + 1 + 1) * (P + 1 + 1)) :
    a = 1 ∨ b = 1 ∨ a = P + 1 + 1 := by
  cases euclid_prime (dvd_conv P hpr) (show (P + 1 + 1) ∣ a * b from ⟨P + 1 + 1, h⟩) with
  | inl ha =>
    obtain ⟨s, hs⟩ := ha
    have e : s * b = P + 1 + 1 := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (P + 1)) (by rw [show (P + 1 + 1) * (s * b) = (P + 1 + 1) * s * b by lib_nat, ← hs, h])
    cases hpr s ⟨b, by rw [← e]; lib_nat⟩ with
    | inl s1 => exact Or.inr (Or.inr (by rw [hs, s1]; lib_nat))
    | inr sp =>
      refine Or.inr (Or.inl ?_)
      rw [sp] at e
      exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (P + 1)) (e.trans (Nat.mul_one _).symm)
  | inr hb =>
    obtain ⟨s, hs⟩ := hb
    have e : a * s = P + 1 + 1 := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (P + 1)) (by
      rw [show (P + 1 + 1) * (a * s) = a * ((P + 1 + 1) * s) by lib_nat, ← hs, h])
    cases hpr a ⟨s, by rw [← e]; lib_nat⟩ with
    | inl a1 => exact Or.inl a1
    | inr ap => exact Or.inr (Or.inr ap)

/-- **p ≡ 2 (mod 3) stays prime in ℤ[ω].** -/
theorem inert_div (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : QChar.chi 20 (LibRing.castZ (ZMod.ring (2 * 20 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) (x y : Int × Int)
    (h : Dvd' (Pi P) ((Ring43.ring43 LibInt.intR).mul x y)) : Dvd' (Pi P) x ∨ Dvd' (Pi P) y := by
  match egcd_exists (Pi P) x with
  | ⟨d, X, Y, hd, ⟨c1, hc1⟩, ⟨c2, hc2⟩⟩ =>
    have hdc : nrm d * nrm c1 = (P + 1 + 1) * (P + 1 + 1) := by rw [← nrm_mul, ← hc1, nrm_Pi]
    rcases split_sq P hpr _ _ hdc with h1 | h1 | h1
    · refine Or.inr (eeuclid (Pi P) x y ((Ring43.ring43 LibInt.intR).mul (cj d) X) ((Ring43.ring43 LibInt.intR).mul (cj d) Y) ?_ h)
      rw [show (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul (cj d) X) (Pi P)) ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul (cj d) Y) x)
          = (Ring43.ring43 LibInt.intR).mul (cj d) ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul X (Pi P)) ((Ring43.ring43 LibInt.intR).mul Y x)) by lib_ring,
        ← hd, (Ring43.ring43 LibInt.intR).mul_comm, unit_inv d h1]
    · refine Or.inl ⟨(Ring43.ring43 LibInt.intR).mul (cj c1) c2, ?_⟩
      have hd' : d = (Ring43.ring43 LibInt.intR).mul (Pi P) (cj c1) := by
        rw [hc1]
        calc d = (Ring43.ring43 LibInt.intR).mul d (Ring43.ring43 LibInt.intR).one := by lib_ring
          _ = _ := by rw [← unit_inv c1 h1]; lib_ring
      rw [hc2, hd']; lib_ring
    · exact (not_p P hc d h1).elim

theorem nat_pow2 (k : Nat) : (P + 1 + 1) ^ (2 * (k + 1)) = (P + 1 + 1) * (P + 1 + 1) * (P + 1 + 1) ^ (2 * k) := by
  rw [show 2 * (k + 1) = 2 * k + 1 + 1 by lib_nat]
  show (P + 1 + 1) ^ (2 * k) * (P + 1 + 1) * (P + 1 + 1) = _
  lib_nat

/-- **Factorisation above an inert prime.** -/
theorem factor_inert (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : QChar.chi 20 (LibRing.castZ (ZMod.ring (2 * 20 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) : ∀ k (α : Int × Int),
    nrm α = (P + 1 + 1) ^ (2 * k) → ∃ u, nrm u = 1 ∧ α = (Ring43.ring43 LibInt.intR).mul u (pw (Ring43.ring43 LibInt.intR) (Pi P) k)
  | 0, α, h => ⟨α, h, by show α = (Ring43.ring43 LibInt.intR).mul α (Ring43.ring43 LibInt.intR).one; lib_ring⟩
  | k + 1, α, h => by
    have hdiv : Dvd' (Pi P) ((Ring43.ring43 LibInt.intR).mul α (cj α)) := by
      refine ⟨(Int.ofNat ((P + 1 + 1) * (P + 1 + 1) ^ (2 * k)), 0), ?_⟩
      rw [mul_cj, ← nrm_cast, h, nat_pow2]
      apply Prod.ext
      · show Int.ofNat ((P + 1 + 1) * (P + 1 + 1) * (P + 1 + 1) ^ (2 * k)) = Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) * (P + 1 + 1) ^ (2 * k)) + -(0 * 0)
        show Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ (2 * k)) = _
        show _ = Int.ofNat (P + 1 + 1) * (Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ (2 * k))) + -(0 * 0)
        lib_int
      · show (0 : Int) = Int.ofNat (P + 1 + 1) * 0 + 0 * Int.ofNat ((P + 1 + 1) * (P + 1 + 1) ^ (2 * k)) + -(0 * 0); lib_int
    have cancel : ∀ β : Int × Int, nrm ((Ring43.ring43 LibInt.intR).mul (Pi P) β) = (P + 1 + 1) ^ (2 * (k + 1)) → nrm β = (P + 1 + 1) ^ (2 * k) := fun β hb => by
      rw [nrm_mul, nrm_Pi, nat_pow2] at hb
      exact Nat.eq_of_mul_eq_mul_left (Nat.mul_pos (Nat.zero_lt_succ _) (Nat.zero_lt_succ _)) hb
    have hβ : ∃ β, α = (Ring43.ring43 LibInt.intR).mul (Pi P) β := by
      cases inert_div P hpr hc α (cj α) hdiv with
      | inl hα => exact hα
      | inr hα =>
        obtain ⟨β, hβ⟩ := hα
        exact ⟨cj β, by rw [← cj_Pi P, ← cj_mul, ← hβ, cj_cj]⟩
    obtain ⟨β, hβ⟩ := hβ
    obtain ⟨u, hu, he⟩ := factor_inert hpr hc k β (cancel β (by rw [← hβ]; exact h))
    refine ⟨u, hu, ?_⟩
    rw [hβ, he]; show _ = (Ring43.ring43 LibInt.intR).mul u ((Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) (Pi P) k) (Pi P)); lib_ring

def nPi : Int × Int := (-Int.ofNat (P + 1 + 1), 0)

theorem prim_nPi (hc : QChar.chi 20 (LibRing.castZ (ZMod.ring (2 * 20 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) : Prim43 (nPi P) := by
  show QChar.chi 20 (Prim43.cr (nPi P)) = 1
  have e : Prim43.cr (nPi P) = (ZMod.ring (2 * 20 + 1 + 1)).neg (LibRing.castZ (ZMod.ring (2 * 20 + 1 + 1)) (Int.ofNat (P + 1 + 1))) := by
    unfold Prim43.cr; rw [show Prim43.rho (nPi P) = -Int.ofNat (P + 1 + 1) by show -Int.ofNat (P + 1 + 1) + 22 * 0 = _; lib_int, HasseDavenport.castZ_neg]
  rw [e, Prim43.chi_neg, hc]; rfl

theorem pw_neg (k : Nat) : pw (Ring43.ring43 LibInt.intR) (Pi P) k =
    (Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) ((Ring43.ring43 LibInt.intR).neg (Ring43.ring43 LibInt.intR).one) k) (pw (Ring43.ring43 LibInt.intR) (nPi P) k) := by
  rw [← Frobenius.pw_mul]
  congr 1
  apply Prod.ext
  · show Int.ofNat (P + 1 + 1) = -1 * -Int.ofNat (P + 1 + 1) + -(-0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0); lib_int
  · show (0 : Int) = -1 * 0 + -0 * -Int.ofNat (P + 1 + 1) + -0 * 0; lib_int

theorem nrm_neg_one_pw : ∀ k, nrm (pw (Ring43.ring43 LibInt.intR) ((Ring43.ring43 LibInt.intR).neg (Ring43.ring43 LibInt.intR).one) k) = 1
  | 0 => rfl
  | k + 1 => by
    show nrm ((Ring43.ring43 LibInt.intR).mul _ _) = 1
    rw [nrm_mul, nrm_neg_one_pw k]; rfl

/-- **The primary elements of norm p^(2k) are (−p)^k.** -/
theorem prim_inert (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : QChar.chi 20 (LibRing.castZ (ZMod.ring (2 * 20 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) (k : Nat) (α : Int × Int)
    (hα : nrm α = (P + 1 + 1) ^ (2 * k)) (pα : Prim43 α) : α = pw (Ring43.ring43 LibInt.intR) (nPi P) k := by
  obtain ⟨u, hu, he⟩ := factor_inert P hpr hc k α hα
  rw [pw_neg] at he
  generalize hv : (Ring43.ring43 LibInt.intR).mul u (pw (Ring43.ring43 LibInt.intR) ((Ring43.ring43 LibInt.intR).neg (Ring43.ring43 LibInt.intR).one) k) = v
  have he' : α = (Ring43.ring43 LibInt.intR).mul v (pw (Ring43.ring43 LibInt.intR) (nPi P) k) := by
    rw [he, ← hv]; lib_ring
  have hv1 : nrm v = 1 := by rw [← hv, nrm_mul, hu, nrm_neg_one_pw]
  have v1 := unit_one v _ (nrm_one_eq v hv1) (prim43_pow _ (prim_nPi P hc) k) (by rw [← he']; exact pα)
  rw [he', v1]; lib_ring

theorem nrm_nPi : nrm (nPi P) = (P + 1 + 1) * (P + 1 + 1) := by
  apply Int.ofNat.inj; rw [nrm_cast]
  show -Int.ofNat (P + 1 + 1) * -Int.ofNat (P + 1 + 1) + -Int.ofNat (P + 1 + 1) * 0 + 11 * (0 * 0) = Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1)
  lib_int

theorem nrm_nPi_pw (k : Nat) : nrm (pw (Ring43.ring43 LibInt.intR) (nPi P) k) = (P + 1 + 1) ^ (2 * k) := by
  rw [HeckePrime43.nrm_pw, nrm_nPi]
  induction k with
  | zero => rfl
  | succ k ih =>
    show ((P + 1 + 1) * (P + 1 + 1)) ^ k * ((P + 1 + 1) * (P + 1 + 1)) = _
    rw [ih, nat_pow2]; lib_nat

/-- **c_W(p^(2k)) = W((−p)^k)** at an inert prime. -/
theorem cw43_even (W : Int × Int → Int × Int) (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : QChar.chi 20 (LibRing.castZ (ZMod.ring (2 * 20 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) (k : Nat) :
    cw43 W ((P + 1 + 1) ^ (2 * k)) = W (pw (Ring43.ring43 LibInt.intR) (nPi P) k) := by
  have hPD : PD43 ((P + 1 + 1) ^ (2 * k)) (pw (Ring43.ring43 LibInt.intR) (nPi P) k) :=
    ⟨by rw [← nrm_cast, nrm_nPi_pw], prim43_pow _ (prim_nPi P hc) k⟩
  unfold cw43
  rw [sumK_ext (Ring43.ring43 LibInt.intR) _ (fun z => (Ring43.ring43 LibInt.intR).mul (delta (Ring43.ring43 LibInt.intR) z (pw (Ring43.ring43 LibInt.intR) (nPi P) k)) (W z)) (fun z => by
    by_cases hz : PD43 ((P + 1 + 1) ^ (2 * k)) z
    · have e := prim_inert P hpr hc k z (Int.ofNat.inj (by rw [nrm_cast, hz.1])) hz.2
      rw [I43_pos _ _ hz]; unfold delta; rw [ite_pos' _ _ e]
    · rw [I43_neg _ _ hz]; unfold delta
      rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact hPD))])]
  have hb := abs_le43 _ _ hPD.1
  exact HeckeMult.pick_box (Ring43.ring43 LibInt.intR) W _ _ hb.1 hb.2

theorem pow_odd (hc : QChar.chi 20 (LibRing.castZ (ZMod.ring (2 * 20 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) : ∀ k, QChar.chi 20 (LibRing.castZ (ZMod.ring (2 * 20 + 1 + 1)) (Int.ofNat ((P + 1 + 1) ^ (2 * k + 1)))) = -1
  | 0 => by rw [show (P + 1 + 1) ^ (2 * 0 + 1) = P + 1 + 1 from Nat.one_mul _]; exact hc
  | k + 1 => by
    rw [show (P + 1 + 1) ^ (2 * (k + 1) + 1) = (P + 1 + 1) ^ (2 * k + 1) * ((P + 1 + 1) * (P + 1 + 1)) by
      rw [show 2 * (k + 1) + 1 = 2 * k + 1 + 1 + 1 by lib_nat]; show (P + 1 + 1) ^ (2 * k + 1) * (P + 1 + 1) * (P + 1 + 1) = _; lib_nat]
    show QChar.chi 20 (LibRing.castZ (ZMod.ring (2 * 20 + 1 + 1)) (Int.ofNat ((P + 1 + 1) ^ (2 * k + 1)) * (Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1)))) = -1
    rw [HasseDavenport.castZ_mul, HasseDavenport.castZ_mul, QChar.chi_mul 20 Prim43.prime43, QChar.chi_mul 20 Prim43.prime43, pow_odd hc k, hc]; rfl

/-- **c_W(p^(2k+1)) = 0** at an inert prime. -/
theorem cw43_odd (W : Int × Int → Int × Int) (hc : QChar.chi 20 (LibRing.castZ (ZMod.ring (2 * 20 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) (k : Nat) : cw43 W ((P + 1 + 1) ^ (2 * k + 1)) = (Ring43.ring43 LibInt.intR).zero := by
  exact cw43_zero W _ (pow_odd P hc k)

end Inert43

#print axioms Inert43.cast_en
#print axioms Inert43.en_mod43
#print axioms Inert43.cw43_zero
#print axioms Inert43.en_Pi
#print axioms Inert43.nrm_Pi
#print axioms Inert43.cj_Pi
#print axioms Inert43.not_p
#print axioms Inert43.dvd_conv
#print axioms Inert43.split_sq
#print axioms Inert43.inert_div
#print axioms Inert43.nat_pow2
#print axioms Inert43.factor_inert
#print axioms Inert43.prim_nPi
#print axioms Inert43.pw_neg
#print axioms Inert43.nrm_neg_one_pw
#print axioms Inert43.prim_inert
#print axioms Inert43.nrm_nPi
#print axioms Inert43.nrm_nPi_pw
#print axioms Inert43.cw43_even
#print axioms Inert43.pow_odd
#print axioms Inert43.cw43_odd

lang/Inert67.lean

lean_all/lang/Inert67.lean

/-
  lang/Inert67 — T48/T50: **the ℚ(√−67) Hecke character at inert primes** (χ₆₇(p) = −1), via the Legendre symbol:
    cast_en, en_mod67   N(z) ≡ ρ(z)² (mod 67) (68N = ρ² + 67a²), so χ₆₇(N(z)) ≠ −1
    cw67_zero, inert_div, factor_inert, prim_inert, cw67_even, pow_odd, cw67_odd   (port of Inert19)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.HeckePrime67

namespace Inert67

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius Euc67 Prim67 Uniq67 Prime67 HeckeMult67

variable (P : Nat)

theorem cast_en (z : Int × Int) : LibRing.castZ (ZMod.ring (2 * 32 + 1 + 1)) (en z) = (ZMod.ring (2 * 32 + 1 + 1)).mul (Prim67.cr z) (Prim67.cr z) := by
  have e44 : 68 * en z = Prim67.rho z * Prim67.rho z + 67 * (z.1 * z.1) := by
    show 68 * (z.1 * z.1 + z.1 * z.2 + 17 * (z.2 * z.2)) = (z.1 + 34 * z.2) * (z.1 + 34 * z.2) + 67 * (z.1 * z.1); lib_int
  have h1 : LibRing.castZ (ZMod.ring (2 * 32 + 1 + 1)) (68 * en z) = LibRing.castZ (ZMod.ring (2 * 32 + 1 + 1)) (en z) := by
    rw [HasseDavenport.castZ_mul, show LibRing.castZ (ZMod.ring (2 * 32 + 1 + 1)) 68 = (ZMod.ring (2 * 32 + 1 + 1)).one from rfl]; lib_ring
  rw [← h1, e44, IntCast.castZ_add, HasseDavenport.castZ_mul, Prim67.cz67]; unfold Prim67.cr; lib_ring

/-- N(z) is never a non-residue mod 67. -/
theorem en_mod67 (z : Int × Int) (n : Nat) (hn : QChar.chi 32 (LibRing.castZ (ZMod.ring (2 * 32 + 1 + 1)) (Int.ofNat n)) = -1) (h : en z = Int.ofNat n) : False := by
  rw [← h, cast_en] at hn
  by_cases h0 : Prim67.cr z = (ZMod.ring (2 * 32 + 1 + 1)).zero
  · rw [h0, show (ZMod.ring (2 * 32 + 1 + 1)).mul (ZMod.ring (2 * 32 + 1 + 1)).zero (ZMod.ring (2 * 32 + 1 + 1)).zero = (ZMod.ring (2 * 32 + 1 + 1)).zero by lib_ring, QChar.chi_zero] at hn; exact Int.noConfusion hn
  · rw [QChar.chi_sq 32 Prim67.prime67 _ h0] at hn; exact Int.noConfusion hn

/-- **c_W(n) = 0 for n a non-residue mod 67.** -/
theorem cw67_zero (W : Int × Int → Int × Int) (n : Nat) (hn : QChar.chi 32 (LibRing.castZ (ZMod.ring (2 * 32 + 1 + 1)) (Int.ofNat n)) = -1) : cw67 W n = (Ring67.ring67 LibInt.intR).zero := by
  unfold cw67
  rw [sumK_ext (Ring67.ring67 LibInt.intR) _ (fun _ => (Ring67.ring67 LibInt.intR).zero) (fun z => by
    rw [I67_neg _ _ (fun h => en_mod67 z n hn h.1)]; lib_ring)]
  exact zsum67 _

def Pi : Int × Int := (Int.ofNat (P + 1 + 1), 0)

theorem en_Pi : en (Pi P) = Int.ofNat ((P + 1 + 1) * (P + 1 + 1)) := by
  show Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1) + Int.ofNat (P + 1 + 1) * 0 + 17 * (0 * 0) = Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1)
  lib_int

theorem nrm_Pi : nrm (Pi P) = (P + 1 + 1) * (P + 1 + 1) := Int.ofNat.inj (by rw [nrm_cast, en_Pi])

theorem cj_Pi : cj (Pi P) = Pi P := Prod.ext (by show Int.ofNat (P + 1 + 1) + 0 = Int.ofNat (P + 1 + 1); lib_int) (by show -(0 : Int) = 0; rfl)

theorem not_p (hc : QChar.chi 32 (LibRing.castZ (ZMod.ring (2 * 32 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) (z : Int × Int) (h : nrm z = P + 1 + 1) : False :=
  en_mod67 z (P + 1 + 1) hc (by rw [← nrm_cast, h])

theorem dvd_conv (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) : ∀ d, d ∣ (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1 :=
  fun d ⟨c, hc⟩ => hpr d ⟨c, by rw [hc]; lib_nat⟩

/-- the two factor case: a b = p², p ∤ … ⇒ a = 1 or b = 1 (a = p is excluded separately). -/
theorem split_sq (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (a b : Nat) (h : a * b = (P + 1 + 1) * (P + 1 + 1)) :
    a = 1 ∨ b = 1 ∨ a = P + 1 + 1 := by
  cases euclid_prime (dvd_conv P hpr) (show (P + 1 + 1) ∣ a * b from ⟨P + 1 + 1, h⟩) with
  | inl ha =>
    obtain ⟨s, hs⟩ := ha
    have e : s * b = P + 1 + 1 := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (P + 1)) (by rw [show (P + 1 + 1) * (s * b) = (P + 1 + 1) * s * b by lib_nat, ← hs, h])
    cases hpr s ⟨b, by rw [← e]; lib_nat⟩ with
    | inl s1 => exact Or.inr (Or.inr (by rw [hs, s1]; lib_nat))
    | inr sp =>
      refine Or.inr (Or.inl ?_)
      rw [sp] at e
      exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (P + 1)) (e.trans (Nat.mul_one _).symm)
  | inr hb =>
    obtain ⟨s, hs⟩ := hb
    have e : a * s = P + 1 + 1 := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (P + 1)) (by
      rw [show (P + 1 + 1) * (a * s) = a * ((P + 1 + 1) * s) by lib_nat, ← hs, h])
    cases hpr a ⟨s, by rw [← e]; lib_nat⟩ with
    | inl a1 => exact Or.inl a1
    | inr ap => exact Or.inr (Or.inr ap)

/-- **p ≡ 2 (mod 3) stays prime in ℤ[ω].** -/
theorem inert_div (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : QChar.chi 32 (LibRing.castZ (ZMod.ring (2 * 32 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) (x y : Int × Int)
    (h : Dvd' (Pi P) ((Ring67.ring67 LibInt.intR).mul x y)) : Dvd' (Pi P) x ∨ Dvd' (Pi P) y := by
  match egcd_exists (Pi P) x with
  | ⟨d, X, Y, hd, ⟨c1, hc1⟩, ⟨c2, hc2⟩⟩ =>
    have hdc : nrm d * nrm c1 = (P + 1 + 1) * (P + 1 + 1) := by rw [← nrm_mul, ← hc1, nrm_Pi]
    rcases split_sq P hpr _ _ hdc with h1 | h1 | h1
    · refine Or.inr (eeuclid (Pi P) x y ((Ring67.ring67 LibInt.intR).mul (cj d) X) ((Ring67.ring67 LibInt.intR).mul (cj d) Y) ?_ h)
      rw [show (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul (cj d) X) (Pi P)) ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul (cj d) Y) x)
          = (Ring67.ring67 LibInt.intR).mul (cj d) ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul X (Pi P)) ((Ring67.ring67 LibInt.intR).mul Y x)) by lib_ring,
        ← hd, (Ring67.ring67 LibInt.intR).mul_comm, unit_inv d h1]
    · refine Or.inl ⟨(Ring67.ring67 LibInt.intR).mul (cj c1) c2, ?_⟩
      have hd' : d = (Ring67.ring67 LibInt.intR).mul (Pi P) (cj c1) := by
        rw [hc1]
        calc d = (Ring67.ring67 LibInt.intR).mul d (Ring67.ring67 LibInt.intR).one := by lib_ring
          _ = _ := by rw [← unit_inv c1 h1]; lib_ring
      rw [hc2, hd']; lib_ring
    · exact (not_p P hc d h1).elim

theorem nat_pow2 (k : Nat) : (P + 1 + 1) ^ (2 * (k + 1)) = (P + 1 + 1) * (P + 1 + 1) * (P + 1 + 1) ^ (2 * k) := by
  rw [show 2 * (k + 1) = 2 * k + 1 + 1 by lib_nat]
  show (P + 1 + 1) ^ (2 * k) * (P + 1 + 1) * (P + 1 + 1) = _
  lib_nat

/-- **Factorisation above an inert prime.** -/
theorem factor_inert (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : QChar.chi 32 (LibRing.castZ (ZMod.ring (2 * 32 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) : ∀ k (α : Int × Int),
    nrm α = (P + 1 + 1) ^ (2 * k) → ∃ u, nrm u = 1 ∧ α = (Ring67.ring67 LibInt.intR).mul u (pw (Ring67.ring67 LibInt.intR) (Pi P) k)
  | 0, α, h => ⟨α, h, by show α = (Ring67.ring67 LibInt.intR).mul α (Ring67.ring67 LibInt.intR).one; lib_ring⟩
  | k + 1, α, h => by
    have hdiv : Dvd' (Pi P) ((Ring67.ring67 LibInt.intR).mul α (cj α)) := by
      refine ⟨(Int.ofNat ((P + 1 + 1) * (P + 1 + 1) ^ (2 * k)), 0), ?_⟩
      rw [mul_cj, ← nrm_cast, h, nat_pow2]
      apply Prod.ext
      · show Int.ofNat ((P + 1 + 1) * (P + 1 + 1) * (P + 1 + 1) ^ (2 * k)) = Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) * (P + 1 + 1) ^ (2 * k)) + -(0 * 0)
        show Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ (2 * k)) = _
        show _ = Int.ofNat (P + 1 + 1) * (Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ (2 * k))) + -(0 * 0)
        lib_int
      · show (0 : Int) = Int.ofNat (P + 1 + 1) * 0 + 0 * Int.ofNat ((P + 1 + 1) * (P + 1 + 1) ^ (2 * k)) + -(0 * 0); lib_int
    have cancel : ∀ β : Int × Int, nrm ((Ring67.ring67 LibInt.intR).mul (Pi P) β) = (P + 1 + 1) ^ (2 * (k + 1)) → nrm β = (P + 1 + 1) ^ (2 * k) := fun β hb => by
      rw [nrm_mul, nrm_Pi, nat_pow2] at hb
      exact Nat.eq_of_mul_eq_mul_left (Nat.mul_pos (Nat.zero_lt_succ _) (Nat.zero_lt_succ _)) hb
    have hβ : ∃ β, α = (Ring67.ring67 LibInt.intR).mul (Pi P) β := by
      cases inert_div P hpr hc α (cj α) hdiv with
      | inl hα => exact hα
      | inr hα =>
        obtain ⟨β, hβ⟩ := hα
        exact ⟨cj β, by rw [← cj_Pi P, ← cj_mul, ← hβ, cj_cj]⟩
    obtain ⟨β, hβ⟩ := hβ
    obtain ⟨u, hu, he⟩ := factor_inert hpr hc k β (cancel β (by rw [← hβ]; exact h))
    refine ⟨u, hu, ?_⟩
    rw [hβ, he]; show _ = (Ring67.ring67 LibInt.intR).mul u ((Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) (Pi P) k) (Pi P)); lib_ring

def nPi : Int × Int := (-Int.ofNat (P + 1 + 1), 0)

theorem prim_nPi (hc : QChar.chi 32 (LibRing.castZ (ZMod.ring (2 * 32 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) : Prim67 (nPi P) := by
  show QChar.chi 32 (Prim67.cr (nPi P)) = 1
  have e : Prim67.cr (nPi P) = (ZMod.ring (2 * 32 + 1 + 1)).neg (LibRing.castZ (ZMod.ring (2 * 32 + 1 + 1)) (Int.ofNat (P + 1 + 1))) := by
    unfold Prim67.cr; rw [show Prim67.rho (nPi P) = -Int.ofNat (P + 1 + 1) by show -Int.ofNat (P + 1 + 1) + 34 * 0 = _; lib_int, HasseDavenport.castZ_neg]
  rw [e, Prim67.chi_neg, hc]; rfl

theorem pw_neg (k : Nat) : pw (Ring67.ring67 LibInt.intR) (Pi P) k =
    (Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) ((Ring67.ring67 LibInt.intR).neg (Ring67.ring67 LibInt.intR).one) k) (pw (Ring67.ring67 LibInt.intR) (nPi P) k) := by
  rw [← Frobenius.pw_mul]
  congr 1
  apply Prod.ext
  · show Int.ofNat (P + 1 + 1) = -1 * -Int.ofNat (P + 1 + 1) + -(-0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0); lib_int
  · show (0 : Int) = -1 * 0 + -0 * -Int.ofNat (P + 1 + 1) + -0 * 0; lib_int

theorem nrm_neg_one_pw : ∀ k, nrm (pw (Ring67.ring67 LibInt.intR) ((Ring67.ring67 LibInt.intR).neg (Ring67.ring67 LibInt.intR).one) k) = 1
  | 0 => rfl
  | k + 1 => by
    show nrm ((Ring67.ring67 LibInt.intR).mul _ _) = 1
    rw [nrm_mul, nrm_neg_one_pw k]; rfl

/-- **The primary elements of norm p^(2k) are (−p)^k.** -/
theorem prim_inert (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : QChar.chi 32 (LibRing.castZ (ZMod.ring (2 * 32 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) (k : Nat) (α : Int × Int)
    (hα : nrm α = (P + 1 + 1) ^ (2 * k)) (pα : Prim67 α) : α = pw (Ring67.ring67 LibInt.intR) (nPi P) k := by
  obtain ⟨u, hu, he⟩ := factor_inert P hpr hc k α hα
  rw [pw_neg] at he
  generalize hv : (Ring67.ring67 LibInt.intR).mul u (pw (Ring67.ring67 LibInt.intR) ((Ring67.ring67 LibInt.intR).neg (Ring67.ring67 LibInt.intR).one) k) = v
  have he' : α = (Ring67.ring67 LibInt.intR).mul v (pw (Ring67.ring67 LibInt.intR) (nPi P) k) := by
    rw [he, ← hv]; lib_ring
  have hv1 : nrm v = 1 := by rw [← hv, nrm_mul, hu, nrm_neg_one_pw]
  have v1 := unit_one v _ (nrm_one_eq v hv1) (prim67_pow _ (prim_nPi P hc) k) (by rw [← he']; exact pα)
  rw [he', v1]; lib_ring

theorem nrm_nPi : nrm (nPi P) = (P + 1 + 1) * (P + 1 + 1) := by
  apply Int.ofNat.inj; rw [nrm_cast]
  show -Int.ofNat (P + 1 + 1) * -Int.ofNat (P + 1 + 1) + -Int.ofNat (P + 1 + 1) * 0 + 17 * (0 * 0) = Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1)
  lib_int

theorem nrm_nPi_pw (k : Nat) : nrm (pw (Ring67.ring67 LibInt.intR) (nPi P) k) = (P + 1 + 1) ^ (2 * k) := by
  rw [HeckePrime67.nrm_pw, nrm_nPi]
  induction k with
  | zero => rfl
  | succ k ih =>
    show ((P + 1 + 1) * (P + 1 + 1)) ^ k * ((P + 1 + 1) * (P + 1 + 1)) = _
    rw [ih, nat_pow2]; lib_nat

/-- **c_W(p^(2k)) = W((−p)^k)** at an inert prime. -/
theorem cw67_even (W : Int × Int → Int × Int) (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : QChar.chi 32 (LibRing.castZ (ZMod.ring (2 * 32 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) (k : Nat) :
    cw67 W ((P + 1 + 1) ^ (2 * k)) = W (pw (Ring67.ring67 LibInt.intR) (nPi P) k) := by
  have hPD : PD67 ((P + 1 + 1) ^ (2 * k)) (pw (Ring67.ring67 LibInt.intR) (nPi P) k) :=
    ⟨by rw [← nrm_cast, nrm_nPi_pw], prim67_pow _ (prim_nPi P hc) k⟩
  unfold cw67
  rw [sumK_ext (Ring67.ring67 LibInt.intR) _ (fun z => (Ring67.ring67 LibInt.intR).mul (delta (Ring67.ring67 LibInt.intR) z (pw (Ring67.ring67 LibInt.intR) (nPi P) k)) (W z)) (fun z => by
    by_cases hz : PD67 ((P + 1 + 1) ^ (2 * k)) z
    · have e := prim_inert P hpr hc k z (Int.ofNat.inj (by rw [nrm_cast, hz.1])) hz.2
      rw [I67_pos _ _ hz]; unfold delta; rw [ite_pos' _ _ e]
    · rw [I67_neg _ _ hz]; unfold delta
      rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact hPD))])]
  have hb := abs_le67 _ _ hPD.1
  exact HeckeMult.pick_box (Ring67.ring67 LibInt.intR) W _ _ hb.1 hb.2

theorem pow_odd (hc : QChar.chi 32 (LibRing.castZ (ZMod.ring (2 * 32 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) : ∀ k, QChar.chi 32 (LibRing.castZ (ZMod.ring (2 * 32 + 1 + 1)) (Int.ofNat ((P + 1 + 1) ^ (2 * k + 1)))) = -1
  | 0 => by rw [show (P + 1 + 1) ^ (2 * 0 + 1) = P + 1 + 1 from Nat.one_mul _]; exact hc
  | k + 1 => by
    rw [show (P + 1 + 1) ^ (2 * (k + 1) + 1) = (P + 1 + 1) ^ (2 * k + 1) * ((P + 1 + 1) * (P + 1 + 1)) by
      rw [show 2 * (k + 1) + 1 = 2 * k + 1 + 1 + 1 by lib_nat]; show (P + 1 + 1) ^ (2 * k + 1) * (P + 1 + 1) * (P + 1 + 1) = _; lib_nat]
    show QChar.chi 32 (LibRing.castZ (ZMod.ring (2 * 32 + 1 + 1)) (Int.ofNat ((P + 1 + 1) ^ (2 * k + 1)) * (Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1)))) = -1
    rw [HasseDavenport.castZ_mul, HasseDavenport.castZ_mul, QChar.chi_mul 32 Prim67.prime67, QChar.chi_mul 32 Prim67.prime67, pow_odd hc k, hc]; rfl

/-- **c_W(p^(2k+1)) = 0** at an inert prime. -/
theorem cw67_odd (W : Int × Int → Int × Int) (hc : QChar.chi 32 (LibRing.castZ (ZMod.ring (2 * 32 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) (k : Nat) : cw67 W ((P + 1 + 1) ^ (2 * k + 1)) = (Ring67.ring67 LibInt.intR).zero := by
  exact cw67_zero W _ (pow_odd P hc k)

end Inert67

#print axioms Inert67.cast_en
#print axioms Inert67.en_mod67
#print axioms Inert67.cw67_zero
#print axioms Inert67.en_Pi
#print axioms Inert67.nrm_Pi
#print axioms Inert67.cj_Pi
#print axioms Inert67.not_p
#print axioms Inert67.dvd_conv
#print axioms Inert67.split_sq
#print axioms Inert67.inert_div
#print axioms Inert67.nat_pow2
#print axioms Inert67.factor_inert
#print axioms Inert67.prim_nPi
#print axioms Inert67.pw_neg
#print axioms Inert67.nrm_neg_one_pw
#print axioms Inert67.prim_inert
#print axioms Inert67.nrm_nPi
#print axioms Inert67.nrm_nPi_pw
#print axioms Inert67.cw67_even
#print axioms Inert67.pow_odd
#print axioms Inert67.cw67_odd

lang/Inert7.lean

lean_all/lang/Inert7.lean

/-
  lang/Inert7 — T48/T50, cycle 645: **49a's Hecke character at inert primes** p ≡ 3, 5, 6 (mod 7):
    en_mod7        N(a + bθ) ≡ (a + 4b)² (mod 7) (8N = (a + 4b)² + 7a²) is never ≡ 3, 5, 6
    cw7_zero       c_W(n) = 0 for n ≡ 3, 5, 6 (mod 7)
    inert_div, factor_inert, prim_inert   p stays prime; the H7-elements of norm p^(2k) are (−p)^k
    cw7_even, cw7_odd                     c_W(p^(2k)) = W((−p)^k), c_W(p^(2k+1)) = 0
  (Port of Sq2Inert8.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.HeckePrime7

namespace Inert7

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius Euc7 Prim7 Uniq7 Prime7 HeckeMult7

variable (P : Nat)

theorem en_mod7 (z : Int × Int) (k : Int) (h : en z = 7 * k + 3 ∨ en z = 7 * k + 5 ∨ en z = 7 * k + 6) : False := by
  obtain ⟨a, b⟩ := z
  have hn : en (a, b) = a * a + a * b + 2 * (b * b) := rfl
  obtain ⟨q, r, hq, hr⟩ := GaussEuclid.rnd (a + 4 * b) 6
  have hq' : a + 4 * b = 7 * q + r := by rw [hq]; show q * 7 + r = 7 * q + r; lib_int
  have key : en (a, b) = r * r + 7 * (7 * q * q + 2 * q * r + a * a + -en (a, b)) := by
    have e8 : 8 * en (a, b) = (a + 4 * b) * (a + 4 * b) + 7 * (a * a) := by rw [hn]; lib_int
    rw [hq'] at e8
    calc en (a, b) = 8 * en (a, b) + -(7 * en (a, b)) := by lib_int
      _ = (7 * q + r) * (7 * q + r) + 7 * (a * a) + -(7 * en (a, b)) := by rw [e8]
      _ = _ := by lib_int
  generalize en (a, b) = N at key h
  generalize 7 * q * q + 2 * q * r + a * a + -N = X at key
  have cases_r : r = 0 ∨ r = 1 ∨ r = 2 ∨ r = 3 ∨ r = -1 ∨ r = -2 ∨ r = -3 := by
    cases r with
    | ofNat t =>
      match t, hr with
      | 0, _ => exact Or.inl rfl
      | 1, _ => exact Or.inr (Or.inl rfl)
      | 2, _ => exact Or.inr (Or.inr (Or.inl rfl))
      | 3, _ => exact Or.inr (Or.inr (Or.inr (Or.inl rfl)))
      | t + 4, hr' =>
        exfalso
        have : 2 * (t + 4) ≤ 7 := hr'
        rw [show 2 * (t + 4) = 2 * t + 1 + 7 by lib_nat] at this
        exact Nat.not_succ_le_self 7 (Nat.le_trans (Nat.add_le_add_right (Nat.zero_le (2 * t)) 8) (Nat.le_trans (Nat.le_of_eq (by lib_nat)) this))
    | negSucc t =>
      match t, hr with
      | 0, _ => exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl rfl))))
      | 1, _ => exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl rfl)))))
      | 2, _ => exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr rfl)))))
      | t + 3, hr' =>
        exfalso
        have : 2 * (t + 4) ≤ 7 := hr'
        rw [show 2 * (t + 4) = 2 * t + 1 + 7 by lib_nat] at this
        exact Nat.not_succ_le_self 7 (Nat.le_trans (Nat.add_le_add_right (Nat.zero_le (2 * t)) 8) (Nat.le_trans (Nat.le_of_eq (by lib_nat)) this))
  rcases cases_r with e | e | e | e | e | e | e <;> rw [e] at key <;> rcases h with h | h | h
  · exact Sq2Prim8.noM 6 (k + -X + -(-1)) 3 2 (by calc (7 : Int) * (k + -X + -(-1)) = (7 * k + 3) + -(0 * 0 + 7 * X) + 4 + -(-3) + (0 * 0 + -3) := by lib_int
        _ = N + -N + 4 := by rw [← h, ← key]; lib_int
        _ = 4 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (k + -X + -(-1)) 1 4 (by calc (7 : Int) * (k + -X + -(-1)) = (7 * k + 5) + -(0 * 0 + 7 * X) + 2 + -(-5) + (0 * 0 + -5) := by lib_int
        _ = N + -N + 2 := by rw [← h, ← key]; lib_int
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (k + -X + -(-1)) 0 5 (by calc (7 : Int) * (k + -X + -(-1)) = (7 * k + 6) + -(0 * 0 + 7 * X) + 1 + -(-6) + (0 * 0 + -6) := by lib_int
        _ = N + -N + 1 := by rw [← h, ← key]; lib_int
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (k + -X + -(-1)) 4 1 (by calc (7 : Int) * (k + -X + -(-1)) = (7 * k + 3) + -(1 * 1 + 7 * X) + 5 + -(-2) + (1 * 1 + -3) := by lib_int
        _ = N + -N + 5 := by rw [← h, ← key]; lib_int
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (k + -X + -(-1)) 2 3 (by calc (7 : Int) * (k + -X + -(-1)) = (7 * k + 5) + -(1 * 1 + 7 * X) + 3 + -(-4) + (1 * 1 + -5) := by lib_int
        _ = N + -N + 3 := by rw [← h, ← key]; lib_int
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (k + -X + -(-1)) 1 4 (by calc (7 : Int) * (k + -X + -(-1)) = (7 * k + 6) + -(1 * 1 + 7 * X) + 2 + -(-5) + (1 * 1 + -6) := by lib_int
        _ = N + -N + 2 := by rw [← h, ← key]; lib_int
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (k + -X + -(0)) 0 5 (by calc (7 : Int) * (k + -X + -(0)) = (7 * k + 3) + -(2 * 2 + 7 * X) + 1 + -(1) + (2 * 2 + -3) := by lib_int
        _ = N + -N + 1 := by rw [← h, ← key]; lib_int
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (k + -X + -(-1)) 5 0 (by calc (7 : Int) * (k + -X + -(-1)) = (7 * k + 5) + -(2 * 2 + 7 * X) + 6 + -(-1) + (2 * 2 + -5) := by lib_int
        _ = N + -N + 6 := by rw [← h, ← key]; lib_int
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (k + -X + -(-1)) 4 1 (by calc (7 : Int) * (k + -X + -(-1)) = (7 * k + 6) + -(2 * 2 + 7 * X) + 5 + -(-2) + (2 * 2 + -6) := by lib_int
        _ = N + -N + 5 := by rw [← h, ← key]; lib_int
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (k + -X + -(0)) 5 0 (by calc (7 : Int) * (k + -X + -(0)) = (7 * k + 3) + -(3 * 3 + 7 * X) + 6 + -(6) + (3 * 3 + -3) := by lib_int
        _ = N + -N + 6 := by rw [← h, ← key]; lib_int
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (k + -X + -(0)) 3 2 (by calc (7 : Int) * (k + -X + -(0)) = (7 * k + 5) + -(3 * 3 + 7 * X) + 4 + -(4) + (3 * 3 + -5) := by lib_int
        _ = N + -N + 4 := by rw [← h, ← key]; lib_int
        _ = 4 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (k + -X + -(0)) 2 3 (by calc (7 : Int) * (k + -X + -(0)) = (7 * k + 6) + -(3 * 3 + 7 * X) + 3 + -(3) + (3 * 3 + -6) := by lib_int
        _ = N + -N + 3 := by rw [← h, ← key]; lib_int
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (k + -X + -(-1)) 4 1 (by calc (7 : Int) * (k + -X + -(-1)) = (7 * k + 3) + -(-1 * -1 + 7 * X) + 5 + -(-2) + (-1 * -1 + -3) := by lib_int
        _ = N + -N + 5 := by rw [← h, ← key]; lib_int
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (k + -X + -(-1)) 2 3 (by calc (7 : Int) * (k + -X + -(-1)) = (7 * k + 5) + -(-1 * -1 + 7 * X) + 3 + -(-4) + (-1 * -1 + -5) := by lib_int
        _ = N + -N + 3 := by rw [← h, ← key]; lib_int
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (k + -X + -(-1)) 1 4 (by calc (7 : Int) * (k + -X + -(-1)) = (7 * k + 6) + -(-1 * -1 + 7 * X) + 2 + -(-5) + (-1 * -1 + -6) := by lib_int
        _ = N + -N + 2 := by rw [← h, ← key]; lib_int
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (k + -X + -(0)) 0 5 (by calc (7 : Int) * (k + -X + -(0)) = (7 * k + 3) + -(-2 * -2 + 7 * X) + 1 + -(1) + (-2 * -2 + -3) := by lib_int
        _ = N + -N + 1 := by rw [← h, ← key]; lib_int
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (k + -X + -(-1)) 5 0 (by calc (7 : Int) * (k + -X + -(-1)) = (7 * k + 5) + -(-2 * -2 + 7 * X) + 6 + -(-1) + (-2 * -2 + -5) := by lib_int
        _ = N + -N + 6 := by rw [← h, ← key]; lib_int
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (k + -X + -(-1)) 4 1 (by calc (7 : Int) * (k + -X + -(-1)) = (7 * k + 6) + -(-2 * -2 + 7 * X) + 5 + -(-2) + (-2 * -2 + -6) := by lib_int
        _ = N + -N + 5 := by rw [← h, ← key]; lib_int
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (k + -X + -(0)) 5 0 (by calc (7 : Int) * (k + -X + -(0)) = (7 * k + 3) + -(-3 * -3 + 7 * X) + 6 + -(6) + (-3 * -3 + -3) := by lib_int
        _ = N + -N + 6 := by rw [← h, ← key]; lib_int
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (k + -X + -(0)) 3 2 (by calc (7 : Int) * (k + -X + -(0)) = (7 * k + 5) + -(-3 * -3 + 7 * X) + 4 + -(4) + (-3 * -3 + -5) := by lib_int
        _ = N + -N + 4 := by rw [← h, ← key]; lib_int
        _ = 4 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (k + -X + -(0)) 2 3 (by calc (7 : Int) * (k + -X + -(0)) = (7 * k + 6) + -(-3 * -3 + 7 * X) + 3 + -(3) + (-3 * -3 + -6) := by lib_int
        _ = N + -N + 3 := by rw [← h, ← key]; lib_int
        _ = 3 := by lib_int) rfl

/-- **c_W(n) = 0 for n ≡ 3, 5, 6 (mod 7).** -/
theorem cw7_zero (W : Int × Int → Int × Int) (n : Nat) (k : Int) (hn : Int.ofNat n = 7 * k + 3 ∨ Int.ofNat n = 7 * k + 5 ∨ Int.ofNat n = 7 * k + 6) : cw7 W n = (Ring7.ring7 LibInt.intR).zero := by
  unfold cw7
  rw [sumK_ext (Ring7.ring7 LibInt.intR) _ (fun _ => (Ring7.ring7 LibInt.intR).zero) (fun z => by
    rw [I7_neg _ _ (fun h => en_mod7 z k (by rw [h.1]; exact hn))]; lib_ring)]
  exact zsum7 _

def Pi : Int × Int := (Int.ofNat (P + 1 + 1), 0)

theorem en_Pi : en (Pi P) = Int.ofNat ((P + 1 + 1) * (P + 1 + 1)) := by
  show Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1) + Int.ofNat (P + 1 + 1) * 0 + 2 * (0 * 0) = Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1)
  lib_int

theorem nrm_Pi : nrm (Pi P) = (P + 1 + 1) * (P + 1 + 1) := Int.ofNat.inj (by rw [nrm_cast, en_Pi])

theorem cj_Pi : cj (Pi P) = Pi P := Prod.ext (by show Int.ofNat (P + 1 + 1) + 0 = Int.ofNat (P + 1 + 1); lib_int) (by show -(0 : Int) = 0; rfl)

theorem not_p (hc : ∃ K, Int.ofNat (P + 1 + 1) = 7 * K + 3 ∨ Int.ofNat (P + 1 + 1) = 7 * K + 5 ∨ Int.ofNat (P + 1 + 1) = 7 * K + 6) (z : Int × Int) (h : nrm z = P + 1 + 1) : False := by
  obtain ⟨K, hK⟩ := hc
  exact en_mod7 z K (by rw [← nrm_cast, h]; exact hK)

theorem dvd_conv (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) : ∀ d, d ∣ (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1 :=
  fun d ⟨c, hc⟩ => hpr d ⟨c, by rw [hc]; lib_nat⟩

/-- the two factor case: a b = p², p ∤ … ⇒ a = 1 or b = 1 (a = p is excluded separately). -/
theorem split_sq (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (a b : Nat) (h : a * b = (P + 1 + 1) * (P + 1 + 1)) :
    a = 1 ∨ b = 1 ∨ a = P + 1 + 1 := by
  cases euclid_prime (dvd_conv P hpr) (show (P + 1 + 1) ∣ a * b from ⟨P + 1 + 1, h⟩) with
  | inl ha =>
    obtain ⟨s, hs⟩ := ha
    have e : s * b = P + 1 + 1 := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (P + 1)) (by rw [show (P + 1 + 1) * (s * b) = (P + 1 + 1) * s * b by lib_nat, ← hs, h])
    cases hpr s ⟨b, by rw [← e]; lib_nat⟩ with
    | inl s1 => exact Or.inr (Or.inr (by rw [hs, s1]; lib_nat))
    | inr sp =>
      refine Or.inr (Or.inl ?_)
      rw [sp] at e
      exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (P + 1)) (e.trans (Nat.mul_one _).symm)
  | inr hb =>
    obtain ⟨s, hs⟩ := hb
    have e : a * s = P + 1 + 1 := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (P + 1)) (by
      rw [show (P + 1 + 1) * (a * s) = a * ((P + 1 + 1) * s) by lib_nat, ← hs, h])
    cases hpr a ⟨s, by rw [← e]; lib_nat⟩ with
    | inl a1 => exact Or.inl a1
    | inr ap => exact Or.inr (Or.inr ap)

/-- **p ≡ 2 (mod 3) stays prime in ℤ[ω].** -/
theorem inert_div (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : ∃ K, Int.ofNat (P + 1 + 1) = 7 * K + 3 ∨ Int.ofNat (P + 1 + 1) = 7 * K + 5 ∨ Int.ofNat (P + 1 + 1) = 7 * K + 6) (x y : Int × Int)
    (h : Dvd' (Pi P) ((Ring7.ring7 LibInt.intR).mul x y)) : Dvd' (Pi P) x ∨ Dvd' (Pi P) y := by
  match egcd_exists _ (Pi P) x (Nat.le_refl _) with
  | ⟨d, X, Y, hd, ⟨c1, hc1⟩, ⟨c2, hc2⟩⟩ =>
    have hdc : nrm d * nrm c1 = (P + 1 + 1) * (P + 1 + 1) := by rw [← nrm_mul, ← hc1, nrm_Pi]
    rcases split_sq P hpr _ _ hdc with h1 | h1 | h1
    · refine Or.inr (eeuclid (Pi P) x y ((Ring7.ring7 LibInt.intR).mul (cj d) X) ((Ring7.ring7 LibInt.intR).mul (cj d) Y) ?_ h)
      rw [show (Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul (cj d) X) (Pi P)) ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul (cj d) Y) x)
          = (Ring7.ring7 LibInt.intR).mul (cj d) ((Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul X (Pi P)) ((Ring7.ring7 LibInt.intR).mul Y x)) by lib_ring,
        ← hd, (Ring7.ring7 LibInt.intR).mul_comm, unit_inv d h1]
    · refine Or.inl ⟨(Ring7.ring7 LibInt.intR).mul (cj c1) c2, ?_⟩
      have hd' : d = (Ring7.ring7 LibInt.intR).mul (Pi P) (cj c1) := by
        rw [hc1]
        calc d = (Ring7.ring7 LibInt.intR).mul d (Ring7.ring7 LibInt.intR).one := by lib_ring
          _ = _ := by rw [← unit_inv c1 h1]; lib_ring
      rw [hc2, hd']; lib_ring
    · exact (not_p P hc d h1).elim

theorem nat_pow2 (k : Nat) : (P + 1 + 1) ^ (2 * (k + 1)) = (P + 1 + 1) * (P + 1 + 1) * (P + 1 + 1) ^ (2 * k) := by
  rw [show 2 * (k + 1) = 2 * k + 1 + 1 by lib_nat]
  show (P + 1 + 1) ^ (2 * k) * (P + 1 + 1) * (P + 1 + 1) = _
  lib_nat

/-- **Factorisation above an inert prime.** -/
theorem factor_inert (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : ∃ K, Int.ofNat (P + 1 + 1) = 7 * K + 3 ∨ Int.ofNat (P + 1 + 1) = 7 * K + 5 ∨ Int.ofNat (P + 1 + 1) = 7 * K + 6) : ∀ k (α : Int × Int),
    nrm α = (P + 1 + 1) ^ (2 * k) → ∃ u, nrm u = 1 ∧ α = (Ring7.ring7 LibInt.intR).mul u (pw (Ring7.ring7 LibInt.intR) (Pi P) k)
  | 0, α, h => ⟨α, h, by show α = (Ring7.ring7 LibInt.intR).mul α (Ring7.ring7 LibInt.intR).one; lib_ring⟩
  | k + 1, α, h => by
    have hdiv : Dvd' (Pi P) ((Ring7.ring7 LibInt.intR).mul α (cj α)) := by
      refine ⟨(Int.ofNat ((P + 1 + 1) * (P + 1 + 1) ^ (2 * k)), 0), ?_⟩
      rw [mul_cj, ← nrm_cast, h, nat_pow2]
      apply Prod.ext
      · show Int.ofNat ((P + 1 + 1) * (P + 1 + 1) * (P + 1 + 1) ^ (2 * k)) = Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) * (P + 1 + 1) ^ (2 * k)) + -(0 * 0)
        show Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ (2 * k)) = _
        show _ = Int.ofNat (P + 1 + 1) * (Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ (2 * k))) + -(0 * 0)
        lib_int
      · show (0 : Int) = Int.ofNat (P + 1 + 1) * 0 + 0 * Int.ofNat ((P + 1 + 1) * (P + 1 + 1) ^ (2 * k)) + -(0 * 0); lib_int
    have cancel : ∀ β : Int × Int, nrm ((Ring7.ring7 LibInt.intR).mul (Pi P) β) = (P + 1 + 1) ^ (2 * (k + 1)) → nrm β = (P + 1 + 1) ^ (2 * k) := fun β hb => by
      rw [nrm_mul, nrm_Pi, nat_pow2] at hb
      exact Nat.eq_of_mul_eq_mul_left (Nat.mul_pos (Nat.zero_lt_succ _) (Nat.zero_lt_succ _)) hb
    have hβ : ∃ β, α = (Ring7.ring7 LibInt.intR).mul (Pi P) β := by
      cases inert_div P hpr hc α (cj α) hdiv with
      | inl hα => exact hα
      | inr hα =>
        obtain ⟨β, hβ⟩ := hα
        exact ⟨cj β, by rw [← cj_Pi P, ← cj_mul, ← hβ, cj_cj]⟩
    obtain ⟨β, hβ⟩ := hβ
    obtain ⟨u, hu, he⟩ := factor_inert hpr hc k β (cancel β (by rw [← hβ]; exact h))
    refine ⟨u, hu, ?_⟩
    rw [hβ, he]; show _ = (Ring7.ring7 LibInt.intR).mul u ((Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) (Pi P) k) (Pi P)); lib_ring

def nPi : Int × Int := (-Int.ofNat (P + 1 + 1), 0)

theorem prim_nPi (hc : ∃ K, Int.ofNat (P + 1 + 1) = 7 * K + 3 ∨ Int.ofNat (P + 1 + 1) = 7 * K + 5 ∨ Int.ofNat (P + 1 + 1) = 7 * K + 6) : Prim7 (nPi P) := by
  obtain ⟨K, hK | hK | hK⟩ := hc
  · exact ⟨-K + -1, Or.inr (Or.inr (by show -Int.ofNat (P + 1 + 1) + 4 * 0 = _; rw [hK]; lib_int))⟩
  · exact ⟨-K + -1, Or.inr (Or.inl (by show -Int.ofNat (P + 1 + 1) + 4 * 0 = _; rw [hK]; lib_int))⟩
  · exact ⟨-K + -1, Or.inl (by show -Int.ofNat (P + 1 + 1) + 4 * 0 = _; rw [hK]; lib_int)⟩

theorem pw_neg (k : Nat) : pw (Ring7.ring7 LibInt.intR) (Pi P) k =
    (Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) ((Ring7.ring7 LibInt.intR).neg (Ring7.ring7 LibInt.intR).one) k) (pw (Ring7.ring7 LibInt.intR) (nPi P) k) := by
  rw [← Frobenius.pw_mul]
  congr 1
  apply Prod.ext
  · show Int.ofNat (P + 1 + 1) = -1 * -Int.ofNat (P + 1 + 1) + -(-0 * 0 + -0 * 0); lib_int
  · show (0 : Int) = -1 * 0 + -0 * -Int.ofNat (P + 1 + 1) + -0 * 0; lib_int

theorem nrm_neg_one_pw : ∀ k, nrm (pw (Ring7.ring7 LibInt.intR) ((Ring7.ring7 LibInt.intR).neg (Ring7.ring7 LibInt.intR).one) k) = 1
  | 0 => rfl
  | k + 1 => by
    show nrm ((Ring7.ring7 LibInt.intR).mul _ _) = 1
    rw [nrm_mul, nrm_neg_one_pw k]; rfl

/-- **The primary elements of norm p^(2k) are (−p)^k.** -/
theorem prim_inert (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : ∃ K, Int.ofNat (P + 1 + 1) = 7 * K + 3 ∨ Int.ofNat (P + 1 + 1) = 7 * K + 5 ∨ Int.ofNat (P + 1 + 1) = 7 * K + 6) (k : Nat) (α : Int × Int)
    (hα : nrm α = (P + 1 + 1) ^ (2 * k)) (pα : Prim7 α) : α = pw (Ring7.ring7 LibInt.intR) (nPi P) k := by
  obtain ⟨u, hu, he⟩ := factor_inert P hpr hc k α hα
  rw [pw_neg] at he
  generalize hv : (Ring7.ring7 LibInt.intR).mul u (pw (Ring7.ring7 LibInt.intR) ((Ring7.ring7 LibInt.intR).neg (Ring7.ring7 LibInt.intR).one) k) = v
  have he' : α = (Ring7.ring7 LibInt.intR).mul v (pw (Ring7.ring7 LibInt.intR) (nPi P) k) := by
    rw [he, ← hv]; lib_ring
  have hv1 : nrm v = 1 := by rw [← hv, nrm_mul, hu, nrm_neg_one_pw]
  have v1 := unit_one7 v _ (nrm_one_eq v hv1) (prim7_pow _ (prim_nPi P hc) k) (by rw [← he']; exact pα)
  rw [he', v1]; lib_ring

theorem nrm_nPi : nrm (nPi P) = (P + 1 + 1) * (P + 1 + 1) := by
  apply Int.ofNat.inj; rw [nrm_cast]
  show -Int.ofNat (P + 1 + 1) * -Int.ofNat (P + 1 + 1) + -Int.ofNat (P + 1 + 1) * 0 + 2 * (0 * 0) = Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1)
  lib_int

theorem nrm_nPi_pw (k : Nat) : nrm (pw (Ring7.ring7 LibInt.intR) (nPi P) k) = (P + 1 + 1) ^ (2 * k) := by
  rw [HeckePrime7.nrm_pw, nrm_nPi]
  induction k with
  | zero => rfl
  | succ k ih =>
    show ((P + 1 + 1) * (P + 1 + 1)) ^ k * ((P + 1 + 1) * (P + 1 + 1)) = _
    rw [ih, nat_pow2]; lib_nat

/-- **c_W(p^(2k)) = W((−p)^k)** at an inert prime. -/
theorem cw7_even (W : Int × Int → Int × Int) (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : ∃ K, Int.ofNat (P + 1 + 1) = 7 * K + 3 ∨ Int.ofNat (P + 1 + 1) = 7 * K + 5 ∨ Int.ofNat (P + 1 + 1) = 7 * K + 6) (k : Nat) :
    cw7 W ((P + 1 + 1) ^ (2 * k)) = W (pw (Ring7.ring7 LibInt.intR) (nPi P) k) := by
  have hPD : PD7 ((P + 1 + 1) ^ (2 * k)) (pw (Ring7.ring7 LibInt.intR) (nPi P) k) :=
    ⟨by rw [← nrm_cast, nrm_nPi_pw], prim7_pow _ (prim_nPi P hc) k⟩
  unfold cw7
  rw [sumK_ext (Ring7.ring7 LibInt.intR) _ (fun z => (Ring7.ring7 LibInt.intR).mul (delta (Ring7.ring7 LibInt.intR) z (pw (Ring7.ring7 LibInt.intR) (nPi P) k)) (W z)) (fun z => by
    by_cases hz : PD7 ((P + 1 + 1) ^ (2 * k)) z
    · have e := prim_inert P hpr hc k z (Int.ofNat.inj (by rw [nrm_cast, hz.1])) hz.2
      rw [I7_pos _ _ hz]; unfold delta; rw [ite_pos' _ _ e]
    · rw [I7_neg _ _ hz]; unfold delta
      rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact hPD))])]
  have hb := abs_le7 _ _ hPD.1
  exact HeckeMult.pick_box (Ring7.ring7 LibInt.intR) W _ _ hb.1 hb.2

theorem sq7 (Y L : Int) (hL : Y = 7 * L + 3 ∨ Y = 7 * L + 5 ∨ Y = 7 * L + 6) : ∃ M : Int, Y * Y = 7 * M + 1 ∨ Y * Y = 7 * M + 2 ∨ Y * Y = 7 * M + 4 := by
  rcases hL with hL | hL | hL
  · exact ⟨(1) + (6) * L + (7) * L * L, Or.inr (Or.inl (by rw [hL]; lib_int))⟩
  · exact ⟨(3) + (10) * L + (7) * L * L, Or.inr (Or.inr (by rw [hL]; lib_int))⟩
  · exact ⟨(5) + (12) * L + (7) * L * L, Or.inl (by rw [hL]; lib_int)⟩

theorem step7 (X Y K L : Int) (hK : X = 7 * K + 3 ∨ X = 7 * K + 5 ∨ X = 7 * K + 6) (hL : Y = 7 * L + 3 ∨ Y = 7 * L + 5 ∨ Y = 7 * L + 6) :
    ∃ W : Int, X * Y * Y = 7 * W + 3 ∨ X * Y * Y = 7 * W + 5 ∨ X * Y * Y = 7 * W + 6 := by
  obtain ⟨M, hM⟩ := sq7 Y L hL
  have e : X * Y * Y = X * (Y * Y) := by lib_int
  rw [e]
  rcases hK with hK | hK | hK <;> rcases hM with hM | hM | hM <;> rw [hK, hM]
  · exact ⟨K + (7) * K * M + (3) * M, Or.inl (by lib_int)⟩
  · exact ⟨(2) * K + (7) * K * M + (3) * M, Or.inr (Or.inr (by lib_int))⟩
  · exact ⟨(1) + (4) * K + (7) * K * M + (3) * M, Or.inr (Or.inl (by lib_int))⟩
  · exact ⟨K + (7) * K * M + (5) * M, Or.inr (Or.inl (by lib_int))⟩
  · exact ⟨(1) + (2) * K + (7) * K * M + (5) * M, Or.inl (by lib_int)⟩
  · exact ⟨(2) + (4) * K + (7) * K * M + (5) * M, Or.inr (Or.inr (by lib_int))⟩
  · exact ⟨K + (7) * K * M + (6) * M, Or.inr (Or.inr (by lib_int))⟩
  · exact ⟨(1) + (2) * K + (7) * K * M + (6) * M, Or.inr (Or.inl (by lib_int))⟩
  · exact ⟨(3) + (4) * K + (7) * K * M + (6) * M, Or.inl (by lib_int)⟩

theorem pow_odd (hc : ∃ K, Int.ofNat (P + 1 + 1) = 7 * K + 3 ∨ Int.ofNat (P + 1 + 1) = 7 * K + 5 ∨ Int.ofNat (P + 1 + 1) = 7 * K + 6) (k : Nat) : ∃ K : Int, Int.ofNat ((P + 1 + 1) ^ (2 * k + 1)) = 7 * K + 3 ∨ Int.ofNat ((P + 1 + 1) ^ (2 * k + 1)) = 7 * K + 5 ∨ Int.ofNat ((P + 1 + 1) ^ (2 * k + 1)) = 7 * K + 6 := by
  obtain ⟨L, hL⟩ := hc
  induction k with
  | zero => exact ⟨L, by rw [show (P + 1 + 1) ^ (2 * 0 + 1) = P + 1 + 1 from Nat.one_mul _]; exact hL⟩
  | succ k ih =>
    obtain ⟨K, hK⟩ := ih
    have e : Int.ofNat ((P + 1 + 1) ^ (2 * (k + 1) + 1)) = Int.ofNat ((P + 1 + 1) ^ (2 * k + 1)) * Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1) := by
      rw [show 2 * (k + 1) + 1 = 2 * k + 1 + 1 + 1 by lib_nat]; rfl
    rw [e]
    exact step7 _ _ K L hK hL

/-- **c_W(p^(2k+1)) = 0** at an inert prime. -/
theorem cw7_odd (W : Int × Int → Int × Int) (hc : ∃ K, Int.ofNat (P + 1 + 1) = 7 * K + 3 ∨ Int.ofNat (P + 1 + 1) = 7 * K + 5 ∨ Int.ofNat (P + 1 + 1) = 7 * K + 6) (k : Nat) : cw7 W ((P + 1 + 1) ^ (2 * k + 1)) = (Ring7.ring7 LibInt.intR).zero := by
  obtain ⟨K, hK⟩ := pow_odd P hc k
  exact cw7_zero W _ K hK

end Inert7

#print axioms Inert7.en_mod7
#print axioms Inert7.cw7_zero
#print axioms Inert7.en_Pi
#print axioms Inert7.nrm_Pi
#print axioms Inert7.cj_Pi
#print axioms Inert7.not_p
#print axioms Inert7.dvd_conv
#print axioms Inert7.split_sq
#print axioms Inert7.inert_div
#print axioms Inert7.nat_pow2
#print axioms Inert7.factor_inert
#print axioms Inert7.prim_nPi
#print axioms Inert7.pw_neg
#print axioms Inert7.nrm_neg_one_pw
#print axioms Inert7.prim_inert
#print axioms Inert7.nrm_nPi
#print axioms Inert7.nrm_nPi_pw
#print axioms Inert7.cw7_even
#print axioms Inert7.sq7
#print axioms Inert7.step7
#print axioms Inert7.pow_odd
#print axioms Inert7.cw7_odd

lang/InertAll.lean

lean_all/lang/InertAll.lean

/-
  lang/InertAll — T43, cycle 181: y² = x³ − x over every field of order p^N at inert p ≡ 3 (mod 4): a = α^N + β^N.

    pow_mod4         p^(2M) ≡ 1, p^(2M+1) ≡ 3 (mod 4)
    inert_even       |F| = p^(2M): i ∈ F (Euler, h even), a = 2(−p)^M        (InertPow)
    inert_odd        |F| = p^(2M+1): χ(−1) = −1 (h odd), a = 0                 (CubeInert.odd_vanish)
  α = i√p, β = −i√p: α^N + β^N = 2(−p)^(N/2) for N even and 0 for N odd.

  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.InertPow
import lang.CubeInert

namespace InertAll

open FTAChain ZMod YMChain FiniteRecChain Frobenius GaussInt GaussFactor

theorem pow_even (P e : Int) (hP : P = 4 * e + 3) : ∀ M, ∃ f, ipow P (2 * M) = 4 * f + 1
  | 0 => ⟨0, rfl⟩
  | M + 1 =>
    match pow_even P e hP M with
    | ⟨f, hf⟩ => ⟨16 * f * e * e + 24 * f * e + 9 * f + 4 * e * e + 6 * e + 2, by
        show ipow P (2 * M) * P * P = _; rw [hf, hP]; lib_int⟩

theorem pow_mod4 (P e : Int) (hP : P = 4 * e + 3) (M : Nat) :
    (∃ f, ipow P (2 * M) = 4 * f + 1) ∧ (∃ f, ipow P (2 * M + 1) = 4 * f + 3) :=
  match pow_even P e hP M with
  | ⟨f, hf⟩ => ⟨⟨f, hf⟩, ⟨4 * f * e + 3 * f + e, by show ipow P (2 * M) * P = 4 * (4 * f * e + 3 * f + e) + 3; rw [hf, hP]; lib_int⟩⟩

theorem P_mod4 (j : Nat) : Int.ofNat (2 * (2 * j) + 1 + 1 + 1) = 4 * Int.ofNat j + 3 := congrArg Int.ofNat (by lib_nat)

variable {R : Type} [DecidableEq R]

theorem half_of (h : Nat) (f : Int) (r : Nat) (e : Int.ofNat (2 * h + 1) = 4 * f + Int.ofNat (2 * r + 1)) :
    Int.ofNat h = 2 * f + Int.ofNat r := by
  have e2 : 2 * Int.ofNat h = 2 * (2 * f + Int.ofNat r) := by
    have e1 : 2 * Int.ofNat h + 1 = 4 * f + (2 * Int.ofNat r + 1) := e
    rw [show 2 * Int.ofNat h = (2 * Int.ofNat h + 1) + -1 by lib_int, e1]; lib_int
  exact CubicTrace.cancel_N 2 (fun e => Nat.noConfusion e) _ _ e2

/-- **Even degree: a = 2(−p)^M.** -/
theorem inert_even (j : Nat) (hpr : ∀ d, Dv d (2 * (2 * j) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * j) + 1 + 1 + 1)) (F : FinField.FF R) (M : Nat)
    (hq : Int.ofNat (2 * F.h + 1) = ipow (Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) (2 * M)) (key : R → Nat) (hkey : ∀ x y, key x = key y → x = y) :
    -(sumK LibInt.intR (fun x => FinField.chi F (FinSign.f F x)) F.els) = 2 * ipow (-Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) M := by
  match (pow_mod4 _ _ (P_mod4 j) M).1 with
  | ⟨f, hf⟩ =>
    have hh := half_of F.h f 0 (hq.trans hf)
    match EisenUnique.nat_of_three |> fun _ => (show ∃ s : Nat, F.h = 2 * s from by
        cases f with
        | ofNat s => exact ⟨s, Int.ofNat.inj (hh.trans (by show Int.ofNat (2 * s) + 0 = _; rfl))⟩
        | negSucc s => exfalso; exact Int.noConfusion (hh.trans (by rfl : 2 * Int.negSucc s + Int.ofNat 0 = Int.negSucc (2 * s + 1)))) with
    | ⟨s, hs⟩ =>
      match F.euler_hard (F.K.neg F.K.one) (by rw [hs]; exact SplitHasse.negone_even F.K s) with
      | ⟨i, hi⟩ => exact InertPow.inert_pow j hpr F M hq key hkey i hi

/-- **Odd degree: a = 0.** -/
theorem inert_odd (j : Nat) (F : FinField.FF R) (M : Nat)
    (hq : Int.ofNat (2 * F.h + 1) = ipow (Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) (2 * M + 1)) : -(sumK LibInt.intR (fun x => FinField.chi F (FinSign.f F x)) F.els) = 0 := by
  match (pow_mod4 _ _ (P_mod4 j) M).2 with
  | ⟨f, hf⟩ =>
    have hh := half_of F.h f 1 (hq.trans hf)
    match (show ∃ s : Nat, F.h = 2 * s + 1 from by
        cases f with
        | ofNat s => exact ⟨s, Int.ofNat.inj (hh.trans (by show Int.ofNat (2 * s) + 1 = _; rfl))⟩
        | negSucc s => exfalso; exact Int.noConfusion (hh.trans (by rfl : 2 * Int.negSucc s + Int.ofNat 1 = Int.negSucc (2 * s)))) with
    | ⟨s, hs⟩ =>
      rw [CubeInert.odd_vanish F (CubeInert.chi_m1_odd F s hs)]; rfl

end InertAll

#print axioms InertAll.pow_even
#print axioms InertAll.pow_mod4
#print axioms InertAll.P_mod4
#print axioms InertAll.half_of
#print axioms InertAll.inert_even
#print axioms InertAll.inert_odd

lang/InertFq.lean

lean_all/lang/InertFq.lean

/-
  lang/InertFq — T43, cycle 105: y² = x³ − x at inert p ≡ 3 (mod 4) over 𝔽_p[√n] for any non-residue n.

    s1_inert        Σ_{𝔽_p} χ(x³ − x) = 0                              (QuarticFamily.phi_inert, χ(−1) = −1)
    inert_fq        −Σ_{𝔽_p[√n]} χ(z³ − z) = 0² − 2p                   (Fq2Trace: A primary, A² + B² = p²; p inert ⇒ {|A|, |B|} = {p, 0};
                                                                       B even ⇒ B = 0; A ≡ 1 (mod 4) ⇒ A = −p)
  The same 𝔽_{p²} model as the split case (HasseDavenport), so a(𝔽_{p²}) = a_p² − 2p is stated with one construction at
  every odd prime (next file).

  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.HasseDavenport
import lang.BaseChange

namespace InertFq

open FTAChain ZMod YMChain FiniteRecChain LibInt

theorem s1_inert (j : Nat) (hpr : ∀ d, Dv d (2 * (2 * j) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * j) + 1 + 1 + 1)) : sumK LibInt.intR (fun x => QChar.chi (2 * j) (CMInert.cub (ring (2 * (2 * j) + 1 + 1)) x)) (ConicSum.L (2 * j)) = 0 := by
  have hm1 := BaseChange.chi_negone j (TwoPower.negone_odd' (2 * (2 * j) + 1) j)
  have h := QuarticFamily.phi_inert (2 * j) hpr hm1 ((ring (2 * (2 * j) + 1 + 1)).neg (ring (2 * (2 * j) + 1 + 1)).one)
  refine Eq.trans ?_ h
  apply sumK_ext
  intro x
  show QChar.chi (2 * j) (CMInert.cub (ring (2 * (2 * j) + 1 + 1)) x) = QChar.chi (2 * j) ((ring (2 * (2 * j) + 1 + 1)).mul x ((ring (2 * (2 * j) + 1 + 1)).add ((ring (2 * (2 * j) + 1 + 1)).mul x x) ((ring (2 * (2 * j) + 1 + 1)).neg (ring (2 * (2 * j) + 1 + 1)).one)))
  rw [show CMInert.cub (ring (2 * (2 * j) + 1 + 1)) x = (ring (2 * (2 * j) + 1 + 1)).mul x ((ring (2 * (2 * j) + 1 + 1)).add ((ring (2 * (2 * j) + 1 + 1)).mul x x) ((ring (2 * (2 * j) + 1 + 1)).neg (ring (2 * (2 * j) + 1 + 1)).one)) by
    show (ring (2 * (2 * j) + 1 + 1)).add ((ring (2 * (2 * j) + 1 + 1)).mul ((ring (2 * (2 * j) + 1 + 1)).mul x x) x) ((ring (2 * (2 * j) + 1 + 1)).neg x) = _; lib_ring]

theorem odd_ne_even4 (j t : Int) (h : 4 * j + 3 - 1 = 4 * t) : False :=
  GaussInt.int_odd_ne_even j t (FinPrimary.two_cancel _ _ (by
    rw [show (2 : Int) * (2 * t) = 4 * t by lib_int, ← h]; show (2 : Int) * (2 * j + 1) = 4 * j + 3 - 1; lib_int))

/-- **Inert p ≡ 3 (mod 4): −S(𝔽_p[√n]) = S(𝔽_p)² − 2p for every non-residue n.** -/
theorem inert_fq (j : Nat) (hpr : ∀ d, Dv d (2 * (2 * j) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * j) + 1 + 1 + 1)) (n : Zp (2 * (2 * j) + 1 + 1))
    (hn : Frobenius.pw (ring (2 * (2 * j) + 1 + 1)) n (2 * j + 1) = (ring (2 * (2 * j) + 1 + 1)).neg (ring (2 * (2 * j) + 1 + 1)).one) :
    -sumK LibInt.intR (fun x => FinField.chi (Fq2Inst.ffq (2 * j) hpr n hn) (FinSign.f (Fq2Inst.ffq (2 * j) hpr n hn) x)) (Fq2Inst.ffq (2 * j) hpr n hn).els = (-sumK LibInt.intR (fun x => QChar.chi (2 * j) (CMInert.cub (ring (2 * (2 * j) + 1 + 1)) x)) (ConicSum.L (2 * j))) * (-sumK LibInt.intR (fun x => QChar.chi (2 * j) (CMInert.cub (ring (2 * (2 * j) + 1 + 1)) x)) (ConicSum.L (2 * j))) + -2 * In  -- (+39 chars; full line in the Zenodo source)
  rw [s1_inert j hpr]
  match Fq2Trace.fq_ap_primary (2 * j) hpr n hn with
  | ⟨A, B, hAB, ⟨⟨g, hg⟩, ⟨t, ht⟩⟩, hS2⟩ =>
    rw [hS2]
    have hP : Int.ofNat (2 * (2 * j) + 1 + 1 + 1) = 4 * Int.ofNat j + 3 := by show 2 * (2 * Int.ofNat j) + 1 + 1 + 1 = _; lib_int
    match CMModular.nat_sq A, CMModular.nat_sq B with
    | ⟨u, hu, hAu⟩, ⟨v, hv, hBv⟩ =>
      have huv : u * u + v * v = (2 * (2 * j) + 1 + 1 + 1) * (2 * (2 * j) + 1 + 1 + 1) := Int.ofNat.inj (by
        show Int.ofNat (u * u) + Int.ofNat (v * v) = Int.ofNat (2 * (2 * j) + 1 + 1 + 1) * Int.ofNat (2 * (2 * j) + 1 + 1 + 1)
        rw [← hu, ← hv]; exact hAB)
      cases Fp2Count.inert_norm j hpr u v huv with
      | inr h =>
        exfalso
        rw [h.2] at hBv
        cases hBv with
        | inl e =>
          exact GaussInt.int_odd_ne_even (2 * Int.ofNat j + 1) g (by rw [← hg, e, hP]; show (2 : Int) * (2 * Int.ofNat j + 1) + 1 = 4 * Int.ofNat j + 3; lib_int)
        | inr e =>
          exact GaussInt.int_odd_ne_even (2 * Int.ofNat j + 1) (-g) (by
            rw [show 2 * -g = -(2 * g) by lib_int, ← hg, e, hP]; show (2 : Int) * (2 * Int.ofNat j + 1) + 1 = -(-(4 * Int.ofNat j + 3)); lib_int)
      | inl h =>
        rw [h.1] at hAu; rw [h.2] at hBv
        have hB0 : B = 0 := by cases hBv with | inl e => exact e | inr e => rw [e]; rfl
        rw [hB0] at ht
        cases hAu with
        | inl e =>
          exfalso
          rw [e, hP] at ht
          exact odd_ne_even4 (Int.ofNat j) t (by rw [← ht]; show (4 : Int) * Int.ofNat j + 3 - 1 = 4 * Int.ofNat j + 3 + 0 - 1; lib_int)
        | inr e => rw [e]; lib_int

end InertFq

#print axioms InertFq.s1_inert
#print axioms InertFq.odd_ne_even4
#print axioms InertFq.inert_fq

lang/InertOdd3.lean

lean_all/lang/InertOdd3.lean

/-
  lang/InertOdd3 — T43, cycle 183: y² = x³ + 1 over every field with q ≡ 2 (mod 3) has a = 0; so at inert p ≡ 2 (mod 3)
  a(𝔽_{p^(2M+1)}) = 0 = α^N + β^N (α = i√p, N odd).

    cube_bij      3e = 4h + 1 ⇒ x ↦ x³ is a bijection with inverse x ↦ x^e
    psi_zero_q2   Σ_x χ(x³ + 1) = Σ_y χ(y + 1) = Σ_z χ(z) = 0
    inert_odd3    |F| = p^(2M+1), p = 6t + 5 ⇒ ψ_F(1) = 0

  Lean 4 core only, proofs by hand (lib_int, lib_ring), no axioms.
-/
import lang.InertPow3
import lang.InertAll

namespace InertOdd3

open FTAChain ZMod YMChain FiniteRecChain Frobenius GaussInt GaussFactor

variable {R : Type} [DecidableEq R] (F : FinField.FF R)

theorem cube_inv (e : Nat) (he : 3 * e = 4 * F.h + 1) (x : R) : pw F.K (pw F.K x 3) e = x := by
  rw [TwoPower.pwpow, he]
  by_cases hx : x = F.K.zero
  · rw [hx]; exact HasseCM.pw_zero_succ F.K _
  · rw [show 4 * F.h + 1 = 2 * F.h + 2 * F.h + 1 by lib_nat, HasseCM.pw_add', HasseCM.pw_add', FinCyclic.fermat_ff F x hx,
      F.K.one_mul, F.K.one_mul]
    exact F.K.one_mul x

theorem cube_inv' (e : Nat) (he : 3 * e = 4 * F.h + 1) (x : R) : pw F.K (pw F.K x e) 3 = x := by
  rw [TwoPower.pwpow, Nat.mul_comm, ← TwoPower.pwpow]; exact cube_inv F e he x

theorem psi_zero_q2 (e : Nat) (he : 3 * e = 4 * F.h + 1) : FinCubic.psi F F.K.one = 0 := by
  show sumK LibInt.intR (fun x => FinField.chi F (F.K.add (FinCubic.cube F x) F.K.one)) F.els = 0
  have hc : ∀ x, FinCubic.cube F x = pw F.K x 3 := fun x => by
    show F.K.mul x (F.K.mul x x) = F.K.mul (F.K.mul (F.K.mul F.K.one x) x) x; lib_ring
  rw [sumK_ext LibInt.intR _ (fun x => (fun y => FinField.chi F (F.K.add y F.K.one)) (pw F.K x 3)) (fun x => by rw [hc])]
  refine Eq.trans (reindex LibInt.intR F.els F.comp (fun x => pw F.K x 3) (fun x => pw F.K x e) (cube_inv F e he) (cube_inv' F e he)
    (fun y => FinField.chi F (F.K.add y F.K.one))) ?_
  refine Eq.trans (reindex LibInt.intR F.els F.comp (fun x => F.K.add x F.K.one) (fun x => F.K.add x (F.K.neg F.K.one))
    (fun x => by show F.K.add (F.K.add x F.K.one) (F.K.neg F.K.one) = x; lib_ring)
    (fun x => by show F.K.add (F.K.add x (F.K.neg F.K.one)) F.K.one = x; lib_ring) (FinField.chi F)) ?_
  exact FinField.chi_sum F

/-- **Odd degree at inert p ≡ 2 (mod 3): ψ_F(1) = 0.** -/
theorem inert_odd3 (t : Nat) (M : Nat) (hq : Int.ofNat (2 * F.h + 1) = ipow (Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1)) (2 * M + 1)) :
    FinCubic.psi F F.K.one = 0 := by
  match InertPow3.one_mod3_pow (Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) * Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1)) (Int.ofNat (12 * t * t + 20 * t + 8)) (by
      show Int.ofNat ((2 * (3 * t + 1) + 1 + 1 + 1) * (2 * (3 * t + 1) + 1 + 1 + 1)) = Int.ofNat (3 * (12 * t * t + 20 * t + 8) + 1)
      exact congrArg Int.ofNat (by lib_nat)) M with
  | ⟨f, hf⟩ =>
    rw [← InertPow3.ipow_two] at hf
    have h3 : Int.ofNat (4 * F.h + 1) = 3 * (6 * f * Int.ofNat (2 * t + 1) + 4 * f + 2 * Int.ofNat (2 * t + 1) + 1) := by
      have e1 : Int.ofNat (2 * F.h + 1) = (3 * f + 1) * Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) := hq.trans (by show ipow _ (2 * M) * _ = _; rw [hf])
      have e2 : Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) = 3 * Int.ofNat (2 * t + 1) + 2 := congrArg Int.ofNat (by lib_nat)
      have e3 : Int.ofNat (4 * F.h + 1) + 1 = 2 * Int.ofNat (2 * F.h + 1) :=
        congrArg Int.ofNat (show 4 * F.h + 1 + 1 = 2 * (2 * F.h + 1) by lib_nat)
      rw [show Int.ofNat (4 * F.h + 1) = (Int.ofNat (4 * F.h + 1) + 1) + -1 by lib_int, e3, e1, e2]; lib_int
    match EisenUnique.nat_of_three (4 * F.h + 1) _ h3 with
    | ⟨e, he⟩ => exact psi_zero_q2 F e (by rw [he]; lib_nat)

end InertOdd3

#print axioms InertOdd3.cube_inv
#print axioms InertOdd3.cube_inv'
#print axioms InertOdd3.psi_zero_q2
#print axioms InertOdd3.inert_odd3

lang/InertPow.lean

lean_all/lang/InertPow.lean

/-
  lang/InertPow — T43, cycle 178: y² = x³ − x over every field of order p^(2M) ∋ i at inert p ≡ 3 (mod 4): a = 2(−p)^M.

    inert_div      p | c² + d² ⇒ p | c and p | d        (−1 is not a square mod p: Euler, (p − 1)/2 odd)
    inert_desc     c² + d² = p^(2M) ⇒ c + di = p^M · unit
    inert_pow      over F with |F| = p^(2M), i ∈ F: −Σ χ(z³ − z) = 2(−p)^M      (FinPrimary: the primary unit is (−1)^M)
  α = i√p, β = −i√p: α^(2M) + β^(2M) = 2(−p)^M — the Hecke L-factor 1 + pT² at inert p governs every even-degree field.

  Lean 4 core only, proofs by hand (lib_int, lib_ring), no axioms.
-/
import lang.FinPow2

namespace InertPow

open FTAChain ZMod YMChain FiniteRecChain Frobenius GaussInt GaussFactor
open LibRing (castZ castN)

theorem inert_div (j : Nat) (hpr : ∀ d, Dv d (2 * (2 * j) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * j) + 1 + 1 + 1)) (c d n : Int)
    (h : c * c + d * d = Int.ofNat (2 * (2 * j) + 1 + 1 + 1) * n) : (∃ u, c = Int.ofNat (2 * (2 * j) + 1 + 1 + 1) * u) ∧ (∃ v, d = Int.ofNat (2 * (2 * j) + 1 + 1 + 1) * v) := by
  have h0 : (ring (2 * (2 * j) + 1 + 1)).add ((ring (2 * (2 * j) + 1 + 1)).mul (castZ (ring (2 * (2 * j) + 1 + 1)) c) (castZ (ring (2 * (2 * j) + 1 + 1)) c)) ((ring (2 * (2 * j) + 1 + 1)).mul (castZ (ring (2 * (2 * j) + 1 + 1)) d) (castZ (ring (2 * (2 * j) + 1 + 1)) d)) = (ring (2 * (2 * j) + 1 + 1)).zero := by
    rw [← HasseDavenport.castZ_mul, ← HasseDavenport.castZ_mul, ← IntCast.castZ_add, h, HasseDavenport.castZ_mul,
      CubeAp.castP, DetChain.zero_mul']
  generalize hCd : castZ (ring (2 * (2 * j) + 1 + 1)) c = C at h0
  generalize hDd : castZ (ring (2 * (2 * j) + 1 + 1)) d = D at h0
  have hCC : (ring (2 * (2 * j) + 1 + 1)).mul C C = (ring (2 * (2 * j) + 1 + 1)).mul ((ring (2 * (2 * j) + 1 + 1)).neg (ring (2 * (2 * j) + 1 + 1)).one) ((ring (2 * (2 * j) + 1 + 1)).mul D D) := by
    rw [DetChain.neg_mul_left, (ring (2 * (2 * j) + 1 + 1)).one_mul]
    exact DetChain.neg_unique (ring (2 * (2 * j) + 1 + 1)) _ _ h0
  have hD : D = (ring (2 * (2 * j) + 1 + 1)).zero := by
    by_cases hD : D = (ring (2 * (2 * j) + 1 + 1)).zero
    · exact hD
    · exfalso
      have e1 := Euler.euler_square (2 * j) hpr D hD
      by_cases hC : C = (ring (2 * (2 * j) + 1 + 1)).zero
      · apply hD
        have h1 : (ring (2 * (2 * j) + 1 + 1)).mul D D = (ring (2 * (2 * j) + 1 + 1)).zero := by
          have h2 := h0
          rw [hC, DetChain.mul_zero, (ring (2 * (2 * j) + 1 + 1)).zero_add] at h2
          exact h2
        cases ZMod.nozd (2 * (2 * j) + 1) hpr _ _ h1 with
        | inl h => exact h
        | inr h => exact h
      · have e2 := Euler.euler_square (2 * j) hpr C hC
        rw [hCC, Frobenius.pw_mul, e1, TwoPower.negone_odd' (2 * (2 * j) + 1) j, (ring (2 * (2 * j) + 1 + 1)).mul_comm, (ring (2 * (2 * j) + 1 + 1)).one_mul] at e2
        exact QChar.ne_pm (2 * j) e2
  have hC : C = (ring (2 * (2 * j) + 1 + 1)).zero := by
    rw [hD, DetChain.mul_zero, DetChain.mul_zero] at hCC
    cases ZMod.nozd (2 * (2 * j) + 1) hpr _ _ hCC with
    | inl h => exact h
    | inr h => exact h
  exact ⟨HasseDavenport.castZ_zero_dvd (2 * (2 * j) + 1 + 1) c (hCd.trans hC), HasseDavenport.castZ_zero_dvd (2 * (2 * j) + 1 + 1) d (hDd.trans hD)⟩

theorem inert_desc (j : Nat) (hpr : ∀ d, Dv d (2 * (2 * j) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * j) + 1 + 1 + 1)) : ∀ (M : Nat) (c d : Int),
    c * c + d * d = ipow (Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) (2 * M) →
    ∃ c0 d0 : Int, c = ipow (Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) M * c0 ∧ d = ipow (Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) M * d0 ∧ c0 * c0 + d0 * d0 = 1
  | 0, c, d, h => ⟨c, d, by show c = 1 * c; lib_int, by show d = 1 * d; lib_int, h⟩
  | M + 1, c, d, h => by
    have h1 : c * c + d * d = Int.ofNat (2 * (2 * j) + 1 + 1 + 1) * (ipow (Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) (2 * M) * Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) := by
      rw [h]; show ipow (Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) (2 * M) * Int.ofNat (2 * (2 * j) + 1 + 1 + 1) * Int.ofNat (2 * (2 * j) + 1 + 1 + 1) = _; lib_int
    match inert_div j hpr c d _ h1 with
    | ⟨⟨c1, hc1⟩, ⟨d1, hd1⟩⟩ =>
      have h2 : c1 * c1 + d1 * d1 = ipow (Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) (2 * M) := by
        apply FermatUnique.cancelP (2 * (2 * j) + 1)
        apply FermatUnique.cancelP (2 * (2 * j) + 1)
        rw [show Int.ofNat (2 * (2 * j) + 1 + 1 + 1) * (Int.ofNat (2 * (2 * j) + 1 + 1 + 1) * (c1 * c1 + d1 * d1)) = Int.ofNat (2 * (2 * j) + 1 + 1 + 1) * c1 * (Int.ofNat (2 * (2 * j) + 1 + 1 + 1) * c1) + Int.ofNat (2 * (2 * j) + 1 + 1 + 1) * d1 * (Int.ofNat (2 * (2 * j) + 1 + 1 + 1) * d1) by lib_int,
          ← hc1, ← hd1, h]
        show ipow (Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) (2 * M) * Int.ofNat (2 * (2 * j) + 1 + 1 + 1) * Int.ofNat (2 * (2 * j) + 1 + 1 + 1) = _; lib_int
      match inert_desc j hpr M c1 d1 h2 with
      | ⟨c0, d0, e1, e2, e3⟩ =>
        refine ⟨c0, d0, ?_, ?_, e3⟩
        · rw [hc1, e1]; show _ = ipow (Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) M * Int.ofNat (2 * (2 * j) + 1 + 1 + 1) * c0; lib_int
        · rw [hd1, e2]; show _ = ipow (Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) M * Int.ofNat (2 * (2 * j) + 1 + 1 + 1) * d0; lib_int

theorem odd_pow (P : Int) (e : Int) (hP : P = 2 * e + 1) : ∀ M, ∃ f, ipow P M = 2 * f + 1
  | 0 => ⟨0, rfl⟩
  | M + 1 => match odd_pow P e hP M with
    | ⟨f, hf⟩ => ⟨2 * f * e + f + e, by show ipow P M * P = _; rw [hf, hP]; lib_int⟩

theorem one_mod4_pow (Q : Int) (e : Int) (hQ : Q = 4 * e + 1) : ∀ M, ∃ f, ipow Q M = 4 * f + 1
  | 0 => ⟨0, rfl⟩
  | M + 1 => match one_mod4_pow Q e hQ M with
    | ⟨f, hf⟩ => ⟨4 * f * e + f + e, by show ipow Q M * Q = _; rw [hf, hQ]; lib_int⟩

theorem ipow_neg_sq (P : Int) : ∀ M, ipow (-P) M * ipow (-P) M = ipow P (2 * M)
  | 0 => rfl
  | M + 1 => by
    show ipow (-P) M * -P * (ipow (-P) M * -P) = ipow P (2 * M) * P * P
    rw [← ipow_neg_sq P M]; lib_int

variable {R : Type} [DecidableEq R]

/-- **Inert p ≡ 3 (mod 4): a(F) = 2(−p)^M over every F of order p^(2M) containing i.** -/
theorem inert_pow (j : Nat) (hpr : ∀ d, Dv d (2 * (2 * j) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * j) + 1 + 1 + 1)) (F : FinField.FF R) (M : Nat)
    (hq : Int.ofNat (2 * F.h + 1) = ipow (Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) (2 * M))
    (key : R → Nat) (hkey : ∀ x y, key x = key y → x = y) (i : R) (hi : F.K.mul i i = F.K.neg F.K.one) :
    -(sumK LibInt.intR (fun x => FinField.chi F (FinSign.f F x)) F.els) = 2 * ipow (-Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) M := by
  match FinPrimary.fin_ap_primary F key hkey i hi with
  | ⟨A, B, hAB, ⟨⟨e, he⟩, ⟨t, ht⟩⟩, hS⟩ =>
    rw [hS]
    have he' : B = 2 * e := he
    have ht' : A + B - 1 = 4 * t := ht
    match inert_desc j hpr M A B (hAB.trans hq), odd_pow (Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) (Int.ofNat (2 * j + 1)) (by
        show Int.ofNat (2 * (2 * j) + 1 + 1 + 1) = 2 * Int.ofNat (2 * j + 1) + 1; exact congrArg Int.ofNat (by lib_nat)) M with
    | ⟨c0, d0, e1, e2, e3⟩, ⟨f, hf⟩ =>
      have hu := norm_one ⟨c0, d0⟩ e3
      have hd0 : d0 = 0 := by
        rcases hu with h | h | h | h
        · exact congrArg GI.im h
        · exact congrArg GI.im h
        · exfalso
          have : d0 = 1 := congrArg GI.im h
          rw [this, hf, he'] at e2
          exact GaussInt.int_odd_ne_even f e (by rw [e2]; lib_int)
        · exfalso
          have : d0 = -1 := congrArg GI.im h
          rw [this, hf, he'] at e2
          exact GaussInt.int_odd_ne_even (-f - 1) e (by rw [e2]; lib_int)
      have hB : B = 0 := by rw [e2, hd0]; lib_int
      have hA2 : A * A = ipow (-Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) M * ipow (-Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) M := by
        rw [ipow_neg_sq, ← hq, ← hAB, hB]; lib_int
      match one_mod4_pow (-Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) (-Int.ofNat (j + 1)) (by
          show -Int.ofNat (2 * (2 * j) + 1 + 1 + 1) = 4 * -Int.ofNat (j + 1) + 1
          have h4 : Int.ofNat (2 * (2 * j) + 1 + 1 + 1) + 1 = 4 * Int.ofNat (j + 1) := congrArg Int.ofNat (show (2 * (2 * j) + 1 + 1 + 1) + 1 = 4 * (j + 1) by lib_nat)
          rw [show -Int.ofNat (2 * (2 * j) + 1 + 1 + 1) = -(Int.ofNat (2 * (2 * j) + 1 + 1 + 1) + 1) + 1 by lib_int, h4]; lib_int) M with
      | ⟨g, hg⟩ =>
        cases QuarticFamily.sq_eq A _ hA2 with
        | inl h => rw [h]
        | inr h =>
          exfalso
          rw [h, hg, hB] at ht'
          exact PrimaryUnique.four_bad (-g - t) (by rw [show 4 * (-g - t) = (-(4 * g + 1) + 0 - 1) - 4 * t + 2 by lib_int, ht']; lib_int)

end InertPow

#print axioms InertPow.inert_div
#print axioms InertPow.inert_desc
#print axioms InertPow.odd_pow
#print axioms InertPow.one_mod4_pow
#print axioms InertPow.ipow_neg_sq
#print axioms InertPow.inert_pow

lang/InertPow3.lean

lean_all/lang/InertPow3.lean

/-
  lang/InertPow3 — T43, cycle 180: y² = x³ + 1 over every field of order p^(2M) at inert p ≡ 2 (mod 3), p = 6t + 5:
  a = 2(−p)^M.

    not_three_dvd   3 ∤ p − 1
    inert_div3      p | U² + 3W² ⇒ p | U, W         (−3 is a non-square mod p: Eisen.inert_neg3)
    inert_desc3     U² + 3W² = p^(2M) ⇒ U = ±p^M, W = 0
    inert_pow3      ψ_F(1) = −2(−p)^M                (FinCubicExact; ψ ≡ 1 mod 3 and (−p)^M ≡ 1 mod 3 fix the sign)

  Lean 4 core only, proofs by hand (lib_int, lib_ring), no axioms.
-/
import lang.InertPow
import lang.FinPow3

namespace InertPow3

open FTAChain ZMod YMChain FiniteRecChain Frobenius GaussInt GaussFactor EisenFactor
open LibRing (castZ castN)

theorem not_three_dvd (t : Nat) (h : Dv (1 + 2) (2 * (3 * t + 1) + 1 + 1)) : False := by
  match h with
  | ⟨q, hq⟩ =>
    apply EisenAp.three_ne_r (Int.ofNat q - 2 * Int.ofNat t - 1) 1 (fun e => Nat.noConfusion e) (Nat.lt_succ_of_lt (Nat.lt_succ_self 1))
    have e : Int.ofNat (6 * t + 4) = Int.ofNat (q * 3) := congrArg Int.ofNat ((show 6 * t + 4 = 2 * (3 * t + 1) + 1 + 1 by lib_nat).trans hq)
    show _ = 1
    have e1 : 6 * Int.ofNat t + 4 = Int.ofNat q * 3 := e
    rw [show 3 * (Int.ofNat q - 2 * Int.ofNat t - 1) = Int.ofNat q * 3 - (6 * Int.ofNat t + 4) + 1 by lib_int, ← e1]; lib_int

theorem castThree {R : Type} (K : DetChain.CRing R) : castZ K 3 = K.add K.one (K.add K.one K.one) := by
  show K.add K.one (K.add K.one (K.add K.one K.zero)) = _
  rw [DetChain.add_zero']

theorem inert_div3 (t : Nat) (hpr : ∀ d, Dv d (2 * (3 * t + 1) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (3 * t + 1) + 1 + 1 + 1)) (c d n : Int)
    (h : c * c + 3 * (d * d) = Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) * n) : (∃ u, c = Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) * u) ∧ (∃ v, d = Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) * v) := by
  have h0 : (ring (2 * (3 * t + 1) + 1 + 1)).add ((ring (2 * (3 * t + 1) + 1 + 1)).mul (castZ (ring (2 * (3 * t + 1) + 1 + 1)) c) (castZ (ring (2 * (3 * t + 1) + 1 + 1)) c)) ((ring (2 * (3 * t + 1) + 1 + 1)).mul (Eisen.three (2 * (3 * t + 1) + 1)) ((ring (2 * (3 * t + 1) + 1 + 1)).mul (castZ (ring (2 * (3 * t + 1) + 1 + 1)) d) (castZ (ring (2 * (3 * t + 1) + 1 + 1)) d))) = (ring (2 * (3 * t + 1) +  -- (+19 chars; full line in the Zenodo source)
    rw [show Eisen.three (2 * (3 * t + 1) + 1) = castZ (ring (2 * (3 * t + 1) + 1 + 1)) 3 from (castThree (ring (2 * (3 * t + 1) + 1 + 1))).symm,
      ← HasseDavenport.castZ_mul, ← HasseDavenport.castZ_mul, ← HasseDavenport.castZ_mul, ← IntCast.castZ_add, h,
      HasseDavenport.castZ_mul, CubeAp.castP, DetChain.zero_mul']
  generalize hCd : castZ (ring (2 * (3 * t + 1) + 1 + 1)) c = C at h0
  generalize hDd : castZ (ring (2 * (3 * t + 1) + 1 + 1)) d = D at h0
  have hD : D = (ring (2 * (3 * t + 1) + 1 + 1)).zero := by
    by_cases hD : D = (ring (2 * (3 * t + 1) + 1 + 1)).zero
    · exact hD
    · exfalso
      have hinv := ZMod.inv_exists (2 * (3 * t + 1) + 1) hpr D hD
      generalize Frobenius.pw (ring (2 * (3 * t + 1) + 1 + 1)) D (2 * (3 * t + 1) + 1) = Di at hinv
      apply not_three_dvd t
      apply Eisen.inert_neg3 (2 * (3 * t + 1) + 1) hpr (Nat.succ_le_succ (Nat.succ_le_succ (Nat.zero_le _))) ((ring (2 * (3 * t + 1) + 1 + 1)).mul C Di)
      have hCC : (ring (2 * (3 * t + 1) + 1 + 1)).mul C C = (ring (2 * (3 * t + 1) + 1 + 1)).neg ((ring (2 * (3 * t + 1) + 1 + 1)).mul (Eisen.three (2 * (3 * t + 1) + 1)) ((ring (2 * (3 * t + 1) + 1 + 1)).mul D D)) := DetChain.neg_unique (ring (2 * (3 * t + 1) + 1 + 1)) _ _ h0
      rw [show (ring (2 * (3 * t + 1) + 1 + 1)).mul ((ring (2 * (3 * t + 1) + 1 + 1)).mul C Di) ((ring (2 * (3 * t + 1) + 1 + 1)).mul C Di) = (ring (2 * (3 * t + 1) + 1 + 1)).mul ((ring (2 * (3 * t + 1) + 1 + 1)).mul C C) ((ring (2 * (3 * t + 1) + 1 + 1)).mul Di Di) by lib_ring, hCC,
        show (ring (2 * (3 * t + 1) + 1 + 1)).mul ((ring (2 * (3 * t + 1) + 1 + 1)).neg ((ring (2 * (3 * t + 1) + 1 + 1)).mul (Eisen.three (2 * (3 * t + 1) + 1)) ((ring (2 * (3 * t + 1) + 1 + 1)).mul D D))) ((ring (2 * (3 * t + 1) + 1 + 1)).mul Di Di)
          = (ring (2 * (3 * t + 1) + 1 + 1)).neg ((ring (2 * (3 * t + 1) + 1 + 1)).mul (Eisen.three (2 * (3 * t + 1) + 1)) ((ring (2 * (3 * t + 1) + 1 + 1)).mul ((ring (2 * (3 * t + 1) + 1 + 1)).mul Di D) ((ring (2 * (3 * t + 1) + 1 + 1)).mul Di D))) by lib_ring, hinv]
      rw [show (ring (2 * (3 * t + 1) + 1 + 1)).mul (ring (2 * (3 * t + 1) + 1 + 1)).one (ring (2 * (3 * t + 1) + 1 + 1)).one = (ring (2 * (3 * t + 1) + 1 + 1)).one from (ring (2 * (3 * t + 1) + 1 + 1)).one_mul _, (ring (2 * (3 * t + 1) + 1 + 1)).mul_comm, (ring (2 * (3 * t + 1) + 1 + 1)).one_mul]
  have hC : C = (ring (2 * (3 * t + 1) + 1 + 1)).zero := by
    rw [hD, DetChain.mul_zero, DetChain.mul_zero, DetChain.add_zero'] at h0
    cases ZMod.nozd (2 * (3 * t + 1) + 1) hpr _ _ h0 with
    | inl h => exact h
    | inr h => exact h
  exact ⟨HasseDavenport.castZ_zero_dvd _ c (hCd.trans hC), HasseDavenport.castZ_zero_dvd _ d (hDd.trans hD)⟩

theorem inert_desc3 (t : Nat) (hpr : ∀ d, Dv d (2 * (3 * t + 1) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (3 * t + 1) + 1 + 1 + 1)) : ∀ (M : Nat) (c d : Int),
    c * c + 3 * (d * d) = ipow (Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1)) (2 * M) →
    ∃ c0 d0 : Int, c = ipow (Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1)) M * c0 ∧ d = ipow (Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1)) M * d0 ∧ c0 * c0 + 3 * (d0 * d0) = 1
  | 0, c, d, h => ⟨c, d, by show c = 1 * c; lib_int, by show d = 1 * d; lib_int, h⟩
  | M + 1, c, d, h => by
    have h1 : c * c + 3 * (d * d) = Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) * (ipow (Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1)) (2 * M) * Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1)) := by
      rw [h]; show ipow (Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1)) (2 * M) * Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) * Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) = _; lib_int
    match inert_div3 t hpr c d _ h1 with
    | ⟨⟨c1, hc1⟩, ⟨d1, hd1⟩⟩ =>
      have h2 : c1 * c1 + 3 * (d1 * d1) = ipow (Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1)) (2 * M) := by
        apply FermatUnique.cancelP (2 * (3 * t + 1) + 1)
        apply FermatUnique.cancelP (2 * (3 * t + 1) + 1)
        rw [show Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) * (Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) * (c1 * c1 + 3 * (d1 * d1))) = Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) * c1 * (Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) * c1) + 3 * (Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) * d1 * (Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) * d1)) by lib_int,
          ← hc1, ← hd1, h]
        show ipow (Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1)) (2 * M) * Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) * Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) = _; lib_int
      match inert_desc3 t hpr M c1 d1 h2 with
      | ⟨c0, d0, e1, e2, e3⟩ =>
        refine ⟨c0, d0, ?_, ?_, e3⟩
        · rw [hc1, e1]; show _ = ipow (Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1)) M * Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) * c0; lib_int
        · rw [hd1, e2]; show _ = ipow (Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1)) M * Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) * d0; lib_int

theorem one_mod3_pow (Q e : Int) (hQ : Q = 3 * e + 1) : ∀ M, ∃ f, ipow Q M = 3 * f + 1
  | 0 => ⟨0, rfl⟩
  | M + 1 => match one_mod3_pow Q e hQ M with
    | ⟨f, hf⟩ => ⟨3 * f * e + f + e, by show ipow Q M * Q = _; rw [hf, hQ]; lib_int⟩

theorem ipow_two (P : Int) : ∀ M, ipow P (2 * M) = ipow (P * P) M
  | 0 => rfl
  | M + 1 => by
    show ipow P (2 * M) * P * P = ipow (P * P) M * (P * P)
    rw [ipow_two P M]; lib_int

variable {R : Type} [DecidableEq R]

/-- **Inert p ≡ 2 (mod 3): a(F) = 2(−p)^M for y² = x³ + 1 over every F of order p^(2M).** -/
theorem inert_pow3 (t : Nat) (hpr : ∀ d, Dv d (2 * (3 * t + 1) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (3 * t + 1) + 1 + 1 + 1)) (F : FinField.FF R) (M : Nat)
    (hq : Int.ofNat (2 * F.h + 1) = ipow (Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1)) (2 * M)) :
    (FinCubic.psi F F.K.one) = -2 * ipow (-Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1)) M := by
  have hN : ∃ N, 2 * F.h = 3 * N := by
    match one_mod3_pow (Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) * Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1)) (Int.ofNat (12 * t * t + 20 * t + 8)) (by
        show Int.ofNat ((2 * (3 * t + 1) + 1 + 1 + 1) * (2 * (3 * t + 1) + 1 + 1 + 1)) = Int.ofNat (3 * (12 * t * t + 20 * t + 8) + 1)
        exact congrArg Int.ofNat (by lib_nat)) M with
    | ⟨f, hf⟩ =>
      rw [← ipow_two, ← hq] at hf
      have e : Int.ofNat (2 * F.h) = 3 * f := by
        have h1 : Int.ofNat (2 * F.h) + 1 = 3 * f + 1 := hf
        rw [show Int.ofNat (2 * F.h) = (Int.ofNat (2 * F.h) + 1) + -1 by lib_int, h1]; lib_int
      match EisenUnique.nat_of_three (2 * F.h) f e with
      | ⟨q, hq'⟩ => exact ⟨q, by rw [hq']; lib_nat⟩
  match hN with
  | ⟨N, hN⟩ =>
  match FinCubicExact.fin_cubic_trace F N hN, FinCubicTrace.psi_one_mod3_ff F N hN with
  | ⟨U, W, hUW, hpsi⟩, ⟨T, hT⟩ =>
    generalize FinCubic.psi F F.K.one = sF at hpsi hT ⊢
    have hZ : ∃ Z : Int, sF = -2 * Z ∧ Z * Z = Int.ofNat U * Int.ofNat U := by
      cases hpsi with
      | inl h => exact ⟨-Int.ofNat U, by rw [h]; show 2 * Int.ofNat U = _; lib_int, by lib_int⟩
      | inr h => exact ⟨Int.ofNat U, by rw [h]; show -(2 * Int.ofNat U) = _; lib_int, rfl⟩
    match hZ with
    | ⟨Z, hZs, hZZ⟩ =>
    have hZ1 : ∃ s, Z = 3 * s + 1 := ⟨Z + T, by
      have : -2 * Z = 1 + 3 * T := hZs.symm.trans hT
      rw [show 3 * (Z + T) + 1 = 3 * Z + (1 + 3 * T) by lib_int, ← this]; lib_int⟩
    have hnorm : Z * Z + 3 * (Int.ofNat W * Int.ofNat W) = ipow (Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1)) (2 * M) := by
      rw [hZZ, ← hq]; exact congrArg Int.ofNat hUW
    match inert_desc3 t hpr M Z (Int.ofNat W) hnorm with
    | ⟨c0, d0, e1, e2, e3⟩ =>
      have hd0 : d0 = 0 := by
        cases enorm_one ⟨c0, d0⟩ e3 with
        | inl h => exact congrArg GI.im h
        | inr h => exact congrArg GI.im h
      have hW : Int.ofNat W = 0 := by rw [e2, hd0]; lib_int
      have hZ2 : Z * Z = ipow (-Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1)) M * ipow (-Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1)) M := by
        rw [InertPow.ipow_neg_sq, ← hnorm, hW]; lib_int
      have hQ1 : ∃ f, ipow (-Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1)) M = 3 * f + 1 :=
        one_mod3_pow (-Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1)) (-Int.ofNat (2 * t + 2)) (by
          have h4 : Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) + 1 = 3 * Int.ofNat (2 * t + 2) := congrArg Int.ofNat (show (2 * (3 * t + 1) + 1 + 1 + 1) + 1 = 3 * (2 * t + 2) by lib_nat)
          rw [show -Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) = -(Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) + 1) + 1 by lib_int, h4]; lib_int) M
      rw [hZs, EisenPow.sign_ok Z _ hZ1 hQ1 (QuarticFamily.sq_eq Z _ hZ2)]

end InertPow3

#print axioms InertPow3.not_three_dvd
#print axioms InertPow3.castThree
#print axioms InertPow3.inert_div3
#print axioms InertPow3.inert_desc3
#print axioms InertPow3.one_mod3_pow
#print axioms InertPow3.ipow_two
#print axioms InertPow3.inert_pow3

lang/InertTower.lean

lean_all/lang/InertTower.lean

/-
  lang/InertTower — T43, cycle 179: y² = x³ − x over every level L ≥ 1 of the tower at inert p ≡ 3 (mod 4).

    inert_tower    level L + 1 has order p^(2M), M = |level L| exponent, and −Σ χ(z³ − z) = 2(−p)^M
  (TowerPow3.good3_tower for the order, TowerTrace.iE for i, InertPow.inert_pow.)

  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.InertPow
import lang.TowerPow3

namespace InertTower

open FTAChain ZMod YMChain FiniteRecChain Frobenius GaussInt GaussFactor FieldTower

/-- **Inert tower: a(level L + 1) = 2(−p)^M with |level L + 1| = p^(2M).** -/
theorem inert_tower (j : Nat) (hpr : ∀ d, Dv d (2 * (2 * j) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * j) + 1 + 1 + 1)) (L : Nat) :
    ∃ N : Nat, Int.ofNat (2 * (TowerPow.tower2 (2 * j) hpr (L + 1)).F.h + 1) = ipow (Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) (2 * (N + 1)) ∧
      -(sumK LibInt.intR (fun x => @FinField.chi _ (instDec (2 * j) (L + 1)) (TowerPow.tower2 (2 * j) hpr (L + 1)).F (@FinSign.f _ (instDec (2 * j) (L + 1)) (TowerPow.tower2 (2 * j) hpr (L + 1)).F x)) (TowerPow.tower2 (2 * j) hpr (L + 1)).F.els)
        = 2 * ipow (-Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) (N + 1) := by
  match TowerPow3.good3_tower (2 * j) hpr L with
  | ⟨_, _, ⟨N, hN⟩⟩ =>
    have hq : Int.ofNat (2 * (TowerPow.tower2 (2 * j) hpr (L + 1)).F.h + 1) = ipow (Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) (2 * (N + 1)) := by
      rw [show 2 * (N + 1) = (N + 1) + (N + 1) by lib_nat]
      exact @FinPow2.quad_size _ (instDec (2 * j) L) (TowerPow.tower2 (2 * j) hpr L).F (@TowerPow.pickNR _ (instDec (2 * j) L) (TowerPow.tower2 (2 * j) hpr L).F).1 (TowerPow.tower2 (2 * j) hpr L).j (TowerPow.tower2 (2 * j) hpr L).hj
        (@TowerPow.pickNR _ (instDec (2 * j) L) (TowerPow.tower2 (2 * j) hpr L).F).2.1 (@TowerPow.pickNR _ (instDec (2 * j) L) (TowerPow.tower2 (2 * j) hpr L).F).2.2 _ (N + 1) hN
    exact ⟨N, hq, @InertPow.inert_pow _ (instDec (2 * j) (L + 1)) j hpr (TowerPow.tower2 (2 * j) hpr (L + 1)).F (N + 1) hq (TowerTrace.keyT (2 * j) (L + 1))
      (TowerTrace.keyT_inj (2 * j) (L + 1))
      (@TowerTrace.iE _ (instDec (2 * j) L) (TowerPow.tower2 (2 * j) hpr L).F (@TowerPow.pickNR _ (instDec (2 * j) L) (TowerPow.tower2 (2 * j) hpr L).F).1)
      (@TowerTrace.iE_sq _ (instDec (2 * j) L) (TowerPow.tower2 (2 * j) hpr L).F (@TowerPow.pickNR _ (instDec (2 * j) L) (TowerPow.tower2 (2 * j) hpr L).F).1
        (@TowerPow.pickNR _ (instDec (2 * j) L) (TowerPow.tower2 (2 * j) hpr L).F).2.1 (@TowerPow.pickNR _ (instDec (2 * j) L) (TowerPow.tower2 (2 * j) hpr L).F).2.2)⟩

end InertTower

#print axioms InertTower.inert_tower

lang/Inf11mod12.lean

lean_all/lang/Inf11mod12.lean

/-
  lang/Inf11mod12 — T43, cycle 201: infinitely many primes p ≡ 11 (mod 12).

  N₀ = 12(F + 1)² − 1 ≡ 11 (mod 12), odd, prime to 3. For every prime factor q, (6(F + 1))² = 3N₀ + 3 ≡ 3, so 3 is a square and
  q ≡ ±1 (mod 12) (Sq3). A product of primes ≡ 1 is ≡ 1, so some factor is ≡ 11; with F + 1 = B! it exceeds B.
    uniq_rem12, mod12n, cofactor12, castZ3, allowed12, has11, inf_11mod12
  The class −1 mod 12 (split in ℚ(√3)).

  Lean 4 core only, proofs by hand (lib_nat, lib_int), no axioms.
-/
import lang.Sq3
import lang.Inf4mod5

namespace Inf11mod12

open FTAChain ZMod LibInt InfSplit
open LibRing (castZ castN)

theorem uniq_rem12 (x y r c : Nat) (hr : r < 12) (hc : c < 12) (h : x * 12 + r = y * 12 + c) : r = c := by
  have big : ∀ x y r c : Nat, r < 12 → x < y → x * 12 + r = y * 12 + c → False := fun x y r c hr hxy h => by
    have h1 : x * 12 + r < (x + 1) * 12 := by rw [Nat.succ_mul]; exact Nat.add_lt_add_left hr _
    have h2 : (x + 1) * 12 ≤ y * 12 := Nat.mul_le_mul_right 12 hxy
    exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (h ▸ h1) (Nat.le_trans h2 (Nat.le_add_right _ _)))
  cases Nat.lt_or_ge x y with
  | inl hxy => exact absurd h (fun h' => big x y r c hr hxy h')
  | inr hxy =>
    cases Nat.lt_or_ge y x with
    | inl hyx => exact absurd h.symm (fun h' => big y x c r hc hyx h')
    | inr hyx =>
      have e : x = y := Nat.le_antisymm hyx hxy
      subst e
      apply Int.ofNat.inj
      have := congrArg Int.ofNat h
      have e' : Int.ofNat x * 12 + Int.ofNat r = Int.ofNat x * 12 + Int.ofNat c := this
      calc Int.ofNat r = (Int.ofNat x * 12 + Int.ofNat r) - Int.ofNat x * 12 := by lib_int
        _ = Int.ofNat c := by rw [e']; lib_int

/-- residue of n mod 8 as a case split. -/
theorem mod12n (n : Nat) : ∃ a r, n = a * 12 + r ∧ r < 12 :=
  match div_exists 11 n with
  | ⟨a, r, k, hn, hr⟩ => ⟨a, r, hn, Nat.lt_of_lt_of_le (Nat.lt_succ_self r) (by rw [show (12 : Nat) = 11 + 1 from rfl, ← hr]; exact Nat.succ_le_succ (Nat.le_add_right r k))⟩

theorem cofactor12 (b N' a : Nat) (h : (b * 12 + 1) * N' = a * 12 + 11) : ∃ c, N' = c * 12 + 11 :=
  match mod12n N' with
  | ⟨c, r, hc, hr⟩ => by
    have e : (b * 12 + 1) * N' = (b * 12 * c + b * r + c) * 12 + r := by rw [hc]; lib_nat
    rw [e] at h
    exact ⟨c, by rw [hc, uniq_rem12 _ _ r 11 hr (Nat.lt_succ_self 11) h]⟩



theorem castZ3 (r : Nat) : castZ (ring (2 * r + 1 + 1)) 3 = Eisen.three (2 * r + 1) := by
  show (ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one (ring (2 * r + 1 + 1)).zero)) = _
  rw [DetChain.add_zero']; rfl

/-- Every prime factor of N₀ = 12(F + 1)² − 1 is ≡ 1 or 11 (mod 12). -/
theorem allowed12 (F l : Nat) (hq : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1)
    (hdv : Dv (l + 1 + 1) (12 * F * F + 24 * F + 11)) : ∃ b r, l + 1 + 1 = b * 12 + r ∧ (r = 1 ∨ r = 11) := by
  have hodd : ∀ c, l + 1 + 1 = 2 * c → False := fun c hc => by
    have h2 : l + 1 + 1 = 2 := by
      cases hq 2 ⟨c, by rw [hc]; lib_nat⟩ with
      | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
      | inr e => exact e.symm
    rw [h2] at hdv
    match hdv with
    | ⟨r, hr⟩ => exact InfSplit.odd_not_dv2 (6 * F * F + 12 * F + 5) r (by rw [← hr]; lib_nat)
  have h3 : l + 1 + 1 ≠ 3 := fun e => by
    rw [e] at hdv
    match hdv with
    | ⟨r, hr⟩ =>
      have hI : Int.ofNat r * 3 = 3 * (4 * Int.ofNat F * Int.ofNat F + 8 * Int.ofNat F + 3) + 2 :=
        (congrArg Int.ofNat hr).symm.trans (congrArg Int.ofNat (by lib_nat : (12 * F * F + 24 * F + 11) = 3 * (4 * F * F + 8 * F + 3) + 2))
      exact EisenAp.three_ne_r (Int.ofNat r - (4 * Int.ofNat F * Int.ofNat F + 8 * Int.ofNat F + 3)) 2 (fun e => Nat.noConfusion e) (Nat.lt_succ_self 2)
        (by show _ = 2; rw [show 3 * (Int.ofNat r - (4 * Int.ofNat F * Int.ofNat F + 8 * Int.ofNat F + 3)) = Int.ofNat r * 3 - 3 * (4 * Int.ofNat F * Int.ofNat F + 8 * Int.ofNat F + 3) by lib_int, hI]; lib_int)
  match EveryPrime.nat_even_odd l with
  | Or.inl ⟨c, hc⟩ => exact absurd (by rw [hc]; lib_nat : l + 1 + 1 = 2 * (c + 1)) (fun e => hodd (c + 1) e)
  | Or.inr ⟨r, hr⟩ =>
    subst hr
    have hu : (ring (2 * r + 1 + 1)).mul (castZ (ring (2 * r + 1 + 1)) (Int.ofNat (6 * (F + 1)))) (castZ (ring (2 * r + 1 + 1)) (Int.ofNat (6 * (F + 1)))) = Eisen.three (2 * r + 1) := by
      match hdv with
      | ⟨w, hw⟩ =>
        rw [← HasseDavenport.castZ_mul, show Int.ofNat (6 * (F + 1)) * Int.ofNat (6 * (F + 1)) = 3 * Int.ofNat (w * (2 * r + 1 + 1 + 1)) + 3 from by
          rw [← hw]; show Int.ofNat (6 * (F + 1) * (6 * (F + 1))) = Int.ofNat (3 * (12 * F * F + 24 * F + 11) + 3); exact congrArg Int.ofNat (by lib_nat),
          IntCast.castZ_add, HasseDavenport.castZ_mul, show Int.ofNat (w * (2 * r + 1 + 1 + 1)) = Int.ofNat w * Int.ofNat (2 * r + 1 + 1 + 1) from rfl,
          HasseDavenport.castZ_mul, CubeAp.castP, DetChain.mul_zero, DetChain.mul_zero, (ring (2 * r + 1 + 1)).zero_add, castZ3]
    match EveryPrime.nat_even_odd r with
    | Or.inl ⟨k, hk⟩ =>
      subst hk
      match Sq3.sq3_inert k hq h3 _ hu with
      | ⟨s, hs⟩ => exact ⟨s, 11, hs, Or.inr rfl⟩
    | Or.inr ⟨k, hk⟩ =>
      subst hk
      match Sq3.sq3_split k hq _ hu with
      | ⟨s, hs⟩ => exact ⟨s, 1, hs, Or.inl rfl⟩

/-- **A factor ≡ 11 (mod 12).** -/
theorem has11 (F : Nat) : ∀ G N, N ≤ G → Dv N (12 * F * F + 24 * F + 11) → (∃ a, N = a * 12 + 11) →
    ∃ q, (∀ d, Dv d q → d = 1 ∨ d = q) ∧ (∃ b, q = b * 12 + 11) ∧ Dv q N
  | 0, N, hN, _, ⟨a, ha⟩ => absurd (ha ▸ hN) (fun h => Nat.not_succ_le_zero _ (Nat.le_trans (Nat.le_add_left _ _) h))
  | G + 1, N, hN, hN0, ⟨a, ha⟩ => by
    match lp_exists (a * 12 + 9) with
    | ⟨l, hl, hmin⟩ =>
    have hq := lp_prime _ l hl hmin
    have hlN : Dv (l + 1 + 1) N := by rw [ha]; exact hl
    match allowed12 F l hq (dv_trans _ _ _ hlN hN0) with
    | ⟨b, r, hb, hr⟩ =>
      cases hr with
      | inr h4 => exact ⟨l + 1 + 1, hq, ⟨b, by rw [hb, h4]⟩, hlN⟩
      | inl h1 =>
        match hlN with
        | ⟨N', hN'⟩ =>
        match cofactor12 b N' a (by rw [← h1, ← hb, ← ha, hN']; lib_nat) with
        | ⟨c, hc⟩ =>
        have hlt : N' ≤ G := by
          have e : N = N' + (N' * (l + 1)) := by rw [hN']; lib_nat
          have h1' : 1 ≤ N' * (l + 1) := by rw [hc]; exact Nat.mul_le_mul (Nat.le_add_left 1 _) (Nat.le_add_left 1 _)
          exact Nat.le_of_succ_le_succ (Nat.le_trans (Nat.add_le_add_left h1' N') (by rw [← e]; exact hN))
        have hN'0 : Dv N' (12 * F * F + 24 * F + 11) := dv_trans _ _ _ ⟨l + 1 + 1, by rw [hN']; lib_nat⟩ hN0
        match has11 F G N' hlt hN'0 ⟨c, hc⟩ with
        | ⟨q, hq', hq4, hqN⟩ => exact ⟨q, hq', hq4, dv_trans q N' N hqN ⟨l + 1 + 1, by rw [hN']; lib_nat⟩⟩

/-- **Infinitely many primes p ≡ 11 (mod 12).** -/
theorem inf_11mod12 (B : Nat) : ∃ b, (∀ d, Dv d (b * 12 + 11) → d = 1 ∨ d = b * 12 + 11) ∧ B < b * 12 + 11 := by
  match fac_pos B with
  | ⟨F, hf⟩ =>
  match has11 F (12 * F * F + 24 * F + 11) (12 * F * F + 24 * F + 11) (Nat.le_refl _) ⟨1, by lib_nat⟩ ⟨F * F + 2 * F, by lib_nat⟩ with
  | ⟨q, hq, ⟨b, hb⟩, hqN⟩ =>
    refine ⟨b, by rw [← hb]; exact hq, ?_⟩
    rw [← hb]
    cases Nat.lt_or_ge B q with
    | inl h => exact h
    | inr h =>
      exfalso
      match dv_fac B q (by rw [hb]; exact Nat.le_add_left 1 _) h with
      | ⟨r, hr⟩ =>
        have h5 : Dv q ((12 * F * F + 24 * F + 11) + 1) := ⟨12 * r * (F + 1), by
          rw [show (12 * F * F + 24 * F + 11) + 1 = 12 * (F + 1) * (F + 1) by lib_nat, ← hf, hr]; lib_nat⟩
        have hq2 : q = (b * 12 + 10) + 1 := hb
        rw [hq2] at hqN h5
        exact dv_one_absurd (b * 12 + 9) (dv_sub (b * 12 + 10) _ 1 hqN h5)


end Inf11mod12

#print axioms Inf11mod12.uniq_rem12
#print axioms Inf11mod12.mod12n
#print axioms Inf11mod12.cofactor12
#print axioms Inf11mod12.castZ3
#print axioms Inf11mod12.allowed12
#print axioms Inf11mod12.has11
#print axioms Inf11mod12.inf_11mod12

lang/Inf11mod24.lean

lean_all/lang/Inf11mod24.lean

/-
  lang/Inf11mod24 — T43, cycle 213: infinitely many primes p ≡ 11 (mod 24).

  x = 100·m², m = Π_{k<B}(3k + 1)(3k + 2) (Inf17mod24.m3), P = x² + x − 1, D = 2x + 1 (D² = 4P + 5), M = 2(x² + x)² + 3 = D² + 2P²
  ≡ 11 (mod 24), M ≡ 3 (mod 5). Mod a prime factor q of M: P is invertible (q | P would give q | 5), −2 = (D/P)² and
  3 = −2(P + 1)² = (D(P + 1)/P)². So q ≡ 1 or 3 (mod 8) (negtwo_mod8, the body of Inf3mod8.allowed3 for any square root) and
  q ≡ ±1 (mod 12) (Sq3), hence q ≡ 1 or 11 (mod 24) (combine11), and M ≡ 11 forces a factor ≡ 11 (Inf17mod24.has_gen).
  q = 3k + 2 ≤ B divides m, hence x, hence x² + x, hence 3.
    dv5, negtwo_mod8, combine11, allowed11, xx_gen, m11, m5, bound11, x12_gen, xq_gen, r_gen, inf_11mod24
  The class 11 mod 24: split in ℚ(√−2, √3), inert in ℚ(i) and ℚ(√2).

  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Inf23mod24
import lang.Inf3mod8

namespace Inf11mod24

open FTAChain ZMod LibInt InfSplit Inf7mod8 Inf5mod8 TwoSupp
open Inf3mod8 (two_eq sq_rem rem_sq)

/-- the only divisor ≥ 2 of 5 is 5. -/
theorem dv5 (l : Nat) (h : Dv (l + 1 + 1) 5) : l + 1 + 1 = 5 := by
  match dv_le (l + 1 + 1) 4 h with
  | ⟨k, hk⟩ =>
    match l, h, hk with
    | 0, ⟨w, hw⟩, _ => exact absurd hw (fun e => odd_not_dv2 2 w e)
    | 1, ⟨w, hw⟩, _ =>
      exfalso
      exact Nat.noConfusion (Inf7mod24.uniq_rem3 1 w 2 0 (Nat.lt_succ_self 2) (Nat.zero_lt_succ 2)
        (by rw [show 1 * 3 + 2 = 5 from rfl, hw]; lib_nat))
    | 2, ⟨w, hw⟩, _ =>
      exfalso
      exact odd_not_dv2 2 (w * 2) (by rw [show 2 * 2 + 1 = 5 from rfl, hw]; lib_nat)
    | 3, _, _ => rfl
    | l + 4, _, hk =>
      exfalso
      have e : l + k + 1 + 5 = 0 + 5 := (show l + k + 1 + 5 = l + 4 + 1 + 1 + k by lib_nat).trans hk
      exact Nat.noConfusion (LibInt.nadd_right_cancel 5 e)

/-- −2 a square mod an odd prime q ⟹ q ≡ 1 or 3 (mod 8) (the body of Inf3mod8.allowed3 for any square root z). -/
theorem negtwo_mod8 (l : Nat) (hq : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1) (hodd : ∀ c, l + 1 + 1 = 2 * c → False)
    (z : Zp (l + 1)) (hx : (ring (l + 1)).mul z z = (ring (l + 1)).neg ((ring (l + 1)).add (ring (l + 1)).one (ring (l + 1)).one)) :
    ∃ b r, l + 1 + 1 = b * 8 + r ∧ (r = 1 ∨ r = 3) := by
  have hPr : Pr (l + 1 + 1) := ⟨⟨l, rfl⟩, hq⟩
  match mod8 (l + 1 + 1) with
  | ⟨b, r, hb, hr⟩ =>
    match r, hr with
    | 0, _ => exact absurd hb (fun e => hodd (4 * b) (by rw [e]; lib_nat))
    | 1, _ => exact ⟨b, 1, hb, Or.inl rfl⟩
    | 2, _ => exact absurd hb (fun e => hodd (4 * b + 1) (by rw [e]; lib_nat))
    | 3, _ => exact ⟨b, 3, hb, Or.inr rfl⟩
    | 4, _ => exact absurd hb (fun e => hodd (4 * b + 2) (by rw [e]; lib_nat))
    | 5, _ =>
      exfalso
      have hl : l = 2 * (2 * (2 * b) + 1) + 1 := Nat.succ.inj (Nat.succ.inj (hb.trans (by lib_nat)))
      subst hl
      match GaussSplit.split_one_mod_four (2 * b) hq with
      | ⟨i0, hi0⟩ =>
        have h2 : (ring (2 * (2 * (2 * b) + 1) + 1 + 1)).mul (mk _ ((ring (2 * (2 * (2 * b) + 1) + 1 + 1)).mul i0 z).val)
            (mk _ ((ring (2 * (2 * (2 * b) + 1) + 1 + 1)).mul i0 z).val) = mk _ 2 := by
          rw [mk_val, two_eq]
          rw [show (ring (2 * (2 * (2 * b) + 1) + 1 + 1)).mul ((ring (2 * (2 * (2 * b) + 1) + 1 + 1)).mul i0 z) ((ring (2 * (2 * (2 * b) + 1) + 1 + 1)).mul i0 z)
              = (ring (2 * (2 * (2 * b) + 1) + 1 + 1)).mul ((ring (2 * (2 * (2 * b) + 1) + 1 + 1)).mul i0 i0) ((ring (2 * (2 * (2 * b) + 1) + 1 + 1)).mul z z) by lib_ring,
            hi0, hx]
          lib_ring
        have e : (b + b + 1) + ((b + b + 1) + 0) + ((b + b + 1) + ((b + b + 1) + 0)) + 1 = 2 * (2 * (2 * b) + 1) + 1 + 1 + 1 := by lib_nat
        exact two_8m5 b (by rw [e]; exact hPr) ⟨_, by rw [e]; exact sq_rem _ _ h2⟩
    | 6, _ => exact absurd hb (fun e => hodd (4 * b + 3) (by rw [e]; lib_nat))
    | 7, _ =>
      exfalso
      have e : (b + b + 1) + ((b + b + 1) + 1) + ((b + b + 1) + ((b + b + 1) + 1)) + 1 = l + 1 + 1 := by rw [hb]; lib_nat
      match two_8m7 b (by rw [e]; exact hPr) with
      | ⟨w0, hw0⟩ =>
        rw [e] at hw0
        have hw := rem_sq l w0 hw0
        rw [two_eq] at hw
        have hwn : mk (l + 1) w0 ≠ (ring (l + 1)).zero := fun e0 => by
          rw [e0, DetChain.mul_zero] at hw
          have h2 : Dv (l + 1 + 1) (1 + 1) := md_zero_dv (l + 1) 2 (by
            have := congrArg Subtype.val hw.symm
            rw [← two_eq] at this; exact this)
          match dv_le (l + 1 + 1) 1 h2 with
          | ⟨k, hk⟩ =>
            have h0 : l + k = 0 := Nat.succ.inj (Nat.succ.inj ((show l + k + 1 + 1 = l + 1 + 1 + k by lib_nat).trans hk))
            exact hodd 1 (by rw [(HasseDavenport.nat_sum_zero _ _ h0).1])
        have hinv := ZMod.inv_exists l hq _ hwn
        have hl : l = 2 * (2 * (2 * b + 1)) + 1 := Nat.succ.inj (Nat.succ.inj (hb.trans (by lib_nat)))
        subst hl
        apply GaussSplit.inert_three_mod_four (2 * b + 1) hq
          ((ring (2 * (2 * (2 * b + 1)) + 1 + 1)).mul z (Frobenius.pw (ring (2 * (2 * (2 * b + 1)) + 1 + 1)) (mk _ w0) (2 * (2 * (2 * b + 1)) + 1)))
        generalize Frobenius.pw (ring (2 * (2 * (2 * b + 1)) + 1 + 1)) (mk _ w0) (2 * (2 * (2 * b + 1)) + 1) = v at hinv ⊢
        rw [show (ring (2 * (2 * (2 * b + 1)) + 1 + 1)).mul ((ring (2 * (2 * (2 * b + 1)) + 1 + 1)).mul z v) ((ring (2 * (2 * (2 * b + 1)) + 1 + 1)).mul z v)
            = (ring (2 * (2 * (2 * b + 1)) + 1 + 1)).mul ((ring (2 * (2 * (2 * b + 1)) + 1 + 1)).mul z z) ((ring (2 * (2 * (2 * b + 1)) + 1 + 1)).mul v v) by lib_ring,
          hx, ← hw]
        rw [show (ring (2 * (2 * (2 * b + 1)) + 1 + 1)).mul ((ring (2 * (2 * (2 * b + 1)) + 1 + 1)).neg ((ring (2 * (2 * (2 * b + 1)) + 1 + 1)).mul (mk _ w0) (mk _ w0))) ((ring (2 * (2 * (2 * b + 1)) + 1 + 1)).mul v v)
            = (ring (2 * (2 * (2 * b + 1)) + 1 + 1)).neg ((ring (2 * (2 * (2 * b + 1)) + 1 + 1)).mul ((ring (2 * (2 * (2 * b + 1)) + 1 + 1)).mul v (mk _ w0)) ((ring (2 * (2 * (2 * b + 1)) + 1 + 1)).mul v (mk _ w0))) by lib_ring,
          hinv, (ring (2 * (2 * (2 * b + 1)) + 1 + 1)).one_mul]
    | r + 8, hr => exact absurd hr (fun h => Nat.lt_irrefl 8 (Nat.lt_of_le_of_lt (Nat.le_add_left 8 r) h))


/-- q ≡ 1, 3 (mod 8) and q ≡ ±1 (mod 12) ⟹ q ≡ 1 or 11 (mod 24). -/
theorem combine11 (q b r s t : Nat) (h8 : q = b * 8 + r) (hr : r = 1 ∨ r = 3) (h12 : q = s * 12 + t) (ht : t = 1 ∨ t = 11) :
    ∃ e u, q = e * 24 + u ∧ (u = 1 ∨ u = 11) := by
  have hr8 : r < 8 := by
    cases hr with
    | inl h => rw [h]; exact Nat.le_of_ble_eq_true rfl
    | inr h => rw [h]; exact Nat.le_of_ble_eq_true rfl
  have bad : ∀ e c, c < 8 → q = e * 8 + c → r = c := fun e c hc h =>
    Inf7mod8.uniq_rem8 b e r c hr8 hc (h8.symm.trans h)
  cases ht with
  | inl h1 =>
    subst h1
    match EveryPrime.nat_even_odd s with
    | Or.inl ⟨e, he⟩ => exact ⟨e, 1, by rw [h12, he]; lib_nat, Or.inl rfl⟩
    | Or.inr ⟨e, he⟩ =>
      exfalso
      have h5 := bad (3 * e + 1) 5 (Nat.le_of_ble_eq_true rfl) (by rw [h12, he]; lib_nat)
      cases hr with
      | inl h => rw [h] at h5; exact Nat.noConfusion (Nat.succ.inj h5)
      | inr h => rw [h] at h5; exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj h5)))
  | inr h11 =>
    subst h11
    match EveryPrime.nat_even_odd s with
    | Or.inl ⟨e, he⟩ => exact ⟨e, 11, by rw [h12, he]; lib_nat, Or.inr rfl⟩
    | Or.inr ⟨e, he⟩ =>
      exfalso
      have h7 := bad (3 * e + 2) 7 (Nat.le_of_ble_eq_true rfl) (by rw [h12, he]; lib_nat)
      cases hr with
      | inl h => rw [h] at h7; exact Nat.noConfusion (Nat.succ.inj h7)
      | inr h => rw [h] at h7; exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj h7)))

/-- prime factors of M = 2(P + 1)² + 3 with D² = 4P + 5, M ≡ 11 (mod 24), 5 ∤ M are ≡ 1 or 11 (mod 24):
  D² + 2P² = M gives −2 = (D/P)², and 2(P + 1)² + 3 = M gives 3 = (D(P + 1)/P)². -/
theorem allowed11 (P D a c5 l : Nat) (hD : D * D = 4 * P + 5)
    (h11 : 2 * (P + 1) * (P + 1) + 3 = a * 24 + 11) (h5 : 2 * (P + 1) * (P + 1) + 3 = c5 * 5 + 3)
    (hq : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1) (hdv : Dv (l + 1 + 1) (2 * (P + 1) * (P + 1) + 3)) :
    ∃ c t, l + 1 + 1 = c * 24 + t ∧ (t = 1 ∨ t = 11) := by
  match Inf19mod24.odd_N l _ (a * 12 + 5) hq hdv (by rw [h11]; lib_nat) with
  | ⟨s, hs⟩ =>
  have hodd : ∀ c, l + 1 + 1 = 2 * c → False := fun c hc =>
    odd_not_dv2 (s + 1) c (by rw [show 2 * (s + 1) + 1 = l + 1 + 1 by rw [hs]; lib_nat, hc]; lib_nat)
  have h3 : l + 1 + 1 ≠ 3 := fun e => by
    rw [e] at hdv
    match hdv with
    | ⟨w, hw⟩ =>
      have := Inf7mod24.uniq_rem3 (a * 8 + 3) w 2 0 (Nat.lt_succ_self 2) (Nat.zero_lt_succ 2)
        (by rw [show (a * 8 + 3) * 3 + 2 = a * 24 + 11 by lib_nat, ← h11, hw]; lib_nat)
      exact Nat.noConfusion this
  have h5' : l + 1 + 1 ≠ 5 := fun e => by
    rw [e] at hdv
    match hdv with
    | ⟨w, hw⟩ =>
      have := Inf3mod8.uniq_rem 4 c5 w 3 0 (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)
        (by show c5 * 5 + 3 = w * 5 + 0; rw [← h5, hw]; lib_nat)
      exact Nat.noConfusion this
  have hP0 : mk (l + 1) P ≠ (ring (l + 1)).zero := fun e => by
    have hqP : Dv (l + 1 + 1) P := md_zero_dv (l + 1) P (congrArg Subtype.val e)
    have hq5 : Dv (l + 1 + 1) 5 := dv_sub (l + 1) _ 5 (Inf17mod24.dv_mulr _ _ (2 * P + 4) hqP)
      (by rw [show P * (2 * P + 4) + 5 = 2 * (P + 1) * (P + 1) + 3 by lib_nat]; exact hdv)
    exact h5' (dv5 l hq5)
  have hv := ZMod.inv_exists l hq _ hP0
  generalize Frobenius.pw (ring (l + 1)) (mk (l + 1) P) l = v at hv
  have hM : (ring (l + 1)).add ((ring (l + 1)).mul (mk (l + 1) D) (mk (l + 1) D)) ((ring (l + 1)).mul (mk (l + 1) 2) ((ring (l + 1)).mul (mk (l + 1) P) (mk (l + 1) P))) = (ring (l + 1)).zero := by
    rw [TwoPower.mk_mul, TwoPower.mk_mul, TwoPower.mk_mul, PrimeOneModQ.mk_add]
    exact Subtype.ext (dv_md_zero (l + 1) _ (by rw [show D * D + 2 * (P * P) = 2 * (P + 1) * (P + 1) + 3 by rw [hD]; lib_nat]; exact hdv))
  have hR : (ring (l + 1)).add (mk (l + 1) 3) ((ring (l + 1)).mul (mk (l + 1) 2) ((ring (l + 1)).mul (mk (l + 1) (P + 1)) (mk (l + 1) (P + 1)))) = (ring (l + 1)).zero := by
    rw [TwoPower.mk_mul, TwoPower.mk_mul, PrimeOneModQ.mk_add]
    exact Subtype.ext (dv_md_zero (l + 1) _ (by rw [show 3 + 2 * ((P + 1) * (P + 1)) = 2 * (P + 1) * (P + 1) + 3 by lib_nat]; exact hdv))
  have hDD := DetChain.neg_unique (ring (l + 1)) _ _ hM
  have hz : (ring (l + 1)).mul ((ring (l + 1)).mul (mk (l + 1) D) v) ((ring (l + 1)).mul (mk (l + 1) D) v) = (ring (l + 1)).neg ((ring (l + 1)).add (ring (l + 1)).one (ring (l + 1)).one) := by
    rw [show (ring (l + 1)).mul ((ring (l + 1)).mul (mk (l + 1) D) v) ((ring (l + 1)).mul (mk (l + 1) D) v) = (ring (l + 1)).mul ((ring (l + 1)).mul (mk (l + 1) D) (mk (l + 1) D)) ((ring (l + 1)).mul v v) by lib_ring, hDD,
      show (ring (l + 1)).mul ((ring (l + 1)).neg ((ring (l + 1)).mul (mk (l + 1) 2) ((ring (l + 1)).mul (mk (l + 1) P) (mk (l + 1) P)))) ((ring (l + 1)).mul v v)
        = (ring (l + 1)).neg ((ring (l + 1)).mul (mk (l + 1) 2) ((ring (l + 1)).mul ((ring (l + 1)).mul v (mk (l + 1) P)) ((ring (l + 1)).mul v (mk (l + 1) P)))) by lib_ring, hv, Inf3mod8.two_eq]
    lib_ring
  have h3' := DetChain.neg_unique (ring (l + 1)) _ _ hR
  have hu : (ring (l + 1)).mul ((ring (l + 1)).mul ((ring (l + 1)).mul (mk (l + 1) D) v) (mk (l + 1) (P + 1))) ((ring (l + 1)).mul ((ring (l + 1)).mul (mk (l + 1) D) v) (mk (l + 1) (P + 1))) = Eisen.three l := by
    rw [show (ring (l + 1)).mul ((ring (l + 1)).mul ((ring (l + 1)).mul (mk (l + 1) D) v) (mk (l + 1) (P + 1))) ((ring (l + 1)).mul ((ring (l + 1)).mul (mk (l + 1) D) v) (mk (l + 1) (P + 1)))
        = (ring (l + 1)).mul ((ring (l + 1)).mul ((ring (l + 1)).mul (mk (l + 1) D) v) ((ring (l + 1)).mul (mk (l + 1) D) v)) ((ring (l + 1)).mul (mk (l + 1) (P + 1)) (mk (l + 1) (P + 1))) by lib_ring, hz,
      show (ring (l + 1)).mul ((ring (l + 1)).neg ((ring (l + 1)).add (ring (l + 1)).one (ring (l + 1)).one)) ((ring (l + 1)).mul (mk (l + 1) (P + 1)) (mk (l + 1) (P + 1)))
        = (ring (l + 1)).neg ((ring (l + 1)).mul ((ring (l + 1)).add (ring (l + 1)).one (ring (l + 1)).one) ((ring (l + 1)).mul (mk (l + 1) (P + 1)) (mk (l + 1) (P + 1)))) by lib_ring, ← Inf3mod8.two_eq, ← h3']
    show mk (l + 1) (1 + 2) = _
    rw [← PrimeOneModQ.mk_add (l + 1) 1 2, Inf3mod8.two_eq]; rfl
  match negtwo_mod8 l hq hodd _ hz with
  | ⟨b, r, hb, hr⟩ =>
  have h12 : ∃ s' t, l + 1 + 1 = s' * 12 + t ∧ (t = 1 ∨ t = 11) := by
    subst hs
    match EveryPrime.nat_even_odd s with
    | Or.inl ⟨k, hk⟩ =>
      subst hk
      match Sq3.sq3_inert k hq h3 _ hu with
      | ⟨s', hs'⟩ => exact ⟨s', 11, hs', Or.inr rfl⟩
    | Or.inr ⟨k, hk⟩ =>
      subst hk
      match Sq3.sq3_split k hq _ hu with
      | ⟨s', hs'⟩ => exact ⟨s', 1, hs', Or.inl rfl⟩
  match h12 with
  | ⟨s', t, hs', ht⟩ => exact combine11 _ b r s' t hb hr hs' ht

/-- (Kt + 4)² + (Kt + 4) = K(Kt² + 9t) + 20 (K an atom; K = 12 below). -/
theorem xx_gen (K t : Nat) : (K * t + 4) * (K * t + 4) + (K * t + 4) = K * (K * t * t + 9 * t) + 20 := by lib_nat

/-- S = 3U + 2 ⟹ 2(4S)² + 3 ≡ 11 (mod 24). -/
theorem m11 (S U : Nat) (h : S = 3 * U + 2) : 2 * (4 * S) * (4 * S) + 3 = (4 * (3 * U * U + 4 * U + 1) + 1) * 24 + 11 := by
  have e1 : 2 * (4 * S) * (4 * S) + 3 = 32 * (S * S) + 3 := by lib_nat
  have e2 : S * S = 3 * (3 * U * U + 4 * U + 1) + 1 := by rw [h]; lib_nat
  rw [e1, e2]
  generalize 3 * U * U + 4 * U + 1 = V
  lib_nat

/-- 5 | x ⟹ 2(x² + x)² + 3 ≡ 3 (mod 5). -/
theorem m5 (x Z P : Nat) (hP : P + 1 = x * x + x) (hx : x = 5 * Z) :
    2 * (P + 1) * (P + 1) + 3 = (10 * ((5 * Z * Z + Z) * (5 * Z * Z + Z))) * 5 + 3 := by
  have e : P + 1 = 5 * (5 * Z * Z + Z) := by rw [hP, hx]; lib_nat
  rw [e]
  generalize 5 * Z * Z + Z = W
  lib_nat

/-- q | R ⟹ q ∤ 2R² + 3 for q = 24c + 11. -/
theorem bound11 (R r c : Nat) (hR : R = r * (c * 24 + 9 + 1 + 1)) (hM : Dv (c * 24 + 9 + 1 + 1) (2 * R * R + 3)) : False := by
  have h1 : Dv (c * 24 + 9 + 1 + 1) (2 * R * R) := ⟨2 * r * R, by
    have e : 2 * R * R = 2 * (r * (c * 24 + 9 + 1 + 1)) * R := congrArg (fun z => 2 * z * R) hR
    rw [e]; lib_nat⟩
  have h3 := dv_sub (c * 24 + 9 + 1) _ 3 h1 hM
  match dv_le _ 2 h3 with
  | ⟨k, hk⟩ =>
    have e : c * 24 + k + 7 + 1 = 0 :=
      Nat.succ.inj (Nat.succ.inj (Nat.succ.inj ((show c * 24 + k + 7 + 1 + 1 + 1 + 1 = c * 24 + 9 + 1 + 1 + k by lib_nat).trans hk)))
    exact Nat.noConfusion e

theorem x12_gen (C : Nat) : 100 * (3 * C + 1) = 12 * (25 * C + 8) + 4 := by lib_nat

theorem xq_gen (m r Q x : Nat) (hX : 100 * (m * m) = x) (hr : m = r * Q) : x = (100 * r * m) * Q := by
  rw [← hX]
  have e : 100 * (m * m) = 100 * (r * Q * m) := congrArg (fun z => 100 * (z * m)) hr
  rw [e]; lib_nat

theorem r_gen (P x a Q : Nat) (hP : P + 1 = x * x + x) (hx : x = a * Q) : P + 1 = (a * (x + 1)) * Q := by
  rw [hP, show x * x + x = x * (x + 1) by lib_nat]
  have e : x * (x + 1) = a * Q * (x + 1) := congrArg (fun z => z * (x + 1)) hx
  rw [e]; lib_nat

/-- **Infinitely many primes p ≡ 11 (mod 24).** -/
theorem inf_11mod24 (B : Nat) : ∃ c, (∀ d, Dv d (c * 24 + 11) → d = 1 ∨ d = c * 24 + 11) ∧ B < c * 24 + 11 := by
  match Inf17mod24.m3_sq B with
  | ⟨C, hC⟩ =>
  have hx12 : 100 * (Inf17mod24.m3 B * Inf17mod24.m3 B) = 12 * (25 * C + 8) + 4 := by rw [hC]; exact x12_gen C
  have hx5 : 100 * (Inf17mod24.m3 B * Inf17mod24.m3 B) = 5 * (20 * (Inf17mod24.m3 B * Inf17mod24.m3 B)) := by lib_nat
  generalize hX : 100 * (Inf17mod24.m3 B * Inf17mod24.m3 B) = x at hx12 hx5
  generalize 25 * C + 8 = t at hx12
  generalize 20 * (Inf17mod24.m3 B * Inf17mod24.m3 B) = Z at hx5
  have hS : x * x + x = 4 * (3 * (12 * t * t + 9 * t + 1) + 2) := by
    rw [hx12, xx_gen]
    generalize 12 * t * t + 9 * t = A
    lib_nat
  generalize 12 * t * t + 9 * t + 1 = U at hS
  have hP : 4 * (3 * U + 1) + 3 + 1 = x * x + x := by rw [hS]; lib_nat
  generalize hPd : 4 * (3 * U + 1) + 3 = P at hP
  have hD : (2 * x + 1) * (2 * x + 1) = 4 * P + 5 := by
    rw [show (2 * x + 1) * (2 * x + 1) = 4 * (x * x + x) + 1 by lib_nat, ← hP]; lib_nat
  have h11 : 2 * (P + 1) * (P + 1) + 3 = (4 * (3 * U * U + 4 * U + 1) + 1) * 24 + 11 := by
    rw [show P + 1 = 4 * (3 * U + 2) by rw [← hPd]; lib_nat]
    exact m11 _ U rfl
  have h5 := m5 x Z P hP hx5
  match Inf17mod24.has_gen 9 (2 * (P + 1) * (P + 1) + 3) (Nat.le_of_ble_eq_true rfl)
      (fun l hq hdv => allowed11 P (2 * x + 1) _ _ l hD h11 h5 hq hdv)
      _ _ (Nat.le_refl _) ⟨1, (Nat.one_mul _).symm⟩ ⟨_, h11⟩ with
  | ⟨q, hq, ⟨c, hc⟩, hqN⟩ =>
    have hq' : q = c * 24 + 9 + 1 + 1 := by rw [hc]
    refine ⟨c, by rw [← hc]; exact hq, ?_⟩
    cases Nat.lt_or_ge B (c * 24 + 11) with
    | inl h => exact h
    | inr h =>
      exfalso
      have hk : 8 * c + 3 < B := Nat.lt_of_lt_of_le
        (Nat.lt_of_lt_of_le (Nat.lt_succ_self _) (by rw [show c * 24 + 11 = (8 * c + 3 + 1) + (16 * c + 7) by lib_nat]; exact Nat.le_add_right _ _)) h
      match Inf17mod24.m3_dv B (8 * c + 3) hk with
      | ⟨r, hr⟩ =>
        have hr' : Inf17mod24.m3 B = r * (c * 24 + 9 + 1 + 1) := by
          rw [hr, show 3 * (8 * c + 3) + 2 = c * 24 + 9 + 1 + 1 by lib_nat]
        have hxq := xq_gen _ r _ x hX hr'
        have hR := r_gen P x _ _ hP hxq
        rw [hq'] at hqN
        exact bound11 (P + 1) _ c hR hqN

end Inf11mod24

#print axioms Inf11mod24.dv5
#print axioms Inf11mod24.negtwo_mod8
#print axioms Inf11mod24.combine11
#print axioms Inf11mod24.allowed11
#print axioms Inf11mod24.xx_gen
#print axioms Inf11mod24.m11
#print axioms Inf11mod24.m5
#print axioms Inf11mod24.bound11
#print axioms Inf11mod24.x12_gen
#print axioms Inf11mod24.xq_gen
#print axioms Inf11mod24.r_gen
#print axioms Inf11mod24.inf_11mod24

lang/Inf13mod24.lean

lean_all/lang/Inf13mod24.lean

/-
  lang/Inf13mod24 — T43, cycle 206: infinitely many primes p ≡ 13 (mod 24).

  a = 6y, y = odd factorial of B: Φ₁₂(a) = a⁴ − a² + 1 ≡ 13 (mod 24). Its prime factors are ≡ 1 (mod 12) (Inf1mod12.allowed1),
  i.e. ≡ 1 or 13 (mod 24); the product is ≡ 13, so some factor is ≡ 13; an odd q ≤ B divides y, hence a, hence 1.
    uniq_rem24, mod24n, cofactor24, allowed13, has13, prod_gen, shift_gen, phi_gen, sq_gen, sq_mod24, dv_lead, inf_13mod24

  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.Inf1mod12

namespace Inf13mod24

open FTAChain ZMod LibInt InfSplit

theorem uniq_rem24 (x y r c : Nat) (hr : r < 24) (hc : c < 24) (h : x * 24 + r = y * 24 + c) : r = c := by
  have big : ∀ x y r c : Nat, r < 24 → x < y → x * 24 + r = y * 24 + c → False := fun x y r c hr hxy h => by
    have h1 : x * 24 + r < (x + 1) * 24 := by rw [Nat.succ_mul]; exact Nat.add_lt_add_left hr _
    have h2 : (x + 1) * 24 ≤ y * 24 := Nat.mul_le_mul_right 24 hxy
    exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (h ▸ h1) (Nat.le_trans h2 (Nat.le_add_right _ _)))
  cases Nat.lt_or_ge x y with
  | inl hxy => exact absurd h (fun h' => big x y r c hr hxy h')
  | inr hxy =>
    cases Nat.lt_or_ge y x with
    | inl hyx => exact absurd h.symm (fun h' => big y x c r hc hyx h')
    | inr hyx =>
      have e : x = y := Nat.le_antisymm hyx hxy
      subst e
      apply Int.ofNat.inj
      have := congrArg Int.ofNat h
      have e' : Int.ofNat x * 24 + Int.ofNat r = Int.ofNat x * 24 + Int.ofNat c := this
      calc Int.ofNat r = (Int.ofNat x * 24 + Int.ofNat r) - Int.ofNat x * 24 := by lib_int
        _ = Int.ofNat c := by rw [e']; lib_int

/-- residue of n mod 24 as a case split. -/
theorem mod24n (n : Nat) : ∃ a r, n = a * 24 + r ∧ r < 24 :=
  match div_exists 23 n with
  | ⟨a, r, k, hn, hr⟩ => ⟨a, r, hn, Nat.lt_of_lt_of_le (Nat.lt_succ_self r) (by rw [show (24 : Nat) = 23 + 1 from rfl, ← hr]; exact Nat.succ_le_succ (Nat.le_add_right r k))⟩

theorem cofactor24 (b N' a : Nat) (h : (b * 24 + 1) * N' = a * 24 + 13) : ∃ c, N' = c * 24 + 13 :=
  match mod24n N' with
  | ⟨c, r, hc, hr⟩ => by
    have e : (b * 24 + 1) * N' = (b * 24 * c + b * r + c) * 24 + r := by rw [hc]; lib_nat
    rw [e] at h
    exact ⟨c, by rw [hc, uniq_rem24 _ _ r 13 hr (Nat.lt_of_lt_of_le (Nat.lt_succ_self 13) (Nat.le_add_right 14 10)) h]⟩

/-- prime factors of Φ₁₂(a) = a(aM) + 1 (a² = M + 1, a even) are ≡ 1 or 13 (mod 24). -/
theorem allowed13 (a M b l : Nat) (hM : a * a = M + 1) (hb : a = 2 * b) (hq : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1)
    (hdv : Dv (l + 1 + 1) (a * (a * M) + 1)) : ∃ c t, l + 1 + 1 = c * 24 + t ∧ (t = 1 ∨ t = 13) := by
  match Inf1mod12.allowed1 a M b l hM hb hq hdv with
  | ⟨s, hs⟩ =>
    match EveryPrime.nat_even_odd s with
    | Or.inl ⟨c, hc⟩ => exact ⟨c, 1, by rw [hs, hc]; lib_nat, Or.inl rfl⟩
    | Or.inr ⟨c, hc⟩ => exact ⟨c, 13, by rw [hs, hc]; lib_nat, Or.inr rfl⟩

theorem has13 (a M b : Nat) (hM : a * a = M + 1) (hb : a = 2 * b) : ∀ G N, N ≤ G → Dv N (a * (a * M) + 1) → (∃ e, N = e * 24 + 13) →
    ∃ q, (∀ d, Dv d q → d = 1 ∨ d = q) ∧ (∃ c, q = c * 24 + 13) ∧ Dv q N
  | 0, N, hN, _, ⟨e, he⟩ => absurd (he ▸ hN) (fun h => Nat.not_succ_le_zero _ (Nat.le_trans (Nat.le_add_left _ _) h))
  | G + 1, N, hN, hN0, ⟨e, he⟩ => by
    match lp_exists (e * 24 + 11) with
    | ⟨l, hl, hmin⟩ =>
    have hq := lp_prime _ l hl hmin
    have hlN : Dv (l + 1 + 1) N := by rw [he]; exact hl
    match allowed13 a M b l hM hb hq (dv_trans _ _ _ hlN hN0) with
    | ⟨c, t, hc, ht⟩ =>
      cases ht with
      | inr h13 => exact ⟨l + 1 + 1, hq, ⟨c, by rw [hc, h13]⟩, hlN⟩
      | inl h1 =>
        match hlN with
        | ⟨N', hN'⟩ =>
        match cofactor24 c N' e (by rw [← h1, ← hc, ← he, hN']; lib_nat) with
        | ⟨d, hd⟩ =>
        have hlt : N' ≤ G := by
          have e' : N = N' + (N' * (l + 1)) := by rw [hN']; lib_nat
          have h1' : 1 ≤ N' * (l + 1) := by rw [hd]; exact Nat.mul_le_mul (Nat.le_add_left 1 _) (Nat.le_add_left 1 _)
          exact Nat.le_of_succ_le_succ (Nat.le_trans (Nat.add_le_add_left h1' N') (by rw [← e']; exact hN))
        have hN'0 : Dv N' (a * (a * M) + 1) := dv_trans _ _ _ ⟨l + 1 + 1, by rw [hN']; lib_nat⟩ hN0
        match has13 a M b hM hb G N' hlt hN'0 ⟨d, hd⟩ with
        | ⟨q, hq', hq13, hqN⟩ => exact ⟨q, hq', hq13, dv_trans q N' N hqN ⟨l + 1 + 1, by rw [hN']; lib_nat⟩⟩

/-- (AK + T + 1)(AK + T) + 1 with K, T atoms. -/
theorem prod_gen (A K T : Nat) :
    (A * K + (T + 1)) * (A * K + T) + 1 = (A * K * A + A * T + A * (T + 1)) * K + (T * (T + 1) + 1) := by lib_nat

theorem shift_gen (X K c d : Nat) : X * K + (c * K + d) = (X + c) * K + d := by lib_nat

/-- a² = 24A + 12 ⟹ Φ₁₂(a) = a(a(a² − 1)) + 1 ≡ 13 (mod 24). -/
theorem phi_gen (a A : Nat) (ha : a * a = A * 24 + 12) :
    a * (a * (A * 24 + 11)) + 1 = (A * 24 * A + A * 11 + A * (11 + 1) + 5) * 24 + 13 := by
  have e : a * (a * (A * 24 + 11)) + 1 = (a * a) * (A * 24 + 11) + 1 := by lib_nat
  exact e.trans ((congrArg (fun x => x * (A * 24 + 11) + 1) ha).trans ((prod_gen A 24 11).trans (shift_gen _ 24 5 13)))

/-- H² = 6H ⟹ (H(2u + 1))² = (6(u² + u) + 1)(4H) + 2H. -/
theorem sq_gen (H u : Nat) (hH : H * H = 6 * H) :
    (H * (2 * u + 1)) * (H * (2 * u + 1)) = (6 * (u * u + u) + 1) * (4 * H) + 2 * H := by
  rw [show (H * (2 * u + 1)) * (H * (2 * u + 1)) = (H * H) * (4 * (u * u + u) + 1) by lib_nat, hH]; lib_nat

/-- a = 6(2u + 1): a² = 24(6(u² + u) + 1) + 12. -/
theorem sq_mod24 (u : Nat) : (6 * (2 * u + 1)) * (6 * (2 * u + 1)) = (6 * (u * u + u) + 1) * 24 + 12 :=
  sq_gen 6 u rfl

theorem dv_lead (q r X : Nat) : Dv q ((6 * (r * q)) * X) := ⟨6 * r * X, by lib_nat⟩

/-- **Infinitely many primes p ≡ 13 (mod 24).** -/
theorem inf_13mod24 (B : Nat) : ∃ c, (∀ d, Dv d (c * 24 + 13) → d = 1 ∨ d = c * 24 + 13) ∧ B < c * 24 + 13 := by
  match Inf5mod8.ofac_odd B with
  | ⟨u, hu⟩ =>
  have hM : (6 * (2 * u + 1)) * (6 * (2 * u + 1)) = ((6 * (u * u + u) + 1) * 24 + 11) + 1 := sq_mod24 u
  match has13 _ _ (3 * (2 * u + 1)) hM (by lib_nat) _ _ (Nat.le_refl _) ⟨1, (Nat.one_mul _).symm⟩
      ⟨_, phi_gen _ _ (sq_mod24 u)⟩ with
  | ⟨q, hq, ⟨c, hc⟩, hqN⟩ =>
    refine ⟨c, by rw [← hc]; exact hq, ?_⟩
    rw [← hc]
    cases Nat.lt_or_ge B q with
    | inl h => exact h
    | inr h =>
      exfalso
      have e13 : 2 * (12 * c + 6) + 1 = q := by rw [hc]; lib_nat
      match Inf5mod8.dv_ofac B (12 * c + 6) (by rw [e13]; exact h) with
      | ⟨r, hr⟩ =>
        have h0 : 6 * (2 * u + 1) = 6 * (r * q) := by rw [← hu, hr, e13]
        have h1 : Dv q ((6 * (2 * u + 1)) * ((6 * (2 * u + 1)) * ((6 * (u * u + u) + 1) * 24 + 11))) := by
          rw [h0]; exact dv_lead q r _
        have hq2 : q = (c * 24 + 12) + 1 := hc
        rw [hq2] at hqN h1
        exact dv_one_absurd (c * 24 + 11) (dv_sub (c * 24 + 12) _ 1 h1 hqN)

end Inf13mod24

#print axioms Inf13mod24.uniq_rem24
#print axioms Inf13mod24.mod24n
#print axioms Inf13mod24.cofactor24
#print axioms Inf13mod24.allowed13
#print axioms Inf13mod24.has13
#print axioms Inf13mod24.prod_gen
#print axioms Inf13mod24.shift_gen
#print axioms Inf13mod24.phi_gen
#print axioms Inf13mod24.sq_gen
#print axioms Inf13mod24.sq_mod24
#print axioms Inf13mod24.dv_lead
#print axioms Inf13mod24.inf_13mod24

lang/Inf17mod24.lean

lean_all/lang/Inf17mod24.lean

/-
  lang/Inf17mod24 — T43, cycle 210: infinitely many primes p ≡ 17 (mod 24).

  m = Π_{k<B} (3k + 1)(3k + 2) (every number prime to 3 below 3B), m² ≡ 1 (mod 3), x = 2m, N = x⁴ + 1 ≡ 17 (mod 24).
  Every prime factor q of N has x⁴ = −1 ≠ 1, x⁸ = 1 in 𝔽_q, so 8 | q − 1 (CycloPow.dvd_of_pow); q ≠ 3·(8a + 3), so q ≡ 1 or 17
  (mod 24). The product is ≡ 17, so some factor is ≡ 17 (descent has_gen, written once for every class a with a factor set {1, a}).
  q = 3(8c + 5) + 2 ≤ B would divide m, hence x⁴, hence 1.
    m3, m3_sq, dv_mulr, m3_dv, cofactor_gen, has_gen, allowed17, n17, dv_pow4, inf_17mod24
  The class 17 mod 24: split in ℚ(ζ₈), inert in ℚ(√−3).

  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.Inf1mod24
import lang.Inf7mod24
import lang.Inf13mod24

namespace Inf17mod24

open FTAChain ZMod InfSplit

/-- product of the numbers prime to 3 below 3B. -/
def m3 : Nat → Nat
  | 0 => 1
  | k + 1 => m3 k * ((3 * k + 1) * (3 * k + 2))

theorem m3_sq : ∀ B, ∃ C, m3 B * m3 B = 3 * C + 1
  | 0 => ⟨0, rfl⟩
  | k + 1 => by
    match m3_sq k with
    | ⟨C, hC⟩ =>
      have hP : (3 * k + 1) * (3 * k + 2) = 3 * (3 * k * k + 3 * k) + 2 := by lib_nat
      have e1 : m3 (k + 1) * m3 (k + 1) = (m3 k * m3 k) * (((3 * k + 1) * (3 * k + 2)) * ((3 * k + 1) * (3 * k + 2))) := by
        show (m3 k * ((3 * k + 1) * (3 * k + 2))) * (m3 k * ((3 * k + 1) * (3 * k + 2))) = _
        lib_nat
      have sq : ∀ A : Nat, (3 * A + 2) * (3 * A + 2) = 3 * (3 * A * A + 4 * A + 1) + 1 := fun A => by lib_nat
      have pr : ∀ C E : Nat, (3 * C + 1) * (3 * E + 1) = 3 * (3 * C * E + C + E) + 1 := fun C E => by lib_nat
      rw [e1, hC, hP, sq]
      exact ⟨_, pr _ _⟩

theorem dv_mulr (d n c : Nat) (h : Dv d n) : Dv d (n * c) :=
  match h with
  | ⟨w, hw⟩ => ⟨w * c, by rw [hw]; lib_nat⟩

theorem m3_dv : ∀ B k, k < B → Dv (3 * k + 2) (m3 B)
  | 0, _, h => absurd h (Nat.not_succ_le_zero _)
  | B + 1, k, h => by
    show Dv (3 * k + 2) (m3 B * ((3 * B + 1) * (3 * B + 2)))
    cases Nat.lt_or_ge k B with
    | inl hk => exact dv_mulr _ _ _ (m3_dv B k hk)
    | inr hk =>
      have e : k = B := Nat.le_antisymm (Nat.le_of_succ_le_succ h) hk
      rw [e]
      exact ⟨m3 B * (3 * B + 1), by lib_nat⟩

theorem cofactor_gen (a b N' e : Nat) (ha : a < 24) (h : (b * 24 + 1) * N' = e * 24 + a) : ∃ c, N' = c * 24 + a :=
  match Inf13mod24.mod24n N' with
  | ⟨c, r, hc, hr⟩ => by
    have e' : (b * 24 + 1) * N' = (b * 24 * c + b * r + c) * 24 + r := by rw [hc]; lib_nat
    rw [e'] at h
    exact ⟨c, by rw [hc, Inf13mod24.uniq_rem24 _ _ r a hr ha h]⟩

/-- **Descent.** If every prime factor of N₀ is ≡ 1 or a (mod 24), a divisor N ≡ a has a prime factor ≡ a. -/
theorem has_gen (a' N0 : Nat) (ha : a' + 1 + 1 < 24)
    (hall : ∀ l, (∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1) → Dv (l + 1 + 1) N0 →
      ∃ c t, l + 1 + 1 = c * 24 + t ∧ (t = 1 ∨ t = a' + 1 + 1)) :
    ∀ G N, N ≤ G → Dv N N0 → (∃ e, N = e * 24 + (a' + 1 + 1)) →
    ∃ q, (∀ d, Dv d q → d = 1 ∨ d = q) ∧ (∃ c, q = c * 24 + (a' + 1 + 1)) ∧ Dv q N
  | 0, N, hN, _, ⟨e, he⟩ => absurd (he ▸ hN) (fun h => Nat.not_succ_le_zero _ (Nat.le_trans (Nat.le_add_left _ _) h))
  | G + 1, N, hN, hN0, ⟨e, he⟩ => by
    match lp_exists (e * 24 + a') with
    | ⟨l, hl, hmin⟩ =>
    have hq := lp_prime _ l hl hmin
    have hlN : Dv (l + 1 + 1) N := by rw [he]; exact hl
    match hall l hq (dv_trans _ _ _ hlN hN0) with
    | ⟨c, t, hc, ht⟩ =>
      cases ht with
      | inr ha' => exact ⟨l + 1 + 1, hq, ⟨c, by rw [hc, ha']⟩, hlN⟩
      | inl h1 =>
        match hlN with
        | ⟨N', hN'⟩ =>
        match cofactor_gen (a' + 1 + 1) c N' e ha (by rw [← he, hN', hc, h1]; lib_nat) with
        | ⟨d, hd⟩ =>
        have hlt : N' ≤ G := by
          have e' : N = N' + (N' * (l + 1)) := by rw [hN']; lib_nat
          have h1' : 1 ≤ N' * (l + 1) := by rw [hd]; exact Nat.mul_le_mul (Nat.le_add_left 1 _) (Nat.le_add_left 1 _)
          exact Nat.le_of_succ_le_succ (Nat.le_trans (Nat.add_le_add_left h1' N') (by rw [← e']; exact hN))
        have hN'0 : Dv N' N0 := dv_trans _ _ _ ⟨l + 1 + 1, by rw [hN']; lib_nat⟩ hN0
        match has_gen a' N0 ha hall G N' hlt hN'0 ⟨d, hd⟩ with
        | ⟨q, hq', hqa, hqN⟩ => exact ⟨q, hq', hqa, dv_trans q N' N hqN ⟨l + 1 + 1, by rw [hN']; lib_nat⟩⟩

/-- prime factors of (2m)⁴ + 1 are ≡ 1 or 17 (mod 24). -/
theorem allowed17 (m l : Nat) (hq : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1)
    (hdv : Dv (l + 1 + 1) ((2 * m) ^ 4 + 1)) : ∃ c t, l + 1 + 1 = c * 24 + t ∧ (t = 1 ∨ t = 17) := by
  have hodd : l + 1 + 1 ≠ 2 := fun e => by
    rw [e] at hdv
    match hdv with
    | ⟨w, hw⟩ =>
      exact odd_not_dv2 (8 * m * m * m * m) w (by rw [← hw]; show _ = 1 * (2 * m) * (2 * m) * (2 * m) * (2 * m) + 1; lib_nat)
  have hxfull : Frobenius.pw (ring (l + 1)) (mk (l + 1) (2 * m)) ((0 + 1 + 1) ^ (2 + 1)) = (ring (l + 1)).one := by
    show Frobenius.pw (ring (l + 1)) (mk (l + 1) (2 * m)) 8 = _
    rw [PrimeOneModQ.mk_pow, Inf1mod24.pow8]; exact Inf1mod24.sq_one l _ hdv
  have hX1 : Frobenius.pw (ring (l + 1)) (mk (l + 1) (2 * m)) ((0 + 1 + 1) ^ 2) ≠ (ring (l + 1)).one := by
    show Frobenius.pw (ring (l + 1)) (mk (l + 1) (2 * m)) 4 ≠ _
    rw [PrimeOneModQ.mk_pow]; exact Inf1mod24.ne_one l _ hodd hdv
  match CycloPow.dvd_of_pow l hq 0 2 StrongMult1.two_prime _ hxfull hX1 with
  | ⟨k, hk⟩ =>
  have hk8 : l + 1 + 1 = k * 8 + 1 := by rw [hk]
  match Inf7mod24.mod3 k with
  | ⟨a, r, hr, hr3⟩ =>
    match r, hr3 with
    | 0, _ => exact ⟨a, 1, by rw [hk8, hr]; lib_nat, Or.inl rfl⟩
    | 1, _ =>
      exfalso
      have e9 : l + 1 + 1 = (8 * a + 3) * 3 := by rw [hk8, hr]; lib_nat
      cases hq 3 ⟨8 * a + 3, e9⟩ with
      | inl h => exact Nat.noConfusion (Nat.succ.inj h)
      | inr h =>
        have h' : 3 = a * 24 + 9 := by rw [h, e9]; lib_nat
        exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj h')))
    | 2, _ => exact ⟨a, 17, by rw [hk8, hr]; lib_nat, Or.inr rfl⟩
    | r + 3, hr3 => exact absurd hr3 (fun h => Nat.lt_irrefl 3 (Nat.lt_of_le_of_lt (Nat.le_add_left 3 r) h))

/-- m² = 3C + 1 ⟹ (2m)⁴ + 1 ≡ 17 (mod 24). -/
theorem n17 (m C : Nat) (h : m * m = 3 * C + 1) : (2 * m) ^ 4 + 1 = (6 * C * C + 4 * C) * 24 + 17 := by
  have e : (2 * m) ^ 4 + 1 = 16 * (m * m) * (m * m) + 1 := by
    show 1 * (2 * m) * (2 * m) * (2 * m) * (2 * m) + 1 = _
    lib_nat
  rw [e, h]; lib_nat

/-- q = 3(8c + 5) + 2 | M ⟹ q | (2M)⁴. -/
theorem dv_pow4 (M r c : Nat) (hr : M = r * (3 * (8 * c + 5) + 2)) : Dv (c * 24 + 17) ((2 * M) ^ 4) := by
  have e1 : 3 * (8 * c + 5) + 2 = c * 24 + 17 := by lib_nat
  rw [e1] at hr
  generalize c * 24 + 17 = Q at hr ⊢
  exact ⟨16 * r * M * M * M, by
    show 1 * (2 * M) * (2 * M) * (2 * M) * (2 * M) = _
    have e2 : 1 * (2 * M) * (2 * M) * (2 * M) * (2 * M) = 16 * M * M * M * M := by lib_nat
    rw [e2]
    have e3 : 16 * M * M * M * M = 16 * (r * Q) * M * M * M := congrArg (fun t => 16 * t * M * M * M) hr
    rw [e3]; lib_nat⟩

/-- **Infinitely many primes p ≡ 17 (mod 24).** -/
theorem inf_17mod24 (B : Nat) : ∃ c, (∀ d, Dv d (c * 24 + 17) → d = 1 ∨ d = c * 24 + 17) ∧ B < c * 24 + 17 := by
  match m3_sq B with
  | ⟨C, hC⟩ =>
  match has_gen 15 ((2 * m3 B) ^ 4 + 1) (Nat.lt_succ_of_lt (Nat.lt_succ_of_lt (Nat.lt_succ_of_lt (Nat.lt_succ_of_lt
      (Nat.lt_succ_of_lt (Nat.lt_succ_of_lt (Nat.lt_succ_self 17))))))) (fun l hq hdv => allowed17 (m3 B) l hq hdv)
      _ _ (Nat.le_refl _) ⟨1, (Nat.one_mul _).symm⟩ ⟨_, n17 _ _ hC⟩ with
  | ⟨q, hq, ⟨c, hc⟩, hqN⟩ =>
    rw [hc] at hq hqN
    refine ⟨c, hq, ?_⟩
    cases Nat.lt_or_ge B (c * 24 + 17) with
    | inl h => exact h
    | inr h =>
      exfalso
      have hk : 8 * c + 5 < B := Nat.lt_of_lt_of_le
        (Nat.lt_of_lt_of_le (Nat.lt_succ_self _) (by rw [show c * 24 + 17 = (8 * c + 5 + 1) + (16 * c + 11) by lib_nat]; exact Nat.le_add_right _ _)) h
      match m3_dv B (8 * c + 5) hk with
      | ⟨r, hr⟩ =>
        have h1 : Dv (c * 24 + 17) ((2 * m3 B) ^ 4) := dv_pow4 (m3 B) r c hr
        exact dv_one_absurd (c * 24 + 15) (dv_sub (c * 24 + 16) _ 1 h1 hqN)

end Inf17mod24

#print axioms Inf17mod24.m3
#print axioms Inf17mod24.m3_sq
#print axioms Inf17mod24.dv_mulr
#print axioms Inf17mod24.m3_dv
#print axioms Inf17mod24.cofactor_gen
#print axioms Inf17mod24.has_gen
#print axioms Inf17mod24.allowed17
#print axioms Inf17mod24.n17
#print axioms Inf17mod24.dv_pow4
#print axioms Inf17mod24.inf_17mod24

lang/Inf19mod24.lean

lean_all/lang/Inf19mod24.lean

/-
  lang/Inf19mod24 — T43, cycle 211: infinitely many primes p ≡ 19 (mod 24).

  y = 3·(odd factorial of B) = 2t + 1, X = y² = 4V + 1 with V = t² + t = 3S + 2, N = 4X² − 2X + 1 = 64V² + 24V + 3 ≡ 19 (mod 24).
  (2X + 1)·N = 8X³ + 1 = 2u² + 1 with u = 2yX, so q | (2u + q)² + 2 with 2u + q odd and q ≡ 1 or 3 (mod 8) (Inf3mod8.allowed3);
  4N = (4X − 1)² + 3, so q ≡ 1 (mod 3) (Inf7mod24.allowed3); hence q ≡ 1 or 19 (mod 24), and N ≡ 19 forces a factor ≡ 19
  (Inf17mod24.has_gen). An odd q ≤ B divides y, hence X, hence N − X(16V + 2) = 1.
    odd_N, not3, combine, allowed19, cube_plus, key19, n19, hu19, bound19, main19, inf_19mod24
  The class 19 mod 24: split in ℚ(√−2) and ℚ(√−3), inert in ℚ(i).

  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.Inf17mod24
import lang.Inf3mod8

namespace Inf19mod24

open FTAChain ZMod InfSplit

/-- a prime factor of an odd N is odd. -/
theorem odd_N (l N k : Nat) (hq : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1) (hdv : Dv (l + 1 + 1) N) (hN : N = 2 * k + 1) :
    ∃ s, l = 2 * s + 1 := by
  match EveryPrime.nat_even_odd l with
  | Or.inr h => exact h
  | Or.inl ⟨c, hc⟩ =>
    exfalso
    have h2 : l + 1 + 1 = 2 := by
      cases hq 2 ⟨c + 1, by rw [hc]; lib_nat⟩ with
      | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
      | inr e => exact e.symm
    rw [h2] at hdv
    match hdv with
    | ⟨w, hw⟩ => exact odd_not_dv2 k w (by rw [← hN, hw])

/-- 3 ∤ N when N ≡ 19 (mod 24). -/
theorem not3 (N a : Nat) (hN : N = a * 24 + 19) (h : Dv 3 N) : False := by
  match h with
  | ⟨w, hw⟩ =>
    have e := Inf7mod24.uniq_rem3 (a * 8 + 6) w 1 0 (Nat.lt_succ_of_lt (Nat.lt_succ_self 1)) (Nat.zero_lt_succ 2)
      (by rw [show (a * 8 + 6) * 3 + 1 = a * 24 + 19 by lib_nat, ← hN, hw]; lib_nat)
    exact Nat.noConfusion e

/-- q = 8b + r, r ∈ {1, 3}, 3 | q − 1 ⟹ q ≡ 1 or 19 (mod 24). -/
theorem combine (b r c : Nat) (hr : r = 1 ∨ r = 3) (h3 : b * 8 + r = c * 3 + 1) :
    ∃ e t, b * 8 + r = e * 24 + t ∧ (t = 1 ∨ t = 19) := by
  match Inf7mod24.mod3 b with
  | ⟨a, s, hb, hs⟩ =>
    have lt1 : 1 < 3 := Nat.lt_succ_of_lt (Nat.lt_succ_self 1)
    have lt2 : 2 < 3 := Nat.lt_succ_self 2
    have lt0 : 1 < 3 := lt1
    cases hr with
    | inl h1 =>
      subst h1
      match s, hs with
      | 0, _ => exact ⟨a, 1, by rw [hb]; lib_nat, Or.inl rfl⟩
      | 1, _ =>
        exact absurd (Inf7mod24.uniq_rem3 (a * 8 + 3) c 0 1 (Nat.zero_lt_succ 2) lt0
          (by rw [← h3, hb]; lib_nat)) (fun e => Nat.noConfusion e)
      | 2, _ =>
        exact absurd (Inf7mod24.uniq_rem3 (a * 8 + 5) c 2 1 lt2 lt0
          (by rw [← h3, hb]; lib_nat)) (fun e => Nat.noConfusion (Nat.succ.inj e))
      | s + 3, hs => exact absurd hs (fun h => Nat.lt_irrefl 3 (Nat.lt_of_le_of_lt (Nat.le_add_left 3 s) h))
    | inr h3' =>
      subst h3'
      match s, hs with
      | 0, _ =>
        exact absurd (Inf7mod24.uniq_rem3 (a * 8 + 1) c 0 1 (Nat.zero_lt_succ 2) lt0
          (by rw [← h3, hb]; lib_nat)) (fun e => Nat.noConfusion e)
      | 1, _ =>
        exact absurd (Inf7mod24.uniq_rem3 (a * 8 + 3) c 2 1 lt2 lt0
          (by rw [← h3, hb]; lib_nat)) (fun e => Nat.noConfusion (Nat.succ.inj e))
      | 2, _ => exact ⟨a, 19, by rw [hb]; lib_nat, Or.inr rfl⟩
      | s + 3, hs => exact absurd hs (fun h => Nat.lt_irrefl 3 (Nat.lt_of_le_of_lt (Nat.le_add_left 3 s) h))

/-- prime factors of N = 64V² + 24V + 3 with (8V + 3)N = 2u² + 1, N ≡ 19 (mod 24), are ≡ 1 or 19 (mod 24). -/
theorem allowed19 (V N u a l : Nat) (hN : N = 64 * V * V + 24 * V + 3) (hu : (8 * V + 3) * N = 2 * u * u + 1)
    (h19 : N = a * 24 + 19) (hq : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1) (hdv : Dv (l + 1 + 1) N) :
    ∃ c t, l + 1 + 1 = c * 24 + t ∧ (t = 1 ∨ t = 19) := by
  match odd_N l N (a * 12 + 9) hq hdv (by rw [h19]; lib_nat) with
  | ⟨s, hs⟩ =>
  -- mod 8: q | (2u + q)² + 2, 2u + q odd
  have hd2 : Dv (l + 1 + 1) ((2 * u + (l + 1 + 1)) * (2 * u + (l + 1 + 1)) + 2) := by
    match hdv with
    | ⟨w, hw⟩ =>
      exact ⟨2 * (8 * V + 3) * w + 4 * u + (l + 1 + 1), by
        have e : (2 * u + (l + 1 + 1)) * (2 * u + (l + 1 + 1)) + 2
            = 2 * (2 * u * u + 1) + (4 * u + (l + 1 + 1)) * (l + 1 + 1) := by lib_nat
        rw [e, ← hu, hw]; lib_nat⟩
  match Inf3mod8.allowed3 _ l ⟨u + s + 1, by rw [hs]; lib_nat⟩ hq hd2 with
  | ⟨b, r, hb, hr⟩ =>
  -- mod 3
  have hl2 : 2 ≤ l := by
    match l, hs with
    | 1, _ =>
      exfalso
      exact not3 N a h19 hdv
    | l + 2, _ => exact Nat.le_add_left 2 l
    | 0, hs0 => exact absurd hs0 (fun e => Nat.noConfusion e)
  match Inf7mod24.allowed3 l N (16 * V + 3) hq hl2 hdv (by rw [hN]; lib_nat) with
  | ⟨c, hc⟩ =>
    match combine b r c hr (by rw [← hb, show l + 1 + 1 = (l + 1) + 1 from rfl, hc]) with
    | ⟨e, t, he, ht⟩ => exact ⟨e, t, hb.trans he, ht⟩

/-- W + 2 = 4X ⟹ (2X + 1)(XW + 1) = 8X³ + 1. -/
theorem cube_plus (X W : Nat) (h : W + 2 = 4 * X) : (2 * X + 1) * (X * W + 1) = 8 * (X * X * X) + 1 := by
  apply LibInt.nadd_right_cancel ((2 * X + 1) * X * 2)
  calc (2 * X + 1) * (X * W + 1) + (2 * X + 1) * X * 2 = (2 * X + 1) * X * (W + 2) + (2 * X + 1) := by lib_nat
    _ = (2 * X + 1) * X * (4 * X) + (2 * X + 1) := by rw [h]
    _ = 8 * (X * X * X) + 1 + (2 * X + 1) * X * 2 := by lib_nat

/-- (8V + 3)(64V² + 24V + 3) = 8(4V + 1)³ + 1. -/
theorem key19 (V : Nat) : (8 * V + 3) * (64 * V * V + 24 * V + 3) = 8 * ((4 * V + 1) * (4 * V + 1) * (4 * V + 1)) + 1 := by
  have e1 : 8 * V + 3 = 2 * (4 * V + 1) + 1 := by lib_nat
  have e2 : 64 * V * V + 24 * V + 3 = (4 * V + 1) * (16 * V + 2) + 1 := by lib_nat
  rw [e1, e2]
  exact cube_plus (4 * V + 1) (16 * V + 2) (by lib_nat)

/-- V = 3S + 2 ⟹ 64V² + 24V + 3 ≡ 19 (mod 24). -/
theorem n19 (V S : Nat) (h : V = 3 * S + 2) : 64 * V * V + 24 * V + 3 = (6 * S * S + 8 * S + 2 + 2 * V * V + V) * 24 + 19 := by
  have e : 64 * V * V = 16 * V * V + 48 * V * V := by lib_nat
  rw [e]
  have e2 : 16 * V * V = (6 * S * S + 8 * S + 2) * 24 + 16 := by rw [h]; lib_nat
  rw [e2]
  have e3 : 48 * V * V = (2 * V * V) * 24 := by lib_nat
  rw [e3]; lib_nat

/-- (8V + 3)·N = 2u² + 1 with u = 2y³, y² = 4V + 1. -/
theorem hu19 (y V : Nat) (hX : y * y = 4 * V + 1) :
    (8 * V + 3) * (64 * V * V + 24 * V + 3) = 2 * (2 * y * (y * y)) * (2 * y * (y * y)) + 1 := by
  rw [key19]
  have e : 2 * (2 * y * (y * y)) * (2 * y * (y * y)) = 8 * ((y * y) * (y * y) * (y * y)) := by lib_nat
  rw [e, hX]

/-- q | y ⟹ q ∤ y²(16V + 2) + 1. -/
theorem bound19 (y V r Q : Nat) (hqy : y = (3 * r) * (Q + 1 + 1)) (hqN : Dv (Q + 1 + 1) ((y * y) * (16 * V + 2) + 1)) : False := by
  have h1 : Dv (Q + 1 + 1) ((y * y) * (16 * V + 2)) := ⟨3 * r * y * (16 * V + 2), by
    have e2 : y * y * (16 * V + 2) = 3 * r * (Q + 1 + 1) * y * (16 * V + 2) :=
      congrArg (fun z => z * y * (16 * V + 2)) hqy
    rw [e2]; lib_nat⟩
  exact dv_one_absurd Q (dv_sub (Q + 1) _ 1 h1 hqN)

theorem main19 (B y V S : Nat) (hX : y * y = 4 * V + 1) (hV : V = 3 * S + 2)
    (hyB : ∀ c, 2 * c + 1 ≤ B → ∃ r, y = (3 * r) * (2 * c + 1)) :
    ∃ c, (∀ d, Dv d (c * 24 + 19) → d = 1 ∨ d = c * 24 + 19) ∧ B < c * 24 + 19 := by
  have hN19 := n19 V S hV
  match Inf17mod24.has_gen 17 (64 * V * V + 24 * V + 3) (Nat.lt_succ_of_lt (Nat.lt_succ_of_lt (Nat.lt_succ_of_lt
      (Nat.lt_succ_of_lt (Nat.lt_succ_self 19)))))
      (fun l hq hdv => allowed19 V _ _ _ l rfl (hu19 y V hX) hN19 hq hdv)
      _ _ (Nat.le_refl _) ⟨1, (Nat.one_mul _).symm⟩ ⟨_, hN19⟩ with
  | ⟨q, hq, ⟨c, hc⟩, hqN⟩ =>
    refine ⟨c, by rw [← hc]; exact hq, ?_⟩
    cases Nat.lt_or_ge B (c * 24 + 19) with
    | inl h => exact h
    | inr h =>
      exfalso
      have hc2 : q = 2 * (12 * c + 9) + 1 := by rw [hc]; lib_nat
      match hyB (12 * c + 9) (by rw [← hc2, hc]; exact h) with
      | ⟨r, hr⟩ =>
        have hNX : 64 * V * V + 24 * V + 3 = (y * y) * (16 * V + 2) + 1 := by rw [hX]; lib_nat
        have hq' : q = c * 24 + 17 + 1 + 1 := by rw [hc2]; lib_nat
        rw [hNX, hq'] at hqN
        exact bound19 y V r (c * 24 + 17) (by rw [hr, hc2.symm.trans hq']) hqN

/-- **Infinitely many primes p ≡ 19 (mod 24).** -/
theorem inf_19mod24 (B : Nat) : ∃ c, (∀ d, Dv d (c * 24 + 19) → d = 1 ∨ d = c * 24 + 19) ∧ B < c * 24 + 19 :=
  match Inf5mod8.ofac_odd B with
  | ⟨w, hw⟩ =>
    main19 B (3 * Inf5mod8.ofac B) ((3 * w + 1) * (3 * w + 1) + (3 * w + 1)) (3 * w * w + 3 * w)
      (by rw [hw]; lib_nat) (by lib_nat)
      (fun c h => match Inf5mod8.dv_ofac B c h with
        | ⟨r, hr⟩ => ⟨r, by rw [hr]; lib_nat⟩)

end Inf19mod24

#print axioms Inf19mod24.odd_N
#print axioms Inf19mod24.not3
#print axioms Inf19mod24.combine
#print axioms Inf19mod24.allowed19
#print axioms Inf19mod24.cube_plus
#print axioms Inf19mod24.key19
#print axioms Inf19mod24.n19
#print axioms Inf19mod24.hu19
#print axioms Inf19mod24.bound19
#print axioms Inf19mod24.main19
#print axioms Inf19mod24.inf_19mod24

lang/Inf1mod12.lean

lean_all/lang/Inf1mod12.lean

/-
  lang/Inf1mod12 — T43, cycle 203: infinitely many primes p ≡ 1 (mod 12).

  a = 2·B!, N = a⁴ − a² + 1 = a²(a² − 1) + 1 = Φ₁₂(a). For a prime factor q (q ∤ a since N ≡ 1 mod a):
    (a² − 1)² + a² = N ≡ 0  ⇒  −1 = ((a² − 1)/a)² is a square  ⇒  q ≡ 1 (mod 4)        (GaussSplit)
    (a² + 1)² − 3a² = N ≡ 0  ⇒  3 = ((a² + 1)/a)² is a square   ⇒  q ≡ 1 (mod 12)      (Sq3.sq3_split)
  and q ≤ B would divide a, hence 1.
    sq_of_div, allowed1, inf_1mod12

  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Inf7mod12

namespace Inf1mod12

open FTAChain ZMod LibInt InfSplit
open LibRing (castZ castN)

/-- in 𝔽_q: X² + c·A² = 0, A ≠ 0 ⇒ (X/A)² = −c. -/
theorem sq_of_div (r : Nat) (hpr : ∀ d, Dv d (2 * r + 1 + 1 + 1) → d = 1 ∨ d = 2 * r + 1 + 1 + 1) (X A c : Zp (2 * r + 1 + 1))
    (hA : A ≠ (ring (2 * r + 1 + 1)).zero) (h : (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).mul X X) ((ring (2 * r + 1 + 1)).mul c ((ring (2 * r + 1 + 1)).mul A A)) = (ring (2 * r + 1 + 1)).zero) :
    ∃ y, (ring (2 * r + 1 + 1)).mul y y = (ring (2 * r + 1 + 1)).neg c := by
  have hw := ZMod.inv_exists (2 * r + 1) hpr A hA
  generalize Frobenius.pw (ring (2 * r + 1 + 1)) A (2 * r + 1) = w at hw
  refine ⟨(ring (2 * r + 1 + 1)).mul X w, ?_⟩
  have hX : (ring (2 * r + 1 + 1)).mul X X = (ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).mul c ((ring (2 * r + 1 + 1)).mul A A)) := DetChain.neg_unique (ring (2 * r + 1 + 1)) _ _ h
  rw [show (ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1 + 1)).mul X w) ((ring (2 * r + 1 + 1)).mul X w) = (ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1 + 1)).mul X X) ((ring (2 * r + 1 + 1)).mul w w) by lib_ring, hX,
    show (ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).mul c ((ring (2 * r + 1 + 1)).mul A A))) ((ring (2 * r + 1 + 1)).mul w w) = (ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).mul c ((ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1 + 1)).mul w A) ((ring (2 * r + 1 + 1)).mul w A))) by lib_ring,
    hw, (ring (2 * r + 1 + 1)).one_mul, (ring (2 * r + 1 + 1)).mul_comm, (ring (2 * r + 1 + 1)).one_mul]

theorem one_ne_zeroZ (r : Nat) : (ring (2 * r + 1 + 1)).one ≠ (ring (2 * r + 1 + 1)).zero := fun e => by
  apply QChar.ne_pm r
  rw [e]; exact (DetChain.neg_unique (ring (2 * r + 1 + 1)) _ _ ((ring (2 * r + 1 + 1)).zero_add _)).symm

/-- every prime factor of N = a(aM) + 1 = Φ₁₂(a) (a² = M + 1, a even) is ≡ 1 (mod 12). -/
theorem allowed1 (a M b l : Nat) (hM : a * a = M + 1) (hb : a = 2 * b) (hq : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1)
    (hdv : Dv (l + 1 + 1) (a * (a * M) + 1)) : ∃ s, l + 1 + 1 = s * 12 + 1 := by
  have hodd : ∀ c, l + 1 + 1 = 2 * c → False := fun c hc => by
    have h2 : l + 1 + 1 = 2 := by
      cases hq 2 ⟨c, by rw [hc]; lib_nat⟩ with
      | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
      | inr e => exact e.symm
    rw [h2] at hdv
    match hdv with
    | ⟨w, hw⟩ => exact InfSplit.odd_not_dv2 (b * (a * M)) w (by rw [← hw, hb]; lib_nat)
  match EveryPrime.nat_even_odd l with
  | Or.inl ⟨c, hc⟩ => exact absurd (by rw [hc]; lib_nat : l + 1 + 1 = 2 * (c + 1)) (fun e => hodd (c + 1) e)
  | Or.inr ⟨r, hr⟩ =>
    subst hr
    have hMI : Int.ofNat a * Int.ofNat a = Int.ofNat M + 1 := congrArg Int.ofNat hM
    have hN0 : castZ (ring (2 * r + 1 + 1)) (Int.ofNat (a * (a * M) + 1)) = (ring (2 * r + 1 + 1)).zero := by
      match hdv with
      | ⟨w, hw⟩ => rw [hw, show Int.ofNat (w * (2 * r + 1 + 1 + 1)) = Int.ofNat w * Int.ofNat (2 * r + 1 + 1 + 1) from rfl,
          HasseDavenport.castZ_mul, CubeAp.castP, DetChain.mul_zero]
    have hNa : Int.ofNat (a * (a * M) + 1) = Int.ofNat a * (Int.ofNat a * Int.ofNat M) + 1 := rfl
    have hA : castZ (ring (2 * r + 1 + 1)) (Int.ofNat a) ≠ (ring (2 * r + 1 + 1)).zero := fun e => by
      rw [hNa, IntCast.castZ_add, HasseDavenport.castZ_mul, e, DetChain.zero_mul'] at hN0
      apply one_ne_zeroZ r
      rw [← hN0]; show _ = (ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).zero ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one (ring (2 * r + 1 + 1)).zero); rw [(ring (2 * r + 1 + 1)).zero_add, DetChain.add_zero']
    have hm1 : ∃ y, (ring (2 * r + 1 + 1)).mul y y = (ring (2 * r + 1 + 1)).neg (ring (2 * r + 1 + 1)).one := by
      apply sq_of_div r hq (castZ (ring (2 * r + 1 + 1)) (Int.ofNat M)) (castZ (ring (2 * r + 1 + 1)) (Int.ofNat a)) (ring (2 * r + 1 + 1)).one hA
      rw [(ring (2 * r + 1 + 1)).one_mul, ← HasseDavenport.castZ_mul, ← HasseDavenport.castZ_mul, ← IntCast.castZ_add,
        show Int.ofNat M * Int.ofNat M + Int.ofNat a * Int.ofNat a = Int.ofNat (a * (a * M) + 1) by
          rw [hNa, show Int.ofNat a * (Int.ofNat a * Int.ofNat M) = (Int.ofNat a * Int.ofNat a) * Int.ofNat M by lib_int, hMI]; lib_int]
      exact hN0
    have h3sq : ∃ y, (ring (2 * r + 1 + 1)).mul y y = Eisen.three (2 * r + 1) := by
      match sq_of_div r hq (castZ (ring (2 * r + 1 + 1)) (Int.ofNat a * Int.ofNat a + 1)) (castZ (ring (2 * r + 1 + 1)) (Int.ofNat a)) ((ring (2 * r + 1 + 1)).neg (Eisen.three (2 * r + 1))) hA (by
        rw [← Inf11mod12.castZ3, ← HasseDavenport.castZ_neg, ← HasseDavenport.castZ_mul, ← HasseDavenport.castZ_mul, ← HasseDavenport.castZ_mul,
          ← IntCast.castZ_add,
          show (Int.ofNat a * Int.ofNat a + 1) * (Int.ofNat a * Int.ofNat a + 1) + -3 * (Int.ofNat a * Int.ofNat a) = Int.ofNat (a * (a * M) + 1) by
            rw [hNa, show Int.ofNat a * (Int.ofNat a * Int.ofNat M) = (Int.ofNat a * Int.ofNat a) * Int.ofNat M by lib_int, hMI]; lib_int]
        exact hN0) with
      | ⟨y, hy⟩ => exact ⟨y, by rw [hy, DetChain.neg_neg']⟩
    match EveryPrime.nat_even_odd r, hm1, h3sq with
    | Or.inl ⟨k, hk⟩, ⟨y, hy⟩, _ => subst hk; exact absurd hy (GaussSplit.inert_three_mod_four k hq y)
    | Or.inr ⟨k, hk⟩, _, ⟨u, hu⟩ => subst hk; exact Sq3.sq3_split k hq u hu

theorem two_le (n : Nat) (h : 2 ≤ n) : ∃ i, n = i + 1 + 1 :=
  match n, h with
  | i + 2, _ => ⟨i, rfl⟩

/-- **Infinitely many primes p ≡ 1 (mod 12).** -/
theorem inf_1mod12 (B : Nat) : ∃ s, (∀ d, Dv d (s * 12 + 1) → d = 1 ∨ d = s * 12 + 1) ∧ B < s * 12 + 1 := by
  match fac_pos B with
  | ⟨f, hf⟩ =>
  have hM : (2 * (f + 1)) * (2 * (f + 1)) = (4 * f * f + 8 * f + 3) + 1 := by lib_nat
  have ha1 : 1 ≤ 2 * (f + 1) := by rw [show 2 * (f + 1) = (2 * f + 1) + 1 by lib_nat]; exact Nat.le_add_left 1 _
  have hM1 : 1 ≤ 4 * f * f + 8 * f + 3 := by rw [show 4 * f * f + 8 * f + 3 = (4 * f * f + 8 * f + 2) + 1 by lib_nat]; exact Nat.le_add_left 1 _
  have hN2 : 2 ≤ (2 * (f + 1)) * ((2 * (f + 1)) * (4 * f * f + 8 * f + 3)) + 1 :=
    Nat.succ_le_succ (Nat.mul_le_mul ha1 (Nat.mul_le_mul ha1 hM1))
  match two_le _ hN2 with
  | ⟨i, hi⟩ =>
  match lp_exists i with
  | ⟨l, hl, hmin⟩ =>
    have hl' : Dv (l + 1 + 1) ((2 * (f + 1)) * ((2 * (f + 1)) * (4 * f * f + 8 * f + 3)) + 1) := by rw [hi]; exact hl
    have hq := lp_prime _ l hl hmin
    match allowed1 _ _ (f + 1) l hM rfl hq hl' with
    | ⟨s, hs⟩ =>
      refine ⟨s, by rw [← hs]; exact hq, ?_⟩
      rw [← hs]
      cases Nat.lt_or_ge B (l + 1 + 1) with
      | inl h => exact h
      | inr h =>
        exfalso
        match dv_fac B (l + 1 + 1) (Nat.le_add_left 1 _) h with
        | ⟨w, hw⟩ =>
          have h1 : Dv (l + 1 + 1) ((2 * (f + 1)) * ((2 * (f + 1)) * (4 * f * f + 8 * f + 3))) :=
            ⟨2 * w * ((2 * (f + 1)) * (4 * f * f + 8 * f + 3)), by rw [← hf, hw]; lib_nat⟩
          exact dv_one_absurd l (dv_sub (l + 1) _ 1 h1 hl')

end Inf1mod12

#print axioms Inf1mod12.sq_of_div
#print axioms Inf1mod12.one_ne_zeroZ
#print axioms Inf1mod12.allowed1
#print axioms Inf1mod12.two_le
#print axioms Inf1mod12.inf_1mod12

lang/Inf1mod24.lean

lean_all/lang/Inf1mod24.lean

/-
  lang/Inf1mod24 — T43, cycle 208: infinitely many primes p ≡ 1 (mod 24).

  d = 2·B!, a = d², N = Φ₁₂(a) = a(a(a² − 1)) + 1. Every prime factor q of N is ≡ 1 (mod 12) (Inf1mod12.allowed1), and
  (a² + 1)·N = a⁶ + 1 = (d³)⁴ + 1, so y = d³ has y⁴ = −1 ≠ 1, y⁸ = 1 in 𝔽_q: 8 | q − 1 (CycloPow.dvd_of_pow). Hence q ≡ 1 (mod 24),
  and q ∤ d gives q > B. No descent is needed: every prime factor of Φ₂₄(d) = Φ₁₂(d²) is in the class.
    cube, key, ne_one, sq_one, pow8, even_of, dvX, inf_1mod24
  The trivial class mod 24: primes split completely in ℚ(ζ₂₄) ⊇ ℚ(i, √2, √−3).
  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.Inf1mod12
import lang.CycloPow
import lang.StrongMult1

namespace Inf1mod24

open FTAChain ZMod InfSplit

theorem cube (M : Nat) : (M + 1 + 1) * ((M + 1) * M + 1) = (M + 1) * (M + 1) * (M + 1) + 1 := by lib_nat

/-- (a² + 1)·Φ₁₂(a) = a⁶ + 1 = (d³)⁴ + 1 for a = d², a² = M + 1. -/
theorem key (d M : Nat) (ha : (d * d) * (d * d) = M + 1) :
    ((d * d) * (d * d) + 1) * ((d * d) * ((d * d) * M) + 1) = (d * d * d) ^ 4 + 1 := by
  have e1 : ((d * d) * (d * d) + 1) * ((d * d) * ((d * d) * M) + 1)
      = ((d * d) * (d * d) + 1) * (((d * d) * (d * d)) * M + 1) := by lib_nat
  have e2 : (d * d * d) ^ 4 + 1 = ((d * d) * (d * d)) * ((d * d) * (d * d)) * ((d * d) * (d * d)) + 1 := by
    show 1 * (d * d * d) * (d * d * d) * (d * d * d) * (d * d * d) + 1 = _
    lib_nat
  rw [e1, e2, ha]
  exact cube M

/-- q | E + 1, q ≠ 2 ⟹ E ≢ 1 (mod q). -/
theorem ne_one (l E : Nat) (hodd : l + 1 + 1 ≠ 2) (h : Dv (l + 1 + 1) (E + 1)) :
    mk (l + 1) E ≠ (ring (l + 1)).one := fun e => by
  have h1 : md (l + 1) E = md (l + 1) 1 := congrArg Subtype.val e
  have h2 : md (l + 1) (E + 1) = md (l + 1) (1 + 1) := md_succ_congr (l + 1) _ _ h1
  rw [dv_md_zero (l + 1) _ h] at h2
  cases StrongMult1.two_prime (l + 1 + 1) (md_zero_dv (l + 1) _ h2.symm) with
  | inl h' => exact Nat.noConfusion (Nat.succ.inj h')
  | inr h' => exact hodd h'

/-- q | E + 1 ⟹ E² ≡ 1 (mod q). -/
theorem sq_one (l E : Nat) (h : Dv (l + 1 + 1) (E + 1)) : mk (l + 1) (E * E) = (ring (l + 1)).one :=
  match h with
  | ⟨k, hk⟩ => Subtype.ext (by
    show md (l + 1) (E * E) = md (l + 1) 1
    have e : E * E + k * (l + 1 + 1) = 1 + (k * E) * (l + 1 + 1) := by
      rw [show (k * E) * (l + 1 + 1) = E * (k * (l + 1 + 1)) by lib_nat, ← hk]; lib_nat
    calc md (l + 1) (E * E) = md (l + 1) (E * E + k * (l + 1 + 1)) := (md_add_mul (l + 1) _ k).symm
      _ = md (l + 1) (1 + (k * E) * (l + 1 + 1)) := congrArg (md (l + 1)) e
      _ = md (l + 1) 1 := md_add_mul (l + 1) 1 (k * E))

theorem pow8 (X : Nat) : X ^ 8 = X ^ 4 * X ^ 4 := by
  show 1 * X * X * X * X * X * X * X * X = (1 * X * X * X * X) * (1 * X * X * X * X)
  lib_nat

theorem even_of (s k : Nat) (h : s * 12 = k * 8) : ∃ c, s = 2 * c :=
  match EveryPrime.nat_even_odd s with
  | Or.inl hc => hc
  | Or.inr ⟨c, hc⟩ => by
    exfalso
    have e : 4 * (2 * (3 * c + 1) + 1) = 4 * (k * 2) := by
      have : (2 * c + 1) * 12 = k * 8 := hc ▸ h
      rw [show 4 * (2 * (3 * c + 1) + 1) = (2 * c + 1) * 12 by lib_nat, this]; lib_nat
    exact odd_not_dv2 (3 * c + 1) k (Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ 3) e)

theorem dvX (q r Y : Nat) : Dv q ((r * q) * Y) := ⟨r * Y, by lib_nat⟩

/-- **Infinitely many primes p ≡ 1 (mod 24).** -/
theorem inf_1mod24 (B : Nat) : ∃ c, (∀ e, Dv e (c * 24 + 1) → e = 1 ∨ e = c * 24 + 1) ∧ B < c * 24 + 1 := by
  match fac_pos B with
  | ⟨f, hf⟩ =>
  have hd : (2 * (f + 1)) * (2 * (f + 1)) = (4 * f * f + 8 * f + 2) + 1 + 1 := by lib_nat
  have hb : (2 * (f + 1)) * (2 * (f + 1)) = 2 * ((f + 1) * (2 * (f + 1))) := by lib_nat
  generalize hdd : 2 * (f + 1) = d at hd hb
  generalize 4 * f * f + 8 * f + 2 = C at hd
  have ha : (d * d) * (d * d) = ((C + 1) * (C + 1) + 2 * (C + 1)) + 1 := by rw [hd]; lib_nat
  have hM1 : 1 ≤ (C + 1) * (C + 1) + 2 * (C + 1) :=
    Nat.le_trans (Nat.mul_le_mul (Nat.le_add_left 1 C) (Nat.le_add_left 1 C)) (Nat.le_add_right _ _)
  generalize (C + 1) * (C + 1) + 2 * (C + 1) = M at ha hM1
  have hpos : 1 ≤ (d * d) * ((d * d) * M) := by
    rw [hd]
    exact Nat.mul_le_mul (Nat.le_add_left 1 _) (Nat.mul_le_mul (Nat.le_add_left 1 _) hM1)
  match Inf1mod12.two_le ((d * d) * ((d * d) * M) + 1) (Nat.succ_le_succ hpos) with
  | ⟨i, hi⟩ =>
  match lp_exists i with
  | ⟨l, hl, hmin⟩ =>
  have hl' : Dv (l + 1 + 1) ((d * d) * ((d * d) * M) + 1) := by rw [hi]; exact hl
  have hq := lp_prime _ l hl hmin
  match Inf1mod12.allowed1 (d * d) M ((f + 1) * d) l ha hb hq hl' with
  | ⟨s, hs⟩ =>
  have hE : Dv (l + 1 + 1) ((d * d * d) ^ 4 + 1) := by rw [← key d M ha]; exact dv_mul_left _ _ _ hl'
  have hodd : l + 1 + 1 ≠ 2 := fun e => odd_not_dv2 (s * 6) 1 (by rw [show 2 * (s * 6) + 1 = s * 12 + 1 by lib_nat, ← hs, e])
  have hxfull : Frobenius.pw (ring (l + 1)) (mk (l + 1) (d * d * d)) ((0 + 1 + 1) ^ (2 + 1)) = (ring (l + 1)).one := by
    show Frobenius.pw (ring (l + 1)) (mk (l + 1) (d * d * d)) 8 = _
    rw [PrimeOneModQ.mk_pow, pow8]; exact sq_one l _ hE
  have hX1 : Frobenius.pw (ring (l + 1)) (mk (l + 1) (d * d * d)) ((0 + 1 + 1) ^ 2) ≠ (ring (l + 1)).one := by
    show Frobenius.pw (ring (l + 1)) (mk (l + 1) (d * d * d)) 4 ≠ _
    rw [PrimeOneModQ.mk_pow]; exact ne_one l _ hodd hE
  match CycloPow.dvd_of_pow l hq 0 2 StrongMult1.two_prime _ hxfull hX1 with
  | ⟨k, hk⟩ =>
  match even_of s k ((Nat.succ.inj hs).symm.trans hk) with
  | ⟨c, hc⟩ =>
  have hq24 : l + 1 + 1 = c * 24 + 1 := by rw [hs, hc]; lib_nat
  refine ⟨c, by rw [← hq24]; exact hq, ?_⟩
  rw [← hq24]
  cases Nat.lt_or_ge B (l + 1 + 1) with
  | inl h => exact h
  | inr h =>
    exfalso
    match dv_fac B (l + 1 + 1) (Nat.le_add_left 1 _) h with
    | ⟨w, hw⟩ =>
      have hdq : d = (2 * w) * (l + 1 + 1) := by rw [← hdd, ← hf, hw]; lib_nat
      have h1 : Dv (l + 1 + 1) ((d * d) * ((d * d) * M)) := by
        rw [show (d * d) * ((d * d) * M) = d * (d * ((d * d) * M)) by lib_nat, hdq]
        exact dvX _ _ _
      exact dv_one_absurd l (dv_sub (l + 1) _ 1 h1 hl')

end Inf1mod24

#print axioms Inf1mod24.cube
#print axioms Inf1mod24.key
#print axioms Inf1mod24.ne_one
#print axioms Inf1mod24.sq_one
#print axioms Inf1mod24.pow8
#print axioms Inf1mod24.even_of
#print axioms Inf1mod24.dvX
#print axioms Inf1mod24.inf_1mod24

lang/Inf23mod24.lean

lean_all/lang/Inf23mod24.lean

/-
  lang/Inf23mod24 — T43, cycle 212: infinitely many primes p ≡ 23 (mod 24).

  x = 12·B!, W = x² + x − 3, M = 2W² + 8W + 5 = 2x⁴ + 4x³ − 2x² − 4x − 1 = −(x⁴ − 4x²y² + y⁴) at y = x + 1, M ≡ 23 (mod 24).
  With D = 2x + 1 (D² = 4W + 13) and T = 2D(W + 1):  T² = 2 + M(8W + 10),  ((W + 2)T)² = 3 + M(1 + (W + 2)²(8W + 10)).
  So 2 and 3 are squares mod every prime factor q of M: q ≡ ±1 (mod 8) (TwoSupp) and q ≡ ±1 (mod 12) (Sq3), hence q ≡ ±1 (mod 24);
  M ≡ −1 forces a factor ≡ 23 (Inf17mod24.has_gen). q ≤ B divides x, hence M + 1 = 2x(x + 1)(W + 1), hence 1.
    not3', two_mod8, combine23, t_sq, s_sq, allowed23, hx_gen, m_gen, m23, bound23, inf_23mod24
  The class −1 mod 24: split in ℚ(√2, √3) = ℚ(ζ₂₄)⁺, inert in ℚ(i). x⁴ − 4x² + 1 is the minimal polynomial of 2cos(π/12).

  Lean 4 core only, proofs by hand (lib_nat, lib_int), no axioms.
-/
import lang.Inf19mod24
import lang.Inf11mod12
import lang.Sq3

namespace Inf23mod24

open FTAChain ZMod InfSplit TwoSupp
open LibRing (castZ castN)

theorem not3' (N a : Nat) (hN : N = a * 24 + 23) (h : Dv 3 N) : False := by
  match h with
  | ⟨w, hw⟩ =>
    have e := Inf7mod24.uniq_rem3 (a * 8 + 7) w 2 0 (Nat.lt_succ_self 2) (Nat.zero_lt_succ 2)
      (by rw [show (a * 8 + 7) * 3 + 2 = a * 24 + 23 by lib_nat, ← hN, hw]; lib_nat)
    exact Nat.noConfusion e

/-- 2 a square mod an odd prime q ⟹ q ≡ ±1 (mod 8). -/
theorem two_mod8 (l t : Nat) (hq : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1)
    (hodd : ∀ c, l + 1 + 1 = 2 * c → False) (hsq : rem (l + 1 + 1) (t * t) = rem (l + 1 + 1) 2) :
    ∃ b r, l + 1 + 1 = b * 8 + r ∧ (r = 1 ∨ r = 7) := by
  have hPr : Pr (l + 1 + 1) := ⟨⟨l, rfl⟩, hq⟩
  match Inf7mod8.mod8 (l + 1 + 1) with
  | ⟨b, r, hb, hr⟩ =>
    match r, hr with
    | 0, _ => exact absurd hb (fun e => hodd (4 * b) (by rw [e]; lib_nat))
    | 1, _ => exact ⟨b, 1, hb, Or.inl rfl⟩
    | 2, _ => exact absurd hb (fun e => hodd (4 * b + 1) (by rw [e]; lib_nat))
    | 3, _ =>
      exfalso
      have e : (b + b) + ((b + b) + 1) + ((b + b) + ((b + b) + 1)) + 1 = l + 1 + 1 := by rw [hb]; lib_nat
      exact two_8m3 b (by rw [e]; exact hPr) ⟨t, by rw [e]; exact hsq⟩
    | 4, _ => exact absurd hb (fun e => hodd (4 * b + 2) (by rw [e]; lib_nat))
    | 5, _ =>
      exfalso
      have e : (b + b + 1) + ((b + b + 1) + 0) + ((b + b + 1) + ((b + b + 1) + 0)) + 1 = l + 1 + 1 := by rw [hb]; lib_nat
      exact two_8m5 b (by rw [e]; exact hPr) ⟨t, by rw [e]; exact hsq⟩
    | 6, _ => exact absurd hb (fun e => hodd (4 * b + 3) (by rw [e]; lib_nat))
    | 7, _ => exact ⟨b, 7, hb, Or.inr rfl⟩
    | r + 8, hr => exact absurd hr (fun h => Nat.lt_irrefl 8 (Nat.lt_of_le_of_lt (Nat.le_add_left 8 r) h))

/-- q ≡ ±1 (mod 8) and q ≡ ±1 (mod 12) ⟹ q ≡ ±1 (mod 24). -/
theorem combine23 (q b r s t : Nat) (h8 : q = b * 8 + r) (hr : r = 1 ∨ r = 7) (h12 : q = s * 12 + t) (ht : t = 1 ∨ t = 11) :
    ∃ e u, q = e * 24 + u ∧ (u = 1 ∨ u = 23) := by
  have hr8 : r < 8 := by
    cases hr with
    | inl h => rw [h]; exact Nat.le_of_ble_eq_true rfl
    | inr h => rw [h]; exact Nat.le_of_ble_eq_true rfl
  have bad : ∀ e c, c < 8 → q = e * 8 + c → r = c := fun e c hc h =>
    Inf7mod8.uniq_rem8 b e r c hr8 hc (h8.symm.trans h)
  cases ht with
  | inl h1 =>
    subst h1
    match EveryPrime.nat_even_odd s with
    | Or.inl ⟨e, he⟩ => exact ⟨e, 1, by rw [h12, he]; lib_nat, Or.inl rfl⟩
    | Or.inr ⟨e, he⟩ =>
      exfalso
      have h5 := bad (3 * e + 1) 5 (Nat.le_of_ble_eq_true rfl) (by rw [h12, he]; lib_nat)
      cases hr with
      | inl h => rw [h] at h5; exact Nat.noConfusion (Nat.succ.inj h5)
      | inr h => rw [h] at h5; exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj h5)))))
  | inr h11 =>
    subst h11
    match EveryPrime.nat_even_odd s with
    | Or.inr ⟨e, he⟩ => exact ⟨e, 23, by rw [h12, he]; lib_nat, Or.inr rfl⟩
    | Or.inl ⟨e, he⟩ =>
      exfalso
      have h3 := bad (3 * e + 1) 3 (Nat.le_of_ble_eq_true rfl) (by rw [h12, he]; lib_nat)
      cases hr with
      | inl h => rw [h] at h3; exact Nat.noConfusion (Nat.succ.inj h3)
      | inr h => rw [h] at h3; exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj h3)))

/-- D² = 4W + 13 ⟹ (2D(W + 1))² = 2 + (2W² + 8W + 5)(8W + 10). -/
theorem t_sq (D W : Nat) (hD : D * D = 4 * W + 13) :
    (2 * D * (W + 1)) * (2 * D * (W + 1)) = 2 + (2 * W * W + 8 * W + 5) * (8 * W + 10) := by
  have e : (2 * D * (W + 1)) * (2 * D * (W + 1)) = 4 * (D * D) * ((W + 1) * (W + 1)) := by lib_nat
  rw [e, hD]; lib_nat

/-- T² = 2 + MK, 2A² = M + 3 ⟹ (AT)² = 3 + M(1 + A²K). -/
theorem s_sq (A T M K : Nat) (hT : T * T = 2 + M * K) (hA : 2 * (A * A) = M + 3) :
    (A * T) * (A * T) = 3 + M * (1 + A * A * K) := by
  have e : (A * T) * (A * T) = (A * A) * (T * T) := by lib_nat
  rw [e, hT]
  calc A * A * (2 + M * K) = 2 * (A * A) + M * (A * A * K) := by lib_nat
    _ = M + 3 + M * (A * A * K) := by rw [hA]
    _ = 3 + M * (1 + A * A * K) := by lib_nat

/-- prime factors of M = 2W² + 8W + 5 with D² = 4W + 13, M ≡ 23 (mod 24), are ≡ ±1 (mod 24). -/
theorem allowed23 (W D a l : Nat) (hD : D * D = 4 * W + 13) (h23 : 2 * W * W + 8 * W + 5 = a * 24 + 23)
    (hq : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1) (hdv : Dv (l + 1 + 1) (2 * W * W + 8 * W + 5)) :
    ∃ c t, l + 1 + 1 = c * 24 + t ∧ (t = 1 ∨ t = 23) := by
  match Inf19mod24.odd_N l _ (a * 12 + 11) hq hdv (by rw [h23]; lib_nat) with
  | ⟨s, hs⟩ =>
  have hodd : ∀ c, l + 1 + 1 = 2 * c → False := fun c hc =>
    odd_not_dv2 (s + 1) c (by rw [show 2 * (s + 1) + 1 = l + 1 + 1 by rw [hs]; lib_nat, hc]; lib_nat)
  have h3 : l + 1 + 1 ≠ 3 := fun e => not3' _ a h23 (e ▸ hdv)
  match hdv with
  | ⟨w, hw⟩ =>
  have hT := t_sq D W hD
  have hsq : rem (l + 1 + 1) ((2 * D * (W + 1)) * (2 * D * (W + 1))) = rem (l + 1 + 1) 2 := by
    rw [hT, hw, show 2 + w * (l + 1 + 1) * (8 * W + 10) = (w * (8 * W + 10)) * (l + 1 + 1) + 2 by lib_nat]
    exact rem_add_mul (l + 1) 2 (w * (8 * W + 10))
  match two_mod8 l _ hq hodd hsq with
  | ⟨b, r, hb, hr⟩ =>
  have hS := s_sq (W + 2) _ _ _ hT (by lib_nat)
  rw [hw] at hS
  have h12 : ∃ s' t, l + 1 + 1 = s' * 12 + t ∧ (t = 1 ∨ t = 11) := by
    subst hs
    have hu : (ring (2 * s + 1 + 1)).mul (castZ (ring (2 * s + 1 + 1)) (Int.ofNat ((W + 2) * (2 * D * (W + 1)))))
        (castZ (ring (2 * s + 1 + 1)) (Int.ofNat ((W + 2) * (2 * D * (W + 1))))) = Eisen.three (2 * s + 1) := by
      have hn : (W + 2) * (2 * D * (W + 1)) * ((W + 2) * (2 * D * (W + 1)))
          = (w * (1 + (W + 2) * (W + 2) * (8 * W + 10))) * (2 * s + 1 + 1 + 1) + 3 := by rw [hS]; lib_nat
      have hI : Int.ofNat ((W + 2) * (2 * D * (W + 1))) * Int.ofNat ((W + 2) * (2 * D * (W + 1)))
          = Int.ofNat (w * (1 + (W + 2) * (W + 2) * (8 * W + 10))) * Int.ofNat (2 * s + 1 + 1 + 1) + 3 := congrArg Int.ofNat hn
      rw [← HasseDavenport.castZ_mul, hI, IntCast.castZ_add, HasseDavenport.castZ_mul, CubeAp.castP, DetChain.mul_zero,
        (ring (2 * s + 1 + 1)).zero_add, Inf11mod12.castZ3]
    match EveryPrime.nat_even_odd s with
    | Or.inl ⟨k, hk⟩ =>
      subst hk
      match Sq3.sq3_inert k hq h3 _ hu with
      | ⟨s', hs'⟩ => exact ⟨s', 11, hs', Or.inr rfl⟩
    | Or.inr ⟨k, hk⟩ =>
      subst hk
      match Sq3.sq3_split k hq _ hu with
      | ⟨s', hs'⟩ => exact ⟨s', 1, hs', Or.inl rfl⟩
  match h12 with
  | ⟨s', t, hs', ht⟩ => exact combine23 _ b r s' t hb hr hs' ht

/-- x = K(f + 1): x² + x = K·J + K, J = Kf² + (2K + 1)f + K (K an atom; K = 12 below). -/
theorem hx_gen (K f : Nat) : (K * (f + 1)) * (K * (f + 1)) + K * (f + 1) = K * (K * f * f + (2 * K + 1) * f + K) + K := by lib_nat

/-- 2(Kj + 9)² + 8(Kj + 9) + 5 = (Kj² + 22j)·2K + 239 (K an atom). -/
theorem m_gen (K j : Nat) : 2 * (K * j + 9) * (K * j + 9) + 8 * (K * j + 9) + 5 = (K * j * j + 22 * j) * (2 * K) + 239 := by lib_nat

/-- W = 12j + 9 ⟹ 2W² + 8W + 5 ≡ 23 (mod 24). -/
theorem m23 (W j : Nat) (h : W = 12 * j + 9) : 2 * W * W + 8 * W + 5 = (12 * j * j + 22 * j + 9) * 24 + 23 := by
  rw [h, m_gen]
  generalize 12 * j * j + 22 * j = A
  lib_nat

/-- q | x ⟹ q ∤ 2W² + 8W + 5 when W + 3 = x² + x. -/
theorem bound23 (x W r Q : Nat) (hx : x = r * (Q + 1 + 1)) (hW : W + 3 = x * x + x)
    (hqM : Dv (Q + 1 + 1) (2 * W * W + 8 * W + 5)) : False := by
  have e1 : 2 * W * W + 8 * W + 5 + 1 = x * (2 * (x + 1) * (W + 1)) := by
    rw [show 2 * W * W + 8 * W + 5 + 1 = 2 * (W + 3) * (W + 1) by lib_nat, hW]; lib_nat
  have h1 : Dv (Q + 1 + 1) (2 * W * W + 8 * W + 5 + 1) := ⟨r * (2 * (x + 1) * (W + 1)), by
    rw [e1]
    have e2 : x * (2 * (x + 1) * (W + 1)) = r * (Q + 1 + 1) * (2 * (x + 1) * (W + 1)) :=
      congrArg (fun z => z * (2 * (x + 1) * (W + 1))) hx
    rw [e2]; lib_nat⟩
  exact dv_one_absurd Q (dv_sub (Q + 1) _ 1 hqM h1)

/-- **Infinitely many primes p ≡ 23 (mod 24).** -/
theorem inf_23mod24 (B : Nat) : ∃ c, (∀ d, Dv d (c * 24 + 23) → d = 1 ∨ d = c * 24 + 23) ∧ B < c * 24 + 23 := by
  match fac_pos B with
  | ⟨f, hf⟩ =>
  have hW := hx_gen 12 f
  generalize hX : 12 * (f + 1) = x at hW
  generalize hJ : 12 * f * f + (2 * 12 + 1) * f + 12 = j at hW
  have hW' : (12 * j + 9) + 3 = x * x + x := hW.symm
  generalize hWd : 12 * j + 9 = W at hW'
  have hD : (2 * x + 1) * (2 * x + 1) = 4 * W + 13 := by
    rw [show (2 * x + 1) * (2 * x + 1) = 4 * (x * x + x) + 1 by lib_nat, ← hW']; lib_nat
  have h23 := m23 W j hWd.symm
  match Inf17mod24.has_gen 21 (2 * W * W + 8 * W + 5) (Nat.le_of_ble_eq_true rfl)
      (fun l hq hdv => allowed23 W (2 * x + 1) _ l hD h23 hq hdv)
      _ _ (Nat.le_refl _) ⟨1, (Nat.one_mul _).symm⟩ ⟨_, h23⟩ with
  | ⟨q, hq, ⟨c, hc⟩, hqN⟩ =>
    have hq' : q = c * 24 + 21 + 1 + 1 := by rw [hc]
    refine ⟨c, by rw [← hc]; exact hq, ?_⟩
    cases Nat.lt_or_ge B (c * 24 + 23) with
    | inl h => exact h
    | inr h =>
      exfalso
      match dv_fac B (c * 24 + 23) (Nat.le_add_left 1 _) h with
      | ⟨r, hr⟩ =>
        rw [hq'] at hqN
        exact bound23 x W (12 * r) (c * 24 + 21) (by rw [← hX, ← hf, hr]; lib_nat) hW' hqN

end Inf23mod24

#print axioms Inf23mod24.not3'
#print axioms Inf23mod24.two_mod8
#print axioms Inf23mod24.combine23
#print axioms Inf23mod24.t_sq
#print axioms Inf23mod24.s_sq
#print axioms Inf23mod24.allowed23
#print axioms Inf23mod24.hx_gen
#print axioms Inf23mod24.m_gen
#print axioms Inf23mod24.m23
#print axioms Inf23mod24.bound23
#print axioms Inf23mod24.inf_23mod24

lang/Inf2e1.lean

lean_all/lang/Inf2e1.lean

/-
  lang/Inf2e1 — T43, cycle 304: infinitely many primes p ≡ 2^(s+2) + 1 (mod 2^(s+3)), for every s (5 mod 8, 9 mod 16, 17 mod 32, …).

  x = 2O² + 1 ≡ 3 (mod 8) (O the odd factorial). Lifting the exponent: x^(2^(k+1)) = 8·2^k·(2u + 1) + 1 (lte), so
  x^(2^(s+1)) + 1 = 2M with M = u·2^(s+3) + 2^(s+2) + 1. A prime factor q of M has x^(2^(s+1)) = −1, x^(2^(s+2)) = 1 in 𝔽_q, so
  2^(s+2) | q − 1 (CycloPow.dvd_of_pow): q ≡ 1 or 2^(s+2) + 1 (mod 2^(s+3)). Descent (hasG) gives a factor in the class, and an odd
  q ≤ B divides O, hence x^n − 1, hence 2.
    pow_add', sq_pow, lte, one_pow_dv, uniq_remG, modG, cofactorG, hasG, allowedG, inf_2e1
  The classes of the primes that split in ℚ(ζ_{2^(s+2)}) but not in ℚ(ζ_{2^(s+3)}).
  Lean 4 core only, proofs by hand (lib_nat, lib_int), no axioms.
-/
import lang.Inf9mod16

namespace Inf2e1

open FTAChain ZMod InfSplit Inf9mod16

theorem pow_add' (a m : Nat) : ∀ n, a ^ (m + n) = a ^ m * a ^ n
  | 0 => by show a ^ m = a ^ m * 1; generalize a ^ m = y; lib_nat
  | n + 1 => by
    show a ^ (m + n) * a = a ^ m * (a ^ n * a)
    rw [pow_add' a m n]; generalize a ^ m = y; generalize a ^ n = z; lib_nat

theorem two_pow (k : Nat) : 2 ^ (k + 1) = 2 ^ k + 2 ^ k := by
  show 2 ^ k * 2 = _; generalize 2 ^ k = t; lib_nat

theorem sq_pow (a k : Nat) : a ^ (2 ^ (k + 1)) = a ^ (2 ^ k) * a ^ (2 ^ k) := by
  rw [two_pow, pow_add']

theorem pos_pow : ∀ s, ∃ R', 2 ^ s = R' + 1
  | 0 => ⟨0, rfl⟩
  | s + 1 => by
    match pos_pow s with
    | ⟨R', h⟩ => exact ⟨R' + R' + 1, by show 2 ^ s * 2 = _; rw [h]; lib_nat⟩

theorem base_sq (w : Nat) : (8 * w + 3) * (8 * w + 3) = 8 * 1 * (2 * (4 * (w * w) + 3 * w) + 1) + 1 := by
  rw [x2g 8 w]
  show 8 * (8 * w * w + 6 * w) + 9 = _
  rw [show 8 * w * w = 8 * (w * w) by lib_nat]
  generalize w * w = W
  lib_nat

theorem step_sq (R u : Nat) : (8 * R * (2 * u + 1) + 1) * (8 * R * (2 * u + 1) + 1)
    = 8 * (R * 2) * (2 * (2 * R * ((2 * u + 1) * (2 * u + 1)) + u) + 1) + 1 := by lib_nat

/-- **Lifting the exponent**: x ≡ 3 (mod 8) ⟹ x^(2^(k+1)) = 8·2^k·(2u + 1) + 1. -/
theorem lte (w : Nat) : ∀ k, ∃ u, (8 * w + 3) ^ (2 ^ (k + 1)) = 8 * 2 ^ k * (2 * u + 1) + 1
  | 0 => ⟨4 * (w * w) + 3 * w, by
      show 1 * (8 * w + 3) * (8 * w + 3) = 8 * 1 * _ + 1
      rw [show 1 * (8 * w + 3) * (8 * w + 3) = (8 * w + 3) * (8 * w + 3) by generalize 8 * w + 3 = y; lib_nat]
      exact base_sq w⟩
  | k + 1 => by
    match lte w k with
    | ⟨u, hu⟩ =>
      refine ⟨2 * 2 ^ k * ((2 * u + 1) * (2 * u + 1)) + u, ?_⟩
      rw [sq_pow, hu]
      exact step_sq (2 ^ k) u

/-- (O·Z + 1)^n = O·Y + 1. -/
theorem one_pow_dv (O Z : Nat) : ∀ n, ∃ Y, (O * Z + 1) ^ n = O * Y + 1
  | 0 => ⟨0, by show 1 = _; lib_nat⟩
  | n + 1 => by
    match one_pow_dv O Z n with
    | ⟨Y, hY⟩ => exact ⟨Y * Z * O + Y + Z, by show (O * Z + 1) ^ n * (O * Z + 1) = _; rw [hY]; lib_nat⟩

theorem uniq_remG (Nm x y r c : Nat) (hr : r < Nm) (hc : c < Nm) (h : x * Nm + r = y * Nm + c) : r = c := by
  have big : ∀ x y r c : Nat, r < Nm → x < y → x * Nm + r = y * Nm + c → False := fun x y r c hr hxy h => by
    have h1 : x * Nm + r < (x + 1) * Nm := by rw [Nat.succ_mul]; exact Nat.add_lt_add_left hr _
    have h2 : (x + 1) * Nm ≤ y * Nm := Nat.mul_le_mul_right Nm hxy
    exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (h ▸ h1) (Nat.le_trans h2 (Nat.le_add_right _ _)))
  cases Nat.lt_or_ge x y with
  | inl hxy => exact absurd h (fun h' => big x y r c hr hxy h')
  | inr hxy =>
    cases Nat.lt_or_ge y x with
    | inl hyx => exact absurd h.symm (fun h' => big y x c r hc hyx h')
    | inr hyx =>
      have e : x = y := Nat.le_antisymm hyx hxy
      subst e
      apply Int.ofNat.inj
      have := congrArg Int.ofNat h
      have e' : Int.ofNat x * Int.ofNat Nm + Int.ofNat r = Int.ofNat x * Int.ofNat Nm + Int.ofNat c := this
      calc Int.ofNat r = (Int.ofNat x * Int.ofNat Nm + Int.ofNat r) - Int.ofNat x * Int.ofNat Nm := by lib_int
        _ = Int.ofNat c := by rw [e']; lib_int

theorem modG (M' n : Nat) : ∃ a r, n = a * (M' + 1) + r ∧ r < M' + 1 :=
  match div_exists M' n with
  | ⟨a, r, k, hn, hr⟩ => ⟨a, r, hn, Nat.lt_of_lt_of_le (Nat.lt_succ_self r) (by rw [← hr]; exact Nat.succ_le_succ (Nat.le_add_right r k))⟩

theorem cofactorG (M' A b N' e : Nat) (hA : A < M' + 1) (h : (b * (M' + 1) + 1) * N' = e * (M' + 1) + A) : ∃ c, N' = c * (M' + 1) + A :=
  match modG M' N' with
  | ⟨c, r, hc, hr⟩ => by
    have e' : (b * (M' + 1) + 1) * N' = (b * (M' + 1) * c + b * r + c) * (M' + 1) + r := by rw [hc]; lib_nat
    rw [e'] at h
    exact ⟨c, by rw [hc, uniq_remG (M' + 1) _ _ r A hr hA h]⟩

/-- **Descent** for the class a' + 2 modulo M' + 1. -/
theorem hasG (M' a' N0 : Nat) (ha : a' + 1 + 1 < M' + 1)
    (hall : ∀ l, (∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1) → Dv (l + 1 + 1) N0 →
      ∃ c t, l + 1 + 1 = c * (M' + 1) + t ∧ (t = 1 ∨ t = a' + 1 + 1)) :
    ∀ G N, N ≤ G → Dv N N0 → (∃ e, N = e * (M' + 1) + (a' + 1 + 1)) →
    ∃ q, (∀ d, Dv d q → d = 1 ∨ d = q) ∧ (∃ c, q = c * (M' + 1) + (a' + 1 + 1)) ∧ Dv q N
  | 0, N, hN, _, ⟨e, he⟩ => absurd (he ▸ hN) (fun h => Nat.not_succ_le_zero _ (Nat.le_trans (Nat.le_add_left _ _) h))
  | G + 1, N, hN, hN0, ⟨e, he⟩ => by
    match lp_exists (e * (M' + 1) + a') with
    | ⟨l, hl, hmin⟩ =>
    have hq := lp_prime _ l hl hmin
    have hlN : Dv (l + 1 + 1) N := by rw [he]; exact hl
    match hall l hq (dv_trans _ _ _ hlN hN0) with
    | ⟨c, t, hc, ht⟩ =>
      cases ht with
      | inr ha' => exact ⟨l + 1 + 1, hq, ⟨c, by rw [hc, ha']⟩, hlN⟩
      | inl h1 =>
        match hlN with
        | ⟨N', hN'⟩ =>
        match cofactorG M' (a' + 1 + 1) c N' e ha (by rw [← he, hN', hc, h1]; lib_nat) with
        | ⟨d, hd⟩ =>
        have hlt : N' ≤ G := by
          have e' : N = N' + (N' * (l + 1)) := by rw [hN']; lib_nat
          have h1' : 1 ≤ N' * (l + 1) := by rw [hd]; exact Nat.mul_le_mul (Nat.le_add_left 1 _) (Nat.le_add_left 1 _)
          exact Nat.le_of_succ_le_succ (Nat.le_trans (Nat.add_le_add_left h1' N') (by rw [← e']; exact hN))
        have hN'0 : Dv N' N0 := dv_trans _ _ _ ⟨l + 1 + 1, by rw [hN']; lib_nat⟩ hN0
        match hasG M' a' N0 ha hall G N' hlt hN'0 ⟨d, hd⟩ with
        | ⟨q, hq', hqa, hqN⟩ => exact ⟨q, hq', hqa, dv_trans q N' N hqN ⟨l + 1 + 1, by rw [hN']; lib_nat⟩⟩

/-- prime factors of M (odd, 2M = x^(2^(s+1)) + 1) are ≡ 1 or 4·2^s + 1 (mod 8·2^s). -/
theorem allowedG (s x M l : Nat) (hM : x ^ (2 ^ (s + 1)) + 1 = 2 * M) (hodd : ∀ r, M ≠ 2 * r)
    (hq : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1) (hdv : Dv (l + 1 + 1) M) :
    ∃ c t, l + 1 + 1 = c * (8 * 2 ^ s) + t ∧ (t = 1 ∨ t = 4 * 2 ^ s + 1) := by
  have hq2 : l + 1 + 1 ≠ 2 := fun e => by
    rw [e] at hdv
    match hdv with
    | ⟨w, hw⟩ => exact hodd w (by rw [hw]; lib_nat)
  have hdv' : Dv (l + 1 + 1) (x ^ (2 ^ (s + 1)) + 1) := by
    match hdv with
    | ⟨w, hw⟩ => exact ⟨2 * w, by rw [hM, hw]; lib_nat⟩
  have hxfull : Frobenius.pw (ring (l + 1)) (mk (l + 1) x) ((0 + 1 + 1) ^ (s + 1 + 1)) = (ring (l + 1)).one := by
    show Frobenius.pw (ring (l + 1)) (mk (l + 1) x) (2 ^ (s + 1 + 1)) = _
    rw [PrimeOneModQ.mk_pow, sq_pow]; exact Inf1mod24.sq_one l _ hdv'
  have hX1 : Frobenius.pw (ring (l + 1)) (mk (l + 1) x) ((0 + 1 + 1) ^ (s + 1)) ≠ (ring (l + 1)).one := by
    show Frobenius.pw (ring (l + 1)) (mk (l + 1) x) (2 ^ (s + 1)) ≠ _
    rw [PrimeOneModQ.mk_pow]; exact Inf1mod24.ne_one l _ hq2 hdv'
  match CycloPow.dvd_of_pow l hq 0 (s + 1) StrongMult1.two_prime _ hxfull hX1 with
  | ⟨k, hk⟩ =>
  have hk' : l + 1 + 1 = k * (4 * 2 ^ s) + 1 := by
    rw [hk]; show k * (2 ^ s * 2 * 2) + 1 = _; generalize 2 ^ s = R; lib_nat
  match par k with
  | ⟨j, hj⟩ =>
    cases hj with
    | inl h => exact ⟨j, 1, by rw [hk', h]; generalize 2 ^ s = R; lib_nat, Or.inl rfl⟩
    | inr h => exact ⟨j, 4 * 2 ^ s + 1, by rw [hk', h]; generalize 2 ^ s = R; lib_nat, Or.inr rfl⟩

/-- **Infinitely many primes p ≡ 2^(s+2) + 1 (mod 2^(s+3))**, written with R = 2^s: p = c·8R + 4R + 1. -/
theorem inf_2e1 (s B : Nat) : ∃ c, (∀ d, Dv d (c * (8 * 2 ^ s) + (4 * 2 ^ s + 1)) → d = 1 ∨ d = c * (8 * 2 ^ s) + (4 * 2 ^ s + 1))
    ∧ B < c * (8 * 2 ^ s) + (4 * 2 ^ s + 1) := by
  match oddf_sq B, pos_pow s with
  | ⟨C, hC⟩, ⟨R', hR⟩ =>
  have hx : 2 * (oddf B * oddf B) + 1 = 8 * (2 * C) + 3 := by rw [hC]; lib_nat
  match lte (2 * C) s with
  | ⟨u, hu⟩ =>
  have hM : (8 * (2 * C) + 3) ^ (2 ^ (s + 1)) + 1 = 2 * (u * (8 * 2 ^ s) + (4 * 2 ^ s + 1)) := by
    rw [hu]; generalize 2 ^ s = R; lib_nat
  have hodd : ∀ r, u * (8 * 2 ^ s) + (4 * 2 ^ s + 1) ≠ 2 * r := fun r e =>
    odd_not_dv2 (u * (4 * 2 ^ s) + 2 * 2 ^ s) r (by rw [show 2 * (u * (4 * 2 ^ s) + 2 * 2 ^ s) + 1 = u * (8 * 2 ^ s) + (4 * 2 ^ s + 1) by generalize 2 ^ s = R; lib_nat, e]; lib_nat)
  have hM' : 8 * 2 ^ s = (8 * R' + 7) + 1 := by rw [hR]; lib_nat
  have hA' : 4 * 2 ^ s + 1 = (4 * R' + 3) + 1 + 1 := by rw [hR]; lib_nat
  have hlt : (4 * R' + 3) + 1 + 1 < (8 * R' + 7) + 1 := Nat.succ_le_succ (by
    show 4 * R' + 3 + 1 + 1 ≤ 8 * R' + 7
    rw [show 8 * R' + 7 = (4 * R' + 3 + 1 + 1) + (4 * R' + 2) by lib_nat]; exact Nat.le_add_right _ _)
  match hasG (8 * R' + 7) (4 * R' + 3) (u * (8 * 2 ^ s) + (4 * 2 ^ s + 1)) hlt
      (fun l hq hdv => by
        match allowedG s _ _ l hM hodd hq hdv with
        | ⟨c, t, hc, ht⟩ => exact ⟨c, t, by rw [hc, hM'], by rw [← hA']; exact ht⟩)
      (u * (8 * 2 ^ s) + (4 * 2 ^ s + 1)) (u * (8 * 2 ^ s) + (4 * 2 ^ s + 1)) (Nat.le_refl _) ⟨1, by lib_nat⟩ ⟨u, by rw [hM', hA']⟩ with
  | ⟨q, hq, ⟨c, hc⟩, hqM⟩ =>
    have hc' : q = c * (8 * 2 ^ s) + (4 * 2 ^ s + 1) := by rw [hc, hM', hA']
    refine ⟨c, by rw [← hc']; exact hq, ?_⟩
    rw [← hc']
    cases Nat.lt_or_ge B q with
    | inl h => exact h
    | inr h =>
      exfalso
      have hq2k : q = 2 * (c * (4 * 2 ^ s) + 2 * 2 ^ s) + 1 := by rw [hc']; generalize 2 ^ s = R; lib_nat
      have hk : c * (4 * 2 ^ s) + 2 * 2 ^ s < B := by
        have h2 : c * (4 * 2 ^ s) + 2 * 2 ^ s < 2 * (c * (4 * 2 ^ s) + 2 * 2 ^ s) + 1 :=
          Nat.lt_succ_of_le (by rw [show 2 * (c * (4 * 2 ^ s) + 2 * 2 ^ s) = (c * (4 * 2 ^ s) + 2 * 2 ^ s) + (c * (4 * 2 ^ s) + 2 * 2 ^ s) by generalize c * (4 * 2 ^ s) + 2 * 2 ^ s = y; lib_nat]; exact Nat.le_add_left _ _)
        exact Nat.lt_of_lt_of_le h2 (by rw [← hq2k]; exact h)
      have hqO : Dv q (oddf B) := by rw [hq2k]; exact oddf_dv B _ hk
      match one_pow_dv (oddf B) (2 * oddf B) (2 ^ (s + 1)) with
      | ⟨Y, hY⟩ =>
      have hq2M : Dv q (oddf B * Y + 1 + 1) := by
        rw [← hY, show oddf B * (2 * oddf B) + 1 = 8 * (2 * C) + 3 by rw [← hx]; generalize oddf B = o; lib_nat, hM]
        match hqM with
        | ⟨w, hw⟩ => exact ⟨2 * w, by rw [hw]; lib_nat⟩
      have hqOY : Dv q (oddf B * Y) := Inf17mod24.dv_mulr _ _ _ hqO
      have hq' : q = (c * (8 * 2 ^ s) + 4 * 2 ^ s) + 1 := by rw [hc']; generalize 2 ^ s = R; lib_nat
      rw [hq'] at hqOY hq2M
      have h2 := dv_sub (c * (8 * 2 ^ s) + 4 * 2 ^ s) _ 2 hqOY (by rw [show oddf B * Y + 2 = oddf B * Y + 1 + 1 from rfl]; exact hq2M)
      have hge : c * (8 * 2 ^ s) + 4 * 2 ^ s = (c * (8 * R' + 8) + 4 * R' + 2) + 1 + 1 := by rw [hR]; lib_nat
      rw [hge] at h2
      cases StrongMult1.two_prime _ h2 with
      | inl e => exact Nat.noConfusion (Nat.succ.inj e)
      | inr e => exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj e))

end Inf2e1

#print axioms Inf2e1.pow_add'
#print axioms Inf2e1.two_pow
#print axioms Inf2e1.sq_pow
#print axioms Inf2e1.pos_pow
#print axioms Inf2e1.base_sq
#print axioms Inf2e1.step_sq
#print axioms Inf2e1.lte
#print axioms Inf2e1.one_pow_dv
#print axioms Inf2e1.uniq_remG
#print axioms Inf2e1.modG
#print axioms Inf2e1.cofactorG
#print axioms Inf2e1.hasG
#print axioms Inf2e1.allowedG
#print axioms Inf2e1.inf_2e1

lang/Inf2em1.lean

lean_all/lang/Inf2em1.lean

/-
  lang/Inf2em1 — T43, cycle 505: **p ≡ 2^(j+2) − 1 (mod 2^(j+3)) beyond every bound, for every j** (7 mod 16, 15 mod 32, …).

  x = O (odd factorial up to B), N₀ = v_j(x² + 2) = uv(x², j) + 2 ≡ 2^(j+2) − 1 (mod 2^(j+3)) and odd (Inf2em1a.uv_exact).
  Every prime factor q of N₀ is odd with a root of v_j(x² + 2), so q ≡ 1 or 2^(j+2) − 1 (Two2m.root2m); some factor is in the
  second class (hasGc). An odd q ≤ B divides x, and then N₀ ≡ v_j(2) = 2 (mod q), impossible.
  With Inf3mod8 (j = 0) and InfCycloPow/Inf2e1/Two2Inf this gives every class a mod 2^k with a² ≡ 1, i.e. every Euclidean class.
    inf_2em1
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Inf2em1a
import lang.Two2m
import lang.Fp2Sqrt

namespace Two2

open FTAChain ZMod Cheb

theorem inf_2em1 (j B : Nat) : ∃ p, (∀ k, Dv k p → k = 1 ∨ k = p) ∧ B < p ∧ Dv (2 ^ (j + 3)) (p + 1 + 2 ^ (j + 2)) := by
  have h3 : 3 ≤ 2 ^ (j + 2) := Nat.le_trans (Nat.le_add_left 3 1)
    (Nat.mul_le_mul (Nat.mul_le_mul (pow_pos' 2 (Nat.le_add_left 1 1) j) (Nat.le_refl 2)) (Nat.le_refl 2))
  obtain ⟨c', hc'⟩ := le_dest2 3 _ h3
  have hL : 2 ^ (j + 3) = 2 * c' + 5 + 1 := by
    show 2 ^ (j + 2) * 2 = _; rw [← hc']; lib_nat
  obtain ⟨a, ha⟩ := ofac_odd (B + 1)
  obtain ⟨e, f, he, hf⟩ := uv_exact a j
  rw [← ha] at he hf
  generalize hx : ofac (B + 1) = x at he hf ha
  -- N₀ = uv(x², j) + 2
  have hcls : uv (x * x) j + 2 = e * (2 * c' + 5 + 1) + (c' + 2) := by
    apply add_rc 1
    calc uv (x * x) j + 2 + 1 = 2 ^ (j + 2) * (2 * e + 1) := he
      _ = (c' + 3) * (2 * e + 1) := by rw [hc']
      _ = _ := by lib_nat
  have hodd : ∀ r, uv (x * x) j + 2 = r * 2 → False := fun r hr => by
    apply InfSplit.odd_not_dv2 (2 * f + 1) r
    rw [← hr]
    calc 2 * (2 * f + 1) + 1 = uv (x * x) j + 1 + 1 := by rw [hf]
      _ = _ := rfl
  -- q odd prime dividing N₀ has the root
  have hall : ∀ l, (∀ k, Dv k (l + 1 + 1) → k = 1 ∨ k = l + 1 + 1) → Dv (l + 1 + 1) (uv (x * x) j + 2) →
      (∃ b, l + 1 + 1 = b * (2 * c' + 5 + 1) + 1) ∨ (∃ b, l + 1 + 1 = b * (2 * c' + 5 + 1) + (c' + 2)) := by
    intro l hq hdv
    rcases EveryPrime.nat_even_odd l with ⟨i, hi⟩ | ⟨m, hm⟩
    · exfalso
      have h2 : l + 1 + 1 = 2 := by
        cases hq 2 ⟨i + 1, by rw [hi]; lib_nat⟩ with
        | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
        | inr e => exact e.symm
      rw [h2] at hdv
      obtain ⟨r, hr⟩ := hdv
      exact hodd r hr
    · subst hm
      have hit := it_emb (ring (2 * m + 1 + 1)) (x * x) j
      rw [emb_dv _ _ hdv, emb_add, emb_mul] at hit
      have hx2 : emb (ring (2 * m + 1 + 1)) 2 = (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one := by
        show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).one) (ring (2 * m + 1 + 1)).one = _
        lib_ring
      rw [hx2] at hit
      rcases root2m m hq j (Fp2Sqrt.two_ne m) _ hit with hb | hb
      · rw [hL] at hb
        obtain ⟨b, hb⟩ := hb
        exact Or.inl ⟨b, by rw [← hb]⟩
      · rw [hL, ← hc'] at hb
        obtain ⟨b, hb⟩ := hb
        match b, hb with
        | 0, hb => exact absurd (hb.trans (Nat.zero_mul _)) (fun h' => Nat.noConfusion h')
        | b + 1, hb =>
          refine Or.inr ⟨b, add_rc (c' + 3 + 1) _ _ ?_⟩
          calc 2 * m + 1 + 1 + 1 + (c' + 3 + 1) = 2 * m + 1 + 1 + 1 + 1 + (c' + 3) := by lib_nat
            _ = (b + 1) * (2 * c' + 5 + 1) := hb
            _ = _ := by lib_nat
  have hcL : c' + 2 < 2 * c' + 5 + 1 := Nat.succ_le_succ (Nat.le_trans (Nat.le_add_right (c' + 2) (c' + 3)) (Nat.le_of_eq (by lib_nat)))
  obtain ⟨q, hq, ⟨b, hb⟩, hqN⟩ := hasGc (2 * c' + 5) c' _ hcL hall _ _ (Nat.le_refl _) (dv_refl _) ⟨e, hcls⟩
  refine ⟨q, hq, ?_, ⟨b + 1, by rw [hL, hb, ← hc']; lib_nat⟩⟩
  cases Nat.lt_or_ge B q with
  | inl h => exact h
  | inr h =>
    exfalso
    rcases EveryPrime.nat_even_odd q with ⟨i, hi⟩ | ⟨i, hi⟩
    · obtain ⟨r, hr⟩ := dv_trans 2 q _ ⟨i, by rw [hi]; lib_nat⟩ hqN
      exact hodd r hr
    · have hqx : Dv q x := by
        have hB : 2 * i + 1 ≤ B := by rw [hi] at h; exact h
        have hle : i ≤ 2 * i + 1 := Nat.le_succ_of_le (Nat.le_trans (Nat.le_add_left i i) (Nat.le_of_eq (by lib_nat)))
        rw [← hx, hi]; exact dv_ofac (B + 1) i (Nat.succ_le_succ (Nat.le_trans hle hB))
      rw [hi] at hqx hqN
      have hit := it_emb (ring (2 * i)) (x * x) j
      rw [emb_dv _ _ hqN, emb_add, emb_mul, emb_dv _ _ hqx] at hit
      have h2 : emb (ring (2 * i)) 2 = (ring (2 * i)).zero := by
        have e2 : (ring (2 * i)).add ((ring (2 * i)).mul (ring (2 * i)).zero (ring (2 * i)).zero) (emb (ring (2 * i)) 2) =
            (ring (2 * i)).add (ring (2 * i)).one (ring (2 * i)).one := by
          show _ = (ring (2 * i)).add (ring (2 * i)).one (ring (2 * i)).one
          show (ring (2 * i)).add _ ((ring (2 * i)).add ((ring (2 * i)).add (ring (2 * i)).zero (ring (2 * i)).one) (ring (2 * i)).one) = _
          lib_ring
        rw [e2, it_two] at hit
        rw [show emb (ring (2 * i)) 2 = (ring (2 * i)).add (ring (2 * i)).one (ring (2 * i)).one by
          show (ring (2 * i)).add ((ring (2 * i)).add (ring (2 * i)).zero (ring (2 * i)).one) (ring (2 * i)).one = _; lib_ring]
        exact hit
      obtain ⟨r, hr⟩ := dv_of_emb (2 * i) 2 h2
      have hq3 : 2 ≤ 2 * i := by
        have h22 : 2 ≤ 2 * i + 1 := Nat.le_trans (Nat.le_add_left 2 c') (by rw [← hi, hb]; exact Nat.le_add_left _ _)
        match i, h22 with
        | 0, h22 => exact absurd h22 (Nat.not_succ_le_self 1)
        | i + 1, _ => exact Nat.le_trans (Nat.le_of_eq (by lib_nat)) (Nat.mul_le_mul_left 2 (Nat.le_add_left 1 i))
      match r, hr with
      | 0, hr => exact absurd (hr.trans (Nat.zero_mul _)) (fun h' => Nat.noConfusion h')
      | r + 1, hr =>
        have hle : 2 * i + 1 ≤ 2 := Nat.le_trans (Nat.le_add_left (2 * i + 1) (r * (2 * i + 1))) (Nat.le_of_eq ((Nat.succ_mul r (2 * i + 1)).symm.trans hr.symm))
        exact Nat.not_succ_le_self 2 (Nat.le_trans (Nat.succ_le_succ hq3) hle)

end Two2

#print axioms Two2.inf_2em1

lang/Inf2em1a.lean

lean_all/lang/Inf2em1a.lean

/-
  lang/Inf2em1a — T43, cycle 504: Euclid descent for a class c with allowed factors {1, c} mod L, and the 2-adic size of
  v_j(x² + 2) + 1 for odd x.

    cofactorG   (bL + 1)·N′ = aL + c, c < L ⇒ N′ ≡ c (mod L)
    hasGc       every prime factor of N₀ is ≡ 1 or ≡ c (mod L), c ≥ 2, N | N₀, N ≡ c ⇒ a prime q | N with q ≡ c (mod L)
    uv_exact    x odd: uv(x², j) + 3 = 2^(j+2)·odd and uv(x², j) + 1 = 2·odd   (v + 1 = (u + 1)(u + 3), Two2Inf.uv)
    it_two      v_j(2) = 2
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Two2Inf
import lang.ChebInf
import lang.LucMul

namespace Two2

open FTAChain ZMod Cheb

theorem cofactorG (L' b N' a c : Nat) (hc : c < L' + 1) (h : (b * (L' + 1) + 1) * N' = a * (L' + 1) + c) :
    ∃ d, N' = d * (L' + 1) + c :=
  match modC L' N' with
  | ⟨d, r, hd, hr⟩ => by
    have e : (b * (L' + 1) + 1) * N' = (b * (L' + 1) * d + b * r + d) * (L' + 1) + r := by rw [hd]; lib_nat
    rw [e] at h
    exact ⟨d, by rw [hd, Nine.uniq_rem (L' + 1) _ _ r c hr hc h]⟩

theorem hasGc (L' c' N0 : Nat) (hcL : c' + 2 < L' + 1)
    (hall : ∀ l, (∀ e, Dv e (l + 1 + 1) → e = 1 ∨ e = l + 1 + 1) → Dv (l + 1 + 1) N0 →
      (∃ a, l + 1 + 1 = a * (L' + 1) + 1) ∨ (∃ a, l + 1 + 1 = a * (L' + 1) + (c' + 2))) :
    ∀ G N, N ≤ G → Dv N N0 → (∃ a, N = a * (L' + 1) + (c' + 2)) →
    ∃ q, (∀ e, Dv e q → e = 1 ∨ e = q) ∧ (∃ b, q = b * (L' + 1) + (c' + 2)) ∧ Dv q N
  | 0, N, hN', _, ⟨a, ha⟩ => absurd (ha ▸ hN') (fun h => Nat.not_succ_le_zero _ (Nat.le_trans (Nat.le_add_left _ _) h))
  | G + 1, N, hNG, hN0, ⟨a, ha⟩ => by
    obtain ⟨l, hl', hmin⟩ := lp_exists (a * (L' + 1) + c')
    have hq := lp_prime _ l hl' hmin
    have hlN : Dv (l + 1 + 1) N := by rw [ha]; exact hl'
    have hlN0 := dv_trans _ _ _ hlN hN0
    rcases hall l hq hlN0 with ⟨b, hb⟩ | ⟨b, hb⟩
    · obtain ⟨N', hN'⟩ := hlN
      obtain ⟨d, hd⟩ := cofactorG L' b N' a (c' + 2) hcL (by rw [← hb, ← ha, hN']; lib_nat)
      have hlt : N' ≤ G := by
        have e : N = N' + (N' * (l + 1)) := by rw [hN']; lib_nat
        have h1' : 1 ≤ N' * (l + 1) := by rw [hd]; exact Nat.mul_le_mul (Nat.le_add_left 1 _) (Nat.le_add_left 1 _)
        exact Nat.le_of_succ_le_succ (Nat.le_trans (Nat.add_le_add_left h1' N') (by rw [← e]; exact hNG))
      have hN'0 : Dv N' N0 := dv_trans _ _ _ ⟨l + 1 + 1, by rw [hN']; lib_nat⟩ hN0
      obtain ⟨q, hq', hqL, hqN⟩ := hasGc L' c' N0 hcL hall G N' hlt hN'0 ⟨d, hd⟩
      exact ⟨q, hq', hqL, dv_trans q N' N hqN ⟨l + 1 + 1, by rw [hN']; lib_nat⟩⟩
    · exact ⟨l + 1 + 1, hq, ⟨b, hb⟩, hlN⟩

theorem uv_exact (x0 : Nat) : ∀ j, ∃ e f, uv ((2 * x0 + 1) * (2 * x0 + 1)) j + 3 = 2 ^ (j + 2) * (2 * e + 1) ∧
    uv ((2 * x0 + 1) * (2 * x0 + 1)) j + 1 = 2 * (2 * f + 1)
  | 0 => by
    rcases EveryPrime.nat_even_odd x0 with ⟨s, hs⟩ | ⟨s, hs⟩
    · refine ⟨2 * s * s + s, x0 * x0 + x0, ?_, ?_⟩
      · show (2 * x0 + 1) * (2 * x0 + 1) + 3 = 2 * 2 * (2 * (2 * s * s + s) + 1)
        rw [hs]; lib_nat
      · show (2 * x0 + 1) * (2 * x0 + 1) + 1 = _
        lib_nat
    · refine ⟨2 * s * s + 3 * s + 1, x0 * x0 + x0, ?_, ?_⟩
      · show (2 * x0 + 1) * (2 * x0 + 1) + 3 = 2 * 2 * (2 * (2 * s * s + 3 * s + 1) + 1)
        rw [hs]; lib_nat
      · show (2 * x0 + 1) * (2 * x0 + 1) + 1 = _
        lib_nat
  | j + 1 => by
    obtain ⟨e, f, he, hf⟩ := uv_exact x0 j
    show ∃ e f, uv _ j * uv _ j + (uv _ j + uv _ j + uv _ j + uv _ j) + 3 = 2 ^ (j + 2) * 2 * (2 * e + 1) ∧
      uv _ j * uv _ j + (uv _ j + uv _ j + uv _ j + uv _ j) + 1 = 2 * (2 * f + 1)
    generalize uv ((2 * x0 + 1) * (2 * x0 + 1)) j = u at he hf
    have hP : 1 ≤ 2 ^ (j + 1) * (2 * (2 * e * f + e + f) + 1) :=
      Nat.mul_le_mul (pow_pos' 2 (Nat.le_add_left 1 1) (j + 1)) (Nat.le_add_left 1 _)
    obtain ⟨P', hP'⟩ := le_dest2 1 _ hP
    have key : u * u + (u + u + u + u) + 3 = (u + 1) * (u + 3) := by lib_nat
    refine ⟨2 * e * f + e + f, P', ?_, ?_⟩
    · rw [key, he, hf]
      show 2 * (2 * f + 1) * (2 ^ (j + 1) * 2 * (2 * e + 1)) = 2 ^ (j + 1) * 2 * 2 * (2 * (2 * e * f + e + f) + 1)
      generalize 2 ^ (j + 1) = X; lib_nat
    · apply Cheb.add_rc 2
      calc u * u + (u + u + u + u) + 1 + 2 = (u + 1) * (u + 3) := by lib_nat
        _ = 2 * (2 * f + 1) * (2 ^ (j + 1) * 2 * (2 * e + 1)) := by rw [he, hf]; rfl
        _ = 4 * (2 ^ (j + 1) * (2 * (2 * e * f + e + f) + 1)) := by generalize 2 ^ (j + 1) = X; lib_nat
        _ = 4 * (P' + 1) := by rw [hP']
        _ = 2 * (2 * P' + 1) + 2 := by lib_nat

theorem it_two {R : Type} (K : DetChain.CRing R) : ∀ j, it K (K.add K.one K.one) j = K.add K.one K.one
  | 0 => rfl
  | j + 1 => by
    show K.add (K.mul (it K (K.add K.one K.one) j) (it K (K.add K.one K.one) j)) (K.neg (K.add K.one K.one)) = _
    rw [it_two K j]; lib_ring

end Two2

#print axioms Two2.cofactorG
#print axioms Two2.hasGc
#print axioms Two2.uv_exact
#print axioms Two2.it_two

lang/Inf2m3.lean

lean_all/lang/Inf2m3.lean

/-
  lang/Inf2m3 — T43, cycle 540: **p ≡ 2 (mod 3), p ≡ 1 (mod ℓ^(e+1)) beyond every bound, for every prime ℓ ≡ 5 (mod 6) and every e.**

  (ℤ/3)^× has two elements, so a value ≡ 2 (mod 3) has a prime factor ≡ 2 (mod 3). O = 3^v·O′ (Val.split), u = 2ℓO′ (3 ∤ u),
  z = u² ≡ 1 (mod 3), y = z^(ℓ^e) ≡ 1 (mod 3), G = Φ_ℓ(y) ≡ ℓ ≡ 2 (mod 3). Prime factors r of G: r ∤ u, so r ≠ 2, r > B unless r = 3,
  and r ≠ 3 since 3 ∤ G; ChebPow.ordpow gives ℓ^(e+1) | r − 1. ChebPowInf.hasG (modulus 3).
    pow31, gN31, sq31, inf_2m3
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Inf3m4p
import lang.Val
import lang.PhiPow
import lang.StrongMult1

namespace Cheb

open FTAChain ZMod TwoPrime

theorem pow31 (s : Nat) : ∀ k, ∃ t, (3 * s + 1) ^ k = 3 * t + 1
  | 0 => ⟨0, rfl⟩
  | k + 1 => by
    obtain ⟨t, ht⟩ := pow31 s k
    exact ⟨3 * t * s + t + s, by show (3 * s + 1) ^ k * (3 * s + 1) = _; rw [ht]; lib_nat⟩

theorem gN31 (s : Nat) : ∀ n, ∃ k, gN (3 * s + 1) n = 3 * k + n
  | 0 => ⟨0, rfl⟩
  | n + 1 => by
    obtain ⟨k, hk⟩ := gN31 s n
    obtain ⟨t, ht⟩ := pow31 s n
    exact ⟨k + t, by show gN (3 * s + 1) n + (3 * s + 1) ^ n = _; rw [hk, ht]; lib_nat⟩

theorem sq31 (u : Nat) (h3 : Dv 3 u → False) : ∃ s, u * u = 3 * s + 1 := by
  obtain ⟨t, ρ, ht, hρ⟩ := Order.divmod 2 u
  match ρ, hρ with
  | 0, _ => exact absurd ⟨t, by rw [ht]; lib_nat⟩ h3
  | 1, _ => exact ⟨3 * t * t + 2 * t, by rw [ht]; lib_nat⟩
  | 2, _ => exact ⟨3 * t * t + 4 * t + 1, by rw [ht]; lib_nat⟩
  | ρ + 3, hρ => exact absurd hρ (fun h => Nat.lt_irrefl 3 (Nat.lt_of_le_of_lt (Nat.le_add_left 3 ρ) h))

theorem inf_2m3 (c e B : Nat) (hl : ∀ d, Dv d (2 * (3 * c + 1) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (3 * c + 1) + 1 + 1 + 1)) :
    ∃ p, (∀ d, Dv d p → d = 1 ∨ d = p) ∧ B < p ∧ (∃ k, p = 3 * k + 2) ∧ ∃ q, p = q * (2 * (3 * c + 1) + 1 + 1 + 1) ^ (e + 1) + 1 := by
  have h3p : ∀ d, Dv d (1 + 1 + 1) → d = 1 ∨ d = 1 + 1 + 1 := StrongMult1.three_prime
  -- O = 3^v · O′
  have hO1 : 1 ≤ Two2.ofac (B + 1) := by obtain ⟨a, ha⟩ := Two2.ofac_odd (B + 1); rw [ha]; exact Nat.le_add_left 1 _
  obtain ⟨v, O', hvO, hO'⟩ := Val.split 1 _ _ (Nat.le_refl _) hO1
  have h32 : Dv (1 + 1 + 1) 2 → False := fun h => by
    obtain ⟨k, hk⟩ := dv_le _ 1 h
    exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj ((show k + 1 + 1 + 1 = 1 + 1 + 1 + k by lib_nat).trans hk)))
  have h3l : Dv (1 + 1 + 1) (2 * (3 * c + 1) + 1 + 1 + 1) → False := fun h => by
    obtain ⟨k, hk⟩ := h
    exact Nat.noConfusion ((Inf3mod8.uniq_rem 2 k (2 * c + 1) 0 2 (Nat.zero_lt_succ 2) (Nat.lt_succ_self 2)
      ((show k * (2 + 1) + 0 = k * (1 + 1 + 1) by lib_nat).trans (hk.symm.trans (by lib_nat)))))
  generalize hu : 2 * (2 * (3 * c + 1) + 1 + 1 + 1) * O' = u
  have h3u : Dv (1 + 1 + 1) u → False := by
    rw [← hu]; exact Val.ndv_mul 1 _ O' h3p (Val.ndv_mul 1 2 _ h3p h32 h3l) hO'
  obtain ⟨s0, hs0⟩ := sq31 u h3u
  obtain ⟨t, ht⟩ := pow31 s0 ((2 * (3 * c + 1) + 1 + 1 + 1) ^ e)
  rw [← hs0] at ht
  generalize hY : (u * u) ^ ((2 * (3 * c + 1) + 1 + 1 + 1) ^ e) = y at ht
  obtain ⟨k0, hk0⟩ := gN31 t (2 * (3 * c + 1) + 1 + 1 + 1)
  rw [← ht] at hk0
  have hE : 1 ≤ (2 * (3 * c + 1) + 1 + 1 + 1) ^ e := pow_pos' _ (Nat.le_add_left 1 _) e
  obtain ⟨E, hE'⟩ := le_dest2 1 _ hE
  have huy : Dv u y := ⟨(u * u) ^ E * u, by rw [← hY, ← hE']; show (u * u) ^ E * (u * u) = _; lib_nat⟩
  have hlu : Dv (2 * (3 * c + 1) + 1 + 1 + 1) u := ⟨2 * O', by rw [← hu]; lib_nat⟩
  have h2u : Dv 2 u := ⟨(2 * (3 * c + 1) + 1 + 1 + 1) * O', by rw [← hu]; lib_nat⟩
  have hG1 : gN y (2 * (3 * c + 1) + 1 + 1 + 1) = 1 + y * gN y (2 * (3 * c + 1) + 1 + 1) := by
    show gN y ((2 * (3 * c + 1) + 1 + 1) + 1) = _
    rw [Nat.add_comm _ 1, gN_one]
  have hcop : ∀ l, Dv (l + 1 + 1) (gN y (2 * (3 * c + 1) + 1 + 1 + 1)) → Dv (l + 1 + 1) u → False := fun l h1 h2 => by
    rw [hG1] at h1
    exact dv_one_absurd l (dv_sub (l + 1) (y * gN y (2 * (3 * c + 1) + 1 + 1)) 1
      (dv_trans _ _ _ (dv_trans _ _ _ h2 huy) ⟨gN y (2 * (3 * c + 1) + 1 + 1), Nat.mul_comm _ _⟩)
      (by rw [Nat.add_comm (y * gN y (2 * (3 * c + 1) + 1 + 1)) 1]; exact h1))
  have hG2 : gN y (2 * (3 * c + 1) + 1 + 1 + 1) = (k0 + 2 * c + 1) * 3 + 2 := by rw [hk0]; lib_nat
  have hG3 : Dv (1 + 1 + 1) (gN y (2 * (3 * c + 1) + 1 + 1 + 1)) → False := fun h => by
    obtain ⟨k, hk⟩ := h
    exact Nat.noConfusion (Inf3mod8.uniq_rem 2 k (k0 + 2 * c + 1) 0 2 (Nat.zero_lt_succ 2) (Nat.lt_succ_self 2)
      ((show k * (2 + 1) + 0 = k * (1 + 1 + 1) by lib_nat).trans (hk.symm.trans (by rw [hG2]))))
  have hall : ∀ l, (∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1) → Dv (l + 1 + 1) (gN y (2 * (3 * c + 1) + 1 + 1 + 1)) →
      (∃ a, l + 1 + 1 = a * (0 + 3) + 1) ∨ (∃ a, l + 1 + 1 = a * (0 + 3) + (0 + 2)) := by
    intro l hq hdv
    obtain ⟨a, ρ, ha, hρ⟩ := Order.divmod 2 (l + 1 + 1)
    match ρ, hρ, ha with
    | 0, _, ha =>
      exfalso
      rcases hq 3 ⟨a, by rw [ha]; lib_nat⟩ with e | e
      · exact Nat.noConfusion (Nat.succ.inj e)
      · exact hG3 (by rw [show 1 + 1 + 1 = l + 1 + 1 from e]; exact hdv)
    | 1, _, ha => exact Or.inl ⟨a, by rw [ha]⟩
    | 2, _, ha => exact Or.inr ⟨a, by rw [ha]⟩
    | ρ + 3, hρ, _ => exact absurd hρ (fun h => Nat.lt_irrefl 3 (Nat.lt_of_le_of_lt (Nat.le_add_left 3 ρ) h))
  obtain ⟨q, hq, ⟨b, hb⟩, hqG⟩ := hasG 0 _ hall _ _ (Nat.le_refl _) (dv_refl _) ⟨k0 + 2 * c + 1, by rw [hG2]⟩
  have hq2 : 2 ≤ q := by rw [hb]; exact Nat.le_add_left _ _
  obtain ⟨q', hq'⟩ := le_dest2 2 q hq2
  rw [← hq'] at hq hqG hb
  have hq'' : ∀ d, Dv d (q' + 1 + 1) → d = 1 ∨ d = q' + 1 + 1 := hq
  have hqu : Dv (q' + 1 + 1) u → False := hcop q' hqG
  have hodd : ∀ i, q' + 1 + 1 = 2 * i → False := fun i hi => by
    rcases hq'' 2 ⟨i, hi.trans (Nat.mul_comm 2 i)⟩ with e | e
    · exact Nat.noConfusion (Nat.succ.inj e)
    · exact hqu (by rw [← e]; exact h2u)
  refine ⟨q' + 2, hq, ?_, ⟨b, by show q' + 1 + 1 = _; rw [hb]; lib_nat⟩, ?_⟩
  · cases Nat.lt_or_ge B (q' + 2) with
    | inl h => exact h
    | inr h =>
      exfalso
      rcases EveryPrime.nat_even_odd (q' + 2) with ⟨i, hi⟩ | ⟨i, hi⟩
      · exact hodd i hi
      · have hle : i ≤ 2 * i + 1 := Nat.le_succ_of_le (Nat.le_trans (Nat.le_add_left i i) (Nat.le_of_eq (by lib_nat)))
        have hB : 2 * i + 1 ≤ B := by rw [hi] at h; exact h
        have hqO := Two2.dv_ofac (B + 1) i (Nat.succ_le_succ (Nat.le_trans hle hB))
        rw [← hi, hvO] at hqO
        have hP : 1 ≤ (1 + 1 + 1) ^ v := pow_pos' _ (Nat.le_add_left 1 _) v
        obtain ⟨P, hP'⟩ := le_dest2 1 _ hP
        rw [← hP'] at hqO
        rcases euclid_core q' P O' hq'' hqO with e | e
        · rw [hP'] at e
          have e' := PhiSq.dv_pow_prime q' _ hq'' (Nat.le_add_left 1 _) v e
          rcases h3p _ e' with e2 | e2
          · exact Nat.noConfusion (Nat.succ.inj e2)
          · rw [e2] at hb
            exact Nat.noConfusion (Inf3mod8.uniq_rem 2 1 b 0 2 (Nat.zero_lt_succ 2) (Nat.lt_succ_self 2) (by rw [← hb]))
        · exact hqu (by rw [← hu]; exact dv_mul_left _ _ _ e)
  · rcases EveryPrime.nat_even_odd q' with ⟨i, hi⟩ | ⟨m, hm⟩
    · exact absurd (show q' + 1 + 1 = 2 * (i + 1) by rw [hi]; lib_nat) (hodd (i + 1))
    · subst hm
      have hz0 : emb (ring (2 * m + 1 + 1)) (u * u) ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => by
        have h1 := dv_of_emb _ _ e0
        match u, h3u, hqu, h1 with
        | 0, h3u, _, _ => exact h3u ⟨0, rfl⟩
        | u' + 1, _, hqu, h1 =>
          rcases euclid_core (2 * m + 1) u' (u' + 1) hq'' h1 with h | h
          · exact hqu h
          · exact hqu h
      have hg : geo (ring (2 * m + 1 + 1)) (Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) (u * u)) ((2 * (3 * c + 1) + 1 + 1 + 1) ^ e))
          (2 * (3 * c + 1) + 1 + 1 + 1) = (ring (2 * m + 1 + 1)).zero := by
        rw [← Cyclo2l.emb_pow, hY, ← emb_gN]; exact emb_dv _ _ hqG
      rcases ordpow (ring (2 * m + 1 + 1)) (2 * (3 * c + 1) + 1) hl e _ _ hg (Cyclo2l.ferm1 m hq'' _ hz0) with ⟨r, hr⟩ | h
      · exact ⟨r, by show 2 * m + 1 + 1 + 1 = _; rw [hr]⟩
      · exfalso
        rw [geo_one_emb] at h
        rcases hl _ (dv_of_emb _ _ h) with e | e
        · exact Nat.noConfusion (Nat.succ.inj e)
        · exact hqu (by rw [e]; exact hlu)

end Cheb

#print axioms Cheb.pow31
#print axioms Cheb.gN31
#print axioms Cheb.sq31
#print axioms Cheb.inf_2m3

lang/Inf3m4.lean

lean_all/lang/Inf3m4.lean

/-
  lang/Inf3m4 — T43, cycle 531: **p ≡ 3 (mod 4), p ≡ 1 (mod ℓ) beyond every bound, for every odd prime ℓ**
  (the class a ≡ −1 mod 4, a ≡ 1 mod ℓ of (ℤ/4ℓ)^×; Euclidean polynomial Φ_ℓ).

  y = 2ℓ·O (O the odd factorial), G = Φ_ℓ(y) = gN(y, ℓ) = 1 + y + y²·X ≡ 3 (mod 4) since y ≡ 2 (mod 4). A prime r | G has
  ord_r y = ℓ (PhiInv.ord_prime), so ℓ | r − 1; r ∤ y (G ≡ 1 mod y), so r is odd and > B. Prime factors are odd, so ≡ 1 or 3 (mod 4),
  and ChebPowInf.hasG (modulus 4) gives one ≡ 3 (mod 4).
    g_mod4, inf_3m4
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.PhiInv
import lang.ChebPowInf
import lang.Two2Inf
import lang.TwoPrimeInf
import lang.Cyclo2l

namespace Cheb

open FTAChain ZMod TwoPrime

theorem g_mod4 (u j : Nat) : ∃ k, gN (2 * (2 * u + 1)) (j + 1 + 1) = k * 4 + 3 := by
  rw [show j + 1 + 1 = 1 + (1 + j) by lib_nat, gN_one, gN_one]
  generalize gN (2 * (2 * u + 1)) j = X
  exact ⟨u + (2 * u + 1) * (2 * u + 1) * X, by lib_nat⟩

theorem inf_3m4 (c B : Nat) (hl : ∀ e, Dv e (2 * c + 1 + 1 + 1) → e = 1 ∨ e = 2 * c + 1 + 1 + 1) :
    ∃ p, (∀ e, Dv e p → e = 1 ∨ e = p) ∧ B < p ∧ (∃ k, p = 4 * k + 3) ∧ ∃ q, p = q * (2 * c + 1 + 1 + 1) + 1 := by
  obtain ⟨a, ha⟩ := Two2.ofac_odd (B + 1)
  -- y = 2·(ℓ·O), ℓ·O odd
  have hodd : (2 * c + 1 + 1 + 1) * Two2.ofac (B + 1) = 2 * ((2 * c + 1 + 1 + 1) * a + c + 1) + 1 := by rw [ha]; lib_nat
  generalize hy : 2 * ((2 * c + 1 + 1 + 1) * Two2.ofac (B + 1)) = y
  have hy' : y = 2 * (2 * ((2 * c + 1 + 1 + 1) * a + c + 1) + 1) := by rw [← hy, hodd]
  obtain ⟨k0, hk0⟩ := g_mod4 ((2 * c + 1 + 1 + 1) * a + c + 1) (2 * c + 1)
  rw [← hy'] at hk0
  have hly : Dv (2 * c + 1 + 1 + 1) y := ⟨2 * Two2.ofac (B + 1), by rw [← hy]; lib_nat⟩
  -- G ≡ 1 (mod y): no prime factor divides y
  have hG1 : gN y (2 * c + 1 + 1 + 1) = 1 + y * gN y (2 * c + 1 + 1) := by
    show gN y ((2 * c + 1 + 1) + 1) = _
    rw [Nat.add_comm _ 1, gN_one]
  have hcop : ∀ l, Dv (l + 1 + 1) (gN y (2 * c + 1 + 1 + 1)) → Dv (l + 1 + 1) y → False := fun l h1 h2 => by
    rw [hG1] at h1
    exact dv_one_absurd l (dv_sub (l + 1) (y * gN y (2 * c + 1 + 1)) 1 (dv_trans _ _ _ h2 ⟨gN y (2 * c + 1 + 1), Nat.mul_comm _ _⟩)
      (by rw [Nat.add_comm (y * gN y (2 * c + 1 + 1)) 1]; exact h1))
  have h2y : Dv 2 y := ⟨(2 * c + 1 + 1 + 1) * Two2.ofac (B + 1), by rw [← hy]; lib_nat⟩
  -- every prime factor is odd: ≡ 1 or 3 (mod 4)
  have hall : ∀ l, (∀ e, Dv e (l + 1 + 1) → e = 1 ∨ e = l + 1 + 1) → Dv (l + 1 + 1) (gN y (2 * c + 1 + 1 + 1)) →
      (∃ a, l + 1 + 1 = a * (1 + 3) + 1) ∨ (∃ a, l + 1 + 1 = a * (1 + 3) + (1 + 2)) := by
    intro l hq hdv
    rcases EveryPrime.nat_even_odd l with ⟨i, hi⟩ | ⟨m, hm⟩
    · exfalso
      have h2 : l + 1 + 1 = 2 := by
        cases hq 2 ⟨i + 1, by rw [hi]; lib_nat⟩ with
        | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
        | inr e => exact e.symm
      exact hcop l hdv (by rw [h2]; exact h2y)
    · rcases EveryPrime.nat_even_odd m with ⟨i, hi⟩ | ⟨i, hi⟩
      · exact Or.inr ⟨i, by rw [hm, hi]; lib_nat⟩
      · exact Or.inl ⟨i + 1, by rw [hm, hi]; lib_nat⟩
  obtain ⟨q, hq, ⟨b, hb⟩, hqG⟩ := hasG 1 _ hall _ _ (Nat.le_refl _) (dv_refl _) ⟨k0, by rw [hk0]⟩
  have hq2 : 2 ≤ q := by rw [hb]; exact Nat.le_add_left _ _
  obtain ⟨q', hq'⟩ := le_dest2 2 q hq2
  rw [← hq'] at hq hqG hb
  have hq'' : ∀ e, Dv e (q' + 1 + 1) → e = 1 ∨ e = q' + 1 + 1 := hq
  have hqy : Dv (q' + 1 + 1) y → False := hcop q' hqG
  refine ⟨q' + 2, hq, ?_, ⟨b, by show q' + 1 + 1 = _; rw [hb]; lib_nat⟩, ?_⟩
  · cases Nat.lt_or_ge B (q' + 2) with
    | inl h => exact h
    | inr h =>
      exfalso
      rcases EveryPrime.nat_even_odd (q' + 2) with ⟨i, hi⟩ | ⟨i, hi⟩
      · rcases hq'' 2 ⟨i, hi.trans (Nat.mul_comm 2 i)⟩ with e | e
        · exact Nat.noConfusion (Nat.succ.inj e)
        · exact hqy (by rw [← e]; exact h2y)
      · have hle : i ≤ 2 * i + 1 := Nat.le_succ_of_le (Nat.le_trans (Nat.le_add_left i i) (Nat.le_of_eq (by lib_nat)))
        have hB : 2 * i + 1 ≤ B := by rw [hi] at h; exact h
        have hqO := Two2.dv_ofac (B + 1) i (Nat.succ_le_succ (Nat.le_trans hle hB))
        apply hqy
        show Dv (q' + 2) y
        rw [hi, ← hy]
        exact dv_trans _ _ _ hqO ⟨2 * (2 * c + 1 + 1 + 1), by lib_nat⟩
  · -- ℓ | q − 1
    have hO := PhiInv.ord_prime (2 * c + 1) q' y hl hly hqG
    rcases EveryPrime.nat_even_odd q' with ⟨i, hi⟩ | ⟨m, hm⟩
    · exfalso
      rcases hq'' 2 ⟨i + 1, by rw [hi]; lib_nat⟩ with e | e
      · exact Nat.noConfusion (Nat.succ.inj e)
      · exact hqy (by rw [← e]; exact h2y)
    · subst hm
      have hX0 : emb (ring (2 * m + 1 + 1)) y ≠ (ring (2 * m + 1 + 1)).zero := fun e => hqy (dv_of_emb _ _ e)
      obtain ⟨r, hr⟩ := hO.2 _ (Cyclo2l.ferm1 m hq'' _ hX0)
      exact ⟨r, by show 2 * m + 1 + 1 + 1 = _; rw [hr]⟩

end Cheb

#print axioms Cheb.g_mod4
#print axioms Cheb.inf_3m4

lang/Inf3m4p.lean

lean_all/lang/Inf3m4p.lean

/-
  lang/Inf3m4p — T43, cycle 532: **p ≡ 3 (mod 4), p ≡ 1 (mod ℓ^(e+1)) beyond every bound, for every prime ℓ ≡ 3 (mod 4) and every e.**

  z = (ℓO)² ≡ 1 (mod 4), y = z^(ℓ^e) ≡ 1 (mod 4), G = Φ_ℓ(y) = gN(y, ℓ) ≡ ℓ ≡ 3 (mod 4). A prime r | G has z of order ℓ^(e+1)
  in 𝔽_r (ChebPow.ordpow, r ≠ ℓ), so ℓ^(e+1) | r − 1; r ∤ z (G ≡ 1 mod y and z | y), so r is odd and > B; hasG mod 4.
    pow41, gN41, inf_3m4p
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Inf3m4
import lang.ChebPow

namespace Cheb

open FTAChain ZMod TwoPrime

theorem pow41 (s : Nat) : ∀ k, ∃ t, (4 * s + 1) ^ k = 4 * t + 1
  | 0 => ⟨0, rfl⟩
  | k + 1 => by
    obtain ⟨t, ht⟩ := pow41 s k
    exact ⟨4 * t * s + t + s, by show (4 * s + 1) ^ k * (4 * s + 1) = _; rw [ht]; lib_nat⟩

theorem gN41 (s : Nat) : ∀ n, ∃ k, gN (4 * s + 1) n = 4 * k + n
  | 0 => ⟨0, rfl⟩
  | n + 1 => by
    obtain ⟨k, hk⟩ := gN41 s n
    obtain ⟨t, ht⟩ := pow41 s n
    exact ⟨k + t, by show gN (4 * s + 1) n + (4 * s + 1) ^ n = _; rw [hk, ht]; lib_nat⟩

theorem inf_3m4p (c e B : Nat) (hl : ∀ d, Dv d (2 * (2 * c) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * c) + 1 + 1 + 1) :
    ∃ p, (∀ d, Dv d p → d = 1 ∨ d = p) ∧ B < p ∧ (∃ k, p = 4 * k + 3) ∧
      ∃ q, p = q * (2 * (2 * c) + 1 + 1 + 1) ^ (e + 1) + 1 := by
  obtain ⟨a, ha⟩ := Two2.ofac_odd (B + 1)
  -- u = ℓ·O odd, z = u², y = z^(ℓ^e)
  have hu : (2 * (2 * c) + 1 + 1 + 1) * Two2.ofac (B + 1) = 2 * ((2 * (2 * c) + 1 + 1 + 1) * a + 2 * c + 1) + 1 := by rw [ha]; lib_nat
  generalize hU : (2 * (2 * c) + 1 + 1 + 1) * Two2.ofac (B + 1) = u at hu
  generalize hv : (2 * (2 * c) + 1 + 1 + 1) * a + 2 * c + 1 = v at hu
  have hz : u * u = 4 * (v * v + v) + 1 := by rw [hu]; lib_nat
  obtain ⟨t, ht⟩ := pow41 (v * v + v) ((2 * (2 * c) + 1 + 1 + 1) ^ e)
  rw [← hz] at ht
  generalize hY : (u * u) ^ ((2 * (2 * c) + 1 + 1 + 1) ^ e) = y at ht
  obtain ⟨k0, hk0⟩ := gN41 t (2 * (2 * c) + 1 + 1 + 1)
  rw [← ht] at hk0
  have hE : 1 ≤ (2 * (2 * c) + 1 + 1 + 1) ^ e := pow_pos' _ (Nat.le_add_left 1 _) e
  obtain ⟨E, hE'⟩ := le_dest2 1 _ hE
  have huy : Dv u y := ⟨(u * u) ^ E * u, by rw [← hY, ← hE']; show (u * u) ^ E * (u * u) = _; lib_nat⟩
  have hlu : Dv (2 * (2 * c) + 1 + 1 + 1) u := ⟨Two2.ofac (B + 1), by rw [← hU]; lib_nat⟩
  have hG1 : gN y (2 * (2 * c) + 1 + 1 + 1) = 1 + y * gN y (2 * (2 * c) + 1 + 1) := by
    show gN y ((2 * (2 * c) + 1 + 1) + 1) = _
    rw [Nat.add_comm _ 1, gN_one]
  have hcop : ∀ l, Dv (l + 1 + 1) (gN y (2 * (2 * c) + 1 + 1 + 1)) → Dv (l + 1 + 1) u → False := fun l h1 h2 => by
    rw [hG1] at h1
    exact dv_one_absurd l (dv_sub (l + 1) (y * gN y (2 * (2 * c) + 1 + 1)) 1
      (dv_trans _ _ _ (dv_trans _ _ _ h2 huy) ⟨gN y (2 * (2 * c) + 1 + 1), Nat.mul_comm _ _⟩)
      (by rw [Nat.add_comm (y * gN y (2 * (2 * c) + 1 + 1)) 1]; exact h1))
  have hGodd : ∀ r, gN y (2 * (2 * c) + 1 + 1 + 1) = r * 2 → False := fun r hr => by
    rw [hk0] at hr
    exact InfSplit.odd_not_dv2 (2 * k0 + 2 * c + 1) r (by rw [← hr]; lib_nat)
  have hall : ∀ l, (∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1) → Dv (l + 1 + 1) (gN y (2 * (2 * c) + 1 + 1 + 1)) →
      (∃ a, l + 1 + 1 = a * (1 + 3) + 1) ∨ (∃ a, l + 1 + 1 = a * (1 + 3) + (1 + 2)) := by
    intro l hq hdv
    rcases EveryPrime.nat_even_odd l with ⟨i, hi⟩ | ⟨m, hm⟩
    · exfalso
      have h2 : l + 1 + 1 = 2 := by
        cases hq 2 ⟨i + 1, by rw [hi]; lib_nat⟩ with
        | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
        | inr e => exact e.symm
      rw [h2] at hdv; obtain ⟨r, hr⟩ := hdv; exact hGodd r hr
    · rcases EveryPrime.nat_even_odd m with ⟨i, hi⟩ | ⟨i, hi⟩
      · exact Or.inr ⟨i, by rw [hm, hi]; lib_nat⟩
      · exact Or.inl ⟨i + 1, by rw [hm, hi]; lib_nat⟩
  obtain ⟨q, hq, ⟨b, hb⟩, hqG⟩ := hasG 1 _ hall _ _ (Nat.le_refl _) (dv_refl _) ⟨k0 + c, by rw [hk0]; lib_nat⟩
  have hq2 : 2 ≤ q := by rw [hb]; exact Nat.le_add_left _ _
  obtain ⟨q', hq'⟩ := le_dest2 2 q hq2
  rw [← hq'] at hq hqG hb
  have hq'' : ∀ d, Dv d (q' + 1 + 1) → d = 1 ∨ d = q' + 1 + 1 := hq
  have hqu : Dv (q' + 1 + 1) u → False := hcop q' hqG
  refine ⟨q' + 2, hq, ?_, ⟨b, by show q' + 1 + 1 = _; rw [hb]; lib_nat⟩, ?_⟩
  · cases Nat.lt_or_ge B (q' + 2) with
    | inl h => exact h
    | inr h =>
      exfalso
      rcases EveryPrime.nat_even_odd (q' + 2) with ⟨i, hi⟩ | ⟨i, hi⟩
      · obtain ⟨r, hr⟩ := dv_trans 2 (q' + 1 + 1) _ ⟨i, hi.trans (Nat.mul_comm 2 i)⟩ hqG
        exact hGodd r hr
      · have hle : i ≤ 2 * i + 1 := Nat.le_succ_of_le (Nat.le_trans (Nat.le_add_left i i) (Nat.le_of_eq (by lib_nat)))
        have hB : 2 * i + 1 ≤ B := by rw [hi] at h; exact h
        have hqO := Two2.dv_ofac (B + 1) i (Nat.succ_le_succ (Nat.le_trans hle hB))
        apply hqu
        show Dv (q' + 2) u
        rw [hi, ← hU]
        exact dv_trans _ _ _ hqO ⟨2 * (2 * c) + 1 + 1 + 1, by lib_nat⟩
  · rcases EveryPrime.nat_even_odd q' with ⟨i, hi⟩ | ⟨m, hm⟩
    · exfalso
      obtain ⟨r, hr⟩ := dv_trans 2 (q' + 1 + 1) _ ⟨i + 1, by rw [hi]; lib_nat⟩ hqG
      exact hGodd r hr
    · subst hm
      have hz0 : emb (ring (2 * m + 1 + 1)) (u * u) ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => by
        have := dv_of_emb _ _ e0
        rcases euclid_core (2 * m + 1) (2 * v) u hq'' (by rw [← hu]; exact this) with h | h
        · exact hqu (by rw [hu]; exact h)
        · exact hqu h
      have hg : geo (ring (2 * m + 1 + 1)) (Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) (u * u)) ((2 * (2 * c) + 1 + 1 + 1) ^ e))
          (2 * (2 * c) + 1 + 1 + 1) = (ring (2 * m + 1 + 1)).zero := by
        rw [← Cyclo2l.emb_pow, hY, ← emb_gN]; exact emb_dv _ _ hqG
      rcases ordpow (ring (2 * m + 1 + 1)) (2 * (2 * c) + 1) hl e _ _ hg (Cyclo2l.ferm1 m hq'' _ hz0) with ⟨r, hr⟩ | h
      · exact ⟨r, by show 2 * m + 1 + 1 + 1 = _; rw [hr]⟩
      · exfalso
        rw [geo_one_emb] at h
        rcases hl _ (dv_of_emb _ _ h) with e | e
        · exact Nat.noConfusion (Nat.succ.inj e)
        · exact hqu (by rw [e]; exact hlu)

end Cheb

#print axioms Cheb.pow41
#print axioms Cheb.gN41
#print axioms Cheb.inf_3m4p

lang/Inf3m8.lean

lean_all/lang/Inf3m8.lean

/-
  lang/Inf3m8 — T43, cycle 539: **p ≡ 3 (mod 8), p ≡ 1 (mod 2n) beyond every bound, for every odd squarefree n ≥ 3.**

  The primitive part F of y^(2n) − 1 at y = 2w², w = nO: y ≡ 2 (mod 4), so F ≡ 3 (mod 4) (PhiSq4 with the list 2 :: primes of n).
  A prime r | F has ord_r y = 2n, so y^n = −1 and (−y)^n = 1 with n odd: −y is a square, hence −2 = −y·w⁻² is a square and
  r ≡ 1 or 3 (mod 8) (Inf3mod8.allowed3); also 2n | r − 1. A factor ≡ 3 (mod 4) is ≡ 3 (mod 8).
    mtwo, inf_3m8
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Inf7m8
import lang.Inf5m8

namespace PhiSq

open FTAChain ZMod Cheb TwoPrime PhiInv

/-- −2 a square mod an odd prime r ⇒ r ≡ 1 or 3 (mod 8). -/
theorem mtwo (l : Nat) (hq : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1) (hodd : ∀ c, l + 1 + 1 = 2 * c → False)
    (z : Zp (l + 1)) (hz : (ring (l + 1)).mul z z = (ring (l + 1)).neg ((ring (l + 1)).add (ring (l + 1)).one (ring (l + 1)).one)) :
    ∃ b r, l + 1 + 1 = b * 8 + r ∧ (r = 1 ∨ r = 3) := by
  have hx : ∀ x, mk (l + 1) x = z → (∃ y, x = 2 * y + 1) → ∃ b r, l + 1 + 1 = b * 8 + r ∧ (r = 1 ∨ r = 3) := fun x hxz hxo => by
    apply Inf3mod8.allowed3 x l hxo hq
    apply md_zero_dv (l + 1)
    have h1 : (ring (l + 1)).add ((ring (l + 1)).mul (mk (l + 1) x) (mk (l + 1) x)) ((ring (l + 1)).add (ring (l + 1)).one (ring (l + 1)).one) = (ring (l + 1)).zero := by
      rw [hxz, hz]; lib_ring
    rw [TwoPower.mk_mul, ← Inf3mod8.two_eq, PrimeOneModQ.mk_add] at h1
    exact congrArg Subtype.val h1
  rcases EveryPrime.nat_even_odd z.val with ⟨i, hi⟩ | ⟨i, hi⟩
  · obtain ⟨m, hm⟩ : ∃ m, l + 1 + 1 = 2 * m + 1 := by
      rcases EveryPrime.nat_even_odd (l + 1 + 1) with ⟨c, hc⟩ | ⟨c, hc⟩
      · exact absurd hc (hodd c)
      · exact ⟨c, hc⟩
    refine hx (z.val + (l + 1 + 1)) ?_ ⟨i + m, by rw [hi, hm]; lib_nat⟩
    exact Subtype.ext ((md_add_p (l + 1) z.val).trans (md_small (l + 1) z.val z.property))
  · exact hx z.val (mk_val _ z) ⟨i, hi⟩

theorem inf_3m8 (j : Nat) (L : List Nat) (hG : Good (j :: L)) (h0 : ∃ h, pr (j :: L) = 2 * h + 1) (B : Nat) :
    ∃ p, (∀ k, Dv k p → k = 1 ∨ k = p) ∧ B < p ∧ (∃ k, p = 8 * k + 3) ∧ ∃ s, p = s * (pr (j :: L) * 2) + 1 := by
  obtain ⟨h, hh⟩ := h0
  have hG2 : Good (0 :: j :: L) := ⟨StrongMult1.two_prime, fun hd => by
    obtain ⟨c, hc⟩ := hd; rw [hh] at hc; exact InfSplit.odd_not_dv2 h c hc, hG⟩
  obtain ⟨a, ha⟩ := Two2.ofac_odd (B + cs (j :: L) + 1)
  have hOge := ofac_ge (B + cs (j :: L))
  have hw : pr (j :: L) * Two2.ofac (B + cs (j :: L) + 1) = 2 * (2 * h * a + h + a) + 1 := by rw [hh, ha]; lib_nat
  generalize hW : pr (j :: L) * Two2.ofac (B + cs (j :: L) + 1) = w at hw
  generalize hv : 2 * h * a + h + a = v at hw
  generalize hy : 2 * (w * w) = y
  have hy4 : y = 4 * (2 * (v * v + v)) + 2 := by rw [← hy, hw]; lib_nat
  have hwA : AllDv (j :: L) w := by
    rw [← hW, Nat.mul_comm]; exact allDv_mul _ _ _ (allDv_pr (j :: L))
  have hA : AllDv (0 :: j :: L) y := ⟨⟨w * w, by rw [← hy]; lib_nat⟩, by rw [← hy]; exact allDv_mul 2 _ _ (allDv_mul w _ _ hwA)⟩
  have hw1 : 1 ≤ w := by rw [hw]; exact Nat.le_add_left 1 _
  have hcy : cs (j :: L) + 2 ≤ y := by
    have h1 : 1 ≤ pr (j :: L) := pr_pos _
    have e1 : cs (j :: L) + 2 ≤ B + cs (j :: L) + 1 + 1 := Nat.le_trans (Nat.le_add_left (cs (j :: L) + 2) B) (Nat.le_of_eq (by lib_nat))
    have e2 : B + cs (j :: L) + 1 ≤ w := by
      rw [← hW]; exact Nat.le_trans hOge (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ h1))
    have e3 : w + 1 ≤ y := by
      rw [← hy]
      exact Nat.le_trans (Nat.add_le_add_left hw1 w) (Nat.le_trans (Nat.le_of_eq (by lib_nat))
        (Nat.mul_le_mul_left 2 (Nat.le_trans (Nat.le_of_eq (Nat.mul_one w).symm) (Nat.mul_le_mul_left w hw1))))
    exact Nat.le_trans e1 (Nat.le_trans (Nat.succ_le_succ e2) e3)
  obtain ⟨y', hy'⟩ := le_dest2 2 y (Nat.le_trans (Nat.le_add_left 2 _) hcy)
  rw [← hy'] at hA
  obtain ⟨F, I, l, _, hR⟩ := sqfree4 0 (j :: L) hG2 y' hA
  rw [show y' + 1 + 1 = y from hy'] at I l hR
  obtain ⟨f, hf⟩ := hR.1 _ hy4
  have hy1 : 1 ≤ y := Nat.le_trans (Nat.le_add_left 1 _) hcy
  have hC : cs (j :: L) < y ^ ph 0 (j :: L) :=
    Nat.le_trans (Nat.le_add_right (cs (j :: L) + 1) 1) (Nat.le_trans hcy (pow_ge y hy1 _ (ph_pos 0 (j :: L))))
  have hF2 := PhiSize.two_le _ _ _ l hC
  have hn1 : 1 ≤ pr (j :: L) := pr_pos _
  have hFodd : ∀ r, F = r * 2 → False := fun r hr => InfSplit.odd_not_dv2 (2 * f + 1) r (by rw [← hr, hf]; lib_nat)
  -- facts about a prime factor of F
  have hfac : ∀ l, (∀ e, Dv e (l + 1 + 1) → e = 1 ∨ e = l + 1 + 1) → Dv (l + 1 + 1) F →
      (Dv (l + 1 + 1) y → False) ∧ (∃ s, l + 1 + 1 = s * (pr (j :: L) * 2) + 1) ∧ ∃ b r, l + 1 + 1 = b * 8 + r ∧ (r = 1 ∨ r = 3) := by
    intro l hq hdv
    obtain ⟨hO, hg⟩ := (I l 0 hq).1 (dv_pow1 _ _ hdv)
    have hpr2 : pr (0 :: j :: L) = pr (j :: L) * 2 := rfl
    rw [hpr2] at hO hg
    obtain ⟨n', hn'⟩ := le_dest2 1 (pr (j :: L) * 2) (Nat.le_trans hn1 (Nat.le_trans (Nat.le_of_eq (Nat.mul_one _).symm) (Nat.mul_le_mul_left _ (Nat.le_add_left 1 1))))
    have hqy : Dv (l + 1 + 1) y → False := fun hd => by
      have hg1 : Dv (l + 1 + 1) (gN y (pr (j :: L) * 2)) := dv_base _ 0 _ hg
      rw [← hn', Nat.add_comm n' 1, gN_one] at hg1
      exact dv_one_absurd l (dv_sub (l + 1) (y * gN y n') 1 (dv_trans _ _ _ hd ⟨gN y n', Nat.mul_comm _ _⟩)
        (by rw [Nat.add_comm (y * gN y n') 1]; exact hg1))
    have hodd : ∀ c, l + 1 + 1 = 2 * c → False := fun c hc => by
      rcases hq 2 ⟨c, hc.trans (Nat.mul_comm 2 c)⟩ with e | e
      · exact Nat.noConfusion (Nat.succ.inj e)
      · exact hqy (by rw [← e, ← hy]; exact ⟨w * w, by lib_nat⟩)
    rcases EveryPrime.nat_even_odd l with ⟨i, hi⟩ | ⟨m, hm⟩
    · exact absurd (show l + 1 + 1 = 2 * (i + 1) by rw [hi]; lib_nat) (hodd (i + 1))
    · subst hm
      have hX0 : emb (ring (2 * m + 1 + 1)) y ≠ (ring (2 * m + 1 + 1)).zero := fun e => hqy (dv_of_emb _ _ e)
      obtain ⟨r, hr⟩ := hO.2 _ (Cyclo2l.ferm1 m hq _ hX0)
      refine ⟨hqy, ⟨r, by show 2 * m + 1 + 1 + 1 = _; rw [hr]⟩, ?_⟩
      -- Y^n = −1
      have hYn : Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) y) (pr (j :: L)) = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one := by
        have h2n : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) y) (pr (j :: L))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))
            ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) y) (pr (j :: L))) (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by
          have e1 := hO.1
          rw [show pr (j :: L) * 2 = pr (j :: L) + pr (j :: L) by lib_nat, HasseCM.pw_add'] at e1
          calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) y) (pr (j :: L))) (Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) y) (pr (j :: L)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
            _ = _ := by rw [e1]; lib_ring
        rcases ZMod.nozd (2 * m + 1) hq _ _ h2n with e | e
        · exfalso
          have e' : Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) y) (pr (j :: L)) = (ring (2 * m + 1 + 1)).one := by
            calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) y) (pr (j :: L))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
              _ = _ := by rw [e]; lib_ring
          exact no_half _ hn1 (hO.2 _ e')
        · calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) y) (pr (j :: L))) (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
            _ = _ := by rw [e]; lib_ring
      -- (−Y)^(h+1) squared is −Y
      have hmY : Frobenius.pw (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).neg (emb (ring (2 * m + 1 + 1)) y)) (pr (j :: L)) = (ring (2 * m + 1 + 1)).one := by
        rw [show (ring (2 * m + 1 + 1)).neg (emb (ring (2 * m + 1 + 1)) y) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) (emb (ring (2 * m + 1 + 1)) y) by lib_ring, Frobenius.pw_mul, hYn, hh, pw_m1]
        lib_ring
      rw [hh] at hmY
      have hsq : (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).neg (emb (ring (2 * m + 1 + 1)) y)) (h + 1)) (Frobenius.pw (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).neg (emb (ring (2 * m + 1 + 1)) y)) (h + 1)) = (ring (2 * m + 1 + 1)).neg (emb (ring (2 * m + 1 + 1)) y) := by
        rw [← HasseCM.pw_add', show h + 1 + (h + 1) = (2 * h + 1) + 1 by lib_nat]
        show (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) _ (2 * h + 1)) _ = _
        rw [hmY]; lib_ring
      have hW0 : emb (ring (2 * m + 1 + 1)) w ≠ (ring (2 * m + 1 + 1)).zero := fun e => hqy (by rw [← hy]; exact dv_mul_left 2 _ _ (dv_mul_left w _ _ (dv_of_emb _ _ e)))
      have hinv := ZMod.inv_exists (2 * m + 1) hq _ hW0
      generalize Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) w) (2 * m + 1) = iw at hinv
      generalize Frobenius.pw (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).neg (emb (ring (2 * m + 1 + 1)) y)) (h + 1) = t at hsq
      have hY : emb (ring (2 * m + 1 + 1)) y = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul (emb (ring (2 * m + 1 + 1)) w) (emb (ring (2 * m + 1 + 1)) w)) := by
        rw [← hy, emb_mul, emb_mul]; show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).one) (ring (2 * m + 1 + 1)).one) _ = _; lib_ring
      apply mtwo (2 * m + 1) hq hodd ((ring (2 * m + 1 + 1)).mul t iw)
      calc _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t t) ((ring (2 * m + 1 + 1)).mul iw iw) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul iw (emb (ring (2 * m + 1 + 1)) w)) ((ring (2 * m + 1 + 1)).mul iw (emb (ring (2 * m + 1 + 1)) w)))) := by
            rw [hsq, hY]; lib_ring
        _ = _ := by rw [hinv]; lib_ring
  have hall : ∀ l, (∀ e, Dv e (l + 1 + 1) → e = 1 ∨ e = l + 1 + 1) → Dv (l + 1 + 1) F →
      (∃ a, l + 1 + 1 = a * (1 + 3) + 1) ∨ (∃ a, l + 1 + 1 = a * (1 + 3) + (1 + 2)) := by
    intro l hq hdv
    rcases EveryPrime.nat_even_odd l with ⟨i, hi⟩ | ⟨m, hm⟩
    · exfalso
      have h2 : l + 1 + 1 = 2 := by
        cases hq 2 ⟨i + 1, by rw [hi]; lib_nat⟩ with
        | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
        | inr e => exact e.symm
      rw [h2] at hdv; obtain ⟨r, hr⟩ := hdv; exact hFodd r hr
    · rcases EveryPrime.nat_even_odd m with ⟨i, hi⟩ | ⟨i, hi⟩
      · exact Or.inr ⟨i, by rw [hm, hi]; lib_nat⟩
      · exact Or.inl ⟨i + 1, by rw [hm, hi]; lib_nat⟩
  obtain ⟨q, hq, ⟨b, hb⟩, hqF⟩ := hasG 1 F hall F F (Nat.le_refl _) (dv_refl _) ⟨f, by rw [hf]; lib_nat⟩
  have hq2 : 2 ≤ q := by rw [hb]; exact Nat.le_add_left _ _
  obtain ⟨q', hq'⟩ := le_dest2 2 q hq2
  rw [← hq'] at hq hqF hb
  have hq'' : ∀ e, Dv e (q' + 1 + 1) → e = 1 ∨ e = q' + 1 + 1 := hq
  obtain ⟨hqy, ⟨s, hs⟩, b8, r8, hb8, hr8⟩ := hfac q' hq'' hqF
  refine ⟨q' + 2, hq, ?_, ?_, ⟨s, hs⟩⟩
  · cases Nat.lt_or_ge B (q' + 2) with
    | inl hlt => exact hlt
    | inr hge =>
      exfalso
      rcases EveryPrime.nat_even_odd (q' + 2) with ⟨i, hi⟩ | ⟨i, hi⟩
      · rcases hq'' 2 ⟨i, hi.trans (Nat.mul_comm 2 i)⟩ with e | e
        · exact Nat.noConfusion (Nat.succ.inj e)
        · exact hqy (by rw [← e, ← hy]; exact ⟨w * w, by lib_nat⟩)
      · have hle : i ≤ 2 * i + 1 := Nat.le_succ_of_le (Nat.le_trans (Nat.le_add_left i i) (Nat.le_of_eq (by lib_nat)))
        have hB : 2 * i + 1 ≤ B + cs (j :: L) := by rw [hi] at hge; exact Nat.le_trans hge (Nat.le_add_right B _)
        have hqO := Two2.dv_ofac (B + cs (j :: L) + 1) i (Nat.succ_le_succ (Nat.le_trans hle hB))
        apply hqy
        show Dv (q' + 2) y
        rw [hi, ← hy, ← hW]
        exact dv_trans _ _ _ hqO ⟨2 * (pr (j :: L) * Two2.ofac (B + cs (j :: L) + 1)) * pr (j :: L), by lib_nat⟩
  · rcases hr8 with e | e
    · exfalso
      rw [e] at hb8
      have := Inf3mod8.uniq_rem 3 (2 * b8) b 1 3 (Nat.lt_of_lt_of_le (Nat.lt_succ_self 1) (Nat.le_add_left 2 2)) (Nat.lt_succ_self 3)
        ((show (2 * b8) * (3 + 1) + 1 = b8 * 8 + 1 by lib_nat).trans (hb8.symm.trans (hb.trans (by lib_nat))))
      exact Nat.noConfusion (Nat.succ.inj this)
    · exact ⟨b8, by show q' + 1 + 1 = _; rw [hb8, e]; lib_nat⟩

end PhiSq

#print axioms PhiSq.mtwo
#print axioms PhiSq.inf_3m8

lang/Inf3mod8.lean

lean_all/lang/Inf3mod8.lean

/-
  lang/Inf3mod8 — T43, cycle 130: infinitely many primes p ≡ 3 (mod 8); with cycles 125–129, Dirichlet for every class mod 8.

  x the odd factorial of B, N = x² + 2 ≡ 3 (mod 8). Every prime factor q of N has −2 ≡ x² a square, so q ≢ 5, 7 (mod 8):
  at q ≡ 7, 2 = w² (TwoSupp) gives −1 = (x/w)², impossible for q ≡ 3 (mod 4); at q ≡ 5, −1 = z² gives 2 = (zx)², impossible
  (TwoSupp). So q ≡ 1 or 3 (mod 8), and some factor is ≡ 3.
    uniq_rem, md_rem         the ZMod residue equals the FTAChain remainder
    allowed3, has3m8, inf_3mod8
  So every class a ∈ (ℤ/8)^× contains infinitely many primes: the Chebotarev section exists for ℚ(ζ₈)/ℚ.

  Lean 4 core only, proofs by hand (lib_ring, lib_nat, lib_int), no axioms.
-/
import lang.Inf5mod8

namespace Inf3mod8

open FTAChain ZMod LibInt InfSplit Inf7mod8 Inf5mod8 TwoSupp

theorem uniq_rem (m' x y r c : Nat) (hr : r < m' + 1) (hc : c < m' + 1) (h : x * (m' + 1) + r = y * (m' + 1) + c) : r = c := by
  have big : ∀ x y r c : Nat, r < m' + 1 → x < y → x * (m' + 1) + r = y * (m' + 1) + c → False := fun x y r c hr hxy h => by
    have h1 : x * (m' + 1) + r < (x + 1) * (m' + 1) := by rw [Nat.succ_mul]; exact Nat.add_lt_add_left hr _
    have h2 : (x + 1) * (m' + 1) ≤ y * (m' + 1) := Nat.mul_le_mul_right (m' + 1) hxy
    exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (h ▸ h1) (Nat.le_trans h2 (Nat.le_add_right _ _)))
  cases Nat.lt_or_ge x y with
  | inl hxy => exact absurd h (fun h' => big x y r c hr hxy h')
  | inr hxy =>
    cases Nat.lt_or_ge y x with
    | inl hyx => exact absurd h.symm (fun h' => big y x c r hc hyx h')
    | inr hyx =>
      have e : x = y := Nat.le_antisymm hyx hxy
      subst e
      apply Int.ofNat.inj
      have e' : Int.ofNat x * Int.ofNat (m' + 1) + Int.ofNat r = Int.ofNat x * Int.ofNat (m' + 1) + Int.ofNat c := congrArg Int.ofNat h
      calc Int.ofNat r = (Int.ofNat x * Int.ofNat (m' + 1) + Int.ofNat r) - Int.ofNat x * Int.ofNat (m' + 1) := by lib_int
        _ = Int.ofNat c := by rw [e']; lib_int

theorem md_rem (m' a : Nat) : md m' a = rem (m' + 1) a :=
  match md_spec m' a, rem_spec m' a with
  | ⟨q, hq⟩, ⟨q', r, k, ha, hr, hrem⟩ => by
    rw [hrem]
    apply uniq_rem m' q q' _ _ (md_lt m' a) (Nat.lt_of_lt_of_le (Nat.lt_succ_self r) (by rw [← hr]; exact Nat.succ_le_succ (Nat.le_add_right r k)))
    exact (show q * (m' + 1) + md m' a = md m' a + q * (m' + 1) by lib_nat).trans (hq.symm.trans ha)

/-- squares: ZMod ↔ remainders. -/
theorem sq_rem (l y : Nat) (h : (ring (l + 1)).mul (mk (l + 1) y) (mk (l + 1) y) = mk (l + 1) 2) :
    rem (l + 1 + 1) (y * y) = rem (l + 1 + 1) 2 := by
  rw [TwoPower.mk_mul] at h
  rw [← md_rem, ← md_rem]; exact congrArg Subtype.val h

theorem rem_sq (l y : Nat) (h : rem (l + 1 + 1) (y * y) = rem (l + 1 + 1) 2) :
    (ring (l + 1)).mul (mk (l + 1) y) (mk (l + 1) y) = mk (l + 1) 2 := by
  rw [TwoPower.mk_mul]; apply Subtype.ext; show md (l + 1) (y * y) = md (l + 1) 2; rw [md_rem, md_rem]; exact h

theorem two_eq (l : Nat) : mk (l + 1) 2 = (ring (l + 1)).add (ring (l + 1)).one (ring (l + 1)).one :=
  (PrimeOneModQ.mk_add (l + 1) 1 1).symm

theorem cofactor3 (b N' a : Nat) (h : (b * 8 + 1) * N' = a * 8 + 3) : ∃ c, N' = c * 8 + 3 :=
  match mod8 N' with
  | ⟨c, r, hc, hr⟩ => by
    have e : (b * 8 + 1) * N' = (b * 8 * c + b * r + c) * 8 + r := by rw [hc]; lib_nat
    rw [e] at h
    exact ⟨c, by rw [hc, uniq_rem8 _ _ r 3 hr (Nat.lt_of_lt_of_le (Nat.lt_succ_self 3) (Nat.le_add_right 4 4)) h]⟩

/-- prime factors of x² + 2 (x odd) are ≡ 1 or 3 (mod 8). -/
theorem allowed3 (x l : Nat) (hxo : ∃ y, x = 2 * y + 1) (hq : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1)
    (hdv : Dv (l + 1 + 1) (x * x + 2)) : ∃ b r, l + 1 + 1 = b * 8 + r ∧ (r = 1 ∨ r = 3) := by
  have hPr : Pr (l + 1 + 1) := ⟨⟨l, rfl⟩, hq⟩
  have hx : (ring (l + 1)).mul (mk (l + 1) x) (mk (l + 1) x) = (ring (l + 1)).neg ((ring (l + 1)).add (ring (l + 1)).one (ring (l + 1)).one) := by
    apply DetChain.neg_unique
    rw [TwoPower.mk_mul, ← two_eq, PrimeOneModQ.mk_add]
    exact Subtype.ext (dv_md_zero (l + 1) _ hdv)
  have hodd : ∀ c, l + 1 + 1 = 2 * c → False := fun c hc => by
    have h2 : l + 1 + 1 = 2 := by
      cases hq 2 ⟨c, by rw [hc]; lib_nat⟩ with
      | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
      | inr e => exact e.symm
    rw [h2] at hdv
    match hdv, hxo with
    | ⟨r, hr⟩, ⟨y, hy⟩ => exact odd_not_dv2 (2 * y * y + 2 * y + 1) r (by rw [← hr, hy]; lib_nat)
  match mod8 (l + 1 + 1) with
  | ⟨b, r, hb, hr⟩ =>
    match r, hr with
    | 0, _ => exact absurd hb (fun e => hodd (4 * b) (by rw [e]; lib_nat))
    | 1, _ => exact ⟨b, 1, hb, Or.inl rfl⟩
    | 2, _ => exact absurd hb (fun e => hodd (4 * b + 1) (by rw [e]; lib_nat))
    | 3, _ => exact ⟨b, 3, hb, Or.inr rfl⟩
    | 4, _ => exact absurd hb (fun e => hodd (4 * b + 2) (by rw [e]; lib_nat))
    | 5, _ =>
      exfalso
      have hl : l = 2 * (2 * (2 * b) + 1) + 1 := Nat.succ.inj (Nat.succ.inj (hb.trans (by lib_nat)))
      subst hl
      match GaussSplit.split_one_mod_four (2 * b) hq with
      | ⟨z, hz⟩ =>
        have h2 : (ring (2 * (2 * (2 * b) + 1) + 1 + 1)).mul (mk _ ((ring (2 * (2 * (2 * b) + 1) + 1 + 1)).mul z (mk _ x)).val)
            (mk _ ((ring (2 * (2 * (2 * b) + 1) + 1 + 1)).mul z (mk _ x)).val) = mk _ 2 := by
          rw [mk_val, two_eq]
          rw [show (ring (2 * (2 * (2 * b) + 1) + 1 + 1)).mul ((ring (2 * (2 * (2 * b) + 1) + 1 + 1)).mul z (mk _ x)) ((ring (2 * (2 * (2 * b) + 1) + 1 + 1)).mul z (mk _ x))
              = (ring (2 * (2 * (2 * b) + 1) + 1 + 1)).mul ((ring (2 * (2 * (2 * b) + 1) + 1 + 1)).mul z z) ((ring (2 * (2 * (2 * b) + 1) + 1 + 1)).mul (mk _ x) (mk _ x)) by lib_ring,
            hz, hx]
          lib_ring
        have e : (b + b + 1) + ((b + b + 1) + 0) + ((b + b + 1) + ((b + b + 1) + 0)) + 1 = 2 * (2 * (2 * b) + 1) + 1 + 1 + 1 := by lib_nat
        exact two_8m5 b (by rw [e]; exact hPr) ⟨_, by rw [e]; exact sq_rem _ _ h2⟩
    | 6, _ => exact absurd hb (fun e => hodd (4 * b + 3) (by rw [e]; lib_nat))
    | 7, _ =>
      exfalso
      have e : (b + b + 1) + ((b + b + 1) + 1) + ((b + b + 1) + ((b + b + 1) + 1)) + 1 = l + 1 + 1 := by rw [hb]; lib_nat
      match two_8m7 b (by rw [e]; exact hPr) with
      | ⟨w0, hw0⟩ =>
        rw [e] at hw0
        have hw := rem_sq l w0 hw0
        rw [two_eq] at hw
        have hwn : mk (l + 1) w0 ≠ (ring (l + 1)).zero := fun e0 => by
          rw [e0, DetChain.mul_zero] at hw
          have h2 : Dv (l + 1 + 1) (1 + 1) := md_zero_dv (l + 1) 2 (by
            have := congrArg Subtype.val hw.symm
            rw [← two_eq] at this; exact this)
          match dv_le (l + 1 + 1) 1 h2 with
          | ⟨k, hk⟩ =>
            have h0 : l + k = 0 := Nat.succ.inj (Nat.succ.inj ((show l + k + 1 + 1 = l + 1 + 1 + k by lib_nat).trans hk))
            exact hodd 1 (by rw [(HasseDavenport.nat_sum_zero _ _ h0).1])
        have hinv := ZMod.inv_exists l hq _ hwn
        have hl : l = 2 * (2 * (2 * b + 1)) + 1 := Nat.succ.inj (Nat.succ.inj (hb.trans (by lib_nat)))
        subst hl
        apply GaussSplit.inert_three_mod_four (2 * b + 1) hq
          ((ring (2 * (2 * (2 * b + 1)) + 1 + 1)).mul (mk _ x) (Frobenius.pw (ring (2 * (2 * (2 * b + 1)) + 1 + 1)) (mk _ w0) (2 * (2 * (2 * b + 1)) + 1)))
        generalize Frobenius.pw (ring (2 * (2 * (2 * b + 1)) + 1 + 1)) (mk _ w0) (2 * (2 * (2 * b + 1)) + 1) = v at hinv ⊢
        rw [show (ring (2 * (2 * (2 * b + 1)) + 1 + 1)).mul ((ring (2 * (2 * (2 * b + 1)) + 1 + 1)).mul (mk _ x) v) ((ring (2 * (2 * (2 * b + 1)) + 1 + 1)).mul (mk _ x) v)
            = (ring (2 * (2 * (2 * b + 1)) + 1 + 1)).mul ((ring (2 * (2 * (2 * b + 1)) + 1 + 1)).mul (mk _ x) (mk _ x)) ((ring (2 * (2 * (2 * b + 1)) + 1 + 1)).mul v v) by lib_ring,
          hx, ← hw]
        rw [show (ring (2 * (2 * (2 * b + 1)) + 1 + 1)).mul ((ring (2 * (2 * (2 * b + 1)) + 1 + 1)).neg ((ring (2 * (2 * (2 * b + 1)) + 1 + 1)).mul (mk _ w0) (mk _ w0))) ((ring (2 * (2 * (2 * b + 1)) + 1 + 1)).mul v v)
            = (ring (2 * (2 * (2 * b + 1)) + 1 + 1)).neg ((ring (2 * (2 * (2 * b + 1)) + 1 + 1)).mul ((ring (2 * (2 * (2 * b + 1)) + 1 + 1)).mul v (mk _ w0)) ((ring (2 * (2 * (2 * b + 1)) + 1 + 1)).mul v (mk _ w0))) by lib_ring,
          hinv, (ring (2 * (2 * (2 * b + 1)) + 1 + 1)).one_mul]
    | r + 8, hr => exact absurd hr (fun h => Nat.lt_irrefl 8 (Nat.lt_of_le_of_lt (Nat.le_add_left 8 r) h))

theorem has3m8 (x : Nat) (hxo : ∃ y, x = 2 * y + 1) : ∀ G N, N ≤ G → Dv N (x * x + 2) → (∃ a, N = a * 8 + 3) →
    ∃ q, (∀ d, Dv d q → d = 1 ∨ d = q) ∧ (∃ b, q = b * 8 + 3) ∧ Dv q N
  | 0, N, hN, _, ⟨a, ha⟩ => absurd (ha ▸ hN) (fun h => Nat.not_succ_le_zero _ (Nat.le_trans (Nat.le_add_left _ _) h))
  | G + 1, N, hN, hN0, ⟨a, ha⟩ => by
    match lp_exists (a * 8 + 1) with
    | ⟨l, hl, hmin⟩ =>
    have hq := lp_prime _ l hl hmin
    have hlN : Dv (l + 1 + 1) N := by rw [ha]; exact hl
    match allowed3 x l hxo hq (dv_trans _ _ _ hlN hN0) with
    | ⟨b, r, hb, hr⟩ =>
      cases hr with
      | inr h3 => exact ⟨l + 1 + 1, hq, ⟨b, by rw [hb, h3]⟩, hlN⟩
      | inl h1 =>
        match hlN with
        | ⟨N', hN'⟩ =>
        match cofactor3 b N' a (by rw [← h1, ← hb, ← ha, hN']; lib_nat) with
        | ⟨c, hc⟩ =>
        have hlt : N' ≤ G := by
          have e : N = N' + (N' * (l + 1)) := by rw [hN']; lib_nat
          have h1' : 1 ≤ N' * (l + 1) := by rw [hc]; exact Nat.mul_le_mul (Nat.le_add_left 1 _) (Nat.le_add_left 1 _)
          exact Nat.le_of_succ_le_succ (Nat.le_trans (Nat.add_le_add_left h1' N') (by rw [← e]; exact hN))
        have hN'0 : Dv N' (x * x + 2) := dv_trans _ _ _ ⟨l + 1 + 1, by rw [hN']; lib_nat⟩ hN0
        match has3m8 x hxo G N' hlt hN'0 ⟨c, hc⟩ with
        | ⟨q, hq', hq3, hqN⟩ => exact ⟨q, hq', hq3, dv_trans q N' N hqN ⟨l + 1 + 1, by rw [hN']; lib_nat⟩⟩

/-- **Infinitely many primes p ≡ 3 (mod 8).** -/
theorem inf_3mod8 (B : Nat) : ∃ b, (∀ d, Dv d (b * 8 + 3) → d = 1 ∨ d = b * 8 + 3) ∧ B < b * 8 + 3 := by
  match ofac_odd B with
  | ⟨y, hy⟩ =>
  have hN : ∃ a, ofac B * ofac B + 2 = a * 8 + 3 := by
    cases EveryPrime.nat_even_odd y with
    | inl h => match h with | ⟨c, hc⟩ => exact ⟨c * (2 * c + 1), by rw [hy, hc]; lib_nat⟩
    | inr h => match h with | ⟨c, hc⟩ => exact ⟨(2 * c + 1) * (c + 1), by rw [hy, hc]; lib_nat⟩
  match has3m8 (ofac B) ⟨y, hy⟩ _ _ (Nat.le_refl _) ⟨1, by lib_nat⟩ hN with
  | ⟨q, hq, ⟨b, hb⟩, hqN⟩ =>
    refine ⟨b, by rw [← hb]; exact hq, ?_⟩
    rw [← hb]
    cases Nat.lt_or_ge B q with
    | inl h => exact h
    | inr h =>
      exfalso
      match dv_ofac B (4 * b + 1) (by rw [show 2 * (4 * b + 1) + 1 = b * 8 + 3 by lib_nat, ← hb]; exact h) with
      | ⟨r, hr⟩ =>
        have hr' : ofac B = r * q := by rw [hr, hb]; lib_nat
        have h1 : Dv q (ofac B * ofac B) := ⟨r * ofac B, by rw [hr']; lib_nat⟩
        have hq2 : q = (b * 8 + 2) + 1 := hb
        rw [hq2] at hqN h1
        have h2 := dv_sub (b * 8 + 2) _ 2 h1 hqN
        match dv_le (b * 8 + 2 + 1) 1 h2 with
        | ⟨k, hk⟩ =>
          have e : b * 8 + 1 + k + 1 + 1 = 0 + 1 + 1 := (show b * 8 + 1 + k + 1 + 1 = b * 8 + 2 + 1 + k by lib_nat).trans hk
          exact Nat.noConfusion ((show b * 8 + k + 1 = b * 8 + 1 + k by lib_nat).trans (Nat.succ.inj (Nat.succ.inj e)))

end Inf3mod8

#print axioms Inf3mod8.uniq_rem
#print axioms Inf3mod8.md_rem
#print axioms Inf3mod8.sq_rem
#print axioms Inf3mod8.rem_sq
#print axioms Inf3mod8.two_eq
#print axioms Inf3mod8.cofactor3
#print axioms Inf3mod8.allowed3
#print axioms Inf3mod8.has3m8
#print axioms Inf3mod8.inf_3mod8

lang/Inf4mod5.lean

lean_all/lang/Inf4mod5.lean

/-
  lang/Inf4mod5 — T43, cycle 199: infinitely many primes p ≡ 4 (mod 5).

  N₀ = 20(F + 1)² − 1 ≡ 4 (mod 5), odd, prime to 5. For every prime factor q of N₀, (10(F + 1))² = 5N₀ + 5 ≡ 5, so 5 is a
  square mod q and q ≡ ±1 (mod 5) (Gauss5, the Gauss sum in 𝔽_q[ζ₅]). A product of primes ≡ 1 is ≡ 1, so some factor is ≡ 4;
  with F + 1 = B! the factor exceeds B.
    uniq_rem5, mod5n, cofactor5, castZ5, allowed5, has4, inf_4mod5
  The class −1 mod 5 (split in ℚ(√5), not completely split in ℚ(ζ₅)): with InfCyclo (1 mod 5) both split classes occur.

  Lean 4 core only, proofs by hand (lib_nat, lib_int), no axioms.
-/
import lang.Gauss5
import lang.Inf7mod8

namespace Inf4mod5

open FTAChain ZMod LibInt InfSplit
open LibRing (castZ castN)

theorem uniq_rem5 (x y r c : Nat) (hr : r < 5) (hc : c < 5) (h : x * 5 + r = y * 5 + c) : r = c := by
  have big : ∀ x y r c : Nat, r < 5 → x < y → x * 5 + r = y * 5 + c → False := fun x y r c hr hxy h => by
    have h1 : x * 5 + r < (x + 1) * 5 := by rw [Nat.succ_mul]; exact Nat.add_lt_add_left hr _
    have h2 : (x + 1) * 5 ≤ y * 5 := Nat.mul_le_mul_right 5 hxy
    exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (h ▸ h1) (Nat.le_trans h2 (Nat.le_add_right _ _)))
  cases Nat.lt_or_ge x y with
  | inl hxy => exact absurd h (fun h' => big x y r c hr hxy h')
  | inr hxy =>
    cases Nat.lt_or_ge y x with
    | inl hyx => exact absurd h.symm (fun h' => big y x c r hc hyx h')
    | inr hyx =>
      have e : x = y := Nat.le_antisymm hyx hxy
      subst e
      apply Int.ofNat.inj
      have := congrArg Int.ofNat h
      have e' : Int.ofNat x * 5 + Int.ofNat r = Int.ofNat x * 5 + Int.ofNat c := this
      calc Int.ofNat r = (Int.ofNat x * 5 + Int.ofNat r) - Int.ofNat x * 5 := by lib_int
        _ = Int.ofNat c := by rw [e']; lib_int

/-- residue of n mod 8 as a case split. -/
theorem mod5n (n : Nat) : ∃ a r, n = a * 5 + r ∧ r < 5 :=
  match div_exists 4 n with
  | ⟨a, r, k, hn, hr⟩ => ⟨a, r, hn, Nat.lt_of_lt_of_le (Nat.lt_succ_self r) (by rw [show (5 : Nat) = 4 + 1 from rfl, ← hr]; exact Nat.succ_le_succ (Nat.le_add_right r k))⟩

theorem cofactor5 (b N' a : Nat) (h : (b * 5 + 1) * N' = a * 5 + 4) : ∃ c, N' = c * 5 + 4 :=
  match mod5n N' with
  | ⟨c, r, hc, hr⟩ => by
    have e : (b * 5 + 1) * N' = (b * 5 * c + b * r + c) * 5 + r := by rw [hc]; lib_nat
    rw [e] at h
    exact ⟨c, by rw [hc, uniq_rem5 _ _ r 4 hr (Nat.lt_succ_self 4) h]⟩


theorem castZ5 (r : Nat) : castZ (ring (2 * r + 1 + 1)) 5 = (ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one (ring (2 * r + 1 + 1)).one))) := by
  show (ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one (ring (2 * r + 1 + 1)).zero)))) = _
  rw [DetChain.add_zero']

/-- Every prime factor of N₀ = 20(F + 1)² − 1 is ≡ 1 or 4 (mod 5). -/
theorem allowed5 (F l : Nat) (hq : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1)
    (hdv : Dv (l + 1 + 1) (20 * F * F + 40 * F + 19)) : ∃ b r, l + 1 + 1 = b * 5 + r ∧ (r = 1 ∨ r = 4) := by
  have hodd : ∀ c, l + 1 + 1 = 2 * c → False := fun c hc => by
    have h2 : l + 1 + 1 = 2 := by
      cases hq 2 ⟨c, by rw [hc]; lib_nat⟩ with
      | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
      | inr e => exact e.symm
    rw [h2] at hdv
    match hdv with
    | ⟨r, hr⟩ => exact InfSplit.odd_not_dv2 (10 * F * F + 20 * F + 9) r (by rw [← hr]; lib_nat)
  have h5 : l + 1 + 1 ≠ 5 := fun e => by
    rw [e] at hdv
    match hdv with
    | ⟨r, hr⟩ => exact Nat.noConfusion (uniq_rem5 (4 * F * F + 8 * F + 3) r 4 0 (Nat.lt_succ_self 4) (Nat.succ_pos 4) (by rw [← hr]; lib_nat) |>.symm)
  match EveryPrime.nat_even_odd l with
  | Or.inl ⟨c, hc⟩ => exact absurd (by rw [hc]; lib_nat : l + 1 + 1 = 2 * (c + 1)) (fun e => hodd (c + 1) e)
  | Or.inr ⟨r, hr⟩ =>
    subst hr
    have hu : (ring (2 * r + 1 + 1)).mul (castZ (ring (2 * r + 1 + 1)) (Int.ofNat (10 * (F + 1)))) (castZ (ring (2 * r + 1 + 1)) (Int.ofNat (10 * (F + 1)))) = (ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one (ring (2 * r + 1 + 1))  -- (+13 chars; full line in the Zenodo source)
      match hdv with
      | ⟨w, hw⟩ =>
        rw [← HasseDavenport.castZ_mul, show Int.ofNat (10 * (F + 1)) * Int.ofNat (10 * (F + 1)) = 5 * Int.ofNat (w * (2 * r + 1 + 1 + 1)) + 5 from by
          rw [← hw]; show Int.ofNat (10 * (F + 1) * (10 * (F + 1))) = Int.ofNat (5 * (20 * F * F + 40 * F + 19) + 5); exact congrArg Int.ofNat (by lib_nat),
          IntCast.castZ_add, HasseDavenport.castZ_mul, show Int.ofNat (w * (2 * r + 1 + 1 + 1)) = Int.ofNat w * Int.ofNat (2 * r + 1 + 1 + 1) from rfl,
          HasseDavenport.castZ_mul, CubeAp.castP, DetChain.mul_zero, DetChain.mul_zero, (ring (2 * r + 1 + 1)).zero_add, castZ5]
    match Gauss5.sq5_mod5 r hq h5 _ hu with
    | ⟨s, h⟩ =>
      cases h with
      | inl h => exact ⟨s, 1, h.trans (Nat.mul_comm 5 s ▸ rfl), Or.inl rfl⟩
      | inr h => exact ⟨s, 4, h.trans (Nat.mul_comm 5 s ▸ rfl), Or.inr rfl⟩

/-- **A factor ≡ 4 (mod 5).** -/
theorem has4 (F : Nat) : ∀ G N, N ≤ G → Dv N (20 * F * F + 40 * F + 19) → (∃ a, N = a * 5 + 4) →
    ∃ q, (∀ d, Dv d q → d = 1 ∨ d = q) ∧ (∃ b, q = b * 5 + 4) ∧ Dv q N
  | 0, N, hN, _, ⟨a, ha⟩ => absurd (ha ▸ hN) (fun h => Nat.not_succ_le_zero _ (Nat.le_trans (Nat.le_add_left _ _) h))
  | G + 1, N, hN, hN0, ⟨a, ha⟩ => by
    match lp_exists (a * 5 + 2) with
    | ⟨l, hl, hmin⟩ =>
    have hq := lp_prime _ l hl hmin
    have hlN : Dv (l + 1 + 1) N := by rw [ha]; exact hl
    match allowed5 F l hq (dv_trans _ _ _ hlN hN0) with
    | ⟨b, r, hb, hr⟩ =>
      cases hr with
      | inr h4 => exact ⟨l + 1 + 1, hq, ⟨b, by rw [hb, h4]⟩, hlN⟩
      | inl h1 =>
        match hlN with
        | ⟨N', hN'⟩ =>
        match cofactor5 b N' a (by rw [← h1, ← hb, ← ha, hN']; lib_nat) with
        | ⟨c, hc⟩ =>
        have hlt : N' ≤ G := by
          have e : N = N' + (N' * (l + 1)) := by rw [hN']; lib_nat
          have h1' : 1 ≤ N' * (l + 1) := by rw [hc]; exact Nat.mul_le_mul (Nat.le_add_left 1 _) (Nat.le_add_left 1 _)
          exact Nat.le_of_succ_le_succ (Nat.le_trans (Nat.add_le_add_left h1' N') (by rw [← e]; exact hN))
        have hN'0 : Dv N' (20 * F * F + 40 * F + 19) := dv_trans _ _ _ ⟨l + 1 + 1, by rw [hN']; lib_nat⟩ hN0
        match has4 F G N' hlt hN'0 ⟨c, hc⟩ with
        | ⟨q, hq', hq4, hqN⟩ => exact ⟨q, hq', hq4, dv_trans q N' N hqN ⟨l + 1 + 1, by rw [hN']; lib_nat⟩⟩

/-- **Infinitely many primes p ≡ 4 (mod 5).** -/
theorem inf_4mod5 (B : Nat) : ∃ b, (∀ d, Dv d (b * 5 + 4) → d = 1 ∨ d = b * 5 + 4) ∧ B < b * 5 + 4 := by
  match fac_pos B with
  | ⟨F, hf⟩ =>
  match has4 F (20 * F * F + 40 * F + 19) (20 * F * F + 40 * F + 19) (Nat.le_refl _) ⟨1, by lib_nat⟩ ⟨4 * F * F + 8 * F + 3, by lib_nat⟩ with
  | ⟨q, hq, ⟨b, hb⟩, hqN⟩ =>
    refine ⟨b, by rw [← hb]; exact hq, ?_⟩
    rw [← hb]
    cases Nat.lt_or_ge B q with
    | inl h => exact h
    | inr h =>
      exfalso
      match dv_fac B q (by rw [hb]; exact Nat.le_add_left 1 _) h with
      | ⟨r, hr⟩ =>
        have h5 : Dv q ((20 * F * F + 40 * F + 19) + 1) := ⟨20 * r * (F + 1), by
          rw [show (20 * F * F + 40 * F + 19) + 1 = 20 * (F + 1) * (F + 1) by lib_nat, ← hf, hr]; lib_nat⟩
        have hq2 : q = (b * 5 + 3) + 1 := hb
        rw [hq2] at hqN h5
        exact dv_one_absurd (b * 5 + 2) (dv_sub (b * 5 + 3) _ 1 hqN h5)

end Inf4mod5

#print axioms Inf4mod5.uniq_rem5
#print axioms Inf4mod5.mod5n
#print axioms Inf4mod5.cofactor5
#print axioms Inf4mod5.castZ5
#print axioms Inf4mod5.allowed5
#print axioms Inf4mod5.has4
#print axioms Inf4mod5.inf_4mod5

lang/Inf5m8.lean

lean_all/lang/Inf5m8.lean

/-
  lang/Inf5m8 — T43, cycle 537: **p ≡ 5 (mod 8), p ≡ 1 (mod 4n) beyond every bound, for every odd squarefree n ≥ 3.**

  x = 2nO (O odd factorial up to B + c), y = x² ≡ 4 (mod 8). PhiSq8.sqfree8 for the primes of 2n (2 prepended) gives F with
  Inv(2n, y, F), y^φ ≤ cF and F ≡ 5 (mod 8). A prime r | F has ord_r y = 2n, hence ord_r x = 4n (OrdQ.ord_of_pow with q = 2;
  ord x = 2n would give y^n = 1), so 4n | r − 1 and r ≡ 1 or 5 (mod 8); r ∤ y, so r > B. Two2.hasGc (modulus 8) picks r ≡ 5.
    no_half, inf_5m8
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.PhiSq8
import lang.InfSq3
import lang.Inf2em1a
import lang.StrongMult1

namespace PhiSq

open FTAChain ZMod Cheb TwoPrime PhiInv

/-- 2n | n is impossible for n ≥ 1. -/
theorem no_half (n : Nat) (hn : 1 ≤ n) (h : Dv (n * 2) n) : False := by
  obtain ⟨c, hc⟩ := h
  match c, hc with
  | 0, hc => rw [Nat.zero_mul] at hc; rw [hc] at hn; exact Nat.not_succ_le_zero 0 hn
  | c + 1, hc =>
    have h1 : n + 1 ≤ n := Nat.le_trans (Nat.add_le_add_left hn n) (Nat.le_trans (Nat.le_of_eq (by lib_nat))
      (Nat.le_trans (Nat.le_add_left (n * 2) (c * (n * 2))) (Nat.le_of_eq ((Nat.succ_mul c (n * 2)).symm.trans hc.symm))))
    exact Nat.not_succ_le_self n h1

theorem inf_5m8 (j : Nat) (L : List Nat) (hG : Good (j :: L)) (h0 : ∃ h, pr (j :: L) = 2 * h + 1) (B : Nat) :
    ∃ p, (∀ k, Dv k p → k = 1 ∨ k = p) ∧ B < p ∧ (∃ k, p = 8 * k + 5) ∧ ∃ s, p = s * (pr (j :: L) * 2 * 2) + 1 := by
  obtain ⟨h, hh⟩ := h0
  have hG2 : Good (0 :: j :: L) := ⟨StrongMult1.two_prime, fun hd => by
    obtain ⟨c, hc⟩ := hd; rw [hh] at hc; exact InfSplit.odd_not_dv2 h c hc, hG⟩
  obtain ⟨a, ha⟩ := Two2.ofac_odd (B + cs (j :: L) + 1)
  have hOge := ofac_ge (B + cs (j :: L))
  have hu : pr (j :: L) * Two2.ofac (B + cs (j :: L) + 1) = 2 * (2 * h * a + h + a) + 1 := by rw [hh, ha]; lib_nat
  generalize hU : pr (j :: L) * Two2.ofac (B + cs (j :: L) + 1) = u at hu
  generalize hv : 2 * h * a + h + a = v at hu
  generalize hy : (2 * u) * (2 * u) = y
  have hy8 : y = 8 * (2 * v * v + 2 * v) + 4 := by rw [← hy, hu]; lib_nat
  have hxA : AllDv (j :: L) (2 * u) := by
    rw [← hU]; exact allDv_mul 2 _ _ (by rw [Nat.mul_comm]; exact allDv_mul _ _ _ (allDv_pr (j :: L)))
  have hA : AllDv (0 :: j :: L) y := ⟨⟨2 * u * u, by rw [← hy]; lib_nat⟩, by rw [← hy]; exact allDv_mul _ _ _ hxA⟩
  have hu1 : 1 ≤ u := by rw [hu]; exact Nat.le_add_left 1 _
  have hcy : cs (j :: L) + 2 ≤ y := by
    have h1 : 1 ≤ pr (j :: L) := pr_pos _
    have e1 : cs (j :: L) + 2 ≤ 2 * (B + cs (j :: L) + 1) :=
      Nat.le_trans (Nat.le_add_left (cs (j :: L) + 2) (2 * B + cs (j :: L))) (Nat.le_of_eq (by lib_nat))
    have e2 : 2 * (B + cs (j :: L) + 1) ≤ 2 * u := Nat.mul_le_mul_left 2 (by
      rw [← hU]; exact Nat.le_trans hOge (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ h1)))
    have e3 : 2 * u ≤ y := by
      have h2u : 1 ≤ 2 * u := Nat.le_trans hu1 (Nat.le_trans (Nat.le_add_left u u) (Nat.le_of_eq (by lib_nat)))
      rw [← hy]; exact Nat.le_trans (Nat.le_of_eq (Nat.mul_one _).symm) (Nat.mul_le_mul_left _ h2u)
    exact Nat.le_trans e1 (Nat.le_trans e2 e3)
  obtain ⟨y', hy'⟩ := le_dest2 2 y (Nat.le_trans (Nat.le_add_left 2 _) hcy)
  rw [← hy'] at hA
  obtain ⟨F, I, l, _, hR⟩ := sqfree8 0 (j :: L) hG2 y' hA
  rw [show y' + 1 + 1 = y from hy'] at I l hR
  obtain ⟨f, hf⟩ := hR.1 _ hy8
  have hy1 : 1 ≤ y := Nat.le_trans (Nat.le_add_left 1 _) hcy
  have hC : cs (j :: L) < y ^ ph 0 (j :: L) :=
    Nat.le_trans (Nat.le_add_right (cs (j :: L) + 1) 1) (Nat.le_trans hcy (pow_ge y hy1 _ (ph_pos 0 (j :: L))))
  have hF2 := PhiSize.two_le _ _ _ l hC
  have hn1 : 1 ≤ pr (j :: L) := pr_pos _
  -- facts about a prime factor of F
  have hfac : ∀ l, (∀ e, Dv e (l + 1 + 1) → e = 1 ∨ e = l + 1 + 1) → Dv (l + 1 + 1) F →
      (Dv (l + 1 + 1) y → False) ∧ ∃ s, l + 1 + 1 = s * (pr (j :: L) * 2 * 2) + 1 := by
    intro l hq hdv
    obtain ⟨hO, hg⟩ := (I l 0 hq).1 (dv_pow1 _ _ hdv)
    have hpr2 : pr (0 :: j :: L) = pr (j :: L) * 2 := rfl
    rw [hpr2] at hO hg
    obtain ⟨n', hn'⟩ := le_dest2 1 (pr (j :: L) * 2) (Nat.le_trans hn1 (Nat.le_trans (Nat.le_of_eq (Nat.mul_one _).symm) (Nat.mul_le_mul_left _ (Nat.le_add_left 1 1))))
    have hqy : Dv (l + 1 + 1) y → False := fun hd => by
      have hg1 : Dv (l + 1 + 1) (gN y (pr (j :: L) * 2)) := dv_base _ 0 _ hg
      rw [← hn', Nat.add_comm n' 1, gN_one] at hg1
      exact dv_one_absurd l (dv_sub (l + 1) (y * gN y n') 1 (dv_trans _ _ _ hd ⟨gN y n', Nat.mul_comm _ _⟩)
        (by rw [Nat.add_comm (y * gN y n') 1]; exact hg1))
    refine ⟨hqy, ?_⟩
    rcases EveryPrime.nat_even_odd l with ⟨i, hi⟩ | ⟨m, hm⟩
    · exfalso
      rcases hq 2 ⟨i + 1, by rw [hi]; lib_nat⟩ with e | e
      · exact Nat.noConfusion (Nat.succ.inj e)
      · exact hqy (by rw [← e, ← hy]; exact ⟨2 * u * u, by lib_nat⟩)
    · subst hm
      have hey : emb (ring (2 * m + 1 + 1)) y = Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) (2 * u)) (0 + 1 + 1) := by
        rw [← hy, emb_mul]; show _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one _) _; lib_ring
      rw [hey] at hO
      rcases OrdQ.ord_of_pow (ring (2 * m + 1 + 1)) _ _ 0 hO (Nat.le_trans hn1 (Nat.le_trans (Nat.le_of_eq (Nat.mul_one _).symm) (Nat.mul_le_mul_left _ (Nat.le_add_left 1 1)))) StrongMult1.two_prime with h1 | h1
      · exfalso
        have hyn : Frobenius.pw (ring (2 * m + 1 + 1)) (Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) (2 * u)) (0 + 1 + 1)) (pr (j :: L)) = (ring (2 * m + 1 + 1)).one := by
          rw [OrdQ.pw_pw, Nat.mul_comm (0 + 1 + 1) (pr (j :: L))]; exact h1.1
        exact no_half _ hn1 (hO.2 _ hyn)
      · have hX0 : emb (ring (2 * m + 1 + 1)) (2 * u) ≠ (ring (2 * m + 1 + 1)).zero := fun e0 =>
          hqy (dv_trans _ _ _ (dv_of_emb _ _ e0) ⟨2 * u, hy.symm⟩)
        obtain ⟨r, hr⟩ := h1.2 _ (Cyclo2l.ferm1 m hq _ hX0)
        exact ⟨r, by show 2 * m + 1 + 1 + 1 = _; rw [hr]⟩
  have hall : ∀ l, (∀ e, Dv e (l + 1 + 1) → e = 1 ∨ e = l + 1 + 1) → Dv (l + 1 + 1) F →
      (∃ a, l + 1 + 1 = a * (7 + 1) + 1) ∨ (∃ a, l + 1 + 1 = a * (7 + 1) + (3 + 2)) := by
    intro l hq hdv
    obtain ⟨_, s0, hs0⟩ := hfac l hq hdv
    rw [hs0, hh]
    rcases EveryPrime.nat_even_odd s0 with ⟨t, ht⟩ | ⟨t, ht⟩
    · exact Or.inl ⟨t * (2 * h + 1), by rw [ht]; lib_nat⟩
    · exact Or.inr ⟨t * (2 * h + 1) + h, by rw [ht]; lib_nat⟩
  obtain ⟨q, hq, ⟨b, hb⟩, hqF⟩ := Two2.hasGc 7 3 F (Nat.succ_le_succ (Nat.le_add_left 5 2)) hall F F (Nat.le_refl _) (dv_refl _) ⟨f, by rw [hf]; lib_nat⟩
  have hq2 : 2 ≤ q := by rw [hb]; exact Nat.le_trans (Nat.le_add_left 2 3) (Nat.le_add_left _ _)
  obtain ⟨q', hq'⟩ := le_dest2 2 q hq2
  rw [← hq'] at hq hqF hb
  have hq'' : ∀ e, Dv e (q' + 1 + 1) → e = 1 ∨ e = q' + 1 + 1 := hq
  obtain ⟨hqy, s, hs⟩ := hfac q' hq'' hqF
  refine ⟨q' + 2, hq, ?_, ⟨b, by show q' + 1 + 1 = _; rw [hb]; lib_nat⟩, ⟨s, hs⟩⟩
  cases Nat.lt_or_ge B (q' + 2) with
  | inl hlt => exact hlt
  | inr hge =>
    exfalso
    rcases EveryPrime.nat_even_odd (q' + 2) with ⟨i, hi⟩ | ⟨i, hi⟩
    · rcases hq'' 2 ⟨i, hi.trans (Nat.mul_comm 2 i)⟩ with e | e
      · exact Nat.noConfusion (Nat.succ.inj e)
      · exact hqy (by rw [← e, ← hy]; exact ⟨2 * u * u, by lib_nat⟩)
    · have hle : i ≤ 2 * i + 1 := Nat.le_succ_of_le (Nat.le_trans (Nat.le_add_left i i) (Nat.le_of_eq (by lib_nat)))
      have hB : 2 * i + 1 ≤ B + cs (j :: L) := by rw [hi] at hge; exact Nat.le_trans hge (Nat.le_add_right B _)
      have hqO := Two2.dv_ofac (B + cs (j :: L) + 1) i (Nat.succ_le_succ (Nat.le_trans hle hB))
      apply hqy
      show Dv (q' + 2) y
      rw [hi, ← hy, ← hU]
      exact dv_trans _ _ _ hqO ⟨2 * (2 * (pr (j :: L) * Two2.ofac (B + cs (j :: L) + 1))) * pr (j :: L), by lib_nat⟩

end PhiSq

#print axioms PhiSq.no_half
#print axioms PhiSq.inf_5m8

lang/Inf5mod12.lean

lean_all/lang/Inf5mod12.lean

/-
  lang/Inf5mod12 — T43, cycle 204: infinitely many primes p ≡ 5 (mod 12).

  x = g(B) = Π_{k ≤ B} (3k + 1)(3k + 2) (prime to 3, divisible by every n ≤ 3B + 2 with 3 ∤ n), N = 4x² + 1 ≡ 5 (mod 12).
  Every prime factor q has −1 = (2x)² a square, so q ≡ 1 (mod 4) (GaussSplit), and 3 ∤ q; hence q ≡ 1 or 5 (mod 12); the product
  is ≡ 5, so some factor is ≡ 5; a factor q = 12b + 5 ≤ B divides x (q = 3(4b + 1) + 2) and cannot divide N.
    uniq_r12, mod12m, cofactor5, g, g_mod3, dv_g, allowed5, has5, inf_5mod12
  With Inf1mod12, Inf7mod12, Inf11mod12: every unit class mod 12.

  Lean 4 core only, proofs by hand (lib_nat, lib_int), no axioms.
-/
import lang.Inf1mod12
import lang.ZetaAll3

namespace Inf5mod12

open FTAChain ZMod LibInt InfSplit
open LibRing (castZ castN)

theorem uniq_r12 (x y r c : Nat) (hr : r < 12) (hc : c < 12) (h : x * 12 + r = y * 12 + c) : r = c := by
  have big : ∀ x y r c : Nat, r < 12 → x < y → x * 12 + r = y * 12 + c → False := fun x y r c hr hxy h => by
    have h1 : x * 12 + r < (x + 1) * 12 := by rw [Nat.succ_mul]; exact Nat.add_lt_add_left hr _
    have h2 : (x + 1) * 12 ≤ y * 12 := Nat.mul_le_mul_right 12 hxy
    exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (h ▸ h1) (Nat.le_trans h2 (Nat.le_add_right _ _)))
  cases Nat.lt_or_ge x y with
  | inl hxy => exact absurd h (fun h' => big x y r c hr hxy h')
  | inr hxy =>
    cases Nat.lt_or_ge y x with
    | inl hyx => exact absurd h.symm (fun h' => big y x c r hc hyx h')
    | inr hyx =>
      have e : x = y := Nat.le_antisymm hyx hxy
      subst e
      apply Int.ofNat.inj
      have := congrArg Int.ofNat h
      have e' : Int.ofNat x * 12 + Int.ofNat r = Int.ofNat x * 12 + Int.ofNat c := this
      calc Int.ofNat r = (Int.ofNat x * 12 + Int.ofNat r) - Int.ofNat x * 12 := by lib_int
        _ = Int.ofNat c := by rw [e']; lib_int

/-- residue of n mod 8 as a case split. -/
theorem mod12m (n : Nat) : ∃ a r, n = a * 12 + r ∧ r < 12 :=
  match div_exists 11 n with
  | ⟨a, r, k, hn, hr⟩ => ⟨a, r, hn, Nat.lt_of_lt_of_le (Nat.lt_succ_self r) (by rw [show (12 : Nat) = 11 + 1 from rfl, ← hr]; exact Nat.succ_le_succ (Nat.le_add_right r k))⟩

theorem cofactor5 (b N' a : Nat) (h : (b * 12 + 1) * N' = a * 12 + 5) : ∃ c, N' = c * 12 + 5 :=
  match mod12m N' with
  | ⟨c, r, hc, hr⟩ => by
    have e : (b * 12 + 1) * N' = (b * 12 * c + b * r + c) * 12 + r := by rw [hc]; lib_nat
    rw [e] at h
    exact ⟨c, by rw [hc, uniq_r12 _ _ r 5 hr (Nat.lt_of_lt_of_le (Nat.lt_succ_self 5) (Nat.le_add_right 6 6)) h]⟩





/-- g B = Π_{k ≤ B} (3k + 1)(3k + 2): prime to 3, divisible by every n ≤ 3B + 2 with 3 ∤ n. -/
def g : Nat → Nat
  | 0 => 2
  | B + 1 => (3 * (B + 1) + 1) * (3 * (B + 1) + 2) * g B

theorem mul_mod3 (c G s t : Nat) (hc : c = 3 * s + 2) (hG : G * G = 3 * t + 1) :
    (c * G) * (c * G) = 3 * ((3 * s + 2) * (3 * s + 2) * t + 3 * s * s + 4 * s + 1) + 1 := by
  rw [show (c * G) * (c * G) = (c * c) * (G * G) by lib_nat, hG, hc]; lib_nat

theorem g_mod3 : ∀ B, ∃ t, g B * g B = 3 * t + 1
  | 0 => ⟨1, rfl⟩
  | B + 1 => match g_mod3 B with
    | ⟨t, ht⟩ => ⟨_, mul_mod3 _ _ (3 * (B + 1) * (B + 1) + 3 * (B + 1)) t (by lib_nat) ht⟩

theorem dv_g : ∀ B k, k ≤ B → Dv (3 * k + 1) (g B) ∧ Dv (3 * k + 2) (g B)
  | 0, k, hk => by
    match k, hk with
    | 0, _ => exact ⟨⟨2, rfl⟩, ⟨1, rfl⟩⟩
  | B + 1, k, hk => by
    cases Nat.lt_or_ge k (B + 1) with
    | inl h =>
      match dv_g B k (Nat.le_of_lt_succ h) with
      | ⟨⟨u, hu⟩, ⟨v, hv⟩⟩ =>
        exact ⟨⟨(3 * (B + 1) + 1) * (3 * (B + 1) + 2) * u, by show _ * g B = _; rw [hu]; lib_nat⟩,
          ⟨(3 * (B + 1) + 1) * (3 * (B + 1) + 2) * v, by show _ * g B = _; rw [hv]; lib_nat⟩⟩
    | inr h =>
      have e : k = B + 1 := Nat.le_antisymm hk h
      subst e
      exact ⟨⟨(3 * (B + 1) + 2) * g B, by show _ * g B = _; lib_nat⟩, ⟨(3 * (B + 1) + 1) * g B, by show _ * g B = _; lib_nat⟩⟩

/-- prime factors of 4x² + 1 with x² ≡ 1 (mod 3) are ≡ 1 or 5 (mod 12). -/
theorem allowed5 (x l : Nat) (h3x : ∃ t, x * x = 3 * t + 1) (hq : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1)
    (hdv : Dv (l + 1 + 1) (4 * x * x + 1)) : ∃ b t, l + 1 + 1 = b * 12 + t ∧ (t = 1 ∨ t = 5) := by
  have hy : (ring (l + 1)).mul (mk (l + 1) (2 * x)) (mk (l + 1) (2 * x)) = (ring (l + 1)).neg (ring (l + 1)).one := by
    apply DetChain.neg_unique
    rw [TwoPower.mk_mul, show (ring (l + 1)).one = mk (l + 1) 1 from rfl, PrimeOneModQ.mk_add,
      show 2 * x * (2 * x) + 1 = 4 * x * x + 1 by lib_nat]
    exact Subtype.ext (dv_md_zero (l + 1) _ hdv)
  have hodd : ∀ c, l + 1 + 1 = 2 * c → False := fun c hc => by
    have h2 : l + 1 + 1 = 2 := by
      cases hq 2 ⟨c, by rw [hc]; lib_nat⟩ with
      | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
      | inr e => exact e.symm
    rw [h2] at hdv
    match hdv with
    | ⟨r, hr⟩ => exact odd_not_dv2 (2 * x * x) r (by rw [← hr]; lib_nat)
  have h3 : ∀ c, l + 1 + 1 = 3 * c → False := fun c hc => by
    have e3 : l + 1 + 1 = 3 := by
      cases hq 3 ⟨c, by rw [hc]; lib_nat⟩ with
      | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
      | inr e => exact e.symm
    rw [e3] at hdv
    match hdv, h3x with
    | ⟨w, hw⟩, ⟨t, ht⟩ =>
      have hI : Int.ofNat w * 3 = 3 * (4 * Int.ofNat t + 1) + 2 :=
        (congrArg Int.ofNat hw).symm.trans (congrArg Int.ofNat (by rw [show (4 * x * x + 1) = 4 * (x * x) + 1 by lib_nat, ht]; lib_nat))
      exact EisenAp.three_ne_r (Int.ofNat w - (4 * Int.ofNat t + 1)) 2 (fun e => Nat.noConfusion e) (Nat.lt_succ_self 2)
        (by show _ = 2; rw [show 3 * (Int.ofNat w - (4 * Int.ofNat t + 1)) = Int.ofNat w * 3 - 3 * (4 * Int.ofNat t + 1) by lib_int, hI]; lib_int)
  match EveryPrime.nat_even_odd l with
  | Or.inl ⟨c, hc⟩ => exact absurd (by rw [hc]; lib_nat : l + 1 + 1 = 2 * (c + 1)) (fun e => hodd (c + 1) e)
  | Or.inr ⟨r, hr⟩ =>
    subst hr
    match EveryPrime.nat_even_odd r with
    | Or.inl ⟨k, hk⟩ => subst hk; exact absurd hy (GaussSplit.inert_three_mod_four k hq _)
    | Or.inr ⟨k, hk⟩ =>
      subst hk
      match ZetaAll3.mod3_cases k with
      | Or.inl ⟨d, hd⟩ => exact ⟨d, 5, by rw [hd]; lib_nat, Or.inr rfl⟩
      | Or.inr (Or.inl ⟨d, hd⟩) => exact absurd (by rw [hd]; lib_nat : 2 * (2 * k + 1) + 1 + 1 + 1 = 3 * (4 * d + 3)) (fun e => h3 _ e)
      | Or.inr (Or.inr ⟨d, hd⟩) => exact ⟨d + 1, 1, by rw [hd]; lib_nat, Or.inl rfl⟩

theorem has5 (x : Nat) (h3x : ∃ t, x * x = 3 * t + 1) : ∀ G N, N ≤ G → Dv N (4 * x * x + 1) → (∃ a, N = a * 12 + 5) →
    ∃ q, (∀ d, Dv d q → d = 1 ∨ d = q) ∧ (∃ b, q = b * 12 + 5) ∧ Dv q N
  | 0, N, hN, _, ⟨a, ha⟩ => absurd (ha ▸ hN) (fun h => Nat.not_succ_le_zero _ (Nat.le_trans (Nat.le_add_left _ _) h))
  | G + 1, N, hN, hN0, ⟨a, ha⟩ => by
    match lp_exists (a * 12 + 3) with
    | ⟨l, hl, hmin⟩ =>
    have hq := lp_prime _ l hl hmin
    have hlN : Dv (l + 1 + 1) N := by rw [ha]; exact hl
    match allowed5 x l h3x hq (dv_trans _ _ _ hlN hN0) with
    | ⟨b, t, hb, ht⟩ =>
      cases ht with
      | inr h5 => exact ⟨l + 1 + 1, hq, ⟨b, by rw [hb, h5]⟩, hlN⟩
      | inl h1 =>
        match hlN with
        | ⟨N', hN'⟩ =>
        match cofactor5 b N' a (by rw [← h1, ← hb, ← ha, hN']; lib_nat) with
        | ⟨c, hc⟩ =>
        have hlt : N' ≤ G := by
          have e : N = N' + (N' * (l + 1)) := by rw [hN']; lib_nat
          have h1' : 1 ≤ N' * (l + 1) := by rw [hc]; exact Nat.mul_le_mul (Nat.le_add_left 1 _) (Nat.le_add_left 1 _)
          exact Nat.le_of_succ_le_succ (Nat.le_trans (Nat.add_le_add_left h1' N') (by rw [← e]; exact hN))
        have hN'0 : Dv N' (4 * x * x + 1) := dv_trans _ _ _ ⟨l + 1 + 1, by rw [hN']; lib_nat⟩ hN0
        match has5 x h3x G N' hlt hN'0 ⟨c, hc⟩ with
        | ⟨q, hq', hq5, hqN⟩ => exact ⟨q, hq', hq5, dv_trans q N' N hqN ⟨l + 1 + 1, by rw [hN']; lib_nat⟩⟩

/-- **Infinitely many primes p ≡ 5 (mod 12).** -/
theorem inf_5mod12 (B : Nat) : ∃ b, (∀ d, Dv d (b * 12 + 5) → d = 1 ∨ d = b * 12 + 5) ∧ B < b * 12 + 5 := by
  match g_mod3 B with
  | ⟨t, ht⟩ =>
  have hN : 4 * g B * g B + 1 = t * 12 + 5 := by rw [show 4 * g B * g B + 1 = 4 * (g B * g B) + 1 by lib_nat, ht]; lib_nat
  match has5 (g B) ⟨t, ht⟩ _ _ (Nat.le_refl _) ⟨1, by lib_nat⟩ ⟨t, hN⟩ with
  | ⟨q, hq, ⟨b, hb⟩, hqN⟩ =>
    refine ⟨b, by rw [← hb]; exact hq, ?_⟩
    rw [← hb]
    cases Nat.lt_or_ge B q with
    | inl h => exact h
    | inr h =>
      exfalso
      -- q = 12b + 5 = 3(4b + 1) + 2 ≤ B: q divides g B
      have hk : 4 * b + 1 ≤ B := Nat.le_trans (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq (show 4 * b + 1 + (8 * b + 4) = b * 12 + 5 by lib_nat))) (hb ▸ h)
      match (dv_g B (4 * b + 1) hk).2 with
      | ⟨r, hr⟩ =>
        have hr' : g B = r * q := by rw [hr, hb]; lib_nat
        have h1 : Dv q (4 * g B * g B) := ⟨4 * r * g B, by rw [hr']; lib_nat⟩
        have hq2 : q = (b * 12 + 4) + 1 := hb
        rw [hq2] at hqN h1
        exact dv_one_absurd (b * 12 + 3) (dv_sub (b * 12 + 4) _ 1 h1 hqN)

end Inf5mod12

#print axioms Inf5mod12.uniq_r12
#print axioms Inf5mod12.mod12m
#print axioms Inf5mod12.cofactor5
#print axioms Inf5mod12.g
#print axioms Inf5mod12.mul_mod3
#print axioms Inf5mod12.g_mod3
#print axioms Inf5mod12.dv_g
#print axioms Inf5mod12.allowed5
#print axioms Inf5mod12.has5
#print axioms Inf5mod12.inf_5mod12

lang/Inf5mod24.lean

lean_all/lang/Inf5mod24.lean

/-
  lang/Inf5mod24 — T43, cycle 214: infinitely many primes p ≡ 5 (mod 24); with cycles 207–213, Dirichlet for every class mod 24.

  m = Π_{j<B}(5j + 1)(5j + 2)(5j + 3)(5j + 4) (m² ≡ 1 mod 5), k = 168·m², i = k − 3. With A = i² + 6i + 7, B = i² + 4i + 2,
  C = i² + 5i + 5:  M = A² + B² = 2C² + 3 ≡ 5 (mod 24), ≡ 5 (mod 7), ≡ 3 (mod 5). Mod a prime factor q of M:
    B is invertible (q | A, B would give q | 7),  −1 = (A/B)²  (so q ≡ 1 mod 4, GaussSplit),  −6 = (2C)²;
    q ≡ 1 (mod 8): 2 = t² (TwoSupp.two_8m1), −3 = (2C/t)², so q ≡ 1 (mod 3) (Eisen.inert_neg3): q ≡ 1 (mod 24);
    q ≡ 5 (mod 8): q ≡ 1 (mod 3) would give −3 = z² (Eisen.split_neg3) and 2 = (2C/z)², against TwoSupp.two_8m5: q ≡ 5 (mod 24).
  M ≡ 5 forces a factor ≡ 5 (Inf17mod24.has_gen); q ≠ 5 (M ≡ 3 mod 5), and q ≤ B divides m, hence k = i + 3, hence C + 1, hence 5.
    sq_zero, mul5, m5, blk5, m5_sq, m5_dv, mk3, ab_id, classify5, allowed5, c12_gen, m24_gen, c7_gen, m7_gen, m5_gen, cq_gen, bound5, main5, k_gen, u_gen, v_gen, inf_5mod24
  The class 5 mod 24: split in ℚ(i) and ℚ(√6), inert in ℚ(√2), ℚ(√3), ℚ(√−2), ℚ(√−3).

  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Inf11mod24

namespace Inf5mod24

open FTAChain ZMod InfSplit TwoSupp
open Inf3mod8 (two_eq sq_rem rem_sq)

/-- a² = 0 in 𝔽_q ⟹ a = 0. -/
theorem sq_zero (l : Nat) (hq : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1) (a : Zp (l + 1))
    (h : (ring (l + 1)).mul a a = (ring (l + 1)).zero) : a = (ring (l + 1)).zero := by
  cases Nat.decEq a.val (ring (l + 1)).zero.val with
  | isTrue h0 => exact Subtype.ext h0
  | isFalse h0 =>
    exfalso
    have hne : a ≠ (ring (l + 1)).zero := fun e => h0 (congrArg Subtype.val e)
    have hv := ZMod.inv_exists l hq a hne
    apply hne
    calc a = (ring (l + 1)).mul (ring (l + 1)).one a := ((ring (l + 1)).one_mul a).symm
      _ = (ring (l + 1)).mul ((ring (l + 1)).mul (Frobenius.pw (ring (l + 1)) a l) a) a := by rw [hv]
      _ = (ring (l + 1)).mul (Frobenius.pw (ring (l + 1)) a l) ((ring (l + 1)).mul a a) := by lib_ring
      _ = (ring (l + 1)).zero := by rw [h, DetChain.mul_zero]

theorem mul5 (a b r s : Nat) : (5 * a + r) * (5 * b + s) = 5 * (5 * a * b + a * s + r * b) + r * s := by lib_nat

/-- product of the numbers prime to 5 below 5B. -/
def m5 : Nat → Nat
  | 0 => 1
  | j + 1 => m5 j * ((5 * j + 1) * (5 * j + 2) * (5 * j + 3) * (5 * j + 4))

theorem blk5 (j : Nat) : ∃ f, (5 * j + 1) * (5 * j + 2) * (5 * j + 3) * (5 * j + 4) = 5 * f + 4 := by
  have h1 := mul5 j j 1 2
  generalize 5 * j * j + j * 2 + 1 * j = x1 at h1
  have h2 := mul5 x1 j 2 3
  generalize 5 * x1 * j + x1 * 3 + 2 * j = x2 at h2
  have h3 := mul5 (x2 + 1) j 1 4
  generalize 5 * (x2 + 1) * j + (x2 + 1) * 4 + 1 * j = x3 at h3
  refine ⟨x3, ?_⟩
  rw [h1, show 5 * x1 + 1 * 2 = 5 * x1 + 2 from rfl, h2, show 5 * x2 + 2 * 3 = 5 * (x2 + 1) + 1 by lib_nat, h3]

theorem m5_sq : ∀ B, ∃ E, m5 B * m5 B = 5 * E + 1
  | 0 => ⟨0, rfl⟩
  | j + 1 => by
    match m5_sq j, blk5 j with
    | ⟨E, hE⟩, ⟨f, hf⟩ =>
      have e1 : m5 (j + 1) * m5 (j + 1) = (m5 j * m5 j) * (((5 * j + 1) * (5 * j + 2) * (5 * j + 3) * (5 * j + 4)) * ((5 * j + 1) * (5 * j + 2) * (5 * j + 3) * (5 * j + 4))) := by
        show (m5 j * ((5 * j + 1) * (5 * j + 2) * (5 * j + 3) * (5 * j + 4))) * (m5 j * ((5 * j + 1) * (5 * j + 2) * (5 * j + 3) * (5 * j + 4))) = _
        generalize (5 * j + 1) * (5 * j + 2) * (5 * j + 3) * (5 * j + 4) = X
        lib_nat
      rw [e1, hE, hf]
      exact ⟨5 * E * (5 * f * f + 8 * f + 3) + E + (5 * f * f + 8 * f + 3), by lib_nat⟩

theorem m5_dv : ∀ B j r, j < B → 1 ≤ r → r ≤ 4 → Dv (5 * j + r) (m5 B)
  | 0, _, _, h, _, _ => absurd h (Nat.not_succ_le_zero _)
  | B + 1, j, r, h, h1, h4 => by
    show Dv (5 * j + r) (m5 B * ((5 * B + 1) * (5 * B + 2) * (5 * B + 3) * (5 * B + 4)))
    cases Nat.lt_or_ge j B with
    | inl hj => exact Inf17mod24.dv_mulr _ _ _ (m5_dv B j r hj h1 h4)
    | inr hj =>
      have e : j = B := Nat.le_antisymm (Nat.le_of_succ_le_succ h) hj
      subst e
      generalize m5 j = d
      match r, h1, h4 with
      | 0, h1, _ => exact absurd h1 (Nat.not_succ_le_zero 0)
      | 1, _, _ =>
        generalize 5 * j + 1 = y1; generalize 5 * j + 2 = y2; generalize 5 * j + 3 = y3; generalize 5 * j + 4 = y4
        exact ⟨d * y2 * y3 * y4, by lib_nat⟩
      | 2, _, _ =>
        generalize 5 * j + 1 = y1; generalize 5 * j + 2 = y2; generalize 5 * j + 3 = y3; generalize 5 * j + 4 = y4
        exact ⟨d * y1 * y3 * y4, by lib_nat⟩
      | 3, _, _ =>
        generalize 5 * j + 1 = y1; generalize 5 * j + 2 = y2; generalize 5 * j + 3 = y3; generalize 5 * j + 4 = y4
        exact ⟨d * y1 * y2 * y4, by lib_nat⟩
      | 4, _, _ =>
        generalize 5 * j + 1 = y1; generalize 5 * j + 2 = y2; generalize 5 * j + 3 = y3; generalize 5 * j + 4 = y4
        exact ⟨d * y1 * y2 * y3, by lib_nat⟩
      | r + 5, _, h4 => exact absurd h4 (fun h => Nat.lt_irrefl 4 (Nat.lt_of_lt_of_le (Nat.lt_of_lt_of_le (Nat.lt_succ_self 4) (Nat.le_add_left 5 r)) h))

theorem mk3 (l : Nat) : mk (l + 1) 3 = Eisen.three l := by
  show mk (l + 1) (1 + 2) = _
  rw [← PrimeOneModQ.mk_add (l + 1) 1 2, two_eq]; rfl

theorem ab_id (i : Nat) : (i * i + 6 * i + 7) * (i * i + 6 * i + 7) + (i * i + 4 * i + 2) * (i * i + 4 * i + 2)
    = 2 * (i * i + 5 * i + 5) * (i * i + 5 * i + 5) + 3 := by lib_nat

theorem quot5 (l : Nat) (y t c : Zp (l + 1)) (hy : (ring (l + 1)).mul y y = c) :
    (ring (l + 1)).mul ((ring (l + 1)).mul y (Frobenius.pw (ring (l + 1)) t l)) ((ring (l + 1)).mul y (Frobenius.pw (ring (l + 1)) t l)) = (ring (l + 1)).mul c ((ring (l + 1)).mul (Frobenius.pw (ring (l + 1)) t l) (Frobenius.pw (ring (l + 1)) t l)) := by
  rw [← hy]; lib_ring

theorem cl_r1 (l : Nat) (hq : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1) (_hodd : ∀ c, l + 1 + 1 = 2 * c → False)
    (hl2 : 2 ≤ l) (w : Zp (l + 1))
    (hw : (ring (l + 1)).mul w w = (ring (l + 1)).neg ((ring (l + 1)).mul (mk (l + 1) 2) (Eisen.three l))) (b : Nat) (hb : l + 1 + 1 = b * 8 + 1) :
    ∃ c t, l + 1 + 1 = c * 24 + t ∧ (t = 1 ∨ t = 5) := by
  have hPr : Pr (l + 1 + 1) := ⟨⟨l, rfl⟩, hq⟩
  have quot : ∀ (y t : Zp (l + 1)) (c d : Zp (l + 1)), (ring (l + 1)).mul y y = c → (ring (l + 1)).mul t t = d → (ring (l + 1)).mul (Frobenius.pw (ring (l + 1)) t l) t = (ring (l + 1)).one →
      (ring (l + 1)).mul ((ring (l + 1)).mul y (Frobenius.pw (ring (l + 1)) t l)) ((ring (l + 1)).mul y (Frobenius.pw (ring (l + 1)) t l)) = (ring (l + 1)).mul c ((ring (l + 1)).mul (Frobenius.pw (ring (l + 1)) t l) (Frobenius.pw (ring (l + 1)) t l)) :=
    fun y t c _ hy _ _ => quot5 l y t c hy
  match b, hb with
  | 0, hb0 => exact absurd (Nat.succ.inj hb0) (fun e => Nat.noConfusion e)
  | m + 1, hb =>
  have e : (m + 1 + (m + 1)) + ((m + 1 + (m + 1)) + 0) + ((m + 1 + (m + 1)) + ((m + 1 + (m + 1)) + 0)) + 1 = l + 1 + 1 := by rw [hb]; lib_nat
  match two_8m1 m (by rw [e]; exact hPr) with
  | ⟨t0, ht0⟩ =>
    rw [e] at ht0
    have ht := rem_sq l t0 ht0
    have ht1 : mk (l + 1) t0 ≠ (ring (l + 1)).zero := fun e0 => by
      rw [e0, DetChain.mul_zero] at ht
      have h2 : Dv (l + 1 + 1) 2 := md_zero_dv (l + 1) 2 (congrArg Subtype.val ht.symm)
      match dv_le (l + 1 + 1) 1 h2 with
      | ⟨k, hk⟩ =>
        have h0 : l + k = 0 := Nat.succ.inj (Nat.succ.inj ((show l + k + 1 + 1 = l + 1 + 1 + k by lib_nat).trans hk))
        exact absurd (h0 ▸ Nat.le_trans hl2 (Nat.le_add_right l k)) (Nat.not_succ_le_zero 1)
    have hinv := ZMod.inv_exists l hq _ ht1
    have hz : (ring (l + 1)).mul ((ring (l + 1)).mul w (Frobenius.pw (ring (l + 1)) (mk (l + 1) t0) l)) ((ring (l + 1)).mul w (Frobenius.pw (ring (l + 1)) (mk (l + 1) t0) l)) = (ring (l + 1)).neg (Eisen.three l) := by
      rw [quot w (mk (l + 1) t0) _ _ hw ht hinv]
      rw [show (ring (l + 1)).mul ((ring (l + 1)).neg ((ring (l + 1)).mul (mk (l + 1) 2) (Eisen.three l))) ((ring (l + 1)).mul (Frobenius.pw (ring (l + 1)) (mk (l + 1) t0) l) (Frobenius.pw (ring (l + 1)) (mk (l + 1) t0) l))
          = (ring (l + 1)).neg ((ring (l + 1)).mul (Eisen.three l) ((ring (l + 1)).mul ((ring (l + 1)).mul (Frobenius.pw (ring (l + 1)) (mk (l + 1) t0) l) (Frobenius.pw (ring (l + 1)) (mk (l + 1) t0) l)) (mk (l + 1) 2))) by lib_ring,
        ← ht, show (ring (l + 1)).mul ((ring (l + 1)).mul (Frobenius.pw (ring (l + 1)) (mk (l + 1) t0) l) (Frobenius.pw (ring (l + 1)) (mk (l + 1) t0) l)) ((ring (l + 1)).mul (mk (l + 1) t0) (mk (l + 1) t0))
          = (ring (l + 1)).mul ((ring (l + 1)).mul (Frobenius.pw (ring (l + 1)) (mk (l + 1) t0) l) (mk (l + 1) t0)) ((ring (l + 1)).mul (Frobenius.pw (ring (l + 1)) (mk (l + 1) t0) l) (mk (l + 1) t0)) by lib_ring, hinv]
      lib_ring
    match Eisen.inert_neg3 l hq hl2 _ hz with
    | ⟨c3, hc3⟩ =>
      have hl1 : l + 1 = (m + 1) * 8 := Nat.succ.inj hb
      match Inf7mod24.mod3 (m + 1) with
      | ⟨e3, r3, hr3, hr3l⟩ =>
        match r3, hr3l with
        | 0, _ => exact ⟨e3, 1, by rw [hb, hr3]; lib_nat, Or.inl rfl⟩
        | 1, _ =>
          exfalso
          exact Nat.noConfusion (Inf7mod24.uniq_rem3 (e3 * 8 + 2) c3 2 0 (Nat.lt_succ_self 2) (Nat.zero_lt_succ 2)
            (calc (e3 * 8 + 2) * 3 + 2 = (e3 * 3 + 1) * 8 := by lib_nat
              _ = (m + 1) * 8 := by rw [hr3]
              _ = l + 1 := hl1.symm
              _ = c3 * (1 + 2) := hc3
              _ = c3 * 3 + 0 := by lib_nat))
        | 2, _ =>
          exfalso
          exact Nat.noConfusion (Inf7mod24.uniq_rem3 (e3 * 8 + 5) c3 1 0 (Nat.lt_succ_of_lt (Nat.lt_succ_self 1)) (Nat.zero_lt_succ 2)
            (calc (e3 * 8 + 5) * 3 + 1 = (e3 * 3 + 2) * 8 := by lib_nat
              _ = (m + 1) * 8 := by rw [hr3]
              _ = l + 1 := hl1.symm
              _ = c3 * (1 + 2) := hc3
              _ = c3 * 3 + 0 := by lib_nat))
        | r3 + 3, hr3l => exact absurd hr3l (fun h => Nat.lt_irrefl 3 (Nat.lt_of_le_of_lt (Nat.le_add_left 3 r3) h))

theorem cl_r5 (l : Nat) (hq : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1)
    (h3 : l + 1 + 1 ≠ 3) (hl2 : 2 ≤ l) (w : Zp (l + 1))
    (hw : (ring (l + 1)).mul w w = (ring (l + 1)).neg ((ring (l + 1)).mul (mk (l + 1) 2) (Eisen.three l))) (b : Nat) (hb : l + 1 + 1 = b * 8 + 5) :
    ∃ c t, l + 1 + 1 = c * 24 + t ∧ (t = 1 ∨ t = 5) := by
  have hPr : Pr (l + 1 + 1) := ⟨⟨l, rfl⟩, hq⟩
  have quot : ∀ (y t : Zp (l + 1)) (c d : Zp (l + 1)), (ring (l + 1)).mul y y = c → (ring (l + 1)).mul t t = d → (ring (l + 1)).mul (Frobenius.pw (ring (l + 1)) t l) t = (ring (l + 1)).one →
      (ring (l + 1)).mul ((ring (l + 1)).mul y (Frobenius.pw (ring (l + 1)) t l)) ((ring (l + 1)).mul y (Frobenius.pw (ring (l + 1)) t l)) = (ring (l + 1)).mul c ((ring (l + 1)).mul (Frobenius.pw (ring (l + 1)) t l) (Frobenius.pw (ring (l + 1)) t l)) :=
    fun y t c _ hy _ _ => quot5 l y t c hy
  match Inf7mod24.mod3 b with
  | ⟨e3, r3, hr3, hr3l⟩ =>
    match r3, hr3l with
    | 0, _ => exact ⟨e3, 5, by rw [hb, hr3]; lib_nat, Or.inr rfl⟩
    | 2, _ =>
      exfalso
      have hq3 : Dv 3 (l + 1 + 1) := ⟨e3 * 8 + 7, by rw [hb, hr3]; lib_nat⟩
      cases hq 3 hq3 with
      | inl e => exact Nat.noConfusion (Nat.succ.inj e)
      | inr e => exact h3 e.symm
    | 1, _ =>
      exfalso
      match Eisen.split_neg3 l hq ⟨e3 * 8 + 4, by rw [Nat.succ.inj hb, hr3]; lib_nat⟩ with
      | ⟨z, hz⟩ =>
        have hz0 : z ≠ (ring (l + 1)).zero := fun e0 => by
          rw [e0, DetChain.mul_zero] at hz
          apply Eisen.three_ne_zero l hl2
          calc Eisen.three l = (ring (l + 1)).neg ((ring (l + 1)).neg (Eisen.three l)) := (DetChain.neg_neg' (ring (l + 1)) _).symm
            _ = (ring (l + 1)).neg (ring (l + 1)).zero := by rw [← hz]
            _ = (ring (l + 1)).zero := by lib_ring
        have hinv := ZMod.inv_exists l hq _ hz0
        have h2 : (ring (l + 1)).mul ((ring (l + 1)).mul w (Frobenius.pw (ring (l + 1)) z l)) ((ring (l + 1)).mul w (Frobenius.pw (ring (l + 1)) z l)) = mk (l + 1) 2 := by
          rw [quot w z _ _ hw hz hinv]
          rw [show (ring (l + 1)).mul ((ring (l + 1)).neg ((ring (l + 1)).mul (mk (l + 1) 2) (Eisen.three l))) ((ring (l + 1)).mul (Frobenius.pw (ring (l + 1)) z l) (Frobenius.pw (ring (l + 1)) z l))
              = (ring (l + 1)).mul (mk (l + 1) 2) ((ring (l + 1)).mul ((ring (l + 1)).mul (Frobenius.pw (ring (l + 1)) z l) (Frobenius.pw (ring (l + 1)) z l)) ((ring (l + 1)).neg (Eisen.three l))) by lib_ring,
            ← hz, show (ring (l + 1)).mul ((ring (l + 1)).mul (Frobenius.pw (ring (l + 1)) z l) (Frobenius.pw (ring (l + 1)) z l)) ((ring (l + 1)).mul z z)
              = (ring (l + 1)).mul ((ring (l + 1)).mul (Frobenius.pw (ring (l + 1)) z l) z) ((ring (l + 1)).mul (Frobenius.pw (ring (l + 1)) z l) z) by lib_ring, hinv]
          lib_ring
        have hsq := sq_rem l _ (show (ring (l + 1)).mul (mk (l + 1) ((ring (l + 1)).mul w (Frobenius.pw (ring (l + 1)) z l)).val) (mk (l + 1) ((ring (l + 1)).mul w (Frobenius.pw (ring (l + 1)) z l)).val) = mk (l + 1) 2 by rw [mk_val]; exact h2)
        exact two_8m5 (e3 * 3 + 1) (by rw [show (e3 * 3 + 1 + (e3 * 3 + 1) + 1) + ((e3 * 3 + 1 + (e3 * 3 + 1) + 1) + 0) + ((e3 * 3 + 1 + (e3 * 3 + 1) + 1) + ((e3 * 3 + 1 + (e3 * 3 + 1) + 1) + 0)) + 1 = l + 1 + 1 by rw [hb, hr3]; lib_nat]; exact hPr)
          ⟨_, by rw [show (e3 * 3 + 1 + (e3 * 3 + 1) + 1) + ((e3 * 3 + 1 + (e3 * 3 + 1) + 1) + 0) + ((e3 * 3 + 1 + (e3 * 3 + 1) + 1) + ((e3 * 3 + 1 + (e3 * 3 + 1) + 1) + 0)) + 1 = l + 1 + 1 by rw [hb, hr3]; lib_nat]; exact hsq⟩
    | r3 + 3, hr3l => exact absurd hr3l (fun h => Nat.lt_irrefl 3 (Nat.lt_of_le_of_lt (Nat.le_add_left 3 r3) h))

/-- −1 = i0² and −6 = w² in 𝔽_q (q odd, q ≠ 3) ⟹ q ≡ 1 or 5 (mod 24). -/
theorem classify5 (l : Nat) (hq : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1) (hodd : ∀ c, l + 1 + 1 = 2 * c → False)
    (h3 : l + 1 + 1 ≠ 3) (hl2 : 2 ≤ l) (i0 w : Zp (l + 1)) (hi : (ring (l + 1)).mul i0 i0 = (ring (l + 1)).neg (ring (l + 1)).one)
    (hw : (ring (l + 1)).mul w w = (ring (l + 1)).neg ((ring (l + 1)).mul (mk (l + 1) 2) (Eisen.three l))) :
    ∃ c t, l + 1 + 1 = c * 24 + t ∧ (t = 1 ∨ t = 5) := by
  have hPr : Pr (l + 1 + 1) := ⟨⟨l, rfl⟩, hq⟩
  -- a square root y of c in 𝔽_q and a unit t with t² = d give (y/t)² = c/d
  have quot : ∀ (y t : Zp (l + 1)) (c d : Zp (l + 1)), (ring (l + 1)).mul y y = c → (ring (l + 1)).mul t t = d → (ring (l + 1)).mul (Frobenius.pw (ring (l + 1)) t l) t = (ring (l + 1)).one →
      (ring (l + 1)).mul ((ring (l + 1)).mul y (Frobenius.pw (ring (l + 1)) t l)) ((ring (l + 1)).mul y (Frobenius.pw (ring (l + 1)) t l)) = (ring (l + 1)).mul c ((ring (l + 1)).mul (Frobenius.pw (ring (l + 1)) t l) (Frobenius.pw (ring (l + 1)) t l)) :=
    fun y t c d hy _ _ => by rw [← hy]; lib_ring
  match Inf7mod8.mod8 (l + 1 + 1) with
  | ⟨b, r, hb, hr⟩ =>
    match r, hr with
    | 0, _ => exact absurd hb (fun e => hodd (4 * b) (by rw [e]; lib_nat))
    | 2, _ => exact absurd hb (fun e => hodd (4 * b + 1) (by rw [e]; lib_nat))
    | 4, _ => exact absurd hb (fun e => hodd (4 * b + 2) (by rw [e]; lib_nat))
    | 6, _ => exact absurd hb (fun e => hodd (4 * b + 3) (by rw [e]; lib_nat))
    | 3, _ =>
      exfalso
      have hl : l = 2 * (2 * (2 * b)) + 1 := Nat.succ.inj (Nat.succ.inj (hb.trans (by lib_nat)))
      subst hl
      exact GaussSplit.inert_three_mod_four (2 * b) hq i0 hi
    | 7, _ =>
      exfalso
      have hl : l = 2 * (2 * (2 * b + 1)) + 1 := Nat.succ.inj (Nat.succ.inj (hb.trans (by lib_nat)))
      subst hl
      exact GaussSplit.inert_three_mod_four (2 * b + 1) hq i0 hi
    | 1, _ =>
      exact cl_r1 l hq hodd hl2 w hw b hb
    | 5, _ =>
      exact cl_r5 l hq h3 hl2 w hw b hb
    | r + 8, hr => exact absurd hr (fun h => Nat.lt_irrefl 8 (Nat.lt_of_le_of_lt (Nat.le_add_left 8 r) h))

/-- prime factors of M = 2C² + 3 = A² + B² (M ≡ 5 mod 24, ≡ 5 mod 7) are ≡ 1 or 5 (mod 24). -/
theorem allowed5 (i a c7 l : Nat)
    (h24 : 2 * (i * i + 5 * i + 5) * (i * i + 5 * i + 5) + 3 = a * 24 + 5)
    (h7 : 2 * (i * i + 5 * i + 5) * (i * i + 5 * i + 5) + 3 = c7 * 7 + 5)
    (hq : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1)
    (hdv : Dv (l + 1 + 1) (2 * (i * i + 5 * i + 5) * (i * i + 5 * i + 5) + 3)) :
    ∃ c t, l + 1 + 1 = c * 24 + t ∧ (t = 1 ∨ t = 5) := by
  have hPr : Pr (l + 1 + 1) := ⟨⟨l, rfl⟩, hq⟩
  match Inf19mod24.odd_N l _ (a * 12 + 2) hq hdv (by rw [h24]; lib_nat) with
  | ⟨s, hs⟩ =>
  have hodd : ∀ c, l + 1 + 1 = 2 * c → False := fun c hc =>
    odd_not_dv2 (s + 1) c (by rw [show 2 * (s + 1) + 1 = l + 1 + 1 by rw [hs]; lib_nat, hc]; lib_nat)
  have h3 : l + 1 + 1 ≠ 3 := fun e => by
    rw [e] at hdv
    match hdv with
    | ⟨w, hw⟩ =>
      have := Inf7mod24.uniq_rem3 (a * 8 + 1) w 2 0 (Nat.lt_succ_self 2) (Nat.zero_lt_succ 2)
        (by rw [show (a * 8 + 1) * 3 + 2 = a * 24 + 5 by lib_nat, ← h24, hw]; lib_nat)
      exact Nat.noConfusion this
  have hl2 : 2 ≤ l := by
    match l, hs with
    | 0, hs0 => exact absurd hs0 (fun e => Nat.noConfusion e)
    | 1, _ => exact absurd rfl h3
    | l + 2, _ => exact Nat.le_add_left 2 l
  -- the two representations in 𝔽_q (M kept atomic)
  generalize hMd : 2 * (i * i + 5 * i + 5) * (i * i + 5 * i + 5) + 3 = M at hdv
  have hM0 : mk (l + 1) M = (ring (l + 1)).zero := Subtype.ext (dv_md_zero (l + 1) M hdv)
  have hM2 : mk (l + 1) (2 * M) = (ring (l + 1)).zero := Subtype.ext (dv_md_zero (l + 1) (2 * M) (dv_mul_left 2 _ _ hdv))
  have hAB : (ring (l + 1)).add ((ring (l + 1)).mul (mk (l + 1) (i * i + 6 * i + 7)) (mk (l + 1) (i * i + 6 * i + 7)))
      ((ring (l + 1)).mul (mk (l + 1) (i * i + 4 * i + 2)) (mk (l + 1) (i * i + 4 * i + 2))) = (ring (l + 1)).zero :=
    (show (ring (l + 1)).add ((ring (l + 1)).mul (mk (l + 1) (i * i + 6 * i + 7)) (mk (l + 1) (i * i + 6 * i + 7)))
        ((ring (l + 1)).mul (mk (l + 1) (i * i + 4 * i + 2)) (mk (l + 1) (i * i + 4 * i + 2)))
        = mk (l + 1) ((i * i + 6 * i + 7) * (i * i + 6 * i + 7) + (i * i + 4 * i + 2) * (i * i + 4 * i + 2)) by
      rw [TwoPower.mk_mul, TwoPower.mk_mul, PrimeOneModQ.mk_add]).trans ((congrArg (mk (l + 1)) ((ab_id i).trans hMd)).trans hM0)
  have hW : (ring (l + 1)).add ((ring (l + 1)).mul (mk (l + 1) (2 * (i * i + 5 * i + 5))) (mk (l + 1) (2 * (i * i + 5 * i + 5)))) (mk (l + 1) 6) = (ring (l + 1)).zero :=
    (show (ring (l + 1)).add ((ring (l + 1)).mul (mk (l + 1) (2 * (i * i + 5 * i + 5))) (mk (l + 1) (2 * (i * i + 5 * i + 5)))) (mk (l + 1) 6)
        = mk (l + 1) (2 * (i * i + 5 * i + 5) * (2 * (i * i + 5 * i + 5)) + 6) by rw [TwoPower.mk_mul, PrimeOneModQ.mk_add]).trans
      ((congrArg (mk (l + 1)) (show 2 * (i * i + 5 * i + 5) * (2 * (i * i + 5 * i + 5)) + 6 = 2 * M by rw [← hMd]; generalize i * i + 5 * i + 5 = C; lib_nat)).trans hM2)
  have hw := DetChain.neg_unique (ring (l + 1)) _ _ hW
  generalize mk (l + 1) (2 * (i * i + 5 * i + 5)) = w at hw
  have h6 : mk (l + 1) 6 = (ring (l + 1)).mul (mk (l + 1) 2) (Eisen.three l) := by
    rw [← mk3, TwoPower.mk_mul]
  -- B is a unit
  have hB0 : mk (l + 1) (i * i + 4 * i + 2) ≠ (ring (l + 1)).zero := fun e => by
    rw [e, DetChain.mul_zero, DetChain.add_zero'] at hAB
    have hA0 := sq_zero l hq _ hAB
    have hqA : Dv (l + 1 + 1) (i * i + 6 * i + 7) := md_zero_dv (l + 1) _ (congrArg Subtype.val hA0)
    have hqB : Dv (l + 1 + 1) (i * i + 4 * i + 2) := md_zero_dv (l + 1) _ (congrArg Subtype.val e)
    have h25 : Dv (l + 1 + 1) (2 * i + 5) := dv_sub (l + 1) _ _ hqB (by rw [show i * i + 4 * i + 2 + (2 * i + 5) = i * i + 6 * i + 7 by lib_nat]; exact hqA)
    have h7' : Dv (l + 1 + 1) 7 := dv_sub (l + 1) _ 7 (dv_mul_left 4 _ _ hqB)
      (by rw [show 4 * (i * i + 4 * i + 2) + 7 = (2 * i + 3) * (2 * i + 5) by lib_nat]; exact dv_mul_left _ _ _ h25)
    have h5 : Dv (l + 1 + 1) 5 := dv_sub (l + 1) _ 5 (dv_mul_left c7 _ _ h7') (by rw [← h7, hMd]; exact hdv)
    have e5 := Inf11mod24.dv5 l h5
    rw [e5] at h7'
    match h7' with
    | ⟨w, hw⟩ =>
      exact Nat.noConfusion (Inf3mod8.uniq_rem 4 1 w 2 0 (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl) (by show 1 * 5 + 2 = w * 5 + 0; rw [show 1 * 5 + 2 = 7 from rfl, hw]; lib_nat))
  have hv := ZMod.inv_exists l hq _ hB0
  generalize Frobenius.pw (ring (l + 1)) (mk (l + 1) (i * i + 4 * i + 2)) l = v at hv
  have hAA := DetChain.neg_unique (ring (l + 1)) _ _ hAB
  have hi : (ring (l + 1)).mul ((ring (l + 1)).mul (mk (l + 1) (i * i + 6 * i + 7)) v) ((ring (l + 1)).mul (mk (l + 1) (i * i + 6 * i + 7)) v) = (ring (l + 1)).neg (ring (l + 1)).one := by
    rw [show (ring (l + 1)).mul ((ring (l + 1)).mul (mk (l + 1) (i * i + 6 * i + 7)) v) ((ring (l + 1)).mul (mk (l + 1) (i * i + 6 * i + 7)) v)
        = (ring (l + 1)).mul ((ring (l + 1)).mul (mk (l + 1) (i * i + 6 * i + 7)) (mk (l + 1) (i * i + 6 * i + 7))) ((ring (l + 1)).mul v v) by lib_ring, hAA,
      show (ring (l + 1)).mul ((ring (l + 1)).neg ((ring (l + 1)).mul (mk (l + 1) (i * i + 4 * i + 2)) (mk (l + 1) (i * i + 4 * i + 2)))) ((ring (l + 1)).mul v v)
        = (ring (l + 1)).neg ((ring (l + 1)).mul ((ring (l + 1)).mul v (mk (l + 1) (i * i + 4 * i + 2))) ((ring (l + 1)).mul v (mk (l + 1) (i * i + 4 * i + 2)))) by lib_ring, hv]
    lib_ring
  generalize (ring (l + 1)).mul (mk (l + 1) (i * i + 6 * i + 7)) v = i0 at hi
  rw [h6] at hw
  exact classify5 l hq hodd h3 hl2 i0 w hi hw

/-- (Ku + 9)² + 5(Ku + 9) + 5 = K(Ku² + 23u) + 131 (K an atom). -/
theorem c12_gen (K u : Nat) : (K * u + 9) * (K * u + 9) + 5 * (K * u + 9) + 5 = K * (K * u * u + 23 * u) + 131 := by lib_nat

/-- 2(Kt + 11)² + 3 = 2K(Kt² + 22t) + 245. -/
theorem m24_gen (K t : Nat) : 2 * (K * t + 11) * (K * t + 11) + 3 = (2 * K) * (K * t * t + 22 * t) + 245 := by lib_nat

/-- (Kv + 4)² + 5(Kv + 4) + 5 = K(Kv² + 13v) + 41. -/
theorem c7_gen (K v : Nat) : (K * v + 4) * (K * v + 4) + 5 * (K * v + 4) + 5 = K * (K * v * v + 13 * v) + 41 := by lib_nat

/-- 2(Kw + 6)² + 3 = K(2Kw² + 24w) + 75. -/
theorem m7_gen (K w : Nat) : 2 * (K * w + 6) * (K * w + 6) + 3 = K * (2 * K * w * w + 24 * w) + 75 := by lib_nat

theorem m5_gen (z : Nat) : 2 * ((5 * z) * (5 * z) + 5 * (5 * z) + 5) * ((5 * z) * (5 * z) + 5 * (5 * z) + 5) + 3
    = (10 * ((5 * z * z + 5 * z + 1) * (5 * z * z + 5 * z + 1))) * 5 + 3 := by
  rw [show (5 * z) * (5 * z) + 5 * (5 * z) + 5 = 5 * (5 * z * z + 5 * z + 1) by lib_nat]
  generalize 5 * z * z + 5 * z + 1 = W
  lib_nat

theorem cq_gen (i M5 r0 Q : Nat) (hk : i + 3 = 168 * (M5 * M5)) (hr : M5 = r0 * Q) :
    i * i + 5 * i + 5 + 1 = ((i + 2) * 168 * M5 * r0) * Q := by
  rw [show i * i + 5 * i + 5 + 1 = (i + 2) * (i + 3) by lib_nat, hk]
  have e : (i + 2) * (168 * (M5 * M5)) = (i + 2) * (168 * (r0 * Q * M5)) := congrArg (fun z => (i + 2) * (168 * (z * M5))) hr
  rw [e]; lib_nat

/-- q | C + 1, q | 2C² + 3 ⟹ q | 5. -/
theorem bound5 (C r w Q : Nat) (hC : C + 1 = r * (Q + 1 + 1)) (hM : 2 * C * C + 3 = w * (Q + 1 + 1)) : Dv (Q + 1 + 1) 5 := by
  have h1 : Dv (Q + 1 + 1) (2 * (C + 1) * (C + 1)) := ⟨2 * r * (C + 1), by
    have e : 2 * (C + 1) * (C + 1) = 2 * (r * (Q + 1 + 1)) * (C + 1) := congrArg (fun z => 2 * z * (C + 1)) hC
    rw [e]; lib_nat⟩
  have h2 : Dv (Q + 1 + 1) (2 * (C + 1) * (C + 1) + 5) := ⟨w + 4 * r, by
    rw [show 2 * (C + 1) * (C + 1) + 5 = (2 * C * C + 3) + 4 * (C + 1) by lib_nat, hM, hC]; lib_nat⟩
  exact dv_sub (Q + 1) _ 5 h1 h2

/-- the descent and the bound, with i, u, v, z, m abstract. -/
theorem main5 (B i u v z M5 : Nat) (hk : i + 3 = 168 * (M5 * M5))
    (hM5 : ∀ j r, j < B → 1 ≤ r → r ≤ 4 → Dv (5 * j + r) M5)
    (h24 : 2 * (i * i + 5 * i + 5) * (i * i + 5 * i + 5) + 3 = ((12 * (12 * u * u + 23 * u + 10)) * (12 * u * u + 23 * u + 10) + 22 * (12 * u * u + 23 * u + 10) + 10) * 24 + 5)
    (h7 : 2 * (i * i + 5 * i + 5) * (i * i + 5 * i + 5) + 3 = (2 * 7 * (7 * v * v + 13 * v + 5) * (7 * v * v + 13 * v + 5) + 24 * (7 * v * v + 13 * v + 5) + 10) * 7 + 5)
    (h5 : 2 * (i * i + 5 * i + 5) * (i * i + 5 * i + 5) + 3 = (10 * ((5 * z * z + 5 * z + 1) * (5 * z * z + 5 * z + 1))) * 5 + 3) :
    ∃ c, (∀ d, Dv d (c * 24 + 5) → d = 1 ∨ d = c * 24 + 5) ∧ B < c * 24 + 5 := by
  match Inf17mod24.has_gen 3 (2 * (i * i + 5 * i + 5) * (i * i + 5 * i + 5) + 3) (Nat.le_of_ble_eq_true rfl)
      (fun l hq hdv => allowed5 i _ _ l h24 h7 hq hdv)
      _ _ (Nat.le_refl _) ⟨1, (Nat.one_mul _).symm⟩ ⟨_, h24⟩ with
  | ⟨q, hq, ⟨c, hc⟩, hqN⟩ =>
    have hq' : q = c * 24 + 5 := by rw [hc]
    refine ⟨c, by rw [← hq']; exact hq, ?_⟩
    rw [hq'] at hqN hq
    match c with
    | 0 =>
      exfalso
      match hqN with
      | ⟨w, hw⟩ =>
        exact Nat.noConfusion (Inf3mod8.uniq_rem 4 _ w 3 0 (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)
          (by show _ * 5 + 3 = w * 5 + 0; rw [← h5, hw]; lib_nat))
    | c + 1 =>
    cases Nat.lt_or_ge B ((c + 1) * 24 + 5) with
    | inl h => exact h
    | inr h =>
      exfalso
      match div_exists 4 ((c + 1) * 24 + 5) with
      | ⟨j, r, k, hj, hrk⟩ =>
        match r, hrk with
        | 0, _ =>
          cases hq 5 ⟨j, by rw [hj]; lib_nat⟩ with
          | inl e => exact Nat.noConfusion (Nat.succ.inj e)
          | inr e =>
            exact Nat.noConfusion (LibInt.nadd_right_cancel 5 ((show c * 24 + 23 + 1 + 5 = (c + 1) * 24 + 5 by lib_nat).trans e.symm |>.trans (show 5 = 0 + 5 from rfl)))
        | r + 1, hrk =>
          have hr4 : r + 1 ≤ 4 := Nat.le_of_succ_le_succ (show r + 1 + 1 ≤ 4 + 1 by rw [← hrk]; exact Nat.add_le_add_left (Nat.succ_le_succ (Nat.zero_le _)) (r + 1))
          have hjB : j < B := Nat.lt_of_lt_of_le
            (Nat.lt_of_lt_of_le (Nat.lt_succ_self j) (by rw [hj, show j * 5 + (r + 1) = (j + 1) + (4 * j + r) by lib_nat]; exact Nat.le_add_right _ _)) h
          match hM5 j (r + 1) hjB (Nat.succ_le_succ (Nat.zero_le r)) hr4 with
          | ⟨r0, hr0⟩ =>
            have hr0' : M5 = r0 * ((c + 1) * 24 + 3 + 1 + 1) := by
              rw [hr0, show 5 * j + (r + 1) = (c + 1) * 24 + 3 + 1 + 1 by rw [show (c + 1) * 24 + 3 + 1 + 1 = (c + 1) * 24 + 5 from rfl, hj]; lib_nat]
            match hqN with
            | ⟨w, hw⟩ =>
              have h5' := bound5 _ _ w ((c + 1) * 24 + 3) (cq_gen i M5 r0 _ hk hr0') (hw.trans (by lib_nat))
              match dv_le _ 4 h5' with
              | ⟨k5, hk5⟩ =>
                exact Nat.noConfusion (LibInt.nadd_right_cancel 5
                  ((show c * 24 + k5 + 23 + 1 + 5 = (c + 1) * 24 + 3 + 1 + 1 + k5 by lib_nat).trans hk5))


/-- **Infinitely many primes p ≡ 5 (mod 24).** -/
theorem k_gen (K E : Nat) : 5 * (K * E) + K = K * (5 * E + 1) := by lib_nat

theorem u_gen (H : Nat) : 5 * (12 * H) + 165 = 12 * (5 * H + 13) + 9 := by lib_nat

theorem v_gen (J : Nat) : 5 * (7 * J) + 165 = 7 * (5 * J + 23) + 4 := by lib_nat

theorem inf_5mod24 (B : Nat) : ∃ c, (∀ d, Dv d (c * 24 + 5) → d = 1 ∨ d = c * 24 + 5) ∧ B < c * 24 + 5 := by
  match m5_sq B with
  | ⟨E, hE⟩ =>
  have hk : 5 * (168 * E) + 165 + 3 = 168 * (m5 B * m5 B) := by rw [hE]; exact k_gen 168 E
  have hH : 168 * E = 12 * (14 * E) := by lib_nat
  have hJ : 168 * E = 7 * (24 * E) := by lib_nat
  generalize 168 * E = G at hk hH hJ
  have hu : 5 * G + 165 = 12 * (5 * (14 * E) + 13) + 9 := by rw [hH]; exact u_gen _
  have hv7 : 5 * G + 165 = 7 * (5 * (24 * E) + 23) + 4 := by rw [hJ]; exact v_gen _
  have hz5 : 5 * G + 165 = 5 * (G + 33) := by lib_nat
  generalize 5 * G + 165 = i at hu hv7 hz5 hk
  generalize 5 * (14 * E) + 13 = u at hu
  generalize 5 * (24 * E) + 23 = v at hv7
  generalize G + 33 = z at hz5
  have hC12 : i * i + 5 * i + 5 = 12 * (12 * u * u + 23 * u + 10) + 11 := by
    rw [hu, c12_gen]; generalize 12 * u * u + 23 * u = A; lib_nat
  have h24 : 2 * (i * i + 5 * i + 5) * (i * i + 5 * i + 5) + 3 = ((12 * (12 * u * u + 23 * u + 10)) * (12 * u * u + 23 * u + 10) + 22 * (12 * u * u + 23 * u + 10) + 10) * 24 + 5 := by
    rw [hC12, m24_gen]; generalize 12 * u * u + 23 * u + 10 = t; generalize 12 * t * t + 22 * t = A
    show 2 * 12 * A + 245 = (A + 10) * 24 + 5
    lib_nat
  have hC7 : i * i + 5 * i + 5 = 7 * (7 * v * v + 13 * v + 5) + 6 := by
    rw [hv7, c7_gen]; generalize 7 * v * v + 13 * v = A; lib_nat
  have h7 : 2 * (i * i + 5 * i + 5) * (i * i + 5 * i + 5) + 3 = (2 * 7 * (7 * v * v + 13 * v + 5) * (7 * v * v + 13 * v + 5) + 24 * (7 * v * v + 13 * v + 5) + 10) * 7 + 5 := by
    rw [hC7, m7_gen]; generalize 7 * v * v + 13 * v + 5 = w; generalize 2 * 7 * w * w + 24 * w = A
    lib_nat
  have h5 : 2 * (i * i + 5 * i + 5) * (i * i + 5 * i + 5) + 3 = (10 * ((5 * z * z + 5 * z + 1) * (5 * z * z + 5 * z + 1))) * 5 + 3 := by
    rw [hz5]; exact m5_gen z
  exact main5 B i u v z (m5 B) hk (fun j r h1 h2 h3 => m5_dv B j r h1 h2 h3) h24 h7 h5

end Inf5mod24

#print axioms Inf5mod24.sq_zero
#print axioms Inf5mod24.mul5
#print axioms Inf5mod24.m5
#print axioms Inf5mod24.blk5
#print axioms Inf5mod24.m5_sq
#print axioms Inf5mod24.m5_dv
#print axioms Inf5mod24.mk3
#print axioms Inf5mod24.ab_id
#print axioms Inf5mod24.quot5
#print axioms Inf5mod24.cl_r1
#print axioms Inf5mod24.cl_r5
#print axioms Inf5mod24.classify5
#print axioms Inf5mod24.allowed5
#print axioms Inf5mod24.c12_gen
#print axioms Inf5mod24.m24_gen
#print axioms Inf5mod24.c7_gen
#print axioms Inf5mod24.m7_gen
#print axioms Inf5mod24.m5_gen
#print axioms Inf5mod24.cq_gen
#print axioms Inf5mod24.bound5
#print axioms Inf5mod24.main5
#print axioms Inf5mod24.k_gen
#print axioms Inf5mod24.u_gen
#print axioms Inf5mod24.v_gen
#print axioms Inf5mod24.inf_5mod24

lang/Inf5mod8.lean

lean_all/lang/Inf5mod8.lean

/-
  lang/Inf5mod8 — T43, cycle 129: infinitely many primes p ≡ 5 (mod 8).

  x = product of the odd numbers ≤ B (odd, divisible by every odd d ≤ B), N = 4x² + 1 ≡ 5 (mod 8). Every prime factor q of
  N has −1 ≡ (2x)² a square, so q ≡ 1 (mod 4) (GaussSplit), i.e. q ≡ 1 or 5 (mod 8); the product is ≡ 5, so some factor is
  ≡ 5; odd q ≤ B divides x and cannot divide N.
    ofac, ofac_odd, dv_ofac     the odd factorial
    allowed5, has5, inf_5mod8
  The residue 5 mod 8 is the class of the inert primes of ℚ(√2) that split in ℚ(i).

  Lean 4 core only, proofs by hand (lib_nat, lib_int), no axioms.
-/
import lang.Inf7mod8

namespace Inf5mod8

open FTAChain ZMod LibInt InfSplit Inf7mod8 QRChain

def ofac : Nat → Nat
  | 0 => 1
  | n + 1 => if par (n + 1) = true then (n + 1) * ofac n else ofac n

theorem par_odd (c : Nat) : par (2 * c + 1) = true := by
  show (!par (2 * c)) = true
  rw [show 2 * c = c + c by lib_nat, par_double]; rfl

theorem par_even (c : Nat) : par (2 * c) = false := by rw [show 2 * c = c + c by lib_nat, par_double]

theorem ofac_odd : ∀ n, ∃ y, ofac n = 2 * y + 1
  | 0 => ⟨0, rfl⟩
  | n + 1 => by
    match ofac_odd n with
    | ⟨y, hy⟩ =>
    cases EveryPrime.nat_even_odd (n + 1) with
    | inl h =>
      match h with
      | ⟨c, hc⟩ =>
        exact ⟨y, by show (if par (n + 1) = true then (n + 1) * ofac n else ofac n) = _; rw [hc, par_even]; exact hy⟩
    | inr h =>
      match h with
      | ⟨c, hc⟩ =>
        refine ⟨2 * c * y + c + y, ?_⟩
        show (if par (n + 1) = true then (n + 1) * ofac n else ofac n) = _
        rw [hc, par_odd, FiniteRecChain.ite_pos' _ _ rfl, hy]; lib_nat

theorem dv_ofac : ∀ n c, 2 * c + 1 ≤ n → Dv (2 * c + 1) (ofac n)
  | 0, _, h => absurd h (Nat.not_succ_le_zero _)
  | n + 1, c, h => by
    show Dv (2 * c + 1) (if par (n + 1) = true then (n + 1) * ofac n else ofac n)
    cases Nat.lt_or_ge (2 * c + 1) (n + 1) with
    | inl hlt =>
      have ih := dv_ofac n c (Nat.le_of_lt_succ hlt)
      cases EveryPrime.nat_even_odd (n + 1) with
      | inl he => match he with
        | ⟨e, he⟩ => rw [he, par_even, FiniteRecChain.ite_neg' _ _ (fun h => Bool.noConfusion h)]; exact ih
      | inr ho => match ho with
        | ⟨e, he⟩ =>
          rw [he, par_odd, FiniteRecChain.ite_pos' _ _ rfl]
          match ih with
          | ⟨q, hq⟩ => exact ⟨(2 * e + 1) * q, by rw [hq]; lib_nat⟩
    | inr hge =>
      have e : 2 * c + 1 = n + 1 := Nat.le_antisymm h hge
      rw [← e, par_odd, FiniteRecChain.ite_pos' _ _ rfl]
      exact ⟨ofac n, by lib_nat⟩

theorem cofactor5 (b N' a : Nat) (h : (b * 8 + 1) * N' = a * 8 + 5) : ∃ c, N' = c * 8 + 5 :=
  match mod8 N' with
  | ⟨c, r, hc, hr⟩ => by
    have e : (b * 8 + 1) * N' = (b * 8 * c + b * r + c) * 8 + r := by rw [hc]; lib_nat
    rw [e] at h
    exact ⟨c, by rw [hc, uniq_rem8 _ _ r 5 hr (by exact Nat.lt_of_lt_of_le (Nat.lt_succ_self 5) (Nat.le_succ_of_le (Nat.le_succ 6))) h]⟩

/-- prime factors of 4x² + 1 are ≡ 1 or 5 (mod 8). -/
theorem allowed5 (x l : Nat) (hq : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1)
    (hdv : Dv (l + 1 + 1) (4 * x * x + 1)) : ∃ b r, l + 1 + 1 = b * 8 + r ∧ (r = 1 ∨ r = 5) := by
  have hy : (ring (l + 1)).mul (mk (l + 1) (2 * x)) (mk (l + 1) (2 * x)) = (ring (l + 1)).neg (ring (l + 1)).one := by
    apply DetChain.neg_unique
    rw [TwoPower.mk_mul, show (ring (l + 1)).one = mk (l + 1) 1 from rfl, PrimeOneModQ.mk_add,
      show 2 * x * (2 * x) + 1 = 4 * x * x + 1 by lib_nat]
    exact Subtype.ext (dv_md_zero (l + 1) _ hdv)
  have hodd : ∀ c, l + 1 + 1 = 2 * c → False := fun c hc => by
    have h2 : l + 1 + 1 = 2 := by
      cases hq 2 ⟨c, by rw [hc]; lib_nat⟩ with
      | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
      | inr e => exact e.symm
    rw [h2] at hdv
    match hdv with
    | ⟨r, hr⟩ => exact odd_not_dv2 (2 * x * x) r (by rw [← hr]; lib_nat)
  have h34 : ∀ k, l + 1 + 1 = 2 * (2 * k) + 1 + 1 + 1 → False := fun k hk => by
    have hl : l = 2 * (2 * k) + 1 := Nat.succ.inj (Nat.succ.inj (hk.trans (by lib_nat)))
    subst hl
    exact GaussSplit.inert_three_mod_four k hq _ hy
  match mod8 (l + 1 + 1) with
  | ⟨b, r, hb, hr⟩ =>
    match r, hr with
    | 0, _ => exact absurd hb (fun e => hodd (4 * b) (by rw [e]; lib_nat))
    | 1, _ => exact ⟨b, 1, hb, Or.inl rfl⟩
    | 2, _ => exact absurd hb (fun e => hodd (4 * b + 1) (by rw [e]; lib_nat))
    | 3, _ => exact absurd hb (fun e => h34 (2 * b) (by rw [e]; lib_nat))
    | 4, _ => exact absurd hb (fun e => hodd (4 * b + 2) (by rw [e]; lib_nat))
    | 5, _ => exact ⟨b, 5, hb, Or.inr rfl⟩
    | 6, _ => exact absurd hb (fun e => hodd (4 * b + 3) (by rw [e]; lib_nat))
    | 7, _ => exact absurd hb (fun e => h34 (2 * b + 1) (by rw [e]; lib_nat))
    | r + 8, hr => exact absurd hr (fun h => Nat.lt_irrefl 8 (Nat.lt_of_le_of_lt (Nat.le_add_left 8 r) h))

theorem has5 (x : Nat) : ∀ G N, N ≤ G → Dv N (4 * x * x + 1) → (∃ a, N = a * 8 + 5) →
    ∃ q, (∀ d, Dv d q → d = 1 ∨ d = q) ∧ (∃ b, q = b * 8 + 5) ∧ Dv q N
  | 0, N, hN, _, ⟨a, ha⟩ => absurd (ha ▸ hN) (fun h => Nat.not_succ_le_zero _ (Nat.le_trans (Nat.le_add_left _ _) h))
  | G + 1, N, hN, hN0, ⟨a, ha⟩ => by
    match lp_exists (a * 8 + 3) with
    | ⟨l, hl, hmin⟩ =>
    have hq := lp_prime _ l hl hmin
    have hlN : Dv (l + 1 + 1) N := by rw [ha]; exact hl
    match allowed5 x l hq (dv_trans _ _ _ hlN hN0) with
    | ⟨b, r, hb, hr⟩ =>
      cases hr with
      | inr h5 => exact ⟨l + 1 + 1, hq, ⟨b, by rw [hb, h5]⟩, hlN⟩
      | inl h1 =>
        match hlN with
        | ⟨N', hN'⟩ =>
        match cofactor5 b N' a (by rw [← h1, ← hb, ← ha, hN']; lib_nat) with
        | ⟨c, hc⟩ =>
        have hlt : N' ≤ G := by
          have e : N = N' + (N' * (l + 1)) := by rw [hN']; lib_nat
          have h1' : 1 ≤ N' * (l + 1) := by rw [hc]; exact Nat.mul_le_mul (Nat.le_add_left 1 _) (Nat.le_add_left 1 _)
          exact Nat.le_of_succ_le_succ (Nat.le_trans (Nat.add_le_add_left h1' N') (by rw [← e]; exact hN))
        have hN'0 : Dv N' (4 * x * x + 1) := dv_trans _ _ _ ⟨l + 1 + 1, by rw [hN']; lib_nat⟩ hN0
        match has5 x G N' hlt hN'0 ⟨c, hc⟩ with
        | ⟨q, hq', hq5, hqN⟩ => exact ⟨q, hq', hq5, dv_trans q N' N hqN ⟨l + 1 + 1, by rw [hN']; lib_nat⟩⟩

/-- **Infinitely many primes p ≡ 5 (mod 8).** -/
theorem inf_5mod8 (B : Nat) : ∃ b, (∀ d, Dv d (b * 8 + 5) → d = 1 ∨ d = b * 8 + 5) ∧ B < b * 8 + 5 := by
  match ofac_odd B with
  | ⟨y, hy⟩ =>
  have hN : 4 * ofac B * ofac B + 1 = (2 * y * y + 2 * y) * 8 + 5 := by rw [hy]; lib_nat
  match has5 (ofac B) _ _ (Nat.le_refl _) ⟨1, by lib_nat⟩ ⟨2 * y * y + 2 * y, hN⟩ with
  | ⟨q, hq, ⟨b, hb⟩, hqN⟩ =>
    refine ⟨b, by rw [← hb]; exact hq, ?_⟩
    rw [← hb]
    cases Nat.lt_or_ge B q with
    | inl h => exact h
    | inr h =>
      exfalso
      -- q is odd: q = 2(4b + 2) + 1
      match dv_ofac B (4 * b + 2) (by rw [show 2 * (4 * b + 2) + 1 = b * 8 + 5 by lib_nat, ← hb]; exact h) with
      | ⟨r, hr⟩ =>
        have hr' : ofac B = r * q := by rw [hr, hb]; lib_nat
        have h1 : Dv q (4 * ofac B * ofac B) := ⟨4 * r * ofac B, by rw [hr']; lib_nat⟩
        have hq2 : q = (b * 8 + 4) + 1 := hb
        rw [hq2] at hqN h1
        exact dv_one_absurd (b * 8 + 3) (dv_sub (b * 8 + 4) _ 1 h1 hqN)

end Inf5mod8

#print axioms Inf5mod8.par_odd
#print axioms Inf5mod8.par_even
#print axioms Inf5mod8.ofac_odd
#print axioms Inf5mod8.dv_ofac
#print axioms Inf5mod8.cofactor5
#print axioms Inf5mod8.allowed5
#print axioms Inf5mod8.has5
#print axioms Inf5mod8.inf_5mod8

lang/Inf7m8.lean

lean_all/lang/Inf7m8.lean

/-
  lang/Inf7m8 — T43, cycle 538: **p ≡ 7 (mod 8), p ≡ 1 (mod n) beyond every bound, for every odd squarefree n ≥ 3.**

  y = 2w², w = nO (odd), so y ≡ 2 (mod 4) and PhiSq4.sqfree4 gives F ≡ 3 (mod 4) with Inv(n, y, F). For a prime r | F, y has odd
  order n, so y = (y^((n+1)/2))² and 2 = y·(w⁻¹)² is a square mod r, hence r ≡ ±1 (mod 8) (TwoSupp); r ≡ 1 (mod n) by Fermat.
  ChebPowInf.hasG (modulus 4) gives a factor ≡ 3 (mod 4), which is then ≡ 7 (mod 8).
    two_pm, inf_7m8
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.InfSq3
import lang.Inf3mod8
import lang.HasseCM

namespace PhiSq

open FTAChain ZMod Cheb TwoPrime PhiInv

/-- 2 a square mod an odd prime r ⇒ r ≡ ±1 (mod 8). -/
theorem two_pm (l : Nat) (hq : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1) (hodd : ∀ c, l + 1 + 1 = 2 * c → False)
    (z : Zp (l + 1)) (hz : (ring (l + 1)).mul z z = (ring (l + 1)).add (ring (l + 1)).one (ring (l + 1)).one) :
    ∃ b, l + 1 + 1 = b * 8 + 1 ∨ l + 1 + 1 = b * 8 + 7 := by
  have hPr : Pr (l + 1 + 1) := ⟨⟨l, rfl⟩, hq⟩
  have h2 : (ring (l + 1)).mul (mk (l + 1) z.val) (mk (l + 1) z.val) = mk (l + 1) 2 := by
    rw [mk_val, Inf3mod8.two_eq]; exact hz
  have hsq := Inf3mod8.sq_rem l z.val h2
  obtain ⟨b, r, hb, hr⟩ := Inf7mod8.mod8 (l + 1 + 1)
  match r, hr with
  | 0, _ => exact absurd hb (fun e => hodd (4 * b) (by rw [e]; lib_nat))
  | 1, _ => exact ⟨b, Or.inl hb⟩
  | 2, _ => exact absurd hb (fun e => hodd (4 * b + 1) (by rw [e]; lib_nat))
  | 3, _ =>
    exfalso
    have e : (b + b) + ((b + b) + 1) + ((b + b) + ((b + b) + 1)) + 1 = l + 1 + 1 := by rw [hb]; lib_nat
    exact TwoSupp.two_8m3 b (by rw [e]; exact hPr) ⟨z.val, by rw [e]; exact hsq⟩
  | 4, _ => exact absurd hb (fun e => hodd (4 * b + 2) (by rw [e]; lib_nat))
  | 5, _ =>
    exfalso
    have e : (b + b + 1) + ((b + b + 1) + 0) + ((b + b + 1) + ((b + b + 1) + 0)) + 1 = l + 1 + 1 := by rw [hb]; lib_nat
    exact TwoSupp.two_8m5 b (by rw [e]; exact hPr) ⟨z.val, by rw [e]; exact hsq⟩
  | 6, _ => exact absurd hb (fun e => hodd (4 * b + 3) (by rw [e]; lib_nat))
  | 7, _ => exact ⟨b, Or.inr hb⟩
  | r + 8, hr => exact absurd hr (fun h => Nat.lt_irrefl 8 (Nat.lt_of_le_of_lt (Nat.le_add_left 8 r) h))

theorem inf_7m8 (j : Nat) (L : List Nat) (hG : Good (j :: L)) (h0 : ∃ h, pr (j :: L) = 2 * h + 1) (B : Nat) :
    ∃ p, (∀ k, Dv k p → k = 1 ∨ k = p) ∧ B < p ∧ (∃ k, p = 8 * k + 7) ∧ ∃ q, p = q * pr (j :: L) + 1 := by
  obtain ⟨h, hh⟩ := h0
  obtain ⟨a, ha⟩ := Two2.ofac_odd (B + cs L + 1)
  have hOge := ofac_ge (B + cs L)
  have hw : pr (j :: L) * Two2.ofac (B + cs L + 1) = 2 * (2 * h * a + h + a) + 1 := by rw [hh, ha]; lib_nat
  generalize hW : pr (j :: L) * Two2.ofac (B + cs L + 1) = w at hw
  generalize hv : 2 * h * a + h + a = v at hw
  generalize hy : 2 * (w * w) = y
  have hy4 : y = 4 * (2 * (v * v + v)) + 2 := by rw [← hy, hw]; lib_nat
  have hwA : AllDv (j :: L) w := by
    rw [← hW, Nat.mul_comm]; exact allDv_mul _ _ _ (allDv_pr (j :: L))
  have hA : AllDv (j :: L) y := by
    rw [← hy]; exact allDv_mul 2 _ _ (allDv_mul w _ _ hwA)
  have hw1 : 1 ≤ w := by rw [hw]; exact Nat.le_add_left 1 _
  have hcy : cs L + 2 ≤ y := by
    have h1 : 1 ≤ pr (j :: L) := pr_pos _
    have e1 : cs L + 2 ≤ B + cs L + 1 + 1 := Nat.le_trans (Nat.le_add_left (cs L + 2) B) (Nat.le_of_eq (by lib_nat))
    have e2 : B + cs L + 1 ≤ w := by
      rw [← hW]; exact Nat.le_trans hOge (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ h1))
    have e3 : w + 1 ≤ y := by
      rw [← hy]
      exact Nat.le_trans (Nat.add_le_add_left hw1 w) (Nat.le_trans (Nat.le_of_eq (by lib_nat))
        (Nat.mul_le_mul_left 2 (Nat.le_trans (Nat.le_of_eq (Nat.mul_one w).symm) (Nat.mul_le_mul_left w hw1))))
    exact Nat.le_trans e1 (Nat.le_trans (Nat.succ_le_succ e2) e3)
  obtain ⟨y', hy'⟩ := le_dest2 2 y (Nat.le_trans (Nat.le_add_left 2 (cs L)) hcy)
  rw [← hy'] at hA
  obtain ⟨F, I, l, _, hR⟩ := sqfree4 j L hG y' hA
  rw [show y' + 1 + 1 = y from hy'] at I l hR
  obtain ⟨f, hf⟩ := hR.1 _ hy4
  have hy1 : 1 ≤ y := Nat.le_trans (Nat.le_add_left 1 (cs L + 1)) hcy
  have hC : cs L < y ^ ph j L := Nat.le_trans (Nat.le_add_right (cs L + 1) 1) (Nat.le_trans hcy (pow_ge y hy1 _ (ph_pos j L)))
  have hF2 := PhiSize.two_le _ _ _ l hC
  have hFodd : ∀ r, F = r * 2 → False := fun r hr => InfSplit.odd_not_dv2 (2 * f + 1) r (by rw [← hr, hf]; lib_nat)
  have hall : ∀ l, (∀ e, Dv e (l + 1 + 1) → e = 1 ∨ e = l + 1 + 1) → Dv (l + 1 + 1) F →
      (∃ a, l + 1 + 1 = a * (1 + 3) + 1) ∨ (∃ a, l + 1 + 1 = a * (1 + 3) + (1 + 2)) := by
    intro l hq hdv
    rcases EveryPrime.nat_even_odd l with ⟨i, hi⟩ | ⟨m, hm⟩
    · exfalso
      have h2 : l + 1 + 1 = 2 := by
        cases hq 2 ⟨i + 1, by rw [hi]; lib_nat⟩ with
        | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
        | inr e => exact e.symm
      rw [h2] at hdv; obtain ⟨r, hr⟩ := hdv; exact hFodd r hr
    · rcases EveryPrime.nat_even_odd m with ⟨i, hi⟩ | ⟨i, hi⟩
      · exact Or.inr ⟨i, by rw [hm, hi]; lib_nat⟩
      · exact Or.inl ⟨i + 1, by rw [hm, hi]; lib_nat⟩
  obtain ⟨q, hq, ⟨b, hb⟩, hqF⟩ := hasG 1 F hall F F (Nat.le_refl _) (dv_refl _) ⟨f, by rw [hf]; lib_nat⟩
  have hq2 : 2 ≤ q := by rw [hb]; exact Nat.le_add_left _ _
  obtain ⟨q', hq'⟩ := le_dest2 2 q hq2
  rw [← hq'] at hq hqF hb
  have hq'' : ∀ e, Dv e (q' + 1 + 1) → e = 1 ∨ e = q' + 1 + 1 := hq
  obtain ⟨hO, hg⟩ := (I q' 0 hq'').1 (dv_pow1 _ _ hqF)
  have hn2 : 2 ≤ pr (j :: L) := Nat.le_trans (Nat.le_add_left 2 j) (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm)
    (Nat.mul_le_mul_right (j + 1 + 1) (pr_pos L)))
  obtain ⟨n', hn'⟩ := le_dest2 1 _ (Nat.le_trans (Nat.le_add_left 1 1) hn2)
  have hqy : Dv (q' + 1 + 1) y → False := fun hd => by
    have hg1 : Dv (q' + 1 + 1) (gN y (pr (j :: L))) := dv_base _ 0 _ hg
    rw [← hn', Nat.add_comm n' 1, gN_one] at hg1
    exact dv_one_absurd q' (dv_sub (q' + 1) (y * gN y n') 1 (dv_trans _ _ _ hd ⟨gN y n', Nat.mul_comm _ _⟩)
      (by rw [Nat.add_comm (y * gN y n') 1]; exact hg1))
  have hqw : Dv (q' + 1 + 1) w → False := fun hd => hqy (by rw [← hy]; exact dv_mul_left 2 _ _ (dv_mul_left w _ _ hd))
  have hodd : ∀ c, q' + 1 + 1 = 2 * c → False := fun c hc => by
    rcases hq'' 2 ⟨c, hc.trans (Nat.mul_comm 2 c)⟩ with e | e
    · exact Nat.noConfusion (Nat.succ.inj e)
    · exact hqy (by rw [← e, ← hy]; exact ⟨w * w, by lib_nat⟩)
  refine ⟨q' + 2, hq, ?_, ?_, ?_⟩
  · cases Nat.lt_or_ge B (q' + 2) with
    | inl hlt => exact hlt
    | inr hge =>
      exfalso
      rcases EveryPrime.nat_even_odd (q' + 2) with ⟨i, hi⟩ | ⟨i, hi⟩
      · exact hodd i hi
      · have hle : i ≤ 2 * i + 1 := Nat.le_succ_of_le (Nat.le_trans (Nat.le_add_left i i) (Nat.le_of_eq (by lib_nat)))
        have hB : 2 * i + 1 ≤ B + cs L := by rw [hi] at hge; exact Nat.le_trans hge (Nat.le_add_right B _)
        have hqO := Two2.dv_ofac (B + cs L + 1) i (Nat.succ_le_succ (Nat.le_trans hle hB))
        apply hqw
        show Dv (q' + 2) w
        rw [hi, ← hW]
        exact dv_trans _ _ _ hqO ⟨pr (j :: L), rfl⟩
  · -- q ≡ 7 (mod 8): q ≡ 3 (mod 4) and 2 is a square mod q
    rcases EveryPrime.nat_even_odd q' with ⟨i, hi⟩ | ⟨m, hm⟩
    · exact absurd (show q' + 1 + 1 = 2 * (i + 1) by rw [hi]; lib_nat) (hodd (i + 1))
    · subst hm
      have hW0 : emb (ring (2 * m + 1 + 1)) w ≠ (ring (2 * m + 1 + 1)).zero := fun e => hqw (dv_of_emb _ _ e)
      have hinv := ZMod.inv_exists (2 * m + 1) hq'' _ hW0
      generalize Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) w) (2 * m + 1) = iw at hinv
      have hYn := hO.1
      rw [hh] at hYn
      have hY : emb (ring (2 * m + 1 + 1)) y = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)
          ((ring (2 * m + 1 + 1)).mul (emb (ring (2 * m + 1 + 1)) w) (emb (ring (2 * m + 1 + 1)) w)) := by
        rw [← hy, emb_mul, emb_mul]; show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).one) (ring (2 * m + 1 + 1)).one) _ = _; lib_ring
      have hsq : (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) y) (h + 1))
          (Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) y) (h + 1)) = emb (ring (2 * m + 1 + 1)) y := by
        rw [← HasseCM.pw_add', show h + 1 + (h + 1) = (2 * h + 1) + 1 by lib_nat]
        show (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) _ (2 * h + 1)) _ = _
        rw [hYn]; lib_ring
      generalize Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) y) (h + 1) = t at hsq
      have hz : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t iw) ((ring (2 * m + 1 + 1)).mul t iw) =
          (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one := by
        calc _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t t) ((ring (2 * m + 1 + 1)).mul iw iw) := by lib_ring
          _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)
                ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul iw (emb (ring (2 * m + 1 + 1)) w)) ((ring (2 * m + 1 + 1)).mul iw (emb (ring (2 * m + 1 + 1)) w))) := by
              rw [hsq, hY]; lib_ring
          _ = _ := by rw [hinv]; lib_ring
      obtain ⟨b8, hb8⟩ := two_pm (2 * m + 1) hq'' hodd _ hz
      rcases hb8 with e | e
      · exfalso
        have := Inf3mod8.uniq_rem 3 (2 * b8) b 1 3 (Nat.lt_of_lt_of_le (Nat.lt_succ_self 1) (Nat.le_add_left 2 2)) (Nat.lt_succ_self 3)
          ((show (2 * b8) * (3 + 1) + 1 = b8 * 8 + 1 by lib_nat).trans (e.symm.trans (hb.trans (by lib_nat))))
        exact Nat.noConfusion (Nat.succ.inj this)
      · exact ⟨b8, by show 2 * m + 1 + 1 + 1 = _; rw [e]; lib_nat⟩
  · rcases EveryPrime.nat_even_odd q' with ⟨i, hi⟩ | ⟨m, hm⟩
    · exact absurd (show q' + 1 + 1 = 2 * (i + 1) by rw [hi]; lib_nat) (hodd (i + 1))
    · subst hm
      have hX0 : emb (ring (2 * m + 1 + 1)) y ≠ (ring (2 * m + 1 + 1)).zero := fun e => hqy (dv_of_emb _ _ e)
      obtain ⟨r, hr⟩ := hO.2 _ (Cyclo2l.ferm1 m hq'' _ hX0)
      exact ⟨r, by show 2 * m + 1 + 1 + 1 = _; rw [hr]⟩

end PhiSq

#print axioms PhiSq.two_pm
#print axioms PhiSq.inf_7m8

lang/Inf7mod12.lean

lean_all/lang/Inf7mod12.lean

/-
  lang/Inf7mod12 — T43, cycle 202: infinitely many primes p ≡ 7 (mod 12).

  x = odd factorial of B, N = 3x² + 4 ≡ 7 (mod 12). Every prime factor q has −3 ≡ (3x(q+1)/2)², so q ≡ 1 (mod 3) (Eisen.inert_neg3),
  q ≡ 1 or 7 (mod 12); the product is ≡ 7, so some factor is ≡ 7; an odd q ≤ B divides x and would divide 4.
    uniq_r12, mod12m, cofactor7, allowed7, has7, dv4_absurd, inf_7mod12
  The class 7 mod 12: split in ℚ(√−3), inert in ℚ(i).

  Lean 4 core only, proofs by hand (lib_nat, lib_int), no axioms.
-/
import lang.Inf11mod12
import lang.Inf5mod8

namespace Inf7mod12

open FTAChain ZMod LibInt InfSplit
open LibRing (castZ castN)

theorem uniq_r12 (x y r c : Nat) (hr : r < 12) (hc : c < 12) (h : x * 12 + r = y * 12 + c) : r = c := by
  have big : ∀ x y r c : Nat, r < 12 → x < y → x * 12 + r = y * 12 + c → False := fun x y r c hr hxy h => by
    have h1 : x * 12 + r < (x + 1) * 12 := by rw [Nat.succ_mul]; exact Nat.add_lt_add_left hr _
    have h2 : (x + 1) * 12 ≤ y * 12 := Nat.mul_le_mul_right 12 hxy
    exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (h ▸ h1) (Nat.le_trans h2 (Nat.le_add_right _ _)))
  cases Nat.lt_or_ge x y with
  | inl hxy => exact absurd h (fun h' => big x y r c hr hxy h')
  | inr hxy =>
    cases Nat.lt_or_ge y x with
    | inl hyx => exact absurd h.symm (fun h' => big y x c r hc hyx h')
    | inr hyx =>
      have e : x = y := Nat.le_antisymm hyx hxy
      subst e
      apply Int.ofNat.inj
      have := congrArg Int.ofNat h
      have e' : Int.ofNat x * 12 + Int.ofNat r = Int.ofNat x * 12 + Int.ofNat c := this
      calc Int.ofNat r = (Int.ofNat x * 12 + Int.ofNat r) - Int.ofNat x * 12 := by lib_int
        _ = Int.ofNat c := by rw [e']; lib_int

/-- residue of n mod 8 as a case split. -/
theorem mod12m (n : Nat) : ∃ a r, n = a * 12 + r ∧ r < 12 :=
  match div_exists 11 n with
  | ⟨a, r, k, hn, hr⟩ => ⟨a, r, hn, Nat.lt_of_lt_of_le (Nat.lt_succ_self r) (by rw [show (12 : Nat) = 11 + 1 from rfl, ← hr]; exact Nat.succ_le_succ (Nat.le_add_right r k))⟩

theorem cofactor7 (b N' a : Nat) (h : (b * 12 + 1) * N' = a * 12 + 7) : ∃ c, N' = c * 12 + 7 :=
  match mod12m N' with
  | ⟨c, r, hc, hr⟩ => by
    have e : (b * 12 + 1) * N' = (b * 12 * c + b * r + c) * 12 + r := by rw [hc]; lib_nat
    rw [e] at h
    exact ⟨c, by rw [hc, uniq_r12 _ _ r 7 hr (Nat.lt_of_lt_of_le (Nat.lt_succ_self 7) (Nat.le_succ_of_le (Nat.le_succ_of_le (Nat.le_succ_of_le (Nat.le_succ 8))))) h]⟩




/-- prime factors of 3x² + 4 (x odd) are ≡ 1 or 7 (mod 12). -/
theorem allowed7 (x l : Nat) (hx : ∃ y, x = 2 * y + 1) (hq : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1)
    (hdv : Dv (l + 1 + 1) (3 * x * x + 4)) : ∃ b t, l + 1 + 1 = b * 12 + t ∧ (t = 1 ∨ t = 7) := by
  match hx with
  | ⟨y, hy⟩ =>
  have hodd : ∀ c, l + 1 + 1 = 2 * c → False := fun c hc => by
    have h2 : l + 1 + 1 = 2 := by
      cases hq 2 ⟨c, by rw [hc]; lib_nat⟩ with
      | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
      | inr e => exact e.symm
    rw [h2] at hdv
    match hdv with
    | ⟨w, hw⟩ => exact InfSplit.odd_not_dv2 (6 * y * y + 6 * y + 3) w (by rw [← hw, hy]; lib_nat)
  have h3 : l + 1 + 1 ≠ 3 := fun e => by
    rw [e] at hdv
    match hdv with
    | ⟨w, hw⟩ =>
      have hI : Int.ofNat w * 3 = 3 * Int.ofNat (x * x + 1) + 1 :=
        (congrArg Int.ofNat hw).symm.trans (congrArg Int.ofNat (by lib_nat : (3 * x * x + 4) = 3 * (x * x + 1) + 1))
      exact EisenAp.three_ne_r (Int.ofNat w - Int.ofNat (x * x + 1)) 1 (fun e => Nat.noConfusion e) (Nat.lt_succ_of_lt (Nat.lt_succ_self 1))
        (by show _ = 1; rw [show 3 * (Int.ofNat w - Int.ofNat (x * x + 1)) = Int.ofNat w * 3 - 3 * Int.ofNat (x * x + 1) by lib_int, hI]; lib_int)
  match EveryPrime.nat_even_odd l with
  | Or.inl ⟨c, hc⟩ => exact absurd (by rw [hc]; lib_nat : l + 1 + 1 = 2 * (c + 1)) (fun e => hodd (c + 1) e)
  | Or.inr ⟨r, hr⟩ =>
    subst hr
    have hr1 : 1 ≤ r := by
      cases r with
      | zero => exact absurd rfl h3
      | succ r => exact Nat.succ_le_succ (Nat.zero_le r)
    have hz : (ring (2 * r + 1 + 1)).mul (castZ (ring (2 * r + 1 + 1)) (Int.ofNat (3 * x * (r + 2)))) (castZ (ring (2 * r + 1 + 1)) (Int.ofNat (3 * x * (r + 2)))) = (ring (2 * r + 1 + 1)).neg (Eisen.three (2 * r + 1)) := by
      match hdv with
      | ⟨w, hw⟩ =>
        apply DetChain.neg_unique
        rw [← Inf11mod12.castZ3, ← HasseDavenport.castZ_mul, ← IntCast.castZ_add,
          show Int.ofNat (3 * x * (r + 2)) * Int.ofNat (3 * x * (r + 2)) + 3 = Int.ofNat (2 * r + 1 + 1 + 1) * (3 * Int.ofNat ((r + 2) * (r + 2) * w) - 3 * Int.ofNat (2 * r + 5)) from by
            have hwI : Int.ofNat (3 * x * x + 4) = Int.ofNat w * Int.ofNat (2 * r + 1 + 1 + 1) := congrArg Int.ofNat hw
            have e1 : Int.ofNat (3 * x * x + 4) = 3 * Int.ofNat x * Int.ofNat x + 4 := rfl
            show Int.ofNat (3 * x * (r + 2)) * Int.ofNat (3 * x * (r + 2)) + 3 = Int.ofNat (2 * r + 1 + 1 + 1) * (3 * (Int.ofNat ((r + 2) * (r + 2)) * Int.ofNat w) - 3 * Int.ofNat (2 * r + 5))
            have e2 : Int.ofNat (3 * x * (r + 2)) = 3 * Int.ofNat x * (Int.ofNat r + 2) := rfl
            have e3 : Int.ofNat ((r + 2) * (r + 2)) = (Int.ofNat r + 2) * (Int.ofNat r + 2) := rfl
            have e4 : Int.ofNat (2 * r + 5) = 2 * Int.ofNat r + 5 := rfl
            have e5 : Int.ofNat (2 * r + 1 + 1 + 1) = 2 * Int.ofNat r + 3 := rfl
            rw [e2, e3, e4, e5]
            rw [e1, e5] at hwI
            rw [show (3 : Int) * Int.ofNat x * (Int.ofNat r + 2) * (3 * Int.ofNat x * (Int.ofNat r + 2)) + 3
              = 3 * (Int.ofNat r + 2) * (Int.ofNat r + 2) * (3 * Int.ofNat x * Int.ofNat x + 4) - 3 * (2 * Int.ofNat r + 3) * (2 * Int.ofNat r + 5) by lib_int, hwI]
            lib_int,
          HasseDavenport.castZ_mul, CubeAp.castP, DetChain.zero_mul']
    have hJ : 2 ≤ 2 * r + 1 := by
      match r, hr1 with
      | r + 1, _ => exact Nat.le_trans (Nat.le_add_left 2 (2 * r + 1)) (Nat.le_of_eq (by lib_nat))
    match Eisen.inert_neg3 (2 * r + 1) hq hJ _ hz with
    | ⟨c, hc⟩ =>
      match EveryPrime.nat_even_odd c with
      | Or.inr ⟨d, hd⟩ =>
        exfalso; apply hodd (3 * d + 2)
        rw [show 2 * r + 1 + 1 + 1 = (2 * r + 1 + 1) + 1 by lib_nat, hc, hd]; lib_nat
      | Or.inl ⟨d, hd⟩ =>
        match EveryPrime.nat_even_odd d with
        | Or.inl ⟨e, he⟩ => exact ⟨e, 1, by rw [show 2 * r + 1 + 1 + 1 = (2 * r + 1 + 1) + 1 by lib_nat, hc, hd, he]; lib_nat, Or.inl rfl⟩
        | Or.inr ⟨e, he⟩ => exact ⟨e, 7, by rw [show 2 * r + 1 + 1 + 1 = (2 * r + 1 + 1) + 1 by lib_nat, hc, hd, he]; lib_nat, Or.inr rfl⟩

/-- **A factor ≡ 7 (mod 12).** -/
theorem has7 (x : Nat) (hx : ∃ y, x = 2 * y + 1) : ∀ G N, N ≤ G → Dv N (3 * x * x + 4) → (∃ a, N = a * 12 + 7) →
    ∃ q, (∀ d, Dv d q → d = 1 ∨ d = q) ∧ (∃ b, q = b * 12 + 7) ∧ Dv q N
  | 0, N, hN, _, ⟨a, ha⟩ => absurd (ha ▸ hN) (fun h => Nat.not_succ_le_zero _ (Nat.le_trans (Nat.le_add_left _ _) h))
  | G + 1, N, hN, hN0, ⟨a, ha⟩ => by
    match lp_exists (a * 12 + 5) with
    | ⟨l, hl, hmin⟩ =>
    have hq := lp_prime _ l hl hmin
    have hlN : Dv (l + 1 + 1) N := by rw [ha]; exact hl
    match allowed7 x l hx hq (dv_trans _ _ _ hlN hN0) with
    | ⟨b, t, hb, ht⟩ =>
      cases ht with
      | inr h7 => exact ⟨l + 1 + 1, hq, ⟨b, by rw [hb, h7]⟩, hlN⟩
      | inl h1 =>
        match hlN with
        | ⟨N', hN'⟩ =>
        match cofactor7 b N' a (by rw [← h1, ← hb, ← ha, hN']; lib_nat) with
        | ⟨c, hc⟩ =>
        have hlt : N' ≤ G := by
          have e : N = N' + (N' * (l + 1)) := by rw [hN']; lib_nat
          have h1' : 1 ≤ N' * (l + 1) := by rw [hc]; exact Nat.mul_le_mul (Nat.le_add_left 1 _) (Nat.le_add_left 1 _)
          exact Nat.le_of_succ_le_succ (Nat.le_trans (Nat.add_le_add_left h1' N') (by rw [← e]; exact hN))
        have hN'0 : Dv N' (3 * x * x + 4) := dv_trans _ _ _ ⟨l + 1 + 1, by rw [hN']; lib_nat⟩ hN0
        match has7 x hx G N' hlt hN'0 ⟨c, hc⟩ with
        | ⟨q, hq', hq7, hqN⟩ => exact ⟨q, hq', hq7, dv_trans q N' N hqN ⟨l + 1 + 1, by rw [hN']; lib_nat⟩⟩

theorem dv4_absurd (b : Nat) (h : Dv (b * 12 + 7) 4) : False := by
  match h with
  | ⟨w, hw⟩ =>
    cases w with
    | zero => exact Nat.noConfusion (hw.trans (Nat.zero_mul _))
    | succ w => exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (hw.trans (show (w + 1) * (b * 12 + 7) = (w * (b * 12 + 7) + b * 12 + 2) + 1 + 1 + 1 + 1 + 1 by lib_nat))))))

/-- **Infinitely many primes p ≡ 7 (mod 12).** -/
theorem inf_7mod12 (B : Nat) : ∃ b, (∀ d, Dv d (b * 12 + 7) → d = 1 ∨ d = b * 12 + 7) ∧ B < b * 12 + 7 := by
  match Inf5mod8.ofac_odd B with
  | ⟨y, hy⟩ =>
  have hN : 3 * Inf5mod8.ofac B * Inf5mod8.ofac B + 4 = (y * y + y) * 12 + 7 := by rw [hy]; lib_nat
  match has7 (Inf5mod8.ofac B) ⟨y, hy⟩ _ _ (Nat.le_refl _) ⟨1, by lib_nat⟩ ⟨y * y + y, hN⟩ with
  | ⟨q, hq, ⟨b, hb⟩, hqN⟩ =>
    refine ⟨b, by rw [← hb]; exact hq, ?_⟩
    rw [← hb]
    cases Nat.lt_or_ge B q with
    | inl h => exact h
    | inr h =>
      exfalso
      match Inf5mod8.dv_ofac B (6 * b + 3) (by rw [show 2 * (6 * b + 3) + 1 = b * 12 + 7 by lib_nat, ← hb]; exact h) with
      | ⟨r, hr⟩ =>
        have hr' : Inf5mod8.ofac B = r * q := by rw [hr, hb]; lib_nat
        have h1 : Dv q (3 * Inf5mod8.ofac B * Inf5mod8.ofac B) := ⟨3 * r * Inf5mod8.ofac B, by rw [hr']; lib_nat⟩
        have hq2 : q = (b * 12 + 6) + 1 := hb
        rw [hq2] at hqN h1
        exact dv4_absurd b (by rw [show b * 12 + 7 = (b * 12 + 6) + 1 from rfl]; exact dv_sub (b * 12 + 6) _ 4 h1 hqN)

end Inf7mod12

#print axioms Inf7mod12.uniq_r12
#print axioms Inf7mod12.mod12m
#print axioms Inf7mod12.cofactor7
#print axioms Inf7mod12.allowed7
#print axioms Inf7mod12.has7
#print axioms Inf7mod12.dv4_absurd
#print axioms Inf7mod12.inf_7mod12

lang/Inf7mod24.lean

lean_all/lang/Inf7mod24.lean

/-
  lang/Inf7mod24 — T43, cycle 209: infinitely many primes p ≡ 7 (mod 24).

  y = odd factorial of B, X = y² = 4V + 1, F + 1 = y·X, N = 4X² + 2X + 1 ≡ 7 (mod 8).
  8F² + 16F + 7 = 8X³ − 1 = (2X − 1)·N, so N has a prime factor q ≡ 7 (mod 8) (Inf7mod8.has7);
  4N = (4X + 1)² + 3, so −3 is a square mod q and q ≡ 1 (mod 3) (Eisen.inert_neg3); hence q ≡ 7 (mod 24).
  An odd q ≤ B divides y, hence X, hence N − X(4X + 2) = 1.
    castZ3', allowed3, uniq_rem3, mod3, class24, cube_gen, dv_main, main_gen, inf_7mod24
  The class 7 mod 24: split in ℚ(√−3) and ℚ(√2), inert in ℚ(i).

  Lean 4 core only, proofs by hand (lib_nat, lib_int), no axioms.
-/
import lang.Inf7mod12
import lang.Inf7mod8

namespace Inf7mod24

open FTAChain ZMod LibInt InfSplit
open LibRing (castZ castN)

theorem castZ3' (J : Nat) : castZ (ring (J + 1)) 3 = Eisen.three J := by
  show (ring (J + 1)).add (ring (J + 1)).one ((ring (J + 1)).add (ring (J + 1)).one ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).zero)) = _
  rw [DetChain.add_zero']; rfl

/-- a prime q = J + 2 dividing N with z² + 3 = 4N is ≡ 1 (mod 3). -/
theorem allowed3 (J N z : Nat) (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (h3 : 2 ≤ J)
    (hdv : Dv (J + 1 + 1) N) (hz : z * z + 3 = 4 * N) : Dv (1 + 2) (J + 1) := by
  have hzz : (ring (J + 1)).mul (castZ (ring (J + 1)) (Int.ofNat z)) (castZ (ring (J + 1)) (Int.ofNat z)) = (ring (J + 1)).neg (Eisen.three J) := by
    match hdv with
    | ⟨w, hw⟩ =>
      apply DetChain.neg_unique
      have hn : z * z + 3 = (J + 1 + 1) * (4 * w) := by rw [hz, hw]; lib_nat
      have hI : Int.ofNat z * Int.ofNat z + 3 = Int.ofNat (J + 1 + 1) * Int.ofNat (4 * w) := congrArg Int.ofNat hn
      rw [← castZ3' J, ← HasseDavenport.castZ_mul, ← IntCast.castZ_add, hI, HasseDavenport.castZ_mul, CubeAp.castP, DetChain.zero_mul']
  exact Eisen.inert_neg3 J hpr h3 _ hzz

theorem uniq_rem3 (x y r c : Nat) (hr : r < 3) (hc : c < 3) (h : x * 3 + r = y * 3 + c) : r = c := by
  have big : ∀ x y r c : Nat, r < 3 → x < y → x * 3 + r = y * 3 + c → False := fun x y r c hr hxy h => by
    have h1 : x * 3 + r < (x + 1) * 3 := by rw [Nat.succ_mul]; exact Nat.add_lt_add_left hr _
    have h2 : (x + 1) * 3 ≤ y * 3 := Nat.mul_le_mul_right 3 hxy
    exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (h ▸ h1) (Nat.le_trans h2 (Nat.le_add_right _ _)))
  cases Nat.lt_or_ge x y with
  | inl hxy => exact absurd h (fun h' => big x y r c hr hxy h')
  | inr hxy =>
    cases Nat.lt_or_ge y x with
    | inl hyx => exact absurd h.symm (fun h' => big y x c r hc hyx h')
    | inr hyx =>
      have e : x = y := Nat.le_antisymm hyx hxy
      subst e
      apply Int.ofNat.inj
      have := congrArg Int.ofNat h
      have e' : Int.ofNat x * 3 + Int.ofNat r = Int.ofNat x * 3 + Int.ofNat c := this
      calc Int.ofNat r = (Int.ofNat x * 3 + Int.ofNat r) - Int.ofNat x * 3 := by lib_int
        _ = Int.ofNat c := by rw [e']; lib_int

theorem mod3 (n : Nat) : ∃ a r, n = a * 3 + r ∧ r < 3 :=
  match div_exists 2 n with
  | ⟨a, r, k, hn, hr⟩ => ⟨a, r, hn, Nat.lt_of_lt_of_le (Nat.lt_succ_self r) (by rw [show (3 : Nat) = 2 + 1 from rfl, ← hr]; exact Nat.succ_le_succ (Nat.le_add_right r k))⟩

/-- q = 8b + 7 with 3 | q − 1 is ≡ 7 (mod 24). -/
theorem class24 (b : Nat) (h : Dv (1 + 2) (b * 8 + 5 + 1)) : ∃ e, b * 8 + 7 = e * 24 + 7 := by
  match h, mod3 b with
  | ⟨c, hc⟩, ⟨a, r, hb, hr⟩ =>
    match r, hr with
    | 0, _ => exact ⟨a, by rw [hb]; lib_nat⟩
    | 1, _ =>
      exfalso
      have e := uniq_rem3 (a * 8 + 4) c 2 0 (Nat.lt_succ_self 2) (Nat.zero_lt_succ 2)
        (by rw [show (a * 8 + 4) * 3 + 2 = (a * 3 + 1) * 8 + 5 + 1 by lib_nat, ← hb, hc]; lib_nat)
      exact Nat.noConfusion e
    | 2, _ =>
      exfalso
      have e := uniq_rem3 (a * 8 + 7) c 1 0 (Nat.lt_succ_of_lt (Nat.lt_succ_self 1)) (Nat.zero_lt_succ 2)
        (by rw [show (a * 8 + 7) * 3 + 1 = (a * 3 + 2) * 8 + 5 + 1 by lib_nat, ← hb, hc]; lib_nat)
      exact Nat.noConfusion e
    | r + 3, hr => exact absurd hr (fun h => Nat.lt_irrefl 3 (Nat.lt_of_le_of_lt (Nat.le_add_left 3 r) h))

/-- P + 1 = 2X ⟹ P(4X² + 2X + 1) + 1 = 8X³. -/
theorem cube_gen (X P : Nat) (h : P + 1 = 2 * X) : P * (4 * X * X + 2 * X + 1) + 1 = 8 * X * X * X := by
  apply LibInt.nadd_right_cancel (4 * X * X + 2 * X)
  calc P * (4 * X * X + 2 * X + 1) + 1 + (4 * X * X + 2 * X) = (P + 1) * (4 * X * X + 2 * X + 1) := by lib_nat
    _ = 2 * X * (4 * X * X + 2 * X + 1) := by rw [h]
    _ = 8 * X * X * X + (4 * X * X + 2 * X) := by lib_nat

/-- 8F² + 16F + 7 = (2X − 1)(4X² + 2X + 1) when F + 1 = yX, y² = X = 4V + 1. -/
theorem dv_main (y X V F : Nat) (hyy : y * y = X) (hX : X = 4 * V + 1) (hF : F + 1 = y * X) :
    8 * F * F + 16 * F + 7 = (8 * V + 1) * (4 * X * X + 2 * X + 1) := by
  apply LibInt.nadd_right_cancel 1
  calc 8 * F * F + 16 * F + 7 + 1 = 8 * (F + 1) * (F + 1) := by lib_nat
    _ = 8 * (y * X) * (y * X) := by rw [hF]
    _ = 8 * (y * y) * X * X := by lib_nat
    _ = 8 * X * X * X := by rw [hyy]
    _ = (8 * V + 1) * (4 * X * X + 2 * X + 1) + 1 := (cube_gen X (8 * V + 1) (by rw [hX]; lib_nat)).symm

theorem main_gen (B y u V X : Nat) (hy : y = 2 * u + 1) (hyy : y * y = X) (hX : X = 4 * V + 1)
    (hyB : ∀ c, 2 * c + 1 ≤ B → Dv (2 * c + 1) y) :
    ∃ e, (∀ d, Dv d (e * 24 + 7) → d = 1 ∨ d = e * 24 + 7) ∧ B < e * 24 + 7 := by
  have hF : 2 * u * X + 4 * V + 1 = y * X := by rw [hy, hX]; lib_nat
  have hdiv : Dv (4 * X * X + 2 * X + 1) (8 * (2 * u * X + 4 * V) * (2 * u * X + 4 * V) + 16 * (2 * u * X + 4 * V) + 7) :=
    ⟨8 * V + 1, dv_main y X V _ hyy hX hF⟩
  have hN8 : 4 * X * X + 2 * X + 1 = (8 * V * V + 5 * V) * 8 + 7 := by rw [hX]; lib_nat
  match Inf7mod8.has7 _ _ _ (Nat.le_refl _) hdiv ⟨_, hN8⟩ with
  | ⟨q, hq, ⟨b, hb⟩, hqN⟩ =>
    rw [hb] at hq hqN
    have h3 : 2 ≤ b * 8 + 5 := Nat.le_trans (Nat.le_succ_of_le (Nat.le_succ_of_le (Nat.le_succ_of_le (Nat.le_refl 2)))) (Nat.le_add_left 5 _)
    match class24 b (allowed3 (b * 8 + 5) _ (4 * X + 1) hq h3 hqN (by lib_nat)) with
    | ⟨e, he⟩ =>
      rw [he] at hq hqN
      refine ⟨e, hq, ?_⟩
      cases Nat.lt_or_ge B (e * 24 + 7) with
      | inl h => exact h
      | inr h =>
        exfalso
        match hyB (12 * e + 3) (by rw [show 2 * (12 * e + 3) + 1 = e * 24 + 7 by lib_nat]; exact h) with
        | ⟨r, hr⟩ =>
          have h1 : Dv (e * 24 + 7) (X * (4 * X + 2)) := ⟨r * y * (4 * X + 2), by
            rw [← hyy]
            have e1 : y * y * (4 * (y * y) + 2) = r * (2 * (12 * e + 3) + 1) * y * (4 * (y * y) + 2) :=
              congrArg (fun t => t * y * (4 * (y * y) + 2)) hr
            rw [e1]; lib_nat⟩
          have hN1 : 4 * X * X + 2 * X + 1 = X * (4 * X + 2) + 1 := by lib_nat
          rw [hN1] at hqN
          exact dv_one_absurd (e * 24 + 5) (dv_sub (e * 24 + 6) _ 1 h1 hqN)

/-- **Infinitely many primes p ≡ 7 (mod 24).** -/
theorem inf_7mod24 (B : Nat) : ∃ e, (∀ d, Dv d (e * 24 + 7) → d = 1 ∨ d = e * 24 + 7) ∧ B < e * 24 + 7 :=
  match Inf5mod8.ofac_odd B with
  | ⟨u, hu⟩ =>
    main_gen B (Inf5mod8.ofac B) u (u * u + u) (Inf5mod8.ofac B * Inf5mod8.ofac B) hu rfl (by rw [hu]; lib_nat)
      (fun c h => Inf5mod8.dv_ofac B c h)

end Inf7mod24

#print axioms Inf7mod24.castZ3'
#print axioms Inf7mod24.allowed3
#print axioms Inf7mod24.uniq_rem3
#print axioms Inf7mod24.mod3
#print axioms Inf7mod24.class24
#print axioms Inf7mod24.cube_gen
#print axioms Inf7mod24.dv_main
#print axioms Inf7mod24.main_gen
#print axioms Inf7mod24.inf_7mod24

lang/Inf7mod8.lean

lean_all/lang/Inf7mod8.lean

/-
  lang/Inf7mod8 — T43, cycle 128: infinitely many primes p ≡ 7 (mod 8).

  N = 8·(B!)² − 1 ≡ 7 (mod 8). For every prime factor q of N, 2·(2·B!)² ≡ 1, so 2 ≡ (4·B!)² is a square mod q and
  q ≡ ±1 (mod 8) (TwoSupp: 2 is not a square at 8m + 3, 8m + 5). A product of primes ≡ 1 (mod 8) is ≡ 1, so some factor is
  ≡ 7; and q ∤ B! gives q > B.
    uniq_rem8, cofactor8     residues mod 8
    allowed8                 q | N ⇒ q ≡ 1 or 7 (mod 8)
    has7                     a factor ≡ 7 (mod 8) of every divisor N ≡ 7 of N₀
    inf_7mod8                ∀ B, ∃ prime p = 8b + 7 > B
  With InfCycloPow (ℓ^e = 8) both residues ±1 mod 8 of the split primes of ℚ(√2) occur infinitely often.

  Lean 4 core only, proofs by hand (lib_nat, lib_int), no axioms.
-/
import lang.InfInert3
import lang.TwoSupp

namespace Inf7mod8

open FTAChain ZMod LibInt InfSplit TwoSupp

theorem uniq_rem8 (x y r c : Nat) (hr : r < 8) (hc : c < 8) (h : x * 8 + r = y * 8 + c) : r = c := by
  have big : ∀ x y r c : Nat, r < 8 → x < y → x * 8 + r = y * 8 + c → False := fun x y r c hr hxy h => by
    have h1 : x * 8 + r < (x + 1) * 8 := by rw [Nat.succ_mul]; exact Nat.add_lt_add_left hr _
    have h2 : (x + 1) * 8 ≤ y * 8 := Nat.mul_le_mul_right 8 hxy
    exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (h ▸ h1) (Nat.le_trans h2 (Nat.le_add_right _ _)))
  cases Nat.lt_or_ge x y with
  | inl hxy => exact absurd h (fun h' => big x y r c hr hxy h')
  | inr hxy =>
    cases Nat.lt_or_ge y x with
    | inl hyx => exact absurd h.symm (fun h' => big y x c r hc hyx h')
    | inr hyx =>
      have e : x = y := Nat.le_antisymm hyx hxy
      subst e
      apply Int.ofNat.inj
      have := congrArg Int.ofNat h
      have e' : Int.ofNat x * 8 + Int.ofNat r = Int.ofNat x * 8 + Int.ofNat c := this
      calc Int.ofNat r = (Int.ofNat x * 8 + Int.ofNat r) - Int.ofNat x * 8 := by lib_int
        _ = Int.ofNat c := by rw [e']; lib_int

/-- residue of n mod 8 as a case split. -/
theorem mod8 (n : Nat) : ∃ a r, n = a * 8 + r ∧ r < 8 :=
  match div_exists 7 n with
  | ⟨a, r, k, hn, hr⟩ => ⟨a, r, hn, Nat.lt_of_lt_of_le (Nat.lt_succ_self r) (by rw [show (8 : Nat) = 7 + 1 from rfl, ← hr]; exact Nat.succ_le_succ (Nat.le_add_right r k))⟩

theorem cofactor8 (b N' a : Nat) (h : (b * 8 + 1) * N' = a * 8 + 7) : ∃ c, N' = c * 8 + 7 :=
  match mod8 N' with
  | ⟨c, r, hc, hr⟩ => by
    have e : (b * 8 + 1) * N' = (b * 8 * c + b * r + c) * 8 + r := by rw [hc]; lib_nat
    rw [e] at h
    exact ⟨c, by rw [hc, uniq_rem8 _ _ r 7 hr (Nat.lt_succ_self 7) h]⟩

/-- Every prime factor of N₀ = 8F² + 16F + 7 = 8(F + 1)² − 1 is ≡ 1 or 7 (mod 8). -/
theorem allowed8 (F l : Nat) (hq : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1)
    (hdv : Dv (l + 1 + 1) (8 * F * F + 16 * F + 7)) : ∃ b r, l + 1 + 1 = b * 8 + r ∧ (r = 1 ∨ r = 7) := by
  have hPr : Pr (l + 1 + 1) := ⟨⟨l, rfl⟩, hq⟩
  -- 2 is a square mod q: (4(F+1))² = 2 + 2 N₀
  have hsq : rem (l + 1 + 1) ((4 * (F + 1)) * (4 * (F + 1))) = rem (l + 1 + 1) 2 := by
    match hdv with
    | ⟨r, hr⟩ =>
      rw [show (4 * (F + 1)) * (4 * (F + 1)) = (2 * r) * (l + 1 + 1) + 2 by rw [show (2 * r) * (l + 1 + 1) = 2 * (r * (l + 1 + 1)) by lib_nat, ← hr]; lib_nat]
      exact rem_add_mul (l + 1) 2 (2 * r)
  have hodd : ∀ c, l + 1 + 1 = 2 * c → False := fun c hc => by
    have h2 : l + 1 + 1 = 2 := by
      cases hq 2 ⟨c, by rw [hc]; lib_nat⟩ with
      | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
      | inr e => exact e.symm
    rw [h2] at hdv
    match hdv with
    | ⟨r, hr⟩ => exact odd_not_dv2 (4 * F * F + 8 * F + 3) r (by rw [← hr]; lib_nat)
  match mod8 (l + 1 + 1) with
  | ⟨b, r, hb, hr⟩ =>
    match r, hr with
    | 0, _ => exact absurd hb (fun e => hodd (4 * b) (by rw [e]; lib_nat))
    | 1, _ => exact ⟨b, 1, hb, Or.inl rfl⟩
    | 2, _ => exact absurd hb (fun e => hodd (4 * b + 1) (by rw [e]; lib_nat))
    | 3, _ =>
      exfalso
      have e : (b + b) + ((b + b) + 1) + ((b + b) + ((b + b) + 1)) + 1 = l + 1 + 1 := by rw [hb]; lib_nat
      exact two_8m3 b (by rw [e]; exact hPr) ⟨4 * (F + 1), by rw [e]; exact hsq⟩
    | 4, _ => exact absurd hb (fun e => hodd (4 * b + 2) (by rw [e]; lib_nat))
    | 5, _ =>
      exfalso
      have e : (b + b + 1) + ((b + b + 1) + 0) + ((b + b + 1) + ((b + b + 1) + 0)) + 1 = l + 1 + 1 := by rw [hb]; lib_nat
      exact two_8m5 b (by rw [e]; exact hPr) ⟨4 * (F + 1), by rw [e]; exact hsq⟩
    | 6, _ => exact absurd hb (fun e => hodd (4 * b + 3) (by rw [e]; lib_nat))
    | 7, _ => exact ⟨b, 7, hb, Or.inr rfl⟩
    | r + 8, hr => exact absurd hr (fun h => Nat.lt_irrefl 8 (Nat.lt_of_le_of_lt (Nat.le_add_left 8 r) h))

/-- **A factor ≡ 7 (mod 8).** -/
theorem has7 (F : Nat) : ∀ G N, N ≤ G → Dv N (8 * F * F + 16 * F + 7) → (∃ a, N = a * 8 + 7) →
    ∃ q, (∀ d, Dv d q → d = 1 ∨ d = q) ∧ (∃ b, q = b * 8 + 7) ∧ Dv q N
  | 0, N, hN, _, ⟨a, ha⟩ => absurd (ha ▸ hN) (fun h => Nat.not_succ_le_zero _ (Nat.le_trans (Nat.le_add_left _ _) h))
  | G + 1, N, hN, hN0, ⟨a, ha⟩ => by
    match lp_exists (a * 8 + 5) with
    | ⟨l, hl, hmin⟩ =>
    have hq := lp_prime _ l hl hmin
    have hlN : Dv (l + 1 + 1) N := by rw [ha]; exact hl
    match allowed8 F l hq (dv_trans _ _ _ hlN hN0) with
    | ⟨b, r, hb, hr⟩ =>
      cases hr with
      | inr h7 => exact ⟨l + 1 + 1, hq, ⟨b, by rw [hb, h7]⟩, hlN⟩
      | inl h1 =>
        match hlN with
        | ⟨N', hN'⟩ =>
        match cofactor8 b N' a (by rw [← h1, ← hb, ← ha, hN']; lib_nat) with
        | ⟨c, hc⟩ =>
        have hlt : N' ≤ G := by
          have e : N = N' + (N' * (l + 1)) := by rw [hN']; lib_nat
          have h1' : 1 ≤ N' * (l + 1) := by rw [hc]; exact Nat.mul_le_mul (Nat.le_add_left 1 _) (Nat.le_add_left 1 _)
          exact Nat.le_of_succ_le_succ (Nat.le_trans (Nat.add_le_add_left h1' N') (by rw [← e]; exact hN))
        have hN'0 : Dv N' (8 * F * F + 16 * F + 7) := dv_trans _ _ _ ⟨l + 1 + 1, by rw [hN']; lib_nat⟩ hN0
        match has7 F G N' hlt hN'0 ⟨c, hc⟩ with
        | ⟨q, hq', hq7, hqN⟩ => exact ⟨q, hq', hq7, dv_trans q N' N hqN ⟨l + 1 + 1, by rw [hN']; lib_nat⟩⟩

/-- **Infinitely many primes p ≡ 7 (mod 8).** -/
theorem inf_7mod8 (B : Nat) : ∃ b, (∀ d, Dv d (b * 8 + 7) → d = 1 ∨ d = b * 8 + 7) ∧ B < b * 8 + 7 := by
  match fac_pos B with
  | ⟨f, hf⟩ =>
  match has7 f (8 * f * f + 16 * f + 7) (8 * f * f + 16 * f + 7) (Nat.le_refl _) ⟨1, by lib_nat⟩ ⟨f * f + 2 * f, by lib_nat⟩ with
  | ⟨q, hq, ⟨b, hb⟩, hqN⟩ =>
    refine ⟨b, by rw [← hb]; exact hq, ?_⟩
    rw [← hb]
    cases Nat.lt_or_ge B q with
    | inl h => exact h
    | inr h =>
      exfalso
      match dv_fac B q (by rw [hb]; exact Nat.le_add_left 1 _) h with
      | ⟨r, hr⟩ =>
        have h8 : Dv q (8 * f * f + 16 * f + 7 + 1) := ⟨8 * r * (f + 1), by
          rw [show 8 * f * f + 16 * f + 7 + 1 = 8 * (f + 1) * (f + 1) by lib_nat, ← hf, hr]; lib_nat⟩
        have hq2 : q = (b * 8 + 6) + 1 := hb
        rw [hq2] at hqN h8
        exact dv_one_absurd (b * 8 + 5) (dv_sub (b * 8 + 6) _ 1 hqN h8)

end Inf7mod8

#print axioms Inf7mod8.uniq_rem8
#print axioms Inf7mod8.mod8
#print axioms Inf7mod8.cofactor8
#print axioms Inf7mod8.allowed8
#print axioms Inf7mod8.has7
#print axioms Inf7mod8.inf_7mod8

lang/Inf9mod16.lean

lean_all/lang/Inf9mod16.lean

/-
  lang/Inf9mod16 — T43, cycle 303: infinitely many primes p ≡ 9 (mod 16).

  O = 1·3·5···(2B − 1) (odd factorial), O² = 8C + 1, x = 2O² + 1 = 16C + 3, x⁴ + 1 = 2M with M ≡ 9 (mod 16), M odd.
  Every prime factor q of M has x⁴ = −1 ≠ 1, x⁸ = 1 in 𝔽_q, so 8 | q − 1 (CycloPow.dvd_of_pow): q ≡ 1 or 9 (mod 16). The product
  is ≡ 9, so some factor is ≡ 9 (descent has16, as Inf17mod24.has_gen). An odd q ≤ 2B − 1 divides O, hence x⁴ + 1 − 2, hence 2.
    oddf, oddf_sq, oddf_dv, uniq_rem16, mod16n, cofactor16, has16, allowed16, nM, inf_9mod16
  The class 9 mod 16: split in ℚ(ζ₈), inert in ℚ(ζ₁₆)/ℚ(ζ₈) (a² ≡ 1 mod 16, so Euclid's method applies).
  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.Inf17mod24

namespace Inf9mod16

open FTAChain ZMod InfSplit

/-- O(B) = 1·3·5···(2B − 1). -/
def oddf : Nat → Nat
  | 0 => 1
  | k + 1 => oddf k * (2 * k + 1)

theorem odd_sq : ∀ n, ∃ T, (2 * n + 1) * (2 * n + 1) = 8 * T + 1
  | 0 => ⟨0, rfl⟩
  | n + 1 => by
    match odd_sq n with
    | ⟨T, hT⟩ => exact ⟨T + n + 1, by
        rw [show (2 * (n + 1) + 1) * (2 * (n + 1) + 1) = (2 * n + 1) * (2 * n + 1) + 8 * (n + 1) by lib_nat, hT]; lib_nat⟩

theorem oddf_sq : ∀ B, ∃ C, oddf B * oddf B = 8 * C + 1
  | 0 => ⟨0, rfl⟩
  | k + 1 => by
    match oddf_sq k, odd_sq k with
    | ⟨C, hC⟩, ⟨T, hT⟩ =>
      have e1 : oddf (k + 1) * oddf (k + 1) = (oddf k * oddf k) * ((2 * k + 1) * (2 * k + 1)) := by
        show (oddf k * (2 * k + 1)) * (oddf k * (2 * k + 1)) = _
        lib_nat
      rw [e1, hC, hT]
      exact ⟨8 * C * T + C + T, by lib_nat⟩

theorem oddf_dv : ∀ B k, k < B → Dv (2 * k + 1) (oddf B)
  | 0, _, h => absurd h (Nat.not_succ_le_zero _)
  | B + 1, k, h => by
    show Dv (2 * k + 1) (oddf B * (2 * B + 1))
    cases Nat.lt_or_ge k B with
    | inl hk => exact Inf17mod24.dv_mulr _ _ _ (oddf_dv B k hk)
    | inr hk =>
      have e : k = B := Nat.le_antisymm (Nat.le_of_succ_le_succ h) hk
      rw [e]
      exact ⟨oddf B, by lib_nat⟩

theorem uniq_rem16 (x y r c : Nat) (hr : r < 16) (hc : c < 16) (h : x * 16 + r = y * 16 + c) : r = c := by
  have big : ∀ x y r c : Nat, r < 16 → x < y → x * 16 + r = y * 16 + c → False := fun x y r c hr hxy h => by
    have h1 : x * 16 + r < (x + 1) * 16 := by rw [Nat.succ_mul]; exact Nat.add_lt_add_left hr _
    have h2 : (x + 1) * 16 ≤ y * 16 := Nat.mul_le_mul_right 16 hxy
    exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (h ▸ h1) (Nat.le_trans h2 (Nat.le_add_right _ _)))
  cases Nat.lt_or_ge x y with
  | inl hxy => exact absurd h (fun h' => big x y r c hr hxy h')
  | inr hxy =>
    cases Nat.lt_or_ge y x with
    | inl hyx => exact absurd h.symm (fun h' => big y x c r hc hyx h')
    | inr hyx =>
      have e : x = y := Nat.le_antisymm hyx hxy
      subst e
      apply Int.ofNat.inj
      have := congrArg Int.ofNat h
      have e' : Int.ofNat x * 16 + Int.ofNat r = Int.ofNat x * 16 + Int.ofNat c := this
      calc Int.ofNat r = (Int.ofNat x * 16 + Int.ofNat r) - Int.ofNat x * 16 := by lib_int
        _ = Int.ofNat c := by rw [e']; lib_int

theorem mod16n (n : Nat) : ∃ a r, n = a * 16 + r ∧ r < 16 :=
  match div_exists 15 n with
  | ⟨a, r, k, hn, hr⟩ => ⟨a, r, hn, Nat.lt_of_lt_of_le (Nat.lt_succ_self r) (by rw [show (16 : Nat) = 15 + 1 from rfl, ← hr]; exact Nat.succ_le_succ (Nat.le_add_right r k))⟩

theorem cofactor16 (b N' e : Nat) (h : (b * 16 + 1) * N' = e * 16 + 9) : ∃ c, N' = c * 16 + 9 :=
  match mod16n N' with
  | ⟨c, r, hc, hr⟩ => by
    have e' : (b * 16 + 1) * N' = (b * 16 * c + b * r + c) * 16 + r := by rw [hc]; lib_nat
    rw [e'] at h
    exact ⟨c, by rw [hc, uniq_rem16 _ _ r 9 hr (Nat.le_add_left 10 6) h]⟩

/-- **Descent.** If every prime factor of N₀ is ≡ 1 or 9 (mod 16), a divisor N ≡ 9 has a prime factor ≡ 9. -/
theorem has16 (N0 : Nat)
    (hall : ∀ l, (∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1) → Dv (l + 1 + 1) N0 →
      ∃ c t, l + 1 + 1 = c * 16 + t ∧ (t = 1 ∨ t = 9)) :
    ∀ G N, N ≤ G → Dv N N0 → (∃ e, N = e * 16 + 9) →
    ∃ q, (∀ d, Dv d q → d = 1 ∨ d = q) ∧ (∃ c, q = c * 16 + 9) ∧ Dv q N
  | 0, N, hN, _, ⟨e, he⟩ => absurd (he ▸ hN) (fun h => Nat.not_succ_le_zero _ (Nat.le_trans (Nat.le_add_left _ _) h))
  | G + 1, N, hN, hN0, ⟨e, he⟩ => by
    match lp_exists (e * 16 + 7) with
    | ⟨l, hl, hmin⟩ =>
    have hq := lp_prime _ l hl hmin
    have hlN : Dv (l + 1 + 1) N := by rw [he]; exact hl
    match hall l hq (dv_trans _ _ _ hlN hN0) with
    | ⟨c, t, hc, ht⟩ =>
      cases ht with
      | inr h9 => exact ⟨l + 1 + 1, hq, ⟨c, by rw [hc, h9]⟩, hlN⟩
      | inl h1 =>
        match hlN with
        | ⟨N', hN'⟩ =>
        match cofactor16 c N' e (by rw [← he, hN', hc, h1]; lib_nat) with
        | ⟨d, hd⟩ =>
        have hlt : N' ≤ G := by
          have e' : N = N' + (N' * (l + 1)) := by rw [hN']; lib_nat
          have h1' : 1 ≤ N' * (l + 1) := by rw [hd]; exact Nat.mul_le_mul (Nat.le_add_left 1 _) (Nat.le_add_left 1 _)
          exact Nat.le_of_succ_le_succ (Nat.le_trans (Nat.add_le_add_left h1' N') (by rw [← e']; exact hN))
        have hN'0 : Dv N' N0 := dv_trans _ _ _ ⟨l + 1 + 1, by rw [hN']; lib_nat⟩ hN0
        match has16 N0 hall G N' hlt hN'0 ⟨d, hd⟩ with
        | ⟨q, hq', hqa, hqN⟩ => exact ⟨q, hq', hqa, dv_trans q N' N hqN ⟨l + 1 + 1, by rw [hN']; lib_nat⟩⟩

theorem x2g (t C : Nat) : (t * C + 3) * (t * C + 3) = t * (t * C * C + 6 * C) + 9 := by lib_nat
theorem x2 (C : Nat) : (16 * C + 3) * (16 * C + 3) = 16 * (16 * C * C + 6 * C) + 9 := x2g 16 C
theorem x4g (t D : Nat) : (t * D + 9) * (t * D + 9) + 1 = t * (t * (D * D) + 18 * D) + 82 := by lib_nat
theorem x4l (h E D : Nat) : 2 * h * (2 * h * E + 18 * D) + (8 * h + 18) = 2 * ((h * E + 9 * D + 2) * (2 * h) + 9) := by lib_nat
theorem x4 (D : Nat) : (16 * D + 9) * (16 * D + 9) + 1 = 2 * ((8 * (D * D) + 9 * D + 2) * 16 + 9) := by
  rw [x4g 16 D]
  exact x4l 8 (D * D) D

/-- x = 16C + 3 ⟹ x⁴ + 1 = 2M, M = 16K + 9. -/
theorem nM (C : Nat) : ∃ K, (16 * C + 3) ^ 4 + 1 = 2 * (K * 16 + 9) :=
  ⟨8 * ((16 * C * C + 6 * C) * (16 * C * C + 6 * C)) + 9 * (16 * C * C + 6 * C) + 2, by
    show 1 * (16 * C + 3) * (16 * C + 3) * (16 * C + 3) * (16 * C + 3) + 1 = _
    rw [show 1 * (16 * C + 3) * (16 * C + 3) * (16 * C + 3) * (16 * C + 3) = ((16 * C + 3) * (16 * C + 3)) * ((16 * C + 3) * (16 * C + 3)) by
      generalize 16 * C + 3 = y; lib_nat, x2, x4]⟩

theorem odd16 (K r : Nat) (e : K * 16 + 9 = 2 * r) : False :=
  odd_not_dv2 (K * 8 + 4) r (by rw [show 2 * (K * 8 + 4) + 1 = K * 16 + 9 by lib_nat, e]; lib_nat)

theorem keyg (t s : Nat) : (t * s + 1) * (t * s + 1) * (t * s + 1) * (t * s + 1) + 1
    = s * (4 * t + 6 * (t * t) * s + 4 * (t * t * t) * (s * s) + (t * t * t * t) * (s * s * s)) + 2 := by lib_nat

/-- (2s + 1)⁴ + 1 = 2 + O·(…), s = O². -/
theorem key (O : Nat) : (2 * (O * O) + 1) ^ 4 + 1
    = (O * O) * (4 * 2 + 6 * (2 * 2) * (O * O) + 4 * (2 * 2 * 2) * ((O * O) * (O * O)) + (2 * 2 * 2 * 2) * ((O * O) * (O * O) * (O * O))) + 2 := by
  show 1 * (2 * (O * O) + 1) * (2 * (O * O) + 1) * (2 * (O * O) + 1) * (2 * (O * O) + 1) + 1 = _
  rw [show 1 * (2 * (O * O) + 1) * (2 * (O * O) + 1) * (2 * (O * O) + 1) * (2 * (O * O) + 1)
      = (2 * (O * O) + 1) * (2 * (O * O) + 1) * (2 * (O * O) + 1) * (2 * (O * O) + 1) by generalize 2 * (O * O) + 1 = y; lib_nat,
    keyg 2 (O * O)]

theorem par : ∀ k, ∃ j, k = 2 * j ∨ k = 2 * j + 1
  | 0 => ⟨0, Or.inl rfl⟩
  | k + 1 => by
    match par k with
    | ⟨j, Or.inl h⟩ => exact ⟨j, Or.inr (by rw [h])⟩
    | ⟨j, Or.inr h⟩ => exact ⟨j + 1, Or.inl (by rw [h]; lib_nat)⟩

/-- prime factors of M (odd, 2M = x⁴ + 1) are ≡ 1 or 9 (mod 16). -/
theorem allowed16 (x M l : Nat) (hM : x ^ 4 + 1 = 2 * M) (hodd : ∀ r, M ≠ 2 * r) (hq : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1)
    (hdv : Dv (l + 1 + 1) M) : ∃ c t, l + 1 + 1 = c * 16 + t ∧ (t = 1 ∨ t = 9) := by
  have hq2 : l + 1 + 1 ≠ 2 := fun e => by
    rw [e] at hdv
    match hdv with
    | ⟨w, hw⟩ => exact hodd w (by rw [hw]; lib_nat)
  have hdv' : Dv (l + 1 + 1) (x ^ 4 + 1) := by
    match hdv with
    | ⟨w, hw⟩ => exact ⟨2 * w, by rw [hM, hw]; lib_nat⟩
  have hxfull : Frobenius.pw (ring (l + 1)) (mk (l + 1) x) ((0 + 1 + 1) ^ (2 + 1)) = (ring (l + 1)).one := by
    show Frobenius.pw (ring (l + 1)) (mk (l + 1) x) 8 = _
    rw [PrimeOneModQ.mk_pow, Inf1mod24.pow8]; exact Inf1mod24.sq_one l _ hdv'
  have hX1 : Frobenius.pw (ring (l + 1)) (mk (l + 1) x) ((0 + 1 + 1) ^ 2) ≠ (ring (l + 1)).one := by
    show Frobenius.pw (ring (l + 1)) (mk (l + 1) x) 4 ≠ _
    rw [PrimeOneModQ.mk_pow]; exact Inf1mod24.ne_one l _ hq2 hdv'
  match CycloPow.dvd_of_pow l hq 0 2 StrongMult1.two_prime _ hxfull hX1 with
  | ⟨k, hk⟩ =>
  have hk8 : l + 1 + 1 = k * 8 + 1 := by rw [hk]
  match par k with
  | ⟨j, hj⟩ =>
    cases hj with
    | inl h => exact ⟨j, 1, by rw [hk8, h]; lib_nat, Or.inl rfl⟩
    | inr h => exact ⟨j, 9, by rw [hk8, h]; lib_nat, Or.inr rfl⟩

/-- **Infinitely many primes p ≡ 9 (mod 16).** -/
theorem inf_9mod16 (B : Nat) : ∃ c, (∀ d, Dv d (c * 16 + 9) → d = 1 ∨ d = c * 16 + 9) ∧ B < c * 16 + 9 := by
  match oddf_sq B with
  | ⟨C, hC⟩ =>
  have hx : 2 * (oddf B * oddf B) + 1 = 16 * C + 3 := by rw [hC]; lib_nat
  match nM C with
  | ⟨K, hK⟩ =>
  match has16 (K * 16 + 9) (fun l hq hdv => allowed16 (16 * C + 3) (K * 16 + 9) l hK (fun r e => odd16 K r e) hq hdv)
      (K * 16 + 9) (K * 16 + 9) (Nat.le_refl _) ⟨1, by lib_nat⟩ ⟨K, rfl⟩ with
  | ⟨q, hq, ⟨c, hc⟩, hqM⟩ =>
    refine ⟨c, by rw [← hc]; exact hq, ?_⟩
    rw [← hc]
    cases Nat.lt_or_ge B q with
    | inl h => exact h
    | inr h =>
      exfalso
      have hk : 8 * c + 4 < B := by
        have h1 : 2 * (8 * c + 4) + 1 ≤ B := by rw [show 2 * (8 * c + 4) + 1 = c * 16 + 9 by lib_nat, ← hc]; exact h
        have h2 : 8 * c + 4 < 2 * (8 * c + 4) + 1 :=
          Nat.lt_succ_of_le (by rw [show 2 * (8 * c + 4) = (8 * c + 4) + (8 * c + 4) by lib_nat]; exact Nat.le_add_left _ _)
        exact Nat.lt_of_lt_of_le h2 h1
      have hqO : Dv q (oddf B) := by rw [hc, show c * 16 + 9 = 2 * (8 * c + 4) + 1 by lib_nat]; exact oddf_dv B _ hk
      -- q | 2M = x⁴ + 1 = O·Y + 2 and q | O·Y, so q | 2
      have hq2M : Dv q ((2 * (oddf B * oddf B) + 1) ^ 4 + 1) := by
        rw [hx, hK]
        match hqM with
        | ⟨w, hw⟩ => exact ⟨2 * w, by rw [hw]; lib_nat⟩
      rw [key] at hq2M
      have hqOY : Dv q ((oddf B * oddf B) * (4 * 2 + 6 * (2 * 2) * (oddf B * oddf B) + 4 * (2 * 2 * 2) * ((oddf B * oddf B) * (oddf B * oddf B)) + (2 * 2 * 2 * 2) * ((oddf B * oddf B) * (oddf B * oddf B) * (oddf B * oddf B)))) :=
        Inf17mod24.dv_mulr _ _ _ (Inf17mod24.dv_mulr _ _ _ hqO)
      have hq' : q = (c * 16 + 8) + 1 := hc
      rw [hq'] at hqOY hq2M
      have h2 := dv_sub (c * 16 + 8) _ 2 hqOY hq2M
      cases StrongMult1.two_prime _ h2 with
      | inl e => exact Nat.noConfusion (Nat.succ.inj e |> fun e' => (show c * 16 + 8 = 0 from e'))
      | inr e => exact absurd (Nat.succ.inj e) (fun e' => Nat.noConfusion (Nat.succ.inj (show c * 16 + 7 + 1 = 0 + 1 from e')))

end Inf9mod16

#print axioms Inf9mod16.odd_sq
#print axioms Inf9mod16.oddf_sq
#print axioms Inf9mod16.oddf_dv
#print axioms Inf9mod16.uniq_rem16
#print axioms Inf9mod16.mod16n
#print axioms Inf9mod16.cofactor16
#print axioms Inf9mod16.has16
#print axioms Inf9mod16.x2g
#print axioms Inf9mod16.x2
#print axioms Inf9mod16.x4g
#print axioms Inf9mod16.x4l
#print axioms Inf9mod16.x4
#print axioms Inf9mod16.nM
#print axioms Inf9mod16.odd16
#print axioms Inf9mod16.keyg
#print axioms Inf9mod16.key
#print axioms Inf9mod16.par
#print axioms Inf9mod16.allowed16
#print axioms Inf9mod16.inf_9mod16

lang/InfCyclo.lean

lean_all/lang/InfCyclo.lean

/-
  lang/InfCyclo — T43, cycle 124: for every prime ℓ, infinitely many primes p ≡ 1 (mod ℓ).

  For a bound B, M = ℓ·B! and N = 1 + M + … + M^(ℓ−1): a prime factor p of N has M of order ℓ in 𝔽_p (M^ℓ ≡ 1, M ≢ 1 since
  p ∤ ℓ), so ℓ | p − 1 (as PrimeOneModQ, base M instead of ℓ), and p > B since p ∤ M.
    inf_one_mod_q    ℓ prime, B ⇒ ∃ prime p > B with ℓ | p − 1
  Dirichlet for the class 1 mod ℓ: infinitely many primes split completely in ℚ(ζ_ℓ), so the Chebotarev section of the
  reciprocity shell exists for the trivial class of every prime cyclotomic field.

  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.PrimeOneModQ
import lang.InfSplit

namespace InfCyclo

open FTAChain ZMod TwoPower Order PrimeOneModQ InfSplit

theorem inf_one_mod_q (t : Nat) (hq : ∀ d, Dv d (t + 1 + 1) → d = 1 ∨ d = t + 1 + 1) (B : Nat) :
    ∃ l, (∀ e, Dv e (l + 1 + 1) → e = 1 ∨ e = l + 1 + 1) ∧ Dv (t + 1 + 1) (l + 1) ∧ B < l + 1 + 1 := by
  match InfSplit.fac_pos B with
  | ⟨f, hf⟩ =>
  have hMq : (t + 1 + 1) * fac B = (t * f + 2 * f + t + 1) + 1 := by rw [hf]; lib_nat
  generalize hMdef : (t + 1 + 1) * fac B = M at hMq
  -- N = gs q q = 1 + q · M, M = gs q (t+1) = M' + 1
  obtain ⟨r, hr⟩ := pow_succ_form (t * f + 2 * f + t + 1) t
  have hM : gs M (t + 1) = (gs M t + r) + 1 := by
    show gs M t + M ^ t = _
    rw [hMq, hr]
    rfl
  have hN : gs M (t + 1 + 1) = 1 + M * ((gs M t + r) + 1) := by
    rw [geomN, hM]
  -- N = i + 2 with i = q (M' ) + t + 1
  have hNi : gs M (t + 1 + 1) = (M * (gs M t + r) + (t * f + 2 * f + t + 1)) + 1 + 1 := by
    rw [hN]
    generalize gs M t + r = G
    rw [hMq]; lib_nat
  obtain ⟨l, hl, hmin⟩ := lp_exists (M * (gs M t + r) + (t * f + 2 * f + t + 1))
  rw [← hNi] at hl hmin
  have hpr := lp_prime _ l hl hmin
  have hbig : B < l + 1 + 1 := by
    cases Nat.lt_or_ge B (l + 1 + 1) with
    | inl h => exact h
    | inr h =>
      exfalso
      match InfSplit.dv_fac B (l + 1 + 1) (Nat.le_add_left 1 _) h with
      | ⟨r', hr'⟩ =>
        have hdN : Dv (l + 1 + 1) (1 + M * ((gs M t + r) + 1)) := by rw [← hN]; exact hl
        have hdqM : Dv (l + 1 + 1) (M * ((gs M t + r) + 1)) := by
          generalize (gs M t + r) + 1 = Y; exact ⟨(t + 1 + 1) * r' * Y, by rw [← hMdef, hr']; lib_nat⟩
        rw [Nat.add_comm 1 (M * ((gs M t + r) + 1))] at hdN
        exact dv_one_absurd l (dv_sub (l + 1) _ 1 hdqM hdN)
  refine ⟨l, hpr, ?_, hbig⟩
  -- in ℤ/p, x = residue of q
  let K := ZMod.ring (l + 1)
  let x := mk (l + 1) M
  have hS : YMChain.sumK K (Frobenius.pw K x) (LinkIntegralZN.rng (t + 1 + 1)) = K.zero := by
    rw [mk_geom]; exact Subtype.ext (dv_md_zero (l + 1) _ hl)
  have hxq : Frobenius.pw K x (t + 1 + 1) = K.one := by
    have g := geom_ring K x (t + 1 + 1)
    rw [hS, DetChain.mul_zero] at g
    have := congrArg (fun z => K.add z K.one) g.symm
    have h' : K.add (K.add (Frobenius.pw K x (t + 1 + 1)) (K.neg K.one)) K.one = K.add K.zero K.one := this
    rw [K.add_assoc, DetChain.add_neg' K K.one, DetChain.add_zero', K.zero_add] at h'
    exact h'
  -- p ≠ q: p | N = 1 + q M and p | q would give p | 1
  have hx1 : x ≠ K.one := fun e => by
    have hS1 : YMChain.sumK K (Frobenius.pw K K.one) (LinkIntegralZN.rng (t + 1 + 1)) = K.zero := by
      rw [← e]; exact hS
    -- Σ_{i<q} 1 = mk q
    have hq1 : YMChain.sumK K (Frobenius.pw K K.one) (LinkIntegralZN.rng (t + 1 + 1)) = mk (l + 1) (t + 1 + 1) := by
      have := mk_geom (l + 1) 1 (t + 1 + 1)
      have one_eq : mk (l + 1) 1 = K.one := rfl
      rw [one_eq] at this
      rw [this]
      exact congrArg (mk (l + 1)) (gs_one (t + 1 + 1))
    rw [hq1] at hS1
    have hdq : Dv (l + 1 + 1) (t + 1 + 1) := md_zero_dv (l + 1) _ (congrArg Subtype.val hS1)
    cases hq (l + 1 + 1) hdq with
    | inl h1 => exact Nat.noConfusion (Nat.succ.inj h1)
    | inr hpq =>
      -- p = q divides N − 1 = q M, and N
      have hdN : Dv (l + 1 + 1) (1 + M * ((gs M t + r) + 1)) := by rw [← hN]; exact hl
      have hdqM : Dv (l + 1 + 1) (M * ((gs M t + r) + 1)) := by
        rw [hpq]; generalize (gs M t + r) + 1 = Y; exact ⟨fac B * Y, by rw [← hMdef]; lib_nat⟩
      rw [Nat.add_comm 1 (M * ((gs M t + r) + 1))] at hdN
      exact dv_one_absurd l (dv_sub (l + 1) _ 1 hdqM hdN)
  -- order of x is q
  obtain ⟨d, hd, hdmin⟩ := ord_exists K x (t + 1) hxq
  have hdq : Dv (d + 1) (t + 1 + 1) := ord_dvd K x d hd hdmin (t + 1 + 1) hxq
  have hd' : d + 1 = t + 1 + 1 := by
    cases hq (d + 1) hdq with
    | inr h => exact h
    | inl h =>
      have h0 : d = 0 := Nat.succ.inj h
      rw [h0] at hd
      exact absurd (by have := hd; show x = K.one; rw [← K.one_mul x]; exact this) hx1
  -- x ≠ 0, so x^(p−1) = 1, and the order divides p − 1
  have hx0 : x ≠ K.zero := fun e => by
    rw [e] at hxq
    have h0 : Frobenius.pw K K.zero (t + 1 + 1) = K.zero := DetChain.mul_zero K _
    rw [h0] at hxq
    have := congrArg Subtype.val hxq
    rw [one_val l] at this
    exact Nat.noConfusion this
  have hp1 := ZMod.unit_pow l hpr x hx0
  have := ord_dvd K x d hd hdmin (l + 1) hp1
  rw [hd'] at this
  exact this

end InfCyclo

#print axioms InfCyclo.inf_one_mod_q

lang/InfCycloPow.lean

lean_all/lang/InfCycloPow.lean

/-
  lang/InfCycloPow — T43, cycle 127: for every prime power ℓ^e, infinitely many primes p ≡ 1 (mod ℓ^e).

    base_order     for M ≥ 1 with ℓ | M and every d ≤ B dividing M: some prime p > B has M of order ℓ in 𝔽_p
                   (a prime factor of 1 + M + … + M^(ℓ−1), as InfCyclo)
    inf_pow        ℓ prime, s, B ⇒ ∃ prime p > B with ℓ^(s+1) | p − 1      (M = (ℓ·B!)^(ℓ^s), y = ℓ·B!: y^(ℓ^s) = M ≠ 1, y^(ℓ^(s+1)) = 1,
                                                                            CycloPow.dvd_of_pow)
  Dirichlet for the trivial class of every prime-power cyclotomic field ℚ(ζ_{ℓ^e}).

  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.InfCyclo
import lang.CycloPow

namespace InfCycloPow

open FTAChain ZMod TwoPower Order PrimeOneModQ InfSplit

theorem base_order (t : Nat) (hq : ∀ d, Dv d (t + 1 + 1) → d = 1 ∨ d = t + 1 + 1) (B M m : Nat) (hMq : M = m + 1)
    (hdvl : Dv (t + 1 + 1) M) (hdvB : ∀ d, 1 ≤ d → d ≤ B → Dv d M) :
    ∃ l, (∀ e, Dv e (l + 1 + 1) → e = 1 ∨ e = l + 1 + 1) ∧ B < l + 1 + 1 ∧
      Frobenius.pw (ZMod.ring (l + 1)) (mk (l + 1) M) (t + 1 + 1) = (ZMod.ring (l + 1)).one ∧
      mk (l + 1) M ≠ (ZMod.ring (l + 1)).one := by
  -- N = gs q q = 1 + q · M, M = gs q (t+1) = M' + 1
  obtain ⟨r, hr⟩ := pow_succ_form m t
  have hM : gs M (t + 1) = (gs M t + r) + 1 := by
    show gs M t + M ^ t = _
    rw [hMq, hr]
    rfl
  have hN : gs M (t + 1 + 1) = 1 + M * ((gs M t + r) + 1) := by
    rw [geomN, hM]
  -- N = i + 2 with i = q (M' ) + t + 1
  have hNi : gs M (t + 1 + 1) = (M * (gs M t + r) + m) + 1 + 1 := by
    rw [hN]
    generalize gs M t + r = G
    rw [hMq]; lib_nat
  obtain ⟨l, hl, hmin⟩ := lp_exists (M * (gs M t + r) + m)
  rw [← hNi] at hl hmin
  have hpr := lp_prime _ l hl hmin
  have hbig : B < l + 1 + 1 := by
    cases Nat.lt_or_ge B (l + 1 + 1) with
    | inl h => exact h
    | inr h =>
      exfalso
      match hdvB (l + 1 + 1) (Nat.le_add_left 1 _) h with
      | ⟨r', hr'⟩ =>
        have hdN : Dv (l + 1 + 1) (1 + M * ((gs M t + r) + 1)) := by rw [← hN]; exact hl
        have hdqM : Dv (l + 1 + 1) (M * ((gs M t + r) + 1)) := by
          generalize (gs M t + r) + 1 = Y; exact ⟨r' * Y, by rw [hr']; lib_nat⟩
        rw [Nat.add_comm 1 (M * ((gs M t + r) + 1))] at hdN
        exact dv_one_absurd l (dv_sub (l + 1) _ 1 hdqM hdN)
  refine ⟨l, hpr, hbig, ?_⟩
  -- in ℤ/p, x = residue of q
  let K := ZMod.ring (l + 1)
  let x := mk (l + 1) M
  have hS : YMChain.sumK K (Frobenius.pw K x) (LinkIntegralZN.rng (t + 1 + 1)) = K.zero := by
    rw [mk_geom]; exact Subtype.ext (dv_md_zero (l + 1) _ hl)
  have hxq : Frobenius.pw K x (t + 1 + 1) = K.one := by
    have g := geom_ring K x (t + 1 + 1)
    rw [hS, DetChain.mul_zero] at g
    have := congrArg (fun z => K.add z K.one) g.symm
    have h' : K.add (K.add (Frobenius.pw K x (t + 1 + 1)) (K.neg K.one)) K.one = K.add K.zero K.one := this
    rw [K.add_assoc, DetChain.add_neg' K K.one, DetChain.add_zero', K.zero_add] at h'
    exact h'
  -- p ≠ q: p | N = 1 + q M and p | q would give p | 1
  have hx1 : x ≠ K.one := fun e => by
    have hS1 : YMChain.sumK K (Frobenius.pw K K.one) (LinkIntegralZN.rng (t + 1 + 1)) = K.zero := by
      rw [← e]; exact hS
    -- Σ_{i<q} 1 = mk q
    have hq1 : YMChain.sumK K (Frobenius.pw K K.one) (LinkIntegralZN.rng (t + 1 + 1)) = mk (l + 1) (t + 1 + 1) := by
      have := mk_geom (l + 1) 1 (t + 1 + 1)
      have one_eq : mk (l + 1) 1 = K.one := rfl
      rw [one_eq] at this
      rw [this]
      exact congrArg (mk (l + 1)) (gs_one (t + 1 + 1))
    rw [hq1] at hS1
    have hdq : Dv (l + 1 + 1) (t + 1 + 1) := md_zero_dv (l + 1) _ (congrArg Subtype.val hS1)
    cases hq (l + 1 + 1) hdq with
    | inl h1 => exact Nat.noConfusion (Nat.succ.inj h1)
    | inr hpq =>
      -- p = q divides N − 1 = q M, and N
      have hdN : Dv (l + 1 + 1) (1 + M * ((gs M t + r) + 1)) := by rw [← hN]; exact hl
      have hdqM : Dv (l + 1 + 1) (M * ((gs M t + r) + 1)) := by
        rw [hpq]; match hdvl with
          | ⟨c, hc⟩ => generalize (gs M t + r) + 1 = Y; exact ⟨c * Y, by rw [hc]; lib_nat⟩
      rw [Nat.add_comm 1 (M * ((gs M t + r) + 1))] at hdN
      exact dv_one_absurd l (dv_sub (l + 1) _ 1 hdqM hdN)
  exact ⟨hxq, hx1⟩

/-- **Infinitely many primes p ≡ 1 (mod ℓ^(s+1)).** -/
theorem inf_pow (t : Nat) (hq : ∀ d, Dv d (t + 1 + 1) → d = 1 ∨ d = t + 1 + 1) (s B : Nat) :
    ∃ l, (∀ e, Dv e (l + 1 + 1) → e = 1 ∨ e = l + 1 + 1) ∧ Dv ((t + 1 + 1) ^ (s + 1)) (l + 1) ∧ B < l + 1 + 1 := by
  match fac_pos B with
  | ⟨f, hf⟩ =>
  have hA : (t + 1 + 1) * fac B = (t * f + 2 * f + t + 1) + 1 := by rw [hf]; lib_nat
  generalize hAdef : (t + 1 + 1) * fac B = A at hA
  match pow_succ_form (t + 1) s with
  | ⟨e, he⟩ =>
  match pow_succ_form (t * f + 2 * f + t + 1) (e + 1) with
  | ⟨m, hm⟩ =>
  have hM : A ^ ((t + 1 + 1) ^ s) = m + 1 := by rw [he, hA]; exact hm
  have hdvA : ∀ d, Dv d A → Dv d (A ^ ((t + 1 + 1) ^ s)) := fun d ⟨c, hc⟩ => by
    rw [he]
    exact ⟨c * A ^ e, by show A ^ e * A = _; rw [hc]; lib_nat⟩
  match base_order t hq B (A ^ ((t + 1 + 1) ^ s)) m hM
      (hdvA _ ⟨fac B, by rw [← hAdef]; lib_nat⟩)
      (fun d h1 h2 => hdvA d (match dv_fac B d h1 h2 with | ⟨c, hc⟩ => ⟨(t + 1 + 1) * c, by rw [← hAdef, hc]; lib_nat⟩)) with
  | ⟨l, hpr, hbig, hxq, hx1⟩ =>
    refine ⟨l, hpr, ?_, hbig⟩
    have hy : Frobenius.pw (ZMod.ring (l + 1)) (mk (l + 1) A) ((t + 1 + 1) ^ s) = mk (l + 1) (A ^ ((t + 1 + 1) ^ s)) := mk_pow _ _ _
    apply CycloPow.dvd_of_pow l hpr t s hq (mk (l + 1) A)
    · show Frobenius.pw (ZMod.ring (l + 1)) (mk (l + 1) A) ((t + 1 + 1) ^ s * (t + 1 + 1)) = _
      rw [← TwoPower.pwpow, hy]; exact hxq
    · rw [hy]; exact hx1

end InfCycloPow

#print axioms InfCycloPow.base_order
#print axioms InfCycloPow.inf_pow

lang/InfInert.lean

lean_all/lang/InfInert.lean

/-
  lang/InfInert — T43, cycle 125: infinitely many primes inert in ℚ(i) (p ≡ 3 mod 4).

    has3        every N ≡ 3 (mod 4) has a prime factor ≡ 3 (mod 4)       (a product of primes ≡ 1 (mod 4) is ≡ 1)
    inf_inert   ∀ B, ∃ prime p = 4k + 3 > B                               (a factor of 4·B! − 1)
  With InfSplit both Frobenius classes of Gal(ℚ(i)/ℚ) contain infinitely many primes: the Chebotarev section of the
  reciprocity shell exists for ℚ(i)/ℚ, and the GL1 reciprocity of ℚ(i) is a bijection on characters mod 4.

  Lean 4 core only, proofs by hand (lib_nat, lib_int), no axioms.
-/
import lang.InfCyclo

namespace InfInert

open FTAChain ZMod LibInt InfSplit

theorem mod4_cases (n : Nat) : (∃ a, n = 4 * a) ∨ (∃ a, n = 4 * a + 1) ∨ (∃ a, n = 4 * a + 2) ∨ (∃ a, n = 4 * a + 3) := by
  cases EveryPrime.nat_even_odd n with
  | inl h => match h with
    | ⟨c, hc⟩ => cases EveryPrime.nat_even_odd c with
      | inl h' => match h' with | ⟨a, ha⟩ => exact Or.inl ⟨a, by rw [hc, ha]; lib_nat⟩
      | inr h' => match h' with | ⟨a, ha⟩ => exact Or.inr (Or.inr (Or.inl ⟨a, by rw [hc, ha]; lib_nat⟩))
  | inr h => match h with
    | ⟨c, hc⟩ => cases EveryPrime.nat_even_odd c with
      | inl h' => match h' with | ⟨a, ha⟩ => exact Or.inr (Or.inl ⟨a, by rw [hc, ha]; lib_nat⟩)
      | inr h' => match h' with | ⟨a, ha⟩ => exact Or.inr (Or.inr (Or.inr ⟨a, by rw [hc, ha]; lib_nat⟩))

/-- 4x + r = 4y + s with r, s < 4 forces r = s; used through Int: 4X = s − r with |s − r| ≤ 3. -/
theorem four_ne (X : Int) (r : Int) (h : 4 * X = r) (h1 : r = 1 ∨ r = 2 ∨ r = 3 ∨ r = -1 ∨ r = -2 ∨ r = -3) : False := by
  have two : ∀ Y : Int, 2 * Y ≠ 1 := fun Y e => GaussInt.two_ne_one Y e
  rcases h1 with e | e | e | e | e | e <;> rw [e] at h
  · exact two (2 * X) (by rw [← h]; lib_int)
  · exact GaussInt.int_odd_ne_even 0 X (FinPrimary.two_cancel _ _ (by rw [show 2 * (2 * X) = 4 * X by lib_int, h]; lib_int)) |> fun x => x
  · exact GaussInt.int_odd_ne_even (2 * X - 2) 0 (by rw [show 2 * (2 * X - 2) + 1 = 4 * X - 3 by lib_int, h]; lib_int)
  · exact GaussInt.int_odd_ne_even (-2 * X - 1) 0 (by rw [show 2 * (-2 * X - 1) + 1 = -(4 * X) - 1 by lib_int, h]; lib_int)
  · exact GaussInt.int_odd_ne_even X 0 (FinPrimary.two_cancel _ _ (by rw [show 2 * (2 * X + 1) = 4 * X + 2 by lib_int, h]; lib_int))
  · exact two (2 * X + 2) (by rw [show 2 * (2 * X + 2) = 4 * X + 4 by lib_int, h]; lib_int)

theorem even_prime (q b : Nat) (hq : ∀ d, Dv d q → d = 1 ∨ d = q) (he : q = 2 * b) : q = 2 := by
  cases hq 2 ⟨b, by rw [he]; lib_nat⟩ with
  | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
  | inr e => exact e.symm

theorem odd_ne_2dv (a r : Nat) (h : 4 * a + 3 = r * 2) : False :=
  odd_not_dv2 (2 * a + 1) r (by rw [← h]; lib_nat)

/-- **Every N ≡ 3 (mod 4) has a prime factor ≡ 3 (mod 4).** -/
theorem has3 : ∀ F N, N ≤ F → (∃ a, N = 4 * a + 3) →
    ∃ q, (∀ d, Dv d q → d = 1 ∨ d = q) ∧ (∃ k, q = 4 * k + 3) ∧ Dv q N
  | 0, N, hN, ⟨a, ha⟩ => absurd (ha ▸ hN) (fun h => Nat.not_succ_le_zero _ (Nat.le_trans (Nat.le_add_left _ _) h))
  | F + 1, N, hN, ⟨a, ha⟩ => by
    match lp_exists (4 * a + 1) with
    | ⟨l, hl, hmin⟩ =>
    have hq := lp_prime _ l hl hmin
    have hlN : Dv (l + 1 + 1) N := by rw [ha]; exact hl
    cases mod4_cases (l + 1 + 1) with
    | inl h =>
      exfalso
      match h with
      | ⟨b, hb⟩ =>
        have h2 := even_prime _ (2 * b) hq (by rw [hb]; lib_nat)
        rw [h2, ha] at hlN
        match hlN with | ⟨r, hr⟩ => exact odd_ne_2dv a r hr
    | inr h =>
      cases h with
      | inr h =>
        cases h with
        | inl h =>
          exfalso
          match h with
          | ⟨b, hb⟩ =>
            have h2 := even_prime _ (2 * b + 1) hq (by rw [hb]; lib_nat)
            rw [h2, ha] at hlN
            match hlN with | ⟨r, hr⟩ => exact odd_ne_2dv a r hr
        | inr h => exact ⟨l + 1 + 1, hq, h, hlN⟩
      | inl h =>
        match h, hlN with
        | ⟨b, hb⟩, ⟨N', hN'⟩ =>
          have hN'3 : ∃ c, N' = 4 * c + 3 := by
            have key : ∀ c r : Nat, N' = 4 * c + r → 4 * (Int.ofNat a - 4 * Int.ofNat b * Int.ofNat c - Int.ofNat c - Int.ofNat r * Int.ofNat b) = Int.ofNat r - 3 := by
              intro c r hc
              have e : Int.ofNat (4 * a + 3) = Int.ofNat ((4 * c + r) * (4 * b + 1)) := by
                rw [← ha, hN', hc, hb]
              have e' : 4 * Int.ofNat a + 3 = (4 * Int.ofNat c + Int.ofNat r) * (4 * Int.ofNat b + 1) := e
              calc 4 * (Int.ofNat a - 4 * Int.ofNat b * Int.ofNat c - Int.ofNat c - Int.ofNat r * Int.ofNat b)
                  = (4 * Int.ofNat a + 3) - 16 * Int.ofNat b * Int.ofNat c - 4 * Int.ofNat c - 4 * Int.ofNat r * Int.ofNat b - 3 := by lib_int
                _ = Int.ofNat r - 3 := by rw [e']; lib_int
            rcases mod4_cases N' with ⟨c, hc⟩ | ⟨c, hc⟩ | ⟨c, hc⟩ | ⟨c, hc⟩
            · exact absurd (key c 0 (by rw [hc]; rfl)) (fun h => four_ne _ _ h (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr rfl))))))
            · exact absurd (key c 1 hc) (fun h => four_ne _ _ h (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl rfl))))))
            · exact absurd (key c 2 hc) (fun h => four_ne _ _ h (Or.inr (Or.inr (Or.inr (Or.inl rfl)))))
            · exact ⟨c, hc⟩
          match hN'3 with
          | ⟨c, hc⟩ =>
          have hlt : N' ≤ F := by
            have e : N = N' + (N' * (l + 1)) := by rw [hN']; lib_nat
            have h1 : 1 ≤ N' * (l + 1) := by rw [hc]; exact Nat.mul_le_mul (Nat.le_add_left 1 _) (Nat.le_add_left 1 _)
            exact Nat.le_of_succ_le_succ (Nat.le_trans (Nat.add_le_add_left h1 N') (by rw [← e]; exact hN))
          match has3 F N' hlt ⟨c, hc⟩ with
          | ⟨q, hq', hq3, hqN⟩ => exact ⟨q, hq', hq3, dv_trans q N' N hqN ⟨l + 1 + 1, by rw [hN']; lib_nat⟩⟩

/-- **Infinitely many primes p ≡ 3 (mod 4).** -/
theorem inf_inert (B : Nat) : ∃ k, (∀ d, Dv d (2 * (2 * k) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * k) + 1 + 1 + 1)
    ∧ B < 2 * (2 * k) + 1 + 1 + 1 := by
  match fac_pos B with
  | ⟨f, hf⟩ =>
  match has3 (4 * f + 3) (4 * f + 3) (Nat.le_refl _) ⟨f, rfl⟩ with
  | ⟨q, hq, ⟨k, hk⟩, hqN⟩ =>
    refine ⟨k, by rw [show 2 * (2 * k) + 1 + 1 + 1 = 4 * k + 3 by lib_nat, ← hk]; exact hq, ?_⟩
    rw [show 2 * (2 * k) + 1 + 1 + 1 = 4 * k + 3 by lib_nat, ← hk]
    cases Nat.lt_or_ge B q with
    | inl h => exact h
    | inr h =>
      exfalso
      have hq2 : q = (4 * k + 2) + 1 := hk
      match dv_fac B q (by rw [hk]; exact Nat.le_add_left 1 _) h with
      | ⟨r, hr⟩ =>
        have h4 : Dv q (4 * f + 3 + 1) := ⟨4 * r, by rw [show 4 * f + 3 + 1 = 4 * (f + 1) by lib_nat, ← hf, hr]; lib_nat⟩
        rw [hq2] at hqN h4
        have := dv_sub (4 * k + 2) _ 1 hqN h4
        exact dv_one_absurd (4 * k + 1) this

end InfInert

#print axioms InfInert.mod4_cases
#print axioms InfInert.four_ne
#print axioms InfInert.even_prime
#print axioms InfInert.odd_ne_2dv
#print axioms InfInert.has3
#print axioms InfInert.inf_inert

lang/InfInert3.lean

lean_all/lang/InfInert3.lean

/-
  lang/InfInert3 — T43, cycle 126: infinitely many primes inert in ℚ(√−3) (p ≡ 2 mod 3).

    has2        every N ≡ 2 (mod 3) has a prime factor ≡ 2 (mod 3)
    inf_inert3  ∀ B, ∃ prime p = 3k + 2 > B                                 (a factor of 3·B! − 1)
  With InfCyclo (ℓ = 3) both Frobenius classes of Gal(ℚ(√−3)/ℚ) contain infinitely many primes.

  Lean 4 core only, proofs by hand (lib_nat, lib_int), no axioms.
-/
import lang.InfInert
import lang.EveryPrime3

namespace InfInert3

open FTAChain ZMod LibInt InfSplit

theorem three_ne (X r : Int) (h : 3 * X = r) (h1 : r = 1 ∨ r = 2 ∨ r = -1 ∨ r = -2) : False := by
  rcases h1 with e | e | e | e <;> rw [e] at h
  · exact CubicSuper.three_ne_one X h
  · exact CubicSuper.three_ne_one (1 - X) (by rw [show 3 * (1 - X) = 3 - 3 * X by lib_int, h]; lib_int)
  · exact CubicSuper.three_ne_one (-X) (by rw [show 3 * -X = -(3 * X) by lib_int, h]; lib_int)
  · exact CubicSuper.three_ne_one (X + 1) (by rw [show 3 * (X + 1) = 3 * X + 3 by lib_int, h]; lib_int)

/-- **Every N ≡ 2 (mod 3) has a prime factor ≡ 2 (mod 3).** -/
theorem has2 : ∀ F N, N ≤ F → (∃ a, N = 3 * a + 2) →
    ∃ q, (∀ d, Dv d q → d = 1 ∨ d = q) ∧ (∃ k, q = 3 * k + 2) ∧ Dv q N
  | 0, N, hN, ⟨a, ha⟩ => absurd (ha ▸ hN) (fun h => Nat.not_succ_le_zero _ (Nat.le_trans (Nat.le_add_left _ _) h))
  | F + 1, N, hN, ⟨a, ha⟩ => by
    match lp_exists (3 * a) with
    | ⟨l, hl, hmin⟩ =>
    have hq := lp_prime _ l hl hmin
    have hlN : Dv (l + 1 + 1) N := by rw [ha]; exact hl
    rcases EveryPrime3.nat_mod3 (l + 1 + 1) with ⟨b, hb⟩ | ⟨b, hb⟩ | ⟨b, hb⟩
    · -- q = 3b: q = 3, but 3 ∤ 3a + 2
      exfalso
      have h3 : l + 1 + 1 = 3 := by
        cases hq 3 ⟨b, by rw [hb]; lib_nat⟩ with
        | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
        | inr e => exact e.symm
      rw [h3, ha] at hlN
      match hlN with
      | ⟨r, hr⟩ =>
        exact CubicSuper.three_ne_one (Int.ofNat a + 1 - Int.ofNat r) (by
          have e : Int.ofNat (3 * a + 2) = Int.ofNat (r * 3) := congrArg Int.ofNat hr
          have e' : 3 * Int.ofNat a + 2 = Int.ofNat r * 3 := e
          calc 3 * (Int.ofNat a + 1 - Int.ofNat r) = (3 * Int.ofNat a + 2) - Int.ofNat r * 3 + 1 := by lib_int
            _ = 1 := by rw [e']; lib_int)
    · match hlN with
      | ⟨N', hN'⟩ =>
        have hN'2 : ∃ c, N' = 3 * c + 2 := by
          have key : ∀ c r : Nat, N' = 3 * c + r → 3 * (Int.ofNat a - 3 * Int.ofNat b * Int.ofNat c - Int.ofNat c - Int.ofNat r * Int.ofNat b) = Int.ofNat r - 2 := by
            intro c r hc
            have e : Int.ofNat (3 * a + 2) = Int.ofNat ((3 * c + r) * (3 * b + 1)) := by rw [← ha, hN', hc, hb]
            have e' : 3 * Int.ofNat a + 2 = (3 * Int.ofNat c + Int.ofNat r) * (3 * Int.ofNat b + 1) := e
            calc 3 * (Int.ofNat a - 3 * Int.ofNat b * Int.ofNat c - Int.ofNat c - Int.ofNat r * Int.ofNat b)
                = (3 * Int.ofNat a + 2) - 9 * Int.ofNat b * Int.ofNat c - 3 * Int.ofNat c - 3 * Int.ofNat r * Int.ofNat b - 2 := by lib_int
              _ = Int.ofNat r - 2 := by rw [e']; lib_int
          rcases EveryPrime3.nat_mod3 N' with ⟨c, hc⟩ | ⟨c, hc⟩ | ⟨c, hc⟩
          · exact absurd (key c 0 (by rw [hc]; rfl)) (fun h => three_ne _ _ h (Or.inr (Or.inr (Or.inr rfl))))
          · exact absurd (key c 1 hc) (fun h => three_ne _ _ h (Or.inr (Or.inr (Or.inl rfl))))
          · exact ⟨c, hc⟩
        match hN'2 with
        | ⟨c, hc⟩ =>
        have hlt : N' ≤ F := by
          have e : N = N' + (N' * (l + 1)) := by rw [hN']; lib_nat
          have h1 : 1 ≤ N' * (l + 1) := by rw [hc]; exact Nat.mul_le_mul (Nat.le_add_left 1 _) (Nat.le_add_left 1 _)
          exact Nat.le_of_succ_le_succ (Nat.le_trans (Nat.add_le_add_left h1 N') (by rw [← e]; exact hN))
        match has2 F N' hlt ⟨c, hc⟩ with
        | ⟨q, hq', hq2, hqN⟩ => exact ⟨q, hq', hq2, dv_trans q N' N hqN ⟨l + 1 + 1, by rw [hN']; lib_nat⟩⟩
    · exact ⟨l + 1 + 1, hq, ⟨b, hb⟩, hlN⟩

/-- **Infinitely many primes p ≡ 2 (mod 3).** -/
theorem inf_inert3 (B : Nat) : ∃ k, (∀ d, Dv d (3 * k + 2) → d = 1 ∨ d = 3 * k + 2) ∧ B < 3 * k + 2 := by
  match fac_pos B with
  | ⟨f, hf⟩ =>
  match has2 (3 * f + 2) (3 * f + 2) (Nat.le_refl _) ⟨f, rfl⟩ with
  | ⟨q, hq, ⟨k, hk⟩, hqN⟩ =>
    refine ⟨k, by rw [← hk]; exact hq, ?_⟩
    rw [← hk]
    cases Nat.lt_or_ge B q with
    | inl h => exact h
    | inr h =>
      exfalso
      cases k with
      | zero =>
        -- q = 2 ≤ B: 2 | B!, and 2 | 3·B! − 1 = 3f + 2 means 2 | 3f, fine; use q ∤ 1 directly
        match dv_fac B q (by rw [hk]; exact Nat.le_add_left 1 _) h with
        | ⟨r, hr⟩ =>
          have h3 : Dv q (3 * f + 2 + 1) := ⟨3 * r, by rw [show 3 * f + 2 + 1 = 3 * (f + 1) by lib_nat, ← hf, hr]; lib_nat⟩
          have hq2 : q = 1 + 1 := hk
          rw [hq2] at hqN h3
          exact dv_one_absurd 0 (dv_sub 1 _ 1 hqN h3)
      | succ k' =>
        match dv_fac B q (by rw [hk]; exact Nat.le_add_left 1 _) h with
        | ⟨r, hr⟩ =>
          have h3 : Dv q (3 * f + 2 + 1) := ⟨3 * r, by rw [show 3 * f + 2 + 1 = 3 * (f + 1) by lib_nat, ← hf, hr]; lib_nat⟩
          have hq2 : q = (3 * k' + 3 + 1) + 1 := by rw [hk]; lib_nat
          rw [hq2] at hqN h3
          exact dv_one_absurd (3 * k' + 3) (dv_sub (3 * k' + 3 + 1) _ 1 hqN h3)

end InfInert3

#print axioms InfInert3.three_ne
#print axioms InfInert3.has2
#print axioms InfInert3.inf_inert3

lang/InfMix.lean

lean_all/lang/InfMix.lean

/-
  lang/InfMix — T43, cycle 530: **for every prime ℓ ≡ 3 (mod 4) there are primes p ≡ 1 (mod 4), p ≡ −1 (mod ℓ) beyond every bound**
  (the class a ≡ 1 mod 4, a ≡ −1 mod ℓ of (ℤ/4ℓ)^×; a² ≡ 1).

  O = odd factorial up to B, O = ℓ^v·w (Val.split), t = 2w, m with (m² + 1)^d = −1 (LucNonRes), x = 2 + t(m + i),
  F = |ψ_ℓ(x)|² = N ∈ ℕ (LucModQ.natF). Then N ≡ −1 (mod ℓ) (LucModL.modl); no q | t divides N (LucModQ.modq); a prime r ≡ 3 (mod 4)
  does not divide N (LucMixT.no_r3_t); a prime r ≡ 1 (mod 4) dividing N is ≡ ±1 (mod ℓ) (LucMixT.k2_t). The descent
  ChebPowInf.hasG gives a prime factor ≡ −1 (mod ℓ); it is ≡ 1 (mod 4) and does not divide O, so it exceeds B.
    eq_l, cls_m1, inf_mix
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.LucModQ
import lang.ChebPowInf
import lang.Two2Inf
import lang.PhiPow
import lang.LucMul

namespace Cheb

open FTAChain ZMod LibRing LibInt

/-- A prime dividing ℓ² is ℓ. -/
theorem eq_l (l L : Nat) (hr : ∀ e, Dv e (l + 1 + 1) → e = 1 ∨ e = l + 1 + 1)
    (hL : ∀ e, Dv e (L + 1 + 1) → e = 1 ∨ e = L + 1 + 1) (h : Dv (l + 1 + 1) ((L + 1 + 1) * (L + 1 + 1))) : l + 1 + 1 = L + 1 + 1 := by
  have h' : Dv (l + 1 + 1) (L + 1 + 1) := by
    rcases euclid_core l (L + 1) (L + 1 + 1) hr h with e | e
    · exact e
    · exact e
  rcases hL _ h' with e | e
  · exact absurd (Nat.succ.inj e) (fun h => Nat.noConfusion h)
  · exact e

/-- emb N = −1 in ℤ/ℓ gives N = qℓ + (ℓ − 1). -/
theorem cls_m1 (M N : Nat) (h : emb (ring (M + 1)) N = (ring (M + 1)).neg (ring (M + 1)).one) :
    ∃ q, N = q * (M + 1 + 1) + (M + 1) := by
  obtain ⟨q, ρ, hq, hρ⟩ := Order.divmod (M + 1) N
  have he : emb (ring (M + 1)) N = mk (M + 1) ρ := by
    rw [hq, emb_add, emb_mul, emb_self, ← emb_mk]
    show (ring (M + 1)).add ((ring (M + 1)).mul _ (ring (M + 1)).zero) _ = _
    lib_ring
  rw [he] at h
  have hval : (mk (M + 1) ρ).val = ρ := md_small _ ρ hρ
  have h1 : ρ = M + 1 := by
    rw [← hval, h]
    show md (M + 1) ((M + 1) * md (M + 1) 1) = _
    rw [md_small _ 1 (Nat.succ_le_succ (Nat.le_add_left 1 _)), Nat.mul_one]
    exact md_small _ _ (Nat.lt_succ_self _)
  exact ⟨q, by rw [hq, h1]⟩

theorem inf_mix (c B : Nat) (hl : ∀ e, Dv e (2 * (2 * c) + 1 + 1 + 1) → e = 1 ∨ e = 2 * (2 * c) + 1 + 1 + 1) :
    ∃ p, (∀ e, Dv e p → e = 1 ∨ e = p) ∧ B < p ∧ (∃ k, p = 4 * k + 1) ∧ Dv (2 * (2 * c) + 1 + 1 + 1) (p + 1) := by
  obtain ⟨m, hm⟩ := nonres c hl
  -- O = ℓ^v · w, t = 2w
  obtain ⟨a, ha⟩ := Two2.ofac_odd (B + 1)
  have hO1 : 1 ≤ Two2.ofac (B + 1) := by rw [ha]; exact Nat.le_add_left 1 _
  obtain ⟨v, w, hvw, hw⟩ := Val.split (2 * (2 * c) + 1) _ _ (Nat.le_refl _) hO1
  have hl2 : Dv (2 * (2 * c) + 1 + 1 + 1) 2 → False := fun h => by
    obtain ⟨k, hk⟩ := dv_le _ 1 h
    have e : 2 * (2 * c) + k + 1 + 1 + 1 = 0 + 1 + 1 := (by lib_nat : 2 * (2 * c) + k + 1 + 1 + 1 = 2 * (2 * c) + 1 + 1 + 1 + k).trans hk
    exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj e))
  have hlt : Dv (2 * (2 * c) + 1 + 1 + 1) (2 * w) → False := Val.ndv_mul _ 2 w hl hl2 hw
  generalize ht : 2 * w = t at hlt
  -- N
  obtain ⟨N, hN⟩ := natF (fd (Cyclo4.cyc4 intR) (Int.ofNat (2 + m * t), Int.ofNat t) (2 * c + 1)).1
    (fd (Cyclo4.cyc4 intR) (Int.ofNat (2 + m * t), Int.ofNat t) (2 * c + 1)).2
  have hNl := modl c m t N hl hm hlt hN
  have hlN : Dv (2 * (2 * c) + 1 + 1 + 1) N → False := fun h => by
    rw [emb_dv _ _ h] at hNl
    exact Fp2Sqrt.two_ne (2 * c) (by
      calc (ring (2 * (2 * c) + 1 + 1)).add (ring (2 * (2 * c) + 1 + 1)).one (ring (2 * (2 * c) + 1 + 1)).one
          = (ring (2 * (2 * c) + 1 + 1)).add ((ring (2 * (2 * c) + 1 + 1)).neg ((ring (2 * (2 * c) + 1 + 1)).neg (ring (2 * (2 * c) + 1 + 1)).one))
              ((ring (2 * (2 * c) + 1 + 1)).neg ((ring (2 * (2 * c) + 1 + 1)).neg (ring (2 * (2 * c) + 1 + 1)).one)) := by lib_ring
        _ = (ring (2 * (2 * c) + 1 + 1)).add ((ring (2 * (2 * c) + 1 + 1)).neg (ring (2 * (2 * c) + 1 + 1)).zero) ((ring (2 * (2 * c) + 1 + 1)).neg (ring (2 * (2 * c) + 1 + 1)).zero) := by rw [← hNl]
        _ = _ := by lib_ring)
  -- a prime dividing t and N is ℓ, impossible
  have hqtN : ∀ l, (∀ e, Dv e (l + 1 + 1) → e = 1 ∨ e = l + 1 + 1) → Dv (l + 1 + 1) t → Dv (l + 1 + 1) N → False := fun l hr h1 h2 => by
    have hq := modq (l + 1) m t (2 * c + 1) N h1 hN
    rw [emb_dv _ _ h2] at hq
    have h3 := dv_of_emb (l + 1) _ hq.symm
    have e := eq_l l (2 * (2 * c) + 1) hr hl (by rw [show 2 * (2 * c + 1) + 1 = 2 * (2 * c) + 1 + 1 + 1 by lib_nat] at h3; exact h3)
    rw [e] at h2; exact hlN h2
  have h2t : Dv 2 t := ⟨w, by rw [← ht]; lib_nat⟩
  have hall : ∀ l, (∀ e, Dv e (l + 1 + 1) → e = 1 ∨ e = l + 1 + 1) → Dv (l + 1 + 1) N →
      (∃ a, l + 1 + 1 = a * (2 * (2 * c) + 3) + 1) ∨ (∃ a, l + 1 + 1 = a * (2 * (2 * c) + 3) + (2 * (2 * c) + 2)) := by
    intro l hr hdv
    rcases EveryPrime.nat_even_odd l with ⟨i, hi⟩ | ⟨m', hm'⟩
    · exfalso
      have h2 : l + 1 + 1 = 2 := by
        cases hr 2 ⟨i + 1, by rw [hi]; lib_nat⟩ with
        | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
        | inr e => exact e.symm
      exact hqtN l hr (by rw [h2]; exact h2t) hdv
    · subst hm'
      have hlr : Dv (2 * (2 * c) + 1 + 1 + 1) (2 * m' + 1 + 1 + 1) → False := fun h => by
        rcases hr _ h with e | e
        · exact Nat.noConfusion (Nat.succ.inj e)
        · rw [← e] at hdv; exact hlN hdv
      have hrt : Dv (2 * m' + 1 + 1 + 1) t → False := fun h => hqtN (2 * m' + 1) hr h hdv
      rcases EveryPrime.nat_even_odd m' with ⟨e, he⟩ | ⟨e, he⟩
      · exfalso
        subst he
        exact no_r3_t e c (2 + m * t) t N hr hl hlr hrt hN hdv
      · rcases k2_t m' (2 + m * t) t (2 * c + 1) N (2 * (2 * c) + 1) hr ⟨e + 1, by rw [he]; lib_nat⟩ hl (by lib_nat) hN hdv with h | h | h
        · obtain ⟨b, hb⟩ := h
          exact Or.inl ⟨b, by rw [hb]⟩
        · obtain ⟨b, hb⟩ := h
          match b, hb with
          | 0, hb => exact absurd (hb.trans (Nat.zero_mul _)) (fun h' => Nat.noConfusion h')
          | b + 1, hb => exact Or.inr ⟨b, add_rc 1 _ _ (hb.trans (by lib_nat))⟩
        · exfalso
          rw [geo_one_emb] at h
          rcases hl _ (dv_of_emb _ _ h) with e | e
          · exact Nat.noConfusion (Nat.succ.inj e)
          · exact hlr ⟨1, by rw [e]; lib_nat⟩
  obtain ⟨q0, hq0⟩ := cls_m1 (2 * (2 * c) + 1) N hNl
  obtain ⟨q, hq, ⟨b, hb⟩, hqN⟩ := hasG (2 * (2 * c)) N hall N N (Nat.le_refl _) (dv_refl _) ⟨q0, by rw [hq0]⟩
  have hq2 : 2 ≤ q := by rw [hb]; exact Nat.le_trans (Nat.le_add_left 2 (2 * (2 * c))) (Nat.le_add_left _ _)
  obtain ⟨q', hq'⟩ := le_dest2 2 q hq2
  rw [← hq'] at hq hqN hb
  have hq'' : ∀ e, Dv e (q' + 1 + 1) → e = 1 ∨ e = q' + 1 + 1 := hq
  have hqt : Dv (q' + 1 + 1) t → False := fun h => hqtN q' hq'' h hqN
  have hql : Dv (2 * (2 * c) + 1 + 1 + 1) (q' + 1 + 1) → False := fun h => by
    rcases hq'' _ h with e | e
    · exact Nat.noConfusion (Nat.succ.inj e)
    · rw [← e] at hqN; exact hlN hqN
  refine ⟨q' + 2, hq, ?_, ?_, ⟨b + 1, by show q' + 1 + 1 + 1 = _; rw [hb]; lib_nat⟩⟩
  · -- q > B
    cases Nat.lt_or_ge B (q' + 2) with
    | inl h => exact h
    | inr h =>
      exfalso
      rcases EveryPrime.nat_even_odd (q' + 2) with ⟨i, hi⟩ | ⟨i, hi⟩
      · rcases hq'' 2 ⟨i, hi.trans (Nat.mul_comm 2 i)⟩ with e | e
        · exact Nat.noConfusion (Nat.succ.inj e)
        · exact hqt (by rw [← e]; exact h2t)
      · have hqO : Dv (q' + 1 + 1) (Two2.ofac (B + 1)) := by
          have hB : 2 * i + 1 ≤ B := by rw [hi] at h; exact h
          have hle : i ≤ 2 * i + 1 := Nat.le_succ_of_le (Nat.le_trans (Nat.le_add_left i i) (Nat.le_of_eq (by lib_nat)))
          show Dv (q' + 2) _
          rw [hi]; exact Two2.dv_ofac (B + 1) i (Nat.succ_le_succ (Nat.le_trans hle hB))
        rw [hvw] at hqO
        have hP : 1 ≤ (2 * (2 * c) + 1 + 1 + 1) ^ v := pow_pos' _ (Nat.le_add_left 1 _) v
        obtain ⟨P, hP'⟩ := le_dest2 1 _ hP
        rw [← hP'] at hqO
        rcases euclid_core q' P w hq'' hqO with e | e
        · rw [hP'] at e
          have e' := PhiSq.dv_pow_prime q' _ hq'' (Nat.le_add_left 1 _) v e
          have e2 := eq_l q' (2 * (2 * c) + 1) hq'' hl (dv_trans _ _ _ e' ⟨2 * (2 * c) + 1 + 1 + 1, rfl⟩)
          rw [e2] at hqN; exact hlN hqN
        · exact hqt (by rw [← ht]; exact dv_mul_left 2 _ _ e)
  · -- q ≡ 1 (mod 4)
    rcases EveryPrime.nat_even_odd q' with ⟨i, hi⟩ | ⟨m', hm'⟩
    · exfalso
      rcases hq'' 2 ⟨i + 1, by rw [hi]; lib_nat⟩ with e | e
      · exact Nat.noConfusion (Nat.succ.inj e)
      · exact hqt (by rw [← e]; exact h2t)
    · subst hm'
      rcases EveryPrime.nat_even_odd m' with ⟨e, he⟩ | ⟨e, he⟩
      · exfalso
        subst he
        exact no_r3_t e c (2 + m * t) t N hq'' hl hql hqt hN hqN
      · refine ⟨e + 1, ?_⟩
        rw [he]
        lib_nat

end Cheb

#print axioms Cheb.eq_l
#print axioms Cheb.cls_m1
#print axioms Cheb.inf_mix

lang/InfSplit.lean

lean_all/lang/InfSplit.lean

/-
  lang/InfSplit — T43, cycle 123: infinitely many primes split in ℚ(i) (p ≡ 1 mod 4).

  For every bound B there is a prime p = 4k + 5 > B: a prime factor q of (2·B!)² + 1 has −1 = (2·B!)² a square mod q, so
  q ≡ 1 (mod 4) (GaussSplit.inert_three_mod_four), q is odd, and q > B (q ∤ B!).
    fac, dv_fac, fac_pos     B! and its divisors
    inf_split                ∀ B, ∃ k, 4k + 5 is prime and B < 4k + 5
  A special case of Dirichlet's theorem — the existence of the Chebotarev section (Recip1/Frobenius shell) for the split
  class of ℚ(i)/ℚ, so `read` is defined on it (the R8-gap of LANGLANDS_MAP for N = 4, split class).

  Lean 4 core only, proofs by hand (lib_nat, lib_int), no axioms.
-/
import lang.EveryPrime

namespace InfSplit

open FTAChain ZMod YMChain LibInt

def fac : Nat → Nat
  | 0 => 1
  | n + 1 => (n + 1) * fac n

theorem fac_pos : ∀ B, ∃ f, fac B = f + 1
  | 0 => ⟨0, rfl⟩
  | B + 1 => match fac_pos B with
    | ⟨f, hf⟩ => ⟨B * f + B + f, by show (B + 1) * fac B = _; rw [hf]; lib_nat⟩

theorem dv_fac : ∀ B d, 1 ≤ d → d ≤ B → Dv d (fac B)
  | 0, _, h1, h2 => absurd (Nat.le_trans h1 h2) (Nat.not_succ_le_zero 0)
  | B + 1, d, h1, h2 => by
    cases Nat.lt_or_ge d (B + 1) with
    | inl h =>
      match dv_fac B d h1 (Nat.le_of_lt_succ h) with
      | ⟨q, hq⟩ => exact ⟨(B + 1) * q, by show (B + 1) * fac B = _; rw [hq]; lib_nat⟩
    | inr h =>
      have e : d = B + 1 := Nat.le_antisymm h2 h
      exact ⟨fac B, by rw [e]; show (B + 1) * fac B = fac B * (B + 1); lib_nat⟩

theorem odd_not_dv2 (x r : Nat) (h : 2 * x + 1 = r * 2) : False :=
  GaussInt.int_odd_ne_even (Int.ofNat x) (Int.ofNat r) (by
    have := congrArg Int.ofNat h
    show 2 * Int.ofNat x + 1 = 2 * Int.ofNat r
    rw [show (2 : Int) * Int.ofNat r = Int.ofNat r * 2 by lib_int]; exact this)

/-- **Infinitely many primes p ≡ 1 (mod 4).** -/
theorem inf_split (B : Nat) : ∃ k, (∀ d, Dv d (2 * (2 * k + 1) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * k + 1) + 1 + 1 + 1)
    ∧ B < 2 * (2 * k + 1) + 1 + 1 + 1 := by
  match fac_pos B with
  | ⟨f, hf⟩ =>
  have hn : (2 * fac B) * (2 * fac B) + 1 = (4 * f * f + 8 * f + 3) + 1 + 1 := by rw [hf]; lib_nat
  match lp_exists (4 * f * f + 8 * f + 3) with
  | ⟨l, hl, hmin⟩ =>
  have hq := lp_prime _ l hl hmin
  have hdiv : Dv (l + 1 + 1) ((2 * fac B) * (2 * fac B) + 1) := by rw [hn]; exact hl
  -- y² = −1 in ℤ/q
  have hy : (ring (l + 1)).mul (mk (l + 1) (2 * fac B)) (mk (l + 1) (2 * fac B)) = (ring (l + 1)).neg (ring (l + 1)).one := by
    apply DetChain.neg_unique
    rw [TwoPower.mk_mul, show (ring (l + 1)).one = mk (l + 1) 1 from rfl, PrimeOneModQ.mk_add]
    exact Subtype.ext (dv_md_zero (l + 1) _ hdiv)
  -- q > B
  have hbig : B < l + 1 + 1 := by
    cases Nat.lt_or_ge B (l + 1 + 1) with
    | inl h => exact h
    | inr h =>
      exfalso
      match dv_fac B (l + 1 + 1) (Nat.le_add_left 1 _) h with
      | ⟨r, hr⟩ =>
        have h0 : mk (l + 1) (2 * fac B) = (ring (l + 1)).zero :=
          Subtype.ext (dv_md_zero (l + 1) _ ⟨2 * r, by rw [hr]; lib_nat⟩)
        rw [h0, DetChain.mul_zero] at hy
        apply PowerSum.one_ne_zero' l
        rw [← DetChain.neg_neg' (ring (l + 1)) (ring (l + 1)).one, ← hy]
        exact (DetChain.neg_unique (ring (l + 1)) _ _ ((ring (l + 1)).zero_add _)).symm
  cases EveryPrime.nat_even_odd l with
  | inl h =>
    exfalso
    match h with
    | ⟨c, hc⟩ =>
      cases hq 2 ⟨c + 1, by rw [hc]; lib_nat⟩ with
      | inl e => exact Nat.noConfusion (Nat.succ.inj e)
      | inr e =>
        have e' : (0 : Nat) + 1 + 1 = 2 * c + 1 + 1 := by rw [← hc]; exact e
        have e0 : 0 = 2 * c := Nat.succ.inj (Nat.succ.inj e')
        have hc0 : c = 0 := by
          cases c with
          | zero => rfl
          | succ c' => exact absurd e0 (fun h => Nat.noConfusion (show 0 = Nat.succ (2 * c' + 1) from h))
        rw [hc, hc0] at hdiv
        match hdiv with
        | ⟨r, hr⟩ => exact odd_not_dv2 (2 * fac B * fac B) r (by rw [← hr]; lib_nat)
  | inr h =>
    match h with
    | ⟨c, hc⟩ =>
      cases EveryPrime.nat_even_odd c with
      | inl h' =>
        exfalso
        match h' with
        | ⟨k, hk⟩ =>
          subst hc; subst hk
          exact GaussSplit.inert_three_mod_four k hq _ hy
      | inr h' =>
        match h' with
        | ⟨k, hk⟩ =>
          subst hc; subst hk
          exact ⟨k, hq, hbig⟩

end InfSplit

#print axioms InfSplit.fac_pos
#print axioms InfSplit.dv_fac
#print axioms InfSplit.odd_not_dv2
#print axioms InfSplit.inf_split

lang/InfSq3.lean

lean_all/lang/InfSq3.lean

/-
  lang/InfSq3 — T43, cycle 534: **p ≡ 3 (mod 4), p ≡ 1 (mod n) beyond every bound, for every odd squarefree n ≥ 3**
  (n = Π of a list of distinct odd primes).

  y = 2·n·O with O = odd factorial up to B + c (c = PhiSq.cs L), so y ≡ 2 (mod 4), all q | y, and y > c. PhiSq4.sqfree4 gives F
  with Inv(n, y, F), y^φ ≤ cF and F ≡ 3 (mod 4); prime factors r of F have ord_r y = n, r ∤ y, so r is odd, > B, and n | r − 1;
  ChebPowInf.hasG (modulus 4) picks one ≡ 3 (mod 4).
    ofac_ge, inf_sq3
  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.PhiSq4
import lang.PhiSqInf

namespace PhiSq

open FTAChain ZMod Cheb TwoPrime PhiInv

theorem ofac_ge : ∀ n, n + 1 ≤ Two2.ofac (n + 1)
  | 0 => Nat.le_refl 1
  | n + 1 => by
    show n + 1 + 1 ≤ Two2.ofac (n + 1) * (2 * (n + 1) + 1)
    have h1 : 1 ≤ Two2.ofac (n + 1) := Nat.le_trans (Nat.le_add_left 1 n) (ofac_ge n)
    exact Nat.le_trans (Nat.le_trans (Nat.le_add_left (n + 1 + 1) (n + 1)) (Nat.le_of_eq (by lib_nat)))
      (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ h1))

theorem inf_sq3 (j : Nat) (L : List Nat) (hG : Good (j :: L)) (h0 : ∃ h, pr (j :: L) = 2 * h + 1) (B : Nat) :
    ∃ p, (∀ k, Dv k p → k = 1 ∨ k = p) ∧ B < p ∧ (∃ k, p = 4 * k + 3) ∧ ∃ q, p = q * pr (j :: L) + 1 := by
  obtain ⟨h, hh⟩ := h0
  obtain ⟨a, ha⟩ := Two2.ofac_odd (B + cs L + 1)
  have hOge := ofac_ge (B + cs L)
  -- y = 2·n·O = 2·(2u + 1)
  have hu : pr (j :: L) * Two2.ofac (B + cs L + 1) = 2 * (2 * h * a + h + a) + 1 := by rw [hh, ha]; lib_nat
  generalize hy : 2 * (pr (j :: L) * Two2.ofac (B + cs L + 1)) = y
  have hy4 : y = 4 * (2 * h * a + h + a) + 2 := by rw [← hy, hu]; lib_nat
  have hA : AllDv (j :: L) y := by
    rw [← hy]; exact allDv_mul 2 _ _ (by rw [Nat.mul_comm]; exact allDv_mul _ _ _ (allDv_pr (j :: L)))
  have hcy : cs L + 2 ≤ y := by
    rw [← hy]
    have h1 : 1 ≤ pr (j :: L) := pr_pos _
    have e1 : cs L + 2 ≤ 2 * (B + cs L + 1) :=
      Nat.le_trans (Nat.le_add_left (cs L + 2) (2 * B + cs L)) (Nat.le_of_eq (by lib_nat))
    exact Nat.le_trans e1 (Nat.mul_le_mul_left 2 (Nat.le_trans hOge
      (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ h1))))
  obtain ⟨y', hy'⟩ := le_dest2 2 y (Nat.le_trans (Nat.le_add_left 2 (cs L)) hcy)
  rw [← hy'] at hA
  obtain ⟨F, I, l, _, hR⟩ := sqfree4 j L hG y' hA
  rw [show y' + 1 + 1 = y from hy'] at I l hR
  obtain ⟨f, hf⟩ := hR.1 _ hy4
  have hy1 : 1 ≤ y := Nat.le_trans (Nat.le_add_left 1 (cs L + 1)) hcy
  have hC : cs L < y ^ ph j L := Nat.le_trans (Nat.le_add_right (cs L + 1) 1) (Nat.le_trans hcy (pow_ge y hy1 _ (ph_pos j L)))
  have hF2 := PhiSize.two_le _ _ _ l hC
  -- F odd
  have hFodd : ∀ r, F = r * 2 → False := fun r hr => InfSplit.odd_not_dv2 (2 * f + 1) r (by rw [← hr, hf]; lib_nat)
  have hall : ∀ l, (∀ e, Dv e (l + 1 + 1) → e = 1 ∨ e = l + 1 + 1) → Dv (l + 1 + 1) F →
      (∃ a, l + 1 + 1 = a * (1 + 3) + 1) ∨ (∃ a, l + 1 + 1 = a * (1 + 3) + (1 + 2)) := by
    intro l hq hdv
    rcases EveryPrime.nat_even_odd l with ⟨i, hi⟩ | ⟨m, hm⟩
    · exfalso
      have h2 : l + 1 + 1 = 2 := by
        cases hq 2 ⟨i + 1, by rw [hi]; lib_nat⟩ with
        | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
        | inr e => exact e.symm
      rw [h2] at hdv; obtain ⟨r, hr⟩ := hdv; exact hFodd r hr
    · rcases EveryPrime.nat_even_odd m with ⟨i, hi⟩ | ⟨i, hi⟩
      · exact Or.inr ⟨i, by rw [hm, hi]; lib_nat⟩
      · exact Or.inl ⟨i + 1, by rw [hm, hi]; lib_nat⟩
  obtain ⟨q, hq, ⟨b, hb⟩, hqF⟩ := hasG 1 F hall F F (Nat.le_refl _) (dv_refl _) ⟨f, by rw [hf]; lib_nat⟩
  have hq2 : 2 ≤ q := by rw [hb]; exact Nat.le_add_left _ _
  obtain ⟨q', hq'⟩ := le_dest2 2 q hq2
  rw [← hq'] at hq hqF hb
  have hq'' : ∀ e, Dv e (q' + 1 + 1) → e = 1 ∨ e = q' + 1 + 1 := hq
  obtain ⟨hO, hg⟩ := (I q' 0 hq'').1 (dv_pow1 _ _ hqF)
  have hn2 : 2 ≤ pr (j :: L) := Nat.le_trans (Nat.le_add_left 2 j) (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm)
    (Nat.mul_le_mul_right (j + 1 + 1) (pr_pos L)))
  obtain ⟨n', hn'⟩ := le_dest2 1 _ (Nat.le_trans (Nat.le_add_left 1 1) hn2)
  have hqy : Dv (q' + 1 + 1) y → False := fun hd => by
    have hg1 : Dv (q' + 1 + 1) (gN y (pr (j :: L))) := dv_base _ 0 _ hg
    rw [← hn', Nat.add_comm n' 1, gN_one] at hg1
    exact dv_one_absurd q' (dv_sub (q' + 1) (y * gN y n') 1 (dv_trans _ _ _ hd ⟨gN y n', Nat.mul_comm _ _⟩)
      (by rw [Nat.add_comm (y * gN y n') 1]; exact hg1))
  refine ⟨q' + 2, hq, ?_, ⟨b, by show q' + 1 + 1 = _; rw [hb]; lib_nat⟩, ?_⟩
  · cases Nat.lt_or_ge B (q' + 2) with
    | inl hlt => exact hlt
    | inr hge =>
      exfalso
      rcases EveryPrime.nat_even_odd (q' + 2) with ⟨i, hi⟩ | ⟨i, hi⟩
      · obtain ⟨r, hr⟩ := dv_trans 2 (q' + 1 + 1) _ ⟨i, hi.trans (Nat.mul_comm 2 i)⟩ hqF
        exact hFodd r hr
      · have hle : i ≤ 2 * i + 1 := Nat.le_succ_of_le (Nat.le_trans (Nat.le_add_left i i) (Nat.le_of_eq (by lib_nat)))
        have hB : 2 * i + 1 ≤ B + cs L := by rw [hi] at hge; exact Nat.le_trans hge (Nat.le_add_right B _)
        have hqO := Two2.dv_ofac (B + cs L + 1) i (Nat.succ_le_succ (Nat.le_trans hle hB))
        apply hqy
        show Dv (q' + 2) y
        rw [hi, ← hy]
        exact dv_trans _ _ _ hqO ⟨2 * pr (j :: L), by lib_nat⟩
  · rcases EveryPrime.nat_even_odd q' with ⟨i, hi⟩ | ⟨m, hm⟩
    · exfalso
      obtain ⟨r, hr⟩ := dv_trans 2 (q' + 1 + 1) _ ⟨i + 1, by rw [hi]; lib_nat⟩ hqF
      exact hFodd r hr
    · subst hm
      have hX0 : emb (ring (2 * m + 1 + 1)) y ≠ (ring (2 * m + 1 + 1)).zero := fun e => hqy (dv_of_emb _ _ e)
      obtain ⟨r, hr⟩ := hO.2 _ (Cyclo2l.ferm1 m hq'' _ hX0)
      exact ⟨r, by show 2 * m + 1 + 1 + 1 = _; rw [hr]⟩

end PhiSq

#print axioms PhiSq.ofac_ge
#print axioms PhiSq.inf_sq3

lang/Inner27w.lean

lean_all/lang/Inner27w.lean

/-
  lang/Inner27w — T47, cycle 826 (27a weight-one factor, the inner direction): **for each (j, k), the (x, y) related to it is
  unique**, and when the exponent condition holds it exists:
    two_cancel     2c = 0 ⇒ c = 0 (Int constructors)
    RE_xy_uniq     RE x y j k ∧ RE x' y' j k ⇒ x = x' ∧ y = y'          (even relation of All27wE)
    RO_xy_uniq     RO x y j k ∧ RO x' y' j k ⇒ x = x' ∧ y = y'          (odd relation of All27wO)
    RE_of_even     k = j + 2e ⇒ RE (1 + 6j + 9e) e j k
    RO_of_odd      k = j + 2e + 1 ⇒ RO (−3 − 3j − 5e) (2e + 1) j k
  (with Bij27w.even_rep / odd_rep the exponent of (j, k) is then the value of the form at (x, y)).
  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.All27wO
import lib.IntRing

namespace All27w

/-- **2c = 0 ⇒ c = 0.** -/
theorem two_cancel (c : Int) (h : 2 * c = 0) : c = 0 := by
  cases c with
  | ofNat m =>
    cases m with
    | zero => rfl
    | succ m =>
      have h' : 2 * (m + 1) = 0 := Int.ofNat.inj (show Int.ofNat (2 * (m + 1)) = Int.ofNat 0 from h)
      have h2 : 2 * m + 1 + 1 = 0 := h'
      nomatch h2
  | negSucc m => exact Int.noConfusion (show Int.negSucc (2 * m + 1) = Int.ofNat 0 from h)

theorem RE_xy_uniq (x y x' y' j k : Int) (h : RE x y j k) (h' : RE x' y' j k) : x = x' ∧ y = y' := by
  have hx : x - x' = 0 := two_cancel _ (by
    calc 2 * (x - x') = 2 * x - 2 * x' := by lib_int
      _ = (2 + 3 * j + 9 * k) - (2 + 3 * j + 9 * k) := by rw [← h.1, ← h'.1]
      _ = 0 := by lib_int)
  have hy : y - y' = 0 := two_cancel _ (by
    calc 2 * (y - y') = 2 * y - 2 * y' := by lib_int
      _ = (k - j) - (k - j) := by rw [← h.2, ← h'.2]
      _ = 0 := by lib_int)
  exact ⟨by calc x = (x - x') + x' := by lib_int
              _ = x' := by rw [hx]; lib_int,
         by calc y = (y - y') + y' := by lib_int
              _ = y' := by rw [hy]; lib_int⟩

theorem RO_xy_uniq (x y x' y' j k : Int) (h : RO x y j k) (h' : RO x' y' j k) : x = x' ∧ y = y' := by
  have hy : y = y' := h.2.symm.trans h'.2
  have hx4 : 4 * (x - x') = 0 := by
    calc 4 * (x - x') = (4 * x + y) - (4 * x' + y') + (y' - y) := by lib_int
      _ = (-(2 + 3 * j + 9 * k)) - (-(2 + 3 * j + 9 * k)) + (y' - y) := by rw [h.1, h'.1]
      _ = y' - y := by lib_int
      _ = 0 := by rw [hy]; lib_int
  have hx : x - x' = 0 := two_cancel _ (two_cancel _ (by
    calc 2 * (2 * (x - x')) = 4 * (x - x') := by lib_int
      _ = 0 := hx4))
  exact ⟨by calc x = (x - x') + x' := by lib_int
              _ = x' := by rw [hx]; lib_int, hy⟩

theorem RE_of_even (j e : Int) : RE (1 + 6 * j + 9 * e) e j (j + 2 * e) :=
  And.intro (by lib_int) (by lib_int)

theorem RO_of_odd (j e : Int) : RO (-3 - 3 * j - 5 * e) (2 * e + 1) j (j + 2 * e + 1) :=
  And.intro (by lib_int) (by lib_int)

/-- the odd-side congruence x + y ≡ 1 (mod 3), y = 2e + 1, gives Bij27w's form x = 3b − 5e. -/
theorem odd_cond (x e t : Int) (h : x + (2 * e + 1) = 3 * t + 1) : x = 3 * (t + e) - 5 * e := by
  calc x = (x + (2 * e + 1)) - 2 * e - 1 := by lib_int
    _ = (3 * t + 1) - 2 * e - 1 := by rw [h]
    _ = 3 * (t + e) - 5 * e := by lib_int

/-- the (x, y) of an even (j, k) has x ≡ 1 (mod 3). -/
theorem even_x (j e : Int) : 1 + 6 * j + 9 * e = 3 * (2 * j + 3 * e) + 1 := by lib_int

/-- the (x, y) of an odd (j, k) has x + y ≡ 1 (mod 3). -/
theorem odd_xy (j e : Int) : (-3 - 3 * j - 5 * e) + (2 * e + 1) = 3 * (-1 - j - e) + 1 := by lib_int

end All27w

#print axioms All27w.two_cancel
#print axioms All27w.RE_xy_uniq
#print axioms All27w.RO_xy_uniq
#print axioms All27w.RE_of_even
#print axioms All27w.RO_of_odd
#print axioms All27w.odd_cond
#print axioms All27w.even_x
#print axioms All27w.odd_xy

lang/IntCancel.lean

lean_all/lang/IntCancel.lean

/-
  lang/IntCancel — T49 (O3') (iii): **cancellation of a nonzero integer** (LibInt.int_noZD), and the
  coefficient form "c·w ≡ const ⇒ w constant" used to divide u ū ≡ 1 (mod N) by p^(2(ℓ−2)).
    int_cancel   c ≠ 0, c x = c y ⇒ x = y
    const_div    c ≠ 0, c·w(b) = s for every b ⇒ w(b) = w(b0) for every b
  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lib.IntRing

namespace IntCancel

theorem int_cancel (c x y : Int) (hc : c ≠ 0) (h : c * x = c * y) : x = y := by
  have h0 : LibInt.intR.mul c (x - y) = LibInt.intR.zero := by
    show c * (x - y) = 0
    calc c * (x - y) = c * x - c * y := by lib_int
      _ = c * y - c * y := by rw [h]
      _ = 0 := by lib_int
  cases LibInt.int_noZD _ _ h0 with
  | inl hz => exact absurd hz hc
  | inr hd =>
    have hd' : x - y = 0 := hd
    calc x = (x - y) + y := by lib_int
      _ = 0 + y := by rw [hd']
      _ = y := by lib_int

theorem const_div {A : Type} (w : A → Int) (c s : Int) (hc : c ≠ 0) (h : ∀ b, c * w b = s) (b b0 : A) :
    w b = w b0 :=
  int_cancel c _ _ hc ((h b).trans (h b0).symm)

end IntCancel

#print axioms IntCancel.int_cancel
#print axioms IntCancel.const_div

lang/IntCast.lean

lean_all/lang/IntCast.lean

/-
  lang/IntCast — T43, cycle 47: the cast ℤ → R into any commutative ring is additive (to compare integer character
  sums with 𝔽_p-valued ones).

  castN, castZ from lib/Ring (castN (n + 1) = 1 + castN n; castZ on ofNat / negSucc).
    castN_add        castN (a + b) = castN a + castN b
    castZ_sNN        castZ (a ⊖ b) = castN a − castN b
    castZ_add        castZ (x + y) = castZ x + castZ y
    castZ_neg_nat    castZ (−n) = −castN n
    castZ_sum        castZ (Σ f) = Σ castZ ∘ f
    castN_mk         in ℤ/p: castN n = mk n
  Next: reduce Σ_x χ(x³ − x) = ±2u (CMModular) mod p and compare with the Hasse invariant (HasseCM): Gauss's congruence
  C((p−1)/2, (p−1)/4) ≡ ±2u (mod p).

  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lib.IntRing
import lang.TwoPower

namespace IntCast

open LibRing LibInt YMChain

variable {R : Type} (K : DetChain.CRing R)

theorem castN_add (a : Nat) : ∀ b, castN K (a + b) = K.add (castN K a) (castN K b)
  | 0 => by
    show castN K a = K.add (castN K a) K.zero
    rw [DetChain.add_zero']
  | b + 1 => by
    show K.add K.one (castN K (a + b)) = K.add (castN K a) (K.add K.one (castN K b))
    rw [castN_add a b]
    lib_ring

theorem castZ_sNN (a b : Nat) : castZ K (Int.subNatNat a b) = K.add (castN K a) (K.neg (castN K b)) := by
  cases Nat.lt_or_ge a b with
  | inr hge =>
    match Nat.le.dest hge with
    | ⟨d, hd⟩ =>
      have e : Int.subNatNat a b = Int.ofNat d := by
        have h1 := sNN_add_k d 0 b
        rw [Nat.zero_add] at h1
        rw [← hd, show b + d = d + b by lib_nat, h1, sNN_zero]
      rw [e, ← hd, castN_add]
      show castN K d = _
      lib_ring
  | inl hlt =>
    match Nat.le.dest hlt with
    | ⟨d, hd⟩ =>
      have e : Int.subNatNat a b = Int.negSucc d := by
        have h2 := sNN_add_k 0 (d + 1) a
        rw [Nat.zero_add] at h2
        rw [← hd, show a + 1 + d = (d + 1) + a by lib_nat, h2]
        rfl
      rw [e, ← hd, show a + 1 + d = a + (d + 1) by lib_nat, castN_add]
      show K.neg (castN K (d + 1)) = _
      lib_ring

theorem castZ_repr (x : Int) : castZ K x = K.add (castN K (pos x)) (K.neg (castN K (ng x))) := by
  conv => lhs; rw [LibInt.repr x]
  exact castZ_sNN K _ _

theorem castZ_add (x y : Int) : castZ K (x + y) = K.add (castZ K x) (castZ K y) := by
  rw [add_pn, castZ_sNN, castZ_repr K x, castZ_repr K y, castN_add, castN_add]
  lib_ring

theorem castZ_neg_nat : ∀ n : Nat, castZ K (-Int.ofNat n) = K.neg (castN K n)
  | 0 => by
    show castZ K 0 = K.neg K.zero
    exact (DetChain.neg_unique K _ _ (K.zero_add K.zero))
  | n + 1 => rfl

theorem castZ_sum {A : Type} (f : A → Int) : ∀ l : List A,
    castZ K (sumK intR f l) = sumK K (fun a => castZ K (f a)) l
  | [] => rfl
  | a :: l => by
    show castZ K (f a + sumK intR f l) = K.add (castZ K (f a)) (sumK K (fun a => castZ K (f a)) l)
    rw [castZ_add, castZ_sum f l]

theorem castN_mk (m : Nat) : ∀ n, castN (ZMod.ring m) n = ZMod.mk m n
  | 0 => rfl
  | n + 1 => by
    show (ZMod.ring m).add (ZMod.ring m).one (castN (ZMod.ring m) n) = ZMod.mk m (n + 1)
    rw [castN_mk m n, (ZMod.ring m).add_comm, TwoPower.mk_add_one]

end IntCast

#print axioms IntCast.castN_add
#print axioms IntCast.castZ_sNN
#print axioms IntCast.castZ_repr
#print axioms IntCast.castZ_add
#print axioms IntCast.castZ_neg_nat
#print axioms IntCast.castZ_sum
#print axioms IntCast.castN_mk

lang/Isog2.lean

lean_all/lang/Isog2.lean

/-
  lang/Isog2 — T48, cycle 599: **2-isogenous curves have the same point count**: for p = 2m + 3 prime, a, b ∈ 𝔽_p, b ≠ 0,
    Σ_x χ(x(x² + ax + b)) = Σ_x χ(x(x² − 2ax + a² − 4b))
  (y² = x(x² + ax + b) and its 2-isogenous curve). Proof: for x ≠ 0, χ(x(x² + ax + b)) = χ(u + a), u = x + b/x;
  #{x : x + b/x = u} = [u = 0] + 1 + χ(u² − 4b) (completing the square, SqCount); Σχ(u + a) = 0; then u ↦ u + a.
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.SqCount

namespace SqCount

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum

variable (m : Nat) (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1)

theorem complL : Complete LibInt.intR (ConicSum.L m) := CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR

/-- Σ_x F(x + c) = Σ_x F(x). -/
theorem shift_sum (F : Zp (2 * m + 1 + 1) → Int) (c : Zp (2 * m + 1 + 1)) :
    sumK LibInt.intR (fun x => F ((K m).add x c)) (L m) = sumK LibInt.intR F (L m) :=
  reindex LibInt.intR _ (complL m) (fun x => (K m).add x c) (fun x => (K m).add x ((K m).neg c))
    (fun x => by show (K m).add ((K m).add x c) ((K m).neg c) = x; lib_ring) (fun y => by show (K m).add ((K m).add y ((K m).neg c)) c = y; lib_ring) F

include hpr in
/-- Σ_x F(2x + c) = Σ_x F(x) (2 is a unit). -/
theorem lin_sum (F : Zp (2 * m + 1 + 1) → Int) (c : Zp (2 * m + 1 + 1)) :
    sumK LibInt.intR (fun x => F ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) x) c)) (L m) = sumK LibInt.intR F (L m) := by
  have h2 : (K m).add (K m).one (K m).one ≠ (K m).zero := GaussSplit.two_ne_zero (2 * m)
  have hi := iv_mul_self m hpr _ h2
  exact reindex LibInt.intR _ (complL m) (fun x => (K m).add ((K m).mul ((K m).add (K m).one (K m).one) x) c)
    (fun y => (K m).mul (iv m ((K m).add (K m).one (K m).one)) ((K m).add y ((K m).neg c)))
    (fun x => by
      show (K m).mul (iv m ((K m).add (K m).one (K m).one)) ((K m).add ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) x) c) ((K m).neg c)) = x
      calc _ = (K m).mul ((K m).mul (iv m ((K m).add (K m).one (K m).one)) ((K m).add (K m).one (K m).one)) x := by lib_ring
        _ = x := by rw [hi]; lib_ring)
    (fun y => by
      show (K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul (iv m ((K m).add (K m).one (K m).one)) ((K m).add y ((K m).neg c)))) c = y
      calc _ = (K m).add ((K m).mul ((K m).mul (iv m ((K m).add (K m).one (K m).one)) ((K m).add (K m).one (K m).one)) ((K m).add y ((K m).neg c))) c := by lib_ring
        _ = y := by rw [hi]; lib_ring) F

include hpr in
/-- #{x : x² − ux + b = 0} = 1 + χ(u² − 4b). -/
theorem quad_count (u b : Zp (2 * m + 1 + 1)) :
    sumK LibInt.intR (fun x => delta LibInt.intR ((K m).add ((K m).add ((K m).mul x x) ((K m).neg ((K m).mul u x))) b) (K m).zero) (L m) =
      1 + chi m ((K m).add ((K m).mul u u) ((K m).neg ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) b))) := by
  have h2 : (K m).add (K m).one (K m).one ≠ (K m).zero := GaussSplit.two_ne_zero (2 * m)
  have h4 : (K m).mul ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one) ≠ (K m).zero := fun e => by
    cases nozd (2 * m + 1) hpr _ _ e with
    | inl h => exact h2 h
    | inr h => exact h2 h
  -- 4(x² − ux + b) = (2x − u)² − (u² − 4b)
  have hpt : ∀ x, delta LibInt.intR ((K m).add ((K m).add ((K m).mul x x) ((K m).neg ((K m).mul u x))) b) (K m).zero =
      delta LibInt.intR ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) x) ((K m).neg u)) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) x) ((K m).neg u)))
        ((K m).add ((K m).mul u u) ((K m).neg ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) b))) := fun x => by
    have key : (K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) x) ((K m).neg u)) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) x) ((K m).neg u)) =
        (K m).add ((K m).mul ((K m).mul ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) ((K m).add ((K m).add ((K m).mul x x) ((K m).neg ((K m).mul u x))) b))
          ((K m).add ((K m).mul u u) ((K m).neg ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) b))) := by lib_ring
    rw [key]
    generalize (K m).add ((K m).add ((K m).mul x x) ((K m).neg ((K m).mul u x))) b = Fx
    generalize (K m).add ((K m).mul u u) ((K m).neg ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) b)) = D
    generalize (K m).mul ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one) = four at h4 ⊢
    unfold delta
    by_cases hq : Fx = (K m).zero
    · rw [ite_pos' _ _ hq, ite_pos' _ _ (by rw [hq]; lib_ring)]
    · rw [ite_neg' _ _ hq, ite_neg' _ _ (fun e => by
        have e' : (K m).mul four Fx = (K m).zero := by
          calc (K m).mul four Fx = (K m).add ((K m).add ((K m).mul four Fx) D) ((K m).neg D) := by lib_ring
            _ = (K m).add D ((K m).neg D) := by rw [e]
            _ = _ := by lib_ring
        cases nozd (2 * m + 1) hpr _ _ e' with
        | inl h => exact h4 h
        | inr h => exact hq h)]
  rw [sumK_ext _ _ _ hpt]
  rw [lin_sum m hpr (fun t => delta LibInt.intR ((K m).mul t t) ((K m).add ((K m).mul u u) ((K m).neg ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) b)))) ((K m).neg u)]
  exact sq_count m hpr _

theorem dpos {G : Type} [DecidableEq G] (y z : G) (h : y = z) : delta LibInt.intR y z = 1 := by unfold delta; rw [ite_pos' _ _ h]; rfl
theorem dneg {G : Type} [DecidableEq G] (y z : G) (h : ¬ y = z) : delta LibInt.intR y z = 0 := by unfold delta; rw [ite_neg' _ _ h]; rfl

def uu (b x : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (K m).add x ((K m).mul b (iv m x))

theorem uu_zero (b : Zp (2 * m + 1 + 1)) : uu m b (K m).zero = (K m).zero := by
  show (K m).add (K m).zero ((K m).mul b (iv m (K m).zero)) = _; rw [iv_zero]; lib_ring

theorem hsub (f g : Zp (2 * m + 1 + 1) → Int) :
    sumK LibInt.intR (fun x => f x - g x) (L m) = sumK LibInt.intR f (L m) - sumK LibInt.intR g (L m) := by
  have h1 := sumK_add LibInt.intR f (fun x => LibInt.intR.neg (g x)) (L m)
  rw [CMInert.sum_neg_fun LibInt.intR g] at h1
  exact h1.trans (by show _ + -_ = _; lib_int)

include hpr in
/-- **2-isogeny**: Σχ(x(x² + ax + b)) = Σχ(x(x² − 2ax + a² − 4b)) for b ≠ 0. -/
theorem isog2 (a b : Zp (2 * m + 1 + 1)) (hb : b ≠ (K m).zero) :
    sumK LibInt.intR (fun x => chi m ((K m).mul x ((K m).add ((K m).add ((K m).mul x x) ((K m).mul a x)) b))) (L m) =
    sumK LibInt.intR (fun x => chi m ((K m).mul x ((K m).add ((K m).add ((K m).mul x x) ((K m).neg ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul a x))))
      ((K m).add ((K m).mul a a) ((K m).neg ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) b)))))) (L m) := by
  have hc := complL m
  have p1 : ∀ x, chi m ((K m).mul x ((K m).add ((K m).add ((K m).mul x x) ((K m).mul a x)) b)) =
      chi m ((K m).add (uu m b x) a) - delta LibInt.intR x (K m).zero * chi m a := fun x => by
    by_cases hx : x = (K m).zero
    · rw [hx, uu_zero, dpos _ _ rfl,
        show (K m).mul (K m).zero ((K m).add ((K m).add ((K m).mul (K m).zero (K m).zero) ((K m).mul a (K m).zero)) b) = (K m).zero by lib_ring,
        show (K m).add (K m).zero a = a by lib_ring, chi_zero]
      lib_int
    · have hi := iv_mul_self m hpr x hx
      have e : (K m).mul x ((K m).add ((K m).add ((K m).mul x x) ((K m).mul a x)) b) = (K m).mul ((K m).mul x x) ((K m).add (uu m b x) a) := by
        show _ = (K m).mul ((K m).mul x x) ((K m).add ((K m).add x ((K m).mul b (iv m x))) a)
        calc _ = (K m).add ((K m).mul ((K m).mul x x) ((K m).add x a)) ((K m).mul (K m).one ((K m).mul b x)) := by lib_ring
          _ = (K m).add ((K m).mul ((K m).mul x x) ((K m).add x a)) ((K m).mul ((K m).mul (iv m x) x) ((K m).mul b x)) := by rw [hi]
          _ = _ := by lib_ring
      rw [e, chi_mul m hpr, chi_sq m hpr x hx, dneg _ _ hx]; lib_int
  have p3 : ∀ v x, delta LibInt.intR v (uu m b x) = delta LibInt.intR x (K m).zero * delta LibInt.intR v (K m).zero +
      delta LibInt.intR ((K m).add ((K m).add ((K m).mul x x) ((K m).neg ((K m).mul v x))) b) (K m).zero := fun v x => by
    by_cases hx : x = (K m).zero
    · rw [hx, uu_zero, dpos _ _ rfl, show (K m).add ((K m).add ((K m).mul (K m).zero (K m).zero) ((K m).neg ((K m).mul v (K m).zero))) b = b by lib_ring,
        dneg _ _ hb]
      lib_int
    · have hi := iv_mul_self m hpr x hx
      have key : (K m).add ((K m).add ((K m).mul x x) ((K m).neg ((K m).mul v x))) b = (K m).mul x ((K m).add (uu m b x) ((K m).neg v)) := by
        show _ = (K m).mul x ((K m).add ((K m).add x ((K m).mul b (iv m x))) ((K m).neg v))
        calc _ = (K m).add ((K m).add ((K m).mul x x) ((K m).neg ((K m).mul v x))) ((K m).mul (K m).one b) := by lib_ring
          _ = (K m).add ((K m).add ((K m).mul x x) ((K m).neg ((K m).mul v x))) ((K m).mul ((K m).mul (iv m x) x) b) := by rw [hi]
          _ = _ := by lib_ring
      rw [key, dneg _ _ hx]
      by_cases hv : v = uu m b x
      · rw [dpos _ _ hv, dpos _ _ (show (K m).mul x ((K m).add (uu m b x) ((K m).neg v)) = (K m).zero by rw [hv]; lib_ring)]; lib_int
      · rw [dneg _ _ hv, dneg ((K m).mul x ((K m).add (uu m b x) ((K m).neg v))) (K m).zero (fun e => by
          cases nozd (2 * m + 1) hpr _ _ e with
          | inl h => exact hx h
          | inr h =>
            apply hv
            calc v = (K m).add (uu m b x) ((K m).neg ((K m).add (uu m b x) ((K m).neg v))) := by lib_ring
              _ = (K m).add (uu m b x) ((K m).neg (K m).zero) := by rw [h]
              _ = _ := by lib_ring)]
        lib_int
  have hA : sumK LibInt.intR (fun x => delta LibInt.intR x (K m).zero * chi m a) (L m) = chi m a := by
    have h := delta_sum LibInt.intR (fun _ => chi m a) (K m).zero (L m)
    rw [hc] at h
    exact h.trans (by show 1 * chi m a = chi m a; lib_int)
  have hT : sumK LibInt.intR (fun x => chi m ((K m).add (uu m b x) a)) (L m) =
      sumK LibInt.intR (fun v => sumK LibInt.intR (fun x => delta LibInt.intR v (uu m b x)) (L m) * chi m ((K m).add v a)) (L m) := by
    have e1 : ∀ x, chi m ((K m).add (uu m b x) a) = sumK LibInt.intR (fun v => delta LibInt.intR v (uu m b x) * chi m ((K m).add v a)) (L m) := fun x => by
      have h := delta_sum LibInt.intR (fun v => chi m ((K m).add v a)) (uu m b x) (L m)
      rw [hc] at h
      exact (h.trans (by show 1 * chi m ((K m).add (uu m b x) a) = chi m ((K m).add (uu m b x) a); lib_int)).symm
    rw [sumK_ext _ _ _ e1, sumK_swap]
    exact sumK_ext _ _ _ (fun v => (sumK_mul LibInt.intR _ _ _).symm) _
  have hN : ∀ v, sumK LibInt.intR (fun x => delta LibInt.intR v (uu m b x)) (L m) = delta LibInt.intR v (K m).zero + (1 + chi m ((K m).add ((K m).mul v v) ((K m).neg ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) b)))) := fun v => by
    rw [sumK_ext _ _ _ (p3 v)]
    show sumK LibInt.intR (fun x => LibInt.intR.add (LibInt.intR.mul (delta LibInt.intR x (K m).zero) (delta LibInt.intR v (K m).zero))
      (delta LibInt.intR ((K m).add ((K m).add ((K m).mul x x) ((K m).neg ((K m).mul v x))) b) (K m).zero)) (L m) = _
    rw [sumK_add, ← sumK_mul, hc, quad_count m hpr v b]
    show 1 * delta LibInt.intR v (K m).zero + _ = _; lib_int
  have hS0 : sumK LibInt.intR (fun v => chi m ((K m).add v a)) (L m) = 0 := by
    rw [shift_sum m (chi m) a]; exact chi_sum m hpr
  have hDa : sumK LibInt.intR (fun v => delta LibInt.intR v (K m).zero * chi m ((K m).add v a)) (L m) = chi m a := by
    have h := delta_sum LibInt.intR (fun v => chi m ((K m).add v a)) (K m).zero (L m)
    rw [hc] at h
    exact h.trans (by rw [show (K m).add (K m).zero a = a by lib_ring]; show 1 * chi m a = chi m a; lib_int)
  have e2 : ∀ v, sumK LibInt.intR (fun x => delta LibInt.intR v (uu m b x)) (L m) * chi m ((K m).add v a) =
      LibInt.intR.add (LibInt.intR.add (delta LibInt.intR v (K m).zero * chi m ((K m).add v a)) (chi m ((K m).add v a)))
        (chi m ((K m).mul ((K m).add v a) ((K m).add ((K m).mul v v) ((K m).neg ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) b))))) := fun v => by
    rw [hN v, chi_mul m hpr]
    generalize delta LibInt.intR v (K m).zero = d
    generalize chi m ((K m).add ((K m).mul v v) ((K m).neg ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) b))) = c1
    generalize chi m ((K m).add v a) = c2
    show (d + (1 + c1)) * c2 = d * c2 + c2 + c2 * c1; lib_int
  have e3 : ∀ v, chi m ((K m).mul ((K m).add v a) ((K m).add ((K m).add ((K m).mul ((K m).add v a) ((K m).add v a)) ((K m).neg ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul a ((K m).add v a)))))
      ((K m).add ((K m).mul a a) ((K m).neg ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) b))))) = chi m ((K m).mul ((K m).add v a) ((K m).add ((K m).mul v v) ((K m).neg ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) b)))) := fun v => by
    rw [show (K m).add ((K m).add ((K m).mul ((K m).add v a) ((K m).add v a)) ((K m).neg ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul a ((K m).add v a)))))
      ((K m).add ((K m).mul a a) ((K m).neg ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) b))) = (K m).add ((K m).mul v v) ((K m).neg ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) b)) by lib_ring]
  rw [sumK_ext _ _ _ p1, hsub, hA, hT, sumK_ext _ _ _ e2, sumK_add, sumK_add, hDa, hS0]
  rw [← shift_sum m (fun x => chi m ((K m).mul x ((K m).add ((K m).add ((K m).mul x x) ((K m).neg ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul a x))))
      ((K m).add ((K m).mul a a) ((K m).neg ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) b)))))) a, sumK_ext _ _ _ e3]
  show chi m a + 0 + _ - chi m a = _; lib_int

end SqCount

#print axioms SqCount.complL
#print axioms SqCount.shift_sum
#print axioms SqCount.lin_sum
#print axioms SqCount.quad_count
#print axioms SqCount.dpos
#print axioms SqCount.hsub
#print axioms SqCount.isog2

lang/Jac1.lean

lean_all/lang/Jac1.lean

/-
  lang/Jac1 — T48, cycle 726: **the Jacobsthal family T(c) = Σ_x χ(x⁵ + cx) is χ-equivariant on 4th-power classes** (OPEN 256a-split, J1):
    tw_pt    (ux)⁵ + (c u⁴)(ux) = u⁵ (x⁵ + cx)
    tw_cls   u ≠ 0 ⇒ T(c u⁴) = χ(u) T(c)
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.Genus2

namespace Jac1

open FTAChain ZMod YMChain FiniteRecChain DetChain QChar SqCount

variable (m : Nat)

def T (c : Zp (2 * m + 1 + 1)) : Int :=
  sumK LibInt.intR (fun x => chi m ((K m).add ((K m).mul ((K m).mul ((K m).mul ((K m).mul x x) x) x) x) ((K m).mul c x))) (ConicSum.L m)

theorem tw_pt (u c x : Zp (2 * m + 1 + 1)) :
    (K m).add ((K m).mul ((K m).mul ((K m).mul ((K m).mul ((K m).mul u x) ((K m).mul u x)) ((K m).mul u x)) ((K m).mul u x)) ((K m).mul u x))
        ((K m).mul ((K m).mul c ((K m).mul ((K m).mul u u) ((K m).mul u u))) ((K m).mul u x))
      = (K m).mul ((K m).mul ((K m).mul u u) ((K m).mul u u)) ((K m).mul u ((K m).add ((K m).mul ((K m).mul ((K m).mul ((K m).mul x x) x) x) x) ((K m).mul c x))) := by
  lib_ring

/-- **T(c u⁴) = χ(u) T(c).** -/
theorem tw_cls (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (u c : Zp (2 * m + 1 + 1)) (hu : u ≠ (K m).zero) :
    T m ((K m).mul c ((K m).mul ((K m).mul u u) ((K m).mul u u))) = chi m u * T m c := by
  have huu : (K m).mul u u ≠ (K m).zero := fun e => by
    rcases G4Gen.mul_eq_zero m hpr u u e with e1 | e1 <;> exact hu e1
  unfold T
  have hsc : ∀ G : Zp (2 * m + 1 + 1) → Int, sumK LibInt.intR (fun x => G ((K m).mul u x)) (ConicSum.L m) = sumK LibInt.intR G (ConicSum.L m) :=
    fun G => CharSumGen.sum_scale LibInt.intR (2 * m) hpr G u hu
  rw [← hsc]
  show _ = LibInt.intR.mul _ _
  rw [mul_sumK]
  exact sumK_ext LibInt.intR _ _ (fun x => by
    show chi m (_) = _
    rw [tw_pt m u c x, Genus2.chi_sq_mul m hpr _ _ huu, chi_mul m hpr]; rfl) _

end Jac1

#print axioms Jac1.T
#print axioms Jac1.tw_pt
#print axioms Jac1.tw_cls

lang/Jac2.lean

lean_all/lang/Jac2.lean

/-
  lang/Jac2 — T48, cycle 727: **the weighted translation moment** (OPEN 256a-split, J2, first half):
    wmoment   Σ_l (Σ_x w(x)χ(l + h(x)))² = −(Σ_x w(x))² + p·Σ_x Σ_y w(x)w(y)[h(x) = h(y)]
  (every weight w : 𝔽_p → ℤ and every h; translation_moment is w = 1). With w = χ, h = x⁴ this is Σ_c T(c)² for T(c) = Σχ(x⁵ + cx).
  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.LegendreMoment
import lang.Jac1

namespace Jac2

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum

variable (m : Nat)

theorem wmoment (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (w : Zp (2 * m + 1 + 1) → Int) (h : Zp (2 * m + 1 + 1) → Zp (2 * m + 1 + 1)) :
    sumK LibInt.intR (fun l => sumK LibInt.intR (fun x => w x * chi m ((ring (2 * m + 1 + 1)).add l (h x))) (L m) * sumK LibInt.intR (fun x => w x * chi m ((ring (2 * m + 1 + 1)).add l (h x))) (L m)) (L m)
      = -(sumK LibInt.intR w (L m) * sumK LibInt.intR w (L m))
        + JacobMoment.P m * sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => w x * w y * delta LibInt.intR (h x) (h y)) (L m)) (L m) := by
  rw [sumK_ext LibInt.intR _ (fun l => sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => w x * w y * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l (h x)) ((ring (2 * m + 1 + 1)).add l (h y)))) (L m)) (L m)) (fun l => by
    show LibInt.intR.mul _ _ = _
    rw [sumK_mul]
    exact sumK_ext LibInt.intR _ _ (fun x => by
      show LibInt.intR.mul _ _ = _
      rw [mul_sumK]
      exact sumK_ext LibInt.intR _ _ (fun y => by
        show (w x * chi m _) * (w y * chi m _) = _
        rw [chi_mul m hpr]; lib_int) _) _)]
  rw [← sumK_swap LibInt.intR (fun x l => sumK LibInt.intR (fun y => w x * w y * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l (h x)) ((ring (2 * m + 1 + 1)).add l (h y)))) (L m))]
  rw [sumK_ext LibInt.intR _ (fun x => sumK LibInt.intR (fun y => LibInt.intR.add (LibInt.intR.mul (-(w x)) (w y)) (LibInt.intR.mul (JacobMoment.P m) (w x * w y * delta LibInt.intR (h x) (h y)))) (L m)) (fun x => by
    rw [← sumK_swap LibInt.intR (fun y l => w x * w y * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l (h x)) ((ring (2 * m + 1 + 1)).add l (h y))))]
    exact sumK_ext LibInt.intR _ _ (fun y => by
      have e := JacobMoment.inner m hpr (h x) (h y)
      rw [show sumK LibInt.intR (fun l => w x * w y * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l (h x)) ((ring (2 * m + 1 + 1)).add l (h y)))) (L m)
          = LibInt.intR.mul (w x * w y) (sumK LibInt.intR (fun l => chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l (h x)) ((ring (2 * m + 1 + 1)).add l (h y)))) (L m)) by
            rw [mul_sumK]; rfl, e]
      show (w x * w y) * (-1 + JacobMoment.P m * _) = -(w x) * w y + JacobMoment.P m * (w x * w y * _)
      rw [show delta JacobMoment.Z (h x) (h y) = delta LibInt.intR (h x) (h y) from rfl]; lib_int) _)]
  rw [sumK_ext LibInt.intR _ _ (fun x => sumK_add LibInt.intR _ _ _), sumK_add]
  rw [sumK_ext LibInt.intR (fun x => sumK LibInt.intR (fun y => LibInt.intR.mul (-(w x)) (w y)) (L m)) _ (fun x => (mul_sumK LibInt.intR _ _ _).symm)]
  rw [sumK_ext LibInt.intR (fun x => sumK LibInt.intR (fun y => LibInt.intR.mul (JacobMoment.P m) (w x * w y * delta LibInt.intR (h x) (h y))) (L m)) _ (fun x => (mul_sumK LibInt.intR _ _ _).symm)]
  rw [← mul_sumK, ← sumK_mul]
  show LibInt.intR.mul (sumK LibInt.intR (fun x => -(w x)) (L m)) _ + _ = _
  rw [show sumK LibInt.intR (fun x => -(w x)) (L m) = LibInt.intR.mul (-1) (sumK LibInt.intR w (L m)) by
    rw [mul_sumK]; exact sumK_ext LibInt.intR _ _ (fun x => by show _ = -1 * w x; lib_int) _]
  show (-1 * _) * _ + _ * _ = _
  lib_int

end Jac2

#print axioms Jac2.wmoment

lang/Jac3.lean

lean_all/lang/Jac3.lean

/-
  lang/Jac3 — T48, cycle 728: **Σ_c T(c)² = 4p(p − 1)** for T(c) = Σχ(x⁵ + cx), when i² = −1 and s² = −i exist in 𝔽_p (J2).
    nz4         x ≠ 0: x, −x, ix, −ix are pairwise distinct (2 ≠ 0, i ≠ ±1)
    four_delta  x ≠ 0: [y⁴ = x⁴] = [y = x] + [y = −x] + [y = ix] + [y = −ix]
    row         x ≠ 0: Σ_y χ(x)χ(y)[x⁴ = y⁴] = 4   (χ(−1) = χ(i²), χ(±i) = χ((si)²), χ(s²))
    jac_moment  Σ_c T(c)² = 4p(p − 1)
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.Jac2

namespace Jac3

open FTAChain ZMod YMChain FiniteRecChain DetChain QChar SqCount ConicSum

variable (m : Nat)

theorem fact4 (x y i : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) :
    (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul y y)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add y x  -- (+194 chars; full line in the Zenodo source)
  have e : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul y y)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x))) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg   -- (+224 chars; full line in the Zenodo source)
      ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i i) (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x))))) := by lib_ring
  rw [e, show ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i i) (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero from hi]; lib_ring

theorem ne_of_mul (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b c x : Zp (2 * m + 1 + 1)) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hc : c ≠ (ring (2 * m + 1 + 1)).zero)
    (h : (ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg b) = (ring (2 * m + 1 + 1)).mul c x) : a ≠ b := fun e => by
  rw [e, show (ring (2 * m + 1 + 1)).add b ((ring (2 * m + 1 + 1)).neg b) = (ring (2 * m + 1 + 1)).zero by lib_ring] at h
  rcases G4Gen.mul_eq_zero m hpr c x h.symm with e1 | e1
  · exact hc e1
  · exact hx e1

theorem dl (a b : Zp (2 * m + 1 + 1)) (h : a = b) : delta LibInt.intR a b = 1 := by unfold delta; rw [ite_pos' _ _ h]; rfl
theorem dz (a b : Zp (2 * m + 1 + 1)) (h : a ≠ b) : delta LibInt.intR a b = 0 := by unfold delta; rw [ite_neg' _ _ h]; rfl

theorem four_delta (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h : Zp (2 * m + 1 + 1)) (hh : (K m).add h h = (K m).one) (i s : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero)
    (x y : Zp (2 * m + 1 + 1)) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) :
    delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul y y)) = delta LibInt.intR y x + delta LibInt.intR y ((ring (2 * m + 1 + 1)).neg x) + delta LibInt.intR y ((ring (2 * m + 1 + 1)).mul i x) + delta LibInt.intR y ((ring (2 * m + 1 + 1)).neg ((ring  -- (+33 chars; full line in the Zenodo source)
  obtain ⟨hi0, _, _, h2⟩ := Genus2.nz m hpr h hh i s hi hs
  have one_nz := PowerSum.one_ne_zero' (2 * m + 1)
  have h1i : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg i) ≠ (ring (2 * m + 1 + 1)).zero := fun e => h2 (by
    have ei : i = (ring (2 * m + 1 + 1)).one := by rw [show i = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg i))) (ring (2 * m + 1 + 1)).one by lib_ring, e]; lib_ring
    rw [show (K m).add (K m).one (K m).one = (K m).add ((K m).add ((K m).mul i i) (K m).one) ((K m).add (K m).one ((K m).neg ((K m).mul i i))) by lib_ring, hi, ei]; lib_ring)
  have h1i' : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one i ≠ (ring (2 * m + 1 + 1)).zero := fun e => h2 (by
    have ei : i = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one := by rw [show i = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one i) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) by lib_ring, e]; lib_ring
    rw [show (K m).add (K m).one (K m).one = (K m).add ((K m).add ((K m).mul i i) (K m).one) ((K m).add (K m).one ((K m).neg ((K m).mul i i))) by lib_ring, hi, ei]; lib_ring)
  have h2i : (ring (2 * m + 1 + 1)).add i i ≠ (ring (2 * m + 1 + 1)).zero := fun e => hi0 (by
    rw [show i = (ring (2 * m + 1 + 1)).mul h ((ring (2 * m + 1 + 1)).add i i) by rw [show (ring (2 * m + 1 + 1)).mul h ((ring (2 * m + 1 + 1)).add i i) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add h h) i by lib_ring, hh]; lib_ring, e]; lib_ring)
  -- the four candidate roots are pairwise distinct
  have d12 : x ≠ (ring (2 * m + 1 + 1)).neg x := ne_of_mul m hpr _ _ _ x hx h2 (by lib_ring)
  have d13 : x ≠ (ring (2 * m + 1 + 1)).mul i x := ne_of_mul m hpr _ _ _ x hx h1i (by lib_ring)
  have d14 : x ≠ (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul i x) := ne_of_mul m hpr _ _ _ x hx h1i' (by lib_ring)
  have d23 : (ring (2 * m + 1 + 1)).neg x ≠ (ring (2 * m + 1 + 1)).mul i x := ne_of_mul m hpr _ _ ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one i)) x hx (fun e => h1i' (by rw [show (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one i = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one i)) by lib_rin  -- (+31 chars; full line in the Zenodo source)
  have d24 : (ring (2 * m + 1 + 1)).neg x ≠ (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul i x) := ne_of_mul m hpr _ _ ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg i))) x hx (fun e => h1i (by rw [show (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg i) = (ring (2 * m + 1 + 1)).neg ((ring (2 *  -- (+146 chars; full line in the Zenodo source)
  have d34 : (ring (2 * m + 1 + 1)).mul i x ≠ (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul i x) := ne_of_mul m hpr _ _ _ x hx h2i (by lib_ring)
  have f4 : ∀ z, ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z)) = ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)) → z = x ∨ z = (ring (2 * m + 1 + 1)).neg x ∨ z = (ring (2 * m + 1 + 1)).mul i x ∨ z = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul i x) := fun z hz => by
    have e0 := fact4 m x z i hi
    rw [hz, show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x))) = (ring (2 * m + 1 + 1)).zero by lib_ring] at e0
    rcases G4Gen.mul_eq_zero m hpr _ _ e0.symm with e1 | e1
    · rcases G4Gen.mul_eq_zero m hpr _ _ e1 with e2 | e2
      · exact Or.inl (by rw [show z = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg x)) x by lib_ring, e2]; lib_ring)
      · exact Or.inr (Or.inl (by rw [show z = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add z x) ((ring (2 * m + 1 + 1)).neg x) by lib_ring, e2]; lib_ring))
    · rcases G4Gen.mul_eq_zero m hpr _ _ e1 with e2 | e2
      · exact Or.inr (Or.inr (Or.inl (by rw [show z = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul i x))) ((ring (2 * m + 1 + 1)).mul i x) by lib_ring, e2]; lib_ring)))
      · exact Or.inr (Or.inr (Or.inr (by rw [show z = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).mul i x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul i x)) by lib_ring, e2]; lib_ring)))
  have q4ix : ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul i x) ((ring (2 * m + 1 + 1)).mul i x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul i x) ((ring (2 * m + 1 + 1)).mul i x))) = ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)) := by
    rw [show ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul i x) ((ring (2 * m + 1 + 1)).mul i x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul i x) ((ring (2 * m + 1 + 1)).mul i x))) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m +   -- (+328 chars; full line in the Zenodo source)
      show ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i i) (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero from hi]; lib_ring
  by_cases hy : ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul y y)) = ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x))
  · rw [dl m _ _ hy.symm]
    rcases f4 y hy with e | e | e | e <;> rw [e]
    · rw [dl m _ _ rfl, dz m _ _ d12, dz m _ _ d13, dz m _ _ d14]; rfl
    · rw [dz m _ _ (fun e' => d12 e'.symm), dl m _ _ rfl, dz m _ _ d23, dz m _ _ d24]; rfl
    · rw [dz m _ _ (fun e' => d13 e'.symm), dz m _ _ (fun e' => d23 e'.symm), dl m _ _ rfl, dz m _ _ d34]; rfl
    · rw [dz m _ _ (fun e' => d14 e'.symm), dz m _ _ (fun e' => d24 e'.symm), dz m _ _ (fun e' => d34 e'.symm), dl m _ _ rfl]; rfl
  · rw [dz m _ _ (fun e => hy e.symm)]
    have n1 : y ≠ x := fun e => hy (by rw [e])
    have n2 : y ≠ (ring (2 * m + 1 + 1)).neg x := fun e => hy (by rw [e]; lib_ring)
    have n3 : y ≠ (ring (2 * m + 1 + 1)).mul i x := fun e => hy (by rw [e]; exact q4ix)
    have n4 : y ≠ (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul i x) := fun e => hy (by rw [e, show ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul i x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul i x))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul i x)) ((ring (2 * m + 1 +   -- (+290 chars; full line in the Zenodo source)
    rw [dz m _ _ n1, dz m _ _ n2, dz m _ _ n3, dz m _ _ n4]; rfl


theorem row (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h : Zp (2 * m + 1 + 1)) (hh : (K m).add h h = (K m).one) (i s : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero)
    (x : Zp (2 * m + 1 + 1)) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) :
    sumK LibInt.intR (fun y => chi m x * chi m y * delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul y y))) (L m) = 4 := by
  obtain ⟨hi0, hs0, hsi, h2⟩ := Genus2.nz m hpr h hh i s hi hs
  have hcomp : Complete LibInt.intR (L m) := CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR
  have pk : ∀ z, sumK LibInt.intR (fun y => LibInt.intR.mul (delta LibInt.intR y z) (chi m x * chi m y)) (L m) = chi m x * chi m z :=
    fun z => pick LibInt.intR _ hcomp (fun y => chi m x * chi m y) z
  rw [sumK_ext LibInt.intR _ (fun y => LibInt.intR.add (LibInt.intR.add (LibInt.intR.add
      (LibInt.intR.mul (delta LibInt.intR y x) (chi m x * chi m y)) (LibInt.intR.mul (delta LibInt.intR y ((ring (2 * m + 1 + 1)).neg x)) (chi m x * chi m y)))
      (LibInt.intR.mul (delta LibInt.intR y ((ring (2 * m + 1 + 1)).mul i x)) (chi m x * chi m y))) (LibInt.intR.mul (delta LibInt.intR y ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul i x))) (chi m x * chi m y))) (fun y => by
    rw [four_delta m hpr h hh i s hi hs x y hx, Genus2.addI, Genus2.addI, Genus2.addI, Genus2.mulI, Genus2.mulI, Genus2.mulI, Genus2.mulI]; lib_int)]
  rw [sumK_add, sumK_add, sumK_add, pk, pk, pk, pk]
  have hxx : chi m x * chi m x = 1 := by rw [← chi_mul m hpr]; exact QChar.chi_sq m hpr x hx
  have hm1 : chi m ((K m).neg (K m).one) = 1 := by
    rw [show (K m).neg (K m).one = (K m).mul i i by rw [show (K m).mul i i = (K m).add ((K m).add ((K m).mul i i) (K m).one) ((K m).neg (K m).one) by lib_ring, hi]; lib_ring]
    exact QChar.chi_sq m hpr i hi0
  have hci : chi m i = 1 := by rw [← Genus2.c5b m i s hi hs]; exact QChar.chi_sq m hpr _ hsi
  have hcni : chi m ((K m).neg i) = 1 := by
    rw [show (K m).neg i = (K m).mul s s by rw [show (K m).mul s s = (K m).add ((K m).add ((K m).mul s s) i) ((K m).neg i) by lib_ring, hs]; lib_ring]
    exact QChar.chi_sq m hpr s hs0
  have v2 : chi m x * chi m ((ring (2 * m + 1 + 1)).neg x) = 1 := by
    rw [show (ring (2 * m + 1 + 1)).neg x = (ring (2 * m + 1 + 1)).mul ((K m).neg (K m).one) x by lib_ring, chi_mul m hpr, hm1]
    show chi m x * (1 * chi m x) = 1; rw [show chi m x * (1 * chi m x) = chi m x * chi m x by lib_int, hxx]
  have v3 : chi m x * chi m ((ring (2 * m + 1 + 1)).mul i x) = 1 := by
    rw [chi_mul m hpr, hci, show chi m x * (1 * chi m x) = chi m x * chi m x by lib_int, hxx]
  have v4 : chi m x * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul i x)) = 1 := by
    rw [show (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul i x) = (ring (2 * m + 1 + 1)).mul ((K m).neg i) x by lib_ring, chi_mul m hpr, hcni,
      show chi m x * (1 * chi m x) = chi m x * chi m x by lib_int, hxx]
  rw [Genus2.addI, Genus2.addI, Genus2.addI, hxx, v2, v3, v4]; rfl

/-- **J2: Σ_c T(c)² = 4p(p − 1).** -/
theorem jac_moment (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h : Zp (2 * m + 1 + 1)) (hh : (K m).add h h = (K m).one) (i s : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero) :
    sumK LibInt.intR (fun c => Jac1.T m c * Jac1.T m c) (L m) = 4 * JacobMoment.P m * (JacobMoment.P m + -1) := by
  have hcomp : Complete LibInt.intR (L m) := CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR
  have hT : ∀ c, Jac1.T m c = sumK LibInt.intR (fun x => chi m x * chi m ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)))) (L m) := fun c =>
    sumK_ext LibInt.intR _ _ (fun x => by
      rw [show ((K m).add ((K m).mul ((K m).mul ((K m).mul ((K m).mul x x) x) x) x) ((K m).mul c x)) = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x))) by lib_ring, chi_mul m hpr]) _
  rw [sumK_ext LibInt.intR _ _ (fun c => by rw [hT c])]
  rw [Jac2.wmoment m hpr (chi m) (fun x => ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)))]
  have hs0 : sumK LibInt.intR (chi m) (L m) = 0 := QChar.chi_sum m hpr
  rw [hs0]
  have hrow : ∀ x, sumK LibInt.intR (fun y => chi m x * chi m y * delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul y y))) (L m) = 4 + -4 * delta LibInt.intR x (ring (2 * m + 1 + 1)).zero := fun x => by
    by_cases hx : x = (ring (2 * m + 1 + 1)).zero
    · rw [hx, dl m _ _ rfl, chi_zero]
      rw [sumK_ext LibInt.intR _ (fun _ => LibInt.intR.zero) (fun y => by show 0 * chi m y * _ = 0; lib_int)]
      rw [zsum0]; rfl
    · rw [row m hpr h hh i s hi hs x hx, dz m _ _ hx]; rfl
  rw [sumK_ext LibInt.intR _ _ hrow, PencilMoment.sum_lin m 4 (-4) (fun x => delta LibInt.intR x (ring (2 * m + 1 + 1)).zero), hcomp]
  show -(0 * 0) + JacobMoment.P m * (4 * JacobMoment.P m + -4 * 1) = _
  lib_int

end Jac3
#print axioms Jac3.fact4
#print axioms Jac3.ne_of_mul
#print axioms Jac3.dl
#print axioms Jac3.dz
#print axioms Jac3.four_delta
#print axioms Jac3.row
#print axioms Jac3.jac_moment

lang/Jac4.lean

lean_all/lang/Jac4.lean

/-
  lang/Jac4 — T48, cycle 729: **the inversion x ↦ λ/x in the Jacobsthal family** (OPEN 256a-split, J3 and J4):
    inv_pt    (λw)⁵ + c(λw) = λc·w⁶·(x⁵ + c′x),  wx = 1, c′ = λ⁴/c
    inv_sub   c, λ ≠ 0:  T(c) = χ(λc)·T(λ⁴/c)
    zero_cls  c = d², χ(d) = −1  ⇒  T(c) = 0                          (J3: squares that are not 4th powers)
    inv_cls   T(c) = χ(c)·T(1/c), so T(c)² = T(1/c)²                  (J4)
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.Jac3

namespace Jac4

open FTAChain ZMod YMChain FiniteRecChain DetChain QChar SqCount ConicSum

variable (m : Nat)

theorem inv_pt (l c ic w x : Zp (2 * m + 1 + 1)) (hw : ((ring (2 * m + 1 + 1)).mul w x) = (ring (2 * m + 1 + 1)).one) (hc : ((ring (2 * m + 1 + 1)).mul c ic) = (ring (2 * m + 1 + 1)).one) :
    ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul l w) ((ring (2 * m + 1 + 1)).mul l w)) ((ring (2 * m + 1 + 1)).mul l w)) ((ring (2 * m + 1 + 1)).mul l w)) ((ring (2 * m + 1 + 1)).mul l w)) ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul l w))) = (ring (2 * m + 1 + 1  -- (+544 chars; full line in the Zenodo source)
  have e : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul l c) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul w w) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul w w) ((ring (2 * m + 1 + 1)).mul w w)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x  -- (+169 chars; full line in the Zenodo source)
      = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul l c) w) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul w x) ((ring (2 * m + 1 + 1)).mul w x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul w x) ((ring (2 * m + 1 + 1)).mul w x))) ((ring (2 * m + 1 + 1)).mu  -- (+8 chars; full line in the Zenodo source)
          ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul l l) ((ring (2 * m + 1 + 1)).mul l l)) ((ring (2 * m + 1 + 1)).mul l ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul w w) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul w w) w)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul w x) ((ring (2 * m + 1 +   -- (+30 chars; full line in the Zenodo source)
  rw [e, hw, hc]; lib_ring

theorem one_mid (A X : Int) : A * 1 * X = A * X := by lib_int

theorem inv_sub (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (c l : Zp (2 * m + 1 + 1)) (hc : c ≠ (ring (2 * m + 1 + 1)).zero) (hl : l ≠ (ring (2 * m + 1 + 1)).zero) :
    Jac1.T m c = chi m ((ring (2 * m + 1 + 1)).mul l c) * Jac1.T m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul l l) ((ring (2 * m + 1 + 1)).mul l l)) (CharSumGen.iv (2 * m) c)) := by
  have hcomp : Complete LibInt.intR (L m) := CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR
  have hic := CharSumGen.iv_mul (2 * m) hpr c hc
  have hcc : (ring (2 * m + 1 + 1)).mul c (CharSumGen.iv (2 * m) c) = (ring (2 * m + 1 + 1)).one := by rw [(ring (2 * m + 1 + 1)).mul_comm]; exact hic
  have hil := CharSumGen.iv_mul (2 * m) hpr l hl
  have sig : ∀ x, (ring (2 * m + 1 + 1)).mul l (CharSumGen.iv (2 * m) ((ring (2 * m + 1 + 1)).mul l (CharSumGen.iv (2 * m) x))) = x := fun x => by
    rw [CharSumGen.iv_mulmul, CharSumGen.iv_iv (2 * m) hpr x,
      show (ring (2 * m + 1 + 1)).mul l ((ring (2 * m + 1 + 1)).mul (CharSumGen.iv (2 * m) l) x) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (CharSumGen.iv (2 * m) l) l) x by lib_ring, hil]; lib_ring
  unfold Jac1.T
  rw [← reindex LibInt.intR _ hcomp (fun x => (ring (2 * m + 1 + 1)).mul l (CharSumGen.iv (2 * m) x)) (fun x => (ring (2 * m + 1 + 1)).mul l (CharSumGen.iv (2 * m) x)) sig sig]
  show _ = LibInt.intR.mul _ _
  rw [mul_sumK]
  refine sumK_ext LibInt.intR _ _ (fun x => ?_) _
  by_cases hx : x = (ring (2 * m + 1 + 1)).zero
  · rw [hx, CharSumGen.iv_zero, show ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul l (ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).mul l (ring (2 * m + 1 + 1)).zero)) ((ring (2 * m + 1 + 1)).mul l (ring (2 * m + 1 + 1)).zero)) ((ring (2 * m + 1 + 1)).mul l (ring (2 * m +  -- (+219 chars; full line in the Zenodo source)
      show ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero) (ring (2 * m + 1 + 1)).zero) (ring (2 * m + 1 + 1)).zero) (ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1  -- (+177 chars; full line in the Zenodo source)
  · have hw := CharSumGen.iv_mul (2 * m) hpr x hx
    have hw0 : (CharSumGen.iv (2 * m) x) ≠ (ring (2 * m + 1 + 1)).zero := fun e => PowerSum.one_ne_zero' (2 * m + 1) (by rw [← hw, e]; lib_ring)
    generalize (CharSumGen.iv (2 * m) x) = w at hw hw0 ⊢
    generalize (CharSumGen.iv (2 * m) c) = ic at hcc ⊢
    rw [inv_pt m l c ic w x hw hcc, chi_mul m hpr,
      show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul w w) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul w w) ((ring (2 * m + 1 + 1)).mul w w)) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul w w) w) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul w w) w) by lib_ring]
    have hw3 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul w w) w ≠ (ring (2 * m + 1 + 1)).zero := fun e => by
      rcases G4Gen.mul_eq_zero m hpr _ _ e with e1 | e1
      · rcases G4Gen.mul_eq_zero m hpr _ _ e1 with e2 | e2 <;> exact hw0 e2
      · exact hw0 e1
    rw [chi_mul m hpr ((ring (2 * m + 1 + 1)).mul l c), QChar.chi_sq m hpr _ hw3]
    rw [Genus2.mulI]; exact one_mid _ _

/-- **J3: c = d², χ(d) = −1 ⇒ T(c) = 0.** -/
theorem zero_cls (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (d : Zp (2 * m + 1 + 1)) (hd : chi m d = -1) :
    Jac1.T m ((ring (2 * m + 1 + 1)).mul d d) = 0 := by
  have hd0 : d ≠ (ring (2 * m + 1 + 1)).zero := fun e => by rw [e, chi_zero] at hd; exact Int.noConfusion hd
  have hdd : (ring (2 * m + 1 + 1)).mul d d ≠ (ring (2 * m + 1 + 1)).zero := fun e => by
    rcases G4Gen.mul_eq_zero m hpr _ _ e with e1 | e1 <;> exact hd0 e1
  have e := inv_sub m hpr ((ring (2 * m + 1 + 1)).mul d d) d hdd hd0
  have hiv := CharSumGen.iv_mul (2 * m) hpr _ hdd
  have hc' : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul d d) ((ring (2 * m + 1 + 1)).mul d d)) (CharSumGen.iv (2 * m) ((ring (2 * m + 1 + 1)).mul d d)) = (ring (2 * m + 1 + 1)).mul d d := by
    rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul d d) ((ring (2 * m + 1 + 1)).mul d d)) (CharSumGen.iv (2 * m) ((ring (2 * m + 1 + 1)).mul d d)) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (CharSumGen.iv (2 * m) ((ring (2 * m + 1 + 1)).mul d d)) ((ring (2 * m + 1 + 1)).mul d d)) ((ring (2 * m + 1 + 1)).mul d d) by lib_ring, hiv]; lib_ring
  rw [hc', show (ring (2 * m + 1 + 1)).mul d ((ring (2 * m + 1 + 1)).mul d d) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul d d) d by lib_ring, Genus2.chi_sq_mul m hpr d d hd0, hd] at e
  -- T = −T
  have h2 : Jac1.T m ((ring (2 * m + 1 + 1)).mul d d) + Jac1.T m ((ring (2 * m + 1 + 1)).mul d d) = 0 := by
    rw [show Jac1.T m ((ring (2 * m + 1 + 1)).mul d d) + Jac1.T m ((ring (2 * m + 1 + 1)).mul d d) = Jac1.T m ((ring (2 * m + 1 + 1)).mul d d) + -1 * -1 * Jac1.T m ((ring (2 * m + 1 + 1)).mul d d) by lib_int]
    rw [show -1 * -1 * Jac1.T m ((ring (2 * m + 1 + 1)).mul d d) = -1 * (-1 * Jac1.T m ((ring (2 * m + 1 + 1)).mul d d)) by lib_int, ← e]; lib_int
  exact QChar.int_two_zero _ h2

/-- **J4: T(c) = χ(c)·T(1/c).** -/
theorem inv_cls (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (c : Zp (2 * m + 1 + 1)) (hc : c ≠ (ring (2 * m + 1 + 1)).zero) :
    Jac1.T m c = chi m c * Jac1.T m (CharSumGen.iv (2 * m) c) := by
  have e := inv_sub m hpr c (ring (2 * m + 1 + 1)).one hc (PowerSum.one_ne_zero' (2 * m + 1))
  rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) (CharSumGen.iv (2 * m) c) = (CharSumGen.iv (2 * m) c) by lib_ring,
    show (ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one c = c by lib_ring] at e
  exact e

end Jac4

#print axioms Jac4.inv_pt
#print axioms Jac4.one_mid
#print axioms Jac4.inv_sub
#print axioms Jac4.zero_cls
#print axioms Jac4.inv_cls

lang/Jac5.lean

lean_all/lang/Jac5.lean

/-
  lang/Jac5 — T48, cycle 730: **T(c)² on the classes of the Jacobsthal family** (OPEN 256a-split), g a non-square:
    sqrt_chi     χ(a) = 1 ⇒ a = e²
    cls4, clsN   T(e⁴)² = T(1)²;  χ(c) = −1 ⇒ T(c)² = T(g)²            (J1, J4)
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.Jac4

namespace Jac5

open FTAChain ZMod YMChain FiniteRecChain DetChain QChar SqCount ConicSum

variable (m : Nat)

theorem sqrt_chi (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a : Zp (2 * m + 1 + 1)) (ha : chi m a = 1) : ∃ e, (ring (2 * m + 1 + 1)).mul e e = a := by
  have ha0 : a ≠ (ring (2 * m + 1 + 1)).zero := fun e => by rw [e, chi_zero] at ha; exact Nat.noConfusion (Int.ofNat.inj ha)
  by_cases hp : Frobenius.pw (ring (2 * m + 1 + 1)) a (m + 1) = (ring (2 * m + 1 + 1)).one
  · exact QuadDiagonal.sq_root m hpr a ha0 hp
  · rw [chi_neg_of m a ha0 hp] at ha; exact Int.noConfusion ha

theorem nz_mul (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b : Zp (2 * m + 1 + 1)) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) : (ring (2 * m + 1 + 1)).mul a b ≠ (ring (2 * m + 1 + 1)).zero := fun e => by
  rcases G4Gen.mul_eq_zero m hpr _ _ e with e1 | e1
  · exact ha e1
  · exact hb e1

theorem sq_T (u t : Int) (hu : u * u = 1) : (u * t) * (u * t) = t * t := by
  rw [show (u * t) * (u * t) = (u * u) * (t * t) by lib_int, hu]; lib_int

theorem chi_unit (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (e : Zp (2 * m + 1 + 1)) (he : e ≠ (ring (2 * m + 1 + 1)).zero) : chi m e * chi m e = 1 := by
  rw [← chi_mul m hpr]; exact QChar.chi_sq m hpr e he

/-- T(e⁴)² = T(1)². -/
theorem cls4 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (e : Zp (2 * m + 1 + 1)) (he : e ≠ (ring (2 * m + 1 + 1)).zero) : Jac1.T m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul e e) ((ring (2 * m + 1 + 1)).mul e e)) * Jac1.T m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul e e) ((ring (2 * m + 1 + 1)).mul e e)) = Jac1.T m (ring (2 * m + 1 + 1)).one * J  -- (+40 chars; full line in the Zenodo source)
  have h1 := Jac1.tw_cls m hpr e (ring (2 * m + 1 + 1)).one he
  rw [show (ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul e e) ((ring (2 * m + 1 + 1)).mul e e)) = ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul e e) ((ring (2 * m + 1 + 1)).mul e e)) by lib_ring] at h1
  rw [h1]; exact sq_T _ _ (chi_unit m hpr e he)

/-- χ(c) = −1 = χ(g) ⇒ T(c)² = T(g)². -/
theorem clsN (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (c g : Zp (2 * m + 1 + 1)) (hc : chi m c = -1) (hg : chi m g = -1) :
    Jac1.T m c * Jac1.T m c = Jac1.T m g * Jac1.T m g := by
  have hc0 : c ≠ (ring (2 * m + 1 + 1)).zero := fun e => by rw [e, chi_zero] at hc; exact Int.noConfusion hc
  have hg0 : g ≠ (ring (2 * m + 1 + 1)).zero := fun e => by rw [e, chi_zero] at hg; exact Int.noConfusion hg
  have hig := CharSumGen.iv_mul (2 * m) hpr g hg0
  have hig0 : (CharSumGen.iv (2 * m) g) ≠ (ring (2 * m + 1 + 1)).zero := fun e => PowerSum.one_ne_zero' (2 * m + 1) (by rw [← hig, e]; lib_ring)
  have hcig : chi m (CharSumGen.iv (2 * m) g) = -1 := by
    have hone : chi m (ring (2 * m + 1 + 1)).one = 1 := by
      have := QChar.chi_sq m hpr (ring (2 * m + 1 + 1)).one (PowerSum.one_ne_zero' (2 * m + 1))
      rwa [show (ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).one by lib_ring] at this
    have := congrArg (chi m) hig
    rw [chi_mul m hpr, hg, hone] at this
    rw [show chi m (CharSumGen.iv (2 * m) g) = -1 * (chi m (CharSumGen.iv (2 * m) g) * -1) by lib_int, this]; rfl
  obtain ⟨e, he⟩ := sqrt_chi m hpr ((ring (2 * m + 1 + 1)).mul c (CharSumGen.iv (2 * m) g)) (by rw [chi_mul m hpr, hc, hcig]; rfl)
  have he0 : e ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => nz_mul m hpr _ _ hc0 hig0 (by rw [← he, e0]; lib_ring)
  have hceg : c = (ring (2 * m + 1 + 1)).mul g ((ring (2 * m + 1 + 1)).mul e e) := by
    rw [he, show (ring (2 * m + 1 + 1)).mul g ((ring (2 * m + 1 + 1)).mul c (CharSumGen.iv (2 * m) g)) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (CharSumGen.iv (2 * m) g) g) c by lib_ring, hig]; lib_ring
  rcases Euler.euler_pm m hpr e he0 with ep | en
  · -- χ(e) = 1: e = f², c = g f⁴
    obtain ⟨f, hf⟩ := sqrt_chi m hpr e (chi_one_of m e he0 ep)
    have hf0 : f ≠ (ring (2 * m + 1 + 1)).zero := fun f0 => he0 (by rw [← hf, f0]; lib_ring)
    have h1 := Jac1.tw_cls m hpr f g hf0
    rw [show (ring (2 * m + 1 + 1)).mul g ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul f f) ((ring (2 * m + 1 + 1)).mul f f)) = c by rw [hceg, ← hf]] at h1
    rw [h1]; exact sq_T _ _ (chi_unit m hpr f hf0)
  · -- χ(e) = −1: c·g = (ge)², χ(ge) = 1, ge = f², c = f⁴/g
    have hce : chi m e = -1 := chi_neg_of m e he0 (by rw [en]; exact ne_pm m)
    obtain ⟨f, hf⟩ := sqrt_chi m hpr ((ring (2 * m + 1 + 1)).mul g e) (by rw [chi_mul m hpr, hg, hce]; rfl)
    have hf0 : f ≠ (ring (2 * m + 1 + 1)).zero := fun f0 => nz_mul m hpr _ _ hg0 he0 (by rw [← hf, f0]; lib_ring)
    have h1 := Jac1.tw_cls m hpr f (CharSumGen.iv (2 * m) g) hf0
    rw [show (ring (2 * m + 1 + 1)).mul (CharSumGen.iv (2 * m) g) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul f f) ((ring (2 * m + 1 + 1)).mul f f)) = c by
      rw [hf, hceg, show (ring (2 * m + 1 + 1)).mul (CharSumGen.iv (2 * m) g) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul g e) ((ring (2 * m + 1 + 1)).mul g e)) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (CharSumGen.iv (2 * m) g) g) g) ((ring (2 * m + 1 + 1)).mul e e) by lib_ring, hig]; lib_ring] at h1
    rw [h1, sq_T _ _ (chi_unit m hpr f hf0), Jac4.inv_cls m hpr g hg0, sq_T _ _ (chi_unit m hpr g hg0)]

end Jac5

#print axioms Jac5.sqrt_chi
#print axioms Jac5.nz_mul
#print axioms Jac5.sq_T
#print axioms Jac5.chi_unit
#print axioms Jac5.cls4
#print axioms Jac5.clsN

lang/Jac6.lean

lean_all/lang/Jac6.lean

/-
  lang/Jac6 — T48, cycle 731: **the class formula and T(1)² + 2T(g)² = 16p** (OPEN 256a-split): in 𝔽_p with i² = −1, s² = −i,
  g a non-square, N4(c) = #{y : y⁴ = c}:
    n4_zero, n4_four   N4(c) = 0 unless c is a 4th power, N4(e⁴) = 4
    cls_formula        c ≠ 0:  4T(c)² = T(1)²·N4(c) + 2T(g)²(1 − χ(c))
    T_zero             T(0) = 0
    jac_sum            T(1)² + 2T(g)² = 16p        (summing cls_formula against Jac3.jac_moment)
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.Jac5

namespace Jac6

open FTAChain ZMod YMChain FiniteRecChain DetChain QChar SqCount ConicSum

variable (m : Nat)

theorem hm1 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (i : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) : chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = 1 := by
  have hi0 : i ≠ (ring (2 * m + 1 + 1)).zero := fun e => PowerSum.one_ne_zero' (2 * m + 1) (by rw [← hi, e]; lib_ring)
  rw [show (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).mul i i by rw [show (ring (2 * m + 1 + 1)).mul i i = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i i) (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) by lib_ring, hi]; lib_ring]
  exact QChar.chi_sq m hpr i hi0

/-- N4(c) = 0 when c is not a 4th power (every y⁴ = c would make c a 4th power). -/
theorem n4_zero (c : Zp (2 * m + 1 + 1)) (h : ∀ y, ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul y y)) ≠ c) : (sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul y y)) c) (L m)) = 0 := by
  rw [sumK_ext LibInt.intR _ (fun _ => LibInt.intR.zero) (fun y => Jac3.dz m _ _ (h y))]; exact zsum0 _

theorem n4_four (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h : Zp (2 * m + 1 + 1)) (hh : (K m).add h h = (K m).one) (i s : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero)
    (e : Zp (2 * m + 1 + 1)) (he : e ≠ (ring (2 * m + 1 + 1)).zero) : (sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul y y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul e e) ((ring (2 * m + 1 + 1)).mul e e))) (L m)) = 4 := by
  have hcomp : Complete LibInt.intR (L m) := CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR
  rw [sumK_ext LibInt.intR _ (fun y => LibInt.intR.add (LibInt.intR.add (LibInt.intR.add (delta LibInt.intR y e) (delta LibInt.intR y ((ring (2 * m + 1 + 1)).neg e)))
      (delta LibInt.intR y ((ring (2 * m + 1 + 1)).mul i e))) (delta LibInt.intR y ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul i e)))) (fun y => by
    rw [delta_symm, Jac3.four_delta m hpr h hh i s hi hs e y he]; rfl)]
  rw [sumK_add, sumK_add, sumK_add, hcomp, hcomp, hcomp, hcomp]; rfl

theorem cls_formula (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h : Zp (2 * m + 1 + 1)) (hh : (K m).add h h = (K m).one) (i s : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero)
    (g : Zp (2 * m + 1 + 1)) (hg : chi m g = -1) (c : Zp (2 * m + 1 + 1)) (hc : c ≠ (ring (2 * m + 1 + 1)).zero) :
    4 * (Jac1.T m c * Jac1.T m c) = Jac1.T m (ring (2 * m + 1 + 1)).one * Jac1.T m (ring (2 * m + 1 + 1)).one * (sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul y y)) c) (L m)) + 2 * (Jac1.T m g * Jac1.T m g) * (1 + -chi m c) := by
  have m1 := hm1 m hpr i hi
  rcases Euler.euler_pm m hpr c hc with ep | en
  · have hc1 : chi m c = 1 := chi_one_of m c hc ep
    obtain ⟨d, hd⟩ := Jac5.sqrt_chi m hpr c hc1
    have hd0 : d ≠ (ring (2 * m + 1 + 1)).zero := fun d0 => hc (by rw [← hd, d0]; lib_ring)
    rw [hc1]
    rcases Euler.euler_pm m hpr d hd0 with dp | dn
    · obtain ⟨e, he⟩ := Jac5.sqrt_chi m hpr d (chi_one_of m d hd0 dp)
      have he0 : e ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => hd0 (by rw [← he, e0]; lib_ring)
      have hce : c = ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul e e) ((ring (2 * m + 1 + 1)).mul e e)) := by rw [← hd, ← he]
      rw [hce, Jac5.cls4 m hpr e he0, n4_four m hpr h hh i s hi hs e he0]; lib_int
    · have hdm : chi m d = -1 := chi_neg_of m d hd0 (by rw [dn]; exact ne_pm m)
      rw [← hd, Jac4.zero_cls m hpr d hdm]
      rw [n4_zero m _ (fun y ey => by
        rcases SqCount.sq_eq m hpr _ _ ey with e1 | e1
        · have : chi m d = 1 := by
            rw [← e1]
            have hy : y ≠ (ring (2 * m + 1 + 1)).zero := fun y0 => hd0 (by
              have : (ring (2 * m + 1 + 1)).mul d d = (ring (2 * m + 1 + 1)).zero := by rw [← ey, y0]; lib_ring
              rcases G4Gen.mul_eq_zero m hpr _ _ this with e2 | e2 <;> exact e2)
            exact QChar.chi_sq m hpr y hy
          rw [hdm] at this; exact Int.noConfusion this
        · have : chi m d = 1 := by
            have e2 : d = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul y y) := by
              rw [show d = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).neg d) by lib_ring, ← e1]; lib_ring
            have hy : y ≠ (ring (2 * m + 1 + 1)).zero := fun y0 => hd0 (by rw [e2, y0]; lib_ring)
            rw [e2, chi_mul m hpr, m1, QChar.chi_sq m hpr y hy]; rfl
          rw [hdm] at this; exact Int.noConfusion this)]
      lib_int
  · have hcm : chi m c = -1 := chi_neg_of m c hc (by rw [en]; exact ne_pm m)
    rw [hcm, Jac5.clsN m hpr c g hcm hg]
    rw [n4_zero m _ (fun y ey => by
      have hy : y ≠ (ring (2 * m + 1 + 1)).zero := fun y0 => hc (by rw [← ey, y0]; lib_ring)
      have hyy : (ring (2 * m + 1 + 1)).mul y y ≠ (ring (2 * m + 1 + 1)).zero := Jac5.nz_mul m hpr _ _ hy hy
      have := QChar.chi_sq m hpr _ hyy
      rw [ey, hcm] at this; exact Int.noConfusion this)]
    lib_int


theorem sum3 (f1 f2 f3 : Zp (2 * m + 1 + 1) → Int) (a b d : Int) :
    sumK LibInt.intR (fun c => a * f1 c + b * f2 c + d * f3 c) (L m)
      = a * sumK LibInt.intR f1 (L m) + b * sumK LibInt.intR f2 (L m) + d * sumK LibInt.intR f3 (L m) := by
  show sumK LibInt.intR (fun c => LibInt.intR.add (LibInt.intR.add (LibInt.intR.mul a (f1 c)) (LibInt.intR.mul b (f2 c))) (LibInt.intR.mul d (f3 c))) (L m) = _
  rw [sumK_add, sumK_add, ← mul_sumK, ← mul_sumK, ← mul_sumK]; rfl

theorem T_zero (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : Jac1.T m (ring (2 * m + 1 + 1)).zero = 0 := by
  unfold Jac1.T
  rw [sumK_ext LibInt.intR _ (chi m) (fun x => by
    by_cases hx : x = (ring (2 * m + 1 + 1)).zero
    · rw [hx, show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero) (ring (2 * m + 1 + 1)).zero) (ring (2 * m + 1 + 1)).zero) (ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero) = (ring (2 *   -- (+29 chars; full line in the Zenodo source)
    · rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) x) x) x) ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero x) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)) x by lib_ring,
        Genus2.chi_sq_mul m hpr _ _ (Jac5.nz_mul m hpr _ _ hx hx)])]
  exact QChar.chi_sum m hpr

/-- **T(1)² + 2T(g)² = 16p.** -/
theorem jac_sum (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h : Zp (2 * m + 1 + 1)) (hh : (K m).add h h = (K m).one) (i s : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero)
    (g : Zp (2 * m + 1 + 1)) (hg : chi m g = -1) :
    Jac1.T m (ring (2 * m + 1 + 1)).one * Jac1.T m (ring (2 * m + 1 + 1)).one + 2 * (Jac1.T m g * Jac1.T m g) = 16 * JacobMoment.P m := by
  have hcomp : Complete LibInt.intR (L m) := CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR
  generalize hA : Jac1.T m (ring (2 * m + 1 + 1)).one * Jac1.T m (ring (2 * m + 1 + 1)).one = A
  generalize hB : Jac1.T m g * Jac1.T m g = B
  have pt : ∀ c, 4 * (Jac1.T m c * Jac1.T m c) = A * (sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul y y)) c) (L m)) + (2 * B) * (1 + -chi m c) + (-(A + 2 * B)) * delta LibInt.intR c (ring (2 * m + 1 + 1)).zero := fun c => by
    by_cases hc : c = (ring (2 * m + 1 + 1)).zero
    · rw [hc, T_zero m hpr, chi_zero, Jac3.dl m _ _ rfl]
      rw [sumK_ext LibInt.intR _ (fun y => delta LibInt.intR y (ring (2 * m + 1 + 1)).zero) (fun y => by
        by_cases hy : y = (ring (2 * m + 1 + 1)).zero
        · rw [hy, Jac3.dl m _ _ (by lib_ring), Jac3.dl m _ _ rfl]
        · rw [Jac3.dz m _ _ (Jac5.nz_mul m hpr _ _ (Jac5.nz_mul m hpr _ _ hy hy) (Jac5.nz_mul m hpr _ _ hy hy)), Jac3.dz m _ _ hy]), hcomp, Genus2.oneI]
      lib_int
    · rw [Jac6.cls_formula m hpr h hh i s hi hs g hg c hc, hA, hB, Jac3.dz m _ _ hc]; lib_int
  have hsum := congrArg (fun F => F) (sumK_ext LibInt.intR _ _ pt (L m))
  rw [sum3] at hsum
  -- Σ 4T² = 4ΣT²
  have h4 : sumK LibInt.intR (fun c => 4 * (Jac1.T m c * Jac1.T m c)) (L m) = 4 * sumK LibInt.intR (fun c => Jac1.T m c * Jac1.T m c) (L m) := by
    show _ = LibInt.intR.mul _ _
    rw [mul_sumK]; rfl
  rw [h4, Jac3.jac_moment m hpr h hh i s hi hs] at hsum
  have sN : sumK LibInt.intR (fun c => (sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul y y)) c) (L m))) (L m) = JacobMoment.P m := by
    rw [sumK_swap]
    rw [sumK_ext LibInt.intR _ (fun _ => (1 : Int)) (fun y => by
      rw [sumK_ext LibInt.intR _ (fun c => delta LibInt.intR c ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul y y))) (fun c => delta_symm _ _ _), hcomp]; rfl)]
    exact QuadDiagonal.T1 m
  have sX : sumK LibInt.intR (fun c => 1 + -chi m c) (L m) = JacobMoment.P m := by
    rw [sumK_ext LibInt.intR _ (fun c => LibInt.intR.add 1 (LibInt.intR.mul (-1) (chi m c))) (fun c => by rw [Genus2.addI, Genus2.mulI]; lib_int),
      sumK_add, ← mul_sumK, QuadDiagonal.T1 m]
    have this : sumK LibInt.intR (chi m) (L m) = 0 := QChar.chi_sum m hpr
    show Int.ofNat (2 * m + 1 + 1 + 1) + -1 * sumK LibInt.intR (chi m) (L m) = _
    rw [this]; show Int.ofNat (2 * m + 1 + 1 + 1) + -1 * 0 = Int.ofNat (2 * m + 1 + 1 + 1); lib_int
  rw [sN, sX, hcomp, Genus2.oneI] at hsum
  -- (A + 2B)(p − 1) = 16 p (p − 1)
  have hP : JacobMoment.P m = Int.ofNat (2 * m + 1 + 1) + 1 := rfl
  rw [hP] at hsum ⊢
  apply JacobiNorm.int_cancel _ _ (2 * m + 1)
  generalize Int.ofNat (2 * m + 1 + 1) = Q at hsum ⊢
  rw [show (A + 2 * B) * Q = A * (Q + 1) + 2 * B * (Q + 1) + -(A + 2 * B) * 1 by lib_int, ← hsum]
  lib_int

end Jac6
#print axioms Jac6.hm1
#print axioms Jac6.n4_zero
#print axioms Jac6.n4_four
#print axioms Jac6.cls_formula
#print axioms Jac6.sum3
#print axioms Jac6.T_zero
#print axioms Jac6.jac_sum

lang/Jac7a.lean

lean_all/lang/Jac7a.lean

/-
  lang/Jac7a — T48, cycle 732: integer and ℤ[√−2] lemmas for the end of the 256a Jacobsthal route.
    par        2S² + T² = 8p ⇒ S = 2a, T = 4b, a² + 2b² = p
    uniq_re    nrm π = p prime, a² + 2b² = p ⇒ a² = (Re π)²       (Sq2Prime8.factor3 at k = 1, units ±1)
  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.Sq2Prime8
import lang.Sq2Unique

namespace Jac7a

open FTAChain DetChain LibInt Frobenius

theorem odd_sq_even (k t : Int) (h : (2 * k + 1) * (2 * k + 1) = 2 * t) : False :=
  GaussInt.int_odd_ne_even (2 * (k * k) + 2 * k) t (by rw [← h]; lib_int)

theorem kill (k : Nat) (W : Int) (h : Int.ofNat (k + 1) * W = 0) : W = 0 :=
  JacobiNorm.int_cancel W 0 k (by
    calc W * Int.ofNat (k + 1) = Int.ofNat (k + 1) * W := by lib_int
      _ = 0 := h
      _ = 0 * Int.ofNat (k + 1) := by lib_int)

theorem par (S T p : Int) (h : 2 * (S * S) + T * T = 8 * p) : ∃ a b : Int, S = 2 * a ∧ T = 4 * b ∧ a * a + 2 * (b * b) = p := by
  rcases GaussInt.int_even_odd T with ⟨t, ht⟩ | ⟨t, ht⟩
  · rcases GaussInt.int_even_odd S with ⟨a, ha⟩ | ⟨k, hk⟩
    · rcases GaussInt.int_even_odd t with ⟨b, hb⟩ | ⟨c, hc⟩
      · refine ⟨a, b, ha, by rw [ht, hb]; lib_int, ?_⟩
        have e : Int.ofNat (7 + 1) * (a * a + 2 * (b * b) + -p) = 0 := by
          rw [show Int.ofNat (7 + 1) * (a * a + 2 * (b * b) + -p) = 2 * (S * S) + T * T + -(8 * p) by rw [ha, ht, hb]; show (8 : Int) * _ = _; lib_int, h]; lib_int
        have e2 := kill 7 _ e
        calc a * a + 2 * (b * b) = (a * a + 2 * (b * b) + -p) + p := by lib_int
          _ = p := by rw [e2]; lib_int
      · exfalso
        -- 8a² + 4(2c+1)² = 8p ⇒ (2c+1)² = 2(p − a²)·... : (2c+1)² = 2(p − a² − ... ) impossible
        have e : Int.ofNat (3 + 1) * ((2 * c + 1) * (2 * c + 1) + -(2 * (p + -(a * a)))) = 0 := by
          rw [show Int.ofNat (3 + 1) * ((2 * c + 1) * (2 * c + 1) + -(2 * (p + -(a * a)))) = 2 * (S * S) + T * T + -(8 * p) by rw [ha, ht, hc]; show (4 : Int) * _ = _; lib_int, h]; lib_int
        have e2 := kill 3 _ e
        exact odd_sq_even c (p + -(a * a)) (by
          calc (2 * c + 1) * (2 * c + 1) = ((2 * c + 1) * (2 * c + 1) + -(2 * (p + -(a * a)))) + 2 * (p + -(a * a)) := by lib_int
            _ = _ := by rw [e2]; lib_int)
    · exfalso
      have e : Int.ofNat (1 + 1) * ((2 * k + 1) * (2 * k + 1) + -(2 * (2 * p + -(t * t)))) = 0 := by
        rw [show Int.ofNat (1 + 1) * ((2 * k + 1) * (2 * k + 1) + -(2 * (2 * p + -(t * t)))) = 2 * (S * S) + T * T + -(8 * p) by rw [hk, ht]; show (2 : Int) * _ = _; lib_int, h]; lib_int
      have e2 := kill 1 _ e
      exact odd_sq_even k (2 * p + -(t * t)) (by
        calc (2 * k + 1) * (2 * k + 1) = ((2 * k + 1) * (2 * k + 1) + -(2 * (2 * p + -(t * t)))) + 2 * (2 * p + -(t * t)) := by lib_int
          _ = _ := by rw [e2]; lib_int)
  · exfalso
    exact odd_sq_even t (4 * p + -(S * S)) (by rw [← ht, show 2 * (4 * p + -(S * S)) = 8 * p + -(2 * (S * S)) by lib_int, ← h]; lib_int)


theorem no_sum (x : Nat) (h : x + 1 + 1 = 1) : False := Nat.noConfusion (Nat.succ.inj h)

theorem pow1 (n : Nat) : n ^ 1 = n := by show 1 * n = n; lib_nat

theorem fst_neg (z : Int × Int) : ((Quad.quad LibInt.intR (-2)).neg z).1 = -z.1 := rfl

/-- **a² + 2b² = p ⇒ a² = (Re π)²** for π of prime norm p in ℤ[√−2]. -/
theorem uniq_re (P : Nat) (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : Sq2Euclid.nrm π = P + 1 + 1)
    (a b : Int) (hab : a * a + 2 * (b * b) = Int.ofNat (P + 1 + 1)) : a * a = π.1 * π.1 := by
  have hα : Sq2Euclid.nrm (a, b) = (P + 1 + 1) ^ 1 := by
    rw [pow1]; exact Int.ofNat.inj (by rw [Sq2Euclid.nrm_cast]; exact hab)
  obtain ⟨u, j, l, hu, hjl, he⟩ := Sq2Prime8.factor3 P hp π hπ 1 (a, b) hα
  have hu' : u = (Quad.quad LibInt.intR (-2)).one ∨ u = (Quad.quad LibInt.intR (-2)).neg (Quad.quad LibInt.intR (-2)).one := by
    rcases Sq2Unique.unit_pm u (Sq2Prime8.nrm_one_eq u hu) with e | e
    · exact Or.inl e
    · exact Or.inr (by rw [e]; rfl)
  have ha : a = (a, b).1 := rfl
  match j, l, hjl with
  | 1, 0, _ =>
    have e2 : (a, b) = (Quad.quad LibInt.intR (-2)).mul u π := by rw [he]; show (Quad.quad LibInt.intR (-2)).mul u ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (Quad.quad LibInt.intR (-2)).one π) (Quad.quad LibInt.intR (-2)).one) = _; lib_ring
    rcases hu' with eu | eu
    · rw [ha, e2, eu, show (Quad.quad LibInt.intR (-2)).mul (Quad.quad LibInt.intR (-2)).one π = π by lib_ring]
    · rw [ha, e2, eu, show (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).neg (Quad.quad LibInt.intR (-2)).one) π = (Quad.quad LibInt.intR (-2)).neg π by lib_ring, fst_neg]; lib_int
  | 0, 1, _ =>
    have e2 : (a, b) = (Quad.quad LibInt.intR (-2)).mul u (Sq2Euclid.cj π) := by rw [he]; show (Quad.quad LibInt.intR (-2)).mul u ((Quad.quad LibInt.intR (-2)).mul (Quad.quad LibInt.intR (-2)).one ((Quad.quad LibInt.intR (-2)).mul (Quad.quad LibInt.intR (-2)).one (Sq2Euclid.cj π))) = _; lib_ring
    rcases hu' with eu | eu
    · rw [ha, e2, eu, show (Quad.quad LibInt.intR (-2)).mul (Quad.quad LibInt.intR (-2)).one (Sq2Euclid.cj π) = Sq2Euclid.cj π by lib_ring]; rfl
    · rw [ha, e2, eu, show (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).neg (Quad.quad LibInt.intR (-2)).one) (Sq2Euclid.cj π) = (Quad.quad LibInt.intR (-2)).neg (Sq2Euclid.cj π) by lib_ring, fst_neg]
      show -π.1 * -π.1 = _; lib_int
  | 0, 0, h => exact absurd h (fun e => Nat.noConfusion e)
  | j + 2, l, h => exact (no_sum (j + l) (by rw [show j + l + 1 + 1 = j + 2 + l by lib_nat]; exact h)).elim
  | 1, l + 1, h => exact (no_sum l (by rw [show l + 1 + 1 = 1 + (l + 1) by lib_nat]; exact h)).elim
  | 0, l + 2, h => exact (no_sum l (by rw [show l + 1 + 1 = 0 + (l + 2) by lib_nat]; exact h)).elim

end Jac7a
#print axioms Jac7a.odd_sq_even
#print axioms Jac7a.kill
#print axioms Jac7a.par
#print axioms Jac7a.no_sum
#print axioms Jac7a.pow1
#print axioms Jac7a.fst_neg
#print axioms Jac7a.uniq_re

lang/JacNormN.lean

lean_all/lang/JacNormN.lean

/-
  lang/JacNormN — T54, cycle 828 (step 1): **the Jacobi sum of a character of any order has norm p**:
  for multiplicative ψ : 𝔽_p → E (ψ(1) = 1, ψ(0) = 0) with ψ and ψ² nontrivial (Σψ = Σψ² = Σψ'² = 0, ψ' = ψ∘ι, ι(x) = x^(p−2)),
    J(ψ, ψ) · J(ψ', ψ') = p      provided p − 1 cancels in E (e.g. E = ℤ[ζ_ℓ]).
  As JacobiNorm (ℓ = 3) but for any E: G² = J G_(ψ²) (jacobi_gauss), G G' = ψ'(−1) X (gauss_norm), X² = pX (XX);
  compute (G G')² both ways and read the ζ⁰ coefficient (gt_X).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.GaussNormN
import lang.JacobiGauss
import lang.GaussCritN
import lib.RingTac

namespace CharSumGen

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

variable {S : Type} (E : CRing S) (j : Nat)

theorem pw_oneJ {T : Type} (K : CRing T) : ∀ n : Nat, pw K K.one n = K.one
  | 0 => rfl
  | n + 1 => by
    show K.mul (pw K K.one n) K.one = K.one
    rw [pw_oneJ K n]; lib_ring

/-- the scalar m0(s) = s ζ⁰ in E[C_p]. -/
abbrev m0 (s : S) : V S (j + 1 + 1 + 1) := mono E (j + 1 + 1) (ring (j + 1 + 1)).zero s

theorem m0_mul (s t : S) : (gring E (j + 1 + 1)).mul (m0 E j s) (m0 E j t) = m0 E j (E.mul s t) := by
  show gmul E (j + 1 + 1) (mono E (j + 1 + 1) _ s) (mono E (j + 1 + 1) _ t) = _
  rw [mono_mul]
  show mono E (j + 1 + 1) ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).zero (ring (j + 1 + 1)).zero) (E.mul s t) = _
  rw [show (ring (j + 1 + 1)).add (ring (j + 1 + 1)).zero (ring (j + 1 + 1)).zero = (ring (j + 1 + 1)).zero by lib_ring]

/-- **J(ψ, ψ) J(ψ', ψ') = p.** -/
theorem jac_norm (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (ψ : Zp (j + 1 + 1) → S)
    (hm : ∀ x y, ψ ((ring (j + 1 + 1)).mul x y) = E.mul (ψ x) (ψ y)) (h1 : ψ (ring (j + 1 + 1)).one = E.one)
    (h0 : ψ (ring (j + 1 + 1)).zero = E.zero) (hs : sumK E ψ (LL j) = E.zero)
    (hs2 : sumK E (fun a => E.mul (ψ a) (ψ a)) (LL j) = E.zero)
    (hs2' : sumK E (fun a => E.mul (ψ (iv j a)) (ψ (iv j a))) (LL j) = E.zero)
    (hcan : ∀ a b, E.mul a (E.add (Pp E j) (E.neg E.one)) = E.mul b (E.add (Pp E j) (E.neg E.one)) → a = b) :
    E.mul (J2 E j ψ ψ) (J2 E j (fun x => ψ (iv j x)) (fun x => ψ (iv j x))) = Pp E j := by
  let R := gring E (j + 1 + 1)
  let ψ' : Zp (j + 1 + 1) → S := fun x => ψ (iv j x)
  let ψ2 : Zp (j + 1 + 1) → S := fun x => E.mul (ψ x) (ψ x)
  let e : S := ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))
  have hm' : ∀ x y, ψ' ((ring (j + 1 + 1)).mul x y) = E.mul (ψ' x) (ψ' y) := fun x y => by
    show ψ (iv j ((ring (j + 1 + 1)).mul x y)) = _; rw [iv_mulmul, hm]
  have h0' : ψ' (ring (j + 1 + 1)).zero = E.zero := by show ψ (iv j _) = _; rw [iv_zero, h0]
  have hm2 : ∀ x y, ψ2 ((ring (j + 1 + 1)).mul x y) = E.mul (ψ2 x) (ψ2 y) := fun x y => by
    show E.mul (ψ _) (ψ _) = E.mul (E.mul (ψ x) (ψ x)) (E.mul (ψ y) (ψ y)); rw [hm]; lib_ring
  have h12 : ψ2 (ring (j + 1 + 1)).one = E.one := by show E.mul (ψ _) (ψ _) = _; rw [h1]; lib_ring
  have h02 : ψ2 (ring (j + 1 + 1)).zero = E.zero := by show E.mul (ψ _) (ψ _) = _; rw [h0]; lib_ring
  -- e² = 1
  have hee : E.mul e e = E.one := by
    show E.mul (ψ (iv j _)) (ψ (iv j _)) = _
    rw [← hm, ← iv_mulmul, show (ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)
      ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) = (ring (j + 1 + 1)).one by lib_ring]
    show ψ (pw (ring (j + 1 + 1)) (ring (j + 1 + 1)).one (j + 1)) = _
    rw [pw_oneJ, h1]
  -- the four group-ring identities
  have hA : R.mul (Gv j ψ) (Gv j ψ) = R.mul (m0 E j (J2 E j ψ ψ)) (Gv j ψ2) := ext E (j + 1 + 1) _ _ (fun c => by
    show gt E (j + 1 + 1) (gmul E (j + 1 + 1) (Gv j ψ) (Gv j ψ)) c = gt E (j + 1 + 1) (gmul E (j + 1 + 1) (m0 E j _) (Gv j ψ2)) c
    rw [jacobi_gauss E j hpr ψ ψ hm hm h0 hs2 c, gt_mono0]
    unfold Gv; rw [gt_tb])
  have hB : R.mul (Gv j ψ') (Gv j ψ') = R.mul (m0 E j (J2 E j ψ' ψ')) (Gv j (fun x => ψ2 (iv j x))) := ext E (j + 1 + 1) _ _ (fun c => by
    show gt E (j + 1 + 1) (gmul E (j + 1 + 1) (Gv j ψ') (Gv j ψ')) c = gt E (j + 1 + 1) (gmul E (j + 1 + 1) (m0 E j _) (Gv j _)) c
    rw [jacobi_gauss E j hpr ψ' ψ' hm' hm' h0' hs2' c, gt_mono0]
    unfold Gv; rw [gt_tb])
  have hC : R.mul (Gv j ψ) (Gv j ψ') = R.mul (m0 E j e) (Xv E j) := ext E (j + 1 + 1) _ _ (fun c => by
    show gt E (j + 1 + 1) (gmul E (j + 1 + 1) (Gv j ψ) (Gv j (fun x => ψ (iv j x)))) c = gt E (j + 1 + 1) (gmul E (j + 1 + 1) (m0 E j e) (Xv E j)) c
    rw [gauss_norm E j hpr ψ hm h1 h0 hs c, gt_mono0])
  have hD : R.mul (Gv j ψ2) (Gv j (fun x => ψ2 (iv j x))) = R.mul (m0 E j (E.mul e e)) (Xv E j) := ext E (j + 1 + 1) _ _ (fun c => by
    show gt E (j + 1 + 1) (gmul E (j + 1 + 1) (Gv j ψ2) (Gv j (fun x => ψ2 (iv j x)))) c = gt E (j + 1 + 1) (gmul E (j + 1 + 1) (m0 E j _) (Xv E j)) c
    rw [gauss_norm E j hpr ψ2 hm2 h12 h02 hs2 c, gt_mono0])
  have hX : R.mul (Xv E j) (Xv E j) = R.mul (m0 E j (Pp E j)) (Xv E j) := XX E j
  -- (G G')² two ways
  have lhs : R.mul (R.mul (Gv j ψ) (Gv j ψ')) (R.mul (Gv j ψ) (Gv j ψ')) = R.mul (m0 E j (Pp E j)) (Xv E j) := by
    rw [hC]
    calc R.mul (R.mul (m0 E j e) (Xv E j)) (R.mul (m0 E j e) (Xv E j))
        = R.mul (R.mul (m0 E j e) (m0 E j e)) (R.mul (Xv E j) (Xv E j)) := by lib_ring
      _ = R.mul (m0 E j E.one) (R.mul (m0 E j (Pp E j)) (Xv E j)) := by rw [m0_mul, hee, hX]
      _ = R.mul (R.mul (m0 E j E.one) (m0 E j (Pp E j))) (Xv E j) := by lib_ring
      _ = R.mul (m0 E j (Pp E j)) (Xv E j) := by rw [m0_mul, show E.mul E.one (Pp E j) = Pp E j by lib_ring]
  have rhs : R.mul (R.mul (Gv j ψ) (Gv j ψ')) (R.mul (Gv j ψ) (Gv j ψ')) =
      R.mul (m0 E j (E.mul (J2 E j ψ ψ) (J2 E j ψ' ψ'))) (Xv E j) := by
    calc R.mul (R.mul (Gv j ψ) (Gv j ψ')) (R.mul (Gv j ψ) (Gv j ψ'))
        = R.mul (R.mul (Gv j ψ) (Gv j ψ)) (R.mul (Gv j ψ') (Gv j ψ')) := by lib_ring
      _ = R.mul (R.mul (m0 E j (J2 E j ψ ψ)) (Gv j ψ2)) (R.mul (m0 E j (J2 E j ψ' ψ')) (Gv j (fun x => ψ2 (iv j x)))) := by rw [hA, hB]
      _ = R.mul (R.mul (m0 E j (J2 E j ψ ψ)) (m0 E j (J2 E j ψ' ψ'))) (R.mul (Gv j ψ2) (Gv j (fun x => ψ2 (iv j x)))) := by lib_ring
      _ = R.mul (m0 E j (E.mul (J2 E j ψ ψ) (J2 E j ψ' ψ'))) (R.mul (m0 E j E.one) (Xv E j)) := by rw [m0_mul, hD, hee]
      _ = R.mul (R.mul (m0 E j (E.mul (J2 E j ψ ψ) (J2 E j ψ' ψ'))) (m0 E j E.one)) (Xv E j) := by lib_ring
      _ = _ := by rw [m0_mul, show E.mul (E.mul (J2 E j ψ ψ) (J2 E j ψ' ψ')) E.one = E.mul (J2 E j ψ ψ) (J2 E j ψ' ψ') by lib_ring]
  -- ζ⁰ coefficient: P·(P − 1) = J J'·(P − 1)
  have h00 : gt E (j + 1 + 1) (gmul E (j + 1 + 1) (m0 E j (Pp E j)) (Xv E j)) (ring (j + 1 + 1)).zero =
      gt E (j + 1 + 1) (gmul E (j + 1 + 1) (m0 E j (E.mul (J2 E j ψ ψ) (J2 E j ψ' ψ'))) (Xv E j)) (ring (j + 1 + 1)).zero :=
    congrArg (fun u => gt E (j + 1 + 1) u (ring (j + 1 + 1)).zero) (lhs.symm.trans rhs)
  rw [gt_mono0, gt_mono0, gt_X] at h00
  have hd : delta E (ring (j + 1 + 1)).zero (ring (j + 1 + 1)).zero = E.one := by
    unfold delta; rw [ite_pos' _ _ rfl]
  rw [hd] at h00
  apply Eq.symm
  apply hcan
  calc E.mul (Pp E j) (E.add (Pp E j) (E.neg E.one))
      = E.mul (Pp E j) (E.add (E.mul (Pp E j) E.one) (E.neg E.one)) := by lib_ring
    _ = E.mul (E.mul (J2 E j ψ ψ) (J2 E j ψ' ψ')) (E.add (E.mul (Pp E j) E.one) (E.neg E.one)) := h00
    _ = E.mul (E.mul (J2 E j ψ ψ) (J2 E j ψ' ψ')) (E.add (Pp E j) (E.neg E.one)) := by lib_ring

end CharSumGen

#print axioms CharSumGen.pw_oneJ
#print axioms CharSumGen.m0_mul
#print axioms CharSumGen.jac_norm

lang/JacNormPair.lean

lean_all/lang/JacNormPair.lean

/-
  lang/JacNormPair — T49/T54: **J(χ, μ)·J(χ̄, μ̄) = p for two characters** (χ, μ, χμ nontrivial), generalising JacNormN (χ = μ).
  G(χ)G(μ) = J(χ, μ) G(χμ) (jacobi_gauss), G(χ)G(χ̄) = χ̄(−1) X (gauss_norm), X² = pX; compute (G_χ G_χ̄)(G_μ G_μ̄) both ways,
  read the ζ⁰ coefficient and cancel p − 1 and the unit χ̄(−1)μ̄(−1).
    e_sq        χ(ι(−1))² = 1
    jac_norm2   J(χ, μ) J(χ∘ι, μ∘ι) = p
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.JacNormN

namespace CharSumGen

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

variable {S : Type} (E : CRing S) (j : Nat)

theorem e_sq (ψ : Zp (j + 1 + 1) → S) (hm : ∀ x y, ψ ((ring (j + 1 + 1)).mul x y) = E.mul (ψ x) (ψ y))
    (h1 : ψ (ring (j + 1 + 1)).one = E.one) :
    E.mul (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))) = E.one := by
  rw [← hm, ← iv_mulmul, show (ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)
    ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) = (ring (j + 1 + 1)).one by lib_ring]
  show ψ (pw (ring (j + 1 + 1)) (ring (j + 1 + 1)).one (j + 1)) = _
  rw [pw_oneJ, h1]

/-- **J(χ, μ) J(χ∘ι, μ∘ι) = p.** -/
theorem jac_norm2 (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (χ μ : Zp (j + 1 + 1) → S)
    (hmχ : ∀ x y, χ ((ring (j + 1 + 1)).mul x y) = E.mul (χ x) (χ y)) (h1χ : χ (ring (j + 1 + 1)).one = E.one)
    (h0χ : χ (ring (j + 1 + 1)).zero = E.zero) (hsχ : sumK E χ (LL j) = E.zero)
    (hmμ : ∀ x y, μ ((ring (j + 1 + 1)).mul x y) = E.mul (μ x) (μ y)) (h1μ : μ (ring (j + 1 + 1)).one = E.one)
    (h0μ : μ (ring (j + 1 + 1)).zero = E.zero) (hsμ : sumK E μ (LL j) = E.zero)
    (hsν : sumK E (fun a => E.mul (χ a) (μ a)) (LL j) = E.zero)
    (hsν' : sumK E (fun a => E.mul (χ (iv j a)) (μ (iv j a))) (LL j) = E.zero)
    (hcan : ∀ a b, E.mul a (E.add (Pp E j) (E.neg E.one)) = E.mul b (E.add (Pp E j) (E.neg E.one)) → a = b) :
    E.mul (J2 E j χ μ) (J2 E j (fun x => χ (iv j x)) (fun x => μ (iv j x))) = Pp E j := by
  let R := gring E (j + 1 + 1)
  let χ' : Zp (j + 1 + 1) → S := fun x => χ (iv j x)
  let μ' : Zp (j + 1 + 1) → S := fun x => μ (iv j x)
  let ν : Zp (j + 1 + 1) → S := fun x => E.mul (χ x) (μ x)
  let eχ : S := χ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))
  let eμ : S := μ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))
  have hmχ' : ∀ x y, χ' ((ring (j + 1 + 1)).mul x y) = E.mul (χ' x) (χ' y) := fun x y => by
    show χ (iv j ((ring (j + 1 + 1)).mul x y)) = _; rw [iv_mulmul, hmχ]
  have hmμ' : ∀ x y, μ' ((ring (j + 1 + 1)).mul x y) = E.mul (μ' x) (μ' y) := fun x y => by
    show μ (iv j ((ring (j + 1 + 1)).mul x y)) = _; rw [iv_mulmul, hmμ]
  have h0χ' : χ' (ring (j + 1 + 1)).zero = E.zero := by show χ (iv j _) = _; rw [iv_zero, h0χ]
  have hmν : ∀ x y, ν ((ring (j + 1 + 1)).mul x y) = E.mul (ν x) (ν y) := fun x y => by
    show E.mul (χ _) (μ _) = E.mul (E.mul (χ x) (μ x)) (E.mul (χ y) (μ y)); rw [hmχ, hmμ]; lib_ring
  have h1ν : ν (ring (j + 1 + 1)).one = E.one := by show E.mul (χ _) (μ _) = _; rw [h1χ, h1μ]; lib_ring
  have h0ν : ν (ring (j + 1 + 1)).zero = E.zero := by show E.mul (χ _) (μ _) = _; rw [h0χ]; lib_ring
  have heχ : E.mul eχ eχ = E.one := e_sq E j χ hmχ h1χ
  have heμ : E.mul eμ eμ = E.one := e_sq E j μ hmμ h1μ
  have hA : R.mul (Gv j χ) (Gv j μ) = R.mul (m0 E j (J2 E j χ μ)) (Gv j ν) := ext E (j + 1 + 1) _ _ (fun c => by
    show gt E (j + 1 + 1) (gmul E (j + 1 + 1) (Gv j χ) (Gv j μ)) c = gt E (j + 1 + 1) (gmul E (j + 1 + 1) (m0 E j _) (Gv j ν)) c
    rw [jacobi_gauss E j hpr χ μ hmχ hmμ h0χ hsν c, gt_mono0]
    unfold Gv; rw [gt_tb])
  have hB : R.mul (Gv j χ') (Gv j μ') = R.mul (m0 E j (J2 E j χ' μ')) (Gv j (fun x => ν (iv j x))) :=
    ext E (j + 1 + 1) _ _ (fun c => by
      show gt E (j + 1 + 1) (gmul E (j + 1 + 1) (Gv j χ') (Gv j μ')) c = gt E (j + 1 + 1) (gmul E (j + 1 + 1) (m0 E j _) (Gv j _)) c
      rw [jacobi_gauss E j hpr χ' μ' hmχ' hmμ' h0χ' hsν' c, gt_mono0]
      unfold Gv; rw [gt_tb])
  have hCχ : R.mul (Gv j χ) (Gv j χ') = R.mul (m0 E j eχ) (Xv E j) := ext E (j + 1 + 1) _ _ (fun c => by
    show gt E (j + 1 + 1) (gmul E (j + 1 + 1) (Gv j χ) (Gv j (fun x => χ (iv j x)))) c = gt E (j + 1 + 1) (gmul E (j + 1 + 1) (m0 E j eχ) (Xv E j)) c
    rw [gauss_norm E j hpr χ hmχ h1χ h0χ hsχ c, gt_mono0])
  have hCμ : R.mul (Gv j μ) (Gv j μ') = R.mul (m0 E j eμ) (Xv E j) := ext E (j + 1 + 1) _ _ (fun c => by
    show gt E (j + 1 + 1) (gmul E (j + 1 + 1) (Gv j μ) (Gv j (fun x => μ (iv j x)))) c = gt E (j + 1 + 1) (gmul E (j + 1 + 1) (m0 E j eμ) (Xv E j)) c
    rw [gauss_norm E j hpr μ hmμ h1μ h0μ hsμ c, gt_mono0])
  have hD : R.mul (Gv j ν) (Gv j (fun x => ν (iv j x))) = R.mul (m0 E j (E.mul eχ eμ)) (Xv E j) := ext E (j + 1 + 1) _ _ (fun c => by
    show gt E (j + 1 + 1) (gmul E (j + 1 + 1) (Gv j ν) (Gv j (fun x => ν (iv j x)))) c = gt E (j + 1 + 1) (gmul E (j + 1 + 1) (m0 E j _) (Xv E j)) c
    rw [gauss_norm E j hpr ν hmν h1ν h0ν hsν c, gt_mono0])
  have hX : R.mul (Xv E j) (Xv E j) = R.mul (m0 E j (Pp E j)) (Xv E j) := XX E j
  have lhs : R.mul (R.mul (Gv j χ) (Gv j χ')) (R.mul (Gv j μ) (Gv j μ')) =
      R.mul (m0 E j (E.mul (E.mul eχ eμ) (Pp E j))) (Xv E j) := by
    rw [hCχ, hCμ]
    calc R.mul (R.mul (m0 E j eχ) (Xv E j)) (R.mul (m0 E j eμ) (Xv E j))
        = R.mul (R.mul (m0 E j eχ) (m0 E j eμ)) (R.mul (Xv E j) (Xv E j)) := by lib_ring
      _ = R.mul (m0 E j (E.mul eχ eμ)) (R.mul (m0 E j (Pp E j)) (Xv E j)) := by rw [m0_mul, hX]
      _ = R.mul (R.mul (m0 E j (E.mul eχ eμ)) (m0 E j (Pp E j))) (Xv E j) := by lib_ring
      _ = _ := by rw [m0_mul]
  have rhs : R.mul (R.mul (Gv j χ) (Gv j χ')) (R.mul (Gv j μ) (Gv j μ')) =
      R.mul (m0 E j (E.mul (E.mul (J2 E j χ μ) (J2 E j χ' μ')) (E.mul eχ eμ))) (Xv E j) := by
    calc R.mul (R.mul (Gv j χ) (Gv j χ')) (R.mul (Gv j μ) (Gv j μ'))
        = R.mul (R.mul (Gv j χ) (Gv j μ)) (R.mul (Gv j χ') (Gv j μ')) := by lib_ring
      _ = R.mul (R.mul (m0 E j (J2 E j χ μ)) (Gv j ν)) (R.mul (m0 E j (J2 E j χ' μ')) (Gv j (fun x => ν (iv j x)))) := by rw [hA, hB]
      _ = R.mul (R.mul (m0 E j (J2 E j χ μ)) (m0 E j (J2 E j χ' μ'))) (R.mul (Gv j ν) (Gv j (fun x => ν (iv j x)))) := by lib_ring
      _ = R.mul (m0 E j (E.mul (J2 E j χ μ) (J2 E j χ' μ'))) (R.mul (m0 E j (E.mul eχ eμ)) (Xv E j)) := by rw [m0_mul, hD]
      _ = R.mul (R.mul (m0 E j (E.mul (J2 E j χ μ) (J2 E j χ' μ'))) (m0 E j (E.mul eχ eμ))) (Xv E j) := by lib_ring
      _ = _ := by rw [m0_mul]
  have h00 : gt E (j + 1 + 1) (gmul E (j + 1 + 1) (m0 E j (E.mul (E.mul eχ eμ) (Pp E j))) (Xv E j)) (ring (j + 1 + 1)).zero =
      gt E (j + 1 + 1) (gmul E (j + 1 + 1) (m0 E j (E.mul (E.mul (J2 E j χ μ) (J2 E j χ' μ')) (E.mul eχ eμ))) (Xv E j)) (ring (j + 1 + 1)).zero :=
    congrArg (fun u => gt E (j + 1 + 1) u (ring (j + 1 + 1)).zero) (lhs.symm.trans rhs)
  rw [gt_mono0, gt_mono0, gt_X] at h00
  have hd : delta E (ring (j + 1 + 1)).zero (ring (j + 1 + 1)).zero = E.one := by
    unfold delta; rw [ite_pos' _ _ rfl]
  rw [hd] at h00
  have hc : E.mul (E.mul eχ eμ) (Pp E j) = E.mul (E.mul (J2 E j χ μ) (J2 E j χ' μ')) (E.mul eχ eμ) := by
    apply hcan
    calc E.mul (E.mul (E.mul eχ eμ) (Pp E j)) (E.add (Pp E j) (E.neg E.one))
        = E.mul (E.mul (E.mul eχ eμ) (Pp E j)) (E.add (E.mul (Pp E j) E.one) (E.neg E.one)) := by lib_ring
      _ = E.mul (E.mul (E.mul (J2 E j χ μ) (J2 E j χ' μ')) (E.mul eχ eμ)) (E.add (E.mul (Pp E j) E.one) (E.neg E.one)) := h00
      _ = _ := by lib_ring
  calc E.mul (J2 E j χ μ) (J2 E j χ' μ')
      = E.mul (E.mul (E.mul (J2 E j χ μ) (J2 E j χ' μ')) (E.mul eχ eμ)) (E.mul eχ eμ) := by
        rw [show E.mul (E.mul (E.mul (J2 E j χ μ) (J2 E j χ' μ')) (E.mul eχ eμ)) (E.mul eχ eμ) =
          E.mul (E.mul (J2 E j χ μ) (J2 E j χ' μ')) (E.mul (E.mul eχ eχ) (E.mul eμ eμ)) by lib_ring, heχ, heμ]
        lib_ring
    _ = E.mul (E.mul (E.mul eχ eμ) (Pp E j)) (E.mul eχ eμ) := by rw [hc]
    _ = E.mul (Pp E j) (E.mul (E.mul eχ eχ) (E.mul eμ eμ)) := by lib_ring
    _ = Pp E j := by rw [heχ, heμ]; lib_ring

end CharSumGen

#print axioms CharSumGen.e_sq
#print axioms CharSumGen.jac_norm2

lang/JacRefl.lean

lean_all/lang/JacRefl.lean

/-
  lang/JacRefl — T49, cycle 757: **the reflection formula for Jacobi sums** over 𝔽_p, for multiplicative ψ, φ : 𝔽_p → E (any
  commutative ring E), ψ(1) = φ(1) = 1, ψ(0) = φ(0) = 0:
      J(ψ, φ) = ψ(−1) · J(ψ, μ),   μ(x) = ψ(x⁻¹) φ(x⁻¹)   (μ = conj(ψφ)),
  by the involution t ↦ t/(t − 1) of 𝔽_p ∖ {1} (fl, fixing 1):  fl_fl, fl_sub (fl(s) − 1 = (s − 1)⁻¹), iv_neg1 ((−1)⁻¹ = −1).
  Used for the quintic character: J(χ, χ³) = J(χ, χ) and J(χ², χ) = J(χ², χ²), so A = J(χ,χ)J(χ²,χ)J(χ³,χ) = J(χ,χ)² σ₂(J(χ,χ)).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.JacobiGauss

namespace JacRefl

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius CharSumGen

variable {S : Type} (E : CRing S) (j : Nat)

theorem neg1_ne : ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) ≠ (ring (j + 1 + 1)).zero := fun e => PowerSum.one_ne_zero' (j + 1) (by
  calc (ring (j + 1 + 1)).one = (ring (j + 1 + 1)).neg ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) := (neg_neg' (ring (j + 1 + 1)) _).symm
    _ = (ring (j + 1 + 1)).neg (ring (j + 1 + 1)).zero := by rw [e]
    _ = (ring (j + 1 + 1)).zero := by lib_ring)

theorem iv_neg1 (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) : iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) = ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) := by
  have h := iv_mul j hpr ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) (neg1_ne j)
  calc iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) = (ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)) ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)) ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) := by lib_ring
    _ = ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) := by rw [h]; lib_ring

def fl (s : Zp (j + 1 + 1)) : Zp (j + 1 + 1) := if s = (ring (j + 1 + 1)).one then (ring (j + 1 + 1)).one else (ring (j + 1 + 1)).mul s (iv j ((ring (j + 1 + 1)).add s ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))

theorem sub_ne (s : Zp (j + 1 + 1)) (hs : s ≠ (ring (j + 1 + 1)).one) : (ring (j + 1 + 1)).add s ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) ≠ (ring (j + 1 + 1)).zero := fun e => hs (by
  calc s = (ring (j + 1 + 1)).add ((ring (j + 1 + 1)).add s ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)) (ring (j + 1 + 1)).one := by lib_ring
    _ = (ring (j + 1 + 1)).one := by rw [e]; lib_ring)

theorem fl_ne (s : Zp (j + 1 + 1)) (hs : s ≠ (ring (j + 1 + 1)).one) : fl j s = (ring (j + 1 + 1)).mul s (iv j ((ring (j + 1 + 1)).add s ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))) := by
  unfold fl; rw [ite_neg' _ _ hs]

theorem fl_sub (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (s : Zp (j + 1 + 1)) (hs : s ≠ (ring (j + 1 + 1)).one) : (ring (j + 1 + 1)).add (fl j s) ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) = iv j ((ring (j + 1 + 1)).add s ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)) := by
  have hu := iv_mul j hpr _ (sub_ne j s hs)
  rw [fl_ne j s hs]
  calc (ring (j + 1 + 1)).add ((ring (j + 1 + 1)).mul s (iv j ((ring (j + 1 + 1)).add s ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)
      = (ring (j + 1 + 1)).add ((ring (j + 1 + 1)).add ((ring (j + 1 + 1)).mul (iv j ((ring (j + 1 + 1)).add s ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))) ((ring (j + 1 + 1)).add s ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))) (iv j ((ring (j + 1 + 1)).add s ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) := by lib_ring
    _ = iv j ((ring (j + 1 + 1)).add s ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)) := by rw [hu]; lib_ring

theorem fl_ne1 (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (s : Zp (j + 1 + 1)) (hs : s ≠ (ring (j + 1 + 1)).one) : fl j s ≠ (ring (j + 1 + 1)).one := fun e => by
  have h1 := fl_sub j hpr s hs
  rw [e, show (ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) = (ring (j + 1 + 1)).zero by lib_ring] at h1
  have hu := iv_mul j hpr _ (sub_ne j s hs)
  rw [← h1, zero_mul'] at hu
  exact PowerSum.one_ne_zero' (j + 1) hu.symm

theorem fl_fl (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (s : Zp (j + 1 + 1)) : fl j (fl j s) = s := by
  by_cases hs : s = (ring (j + 1 + 1)).one
  · rw [hs]; unfold fl; rw [ite_pos' _ _ rfl, ite_pos' _ _ rfl]
  · have hu := iv_mul j hpr _ (sub_ne j s hs)
    rw [fl_ne j _ (fl_ne1 j hpr s hs), fl_sub j hpr s hs, iv_iv j hpr, fl_ne j s hs]
    calc (ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul s (iv j ((ring (j + 1 + 1)).add s ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) ((ring (j + 1 + 1)).add s ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))
        = (ring (j + 1 + 1)).mul s ((ring (j + 1 + 1)).mul (iv j ((ring (j + 1 + 1)).add s ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))) ((ring (j + 1 + 1)).add s ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))) := by lib_ring
      _ = s := by rw [hu]; lib_ring

/-- **J(ψ, φ) = ψ(−1) J(ψ, μ)**, μ(x) = ψ(x⁻¹)φ(x⁻¹). -/
theorem jac_refl (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (ψ φ : Zp (j + 1 + 1) → S) (hψm : ∀ x y, ψ ((ring (j + 1 + 1)).mul x y) = E.mul (ψ x) (ψ y)) (hφm : ∀ x y, φ ((ring (j + 1 + 1)).mul x y) = E.mul (φ x) (φ y)) (hψ1 : ψ (ring (j + 1 + 1)).one = E.one) (hφ0 : φ (ring (j + 1 + 1)).zero = E.zero) (hψ0 : ψ (ring (j + 1 + 1)).zero = E.zero) :
    J2 E j ψ φ = E.mul (ψ ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)) (J2 E j ψ (fun x => E.mul (ψ (iv j x)) (φ (iv j x)))) := by
  have hpm : E.mul (ψ ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)) (ψ ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)) = E.one := by
    rw [← hψm, show (ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) = (ring (j + 1 + 1)).one by lib_ring, hψ1]
  have hre : sumK E (fun s => E.mul (ψ (fl j s)) (φ ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg (fl j s))))) (LL j)
      = sumK E (fun t => E.mul (ψ t) (φ ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg t)))) (LL j) :=
    reindex E _ (CMInert.complete_allZ (j + 1 + 1) E) (fl j) (fl j) (fl_fl j hpr) (fl_fl j hpr)
      (fun t => E.mul (ψ t) (φ ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg t))))
  have pt : ∀ s, E.mul (ψ (fl j s)) (φ ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg (fl j s))))
      = E.mul (ψ ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)) (E.mul (ψ s) (E.mul (ψ (iv j ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg s)))) (φ (iv j ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg s)))))) := fun s => by
    by_cases hs : s = (ring (j + 1 + 1)).one
    · rw [hs]; unfold fl; rw [ite_pos' _ _ rfl, show (ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) = (ring (j + 1 + 1)).zero by lib_ring, iv_zero, hφ0, hψ0]; lib_ring
    · have e1 : (ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg (fl j s)) = (ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) (iv j ((ring (j + 1 + 1)).add s ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))) := by
        rw [← fl_sub j hpr s hs]; lib_ring
      have e2 : (ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg s) = (ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) ((ring (j + 1 + 1)).add s ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)) := by lib_ring
      rw [e1, e2, iv_mulmul, iv_neg1 j hpr, fl_ne j s hs, hψm, hψm, hφm]
      calc E.mul (E.mul (ψ s) (ψ (iv j ((ring (j + 1 + 1)).add s ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))))) (E.mul (φ ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)) (φ (iv j ((ring (j + 1 + 1)).add s ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))))
          = E.mul (E.mul (ψ ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)) (ψ ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))) (E.mul (E.mul (ψ s) (ψ (iv j ((ring (j + 1 + 1)).add s ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))))) (E.mul (φ ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)) (φ (iv j ((ring (j + 1 + 1)).add s ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))))) := by rw [hpm];   -- (+8 chars; full line in the Zenodo source)
        _ = _ := by lib_ring
  unfold J2
  rw [← hre, sumK_ext E _ _ pt]
  exact (mul_sumK E _ _ _).symm

end JacRefl

#print axioms JacRefl.neg1_ne
#print axioms JacRefl.iv_neg1
#print axioms JacRefl.sub_ne
#print axioms JacRefl.fl_ne
#print axioms JacRefl.fl_sub
#print axioms JacRefl.fl_ne1
#print axioms JacRefl.fl_fl
#print axioms JacRefl.jac_refl

lang/JacobClass.lean

lean_all/lang/JacobClass.lean

/-
  lang/JacobClass — T43, cycle 45: Jacobsthal sums on square classes: 2 Σ_a φ(a)² = (p − 1)(φ(1)² + φ(n)²).

  p = 2m + 3, n a non-residue (QChar.exists_nonres), φ from lang/Jacobsthal.
    chi_pw_one      χ(a) = 1 ⇒ a^((p−1)/2) = 1
    chi_iv          χ(ι n) = χ(n)
    phi_class       2 φ(a)² = φ(1)² (χ(a)² + χ(a)) + φ(n)² (χ(a)² − χ(a))     (a = t² or a = n t², EulerHard)
    sum_chi_sq      Σ_a χ(a)² = p − 1
    grouped         2 Σ_a φ(a)² = (p − 1)(φ(1)² + φ(n)²)
  With JacobMoment.moment: (p − 1)(φ(1)² + φ(n)²) = 2p(p − 1)(1 + χ(−1)), so φ(1)² + φ(n)² = 4p for p ≡ 1 (mod 4).

  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.JacobMoment
import lang.EulerHard

namespace JacobClass

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum Jacobsthal JacobMoment

variable (m : Nat)

theorem z01 : (0 : Int) ≠ 1 := fun e => Nat.noConfusion (Int.ofNat.inj e)

theorem chi_cases (x : Zp (2 * m + 1 + 1)) : chi m x = 0 ∨ chi m x = 1 ∨ chi m x = -1 := by
  unfold chi
  by_cases h0 : x = (ring (2 * m + 1 + 1)).zero
  · rw [ite_pos' _ _ h0]; exact Or.inl rfl
  · rw [ite_neg' _ _ h0]
    by_cases h1 : Frobenius.pw (ring (2 * m + 1 + 1)) x (m + 1) = (ring (2 * m + 1 + 1)).one
    · rw [ite_pos' _ _ h1]; exact Or.inr (Or.inl rfl)
    · rw [ite_neg' _ _ h1]; exact Or.inr (Or.inr rfl)

theorem chi_pw_one (x : Zp (2 * m + 1 + 1)) (h : chi m x = 1) : Frobenius.pw (ring (2 * m + 1 + 1)) x (m + 1) = (ring (2 * m + 1 + 1)).one := by
  unfold chi at h
  by_cases h0 : x = (ring (2 * m + 1 + 1)).zero
  · rw [ite_pos' _ _ h0] at h; exact absurd h z01
  · rw [ite_neg' _ _ h0] at h
    by_cases h1 : Frobenius.pw (ring (2 * m + 1 + 1)) x (m + 1) = (ring (2 * m + 1 + 1)).one
    · exact h1
    · rw [ite_neg' _ _ h1] at h; exact absurd h (fun e => Int.noConfusion e)

theorem chi_ne_zero (x : Zp (2 * m + 1 + 1)) (h : x ≠ (ring (2 * m + 1 + 1)).zero) : chi m x ≠ 0 := by
  unfold chi
  rw [ite_neg' _ _ h]
  by_cases h1 : Frobenius.pw (ring (2 * m + 1 + 1)) x (m + 1) = (ring (2 * m + 1 + 1)).one
  · rw [ite_pos' _ _ h1]; exact fun e => z01 e.symm
  · rw [ite_neg' _ _ h1]; exact fun e => Int.noConfusion e

theorem chi_sq_val (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (t : Zp (2 * m + 1 + 1))
    (ht : t ≠ (ring (2 * m + 1 + 1)).zero) : chi m t * chi m t = 1 := by
  rw [← chi_mul m hpr]; exact chi_sq m hpr t ht

theorem chi_iv (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (n : Zp (2 * m + 1 + 1))
    (hn : n ≠ (ring (2 * m + 1 + 1)).zero) : chi m (iv m n) = chi m n := by
  have h1 : chi m n * chi m (iv m n) = 1 := by
    rw [← chi_mul m hpr, (ring (2 * m + 1 + 1)).mul_comm, iv_mul_self m hpr n hn, ConicSum.chi_one]
  have h2 := chi_sq_val m hpr n hn
  -- c i = 1 and c c = 1 ⇒ i = c
  calc chi m (iv m n) = (chi m n * chi m n) * chi m (iv m n) := by rw [h2]; lib_int
    _ = chi m n * (chi m n * chi m (iv m n)) := by lib_int
    _ = chi m n := by rw [h1]; lib_int

theorem square_of (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a : Zp (2 * m + 1 + 1))
    (h : chi m a = 1) : ∃ t, t ≠ (ring (2 * m + 1 + 1)).zero ∧ (ring (2 * m + 1 + 1)).mul t t = a := by
  match EulerHard.euler_hard m hpr a (chi_pw_one m a h) with
  | ⟨t, ht⟩ =>
    refine ⟨t, fun e => ?_, ht⟩
    rw [e, DetChain.zero_mul'] at ht
    rw [← ht, chi_zero] at h
    exact z01 h

/-- **φ² on square classes.** -/
theorem phi_class (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (n : Zp (2 * m + 1 + 1))
    (hn0 : n ≠ (ring (2 * m + 1 + 1)).zero) (hn : chi m n = -1) (a : Zp (2 * m + 1 + 1)) :
    2 * (phi m a * phi m a) = phi m (ring (2 * m + 1 + 1)).one * phi m (ring (2 * m + 1 + 1)).one * (chi m a * chi m a + chi m a)
      + phi m n * phi m n * (chi m a * chi m a - chi m a) := by
  by_cases ha : a = (ring (2 * m + 1 + 1)).zero
  · rw [ha, phi_zero m hpr, chi_zero]; lib_int
  cases chi_cases m a with
  | inl h0 => exact absurd h0 (chi_ne_zero m a ha)
  | inr h =>
    cases h with
    | inl h1 =>
      match square_of m hpr a h1 with
      | ⟨t, ht0, ht⟩ =>
        have e : phi m a = chi m t * phi m (ring (2 * m + 1 + 1)).one := by
          rw [← ht, ← phi_scale m hpr (ring (2 * m + 1 + 1)).one t ht0, (ring (2 * m + 1 + 1)).one_mul]
        rw [e, h1]
        have := chi_sq_val m hpr t ht0
        calc 2 * (chi m t * phi m (ring (2 * m + 1 + 1)).one * (chi m t * phi m (ring (2 * m + 1 + 1)).one))
            = 2 * ((chi m t * chi m t) * (phi m (ring (2 * m + 1 + 1)).one * phi m (ring (2 * m + 1 + 1)).one)) := by lib_int
          _ = _ := by rw [this]; lib_int
    | inr hm =>
      have hq : chi m ((ring (2 * m + 1 + 1)).mul a (iv m n)) = 1 := by
        rw [chi_mul m hpr, chi_iv m hpr n hn0, hm, hn]; rfl
      match square_of m hpr _ hq with
      | ⟨t, ht0, ht⟩ =>
        have ha' : a = (ring (2 * m + 1 + 1)).mul n ((ring (2 * m + 1 + 1)).mul t t) := by
          rw [ht, (ring (2 * m + 1 + 1)).mul_comm a, ← (ring (2 * m + 1 + 1)).mul_assoc, (ring (2 * m + 1 + 1)).mul_comm n, iv_mul_self m hpr n hn0, (ring (2 * m + 1 + 1)).one_mul]
        have e : phi m a = chi m t * phi m n := by rw [ha']; exact phi_scale m hpr n t ht0
        rw [e, hm]
        have := chi_sq_val m hpr t ht0
        calc 2 * (chi m t * phi m n * (chi m t * phi m n))
            = 2 * ((chi m t * chi m t) * (phi m n * phi m n)) := by lib_int
          _ = _ := by rw [this]; lib_int

theorem sum_chi_sq (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) :
    sumK Z (fun a => chi m a * chi m a) (L m) = P m + -1 := by
  have pt : ∀ a, chi m a * chi m a = Z.add 1 (Z.mul (-1) (delta Z a (ring (2 * m + 1 + 1)).zero)) := fun a => by
    by_cases ha : a = (ring (2 * m + 1 + 1)).zero
    · rw [ha, chi_zero, delta_one m _ _ rfl]; rfl
    · rw [chi_sq_val m hpr a ha, delta_zero m _ _ ha]; rfl
  rw [sumK_ext Z _ _ pt, sumK_add, ← mul_sumK, sum_one, show sumK Z (fun x => delta Z x (ring (2 * m + 1 + 1)).zero) (L m) = 1 from compL m (ring (2 * m + 1 + 1)).zero]
  show P m + -1 * 1 = P m + -1
  lib_int

/-- **2 Σ_a φ(a)² = (p − 1)(φ(1)² + φ(n)²).** -/
theorem grouped (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (n : Zp (2 * m + 1 + 1))
    (hn0 : n ≠ (ring (2 * m + 1 + 1)).zero) (hn : chi m n = -1) :
    2 * sumK Z (fun a => phi m a * phi m a) (L m)
      = (P m + -1) * (phi m (ring (2 * m + 1 + 1)).one * phi m (ring (2 * m + 1 + 1)).one + phi m n * phi m n) := by
  have pt : ∀ a, Z.mul 2 (phi m a * phi m a)
      = Z.add (Z.mul (phi m (ring (2 * m + 1 + 1)).one * phi m (ring (2 * m + 1 + 1)).one + phi m n * phi m n) (chi m a * chi m a))
          (Z.mul (phi m (ring (2 * m + 1 + 1)).one * phi m (ring (2 * m + 1 + 1)).one + -1 * (phi m n * phi m n)) (chi m a)) := fun a => by
    show 2 * (phi m a * phi m a) = _
    rw [phi_class m hpr n hn0 hn a]
    show _ = (phi m (ring (2 * m + 1 + 1)).one * phi m (ring (2 * m + 1 + 1)).one + phi m n * phi m n) * (chi m a * chi m a)
      + (phi m (ring (2 * m + 1 + 1)).one * phi m (ring (2 * m + 1 + 1)).one + -1 * (phi m n * phi m n)) * chi m a
    lib_int
  show Z.mul 2 (sumK Z _ (L m)) = _
  rw [mul_sumK, sumK_ext Z _ _ pt, sumK_add, ← mul_sumK, ← mul_sumK, sum_chi_sq m hpr,
    show sumK Z (chi m) (L m) = 0 from chi_sum m hpr]
  show (phi m (ring (2 * m + 1 + 1)).one * phi m (ring (2 * m + 1 + 1)).one + phi m n * phi m n) * (P m + -1)
    + (phi m (ring (2 * m + 1 + 1)).one * phi m (ring (2 * m + 1 + 1)).one + -1 * (phi m n * phi m n)) * 0 = _
  lib_int

end JacobClass

#print axioms JacobClass.z01
#print axioms JacobClass.chi_cases
#print axioms JacobClass.chi_pw_one
#print axioms JacobClass.chi_ne_zero
#print axioms JacobClass.chi_sq_val
#print axioms JacobClass.chi_iv
#print axioms JacobClass.square_of
#print axioms JacobClass.phi_class
#print axioms JacobClass.sum_chi_sq
#print axioms JacobClass.grouped

lang/JacobMoment.lean

lean_all/lang/JacobMoment.lean

/-
  lang/JacobMoment — T43, cycle 44: the second moment of Jacobsthal sums, Σ_a φ(a)² = p (p − 1)(1 + χ(−1)).

  p = 2m + 3, sums over 𝔽_p with values in ℤ (LibInt.intR), P = p as an integer.
    sum_one          Σ_a 1 = p
    sq_delta         χ(z²) = 1 − δ(z, 0)
    inner            Σ_a χ((a + b)(a + c)) = −1 + p δ(b, c)        (ConicSum.conic_sum for b ≠ c)
    pair             χ(x(x² + a)) χ(y(y² + a)) = χ(xy) χ((a + x²)(a + y²))
    roots_sq         x ≠ 0 ⇒ δ(x², y²) = δ(y, x) + δ(y, −x)
    diag             Σ_x Σ_y χ(xy) δ(x², y²) = (p − 1)(1 + χ(−1))
    moment           Σ_a φ(a)² = p (p − 1)(1 + χ(−1))
  For p ≡ 1 (mod 4) (χ(−1) = 1): Σ_a φ(a)² = 2p(p − 1); with φ(a t²) = χ(t)φ(a) (Jacobsthal) this gives
  φ(1)² + φ(n)² = 4p (next cycle).

  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.Jacobsthal
import lang.Thue
import lang.Eisen

namespace JacobMoment

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum Jacobsthal

variable (m : Nat)

abbrev Z := LibInt.intR

def P : Int := Int.ofNat (2 * m + 1 + 1 + 1)

theorem sum_one_n : ∀ n, sumK Z (fun _ => (1 : Int)) (CMInert.allZ (2 * m + 1 + 1) n) = Int.ofNat n
  | 0 => rfl
  | n + 1 => by
    show sumK Z _ (CMInert.allZ (2 * m + 1 + 1) n ++ [mk _ n]) = _
    rw [CMInert.sumK_app', sum_one_n n]
    rfl

theorem sum_one : sumK Z (fun _ => (1 : Int)) (L m) = P m := sum_one_n m _

theorem delta_one (y z : Zp (2 * m + 1 + 1)) (h : y = z) : delta Z y z = 1 := ite_pos' _ _ h
theorem delta_zero (y z : Zp (2 * m + 1 + 1)) (h : y ≠ z) : delta Z y z = 0 := ite_neg' _ _ h

theorem sq_delta (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (z : Zp (2 * m + 1 + 1)) :
    chi m ((ring (2 * m + 1 + 1)).mul z z) = 1 + -1 * delta Z z (ring (2 * m + 1 + 1)).zero := by
  by_cases hz : z = (ring (2 * m + 1 + 1)).zero
  · rw [hz, DetChain.zero_mul', chi_zero, delta_one m _ _ rfl]; rfl
  · rw [chi_sq m hpr z hz, delta_zero m _ _ hz]; rfl

theorem sum_delta_shift (b : Zp (2 * m + 1 + 1)) : sumK Z (fun a => delta Z ((ring (2 * m + 1 + 1)).add a b) (ring (2 * m + 1 + 1)).zero) (L m) = 1 := by
  rw [shift_sum m (fun y => delta Z y (ring (2 * m + 1 + 1)).zero) b]
  exact CMInert.complete_allZ (2 * m + 1 + 1) Z (ring (2 * m + 1 + 1)).zero

/-- **Σ_a χ((a + b)(a + c)) = −1 + p δ(b, c).** -/
theorem inner (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (b c : Zp (2 * m + 1 + 1)) :
    sumK Z (fun a => chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add a b) ((ring (2 * m + 1 + 1)).add a c))) (L m) = -1 + P m * delta Z b c := by
  by_cases hbc : b = c
  · rw [← hbc, delta_one m _ _ rfl]
    rw [sumK_ext Z _ _ (fun a => sq_delta m hpr ((ring (2 * m + 1 + 1)).add a b))]
    show sumK Z (fun a => Z.add 1 (Z.mul (-1) (delta Z ((ring (2 * m + 1 + 1)).add a b) (ring (2 * m + 1 + 1)).zero))) (L m) = _
    rw [sumK_add, ← mul_sumK, sum_delta_shift, sum_one]
    show P m + -1 * 1 = -1 + P m * 1
    lib_int
  · rw [conic_sum m hpr b c hbc, delta_zero m _ _ hbc]
    lib_int

theorem pair (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y a : Zp (2 * m + 1 + 1)) :
    chi m ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) a)) * chi m ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) a))
      = chi m ((ring (2 * m + 1 + 1)).mul x y) * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).mul y y))) := by
  rw [← chi_mul m hpr, ← chi_mul m hpr]
  have e : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) a)) ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) a))
      = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).mul y y))) := by lib_ring
  rw [e]

theorem zsum {A : Type} : ∀ l : List A, sumK Z (fun _ => (0 : Int)) l = 0
  | [] => rfl
  | _ :: l => by
    show (0 : Int) + sumK Z (fun _ => (0 : Int)) l = 0
    rw [zsum l]; rfl

theorem compL : Complete Z (L m) := CMInert.complete_allZ (2 * m + 1 + 1) Z

theorem two_ne (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x : Zp (2 * m + 1 + 1))
    (hx : x ≠ (ring (2 * m + 1 + 1)).zero) : x ≠ (ring (2 * m + 1 + 1)).neg x := fun e => by
  have h2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) x = (ring (2 * m + 1 + 1)).zero := by
    have : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) x = (ring (2 * m + 1 + 1)).add x x := by lib_ring
    rw [this]; conv => lhs; rhs; rw [e]
    exact (ring (2 * m + 1 + 1)).add_neg x
  cases nozd (2 * m + 1) hpr _ _ h2 with
  | inl h =>
    have := congrArg Subtype.val h
    have v : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one).val = 2 := by
      show md (2 * m + 1 + 1) (md (2 * m + 1 + 1) 1 + md (2 * m + 1 + 1) 1) = 2
      rw [md_small _ 1 (Nat.succ_lt_succ (Nat.zero_lt_succ _))]
      exact md_small _ 2 (Nat.succ_lt_succ (Nat.succ_lt_succ (Nat.zero_lt_succ _)))
    rw [v] at this
    exact Nat.noConfusion this
  | inr h => exact hx h

theorem roots_sq (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1))
    (hx : x ≠ (ring (2 * m + 1 + 1)).zero) : delta Z ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul y y) = delta Z y x + delta Z y ((ring (2 * m + 1 + 1)).neg x) := by
  have hxn := two_ne m hpr x hx
  by_cases h1 : y = x
  · rw [h1, delta_one m _ _ rfl, delta_one m _ _ rfl, delta_zero m _ _ hxn]; rfl
  by_cases h2 : y = (ring (2 * m + 1 + 1)).neg x
  · rw [h2, Thue.negsq (ring (2 * m + 1 + 1)), delta_one m _ _ rfl, delta_one m _ _ rfl, delta_zero m _ _ (fun e => hxn e.symm)]; rfl
  · have hne : (ring (2 * m + 1 + 1)).mul x x ≠ (ring (2 * m + 1 + 1)).mul y y := fun e => by
      have f : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add y x) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x)) := by lib_ring
      rw [← e, (ring (2 * m + 1 + 1)).add_neg] at f
      cases nozd (2 * m + 1) hpr _ _ f with
      | inl h => exact h1 (Eisen.eq_of_sub_zero (2 * m + 1) y x h)
      | inr h =>
        apply h2
        have := congrArg (fun z => (ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg x)) h
        have e2 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add y x) ((ring (2 * m + 1 + 1)).neg x) = y := by rw [(ring (2 * m + 1 + 1)).add_assoc, (ring (2 * m + 1 + 1)).add_neg, DetChain.add_zero']
        rw [← e2]
        exact this.trans ((ring (2 * m + 1 + 1)).zero_add _)
    rw [delta_zero m _ _ hne, delta_zero m _ _ h1, delta_zero m _ _ h2]; rfl

theorem chi_neg_sq (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x : Zp (2 * m + 1 + 1))
    (hx : x ≠ (ring (2 * m + 1 + 1)).zero) : chi m ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).neg x)) = chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by
  have e : (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).neg x) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul x x) := by lib_ring
  rw [e, chi_mul m hpr, chi_sq m hpr x hx]; lib_int

theorem diag_x (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x : Zp (2 * m + 1 + 1)) :
    sumK Z (fun y => chi m ((ring (2 * m + 1 + 1)).mul x y) * delta Z ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul y y)) (L m)
      = (1 + chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (1 + -1 * delta Z x (ring (2 * m + 1 + 1)).zero) := by
  by_cases hx : x = (ring (2 * m + 1 + 1)).zero
  · rw [hx, delta_one m _ _ rfl]
    have : sumK Z (fun y => chi m ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero y) * delta Z ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).mul y y)) (L m) = 0 := by
      rw [sumK_ext Z _ (fun _ => (0 : Int)) (fun y => by rw [DetChain.zero_mul', chi_zero]; lib_int)]
      exact zsum (L m)
    rw [this]; lib_int
  · rw [delta_zero m _ _ hx]
    have pt : ∀ y, chi m ((ring (2 * m + 1 + 1)).mul x y) * delta Z ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul y y)
        = Z.add (Z.mul (delta Z y x) (chi m ((ring (2 * m + 1 + 1)).mul x y))) (Z.mul (delta Z y ((ring (2 * m + 1 + 1)).neg x)) (chi m ((ring (2 * m + 1 + 1)).mul x y))) := fun y => by
      rw [roots_sq m hpr x y hx]
      show _ = delta Z y x * chi m _ + delta Z y ((ring (2 * m + 1 + 1)).neg x) * chi m _
      lib_int
    rw [sumK_ext Z _ _ pt, sumK_add, pick Z (L m) (compL m) (fun y => chi m ((ring (2 * m + 1 + 1)).mul x y)) x,
      pick Z (L m) (compL m) (fun y => chi m ((ring (2 * m + 1 + 1)).mul x y)) ((ring (2 * m + 1 + 1)).neg x), chi_sq m hpr x hx,
      chi_neg_sq m hpr x hx]
    show 1 + chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = _
    lib_int

theorem diag (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) :
    sumK Z (fun x => sumK Z (fun y => chi m ((ring (2 * m + 1 + 1)).mul x y) * delta Z ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul y y)) (L m)) (L m)
      = (1 + chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (P m + -1) := by
  rw [sumK_ext Z _ _ (diag_x m hpr)]
  show sumK Z (fun x => Z.mul (1 + chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (Z.add 1 (Z.mul (-1) (delta Z x (ring (2 * m + 1 + 1)).zero)))) (L m) = _
  rw [← mul_sumK, sumK_add, ← mul_sumK, sum_one, show sumK Z (fun x => delta Z x (ring (2 * m + 1 + 1)).zero) (L m) = 1 from compL m (ring (2 * m + 1 + 1)).zero]
  show (1 + chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (P m + -1 * 1) = _
  lib_int

theorem sum_chi_xy (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x : Zp (2 * m + 1 + 1)) :
    sumK Z (fun y => chi m ((ring (2 * m + 1 + 1)).mul x y)) (L m) = 0 := by
  rw [sumK_ext Z _ (fun y => Z.mul (chi m x) (chi m y)) (fun y => chi_mul m hpr x y), ← mul_sumK,
    show sumK Z (chi m) (L m) = 0 from chi_sum m hpr]
  show chi m x * 0 = 0
  lib_int

/-- **Σ_a φ(a)² = p (p − 1)(1 + χ(−1)).** -/
theorem moment (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) :
    sumK Z (fun a => phi m a * phi m a) (L m) = P m * ((1 + chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (P m + -1)) := by
  -- expand the square
  have e1 : ∀ a, phi m a * phi m a = sumK Z (fun x => sumK Z (fun y =>
      chi m ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) a)) * chi m ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) a))) (L m)) (L m) := fun a => by
    show Z.mul (sumK Z _ (L m)) (sumK Z _ (L m)) = _
    rw [sumK_mul]
    exact sumK_ext Z _ _ (fun x => mul_sumK Z _ _ (L m)) _
  rw [sumK_ext Z _ _ e1, sumK_swap]
  -- swap a and y for each x, then use pair and inner
  have e2 : ∀ x, sumK Z (fun a => sumK Z (fun y =>
      chi m ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) a)) * chi m ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) a))) (L m)) (L m)
      = sumK Z (fun y => Z.add (Z.mul (-1) (chi m ((ring (2 * m + 1 + 1)).mul x y)))
          (Z.mul (P m) (chi m ((ring (2 * m + 1 + 1)).mul x y) * delta Z ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul y y)))) (L m) := fun x => by
    rw [sumK_swap]
    apply sumK_ext
    intro y
    rw [sumK_ext Z _ (fun a => Z.mul (chi m ((ring (2 * m + 1 + 1)).mul x y))
      (chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).mul y y))))) (fun a => pair m hpr x y a),
      ← mul_sumK, inner m hpr]
    show chi m ((ring (2 * m + 1 + 1)).mul x y) * (-1 + P m * delta Z ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul y y)) = _
    show _ = -1 * chi m ((ring (2 * m + 1 + 1)).mul x y) + P m * (chi m ((ring (2 * m + 1 + 1)).mul x y) * delta Z ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul y y))
    lib_int
  rw [sumK_ext Z _ _ e2]
  have e3 : ∀ x, sumK Z (fun y => Z.add (Z.mul (-1) (chi m ((ring (2 * m + 1 + 1)).mul x y)))
      (Z.mul (P m) (chi m ((ring (2 * m + 1 + 1)).mul x y) * delta Z ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul y y)))) (L m)
      = Z.mul (P m) (sumK Z (fun y => chi m ((ring (2 * m + 1 + 1)).mul x y) * delta Z ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul y y)) (L m)) := fun x => by
    rw [sumK_add, ← mul_sumK, ← mul_sumK, sum_chi_xy m hpr x]
    show -1 * 0 + _ = _
    lib_int
  rw [sumK_ext Z _ _ e3, ← mul_sumK, diag m hpr]
  rfl

end JacobMoment

#print axioms JacobMoment.sum_one_n
#print axioms JacobMoment.sum_one
#print axioms JacobMoment.delta_one
#print axioms JacobMoment.delta_zero
#print axioms JacobMoment.sq_delta
#print axioms JacobMoment.sum_delta_shift
#print axioms JacobMoment.inner
#print axioms JacobMoment.pair
#print axioms JacobMoment.zsum
#print axioms JacobMoment.compL
#print axioms JacobMoment.two_ne
#print axioms JacobMoment.roots_sq
#print axioms JacobMoment.chi_neg_sq
#print axioms JacobMoment.diag_x
#print axioms JacobMoment.diag
#print axioms JacobMoment.sum_chi_xy
#print axioms JacobMoment.moment

lang/JacobiD.lean

lean_all/lang/JacobiD.lean

/-
  lang/JacobiD — T47, cycle 550: the z·d/dz of the finite triple product, toward Jacobi's η³.

    em f z L        Σ_{m<L} m·f(m) z^m
    em_rec          for the three-term recursion b′ = A b + C b(·−1) + A b(·−2):
                    em b′ (L+2) = A·em b (L+2) + Cz·(em b (L+1) + es b (L+1)) + Az²·(em b L + 2·es b L)
    dj              D_N := em (bj N) z (2N+1) satisfies D_(N+1) = f_N(z)·D_N + (Cz + 2Az²)·jf N
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.GaussMod

namespace QBinom

open FTAChain

variable {R : Type} (K : DetChain.CRing R)

def em (f : Nat → R) (z : R) : Nat → R
  | 0 => K.zero
  | L + 1 => K.add (em f z L) (K.mul (Cheb.emb K L) (K.mul (f L) (Frobenius.pw K z L)))

theorem em_rec (b b' : Nat → R) (A C z : R) (h0 : b' 0 = K.mul A (b 0)) (h1 : b' 1 = K.add (K.mul A (b 1)) (K.mul C (b 0)))
    (h2 : ∀ m, b' (m + 2) = K.add (K.add (K.mul A (b (m + 2))) (K.mul C (b (m + 1)))) (K.mul A (b m))) : ∀ L,
    em K b' z (L + 2) = K.add (K.add (K.mul A (em K b z (L + 2))) (K.mul (K.mul C z) (K.add (em K b z (L + 1)) (es K b z (L + 1)))))
      (K.mul (K.mul A (K.mul z z)) (K.add (em K b z L) (K.add (es K b z L) (es K b z L))))
  | 0 => by
    show K.add (K.add K.zero (K.mul (Cheb.emb K 0) (K.mul (b' 0) (Frobenius.pw K z 0)))) (K.mul (Cheb.emb K 1) (K.mul (b' 1) (Frobenius.pw K z 1))) = _
    rw [h0, h1]
    show K.add (K.add K.zero (K.mul K.zero (K.mul (K.mul A (b 0)) K.one))) (K.mul (K.add K.zero K.one) (K.mul (K.add (K.mul A (b 1)) (K.mul C (b 0))) (K.mul K.one z))) =
      K.add (K.add (K.mul A (K.add (K.add K.zero (K.mul K.zero (K.mul (b 0) K.one))) (K.mul (K.add K.zero K.one) (K.mul (b 1) (K.mul K.one z)))))
        (K.mul (K.mul C z) (K.add (K.add K.zero (K.mul K.zero (K.mul (b 0) K.one))) (K.add K.zero (K.mul (b 0) K.one)))))
        (K.mul (K.mul A (K.mul z z)) (K.add K.zero (K.add K.zero K.zero)))
    lib_ring
  | L + 1 => by
    show K.add (em K b' z (L + 2)) (K.mul (Cheb.emb K (L + 2)) (K.mul (b' (L + 2)) (Frobenius.pw K z (L + 2)))) =
      K.add (K.add (K.mul A (K.add (em K b z (L + 2)) (K.mul (Cheb.emb K (L + 2)) (K.mul (b (L + 2)) (Frobenius.pw K z (L + 2))))))
        (K.mul (K.mul C z) (K.add (K.add (em K b z (L + 1)) (K.mul (Cheb.emb K (L + 1)) (K.mul (b (L + 1)) (Frobenius.pw K z (L + 1)))))
          (K.add (es K b z (L + 1)) (K.mul (b (L + 1)) (Frobenius.pw K z (L + 1)))))))
        (K.mul (K.mul A (K.mul z z)) (K.add (K.add (em K b z L) (K.mul (Cheb.emb K L) (K.mul (b L) (Frobenius.pw K z L))))
          (K.add (K.add (es K b z L) (K.mul (b L) (Frobenius.pw K z L))) (K.add (es K b z L) (K.mul (b L) (Frobenius.pw K z L))))))
    rw [em_rec b b' A C z h0 h1 h2 L, h2 L]
    have e2 : Frobenius.pw K z (L + 2) = K.mul (K.mul (Frobenius.pw K z L) z) z := rfl
    have e1 : Frobenius.pw K z (L + 1) = K.mul (Frobenius.pw K z L) z := rfl
    have n2 : Cheb.emb K (L + 2) = K.add (K.add (Cheb.emb K L) K.one) K.one := rfl
    have n1 : Cheb.emb K (L + 1) = K.add (Cheb.emb K L) K.one := rfl
    rw [e2, e1, n2, n1]
    generalize Frobenius.pw K z L = P
    generalize Cheb.emb K L = E
    lib_ring

variable (q : R)

theorem dj (z : R) (N : Nat) :
    em K (bj K q (N + 1)) z (2 * (N + 1) + 1) =
      K.add (K.mul (K.add (K.add (Frobenius.pw K q (2 * N + 1)) (K.mul (K.add K.one (Frobenius.pw K q (4 * N + 2))) z))
        (K.mul (Frobenius.pw K q (2 * N + 1)) (K.mul z z))) (em K (bj K q N) z (2 * N + 1)))
      (K.mul (K.add (K.mul (K.add K.one (Frobenius.pw K q (4 * N + 2))) z) (K.mul (K.add (Frobenius.pw K q (2 * N + 1)) (Frobenius.pw K q (2 * N + 1))) (K.mul z z)))
        (jf K q z N)) := by
  have hr := em_rec K (bj K q N) (bj K q (N + 1)) (Frobenius.pw K q (2 * N + 1)) (K.add K.one (Frobenius.pw K q (4 * N + 2))) z
    rfl rfl (fun m => rfl) (2 * N + 1)
  rw [show 2 * (N + 1) + 1 = 2 * N + 1 + 2 by lib_nat, hr]
  have hz : ∀ t, bj K q N (2 * N + 1 + t) = K.zero := fun t => bj_big K q N _ (Nat.lt_of_lt_of_le (Nat.lt_succ_self _) (Nat.le_add_right _ t))
  have emx : ∀ t, em K (bj K q N) z (2 * N + 1 + t) = em K (bj K q N) z (2 * N + 1) := by
    intro t; induction t with
    | zero => rfl
    | succ t ih =>
      show K.add (em K (bj K q N) z (2 * N + 1 + t)) (K.mul _ (K.mul (bj K q N (2 * N + 1 + t)) _)) = _
      rw [ih, hz t]; lib_ring
  rw [show 2 * N + 1 + 2 = (2 * N + 1) + 2 from rfl, emx 2, emx 1, es_ext K _ z _ hz 1, ev_jtp]
  lib_ring

/-- f_N(−q) = −q(1 − q^(2N))(1 − q^(2N+2)). -/
theorem f_at (N : Nat) : K.add (K.add (Frobenius.pw K q (2 * N + 1)) (K.mul (K.add K.one (Frobenius.pw K q (4 * N + 2))) (K.neg q)))
      (K.mul (Frobenius.pw K q (2 * N + 1)) (K.mul (K.neg q) (K.neg q))) =
    K.mul (K.neg q) (K.mul (K.add K.one (K.neg (Frobenius.pw K q (2 * N)))) (K.add K.one (K.neg (Frobenius.pw K q (2 * N + 2))))) := by
  have e1 : Frobenius.pw K q (2 * N + 1) = K.mul (Frobenius.pw K q (2 * N)) q := rfl
  have e2 : Frobenius.pw K q (2 * N + 2) = K.mul (K.mul (Frobenius.pw K q (2 * N)) q) q := rfl
  have e4 : Frobenius.pw K q (4 * N + 2) = K.mul (K.mul (K.mul (Frobenius.pw K q (2 * N)) (Frobenius.pw K q (2 * N))) q) q := by
    rw [show 4 * N + 2 = (2 * N + 2 * N) + 1 + 1 by lib_nat]
    show K.mul (K.mul (Frobenius.pw K q (2 * N + 2 * N)) q) q = _
    rw [HasseCM.pw_add']
  rw [e1, e2, e4]
  generalize Frobenius.pw K q (2 * N) = P
  lib_ring

theorem jf_zero (N : Nat) : jf K q (K.neg q) (N + 1) = K.zero := by
  induction N with
  | zero =>
    show K.mul K.one _ = K.zero
    rw [f_at K q 0]; show K.mul K.one (K.mul (K.neg q) (K.mul (K.add K.one (K.neg K.one)) _)) = K.zero; lib_ring
  | succ N ih => show K.mul (jf K q (K.neg q) (N + 1)) _ = K.zero; rw [ih]; lib_ring

/-- D_(N+1)(−q) by recursion: jd 0 = −q(1 − q²), jd (N+1) = jd N · f_(N+1)(−q). -/
def jd : Nat → R
  | 0 => K.mul (K.neg q) (K.add K.one (K.neg (K.mul q q)))
  | N + 1 => K.mul (jd N) (K.mul (K.neg q) (K.mul (K.add K.one (K.neg (Frobenius.pw K q (2 * (N + 1)))))
      (K.add K.one (K.neg (Frobenius.pw K q (2 * (N + 1) + 2))))))

theorem dj_at : ∀ N, em K (bj K q (N + 1)) (K.neg q) (2 * (N + 1) + 1) = jd K q N
  | 0 => by
    rw [dj]
    show K.add (K.mul _ (K.add K.zero (K.mul (Cheb.emb K 0) (K.mul (bj K q 0 0) (Frobenius.pw K (K.neg q) 0))))) (K.mul _ K.one) = _
    show K.add (K.mul (K.add (K.add (K.mul K.one q) (K.mul (K.add K.one (K.mul (K.mul K.one q) q)) (K.neg q))) (K.mul (K.mul K.one q) (K.mul (K.neg q) (K.neg q))))
      (K.add K.zero (K.mul K.zero (K.mul K.one K.one))))
      (K.mul (K.add (K.mul (K.add K.one (K.mul (K.mul K.one q) q)) (K.neg q)) (K.mul (K.add (K.mul K.one q) (K.mul K.one q)) (K.mul (K.neg q) (K.neg q)))) K.one) =
      K.mul (K.neg q) (K.add K.one (K.neg (K.mul q q)))
    lib_ring
  | N + 1 => by
    rw [dj, jf_zero, dj_at N, f_at]
    show _ = K.mul (jd K q N) _
    rw [show 2 * (N + 1) + 2 = 2 * (N + 1) + 2 from rfl]
    lib_ring

end QBinom

#print axioms QBinom.em_rec
#print axioms QBinom.dj
#print axioms QBinom.f_at
#print axioms QBinom.jf_zero
#print axioms QBinom.dj_at

lang/JacobiE.lean

lean_all/lang/JacobiE.lean

/-
  lang/JacobiE — T47, cycle 554: value and x·d/dx of the shifted product ∏_{k=1}^{N}(1 + xq^(2k))(x + q^(2k−2)), and both at x = −1.

    es_rec3, em_rec3    sum and weighted-sum recursions for b′ = A b + C b(·−1) + B b(·−2)
    dd_big              dd N m = 0 for m > 2N
    ev_dd               es (dd N) x (2N+1) = jg x N = ∏_{k<N} (q^(2k) + (1 + q^(4k+2))x + q^(2k+2)x²)
    g_at                the k-th factor at x = −1 is −(1 − q^(2k))(1 − q^(2k+2)); it is 0 for k = 0
    dd_at               Σ_m m·dd (N+1) m·(−1)^m = jd2 N = −(1 − q²)·∏_{k=1}^{N} (−(1 − q^(2k))(1 − q^(2k+2)))
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.JacobiS

namespace QBinom

open FTAChain

variable {R : Type} (K : DetChain.CRing R)

theorem es_rec3 (b b' : Nat → R) (A C B z : R) (h0 : b' 0 = K.mul A (b 0)) (h1 : b' 1 = K.add (K.mul A (b 1)) (K.mul C (b 0)))
    (h2 : ∀ m, b' (m + 2) = K.add (K.add (K.mul A (b (m + 2))) (K.mul C (b (m + 1)))) (K.mul B (b m))) : ∀ L,
    es K b' z (L + 2) = K.add (K.add (K.mul A (es K b z (L + 2))) (K.mul (K.mul C z) (es K b z (L + 1))))
      (K.mul (K.mul B (K.mul z z)) (es K b z L))
  | 0 => by
    show K.add (K.add K.zero (K.mul (b' 0) (Frobenius.pw K z 0))) (K.mul (b' 1) (Frobenius.pw K z 1)) =
      K.add (K.add (K.mul A (K.add (K.add K.zero (K.mul (b 0) (Frobenius.pw K z 0))) (K.mul (b 1) (Frobenius.pw K z 1))))
        (K.mul (K.mul C z) (K.add K.zero (K.mul (b 0) (Frobenius.pw K z 0))))) (K.mul (K.mul B (K.mul z z)) K.zero)
    rw [h0, h1]
    show K.add (K.add K.zero (K.mul (K.mul A (b 0)) K.one)) (K.mul (K.add (K.mul A (b 1)) (K.mul C (b 0))) (K.mul K.one z)) =
      K.add (K.add (K.mul A (K.add (K.add K.zero (K.mul (b 0) K.one)) (K.mul (b 1) (K.mul K.one z))))
        (K.mul (K.mul C z) (K.add K.zero (K.mul (b 0) K.one)))) (K.mul (K.mul B (K.mul z z)) K.zero)
    lib_ring
  | L + 1 => by
    show K.add (es K b' z (L + 2)) (K.mul (b' (L + 2)) (Frobenius.pw K z (L + 2))) =
      K.add (K.add (K.mul A (K.add (es K b z (L + 2)) (K.mul (b (L + 2)) (Frobenius.pw K z (L + 2)))))
        (K.mul (K.mul C z) (K.add (es K b z (L + 1)) (K.mul (b (L + 1)) (Frobenius.pw K z (L + 1))))))
        (K.mul (K.mul B (K.mul z z)) (K.add (es K b z L) (K.mul (b L) (Frobenius.pw K z L))))
    rw [es_rec3 b b' A C B z h0 h1 h2 L, h2 L]
    have e2 : Frobenius.pw K z (L + 2) = K.mul (K.mul (Frobenius.pw K z L) z) z := rfl
    have e1 : Frobenius.pw K z (L + 1) = K.mul (Frobenius.pw K z L) z := rfl
    rw [e2, e1]
    generalize Frobenius.pw K z L = P
    lib_ring

theorem em_rec3 (b b' : Nat → R) (A C B z : R) (h0 : b' 0 = K.mul A (b 0)) (h1 : b' 1 = K.add (K.mul A (b 1)) (K.mul C (b 0)))
    (h2 : ∀ m, b' (m + 2) = K.add (K.add (K.mul A (b (m + 2))) (K.mul C (b (m + 1)))) (K.mul B (b m))) : ∀ L,
    em K b' z (L + 2) = K.add (K.add (K.mul A (em K b z (L + 2))) (K.mul (K.mul C z) (K.add (em K b z (L + 1)) (es K b z (L + 1)))))
      (K.mul (K.mul B (K.mul z z)) (K.add (em K b z L) (K.add (es K b z L) (es K b z L))))
  | 0 => by
    show K.add (K.add K.zero (K.mul (Cheb.emb K 0) (K.mul (b' 0) (Frobenius.pw K z 0)))) (K.mul (Cheb.emb K 1) (K.mul (b' 1) (Frobenius.pw K z 1))) = _
    rw [h0, h1]
    show K.add (K.add K.zero (K.mul K.zero (K.mul (K.mul A (b 0)) K.one))) (K.mul (K.add K.zero K.one) (K.mul (K.add (K.mul A (b 1)) (K.mul C (b 0))) (K.mul K.one z))) =
      K.add (K.add (K.mul A (K.add (K.add K.zero (K.mul K.zero (K.mul (b 0) K.one))) (K.mul (K.add K.zero K.one) (K.mul (b 1) (K.mul K.one z)))))
        (K.mul (K.mul C z) (K.add (K.add K.zero (K.mul K.zero (K.mul (b 0) K.one))) (K.add K.zero (K.mul (b 0) K.one)))))
        (K.mul (K.mul B (K.mul z z)) (K.add K.zero (K.add K.zero K.zero)))
    lib_ring
  | L + 1 => by
    show K.add (em K b' z (L + 2)) (K.mul (Cheb.emb K (L + 2)) (K.mul (b' (L + 2)) (Frobenius.pw K z (L + 2)))) =
      K.add (K.add (K.mul A (K.add (em K b z (L + 2)) (K.mul (Cheb.emb K (L + 2)) (K.mul (b (L + 2)) (Frobenius.pw K z (L + 2))))))
        (K.mul (K.mul C z) (K.add (K.add (em K b z (L + 1)) (K.mul (Cheb.emb K (L + 1)) (K.mul (b (L + 1)) (Frobenius.pw K z (L + 1)))))
          (K.add (es K b z (L + 1)) (K.mul (b (L + 1)) (Frobenius.pw K z (L + 1)))))))
        (K.mul (K.mul B (K.mul z z)) (K.add (K.add (em K b z L) (K.mul (Cheb.emb K L) (K.mul (b L) (Frobenius.pw K z L))))
          (K.add (K.add (es K b z L) (K.mul (b L) (Frobenius.pw K z L))) (K.add (es K b z L) (K.mul (b L) (Frobenius.pw K z L))))))
    rw [em_rec3 b b' A C B z h0 h1 h2 L, h2 L]
    have e2 : Frobenius.pw K z (L + 2) = K.mul (K.mul (Frobenius.pw K z L) z) z := rfl
    have e1 : Frobenius.pw K z (L + 1) = K.mul (Frobenius.pw K z L) z := rfl
    have n2 : Cheb.emb K (L + 2) = K.add (K.add (Cheb.emb K L) K.one) K.one := rfl
    have n1 : Cheb.emb K (L + 1) = K.add (Cheb.emb K L) K.one := rfl
    rw [e2, e1, n2, n1]
    generalize Frobenius.pw K z L = P
    generalize Cheb.emb K L = E
    lib_ring

variable (q : R)

theorem dd_big (N m : Nat) (h : 2 * N < m) : dd K q N m = K.zero := by
  show K.mul (gb K (K.mul q q) (2 * N) m) _ = K.zero
  rw [gb_big K _ _ _ h]; lib_ring

/-- The k-th factor q^(2k) + (1 + q^(4k+2))x + q^(2k+2)x². -/
def gf (x : R) (N : Nat) : R :=
  K.add (K.add (Frobenius.pw K q (2 * N)) (K.mul (K.add K.one (Frobenius.pw K q (4 * N + 2))) x)) (K.mul (Frobenius.pw K q (2 * N + 2)) (K.mul x x))

def jg (x : R) : Nat → R
  | 0 => K.one
  | N + 1 => K.mul (jg x N) (gf K q x N)

theorem dd_ext (N : Nat) : ∀ t, dd K q N (2 * N + 1 + t) = K.zero :=
  fun t => dd_big K q N _ (Nat.lt_of_lt_of_le (Nat.lt_succ_self _) (Nat.le_add_right _ t))

theorem ev_dd (x : R) : ∀ N, es K (dd K q N) x (2 * N + 1) = jg K q x N
  | 0 => by show K.add K.zero (K.mul (K.mul K.one K.one) K.one) = K.one; lib_ring
  | N + 1 => by
    have hr := es_rec3 K (dd K q N) (dd K q (N + 1)) (Frobenius.pw K q (2 * N)) (K.add K.one (Frobenius.pw K q (4 * N + 2)))
      (Frobenius.pw K q (2 * N + 2)) x (dd_h0 K q N) (dd_h1 K q N) (dd_h2 K q N) (2 * N + 1)
    rw [show 2 * (N + 1) + 1 = 2 * N + 1 + 2 by lib_nat, hr]
    rw [show 2 * N + 1 + 2 = (2 * N + 1) + 2 from rfl, es_ext K _ x _ (dd_ext K q N) 2, es_ext K _ x _ (dd_ext K q N) 1, ev_dd x N]
    show _ = K.mul (jg K q x N) (K.add (K.add (Frobenius.pw K q (2 * N)) (K.mul (K.add K.one (Frobenius.pw K q (4 * N + 2))) x))
      (K.mul (Frobenius.pw K q (2 * N + 2)) (K.mul x x)))
    lib_ring

theorem em_dd (x : R) (N : Nat) :
    em K (dd K q (N + 1)) x (2 * (N + 1) + 1) =
      K.add (K.mul (gf K q x N) (em K (dd K q N) x (2 * N + 1)))
        (K.mul (K.add (K.mul (K.add K.one (Frobenius.pw K q (4 * N + 2))) x) (K.mul (K.add (Frobenius.pw K q (2 * N + 2)) (Frobenius.pw K q (2 * N + 2))) (K.mul x x)))
          (jg K q x N)) := by
  have hr := em_rec3 K (dd K q N) (dd K q (N + 1)) (Frobenius.pw K q (2 * N)) (K.add K.one (Frobenius.pw K q (4 * N + 2)))
    (Frobenius.pw K q (2 * N + 2)) x (dd_h0 K q N) (dd_h1 K q N) (dd_h2 K q N) (2 * N + 1)
  rw [show 2 * (N + 1) + 1 = 2 * N + 1 + 2 by lib_nat, hr]
  have emx : ∀ t, em K (dd K q N) x (2 * N + 1 + t) = em K (dd K q N) x (2 * N + 1) := by
    intro t; induction t with
    | zero => rfl
    | succ t ih =>
      show K.add (em K (dd K q N) x (2 * N + 1 + t)) (K.mul _ (K.mul (dd K q N (2 * N + 1 + t)) _)) = _
      rw [ih, dd_ext K q N t]; lib_ring
  rw [show 2 * N + 1 + 2 = (2 * N + 1) + 2 from rfl, emx 2, emx 1, es_ext K _ x _ (dd_ext K q N) 1, ev_dd]
  show _ = K.add (K.mul (K.add (K.add (Frobenius.pw K q (2 * N)) (K.mul (K.add K.one (Frobenius.pw K q (4 * N + 2))) x))
    (K.mul (Frobenius.pw K q (2 * N + 2)) (K.mul x x))) _) _
  lib_ring

theorem g_at (N : Nat) : gf K q (K.neg K.one) N =
    K.neg (K.mul (K.add K.one (K.neg (Frobenius.pw K q (2 * N)))) (K.add K.one (K.neg (Frobenius.pw K q (2 * N + 2))))) := by
  show K.add (K.add (Frobenius.pw K q (2 * N)) (K.mul (K.add K.one (Frobenius.pw K q (4 * N + 2))) (K.neg K.one)))
    (K.mul (Frobenius.pw K q (2 * N + 2)) (K.mul (K.neg K.one) (K.neg K.one))) = _
  have e2 : Frobenius.pw K q (2 * N + 2) = K.mul (K.mul (Frobenius.pw K q (2 * N)) q) q := rfl
  have e4 : Frobenius.pw K q (4 * N + 2) = K.mul (K.mul (K.mul (Frobenius.pw K q (2 * N)) (Frobenius.pw K q (2 * N))) q) q := by
    rw [show 4 * N + 2 = (2 * N + 2 * N) + 1 + 1 by lib_nat]
    show K.mul (K.mul (Frobenius.pw K q (2 * N + 2 * N)) q) q = _
    rw [HasseCM.pw_add']
  rw [e2, e4]
  generalize Frobenius.pw K q (2 * N) = P
  lib_ring

theorem jg_zero (N : Nat) : jg K q (K.neg K.one) (N + 1) = K.zero := by
  induction N with
  | zero =>
    show K.mul K.one (gf K q (K.neg K.one) 0) = K.zero
    rw [g_at]; show K.mul K.one (K.neg (K.mul (K.add K.one (K.neg K.one)) _)) = K.zero; lib_ring
  | succ N ih => show K.mul (jg K q (K.neg K.one) (N + 1)) _ = K.zero; rw [ih]; lib_ring

def jd2 : Nat → R
  | 0 => K.neg (K.add K.one (K.neg (K.mul q q)))
  | N + 1 => K.mul (jd2 N) (K.neg (K.mul (K.add K.one (K.neg (Frobenius.pw K q (2 * (N + 1)))))
      (K.add K.one (K.neg (Frobenius.pw K q (2 * (N + 1) + 2))))))

theorem dd_at : ∀ N, em K (dd K q (N + 1)) (K.neg K.one) (2 * (N + 1) + 1) = jd2 K q N
  | 0 => by
    rw [em_dd]
    show K.add (K.mul _ (K.add K.zero (K.mul K.zero _))) (K.mul (K.add (K.mul (K.add K.one (K.mul (K.mul K.one q) q)) (K.neg K.one))
      (K.mul (K.add (K.mul (K.mul K.one q) q) (K.mul (K.mul K.one q) q)) (K.mul (K.neg K.one) (K.neg K.one)))) K.one) =
      K.neg (K.add K.one (K.neg (K.mul q q)))
    lib_ring
  | N + 1 => by
    rw [em_dd, jg_zero, dd_at N, g_at]
    show _ = K.mul (jd2 K q N) _
    lib_ring

end QBinom

#print axioms QBinom.es_rec3
#print axioms QBinom.em_rec3
#print axioms QBinom.dd_big
#print axioms QBinom.dd_ext
#print axioms QBinom.ev_dd
#print axioms QBinom.em_dd
#print axioms QBinom.g_at
#print axioms QBinom.jg_zero
#print axioms QBinom.dd_at

lang/JacobiGauss.lean

lean_all/lang/JacobiGauss.lean

/-
  lang/JacobiGauss — T49, cycle 604: **G_χ G_λ = J(χ, λ) G_χλ** for multiplicative χ, λ : 𝔽_p → E with χλ nontrivial
  (Σ_a χ(a)λ(a) = 0) and χ(0) = 0 (coefficientwise in E[C_p]); J(χ, λ) = Σ_t χ(t) λ(1 − t).
  Iterated with GaussNormN this expresses G_ψ^n through the Jacobi sums J(ψ, ψ^k): the order-n part of n-th power reciprocity.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.GaussNormN

namespace CharSumGen

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

variable {S : Type} (E : CRing S) (j : Nat)

def J2 (χ μ : Zp (j + 1 + 1) → S) : S :=
  sumK E (fun t => E.mul (χ t) (μ ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg t)))) (LL j)

theorem jacobi_gauss (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (χ μ : Zp (j + 1 + 1) → S)
    (hm : ∀ x y, χ ((ring (j + 1 + 1)).mul x y) = E.mul (χ x) (χ y)) (hm' : ∀ x y, μ ((ring (j + 1 + 1)).mul x y) = E.mul (μ x) (μ y))
    (h0 : χ (ring (j + 1 + 1)).zero = E.zero) (hs : sumK E (fun a => E.mul (χ a) (μ a)) (LL j) = E.zero) (c : Zp (j + 1 + 1)) :
    gt E (j + 1 + 1) (gmul E (j + 1 + 1) (Gv j χ) (Gv j μ)) c = E.mul (J2 E j χ μ) (E.mul (χ c) (μ c)) := by
  rw [gt_mul]
  have e1 : sumK E (fun a => E.mul (gt E (j + 1 + 1) (Gv j χ) a) (gt E (j + 1 + 1) (Gv j μ) ((ring (j + 1 + 1)).add c ((ring (j + 1 + 1)).neg a)))) (GroupRing.L (j + 1 + 1))
      = sumK E (fun a => E.mul (χ a) (μ ((ring (j + 1 + 1)).add c ((ring (j + 1 + 1)).neg a)))) (LL j) :=
    sumK_ext E _ _ (fun a => by unfold Gv; rw [gt_tb, gt_tb]) _
  rw [e1]
  by_cases hc : c = (ring (j + 1 + 1)).zero
  · rw [hc, h0]
    have e2 : ∀ a, E.mul (χ a) (μ ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).zero ((ring (j + 1 + 1)).neg a))) =
        E.mul (μ ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)) (E.mul (χ a) (μ a)) := fun a => by
      rw [show (ring (j + 1 + 1)).add (ring (j + 1 + 1)).zero ((ring (j + 1 + 1)).neg a) = (ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) a by lib_ring, hm']
      lib_ring
    rw [sumK_ext E _ _ e2, ← mul_sumK, hs]; lib_ring
  · rw [← sum_scale E j hpr _ c hc]
    unfold J2; rw [sumK_mul]
    apply sumK_ext; intro t
    rw [show (ring (j + 1 + 1)).add c ((ring (j + 1 + 1)).neg ((ring (j + 1 + 1)).mul c t)) =
      (ring (j + 1 + 1)).mul c ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg t)) by lib_ring, hm, hm']
    lib_ring

end CharSumGen

#print axioms CharSumGen.jacobi_gauss

lang/JacobiL.lean

lean_all/lang/JacobiL.lean

/-
  lang/JacobiL — T47, cycle 555: Jacobi's weighted sum modulo q^M: in every commutative ring with q^M = 0, N = 2M + 2,
    Σ_{m ≤ 2N} m·(−1)^m·q^(Ex2 N m) = sg (2M+1) · ((q²; q²)_(M+1))³,    Ex2 N m = j² + j (j = m − N), sg n = (−1)^(n+1).
    sg, jd2_pp     jd2 n = sg n · (q²;q²)_n · (q²;q²)_(n+1)
    e2_ge_l/r      Ex2 N m ≥ N − (m + 1), ≥ m − N
    jacobi3_mod
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.JacobiE

namespace QBinom

open FTAChain

variable {R : Type} (K : DetChain.CRing R)

def sg : Nat → R
  | 0 => K.neg K.one
  | n + 1 => K.neg (sg n)

theorem jd2_pp (q : R) : ∀ n, jd2 K q n = K.mul (sg K n) (K.mul (pp K (K.mul q q) n) (pp K (K.mul q q) (n + 1)))
  | 0 => by
    show K.neg (K.add K.one (K.neg (K.mul q q))) = K.mul (K.neg K.one) (K.mul K.one (K.mul K.one (K.add K.one (K.neg (K.mul K.one (K.mul q q))))))
    lib_ring
  | n + 1 => by
    show K.mul (jd2 K q n) (K.neg (K.mul (K.add K.one (K.neg (Frobenius.pw K q (2 * (n + 1))))) (K.add K.one (K.neg (Frobenius.pw K q (2 * (n + 1) + 2)))))) =
      K.mul (K.neg (sg K n)) (K.mul (K.mul (pp K (K.mul q q) n) (K.add K.one (K.neg (Frobenius.pw K (K.mul q q) (n + 1)))))
        (K.mul (pp K (K.mul q q) (n + 1)) (K.add K.one (K.neg (Frobenius.pw K (K.mul q q) (n + 1 + 1))))))
    rw [jd2_pp q n, pwQ, pwQ, show 2 * (n + 1 + 1) = 2 * (n + 1) + 2 by lib_nat]
    lib_ring

theorem e2_ge_l (N m : Nat) : N - (m + 1) ≤ Ex2 N m :=
  Nat.le_trans (Nat.le_add_left _ ((N - (m + 1)) * (N - (m + 1)))) (Nat.le_add_left _ _)
theorem e2_ge_r (N m : Nat) : m - N ≤ Ex2 N m :=
  Nat.le_trans (Nat.le_add_left _ ((m - N) * (m - N))) (Nat.le_add_right _ _)

theorem jacobi3_mod (q : R) (M : Nat) (hq : Frobenius.pw K q M = K.zero) :
    em K (fun m => Frobenius.pw K q (Ex2 (2 * M + 2) m)) (K.neg K.one) (2 * (2 * M + 2) + 1) =
      K.mul (sg K (2 * M + 1)) (K.mul (K.mul (pp K (K.mul q q) (M + 1)) (pp K (K.mul q q) (M + 1))) (pp K (K.mul q q) (M + 1))) := by
  have hq1 : Frobenius.pw K q (M + 1) = K.zero := pw_ge K q M hq 1
  have hQ : Frobenius.pw K (K.mul q q) (M + 1) = K.zero := by
    rw [pwQ]; exact pw_zero_of_le K q (M + 1) _ hq1 (Nat.le_trans (Nat.le_add_left (M + 1) (M + 1)) (Nat.le_of_eq (by lib_nat)))
  have hst := pp_stable K (K.mul q q) (M + 1) hQ
  generalize hP : pp K (K.mul q q) (M + 1) = P at hst
  have hterm : ∀ m, m < 2 * (2 * M + 2) + 1 →
      K.mul (K.mul P P) (dd K q (2 * M + 2) m) = K.mul P (Frobenius.pw K q (Ex2 (2 * M + 2) m)) := by
    intro m _
    show K.mul (K.mul P P) (K.mul (gb K (K.mul q q) (2 * (2 * M + 2)) m) (Frobenius.pw K q (Ex2 (2 * M + 2) m))) = _
    cases Nat.lt_or_ge m (M + 1) with
    | inl h1 =>
      have hz : Frobenius.pw K q (Ex2 (2 * M + 2) m) = K.zero := by
        apply pw_zero_of_le K q M _ hq
        apply Nat.le_trans _ (e2_ge_l (2 * M + 2) m)
        obtain ⟨d, hd⟩ := Cheb.le_dest2 m M (Nat.le_of_lt_succ h1)
        rw [show 2 * M + 2 = (m + 1) + (d + 1 + M) by rw [← hd]; lib_nat, sub_add_l]
        exact Nat.le_add_left M (d + 1)
      rw [hz]; lib_ring
    | inr h1 =>
      cases Nat.lt_or_ge (3 * M + 3) m with
      | inl h2 =>
        have hz : Frobenius.pw K q (Ex2 (2 * M + 2) m) = K.zero := by
          apply pw_zero_of_le K q (M + 1) _ hq1
          apply Nat.le_trans _ (e2_ge_r (2 * M + 2) m)
          obtain ⟨d, hd⟩ := Cheb.le_dest2 (3 * M + 3 + 1) m h2
          rw [show m = (2 * M + 2) + (d + (M + 1) + 1) by rw [← hd]; lib_nat, sub_add_l]
          exact Nat.le_trans (Nat.le_add_left (M + 1) d) (Nat.le_succ _)
        rw [hz]; lib_ring
      | inr h2 =>
        have hm2 : m ≤ 2 * (2 * M + 2) := Nat.le_trans h2 (Nat.le_trans (Nat.le_add_right (3 * M + 3) (M + 1)) (Nat.le_of_eq (by lib_nat)))
        have hg := gb_pp K (K.mul q q) (2 * (2 * M + 2)) m hm2
        obtain ⟨a, ha⟩ := Cheb.le_dest2 (M + 1) m h1
        obtain ⟨c, hc⟩ := Cheb.le_dest2 m (3 * M + 3) h2
        have h4 : 2 * (2 * M + 2) = m + ((M + 1) + c) := by
          rw [show 2 * (2 * M + 2) = (c + m) + (M + 1) by rw [hc]; lib_nat]; lib_nat
        have hs : 2 * (2 * M + 2) - m = (M + 1) + c := by rw [h4, sub_add_l]
        have e1 : pp K (K.mul q q) m = P := by rw [← ha, Nat.add_comm, hst]
        have e2 : pp K (K.mul q q) (2 * (2 * M + 2) - m) = P := by rw [hs, hst]
        have e3 : pp K (K.mul q q) (2 * (2 * M + 2)) = P := by rw [show 2 * (2 * M + 2) = (M + 1) + (3 * M + 3) by lib_nat, hst]
        rw [e1, e2, e3] at hg
        generalize gb K (K.mul q q) (2 * (2 * M + 2)) m = G at hg ⊢
        calc _ = K.mul (K.mul (K.mul G P) P) (Frobenius.pw K q (Ex2 (2 * M + 2) m)) := by lib_ring
          _ = _ := by rw [hg]
  have hes := em_scale K (K.mul P P) (dd K q (2 * M + 2)) _ (K.neg K.one) _ hterm
  have hes2 := em_scale K P (fun m => Frobenius.pw K q (Ex2 (2 * M + 2) m)) _ (K.neg K.one) (2 * (2 * M + 2) + 1) (fun m _ => rfl)
  have hD : em K (dd K q (2 * M + 2)) (K.neg K.one) (2 * (2 * M + 2) + 1) = jd2 K q (2 * M + 1) := dd_at K q (2 * M + 1)
  rw [hD, ← hes2, jd2_pp] at hes
  have f1 : pp K (K.mul q q) (2 * M + 1) = P := by rw [show 2 * M + 1 = (M + 1) + M by lib_nat, hst]
  have f2 : pp K (K.mul q q) (2 * M + 1 + 1) = P := by rw [show 2 * M + 1 + 1 = (M + 1) + (M + 1) by lib_nat, hst]
  rw [f1, f2] at hes
  obtain ⟨y, hy⟩ := pp_unit K (K.mul q q) (M + 1) hQ (M + 1)
  rw [hP] at hy
  generalize em K (fun m => Frobenius.pw K q (Ex2 (2 * M + 2) m)) (K.neg K.one) (2 * (2 * M + 2) + 1) = S at hes ⊢
  calc S = K.mul (K.mul P y) S := by rw [hy]; lib_ring
    _ = K.mul y (K.mul P S) := by lib_ring
    _ = K.mul y (K.mul (K.mul P P) (K.mul (sg K (2 * M + 1)) (K.mul P P))) := by rw [← hes]
    _ = K.mul (K.mul P y) (K.mul (sg K (2 * M + 1)) (K.mul (K.mul P P) P)) := by lib_ring
    _ = _ := by rw [hy]; lib_ring

end QBinom

#print axioms QBinom.jd2_pp
#print axioms QBinom.e2_ge_l
#print axioms QBinom.e2_ge_r
#print axioms QBinom.jacobi3_mod

lang/JacobiMod.lean

lean_all/lang/JacobiMod.lean

/-
  lang/JacobiMod — T47, cycle 552: the weighted (Jacobi) side modulo q^M: in every commutative ring with q^M = 0,
    Σ_{m ≤ 2N} m·q^((m−N)²)·(−q)^m = (q²; q²)_M · jd (N − 1),   N = 2M + 2,
  jd (N − 1) = −q(1 − q²)∏_{k=1}^{N−1}(−q)(1 − q^(2k))(1 − q^(2k+2)).
    em_scale    c·Σ m f(m) z^m = Σ m g(m) z^m if c·f = g termwise
    jacobi_mod
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.JacobiD

namespace QBinom

open FTAChain

variable {R : Type} (K : DetChain.CRing R)

theorem em_scale (c : R) (f g : Nat → R) (z : R) : ∀ L, (∀ m, m < L → K.mul c (f m) = g m) → K.mul c (em K f z L) = em K g z L
  | 0, _ => by show K.mul c K.zero = K.zero; lib_ring
  | L + 1, h => by
    show K.mul c (K.add (em K f z L) (K.mul (Cheb.emb K L) (K.mul (f L) (Frobenius.pw K z L)))) =
      K.add (em K g z L) (K.mul (Cheb.emb K L) (K.mul (g L) (Frobenius.pw K z L)))
    rw [← em_scale c f g z L (fun m hm => h m (Nat.lt_succ_of_lt hm)), ← h L (Nat.lt_succ_self L)]; lib_ring

theorem jacobi_mod (q : R) (M : Nat) (hq : Frobenius.pw K q M = K.zero) :
    em K (fun m => Frobenius.pw K q (Ex (2 * M + 2) m)) (K.neg q) (2 * (2 * M + 2) + 1) =
      K.mul (pp K (K.mul q q) (M + 1)) (jd K q (2 * M + 1)) := by
  have hq1 : Frobenius.pw K q (M + 1) = K.zero := pw_ge K q M hq 1
  have hQ : Frobenius.pw K (K.mul q q) (M + 1) = K.zero := by
    rw [pwQ]; exact pw_zero_of_le K q (M + 1) _ hq1 (Nat.le_trans (Nat.le_add_left (M + 1) (M + 1)) (Nat.le_of_eq (by lib_nat)))
  have hst := pp_stable K (K.mul q q) (M + 1) hQ
  generalize hP : pp K (K.mul q q) (M + 1) = P at hst
  have hterm : ∀ m, m < 2 * (2 * M + 2) + 1 →
      K.mul (K.mul P P) (bj K q (2 * M + 2) m) = K.mul P (Frobenius.pw K q (Ex (2 * M + 2) m)) := by
    intro m hm
    rw [fjtp]
    cases Nat.lt_or_ge m (M + 1) with
    | inl h1 =>
      have hz : Frobenius.pw K q (Ex (2 * M + 2) m) = K.zero := by
        apply pw_zero_of_le K q (M + 1) _ hq1
        apply Nat.le_trans _ (ex_ge_l (2 * M + 2) m)
        obtain ⟨d, hd⟩ := Cheb.le_dest2 m M (Nat.le_of_lt_succ h1)
        rw [show 2 * M + 2 = m + (d + 1 + (M + 1)) by rw [← hd]; lib_nat, sub_add_l]
        exact Nat.le_add_left (M + 1) (d + 1)
      rw [hz]; lib_ring
    | inr h1 =>
      cases Nat.lt_or_ge (3 * M + 3) m with
      | inl h2 =>
        have hz : Frobenius.pw K q (Ex (2 * M + 2) m) = K.zero := by
          apply pw_zero_of_le K q (M + 1) _ hq1
          apply Nat.le_trans _ (ex_ge_r (2 * M + 2) m)
          obtain ⟨d, hd⟩ := Cheb.le_dest2 (3 * M + 3 + 1) m h2
          rw [show m = (2 * M + 2) + (d + (M + 1) + 1) by rw [← hd]; lib_nat, sub_add_l]
          exact Nat.le_trans (Nat.le_add_left (M + 1) d) (Nat.le_succ _)
        rw [hz]; lib_ring
      | inr h2 =>
        have hm2 : m ≤ 2 * (2 * M + 2) := Nat.le_trans h2 (Nat.le_trans (Nat.le_add_right (3 * M + 3) (M + 1)) (Nat.le_of_eq (by lib_nat)))
        have hg := gb_pp K (K.mul q q) (2 * (2 * M + 2)) m hm2
        obtain ⟨a, ha⟩ := Cheb.le_dest2 (M + 1) m h1
        obtain ⟨c, hc⟩ := Cheb.le_dest2 m (3 * M + 3) h2
        have h4 : 2 * (2 * M + 2) = m + ((M + 1) + c) := by
          rw [show 2 * (2 * M + 2) = (c + m) + (M + 1) by rw [hc]; lib_nat]; lib_nat
        have hs : 2 * (2 * M + 2) - m = (M + 1) + c := by rw [h4, sub_add_l]
        have e1 : pp K (K.mul q q) m = P := by rw [← ha, Nat.add_comm, hst]
        have e2 : pp K (K.mul q q) (2 * (2 * M + 2) - m) = P := by rw [hs, hst]
        have e3 : pp K (K.mul q q) (2 * (2 * M + 2)) = P := by rw [show 2 * (2 * M + 2) = (M + 1) + (3 * M + 3) by lib_nat, hst]
        rw [e1, e2, e3] at hg
        generalize gb K (K.mul q q) (2 * (2 * M + 2)) m = G at hg ⊢
        calc _ = K.mul (K.mul (K.mul G P) P) (Frobenius.pw K q (Ex (2 * M + 2) m)) := by lib_ring
          _ = _ := by rw [hg]
  have hes := em_scale K (K.mul P P) (bj K q (2 * M + 2)) _ (K.neg q) _ hterm
  have hes2 := em_scale K P (fun m => Frobenius.pw K q (Ex (2 * M + 2) m)) _ (K.neg q) (2 * (2 * M + 2) + 1) (fun m _ => rfl)
  have hD : em K (bj K q (2 * M + 2)) (K.neg q) (2 * (2 * M + 2) + 1) = jd K q (2 * M + 1) := dj_at K q (2 * M + 1)
  rw [hD, ← hes2] at hes
  obtain ⟨y, hy⟩ := pp_unit K (K.mul q q) (M + 1) hQ (M + 1)
  rw [hP] at hy
  calc em K (fun m => Frobenius.pw K q (Ex (2 * M + 2) m)) (K.neg q) (2 * (2 * M + 2) + 1)
      = K.mul (K.mul P y) (em K (fun m => Frobenius.pw K q (Ex (2 * M + 2) m)) (K.neg q) (2 * (2 * M + 2) + 1)) := by rw [hy]; lib_ring
    _ = K.mul y (K.mul P (em K (fun m => Frobenius.pw K q (Ex (2 * M + 2) m)) (K.neg q) (2 * (2 * M + 2) + 1))) := by lib_ring
    _ = K.mul y (K.mul (K.mul P P) (jd K q (2 * M + 1))) := by rw [← hes]
    _ = K.mul (K.mul P y) (K.mul P (jd K q (2 * M + 1))) := by lib_ring
    _ = _ := by rw [hy]; lib_ring

end QBinom

#print axioms QBinom.em_scale
#print axioms QBinom.jacobi_mod

lang/JacobiNorm.lean

lean_all/lang/JacobiNorm.lean

/-
  lang/JacobiNorm — G2 (T43): the cubic Jacobi sum has norm p.

  p = 6n + 7, χ = chi3 w, χ̄ = chi3 w² (= χ², cb_eq), g = Σ χ(a) ζ^a, ḡ = Σ χ̄(a) ζ^a in ℤ[ω][C_p] (GroupRing, K = ℤ[ω]).
    g ⋆ g = J ḡ,  ḡ ⋆ ḡ = J̄ g,  g ⋆ ḡ = X := pζ⁰ − N,  X ⋆ X = p X                  (JacobiSum)
  so J J̄ X = (g ḡ)² = p X; at ζ⁰: (p − 1) J J̄ = (p − 1) p, hence
      jacobi_norm   J = a + bω  ⇒  a² − ab + b² = p
  (the cubic Jacobi sum is an element of norm p in ℤ[ω]: p = ππ̄ with π = J).
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.JacobiSum
import lang.GroupRingFrob

namespace JacobiNorm

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius CubicChar JacobiSum GroupRing GroupRingFrob

variable (n : Nat)

def cconj (z : Int × Int) : Int × Int := (z.1 - z.2, -z.2)

theorem w2_ne (_hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : (ring (6 * n + 5 + 1)).mul w w ≠ (ring (6 * n + 5 + 1)).one := ww_ne_one n w hw1 hw3

theorem w2_3 (w : Zp (6 * n + 5 + 1)) (_hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : pw (ring (6 * n + 5 + 1)) ((ring (6 * n + 5 + 1)).mul w w) 3 = (ring (6 * n + 5 + 1)).one := by
  have h3 : (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul w w) w = (ring (6 * n + 5 + 1)).one := by
    have : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (ring (6 * n + 5 + 1)).one w) w) w := rfl
    rw [this, (ring (6 * n + 5 + 1)).one_mul] at hw3; exact hw3
  show (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).mul w w)) ((ring (6 * n + 5 + 1)).mul w w)) ((ring (6 * n + 5 + 1)).mul w w) = (ring (6 * n + 5 + 1)).one
  rw [show (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).mul w w)) ((ring (6 * n + 5 + 1)).mul w w)) ((ring (6 * n + 5 + 1)).mul w w) = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul w w) w) ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul w w) w) by lib_ring, h3]
  lib_ring

theorem w4 (w : Zp (6 * n + 5 + 1)) (_hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul w w) ((ring (6 * n + 5 + 1)).mul w w) = w := by
  have h3 : (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul w w) w = (ring (6 * n + 5 + 1)).one := by
    have : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (ring (6 * n + 5 + 1)).one w) w) w := rfl
    rw [this, (ring (6 * n + 5 + 1)).one_mul] at hw3; exact hw3
  rw [show (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul w w) ((ring (6 * n + 5 + 1)).mul w w) = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul w w) w) w by lib_ring, h3]; lib_ring

/-- χ² = χ_{w²}, and χ_{w²} = conj ∘ χ. -/
theorem cb_eq (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (x : Zp (6 * n + 5 + 1)) : cb n w x = chi3 n ((ring (6 * n + 5 + 1)).mul w w) x ∧ chi3 n ((ring (6 * n + 5 + 1)).mul w w) x = cconj (chi3 n w x) := by
  by_cases hx : x = (ring (6 * n + 5 + 1)).zero
  · rw [hx]; unfold cb; rw [chi3_zero, chi3_zero]; exact ⟨rfl, rfl⟩
  match exists_i n hpr w hw1 hw3 x hx with
  | ⟨i, hi, ei⟩ =>
    have v1 := val_of n hpr w hw1 hw3 x hx i hi ei
    unfold cb; rw [v1]
    have h3 : (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul w w) w = (ring (6 * n + 5 + 1)).one := by
      have : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (ring (6 * n + 5 + 1)).one w) w) w := rfl
      rw [this, (ring (6 * n + 5 + 1)).one_mul] at hw3; exact hw3
    match i, hi with
    | 0, _ =>
      rw [val_of n hpr ((ring (6 * n + 5 + 1)).mul w w) (w2_ne n hpr w hw1 hw3) (w2_3 n w hw1 hw3) x hx 0 (Nat.zero_lt_succ 2) (ei.trans rfl)]; exact ⟨rfl, rfl⟩
    | 1, _ =>
      have e : pw (ring (6 * n + 5 + 1)) x (2 * n + 2) = pw (ring (6 * n + 5 + 1)) ((ring (6 * n + 5 + 1)).mul w w) 2 := by
        rw [ei]; show (ring (6 * n + 5 + 1)).mul (ring (6 * n + 5 + 1)).one w = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).mul w w)) ((ring (6 * n + 5 + 1)).mul w w); rw [(ring (6 * n + 5 + 1)).one_mul, (ring (6 * n + 5 + 1)).one_mul, w4 n w hw1 hw3]
      rw [val_of n hpr ((ring (6 * n + 5 + 1)).mul w w) (w2_ne n hpr w hw1 hw3) (w2_3 n w hw1 hw3) x hx 2 (Nat.lt_succ_self 2) e]; exact ⟨rfl, rfl⟩
    | 2, _ =>
      have e : pw (ring (6 * n + 5 + 1)) x (2 * n + 2) = pw (ring (6 * n + 5 + 1)) ((ring (6 * n + 5 + 1)).mul w w) 1 := by
        rw [ei]; show (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (ring (6 * n + 5 + 1)).one w) w = (ring (6 * n + 5 + 1)).mul (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).mul w w); rw [(ring (6 * n + 5 + 1)).one_mul, (ring (6 * n + 5 + 1)).one_mul]
      rw [val_of n hpr ((ring (6 * n + 5 + 1)).mul w w) (w2_ne n hpr w hw1 hw3) (w2_3 n w hw1 hw3) x hx 1 (Nat.succ_lt_succ (Nat.zero_lt_succ 1)) e]; exact ⟨rfl, rfl⟩
    | k + 3, hk => exact absurd hk (fun h => Nat.lt_irrefl 3 (Nat.lt_of_le_of_lt (Nat.le_add_left 3 k) h))

theorem cconj_mul (x y : Int × Int) : cconj ((Cyclo3.cyc3 LibInt.intR).mul x y) = (Cyclo3.cyc3 LibInt.intR).mul (cconj x) (cconj y) := by
  apply Prod.ext
  · show (x.1 * y.1 + -(x.2 * y.2)) - (x.1 * y.2 + x.2 * y.1 + -(x.2 * y.2)) = (x.1 - x.2) * (y.1 - y.2) + -(-x.2 * -y.2)
    lib_int
  · show -(x.1 * y.2 + x.2 * y.1 + -(x.2 * y.2)) = (x.1 - x.2) * -y.2 + -x.2 * (y.1 - y.2) + -(-x.2 * -y.2)
    lib_int

theorem cconj_sum {A : Type} (f : A → Int × Int) : ∀ l : List A, cconj (sumK (Cyclo3.cyc3 LibInt.intR) f l) = sumK (Cyclo3.cyc3 LibInt.intR) (fun a => cconj (f a)) l
  | [] => rfl
  | a :: l => by
    show cconj ((Cyclo3.cyc3 LibInt.intR).add (f a) (sumK (Cyclo3.cyc3 LibInt.intR) f l)) = (Cyclo3.cyc3 LibInt.intR).add (cconj (f a)) (sumK (Cyclo3.cyc3 LibInt.intR) (fun a => cconj (f a)) l)
    rw [← cconj_sum f l]
    apply Prod.ext
    · show ((f a).1 + (sumK (Cyclo3.cyc3 LibInt.intR) f l).1) - ((f a).2 + (sumK (Cyclo3.cyc3 LibInt.intR) f l).2) = ((f a).1 - (f a).2) + ((sumK (Cyclo3.cyc3 LibInt.intR) f l).1 - (sumK (Cyclo3.cyc3 LibInt.intR) f l).2)
      lib_int
    · show -((f a).2 + (sumK (Cyclo3.cyc3 LibInt.intR) f l).2) = -(f a).2 + -(sumK (Cyclo3.cyc3 LibInt.intR) f l).2
      lib_int

theorem J2_conj (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : J n ((ring (6 * n + 5 + 1)).mul w w) = cconj (J n w) := by
  unfold J; rw [cconj_sum]
  exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun t => by rw [cconj_mul, (cb_eq n hpr w hw1 hw3 t).2, (cb_eq n hpr w hw1 hw3 _).2]) _

theorem norm_form (a b : Int) : (Cyclo3.cyc3 LibInt.intR).mul (a, b) (cconj (a, b)) = (a * a - a * b + b * b, 0) := by
  apply Prod.ext
  · show a * (a - b) + -(b * -b) = a * a - a * b + b * b
    lib_int
  · show a * -b + b * (a - b) + -(b * -b) = 0
    lib_int

section Grp
variable {R : Type} (K : CRing R) (m : Nat)
theorem mono0_mulg (s : R) (u : V R (m + 1)) (c : Zp m) :
    gt K m (gmul K m (mono K m (ring m).zero s) u) c = K.mul s (gt K m u c) := by
  rw [gt_mul]
  have e : sumK K (fun a => K.mul (gt K m (mono K m (ring m).zero s) a) (gt K m u ((ring m).add c ((ring m).neg a)))) (GroupRing.L m)
      = sumK K (fun a => K.mul (delta K a (ring m).zero) (K.mul s (gt K m u ((ring m).add c ((ring m).neg a))))) (GroupRing.L m) :=
    sumK_ext K _ _ (fun a => by
      rw [gt_mono]
      show K.mul (K.mul s (delta K a (ring m).zero)) _ = _
      lib_ring) _
  rw [e]
  refine (pick K _ (CMInert.complete_allZ m K) (fun a => K.mul s (gt K m u ((ring m).add c ((ring m).neg a)))) (ring m).zero).trans ?_
  show K.mul s (gt K m u ((ring m).add c ((ring m).neg (ring m).zero))) = _
  rw [show (ring m).add c ((ring m).neg (ring m).zero) = c by lib_ring]
end Grp

def G1 (w : Zp (6 * n + 5 + 1)) : V (Int × Int) ((6 * n + 5 + 1) + 1) := tb (6 * n + 5 + 1) (chi3 n w)
def Nv : V (Int × Int) ((6 * n + 5 + 1) + 1) := tb (6 * n + 5 + 1) (fun _ => (Cyclo3.cyc3 LibInt.intR).one)
def Pe : Int × Int := (Int.ofNat (6 * n + 5 + 1 + 1), 0)
def Xv : V (Int × Int) ((6 * n + 5 + 1) + 1) := gadd (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (Pe n)) (gneg (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (Nv n))

theorem conv_eq (w : Zp (6 * n + 5 + 1)) (f : Zp (6 * n + 5 + 1) → Int × Int) (c : Zp (6 * n + 5 + 1)) :
    gt (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (gmul (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (G1 n w) (tb (6 * n + 5 + 1) f)) c = sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (f ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg a)))) (LL n) := by
  rw [gt_mul]
  exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun a => by unfold G1; rw [gt_tb, gt_tb]) _

/-- g ⋆ g = J ḡ. -/
theorem sq1 (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : gmul (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (G1 n w) (G1 n w) = gmul (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).  -- (+56 chars; full line in the Zenodo source)
  ext (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) _ _ (fun c => by
    refine (conv_eq n w (chi3 n w) c).trans ?_
    rw [mono0_mulg]
    show _ = (Cyclo3.cyc3 LibInt.intR).mul (J n w) (gt (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (tb (6 * n + 5 + 1) (chi3 n ((ring (6 * n + 5 + 1)).mul w w))) c)
    rw [gt_tb]
    by_cases hc : c = (ring (6 * n + 5 + 1)).zero
    · rw [hc, S_zero n hpr w hw1 hw3, chi3_zero]; exact (mul_zero (Cyclo3.cyc3 LibInt.intR) _).symm
    · rw [S_ne n hpr w hw1 hw3 c hc, (cb_eq n hpr w hw1 hw3 c).1, (Cyclo3.cyc3 LibInt.intR).mul_comm])

theorem sq2 (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : gmul (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (G1 n ((ring (6 * n + 5 + 1)).mul w w)) (G1 n ((ring (6 * n + 5 + 1)).mul w w)) = gmul (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (mono (Cy  -- (+121 chars; full line in the Zenodo source)
  have h := sq1 n hpr ((ring (6 * n + 5 + 1)).mul w w) (w2_ne n hpr w hw1 hw3) (w2_3 n w hw1 hw3)
  rw [w4 n w hw1 hw3] at h
  exact h

theorem neg_b (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (b : Zp (6 * n + 5 + 1)) : cb n w ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).zero ((ring (6 * n + 5 + 1)).neg b)) = cb n w b := by
  rw [show (ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).zero ((ring (6 * n + 5 + 1)).neg b) = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one) b by lib_ring, cb_mul n hpr w hw1 hw3]
  unfold cb; rw [chi3_neg1 n hpr w hw1 hw3]; lib_ring

/-- g ⋆ ḡ = pζ⁰ − N. -/
theorem gg (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : gmul (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (G1 n w) (G1 n ((ring (6 * n + 5 + 1)).mul w w)) = Xv n :=
  ext (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) _ _ (fun c => by
    refine (conv_eq n w (chi3 n ((ring (6 * n + 5 + 1)).mul w w)) c).trans ?_
    have e1 : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (chi3 n ((ring (6 * n + 5 + 1)).mul w w) ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg a)))) (LL n)
        = sumK (Cyclo3.cyc3 LibInt.intR) (fun b => (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).add c b)) (cb n w b)) (LL n) := by
      refine (GroupRing.shift (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (chi3 n ((ring (6 * n + 5 + 1)).mul w w) ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg a)))) c).symm.trans ?_
      exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun b => by
        show (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).add b c)) (chi3 n ((ring (6 * n + 5 + 1)).mul w w) ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).add b c)))) = _
        rw [← (cb_eq n hpr w hw1 hw3 _).1, show (ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).add b c)) = (ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).zero ((ring (6 * n + 5 + 1)).neg b) by lib_ring, neg_b n hpr w hw1 hw3,
          (ring (6 * n + 5 + 1)).add_comm b c]) _
    rw [e1]
    unfold Xv gadd gneg Nv
    rw [gt_tb, gt_mono, gt_tb]
    by_cases hc : c = (ring (6 * n + 5 + 1)).zero
    · rw [hc, show (ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).zero = fun b => (ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).zero b from rfl]
      have e2 : sumK (Cyclo3.cyc3 LibInt.intR) (fun b => (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).zero b)) (cb n w b)) (LL n) = sumK (Cyclo3.cyc3 LibInt.intR) (fun b => (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w b) (cb n w b)) (LL n) :=
        sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun b => by rw [(ring (6 * n + 5 + 1)).zero_add]) _
      rw [e2, D_zero n hpr w hw1 hw3]
      unfold delta; rw [ite_pos' _ _ rfl, (Cyclo3.cyc3 LibInt.intR).mul_comm, (Cyclo3.cyc3 LibInt.intR).one_mul]
      unfold Pe
      exact Prod.ext (by show Int.ofNat (6 * n + 5 + 1 + 1) - 1 = Int.ofNat (6 * n + 5 + 1 + 1) + -1; rfl) (by show (0 : Int) = 0 + -0; rfl)
    · rw [D_ne n hpr w hw1 hw3 c hc]
      unfold delta; rw [ite_neg' _ _ hc]
      rw [gt_tb]
      lib_ring)

theorem NN : gmul (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (Nv n) (Nv n) = gmul (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (Pe n)) (Nv n) :=
  ext (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) _ _ (fun c => by
    rw [gt_mul, mono0_mulg]
    unfold Nv
    rw [gt_tb]
    have e : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (gt (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (tb (6 * n + 5 + 1) (fun _ => (Cyclo3.cyc3 LibInt.intR).one)) a) (gt (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (tb (6 * n + 5 + 1) (fun _ => (Cyclo3.cyc3 LibInt.intR).one)) ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg a)))) (GroupRing.L (6 * n + 5 + 1))
        = sumK (Cyclo3.cyc3 LibInt.intR) (fun _ => (Cyclo3.cyc3 LibInt.intR).one) (CMInert.allZ (6 * n + 5 + 1) ((6 * n + 5 + 1) + 1)) :=
      sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun a => by rw [gt_tb, gt_tb]; exact (Cyclo3.cyc3 LibInt.intR).one_mul _) _
    rw [e, sum_one, (Cyclo3.cyc3 LibInt.intR).mul_comm, (Cyclo3.cyc3 LibInt.intR).one_mul]
    rfl)

theorem XX : gmul (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (Xv n) (Xv n) = gmul (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (Pe n)) (Xv n) := by
  have hN := NN n
  have hc := gm_comm (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (Nv n) (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (Pe n))
  show (gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).add (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (Pe n)) ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).neg (Nv n))) ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).add (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (Pe n  -- (+66 chars; full line in the Zenodo source)
    = (gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (Pe n)) ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).add (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (Pe n)) ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).neg (Nv n)))
  have hN' : (gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul (Nv n) (Nv n) = (gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (Pe n)) (Nv n) := hN
  rw [show (gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).add (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (Pe n)) ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).neg (Nv n))) ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).add (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (  -- (+70 chars; full line in the Zenodo source)
      = (gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).add ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (Pe n)) ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).add (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (Pe n)) ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).neg (Nv n  -- (+4 chars; full line in the Zenodo source)
          ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).add ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).neg ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (Pe n)) (Nv n))) ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul (Nv n) (Nv n))) by lib_ring, hN']
  lib_ring

theorem int_cancel (u v : Int) (k : Nat) (h : u * Int.ofNat (k + 1) = v * Int.ofNat (k + 1)) : u = v := by
  have h0 : (u - v) * Int.ofNat (k + 1) = 0 := by rw [show (u - v) * Int.ofNat (k + 1) = u * Int.ofNat (k + 1) - v * Int.ofNat (k + 1) by lib_int, h]; lib_int
  cases LibInt.int_noZD _ _ h0 with
  | inl h1 => calc u = (u - v) + v := by lib_int
      _ = 0 + v := by rw [h1]; rfl
      _ = v := by lib_int
  | inr h1 => exact absurd (Int.ofNat.inj h1) (fun e => Nat.noConfusion e)

/-- **N(J) = p**: the cubic Jacobi sum J = a + bω has a² − ab + b² = p. -/
theorem jacobi_norm (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : (J n w).1 * (J n w).1 - (J n w).1 * (J n w).2 + (J n w).2 * (J n w).2 = Int.ofNat (6 * n + 5 + 1 + 1) := by
  have h1 := sq1 n hpr w hw1 hw3
  have h2 := sq2 n hpr w hw1 hw3
  have h3 := gg n hpr w hw1 hw3
  have key : gmul (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero ((Cyclo3.cyc3 LibInt.intR).mul (J n w) (J n ((ring (6 * n + 5 + 1)).mul w w)))) (Xv n) = gmul (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (Pe n)) (Xv n) := by
    rw [← XX n, ← h3]
    have hm : gmul (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (J n w)) (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (J n ((ring (6 * n + 5 + 1)).mul w w))) = mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero ((Cyclo3.cyc3 LibInt.intR).mul (J n w) (J n ((ring (6 * n + 5  -- (+23 chars; full line in the Zenodo source)
      rw [mono_mul, show (ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).zero (ring (6 * n + 5 + 1)).zero = (ring (6 * n + 5 + 1)).zero by lib_ring]
    rw [← hm]
    show (gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (J n w)) (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (J n ((ring (6 * n + 5 + 1)).mul w w)))) ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul (G1 n w) (G1 n ((ring (6   -- (+24 chars; full line in the Zenodo source)
      = (gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul (G1 n w) (G1 n ((ring (6 * n + 5 + 1)).mul w w))) ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul (G1 n w) (G1 n ((ring (6 * n + 5 + 1)).mul w w)))
    have h1' : (gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul (G1 n w) (G1 n w) = (gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (J n w)) (G1 n ((ring (6 * n + 5 + 1)).mul w w)) := h1
    have h2' : (gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul (G1 n ((ring (6 * n + 5 + 1)).mul w w)) (G1 n ((ring (6 * n + 5 + 1)).mul w w)) = (gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (J n ((ring (6 * n + 5 + 1)).mul w w))) (G1 n w) := h2
    rw [show (gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul (G1 n w) (G1 n ((ring (6 * n + 5 + 1)).mul w w))) ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul (G1 n w) (G1 n ((ring (6 * n + 5 + 1)).mul w w)))
        = (gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul (G1 n w) (G1 n w)) ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul (G1 n ((ring (6 * n + 5 + 1)).mul w w)) (G1 n ((ring (6 * n + 5 + 1)).mul w w))) by lib_ring, h1', h2']
    lib_ring
  have k0 := congrArg (fun u => gt (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) u (ring (6 * n + 5 + 1)).zero) key
  have k1 : (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (J n w) (J n ((ring (6 * n + 5 + 1)).mul w w))) (gt (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (Xv n) (ring (6 * n + 5 + 1)).zero) = (Cyclo3.cyc3 LibInt.intR).mul (Pe n) (gt (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (Xv n) (ring (6 * n + 5 + 1)).zero) := by
    have := k0
    rw [mono0_mulg, mono0_mulg] at this
    exact this
  have hX0 : gt (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (Xv n) (ring (6 * n + 5 + 1)).zero = (Int.ofNat (6 * n + 5 + 1), 0) := by
    unfold Xv gadd gneg Nv
    rw [gt_tb, gt_mono, gt_tb]
    unfold delta; rw [ite_pos' _ _ rfl, (Cyclo3.cyc3 LibInt.intR).mul_comm, (Cyclo3.cyc3 LibInt.intR).one_mul]
    unfold Pe
    exact Prod.ext (by show Int.ofNat (6 * n + 5 + 1) + 1 + -1 = Int.ofNat (6 * n + 5 + 1); lib_int) (by show (0 : Int) + -0 = 0; rfl)
  have hN : (Cyclo3.cyc3 LibInt.intR).mul (J n w) (J n ((ring (6 * n + 5 + 1)).mul w w)) = ((J n w).1 * (J n w).1 - (J n w).1 * (J n w).2 + (J n w).2 * (J n w).2, 0) := by
    rw [J2_conj n hpr w hw1 hw3]
    apply Prod.ext
    · show (J n w).1 * ((J n w).1 - (J n w).2) + -((J n w).2 * -(J n w).2) = (J n w).1 * (J n w).1 - (J n w).1 * (J n w).2 + (J n w).2 * (J n w).2
      lib_int
    · show (J n w).1 * -(J n w).2 + (J n w).2 * ((J n w).1 - (J n w).2) + -((J n w).2 * -(J n w).2) = 0
      lib_int
  rw [hX0, hN] at k1
  have k2 := congrArg Prod.fst k1
  change _ * Int.ofNat (6 * n + 5 + 1) + -(0 * 0) = Int.ofNat (6 * n + 5 + 1 + 1) * Int.ofNat (6 * n + 5 + 1) + -(0 * 0) at k2
  apply int_cancel _ _ (6 * n + 5)
  rw [show ((J n w).1 * (J n w).1 - (J n w).1 * (J n w).2 + (J n w).2 * (J n w).2) * Int.ofNat (6 * n + 5 + 1)
      = ((J n w).1 * (J n w).1 - (J n w).1 * (J n w).2 + (J n w).2 * (J n w).2) * Int.ofNat (6 * n + 5 + 1) + -(0 * 0) by lib_int, k2]
  lib_int

end JacobiNorm

#print axioms JacobiNorm.cconj
#print axioms JacobiNorm.w2_ne
#print axioms JacobiNorm.w2_3
#print axioms JacobiNorm.w4
#print axioms JacobiNorm.cb_eq
#print axioms JacobiNorm.cconj_mul
#print axioms JacobiNorm.cconj_sum
#print axioms JacobiNorm.J2_conj
#print axioms JacobiNorm.norm_form
#print axioms JacobiNorm.mono0_mulg
#print axioms JacobiNorm.conv_eq
#print axioms JacobiNorm.sq1
#print axioms JacobiNorm.sq2
#print axioms JacobiNorm.neg_b
#print axioms JacobiNorm.gg
#print axioms JacobiNorm.NN
#print axioms JacobiNorm.XX
#print axioms JacobiNorm.int_cancel
#print axioms JacobiNorm.jacobi_norm

lang/JacobiP.lean

lean_all/lang/JacobiP.lean

/-
  lang/JacobiP — T47, cycle 556: **Jacobi's identity for η³ at every degree**: in every commutative ring with q^M = 0,
    Σ_{j < 2M+2} (−1)^j (2j + 1) q^(j² + j) = ((q²; q²)_(M+1))³,
  i.e. ∏_{n ≥ 1} (1 − Q^n)³ = Σ_{j ≥ 0} (−1)^j (2j + 1) Q^(j(j+1)/2) coefficientwise (Q = q²).

    es_congr, em_congr, es_shift, em_shift    pointwise congruence and index shift of the sums
    e2_shift                                  Ex2 (N+1) (m+1) = Ex2 N m
    pm_even, pm_sq                            (−1)^(2N) = 1, ((−1)^N)² = 1
    w_zero                                    Σ_{m<2N} (−1)^m q^(Ex2 N m) = 0
    v_pair                                    Σ_{m<2N} m (−1)^m q^(Ex2 N m) = (−1)^N · T N,  T N = Σ_{j<N} (−1)^j (2j+1) q^(j²+j)
    jacobi_eta3
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.JacobiL

namespace QBinom

open FTAChain

variable {R : Type} (K : DetChain.CRing R)

theorem es_congr (f g : Nat → R) (z : R) (h : ∀ m, f m = g m) : ∀ L, es K f z L = es K g z L
  | 0 => rfl
  | L + 1 => by
    show K.add (es K f z L) (K.mul (f L) (Frobenius.pw K z L)) = K.add (es K g z L) (K.mul (g L) (Frobenius.pw K z L))
    rw [es_congr f g z h L, h L]

theorem em_congr (f g : Nat → R) (z : R) (h : ∀ m, f m = g m) : ∀ L, em K f z L = em K g z L
  | 0 => rfl
  | L + 1 => by
    show K.add (em K f z L) (K.mul (Cheb.emb K L) (K.mul (f L) (Frobenius.pw K z L))) =
      K.add (em K g z L) (K.mul (Cheb.emb K L) (K.mul (g L) (Frobenius.pw K z L)))
    rw [em_congr f g z h L, h L]

theorem es_shift (f : Nat → R) (z : R) : ∀ L, es K f z (L + 1) = K.add (f 0) (K.mul z (es K (fun m => f (m + 1)) z L))
  | 0 => by show K.add K.zero (K.mul (f 0) K.one) = K.add (f 0) (K.mul z K.zero); lib_ring
  | L + 1 => by
    show K.add (es K f z (L + 1)) (K.mul (f (L + 1)) (Frobenius.pw K z (L + 1))) =
      K.add (f 0) (K.mul z (K.add (es K (fun m => f (m + 1)) z L) (K.mul (f (L + 1)) (Frobenius.pw K z L))))
    rw [es_shift f z L, show Frobenius.pw K z (L + 1) = K.mul (Frobenius.pw K z L) z from rfl]
    lib_ring

theorem em_shift (f : Nat → R) (z : R) : ∀ L,
    em K f z (L + 1) = K.mul z (K.add (em K (fun m => f (m + 1)) z L) (es K (fun m => f (m + 1)) z L))
  | 0 => by show K.add K.zero (K.mul K.zero (K.mul (f 0) K.one)) = K.mul z (K.add K.zero K.zero); lib_ring
  | L + 1 => by
    show K.add (em K f z (L + 1)) (K.mul (Cheb.emb K (L + 1)) (K.mul (f (L + 1)) (Frobenius.pw K z (L + 1)))) =
      K.mul z (K.add (K.add (em K (fun m => f (m + 1)) z L) (K.mul (Cheb.emb K L) (K.mul (f (L + 1)) (Frobenius.pw K z L))))
        (K.add (es K (fun m => f (m + 1)) z L) (K.mul (f (L + 1)) (Frobenius.pw K z L))))
    rw [em_shift f z L, show Frobenius.pw K z (L + 1) = K.mul (Frobenius.pw K z L) z from rfl,
      show Cheb.emb K (L + 1) = K.add (Cheb.emb K L) K.one from rfl]
    lib_ring

theorem e2_shift (N m : Nat) : Ex2 (N + 1) (m + 1) = Ex2 N m := by
  show (m + 1 - (N + 1)) * (m + 1 - (N + 1) + 1) + (N + 1 - (m + 1 + 1)) * (N + 1 - (m + 1 + 1) + 1) =
    (m - N) * (m - N + 1) + (N - (m + 1)) * (N - (m + 1) + 1)
  rw [LibInt.nsub_succ_succ, LibInt.nsub_succ_succ]

theorem pm_even : ∀ N, Frobenius.pw K (K.neg K.one) (2 * N) = K.one
  | 0 => rfl
  | N + 1 => by
    rw [show 2 * (N + 1) = 2 * N + 1 + 1 by lib_nat]
    show K.mul (K.mul (Frobenius.pw K (K.neg K.one) (2 * N)) (K.neg K.one)) (K.neg K.one) = K.one
    rw [pm_even N]; lib_ring

theorem pm_sq : ∀ N, K.mul (Frobenius.pw K (K.neg K.one) N) (Frobenius.pw K (K.neg K.one) N) = K.one
  | 0 => by show K.mul K.one K.one = K.one; lib_ring
  | N + 1 => by
    show K.mul (K.mul (Frobenius.pw K (K.neg K.one) N) (K.neg K.one)) (K.mul (Frobenius.pw K (K.neg K.one) N) (K.neg K.one)) = K.one
    calc _ = K.mul (K.mul (Frobenius.pw K (K.neg K.one) N) (Frobenius.pw K (K.neg K.one) N)) (K.mul (K.neg K.one) (K.neg K.one)) := by lib_ring
      _ = _ := by rw [pm_sq N]; lib_ring

theorem sg_odd : ∀ M, sg K (2 * M + 1) = K.one
  | 0 => by show K.neg (K.neg K.one) = K.one; lib_ring
  | M + 1 => by
    rw [show 2 * (M + 1) + 1 = 2 * M + 1 + 1 + 1 by lib_nat]
    show K.neg (K.neg (sg K (2 * M + 1))) = K.one
    rw [sg_odd M]; lib_ring

variable (q : R)

theorem a_dn (N : Nat) : Ex2 (N + 1) 0 = N * N + N := by rw [show N + 1 = 0 + 1 + N by lib_nat, e2_dn]
theorem a_up (N : Nat) : Ex2 N (2 * N) = N * N + N := by rw [show 2 * N = N + N by lib_nat, e2_up]

theorem w_zero : ∀ N, es K (fun m => Frobenius.pw K q (Ex2 N m)) (K.neg K.one) (2 * N) = K.zero
  | 0 => rfl
  | N + 1 => by
    have hc := es_congr K (fun m => Frobenius.pw K q (Ex2 (N + 1) (m + 1))) (fun m => Frobenius.pw K q (Ex2 N m)) (K.neg K.one)
      (fun m => congrArg (Frobenius.pw K q) (e2_shift N m)) (2 * N + 1)
    rw [show 2 * (N + 1) = 2 * N + 1 + 1 by lib_nat, es_shift, hc]
    show K.add (Frobenius.pw K q (Ex2 (N + 1) 0)) (K.mul (K.neg K.one) (K.add (es K (fun m => Frobenius.pw K q (Ex2 N m)) (K.neg K.one) (2 * N))
      (K.mul (Frobenius.pw K q (Ex2 N (2 * N))) (Frobenius.pw K (K.neg K.one) (2 * N))))) = K.zero
    rw [w_zero N, pm_even, a_dn, a_up]; lib_ring

def T : Nat → R
  | 0 => K.zero
  | N + 1 => K.add (T N) (K.mul (Frobenius.pw K (K.neg K.one) N) (K.mul (Cheb.emb K (2 * N + 1)) (Frobenius.pw K q (N * N + N))))

theorem v_pair : ∀ N, em K (fun m => Frobenius.pw K q (Ex2 N m)) (K.neg K.one) (2 * N) =
    K.mul (Frobenius.pw K (K.neg K.one) N) (T K q N)
  | 0 => by show K.zero = K.mul K.one K.zero; lib_ring
  | N + 1 => by
    have hc := es_congr K (fun m => Frobenius.pw K q (Ex2 (N + 1) (m + 1))) (fun m => Frobenius.pw K q (Ex2 N m)) (K.neg K.one)
      (fun m => congrArg (Frobenius.pw K q) (e2_shift N m)) (2 * N + 1)
    have hc2 := em_congr K (fun m => Frobenius.pw K q (Ex2 (N + 1) (m + 1))) (fun m => Frobenius.pw K q (Ex2 N m)) (K.neg K.one)
      (fun m => congrArg (Frobenius.pw K q) (e2_shift N m)) (2 * N + 1)
    rw [show 2 * (N + 1) = 2 * N + 1 + 1 by lib_nat, em_shift, hc, hc2]
    show K.mul (K.neg K.one) (K.add (K.add (em K (fun m => Frobenius.pw K q (Ex2 N m)) (K.neg K.one) (2 * N))
        (K.mul (Cheb.emb K (2 * N)) (K.mul (Frobenius.pw K q (Ex2 N (2 * N))) (Frobenius.pw K (K.neg K.one) (2 * N)))))
      (K.add (es K (fun m => Frobenius.pw K q (Ex2 N m)) (K.neg K.one) (2 * N))
        (K.mul (Frobenius.pw K q (Ex2 N (2 * N))) (Frobenius.pw K (K.neg K.one) (2 * N))))) =
      K.mul (K.mul (Frobenius.pw K (K.neg K.one) N) (K.neg K.one))
        (K.add (T K q N) (K.mul (Frobenius.pw K (K.neg K.one) N) (K.mul (K.add (Cheb.emb K (2 * N)) K.one) (Frobenius.pw K q (N * N + N)))))
    rw [v_pair N, w_zero, pm_even, a_up]
    have hs := pm_sq K N
    generalize Frobenius.pw K (K.neg K.one) N = s at hs ⊢
    calc _ = K.mul (K.neg K.one) (K.add (K.mul s (T K q N)) (K.mul (K.add (Cheb.emb K (2 * N)) K.one) (Frobenius.pw K q (N * N + N)))) := by lib_ring
      _ = K.mul (K.neg K.one) (K.add (K.mul s (T K q N)) (K.mul (K.mul s s) (K.mul (K.add (Cheb.emb K (2 * N)) K.one) (Frobenius.pw K q (N * N + N))))) := by
          rw [hs]; lib_ring
      _ = _ := by lib_ring

/-- **Jacobi's η³ identity mod q^M**: Σ_{j < 2M+2} (−1)^j (2j+1) q^(j²+j) = (q²; q²)_(M+1)³. -/
theorem jacobi_eta3 (M : Nat) (hq : Frobenius.pw K q M = K.zero) :
    T K q (2 * M + 2) = K.mul (K.mul (pp K (K.mul q q) (M + 1)) (pp K (K.mul q q) (M + 1))) (pp K (K.mul q q) (M + 1)) := by
  have h := jacobi3_mod K q M hq
  have hz : Frobenius.pw K q (Ex2 (2 * M + 2) (2 * (2 * M + 2))) = K.zero := by
    rw [a_up]
    exact pw_zero_of_le K q M _ hq (Nat.le_trans (Nat.le_trans (Nat.le_add_left M (M + 2)) (Nat.le_of_eq (by lib_nat))) (Nat.le_add_left _ _))
  have e : em K (fun m => Frobenius.pw K q (Ex2 (2 * M + 2) m)) (K.neg K.one) (2 * (2 * M + 2) + 1) =
      K.add (em K (fun m => Frobenius.pw K q (Ex2 (2 * M + 2) m)) (K.neg K.one) (2 * (2 * M + 2)))
        (K.mul (Cheb.emb K (2 * (2 * M + 2))) (K.mul (Frobenius.pw K q (Ex2 (2 * M + 2) (2 * (2 * M + 2)))) (Frobenius.pw K (K.neg K.one) (2 * (2 * M + 2))))) := rfl
  rw [e, hz, v_pair, sg_odd, show 2 * M + 2 = 2 * (M + 1) by lib_nat, pm_even] at h
  rw [show 2 * M + 2 = 2 * (M + 1) by lib_nat]
  calc _ = K.add (K.mul K.one (T K q (2 * (M + 1)))) (K.mul (Cheb.emb K (2 * (2 * (M + 1)))) (K.mul K.zero (Frobenius.pw K (K.neg K.one) (2 * (2 * (M + 1)))))) := by lib_ring
    _ = _ := by rw [h]; lib_ring

end QBinom

#print axioms QBinom.es_congr
#print axioms QBinom.em_congr
#print axioms QBinom.es_shift
#print axioms QBinom.em_shift
#print axioms QBinom.e2_shift
#print axioms QBinom.pm_even
#print axioms QBinom.pm_sq
#print axioms QBinom.sg_odd
#print axioms QBinom.a_dn
#print axioms QBinom.a_up
#print axioms QBinom.w_zero
#print axioms QBinom.v_pair
#print axioms QBinom.jacobi_eta3

lang/JacobiPrimary.lean

lean_all/lang/JacobiPrimary.lean

/-
  lang/JacobiPrimary — G2 (T43): the cubic Jacobi sum is primary, J ≡ −1 (mod 3).

  Reduce ℤ[ω][C_p] mod 3: φ = castZ componentwise, ℤ[ω] → 𝔽₃[ω] (a ring map), and on the group ring (map_mul). In 𝔽₃[ω][C_p]
  (characteristic 3) the Frobenius gives g³ = Σ_a χ(a)³ ζ^{3a} = Σ_{a≠0} ζ^{3a}, whose ζ¹ coefficient is 1 (3 is a unit mod p);
  in ℤ[ω][C_p], g³ = g² g = J ḡ g = J (pζ⁰ − N), whose ζ¹ coefficient is −J. Hence φ(J)·(−1) = 1:
      jacobi_primary   J = a + bω with a ≡ −1, b ≡ 0 (mod 3)       (as castZ a = −1, castZ b = 0 in 𝔽₃)
  With JacobiNorm: J is the primary prime of ℤ[ω] above p (norm p, J ≡ −1 mod 3).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.JacobiNorm
import lang.Eisen
import lang.IntCast
import lang.HasseDavenport

namespace JacobiPrimary

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius CubicChar JacobiSum JacobiNorm GroupRing GroupRingFrob
open LibRing (castZ castN)

variable (n : Nat)

def phi (z : Int × Int) : Zp 2 × Zp 2 := (castZ (ring 2) z.1, castZ (ring 2) z.2)

theorem phi_add (x y : Int × Int) : phi ((Cyclo3.cyc3 LibInt.intR).add x y) = (Cyclo3.cyc3 (ring 2)).add (phi x) (phi y) := by
  apply Prod.ext
  · exact IntCast.castZ_add (ring 2) x.1 y.1
  · exact IntCast.castZ_add (ring 2) x.2 y.2

theorem phi_mul (x y : Int × Int) : phi ((Cyclo3.cyc3 LibInt.intR).mul x y) = (Cyclo3.cyc3 (ring 2)).mul (phi x) (phi y) := by
  apply Prod.ext
  · show castZ (ring 2) (x.1 * y.1 + -(x.2 * y.2)) = (ring 2).add ((ring 2).mul (castZ (ring 2) x.1) (castZ (ring 2) y.1)) ((ring 2).neg ((ring 2).mul (castZ (ring 2) x.2) (castZ (ring 2) y.2)))
    rw [IntCast.castZ_add, HasseDavenport.castZ_neg, HasseDavenport.castZ_mul, HasseDavenport.castZ_mul]
  · show castZ (ring 2) (x.1 * y.2 + x.2 * y.1 + -(x.2 * y.2)) = (ring 2).add ((ring 2).add ((ring 2).mul (castZ (ring 2) x.1) (castZ (ring 2) y.2)) ((ring 2).mul (castZ (ring 2) x.2) (castZ (ring 2) y.1))) ((ring 2).neg ((ring 2).mul (castZ (ring 2) x.2) (castZ (ring 2) y.2)))
    rw [IntCast.castZ_add, IntCast.castZ_add, HasseDavenport.castZ_neg, HasseDavenport.castZ_mul, HasseDavenport.castZ_mul, HasseDavenport.castZ_mul]

theorem phi_sum {A : Type} (f : A → Int × Int) : ∀ l : List A, phi (sumK (Cyclo3.cyc3 LibInt.intR) f l) = sumK (Cyclo3.cyc3 (ring 2)) (fun a => phi (f a)) l
  | [] => rfl
  | a :: l => by
    show phi ((Cyclo3.cyc3 LibInt.intR).add (f a) (sumK (Cyclo3.cyc3 LibInt.intR) f l)) = (Cyclo3.cyc3 (ring 2)).add (phi (f a)) (sumK (Cyclo3.cyc3 (ring 2)) (fun a => phi (f a)) l)
    rw [phi_add, phi_sum f l]

theorem phi_neg (x : Int × Int) : phi ((Cyclo3.cyc3 LibInt.intR).neg x) = (Cyclo3.cyc3 (ring 2)).neg (phi x) := by
  apply Prod.ext
  · exact HasseDavenport.castZ_neg (ring 2) x.1
  · exact HasseDavenport.castZ_neg (ring 2) x.2

theorem phi_one : phi (Cyclo3.cyc3 LibInt.intR).one = (Cyclo3.cyc3 (ring 2)).one := by
  apply Prod.ext
  · show castN (ring 2) 1 = (ring 2).one
    exact add_zero' (ring 2) _
  · rfl

def map (u : V (Int × Int) ((6 * n + 5 + 1) + 1)) : V (Zp 2 × Zp 2) ((6 * n + 5 + 1) + 1) := tb (6 * n + 5 + 1) (fun a => phi (gt (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) u a))

theorem map_mul (u v : V (Int × Int) ((6 * n + 5 + 1) + 1)) : gmul (Cyclo3.cyc3 (ring 2)) (6 * n + 5 + 1) (map n u) (map n v) = map n (gmul (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) u v) :=
  ext (Cyclo3.cyc3 (ring 2)) (6 * n + 5 + 1) _ _ (fun c => by
    unfold map
    rw [gt_tb, gt_mul, gt_mul, phi_sum]
    exact sumK_ext (Cyclo3.cyc3 (ring 2)) _ _ (fun a => by rw [gt_tb, gt_tb, phi_mul]) _)

theorem char3 : nsm (Cyclo3.cyc3 (ring 2)) 3 (Cyclo3.cyc3 (ring 2)).one = (Cyclo3.cyc3 (ring 2)).zero := rfl

/-- reindexing by a unit Q of 𝔽_p, with values in 𝔽₃[ω]. -/
theorem coef3 (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (Q : Zp (6 * n + 5 + 1)) (hQ : Q ≠ (ring (6 * n + 5 + 1)).zero) (s : Zp (6 * n + 5 + 1) → Zp 2 × Zp 2) (c : Zp (6 * n + 5 + 1)) :
    sumK (Cyclo3.cyc3 (ring 2)) (fun a => (Cyclo3.cyc3 (ring 2)).mul (s a) (delta (Cyclo3.cyc3 (ring 2)) c ((ring (6 * n + 5 + 1)).mul Q a))) (GroupRing.L (6 * n + 5 + 1)) = s ((ring (6 * n + 5 + 1)).mul (iv n Q) c) := by
  have hQQ := iv_mul n hpr Q hQ
  have e := reindex (Cyclo3.cyc3 (ring 2)) _ (CMInert.complete_allZ (6 * n + 5 + 1) (Cyclo3.cyc3 (ring 2))) (fun a => (ring (6 * n + 5 + 1)).mul Q a) (fun b => (ring (6 * n + 5 + 1)).mul (iv n Q) b)
    (fun x => by rw [show (ring (6 * n + 5 + 1)).mul (iv n Q) ((ring (6 * n + 5 + 1)).mul Q x) = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (iv n Q) Q) x by lib_ring, hQQ]; lib_ring)
    (fun y => by rw [show (ring (6 * n + 5 + 1)).mul Q ((ring (6 * n + 5 + 1)).mul (iv n Q) y) = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (iv n Q) Q) y by lib_ring, hQQ]; lib_ring)
    (fun b => (Cyclo3.cyc3 (ring 2)).mul (delta (Cyclo3.cyc3 (ring 2)) b c) (s ((ring (6 * n + 5 + 1)).mul (iv n Q) b)))
  have e2 : sumK (Cyclo3.cyc3 (ring 2)) (fun a => (Cyclo3.cyc3 (ring 2)).mul (s a) (delta (Cyclo3.cyc3 (ring 2)) c ((ring (6 * n + 5 + 1)).mul Q a))) (GroupRing.L (6 * n + 5 + 1))
      = sumK (Cyclo3.cyc3 (ring 2)) (fun a => (fun b => (Cyclo3.cyc3 (ring 2)).mul (delta (Cyclo3.cyc3 (ring 2)) b c) (s ((ring (6 * n + 5 + 1)).mul (iv n Q) b))) ((ring (6 * n + 5 + 1)).mul Q a)) (GroupRing.L (6 * n + 5 + 1)) :=
    sumK_ext (Cyclo3.cyc3 (ring 2)) _ _ (fun a => by
      show _ = (Cyclo3.cyc3 (ring 2)).mul (delta (Cyclo3.cyc3 (ring 2)) ((ring (6 * n + 5 + 1)).mul Q a) c) (s ((ring (6 * n + 5 + 1)).mul (iv n Q) ((ring (6 * n + 5 + 1)).mul Q a)))
      rw [show (ring (6 * n + 5 + 1)).mul (iv n Q) ((ring (6 * n + 5 + 1)).mul Q a) = a by rw [show (ring (6 * n + 5 + 1)).mul (iv n Q) ((ring (6 * n + 5 + 1)).mul Q a) = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (iv n Q) Q) a by lib_ring, hQQ]; lib_ring,
        delta_symm, (Cyclo3.cyc3 (ring 2)).mul_comm]) _
  rw [e2]
  exact e.trans (pick (Cyclo3.cyc3 (ring 2)) _ (CMInert.complete_allZ (6 * n + 5 + 1) (Cyclo3.cyc3 (ring 2))) _ c)

theorem cube1 (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (a : Zp (6 * n + 5 + 1)) (ha : a ≠ (ring (6 * n + 5 + 1)).zero) : pw (Cyclo3.cyc3 (ring 2)) (phi (chi3 n w a)) 3 = (Cyclo3.cyc3 (ring 2)).one := by
  match exists_i n hpr w hw1 hw3 a ha with
  | ⟨i, hi, ei⟩ =>
    rw [val_of n hpr w hw1 hw3 a ha i hi ei]
    match i, hi with
    | 0, _ => rfl
    | 1, _ => rfl
    | 2, _ => rfl
    | k + 3, hk => exact absurd hk (fun h => Nat.lt_irrefl 3 (Nat.lt_of_le_of_lt (Nat.le_add_left 3 k) h))

theorem Q3_ne : nsm (ring (6 * n + 5 + 1)) 3 (ring (6 * n + 5 + 1)).one ≠ (ring (6 * n + 5 + 1)).zero := fun e => by
  apply Eisen.three_ne_zero (6 * n + 5) (by rw [show 6 * n + 5 = (6 * n + 3) + 2 by lib_nat]; exact Nat.le_add_left 2 _)
  rw [← e]
  show (ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one (ring (6 * n + 5 + 1)).one) = (ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).zero (ring (6 * n + 5 + 1)).one) (ring (6 * n + 5 + 1)).one) (ring (6 * n + 5 + 1)).one
  lib_ring

/-- **J ≡ −1 (mod 3)**: φ(J) = −1 in 𝔽₃[ω]. -/
theorem jacobi_primary (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : castZ (ring 2) (J n w).1 = (ring 2).neg (ring 2).one ∧ castZ (ring 2) (J n w).2 = (ring 2).zero := by
  have Q0 := Q3_ne n
  -- Frobenius side
  have hF : gt (Cyclo3.cyc3 (ring 2)) (6 * n + 5 + 1) (pw (gring (Cyclo3.cyc3 (ring 2)) (6 * n + 5 + 1)) (map n (G1 n w)) 3) (ring (6 * n + 5 + 1)).one = (Cyclo3.cyc3 (ring 2)).one := by
    have hd := decomp (Cyclo3.cyc3 (ring 2)) (6 * n + 5 + 1) (map n (G1 n w))
    have e1 := congrArg (fun u => gt (Cyclo3.cyc3 (ring 2)) (6 * n + 5 + 1) (pw (gring (Cyclo3.cyc3 (ring 2)) (6 * n + 5 + 1)) u (1 + 1 + 1)) (ring (6 * n + 5 + 1)).one) hd
    refine e1.trans ?_
    show gt (Cyclo3.cyc3 (ring 2)) (6 * n + 5 + 1) (pw (gring (Cyclo3.cyc3 (ring 2)) (6 * n + 5 + 1)) (sumK (gring (Cyclo3.cyc3 (ring 2)) (6 * n + 5 + 1)) _ (GroupRing.L (6 * n + 5 + 1))) (1 + 1 + 1)) (ring (6 * n + 5 + 1)).one = (Cyclo3.cyc3 (ring 2)).one
    rw [frob_sum (Cyclo3.cyc3 (ring 2)) (6 * n + 5 + 1) 1 (fun d h => Eisen.prime3 d h) char3, gt_sum]
    have e2 : sumK (Cyclo3.cyc3 (ring 2)) (fun a => gt (Cyclo3.cyc3 (ring 2)) (6 * n + 5 + 1) (pw (gring (Cyclo3.cyc3 (ring 2)) (6 * n + 5 + 1)) (mono (Cyclo3.cyc3 (ring 2)) (6 * n + 5 + 1) a (gt (Cyclo3.cyc3 (ring 2)) (6 * n + 5 + 1) (map n (G1 n w)) a)) (1 + 1 + 1)) (ring (6 * n + 5 + 1)).one) (GroupRing.L (6 * n + 5 + 1))
        = sumK (Cyclo3.cyc3 (ring 2)) (fun a => (Cyclo3.cyc3 (ring 2)).mul (pw (Cyclo3.cyc3 (ring 2)) (phi (chi3 n w a)) 3) (delta (Cyclo3.cyc3 (ring 2)) (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).mul (nsm (ring (6 * n + 5 + 1)) 3 (ring (6 * n + 5 + 1)).one) a))) (GroupRing.L (6 * n + 5 + 1)) :=
      sumK_ext (Cyclo3.cyc3 (ring 2)) _ _ (fun a => by
        rw [mono_pow, gt_mono, nsm_eq_mul]
        unfold map G1; rw [gt_tb, gt_tb]) _
    rw [e2, coef3 n hpr _ Q0 (fun a => pw (Cyclo3.cyc3 (ring 2)) (phi (chi3 n w a)) 3) (ring (6 * n + 5 + 1)).one]
    apply cube1 n hpr w hw1 hw3
    intro e0
    have := congrArg ((ring (6 * n + 5 + 1)).mul (nsm (ring (6 * n + 5 + 1)) 3 (ring (6 * n + 5 + 1)).one)) e0
    rw [← (ring (6 * n + 5 + 1)).mul_assoc, (ring (6 * n + 5 + 1)).mul_comm (nsm (ring (6 * n + 5 + 1)) 3 (ring (6 * n + 5 + 1)).one), iv_mul n hpr _ Q0, (ring (6 * n + 5 + 1)).one_mul, mul_zero] at this
    exact one_ne_zero'' n this
  -- integral side
  have hI : pw (gring (Cyclo3.cyc3 (ring 2)) (6 * n + 5 + 1)) (map n (G1 n w)) 3 = map n (gmul (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (J n w)) (Xv n)) := by
    show gmul (Cyclo3.cyc3 (ring 2)) (6 * n + 5 + 1) (gmul (Cyclo3.cyc3 (ring 2)) (6 * n + 5 + 1) (gmul (Cyclo3.cyc3 (ring 2)) (6 * n + 5 + 1) (gone (Cyclo3.cyc3 (ring 2)) (6 * n + 5 + 1)) (map n (G1 n w))) (map n (G1 n w))) (map n (G1 n w)) = _
    rw [gm_one, map_mul, map_mul, sq1 n hpr w hw1 hw3]
    rw [show gmul (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (gmul (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (J n w)) (G1 n ((ring (6 * n + 5 + 1)).mul w w))) (G1 n w)
        = gmul (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (J n w)) (gmul (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (G1 n w) (G1 n ((ring (6 * n + 5 + 1)).mul w w))) by
          rw [gm_assoc, gm_comm (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (G1 n ((ring (6 * n + 5 + 1)).mul w w))], gg n hpr w hw1 hw3]
  rw [hI] at hF
  unfold map at hF
  rw [gt_tb, mono0_mulg] at hF
  have hX1 : gt (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (Xv n) (ring (6 * n + 5 + 1)).one = (Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one := by
    unfold Xv gadd gneg Nv
    rw [gt_tb, gt_mono, gt_tb, gt_tb]
    unfold delta; rw [ite_neg' _ _ (one_ne_zero'' n)]
    lib_ring
  rw [hX1, phi_mul] at hF
  have hJ : phi (J n w) = (Cyclo3.cyc3 (ring 2)).neg (Cyclo3.cyc3 (ring 2)).one := by
    calc phi (J n w) = (Cyclo3.cyc3 (ring 2)).mul ((Cyclo3.cyc3 (ring 2)).mul (phi (J n w)) (phi ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one))) ((Cyclo3.cyc3 (ring 2)).neg (Cyclo3.cyc3 (ring 2)).one) := by
          rw [phi_neg, phi_one]; lib_ring
      _ = (Cyclo3.cyc3 (ring 2)).neg (Cyclo3.cyc3 (ring 2)).one := by rw [hF]; lib_ring
  exact ⟨congrArg Prod.fst hJ, congrArg Prod.snd hJ⟩

end JacobiPrimary

#print axioms JacobiPrimary.phi_add
#print axioms JacobiPrimary.phi_mul
#print axioms JacobiPrimary.phi_sum
#print axioms JacobiPrimary.phi_neg
#print axioms JacobiPrimary.phi_one
#print axioms JacobiPrimary.map_mul
#print axioms JacobiPrimary.char3
#print axioms JacobiPrimary.coef3
#print axioms JacobiPrimary.cube1
#print axioms JacobiPrimary.Q3_ne
#print axioms JacobiPrimary.jacobi_primary

lang/JacobiS.lean

lean_all/lang/JacobiS.lean

/-
  lang/JacobiS — T47, cycle 553: the shifted triple product ∏_{k=1}^{N} (1 + x q^(2k))(x + q^(2k−2)), which has no q^N prefactor.

    Ex2 N m = (m − N)(m − N + 1) + (N − m − 1)(N − m)   (= j² + j, j = m − N)
    e2_up, e2_dn, e2_0, e2_1, e2_1b, e2_a, e2_b, e2_c    exponent identities
    dd N m := [2N; m]_{q²} · q^(Ex2 N m)
    dd_h0, dd_h1, dd_h2    dd (N+1) satisfies the three-term recursion of multiplying by
                           q^(2N) + (1 + q^(4N+2)) x + q^(2N+2) x²,
  so dd N m is the coefficient of x^m in the product (Jacobi's η³ comes from its x·d/dx at x = −1).
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.JacobiMod

namespace QBinom

open FTAChain

def Ex2 (N m : Nat) : Nat := (m - N) * (m - N + 1) + (N - (m + 1)) * (N - (m + 1) + 1)

theorem e2_up (N t : Nat) : Ex2 N (N + t) = t * t + t := by
  show (N + t - N) * (N + t - N + 1) + (N - (N + t + 1)) * (N - (N + t + 1) + 1) = _
  rw [sub_add_l, show N + t + 1 = N + (t + 1) by lib_nat, sub_add_r]; lib_nat

theorem e2_dn (m t : Nat) : Ex2 (m + 1 + t) m = t * t + t := by
  show (m - (m + 1 + t)) * (m - (m + 1 + t) + 1) + (m + 1 + t - (m + 1)) * (m + 1 + t - (m + 1) + 1) = _
  rw [sub_add_l (m + 1) t, show m + 1 + t = m + (1 + t) by lib_nat, sub_add_r]; lib_nat

theorem e2_0 (N : Nat) : Ex2 (N + 1) 0 = 2 * N + Ex2 N 0 := by
  cases N with
  | zero => rfl
  | succ u =>
    rw [show u + 1 + 1 = 0 + 1 + (u + 1) by lib_nat, e2_dn 0 (u + 1), show u + 1 = 0 + 1 + u by lib_nat, e2_dn 0 u]; lib_nat

theorem e2_1b (N : Nat) : Ex2 (N + 1) 1 = Ex2 N 0 := by
  cases N with
  | zero => rfl
  | succ u => rw [show u + 1 + 1 = 1 + 1 + u by lib_nat, e2_dn 1 u, show u + 1 = 0 + 1 + u by lib_nat, e2_dn 0 u]

theorem e2_1 (N : Nat) : 2 * N + Ex2 N 1 = 2 + Ex2 (N + 1) 1 := by
  cases N with
  | zero => rfl
  | succ u =>
    cases u with
    | zero => rfl
    | succ v =>
      rw [show v + 1 + 1 + 1 = 1 + 1 + (v + 1) by lib_nat, e2_dn 1 (v + 1), show v + 1 + 1 = 1 + 1 + v by lib_nat, e2_dn 1 v]; lib_nat

theorem e2_a (N m : Nat) : 2 * N + Ex2 N (m + 2) = 2 * (m + 2) + Ex2 (N + 1) (m + 2) := by
  cases Nat.lt_or_ge N (m + 2) with
  | inl h =>
    obtain ⟨t, ht⟩ := Cheb.le_dest2 (N + 1) (m + 2) h
    rw [← ht, show t + (N + 1) = (N + 1) + t by lib_nat, e2_up (N + 1) t, show N + 1 + t = N + (t + 1) by lib_nat, e2_up N (t + 1)]; lib_nat
  | inr h =>
    obtain ⟨t, ht⟩ := Cheb.le_dest2 (m + 2) N h
    rw [← ht]
    cases t with
    | zero =>
      rw [show 0 + (m + 2) + 1 = (m + 2) + 1 + 0 by lib_nat, e2_dn (m + 2) 0, show 0 + (m + 2) = (m + 2) + 0 by lib_nat, e2_up (m + 2) 0]
    | succ s =>
      rw [show s + 1 + (m + 2) + 1 = (m + 2) + 1 + (s + 1) by lib_nat, e2_dn (m + 2) (s + 1),
        show s + 1 + (m + 2) = (m + 2) + 1 + s by lib_nat, e2_dn (m + 2) s]; lib_nat

theorem e2_b (N m : Nat) : Ex2 (N + 1) (m + 2) = Ex2 N (m + 1) := by
  cases Nat.lt_or_ge N (m + 1) with
  | inl h =>
    obtain ⟨t, ht⟩ := Cheb.le_dest2 (N + 1) (m + 1) h
    have e1 : m + 2 = (N + 1) + (t + 1) := by rw [show m + 2 = (m + 1) + 1 from rfl, ← ht]; lib_nat
    have e2 : m + 1 = N + (t + 1) := by rw [← ht]; lib_nat
    rw [e1, e2, e2_up, e2_up]
  | inr h =>
    obtain ⟨t, ht⟩ := Cheb.le_dest2 (m + 1) N h
    rw [← ht]
    cases t with
    | zero =>
      rw [show 0 + (m + 1) + 1 = m + 2 by lib_nat, show 0 + (m + 1) = m + 1 by lib_nat]
      exact (e2_up (m + 2) 0).trans (e2_up (m + 1) 0).symm
    | succ s =>
      rw [show s + 1 + (m + 1) + 1 = (m + 2) + 1 + s by lib_nat, show s + 1 + (m + 1) = (m + 1) + 1 + s by lib_nat, e2_dn, e2_dn]

theorem e2_c (N m : Nat) (hm : m ≤ 2 * N) : 2 * N + 2 + Ex2 N m = 2 * (2 * N - m) + Ex2 (N + 1) (m + 2) := by
  obtain ⟨s, hs⟩ := Cheb.le_dest2 m (2 * N) hm
  have h2 : 2 * N - m = s := by rw [← hs]; exact ex_c.sub_add_l' s m
  rw [h2]
  cases Nat.lt_or_ge m N with
  | inl h =>
    obtain ⟨t, ht⟩ := Cheb.le_dest2 (m + 1) N h
    have hs' : s = m + 2 * t + 2 := by
      have := hs; rw [← ht] at this
      exact Cheb.add_rc m _ _ (this.trans (by lib_nat))
    rw [hs', ← ht]
    cases t with
    | zero =>
      rw [show 0 + (m + 1) + 1 = (m + 2) + 0 by lib_nat, show 0 + (m + 1) = m + 1 + 0 by lib_nat, e2_up (m + 2) 0, e2_dn m 0]; lib_nat
    | succ u =>
      rw [show u + 1 + (m + 1) + 1 = (m + 2) + 1 + u by lib_nat, show u + 1 + (m + 1) = m + 1 + (u + 1) by lib_nat,
        e2_dn (m + 2) u, e2_dn m (u + 1)]; lib_nat
  | inr h =>
    obtain ⟨t, ht⟩ := Cheb.le_dest2 N m h
    have hs' : s + t = N := Cheb.add_rc N _ _ ((show s + t + N = s + (t + N) by lib_nat).trans (by rw [ht]; exact hs.trans (by lib_nat)))
    rw [← ht, show t + N = N + t by lib_nat, e2_up, show N + t + 2 = (N + 1) + (t + 1) by lib_nat, e2_up]
    rw [← hs']; lib_nat

variable {R : Type} (K : DetChain.CRing R) (q : R)

def dd (N m : Nat) : R := K.mul (gb K (K.mul q q) (2 * N) m) (Frobenius.pw K q (Ex2 N m))

theorem dd_h0 (N : Nat) : dd K q (N + 1) 0 = K.mul (Frobenius.pw K q (2 * N)) (dd K q N 0) := by
  show K.mul (gb K (K.mul q q) (2 * (N + 1)) 0) (Frobenius.pw K q (Ex2 (N + 1) 0)) =
    K.mul (Frobenius.pw K q (2 * N)) (K.mul (gb K (K.mul q q) (2 * N) 0) (Frobenius.pw K q (Ex2 N 0)))
  rw [gb_zero, gb_zero, e2_0, ← pw_join K q (2 * N) (Ex2 N 0) _ rfl]; lib_ring

theorem dd_h1 (N : Nat) : dd K q (N + 1) 1 = K.add (K.mul (Frobenius.pw K q (2 * N)) (dd K q N 1))
    (K.mul (K.add K.one (Frobenius.pw K q (4 * N + 2))) (dd K q N 0)) := by
  have g1 : gb K (K.mul q q) (2 * (N + 1)) 1 = K.add (K.mul (Frobenius.pw K (K.mul q q) 1)
      (K.add (gb K (K.mul q q) (2 * N) 1) (K.mul (Frobenius.pw K (K.mul q q) (2 * N)) K.one))) K.one := by
    rw [show 2 * (N + 1) = 2 * N + 1 + 1 by lib_nat]
    show gb K (K.mul q q) (2 * N + 1 + 1) (0 + 1) = _
    rw [pascalA, gb_zero]
    show K.add (K.mul (Frobenius.pw K (K.mul q q) (0 + 1)) (K.add (gb K (K.mul q q) (2 * N) (0 + 1))
      (K.mul (Frobenius.pw K (K.mul q q) (2 * N - 0)) (gb K (K.mul q q) (2 * N) 0)))) K.one = _
    rw [gb_zero]; rfl
  show K.mul (gb K (K.mul q q) (2 * (N + 1)) 1) (Frobenius.pw K q (Ex2 (N + 1) 1)) =
    K.add (K.mul (Frobenius.pw K q (2 * N)) (K.mul (gb K (K.mul q q) (2 * N) 1) (Frobenius.pw K q (Ex2 N 1))))
      (K.mul (K.add K.one (Frobenius.pw K q (4 * N + 2))) (K.mul (gb K (K.mul q q) (2 * N) 0) (Frobenius.pw K q (Ex2 N 0))))
  rw [g1, gb_zero, pwQ, pwQ, e2_1b]
  have t1 : K.mul (Frobenius.pw K q (2 * N)) (Frobenius.pw K q (Ex2 N 1)) =
      K.mul (Frobenius.pw K q (2 * 1)) (Frobenius.pw K q (Ex2 N 0)) := by
    rw [pw_join K q _ _ _ rfl, pw_join K q _ _ _ rfl]
    exact congrArg (Frobenius.pw K q) ((e2_1 N).trans (by rw [e2_1b]))
  have t2 : K.mul (Frobenius.pw K q (2 * 1)) (Frobenius.pw K q (2 * (2 * N))) = Frobenius.pw K q (4 * N + 2) :=
    pw_join K q _ _ _ (by lib_nat)
  rw [← t2]
  generalize gb K (K.mul q q) (2 * N) 1 = G
  calc _ = K.add (K.mul (K.mul (Frobenius.pw K q (2 * 1)) (Frobenius.pw K q (Ex2 N 0))) G)
        (K.mul (K.add K.one (K.mul (Frobenius.pw K q (2 * 1)) (Frobenius.pw K q (2 * (2 * N))))) (Frobenius.pw K q (Ex2 N 0))) := by lib_ring
    _ = K.add (K.mul (K.mul (Frobenius.pw K q (2 * N)) (Frobenius.pw K q (Ex2 N 1))) G)
        (K.mul (K.add K.one (K.mul (Frobenius.pw K q (2 * 1)) (Frobenius.pw K q (2 * (2 * N))))) (Frobenius.pw K q (Ex2 N 0))) := by rw [t1]
    _ = _ := by lib_ring

theorem dd_h2 (N m : Nat) : dd K q (N + 1) (m + 2) = K.add (K.add (K.mul (Frobenius.pw K q (2 * N)) (dd K q N (m + 2)))
    (K.mul (K.add K.one (Frobenius.pw K q (4 * N + 2))) (dd K q N (m + 1)))) (K.mul (Frobenius.pw K q (2 * N + 2)) (dd K q N m)) := by
  show K.mul (gb K (K.mul q q) (2 * (N + 1)) (m + 2)) (Frobenius.pw K q (Ex2 (N + 1) (m + 2))) =
    K.add (K.add (K.mul (Frobenius.pw K q (2 * N)) (K.mul (gb K (K.mul q q) (2 * N) (m + 2)) (Frobenius.pw K q (Ex2 N (m + 2)))))
      (K.mul (K.add K.one (Frobenius.pw K q (4 * N + 2))) (K.mul (gb K (K.mul q q) (2 * N) (m + 1)) (Frobenius.pw K q (Ex2 N (m + 1))))))
      (K.mul (Frobenius.pw K q (2 * N + 2)) (K.mul (gb K (K.mul q q) (2 * N) m) (Frobenius.pw K q (Ex2 N m))))
  cases Nat.lt_or_ge (2 * N) m with
  | inl hbig =>
    rw [gb_big K _ (2 * (N + 1)) (m + 2) (by rw [show 2 * (N + 1) = 2 * N + 2 by lib_nat]; exact Nat.succ_lt_succ (Nat.succ_lt_succ hbig)),
      gb_big K _ (2 * N) (m + 2) (Nat.lt_trans hbig (Nat.lt_trans (Nat.lt_succ_self m) (Nat.lt_succ_self _))),
      gb_big K _ (2 * N) (m + 1) (Nat.lt_trans hbig (Nat.lt_succ_self m)), gb_big K _ (2 * N) m hbig]
    lib_ring
  | inr hle =>
    rw [show 2 * (N + 1) = 2 * N + 2 by lib_nat, dpascal K _ (2 * N) m hle, pwQ, pwQ, pwQ]
    have t1 : K.mul (Frobenius.pw K q (2 * N)) (Frobenius.pw K q (Ex2 N (m + 2))) =
        K.mul (Frobenius.pw K q (2 * (m + 2))) (Frobenius.pw K q (Ex2 (N + 1) (m + 2))) := by
      rw [pw_join K q _ _ _ rfl, pw_join K q _ _ _ rfl]; exact congrArg (Frobenius.pw K q) (e2_a N m)
    have t2 : Frobenius.pw K q (Ex2 N (m + 1)) = Frobenius.pw K q (Ex2 (N + 1) (m + 2)) := congrArg _ (e2_b N m).symm
    have t3 : K.mul (Frobenius.pw K q (2 * N + 2)) (Frobenius.pw K q (Ex2 N m)) =
        K.mul (Frobenius.pw K q (2 * (2 * N - m))) (Frobenius.pw K q (Ex2 (N + 1) (m + 2))) := by
      rw [pw_join K q _ _ _ rfl, pw_join K q _ _ _ rfl]; exact congrArg (Frobenius.pw K q) (e2_c N m hle)
    have t4 : Frobenius.pw K q (2 * (2 * N + 1)) = Frobenius.pw K q (4 * N + 2) := congrArg _ (by lib_nat)
    rw [t4]
    generalize gb K (K.mul q q) (2 * N) (m + 2) = G2 at *
    generalize gb K (K.mul q q) (2 * N) (m + 1) = G1 at *
    generalize gb K (K.mul q q) (2 * N) m = G0 at *
    symm
    calc _ = K.add (K.add (K.mul (K.mul (Frobenius.pw K q (2 * N)) (Frobenius.pw K q (Ex2 N (m + 2)))) G2)
          (K.mul (K.add K.one (Frobenius.pw K q (4 * N + 2))) (K.mul G1 (Frobenius.pw K q (Ex2 N (m + 1))))))
          (K.mul (K.mul (Frobenius.pw K q (2 * N + 2)) (Frobenius.pw K q (Ex2 N m))) G0) := by lib_ring
      _ = _ := by rw [t1, t2, t3]; lib_ring

end QBinom

#print axioms QBinom.e2_up
#print axioms QBinom.e2_dn
#print axioms QBinom.e2_0
#print axioms QBinom.e2_1b
#print axioms QBinom.e2_1
#print axioms QBinom.e2_a
#print axioms QBinom.e2_b
#print axioms QBinom.e2_c
#print axioms QBinom.dd_h0
#print axioms QBinom.dd_h1
#print axioms QBinom.dd_h2

lang/JacobiSum.lean

lean_all/lang/JacobiSum.lean

/-
  lang/JacobiSum — G2 (T43): the character sums behind the cubic Jacobi sum.

  p = 6n + 7, χ = CubicChar.chi3 (values 0, 1, ω, ω² in ℤ[ω]), χ̄ = χ² (cb), ι(x) = x^(p−2) (ι(0) = 0), J = Σ_t χ(t) χ(1 − t).
    chi_one, chi_cube, cb_mul, chi_iv      χ(1) = 1, χ χ̄ = 1 off 0, χ̄ multiplicative, χ(ι x) = χ̄(x)
    kill, cb_sum                           (1 − ω^i) S = 0 (i = 1, 2) ⇒ S = 0;  Σ χ̄ = 0
    D_zero, D_ne                           Σ_b χ(b) χ̄(b) = p − 1;  c ≠ 0: Σ_b χ(c + b) χ̄(b) = −1
    S_zero, S_ne                           Σ_a χ(a) χ(−a) = 0;  c ≠ 0: Σ_a χ(a) χ(c − a) = χ̄(c) J
  In ℤ[ω][C_p] these say g(χ) g(χ̄) = pζ⁰ − N and g(χ)² = J g(χ̄) (next file), whence J J̄ = p.
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.CubicChar

namespace JacobiSum

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius CubicChar

variable (n : Nat)

def cb (w x : Zp (6 * n + 5 + 1)) : Int × Int := (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w x) (chi3 n w x)
def iv (x : Zp (6 * n + 5 + 1)) : Zp (6 * n + 5 + 1) := pw (ring (6 * n + 5 + 1)) x (6 * n + 5)
def LL : List (Zp (6 * n + 5 + 1)) := CMInert.allZ (6 * n + 5 + 1) ((6 * n + 5 + 1) + 1)
def J (w : Zp (6 * n + 5 + 1)) : Int × Int := sumK (Cyclo3.cyc3 LibInt.intR) (fun t => (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w t) (chi3 n w ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).neg t)))) (LL n)

theorem iv_mul (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (x : Zp (6 * n + 5 + 1)) (hx : x ≠ (ring (6 * n + 5 + 1)).zero) : (ring (6 * n + 5 + 1)).mul (iv n x) x = (ring (6 * n + 5 + 1)).one := inv_exists (6 * n + 5) hpr x hx

theorem iv_zero : iv n (ring (6 * n + 5 + 1)).zero = (ring (6 * n + 5 + 1)).zero := HasseCM.pw_zero_succ (ring (6 * n + 5 + 1)) (6 * n + 4)

theorem iv_mulmul (x y : Zp (6 * n + 5 + 1)) : iv n ((ring (6 * n + 5 + 1)).mul x y) = (ring (6 * n + 5 + 1)).mul (iv n x) (iv n y) := pw_mul (ring (6 * n + 5 + 1)) x y (6 * n + 5)

theorem iv_iv (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (x : Zp (6 * n + 5 + 1)) : iv n (iv n x) = x := by
  by_cases hx : x = (ring (6 * n + 5 + 1)).zero
  · rw [hx, iv_zero, iv_zero]
  · show pw (ring (6 * n + 5 + 1)) (pw (ring (6 * n + 5 + 1)) x (6 * n + 5)) (6 * n + 5) = x
    rw [TwoPower.pwpow, show (6 * n + 5) * (6 * n + 5) = (6 * n + 5 + 1) * (6 * n + 4) + 1 by lib_nat,
      HasseCM.pw_add', ← TwoPower.pwpow, unit_pow (6 * n + 5) hpr x hx, TwoPower.pwone, (ring (6 * n + 5 + 1)).one_mul]
    exact (ring (6 * n + 5 + 1)).one_mul x

theorem one_ne_zero'' : (ring (6 * n + 5 + 1)).one ≠ (ring (6 * n + 5 + 1)).zero := PowerSum.one_ne_zero' (6 * n + 5)

theorem chi_one (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : chi3 n w (ring (6 * n + 5 + 1)).one = (Cyclo3.cyc3 LibInt.intR).one :=
  val_of n hpr w hw1 hw3 (ring (6 * n + 5 + 1)).one (one_ne_zero'' n) 0 (Nat.zero_lt_succ 2) (pw_one'' (ring (6 * n + 5 + 1)) _)

theorem chi_cube (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (x : Zp (6 * n + 5 + 1)) (hx : x ≠ (ring (6 * n + 5 + 1)).zero) : (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w x) (cb n w x) = (Cyclo3.cyc3 LibInt.intR).one := by
  match exists_i n hpr w hw1 hw3 x hx with
  | ⟨i, hi, ei⟩ =>
    unfold cb; rw [val_of n hpr w hw1 hw3 x hx i hi ei]
    match i, hi with
    | 0, _ => rfl
    | 1, _ => rfl
    | 2, _ => rfl
    | k + 3, hk => exact absurd hk (fun h => Nat.lt_irrefl 3 (Nat.lt_of_le_of_lt (Nat.le_add_left 3 k) h))

theorem cb_mul (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (x y : Zp (6 * n + 5 + 1)) : cb n w ((ring (6 * n + 5 + 1)).mul x y) = (Cyclo3.cyc3 LibInt.intR).mul (cb n w x) (cb n w y) := by
  unfold cb; rw [chi3_mul n hpr w hw1 hw3]; lib_ring

theorem chi_iv (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (x : Zp (6 * n + 5 + 1)) (hx : x ≠ (ring (6 * n + 5 + 1)).zero) : chi3 n w (iv n x) = cb n w x := by
  have h1 : (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w (iv n x)) (chi3 n w x) = (Cyclo3.cyc3 LibInt.intR).one := by rw [← chi3_mul n hpr w hw1 hw3, iv_mul n hpr x hx, chi_one n hpr w hw1 hw3]
  have h2 := chi_cube n hpr w hw1 hw3 x hx
  calc chi3 n w (iv n x) = (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w (iv n x)) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w x) (cb n w x)) := by rw [h2]; lib_ring
    _ = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w (iv n x)) (chi3 n w x)) (cb n w x) := by lib_ring
    _ = cb n w x := by rw [h1]; lib_ring

/-- (1 − ω^i) S = 0 for i = 1, 2 ⇒ S = 0. -/
theorem kill (S c : Int × Int) (hc : c = pw (Cyclo3.cyc3 LibInt.intR) om 1 ∨ c = pw (Cyclo3.cyc3 LibInt.intR) om 2) (h : (Cyclo3.cyc3 LibInt.intR).mul c S = S) : S = (Cyclo3.cyc3 LibInt.intR).zero := by
  match S, hc with
  | (a, b), Or.inl hc =>
    rw [hc] at h
    have h1 : (Cyclo3.c3mul LibInt.intR (0, 1) (a, b)).1 = a := congrArg Prod.fst h
    have h2 : (Cyclo3.c3mul LibInt.intR (0, 1) (a, b)).2 = b := congrArg Prod.snd h
    change 0 * a + -(1 * b) = a at h1
    change 0 * b + 1 * a + -(1 * b) = b at h2
    rw [← h1] at h2
    have hb : b = 0 := int3 b (by rw [show (3 : Int) * b = b - (0 * b + 1 * (0 * a + -(1 * b)) + -(1 * b)) by lib_int, h2]; lib_int)
    have ha : a = 0 := by rw [← h1, hb]; lib_int
    rw [ha, hb]; rfl
  | (a, b), Or.inr hc =>
    rw [hc] at h
    have h1 : (Cyclo3.c3mul LibInt.intR (-1, -1) (a, b)).1 = a := congrArg Prod.fst h
    have h2 : (Cyclo3.c3mul LibInt.intR (-1, -1) (a, b)).2 = b := congrArg Prod.snd h
    change -1 * a + -(-1 * b) = a at h1
    change -1 * b + -1 * a + -(-1 * b) = b at h2
    have hb' : b = -a := by
      calc b = -1 * b + -1 * a + -(-1 * b) := h2.symm
        _ = -a := by lib_int
    rw [hb'] at h1
    have ha : a = 0 := int3 a (by rw [show (3 : Int) * a = a - (-1 * a + -(-1 * -a)) by lib_int, h1]; lib_int)
    have hb : b = 0 := by rw [hb', ha]; lib_int
    rw [ha, hb]; rfl

theorem sum_scale (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (F : Zp (6 * n + 5 + 1) → Int × Int) (x0 : Zp (6 * n + 5 + 1)) (hx0 : x0 ≠ (ring (6 * n + 5 + 1)).zero) :
    sumK (Cyclo3.cyc3 LibInt.intR) (fun x => F ((ring (6 * n + 5 + 1)).mul x0 x)) (LL n) = sumK (Cyclo3.cyc3 LibInt.intR) F (LL n) :=
  reindex (Cyclo3.cyc3 LibInt.intR) _ (CMInert.complete_allZ (6 * n + 5 + 1) (Cyclo3.cyc3 LibInt.intR)) ((ring (6 * n + 5 + 1)).mul x0) ((ring (6 * n + 5 + 1)).mul (pw (ring (6 * n + 5 + 1)) x0 (6 * n + 5)))
    (fun x => by rw [← (ring (6 * n + 5 + 1)).mul_assoc, inv_exists (6 * n + 5) hpr x0 hx0, (ring (6 * n + 5 + 1)).one_mul])
    (fun y => by rw [← (ring (6 * n + 5 + 1)).mul_assoc, (ring (6 * n + 5 + 1)).mul_comm x0, inv_exists (6 * n + 5) hpr x0 hx0, (ring (6 * n + 5 + 1)).one_mul]) F

/-- **Σ χ̄ = 0.** -/
theorem cb_sum (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : sumK (Cyclo3.cyc3 LibInt.intR) (cb n w) (LL n) = (Cyclo3.cyc3 LibInt.intR).zero := by
  match PowerSum.exists_nonroot (6 * n + 5) hpr (2 * n + 1) (by rw [show 6 * n + 5 = (2 * n + 1 + 1) + (4 * n + 3) by lib_nat]; exact Nat.le_add_right _ _) with
  | ⟨x0, hx0, hn⟩ =>
    have e : sumK (Cyclo3.cyc3 LibInt.intR) (fun x => cb n w ((ring (6 * n + 5 + 1)).mul x0 x)) (LL n) = (Cyclo3.cyc3 LibInt.intR).mul (cb n w x0) (sumK (Cyclo3.cyc3 LibInt.intR) (cb n w) (LL n)) := by
      rw [mul_sumK]; exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun x => cb_mul n hpr w hw1 hw3 x0 x) _
    have hre := sum_scale n hpr (cb n w) x0 hx0
    rw [e] at hre
    match exists_i n hpr w hw1 hw3 x0 hx0 with
    | ⟨i, hi, ei⟩ =>
      have hcb : cb n w x0 = pw (Cyclo3.cyc3 LibInt.intR) om 1 ∨ cb n w x0 = pw (Cyclo3.cyc3 LibInt.intR) om 2 := by
        unfold cb; rw [val_of n hpr w hw1 hw3 x0 hx0 i hi ei]
        match i, hi with
        | 0, _ => exact absurd ei hn
        | 1, _ => exact Or.inr rfl
        | 2, _ => exact Or.inl rfl
        | k + 3, hk => exact absurd hk (fun h => Nat.lt_irrefl 3 (Nat.lt_of_le_of_lt (Nat.le_add_left 3 k) h))
      exact kill _ _ hcb hre

theorem sum_one : ∀ k, sumK (Cyclo3.cyc3 LibInt.intR) (fun _ : Zp (6 * n + 5 + 1) => (Cyclo3.cyc3 LibInt.intR).one) (CMInert.allZ (6 * n + 5 + 1) k) = (Int.ofNat k, 0)
  | 0 => rfl
  | k + 1 => by
    show sumK (Cyclo3.cyc3 LibInt.intR) (fun _ : Zp (6 * n + 5 + 1) => (Cyclo3.cyc3 LibInt.intR).one) (CMInert.allZ (6 * n + 5 + 1) k ++ [mk (6 * n + 5 + 1) k]) = _
    rw [sumK_append, sum_one k]
    show ((Int.ofNat k, (0 : Int)).1 + ((1 : Int) + 0), (Int.ofNat k, (0 : Int)).2 + ((0 : Int) + 0)) = (Int.ofNat (k + 1), 0)
    rfl

theorem delta_sum1 : sumK (Cyclo3.cyc3 LibInt.intR) (fun b => delta (Cyclo3.cyc3 LibInt.intR) b (ring (6 * n + 5 + 1)).zero) (LL n) = (Cyclo3.cyc3 LibInt.intR).one := CMInert.complete_allZ (6 * n + 5 + 1) (Cyclo3.cyc3 LibInt.intR) (ring (6 * n + 5 + 1)).zero

/-- **Σ_b χ(b) χ̄(b) = p − 1.** -/
theorem D_zero (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : sumK (Cyclo3.cyc3 LibInt.intR) (fun b => (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w b) (cb n w b)) (LL n) = (Int.ofNat (6 * n + 5 + 1 + 1) - 1, 0) := by
  have pt : ∀ b, (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w b) (cb n w b) = (Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one ((Cyclo3.cyc3 LibInt.intR).neg (delta (Cyclo3.cyc3 LibInt.intR) b (ring (6 * n + 5 + 1)).zero)) := fun b => by
    by_cases hb : b = (ring (6 * n + 5 + 1)).zero
    · rw [hb, chi3_zero]; unfold delta; rw [ite_pos' _ _ rfl]; show (Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).zero _ = _; rw [zero_mul']; lib_ring
    · rw [chi_cube n hpr w hw1 hw3 b hb]; unfold delta; rw [ite_neg' _ _ hb]; lib_ring
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ pt, sumK_add]
  unfold LL
  rw [sum_one]
  rw [show CMInert.allZ (6 * n + 5 + 1) (6 * n + 5 + 1 + 1) = LL n from rfl]
  have hneg : sumK (Cyclo3.cyc3 LibInt.intR) (fun b => (Cyclo3.cyc3 LibInt.intR).neg (delta (Cyclo3.cyc3 LibInt.intR) b (ring (6 * n + 5 + 1)).zero)) (LL n) = (Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one := by
    have e : sumK (Cyclo3.cyc3 LibInt.intR) (fun b => (Cyclo3.cyc3 LibInt.intR).neg (delta (Cyclo3.cyc3 LibInt.intR) b (ring (6 * n + 5 + 1)).zero)) (LL n) = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one) (sumK (Cyclo3.cyc3 LibInt.intR) (fun b => delta (Cyclo3.cyc3 LibInt.intR) b (ring (6 * n + 5 + 1)).zero) (LL n)) := by
      rw [mul_sumK]; exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun b => by lib_ring) _
    rw [e, delta_sum1]; lib_ring
  rw [hneg]; rfl

theorem shift1 (G : Zp (6 * n + 5 + 1) → Int × Int) : sumK (Cyclo3.cyc3 LibInt.intR) (fun y => G ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one y)) (LL n) = sumK (Cyclo3.cyc3 LibInt.intR) G (LL n) :=
  reindex (Cyclo3.cyc3 LibInt.intR) _ (CMInert.complete_allZ (6 * n + 5 + 1) (Cyclo3.cyc3 LibInt.intR)) (fun y => (ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one y) (fun y => (ring (6 * n + 5 + 1)).add y ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one))
    (fun x => by lib_ring) (fun y => by lib_ring) G

/-- **c ≠ 0: Σ_b χ(c + b) χ̄(b) = −1.** -/
theorem D_ne (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (c : Zp (6 * n + 5 + 1)) (hc : c ≠ (ring (6 * n + 5 + 1)).zero) :
    sumK (Cyclo3.cyc3 LibInt.intR) (fun b => (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).add c b)) (cb n w b)) (LL n) = (Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one := by
  have pt : ∀ b, (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).add c b)) (cb n w b)
      = (Cyclo3.cyc3 LibInt.intR).add (chi3 n w ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).mul c (iv n b)))) ((Cyclo3.cyc3 LibInt.intR).neg (delta (Cyclo3.cyc3 LibInt.intR) b (ring (6 * n + 5 + 1)).zero)) := fun b => by
    by_cases hb : b = (ring (6 * n + 5 + 1)).zero
    · rw [hb, iv_zero, mul_zero, show (ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one (ring (6 * n + 5 + 1)).zero = (ring (6 * n + 5 + 1)).one by lib_ring, chi_one n hpr w hw1 hw3]
      unfold cb delta; rw [chi3_zero, ite_pos' _ _ rfl]
      show (Cyclo3.cyc3 LibInt.intR).mul _ ((Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).zero (Cyclo3.cyc3 LibInt.intR).zero) = _
      rw [zero_mul', mul_zero]; lib_ring
    · rw [← chi_iv n hpr w hw1 hw3 b hb, ← chi3_mul n hpr w hw1 hw3]
      unfold delta; rw [ite_neg' _ _ hb]
      have e : (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).add c b) (iv n b) = (ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).mul c (iv n b)) := by
        rw [show (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).add c b) (iv n b) = (ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).mul (iv n b) b) ((ring (6 * n + 5 + 1)).mul c (iv n b)) by lib_ring, iv_mul n hpr b hb]
      rw [e]; lib_ring
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ pt, sumK_add]
  have hc' : (ring (6 * n + 5 + 1)).mul (iv n c) c = (ring (6 * n + 5 + 1)).one := iv_mul n hpr c hc
  have h1 : sumK (Cyclo3.cyc3 LibInt.intR) (fun b => chi3 n w ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).mul c (iv n b)))) (LL n) = (Cyclo3.cyc3 LibInt.intR).zero := by
    have hre := reindex (Cyclo3.cyc3 LibInt.intR) _ (CMInert.complete_allZ (6 * n + 5 + 1) (Cyclo3.cyc3 LibInt.intR)) (fun b => (ring (6 * n + 5 + 1)).mul c (iv n b)) (fun y => iv n ((ring (6 * n + 5 + 1)).mul (iv n c) y))
      (fun x => by rw [← (ring (6 * n + 5 + 1)).mul_assoc, hc', (ring (6 * n + 5 + 1)).one_mul, iv_iv n hpr])
      (fun y => by rw [iv_iv n hpr, ← (ring (6 * n + 5 + 1)).mul_assoc, (ring (6 * n + 5 + 1)).mul_comm c, hc', (ring (6 * n + 5 + 1)).one_mul]) (fun y => chi3 n w ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one y))
    exact hre.trans ((shift1 n _).trans (chi3_sum n hpr w hw1 hw3))
  have h2 : sumK (Cyclo3.cyc3 LibInt.intR) (fun b => (Cyclo3.cyc3 LibInt.intR).neg (delta (Cyclo3.cyc3 LibInt.intR) b (ring (6 * n + 5 + 1)).zero)) (LL n) = (Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one := by
    have e : sumK (Cyclo3.cyc3 LibInt.intR) (fun b => (Cyclo3.cyc3 LibInt.intR).neg (delta (Cyclo3.cyc3 LibInt.intR) b (ring (6 * n + 5 + 1)).zero)) (LL n) = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one) (sumK (Cyclo3.cyc3 LibInt.intR) (fun b => delta (Cyclo3.cyc3 LibInt.intR) b (ring (6 * n + 5 + 1)).zero) (LL n)) := by
      rw [mul_sumK]; exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun b => by lib_ring) _
    rw [e, delta_sum1]; lib_ring
  rw [h1, h2]; lib_ring

/-- **Σ_a χ(a) χ(−a) = 0.** -/
theorem S_zero (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (chi3 n w ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).zero ((ring (6 * n + 5 + 1)).neg a)))  -- (+47 chars; full line in the Zenodo source)
  rw [← cb_sum n hpr w hw1 hw3]
  exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun a => by
    rw [show (ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).zero ((ring (6 * n + 5 + 1)).neg a) = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one) a by lib_ring, chi3_mul n hpr w hw1 hw3, chi3_neg1 n hpr w hw1 hw3]
    unfold cb; lib_ring) _

/-- **c ≠ 0: Σ_a χ(a) χ(c − a) = χ̄(c) J.** -/
theorem S_ne (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (c : Zp (6 * n + 5 + 1)) (hc : c ≠ (ring (6 * n + 5 + 1)).zero) :
    sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (chi3 n w ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg a)))) (LL n) = (Cyclo3.cyc3 LibInt.intR).mul (cb n w c) (J n w) := by
  rw [← sum_scale n hpr _ c hc]
  unfold J; rw [mul_sumK]
  exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun t => by
    rw [show (ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul c t)) = (ring (6 * n + 5 + 1)).mul c ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).neg t)) by lib_ring, chi3_mul n hpr w hw1 hw3, chi3_mul n hpr w hw1 hw3]
    unfold cb; lib_ring) _

end JacobiSum

#print axioms JacobiSum.iv_mul
#print axioms JacobiSum.iv_zero
#print axioms JacobiSum.iv_mulmul
#print axioms JacobiSum.iv_iv
#print axioms JacobiSum.chi_one
#print axioms JacobiSum.chi_cube
#print axioms JacobiSum.cb_mul
#print axioms JacobiSum.chi_iv
#print axioms JacobiSum.kill
#print axioms JacobiSum.sum_scale
#print axioms JacobiSum.cb_sum
#print axioms JacobiSum.sum_one
#print axioms JacobiSum.delta_sum1
#print axioms JacobiSum.D_zero
#print axioms JacobiSum.shift1
#print axioms JacobiSum.D_ne
#print axioms JacobiSum.S_zero
#print axioms JacobiSum.S_ne

lang/Jacobsthal.lean

lean_all/lang/Jacobsthal.lean

/-
  lang/Jacobsthal — T43, cycle 43: Jacobsthal sums φ(a) = Σ_x χ(x (x² + a)) and their scaling law.

  p = 2m + 3, χ from lang/QChar.
    chi_cube      χ(t³) = χ(t)
    phi_scale     t ≠ 0 ⇒ φ(a t²) = χ(t) φ(a)             (x ↦ t x)
    phi_zero      φ(0) = Σ_x χ(x³) = 0
  For y² = x³ − x: a_p = −φ(−1) (the Frobenius trace is minus the character sum). Next: Σ_a φ(a)² = 2p(p − 1) for
  p ≡ 1 (mod 4) (ConicSum), whence φ(1)² + φ(n)² = 4p and a_p = ±2u with p = u² + v² — the CM modularity of
  y² = x³ − x in integers.

  Lean 4 core only, proofs by hand (lib_ring, lib_int from lib_shared), no axioms.
-/
import lang.ConicSum

namespace Jacobsthal

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum

variable (m : Nat)

def phi (a : Zp (2 * m + 1 + 1)) : Int :=
  sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) a))) (L m)

theorem chi_cube (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (t : Zp (2 * m + 1 + 1)) :
    chi m ((ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)).mul t t)) = chi m t := by
  by_cases ht : t = (ring (2 * m + 1 + 1)).zero
  · rw [ht, DetChain.zero_mul']
  · rw [chi_mul m hpr, chi_sq m hpr t ht]; lib_int

theorem reindex_mul (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (t : Zp (2 * m + 1 + 1))
    (ht : t ≠ (ring (2 * m + 1 + 1)).zero) (F : Zp (2 * m + 1 + 1) → Int) :
    sumK LibInt.intR (fun x => F ((ring (2 * m + 1 + 1)).mul t x)) (L m) = sumK LibInt.intR F (L m) :=
  reindex LibInt.intR _ (CMInert.complete_allZ _ LibInt.intR) ((ring (2 * m + 1 + 1)).mul t) ((ring (2 * m + 1 + 1)).mul (iv m t))
    (fun x => by rw [← (ring (2 * m + 1 + 1)).mul_assoc, iv_mul_self m hpr t ht, (ring (2 * m + 1 + 1)).one_mul])
    (fun y => by rw [← (ring (2 * m + 1 + 1)).mul_assoc, (ring (2 * m + 1 + 1)).mul_comm t, iv_mul_self m hpr t ht, (ring (2 * m + 1 + 1)).one_mul]) F

/-- **φ(a t²) = χ(t) φ(a).** -/
theorem phi_scale (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a t : Zp (2 * m + 1 + 1))
    (ht : t ≠ (ring (2 * m + 1 + 1)).zero) : phi m ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul t t)) = chi m t * phi m a := by
  have h := reindex_mul m hpr t ht (fun x => chi m ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul t t)))))
  show sumK LibInt.intR _ (L m) = _
  rw [← h]
  have pt : ∀ x, chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t x) ((ring (2 * m + 1 + 1)).mul t x)) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul t t))))
      = LibInt.intR.mul (chi m t) (chi m ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) a))) := fun x => by
    have e : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t x) ((ring (2 * m + 1 + 1)).mul t x)) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul t t)))
        = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)).mul t t)) ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) a)) := by lib_ring
    rw [e, chi_mul m hpr, chi_cube m hpr]
    rfl
  rw [sumK_ext LibInt.intR _ _ pt, ← mul_sumK]
  rfl

/-- **φ(0) = 0.** -/
theorem phi_zero (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : phi m (ring (2 * m + 1 + 1)).zero = 0 := by
  show sumK LibInt.intR _ (L m) = 0
  have pt : ∀ x, chi m ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) (ring (2 * m + 1 + 1)).zero)) = chi m x := fun x => by
    rw [DetChain.add_zero', ← (ring (2 * m + 1 + 1)).mul_assoc]
    have : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) x = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x) := by lib_ring
    rw [this, chi_cube m hpr]
  rw [sumK_ext LibInt.intR _ _ pt]
  exact chi_sum m hpr

end Jacobsthal

#print axioms Jacobsthal.chi_cube
#print axioms Jacobsthal.reindex_mul
#print axioms Jacobsthal.phi_scale
#print axioms Jacobsthal.phi_zero

lang/JprHom.lean

lean_all/lang/JprHom.lean

/-
  lang/JprHom — T49 (general ℓ): **Jacobi sums and their products commute with ring maps**.
  For a ring map φ : E → F and ψ : 𝔽_p → E,
    sumK_congr   pointwise equal summands give equal sums (no function extensionality)
    j2_map       φ J(χ, μ) = J(φ∘χ, φ∘μ)
    jpr_map      φ (∏_(i ≤ k) J(ψ^i, ψ)) = ∏_(i ≤ k) J((φ∘ψ)^i, φ∘ψ)
    a_map        φ (∏ J(ψ^i, ψ)·ψ(ι(−1))) = ∏ J((φ∘ψ)^i, φ∘ψ)·(φ∘ψ)(ι(−1))
  so the element A of the residue criterion (NthRecipPow, NthEis) and of the primary normalisation (PrimA) are images of
  one element of any ring through which the character factors.
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.GaussPowN

namespace JprHom

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob CharSumGen

variable {S F : Type} (E : CRing S) (K : CRing F) (φ : S → F) (j : Nat)

theorem sumK_congr {A : Type} (f g : A → F) (h : ∀ a, f a = g a) : ∀ l : List A, sumK K f l = sumK K g l
  | [] => rfl
  | a :: l => by
    show K.add (f a) (sumK K f l) = K.add (g a) (sumK K g l)
    rw [h a, sumK_congr f g h l]

theorem map_sum {A : Type} (hadd : ∀ a b, φ (E.add a b) = K.add (φ a) (φ b)) (hzero : φ E.zero = K.zero) (f : A → S) :
    ∀ l : List A, φ (sumK E f l) = sumK K (fun a => φ (f a)) l
  | [] => hzero
  | a :: l => by
    show φ (E.add (f a) (sumK E f l)) = K.add (φ (f a)) (sumK K (fun a => φ (f a)) l)
    rw [hadd, map_sum hadd hzero f l]

theorem map_pw (hmul : ∀ a b, φ (E.mul a b) = K.mul (φ a) (φ b)) (hone : φ E.one = K.one) (a : S) :
    ∀ k, φ (pw E a k) = pw K (φ a) k
  | 0 => hone
  | k + 1 => by
    show φ (E.mul (pw E a k) a) = K.mul (pw K (φ a) k) (φ a)
    rw [hmul, map_pw hmul hone a k]

/-- **φ J(χ, μ) = J(φ∘χ, φ∘μ)**. -/
theorem j2_map (hadd : ∀ a b, φ (E.add a b) = K.add (φ a) (φ b)) (hzero : φ E.zero = K.zero)
    (hmul : ∀ a b, φ (E.mul a b) = K.mul (φ a) (φ b)) (χ μ : Zp (j + 1 + 1) → S) :
    φ (J2 E j χ μ) = J2 K j (fun x => φ (χ x)) (fun x => φ (μ x)) := by
  show φ (sumK E (fun t => E.mul (χ t) (μ ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg t)))) (LL j)) =
    sumK K (fun t => K.mul (φ (χ t)) (φ (μ ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg t))))) (LL j)
  rw [map_sum E K φ hadd hzero]
  exact sumK_congr K _ _ (fun t => hmul _ _) (LL j)

/-- **φ (∏_(i ≤ k) J(ψ^i, ψ)) = ∏_(i ≤ k) J((φ∘ψ)^i, φ∘ψ)**. -/
theorem jpr_map (hadd : ∀ a b, φ (E.add a b) = K.add (φ a) (φ b)) (hzero : φ E.zero = K.zero)
    (hmul : ∀ a b, φ (E.mul a b) = K.mul (φ a) (φ b)) (hone : φ E.one = K.one) (ψ : Zp (j + 1 + 1) → S) :
    ∀ k, φ (jpr E j ψ k) = jpr K j (fun x => φ (ψ x)) k
  | 0 => hone
  | k + 1 => by
    show φ (E.mul (jpr E j ψ k) (J2 E j (psp E j ψ (k + 1)) ψ)) =
      K.mul (jpr K j (fun x => φ (ψ x)) k) (J2 K j (psp K j (fun x => φ (ψ x)) (k + 1)) (fun x => φ (ψ x)))
    rw [hmul, jpr_map hadd hzero hmul hone ψ k, j2_map E K φ j hadd hzero hmul]
    show K.mul (jpr K j (fun x => φ (ψ x)) k)
        (sumK K (fun t => K.mul (φ (pw E (ψ t) (k + 1))) (φ (ψ ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg t))))) (LL j)) =
      K.mul (jpr K j (fun x => φ (ψ x)) k)
        (sumK K (fun t => K.mul (pw K (φ (ψ t)) (k + 1)) (φ (ψ ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg t))))) (LL j))
    exact congrArg (K.mul (jpr K j (fun x => φ (ψ x)) k)) (sumK_congr K _ _ (fun t =>
      congrArg (fun z => K.mul z (φ (ψ ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg t)))))
        (map_pw E K φ hmul hone (ψ t) (k + 1))) (LL j))

/-- **the residue-criterion element maps to the residue-criterion element**. -/
theorem a_map (hadd : ∀ a b, φ (E.add a b) = K.add (φ a) (φ b)) (hzero : φ E.zero = K.zero)
    (hmul : ∀ a b, φ (E.mul a b) = K.mul (φ a) (φ b)) (hone : φ E.one = K.one) (ψ : Zp (j + 1 + 1) → S) (k : Nat) :
    φ (E.mul (jpr E j ψ k) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) =
      K.mul (jpr K j (fun x => φ (ψ x)) k) (φ (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) := by
  rw [hmul, jpr_map E K φ j hadd hzero hmul hone ψ k]

end JprHom

#print axioms JprHom.sumK_congr
#print axioms JprHom.map_sum
#print axioms JprHom.map_pw
#print axioms JprHom.j2_map
#print axioms JprHom.jpr_map
#print axioms JprHom.a_map

lang/JprZero.lean

lean_all/lang/JprZero.lean

/-
  lang/JprZero — T49 (general): **a Jacobi product with a vanishing factor vanishes**:
    jpr_zero    J(ψ^(i+1), ψ) = 0 and i + 1 ≤ k ⇒ ∏_(i' ≤ k) J(ψ^(i'), ψ) = 0
  (applied after a ring map, e.g. to show that the primary element A lies in a prime P_t).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.GaussPowN
import lib.RingTac

namespace JprZero

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius CharSumGen

variable {S : Type} (E : CRing S) (j : Nat) (ψ : Zp (j + 1 + 1) → S)

theorem jpr_zero (i : Nat) (hz : J2 E j (psp E j ψ (i + 1)) ψ = E.zero) : ∀ k, i + 1 ≤ k → jpr E j ψ k = E.zero
  | 0, h => absurd h (fun h' => nomatch h')
  | k + 1, h => by
    show E.mul (jpr E j ψ k) (J2 E j (psp E j ψ (k + 1)) ψ) = E.zero
    by_cases e : i = k
    · rw [← e, hz]; lib_ring
    · rw [jpr_zero i hz k (Nat.lt_of_le_of_ne (Nat.le_of_succ_le_succ h) e)]; lib_ring

end JprZero

#print axioms JprZero.jpr_zero

lang/KoszulAll.lean

lean_all/lang/KoszulAll.lean

/-
  lang/KoszulAll — T52 (tool for T21), cycle 804: **the Koszul complex of a regular sequence is exact in every positive degree,
  every length n**.
  The Koszul complex K(x₀, …, x_(n−1)) is built recursively as a mapping cone: K(x₀…x_n) = cone(x_n : K(x₀…x_(n−1)) → K(x₀…x_(n−1))),
    KC 0 n = R,   KC (k+1) 0 = 0,   KC (k+1) (n+1) = KC (k+1) n × KC k n,
    d (u, v) = (d u + (−1)^(k+1) x_n v, d v)      (degree 0 of the new factor: d (u, v) = d u + x_n v).
    KZ, KA, KS        zero, addition, scalar multiplication (componentwise, by recursion)
    ks_add, ka4, ks_comm, cancel     the module laws used
    d_add, d_smul     d is linear
    surj, img         d : KC 1 n → R has image exactly the ideal (x₀, …, x_(n−1))
    exact             Reg x n (KoszulN) ⇒ every cycle of degree k + 1 ≥ 1 is a boundary: H_(k+1)(x; R) = 0 for all k, n
  Proof of exact: induction on n; for z = (u, v) with d z = 0, v is a cycle (or, in degree 1, x_n v ∈ (x₀…x_(n−1)) so v is in the
  ideal by regularity), so v = d w'; then u − (±x_n) w' is a cycle of K(x₀…x_(n−1)), = d t, and z = d (t, w').
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.KoszulN

namespace KoszulAll

open DetChain FiniteRecChain KoszulN

/-- degree k + 1 over n generators: KCn prev 0 = 0, KCn prev (n+1) = KCn prev n × prev n. -/
@[reducible] def KCn (prev : Nat → Type) : Nat → Type
  | 0 => Unit
  | n + 1 => KCn prev n × prev n

@[reducible] def KC (R : Type) : Nat → Nat → Type
  | 0 => fun _ => R
  | k + 1 => KCn (KC R k)

variable {R : Type} (K : CRing R) (x : Nat → R)

noncomputable def KZ : (k : Nat) → (n : Nat) → KC R k n
  | 0 => fun _ => K.zero
  | k + 1 => fun n => Nat.rec (motive := fun n => KC R (k + 1) n) () (fun n acc => (acc, KZ k n)) n

noncomputable def KA : (k : Nat) → (n : Nat) → KC R k n → KC R k n → KC R k n
  | 0 => fun _ a b => K.add a b
  | k + 1 => fun n => Nat.rec (motive := fun n => KC R (k + 1) n → KC R (k + 1) n → KC R (k + 1) n)
      (fun _ _ => ()) (fun n acc a b => (acc a.1 b.1, KA k n a.2 b.2)) n

noncomputable def KS (c : R) : (k : Nat) → (n : Nat) → KC R k n → KC R k n
  | 0 => fun _ a => K.mul c a
  | k + 1 => fun n => Nat.rec (motive := fun n => KC R (k + 1) n → KC R (k + 1) n)
      (fun _ => ()) (fun n acc a => (acc a.1, KS c k n a.2)) n

def sg : Nat → R
  | 0 => K.one
  | k + 1 => K.neg (sg k)

noncomputable def d : (k : Nat) → (n : Nat) → KC R (k + 1) n → KC R k n
  | 0 => fun n => Nat.rec (motive := fun n => KC R 1 n → KC R 0 n)
      (fun _ => K.zero) (fun n acc z => K.add (acc z.1) (K.mul (x n) z.2)) n
  | k + 1 => fun n => Nat.rec (motive := fun n => KC R (k + 2) n → KC R (k + 1) n)
      (fun _ => ()) (fun n acc z => (KA K (k + 1) n (acc z.1) (KS K (K.mul (sg K (k + 1)) (x n)) (k + 1) n z.2), d k n z.2)) n

theorem pext {α β : Type} : ∀ {p q : α × β}, p.1 = q.1 → p.2 = q.2 → p = q
  | (_, _), (_, _), h1, h2 => by cases h1; cases h2; rfl

theorem ks_add (c : R) : ∀ k n (a b : KC R k n), KS K c k n (KA K k n a b) = KA K k n (KS K c k n a) (KS K c k n b)
  | 0, _, a, b => by show K.mul c (K.add a b) = K.add (K.mul c a) (K.mul c b); lib_ring
  | _ + 1, 0, _, _ => rfl
  | k + 1, n + 1, a, b => pext (ks_add c (k + 1) n a.1 b.1) (ks_add c k n a.2 b.2)

theorem ka4 : ∀ k n (p q r t : KC R k n), KA K k n (KA K k n p q) (KA K k n r t) = KA K k n (KA K k n p r) (KA K k n q t)
  | 0, _, p, q, r, t => by show K.add (K.add p q) (K.add r t) = K.add (K.add p r) (K.add q t); lib_ring
  | _ + 1, 0, _, _, _, _ => rfl
  | k + 1, n + 1, p, q, r, t => pext (ka4 (k + 1) n p.1 q.1 r.1 t.1) (ka4 k n p.2 q.2 r.2 t.2)

theorem ks_comm (c e : R) : ∀ k n (a : KC R k n), KS K c k n (KS K e k n a) = KS K e k n (KS K c k n a)
  | 0, _, a => by show K.mul c (K.mul e a) = K.mul e (K.mul c a); lib_ring
  | _ + 1, 0, _ => rfl
  | k + 1, n + 1, a => pext (ks_comm c e (k + 1) n a.1) (ks_comm c e k n a.2)

theorem cancel (c : R) : ∀ k n (u w : KC R k n), KA K k n (KA K k n u (KS K c k n w)) (KS K (K.neg c) k n w) = u
  | 0, _, u, w => by show K.add (K.add u (K.mul c w)) (K.mul (K.neg c) w) = u; lib_ring
  | _ + 1, 0, _, _ => rfl
  | k + 1, n + 1, u, w => pext (cancel c (k + 1) n u.1 w.1) (cancel c k n u.2 w.2)

theorem d_add : ∀ k n (a b : KC R (k + 1) n), d K x k n (KA K (k + 1) n a b) = KA K k n (d K x k n a) (d K x k n b)
  | 0, 0, _, _ => by show K.zero = K.add K.zero K.zero; lib_ring
  | _ + 1, 0, _, _ => rfl
  | 0, n + 1, a, b => by
    show K.add (d K x 0 n (KA K 1 n a.1 b.1)) (K.mul (x n) (K.add a.2 b.2))
      = K.add (K.add (d K x 0 n a.1) (K.mul (x n) a.2)) (K.add (d K x 0 n b.1) (K.mul (x n) b.2))
    rw [d_add 0 n a.1 b.1]
    show K.add (K.add (d K x 0 n a.1) (d K x 0 n b.1)) (K.mul (x n) (K.add a.2 b.2)) = _
    lib_ring
  | k + 1, n + 1, a, b => pext (by
      show KA K (k + 1) n (d K x (k + 1) n (KA K (k + 2) n a.1 b.1)) (KS K (K.mul (sg K (k + 1)) (x n)) (k + 1) n (KA K (k + 1) n a.2 b.2))
        = KA K (k + 1) n (KA K (k + 1) n (d K x (k + 1) n a.1) (KS K (K.mul (sg K (k + 1)) (x n)) (k + 1) n a.2))
            (KA K (k + 1) n (d K x (k + 1) n b.1) (KS K (K.mul (sg K (k + 1)) (x n)) (k + 1) n b.2))
      rw [d_add (k + 1) n a.1 b.1, ks_add, ka4]) (d_add k n a.2 b.2)

theorem d_smul (c : R) : ∀ k n (a : KC R (k + 1) n), d K x k n (KS K c (k + 1) n a) = KS K c k n (d K x k n a)
  | 0, 0, _ => by show K.zero = K.mul c K.zero; lib_ring
  | _ + 1, 0, _ => rfl
  | 0, n + 1, a => by
    show K.add (d K x 0 n (KS K c 1 n a.1)) (K.mul (x n) (K.mul c a.2)) = K.mul c (K.add (d K x 0 n a.1) (K.mul (x n) a.2))
    rw [d_smul c 0 n a.1]
    show K.add (K.mul c (d K x 0 n a.1)) (K.mul (x n) (K.mul c a.2)) = _
    lib_ring
  | k + 1, n + 1, a => pext (by
      show KA K (k + 1) n (d K x (k + 1) n (KS K c (k + 2) n a.1)) (KS K (K.mul (sg K (k + 1)) (x n)) (k + 1) n (KS K c (k + 1) n a.2))
        = KS K c (k + 1) n (KA K (k + 1) n (d K x (k + 1) n a.1) (KS K (K.mul (sg K (k + 1)) (x n)) (k + 1) n a.2))
      rw [d_smul c (k + 1) n a.1, ks_add, ks_comm]) (d_smul c k n a.2)

/-- every element of the ideal (x₀, …, x_(n−1)) is a d-image of degree 1. -/
theorem surj (c : Nat → R) : ∀ n, ∃ w : KC R 1 n, d K x 0 n w = sm K (fun j => K.mul (c j) (x j)) n
  | 0 => ⟨(), rfl⟩
  | n + 1 => by
    match surj c n with
    | ⟨w, hw⟩ =>
      refine ⟨(w, c n), ?_⟩
      show K.add (d K x 0 n w) (K.mul (x n) (c n)) = K.add (sm K (fun j => K.mul (c j) (x j)) n) (K.mul (c n) (x n))
      rw [hw]
      lib_ring

/-- every d-image of degree 1 is in the ideal (x₀, …, x_(n−1)). -/
theorem img : ∀ n (u : KC R 1 n), InI K x n (d K x 0 n u)
  | 0, _ => ⟨fun _ => K.zero, rfl⟩
  | n + 1, u => by
    match img n u.1 with
    | ⟨c, hc⟩ =>
      refine ⟨fun j => if j = n then u.2 else c j, ?_⟩
      show K.add (d K x 0 n u.1) (K.mul (x n) u.2)
        = K.add (sm K (fun j => K.mul (if j = n then u.2 else c j) (x j)) n) (K.mul (if n = n then u.2 else c n) (x n))
      have e : sm K (fun j => K.mul (if j = n then u.2 else c j) (x j)) n = sm K (fun j => K.mul (c j) (x j)) n :=
        sm_ext K _ _ n (ab_mono _ _ (fun j hj => by
          show K.mul (if j = n then u.2 else c j) (x j) = K.mul (c j) (x j)
          rw [ite_neg' _ _ hj]) n (ab_ne n))
      rw [e, ite_pos' _ _ (rfl : n = n), ← hc]
      lib_ring

/-- **Koszul exactness**: for a regular sequence, H_(k+1)(x₀, …, x_(n−1); R) = 0 for every k and n. -/
theorem exact : ∀ n, Reg K x n → ∀ k (z : KC R (k + 1) n), d K x k n z = KZ K k n → ∃ w : KC R (k + 2) n, d K x (k + 1) n w = z
  | 0, _, _, _, _ => ⟨(), rfl⟩
  | n + 1, ⟨hreg, hx⟩, 0, z, hz => by
    have hz' : K.add (d K x 0 n z.1) (K.mul (x n) z.2) = K.zero := hz
    -- x_n v lies in the ideal, hence v does
    have hin : InI K x n (K.mul z.2 (x n)) := by
      match img K x n (KS K (K.neg K.one) 1 n z.1) with
      | ⟨c, hc⟩ =>
        refine ⟨c, ?_⟩
        rw [← hc, d_smul]
        show K.mul z.2 (x n) = K.mul (K.neg K.one) (d K x 0 n z.1)
        calc K.mul z.2 (x n) = K.add (K.mul (K.neg K.one) (d K x 0 n z.1)) (K.add (d K x 0 n z.1) (K.mul (x n) z.2)) := by lib_ring
          _ = _ := by rw [hz']; lib_ring
    match hx z.2 hin with
    | ⟨c, hc⟩ =>
      match surj K x c n with
      | ⟨w', hw'⟩ =>
        have hv : d K x 0 n w' = z.2 := hw'.trans hc.symm
        let s : R := K.mul (sg K 1) (x n)
        have hcyc : d K x 0 n (KA K 1 n z.1 (KS K (K.neg s) 1 n w')) = KZ K 0 n := by
          rw [d_add, d_smul, hv]
          show K.add (d K x 0 n z.1) (K.mul (K.neg (K.mul (K.neg K.one) (x n))) z.2) = K.zero
          calc _ = K.add (d K x 0 n z.1) (K.mul (x n) z.2) := by lib_ring
            _ = K.zero := hz'
        match exact n hreg 0 _ hcyc with
        | ⟨t, ht⟩ =>
          refine ⟨(t, w'), pext ?_ hv⟩
          show KA K 1 n (d K x 1 n t) (KS K s 1 n w') = z.1
          rw [ht]
          have e := cancel K (K.neg s) 1 n z.1 w'
          have e2 : K.neg (K.neg s) = s := neg_neg' K s
          rw [e2] at e
          exact e
  | n + 1, ⟨hreg, hx⟩, k + 1, z, hz => by
    have hz1 : KA K (k + 1) n (d K x (k + 1) n z.1) (KS K (K.mul (sg K (k + 1)) (x n)) (k + 1) n z.2) = KZ K (k + 1) n :=
      congrArg Prod.fst hz
    have hz2 : d K x k n z.2 = KZ K k n := congrArg Prod.snd hz
    match exact n hreg k z.2 hz2 with
    | ⟨w', hv⟩ =>
      let s : R := K.mul (sg K (k + 2)) (x n)
      have hcyc : d K x (k + 1) n (KA K (k + 2) n z.1 (KS K (K.neg s) (k + 2) n w')) = KZ K (k + 1) n := by
        rw [d_add, d_smul, hv]
        have es : K.neg s = K.mul (sg K (k + 1)) (x n) := by
          show K.neg (K.mul (K.neg (sg K (k + 1))) (x n)) = K.mul (sg K (k + 1)) (x n)
          lib_ring
        rw [es]
        exact hz1
      match exact n hreg (k + 1) _ hcyc with
      | ⟨t, ht⟩ =>
        refine ⟨(t, w'), pext ?_ hv⟩
        show KA K (k + 2) n (d K x (k + 2) n t) (KS K s (k + 2) n w') = z.1
        rw [ht]
        have e := cancel K (K.neg s) (k + 2) n z.1 w'
        have e2 : K.neg (K.neg s) = s := neg_neg' K s
        rw [e2] at e
        exact e

end KoszulAll

#print axioms KoszulAll.pext
#print axioms KoszulAll.ks_add
#print axioms KoszulAll.ka4
#print axioms KoszulAll.ks_comm
#print axioms KoszulAll.cancel
#print axioms KoszulAll.d_add
#print axioms KoszulAll.d_smul
#print axioms KoszulAll.surj
#print axioms KoszulAll.img
#print axioms KoszulAll.exact

lang/KoszulN.lean

lean_all/lang/KoszulN.lean

/-
  lang/KoszulN — T52 (tool for T21), cycle 803: **the first Koszul homology of a regular sequence vanishes, every length n**.
  For x₀, …, x_(n−1) in a commutative ring with x_k regular modulo (x₀, …, x_(k−1)) for every k < n (Reg x n):
      Σ_(i<n) a_i x_i = 0   ⇒   a_i = Σ_(j<n) b_ij x_j   with b alternating (b_ii = 0, b_ij = −b_ji),
  i.e. every 1-cycle of the Koszul complex is a boundary d₂(Σ b_ij e_i ∧ e_j)  (H₁(x; R) = 0).
  This is the length-n form of DetChain.koszul1_exact / koszul2_H1.
    sm, AB, InI, Reg        finite sums, "for all j < n", ideal membership, regular sequence (recursive, no order lemmas)
    sm_ext, sm_add, sm_mul  sum lemmas;  ab_lt, lt_ne  j < n ⇒ j ≠ n
    koszulH1                the theorem (induction on n: a_n ∈ (x₀…x_(n−1)) by regularity, shift a, extend b by ±c)
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import DetChain
import FiniteRecChain
import lib.RingTac
import lib.Semi

namespace KoszulN

open DetChain FiniteRecChain

variable {R : Type} (K : CRing R)

def sm (f : Nat → R) : Nat → R
  | 0 => K.zero
  | n + 1 => K.add (sm f n) (f n)

/-- P j for every j < n. -/
def AB (P : Nat → Prop) : Nat → Prop
  | 0 => True
  | n + 1 => AB P n ∧ P n

def InI (x : Nat → R) (n : Nat) (r : R) : Prop := ∃ c : Nat → R, r = sm K (fun j => K.mul (c j) (x j)) n

/-- x₀, …, x_(n−1) is a regular sequence. -/
def Reg (x : Nat → R) : Nat → Prop
  | 0 => True
  | n + 1 => Reg x n ∧ ∀ r, InI K x n (K.mul r (x n)) → InI K x n r

theorem sm_ext (f g : Nat → R) : ∀ n, AB (fun j => f j = g j) n → sm K f n = sm K g n
  | 0, _ => rfl
  | n + 1, ⟨h, e⟩ => by
    show K.add (sm K f n) (f n) = K.add (sm K g n) (g n)
    rw [sm_ext f g n h, e]

theorem sm_all (f g : Nat → R) (h : ∀ j, f j = g j) : ∀ n, sm K f n = sm K g n
  | 0 => rfl
  | n + 1 => by
    show K.add (sm K f n) (f n) = K.add (sm K g n) (g n)
    rw [sm_all f g h n, h n]

theorem sm_add (f g : Nat → R) : ∀ n, sm K (fun j => K.add (f j) (g j)) n = K.add (sm K f n) (sm K g n)
  | 0 => by show K.zero = K.add K.zero K.zero; lib_ring
  | n + 1 => by
    show K.add (sm K (fun j => K.add (f j) (g j)) n) (K.add (f n) (g n)) = K.add (K.add (sm K f n) (f n)) (K.add (sm K g n) (g n))
    rw [sm_add f g n]
    lib_ring

theorem sm_mul (c : R) (f : Nat → R) : ∀ n, sm K (fun j => K.mul c (f j)) n = K.mul c (sm K f n)
  | 0 => by show K.zero = K.mul c K.zero; lib_ring
  | n + 1 => by
    show K.add (sm K (fun j => K.mul c (f j)) n) (K.mul c (f n)) = K.mul c (K.add (sm K f n) (f n))
    rw [sm_mul c f n]
    lib_ring

theorem ab_mono (P Q : Nat → Prop) (h : ∀ j, P j → Q j) : ∀ n, AB P n → AB Q n
  | 0, _ => trivial
  | n + 1, ⟨hp, e⟩ => ⟨ab_mono P Q h n hp, h n e⟩

theorem ab_two (P Q S : Nat → Prop) (h : ∀ j, P j → Q j → S j) : ∀ n, AB P n → AB Q n → AB S n
  | 0, _, _ => trivial
  | n + 1, ⟨hp, e⟩, ⟨hq, e'⟩ => ⟨ab_two P Q S h n hp hq, h n e e'⟩

theorem ab_lt : ∀ n, AB (fun j => ∃ d, j + d + 1 = n) n
  | 0 => trivial
  | n + 1 => ⟨ab_mono _ _ (fun j ⟨d, hd⟩ => ⟨d + 1, by rw [← hd]; lib_nat⟩) n (ab_lt n), ⟨0, rfl⟩⟩

theorem self_lt : ∀ n d, n + d + 1 = n → False
  | 0, d, h => Nat.noConfusion h
  | n + 1, d, h => self_lt n d (Nat.succ.inj (show n + d + 1 + 1 = n + 1 by rw [← h]; lib_nat))

theorem ab_ne (n : Nat) : AB (fun j => j = n → False) n :=
  ab_mono _ _ (fun j ⟨d, hd⟩ e => self_lt n d (by rw [e] at hd; exact hd)) n (ab_lt n)

/-- the alternating matrix b extended by the column −c and the row c at index n. -/
def ext (b : Nat → Nat → R) (c : Nat → R) (n i j : Nat) : R :=
  if i = n then (if j = n then K.zero else c j) else (if j = n then K.neg (c i) else b i j)

theorem ext_nn (b : Nat → Nat → R) (c : Nat → R) (n : Nat) : ext K b c n n n = K.zero := by
  unfold ext; rw [ite_pos' _ _ (rfl : n = n), ite_pos' _ _ (rfl : n = n)]

theorem ext_nj (b : Nat → Nat → R) (c : Nat → R) (n j : Nat) (hj : j = n → False) : ext K b c n n j = c j := by
  unfold ext; rw [ite_pos' _ _ (rfl : n = n), ite_neg' _ _ hj]

theorem ext_in (b : Nat → Nat → R) (c : Nat → R) (n i : Nat) (hi : i = n → False) : ext K b c n i n = K.neg (c i) := by
  unfold ext; rw [ite_neg' _ _ hi, ite_pos' _ _ (rfl : n = n)]

theorem ext_ij (b : Nat → Nat → R) (c : Nat → R) (n i j : Nat) (hi : i = n → False) (hj : j = n → False) :
    ext K b c n i j = b i j := by
  unfold ext; rw [ite_neg' _ _ hi, ite_neg' _ _ hj]

/-- **H₁ of the Koszul complex of a regular sequence vanishes**, every length n. -/
theorem koszulH1 (x : Nat → R) : ∀ n, Reg K x n → ∀ a : Nat → R, sm K (fun i => K.mul (a i) (x i)) n = K.zero →
    ∃ b : Nat → Nat → R, (∀ i, b i i = K.zero) ∧ (∀ i j, b i j = K.neg (b j i)) ∧
      AB (fun i => a i = sm K (fun j => K.mul (b i j) (x j)) n) n
  | 0, _, _, _ => ⟨fun _ _ => K.zero, fun _ => rfl, fun _ _ => by show K.zero = K.neg K.zero; lib_ring, trivial⟩
  | n + 1, ⟨hreg, hx⟩, a, ha => by
    have ha' : K.add (sm K (fun i => K.mul (a i) (x i)) n) (K.mul (a n) (x n)) = K.zero := ha
    have hneg : sm K (fun j => K.mul (K.neg (a j)) (x j)) n = K.neg (sm K (fun i => K.mul (a i) (x i)) n) := by
      rw [sm_all K (fun j => K.mul (K.neg (a j)) (x j)) (fun j => K.mul (K.neg K.one) (K.mul (a j) (x j)))
        (fun j => by show K.mul (K.neg (a j)) (x j) = K.mul (K.neg K.one) (K.mul (a j) (x j)); lib_ring) n,
        sm_mul K]
      lib_ring
    have hin : InI K x n (K.mul (a n) (x n)) := ⟨fun j => K.neg (a j), by
      rw [hneg]
      calc K.mul (a n) (x n) = K.add (K.add (sm K (fun i => K.mul (a i) (x i)) n) (K.mul (a n) (x n)))
            (K.neg (sm K (fun i => K.mul (a i) (x i)) n)) := by lib_ring
        _ = _ := by rw [ha']; lib_ring⟩
    match hx (a n) hin with
    | ⟨c, hc⟩ =>
      have ha2 : sm K (fun i => K.mul (K.add (a i) (K.mul (c i) (x n))) (x i)) n = K.zero := by
        rw [sm_all K (fun i => K.mul (K.add (a i) (K.mul (c i) (x n))) (x i)) (fun i => K.add (K.mul (a i) (x i)) (K.mul (x n) (K.mul (c i) (x i))))
          (fun i => by show K.mul (K.add (a i) (K.mul (c i) (x n))) (x i) = K.add (K.mul (a i) (x i)) (K.mul (x n) (K.mul (c i) (x i))); lib_ring) n,
          sm_add K, sm_mul K, ← hc]
        calc _ = K.add (sm K (fun i => K.mul (a i) (x i)) n) (K.mul (a n) (x n)) := by lib_ring
          _ = K.zero := ha'
      match koszulH1 x n hreg (fun i => K.add (a i) (K.mul (c i) (x n))) ha2 with
      | ⟨b, hd, hanti, hrow⟩ =>
        refine ⟨ext K b c n, fun i => ?_, fun i j => ?_, ?_⟩
        · by_cases hi : i = n
          · rw [hi]; exact ext_nn K b c n
          · rw [ext_ij K b c n i i hi hi, hd i]
        · by_cases hi : i = n
          · by_cases hj : j = n
            · rw [hi, hj, ext_nn K b c n]; lib_ring
            · rw [hi, ext_nj K b c n j hj, ext_in K b c n j hj, neg_neg' K]
          · by_cases hj : j = n
            · rw [hj, ext_in K b c n i hi, ext_nj K b c n i hi]
            · rw [ext_ij K b c n i j hi hj, ext_ij K b c n j i hj hi]; exact hanti i j
        · refine ⟨?_, ?_⟩
          · refine ab_two _ _ _ (fun i hr hi => ?_) n hrow (ab_ne n)
            show a i = K.add (sm K (fun j => K.mul (ext K b c n i j) (x j)) n) (K.mul (ext K b c n i n) (x n))
            have e : sm K (fun j => K.mul (ext K b c n i j) (x j)) n = sm K (fun j => K.mul (b i j) (x j)) n :=
              sm_ext K _ _ n (ab_mono _ _ (fun j hj => by
                show K.mul (ext K b c n i j) (x j) = K.mul (b i j) (x j)
                rw [ext_ij K b c n i j hi hj]) n (ab_ne n))
            rw [e, ext_in K b c n i hi, ← hr]
            lib_ring
          · show a n = K.add (sm K (fun j => K.mul (ext K b c n n j) (x j)) n) (K.mul (ext K b c n n n) (x n))
            have e : sm K (fun j => K.mul (ext K b c n n j) (x j)) n = sm K (fun j => K.mul (c j) (x j)) n :=
              sm_ext K _ _ n (ab_mono _ _ (fun j hj => by
                show K.mul (ext K b c n n j) (x j) = K.mul (c j) (x j)
                rw [ext_nj K b c n j hj]) n (ab_ne n))
            rw [e, ext_nn K b c n, ← hc]
            lib_ring

end KoszulN

#print axioms KoszulN.sm_ext
#print axioms KoszulN.sm_all
#print axioms KoszulN.sm_add
#print axioms KoszulN.sm_mul
#print axioms KoszulN.ab_mono
#print axioms KoszulN.ab_two
#print axioms KoszulN.ab_lt
#print axioms KoszulN.self_lt
#print axioms KoszulN.ab_ne
#print axioms KoszulN.ext_nn
#print axioms KoszulN.ext_nj
#print axioms KoszulN.ext_in
#print axioms KoszulN.ext_ij
#print axioms KoszulN.koszulH1

lang/KummerFF.lean

lean_all/lang/KummerFF.lean

/-
  lang/KummerFF — T49 (Kummer step): **F[x]/(x^n − c) as a finite field of the interface, of order |F|^n**, for F a
  finite field of the interface of characteristic p, |F| = p^e = nt + 1 (n = j' + 2 prime), and c with ω = c^t ≠ 1,
  ω^n = 1 (lang/KummerNoZD, lang/KummerInv; MkFF as in lang/GenCubicInst).
    elsV, compV, cardV    the |F|^n tuples, listed once each
    odd_pow               (2a + 1)^n = 2X + 1
    kummer_ff             ∃ G, 2h_G + 1 = |F|^n and p·1 = 0 in G
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.KummerInv
import lang.MkFF
import lang.GenCubicExt

namespace KummerFF

open FTAChain ZMod YMChain FiniteRecChain DetChain GroupRing Frobenius TwistRing TwistFrob TwistPhi TwistIter TwistCoef
  TwistS TwistBeta TwistFix KummerNoZD KummerInv

variable {R : Type} [DecidableEq R] (F : FinField.FF R)

def elsV : ∀ n, List (V R n)
  | 0 => [()]
  | n + 1 => CubicInst.prodL (elsV n) F.els

theorem compV : ∀ n, Complete LibInt.intR (elsV F n)
  | 0 => fun z => by
    show LibInt.intR.add (delta LibInt.intR () z) LibInt.intR.zero = LibInt.intR.one
    rfl
  | n + 1 => CubicInst.comp_prodL F.els (elsV F n) F.comp (compV n)

theorem cardV : ∀ n, sumK LibInt.intR (fun _ => (1 : Int)) (elsV F n) = Int.ofNat ((2 * F.h + 1) ^ n)
  | 0 => rfl
  | n + 1 => by
    show sumK LibInt.intR (fun _ => (1 : Int)) (CubicInst.prodL (elsV F n) F.els) = _
    rw [CubicInst.card_prodL F.els (elsV F n) _ _ F.card (cardV n)]
    show Int.ofNat ((2 * F.h + 1) * (2 * F.h + 1) ^ n) = Int.ofNat ((2 * F.h + 1) ^ n * (2 * F.h + 1))
    rw [Nat.mul_comm]

theorem odd_pow (a : Nat) : ∀ n, ∃ X, (2 * a + 1) ^ n = 2 * X + 1
  | 0 => ⟨0, rfl⟩
  | n + 1 => by
    match odd_pow a n with
    | ⟨X, hX⟩ =>
      exact ⟨2 * X * a + X + a, by
        show (2 * a + 1) ^ n * (2 * a + 1) = _
        rw [hX]
        lib_nat⟩

/-- **The Kummer extension of degree n.** -/
theorem kummer_ff (j e : Nat) (hpr : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1)
    (hp : nsm F.K (j + 1 + 1) F.K.one = F.K.zero) (j' t : Nat) (hQ : (j + 1 + 1) ^ e = (j' + 1 + 1) * t + 1)
    (hq : 2 * F.h + 1 = (j' + 1 + 1) * t + 1)
    (hr : ∀ d, Dv d (j' + 1 + 1) → d = 1 ∨ d = j' + 1 + 1) (c : R)
    (hwr : pw F.K (pw F.K c t) (j' + 1 + 1) = F.K.one) (hw1 : pw F.K c t ≠ F.K.one) :
    ∃ G : FinField.FF (V R (j' + 1 + 1)), 2 * G.h + 1 = (2 * F.h + 1) ^ (j' + 1 + 1) ∧
      nsm G.K (j + 1 + 1) G.K.one = G.K.zero := by
  have h10 : F.K.one ≠ F.K.zero := fun e => F.ne_pm (by
    rw [e]
    show F.K.neg F.K.zero = F.K.zero
    lib_ring)
  have hferm : ∀ a, pw F.K a ((j' + 1 + 1) * t + 1) = a := fun a => by
    rw [← hq]
    exact GenCubicExt.fermat_q F a
  have hE := nozd F.K c t j' F.nozd h10 F.inv j e hpr hp hQ hferm hr hwr hw1
  match GenCubicExt.h_succ F, odd_pow F.h (j' + 1) with
  | ⟨a', ha'⟩, ⟨X, hX⟩ =>
    have hk : (2 * F.h + 1) ^ (j' + 1 + 1) = 2 * ((2 * X * (a' + 1) + X + a') + 1) + 1 := by
      show (2 * F.h + 1) ^ (j' + 1) * (2 * F.h + 1) = _
      rw [hX, ha']
      lib_nat
    have card : sumK LibInt.intR (fun _ => (1 : Int)) (elsV F (j' + 1 + 1))
        = Int.ofNat (2 * ((2 * X * (a' + 1) + X + a') + 1) + 1) := by
      rw [cardV, hk]
    have e1 : gt F.K (j' + 1) (tring (j' + 1) F.K c).one (ring (j' + 1)).zero = F.K.one := by
      show gt F.K (j' + 1) (gone F.K (j' + 1)) (ring (j' + 1)).zero = F.K.one
      unfold gone
      rw [gt_tb]
      unfold delta
      rw [ite_pos' _ _ rfl]
    have hne1 : (tring (j' + 1) F.K c).neg (tring (j' + 1) F.K c).one ≠ (tring (j' + 1) F.K c).one := fun h => F.ne_pm (by
      have h1 := congrArg (fun v => gt F.K (j' + 1) v (ring (j' + 1)).zero) h
      have e2 : gt F.K (j' + 1) ((tring (j' + 1) F.K c).neg (tring (j' + 1) F.K c).one) (ring (j' + 1)).zero
          = F.K.neg (gt F.K (j' + 1) (tring (j' + 1) F.K c).one (ring (j' + 1)).zero) :=
        gt_tb F.K (j' + 1) (fun x => F.K.neg (gt F.K (j' + 1) (tring (j' + 1) F.K c).one x)) _
      calc F.K.neg F.K.one = F.K.neg (gt F.K (j' + 1) (tring (j' + 1) F.K c).one (ring (j' + 1)).zero) := by rw [e1]
        _ = gt F.K (j' + 1) ((tring (j' + 1) F.K c).neg (tring (j' + 1) F.K c).one) (ring (j' + 1)).zero := e2.symm
        _ = gt F.K (j' + 1) (tring (j' + 1) F.K c).one (ring (j' + 1)).zero := h1
        _ = F.K.one := e1)
    have hfermE : ∀ x, x ≠ (tring (j' + 1) F.K c).zero →
        pw (tring (j' + 1) F.K c) x (2 * ((2 * X * (a' + 1) + X + a') + 1)) = (tring (j' + 1) F.K c).one := fun x hx => by
      have hc : ∀ z, FinList.cnt z (elsV F (j' + 1 + 1)) = 1 := fun z =>
        Int.ofNat.inj ((FinList.cnt_int z _).symm.trans (compV F (j' + 1 + 1) z))
      have hl := FinList.len_nzL (tring (j' + 1) F.K c).zero (elsV F (j' + 1 + 1))
      have hlen : (elsV F (j' + 1 + 1)).length = 2 * ((2 * X * (a' + 1) + X + a') + 1) + 1 :=
        Int.ofNat.inj ((FinList.len_int _).symm.trans card)
      rw [hc, hlen] at hl
      rw [← Nat.succ.inj hl]
      apply FiniteField.unit_pow (tring (j' + 1) F.K c) hE (inv F.K j' c t F.iv)
        (fun y hy => inv_spec F.K j' c t F.nozd h10 F.inv F.iv F.iv_mul j e hpr hp hQ hferm hr hwr hw1 y hy)
      · intro z
        rw [QuadExt.cnt_eq, QuadExt.cnt_nzL]
        by_cases ez : z = (tring (j' + 1) F.K c).zero
        · rw [ite_pos' _ _ ez, ite_pos' _ _ ez]
        · rw [ite_neg' _ _ ez, ite_neg' _ _ ez, hc]
      · intro z hz ez
        rw [QuadExt.cnt_eq, QuadExt.cnt_nzL, ite_pos' _ _ ez] at hz
        exact Nat.lt_irrefl 0 hz
      · exact one_ne F.K (j' + 1) c h10
      · exact hx
    refine ⟨MkFF.mkFF (tring (j' + 1) F.K c) hE (elsV F (j' + 1 + 1)) (compV F (j' + 1 + 1)) (2 * X * (a' + 1) + X + a')
      card hne1 hfermE, hk.symm, char_T F.K (j' + 1) c _ hp⟩

end KummerFF

#print axioms KummerFF.elsV
#print axioms KummerFF.compV
#print axioms KummerFF.cardV
#print axioms KummerFF.odd_pow
#print axioms KummerFF.kummer_ff

lang/KummerInv.lean

lean_all/lang/KummerInv.lean

/-
  lang/KummerInv — T49 (Kummer step): **inverses in K[x]/(x^n − c) through the norm**. With Φ the Frobenius
  (lang/TwistIter) of period n: N(y) = Π_{k<n} Φ^k(y) is Φ-fixed, hence a constant N(y)(0)·1 (lang/TwistFix);
  for y ≠ 0 every Φ^k(y) ≠ 0, so N(y) ≠ 0 when the ring has no zero divisors (lang/KummerNoZD), and
  y^(-1) = (N(y)(0)^(-1)·1)·Π_{0<k<n} Φ^k(y).
    nrm, cof, nrm_eq, nrm_fix, it_ne_zero, prodI_ne, nrm_const, inv, inv_spec
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.KummerNoZD

namespace KummerInv

open FTAChain ZMod YMChain FiniteRecChain DetChain GroupRing Frobenius TwistRing TwistFrob TwistPhi TwistIter TwistCoef
  TwistS TwistBeta TwistFix IdemAtom1 IdemAtom2 KummerNoZD

variable {R : Type} (K : CRing R) (j' : Nat) (c : R) (t : Nat)

def nrm (y : V R (j' + 1 + 1)) : V R (j' + 1 + 1) := prodI (tring (j' + 1) K c) (Phi K (j' + 1) c t) y (j' + 1 + 1)

def cof (y : V R (j' + 1 + 1)) : V R (j' + 1 + 1) := prodQ (tring (j' + 1) K c) (Phi K (j' + 1) c t) y (j' + 1)

theorem nrm_eq (y : V R (j' + 1 + 1)) : nrm K j' c t y = (tring (j' + 1) K c).mul y (cof K j' c t y) :=
  prod_succ (tring (j' + 1) K c) (Phi K (j' + 1) c t) y (j' + 1)

theorem nrm_fix (j e : Nat) (hpr : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1)
    (hp : nsm K (j + 1 + 1) K.one = K.zero) (hQ : (j + 1 + 1) ^ e = (j' + 1 + 1) * t + 1)
    (hferm : ∀ a, pw K a ((j' + 1 + 1) * t + 1) = a) (hwr : pw K (pw K c t) (j' + 1 + 1) = K.one)
    (y : V R (j' + 1 + 1)) : Phi K (j' + 1) c t (nrm K j' c t y) = nrm K j' c t y := by
  unfold nrm
  rw [phi_prod (tring (j' + 1) K c) (Phi K (j' + 1) c t) (endo K (j' + 1) c t j e hpr hp hQ)]
  show (tring (j' + 1) K c).mul (prodQ (tring (j' + 1) K c) (Phi K (j' + 1) c t) y (j' + 1))
      (it (Phi K (j' + 1) c t) (j' + 1 + 1) y) = _
  rw [it_eq, phiN_r K c t j' j e hpr hp hQ hferm hwr, (tring (j' + 1) K c).mul_comm, ← prod_succ]

theorem it_ne_zero (j e : Nat) (hpr : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1)
    (hp : nsm K (j + 1 + 1) K.one = K.zero) (hQ : (j + 1 + 1) ^ e = (j' + 1 + 1) * t + 1)
    (hferm : ∀ a, pw K a ((j' + 1 + 1) * t + 1) = a) (hwr : pw K (pw K c t) (j' + 1 + 1) = K.one)
    (y : V R (j' + 1 + 1)) (hy : y ≠ (tring (j' + 1) K c).zero) (k : Nat) (hk : k ≤ j' + 1 + 1) :
    it (Phi K (j' + 1) c t) k y ≠ (tring (j' + 1) K c).zero := fun h => hy (by
  match Nat.le.dest hk with
  | ⟨d, hd⟩ =>
    have e1 : it (Phi K (j' + 1) c t) (k + d) y = y := by
      rw [hd, it_eq]
      exact phiN_r K c t j' j e hpr hp hQ hferm hwr y
    rw [← e1, it_comp, h, it_zero (tring (j' + 1) K c) (Phi K (j' + 1) c t) (endo K (j' + 1) c t j e hpr hp hQ)])

theorem prodI_ne (hE : NoZD (tring (j' + 1) K c)) (h10 : (tring (j' + 1) K c).one ≠ (tring (j' + 1) K c).zero)
    (y : V R (j' + 1 + 1)) (hit : ∀ k, k ≤ j' + 1 + 1 → it (Phi K (j' + 1) c t) k y ≠ (tring (j' + 1) K c).zero) :
    ∀ k, k ≤ j' + 1 + 1 → prodI (tring (j' + 1) K c) (Phi K (j' + 1) c t) y k ≠ (tring (j' + 1) K c).zero
  | 0, _ => h10
  | k + 1, hk => fun h => by
    rcases hE _ _ h with h1 | h1
    · exact prodI_ne hE h10 y hit k (Nat.le_of_succ_le hk) h1
    · exact hit k (Nat.le_of_succ_le hk) h1

/-- The inverse, given an inverse map on K. -/
def inv (ivK : R → R) (y : V R (j' + 1 + 1)) : V R (j' + 1 + 1) :=
  (tring (j' + 1) K c).mul
    (GroupRingFrob.mono K (j' + 1) (ring (j' + 1)).zero (ivK (gt K (j' + 1) (nrm K j' c t y) (ring (j' + 1)).zero)))
    (cof K j' c t y)

/-- **y^(-1)·y = 1.** -/
theorem inv_spec [DecidableEq R] (hK : NoZD K) (h10 : K.one ≠ K.zero) (hinv : ∀ x, x ≠ K.zero → ∃ y, K.mul x y = K.one)
    (ivK : R → R) (hivK : ∀ x, x ≠ K.zero → K.mul (ivK x) x = K.one)
    (j e : Nat) (hpr : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1)
    (hp : nsm K (j + 1 + 1) K.one = K.zero) (hQ : (j + 1 + 1) ^ e = (j' + 1 + 1) * t + 1)
    (hferm : ∀ a, pw K a ((j' + 1 + 1) * t + 1) = a)
    (hr : ∀ d, Dv d (j' + 1 + 1) → d = 1 ∨ d = j' + 1 + 1)
    (hwr : pw K (pw K c t) (j' + 1 + 1) = K.one) (hw1 : pw K c t ≠ K.one)
    (y : V R (j' + 1 + 1)) (hy : y ≠ (tring (j' + 1) K c).zero) :
    (tring (j' + 1) K c).mul (inv K j' c t ivK y) y = (tring (j' + 1) K c).one := by
  have hE := nozd K c t j' hK h10 hinv j e hpr hp hQ hferm hr hwr hw1
  have hN := fix_const K j' c t hK j e hpr hp hQ hferm hr hwr hw1 (nrm K j' c t y) (nrm_fix K j' c t j e hpr hp hQ hferm hwr y)
  obtain ⟨s, hs⟩ : ∃ s, s = gt K (j' + 1) (nrm K j' c t y) (ring (j' + 1)).zero := ⟨_, rfl⟩
  have hN0 : nrm K j' c t y ≠ (tring (j' + 1) K c).zero :=
    prodI_ne K j' c t hE (one_ne K (j' + 1) c h10) y
      (fun k hk => it_ne_zero K j' c t j e hpr hp hQ hferm hwr y hy k hk) (j' + 1 + 1) (Nat.le_refl _)
  have hs0 : s ≠ K.zero := fun h => hN0 (by rw [hN, ← hs, h]; exact mono0_zero K j' c)
  unfold inv
  rw [← hs, (tring (j' + 1) K c).mul_assoc, (tring (j' + 1) K c).mul_comm (cof K j' c t y), ← nrm_eq, hN, ← hs]
  show tmul (j' + 1) K c (GroupRingFrob.mono K (j' + 1) (ring (j' + 1)).zero (ivK s))
    (GroupRingFrob.mono K (j' + 1) (ring (j' + 1)).zero s) = _
  rw [mono0_mul, hivK s hs0]
  exact (one_eq K (j' + 1) c).symm

end KummerInv

#print axioms KummerInv.nrm
#print axioms KummerInv.cof
#print axioms KummerInv.nrm_eq
#print axioms KummerInv.nrm_fix
#print axioms KummerInv.it_ne_zero
#print axioms KummerInv.prodI_ne
#print axioms KummerInv.inv
#print axioms KummerInv.inv_spec

lang/KummerNoZD.lean

lean_all/lang/KummerNoZD.lean

/-
  lang/KummerNoZD — T49 (Kummer step): **K[x]/(x^n − c) has no zero divisors** when K is a field of characteristic p
  with a^Q = a (Q = p^e = nt + 1), n = j' + 2 prime and ω = c^t ≠ 1, ω^n = 1. If ab = 0 with a ≠ 0, then
  e = a^(Q^n − 1) is an idempotent (every u satisfies u^(Q^n) = Φ^n(u) = u) with ea = a, eb = 0; if e ≠ 1,
  lang/IdemAtom3 gives an idempotent orthogonal to its Frobenius conjugates, which lang/TwistBeta rules out.
    instDecV         decidable equality of tuples
    it_eq, phiN_r, phiN_pow, endo, one_ne
    nozd
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.TwistFix
import lang.IdemAtom3

namespace KummerNoZD

open FTAChain ZMod YMChain FiniteRecChain DetChain GroupRing Frobenius TwistRing TwistFrob TwistPhi TwistIter TwistCoef
  TwistS TwistBeta TwistFix

instance instDecV {R : Type} [DecidableEq R] : ∀ n, DecidableEq (V R n)
  | 0 => inferInstanceAs (DecidableEq Unit)
  | n + 1 => @instDecidableEqProd R (V R n) _ (instDecV n)

variable {R : Type} (K : CRing R) (m : Nat) (c : R) (t : Nat)

theorem it_eq (u : V R (m + 1)) : ∀ k, IdemAtom1.it (Phi K m c t) k u = phiN K m c t k u
  | 0 => rfl
  | k + 1 => by
    show Phi K m c t (IdemAtom1.it (Phi K m c t) k u) = Phi K m c t (phiN K m c t k u)
    rw [it_eq u k]

theorem phiN_pow (u : V R (m + 1)) : ∀ k, phiN K m c t k u = pw (tring m K c) u (((m + 1) * t + 1) ^ k)
  | 0 => ((tring m K c).one_mul u).symm
  | k + 1 => by
    show pw (tring m K c) (phiN K m c t k u) ((m + 1) * t + 1) = pw (tring m K c) u (((m + 1) * t + 1) ^ k * ((m + 1) * t + 1))
    rw [phiN_pow u k, TwoPower.pwpow]

theorem endo (j e : Nat) (hpr : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1)
    (hp : nsm K (j + 1 + 1) K.one = K.zero) (hQ : (j + 1 + 1) ^ e = (m + 1) * t + 1) :
    IdemAtom1.Endo (tring m K c) (Phi K m c t) where
  hadd := fun a b => by
    unfold Phi
    rw [← hQ]
    exact frob_pk (tring m K c) j hpr (char_T K m c _ hp) a b e
  hmul := fun a b => Frobenius.pw_mul (tring m K c) a b _
  hone := TwoPower.pwone (tring m K c) _

theorem one_ne (h10 : K.one ≠ K.zero) : (tring m K c).one ≠ (tring m K c).zero := fun h => h10 (by
  have h1 := congrArg (fun v => gt K m v (ring m).zero) h
  have e1 : gt K m (tring m K c).one (ring m).zero = K.one := by
    show gt K m (gone K m) (ring m).zero = K.one
    unfold gone
    rw [gt_tb]
    unfold delta
    rw [ite_pos' _ _ rfl]
  have e0 : gt K m (tring m K c).zero (ring m).zero = K.zero := gt_tb K m (fun _ => K.zero) _
  exact e1.symm.trans (h1.trans e0))

variable (j' : Nat)

theorem phiN_r (j e : Nat) (hpr : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1)
    (hp : nsm K (j + 1 + 1) K.one = K.zero) (hQ : (j + 1 + 1) ^ e = (j' + 1 + 1) * t + 1)
    (hferm : ∀ a, pw K a ((j' + 1 + 1) * t + 1) = a) (hwr : pw K (pw K c t) (j' + 1 + 1) = K.one)
    (u : V R (j' + 1 + 1)) : phiN K (j' + 1) c t (j' + 1 + 1) u = u := by
  have hm : 1 < j' + 1 + 1 := Nat.succ_lt_succ (Nat.zero_lt_succ j')
  rw [phiN_gen K (j' + 1) c t j e hpr hp hQ hm hferm u]
  exact (sumK_ext (tring (j' + 1) K c) _ _ (fun a => by
    rw [z_pow K (j' + 1 + 1) (pw K c t) hwr, K.mul_comm, K.one_mul]) _).trans (decompT K (j' + 1) c u).symm

theorem pos_form (hw1 : pw K c t ≠ K.one) : ∃ M, ((j' + 1 + 1) * t + 1) ^ (j' + 1 + 1) = M + 1 + 1 := by
  match t, hw1 with
  | 0, hw1 => exact absurd (rfl : pw K c 0 = K.one) hw1
  | t' + 1, _ =>
    match TwistPhi.pow_succ_ex ((j' + 1 + 1) * (t' + 1)) (j' + 1) with
    | ⟨N, hN⟩ =>
      refine ⟨N * ((j' + 1 + 1) * (t' + 1) + 1) + (j' + 1 + 1) * t' + j' + 1, ?_⟩
      show ((j' + 1 + 1) * (t' + 1) + 1) ^ (j' + 1) * ((j' + 1 + 1) * (t' + 1) + 1) = _
      rw [hN]
      lib_nat

/-- **K[x]/(x^n − c) has no zero divisors.** -/
theorem nozd [DecidableEq R] (hK : NoZD K) (h10 : K.one ≠ K.zero) (hinv : ∀ x, x ≠ K.zero → ∃ y, K.mul x y = K.one)
    (j e : Nat) (hpr : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1)
    (hp : nsm K (j + 1 + 1) K.one = K.zero) (hQ : (j + 1 + 1) ^ e = (j' + 1 + 1) * t + 1)
    (hferm : ∀ a, pw K a ((j' + 1 + 1) * t + 1) = a)
    (hr : ∀ d, Dv d (j' + 1 + 1) → d = 1 ∨ d = j' + 1 + 1)
    (hwr : pw K (pw K c t) (j' + 1 + 1) = K.one) (hw1 : pw K c t ≠ K.one) :
    NoZD (tring (j' + 1) K c) := fun a b hab => by
  by_cases ha : a = (tring (j' + 1) K c).zero
  · exact Or.inl ha
  · right
    match pos_form K c t j' hw1 with
    | ⟨M, hM⟩ =>
      have haM : pw (tring (j' + 1) K c) a (M + 1 + 1) = a := by
        rw [← hM, ← phiN_pow K (j' + 1) c t a (j' + 1 + 1)]
        exact phiN_r K c t j' j e hpr hp hQ hferm hwr a
      have he : (tring (j' + 1) K c).mul (pw (tring (j' + 1) K c) a (M + 1)) (pw (tring (j' + 1) K c) a (M + 1))
          = pw (tring (j' + 1) K c) a (M + 1) := by
        rw [← TwistRing.pw_add, show M + 1 + (M + 1) = M + (M + 1 + 1) by lib_nat, TwistRing.pw_add, haM]
        rfl
      have hea : (tring (j' + 1) K c).mul (pw (tring (j' + 1) K c) a (M + 1)) a = a := haM
      have heb : (tring (j' + 1) K c).mul (pw (tring (j' + 1) K c) a (M + 1)) b = (tring (j' + 1) K c).zero := by
        show (tring (j' + 1) K c).mul ((tring (j' + 1) K c).mul (pw (tring (j' + 1) K c) a M) a) b = _
        rw [(tring (j' + 1) K c).mul_assoc, hab, DetChain.mul_zero]
      have he0 : pw (tring (j' + 1) K c) a (M + 1) ≠ (tring (j' + 1) K c).zero := fun h => ha (by
        rw [← hea, h, (tring (j' + 1) K c).mul_comm, DetChain.mul_zero])
      by_cases he1 : pw (tring (j' + 1) K c) a (M + 1) = (tring (j' + 1) K c).one
      · rw [← heb, he1, (tring (j' + 1) K c).one_mul]
      · exfalso
        have H := endo K (j' + 1) c t j e hpr hp hQ
        have hper : ∀ x, IdemAtom1.it (Phi K (j' + 1) c t) (j' + 1 + 1) x = x := fun x => by
          rw [it_eq]
          exact phiN_r K c t j' j e hpr hp hQ hferm hwr x
        have hfixid : ∀ f, Phi K (j' + 1) c t f = f → (tring (j' + 1) K c).mul f f = f →
            f = (tring (j' + 1) K c).zero ∨ f = (tring (j' + 1) K c).one := fun f hf hff =>
          idem_fix K j' c t hK j e hpr hp hQ hferm hr hwr hw1 f hf hff
        match IdemAtom3.atom_orth (tring (j' + 1) K c) (Phi K (j' + 1) c t) H (one_ne K (j' + 1) c h10) j' hper hfixid hr
            _ he he0 he1 with
        | ⟨u, hu0, huu, horth⟩ =>
          exact twist_beta K j' c t hK h10 hinv j e hpr hp hQ hferm hr hwr hw1 u huu (fun k hk0 hk => by
            rw [← it_eq]
            exact horth k hk0 hk) hu0

end KummerNoZD

#print axioms KummerNoZD.instDecV
#print axioms KummerNoZD.it_eq
#print axioms KummerNoZD.phiN_pow
#print axioms KummerNoZD.endo
#print axioms KummerNoZD.one_ne
#print axioms KummerNoZD.phiN_r
#print axioms KummerNoZD.pos_form
#print axioms KummerNoZD.nozd

lang/LegMul.lean

lean_all/lang/LegMul.lean

/-
  lang/LegMul — T43, cycle 35: the Legendre symbol is multiplicative (Boolean form).

  p = h + h + 1 prime, p ∤ a, p ∤ b, legB p a = true ⇔ a is a square mod p. Then
    mul_pow'      (a b)^h = a^h b^h                                 (Nat.mul_pow carries propext)
    leg_euler     legB p a = true ⇔ a^h ≡ 1   (Euler, QRChain.euler_iff)
    legB_mul      legB p (a b) = (legB p a == legB p b),  i.e. (ab/p) = (a/p)(b/p)
  With QuadPeriod, GaussSplit, TwoSupp: (a/p) is a product of characters of p mod 4q, mod 4, mod 8 over the prime
  factors of a — Artin reciprocity for every quadratic field ℚ(√a) (next cycle).

  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.QuadPeriod
import lang.PrimRoot

namespace LegMul

open FTAChain QRChain

theorem mul_pow' (a b : Nat) : ∀ n, (a * b) ^ n = a ^ n * b ^ n
  | 0 => rfl
  | n + 1 => by
    show (a * b) ^ n * (a * b) = (a ^ n * a) * (b ^ n * b)
    rw [mul_pow' a b n, mmid4]

theorem leg_euler (h a : Nat) (hp : Pr (h + h + 1)) (ha : ¬ Dv (h + h + 1) a) :
    legB (h + h + 1) a = true ↔ rem (h + h + 1) (a ^ h) = 1 :=
  ⟨fun t => (euler_iff h a hp ha).mpr ((legB_iff (h + h) a).mp t),
   fun e => (legB_iff (h + h) a).mpr ((euler_iff h a hp ha).mp e)⟩

theorem not_dv_mul (h a b : Nat) (hp : Pr (h + h + 1)) (ha : ¬ Dv (h + h + 1) a) (hb : ¬ Dv (h + h + 1) b) :
    ¬ Dv (h + h + 1) (a * b) := fun hd => by
  have hp' : ∀ d, d ∣ h + h + 1 → d = 1 ∨ d = h + h + 1 := fun d hdv => hp.2 d (PrimRoot.dv_of_dvd hdv)
  cases LibGcd.euclid_prime hp' (PrimRoot.dvd_of_dv hd) with
  | inl h1 => exact ha (PrimRoot.dv_of_dvd h1)
  | inr h1 => exact hb (PrimRoot.dv_of_dvd h1)

theorem false_of_neg (h a : Nat) (hp : Pr (h + h + 1)) (ha : ¬ Dv (h + h + 1) a)
    (e : rem (h + h + 1) (a ^ h) = h + h) : legB (h + h + 1) a = false := by
  cases hl : legB (h + h + 1) a with
  | false => rfl
  | true =>
    exfalso
    have := (leg_euler h a hp ha).mp hl
    rw [e] at this
    exact par_one_ne h this

/-- **(ab/p) = (a/p)(b/p).** -/
theorem legB_mul (h a b : Nat) (hp : Pr (h + h + 1)) (ha : ¬ Dv (h + h + 1) a) (hb : ¬ Dv (h + h + 1) b) :
    legB (h + h + 1) (a * b) = (legB (h + h + 1) a == legB (h + h + 1) b) := by
  have hab := not_dv_mul h a b hp ha hb
  have h0 : 0 < h + h := Nat.lt_of_lt_of_le (h_pos h hp) (Nat.le_add_right h h)
  have r1 : rem (h + h + 1) 1 = 1 := rem_one (h + h) h0
  have rN : rem (h + h + 1) (h + h) = h + h := rem_of_lt (h + h) (h + h) (Nat.lt_succ_self _)
  have hpow : rem (h + h + 1) ((a * b) ^ h) = rem (h + h + 1) (rem (h + h + 1) (a ^ h) * rem (h + h + 1) (b ^ h)) := by
    rw [mul_pow']
    exact rem_mul_congr2 (h + h) _ _ _ _ (rem_idem (h + h) _).symm (rem_idem (h + h) _).symm
  cases euler_pm h a hp ha with
  | inl ea =>
    have la := (leg_euler h a hp ha).mpr ea
    cases euler_pm h b hp hb with
    | inl eb =>
      have lb := (leg_euler h b hp hb).mpr eb
      rw [la, lb, (leg_euler h _ hp hab).mpr (by rw [hpow, ea, eb]; exact r1)]
      rfl
    | inr eb =>
      have lb := false_of_neg h b hp hb eb
      rw [la, lb, false_of_neg h _ hp hab (by rw [hpow, ea, eb, Nat.one_mul]; exact rN)]
      rfl
  | inr ea =>
    have la := false_of_neg h a hp ha ea
    cases euler_pm h b hp hb with
    | inl eb =>
      have lb := (leg_euler h b hp hb).mpr eb
      rw [la, lb, false_of_neg h _ hp hab (by rw [hpow, ea, eb, Nat.mul_one]; exact rN)]
      rfl
    | inr eb =>
      have lb := false_of_neg h b hp hb eb
      rw [la, lb, (leg_euler h _ hp hab).mpr (by rw [hpow, ea, eb]; exact neg_sq' h hp)]
      rfl

end LegMul

#print axioms LegMul.mul_pow'
#print axioms LegMul.leg_euler
#print axioms LegMul.not_dv_mul
#print axioms LegMul.false_of_neg
#print axioms LegMul.legB_mul

lang/LegendreFour.lean

lean_all/lang/LegendreFour.lean

/-
  lang/LegendreFour — T43, cycle 354: #E_λ(𝔽_p) ≡ 0 (mod 4) for the Legendre curves, by an involution and an ordering.

  p = 2m + 3, λ ≠ 0, 1, g(x) = x(x − 1)(x − λ), a(λ) = Σ_x χ(g x), F(x) = χ(g x)² + χ(g x) ∈ {0, 2}.
    sig_inv       σ(x) = λ/x is an involution of 𝔽_p (σ(0) = 0)
    F_sig         F(σx) = F(x)                                       (g(λ/x) = λ² g(x)/x⁴)
    split3        Σ_x H(x) = Σ_x H(x)[x < σx] + Σ_x H(x)[σx < x] + Σ_x H(x)[σx = x]   (order on the representatives)
    swap_half     Σ_x F(x)[σx < x] = Σ_x F(x)[x < σx]                (reindex by σ)
    fixed_pts     σx = x ⟺ x = 0 or x² = λ
    legendre_four p + 1 + a(λ) = 4k                                  (#E_λ ≡ 0 mod 4: the rational 2-torsion has order 4)
  The pairing argument (orbits of a fixed-point-free involution) is written as sum identities, without finite sets.
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.LegendreMoment

namespace LegendreFour

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum Jacobsthal QuadTwist JacobMoment PencilMoment PencilClosed LegendreMoment
open FiniteRecChain (ite_pos' ite_neg')

variable (m : Nat)

def sig (l x : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).mul l (iv m x)

theorem sig_inv (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (l x : Zp (2 * m + 1 + 1)) (hl : l ≠ (ring (2 * m + 1 + 1)).zero) : sig m l (sig m l x) = x := by
  unfold sig
  rw [inv_mul, inv_inv m hpr]
  calc (ring (2 * m + 1 + 1)).mul l ((ring (2 * m + 1 + 1)).mul (iv m l) x) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m l) l) x := by lib_ring
    _ = x := by rw [iv_mul_self m hpr l hl]; lib_ring

def ltI (a b : Zp (2 * m + 1 + 1)) : Int := if a.val < b.val then 1 else 0

theorem trich (a b : Zp (2 * m + 1 + 1)) : ltI m a b + ltI m b a + delta LibInt.intR b a = 1 := by
  unfold ltI
  cases Nat.lt_or_ge a.val b.val with
  | inl h =>
    rw [ite_pos' _ _ h, ite_neg' _ _ (fun h' => Nat.lt_asymm h h'), delta_zero m _ _ (fun e => Nat.lt_irrefl a.val (by rw [e] at h; exact h))]
    rfl
  | inr h =>
    cases Nat.lt_or_ge b.val a.val with
    | inl h' =>
      rw [ite_neg' _ _ (fun h'' => Nat.lt_asymm h' h''), ite_pos' _ _ h', delta_zero m _ _ (fun e => Nat.lt_irrefl a.val (by rw [e] at h'; exact h'))]
      rfl
    | inr h' =>
      have e : b = a := Subtype.ext (Nat.le_antisymm h h')
      rw [ite_neg' _ _ (fun h'' => Nat.lt_irrefl _ (Nat.lt_of_lt_of_le h'' h')), ite_neg' _ _ (fun h'' => Nat.lt_irrefl _ (Nat.lt_of_lt_of_le h'' h)),
        delta_one m _ _ e]
      rfl

theorem split3 (sg : Zp (2 * m + 1 + 1) → Zp (2 * m + 1 + 1)) (H : Zp (2 * m + 1 + 1) → Int) :
    sumK LibInt.intR H (L m) = sumK LibInt.intR (fun x => H x * ltI m x (sg x)) (L m) + sumK LibInt.intR (fun x => H x * ltI m (sg x) x) (L m)
      + sumK LibInt.intR (fun x => H x * delta LibInt.intR (sg x) x) (L m) := by
  rw [← QuadDiagonal.sI_add, ← QuadDiagonal.sI_add]
  exact sumK_ext LibInt.intR _ _ (fun x => by
    have t := trich m x (sg x)
    calc H x = H x * (ltI m x (sg x) + ltI m (sg x) x + delta LibInt.intR (sg x) x) := by rw [t]; generalize H x = h; lib_int
      _ = _ := by generalize H x = h; generalize ltI m x (sg x) = u; generalize ltI m (sg x) x = v; generalize delta LibInt.intR (sg x) x = d; lib_int) _

theorem swap_half (sg : Zp (2 * m + 1 + 1) → Zp (2 * m + 1 + 1)) (hs : ∀ x, sg (sg x) = x) (H : Zp (2 * m + 1 + 1) → Int) (hH : ∀ x, H (sg x) = H x) :
    sumK LibInt.intR (fun x => H x * ltI m (sg x) x) (L m) = sumK LibInt.intR (fun x => H x * ltI m x (sg x)) (L m) := by
  refine (FiniteRecChain.reindex LibInt.intR (L m) (QuadDiagonal.hcomp m) sg sg hs hs (fun x => H x * ltI m (sg x) x)).symm.trans ?_
  exact sumK_ext LibInt.intR _ _ (fun x => by show H (sg x) * ltI m (sg (sg x)) (sg x) = H x * ltI m x (sg x); rw [hs, hH]) _

theorem g_sig (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (l x : Zp (2 * m + 1 + 1)) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) :
    (ring (2 * m + 1 + 1)).mul (f m (sig m l x)) ((ring (2 * m + 1 + 1)).add (sig m l x) ((ring (2 * m + 1 + 1)).neg l)) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul l l) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m x) (iv m x)) ((ring (2 * m +  -- (+39 chars; full line in the Zenodo source)
  have e : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  unfold sig f
  symm
  calc _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul l l) ((ring (2 * m + 1 + 1)).mul (iv m x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (iv m x) x) ((ring (2 * m + 1 + 1)).neg (iv m x))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (iv m x) x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul l (iv  -- (+66 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul l l) ((ring (2 * m + 1 + 1)).mul (iv m x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg (iv m x))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul l (iv m x))))))) (ring (2 * m + 1   -- (+22 chars; full line in the Zenodo source)
    _ = _ := by lib_ring

/-- F(x) = χ(g x)² + χ(g x). -/
def F (l x : Zp (2 * m + 1 + 1)) : Int := chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))) * chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))) + chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)))

theorem F_sig (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (l x : Zp (2 * m + 1 + 1)) (hl : l ≠ (ring (2 * m + 1 + 1)).zero) : F m l (sig m l x) = F m l x := by
  by_cases hx : x = (ring (2 * m + 1 + 1)).zero
  · rw [hx, show sig m l (ring (2 * m + 1 + 1)).zero = (ring (2 * m + 1 + 1)).zero by unfold sig; rw [show iv m (ring (2 * m + 1 + 1)).zero = (ring (2 * m + 1 + 1)).zero from iv_zero m]; lib_ring]
  · have hix := iv_nz m hpr x hx
    have hc : chi m ((ring (2 * m + 1 + 1)).mul (f m (sig m l x)) ((ring (2 * m + 1 + 1)).add (sig m l x) ((ring (2 * m + 1 + 1)).neg l))) = chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))) := by
      rw [g_sig m hpr l x hx, chi_mul m hpr ((ring (2 * m + 1 + 1)).mul l l), chi_mul m hpr ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))), chi_mul m hpr ((ring (2 * m + 1 + 1)).mul (iv m x) (iv m x)) ((ring (2 * m + 1 + 1)).mul (iv m x) (iv m x)),
        chi_sq m hpr _ hl, chi_sq m hpr _ hix]
      generalize chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))) = t; lib_int
    unfold F; rw [hc]

theorem fixed_delta (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (l x : Zp (2 * m + 1 + 1)) (hl : l ≠ (ring (2 * m + 1 + 1)).zero) :
    delta LibInt.intR (sig m l x) x = delta LibInt.intR x (ring (2 * m + 1 + 1)).zero + delta LibInt.intR ((ring (2 * m + 1 + 1)).mul x x) l := by
  by_cases hx : x = (ring (2 * m + 1 + 1)).zero
  · rw [hx, show sig m l (ring (2 * m + 1 + 1)).zero = (ring (2 * m + 1 + 1)).zero by unfold sig; rw [show iv m (ring (2 * m + 1 + 1)).zero = (ring (2 * m + 1 + 1)).zero from iv_zero m]; lib_ring,
      delta_one m _ _ rfl, delta_zero m _ _ (fun e => hl (by rw [← e]; lib_ring))]; rfl
  · have e : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
    rw [delta_zero m _ _ hx]
    by_cases h : (ring (2 * m + 1 + 1)).mul x x = l
    · rw [delta_one m _ _ h, delta_one m _ _ (by
        unfold sig; rw [← h]
        calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) (iv m x) = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul (iv m x) x) := by lib_ring
          _ = x := by rw [e]; lib_ring)]; rfl
    · rw [delta_zero m _ _ h, delta_zero m _ _ (fun e' => h (by
        unfold sig at e'
        calc (ring (2 * m + 1 + 1)).mul x x = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul l (iv m x)) x := by rw [e']
          _ = (ring (2 * m + 1 + 1)).mul l ((ring (2 * m + 1 + 1)).mul (iv m x) x) := by lib_ring
          _ = l := by rw [e]; lib_ring))]; rfl

theorem F_zero (l : Zp (2 * m + 1 + 1)) : F m l (ring (2 * m + 1 + 1)).zero = 0 := by
  unfold F; rw [show (ring (2 * m + 1 + 1)).mul (f m (ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).neg l)) = (ring (2 * m + 1 + 1)).zero by unfold f; lib_ring, chi_zero]; rfl

theorem F_even (x l : Zp (2 * m + 1 + 1)) : ∃ k : Int, F m l x = 2 * k := by
  unfold F
  rcases chi_cases m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))) with e | e | e
  · exact ⟨0, by rw [e]; rfl⟩
  · exact ⟨1, by rw [e]; rfl⟩
  · exact ⟨0, by rw [e]; rfl⟩

/-- the fixed points of σ contribute 0 or 4. -/
theorem fixed_sum (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (l : Zp (2 * m + 1 + 1)) (hl : l ≠ (ring (2 * m + 1 + 1)).zero) (hl1 : l ≠ (ring (2 * m + 1 + 1)).one) :
    ∃ k : Int, sumK LibInt.intR (fun x => F m l x * delta LibInt.intR (sig m l x) x) (L m) = 4 * k := by
  rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.add (LibInt.intR.mul (delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) (F m l x)) (LibInt.intR.mul (delta LibInt.intR ((ring (2 * m + 1 + 1)).mul x x) l) (F m l x))) (fun x => by
    rw [fixed_delta m hpr l x hl]; show _ = delta LibInt.intR x (ring (2 * m + 1 + 1)).zero * F m l x + delta LibInt.intR ((ring (2 * m + 1 + 1)).mul x x) l * F m l x
    generalize F m l x = a; generalize delta LibInt.intR x (ring (2 * m + 1 + 1)).zero = u; generalize delta LibInt.intR ((ring (2 * m + 1 + 1)).mul x x) l = v; lib_int), sumK_add,
    FiniteRecChain.pick LibInt.intR (L m) (QuadDiagonal.hcomp m) (F m l) (ring (2 * m + 1 + 1)).zero, F_zero]
  by_cases hp : chi m l = 1
  case neg =>
      have hm := chi_pm m l hl hp
      refine ⟨0, ?_⟩
      rw [sumK_ext LibInt.intR _ (fun _ => (0 : Int)) (fun x => by
        rw [delta_zero m _ _ (fun e => by
          have hx0 : x ≠ (ring (2 * m + 1 + 1)).zero := fun h0 => hl (by rw [← e, h0]; lib_ring)
          have := chi_sq m hpr x hx0
          rw [e, hm] at this
          exact int_m1_ne this)]; show 0 * F m l x = 0; generalize F m l x = a; lib_int), QuadDiagonal.zsum]
      rfl
  case pos =>
      match QuadDiagonal.sq_root m hpr l hl (chi_one_pw m l hl hp) with
      | ⟨r, hr⟩ =>
        have hr0 : r ≠ (ring (2 * m + 1 + 1)).zero := fun h0 => hl (by rw [← hr, h0]; lib_ring)
        rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.add (LibInt.intR.mul (delta LibInt.intR x r) (F m l x)) (LibInt.intR.mul (delta LibInt.intR x ((ring (2 * m + 1 + 1)).neg r)) (F m l x))) (fun x => by
          rw [show delta LibInt.intR ((ring (2 * m + 1 + 1)).mul x x) l = delta LibInt.intR x r + delta LibInt.intR x ((ring (2 * m + 1 + 1)).neg r) from by
            rw [← hr, FiniteRecChain.delta_symm, roots_sq m hpr r x hr0]]
          show (delta LibInt.intR x r + delta LibInt.intR x ((ring (2 * m + 1 + 1)).neg r)) * F m l x = delta LibInt.intR x r * F m l x + delta LibInt.intR x ((ring (2 * m + 1 + 1)).neg r) * F m l x
          generalize F m l x = a; generalize delta LibInt.intR x r = u; generalize delta LibInt.intR x ((ring (2 * m + 1 + 1)).neg r) = v; lib_int), sumK_add,
          FiniteRecChain.pick LibInt.intR (L m) (QuadDiagonal.hcomp m) (F m l) r, FiniteRecChain.pick LibInt.intR (L m) (QuadDiagonal.hcomp m) (F m l) ((ring (2 * m + 1 + 1)).neg r)]
        -- F(±r) = χ(−1)² + χ(−1): g(±r) = −r²(r ∓ 1)²
        have hgr : ∀ t : Zp (2 * m + 1 + 1), (ring (2 * m + 1 + 1)).mul t t = l → t ≠ (ring (2 * m + 1 + 1)).zero → (ring (2 * m + 1 + 1)).add t ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ≠ (ring (2 * m + 1 + 1)).zero →
            F m l t = chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) + chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := fun t ht ht0 ht1 => by
          have hg : (ring (2 * m + 1 + 1)).mul (f m t) ((ring (2 * m + 1 + 1)).add t ((ring (2 * m + 1 + 1)).neg l)) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)).add t ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)  -- (+72 chars; full line in the Zenodo source)
            unfold f; rw [← ht]; lib_ring
          have hc : chi m ((ring (2 * m + 1 + 1)).mul (f m t) ((ring (2 * m + 1 + 1)).add t ((ring (2 * m + 1 + 1)).neg l))) = chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by
            rw [hg, chi_mul m hpr, chi_sq m hpr _ (nzm m hpr _ _ ht0 ht1)]; generalize chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = e; lib_int
          unfold F; rw [hc]
        have hne1 : ∀ t : Zp (2 * m + 1 + 1), (ring (2 * m + 1 + 1)).mul t t = l → (ring (2 * m + 1 + 1)).add t ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ≠ (ring (2 * m + 1 + 1)).zero := fun t ht h1 => hl1 (by
          rw [← ht, show t = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add t ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one by lib_ring, h1]; lib_ring)
        rw [hgr r hr hr0 (hne1 r hr), hgr ((ring (2 * m + 1 + 1)).neg r) (by rw [← hr]; lib_ring) (negnz m _ hr0) (hne1 _ (by rw [← hr]; lib_ring))]
        cases QuadDiagonal.e_val m hpr with
        | inl e1 => exact ⟨1, by rw [e1]; rfl⟩
        | inr e1 => exact ⟨0, by rw [e1]; rfl⟩

/-- **#E_λ(𝔽_p) ≡ 0 (mod 4)**: p + 1 + a(λ) = 4k for λ ≠ 0, 1 (a(λ) = Σ_x χ(x(x − 1)(x − λ))). -/
theorem legendre_four (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (l : Zp (2 * m + 1 + 1)) (hl : l ≠ (ring (2 * m + 1 + 1)).zero) (hl1 : l ≠ (ring (2 * m + 1 + 1)).one) :
    ∃ k : Int, P m + 1 + A m l = 4 * k := by
  have hsq : sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))) * chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)))) (L m) = P m + -1 + -1 + -1 := by
    rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.add (LibInt.intR.mul 1 1) (LibInt.intR.add (LibInt.intR.mul (-1) (delta LibInt.intR x (ring (2 * m + 1 + 1)).zero)) (LibInt.intR.add (LibInt.intR.mul (-1) (delta LibInt.intR x (ring (2 * m + 1 + 1)).one)) (LibInt.intR.mul (-1) (delta LibInt.intR x l))))) (fun x => by
      rw [QuadDiagonal.sqchi m hpr, cubic_delta m hpr l x hl hl1]
      show 1 + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).zero + delta LibInt.intR x (ring (2 * m + 1 + 1)).one + delta LibInt.intR x l) = 1 * 1 + (-1 * delta LibInt.intR x (ring (2 * m + 1 + 1)).zero + (-1 * delta LibInt.intR x (ring (2 * m + 1 + 1)).one + -1 * delta LibInt.intR x l))
      generalize delta LibInt.intR x (ring (2 * m + 1 + 1)).zero = u; generalize delta LibInt.intR x (ring (2 * m + 1 + 1)).one = v; generalize delta LibInt.intR x l = w; lib_int)]
    rw [sumK_add, sumK_add, sumK_add, ← mul_sumK, ← mul_sumK, ← mul_sumK, ← mul_sumK, sum_one,
      show sumK LibInt.intR (fun x => delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR (ring (2 * m + 1 + 1)).zero,
      show sumK LibInt.intR (fun x => delta LibInt.intR x (ring (2 * m + 1 + 1)).one) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR (ring (2 * m + 1 + 1)).one,
      show sumK LibInt.intR (fun x => delta LibInt.intR x l) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR l]
    show 1 * P m + (-1 * 1 + (-1 * 1 + -1 * 1)) = _
    lib_int
  have hF : sumK LibInt.intR (F m l) (L m) = (P m + -1 + -1 + -1) + A m l := by
    rw [← hsq]; unfold A; rw [← QuadDiagonal.sI_add]; rfl
  have hsplit := split3 m (sig m l) (F m l)
  rw [swap_half m (sig m l) (fun x => sig_inv m hpr l x hl) (F m l) (fun x => F_sig m hpr l x hl)] at hsplit
  match even_sum m (L m) (fun x => F m l x * ltI m x (sig m l x)) (fun x => by
    match F_even m x l with
    | ⟨k, hk⟩ => exact ⟨k * ltI m x (sig m l x), by rw [hk]; generalize ltI m x (sig m l x) = t; lib_int⟩), fixed_sum m hpr l hl hl1 with
  | ⟨k1, hk1⟩, ⟨k2, hk2⟩ =>
    refine ⟨k1 + k2 + 1, ?_⟩
    have : (P m + -1 + -1 + -1) + A m l = 2 * k1 + 2 * k1 + 4 * k2 := by rw [← hF, hsplit, hk1, hk2]
    calc P m + 1 + A m l = ((P m + -1 + -1 + -1) + A m l) + 4 := by generalize A m l = a; lib_int
      _ = _ := by rw [this]; lib_int

end LegendreFour

#print axioms LegendreFour.sig_inv
#print axioms LegendreFour.trich
#print axioms LegendreFour.split3
#print axioms LegendreFour.swap_half
#print axioms LegendreFour.g_sig
#print axioms LegendreFour.F_sig
#print axioms LegendreFour.fixed_delta
#print axioms LegendreFour.F_zero
#print axioms LegendreFour.F_even
#print axioms LegendreFour.fixed_sum
#print axioms LegendreFour.legendre_four

lang/LegendreMoment.lean

lean_all/lang/LegendreMoment.lean

/-
  lang/LegendreMoment — T43, cycle 316: the second moment of the Legendre family y² = x(x − 1)(x − λ), every odd p.

  p = 2m + 3, χ the Legendre symbol, f(x) = x(x − 1), a(λ) = Σ_x χ(f(x)(x − λ)) (= −a_p of E_λ).
    sq_expand   a(λ)² = Σ_x Σ_y χ(f x)χ(f y)χ((λ − x)(λ − y))
    sum_f       Σ_x χ(x(x − 1)) = −1                              (ConicSum.sum_xxd)
    sum_f2      Σ_x χ(x(x − 1))² = p − 2
    legendre_moment   Σ_λ a(λ)² = p² − 2p − 1                    (JacobMoment.inner)
    refl_one, refl_inv, sum_A   a(1 − λ) = χ(−1)a(λ), a(1/λ) = χ(λ)a(λ), Σ_λ a(λ) = 0
    twisted_sum   Σ_λ χ(λ)a(λ) = χ(−1)
    adjacent      Σ_λ a(λ)a(λ + 1) = −1 − p(1 + χ(−1))
    odd_moments   p ≡ 3 (mod 4): Σ_λ a(λ)^(2k+1) = 0
    family_moment every g: Σ_λ (Σ_x χ(g(x)(x − λ)))² = −(Σ χ(g))² + p Σ χ(g)²
    root_count    a ≠ 0: #{ax² + bx + c = 0} = 1 + χ(b² − 4ac)
    quad_family   a ≠ 0, b² ≠ 4ac: Σ_λ (Σ_x χ((ax² + bx + c)(x − λ)))² = −1 + p(p − 1 − χ(b² − 4ac))
    translation_moment  every h: Σ_λ (Σ_x χ(h(x) + λ))² = −p² + p·#{h(x) = h(y)}
    cube_pairs    p > 3: #{x³ = y³} = p + (p − 1)(1 + χ(−3))   (two moments compared)
    cube_roots    p > 3: #{z : z³ = 1} = 2 + χ(−3)
    fourth_roots  #{z : z⁴ = 1} = 3 + χ(−1)
    sixth_roots   p > 3: #{z : z⁶ = 1} = 4 + 2χ(−3)
    quartic_m1    #{z : z⁴ = −1} = (1 + χ(−1))(1 + χ(2))
    eighth_roots  #{z : z⁸ = 1} = 3 + χ(−1) + (1 + χ(−1))(1 + χ(2))
    cube_m1, sixth_m1   #{t³ = −1} = 2 + χ(−3), #{z⁶ = −1} = (1 + χ(−1))(2 + χ(−3))
    twelfth_roots #{z : z¹² = 1} = (2 + χ(−3))(3 + χ(−1))
    pair_sum      Σ_x (1 + χ(x))(1 + χ(x + d)) = p − 1 (d ≠ 0)
    translation_cov  Σ_λ S_h(λ)S_k(λ) = −p² + p·#{h(x) = k(y)}
    jacobi_quad   J(χ, χ) = Σ_x χ(x)χ(1 − x) = −χ(−1)
    legendre_even λ ≠ 0, 1: a(λ) is even
    char_parity   every g: Σ_x χ(g x) + #{g = 0} ≡ p (mod 2)
    three_div     χ(−3) = 1 ⇒ 3 | p − 1
    three_div_conv  3 | p − 1 ⇒ χ(−3) = 1
    four_div, four_div_conv   χ(−1) = 1 ⟺ 4 | p − 1
    eight_div     χ(−1) = χ(2) = 1 ⇒ 8 | p − 1
    eight_div_conv  8 | p − 1 ⇒ χ(2) = 1
    chi3_one, chi3_eleven   p ≡ ±1 (mod 12) ⇒ χ(3) = 1
    chi3_five, chi3_seven   p ≡ ±5 (mod 12) ⇒ χ(3) = −1
    gauss5, five_div_conv   (z − z² − z³ + z⁴)² = 5 for z⁵ = 1 ≠ z; 5 | p − 1 ⇒ χ(5) = 1
    gauss7, seven_div_conv  (z + z² − z³ + z⁴ − z⁵ − z⁶)² = −7; 7 | p − 1 ⇒ χ(−7) = 1
    gauss11, gauss13, eleven_div_conv, thirteen_div_conv   11 | p − 1 ⇒ χ(−11) = 1, 13 | p − 1 ⇒ χ(13) = 1
    gauss17, gauss19, seventeen_div_conv, nineteen_div_conv   17 | p − 1 ⇒ χ(17) = 1, 19 | p − 1 ⇒ χ(−19) = 1
    square_pairs, square_family   #{x² = y²} = 2p − 1; Σ_λ (Σ_x χ(x² + λ))² = p² − p
    stick_root, stick_three   Stickelberger for cubics with a root: χ(Δ) = χ(disc of the quadratic factor); three roots ⇒ χ(Δ) = 1
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.PencilClosed
import lang.JacobiNorm
import lang.Inf17mod24
import lang.Inf2e1

namespace LegendreMoment

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum Jacobsthal QuadTwist JacobMoment PencilMoment PencilClosed
open FiniteRecChain (ite_pos' ite_neg')

variable (m : Nat)

def f (x : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))

def A (l : Zp (2 * m + 1 + 1)) : Int := sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)))) (L m)

theorem pt (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (l x y : Zp (2 * m + 1 + 1)) :
    chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))) * chi m ((ring (2 * m + 1 + 1)).mul (f m y) ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg l)))
      = (chi m (f m x) * chi m (f m y)) * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg y))) := by
  rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg y)) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)) ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg l)) by lib_ring,
    chi_mul m hpr (f m x), chi_mul m hpr (f m y), chi_mul m hpr ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))]
  generalize chi m (f m x) = a; generalize chi m (f m y) = b
  generalize chi m ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)) = c; generalize chi m ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg l)) = d
  lib_int

theorem sq_expand (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (l : Zp (2 * m + 1 + 1)) :
    A m l * A m l = sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => (chi m (f m x) * chi m (f m y)) * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg y)))) (L m)) (L m) := by
  unfold A
  show LibInt.intR.mul _ _ = _
  rw [sumK_mul]
  exact sumK_ext LibInt.intR _ _ (fun x => by
    rw [mul_sumK]
    exact sumK_ext LibInt.intR _ _ (fun y => pt m hpr l x y) _) _

theorem neg_delta (x y : Zp (2 * m + 1 + 1)) : delta LibInt.intR ((ring (2 * m + 1 + 1)).neg x) ((ring (2 * m + 1 + 1)).neg y) = delta LibInt.intR y x := by
  by_cases h : y = x
  · rw [h, delta_one m _ _ rfl, delta_one m _ _ rfl]
  · rw [delta_zero m _ _ h, delta_zero m _ _ (fun e => h (by
      calc y = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).neg y) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).neg x) := by rw [e]
        _ = x := by lib_ring))]

theorem sum_f (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : sumK LibInt.intR (fun x => chi m (f m x)) (L m) = -1 :=
  sum_xxd m hpr _ (negnz m _ (PowerSum.one_ne_zero' (2 * m + 1)))

theorem f_delta (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x : Zp (2 * m + 1 + 1)) :
    delta LibInt.intR (f m x) (ring (2 * m + 1 + 1)).zero = delta LibInt.intR x (ring (2 * m + 1 + 1)).zero + delta LibInt.intR x (ring (2 * m + 1 + 1)).one := by
  have h10 : ((ring (2 * m + 1 + 1)).one : Zp (2 * m + 1 + 1)) ≠ (ring (2 * m + 1 + 1)).zero := PowerSum.one_ne_zero' (2 * m + 1)
  by_cases h0 : x = (ring (2 * m + 1 + 1)).zero
  · rw [h0, delta_one m _ _ (by unfold f; lib_ring), delta_one m _ _ rfl, delta_zero m _ _ h10.symm]; rfl
  · by_cases h1 : x = (ring (2 * m + 1 + 1)).one
    · rw [h1, delta_one m _ _ (by unfold f; lib_ring), delta_zero m _ _ h10, delta_one m _ _ rfl]; rfl
    · rw [delta_zero m _ _ (fun e => by
          unfold f at e
          cases nz2 m hpr _ _ e with
          | inl h => exact h0 h
          | inr h => exact h1 (by
              calc x = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
                _ = (ring (2 * m + 1 + 1)).one := by rw [h]; lib_ring)),
        delta_zero m _ _ h0, delta_zero m _ _ h1]; rfl

theorem sum_f2 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : sumK LibInt.intR (fun x => chi m (f m x) * chi m (f m x)) (L m) = P m + -1 + -1 := by
  rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.add (LibInt.intR.mul 1 1) (LibInt.intR.add (LibInt.intR.mul (-1) (delta LibInt.intR x (ring (2 * m + 1 + 1)).zero)) (LibInt.intR.mul (-1) (delta LibInt.intR x (ring (2 * m + 1 + 1)).one)))) (fun x => by
    rw [QuadDiagonal.sqchi m hpr, f_delta m hpr]
    show 1 + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).zero + delta LibInt.intR x (ring (2 * m + 1 + 1)).one) = 1 * 1 + (-1 * delta LibInt.intR x (ring (2 * m + 1 + 1)).zero + -1 * delta LibInt.intR x (ring (2 * m + 1 + 1)).one)
    generalize delta LibInt.intR x (ring (2 * m + 1 + 1)).zero = u; generalize delta LibInt.intR x (ring (2 * m + 1 + 1)).one = v; lib_int)]
  rw [sumK_add, sumK_add, ← mul_sumK, ← mul_sumK, ← mul_sumK, sum_one,
    show sumK LibInt.intR (fun x => delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR (ring (2 * m + 1 + 1)).zero,
    show sumK LibInt.intR (fun x => delta LibInt.intR x (ring (2 * m + 1 + 1)).one) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR (ring (2 * m + 1 + 1)).one]
  show 1 * P m + (-1 * 1 + -1 * 1) = _
  lib_int

/-- **Second moment of the Legendre family**: Σ_λ (Σ_x χ(x(x − 1)(x − λ)))² = p² − 2p − 1. -/
theorem legendre_moment (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : sumK LibInt.intR (fun l => A m l * A m l) (L m) = P m * P m + -(P m + P m) + -1 := by
  rw [sumK_ext LibInt.intR _ _ (sq_expand m hpr)]
  rw [← sumK_swap LibInt.intR (fun x l => sumK LibInt.intR (fun y => (chi m (f m x) * chi m (f m y)) * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg y)))) (L m))]
  rw [sumK_ext LibInt.intR _ (fun x => sumK LibInt.intR (fun y => LibInt.intR.add (LibInt.intR.mul (-(chi m (f m x))) (chi m (f m y))) (LibInt.intR.mul (P m * chi m (f m x)) (LibInt.intR.mul (delta LibInt.intR y x) (chi m (f m y))))) (L m)) (fun x => by
    rw [← sumK_swap LibInt.intR (fun y l => (chi m (f m x) * chi m (f m y)) * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg y))))]
    exact sumK_ext LibInt.intR _ _ (fun y => by
      rw [show (fun l => (chi m (f m x) * chi m (f m y)) * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg y))))
          = (fun l => LibInt.intR.mul (chi m (f m x) * chi m (f m y)) (chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg y))))) from rfl,
        ← mul_sumK, inner m hpr, neg_delta]
      show (chi m (f m x) * chi m (f m y)) * (-1 + P m * delta LibInt.intR y x) = -(chi m (f m x)) * chi m (f m y) + P m * chi m (f m x) * (delta LibInt.intR y x * chi m (f m y))
      generalize chi m (f m x) = a; generalize chi m (f m y) = b; generalize delta LibInt.intR y x = d; lib_int) _)]
  rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.add (LibInt.intR.mul (-(chi m (f m x))) (-1)) (LibInt.intR.mul (P m) (chi m (f m x) * chi m (f m x)))) (fun x => by
    rw [sumK_add, ← mul_sumK, ← mul_sumK, FiniteRecChain.pick LibInt.intR (L m) (QuadDiagonal.hcomp m) (fun y => chi m (f m y)) x, sum_f m hpr]
    show -(chi m (f m x)) * -1 + P m * chi m (f m x) * chi m (f m x) = _
    generalize chi m (f m x) = a; show _ = -a * -1 + P m * (a * a); lib_int)]
  rw [sumK_add, ← sumK_mul, ← mul_sumK, sum_f2 m hpr]
  rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.mul (-1) (chi m (f m x))) (fun x => by show -(chi m (f m x)) = -1 * chi m (f m x); generalize chi m (f m x) = a; lib_int)]
  rw [← mul_sumK, sum_f m hpr]
  show -1 * -1 * -1 + P m * (P m + -1 + -1) = _
  lib_int

/-- **A(1 − λ) = χ(−1)A(λ)** (x ↦ 1 − x). -/
theorem refl_one (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (l : Zp (2 * m + 1 + 1)) : A m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg l)) = chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * A m l := by
  unfold A
  refine Eq.trans (FiniteRecChain.reindex LibInt.intR (L m) (QuadDiagonal.hcomp m) (fun x => (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x)) (fun x => (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x))
    (fun x => by lib_ring) (fun y => by lib_ring) (fun x => chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg l))))))).symm ?_
  show _ = LibInt.intR.mul _ _
  rw [mul_sumK]
  exact sumK_ext LibInt.intR _ _ (fun x => by
    show chi m ((ring (2 * m + 1 + 1)).mul (f m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg l))))) = _
    rw [show (ring (2 * m + 1 + 1)).mul (f m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg l))))
      = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))) by unfold f; lib_ring, chi_mul m hpr]; rfl) _

/-- **A(λ⁻¹) = χ(λ)A(λ)** for λ ≠ 0 (x ↦ x/λ). -/
theorem refl_inv (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (l : Zp (2 * m + 1 + 1)) (hl : l ≠ (ring (2 * m + 1 + 1)).zero) : A m (iv m l) = chi m l * A m l := by
  have e : (ring (2 * m + 1 + 1)).mul (iv m l) l = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr l hl
  have hil := iv_nz m hpr l hl
  unfold A
  refine Eq.trans (reindex_mul m hpr (iv m l) hil (fun x => chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg (iv m l)))))).symm ?_
  show _ = LibInt.intR.mul _ _
  rw [mul_sumK]
  exact sumK_ext LibInt.intR _ _ (fun x => by
    show chi m ((ring (2 * m + 1 + 1)).mul (f m ((ring (2 * m + 1 + 1)).mul (iv m l) x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (iv m l) x) ((ring (2 * m + 1 + 1)).neg (iv m l)))) = _
    have hx : (ring (2 * m + 1 + 1)).mul (f m ((ring (2 * m + 1 + 1)).mul (iv m l) x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (iv m l) x) ((ring (2 * m + 1 + 1)).neg (iv m l)))
        = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m l) ((ring (2 * m + 1 + 1)).mul (iv m l) (iv m l))) ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))) := by
      have h1 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (iv m l) x) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).mul (iv m l) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)) := by rw [← e]; lib_ring
      unfold f
      rw [h1]
      lib_ring
    rw [hx, chi_mul m hpr, Jacobsthal.chi_cube m hpr, chi_iv m hpr l hl]; rfl) _

/-- **Σ_λ A(λ) = 0.** -/
theorem sum_A (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : sumK LibInt.intR (fun l => A m l) (L m) = 0 := by
  unfold A
  rw [← sumK_swap LibInt.intR (fun x l => chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))))]
  rw [sumK_ext LibInt.intR _ (fun _ => (0 : Int)) (fun x => by
    rw [sumK_ext LibInt.intR _ (fun l => LibInt.intR.mul (chi m (f m x)) (chi m ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)))) (fun l => chi_mul m hpr _ _), ← mul_sumK, QuadDiagonal.S1 m hpr x]
    show chi m (f m x) * 0 = 0; generalize chi m (f m x) = a; lib_int)]
  exact QuadDiagonal.zsum _

/-- **Σ_λ χ(λ) a(λ) = χ(−1).** -/
theorem twisted_sum (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : sumK LibInt.intR (fun l => chi m l * A m l) (L m) = chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by
  unfold A
  rw [sumK_ext LibInt.intR _ (fun l => sumK LibInt.intR (fun x => LibInt.intR.mul (chi m (f m x)) (chi m ((ring (2 * m + 1 + 1)).mul l ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))))) (L m)) (fun l => by
    show LibInt.intR.mul _ _ = _
    rw [mul_sumK]
    exact sumK_ext LibInt.intR _ _ (fun x => by
      rw [chi_mul m hpr (f m x), chi_mul m hpr l]
      show chi m l * (chi m (f m x) * chi m ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))) = chi m (f m x) * (chi m l * chi m ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)))
      generalize chi m l = a; generalize chi m (f m x) = b; generalize chi m ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)) = c; lib_int) _)]
  rw [sumK_swap LibInt.intR (fun l x => LibInt.intR.mul (chi m (f m x)) (chi m ((ring (2 * m + 1 + 1)).mul l ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)))))]
  have hin : ∀ x, sumK LibInt.intR (fun l => chi m ((ring (2 * m + 1 + 1)).mul l ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)))) (L m)
      = chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * (-1 + P m * delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) := fun x => by
    rw [sumK_ext LibInt.intR _ (fun l => LibInt.intR.mul (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (chi m ((ring (2 * m + 1 + 1)).mul l ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x))))) (fun l => by
      rw [show (ring (2 * m + 1 + 1)).mul l ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul l ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x))) by lib_ring, chi_mul m hpr]; rfl)]
    rw [← mul_sumK]
    by_cases hx : x = (ring (2 * m + 1 + 1)).zero
    · rw [hx, delta_one m _ _ rfl]
      rw [sumK_ext LibInt.intR _ (fun l => 1 + -1 * delta LibInt.intR l (ring (2 * m + 1 + 1)).zero) (fun l => by
        rw [show (ring (2 * m + 1 + 1)).mul l ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).zero)) = (ring (2 * m + 1 + 1)).mul l l by lib_ring]; exact sq_delta m hpr l)]
      rw [QuadDiagonal.sI_add, QuadDiagonal.sI_mul, sum_one, show sumK LibInt.intR (fun l => delta LibInt.intR l (ring (2 * m + 1 + 1)).zero) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR (ring (2 * m + 1 + 1)).zero]
      show chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * (P m + -1 * 1) = _; lib_int
    · rw [sum_xxd m hpr _ (negnz m _ hx), delta_zero m _ _ hx]
      show chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * -1 = _; lib_int
  rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.mul (chi m (f m x)) (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * (-1 + P m * delta LibInt.intR x (ring (2 * m + 1 + 1)).zero))) (fun x => by rw [← mul_sumK, hin x])]
  rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.add (LibInt.intR.mul (-(chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))) (chi m (f m x))) (LibInt.intR.mul (P m * chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (LibInt.intR.mul (delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) (chi m (f m x))))) (fun x => by
    show chi m (f m x) * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * (-1 + P m * delta LibInt.intR x (ring (2 * m + 1 + 1)).zero)) = -(chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * chi m (f m x) + P m * chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * (delta LibInt.intR x (ring (2 * m + 1 + 1)).zero * chi m (f m x))
    generalize chi m (f m x) = a; generalize chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = e; generalize delta LibInt.intR x (ring (2 * m + 1 + 1)).zero = d; lib_int)]
  rw [sumK_add, ← mul_sumK, ← mul_sumK, sum_f m hpr, FiniteRecChain.pick LibInt.intR (L m) (QuadDiagonal.hcomp m) (fun x => chi m (f m x)) (ring (2 * m + 1 + 1)).zero,
    show f m (ring (2 * m + 1 + 1)).zero = (ring (2 * m + 1 + 1)).zero by unfold f; lib_ring, chi_zero]
  show -(chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * -1 + P m * chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * 0 = _
  generalize chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = e; lib_int

theorem pt1 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (l x y : Zp (2 * m + 1 + 1)) :
    chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))) * chi m ((ring (2 * m + 1 + 1)).mul (f m y) ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add l (ring (2 * m + 1 + 1)).one))))
      = (chi m (f m x) * chi m (f m y)) * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))))) := by
  rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)) ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg ((ri  -- (+68 chars; full line in the Zenodo source)
    chi_mul m hpr (f m x), chi_mul m hpr (f m y), chi_mul m hpr ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))]
  generalize chi m (f m x) = a; generalize chi m (f m y) = b
  generalize chi m ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)) = c; generalize chi m ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add l (ring (2 * m + 1 + 1)).one))) = d
  lib_int

theorem delta_shift1 (x y : Zp (2 * m + 1 + 1)) : delta LibInt.intR ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) x = delta LibInt.intR y ((ring (2 * m + 1 + 1)).add x (ring (2 * m + 1 + 1)).one) := by
  by_cases h : y = (ring (2 * m + 1 + 1)).add x (ring (2 * m + 1 + 1)).one
  · rw [delta_one m _ _ h, delta_one m _ _ (by rw [h]; lib_ring)]
  · rw [delta_zero m _ _ h, delta_zero m _ _ (fun e => h (by
      calc y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
        _ = _ := by rw [e]))]

theorem ff1 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x : Zp (2 * m + 1 + 1)) :
    chi m (f m x) * chi m (f m ((ring (2 * m + 1 + 1)).add x (ring (2 * m + 1 + 1)).one)) = chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).zero * chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by
  rw [← chi_mul m hpr, show (ring (2 * m + 1 + 1)).mul (f m x) (f m ((ring (2 * m + 1 + 1)).add x (ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) by unfold f; lib_ring,
    chi_mul m hpr, sq_delta m hpr]
  by_cases hx : x = (ring (2 * m + 1 + 1)).zero
  · rw [hx, delta_one m _ _ rfl, show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one by lib_ring]
    generalize chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = e; lib_int
  · rw [delta_zero m _ _ hx]
    generalize chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) = e; lib_int

/-- **Σ_λ a(λ)a(λ + 1) = −1 − p(1 + χ(−1)).** -/
theorem adjacent (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : sumK LibInt.intR (fun l => A m l * A m ((ring (2 * m + 1 + 1)).add l (ring (2 * m + 1 + 1)).one)) (L m) = -1 + -(P m * (1 + chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))) := by
  have hexp : ∀ l, A m l * A m ((ring (2 * m + 1 + 1)).add l (ring (2 * m + 1 + 1)).one) = sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => (chi m (f m x) * chi m (f m y)) * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg (ring (2   -- (+51 chars; full line in the Zenodo source)
    unfold A
    show LibInt.intR.mul _ _ = _
    rw [sumK_mul]
    exact sumK_ext LibInt.intR _ _ (fun x => by
      rw [mul_sumK]
      exact sumK_ext LibInt.intR _ _ (fun y => pt1 m hpr l x y) _) _
  rw [sumK_ext LibInt.intR _ _ hexp]
  rw [← sumK_swap LibInt.intR (fun x l => sumK LibInt.intR (fun y => (chi m (f m x) * chi m (f m y)) * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)))))) (L m))]
  rw [sumK_ext LibInt.intR _ (fun x => sumK LibInt.intR (fun y => LibInt.intR.add (LibInt.intR.mul (-(chi m (f m x))) (chi m (f m y))) (LibInt.intR.mul (P m * chi m (f m x)) (LibInt.intR.mul (delta LibInt.intR y ((ring (2 * m + 1 + 1)).add x (ring (2 * m + 1 + 1)).one)) (chi m (f m y))))) (L m)) (fun x => by
    rw [← sumK_swap LibInt.intR (fun y l => (chi m (f m x) * chi m (f m y)) * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))))))]
    exact sumK_ext LibInt.intR _ _ (fun y => by
      rw [show (fun l => (chi m (f m x) * chi m (f m y)) * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)))))) = (fun l => LibInt.intR.mul (chi m (f m x) * chi m (f m y)) (chi m ((ring (2 * m + 1 + 1)).mul (  -- (+220 chars; full line in the Zenodo source)
        ← mul_sumK, inner m hpr, neg_delta, delta_shift1]
      show (chi m (f m x) * chi m (f m y)) * (-1 + P m * delta LibInt.intR y ((ring (2 * m + 1 + 1)).add x (ring (2 * m + 1 + 1)).one)) = -(chi m (f m x)) * chi m (f m y) + P m * chi m (f m x) * (delta LibInt.intR y ((ring (2 * m + 1 + 1)).add x (ring (2 * m + 1 + 1)).one) * chi m (f m y))
      generalize chi m (f m x) = a; generalize chi m (f m y) = b; generalize delta LibInt.intR y ((ring (2 * m + 1 + 1)).add x (ring (2 * m + 1 + 1)).one) = d; lib_int) _)]
  rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.add (LibInt.intR.mul (-1) (LibInt.intR.mul (chi m (f m x)) (-1))) (LibInt.intR.mul (P m) (chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).zero * chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))))) (fun x => by
    rw [sumK_add, ← mul_sumK, ← mul_sumK, FiniteRecChain.pick LibInt.intR (L m) (QuadDiagonal.hcomp m) (fun y => chi m (f m y)) _, sum_f m hpr, ← ff1 m hpr x]
    show -(chi m (f m x)) * -1 + P m * chi m (f m x) * chi m (f m ((ring (2 * m + 1 + 1)).add x (ring (2 * m + 1 + 1)).one)) = -1 * (chi m (f m x) * -1) + P m * (chi m (f m x) * chi m (f m ((ring (2 * m + 1 + 1)).add x (ring (2 * m + 1 + 1)).one)))
    generalize chi m (f m x) = a; generalize chi m (f m ((ring (2 * m + 1 + 1)).add x (ring (2 * m + 1 + 1)).one)) = b; lib_int)]
  rw [sumK_add, ← mul_sumK, ← mul_sumK, ← sumK_mul, sum_f m hpr]
  rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.add (chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))) (LibInt.intR.mul (-(chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))) (delta LibInt.intR x (ring (2 * m + 1 + 1)).zero))) (fun x => by
    show _ = _ + -(chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * _; generalize chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) = u; generalize delta LibInt.intR x (ring (2 * m + 1 + 1)).zero = d; generalize chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = e; lib_int),
    sumK_add, ← mul_sumK, sum_y2c m hpr _ (negnz m _ (PowerSum.one_ne_zero' (2 * m + 1))),
    show sumK LibInt.intR (fun x => delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR (ring (2 * m + 1 + 1)).zero]
  show -1 * (-1 * -1) + P m * (-1 + -(chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * 1) = _
  generalize chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = e; lib_int

/-- **Odd moments vanish at p ≡ 3 (mod 4)**: χ(−1) = −1 ⇒ Σ_λ a(λ)·(a(λ)²)^k = 0. -/
theorem odd_moments (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (hm : chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = -1) (k : Nat) :
    sumK LibInt.intR (fun l => A m l * PencilMoment.ipow (A m l * A m l) k) (L m) = 0 := by
  have hneg : sumK LibInt.intR (fun l => A m l * PencilMoment.ipow (A m l * A m l) k) (L m)
      = LibInt.intR.mul (-1) (sumK LibInt.intR (fun l => A m l * PencilMoment.ipow (A m l * A m l) k) (L m)) := by
    calc _ = sumK LibInt.intR (fun l => A m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg l)) * PencilMoment.ipow (A m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg l)) * A m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg l))) k) (L m) :=
          (FiniteRecChain.reindex LibInt.intR (L m) (QuadDiagonal.hcomp m) (fun x => (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x)) (fun x => (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x))
            (fun x => by lib_ring) (fun y => by lib_ring) (fun l => A m l * PencilMoment.ipow (A m l * A m l) k)).symm
      _ = _ := by
        rw [mul_sumK]
        exact sumK_ext LibInt.intR _ _ (fun l => by
          rw [refl_one m hpr l, hm, show (-1 * A m l) * (-1 * A m l) = A m l * A m l by generalize A m l = a; lib_int]
          show -1 * A m l * PencilMoment.ipow (A m l * A m l) k = -1 * (A m l * PencilMoment.ipow (A m l * A m l) k)
          generalize PencilMoment.ipow (A m l * A m l) k = q; generalize A m l = a; lib_int) _
  exact JacobiNorm.int_cancel _ _ 1 (by
    have e : ∀ X : Int, X = LibInt.intR.mul (-1) X → X * Int.ofNat (1 + 1) = 0 * Int.ofNat (1 + 1) := fun X hX => by
      calc X * Int.ofNat (1 + 1) = X + X := by show X * 2 = X + X; lib_int
        _ = X + -1 * X := by
          have : X = -1 * X := hX
          rw [← this]
        _ = 0 * Int.ofNat (1 + 1) := by show X + -1 * X = 0 * 2; lib_int
    exact e _ hneg)

/-- **Second moment of any family y² = g(x)(x − λ)**: Σ_λ (Σ_x χ(g(x)(x − λ)))² = −(Σ_x χ(g x))² + p Σ_x χ(g x)². -/
theorem family_moment (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (g : Zp (2 * m + 1 + 1) → Zp (2 * m + 1 + 1)) :
    sumK LibInt.intR (fun l => sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).mul (g x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)))) (L m) * sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).mul (g x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)))) (L m)) (L m)
      = -(sumK LibInt.intR (fun x => chi m (g x)) (L m) * sumK LibInt.intR (fun x => chi m (g x)) (L m)) + P m * sumK LibInt.intR (fun x => chi m (g x) * chi m (g x)) (L m) := by
  have hpt : ∀ l x y, chi m ((ring (2 * m + 1 + 1)).mul (g x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))) * chi m ((ring (2 * m + 1 + 1)).mul (g y) ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg l)))
      = (chi m (g x) * chi m (g y)) * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg y))) := fun l x y => by
    rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg y)) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)) ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg l)) by lib_ring,
      chi_mul m hpr (g x), chi_mul m hpr (g y), chi_mul m hpr ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))]
    generalize chi m (g x) = a; generalize chi m (g y) = b
    generalize chi m ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)) = c; generalize chi m ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg l)) = d
    lib_int
  rw [sumK_ext LibInt.intR _ (fun l => sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => (chi m (g x) * chi m (g y)) * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg y)))) (L m)) (L m)) (fun l => by
    show LibInt.intR.mul _ _ = _
    rw [sumK_mul]
    exact sumK_ext LibInt.intR _ _ (fun x => by
      rw [mul_sumK]
      exact sumK_ext LibInt.intR _ _ (fun y => hpt l x y) _) _)]
  rw [← sumK_swap LibInt.intR (fun x l => sumK LibInt.intR (fun y => (chi m (g x) * chi m (g y)) * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg y)))) (L m))]
  rw [sumK_ext LibInt.intR _ (fun x => sumK LibInt.intR (fun y => LibInt.intR.add (LibInt.intR.mul (-(chi m (g x))) (chi m (g y))) (LibInt.intR.mul (P m * chi m (g x)) (LibInt.intR.mul (delta LibInt.intR y x) (chi m (g y))))) (L m)) (fun x => by
    rw [← sumK_swap LibInt.intR (fun y l => (chi m (g x) * chi m (g y)) * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg y))))]
    exact sumK_ext LibInt.intR _ _ (fun y => by
      rw [show (fun l => (chi m (g x) * chi m (g y)) * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg y))))
          = (fun l => LibInt.intR.mul (chi m (g x) * chi m (g y)) (chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg y))))) from rfl,
        ← mul_sumK, inner m hpr, neg_delta]
      show (chi m (g x) * chi m (g y)) * (-1 + P m * delta LibInt.intR y x) = -(chi m (g x)) * chi m (g y) + P m * chi m (g x) * (delta LibInt.intR y x * chi m (g y))
      generalize chi m (g x) = a; generalize chi m (g y) = b; generalize delta LibInt.intR y x = d; lib_int) _)]
  rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.add (LibInt.intR.mul (chi m (g x)) (-(sumK LibInt.intR (fun y => chi m (g y)) (L m)))) (LibInt.intR.mul (P m) (chi m (g x) * chi m (g x)))) (fun x => by
    rw [sumK_add, ← mul_sumK, ← mul_sumK, FiniteRecChain.pick LibInt.intR (L m) (QuadDiagonal.hcomp m) (fun y => chi m (g y)) x]
    show -(chi m (g x)) * sumK LibInt.intR (fun y => chi m (g y)) (L m) + P m * chi m (g x) * chi m (g x) = _
    generalize sumK LibInt.intR (fun y => chi m (g y)) (L m) = S; generalize chi m (g x) = a; show _ = a * -S + P m * (a * a); lib_int)]
  rw [sumK_add, ← sumK_mul, ← mul_sumK]
  show sumK LibInt.intR (fun x => chi m (g x)) (L m) * -(sumK LibInt.intR (fun y => chi m (g y)) (L m)) + P m * sumK LibInt.intR (fun x => chi m (g x) * chi m (g x)) (L m) = _
  generalize sumK LibInt.intR (fun x => chi m (g x)) (L m) = S
  generalize sumK LibInt.intR (fun x => chi m (g x) * chi m (g x)) (L m) = T
  lib_int

/-- **#{x : ax² + bx + c = 0} = 1 + χ(b² − 4ac)** for a ≠ 0. -/
theorem root_count (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b c : Zp (2 * m + 1 + 1)) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) :
    sumK LibInt.intR (fun x => delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b x) c)) (ring (2 * m + 1 + 1)).zero) (L m) = 1 + chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b b) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((  -- (+206 chars; full line in the Zenodo source)
  have hpt : ∀ x, delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b x) c)) (ring (2 * m + 1 + 1)).zero = delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m  -- (+709 chars; full line in the Zenodo source)
    rw [scale_q m hpr a b c x ha, quad_shift m hpr ((ring (2 * m + 1 + 1)).mul (iv m a) b) ((ring (2 * m + 1 + 1)).mul (iv m a) c) x]
    by_cases hz : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (iv m a) b))) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1  -- (+469 chars; full line in the Zenodo source)
    · rw [delta_one m _ _ hz, delta_one m _ _ (by rw [hz]; lib_ring)]
    · rw [delta_zero m _ _ hz, delta_zero m _ _ (nzm m hpr _ _ ha (fun e => hz (by
          calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (iv m a) b))) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1  -- (+1017 chars; full line in the Zenodo source)
            _ = _ := by rw [e]; lib_ring)))]
  rw [sumK_ext LibInt.intR _ _ hpt, shift_sum m (fun z => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (iv m a) b)) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m +  -- (+343 chars; full line in the Zenodo source)
  have h4 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ≠ (ring (2 * m + 1 + 1)).zero := nzm m hpr _ _ (QuadDiagonal.two_ne m) (QuadDiagonal.two_ne m)
  have hia := iv_nz m hpr a ha
  have e1 : chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m +  -- (+745 chars; full line in the Zenodo source)
    rw [chi_mul m hpr, chi_sq m hpr _ (QuadDiagonal.two_ne m)]; lib_int
  have e2 : chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m a) (iv m a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b b) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).o  -- (+403 chars; full line in the Zenodo source)
    rw [chi_mul m hpr, chi_sq m hpr _ hia]; lib_int
  rw [← e1, disc4 m hpr ((ring (2 * m + 1 + 1)).mul (iv m a) b) ((ring (2 * m + 1 + 1)).mul (iv m a) c), disc_scale m hpr a b c ha, e2]

/-- **Every quadratic family**: for a ≠ 0, b² ≠ 4ac, Σ_λ (Σ_x χ((ax² + bx + c)(x − λ)))² = −1 + p(p − 1 − χ(b² − 4ac)). -/
theorem quad_family (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b c : Zp (2 * m + 1 + 1)) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) (hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b b) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2  -- (+142 chars; full line in the Zenodo source)
    sumK LibInt.intR (fun l => sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b x) c)) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)))) (L m) * sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1   -- (+215 chars; full line in the Zenodo source)
      = -1 + P m * (P m + -1 + -chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b b) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul a c))))) := by
  rw [family_moment m hpr (fun x => (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b x) c)), quad_sum_a m hpr a b c ha hD]
  rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.add (LibInt.intR.mul 1 1) (LibInt.intR.mul (-1) (delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b x) c)) (ring (2 * m + 1 + 1)).zero))) (fun x => by
    rw [QuadDiagonal.sqchi m hpr]; show _ = 1 * 1 + -1 * _; generalize delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b x) c)) (ring (2 * m + 1 + 1)).zero = u; lib_int),
    sumK_add, ← mul_sumK, ← mul_sumK, sum_one, root_count m hpr a b c ha]
  have hca : chi m a * chi m a = 1 := chi_unit m hpr a ha
  show -(-chi m a * -chi m a) + P m * (1 * P m + -1 * (1 + chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b b) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1))  -- (+18 chars; full line in the Zenodo source)
  rw [show -chi m a * -chi m a = chi m a * chi m a by generalize chi m a = t; lib_int, hca]
  generalize chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b b) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul a c)))) = e; lib_int

/-- **Second moment of any translation family y² = h(x) + λ**: Σ_λ (Σ_x χ(h(x) + λ))² = −p² + p·#{(x, y) : h(x) = h(y)}. -/
theorem translation_moment (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h : Zp (2 * m + 1 + 1) → Zp (2 * m + 1 + 1)) :
    sumK LibInt.intR (fun l => sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).add l (h x))) (L m) * sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).add l (h x))) (L m)) (L m)
      = -(P m * P m) + P m * sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR (h x) (h y)) (L m)) (L m) := by
  rw [sumK_ext LibInt.intR _ (fun l => sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l (h x)) ((ring (2 * m + 1 + 1)).add l (h y)))) (L m)) (L m)) (fun l => by
    show LibInt.intR.mul _ _ = _
    rw [sumK_mul]
    exact sumK_ext LibInt.intR _ _ (fun x => by
      rw [mul_sumK]
      exact sumK_ext LibInt.intR _ _ (fun y => (chi_mul m hpr _ _).symm) _) _)]
  rw [← sumK_swap LibInt.intR (fun x l => sumK LibInt.intR (fun y => chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l (h x)) ((ring (2 * m + 1 + 1)).add l (h y)))) (L m))]
  rw [sumK_ext LibInt.intR _ (fun x => sumK LibInt.intR (fun y => -1 + P m * delta LibInt.intR (h x) (h y)) (L m)) (fun x => by
    rw [← sumK_swap LibInt.intR (fun y l => chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l (h x)) ((ring (2 * m + 1 + 1)).add l (h y))))]
    exact sumK_ext LibInt.intR _ _ (fun y => inner m hpr _ _) _)]
  rw [sumK_ext LibInt.intR _ _ (fun x => PencilMoment.sum_lin m (-1) (P m) _), PencilMoment.sum_lin]
  lib_int

/-- **#{(x, y) : x³ = y³} = p + (p − 1)(1 + χ(−3))** for p > 3 — read off from two second moments (translation_moment, pencil_zero_all):
    the number of cube roots of unity is 2 + χ(−3). -/
theorem cube_pairs (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h3 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ≠ (ring (2 * m + 1 + 1)).zero) :
    sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul y y))) (L m)) (L m)
      = P m + (P m + -1) * (1 + chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)))) := by
  have ht := translation_moment m hpr (fun x => (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x))
  have hz := pencil_zero_all m hpr h3
  have hW : ∀ b, W m (ring (2 * m + 1 + 1)).zero b = sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).add b ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)))) (L m) := fun b =>
    sumK_ext LibInt.intR _ _ (fun x => by
      rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero x) b) = (ring (2 * m + 1 + 1)).add b ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) by lib_ring]) _
  rw [sumK_ext LibInt.intR _ _ (fun b => by rw [hW b])] at hz
  rw [hz] at ht
  generalize sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul y y))) (L m)) (L m) = N at ht ⊢
  have hP : P m = Int.ofNat (2 * m + 1 + 1 + 1) := rfl
  apply JacobiNorm.int_cancel _ _ (2 * m + 1 + 1)
  rw [← hP]
  calc N * P m = (P m * P m + -(P m)) * (1 + chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)))) + P m * P m := by rw [ht]; lib_int
    _ = _ := by lib_int

theorem row (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x : Zp (2 * m + 1 + 1)) :
    sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul y y))) (L m) = delta LibInt.intR x (ring (2 * m + 1 + 1)).zero + (1 + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).zero)) * sumK LibInt.intR (fun z => delta LibInt.intR (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul z ((r  -- (+43 chars; full line in the Zenodo source)
  by_cases hx : x = (ring (2 * m + 1 + 1)).zero
  · rw [hx, delta_one m _ _ rfl]
    rw [sumK_ext LibInt.intR _ (fun y => delta LibInt.intR y (ring (2 * m + 1 + 1)).zero) (fun y => by
      by_cases hy : y = (ring (2 * m + 1 + 1)).zero
      · rw [hy, delta_one m _ _ (by lib_ring), delta_one m _ _ rfl]
      · rw [delta_zero m _ _ hy, delta_zero m _ _ (fun e => hy (by
          cases nz2 m hpr _ _ e.symm with
          | inl h => exact h
          | inr h => cases nz2 m hpr _ _ h with
            | inl h' => exact h'
            | inr h' => exact h')) ])]
    rw [show sumK LibInt.intR (fun y => delta LibInt.intR y (ring (2 * m + 1 + 1)).zero) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR (ring (2 * m + 1 + 1)).zero]
    generalize sumK LibInt.intR (fun z => delta LibInt.intR (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) (L m) = r; lib_int
  · rw [delta_zero m _ _ hx]
    have hx3 : (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x) ≠ (ring (2 * m + 1 + 1)).zero := nzm m hpr _ _ hx (nzm m hpr _ _ hx hx)
    refine Eq.trans (reindex_mul m hpr x hx (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul y y)))).symm ?_
    rw [sumK_ext LibInt.intR _ (fun z => delta LibInt.intR (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) (fun z => by
      show delta LibInt.intR ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x z) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x z) ((ring (2 * m + 1 + 1)).mul x z))) = _
      by_cases hz : (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)
      · rw [delta_one m _ _ hz, delta_one m _ _ (by
          calc (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) (ring (2 * m + 1 + 1)).one := by lib_ring
            _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) := by rw [hz]
            _ = _ := by lib_ring)]
      · rw [delta_zero m _ _ hz, delta_zero m _ _ (fun e => hz (by
          have e2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)))) = (ring (2 * m + 1 + 1)).zero := by
            calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x z) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x z) ((ring (2 * m + 1 + 1)).mul x z)))) := by lib_ring
              _ = (ring (2 * m + 1 + 1)).zero := by rw [e]; lib_ring
          cases nz2 m hpr _ _ e2 with
          | inl h => exact absurd h hx3
          | inr h => calc (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)))) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) := by lib_ring
              _ = _ := by rw [h]; lib_ring))])]
    generalize sumK LibInt.intR (fun z => delta LibInt.intR (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) (L m) = r; lib_int

/-- **𝔽_p has exactly 2 + χ(−3) cube roots of unity** (p > 3). -/
theorem cube_roots (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h3 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ≠ (ring (2 * m + 1 + 1)).zero) : sumK LibInt.intR (fun z => delta LibInt.intR (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z  -- (+195 chars; full line in the Zenodo source)
  have hc := cube_pairs m hpr h3
  rw [sumK_ext LibInt.intR _ _ (row m hpr)] at hc
  rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.add (LibInt.intR.mul (sumK LibInt.intR (fun z => delta LibInt.intR (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) (L m)) 1) (LibInt.intR.mul (1 + -(sumK LibInt.intR (fun z => delta LibInt.intR (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) (L m))) (delta LibI  -- (+53 chars; full line in the Zenodo source)
    show _ = sumK LibInt.intR (fun z => delta LibInt.intR (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) (L m) * 1 + (1 + -(sumK LibInt.intR (fun z => delta LibInt.intR (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) (L m))) * delta LibInt.intR x (ring (2 * m + 1 + 1)).zero; generalize sumK LibInt.intR (fun z   -- (+197 chars; full line in the Zenodo source)
    sumK_add, ← mul_sumK, ← mul_sumK, sum_one, show sumK LibInt.intR (fun x => delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR (ring (2 * m + 1 + 1)).zero] at hc
  have hc' : sumK LibInt.intR (fun z => delta LibInt.intR (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) (L m) * P m + (1 + -(sumK LibInt.intR (fun z => delta LibInt.intR (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) (L m))) * 1 = P m + (P m + -1) * (1 + chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 +   -- (+126 chars; full line in the Zenodo source)
  generalize sumK LibInt.intR (fun z => delta LibInt.intR (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) (L m) = r at hc' ⊢
  generalize chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) = e at hc' ⊢
  have hP : P m + -1 = Int.ofNat (2 * m + 1 + 1) := by show Int.ofNat (2 * m + 1 + 1) + 1 + -1 = Int.ofNat (2 * m + 1 + 1); generalize Int.ofNat (2 * m + 1 + 1) = q; lib_int
  apply JacobiNorm.int_cancel _ _ (2 * m + 1)
  rw [← hP]
  calc r * (P m + -1) = (r * P m + (1 + -r) * 1) + -1 := by lib_int
    _ = (P m + (P m + -1) * (1 + e)) + -1 := by rw [hc']
    _ = _ := by lib_int

/-- **𝔽_p has exactly 3 + χ(−1) fourth roots of unity.** -/
theorem fourth_roots (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : sumK LibInt.intR (fun z => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z)) (ring (2 * m + 1 + 1)).one) (L m) = 3 + chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by
  have h1 : ((ring (2 * m + 1 + 1)).one : Zp (2 * m + 1 + 1)) ≠ (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one := fun e => QuadDiagonal.two_ne m (by
    calc (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by rw [← e]
      _ = (ring (2 * m + 1 + 1)).zero := by lib_ring)
  have hpt : ∀ z, delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z)) (ring (2 * m + 1 + 1)).one = delta LibInt.intR ((ring (2 * m + 1 + 1)).mul z z) (ring (2 * m + 1 + 1)).one + delta LibInt.intR ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := fun z => by
    by_cases ha : (ring (2 * m + 1 + 1)).mul z z = (ring (2 * m + 1 + 1)).one
    · rw [ha, delta_one m _ _ (by lib_ring), delta_one m _ _ rfl, delta_zero m _ _ h1]; rfl
    · by_cases hb : (ring (2 * m + 1 + 1)).mul z z = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one
      · rw [hb, delta_one m _ _ (by lib_ring), delta_zero m _ _ (fun e => h1 e.symm), delta_one m _ _ rfl]; rfl
      · rw [delta_zero m _ _ ha, delta_zero m _ _ hb, delta_zero m _ _ (fun e => by
          have e2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z z) (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by
            calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
              _ = (ring (2 * m + 1 + 1)).zero := by rw [e]; lib_ring
          cases nz2 m hpr _ _ e2 with
          | inl h => exact ha (by
              calc (ring (2 * m + 1 + 1)).mul z z = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
                _ = _ := by rw [h]; lib_ring)
          | inr h => exact hb (by
              calc (ring (2 * m + 1 + 1)).mul z z = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z z) (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
                _ = _ := by rw [h]; lib_ring))]; rfl
  rw [sumK_ext LibInt.intR _ _ hpt, QuadDiagonal.sI_add, QuadDiagonal.sq_count m hpr, QuadDiagonal.sq_count m hpr, chi_one m]
  generalize chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = e; lib_int

/-- **𝔽_p has exactly 4 + 2χ(−3) sixth roots of unity** (p > 3). -/
theorem sixth_roots (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h3 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ≠ (ring (2 * m + 1 + 1)).zero) : sumK LibInt.intR (fun z => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z  -- (+293 chars; full line in the Zenodo source)
  have h1 : ((ring (2 * m + 1 + 1)).one : Zp (2 * m + 1 + 1)) ≠ (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one := fun e => QuadDiagonal.two_ne m (by
    calc (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by rw [← e]
      _ = (ring (2 * m + 1 + 1)).zero := by lib_ring)
  have hpt : ∀ z, delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) (ring (2 * m + 1 + 1)).one = delta LibInt.intR (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) + delta LibInt.intR (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)  -- (+85 chars; full line in the Zenodo source)
    by_cases ha : (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)
    · rw [← ha, delta_one m _ _ (by lib_ring), delta_one m _ _ rfl, delta_zero m _ _ h1]; rfl
    · by_cases hb : (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))
      · rw [delta_one m _ _ (by
            calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) := by lib_rin  -- (+1 chars; full line in the Zenodo source)
              _ = (ring (2 * m + 1 + 1)).one := by rw [← hb]; lib_ring), delta_zero m _ _ ha, delta_one m _ _ hb]; rfl
      · rw [delta_zero m _ _ ha, delta_zero m _ _ hb, delta_zero m _ _ (fun e => by
          have e2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by
            calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
              _ = (ring (2 * m + 1 + 1)).zero := by rw [e]; lib_ring
          cases nz2 m hpr _ _ e2 with
          | inl h => exact ha (by
              calc (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by rw [h]; lib_ring
                _ = _ := by lib_ring)
          | inr h => exact hb (by
              calc (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) (ring (2 * m + 1 + 1)).one)) := by rw [h]; lib_ring
                _ = _ := by lib_ring))]; rfl
  rw [sumK_ext LibInt.intR _ _ hpt, QuadDiagonal.sI_add, cube_roots m hpr h3]
  have hneg : sumK LibInt.intR (fun z => delta LibInt.intR (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)))) (L m) = sumK LibInt.intR (fun z => delta LibInt.intR (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) (L m) := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR (L m) (QuadDiagonal.hcomp m) (fun x => (ring (2 * m + 1 + 1)).neg x) (fun x => (ring (2 * m + 1 + 1)).neg x)
      (fun x => by lib_ring) (fun y => by lib_ring) (fun z => delta LibInt.intR (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))))
    exact sumK_ext LibInt.intR _ _ (fun z => by
      show _ = delta LibInt.intR (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg z) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg z) ((ring (2 * m + 1 + 1)).neg z)))
      rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg z) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg z) ((ring (2 * m + 1 + 1)).neg z)) = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) by lib_ring]) _
  rw [hneg, cube_roots m hpr h3]
  generalize chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) = e; lib_int

theorem int_m1_ne : (-1 : Int) ≠ 1 := fun h => Int.noConfusion h

/-- **#{z : z⁴ = −1} = (1 + χ(−1))(1 + χ(2))** (four solutions exactly when p ≡ 1 mod 8). -/
theorem quartic_m1 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : sumK LibInt.intR (fun z => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (L m) = (1 + chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (1 + chi m ((ring (2 * m + 1 + 1))  -- (+66 chars; full line in the Zenodo source)
  have hm10 : ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one : Zp (2 * m + 1 + 1)) ≠ (ring (2 * m + 1 + 1)).zero := negnz m _ (PowerSum.one_ne_zero' (2 * m + 1))
  cases QuadDiagonal.e_val m hpr with
  | inr hneg =>
    rw [hneg, sumK_ext LibInt.intR _ (fun _ => (0 : Int)) (fun z => delta_zero m _ _ (fun e => by
      have hz : (ring (2 * m + 1 + 1)).mul z z ≠ (ring (2 * m + 1 + 1)).zero := fun h0 => hm10 (by rw [← e, h0]; lib_ring)
      have := chi_sq m hpr _ hz
      rw [e, hneg] at this
      exact int_m1_ne this)), QuadDiagonal.zsum]
    generalize chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) = t; lib_int
  | inl hpos =>
    have hpw : Frobenius.pw (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) (m + 1) = (ring (2 * m + 1 + 1)).one := by
      by_cases h : Frobenius.pw (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) (m + 1) = (ring (2 * m + 1 + 1)).one
      · exact h
      · exact absurd (hpos.symm.trans (chi_neg_of m _ hm10 h)) (fun h' => int_m1_ne h'.symm)
    match QuadDiagonal.sq_root m hpr _ hm10 hpw with
    | ⟨i, hi⟩ =>
      have hi0 : i ≠ (ring (2 * m + 1 + 1)).zero := fun h0 => hm10 (by rw [← hi, h0]; lib_ring)
      have hii : i ≠ (ring (2 * m + 1 + 1)).neg i := fun h => hi0 (by
        cases nz2 m hpr ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) i (by
          calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) i = (ring (2 * m + 1 + 1)).add i ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).neg i)) := by lib_ring
            _ = (ring (2 * m + 1 + 1)).zero := by rw [← h]; lib_ring) with
        | inl h2 => exact absurd h2 (QuadDiagonal.two_ne m)
        | inr h2 => exact h2)
      have hpt : ∀ z, delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = delta LibInt.intR ((ring (2 * m + 1 + 1)).mul z z) i + delta LibInt.intR ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).neg i) := fun z => by
        by_cases ha : (ring (2 * m + 1 + 1)).mul z z = i
        · rw [ha, delta_one m _ _ (by rw [hi]), delta_one m _ _ rfl, delta_zero m _ _ hii]; rfl
        · by_cases hb : (ring (2 * m + 1 + 1)).mul z z = (ring (2 * m + 1 + 1)).neg i
          · rw [hb, delta_one m _ _ (by rw [← hi]; lib_ring), delta_zero m _ _ (fun e => hii e.symm), delta_one m _ _ rfl]; rfl
          · rw [delta_zero m _ _ ha, delta_zero m _ _ hb, delta_zero m _ _ (fun e => by
              have e2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).neg i)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z z) i) = (ring (2 * m + 1 + 1)).zero := by
                calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul i i)) := by lib_ring
                  _ = (ring (2 * m + 1 + 1)).zero := by rw [e, hi]; lib_ring
              cases nz2 m hpr _ _ e2 with
              | inl h => exact ha (by
                  calc (ring (2 * m + 1 + 1)).mul z z = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).neg i)) i := by lib_ring
                    _ = _ := by rw [h]; lib_ring)
              | inr h => exact hb (by
                  calc (ring (2 * m + 1 + 1)).mul z z = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z z) i) ((ring (2 * m + 1 + 1)).neg i) := by lib_ring
                    _ = _ := by rw [h]; lib_ring))]; rfl
      rw [sumK_ext LibInt.intR _ _ hpt, QuadDiagonal.sI_add, QuadDiagonal.sq_count m hpr, QuadDiagonal.sq_count m hpr,
        show (ring (2 * m + 1 + 1)).neg i = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) i by lib_ring, chi_mul m hpr, hpos]
      -- χ(i) = χ(2): (1 + i)² = 2i
      have h1i : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one i ≠ (ring (2 * m + 1 + 1)).zero := fun h => ne_pm m (by
        calc (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).mul i i := hi.symm
          _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by
            rw [show i = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one i) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) by lib_ring, h]; lib_ring
          _ = (ring (2 * m + 1 + 1)).one := by lib_ring)
      have hsq : chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) * chi m i = 1 := by
        rw [← chi_mul m hpr, show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) i = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one i) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one i) by
          calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) i = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add i i)) ((ring (2 * m + 1 + 1)).mul i i) := by
                rw [hi]; lib_ring
            _ = _ := by lib_ring]
        exact chi_sq m hpr _ h1i
      have h22 : chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) * chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) = 1 := chi_unit m hpr _ (QuadDiagonal.two_ne m)
      have hci : chi m i = chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) := by
        calc chi m i = (chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) * chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) * chi m i := by rw [h22]; lib_int
          _ = chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) * (chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) * chi m i) := by lib_int
          _ = chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) := by rw [hsq]; lib_int
      rw [hci]
      generalize chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) = t; lib_int

/-- **𝔽_p has exactly 3 + χ(−1) + (1 + χ(−1))(1 + χ(2)) eighth roots of unity.** -/
theorem eighth_roots (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : sumK LibInt.intR (fun z => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z))) (ring (2 * m + 1 + 1)).one) (L m) = 3 + chi  -- (+231 chars; full line in the Zenodo source)
  have h1 : ((ring (2 * m + 1 + 1)).one : Zp (2 * m + 1 + 1)) ≠ (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one := fun e => QuadDiagonal.two_ne m (by
    calc (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by rw [← e]
      _ = (ring (2 * m + 1 + 1)).zero := by lib_ring)
  have hpt : ∀ z, delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z))) (ring (2 * m + 1 + 1)).one = delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z)) (ri  -- (+209 chars; full line in the Zenodo source)
    by_cases ha : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z) = (ring (2 * m + 1 + 1)).one
    · rw [ha, delta_one m _ _ (by lib_ring), delta_one m _ _ rfl, delta_zero m _ _ h1]; rfl
    · by_cases hb : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z) = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one
      · rw [hb, delta_one m _ _ (by lib_ring), delta_zero m _ _ (fun e => h1 e.symm), delta_one m _ _ rfl]; rfl
      · rw [delta_zero m _ _ ha, delta_zero m _ _ hb, delta_zero m _ _ (fun e => by
          have e2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z)) (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1  -- (+13 chars; full line in the Zenodo source)
            calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
              _ = (ring (2 * m + 1 + 1)).zero := by rw [e]; lib_ring
          cases nz2 m hpr _ _ e2 with
          | inl h => exact ha (by
              calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
                _ = _ := by rw [h]; lib_ring)
          | inr h => exact hb (by
              calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z)) (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
                _ = _ := by rw [h]; lib_ring))]; rfl
  rw [sumK_ext LibInt.intR _ _ hpt, QuadDiagonal.sI_add, fourth_roots m hpr, quartic_m1 m hpr]

theorem cube_m1 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h3 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ≠ (ring (2 * m + 1 + 1)).zero) : sumK LibInt.intR (fun t => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)).mul t t)) ((ring (2 * m + 1 + 1)).neg  -- (+221 chars; full line in the Zenodo source)
  rw [← cube_roots m hpr h3]
  refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR (L m) (QuadDiagonal.hcomp m) (fun x => (ring (2 * m + 1 + 1)).neg x) (fun x => (ring (2 * m + 1 + 1)).neg x)
    (fun x => by lib_ring) (fun y => by lib_ring) (fun z => delta LibInt.intR (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))))
  exact sumK_ext LibInt.intR _ _ (fun t => by
    show _ = delta LibInt.intR (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg t) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg t) ((ring (2 * m + 1 + 1)).neg t)))
    by_cases h : (ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)).mul t t) = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one
    · rw [delta_one m _ _ h, delta_one m _ _ (by
        calc (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
          _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)).mul t t)) := by rw [h]
          _ = _ := by lib_ring)]
    · rw [delta_zero m _ _ h, delta_zero m _ _ (fun e => h (by
        calc (ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)).mul t t) = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg t) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg t) ((ring (2 * m + 1 + 1)).neg t))) := by lib_ring
          _ = _ := by rw [← e]))]) _

/-- **#{z : z⁶ = −1} = (1 + χ(−1))(2 + χ(−3))** (p > 3). -/
theorem sixth_m1 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h3 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ≠ (ring (2 * m + 1 + 1)).zero) :
    sumK LibInt.intR (fun z => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (L m) = (1 + chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (2 + chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 +   -- (+126 chars; full line in the Zenodo source)
  rw [sumK_ext LibInt.intR _ (fun z => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (fun z => by rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m +   -- (+219 chars; full line in the Zenodo source)
  rw [QuadDiagonal.conv m hpr (fun t => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)).mul t t)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))]
  rw [sumK_ext LibInt.intR _ (fun t => LibInt.intR.mul (1 + chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (delta LibInt.intR ((ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)).mul t t)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))) (fun t => by
    by_cases h : (ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)).mul t t) = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one
    · rw [delta_one m _ _ h, ← Jacobsthal.chi_cube m hpr t, h]; rfl
    · rw [delta_zero m _ _ h]; show (1 + chi m t) * 0 = (1 + chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * 0; generalize chi m t = a; generalize chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = b; lib_int)]
  rw [← mul_sumK, cube_m1 m hpr h3]; rfl


/-- **𝔽_p has exactly (2 + χ(−3))(3 + χ(−1)) twelfth roots of unity** (p > 3; = gcd(12, p − 1)). -/
theorem twelfth_roots (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h3 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ≠ (ring (2 * m + 1 + 1)).zero) :
    sumK LibInt.intR (fun z => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)))) (ring (2  -- (+283 chars; full line in the Zenodo source)
  have h1 : ((ring (2 * m + 1 + 1)).one : Zp (2 * m + 1 + 1)) ≠ (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one := fun e => QuadDiagonal.two_ne m (by
    calc (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by rw [← e]
      _ = (ring (2 * m + 1 + 1)).zero := by lib_ring)
  have hpt : ∀ z, delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)))) (ring (2 * m + 1 + 1)  -- (+457 chars; full line in the Zenodo source)
    by_cases ha : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) = (ring (2 * m + 1 + 1)).one
    · rw [ha, delta_one m _ _ (by lib_ring), delta_one m _ _ rfl, delta_zero m _ _ h1]; rfl
    · by_cases hb : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one
      · rw [hb, delta_one m _ _ (by lib_ring), delta_zero m _ _ (fun e => h1 e.symm), delta_one m _ _ rfl]; rfl
      · rw [delta_zero m _ _ ha, delta_zero m _ _ hb, delta_zero m _ _ (fun e => by
          have e2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).m  -- (+137 chars; full line in the Zenodo source)
            calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)))) ((ring (2  -- (+61 chars; full line in the Zenodo source)
              _ = (ring (2 * m + 1 + 1)).zero := by rw [e]; lib_ring
          cases nz2 m hpr _ _ e2 with
          | inl h => exact ha (by
              calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) ((ring (2 *   -- (+86 chars; full line in the Zenodo source)
                _ = _ := by rw [h]; lib_ring)
          | inr h => exact hb (by
              calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) (ring (2 * m  -- (+86 chars; full line in the Zenodo source)
                _ = _ := by rw [h]; lib_ring))]; rfl
  rw [sumK_ext LibInt.intR _ _ hpt, QuadDiagonal.sI_add, sixth_m1 m hpr h3]
  rw [show sumK LibInt.intR (fun z => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) (ring (2 * m + 1 + 1)).one) (L m) = 4 + 2 * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one  -- (+57 chars; full line in the Zenodo source)
  generalize chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) = c; generalize chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = d; lib_int

/-- **Residue pairs at distance d**: Σ_x (1 + χ(x))(1 + χ(x + d)) = p − 1 for d ≠ 0
    (the boundary terms x = 0, −d give 4·#{x : x, x + d both nonzero squares} = p − 3 − χ(d) − χ(−d)). -/
theorem pair_sum (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (d : Zp (2 * m + 1 + 1)) (hd : d ≠ (ring (2 * m + 1 + 1)).zero) :
    sumK LibInt.intR (fun x => (1 + chi m x) * (1 + chi m ((ring (2 * m + 1 + 1)).add x d))) (L m) = P m + -1 := by
  rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.add (LibInt.intR.add (LibInt.intR.mul 1 1) (chi m x)) (LibInt.intR.add (chi m ((ring (2 * m + 1 + 1)).add x d)) (chi m ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add x d))))) (fun x => by
    rw [chi_mul m hpr]; show _ = 1 * 1 + chi m x + (chi m ((ring (2 * m + 1 + 1)).add x d) + chi m x * chi m ((ring (2 * m + 1 + 1)).add x d))
    generalize chi m x = a; generalize chi m ((ring (2 * m + 1 + 1)).add x d) = b; lib_int)]
  rw [sumK_add, sumK_add, sumK_add, ← mul_sumK, sum_one, show sumK LibInt.intR (chi m) (L m) = 0 from chi_sum m hpr,
    shift_sum m (chi m) d, show sumK LibInt.intR (chi m) (L m) = 0 from chi_sum m hpr, sum_xxd m hpr d hd]
  show 1 * P m + 0 + (0 + -1) = _
  lib_int

/-- **Covariance of two translation families**: Σ_λ (Σ_x χ(h(x) + λ))(Σ_y χ(k(y) + λ)) = −p² + p·#{(x, y) : h(x) = k(y)}. -/
theorem translation_cov (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h k : Zp (2 * m + 1 + 1) → Zp (2 * m + 1 + 1)) :
    sumK LibInt.intR (fun l => sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).add l (h x))) (L m) * sumK LibInt.intR (fun y => chi m ((ring (2 * m + 1 + 1)).add l (k y))) (L m)) (L m)
      = -(P m * P m) + P m * sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR (h x) (k y)) (L m)) (L m) := by
  rw [sumK_ext LibInt.intR _ (fun l => sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l (h x)) ((ring (2 * m + 1 + 1)).add l (k y)))) (L m)) (L m)) (fun l => by
    show LibInt.intR.mul _ _ = _
    rw [sumK_mul]
    exact sumK_ext LibInt.intR _ _ (fun x => by
      rw [mul_sumK]
      exact sumK_ext LibInt.intR _ _ (fun y => (chi_mul m hpr _ _).symm) _) _)]
  rw [← sumK_swap LibInt.intR (fun x l => sumK LibInt.intR (fun y => chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l (h x)) ((ring (2 * m + 1 + 1)).add l (k y)))) (L m))]
  rw [sumK_ext LibInt.intR _ (fun x => sumK LibInt.intR (fun y => -1 + P m * delta LibInt.intR (h x) (k y)) (L m)) (fun x => by
    rw [← sumK_swap LibInt.intR (fun y l => chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l (h x)) ((ring (2 * m + 1 + 1)).add l (k y))))]
    exact sumK_ext LibInt.intR _ _ (fun y => inner m hpr _ _) _)]
  rw [sumK_ext LibInt.intR _ _ (fun x => PencilMoment.sum_lin m (-1) (P m) _), PencilMoment.sum_lin]
  lib_int

/-- **The quadratic Jacobi sum**: J(χ, χ) = Σ_x χ(x)χ(1 − x) = −χ(−1). -/
theorem jacobi_quad (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : sumK LibInt.intR (fun x => chi m x * chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x))) (L m) = -chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by
  rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.mul (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (chi m (f m x))) (fun x => by
    rw [← chi_mul m hpr, show (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x)) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) (f m x) by unfold f; lib_ring, chi_mul m hpr]; rfl)]
  rw [← mul_sumK, sum_f m hpr]
  show chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * -1 = _
  generalize chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = e; lib_int

theorem chi_cases (t : Zp (2 * m + 1 + 1)) : chi m t = 0 ∨ chi m t = 1 ∨ chi m t = -1 := by
  unfold chi
  by_cases h0 : t = (ring (2 * m + 1 + 1)).zero
  · rw [ite_pos' _ _ h0]; exact Or.inl rfl
  · rw [ite_neg' _ _ h0]
    by_cases h1 : Frobenius.pw (ring (2 * m + 1 + 1)) t (m + 1) = (ring (2 * m + 1 + 1)).one
    · rw [ite_pos' _ _ h1]; exact Or.inr (Or.inl rfl)
    · rw [ite_neg' _ _ h1]; exact Or.inr (Or.inr rfl)

theorem even_sum : ∀ (l : List (Zp (2 * m + 1 + 1))) (F : Zp (2 * m + 1 + 1) → Int), (∀ x, ∃ k : Int, F x = 2 * k) → ∃ k : Int, sumK LibInt.intR F l = 2 * k
  | [], _, _ => ⟨0, rfl⟩
  | x :: xs, F, h => by
    match h x, even_sum xs F h with
    | ⟨a, ha⟩, ⟨b, hb⟩ => exact ⟨a + b, by show F x + sumK LibInt.intR F xs = _; rw [ha, hb]; lib_int⟩

theorem cubic_delta (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (l x : Zp (2 * m + 1 + 1)) (h0 : l ≠ (ring (2 * m + 1 + 1)).zero) (h1 : l ≠ (ring (2 * m + 1 + 1)).one) :
    delta LibInt.intR ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))) (ring (2 * m + 1 + 1)).zero = delta LibInt.intR x (ring (2 * m + 1 + 1)).zero + delta LibInt.intR x (ring (2 * m + 1 + 1)).one + delta LibInt.intR x l := by
  have h10 : ((ring (2 * m + 1 + 1)).one : Zp (2 * m + 1 + 1)) ≠ (ring (2 * m + 1 + 1)).zero := PowerSum.one_ne_zero' (2 * m + 1)
  by_cases hx0 : x = (ring (2 * m + 1 + 1)).zero
  · rw [hx0, delta_one m _ _ (by unfold f; lib_ring), delta_one m _ _ rfl, delta_zero m _ _ h10.symm, delta_zero m _ _ (fun e => h0 e.symm)]; rfl
  · by_cases hx1 : x = (ring (2 * m + 1 + 1)).one
    · rw [hx1, delta_one m _ _ (by unfold f; lib_ring), delta_zero m _ _ h10, delta_one m _ _ rfl, delta_zero m _ _ (fun e => h1 e.symm)]; rfl
    · by_cases hxl : x = l
      · rw [hxl, delta_one m _ _ (by unfold f; lib_ring), delta_zero m _ _ h0, delta_zero m _ _ h1, delta_one m _ _ rfl]; rfl
      · rw [delta_zero m _ _ hx0, delta_zero m _ _ hx1, delta_zero m _ _ hxl, delta_zero m _ _ (fun e => by
          unfold f at e
          cases nz2 m hpr _ _ e with
          | inl h => cases nz2 m hpr _ _ h with
            | inl h' => exact hx0 h'
            | inr h' => exact hx1 (by
                calc x = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
                  _ = (ring (2 * m + 1 + 1)).one := by rw [h']; lib_ring)
          | inr h => exact hxl (by
              calc x = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)) l := by lib_ring
                _ = l := by rw [h]; lib_ring))]; rfl

/-- **The Legendre traces are even**: for λ ≠ 0, 1, a(λ) = 2k (2-torsion: a(λ) ≡ p − 3 mod 2). -/
theorem legendre_even (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (l : Zp (2 * m + 1 + 1)) (h0 : l ≠ (ring (2 * m + 1 + 1)).zero) (h1 : l ≠ (ring (2 * m + 1 + 1)).one) : ∃ k : Int, A m l = 2 * k := by
  -- a = Σχ² + Σ(χ − χ²), Σχ² = p − 3, and χ − χ² ∈ {0, −2}
  have hsq : sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))) * chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)))) (L m) = P m + -1 + -1 + -1 := by
    rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.add (LibInt.intR.mul 1 1) (LibInt.intR.add (LibInt.intR.mul (-1) (delta LibInt.intR x (ring (2 * m + 1 + 1)).zero)) (LibInt.intR.add (LibInt.intR.mul (-1) (delta LibInt.intR x (ring (2 * m + 1 + 1)).one)) (LibInt.intR.mul (-1) (delta LibInt.intR x l))))) (fun x => by
      rw [QuadDiagonal.sqchi m hpr, cubic_delta m hpr l x h0 h1]
      show 1 + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).zero + delta LibInt.intR x (ring (2 * m + 1 + 1)).one + delta LibInt.intR x l) = 1 * 1 + (-1 * delta LibInt.intR x (ring (2 * m + 1 + 1)).zero + (-1 * delta LibInt.intR x (ring (2 * m + 1 + 1)).one + -1 * delta LibInt.intR x l))
      generalize delta LibInt.intR x (ring (2 * m + 1 + 1)).zero = u; generalize delta LibInt.intR x (ring (2 * m + 1 + 1)).one = v; generalize delta LibInt.intR x l = w; lib_int)]
    rw [sumK_add, sumK_add, sumK_add, ← mul_sumK, ← mul_sumK, ← mul_sumK, ← mul_sumK, sum_one,
      show sumK LibInt.intR (fun x => delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR (ring (2 * m + 1 + 1)).zero,
      show sumK LibInt.intR (fun x => delta LibInt.intR x (ring (2 * m + 1 + 1)).one) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR (ring (2 * m + 1 + 1)).one,
      show sumK LibInt.intR (fun x => delta LibInt.intR x l) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR l]
    show 1 * P m + (-1 * 1 + (-1 * 1 + -1 * 1)) = _
    lib_int
  match even_sum m (L m) (fun x => chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))) + -(chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))) * chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))))) (fun x => by
    rcases chi_cases m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))) with e | e | e
    · exact ⟨0, by rw [e]; rfl⟩
    · exact ⟨0, by rw [e]; rfl⟩
    · exact ⟨-1, by rw [e]; rfl⟩) with
  | ⟨k, hk⟩ =>
    have hA : A m l = sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))) * chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)))) (L m) + sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))  -- (+231 chars; full line in the Zenodo source)
      unfold A
      rw [← QuadDiagonal.sI_add]
      exact sumK_ext LibInt.intR _ _ (fun x => by generalize chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))) = a; lib_int) _
    refine ⟨Int.ofNat m + k, ?_⟩
    rw [hA, hsq, hk]
    show Int.ofNat (2 * m + 1 + 1 + 1) + -1 + -1 + -1 + 2 * k = _
    rw [show Int.ofNat (2 * m + 1 + 1 + 1) = 2 * Int.ofNat m + 3 from rfl]
    lib_int

/-- **Parity of any character sum**: Σ_x χ(g x) + #{x : g x = 0} = p + 2k (so a_p of y² = g(x) ≡ #roots of g + p mod 2). -/
theorem char_parity (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (g : Zp (2 * m + 1 + 1) → Zp (2 * m + 1 + 1)) :
    ∃ k : Int, sumK LibInt.intR (fun x => chi m (g x)) (L m) + sumK LibInt.intR (fun x => delta LibInt.intR (g x) (ring (2 * m + 1 + 1)).zero) (L m) = P m + 2 * k := by
  match even_sum m (L m) (fun x => chi m (g x) + -(chi m (g x) * chi m (g x))) (fun x => by
    rcases chi_cases m (g x) with e | e | e
    · exact ⟨0, by rw [e]; rfl⟩
    · exact ⟨0, by rw [e]; rfl⟩
    · exact ⟨-1, by rw [e]; rfl⟩) with
  | ⟨k, hk⟩ =>
    refine ⟨k, ?_⟩
    have h1 : sumK LibInt.intR (fun x => chi m (g x)) (L m)
        = sumK LibInt.intR (fun x => chi m (g x) + -(chi m (g x) * chi m (g x))) (L m) + sumK LibInt.intR (fun x => 1 + -(delta LibInt.intR (g x) (ring (2 * m + 1 + 1)).zero)) (L m) := by
      rw [← QuadDiagonal.sI_add]
      exact sumK_ext LibInt.intR _ _ (fun x => by rw [← QuadDiagonal.sqchi m hpr]; generalize chi m (g x) = a; lib_int) _
    have h2 : sumK LibInt.intR (fun x => 1 + -(delta LibInt.intR (g x) (ring (2 * m + 1 + 1)).zero)) (L m) = P m + -(sumK LibInt.intR (fun x => delta LibInt.intR (g x) (ring (2 * m + 1 + 1)).zero) (L m)) := by
      rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.add (LibInt.intR.mul 1 1) (LibInt.intR.mul (-1) (delta LibInt.intR (g x) (ring (2 * m + 1 + 1)).zero))) (fun x => by
        show _ = 1 * 1 + -1 * _; generalize delta LibInt.intR (g x) (ring (2 * m + 1 + 1)).zero = d; lib_int), sumK_add, ← mul_sumK, ← mul_sumK, sum_one]
      show 1 * P m + -1 * _ = _
      generalize sumK LibInt.intR (fun x => delta LibInt.intR (g x) (ring (2 * m + 1 + 1)).zero) (L m) = D; lib_int
    rw [h1, h2, hk]
    generalize sumK LibInt.intR (fun x => delta LibInt.intR (g x) (ring (2 * m + 1 + 1)).zero) (L m) = D; lib_int

/-- **χ(−3) = 1 ⟹ 3 | p − 1**: z = (−1 + √−3)/2 is a primitive cube root of unity. -/
theorem three_div (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h3 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ≠ (ring (2 * m + 1 + 1)).zero) (hc : chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m   -- (+99 chars; full line in the Zenodo source)
  have hm3 : ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) : Zp (2 * m + 1 + 1)) ≠ (ring (2 * m + 1 + 1)).zero := negnz m _ h3
  have hpw : Frobenius.pw (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) (m + 1) = (ring (2 * m + 1 + 1)).one := by
    by_cases h : Frobenius.pw (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) (m + 1) = (ring (2 * m + 1 + 1)).one
    · exact h
    · exact absurd (hc.symm.trans (chi_neg_of m _ hm3 h)) (fun h' => int_m1_ne h'.symm)
  match QuadDiagonal.sq_root m hpr _ hm3 hpw with
  | ⟨s, hs⟩ =>
    have e2 : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ (QuadDiagonal.two_ne m)
    -- 2z + 1 = s, so 4(z² + z + 1) = s² + 3 = 0
    have h2z : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) s))) (ring (2 * m + 1 + 1)).one = s := by
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) s)) (ring (2 * m + 1 + 1)).one := by lib_rin  -- (+1 chars; full line in the Zenodo source)
        _ = s := by rw [e2]; lib_ring
    have hq : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 +   -- (+603 chars; full line in the Zenodo source)
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1   -- (+572 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [h2z, hs]; lib_ring
    have hq' : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) s)) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring  -- (+380 chars; full line in the Zenodo source)
      cases nz2 m hpr _ _ hq with
      | inl h => exact absurd h (nzm m hpr _ _ (QuadDiagonal.two_ne m) (QuadDiagonal.two_ne m))
      | inr h => exact h
    have hz3 : Frobenius.pw (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) s)) ((1 + 1 + 1) ^ (0 + 1)) = (ring (2 * m + 1 + 1)).one := by
      show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) s))) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 *  -- (+369 chars; full line in the Zenodo source)
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) s)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 *  -- (+735 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).one := by rw [hq']; lib_ring
    have hz1 : Frobenius.pw (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) s)) ((1 + 1 + 1) ^ 0) ≠ (ring (2 * m + 1 + 1)).one := fun e => by
      have e' : ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) s)) = (ring (2 * m + 1 + 1)).one := by
        calc ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) s)) = (ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring   -- (+95 chars; full line in the Zenodo source)
          _ = (ring (2 * m + 1 + 1)).one := e
      apply h3
      calc ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 +   -- (+505 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := hq'
    exact CycloPow.dvd_of_pow (2 * m + 1) hpr 1 0 StrongMult1.three_prime _ hz3 hz1

theorem no_grow (a b : Nat) (h : a = a + b + 1) : False := by
  have h' : a < a + b + 1 := Nat.lt_succ_of_le (Nat.le_add_right a b)
  rw [← h] at h'
  exact Nat.lt_irrefl a h'

/-- **3 | p − 1 ⟹ χ(−3) = 1**: with z = g^((p−1)/3), (2z + 1)² = −3. -/
theorem three_div_conv (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h3 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ≠ (ring (2 * m + 1 + 1)).zero) (c : Nat) (hc : 2 * m + 1 + 1 = 3 * c) : chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (  -- (+93 chars; full line in the Zenodo source)
  match PrimRoot.primitive_root (2 * m + 1) hpr with
  | ⟨g, hg⟩ =>
    have hz3 : (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) (Frobenius.pw (ring (2 * m + 1 + 1)) g c)) = (ring (2 * m + 1 + 1)).one := by
      have : Frobenius.pw (ring (2 * m + 1 + 1)) g (c * 3) = (ring (2 * m + 1 + 1)).one := by rw [show c * 3 = 2 * m + 1 + 1 by rw [hc]; lib_nat]; exact hg.1
      rw [← TwoPower.pwpow] at this
      rw [← this]; show _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one _) _) _; lib_ring
    have hz1 : (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ≠ (ring (2 * m + 1 + 1)).one := fun e => by
      match hg.2 c e with
      | ⟨k, hk⟩ =>
        revert hk hc
        cases c with
        | zero => intro hc0 _; exact Nat.noConfusion hc0
        | succ c' =>
          intro hc1 hk
          cases k with
          | zero => exact Nat.noConfusion (show c' + 1 = 0 by rw [hk]; lib_nat)
          | succ k' =>
            have e1 : c' + 1 = 3 * (c' + 1) * (k' + 1) := by
              calc c' + 1 = (2 * m + 1 + 1) * (k' + 1) := hk
                _ = 3 * (c' + 1) * (k' + 1) := by rw [hc1]
            exact no_grow (c' + 1) (2 * c' + 1 + 3 * (c' + 1) * k') (by
              calc c' + 1 = 3 * (c' + 1) * (k' + 1) := e1
                _ = _ := by lib_nat)
    have hq : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) (Frobenius.pw (ring (2 * m + 1 + 1)) g c)) ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) g c) (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by
      have e : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) (Frobenius.pw (ring (2 * m + 1 + 1)) g c)) ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) g c) (ring (2 * m + 1 + 1  -- (+44 chars; full line in the Zenodo source)
        calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) (Frobenius.pw (ring (2 * m + 1 + 1)) g c))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
          _ = (ring (2 * m + 1 + 1)).zero := by rw [hz3]; lib_ring
      cases nz2 m hpr _ _ e with
      | inl h => exact absurd (by
          calc (Frobenius.pw (ring (2 * m + 1 + 1)) g c) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
            _ = (ring (2 * m + 1 + 1)).one := by rw [h]; lib_ring) hz1
      | inr h => exact h
    have hsq : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) (Frobenius.pw (ring (2 * m + 1 + 1)) g c)) (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) (Frobeniu  -- (+235 chars; full line in the Zenodo source)
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) (Frobenius.pw (ring (2 * m + 1 + 1))   -- (+288 chars; full line in the Zenodo source)
        _ = _ := by rw [hq]; lib_ring
    have hne : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) (Frobenius.pw (ring (2 * m + 1 + 1)) g c)) (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero := fun e => negnz m _ h3 (by rw [← hsq, e]; lib_ring)
    rw [← hsq]; exact chi_sq m hpr _ hne

/-- **χ(−1) = 1 ⟹ 4 | p − 1** (i² = −1 has order 4). -/
theorem four_div (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (hc : chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = 1) : Dv ((0 + 1 + 1) ^ (1 + 1)) (2 * m + 1 + 1) := by
  have hm10 : ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one : Zp (2 * m + 1 + 1)) ≠ (ring (2 * m + 1 + 1)).zero := negnz m _ (PowerSum.one_ne_zero' (2 * m + 1))
  have hpw : Frobenius.pw (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) (m + 1) = (ring (2 * m + 1 + 1)).one := by
    by_cases h : Frobenius.pw (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) (m + 1) = (ring (2 * m + 1 + 1)).one
    · exact h
    · exact absurd (hc.symm.trans (chi_neg_of m _ hm10 h)) (fun h' => int_m1_ne h'.symm)
  match QuadDiagonal.sq_root m hpr _ hm10 hpw with
  | ⟨i, hi⟩ =>
    have h4 : Frobenius.pw (ring (2 * m + 1 + 1)) i ((0 + 1 + 1) ^ (1 + 1)) = (ring (2 * m + 1 + 1)).one := by
      show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one i) i) i) i = (ring (2 * m + 1 + 1)).one
      calc _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul i i) ((ring (2 * m + 1 + 1)).mul i i) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).one := by rw [hi]; lib_ring
    have h2 : Frobenius.pw (ring (2 * m + 1 + 1)) i ((0 + 1 + 1) ^ 1) ≠ (ring (2 * m + 1 + 1)).one := fun e => ne_pm m (by
      calc (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).mul i i := hi.symm
        _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one i) i := by lib_ring
        _ = (ring (2 * m + 1 + 1)).one := e)
    exact CycloPow.dvd_of_pow (2 * m + 1) hpr 0 1 StrongMult1.two_prime _ h4 h2

/-- **4 | p − 1 ⟹ χ(−1) = 1** (z = g^((p−1)/4) has z² = −1). -/
theorem four_div_conv (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (c : Nat) (hc : 2 * m + 1 + 1 = 4 * c) : chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = 1 := by
  match PrimRoot.primitive_root (2 * m + 1) hpr with
  | ⟨g, hg⟩ =>
    have hw : Frobenius.pw (ring (2 * m + 1 + 1)) g (2 * c) ≠ (ring (2 * m + 1 + 1)).one := fun e => by
      match hg.2 (2 * c) e with
      | ⟨k, hk⟩ =>
        revert hk hc
        cases c with
        | zero => intro hc0 _; exact Nat.noConfusion hc0
        | succ c' =>
          intro hc1 hk
          cases k with
          | zero => exact Nat.noConfusion (show 2 * (c' + 1) = 0 by rw [hk]; lib_nat)
          | succ k' =>
            exact no_grow (2 * (c' + 1)) (2 * c' + 1 + 4 * (c' + 1) * k') (by
              calc 2 * (c' + 1) = (2 * m + 1 + 1) * (k' + 1) := hk
                _ = 4 * (c' + 1) * (k' + 1) := by rw [hc1]
                _ = _ := by lib_nat)
    have hsq : (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g (2 * c)) (Frobenius.pw (ring (2 * m + 1 + 1)) g (2 * c)) = (ring (2 * m + 1 + 1)).one := by
      have : Frobenius.pw (ring (2 * m + 1 + 1)) g (2 * c * 2) = (ring (2 * m + 1 + 1)).one := by rw [show 2 * c * 2 = 2 * m + 1 + 1 by rw [hc]; lib_nat]; exact hg.1
      rw [← TwoPower.pwpow] at this
      rw [← this]; show _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one _) _; lib_ring
    have hm : Frobenius.pw (ring (2 * m + 1 + 1)) g (2 * c) = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one := by
      have e : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) g (2 * c)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) g (2 * c)) (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by
        calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g (2 * c)) (Frobenius.pw (ring (2 * m + 1 + 1)) g (2 * c))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
          _ = (ring (2 * m + 1 + 1)).zero := by rw [hsq]; lib_ring
      cases nz2 m hpr _ _ e with
      | inl h => exact absurd (by
          calc Frobenius.pw (ring (2 * m + 1 + 1)) g (2 * c) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) g (2 * c)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
            _ = (ring (2 * m + 1 + 1)).one := by rw [h]; lib_ring) hw
      | inr h => calc Frobenius.pw (ring (2 * m + 1 + 1)) g (2 * c) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) g (2 * c)) (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
          _ = _ := by rw [h]; lib_ring
    have hg0 : Frobenius.pw (ring (2 * m + 1 + 1)) g c ≠ (ring (2 * m + 1 + 1)).zero := fun e => by
      have := hm
      rw [show 2 * c = c * 2 by lib_nat, ← TwoPower.pwpow, e] at this
      exact negnz m _ (PowerSum.one_ne_zero' (2 * m + 1)) (by rw [← this]; show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).zero) (ring (2 * m + 1 + 1)).zero = _; lib_ring)
    rw [← hm, show 2 * c = c * 2 by lib_nat, ← TwoPower.pwpow, show Frobenius.pw (ring (2 * m + 1 + 1)) (Frobenius.pw (ring (2 * m + 1 + 1)) g c) 2 = (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) (Frobenius.pw (ring (2 * m + 1 + 1)) g c) by
      show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one _) _ = _; lib_ring]
    exact chi_sq m hpr _ hg0

theorem chi_one_pw (x : Zp (2 * m + 1 + 1)) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hc : chi m x = 1) : Frobenius.pw (ring (2 * m + 1 + 1)) x (m + 1) = (ring (2 * m + 1 + 1)).one := by
  by_cases h : Frobenius.pw (ring (2 * m + 1 + 1)) x (m + 1) = (ring (2 * m + 1 + 1)).one
  · exact h
  · exact absurd (hc.symm.trans (chi_neg_of m _ hx h)) (fun h' => int_m1_ne h'.symm)

/-- **χ(−1) = χ(2) = 1 ⟹ 8 | p − 1**: z = (1 + i)/√2 has z² = i, order 8. -/
theorem eight_div (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (hc1 : chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = 1) (hc2 : chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) = 1) : Dv ((0 + 1 + 1) ^ (2 + 1)) (2 * m + 1 + 1) := by
  have hm10 : ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one : Zp (2 * m + 1 + 1)) ≠ (ring (2 * m + 1 + 1)).zero := negnz m _ (PowerSum.one_ne_zero' (2 * m + 1))
  match QuadDiagonal.sq_root m hpr _ hm10 (chi_one_pw m _ hm10 hc1), QuadDiagonal.sq_root m hpr _ (QuadDiagonal.two_ne m) (chi_one_pw m _ (QuadDiagonal.two_ne m) hc2) with
  | ⟨i, hi⟩, ⟨r, hr⟩ =>
    have hr0 : r ≠ (ring (2 * m + 1 + 1)).zero := fun h0 => QuadDiagonal.two_ne m (by rw [← hr, h0]; lib_ring)
    have er : (ring (2 * m + 1 + 1)).mul (iv m r) r = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr r hr0
    -- z = (1 + i)·r⁻¹, z² = 2i·r⁻² = i
    have hz2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one i) (iv m r)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one i) (iv m r)) = i := by
      calc _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul i i)) ((ring (2 * m + 1 + 1)).add i i)) ((ring (2 * m + 1 + 1)).mul (iv m r) (iv m r)) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul r r) i) ((ring (2 * m + 1 + 1)).mul (iv m r) (iv m r)) := by rw [hi, hr]; lib_ring
        _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m r) r) ((ring (2 * m + 1 + 1)).mul (iv m r) r)) i := by lib_ring
        _ = i := by rw [er]; lib_ring
    have h8 : Frobenius.pw (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one i) (iv m r)) ((0 + 1 + 1) ^ (2 + 1)) = (ring (2 * m + 1 + 1)).one := by
      show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one _) _) _) _) _) _) _) _ = _
      generalize hz : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one i) (iv m r) = z at hz2 ⊢
      calc _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z)) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).one := by rw [hz2, hi]; lib_ring
    have h4 : Frobenius.pw (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one i) (iv m r)) ((0 + 1 + 1) ^ 2) ≠ (ring (2 * m + 1 + 1)).one := fun e => ne_pm m (by
      have e' : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one i) (iv m r))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one i) (iv m r))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).  -- (+174 chars; full line in the Zenodo source)
      generalize hz : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one i) (iv m r) = z at hz2 e'
      calc (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).mul i i := hi.symm
        _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z) := by rw [hz2]
        _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one z) z) z) z := by lib_ring
        _ = (ring (2 * m + 1 + 1)).one := e')
    exact CycloPow.dvd_of_pow (2 * m + 1) hpr 0 2 StrongMult1.two_prime _ h8 h4

theorem pw_mul_add (x : Zp (2 * m + 1 + 1)) (a b : Nat) : Frobenius.pw (ring (2 * m + 1 + 1)) x (a + b) = (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) x a) (Frobenius.pw (ring (2 * m + 1 + 1)) x b) := by
  induction b with
  | zero => show Frobenius.pw (ring (2 * m + 1 + 1)) x a = (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) x a) (ring (2 * m + 1 + 1)).one; lib_ring
  | succ b ih => show (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) x (a + b)) x = (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) x a) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) x b) x); rw [ih]; lib_ring

/-- **8 | p − 1 ⟹ χ(2) = 1**: z = g^((p−1)/8) has z⁴ = −1 and (z + z⁷)² = 2. -/
theorem eight_div_conv (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (c : Nat) (hc : 2 * m + 1 + 1 = 8 * c) : chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) = 1 := by
  match PrimRoot.primitive_root (2 * m + 1) hpr with
  | ⟨g, hg⟩ =>
    have hw : Frobenius.pw (ring (2 * m + 1 + 1)) g (4 * c) ≠ (ring (2 * m + 1 + 1)).one := fun e => by
      match hg.2 (4 * c) e with
      | ⟨k, hk⟩ =>
        revert hk hc
        cases c with
        | zero => intro hc0 _; exact Nat.noConfusion hc0
        | succ c' =>
          intro hc1 hk
          cases k with
          | zero => exact Nat.noConfusion (show 4 * (c' + 1) = 0 by rw [hk]; lib_nat)
          | succ k' =>
            exact no_grow (4 * (c' + 1)) (4 * c' + 3 + 8 * (c' + 1) * k') (by
              calc 4 * (c' + 1) = (2 * m + 1 + 1) * (k' + 1) := hk
                _ = 8 * (c' + 1) * (k' + 1) := by rw [hc1]
                _ = _ := by lib_nat)
    have h8 : Frobenius.pw (ring (2 * m + 1 + 1)) g (8 * c) = (ring (2 * m + 1 + 1)).one := by rw [← hc]; exact hg.1
    have hsq : (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g (4 * c)) (Frobenius.pw (ring (2 * m + 1 + 1)) g (4 * c)) = (ring (2 * m + 1 + 1)).one := by
      rw [← pw_mul_add, show 4 * c + 4 * c = 8 * c by lib_nat, h8]
    have h4 : Frobenius.pw (ring (2 * m + 1 + 1)) g (4 * c) = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one := by
      have e : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) g (4 * c)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) g (4 * c)) (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by
        calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g (4 * c)) (Frobenius.pw (ring (2 * m + 1 + 1)) g (4 * c))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
          _ = (ring (2 * m + 1 + 1)).zero := by rw [hsq]; lib_ring
      cases nz2 m hpr _ _ e with
      | inl h => exact absurd (by
          calc Frobenius.pw (ring (2 * m + 1 + 1)) g (4 * c) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) g (4 * c)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
            _ = (ring (2 * m + 1 + 1)).one := by rw [h]; lib_ring) hw
      | inr h => calc Frobenius.pw (ring (2 * m + 1 + 1)) g (4 * c) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) g (4 * c)) (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
          _ = _ := by rw [h]; lib_ring
    -- powers of z = g^c
    have p4 : Frobenius.pw (ring (2 * m + 1 + 1)) g (4 * c) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) (Frobenius.pw (ring (2 * m + 1 + 1)) g c)) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) (Frobenius.pw (ring (2 * m + 1 + 1)) g c)) := by
      rw [show 4 * c = c + c + (c + c) by lib_nat, pw_mul_add, pw_mul_add]
    have p8 : Frobenius.pw (ring (2 * m + 1 + 1)) g (8 * c) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) (Frobenius.pw (ring (2 * m + 1 + 1)) g c)) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) (Frobenius.pw (ring (2 * m + 1 + 1)) g c))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul   -- (+204 chars; full line in the Zenodo source)
      rw [show 8 * c = 4 * c + 4 * c by lib_nat, pw_mul_add, p4]
    have hw2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) (Frobenius.pw (ring (2 * m + 1 + 1)) g c)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) (Frobenius.pw (ring (2 * m + 1 + 1)) g c)) ((ring  -- (+180 chars; full line in the Zenodo source)
        ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) (Frobenius.pw (ring (2 * m + 1 + 1)) g c)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) (Frobenius.pw (ring (2 * m + 1 + 1)) g c)) ((ring (2 * m + 1 + 1)).mul (Frobenius.p  -- (+237 chars; full line in the Zenodo source)
      have a4 := h4; rw [p4] at a4
      have a8 := h8; rw [p8] at a8
      generalize (Frobenius.pw (ring (2 * m + 1 + 1)) g c) = z at a4 a8 ⊢
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1))  -- (+454 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z z)) (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ri  -- (+66 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul z z)) (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) (ring (2 * m +  -- (+27 chars; full line in the Zenodo source)
        _ = ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) := by lib_ring
    have hne : (ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) (Frobenius.pw (ring (2 * m + 1 + 1)) g c)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) (Frobenius.pw (ring (2 * m + 1 + 1)) g c)) ((ring (2 * m + 1 + 1)).mul (Frobe  -- (+193 chars; full line in the Zenodo source)
      QuadDiagonal.two_ne m (by rw [← hw2, e]; lib_ring)
    rw [← hw2]; exact chi_sq m hpr _ hne

theorem chi3_split (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) = chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2  -- (+81 chars; full line in the Zenodo source)
  rw [← chi_mul m hpr, show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) = ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2   -- (+32 chars; full line in the Zenodo source)

theorem chi_pm (t : Zp (2 * m + 1 + 1)) (ht : t ≠ (ring (2 * m + 1 + 1)).zero) (hn : chi m t ≠ 1) : chi m t = -1 := by
  rcases chi_cases m t with e | e | e
  · unfold chi at e; rw [ite_neg' _ _ ht] at e
    by_cases h : Frobenius.pw (ring (2 * m + 1 + 1)) t (m + 1) = (ring (2 * m + 1 + 1)).one
    · rw [ite_pos' _ _ h] at e; exact absurd (Int.ofNat.inj e) (fun h' => Nat.noConfusion h')
    · rw [ite_neg' _ _ h] at e; exact absurd e (fun h' => Int.noConfusion h')
  · exact absurd e hn
  · exact e

/-- **p ≡ 1 (mod 12) ⟹ χ(3) = 1.** -/
theorem chi3_one (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h3 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ≠ (ring (2 * m + 1 + 1)).zero) (c : Nat) (hc : 2 * m + 1 + 1 = 12 * c) : chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring   -- (+59 chars; full line in the Zenodo source)
  rw [chi3_split m hpr, four_div_conv m hpr (3 * c) (by rw [hc]; lib_nat), three_div_conv m hpr h3 (4 * c) (by rw [hc]; lib_nat)]; rfl

/-- **p ≡ 11 (mod 12) ⟹ χ(3) = 1** (both −1 and −3 are non-squares). -/
theorem chi3_eleven (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h3 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ≠ (ring (2 * m + 1 + 1)).zero) (c : Nat) (hc : 2 * m + 1 + 1 = 12 * c + 10) : chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).ad  -- (+67 chars; full line in the Zenodo source)
  have hm1 : chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = -1 := chi_pm m _ (negnz m _ (PowerSum.one_ne_zero' (2 * m + 1))) (fun e => by
    match four_div m hpr e with
    | ⟨k, hk⟩ =>
      have : (3 * c + 2) * 4 + 2 = k * 4 + 0 := by
        calc (3 * c + 2) * 4 + 2 = 2 * m + 1 + 1 := by rw [hc]; lib_nat
          _ = k * ((0 + 1 + 1) ^ (1 + 1)) := hk
          _ = k * 4 + 0 := rfl
      exact Nat.noConfusion (Inf2e1.uniq_remG 4 _ _ 2 0 (Nat.le_add_left 3 1) (Nat.le_add_left 1 3) this))
  have hm3 : chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) = -1 := chi_pm m _ (negnz m _ h3) (fun e => by
    match three_div m hpr h3 e with
    | ⟨k, hk⟩ =>
      have : (4 * c + 3) * 3 + 1 = k * 3 + 0 := by
        calc (4 * c + 3) * 3 + 1 = 2 * m + 1 + 1 := by rw [hc]; lib_nat
          _ = k * ((1 + 1 + 1) ^ (0 + 1)) := hk
          _ = k * 3 + 0 := rfl
      exact Nat.noConfusion (Inf2e1.uniq_remG 3 _ _ 1 0 (Nat.le_add_left 2 1) (Nat.le_add_left 1 2) this))
  rw [chi3_split m hpr, hm1, hm3]; rfl

/-- **p ≡ 5 (mod 12) ⟹ χ(3) = −1.** -/
theorem chi3_five (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h3 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ≠ (ring (2 * m + 1 + 1)).zero) (c : Nat) (hc : 2 * m + 1 + 1 = 12 * c + 4) : chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (  -- (+65 chars; full line in the Zenodo source)
  have hm3 : chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) = -1 := chi_pm m _ (negnz m _ h3) (fun e => by
    match three_div m hpr h3 e with
    | ⟨k, hk⟩ =>
      have : (4 * c + 1) * 3 + 1 = k * 3 + 0 := by
        calc (4 * c + 1) * 3 + 1 = 2 * m + 1 + 1 := by rw [hc]; lib_nat
          _ = k * ((1 + 1 + 1) ^ (0 + 1)) := hk
          _ = k * 3 + 0 := rfl
      exact Nat.noConfusion (Inf2e1.uniq_remG 3 _ _ 1 0 (Nat.le_add_left 2 1) (Nat.le_add_left 1 2) this))
  rw [chi3_split m hpr, four_div_conv m hpr (3 * c + 1) (by rw [hc]; lib_nat), hm3]; rfl

/-- **p ≡ 7 (mod 12) ⟹ χ(3) = −1.** -/
theorem chi3_seven (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h3 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ≠ (ring (2 * m + 1 + 1)).zero) (c : Nat) (hc : 2 * m + 1 + 1 = 12 * c + 6) : chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add   -- (+66 chars; full line in the Zenodo source)
  have hm1 : chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = -1 := chi_pm m _ (negnz m _ (PowerSum.one_ne_zero' (2 * m + 1))) (fun e => by
    match four_div m hpr e with
    | ⟨k, hk⟩ =>
      have : (3 * c + 1) * 4 + 2 = k * 4 + 0 := by
        calc (3 * c + 1) * 4 + 2 = 2 * m + 1 + 1 := by rw [hc]; lib_nat
          _ = k * ((0 + 1 + 1) ^ (1 + 1)) := hk
          _ = k * 4 + 0 := rfl
      exact Nat.noConfusion (Inf2e1.uniq_remG 4 _ _ 2 0 (Nat.le_add_left 3 1) (Nat.le_add_left 1 3) this))
  rw [chi3_split m hpr, hm1, three_div_conv m hpr h3 (4 * c + 2) (by rw [hc]; lib_nat)]; rfl

/-- The quadratic Gauss sum of order 5 inside 𝔽_p: z⁵ = 1, z ≠ 1 ⟹ (z − z² − z³ + z⁴)² = 5. -/
theorem gauss5 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (z : Zp (2 * m + 1 + 1)) (hz5 : ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)))) = (ring (2 * m + 1 + 1)).one) (hz1 : z ≠ (ring (2 * m + 1 + 1)).one) : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add z (  -- (+877 chars; full line in the Zenodo source)
  have hphi : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)))))) = (ring (2 * m + 1 + 1)).zero := by
    have e : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z ((rin  -- (+99 chars; full line in the Zenodo source)
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [hz5]; lib_ring
    cases nz2 m hpr _ _ e with
    | inl h => exact absurd (by
        calc z = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
          _ = (ring (2 * m + 1 + 1)).one := by rw [h]; lib_ring) hz1
    | inr h => exact h
  calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one   -- (+1089 chars; full line in the Zenodo source)
    _ = ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)))) := by rw [hz5, hphi]; lib_ring

/-- **5 | p − 1 ⟹ χ(5) = 1** (z = g^((p−1)/5) and the Gauss sum of order 5). -/
theorem five_div_conv (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h5 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)))) ≠ (ring (2 * m + 1 + 1)).zero) (c : Nat) (hc : 2 *   -- (+287 chars; full line in the Zenodo source)
  match PrimRoot.primitive_root (2 * m + 1) hpr with
  | ⟨g, hg⟩ =>
    have hz1 : Frobenius.pw (ring (2 * m + 1 + 1)) g c ≠ (ring (2 * m + 1 + 1)).one := fun e => by
      match hg.2 c e with
      | ⟨k, hk⟩ =>
        revert hk hc
        cases c with
        | zero => intro hc0 _; exact Nat.noConfusion hc0
        | succ c' =>
          intro hc1 hk
          cases k with
          | zero => exact Nat.noConfusion (show c' + 1 = 0 by rw [hk]; lib_nat)
          | succ k' =>
            exact no_grow (c' + 1) (4 * c' + 3 + 5 * (c' + 1) * k') (by
              calc c' + 1 = (2 * m + 1 + 1) * (k' + 1) := hk
                _ = 5 * (c' + 1) * (k' + 1) := by rw [hc1]
                _ = _ := by lib_nat)
    have hz5 : (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) (Frobenius.pw (ring (2 * m + 1 + 1)) g c)))) = (ring (2 * m + 1 + 1)).one := by
      rw [← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, show c + (c + (c + (c + c))) = 2 * m + 1 + 1 by rw [hc]; lib_nat]
      exact hg.1
    have hs := gauss5 m hpr _ hz5 hz1
    have hs0 : ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) (Frobenius.pw (ring (2 * m + 1 + 1)) g c)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1   -- (+426 chars; full line in the Zenodo source)
    rw [← hs]; exact chi_sq m hpr _ hs0

/-- The quadratic Gauss sum of order 7 inside 𝔽_p: z⁷ = 1 ≠ z ⟹ (z + z² − z³ + z⁴ − z⁵ − z⁶)² = −7. -/
theorem gauss7 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (z : Zp (2 * m + 1 + 1)) (hz7 : (ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z (z)))))) = (ring (2 * m + 1 + 1)).one) (hz1 : z ≠ (ring (2 * m + 1 + 1)).one) : (ring (2 * m + 1 + 1)).m  -- (+2552 chars; full line in the Zenodo source)
  have hphi : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (z) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z (z)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z (z))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z (z)))) ((ring (2 * m +   -- (+339 chars; full line in the Zenodo source)
    have e : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (z) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z (z)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z (z))) ((ring (2 * m + 1 + 1)).add  -- (+454 chars; full line in the Zenodo source)
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z (z))))))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [hz7]; lib_ring
    cases nz2 m hpr _ _ e with
    | inl h => exact absurd (by
        calc z = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
          _ = (ring (2 * m + 1 + 1)).one := by rw [h]; lib_ring) hz1
    | inr h => exact h
  calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1  -- (+2219 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1))  -- (+42 chars; full line in the Zenodo source)

/-- **7 | p − 1 ⟹ χ(−7) = 1.** -/
theorem seven_div_conv (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h7 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1  -- (+567 chars; full line in the Zenodo source)
  match PrimRoot.primitive_root (2 * m + 1) hpr with
  | ⟨g, hg⟩ =>
    have hz1 : Frobenius.pw (ring (2 * m + 1 + 1)) g c ≠ (ring (2 * m + 1 + 1)).one := fun e => by
      match hg.2 c e with
      | ⟨k, hk⟩ =>
        revert hk hc
        cases c with
        | zero => intro hc0 _; exact Nat.noConfusion hc0
        | succ c' =>
          intro hc1 hk
          cases k with
          | zero => exact Nat.noConfusion (show c' + 1 = 0 by rw [hk]; lib_nat)
          | succ k' =>
            exact no_grow (c' + 1) (6 * c' + 5 + 7 * (c' + 1) * k') (by
              calc c' + 1 = (2 * m + 1 + 1) * (k' + 1) := hk
                _ = 7 * (c' + 1) * (k' + 1) := by rw [hc1]
                _ = _ := by lib_nat)
    have hz7 : (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobeni  -- (+117 chars; full line in the Zenodo source)
      rw [← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add,
        show c + (c + (c + (c + (c + (c + c))))) = 2 * m + 1 + 1 by rw [hc]; lib_nat]
      exact hg.1
    have hs := gauss7 m hpr _ hz7 hz1
    have hs0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((Frobenius.pw (ring (2 * m + 1 + 1)) g c))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((Frobenius.pw (ring (2 * m + 1 + 1)) g c)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul  -- (+1604 chars; full line in the Zenodo source)
    rw [← hs]; exact chi_sq m hpr _ hs0

/-- The quadratic Gauss sum of order 11 inside 𝔽_p: z^11 = 1 ≠ z ⟹ (Σ_k (k/11) z^k)² = -11. -/
theorem gauss11 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (z : Zp (2 * m + 1 + 1)) (hzl : (ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m  -- (+5613 chars; full line in the Zenodo source)
  have hphi : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (z) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z (z)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z (z))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z (z)))) ((ring (2 * m +   -- (+1401 chars; full line in the Zenodo source)
    have e : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (z) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z (z)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z (z))) ((ring (2 * m + 1 + 1)).add  -- (+1516 chars; full line in the Zenodo source)
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z (z))))))))))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1  -- (+26 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [hzl]; lib_ring
    cases nz2 m hpr _ _ e with
    | inl h => exact absurd (by
        calc z = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
          _ = (ring (2 * m + 1 + 1)).one := by rw [h]; lib_ring) hz1
    | inr h => exact h
  calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1  -- (+5170 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1))  -- (+274 chars; full line in the Zenodo source)

/-- **11 | p − 1 ⟹ χ(-11) = 1.** -/
theorem eleven_div_conv (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (hl : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m +   -- (+1033 chars; full line in the Zenodo source)
  match PrimRoot.primitive_root (2 * m + 1) hpr with
  | ⟨g, hg⟩ =>
    have hz1 : Frobenius.pw (ring (2 * m + 1 + 1)) g c ≠ (ring (2 * m + 1 + 1)).one := fun e => by
      match hg.2 c e with
      | ⟨k, hk⟩ =>
        revert hk hc
        cases c with
        | zero => intro hc0 _; exact Nat.noConfusion hc0
        | succ c' =>
          intro hc1 hk
          cases k with
          | zero => exact Nat.noConfusion (show c' + 1 = 0 by rw [hk]; lib_nat)
          | succ k' =>
            exact no_grow (c' + 1) (10 * c' + 9 + 11 * (c' + 1) * k') (by
              calc c' + 1 = (2 * m + 1 + 1) * (k' + 1) := hk
                _ = 11 * (c' + 1) * (k' + 1) := by rw [hc1]
                _ = _ := by lib_nat)
    have hzl : (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobeni  -- (+401 chars; full line in the Zenodo source)
      rw [← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add,
        show c + (c + (c + (c + (c + (c + (c + (c + (c + (c + (c)))))))))) = 2 * m + 1 + 1 by rw [hc]; lib_nat]
      exact hg.1
    have hs := gauss11 m hpr _ hzl hz1
    have hs0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((Frobenius.pw (ring (2 * m + 1 + 1)) g c))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((Frobenius.pw (ring (2 * m + 1 + 1)) g c)))) ((ring (2 * m + 1 + 1)).ad  -- (+4316 chars; full line in the Zenodo source)
    rw [← hs]; exact chi_sq m hpr _ hs0

/-- The quadratic Gauss sum of order 13 inside 𝔽_p: z^13 = 1 ≠ z ⟹ (Σ_k (k/13) z^k)² = 13. -/
theorem gauss13 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (z : Zp (2 * m + 1 + 1)) (hzl : (ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m  -- (+7486 chars; full line in the Zenodo source)
  have hphi : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (z) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z (z)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z (z))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z (z)))) ((ring (2 * m +   -- (+2118 chars; full line in the Zenodo source)
    have e : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (z) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z (z)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z (z))) ((ring (2 * m + 1 + 1)).add  -- (+2233 chars; full line in the Zenodo source)
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul   -- (+88 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [hzl]; lib_ring
    cases nz2 m hpr _ _ e with
    | inl h => exact absurd (by
        calc z = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
          _ = (ring (2 * m + 1 + 1)).one := by rw [h]; lib_ring) hz1
    | inr h => exact h
  calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1  -- (+7331 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1))  -- (+361 chars; full line in the Zenodo source)

/-- **13 | p − 1 ⟹ χ(13) = 1.** -/
theorem thirteen_div_conv (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (hl : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m   -- (+1238 chars; full line in the Zenodo source)
  match PrimRoot.primitive_root (2 * m + 1) hpr with
  | ⟨g, hg⟩ =>
    have hz1 : Frobenius.pw (ring (2 * m + 1 + 1)) g c ≠ (ring (2 * m + 1 + 1)).one := fun e => by
      match hg.2 c e with
      | ⟨k, hk⟩ =>
        revert hk hc
        cases c with
        | zero => intro hc0 _; exact Nat.noConfusion hc0
        | succ c' =>
          intro hc1 hk
          cases k with
          | zero => exact Nat.noConfusion (show c' + 1 = 0 by rw [hk]; lib_nat)
          | succ k' =>
            exact no_grow (c' + 1) (12 * c' + 11 + 13 * (c' + 1) * k') (by
              calc c' + 1 = (2 * m + 1 + 1) * (k' + 1) := hk
                _ = 13 * (c' + 1) * (k' + 1) := by rw [hc1]
                _ = _ := by lib_nat)
    have hzl : (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobeni  -- (+543 chars; full line in the Zenodo source)
      rw [← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add,
        show c + (c + (c + (c + (c + (c + (c + (c + (c + (c + (c + (c + (c)))))))))))) = 2 * m + 1 + 1 by rw [hc]; lib_nat]
      exact hg.1
    have hs := gauss13 m hpr _ hzl hz1
    have hs0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((Frobenius.pw (ring (2 * m + 1 + 1)) g c))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((Frobenius.pw (ring (2 * m + 1 + 1)) g c)))) ((ring (2 * m + 1 + 1)).ad  -- (+6088 chars; full line in the Zenodo source)
    rw [← hs]; exact chi_sq m hpr _ hs0

/-- **Stickelberger for a cubic with a root**: for x³ + ax + b = (x − r)(x² + rx + c), Δ = −4a³ − 27b² = (r² − 4c)(c + 2r²)²,
    so χ(Δ) = χ(r² − 4c) when r is not a root of the quadratic factor. -/
theorem stick_root (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (r c : Zp (2 * m + 1 + 1)) (hw : (ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul r r)) ≠ (ring (2 * m + 1 + 1)).zero) :
    chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).m  -- (+1245 chars; full line in the Zenodo source)
  rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).m  -- (+1687 chars; full line in the Zenodo source)
  generalize chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul r r) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) c))) = e; lib_int

/-- **Three roots in 𝔽_p ⟹ the discriminant is a square**: roots r₁, r₂, −r₁ − r₂ pairwise distinct ⟹ χ(Δ) = 1. -/
theorem stick_three (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (r1 r2 : Zp (2 * m + 1 + 1)) (hd : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add r1 ((ring (2 * m + 1 + 1)).neg r2)) ((ring (2 * m + 1 + 1)).add r1 ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add r1 r2))))) ((ring (2 * m + 1 + 1)).add r2  -- (+2214 chars; full line in the Zenodo source)
  rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul r1 r2) ((ring (2 * m + 1 +   -- (+2465 chars; full line in the Zenodo source)
  exact chi_sq m hpr _ hd

/-- The quadratic Gauss sum of order 17 inside 𝔽_p: (Σ_k (k/17) z^k)² = 17. -/
theorem gauss17 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (z : Zp (2 * m + 1 + 1)) (hzl : (ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m  -- (+12034 chars; full line in the Zenodo source)
  have hphi : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (z) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z (z)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z (z))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z (z)))) ((ring (2 * m +   -- (+3924 chars; full line in the Zenodo source)
    have e : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (z) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z (z)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z (z))) ((ring (2 * m + 1 + 1)).add  -- (+4039 chars; full line in the Zenodo source)
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul   -- (+212 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [hzl]; lib_ring
    cases nz2 m hpr _ _ e with
    | inl h => exact absurd (by
        calc z = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
          _ = (ring (2 * m + 1 + 1)).one := by rw [h]; lib_ring) hz1
    | inr h => exact h
  calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1  -- (+11313 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1))  -- (+593 chars; full line in the Zenodo source)

/-- **17 | p − 1 ⟹ χ(17) = 1.** -/
theorem seventeen_div_conv (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (hl : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m  -- (+1703 chars; full line in the Zenodo source)
  match PrimRoot.primitive_root (2 * m + 1) hpr with
  | ⟨g, hg⟩ =>
    have hz1 : Frobenius.pw (ring (2 * m + 1 + 1)) g c ≠ (ring (2 * m + 1 + 1)).one := fun e => by
      match hg.2 c e with
      | ⟨k, hk⟩ =>
        revert hk hc
        cases c with
        | zero => intro hc0 _; exact Nat.noConfusion hc0
        | succ c' =>
          intro hc1 hk
          cases k with
          | zero => exact Nat.noConfusion (show c' + 1 = 0 by rw [hk]; lib_nat)
          | succ k' =>
            exact no_grow (c' + 1) (16 * c' + 15 + 17 * (c' + 1) * k') (by
              calc c' + 1 = (2 * m + 1 + 1) * (k' + 1) := hk
                _ = 17 * (c' + 1) * (k' + 1) := by rw [hc1]
                _ = _ := by lib_nat)
    have hzl : (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobeni  -- (+827 chars; full line in the Zenodo source)
      rw [← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add,
        show c + (c + (c + (c + (c + (c + (c + (c + (c + (c + (c + (c + (c + (c + (c + (c + (c)))))))))))))))) = 2 * m + 1 + 1 by rw [hc]; lib_nat]
      exact hg.1
    have hs := gauss17 m hpr _ hzl hz1
    have hs0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((Frobenius.pw (ring (2 * m + 1 + 1)) g c))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((Frobenius.pw (ring (2 * m + 1 + 1)) g c)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul  -- (+10504 chars; full line in the Zenodo source)
    rw [← hs]; exact chi_sq m hpr _ hs0

/-- The quadratic Gauss sum of order 19 inside 𝔽_p: (Σ_k (k/19) z^k)² = -19. -/
theorem gauss19 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (z : Zp (2 * m + 1 + 1)) (hzl : (ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m  -- (+14709 chars; full line in the Zenodo source)
  have hphi : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (z) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z (z)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z (z))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z (z)))) ((ring (2 * m +   -- (+5013 chars; full line in the Zenodo source)
    have e : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (z) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z (z)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z (z))) ((ring (2 * m + 1 + 1)).add  -- (+5128 chars; full line in the Zenodo source)
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul   -- (+274 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [hzl]; lib_ring
    cases nz2 m hpr _ _ e with
    | inl h => exact absurd (by
        calc z = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
          _ = (ring (2 * m + 1 + 1)).one := by rw [h]; lib_ring) hz1
    | inr h => exact h
  calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1  -- (+14714 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1))  -- (+738 chars; full line in the Zenodo source)

/-- **19 | p − 1 ⟹ χ(-19) = 1.** -/
theorem nineteen_div_conv (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (hl : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m   -- (+1963 chars; full line in the Zenodo source)
  match PrimRoot.primitive_root (2 * m + 1) hpr with
  | ⟨g, hg⟩ =>
    have hz1 : Frobenius.pw (ring (2 * m + 1 + 1)) g c ≠ (ring (2 * m + 1 + 1)).one := fun e => by
      match hg.2 c e with
      | ⟨k, hk⟩ =>
        revert hk hc
        cases c with
        | zero => intro hc0 _; exact Nat.noConfusion hc0
        | succ c' =>
          intro hc1 hk
          cases k with
          | zero => exact Nat.noConfusion (show c' + 1 = 0 by rw [hk]; lib_nat)
          | succ k' =>
            exact no_grow (c' + 1) (18 * c' + 17 + 19 * (c' + 1) * k') (by
              calc c' + 1 = (2 * m + 1 + 1) * (k' + 1) := hk
                _ = 19 * (c' + 1) * (k' + 1) := by rw [hc1]
                _ = _ := by lib_nat)
    have hzl : (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobeni  -- (+969 chars; full line in the Zenodo source)
      rw [← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add,
        show c + (c + (c + (c + (c + (c + (c + (c + (c + (c + (c + (c + (c + (c + (c + (c + (c + (c + (c)))))))))))))))))) = 2 * m + 1 + 1 by rw [hc]; lib_nat]
      exact hg.1
    have hs := gauss19 m hpr _ hzl hz1
    have hs0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((Frobenius.pw (ring (2 * m + 1 + 1)) g c))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((Frobenius.pw (ring (2 * m + 1 + 1)) g c)))) ((ring (2 * m + 1 + 1)).ad  -- (+13148 chars; full line in the Zenodo source)
    rw [← hs]; exact chi_sq m hpr _ hs0

/-- **#{(x, y) : x² = y²} = 2p − 1.** -/
theorem square_pairs (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) :
    sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul y y)) (L m)) (L m) = P m + P m + -1 := by
  have hrow : ∀ x, sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul y y)) (L m) = 2 + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) := fun x => by
    by_cases hx : x = (ring (2 * m + 1 + 1)).zero
    · rw [hx, delta_one m _ _ rfl]
      rw [sumK_ext LibInt.intR _ (fun y => delta LibInt.intR y (ring (2 * m + 1 + 1)).zero) (fun y => by
        by_cases hy : y = (ring (2 * m + 1 + 1)).zero
        · rw [hy, delta_one m _ _ (by lib_ring), delta_one m _ _ rfl]
        · rw [delta_zero m _ _ hy, delta_zero m _ _ (fun e => hy (by
            cases nz2 m hpr _ _ e.symm with
            | inl h => exact h
            | inr h => exact h))]),
        show sumK LibInt.intR (fun y => delta LibInt.intR y (ring (2 * m + 1 + 1)).zero) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR (ring (2 * m + 1 + 1)).zero]
      rfl
    · rw [sumK_ext LibInt.intR _ _ (fun y => roots_sq m hpr x y hx), QuadDiagonal.sI_add,
        show sumK LibInt.intR (fun y => delta LibInt.intR y x) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR x,
        show sumK LibInt.intR (fun y => delta LibInt.intR y ((ring (2 * m + 1 + 1)).neg x)) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR ((ring (2 * m + 1 + 1)).neg x),
        delta_zero m _ _ hx]
      rfl
  rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.add (LibInt.intR.mul 2 1) (LibInt.intR.mul (-1) (delta LibInt.intR x (ring (2 * m + 1 + 1)).zero))) (fun x => by
    rw [hrow x]; show _ = 2 * 1 + -1 * _; generalize delta LibInt.intR x (ring (2 * m + 1 + 1)).zero = d; lib_int), sumK_add, ← mul_sumK, ← mul_sumK, sum_one,
    show sumK LibInt.intR (fun x => delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR (ring (2 * m + 1 + 1)).zero]
  show 2 * P m + -1 * 1 = _
  lib_int

/-- **Σ_λ (Σ_x χ(x² + λ))² = p² − p** (translation_moment with #{x² = y²} = 2p − 1). -/
theorem square_family (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) :
    sumK LibInt.intR (fun l => sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).mul x x))) (L m) * sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).mul x x))) (L m)) (L m)
      = P m * P m + -(P m) := by
  rw [translation_moment m hpr (fun x => (ring (2 * m + 1 + 1)).mul x x), square_pairs m hpr]
  lib_int

end LegendreMoment

#print axioms LegendreMoment.pt
#print axioms LegendreMoment.sq_expand
#print axioms LegendreMoment.neg_delta
#print axioms LegendreMoment.sum_f
#print axioms LegendreMoment.f_delta
#print axioms LegendreMoment.sum_f2
#print axioms LegendreMoment.legendre_moment
#print axioms LegendreMoment.refl_one
#print axioms LegendreMoment.refl_inv
#print axioms LegendreMoment.sum_A
#print axioms LegendreMoment.twisted_sum
#print axioms LegendreMoment.pt1
#print axioms LegendreMoment.delta_shift1
#print axioms LegendreMoment.ff1
#print axioms LegendreMoment.adjacent
#print axioms LegendreMoment.odd_moments
#print axioms LegendreMoment.family_moment
#print axioms LegendreMoment.root_count
#print axioms LegendreMoment.quad_family
#print axioms LegendreMoment.translation_moment
#print axioms LegendreMoment.cube_pairs
#print axioms LegendreMoment.row
#print axioms LegendreMoment.cube_roots
#print axioms LegendreMoment.fourth_roots
#print axioms LegendreMoment.sixth_roots
#print axioms LegendreMoment.int_m1_ne
#print axioms LegendreMoment.quartic_m1
#print axioms LegendreMoment.eighth_roots
#print axioms LegendreMoment.cube_m1
#print axioms LegendreMoment.sixth_m1
#print axioms LegendreMoment.twelfth_roots
#print axioms LegendreMoment.pair_sum
#print axioms LegendreMoment.translation_cov
#print axioms LegendreMoment.jacobi_quad
#print axioms LegendreMoment.chi_cases
#print axioms LegendreMoment.even_sum
#print axioms LegendreMoment.cubic_delta
#print axioms LegendreMoment.legendre_even
#print axioms LegendreMoment.char_parity
#print axioms LegendreMoment.three_div
#print axioms LegendreMoment.no_grow
#print axioms LegendreMoment.three_div_conv
#print axioms LegendreMoment.four_div
#print axioms LegendreMoment.four_div_conv
#print axioms LegendreMoment.chi_one_pw
#print axioms LegendreMoment.eight_div
#print axioms LegendreMoment.pw_mul_add
#print axioms LegendreMoment.eight_div_conv
#print axioms LegendreMoment.chi3_split
#print axioms LegendreMoment.chi_pm
#print axioms LegendreMoment.chi3_one
#print axioms LegendreMoment.chi3_eleven
#print axioms LegendreMoment.chi3_five
#print axioms LegendreMoment.chi3_seven
#print axioms LegendreMoment.gauss5
#print axioms LegendreMoment.five_div_conv
#print axioms LegendreMoment.gauss7
#print axioms LegendreMoment.seven_div_conv
#print axioms LegendreMoment.gauss11
#print axioms LegendreMoment.eleven_div_conv
#print axioms LegendreMoment.gauss13
#print axioms LegendreMoment.thirteen_div_conv
#print axioms LegendreMoment.stick_root
#print axioms LegendreMoment.stick_three
#print axioms LegendreMoment.gauss17
#print axioms LegendreMoment.seventeen_div_conv
#print axioms LegendreMoment.gauss19
#print axioms LegendreMoment.nineteen_div_conv
#print axioms LegendreMoment.square_pairs
#print axioms LegendreMoment.square_family

lang/LucChar.lean

lean_all/lang/LucChar.lean

/-
  lang/LucChar — T43, cycle 510: geo(1 + v, ℓ) = v^(ℓ−1) in every commutative ring of prime characteristic ℓ (step P1 of
  ψ_ℓ(x) ≡ (x − 2)^((ℓ−1)/2) (mod ℓ)).

    TS v n m        Σ_{k<m} C(n, k+1)·v^k
    TS_succ         T(n+1, m+1) = T(n, m+1) + v·T(n, m) + 1        (Pascal)
    geo_TS          geo(1 + v, n) = T(n, n)
    TS_char_low     ℓ·1 = 0, m < ℓ ⇒ T(ℓ, m) = 0                    (ℓ | C(ℓ, k+1), Frobenius.dv_choose)
    geo_char        geo(1 + v, ℓ) = v^(ℓ−1)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Cheb
import lang.ChebAll

namespace Cheb

open FTAChain Frobenius

variable {R : Type} (K : DetChain.CRing R)

def TS (v : R) (n : Nat) : Nat → R
  | 0 => K.zero
  | m + 1 => K.add (TS v n m) (nsm K (Frobenius.C n (m + 1)) (Frobenius.pw K v m))

theorem TS_succ (v : R) (n : Nat) : ∀ m,
    TS K v (n + 1) (m + 1) = K.add (K.add (TS K v n (m + 1)) (K.mul v (TS K v n m))) K.one
  | 0 => by
    show K.add K.zero (nsm K (Frobenius.C n 0 + Frobenius.C n 1) (Frobenius.pw K v 0)) =
      K.add (K.add (K.add K.zero (nsm K (Frobenius.C n 1) (Frobenius.pw K v 0))) (K.mul v K.zero)) K.one
    rw [C_zero_right, nsm_add, nsm_one]
    show K.add K.zero (K.add K.one (nsm K (Frobenius.C n 1) K.one)) =
      K.add (K.add (K.add K.zero (nsm K (Frobenius.C n 1) K.one)) (K.mul v K.zero)) K.one
    generalize nsm K (Frobenius.C n 1) K.one = A
    lib_ring
  | m + 1 => by
    show K.add (TS K v (n + 1) (m + 1)) (nsm K (Frobenius.C n (m + 1) + Frobenius.C n (m + 1 + 1)) (Frobenius.pw K v (m + 1))) =
      K.add (K.add (K.add (TS K v n (m + 1)) (nsm K (Frobenius.C n (m + 1 + 1)) (Frobenius.pw K v (m + 1))))
        (K.mul v (K.add (TS K v n m) (nsm K (Frobenius.C n (m + 1)) (Frobenius.pw K v m))))) K.one
    rw [TS_succ v n m, nsm_add]
    have e2 : K.mul v (nsm K (Frobenius.C n (m + 1)) (Frobenius.pw K v m)) = nsm K (Frobenius.C n (m + 1)) (Frobenius.pw K v (m + 1)) := by
      rw [mul_nsm]; exact congrArg (nsm K (Frobenius.C n (m + 1))) (K.mul_comm _ _)
    rw [← e2]
    generalize nsm K (Frobenius.C n (m + 1)) (Frobenius.pw K v m) = A
    generalize nsm K (Frobenius.C n (m + 1 + 1)) (Frobenius.pw K v (m + 1)) = B
    lib_ring

theorem geo_TS (v : R) : ∀ n, geo K (K.add K.one v) n = TS K v n n
  | 0 => rfl
  | n + 1 => by
    have hs := geo_shift K (K.add K.one v) n
    show K.add (geo K (K.add K.one v) n) (Frobenius.pw K (K.add K.one v) n) = _
    rw [← hs, geo_TS v n, TS_succ]
    show _ = K.add (K.add (K.add (TS K v n n) (nsm K (Frobenius.C n (n + 1)) (Frobenius.pw K v n))) (K.mul v (TS K v n n))) K.one
    rw [C_above']
    show _ = K.add (K.add (K.add (TS K v n n) K.zero) (K.mul v (TS K v n n))) K.one
    lib_ring

theorem TS_char_low (v : R) (j : Nat) (hpr : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1)
    (hp : nsm K (j + 1 + 1) K.one = K.zero) : ∀ m, m ≤ j + 1 → TS K v (j + 1 + 1) m = K.zero
  | 0, _ => rfl
  | m + 1, hm => by
    show K.add (TS K v (j + 1 + 1) m) (nsm K (Frobenius.C (j + 1 + 1) (m + 1)) (Frobenius.pw K v m)) = K.zero
    rw [TS_char_low v j hpr hp m (Nat.le_trans (Nat.le_succ m) hm)]
    obtain ⟨e, he⟩ := le_dest2 (m + 1) (j + 1) hm
    obtain ⟨q, hq⟩ := dv_choose j m hpr ⟨e, by rw [← he]; lib_nat⟩
    rw [hq, nsm_char K (j + 1 + 1) q hp]
    lib_ring

theorem geo_char (v : R) (j : Nat) (hpr : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1)
    (hp : nsm K (j + 1 + 1) K.one = K.zero) : geo K (K.add K.one v) (j + 1 + 1) = Frobenius.pw K v (j + 1) := by
  rw [geo_TS]
  show K.add (TS K v (j + 1 + 1) (j + 1)) (nsm K (Frobenius.C (j + 1 + 1) (j + 1 + 1)) (Frobenius.pw K v (j + 1))) = _
  rw [TS_char_low K v j hpr hp (j + 1) (Nat.le_refl _), C_self, nsm_one]
  lib_ring

end Cheb

#print axioms Cheb.TS_succ
#print axioms Cheb.geo_TS
#print axioms Cheb.TS_char_low
#print axioms Cheb.geo_char

lang/LucChar2.lean

lean_all/lang/LucChar2.lean

/-
  lang/LucChar2 — T43, cycle 511: **f_d(x) = (x − 2)^d in every commutative ring of prime characteristic ℓ = 2d + 1**
  (f_d = 1 + C_1 + … + C_d, the minimal polynomial of 2cos(2π/ℓ): ψ_ℓ ≡ (x − 2)^((ℓ−1)/2) (mod ℓ)).

  In Q = K[s]/(s² − (x² − 4)) take h = (d + 1)·1 (2h = ℓ + 1 = 1), z = (hx, h), w = (hx, −h): zw = 1, z + w = x. Cheb.phi_fd gives
  z^d f_d(x) = geo(z, ℓ) = (z − 1)^(ℓ−1) (LucChar.geo_char) = ((z − 1)²)^d = (z(x − 2))^d; cancel z^d with w^d.
    qe_mul, nsm_e, pw_e, C_e, fd_e      the embedding a ↦ (a, 0) into Q
    fd_char                            f_d(x) = (x − 2)^d
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.LucChar
import lang.Quad
import lang.TwoPower

namespace Cheb

open FTAChain Frobenius

variable {R : Type} (K : DetChain.CRing R) (D : R)

theorem qe_mul (a b : R) : (Quad.quad K D).mul (a, K.zero) (b, K.zero) = (K.mul a b, K.zero) := by
  apply Prod.ext
  · show K.add (K.mul a b) (K.mul D (K.mul K.zero K.zero)) = K.mul a b; lib_ring
  · show K.add (K.mul a K.zero) (K.mul K.zero b) = K.zero; lib_ring

theorem qe_add (a b : R) : (Quad.quad K D).add (a, K.zero) (b, K.zero) = (K.add a b, K.zero) :=
  Prod.ext rfl (by show K.add K.zero K.zero = K.zero; lib_ring)

theorem qe_neg (a : R) : (Quad.quad K D).neg (a, K.zero) = (K.neg a, K.zero) :=
  Prod.ext rfl (by show K.neg K.zero = K.zero; lib_ring)

theorem nsm_e (a : R) : ∀ n, nsm (Quad.quad K D) n (a, K.zero) = (nsm K n a, K.zero)
  | 0 => rfl
  | n + 1 => by
    show (Quad.quad K D).add (nsm (Quad.quad K D) n (a, K.zero)) (a, K.zero) = _
    rw [nsm_e a n, qe_add]; rfl

theorem pw_e (a : R) : ∀ n, Frobenius.pw (Quad.quad K D) (a, K.zero) n = (Frobenius.pw K a n, K.zero)
  | 0 => rfl
  | n + 1 => by
    show (Quad.quad K D).mul (Frobenius.pw (Quad.quad K D) (a, K.zero) n) (a, K.zero) = _
    rw [pw_e a n, qe_mul]; rfl

theorem C_e (a : R) : ∀ k, C (Quad.quad K D) (a, K.zero) k = (C K a k, K.zero) ∧
    C (Quad.quad K D) (a, K.zero) (k + 1) = (C K a (k + 1), K.zero)
  | 0 => ⟨by show (Quad.quad K D).add (Quad.quad K D).one (Quad.quad K D).one = _; exact qe_add K D _ _, rfl⟩
  | k + 1 => by
    obtain ⟨h0, h1⟩ := C_e a k
    refine ⟨h1, ?_⟩
    show (Quad.quad K D).add ((Quad.quad K D).mul (a, K.zero) (C (Quad.quad K D) (a, K.zero) (k + 1)))
      ((Quad.quad K D).neg (C (Quad.quad K D) (a, K.zero) k)) = _
    rw [h0, h1, qe_mul, qe_neg, qe_add]; rfl

theorem fd_e (a : R) : ∀ d, fd (Quad.quad K D) (a, K.zero) d = (fd K a d, K.zero)
  | 0 => rfl
  | d + 1 => by
    show (Quad.quad K D).add (fd (Quad.quad K D) (a, K.zero) d) (C (Quad.quad K D) (a, K.zero) (d + 1)) = _
    rw [fd_e a d, (C_e K D a d).2, qe_add]; rfl

theorem fd_char (j d : Nat) (hpr : ∀ e, Dv e (j + 1 + 1) → e = 1 ∨ e = j + 1 + 1) (hjd : j + 1 + 1 = 2 * d + 1)
    (hp : nsm K (j + 1 + 1) K.one = K.zero) (x : R) :
    fd K x d = Frobenius.pw K (K.add x (K.neg (K.add K.one K.one))) d := by
  -- h = (d + 1)·1, 2h = 1
  have h2 : K.add (nsm K (d + 1) K.one) (nsm K (d + 1) K.one) = K.one := by
    rw [← nsm_add, show d + 1 + (d + 1) = (j + 1 + 1) + 1 by rw [hjd]; lib_nat, nsm_add, hp, nsm_one]; lib_ring
  generalize nsm K (d + 1) K.one = h at h2
  generalize hDD : K.add (K.mul x x) (K.neg (K.mul (K.add K.one K.one) (K.add K.one K.one))) = DD
  have hzw : (Quad.quad K DD).mul (K.mul h x, h) (K.mul h x, K.neg h) = (Quad.quad K DD).one := by
    apply Prod.ext
    · show K.add (K.mul (K.mul h x) (K.mul h x)) (K.mul DD (K.mul h (K.neg h))) = K.one
      rw [← hDD]
      calc _ = K.mul (K.add h h) (K.add h h) := by lib_ring
        _ = K.one := by rw [h2]; lib_ring
    · show K.add (K.mul (K.mul h x) (K.neg h)) (K.mul h (K.mul h x)) = K.zero; lib_ring
  have hsum : (Quad.quad K DD).add (K.mul h x, h) (K.mul h x, K.neg h) = (x, K.zero) := by
    apply Prod.ext
    · show K.add (K.mul h x) (K.mul h x) = x
      calc _ = K.mul x (K.add h h) := by lib_ring
        _ = x := by rw [h2]; lib_ring
    · show K.add h (K.neg h) = K.zero; lib_ring
  have hpQ : nsm (Quad.quad K DD) (j + 1 + 1) (Quad.quad K DD).one = (Quad.quad K DD).zero := by
    show nsm (Quad.quad K DD) (j + 1 + 1) (K.one, K.zero) = _
    rw [nsm_e, hp]; rfl
  have hphi := phi_fd (Quad.quad K DD) _ _ hzw d
  rw [hsum, fd_e, ← hjd] at hphi
  generalize ((K.mul h x, h) : R × R) = z at hzw hsum hphi
  generalize ((K.mul h x, K.neg h) : R × R) = w at hzw hsum
  have hz1 : z = (Quad.quad K DD).add (Quad.quad K DD).one ((Quad.quad K DD).add z ((Quad.quad K DD).neg (Quad.quad K DD).one)) := by
    lib_ring
  rw [hz1, geo_char (Quad.quad K DD) _ j hpr hpQ, ← hz1] at hphi
  have hj1 : j + 1 = 2 * d := Nat.succ.inj hjd
  rw [hj1, ← TwoPower.pwpow] at hphi
  have hvv : Frobenius.pw (Quad.quad K DD) ((Quad.quad K DD).add z ((Quad.quad K DD).neg (Quad.quad K DD).one)) 2 =
      (Quad.quad K DD).mul z ((Quad.quad K DD).add (x, K.zero) ((Quad.quad K DD).neg ((Quad.quad K DD).add (Quad.quad K DD).one (Quad.quad K DD).one))) := by
    rw [← hsum]
    show (Quad.quad K DD).mul ((Quad.quad K DD).mul (Quad.quad K DD).one _) _ = _
    calc _ = (Quad.quad K DD).add ((Quad.quad K DD).mul z ((Quad.quad K DD).add ((Quad.quad K DD).add z w)
          ((Quad.quad K DD).neg ((Quad.quad K DD).add (Quad.quad K DD).one (Quad.quad K DD).one))))
          ((Quad.quad K DD).add (Quad.quad K DD).one ((Quad.quad K DD).neg ((Quad.quad K DD).mul z w))) := by lib_ring
      _ = _ := by rw [hzw]; lib_ring
  have hx2 : (Quad.quad K DD).add (x, K.zero) ((Quad.quad K DD).neg ((Quad.quad K DD).add (Quad.quad K DD).one (Quad.quad K DD).one)) =
      (K.add x (K.neg (K.add K.one K.one)), K.zero) := by
    show (Quad.quad K DD).add (x, K.zero) ((Quad.quad K DD).neg ((Quad.quad K DD).add (K.one, K.zero) (K.one, K.zero))) = _
    rw [qe_add, qe_neg, qe_add]
  rw [hvv, hx2, Frobenius.pw_mul, pw_e] at hphi
  have hzwd : (Quad.quad K DD).mul (Frobenius.pw (Quad.quad K DD) w d) (Frobenius.pw (Quad.quad K DD) z d) = (Quad.quad K DD).one := by
    rw [← Frobenius.pw_mul, (Quad.quad K DD).mul_comm w z, hzw]; exact TwoPower.pwone _ d
  have hfin : (fd K x d, K.zero) = (Frobenius.pw K (K.add x (K.neg (K.add K.one K.one))) d, K.zero) := by
    calc (fd K x d, K.zero) = (Quad.quad K DD).mul ((Quad.quad K DD).mul (Frobenius.pw (Quad.quad K DD) w d) (Frobenius.pw (Quad.quad K DD) z d))
          (fd K x d, K.zero) := by rw [hzwd]; lib_ring
      _ = (Quad.quad K DD).mul (Frobenius.pw (Quad.quad K DD) w d)
          ((Quad.quad K DD).mul (Frobenius.pw (Quad.quad K DD) z d) (fd K x d, K.zero)) := by lib_ring
      _ = (Quad.quad K DD).mul (Frobenius.pw (Quad.quad K DD) w d)
          ((Quad.quad K DD).mul (Frobenius.pw (Quad.quad K DD) z d) (Frobenius.pw K (K.add x (K.neg (K.add K.one K.one))) d, K.zero)) := by rw [hphi]
      _ = (Quad.quad K DD).mul ((Quad.quad K DD).mul (Frobenius.pw (Quad.quad K DD) w d) (Frobenius.pw (Quad.quad K DD) z d))
          (Frobenius.pw K (K.add x (K.neg (K.add K.one K.one))) d, K.zero) := by lib_ring
      _ = _ := by rw [hzwd]; lib_ring
  exact congrArg Prod.fst hfin

end Cheb

#print axioms Cheb.qe_mul
#print axioms Cheb.qe_add
#print axioms Cheb.qe_neg
#print axioms Cheb.nsm_e
#print axioms Cheb.pw_e
#print axioms Cheb.C_e
#print axioms Cheb.fd_e
#print axioms Cheb.fd_char

lang/LucFrobC.lean

lean_all/lang/LucFrobC.lean

/-
  lang/LucFrobC — T43, cycle 519: C_r(x) = x^r in every commutative ring of odd prime characteristic r
  (z + 1/z = x in K[√(x² − 4)], C_r(x) = z^r + w^r = (z + w)^r by the Frobenius).

    cr_char     C_r(x) = x^r
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.LucChar2

namespace Cheb

open FTAChain Frobenius

variable {R : Type} (K : DetChain.CRing R)

theorem cr_char (j d : Nat) (hpr : ∀ e, Dv e (j + 1 + 1) → e = 1 ∨ e = j + 1 + 1) (hjd : j + 1 + 1 = 2 * d + 1)
    (hp : nsm K (j + 1 + 1) K.one = K.zero) (x : R) :
    C K x (j + 1 + 1) = Frobenius.pw K x (j + 1 + 1) := by
  have h2 : K.add (nsm K (d + 1) K.one) (nsm K (d + 1) K.one) = K.one := by
    rw [← nsm_add, show d + 1 + (d + 1) = (j + 1 + 1) + 1 by rw [hjd]; lib_nat, nsm_add, hp, nsm_one]; lib_ring
  generalize nsm K (d + 1) K.one = h at h2
  generalize hDD : K.add (K.mul x x) (K.neg (K.mul (K.add K.one K.one) (K.add K.one K.one))) = DD
  have hzw : (Quad.quad K DD).mul (K.mul h x, h) (K.mul h x, K.neg h) = (Quad.quad K DD).one := by
    apply Prod.ext
    · show K.add (K.mul (K.mul h x) (K.mul h x)) (K.mul DD (K.mul h (K.neg h))) = K.one
      rw [← hDD]
      calc _ = K.mul (K.add h h) (K.add h h) := by lib_ring
        _ = K.one := by rw [h2]; lib_ring
    · show K.add (K.mul (K.mul h x) (K.neg h)) (K.mul h (K.mul h x)) = K.zero; lib_ring
  have hsum : (Quad.quad K DD).add (K.mul h x, h) (K.mul h x, K.neg h) = (x, K.zero) := by
    apply Prod.ext
    · show K.add (K.mul h x) (K.mul h x) = x
      calc _ = K.mul x (K.add h h) := by lib_ring
        _ = x := by rw [h2]; lib_ring
    · show K.add h (K.neg h) = K.zero; lib_ring
  have hpQ : nsm (Quad.quad K DD) (j + 1 + 1) (Quad.quad K DD).one = (Quad.quad K DD).zero := by
    show nsm (Quad.quad K DD) (j + 1 + 1) (K.one, K.zero) = _
    rw [nsm_e, hp]; rfl
  have hc := cheb (Quad.quad K DD) _ _ hzw (j + 1 + 1)
  rw [← frob_add (Quad.quad K DD) j hpr hpQ, hsum, pw_e] at hc
  have hCe := (C_e K DD x (j + 1)).2
  rw [hCe] at hc
  exact (congrArg Prod.fst hc).symm

end Cheb

#print axioms Cheb.cr_char

lang/LucGauss.lean

lean_all/lang/LucGauss.lean

/-
  lang/LucGauss — T43, cycle 514: f_d along ring maps, and the Gaussian evaluation (step K2 for r ≡ 1 mod 4).

    C_map, fd_map    a map preserving +, ·, −, 1 commutes with C_k and f_d
    gmap             (a, b) ↦ a + ιb from K[i] (Cyclo4.cyc4) to K preserves +, ·, −, 1 when ι² = −1
    gauss_root       ι² = −1, f_d(n + i) = (A, B) in K[i], A² + B² = 0 in a ring without zero divisors
                     ⇒ f_d(n + ι) = 0 or f_d(n − ι) = 0 in K
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Cheb
import lang.Cyclo4

namespace Cheb

open FTAChain

theorem C_map {R S : Type} (K : DetChain.CRing R) (L : DetChain.CRing S) (φ : R → S)
    (hadd : ∀ a b, φ (K.add a b) = L.add (φ a) (φ b)) (hmul : ∀ a b, φ (K.mul a b) = L.mul (φ a) (φ b))
    (hneg : ∀ a, φ (K.neg a) = L.neg (φ a)) (hone : φ K.one = L.one) (s : R) :
    ∀ k, φ (C K s k) = C L (φ s) k ∧ φ (C K s (k + 1)) = C L (φ s) (k + 1)
  | 0 => ⟨by show φ (K.add K.one K.one) = L.add L.one L.one; rw [hadd, hone], rfl⟩
  | k + 1 => by
    obtain ⟨h0, h1⟩ := C_map K L φ hadd hmul hneg hone s k
    refine ⟨h1, ?_⟩
    show φ (K.add (K.mul s (C K s (k + 1))) (K.neg (C K s k))) = L.add (L.mul (φ s) (C L (φ s) (k + 1))) (L.neg (C L (φ s) k))
    rw [hadd, hmul, hneg, h0, h1]

theorem fd_map {R S : Type} (K : DetChain.CRing R) (L : DetChain.CRing S) (φ : R → S)
    (hadd : ∀ a b, φ (K.add a b) = L.add (φ a) (φ b)) (hmul : ∀ a b, φ (K.mul a b) = L.mul (φ a) (φ b))
    (hneg : ∀ a, φ (K.neg a) = L.neg (φ a)) (hone : φ K.one = L.one) (s : R) :
    ∀ d, φ (fd K s d) = fd L (φ s) d
  | 0 => hone
  | d + 1 => by
    show φ (K.add (fd K s d) (C K s (d + 1))) = L.add (fd L (φ s) d) (C L (φ s) (d + 1))
    rw [hadd, fd_map K L φ hadd hmul hneg hone s d, (C_map K L φ hadd hmul hneg hone s d).2]

variable {R : Type} (K : DetChain.CRing R)

def gmap (ι : R) (x : R × R) : R := K.add x.1 (K.mul ι x.2)

theorem gmap_mul (ι : R) (hι : K.mul ι ι = K.neg K.one) (x y : R × R) :
    gmap K ι ((Cyclo4.cyc4 K).mul x y) = K.mul (gmap K ι x) (gmap K ι y) := by
  show K.add (K.add (K.mul x.1 y.1) (K.neg (K.mul x.2 y.2))) (K.mul ι (K.add (K.mul x.1 y.2) (K.mul x.2 y.1))) = _
  show _ = K.mul (K.add x.1 (K.mul ι x.2)) (K.add y.1 (K.mul ι y.2))
  calc _ = K.add (K.mul (K.add x.1 (K.mul ι x.2)) (K.add y.1 (K.mul ι y.2)))
        (K.mul (K.mul x.2 y.2) (K.add (K.neg K.one) (K.neg (K.mul ι ι)))) := by lib_ring
    _ = _ := by rw [hι]; lib_ring

theorem gauss_root (hnz : ∀ a b : R, K.mul a b = K.zero → a = K.zero ∨ b = K.zero)
    (ι n : R) (hι : K.mul ι ι = K.neg K.one) (d : Nat)
    (h0 : K.add (K.mul (fd (Cyclo4.cyc4 K) (n, K.one) d).1 (fd (Cyclo4.cyc4 K) (n, K.one) d).1)
      (K.mul (fd (Cyclo4.cyc4 K) (n, K.one) d).2 (fd (Cyclo4.cyc4 K) (n, K.one) d).2) = K.zero) :
    fd K (K.add n ι) d = K.zero ∨ fd K (K.add n (K.neg ι)) d = K.zero := by
  have hι' : K.mul (K.neg ι) (K.neg ι) = K.neg K.one := by rw [← hι]; lib_ring
  have hadd : ∀ (j : R) (x y : R × R), gmap K j ((Cyclo4.cyc4 K).add x y) = K.add (gmap K j x) (gmap K j y) := fun j x y => by
    show K.add (K.add x.1 y.1) (K.mul j (K.add x.2 y.2)) = K.add (K.add x.1 (K.mul j x.2)) (K.add y.1 (K.mul j y.2)); lib_ring
  have hneg : ∀ (j : R) (x : R × R), gmap K j ((Cyclo4.cyc4 K).neg x) = K.neg (gmap K j x) := fun j x => by
    show K.add (K.neg x.1) (K.mul j (K.neg x.2)) = K.neg (K.add x.1 (K.mul j x.2)); lib_ring
  have hone : ∀ j : R, gmap K j (Cyclo4.cyc4 K).one = K.one := fun j => by
    show K.add K.one (K.mul j K.zero) = K.one; lib_ring
  have e1 := fd_map (Cyclo4.cyc4 K) K (gmap K ι) (hadd ι) (gmap_mul K ι hι) (hneg ι) (hone ι) (n, K.one) d
  have e2 := fd_map (Cyclo4.cyc4 K) K (gmap K (K.neg ι)) (hadd _) (gmap_mul K _ hι') (hneg _) (hone _) (n, K.one) d
  have g1 : gmap K ι (n, K.one) = K.add n ι := by show K.add n (K.mul ι K.one) = _; lib_ring
  have g2 : gmap K (K.neg ι) (n, K.one) = K.add n (K.neg ι) := by show K.add n (K.mul (K.neg ι) K.one) = _; lib_ring
  rw [g1] at e1; rw [g2] at e2
  generalize fd (Cyclo4.cyc4 K) (n, K.one) d = P at h0 e1 e2
  have hprod : K.mul (gmap K ι P) (gmap K (K.neg ι) P) = K.zero := by
    show K.mul (K.add P.1 (K.mul ι P.2)) (K.add P.1 (K.mul (K.neg ι) P.2)) = K.zero
    rw [← h0]
    calc _ = K.add (K.add (K.mul P.1 P.1) (K.mul P.2 P.2)) (K.mul (K.mul P.2 P.2) (K.add (K.neg (K.mul ι ι)) (K.neg K.one))) := by lib_ring
      _ = _ := by rw [hι]; lib_ring
  rcases hnz _ _ hprod with h | h
  · exact Or.inl (e1.symm.trans h)
  · exact Or.inr (e2.symm.trans h)

end Cheb

#print axioms Cheb.C_map
#print axioms Cheb.fd_map
#print axioms Cheb.gmap_mul
#print axioms Cheb.gauss_root

lang/LucGaussZ.lean

lean_all/lang/LucGaussZ.lean

/-
  lang/LucGaussZ — T43, cycle 515: the integer Gaussian value of f_d and its reduction mod r (step K1, and K2 for r ≡ 1 mod 4).

  P = f_d(n + i) ∈ ℤ[i] (Cyclo4.cyc4 intR), F = P₁² + P₂².
    cpair            (a, b) ↦ (castZ a, castZ b) is a ring map ℤ[i] → K[i]
    red_sum          r | F (F = N ∈ ℕ) ⇒ Q₁² + Q₂² = 0 in 𝔽_r for Q = f_d(n̄ + i) ∈ 𝔽_r[i]
    k2_one           r ≡ 1 (mod 4) prime, r | F ⇒ f_d(n̄ ± ι) = 0 in 𝔽_r for some ι with ι² = −1
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.LucGauss
import lang.HasseDavenport
import lang.IntCast
import lang.EulerHard

namespace Cheb

open FTAChain ZMod LibRing LibInt

variable {R : Type} (K : DetChain.CRing R)

def cpair (x : Int × Int) : R × R := (castZ K x.1, castZ K x.2)

theorem cpair_add (x y : Int × Int) : cpair K ((Cyclo4.cyc4 intR).add x y) = (Cyclo4.cyc4 K).add (cpair K x) (cpair K y) :=
  Prod.ext (IntCast.castZ_add K x.1 y.1) (IntCast.castZ_add K x.2 y.2)

theorem cpair_neg (x : Int × Int) : cpair K ((Cyclo4.cyc4 intR).neg x) = (Cyclo4.cyc4 K).neg (cpair K x) :=
  Prod.ext (HasseDavenport.castZ_neg K x.1) (HasseDavenport.castZ_neg K x.2)

theorem cpair_mul (x y : Int × Int) : cpair K ((Cyclo4.cyc4 intR).mul x y) = (Cyclo4.cyc4 K).mul (cpair K x) (cpair K y) := by
  apply Prod.ext
  · show castZ K (x.1 * y.1 + -(x.2 * y.2)) = K.add (K.mul (castZ K x.1) (castZ K y.1)) (K.neg (K.mul (castZ K x.2) (castZ K y.2)))
    rw [IntCast.castZ_add, HasseDavenport.castZ_neg, HasseDavenport.castZ_mul, HasseDavenport.castZ_mul]
  · show castZ K (x.1 * y.2 + x.2 * y.1) = K.add (K.mul (castZ K x.1) (castZ K y.2)) (K.mul (castZ K x.2) (castZ K y.1))
    rw [IntCast.castZ_add, HasseDavenport.castZ_mul, HasseDavenport.castZ_mul]

theorem cpair_one : cpair K (Cyclo4.cyc4 intR).one = (Cyclo4.cyc4 K).one :=
  Prod.ext (show K.add K.one K.zero = K.one by lib_ring) rfl

theorem red_sum (m n d N : Nat)
    (hN : (fd (Cyclo4.cyc4 intR) (Int.ofNat n, 1) d).1 * (fd (Cyclo4.cyc4 intR) (Int.ofNat n, 1) d).1 +
      (fd (Cyclo4.cyc4 intR) (Int.ofNat n, 1) d).2 * (fd (Cyclo4.cyc4 intR) (Int.ofNat n, 1) d).2 = Int.ofNat N)
    (hr : Dv (m + 1) N) :
    (ring m).add ((ring m).mul (fd (Cyclo4.cyc4 (ring m)) (castN (ring m) n, (ring m).one) d).1 (fd (Cyclo4.cyc4 (ring m)) (castN (ring m) n, (ring m).one) d).1)
      ((ring m).mul (fd (Cyclo4.cyc4 (ring m)) (castN (ring m) n, (ring m).one) d).2 (fd (Cyclo4.cyc4 (ring m)) (castN (ring m) n, (ring m).one) d).2) = (ring m).zero := by
  have hmap := fd_map (Cyclo4.cyc4 intR) (Cyclo4.cyc4 (ring m)) (cpair (ring m)) (cpair_add _) (cpair_mul _) (cpair_neg _) (cpair_one _) (Int.ofNat n, 1) d
  have hc : cpair (ring m) ((Int.ofNat n, 1) : Int × Int) = (castN (ring m) n, (ring m).one) :=
    Prod.ext rfl (show (ring m).add (ring m).one (ring m).zero = (ring m).one by lib_ring)
  rw [hc] at hmap
  rw [← hmap]
  generalize fd (Cyclo4.cyc4 intR) (Int.ofNat n, 1) d = P at hN
  show (ring m).add ((ring m).mul (castZ (ring m) P.1) (castZ (ring m) P.1)) ((ring m).mul (castZ (ring m) P.2) (castZ (ring m) P.2)) = _
  rw [← HasseDavenport.castZ_mul, ← HasseDavenport.castZ_mul, ← IntCast.castZ_add, hN]
  show castN (ring m) N = _
  rw [IntCast.castN_mk]
  exact Subtype.ext (dv_md_zero m N hr)

theorem k2_one (m n d N : Nat) (hpr : ∀ e, Dv e (2 * m + 1 + 1 + 1) → e = 1 ∨ e = 2 * m + 1 + 1 + 1)
    (h4 : ∃ k, m + 1 = 2 * k)
    (hN : (fd (Cyclo4.cyc4 intR) (Int.ofNat n, 1) d).1 * (fd (Cyclo4.cyc4 intR) (Int.ofNat n, 1) d).1 +
      (fd (Cyclo4.cyc4 intR) (Int.ofNat n, 1) d).2 * (fd (Cyclo4.cyc4 intR) (Int.ofNat n, 1) d).2 = Int.ofNat N)
    (hr : Dv (2 * m + 1 + 1 + 1) N) :
    ∃ ι : Zp (2 * m + 1 + 1), (ring (2 * m + 1 + 1)).mul ι ι = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one ∧
      (fd (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).add (castN (ring (2 * m + 1 + 1)) n) ι) d = (ring (2 * m + 1 + 1)).zero ∨
       fd (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).add (castN (ring (2 * m + 1 + 1)) n) ((ring (2 * m + 1 + 1)).neg ι)) d = (ring (2 * m + 1 + 1)).zero) := by
  obtain ⟨k, hk⟩ := h4
  have hm1 : Frobenius.pw (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) (m + 1) = (ring (2 * m + 1 + 1)).one := by
    rw [hk, ← TwoPower.pwpow]
    have e : Frobenius.pw (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) 2 = (ring (2 * m + 1 + 1)).one := by
      show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one _) _ = _; lib_ring
    rw [e]; exact TwoPower.pwone _ k
  obtain ⟨ι, hι⟩ := EulerHard.euler_hard m hpr _ hm1
  exact ⟨ι, hι, gauss_root (ring (2 * m + 1 + 1)) (ZMod.nozd (2 * m + 1) hpr) _ _ hι d (red_sum (2 * m + 1 + 1) n d N hN hr)⟩

end Cheb

#print axioms Cheb.cpair_add
#print axioms Cheb.cpair_neg
#print axioms Cheb.cpair_mul
#print axioms Cheb.cpair_one
#print axioms Cheb.red_sum
#print axioms Cheb.k2_one

lang/LucInv.lean

lean_all/lang/LucInv.lean

/-
  lang/LucInv — T43, cycle 499: the Lucas invariant (PhiInv with U_n(s), V_q(s) and ranks of apparition).

  LInv(n, t, F) :⇔ for every prime r and k ≥ 1:  r^k | F ⇔ (rank_r(s) = n ∧ r^k | U_n(s)),   s = t + 2.
    rank_uniq   two ranks of the same r are equal
    linv_base   q prime ⇒ LInv(q, t, U_q(s))
    lstep_dv    LInv(n, t, F₀), LInv(n, t′, F₁), V_q(s) = t′ + 2, q prime, q ∤ n ⇒ F₀ | F₁
  Unlike PhiInv no hypothesis q | y is needed: rank 1 never occurs (U_1 = 1).
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.LucRankV
import lang.ChebPowInf
import lang.Val

namespace Cheb

open FTAChain

def LInv (n t F : Nat) : Prop :=
  ∀ l k, (∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1) →
    (Dv ((l + 1 + 1) ^ (k + 1)) F ↔ (IsRank t l n ∧ Dv ((l + 1 + 1) ^ (k + 1)) (lu t n).1))

theorem rank_uniq (t l a b : Nat) (ha : 1 ≤ a) (hb : 1 ≤ b) (hA : IsRank t l a) (hB : IsRank t l b) : a = b :=
  dv_eq a b ha hb (hA.2 _ hB.1) (hB.2 _ hA.1)

theorem linv_base (t j : Nat) (hq : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1) :
    LInv (j + 1 + 1) t (lu t (j + 1 + 1)).1 := fun l k hl =>
  ⟨fun h => ⟨rank_prime t l j hl hq (dv_trans _ _ _ ⟨(l + 1 + 1) ^ k, rfl⟩ h), h⟩, fun h => h.2⟩

theorem lstep_dv (t t' j n F0 F1 : Nat) (hq : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1)
    (hv : (cd t (j + 1 + 1)).1 = t' + 2) (hqn : Dv (j + 1 + 1) n → False) (hn : 1 ≤ n) (hF0 : 1 ≤ F0)
    (I0 : LInv n t F0) (I1 : LInv n t' F1) : Dv F0 F1 := by
  refine Val.dv_of_local F0 F0 F1 (Nat.le_refl F0) hF0 (fun l k hr hk => ?_)
  match k, hk with
  | 0, _ => exact ⟨F1, (Nat.mul_one F1).symm⟩
  | k + 1, hk =>
    obtain ⟨hR, hg⟩ := (I0 l k hr).1 hk
    have hrq : Dv (l + 1 + 1) (lu t (j + 1 + 1)).1 → False := fun h => by
      have e := rank_uniq t l n (j + 1 + 1) hn (Nat.le_add_left 1 (j + 1)) hR (rank_prime t l j hr hq h)
      exact hqn ⟨1, by rw [e]; lib_nat⟩
    have hR1 := rankV_pow t t' l j n hr hq hv hrq hn hqn hR
    obtain ⟨t'', ht''⟩ := c_ge t n
    have hsplit1 := lu_mul t t' (j + 1 + 1) n hv
    have hsplit2 := lu_mul t t'' n (j + 1 + 1) ht''
    have hg2 : Dv ((l + 1 + 1) ^ (k + 1)) ((lu t (j + 1 + 1)).1 * (lu t' n).1) := by
      rw [← hsplit1, Nat.mul_comm, hsplit2]
      exact dv_trans _ _ _ hg ⟨(lu t'' (j + 1 + 1)).1, Nat.mul_comm _ _⟩
    have hg3 := Val.ppow_cancel l _ hr hrq (k + 1) _ hg2
    exact (I1 l k hr).2 ⟨hR1, hg3⟩

end Cheb

#print axioms Cheb.rank_uniq
#print axioms Cheb.linv_base
#print axioms Cheb.lstep_dv

lang/LucK5.lean

lean_all/lang/LucK5.lean

/-
  lang/LucK5 — T43, cycle 523: **K5′** — for ℓ ≡ 3 (mod 4) and a prime r ≡ 3 (mod 4), r ≠ ℓ, n + i is never a root of
  ψ_ℓ = f_d in 𝔽_r[i]. So no prime r ≡ 3 (mod 4) divides both A and B in ψ_ℓ(n + i) = A + Bi.

  d = (ℓ − 1)/2 is odd; Euler gives r^d ≡ ±1 (mod ℓ), so x^(r^d) = x (LucK5a.root_fix); but x^r = x̄ (LucK5b.conj_r) and d is odd,
  so x^(r^d) = x̄ = (n̄, −1), forcing 1 = −1 in 𝔽_r.
    conj_iter, k5
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.LucK5b
import lang.Fp2Sqrt

namespace Cheb

open FTAChain ZMod

variable (e : Nat)

local notation "𝔽" => ring (2 * (2 * e) + 1 + 1)
local notation "𝔾" => Cyclo4.cyc4 (ring (2 * (2 * e) + 1 + 1))

theorem conj_iter (hpr : ∀ d, Dv d (2 * (2 * e) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * e) + 1 + 1 + 1) (a b : Zp (2 * (2 * e) + 1 + 1)) :
    ∀ k, Frobenius.pw 𝔾 (a, b) ((2 * (2 * e) + 1 + 1 + 1) ^ (2 * k)) = (a, b) ∧
      Frobenius.pw 𝔾 (a, b) ((2 * (2 * e) + 1 + 1 + 1) ^ (2 * k + 1)) = (a, (𝔽).neg b)
  | 0 => ⟨by show (𝔾).mul (𝔾).one (a, b) = (a, b); lib_ring,
      by show Frobenius.pw 𝔾 (a, b) (1 * (2 * (2 * e) + 1 + 1 + 1)) = _; rw [Nat.one_mul]; exact conj_r e hpr a b⟩
  | k + 1 => by
    obtain ⟨_, h1⟩ := conj_iter hpr a b k
    have e0 : Frobenius.pw 𝔾 (a, b) ((2 * (2 * e) + 1 + 1 + 1) ^ (2 * (k + 1))) = (a, b) := by
      rw [show 2 * (k + 1) = 2 * k + 1 + 1 by lib_nat]
      show Frobenius.pw 𝔾 (a, b) ((2 * (2 * e) + 1 + 1 + 1) ^ (2 * k + 1) * (2 * (2 * e) + 1 + 1 + 1)) = _
      rw [← TwoPower.pwpow, h1, conj_r e hpr]
      exact Prod.ext rfl (show (𝔽).neg ((𝔽).neg b) = b by lib_ring)
    refine ⟨e0, ?_⟩
    show Frobenius.pw 𝔾 (a, b) ((2 * (2 * e) + 1 + 1 + 1) ^ (2 * (k + 1)) * (2 * (2 * e) + 1 + 1 + 1)) = _
    rw [← TwoPower.pwpow, e0, conj_r e hpr]

theorem k5 (c : Nat) (hpr : ∀ d, Dv d (2 * (2 * e) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * e) + 1 + 1 + 1)
    (hl : ∀ d, Dv d (2 * (2 * c) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * c) + 1 + 1 + 1)
    (hlr : Dv (2 * (2 * c) + 1 + 1 + 1) (2 * (2 * e) + 1 + 1 + 1) → False) (n : Zp (2 * (2 * e) + 1 + 1))
    (hf : fd 𝔾 (n, (𝔽).one) (2 * c + 1) = (𝔾).zero) : False := by
  have hp : Frobenius.nsm 𝔾 (2 * (2 * e) + 1 + 1 + 1) (𝔾).one = (𝔾).zero := by
    show Frobenius.nsm _ _ ((𝔽).one, (𝔽).zero) = _
    rw [c_nsm_e, char_p]; rfl
  have hk := euler_nat (2 * c) (2 * (2 * e) + 1 + 1 + 1) hl hlr
  rw [show 2 * (2 * c) + 1 + 1 + 1 = 2 * (2 * c + 1) + 1 by lib_nat, show 2 * c + 1 = 2 * c + 1 from rfl] at hk
  have hfix := root_fix 𝔾 (2 * (2 * e) + 1) (2 * c + 1) (2 * e + 1) hpr (by lib_nat) hp (n, (𝔽).one) hf (2 * c + 1) hk
  rw [(conj_iter e hpr n (𝔽).one c).2] at hfix
  have h1 : (𝔽).neg (𝔽).one = (𝔽).one := congrArg Prod.snd hfix
  apply Fp2Sqrt.two_ne (2 * e)
  calc (𝔽).add (𝔽).one (𝔽).one = (𝔽).add (𝔽).one ((𝔽).neg (𝔽).one) := by rw [h1]
    _ = (𝔽).zero := by lib_ring

end Cheb

#print axioms Cheb.conj_iter
#print axioms Cheb.k5

lang/LucK5a.lean

lean_all/lang/LucK5a.lean

/-
  lang/LucK5a — T43, cycle 520: roots of f_d are fixed by x ↦ x^(r^k) when r^k ≡ ±1 (mod ℓ)   (characteristic r, ℓ = 2d + 1).

    frob_iter    (a + b)^(r^k) = a^(r^k) + b^(r^k) in characteristic r
    root_fix     f_d(x) = 0, r^k = qℓ + 1 or r^k + 1 = qℓ  ⇒  x^(r^k) = x
  In Q = K[√(x² − 4)]: z + w = x, zw = 1, z^ℓ = 1 (phi_fd, ChebKey.ellth), so z^(r^k) + w^(r^k) = z + w.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.LucChar2
import lang.ChebKey
import lang.HasseCM

namespace Cheb

open FTAChain Frobenius

variable {R : Type} (K : DetChain.CRing R)

theorem frob_iter (j : Nat) (hpr : ∀ e, Dv e (j + 1 + 1) → e = 1 ∨ e = j + 1 + 1)
    (hp : nsm K (j + 1 + 1) K.one = K.zero) (a b : R) : ∀ k,
    Frobenius.pw K (K.add a b) ((j + 1 + 1) ^ k) = K.add (Frobenius.pw K a ((j + 1 + 1) ^ k)) (Frobenius.pw K b ((j + 1 + 1) ^ k))
  | 0 => by show K.mul K.one (K.add a b) = K.add (K.mul K.one a) (K.mul K.one b); lib_ring
  | k + 1 => by
    show Frobenius.pw K (K.add a b) ((j + 1 + 1) ^ k * (j + 1 + 1)) = K.add (Frobenius.pw K a ((j + 1 + 1) ^ k * (j + 1 + 1)))
      (Frobenius.pw K b ((j + 1 + 1) ^ k * (j + 1 + 1)))
    rw [← TwoPower.pwpow, ← TwoPower.pwpow, ← TwoPower.pwpow, frob_iter j hpr hp a b k, frob_add K j hpr hp]

theorem pw_qe1 (z : R) (l q : Nat) (hz : Frobenius.pw K z l = K.one) : Frobenius.pw K z (q * l) = K.one := by
  rw [Nat.mul_comm, ← TwoPower.pwpow, hz]; exact TwoPower.pwone K q

theorem root_fix (j d jl : Nat) (hpr : ∀ e, Dv e (j + 1 + 1) → e = 1 ∨ e = j + 1 + 1) (hjd : j + 1 + 1 = 2 * jl + 1)
    (hp : nsm K (j + 1 + 1) K.one = K.zero) (x : R) (hf : fd K x d = K.zero) (k : Nat)
    (hk : (∃ q, (j + 1 + 1) ^ k = q * (2 * d + 1) + 1) ∨ (∃ q, (j + 1 + 1) ^ k + 1 = q * (2 * d + 1))) :
    Frobenius.pw K x ((j + 1 + 1) ^ k) = x := by
  have h2 : K.add (nsm K (jl + 1) K.one) (nsm K (jl + 1) K.one) = K.one := by
    rw [← nsm_add, show jl + 1 + (jl + 1) = (j + 1 + 1) + 1 by rw [hjd]; lib_nat, nsm_add, hp, nsm_one]; lib_ring
  generalize nsm K (jl + 1) K.one = h at h2
  generalize hDD : K.add (K.mul x x) (K.neg (K.mul (K.add K.one K.one) (K.add K.one K.one))) = DD
  have hzw : (Quad.quad K DD).mul (K.mul h x, h) (K.mul h x, K.neg h) = (Quad.quad K DD).one := by
    apply Prod.ext
    · show K.add (K.mul (K.mul h x) (K.mul h x)) (K.mul DD (K.mul h (K.neg h))) = K.one
      rw [← hDD]
      calc _ = K.mul (K.add h h) (K.add h h) := by lib_ring
        _ = K.one := by rw [h2]; lib_ring
    · show K.add (K.mul (K.mul h x) (K.neg h)) (K.mul h (K.mul h x)) = K.zero; lib_ring
  have hsum : (Quad.quad K DD).add (K.mul h x, h) (K.mul h x, K.neg h) = (x, K.zero) := by
    apply Prod.ext
    · show K.add (K.mul h x) (K.mul h x) = x
      calc _ = K.mul x (K.add h h) := by lib_ring
        _ = x := by rw [h2]; lib_ring
    · show K.add h (K.neg h) = K.zero; lib_ring
  have hpQ : nsm (Quad.quad K DD) (j + 1 + 1) (Quad.quad K DD).one = (Quad.quad K DD).zero := by
    show nsm (Quad.quad K DD) (j + 1 + 1) (K.one, K.zero) = _
    rw [nsm_e, hp]; rfl
  have hphi := phi_fd (Quad.quad K DD) _ _ hzw d
  rw [hsum, fd_e, hf] at hphi
  have hgeo : geo (Quad.quad K DD) (K.mul h x, h) (2 * d + 1) = (Quad.quad K DD).zero := by
    rw [← hphi]; show (Quad.quad K DD).mul _ (Quad.quad K DD).zero = (Quad.quad K DD).zero; lib_ring
  have hzl := ellth (Quad.quad K DD) _ _ hgeo
  have hpe := frob_iter (Quad.quad K DD) j hpr hpQ (K.mul h x, h) (K.mul h x, K.neg h) k
  rw [hsum, pw_e] at hpe
  generalize ((K.mul h x, h) : R × R) = z at hzw hsum hzl hpe
  generalize ((K.mul h x, K.neg h) : R × R) = w at hzw hsum hpe
  have hwl : Frobenius.pw (Quad.quad K DD) w (2 * d + 1) = (Quad.quad K DD).one := by
    calc _ = (Quad.quad K DD).mul (Frobenius.pw (Quad.quad K DD) w (2 * d + 1)) (Frobenius.pw (Quad.quad K DD) z (2 * d + 1)) := by
          rw [hzl]; lib_ring
      _ = Frobenius.pw (Quad.quad K DD) ((Quad.quad K DD).mul w z) (2 * d + 1) := (Frobenius.pw_mul _ w z _).symm
      _ = _ := by rw [(Quad.quad K DD).mul_comm, hzw]; exact TwoPower.pwone _ _
  have hfin : (Frobenius.pw K x ((j + 1 + 1) ^ k), K.zero) = (x, K.zero) := by
    rw [hpe, ← hsum]
    rcases hk with ⟨q, hq⟩ | ⟨q, hq⟩
    · rw [hq, HasseCM.pw_add', HasseCM.pw_add', pw_qe1 _ z _ q hzl, pw_qe1 _ w _ q hwl]
      show (Quad.quad K DD).add ((Quad.quad K DD).mul _ ((Quad.quad K DD).mul (Quad.quad K DD).one z))
        ((Quad.quad K DD).mul _ ((Quad.quad K DD).mul (Quad.quad K DD).one w)) = _
      lib_ring
    · have ez : Frobenius.pw (Quad.quad K DD) z ((j + 1 + 1) ^ k) = w := by
        calc _ = (Quad.quad K DD).mul (Frobenius.pw (Quad.quad K DD) z ((j + 1 + 1) ^ k)) ((Quad.quad K DD).mul z w) := by rw [hzw]; lib_ring
          _ = (Quad.quad K DD).mul (Frobenius.pw (Quad.quad K DD) z ((j + 1 + 1) ^ k + 1)) w := by
              rw [HasseCM.pw_add']; show _ = (Quad.quad K DD).mul ((Quad.quad K DD).mul _ ((Quad.quad K DD).mul (Quad.quad K DD).one z)) w; lib_ring
          _ = w := by rw [hq, pw_qe1 _ z _ q hzl]; lib_ring
      have ew : Frobenius.pw (Quad.quad K DD) w ((j + 1 + 1) ^ k) = z := by
        calc _ = (Quad.quad K DD).mul (Frobenius.pw (Quad.quad K DD) w ((j + 1 + 1) ^ k)) ((Quad.quad K DD).mul z w) := by rw [hzw]; lib_ring
          _ = (Quad.quad K DD).mul (Frobenius.pw (Quad.quad K DD) w ((j + 1 + 1) ^ k + 1)) z := by
              rw [HasseCM.pw_add']; show _ = (Quad.quad K DD).mul ((Quad.quad K DD).mul _ ((Quad.quad K DD).mul (Quad.quad K DD).one w)) z; lib_ring
          _ = z := by rw [hq, pw_qe1 _ w _ q hwl]; lib_ring
      rw [ez, ew]; lib_ring
  exact congrArg Prod.fst hfin

end Cheb

#print axioms Cheb.frob_iter
#print axioms Cheb.pw_qe1
#print axioms Cheb.root_fix

lang/LucK5b.lean

lean_all/lang/LucK5b.lean

/-
  lang/LucK5b — T43, cycle 522: Euler's criterion in ℕ form and the conjugation x^r = x̄ in 𝔽_r[i] for r ≡ 3 (mod 4).

    euler_nat    ℓ = 2m′ + 3 prime, ℓ ∤ r ⇒ r^(m′+1) = qℓ + 1 or r^(m′+1) + 1 = qℓ
    c_mul_e, c_pw_e, c_nsm_e    the embedding a ↦ (a, 0) into Cyclo4.cyc4
    conj_r       r = 4e + 3 prime: (a, b)^r = (a, −b) in 𝔽_r[i]
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.LucK5a
import lang.Cyclo4
import lang.Euler
import lang.Cyclo2l
import lang.ChebCong
import lang.ChebNat

namespace Cheb

open FTAChain ZMod

theorem euler_nat (m' r : Nat) (hpr : ∀ d, Dv d (2 * m' + 1 + 1 + 1) → d = 1 ∨ d = 2 * m' + 1 + 1 + 1)
    (hr : Dv (2 * m' + 1 + 1 + 1) r → False) :
    (∃ q, r ^ (m' + 1) = q * (2 * m' + 1 + 1 + 1) + 1) ∨ (∃ q, r ^ (m' + 1) + 1 = q * (2 * m' + 1 + 1 + 1)) := by
  have hx : emb (ring (2 * m' + 1 + 1)) r ≠ (ring (2 * m' + 1 + 1)).zero := fun e => hr (dv_of_emb _ _ e)
  obtain ⟨q, ρ, hq, hρ⟩ := Order.divmod (2 * m' + 1 + 1) (r ^ (m' + 1))
  have he : emb (ring (2 * m' + 1 + 1)) (r ^ (m' + 1)) = mk (2 * m' + 1 + 1) ρ := by
    rw [hq, emb_add, emb_mul, emb_self, ← emb_mk]
    show (ring (2 * m' + 1 + 1)).add ((ring (2 * m' + 1 + 1)).mul _ (ring (2 * m' + 1 + 1)).zero) _ = _
    lib_ring
  rw [Cyclo2l.emb_pow] at he
  have hval : (mk (2 * m' + 1 + 1) ρ).val = ρ := md_small _ ρ hρ
  rcases Euler.euler_pm m' hpr _ hx with h | h
  · rw [h] at he
    have h1 : ρ = 1 := by
      rw [← hval, ← he]; exact md_small _ 1 (Nat.succ_le_succ (Nat.le_add_left 1 _))
    exact Or.inl ⟨q, by rw [hq, h1]⟩
  · rw [h] at he
    have h1 : ρ = 2 * m' + 1 + 1 := by
      rw [← hval, ← he]
      show md (2 * m' + 1 + 1) ((2 * m' + 1 + 1) * md (2 * m' + 1 + 1) 1) = _
      rw [md_small _ 1 (Nat.succ_le_succ (Nat.le_add_left 1 _)), Nat.mul_one]
      exact md_small _ _ (Nat.lt_succ_self _)
    exact Or.inr ⟨q + 1, by rw [hq, h1]; lib_nat⟩

variable {R : Type} (K : DetChain.CRing R)

theorem c_mul_e (a b : R) : (Cyclo4.cyc4 K).mul (a, K.zero) (b, K.zero) = (K.mul a b, K.zero) := by
  apply Prod.ext
  · show K.add (K.mul a b) (K.neg (K.mul K.zero K.zero)) = K.mul a b; lib_ring
  · show K.add (K.mul a K.zero) (K.mul K.zero b) = K.zero; lib_ring

theorem c_pw_e (a : R) : ∀ n, Frobenius.pw (Cyclo4.cyc4 K) (a, K.zero) n = (Frobenius.pw K a n, K.zero)
  | 0 => rfl
  | n + 1 => by
    show (Cyclo4.cyc4 K).mul (Frobenius.pw (Cyclo4.cyc4 K) (a, K.zero) n) (a, K.zero) = _
    rw [c_pw_e a n, c_mul_e]; rfl

theorem c_nsm_e (a : R) : ∀ n, Frobenius.nsm (Cyclo4.cyc4 K) n (a, K.zero) = (Frobenius.nsm K n a, K.zero)
  | 0 => rfl
  | n + 1 => by
    show (Cyclo4.cyc4 K).add (Frobenius.nsm (Cyclo4.cyc4 K) n (a, K.zero)) (a, K.zero) = _
    rw [c_nsm_e a n]
    exact Prod.ext rfl (show K.add K.zero K.zero = K.zero by lib_ring)

variable (e : Nat)

local notation "𝔽" => ring (2 * (2 * e) + 1 + 1)
local notation "𝔾" => Cyclo4.cyc4 (ring (2 * (2 * e) + 1 + 1))

theorem conj_r (hpr : ∀ d, Dv d (2 * (2 * e) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * e) + 1 + 1 + 1)
    (a b : Zp (2 * (2 * e) + 1 + 1)) :
    Frobenius.pw 𝔾 (a, b) (2 * (2 * e) + 1 + 1 + 1) = (a, (𝔽).neg b) := by
  have hp : Frobenius.nsm 𝔾 (2 * (2 * e) + 1 + 1 + 1) (𝔾).one = (𝔾).zero := by
    show Frobenius.nsm _ _ ((𝔽).one, (𝔽).zero) = _
    rw [c_nsm_e, char_p]; rfl
  have hab : ((a, b) : Zp (2 * (2 * e) + 1 + 1) × Zp (2 * (2 * e) + 1 + 1)) =
      (𝔾).add (a, (𝔽).zero) ((𝔾).mul ((𝔽).zero, (𝔽).one) (b, (𝔽).zero)) := by
    apply Prod.ext
    · show a = (𝔽).add a ((𝔽).add ((𝔽).mul (𝔽).zero b) ((𝔽).neg ((𝔽).mul (𝔽).one (𝔽).zero))); lib_ring
    · show b = (𝔽).add (𝔽).zero ((𝔽).add ((𝔽).mul (𝔽).zero (𝔽).zero) ((𝔽).mul (𝔽).one b)); lib_ring
  have hii : (𝔾).mul ((𝔽).zero, (𝔽).one) ((𝔽).zero, (𝔽).one) = (𝔾).neg (𝔾).one := by
    apply Prod.ext
    · show (𝔽).add ((𝔽).mul (𝔽).zero (𝔽).zero) ((𝔽).neg ((𝔽).mul (𝔽).one (𝔽).one)) = (𝔽).neg (𝔽).one; lib_ring
    · show (𝔽).add ((𝔽).mul (𝔽).zero (𝔽).one) ((𝔽).mul (𝔽).one (𝔽).zero) = (𝔽).neg (𝔽).zero; lib_ring
  have hir : Frobenius.pw 𝔾 ((𝔽).zero, (𝔽).one) (2 * (2 * e) + 1 + 1 + 1) = (𝔾).neg ((𝔽).zero, (𝔽).one) := by
    have ex : 2 * (2 * e) + 1 + 1 + 1 = 2 * (2 * e + 1) + 1 := by lib_nat
    rw [ex]
    show (𝔾).mul (Frobenius.pw 𝔾 _ (2 * (2 * e + 1))) _ = _
    rw [← TwoPower.pwpow]
    have h2 : Frobenius.pw 𝔾 ((𝔽).zero, (𝔽).one) 2 = (𝔾).neg (𝔾).one := by
      rw [← hii]; show (𝔾).mul ((𝔾).mul (𝔾).one _) _ = _; lib_ring
    rw [h2, pw_m1 𝔾 e]; lib_ring
  rw [hab, Frobenius.frob_add _ (2 * (2 * e) + 1) hpr hp, Frobenius.pw_mul, c_pw_e, c_pw_e, ZMod.fermat _ hpr, ZMod.fermat _ hpr, hir]
  apply Prod.ext
  · show (𝔽).add a ((𝔽).add ((𝔽).mul ((𝔽).neg (𝔽).zero) b) ((𝔽).neg ((𝔽).mul ((𝔽).neg (𝔽).one) (𝔽).zero))) = a; lib_ring
  · show (𝔽).add (𝔽).zero ((𝔽).add ((𝔽).mul ((𝔽).neg (𝔽).zero) (𝔽).zero) ((𝔽).mul ((𝔽).neg (𝔽).one) b)) = (𝔽).neg b; lib_ring

end Cheb

#print axioms Cheb.euler_nat
#print axioms Cheb.c_mul_e
#print axioms Cheb.c_pw_e
#print axioms Cheb.c_nsm_e
#print axioms Cheb.conj_r

lang/LucKey.lean

lean_all/lang/LucKey.lean

/-
  lang/LucKey — T43, cycle 494: the Lucas key — r | U_q(s) forces q | (r ∓ 1)/2.

  In a commutative ring with zw = 1, z + w = s and z − w invertible:
    luc_geo     z^k·U_k(s) = z·geo(z², k)            (from (z − w)U_k = z^k − w^k and (z² − 1)geo(z², k) = z^(2k) − 1)
    luc_key     U_q(s) = 0, q prime, (z²)^N = 1  ⟹  q | N  or  q·1 = 0    (ChebKey.key at u = z²)
  With LucRoot (z^(r−1) = 1 or z^(r+1) = 1, N = (r ∓ 1)/2) this is Lucas's law of apparition for primes.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.LucU
import lang.ChebKey
import lang.OrdQ

namespace Cheb

open FTAChain

variable {R : Type} (K : DetChain.CRing R)

theorem luc_geo (t : Nat) (z w : R) (hzw : K.mul z w = K.one)
    (hs : K.add z w = K.add (emb K t) (K.add K.one K.one)) (e : R) (he : K.mul (K.add z (K.neg w)) e = K.one) (k : Nat) :
    K.mul (Frobenius.pw K z k) (emb K (lu t k).1) = K.mul z (geo K (K.mul z z) k) := by
  have h1 := (lu_emb K t z w hzw hs k).1
  have h2 := geo_mul K (K.mul z z) k
  have h3 : Frobenius.pw K (K.mul z z) k = K.mul (Frobenius.pw K z k) (Frobenius.pw K z k) := Frobenius.pw_mul K z z k
  have h4 : K.mul (Frobenius.pw K z k) (Frobenius.pw K w k) = K.one := by
    rw [← Frobenius.pw_mul K z w k, hzw]; exact OrdQ.pw_one1 K k
  calc K.mul (Frobenius.pw K z k) (emb K (lu t k).1)
      = K.mul (K.mul (K.add z (K.neg w)) e) (K.mul (Frobenius.pw K z k) (emb K (lu t k).1)) := by rw [he]; lib_ring
    _ = K.mul e (K.mul (Frobenius.pw K z k) (K.mul (K.add z (K.neg w)) (emb K (lu t k).1))) := by lib_ring
    _ = K.mul e (K.mul (Frobenius.pw K z k) (K.add (Frobenius.pw K z k) (K.neg (Frobenius.pw K w k)))) := by rw [h1]
    _ = K.mul e (K.add (K.mul (Frobenius.pw K z k) (Frobenius.pw K z k))
          (K.neg (K.mul (Frobenius.pw K z k) (Frobenius.pw K w k)))) := by lib_ring
    _ = K.mul e (K.add (Frobenius.pw K (K.mul z z) k) (K.neg K.one)) := by rw [h4, h3]
    _ = K.mul e (K.mul (K.add (K.mul z z) (K.neg K.one)) (geo K (K.mul z z) k)) := by rw [h2]
    _ = K.mul e (K.mul (K.add (K.mul z z) (K.neg (K.mul z w))) (geo K (K.mul z z) k)) := by rw [hzw]
    _ = K.mul (K.mul (K.add z (K.neg w)) e) (K.mul z (geo K (K.mul z z) k)) := by lib_ring
    _ = K.mul z (geo K (K.mul z z) k) := by rw [he]; lib_ring

theorem luc_key (t : Nat) (z w : R) (hzw : K.mul z w = K.one)
    (hs : K.add z w = K.add (emb K t) (K.add K.one K.one)) (e : R) (he : K.mul (K.add z (K.neg w)) e = K.one)
    (j : Nat) (hl : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1) (hU : emb K (lu t (j + 1 + 1)).1 = K.zero)
    (N : Nat) (hN : Frobenius.pw K (K.mul z z) N = K.one) :
    Dv (j + 1 + 1) N ∨ geo K K.one (j + 1 + 1) = K.zero := by
  have hg := luc_geo K t z w hzw hs e he (j + 1 + 1)
  rw [hU] at hg
  have hg0 : geo K (K.mul z z) (j + 1 + 1) = K.zero := by
    calc geo K (K.mul z z) (j + 1 + 1) = K.mul (K.mul z w) (geo K (K.mul z z) (j + 1 + 1)) := by rw [hzw]; lib_ring
      _ = K.mul w (K.mul z (geo K (K.mul z z) (j + 1 + 1))) := by lib_ring
      _ = K.mul w (K.mul (Frobenius.pw K z (j + 1 + 1)) K.zero) := by rw [hg]
      _ = K.zero := by lib_ring
  exact key K j hl _ hg0 N hN

end Cheb

#print axioms Cheb.luc_geo
#print axioms Cheb.luc_key

lang/LucLTE.lean

lean_all/lang/LucLTE.lean

/-
  lang/LucLTE — T43, cycle 498: r | U_e(s) and r ∤ m ⇒ r ∤ U_m(V_e(s))   (the Lucas analogue of ValGeo.gN_rpart's core).

    vu_rel      V_k + s·U_k = 2·U_{k+1}            (ℕ, two-step induction)
    lu_twou     in a commutative ring with u² = 1 and t′ + 2 = 2u:  U_k(t′ + 2)·u = k·u^k
    luc_ndv     r ≥ 2 (any modulus), r | U_e(s), V_e(s) = t′ + 2, r ∤ m ⇒ r ∤ U_m(t′ + 2)
  Modulo r: U_e ≡ 0 gives V_e ≡ 2U_{e+1} and U_{e+1}² ≡ 1 (Cassini), so V_e ≡ 2u with u² = 1 and U_m(V_e) ≡ m·u^(m−1).
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.LucRank
import lang.ChebInf
import lang.ChebCong
import lang.OrdQ

namespace Cheb

open FTAChain ZMod

theorem vu_rel2 (t : Nat) : ∀ k, (cd t k).1 + (t + 2) * (lu t k).1 = 2 * (lu t (k + 1)).1 ∧
    (cd t (k + 1)).1 + (t + 2) * (lu t (k + 1)).1 = 2 * (lu t (k + 1 + 1)).1
  | 0 => by
    refine ⟨?_, ?_⟩
    · show 2 + (t + 2) * 0 = 2 * (0 + 1); lib_nat
    · show (2 + t) + (t + 2) * (0 + 1) = 2 * ((0 + 1) + (t * (0 + 1) + 1)); lib_nat
  | k + 1 => by
    obtain ⟨h0, h1⟩ := vu_rel2 t k
    refine ⟨h1, ?_⟩
    have R1 := rec2 t k
    have R2 := lu_rec t k
    have R3 := lu_rec t (k + 1)
    rw [show k + 1 + 2 = k + 1 + 1 + 1 by lib_nat] at R3
    rw [show k + 2 = k + 1 + 1 by lib_nat] at R2
    generalize (cd t (k + 1 + 1)).1 = c2 at R1 ⊢
    generalize (cd t (k + 1)).1 = c1 at R1 h1
    generalize (cd t k).1 = c0 at R1 h0
    generalize (lu t (k + 1 + 1 + 1)).1 = u3 at R3 ⊢
    generalize (lu t (k + 1 + 1)).1 = u2 at R2 R3 h1 ⊢
    generalize (lu t (k + 1)).1 = u1 at R2 R3 h0 h1
    generalize (lu t k).1 = u0 at R2 h0
    apply add_rc (c0 + (t + 2) * u0)
    calc c2 + (t + 2) * u2 + (c0 + (t + 2) * u0) = (c2 + c0) + (t + 2) * (u2 + u0) := by lib_nat
      _ = (t + 2) * c1 + (t + 2) * ((t + 2) * u1) := by rw [R1, R2]
      _ = (t + 2) * (c1 + (t + 2) * u1) := by lib_nat
      _ = (t + 2) * (2 * u2) := by rw [h1]
      _ = 2 * ((t + 2) * u2) := by lib_nat
      _ = 2 * (u3 + u1) := by rw [R3]
      _ = 2 * u3 + 2 * u1 := by lib_nat
      _ = 2 * u3 + (c0 + (t + 2) * u0) := by rw [h0]

theorem vu_rel (t k : Nat) : (cd t k).1 + (t + 2) * (lu t k).1 = 2 * (lu t (k + 1)).1 := (vu_rel2 t k).1

variable {R : Type} (K : DetChain.CRing R)

theorem lu_twou (t' : Nat) (u : R) (hu : K.mul u u = K.one) (ht : K.add (emb K t') (K.add K.one K.one) = K.add u u) : ∀ k,
    K.mul (emb K (lu t' k).1) u = K.mul (emb K k) (Frobenius.pw K u k) ∧
    K.mul (emb K (lu t' k).2) u = K.add (K.mul (emb K (k + 1)) (Frobenius.pw K u (k + 1))) (K.neg (K.mul (emb K k) (Frobenius.pw K u k)))
  | 0 => by
    refine ⟨?_, ?_⟩
    · show K.mul K.zero u = K.mul K.zero K.one; lib_ring
    · show K.mul (K.add K.zero K.one) u = K.add (K.mul (K.add K.zero K.one) (K.mul K.one u)) (K.neg (K.mul K.zero K.one)); lib_ring
  | k + 1 => by
    obtain ⟨h0, h1⟩ := lu_twou t' u hu ht k
    have hA : K.mul (emb K (lu t' (k + 1)).1) u = K.mul (emb K (k + 1)) (Frobenius.pw K u (k + 1)) := by
      show K.mul (emb K ((lu t' k).1 + (lu t' k).2)) u = _
      rw [emb_add]
      calc _ = K.add (K.mul (emb K (lu t' k).1) u) (K.mul (emb K (lu t' k).2) u) := by lib_ring
        _ = _ := by rw [h0, h1]; lib_ring
    refine ⟨hA, ?_⟩
    have het : emb K t' = K.add (K.add u u) (K.neg (K.add K.one K.one)) := by
      calc emb K t' = K.add (K.add (emb K t') (K.add K.one K.one)) (K.neg (K.add K.one K.one)) := by lib_ring
        _ = _ := by rw [ht]
    show K.mul (emb K (t' * (lu t' (k + 1)).1 + (lu t' k).2)) u = _
    rw [emb_add, emb_mul, het]
    calc _ = K.add (K.mul (K.add (K.add u u) (K.neg (K.add K.one K.one))) (K.mul (emb K (lu t' (k + 1)).1) u))
          (K.mul (emb K (lu t' k).2) u) := by lib_ring
      _ = _ := by
        rw [hA, h1]
        show _ = K.add (K.mul (emb K (k + 1 + 1)) (K.mul (K.mul (Frobenius.pw K u k) u) u))
          (K.neg (K.mul (emb K (k + 1)) (K.mul (Frobenius.pw K u k) u)))
        show K.add (K.mul _ (K.mul (emb K (k + 1)) (K.mul (Frobenius.pw K u k) u)))
          (K.add (K.mul (emb K (k + 1)) (K.mul (Frobenius.pw K u k) u)) (K.neg (K.mul (emb K k) (Frobenius.pw K u k)))) = _
        have e1 : emb K (k + 1) = K.add (emb K k) K.one := rfl
        have e2 : emb K (k + 1 + 1) = K.add (K.add (emb K k) K.one) K.one := rfl
        rw [e2, e1]
        generalize Frobenius.pw K u k = P
        generalize emb K k = E
        calc _ = K.add (K.add (K.mul (K.add (K.add E K.one) K.one) (K.mul (K.mul P u) u))
                (K.neg (K.mul (K.add E K.one) (K.mul P u))))
              (K.mul (K.mul E P) (K.add (K.mul u u) (K.neg K.one))) := by lib_ring
          _ = _ := by rw [hu]; lib_ring

theorem luc_ndv (t t' l e m : Nat)
    (hU : Dv (l + 1 + 1) (lu t e).1) (hv : (cd t e).1 = t' + 2) (hm : Dv (l + 1 + 1) m → False) :
    Dv (l + 1 + 1) (lu t' m).1 → False := by
  intro h
  have K0 := emb_dv (l + 1) _ hU
  have hc := congrArg (emb (ring (l + 1))) (cassini t e)
  rw [emb_add, emb_mul, emb_mul, K0] at hc
  have hu : (ring (l + 1)).mul (emb (ring (l + 1)) (lu t (e + 1)).1) (emb (ring (l + 1)) (lu t (e + 1)).1) = (ring (l + 1)).one := by
    rw [hc]
    show (ring (l + 1)).add ((ring (l + 1)).mul (ring (l + 1)).zero (emb (ring (l + 1)) (lu t (e + 2)).1)) ((ring (l + 1)).add (ring (l + 1)).zero (ring (l + 1)).one) = _
    lib_ring
  have hvr := congrArg (emb (ring (l + 1))) (vu_rel t e)
  rw [emb_add, emb_mul, emb_mul, K0, hv, emb_add] at hvr
  have ht : (ring (l + 1)).add (emb (ring (l + 1)) t') ((ring (l + 1)).add (ring (l + 1)).one (ring (l + 1)).one) =
      (ring (l + 1)).add (emb (ring (l + 1)) (lu t (e + 1)).1) (emb (ring (l + 1)) (lu t (e + 1)).1) := by
    have e2 : emb (ring (l + 1)) 2 = (ring (l + 1)).add (ring (l + 1)).one (ring (l + 1)).one := by
      show (ring (l + 1)).add ((ring (l + 1)).add (ring (l + 1)).zero (ring (l + 1)).one) (ring (l + 1)).one = _; lib_ring
    rw [e2] at hvr
    calc _ = (ring (l + 1)).add ((ring (l + 1)).add (emb (ring (l + 1)) t') ((ring (l + 1)).add (ring (l + 1)).one (ring (l + 1)).one))
          ((ring (l + 1)).mul (emb (ring (l + 1)) (t + 2)) (ring (l + 1)).zero) := by lib_ring
      _ = _ := by rw [hvr]; lib_ring
  generalize emb (ring (l + 1)) (lu t (e + 1)).1 = u at hu ht
  have hk := (lu_twou (ring (l + 1)) t' u hu ht m).1
  rw [emb_dv (l + 1) _ h] at hk
  have hpu : (ring (l + 1)).mul (Frobenius.pw (ring (l + 1)) u m) (Frobenius.pw (ring (l + 1)) u m) = (ring (l + 1)).one := by
    rw [← Frobenius.pw_mul, hu]; exact OrdQ.pw_one1 _ m
  apply hm
  apply dv_of_emb (l + 1) m
  calc emb (ring (l + 1)) m = (ring (l + 1)).mul ((ring (l + 1)).mul (emb (ring (l + 1)) m) (Frobenius.pw (ring (l + 1)) u m))
        (Frobenius.pw (ring (l + 1)) u m) := by
        rw [show (ring (l + 1)).mul ((ring (l + 1)).mul (emb (ring (l + 1)) m) (Frobenius.pw (ring (l + 1)) u m))
          (Frobenius.pw (ring (l + 1)) u m) = (ring (l + 1)).mul (emb (ring (l + 1)) m)
            ((ring (l + 1)).mul (Frobenius.pw (ring (l + 1)) u m) (Frobenius.pw (ring (l + 1)) u m)) by lib_ring, hpu]; lib_ring
    _ = (ring (l + 1)).mul ((ring (l + 1)).mul (ring (l + 1)).zero u) (Frobenius.pw (ring (l + 1)) u m) := by rw [hk]
    _ = (ring (l + 1)).zero := by lib_ring

end Cheb

#print axioms Cheb.vu_rel2
#print axioms Cheb.vu_rel
#print axioms Cheb.lu_twou
#print axioms Cheb.luc_ndv

lang/LucMix.lean

lean_all/lang/LucMix.lean

/-
  lang/LucMix — T43, cycle 516: prime factors r ≡ 1 (mod 4) of F(n) = |ψ_ℓ(n + i)|² are ≡ ±1 (mod ℓ) (K2 complete for r ≡ 1 mod 4).

    k2_full     r = 2m + 3 ≡ 1 (mod 4) prime, r | F(n), ℓ = 2d + 1 prime  ⇒  ℓ | r − 1, ℓ | r + 1, or ℓ·1 = 0 in 𝔽_r
  (LucGaussZ.k2_one gives a root n̄ ± ι of f_d in 𝔽_r; ChebRoot.root.)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.LucGaussZ
import lang.ChebRoot
import lang.Fp2Sqrt

namespace Cheb

open FTAChain ZMod LibRing LibInt

theorem k2_full (m n d N j : Nat) (hpr : ∀ e, Dv e (2 * m + 1 + 1 + 1) → e = 1 ∨ e = 2 * m + 1 + 1 + 1)
    (h4 : ∃ k, m + 1 = 2 * k) (hl : ∀ e, Dv e (j + 1 + 1) → e = 1 ∨ e = j + 1 + 1) (hjd : j + 1 + 1 = 2 * d + 1)
    (hN : (fd (Cyclo4.cyc4 intR) (Int.ofNat n, 1) d).1 * (fd (Cyclo4.cyc4 intR) (Int.ofNat n, 1) d).1 +
      (fd (Cyclo4.cyc4 intR) (Int.ofNat n, 1) d).2 * (fd (Cyclo4.cyc4 intR) (Int.ofNat n, 1) d).2 = Int.ofNat N)
    (hr : Dv (2 * m + 1 + 1 + 1) N) :
    Dv (j + 1 + 1) (2 * m + 1 + 1) ∨ Dv (j + 1 + 1) (2 * m + 1 + 1 + 1 + 1) ∨
      geo (ring (2 * m + 1 + 1)) (ring (2 * m + 1 + 1)).one (j + 1 + 1) = (ring (2 * m + 1 + 1)).zero := by
  obtain ⟨ι, _, h⟩ := k2_one m n d N hpr h4 hN hr
  rcases h with h | h
  · exact root m hpr j d hl hjd (Fp2Sqrt.two_ne m) _ h
  · exact root m hpr j d hl hjd (Fp2Sqrt.two_ne m) _ h

end Cheb

#print axioms Cheb.k2_full

lang/LucMixT.lean

lean_all/lang/LucMixT.lean

/-
  lang/LucMixT — T43, cycle 526: K1, K2, K5′, Q1 at the Gaussian point x = n + t·i (second coordinate t instead of 1).

    gauss_root_t   f_d(n + ti) = A + Bi, A² + B² = 0 (no zero divisors), ι² = −1 ⇒ f_d(n + ιt) = 0 or f_d(n − ιt) = 0
    red_sum_t      r | F = A² + B² (A + Bi = f_d(n + ti) ∈ ℤ[i]) ⇒ Q₁² + Q₂² = 0 in 𝔽_r
    k2_t           r ≡ 1 (mod 4) prime, r | F ⇒ ℓ | r − 1, ℓ | r + 1 or ℓ·1 = 0
    k5_t, no_r3_t  ℓ ≡ 3, r ≡ 3 (mod 4), r ≠ ℓ, r ∤ t ⇒ r ∤ F
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.LucQ1
import lang.LucMix

namespace Cheb

open FTAChain ZMod LibRing LibInt

section G
variable {R : Type} (K : DetChain.CRing R)

theorem gauss_root_t (hnz : ∀ a b : R, K.mul a b = K.zero → a = K.zero ∨ b = K.zero)
    (ι n t : R) (hι : K.mul ι ι = K.neg K.one) (d : Nat)
    (h0 : K.add (K.mul (fd (Cyclo4.cyc4 K) (n, t) d).1 (fd (Cyclo4.cyc4 K) (n, t) d).1)
      (K.mul (fd (Cyclo4.cyc4 K) (n, t) d).2 (fd (Cyclo4.cyc4 K) (n, t) d).2) = K.zero) :
    fd K (K.add n (K.mul ι t)) d = K.zero ∨ fd K (K.add n (K.mul (K.neg ι) t)) d = K.zero := by
  have hι' : K.mul (K.neg ι) (K.neg ι) = K.neg K.one := by rw [← hι]; lib_ring
  have hadd : ∀ (j : R) (x y : R × R), gmap K j ((Cyclo4.cyc4 K).add x y) = K.add (gmap K j x) (gmap K j y) := fun j x y => by
    show K.add (K.add x.1 y.1) (K.mul j (K.add x.2 y.2)) = K.add (K.add x.1 (K.mul j x.2)) (K.add y.1 (K.mul j y.2)); lib_ring
  have hneg : ∀ (j : R) (x : R × R), gmap K j ((Cyclo4.cyc4 K).neg x) = K.neg (gmap K j x) := fun j x => by
    show K.add (K.neg x.1) (K.mul j (K.neg x.2)) = K.neg (K.add x.1 (K.mul j x.2)); lib_ring
  have hone : ∀ j : R, gmap K j (Cyclo4.cyc4 K).one = K.one := fun j => by
    show K.add K.one (K.mul j K.zero) = K.one; lib_ring
  have e1 := fd_map (Cyclo4.cyc4 K) K (gmap K ι) (hadd ι) (gmap_mul K ι hι) (hneg ι) (hone ι) (n, t) d
  have e2 := fd_map (Cyclo4.cyc4 K) K (gmap K (K.neg ι)) (hadd _) (gmap_mul K _ hι') (hneg _) (hone _) (n, t) d
  have g1 : gmap K ι (n, t) = K.add n (K.mul ι t) := rfl
  have g2 : gmap K (K.neg ι) (n, t) = K.add n (K.mul (K.neg ι) t) := rfl
  rw [g1] at e1; rw [g2] at e2
  generalize fd (Cyclo4.cyc4 K) (n, t) d = P at h0 e1 e2
  have hprod : K.mul (gmap K ι P) (gmap K (K.neg ι) P) = K.zero := by
    show K.mul (K.add P.1 (K.mul ι P.2)) (K.add P.1 (K.mul (K.neg ι) P.2)) = K.zero
    rw [← h0]
    calc _ = K.add (K.add (K.mul P.1 P.1) (K.mul P.2 P.2)) (K.mul (K.mul P.2 P.2) (K.add (K.neg (K.mul ι ι)) (K.neg K.one))) := by lib_ring
      _ = _ := by rw [hι]; lib_ring
  rcases hnz _ _ hprod with h | h
  · exact Or.inl (e1.symm.trans h)
  · exact Or.inr (e2.symm.trans h)

end G

theorem red_sum_t (m n t d N : Nat)
    (hN : (fd (Cyclo4.cyc4 intR) (Int.ofNat n, Int.ofNat t) d).1 * (fd (Cyclo4.cyc4 intR) (Int.ofNat n, Int.ofNat t) d).1 +
      (fd (Cyclo4.cyc4 intR) (Int.ofNat n, Int.ofNat t) d).2 * (fd (Cyclo4.cyc4 intR) (Int.ofNat n, Int.ofNat t) d).2 = Int.ofNat N)
    (hr : Dv (m + 1) N) :
    (ring m).add ((ring m).mul (fd (Cyclo4.cyc4 (ring m)) (castN (ring m) n, castN (ring m) t) d).1 (fd (Cyclo4.cyc4 (ring m)) (castN (ring m) n, castN (ring m) t) d).1)
      ((ring m).mul (fd (Cyclo4.cyc4 (ring m)) (castN (ring m) n, castN (ring m) t) d).2 (fd (Cyclo4.cyc4 (ring m)) (castN (ring m) n, castN (ring m) t) d).2) = (ring m).zero := by
  have hmap := fd_map (Cyclo4.cyc4 intR) (Cyclo4.cyc4 (ring m)) (cpair (ring m)) (cpair_add _) (cpair_mul _) (cpair_neg _) (cpair_one _) (Int.ofNat n, Int.ofNat t) d
  have hc : cpair (ring m) ((Int.ofNat n, Int.ofNat t) : Int × Int) = (castN (ring m) n, castN (ring m) t) := rfl
  rw [hc] at hmap
  rw [← hmap]
  generalize fd (Cyclo4.cyc4 intR) (Int.ofNat n, Int.ofNat t) d = P at hN
  show (ring m).add ((ring m).mul (castZ (ring m) P.1) (castZ (ring m) P.1)) ((ring m).mul (castZ (ring m) P.2) (castZ (ring m) P.2)) = _
  rw [← HasseDavenport.castZ_mul, ← HasseDavenport.castZ_mul, ← IntCast.castZ_add, hN]
  show castN (ring m) N = _
  rw [IntCast.castN_mk]
  exact Subtype.ext (dv_md_zero m N hr)

theorem k2_t (m n t d N j : Nat) (hpr : ∀ e, Dv e (2 * m + 1 + 1 + 1) → e = 1 ∨ e = 2 * m + 1 + 1 + 1)
    (h4 : ∃ k, m + 1 = 2 * k) (hl : ∀ e, Dv e (j + 1 + 1) → e = 1 ∨ e = j + 1 + 1) (hjd : j + 1 + 1 = 2 * d + 1)
    (hN : (fd (Cyclo4.cyc4 intR) (Int.ofNat n, Int.ofNat t) d).1 * (fd (Cyclo4.cyc4 intR) (Int.ofNat n, Int.ofNat t) d).1 +
      (fd (Cyclo4.cyc4 intR) (Int.ofNat n, Int.ofNat t) d).2 * (fd (Cyclo4.cyc4 intR) (Int.ofNat n, Int.ofNat t) d).2 = Int.ofNat N)
    (hr : Dv (2 * m + 1 + 1 + 1) N) :
    Dv (j + 1 + 1) (2 * m + 1 + 1) ∨ Dv (j + 1 + 1) (2 * m + 1 + 1 + 1 + 1) ∨
      geo (ring (2 * m + 1 + 1)) (ring (2 * m + 1 + 1)).one (j + 1 + 1) = (ring (2 * m + 1 + 1)).zero := by
  obtain ⟨k, hk⟩ := h4
  have hm1 : Frobenius.pw (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) (m + 1) = (ring (2 * m + 1 + 1)).one := by
    rw [hk, ← TwoPower.pwpow]
    have e : Frobenius.pw (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) 2 = (ring (2 * m + 1 + 1)).one := by
      show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one _) _ = _; lib_ring
    rw [e]; exact TwoPower.pwone _ k
  obtain ⟨ι, hι⟩ := EulerHard.euler_hard m hpr _ hm1
  rcases gauss_root_t (ring (2 * m + 1 + 1)) (ZMod.nozd (2 * m + 1) hpr) ι _ _ hι d (red_sum_t (2 * m + 1 + 1) n t d N hN hr) with h | h
  · exact root m hpr j d hl hjd (Fp2Sqrt.two_ne m) _ h
  · exact root m hpr j d hl hjd (Fp2Sqrt.two_ne m) _ h

variable (e : Nat)

local notation "𝔽" => ring (2 * (2 * e) + 1 + 1)
local notation "𝔾" => Cyclo4.cyc4 (ring (2 * (2 * e) + 1 + 1))

theorem k5_t (c : Nat) (hpr : ∀ d, Dv d (2 * (2 * e) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * e) + 1 + 1 + 1)
    (hl : ∀ d, Dv d (2 * (2 * c) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * c) + 1 + 1 + 1)
    (hlr : Dv (2 * (2 * c) + 1 + 1 + 1) (2 * (2 * e) + 1 + 1 + 1) → False) (n t : Zp (2 * (2 * e) + 1 + 1))
    (ht : t ≠ (𝔽).zero) (hf : fd 𝔾 (n, t) (2 * c + 1) = (𝔾).zero) : False := by
  have hp : Frobenius.nsm 𝔾 (2 * (2 * e) + 1 + 1 + 1) (𝔾).one = (𝔾).zero := by
    show Frobenius.nsm _ _ ((𝔽).one, (𝔽).zero) = _
    rw [c_nsm_e, char_p]; rfl
  have hk := euler_nat (2 * c) (2 * (2 * e) + 1 + 1 + 1) hl hlr
  rw [show 2 * (2 * c) + 1 + 1 + 1 = 2 * (2 * c + 1) + 1 by lib_nat] at hk
  have hfix := root_fix 𝔾 (2 * (2 * e) + 1) (2 * c + 1) (2 * e + 1) hpr (by lib_nat) hp (n, t) hf (2 * c + 1) hk
  rw [(conj_iter e hpr n t c).2] at hfix
  have h1 : (𝔽).neg t = t := congrArg Prod.snd hfix
  have h2 : (𝔽).mul ((𝔽).add (𝔽).one (𝔽).one) t = (𝔽).zero := by
    calc _ = (𝔽).add t t := by lib_ring
      _ = (𝔽).add t ((𝔽).neg t) := by rw [h1]
      _ = _ := by lib_ring
  rcases nozd (2 * (2 * e) + 1) hpr _ _ h2 with h | h
  · exact Fp2Sqrt.two_ne (2 * e) h
  · exact ht h

theorem no_r3_t (c n t N : Nat) (hpr : ∀ d, Dv d (2 * (2 * e) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * e) + 1 + 1 + 1)
    (hl : ∀ d, Dv d (2 * (2 * c) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * c) + 1 + 1 + 1)
    (hlr : Dv (2 * (2 * c) + 1 + 1 + 1) (2 * (2 * e) + 1 + 1 + 1) → False)
    (hrt : Dv (2 * (2 * e) + 1 + 1 + 1) t → False)
    (hN : (fd (Cyclo4.cyc4 intR) (Int.ofNat n, Int.ofNat t) (2 * c + 1)).1 * (fd (Cyclo4.cyc4 intR) (Int.ofNat n, Int.ofNat t) (2 * c + 1)).1 +
      (fd (Cyclo4.cyc4 intR) (Int.ofNat n, Int.ofNat t) (2 * c + 1)).2 * (fd (Cyclo4.cyc4 intR) (Int.ofNat n, Int.ofNat t) (2 * c + 1)).2 = Int.ofNat N)
    (hr : Dv (2 * (2 * e) + 1 + 1 + 1) N) : False := by
  have hs := red_sum_t (2 * (2 * e) + 1 + 1) n t (2 * c + 1) N hN hr
  obtain ⟨h1, h2⟩ := sq_sum_zero e hpr _ _ hs
  have ht : castN (𝔽) t ≠ (𝔽).zero := fun h0 => hrt (dv_of_emb _ t (by rw [emb_mk, ← IntCast.castN_mk]; exact h0))
  exact k5_t e c hpr hl hlr (castN (𝔽) n) (castN (𝔽) t) ht (Prod.ext h1 h2)

end Cheb

#print axioms Cheb.gauss_root_t
#print axioms Cheb.red_sum_t
#print axioms Cheb.k2_t
#print axioms Cheb.k5_t
#print axioms Cheb.no_r3_t

lang/LucModL.lean

lean_all/lang/LucModL.lean

/-
  lang/LucModL — T43, cycle 528: F(n) ≡ −1 (mod ℓ) at n = 2 + mt (ℓ = 4c + 3, (m² + 1)^d = −1, ℓ ∤ t).

    val_sum      castN N = Q₁² + Q₂² for Q = f_d(n̄ + t̄i) ∈ 𝔽_r[i] when F = A² + B² = N
    nrm_mul, nrm_pw   the norm a² + b² on K[i] is multiplicative
    modl         emb N = −1 in 𝔽_ℓ
  In 𝔽_ℓ[i] (characteristic ℓ) f_d(x) = (x − 2)^d (LucChar2.fd_char), N(x − 2) = t̄²(m̄² + 1), and t̄^(ℓ−1) = 1.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.LucMixT
import lang.LucNonRes

namespace Cheb

open FTAChain ZMod LibRing LibInt

theorem val_sum (m n t d N : Nat)
    (hN : (fd (Cyclo4.cyc4 intR) (Int.ofNat n, Int.ofNat t) d).1 * (fd (Cyclo4.cyc4 intR) (Int.ofNat n, Int.ofNat t) d).1 +
      (fd (Cyclo4.cyc4 intR) (Int.ofNat n, Int.ofNat t) d).2 * (fd (Cyclo4.cyc4 intR) (Int.ofNat n, Int.ofNat t) d).2 = Int.ofNat N) :
    emb (ring m) N = (ring m).add ((ring m).mul (fd (Cyclo4.cyc4 (ring m)) (emb (ring m) n, emb (ring m) t) d).1 (fd (Cyclo4.cyc4 (ring m)) (emb (ring m) n, emb (ring m) t) d).1)
      ((ring m).mul (fd (Cyclo4.cyc4 (ring m)) (emb (ring m) n, emb (ring m) t) d).2 (fd (Cyclo4.cyc4 (ring m)) (emb (ring m) n, emb (ring m) t) d).2) := by
  have hmap := fd_map (Cyclo4.cyc4 intR) (Cyclo4.cyc4 (ring m)) (cpair (ring m)) (cpair_add _) (cpair_mul _) (cpair_neg _) (cpair_one _) (Int.ofNat n, Int.ofNat t) d
  have hc : cpair (ring m) ((Int.ofNat n, Int.ofNat t) : Int × Int) = (emb (ring m) n, emb (ring m) t) := by
    show (castN (ring m) n, castN (ring m) t) = _
    rw [IntCast.castN_mk, IntCast.castN_mk, emb_mk, emb_mk]
  rw [hc] at hmap
  rw [← hmap]
  generalize fd (Cyclo4.cyc4 intR) (Int.ofNat n, Int.ofNat t) d = P at hN
  show _ = (ring m).add ((ring m).mul (castZ (ring m) P.1) (castZ (ring m) P.1)) ((ring m).mul (castZ (ring m) P.2) (castZ (ring m) P.2))
  rw [← HasseDavenport.castZ_mul, ← HasseDavenport.castZ_mul, ← IntCast.castZ_add, hN]
  show _ = castN (ring m) N
  rw [IntCast.castN_mk, emb_mk]

section N
variable {R : Type} (K : DetChain.CRing R)

def nrm (x : R × R) : R := K.add (K.mul x.1 x.1) (K.mul x.2 x.2)

theorem nrm_mul (x y : R × R) : nrm K ((Cyclo4.cyc4 K).mul x y) = K.mul (nrm K x) (nrm K y) := by
  show K.add (K.mul (K.add (K.mul x.1 y.1) (K.neg (K.mul x.2 y.2))) (K.add (K.mul x.1 y.1) (K.neg (K.mul x.2 y.2))))
    (K.mul (K.add (K.mul x.1 y.2) (K.mul x.2 y.1)) (K.add (K.mul x.1 y.2) (K.mul x.2 y.1))) =
    K.mul (K.add (K.mul x.1 x.1) (K.mul x.2 x.2)) (K.add (K.mul y.1 y.1) (K.mul y.2 y.2))
  lib_ring

theorem nrm_pw (x : R × R) : ∀ n, nrm K (Frobenius.pw (Cyclo4.cyc4 K) x n) = Frobenius.pw K (nrm K x) n
  | 0 => by show K.add (K.mul K.one K.one) (K.mul K.zero K.zero) = K.one; lib_ring
  | n + 1 => by
    show nrm K ((Cyclo4.cyc4 K).mul (Frobenius.pw (Cyclo4.cyc4 K) x n) x) = K.mul (Frobenius.pw K (nrm K x) n) (nrm K x)
    rw [nrm_mul, nrm_pw x n]
end N

variable (c : Nat)

local notation "𝔽" => ring (2 * (2 * c) + 1 + 1)
local notation "𝔾" => Cyclo4.cyc4 (ring (2 * (2 * c) + 1 + 1))

theorem modl (m t N : Nat) (hl : ∀ d, Dv d (2 * (2 * c) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * c) + 1 + 1 + 1)
    (hm : Frobenius.pw (𝔽) (emb (𝔽) (m * m + 1)) (2 * c + 1) = (𝔽).neg (𝔽).one)
    (hlt : Dv (2 * (2 * c) + 1 + 1 + 1) t → False)
    (hN : (fd (Cyclo4.cyc4 intR) (Int.ofNat (2 + m * t), Int.ofNat t) (2 * c + 1)).1 * (fd (Cyclo4.cyc4 intR) (Int.ofNat (2 + m * t), Int.ofNat t) (2 * c + 1)).1 +
      (fd (Cyclo4.cyc4 intR) (Int.ofNat (2 + m * t), Int.ofNat t) (2 * c + 1)).2 * (fd (Cyclo4.cyc4 intR) (Int.ofNat (2 + m * t), Int.ofNat t) (2 * c + 1)).2 = Int.ofNat N) :
    emb (𝔽) N = (𝔽).neg (𝔽).one := by
  have hv := val_sum (2 * (2 * c) + 1 + 1) (2 + m * t) t (2 * c + 1) N hN
  have hp : Frobenius.nsm 𝔾 (2 * (2 * c) + 1 + 1 + 1) (𝔾).one = (𝔾).zero := by
    show Frobenius.nsm _ _ ((𝔽).one, (𝔽).zero) = _
    rw [c_nsm_e, char_p]; rfl
  have hfc := fd_char 𝔾 (2 * (2 * c) + 1) (2 * c + 1) hl (by lib_nat) hp (emb (𝔽) (2 + m * t), emb (𝔽) t)
  rw [hfc] at hv
  have hx2 : (𝔾).add (emb (𝔽) (2 + m * t), emb (𝔽) t) ((𝔾).neg ((𝔾).add (𝔾).one (𝔾).one)) =
      ((𝔽).mul (emb (𝔽) m) (emb (𝔽) t), emb (𝔽) t) := by
    apply Prod.ext
    · show (𝔽).add (emb (𝔽) (2 + m * t)) ((𝔽).neg ((𝔽).add (𝔽).one (𝔽).one)) = (𝔽).mul (emb (𝔽) m) (emb (𝔽) t)
      rw [emb_add, emb_mul]
      show (𝔽).add ((𝔽).add ((𝔽).add ((𝔽).add (𝔽).zero (𝔽).one) (𝔽).one) ((𝔽).mul (emb (𝔽) m) (emb (𝔽) t)))
        ((𝔽).neg ((𝔽).add (𝔽).one (𝔽).one)) = (𝔽).mul (emb (𝔽) m) (emb (𝔽) t); lib_ring
    · show (𝔽).add (emb (𝔽) t) ((𝔽).neg ((𝔽).add (𝔽).zero (𝔽).zero)) = emb (𝔽) t; lib_ring
  rw [hx2] at hv
  have hnr := nrm_pw (𝔽) ((𝔽).mul (emb (𝔽) m) (emb (𝔽) t), emb (𝔽) t) (2 * c + 1)
  have hv' : emb (𝔽) N = nrm (𝔽) (Frobenius.pw 𝔾 ((𝔽).mul (emb (𝔽) m) (emb (𝔽) t), emb (𝔽) t) (2 * c + 1)) := hv
  rw [hv', hnr]
  have ht0 : emb (𝔽) t ≠ (𝔽).zero := fun h0 => hlt (dv_of_emb _ t h0)
  have hu := unit_pow (2 * (2 * c) + 1) hl _ ht0
  have hsplit : nrm (𝔽) ((𝔽).mul (emb (𝔽) m) (emb (𝔽) t), emb (𝔽) t) =
      (𝔽).mul ((𝔽).mul (emb (𝔽) t) (emb (𝔽) t)) (emb (𝔽) (m * m + 1)) := by
    show (𝔽).add ((𝔽).mul ((𝔽).mul (emb (𝔽) m) (emb (𝔽) t)) ((𝔽).mul (emb (𝔽) m) (emb (𝔽) t))) ((𝔽).mul (emb (𝔽) t) (emb (𝔽) t)) = _
    rw [emb_add, emb_mul]; show _ = (𝔽).mul _ ((𝔽).add _ ((𝔽).add (𝔽).zero (𝔽).one)); lib_ring
  rw [hsplit, Frobenius.pw_mul, hm]
  have htt : Frobenius.pw (𝔽) ((𝔽).mul (emb (𝔽) t) (emb (𝔽) t)) (2 * c + 1) = (𝔽).one := by
    rw [← hu]
    show _ = Frobenius.pw (𝔽) (emb (𝔽) t) (2 * (2 * c) + 1 + 1)
    rw [show 2 * (2 * c) + 1 + 1 = 2 * (2 * c + 1) by lib_nat, ← TwoPower.pwpow]
    congr 1; show _ = (𝔽).mul ((𝔽).mul (𝔽).one _) _; lib_ring
  rw [htt]; lib_ring

end Cheb

#print axioms Cheb.val_sum
#print axioms Cheb.nrm_mul
#print axioms Cheb.nrm_pw
#print axioms Cheb.modl

lang/LucModQ.lean

lean_all/lang/LucModQ.lean

/-
  lang/LucModQ — T43, cycle 529: F(2 + mt) ≡ ℓ² (mod q) for every q | t, and F as a natural number.

    C_two, fd_two    C_k(2) = 2, f_d(2) = 2d + 1 = ℓ
    modq             q | t ⇒ emb N = emb (ℓ²) in ℤ/q        (x = 2 + t(m + i) ≡ 2)
    natF             A² + B² = Int.ofNat N for N = |A|² + |B|²
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.LucModL
import lang.GaussEuclid

namespace Cheb

open FTAChain ZMod LibRing LibInt

section G
variable {R : Type} (K : DetChain.CRing R)

theorem C_two : ∀ k, C K (K.add K.one K.one) k = K.add K.one K.one ∧ C K (K.add K.one K.one) (k + 1) = K.add K.one K.one
  | 0 => ⟨rfl, rfl⟩
  | k + 1 => by
    obtain ⟨h0, h1⟩ := C_two k
    refine ⟨h1, ?_⟩
    show K.add (K.mul (K.add K.one K.one) (C K (K.add K.one K.one) (k + 1))) (K.neg (C K (K.add K.one K.one) k)) = _
    rw [h0, h1]; lib_ring

theorem fd_two : ∀ d, fd K (K.add K.one K.one) d = emb K (2 * d + 1)
  | 0 => by show K.one = K.add K.zero K.one; lib_ring
  | d + 1 => by
    show K.add (fd K (K.add K.one K.one) d) (C K (K.add K.one K.one) (d + 1)) = K.add (K.add (emb K (2 * d + 1)) K.one) K.one
    rw [fd_two d, (C_two K d).2]; lib_ring

theorem c_fd_e (a : R) (d : Nat) : fd (Cyclo4.cyc4 K) (a, K.zero) d = (fd K a d, K.zero) :=
  (fd_map K (Cyclo4.cyc4 K) (fun x => (x, K.zero)) (fun x y => (Prod.ext rfl (show K.zero = K.add K.zero K.zero by lib_ring)))
    (fun x y => (c_mul_e K x y).symm) (fun x => Prod.ext rfl (show K.zero = K.neg K.zero by lib_ring)) rfl a d).symm
end G

theorem modq (q' m t d N : Nat) (hqt : Dv (q' + 1) t)
    (hN : (fd (Cyclo4.cyc4 intR) (Int.ofNat (2 + m * t), Int.ofNat t) d).1 * (fd (Cyclo4.cyc4 intR) (Int.ofNat (2 + m * t), Int.ofNat t) d).1 +
      (fd (Cyclo4.cyc4 intR) (Int.ofNat (2 + m * t), Int.ofNat t) d).2 * (fd (Cyclo4.cyc4 intR) (Int.ofNat (2 + m * t), Int.ofNat t) d).2 = Int.ofNat N) :
    emb (ring q') N = emb (ring q') ((2 * d + 1) * (2 * d + 1)) := by
  rw [val_sum q' (2 + m * t) t d N hN]
  have h0 : emb (ring q') t = (ring q').zero := emb_dv q' t hqt
  have h2 : emb (ring q') (2 + m * t) = (ring q').add (ring q').one (ring q').one := by
    rw [emb_add, emb_mul, h0]
    show (ring q').add ((ring q').add ((ring q').add (ring q').zero (ring q').one) (ring q').one) ((ring q').mul _ (ring q').zero) = _
    lib_ring
  rw [h0, h2, c_fd_e, fd_two, emb_mul]
  show (ring q').add ((ring q').mul (emb (ring q') (2 * d + 1)) (emb (ring q') (2 * d + 1))) ((ring q').mul (ring q').zero (ring q').zero) = _
  lib_ring

theorem natF (A B : Int) : ∃ N : Nat, A * A + B * B = Int.ofNat N :=
  ⟨Int.natAbs A * Int.natAbs A + Int.natAbs B * Int.natAbs B, by
    show A * A + B * B = Int.ofNat (Int.natAbs A * Int.natAbs A) + Int.ofNat (Int.natAbs B * Int.natAbs B)
    rw [GaussEuclid.nas, GaussEuclid.nas]⟩

end Cheb

#print axioms Cheb.C_two
#print axioms Cheb.fd_two
#print axioms Cheb.c_fd_e
#print axioms Cheb.modq
#print axioms Cheb.natF

lang/LucMul.lean

lean_all/lang/LucMul.lean

/-
  lang/LucMul — T43, cycle 492: the Lucas splitting U_{mn}(s) = U_m(s)·U_n(V_m(s)) in ℕ (the analogue of TwoPrime.gN_split).

  U_k = (lu t k).1, V_k = (cd t k).1 (= C_k(s)), s = t + 2.
    add_rc      a + c = b + c ⇒ a = b   (core Nat.add_right_cancel uses propext)
    lu_add      U_{j+2m} + U_j = U_{j+m}·V_m          (two-step induction on m: V_0 = 2, V_1 = s, V_{m+2} + V_m = s·V_{m+1})
    lu_mul      V_m = t' + 2 ⇒ U_{mn}(s) = U_m(s)·U_n(t' + 2)   (two-step induction on n)
  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.LucU

namespace Cheb

open FTAChain

theorem add_rc (c : Nat) : ∀ a b, a + c = b + c → a = b := by
  induction c with
  | zero => intro a b h; exact h
  | succ c ih => intro a b h; exact ih a b (Nat.succ.inj h)

theorem lu_add2 (t : Nat) : ∀ m, (∀ j, (lu t (j + m + m)).1 + (lu t j).1 = (lu t (j + m)).1 * (cd t m).1) ∧
    (∀ j, (lu t (j + (m + 1) + (m + 1))).1 + (lu t j).1 = (lu t (j + (m + 1))).1 * (cd t (m + 1)).1)
  | 0 => by
    refine ⟨fun j => ?_, fun j => ?_⟩
    · show (lu t j).1 + (lu t j).1 = (lu t j).1 * 2
      generalize (lu t j).1 = u; lib_nat
    · rw [show j + (0 + 1) + (0 + 1) = j + 2 by lib_nat, lu_rec]
      show (t + 2) * (lu t (j + 1)).1 = (lu t (j + 1)).1 * (2 + t)
      generalize (lu t (j + 1)).1 = u; lib_nat
  | m + 1 => by
    obtain ⟨P0, P1⟩ := lu_add2 t m
    refine ⟨P1, fun j => ?_⟩
    have A := P1 (j + 1)
    have B := P0 (j + 2)
    have R1 := lu_rec t (j + m + m + 2)
    have R2 := lu_rec t j
    have R3 := rec2 t m
    rw [show j + 1 + (m + 1) + (m + 1) = j + m + m + 2 + 1 by lib_nat, show j + 1 + (m + 1) = j + m + 2 by lib_nat] at A
    rw [show j + 2 + m + m = j + m + m + 2 by lib_nat, show j + 2 + m = j + m + 2 by lib_nat] at B
    rw [show j + (m + 1 + 1) + (m + 1 + 1) = j + m + m + 2 + 2 by lib_nat, show j + (m + 1 + 1) = j + m + 2 by lib_nat]
    generalize (lu t (j + m + m + 2 + 2)).1 = a4 at R1 ⊢
    generalize (lu t (j + m + m + 2 + 1)).1 = a3 at R1 A
    generalize (lu t (j + m + m + 2)).1 = a2 at R1 B
    generalize (lu t (j + 2)).1 = b2 at R2 B
    generalize (lu t (j + 1)).1 = b1 at R2 A
    generalize (lu t j).1 = b0 at R2 ⊢
    generalize (lu t (j + m + 2)).1 = c at A B ⊢
    generalize (cd t (m + 1 + 1)).1 = v2 at R3 ⊢
    generalize (cd t (m + 1)).1 = v1 at R3 A
    generalize (cd t m).1 = v0 at R3 B
    apply add_rc (a2 + b2)
    calc a4 + b0 + (a2 + b2) = (a4 + a2) + (b2 + b0) := by lib_nat
      _ = (t + 2) * a3 + (t + 2) * b1 := by rw [R1, R2]
      _ = (t + 2) * (a3 + b1) := by lib_nat
      _ = (t + 2) * (c * v1) := by rw [A]
      _ = c * ((t + 2) * v1) := by lib_nat
      _ = c * (v2 + v0) := by rw [R3]
      _ = c * v2 + c * v0 := by lib_nat
      _ = c * v2 + (a2 + b2) := by rw [B]

theorem lu_add (t m j : Nat) : (lu t (j + m + m)).1 + (lu t j).1 = (lu t (j + m)).1 * (cd t m).1 := (lu_add2 t m).1 j

theorem lu_mul2 (t t' m : Nat) (hv : (cd t m).1 = t' + 2) : ∀ n,
    (lu t (m * n)).1 = (lu t m).1 * (lu t' n).1 ∧ (lu t (m * (n + 1))).1 = (lu t m).1 * (lu t' (n + 1)).1
  | 0 => by
    refine ⟨?_, ?_⟩
    · show 0 = (lu t m).1 * 0; rfl
    · show (lu t (m * 1)).1 = (lu t m).1 * (0 + 1)
      rw [Nat.mul_one, Nat.zero_add, Nat.mul_one]
  | n + 1 => by
    obtain ⟨h0, h1⟩ := lu_mul2 t t' m hv n
    refine ⟨h1, ?_⟩
    have L := lu_add t m (m * n)
    rw [show m * n + m + m = m * (n + 1 + 1) by lib_nat, show m * n + m = m * (n + 1) by lib_nat, hv, h0, h1] at L
    have R := lu_rec t' n
    apply add_rc ((lu t m).1 * (lu t' n).1)
    rw [L]
    generalize (lu t m).1 = um at R ⊢
    generalize (lu t' (n + 2)).1 = x2 at R ⊢
    generalize (lu t' (n + 1)).1 = x1 at R ⊢
    generalize (lu t' n).1 = x0 at R ⊢
    calc um * x1 * (t' + 2) = um * ((t' + 2) * x1) := by lib_nat
      _ = um * (x2 + x0) := by rw [R]
      _ = um * x2 + um * x0 := by lib_nat

theorem lu_mul (t t' m n : Nat) (hv : (cd t m).1 = t' + 2) : (lu t (m * n)).1 = (lu t m).1 * (lu t' n).1 :=
  (lu_mul2 t t' m hv n).1

end Cheb

#print axioms Cheb.add_rc
#print axioms Cheb.lu_add2
#print axioms Cheb.lu_add
#print axioms Cheb.lu_mul2
#print axioms Cheb.lu_mul

lang/LucNonRes.lean

lean_all/lang/LucNonRes.lean

/-
  lang/LucNonRes — T43, cycle 527: for a prime ℓ = 4c + 3 there is m ∈ ℕ with (m² + 1)^((ℓ−1)/2) = −1 in 𝔽_ℓ.

  If every square plus one were a square (or 0), every k = 0, 1, 2, … would be a square, also −1 = ℓ − 1; but −1 is not a
  square for ℓ ≡ 3 (mod 4) (LucQ1.sq_sum_zero). Constructive, by induction on k with Euler's criterion (Euler.euler_pm,
  EulerHard.euler_hard).
    sq_or_found, nonres
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.LucQ1
import lang.ChebCong

namespace Cheb

open FTAChain ZMod

variable (c : Nat)

local notation "𝔽" => ring (2 * (2 * c) + 1 + 1)

theorem sq_or_found (hl : ∀ d, Dv d (2 * (2 * c) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * c) + 1 + 1 + 1) : ∀ k,
    (∃ y : Zp (2 * (2 * c) + 1 + 1), (𝔽).mul y y = emb (𝔽) k) ∨
    (∃ m : Nat, Frobenius.pw (𝔽) (emb (𝔽) (m * m + 1)) (2 * c + 1) = (𝔽).neg (𝔽).one)
  | 0 => Or.inl ⟨(𝔽).zero, by show _ = (𝔽).zero; lib_ring⟩
  | k + 1 => by
    rcases sq_or_found hl k with ⟨y, hy⟩ | hf
    · have hyM : emb (𝔽) y.val = y := by rw [emb_mk]; exact mk_val _ y
      have ht : emb (𝔽) (y.val * y.val + 1) = emb (𝔽) (k + 1) := by
        rw [emb_add, emb_mul, hyM, hy]; rfl
      by_cases h0 : emb (𝔽) (k + 1) = (𝔽).zero
      · exact Or.inl ⟨(𝔽).zero, by rw [h0]; lib_ring⟩
      · rw [← ht] at h0
        rcases Euler.euler_pm (2 * c) hl _ h0 with h | h
        · obtain ⟨z, hz⟩ := EulerHard.euler_hard (2 * c) hl _ h
          exact Or.inl ⟨z, by rw [hz, ht]⟩
        · exact Or.inr ⟨y.val, h⟩
    · exact Or.inr hf

theorem nonres (hl : ∀ d, Dv d (2 * (2 * c) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * c) + 1 + 1 + 1) :
    ∃ m : Nat, Frobenius.pw (𝔽) (emb (𝔽) (m * m + 1)) (2 * c + 1) = (𝔽).neg (𝔽).one := by
  rcases sq_or_found c hl (2 * (2 * c) + 1 + 1) with ⟨y, hy⟩ | h
  · exfalso
    have hm1 : emb (𝔽) (2 * (2 * c) + 1 + 1) = (𝔽).neg (𝔽).one := by
      have h0 := emb_self (2 * (2 * c) + 1 + 1)
      show emb (𝔽) (2 * (2 * c) + 1 + 1) = _
      calc _ = (𝔽).add (emb (𝔽) (2 * (2 * c) + 1 + 1 + 1)) ((𝔽).neg (𝔽).one) := by
            show _ = (𝔽).add ((𝔽).add (emb (𝔽) (2 * (2 * c) + 1 + 1)) (𝔽).one) ((𝔽).neg (𝔽).one); lib_ring
        _ = _ := by rw [h0]; lib_ring
    rw [hm1] at hy
    have hs : (𝔽).add ((𝔽).mul y y) ((𝔽).mul (𝔽).one (𝔽).one) = (𝔽).zero := by rw [hy]; lib_ring
    have h1 := (sq_sum_zero c hl y (𝔽).one hs).2
    exact Fp2Sqrt.two_ne (2 * c) (by rw [h1]; lib_ring)
  · exact h

end Cheb

#print axioms Cheb.sq_or_found
#print axioms Cheb.nonres

lang/LucQ1.lean

lean_all/lang/LucQ1.lean

/-
  lang/LucQ1 — T43, cycle 524: in 𝔽_r with r ≡ 3 (mod 4), a² + b² = 0 forces a = b = 0; hence a prime r ≡ 3 (mod 4), r ≠ ℓ,
  never divides F(n) = |ψ_ℓ(n + i)|² when ℓ ≡ 3 (mod 4).

    sq_sum_zero   r = 4e + 3 prime, a² + b² = 0 in 𝔽_r ⇒ a = 0 ∧ b = 0      (b ≠ 0 would give (a/b)² = −1, but (−1)^((r−1)/2) = −1)
    no_r3         r = 4e + 3, ℓ = 4c + 3 primes, r ≠ ℓ, r | F(n) ⇒ False   (LucGaussZ.red_sum, LucK5.k5)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.LucK5
import lang.LucGaussZ

namespace Cheb

open FTAChain ZMod LibRing LibInt

variable (e : Nat)

local notation "𝔽" => ring (2 * (2 * e) + 1 + 1)

theorem sq_sum_zero (hpr : ∀ d, Dv d (2 * (2 * e) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * e) + 1 + 1 + 1)
    (a b : Zp (2 * (2 * e) + 1 + 1)) (h : (𝔽).add ((𝔽).mul a a) ((𝔽).mul b b) = (𝔽).zero) : a = (𝔽).zero ∧ b = (𝔽).zero := by
  have hb : b = (𝔽).zero := by
    by_cases hb0 : b = (𝔽).zero
    · exact hb0
    · exfalso
      have hinv := inv_exists (2 * (2 * e) + 1) hpr b hb0
      generalize Frobenius.pw (𝔽) b (2 * (2 * e) + 1) = v at hinv
      -- y = a·v, y² = −1
      have hy : (𝔽).mul ((𝔽).mul a v) ((𝔽).mul a v) = (𝔽).neg (𝔽).one := by
        calc _ = (𝔽).add ((𝔽).mul ((𝔽).mul v v) ((𝔽).add ((𝔽).mul a a) ((𝔽).mul b b)))
              ((𝔽).neg ((𝔽).mul ((𝔽).mul v b) ((𝔽).mul v b))) := by lib_ring
          _ = _ := by rw [h, hinv]; lib_ring
      have hy0 : (𝔽).mul a v ≠ (𝔽).zero := fun e0 => by
        rw [e0] at hy
        have hz : (𝔽).neg (𝔽).one = (𝔽).zero := hy.symm.trans (by lib_ring)
        exact Fp2Sqrt.two_ne (2 * e) (by
          calc (𝔽).add (𝔽).one (𝔽).one = (𝔽).add ((𝔽).neg ((𝔽).neg (𝔽).one)) ((𝔽).neg ((𝔽).neg (𝔽).one)) := by lib_ring
            _ = (𝔽).add ((𝔽).neg (𝔽).zero) ((𝔽).neg (𝔽).zero) := by rw [hz]
            _ = (𝔽).zero := by lib_ring)
      have hu := unit_pow (2 * (2 * e) + 1) hpr _ hy0
      have hex : Frobenius.pw (𝔽) ((𝔽).mul a v) (2 * (2 * e) + 1 + 1) = Frobenius.pw (𝔽) (Frobenius.pw (𝔽) ((𝔽).mul a v) 2) (2 * e + 1) := by
        rw [TwoPower.pwpow]; exact congrArg (Frobenius.pw (𝔽) ((𝔽).mul a v)) (by lib_nat)
      rw [hex] at hu
      have h2 : Frobenius.pw (𝔽) ((𝔽).mul a v) 2 = (𝔽).neg (𝔽).one := by
        rw [← hy]; show (𝔽).mul ((𝔽).mul (𝔽).one _) _ = _; lib_ring
      rw [h2, pw_m1] at hu
      exact Fp2Sqrt.two_ne (2 * e) (by
        calc (𝔽).add (𝔽).one (𝔽).one = (𝔽).add (𝔽).one ((𝔽).neg (𝔽).one) := by rw [hu]
          _ = (𝔽).zero := by lib_ring)
  refine ⟨?_, hb⟩
  rw [hb] at h
  rcases nozd (2 * (2 * e) + 1) hpr a a (by rw [← h]; lib_ring) with h' | h'
  · exact h'
  · exact h'

theorem no_r3 (c n N : Nat) (hpr : ∀ d, Dv d (2 * (2 * e) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * e) + 1 + 1 + 1)
    (hl : ∀ d, Dv d (2 * (2 * c) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * c) + 1 + 1 + 1)
    (hlr : Dv (2 * (2 * c) + 1 + 1 + 1) (2 * (2 * e) + 1 + 1 + 1) → False)
    (hN : (fd (Cyclo4.cyc4 intR) (Int.ofNat n, 1) (2 * c + 1)).1 * (fd (Cyclo4.cyc4 intR) (Int.ofNat n, 1) (2 * c + 1)).1 +
      (fd (Cyclo4.cyc4 intR) (Int.ofNat n, 1) (2 * c + 1)).2 * (fd (Cyclo4.cyc4 intR) (Int.ofNat n, 1) (2 * c + 1)).2 = Int.ofNat N)
    (hr : Dv (2 * (2 * e) + 1 + 1 + 1) N) : False := by
  have hs := red_sum (2 * (2 * e) + 1 + 1) n (2 * c + 1) N hN hr
  obtain ⟨h1, h2⟩ := sq_sum_zero e hpr _ _ hs
  exact k5 e c hpr hl hlr (castN (𝔽) n) (Prod.ext h1 h2)

end Cheb

#print axioms Cheb.sq_sum_zero
#print axioms Cheb.no_r3

lang/LucRank.lean

lean_all/lang/LucRank.lean

/-
  lang/LucRank — T43, cycle 495: U_n(s) is a divisibility sequence modulo a prime (in ℕ, no ring).

  U_k = (lu t k).1, s = t + 2.
    lu_addf     U_{a+b+1} + U_a·U_b = U_{a+1}·U_{b+1}
    cassini     U_{a+1}² = U_a·U_{a+2} + 1
    ndv_succ    r prime, r | U_a ⇒ r ∤ U_{a+1}
    dv_addU     r prime, r | U_a:  r | U_b ⇔ r | U_{a+b}
  These replace the group law behind the order: the r-rank of apparition will be the least n with r | U_n.
  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.LucMul

namespace Cheb

open FTAChain

theorem dvA (d x y : Nat) (hx : Dv d x) (hy : Dv d y) : Dv d (x + y) := by
  obtain ⟨a, ha⟩ := hx; obtain ⟨b, hb⟩ := hy
  exact ⟨a + b, by rw [ha, hb]; lib_nat⟩

theorem lu_addf2 (t a : Nat) : ∀ b, (lu t (a + b + 1)).1 + (lu t a).1 * (lu t b).1 = (lu t (a + 1)).1 * (lu t (b + 1)).1 ∧
    (lu t (a + (b + 1) + 1)).1 + (lu t a).1 * (lu t (b + 1)).1 = (lu t (a + 1)).1 * (lu t (b + 1 + 1)).1
  | 0 => by
    refine ⟨?_, ?_⟩
    · show (lu t (a + 1)).1 + (lu t a).1 * 0 = (lu t (a + 1)).1 * (0 + 1)
      generalize (lu t (a + 1)).1 = u; generalize (lu t a).1 = v; lib_nat
    · have R := lu_rec t a
      show (lu t (a + 2)).1 + (lu t a).1 * (0 + 1) = (lu t (a + 1)).1 * ((0 + 1) + (t * (0 + 1) + 1))
      rw [show (lu t (a + 2)).1 + (lu t a).1 * (0 + 1) = (lu t (a + 2)).1 + (lu t a).1 by lib_nat, R]
      generalize (lu t (a + 1)).1 = u; lib_nat
  | b + 1 => by
    obtain ⟨h0, h1⟩ := lu_addf2 t a b
    refine ⟨h1, ?_⟩
    have R1 := lu_rec t (a + b + 1)
    have R2 := lu_rec t b
    have R3 := lu_rec t (b + 1)
    rw [show a + b + 1 + 2 = a + b + 3 by lib_nat, show a + b + 1 + 1 = a + b + 2 by lib_nat] at R1
    rw [show a + (b + 1) + 1 = a + b + 2 by lib_nat] at h1
    rw [show a + (b + 1 + 1) + 1 = a + b + 3 by lib_nat, show b + 1 + 1 + 1 = b + 1 + 2 by lib_nat]
    rw [show b + 1 + 1 = b + 2 by lib_nat] at h1 ⊢
    generalize (lu t (a + b + 3)).1 = x3 at R1 ⊢
    generalize (lu t (a + b + 2)).1 = x2 at R1 h1
    generalize (lu t (a + b + 1)).1 = x1 at R1 h0
    generalize (lu t (b + 1 + 2)).1 = y3 at R3 ⊢
    generalize (lu t (b + 2)).1 = y2 at R2 R3 h1 ⊢
    generalize (lu t (b + 1)).1 = y1 at R2 R3 h0 h1
    generalize (lu t b).1 = y0 at R2 h0
    generalize (lu t a).1 = ua at h0 h1 ⊢
    generalize (lu t (a + 1)).1 = ub at h0 h1 ⊢
    apply add_rc (x1 + ua * y0)
    calc x3 + ua * y2 + (x1 + ua * y0) = (x3 + x1) + ua * (y2 + y0) := by lib_nat
      _ = (t + 2) * x2 + ua * ((t + 2) * y1) := by rw [R1, R2]
      _ = (t + 2) * (x2 + ua * y1) := by lib_nat
      _ = (t + 2) * (ub * y2) := by rw [h1]
      _ = ub * ((t + 2) * y2) := by lib_nat
      _ = ub * (y3 + y1) := by rw [R3]
      _ = ub * y3 + ub * y1 := by lib_nat
      _ = ub * y3 + (x1 + ua * y0) := by rw [h0]

theorem lu_addf (t a b : Nat) : (lu t (a + b + 1)).1 + (lu t a).1 * (lu t b).1 = (lu t (a + 1)).1 * (lu t (b + 1)).1 :=
  (lu_addf2 t a b).1

theorem cassini (t : Nat) : ∀ a, (lu t (a + 1)).1 * (lu t (a + 1)).1 = (lu t a).1 * (lu t (a + 2)).1 + 1
  | 0 => by
    show (0 + 1) * (0 + 1) = 0 * (lu t 2).1 + 1
    generalize (lu t 2).1 = u; lib_nat
  | a + 1 => by
    have ih := cassini t a
    have R1 := lu_rec t a
    have R2 := lu_rec t (a + 1)
    rw [show a + 1 + 2 = a + 3 by lib_nat, show a + 1 + 1 = a + 2 by lib_nat] at R2
    rw [show a + 1 + 1 = a + 2 by lib_nat, show a + 1 + 2 = a + 3 by lib_nat]
    generalize (lu t (a + 3)).1 = u3 at R2 ⊢
    generalize (lu t (a + 2)).1 = u2 at R1 R2 ih ⊢
    generalize (lu t (a + 1)).1 = u1 at R1 R2 ih ⊢
    generalize (lu t a).1 = u0 at R1 ih
    apply add_rc (u0 * u2)
    calc u2 * u2 + u0 * u2 = u2 * (u2 + u0) := by lib_nat
      _ = u2 * ((t + 2) * u1) := by rw [R1]
      _ = u1 * ((t + 2) * u2) := by lib_nat
      _ = u1 * (u3 + u1) := by rw [R2]
      _ = u1 * u3 + u1 * u1 := by lib_nat
      _ = u1 * u3 + (u0 * u2 + 1) := by rw [ih]
      _ = u1 * u3 + 1 + u0 * u2 := by lib_nat

theorem ndv_succ (t l a : Nat) (ha : Dv (l + 1 + 1) (lu t a).1) (hb : Dv (l + 1 + 1) (lu t (a + 1)).1) : False := by
  have hc := cassini t a
  have h1 : Dv (l + 1 + 1) ((lu t a).1 * (lu t (a + 2)).1) := by
    obtain ⟨c, hc'⟩ := ha; exact ⟨c * (lu t (a + 2)).1, by rw [hc']; lib_nat⟩
  have h2 : Dv (l + 1 + 1) ((lu t a).1 * (lu t (a + 2)).1 + 1) := by
    rw [← hc]; obtain ⟨c, hc'⟩ := hb; exact ⟨c * (lu t (a + 1)).1, by rw [hc']; lib_nat⟩
  exact dv_one_absurd l (dv_sub (l + 1) _ 1 h1 h2)

theorem dv_addU (t l a : Nat) (hl : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1) (ha : Dv (l + 1 + 1) (lu t a).1) :
    ∀ b, Dv (l + 1 + 1) (lu t b).1 ↔ Dv (l + 1 + 1) (lu t (a + b)).1
  | 0 => ⟨fun _ => ha, fun _ => ⟨0, (Nat.zero_mul _).symm⟩⟩
  | b + 1 => by
    have hf := lu_addf t a b
    have hab : Dv (l + 1 + 1) ((lu t a).1 * (lu t b).1) := by
      obtain ⟨c, hc⟩ := ha; exact ⟨c * (lu t b).1, by rw [hc]; lib_nat⟩
    refine ⟨fun h => ?_, fun h => ?_⟩
    · have h2 : Dv (l + 1 + 1) ((lu t a).1 * (lu t b).1 + (lu t (a + b + 1)).1) := by
        rw [Nat.add_comm ((lu t a).1 * (lu t b).1), hf]; obtain ⟨c, hc⟩ := h; exact ⟨(lu t (a + 1)).1 * c, by rw [hc]; lib_nat⟩
      exact dv_sub (l + 1) _ _ hab h2
    · have h2 : Dv (l + 1 + 1) ((lu t (a + 1)).1 * (lu t (b + 1)).1) := by
        rw [← hf]; exact dvA _ _ _ h hab
      match hU : (lu t (a + 1)).1, h2 with
      | 0, _ => exact absurd (show Dv (l + 1 + 1) (lu t (a + 1)).1 from ⟨0, hU.trans (Nat.zero_mul _).symm⟩) (ndv_succ t l a ha)
      | u + 1, h2 =>
        rcases euclid_core l u _ hl h2 with e | e
        · exact absurd (show Dv (l + 1 + 1) (lu t (a + 1)).1 by rw [hU]; exact e) (ndv_succ t l a ha)
        · exact e

end Cheb

#print axioms Cheb.dvA
#print axioms Cheb.lu_addf2
#print axioms Cheb.lu_addf
#print axioms Cheb.cassini
#print axioms Cheb.ndv_succ
#print axioms Cheb.dv_addU

lang/LucRankEx.lean

lean_all/lang/LucRankEx.lean

/-
  lang/LucRankEx — T43, cycle 496: the rank of apparition of a prime r in U_n(s) (in ℕ).

  IsRank t l n :⇔ r | U_n ∧ ∀ m, r | U_m ⇒ n | m      (r = l + 2 prime, U_k = (lu t k).1, s = t + 2)
    dv_mulU       r | U_a ⇒ r | U_{ka}
    rank_dvd      the least positive n with r | U_n is the rank
    rank_exists   r | U_{N+1} ⇒ a rank exists
    rank_prime    q prime, r | U_q ⇒ the rank is q   (rank 1 is impossible: U_1 = 1)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.LucRank
import lang.Order

namespace Cheb

open FTAChain

def IsRank (t l n : Nat) : Prop := Dv (l + 1 + 1) (lu t n).1 ∧ ∀ m, Dv (l + 1 + 1) (lu t m).1 → Dv n m

theorem dv_mulU (t l a : Nat) (hl : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1) (ha : Dv (l + 1 + 1) (lu t a).1) :
    ∀ k, Dv (l + 1 + 1) (lu t (k * a)).1
  | 0 => by rw [Nat.zero_mul]; exact ⟨0, (Nat.zero_mul _).symm⟩
  | k + 1 => by
    rw [Nat.succ_mul]
    exact (dv_addU t l (k * a) hl (dv_mulU t l a hl ha k) a).1 ha

theorem rank_dvd (t l d : Nat) (hl : ∀ e, Dv e (l + 1 + 1) → e = 1 ∨ e = l + 1 + 1) (hd : Dv (l + 1 + 1) (lu t (d + 1)).1)
    (hmin : ∀ ρ, 0 < ρ → ρ < d + 1 → Dv (l + 1 + 1) (lu t ρ).1 → False) : IsRank t l (d + 1) := by
  refine ⟨hd, fun m hm => ?_⟩
  obtain ⟨q, ρ, hqr, hρ⟩ := Order.divmod d m
  have hqa := dv_mulU t l (d + 1) hl hd q
  rw [hqr] at hm
  have hr := (dv_addU t l (q * (d + 1)) hl hqa ρ).2 hm
  match ρ, hr, hρ, hqr with
  | 0, _, _, hqr => exact ⟨q, hqr⟩
  | ρ + 1, hr, hρ, _ => exact absurd hr (hmin (ρ + 1) (Nat.zero_lt_succ ρ) hρ)

theorem rank_exists (t l N : Nat) (hl : ∀ e, Dv e (l + 1 + 1) → e = 1 ∨ e = l + 1 + 1) (h : Dv (l + 1 + 1) (lu t (N + 1)).1) :
    ∃ d, IsRank t l (d + 1) := by
  have search : ∀ m, (∃ d, Dv (l + 1 + 1) (lu t (d + 1)).1 ∧ ∀ ρ, 0 < ρ → ρ < d + 1 → Dv (l + 1 + 1) (lu t ρ).1 → False) ∨
      (∀ ρ, 0 < ρ → ρ < m + 1 → Dv (l + 1 + 1) (lu t ρ).1 → False) := by
    intro m
    induction m with
    | zero => exact Or.inr (fun ρ h0 h1 _ => Nat.lt_irrefl 0 (Nat.lt_of_lt_of_le h0 (Nat.le_of_lt_succ h1)))
    | succ m ih =>
      cases ih with
      | inl h => exact Or.inl h
      | inr h =>
        rcases dv_dec (l + 1) (lu t (m + 1)).1 with hm | hm
        · exact Or.inl ⟨m, hm, h⟩
        · refine Or.inr (fun ρ h0 h1 hd => ?_)
          cases Nat.lt_or_ge ρ (m + 1) with
          | inl hlt => exact h ρ h0 hlt hd
          | inr hge =>
            have e : ρ = m + 1 := Nat.le_antisymm (Nat.le_of_lt_succ h1) hge
            rw [e] at hd; exact hm hd
  cases search (N + 1) with
  | inl h' => obtain ⟨d, hd, hmin⟩ := h'; exact ⟨d, rank_dvd t l d hl hd hmin⟩
  | inr h' => exact absurd h (h' (N + 1) (Nat.zero_lt_succ N) (Nat.lt_succ_self (N + 1)))

theorem rank_prime (t l j : Nat) (hl : ∀ e, Dv e (l + 1 + 1) → e = 1 ∨ e = l + 1 + 1)
    (hq : ∀ e, Dv e (j + 1 + 1) → e = 1 ∨ e = j + 1 + 1) (h : Dv (l + 1 + 1) (lu t (j + 1 + 1)).1) : IsRank t l (j + 1 + 1) := by
  obtain ⟨d, hR⟩ := rank_exists t l (j + 1) hl h
  rcases hq _ (hR.2 _ h) with e | e
  · exfalso
    have hd0 : d = 0 := Nat.succ.inj e
    have h1 := hR.1
    rw [hd0] at h1
    exact dv_one_absurd l h1
  · rw [← e]; exact hR

end Cheb

#print axioms Cheb.dv_mulU
#print axioms Cheb.rank_dvd
#print axioms Cheb.rank_exists
#print axioms Cheb.rank_prime

lang/LucRankV.lean

lean_all/lang/LucRankV.lean

/-
  lang/LucRankV — T43, cycle 497: the rank of apparition under s ↦ V_q(s) (Lucas analogues of OrdQ.ord_pow, ord_of_pow).

  q prime, V_q(s) = t' + 2 (so U_{qm}(s) = U_q(s)·U_m(t' + 2), LucMul.lu_mul), r prime, r ∤ U_q(s).
    dv_eq         a | b, b | a, a, b ≥ 1 ⇒ a = b
    dvW_iff       r | U_m(V_q) ⇔ r | U_{qm}(s)
    rankV_pow     rank_r(s) = n, q ∤ n ⇒ rank_r(V_q(s)) = n
    rankV_of      rank_r(V_q(s)) = n ⇒ rank_r(s) = n or nq
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.LucRankEx
import lang.OrdQ

namespace Cheb

open FTAChain

theorem dv_eq (a b : Nat) (ha : 1 ≤ a) (hb : 1 ≤ b) (h1 : Dv a b) (h2 : Dv b a) : a = b := by
  obtain ⟨a', ha'⟩ := le_dest2 1 a ha
  obtain ⟨b', hb'⟩ := le_dest2 1 b hb
  rw [← ha', ← hb'] at h1 h2 ⊢
  exact le_antisym' _ _ (dv_le _ b' h1) (dv_le _ a' h2)

theorem dvW_iff (t t' l j : Nat) (hl : ∀ e, Dv e (l + 1 + 1) → e = 1 ∨ e = l + 1 + 1)
    (hv : (cd t (j + 1 + 1)).1 = t' + 2) (hrq : Dv (l + 1 + 1) (lu t (j + 1 + 1)).1 → False) (m : Nat) :
    Dv (l + 1 + 1) (lu t' m).1 ↔ Dv (l + 1 + 1) (lu t ((j + 1 + 1) * m)).1 := by
  rw [lu_mul t t' (j + 1 + 1) m hv]
  refine ⟨fun h => dv_mul_left _ _ _ h, fun h => ?_⟩
  match hU : (lu t (j + 1 + 1)).1, h with
  | 0, _ => exact absurd ⟨0, hU.trans (Nat.zero_mul _).symm⟩ hrq
  | u + 1, h =>
    rcases euclid_core l u _ hl h with e | e
    · exact absurd (show Dv (l + 1 + 1) (lu t (j + 1 + 1)).1 by rw [hU]; exact e) hrq
    · exact e

theorem rankV_pow (t t' l j n : Nat) (hl : ∀ e, Dv e (l + 1 + 1) → e = 1 ∨ e = l + 1 + 1)
    (hq : ∀ e, Dv e (j + 1 + 1) → e = 1 ∨ e = j + 1 + 1)
    (hv : (cd t (j + 1 + 1)).1 = t' + 2) (hrq : Dv (l + 1 + 1) (lu t (j + 1 + 1)).1 → False)
    (hn : 1 ≤ n) (hqn : Dv (j + 1 + 1) n → False) (hR : IsRank t l n) : IsRank t' l n := by
  refine ⟨(dvW_iff t t' l j hl hv hrq n).2 (dv_mulU t l n hl hR.1 (j + 1 + 1)), fun m hm => ?_⟩
  exact OrdQ.coprime_dv n j m hn hq hqn (hR.2 _ ((dvW_iff t t' l j hl hv hrq m).1 hm))

theorem rankV_of (t t' l j n : Nat) (hl : ∀ e, Dv e (l + 1 + 1) → e = 1 ∨ e = l + 1 + 1)
    (hq : ∀ e, Dv e (j + 1 + 1) → e = 1 ∨ e = j + 1 + 1)
    (hv : (cd t (j + 1 + 1)).1 = t' + 2) (hrq : Dv (l + 1 + 1) (lu t (j + 1 + 1)).1 → False)
    (hn : 1 ≤ n) (hR' : IsRank t' l n) : IsRank t l n ∨ IsRank t l (n * (j + 1 + 1)) := by
  have h1 := (dvW_iff t t' l j hl hv hrq n).1 hR'.1
  obtain ⟨n', hn'⟩ := le_dest2 1 n hn
  have hqn1 : 1 ≤ (j + 1 + 1) * n := Nat.mul_le_mul (Nat.le_add_left 1 (j + 1)) hn
  obtain ⟨N, hN⟩ := le_dest2 1 _ hqn1
  rw [← hN] at h1
  obtain ⟨d, hR⟩ := rank_exists t l N hl h1
  -- E(m): n | m ⇔ d + 1 | q·m
  have E1 : ∀ m, Dv n m → Dv (d + 1) ((j + 1 + 1) * m) := fun m h => by
    obtain ⟨c, hc⟩ := h
    have := dv_mulU t' l n hl hR'.1 c
    rw [← hc] at this
    exact hR.2 _ ((dvW_iff t t' l j hl hv hrq m).1 this)
  have E2 : ∀ m, Dv (d + 1) ((j + 1 + 1) * m) → Dv n m := fun m h => by
    obtain ⟨c, hc⟩ := h
    have := dv_mulU t l (d + 1) hl hR.1 c
    rw [← hc] at this
    exact hR'.2 _ ((dvW_iff t t' l j hl hv hrq m).2 this)
  rcases dv_dec (j + 1) (d + 1) with hqd | hqd
  · obtain ⟨e, he⟩ := hqd
    have he1 : 1 ≤ e := by
      match e, he with
      | 0, he => rw [Nat.zero_mul] at he; exact absurd he (fun h => Nat.noConfusion h)
      | e + 1, _ => exact Nat.le_add_left 1 e
    have hen : Dv e n := by
      obtain ⟨c, hc⟩ := E1 n ⟨1, (Nat.one_mul n).symm⟩
      rw [he] at hc
      refine ⟨c, Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (j + 1)) ?_⟩
      rw [hc]; lib_nat
    have hne : Dv n e := E2 e ⟨1, by rw [he]; lib_nat⟩
    have hen' := dv_eq e n he1 hn hen hne
    refine Or.inr ?_
    rw [← hen', ← he]; exact hR
  · have h2 : Dv (d + 1) n := OrdQ.coprime_dv (d + 1) j n (Nat.le_add_left 1 d) hq hqd (E1 n ⟨1, (Nat.one_mul n).symm⟩)
    have h3 : Dv n (d + 1) := E2 (d + 1) ⟨j + 1 + 1, rfl⟩
    refine Or.inl ?_
    rw [← dv_eq (d + 1) n (Nat.le_add_left 1 d) hn h2 h3]; exact hR

end Cheb

#print axioms Cheb.dv_eq
#print axioms Cheb.dvW_iff
#print axioms Cheb.rankV_pow
#print axioms Cheb.rankV_of

lang/LucRoot.lean

lean_all/lang/LucRoot.lean

/-
  lang/LucRoot — T43, cycle 493: x = z + 1/z in the torus of 𝔽_r or 𝔽_r[√D] (extracted from ChebRoot for the Lucas route).

  r = 2m + 3 prime, x ∈ 𝔽_r, D = x² − 4.
    tor_sq      D = s² in 𝔽_r  ⟹  ∃ u v ∈ 𝔽_r, uv = 1, u + v = x, u^(r−1) = 1
    tor_nsq     D^((r−1)/2) = −1  ⟹  ∃ z w ∈ 𝔽_r[√D], zw = 1, z + w = (x, 0), z^(r+1) = 1
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.ChebRoot

namespace Cheb

open FTAChain ZMod YMChain ConicSum G4Gen G4Map

variable (m : Nat)

local notation "𝔽" => ring (2 * m + 1 + 1)

theorem tor_sq (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1)
    (h2 : (𝔽).add (𝔽).one (𝔽).one ≠ (𝔽).zero) (x s : Zp (2 * m + 1 + 1))
    (hs : (𝔽).mul s s = (𝔽).add ((𝔽).mul x x) ((𝔽).neg ((𝔽).mul ((𝔽).add (𝔽).one (𝔽).one) ((𝔽).add (𝔽).one (𝔽).one)))) :
    ∃ u v : Zp (2 * m + 1 + 1), (𝔽).mul u v = (𝔽).one ∧ (𝔽).add u v = x ∧ Frobenius.pw (𝔽) u (2 * m + 1 + 1) = (𝔽).one := by
  have ei : (𝔽).mul (iv m ((𝔽).add (𝔽).one (𝔽).one)) ((𝔽).add (𝔽).one (𝔽).one) = (𝔽).one := iv_mul_self m hpr _ h2
  generalize iv m ((𝔽).add (𝔽).one (𝔽).one) = i at ei
  refine ⟨(𝔽).mul ((𝔽).add x s) i, (𝔽).mul ((𝔽).add x ((𝔽).neg s)) i, ?_, ?_, ?_⟩
  · calc _ = (𝔽).add ((𝔽).mul ((𝔽).mul i i) ((𝔽).add ((𝔽).mul x x) ((𝔽).neg ((𝔽).mul s s)))) (𝔽).zero := by lib_ring
      _ = (𝔽).mul ((𝔽).mul i ((𝔽).add (𝔽).one (𝔽).one)) ((𝔽).mul i ((𝔽).add (𝔽).one (𝔽).one)) := by rw [hs]; lib_ring
      _ = (𝔽).one := by rw [ei]; lib_ring
  · calc _ = (𝔽).mul x ((𝔽).mul i ((𝔽).add (𝔽).one (𝔽).one)) := by lib_ring
      _ = x := by rw [ei]; lib_ring
  · have huv : (𝔽).mul ((𝔽).mul ((𝔽).add x s) i) ((𝔽).mul ((𝔽).add x ((𝔽).neg s)) i) = (𝔽).one := by
      calc _ = (𝔽).add ((𝔽).mul ((𝔽).mul i i) ((𝔽).add ((𝔽).mul x x) ((𝔽).neg ((𝔽).mul s s)))) (𝔽).zero := by lib_ring
        _ = (𝔽).mul ((𝔽).mul i ((𝔽).add (𝔽).one (𝔽).one)) ((𝔽).mul i ((𝔽).add (𝔽).one (𝔽).one)) := by rw [hs]; lib_ring
        _ = (𝔽).one := by rw [ei]; lib_ring
    generalize (𝔽).mul ((𝔽).add x s) i = u at huv ⊢
    generalize (𝔽).mul ((𝔽).add x ((𝔽).neg s)) i = v at huv
    have hu0 : u ≠ (𝔽).zero := fun e => one_ne m (by rw [← huv, e]; lib_ring)
    have hF := ZMod.fermat (2 * m + 1) hpr u
    have hz : (𝔽).mul u ((𝔽).add (Frobenius.pw (𝔽) u (2 * m + 1 + 1)) ((𝔽).neg (𝔽).one)) = (𝔽).zero := by
      calc _ = (𝔽).add ((𝔽).mul (Frobenius.pw (𝔽) u (2 * m + 1 + 1)) u) ((𝔽).neg u) := by lib_ring
        _ = (𝔽).zero := by rw [show (𝔽).mul (Frobenius.pw (𝔽) u (2 * m + 1 + 1)) u = u from hF]; lib_ring
    rcases mul_eq_zero m hpr _ _ hz with e | e
    · exact absurd e hu0
    · calc _ = (𝔽).add ((𝔽).add (Frobenius.pw (𝔽) u (2 * m + 1 + 1)) ((𝔽).neg (𝔽).one)) (𝔽).one := by lib_ring
        _ = (𝔽).one := by rw [e]; lib_ring

theorem tor_nsq (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1)
    (h2 : (𝔽).add (𝔽).one (𝔽).one ≠ (𝔽).zero) (x DD : Zp (2 * m + 1 + 1))
    (hDD : (𝔽).add ((𝔽).mul x x) ((𝔽).neg ((𝔽).mul ((𝔽).add (𝔽).one (𝔽).one) ((𝔽).add (𝔽).one (𝔽).one))) = DD)
    (hn : Frobenius.pw (𝔽) DD (m + 1) = (𝔽).neg (𝔽).one) :
    ∃ z w : Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1), (Fq2.G2 (2 * m + 1) DD).mul z w = (Fq2.G2 (2 * m + 1) DD).one ∧
      (Fq2.G2 (2 * m + 1) DD).add z w = (x, (𝔽).zero) ∧
      Frobenius.pw (Fq2.G2 (2 * m + 1) DD) z (2 * m + 1 + 1 + 1 + 1) = (Fq2.G2 (2 * m + 1) DD).one := by
  have ei : (𝔽).mul (iv m ((𝔽).add (𝔽).one (𝔽).one)) ((𝔽).add (𝔽).one (𝔽).one) = (𝔽).one := iv_mul_self m hpr _ h2
  generalize iv m ((𝔽).add (𝔽).one (𝔽).one) = i at ei
  refine ⟨((𝔽).mul x i, i), ((𝔽).mul x i, (𝔽).neg i), ?_, ?_, ?_⟩
  · apply Prod.ext
    · show (𝔽).add ((𝔽).mul ((𝔽).mul x i) ((𝔽).mul x i)) ((𝔽).mul DD ((𝔽).mul i ((𝔽).neg i))) = (𝔽).one
      rw [← hDD]
      calc _ = (𝔽).mul ((𝔽).mul i ((𝔽).add (𝔽).one (𝔽).one)) ((𝔽).mul i ((𝔽).add (𝔽).one (𝔽).one)) := by lib_ring
        _ = (𝔽).one := by rw [ei]; lib_ring
    · show (𝔽).add ((𝔽).mul ((𝔽).mul x i) ((𝔽).neg i)) ((𝔽).mul i ((𝔽).mul x i)) = (𝔽).zero
      lib_ring
  · apply Prod.ext
    · show (𝔽).add ((𝔽).mul x i) ((𝔽).mul x i) = x
      calc _ = (𝔽).mul x ((𝔽).mul i ((𝔽).add (𝔽).one (𝔽).one)) := by lib_ring
        _ = x := by rw [ei]; lib_ring
    · show (𝔽).add i ((𝔽).neg i) = (𝔽).zero
      lib_ring
  · rw [Fq2.pw_p1 (2 * m + 1) DD (m + 1) hpr rfl hn ((𝔽).mul x i, i)]
    apply Prod.ext
    · show (𝔽).add ((𝔽).mul ((𝔽).mul x i) ((𝔽).mul x i)) ((𝔽).neg ((𝔽).mul DD ((𝔽).mul i i))) = (𝔽).one
      rw [← hDD]
      calc _ = (𝔽).mul ((𝔽).mul i ((𝔽).add (𝔽).one (𝔽).one)) ((𝔽).mul i ((𝔽).add (𝔽).one (𝔽).one)) := by lib_ring
        _ = (𝔽).one := by rw [ei]; lib_ring
    · rfl

end Cheb

#print axioms Cheb.tor_sq
#print axioms Cheb.tor_nsq

lang/LucStep.lean

lean_all/lang/LucStep.lean

/-
  lang/LucStep — T43, cycle 500: the Lucas step invariant (PhiInv.step_inv with ranks of apparition).

  s = t + 2 with every prime of n·q dividing t (s ≡ 2); this survives s ↦ V_q(s).
    u_mod2      p | t ⇒ (p | U_m(s) ⇔ p | m)              (U_m(2) = m)
    v_mod2      p | t, V_k(s) = t′ + 2 ⇒ p | t′           (V_k(2) = 2)
    lstep_inv   LInv(n, t, F₀), LInv(n, t′, F₁), V_q(s) = t′ + 2, F₁ = F₀·F, n ≥ 2, q | t, primes of n divide t ⇒ LInv(nq, t, F)
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.LucInv
import lang.LucLTE
import lang.PhiInv

namespace Cheb

open FTAChain ZMod

theorem u_emb2 (p t : Nat) (ht : emb (ring p) t = (ring p).zero) : ∀ m,
    emb (ring p) (lu t m).1 = emb (ring p) m ∧ emb (ring p) (lu t m).2 = (ring p).one
  | 0 => ⟨rfl, by show (ring p).add (ring p).zero (ring p).one = _; lib_ring⟩
  | m + 1 => by
    obtain ⟨h0, h1⟩ := u_emb2 p t ht m
    refine ⟨?_, ?_⟩
    · show emb (ring p) ((lu t m).1 + (lu t m).2) = (ring p).add (emb (ring p) m) (ring p).one
      rw [emb_add, h0, h1]
    · show emb (ring p) (t * ((lu t m).1 + (lu t m).2) + (lu t m).2) = _
      rw [emb_add, emb_mul, ht, h1]; lib_ring

theorem u_mod2 (p' t m : Nat) (ht : Dv (p' + 1) t) : Dv (p' + 1) (lu t m).1 ↔ Dv (p' + 1) m := by
  have h := (u_emb2 p' t (emb_dv p' t ht) m).1
  exact ⟨fun hd => dv_of_emb p' m (h ▸ emb_dv p' _ hd), fun hd => dv_of_emb p' _ (h.trans (emb_dv p' m hd))⟩

theorem v_emb2 (p t : Nat) (ht : emb (ring p) t = (ring p).zero) : ∀ k,
    emb (ring p) (cd t k).1 = (ring p).add (ring p).one (ring p).one ∧ emb (ring p) (cd t k).2 = (ring p).zero
  | 0 => ⟨by show (ring p).add ((ring p).add (ring p).zero (ring p).one) (ring p).one = _; lib_ring, ht⟩
  | k + 1 => by
    obtain ⟨h0, h1⟩ := v_emb2 p t ht k
    refine ⟨?_, ?_⟩
    · show emb (ring p) ((cd t k).1 + (cd t k).2) = _
      rw [emb_add, h0, h1]; lib_ring
    · show emb (ring p) (t * ((cd t k).1 + (cd t k).2) + (cd t k).2) = _
      rw [emb_add, emb_mul, ht, h1]; lib_ring

theorem v_mod2 (p' t t' k : Nat) (ht : Dv (p' + 1) t) (hv : (cd t k).1 = t' + 2) : Dv (p' + 1) t' := by
  have h := (v_emb2 p' t (emb_dv p' t ht) k).1
  rw [hv, emb_add] at h
  apply dv_of_emb p' t'
  have e2 : emb (ring p') 2 = (ring p').add (ring p').one (ring p').one := by
    show (ring p').add ((ring p').add (ring p').zero (ring p').one) (ring p').one = _; lib_ring
  rw [e2] at h
  calc emb (ring p') t' = (ring p').add ((ring p').add (emb (ring p') t') ((ring p').add (ring p').one (ring p').one))
        ((ring p').neg ((ring p').add (ring p').one (ring p').one)) := by lib_ring
    _ = _ := by rw [h]; lib_ring

theorem lstep_inv (t t' j n' F0 F1 F : Nat) (hq : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1)
    (hv : (cd t (j + 1 + 1)).1 = t' + 2) (hqt : Dv (j + 1 + 1) t)
    (hnt : ∀ l, (∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1) → Dv (l + 1 + 1) (n' + 1 + 1) → Dv (l + 1 + 1) t)
    (hqn : Dv (j + 1 + 1) (n' + 1 + 1) → False)
    (hF0 : 1 ≤ F0) (I0 : LInv (n' + 1 + 1) t F0) (I1 : LInv (n' + 1 + 1) t' F1) (hF : F1 = F0 * F) :
    LInv ((n' + 1 + 1) * (j + 1 + 1)) t F := by
  intro l k hr
  have hn1 : 1 ≤ n' + 1 + 1 := Nat.le_add_left 1 (n' + 1)
  -- r = q is impossible for a prime of rank n (q | t, q ∤ n)
  have hrq_of_rank : IsRank t l (n' + 1 + 1) → l + 1 + 1 = j + 1 + 1 → False := fun hR e => by
    have := (u_mod2 (l + 1) t (n' + 1 + 1) (by rw [e]; exact hqt)).1 hR.1
    rw [e] at this; exact hqn this
  obtain ⟨t'', ht''⟩ := c_ge t (n' + 1 + 1)
  have hsplitA := lu_mul t t' (j + 1 + 1) (n' + 1 + 1) hv
  have hsplitB := lu_mul t t'' (n' + 1 + 1) (j + 1 + 1) ht''
  rw [Nat.mul_comm (j + 1 + 1) (n' + 1 + 1)] at hsplitA
  constructor
  · intro hk
    have hk1 : Dv ((l + 1 + 1) ^ (k + 1)) F1 := by rw [hF]; exact dv_mul_left F0 _ F hk
    obtain ⟨hR1, hg1⟩ := (I1 l k hr).1 hk1
    have hgq : Dv ((l + 1 + 1) ^ (k + 1)) (lu t ((n' + 1 + 1) * (j + 1 + 1))).1 := by
      rw [hsplitA]; exact dv_mul_left _ _ _ hg1
    -- r ∤ U_q(s)
    have hrU : Dv (l + 1 + 1) (lu t (j + 1 + 1)).1 → False := fun h => by
      have hrr : Dv (l + 1 + 1) (n' + 1 + 1) := by
        rcases dv_dec (l + 1) (n' + 1 + 1) with e | e
        · exact e
        · exact absurd (dv_trans _ _ _ ⟨(l + 1 + 1) ^ k, rfl⟩ hg1) (luc_ndv t t' l (j + 1 + 1) (n' + 1 + 1) h hv e)
      have hrt := hnt l hr hrr
      have hrqd := (u_mod2 (l + 1) t (j + 1 + 1) hrt).1 h
      rcases hq _ hrqd with e | e
      · exact Nat.noConfusion (Nat.succ.inj e)
      · rw [e] at hrr; exact hqn hrr
    rcases rankV_of t t' l j (n' + 1 + 1) hr hq hv hrU hn1 hR1 with hR | hR
    · exfalso
      obtain ⟨v, m, hvm, hm⟩ := Val.split l F0 F0 (Nat.le_refl F0) hF0
      obtain ⟨c, hc⟩ := hk
      have hv1 : Dv ((l + 1 + 1) ^ (v + 1)) F1 := ⟨m * c * (l + 1 + 1) ^ k, by
        rw [hF, hvm, hc]
        show (l + 1 + 1) ^ v * m * (c * ((l + 1 + 1) ^ k * (l + 1 + 1))) = m * c * (l + 1 + 1) ^ k * ((l + 1 + 1) ^ v * (l + 1 + 1))
        generalize (l + 1 + 1) ^ v = A; generalize (l + 1 + 1) ^ k = B; lib_nat⟩
      obtain ⟨_, hgv⟩ := (I1 l v hr).1 hv1
      have hgv2 : Dv ((l + 1 + 1) ^ (v + 1)) ((lu t'' (j + 1 + 1)).1 * (lu t (n' + 1 + 1)).1) := by
        rw [Nat.mul_comm, ← hsplitB, hsplitA]; exact dv_mul_left _ _ _ hgv
      have hnr : Dv (l + 1 + 1) (j + 1 + 1) → False := fun hd => by
        rcases hq _ hd with e | e
        · exact Nat.noConfusion (Nat.succ.inj e)
        · exact hrq_of_rank hR e
      have hc2 := luc_ndv t t'' l (n' + 1 + 1) (j + 1 + 1) hR.1 ht'' hnr
      have hgv3 := Val.ppow_cancel l _ hr hc2 (v + 1) _ hgv2
      have hF0v := (I0 l v hr).2 ⟨hR, hgv3⟩
      rw [hvm] at hF0v
      exact hm (PhiInv.ppow_strip l m v hF0v)
    · exact ⟨hR, hgq⟩
  · intro ⟨hR, hg⟩
    have hrU : Dv (l + 1 + 1) (lu t (j + 1 + 1)).1 → False := fun h => by
      obtain ⟨c, hc⟩ := hR.2 _ h
      exact PhiInv.no_one c n' (mul_cancel (j + 1) 1 (c * (n' + 1 + 1)) (by
        calc (j + 1 + 1) * 1 = c * ((n' + 1 + 1) * (j + 1 + 1)) := by rw [← hc]; lib_nat
          _ = _ := by lib_nat))
    rw [hsplitA] at hg
    have hg2 := Val.ppow_cancel l _ hr hrU (k + 1) _ hg
    have hR1 : IsRank t' l (n' + 1 + 1) := by
      refine ⟨dv_trans _ _ _ ⟨(l + 1 + 1) ^ k, rfl⟩ hg2, fun m hm => ?_⟩
      obtain ⟨c, hc⟩ := hR.2 _ ((dvW_iff t t' l j hr hv hrU m).1 hm)
      exact ⟨c, mul_cancel (j + 1) _ _ (hc.trans (by lib_nat))⟩
    have hF1 := (I1 l k hr).2 ⟨hR1, hg2⟩
    have hnF0 : Dv (l + 1 + 1) F0 → False := fun h => by
      have h' : Dv ((l + 1 + 1) ^ (0 + 1)) F0 := by rw [show (l + 1 + 1) ^ (0 + 1) = l + 1 + 1 from Val.pow1 _]; exact h
      obtain ⟨hRn, _⟩ := (I0 l 0 hr).1 h'
      have e := rank_uniq t l _ _ hn1 (Nat.le_trans hn1 (Nat.le_trans (Nat.le_of_eq (Nat.mul_one _).symm)
        (Nat.mul_le_mul_left _ (Nat.le_add_left 1 (j + 1))))) hRn hR
      exact PhiInv.no_one 1 j (mul_cancel (n' + 1) 1 (1 * (j + 1 + 1)) (by
        calc (n' + 1 + 1) * 1 = (n' + 1 + 1) * (j + 1 + 1) := by rw [← e]; exact Nat.mul_one _
          _ = _ := by lib_nat))
    rw [hF] at hF1
    exact Val.ppow_cancel l F0 hr hnF0 (k + 1) F hF1

end Cheb

#print axioms Cheb.u_emb2
#print axioms Cheb.u_mod2
#print axioms Cheb.v_emb2
#print axioms Cheb.v_mod2
#print axioms Cheb.lstep_inv

lang/LucU.lean

lean_all/lang/LucU.lean

/-
  lang/LucU — T43, cycle 491: the Lucas sequence U_k(s), s = t + 2, as natural numbers (toward p ≡ −1 (mod N) for every N).

  u₀ = 0, d₀ = 1, u_{k+1} = u_k + d_k, d_{k+1} = t·u_{k+1} + d_k   (d_k = u_{k+1} − u_k ≥ 0), so that u_{k+2} + u_k = s·u_{k+1}.
  If z·w = 1 and z + w = s in a commutative ring, then (z − w)·u_k = z^k − w^k: u_k plays the role of gN for the norm-one torus.
    lu, lu_rec      the pair (u_k, d_k);  u_{k+2} + u_k = (t + 2)·u_{k+1}
    lu_emb          (z − w)·emb u_k = z^k − w^k
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.ChebNat

namespace Cheb

open FTAChain

def lu (t : Nat) : Nat → Nat × Nat
  | 0 => (0, 1)
  | k + 1 => ((lu t k).1 + (lu t k).2, t * ((lu t k).1 + (lu t k).2) + (lu t k).2)

theorem lu_rec (t k : Nat) : (lu t (k + 2)).1 + (lu t k).1 = (t + 2) * (lu t (k + 1)).1 := by
  show ((lu t k).1 + (lu t k).2) + (t * ((lu t k).1 + (lu t k).2) + (lu t k).2) + (lu t k).1 = (t + 2) * ((lu t k).1 + (lu t k).2)
  generalize (lu t k).1 = u; generalize (lu t k).2 = d; lib_nat

variable {R : Type} (K : DetChain.CRing R)

theorem lu_emb (t : Nat) (z w : R) (hzw : K.mul z w = K.one)
    (hs : K.add z w = K.add (emb K t) (K.add K.one K.one)) : ∀ k,
    K.mul (K.add z (K.neg w)) (emb K (lu t k).1) = K.add (Frobenius.pw K z k) (K.neg (Frobenius.pw K w k)) ∧
    K.mul (K.add z (K.neg w)) (emb K (lu t k).2) =
      K.add (K.add (Frobenius.pw K z (k + 1)) (K.neg (Frobenius.pw K w (k + 1))))
        (K.neg (K.add (Frobenius.pw K z k) (K.neg (Frobenius.pw K w k))))
  | 0 => by
    refine ⟨?_, ?_⟩
    · show K.mul (K.add z (K.neg w)) K.zero = K.add K.one (K.neg K.one); lib_ring
    · show K.mul (K.add z (K.neg w)) (K.add K.zero K.one) =
        K.add (K.add (K.mul K.one z) (K.neg (K.mul K.one w))) (K.neg (K.add K.one (K.neg K.one))); lib_ring
  | k + 1 => by
    obtain ⟨h0, h1⟩ := lu_emb t z w hzw hs k
    have hu : K.mul (K.add z (K.neg w)) (emb K (lu t (k + 1)).1) =
        K.add (Frobenius.pw K z (k + 1)) (K.neg (Frobenius.pw K w (k + 1))) := by
      show K.mul _ (emb K ((lu t k).1 + (lu t k).2)) = _
      rw [emb_add]
      calc _ = K.add (K.mul (K.add z (K.neg w)) (emb K (lu t k).1)) (K.mul (K.add z (K.neg w)) (emb K (lu t k).2)) := by lib_ring
        _ = _ := by rw [h0, h1]; lib_ring
    refine ⟨hu, ?_⟩
    have ht : emb K t = K.add (K.add z w) (K.neg (K.add K.one K.one)) := by
      calc emb K t = K.add (K.add (emb K t) (K.add K.one K.one)) (K.neg (K.add K.one K.one)) := by lib_ring
        _ = _ := by rw [← hs]
    show K.mul _ (emb K (t * (lu t (k + 1)).1 + (lu t k).2)) = _
    rw [emb_add, emb_mul, ht]
    calc _ = K.add (K.mul (K.add (K.add z w) (K.neg (K.add K.one K.one)))
              (K.mul (K.add z (K.neg w)) (emb K (lu t (k + 1)).1)))
            (K.mul (K.add z (K.neg w)) (emb K (lu t k).2)) := by lib_ring
      _ = _ := by
        rw [hu, h1]
        show _ = K.add (K.add (K.mul (K.mul (Frobenius.pw K z k) z) z) (K.neg (K.mul (K.mul (Frobenius.pw K w k) w) w)))
          (K.neg (K.add (K.mul (Frobenius.pw K z k) z) (K.neg (K.mul (Frobenius.pw K w k) w))))
        show K.add (K.mul _ (K.add (K.mul (Frobenius.pw K z k) z) (K.neg (K.mul (Frobenius.pw K w k) w))))
          (K.add (K.add (K.mul (Frobenius.pw K z k) z) (K.neg (K.mul (Frobenius.pw K w k) w)))
            (K.neg (K.add (Frobenius.pw K z k) (K.neg (Frobenius.pw K w k))))) = _
        generalize Frobenius.pw K z k = a
        generalize Frobenius.pw K w k = c
        calc _ = K.add (K.add (K.add (K.mul (K.mul a z) z) (K.neg (K.mul (K.mul c w) w)))
                (K.neg (K.add (K.mul a z) (K.neg (K.mul c w)))))
              (K.mul (K.add a (K.neg c)) (K.add (K.mul z w) (K.neg K.one))) := by lib_ring
          _ = _ := by rw [hzw]; lib_ring

end Cheb

#print axioms Cheb.lu_rec
#print axioms Cheb.lu_emb

lang/Lucas.lean

lean_all/lang/Lucas.lean

/-
  lang/Lucas — T43, cycle 98: Lucas' theorem for two digits, in every commutative ring of prime characteristic p.

  Binomial coefficients as Satake coefficients: rp n = [−1, …, −1] (n entries), cf(rp n) = coefficients of (1 + X)^n.
    B_C          cf(rp n)_k = C(n, k)·1
    sRn, sRn_low, sRn_shift, conv_sRn, conv_delta_right     shifts by p and Cauchy products
    B_p          in characteristic p: (1 + X)^p = 1 + X^p            (p | C(p, k), 0 < k < p: Frobenius.dv_choose)
    B_step       (1 + X)^(n + p) = (1 + X)^n + X^p (1 + X)^n
    lucas        b, d < p:  C(b + a p, c p + d)·1 = C(a, c) C(b, d)·1
  Used for C((p²−1)/2, (p²−1)/4) ≡ C((p−1)/2, (p−1)/4)² (mod p): the Hasse invariant over 𝔽_{p²} is the square of the one over
  𝔽_p (FinHasse), the ordinarity input for a(𝔽_{p²}) = a_p² − 2p at split primes.

  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.SymNLocal
import lib.RingTac
import lib.Semi

namespace Lucas

open FTAChain GLnLocal SymNLocal Frobenius

variable {R : Type} (K : DetChain.CRing R)

def rp : Nat → List R
  | 0 => []
  | n + 1 => K.neg K.one :: rp n

theorem B_C : ∀ n k, cf K (rp K n) k = nsm K (C n k) K.one
  | 0, 0 => (K.zero_add _).symm
  | 0, _ + 1 => rfl
  | n + 1, 0 => by
    show cf K (rp K n) 0 = nsm K (C (n + 1) 0) K.one
    rw [B_C n 0, C_zero_right, C_zero_right]
  | n + 1, k + 1 => by
    show K.add (cf K (rp K n) (k + 1)) (K.neg (K.mul (K.neg K.one) (cf K (rp K n) k))) = nsm K (C n k + C n (k + 1)) K.one
    rw [B_C n (k + 1), B_C n k, nsm_add]
    lib_ring

def sRn : Nat → (Nat → R) → Nat → R
  | 0, f => f
  | n + 1, f => sR K (sRn n f)

theorem sRn_low (f : Nat → R) : ∀ n k, k < n → sRn K n f k = K.zero
  | 0, k, h => absurd h (Nat.not_lt_zero k)
  | _ + 1, 0, _ => rfl
  | n + 1, k + 1, h => sRn_low f n k (Nat.lt_of_succ_lt_succ h)

theorem sRn_shift (f : Nat → R) : ∀ n k, sRn K n f (k + n) = f k
  | 0, _ => rfl
  | n + 1, k => sRn_shift f n k

theorem sRn_ext (f g : Nat → R) (hfg : ∀ i, f i = g i) : ∀ n k, sRn K n f k = sRn K n g k
  | 0, k => hfg k
  | n + 1, k => sR_ext K _ _ (fun i => sRn_ext f g hfg n i) k

theorem conv_sRn (c h : Nat → R) : ∀ n k, conv K c (sRn K n h) k = sRn K n (conv K c h) k
  | 0, _ => rfl
  | n + 1, k => by
    show conv K c (sR K (sRn K n h)) k = sR K (sRn K n (conv K c h)) k
    rw [conv_sR]
    exact sR_ext K _ _ (fun i => conv_sRn c h n i) k

theorem conv_delta_right (c : Nat → R) : ∀ k, conv K c (delta K) k = c k := by
  intro k
  induction k generalizing c with
  | zero => show K.mul (c 0) K.one = c 0; rw [K.mul_comm, K.one_mul]
  | succ k ih =>
    show K.add (K.mul (c 0) K.zero) (conv K (fun i => c (i + 1)) (delta K) k) = c (k + 1)
    rw [DetChain.mul_zero, K.zero_add, ih]

theorem rp_add : ∀ m n, rp K (m + n) = rp K m ++ rp K n
  | 0, n => by rw [Nat.zero_add]; rfl
  | m + 1, n => by
    rw [Nat.succ_add]
    show K.neg K.one :: rp K (m + n) = K.neg K.one :: (rp K m ++ rp K n)
    rw [rp_add m n]


/-- **(1 + X)^p = 1 + X^p in characteristic p.** -/
theorem B_p (j : Nat) (hpr : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1) (hp : nsm K (j + 1 + 1) K.one = K.zero) (k : Nat) : cf K (rp K (j + 1 + 1)) k = K.add (delta K k) (sRn K (j + 1 + 1) (delta K) k) := by
  rw [B_C]
  cases Nat.lt_or_ge k (j + 1 + 1) with
  | inl hk =>
    rw [sRn_low K _ _ k hk, DetChain.add_zero']
    cases k with
    | zero => rw [C_zero_right]; exact K.zero_add _
    | succ k' =>
      match dv_choose j k' hpr (Nat.le.dest hk) with
      | ⟨q, hq⟩ => rw [hq, nsm_char K _ q hp]; rfl
  | inr hk =>
    match Nat.le.dest hk with
    | ⟨m, hm⟩ =>
      rw [← hm, Nat.add_comm (j + 1 + 1) m, sRn_shift]
      cases m with
      | zero =>
        rw [Nat.zero_add, C_self]
        show K.add K.zero K.one = K.add K.zero K.one
        rfl
      | succ m' =>
        rw [show m' + 1 + (j + 1 + 1) = m' + (j + 1 + 1) + 1 by lib_nat, C_above]
        show K.zero = K.add K.zero K.zero
        exact (K.zero_add _).symm

/-- **(1 + X)^(n + p) = (1 + X)^n + X^p (1 + X)^n.** -/
theorem B_step (j : Nat) (hpr : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1) (hp : nsm K (j + 1 + 1) K.one = K.zero) (n k : Nat) :
    cf K (rp K (n + (j + 1 + 1))) k = K.add (cf K (rp K n) k) (sRn K (j + 1 + 1) (cf K (rp K n)) k) := by
  rw [rp_add, cf_append, conv_ext K _ _ _ (B_p K j hpr hp) k, conv_add, conv_delta_right, conv_sRn]
  rw [sRn_ext K _ _ (conv_delta_right K _) (j + 1 + 1) k]

/-- **Lucas' theorem for two digits**: C(b + a p, c p + d) ≡ C(a, c) C(b, d) (mod p) for b, d < p. -/
theorem lucas (j : Nat) (hpr : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1) (hp : nsm K (j + 1 + 1) K.one = K.zero) (b d : Nat) (hb : b < j + 1 + 1) (hd : d < j + 1 + 1) : ∀ a c,
    nsm K (C (b + a * (j + 1 + 1)) (c * (j + 1 + 1) + d)) K.one = nsm K (C a c * C b d) K.one
  | 0, c => by
    rw [Nat.zero_mul, Nat.add_zero]
    cases c with
    | zero => rw [Nat.zero_mul, Nat.zero_add]; show _ = nsm K (1 * C b d) K.one; rw [Nat.one_mul]
    | succ c' =>
      show nsm K (C b ((c' + 1) * (j + 1 + 1) + d)) K.one = nsm K (0 * C b d) K.one
      have hle : b + 1 ≤ (c' + 1) * (j + 1 + 1) + d :=
        Nat.le_trans hb (Nat.le_trans (Nat.le_add_left _ (c' * (j + 1 + 1))) (by rw [Nat.succ_mul]; exact Nat.le_add_right _ _))
      match Nat.le.dest hle with
      | ⟨m, hm⟩ =>
        rw [← hm, show b + 1 + m = m + b + 1 by lib_nat, C_above, Nat.zero_mul]
  | a + 1, c => by
    have e1 : b + (a + 1) * (j + 1 + 1) = (b + a * (j + 1 + 1)) + (j + 1 + 1) := by lib_nat
    rw [← B_C, e1, B_step K j hpr hp]
    cases c with
    | zero =>
      rw [sRn_low K _ _ _ (by rw [Nat.zero_mul, Nat.zero_add]; exact hd), DetChain.add_zero', B_C,
        lucas j hpr hp b d hb hd a 0, C_zero_right, C_zero_right]
    | succ c' =>
      rw [show (c' + 1) * (j + 1 + 1) + d = (c' * (j + 1 + 1) + d) + (j + 1 + 1) by lib_nat, sRn_shift,
        show c' * (j + 1 + 1) + d + (j + 1 + 1) = (c' + 1) * (j + 1 + 1) + d by lib_nat, B_C, B_C,
        lucas j hpr hp b d hb hd a (c' + 1), lucas j hpr hp b d hb hd a c', ← nsm_add]
      show _ = nsm K ((C a c' + C a (c' + 1)) * C b d) K.one
      rw [show (C a c' + C a (c' + 1)) * C b d = C a (c' + 1) * C b d + C a c' * C b d by lib_nat]

end Lucas

#print axioms Lucas.B_C
#print axioms Lucas.sRn_low
#print axioms Lucas.sRn_shift
#print axioms Lucas.sRn_ext
#print axioms Lucas.conv_sRn
#print axioms Lucas.conv_delta_right
#print axioms Lucas.rp_add
#print axioms Lucas.B_p
#print axioms Lucas.B_step
#print axioms Lucas.lucas

lang/LucasIter.lean

lean_all/lang/LucasIter.lean

/-
  lang/LucasIter — T43, cycle 170: Lucas' theorem for N equal digits.

  rep a N = a + a p + … + a p^(N−1) (rep a 0 = 0, rep a (N+1) = a + rep a N · p). For b, d < p in characteristic p:
    lucas_rep     C(rep b N, rep d N)·1 = (C(b, d)·1)^N
  So the Hasse invariant over 𝔽_{p^N} (h = rep ((p−1)/2) N) is the N-th power of the one over 𝔽_p, for every N — the
  ordinarity input at every level, not only N = 2, 3.

  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.Lucas
import lang.CubeHasse

namespace LucasIter

open FTAChain Frobenius

variable {R : Type} (K : DetChain.CRing R)

def rep (P a : Nat) : Nat → Nat
  | 0 => 0
  | N + 1 => a + rep P a N * P

theorem lucas_rep (j : Nat) (hpr : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1) (hp : nsm K (j + 1 + 1) K.one = K.zero)
    (b d : Nat) (hb : b < j + 1 + 1) (hd : d < j + 1 + 1) : ∀ N,
    nsm K (C (rep (j + 1 + 1) b N) (rep (j + 1 + 1) d N)) K.one = pw K (nsm K (C b d) K.one) N
  | 0 => by show nsm K 1 K.one = K.one; show K.add K.zero K.one = K.one; exact K.zero_add _
  | N + 1 => by
    show nsm K (C (b + rep (j + 1 + 1) b N * (j + 1 + 1)) (d + rep (j + 1 + 1) d N * (j + 1 + 1))) K.one = _
    rw [Nat.add_comm d, Lucas.lucas K j hpr hp b d hb hd, CubeHasse.nsm_prod, lucas_rep j hpr hp b d hb hd N]
    rfl

end LucasIter

#print axioms LucasIter.rep
#print axioms LucasIter.lucas_rep

lang/MapSplit.lean

lean_all/lang/MapSplit.lean

/-
  lang/MapSplit — T21: **row-chunked checking of list equalities L.map f = C.map g** (the shape of the Hecke-preservation
  certificates `pres` of the Bianchi K files), so one large `rfl` can be split into modules by rows:
    map_append   (l₁ ++ l₂).map f = l₁.map f ++ l₂.map f
    map_cat      chunk of k rows after s, plus the rows after s + k, gives the rows after s
  (list slicing facts from lang/RepSplit).
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.RepSplit

namespace MapSplit

theorem map_append {α β : Type} (f : α → β) : ∀ l₁ l₂ : List α, (l₁ ++ l₂).map f = l₁.map f ++ l₂.map f
  | [], _ => rfl
  | a :: l, l₂ => congrArg (List.cons (f a)) (map_append f l l₂)

/-- **gluing a chunk of rows**. -/
theorem map_cat {α γ β : Type} (f : α → β) (g : γ → β) (L : List α) (C : List γ) (s k : Nat)
    (h₁ : ((L.drop s).take k).map f = ((C.drop s).take k).map g)
    (h₂ : (L.drop (k + s)).map f = (C.drop (k + s)).map g) :
    (L.drop s).map f = (C.drop s).map g := by
  rw [← RepSplit.take_append_drop k (L.drop s), ← RepSplit.take_append_drop k (C.drop s), map_append, map_append, h₁,
    RepSplit.drop_drop s k L, RepSplit.drop_drop s k C, h₂]

end MapSplit

#print axioms MapSplit.map_append
#print axioms MapSplit.map_cat

lang/MinusOnePP.lean

lean_all/lang/MinusOnePP.lean

/-
  lang/MinusOnePP — T43, cycle 416: **for every prime power ℓ^(e+1) there are infinitely many primes p ≡ −1 (mod ℓ^(e+1)).**

  ℓ odd: ChebPowAll.inf_powall. ℓ = 2: e + 1 ≤ 2 from InfInert.inf_inert (p ≡ 3 mod 4), e + 1 ≥ 3 from Two2Inf.inf_2pow.
  One statement for all prime powers (the Euclid-type class −1; with InfCycloPow the class +1 as well).
  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.ChebPowAll
import lang.Two2Inf
import lang.InfInert
import lib.Semi

namespace MinusOnePP

open FTAChain

theorem inf_m1_pp (l e : Nat) (h2 : 2 ≤ l) (hpr : ∀ k, Dv k l → k = 1 ∨ k = l) (B : Nat) :
    ∃ p, (∀ k, Dv k p → k = 1 ∨ k = p) ∧ B < p ∧ Dv (l ^ (e + 1)) (p + 1) := by
  rcases EveryPrime.nat_even_odd l with ⟨i, hi⟩ | ⟨i, hi⟩
  · -- l = 2
    have hl : l = 2 := by
      cases hpr 2 ⟨i, by rw [hi]; lib_nat⟩ with
      | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
      | inr e => exact e.symm
    subst hl
    match e with
    | 0 =>
      obtain ⟨k, hk, hB⟩ := InfInert.inf_inert B
      exact ⟨_, hk, hB, ⟨2 * k + 2, by show 2 * (2 * k) + 1 + 1 + 1 + 1 = (2 * k + 2) * (1 * 2); lib_nat⟩⟩
    | 1 =>
      obtain ⟨k, hk, hB⟩ := InfInert.inf_inert B
      exact ⟨_, hk, hB, ⟨k + 1, by show 2 * (2 * k) + 1 + 1 + 1 + 1 = (k + 1) * (1 * 2 * 2); lib_nat⟩⟩
    | j + 2 => exact Two2.inf_2pow j B
  · match i, hi with
    | 0, hi => rw [hi] at h2; exact absurd h2 (Nat.not_succ_le_self 1)
    | m + 1, hi =>
      have hl : l = 2 * m + 1 + 1 + 1 := by rw [hi]; lib_nat
      subst hl
      exact Cheb.inf_powall m hpr e B

end MinusOnePP

#print axioms MinusOnePP.inf_m1_pp

lang/MixedCard.lean

lean_all/lang/MixedCard.lean

/-
  lang/MixedCard — T49 (residue fields of degree f > 1): **exact orders of the mixed tower**. The quadratic tower
  TowerPow.tower2 has order q^(2^L), and every cubic step of lang/MixedTower cubes the order, so the mixed tower
  carries a finite field of order exactly q^(2^(L+1)·3^b) (q = 2m + 3 prime, q not 0 mod 3).
    tower2_card      2h_L + 1 = q^(2^L)
    step3c           a cubic step keeps Good3' and gives 2h' + 1 = (2h + 1)^3
    pow_cube         q^(AB)·(q^(AB)·q^(AB)) = q^(A(3B))
    mixed_card       ∃ G on C m L b, Good3' q G and 2h + 1 = q^(2^(L+1)·3^b)
  Lean 4 core only, proofs by hand (lib_nat, lib_int), no axioms.
-/
import lang.MixedTower
import lang.TowerCard

namespace MixedCard

open FTAChain ZMod YMChain FiniteRecChain Frobenius GaussInt GaussFactor FieldTower TowerPow3 MixedTower
open LibRing (castZ castN)

/-- **|tower2_L| = q^(2^L)**. -/
theorem tower2_card (m : Nat) (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) :
    ∀ L, 2 * (TowerPow.tower2 m hpr L).F.h + 1 = (2 * m + 1 + 1 + 1) ^ (2 ^ L)
  | 0 => by
    rw [(TowerPow.tower2 m hpr 0).hj]
    show 2 * (m + 1) + 1 = 1 * (2 * m + 1 + 1 + 1)
    lib_nat
  | L + 1 => by
    rw [(TowerPow.tower2 m hpr (L + 1)).hj]
    show 2 * ((2 * (TowerPow.tower2 m hpr L).j * (TowerPow.tower2 m hpr L).j + 6 * (TowerPow.tower2 m hpr L).j + 3) + 1) + 1
      = (2 * m + 1 + 1 + 1) ^ (2 ^ (L + 1))
    rw [order_sq, ← (TowerPow.tower2 m hpr L).hj, tower2_card m hpr L, TowerCard.pow_sq]
    show (2 * m + 1 + 1 + 1) ^ (2 * 2 ^ L) = (2 * m + 1 + 1 + 1) ^ (2 ^ L * 2)
    rw [show 2 * 2 ^ L = 2 ^ L * 2 by lib_nat]

/-- A cubic step with its order. -/
theorem step3c {R : Type} [DecidableEq R] (j : Nat) (hpr : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1)
    (F : FinField.FF R) (hG : Good3' (j + 1 + 1) F) :
    ∃ G : FinField.FF (R × R × R), Good3' (j + 1 + 1) G ∧
      2 * G.h + 1 = (2 * F.h + 1) * ((2 * F.h + 1) * (2 * F.h + 1)) := by
  match hG with
  | ⟨⟨hc, hz, ⟨N, hN⟩⟩, ⟨m, hm⟩⟩ =>
    have hq : FrobIter.npow (j + 1 + 1) (N + 1) = 2 * F.h + 1 :=
      Int.ofNat.inj ((FrobIter.npow_int _ _).trans hN.symm)
    match GenCubicExt.cubic_ext F j hpr hc (N + 1) hq m hm with
    | ⟨G, hs, hgc, hgz⟩ =>
      refine ⟨G, ⟨⟨hgc, fun z h => hz z (hgz z h), ⟨N + 1 + (N + 1) + N, ?_⟩⟩, ⟨9 * m * m * m + 9 * m * m + 3 * m, ?_⟩⟩, hs⟩
      · rw [show N + 1 + (N + 1) + N + 1 = (N + 1) * 3 by lib_nat, CubicTower.ipow_mul, ← hN, hs]
        show Int.ofNat (2 * F.h + 1) * (Int.ofNat (2 * F.h + 1) * Int.ofNat (2 * F.h + 1)) = 1 * Int.ofNat (2 * F.h + 1) * Int.ofNat (2 * F.h + 1) * Int.ofNat (2 * F.h + 1)
        lib_int
      · have e : 2 * G.h + 1 = (3 * m + 1) * ((3 * m + 1) * (3 * m + 1)) := by rw [hs, hm]
        apply FermatUnique.add_cancel 1
        rw [Nat.add_comm 1 (2 * G.h), e]; lib_nat

theorem pow_cube (q A B : Nat) : q ^ (A * B) * (q ^ (A * B) * q ^ (A * B)) = q ^ (A * (B * 3)) := by
  rw [show A * (B * 3) = A * B + (A * B + A * B) by lib_nat, TowerCard.pow_add', TowerCard.pow_add']

/-- **The mixed tower has order q^(2^(L+1)·3^b)**. -/
theorem mixed_card (m : Nat) (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = (2 * m + 1 + 1 + 1))
    (h3 : (∃ t, m = 3 * t + 1) ∨ (∃ t, m = 3 * t + 2)) (L : Nat) :
    ∀ b, ∃ G : @FinField.FF (C m L b) (instDecC m L b), @Good3' _ (instDecC m L b) (2 * m + 1 + 1 + 1) G ∧
      2 * G.h + 1 = (2 * m + 1 + 1 + 1) ^ (2 ^ (L + 1) * 3 ^ b)
  | 0 => by
    refine ⟨(TowerPow.tower2 m hpr (L + 1)).F, ⟨good3_tower m hpr (L + 1), level_mod3 m hpr h3 L⟩, ?_⟩
    rw [tower2_card m hpr (L + 1)]
    show (2 * m + 1 + 1 + 1) ^ (2 ^ (L + 1)) = (2 * m + 1 + 1 + 1) ^ (2 ^ (L + 1) * 1)
    rw [show 2 ^ (L + 1) * 1 = 2 ^ (L + 1) by lib_nat]
  | b + 1 => by
    match mixed_card m hpr h3 L b with
    | ⟨G, hG, hc⟩ =>
      match @step3c _ (instDecC m L b) (2 * m + 1) hpr G hG with
      | ⟨G', hG', hc'⟩ =>
        refine ⟨G', hG', ?_⟩
        rw [hc', hc]
        exact pow_cube (2 * m + 1 + 1 + 1) (2 ^ (L + 1)) (3 ^ b)

end MixedCard

#print axioms MixedCard.tower2_card
#print axioms MixedCard.step3c
#print axioms MixedCard.pow_cube
#print axioms MixedCard.mixed_card

lang/MixedTower.lean

lean_all/lang/MixedTower.lean

/-
  lang/MixedTower — T43, cycle 194: fields of order p^(3^b · 2^(L+1)) for every p > 3, L and b, all satisfying Good3.

    C m L b          the carrier: level L + 1 of the tower, then b cubic steps
    good3_mod3       Good3 with 3 | 2h passes through a cubic step, and 3 | 2h' again (q³ ≡ 1 mod 3)
    mixed            ∃ G : FF (C m L b), Good3 P G
  So ZetaAll3 (y² = x³ + 1) holds over fields of every order p^(3^b·2^(L+1)).

  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.CubicTower

namespace MixedTower

open FTAChain ZMod YMChain FiniteRecChain Frobenius GaussInt GaussFactor FieldTower TowerPow3
open LibRing (castZ castN)

def C (m L : Nat) : Nat → Type
  | 0 => T m (L + 1)
  | b + 1 => C m L b × C m L b × C m L b

instance instDecC (m L : Nat) : ∀ b, DecidableEq (C m L b)
  | 0 => instDec m (L + 1)
  | b + 1 => @instDecidableEqProd _ _ (instDecC m L b) (@instDecidableEqProd _ _ (instDecC m L b) (instDecC m L b))

/-- Good3 together with 3 | 2h. -/
def Good3' {R : Type} [DecidableEq R] (P : Nat) (F : FinField.FF R) : Prop := Good3 P F ∧ ∃ m, 2 * F.h = 3 * m

theorem step3 {R : Type} [DecidableEq R] (j : Nat) (hpr : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1) (F : FinField.FF R)
    (hG : Good3' (j + 1 + 1) F) : ∃ G : FinField.FF (R × R × R), Good3' (j + 1 + 1) G := by
  match hG with
  | ⟨⟨hc, hz, ⟨N, hN⟩⟩, ⟨m, hm⟩⟩ =>
    have hq : FrobIter.npow (j + 1 + 1) (N + 1) = 2 * F.h + 1 :=
      Int.ofNat.inj ((FrobIter.npow_int _ _).trans hN.symm)
    match GenCubicExt.cubic_ext F j hpr hc (N + 1) hq m hm with
    | ⟨G, hs, hgc, hgz⟩ =>
      refine ⟨G, ⟨hgc, fun z h => hz z (hgz z h), ⟨N + 1 + (N + 1) + N, ?_⟩⟩, ⟨9 * m * m * m + 9 * m * m + 3 * m, ?_⟩⟩
      · rw [show N + 1 + (N + 1) + N + 1 = (N + 1) * 3 by lib_nat, CubicTower.ipow_mul, ← hN, hs]
        show Int.ofNat (2 * F.h + 1) * (Int.ofNat (2 * F.h + 1) * Int.ofNat (2 * F.h + 1)) = 1 * Int.ofNat (2 * F.h + 1) * Int.ofNat (2 * F.h + 1) * Int.ofNat (2 * F.h + 1)
        lib_int
      · have e : 2 * G.h + 1 = (3 * m + 1) * ((3 * m + 1) * (3 * m + 1)) := by rw [hs, hm]
        apply FermatUnique.add_cancel 1
        rw [Nat.add_comm 1 (2 * G.h), e]; lib_nat

theorem level_mod3 (m : Nat) (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = (2 * m + 1 + 1 + 1)) (h3 : (∃ t, m = 3 * t + 1) ∨ (∃ t, m = 3 * t + 2)) (L : Nat) :
    ∃ q, 2 * (TowerPow.tower2 m hpr (L + 1)).F.h = 3 * q := by
  match good3_tower m hpr L, CubicTower.P2_mod3 m h3 with
  | ⟨_, _, ⟨N, hN⟩⟩, ⟨e, he⟩ =>
    have hq : Int.ofNat (2 * (TowerPow.tower2 m hpr (L + 1)).F.h + 1) = ipow (Int.ofNat (2 * m + 1 + 1 + 1)) (2 * (N + 1)) := by
      rw [show 2 * (N + 1) = (N + 1) + (N + 1) by lib_nat]
      exact @FinPow2.quad_size _ (instDec m L) (TowerPow.tower2 m hpr L).F (@TowerPow.pickNR _ (instDec m L) (TowerPow.tower2 m hpr L).F).1 (TowerPow.tower2 m hpr L).j (TowerPow.tower2 m hpr L).hj
        (@TowerPow.pickNR _ (instDec m L) (TowerPow.tower2 m hpr L).F).2.1 (@TowerPow.pickNR _ (instDec m L) (TowerPow.tower2 m hpr L).F).2.2 _ (N + 1) hN
    match InertPow3.one_mod3_pow _ e he (N + 1) with
    | ⟨f, hf⟩ =>
      rw [← InertPow3.ipow_two, ← hq] at hf
      have e2 : Int.ofNat (2 * (TowerPow.tower2 m hpr (L + 1)).F.h) = 3 * f := by
        have h1 : Int.ofNat (2 * (TowerPow.tower2 m hpr (L + 1)).F.h) + 1 = 3 * f + 1 := hf
        rw [show Int.ofNat (2 * (TowerPow.tower2 m hpr (L + 1)).F.h) = (Int.ofNat (2 * (TowerPow.tower2 m hpr (L + 1)).F.h) + 1) + -1 by lib_int, h1]; lib_int
      match EisenUnique.nat_of_three _ f e2 with
      | ⟨q, hq'⟩ => exact ⟨q, by rw [hq']; lib_nat⟩

theorem mixed (m : Nat) (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = (2 * m + 1 + 1 + 1)) (h3 : (∃ t, m = 3 * t + 1) ∨ (∃ t, m = 3 * t + 2)) (L : Nat) :
    ∀ b, ∃ G : @FinField.FF (C m L b) (instDecC m L b), @Good3' _ (instDecC m L b) (2 * m + 1 + 1 + 1) G
  | 0 => by
    exact ⟨(TowerPow.tower2 m hpr (L + 1)).F, good3_tower m hpr (L + 1), level_mod3 m hpr h3 L⟩
  | b + 1 => by
    match mixed m hpr h3 L b with
    | ⟨G, hG⟩ => exact @step3 _ (instDecC m L b) (2 * m + 1) hpr G hG

end MixedTower

#print axioms MixedTower.C
#print axioms MixedTower.Good3'
#print axioms MixedTower.step3
#print axioms MixedTower.level_mod3
#print axioms MixedTower.mixed
#print axioms MixedTower.instDecC

lang/MixedZeta.lean

lean_all/lang/MixedZeta.lean

/-
  lang/MixedZeta — T43, cycle 195: both CM zeta functions over the fields of order p^(3^b·2^(L+1)) (p > 3).

    MC, keyC, keyC_lt, keyC_inj    an injective bounded key on C m L b (base-M digits, as TowerTrace.keyT)
    mixed_zeta     y² = x³ − x:  a(G) = s_N(a_p, p)
    mixed_zeta3    y² = x³ + 1:  −ψ_G(1) = s_N(a_p, p)

  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.MixedTower

namespace MixedZeta

open FTAChain ZMod YMChain FiniteRecChain Frobenius GaussInt GaussFactor FieldTower TowerPow3 MixedTower ZetaAll

def MC (m L : Nat) : Nat → Nat
  | 0 => TowerTrace.M m (L + 1)
  | b + 1 => MC m L b * (MC m L b * MC m L b)

def keyC (m L : Nat) : (b : Nat) → C m L b → Nat
  | 0, x => TowerTrace.keyT m (L + 1) x
  | b + 1, x => keyC m L b x.1 * (MC m L b * MC m L b) + (keyC m L b x.2.1 * MC m L b + keyC m L b x.2.2)

theorem lt2 (M a b : Nat) (ha : a < M) (hb : b < M) : a * M + b < M * M :=
  calc a * M + b < a * M + M := Nat.add_lt_add_left hb _
    _ = (a + 1) * M := (Nat.succ_mul a M).symm
    _ ≤ M * M := Nat.mul_le_mul_right _ ha

theorem lt2g (M1 M2 a b : Nat) (ha : a < M1) (hb : b < M2) : a * M2 + b < M1 * M2 :=
  calc a * M2 + b < a * M2 + M2 := Nat.add_lt_add_left hb _
    _ = (a + 1) * M2 := (Nat.succ_mul a M2).symm
    _ ≤ M1 * M2 := Nat.mul_le_mul_right _ ha

theorem keyC_lt (m L : Nat) : ∀ b (x : C m L b), keyC m L b x < MC m L b
  | 0, x => TowerTrace.keyT_lt m (L + 1) x
  | b + 1, x => by
    show keyC m L b x.1 * (MC m L b * MC m L b) + (keyC m L b x.2.1 * MC m L b + keyC m L b x.2.2) < MC m L b * (MC m L b * MC m L b)
    exact lt2g _ _ _ _ (keyC_lt m L b x.1) (lt2 _ _ _ (keyC_lt m L b x.2.1) (keyC_lt m L b x.2.2))

theorem keyC_inj (m L : Nat) : ∀ b (x y : C m L b), keyC m L b x = keyC m L b y → x = y
  | 0, x, y, h => TowerTrace.keyT_inj m (L + 1) x y h
  | b + 1, x, y, h =>
    match Fp2Count.divmod_unique _ _ _ _ _ (lt2 _ _ _ (keyC_lt m L b x.2.1) (keyC_lt m L b x.2.2))
      (lt2 _ _ _ (keyC_lt m L b y.2.1) (keyC_lt m L b y.2.2)) h with
    | ⟨h1, h2⟩ =>
      match Fp2Count.divmod_unique _ _ _ _ _ (keyC_lt m L b x.2.2) (keyC_lt m L b y.2.2) h2 with
      | ⟨h3, h4⟩ => Prod.ext (keyC_inj m L b _ _ h1) (Prod.ext (keyC_inj m L b _ _ h3) (keyC_inj m L b _ _ h4))

/-- **y² = x³ − x over fields of order p^(3^b·2^(L+1)).** -/
theorem mixed_zeta (m : Nat) (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = (2 * m + 1 + 1 + 1)) (h3 : (∃ t, m = 3 * t + 1) ∨ (∃ t, m = 3 * t + 2)) (L b : Nat) :
    ∃ G : @FinField.FF (C m L b) (instDecC m L b), ∃ N : Nat, Int.ofNat (2 * G.h + 1) = ipow (Int.ofNat (2 * m + 1 + 1 + 1)) (N + 1) ∧
      -(sumK LibInt.intR (fun x => @FinField.chi _ (instDecC m L b) G (@FinSign.f _ (instDecC m L b) G x)) G.els)
        = s (-(sumK LibInt.intR (fun x => QChar.chi m (CMInert.cub (ring (2 * m + 1 + 1)) x)) (ConicSum.L m))) (Int.ofNat (2 * m + 1 + 1 + 1)) (N + 1) := by
  match mixed m hpr h3 L b with
  | ⟨G, ⟨hc, hz, ⟨N, hN⟩⟩, _⟩ =>
    exact ⟨G, N, hN, @zeta_all _ (instDecC m L b) m hpr G hc hz N hN (keyC m L b) (keyC_inj m L b)⟩

/-- **y² = x³ + 1 over fields of order p^(3^b·2^(L+1)).** -/
theorem mixed_zeta3 (m : Nat) (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = (2 * m + 1 + 1 + 1)) (h3 : (∃ t, m = 3 * t + 1) ∨ (∃ t, m = 3 * t + 2)) (L b : Nat) :
    ∃ G : @FinField.FF (C m L b) (instDecC m L b), ∃ N : Nat, Int.ofNat (2 * G.h + 1) = ipow (Int.ofNat (2 * m + 1 + 1 + 1)) (N + 1) ∧
      -(@FinCubic.psi _ (instDecC m L b) G G.K.one)
        = s (-(FinCubic.psi (FinField.ffp m hpr) (FinField.ffp m hpr).K.one)) (Int.ofNat (2 * m + 1 + 1 + 1)) (N + 1) := by
  have hp3 : (2 * m + 1 + 1 + 1) ≠ 3 := fun e => by
    cases h3 with
    | inl h => match h with | ⟨t, ht⟩ => subst ht; exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj ((show 2 * (3 * t + 1) + 1 + 1 + 1 = (6 * t + 1) + 1 + 1 + 1 + 1 by lib_nat).symm.trans e))))
    | inr h => match h with | ⟨t, ht⟩ => subst ht; exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj ((show 2 * (3 * t + 2) + 1 + 1 + 1 = (6 * t + 3) + 1 + 1 + 1 + 1 by lib_nat).symm.trans e))))
  match mixed m hpr h3 L b with
  | ⟨G, ⟨hc, hz, ⟨N, hN⟩⟩, _⟩ =>
    exact ⟨G, N, hN, @ZetaAll3.zeta_all3 _ (instDecC m L b) m hpr hp3 G hc hz N hN⟩

end MixedZeta

#print axioms MixedZeta.MC
#print axioms MixedZeta.keyC
#print axioms MixedZeta.lt2
#print axioms MixedZeta.lt2g
#print axioms MixedZeta.keyC_lt
#print axioms MixedZeta.keyC_inj
#print axioms MixedZeta.mixed_zeta
#print axioms MixedZeta.mixed_zeta3

lang/MkFF.lean

lean_all/lang/MkFF.lean

/-
  lang/MkFF — T43, cycle 138: a finite field of the interface from a ring, its element list, Fermat and −1 ≠ 1.

  Given K without zero divisors, its q = 2(k + 1) + 1 elements listed once each, −1 ≠ 1 and x^(q−1) = 1 for x ≠ 0:
    mkFF     the FF structure: inverses x^(q−2), Euler's criterion ±1 (sq_one), squares, the hard half (GenericEuler),
             a non-residue (Fq2Inst.nonroot_gen), and the multiplicative inverse map
  Every further field (quadratic towers, cubic extensions) only has to supply its ring, its list and Fermat.

  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.GenericEuler
import lang.Fq2Inst
import lang.Euler

namespace MkFF

open FTAChain YMChain FiniteRecChain

def mkFF {R : Type} [DecidableEq R] (K : DetChain.CRing R) (hK : DetChain.NoZD K) (els : List R)
    (comp : Complete LibInt.intR els) (k : Nat)
    (card : sumK LibInt.intR (fun _ => (1 : Int)) els = Int.ofNat (2 * (k + 1) + 1))
    (hne1 : K.neg K.one ≠ K.one) (hferm : ∀ x, x ≠ K.zero → Frobenius.pw K x (2 * (k + 1)) = K.one) :
    FinField.FF R where
  K := K
  els := els
  comp := comp
  h := k + 1
  card := card
  nozd := hK
  inv := fun x hx => ⟨Frobenius.pw K x (2 * k + 1), by
    rw [K.mul_comm]; show K.mul (Frobenius.pw K x (2 * k + 1)) x = K.one
    rw [show K.mul (Frobenius.pw K x (2 * k + 1)) x = Frobenius.pw K x (2 * (k + 1)) from rfl]
    exact hferm x hx⟩
  euler_pm := fun x hx => Euler.sq_one K hK _ (by
    rw [← HasseCM.pw_add', show k + 1 + (k + 1) = 2 * (k + 1) by lib_nat]; exact hferm x hx)
  euler_sq := fun y hy => by
    rw [Frobenius.pw_mul, ← HasseCM.pw_add', show k + 1 + (k + 1) = 2 * (k + 1) by lib_nat]; exact hferm y hy
  euler_hard := fun x hx => GenericEuler.generic_euler_hard K hK els comp (k + 1) (Nat.le_add_left 1 k) card hferm hne1 x hx
  ne_pm := hne1
  nonres := Fq2Inst.nonroot_gen K hK els comp (k + 1) card k (Nat.le.intro (k := k) (by lib_nat))
  iv := fun x => Frobenius.pw K x (2 * k + 1)
  iv_mul := fun x hx => hferm x hx
  iv_zero := HasseCM.pw_zero_succ K (2 * k)
  iv_hom := fun x y => Frobenius.pw_mul K x y (2 * k + 1)

end MkFF

#print axioms MkFF.mkFF

lang/ModDiv.lean

lean_all/lang/ModDiv.lean

/-
  lang/ModDiv — T49 (O3') (iii): **dividing a congruence mod N by a nonzero integer**, coefficient form.
  X(b) = c·Y(b) + s₁ and X(b) = c·D(b) + s₂ for every b, c ≠ 0 ⇒ Y(b) = D(b) + t for every b (t = Y(b₀) − D(b₀)).
  Used with c = p^(2(ℓ−2)), X = W·W̄ (W = A B̄ = p^(ℓ−2)u + sN), Y = u ū, D = δ₀: u ū ≡ 1 (mod N).
  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.IntCancel

namespace ModDiv

theorem mod_div {A : Type} (X Y D : A → Int) (c s₁ s₂ : Int) (hc : c ≠ 0)
    (h1 : ∀ b, X b = c * Y b + s₁) (h2 : ∀ b, X b = c * D b + s₂) (b₀ : A) :
    ∀ b, Y b = D b + (Y b₀ - D b₀) := by
  have hw : ∀ b, c * (Y b - D b) = s₂ - s₁ := fun b => by
    calc c * (Y b - D b) = (c * Y b + s₁) - (c * D b + s₂) + (s₂ - s₁) := by lib_int
      _ = X b - X b + (s₂ - s₁) := by rw [← h1 b, ← h2 b]
      _ = s₂ - s₁ := by lib_int
  intro b
  have := IntCancel.const_div (fun b => Y b - D b) c (s₂ - s₁) hc hw b b₀
  calc Y b = D b + (Y b - D b) := by lib_int
    _ = D b + (Y b₀ - D b₀) := by rw [show Y b - D b = Y b₀ - D b₀ from this]

end ModDiv

#print axioms ModDiv.mod_div

lang/ModN.lean

lean_all/lang/ModN.lean

/-
  lang/ModN — T49 (O3') (iii): **congruence modulo N = Σ σ^i in ℤ[C_ℓ]** in coefficient form:
  u ≡ v (mod N) :⟺ ∃ t, u(b) = v(b) + t for every b.
    modn_refl, modn_symm, modn_trans
    modn_mul    u ≡ u' ⇒ u·v ≡ u'·v                 (the shift Σ_a v(b − a) = Σ v is independent of b)
  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.GroupRing
import lib.IntRing
import lib.RingTac

namespace ModN

open FTAChain ZMod YMChain FiniteRecChain DetChain GroupRing

variable (m : Nat)

def ModN (u v : V Int (m + 1)) : Prop := ∃ t : Int, ∀ b, gt LibInt.intR m u b = gt LibInt.intR m v b + t

theorem modn_refl (u : V Int (m + 1)) : ModN m u u := ⟨0, fun b => by lib_int⟩

theorem modn_symm (u v : V Int (m + 1)) (h : ModN m u v) : ModN m v u := by
  obtain ⟨t, ht⟩ := h
  exact ⟨-t, fun b => by rw [ht b]; lib_int⟩

theorem modn_trans (u v w : V Int (m + 1)) (h1 : ModN m u v) (h2 : ModN m v w) : ModN m u w := by
  obtain ⟨t, ht⟩ := h1
  obtain ⟨s, hs⟩ := h2
  exact ⟨s + t, fun b => by rw [ht b, hs b]; lib_int⟩

theorem sum_split (f g : Zp m → Int) (t : Int) : ∀ l : List (Zp m),
    sumK LibInt.intR (fun a => (f a + t) * g a) l
      = sumK LibInt.intR (fun a => f a * g a) l + t * sumK LibInt.intR g l
  | [] => by show (0 : Int) = 0 + t * 0; lib_int
  | a :: l => by
    show (f a + t) * g a + sumK LibInt.intR (fun a => (f a + t) * g a) l
      = (f a * g a + sumK LibInt.intR (fun a => f a * g a) l) + t * (g a + sumK LibInt.intR g l)
    rw [sum_split f g t l]
    generalize sumK LibInt.intR (fun a => f a * g a) l = X
    generalize sumK LibInt.intR g l = Y
    lib_int

/-- **u ≡ u' (mod N) ⇒ u v ≡ u' v (mod N)**. -/
theorem modn_mul (u u' v : V Int (m + 1)) (h : ModN m u u') :
    ModN m (gmul LibInt.intR m u v) (gmul LibInt.intR m u' v) := by
  obtain ⟨t, ht⟩ := h
  refine ⟨t * sumK LibInt.intR (gt LibInt.intR m v) (L m), fun b => ?_⟩
  rw [gt_mul, gt_mul]
  have e1 : sumK LibInt.intR (fun a => LibInt.intR.mul (gt LibInt.intR m u a)
        (gt LibInt.intR m v ((ring m).add b ((ring m).neg a)))) (L m)
      = sumK LibInt.intR (fun a => (gt LibInt.intR m u' a + t) * gt LibInt.intR m v ((ring m).add b ((ring m).neg a))) (L m) :=
    sumK_ext LibInt.intR _ _ (fun a => by
      show gt LibInt.intR m u a * gt LibInt.intR m v ((ring m).add b ((ring m).neg a))
        = (gt LibInt.intR m u' a + t) * gt LibInt.intR m v ((ring m).add b ((ring m).neg a))
      rw [ht a]) (L m)
  rw [e1, sum_split]
  have e2 : sumK LibInt.intR (fun a => gt LibInt.intR m v ((ring m).add b ((ring m).neg a))) (L m)
      = sumK LibInt.intR (gt LibInt.intR m v) (L m) := by
    have hinv : ∀ a, (ring m).add b ((ring m).neg ((ring m).add b ((ring m).neg a))) = a := fun a => by lib_ring
    exact reindex LibInt.intR (L m) (CMInert.complete_allZ m LibInt.intR)
      (fun a => (ring m).add b ((ring m).neg a)) (fun a => (ring m).add b ((ring m).neg a)) hinv hinv
      (gt LibInt.intR m v)
  rw [e2]
  show sumK LibInt.intR (fun a => gt LibInt.intR m u' a * gt LibInt.intR m v ((ring m).add b ((ring m).neg a))) (L m)
      + t * sumK LibInt.intR (gt LibInt.intR m v) (L m)
    = sumK LibInt.intR (fun a => LibInt.intR.mul (gt LibInt.intR m u' a) (gt LibInt.intR m v ((ring m).add b ((ring m).neg a)))) (L m)
      + t * sumK LibInt.intR (gt LibInt.intR m v) (L m)
  rfl

end ModN

#print axioms ModN.modn_refl
#print axioms ModN.modn_symm
#print axioms ModN.modn_trans
#print axioms ModN.sum_split
#print axioms ModN.modn_mul

lang/ModSq.lean

lean_all/lang/ModSq.lean

/-
  lang/ModSq — T49 (O3') (iii): **(c u + sN)·(c ū + s'N) ≡ c² u ū (mod N)** in coefficient form.
    sc            c·u (coefficientwise)
    gt_sc_mul     (c u)·(c v) has coefficients c²·(u v)(b)
    modn_sc       W(b) = c u(b) + s ⇒ W ≡ c u (mod N)
    modn_gsig     W ≡ W' ⇒ σ_d W ≡ σ_d W'
    sq_form       W(b) = c u(b) + s ⇒ ∃ s₁, (W·σ_(−1)W)(b) = c²·(u·σ_(−1)u)(b) + s₁
  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.ModN
import lang.CycGal
import lang.StickBB

namespace ModSq

open FTAChain ZMod YMChain FiniteRecChain DetChain GroupRing ModN

variable (m : Nat)

def sc (c : Int) (u : V Int (m + 1)) : V Int (m + 1) := tb m (fun b => c * gt LibInt.intR m u b)

theorem sum_sc (c d : Int) (f : Zp m → Int) : ∀ l : List (Zp m),
    sumK LibInt.intR (fun a => (c * d) * f a) l = (c * d) * sumK LibInt.intR f l
  | [] => by show (0 : Int) = c * d * 0; lib_int
  | a :: l => by
    show c * d * f a + sumK LibInt.intR (fun a => (c * d) * f a) l = c * d * (f a + sumK LibInt.intR f l)
    rw [sum_sc c d f l]; lib_int

theorem gt_sc_mul (c : Int) (u v : V Int (m + 1)) (b : Zp m) :
    gt LibInt.intR m (gmul LibInt.intR m (sc m c u) (sc m c v)) b = c * c * gt LibInt.intR m (gmul LibInt.intR m u v) b := by
  rw [gt_mul, gt_mul]
  exact (sumK_ext LibInt.intR _
    (fun a => (c * c) * (gt LibInt.intR m u a * gt LibInt.intR m v ((ring m).add b ((ring m).neg a))))
    (fun a => by
      show gt LibInt.intR m (sc m c u) a * gt LibInt.intR m (sc m c v) ((ring m).add b ((ring m).neg a)) = _
      unfold sc
      rw [gt_tb, gt_tb]
      lib_int) (L m)).trans
    (sum_sc m c c (fun a => gt LibInt.intR m u a * gt LibInt.intR m v ((ring m).add b ((ring m).neg a))) (L m))

theorem modn_sc (c s : Int) (W u : V Int (m + 1)) (h : ∀ b, gt LibInt.intR m W b = c * gt LibInt.intR m u b + s) :
    ModN m W (sc m c u) := ⟨s, fun b => by unfold sc; rw [gt_tb]; exact h b⟩

theorem modn_gsig (d : Zp m) (W W' : V Int (m + 1)) (h : ModN m W W') :
    ModN m (CycGal.gsig LibInt.intR m d W) (CycGal.gsig LibInt.intR m d W') := by
  obtain ⟨t, ht⟩ := h
  exact ⟨t, fun b => by rw [CycGal.gt_gsig, CycGal.gt_gsig]; exact ht _⟩

theorem gsig_sc (d : Zp m) (c : Int) (u : V Int (m + 1)) :
    CycGal.gsig LibInt.intR m d (sc m c u) = sc m c (CycGal.gsig LibInt.intR m d u) :=
  ext LibInt.intR m _ _ (fun b => by unfold sc; rw [CycGal.gt_gsig, gt_tb, gt_tb, CycGal.gt_gsig])

/-- **(c u + sN)·σ_(−1)(c u + sN) ≡ c² u ū (mod N)**. -/
theorem sq_form (c s : Int) (W u : V Int (m + 1)) (h : ∀ b, gt LibInt.intR m W b = c * gt LibInt.intR m u b + s) :
    ∃ s₁, ∀ b, gt LibInt.intR m (gmul LibInt.intR m W (StickBB.J m W)) b
      = c * c * gt LibInt.intR m (gmul LibInt.intR m u (StickBB.J m u)) b + s₁ := by
  have h1 : ModN m W (sc m c u) := modn_sc m c s W u h
  have h2 : ModN m (StickBB.J m W) (sc m c (StickBB.J m u)) := by
    have := modn_gsig m (StickBB.e m) W (sc m c u) h1
    show ModN m (CycGal.gsig LibInt.intR m (StickBB.e m) W) (sc m c (CycGal.gsig LibInt.intR m (StickBB.e m) u))
    rw [← gsig_sc]; exact this
  have h3 := modn_mul m W (sc m c u) (StickBB.J m W) h1
  have h4 := modn_mul m (StickBB.J m W) (sc m c (StickBB.J m u)) (sc m c u) h2
  rw [gm_comm LibInt.intR m (StickBB.J m W) (sc m c u), gm_comm LibInt.intR m (sc m c (StickBB.J m u)) (sc m c u)] at h4
  obtain ⟨t, ht⟩ := modn_trans m _ _ _ h3 h4
  exact ⟨t, fun b => by rw [ht b, gt_sc_mul]⟩

end ModSq

#print axioms ModSq.sum_sc
#print axioms ModSq.gt_sc_mul
#print axioms ModSq.modn_sc
#print axioms ModSq.modn_gsig
#print axioms ModSq.gsig_sc
#print axioms ModSq.sq_form

lang/Mult32.lean

lean_all/lang/Mult32.lean

/-
  lang/Mult32 — T47/T50, cycle 583: **the 32a eta coefficients are multiplicative at every pair of coprime odd m, n**:
    [q^(mn)]η(4z)²η(8z)² = [q^m]·[q^n]   (m, n odd, gcd(m, n) = 1),
  from c_(mn) = c_m c_n (HeckeEuler.mult2) and Im c_n = 0 (b ↦ −b permutes the primary elements of norm n).

    zl_neg, pd_conj, im_c     Σ over zl is symmetric; PD n (a, −b) ⇔ PD n (a, b); Im c_n = 0
    eta32_mult
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.Ap32
import lang.HeckeEuler

namespace Hecke32

open DetChain YMChain FiniteRecChain HeckeMult GaussEuclid GaussUnique GaussInt LibGcd

theorem zl_neg (g : Int → Int) : ∀ N, sumK Z g (zl N) = sumK Z (fun b => g (-b)) (zl N)
  | 0 => by show Z.add (g 0) Z.zero = Z.add (g (-0)) Z.zero; rfl
  | N + 1 => by
    show sumK Z g (zl N ++ [Int.ofNat (N + 1), Int.negSucc N]) = sumK Z (fun b => g (-b)) (zl N ++ [Int.ofNat (N + 1), Int.negSucc N])
    rw [sumK_append, sumK_append, zl_neg g N]
    show Z.add _ (Z.add (g (Int.ofNat (N + 1))) (Z.add (g (Int.negSucc N)) Z.zero)) =
      Z.add _ (Z.add (g (-Int.ofNat (N + 1))) (Z.add (g (-Int.negSucc N)) Z.zero))
    rw [show -Int.ofNat (N + 1) = Int.negSucc N from rfl, show -Int.negSucc N = Int.ofNat (N + 1) from rfl]
    lib_ring

theorem prim_conj (a b : Int) (h : Prim (a, b)) : Prim (a, -b) := by
  obtain ⟨⟨c, hc⟩, ⟨e, he⟩⟩ := h
  have hb : b = 2 * c := hc
  refine ⟨⟨-c, by show -b = 2 * -c; rw [hb]; lib_int⟩, ⟨e - c, ?_⟩⟩
  show a + -b - 1 = 4 * (e - c)
  have he' : a + b - 1 = 4 * e := he
  calc a + -b - 1 = (a + b - 1) - 2 * b := by lib_int
    _ = _ := by rw [he', hb]; lib_int

theorem pd_conj (n : Nat) (a b : Int) (h : PD n (a, b)) : PD n (a, -b) :=
  ⟨(show a * a + -b * -b = a * a + b * b by lib_int).trans h.1, prim_conj a b h.2⟩

theorem snd_I (n : Nat) (z : Int × Int) : ((Cyclo4.cyc4 Z).mul (I n z) z).2 = if PD n z then z.2 else 0 := by
  by_cases h : PD n z
  · rw [I_pos n z h, ite_pos' _ _ h]; show Z.add (Z.mul Z.one z.2) (Z.mul Z.zero z.1) = z.2; lib_ring
  · rw [I_neg n z h, ite_neg' _ _ h]; show Z.add (Z.mul Z.zero z.2) (Z.mul Z.zero z.1) = Z.zero; lib_ring

theorem snd_sumK {A : Type} (f : A → Int × Int) : ∀ l : List A, (sumK (Cyclo4.cyc4 Z) f l).2 = sumK Z (fun a => (f a).2) l
  | [] => rfl
  | a :: l => by
    show Z.add (f a).2 (sumK (Cyclo4.cyc4 Z) f l).2 = Z.add (f a).2 (sumK Z (fun a => (f a).2) l)
    rw [snd_sumK f l]

/-- **Im c_n = 0.** -/
theorem im_c (n : Nat) : (c n).2 = 0 := by
  have h1 : (c n).2 = sumK Z (fun a => sumK Z (fun b => if PD n (a, b) then b else 0) (zl n)) (zl n) := by
    show (sumK (Cyclo4.cyc4 Z) _ (box n)).2 = _
    rw [snd_sumK]
    unfold box; rw [sumK_pairs]
    exact sumK_ext Z _ _ (fun a => sumK_ext Z _ _ (fun b => snd_I n (a, b)) _) _
  have h2 : ∀ a, sumK Z (fun b => if PD n (a, b) then b else 0) (zl n) = -sumK Z (fun b => if PD n (a, b) then b else 0) (zl n) := by
    intro a
    have hs := zl_neg (fun b => if PD n (a, b) then b else 0) n
    have hneg : ∀ b, (if PD n (a, -b) then -b else (0 : Int)) = -(if PD n (a, b) then b else 0) := fun b => by
      by_cases h : PD n (a, b)
      · rw [ite_pos' _ _ (pd_conj n a b h), ite_pos' _ _ h]
      · have h' : ¬ PD n (a, -b) := fun e => h (by have := pd_conj n a (-b) e; rw [show - -b = b by lib_int] at this; exact this)
        rw [ite_neg' _ _ h', ite_neg' _ _ h]; rfl
    rw [sumK_ext Z _ _ hneg] at hs
    have hs' : sumK Z (fun b => if PD n (a, b) then b else 0) (zl n) = sumK Z (fun b => Z.neg (if PD n (a, b) then b else 0)) (zl n) := hs
    rw [CMInert.sum_neg_fun Z (fun b => if PD n (a, b) then b else 0)] at hs'
    exact hs'
  rw [h1]
  have h3 : ∀ a, sumK Z (fun b => if PD n (a, b) then b else 0) (zl n) = 0 := fun a => by
    have := h2 a
    generalize sumK Z (fun b => if PD n (a, b) then b else 0) (zl n) = S at this
    exact two_cancel S 0 (by calc 2 * S = S + S := by lib_int
      _ = -S + S := by rw [← this]
      _ = 2 * 0 := by lib_int)
  rw [sumK_ext Z _ (fun _ => Z.zero) h3]
  exact HeckePrime.zsumR Z _

/-- **Multiplicativity of the 32a coefficients.** -/
theorem eta32_mult (m n : Nat) (hm : HeckeEuler.Odd' m) (hn : HeckeEuler.Odd' n) (hc : HeckeEuler.Cop m n) :
    etaC (m * n) = etaC m * etaC n := by
  rw [etaC_c, etaC_c, etaC_c, HeckeEuler.mult2 m n hm hn hc]
  show Z.add (Z.mul (c m).1 (c n).1) (Z.neg (Z.mul (c m).2 (c n).2)) = (c m).1 * (c n).1
  rw [im_c, im_c]; show (c m).1 * (c n).1 + -(0 * 0) = (c m).1 * (c n).1; lib_int

end Hecke32

#print axioms Hecke32.zl_neg
#print axioms Hecke32.prim_conj
#print axioms Hecke32.pd_conj
#print axioms Hecke32.snd_I
#print axioms Hecke32.snd_sumK
#print axioms Hecke32.im_c
#print axioms Hecke32.eta32_mult

lang/Mult36.lean

lean_all/lang/Mult36.lean

/-
  lang/Mult36 — T47/T50, cycle 623: **the coefficients of η(6z)⁴ (36a) are multiplicative**:
    c3(mn) = c3(m) c3(n)   for coprime m, n prime to 6,
  where c3 n = Σ_{x ≡ 1 (3), x² + 3y² = n} x = etaC36 n (Theta36, Eta36All).
    sum_pair     a sum in ℤ[ω] is the pair of the coordinate sums
    js_phi       for n odd: [β = (x + y, 2y) ∈ S, N β = n] = [x ≡ 1 (3), x² + 3y² = n]
    sumS         Σ_{β ∈ S, N β = n} β = (c3 n, 0)                     (β ↔ (x, y); Theta36.c3_xy, g3_zero)
    cw3_c3       cw3 W36 n = (c3 n, 0) for n prime to 6                 (Theta36W.cw3_w36)
    c3_mult      c3 multiplicative (HeckeMult3.hecke_mult3 with W36Mul.w36_mul)
    eta36_mult   the η(6z)⁴ coefficients are multiplicative
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Theta36W
import lang.Eta36All

namespace Mult36

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius EisEuclid EisPrim HeckeMult3 W36Mul Theta36W Theta36

theorem sum_pair {A : Type} (f : A → Int × Int) : ∀ l : List A,
    sumK (Cyclo3.cyc3 LibInt.intR) f l = (sumK LibInt.intR (fun a => (f a).1) l, sumK LibInt.intR (fun a => (f a).2) l)
  | [] => rfl
  | a :: l => by
    show (Cyclo3.cyc3 LibInt.intR).add (f a) (sumK (Cyclo3.cyc3 LibInt.intR) f l) = _
    rw [sum_pair f l]; rfl

def phi (p : Int × Int) : Int × Int := (p.1 + p.2, 2 * p.2)

theorem en_phi (x y : Int) : en (phi (x, y)) = x * x + 3 * (y * y) := by
  show (x + y) * (x + y) + -((x + y) * (2 * y)) + 2 * y * (2 * y) = _; lib_int

theorem js_phi (n : Nat) (hodd : ∃ k, Int.ofNat n = 2 * k + 1) (x y : Int) :
    JS n (phi (x, y)) = if P3 n (x, y) then (Cyclo3.cyc3 LibInt.intR).one else (Cyclo3.cyc3 LibInt.intR).zero := by
  unfold JS
  by_cases h : P3 n (x, y)
  · rw [ite_pos' _ _ h]
    obtain ⟨hn0, c, hc⟩ := h
    have hn : x * x + 3 * (y * y) = Int.ofNat n := hn0
    have hc' : x - 1 = 3 * c := hc
    have hxy : ∃ r, x + y - 1 = 2 * r := by
      obtain ⟨K, hK⟩ := hodd
      rcases GaussInt.int_even_odd x with ⟨a, ha⟩ | ⟨a, ha⟩ <;> rcases GaussInt.int_even_odd y with ⟨b, hb⟩ | ⟨b, hb⟩
      · exact (GaussInt.int_odd_ne_even K (2 * a * a + 6 * b * b) (by rw [← hK, ← hn, ha, hb]; lib_int)).elim
      · exact ⟨a + b, by rw [ha, hb]; lib_int⟩
      · exact ⟨a + b, by rw [ha, hb]; lib_int⟩
      · exact (GaussInt.int_odd_ne_even K (2 * a * a + 2 * a + 6 * b * b + 6 * b + 2) (by rw [← hK, ← hn, ha, hb]; lib_int)).elim
    obtain ⟨r, hr⟩ := hxy
    rw [ite_pos' _ _ ⟨⟨⟨y, rfl⟩, ⟨r, hr⟩, ⟨2 * c, by show 2 * (x + y) - 2 * y - 2 = _; rw [show 2 * (x + y) - 2 * y - 2 = 2 * (x - 1) by lib_int, hc']; lib_int⟩⟩, by rw [en_phi, hn]⟩]
  · rw [ite_neg' _ _ h]
    rw [ite_neg' _ _ (fun hs => h ⟨by rw [← en_phi]; exact hs.2, by
      obtain ⟨_, _, ⟨c, hc⟩⟩ := hs.1
      have hc' : 2 * (x + y) - 2 * y - 2 = 3 * c := hc
      exact ⟨2 * c + -(x - 1), by
        calc x - 1 = 4 * (x - 1) + -(3 * (x - 1)) := by lib_int
          _ = 2 * (2 * (x + y) - 2 * y - 2) + -(3 * (x - 1)) := by lib_int
          _ = _ := by rw [hc']; lib_int⟩⟩)]

theorem natsq_le (a : Int) (b : Int) (n : Nat) (h : a * a + 3 * (b * b) = Int.ofNat n) : Int.natAbs a ≤ n ∧ Int.natAbs b ≤ n := by
  have h' : Int.natAbs a * Int.natAbs a + 3 * (Int.natAbs b * Int.natAbs b) = n := Int.ofNat.inj (by
    show Int.ofNat (Int.natAbs a * Int.natAbs a) + 3 * Int.ofNat (Int.natAbs b * Int.natAbs b) = _
    rw [GaussEuclid.nas, GaussEuclid.nas, h])
  constructor
  · exact Nat.le_trans (HeckeMult.le_sq _) (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq h'))
  · exact Nat.le_trans (HeckeMult.le_sq _) (Nat.le_trans (Nat.le_trans (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ (Nat.le_succ_of_le (Nat.le_succ 1)))) (Nat.le_add_left _ _)) (Nat.le_of_eq h'))

/-- **Σ_{β ∈ S, N β = n} β = (c3 n, 0)** for odd n. -/
theorem sumS (n : Nat) (hodd : ∃ k, Int.ofNat n = 2 * k + 1) :
    sumK (Cyclo3.cyc3 LibInt.intR) (fun β => (Cyclo3.cyc3 LibInt.intR).mul (JS n β) β) (box3 n) = (c3 n, 0) := by
  -- step 1: insert Σ_p δ(φ p, β)
  have st1 : ∀ β, (Cyclo3.cyc3 LibInt.intR).mul (JS n β) β =
      sumK (Cyclo3.cyc3 LibInt.intR) (fun p => (Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) β (phi p)) ((Cyclo3.cyc3 LibInt.intR).mul (JS n β) β)) (HeckeMult.box n) := fun β => by
    unfold JS
    by_cases h : InS β ∧ en β = Int.ofNat n
    · rw [ite_pos' _ _ h, ← sumK_mul]
      obtain ⟨⟨s, hs⟩, _, _⟩ := h.1
      have hs' : β.2 = 2 * s := hs
      have hq : (β.1 - s) * (β.1 - s) + 3 * (s * s) = Int.ofNat n := by
        rw [← h.2]; show _ = β.1 * β.1 + -(β.1 * β.2) + β.2 * β.2; rw [hs']; lib_int
      have hb := natsq_le _ _ n hq
      rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun p => delta (Cyclo3.cyc3 LibInt.intR) p (β.1 - s, s)) (fun p => by
        unfold delta
        by_cases e : p = (β.1 - s, s)
        · rw [ite_pos' _ _ e, ite_pos' _ _ (by rw [e]; exact Prod.ext (show β.1 = β.1 - s + s by lib_int) (show β.2 = 2 * s from hs'))]
        · rw [ite_neg' _ _ e, ite_neg' _ _ (fun (e' : β = phi p) => e (by
            obtain ⟨p1, p2⟩ := p
            have e1 : β.1 = p1 + p2 := congrArg Prod.fst e'
            have e2 : β.2 = 2 * p2 := congrArg Prod.snd e'
            have hp2 : p2 = s := two_cancel_int p2 s (by rw [← e2, hs'])
            exact Prod.ext (show p1 = β.1 - s by rw [e1, hp2]; lib_int) hp2))]),
        HeckeMult.count_box (Cyclo3.cyc3 LibInt.intR) (β.1 - s, s) n hb.1 hb.2]
      lib_ring
    · rw [ite_neg' _ _ h]
      rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun _ => (Cyclo3.cyc3 LibInt.intR).zero) (fun p => by lib_ring), zsum3]; lib_ring
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ st1, sumK_swap]
  -- step 2: Σ_β δ(β, φ p) JS β β = JS(φ p) φ p
  have st2 : ∀ p, sumK (Cyclo3.cyc3 LibInt.intR) (fun β => (Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) β (phi p)) ((Cyclo3.cyc3 LibInt.intR).mul (JS n β) β)) (box3 n) =
      (Cyclo3.cyc3 LibInt.intR).mul (JS n (phi p)) (phi p) := fun p => by
    by_cases h : InS (phi p) ∧ en (phi p) = Int.ofNat n
    · have hb := abs_le3 _ n h.2
      exact HeckeMult.pick_box (Cyclo3.cyc3 LibInt.intR) (fun β => (Cyclo3.cyc3 LibInt.intR).mul (JS n β) β) (phi p) _ hb.1 hb.2
    · have hz : JS n (phi p) = (Cyclo3.cyc3 LibInt.intR).zero := ite_neg' _ _ h
      rw [hz, sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun _ => (Cyclo3.cyc3 LibInt.intR).zero) (fun β => by
        unfold delta
        by_cases e : β = phi p
        · rw [ite_pos' _ _ e, e, hz]; lib_ring
        · rw [ite_neg' _ _ e]; lib_ring), zsum3]; lib_ring
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ st2]
  unfold HeckeMult.box; rw [HeckeMult.sumK_pairs]
  -- step 3: coordinates
  have st3 : ∀ x y, (Cyclo3.cyc3 LibInt.intR).mul (JS n (phi (x, y))) (phi (x, y)) = (H3 n (x, y), 2 * G3 n (x, y)) := fun x y => by
    rw [js_phi n hodd x y]; unfold H3 G3
    by_cases h : P3 n (x, y)
    · rw [ite_pos' _ _ h, ite_pos' _ _ h, ite_pos' _ _ h]
      apply Prod.ext
      · show 1 * (x + y) + -(0 * (2 * y)) = x + y; lib_int
      · show 1 * (2 * y) + 0 * (x + y) + -(0 * (2 * y)) = 2 * y; lib_int
    · rw [ite_neg' _ _ h, ite_neg' _ _ h, ite_neg' _ _ h]
      apply Prod.ext
      · show 0 * (x + y) + -(0 * (2 * y)) = 0; lib_int
      · show 0 * (2 * y) + 0 * (x + y) + -(0 * (2 * y)) = 2 * 0; lib_int
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun x => sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun y => st3 x y) _)]
  rw [sum_pair]
  rw [sumK_ext LibInt.intR _ _ (fun x => congrArg Prod.fst (sum_pair (fun y => (H3 n (x, y), 2 * G3 n (x, y))) (HeckeMult.zl n))),
    sumK_ext LibInt.intR (fun x => (sumK (Cyclo3.cyc3 LibInt.intR) (fun y => (H3 n (x, y), 2 * G3 n (x, y))) (HeckeMult.zl n)).2) _
      (fun x => congrArg Prod.snd (sum_pair (fun y => (H3 n (x, y), 2 * G3 n (x, y))) (HeckeMult.zl n)))]
  apply Prod.ext
  · exact (c3_xy n).symm
  · show sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => 2 * G3 n (x, y)) (HeckeMult.zl n)) (HeckeMult.zl n) = 0
    rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.mul 2 (sumK LibInt.intR (fun y => G3 n (x, y)) (HeckeMult.zl n))) (fun x => by rw [mul_sumK]; rfl),
      ← mul_sumK, g3_zero]
    show (2 : Int) * 0 = 0; rfl

theorem cw3_c3 (n : Nat) (hodd : ∃ k, Int.ofNat n = 2 * k + 1) (h3 : C3 (Int.ofNat n)) : cw3 W36 n = (c3 n, 0) := by
  rw [cw3_w36 n hodd h3, sumS n hodd]

theorem odd_mul (a b : Int) (ha : ∃ k, a = 2 * k + 1) (hb : ∃ k, b = 2 * k + 1) : ∃ k, a * b = 2 * k + 1 := by
  obtain ⟨k, hk⟩ := ha; obtain ⟨l, hl⟩ := hb
  exact ⟨2 * k * l + k + l, by rw [hk, hl]; lib_int⟩

theorem c3_mul (a b : Int) (ha : C3 a) (hb : C3 b) : C3 (a * b) := by
  obtain ⟨k, hk | hk⟩ := ha <;> obtain ⟨l, hl | hl⟩ := hb
  · exact ⟨3 * k * l + k + l, Or.inl (by rw [hk, hl]; lib_int)⟩
  · exact ⟨3 * k * l + 2 * k + l, Or.inr (by rw [hk, hl]; lib_int)⟩
  · exact ⟨3 * k * l + k + 2 * l, Or.inr (by rw [hk, hl]; lib_int)⟩
  · exact ⟨3 * k * l + 2 * k + 2 * l + 1, Or.inl (by rw [hk, hl]; lib_int)⟩

/-- **c3(mn) = c3(m) c3(n)** for coprime m, n prime to 6. -/
theorem c3_mult (m n : Nat) (s t : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (om : ∃ k, Int.ofNat (m + 1) = 2 * k + 1) (on : ∃ k, Int.ofNat (n + 1) = 2 * k + 1)
    (hm : C3 (Int.ofNat (m + 1))) (hn : C3 (Int.ofNat (n + 1))) :
    c3 ((m + 1) * (n + 1)) = c3 (m + 1) * c3 (n + 1) := by
  have h := hecke_mult3 W36 w36_mul m n s t hst hm hn
  rw [cw3_c3 _ om hm, cw3_c3 _ on hn, cw3_c3 _ (odd_mul _ _ om on) (c3_mul _ _ hm hn)] at h
  have h1 : c3 ((m + 1) * (n + 1)) = c3 (m + 1) * c3 (n + 1) + -(0 * 0) := congrArg Prod.fst h
  rw [h1]; lib_int

/-- **The coefficients of η(6z)⁴ are multiplicative** (coprime arguments prime to 6). -/
theorem eta36_mult (m n : Nat) (s t : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (om : ∃ k, Int.ofNat (m + 1) = 2 * k + 1) (on : ∃ k, Int.ofNat (n + 1) = 2 * k + 1)
    (hm : C3 (Int.ofNat (m + 1))) (hn : C3 (Int.ofNat (n + 1))) :
    Hecke36.etaC36 ((m + 1) * (n + 1)) = Hecke36.etaC36 (m + 1) * Hecke36.etaC36 (n + 1) := by
  rw [Theta36.eta36_all, Theta36.eta36_all, Theta36.eta36_all]
  exact c3_mult m n s t hst om on hm hn

end Mult36

#print axioms Mult36.sum_pair
#print axioms Mult36.en_phi
#print axioms Mult36.js_phi
#print axioms Mult36.natsq_le
#print axioms Mult36.sumS
#print axioms Mult36.cw3_c3
#print axioms Mult36.odd_mul
#print axioms Mult36.c3_mul
#print axioms Mult36.c3_mult
#print axioms Mult36.eta36_mult

lang/Nine9.lean

lean_all/lang/Nine9.lean

/-
  lang/Nine9 — T43, cycle 398: Dirichlet for p ≡ 8 (mod 9), part 1 (ring-theoretic tools).

    phi9        z·w = 1, f(X) = X³ − 3X + 1  ⟹  z³ f(z + w) = 1 + z³ + z⁶ = Φ₉(z)   (f = minimal polynomial of 2cos(2π/9))
    ninth       Φ₉(z) = 0  ⟹  z⁹ = 1
    cube_three  Φ₉(z) = 0, z³ = 1  ⟹  3 = 0
    one_of      z⁹ = 1, z^r = 1, r·a = 9b + 1  ⟹  z = 1
    key9        Φ₉(z) = 0, z^N = 1  ⟹  9 | N or 3 = 0
  in any commutative ring. Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Order
import lang.TwoPower
import lib.RingTac

namespace Nine

open FTAChain

variable {R : Type} (K : DetChain.CRing R)

theorem phi9 (u v : R) (huv : K.mul u v = K.one) :
    K.mul (K.mul u (K.mul u u)) (K.add (K.add (K.mul (K.add (u) (v)) (K.mul (K.add (u) (v)) (K.add (u) (v)))) (K.neg (K.mul (K.add (K.one) (K.add (K.one) (K.one))) (K.add (u) (v))))) (K.one)) = K.add (K.add (K.one) (K.mul (u) (K.mul (u) (u)))) (K.mul (K.mul (u) (K.mul (u) (u))) (K.mul (u) (K.mul (u) (u)))) := by
  calc _ = K.add (K.add (K.add (K.one) (K.mul (u) (K.mul (u) (u)))) (K.mul (K.mul (u) (K.mul (u) (u))) (K.mul (u) (K.mul (u) (u))))) (K.mul (K.add (K.add (K.one) (K.mul v (u))) (K.add (K.mul v (K.mul v (K.mul u (u)))) (K.add (K.mul (K.add K.one (K.add K.one K.one)) (K.mul v (K.mul u (K.mul u (u))))) (K.mul (K.add K.one (K.add K.one K.one)) (K.mul u (K.mul u (K.mul u (u)))))))) (K.add (K.mul u v) (  -- (+29 chars; full line in the Zenodo source)
    _ = _ := by rw [huv]; lib_ring

theorem pw9 (u : R) : Frobenius.pw K u 9 = K.mul (K.mul u (K.mul u u)) (K.mul (K.mul u (K.mul u u)) (K.mul u (K.mul u u))) := by
  show K.mul (K.mul (K.mul (K.mul (K.mul (K.mul (K.mul (K.mul (K.mul K.one u) u) u) u) u) u) u) u) u = _
  lib_ring

theorem pw3 (u : R) : Frobenius.pw K u 3 = K.mul u (K.mul u u) := by
  show K.mul (K.mul (K.mul K.one u) u) u = _
  lib_ring

theorem ninth (u : R) (h : K.add (K.add (K.one) (K.mul (u) (K.mul (u) (u)))) (K.mul (K.mul (u) (K.mul (u) (u))) (K.mul (u) (K.mul (u) (u)))) = K.zero) : Frobenius.pw K u 9 = K.one := by
  rw [pw9 K u]
  calc _ = K.add (K.mul (K.add (K.mul u (K.mul u u)) (K.neg K.one)) (K.add (K.add (K.one) (K.mul (u) (K.mul (u) (u)))) (K.mul (K.mul (u) (K.mul (u) (u))) (K.mul (u) (K.mul (u) (u)))))) K.one := by lib_ring
    _ = K.one := by rw [h]; lib_ring

theorem cube_three (u : R) (h : K.add (K.add (K.one) (K.mul (u) (K.mul (u) (u)))) (K.mul (K.mul (u) (K.mul (u) (u))) (K.mul (u) (K.mul (u) (u)))) = K.zero) (hc : Frobenius.pw K u 3 = K.one) : K.add K.one (K.add K.one K.one) = K.zero := by
  rw [pw3 K u] at hc
  calc _ = K.add K.one (K.add (K.mul u (K.mul u u)) (K.mul (K.mul u (K.mul u u)) (K.mul u (K.mul u u)))) := by rw [hc]; lib_ring
    _ = K.add (K.add (K.one) (K.mul (u) (K.mul (u) (u)))) (K.mul (K.mul (u) (K.mul (u) (u))) (K.mul (u) (K.mul (u) (u)))) := by lib_ring
    _ = K.zero := h

theorem one_of (u : R) (h9 : Frobenius.pw K u 9 = K.one) (r a b : Nat) (hr : Frobenius.pw K u r = K.one) (hab : r * a = 9 * b + 1) :
    u = K.one := by
  calc u = K.mul (Frobenius.pw K (Frobenius.pw K u 9) b) u := by rw [h9, TwoPower.pwone]; lib_ring
    _ = Frobenius.pw K u (9 * b + 1) := by rw [TwoPower.pwpow]; rfl
    _ = Frobenius.pw K (Frobenius.pw K u r) a := by rw [TwoPower.pwpow, hab]
    _ = K.one := by rw [hr, TwoPower.pwone]

theorem key9 (u : R) (h : K.add (K.add (K.one) (K.mul (u) (K.mul (u) (u)))) (K.mul (K.mul (u) (K.mul (u) (u))) (K.mul (u) (K.mul (u) (u)))) = K.zero) (N : Nat) (hN : Frobenius.pw K u N = K.one) :
    Dv 9 N ∨ K.add K.one (K.add K.one K.one) = K.zero := by
  have h9 := ninth K u h
  obtain ⟨q, r, hqr, hr⟩ := Order.divmod 8 N
  have hxr : Frobenius.pw K u r = K.one := by
    rw [← Order.pw_qe K u 9 r h9 q]; rw [← hqr]; exact hN
  match r, hr, hxr, hqr with
  | 0, _, _, hqr => exact Or.inl ⟨q, by rw [hqr]; rfl⟩
  | 1, _, hxr, _ => exact Or.inr (cube_three K u h (by rw [one_of K u h9 1 1 0 hxr rfl]; exact TwoPower.pwone K 3))
  | 2, _, hxr, _ => exact Or.inr (cube_three K u h (by rw [one_of K u h9 2 5 1 hxr rfl]; exact TwoPower.pwone K 3))
  | 4, _, hxr, _ => exact Or.inr (cube_three K u h (by rw [one_of K u h9 4 7 3 hxr rfl]; exact TwoPower.pwone K 3))
  | 5, _, hxr, _ => exact Or.inr (cube_three K u h (by rw [one_of K u h9 5 2 1 hxr rfl]; exact TwoPower.pwone K 3))
  | 7, _, hxr, _ => exact Or.inr (cube_three K u h (by rw [one_of K u h9 7 4 3 hxr rfl]; exact TwoPower.pwone K 3))
  | 8, _, hxr, _ => exact Or.inr (cube_three K u h (by rw [one_of K u h9 8 8 7 hxr rfl]; exact TwoPower.pwone K 3))
  | 3, _, hxr, _ => exact Or.inr (cube_three K u h hxr)
  | 6, _, hxr, _ => exact Or.inr (cube_three K u h (Order.exp_sub K u 6 3 hxr h9))
  | r + 9, hr, _, _ => exact absurd hr (fun h => Nat.lt_irrefl 9 (Nat.lt_of_le_of_lt (Nat.le_add_left 9 r) h))

end Nine

#print axioms Nine.phi9
#print axioms Nine.pw9
#print axioms Nine.pw3
#print axioms Nine.ninth
#print axioms Nine.cube_three
#print axioms Nine.one_of
#print axioms Nine.key9

lang/Nine9b.lean

lean_all/lang/Nine9b.lean

/-
  lang/Nine9b — T43, cycle 398: Dirichlet for p ≡ 8 (mod 9), part 2: a root of f(X) = X³ − 3X + 1 in 𝔽_q (q = 2m + 3) forces
  9 | q − 1, 9 | q + 1 or 3 = 0 in 𝔽_q.
    root_D0    D = r² − 4 = 0: r = ±2, f(2) = 3, f(−2) = −1
    root_sq    D a nonzero square: z = (r + s)/2 ∈ 𝔽_q, Φ₉(z) = 0, Fermat
    root_nsq   D a non-square: z = (r + √D)/2 ∈ 𝔽_q[√D], Φ₉(z) = 0, z^(q+1) = N(z) = 1
    root9      the three cases together
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Nine9
import lang.EulerHard
import lang.Euler
import lang.G4Map
import lang.Fq2

namespace Nine

open FTAChain ZMod YMChain ConicSum G4Gen G4Map

variable (m : Nat)

def fr (x : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) (x)))) ((ring (2 *  -- (+17 chars; full line in the Zenodo source)
def three : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))

theorem root_D0 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x : Zp (2 * m + 1 + 1)) (hf : fr m x = (ring (2 * m + 1 + 1)).zero) (hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ri  -- (+133 chars; full line in the Zenodo source)
  unfold fr at hf
  have h := mul_eq_zero m hpr ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) (by rw [← hD]; lib_ring)
  rcases h with e | e
  · have hx : x = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one) := by
      calc x = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) := by lib_ring
        _ = _ := by rw [e]; lib_ring
    rw [hx] at hf
    unfold three
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m  -- (+321 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := hf
  · have hx : x = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) := by
      calc x = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) := by lib_ring
        _ = _ := by rw [e]; lib_ring
    rw [hx] at hf
    exact absurd (show (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).zero by
      calc (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 +   -- (+493 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [hf]; lib_ring) (one_ne m)

theorem root_sq (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h2 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one) ≠ (ring (2 * m + 1 + 1)).zero) (x : Zp (2 * m + 1 + 1)) (hf : fr m x = (ring (2 * m + 1 + 1)).zero)
    (s : Zp (2 * m + 1 + 1)) (hs : (ring (2 * m + 1 + 1)).mul s s = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))))) : Dv 9 (2 * m + 1 + 1) ∨ three m =   -- (+33 chars; full line in the Zenodo source)
  have ei : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ h2
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) = i at ei
  obtain ⟨u, hu⟩ : ∃ u : Zp (2 * m + 1 + 1), u = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x s) i := ⟨_, rfl⟩
  obtain ⟨v, hv⟩ : ∃ v : Zp (2 * m + 1 + 1), v = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg s)) i := ⟨_, rfl⟩
  have huv : (ring (2 * m + 1 + 1)).mul u v = (ring (2 * m + 1 + 1)).one := by
    rw [hu, hv]
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul i i) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul s s)))) (ring (2 * m + 1 + 1)).zero := by lib_ring
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) := by rw [hs]; lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [ei]; lib_ring
  have hsum : (ring (2 * m + 1 + 1)).add u v = x := by
    rw [hu, hv]
    calc _ = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) := by lib_ring
      _ = x := by rw [ei]; lib_ring
  have hp := phi9 (ring (2 * m + 1 + 1)) u v huv
  rw [hsum] at hp
  unfold fr at hf
  rw [hf] at hp
  have hu0 : u ≠ (ring (2 * m + 1 + 1)).zero := fun e => one_ne m (by rw [← huv, e]; lib_ring)
  have hferm : Frobenius.pw (ring (2 * m + 1 + 1)) u (2 * m + 1 + 1) = (ring (2 * m + 1 + 1)).one := by
    have hF := ZMod.fermat (2 * m + 1) hpr u
    have hz : (ring (2 * m + 1 + 1)).mul u ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) u (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) u (2 * m + 1 + 1)) u) ((ring (2 * m + 1 + 1)).neg u) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [show (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) u (2 * m + 1 + 1)) u = u from hF]; lib_ring
    rcases mul_eq_zero m hpr _ _ hz with e | e
    · exact absurd e hu0
    · calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) u (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
        _ = (ring (2 * m + 1 + 1)).one := by rw [e]; lib_ring
  exact key9 (ring (2 * m + 1 + 1)) u (by rw [← hp]; lib_ring) _ hferm

theorem root_nsq (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h2 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one) ≠ (ring (2 * m + 1 + 1)).zero) (x : Zp (2 * m + 1 + 1)) (hf : fr m x = (ring (2 * m + 1 + 1)).zero)
    (hn : Frobenius.pw (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))))) (m + 1) = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 +   -- (+85 chars; full line in the Zenodo source)
  have ei : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ h2
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) = i at ei
  have hzw : (Fq2.G2 (2 * m + 1) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)))))).mul ((ring (2 * m + 1 + 1)).mul x i, i) ((ring (2 * m + 1 + 1)).mul   -- (+365 chars; full line in the Zenodo source)
    apply Prod.ext
    · show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x i) ((ring (2 * m + 1 + 1)).mul x i)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((r  -- (+150 chars; full line in the Zenodo source)
      calc _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).one := by rw [ei]; lib_ring
    · show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x i) ((ring (2 * m + 1 + 1)).neg i)) ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul x i)) = (ring (2 * m + 1 + 1)).zero
      lib_ring
  have hsum : (Fq2.G2 (2 * m + 1) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)))))).add ((ring (2 * m + 1 + 1)).mul x i, i) ((ring (2 * m + 1 + 1)).mul  -- (+76 chars; full line in the Zenodo source)
    apply Prod.ext
    · show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x i) ((ring (2 * m + 1 + 1)).mul x i) = x
      calc _ = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) := by lib_ring
        _ = x := by rw [ei]; lib_ring
    · show (ring (2 * m + 1 + 1)).add i ((ring (2 * m + 1 + 1)).neg i) = (ring (2 * m + 1 + 1)).zero
      lib_ring
  have hp := phi9 (Fq2.G2 (2 * m + 1) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)))))) _ _ hzw
  rw [hsum] at hp
  have hfe : (Fq2.G2 (2 * m + 1) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)))))).add ((Fq2.G2 (2 * m + 1) ((ring (2 * m + 1 + 1)).add ((ring (2 * m +  -- (+3982 chars; full line in the Zenodo source)
    apply Prod.ext
    · show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((r  -- (+1580 chars; full line in the Zenodo source)
      unfold fr at hf
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) (x)))) ((ring (2 * m + 1 + 1)).one) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := hf
    · show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).zero)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).zero) (x)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x)   -- (+927 chars; full line in the Zenodo source)
      lib_ring
  rw [hfe, DetChain.mul_zero] at hp
  have hq := Fq2.pw_p1 (2 * m + 1) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))))) (m + 1) hpr rfl hn ((ring (2 * m + 1 + 1)).mul x i, i)
  have hN : Frobenius.pw (Fq2.G2 (2 * m + 1) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)))))) ((ring (2 * m + 1 + 1)).mul x i, i) (2 * m + 1 + 1 + 1 +  -- (+334 chars; full line in the Zenodo source)
    rw [hq]
    apply Prod.ext
    · show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x i) ((ring (2 * m + 1 + 1)).mul x i)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((r  -- (+150 chars; full line in the Zenodo source)
      calc _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).one := by rw [ei]; lib_ring
    · rfl
  rcases key9 (Fq2.G2 (2 * m + 1) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)))))) _ hp.symm _ hN with h | h
  · exact Or.inl h
  · exact Or.inr (congrArg Prod.fst h)

/-- **A root of f in 𝔽_q**: 9 | q − 1, 9 | q + 1, or 3 = 0 in 𝔽_q. -/
theorem root9 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h2 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one) ≠ (ring (2 * m + 1 + 1)).zero) (x : Zp (2 * m + 1 + 1)) (hf : fr m x = (ring (2 * m + 1 + 1)).zero) :
    Dv 9 (2 * m + 1 + 1) ∨ Dv 9 (2 * m + 1 + 1 + 1 + 1) ∨ three m = (ring (2 * m + 1 + 1)).zero := by
  by_cases hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)))) = (ring (2 * m + 1 + 1)).zero
  · exact Or.inr (Or.inr (root_D0 m hpr x hf hD))
  · rcases Euler.euler_pm m hpr _ hD with h | h
    · obtain ⟨s, hs⟩ := EulerHard.euler_hard m hpr _ h
      rcases root_sq m hpr h2 x hf s hs with h' | h'
      · exact Or.inl h'
      · exact Or.inr (Or.inr h')
    · rcases root_nsq m hpr h2 x hf h with h' | h'
      · exact Or.inr (Or.inl h')
      · exact Or.inr (Or.inr h')

end Nine

#print axioms Nine.root_D0
#print axioms Nine.root_sq
#print axioms Nine.root_nsq
#print axioms Nine.root9

lang/Nine9c.lean

lean_all/lang/Nine9c.lean

/-
  lang/Nine9c — T43, cycle 398: **infinitely many primes p ≡ 8 (mod 9)** (Euclid-type, via f(X) = X³ − 3X + 1).

  g(X) = −f(−X) = X³ − 3X − 1. For j = 9·F + 7 with B! = F + 1, n = j + 2 = 9·B! and N₀ = g(n) = j³ + 6j² + 9j + 1 ≡ 8 (mod 9).
  Every prime factor q of N₀ is odd and −n is a root of f mod q, so (Nine9b.root9) 9 | q − 1, 9 | q + 1 or q = 3; q = 3 is
  impossible (N₀ ≡ 2 mod 3). A product of primes ≡ 1 (mod 9) is ≡ 1, so N₀ has a prime factor ≡ 8; it does not divide n.
    uniq_rem, mod9, cofactor9   residues
    three_zero                  3 = 0 in 𝔽_q (q = 2m + 3) ⟹ q = 3
    allowed9                    q prime, q | N, N + (3n + 1) = n³  ⟹  q ≡ 1, q ≡ 8 (mod 9) or q = 3
    has8                        a prime factor ≡ 8 (mod 9) of every divisor N ≡ 8 of N₀ ≡ 8
    inf_8mod9                   ∀ B, ∃ prime p = 9b + 8 > B
  Lean 4 core only, proofs by hand (lib_ring, lib_nat, lib_int), no axioms.
-/
import lang.Nine9b
import lang.Fp2Sqrt
import lang.PrimeOneModQ
import lang.TwoPower
import lang.Inf7mod8

namespace Nine

open FTAChain ZMod LibInt InfSplit

theorem uniq_rem (d x y r c : Nat) (hr : r < d) (hc : c < d) (h : x * d + r = y * d + c) : r = c := by
  have big : ∀ x y r c : Nat, r < d → x < y → x * d + r = y * d + c → False := fun x y r c hr hxy h => by
    have h1 : x * d + r < (x + 1) * d := by rw [Nat.succ_mul]; exact Nat.add_lt_add_left hr _
    have h2 : (x + 1) * d ≤ y * d := Nat.mul_le_mul_right d hxy
    exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (h ▸ h1) (Nat.le_trans h2 (Nat.le_add_right _ _)))
  cases Nat.lt_or_ge x y with
  | inl hxy => exact absurd h (fun h' => big x y r c hr hxy h')
  | inr hxy =>
    cases Nat.lt_or_ge y x with
    | inl hyx => exact absurd h.symm (fun h' => big y x c r hc hyx h')
    | inr hyx =>
      have e : x = y := Nat.le_antisymm hyx hxy
      subst e
      apply Int.ofNat.inj
      have := congrArg Int.ofNat h
      have e' : Int.ofNat x * Int.ofNat d + Int.ofNat r = Int.ofNat x * Int.ofNat d + Int.ofNat c := this
      calc Int.ofNat r = (Int.ofNat x * Int.ofNat d + Int.ofNat r) - Int.ofNat x * Int.ofNat d := by lib_int
        _ = Int.ofNat c := by rw [e']; lib_int

theorem mod9 (n : Nat) : ∃ a r, n = a * 9 + r ∧ r < 9 :=
  match div_exists 8 n with
  | ⟨a, r, k, hn, hr⟩ => ⟨a, r, hn, Nat.lt_of_lt_of_le (Nat.lt_succ_self r) (by rw [show (9 : Nat) = 8 + 1 from rfl, ← hr]; exact Nat.succ_le_succ (Nat.le_add_right r k))⟩

theorem cofactor9 (b N' a : Nat) (h : (b * 9 + 1) * N' = a * 9 + 8) : ∃ c, N' = c * 9 + 8 :=
  match mod9 N' with
  | ⟨c, r, hc, hr⟩ => by
    have e : (b * 9 + 1) * N' = (b * 9 * c + b * r + c) * 9 + r := by rw [hc]; lib_nat
    rw [e] at h
    exact ⟨c, by rw [hc, uniq_rem 9 _ _ r 8 hr (Nat.lt_succ_self 8) h]⟩

theorem three_zero (m : Nat) (h : three m = (ring (2 * m + 1 + 1)).zero) : 2 * m + 1 + 1 + 1 = 3 := by
  have e : three m = mk (2 * m + 1 + 1) 3 := by
    rw [show mk (2 * m + 1 + 1) 3 = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).one) (ring (2 * m + 1 + 1)).one) (ring (2 * m + 1 + 1)).one by
      rw [show (ring (2 * m + 1 + 1)).zero = mk (2 * m + 1 + 1) 0 from rfl, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one]]
    unfold three; lib_ring
  rw [e] at h
  have h0 : md (2 * m + 1 + 1) 3 = 0 := congrArg Subtype.val h
  match m, h0 with
  | 0, _ => rfl
  | k + 1, h0 =>
    rw [md_small _ 3 (Nat.lt_of_lt_of_le (Nat.lt_succ_self 3) (by
      show 4 ≤ 2 * (k + 1) + 1 + 1 + 1
      exact Nat.le_trans (Nat.le_add_left 4 (2 * k + 1)) (Nat.le_of_eq (by lib_nat))))] at h0
    exact Nat.noConfusion h0

theorem allowed9 (n N l : Nat) (hq : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1)
    (hN : N + (n + n + n + 1) = n * n * n) (hdv : Dv (l + 1 + 1) N) :
    (∃ a, l + 1 + 1 = a * 9 + 1) ∨ (∃ a, l + 1 + 1 = a * 9 + 8) ∨ l + 1 + 1 = 3 := by
  rcases EveryPrime.nat_even_odd l with ⟨j, hj⟩ | ⟨m, hm⟩
  · exfalso
    have h2 : l + 1 + 1 = 2 := by
      cases hq 2 ⟨j + 1, by rw [hj]; lib_nat⟩ with
      | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
      | inr e => exact e.symm
    rw [h2] at hdv
    obtain ⟨r, hr⟩ := hdv
    rw [hr] at hN
    rcases EveryPrime.nat_even_odd n with ⟨i, hi⟩ | ⟨i, hi⟩
    · rw [hi] at hN
      exact odd_not_dv2 (r + 3 * i) (4 * i * i * i) (by rw [show (4 * i * i * i) * 2 = 2 * i * (2 * i) * (2 * i) by lib_nat, ← hN]; lib_nat)
    · rw [hi] at hN
      exact odd_not_dv2 (4 * i * i * i + 6 * i * i + 3 * i) (r + 3 * i + 2) (by
        rw [show 2 * (4 * i * i * i + 6 * i * i + 3 * i) + 1 = (2 * i + 1) * (2 * i + 1) * (2 * i + 1) by lib_nat, ← hN]; lib_nat)
  · subst hm
    have hx : Nine.fr m ((ring (2 * m + 1 + 1)).neg (mk (2 * m + 1 + 1) n)) = (ring (2 * m + 1 + 1)).zero := by
      have h1 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (mk (2 * m + 1 + 1) n) (mk (2 * m + 1 + 1) n)) (mk (2 * m + 1 + 1) n) = mk (2 * m + 1 + 1) (n * n * n) := by
        rw [TwoPower.mk_mul, TwoPower.mk_mul]
      have h2 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (mk (2 * m + 1 + 1) n) (mk (2 * m + 1 + 1) n)) (mk (2 * m + 1 + 1) n)) (ring (2 * m + 1 + 1)).one = mk (2 * m + 1 + 1) (n + n + n + 1) := by
        rw [PrimeOneModQ.mk_add, PrimeOneModQ.mk_add, TwoPower.mk_add_one]
      have h0 : mk (2 * m + 1 + 1) N = (ring (2 * m + 1 + 1)).zero := Subtype.ext (by
        show md (2 * m + 1 + 1) N = md (2 * m + 1 + 1) 0
        rw [dv_md_zero _ N hdv]; rfl)
      have h3 : (ring (2 * m + 1 + 1)).add (mk (2 * m + 1 + 1) N) (mk (2 * m + 1 + 1) (n + n + n + 1)) = mk (2 * m + 1 + 1) (n * n * n) := by
        rw [PrimeOneModQ.mk_add, hN]
      rw [h0, ← h1, ← h2] at h3
      unfold Nine.fr
      generalize mk (2 * m + 1 + 1) n = x at h3
      calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add x x) x) (ring (2 * m + 1 + 1)).one))) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add x x) x) (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add x x) x) (ring (2 * m + 1 + 1)).one))) := by   -- (+7 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by lib_ring
    rcases root9 m hq (Fp2Sqrt.two_ne m) _ hx with ⟨a, ha⟩ | ⟨a, ha⟩ | h3
    · exact Or.inl ⟨a, by rw [← ha]⟩
    · match a, ha with
      | 0, ha => exact absurd ha (fun h' => Nat.noConfusion h')
      | a + 1, ha => exact Or.inr (Or.inl ⟨a, Nat.succ.inj (by show 2 * m + 1 + 1 + 1 + 1 = a * 9 + 8 + 1; rw [ha, Nat.succ_mul])⟩)
    · exact Or.inr (Or.inr (three_zero m h3))

/-- **A prime factor ≡ 8 (mod 9).** -/
theorem has8 (n N0 : Nat) (hN : N0 + (n + n + n + 1) = n * n * n) (h8 : ∃ c, N0 = c * 9 + 8) :
    ∀ G N, N ≤ G → Dv N N0 → (∃ a, N = a * 9 + 8) →
    ∃ q, (∀ d, Dv d q → d = 1 ∨ d = q) ∧ (∃ b, q = b * 9 + 8) ∧ Dv q N
  | 0, N, hN', _, ⟨a, ha⟩ => absurd (ha ▸ hN') (fun h => Nat.not_succ_le_zero _ (Nat.le_trans (Nat.le_add_left _ _) h))
  | G + 1, N, hNG, hN0, ⟨a, ha⟩ => by
    match lp_exists (a * 9 + 6) with
    | ⟨l, hl, hmin⟩ =>
    have hq := lp_prime _ l hl hmin
    have hlN : Dv (l + 1 + 1) N := by rw [ha]; exact hl
    have hlN0 := dv_trans _ _ _ hlN hN0
    rcases allowed9 n N0 l hq hN hlN0 with ⟨b, hb⟩ | ⟨b, hb⟩ | h3
    · match hlN with
      | ⟨N', hN'⟩ =>
      match cofactor9 b N' a (by rw [← hb, ← ha, hN']; lib_nat) with
      | ⟨c, hc⟩ =>
      have hlt : N' ≤ G := by
        have e : N = N' + (N' * (l + 1)) := by rw [hN']; lib_nat
        have h1' : 1 ≤ N' * (l + 1) := by rw [hc]; exact Nat.mul_le_mul (Nat.le_add_left 1 _) (Nat.le_add_left 1 _)
        exact Nat.le_of_succ_le_succ (Nat.le_trans (Nat.add_le_add_left h1' N') (by rw [← e]; exact hNG))
      have hN'0 : Dv N' N0 := dv_trans _ _ _ ⟨l + 1 + 1, by rw [hN']; lib_nat⟩ hN0
      match has8 n N0 hN h8 G N' hlt hN'0 ⟨c, hc⟩ with
      | ⟨q, hq', hq8, hqN⟩ => exact ⟨q, hq', hq8, dv_trans q N' N hqN ⟨l + 1 + 1, by rw [hN']; lib_nat⟩⟩
    · exact ⟨l + 1 + 1, hq, ⟨b, hb⟩, hlN⟩
    · exfalso
      obtain ⟨c, hc⟩ := h8
      obtain ⟨r, hr⟩ := hlN0
      rw [h3] at hr
      exact absurd (uniq_rem 3 (3 * c + 2) r 2 0 (Nat.lt_succ_self 2) (Nat.zero_lt_succ 2)
        (by rw [show (3 * c + 2) * 3 + 2 = c * 9 + 8 by lib_nat, ← hc, hr]; rfl)) (fun h' => Nat.noConfusion h')

/-- **Infinitely many primes p ≡ 8 (mod 9).** -/
theorem inf_8mod9 (B : Nat) : ∃ b, (∀ d, Dv d (b * 9 + 8) → d = 1 ∨ d = b * 9 + 8) ∧ B < b * 9 + 8 := by
  match fac_pos B with
  | ⟨f, hf⟩ =>
  have hN : (9 * f + 7) * (9 * f + 7) * (9 * f + 7) + 6 * ((9 * f + 7) * (9 * f + 7)) + 9 * (9 * f + 7) + 1
      + ((9 * f + 7 + 2) + (9 * f + 7 + 2) + (9 * f + 7 + 2) + 1) = (9 * f + 7 + 2) * (9 * f + 7 + 2) * (9 * f + 7 + 2) := by
    generalize 9 * f + 7 = j; lib_nat
  have h8 : ∃ c, (9 * f + 7) * (9 * f + 7) * (9 * f + 7) + 6 * ((9 * f + 7) * (9 * f + 7)) + 9 * (9 * f + 7) + 1 = c * 9 + 8 := by
    have e1 : (9 * f + 7) * (9 * f + 7) = 9 * (9 * f * f + 14 * f + 5) + 4 := by lib_nat
    have e2 : (9 * f + 7) * (9 * f + 7) * (9 * f + 7) = 9 * (9 * (9 * f * f + 14 * f + 5) * f + 7 * (9 * f * f + 14 * f + 5) + 4 * f + 3) + 1 := by
      rw [e1]; generalize 9 * f * f + 14 * f + 5 = u; lib_nat
    rw [e2, e1]
    exact ⟨9 * (9 * f * f + 14 * f + 5) * f + 7 * (9 * f * f + 14 * f + 5) + 4 * f + 3 + 6 * (9 * f * f + 14 * f + 5) + (9 * f + 7) + 2, by
      generalize 9 * (9 * f * f + 14 * f + 5) * f + 7 * (9 * f * f + 14 * f + 5) + 4 * f + 3 = v
      generalize 9 * f * f + 14 * f + 5 = u
      generalize 9 * f + 7 = j; lib_nat⟩
  obtain ⟨N0, hN0⟩ : ∃ N0, N0 = (9 * f + 7) * (9 * f + 7) * (9 * f + 7) + 6 * ((9 * f + 7) * (9 * f + 7)) + 9 * (9 * f + 7) + 1 := ⟨_, rfl⟩
  rw [← hN0] at hN h8
  match h8 with
  | ⟨c, hc⟩ =>
  match has8 (9 * f + 7 + 2) N0 hN ⟨c, hc⟩ N0 N0 (Nat.le_refl _) (dv_refl _) ⟨c, hc⟩ with
  | ⟨q, hq, ⟨b, hb⟩, hqN⟩ =>
    refine ⟨b, by rw [← hb]; exact hq, ?_⟩
    rw [← hb]
    cases Nat.lt_or_ge B q with
    | inl h => exact h
    | inr h =>
      exfalso
      match dv_fac B q (by rw [hb]; exact Nat.le_add_left 1 _) h with
      | ⟨r, hr⟩ =>
        have hn : Dv q (9 * f + 7 + 2) := ⟨9 * r, by
          rw [show 9 * f + 7 + 2 = 9 * (f + 1) by lib_nat, ← hf, hr]; lib_nat⟩
        obtain ⟨s, hs⟩ := hn
        have hq1 : q = (b * 9 + 7) + 1 := hb
        rw [hq1] at hqN hs
        have hsum : Dv (b * 9 + 7 + 1) (N0 + ((9 * f + 7 + 2) + (9 * f + 7 + 2) + (9 * f + 7 + 2) + 1)) := by
          rw [hN, hs]; exact ⟨s * (b * 9 + 7 + 1) * s * (b * 9 + 7 + 1) * s, by generalize b * 9 + 7 + 1 = q; lib_nat⟩
        have h1 := dv_sub (b * 9 + 7) _ _ hqN hsum
        have h2 : Dv (b * 9 + 7 + 1) ((9 * f + 7 + 2) + (9 * f + 7 + 2) + (9 * f + 7 + 2)) := ⟨s + s + s, by rw [hs]; lib_nat⟩
        exact dv_one_absurd (b * 9 + 6) (dv_sub (b * 9 + 7) _ 1 h2 h1)

end Nine

#print axioms Nine.uniq_rem
#print axioms Nine.mod9
#print axioms Nine.cofactor9
#print axioms Nine.three_zero
#print axioms Nine.allowed9
#print axioms Nine.has8
#print axioms Nine.inf_8mod9

lang/NormEval.lean

lean_all/lang/NormEval.lean

/-
  lang/NormEval — T49 (general primary α, step (O1')): **the evaluations of α at the primes above p multiply to
  the evaluation of its norm**, and a norm-p element is divisible by exactly one P_c, once.
  ℓ = m + 1, V = ℤ[C_ℓ], σ_d = CycGal.gsig d, ps = list of pairs (c, d) with c·d = 1 in 𝔽_ℓ.
    nrm ps v        Π_((c,d) ∈ ps) σ_d(v)                     (the norm when ps runs over all of 𝔽_ℓ^×)
    evs ps v        the list (φ_(z^c)(v))_((c,d) ∈ ps)
    eval_norm       φ_z(nrm ps v) = Π evs ps v                 (CycGalEv.phz_gsig, CycEis.phz_mul)
    phz_nsm         φ_z(n·1) = n·1
    norm_p_one      in ℤ/p² at a root z with z^ℓ = 1: nrm ps α = p·1 ⇒ exactly one φ_(z^c)(α) is ≡ 0 (mod p)
                    (lang/ExactP2; test work/data/t49/normp_test.py)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.CycGalEv
import lang.ExactP2

namespace NormEval

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

def AllInv (m : Nat) : List (Zp m × Zp m) → Prop
  | [] => True
  | p :: ps => (ring m).mul p.1 p.2 = (ring m).one ∧ AllInv m ps

def nrm (m : Nat) (v : V Int (m + 1)) : List (Zp m × Zp m) → V Int (m + 1)
  | [] => (gring LibInt.intR m).one
  | p :: ps => (gring LibInt.intR m).mul (CycGal.gsig LibInt.intR m p.2 v) (nrm m v ps)

def evs {S : Type} (E : CRing S) (m : Nat) (z : S) (v : V Int (m + 1)) (ps : List (Zp m × Zp m)) : List S :=
  ps.map (fun p => CycEis.phz E m (pw E z p.1.val) v)

/-- **φ_z(Π σ_d v) = Π φ_(z^c)(v)**. -/
theorem eval_norm {S : Type} (E : CRing S) (m : Nat) (z : S) (hz : pw E z (m + 1) = E.one) (v : V Int (m + 1)) :
    ∀ ps, AllInv m ps → CycEis.phz E m z (nrm m v ps) = ExactOne.pr E (evs E m z v ps)
  | [], _ => CycEis.phz_one E m z
  | p :: ps, ⟨h1, h2⟩ => by
    show CycEis.phz E m z ((gring LibInt.intR m).mul (CycGal.gsig LibInt.intR m p.2 v) (nrm m v ps))
      = E.mul (CycEis.phz E m (pw E z p.1.val) v) (ExactOne.pr E (evs E m z v ps))
    rw [CycEis.phz_mul E m z hz, CycGalEv.phz_gsig E m z hz p.1 p.2 h1 v, eval_norm E m z hz v ps h2]

theorem phz_nsm {S : Type} (E : CRing S) (m : Nat) (z : S) :
    ∀ n, CycEis.phz E m z (nsm (gring LibInt.intR m) n (gring LibInt.intR m).one) = nsm E n E.one
  | 0 => CycEis.phz_zero E m z
  | n + 1 => by
    show CycEis.phz E m z ((gring LibInt.intR m).add (nsm (gring LibInt.intR m) n (gring LibInt.intR m).one)
        (gring LibInt.intR m).one) = E.add (nsm E n E.one) E.one
    rw [CycEis.phz_add, phz_nsm E m z n, CycEis.phz_one]

/-- **a norm-p element is divisible by exactly one P_c**: in ℤ/p² (M + 1 = p², p = J + 2), z^ℓ = 1,
  nrm ps α = p·1 ⇒ exactly one evaluation φ_(z^c)(α), (c, d) ∈ ps, reduces to 0 mod p. -/
theorem norm_p_one (J M : Nat) (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1)
    (hM : M + 1 = (J + 1 + 1) * (J + 1 + 1)) (m : Nat) (z : Zp M) (hz : pw (ring M) z (m + 1) = (ring M).one)
    (α : V Int (m + 1)) (ps : List (Zp m × Zp m)) (hps : AllInv m ps)
    (hN : nrm m α ps = nsm (gring LibInt.intR m) (J + 1 + 1) (gring LibInt.intR m).one) :
    ExactOne.cnt (ExactP2.d J M) (evs (ring M) m z α ps) = 1 := by
  apply ExactP2.exact_one_p2 J M hpr hM
  rw [← eval_norm (ring M) m z hz α ps hps, hN, phz_nsm]
  rfl

end NormEval

#print axioms NormEval.eval_norm
#print axioms NormEval.phz_nsm
#print axioms NormEval.norm_p_one

lang/NormHyp.lean

lean_all/lang/NormHyp.lean

/-
  lang/NormHyp — T49 (O3') norm side, B: **N(α) = p·1 ⇒ (B·B̄)(b) = p^k·[b = 0]** for B the paired Stickelberger
  product (BBNorm.bb_norm, NsmCoef). This is hypothesis hB of WWbar.ww (with sB = 0).
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.BBNorm
import lang.NsmCoef

namespace NormHyp

open FTAChain ZMod YMChain FiniteRecChain DetChain GroupRing

theorem norm_hyp (m : Nat) (α : V Int (m + 1)) (iv : Zp m → Zp m) (k p : Nat) (qs : List (Zp m × Nat × Nat))
    (hs : StickBB.Sums m k qs)
    (hN : NormEval.nrm m α (PairNorm.flat m (BBNorm.conv m iv qs))
      = Frobenius.nsm (gring LibInt.intR m) p (gring LibInt.intR m).one) (b : Zp m) :
    gt LibInt.intR m (gmul LibInt.intR m (AbsPair.Bp (gring LibInt.intR m) (StickBB.ents m α qs))
        (StickBB.J m (AbsPair.Bp (gring LibInt.intR m) (StickBB.ents m α qs)))) b
      = LibRing.pw LibInt.intR (Int.ofNat p) k * delta LibInt.intR b (ring m).zero := by
  have h := BBNorm.bb_norm m α iv k qs hs
  rw [hN] at h
  show gt LibInt.intR m ((gring LibInt.intR m).mul _ _) b = _
  rw [h]
  exact NsmCoef.gt_npw m (Int.ofNat p) _ (fun y => NsmCoef.gt_nsm m p y) k b

end NormHyp

#print axioms NormHyp.norm_hyp

lang/NormPk.lean

lean_all/lang/NormPk.lean

/-
  lang/NormPk — T49 (O3') (v3): **exactly one factor divisible by p in ℤ/p^n** (M + 1 = p·k', (p·1)^n = 0, p < M + 1).
    red_P_k        red(p·1) = 0 in 𝔽_p
    red_neg_k      red(−v) = −red(v)
    P_nunit_k      p·1 is not a unit in ℤ/M
    one_sub_unit   1 − p·t is a unit in ℤ/M                      (UnitPk.unit_pk)
    exact_one_pk   Π xs = p·1 ⇒ exactly one x ∈ xs has red(x) = 0  (ExactOneK)
    flagged_unit   for Π (x :: xs) = p·1 with red(x) = 0: x = p·U, U a unit
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.ExactOneK
import lang.UnitPk

namespace NormPk

open FTAChain ZMod YMChain FiniteRecChain Frobenius

variable (J M k' n : Nat) (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1)
  (hk : M + 1 = (J + 1 + 1) * k') (hPn : pw (ring M) (ExactP2.P J M) n = (ring M).zero)

include hk in
theorem red_P_k : RedP2.red J M (ExactP2.P J M) = (ring (J + 1)).zero := Subtype.ext (by
  show md (J + 1) (nsm (ring M) (J + 1 + 1) (ring M).one).val = md (J + 1) 0
  rw [nsm_one_val, RedP2.mdmd J M k' hk (J + 1 + 1), md_self]
  rfl)

include hk in
theorem red_neg_k (v : Zp M) : RedP2.red J M ((ring M).neg v) = (ring (J + 1)).neg (RedP2.red J M v) := by
  have h0 := RedP2.red_add J M k' hk v ((ring M).neg v)
  rw [show (ring M).add v ((ring M).neg v) = (ring M).zero by lib_ring, RedP2.red_zero J M k' hk] at h0
  calc RedP2.red J M ((ring M).neg v)
      = (ring (J + 1)).add (ring (J + 1)).zero ((ring (J + 1)).neg (RedP2.red J M v)) := by rw [h0]; lib_ring
    _ = _ := by lib_ring

include hk in
theorem P_nunit_k (w : Zp M) : (ring M).mul (ExactP2.P J M) w ≠ (ring M).one := fun h => by
  have h1 := congrArg (RedP2.red J M) h
  rw [RedP2.red_mul J M k' hk, red_P_k J M k' hk, RedP2.red_one J M k' hk] at h1
  exact PowerSum.one_ne_zero' J (h1.symm.trans
    (show (ring (J + 1)).mul (ring (J + 1)).zero (RedP2.red J M w) = (ring (J + 1)).zero by lib_ring))

include hpr hk hPn in
theorem one_sub_unit (t : Zp M) :
    ∃ v, (ring M).mul ((ring M).add (ring M).one ((ring M).neg ((ring M).mul (ExactP2.P J M) t))) v = (ring M).one := by
  apply UnitPk.unit_pk J M k' n hpr hk hPn
  rw [RedP2.red_add J M k' hk, red_neg_k J M k' hk, RedP2.red_mul J M k' hk, red_P_k J M k' hk, RedP2.red_one J M k' hk]
  intro h
  exact PowerSum.one_ne_zero' J ((show (ring (J + 1)).one = (ring (J + 1)).add (ring (J + 1)).one
    ((ring (J + 1)).neg ((ring (J + 1)).mul (ring (J + 1)).zero (RedP2.red J M t))) by lib_ring).trans h)

include hpr hk hPn in
/-- **exactly one factor is divisible by p** when the product in ℤ/p^n is p. -/
theorem exact_one_pk (hlt : J + 1 + 1 < M + 1) (xs : List (Zp M)) (hx : ExactOne.pr (ring M) xs = ExactP2.P J M) :
    ExactOne.cnt (ExactP2.d J M) xs = 1 :=
  ExactOneK.exact_one_k (ring M) (ExactP2.P J M) (ExactP2.d J M)
    (fun x h => RedP2.red_ker J M x (of_decide_eq_true h))
    (fun x h => UnitPk.unit_pk J M k' n hpr hk hPn x (of_decide_eq_false h))
    (one_sub_unit J M k' n hpr hk hPn) (StickVal.small_ne_zero M (J + 1) hlt) (P_nunit_k J M k' hk) xs hx

include hpr hk hPn in
/-- **the factor divisible by p is p times a unit** when the product is p. -/
theorem flagged_unit (hlt : J + 1 + 1 < M + 1) (x : Zp M) (xs : List (Zp M))
    (hx : ExactOne.pr (ring M) (x :: xs) = ExactP2.P J M) (h0 : RedP2.red J M x = (ring (J + 1)).zero) :
    ∃ U w, x = (ring M).mul (ExactP2.P J M) U ∧ (ring M).mul U w = (ring M).one :=
  ExactOneK.others_unit (ring M) (ExactP2.P J M) (ExactP2.d J M)
    (fun x h => UnitPk.unit_pk J M k' n hpr hk hPn x (of_decide_eq_false h)) x xs hx (decide_eq_true h0)
    (exact_one_pk J M k' n hpr hk hPn hlt (x :: xs) hx)

end NormPk

#print axioms NormPk.red_P_k
#print axioms NormPk.red_neg_k
#print axioms NormPk.P_nunit_k
#print axioms NormPk.one_sub_unit
#print axioms NormPk.exact_one_pk
#print axioms NormPk.flagged_unit

lang/NpPow.lean

lean_all/lang/NpPow.lean

/-
  lang/NpPow — T49 (O3') (ii'): **Π_i (x_i y_i)^k = (Π_i x_i y_i)^k**, so B·σ_(−1)(B) (lang/StickBB) is the k-th power
  of the product of the pairs σ_d α · σ_(−d) α, i.e. N(α)^k when the pairs run over all of 𝔽_ℓ^× / ±1.
    Pp       Π_i x_i y_i
    np_pow   AbsPair.Np k l = (Pp l)^k
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.AbsPair

namespace NpPow

open Frobenius

variable {R : Type} (K : DetChain.CRing R)

def Pp : List (R × R × Nat × Nat) → R
  | [] => K.one
  | e :: es => K.mul (K.mul e.1 e.2.1) (Pp es)

theorem np_pow (k : Nat) : ∀ l, AbsPair.Np K k l = pw K (Pp K l) k
  | [] => by
    show K.one = pw K K.one k
    induction k with
    | zero => rfl
    | succ k ih =>
      show K.one = K.mul (pw K K.one k) K.one
      rw [← ih]; lib_ring
  | e :: es => by
    show K.mul (pw K (K.mul e.1 e.2.1) k) (AbsPair.Np K k es) = pw K (K.mul (K.mul e.1 e.2.1) (Pp K es)) k
    rw [np_pow k es, pw_mul K (K.mul e.1 e.2.1) (Pp K es) k, pw_mul K e.1 e.2.1 k]

end NpPow

#print axioms NpPow.np_pow

lang/NsmCoef.lean

lean_all/lang/NsmCoef.lean

/-
  lang/NsmCoef — T49 (O3') norm side: **coefficients of n·1 and (n·1)^k in ℤ[C_ℓ]**.
    gt_nsm    (n·1)(b) = n·[b = 0]
    gt_npw    ((n·1)^k)(b) = n^k·[b = 0]      (written with LibRing.pw on Int)
  So B B̄ = N(α)^(ℓ−2) with N(α) = p·1 has coefficients p^(ℓ−2)·[b = 0] — the form WWbar.ww needs.
  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.WWbar

namespace NsmCoef

open FTAChain ZMod YMChain FiniteRecChain DetChain GroupRing

variable (m : Nat)

theorem gt_nsm : ∀ n (b : Zp m), gt LibInt.intR m (Frobenius.nsm (gring LibInt.intR m) n (gring LibInt.intR m).one) b
    = Int.ofNat n * delta LibInt.intR b (ring m).zero
  | 0, b => by
    show gt LibInt.intR m (gzero LibInt.intR m) b = _
    unfold gzero; rw [gt_tb]
    show (0 : Int) = 0 * delta LibInt.intR b (ring m).zero
    lib_int
  | n + 1, b => by
    show gt LibInt.intR m (gadd LibInt.intR m (Frobenius.nsm (gring LibInt.intR m) n (gring LibInt.intR m).one)
      (gone LibInt.intR m)) b = _
    unfold gadd; rw [gt_tb, gt_nsm n b, WWbar.gt_one]
    show Int.ofNat n * delta LibInt.intR b (ring m).zero + delta LibInt.intR b (ring m).zero
      = Int.ofNat (n + 1) * delta LibInt.intR b (ring m).zero
    rw [show Int.ofNat (n + 1) = Int.ofNat n + 1 from rfl]
    lib_int

theorem gt_npw (c : Int)
    (x : V Int (m + 1)) (hx : ∀ b, gt LibInt.intR m x b = c * delta LibInt.intR b (ring m).zero) :
    ∀ k (b : Zp m), gt LibInt.intR m (Frobenius.pw (gring LibInt.intR m) x k) b
      = LibRing.pw LibInt.intR c k * delta LibInt.intR b (ring m).zero
  | 0, b => by
    show gt LibInt.intR m (gone LibInt.intR m) b = LibRing.pw LibInt.intR c 0 * delta LibInt.intR b (ring m).zero
    rw [WWbar.gt_one]
    show delta LibInt.intR b (ring m).zero = 1 * delta LibInt.intR b (ring m).zero
    lib_int
  | k + 1, b => by
    show gt LibInt.intR m (gmul LibInt.intR m (Frobenius.pw (gring LibInt.intR m) x k) x) b = _
    have hx' : x = ModSq.sc m c (gone LibInt.intR m) :=
      ext LibInt.intR m _ _ (fun y => by unfold ModSq.sc; rw [gt_tb, hx y, WWbar.gt_one])
    rw [hx', GroupRing.gm_comm, WWbar.sc_mul_l, gm_one, ← hx', gt_npw c x hx k b]
    show c * (LibRing.pw LibInt.intR c k * delta LibInt.intR b (ring m).zero)
      = (LibRing.pw LibInt.intR c k * c) * delta LibInt.intR b (ring m).zero
    generalize LibRing.pw LibInt.intR c k = P
    generalize delta LibInt.intR b (ring m).zero = D
    lib_int

end NsmCoef

#print axioms NsmCoef.gt_nsm
#print axioms NsmCoef.gt_npw

lang/NthCount.lean

lean_all/lang/NthCount.lean

/-
  lang/NthCount — T49, cycle 582: **exactly (p − 1)/n nonzero n-th powers mod p** (n | p − 1): with p − 1 = n·k,
    #{a ∈ 𝔽_p : a^k = 1} = k,
  i.e. (NthPow.nth_crit) the n-th powers form a subgroup of index n, the kernel of the n-th power residue symbol.

    rs, rs_add, rs_one       finite sums Σ_{s<L} h(s) and their splitting
    pows_rs                  Σ over DLog.pows is a range sum of g^s
    block                    a^k = 1 for a = g^(nt + r), r < n  ⇔  r = 0
    count_nth
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.NthPow
import lang.LucMul
import lang.ChebAll

namespace NthPow

open FTAChain ZMod OrdMul YMChain FiniteRecChain

def rs (h : Nat → Int) : Nat → Int
  | 0 => 0
  | L + 1 => h L + rs h L

theorem rs_add (h : Nat → Int) (a : Nat) : ∀ b, rs h (a + b) = rs (fun s => h (a + s)) b + rs h a
  | 0 => by show rs h a = 0 + rs h a; lib_int
  | b + 1 => by show h (a + b) + rs h (a + b) = (h (a + b) + rs (fun s => h (a + s)) b) + rs h a; rw [rs_add h a b]; lib_int

theorem rs_one : ∀ n, rs (fun r => if r = 0 then 1 else 0) (n + 1) = 1
  | 0 => rfl
  | n + 1 => by
    show (if n + 1 = 0 then (1 : Int) else 0) + rs (fun r => if r = 0 then 1 else 0) (n + 1) = 1
    rw [rs_one n, ite_neg' _ _ (fun e => Nat.noConfusion e)]; lib_int

variable (J : Nat) (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1)

def F1 (k : Nat) (a : Zp (J + 1)) : Int := if Frobenius.pw (ring (J + 1)) a k = (ring (J + 1)).one then 1 else 0

theorem pows_rs (f : Zp (J + 1) → Int) (g : Zp (J + 1)) : ∀ n, sumK LibInt.intR f (DLog.pows J g n) = rs (fun s => f (Frobenius.pw (ring (J + 1)) g s)) n
  | 0 => rfl
  | n + 1 => by
    show f (Frobenius.pw (ring (J + 1)) g n) + sumK LibInt.intR f (DLog.pows J g n) = _
    rw [pows_rs f g n]; rfl

theorem block (n k : Nat) (hnk : J + 1 = n * k) (g : Zp (J + 1)) (hg : Ord (ring (J + 1)) g (J + 1)) (t r : Nat) (hr : r < n) :
    F1 J k (Frobenius.pw (ring (J + 1)) g (n * t + r)) = if r = 0 then 1 else 0 := by
  obtain ⟨⟨n', hn'⟩, ⟨k', hk'⟩⟩ := pos_of_mul J n k hnk
  have e : Frobenius.pw (ring (J + 1)) (Frobenius.pw (ring (J + 1)) g (n * t + r)) k = Frobenius.pw (ring (J + 1)) g (r * k) := by
    have one_pw : ∀ t, Frobenius.pw (ring (J + 1)) (ring (J + 1)).one t = (ring (J + 1)).one := by
      intro t; induction t with
      | zero => rfl
      | succ t ih => show (ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) (ring (J + 1)).one t) (ring (J + 1)).one = _; rw [ih]; exact (ring (J + 1)).one_mul _
    rw [TwoPower.pwpow, show (n * t + r) * k = (n * k) * t + r * k by lib_nat, HasseCM.pw_add', ← TwoPower.pwpow, ← hnk, hg.1, one_pw]
    exact (ring (J + 1)).one_mul _
  unfold F1
  rw [e]
  cases r with
  | zero => rw [ite_pos' _ _ (show Frobenius.pw (ring (J + 1)) g (0 * k) = (ring (J + 1)).one by rw [Nat.zero_mul]; rfl), ite_pos' _ _ rfl]
  | succ r =>
    rw [ite_neg' _ _ (fun e => Nat.noConfusion e)]
    apply ite_neg'
    intro h1
    obtain ⟨c, hc⟩ := hg.2 _ h1
    obtain ⟨d, hd⟩ := Cheb.le_dest2 (r + 1 + 1) n hr
    rw [hnk] at hc
    cases c with
    | zero =>
      have h00 : (r + 1) * k = 0 := hc.trans (Nat.mul_zero _)
      rw [hk'] at h00
      exact Nat.noConfusion ((show (r + 1) * (k' + 1) = (r * (k' + 1) + k') + 1 by lib_nat).symm.trans h00)
    | succ c =>
      have this : (r + 1) * k + 0 = (r + 1) * k + ((d + 1) * k + n * k * c) := by
        calc (r + 1) * k + 0 = n * k * (c + 1) := by rw [Nat.add_zero, hc]
          _ = _ := by rw [← hd]; lib_nat
      have h0 : 0 = (d + 1) * k + n * k * c := Cheb.add_rc ((r + 1) * k) 0 _ ((Nat.add_comm 0 _).trans (this.trans (Nat.add_comm _ _)))
      rw [hk'] at h0
      exact Nat.noConfusion (h0.trans (by lib_nat : (d + 1) * (k' + 1) + n * (k' + 1) * c = (d * (k' + 1) + k' + n * (k' + 1) * c) + 1))

theorem rs_blocks (n : Nat) (h : Nat → Int) (hb : ∀ t r, r < n + 1 → h ((n + 1) * t + r) = if r = 0 then 1 else 0) :
    ∀ t, rs h ((n + 1) * t) = Int.ofNat t
  | 0 => rfl
  | t + 1 => by
    rw [show (n + 1) * (t + 1) = (n + 1) * t + (n + 1) by lib_nat, rs_add, rs_blocks n h hb t]
    have : rs (fun s => h ((n + 1) * t + s)) (n + 1) = rs (fun r => if r = 0 then 1 else 0) (n + 1) := by
      have hh : ∀ L, L ≤ n + 1 → rs (fun s => h ((n + 1) * t + s)) L = rs (fun r => if r = 0 then 1 else 0) L := by
        intro L; induction L with
        | zero => intro _; rfl
        | succ L ih =>
          intro hL
          show h ((n + 1) * t + L) + rs _ L = (if L = 0 then (1 : Int) else 0) + rs _ L
          rw [hb t L hL, ih (Nat.le_of_succ_le hL)]
      exact hh (n + 1) (Nat.le_refl _)
    rw [this, rs_one]; show 1 + Int.ofNat t = Int.ofNat t + 1; lib_int

include hpr in
/-- **#{a : a^k = 1} = k** for p − 1 = n·k. -/
theorem count_nth (n k : Nat) (hnk : J + 1 = n * k) :
    sumK LibInt.intR (F1 J k) (CMInert.allZ (J + 1) (J + 1 + 1)) = Int.ofNat k := by
  obtain ⟨⟨n', hn'⟩, ⟨k', hk'⟩⟩ := pos_of_mul J n k hnk
  obtain ⟨g, hg⟩ := PrimRoot.primitive_root J hpr
  rw [DLog.sum_split J LibInt.intR hpr g hg, pows_rs]
  have h0 : F1 J k (ring (J + 1)).zero = 0 := by
    unfold F1; apply ite_neg'; rw [hk', HasseCM.pw_zero_succ]; exact fun e => PowerSum.one_ne_zero' J e.symm
  subst hn'
  rw [h0, congrArg (rs (fun s => F1 J k (Frobenius.pw (ring (J + 1)) g s))) hnk,
    rs_blocks n' _ (fun t r hr => block J (n' + 1) k hnk g hg t r hr) k]
  show 0 + Int.ofNat k = Int.ofNat k; lib_int

end NthPow

#print axioms NthPow.rs_add
#print axioms NthPow.rs_one
#print axioms NthPow.pows_rs
#print axioms NthPow.block
#print axioms NthPow.rs_blocks
#print axioms NthPow.count_nth

lang/NthEis.lean

lean_all/lang/NthEis.lean

/-
  lang/NthEis — T49 (general ℓ, Eisenstein's relation without p): the general form of Quint19.
  In a ring E of characteristic q with a ring map ι : 𝔽_q → E, p ≢ 0 (mod q) gives p^(q−1) = 1 (Fermat), so when
  (q − 1) | m the factor p^m in the n-th power residue criterion (NthRecipPow) disappears:
    ψ(q^(−e)) = A^m,   A = ∏_(i ≤ k) J(ψ^i, ψ)·ψ(ι(−1)),   q^e = n m + 1,
  i.e. χ_p(q)^(−e) ≡ A^((q^e − 1)/n) (mod q) for the element A that is primary by lang/PrimA.
    fermat_unit   x ≠ 0 in 𝔽_q ⇒ x^(q−1) = 1
    iota_nsm      a ring map sends n·1 to n·1
    pp_nsm        P = Σ_(x ∈ 𝔽_p) 1 = p·1
    pp_pow        P^((q−1)t) = 1 in E
    nth_eis       ψ(q^(−e)) = A^m when m = (q − 1)t
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.NthRecipPow
import lang.TeichChar3
import lib.RingTac

namespace NthEis

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob CharSumGen

/-- **Fermat with cancellation**: x ≠ 0 in 𝔽_q (q = k' + 2 prime) ⇒ x^(q−1) = 1. -/
theorem fermat_unit (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (x : Zp (k' + 1))
    (hx : x ≠ (ring (k' + 1)).zero) : pw (ring (k' + 1)) x (k' + 1) = (ring (k' + 1)).one := by
  have hf : (ring (k' + 1)).mul (pw (ring (k' + 1)) x (k' + 1)) x = x := ZMod.fermat k' hq x
  have hz : (ring (k' + 1)).mul ((ring (k' + 1)).add (pw (ring (k' + 1)) x (k' + 1)) ((ring (k' + 1)).neg (ring (k' + 1)).one)) x
      = (ring (k' + 1)).zero := by
    calc (ring (k' + 1)).mul ((ring (k' + 1)).add (pw (ring (k' + 1)) x (k' + 1)) ((ring (k' + 1)).neg (ring (k' + 1)).one)) x
        = (ring (k' + 1)).add ((ring (k' + 1)).mul (pw (ring (k' + 1)) x (k' + 1)) x) ((ring (k' + 1)).neg x) := by lib_ring
      _ = (ring (k' + 1)).add x ((ring (k' + 1)).neg x) := by rw [hf]
      _ = (ring (k' + 1)).zero := by lib_ring
  cases ZMod.nozd k' hq _ _ hz with
  | inl h =>
    calc pw (ring (k' + 1)) x (k' + 1)
        = (ring (k' + 1)).add ((ring (k' + 1)).add (pw (ring (k' + 1)) x (k' + 1)) ((ring (k' + 1)).neg (ring (k' + 1)).one))
            (ring (k' + 1)).one := by lib_ring
      _ = (ring (k' + 1)).add (ring (k' + 1)).zero (ring (k' + 1)).one := by rw [h]
      _ = (ring (k' + 1)).one := by lib_ring
  | inr h => exact absurd h hx

section Hom
variable {R S : Type} (T : CRing R) (E : CRing S) (ι : R → S)

/-- a ring map sends n·1 to n·1. -/
theorem iota_nsm (hadd : ∀ a b, ι (T.add a b) = E.add (ι a) (ι b)) (hone : ι T.one = E.one) (hzero : ι T.zero = E.zero) :
    ∀ n, ι (nsm T n T.one) = nsm E n E.one
  | 0 => hzero
  | n + 1 => by
    show ι (T.add (nsm T n T.one) T.one) = E.add (nsm E n E.one) E.one
    rw [hadd, hone, iota_nsm hadd hone hzero n]

/-- a ring map commutes with powers. -/
theorem iota_pw (hmul : ∀ a b, ι (T.mul a b) = E.mul (ι a) (ι b)) (hone : ι T.one = E.one) (x : R) :
    ∀ k, ι (pw T x k) = pw E (ι x) k
  | 0 => hone
  | k + 1 => by
    show ι (T.mul (pw T x k) x) = E.mul (pw E (ι x) k) (ι x)
    rw [hmul, iota_pw hmul hone x k]
end Hom

theorem pw_one {S : Type} (E : CRing S) : ∀ t, pw E E.one t = E.one
  | 0 => rfl
  | t + 1 => by
    show E.mul (pw E E.one t) E.one = E.one
    rw [pw_one E t]
    lib_ring

/-- P = Σ_(x ∈ 𝔽_p) 1 = p·1. -/
theorem pp_nsm {S : Type} (E : CRing S) (j : Nat) : Pp E j = nsm E (j + 1 + 1 + 1) E.one :=
  TeichChar.sum_one_allZ E (j + 1 + 1) (j + 1 + 1 + 1)

/-- **P^((q−1)t) = 1** in E when p ≢ 0 (mod q). -/
theorem pp_pow {S : Type} (E : CRing S) (j k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1)
    (ι : Zp (k' + 1) → S) (hadd : ∀ a b, ι ((ring (k' + 1)).add a b) = E.add (ι a) (ι b))
    (hmul : ∀ a b, ι ((ring (k' + 1)).mul a b) = E.mul (ι a) (ι b)) (hone : ι (ring (k' + 1)).one = E.one)
    (hzero : ι (ring (k' + 1)).zero = E.zero)
    (hp : nsm (ring (k' + 1)) (j + 1 + 1 + 1) (ring (k' + 1)).one ≠ (ring (k' + 1)).zero) (t : Nat) :
    pw E (Pp E j) ((k' + 1) * t) = E.one := by
  rw [pp_nsm E j, ← iota_nsm (ring (k' + 1)) E ι hadd hone hzero (j + 1 + 1 + 1), ← TwoPower.pwpow,
    ← iota_pw (ring (k' + 1)) E ι hmul hone _ (k' + 1), fermat_unit k' hq _ hp, hone]
  exact pw_one E t

/-- **Eisenstein's relation for every n**: if q^e = n m + 1 with (q − 1) | m and p ≢ 0 (mod q), then
    ψ(q^(−e)) = A^m, A = ∏_(i ≤ k) J(ψ^i, ψ)·ψ(ι(−1)) (n = k + 2). -/
theorem nth_eis {S : Type} (E : CRing S) (j : Nat) (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (ψ : Zp (j + 1 + 1) → S)
    (hm : ∀ x y, ψ ((ring (j + 1 + 1)).mul x y) = E.mul (ψ x) (ψ y)) (h1 : ψ (ring (j + 1 + 1)).one = E.one)
    (h0 : ψ (ring (j + 1 + 1)).zero = E.zero) (hs : sumK E ψ (LL j) = E.zero) (k : Nat)
    (hnt : ∀ i, i < k → sumK E (fun a => pw E (ψ a) (i + 2)) (LL j) = E.zero) (hinv : ∀ x, pw E (ψ x) (k + 1) = ψ (iv j x))
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (hE : nsm E (k' + 1 + 1) E.one = E.zero) (e : Nat)
    (hfix : ∀ a, pw E (ψ a) ((k' + 1 + 1) ^ e) = ψ a)
    (hQ : nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (m : Nat) (hqn : (k' + 1 + 1) ^ e = (k + 2) * m + 1)
    (ι : Zp (k' + 1) → S) (hadd : ∀ a b, ι ((ring (k' + 1)).add a b) = E.add (ι a) (ι b))
    (hmul : ∀ a b, ι ((ring (k' + 1)).mul a b) = E.mul (ι a) (ι b)) (hone : ι (ring (k' + 1)).one = E.one)
    (hzero : ι (ring (k' + 1)).zero = E.zero)
    (hp : nsm (ring (k' + 1)) (j + 1 + 1 + 1) (ring (k' + 1)).one ≠ (ring (k' + 1)).zero) (t : Nat) (ht : m = (k' + 1) * t) :
    ψ (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one) (j + 1)) =
      pw E (E.mul (jpr E j ψ k) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) m := by
  rw [CharSumGen.nth_recip_pow E j hpr ψ hm h1 h0 hs k hnt hinv k' hq hE e hfix hQ m hqn, Frobenius.pw_mul, ht,
    pp_pow E j k' hq ι hadd hmul hone hzero hp t]
  lib_ring

end NthEis

#print axioms NthEis.fermat_unit
#print axioms NthEis.iota_nsm
#print axioms NthEis.iota_pw
#print axioms NthEis.pw_one
#print axioms NthEis.pp_nsm
#print axioms NthEis.pp_pow
#print axioms NthEis.nth_eis

lang/NthPow.lean

lean_all/lang/NthPow.lean

/-
  lang/NthPow — T49, cycle 581: **n-th power residues mod p for every n | p − 1** (Euler's criterion in general).
  p = J + 2 prime, p − 1 = n·k:
    nth_crit      a ≠ 0:  a is an n-th power  ⇔  a^k = 1
    sym_root      the n-th power residue symbol a^k is an n-th root of unity: (a^k)^n = 1
    sym_mul       (ab)^k = a^k b^k
  (The general statement behind the quadratic, cubic and quartic characters used so far.)
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.DLog

namespace NthPow

open FTAChain ZMod OrdMul

variable (J : Nat) (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1)

theorem pos_of_mul (n k : Nat) (h : J + 1 = n * k) : (∃ n', n = n' + 1) ∧ (∃ k', k = k' + 1) := by
  constructor
  · cases n with
    | zero => exact absurd (h.trans (Nat.zero_mul k)) (fun e => Nat.noConfusion e)
    | succ n' => exact ⟨n', rfl⟩
  · cases k with
    | zero => exact absurd (h.trans (Nat.mul_zero n)) (fun e => Nat.noConfusion e)
    | succ k' => exact ⟨k', rfl⟩

include hpr in
/-- **Euler's criterion for n-th powers.** -/
theorem nth_crit (n k : Nat) (hnk : J + 1 = n * k) (a : Zp (J + 1)) (ha : a ≠ (ring (J + 1)).zero) :
    (∃ x, Frobenius.pw (ring (J + 1)) x n = a) ↔ Frobenius.pw (ring (J + 1)) a k = (ring (J + 1)).one := by
  obtain ⟨⟨n', hn'⟩, ⟨k', hk'⟩⟩ := pos_of_mul J n k hnk
  constructor
  · intro ⟨x, hx⟩
    have hx0 : x ≠ (ring (J + 1)).zero := fun e => by
      apply ha; rw [← hx, e, hn']
      show (ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) _ n') (ring (J + 1)).zero = _
      exact DetChain.mul_zero _ _
    rw [← hx, TwoPower.pwpow, ← hnk]
    exact unit_pow J hpr x hx0
  · intro hak
    obtain ⟨g, hg⟩ := PrimRoot.primitive_root J hpr
    obtain ⟨s, _, hs⟩ := DLog.mem_pows J g a (J + 1) (DLog.every_unit J hpr g hg a ha)
    have h1 : Frobenius.pw (ring (J + 1)) g (s * k) = (ring (J + 1)).one := by rw [← TwoPower.pwpow, ← hs]; exact hak
    obtain ⟨c, hc⟩ := hg.2 _ h1
    -- s·k = (n·k)·c ⇒ s = n·c
    have hsc : s = n * c := by
      rw [hnk] at hc
      apply Nat.eq_of_mul_eq_mul_left (show 0 < k by rw [hk']; exact Nat.zero_lt_succ _)
      exact (show k * s = s * k by lib_nat).trans (hc.trans (by lib_nat))
    refine ⟨Frobenius.pw (ring (J + 1)) g c, ?_⟩
    rw [TwoPower.pwpow, hs, hsc, Nat.mul_comm]

include hpr in
theorem sym_root (n k : Nat) (hnk : J + 1 = n * k) (a : Zp (J + 1)) (ha : a ≠ (ring (J + 1)).zero) :
    Frobenius.pw (ring (J + 1)) (Frobenius.pw (ring (J + 1)) a k) n = (ring (J + 1)).one := by
  rw [TwoPower.pwpow, Nat.mul_comm, ← hnk]; exact unit_pow J hpr a ha

theorem sym_mul (k : Nat) (a b : Zp (J + 1)) :
    Frobenius.pw (ring (J + 1)) ((ring (J + 1)).mul a b) k = (ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) a k) (Frobenius.pw (ring (J + 1)) b k) :=
  Frobenius.pw_mul _ a b k

end NthPow

#print axioms NthPow.pos_of_mul
#print axioms NthPow.nth_crit
#print axioms NthPow.sym_root
#print axioms NthPow.sym_mul

lang/NthRecip.lean

lean_all/lang/NthRecip.lean

/-
  lang/NthRecip — T49, cycle 607: **the n-th power residue criterion for every n** (Gauss sums in E[C_p]):
  p = j + 3 prime, ψ : 𝔽_p → E multiplicative of order n = k + 2 (ψ(1) = 1, ψ(0) = 0, Σψ^i = 0 for 1 ≤ i ≤ n − 1, ψ^(k+1) = ψ∘ι),
  q prime with q = n·m + 1, E of characteristic q, ψ^q = ψ. Then
    ψ(q⁻¹) = (A·p)^m,   A = (∏_{i≤k} J(ψ^i, ψ))·ψ(ι(−1)),
  so q is an n-th power residue mod p (ψ(q) = 1 for a faithful ψ) iff (A·p)^((q−1)/n) = 1 in E — the cubic and quartic criteria
  (ResidueCriterion) for an arbitrary order n.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.GaussCritN

namespace CharSumGen

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

variable {S : Type} (E : CRing S) (j : Nat)

theorem mono0_one : mono E (j + 1 + 1) (ring (j + 1 + 1)).zero E.one = gone E (j + 1 + 1) :=
  ext E (j + 1 + 1) _ _ (fun c => by rw [gt_mono]; unfold gone; rw [gt_tb]; exact E.one_mul _)

theorem mono0_mul (s t : S) : gmul E (j + 1 + 1) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero s) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero t) =
    mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (E.mul s t) := by
  rw [mono_mul, show (ring (j + 1 + 1)).add (ring (j + 1 + 1)).zero (ring (j + 1 + 1)).zero = (ring (j + 1 + 1)).zero by lib_ring]

/-- (mono0 A · X)^t · G = mono0 ((A p)^t) · G when X G = mono0 p · G. -/
theorem powXG (A : S) (G : V S (j + 1 + 1 + 1))
    (hXG : gmul E (j + 1 + 1) (Xv E j) G = gmul E (j + 1 + 1) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (Pp E j)) G) : ∀ t,
    (gring E (j + 1 + 1)).mul (pw (gring E (j + 1 + 1)) ((gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero A) (Xv E j)) t) G =
      (gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (pw E (E.mul A (Pp E j)) t)) G
  | 0 => by
    show (gring E (j + 1 + 1)).mul (gring E (j + 1 + 1)).one G = (gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero E.one) G
    rw [mono0_one]; rfl
  | t + 1 => by
    have ih := powXG A G hXG t
    have hXG' : (gring E (j + 1 + 1)).mul (Xv E j) G = (gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (Pp E j)) G := hXG
    show (gring E (j + 1 + 1)).mul ((gring E (j + 1 + 1)).mul (pw (gring E (j + 1 + 1)) ((gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero A) (Xv E j)) t)
      ((gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero A) (Xv E j))) G = _
    generalize pw (gring E (j + 1 + 1)) ((gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero A) (Xv E j)) t = P at ih
    calc _ = (gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero A) ((gring E (j + 1 + 1)).mul P ((gring E (j + 1 + 1)).mul (Xv E j) G)) := by lib_ring
      _ = (gring E (j + 1 + 1)).mul ((gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero A) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (Pp E j)))
            ((gring E (j + 1 + 1)).mul P G) := by rw [hXG']; lib_ring
      _ = (gring E (j + 1 + 1)).mul ((gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero A) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (Pp E j)))
            ((gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (pw E (E.mul A (Pp E j)) t)) G) := by rw [ih]
      _ = (gring E (j + 1 + 1)).mul ((gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (pw E (E.mul A (Pp E j)) t))
            ((gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero A) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (Pp E j)))) G := by lib_ring
      _ = _ := by
        show gmul E (j + 1 + 1) (gmul E (j + 1 + 1) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (pw E (E.mul A (Pp E j)) t))
          (gmul E (j + 1 + 1) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero A) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (Pp E j)))) G = _
        rw [mono0_mul, mono0_mul]; rfl

/-- **The n-th power residue criterion.** -/
theorem nth_recip (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (ψ : Zp (j + 1 + 1) → S)
    (hm : ∀ x y, ψ ((ring (j + 1 + 1)).mul x y) = E.mul (ψ x) (ψ y)) (h1 : ψ (ring (j + 1 + 1)).one = E.one)
    (h0 : ψ (ring (j + 1 + 1)).zero = E.zero) (hs : sumK E ψ (LL j) = E.zero) (k : Nat)
    (hnt : ∀ i, i < k → sumK E (fun a => pw E (ψ a) (i + 2)) (LL j) = E.zero) (hinv : ∀ x, pw E (ψ x) (k + 1) = ψ (iv j x))
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (hE : nsm E (k' + 1 + 1) E.one = E.zero)
    (hfix : ∀ a, pw E (ψ a) (k' + 1 + 1) = ψ a) (hQ : nsm (ring (j + 1 + 1)) (k' + 1 + 1) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (m : Nat) (hqn : k' + 1 + 1 = (k + 2) * m + 1) :
    ψ (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) (k' + 1 + 1) (ring (j + 1 + 1)).one) (j + 1)) =
      pw E (E.mul (E.mul (jpr E j ψ k) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) (Pp E j)) m := by
  have hF := gauss_frob E (j + 1) hpr k' hq hE ψ hm hfix hQ
  rw [gauss_Gv] at hF
  have hP : pw (gring E (j + 1 + 1)) (Gv j ψ) (k' + 1 + 1) = (gring E (j + 1 + 1)).mul (pw (gring E (j + 1 + 1)) (pw (gring E (j + 1 + 1)) (Gv j ψ) (k + 2)) m) (Gv j ψ) := by
    rw [hqn, TwoPower.pwpow]; rfl
  rw [hP, gpow_n E j hpr ψ hm h1 h0 hs k hnt hinv] at hF
  have hX := powXG E j (E.mul (jpr E j ψ k) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) (Gv j ψ) (XG E j ψ hs) m
  have hF' := hX.symm.trans hF
  have hc : gt E (j + 1 + 1) (gmul E (j + 1 + 1) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero
        (pw E (E.mul (E.mul (jpr E j ψ k) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) (Pp E j)) m)) (Gv j ψ)) (ring (j + 1 + 1)).one =
      gt E (j + 1 + 1) (gmul E (j + 1 + 1) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero
        (ψ (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) (k' + 1 + 1) (ring (j + 1 + 1)).one) (j + 1)))) (Gv j ψ)) (ring (j + 1 + 1)).one :=
    congrArg (fun u => gt E (j + 1 + 1) u (ring (j + 1 + 1)).one) hF'
  rw [gt_mono0, gt_mono0] at hc
  unfold Gv at hc
  rw [gt_tb, h1] at hc
  calc _ = E.mul (ψ (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) (k' + 1 + 1) (ring (j + 1 + 1)).one) (j + 1))) E.one := by lib_ring
    _ = E.mul (pw E (E.mul (E.mul (jpr E j ψ k) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) (Pp E j)) m) E.one := hc.symm
    _ = _ := by lib_ring

end CharSumGen

#print axioms CharSumGen.mono0_one
#print axioms CharSumGen.mono0_mul
#print axioms CharSumGen.powXG
#print axioms CharSumGen.nth_recip

lang/NthRecip2.lean

lean_all/lang/NthRecip2.lean

/-
  lang/NthRecip2 — T49, cycle 610: **non-vacuity check of NthRecip: the case n = 2**. With ψ = the Legendre symbol χ_p cast into 𝔽_q
  (q = 2r + 3), every hypothesis of CharSumGen.nth_recip is met (ψ² nontriviality is vacuous for n = 2, ψ = ψ∘ι, Fermat in 𝔽_q), and it gives
    χ_p(q^(−1)) = (χ_p(−1)·p)^((q−1)/2)   in 𝔽_q,
  i.e. Euler's criterion for p* = χ_p(−1)p modulo q: quadratic reciprocity (compare GaussQR.qr_gauss).
  Lean 4 core only, proofs by hand (lib_int, lib_ring), no axioms.
-/
import lang.NthRecip
import lang.GaussQR
import lang.IntCast

namespace CharSumGen

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius QChar ConicSum
open LibRing (castZ castN)

variable (m r : Nat)

theorem castZ_sum {A : Type} (f : A → Int) : ∀ l : List A,
    castZ (ring (2 * r + 1 + 1)) (sumK LibInt.intR f l) = sumK (ring (2 * r + 1 + 1)) (fun a => castZ (ring (2 * r + 1 + 1)) (f a)) l
  | [] => rfl
  | a :: l => by
    show castZ (ring (2 * r + 1 + 1)) (f a + sumK LibInt.intR f l) = _
    rw [IntCast.castZ_add, castZ_sum f l]; rfl

/-- **n = 2**: χ_p(q⁻¹) = (χ_p(−1)·p)^(r+1) in 𝔽_q, from the general criterion. -/
theorem nth_recip_two (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1)
    (hq : ∀ d, Dv d (2 * r + 1 + 1 + 1) → d = 1 ∨ d = 2 * r + 1 + 1 + 1)
    (hQ : nsm (ring (2 * m + 1 + 1)) (2 * r + 1 + 1 + 1) (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero) :
    castZ (ring (2 * r + 1 + 1)) (chi m (pw (ring (2 * m + 1 + 1)) (nsm (ring (2 * m + 1 + 1)) (2 * r + 1 + 1 + 1) (ring (2 * m + 1 + 1)).one) (2 * m + 1))) =
      pw (ring (2 * r + 1 + 1)) ((ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1 + 1)).mul (ring (2 * r + 1 + 1)).one
        (castZ (ring (2 * r + 1 + 1)) (chi m (iv (2 * m) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))))) (Pp (ring (2 * r + 1 + 1)) (2 * m))) (r + 1) := by
  have h := nth_recip (ring (2 * r + 1 + 1)) (2 * m) hpr (fun a => castZ (ring (2 * r + 1 + 1)) (chi m a))
    (fun x y => by show castZ _ (chi m ((ring (2 * m + 1 + 1)).mul x y)) = _; rw [chi_mul m hpr, HasseDavenport.castZ_mul])
    (by show castZ _ (chi m (ring (2 * m + 1 + 1)).one) = _; rw [chi_one]; exact GaussQR.castZ_one' _)
    (by show castZ _ (chi m (ring (2 * m + 1 + 1)).zero) = _; rw [chi_zero]; rfl)
    (by show sumK _ (fun a => castZ (ring (2 * r + 1 + 1)) (chi m a)) (L m) = _
        rw [← castZ_sum, show sumK LibInt.intR (chi m) (L m) = 0 from chi_sum m hpr]; rfl)
    0 (fun i hi => absurd hi (Nat.not_lt_zero i))
    (fun x => by
      show (ring (2 * r + 1 + 1)).mul (ring (2 * r + 1 + 1)).one (castZ _ (chi m x)) = castZ _ (chi m (pw (ring (2 * m + 1 + 1)) x (2 * m + 1)))
      rw [(ring (2 * r + 1 + 1)).one_mul]
      by_cases hx : x = (ring (2 * m + 1 + 1)).zero
      · rw [hx, show pw (ring (2 * m + 1 + 1)) (ring (2 * m + 1 + 1)).zero (2 * m + 1) = (ring (2 * m + 1 + 1)).zero from ConicSum.iv_zero m]
      · rw [show pw (ring (2 * m + 1 + 1)) x (2 * m + 1) = ConicSum.iv m x from rfl, GaussFrob.chi_inv m hpr x hx])
    (2 * r + 1) hq (char_p (2 * r + 1 + 1)) (fun a => GaussFrob.fermat_q r hq _) hQ (r + 1) (by lib_nat)
  exact h

end CharSumGen

#print axioms CharSumGen.castZ_sum
#print axioms CharSumGen.nth_recip_two

lang/NthRecipInert.lean

lean_all/lang/NthRecipInert.lean

/-
  lang/NthRecipInert — T49, cycle 608: **the n-th power residue criterion at primes q ≡ −1 (mod n)**:
  with ψ as in NthRecip (order n = k + 2), q prime, q + 1 = n·m, E of characteristic q with ψ^q = ψ̄ (= ψ∘ι):
    gauss_frob_bar   G_ψ^q = ψ(q) G_ψ̄
    nth_recip_inert  ψ(q)·ψ(ι(−1)) = A^m p^(m−1),   A = ∏_{i≤k} J(ψ^i, ψ)·ψ(ι(−1))
  (the cubic case is CubicRecip: χ_p(q) = J^(k+1) p^k for q = 3k + 2).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.NthRecip

namespace CharSumGen

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

variable {S : Type} (E : CRing S) (j : Nat)

theorem gauss_frob_bar (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1)
    (hE : nsm E (k' + 1 + 1) E.one = E.zero) (ψ : Zp (j + 1 + 1) → S)
    (hm : ∀ a b, ψ ((ring (j + 1 + 1)).mul a b) = E.mul (ψ a) (ψ b)) (hbar : ∀ a, pw E (ψ a) (k' + 1 + 1) = ψ (iv j a))
    (hQ : nsm (ring (j + 1 + 1)) (k' + 1 + 1) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero) :
    pw (gring E (j + 1 + 1)) (Gv j ψ) (k' + 1 + 1) =
      gmul E (j + 1 + 1) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (ψ (nsm (ring (j + 1 + 1)) (k' + 1 + 1) (ring (j + 1 + 1)).one))) (Gv j (fun x => ψ (iv j x))) := by
  rw [← gauss_Gv E j ψ, ← gauss_Gv E j (fun x => ψ (iv j x))]
  generalize hQd : nsm (ring (j + 1 + 1)) (k' + 1 + 1) (ring (j + 1 + 1)).one = Q at hQ ⊢
  have hinv := inv_exists (j + 1) hpr Q hQ
  generalize hQi : pw (ring (j + 1 + 1)) Q (j + 1) = Qi at hinv
  have hL : pw (gring E (j + 1 + 1)) (gauss E (j + 1) ψ) (k' + 1 + 1) = sumK (gring E (j + 1 + 1)) (fun a => mono E (j + 1 + 1) ((ring (j + 1 + 1)).mul Q a) (ψ (iv j a))) (L (j + 1 + 1)) := by
    unfold gauss
    rw [frob_sum E (j + 1 + 1) k' hq hE]
    apply sumK_ext; intro a
    rw [mono_pow, hbar, nsm_eq_mul, hQd]
  have hc := CMInert.complete_allZ (j + 1 + 1) (gring E (j + 1 + 1))
  have hre := reindex (gring E (j + 1 + 1)) (L (j + 1 + 1)) hc (fun b => (ring (j + 1 + 1)).mul Qi b) (fun a => (ring (j + 1 + 1)).mul Q a)
    (fun b => by show (ring (j + 1 + 1)).mul Q ((ring (j + 1 + 1)).mul Qi b) = b
                 calc _ = (ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul Qi Q) b := by lib_ring
                   _ = b := by rw [hinv]; lib_ring)
    (fun a => by show (ring (j + 1 + 1)).mul Qi ((ring (j + 1 + 1)).mul Q a) = a
                 calc _ = (ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul Qi Q) a := by lib_ring
                   _ = a := by rw [hinv]; lib_ring)
    (fun a => mono E (j + 1 + 1) ((ring (j + 1 + 1)).mul Q a) (ψ (iv j a)))
  rw [hL, ← hre]
  unfold gauss
  show _ = (gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (ψ Q)) (sumK (gring E (j + 1 + 1)) (fun a => mono E (j + 1 + 1) a (ψ (iv j a))) (L (j + 1 + 1)))
  rw [mul_sumK]
  apply sumK_ext; intro b
  show _ = gmul E (j + 1 + 1) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (ψ Q)) (mono E (j + 1 + 1) b (ψ (iv j b)))
  rw [mono_mul, ← hm]
  have e1 : (ring (j + 1 + 1)).mul Q ((ring (j + 1 + 1)).mul Qi b) = (ring (j + 1 + 1)).add (ring (j + 1 + 1)).zero b := by
    calc _ = (ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul Qi Q) b := by lib_ring
      _ = _ := by rw [hinv]; lib_ring
  have e2 : iv j ((ring (j + 1 + 1)).mul Qi b) = (ring (j + 1 + 1)).mul Q (iv j b) := by
    rw [iv_mulmul, ← hQi]
    show (ring (j + 1 + 1)).mul (iv j (iv j Q)) (iv j b) = _
    rw [iv_iv j hpr]
  rw [e1, e2]

/-- (mono0 A · X)^(t+1) = mono0 (A^(t+1) p^t) · X. -/
theorem powX (A : S) : ∀ t,
    pw (gring E (j + 1 + 1)) ((gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero A) (Xv E j)) (t + 1) =
      (gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (E.mul (pw E A (t + 1)) (pw E (Pp E j) t))) (Xv E j)
  | 0 => by
    show (gring E (j + 1 + 1)).mul (gring E (j + 1 + 1)).one ((gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero A) (Xv E j)) = _
    rw [(gring E (j + 1 + 1)).one_mul]
    show _ = (gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (E.mul (E.mul E.one A) E.one)) (Xv E j)
    rw [show E.mul (E.mul E.one A) E.one = A by lib_ring]
  | t + 1 => by
    show (gring E (j + 1 + 1)).mul (pw (gring E (j + 1 + 1)) ((gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero A) (Xv E j)) (t + 1))
      ((gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero A) (Xv E j)) = _
    rw [powX A t]
    have hXX : (gring E (j + 1 + 1)).mul (Xv E j) (Xv E j) = (gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (Pp E j)) (Xv E j) := XX E j
    calc _ = (gring E (j + 1 + 1)).mul ((gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (E.mul (pw E A (t + 1)) (pw E (Pp E j) t))) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero A))
          ((gring E (j + 1 + 1)).mul (Xv E j) (Xv E j)) := by lib_ring
      _ = (gring E (j + 1 + 1)).mul ((gring E (j + 1 + 1)).mul ((gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (E.mul (pw E A (t + 1)) (pw E (Pp E j) t))) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero A))
          (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (Pp E j))) (Xv E j) := by rw [hXX]; lib_ring
      _ = _ := by
        show gmul E (j + 1 + 1) (gmul E (j + 1 + 1) (gmul E (j + 1 + 1) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero _) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero A)) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (Pp E j))) (Xv E j) = _
        rw [mono0_mul, mono0_mul]
        show _ = gmul E (j + 1 + 1) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (E.mul (E.mul (pw E A (t + 1)) A) (E.mul (pw E (Pp E j) t) (Pp E j)))) (Xv E j)
        rw [show E.mul (E.mul (E.mul (pw E A (t + 1)) (pw E (Pp E j) t)) A) (Pp E j) = E.mul (E.mul (pw E A (t + 1)) A) (E.mul (pw E (Pp E j) t) (Pp E j)) by lib_ring]

/-- **The n-th power residue criterion at q ≡ −1 (mod n)**: ψ(q)·ψ(ι(−1)) = A^m p^(m−1). -/
theorem nth_recip_inert (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (ψ : Zp (j + 1 + 1) → S)
    (hm : ∀ x y, ψ ((ring (j + 1 + 1)).mul x y) = E.mul (ψ x) (ψ y)) (h1 : ψ (ring (j + 1 + 1)).one = E.one)
    (h0 : ψ (ring (j + 1 + 1)).zero = E.zero) (hs : sumK E ψ (LL j) = E.zero) (k : Nat)
    (hnt : ∀ i, i < k → sumK E (fun a => pw E (ψ a) (i + 2)) (LL j) = E.zero) (hinv : ∀ x, pw E (ψ x) (k + 1) = ψ (iv j x))
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (hE : nsm E (k' + 1 + 1) E.one = E.zero)
    (hbar : ∀ a, pw E (ψ a) (k' + 1 + 1) = ψ (iv j a)) (hQ : nsm (ring (j + 1 + 1)) (k' + 1 + 1) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (m : Nat) (hqn : k' + 1 + 1 + 1 = (k + 2) * (m + 1)) :
    E.mul (ψ (nsm (ring (j + 1 + 1)) (k' + 1 + 1) (ring (j + 1 + 1)).one)) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))) =
      E.mul (pw E (E.mul (jpr E j ψ k) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) (m + 1)) (pw E (Pp E j) m) := by
  have hF := gauss_frob_bar E j hpr k' hq hE ψ hm hbar hQ
  -- G^(q+1) two ways
  have hA : pw (gring E (j + 1 + 1)) (Gv j ψ) (k' + 1 + 1 + 1) =
      (gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (E.mul (pw E (E.mul (jpr E j ψ k) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) (m + 1)) (pw E (Pp E j) m))) (Xv E j) := by
    rw [hqn, ← TwoPower.pwpow, gpow_n E j hpr ψ hm h1 h0 hs k hnt hinv]
    exact powX E j _ m
  have hB : pw (gring E (j + 1 + 1)) (Gv j ψ) (k' + 1 + 1 + 1) =
      (gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (E.mul (ψ (nsm (ring (j + 1 + 1)) (k' + 1 + 1) (ring (j + 1 + 1)).one)) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))))) (Xv E j) := by
    show (gring E (j + 1 + 1)).mul (pw (gring E (j + 1 + 1)) (Gv j ψ) (k' + 1 + 1)) (Gv j ψ) = _
    rw [hF]
    have hN : (gring E (j + 1 + 1)).mul (Gv j ψ) (Gv j (fun x => ψ (iv j x))) = (gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) (Xv E j) :=
      ext E (j + 1 + 1) _ _ (fun c => by
        show gt E (j + 1 + 1) (gmul E (j + 1 + 1) _ _) c = gt E (j + 1 + 1) (gmul E (j + 1 + 1) _ _) c
        rw [gt_mono0, gauss_norm E j hpr ψ hm h1 h0 hs c])
    calc (gring E (j + 1 + 1)).mul (gmul E (j + 1 + 1) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (ψ (nsm (ring (j + 1 + 1)) (k' + 1 + 1) (ring (j + 1 + 1)).one))) (Gv j (fun x => ψ (iv j x)))) (Gv j ψ)
        = (gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (ψ (nsm (ring (j + 1 + 1)) (k' + 1 + 1) (ring (j + 1 + 1)).one))) ((gring E (j + 1 + 1)).mul (Gv j ψ) (Gv j (fun x => ψ (iv j x)))) := by
          show (gring E (j + 1 + 1)).mul ((gring E (j + 1 + 1)).mul _ _) _ = _; lib_ring
      _ = (gring E (j + 1 + 1)).mul ((gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (ψ (nsm (ring (j + 1 + 1)) (k' + 1 + 1) (ring (j + 1 + 1)).one))) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))))) (Xv E j) := by rw [hN]; lib_ring
      _ = _ := by show gmul E (j + 1 + 1) (gmul E (j + 1 + 1) _ _) _ = _; rw [mono0_mul]; rfl
  have hX1 : gt E (j + 1 + 1) (Xv E j) (ring (j + 1 + 1)).one = E.neg E.one := by
    rw [gt_X]; unfold delta; rw [ite_neg' _ _ (fun e => PowerSum.one_ne_zero' (j + 1) e)]; lib_ring
  have hc' : E.mul (E.mul (pw E (E.mul (jpr E j ψ k) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) (m + 1)) (pw E (Pp E j) m)) (E.neg E.one) =
      E.mul (E.mul (ψ (nsm (ring (j + 1 + 1)) (k' + 1 + 1) (ring (j + 1 + 1)).one)) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) (E.neg E.one) := by
    have hc0 : gt E (j + 1 + 1) (gmul E (j + 1 + 1) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (E.mul (pw E (E.mul (jpr E j ψ k) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) (m + 1)) (pw E (Pp E j) m))) (Xv E j)) (ring (j + 1 + 1)).one =
        gt E (j + 1 + 1) (gmul E (j + 1 + 1) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (E.mul (ψ (nsm (ring (j + 1 + 1)) (k' + 1 + 1) (ring (j + 1 + 1)).one)) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))))) (Xv E j)) (ring (j + 1 + 1)).one :=
      congrArg (fun u => gt E (j + 1 + 1) u (ring (j + 1 + 1)).one) (hA.symm.trans hB)
    rw [gt_mono0, gt_mono0, hX1] at hc0; exact hc0
  calc _ = E.mul (E.neg E.one) (E.mul (E.mul (ψ (nsm (ring (j + 1 + 1)) (k' + 1 + 1) (ring (j + 1 + 1)).one)) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) (E.neg E.one)) := by lib_ring
    _ = _ := by rw [← hc']; lib_ring

end CharSumGen

#print axioms CharSumGen.gauss_frob_bar
#print axioms CharSumGen.powX
#print axioms CharSumGen.nth_recip_inert

lang/NthRecipPow.lean

lean_all/lang/NthRecipPow.lean

/-
  lang/NthRecipPow — T49, cycle 625: **the n-th power residue criterion for every prime q ∤ np** (any residue class of q mod n).
  If q^f ≡ 1 (mod n) (f the order of q, or any multiple), iterate the Frobenius:
    frob_sum_pow     (Σ x_a)^(q^e) = Σ x_a^(q^e) in E[C_p], char E = q
    gauss_frob_pow   ψ^(q^e) = ψ ⇒ G_ψ^(q^e) = ψ(q^(−e)) G_ψ
    nth_recip_pow    q^e = n·m + 1 ⇒ ψ(q^(−e)) = (A·p)^m,  A = (∏_{i≤k} J(ψ^i, ψ))·ψ(ι(−1))
  (NthRecip is e = 1; for q of order f mod n this gives ψ(q)^f, i.e. the n-th power residue symbol of q up to f-th roots of unity,
  and q is an n-th power mod p ⇒ (A·p)^((q^f − 1)/n) = 1 in E = 𝔽_{q^f}.)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.NthRecip

namespace GroupRingFrob

open FTAChain ZMod YMChain FiniteRecChain DetChain GroupRing Frobenius

variable {S : Type} (E : CRing S) (j : Nat)

theorem frob_sum_pow (k : Nat) (hq : ∀ d, Dv d (k + 1 + 1) → d = 1 ∨ d = k + 1 + 1) (hE : nsm E (k + 1 + 1) E.one = E.zero) :
    ∀ e {A : Type} (f : A → V S (j + 1 + 1)) (l : List A),
      pw (gring E (j + 1)) (sumK (gring E (j + 1)) f l) ((k + 1 + 1) ^ e) = sumK (gring E (j + 1)) (fun a => pw (gring E (j + 1)) (f a) ((k + 1 + 1) ^ e)) l
  | 0, _, f, l => by
    show (gring E (j + 1)).mul (gring E (j + 1)).one _ = _
    rw [(gring E (j + 1)).one_mul]
    exact (sumK_ext _ _ _ (fun a => (gring E (j + 1)).one_mul (f a)) l).symm
  | e + 1, _, f, l => by
    rw [show (k + 1 + 1) ^ (e + 1) = (k + 1 + 1) ^ e * (k + 1 + 1) from rfl, ← TwoPower.pwpow, frob_sum_pow k hq hE e f l,
      frob_sum E (j + 1) k hq hE]
    exact sumK_ext _ _ _ (fun a => TwoPower.pwpow _ _ _ _) l

theorem gauss_frob_pow (hpr : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1) (k : Nat) (hq : ∀ d, Dv d (k + 1 + 1) → d = 1 ∨ d = k + 1 + 1)
    (hE : nsm E (k + 1 + 1) E.one = E.zero) (e : Nat) (ψ : Zp (j + 1) → S)
    (hmul : ∀ a b, ψ ((ring (j + 1)).mul a b) = E.mul (ψ a) (ψ b)) (hfix : ∀ a, pw E (ψ a) ((k + 1 + 1) ^ e) = ψ a)
    (hQ : nsm (ring (j + 1)) ((k + 1 + 1) ^ e) (ring (j + 1)).one ≠ (ring (j + 1)).zero) :
    pw (gring E (j + 1)) (gauss E j ψ) ((k + 1 + 1) ^ e) =
      gmul E (j + 1) (mono E (j + 1) (ring (j + 1)).zero (ψ (pw (ring (j + 1)) (nsm (ring (j + 1)) ((k + 1 + 1) ^ e) (ring (j + 1)).one) j))) (gauss E j ψ) := by
  generalize hQd : nsm (ring (j + 1)) ((k + 1 + 1) ^ e) (ring (j + 1)).one = Q at hQ
  have hinv := inv_exists j hpr Q hQ
  generalize hQi : pw (ring (j + 1)) Q j = Qi at hinv
  -- LHS = Σ_a mono (Q a) (ψ a)
  have hL : pw (gring E (j + 1)) (gauss E j ψ) ((k + 1 + 1) ^ e) = sumK (gring E (j + 1)) (fun a => mono E (j + 1) ((ring (j + 1)).mul Q a) (ψ a)) (L (j + 1)) := by
    unfold gauss
    rw [frob_sum_pow E j k hq hE e]
    apply sumK_ext; intro a
    rw [mono_pow, hfix, nsm_eq_mul, hQd]
  -- reindex a = Qi b
  have hc := CMInert.complete_allZ (j + 1) (gring E (j + 1))
  have hre := reindex (gring E (j + 1)) (L (j + 1)) hc (fun b => (ring (j + 1)).mul Qi b) (fun a => (ring (j + 1)).mul Q a)
    (fun b => by show (ring (j + 1)).mul Q ((ring (j + 1)).mul Qi b) = b
                 calc _ = (ring (j + 1)).mul ((ring (j + 1)).mul Qi Q) b := by lib_ring
                   _ = b := by rw [hinv]; lib_ring)
    (fun a => by show (ring (j + 1)).mul Qi ((ring (j + 1)).mul Q a) = a
                 calc _ = (ring (j + 1)).mul ((ring (j + 1)).mul Qi Q) a := by lib_ring
                   _ = a := by rw [hinv]; lib_ring)
    (fun a => mono E (j + 1) ((ring (j + 1)).mul Q a) (ψ a))
  rw [hL, ← hre]
  unfold gauss
  show _ = (gring E (j + 1)).mul (mono E (j + 1) (ring (j + 1)).zero (ψ Qi)) (sumK (gring E (j + 1)) (fun a => mono E (j + 1) a (ψ a)) (L (j + 1)))
  rw [mul_sumK]
  apply sumK_ext; intro b
  show _ = gmul E (j + 1) (mono E (j + 1) (ring (j + 1)).zero (ψ Qi)) (mono E (j + 1) b (ψ b))
  rw [mono_mul, ← hmul]
  have e1 : (ring (j + 1)).mul Q ((ring (j + 1)).mul Qi b) = (ring (j + 1)).add (ring (j + 1)).zero b := by
    calc _ = (ring (j + 1)).mul ((ring (j + 1)).mul Qi Q) b := by lib_ring
      _ = _ := by rw [hinv]; lib_ring
  rw [e1]

end GroupRingFrob

namespace CharSumGen

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

variable {S : Type} (E : CRing S) (j : Nat)

/-- **The n-th power residue criterion at q^e ≡ 1 (mod n).** -/
theorem nth_recip_pow (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (ψ : Zp (j + 1 + 1) → S)
    (hm : ∀ x y, ψ ((ring (j + 1 + 1)).mul x y) = E.mul (ψ x) (ψ y)) (h1 : ψ (ring (j + 1 + 1)).one = E.one)
    (h0 : ψ (ring (j + 1 + 1)).zero = E.zero) (hs : sumK E ψ (LL j) = E.zero) (k : Nat)
    (hnt : ∀ i, i < k → sumK E (fun a => pw E (ψ a) (i + 2)) (LL j) = E.zero) (hinv : ∀ x, pw E (ψ x) (k + 1) = ψ (iv j x))
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (hE : nsm E (k' + 1 + 1) E.one = E.zero) (e : Nat)
    (hfix : ∀ a, pw E (ψ a) ((k' + 1 + 1) ^ e) = ψ a) (hQ : nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (m : Nat) (hqn : (k' + 1 + 1) ^ e = (k + 2) * m + 1) :
    ψ (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one) (j + 1)) =
      pw E (E.mul (E.mul (jpr E j ψ k) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) (Pp E j)) m := by
  have hF := gauss_frob_pow E (j + 1) hpr k' hq hE e ψ hm hfix hQ
  rw [gauss_Gv] at hF
  have hP : pw (gring E (j + 1 + 1)) (Gv j ψ) ((k' + 1 + 1) ^ e) = (gring E (j + 1 + 1)).mul (pw (gring E (j + 1 + 1)) (pw (gring E (j + 1 + 1)) (Gv j ψ) (k + 2)) m) (Gv j ψ) := by
    rw [hqn, TwoPower.pwpow]; rfl
  rw [hP, gpow_n E j hpr ψ hm h1 h0 hs k hnt hinv] at hF
  have hX := powXG E j (E.mul (jpr E j ψ k) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) (Gv j ψ) (XG E j ψ hs) m
  have hF' := hX.symm.trans hF
  have hc : gt E (j + 1 + 1) (gmul E (j + 1 + 1) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero
        (pw E (E.mul (E.mul (jpr E j ψ k) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) (Pp E j)) m)) (Gv j ψ)) (ring (j + 1 + 1)).one =
      gt E (j + 1 + 1) (gmul E (j + 1 + 1) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero
        (ψ (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one) (j + 1)))) (Gv j ψ)) (ring (j + 1 + 1)).one :=
    congrArg (fun u => gt E (j + 1 + 1) u (ring (j + 1 + 1)).one) hF'
  rw [gt_mono0, gt_mono0] at hc
  unfold Gv at hc
  rw [gt_tb, h1] at hc
  calc _ = E.mul (ψ (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one) (j + 1))) E.one := by lib_ring
    _ = E.mul (pw E (E.mul (E.mul (jpr E j ψ k) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) (Pp E j)) m) E.one := hc.symm
    _ = _ := by lib_ring

end CharSumGen

#print axioms GroupRingFrob.frob_sum_pow
#print axioms GroupRingFrob.gauss_frob_pow
#print axioms CharSumGen.nth_recip_pow

lang/OneHotIdx.lean

lean_all/lang/OneHotIdx.lean

/-
  lang/OneHotIdx — T49 (O3'): **a one-hot coefficient list has one ±1 entry at some index j, zeros elsewhere**.
  For the list (L m).map f (L m = allZ m (m + 1), built by appending):
    map_app, map_map      list map facts (by hand)
    zeros_app, onehot_app  Zeros / OneHot of l ++ [x]
    lt_succ_cases         i < k + 1 ⇒ i < k ∨ i = k
    onehot_idx            OneHot ((allZ k).map f) ⇒ f(j) = ±1 for one j < k, f(i) = 0 for the other i < k
    onehot_L              OneHot ((L m).map f) ⇒ ∃ j, f j = ±1 ∧ ∀ b ≠ j, f b = 0
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.UnitCirc2
import lang.GroupRing

namespace OneHotIdx

open FTAChain ZMod YMChain FiniteRecChain DetChain GroupRing UnitCirc2

theorem map_app {A B : Type} (f : A → B) (x : A) : ∀ l : List A, (l ++ [x]).map f = l.map f ++ [f x]
  | [] => rfl
  | y :: l => congrArg (List.cons (f y)) (map_app f x l)

theorem map_map {A B C : Type} (g : A → B) (h : B → C) : ∀ l : List A, (l.map g).map h = l.map (fun a => h (g a))
  | [] => rfl
  | y :: l => congrArg (List.cons (h (g y))) (map_map g h l)

theorem zeros_app (x : Int) : ∀ l : List Int, Zeros (l ++ [x]) → Zeros l ∧ x = 0
  | [], h => And.intro True.intro (And.left h)
  | _ :: l, h => And.intro (And.intro (And.left h) (zeros_app x l (And.right h)).1) (zeros_app x l (And.right h)).2

theorem onehot_app (x : Int) : ∀ l : List Int, OneHot (l ++ [x]) →
    (OneHot l ∧ x = 0) ∨ (Zeros l ∧ (x = 1 ∨ x = -1))
  | [], h => by
    cases h with
    | inl h1 => exact Or.inr ⟨True.intro, h1.1⟩
    | inr h2 => exact (h2.2 : False).elim
  | y :: l, h => by
    cases h with
    | inl h1 =>
      obtain ⟨hz, hx⟩ := zeros_app x l h1.2
      exact Or.inl ⟨Or.inl ⟨h1.1, hz⟩, hx⟩
    | inr h2 =>
      cases onehot_app x l h2.2 with
      | inl h3 => exact Or.inl ⟨Or.inr ⟨h2.1, h3.1⟩, h3.2⟩
      | inr h4 => exact Or.inr ⟨And.intro h2.1 h4.1, h4.2⟩

theorem lt_succ_cases : ∀ i k : Nat, i < k + 1 → i < k ∨ i = k
  | 0, 0, _ => Or.inr rfl
  | 0, _ + 1, _ => Or.inl (Nat.zero_lt_succ _)
  | _ + 1, 0, h => absurd (Nat.le_of_succ_le_succ h) (Nat.not_succ_le_zero _)
  | i + 1, k + 1, h => by
    cases lt_succ_cases i k (Nat.le_of_succ_le_succ h) with
    | inl h1 => exact Or.inl (Nat.succ_lt_succ h1)
    | inr h2 => exact Or.inr (congrArg Nat.succ h2)

variable (m : Nat)

theorem zeros_get (f : Zp m → Int) : ∀ k, Zeros ((CMInert.allZ m k).map f) → ∀ i, i < k → f (mk m i) = 0
  | 0, _, _, hi => absurd hi (Nat.not_lt_zero _)
  | k + 1, h, i, hi => by
    have h' : Zeros ((CMInert.allZ m k).map f ++ [f (mk m k)]) := by rw [← map_app f (mk m k)]; exact h
    obtain ⟨hz, hx⟩ := zeros_app _ _ h'
    cases lt_succ_cases i k hi with
    | inl h1 => exact zeros_get f k hz i h1
    | inr h2 => rw [h2]; exact hx

theorem onehot_idx (f : Zp m → Int) : ∀ k, OneHot ((CMInert.allZ m k).map f) →
    ∃ j, j < k ∧ (f (mk m j) = 1 ∨ f (mk m j) = -1) ∧ ∀ i, i < k → i ≠ j → f (mk m i) = 0
  | 0, h => (h : False).elim
  | k + 1, h => by
    have h' : OneHot ((CMInert.allZ m k).map f ++ [f (mk m k)]) := by rw [← map_app f (mk m k)]; exact h
    cases onehot_app _ _ h' with
    | inl h1 =>
      obtain ⟨j, hj, hpm, hz⟩ := onehot_idx f k h1.1
      refine ⟨j, Nat.lt_succ_of_lt hj, hpm, fun i hi hij => ?_⟩
      cases lt_succ_cases i k hi with
      | inl h3 => exact hz i h3 hij
      | inr h4 => rw [h4]; exact h1.2
    | inr h2 =>
      refine ⟨k, Nat.lt_succ_self k, h2.2, fun i hi hik => ?_⟩
      cases lt_succ_cases i k hi with
      | inl h3 => exact zeros_get m f k h2.1 i h3
      | inr h4 => exact absurd h4 hik

/-- **one-hot over all of 𝔽_ℓ**: f j = ±1 at one j, f b = 0 for b ≠ j. -/
theorem onehot_L (f : Zp m → Int) (h : OneHot ((L m).map f)) :
    ∃ j : Zp m, (f j = 1 ∨ f j = -1) ∧ ∀ b, b ≠ j → f b = 0 := by
  obtain ⟨j, hj, hpm, hz⟩ := onehot_idx m f (m + 1) h
  refine ⟨mk m j, hpm, fun b hb => ?_⟩
  rw [← mk_val m b]
  exact hz b.val b.property (fun e => hb ((mk_val m b).symm.trans (congrArg (mk m) e)))

end OneHotIdx

#print axioms OneHotIdx.map_app
#print axioms OneHotIdx.map_map
#print axioms OneHotIdx.zeros_app
#print axioms OneHotIdx.onehot_app
#print axioms OneHotIdx.lt_succ_cases
#print axioms OneHotIdx.zeros_get
#print axioms OneHotIdx.onehot_idx
#print axioms OneHotIdx.onehot_L

lang/Orb36.lean

lean_all/lang/Orb36.lean

/-
  lang/Orb36 — T47, cycle 592: **the 36a theta coefficients vanish at even n**: c3 (2m) = 0
  (the rotation Rt permutes the pairs (x, y), x ≡ 1 (mod 3), of norm 2m; Σx = Σx' = Σx'' and x + x' + x'' = 0, so 3·c3 = 0).

    pI, rtI, pre_count, bij, img_sum, c3_even
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.Rot36

namespace Theta36

open DetChain YMChain FiniteRecChain HeckeMult GaussInt LibGcd Hecke32

def pI (n : Nat) (z : Int × Int) : Int := if P3 n z then Z.one else Z.zero
def rtI (z w : Int × Int) : Int := if Rt z w then Z.one else Z.zero

theorem in_box (n : Nat) (w : Int × Int) (h : P3 n w) : Int.natAbs w.1 ≤ n ∧ Int.natAbs w.2 ≤ n := abs_bound w.1 w.2 n h.1

/-- Σ_z [P3 z]·[Rt z w] = [P3 w] for even norm. -/
theorem pre_count (m : Nat) (w : Int × Int) :
    sumK Z (fun z => Z.mul (pI (2 * m) z) (rtI z w)) (box (2 * m)) = pI (2 * m) w := by
  by_cases hw : P3 (2 * m) w
  · obtain ⟨z0, hz0⟩ := rt_pre m w.1 w.2 hw.1
    have hP0 : P3 (2 * m) z0 := (rt_P _ z0 w hz0).mpr hw
    have hpt : ∀ z, Z.mul (pI (2 * m) z) (rtI z w) = Z.mul (delta Z z z0) Z.one := fun z => by
      unfold pI rtI
      by_cases hz : z = z0
      · rw [hz, ite_pos' _ _ hP0, ite_pos' _ _ hz0]; unfold delta; rw [ite_pos' _ _ rfl]
      · rw [ite_neg' _ _ (fun hr => hz (rt_pre_uniq z z0 w hr hz0))]; unfold delta; rw [ite_neg' _ _ hz]; lib_ring
    rw [sumK_ext Z _ _ hpt, ← sumK_mul, count_box Z z0 _ (in_box _ z0 hP0).1 (in_box _ z0 hP0).2]
    unfold pI; rw [ite_pos' _ _ hw]; lib_ring
  · have hpt : ∀ z, Z.mul (pI (2 * m) z) (rtI z w) = Z.zero := fun z => by
      unfold pI rtI
      by_cases hr : Rt z w
      · rw [ite_neg' _ _ (fun hz => hw ((rt_P _ z w hr).mp hz))]; lib_ring
      · rw [ite_neg' _ _ hr]; lib_ring
    rw [sumK_ext Z _ _ hpt, HeckePrime.zsumR]
    unfold pI; rw [ite_neg' _ _ hw]

/-- Σ_z [P3 z] Σ_w [Rt z w] g(w) = Σ_w [P3 w] g(w). -/
theorem bij (m : Nat) (g : Int × Int → Int) :
    sumK Z (fun z => Z.mul (pI (2 * m) z) (sumK Z (fun w => Z.mul (rtI z w) (g w)) (box (2 * m)))) (box (2 * m)) =
      sumK Z (fun w => Z.mul (pI (2 * m) w) (g w)) (box (2 * m)) := by
  rw [sumK_ext Z _ _ (fun z => mul_sumK Z _ _ _), sumK_swap]
  apply sumK_ext; intro w
  rw [show sumK Z (fun z => Z.mul (pI (2 * m) z) (Z.mul (rtI z w) (g w))) (box (2 * m)) =
      sumK Z (fun z => Z.mul (Z.mul (pI (2 * m) z) (rtI z w)) (g w)) (box (2 * m)) from
    sumK_ext Z _ _ (fun z => by lib_ring) _, ← sumK_mul, pre_count]

/-- For P3 z: Σ_w [Rt z w] g(w) = g(image). -/
theorem img_sum (m : Nat) (z : Int × Int) (hz : P3 (2 * m) z) (g : Int × Int → Int) :
    ∃ w0, Rt z w0 ∧ P3 (2 * m) w0 ∧ sumK Z (fun w => Z.mul (rtI z w) (g w)) (box (2 * m)) = g w0 := by
  obtain ⟨w0, hw0⟩ := rt_img m z.1 z.2 hz.1
  have hP : P3 (2 * m) w0 := (rt_P _ z w0 hw0).mp hz
  refine ⟨w0, hw0, hP, ?_⟩
  have hpt : ∀ w, Z.mul (rtI z w) (g w) = Z.mul (delta Z w w0) (g w) := fun w => by
    unfold rtI
    by_cases hw : w = w0
    · rw [hw, ite_pos' _ _ hw0]; unfold delta; rw [ite_pos' _ _ rfl]
    · rw [ite_neg' _ _ (fun hr => hw (rt_uniq z w w0 hr hw0))]; unfold delta; rw [ite_neg' _ _ hw]
  rw [sumK_ext Z _ _ hpt, pick_box Z g w0 _ (in_box _ w0 hP).1 (in_box _ w0 hP).2]

/-- **c3 (2m) = 0.** -/
theorem c3_even (m : Nat) : c3 (2 * m) = 0 := by
  have h0 : c3 (2 * m) = sumK Z (fun z => Z.mul (pI (2 * m) z) z.1) (box (2 * m)) := by
    unfold c3 box; rw [sumK_pairs]
    exact sumK_ext Z _ _ (fun x => sumK_ext Z _ _ (fun y => by
      unfold F3 pI; by_cases h : P3 (2 * m) (x, y)
      · rw [ite_pos' _ _ h, ite_pos' _ _ h]; exact (Z.one_mul _).symm
      · rw [ite_neg' _ _ h, ite_neg' _ _ h]; exact (DetChain.zero_mul' Z _).symm) _) _
  let g1 : Int × Int → Int := fun w => w.1
  let g2 : Int × Int → Int := fun w => sumK Z (fun v => Z.mul (rtI w v) v.1) (box (2 * m))
  have e1 := bij m g1
  have e2 := bij m g2
  -- S0 + S1 + S2 = 0 pointwise
  have hsum : ∀ z, Z.add (Z.add (Z.mul (pI (2 * m) z) z.1) (Z.mul (pI (2 * m) z) (sumK Z (fun w => Z.mul (rtI z w) (g1 w)) (box (2 * m)))))
      (Z.mul (pI (2 * m) z) (sumK Z (fun w => Z.mul (rtI z w) (g2 w)) (box (2 * m)))) = Z.zero := fun z => by
    by_cases hz : P3 (2 * m) z
    · obtain ⟨w0, hw0, hP0, hs1⟩ := img_sum m z hz g1
      obtain ⟨w0', hw0', _, hs2⟩ := img_sum m z hz g2
      have hww : w0' = w0 := rt_uniq z w0' w0 hw0' hw0
      rw [hww] at hs2
      obtain ⟨v0, hv0, _, hs3⟩ := img_sum m w0 hP0 (fun v => v.1)
      rw [hs1, hs2]
      show Z.add (Z.add (Z.mul (pI (2 * m) z) z.1) (Z.mul (pI (2 * m) z) w0.1)) (Z.mul (pI (2 * m) z) (sumK Z (fun v => Z.mul (rtI w0 v) v.1) (box (2 * m)))) = Z.zero
      rw [hs3]
      have ho := orbit_sum z w0 v0 hw0 hv0
      calc _ = Z.mul (pI (2 * m) z) (z.1 + w0.1 + v0.1) := by show _ = Z.mul _ (Z.add (Z.add _ _) _); lib_ring
        _ = _ := by rw [ho]; exact DetChain.mul_zero Z _
    · unfold pI; rw [ite_neg' _ _ hz]; lib_ring
  have htot := sumK_ext Z _ _ hsum (box (2 * m))
  rw [sumK_add, sumK_add, e1, e2, HeckePrime.zsumR] at htot
  -- the second term: Σ_w [P3 w] g2(w) = S1 = S0
  have e3 : sumK Z (fun w => Z.mul (pI (2 * m) w) (g2 w)) (box (2 * m)) = sumK Z (fun w => Z.mul (pI (2 * m) w) (g1 w)) (box (2 * m)) := bij m g1
  rw [e3] at htot
  rw [h0]
  generalize sumK Z (fun z => Z.mul (pI (2 * m) z) z.1) (box (2 * m)) = S at htot ⊢
  have : (fun w => Z.mul (pI (2 * m) w) (g1 w)) = (fun w => Z.mul (pI (2 * m) w) w.1) := rfl
  exact HeckeInert.int_cancel' S 0 2 (by
    show S * 3 = 0 * 3
    calc S * 3 = Z.add (Z.add S S) S := by show S * 3 = S + S + S; lib_int
      _ = 0 := htot
      _ = 0 * 3 := by lib_int)

end Theta36

#print axioms Theta36.in_box
#print axioms Theta36.pre_count
#print axioms Theta36.bij
#print axioms Theta36.img_sum
#print axioms Theta36.c3_even

lang/Orbit3.lean

lean_all/lang/Orbit3.lean

/-
  lang/Orbit3 — T43, cycle 355: counting orbits of an order-3 map by sums, and the law for −3 by counting.

    min3            for pairwise distinct a, b, c ∈ ℕ exactly one is the smallest
    orbit3          τ³ = id, H∘τ = H, H vanishes on fixed points ⟹ Σ_x H(x) = 3·Σ_x H(x)[x is the least of its orbit]
    mobius_count    τ(x) = 1/(1 − x) on 𝔽_p ∖ {0, 1}: p − 2 − #{x² − x + 1 = 0} ≡ 0 (mod 3), i.e. 3 | p − 3 − χ(−3)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.LegendreFour

namespace Orbit3

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum Jacobsthal QuadTwist JacobMoment PencilMoment PencilClosed LegendreMoment LegendreFour
open FiniteRecChain (ite_pos' ite_neg')

variable (m : Nat)

/-- [a is the least of a, b, c] -/
def least (a b c : Nat) : Int := if a < b then (if a < c then 1 else 0) else 0

theorem min3 (a b c : Nat) (hab : a ≠ b) (hbc : b ≠ c) (hca : c ≠ a) : least a b c + least b c a + least c a b = 1 := by
  unfold least
  cases Nat.lt_or_ge a b with
  | inl h1 =>
    cases Nat.lt_or_ge b c with
    | inl h2 =>
      rw [ite_pos' _ _ h1, ite_pos' _ _ (Nat.lt_trans h1 h2), ite_pos' _ _ h2, ite_neg' _ _ (fun h => Nat.lt_asymm h h1),
        ite_neg' _ _ (fun h => Nat.lt_asymm h (Nat.lt_trans h1 h2))]; rfl
    | inr h2 =>
      have h2' : c < b := Nat.lt_of_le_of_ne h2 (fun e => hbc e.symm)
      cases Nat.lt_or_ge a c with
      | inl h3 =>
        rw [ite_pos' _ _ h1, ite_pos' _ _ h3, ite_neg' _ _ (fun h => Nat.lt_asymm h h2'), ite_neg' _ _ (fun h => Nat.lt_asymm h h3)]; rfl
      | inr h3 =>
        have h3' : c < a := Nat.lt_of_le_of_ne h3 hca
        rw [ite_pos' _ _ h1, ite_neg' _ _ (fun h => Nat.lt_asymm h h3'), ite_neg' _ _ (fun h => Nat.lt_asymm h h2'), ite_pos' _ _ h3', ite_pos' _ _ h2']; rfl
  | inr h1 =>
    have h1' : b < a := Nat.lt_of_le_of_ne h1 (fun e => hab e.symm)
    cases Nat.lt_or_ge b c with
    | inl h2 =>
      rw [ite_neg' _ _ (fun h => Nat.lt_asymm h h1'), ite_pos' _ _ h2, ite_pos' _ _ h1', ite_neg' _ _ (fun h => Nat.lt_asymm h h2)]
      by_cases hc : c < a
      · rw [ite_pos' _ _ hc]; rfl
      · rw [ite_neg' _ _ hc]; rfl
    | inr h2 =>
      have h2' : c < b := Nat.lt_of_le_of_ne h2 (fun e => hbc e.symm)
      rw [ite_neg' _ _ (fun h => Nat.lt_asymm h h1'), ite_neg' _ _ (fun h => Nat.lt_asymm h h2'), ite_pos' _ _ (Nat.lt_trans h2' h1'), ite_pos' _ _ h2']; rfl

def Lst (sg : Zp (2 * m + 1 + 1) → Zp (2 * m + 1 + 1)) (x : Zp (2 * m + 1 + 1)) : Int := least x.val (sg x).val (sg (sg x)).val

/-- **Orbits of an order-3 map**: Σ_x H(x) = 3·Σ_x H(x)·[x least in its orbit], when τ³ = id, H∘τ = H and H = 0 at fixed points. -/
theorem orbit3 (sg : Zp (2 * m + 1 + 1) → Zp (2 * m + 1 + 1)) (h3 : ∀ x, sg (sg (sg x)) = x) (H : Zp (2 * m + 1 + 1) → Int)
    (hH : ∀ x, H (sg x) = H x) (hfix : ∀ x, sg x = x → H x = 0) :
    sumK LibInt.intR H (L m) = 3 * sumK LibInt.intR (fun x => H x * Lst m sg x) (L m) := by
  have hpt : ∀ x, H x = H x * Lst m sg x + H x * Lst m sg (sg x) + H x * Lst m sg (sg (sg x)) := fun x => by
    by_cases hx : sg x = x
    · rw [hfix x hx]; generalize Lst m sg x = u; generalize Lst m sg (sg x) = v; generalize Lst m sg (sg (sg x)) = w; lib_int
    · have h2 : sg (sg x) ≠ x := fun e => hx (by
        calc sg x = sg (sg (sg x)) := by rw [e]
          _ = x := h3 x)
      have h12 : sg (sg x) ≠ sg x := fun e => h2 (by
        have : x = sg (sg x) := by
          calc x = sg (sg (sg x)) := (h3 x).symm
            _ = sg (sg x) := by rw [e]; exact e
        exact this.symm)
      have val_ne : ∀ a b : Zp (2 * m + 1 + 1), a ≠ b → a.val ≠ b.val := fun a b h e => h (Subtype.ext e)
      have hm := min3 x.val (sg x).val (sg (sg x)).val (val_ne _ _ (fun e => hx e.symm)) (val_ne _ _ (fun e => h12 e.symm)) (val_ne _ _ h2)
      have e1 : Lst m sg (sg x) = least (sg x).val (sg (sg x)).val x.val := by unfold Lst; rw [h3]
      have e2 : Lst m sg (sg (sg x)) = least (sg (sg x)).val x.val (sg x).val := by unfold Lst; rw [h3]
      rw [e1, e2]
      unfold Lst
      calc H x = H x * (least x.val (sg x).val (sg (sg x)).val + least (sg x).val (sg (sg x)).val x.val + least (sg (sg x)).val x.val (sg x).val) := by
            rw [hm]; generalize H x = h; lib_int
        _ = _ := by
            generalize H x = h; generalize least x.val (sg x).val (sg (sg x)).val = u
            generalize least (sg x).val (sg (sg x)).val x.val = v; generalize least (sg (sg x)).val x.val (sg x).val = w; lib_int
  rw [sumK_ext LibInt.intR _ _ hpt, QuadDiagonal.sI_add, QuadDiagonal.sI_add]
  have r1 : sumK LibInt.intR (fun x => H x * Lst m sg (sg x)) (L m) = sumK LibInt.intR (fun x => H x * Lst m sg x) (L m) := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR (L m) (QuadDiagonal.hcomp m) sg (fun y => sg (sg y)) h3 (fun y => h3 y) (fun y => H y * Lst m sg y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Lst m sg (sg x) = H (sg x) * Lst m sg (sg x); rw [hH]) _
  have r2 : sumK LibInt.intR (fun x => H x * Lst m sg (sg (sg x))) (L m) = sumK LibInt.intR (fun x => H x * Lst m sg x) (L m) := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR (L m) (QuadDiagonal.hcomp m) (fun y => sg (sg y)) sg (fun y => h3 y) h3 (fun y => H y * Lst m sg y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Lst m sg (sg (sg x)) = H (sg (sg x)) * Lst m sg (sg (sg x)); rw [hH, hH]) _
  rw [r1, r2]
  generalize sumK LibInt.intR (fun x => H x * Lst m sg x) (L m) = S
  lib_int

/-- τ(x) = 1/(1 − x) on 𝔽_p ∖ {0, 1}, identity on {0, 1}. -/
def tau (x : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) :=
  if x = (ring (2 * m + 1 + 1)).zero then x else if x = (ring (2 * m + 1 + 1)).one then x else iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x))

theorem one_ne : ((ring (2 * m + 1 + 1)).one : Zp (2 * m + 1 + 1)) ≠ (ring (2 * m + 1 + 1)).zero := PowerSum.one_ne_zero' (2 * m + 1)

theorem sub_ne (x : Zp (2 * m + 1 + 1)) (h1 : x ≠ (ring (2 * m + 1 + 1)).one) : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x) ≠ (ring (2 * m + 1 + 1)).zero := fun e => h1 (by
  calc x = (ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x)) := by rw [e]; lib_ring
    _ = (ring (2 * m + 1 + 1)).one := by lib_ring)

theorem tau_gen (x : Zp (2 * m + 1 + 1)) (h0 : x ≠ (ring (2 * m + 1 + 1)).zero) (h1 : x ≠ (ring (2 * m + 1 + 1)).one) : tau m x = iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x)) := by
  unfold tau; rw [ite_neg' _ _ h0, ite_neg' _ _ h1]

theorem tau_ne0 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x : Zp (2 * m + 1 + 1)) (h0 : x ≠ (ring (2 * m + 1 + 1)).zero) (h1 : x ≠ (ring (2 * m + 1 + 1)).one) : tau m x ≠ (ring (2 * m + 1 + 1)).zero := by
  rw [tau_gen m x h0 h1]; exact iv_nz m hpr _ (sub_ne m x h1)

theorem tau_ne1 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x : Zp (2 * m + 1 + 1)) (h0 : x ≠ (ring (2 * m + 1 + 1)).zero) (h1 : x ≠ (ring (2 * m + 1 + 1)).one) : tau m x ≠ (ring (2 * m + 1 + 1)).one := by
  rw [tau_gen m x h0 h1]; intro e
  have := iv_mul_self m hpr _ (sub_ne m x h1)
  rw [e] at this
  exact h0 (by calc x = (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x)))) := by lib_ring
    _ = (ring (2 * m + 1 + 1)).zero := by rw [this]; lib_ring)

theorem tau3 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x : Zp (2 * m + 1 + 1)) : tau m (tau m (tau m x)) = x := by
  by_cases h0 : x = (ring (2 * m + 1 + 1)).zero
  · have : tau m x = x := by unfold tau; rw [ite_pos' _ _ h0]
    rw [this, this, this]
  by_cases h1 : x = (ring (2 * m + 1 + 1)).one
  · have : tau m x = x := by unfold tau; rw [ite_neg' _ _ h0, ite_pos' _ _ h1]
    rw [this, this, this]
  have y0 := tau_ne0 m hpr x h0 h1
  have y1 := tau_ne1 m hpr x h0 h1
  have z0 := tau_ne0 m hpr _ y0 y1
  have z1 := tau_ne1 m hpr _ y0 y1
  rw [tau_gen m _ z0 z1, tau_gen m _ y0 y1, tau_gen m x h0 h1]
  have ex : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x h0
  have eu : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x)) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ (sub_ne m x h1)
  -- 1 − 1/(1 − x) = −x/(1 − x)
  have s1 : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x)))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg x) (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x))) := by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x))) ((ring (2 * m + 1 + 1)).neg (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x)))) := by rw [eu]
      _ = _ := by lib_ring
  rw [s1, inv_mul, inv_inv m hpr]
  -- 1 − (1/(−x))(1 − x) = 1/x
  have s2 : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x)))) = iv m x := by
    rw [show (ring (2 * m + 1 + 1)).neg x = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) x by lib_ring, inv_mul,
      show iv m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one by
        have e1 := iv_mul_self m hpr _ (negnz m _ (one_ne m))
        calc iv m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by lib_ring
          _ = _ := by rw [e1]]
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (iv m x) x) ((ring (2 * m + 1 + 1)).mul (iv m x) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x))) := by
          rw [ex]; lib_ring
      _ = iv m x := by lib_ring
  rw [s2, inv_inv m hpr]

theorem fix_iff (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x : Zp (2 * m + 1 + 1)) : tau m (tau m x) = tau m x → tau m x = x := fun e => by
  calc tau m x = tau m (tau m (tau m x)) := by rw [e, e]
    _ = x := tau3 m hpr x

theorem tau_fixed (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x : Zp (2 * m + 1 + 1)) :
    (1 + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).one)) * delta LibInt.intR (tau m x) x = delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) x) (ri  -- (+59 chars; full line in the Zenodo source)
  by_cases h0 : x = (ring (2 * m + 1 + 1)).zero
  · rw [h0, delta_one m _ _ rfl, delta_zero m _ _ (one_ne m).symm, delta_zero m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) (ring (2 * m + 1 + 1)).zero) (ring (2 *  -- (+69 chars; full line in the Zenodo source)
        calc (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) (ring (2 * m + 1 + 1)).zero) (ring (2 * m + 1 + 1)).one) := by lib_ring
          _ = (ring (2 * m + 1 + 1)).zero := e))]
    generalize delta LibInt.intR (tau m (ring (2 * m + 1 + 1)).zero) (ring (2 * m + 1 + 1)).zero = d; lib_int
  by_cases h1 : x = (ring (2 * m + 1 + 1)).one
  · rw [h1, delta_zero m _ _ (one_ne m), delta_one m _ _ rfl, delta_zero m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) (ring (2 * m + 1 + 1)).one) (ring (2 * m + 1 +  -- (+61 chars; full line in the Zenodo source)
        calc (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) (ring (2 * m + 1 + 1)).one) (ring (2 * m + 1 + 1)).one) := by lib_ring
          _ = (ring (2 * m + 1 + 1)).zero := e))]
    generalize delta LibInt.intR (tau m (ring (2 * m + 1 + 1)).one) (ring (2 * m + 1 + 1)).one = d; lib_int
  rw [delta_zero m _ _ h0, delta_zero m _ _ h1, tau_gen m x h0 h1]
  have eu : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x)) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ (sub_ne m x h1)
  by_cases h : iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x)) = x
  · rw [delta_one m _ _ h, delta_one m _ _ (by
      calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) x) (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m +   -- (+87 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x)))) := by rw [h]
        _ = (ring (2 * m + 1 + 1)).zero := by rw [eu]; lib_ring)]; rfl
  · rw [delta_zero m _ _ h, delta_zero m _ _ (fun e => h (by
      have e2 : (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x)) = (ring (2 * m + 1 + 1)).one := by
        calc _ = (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) x) (ring (2 * m + 1 + 1)).one))) := by lib_ring
          _ = (ring (2 * m + 1 + 1)).one := by rw [e]; lib_ring
      calc iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x)) = (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x))) ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x))) := by rw [e2]; lib_ring
        _ = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x))) := by lib_ring
        _ = x := by rw [eu]; lib_ring))]; rfl

/-- **The law for −3 by counting orbits**: p − 3 − χ(−3) ≡ 0 (mod 3) (τ(x) = 1/(1 − x) has order 3 on 𝔽_p ∖ {0, 1}). -/
theorem mobius_count (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : ∃ k : Int, P m + -3 + -(chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)))) = 3 * k := by
  let H : Zp (2 * m + 1 + 1) → Int := fun x => (1 + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).one)) * (1 + -(delta LibInt.intR (tau m x) x))
  have hH : ∀ x, H (tau m x) = H x := fun x => by
    show (1 + -(delta LibInt.intR (tau m x) (ring (2 * m + 1 + 1)).zero) + -(delta LibInt.intR (tau m x) (ring (2 * m + 1 + 1)).one)) * (1 + -(delta LibInt.intR (tau m (tau m x)) (tau m x)))
      = (1 + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).one)) * (1 + -(delta LibInt.intR (tau m x) x))
    by_cases h0 : x = (ring (2 * m + 1 + 1)).zero
    · have : tau m x = x := by unfold tau; rw [ite_pos' _ _ h0]
      rw [this, this]
    by_cases h1 : x = (ring (2 * m + 1 + 1)).one
    · have : tau m x = x := by unfold tau; rw [ite_neg' _ _ h0, ite_pos' _ _ h1]
      rw [this, this]
    rw [delta_zero m _ _ h0, delta_zero m _ _ h1, delta_zero m _ _ (tau_ne0 m hpr x h0 h1), delta_zero m _ _ (tau_ne1 m hpr x h0 h1)]
    by_cases hf : tau m x = x
    · rw [hf, hf]
    · rw [delta_zero m _ _ hf, delta_zero m _ _ (fun e => hf (fix_iff m hpr x e))]
  have hfix : ∀ x, tau m x = x → H x = 0 := fun x e => by
    show (1 + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).one)) * (1 + -(delta LibInt.intR (tau m x) x)) = 0
    rw [delta_one m _ _ e]; generalize delta LibInt.intR x (ring (2 * m + 1 + 1)).zero = u; generalize delta LibInt.intR x (ring (2 * m + 1 + 1)).one = v; lib_int
  have ho := orbit3 m (tau m) (tau3 m hpr) H hH hfix
  have hval : sumK LibInt.intR H (L m) = P m + -3 + -(chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)))) := by
    rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.add (LibInt.intR.mul 1 1) (LibInt.intR.add (LibInt.intR.mul (-1) (delta LibInt.intR x (ring (2 * m + 1 + 1)).zero)) (LibInt.intR.add (LibInt.intR.mul (-1) (delta LibInt.intR x (ring (2 * m + 1 + 1)).one)) (LibInt.intR.mul (-1) (delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1   -- (+205 chars; full line in the Zenodo source)
      show (1 + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).one)) * (1 + -(delta LibInt.intR (tau m x) x)) = _
      rw [show (1 + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).one)) * (1 + -(delta LibInt.intR (tau m x) x))
          = (1 + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).one)) + -((1 + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).one)) * delta LibInt.intR (tau m x) x) by
            generalize delta LibInt.intR x (ring (2 * m + 1 + 1)).zero = u; generalize delta LibInt.intR x (ring (2 * m + 1 + 1)).one = v; generalize delta LibInt.intR (tau m x) x = w; lib_int,
        tau_fixed m hpr x]
      show _ = 1 * 1 + (-1 * delta LibInt.intR x (ring (2 * m + 1 + 1)).zero + (-1 * delta LibInt.intR x (ring (2 * m + 1 + 1)).one + -1 * delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) x) (ring (2 *   -- (+47 chars; full line in the Zenodo source)
      generalize delta LibInt.intR x (ring (2 * m + 1 + 1)).zero = u; generalize delta LibInt.intR x (ring (2 * m + 1 + 1)).one = v; generalize delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) x) (ring   -- (+65 chars; full line in the Zenodo source)
    rw [sumK_add, sumK_add, sumK_add, ← mul_sumK, ← mul_sumK, ← mul_sumK, ← mul_sumK, sum_one,
      show sumK LibInt.intR (fun x => delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR (ring (2 * m + 1 + 1)).zero,
      show sumK LibInt.intR (fun x => delta LibInt.intR x (ring (2 * m + 1 + 1)).one) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR (ring (2 * m + 1 + 1)).one,
      root_count m hpr (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) (ring (2 * m + 1 + 1)).one (one_ne m)]
    rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).o  -- (+297 chars; full line in the Zenodo source)
    show 1 * P m + (-1 * 1 + (-1 * 1 + -1 * (1 + chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)))))) = _
    generalize chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) = e; lib_int
  exact ⟨sumK LibInt.intR (fun x => H x * Lst m (tau m) x) (L m), by rw [← hval, ho]⟩

end Orbit3

#print axioms Orbit3.min3
#print axioms Orbit3.orbit3
#print axioms Orbit3.tau3
#print axioms Orbit3.fix_iff
#print axioms Orbit3.tau_fixed
#print axioms Orbit3.mobius_count

lang/Orbit4.lean

lean_all/lang/Orbit4.lean

/-
  lang/Orbit4 — T43, cycle 376: counting orbits of an order-4 map by sums (G5 order 4, counting tool).

    m4, m4_mem, m4_le, m4_rot, one_min4
    orbit4   τ⁴ = id, H∘τ = H, H = 0 wherever τ²x = x ⟹ Σ H = 4·Σ H·[x least in its orbit]
             (if τ²x ≠ x the four points x, τx, τ²x, τ³x are distinct: τ³x = x would give τx = τ⁴x... = x)
  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.Orbit5

namespace Orbit4

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum Orbit5
open FiniteRecChain (ite_pos' ite_neg')

def m4 (a b c d : Nat) : Nat := mn a (mn b (mn c d))

theorem m4_mem (a b c d : Nat) : m4 a b c d = a ∨ m4 a b c d = b ∨ m4 a b c d = c ∨ m4 a b c d = d := by
  unfold m4
  rcases mn_or a (mn b (mn c d)) with h | h
  · exact Or.inl h
  rw [h]
  rcases mn_or b (mn c d) with h | h
  · exact Or.inr (Or.inl h)
  rw [h]
  rcases mn_or c d with h | h
  · exact Or.inr (Or.inr (Or.inl h))
  · exact Or.inr (Or.inr (Or.inr h))

theorem m4_le (a b c d : Nat) : m4 a b c d ≤ a ∧ m4 a b c d ≤ b ∧ m4 a b c d ≤ c ∧ m4 a b c d ≤ d := by
  unfold m4
  exact ⟨mn_le_l _ _, Nat.le_trans (mn_le_r _ _) (mn_le_l _ _), Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (mn_le_l _ _)),
    Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (mn_le_r _ _))⟩

theorem m4_rot (a b c d : Nat) : m4 b c d a = m4 a b c d := by
  have l1 := m4_le a b c d
  have l2 := m4_le b c d a
  apply Nat.le_antisymm
  · rcases m4_mem a b c d with h | h | h | h <;> rw [h]
    · exact l2.2.2.2
    · exact l2.1
    · exact l2.2.1
    · exact l2.2.2.1
  · rcases m4_mem b c d a with h | h | h | h <;> rw [h]
    · exact l1.2.1
    · exact l1.2.2.1
    · exact l1.2.2.2
    · exact l1.1

theorem one_min4 (a b c d : Nat) (hab : a ≠ b) (hac : a ≠ c) (had : a ≠ d) (hbc : b ≠ c) (hbd : b ≠ d) (hcd : c ≠ d) :
    eqI a (m4 a b c d) + eqI b (m4 a b c d) + eqI c (m4 a b c d) + eqI d (m4 a b c d) = 1 := by
  unfold eqI
  rcases m4_mem a b c d with h | h | h | h <;> rw [h]
  · rw [ite_pos' _ _ rfl, ite_neg' _ _ (fun e => hab e.symm), ite_neg' _ _ (fun e => hac e.symm), ite_neg' _ _ (fun e => had e.symm)]; rfl
  · rw [ite_neg' _ _ hab, ite_pos' _ _ rfl, ite_neg' _ _ (fun e => hbc e.symm), ite_neg' _ _ (fun e => hbd e.symm)]; rfl
  · rw [ite_neg' _ _ hac, ite_neg' _ _ hbc, ite_pos' _ _ rfl, ite_neg' _ _ (fun e => hcd e.symm)]; rfl
  · rw [ite_neg' _ _ had, ite_neg' _ _ hbd, ite_neg' _ _ hcd, ite_pos' _ _ rfl]; rfl

section General
variable {G : Type} [DecidableEq G]

/-- **Orbits of an order-4 map** with H = 0 on the points fixed by τ². -/
theorem orbit4 (els : List G) (hc : Complete LibInt.intR els) (code : G → Nat) (hinj : ∀ u v, code u = code v → u = v)
    (sg : G → G) (h4 : ∀ x, sg (sg (sg (sg x))) = x) (H : G → Int) (hH : ∀ x, H (sg x) = H x) (hfix : ∀ x, sg (sg x) = x → H x = 0) :
    sumK LibInt.intR H els = 4 * sumK LibInt.intR (fun x => H x * eqI (code x) (m4 (code x) (code (sg x)) (code (sg (sg x))) (code (sg (sg (sg x)))))) els := by
  let Ls : G → Int := fun x => eqI (code x) (m4 (code x) (code (sg x)) (code (sg (sg x))) (code (sg (sg (sg x)))))
  have hpt : ∀ x, H x = H x * Ls x + H x * Ls (sg x) + H x * Ls (sg (sg x)) + H x * Ls (sg (sg (sg x))) := fun x => by
    by_cases hx : sg (sg x) = x
    · rw [hfix x hx]; lib_int
    · have ne : ∀ u v : G, u ≠ v → code u ≠ code v := fun u v h e => h (hinj u v e)
      have shift : ∀ y z : G, y ≠ z → sg y ≠ sg z := fun y z h e => h (by
        calc y = sg (sg (sg (sg y))) := (h4 y).symm
          _ = sg (sg (sg (sg z))) := by rw [e]
          _ = z := h4 z)
      have d01 : sg x ≠ x := fun e => hx (by rw [e, e])
      have d02 : sg (sg x) ≠ x := hx
      have d03 : sg (sg (sg x)) ≠ x := fun e => d01 (by
        calc sg x = sg (sg (sg (sg x))) := by rw [e]
          _ = x := h4 x)
      have d12 := shift _ _ d01
      have d13 := shift _ _ d02
      have d23 := shift _ _ d12
      have hm := one_min4 (code x) (code (sg x)) (code (sg (sg x))) (code (sg (sg (sg x))))
        (ne _ _ (fun e => d01 e.symm)) (ne _ _ (fun e => d02 e.symm)) (ne _ _ (fun e => d03 e.symm))
        (ne _ _ (fun e => d12 e.symm)) (ne _ _ (fun e => d13 e.symm)) (ne _ _ (fun e => d23 e.symm))
      have r1 : Ls (sg x) = eqI (code (sg x)) (m4 (code x) (code (sg x)) (code (sg (sg x))) (code (sg (sg (sg x))))) := by
        show eqI _ (m4 _ _ _ (code (sg (sg (sg (sg x)))))) = _; rw [h4, m4_rot]
      have r2 : Ls (sg (sg x)) = eqI (code (sg (sg x))) (m4 (code x) (code (sg x)) (code (sg (sg x))) (code (sg (sg (sg x))))) := by
        show eqI _ (m4 _ _ (code (sg (sg (sg (sg x))))) (code (sg (sg (sg (sg (sg x))))))) = _; rw [h4, m4_rot, m4_rot]
      have r3 : Ls (sg (sg (sg x))) = eqI (code (sg (sg (sg x)))) (m4 (code x) (code (sg x)) (code (sg (sg x))) (code (sg (sg (sg x))))) := by
        show eqI _ (m4 _ (code (sg (sg (sg (sg x))))) (code (sg (sg (sg (sg (sg x)))))) (code (sg (sg (sg (sg (sg (sg x)))))))) = _
        rw [h4, m4_rot, m4_rot, m4_rot]
      rw [r1, r2, r3]
      show H x = H x * eqI (code x) _ + _ + _ + _
      generalize m4 (code x) (code (sg x)) (code (sg (sg x))) (code (sg (sg (sg x)))) = μ at hm ⊢
      generalize eqI (code x) μ = u0 at hm ⊢; generalize eqI (code (sg x)) μ = u1 at hm ⊢
      generalize eqI (code (sg (sg x))) μ = u2 at hm ⊢; generalize eqI (code (sg (sg (sg x)))) μ = u3 at hm ⊢
      calc H x = H x * (u0 + u1 + u2 + u3) := by rw [hm]; lib_int
        _ = _ := by lib_int
  have q1 : sumK LibInt.intR (fun x => H x * Ls (sg x)) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc sg (fun x => sg (sg (sg x))) (fun x => h4 x) (fun y => h4 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg x) = H (sg x) * Ls (sg x); rw [hH]) _
  have q2 : sumK LibInt.intR (fun x => H x * Ls (sg (sg x))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (sg x)) (fun x => sg (sg x)) (fun x => h4 x) (fun y => h4 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (sg x)) = H (sg (sg x)) * Ls (sg (sg x)); rw [hH, hH]) _
  have q3 : sumK LibInt.intR (fun x => H x * Ls (sg (sg (sg x)))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (sg (sg x))) sg (fun x => h4 x) (fun y => h4 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (sg (sg x))) = H (sg (sg (sg x))) * Ls (sg (sg (sg x))); rw [hH, hH, hH]) _
  rw [sumK_ext LibInt.intR _ _ hpt, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, q1, q2, q3]
  generalize sumK LibInt.intR (fun x => H x * Ls x) els = T
  lib_int

end General

end Orbit4

#print axioms Orbit4.m4_mem
#print axioms Orbit4.m4_le
#print axioms Orbit4.m4_rot
#print axioms Orbit4.one_min4
#print axioms Orbit4.orbit4

lang/Orbit42.lean

lean_all/lang/Orbit42.lean

/-
  lang/Orbit42 — T43, cycle 422: orbits of a free action of Z/4 × Z/2 counted by sums (tool for 8 | #E(𝔽_p)).

    tp f i         f applied i times;  act s t i j y = s^j (t^i y)
    act_comp       act a b (act c d y) = act (c + a) (d + b) y      (s t = t s)
    act_id         act (4i) (2j) y = y                               (t⁴ = s² = id)
    m8, one_min8   the least of eight distinct codes is attained exactly once
    orbit42        t⁴ = s² = id, st = ts, H invariant, H = 0 at points fixed by one of the seven non-identity act i j
                   ⟹ Σ H = 8·Σ H·[x least in its orbit]
  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.OrbitV4
import lang.G5Two

namespace Orbit42

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum Orbit5 Orbit4
open FiniteRecChain (ite_pos' ite_neg')

section General
variable {G : Type}

def tp (f : G → G) : Nat → G → G
  | 0, y => y
  | i + 1, y => f (tp f i y)

def act (s t : G → G) (i j : Nat) (y : G) : G := tp s j (tp t i y)

theorem tp_add (f : G → G) : ∀ a b y, tp f a (tp f b y) = tp f (b + a) y
  | 0, _, _ => rfl
  | a + 1, b, y => by show f (tp f a (tp f b y)) = f (tp f (b + a) y); rw [tp_add f a b y]

theorem tp_comm1 (s t : G → G) (hst : ∀ y, s (t y) = t (s y)) : ∀ i y, s (tp t i y) = tp t i (s y)
  | 0, _ => rfl
  | i + 1, y => by show s (t (tp t i y)) = t (tp t i (s y)); rw [hst, tp_comm1 s t hst i y]

theorem tp_comm (s t : G → G) (hst : ∀ y, s (t y) = t (s y)) : ∀ j i y, tp s j (tp t i y) = tp t i (tp s j y)
  | 0, _, _ => rfl
  | j + 1, i, y => by show s (tp s j (tp t i y)) = tp t i (s (tp s j y)); rw [tp_comm s t hst j i y, tp_comm1 s t hst]

theorem act_comp {s t : G → G} (hst : ∀ y, s (t y) = t (s y)) (a b c d : Nat) (y : G) :
    act s t a b (act s t c d y) = act s t (c + a) (d + b) y := by
  show tp s b (tp t a (tp s d (tp t c y))) = tp s (d + b) (tp t (c + a) y)
  rw [← tp_comm s t hst d a, tp_add, tp_add, tp_comm s t hst]

theorem tp_mul (f : G → G) (k : Nat) (hk : ∀ y, tp f k y = y) : ∀ i y, tp f (k * i) y = y
  | 0, _ => rfl
  | i + 1, y => by rw [show k * (i + 1) = k * i + k by lib_nat, ← tp_add, hk, tp_mul f k hk i y]

theorem act_id {s t : G → G} (ht : ∀ y, tp t 4 y = y) (hs : ∀ y, tp s 2 y = y) (i j : Nat) (y : G) :
    act s t (4 * i) (2 * j) y = y := by
  show tp s (2 * j) (tp t (4 * i) y) = y
  rw [tp_mul t 4 ht, tp_mul s 2 hs]

theorem act_red4 {s t : G → G} {ht : ∀ y, tp t 4 y = y} (k j : Nat) (y : G) : act s t (k + 4) j y = act s t k j y := by
  show tp s j (tp t (k + 4) y) = tp s j (tp t k y)
  rw [← tp_add, ht]

theorem act_red2 {s t : G → G} {hs : ∀ y, tp s 2 y = y} (i k : Nat) (y : G) : act s t i (k + 2) y = act s t i k y := by
  show tp s (k + 2) (tp t i y) = tp s k (tp t i y)
  rw [← tp_add, hs]

def m8 (c0 c1 c2 c3 c4 c5 c6 c7 : Nat) : Nat := mn (m4 c0 c1 c2 c3) (m4 c4 c5 c6 c7)

theorem eqI_ne (a b : Nat) (h : a ≠ b) : eqI a b = 0 := ite_neg' _ _ h

theorem one_min8 (c0 c1 c2 c3 c4 c5 c6 c7 : Nat) (h01 : c0 ≠ c1) (h02 : c0 ≠ c2) (h03 : c0 ≠ c3) (h04 : c0 ≠ c4) (h05 : c0 ≠ c5) (h06 : c0 ≠ c6) (h07 : c0 ≠ c7) (h12 : c1 ≠ c2) (h13 : c1 ≠ c3) (h14 : c1 ≠ c4) (h15 : c1 ≠ c5) (h16 : c1 ≠ c6) (h17 : c1 ≠ c7) (h23 : c2 ≠ c3) (h24 : c2 ≠ c4) (h25 : c2 ≠ c5) (h26 : c2 ≠ c6) (h27 : c2 ≠ c7) (h34 : c3 ≠ c4) (h35 : c3 ≠ c5) (h36 : c3 ≠ c6) (h37 : c3 ≠ c7)  -- (+98 chars; full line in the Zenodo source)
    eqI c0 (m8 c0 c1 c2 c3 c4 c5 c6 c7) + eqI c1 (m8 c0 c1 c2 c3 c4 c5 c6 c7) + eqI c2 (m8 c0 c1 c2 c3 c4 c5 c6 c7) +
    eqI c3 (m8 c0 c1 c2 c3 c4 c5 c6 c7) + eqI c4 (m8 c0 c1 c2 c3 c4 c5 c6 c7) + eqI c5 (m8 c0 c1 c2 c3 c4 c5 c6 c7) +
    eqI c6 (m8 c0 c1 c2 c3 c4 c5 c6 c7) + eqI c7 (m8 c0 c1 c2 c3 c4 c5 c6 c7) = 1 := by
  unfold m8
  rcases mn_or (m4 c0 c1 c2 c3) (m4 c4 c5 c6 c7) with hm | hm <;> rw [hm]
  · have h1 := one_min4 c0 c1 c2 c3 h01 h02 h03 h12 h13 h23
    have z : ∀ c, c ≠ c0 → c ≠ c1 → c ≠ c2 → c ≠ c3 → eqI c (m4 c0 c1 c2 c3) = 0 := fun c n0 n1 n2 n3 => by
      apply eqI_ne
      rcases m4_mem c0 c1 c2 c3 with e | e | e | e <;> rw [e]
      · exact n0
      · exact n1
      · exact n2
      · exact n3
    rw [z c4 (Ne.symm h04) (Ne.symm h14) (Ne.symm h24) (Ne.symm h34), z c5 (Ne.symm h05) (Ne.symm h15) (Ne.symm h25) (Ne.symm h35),
      z c6 (Ne.symm h06) (Ne.symm h16) (Ne.symm h26) (Ne.symm h36), z c7 (Ne.symm h07) (Ne.symm h17) (Ne.symm h27) (Ne.symm h37)]
    rw [← h1]; lib_int
  · have h1 := one_min4 c4 c5 c6 c7 h45 h46 h47 h56 h57 h67
    have z : ∀ c, c ≠ c4 → c ≠ c5 → c ≠ c6 → c ≠ c7 → eqI c (m4 c4 c5 c6 c7) = 0 := fun c n0 n1 n2 n3 => by
      apply eqI_ne
      rcases m4_mem c4 c5 c6 c7 with e | e | e | e <;> rw [e]
      · exact n0
      · exact n1
      · exact n2
      · exact n3
    rw [z c0 h04 h05 h06 h07, z c1 h14 h15 h16 h17, z c2 h24 h25 h26 h27, z c3 h34 h35 h36 h37]
    rw [← h1]; lib_int

theorem act_t {s t : G → G} (i j : Nat) (y : G) : act s t i j (t y) = act s t (i + 1) j y := by
  show tp s j (tp t i (tp t 1 y)) = tp s j (tp t (i + 1) y)
  rw [tp_add]; rw [Nat.add_comm 1 i]

theorem act_s {s t : G → G} (hst : ∀ y, s (t y) = t (s y)) (i j : Nat) (y : G) : act s t i j (s y) = act s t i (j + 1) y := by
  show tp s j (tp t i (tp s 1 y)) = tp s (j + 1) (tp t i y)
  rw [← tp_comm s t hst, tp_add, Nat.add_comm 1 j]

theorem M_t {s t : G → G} (ht : ∀ y, tp t 4 y = y) (code : G → Nat) (x : G) : m8 (code (act s t 0 0 (t x))) (code (act s t 1 0 (t x))) (code (act s t 2 0 (t x))) (code (act s t 3 0 (t x))) (code (act s t 0 1 (t x))) (code (act s t 1 1 (t x))) (code (act s t 2 1 (t x))) (code (act s t 3 1 (t x))) = m8 (code (act s t 0 0 x)) (code (act s t 1 0 x)) (code (act s t 2 0 x)) (code (act s t 3 0 x)) (code   -- (+91 chars; full line in the Zenodo source)
  rw [act_t, act_t, act_t, act_t, act_t, act_t, act_t, act_t]
  rw [act_red4 (ht := ht) 0 0, act_red4 (ht := ht) 0 1]
  show m8 (code (act s t 1 0 x)) (code (act s t 2 0 x)) (code (act s t 3 0 x)) (code (act s t 0 0 x)) (code (act s t 1 1 x)) (code (act s t 2 1 x)) (code (act s t 3 1 x)) (code (act s t 0 1 x)) = _
  unfold m8
  rw [m4_rot (code (act s t 0 0 x)) (code (act s t 1 0 x)) (code (act s t 2 0 x)) (code (act s t 3 0 x)), m4_rot (code (act s t 0 1 x)) (code (act s t 1 1 x)) (code (act s t 2 1 x)) (code (act s t 3 1 x))]

theorem M_s {s t : G → G} (hs : ∀ y, tp s 2 y = y) (hst : ∀ y, s (t y) = t (s y)) (code : G → Nat) (x : G) : m8 (code (act s t 0 0 (s x))) (code (act s t 1 0 (s x))) (code (act s t 2 0 (s x))) (code (act s t 3 0 (s x))) (code (act s t 0 1 (s x))) (code (act s t 1 1 (s x))) (code (act s t 2 1 (s x))) (code (act s t 3 1 (s x))) = m8 (code (act s t 0 0 x)) (code (act s t 1 0 x)) (code (act s t 2 0 x)  -- (+122 chars; full line in the Zenodo source)
  rw [act_s hst, act_s hst, act_s hst, act_s hst, act_s hst, act_s hst, act_s hst, act_s hst]
  rw [act_red2 (hs := hs) 0 0, act_red2 (hs := hs) 1 0, act_red2 (hs := hs) 2 0, act_red2 (hs := hs) 3 0]
  show m8 (code (act s t 0 1 x)) (code (act s t 1 1 x)) (code (act s t 2 1 x)) (code (act s t 3 1 x)) (code (act s t 0 0 x)) (code (act s t 1 0 x)) (code (act s t 2 0 x)) (code (act s t 3 0 x)) = _
  unfold m8
  exact G5Two.mn_comm (m4 (code (act s t 0 0 x)) (code (act s t 1 0 x)) (code (act s t 2 0 x)) (code (act s t 3 0 x))) (m4 (code (act s t 0 1 x)) (code (act s t 1 1 x)) (code (act s t 2 1 x)) (code (act s t 3 1 x)))

theorem orbit42 [DecidableEq G] (els : List G) (hc : Complete LibInt.intR els) (code : G → Nat) (hinj : ∀ u v, code u = code v → u = v)
    (s t : G → G) (hs : ∀ y, tp s 2 y = y) (ht : ∀ y, tp t 4 y = y) (hst : ∀ y, s (t y) = t (s y))
    (H : G → Int) (hHs : ∀ x, H (s x) = H x) (hHt : ∀ x, H (t x) = H x)
    (hfix : ∀ x, (act s t 1 0 x = x ∨ act s t 2 0 x = x ∨ act s t 3 0 x = x ∨ act s t 0 1 x = x ∨ act s t 1 1 x = x ∨ act s t 2 1 x = x ∨ act s t 3 1 x = x) → H x = 0) :
    sumK LibInt.intR H els = 8 * sumK LibInt.intR (fun x => H x * eqI (code x) (m8 (code (act s t 0 0 x)) (code (act s t 1 0 x)) (code (act s t 2 0 x)) (code (act s t 3 0 x)) (code (act s t 0 1 x)) (code (act s t 1 1 x)) (code (act s t 2 1 x)) (code (act s t 3 1 x)))) els := by
  let Lf : G → Int := fun y => eqI (code y) (m8 (code (act s t 0 0 y)) (code (act s t 1 0 y)) (code (act s t 2 0 y)) (code (act s t 3 0 y)) (code (act s t 0 1 y)) (code (act s t 1 1 y)) (code (act s t 2 1 y)) (code (act s t 3 1 y)))
  have hMt : ∀ i y, m8 (code (act s t 0 0 (tp t i y))) (code (act s t 1 0 (tp t i y))) (code (act s t 2 0 (tp t i y))) (code (act s t 3 0 (tp t i y))) (code (act s t 0 1 (tp t i y))) (code (act s t 1 1 (tp t i y))) (code (act s t 2 1 (tp t i y))) (code (act s t 3 1 (tp t i y))) = m8 (code (act s t 0 0 y)) (code (act s t 1 0 y)) (code (act s t 2 0 y)) (code (act s t 3 0 y)) (code (act s t 0 1 y)) (  -- (+82 chars; full line in the Zenodo source)
    induction i with
    | zero => intro y; rfl
    | succ i ih => intro y; show m8 (code (act s t 0 0 (t (tp t i y)))) (code (act s t 1 0 (t (tp t i y)))) (code (act s t 2 0 (t (tp t i y)))) (code (act s t 3 0 (t (tp t i y)))) (code (act s t 0 1 (t (tp t i y)))) (code (act s t 1 1 (t (tp t i y)))) (code (act s t 2 1 (t (tp t i y)))) (code (act s t 3 1 (t (tp t i y)))) = _; rw [M_t ht code, ih]
  have hMs : ∀ j y, m8 (code (act s t 0 0 (tp s j y))) (code (act s t 1 0 (tp s j y))) (code (act s t 2 0 (tp s j y))) (code (act s t 3 0 (tp s j y))) (code (act s t 0 1 (tp s j y))) (code (act s t 1 1 (tp s j y))) (code (act s t 2 1 (tp s j y))) (code (act s t 3 1 (tp s j y))) = m8 (code (act s t 0 0 y)) (code (act s t 1 0 y)) (code (act s t 2 0 y)) (code (act s t 3 0 y)) (code (act s t 0 1 y)) (  -- (+82 chars; full line in the Zenodo source)
    induction j with
    | zero => intro y; rfl
    | succ j ih => intro y; show m8 (code (act s t 0 0 (s (tp s j y)))) (code (act s t 1 0 (s (tp s j y)))) (code (act s t 2 0 (s (tp s j y)))) (code (act s t 3 0 (s (tp s j y)))) (code (act s t 0 1 (s (tp s j y)))) (code (act s t 1 1 (s (tp s j y)))) (code (act s t 2 1 (s (tp s j y)))) (code (act s t 3 1 (s (tp s j y)))) = _; rw [M_s hs hst code, ih]
  have hLact : ∀ i j y, Lf (act s t i j y) = eqI (code (act s t i j y)) (m8 (code (act s t 0 0 y)) (code (act s t 1 0 y)) (code (act s t 2 0 y)) (code (act s t 3 0 y)) (code (act s t 0 1 y)) (code (act s t 1 1 y)) (code (act s t 2 1 y)) (code (act s t 3 1 y))) := fun i j y => by
    show eqI _ (m8 (code (act s t 0 0 (tp s j (tp t i y)))) (code (act s t 1 0 (tp s j (tp t i y)))) (code (act s t 2 0 (tp s j (tp t i y)))) (code (act s t 3 0 (tp s j (tp t i y)))) (code (act s t 0 1 (tp s j (tp t i y)))) (code (act s t 1 1 (tp s j (tp t i y)))) (code (act s t 2 1 (tp s j (tp t i y)))) (code (act s t 3 1 (tp s j (tp t i y))))) = _; rw [hMs, hMt]
  have hHt' : ∀ i y, H (tp t i y) = H y := fun i => by
    induction i with
    | zero => intro y; rfl
    | succ i ih => intro y; show H (t (tp t i y)) = _; rw [hHt, ih]
  have hHs' : ∀ j y, H (tp s j y) = H y := fun j => by
    induction j with
    | zero => intro y; rfl
    | succ j ih => intro y; show H (s (tp s j y)) = _; rw [hHs, ih]
  have hHact : ∀ i j y, H (act s t i j y) = H y := fun i j y => by show H (tp s j (tp t i y)) = _; rw [hHs', hHt']
  have hpt : ∀ x, H x = H x * Lf (act s t 0 0 x) + H x * Lf (act s t 1 0 x) + H x * Lf (act s t 2 0 x) + H x * Lf (act s t 3 0 x) + H x * Lf (act s t 0 1 x) + H x * Lf (act s t 1 1 x) + H x * Lf (act s t 2 1 x) + H x * Lf (act s t 3 1 x) := fun x => by
    by_cases hx : (act s t 1 0 x = x ∨ act s t 2 0 x = x ∨ act s t 3 0 x = x ∨ act s t 0 1 x = x ∨ act s t 1 1 x = x ∨ act s t 2 1 x = x ∨ act s t 3 1 x = x)
    · rw [hfix x hx]; lib_int
    · rw [hLact 0 0 x, hLact 1 0, hLact 2 0, hLact 3 0, hLact 0 1, hLact 1 1, hLact 2 1, hLact 3 1]
      have d01 : code (act s t 0 0 x) ≠ code (act s t 1 0 x) := fun e => hx (by
        have e1 := congrArg (act s t 3 0) (hinj _ _ e)
        have e2 : act s t 3 0 x = act s t 4 0 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 4 0 x = x from act_id ht hs 1 0 x] at e2
        exact Or.inr (Or.inr (Or.inl e2)))
      have d02 : code (act s t 0 0 x) ≠ code (act s t 2 0 x) := fun e => hx (by
        have e1 := congrArg (act s t 6 0) (hinj _ _ e)
        have e2 : act s t 6 0 x = act s t 8 0 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id ht hs 2 0 x] at e2
        rw [show (6 : Nat) = 2 + 4 from rfl, act_red4 (ht := ht)] at e2; exact Or.inr (Or.inl e2))
      have d03 : code (act s t 0 0 x) ≠ code (act s t 3 0 x) := fun e => hx (by
        have e1 := congrArg (act s t 9 0) (hinj _ _ e)
        have e2 : act s t 9 0 x = act s t 12 0 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 12 0 x = x from act_id ht hs 3 0 x] at e2
        rw [show (9 : Nat) = 5 + 4 from rfl, act_red4 (ht := ht)] at e2; rw [show (5 : Nat) = 1 + 4 from rfl, act_red4 (ht := ht)] at e2; exact Or.inl e2)
      have d04 : code (act s t 0 0 x) ≠ code (act s t 0 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 0 1) (hinj _ _ e)
        have e2 : act s t 0 1 x = act s t 0 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 0 2 x = x from act_id ht hs 0 1 x] at e2
        exact Or.inr (Or.inr (Or.inr (Or.inl e2))))
      have d05 : code (act s t 0 0 x) ≠ code (act s t 1 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 3 1) (hinj _ _ e)
        have e2 : act s t 3 1 x = act s t 4 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 4 2 x = x from act_id ht hs 1 1 x] at e2
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (e2)))))))
      have d06 : code (act s t 0 0 x) ≠ code (act s t 2 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 6 1) (hinj _ _ e)
        have e2 : act s t 6 1 x = act s t 8 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 2 x = x from act_id ht hs 2 1 x] at e2
        rw [show (6 : Nat) = 2 + 4 from rfl, act_red4 (ht := ht)] at e2; exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e2))))))
      have d07 : code (act s t 0 0 x) ≠ code (act s t 3 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 9 1) (hinj _ _ e)
        have e2 : act s t 9 1 x = act s t 12 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 12 2 x = x from act_id ht hs 3 1 x] at e2
        rw [show (9 : Nat) = 5 + 4 from rfl, act_red4 (ht := ht)] at e2; rw [show (5 : Nat) = 1 + 4 from rfl, act_red4 (ht := ht)] at e2; exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e2)))))
      have d12 : code (act s t 1 0 x) ≠ code (act s t 2 0 x) := fun e => hx (by
        have e1 := congrArg (act s t 6 0) (hinj _ _ e)
        have e2 : act s t 7 0 x = act s t 8 0 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id ht hs 2 0 x] at e2
        rw [show (7 : Nat) = 3 + 4 from rfl, act_red4 (ht := ht)] at e2; exact Or.inr (Or.inr (Or.inl e2)))
      have d13 : code (act s t 1 0 x) ≠ code (act s t 3 0 x) := fun e => hx (by
        have e1 := congrArg (act s t 9 0) (hinj _ _ e)
        have e2 : act s t 10 0 x = act s t 12 0 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 12 0 x = x from act_id ht hs 3 0 x] at e2
        rw [show (10 : Nat) = 6 + 4 from rfl, act_red4 (ht := ht)] at e2; rw [show (6 : Nat) = 2 + 4 from rfl, act_red4 (ht := ht)] at e2; exact Or.inr (Or.inl e2))
      have d14 : code (act s t 1 0 x) ≠ code (act s t 0 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 0 1) (hinj _ _ e)
        have e2 : act s t 1 1 x = act s t 0 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 0 2 x = x from act_id ht hs 0 1 x] at e2
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e2)))))
      have d15 : code (act s t 1 0 x) ≠ code (act s t 1 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 3 1) (hinj _ _ e)
        have e2 : act s t 4 1 x = act s t 4 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 4 2 x = x from act_id ht hs 1 1 x] at e2
        rw [show (4 : Nat) = 0 + 4 from rfl, act_red4 (ht := ht)] at e2; exact Or.inr (Or.inr (Or.inr (Or.inl e2))))
      have d16 : code (act s t 1 0 x) ≠ code (act s t 2 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 6 1) (hinj _ _ e)
        have e2 : act s t 7 1 x = act s t 8 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 2 x = x from act_id ht hs 2 1 x] at e2
        rw [show (7 : Nat) = 3 + 4 from rfl, act_red4 (ht := ht)] at e2; exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (e2)))))))
      have d17 : code (act s t 1 0 x) ≠ code (act s t 3 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 9 1) (hinj _ _ e)
        have e2 : act s t 10 1 x = act s t 12 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 12 2 x = x from act_id ht hs 3 1 x] at e2
        rw [show (10 : Nat) = 6 + 4 from rfl, act_red4 (ht := ht)] at e2; rw [show (6 : Nat) = 2 + 4 from rfl, act_red4 (ht := ht)] at e2; exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e2))))))
      have d23 : code (act s t 2 0 x) ≠ code (act s t 3 0 x) := fun e => hx (by
        have e1 := congrArg (act s t 9 0) (hinj _ _ e)
        have e2 : act s t 11 0 x = act s t 12 0 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 12 0 x = x from act_id ht hs 3 0 x] at e2
        rw [show (11 : Nat) = 7 + 4 from rfl, act_red4 (ht := ht)] at e2; rw [show (7 : Nat) = 3 + 4 from rfl, act_red4 (ht := ht)] at e2; exact Or.inr (Or.inr (Or.inl e2)))
      have d24 : code (act s t 2 0 x) ≠ code (act s t 0 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 0 1) (hinj _ _ e)
        have e2 : act s t 2 1 x = act s t 0 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 0 2 x = x from act_id ht hs 0 1 x] at e2
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e2))))))
      have d25 : code (act s t 2 0 x) ≠ code (act s t 1 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 3 1) (hinj _ _ e)
        have e2 : act s t 5 1 x = act s t 4 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 4 2 x = x from act_id ht hs 1 1 x] at e2
        rw [show (5 : Nat) = 1 + 4 from rfl, act_red4 (ht := ht)] at e2; exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e2)))))
      have d26 : code (act s t 2 0 x) ≠ code (act s t 2 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 6 1) (hinj _ _ e)
        have e2 : act s t 8 1 x = act s t 8 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 2 x = x from act_id ht hs 2 1 x] at e2
        rw [show (8 : Nat) = 4 + 4 from rfl, act_red4 (ht := ht)] at e2; rw [show (4 : Nat) = 0 + 4 from rfl, act_red4 (ht := ht)] at e2; exact Or.inr (Or.inr (Or.inr (Or.inl e2))))
      have d27 : code (act s t 2 0 x) ≠ code (act s t 3 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 9 1) (hinj _ _ e)
        have e2 : act s t 11 1 x = act s t 12 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 12 2 x = x from act_id ht hs 3 1 x] at e2
        rw [show (11 : Nat) = 7 + 4 from rfl, act_red4 (ht := ht)] at e2; rw [show (7 : Nat) = 3 + 4 from rfl, act_red4 (ht := ht)] at e2; exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (e2)))))))
      have d34 : code (act s t 3 0 x) ≠ code (act s t 0 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 0 1) (hinj _ _ e)
        have e2 : act s t 3 1 x = act s t 0 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 0 2 x = x from act_id ht hs 0 1 x] at e2
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (e2)))))))
      have d35 : code (act s t 3 0 x) ≠ code (act s t 1 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 3 1) (hinj _ _ e)
        have e2 : act s t 6 1 x = act s t 4 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 4 2 x = x from act_id ht hs 1 1 x] at e2
        rw [show (6 : Nat) = 2 + 4 from rfl, act_red4 (ht := ht)] at e2; exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e2))))))
      have d36 : code (act s t 3 0 x) ≠ code (act s t 2 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 6 1) (hinj _ _ e)
        have e2 : act s t 9 1 x = act s t 8 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 2 x = x from act_id ht hs 2 1 x] at e2
        rw [show (9 : Nat) = 5 + 4 from rfl, act_red4 (ht := ht)] at e2; rw [show (5 : Nat) = 1 + 4 from rfl, act_red4 (ht := ht)] at e2; exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e2)))))
      have d37 : code (act s t 3 0 x) ≠ code (act s t 3 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 9 1) (hinj _ _ e)
        have e2 : act s t 12 1 x = act s t 12 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 12 2 x = x from act_id ht hs 3 1 x] at e2
        rw [show (12 : Nat) = 8 + 4 from rfl, act_red4 (ht := ht)] at e2; rw [show (8 : Nat) = 4 + 4 from rfl, act_red4 (ht := ht)] at e2; rw [show (4 : Nat) = 0 + 4 from rfl, act_red4 (ht := ht)] at e2; exact Or.inr (Or.inr (Or.inr (Or.inl e2))))
      have d45 : code (act s t 0 1 x) ≠ code (act s t 1 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 3 1) (hinj _ _ e)
        have e2 : act s t 3 2 x = act s t 4 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 4 2 x = x from act_id ht hs 1 1 x] at e2
        rw [show (2 : Nat) = 0 + 2 from rfl, act_red2 (hs := hs)] at e2; exact Or.inr (Or.inr (Or.inl e2)))
      have d46 : code (act s t 0 1 x) ≠ code (act s t 2 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 6 1) (hinj _ _ e)
        have e2 : act s t 6 2 x = act s t 8 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 2 x = x from act_id ht hs 2 1 x] at e2
        rw [show (6 : Nat) = 2 + 4 from rfl, act_red4 (ht := ht)] at e2; rw [show (2 : Nat) = 0 + 2 from rfl, act_red2 (hs := hs)] at e2; exact Or.inr (Or.inl e2))
      have d47 : code (act s t 0 1 x) ≠ code (act s t 3 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 9 1) (hinj _ _ e)
        have e2 : act s t 9 2 x = act s t 12 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 12 2 x = x from act_id ht hs 3 1 x] at e2
        rw [show (9 : Nat) = 5 + 4 from rfl, act_red4 (ht := ht)] at e2; rw [show (5 : Nat) = 1 + 4 from rfl, act_red4 (ht := ht)] at e2; rw [show (2 : Nat) = 0 + 2 from rfl, act_red2 (hs := hs)] at e2; exact Or.inl e2)
      have d56 : code (act s t 1 1 x) ≠ code (act s t 2 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 6 1) (hinj _ _ e)
        have e2 : act s t 7 2 x = act s t 8 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 2 x = x from act_id ht hs 2 1 x] at e2
        rw [show (7 : Nat) = 3 + 4 from rfl, act_red4 (ht := ht)] at e2; rw [show (2 : Nat) = 0 + 2 from rfl, act_red2 (hs := hs)] at e2; exact Or.inr (Or.inr (Or.inl e2)))
      have d57 : code (act s t 1 1 x) ≠ code (act s t 3 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 9 1) (hinj _ _ e)
        have e2 : act s t 10 2 x = act s t 12 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 12 2 x = x from act_id ht hs 3 1 x] at e2
        rw [show (10 : Nat) = 6 + 4 from rfl, act_red4 (ht := ht)] at e2; rw [show (6 : Nat) = 2 + 4 from rfl, act_red4 (ht := ht)] at e2; rw [show (2 : Nat) = 0 + 2 from rfl, act_red2 (hs := hs)] at e2; exact Or.inr (Or.inl e2))
      have d67 : code (act s t 2 1 x) ≠ code (act s t 3 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 9 1) (hinj _ _ e)
        have e2 : act s t 11 2 x = act s t 12 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 12 2 x = x from act_id ht hs 3 1 x] at e2
        rw [show (11 : Nat) = 7 + 4 from rfl, act_red4 (ht := ht)] at e2; rw [show (7 : Nat) = 3 + 4 from rfl, act_red4 (ht := ht)] at e2; rw [show (2 : Nat) = 0 + 2 from rfl, act_red2 (hs := hs)] at e2; exact Or.inr (Or.inr (Or.inl e2)))
      have hm := one_min8 _ _ _ _ _ _ _ _ d01 d02 d03 d04 d05 d06 d07 d12 d13 d14 d15 d16 d17 d23 d24 d25 d26 d27 d34 d35 d36 d37 d45 d46 d47 d56 d57 d67
      generalize m8 (code (act s t 0 0 x)) (code (act s t 1 0 x)) (code (act s t 2 0 x)) (code (act s t 3 0 x)) (code (act s t 0 1 x)) (code (act s t 1 1 x)) (code (act s t 2 1 x)) (code (act s t 3 1 x)) = μ at hm ⊢
      calc H x = H x * (eqI (code (act s t 0 0 x)) (μ) + eqI (code (act s t 1 0 x)) (μ) + eqI (code (act s t 2 0 x)) (μ) + eqI (code (act s t 3 0 x)) (μ) + eqI (code (act s t 0 1 x)) (μ) + eqI (code (act s t 1 1 x)) (μ) + eqI (code (act s t 2 1 x)) (μ) + eqI (code (act s t 3 1 x)) (μ)) := by rw [hm]; lib_int
        _ = _ := by lib_int
  have q1 : sumK LibInt.intR (fun x => H x * Lf (act s t 1 0 x)) els = sumK LibInt.intR (fun x => H x * Lf x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (act s t 1 0) (act s t 3 0)
      (fun y => by rw [act_comp hst]; exact act_id ht hs 1 0 y) (fun y => by rw [act_comp hst]; exact act_id ht hs 1 0 y) (fun y => H y * Lf y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Lf (act s t 1 0 x) = H (act s t 1 0 x) * Lf (act s t 1 0 x); rw [hHact]) _
  have q2 : sumK LibInt.intR (fun x => H x * Lf (act s t 2 0 x)) els = sumK LibInt.intR (fun x => H x * Lf x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (act s t 2 0) (act s t 6 0)
      (fun y => by rw [act_comp hst]; exact act_id ht hs 2 0 y) (fun y => by rw [act_comp hst]; exact act_id ht hs 2 0 y) (fun y => H y * Lf y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Lf (act s t 2 0 x) = H (act s t 2 0 x) * Lf (act s t 2 0 x); rw [hHact]) _
  have q3 : sumK LibInt.intR (fun x => H x * Lf (act s t 3 0 x)) els = sumK LibInt.intR (fun x => H x * Lf x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (act s t 3 0) (act s t 9 0)
      (fun y => by rw [act_comp hst]; exact act_id ht hs 3 0 y) (fun y => by rw [act_comp hst]; exact act_id ht hs 3 0 y) (fun y => H y * Lf y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Lf (act s t 3 0 x) = H (act s t 3 0 x) * Lf (act s t 3 0 x); rw [hHact]) _
  have q4 : sumK LibInt.intR (fun x => H x * Lf (act s t 0 1 x)) els = sumK LibInt.intR (fun x => H x * Lf x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (act s t 0 1) (act s t 0 1)
      (fun y => by rw [act_comp hst]; exact act_id ht hs 0 1 y) (fun y => by rw [act_comp hst]; exact act_id ht hs 0 1 y) (fun y => H y * Lf y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Lf (act s t 0 1 x) = H (act s t 0 1 x) * Lf (act s t 0 1 x); rw [hHact]) _
  have q5 : sumK LibInt.intR (fun x => H x * Lf (act s t 1 1 x)) els = sumK LibInt.intR (fun x => H x * Lf x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (act s t 1 1) (act s t 3 1)
      (fun y => by rw [act_comp hst]; exact act_id ht hs 1 1 y) (fun y => by rw [act_comp hst]; exact act_id ht hs 1 1 y) (fun y => H y * Lf y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Lf (act s t 1 1 x) = H (act s t 1 1 x) * Lf (act s t 1 1 x); rw [hHact]) _
  have q6 : sumK LibInt.intR (fun x => H x * Lf (act s t 2 1 x)) els = sumK LibInt.intR (fun x => H x * Lf x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (act s t 2 1) (act s t 6 1)
      (fun y => by rw [act_comp hst]; exact act_id ht hs 2 1 y) (fun y => by rw [act_comp hst]; exact act_id ht hs 2 1 y) (fun y => H y * Lf y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Lf (act s t 2 1 x) = H (act s t 2 1 x) * Lf (act s t 2 1 x); rw [hHact]) _
  have q7 : sumK LibInt.intR (fun x => H x * Lf (act s t 3 1 x)) els = sumK LibInt.intR (fun x => H x * Lf x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (act s t 3 1) (act s t 9 1)
      (fun y => by rw [act_comp hst]; exact act_id ht hs 3 1 y) (fun y => by rw [act_comp hst]; exact act_id ht hs 3 1 y) (fun y => H y * Lf y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Lf (act s t 3 1 x) = H (act s t 3 1 x) * Lf (act s t 3 1 x); rw [hHact]) _
  have q0 : sumK LibInt.intR (fun x => H x * Lf (act s t 0 0 x)) els = sumK LibInt.intR (fun x => H x * Lf x) els := rfl
  rw [sumK_ext LibInt.intR _ _ hpt, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add,
    QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, q0, q1, q2, q3, q4, q5, q6, q7]
  show _ = 8 * sumK LibInt.intR (fun x => H x * Lf x) els
  generalize sumK LibInt.intR (fun x => H x * Lf x) els = T
  lib_int


end General

end Orbit42

#print axioms Orbit42.tp_add
#print axioms Orbit42.act_comp
#print axioms Orbit42.act_id
#print axioms Orbit42.one_min8
#print axioms Orbit42.M_t
#print axioms Orbit42.M_s
#print axioms Orbit42.orbit42

lang/Orbit5.lean

lean_all/lang/Orbit5.lean

/-
  lang/Orbit5 — T43, cycle 356: counting orbits of an order-5 map by sums (the orbit tool needed for G4).

    min_or       min a b = a ∨ min a b = b
    eqI          [a = b] as an integer (Nat equality, by cases)
    one_min5     for pairwise distinct a, b, c, d, e: exactly one of them equals min a (min b (min c (min d e)))
    orbit5       τ⁵ = id, H∘τ = H, H = 0 at fixed points ⟹ Σ_x H(x) = 5·Σ_x H(x)·[x is the least of its orbit]
  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.Orbit3

namespace Orbit5

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum
open FiniteRecChain (ite_pos' ite_neg')

variable (m : Nat)

def mn (a b : Nat) : Nat := if a ≤ b then a else b

theorem mn_or (a b : Nat) : mn a b = a ∨ mn a b = b := by
  unfold mn
  by_cases h : a ≤ b
  · rw [ite_pos' _ _ h]; exact Or.inl rfl
  · rw [ite_neg' _ _ h]; exact Or.inr rfl

theorem mn_le_l (a b : Nat) : mn a b ≤ a := by
  unfold mn
  by_cases h : a ≤ b
  · rw [ite_pos' _ _ h]; exact Nat.le_refl a
  · rw [ite_neg' _ _ h]; exact Nat.le_of_lt (Nat.lt_of_not_le h)

theorem mn_le_r (a b : Nat) : mn a b ≤ b := by
  unfold mn
  by_cases h : a ≤ b
  · rw [ite_pos' _ _ h]; exact h
  · rw [ite_neg' _ _ h]; exact Nat.le_refl b

def eqI (a b : Nat) : Int := if a = b then 1 else 0

def m5 (a b c d e : Nat) : Nat := mn a (mn b (mn c (mn d e)))

/-- **Exactly one of five distinct numbers is the minimum.** -/
theorem one_min5 (a b c d e : Nat) (hab : a ≠ b) (hac : a ≠ c) (had : a ≠ d) (hae : a ≠ e) (hbc : b ≠ c) (hbd : b ≠ d) (hbe : b ≠ e)
    (hcd : c ≠ d) (hce : c ≠ e) (hde : d ≠ e) :
    eqI a (m5 a b c d e) + eqI b (m5 a b c d e) + eqI c (m5 a b c d e) + eqI d (m5 a b c d e) + eqI e (m5 a b c d e) = 1 := by
  unfold eqI
  have hmem : m5 a b c d e = a ∨ m5 a b c d e = b ∨ m5 a b c d e = c ∨ m5 a b c d e = d ∨ m5 a b c d e = e := by
    unfold m5
    rcases mn_or a (mn b (mn c (mn d e))) with h | h
    · exact Or.inl h
    rw [h]
    rcases mn_or b (mn c (mn d e)) with h | h
    · exact Or.inr (Or.inl h)
    rw [h]
    rcases mn_or c (mn d e) with h | h
    · exact Or.inr (Or.inr (Or.inl h))
    rw [h]
    rcases mn_or d e with h | h
    · exact Or.inr (Or.inr (Or.inr (Or.inl h)))
    · exact Or.inr (Or.inr (Or.inr (Or.inr h)))
  rcases hmem with h | h | h | h | h <;> rw [h]
  · rw [ite_pos' _ _ rfl, ite_neg' _ _ (fun e' => hab e'.symm), ite_neg' _ _ (fun e' => hac e'.symm), ite_neg' _ _ (fun e' => had e'.symm), ite_neg' _ _ (fun e' => hae e'.symm)]; rfl
  · rw [ite_neg' _ _ hab, ite_pos' _ _ rfl, ite_neg' _ _ (fun e' => hbc e'.symm), ite_neg' _ _ (fun e' => hbd e'.symm), ite_neg' _ _ (fun e' => hbe e'.symm)]; rfl
  · rw [ite_neg' _ _ hac, ite_neg' _ _ hbc, ite_pos' _ _ rfl, ite_neg' _ _ (fun e' => hcd e'.symm), ite_neg' _ _ (fun e' => hce e'.symm)]; rfl
  · rw [ite_neg' _ _ had, ite_neg' _ _ hbd, ite_neg' _ _ hcd, ite_pos' _ _ rfl, ite_neg' _ _ (fun e' => hde e'.symm)]; rfl
  · rw [ite_neg' _ _ hae, ite_neg' _ _ hbe, ite_neg' _ _ hce, ite_neg' _ _ hde, ite_pos' _ _ rfl]; rfl

theorem m5_le (a b c d e : Nat) : m5 a b c d e ≤ a ∧ m5 a b c d e ≤ b ∧ m5 a b c d e ≤ c ∧ m5 a b c d e ≤ d ∧ m5 a b c d e ≤ e := by
  unfold m5
  refine ⟨mn_le_l _ _, Nat.le_trans (mn_le_r _ _) (mn_le_l _ _), Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (mn_le_l _ _)),
    Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (mn_le_l _ _))),
    Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (mn_le_r _ _)))⟩

theorem m5_mem (a b c d e : Nat) : m5 a b c d e = a ∨ m5 a b c d e = b ∨ m5 a b c d e = c ∨ m5 a b c d e = d ∨ m5 a b c d e = e := by
  unfold m5
  rcases mn_or a (mn b (mn c (mn d e))) with h | h
  · exact Or.inl h
  rw [h]
  rcases mn_or b (mn c (mn d e)) with h | h
  · exact Or.inr (Or.inl h)
  rw [h]
  rcases mn_or c (mn d e) with h | h
  · exact Or.inr (Or.inr (Or.inl h))
  rw [h]
  rcases mn_or d e with h | h
  · exact Or.inr (Or.inr (Or.inr (Or.inl h)))
  · exact Or.inr (Or.inr (Or.inr (Or.inr h)))

/-- the minimum of five numbers does not depend on the cyclic order. -/
theorem m5_rot (a b c d e : Nat) : m5 b c d e a = m5 a b c d e := by
  have l1 := m5_le a b c d e
  have l2 := m5_le b c d e a
  apply Nat.le_antisymm
  · rcases m5_mem a b c d e with h | h | h | h | h <;> rw [h]
    · exact l2.2.2.2.2
    · exact l2.1
    · exact l2.2.1
    · exact l2.2.2.1
    · exact l2.2.2.2.1
  · rcases m5_mem b c d e a with h | h | h | h | h <;> rw [h]
    · exact l1.2.1
    · exact l1.2.2.1
    · exact l1.2.2.2.1
    · exact l1.2.2.2.2
    · exact l1.1

section General
variable {G : Type} [DecidableEq G]

/-- **Orbits of an order-5 map**, on any completely listed type with an injective code: Σ H = 5·Σ H·[x least in its orbit]. -/
theorem orbit5 (els : List G) (hc : Complete LibInt.intR els) (code : G → Nat) (hinj : ∀ u v, code u = code v → u = v)
    (sg : G → G) (h5 : ∀ x, sg (sg (sg (sg (sg (x))))) = x) (H : G → Int) (hH : ∀ x, H (sg x) = H x) (hfix : ∀ x, sg x = x → H x = 0) :
    sumK LibInt.intR H els = 5 * sumK LibInt.intR (fun x => H x * eqI (code x) (m5 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))))) els := by
  let Ls : G → Int := fun x => eqI (code x) (m5 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))))
  have f2 : ∀ x, sg (sg (x)) = x → sg x = x := fun x e => by
    calc sg (x) = sg (sg (sg (sg (sg (sg (x)))))) := by rw [h5]
      _ = x := by rw [e, e, e]
  have f3 : ∀ x, sg (sg (sg (x))) = x → sg x = x := fun x e => by
    calc sg (x) = sg (sg (sg (sg (sg (sg (x)))))) := by rw [h5]
      _ = x := by rw [e, e]
  have f4 : ∀ x, sg (sg (sg (sg (x)))) = x → sg x = x := fun x e => by
    calc sg (x) = sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))))))) := by rw [h5, h5, h5]
      _ = x := by rw [e, e, e, e]
  have hpt : ∀ x, H x = H x * Ls x + H x * Ls (sg (x)) + H x * Ls (sg (sg (x))) + H x * Ls (sg (sg (sg (x)))) + H x * Ls (sg (sg (sg (sg (x))))) := fun x => by
    by_cases hx : sg x = x
    · rw [hfix x hx]; lib_int
    · have ne : ∀ u v : G, u ≠ v → code u ≠ code v := fun u v h e => h (hinj u v e)
      -- the five orbit elements are pairwise distinct
      have d01 : sg (x) ≠ x := hx
      have d02 : sg (sg (x)) ≠ x := fun e => hx (f2 x e)
      have d03 : sg (sg (sg (x))) ≠ x := fun e => hx (f3 x e)
      have d04 : sg (sg (sg (sg (x)))) ≠ x := fun e => hx (f4 x e)
      have shift : ∀ y z : G, y ≠ z → sg y ≠ sg z := fun y z h e => h (by
        calc y = sg (sg (sg (sg (sg (y))))) := (h5 y).symm
          _ = sg (sg (sg (sg (sg (z))))) := by rw [show sg (y) = sg (z) from e]
          _ = z := h5 z)
      have d12 := shift _ _ d01; have d13 := shift _ _ d02; have d14 := shift _ _ d03
      have d23 := shift _ _ d12; have d24 := shift _ _ d13; have d34 := shift _ _ d23
      have hm := one_min5 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x))))))
        (ne _ _ (fun e => d01 e.symm)) (ne _ _ (fun e => d02 e.symm)) (ne _ _ (fun e => d03 e.symm)) (ne _ _ (fun e => d04 e.symm))
        (ne _ _ (fun e => d12 e.symm)) (ne _ _ (fun e => d13 e.symm)) (ne _ _ (fun e => d14 e.symm))
        (ne _ _ (fun e => d23 e.symm)) (ne _ _ (fun e => d24 e.symm)) (ne _ _ (fun e => d34 e.symm))
      have r1 : Ls (sg (x)) = eqI (code (sg (x))) (m5 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x))))))) := by
        show eqI _ (m5 _ _ _ _ (code (sg (sg (sg (sg (sg (x)))))))) = _; rw [h5, m5_rot]
      have r2 : Ls (sg (sg (x))) = eqI (code (sg (sg (x)))) (m5 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x))))))) := by
        show eqI _ (m5 _ _ _ (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x))))))))) = _; rw [h5, m5_rot, m5_rot]
      have r3 : Ls (sg (sg (sg (x)))) = eqI (code (sg (sg (sg (x))))) (m5 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x))))))) := by
        show eqI _ (m5 _ _ (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x)))))))))) = _; rw [h5, m5_rot, m5_rot, m5_rot]
      have r4 : Ls (sg (sg (sg (sg (x))))) = eqI (code (sg (sg (sg (sg (x)))))) (m5 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x))))))) := by
        show eqI _ (m5 _ (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))) = _; rw [h5, m5_rot, m5_rot, m5_rot, m5_rot]
      rw [r1, r2, r3, r4]
      show H x = H x * eqI (code x) _ + _ + _ + _ + _
      generalize m5 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) = μ at hm ⊢
      generalize eqI (code x) μ = u0 at hm ⊢; generalize eqI (code (sg (x))) μ = u1 at hm ⊢
      generalize eqI (code (sg (sg (x)))) μ = u2 at hm ⊢; generalize eqI (code (sg (sg (sg (x))))) μ = u3 at hm ⊢
      generalize eqI (code (sg (sg (sg (sg (x)))))) μ = u4 at hm ⊢
      calc H x = H x * (u0 + u1 + u2 + u3 + u4) := by rw [hm]; lib_int
        _ = _ := by lib_int
  have q1 : sumK LibInt.intR (fun x => H x * Ls (sg (x))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (x)) (fun x => sg (sg (sg (sg (x))))) (fun x => h5 x) (fun y => by show sg (sg (sg (sg (sg y)))) = y; exact h5 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (x)) = H (sg (x)) * Ls (sg (x)); rw [hH]) _
  have q2 : sumK LibInt.intR (fun x => H x * Ls (sg (sg (x)))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (sg (x))) (fun x => sg (sg (sg (x)))) (fun x => h5 x) (fun y => by show sg (sg (sg (sg (sg y)))) = y; exact h5 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (sg (x))) = H (sg (sg (x))) * Ls (sg (sg (x))); rw [hH, hH]) _
  have q3 : sumK LibInt.intR (fun x => H x * Ls (sg (sg (sg (x))))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (sg (sg (x)))) (fun x => sg (sg (x))) (fun x => h5 x) (fun y => by show sg (sg (sg (sg (sg y)))) = y; exact h5 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (sg (sg (x)))) = H (sg (sg (sg (x)))) * Ls (sg (sg (sg (x)))); rw [hH, hH, hH]) _
  have q4 : sumK LibInt.intR (fun x => H x * Ls (sg (sg (sg (sg (x)))))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (sg (sg (sg (x))))) (fun x => sg (x)) (fun x => h5 x) (fun y => by show sg (sg (sg (sg (sg y)))) = y; exact h5 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (sg (sg (sg (x))))) = H (sg (sg (sg (sg (x))))) * Ls (sg (sg (sg (sg (x))))); rw [hH, hH, hH, hH]) _
  rw [sumK_ext LibInt.intR _ _ hpt, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, q1, q2, q3, q4]
  generalize sumK LibInt.intR (fun x => H x * Ls x) els = T
  lib_int

end General

end Orbit5

#print axioms Orbit5.mn_or
#print axioms Orbit5.mn_le_l
#print axioms Orbit5.mn_le_r
#print axioms Orbit5.one_min5
#print axioms Orbit5.m5_le
#print axioms Orbit5.m5_mem
#print axioms Orbit5.m5_rot
#print axioms Orbit5.orbit5

lang/Orbit6.lean

lean_all/lang/Orbit6.lean

/-
  lang/Orbit6 — T43, cycle 377: counting orbits of an order-6 map by sums (G5 order 6, counting tool).

    m6, m6_mem, m6_le, m6_rot   the nested minimum of six numbers and its rotation invariance
    one_min6                    exactly one of six distinct naturals equals their minimum
    orbit6                      τ⁶ = id, H∘τ = H, H = 0 where τ²x = x or τ³x = x ⟹ Σ H = 6·Σ H·[x least in its orbit]
  Generated by a std-lib Python script from the Orbit5 pattern. Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.Orbit5

namespace Orbit6

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum Orbit5
open FiniteRecChain (ite_pos' ite_neg')

def m6 (v0 v1 v2 v3 v4 v5 : Nat) : Nat := mn v0 (mn v1 (mn v2 (mn v3 (mn v4 (v5)))))
theorem m6_mem (v0 v1 v2 v3 v4 v5 : Nat) : m6 v0 v1 v2 v3 v4 v5 = v0 ∨ m6 v0 v1 v2 v3 v4 v5 = v1 ∨ m6 v0 v1 v2 v3 v4 v5 = v2 ∨ m6 v0 v1 v2 v3 v4 v5 = v3 ∨ m6 v0 v1 v2 v3 v4 v5 = v4 ∨ m6 v0 v1 v2 v3 v4 v5 = v5 := by
  unfold m6
  rcases mn_or v0 (mn v1 (mn v2 (mn v3 (mn v4 (v5))))) with h | h
  · exact Or.inl h
  rw [h]
  rcases mn_or v1 (mn v2 (mn v3 (mn v4 (v5)))) with h | h
  · exact Or.inr (Or.inl h)
  rw [h]
  rcases mn_or v2 (mn v3 (mn v4 (v5))) with h | h
  · exact Or.inr (Or.inr (Or.inl h))
  rw [h]
  rcases mn_or v3 (mn v4 (v5)) with h | h
  · exact Or.inr (Or.inr (Or.inr (Or.inl h)))
  rw [h]
  rcases mn_or v4 (v5) with h | h
  · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h))))
  · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr h))))

theorem m6_le (v0 v1 v2 v3 v4 v5 : Nat) : m6 v0 v1 v2 v3 v4 v5 ≤ v0 ∧ m6 v0 v1 v2 v3 v4 v5 ≤ v1 ∧ m6 v0 v1 v2 v3 v4 v5 ≤ v2 ∧ m6 v0 v1 v2 v3 v4 v5 ≤ v3 ∧ m6 v0 v1 v2 v3 v4 v5 ≤ v4 ∧ m6 v0 v1 v2 v3 v4 v5 ≤ v5 := by
  unfold m6
  exact ⟨mn_le_l _ _, Nat.le_trans (mn_le_r _ _) (mn_le_l _ _), Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (mn_le_l _ _)), Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (mn_le_l _ _))), Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (mn_le_l _ _)))), Nat.le_trans (mn_le_r _ _) (Nat.le_t  -- (+92 chars; full line in the Zenodo source)

theorem m6_rot (v0 v1 v2 v3 v4 v5 : Nat) : m6 v1 v2 v3 v4 v5 v0 = m6 v0 v1 v2 v3 v4 v5 := by
  have l1 := m6_le v0 v1 v2 v3 v4 v5
  have l2 := m6_le v1 v2 v3 v4 v5 v0
  apply Nat.le_antisymm
  · rcases m6_mem v0 v1 v2 v3 v4 v5 with h | h | h | h | h | h <;> rw [h]
    · exact l2.2.2.2.2.2
    · exact l2.1
    · exact l2.2.1
    · exact l2.2.2.1
    · exact l2.2.2.2.1
    · exact l2.2.2.2.2.1
  · rcases m6_mem v1 v2 v3 v4 v5 v0 with h | h | h | h | h | h <;> rw [h]
    · exact l1.2.1
    · exact l1.2.2.1
    · exact l1.2.2.2.1
    · exact l1.2.2.2.2.1
    · exact l1.2.2.2.2.2
    · exact l1.1

theorem one_min6 (v0 v1 v2 v3 v4 v5 : Nat) (h01 : v0 ≠ v1) (h02 : v0 ≠ v2) (h03 : v0 ≠ v3) (h04 : v0 ≠ v4) (h05 : v0 ≠ v5) (h12 : v1 ≠ v2) (h13 : v1 ≠ v3) (h14 : v1 ≠ v4) (h15 : v1 ≠ v5) (h23 : v2 ≠ v3) (h24 : v2 ≠ v4) (h25 : v2 ≠ v5) (h34 : v3 ≠ v4) (h35 : v3 ≠ v5) (h45 : v4 ≠ v5) :
    eqI v0 (m6 v0 v1 v2 v3 v4 v5) + eqI v1 (m6 v0 v1 v2 v3 v4 v5) + eqI v2 (m6 v0 v1 v2 v3 v4 v5) + eqI v3 (m6 v0 v1 v2 v3 v4 v5) + eqI v4 (m6 v0 v1 v2 v3 v4 v5) + eqI v5 (m6 v0 v1 v2 v3 v4 v5) = 1 := by
  unfold eqI
  rcases m6_mem v0 v1 v2 v3 v4 v5 with h | h | h | h | h | h <;> rw [h]
  · rw [ite_pos' _ _ rfl, ite_neg' _ _ (fun e' => h01 e'.symm), ite_neg' _ _ (fun e' => h02 e'.symm), ite_neg' _ _ (fun e' => h03 e'.symm), ite_neg' _ _ (fun e' => h04 e'.symm), ite_neg' _ _ (fun e' => h05 e'.symm)]; rfl
  · rw [ite_neg' _ _ h01, ite_pos' _ _ rfl, ite_neg' _ _ (fun e' => h12 e'.symm), ite_neg' _ _ (fun e' => h13 e'.symm), ite_neg' _ _ (fun e' => h14 e'.symm), ite_neg' _ _ (fun e' => h15 e'.symm)]; rfl
  · rw [ite_neg' _ _ h02, ite_neg' _ _ h12, ite_pos' _ _ rfl, ite_neg' _ _ (fun e' => h23 e'.symm), ite_neg' _ _ (fun e' => h24 e'.symm), ite_neg' _ _ (fun e' => h25 e'.symm)]; rfl
  · rw [ite_neg' _ _ h03, ite_neg' _ _ h13, ite_neg' _ _ h23, ite_pos' _ _ rfl, ite_neg' _ _ (fun e' => h34 e'.symm), ite_neg' _ _ (fun e' => h35 e'.symm)]; rfl
  · rw [ite_neg' _ _ h04, ite_neg' _ _ h14, ite_neg' _ _ h24, ite_neg' _ _ h34, ite_pos' _ _ rfl, ite_neg' _ _ (fun e' => h45 e'.symm)]; rfl
  · rw [ite_neg' _ _ h05, ite_neg' _ _ h15, ite_neg' _ _ h25, ite_neg' _ _ h35, ite_neg' _ _ h45, ite_pos' _ _ rfl]; rfl

section General
variable {G : Type} [DecidableEq G]

/-- **Orbits of an order-6 map** on any completely listed type with an injective code. -/
theorem orbit6 (els : List G) (hc : Complete LibInt.intR els) (code : G → Nat) (hinj : ∀ u v, code u = code v → u = v)
    (sg : G → G) (h6 : ∀ x, sg (sg (sg (sg (sg (sg (x)))))) = x) (H : G → Int) (hH : ∀ x, H (sg x) = H x) (hfix : ∀ x, (sg (sg (x)) = x ∨ sg (sg (sg (x))) = x) → H x = 0) :
    sumK LibInt.intR H els = 6 * sumK LibInt.intR (fun x => H x * eqI (code x) (m6 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))))) els := by
  let Ls : G → Int := fun x => eqI (code x) (m6 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))))
  have hpt : ∀ x, H x = H x * Ls (x) + H x * Ls (sg (x)) + H x * Ls (sg (sg (x))) + H x * Ls (sg (sg (sg (x)))) + H x * Ls (sg (sg (sg (sg (x))))) + H x * Ls (sg (sg (sg (sg (sg (x)))))) := fun x => by
    by_cases hx : (sg (sg (x)) = x ∨ sg (sg (sg (x))) = x)
    · rw [hfix x hx]; lib_int
    · have ne : ∀ u v : G, u ≠ v → code u ≠ code v := fun u v h e => h (hinj u v e)
      have hx2 : sg (sg (x)) ≠ x := fun e => hx (Or.inl e)
      have hx3 : sg (sg (sg (x))) ≠ x := fun e => hx (Or.inr e)
      have shift : ∀ y z : G, y ≠ z → sg y ≠ sg z := fun y z h e => h (by
        calc y = sg (sg (sg (sg (sg (sg (y)))))) := (h6 y).symm
          _ = sg (sg (sg (sg (sg (sg (z)))))) := by rw [show sg (y) = sg (z) from e]
          _ = z := h6 z)
      have d01 : sg (x) ≠ x := fun e => hx2 (by rw [e, e])
      have d02 : sg (sg (x)) ≠ x := hx2
      have d03 : sg (sg (sg (x))) ≠ x := hx3
      have d04 : sg (sg (sg (sg (x)))) ≠ x := fun e => hx2 (by
        calc sg (sg (x)) = sg (sg (sg (sg (sg (sg (x)))))) := by rw [e]
          _ = x := h6 x)
      have d05 : sg (sg (sg (sg (sg (x))))) ≠ x := fun e => d01 (by
        calc sg (x) = sg (sg (sg (sg (sg (sg (x)))))) := by rw [e]
          _ = x := h6 x)
      have d12 := shift _ _ d01
      have d13 := shift _ _ d02
      have d14 := shift _ _ d03
      have d15 := shift _ _ d04
      have d23 := shift _ _ d12
      have d24 := shift _ _ d13
      have d25 := shift _ _ d14
      have d34 := shift _ _ d23
      have d35 := shift _ _ d24
      have d45 := shift _ _ d34
      have hm := one_min6 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x)))))))
        (ne _ _ (fun e => d01 e.symm)) (ne _ _ (fun e => d02 e.symm)) (ne _ _ (fun e => d03 e.symm)) (ne _ _ (fun e => d04 e.symm)) (ne _ _ (fun e => d05 e.symm)) (ne _ _ (fun e => d12 e.symm)) (ne _ _ (fun e => d13 e.symm)) (ne _ _ (fun e => d14 e.symm)) (ne _ _ (fun e => d15 e.symm)) (ne _ _ (fun e => d23 e.symm)) (ne _ _ (fun e => d24 e.symm)) (ne _ _ (fun e => d25 e.symm)) (ne _ _ (fun e => d3  -- (+72 chars; full line in the Zenodo source)
      have r1 : Ls (sg (x)) = eqI (code (sg (x))) (m6 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x)))))))) := by
        show eqI _ (m6 (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x))))))))) = _
        rw [h6, m6_rot]
      have r2 : Ls (sg (sg (x))) = eqI (code (sg (sg (x)))) (m6 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x)))))))) := by
        show eqI _ (m6 (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x)))))))))) = _
        rw [h6, m6_rot, m6_rot]
      have r3 : Ls (sg (sg (sg (x)))) = eqI (code (sg (sg (sg (x))))) (m6 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x)))))))) := by
        show eqI _ (m6 (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))) = _
        rw [h6, m6_rot, m6_rot, m6_rot]
      have r4 : Ls (sg (sg (sg (sg (x))))) = eqI (code (sg (sg (sg (sg (x)))))) (m6 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x)))))))) := by
        show eqI _ (m6 (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))) = _
        rw [h6, m6_rot, m6_rot, m6_rot, m6_rot]
      have r5 : Ls (sg (sg (sg (sg (sg (x)))))) = eqI (code (sg (sg (sg (sg (sg (x))))))) (m6 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x)))))))) := by
        show eqI _ (m6 (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))))) = _
        rw [h6, m6_rot, m6_rot, m6_rot, m6_rot, m6_rot]
      rw [r1, r2, r3, r4, r5]
      show H x = H x * eqI (code x) _ + _ + _ + _ + _ + _
      generalize m6 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) = μ at hm ⊢
      generalize eqI (code x) μ = u0 at hm ⊢
      generalize eqI (code (sg (x))) μ = u1 at hm ⊢
      generalize eqI (code (sg (sg (x)))) μ = u2 at hm ⊢
      generalize eqI (code (sg (sg (sg (x))))) μ = u3 at hm ⊢
      generalize eqI (code (sg (sg (sg (sg (x)))))) μ = u4 at hm ⊢
      generalize eqI (code (sg (sg (sg (sg (sg (x))))))) μ = u5 at hm ⊢
      calc H x = H x * (u0 + u1 + u2 + u3 + u4 + u5) := by rw [hm]; lib_int
        _ = _ := by lib_int
  have q1 : sumK LibInt.intR (fun x => H x * Ls (sg (x))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (x)) (fun x => sg (sg (sg (sg (sg (x)))))) (fun x => h6 x) (fun y => by show sg (sg (sg (sg (sg (sg y))))) = y; exact h6 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (x)) = H (sg (x)) * Ls (sg (x)); rw [hH]) _
  have q2 : sumK LibInt.intR (fun x => H x * Ls (sg (sg (x)))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (sg (x))) (fun x => sg (sg (sg (sg (x))))) (fun x => h6 x) (fun y => by show sg (sg (sg (sg (sg (sg y))))) = y; exact h6 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (sg (x))) = H (sg (sg (x))) * Ls (sg (sg (x))); rw [hH, hH]) _
  have q3 : sumK LibInt.intR (fun x => H x * Ls (sg (sg (sg (x))))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (sg (sg (x)))) (fun x => sg (sg (sg (x)))) (fun x => h6 x) (fun y => by show sg (sg (sg (sg (sg (sg y))))) = y; exact h6 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (sg (sg (x)))) = H (sg (sg (sg (x)))) * Ls (sg (sg (sg (x)))); rw [hH, hH, hH]) _
  have q4 : sumK LibInt.intR (fun x => H x * Ls (sg (sg (sg (sg (x)))))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (sg (sg (sg (x))))) (fun x => sg (sg (x))) (fun x => h6 x) (fun y => by show sg (sg (sg (sg (sg (sg y))))) = y; exact h6 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (sg (sg (sg (x))))) = H (sg (sg (sg (sg (x))))) * Ls (sg (sg (sg (sg (x))))); rw [hH, hH, hH, hH]) _
  have q5 : sumK LibInt.intR (fun x => H x * Ls (sg (sg (sg (sg (sg (x))))))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (sg (sg (sg (sg (x)))))) (fun x => sg (x)) (fun x => h6 x) (fun y => by show sg (sg (sg (sg (sg (sg y))))) = y; exact h6 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (sg (sg (sg (sg (x)))))) = H (sg (sg (sg (sg (sg (x)))))) * Ls (sg (sg (sg (sg (sg (x)))))); rw [hH, hH, hH, hH, hH]) _
  rw [sumK_ext LibInt.intR _ _ hpt, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, q1, q2, q3, q4, q5]
  generalize sumK LibInt.intR (fun x => H x * Ls x) els = T
  lib_int

end General

end Orbit6

#print axioms Orbit6.m6_mem
#print axioms Orbit6.m6_le
#print axioms Orbit6.m6_rot
#print axioms Orbit6.one_min6
#print axioms Orbit6.orbit6

lang/Orbit7.lean

lean_all/lang/Orbit7.lean

/-
  lang/Orbit7 — T43, cycle 372: counting orbits of an order-7 map by sums (G5 order 7, counting tool).

    m7, m7_mem, m7_le, m7_rot   the nested minimum of seven numbers and its rotation invariance
    one_min7                    exactly one of seven distinct naturals equals their minimum
    orbit7                      τ⁷ = id, H∘τ = H, H = 0 at fixed points ⟹ Σ H = 7·Σ H·[x least in its orbit]
                                (τ^d x = x with 0 < d < 7 forces τx = x since 7 is prime: τx = τ^(de)x with de ≡ 1 mod 7)
  Generated by a std-lib Python script from the Orbit5 pattern. Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.Orbit5

namespace Orbit7

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum Orbit5
open FiniteRecChain (ite_pos' ite_neg')

def m7 (v0 v1 v2 v3 v4 v5 v6 : Nat) : Nat := mn v0 (mn v1 (mn v2 (mn v3 (mn v4 (mn v5 (v6))))))
theorem m7_mem (v0 v1 v2 v3 v4 v5 v6 : Nat) : m7 v0 v1 v2 v3 v4 v5 v6 = v0 ∨ m7 v0 v1 v2 v3 v4 v5 v6 = v1 ∨ m7 v0 v1 v2 v3 v4 v5 v6 = v2 ∨ m7 v0 v1 v2 v3 v4 v5 v6 = v3 ∨ m7 v0 v1 v2 v3 v4 v5 v6 = v4 ∨ m7 v0 v1 v2 v3 v4 v5 v6 = v5 ∨ m7 v0 v1 v2 v3 v4 v5 v6 = v6 := by
  unfold m7
  rcases mn_or v0 (mn v1 (mn v2 (mn v3 (mn v4 (mn v5 (v6)))))) with h | h
  · exact Or.inl h
  rw [h]
  rcases mn_or v1 (mn v2 (mn v3 (mn v4 (mn v5 (v6))))) with h | h
  · exact Or.inr (Or.inl h)
  rw [h]
  rcases mn_or v2 (mn v3 (mn v4 (mn v5 (v6)))) with h | h
  · exact Or.inr (Or.inr (Or.inl h))
  rw [h]
  rcases mn_or v3 (mn v4 (mn v5 (v6))) with h | h
  · exact Or.inr (Or.inr (Or.inr (Or.inl h)))
  rw [h]
  rcases mn_or v4 (mn v5 (v6)) with h | h
  · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h))))
  rw [h]
  rcases mn_or v5 (v6) with h | h
  · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h)))))
  · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr h)))))

theorem m7_le (v0 v1 v2 v3 v4 v5 v6 : Nat) : m7 v0 v1 v2 v3 v4 v5 v6 ≤ v0 ∧ m7 v0 v1 v2 v3 v4 v5 v6 ≤ v1 ∧ m7 v0 v1 v2 v3 v4 v5 v6 ≤ v2 ∧ m7 v0 v1 v2 v3 v4 v5 v6 ≤ v3 ∧ m7 v0 v1 v2 v3 v4 v5 v6 ≤ v4 ∧ m7 v0 v1 v2 v3 v4 v5 v6 ≤ v5 ∧ m7 v0 v1 v2 v3 v4 v5 v6 ≤ v6 := by
  unfold m7
  exact ⟨mn_le_l _ _, Nat.le_trans (mn_le_r _ _) (mn_le_l _ _), Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (mn_le_l _ _)), Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (mn_le_l _ _))), Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (mn_le_l _ _)))), Nat.le_trans (mn_le_r _ _) (Nat.le_t  -- (+279 chars; full line in the Zenodo source)

theorem m7_rot (v0 v1 v2 v3 v4 v5 v6 : Nat) : m7 v1 v2 v3 v4 v5 v6 v0 = m7 v0 v1 v2 v3 v4 v5 v6 := by
  have l1 := m7_le v0 v1 v2 v3 v4 v5 v6
  have l2 := m7_le v1 v2 v3 v4 v5 v6 v0
  apply Nat.le_antisymm
  · rcases m7_mem v0 v1 v2 v3 v4 v5 v6 with h | h | h | h | h | h | h <;> rw [h]
    · exact l2.2.2.2.2.2.2
    · exact l2.1
    · exact l2.2.1
    · exact l2.2.2.1
    · exact l2.2.2.2.1
    · exact l2.2.2.2.2.1
    · exact l2.2.2.2.2.2.1
  · rcases m7_mem v1 v2 v3 v4 v5 v6 v0 with h | h | h | h | h | h | h <;> rw [h]
    · exact l1.2.1
    · exact l1.2.2.1
    · exact l1.2.2.2.1
    · exact l1.2.2.2.2.1
    · exact l1.2.2.2.2.2.1
    · exact l1.2.2.2.2.2.2
    · exact l1.1

theorem one_min7 (v0 v1 v2 v3 v4 v5 v6 : Nat) (h01 : v0 ≠ v1) (h02 : v0 ≠ v2) (h03 : v0 ≠ v3) (h04 : v0 ≠ v4) (h05 : v0 ≠ v5) (h06 : v0 ≠ v6) (h12 : v1 ≠ v2) (h13 : v1 ≠ v3) (h14 : v1 ≠ v4) (h15 : v1 ≠ v5) (h16 : v1 ≠ v6) (h23 : v2 ≠ v3) (h24 : v2 ≠ v4) (h25 : v2 ≠ v5) (h26 : v2 ≠ v6) (h34 : v3 ≠ v4) (h35 : v3 ≠ v5) (h36 : v3 ≠ v6) (h45 : v4 ≠ v5) (h46 : v4 ≠ v6) (h56 : v5 ≠ v6) :
    eqI v0 (m7 v0 v1 v2 v3 v4 v5 v6) + eqI v1 (m7 v0 v1 v2 v3 v4 v5 v6) + eqI v2 (m7 v0 v1 v2 v3 v4 v5 v6) + eqI v3 (m7 v0 v1 v2 v3 v4 v5 v6) + eqI v4 (m7 v0 v1 v2 v3 v4 v5 v6) + eqI v5 (m7 v0 v1 v2 v3 v4 v5 v6) + eqI v6 (m7 v0 v1 v2 v3 v4 v5 v6) = 1 := by
  unfold eqI
  rcases m7_mem v0 v1 v2 v3 v4 v5 v6 with h | h | h | h | h | h | h <;> rw [h]
  · rw [ite_pos' _ _ rfl, ite_neg' _ _ (fun e' => h01 e'.symm), ite_neg' _ _ (fun e' => h02 e'.symm), ite_neg' _ _ (fun e' => h03 e'.symm), ite_neg' _ _ (fun e' => h04 e'.symm), ite_neg' _ _ (fun e' => h05 e'.symm), ite_neg' _ _ (fun e' => h06 e'.symm)]; rfl
  · rw [ite_neg' _ _ h01, ite_pos' _ _ rfl, ite_neg' _ _ (fun e' => h12 e'.symm), ite_neg' _ _ (fun e' => h13 e'.symm), ite_neg' _ _ (fun e' => h14 e'.symm), ite_neg' _ _ (fun e' => h15 e'.symm), ite_neg' _ _ (fun e' => h16 e'.symm)]; rfl
  · rw [ite_neg' _ _ h02, ite_neg' _ _ h12, ite_pos' _ _ rfl, ite_neg' _ _ (fun e' => h23 e'.symm), ite_neg' _ _ (fun e' => h24 e'.symm), ite_neg' _ _ (fun e' => h25 e'.symm), ite_neg' _ _ (fun e' => h26 e'.symm)]; rfl
  · rw [ite_neg' _ _ h03, ite_neg' _ _ h13, ite_neg' _ _ h23, ite_pos' _ _ rfl, ite_neg' _ _ (fun e' => h34 e'.symm), ite_neg' _ _ (fun e' => h35 e'.symm), ite_neg' _ _ (fun e' => h36 e'.symm)]; rfl
  · rw [ite_neg' _ _ h04, ite_neg' _ _ h14, ite_neg' _ _ h24, ite_neg' _ _ h34, ite_pos' _ _ rfl, ite_neg' _ _ (fun e' => h45 e'.symm), ite_neg' _ _ (fun e' => h46 e'.symm)]; rfl
  · rw [ite_neg' _ _ h05, ite_neg' _ _ h15, ite_neg' _ _ h25, ite_neg' _ _ h35, ite_neg' _ _ h45, ite_pos' _ _ rfl, ite_neg' _ _ (fun e' => h56 e'.symm)]; rfl
  · rw [ite_neg' _ _ h06, ite_neg' _ _ h16, ite_neg' _ _ h26, ite_neg' _ _ h36, ite_neg' _ _ h46, ite_neg' _ _ h56, ite_pos' _ _ rfl]; rfl

section General
variable {G : Type} [DecidableEq G]

/-- **Orbits of an order-7 map** on any completely listed type with an injective code. -/
theorem orbit7 (els : List G) (hc : Complete LibInt.intR els) (code : G → Nat) (hinj : ∀ u v, code u = code v → u = v)
    (sg : G → G) (h7 : ∀ x, sg (sg (sg (sg (sg (sg (sg (x))))))) = x) (H : G → Int) (hH : ∀ x, H (sg x) = H x) (hfix : ∀ x, sg x = x → H x = 0) :
    sumK LibInt.intR H els = 7 * sumK LibInt.intR (fun x => H x * eqI (code x) (m7 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))))) els := by
  let Ls : G → Int := fun x => eqI (code x) (m7 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))))
  have hpt : ∀ x, H x = H x * Ls (x) + H x * Ls (sg (x)) + H x * Ls (sg (sg (x))) + H x * Ls (sg (sg (sg (x)))) + H x * Ls (sg (sg (sg (sg (x))))) + H x * Ls (sg (sg (sg (sg (sg (x)))))) + H x * Ls (sg (sg (sg (sg (sg (sg (x))))))) := fun x => by
    by_cases hx : sg x = x
    · rw [hfix x hx]; lib_int
    · have ne : ∀ u v : G, u ≠ v → code u ≠ code v := fun u v h e => h (hinj u v e)
      have f2 : ∀ z, sg (sg (z)) = z → sg z = z := fun z hz => by
        calc sg z = sg (sg (sg (sg (sg (sg (sg (sg (z)))))))) := by rw [h7]
          _ = z := by rw [hz, hz, hz, hz]
      have f3 : ∀ z, sg (sg (sg (z))) = z → sg z = z := fun z hz => by
        calc sg z = sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (z))))))))))))))) := by rw [h7, h7]
          _ = z := by rw [hz, hz, hz, hz, hz]
      have f4 : ∀ z, sg (sg (sg (sg (z)))) = z → sg z = z := fun z hz => by
        calc sg z = sg (sg (sg (sg (sg (sg (sg (sg (z)))))))) := by rw [h7]
          _ = z := by rw [hz, hz]
      have f5 : ∀ z, sg (sg (sg (sg (sg (z))))) = z → sg z = z := fun z hz => by
        calc sg z = sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (z))))))))))))))) := by rw [h7, h7]
          _ = z := by rw [hz, hz, hz]
      have f6 : ∀ z, sg (sg (sg (sg (sg (sg (z)))))) = z → sg z = z := fun z hz => by
        calc sg z = sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (z)))))))))))))))))))))))))))))))))))) := by rw [h7, h7, h7, h7, h7]
          _ = z := by rw [hz, hz, hz, hz, hz, hz]
      have shift : ∀ y z : G, y ≠ z → sg y ≠ sg z := fun y z h e => h (by
        calc y = sg (sg (sg (sg (sg (sg (sg (y))))))) := (h7 y).symm
          _ = sg (sg (sg (sg (sg (sg (sg (z))))))) := by rw [show sg (y) = sg (z) from e]
          _ = z := h7 z)
      have d01 : sg (x) ≠ x := hx
      have d02 : sg (sg (x)) ≠ x := fun e => hx (f2 x e)
      have d03 : sg (sg (sg (x))) ≠ x := fun e => hx (f3 x e)
      have d04 : sg (sg (sg (sg (x)))) ≠ x := fun e => hx (f4 x e)
      have d05 : sg (sg (sg (sg (sg (x))))) ≠ x := fun e => hx (f5 x e)
      have d06 : sg (sg (sg (sg (sg (sg (x)))))) ≠ x := fun e => hx (f6 x e)
      have d12 := shift _ _ d01
      have d13 := shift _ _ d02
      have d14 := shift _ _ d03
      have d15 := shift _ _ d04
      have d16 := shift _ _ d05
      have d23 := shift _ _ d12
      have d24 := shift _ _ d13
      have d25 := shift _ _ d14
      have d26 := shift _ _ d15
      have d34 := shift _ _ d23
      have d35 := shift _ _ d24
      have d36 := shift _ _ d25
      have d45 := shift _ _ d34
      have d46 := shift _ _ d35
      have d56 := shift _ _ d45
      have hm := one_min7 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x))))))))
        (ne _ _ (fun e => d01 e.symm)) (ne _ _ (fun e => d02 e.symm)) (ne _ _ (fun e => d03 e.symm)) (ne _ _ (fun e => d04 e.symm)) (ne _ _ (fun e => d05 e.symm)) (ne _ _ (fun e => d06 e.symm)) (ne _ _ (fun e => d12 e.symm)) (ne _ _ (fun e => d13 e.symm)) (ne _ _ (fun e => d14 e.symm)) (ne _ _ (fun e => d15 e.symm)) (ne _ _ (fun e => d16 e.symm)) (ne _ _ (fun e => d23 e.symm)) (ne _ _ (fun e => d2  -- (+258 chars; full line in the Zenodo source)
      have r1 : Ls (sg (x)) = eqI (code (sg (x))) (m7 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x))))))))) := by
        show eqI _ (m7 (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x)))))))))) = _
        rw [h7, m7_rot]
      have r2 : Ls (sg (sg (x))) = eqI (code (sg (sg (x)))) (m7 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x))))))))) := by
        show eqI _ (m7 (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))) = _
        rw [h7, m7_rot, m7_rot]
      have r3 : Ls (sg (sg (sg (x)))) = eqI (code (sg (sg (sg (x))))) (m7 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x))))))))) := by
        show eqI _ (m7 (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))) = _
        rw [h7, m7_rot, m7_rot, m7_rot]
      have r4 : Ls (sg (sg (sg (sg (x))))) = eqI (code (sg (sg (sg (sg (x)))))) (m7 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x))))))))) := by
        show eqI _ (m7 (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))))) = _
        rw [h7, m7_rot, m7_rot, m7_rot, m7_rot]
      have r5 : Ls (sg (sg (sg (sg (sg (x)))))) = eqI (code (sg (sg (sg (sg (sg (x))))))) (m7 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x))))))))) := by
        show eqI _ (m7 (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))))) = _
        rw [h7, m7_rot, m7_rot, m7_rot, m7_rot, m7_rot]
      have r6 : Ls (sg (sg (sg (sg (sg (sg (x))))))) = eqI (code (sg (sg (sg (sg (sg (sg (x)))))))) (m7 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x))))))))) := by
        show eqI _ (m7 (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x  -- (+19 chars; full line in the Zenodo source)
        rw [h7, m7_rot, m7_rot, m7_rot, m7_rot, m7_rot, m7_rot]
      rw [r1, r2, r3, r4, r5, r6]
      show H x = H x * eqI (code x) _ + _ + _ + _ + _ + _ + _
      generalize m7 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) = μ at hm ⊢
      generalize eqI (code x) μ = u0 at hm ⊢
      generalize eqI (code (sg (x))) μ = u1 at hm ⊢
      generalize eqI (code (sg (sg (x)))) μ = u2 at hm ⊢
      generalize eqI (code (sg (sg (sg (x))))) μ = u3 at hm ⊢
      generalize eqI (code (sg (sg (sg (sg (x)))))) μ = u4 at hm ⊢
      generalize eqI (code (sg (sg (sg (sg (sg (x))))))) μ = u5 at hm ⊢
      generalize eqI (code (sg (sg (sg (sg (sg (sg (x)))))))) μ = u6 at hm ⊢
      calc H x = H x * (u0 + u1 + u2 + u3 + u4 + u5 + u6) := by rw [hm]; lib_int
        _ = _ := by lib_int
  have q1 : sumK LibInt.intR (fun x => H x * Ls (sg (x))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (x)) (fun x => sg (sg (sg (sg (sg (sg (x))))))) (fun x => h7 x) (fun y => by show sg (sg (sg (sg (sg (sg (sg y)))))) = y; exact h7 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (x)) = H (sg (x)) * Ls (sg (x)); rw [hH]) _
  have q2 : sumK LibInt.intR (fun x => H x * Ls (sg (sg (x)))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (sg (x))) (fun x => sg (sg (sg (sg (sg (x)))))) (fun x => h7 x) (fun y => by show sg (sg (sg (sg (sg (sg (sg y)))))) = y; exact h7 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (sg (x))) = H (sg (sg (x))) * Ls (sg (sg (x))); rw [hH, hH]) _
  have q3 : sumK LibInt.intR (fun x => H x * Ls (sg (sg (sg (x))))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (sg (sg (x)))) (fun x => sg (sg (sg (sg (x))))) (fun x => h7 x) (fun y => by show sg (sg (sg (sg (sg (sg (sg y)))))) = y; exact h7 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (sg (sg (x)))) = H (sg (sg (sg (x)))) * Ls (sg (sg (sg (x)))); rw [hH, hH, hH]) _
  have q4 : sumK LibInt.intR (fun x => H x * Ls (sg (sg (sg (sg (x)))))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (sg (sg (sg (x))))) (fun x => sg (sg (sg (x)))) (fun x => h7 x) (fun y => by show sg (sg (sg (sg (sg (sg (sg y)))))) = y; exact h7 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (sg (sg (sg (x))))) = H (sg (sg (sg (sg (x))))) * Ls (sg (sg (sg (sg (x))))); rw [hH, hH, hH, hH]) _
  have q5 : sumK LibInt.intR (fun x => H x * Ls (sg (sg (sg (sg (sg (x))))))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (sg (sg (sg (sg (x)))))) (fun x => sg (sg (x))) (fun x => h7 x) (fun y => by show sg (sg (sg (sg (sg (sg (sg y)))))) = y; exact h7 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (sg (sg (sg (sg (x)))))) = H (sg (sg (sg (sg (sg (x)))))) * Ls (sg (sg (sg (sg (sg (x)))))); rw [hH, hH, hH, hH, hH]) _
  have q6 : sumK LibInt.intR (fun x => H x * Ls (sg (sg (sg (sg (sg (sg (x)))))))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (sg (sg (sg (sg (sg (x))))))) (fun x => sg (x)) (fun x => h7 x) (fun y => by show sg (sg (sg (sg (sg (sg (sg y)))))) = y; exact h7 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (sg (sg (sg (sg (sg (x))))))) = H (sg (sg (sg (sg (sg (sg (x))))))) * Ls (sg (sg (sg (sg (sg (sg (x))))))); rw [hH, hH, hH, hH, hH, hH]) _
  rw [sumK_ext LibInt.intR _ _ hpt, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, q1, q2, q3, q4, q5, q6]
  generalize sumK LibInt.intR (fun x => H x * Ls x) els = T
  lib_int

end General

end Orbit7

#print axioms Orbit7.m7_mem
#print axioms Orbit7.m7_le
#print axioms Orbit7.m7_rot
#print axioms Orbit7.one_min7
#print axioms Orbit7.orbit7

lang/Orbit8.lean

lean_all/lang/Orbit8.lean

/-
  lang/Orbit8 — T43, cycle 441: counting orbits of an order-8 map by sums (Z/8 counting tool, the order-8 analogue of lang/Orbit4).

    m8, m8_mem, m8_le, m8_rot, one_min8
    orbit8   σ⁸ = id, H∘σ = H, H = 0 wherever σ⁴x = x ⟹ Σ H = 8·Σ H·[x least in its orbit]
             (σᵏx = x for some 0 < k < 8 forces σ⁴x = x: k·j ≡ 4 (mod 8) for a suitable j)
  Generated by a std-lib Python script. Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.Orbit4

namespace Orbit8

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum Orbit5
open FiniteRecChain (ite_pos' ite_neg')

def m8 (a0 a1 a2 a3 a4 a5 a6 a7 : Nat) : Nat := mn a0 (mn a1 (mn a2 (mn a3 (mn a4 (mn a5 (mn a6 (a7)))))))

theorem m8_mem (a0 a1 a2 a3 a4 a5 a6 a7 : Nat) : m8 a0 a1 a2 a3 a4 a5 a6 a7 = a0 ∨ m8 a0 a1 a2 a3 a4 a5 a6 a7 = a1 ∨ m8 a0 a1 a2 a3 a4 a5 a6 a7 = a2 ∨ m8 a0 a1 a2 a3 a4 a5 a6 a7 = a3 ∨ m8 a0 a1 a2 a3 a4 a5 a6 a7 = a4 ∨ m8 a0 a1 a2 a3 a4 a5 a6 a7 = a5 ∨ m8 a0 a1 a2 a3 a4 a5 a6 a7 = a6 ∨ m8 a0 a1 a2 a3 a4 a5 a6 a7 = a7 := by
  unfold m8
  rcases mn_or a0 (mn a1 (mn a2 (mn a3 (mn a4 (mn a5 (mn a6 (a7))))))) with h | h
  · exact Or.inl h
  rw [h]
  rcases mn_or a1 (mn a2 (mn a3 (mn a4 (mn a5 (mn a6 (a7)))))) with h | h
  · exact Or.inr (Or.inl h)
  rw [h]
  rcases mn_or a2 (mn a3 (mn a4 (mn a5 (mn a6 (a7))))) with h | h
  · exact Or.inr (Or.inr (Or.inl h))
  rw [h]
  rcases mn_or a3 (mn a4 (mn a5 (mn a6 (a7)))) with h | h
  · exact Or.inr (Or.inr (Or.inr (Or.inl h)))
  rw [h]
  rcases mn_or a4 (mn a5 (mn a6 (a7))) with h | h
  · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h))))
  rw [h]
  rcases mn_or a5 (mn a6 (a7)) with h | h
  · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h)))))
  rw [h]
  rcases mn_or a6 (a7) with h | h
  · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h))))))
  rw [h]
  exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (rfl)))))))

theorem m8_le (a0 a1 a2 a3 a4 a5 a6 a7 : Nat) : m8 a0 a1 a2 a3 a4 a5 a6 a7 ≤ a0 ∧ m8 a0 a1 a2 a3 a4 a5 a6 a7 ≤ a1 ∧ m8 a0 a1 a2 a3 a4 a5 a6 a7 ≤ a2 ∧ m8 a0 a1 a2 a3 a4 a5 a6 a7 ≤ a3 ∧ m8 a0 a1 a2 a3 a4 a5 a6 a7 ≤ a4 ∧ m8 a0 a1 a2 a3 a4 a5 a6 a7 ≤ a5 ∧ m8 a0 a1 a2 a3 a4 a5 a6 a7 ≤ a6 ∧ m8 a0 a1 a2 a3 a4 a5 a6 a7 ≤ a7 := by
  unfold m8
  exact ⟨mn_le_l _ _, Nat.le_trans (mn_le_r _ _) (mn_le_l _ _), Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (mn_le_l _ _)), Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (mn_le_l _ _))), Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (mn_le_l _ _)))), Nat.le_trans (mn_le_r _ _) (Nat.le_t  -- (+495 chars; full line in the Zenodo source)

theorem m8_rot (a0 a1 a2 a3 a4 a5 a6 a7 : Nat) : m8 a1 a2 a3 a4 a5 a6 a7 a0 = m8 a0 a1 a2 a3 a4 a5 a6 a7 := by
  have l1 := m8_le a0 a1 a2 a3 a4 a5 a6 a7
  have l2 := m8_le a1 a2 a3 a4 a5 a6 a7 a0
  apply Nat.le_antisymm
  · rcases m8_mem a0 a1 a2 a3 a4 a5 a6 a7 with h | h | h | h | h | h | h | h <;> rw [h]
    · exact l2.2.2.2.2.2.2.2
    · exact l2.1
    · exact l2.2.1
    · exact l2.2.2.1
    · exact l2.2.2.2.1
    · exact l2.2.2.2.2.1
    · exact l2.2.2.2.2.2.1
    · exact l2.2.2.2.2.2.2.1
  · rcases m8_mem a1 a2 a3 a4 a5 a6 a7 a0 with h | h | h | h | h | h | h | h <;> rw [h]
    · exact l1.2.1
    · exact l1.2.2.1
    · exact l1.2.2.2.1
    · exact l1.2.2.2.2.1
    · exact l1.2.2.2.2.2.1
    · exact l1.2.2.2.2.2.2.1
    · exact l1.2.2.2.2.2.2.2
    · exact l1.1

theorem one_min8 (a0 a1 a2 a3 a4 a5 a6 a7 : Nat) (h01 : a0 ≠ a1) (h02 : a0 ≠ a2) (h03 : a0 ≠ a3) (h04 : a0 ≠ a4) (h05 : a0 ≠ a5) (h06 : a0 ≠ a6) (h07 : a0 ≠ a7) (h12 : a1 ≠ a2) (h13 : a1 ≠ a3) (h14 : a1 ≠ a4) (h15 : a1 ≠ a5) (h16 : a1 ≠ a6) (h17 : a1 ≠ a7) (h23 : a2 ≠ a3) (h24 : a2 ≠ a4) (h25 : a2 ≠ a5) (h26 : a2 ≠ a6) (h27 : a2 ≠ a7) (h34 : a3 ≠ a4) (h35 : a3 ≠ a5) (h36 : a3 ≠ a6) (h37 : a3 ≠ a7)  -- (+98 chars; full line in the Zenodo source)
    eqI a0 (m8 a0 a1 a2 a3 a4 a5 a6 a7) + eqI a1 (m8 a0 a1 a2 a3 a4 a5 a6 a7) + eqI a2 (m8 a0 a1 a2 a3 a4 a5 a6 a7) + eqI a3 (m8 a0 a1 a2 a3 a4 a5 a6 a7) + eqI a4 (m8 a0 a1 a2 a3 a4 a5 a6 a7) + eqI a5 (m8 a0 a1 a2 a3 a4 a5 a6 a7) + eqI a6 (m8 a0 a1 a2 a3 a4 a5 a6 a7) + eqI a7 (m8 a0 a1 a2 a3 a4 a5 a6 a7) = 1 := by
  unfold eqI
  rcases m8_mem a0 a1 a2 a3 a4 a5 a6 a7 with h | h | h | h | h | h | h | h <;> rw [h]
  · rw [ite_pos' _ _ rfl, ite_neg' _ _ (fun e => h01 e.symm), ite_neg' _ _ (fun e => h02 e.symm), ite_neg' _ _ (fun e => h03 e.symm), ite_neg' _ _ (fun e => h04 e.symm), ite_neg' _ _ (fun e => h05 e.symm), ite_neg' _ _ (fun e => h06 e.symm), ite_neg' _ _ (fun e => h07 e.symm)]; rfl
  · rw [ite_neg' _ _ h01, ite_pos' _ _ rfl, ite_neg' _ _ (fun e => h12 e.symm), ite_neg' _ _ (fun e => h13 e.symm), ite_neg' _ _ (fun e => h14 e.symm), ite_neg' _ _ (fun e => h15 e.symm), ite_neg' _ _ (fun e => h16 e.symm), ite_neg' _ _ (fun e => h17 e.symm)]; rfl
  · rw [ite_neg' _ _ h02, ite_neg' _ _ h12, ite_pos' _ _ rfl, ite_neg' _ _ (fun e => h23 e.symm), ite_neg' _ _ (fun e => h24 e.symm), ite_neg' _ _ (fun e => h25 e.symm), ite_neg' _ _ (fun e => h26 e.symm), ite_neg' _ _ (fun e => h27 e.symm)]; rfl
  · rw [ite_neg' _ _ h03, ite_neg' _ _ h13, ite_neg' _ _ h23, ite_pos' _ _ rfl, ite_neg' _ _ (fun e => h34 e.symm), ite_neg' _ _ (fun e => h35 e.symm), ite_neg' _ _ (fun e => h36 e.symm), ite_neg' _ _ (fun e => h37 e.symm)]; rfl
  · rw [ite_neg' _ _ h04, ite_neg' _ _ h14, ite_neg' _ _ h24, ite_neg' _ _ h34, ite_pos' _ _ rfl, ite_neg' _ _ (fun e => h45 e.symm), ite_neg' _ _ (fun e => h46 e.symm), ite_neg' _ _ (fun e => h47 e.symm)]; rfl
  · rw [ite_neg' _ _ h05, ite_neg' _ _ h15, ite_neg' _ _ h25, ite_neg' _ _ h35, ite_neg' _ _ h45, ite_pos' _ _ rfl, ite_neg' _ _ (fun e => h56 e.symm), ite_neg' _ _ (fun e => h57 e.symm)]; rfl
  · rw [ite_neg' _ _ h06, ite_neg' _ _ h16, ite_neg' _ _ h26, ite_neg' _ _ h36, ite_neg' _ _ h46, ite_neg' _ _ h56, ite_pos' _ _ rfl, ite_neg' _ _ (fun e => h67 e.symm)]; rfl
  · rw [ite_neg' _ _ h07, ite_neg' _ _ h17, ite_neg' _ _ h27, ite_neg' _ _ h37, ite_neg' _ _ h47, ite_neg' _ _ h57, ite_neg' _ _ h67, ite_pos' _ _ rfl]; rfl

section General
variable {G : Type} [DecidableEq G]

/-- **Orbits of an order-8 map** with H = 0 on the points fixed by σ⁴. -/
theorem orbit8 (els : List G) (hc : Complete LibInt.intR els) (code : G → Nat) (hinj : ∀ u v, code u = code v → u = v)
    (sg : G → G) (h8 : ∀ x, sg (sg (sg (sg (sg (sg (sg (sg (x)))))))) = x) (H : G → Int) (hH : ∀ x, H (sg x) = H x) (hfix : ∀ x, sg (sg (sg (sg (x)))) = x → H x = 0) :
    sumK LibInt.intR H els = 8 * sumK LibInt.intR (fun x => H x * eqI (code x) (m8 (code (x)) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x))))))))))) els := by
  let Ls : G → Int := fun x => eqI (code x) (m8 (code (x)) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x))))))))))
  have hpt : ∀ x, H x = H x * Ls (x) + H x * Ls (sg (x)) + H x * Ls (sg (sg (x))) + H x * Ls (sg (sg (sg (x)))) + H x * Ls (sg (sg (sg (sg (x))))) + H x * Ls (sg (sg (sg (sg (sg (x)))))) + H x * Ls (sg (sg (sg (sg (sg (sg (x))))))) + H x * Ls (sg (sg (sg (sg (sg (sg (sg (x)))))))) := fun x => by
    by_cases hx : sg (sg (sg (sg (x)))) = x
    · rw [hfix x hx]; lib_int
    · have ne : ∀ u v : G, u ≠ v → code u ≠ code v := fun u v h e => h (hinj u v e)
      have shift : ∀ y z : G, y ≠ z → sg y ≠ sg z := fun y z h e => h (by
        calc y = sg (sg (sg (sg (sg (sg (sg (sg (y)))))))) := (h8 y).symm
          _ = sg (sg (sg (sg (sg (sg (sg ((sg z)))))))) := by rw [e]
          _ = z := h8 z)
      have d01 : sg (x) ≠ x := fun e => hx (by
        have e2 : sg (sg (sg (sg (x)))) = x := by rw [e, e, e]; exact e
        calc sg (sg (sg (sg (x)))) = sg (sg (sg (sg (x)))) := by rw []
          _ = x := e2)
      have d02 : sg (sg (x)) ≠ x := fun e => hx (by
        have e2 : sg (sg (sg (sg (x)))) = x := by rw [e]; exact e
        calc sg (sg (sg (sg (x)))) = sg (sg (sg (sg (x)))) := by rw []
          _ = x := e2)
      have d03 : sg (sg (sg (x))) ≠ x := fun e => hx (by
        have e2 : sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))) = x := by rw [e, e, e]; exact e
        calc sg (sg (sg (sg (x)))) = sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))) := by rw [h8]
          _ = x := e2)
      have d05 : sg (sg (sg (sg (sg (x))))) ≠ x := fun e => hx (by
        have e2 : sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))))))))))) = x := by rw [e, e, e]; exact e
        calc sg (sg (sg (sg (x)))) = sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))))))))))) := by rw [h8, h8]
          _ = x := e2)
      have d06 : sg (sg (sg (sg (sg (sg (x)))))) ≠ x := fun e => hx (by
        have e2 : sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))) = x := by rw [e]; exact e
        calc sg (sg (sg (sg (x)))) = sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))) := by rw [h8]
          _ = x := e2)
      have d07 : sg (sg (sg (sg (sg (sg (sg (x))))))) ≠ x := fun e => hx (by
        have e2 : sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))))))))))))))))))) = x := by rw [e, e, e]; exact e
        calc sg (sg (sg (sg (x)))) = sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))))))))))))))))))) := by rw [h8, h8, h8]
          _ = x := e2)
      have d04 : sg (sg (sg (sg (x)))) ≠ x := hx
      have d12 := shift _ _ d01
      have d13 := shift _ _ d02
      have d14 := shift _ _ d03
      have d15 := shift _ _ d04
      have d16 := shift _ _ d05
      have d17 := shift _ _ d06
      have d23 := shift _ _ d12
      have d24 := shift _ _ d13
      have d25 := shift _ _ d14
      have d26 := shift _ _ d15
      have d27 := shift _ _ d16
      have d34 := shift _ _ d23
      have d35 := shift _ _ d24
      have d36 := shift _ _ d25
      have d37 := shift _ _ d26
      have d45 := shift _ _ d34
      have d46 := shift _ _ d35
      have d47 := shift _ _ d36
      have d56 := shift _ _ d45
      have d57 := shift _ _ d46
      have d67 := shift _ _ d56
      have hm := one_min8 (code (x)) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x)))))))))
        (ne _ _ (fun e => d01 e.symm)) (ne _ _ (fun e => d02 e.symm)) (ne _ _ (fun e => d03 e.symm)) (ne _ _ (fun e => d04 e.symm)) (ne _ _ (fun e => d05 e.symm)) (ne _ _ (fun e => d06 e.symm)) (ne _ _ (fun e => d07 e.symm)) (ne _ _ (fun e => d12 e.symm)) (ne _ _ (fun e => d13 e.symm)) (ne _ _ (fun e => d14 e.symm)) (ne _ _ (fun e => d15 e.symm)) (ne _ _ (fun e => d16 e.symm)) (ne _ _ (fun e => d1  -- (+475 chars; full line in the Zenodo source)
      have r1 : Ls (sg (x)) = eqI (code (sg (x))) (m8 (code (x)) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x)))))))))) := by
        show eqI _ (m8 _ _ _ _ _ _ _ (code (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))) = _
        rw [h8 (x), m8_rot]
      have r2 : Ls (sg (sg (x))) = eqI (code (sg (sg (x)))) (m8 (code (x)) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x)))))))))) := by
        show eqI _ (m8 _ _ _ _ _ _ (code (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))) = _
        rw [h8 (x), m8_rot, m8_rot]
      have r3 : Ls (sg (sg (sg (x)))) = eqI (code (sg (sg (sg (x))))) (m8 (code (x)) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x)))))))))) := by
        show eqI _ (m8 _ _ _ _ _ (code (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))))) = _
        rw [h8 (x), m8_rot, m8_rot, m8_rot]
      have r4 : Ls (sg (sg (sg (sg (x))))) = eqI (code (sg (sg (sg (sg (x)))))) (m8 (code (x)) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x)))))))))) := by
        show eqI _ (m8 _ _ _ _ (code (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))))) = _
        rw [h8 (x), m8_rot, m8_rot, m8_rot, m8_rot]
      have r5 : Ls (sg (sg (sg (sg (sg (x)))))) = eqI (code (sg (sg (sg (sg (sg (x))))))) (m8 (code (x)) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x)))))))))) := by
        show eqI _ (m8 _ _ _ (code (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))))))) = _
        rw [h8 (x), m8_rot, m8_rot, m8_rot, m8_rot, m8_rot]
      have r6 : Ls (sg (sg (sg (sg (sg (sg (x))))))) = eqI (code (sg (sg (sg (sg (sg (sg (x)))))))) (m8 (code (x)) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x)))))))))) := by
        show eqI _ (m8 _ _ (code (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))  -- (+12 chars; full line in the Zenodo source)
        rw [h8 (x), m8_rot, m8_rot, m8_rot, m8_rot, m8_rot, m8_rot]
      have r7 : Ls (sg (sg (sg (sg (sg (sg (sg (x)))))))) = eqI (code (sg (sg (sg (sg (sg (sg (sg (x))))))))) (m8 (code (x)) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x)))))))))) := by
        show eqI _ (m8 _ (code (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))  -- (+91 chars; full line in the Zenodo source)
        rw [h8 (x), m8_rot, m8_rot, m8_rot, m8_rot, m8_rot, m8_rot, m8_rot]
      rw [r1, r2, r3, r4, r5, r6, r7]
      show H x = H x * eqI (code x) _ + _ + _ + _ + _ + _ + _ + _
      generalize m8 (code (x)) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x))))))))) = μ at hm ⊢
      generalize eqI (code (x)) μ = u0 at hm ⊢
      generalize eqI (code (sg (x))) μ = u1 at hm ⊢
      generalize eqI (code (sg (sg (x)))) μ = u2 at hm ⊢
      generalize eqI (code (sg (sg (sg (x))))) μ = u3 at hm ⊢
      generalize eqI (code (sg (sg (sg (sg (x)))))) μ = u4 at hm ⊢
      generalize eqI (code (sg (sg (sg (sg (sg (x))))))) μ = u5 at hm ⊢
      generalize eqI (code (sg (sg (sg (sg (sg (sg (x)))))))) μ = u6 at hm ⊢
      generalize eqI (code (sg (sg (sg (sg (sg (sg (sg (x))))))))) μ = u7 at hm ⊢
      calc H x = H x * (u0 + u1 + u2 + u3 + u4 + u5 + u6 + u7) := by rw [hm]; lib_int
        _ = _ := by lib_int
  have q1 : sumK LibInt.intR (fun x => H x * Ls (sg (x))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (x)) (fun x => sg (sg (sg (sg (sg (sg (sg (x)))))))) (fun x => h8 x) (fun y => h8 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (x)) = H (sg (x)) * Ls (sg (x)); rw [hH]) _
  have q2 : sumK LibInt.intR (fun x => H x * Ls (sg (sg (x)))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (sg (x))) (fun x => sg (sg (sg (sg (sg (sg (x))))))) (fun x => h8 x) (fun y => h8 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (sg (x))) = H (sg (sg (x))) * Ls (sg (sg (x))); rw [hH, hH]) _
  have q3 : sumK LibInt.intR (fun x => H x * Ls (sg (sg (sg (x))))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (sg (sg (x)))) (fun x => sg (sg (sg (sg (sg (x)))))) (fun x => h8 x) (fun y => h8 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (sg (sg (x)))) = H (sg (sg (sg (x)))) * Ls (sg (sg (sg (x)))); rw [hH, hH, hH]) _
  have q4 : sumK LibInt.intR (fun x => H x * Ls (sg (sg (sg (sg (x)))))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (sg (sg (sg (x))))) (fun x => sg (sg (sg (sg (x))))) (fun x => h8 x) (fun y => h8 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (sg (sg (sg (x))))) = H (sg (sg (sg (sg (x))))) * Ls (sg (sg (sg (sg (x))))); rw [hH, hH, hH, hH]) _
  have q5 : sumK LibInt.intR (fun x => H x * Ls (sg (sg (sg (sg (sg (x))))))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (sg (sg (sg (sg (x)))))) (fun x => sg (sg (sg (x)))) (fun x => h8 x) (fun y => h8 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (sg (sg (sg (sg (x)))))) = H (sg (sg (sg (sg (sg (x)))))) * Ls (sg (sg (sg (sg (sg (x)))))); rw [hH, hH, hH, hH, hH]) _
  have q6 : sumK LibInt.intR (fun x => H x * Ls (sg (sg (sg (sg (sg (sg (x)))))))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (sg (sg (sg (sg (sg (x))))))) (fun x => sg (sg (x))) (fun x => h8 x) (fun y => h8 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (sg (sg (sg (sg (sg (x))))))) = H (sg (sg (sg (sg (sg (sg (x))))))) * Ls (sg (sg (sg (sg (sg (sg (x))))))); rw [hH, hH, hH, hH, hH, hH]) _
  have q7 : sumK LibInt.intR (fun x => H x * Ls (sg (sg (sg (sg (sg (sg (sg (x))))))))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (sg (sg (sg (sg (sg (sg (x)))))))) (fun x => sg (x)) (fun x => h8 x) (fun y => h8 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (sg (sg (sg (sg (sg (sg (x)))))))) = H (sg (sg (sg (sg (sg (sg (sg (x)))))))) * Ls (sg (sg (sg (sg (sg (sg (sg (x)))))))); rw [hH, hH, hH, hH, hH, hH, hH]) _
  rw [sumK_ext LibInt.intR _ _ hpt, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, q1, q2, q3, q4, q5, q6, q7]
  generalize sumK LibInt.intR (fun x => H x * Ls x) els = T
  lib_int

end General

end Orbit8

#print axioms Orbit8.m8_mem
#print axioms Orbit8.m8_le
#print axioms Orbit8.m8_rot
#print axioms Orbit8.one_min8
#print axioms Orbit8.orbit8

lang/Orbit82.lean

lean_all/lang/Orbit82.lean

/-
  lang/Orbit82 — T43, cycle 472: orbits of a free action of Z/8 × Z/2 counted by sums (tool for 16 | #E(𝔽_p)).

    m16, one_min16   the least of sixteen distinct codes is attained exactly once (two halves of Orbit8.m8)
    act_id8, act_red8, M_t, M_s   t⁸ = s² = id; the minimum is invariant under t (rotation of each half) and s (swap of the halves)
    orbit82          t⁸ = s² = id, st = ts, H invariant, H = 0 at points fixed by one of the fifteen non-identity act i j
                     ⟹ Σ H = 16·Σ H·[x least in its orbit]
  Generated by a std-lib Python script (orb82.py) after lang/Orbit42. Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.Orbit8
import lang.Orbit42

namespace Orbit82

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum Orbit5 Orbit4 Orbit8
open Orbit42 (tp act tp_add tp_comm1 tp_comm act_comp tp_mul act_red2 act_t act_s)
open FiniteRecChain (ite_pos' ite_neg')

section General
variable {G : Type}

def m16 (a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 : Nat) : Nat := mn (m8 a0 a1 a2 a3 a4 a5 a6 a7) (m8 a8 a9 a10 a11 a12 a13 a14 a15)

theorem one_min16 (a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 : Nat) (h0_1 : a0 ≠ a1) (h0_2 : a0 ≠ a2) (h0_3 : a0 ≠ a3) (h0_4 : a0 ≠ a4) (h0_5 : a0 ≠ a5) (h0_6 : a0 ≠ a6) (h0_7 : a0 ≠ a7) (h0_8 : a0 ≠ a8) (h0_9 : a0 ≠ a9) (h0_10 : a0 ≠ a10) (h0_11 : a0 ≠ a11) (h0_12 : a0 ≠ a12) (h0_13 : a0 ≠ a13) (h0_14 : a0 ≠ a14) (h0_15 : a0 ≠ a15) (h1_2 : a1 ≠ a2) (h1_3 : a1 ≠ a3) (h1_4 : a1 ≠ a4) (h  -- (+1901 chars; full line in the Zenodo source)
    eqI a0 (m16 a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15) + eqI a1 (m16 a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15) + eqI a2 (m16 a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15) + eqI a3 (m16 a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15) + eqI a4 (m16 a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15) + eqI a5 (m16 a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a  -- (+721 chars; full line in the Zenodo source)
  unfold m16
  rcases mn_or (m8 a0 a1 a2 a3 a4 a5 a6 a7) (m8 a8 a9 a10 a11 a12 a13 a14 a15) with hm | hm <;> rw [hm]
  · have h1 := Orbit8.one_min8 a0 a1 a2 a3 a4 a5 a6 a7 h0_1 h0_2 h0_3 h0_4 h0_5 h0_6 h0_7 h1_2 h1_3 h1_4 h1_5 h1_6 h1_7 h2_3 h2_4 h2_5 h2_6 h2_7 h3_4 h3_5 h3_6 h3_7 h4_5 h4_6 h4_7 h5_6 h5_7 h6_7
    have z : ∀ c, c ≠ a0 → c ≠ a1 → c ≠ a2 → c ≠ a3 → c ≠ a4 → c ≠ a5 → c ≠ a6 → c ≠ a7 → eqI c (m8 a0 a1 a2 a3 a4 a5 a6 a7) = 0 := fun c n0 n1 n2 n3 n4 n5 n6 n7 => by
      apply Orbit42.eqI_ne
      rcases Orbit8.m8_mem a0 a1 a2 a3 a4 a5 a6 a7 with e | e | e | e | e | e | e | e <;> rw [e]
      · exact n0
      · exact n1
      · exact n2
      · exact n3
      · exact n4
      · exact n5
      · exact n6
      · exact n7
    rw [z a8 (Ne.symm h0_8) (Ne.symm h1_8) (Ne.symm h2_8) (Ne.symm h3_8) (Ne.symm h4_8) (Ne.symm h5_8) (Ne.symm h6_8) (Ne.symm h7_8), z a9 (Ne.symm h0_9) (Ne.symm h1_9) (Ne.symm h2_9) (Ne.symm h3_9) (Ne.symm h4_9) (Ne.symm h5_9) (Ne.symm h6_9) (Ne.symm h7_9), z a10 (Ne.symm h0_10) (Ne.symm h1_10) (Ne.symm h2_10) (Ne.symm h3_10) (Ne.symm h4_10) (Ne.symm h5_10) (Ne.symm h6_10) (Ne.symm h7_10), z a11  -- (+669 chars; full line in the Zenodo source)
    rw [← h1]; lib_int
  · have h1 := Orbit8.one_min8 a8 a9 a10 a11 a12 a13 a14 a15 h8_9 h8_10 h8_11 h8_12 h8_13 h8_14 h8_15 h9_10 h9_11 h9_12 h9_13 h9_14 h9_15 h10_11 h10_12 h10_13 h10_14 h10_15 h11_12 h11_13 h11_14 h11_15 h12_13 h12_14 h12_15 h13_14 h13_15 h14_15
    have z : ∀ c, c ≠ a8 → c ≠ a9 → c ≠ a10 → c ≠ a11 → c ≠ a12 → c ≠ a13 → c ≠ a14 → c ≠ a15 → eqI c (m8 a8 a9 a10 a11 a12 a13 a14 a15) = 0 := fun c n0 n1 n2 n3 n4 n5 n6 n7 => by
      apply Orbit42.eqI_ne
      rcases Orbit8.m8_mem a8 a9 a10 a11 a12 a13 a14 a15 with e | e | e | e | e | e | e | e <;> rw [e]
      · exact n0
      · exact n1
      · exact n2
      · exact n3
      · exact n4
      · exact n5
      · exact n6
      · exact n7
    rw [z a0 h0_8 h0_9 h0_10 h0_11 h0_12 h0_13 h0_14 h0_15, z a1 h1_8 h1_9 h1_10 h1_11 h1_12 h1_13 h1_14 h1_15, z a2 h2_8 h2_9 h2_10 h2_11 h2_12 h2_13 h2_14 h2_15, z a3 h3_8 h3_9 h3_10 h3_11 h3_12 h3_13 h3_14 h3_15, z a4 h4_8 h4_9 h4_10 h4_11 h4_12 h4_13 h4_14 h4_15, z a5 h5_8 h5_9 h5_10 h5_11 h5_12 h5_13 h5_14 h5_15, z a6 h6_8 h6_9 h6_10 h6_11 h6_12 h6_13 h6_14 h6_15, z a7 h7_8 h7_9 h7_10 h7_11 h  -- (+23 chars; full line in the Zenodo source)
    rw [← h1]; lib_int

theorem act_id8 {s t : G → G} (ht : ∀ y, tp t 8 y = y) (hs : ∀ y, tp s 2 y = y) (i j : Nat) (y : G) :
    act s t (8 * i) (2 * j) y = y := by
  show tp s (2 * j) (tp t (8 * i) y) = y
  rw [tp_mul t 8 ht, tp_mul s 2 hs]

theorem act_red8 {s t : G → G} {ht : ∀ y, tp t 8 y = y} (k j : Nat) (y : G) : act s t (k + 8) j y = act s t k j y := by
  show tp s j (tp t (k + 8) y) = tp s j (tp t k y)
  rw [← tp_add, ht]

theorem M_t {s t : G → G} (ht : ∀ y, tp t 8 y = y) (code : G → Nat) (x : G) : m16 (code (act s t 0 0 (t x))) (code (act s t 1 0 (t x))) (code (act s t 2 0 (t x))) (code (act s t 3 0 (t x))) (code (act s t 4 0 (t x))) (code (act s t 5 0 (t x))) (code (act s t 6 0 (t x))) (code (act s t 7 0 (t x))) (code (act s t 0 1 (t x))) (code (act s t 1 1 (t x))) (code (act s t 2 1 (t x))) (code (act s t 3 1 (t  -- (+493 chars; full line in the Zenodo source)
  rw [act_t, act_t, act_t, act_t, act_t, act_t, act_t, act_t, act_t, act_t, act_t, act_t, act_t, act_t, act_t, act_t]
  rw [act_red8 (ht := ht) 0 0, act_red8 (ht := ht) 0 1]
  show m16 (code (act s t 1 0 x)) (code (act s t 2 0 x)) (code (act s t 3 0 x)) (code (act s t 4 0 x)) (code (act s t 5 0 x)) (code (act s t 6 0 x)) (code (act s t 7 0 x)) (code (act s t 0 0 x)) (code (act s t 1 1 x)) (code (act s t 2 1 x)) (code (act s t 3 1 x)) (code (act s t 4 1 x)) (code (act s t 5 1 x)) (code (act s t 6 1 x)) (code (act s t 7 1 x)) (code (act s t 0 1 x)) = _
  unfold m16
  rw [Orbit8.m8_rot (code (act s t 0 0 x)) (code (act s t 1 0 x)) (code (act s t 2 0 x)) (code (act s t 3 0 x)) (code (act s t 4 0 x)) (code (act s t 5 0 x)) (code (act s t 6 0 x)) (code (act s t 7 0 x)), Orbit8.m8_rot (code (act s t 0 1 x)) (code (act s t 1 1 x)) (code (act s t 2 1 x)) (code (act s t 3 1 x)) (code (act s t 4 1 x)) (code (act s t 5 1 x)) (code (act s t 6 1 x)) (code (act s t 7 1 x  -- (+3 chars; full line in the Zenodo source)

theorem M_s {s t : G → G} (hs : ∀ y, tp s 2 y = y) (hst : ∀ y, s (t y) = t (s y)) (code : G → Nat) (x : G) : m16 (code (act s t 0 0 (s x))) (code (act s t 1 0 (s x))) (code (act s t 2 0 (s x))) (code (act s t 3 0 (s x))) (code (act s t 4 0 (s x))) (code (act s t 5 0 (s x))) (code (act s t 6 0 (s x))) (code (act s t 7 0 (s x))) (code (act s t 0 1 (s x))) (code (act s t 1 1 (s x))) (code (act s t 2   -- (+524 chars; full line in the Zenodo source)
  rw [act_s hst, act_s hst, act_s hst, act_s hst, act_s hst, act_s hst, act_s hst, act_s hst, act_s hst, act_s hst, act_s hst, act_s hst, act_s hst, act_s hst, act_s hst, act_s hst]
  rw [act_red2 (hs := hs) 0 0, act_red2 (hs := hs) 1 0, act_red2 (hs := hs) 2 0, act_red2 (hs := hs) 3 0, act_red2 (hs := hs) 4 0, act_red2 (hs := hs) 5 0, act_red2 (hs := hs) 6 0, act_red2 (hs := hs) 7 0]
  show m16 (code (act s t 0 1 x)) (code (act s t 1 1 x)) (code (act s t 2 1 x)) (code (act s t 3 1 x)) (code (act s t 4 1 x)) (code (act s t 5 1 x)) (code (act s t 6 1 x)) (code (act s t 7 1 x)) (code (act s t 0 0 x)) (code (act s t 1 0 x)) (code (act s t 2 0 x)) (code (act s t 3 0 x)) (code (act s t 4 0 x)) (code (act s t 5 0 x)) (code (act s t 6 0 x)) (code (act s t 7 0 x)) = _
  unfold m16
  exact G5Two.mn_comm (m8 (code (act s t 0 0 x)) (code (act s t 1 0 x)) (code (act s t 2 0 x)) (code (act s t 3 0 x)) (code (act s t 4 0 x)) (code (act s t 5 0 x)) (code (act s t 6 0 x)) (code (act s t 7 0 x))) (m8 (code (act s t 0 1 x)) (code (act s t 1 1 x)) (code (act s t 2 1 x)) (code (act s t 3 1 x)) (code (act s t 4 1 x)) (code (act s t 5 1 x)) (code (act s t 6 1 x)) (code (act s t 7 1 x)))

theorem orbit82 [DecidableEq G] (els : List G) (hc : Complete LibInt.intR els) (code : G → Nat) (hinj : ∀ u v, code u = code v → u = v)
    (s t : G → G) (hs : ∀ y, tp s 2 y = y) (ht : ∀ y, tp t 8 y = y) (hst : ∀ y, s (t y) = t (s y))
    (H : G → Int) (hHs : ∀ x, H (s x) = H x) (hHt : ∀ x, H (t x) = H x)
    (hfix : ∀ x, (act s t 1 0 x = x ∨ act s t 2 0 x = x ∨ act s t 3 0 x = x ∨ act s t 4 0 x = x ∨ act s t 5 0 x = x ∨ act s t 6 0 x = x ∨ act s t 7 0 x = x ∨ act s t 0 1 x = x ∨ act s t 1 1 x = x ∨ act s t 2 1 x = x ∨ act s t 3 1 x = x ∨ act s t 4 1 x = x ∨ act s t 5 1 x = x ∨ act s t 6 1 x = x ∨ act s t 7 1 x = x) → H x = 0) :
    sumK LibInt.intR H els = 16 * sumK LibInt.intR (fun x => H x * eqI (code x) (m16 (code (act s t 0 0 x)) (code (act s t 1 0 x)) (code (act s t 2 0 x)) (code (act s t 3 0 x)) (code (act s t 4 0 x)) (code (act s t 5 0 x)) (code (act s t 6 0 x)) (code (act s t 7 0 x)) (code (act s t 0 1 x)) (code (act s t 1 1 x)) (code (act s t 2 1 x)) (code (act s t 3 1 x)) (code (act s t 4 1 x)) (code (act s t 5  -- (+64 chars; full line in the Zenodo source)
  let Lf : G → Int := fun y => eqI (code y) (m16 (code (act s t 0 0 y)) (code (act s t 1 0 y)) (code (act s t 2 0 y)) (code (act s t 3 0 y)) (code (act s t 4 0 y)) (code (act s t 5 0 y)) (code (act s t 6 0 y)) (code (act s t 7 0 y)) (code (act s t 0 1 y)) (code (act s t 1 1 y)) (code (act s t 2 1 y)) (code (act s t 3 1 y)) (code (act s t 4 1 y)) (code (act s t 5 1 y)) (code (act s t 6 1 y)) (code   -- (+17 chars; full line in the Zenodo source)
  have hMt : ∀ i y, m16 (code (act s t 0 0 (tp t i y))) (code (act s t 1 0 (tp t i y))) (code (act s t 2 0 (tp t i y))) (code (act s t 3 0 (tp t i y))) (code (act s t 4 0 (tp t i y))) (code (act s t 5 0 (tp t i y))) (code (act s t 6 0 (tp t i y))) (code (act s t 7 0 (tp t i y))) (code (act s t 0 1 (tp t i y))) (code (act s t 1 1 (tp t i y))) (code (act s t 2 1 (tp t i y))) (code (act s t 3 1 (tp t  -- (+524 chars; full line in the Zenodo source)
    induction i with
    | zero => intro y; rfl
    | succ i ih => intro y; show m16 (code (act s t 0 0 (t (tp t i y)))) (code (act s t 1 0 (t (tp t i y)))) (code (act s t 2 0 (t (tp t i y)))) (code (act s t 3 0 (t (tp t i y)))) (code (act s t 4 0 (t (tp t i y)))) (code (act s t 5 0 (t (tp t i y)))) (code (act s t 6 0 (t (tp t i y)))) (code (act s t 7 0 (t (tp t i y)))) (code (act s t 0 1 (t (tp t i y)))) (code (act s t 1 1 (t (tp t i y)))) (co  -- (+238 chars; full line in the Zenodo source)
  have hMs : ∀ j y, m16 (code (act s t 0 0 (tp s j y))) (code (act s t 1 0 (tp s j y))) (code (act s t 2 0 (tp s j y))) (code (act s t 3 0 (tp s j y))) (code (act s t 4 0 (tp s j y))) (code (act s t 5 0 (tp s j y))) (code (act s t 6 0 (tp s j y))) (code (act s t 7 0 (tp s j y))) (code (act s t 0 1 (tp s j y))) (code (act s t 1 1 (tp s j y))) (code (act s t 2 1 (tp s j y))) (code (act s t 3 1 (tp s  -- (+524 chars; full line in the Zenodo source)
    induction j with
    | zero => intro y; rfl
    | succ j ih => intro y; show m16 (code (act s t 0 0 (s (tp s j y)))) (code (act s t 1 0 (s (tp s j y)))) (code (act s t 2 0 (s (tp s j y)))) (code (act s t 3 0 (s (tp s j y)))) (code (act s t 4 0 (s (tp s j y)))) (code (act s t 5 0 (s (tp s j y)))) (code (act s t 6 0 (s (tp s j y)))) (code (act s t 7 0 (s (tp s j y)))) (code (act s t 0 1 (s (tp s j y)))) (code (act s t 1 1 (s (tp s j y)))) (co  -- (+242 chars; full line in the Zenodo source)
  have hLact : ∀ i j y, Lf (act s t i j y) = eqI (code (act s t i j y)) (m16 (code (act s t 0 0 y)) (code (act s t 1 0 y)) (code (act s t 2 0 y)) (code (act s t 3 0 y)) (code (act s t 4 0 y)) (code (act s t 5 0 y)) (code (act s t 6 0 y)) (code (act s t 7 0 y)) (code (act s t 0 1 y)) (code (act s t 1 1 y)) (code (act s t 2 1 y)) (code (act s t 3 1 y)) (code (act s t 4 1 y)) (code (act s t 5 1 y)) (  -- (+64 chars; full line in the Zenodo source)
    show eqI _ (m16 (code (act s t 0 0 (tp s j (tp t i y)))) (code (act s t 1 0 (tp s j (tp t i y)))) (code (act s t 2 0 (tp s j (tp t i y)))) (code (act s t 3 0 (tp s j (tp t i y)))) (code (act s t 4 0 (tp s j (tp t i y)))) (code (act s t 5 0 (tp s j (tp t i y)))) (code (act s t 6 0 (tp s j (tp t i y)))) (code (act s t 7 0 (tp s j (tp t i y)))) (code (act s t 0 1 (tp s j (tp t i y)))) (code (act   -- (+295 chars; full line in the Zenodo source)
  have hHt' : ∀ i y, H (tp t i y) = H y := fun i => by
    induction i with
    | zero => intro y; rfl
    | succ i ih => intro y; show H (t (tp t i y)) = _; rw [hHt, ih]
  have hHs' : ∀ j y, H (tp s j y) = H y := fun j => by
    induction j with
    | zero => intro y; rfl
    | succ j ih => intro y; show H (s (tp s j y)) = _; rw [hHs, ih]
  have hHact : ∀ i j y, H (act s t i j y) = H y := fun i j y => by show H (tp s j (tp t i y)) = _; rw [hHs', hHt']
  have hpt : ∀ x, H x = H x * Lf (act s t 0 0 x) + H x * Lf (act s t 1 0 x) + H x * Lf (act s t 2 0 x) + H x * Lf (act s t 3 0 x) + H x * Lf (act s t 4 0 x) + H x * Lf (act s t 5 0 x) + H x * Lf (act s t 6 0 x) + H x * Lf (act s t 7 0 x) + H x * Lf (act s t 0 1 x) + H x * Lf (act s t 1 1 x) + H x * Lf (act s t 2 1 x) + H x * Lf (act s t 3 1 x) + H x * Lf (act s t 4 1 x) + H x * Lf (act s t 5 1 x)   -- (+68 chars; full line in the Zenodo source)
    by_cases hx : (act s t 1 0 x = x ∨ act s t 2 0 x = x ∨ act s t 3 0 x = x ∨ act s t 4 0 x = x ∨ act s t 5 0 x = x ∨ act s t 6 0 x = x ∨ act s t 7 0 x = x ∨ act s t 0 1 x = x ∨ act s t 1 1 x = x ∨ act s t 2 1 x = x ∨ act s t 3 1 x = x ∨ act s t 4 1 x = x ∨ act s t 5 1 x = x ∨ act s t 6 1 x = x ∨ act s t 7 1 x = x)
    · rw [hfix x hx]; lib_int
    · rw [hLact 0 0 x, hLact 1 0, hLact 2 0, hLact 3 0, hLact 4 0, hLact 5 0, hLact 6 0, hLact 7 0, hLact 0 1, hLact 1 1, hLact 2 1, hLact 3 1, hLact 4 1, hLact 5 1, hLact 6 1, hLact 7 1]
      have d0_1 : code (act s t 0 0 x) ≠ code (act s t 1 0 x) := fun e => hx (by
        have e2 : x = act s t 1 0 x := hinj _ _ e
        have e3 := e2.symm
        exact Or.inl e3)
      have d0_2 : code (act s t 0 0 x) ≠ code (act s t 2 0 x) := fun e => hx (by
        have e2 : x = act s t 2 0 x := hinj _ _ e
        have e3 := e2.symm
        exact Or.inr (Or.inl e3))
      have d0_3 : code (act s t 0 0 x) ≠ code (act s t 3 0 x) := fun e => hx (by
        have e2 : x = act s t 3 0 x := hinj _ _ e
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inl e3)))
      have d0_4 : code (act s t 0 0 x) ≠ code (act s t 4 0 x) := fun e => hx (by
        have e2 : x = act s t 4 0 x := hinj _ _ e
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inl e3))))
      have d0_5 : code (act s t 0 0 x) ≠ code (act s t 5 0 x) := fun e => hx (by
        have e2 : x = act s t 5 0 x := hinj _ _ e
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3)))))
      have d0_6 : code (act s t 0 0 x) ≠ code (act s t 6 0 x) := fun e => hx (by
        have e2 : x = act s t 6 0 x := hinj _ _ e
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3))))))
      have d0_7 : code (act s t 0 0 x) ≠ code (act s t 7 0 x) := fun e => hx (by
        have e2 : x = act s t 7 0 x := hinj _ _ e
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3)))))))
      have d0_8 : code (act s t 0 0 x) ≠ code (act s t 0 1 x) := fun e => hx (by
        have e2 : x = act s t 0 1 x := hinj _ _ e
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3))))))))
      have d0_9 : code (act s t 0 0 x) ≠ code (act s t 1 1 x) := fun e => hx (by
        have e2 : x = act s t 1 1 x := hinj _ _ e
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3)))))))))
      have d0_10 : code (act s t 0 0 x) ≠ code (act s t 2 1 x) := fun e => hx (by
        have e2 : x = act s t 2 1 x := hinj _ _ e
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3))))))))))
      have d0_11 : code (act s t 0 0 x) ≠ code (act s t 3 1 x) := fun e => hx (by
        have e2 : x = act s t 3 1 x := hinj _ _ e
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3)))))))))))
      have d0_12 : code (act s t 0 0 x) ≠ code (act s t 4 1 x) := fun e => hx (by
        have e2 : x = act s t 4 1 x := hinj _ _ e
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3))))))))))))
      have d0_13 : code (act s t 0 0 x) ≠ code (act s t 5 1 x) := fun e => hx (by
        have e2 : x = act s t 5 1 x := hinj _ _ e
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3)))))))))))))
      have d0_14 : code (act s t 0 0 x) ≠ code (act s t 6 1 x) := fun e => hx (by
        have e2 : x = act s t 6 1 x := hinj _ _ e
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3))))))))))))))
      have d0_15 : code (act s t 0 0 x) ≠ code (act s t 7 1 x) := fun e => hx (by
        have e2 : x = act s t 7 1 x := hinj _ _ e
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (e3)))))))))))))))
      have d1_2 : code (act s t 1 0 x) ≠ code (act s t 2 0 x) := fun e => hx (by
        have e1 := congrArg (act s t 7 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 9 0 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 9 0 x = act s t 1 0 x from act_red8 (ht := ht) 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inl e3)
      have d1_3 : code (act s t 1 0 x) ≠ code (act s t 3 0 x) := fun e => hx (by
        have e1 := congrArg (act s t 7 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 10 0 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 10 0 x = act s t 2 0 x from act_red8 (ht := ht) 2 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inl e3))
      have d1_4 : code (act s t 1 0 x) ≠ code (act s t 4 0 x) := fun e => hx (by
        have e1 := congrArg (act s t 7 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 11 0 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 11 0 x = act s t 3 0 x from act_red8 (ht := ht) 3 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inl e3)))
      have d1_5 : code (act s t 1 0 x) ≠ code (act s t 5 0 x) := fun e => hx (by
        have e1 := congrArg (act s t 7 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 12 0 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 12 0 x = act s t 4 0 x from act_red8 (ht := ht) 4 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inl e3))))
      have d1_6 : code (act s t 1 0 x) ≠ code (act s t 6 0 x) := fun e => hx (by
        have e1 := congrArg (act s t 7 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 13 0 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 13 0 x = act s t 5 0 x from act_red8 (ht := ht) 5 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3)))))
      have d1_7 : code (act s t 1 0 x) ≠ code (act s t 7 0 x) := fun e => hx (by
        have e1 := congrArg (act s t 7 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 14 0 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 14 0 x = act s t 6 0 x from act_red8 (ht := ht) 6 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3))))))
      have d1_8 : code (act s t 1 0 x) ≠ code (act s t 0 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 7 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 7 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (e3)))))))))))))))
      have d1_9 : code (act s t 1 0 x) ≠ code (act s t 1 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 7 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 8 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 8 1 x = act s t 0 1 x from act_red8 (ht := ht) 0 1 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3))))))))
      have d1_10 : code (act s t 1 0 x) ≠ code (act s t 2 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 7 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 9 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 9 1 x = act s t 1 1 x from act_red8 (ht := ht) 1 1 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3)))))))))
      have d1_11 : code (act s t 1 0 x) ≠ code (act s t 3 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 7 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 10 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 10 1 x = act s t 2 1 x from act_red8 (ht := ht) 2 1 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3))))))))))
      have d1_12 : code (act s t 1 0 x) ≠ code (act s t 4 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 7 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 11 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 11 1 x = act s t 3 1 x from act_red8 (ht := ht) 3 1 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3)))))))))))
      have d1_13 : code (act s t 1 0 x) ≠ code (act s t 5 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 7 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 12 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 12 1 x = act s t 4 1 x from act_red8 (ht := ht) 4 1 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3))))))))))))
      have d1_14 : code (act s t 1 0 x) ≠ code (act s t 6 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 7 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 13 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 13 1 x = act s t 5 1 x from act_red8 (ht := ht) 5 1 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3)))))))))))))
      have d1_15 : code (act s t 1 0 x) ≠ code (act s t 7 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 7 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 14 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 14 1 x = act s t 6 1 x from act_red8 (ht := ht) 6 1 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3))))))))))))))
      have d2_3 : code (act s t 2 0 x) ≠ code (act s t 3 0 x) := fun e => hx (by
        have e1 := congrArg (act s t 6 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 9 0 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 9 0 x = act s t 1 0 x from act_red8 (ht := ht) 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inl e3)
      have d2_4 : code (act s t 2 0 x) ≠ code (act s t 4 0 x) := fun e => hx (by
        have e1 := congrArg (act s t 6 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 10 0 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 10 0 x = act s t 2 0 x from act_red8 (ht := ht) 2 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inl e3))
      have d2_5 : code (act s t 2 0 x) ≠ code (act s t 5 0 x) := fun e => hx (by
        have e1 := congrArg (act s t 6 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 11 0 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 11 0 x = act s t 3 0 x from act_red8 (ht := ht) 3 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inl e3)))
      have d2_6 : code (act s t 2 0 x) ≠ code (act s t 6 0 x) := fun e => hx (by
        have e1 := congrArg (act s t 6 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 12 0 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 12 0 x = act s t 4 0 x from act_red8 (ht := ht) 4 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inl e3))))
      have d2_7 : code (act s t 2 0 x) ≠ code (act s t 7 0 x) := fun e => hx (by
        have e1 := congrArg (act s t 6 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 13 0 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 13 0 x = act s t 5 0 x from act_red8 (ht := ht) 5 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3)))))
      have d2_8 : code (act s t 2 0 x) ≠ code (act s t 0 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 6 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 6 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3))))))))))))))
      have d2_9 : code (act s t 2 0 x) ≠ code (act s t 1 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 6 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 7 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (e3)))))))))))))))
      have d2_10 : code (act s t 2 0 x) ≠ code (act s t 2 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 6 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 8 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 8 1 x = act s t 0 1 x from act_red8 (ht := ht) 0 1 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3))))))))
      have d2_11 : code (act s t 2 0 x) ≠ code (act s t 3 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 6 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 9 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 9 1 x = act s t 1 1 x from act_red8 (ht := ht) 1 1 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3)))))))))
      have d2_12 : code (act s t 2 0 x) ≠ code (act s t 4 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 6 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 10 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 10 1 x = act s t 2 1 x from act_red8 (ht := ht) 2 1 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3))))))))))
      have d2_13 : code (act s t 2 0 x) ≠ code (act s t 5 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 6 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 11 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 11 1 x = act s t 3 1 x from act_red8 (ht := ht) 3 1 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3)))))))))))
      have d2_14 : code (act s t 2 0 x) ≠ code (act s t 6 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 6 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 12 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 12 1 x = act s t 4 1 x from act_red8 (ht := ht) 4 1 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3))))))))))))
      have d2_15 : code (act s t 2 0 x) ≠ code (act s t 7 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 6 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 13 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 13 1 x = act s t 5 1 x from act_red8 (ht := ht) 5 1 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3)))))))))))))
      have d3_4 : code (act s t 3 0 x) ≠ code (act s t 4 0 x) := fun e => hx (by
        have e1 := congrArg (act s t 5 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 9 0 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 9 0 x = act s t 1 0 x from act_red8 (ht := ht) 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inl e3)
      have d3_5 : code (act s t 3 0 x) ≠ code (act s t 5 0 x) := fun e => hx (by
        have e1 := congrArg (act s t 5 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 10 0 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 10 0 x = act s t 2 0 x from act_red8 (ht := ht) 2 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inl e3))
      have d3_6 : code (act s t 3 0 x) ≠ code (act s t 6 0 x) := fun e => hx (by
        have e1 := congrArg (act s t 5 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 11 0 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 11 0 x = act s t 3 0 x from act_red8 (ht := ht) 3 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inl e3)))
      have d3_7 : code (act s t 3 0 x) ≠ code (act s t 7 0 x) := fun e => hx (by
        have e1 := congrArg (act s t 5 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 12 0 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 12 0 x = act s t 4 0 x from act_red8 (ht := ht) 4 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inl e3))))
      have d3_8 : code (act s t 3 0 x) ≠ code (act s t 0 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 5 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 5 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3)))))))))))))
      have d3_9 : code (act s t 3 0 x) ≠ code (act s t 1 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 5 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 6 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3))))))))))))))
      have d3_10 : code (act s t 3 0 x) ≠ code (act s t 2 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 5 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 7 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (e3)))))))))))))))
      have d3_11 : code (act s t 3 0 x) ≠ code (act s t 3 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 5 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 8 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 8 1 x = act s t 0 1 x from act_red8 (ht := ht) 0 1 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3))))))))
      have d3_12 : code (act s t 3 0 x) ≠ code (act s t 4 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 5 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 9 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 9 1 x = act s t 1 1 x from act_red8 (ht := ht) 1 1 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3)))))))))
      have d3_13 : code (act s t 3 0 x) ≠ code (act s t 5 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 5 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 10 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 10 1 x = act s t 2 1 x from act_red8 (ht := ht) 2 1 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3))))))))))
      have d3_14 : code (act s t 3 0 x) ≠ code (act s t 6 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 5 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 11 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 11 1 x = act s t 3 1 x from act_red8 (ht := ht) 3 1 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3)))))))))))
      have d3_15 : code (act s t 3 0 x) ≠ code (act s t 7 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 5 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 12 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 12 1 x = act s t 4 1 x from act_red8 (ht := ht) 4 1 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3))))))))))))
      have d4_5 : code (act s t 4 0 x) ≠ code (act s t 5 0 x) := fun e => hx (by
        have e1 := congrArg (act s t 4 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 9 0 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 9 0 x = act s t 1 0 x from act_red8 (ht := ht) 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inl e3)
      have d4_6 : code (act s t 4 0 x) ≠ code (act s t 6 0 x) := fun e => hx (by
        have e1 := congrArg (act s t 4 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 10 0 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 10 0 x = act s t 2 0 x from act_red8 (ht := ht) 2 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inl e3))
      have d4_7 : code (act s t 4 0 x) ≠ code (act s t 7 0 x) := fun e => hx (by
        have e1 := congrArg (act s t 4 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 11 0 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 11 0 x = act s t 3 0 x from act_red8 (ht := ht) 3 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inl e3)))
      have d4_8 : code (act s t 4 0 x) ≠ code (act s t 0 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 4 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 4 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3))))))))))))
      have d4_9 : code (act s t 4 0 x) ≠ code (act s t 1 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 4 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 5 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3)))))))))))))
      have d4_10 : code (act s t 4 0 x) ≠ code (act s t 2 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 4 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 6 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3))))))))))))))
      have d4_11 : code (act s t 4 0 x) ≠ code (act s t 3 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 4 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 7 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (e3)))))))))))))))
      have d4_12 : code (act s t 4 0 x) ≠ code (act s t 4 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 4 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 8 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 8 1 x = act s t 0 1 x from act_red8 (ht := ht) 0 1 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3))))))))
      have d4_13 : code (act s t 4 0 x) ≠ code (act s t 5 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 4 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 9 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 9 1 x = act s t 1 1 x from act_red8 (ht := ht) 1 1 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3)))))))))
      have d4_14 : code (act s t 4 0 x) ≠ code (act s t 6 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 4 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 10 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 10 1 x = act s t 2 1 x from act_red8 (ht := ht) 2 1 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3))))))))))
      have d4_15 : code (act s t 4 0 x) ≠ code (act s t 7 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 4 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 11 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 11 1 x = act s t 3 1 x from act_red8 (ht := ht) 3 1 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3)))))))))))
      have d5_6 : code (act s t 5 0 x) ≠ code (act s t 6 0 x) := fun e => hx (by
        have e1 := congrArg (act s t 3 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 9 0 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 9 0 x = act s t 1 0 x from act_red8 (ht := ht) 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inl e3)
      have d5_7 : code (act s t 5 0 x) ≠ code (act s t 7 0 x) := fun e => hx (by
        have e1 := congrArg (act s t 3 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 10 0 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 10 0 x = act s t 2 0 x from act_red8 (ht := ht) 2 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inl e3))
      have d5_8 : code (act s t 5 0 x) ≠ code (act s t 0 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 3 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 3 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3)))))))))))
      have d5_9 : code (act s t 5 0 x) ≠ code (act s t 1 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 3 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 4 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3))))))))))))
      have d5_10 : code (act s t 5 0 x) ≠ code (act s t 2 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 3 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 5 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3)))))))))))))
      have d5_11 : code (act s t 5 0 x) ≠ code (act s t 3 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 3 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 6 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3))))))))))))))
      have d5_12 : code (act s t 5 0 x) ≠ code (act s t 4 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 3 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 7 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (e3)))))))))))))))
      have d5_13 : code (act s t 5 0 x) ≠ code (act s t 5 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 3 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 8 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 8 1 x = act s t 0 1 x from act_red8 (ht := ht) 0 1 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3))))))))
      have d5_14 : code (act s t 5 0 x) ≠ code (act s t 6 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 3 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 9 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 9 1 x = act s t 1 1 x from act_red8 (ht := ht) 1 1 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3)))))))))
      have d5_15 : code (act s t 5 0 x) ≠ code (act s t 7 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 3 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 10 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 10 1 x = act s t 2 1 x from act_red8 (ht := ht) 2 1 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3))))))))))
      have d6_7 : code (act s t 6 0 x) ≠ code (act s t 7 0 x) := fun e => hx (by
        have e1 := congrArg (act s t 2 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 9 0 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 9 0 x = act s t 1 0 x from act_red8 (ht := ht) 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inl e3)
      have d6_8 : code (act s t 6 0 x) ≠ code (act s t 0 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 2 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 2 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3))))))))))
      have d6_9 : code (act s t 6 0 x) ≠ code (act s t 1 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 2 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 3 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3)))))))))))
      have d6_10 : code (act s t 6 0 x) ≠ code (act s t 2 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 2 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 4 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3))))))))))))
      have d6_11 : code (act s t 6 0 x) ≠ code (act s t 3 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 2 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 5 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3)))))))))))))
      have d6_12 : code (act s t 6 0 x) ≠ code (act s t 4 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 2 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 6 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3))))))))))))))
      have d6_13 : code (act s t 6 0 x) ≠ code (act s t 5 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 2 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 7 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (e3)))))))))))))))
      have d6_14 : code (act s t 6 0 x) ≠ code (act s t 6 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 2 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 8 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 8 1 x = act s t 0 1 x from act_red8 (ht := ht) 0 1 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3))))))))
      have d6_15 : code (act s t 6 0 x) ≠ code (act s t 7 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 2 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 9 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 9 1 x = act s t 1 1 x from act_red8 (ht := ht) 1 1 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3)))))))))
      have d7_8 : code (act s t 7 0 x) ≠ code (act s t 0 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 1 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 1 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3)))))))))
      have d7_9 : code (act s t 7 0 x) ≠ code (act s t 1 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 1 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 2 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3))))))))))
      have d7_10 : code (act s t 7 0 x) ≠ code (act s t 2 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 1 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 3 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3)))))))))))
      have d7_11 : code (act s t 7 0 x) ≠ code (act s t 3 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 1 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 4 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3))))))))))))
      have d7_12 : code (act s t 7 0 x) ≠ code (act s t 4 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 1 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 5 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3)))))))))))))
      have d7_13 : code (act s t 7 0 x) ≠ code (act s t 5 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 1 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 6 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3))))))))))))))
      have d7_14 : code (act s t 7 0 x) ≠ code (act s t 6 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 1 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 7 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (e3)))))))))))))))
      have d7_15 : code (act s t 7 0 x) ≠ code (act s t 7 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 1 0) (hinj _ _ e)
        have e2 : act s t 8 0 x = act s t 8 1 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 0 x = x from act_id8 ht hs 1 0 x] at e2
        rw [show act s t 8 1 x = act s t 0 1 x from act_red8 (ht := ht) 0 1 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3))))))))
      have d8_9 : code (act s t 0 1 x) ≠ code (act s t 1 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 0 1) (hinj _ _ e)
        have e2 : act s t 0 2 x = act s t 1 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 0 2 x = x from act_id8 ht hs 0 1 x] at e2
        rw [show act s t 1 2 x = act s t 1 0 x from act_red2 (hs := hs) 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inl e3)
      have d8_10 : code (act s t 0 1 x) ≠ code (act s t 2 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 0 1) (hinj _ _ e)
        have e2 : act s t 0 2 x = act s t 2 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 0 2 x = x from act_id8 ht hs 0 1 x] at e2
        rw [show act s t 2 2 x = act s t 2 0 x from act_red2 (hs := hs) 2 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inl e3))
      have d8_11 : code (act s t 0 1 x) ≠ code (act s t 3 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 0 1) (hinj _ _ e)
        have e2 : act s t 0 2 x = act s t 3 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 0 2 x = x from act_id8 ht hs 0 1 x] at e2
        rw [show act s t 3 2 x = act s t 3 0 x from act_red2 (hs := hs) 3 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inl e3)))
      have d8_12 : code (act s t 0 1 x) ≠ code (act s t 4 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 0 1) (hinj _ _ e)
        have e2 : act s t 0 2 x = act s t 4 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 0 2 x = x from act_id8 ht hs 0 1 x] at e2
        rw [show act s t 4 2 x = act s t 4 0 x from act_red2 (hs := hs) 4 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inl e3))))
      have d8_13 : code (act s t 0 1 x) ≠ code (act s t 5 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 0 1) (hinj _ _ e)
        have e2 : act s t 0 2 x = act s t 5 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 0 2 x = x from act_id8 ht hs 0 1 x] at e2
        rw [show act s t 5 2 x = act s t 5 0 x from act_red2 (hs := hs) 5 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3)))))
      have d8_14 : code (act s t 0 1 x) ≠ code (act s t 6 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 0 1) (hinj _ _ e)
        have e2 : act s t 0 2 x = act s t 6 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 0 2 x = x from act_id8 ht hs 0 1 x] at e2
        rw [show act s t 6 2 x = act s t 6 0 x from act_red2 (hs := hs) 6 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3))))))
      have d8_15 : code (act s t 0 1 x) ≠ code (act s t 7 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 0 1) (hinj _ _ e)
        have e2 : act s t 0 2 x = act s t 7 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 0 2 x = x from act_id8 ht hs 0 1 x] at e2
        rw [show act s t 7 2 x = act s t 7 0 x from act_red2 (hs := hs) 7 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3)))))))
      have d9_10 : code (act s t 1 1 x) ≠ code (act s t 2 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 7 1) (hinj _ _ e)
        have e2 : act s t 8 2 x = act s t 9 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 2 x = x from act_id8 ht hs 1 1 x] at e2
        rw [show act s t 9 2 x = act s t 1 2 x from act_red8 (ht := ht) 1 2 x] at e2
        rw [show act s t 1 2 x = act s t 1 0 x from act_red2 (hs := hs) 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inl e3)
      have d9_11 : code (act s t 1 1 x) ≠ code (act s t 3 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 7 1) (hinj _ _ e)
        have e2 : act s t 8 2 x = act s t 10 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 2 x = x from act_id8 ht hs 1 1 x] at e2
        rw [show act s t 10 2 x = act s t 2 2 x from act_red8 (ht := ht) 2 2 x] at e2
        rw [show act s t 2 2 x = act s t 2 0 x from act_red2 (hs := hs) 2 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inl e3))
      have d9_12 : code (act s t 1 1 x) ≠ code (act s t 4 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 7 1) (hinj _ _ e)
        have e2 : act s t 8 2 x = act s t 11 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 2 x = x from act_id8 ht hs 1 1 x] at e2
        rw [show act s t 11 2 x = act s t 3 2 x from act_red8 (ht := ht) 3 2 x] at e2
        rw [show act s t 3 2 x = act s t 3 0 x from act_red2 (hs := hs) 3 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inl e3)))
      have d9_13 : code (act s t 1 1 x) ≠ code (act s t 5 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 7 1) (hinj _ _ e)
        have e2 : act s t 8 2 x = act s t 12 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 2 x = x from act_id8 ht hs 1 1 x] at e2
        rw [show act s t 12 2 x = act s t 4 2 x from act_red8 (ht := ht) 4 2 x] at e2
        rw [show act s t 4 2 x = act s t 4 0 x from act_red2 (hs := hs) 4 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inl e3))))
      have d9_14 : code (act s t 1 1 x) ≠ code (act s t 6 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 7 1) (hinj _ _ e)
        have e2 : act s t 8 2 x = act s t 13 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 2 x = x from act_id8 ht hs 1 1 x] at e2
        rw [show act s t 13 2 x = act s t 5 2 x from act_red8 (ht := ht) 5 2 x] at e2
        rw [show act s t 5 2 x = act s t 5 0 x from act_red2 (hs := hs) 5 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3)))))
      have d9_15 : code (act s t 1 1 x) ≠ code (act s t 7 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 7 1) (hinj _ _ e)
        have e2 : act s t 8 2 x = act s t 14 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 2 x = x from act_id8 ht hs 1 1 x] at e2
        rw [show act s t 14 2 x = act s t 6 2 x from act_red8 (ht := ht) 6 2 x] at e2
        rw [show act s t 6 2 x = act s t 6 0 x from act_red2 (hs := hs) 6 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3))))))
      have d10_11 : code (act s t 2 1 x) ≠ code (act s t 3 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 6 1) (hinj _ _ e)
        have e2 : act s t 8 2 x = act s t 9 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 2 x = x from act_id8 ht hs 1 1 x] at e2
        rw [show act s t 9 2 x = act s t 1 2 x from act_red8 (ht := ht) 1 2 x] at e2
        rw [show act s t 1 2 x = act s t 1 0 x from act_red2 (hs := hs) 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inl e3)
      have d10_12 : code (act s t 2 1 x) ≠ code (act s t 4 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 6 1) (hinj _ _ e)
        have e2 : act s t 8 2 x = act s t 10 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 2 x = x from act_id8 ht hs 1 1 x] at e2
        rw [show act s t 10 2 x = act s t 2 2 x from act_red8 (ht := ht) 2 2 x] at e2
        rw [show act s t 2 2 x = act s t 2 0 x from act_red2 (hs := hs) 2 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inl e3))
      have d10_13 : code (act s t 2 1 x) ≠ code (act s t 5 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 6 1) (hinj _ _ e)
        have e2 : act s t 8 2 x = act s t 11 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 2 x = x from act_id8 ht hs 1 1 x] at e2
        rw [show act s t 11 2 x = act s t 3 2 x from act_red8 (ht := ht) 3 2 x] at e2
        rw [show act s t 3 2 x = act s t 3 0 x from act_red2 (hs := hs) 3 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inl e3)))
      have d10_14 : code (act s t 2 1 x) ≠ code (act s t 6 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 6 1) (hinj _ _ e)
        have e2 : act s t 8 2 x = act s t 12 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 2 x = x from act_id8 ht hs 1 1 x] at e2
        rw [show act s t 12 2 x = act s t 4 2 x from act_red8 (ht := ht) 4 2 x] at e2
        rw [show act s t 4 2 x = act s t 4 0 x from act_red2 (hs := hs) 4 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inl e3))))
      have d10_15 : code (act s t 2 1 x) ≠ code (act s t 7 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 6 1) (hinj _ _ e)
        have e2 : act s t 8 2 x = act s t 13 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 2 x = x from act_id8 ht hs 1 1 x] at e2
        rw [show act s t 13 2 x = act s t 5 2 x from act_red8 (ht := ht) 5 2 x] at e2
        rw [show act s t 5 2 x = act s t 5 0 x from act_red2 (hs := hs) 5 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e3)))))
      have d11_12 : code (act s t 3 1 x) ≠ code (act s t 4 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 5 1) (hinj _ _ e)
        have e2 : act s t 8 2 x = act s t 9 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 2 x = x from act_id8 ht hs 1 1 x] at e2
        rw [show act s t 9 2 x = act s t 1 2 x from act_red8 (ht := ht) 1 2 x] at e2
        rw [show act s t 1 2 x = act s t 1 0 x from act_red2 (hs := hs) 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inl e3)
      have d11_13 : code (act s t 3 1 x) ≠ code (act s t 5 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 5 1) (hinj _ _ e)
        have e2 : act s t 8 2 x = act s t 10 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 2 x = x from act_id8 ht hs 1 1 x] at e2
        rw [show act s t 10 2 x = act s t 2 2 x from act_red8 (ht := ht) 2 2 x] at e2
        rw [show act s t 2 2 x = act s t 2 0 x from act_red2 (hs := hs) 2 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inl e3))
      have d11_14 : code (act s t 3 1 x) ≠ code (act s t 6 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 5 1) (hinj _ _ e)
        have e2 : act s t 8 2 x = act s t 11 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 2 x = x from act_id8 ht hs 1 1 x] at e2
        rw [show act s t 11 2 x = act s t 3 2 x from act_red8 (ht := ht) 3 2 x] at e2
        rw [show act s t 3 2 x = act s t 3 0 x from act_red2 (hs := hs) 3 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inl e3)))
      have d11_15 : code (act s t 3 1 x) ≠ code (act s t 7 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 5 1) (hinj _ _ e)
        have e2 : act s t 8 2 x = act s t 12 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 2 x = x from act_id8 ht hs 1 1 x] at e2
        rw [show act s t 12 2 x = act s t 4 2 x from act_red8 (ht := ht) 4 2 x] at e2
        rw [show act s t 4 2 x = act s t 4 0 x from act_red2 (hs := hs) 4 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inr (Or.inl e3))))
      have d12_13 : code (act s t 4 1 x) ≠ code (act s t 5 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 4 1) (hinj _ _ e)
        have e2 : act s t 8 2 x = act s t 9 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 2 x = x from act_id8 ht hs 1 1 x] at e2
        rw [show act s t 9 2 x = act s t 1 2 x from act_red8 (ht := ht) 1 2 x] at e2
        rw [show act s t 1 2 x = act s t 1 0 x from act_red2 (hs := hs) 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inl e3)
      have d12_14 : code (act s t 4 1 x) ≠ code (act s t 6 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 4 1) (hinj _ _ e)
        have e2 : act s t 8 2 x = act s t 10 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 2 x = x from act_id8 ht hs 1 1 x] at e2
        rw [show act s t 10 2 x = act s t 2 2 x from act_red8 (ht := ht) 2 2 x] at e2
        rw [show act s t 2 2 x = act s t 2 0 x from act_red2 (hs := hs) 2 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inl e3))
      have d12_15 : code (act s t 4 1 x) ≠ code (act s t 7 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 4 1) (hinj _ _ e)
        have e2 : act s t 8 2 x = act s t 11 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 2 x = x from act_id8 ht hs 1 1 x] at e2
        rw [show act s t 11 2 x = act s t 3 2 x from act_red8 (ht := ht) 3 2 x] at e2
        rw [show act s t 3 2 x = act s t 3 0 x from act_red2 (hs := hs) 3 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inr (Or.inl e3)))
      have d13_14 : code (act s t 5 1 x) ≠ code (act s t 6 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 3 1) (hinj _ _ e)
        have e2 : act s t 8 2 x = act s t 9 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 2 x = x from act_id8 ht hs 1 1 x] at e2
        rw [show act s t 9 2 x = act s t 1 2 x from act_red8 (ht := ht) 1 2 x] at e2
        rw [show act s t 1 2 x = act s t 1 0 x from act_red2 (hs := hs) 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inl e3)
      have d13_15 : code (act s t 5 1 x) ≠ code (act s t 7 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 3 1) (hinj _ _ e)
        have e2 : act s t 8 2 x = act s t 10 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 2 x = x from act_id8 ht hs 1 1 x] at e2
        rw [show act s t 10 2 x = act s t 2 2 x from act_red8 (ht := ht) 2 2 x] at e2
        rw [show act s t 2 2 x = act s t 2 0 x from act_red2 (hs := hs) 2 0 x] at e2
        have e3 := e2.symm
        exact Or.inr (Or.inl e3))
      have d14_15 : code (act s t 6 1 x) ≠ code (act s t 7 1 x) := fun e => hx (by
        have e1 := congrArg (act s t 2 1) (hinj _ _ e)
        have e2 : act s t 8 2 x = act s t 9 2 x := by rw [act_comp hst, act_comp hst] at e1; exact e1
        rw [show act s t 8 2 x = x from act_id8 ht hs 1 1 x] at e2
        rw [show act s t 9 2 x = act s t 1 2 x from act_red8 (ht := ht) 1 2 x] at e2
        rw [show act s t 1 2 x = act s t 1 0 x from act_red2 (hs := hs) 1 0 x] at e2
        have e3 := e2.symm
        exact Or.inl e3)
      have hm := one_min16 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d0_1 d0_2 d0_3 d0_4 d0_5 d0_6 d0_7 d0_8 d0_9 d0_10 d0_11 d0_12 d0_13 d0_14 d0_15 d1_2 d1_3 d1_4 d1_5 d1_6 d1_7 d1_8 d1_9 d1_10 d1_11 d1_12 d1_13 d1_14 d1_15 d2_3 d2_4 d2_5 d2_6 d2_7 d2_8 d2_9 d2_10 d2_11 d2_12 d2_13 d2_14 d2_15 d3_4 d3_5 d3_6 d3_7 d3_8 d3_9 d3_10 d3_11 d3_12 d3_13 d3_14 d3_15 d4_5 d4_6 d4_7 d4_8 d4_9 d4_10 d4_11 d4_12 d4_1  -- (+348 chars; full line in the Zenodo source)
      generalize m16 (code (act s t 0 0 x)) (code (act s t 1 0 x)) (code (act s t 2 0 x)) (code (act s t 3 0 x)) (code (act s t 4 0 x)) (code (act s t 5 0 x)) (code (act s t 6 0 x)) (code (act s t 7 0 x)) (code (act s t 0 1 x)) (code (act s t 1 1 x)) (code (act s t 2 1 x)) (code (act s t 3 1 x)) (code (act s t 4 1 x)) (code (act s t 5 1 x)) (code (act s t 6 1 x)) (code (act s t 7 1 x)) = μ at hm ⊢
      calc H x = H x * (eqI (code (act s t 0 0 x)) (μ) + eqI (code (act s t 1 0 x)) (μ) + eqI (code (act s t 2 0 x)) (μ) + eqI (code (act s t 3 0 x)) (μ) + eqI (code (act s t 4 0 x)) (μ) + eqI (code (act s t 5 0 x)) (μ) + eqI (code (act s t 6 0 x)) (μ) + eqI (code (act s t 7 0 x)) (μ) + eqI (code (act s t 0 1 x)) (μ) + eqI (code (act s t 1 1 x)) (μ) + eqI (code (act s t 2 1 x)) (μ) + eqI (code (ac  -- (+173 chars; full line in the Zenodo source)
        _ = _ := by lib_int
  have q1_0 : sumK LibInt.intR (fun x => H x * Lf (act s t 1 0 x)) els = sumK LibInt.intR (fun x => H x * Lf x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (act s t 1 0) (act s t 7 0)
      (fun y => by rw [act_comp hst]; exact act_id8 ht hs 1 0 y) (fun y => by rw [act_comp hst]; exact act_id8 ht hs 1 0 y) (fun y => H y * Lf y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Lf (act s t 1 0 x) = H (act s t 1 0 x) * Lf (act s t 1 0 x); rw [hHact]) _
  have q2_0 : sumK LibInt.intR (fun x => H x * Lf (act s t 2 0 x)) els = sumK LibInt.intR (fun x => H x * Lf x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (act s t 2 0) (act s t 6 0)
      (fun y => by rw [act_comp hst]; exact act_id8 ht hs 1 0 y) (fun y => by rw [act_comp hst]; exact act_id8 ht hs 1 0 y) (fun y => H y * Lf y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Lf (act s t 2 0 x) = H (act s t 2 0 x) * Lf (act s t 2 0 x); rw [hHact]) _
  have q3_0 : sumK LibInt.intR (fun x => H x * Lf (act s t 3 0 x)) els = sumK LibInt.intR (fun x => H x * Lf x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (act s t 3 0) (act s t 5 0)
      (fun y => by rw [act_comp hst]; exact act_id8 ht hs 1 0 y) (fun y => by rw [act_comp hst]; exact act_id8 ht hs 1 0 y) (fun y => H y * Lf y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Lf (act s t 3 0 x) = H (act s t 3 0 x) * Lf (act s t 3 0 x); rw [hHact]) _
  have q4_0 : sumK LibInt.intR (fun x => H x * Lf (act s t 4 0 x)) els = sumK LibInt.intR (fun x => H x * Lf x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (act s t 4 0) (act s t 4 0)
      (fun y => by rw [act_comp hst]; exact act_id8 ht hs 1 0 y) (fun y => by rw [act_comp hst]; exact act_id8 ht hs 1 0 y) (fun y => H y * Lf y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Lf (act s t 4 0 x) = H (act s t 4 0 x) * Lf (act s t 4 0 x); rw [hHact]) _
  have q5_0 : sumK LibInt.intR (fun x => H x * Lf (act s t 5 0 x)) els = sumK LibInt.intR (fun x => H x * Lf x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (act s t 5 0) (act s t 3 0)
      (fun y => by rw [act_comp hst]; exact act_id8 ht hs 1 0 y) (fun y => by rw [act_comp hst]; exact act_id8 ht hs 1 0 y) (fun y => H y * Lf y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Lf (act s t 5 0 x) = H (act s t 5 0 x) * Lf (act s t 5 0 x); rw [hHact]) _
  have q6_0 : sumK LibInt.intR (fun x => H x * Lf (act s t 6 0 x)) els = sumK LibInt.intR (fun x => H x * Lf x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (act s t 6 0) (act s t 2 0)
      (fun y => by rw [act_comp hst]; exact act_id8 ht hs 1 0 y) (fun y => by rw [act_comp hst]; exact act_id8 ht hs 1 0 y) (fun y => H y * Lf y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Lf (act s t 6 0 x) = H (act s t 6 0 x) * Lf (act s t 6 0 x); rw [hHact]) _
  have q7_0 : sumK LibInt.intR (fun x => H x * Lf (act s t 7 0 x)) els = sumK LibInt.intR (fun x => H x * Lf x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (act s t 7 0) (act s t 1 0)
      (fun y => by rw [act_comp hst]; exact act_id8 ht hs 1 0 y) (fun y => by rw [act_comp hst]; exact act_id8 ht hs 1 0 y) (fun y => H y * Lf y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Lf (act s t 7 0 x) = H (act s t 7 0 x) * Lf (act s t 7 0 x); rw [hHact]) _
  have q0_1 : sumK LibInt.intR (fun x => H x * Lf (act s t 0 1 x)) els = sumK LibInt.intR (fun x => H x * Lf x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (act s t 0 1) (act s t 0 1)
      (fun y => by rw [act_comp hst]; exact act_id8 ht hs 0 1 y) (fun y => by rw [act_comp hst]; exact act_id8 ht hs 0 1 y) (fun y => H y * Lf y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Lf (act s t 0 1 x) = H (act s t 0 1 x) * Lf (act s t 0 1 x); rw [hHact]) _
  have q1_1 : sumK LibInt.intR (fun x => H x * Lf (act s t 1 1 x)) els = sumK LibInt.intR (fun x => H x * Lf x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (act s t 1 1) (act s t 7 1)
      (fun y => by rw [act_comp hst]; exact act_id8 ht hs 1 1 y) (fun y => by rw [act_comp hst]; exact act_id8 ht hs 1 1 y) (fun y => H y * Lf y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Lf (act s t 1 1 x) = H (act s t 1 1 x) * Lf (act s t 1 1 x); rw [hHact]) _
  have q2_1 : sumK LibInt.intR (fun x => H x * Lf (act s t 2 1 x)) els = sumK LibInt.intR (fun x => H x * Lf x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (act s t 2 1) (act s t 6 1)
      (fun y => by rw [act_comp hst]; exact act_id8 ht hs 1 1 y) (fun y => by rw [act_comp hst]; exact act_id8 ht hs 1 1 y) (fun y => H y * Lf y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Lf (act s t 2 1 x) = H (act s t 2 1 x) * Lf (act s t 2 1 x); rw [hHact]) _
  have q3_1 : sumK LibInt.intR (fun x => H x * Lf (act s t 3 1 x)) els = sumK LibInt.intR (fun x => H x * Lf x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (act s t 3 1) (act s t 5 1)
      (fun y => by rw [act_comp hst]; exact act_id8 ht hs 1 1 y) (fun y => by rw [act_comp hst]; exact act_id8 ht hs 1 1 y) (fun y => H y * Lf y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Lf (act s t 3 1 x) = H (act s t 3 1 x) * Lf (act s t 3 1 x); rw [hHact]) _
  have q4_1 : sumK LibInt.intR (fun x => H x * Lf (act s t 4 1 x)) els = sumK LibInt.intR (fun x => H x * Lf x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (act s t 4 1) (act s t 4 1)
      (fun y => by rw [act_comp hst]; exact act_id8 ht hs 1 1 y) (fun y => by rw [act_comp hst]; exact act_id8 ht hs 1 1 y) (fun y => H y * Lf y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Lf (act s t 4 1 x) = H (act s t 4 1 x) * Lf (act s t 4 1 x); rw [hHact]) _
  have q5_1 : sumK LibInt.intR (fun x => H x * Lf (act s t 5 1 x)) els = sumK LibInt.intR (fun x => H x * Lf x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (act s t 5 1) (act s t 3 1)
      (fun y => by rw [act_comp hst]; exact act_id8 ht hs 1 1 y) (fun y => by rw [act_comp hst]; exact act_id8 ht hs 1 1 y) (fun y => H y * Lf y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Lf (act s t 5 1 x) = H (act s t 5 1 x) * Lf (act s t 5 1 x); rw [hHact]) _
  have q6_1 : sumK LibInt.intR (fun x => H x * Lf (act s t 6 1 x)) els = sumK LibInt.intR (fun x => H x * Lf x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (act s t 6 1) (act s t 2 1)
      (fun y => by rw [act_comp hst]; exact act_id8 ht hs 1 1 y) (fun y => by rw [act_comp hst]; exact act_id8 ht hs 1 1 y) (fun y => H y * Lf y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Lf (act s t 6 1 x) = H (act s t 6 1 x) * Lf (act s t 6 1 x); rw [hHact]) _
  have q7_1 : sumK LibInt.intR (fun x => H x * Lf (act s t 7 1 x)) els = sumK LibInt.intR (fun x => H x * Lf x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (act s t 7 1) (act s t 1 1)
      (fun y => by rw [act_comp hst]; exact act_id8 ht hs 1 1 y) (fun y => by rw [act_comp hst]; exact act_id8 ht hs 1 1 y) (fun y => H y * Lf y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Lf (act s t 7 1 x) = H (act s t 7 1 x) * Lf (act s t 7 1 x); rw [hHact]) _
  have q0_0 : sumK LibInt.intR (fun x => H x * Lf (act s t 0 0 x)) els = sumK LibInt.intR (fun x => H x * Lf x) els := rfl
  rw [sumK_ext LibInt.intR _ _ hpt, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add,
    q0_0, q1_0, q2_0, q3_0, q4_0, q5_0, q6_0, q7_0, q0_1, q1_1, q2_1, q3_1, q4_1, q5_1, q6_1, q7_1]
  show _ = 16 * sumK LibInt.intR (fun x => H x * Lf x) els
  generalize sumK LibInt.intR (fun x => H x * Lf x) els = T
  lib_int

end General

end Orbit82

#print axioms Orbit82.one_min16
#print axioms Orbit82.act_id8
#print axioms Orbit82.act_red8
#print axioms Orbit82.M_t
#print axioms Orbit82.M_s
#print axioms Orbit82.orbit82

lang/Orbit9.lean

lean_all/lang/Orbit9.lean

/-
  lang/Orbit9 — T43: counting orbits of an order-9 map by sums (the order-9 analogue of lang/Orbit4, lang/Orbit8).

    m9, m9_mem, m9_le, m9_rot, one_min9
    orbit9   σ^9 = id, H∘σ = H, H = 0 wherever σ^3x = x ⟹ Σ H = 9·Σ H·[x least in its orbit]
             (σᵏx = x for some 0 < k < 9 forces σ^3x = x: k·j ≡ 3 (mod 9) for a suitable j)
  Generated by a std-lib Python script. Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.Orbit4

namespace Orbit9

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum Orbit5
open FiniteRecChain (ite_pos' ite_neg')


def m9 (a0 a1 a2 a3 a4 a5 a6 a7 a8 : Nat) : Nat := mn a0 (mn a1 (mn a2 (mn a3 (mn a4 (mn a5 (mn a6 (mn a7 (a8))))))))
theorem m9_mem (a0 a1 a2 a3 a4 a5 a6 a7 a8 : Nat) : m9 a0 a1 a2 a3 a4 a5 a6 a7 a8 = a0 ∨ m9 a0 a1 a2 a3 a4 a5 a6 a7 a8 = a1 ∨ m9 a0 a1 a2 a3 a4 a5 a6 a7 a8 = a2 ∨ m9 a0 a1 a2 a3 a4 a5 a6 a7 a8 = a3 ∨ m9 a0 a1 a2 a3 a4 a5 a6 a7 a8 = a4 ∨ m9 a0 a1 a2 a3 a4 a5 a6 a7 a8 = a5 ∨ m9 a0 a1 a2 a3 a4 a5 a6 a7 a8 = a6 ∨ m9 a0 a1 a2 a3 a4 a5 a6 a7 a8 = a7 ∨ m9 a0 a1 a2 a3 a4 a5 a6 a7 a8 = a8 := by
  unfold m9
  rcases mn_or a0 (mn a1 (mn a2 (mn a3 (mn a4 (mn a5 (mn a6 (mn a7 (a8)))))))) with h | h
  · exact Or.inl h
  rw [h]
  rcases mn_or a1 (mn a2 (mn a3 (mn a4 (mn a5 (mn a6 (mn a7 (a8))))))) with h | h
  · exact Or.inr (Or.inl h)
  rw [h]
  rcases mn_or a2 (mn a3 (mn a4 (mn a5 (mn a6 (mn a7 (a8)))))) with h | h
  · exact Or.inr (Or.inr (Or.inl h))
  rw [h]
  rcases mn_or a3 (mn a4 (mn a5 (mn a6 (mn a7 (a8))))) with h | h
  · exact Or.inr (Or.inr (Or.inr (Or.inl h)))
  rw [h]
  rcases mn_or a4 (mn a5 (mn a6 (mn a7 (a8)))) with h | h
  · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h))))
  rw [h]
  rcases mn_or a5 (mn a6 (mn a7 (a8))) with h | h
  · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h)))))
  rw [h]
  rcases mn_or a6 (mn a7 (a8)) with h | h
  · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h))))))
  rw [h]
  rcases mn_or a7 (a8) with h | h
  · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h)))))))
  rw [h]
  exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (rfl))))))))
theorem m9_le (a0 a1 a2 a3 a4 a5 a6 a7 a8 : Nat) : m9 a0 a1 a2 a3 a4 a5 a6 a7 a8 ≤ a0 ∧ m9 a0 a1 a2 a3 a4 a5 a6 a7 a8 ≤ a1 ∧ m9 a0 a1 a2 a3 a4 a5 a6 a7 a8 ≤ a2 ∧ m9 a0 a1 a2 a3 a4 a5 a6 a7 a8 ≤ a3 ∧ m9 a0 a1 a2 a3 a4 a5 a6 a7 a8 ≤ a4 ∧ m9 a0 a1 a2 a3 a4 a5 a6 a7 a8 ≤ a5 ∧ m9 a0 a1 a2 a3 a4 a5 a6 a7 a8 ≤ a6 ∧ m9 a0 a1 a2 a3 a4 a5 a6 a7 a8 ≤ a7 ∧ m9 a0 a1 a2 a3 a4 a5 a6 a7 a8 ≤ a8 := by
  unfold m9
  exact ⟨mn_le_l _ _, Nat.le_trans (mn_le_r _ _) (mn_le_l _ _), Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (mn_le_l _ _)), Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (mn_le_l _ _))), Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (mn_le_l _ _)))), Nat.le_trans (mn_le_r _ _) (Nat.le_t  -- (+740 chars; full line in the Zenodo source)
theorem m9_rot (a0 a1 a2 a3 a4 a5 a6 a7 a8 : Nat) : m9 a1 a2 a3 a4 a5 a6 a7 a8 a0 = m9 a0 a1 a2 a3 a4 a5 a6 a7 a8 := by
  have l1 := m9_le a0 a1 a2 a3 a4 a5 a6 a7 a8
  have l2 := m9_le a1 a2 a3 a4 a5 a6 a7 a8 a0
  apply Nat.le_antisymm
  · rcases m9_mem a0 a1 a2 a3 a4 a5 a6 a7 a8 with h | h | h | h | h | h | h | h | h <;> rw [h]
    · exact l2.2.2.2.2.2.2.2.2
    · exact l2.1
    · exact l2.2.1
    · exact l2.2.2.1
    · exact l2.2.2.2.1
    · exact l2.2.2.2.2.1
    · exact l2.2.2.2.2.2.1
    · exact l2.2.2.2.2.2.2.1
    · exact l2.2.2.2.2.2.2.2.1
  · rcases m9_mem a1 a2 a3 a4 a5 a6 a7 a8 a0 with h | h | h | h | h | h | h | h | h <;> rw [h]
    · exact l1.2.1
    · exact l1.2.2.1
    · exact l1.2.2.2.1
    · exact l1.2.2.2.2.1
    · exact l1.2.2.2.2.2.1
    · exact l1.2.2.2.2.2.2.1
    · exact l1.2.2.2.2.2.2.2.1
    · exact l1.2.2.2.2.2.2.2.2
    · exact l1.1
theorem one_min9 (a0 a1 a2 a3 a4 a5 a6 a7 a8 : Nat) (h0_1 : a0 ≠ a1) (h0_2 : a0 ≠ a2) (h0_3 : a0 ≠ a3) (h0_4 : a0 ≠ a4) (h0_5 : a0 ≠ a5) (h0_6 : a0 ≠ a6) (h0_7 : a0 ≠ a7) (h0_8 : a0 ≠ a8) (h1_2 : a1 ≠ a2) (h1_3 : a1 ≠ a3) (h1_4 : a1 ≠ a4) (h1_5 : a1 ≠ a5) (h1_6 : a1 ≠ a6) (h1_7 : a1 ≠ a7) (h1_8 : a1 ≠ a8) (h2_3 : a2 ≠ a3) (h2_4 : a2 ≠ a4) (h2_5 : a2 ≠ a5) (h2_6 : a2 ≠ a6) (h2_7 : a2 ≠ a7) (h2_8 :   -- (+265 chars; full line in the Zenodo source)
    eqI a0 (m9 a0 a1 a2 a3 a4 a5 a6 a7 a8) + eqI a1 (m9 a0 a1 a2 a3 a4 a5 a6 a7 a8) + eqI a2 (m9 a0 a1 a2 a3 a4 a5 a6 a7 a8) + eqI a3 (m9 a0 a1 a2 a3 a4 a5 a6 a7 a8) + eqI a4 (m9 a0 a1 a2 a3 a4 a5 a6 a7 a8) + eqI a5 (m9 a0 a1 a2 a3 a4 a5 a6 a7 a8) + eqI a6 (m9 a0 a1 a2 a3 a4 a5 a6 a7 a8) + eqI a7 (m9 a0 a1 a2 a3 a4 a5 a6 a7 a8) + eqI a8 (m9 a0 a1 a2 a3 a4 a5 a6 a7 a8) = 1 := by
  unfold eqI
  rcases m9_mem a0 a1 a2 a3 a4 a5 a6 a7 a8 with h | h | h | h | h | h | h | h | h <;> rw [h]
  · rw [ite_pos' _ _ rfl, ite_neg' _ _ (fun e => h0_1 e.symm), ite_neg' _ _ (fun e => h0_2 e.symm), ite_neg' _ _ (fun e => h0_3 e.symm), ite_neg' _ _ (fun e => h0_4 e.symm), ite_neg' _ _ (fun e => h0_5 e.symm), ite_neg' _ _ (fun e => h0_6 e.symm), ite_neg' _ _ (fun e => h0_7 e.symm), ite_neg' _ _ (fun e => h0_8 e.symm)]; rfl
  · rw [ite_neg' _ _ h0_1, ite_pos' _ _ rfl, ite_neg' _ _ (fun e => h1_2 e.symm), ite_neg' _ _ (fun e => h1_3 e.symm), ite_neg' _ _ (fun e => h1_4 e.symm), ite_neg' _ _ (fun e => h1_5 e.symm), ite_neg' _ _ (fun e => h1_6 e.symm), ite_neg' _ _ (fun e => h1_7 e.symm), ite_neg' _ _ (fun e => h1_8 e.symm)]; rfl
  · rw [ite_neg' _ _ h0_2, ite_neg' _ _ h1_2, ite_pos' _ _ rfl, ite_neg' _ _ (fun e => h2_3 e.symm), ite_neg' _ _ (fun e => h2_4 e.symm), ite_neg' _ _ (fun e => h2_5 e.symm), ite_neg' _ _ (fun e => h2_6 e.symm), ite_neg' _ _ (fun e => h2_7 e.symm), ite_neg' _ _ (fun e => h2_8 e.symm)]; rfl
  · rw [ite_neg' _ _ h0_3, ite_neg' _ _ h1_3, ite_neg' _ _ h2_3, ite_pos' _ _ rfl, ite_neg' _ _ (fun e => h3_4 e.symm), ite_neg' _ _ (fun e => h3_5 e.symm), ite_neg' _ _ (fun e => h3_6 e.symm), ite_neg' _ _ (fun e => h3_7 e.symm), ite_neg' _ _ (fun e => h3_8 e.symm)]; rfl
  · rw [ite_neg' _ _ h0_4, ite_neg' _ _ h1_4, ite_neg' _ _ h2_4, ite_neg' _ _ h3_4, ite_pos' _ _ rfl, ite_neg' _ _ (fun e => h4_5 e.symm), ite_neg' _ _ (fun e => h4_6 e.symm), ite_neg' _ _ (fun e => h4_7 e.symm), ite_neg' _ _ (fun e => h4_8 e.symm)]; rfl
  · rw [ite_neg' _ _ h0_5, ite_neg' _ _ h1_5, ite_neg' _ _ h2_5, ite_neg' _ _ h3_5, ite_neg' _ _ h4_5, ite_pos' _ _ rfl, ite_neg' _ _ (fun e => h5_6 e.symm), ite_neg' _ _ (fun e => h5_7 e.symm), ite_neg' _ _ (fun e => h5_8 e.symm)]; rfl
  · rw [ite_neg' _ _ h0_6, ite_neg' _ _ h1_6, ite_neg' _ _ h2_6, ite_neg' _ _ h3_6, ite_neg' _ _ h4_6, ite_neg' _ _ h5_6, ite_pos' _ _ rfl, ite_neg' _ _ (fun e => h6_7 e.symm), ite_neg' _ _ (fun e => h6_8 e.symm)]; rfl
  · rw [ite_neg' _ _ h0_7, ite_neg' _ _ h1_7, ite_neg' _ _ h2_7, ite_neg' _ _ h3_7, ite_neg' _ _ h4_7, ite_neg' _ _ h5_7, ite_neg' _ _ h6_7, ite_pos' _ _ rfl, ite_neg' _ _ (fun e => h7_8 e.symm)]; rfl
  · rw [ite_neg' _ _ h0_8, ite_neg' _ _ h1_8, ite_neg' _ _ h2_8, ite_neg' _ _ h3_8, ite_neg' _ _ h4_8, ite_neg' _ _ h5_8, ite_neg' _ _ h6_8, ite_neg' _ _ h7_8, ite_pos' _ _ rfl]; rfl

section General
variable {G : Type} [DecidableEq G]

/-- **Orbits of an order-9 map** with H = 0 on the points fixed by σ^3. -/
theorem orbit9 (els : List G) (hc : Complete LibInt.intR els) (code : G → Nat) (hinj : ∀ u v, code u = code v → u = v)
    (sg : G → G) (hN : ∀ x, sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))) = x) (H : G → Int) (hH : ∀ x, H (sg x) = H x) (hfix : ∀ x, sg (sg (sg (x))) = x → H x = 0) :
    sumK LibInt.intR H els = 9 * sumK LibInt.intR (fun x => H x * eqI (code x) (m9 (code (x)) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))) els := by
  let Ls : G → Int := fun x => eqI (code x) (m9 (code (x)) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))
  have hpt : ∀ x, H x = H x * Ls (x) + H x * Ls (sg (x)) + H x * Ls (sg (sg (x))) + H x * Ls (sg (sg (sg (x)))) + H x * Ls (sg (sg (sg (sg (x))))) + H x * Ls (sg (sg (sg (sg (sg (x)))))) + H x * Ls (sg (sg (sg (sg (sg (sg (x))))))) + H x * Ls (sg (sg (sg (sg (sg (sg (sg (x)))))))) + H x * Ls (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))) := fun x => by
    by_cases hx : sg (sg (sg (x))) = x
    · rw [hfix x hx]; lib_int
    · have ne : ∀ u v : G, u ≠ v → code u ≠ code v := fun u v h e => h (hinj u v e)
      have shift : ∀ y z : G, y ≠ z → sg y ≠ sg z := fun y z h e => h (by
        calc y = sg (sg (sg (sg (sg (sg (sg (sg (sg (y))))))))) := (hN y).symm
          _ = sg (sg (sg (sg (sg (sg (sg (sg ((sg z))))))))) := by rw [e]
          _ = z := hN z)
      have d0_1 : sg (x) ≠ x := fun e => hx (by
        have e2 : sg (sg (sg (x))) = x := by rw [e, e]; exact e
        calc sg (sg (sg (x))) = sg (sg (sg (x))) := rfl
          _ = x := e2)
      have d0_2 : sg (sg (x)) ≠ x := fun e => hx (by
        have e2 : sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))) = x := by rw [e, e, e, e, e]; exact e
        calc sg (sg (sg (x))) = sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))) := by rw [hN]
          _ = x := e2)
      have d0_3 : sg (sg (sg (x))) ≠ x := hx
      have d0_4 : sg (sg (sg (sg (x)))) ≠ x := fun e => hx (by
        have e2 : sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))) = x := by rw [e, e]; exact e
        calc sg (sg (sg (x))) = sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))) := by rw [hN]
          _ = x := e2)
      have d0_5 : sg (sg (sg (sg (sg (x))))) ≠ x := fun e => hx (by
        have e2 : sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))))))))))))))))))))) = x := by rw [e, e, e, e, e]; exact e
        calc sg (sg (sg (x))) = sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))))))))))))))))))))) := by rw [hN, hN, hN]
          _ = x := e2)
      have d0_6 : sg (sg (sg (sg (sg (sg (x)))))) ≠ x := fun e => hx (by
        have e2 : sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))) = x := by rw [e]; exact e
        calc sg (sg (sg (x))) = sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))) := by rw [hN]
          _ = x := e2)
      have d0_7 : sg (sg (sg (sg (sg (sg (sg (x))))))) ≠ x := fun e => hx (by
        have e2 : sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))))))))))))) = x := by rw [e, e]; exact e
        calc sg (sg (sg (x))) = sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))))))))))))) := by rw [hN, hN]
          _ = x := e2)
      have d0_8 : sg (sg (sg (sg (sg (sg (sg (sg (x)))))))) ≠ x := fun e => hx (by
        have e2 : sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))))))))))))))))))))))))))))))))))))))) = x := by rw [e, e, e, e, e]; exact e
        calc sg (sg (sg (x))) = sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))))))))))))))))))))))))))))))))))))))) := by rw [hN, hN, hN, hN, hN]
          _ = x := e2)
      have d1_2 := shift _ _ d0_1
      have d1_3 := shift _ _ d0_2
      have d1_4 := shift _ _ d0_3
      have d1_5 := shift _ _ d0_4
      have d1_6 := shift _ _ d0_5
      have d1_7 := shift _ _ d0_6
      have d1_8 := shift _ _ d0_7
      have d2_3 := shift _ _ d1_2
      have d2_4 := shift _ _ d1_3
      have d2_5 := shift _ _ d1_4
      have d2_6 := shift _ _ d1_5
      have d2_7 := shift _ _ d1_6
      have d2_8 := shift _ _ d1_7
      have d3_4 := shift _ _ d2_3
      have d3_5 := shift _ _ d2_4
      have d3_6 := shift _ _ d2_5
      have d3_7 := shift _ _ d2_6
      have d3_8 := shift _ _ d2_7
      have d4_5 := shift _ _ d3_4
      have d4_6 := shift _ _ d3_5
      have d4_7 := shift _ _ d3_6
      have d4_8 := shift _ _ d3_7
      have d5_6 := shift _ _ d4_5
      have d5_7 := shift _ _ d4_6
      have d5_8 := shift _ _ d4_7
      have d6_7 := shift _ _ d5_6
      have d6_8 := shift _ _ d5_7
      have d7_8 := shift _ _ d6_7
      have hm := one_min9 (code (x)) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))
        (ne _ _ (fun e => d0_1 e.symm)) (ne _ _ (fun e => d0_2 e.symm)) (ne _ _ (fun e => d0_3 e.symm)) (ne _ _ (fun e => d0_4 e.symm)) (ne _ _ (fun e => d0_5 e.symm)) (ne _ _ (fun e => d0_6 e.symm)) (ne _ _ (fun e => d0_7 e.symm)) (ne _ _ (fun e => d0_8 e.symm)) (ne _ _ (fun e => d1_2 e.symm)) (ne _ _ (fun e => d1_3 e.symm)) (ne _ _ (fun e => d1_4 e.symm)) (ne _ _ (fun e => d1_5 e.symm)) (ne _ _   -- (+759 chars; full line in the Zenodo source)
      have r1 : Ls (sg (x)) = eqI (code (sg (x))) (m9 (code (x)) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))) := by
        show eqI _ (m9 _ _ _ _ _ _ _ _ (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))) = _
        rw [hN (x), m9_rot]
      have r2 : Ls (sg (sg (x))) = eqI (code (sg (sg (x)))) (m9 (code (x)) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))) := by
        show eqI _ (m9 _ _ _ _ _ _ _ (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))))) = _
        rw [hN (x), m9_rot, m9_rot]
      have r3 : Ls (sg (sg (sg (x)))) = eqI (code (sg (sg (sg (x))))) (m9 (code (x)) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))) := by
        show eqI _ (m9 _ _ _ _ _ _ (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))))) = _
        rw [hN (x), m9_rot, m9_rot, m9_rot]
      have r4 : Ls (sg (sg (sg (sg (x))))) = eqI (code (sg (sg (sg (sg (x)))))) (m9 (code (x)) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))) := by
        show eqI _ (m9 _ _ _ _ _ (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))))))) = _
        rw [hN (x), m9_rot, m9_rot, m9_rot, m9_rot]
      have r5 : Ls (sg (sg (sg (sg (sg (x)))))) = eqI (code (sg (sg (sg (sg (sg (x))))))) (m9 (code (x)) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))) := by
        show eqI _ (m9 _ _ _ _ (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))))))) = _
        rw [hN (x), m9_rot, m9_rot, m9_rot, m9_rot, m9_rot]
      have r6 : Ls (sg (sg (sg (sg (sg (sg (x))))))) = eqI (code (sg (sg (sg (sg (sg (sg (x)))))))) (m9 (code (x)) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))) := by
        show eqI _ (m9 _ _ _ (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg  -- (+44 chars; full line in the Zenodo source)
        rw [hN (x), m9_rot, m9_rot, m9_rot, m9_rot, m9_rot, m9_rot]
      have r7 : Ls (sg (sg (sg (sg (sg (sg (sg (x)))))))) = eqI (code (sg (sg (sg (sg (sg (sg (sg (x))))))))) (m9 (code (x)) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))) := by
        show eqI _ (m9 _ _ (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (  -- (+128 chars; full line in the Zenodo source)
        rw [hN (x), m9_rot, m9_rot, m9_rot, m9_rot, m9_rot, m9_rot, m9_rot]
      have r8 : Ls (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))) = eqI (code (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))) (m9 (code (x)) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))  -- (+9 chars; full line in the Zenodo source)
        show eqI _ (m9 _ (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg  -- (+217 chars; full line in the Zenodo source)
        rw [hN (x), m9_rot, m9_rot, m9_rot, m9_rot, m9_rot, m9_rot, m9_rot, m9_rot]
      rw [r1, r2, r3, r4, r5, r6, r7, r8]
      show H x = H x * eqI (code x) _ + _ + _ + _ + _ + _ + _ + _ + _
      generalize m9 (code (x)) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))) = μ at hm ⊢
      generalize eqI (code (x)) μ = u0 at hm ⊢
      generalize eqI (code (sg (x))) μ = u1 at hm ⊢
      generalize eqI (code (sg (sg (x)))) μ = u2 at hm ⊢
      generalize eqI (code (sg (sg (sg (x))))) μ = u3 at hm ⊢
      generalize eqI (code (sg (sg (sg (sg (x)))))) μ = u4 at hm ⊢
      generalize eqI (code (sg (sg (sg (sg (sg (x))))))) μ = u5 at hm ⊢
      generalize eqI (code (sg (sg (sg (sg (sg (sg (x)))))))) μ = u6 at hm ⊢
      generalize eqI (code (sg (sg (sg (sg (sg (sg (sg (x))))))))) μ = u7 at hm ⊢
      generalize eqI (code (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))) μ = u8 at hm ⊢
      calc H x = H x * (u0 + u1 + u2 + u3 + u4 + u5 + u6 + u7 + u8) := by rw [hm]; lib_int
        _ = _ := by lib_int
  have q1 : sumK LibInt.intR (fun x => H x * Ls (sg (x))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (x)) (fun x => sg (sg (sg (sg (sg (sg (sg (sg (x))))))))) (fun x => hN x) (fun y => hN y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (x)) = H (sg (x)) * Ls (sg (x)); rw [hH]) _
  have q2 : sumK LibInt.intR (fun x => H x * Ls (sg (sg (x)))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (sg (x))) (fun x => sg (sg (sg (sg (sg (sg (sg (x)))))))) (fun x => hN x) (fun y => hN y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (sg (x))) = H (sg (sg (x))) * Ls (sg (sg (x))); rw [hH, hH]) _
  have q3 : sumK LibInt.intR (fun x => H x * Ls (sg (sg (sg (x))))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (sg (sg (x)))) (fun x => sg (sg (sg (sg (sg (sg (x))))))) (fun x => hN x) (fun y => hN y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (sg (sg (x)))) = H (sg (sg (sg (x)))) * Ls (sg (sg (sg (x)))); rw [hH, hH, hH]) _
  have q4 : sumK LibInt.intR (fun x => H x * Ls (sg (sg (sg (sg (x)))))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (sg (sg (sg (x))))) (fun x => sg (sg (sg (sg (sg (x)))))) (fun x => hN x) (fun y => hN y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (sg (sg (sg (x))))) = H (sg (sg (sg (sg (x))))) * Ls (sg (sg (sg (sg (x))))); rw [hH, hH, hH, hH]) _
  have q5 : sumK LibInt.intR (fun x => H x * Ls (sg (sg (sg (sg (sg (x))))))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (sg (sg (sg (sg (x)))))) (fun x => sg (sg (sg (sg (x))))) (fun x => hN x) (fun y => hN y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (sg (sg (sg (sg (x)))))) = H (sg (sg (sg (sg (sg (x)))))) * Ls (sg (sg (sg (sg (sg (x)))))); rw [hH, hH, hH, hH, hH]) _
  have q6 : sumK LibInt.intR (fun x => H x * Ls (sg (sg (sg (sg (sg (sg (x)))))))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (sg (sg (sg (sg (sg (x))))))) (fun x => sg (sg (sg (x)))) (fun x => hN x) (fun y => hN y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (sg (sg (sg (sg (sg (x))))))) = H (sg (sg (sg (sg (sg (sg (x))))))) * Ls (sg (sg (sg (sg (sg (sg (x))))))); rw [hH, hH, hH, hH, hH, hH]) _
  have q7 : sumK LibInt.intR (fun x => H x * Ls (sg (sg (sg (sg (sg (sg (sg (x))))))))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (sg (sg (sg (sg (sg (sg (x)))))))) (fun x => sg (sg (x))) (fun x => hN x) (fun y => hN y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (sg (sg (sg (sg (sg (sg (x)))))))) = H (sg (sg (sg (sg (sg (sg (sg (x)))))))) * Ls (sg (sg (sg (sg (sg (sg (sg (x)))))))); rw [hH, hH, hH, hH, hH, hH, hH]) _
  have q8 : sumK LibInt.intR (fun x => H x * Ls (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (sg (sg (sg (sg (sg (sg (sg (x))))))))) (fun x => sg (x)) (fun x => hN x) (fun y => hN y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))) = H (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))) * Ls (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))); rw [hH, hH, hH, hH, hH, hH, hH, hH]) _
  rw [sumK_ext LibInt.intR _ _ hpt, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, q1, q2, q3, q4, q5, q6, q7, q8]
  generalize sumK LibInt.intR (fun x => H x * Ls x) els = T
  lib_int

end General

end Orbit9

#print axioms Orbit9.m9_mem
#print axioms Orbit9.m9_le
#print axioms Orbit9.m9_rot
#print axioms Orbit9.one_min9
#print axioms Orbit9.orbit9

lang/OrbitV4.lean

lean_all/lang/OrbitV4.lean

/-
  lang/OrbitV4 — T43, cycle 393: orbits of a Klein four group counted by sums (tool for 4 | #E with full 2-torsion).

    orbitV4   σ² = τ² = id, στ = τσ, H invariant under σ and τ, H = 0 wherever σx = x, τx = x or στx = x
              ⟹ Σ H = 4·Σ H·[x least in its orbit {x, σx, τx, στx}]
  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.Orbit4

namespace OrbitV4

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum Orbit5 Orbit4
open FiniteRecChain (ite_pos' ite_neg')

section General
variable {G : Type} [DecidableEq G]

theorem orbitV4 (els : List G) (hc : Complete LibInt.intR els) (code : G → Nat) (hinj : ∀ u v, code u = code v → u = v)
    (s t : G → G) (hs : ∀ x, s (s x) = x) (ht : ∀ x, t (t x) = x) (hst : ∀ x, s (t x) = t (s x))
    (H : G → Int) (hHs : ∀ x, H (s x) = H x) (hHt : ∀ x, H (t x) = H x)
    (hfix : ∀ x, (s x = x ∨ t x = x ∨ s (t x) = x) → H x = 0) :
    sumK LibInt.intR H els = 4 * sumK LibInt.intR (fun x => H x * eqI (code x) (m4 (code x) (code (s x)) (code (t x)) (code (s (t x))))) els := by
  let Ls : G → Int := fun x => eqI (code x) (m4 (code x) (code (s x)) (code (t x)) (code (s (t x))))
  -- the minimum over the orbit is the same from every point of the orbit
  have m4_perm1 : ∀ a b c d : Nat, m4 b a d c = m4 a b c d := fun a b c d => by
    have l1 := m4_le a b c d; have l2 := m4_le b a d c
    apply Nat.le_antisymm
    · rcases m4_mem a b c d with h | h | h | h <;> rw [h]
      · exact l2.2.1
      · exact l2.1
      · exact l2.2.2.2
      · exact l2.2.2.1
    · rcases m4_mem b a d c with h | h | h | h <;> rw [h]
      · exact l1.2.1
      · exact l1.1
      · exact l1.2.2.2
      · exact l1.2.2.1
  have m4_perm2 : ∀ a b c d : Nat, m4 c d a b = m4 a b c d := fun a b c d => by
    have l1 := m4_le a b c d; have l2 := m4_le c d a b
    apply Nat.le_antisymm
    · rcases m4_mem a b c d with h | h | h | h <;> rw [h]
      · exact l2.2.2.1
      · exact l2.2.2.2
      · exact l2.1
      · exact l2.2.1
    · rcases m4_mem c d a b with h | h | h | h <;> rw [h]
      · exact l1.2.2.1
      · exact l1.2.2.2
      · exact l1.1
      · exact l1.2.1
  have hpt : ∀ x, H x = H x * Ls x + H x * Ls (s x) + H x * Ls (t x) + H x * Ls (s (t x)) := fun x => by
    by_cases hx : (s x = x ∨ t x = x ∨ s (t x) = x)
    · rw [hfix x hx]; lib_int
    · have ne : ∀ u v : G, u ≠ v → code u ≠ code v := fun u v h e => h (hinj u v e)
      have n1 : s x ≠ x := fun e => hx (Or.inl e)
      have n2 : t x ≠ x := fun e => hx (Or.inr (Or.inl e))
      have n3 : s (t x) ≠ x := fun e => hx (Or.inr (Or.inr e))
      -- s x ≠ t x : else s (t x) = s (s x) = x
      have n12 : s x ≠ t x := fun e => n3 (by rw [← e, hs])
      -- s x ≠ s (t x): else x = t x
      have n13 : s x ≠ s (t x) := fun e => n2 (by
        calc t x = s (s (t x)) := (hs _).symm
          _ = s (s x) := by rw [← e]
          _ = x := hs x)
      -- t x ≠ s (t x): else s (t x) = t x ⇒ s x' = x' for x' = t x, then x = t (t x) ... gives s x = x
      have n23 : t x ≠ s (t x) := fun e => n1 (by
        calc s x = s (t (t x)) := by rw [ht]
          _ = t (s (t x)) := hst _
          _ = t (t x) := by rw [← e]
          _ = x := ht x)
      have hm := one_min4 (code x) (code (s x)) (code (t x)) (code (s (t x)))
        (ne _ _ (fun e => n1 e.symm)) (ne _ _ (fun e => n2 e.symm)) (ne _ _ (fun e => n3 e.symm))
        (ne _ _ n12) (ne _ _ n13) (ne _ _ n23)
      have r1 : Ls (s x) = eqI (code (s x)) (m4 (code x) (code (s x)) (code (t x)) (code (s (t x)))) := by
        show eqI _ (m4 (code (s x)) (code (s (s x))) (code (t (s x))) (code (s (t (s x))))) = _
        rw [hs, ← hst, hs]; exact congrArg _ (m4_perm1 _ _ _ _)
      have r2 : Ls (t x) = eqI (code (t x)) (m4 (code x) (code (s x)) (code (t x)) (code (s (t x)))) := by
        show eqI _ (m4 (code (t x)) (code (s (t x))) (code (t (t x))) (code (s (t (t x))))) = _
        rw [ht]; exact congrArg _ (m4_perm2 _ _ _ _)
      have r3 : Ls (s (t x)) = eqI (code (s (t x))) (m4 (code x) (code (s x)) (code (t x)) (code (s (t x)))) := by
        show eqI _ (m4 (code (s (t x))) (code (s (s (t x)))) (code (t (s (t x)))) (code (s (t (s (t x)))))) = _
        rw [hs, ← hst, hs, ht]
        exact congrArg _ ((m4_perm1 _ _ _ _).trans (m4_perm2 _ _ _ _))
      rw [r1, r2, r3]
      show H x = H x * eqI (code x) _ + _ + _ + _
      generalize m4 (code x) (code (s x)) (code (t x)) (code (s (t x))) = μ at hm ⊢
      generalize eqI (code x) μ = u0 at hm ⊢; generalize eqI (code (s x)) μ = u1 at hm ⊢
      generalize eqI (code (t x)) μ = u2 at hm ⊢; generalize eqI (code (s (t x))) μ = u3 at hm ⊢
      calc H x = H x * (u0 + u1 + u2 + u3) := by rw [hm]; lib_int
        _ = _ := by lib_int
  have q1 : sumK LibInt.intR (fun x => H x * Ls (s x)) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc s s (fun x => hs x) (fun y => hs y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (s x) = H (s x) * Ls (s x); rw [hHs]) _
  have q2 : sumK LibInt.intR (fun x => H x * Ls (t x)) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc t t (fun x => ht x) (fun y => ht y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (t x) = H (t x) * Ls (t x); rw [hHt]) _
  have q3 : sumK LibInt.intR (fun x => H x * Ls (s (t x))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => s (t x)) (fun x => s (t x))
      (fun x => by show s (t (s (t x))) = x; rw [hst x, ht, hs]) (fun y => by show s (t (s (t y))) = y; rw [hst y, ht, hs]) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (s (t x)) = H (s (t x)) * Ls (s (t x)); rw [hHs, hHt]) _
  rw [sumK_ext LibInt.intR _ _ hpt, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, q1, q2, q3]
  generalize sumK LibInt.intR (fun x => H x * Ls x) els = T
  lib_int

end General

end OrbitV4

#print axioms OrbitV4.orbitV4

lang/OrdMul.lean

lean_all/lang/OrdMul.lean

/-
  lang/OrdMul — T43, cycle 32: orders in a commutative ring; coprime orders multiply; exact order ℓ^e in 𝔽_p.

    Ord x m           x^m = 1 and every r with x^r = 1 is a multiple of m (core ∣)
    ord_of_min        the least positive exponent (Order.ord_exists) is the order
    ord_mul_coprime   Ord a m, Ord b n, m n coprime ⇒ Ord (a b) (m n)      (LibGcd.gauss, coprime_mul_dvd)
    ord_prime_pow     ℓ^e | p − 1 ⇒ some y ∈ 𝔽_p has Ord y ℓ^e            (CycloPow.split_pow + the order argument)
  Next: splitting p − 1 into prime powers gives an element of order p − 1 (𝔽_p^× cyclic) and Artin reciprocity for
  ℚ(ζ_N), N arbitrary.

  Lean 4 core only, proofs by hand (lib/Gcd from lib_shared), no axioms.
-/
import lang.CycloPow
import lib.Gcd

namespace OrdMul

open FTAChain ZMod TwoPower Order PrimeOneModQ PrimePowerOne

variable {R : Type} (K : DetChain.CRing R)

def Ord (x : R) (m : Nat) : Prop := Frobenius.pw K x m = K.one ∧ ∀ r, Frobenius.pw K x r = K.one → m ∣ r

theorem dv_to_dvd {d f : Nat} (h : Dv d f) : d ∣ f :=
  match h with
  | ⟨q, e⟩ => ⟨q, by rw [e, nmul_comm]⟩

theorem ord_of_min (x : R) (d : Nat) (hd : Frobenius.pw K x (d + 1) = K.one)
    (hmin : ∀ r, 0 < r → r < d + 1 → Frobenius.pw K x r ≠ K.one) : Ord K x (d + 1) :=
  ⟨hd, fun r hr => dv_to_dvd (ord_dvd K x d hd hmin r hr)⟩

theorem pw_one_of_dvd (x : R) (m r : Nat) (hm : Frobenius.pw K x m = K.one) (h : m ∣ r) :
    Frobenius.pw K x r = K.one := by
  match h with
  | ⟨c, e⟩ => rw [e, ← pwpow, hm, pwone]

/-- **Coprime orders multiply.** -/
theorem ord_mul_coprime (a b : R) (m n : Nat) (ha : Ord K a m) (hb : Ord K b n)
    (hc : ∀ d, d ∣ m → d ∣ n → d = 1) : Ord K (K.mul a b) (m * n) := by
  refine ⟨?_, fun r hr => ?_⟩
  · rw [Frobenius.pw_mul, pw_one_of_dvd K a m (m * n) ha.1 ⟨n, rfl⟩,
      pw_one_of_dvd K b n (m * n) hb.1 ⟨m, nmul_comm m n⟩, K.one_mul]
  · -- (ab)^(r n) = 1 ⇒ a^(r n) = 1 ⇒ m | r n ⇒ m | r
    have hrn : Frobenius.pw K (K.mul a b) (r * n) = K.one := by
      rw [← pwpow, hr, pwone]
    rw [Frobenius.pw_mul, pw_one_of_dvd K b n (r * n) hb.1 ⟨r, nmul_comm r n⟩, K.mul_comm, K.one_mul] at hrn
    have hm : m ∣ r := LibGcd.gauss hc (ha.2 _ hrn)
    have hrm : Frobenius.pw K (K.mul a b) (r * m) = K.one := by
      rw [← pwpow, hr, pwone]
    rw [Frobenius.pw_mul, pw_one_of_dvd K a m (r * m) ha.1 ⟨r, nmul_comm r m⟩, K.one_mul] at hrm
    have hn : n ∣ r := LibGcd.gauss (fun d d1 d2 => hc d d2 d1) (hb.2 _ hrm)
    exact LibGcd.coprime_mul_dvd hc hm hn

variable (J : Nat)

/-- **An element of exact order ℓ^(s+1) in 𝔽_p** when ℓ^(s+1) | p − 1. -/
theorem ord_prime_pow (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (t s : Nat)
    (hq : ∀ d, Dv d (t + 1 + 1) → d = 1 ∨ d = t + 1 + 1) (hd : Dv ((t + 1 + 1) ^ (s + 1)) (J + 1)) :
    ∃ y : Zp (J + 1), Ord (ring (J + 1)) y ((t + 1 + 1) ^ (s + 1)) := by
  match CycloPow.split_pow J hpr t s hd with
  | ⟨x, hxfull, hX1⟩ =>
    obtain ⟨n, hn⟩ := pow_succ_form (t + 1) (s + 1)
    rw [hn] at hxfull
    obtain ⟨d, hd1, hdmin⟩ := ord_exists (ring (J + 1)) x n hxfull
    obtain ⟨c, hc⟩ := ord_dvd (ring (J + 1)) x d hd1 hdmin (n + 1) hxfull
    have hdq : d + 1 = (t + 1 + 1) ^ (s + 1) := by
      cases c with
      | zero => rw [nzero_mul] at hc; exact absurd hc (Nat.noConfusion)
      | succ c =>
        cases c with
        | zero => rw [hn, hc, nmul_comm, nmul_one]
        | succ c =>
          exfalso
          apply hX1
          have hpd := proper_div t s (d + 1) c hq (by rw [hn, hc, nmul_comm])
          obtain ⟨m', hm'⟩ := hpd
          rw [hm']
          exact pw_qe (ring (J + 1)) x (d + 1) 0 hd1 m'
    exact ⟨x, hdq ▸ ord_of_min (ring (J + 1)) x d hd1 hdmin⟩

end OrdMul

#print axioms OrdMul.dv_to_dvd
#print axioms OrdMul.ord_of_min
#print axioms OrdMul.pw_one_of_dvd
#print axioms OrdMul.ord_mul_coprime
#print axioms OrdMul.ord_prime_pow

lang/OrdQ.lean

lean_all/lang/OrdQ.lean

/-
  lang/OrdQ — T43, cycle 482: orders under x ↦ x^q (step O of the general-modulus Dirichlet plan).

  IsOrd x n  :⇔  x^n = 1 and x^f = 1 ⇒ n | f   (FTAChain divisibility).
    pw_pw        (x^a)^b = x^(a·b)
    coprime_dv   n ≥ 1, q prime, q ∤ n, n | q·f ⇒ n | f        (Val.dv_of_local + Val.ppow_cancel)
    ord_pow      IsOrd x n, q prime, q ∤ n ⇒ IsOrd (x^q) n
    ord_of_pow   IsOrd (x^q) n, n ≥ 1, q prime, finite field ⇒ IsOrd x n or IsOrd x (n·q)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.Val
import lang.Order

namespace OrdQ

open FTAChain

variable {R : Type} (K : DetChain.CRing R)

def IsOrd (x : R) (n : Nat) : Prop := Frobenius.pw K x n = K.one ∧ ∀ f, Frobenius.pw K x f = K.one → Dv n f

theorem pw_pw (x : R) (a : Nat) : ∀ b, Frobenius.pw K (Frobenius.pw K x a) b = Frobenius.pw K x (a * b)
  | 0 => rfl
  | b + 1 => by
    show K.mul (Frobenius.pw K (Frobenius.pw K x a) b) (Frobenius.pw K x a) = Frobenius.pw K x (a * b + a)
    rw [pw_pw x a b]
    exact (LinkIntegralZN.pw_add K x (a * b) a).symm

theorem pw_one1 : ∀ k, Frobenius.pw K K.one k = K.one
  | 0 => rfl
  | k + 1 => by show K.mul (Frobenius.pw K K.one k) K.one = K.one; rw [pw_one1 k]; exact K.one_mul K.one

theorem coprime_dv (n j f : Nat) (hn : 1 ≤ n) (hq : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1)
    (hqn : Dv (j + 1 + 1) n → False) (h : Dv n ((j + 1 + 1) * f)) : Dv n f := by
  refine Val.dv_of_local n n f (Nat.le_refl n) hn (fun r k hr hk => ?_)
  match k, hk with
  | 0, _ => exact ⟨f, by show f = f * 1; exact (Nat.mul_one f).symm⟩
  | k + 1, hk =>
    have hrq : Dv (r + 1 + 1) (j + 1 + 1) → False := fun hd => by
      rcases hq _ hd with e | e
      · exact Nat.noConfusion (Nat.succ.inj e)
      · apply hqn
        rw [← e]
        exact dv_trans _ _ _ ⟨(r + 1 + 1) ^ k, rfl⟩ hk
    exact Val.ppow_cancel r (j + 1 + 1) hr hrq (k + 1) f (dv_trans _ _ _ hk h)

theorem ord_pow (x : R) (n j : Nat) (hx : IsOrd K x n) (hn : 1 ≤ n)
    (hq : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1) (hqn : Dv (j + 1 + 1) n → False) :
    IsOrd K (Frobenius.pw K x (j + 1 + 1)) n := by
  refine ⟨?_, fun f hf => ?_⟩
  · rw [pw_pw, Nat.mul_comm, ← pw_pw, hx.1, pw_one1]
  · rw [pw_pw] at hf
    exact coprime_dv n j f hn hq hqn (hx.2 _ hf)

theorem ord_of_pow [DecidableEq R] (x : R) (n j : Nat) (hx : IsOrd K (Frobenius.pw K x (j + 1 + 1)) n) (hn : 1 ≤ n)
    (hq : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1) :
    IsOrd K x n ∨ IsOrd K x (n * (j + 1 + 1)) := by
  obtain ⟨n', hn'⟩ := Cheb.le_dest2 1 n hn
  have hN : Frobenius.pw K x (n' * (j + 1 + 1) + (j + 1) + 1) = K.one := by
    rw [show n' * (j + 1 + 1) + (j + 1) + 1 = (j + 1 + 1) * n by rw [← hn']; lib_nat, ← pw_pw]; exact hx.1
  obtain ⟨d, hd, hmin⟩ := Order.ord_exists K x _ hN
  have hO : IsOrd K x (d + 1) := ⟨hd, fun f hf => Order.ord_dvd K x d hd hmin f hf⟩
  -- n | d + 1
  have h1 : Dv n (d + 1) := hx.2 _ (by rw [pw_pw, Nat.mul_comm, ← pw_pw, hd, pw_one1])
  -- d + 1 | n q
  have h2 : Dv (d + 1) (n * (j + 1 + 1)) := hO.2 _ (by rw [Nat.mul_comm, ← pw_pw, hx.1])
  obtain ⟨t, ht⟩ := h1
  obtain ⟨u, hu⟩ := h2
  have htq : Dv t (j + 1 + 1) := ⟨u, by
    apply mul_cancel n'
    rw [hn']
    calc n * (j + 1 + 1) = u * (d + 1) := hu
      _ = n * (u * t) := by rw [ht]; generalize n = N; lib_nat⟩
  rcases hq t htq with e | e
  · left; rw [ht, e, Nat.one_mul] at hO; exact hO
  · right; rw [ht, e, Nat.mul_comm] at hO; exact hO

end OrdQ

#print axioms OrdQ.pw_pw
#print axioms OrdQ.pw_one1
#print axioms OrdQ.coprime_dv
#print axioms OrdQ.ord_pow
#print axioms OrdQ.ord_of_pow

lang/Order.lean

lean_all/lang/Order.lean

/-
  lang/Order — T43, cycle 14: orders of units, in any commutative ring with decidable equality.

  For x with x^n = 1 (n ≥ 1):
    exp_sub       x^a = 1 and x^(a + b) = 1  ⇒  x^b = 1
    pw_qe         x^e = 1  ⇒  x^(q e + r) = x^r;   divmod: f = q (d+1) + r, r ≤ d (no %)
    ord_exists    there is a least d ≥ 1 with x^d = 1 (bounded search up to n)
    ord_dvd       x^f = 1  ⇒  d | f
  Next cycle: a prime p ≠ q dividing 1 + a + … + a^(q−1) has ord(a) = q, so q | p − 1.

  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.TwoPower

namespace Order

open FTAChain TwoPower

variable {R : Type} (K : DetChain.CRing R)

theorem exp_sub (x : R) (a b : Nat)
    (ha : Frobenius.pw K x a = K.one) (hab : Frobenius.pw K x (a + b) = K.one) : Frobenius.pw K x b = K.one := by
  have hadd : Frobenius.pw K x (a + b) = K.mul (Frobenius.pw K x a) (Frobenius.pw K x b) :=
    LinkIntegralZN.pw_add K x a b
  rw [hadd, ha, K.one_mul] at hab
  exact hab

/-- If `x^e = 1` then `x^(q e + r) = x^r`. -/
theorem pw_qe (x : R) (e r : Nat) (he : Frobenius.pw K x e = K.one) :
    ∀ q, Frobenius.pw K x (q * e + r) = Frobenius.pw K x r
  | 0 => by rw [nzero_mul, Nat.zero_add]
  | q + 1 => by
    rw [nsucc_mul, nadd_assoc, Nat.add_comm e r, ← nadd_assoc]
    show LinkIntegralZN.pw K x (q * e + r + e) = _
    rw [LinkIntegralZN.pw_add K x (q * e + r) e]
    show K.mul (Frobenius.pw K x (q * e + r)) (Frobenius.pw K x e) = _
    rw [he, pw_qe x e r he q, K.mul_comm, K.one_mul]

/-- Division with remainder by `d + 1`, without `%`: `f = q (d+1) + r`, `r ≤ d`. -/
theorem divmod (d : Nat) : ∀ f, ∃ q r, f = q * (d + 1) + r ∧ r < d + 1
  | 0 => ⟨0, 0, by rw [nzero_mul], Nat.zero_lt_succ d⟩
  | f + 1 => by
    obtain ⟨q, r, hf, hr⟩ := divmod d f
    cases Nat.lt_or_ge (r + 1) (d + 1) with
    | inl h => exact ⟨q, r + 1, by rw [hf]; rfl, h⟩
    | inr h =>
      -- r + 1 = d + 1
      have hr' : r = d := Nat.le_antisymm (Nat.le_of_lt_succ hr) (Nat.le_of_succ_le_succ h)
      refine ⟨q + 1, 0, ?_, Nat.zero_lt_succ d⟩
      rw [hf, hr', nsucc_mul]
      rfl

/-- **Orders divide exponents.** If `d + 1` is the least positive exponent with `x^(d+1) = 1`, every `f`
    with `x^f = 1` is a multiple of `d + 1`. -/
theorem ord_dvd (x : R) (d : Nat) (hd : Frobenius.pw K x (d + 1) = K.one)
    (hmin : ∀ r, 0 < r → r < d + 1 → Frobenius.pw K x r ≠ K.one) (f : Nat) (hf : Frobenius.pw K x f = K.one) :
    Dv (d + 1) f := by
  obtain ⟨q, r, hqr, hr⟩ := divmod d f
  have hxr : Frobenius.pw K x r = K.one := by
    rw [← pw_qe K x (d + 1) r hd q, ← hqr]; exact hf
  cases r with
  | zero => exact ⟨q, by rw [hqr]; rfl⟩
  | succ r => exact absurd hxr (hmin (r + 1) (Nat.zero_lt_succ r) hr)

/-- **The order exists**: a least positive exponent below any given `n + 1` with `x^(n+1) = 1`. -/
theorem ord_exists [DecidableEq R] (x : R) (n : Nat) (hn : Frobenius.pw K x (n + 1) = K.one) :
    ∃ d, Frobenius.pw K x (d + 1) = K.one ∧ ∀ r, 0 < r → r < d + 1 → Frobenius.pw K x r ≠ K.one := by
  -- search the least d ≤ n
  have search : ∀ m, (∃ d, d < m ∧ Frobenius.pw K x (d + 1) = K.one ∧ ∀ r, 0 < r → r < d + 1 → Frobenius.pw K x r ≠ K.one) ∨
      (∀ r, 0 < r → r < m + 1 → Frobenius.pw K x r ≠ K.one) := by
    intro m
    induction m with
    | zero => exact Or.inr (fun r h0 h1 => absurd h0 (by rw [Nat.le_zero.mp (Nat.le_of_lt_succ h1)]; exact Nat.lt_irrefl 0))
    | succ m ih =>
      cases ih with
      | inl h => obtain ⟨d, hd, h2⟩ := h; exact Or.inl ⟨d, Nat.lt_succ_of_lt hd, h2⟩
      | inr h =>
        by_cases hm : Frobenius.pw K x (m + 1) = K.one
        · exact Or.inl ⟨m, Nat.lt_succ_self m, hm, h⟩
        · refine Or.inr (fun r h0 h1 => ?_)
          cases Nat.lt_or_ge r (m + 1) with
          | inl hlt => exact h r h0 hlt
          | inr hge =>
            have : r = m + 1 := Nat.le_antisymm (Nat.le_of_lt_succ h1) hge
            rw [this]; exact hm
  cases search (n + 1) with
  | inl h => obtain ⟨d, _, h2⟩ := h; exact ⟨d, h2⟩
  | inr h => exact absurd hn (h (n + 1) (Nat.zero_lt_succ n) (Nat.lt_succ_self (n + 1)))

end Order

#print axioms Order.exp_sub
#print axioms Order.pw_qe
#print axioms Order.divmod
#print axioms Order.ord_dvd
#print axioms Order.ord_exists

lang/PSer.lean

lean_all/lang/PSer.lean

/-
  lang/PSer — T47, cycle 541: formal power series over a commutative ring, pointwise (no funext):
  the Cauchy product GLnLocal.conv is commutative and associative.

    conv_one        conv c h 1 = c₀h₁ + c₁h₀
    conv_comm2, conv_comm    conv c h = conv h c          (two-step induction on the index, for all c, h)
    conv_sR_left    conv (sR c) h = sR (conv c h)
    conv_assoc      conv (conv a b) c = conv a (conv b c)  (a = a₀δ + sR a′)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.GLnLocal
import lib.RingTac

namespace PSer

open GLnLocal

variable {R : Type} (K : DetChain.CRing R)

theorem conv_one (c h : Nat → R) : conv K c h 1 = K.add (K.mul (c 0) (h 1)) (K.mul (c 1) (h 0)) := rfl

theorem conv_comm2 : ∀ k (c h : Nat → R), conv K c h k = conv K h c k ∧ conv K c h (k + 1) = conv K h c (k + 1)
  | 0 => fun c h => ⟨K.mul_comm _ _, by
      show K.add (K.mul (c 0) (h 1)) (K.mul (c 1) (h 0)) = K.add (K.mul (h 0) (c 1)) (K.mul (h 1) (c 0)); lib_ring⟩
  | k + 1 => fun c h => by
    refine ⟨(conv_comm2 k c h).2, ?_⟩
    -- conv c h (k+2) = c₀h_{k+2} + conv c' h (k+1) = c₀h_{k+2} + conv h c' (k+1)
    --               = c₀h_{k+2} + h₀c_{k+2} + conv h' c' k
    show K.add (K.mul (c 0) (h (k + 1 + 1))) (conv K (fun i => c (i + 1)) h (k + 1)) =
      K.add (K.mul (h 0) (c (k + 1 + 1))) (conv K (fun i => h (i + 1)) c (k + 1))
    rw [(conv_comm2 k (fun i => c (i + 1)) h).2, (conv_comm2 k (fun i => h (i + 1)) c).2]
    show K.add (K.mul (c 0) (h (k + 1 + 1))) (K.add (K.mul (h 0) (c (k + 1 + 1))) (conv K (fun i => h (i + 1)) (fun i => c (i + 1)) k)) =
      K.add (K.mul (h 0) (c (k + 1 + 1))) (K.add (K.mul (c 0) (h (k + 1 + 1))) (conv K (fun i => c (i + 1)) (fun i => h (i + 1)) k))
    rw [(conv_comm2 k (fun i => h (i + 1)) (fun i => c (i + 1))).1]
    generalize conv K (fun i => c (i + 1)) (fun i => h (i + 1)) k = X
    lib_ring

theorem conv_comm (c h : Nat → R) (k : Nat) : conv K c h k = conv K h c k := (conv_comm2 K k c h).1

theorem conv_sR_left (c h : Nat → R) (k : Nat) : conv K (sR K c) h k = sR K (conv K c h) k := by
  rw [conv_comm, conv_sR]
  cases k with
  | zero => rfl
  | succ k => exact conv_comm K h c k

/-- conv a X = a₀·X + sR (conv a′ X) pointwise. -/
theorem conv_split (a X : Nat → R) : ∀ k, conv K a X k = K.add (K.mul (a 0) (X k)) (sR K (conv K (fun i => a (i + 1)) X) k)
  | 0 => by show K.mul (a 0) (X 0) = K.add (K.mul (a 0) (X 0)) K.zero; lib_ring
  | k + 1 => rfl

theorem conv_assoc : ∀ k (a b c : Nat → R), conv K (conv K a b) c k = conv K a (conv K b c) k
  | 0 => fun a b c => by show K.mul (K.mul (a 0) (b 0)) (c 0) = K.mul (a 0) (K.mul (b 0) (c 0)); lib_ring
  | k + 1 => fun a b c => by
    -- (conv a b) = a₀·b + sR (conv a′ b)
    have e1 : conv K (conv K a b) c (k + 1) =
        conv K (fun i => K.add (K.mul (a 0) (b i)) (sR K (conv K (fun i => a (i + 1)) b) i)) c (k + 1) :=
      conv_ext_left K _ _ c (conv_split K a b) (k + 1)
    rw [e1, conv_add_left, conv_smul_left, conv_sR_left]
    show K.add (K.mul (a 0) (conv K b c (k + 1))) (conv K (conv K (fun i => a (i + 1)) b) c k) =
      K.add (K.mul (a 0) (conv K b c (k + 1))) (conv K (fun i => a (i + 1)) (conv K b c) k)
    rw [conv_assoc k (fun i => a (i + 1)) b c]

end PSer

#print axioms PSer.conv_one
#print axioms PSer.conv_comm2
#print axioms PSer.conv_comm
#print axioms PSer.conv_sR_left
#print axioms PSer.conv_split
#print axioms PSer.conv_assoc

lang/PairNorm.lean

lean_all/lang/PairNorm.lean

/-
  lang/PairNorm — T49 (O3') (ii'): **the product of the pairs σ_a α · σ_b α is the norm product** of NormEval.
  For a list of entries (σ_(a_i) α, σ_(b_i) α, ·, ·) and inverse data (c_i, a_i), (c'_i, b_i):
    flat     [(c_1, a_1), (c'_1, b_1), (c_2, a_2), …]
    pp_nrm   NpPow.Pp (entries) = NormEval.nrm α (flat)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.NpPow
import lang.NormEval

namespace PairNorm

open FTAChain ZMod YMChain FiniteRecChain DetChain GroupRing

variable (m : Nat) (α : V Int (m + 1))

/-- entries from (a, b, c, c', i, j): (σ_a α, σ_b α, i, j). -/
def ents : List ((Zp m × Zp m) × (Zp m × Zp m) × Nat × Nat) → List (V Int (m + 1) × V Int (m + 1) × Nat × Nat)
  | [] => []
  | q :: qs => (CycGal.gsig LibInt.intR m q.1.2 α, CycGal.gsig LibInt.intR m q.2.1.2 α, q.2.2.1, q.2.2.2) :: ents qs

def flat : List ((Zp m × Zp m) × (Zp m × Zp m) × Nat × Nat) → List (Zp m × Zp m)
  | [] => []
  | q :: qs => q.1 :: q.2.1 :: flat qs

theorem pp_nrm : ∀ qs, NpPow.Pp (gring LibInt.intR m) (ents m α qs) = NormEval.nrm m α (flat m qs)
  | [] => rfl
  | q :: qs => by
    show (gring LibInt.intR m).mul
        ((gring LibInt.intR m).mul (CycGal.gsig LibInt.intR m q.1.2 α) (CycGal.gsig LibInt.intR m q.2.1.2 α))
        (NpPow.Pp (gring LibInt.intR m) (ents m α qs))
      = (gring LibInt.intR m).mul (CycGal.gsig LibInt.intR m q.1.2 α)
        ((gring LibInt.intR m).mul (CycGal.gsig LibInt.intR m q.2.1.2 α) (NormEval.nrm m α (flat m qs)))
    rw [pp_nrm qs]
    generalize CycGal.gsig LibInt.intR m q.1.2 α = X
    generalize CycGal.gsig LibInt.intR m q.2.1.2 α = Y
    generalize NormEval.nrm m α (flat m qs) = Z
    lib_ring

end PairNorm

#print axioms PairNorm.pp_nrm

lang/Pairing.lean

lean_all/lang/Pairing.lean

/-
  lang/Pairing — T43, cycle 55: the involution pairing lemma on 𝔽_p.

  σ an involution of ℤ/p (σ σ x = x), F : ℤ/p → ℤ with F(σ x) = F(x). Order the residues by their values. Then
    lt_split     1 = [σx = x] + [x < σx] + [σx < x]
    pairing      Σ_x F(x) = Σ_x [σx = x] F(x) + 2 Σ_x [x < σx] F(x)
  So Σ F ≡ Σ over the fixed points (mod 2) — the tool for the parity / mod-8 counts of points on curves (orbits of the
  automorphisms x ↦ −x, x ↦ x⁻¹ of y² = x³ − x), toward the sign a_p = ψ(π) + ψ(π̄).

  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.ConicSum

namespace Pairing

open FTAChain ZMod YMChain FiniteRecChain

variable (J : Nat)

abbrev Z := LibInt.intR

def ltI (x y : Zp (J + 1)) : Int := if x.val < y.val then 1 else 0
def eqI (x y : Zp (J + 1)) : Int := if x = y then 1 else 0

theorem lt_split (x y : Zp (J + 1)) : eqI J y x + ltI J x y + ltI J y x = 1 := by
  unfold eqI ltI
  by_cases h : y = x
  · rw [ite_pos' _ _ h, h, ite_neg' _ _ (Nat.lt_irrefl _)]; rfl
  · rw [ite_neg' _ _ h]
    cases Nat.lt_or_ge x.val y.val with
    | inl hl => rw [ite_pos' _ _ hl, ite_neg' _ _ (Nat.not_lt_of_le (Nat.le_of_lt hl))]; rfl
    | inr hg =>
      have hl : y.val < x.val := Nat.lt_of_le_of_ne hg (fun e => h (Subtype.ext e))
      rw [ite_neg' _ _ (Nat.not_lt_of_le hg), ite_pos' _ _ hl]; rfl

/-- **Pairing.** -/
theorem pairing (σ : Zp (J + 1) → Zp (J + 1)) (hσ : ∀ x, σ (σ x) = x) (F : Zp (J + 1) → Int)
    (hF : ∀ x, F (σ x) = F x) (l : List (Zp (J + 1))) (hl : Complete Z l) :
    sumK Z F l = sumK Z (fun x => eqI J (σ x) x * F x) l + 2 * sumK Z (fun x => ltI J x (σ x) * F x) l := by
  have pt : ∀ x, F x = Z.add (Z.add (eqI J (σ x) x * F x) (ltI J x (σ x) * F x)) (ltI J (σ x) x * F x) := fun x => by
    have := lt_split J x (σ x)
    show F x = eqI J (σ x) x * F x + ltI J x (σ x) * F x + ltI J (σ x) x * F x
    calc F x = (eqI J (σ x) x + ltI J x (σ x) + ltI J (σ x) x) * F x := by rw [this]; lib_int
      _ = _ := by lib_int
  rw [sumK_ext Z _ _ pt, sumK_add, sumK_add]
  -- Σ [σx < x] F(x) = Σ [x < σx] F(x) by reindexing with σ
  have hr := reindex Z l hl σ σ hσ hσ (fun x => ltI J x (σ x) * F x)
  have e : sumK Z (fun x => ltI J (σ x) x * F x) l = sumK Z (fun x => ltI J x (σ x) * F x) l := by
    rw [← hr]
    exact sumK_ext Z _ _ (fun x => by show _ = ltI J (σ x) (σ (σ x)) * F (σ x); rw [hσ, hF]) _
  rw [e]
  show sumK Z _ l + sumK Z _ l + sumK Z _ l = _
  lib_int

end Pairing

#print axioms Pairing.lt_split
#print axioms Pairing.pairing

lang/Pairs.lean

lean_all/lang/Pairs.lean

/-
  lang/Pairs — T43, cycle 357: the finite set {O} ∪ 𝔽_p² as a completely listed type with an injective code (G4 tool, part 2).

    prodL, sum_prodL     the product list and Σ over it = double sum
    complete_prod        Complete for 𝔽_p × 𝔽_p
    ptsL, complete_pts   Complete for Option (𝔽_p × 𝔽_p) (none = the point at infinity)
    code_inj             code(none) = 0, code(some (x, y)) = 1 + x + N·y is injective (N = p)
  With Orbit5.orbit5 this lets one count orbits of maps on E(𝔽_p) ∪ {O} ⊂ {O} ∪ 𝔽_p².
  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.Orbit5
import lang.JacobiNorm

namespace Pairs

open FTAChain ZMod YMChain FiniteRecChain JacobMoment
open FiniteRecChain (ite_pos' ite_neg')

variable (m : Nat)

def prodL {A B : Type} : List A → List B → List (A × B)
  | [], _ => []
  | a :: as, bs => bs.map (fun b => (a, b)) ++ prodL as bs

theorem sum_map {A B : Type} (f : B → Int) (g : A → B) : ∀ l : List A, sumK LibInt.intR f (l.map g) = sumK LibInt.intR (fun a => f (g a)) l
  | [] => rfl
  | a :: as => by show LibInt.intR.add (f (g a)) (sumK LibInt.intR f (as.map g)) = LibInt.intR.add (f (g a)) _; rw [sum_map f g as]

theorem sum_prodL {A B : Type} (f : A × B → Int) (bs : List B) : ∀ as : List A,
    sumK LibInt.intR f (prodL as bs) = sumK LibInt.intR (fun a => sumK LibInt.intR (fun b => f (a, b)) bs) as
  | [] => rfl
  | a :: as => by
    show sumK LibInt.intR f (bs.map (fun b => (a, b)) ++ prodL as bs) = LibInt.intR.add _ _
    rw [sumK_append, sum_map, sum_prodL f bs as]

theorem dg_one {G : Type} [DecidableEq G] (a b : G) (h : a = b) : delta LibInt.intR a b = 1 := ite_pos' _ _ h
theorem dg_zero {G : Type} [DecidableEq G] (a b : G) (h : a ≠ b) : delta LibInt.intR a b = 0 := ite_neg' _ _ h

def pL : List (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) := prodL (CMInert.allZ (2 * m + 1 + 1) (2 * m + 1 + 1 + 1)) (CMInert.allZ (2 * m + 1 + 1) (2 * m + 1 + 1 + 1))

theorem delta_pair (u v : Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) :
    delta LibInt.intR u v = delta LibInt.intR u.1 v.1 * delta LibInt.intR u.2 v.2 := by
  by_cases h1 : u.1 = v.1
  · by_cases h2 : u.2 = v.2
    · rw [dg_one u.1 v.1 h1, dg_one u.2 v.2 h2, dg_one u v (Prod.ext h1 h2)]; rfl
    · rw [dg_zero u.2 v.2 h2, dg_zero u v (fun e => h2 (congrArg Prod.snd e))]; show (0 : Int) = _ * 0; generalize delta LibInt.intR u.1 v.1 = a; lib_int
  · rw [dg_zero u.1 v.1 h1, dg_zero u v (fun e => h1 (congrArg Prod.fst e))]; show (0 : Int) = 0 * _; generalize delta LibInt.intR u.2 v.2 = a; lib_int

theorem complete_prod : Complete LibInt.intR (pL m) := fun z => by
  unfold pL
  rw [sum_prodL]
  rw [sumK_ext LibInt.intR _ (fun a => LibInt.intR.mul (delta LibInt.intR a z.1) 1) (fun a => by
    rw [sumK_ext LibInt.intR _ (fun b => LibInt.intR.mul (delta LibInt.intR a z.1) (delta LibInt.intR b z.2)) (fun b => delta_pair m (a, b) z), ← mul_sumK,
      show sumK LibInt.intR (fun b => delta LibInt.intR b z.2) (CMInert.allZ (2 * m + 1 + 1) (2 * m + 1 + 1 + 1)) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR z.2])]
  rw [← sumK_mul, show sumK LibInt.intR (fun a => delta LibInt.intR a z.1) (CMInert.allZ (2 * m + 1 + 1) (2 * m + 1 + 1 + 1)) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR z.1]
  rfl

theorem some_ne_none (u : Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) : (some u : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) ≠ none :=
  fun h => by cases h

def ptsL : List (Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) := none :: (pL m).map some

theorem complete_pts : Complete LibInt.intR (ptsL m) := fun z => by
  show LibInt.intR.add (delta LibInt.intR none z) (sumK LibInt.intR (fun P => delta LibInt.intR P z) ((pL m).map some)) = LibInt.intR.one
  rw [sum_map]
  cases z with
  | none =>
    rw [dg_one _ _ rfl, sumK_ext LibInt.intR _ (fun _ => (0 : Int)) (fun u => dg_zero _ _ (some_ne_none m u)), QuadDiagonal.zsum]
    rfl
  | some q =>
    rw [dg_zero _ _ (fun e => some_ne_none m q e.symm), sumK_ext LibInt.intR _ (fun u => delta LibInt.intR u q) (fun u => by
      by_cases h : u = q
      · rw [dg_one u q h, dg_one (some u) (some q) (by rw [h])]
      · rw [dg_zero u q h, dg_zero (some u) (some q) (fun e => h (Option.some.inj e))]), complete_prod m q]
    rfl

def code : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) → Nat
  | none => 0
  | some (x, y) => 1 + x.val + (2 * m + 1 + 1 + 1) * y.val

theorem code_inj (u v : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) (h : code m u = code m v) : u = v := by
  cases u with
  | none => cases v with
    | none => rfl
    | some q =>
      have e : 0 = 1 + q.1.val + (2 * m + 1 + 1 + 1) * q.2.val := h
      exact absurd (e.trans (show 1 + q.1.val + (2 * m + 1 + 1 + 1) * q.2.val = Nat.succ (q.1.val + (2 * m + 1 + 1 + 1) * q.2.val) by lib_nat)) (fun e' => Nat.noConfusion e')
  | some p => cases v with
    | none =>
      have e : 1 + p.1.val + (2 * m + 1 + 1 + 1) * p.2.val = 0 := h
      exact absurd ((show Nat.succ (p.1.val + (2 * m + 1 + 1 + 1) * p.2.val) = 1 + p.1.val + (2 * m + 1 + 1 + 1) * p.2.val by lib_nat).trans e) (fun e' => Nat.noConfusion e')
    | some q =>
      have h' : Nat.succ (p.2.val * (2 * m + 1 + 1 + 1) + p.1.val) = Nat.succ (q.2.val * (2 * m + 1 + 1 + 1) + q.1.val) := by
        calc Nat.succ (p.2.val * (2 * m + 1 + 1 + 1) + p.1.val) = 1 + p.1.val + (2 * m + 1 + 1 + 1) * p.2.val := by lib_nat
          _ = 1 + q.1.val + (2 * m + 1 + 1 + 1) * q.2.val := h
          _ = _ := by lib_nat
      have e := Nat.succ.inj h'
      have h1 : p.1.val = q.1.val := Inf2e1.uniq_remG _ _ _ _ _ p.1.property q.1.property e
      have h2 : p.2.val = q.2.val := by
        rw [h1] at e
        have ei : Int.ofNat p.2.val * Int.ofNat (2 * m + 1 + 1 + 1) = Int.ofNat q.2.val * Int.ofNat (2 * m + 1 + 1 + 1) := by
          have := congrArg Int.ofNat e
          have e' : Int.ofNat p.2.val * Int.ofNat (2 * m + 1 + 1 + 1) + Int.ofNat q.1.val = Int.ofNat q.2.val * Int.ofNat (2 * m + 1 + 1 + 1) + Int.ofNat q.1.val := this
          calc _ = (Int.ofNat p.2.val * Int.ofNat (2 * m + 1 + 1 + 1) + Int.ofNat q.1.val) + -Int.ofNat q.1.val := by lib_int
            _ = (Int.ofNat q.2.val * Int.ofNat (2 * m + 1 + 1 + 1) + Int.ofNat q.1.val) + -Int.ofNat q.1.val := by rw [e']
            _ = _ := by lib_int
        exact Int.ofNat.inj (JacobiNorm.int_cancel _ _ (2 * m + 1 + 1) ei)
      exact congrArg some (Prod.ext (Subtype.ext h1) (Subtype.ext h2))

end Pairs

#print axioms Pairs.sum_map
#print axioms Pairs.dg_one
#print axioms Pairs.dg_zero
#print axioms Pairs.some_ne_none
#print axioms Pairs.sum_prodL
#print axioms Pairs.delta_pair
#print axioms Pairs.complete_prod
#print axioms Pairs.complete_pts
#print axioms Pairs.code_inj

lang/PencilClosed.lean

lean_all/lang/PencilClosed.lean

/-
  lang/PencilClosed — T43, cycle 305: the second moment over the pencil b ↦ y² = x³ + ax + b in closed form (p > 3, a ≠ 0).

  p = 2m + 3 ≠ 3, χ the Legendre symbol.
    sum_y2c      Σ_y χ(y² + c) = −1 for c ≠ 0                       (QuadDiagonal.conv, ConicSum.sum_xxd)
    delta_sub    δ(z − d, 0) = δ(z, d)
    quad_count   #{x : x² + xy + y² + a = 0} = 1 + χ(−3y² − 4a)      (complete the square)
    conic_count  #{(x, y) : x² + xy + y² + a = 0} = p − χ(−3)       (a ≠ 0)
    diag_count   #{x : 3x² + a = 0} = 1 + χ(−3a)
    N_closed     #{(x, y) : x³ + ax = y³ + ay} = 2p − 1 − χ(−3) − χ(−3a)
    pencil_closed  Σ_b (Σ_x χ(x³ + ax + b))² = p² − p − p χ(−3) − p χ(−3a)
    pencil_zero    a = 0, p ≡ 2 (mod 3): Σ_b (Σ_x χ(x³ + b))² = 0
    pencil_zero_all  a = 0, every p > 3: Σ_b (Σ_x χ(x³ + b))² = p(p − 1)(1 + χ(−3))
    quad_sum, quad_sum0  Σ_x χ(x² + bx + c) = −1 if b² − 4c ≠ 0, p − 1 if b² − 4c = 0
    conic_points, conic_points0  #{y² = x² + bx + c} = p − 1, resp. 2p − 1
    quad_sum_a, quad_sum_a0, conic_points_a   a ≠ 0: Σ_x χ(ax² + bx + c) = −χ(a), resp. (p − 1)χ(a); #{y² = ax² + bx + c} = p − χ(a)
    binary_form   a, b, c ≠ 0: #{ax² + by² = c} = p − χ(−ab)
    binary_form0  b ≠ 0: #{ax² + by² = 0} = p + (p − 1)χ(−ab)
    ternary_form  a, b, c ≠ 0, any d: #{ax² + by² + cz² = d} = p² + pχ(−abcd)
    quaternary_form  a, b, c, d, e ≠ 0: #{ax² + by² + cz² + dw² = e} = p³ − pχ(abcd)
    quaternary_form0  a, b, c, d ≠ 0: #{ax² + by² + cz² + dw² = 0} = p³ + (p² − p)χ(abcd)
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.PencilMoment
import lang.QuadDiagonal
import lang.CMEisen

namespace PencilClosed

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum Jacobsthal QuadTwist JacobMoment PencilMoment
open FiniteRecChain (ite_pos' ite_neg')

variable (m : Nat)

theorem nzm (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1)) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hy : y ≠ (ring (2 * m + 1 + 1)).zero) : (ring (2 * m + 1 + 1)).mul x y ≠ (ring (2 * m + 1 + 1)).zero := fun e => by
  cases nz2 m hpr _ _ e with
  | inl h => exact hx h
  | inr h => exact hy h

theorem negnz (x : Zp (2 * m + 1 + 1)) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) : (ring (2 * m + 1 + 1)).neg x ≠ (ring (2 * m + 1 + 1)).zero := fun e => hx (by
  calc x = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).neg x) := by lib_ring
    _ = (ring (2 * m + 1 + 1)).zero := by rw [e]; lib_ring)

theorem chi_unit (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (t : Zp (2 * m + 1 + 1)) (ht : t ≠ (ring (2 * m + 1 + 1)).zero) : chi m t * chi m t = 1 :=
  QuadDiagonal.chi_sq_val m hpr t ht

theorem chi_iv (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (t : Zp (2 * m + 1 + 1)) (ht : t ≠ (ring (2 * m + 1 + 1)).zero) : chi m (iv m t) = chi m t := by
  have h1 : chi m (iv m t) * chi m t = 1 := by rw [← chi_mul m hpr, iv_mul_self m hpr t ht]; exact chi_one m
  calc chi m (iv m t) = chi m (iv m t) * (chi m t * chi m t) := by rw [chi_unit m hpr t ht]; lib_int
    _ = (chi m (iv m t) * chi m t) * chi m t := by lib_int
    _ = chi m t := by rw [h1]; lib_int

/-- **Σ_y χ(y² + c) = −1** for c ≠ 0. -/
theorem sum_y2c (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (c : Zp (2 * m + 1 + 1)) (hc : c ≠ (ring (2 * m + 1 + 1)).zero) :
    sumK LibInt.intR (fun y => chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) c)) (L m) = -1 := by
  rw [QuadDiagonal.conv m hpr (fun t => chi m ((ring (2 * m + 1 + 1)).add t c))]
  rw [sumK_ext LibInt.intR _ (fun t => chi m ((ring (2 * m + 1 + 1)).add t c) + chi m ((ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)).add t c))) (fun t => by
    rw [chi_mul m hpr]; show (1 + chi m t) * chi m ((ring (2 * m + 1 + 1)).add t c) = _; lib_int)]
  rw [QuadDiagonal.sI_add, shift_sum m (chi m) c, show sumK LibInt.intR (chi m) (L m) = 0 from chi_sum m hpr, sum_xxd m hpr c hc]
  lib_int

theorem delta_sub (z d : Zp (2 * m + 1 + 1)) : delta LibInt.intR ((ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg d)) (ring (2 * m + 1 + 1)).zero = delta LibInt.intR z d := by
  by_cases h : z = d
  · rw [delta_one m _ _ h, delta_one m _ _ (by rw [h]; lib_ring)]
  · rw [delta_zero m _ _ h, delta_zero m _ _ (fun e => h (by
      calc z = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg d)) d := by lib_ring
        _ = d := by rw [e]; lib_ring))]

theorem iv_nz (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (t : Zp (2 * m + 1 + 1)) (ht : t ≠ (ring (2 * m + 1 + 1)).zero) : iv m t ≠ (ring (2 * m + 1 + 1)).zero := fun e => by
  have h := iv_mul_self m hpr t ht
  rw [e] at h
  exact PowerSum.one_ne_zero' (2 * m + 1) (by rw [← h]; lib_ring)

/-- **#{y : y² + xy + x² + a = 0} = 1 + χ(−3x² − 4a).** -/
theorem quad_count (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x : Zp (2 * m + 1 + 1)) :
    sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x y) ((ring (2 * m + 1 + 1)).mul y y)))) (ring (2 * m + 1 + 1)).zero) (L m) = 1 + chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (rin  -- (+403 chars; full line in the Zenodo source)
  have h2 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ≠ (ring (2 * m + 1 + 1)).zero := QuadDiagonal.two_ne m
  have e2 : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ h2
  have hS : ∀ y, (ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x y) ((ring (2 * m + 1 + 1)).mul y y))) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)  -- (+164 chars; full line in the Zenodo source)
      ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) x) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 *  -- (+42 chars; full line in the Zenodo source)
    calc (ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x y) ((ring (2 * m + 1 + 1)).mul y y))) = (ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x y) ((ring (2 * m + 1 + 1)).  -- (+237 chars; full line in the Zenodo source)
      _ = _ := by lib_ring
  rw [sumK_ext LibInt.intR _ _ (fun y => by rw [hS y, delta_sub])]
  rw [shift_sum m (fun z => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) x) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) x)) ((ring (2 * m + 1 + 1)  -- (+73 chars; full line in the Zenodo source)
  rw [QuadDiagonal.sq_count m hpr]
  have h4 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)  -- (+226 chars; full line in the Zenodo source)
      = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).  -- (+125 chars; full line in the Zenodo source)
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m   -- (+134 chars; full line in the Zenodo source)
          ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) a))) := by lib_ring
      _ = _ := by rw [e2]; lib_ring
  rw [← h4, chi_mul m hpr, chi_sq m hpr _ h2]
  lib_int

/-- **#{(x, y) : x² + xy + y² + a = 0} = p − χ(−3)** for a ≠ 0, p ≠ 3. -/
theorem conic_count (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h3 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ≠ (ring (2 * m + 1 + 1)).zero) (a : Zp (2 * m + 1 + 1)) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) :
    sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x y) ((ring (2 * m + 1 + 1)).mul y y)))) (ring (2 * m + 1 + 1)).zero) (L m)) (L m) = P m + -chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).  -- (+94 chars; full line in the Zenodo source)
  have h2 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ≠ (ring (2 * m + 1 + 1)).zero := QuadDiagonal.two_ne m
  have e3 : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ h3
  have hc : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul a (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1  -- (+117 chars; full line in the Zenodo source)
  rw [sumK_ext LibInt.intR _ _ (fun x => quad_count m hpr a x)]
  have hT : ∀ x, chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (  -- (+798 chars; full line in the Zenodo source)
    have hTe : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1  -- (+792 chars; full line in the Zenodo source)
      symm
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1  -- (+484 chars; full line in the Zenodo source)
        _ = _ := by rw [e3]; lib_ring
    rw [hTe, chi_mul m hpr]
  rw [sumK_ext LibInt.intR _ (fun x => 1 + chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) * chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ri  -- (+313 chars; full line in the Zenodo source)
    QuadDiagonal.sI_add, QuadDiagonal.sI_mul, sum_one, sum_y2c m hpr _ hc]
  lib_int

/-- **#{x : 3x² + a = 0} = 1 + χ(−3a).** -/
theorem diag_count (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h3 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ≠ (ring (2 * m + 1 + 1)).zero) (a : Zp (2 * m + 1 + 1)) :
    sumK LibInt.intR (fun x => delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)))) (ring (2 * m + 1 + 1)).zero) (L m) = 1 + chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((rin  -- (+88 chars; full line in the Zenodo source)
  have e3 : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ h3
  have hi3 := iv_nz m hpr _ h3
  have hpt : ∀ x, delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)))) (ring (2 * m + 1 + 1)).zero = delta LibInt.intR ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a (iv m ((ring (2 * m + 1 + 1)).add (ri  -- (+125 chars; full line in the Zenodo source)
    have hS : (ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ri  -- (+270 chars; full line in the Zenodo source)
      symm
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) ) ((ring (2 *   -- (+159 chars; full line in the Zenodo source)
        _ = _ := by rw [e3]; lib_ring
    rw [hS, ← delta_sub m ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)))))]
    by_cases hz : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)))))) = (ring (2 * m + 1 + 1)).zero
    · rw [hz, delta_one m _ _ rfl, delta_one m _ _ (by lib_ring)]
    · rw [delta_zero m _ _ hz, delta_zero m _ _ (nzm m hpr _ _ h3 hz)]
  rw [sumK_ext LibInt.intR _ _ hpt, QuadDiagonal.sq_count m hpr]
  have : (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 +   -- (+367 chars; full line in the Zenodo source)
    symm
    calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 +  -- (+174 chars; full line in the Zenodo source)
      _ = _ := by rw [e3]; lib_ring
  rw [this, chi_mul m hpr, chi_mul m hpr, chi_unit m hpr _ hi3]
  lib_int

theorem split (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) :
    delta LibInt.intR (u m a x) (u m a y) = delta LibInt.intR y x + delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x y) ((ring (2 * m + 1 + 1)).mul y y)))) (ring (2 * m + 1 + 1)).zero + -(delta LibInt.intR y x * delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)  -- (+170 chars; full line in the Zenodo source)
  by_cases hxy : y = x
  · rw [hxy, delta_one m _ _ rfl, delta_one m _ _ rfl]
    generalize delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)))) (ring (2 * m + 1 + 1)).zero = d
    lib_int
  · rw [delta_zero m _ _ hxy]
    have hs : delta LibInt.intR (u m a x) (u m a y) = delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x y) ((ring (2 * m + 1 + 1)).mul y y)))) (ring (2 * m + 1 + 1)).zero := by
      by_cases ha : (ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x y) ((ring (2 * m + 1 + 1)).mul y y))) = (ring (2 * m + 1 + 1)).zero
      · rw [delta_one m _ _ ha, delta_one m _ _ (by
          unfold u
          calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul a x)
              = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul y y)) ((ring (2 * m + 1 + 1)).mul a y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg y)) ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)  -- (+62 chars; full line in the Zenodo source)
            _ = _ := by rw [ha]; lib_ring)]
      · rw [delta_zero m _ _ ha, delta_zero m _ _ (fun e => by
          unfold u at e
          have e2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg y)) ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x y) ((ring (2 * m + 1 + 1)).mul y y)))) = (ring (2 * m + 1 + 1)).zero := by
            calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul a x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul y y)) ((ring (2 * m + 1 + 1)).mul a y))) := by lib_ring
              _ = (ring (2 * m + 1 + 1)).zero := by rw [e]; lib_ring
          cases nz2 m hpr _ _ e2 with
          | inl h => exact hxy (by
              calc y = (ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg y)) := by rw [h]; lib_ring
                _ = x := by lib_ring)
          | inr h => exact ha h)]
    rw [hs]
    generalize delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x y) ((ring (2 * m + 1 + 1)).mul y y)))) (ring (2 * m + 1 + 1)).zero = d
    lib_int

theorem inner_eq (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x : Zp (2 * m + 1 + 1)) :
    sumK LibInt.intR (fun y => delta LibInt.intR (u m a x) (u m a y)) (L m)
      = 1 + sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x y) ((ring (2 * m + 1 + 1)).mul y y)))) (ring (2 * m + 1 + 1)).zero) (L m) + -(delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring   -- (+124 chars; full line in the Zenodo source)
  rw [sumK_ext LibInt.intR _ _ (split m hpr a x)]
  rw [sumK_ext LibInt.intR _ (fun y => (delta LibInt.intR y x + delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x y) ((ring (2 * m + 1 + 1)).mul y y)))) (ring (2 * m + 1 + 1)).zero) + (-1) * LibInt.intR.mul (delta LibInt.intR y x) (delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((r  -- (+197 chars; full line in the Zenodo source)
    show _ = _ + -1 * (delta LibInt.intR y x * delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x y) ((ring (2 * m + 1 + 1)).mul y y)))) (ring (2 * m + 1 + 1)).zero); generalize delta LibInt.intR y x = e; generalize delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 +   -- (+180 chars; full line in the Zenodo source)
  rw [QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_mul,
    FiniteRecChain.pick LibInt.intR (L m) (QuadDiagonal.hcomp m) (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x y) ((ring (2 * m + 1 + 1)).mul y y)))) (ring (2 * m + 1 + 1)).zero) x,
    show sumK LibInt.intR (fun y => delta LibInt.intR y x) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR x]
  lib_int

/-- **#{(x, y) : x³ + ax = y³ + ay} = 2p − 1 − χ(−3) − χ(−3a)** for a ≠ 0, p ≠ 3. -/
theorem N_closed (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h3 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ≠ (ring (2 * m + 1 + 1)).zero) (a : Zp (2 * m + 1 + 1)) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) :
    sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR (u m a x) (u m a y)) (L m)) (L m)
      = P m + P m + -1 + -chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) + -chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).on  -- (+13 chars; full line in the Zenodo source)
  rw [sumK_ext LibInt.intR _ _ (inner_eq m hpr a)]
  rw [sumK_ext LibInt.intR _ (fun x => (1 + sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x y) ((ring (2 * m + 1 + 1)).mul y y)))) (ring (2 * m + 1 + 1)).zero) (L m)) + (-1) * delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((  -- (+169 chars; full line in the Zenodo source)
    show _ = _ + -1 * _; generalize sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x y) ((ring (2 * m + 1 + 1)).mul y y)))) (ring (2 * m + 1 + 1)).zero) (L m) = c; generalize delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ri  -- (+168 chars; full line in the Zenodo source)
  rw [QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_mul, sum_one, conic_count m hpr h3 a ha, diag_count m hpr h3 a]
  lib_int

/-- **The second moment over the pencil, in closed form**: Σ_b (Σ_x χ(x³ + ax + b))² = p² − p − pχ(−3) − pχ(−3a), a ≠ 0, p > 3. -/
theorem pencil_closed (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h3 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ≠ (ring (2 * m + 1 + 1)).zero) (a : Zp (2 * m + 1 + 1)) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) :
    sumK LibInt.intR (fun b => W m a b * W m a b) (L m)
      = P m * P m + -(P m) + -(P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)))) + -(P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring   -- (+33 chars; full line in the Zenodo source)
  rw [pencil_moment m hpr a, N_closed m hpr h3 a ha]
  lib_int

theorem cube_pw (z : Zp (2 * m + 1 + 1)) : Frobenius.pw (ring (2 * m + 1 + 1)) z 3 = (ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z) := by
  show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one z) z) z = _; lib_ring

/-- **The pencil b ↦ y² = x³ + b at p ≡ 2 (mod 3)**: Σ_b (Σ_x χ(x³ + b))² = 0 (x ↦ x³ is a bijection, 3e = 2(p − 1) + 1). -/
theorem pencil_zero (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (e : Nat) (he : 3 * e = 2 * (2 * m + 1 + 1) + 1) :
    sumK LibInt.intR (fun b => W m (ring (2 * m + 1 + 1)).zero b * W m (ring (2 * m + 1 + 1)).zero b) (L m) = 0 := by
  rw [pencil_moment m hpr (ring (2 * m + 1 + 1)).zero]
  have hpt : ∀ x y, delta LibInt.intR (u m (ring (2 * m + 1 + 1)).zero x) (u m (ring (2 * m + 1 + 1)).zero y) = delta LibInt.intR y x := fun x y => by
    by_cases hxy : y = x
    · rw [hxy, delta_one m _ _ rfl, delta_one m _ _ rfl]
    · rw [delta_zero m _ _ hxy, delta_zero m _ _ (fun h => hxy (by
        have h' : Frobenius.pw (ring (2 * m + 1 + 1)) x 3 = Frobenius.pw (ring (2 * m + 1 + 1)) y 3 := by
          rw [cube_pw, cube_pw]
          calc (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x) = u m (ring (2 * m + 1 + 1)).zero x := by unfold u; lib_ring
            _ = u m (ring (2 * m + 1 + 1)).zero y := h
            _ = _ := by unfold u; lib_ring
        calc y = Frobenius.pw (ring (2 * m + 1 + 1)) (Frobenius.pw (ring (2 * m + 1 + 1)) y 3) e := (CMEisen.cube_inv (2 * m + 1) hpr e he y).symm
          _ = Frobenius.pw (ring (2 * m + 1 + 1)) (Frobenius.pw (ring (2 * m + 1 + 1)) x 3) e := by rw [h']
          _ = x := CMEisen.cube_inv (2 * m + 1) hpr e he x))]
  rw [sumK_ext LibInt.intR _ (fun _ => (1 : Int)) (fun x => by
    rw [sumK_ext LibInt.intR _ _ (hpt x)]; exact CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR x), sum_one]
  lib_int

/-- **The pencil y² = x³ + b, every p > 3**: Σ_b (Σ_x χ(x³ + b))² = p(p − 1)(1 + χ(−3)) (Birch's total minus the closed forms a ≠ 0). -/
theorem pencil_zero_all (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h3 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ≠ (ring (2 * m + 1 + 1)).zero) :
    sumK LibInt.intR (fun b => W m (ring (2 * m + 1 + 1)).zero b * W m (ring (2 * m + 1 + 1)).zero b) (L m) = (P m * P m + -(P m)) * (1 + chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)))) := by
  have hb := birch_moment2 m hpr
  generalize hS0 : sumK LibInt.intR (fun b => W m (ring (2 * m + 1 + 1)).zero b * W m (ring (2 * m + 1 + 1)).zero b) (L m) = S0
  have hpt : ∀ a, sumK LibInt.intR (fun b => W m a b * W m a b) (L m)
      = (P m * P m + -(P m) + -(P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)))) + -(P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring  -- (+552 chars; full line in the Zenodo source)
    by_cases ha : a = (ring (2 * m + 1 + 1)).zero
    · rw [ha, delta_one m _ _ rfl, hS0]; show S0 = _ + 1 * (S0 + -_); lib_int
    · rw [pencil_closed m hpr h3 a ha, delta_zero m _ _ ha]; show _ = _ + 0 * _; lib_int
  rw [sumK_ext LibInt.intR _ (fun a => LibInt.intR.add ((P m * P m + -(P m) + -(P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)))) + -(P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 *   -- (+614 chars; full line in the Zenodo source)
  rw [sumK_add, ← sumK_mul, show sumK LibInt.intR (fun a => delta LibInt.intR a (ring (2 * m + 1 + 1)).zero) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR (ring (2 * m + 1 + 1)).zero] at hb
  have hf : sumK LibInt.intR (fun a => P m * P m + -(P m) + -(P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)))) + -(P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (r  -- (+285 chars; full line in the Zenodo source)
    rw [sumK_ext LibInt.intR _ (fun a => (P m * P m + -(P m) + -(P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))))) * 1 + (-(P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)  -- (+61 chars; full line in the Zenodo source)
      show _ = _
      rw [show (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) a) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)  -- (+49 chars; full line in the Zenodo source)
    rw [QuadDiagonal.sI_add, QuadDiagonal.sI_mul, QuadDiagonal.sI_mul, sum_one, show sumK LibInt.intR (chi m) (L m) = 0 from chi_sum m hpr]
    lib_int
  rw [hf] at hb
  have hf0 : (P m * P m + -(P m) + -(P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)))) + -(P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one   -- (+285 chars; full line in the Zenodo source)
    rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).zero = (ring (2 * m + 1 + 1)).zero by lib_ring, show (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).zero = (ring (2 * m + 1 + 1)).zero by lib_ring, chi_zero]
  rw [hf0] at hb
  have hb' : (P m * P m + -(P m) + -(P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))))) * P m + 1 * (S0 + -(P m * P m + -(P m) + -(P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m +   -- (+96 chars; full line in the Zenodo source)
  calc S0 = ((P m * P m + -(P m) + -(P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))))) * P m + 1 * (S0 + -(P m * P m + -(P m) + -(P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m +   -- (+510 chars; full line in the Zenodo source)
    _ = (P m * P m * P m + -(P m * P m)) + -((P m * P m + -(P m) + -(P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))))) * P m) + (P m * P m + -(P m) + -(P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1  -- (+93 chars; full line in the Zenodo source)
    _ = _ := by lib_int

theorem quad_shift (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (b c x : Zp (2 * m + 1 + 1)) :
    (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b x) c) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) b)) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).mul (iv m  -- (+455 chars; full line in the Zenodo source)
  have e2 : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ (QuadDiagonal.two_ne m)
  symm
  calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b x) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) c) := by lib_ring
    _ = _ := by rw [e2]; lib_ring

theorem disc4 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (b c : Zp (2 * m + 1 + 1)) : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2  -- (+588 chars; full line in the Zenodo source)
  have e2 : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ (QuadDiagonal.two_ne m)
  calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m +   -- (+402 chars; full line in the Zenodo source)
    _ = _ := by rw [e2]; lib_ring

/-- **Σ_x χ(x² + bx + c) = −1** when b² − 4c ≠ 0. -/
theorem quad_sum (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (b c : Zp (2 * m + 1 + 1)) (hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b b) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).on  -- (+67 chars; full line in the Zenodo source)
    sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b x) c))) (L m) = -1 := by
  have hd : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) b) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) b)) ((ring (2 * m + 1 + 1)).neg c) ≠ (ring (2 * m + 1 + 1)).zero := fun e => hD (by rw [← disc  -- (+26 chars; full line in the Zenodo source)
  rw [sumK_ext LibInt.intR _ _ (fun x => by rw [quad_shift m hpr b c x])]
  rw [shift_sum m (fun z => chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) b) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 *   -- (+178 chars; full line in the Zenodo source)
  exact sum_y2c m hpr _ (negnz m _ hd)

/-- **Σ_x χ(x² + bx + c) = p − 1** when b² − 4c = 0. -/
theorem quad_sum0 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (b c : Zp (2 * m + 1 + 1)) (hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b b) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).o  -- (+68 chars; full line in the Zenodo source)
    sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b x) c))) (L m) = P m + -1 := by
  have hd : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) b) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) b)) ((ring (2 * m + 1 + 1)).neg c) = (ring (2 * m + 1 + 1)).zero := by
    have h4 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ≠ (ring (2 * m + 1 + 1)).zero := nzm m hpr _ _ (QuadDiagonal.two_ne m) (QuadDiagonal.two_ne m)
    have := disc4 m hpr b c
    rw [hD] at this
    cases nz2 m hpr _ _ this with
    | inl h => exact absurd h h4
    | inr h => exact h
  rw [sumK_ext LibInt.intR _ _ (fun x => by rw [quad_shift m hpr b c x, hd])]
  rw [shift_sum m (fun z => chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).zero))) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) b)]
  rw [sumK_ext LibInt.intR _ (fun z => 1 + -1 * delta LibInt.intR z (ring (2 * m + 1 + 1)).zero) (fun z => by
    rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).zero) = (ring (2 * m + 1 + 1)).mul z z by lib_ring]
    exact sq_delta m hpr z)]
  rw [QuadDiagonal.sI_add, QuadDiagonal.sI_mul, sum_one, show sumK LibInt.intR (fun z => delta LibInt.intR z (ring (2 * m + 1 + 1)).zero) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR (ring (2 * m + 1 + 1)).zero]
  lib_int

/-- **#{(x, y) : y² = x² + bx + c} = p − 1** when b² − 4c ≠ 0 (a smooth affine conic with two points at infinity). -/
theorem conic_points (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (b c : Zp (2 * m + 1 + 1)) (hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b b) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)  -- (+71 chars; full line in the Zenodo source)
    sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b x) c))) (L m)) (L m) = P m + -1 := by
  rw [sumK_ext LibInt.intR _ _ (fun x => QuadDiagonal.sq_count m hpr ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b x) c))), QuadDiagonal.sI_add, sum_one, quad_sum m hpr b c hD]

/-- **#{(x, y) : y² = x² + bx + c} = 2p − 1** when b² = 4c (two crossing lines). -/
theorem conic_points0 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (b c : Zp (2 * m + 1 + 1)) (hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b b) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1  -- (+72 chars; full line in the Zenodo source)
    sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b x) c))) (L m)) (L m) = P m + P m + -1 := by
  rw [sumK_ext LibInt.intR _ _ (fun x => QuadDiagonal.sq_count m hpr ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b x) c))), QuadDiagonal.sI_add, sum_one, quad_sum0 m hpr b c hD]
  lib_int

theorem scale_q (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b c x : Zp (2 * m + 1 + 1)) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) :
    (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b x) c) = (ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m a) b) x) ((ring (2 * m + 1 + 1)).mul (iv m a) c))) := by
  have e : (ring (2 * m + 1 + 1)).mul (iv m a) a = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr a ha
  symm
  calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a (iv m a)) ((ring (2 * m + 1 + 1)).mul b x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a (iv m a)) c)) := by lib_ring
    _ = _ := by rw [show (ring (2 * m + 1 + 1)).mul a (iv m a) = (ring (2 * m + 1 + 1)).one by rw [((ring (2 * m + 1 + 1))).mul_comm]; exact e]; lib_ring

theorem disc_scale (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b c : Zp (2 * m + 1 + 1)) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) :
    (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m a) b) ((ring (2 * m + 1 + 1)).mul (iv m a) b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2  -- (+462 chars; full line in the Zenodo source)
  have e : (ring (2 * m + 1 + 1)).mul (iv m a) a = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr a ha
  symm
  calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m a) (iv m a)) ((ring (2 * m + 1 + 1)).mul b b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((  -- (+123 chars; full line in the Zenodo source)
    _ = _ := by rw [show (ring (2 * m + 1 + 1)).mul a (iv m a) = (ring (2 * m + 1 + 1)).one by rw [((ring (2 * m + 1 + 1))).mul_comm]; exact e]; lib_ring

/-- **Σ_x χ(ax² + bx + c) = −χ(a)** when a ≠ 0 and b² − 4ac ≠ 0. -/
theorem quad_sum_a (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b c : Zp (2 * m + 1 + 1)) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) (hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b b) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2   -- (+141 chars; full line in the Zenodo source)
    sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b x) c))) (L m) = -chi m a := by
  rw [sumK_ext LibInt.intR _ _ (fun x => by rw [scale_q m hpr a b c x ha, chi_mul m hpr]), QuadDiagonal.sI_mul,
    quad_sum m hpr _ _ (by rw [disc_scale m hpr a b c ha]; exact nzm m hpr _ _ (nzm m hpr _ _ (iv_nz m hpr a ha) (iv_nz m hpr a ha)) hD)]
  lib_int

/-- **Σ_x χ(ax² + bx + c) = (p − 1)χ(a)** when a ≠ 0 and b² = 4ac. -/
theorem quad_sum_a0 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b c : Zp (2 * m + 1 + 1)) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) (hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b b) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2  -- (+142 chars; full line in the Zenodo source)
    sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b x) c))) (L m) = (P m + -1) * chi m a := by
  rw [sumK_ext LibInt.intR _ _ (fun x => by rw [scale_q m hpr a b c x ha, chi_mul m hpr]), QuadDiagonal.sI_mul,
    quad_sum0 m hpr _ _ (by rw [disc_scale m hpr a b c ha, hD]; lib_ring)]
  lib_int

/-- **#{(x, y) : y² = ax² + bx + c} = p − χ(a)** when a ≠ 0, b² ≠ 4ac. -/
theorem conic_points_a (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b c : Zp (2 * m + 1 + 1)) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) (hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b b) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring  -- (+145 chars; full line in the Zenodo source)
    sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b x) c))) (L m)) (L m) = P m + -chi m a := by
  rw [sumK_ext LibInt.intR _ _ (fun x => QuadDiagonal.sq_count m hpr ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b x) c))), QuadDiagonal.sI_add, sum_one, quad_sum_a m hpr a b c ha hD]

theorem delta_scale (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (b z t : Zp (2 * m + 1 + 1)) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) :
    delta LibInt.intR ((ring (2 * m + 1 + 1)).mul b z) t = delta LibInt.intR z ((ring (2 * m + 1 + 1)).mul (iv m b) t) := by
  have e : (ring (2 * m + 1 + 1)).mul (iv m b) b = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr b hb
  by_cases h : z = (ring (2 * m + 1 + 1)).mul (iv m b) t
  · rw [delta_one m _ _ h, delta_one m _ _ (by
      rw [h]
      calc (ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul (iv m b) t) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m b) b) t := by lib_ring
        _ = t := by rw [e]; lib_ring)]
  · rw [delta_zero m _ _ h, delta_zero m _ _ (fun h' => h (by
      calc z = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m b) b) z := by rw [e]; lib_ring
        _ = (ring (2 * m + 1 + 1)).mul (iv m b) ((ring (2 * m + 1 + 1)).mul b z) := by lib_ring
        _ = _ := by rw [h']))]

/-- **#{(x, y) : ax² + by² = c} = p − χ(−ab)** for a, b, c ≠ 0. -/
theorem binary_form (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b c : Zp (2 * m + 1 + 1)) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) (hc : c ≠ (ring (2 * m + 1 + 1)).zero) :
    sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y))) c) (L m)) (L m)
      = P m + -chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b)) := by
  have hib := iv_nz m hpr b hb
  have hpt : ∀ x y, delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y))) c
      = delta LibInt.intR ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (iv m b)) a) ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero x) ((ring (2 * m + 1 + 1)).mul (iv m b) c))) := fun x y => by
    rw [← delta_sub, show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y))) ((ring (2 * m + 1 + 1)).neg c)
        = (ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (iv m b) ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x))))))) by
          have e : (ring (2 * m + 1 + 1)).mul (iv m b) b = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr b hb
          symm
          calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m b) b) ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)))))) := by lib_ring
            _ = _ := by rw [e]; lib_ring]
    by_cases hz : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (iv m b) ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)))))) = (ring (2 * m + 1 + 1)).zero
    · rw [hz, delta_one m _ _ (by lib_ring), delta_one m _ _ (by
        calc (ring (2 * m + 1 + 1)).mul y y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (iv m b) ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x))))))) ((ring (2 * m + 1 + 1)).mul (iv m b) ((ring (2 * m + 1 + 1)).add c ((ring   -- (+102 chars; full line in the Zenodo source)
          _ = _ := by rw [hz]; lib_ring)]
    · rw [delta_zero m _ _ (nzm m hpr _ _ hb hz), delta_zero m _ _ (fun e => hz (by rw [e]; lib_ring))]
  rw [sumK_ext LibInt.intR _ _ (fun x => by rw [sumK_ext LibInt.intR _ _ (hpt x), QuadDiagonal.sq_count m hpr])]
  rw [QuadDiagonal.sI_add, sum_one]
  have hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).m  -- (+150 chars; full line in the Zenodo source)
    rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).  -- (+115 chars; full line in the Zenodo source)
        = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m b) (iv m b)) ((ring (2 * m + 1 + 1)).mul a c)) by lib_ring]
    exact nzm m hpr _ _ (nzm m hpr _ _ (QuadDiagonal.two_ne m) (QuadDiagonal.two_ne m)) (nzm m hpr _ _ (nzm m hpr _ _ hib hib) (nzm m hpr _ _ ha hc))
  rw [quad_sum_a m hpr _ _ _ (nzm m hpr _ _ (negnz m _ hib) ha) hD]
  rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (iv m b)) a = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b)) ((ring (2 * m + 1 + 1)).mul (iv m b) (iv m b)) by
    have e : (ring (2 * m + 1 + 1)).mul (iv m b) b = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr b hb
    symm
    calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul (iv m b) ((ring (2 * m + 1 + 1)).mul (iv m b) b))) := by lib_ring
      _ = _ := by rw [e]; lib_ring, chi_mul m hpr, chi_mul m hpr, chi_unit m hpr _ hib]
  lib_int

/-- **#{(x, y) : ax² + by² = 0} = p + (p − 1)χ(−ab)** for b ≠ 0. -/
theorem binary_form0 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b : Zp (2 * m + 1 + 1)) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) :
    sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y))) (ring (2 * m + 1 + 1)).zero) (L m)) (L m)
      = P m + (P m + -1) * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b)) := by
  have hib := iv_nz m hpr b hb
  have e : (ring (2 * m + 1 + 1)).mul (iv m b) b = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr b hb
  have hpt : ∀ x y, delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y))) (ring (2 * m + 1 + 1)).zero
      = delta LibInt.intR ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (iv m b)) a) ((ring (2 * m + 1 + 1)).mul x x)) := fun x y => by
    have hS : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y))
        = (ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (iv m b)) a) ((ring (2 * m + 1 + 1)).mul x x)))) := by
      symm
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m b) b) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x))) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y)) := by lib_ring
        _ = _ := by rw [e]; lib_ring
    rw [hS, ← delta_sub m ((ring (2 * m + 1 + 1)).mul y y)]
    by_cases hz : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (iv m b)) a) ((ring (2 * m + 1 + 1)).mul x x))) = (ring (2 * m + 1 + 1)).zero
    · rw [hz, delta_one m _ _ (by lib_ring), delta_one m _ _ rfl]
    · rw [delta_zero m _ _ (nzm m hpr _ _ hb hz), delta_zero m _ _ hz]
  have hc : chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (iv m b)) a) = chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b)) := by
    rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (iv m b)) a = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b)) ((ring (2 * m + 1 + 1)).mul (iv m b) (iv m b)) by
      symm
      calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul (iv m b) ((ring (2 * m + 1 + 1)).mul (iv m b) b))) := by lib_ring
        _ = _ := by rw [e]; lib_ring, chi_mul m hpr, chi_mul m hpr (iv m b) (iv m b), chi_unit m hpr _ hib]
    lib_int
  rw [sumK_ext LibInt.intR _ (fun x => 1 * 1 + chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b)) * 1 + (-chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b))) * delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) (fun x => by
    rw [sumK_ext LibInt.intR _ _ (hpt x), QuadDiagonal.sq_count m hpr, chi_mul m hpr, hc, sq_delta m hpr]
    show 1 + chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b)) * (1 + -1 * delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) = _
    generalize chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b)) = t; generalize delta LibInt.intR x (ring (2 * m + 1 + 1)).zero = d; lib_int)]
  rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.add (LibInt.intR.mul (1 * 1 + chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b)) * 1) 1) (LibInt.intR.mul (-chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b))) (delta LibInt.intR x (ring (2 * m + 1 + 1)).zero))) (fun x => by
    show _ = (1 * 1 + chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b)) * 1) * 1 + -chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b)) * delta LibInt.intR x (ring (2 * m + 1 + 1)).zero; lib_int),
    sumK_add, ← mul_sumK, ← mul_sumK, sum_one, show sumK LibInt.intR (fun x => delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR (ring (2 * m + 1 + 1)).zero]
  show (1 * 1 + chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b)) * 1) * P m + -chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b)) * 1 = _
  lib_int

/-- **#{(x, y, z) : ax² + by² + cz² = d} = p² + pχ(−abcd)** for a, b, c ≠ 0 (any d). -/
theorem ternary_form (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b c d : Zp (2 * m + 1 + 1)) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) (hc : c ≠ (ring (2 * m + 1 + 1)).zero) :
    sumK LibInt.intR (fun z => sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y))) ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul z z))) d) (L m)) (L m)) (L m)
      = P m * P m + P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) ((ring (2 * m + 1 + 1)).mul c d))) := by
  have hz : ∀ z, sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y))) ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul z z))) d) (L m)) (L m)
      = (P m + -chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b))) + P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b)) * delta LibInt.intR ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul z z)) d := fun z => by
    have hpt : ∀ x y, delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y))) ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul z z))) d = delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x   -- (+209 chars; full line in the Zenodo source)
      rw [← delta_sub m _ d, ← delta_sub m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y))) ((ring (2 * m + 1 + 1)).add d ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul z z)))), show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m +  -- (+237 chars; full line in the Zenodo source)
        = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add d ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul z z))))) by lib_ring]
    rw [sumK_ext LibInt.intR _ _ (fun x => sumK_ext LibInt.intR _ _ (hpt x) _)]
    by_cases h0 : (ring (2 * m + 1 + 1)).add d ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul z z))) = (ring (2 * m + 1 + 1)).zero
    · rw [h0, binary_form0 m hpr a b hb, delta_one m _ _ (by
        calc (ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul z z) = (ring (2 * m + 1 + 1)).add d ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add d ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul z z))))) := by lib_ring
          _ = d := by rw [h0]; lib_ring)]
      lib_int
    · rw [binary_form m hpr a b _ ha hb h0, delta_zero m _ _ (fun e => h0 (by rw [e]; lib_ring))]
      lib_int
  rw [sumK_ext LibInt.intR _ _ hz]
  rw [sumK_ext LibInt.intR _ (fun z => LibInt.intR.add (LibInt.intR.mul (P m + -chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b))) 1) (LibInt.intR.mul (P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b))) (delta LibInt.intR ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul z z)) d))) (fun z => by
    show _ = (P m + -chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b))) * 1 + P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b)) * _; lib_int), sumK_add, ← mul_sumK, ← mul_sumK, sum_one]
  have hcnt : sumK LibInt.intR (fun z => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul z z)) d) (L m) = 1 + chi m ((ring (2 * m + 1 + 1)).mul c d) := by
    rw [sumK_ext LibInt.intR _ _ (fun z => delta_scale m hpr c _ d hc), QuadDiagonal.sq_count m hpr, chi_mul m hpr, chi_iv m hpr c hc, ← chi_mul m hpr]
  rw [hcnt]
  rw [show (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) ((ring (2 * m + 1 + 1)).mul c d)) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b)) ((ring (2 * m + 1 + 1)).mul c d) by lib_ring, chi_mul m hpr ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b)) ((ring (2 * m + 1 + 1)).mul c d)]
  show (P m + -chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b))) * P m + P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b)) * (1 + chi m ((ring (2 * m + 1 + 1)).mul c d)) = _
  lib_int

/-- **#{(x, y, z, w) : ax² + by² + cz² + dw² = e} = p³ − pχ(abcd)** for a, b, c, d, e ≠ 0. -/
theorem quaternary_form (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b c d e : Zp (2 * m + 1 + 1)) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) (hc : c ≠ (ring (2 * m + 1 + 1)).zero)
    (hd : d ≠ (ring (2 * m + 1 + 1)).zero) (he : e ≠ (ring (2 * m + 1 + 1)).zero) :
    sumK LibInt.intR (fun w => sumK LibInt.intR (fun z => sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y))) ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul  -- (+102 chars; full line in the Zenodo source)
      = P m * P m * P m + -(P m * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) ((ring (2 * m + 1 + 1)).mul c d))) := by
  have hw : ∀ w, sumK LibInt.intR (fun z => sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y))) ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul z z))) ((ring  -- (+81 chars; full line in the Zenodo source)
      = P m * P m + P m * (chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) c)) * chi m ((ring (2 * m + 1 + 1)).add e ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul d ((ring (2 * m + 1 + 1)).mul w w))))) := fun w => by
    have hpt : ∀ z x y, delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y))) ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul z z))) ((ring (2 * m + 1 + 1)).mul d ((ring (2 * m + 1 + 1)).mul w w))) e = delta LibIn  -- (+399 chars; full line in the Zenodo source)
      rw [← delta_sub m _ e, ← delta_sub m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y))) ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul z z))) ((ring (2 * m + 1 + 1)).add e ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul d ((ring (2 * m + 1 +  -- (+423 chars; full line in the Zenodo source)
        = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y))) ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul z z))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add e ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul  -- (+52 chars; full line in the Zenodo source)
    rw [sumK_ext LibInt.intR _ _ (fun z => sumK_ext LibInt.intR _ _ (fun x => sumK_ext LibInt.intR _ _ (hpt z x) _) _), ternary_form m hpr a b c _ ha hb hc,
      show (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).add e ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul d ((ring (2 * m + 1 + 1)).mul w w)))))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) c)) ((ring (2 * m + 1 + 1))  -- (+221 chars; full line in the Zenodo source)
  rw [sumK_ext LibInt.intR _ (fun w => LibInt.intR.add (LibInt.intR.mul (P m * P m) 1) (LibInt.intR.mul (P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) c))) (chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg d) ((ring (2 * m + 1 + 1)).mul w w)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring  -- (+45 chars; full line in the Zenodo source)
      rw [hw w, show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg d) ((ring (2 * m + 1 + 1)).mul w w)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero w) e) = (ring (2 * m + 1 + 1)).add e ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul d ((ring (2 * m + 1 + 1)).mul w w))) by lib_ring]
      show _ = P m * P m * 1 + P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) c)) * chi m ((ring (2 * m + 1 + 1)).add e ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul d ((ring (2 * m + 1 + 1)).mul w w)))); lib_int),
    sumK_add, ← mul_sumK, ← mul_sumK, sum_one,
    quad_sum_a m hpr _ _ _ (negnz m _ hd) (by
      rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)  -- (+311 chars; full line in the Zenodo source)
      exact nzm m hpr _ _ (nzm m hpr _ _ (QuadDiagonal.two_ne m) (QuadDiagonal.two_ne m)) (nzm m hpr _ _ hd he))]
  rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) ((ring (2 * m + 1 + 1)).mul c d) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) c)) ((ring (2 * m + 1 + 1)).neg d) by lib_ring, chi_mul m hpr ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) c)) ((ring (2 * m + 1 + 1))  -- (+8 chars; full line in the Zenodo source)
  show P m * P m * P m + P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) c)) * -chi m ((ring (2 * m + 1 + 1)).neg d) = _
  lib_int

/-- **#{(x, y, z, w) : ax² + by² + cz² + dw² = 0} = p³ + (p² − p)χ(abcd)** for a, b, c, d ≠ 0. -/
theorem quaternary_form0 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b c d : Zp (2 * m + 1 + 1)) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) (hc : c ≠ (ring (2 * m + 1 + 1)).zero)
    (hd : d ≠ (ring (2 * m + 1 + 1)).zero) :
    sumK LibInt.intR (fun w => sumK LibInt.intR (fun z => sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y))) ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul  -- (+128 chars; full line in the Zenodo source)
      = P m * P m * P m + (P m * P m + -(P m)) * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) ((ring (2 * m + 1 + 1)).mul c d)) := by
  have hw : ∀ w, sumK LibInt.intR (fun z => sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y))) ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul z z))) ((ring  -- (+107 chars; full line in the Zenodo source)
      = P m * P m + P m * (chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) c)) * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul d ((ring (2 * m + 1 + 1)).mul w w)))) := fun w => by
    have hpt : ∀ z x y, delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y))) ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul z z))) ((ring (2 * m + 1 + 1)).mul d ((ring (2 * m + 1 + 1)).mul w w))) (ring (2 * m +   -- (+394 chars; full line in the Zenodo source)
      rw [← delta_sub m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y))) ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul z z))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul d ((ring (2 * m + 1 + 1)).mul w w))), show (ring (2 * m + 1 + 1)).add   -- (+727 chars; full line in the Zenodo source)
    rw [sumK_ext LibInt.intR _ _ (fun z => sumK_ext LibInt.intR _ _ (fun x => sumK_ext LibInt.intR _ _ (hpt z x) _) _), ternary_form m hpr a b c _ ha hb hc,
      show (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul d ((ring (2 * m + 1 + 1)).mul w w))))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) c)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mu  -- (+159 chars; full line in the Zenodo source)
  rw [sumK_ext LibInt.intR _ (fun w => LibInt.intR.add (LibInt.intR.mul (P m * P m) 1) (LibInt.intR.mul (P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) c))) (chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg d) ((ring (2 * m + 1 + 1)).mul w w)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring  -- (+71 chars; full line in the Zenodo source)
      rw [hw w, show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg d) ((ring (2 * m + 1 + 1)).mul w w)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero w) (ring (2 * m + 1 + 1)).zero) = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul d ((ring (2 * m + 1 + 1)).mul w w)) by lib_ring]
      show _ = P m * P m * 1 + P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) c)) * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul d ((ring (2 * m + 1 + 1)).mul w w))); lib_int),
    sumK_add, ← mul_sumK, ← mul_sumK, sum_one, quad_sum_a0 m hpr _ _ _ (negnz m _ hd) (by lib_ring)]
  rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) ((ring (2 * m + 1 + 1)).mul c d) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) c)) ((ring (2 * m + 1 + 1)).neg d) by lib_ring, chi_mul m hpr ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) c)) ((ring (2 * m + 1 + 1))  -- (+8 chars; full line in the Zenodo source)
  show P m * P m * P m + P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) c)) * ((P m + -1) * chi m ((ring (2 * m + 1 + 1)).neg d)) = _
  lib_int

end PencilClosed

#print axioms PencilClosed.nzm
#print axioms PencilClosed.negnz
#print axioms PencilClosed.chi_unit
#print axioms PencilClosed.chi_iv
#print axioms PencilClosed.sum_y2c
#print axioms PencilClosed.delta_sub
#print axioms PencilClosed.iv_nz
#print axioms PencilClosed.quad_count
#print axioms PencilClosed.conic_count
#print axioms PencilClosed.diag_count
#print axioms PencilClosed.split
#print axioms PencilClosed.inner_eq
#print axioms PencilClosed.N_closed
#print axioms PencilClosed.pencil_closed
#print axioms PencilClosed.cube_pw
#print axioms PencilClosed.pencil_zero
#print axioms PencilClosed.pencil_zero_all
#print axioms PencilClosed.quad_shift
#print axioms PencilClosed.disc4
#print axioms PencilClosed.quad_sum
#print axioms PencilClosed.quad_sum0
#print axioms PencilClosed.conic_points
#print axioms PencilClosed.conic_points0
#print axioms PencilClosed.scale_q
#print axioms PencilClosed.disc_scale
#print axioms PencilClosed.quad_sum_a
#print axioms PencilClosed.quad_sum_a0
#print axioms PencilClosed.conic_points_a
#print axioms PencilClosed.delta_scale
#print axioms PencilClosed.binary_form
#print axioms PencilClosed.binary_form0
#print axioms PencilClosed.ternary_form
#print axioms PencilClosed.quaternary_form
#print axioms PencilClosed.quaternary_form0

lang/PencilMoment.lean

lean_all/lang/PencilMoment.lean

/-
  lang/PencilMoment — T43, cycle 296: the second moment of the traces of the pencil b ↦ y² = x³ + ax + b, every odd p.

  p = 2m + 3, W(a, b) = Σ_x χ(x³ + ax + b) (QuadTwist), u(x) = x³ + ax.
    sq_expand       W(a, b)² = Σ_x Σ_y χ((b + u(x))(b + u(y)))
    pencil_moment   Σ_b W(a, b)² = −p² + p · #{(x, y) : u(x) = u(y)}
    inner_a         Σ_a δ(x³ + ax, y³ + ay) = 1 + (p − 1)δ(x, y)
    birch_moment2   Σ_a Σ_b W(a, b)² = p³ − p²                        (Birch's second moment)
    birch_moment3   Σ_a Σ_b W(a, b)³ = 0                              ((a, b) ↦ (t²a, t³b), χ(t) = −1)
    birch_odd       Σ_a Σ_b W(a, b)^(2k+1) = 0 for every k
    sum_a           Σ_a W(a, b) = p χ(b)
    pencil_cov      Σ_b W(a, b) W(a', b) = −p² + p · #{(x, y) : x³ + ax = y³ + a'y}
    sum_a_sq        Σ_b (Σ_a W(a, b))² = p²(p − 1)
  (JacobMoment.inner: Σ_b χ((b + s)(b + t)) = −1 + p δ(s, t).) With sum_b (Σ_b W = 0) this is the variance of a_p over the pencil.
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.QuadTwist
import lang.JacobMoment
import lang.JacobiNorm

namespace PencilMoment

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum Jacobsthal QuadTwist JacobMoment

variable (m : Nat)

def u (a x : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul a x)

theorem W_eq (a b : Zp (2 * m + 1 + 1)) : W m a b = sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).add b (u m a x))) (L m) :=
  sumK_ext LibInt.intR _ _ (fun x => by unfold u; rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a x) b)
    = (ring (2 * m + 1 + 1)).add b ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul a x)) by lib_ring]) _

theorem sq_expand (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b : Zp (2 * m + 1 + 1)) :
    W m a b * W m a b = sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add b (u m a x)) ((ring (2 * m + 1 + 1)).add b (u m a y)))) (L m)) (L m) := by
  rw [W_eq]
  show LibInt.intR.mul _ _ = _
  rw [sumK_mul]
  exact sumK_ext LibInt.intR _ _ (fun x => by
    rw [mul_sumK]
    exact sumK_ext LibInt.intR _ _ (fun y => (chi_mul m hpr _ _).symm) _) _

theorem sum_lin (c s : Int) (f : Zp (2 * m + 1 + 1) → Int) :
    sumK LibInt.intR (fun y => c + s * f y) (L m) = c * P m + s * sumK LibInt.intR f (L m) := by
  rw [sumK_ext LibInt.intR _ (fun y => LibInt.intR.add (LibInt.intR.mul c 1) (LibInt.intR.mul s (f y))) (fun y => by show _ = c * 1 + s * f y; lib_int),
    sumK_add, ← mul_sumK, ← mul_sumK, sum_one]
  rfl

/-- **Second moment over the pencil**: Σ_b W(a, b)² = −p² + p · #{(x, y) : x³ + ax = y³ + ay}. -/
theorem pencil_moment (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a : Zp (2 * m + 1 + 1)) :
    sumK LibInt.intR (fun b => W m a b * W m a b) (L m)
      = -(P m * P m) + P m * sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR (u m a x) (u m a y)) (L m)) (L m) := by
  rw [sumK_ext LibInt.intR _ _ (sq_expand m hpr a)]
  rw [← sumK_swap LibInt.intR (fun x b => sumK LibInt.intR (fun y => chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add b (u m a x)) ((ring (2 * m + 1 + 1)).add b (u m a y)))) (L m))]
  rw [sumK_ext LibInt.intR _ (fun x => sumK LibInt.intR (fun y => -1 + P m * delta LibInt.intR (u m a x) (u m a y)) (L m)) (fun x => by
    rw [← sumK_swap LibInt.intR (fun y b => chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add b (u m a x)) ((ring (2 * m + 1 + 1)).add b (u m a y))))]
    exact sumK_ext LibInt.intR _ _ (fun y => inner m hpr _ _) _)]
  rw [sumK_ext LibInt.intR _ _ (fun x => sum_lin m (-1) (P m) _), sum_lin]
  lib_int

theorem nz2 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1)) (h : (ring (2 * m + 1 + 1)).mul x y = (ring (2 * m + 1 + 1)).zero) : x = (ring (2 * m + 1 + 1)).zero ∨ y = (ring (2 * m + 1 + 1)).zero :=
  nozd (2 * m + 1) hpr _ _ h

/-- For x ≠ y, x³ + ax = y³ + ay exactly when a = −(x² + xy + y²). -/
theorem inner_a (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1)) :
    sumK LibInt.intR (fun a => delta LibInt.intR (u m a x) (u m a y)) (L m) = 1 + (P m + -1) * delta LibInt.intR y x := by
  by_cases hxy : y = x
  · rw [hxy, sumK_ext LibInt.intR _ (fun _ => (1 : Int)) (fun a => delta_one m _ _ rfl), sum_one, delta_one m _ _ rfl]; lib_int
  · rw [delta_zero m _ _ hxy]
    have hs : ∀ a, delta LibInt.intR (u m a x) (u m a y) = delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x y) ((ring (2 * m + 1 + 1)).mul y y)))) (ring (2 * m + 1 + 1)).zero := fun a => by
      by_cases ha : (ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x y) ((ring (2 * m + 1 + 1)).mul y y))) = (ring (2 * m + 1 + 1)).zero
      · rw [delta_one m _ _ ha, delta_one m _ _ (by
          unfold u
          calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul a x)
              = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul y y)) ((ring (2 * m + 1 + 1)).mul a y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg y)) ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)  -- (+62 chars; full line in the Zenodo source)
            _ = _ := by rw [ha]; lib_ring)]
      · rw [delta_zero m _ _ ha, delta_zero m _ _ (fun e => by
          unfold u at e
          have e2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg y)) ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x y) ((ring (2 * m + 1 + 1)).mul y y)))) = (ring (2 * m + 1 + 1)).zero := by
            calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul a x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul y y)) ((ring (2 * m + 1 + 1)).mul a y))) := by lib_ring
              _ = (ring (2 * m + 1 + 1)).zero := by rw [e]; lib_ring
          cases nz2 m hpr _ _ e2 with
          | inl h => exact hxy (by
              calc y = (ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg y)) := by rw [h]; lib_ring
                _ = x := by lib_ring)
          | inr h => exact ha h)]
    rw [sumK_ext LibInt.intR _ _ hs, sum_delta_shift]; lib_int

/-- **Birch's second moment**: Σ_a Σ_b (Σ_x χ(x³ + ax + b))² = p³ − p², every odd p. -/
theorem birch_moment2 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) :
    sumK LibInt.intR (fun a => sumK LibInt.intR (fun b => W m a b * W m a b) (L m)) (L m) = P m * P m * P m + -(P m * P m) := by
  rw [sumK_ext LibInt.intR _ _ (fun a => pencil_moment m hpr a)]
  rw [show (fun a => -(P m * P m) + P m * sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR (u m a x) (u m a y)) (L m)) (L m))
      = (fun a => -(P m * P m) + P m * sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR (u m a x) (u m a y)) (L m)) (L m)) from rfl,
    sum_lin]
  rw [sumK_swap LibInt.intR (fun a x => sumK LibInt.intR (fun y => delta LibInt.intR (u m a x) (u m a y)) (L m))]
  rw [sumK_ext LibInt.intR _ (fun x => sumK LibInt.intR (fun y => 1 + (P m + -1) * delta LibInt.intR y x) (L m)) (fun x => by
    rw [sumK_swap LibInt.intR (fun a y => delta LibInt.intR (u m a x) (u m a y))]
    exact sumK_ext LibInt.intR _ _ (fun y => inner_a m hpr x y) _)]
  rw [sumK_ext LibInt.intR _ (fun _ => 1 * P m + (P m + -1) * 1) (fun x => by rw [sum_lin, show sumK LibInt.intR (fun y => delta LibInt.intR y x) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR x])]
  rw [sumK_ext LibInt.intR _ (fun _ => LibInt.intR.mul (1 * P m + (P m + -1) * 1) 1) (fun _ => by show (1 * P m + (P m + -1) * 1 : Int) = (1 * P m + (P m + -1) * 1) * 1; lib_int), ← mul_sumK, sum_one]
  show -(P m * P m) * P m + P m * ((1 * P m + (P m + -1) * 1) * P m) = _
  lib_int

/-- **Birch's third moment vanishes**: Σ_a Σ_b (Σ_x χ(x³ + ax + b))³ = 0 (twist by a non-residue). -/
theorem birch_moment3 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) :
    sumK LibInt.intR (fun a => sumK LibInt.intR (fun b => W m a b * W m a b * W m a b) (L m)) (L m) = 0 := by
  match exists_nonres m hpr with
  | ⟨t, ht0, ht⟩ =>
    have nz : ∀ x y : Zp (2 * m + 1 + 1), x ≠ (ring (2 * m + 1 + 1)).zero → y ≠ (ring (2 * m + 1 + 1)).zero → (ring (2 * m + 1 + 1)).mul x y ≠ (ring (2 * m + 1 + 1)).zero := fun x y hx hy e => by
      cases nz2 m hpr _ _ e with
      | inl h => exact hx h
      | inr h => exact hy h
    have h2 := nz _ _ ht0 ht0
    have h3 := nz _ _ ht0 h2
    have hneg : sumK LibInt.intR (fun a => sumK LibInt.intR (fun b => W m a b * W m a b * W m a b) (L m)) (L m)
        = LibInt.intR.mul (-1) (sumK LibInt.intR (fun a => sumK LibInt.intR (fun b => W m a b * W m a b * W m a b) (L m)) (L m)) := by
      calc _ = sumK LibInt.intR (fun a => sumK LibInt.intR (fun b => W m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t t) a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)).mul t t)) b) * W m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t t) a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)).mul t t)) b) * W  -- (+181 chars; full line in the Zenodo source)
            rw [← reindex_mul m hpr ((ring (2 * m + 1 + 1)).mul t t) h2 (fun a => sumK LibInt.intR (fun b => W m a b * W m a b * W m a b) (L m))]
            exact sumK_ext LibInt.intR _ _ (fun a => (reindex_mul m hpr ((ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)).mul t t)) h3
              (fun b => W m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t t) a) b * W m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t t) a) b * W m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t t) a) b)).symm) _
        _ = _ := by
          rw [mul_sumK]
          exact sumK_ext LibInt.intR _ _ (fun a => by
            rw [mul_sumK]
            exact sumK_ext LibInt.intR _ _ (fun b => by
              rw [quad_twist m hpr a b t ht0, ht]
              show (-1 * W m a b) * (-1 * W m a b) * (-1 * W m a b) = -1 * (W m a b * W m a b * W m a b)
              lib_int) _) _
    exact JacobiNorm.int_cancel _ _ 1 (by
      have e : ∀ X : Int, X = LibInt.intR.mul (-1) X → X * Int.ofNat (1 + 1) = 0 * Int.ofNat (1 + 1) := fun X hX => by
        calc X * Int.ofNat (1 + 1) = X + X := by show X * 2 = X + X; lib_int
          _ = X + -1 * X := by
            have : X = -1 * X := hX
            rw [← this]
          _ = 0 * Int.ofNat (1 + 1) := by show X + -1 * X = 0 * 2; lib_int
      exact e _ hneg)

def ipow (x : Int) : Nat → Int
  | 0 => 1
  | k + 1 => x * ipow x k

/-- **Every odd Birch moment vanishes**: Σ_a Σ_b (Σ_x χ(x³ + ax + b))^(2k+1) = 0. -/
theorem birch_odd (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (k : Nat) :
    sumK LibInt.intR (fun a => sumK LibInt.intR (fun b => W m a b * ipow (W m a b * W m a b) k) (L m)) (L m) = 0 := by
  match exists_nonres m hpr with
  | ⟨t, ht0, ht⟩ =>
    have nz : ∀ x y : Zp (2 * m + 1 + 1), x ≠ (ring (2 * m + 1 + 1)).zero → y ≠ (ring (2 * m + 1 + 1)).zero → (ring (2 * m + 1 + 1)).mul x y ≠ (ring (2 * m + 1 + 1)).zero := fun x y hx hy e => by
      cases nz2 m hpr _ _ e with
      | inl h => exact hx h
      | inr h => exact hy h
    have h2 := nz _ _ ht0 ht0
    have h3 := nz _ _ ht0 h2
    have hneg : sumK LibInt.intR (fun a => sumK LibInt.intR (fun b => W m a b * ipow (W m a b * W m a b) k) (L m)) (L m)
        = LibInt.intR.mul (-1) (sumK LibInt.intR (fun a => sumK LibInt.intR (fun b => W m a b * ipow (W m a b * W m a b) k) (L m)) (L m)) := by
      calc _ = sumK LibInt.intR (fun a => sumK LibInt.intR (fun b => W m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t t) a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)).mul t t)) b) * ipow (W m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t t) a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)).mul t t))   -- (+190 chars; full line in the Zenodo source)
            rw [← reindex_mul m hpr ((ring (2 * m + 1 + 1)).mul t t) h2 (fun a => sumK LibInt.intR (fun b => W m a b * ipow (W m a b * W m a b) k) (L m))]
            exact sumK_ext LibInt.intR _ _ (fun a => (reindex_mul m hpr ((ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)).mul t t)) h3
              (fun b => W m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t t) a) b * ipow (W m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t t) a) b * W m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t t) a) b) k)).symm) _
        _ = _ := by
          rw [mul_sumK]
          exact sumK_ext LibInt.intR _ _ (fun a => by
            rw [mul_sumK]
            exact sumK_ext LibInt.intR _ _ (fun b => by
              rw [quad_twist m hpr a b t ht0, ht]
              rw [show (-1 * W m a b) * (-1 * W m a b) = W m a b * W m a b by lib_int]
              show -1 * W m a b * ipow (W m a b * W m a b) k = -1 * (W m a b * ipow (W m a b * W m a b) k)
              lib_int) _) _
    exact JacobiNorm.int_cancel _ _ 1 (by
      have e : ∀ X : Int, X = LibInt.intR.mul (-1) X → X * Int.ofNat (1 + 1) = 0 * Int.ofNat (1 + 1) := fun X hX => by
        calc X * Int.ofNat (1 + 1) = X + X := by show X * 2 = X + X; lib_int
          _ = X + -1 * X := by
            have : X = -1 * X := hX
            rw [← this]
          _ = 0 * Int.ofNat (1 + 1) := by show X + -1 * X = 0 * 2; lib_int
      exact e _ hneg)

/-- **Summing over a**: Σ_a Σ_x χ(x³ + ax + b) = p χ(b) (only x = 0 survives). -/
theorem sum_a (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (b : Zp (2 * m + 1 + 1)) : sumK LibInt.intR (fun a => W m a b) (L m) = P m * chi m b := by
  unfold W
  rw [sumK_swap LibInt.intR (fun a x => chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a x) b)))]
  have h : ∀ x, sumK LibInt.intR (fun a => chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a x) b))) (L m)
      = LibInt.intR.mul (P m * chi m b) (delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) := fun x => by
    by_cases hx : x = (ring (2 * m + 1 + 1)).zero
    · rw [hx, delta_one m _ _ rfl]
      rw [sumK_ext LibInt.intR _ (fun _ => LibInt.intR.mul (chi m b) 1) (fun a => by
        rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a (ring (2 * m + 1 + 1)).zero) b) = b by lib_ring]
        show _ = chi m b * 1; lib_int), ← mul_sumK, sum_one]
      show chi m b * P m = P m * chi m b * 1; lib_int
    · rw [delta_zero m _ _ hx]
      rw [sumK_ext LibInt.intR _ (fun a => chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x a) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) b))) (fun a => by
        rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a x) b) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x a) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) b) by lib_ring]),
        reindex_mul m hpr x hx (fun y => chi m ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) b))),
        shift_sum m (chi m), show sumK LibInt.intR (chi m) (L m) = 0 from chi_sum m hpr]
      show (0 : Int) = P m * chi m b * 0; lib_int
  rw [sumK_ext LibInt.intR _ _ h, ← mul_sumK, show sumK LibInt.intR (fun x => delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR (ring (2 * m + 1 + 1)).zero]
  show P m * chi m b * 1 = _; lib_int

theorem cov_expand (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a a' b : Zp (2 * m + 1 + 1)) :
    W m a b * W m a' b = sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add b (u m a x)) ((ring (2 * m + 1 + 1)).add b (u m a' y)))) (L m)) (L m) := by
  rw [W_eq, W_eq]
  show LibInt.intR.mul _ _ = _
  rw [sumK_mul]
  exact sumK_ext LibInt.intR _ _ (fun x => by
    rw [mul_sumK]
    exact sumK_ext LibInt.intR _ _ (fun y => (chi_mul m hpr _ _).symm) _) _

/-- **Covariance over the pencil**: Σ_b W(a, b) W(a', b) = −p² + p · #{(x, y) : x³ + ax = y³ + a'y}. -/
theorem pencil_cov (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a a' : Zp (2 * m + 1 + 1)) :
    sumK LibInt.intR (fun b => W m a b * W m a' b) (L m)
      = -(P m * P m) + P m * sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR (u m a x) (u m a' y)) (L m)) (L m) := by
  rw [sumK_ext LibInt.intR _ _ (cov_expand m hpr a a')]
  rw [← sumK_swap LibInt.intR (fun x b => sumK LibInt.intR (fun y => chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add b (u m a x)) ((ring (2 * m + 1 + 1)).add b (u m a' y)))) (L m))]
  rw [sumK_ext LibInt.intR _ (fun x => sumK LibInt.intR (fun y => -1 + P m * delta LibInt.intR (u m a x) (u m a' y)) (L m)) (fun x => by
    rw [← sumK_swap LibInt.intR (fun y b => chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add b (u m a x)) ((ring (2 * m + 1 + 1)).add b (u m a' y))))]
    exact sumK_ext LibInt.intR _ _ (fun y => inner m hpr _ _) _)]
  rw [sumK_ext LibInt.intR _ _ (fun x => sum_lin m (-1) (P m) _), sum_lin]
  lib_int

/-- **Σ_b (Σ_a W(a, b))² = p²(p − 1).** -/
theorem sum_a_sq (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) :
    sumK LibInt.intR (fun b => sumK LibInt.intR (fun a => W m a b) (L m) * sumK LibInt.intR (fun a => W m a b) (L m)) (L m) = P m * P m * P m + -(P m * P m) := by
  rw [sumK_ext LibInt.intR _ (fun b => -(P m * P m) * delta LibInt.intR b (ring (2 * m + 1 + 1)).zero + P m * P m) (fun b => by
    rw [sum_a m hpr b]
    show P m * chi m b * (P m * chi m b) = _
    rw [show P m * chi m b * (P m * chi m b) = P m * P m * (chi m b * chi m b) by lib_int, ← chi_mul m hpr, sq_delta m hpr]
    show P m * P m * (1 + -1 * delta LibInt.intR b (ring (2 * m + 1 + 1)).zero) = -(P m * P m) * delta LibInt.intR b (ring (2 * m + 1 + 1)).zero + P m * P m
    lib_int)]
  rw [sumK_ext LibInt.intR _ (fun b => LibInt.intR.add (LibInt.intR.mul (-(P m * P m)) (delta LibInt.intR b (ring (2 * m + 1 + 1)).zero)) (LibInt.intR.mul (P m * P m) 1)) (fun b => by
    show _ = -(P m * P m) * delta LibInt.intR b (ring (2 * m + 1 + 1)).zero + P m * P m * 1; lib_int), sumK_add, ← mul_sumK, ← mul_sumK, sum_one,
    show sumK LibInt.intR (fun b => delta LibInt.intR b (ring (2 * m + 1 + 1)).zero) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR (ring (2 * m + 1 + 1)).zero]
  show -(P m * P m) * 1 + P m * P m * P m = _
  lib_int

end PencilMoment

#print axioms PencilMoment.W_eq
#print axioms PencilMoment.sq_expand
#print axioms PencilMoment.sum_lin
#print axioms PencilMoment.pencil_moment
#print axioms PencilMoment.nz2
#print axioms PencilMoment.inner_a
#print axioms PencilMoment.birch_moment2
#print axioms PencilMoment.birch_moment3
#print axioms PencilMoment.birch_odd
#print axioms PencilMoment.sum_a
#print axioms PencilMoment.cov_expand
#print axioms PencilMoment.pencil_cov
#print axioms PencilMoment.sum_a_sq

lang/Pent.lean

lean_all/lang/Pent.lean

/-
  lang/Pent — T47, cycle 571: **Euler's pentagonal theorem at every degree**: in every commutative ring with t^M = 0,
    Σ_{m ≤ 4M} (−1)^m t^(2·Ta(2M, m) + Tb(2M, m)) = (t; t)_M,
  where for j = m − 2M the exponent 2Ta + Tb = j(j+1) + j(j−1)/2 = (3j² + j)/2, i.e. ∏(1 − tⁿ) = Σ_j (−1)^j t^(j(3j+1)/2).

    pq3, od3, pp_split3     (t; t)_(3T) = (t³; t³)_T · ∏_{k<T} (1 − t·t^(3k))(1 − t²·t^(3k))
    pr_od                   the x = −1 product of PentL with Q = t³, a = t², b = t
    pent
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.PentL
import lang.JacobiP

namespace QBinom

open FTAChain

variable {R : Type} (K : DetChain.CRing R) (t : R)

theorem pq3 (T : Nat) : Frobenius.pw K (K.mul (K.mul t t) t) T = Frobenius.pw K t (3 * T) := by
  rw [Frobenius.pw_mul, Frobenius.pw_mul, pw_sum K t T T (T + T) rfl, pw_sum K t (T + T) T (3 * T) (by lib_nat)]

def od3 : Nat → R
  | 0 => K.one
  | T + 1 => K.mul (od3 T) (K.mul (K.add K.one (K.neg (K.mul t (Frobenius.pw K (K.mul (K.mul t t) t) T))))
      (K.add K.one (K.neg (K.mul (K.mul t t) (Frobenius.pw K (K.mul (K.mul t t) t) T)))))

theorem pp_split3 : ∀ T, pp K t (3 * T) = K.mul (pp K (K.mul (K.mul t t) t) T) (od3 K t T)
  | 0 => by show K.one = K.mul K.one K.one; lib_ring
  | T + 1 => by
    rw [show 3 * (T + 1) = 3 * T + 1 + 1 + 1 by lib_nat]
    show K.mul (K.mul (K.mul (pp K t (3 * T)) (K.add K.one (K.neg (Frobenius.pw K t (3 * T + 1)))))
        (K.add K.one (K.neg (Frobenius.pw K t (3 * T + 1 + 1))))) (K.add K.one (K.neg (Frobenius.pw K t (3 * T + 1 + 1 + 1)))) =
      K.mul (K.mul (pp K (K.mul (K.mul t t) t) T) (K.add K.one (K.neg (Frobenius.pw K (K.mul (K.mul t t) t) (T + 1)))))
        (K.mul (od3 K t T) (K.mul (K.add K.one (K.neg (K.mul t (Frobenius.pw K (K.mul (K.mul t t) t) T))))
          (K.add K.one (K.neg (K.mul (K.mul t t) (Frobenius.pw K (K.mul (K.mul t t) t) T))))))
    rw [pp_split3 T, show Frobenius.pw K (K.mul (K.mul t t) t) (T + 1) = K.mul (Frobenius.pw K (K.mul (K.mul t t) t) T) (K.mul (K.mul t t) t) from rfl,
      show Frobenius.pw K t (3 * T + 1 + 1 + 1) = K.mul (K.mul (K.mul (Frobenius.pw K t (3 * T)) t) t) t from rfl,
      show Frobenius.pw K t (3 * T + 1 + 1) = K.mul (K.mul (Frobenius.pw K t (3 * T)) t) t from rfl,
      show Frobenius.pw K t (3 * T + 1) = K.mul (Frobenius.pw K t (3 * T)) t from rfl, ← pq3]
    generalize Frobenius.pw K (K.mul (K.mul t t) t) T = P
    lib_ring

theorem pr_od : ∀ N, pr K (K.mul t t) t (K.mul (K.mul t t) t) N = K.mul (Frobenius.pw K (K.neg K.one) N) (od3 K t N)
  | 0 => by show K.one = K.mul K.one K.one; lib_ring
  | N + 1 => by
    show K.mul (pr K (K.mul t t) t (K.mul (K.mul t t) t) N) _ = K.mul (K.mul (Frobenius.pw K (K.neg K.one) N) (K.neg K.one)) (K.mul (od3 K t N) _)
    rw [pr_od N]
    generalize Frobenius.pw K (K.mul (K.mul t t) t) N = P
    lib_ring

/-- **Euler's pentagonal theorem mod t^M.** -/
theorem pent (M : Nat) (ht : Frobenius.pw K t M = K.zero) :
    es K (fun m => Frobenius.pw K t (2 * Ta (2 * M) m + Tb (2 * M) m)) (K.neg K.one) (2 * (2 * M) + 1) = pp K t M := by
  have ha : Frobenius.pw K (K.mul t t) M = K.zero := by
    rw [pwQ]; exact pw_zero_of_le K t M _ ht (Nat.le_trans (Nat.le_add_left M M) (Nat.le_of_eq (by lib_nat)))
  have hg := pent_gen K (K.mul t t) t (K.mul (K.mul t t) t) rfl M ha ht
  have hc := es_congr K (fun m => K.mul (Frobenius.pw K (K.mul t t) (Ta (2 * M) m)) (Frobenius.pw K t (Tb (2 * M) m)))
    (fun m => Frobenius.pw K t (2 * Ta (2 * M) m + Tb (2 * M) m)) (K.neg K.one) (fun m => by rw [pwQ, pw_sum K t _ _ _ rfl]) (2 * (2 * M) + 1)
  rw [← hc, hg, pr_od, pm_even]
  have hQ : Frobenius.pw K (K.mul (K.mul t t) t) M = K.zero := by
    rw [pq3]; exact pw_zero_of_le K t M _ ht (Nat.le_trans (Nat.le_add_left M (2 * M)) (Nat.le_of_eq (by lib_nat)))
  have s1 := pp_split3 K t (2 * M)
  rw [show pp K (K.mul (K.mul t t) t) (2 * M) = pp K (K.mul (K.mul t t) t) M by
      rw [show 2 * M = M + M by lib_nat, pp_stable K _ M hQ M],
    show pp K t (3 * (2 * M)) = pp K t M by rw [show 3 * (2 * M) = M + 5 * M by lib_nat, pp_stable K t M ht]] at s1
  rw [s1]; lib_ring

end QBinom

#print axioms QBinom.pq3
#print axioms QBinom.pp_split3
#print axioms QBinom.pr_od
#print axioms QBinom.pent

lang/PentD.lean

lean_all/lang/PentD.lean

/-
  lang/PentD — T47, cycle 569: the coefficients dg N m = [2N; m]_Q · a^(Ta N m) · b^(Tb N m) (ab = Q) satisfy the three-term recursion of
  multiplying by (1 + x·a·Q^N)(x + b·Q^N) = bQ^N + (1 + Q^(2N+1))x + aQ^N x².

    pwQab          Q^k = a^k b^k
    dg_h0, dg_h1, dg_h2
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.PentE

namespace QBinom

open FTAChain

variable {R : Type} (K : DetChain.CRing R) (a b Q : R) (hab : K.mul a b = Q)

def dg (N m : Nat) : R := K.mul (gb K Q (2 * N) m) (K.mul (Frobenius.pw K a (Ta N m)) (Frobenius.pw K b (Tb N m)))

include hab in
theorem pwQab (k : Nat) : Frobenius.pw K Q k = K.mul (Frobenius.pw K a k) (Frobenius.pw K b k) := by
  rw [← hab]; exact Frobenius.pw_mul K a b k

theorem pw_sum (x : R) (u v w : Nat) (h : u + v = w) : K.mul (Frobenius.pw K x u) (Frobenius.pw K x v) = Frobenius.pw K x w := by
  rw [← HasseCM.pw_add', h]

include hab in
theorem dg_h0 (N : Nat) : dg K a b Q (N + 1) 0 = K.mul (K.mul b (Frobenius.pw K Q N)) (dg K a b Q N 0) := by
  show K.mul (gb K Q (2 * (N + 1)) 0) (K.mul (Frobenius.pw K a (Ta (N + 1) 0)) (Frobenius.pw K b (Tb (N + 1) 0))) =
    K.mul (K.mul b (Frobenius.pw K Q N)) (K.mul (gb K Q (2 * N) 0) (K.mul (Frobenius.pw K a (Ta N 0)) (Frobenius.pw K b (Tb N 0))))
  rw [gb_zero, gb_zero, pwQab K a b Q hab, ta_0, tb_0, HasseCM.pw_add', HasseCM.pw_add', HasseCM.pw_add']
  show K.mul K.one (K.mul (K.mul (Frobenius.pw K a N) (Frobenius.pw K a (Ta N 0)))
    (K.mul (K.mul (Frobenius.pw K b N) (K.mul K.one b)) (Frobenius.pw K b (Tb N 0)))) = _
  lib_ring

include hab in
theorem dg_h1 (N : Nat) : dg K a b Q (N + 1) 1 = K.add (K.mul (K.mul b (Frobenius.pw K Q N)) (dg K a b Q N 1))
    (K.mul (K.add K.one (Frobenius.pw K Q (2 * N + 1))) (dg K a b Q N 0)) := by
  have g1 : gb K Q (2 * (N + 1)) 1 = K.add (K.mul (Frobenius.pw K Q 1)
      (K.add (gb K Q (2 * N) 1) (K.mul (Frobenius.pw K Q (2 * N)) K.one))) K.one := by
    rw [show 2 * (N + 1) = 2 * N + 1 + 1 by lib_nat]
    show gb K Q (2 * N + 1 + 1) (0 + 1) = _
    rw [pascalA, gb_zero]
    show K.add (K.mul (Frobenius.pw K Q (0 + 1)) (K.add (gb K Q (2 * N) (0 + 1))
      (K.mul (Frobenius.pw K Q (2 * N - 0)) (gb K Q (2 * N) 0)))) K.one = _
    rw [gb_zero]; rfl
  show K.mul (gb K Q (2 * (N + 1)) 1) (K.mul (Frobenius.pw K a (Ta (N + 1) (0 + 1))) (Frobenius.pw K b (Tb (N + 1) (0 + 1)))) =
    K.add (K.mul (K.mul b (Frobenius.pw K Q N)) (K.mul (gb K Q (2 * N) 1) (K.mul (Frobenius.pw K a (Ta N 1)) (Frobenius.pw K b (Tb N 1)))))
      (K.mul (K.add K.one (Frobenius.pw K Q (2 * N + 1))) (K.mul (gb K Q (2 * N) 0) (K.mul (Frobenius.pw K a (Ta N 0)) (Frobenius.pw K b (Tb N 0)))))
  rw [g1, gb_zero, ta_shift, tb_shift]
  have ea : K.mul a (Frobenius.pw K a (Ta N 0)) = K.mul (Frobenius.pw K a N) (Frobenius.pw K a (Ta N 1)) := by
    rw [pw_sum K a N (Ta N 1) _ rfl, ← ta_1, Nat.add_comm, HasseCM.pw_add']
    show _ = K.mul (Frobenius.pw K a (Ta N 0)) (K.mul K.one a); lib_ring
  have eb : Frobenius.pw K b (Tb N 0) = K.mul (Frobenius.pw K b N) (Frobenius.pw K b (Tb N 1)) := by
    rw [pw_sum K b N (Tb N 1) _ rfl, ← tb_1]
  have e1 : Frobenius.pw K Q 1 = K.mul a b := by rw [hab]; show K.mul K.one Q = Q; lib_ring
  have e21 : Frobenius.pw K Q (2 * N + 1) = K.mul (Frobenius.pw K Q (2 * N)) Q := rfl
  rw [e1, e21, pwQab K a b Q hab N, eb]
  generalize gb K Q (2 * N) 1 = G
  generalize Frobenius.pw K Q (2 * N) = P2
  rw [← hab]
  calc _ = K.add (K.mul (K.mul b G) (K.mul (K.mul a (Frobenius.pw K a (Ta N 0))) (K.mul (Frobenius.pw K b N) (Frobenius.pw K b (Tb N 1)))))
        (K.mul (K.add K.one (K.mul P2 (K.mul a b))) (K.mul (Frobenius.pw K a (Ta N 0)) (K.mul (Frobenius.pw K b N) (Frobenius.pw K b (Tb N 1))))) := by lib_ring
    _ = _ := by rw [ea]; lib_ring

include hab in
theorem dg_h2 (N m : Nat) : dg K a b Q (N + 1) (m + 2) =
    K.add (K.add (K.mul (K.mul b (Frobenius.pw K Q N)) (dg K a b Q N (m + 2)))
      (K.mul (K.add K.one (Frobenius.pw K Q (2 * N + 1))) (dg K a b Q N (m + 1))))
      (K.mul (K.mul a (Frobenius.pw K Q N)) (dg K a b Q N m)) := by
  show K.mul (gb K Q (2 * (N + 1)) (m + 2)) (K.mul (Frobenius.pw K a (Ta (N + 1) (m + 2))) (Frobenius.pw K b (Tb (N + 1) (m + 2)))) =
    K.add (K.add (K.mul (K.mul b (Frobenius.pw K Q N)) (K.mul (gb K Q (2 * N) (m + 2)) (K.mul (Frobenius.pw K a (Ta N (m + 2))) (Frobenius.pw K b (Tb N (m + 2))))))
      (K.mul (K.add K.one (Frobenius.pw K Q (2 * N + 1))) (K.mul (gb K Q (2 * N) (m + 1)) (K.mul (Frobenius.pw K a (Ta N (m + 1))) (Frobenius.pw K b (Tb N (m + 1)))))))
      (K.mul (K.mul a (Frobenius.pw K Q N)) (K.mul (gb K Q (2 * N) m) (K.mul (Frobenius.pw K a (Ta N m)) (Frobenius.pw K b (Tb N m)))))
  cases Nat.lt_or_ge (2 * N) m with
  | inl hbig =>
    rw [gb_big K _ (2 * (N + 1)) (m + 2) (by rw [show 2 * (N + 1) = 2 * N + 2 by lib_nat]; exact Nat.succ_lt_succ (Nat.succ_lt_succ hbig)),
      gb_big K _ (2 * N) (m + 2) (Nat.lt_trans hbig (Nat.lt_trans (Nat.lt_succ_self m) (Nat.lt_succ_self _))),
      gb_big K _ (2 * N) (m + 1) (Nat.lt_trans hbig (Nat.lt_succ_self m)), gb_big K _ (2 * N) m hbig]
    lib_ring
  | inr hle =>
    rw [show 2 * (N + 1) = 2 * N + 2 by lib_nat, dpascal K _ (2 * N) m hle, show 2 * N + 1 = 2 * N + 1 from rfl,
      pwQab K a b Q hab (m + 2), pwQab K a b Q hab N, pwQab K a b Q hab (2 * N - m)]
    have ta' : Ta N (m + 1) = Ta (N + 1) (m + 2) := (ta_shift N (m + 1)).symm
    have tb' : Tb N (m + 1) = Tb (N + 1) (m + 2) := (tb_shift N (m + 1)).symm
    rw [ta', tb']
    have a1 : K.mul (Frobenius.pw K a N) (Frobenius.pw K a (Ta N (m + 2))) =
        K.mul (Frobenius.pw K a (m + 2)) (Frobenius.pw K a (Ta (N + 1) (m + 2))) := by
      rw [pw_sum K a _ _ _ rfl, pw_sum K a _ _ _ rfl, Nat.add_comm N, ta_a]
    have b1 : K.mul b (K.mul (Frobenius.pw K b N) (Frobenius.pw K b (Tb N (m + 2)))) =
        K.mul (Frobenius.pw K b (m + 2)) (Frobenius.pw K b (Tb (N + 1) (m + 2))) := by
      rw [pw_sum K b _ _ _ rfl, pw_sum K b _ _ _ rfl, ← tb_a, show 1 + Tb N (m + 2) + N = (N + Tb N (m + 2)) + 1 by lib_nat]
      show _ = K.mul (Frobenius.pw K b (N + Tb N (m + 2))) b; lib_ring
    have a3 : K.mul a (K.mul (Frobenius.pw K a N) (Frobenius.pw K a (Ta N m))) =
        K.mul (Frobenius.pw K a (2 * N - m)) (Frobenius.pw K a (Ta (N + 1) (m + 2))) := by
      rw [pw_sum K a _ _ _ rfl, pw_sum K a _ _ _ rfl, ← ta_c N m hle, show 1 + N + Ta N m = (N + Ta N m) + 1 by lib_nat]
      show _ = K.mul (Frobenius.pw K a (N + Ta N m)) a; lib_ring
    have b3 : K.mul (Frobenius.pw K b N) (Frobenius.pw K b (Tb N m)) =
        K.mul (Frobenius.pw K b (2 * N - m)) (Frobenius.pw K b (Tb (N + 1) (m + 2))) := by
      rw [pw_sum K b _ _ _ rfl, pw_sum K b _ _ _ rfl, tb_c N m hle]
    generalize gb K Q (2 * N) (m + 2) = G2
    generalize gb K Q (2 * N) (m + 1) = G1
    generalize gb K Q (2 * N) m = G0
    symm
    calc _ = K.add (K.add (K.mul (K.mul (K.mul (Frobenius.pw K a N) (Frobenius.pw K a (Ta N (m + 2))))
            (K.mul b (K.mul (Frobenius.pw K b N) (Frobenius.pw K b (Tb N (m + 2)))))) G2)
          (K.mul (K.add K.one (Frobenius.pw K Q (2 * N + 1))) (K.mul G1 (K.mul (Frobenius.pw K a (Ta (N + 1) (m + 2))) (Frobenius.pw K b (Tb (N + 1) (m + 2)))))))
          (K.mul (K.mul (K.mul a (K.mul (Frobenius.pw K a N) (Frobenius.pw K a (Ta N m))))
            (K.mul (Frobenius.pw K b N) (Frobenius.pw K b (Tb N m)))) G0) := by lib_ring
      _ = _ := by rw [a1, b1, a3, b3]; lib_ring

end QBinom

#print axioms QBinom.pwQab
#print axioms QBinom.pw_sum
#print axioms QBinom.dg_h0
#print axioms QBinom.dg_h1
#print axioms QBinom.dg_h2

lang/PentE.lean

lean_all/lang/PentE.lean

/-
  lang/PentE — T47, cycle 568: exponent bookkeeping for the two-parameter finite triple product
  ∏_{k=1}^{N} (1 + x·a·Q^(k−1))(x + b·Q^(k−1)),  ab = Q,  coefficient of x^m = [2N; m]_Q · a^(Ta N m) · b^(Tb N m), with j = m − N:
    Ta = j(j+1)/2, Tb = j(j−1)/2 (as functions of j ∈ ℤ, in ℕ without division):
    tr2 k = k(k+1)/2,  Ta N m = tr2 (m − N) + tr2 (N − (m+1)),  Tb N m = tr2 (m − (N+1)) + tr2 (N − m).

    ta_up, ta_dn, tb_up, tb_dn (and the _eq forms), ta_shift, tb_shift
    ta_0, tb_0, ta_1, tb_1                      the boundary identities (m = 0, 1)
    ta_a, tb_a, ta_c, tb_c                      the identities behind the double Pascal step
  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.JacobiS

namespace QBinom

def tr2 : Nat → Nat
  | 0 => 0
  | k + 1 => tr2 k + (k + 1)

theorem tr2_succ (k : Nat) : tr2 (k + 1) = tr2 k + (k + 1) := rfl

def Ta (N m : Nat) : Nat := tr2 (m - N) + tr2 (N - (m + 1))
def Tb (N m : Nat) : Nat := tr2 (m - (N + 1)) + tr2 (N - m)

theorem ta_up (N t : Nat) : Ta N (N + t) = tr2 t := by
  show tr2 (N + t - N) + tr2 (N - (N + t + 1)) = _
  rw [sub_add_l, show N + t + 1 = N + (t + 1) by lib_nat, sub_add_r]; rfl

theorem ta_dn (m t : Nat) : Ta (m + 1 + t) m = tr2 t := by
  show tr2 (m - (m + 1 + t)) + tr2 (m + 1 + t - (m + 1)) = _
  rw [sub_add_l (m + 1) t, show m + 1 + t = m + (1 + t) by lib_nat, sub_add_r]; exact Nat.zero_add _

theorem tb_up (N t : Nat) : Tb N (N + 1 + t) = tr2 t := by
  show tr2 (N + 1 + t - (N + 1)) + tr2 (N - (N + 1 + t)) = _
  rw [sub_add_l (N + 1) t, show N + 1 + t = N + (1 + t) by lib_nat, sub_add_r]; rfl

theorem tb_dn (m t : Nat) : Tb (m + t) m = tr2 t := by
  show tr2 (m - (m + t + 1)) + tr2 (m + t - m) = _
  rw [sub_add_l m t, show m + t + 1 = m + (t + 1) by lib_nat, sub_add_r]; exact Nat.zero_add _

theorem ta_eq_up (N m t : Nat) (h : m = N + t) : Ta N m = tr2 t := by rw [h]; exact ta_up N t
theorem ta_eq_dn (N m t : Nat) (h : N = m + 1 + t) : Ta N m = tr2 t := by rw [h]; exact ta_dn m t
theorem tb_eq_up (N m t : Nat) (h : m = N + 1 + t) : Tb N m = tr2 t := by rw [h]; exact tb_up N t
theorem tb_eq_dn (N m t : Nat) (h : N = m + t) : Tb N m = tr2 t := by rw [h]; exact tb_dn m t

theorem ta_shift (N m : Nat) : Ta (N + 1) (m + 1) = Ta N m := by
  show tr2 (m + 1 - (N + 1)) + tr2 (N + 1 - (m + 1 + 1)) = tr2 (m - N) + tr2 (N - (m + 1))
  rw [LibInt.nsub_succ_succ, LibInt.nsub_succ_succ]

theorem tb_shift (N m : Nat) : Tb (N + 1) (m + 1) = Tb N m := by
  show tr2 (m + 1 - (N + 1 + 1)) + tr2 (N + 1 - (m + 1)) = tr2 (m - (N + 1)) + tr2 (N - m)
  rw [LibInt.nsub_succ_succ, LibInt.nsub_succ_succ]

theorem ta_0 (N : Nat) : Ta (N + 1) 0 = N + Ta N 0 := by
  rw [ta_eq_dn (N + 1) 0 N (by lib_nat)]
  cases N with
  | zero => rfl
  | succ u => rw [ta_eq_dn (u + 1) 0 u (by lib_nat), tr2_succ]; lib_nat

theorem tb_0 (N : Nat) : Tb (N + 1) 0 = N + 1 + Tb N 0 := by
  rw [tb_eq_dn (N + 1) 0 (N + 1) (by lib_nat), tb_eq_dn N 0 N (by lib_nat), tr2_succ]; lib_nat

theorem ta_1 (N : Nat) : 1 + Ta N 0 = N + Ta N 1 := by
  cases N with
  | zero => rfl
  | succ u =>
    cases u with
    | zero => rfl
    | succ v => rw [ta_eq_dn (v + 1 + 1) 0 (v + 1) (by lib_nat), ta_eq_dn (v + 1 + 1) 1 v (by lib_nat), tr2_succ]; lib_nat

theorem tb_1 (N : Nat) : Tb N 0 = N + Tb N 1 := by
  cases N with
  | zero => rfl
  | succ u => rw [tb_eq_dn (u + 1) 0 (u + 1) (by lib_nat), tb_eq_dn (u + 1) 1 u (by lib_nat), tr2_succ]; lib_nat

theorem ta_a (N m : Nat) : Ta N (m + 2) + N = (m + 2) + Ta (N + 1) (m + 2) := by
  cases Nat.lt_or_ge N (m + 2) with
  | inl h =>
    obtain ⟨t, ht⟩ := Cheb.le_dest2 (N + 1) (m + 2) h
    rw [ta_eq_up N (m + 2) (t + 1) (by rw [← ht]; lib_nat), ta_eq_up (N + 1) (m + 2) t (by rw [← ht]; lib_nat), tr2_succ, ← ht]; lib_nat
  | inr h =>
    obtain ⟨t, ht⟩ := Cheb.le_dest2 (m + 2) N h
    cases t with
    | zero =>
      rw [ta_eq_up N (m + 2) 0 (by rw [← ht]; lib_nat), ta_eq_dn (N + 1) (m + 2) 0 (by rw [← ht]; lib_nat), ← ht]; lib_nat
    | succ s =>
      rw [ta_eq_dn N (m + 2) s (by rw [← ht]; lib_nat), ta_eq_dn (N + 1) (m + 2) (s + 1) (by rw [← ht]; lib_nat), tr2_succ, ← ht]; lib_nat

theorem tb_a (N m : Nat) : 1 + Tb N (m + 2) + N = (m + 2) + Tb (N + 1) (m + 2) := by
  cases Nat.lt_or_ge N (m + 2) with
  | inl h =>
    obtain ⟨t, ht⟩ := Cheb.le_dest2 (N + 1) (m + 2) h
    cases t with
    | zero =>
      rw [tb_eq_up N (m + 2) 0 (by rw [← ht]; lib_nat), tb_eq_dn (N + 1) (m + 2) 0 (by rw [← ht]; lib_nat), ← ht]; lib_nat
    | succ s =>
      rw [tb_eq_up N (m + 2) (s + 1) (by rw [← ht]; lib_nat), tb_eq_up (N + 1) (m + 2) s (by rw [← ht]; lib_nat), tr2_succ, ← ht]; lib_nat
  | inr h =>
    obtain ⟨t, ht⟩ := Cheb.le_dest2 (m + 2) N h
    rw [tb_eq_dn N (m + 2) t (by rw [← ht]; lib_nat), tb_eq_dn (N + 1) (m + 2) (t + 1) (by rw [← ht]; lib_nat), tr2_succ, ← ht]; lib_nat

theorem ta_c (N m : Nat) (hm : m ≤ 2 * N) : 1 + N + Ta N m = (2 * N - m) + Ta (N + 1) (m + 2) := by
  obtain ⟨s, hs⟩ := Cheb.le_dest2 m (2 * N) hm
  have h2 : 2 * N - m = s := by rw [← hs]; exact ex_c.sub_add_l' s m
  rw [h2]
  cases Nat.lt_or_ge m N with
  | inl h =>
    obtain ⟨t, ht⟩ := Cheb.le_dest2 (m + 1) N h
    have hs' : s = m + 2 * t + 2 := by
      have := hs; rw [← ht] at this
      exact Cheb.add_rc m _ _ (this.trans (by lib_nat))
    rw [hs', ta_eq_dn N m t (by rw [← ht]; lib_nat)]
    cases t with
    | zero => rw [ta_eq_up (N + 1) (m + 2) 0 (by rw [← ht]; lib_nat), ← ht]; lib_nat
    | succ u => rw [ta_eq_dn (N + 1) (m + 2) u (by rw [← ht]; lib_nat), tr2_succ, ← ht]; lib_nat
  | inr h =>
    obtain ⟨t, ht⟩ := Cheb.le_dest2 N m h
    have hs' : s + t = N := Cheb.add_rc N _ _ ((show s + t + N = s + (t + N) by lib_nat).trans (by rw [ht]; exact hs.trans (by lib_nat)))
    rw [ta_eq_up N m t (by rw [← ht]; lib_nat), ta_eq_up (N + 1) (m + 2) (t + 1) (by rw [← ht]; lib_nat), tr2_succ, ← hs']; lib_nat

theorem tb_c (N m : Nat) (hm : m ≤ 2 * N) : N + Tb N m = (2 * N - m) + Tb (N + 1) (m + 2) := by
  obtain ⟨s, hs⟩ := Cheb.le_dest2 m (2 * N) hm
  have h2 : 2 * N - m = s := by rw [← hs]; exact ex_c.sub_add_l' s m
  rw [h2]
  cases Nat.lt_or_ge m N with
  | inl h =>
    obtain ⟨t, ht⟩ := Cheb.le_dest2 (m + 1) N h
    have hs' : s = m + 2 * t + 2 := by
      have := hs; rw [← ht] at this
      exact Cheb.add_rc m _ _ (this.trans (by lib_nat))
    rw [hs', tb_eq_dn N m (t + 1) (by rw [← ht]; lib_nat), tb_eq_dn (N + 1) (m + 2) t (by rw [← ht]; lib_nat), tr2_succ, ← ht]; lib_nat
  | inr h =>
    obtain ⟨t, ht⟩ := Cheb.le_dest2 N m h
    have hs' : s + t = N := Cheb.add_rc N _ _ ((show s + t + N = s + (t + N) by lib_nat).trans (by rw [ht]; exact hs.trans (by lib_nat)))
    rw [tb_eq_up (N + 1) (m + 2) t (by rw [← ht]; lib_nat)]
    cases t with
    | zero => rw [tb_eq_dn N m 0 (by rw [← ht]; lib_nat), ← hs']; lib_nat
    | succ u => rw [tb_eq_up N m u (by rw [← ht]; lib_nat), tr2_succ, ← hs']; lib_nat

end QBinom

#print axioms QBinom.ta_up
#print axioms QBinom.ta_dn
#print axioms QBinom.tb_up
#print axioms QBinom.tb_dn
#print axioms QBinom.ta_eq_up
#print axioms QBinom.ta_eq_dn
#print axioms QBinom.tb_eq_up
#print axioms QBinom.tb_eq_dn
#print axioms QBinom.ta_shift
#print axioms QBinom.tb_shift
#print axioms QBinom.ta_0
#print axioms QBinom.tb_0
#print axioms QBinom.ta_1
#print axioms QBinom.tb_1
#print axioms QBinom.ta_a
#print axioms QBinom.tb_a
#print axioms QBinom.ta_c
#print axioms QBinom.tb_c

lang/PentL.lean

lean_all/lang/PentL.lean

/-
  lang/PentL — T47, cycle 570: the two-parameter triple product at x = −1 and its nilpotent limit:
  in every commutative ring with ab = Q, a^M = 0 and b^M = 0, for N = 2M:
    Σ_{m ≤ 2N} (−1)^m a^(Ta N m) b^(Tb N m) = (Q; Q)_M · ∏_{k<N} (−(1 − aQ^k)(1 − bQ^k)).
  (With Q = t³, a = t², b = t this is Euler's pentagonal theorem mod t^M; Ta, Tb from PentE, j = m − N, 2Ta + Tb = (3j² + j)/2.)

    dg_big, ev_dg, gp_at        value of the finite product at x = −1
    ta_ge, tb_ge                Ta N m ≥ m − N, Tb N m ≥ N − m
    pent_gen
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.PentD
import lang.JacobiE

namespace QBinom

open FTAChain

variable {R : Type} (K : DetChain.CRing R) (a b Q : R) (hab : K.mul a b = Q)

theorem dg_big (N m : Nat) (h : 2 * N < m) : dg K a b Q N m = K.zero := by
  show K.mul (gb K Q (2 * N) m) _ = K.zero
  rw [gb_big K _ _ _ h]; lib_ring

theorem dg_ext (N : Nat) : ∀ t, dg K a b Q N (2 * N + 1 + t) = K.zero :=
  fun t => dg_big K a b Q N _ (Nat.lt_of_lt_of_le (Nat.lt_succ_self _) (Nat.le_add_right _ t))

def pr : Nat → R
  | 0 => K.one
  | N + 1 => K.mul (pr N) (K.neg (K.mul (K.add K.one (K.neg (K.mul a (Frobenius.pw K Q N)))) (K.add K.one (K.neg (K.mul b (Frobenius.pw K Q N))))))

include hab in
theorem ev_dg : ∀ N, es K (dg K a b Q N) (K.neg K.one) (2 * N + 1) = pr K a b Q N
  | 0 => by
    show K.add K.zero (K.mul (K.mul K.one (K.mul K.one K.one)) K.one) = K.one; lib_ring
  | N + 1 => by
    have hr := es_rec3 K (dg K a b Q N) (dg K a b Q (N + 1)) (K.mul b (Frobenius.pw K Q N)) (K.add K.one (Frobenius.pw K Q (2 * N + 1)))
      (K.mul a (Frobenius.pw K Q N)) (K.neg K.one) (dg_h0 K a b Q hab N) (dg_h1 K a b Q hab N) (dg_h2 K a b Q hab N) (2 * N + 1)
    rw [show 2 * (N + 1) + 1 = 2 * N + 1 + 2 by lib_nat, hr]
    rw [show 2 * N + 1 + 2 = (2 * N + 1) + 2 from rfl, es_ext K _ _ _ (dg_ext K a b Q N) 2, es_ext K _ _ _ (dg_ext K a b Q N) 1, ev_dg N]
    show _ = K.mul (pr K a b Q N) _
    have e : Frobenius.pw K Q (2 * N + 1) = K.mul (K.mul a b) (K.mul (Frobenius.pw K Q N) (Frobenius.pw K Q N)) := by
      rw [hab, pw_sum K Q N N (2 * N) (by lib_nat)]
      show K.mul (Frobenius.pw K Q (2 * N)) Q = _; lib_ring
    rw [e]
    generalize Frobenius.pw K Q N = P
    lib_ring

theorem tr2_ge : ∀ k, k ≤ tr2 k
  | 0 => Nat.le_refl 0
  | _ + 1 => Nat.le_add_left _ _

theorem ta_ge (N m : Nat) : m - N ≤ Ta N m := Nat.le_trans (tr2_ge _) (Nat.le_add_right _ _)
theorem tb_ge (N m : Nat) : N - m ≤ Tb N m := Nat.le_trans (tr2_ge _) (Nat.le_add_left _ _)

include hab in
/-- **The general pentagonal-type identity mod a^M, b^M.** -/
theorem pent_gen (M : Nat) (ha : Frobenius.pw K a M = K.zero) (hb : Frobenius.pw K b M = K.zero) :
    es K (fun m => K.mul (Frobenius.pw K a (Ta (2 * M) m)) (Frobenius.pw K b (Tb (2 * M) m))) (K.neg K.one) (2 * (2 * M) + 1) =
      K.mul (pp K Q M) (pr K a b Q (2 * M)) := by
  have hQ : Frobenius.pw K Q M = K.zero := by rw [pwQab K a b Q hab, ha]; lib_ring
  have hst := pp_stable K Q M hQ
  generalize hP : pp K Q M = P at hst
  have hterm : ∀ m, m < 2 * (2 * M) + 1 → K.mul (K.mul P P) (dg K a b Q (2 * M) m) =
      K.mul P (K.mul (Frobenius.pw K a (Ta (2 * M) m)) (Frobenius.pw K b (Tb (2 * M) m))) := by
    intro m _
    show K.mul (K.mul P P) (K.mul (gb K Q (2 * (2 * M)) m) (K.mul (Frobenius.pw K a (Ta (2 * M) m)) (Frobenius.pw K b (Tb (2 * M) m)))) = _
    cases Nat.lt_or_ge m M with
    | inl h1 =>
      have hz : Frobenius.pw K b (Tb (2 * M) m) = K.zero := by
        apply pw_zero_of_le K b M _ hb
        apply Nat.le_trans _ (tb_ge (2 * M) m)
        obtain ⟨d, hd⟩ := Cheb.le_dest2 (m + 1) M h1
        rw [show 2 * M = m + (d + 1 + M) by rw [← hd]; lib_nat, sub_add_l]
        exact Nat.le_add_left M (d + 1)
      rw [hz]; lib_ring
    | inr h1 =>
      cases Nat.lt_or_ge (3 * M) m with
      | inl h2 =>
        have hz : Frobenius.pw K a (Ta (2 * M) m) = K.zero := by
          apply pw_zero_of_le K a M _ ha
          apply Nat.le_trans _ (ta_ge (2 * M) m)
          obtain ⟨d, hd⟩ := Cheb.le_dest2 (3 * M + 1) m h2
          rw [show m = 2 * M + (d + M + 1) by rw [← hd]; lib_nat, sub_add_l]
          exact Nat.le_trans (Nat.le_add_left M d) (Nat.le_succ _)
        rw [hz]; lib_ring
      | inr h2 =>
        have hm2 : m ≤ 2 * (2 * M) := Nat.le_trans h2 (Nat.le_trans (Nat.le_add_right (3 * M) M) (Nat.le_of_eq (by lib_nat)))
        have hg := gb_pp K Q (2 * (2 * M)) m hm2
        obtain ⟨a', ha'⟩ := Cheb.le_dest2 M m h1
        obtain ⟨c, hc⟩ := Cheb.le_dest2 m (3 * M) h2
        have h4 : 2 * (2 * M) = m + (M + c) := by
          rw [show 2 * (2 * M) = (c + m) + M by rw [hc]; lib_nat]; lib_nat
        have hs : 2 * (2 * M) - m = M + c := by rw [h4, sub_add_l]
        have e1 : pp K Q m = P := by rw [← ha', Nat.add_comm, hst]
        have e2 : pp K Q (2 * (2 * M) - m) = P := by rw [hs, hst]
        have e3 : pp K Q (2 * (2 * M)) = P := by rw [show 2 * (2 * M) = M + 3 * M by lib_nat, hst]
        rw [e1, e2, e3] at hg
        generalize gb K Q (2 * (2 * M)) m = G at hg ⊢
        calc _ = K.mul (K.mul (K.mul G P) P) (K.mul (Frobenius.pw K a (Ta (2 * M) m)) (Frobenius.pw K b (Tb (2 * M) m))) := by lib_ring
          _ = _ := by rw [hg]
  have hes := es_scale K (K.mul P P) (dg K a b Q (2 * M)) _ (K.neg K.one) _ hterm
  have hes2 := es_scale K P (fun m => K.mul (Frobenius.pw K a (Ta (2 * M) m)) (Frobenius.pw K b (Tb (2 * M) m))) _ (K.neg K.one)
    (2 * (2 * M) + 1) (fun m _ => rfl)
  rw [ev_dg K a b Q hab, ← hes2] at hes
  obtain ⟨y, hy⟩ := pp_unit K Q M hQ M
  rw [hP] at hy
  generalize es K (fun m => K.mul (Frobenius.pw K a (Ta (2 * M) m)) (Frobenius.pw K b (Tb (2 * M) m))) (K.neg K.one) (2 * (2 * M) + 1) = S at hes ⊢
  calc S = K.mul (K.mul P y) S := by rw [hy]; lib_ring
    _ = K.mul y (K.mul P S) := by lib_ring
    _ = K.mul y (K.mul (K.mul P P) (pr K a b Q (2 * M))) := by rw [← hes]
    _ = K.mul (K.mul P y) (K.mul P (pr K a b Q (2 * M))) := by lib_ring
    _ = _ := by rw [hy]; lib_ring

end QBinom

#print axioms QBinom.dg_big
#print axioms QBinom.dg_ext
#print axioms QBinom.ev_dg
#print axioms QBinom.tr2_ge
#print axioms QBinom.ta_ge
#print axioms QBinom.tb_ge
#print axioms QBinom.pent_gen

lang/PhiAll.lean

lean_all/lang/PhiAll.lean

/-
  lang/PhiAll — T43, cycle 490: **Dirichlet for 1 mod N: for every N ≥ 1 there are primes p ≡ 1 (mod N) beyond every bound.**

  rad: every N ≥ 1 divides (Π Q)^k for a list Q of distinct primes (strong induction through a bound: N = M·s with s the least
  prime of N, M < N; s is appended to Q unless it already divides Π Q). Then PhiPow.inf_pp with n = Π Q (or Q = [2] for N = 1).
    mul_pow', dv_mul_mul    (ab)^k = a^k b^k;  a | b, c | d ⇒ ac | bd
    rad_bound, rad          ∀ N ≥ 1 ∃ Q k, Good Q ∧ N | (Π Q)^k
    inf_one_mod             ∀ N B ∃ prime p + 1 > B with N + 1 | p
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.PhiPow
import lang.StrongMult1

namespace PhiSq

open FTAChain ZMod Cheb TwoPrime PhiInv InfSplit

theorem mul_pow' (a b : Nat) : ∀ k, (a * b) ^ k = a ^ k * b ^ k
  | 0 => rfl
  | k + 1 => by
    show (a * b) ^ k * (a * b) = a ^ k * a * (b ^ k * b)
    rw [mul_pow' a b k]; generalize a ^ k = P; generalize b ^ k = Q; lib_nat

theorem dv_mul_mul (a b c d : Nat) (h1 : Dv a b) (h2 : Dv c d) : Dv (a * c) (b * d) := by
  obtain ⟨q, hq⟩ := h1; obtain ⟨q', hq'⟩ := h2
  exact ⟨q * q', by rw [hq, hq']; lib_nat⟩

theorem one_pow' : ∀ k, 1 ^ k = 1
  | 0 => rfl
  | k + 1 => (Nat.mul_one _).trans (one_pow' k)

theorem rad_bound : ∀ B N, N + 1 ≤ B → ∃ Q k, Good Q ∧ Dv (N + 1) (pr Q ^ k)
  | 0, _, h => absurd h (Nat.not_succ_le_zero _)
  | B + 1, 0, _ => ⟨[], 0, trivial, ⟨1, rfl⟩⟩
  | B + 1, N'' + 1, hB => by
    obtain ⟨s, hsN, hsmin⟩ := lp_exists N''
    have hspr := lp_prime _ s hsN hsmin
    obtain ⟨M, hM⟩ := hsN
    -- M ≥ 1 and M + 1 ≤ N'' + 2
    have hM1 : 1 ≤ M := by
      match M, hM with
      | 0, hM => rw [Nat.zero_mul] at hM; exact Nat.noConfusion hM
      | M + 1, _ => exact Nat.le_add_left 1 M
    obtain ⟨M', hM'⟩ := le_dest2 1 M hM1
    have hlt : M' + 1 + 1 ≤ B + 1 := by
      have h2 : (M' + 1) * 2 ≤ N'' + 1 + 1 := by
        rw [hM, ← hM']; exact Nat.mul_le_mul_left _ (Nat.le_add_left 2 s)
      have h3 : M' + 1 + 1 ≤ (M' + 1) * 2 := Nat.le_trans (Nat.le_add_right (M' + 1 + 1) M') (Nat.le_of_eq (by lib_nat))
      exact Nat.le_trans h3 (Nat.le_trans h2 hB)
    obtain ⟨Q, k, hQ, hdv⟩ := rad_bound B M' (Nat.le_of_succ_le_succ hlt)
    rw [hM'] at hdv
    rcases dv_dec (s + 1) (pr Q) with hs | hs
    · refine ⟨Q, k + 1, hQ, ?_⟩
      rw [hM]
      exact dv_mul_mul _ _ _ _ hdv hs
    · refine ⟨s :: Q, k + 1, ⟨hspr, hs, hQ⟩, ?_⟩
      rw [hM]
      show Dv (M * (s + 1 + 1)) ((pr Q * (s + 1 + 1)) ^ k * (pr Q * (s + 1 + 1)))
      rw [mul_pow']
      obtain ⟨u, hu⟩ := dv_mul_mul _ _ _ _ hdv (dv_refl (s + 1 + 1))
      refine ⟨u * ((s + 1 + 1) ^ k * pr Q), ?_⟩
      have e : pr Q ^ k * (s + 1 + 1) ^ k * (pr Q * (s + 1 + 1)) = (pr Q ^ k * (s + 1 + 1)) * ((s + 1 + 1) ^ k * pr Q) := by
        generalize pr Q ^ k = P; generalize (s + 1 + 1) ^ k = S; generalize pr Q = R; lib_nat
      rw [e, hu]; generalize (s + 1 + 1) ^ k = S; generalize pr Q = R; lib_nat

theorem rad (N : Nat) : ∃ Q k, Good Q ∧ Dv (N + 1) (pr Q ^ k) := rad_bound (N + 1) N (Nat.le_refl _)

theorem inf_one_mod (N B : Nat) :
    ∃ p, (∀ k, Dv k (p + 1) → k = 1 ∨ k = p + 1) ∧ B < p + 1 ∧ Dv (N + 1) p := by
  obtain ⟨Q, k, hQ, hdv⟩ := rad N
  match Q, hQ, hdv with
  | [], _, hdv =>
    obtain ⟨p, hp, hB, _⟩ := inf_pp 0 [] ⟨StrongMult1.two_prime, fun h => dv_one_absurd 0 h, trivial⟩ 0 B
    have hN : N + 1 = 1 := by
      obtain ⟨q, hq⟩ := hdv
      obtain ⟨c, hc⟩ := dv_le (N + 1) 0 ⟨q, (one_pow' k).symm.trans hq⟩
      match N, c, hc with
      | 0, _, _ => rfl
      | N + 1, c, hc => exact absurd (show N + 1 + 1 + c = 0 + 1 by exact hc) (fun h => two_add_ne_one N c h)
    exact ⟨p, hp, hB, ⟨p, by rw [hN, Nat.mul_one]⟩⟩
  | j :: L, hQ, hdv =>
    obtain ⟨p, hp, hB, hd⟩ := inf_pp j L hQ k B
    exact ⟨p, hp, hB, dv_trans _ _ _ hdv (dv_trans _ _ _ ⟨pr (j :: L), Nat.mul_comm _ _⟩ hd)⟩

end PhiSq

#print axioms PhiSq.mul_pow'
#print axioms PhiSq.dv_mul_mul
#print axioms PhiSq.one_pow'
#print axioms PhiSq.rad_bound
#print axioms PhiSq.rad
#print axioms PhiSq.inf_one_mod

lang/PhiInv.lean

lean_all/lang/PhiInv.lean

/-
  lang/PhiInv — T43, cycle 483: the invariant of the squarefree induction (step I1, the base case).

  Inv(n, y, F) :⇔ for every prime r and k ≥ 1:  r^k | F  ⇔  (y has order n in ℤ/r  ∧  r^k | gN(y, n)).
    dv_base     r^(k+1) | X ⇒ r | X
    ord_prime   q prime, q | y, r | gN(y, q) ⇒ y has order q in ℤ/r
    inv_base    q prime, q | y ⇒ Inv(q, y, gN(y, q))
    step_dv     I2: Inv(n, y, F₀), Inv(n, y^q, F₁), q ∤ n, q | y ⇒ F₀ | F₁
    step_inv    I3: F₁ = F₀·F ⇒ Inv(n·q, y, F)   (n ≥ 2)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.PhiS
import lang.OrdQ
import lang.ValGeo

namespace PhiInv

open FTAChain ZMod Cheb TwoPrime

def Inv (n y F : Nat) : Prop :=
  ∀ l k, (∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1) →
    (Dv ((l + 1 + 1) ^ (k + 1)) F ↔
      (OrdQ.IsOrd (ring (l + 1)) (emb (ring (l + 1)) y) n ∧ Dv ((l + 1 + 1) ^ (k + 1)) (gN y n)))

theorem dv_base (r k X : Nat) (h : Dv (r ^ (k + 1)) X) : Dv r X := dv_trans _ _ _ ⟨r ^ k, rfl⟩ h

theorem ord_prime (j l y : Nat) (hq : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1) (hy : Dv (j + 1 + 1) y)
    (h : Dv (l + 1 + 1) (gN y (j + 1 + 1))) : OrdQ.IsOrd (ring (l + 1)) (emb (ring (l + 1)) y) (j + 1 + 1) := by
  have g1 := emb_dv (l + 1) _ h
  rw [emb_gN] at g1
  have p1 := PhiS.pw_of_geo (ring (l + 1)) _ _ g1
  obtain ⟨d, hd, hmin⟩ := Order.ord_exists (ring (l + 1)) _ (j + 1) p1
  have hO : OrdQ.IsOrd (ring (l + 1)) (emb (ring (l + 1)) y) (d + 1) :=
    ⟨hd, fun f hf => Order.ord_dvd (ring (l + 1)) _ d hd hmin f hf⟩
  rcases hq _ (hO.2 _ p1) with e | e
  · exfalso
    have hd0 : d = 0 := Nat.succ.inj e
    rw [hd0] at hd
    have hz : emb (ring (l + 1)) y = (ring (l + 1)).one := by
      calc emb (ring (l + 1)) y = (ring (l + 1)).mul (ring (l + 1)).one (emb (ring (l + 1)) y) := by lib_ring
        _ = _ := hd
    rw [hz, PhiS.geo_one] at g1
    have hlq := dv_of_emb (l + 1) _ g1
    rcases hq _ hlq with e1 | e1
    · exact Nat.noConfusion (Nat.succ.inj e1)
    · rw [← e1] at hy
      have hz0 := emb_dv (l + 1) y hy
      rw [hz] at hz0
      apply dv_one_absurd l
      apply dv_of_emb (l + 1) 1
      show (ring (l + 1)).add (ring (l + 1)).zero (ring (l + 1)).one = _
      rw [hz0]; lib_ring
  · rw [e] at hO; exact hO

theorem inv_base (j y : Nat) (hq : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1) (hy : Dv (j + 1 + 1) y) :
    Inv (j + 1 + 1) y (gN y (j + 1 + 1)) := fun l k _ =>
  ⟨fun h => ⟨ord_prime j l y hq hy (dv_base _ k _ h), h⟩, fun h => h.2⟩

/-- r | gN(y, q) is impossible when y has order n in ℤ/r and q ∤ n (q prime, q | y). -/
theorem ndv_gNq (j l y n : Nat) (hq : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1) (hy : Dv (j + 1 + 1) y)
    (hqn : Dv (j + 1 + 1) n → False) (hO : OrdQ.IsOrd (ring (l + 1)) (emb (ring (l + 1)) y) n) :
    Dv (l + 1 + 1) (gN y (j + 1 + 1)) → False := fun h =>
  hqn ((ord_prime j l y hq hy h).2 _ hO.1)

theorem gN_swap (y n q : Nat) : gN y (n * q) = gN y q * gN (y ^ q) n := by
  rw [Nat.mul_comm n q, gN_split]

/-- **I2**: Inv(n, y^q, F₁), Inv(n, y, F₀), q ∤ n ⇒ F₀ | F₁. -/
theorem step_dv (j n y F0 F1 : Nat) (hq : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1) (hy : Dv (j + 1 + 1) y)
    (hqn : Dv (j + 1 + 1) n → False) (hn : 1 ≤ n) (hF0 : 1 ≤ F0)
    (I0 : Inv n y F0) (I1 : Inv n (y ^ (j + 1 + 1)) F1) : Dv F0 F1 := by
  refine Val.dv_of_local F0 F0 F1 (Nat.le_refl F0) hF0 (fun l k hr hk => ?_)
  match k, hk with
  | 0, _ => exact ⟨F1, (Nat.mul_one F1).symm⟩
  | k + 1, hk =>
    obtain ⟨hO, hg⟩ := (I0 l k hr).1 hk
    have hO1 : OrdQ.IsOrd (ring (l + 1)) (emb (ring (l + 1)) (y ^ (j + 1 + 1))) n := by
      rw [← ValGeo.pw_emb]; exact OrdQ.ord_pow _ _ n j hO hn hq hqn
    have hg2 : Dv ((l + 1 + 1) ^ (k + 1)) (gN y (j + 1 + 1) * gN (y ^ (j + 1 + 1)) n) := by
      rw [← gN_swap, gN_split]
      exact dv_trans _ _ _ hg ⟨gN (y ^ n) (j + 1 + 1), Nat.mul_comm _ _⟩
    have hg3 := Val.ppow_cancel l _ hr (ndv_gNq j l y n hq hy hqn hO) (k + 1) _ hg2
    exact (I1 l k hr).2 ⟨hO1, hg3⟩

theorem no_one (c a : Nat) (h : 1 = c * (a + 1 + 1)) : False := by
  match c, h with
  | 0, h => exact Nat.noConfusion (h.trans (Nat.zero_mul _))
  | c + 1, h =>
    have e : 0 = c * (a + 1 + 1) + a + 1 := Nat.succ.inj (h.trans (by lib_nat))
    exact Nat.noConfusion e

/-- If r | y then y has no finite order n ≥ 1 in ℤ/r. -/
theorem ndv_of_ord (l y n : Nat) (hn : 1 ≤ n) (hO : OrdQ.IsOrd (ring (l + 1)) (emb (ring (l + 1)) y) n)
    (hy : Dv (l + 1 + 1) y) : False := by
  obtain ⟨n', hn'⟩ := Cheb.le_dest2 1 n hn
  have h := hO.1
  rw [← hn', emb_dv (l + 1) y hy] at h
  have h0 : Frobenius.pw (ring (l + 1)) (ring (l + 1)).zero (n' + 1) = (ring (l + 1)).zero := by
    show (ring (l + 1)).mul (Frobenius.pw (ring (l + 1)) (ring (l + 1)).zero n') (ring (l + 1)).zero = _
    generalize Frobenius.pw (ring (l + 1)) (ring (l + 1)).zero n' = P; lib_ring
  rw [h0] at h
  apply dv_one_absurd l
  apply dv_of_emb (l + 1) 1
  show (ring (l + 1)).add (ring (l + 1)).zero (ring (l + 1)).one = _
  rw [← h]; lib_ring

/-- r^(v+1) | r^v · m ⇒ r | m. -/
theorem ppow_strip (r' m : Nat) : ∀ v, Dv ((r' + 1 + 1) ^ (v + 1)) ((r' + 1 + 1) ^ v * m) → Dv (r' + 1 + 1) m
  | 0, h => by
    obtain ⟨c, hc⟩ := h
    exact ⟨c, by rw [show m = (r' + 1 + 1) ^ 0 * m by show m = 1 * m; lib_nat, hc]; show c * (1 * (r' + 1 + 1)) = _; lib_nat⟩
  | v + 1, h => by
    apply ppow_strip r' m v
    apply Val.dv_cancel r' (v + 1)
    rw [show (r' + 1 + 1) * ((r' + 1 + 1) ^ v * m) = (r' + 1 + 1) ^ (v + 1) * m by
      show _ = (r' + 1 + 1) ^ v * (r' + 1 + 1) * m; generalize (r' + 1 + 1) ^ v = P; lib_nat]
    exact h

/-- **I3**: with F₁ = F₀ · F, the quotient F satisfies Inv(n·q, y, F). -/
theorem step_inv (j n' y F0 F1 F : Nat) (hq : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1) (hy : Dv (j + 1 + 1) y)
    (hF0 : 1 ≤ F0) (I0 : Inv (n' + 1 + 1) y F0) (I1 : Inv (n' + 1 + 1) (y ^ (j + 1 + 1)) F1) (hF : F1 = F0 * F) :
    Inv ((n' + 1 + 1) * (j + 1 + 1)) y F := by
  intro l k hr
  have hn1 : 1 ≤ n' + 1 + 1 := Nat.le_add_left 1 (n' + 1)
  have hpe : ∀ e, Frobenius.pw (ring (l + 1)) (emb (ring (l + 1)) y) e = emb (ring (l + 1)) (y ^ e) := ValGeo.pw_emb l y
  constructor
  · intro hk
    have hk1 : Dv ((l + 1 + 1) ^ (k + 1)) F1 := by rw [hF]; exact dv_mul_left F0 _ F hk
    obtain ⟨hO1, hg1⟩ := (I1 l k hr).1 hk1
    rw [← hpe] at hO1
    rcases OrdQ.ord_of_pow _ _ _ j hO1 hn1 hq with hO | hO
    · exfalso
      obtain ⟨v, m, hvm, hm⟩ := Val.split l F0 F0 (Nat.le_refl F0) hF0
      have hrq : Dv (l + 1 + 1) (j + 1 + 1) → False := fun hd => by
        rcases hq _ hd with e | e
        · exact Nat.noConfusion (Nat.succ.inj e)
        · rw [← e] at hy; exact ndv_of_ord l y _ hn1 hO hy
      obtain ⟨c, hc⟩ := hk
      have hv1 : Dv ((l + 1 + 1) ^ (v + 1)) F1 := ⟨m * c * (l + 1 + 1) ^ k, by
        rw [hF, hvm, hc]
        show (l + 1 + 1) ^ v * m * (c * ((l + 1 + 1) ^ k * (l + 1 + 1))) = m * c * (l + 1 + 1) ^ k * ((l + 1 + 1) ^ v * (l + 1 + 1))
        generalize (l + 1 + 1) ^ v = A; generalize (l + 1 + 1) ^ k = B; lib_nat⟩
      obtain ⟨_, hgv⟩ := (I1 l v hr).1 hv1
      have hg2 : Dv ((l + 1 + 1) ^ (v + 1)) (gN y ((n' + 1 + 1) * (j + 1 + 1)) * 1) := by
        rw [Nat.mul_one, gN_swap]; exact dv_mul_left _ _ _ hgv
      have hg3 := (ValGeo.gN_rpart l y (n' + 1 + 1) (j + 1 + 1) (v + 1) 1 hr hO.1 hrq).1 hg2
      rw [Nat.mul_one] at hg3
      have hF0v := (I0 l v hr).2 ⟨hO, hg3⟩
      rw [hvm] at hF0v
      exact hm (ppow_strip l m v hF0v)
    · refine ⟨hO, ?_⟩
      rw [gN_swap]
      exact dv_mul_left _ _ _ hg1
  · intro ⟨hO, hg⟩
    have hnd : Dv (l + 1 + 1) (gN y (j + 1 + 1)) → False := fun h => by
      obtain ⟨c, hc⟩ := hO.2 _ (ord_prime j l y hq hy h).1
      exact no_one c n' (mul_cancel (j + 1) 1 (c * (n' + 1 + 1)) (by
        show (j + 1 + 1) * 1 = _
        calc (j + 1 + 1) * 1 = c * ((n' + 1 + 1) * (j + 1 + 1)) := by rw [← hc]; lib_nat
          _ = _ := by lib_nat))
    rw [gN_swap] at hg
    have hg2 := Val.ppow_cancel l _ hr hnd (k + 1) _ hg
    have hO1 : OrdQ.IsOrd (ring (l + 1)) (emb (ring (l + 1)) (y ^ (j + 1 + 1))) (n' + 1 + 1) := by
      rw [← hpe]
      refine ⟨?_, fun f hf => ?_⟩
      · rw [OrdQ.pw_pw, Nat.mul_comm]; exact hO.1
      · rw [OrdQ.pw_pw] at hf
        obtain ⟨c, hc⟩ := hO.2 _ hf
        exact ⟨c, mul_cancel (j + 1) _ _ (hc.trans (by lib_nat))⟩
    have hF1 := (I1 l k hr).2 ⟨hO1, hg2⟩
    have hnF0 : Dv (l + 1 + 1) F0 → False := fun h => by
      have h' : Dv ((l + 1 + 1) ^ (0 + 1)) F0 := by rw [show (l + 1 + 1) ^ (0 + 1) = l + 1 + 1 from Val.pow1 _]; exact h
      obtain ⟨hOn, _⟩ := (I0 l 0 hr).1 h'
      obtain ⟨c, hc⟩ := hO.2 _ hOn.1
      exact no_one c j (mul_cancel (n' + 1) 1 (c * (j + 1 + 1)) (by
        calc (n' + 1 + 1) * 1 = c * ((n' + 1 + 1) * (j + 1 + 1)) := by rw [← hc]; lib_nat
          _ = _ := by lib_nat))
    rw [hF] at hF1
    exact Val.ppow_cancel l F0 hr hnF0 (k + 1) F hF1

end PhiInv

#print axioms PhiInv.dv_base
#print axioms PhiInv.ord_prime
#print axioms PhiInv.inv_base
#print axioms PhiInv.ndv_gNq
#print axioms PhiInv.gN_swap
#print axioms PhiInv.step_dv
#print axioms PhiInv.no_one
#print axioms PhiInv.ndv_of_ord
#print axioms PhiInv.ppow_strip
#print axioms PhiInv.step_inv

lang/PhiPow.lean

lean_all/lang/PhiPow.lean

/-
  lang/PhiPow — T43, cycle 489: **p ≡ 1 (mod n^(a+1)) beyond every bound, for every squarefree n ≥ 2 and every a.**

  Run the squarefree construction at y = x^(n^a), x = n·(B + c + 2)!. A prime r | F has ord_r y = n; then ord_r x = n^(a+1)
  (ord_full: if d = ord_r x is a proper divisor of N = n^(a+1), a prime s | N/d divides n, n = t·s, and x^(n^a·t) = 1 gives
  y^t = 1, so n | t < n). r ∤ x as before, and Fermat gives n^(a+1) | r − 1.
    dv_pow_prime    s prime, s | m^k ⇒ s | m
    no_big          n ≥ 1, n = h·n·s, s ≥ 2 is impossible
    ord_full        ord(x^(n^a)) = n ⇒ ord x = n^(a+1)
    inf_pp          ∀ B ∃ prime p + 1 > B with n^(a+1) | p
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.PhiSqInf

namespace PhiSq

open FTAChain ZMod Cheb TwoPrime PhiInv InfSplit

theorem dv_pow_prime (s m : Nat) (hs : ∀ d, Dv d (s + 1 + 1) → d = 1 ∨ d = s + 1 + 1) (hm : 1 ≤ m) :
    ∀ k, Dv (s + 1 + 1) (m ^ k) → Dv (s + 1 + 1) m
  | 0, h => absurd h (dv_one_absurd s)
  | k + 1, h => by
    obtain ⟨u, hu⟩ := le_dest2 1 (m ^ k) (pow_pos' m hm k)
    have h' : Dv (s + 1 + 1) ((u + 1) * m) := by rw [hu]; exact h
    rcases euclid_core s u m hs h' with e | e
    · rw [hu] at e; exact dv_pow_prime s m hs hm k e
    · exact e

theorem no_big (n h s : Nat) (hn : 1 ≤ n) (hs : 2 ≤ s) (e : n = h * n * s) : False := by
  match h, e with
  | 0, e =>
    rw [Nat.zero_mul, Nat.zero_mul] at e
    rw [e] at hn; exact Nat.not_succ_le_zero 0 hn
  | h + 1, e =>
    have h1 : n * 2 ≤ n := by
      calc n * 2 ≤ n * s := Nat.mul_le_mul_left n hs
        _ ≤ (h + 1) * n * s := Nat.mul_le_mul_right s (Nat.le_trans (Nat.le_of_eq (Nat.one_mul n).symm) (Nat.mul_le_mul_right n (Nat.le_add_left 1 h)))
        _ = n := e.symm
    have h2 : n + 1 ≤ n := Nat.le_trans (Nat.add_le_add_left hn n) (Nat.le_trans (Nat.le_of_eq (by lib_nat)) h1)
    exact Nat.not_succ_le_self n h2

theorem ord_full {R : Type} [DecidableEq R] (K : DetChain.CRing R) (x : R) (n a : Nat) (hn : 1 ≤ n)
    (hy : OrdQ.IsOrd K (Frobenius.pw K x (n ^ a)) n) : OrdQ.IsOrd K x (n ^ (a + 1)) := by
  have hN : Frobenius.pw K x (n ^ (a + 1)) = K.one := by
    show Frobenius.pw K x (n ^ a * n) = K.one
    rw [← OrdQ.pw_pw]; exact hy.1
  obtain ⟨N', hN'⟩ := le_dest2 1 (n ^ (a + 1)) (pow_pos' n hn (a + 1))
  rw [← hN'] at hN
  obtain ⟨d, hd, hmin⟩ := Order.ord_exists K x N' hN
  have hO : OrdQ.IsOrd K x (d + 1) := ⟨hd, fun f hf => Order.ord_dvd K x d hd hmin f hf⟩
  obtain ⟨e, he⟩ := hO.2 _ hN
  match e, he with
  | 0, he => rw [Nat.zero_mul] at he; exact Nat.noConfusion he
  | 1, he => rw [show n ^ (a + 1) = d + 1 from hN'.symm.trans (he.trans (Nat.one_mul _))]; exact hO
  | e' + 2, he =>
    exfalso
    obtain ⟨s, hsE, hsmin⟩ := lp_exists e'
    have hspr := lp_prime _ s hsE hsmin
    obtain ⟨g, hg⟩ := hsE
    have hsN : Dv (s + 1 + 1) (n ^ (a + 1)) := ⟨g * (d + 1), by rw [← hN', he, hg]; lib_nat⟩
    obtain ⟨t, ht⟩ := dv_pow_prime s n hspr hn (a + 1) hsN
    -- n^a·t·s = g·s·(d+1), so n^a·t = g·(d+1)
    have hcan : (s + 1 + 1) * (n ^ a * t) = (s + 1 + 1) * (g * (d + 1)) := by
      have e1 : n ^ (a + 1) = n ^ a * n := rfl
      have he2 : n ^ a * (t * (s + 1 + 1)) = (g * (s + 1 + 1)) * (d + 1) := by
        rw [← ht, ← hg, ← e1, ← hN']; exact he
      calc _ = n ^ a * (t * (s + 1 + 1)) := by generalize n ^ a = P; lib_nat
        _ = _ := by rw [he2]; lib_nat
    have hc := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (s + 1)) hcan
    have hxt : Frobenius.pw K (Frobenius.pw K x (n ^ a)) t = K.one := by
      rw [OrdQ.pw_pw, hc, Nat.mul_comm, ← OrdQ.pw_pw, hd, OrdQ.pw_one1]
    obtain ⟨h, hh⟩ := hy.2 _ hxt
    rw [hh] at ht
    exact no_big n h (s + 1 + 1) hn (Nat.le_add_left 2 s) ht

theorem inf_pp (j : Nat) (L : List Nat) (hG : Good (j :: L)) (a B : Nat) :
    ∃ p, (∀ k, Dv k (p + 1) → k = 1 ∨ k = p + 1) ∧ B < p + 1 ∧ Dv (pr (j :: L) ^ (a + 1)) p := by
  obtain ⟨f, hf⟩ := fac_pos (B + cs L + 2)
  have hxB : ∀ q, 1 ≤ q → q ≤ B + cs L + 2 → Dv q (pr (j :: L) * (f + 1)) := fun q h1 h2 => by
    rw [← hf]; exact dv_mul_left _ _ _ (dv_fac _ q h1 h2)
  have hxA : AllDv (j :: L) (pr (j :: L) * (f + 1)) := by
    rw [Nat.mul_comm]; exact allDv_mul _ _ _ (allDv_pr (j :: L))
  have hx1 : 1 ≤ pr (j :: L) * (f + 1) := Nat.mul_le_mul (pr_pos (j :: L)) (Nat.le_add_left 1 f)
  generalize hxdef : pr (j :: L) * (f + 1) = x at hxB hxA hx1
  have hcx : cs L + 2 ≤ x := by
    obtain ⟨x', hx'⟩ := le_dest2 1 x hx1
    have hd := hxB (cs L + 2) (Nat.le_add_left 1 (cs L + 1)) (Nat.le_add_left _ B)
    rw [← hx'] at hd
    obtain ⟨k, hk⟩ := dv_le _ x' hd
    rw [← hx', ← hk]; exact Nat.le_add_right _ _
  -- y = x^(n^a)
  have hna : 1 ≤ pr (j :: L) ^ a := pow_pos' _ (pr_pos (j :: L)) a
  obtain ⟨E, hE⟩ := le_dest2 1 _ hna
  have hyA : AllDv (j :: L) (x ^ (E + 1)) := allDv_pow E _ _ hxA
  have hxy : Dv x (x ^ (E + 1)) := ⟨x ^ E, rfl⟩
  have hxy' : x ≤ x ^ (E + 1) := pow_ge x hx1 _ (Nat.le_add_left 1 E)
  generalize hydef : x ^ (E + 1) = y at hyA hxy hxy'
  obtain ⟨y'', hy''⟩ := le_dest2 2 y (Nat.le_trans (Nat.le_add_left 2 (cs L)) (Nat.le_trans hcx hxy'))
  rw [← hy''] at hyA
  obtain ⟨F, I, l, _⟩ := sqfree j L hG y'' hyA
  rw [show y'' + 1 + 1 = y from hy''] at I l
  have hy1 : 1 ≤ y := Nat.le_trans hx1 hxy'
  have hC : cs L < y ^ ph j L :=
    Nat.le_trans (Nat.le_add_right (cs L + 1) 1) (Nat.le_trans (Nat.le_trans hcx hxy') (pow_ge y hy1 _ (ph_pos j L)))
  have hF2 := PhiSize.two_le _ _ _ l hC
  obtain ⟨F', hF'⟩ := le_dest2 2 F hF2
  obtain ⟨r, hrF, hmin⟩ := lp_exists F'
  have hpr := lp_prime _ r hrF hmin
  rw [hF'] at hrF
  obtain ⟨hO, hg⟩ := (I r 0 hpr).1 (dv_pow1 _ _ hrF)
  have hn2 : 2 ≤ pr (j :: L) := Nat.le_trans (Nat.le_add_left 2 j) (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm)
    (Nat.mul_le_mul_right (j + 1 + 1) (pr_pos L)))
  obtain ⟨n', hn'⟩ := le_dest2 1 _ (Nat.le_trans (Nat.le_add_left 1 1) hn2)
  have hry : Dv (r + 1 + 1) y → False := fun hd => by
    have hg1 : Dv (r + 1 + 1) (gN y (pr (j :: L))) := dv_base _ 0 _ hg
    rw [← hn', Nat.add_comm n' 1, gN_one] at hg1
    exact dv_one_absurd r (dv_sub (r + 1) (y * gN y n') 1 (dv_trans _ _ _ hd ⟨gN y n', Nat.mul_comm _ _⟩)
      (by rw [Nat.add_comm (y * gN y n') 1]; exact hg1))
  have hrx : Dv (r + 1 + 1) x → False := fun hd => hry (dv_trans _ _ _ hd hxy)
  rcases EveryPrime.nat_even_odd r with ⟨i, hi⟩ | ⟨m, hm⟩
  · exfalso
    have h2 : r + 1 + 1 = 2 := by
      cases hpr 2 ⟨i + 1, by rw [hi]; lib_nat⟩ with
      | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
      | inr e => exact e.symm
    rw [h2] at hrx
    exact hrx (hxB 2 (Nat.le_add_left 1 1) (Nat.le_add_left 2 _))
  · subst hm
    have hX0 : emb (ring (2 * m + 1 + 1)) x ≠ (ring (2 * m + 1 + 1)).zero := fun e => hrx (dv_of_emb _ _ e)
    have hXq := Cyclo2l.ferm1 m hpr _ hX0
    rw [← hydef, Cyclo2l.emb_pow, hE] at hO
    have hfull := ord_full (ring (2 * m + 1 + 1)) _ (pr (j :: L)) a (pr_pos (j :: L)) hO
    refine ⟨2 * m + 1 + 1, hpr, ?_, hfull.2 _ hXq⟩
    cases Nat.lt_or_ge B (2 * m + 1 + 1 + 1) with
    | inl h => exact h
    | inr h => exact absurd (hxB _ (Nat.le_add_left 1 _) (Nat.le_trans h (Nat.le_trans (Nat.le_add_right B (cs L)) (Nat.le_add_right _ 2)))) hrx

end PhiSq

#print axioms PhiSq.dv_pow_prime
#print axioms PhiSq.no_big
#print axioms PhiSq.ord_full
#print axioms PhiSq.inf_pp

lang/PhiS.lean

lean_all/lang/PhiS.lean

/-
  lang/PhiS — T43, cycle 476: toward p ≡ 1 (mod N) for every N without cyclotomic polynomials (R8, general modulus).

  Step L1 of the plan (LANGLANDS_LOG, cycle 475): for distinct primes q₁, q₂ with q₁ | y, the geometric sums
  gN(y, q₁) = 1 + y + … + y^(q₁−1) and gN(y, q₂) have no common prime factor.
    geo_one      geo(1, n) = n in any commutative ring
    pw_of_geo    geo(z, n) = 0 ⇒ z^n = 1
    gN_coprime   r ≥ 2 (no primality needed), r | gN(y, q₁), r | gN(y, q₂), q₁ ≠ q₂ primes, q₁ | y ⇒ False
                 (in 𝔽_r: z = y has z^q₁ = z^q₂ = 1, so its order is 1, z = 1, then geo(1, q₁) = q₁ = 0 forces r = q₁ | y, z = 0)
    phi2         L2: gN(y^q₂, q₁) = gN(y, q₁)·Φ and gN(y^q₁, q₂) = gN(y, q₂)·Φ for one Φ (TwoPrime.copr_div + gN_coprime)
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.TwoPrimeInf
import lang.Order

namespace PhiS

open FTAChain ZMod Cheb TwoPrime

theorem geo_one {R : Type} (K : DetChain.CRing R) : ∀ n, geo K K.one n = emb K n
  | 0 => rfl
  | n + 1 => by
    show K.add (geo K K.one n) (Frobenius.pw K K.one n) = K.add (emb K n) K.one
    rw [geo_one K n]
    have h1 : ∀ k, Frobenius.pw K K.one k = K.one := fun k => by
      induction k with
      | zero => rfl
      | succ k ih => show K.mul (Frobenius.pw K K.one k) K.one = K.one; rw [ih]; lib_ring
    rw [h1]

theorem pw_of_geo {R : Type} (K : DetChain.CRing R) (z : R) (n : Nat) (h : geo K z n = K.zero) :
    Frobenius.pw K z n = K.one := by
  have e := geo_shift K z n
  rw [h] at e
  calc Frobenius.pw K z n = K.add K.zero (Frobenius.pw K z n) := by lib_ring
    _ = K.add (K.mul z K.zero) K.one := e.symm
    _ = K.one := by lib_ring

theorem gN_coprime (j1 j2 l y : Nat) (hq1 : ∀ k, Dv k (j1 + 1 + 1) → k = 1 ∨ k = j1 + 1 + 1)
    (hq2 : ∀ k, Dv k (j2 + 1 + 1) → k = 1 ∨ k = j2 + 1 + 1) (hne : j1 ≠ j2)
    (hy : Dv (j1 + 1 + 1) y)
    (h1 : Dv (l + 1 + 1) (gN y (j1 + 1 + 1))) (h2 : Dv (l + 1 + 1) (gN y (j2 + 1 + 1))) : False := by
  have g1 := emb_dv (l + 1) _ h1
  have g2 := emb_dv (l + 1) _ h2
  rw [emb_gN] at g1 g2
  have p1 := pw_of_geo (ring (l + 1)) _ _ g1
  have p2 := pw_of_geo (ring (l + 1)) _ _ g2
  obtain ⟨d, hd, hmin⟩ := Order.ord_exists (ring (l + 1)) _ (j1 + 1) p1
  have d1 := Order.ord_dvd (ring (l + 1)) _ d hd hmin _ p1
  have d2 := Order.ord_dvd (ring (l + 1)) _ d hd hmin _ p2
  have hd0 : d = 0 := by
    rcases hq1 _ d1 with e | e
    · exact Nat.succ.inj e
    · rcases hq2 _ d2 with e' | e'
      · exact Nat.succ.inj e'
      · exact absurd (Nat.succ.inj (Nat.succ.inj (e.symm.trans e'))) hne
  rw [hd0] at hd
  have hz : emb (ring (l + 1)) y = (ring (l + 1)).one := by
    calc emb (ring (l + 1)) y = (ring (l + 1)).mul (ring (l + 1)).one (emb (ring (l + 1)) y) := by lib_ring
      _ = _ := hd
  rw [hz, geo_one] at g1
  have hlq := dv_of_emb (l + 1) _ g1
  rcases hq1 _ hlq with e | e
  · exact Nat.noConfusion (Nat.succ.inj e)
  · rw [← e] at hy
    have hz0 := emb_dv (l + 1) y hy
    rw [hz] at hz0
    apply dv_one_absurd l
    apply dv_of_emb (l + 1) 1
    show (ring (l + 1)).add (ring (l + 1)).zero (ring (l + 1)).one = _
    rw [hz0]; lib_ring

theorem gN_pos (w : Nat) (j : Nat) : 1 ≤ gN w (j + 1) := by
  rw [show j + 1 = 1 + j by lib_nat, gN_one]; exact Nat.le_add_right 1 _

/-- **L2**: for distinct primes q₁, q₂ with q₁ | y there is Φ with gN(y^q₂, q₁) = gN(y, q₁)·Φ and gN(y^q₁, q₂) = gN(y, q₂)·Φ
    (Φ = Φ_{q₁q₂}(y)). -/
theorem phi2 (j1 j2 y : Nat) (hq1 : ∀ k, Dv k (j1 + 1 + 1) → k = 1 ∨ k = j1 + 1 + 1)
    (hq2 : ∀ k, Dv k (j2 + 1 + 1) → k = 1 ∨ k = j2 + 1 + 1) (hne : j1 ≠ j2) (hy : Dv (j1 + 1 + 1) y) :
    ∃ F, gN (y ^ (j2 + 1 + 1)) (j1 + 1 + 1) = gN y (j1 + 1 + 1) * F ∧
      gN (y ^ (j1 + 1 + 1)) (j2 + 1 + 1) = gN y (j2 + 1 + 1) * F := by
  have hEq : gN y (j1 + 1 + 1) * gN (y ^ (j1 + 1 + 1)) (j2 + 1 + 1) = gN (y ^ (j2 + 1 + 1)) (j1 + 1 + 1) * gN y (j2 + 1 + 1) := by
    calc _ = gN y ((j1 + 1 + 1) * (j2 + 1 + 1)) := (gN_split y _ _).symm
      _ = gN y ((j2 + 1 + 1) * (j1 + 1 + 1)) := by rw [Nat.mul_comm (j1 + 1 + 1) (j2 + 1 + 1)]
      _ = gN y (j2 + 1 + 1) * gN (y ^ (j2 + 1 + 1)) (j1 + 1 + 1) := gN_split y _ _
      _ = _ := Nat.mul_comm _ _
  have hdv := copr_div (gN y (j2 + 1 + 1)) (gN (y ^ (j1 + 1 + 1)) (j2 + 1 + 1)) (gN y (j1 + 1 + 1)) (gN y (j1 + 1 + 1))
    (gN (y ^ (j2 + 1 + 1)) (j1 + 1 + 1)) (Nat.le_refl _) (gN_pos y (j1 + 1)) (gN_pos _ (j1 + 1))
    (fun l _ h1 h2 => gN_coprime j1 j2 l y hq1 hq2 hne hy h1 h2) hEq
  obtain ⟨F, hF⟩ := hdv
  refine ⟨F, hF.trans (Nat.mul_comm _ _), ?_⟩
  obtain ⟨D', hD'⟩ := le_dest2 1 (gN y (j1 + 1 + 1)) (gN_pos y (j1 + 1))
  apply mul_cancel D'
  calc (D' + 1) * gN (y ^ (j1 + 1 + 1)) (j2 + 1 + 1) = gN y (j1 + 1 + 1) * gN (y ^ (j1 + 1 + 1)) (j2 + 1 + 1) := by rw [← hD']
    _ = gN (y ^ (j2 + 1 + 1)) (j1 + 1 + 1) * gN y (j2 + 1 + 1) := hEq
    _ = F * gN y (j1 + 1 + 1) * gN y (j2 + 1 + 1) := by rw [hF]
    _ = _ := by rw [← hD']; generalize gN y (j2 + 1 + 1) = U; lib_nat

end PhiS

#print axioms PhiS.geo_one
#print axioms PhiS.pw_of_geo
#print axioms PhiS.gN_coprime
#print axioms PhiS.gN_pos
#print axioms PhiS.phi2

lang/PhiSize.lean

lean_all/lang/PhiSize.lean

/-
  lang/PhiSize — T43, cycle 486: size bounds for the squarefree induction (step I4).

    gN_le        gN(y, m) ≤ y^m                       (y ≥ 2)
    gN_bounds    y^m ≤ gN(y, m + 1) ≤ 2·y^m            (y ≥ 2)
    size_step    Y·Z ≤ c·F₁, F₁ ≤ c·Y·Z, Y ≤ c·F₀, F₀ ≤ c·Y, F₁ = F₀·F, Y ≥ 1  ⇒  Z ≤ c²·F and F ≤ c²·Z
    two_le       Z ≤ C·F, C < Z ⇒ 2 ≤ F
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.TwoPrime

namespace PhiSize

open TwoPrime

theorem gN_le (y' : Nat) : ∀ m, gN (y' + 1 + 1) m ≤ (y' + 1 + 1) ^ m
  | 0 => Nat.zero_le 1
  | m + 1 => by
    show gN (y' + 1 + 1) m + (y' + 1 + 1) ^ m ≤ (y' + 1 + 1) ^ m * (y' + 1 + 1)
    have h := Nat.add_le_add_right (gN_le y' m) ((y' + 1 + 1) ^ m)
    refine Nat.le_trans h ?_
    have e : (y' + 1 + 1) ^ m * (y' + 1 + 1) = ((y' + 1 + 1) ^ m + (y' + 1 + 1) ^ m) + (y' + 1 + 1) ^ m * y' := by
      generalize (y' + 1 + 1) ^ m = P; lib_nat
    rw [e]; exact Nat.le_add_right _ _

theorem gN_bounds (y' m : Nat) :
    (y' + 1 + 1) ^ m ≤ gN (y' + 1 + 1) (m + 1) ∧ gN (y' + 1 + 1) (m + 1) ≤ 2 * (y' + 1 + 1) ^ m := by
  refine ⟨Nat.le_add_left _ _, ?_⟩
  show gN (y' + 1 + 1) m + (y' + 1 + 1) ^ m ≤ _
  have h := Nat.add_le_add_right (gN_le y' m) ((y' + 1 + 1) ^ m)
  exact Nat.le_trans h (Nat.le_of_eq (by generalize (y' + 1 + 1) ^ m = P; lib_nat))

theorem size_step (c Y Z F0 F1 F : Nat) (hY : 1 ≤ Y) (l1 : Y * Z ≤ c * F1) (u1 : F1 ≤ c * (Y * Z))
    (l0 : Y ≤ c * F0) (u0 : F0 ≤ c * Y) (hF : F1 = F0 * F) : Z ≤ c * c * F ∧ F ≤ c * c * Z := by
  constructor
  · apply Nat.le_of_mul_le_mul_left (c := Y) _ hY
    calc Y * Z ≤ c * F1 := l1
      _ = c * F0 * F := by rw [hF]; lib_nat
      _ ≤ c * (c * Y) * F := Nat.mul_le_mul_right F (Nat.mul_le_mul_left c u0)
      _ = Y * (c * c * F) := by lib_nat
  · apply Nat.le_of_mul_le_mul_left (c := Y) _ hY
    calc Y * F ≤ c * F0 * F := Nat.mul_le_mul_right F l0
      _ = c * F1 := by rw [hF]; lib_nat
      _ ≤ c * (c * (Y * Z)) := Nat.mul_le_mul_left c u1
      _ = Y * (c * c * Z) := by lib_nat

theorem two_le (C Z F : Nat) (h : Z ≤ C * F) (hC : C < Z) : 2 ≤ F := by
  match F, h with
  | 0, h => exact absurd (Nat.lt_of_lt_of_le hC (Nat.le_trans h (Nat.le_of_eq (Nat.mul_zero C)))) (Nat.not_lt_zero C)
  | 1, h => exact absurd (Nat.lt_of_lt_of_le hC (Nat.le_trans h (Nat.le_of_eq (Nat.mul_one C)))) (Nat.lt_irrefl C)
  | F + 2, _ => exact Nat.le_add_left 2 F

end PhiSize

#print axioms PhiSize.gN_le
#print axioms PhiSize.gN_bounds
#print axioms PhiSize.size_step
#print axioms PhiSize.two_le

lang/PhiSq.lean

lean_all/lang/PhiSq.lean

/-
  lang/PhiSq — T43, cycle 487: the squarefree induction assembled (step I5).

  For a list q :: L of distinct primes (q_i = j_i + 2) and y ≥ 2 divisible by all of them there is F with
    Inv(Π q_i, y, F)  and  y^φ ≤ c·F ≤ c²·y^φ,
  where φ = Π (q_i − 1) and c = 2^(2^(|L|)) (built by squaring).
    pr, ph, cs, Good, AllDv   the product, the exponent φ, the constant, distinctness/primality, divisibility of y
    pow_mul'                  (y^b)^a = y^(b·a)
    sqfree                    the induction (PhiInv.inv_base, step_dv, step_inv; PhiSize.gN_bounds, size_step)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.PhiInv
import lang.PhiSize
import lang.ChebAll

namespace PhiSq

open FTAChain TwoPrime PhiInv

def pr : List Nat → Nat
  | [] => 1
  | j :: L => pr L * (j + 1 + 1)

def ph (j : Nat) : List Nat → Nat
  | [] => j + 1
  | k :: L => ph k L * (j + 1)

def cs : List Nat → Nat
  | [] => 2
  | _ :: L => cs L * cs L

def Good : List Nat → Prop
  | [] => True
  | j :: L => (∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1) ∧ (Dv (j + 1 + 1) (pr L) → False) ∧ Good L

def AllDv : List Nat → Nat → Prop
  | [], _ => True
  | j :: L, y => Dv (j + 1 + 1) y ∧ AllDv L y

theorem pr_pos : ∀ L, 1 ≤ pr L
  | [] => Nat.le_refl 1
  | j :: L => Nat.le_trans (pr_pos L) (Nat.le_trans (Nat.le_of_eq (Nat.mul_one _).symm)
      (Nat.mul_le_mul_left (pr L) (Nat.le_add_left 1 (j + 1))))

theorem allDv_pow (e : Nat) : ∀ L y, AllDv L y → AllDv L (y ^ (e + 1))
  | [], _, _ => trivial
  | _ :: L, y, ⟨h, hL⟩ => ⟨dv_mul_left _ _ _ h, allDv_pow e L y hL⟩

theorem pow_mul' (y b : Nat) : ∀ a, (y ^ b) ^ a = y ^ (b * a)
  | 0 => rfl
  | a + 1 => by
    show (y ^ b) ^ a * y ^ b = y ^ (b * a + b)
    rw [pow_mul' y b a, pow_add']

theorem sqfree (j : Nat) (L : List Nat) : Good (j :: L) → ∀ y', AllDv (j :: L) (y' + 1 + 1) →
    ∃ F, Inv (pr (j :: L)) (y' + 1 + 1) F ∧ (y' + 1 + 1) ^ ph j L ≤ cs L * F ∧ F ≤ cs L * (y' + 1 + 1) ^ ph j L := by
  induction L generalizing j with
  | nil =>
    intro hG y' hA
    have hb := PhiSize.gN_bounds y' (j + 1)
    refine ⟨gN (y' + 1 + 1) (j + 1 + 1), ?_, Nat.le_trans hb.1 ?_, hb.2⟩
    · show Inv (1 * (j + 1 + 1)) _ _
      rw [Nat.one_mul]; exact inv_base j _ hG.1 hA.1
    · show _ ≤ 2 * _
      exact Nat.le_trans (Nat.le_add_left _ _) (Nat.le_of_eq (Nat.two_mul _).symm)
  | cons k L ih =>
    intro hG y' hA
    obtain ⟨hq, hqn, hG'⟩ := hG
    -- y and y^q
    have hy2 : 2 ≤ (y' + 1 + 1) ^ (j + 1 + 1) :=
      Nat.le_trans (Nat.le_add_left 2 y') (Nat.le_trans (Nat.le_of_eq (Nat.mul_one _).symm)
        (Nat.le_trans (Nat.mul_le_mul_left (y' + 1 + 1) (Cheb.pow_pos' _ (Nat.le_add_left 1 (y' + 1)) (j + 1)))
          (Nat.le_of_eq (Nat.mul_comm _ _))))
    obtain ⟨y'', hy''⟩ := Cheb.le_dest2 2 _ hy2
    have hA1 : AllDv (k :: L) (y'' + 1 + 1) := by
      rw [show y'' + 1 + 1 = y'' + 2 from rfl, hy'']; exact allDv_pow (j + 1) _ _ hA.2
    obtain ⟨F0, I0, l0, u0⟩ := ih k hG' y' hA.2
    obtain ⟨F1, I1, l1, u1⟩ := ih k hG' y'' hA1
    rw [show y'' + 1 + 1 = (y' + 1 + 1) ^ (j + 1 + 1) from hy''] at I1 l1 u1
    rw [pow_mul', Nat.mul_comm (j + 1 + 1) (ph k L),
      show ph k L * (j + 1 + 1) = ph k L + ph k L * (j + 1) by lib_nat, pow_add'] at l1 u1
    have hY : 1 ≤ (y' + 1 + 1) ^ ph k L := Cheb.pow_pos' _ (Nat.le_add_left 1 (y' + 1)) _
    have hF0 : 1 ≤ F0 := by
      match F0, l0 with
      | 0, l0 => exact absurd (Nat.le_trans hY (Nat.le_trans l0 (Nat.le_of_eq (Nat.mul_zero _)))) (Nat.not_succ_le_zero 0)
      | f + 1, _ => exact Nat.le_add_left 1 f
    have hn := pr_pos (k :: L)
    have hD := step_dv j (pr (k :: L)) (y' + 1 + 1) F0 F1 hq hA.1 hqn hn hF0 I0 I1
    obtain ⟨F, hF⟩ := hD
    have hF' : F1 = F0 * F := hF.trans (Nat.mul_comm _ _)
    have hsz := PhiSize.size_step (cs L) _ _ F0 F1 F hY l1 u1 l0 u0 hF'
    refine ⟨F, ?_, hsz.1, hsz.2⟩
    -- pr (k :: L) = n' + 2
    have hn2 : 2 ≤ pr (k :: L) := Nat.le_trans (Nat.le_add_left 2 k) (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm)
      (Nat.mul_le_mul_right (k + 1 + 1) (pr_pos L)))
    obtain ⟨n', hn'⟩ := Cheb.le_dest2 2 _ hn2
    rw [← hn'] at I0 I1
    show Inv (pr (k :: L) * (j + 1 + 1)) _ F
    rw [← hn']
    exact step_inv j n' (y' + 1 + 1) F0 F1 F hq hA.1 hF0 I0 I1 hF'

end PhiSq

#print axioms PhiSq.pr_pos
#print axioms PhiSq.allDv_pow
#print axioms PhiSq.pow_mul'
#print axioms PhiSq.sqfree

lang/PhiSq4.lean

lean_all/lang/PhiSq4.lean

/-
  lang/PhiSq4 — T43, cycle 533: the squarefree induction of PhiSq with the residue of F mod 4:
  y ≡ 2 (mod 4) ⇒ F ≡ 3 (mod 4), and 4 | y ⇒ F ≡ 1 (mod 4)   (F = Φ_n(y) for squarefree n; Φ_n(y) ≡ 1 − μ(n)y mod y²).

    m4_div      4a + 1 = (4b + s)·F, s ∈ {1, 3} ⇒ F ≡ s (mod 4)
    base4       gN(y, q) ≡ 3 (y ≡ 2 mod 4), ≡ 1 (4 | y)
    sqfree4     PhiSq.sqfree plus the residues
  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.PhiSq
import lang.Inf3m4
import lang.Inf3mod8

namespace PhiSq

open FTAChain TwoPrime PhiInv

theorem lt4 (r : Nat) (h : r + 1 ≤ 3) : r < 3 + 1 := Nat.lt_succ_of_le (Nat.le_trans (Nat.le_succ r) h)

theorem m4_div (a b s F : Nat) (hs : s = 1 ∨ s = 3) (h : 4 * a + 1 = (4 * b + s) * F) : ∃ c, F = 4 * c + s := by
  obtain ⟨q, ρ, hq, hρ⟩ := Order.divmod 3 F
  have e : (4 * b + s) * F = (4 * b * q + b * ρ + s * q) * (3 + 1) + s * ρ := by rw [hq]; lib_nat
  rw [e] at h
  have h' : a * (3 + 1) + 1 = (4 * b * q + b * ρ + s * q) * (3 + 1) + s * ρ := by rw [← h]; lib_nat
  have l1 : 1 < 3 + 1 := lt4 1 (Nat.le_add_left 2 1)
  have l0 : 0 < 3 + 1 := Nat.zero_lt_succ 3
  have l2 : 2 < 3 + 1 := lt4 2 (Nat.le_refl 3)
  have l3 : 3 < 3 + 1 := Nat.lt_succ_self 3
  match ρ, hρ, hs with
  | 0, _, Or.inl hs1 =>
    rw [hs1] at h'; exact absurd (Inf3mod8.uniq_rem 3 _ _ 1 0 l1 l0 h') (fun e => Nat.noConfusion e)
  | 0, _, Or.inr hs3 =>
    rw [hs3] at h'; exact absurd (Inf3mod8.uniq_rem 3 _ _ 1 0 l1 l0 h') (fun e => Nat.noConfusion e)
  | 1, _, Or.inl hs1 => exact ⟨q, by rw [hq, hs1]; lib_nat⟩
  | 1, _, Or.inr hs3 =>
    rw [hs3] at h'; exact absurd (Inf3mod8.uniq_rem 3 _ _ 1 3 l1 l3 h') (fun e => Nat.noConfusion (Nat.succ.inj e))
  | 2, _, Or.inl hs1 =>
    rw [hs1] at h'; exact absurd (Inf3mod8.uniq_rem 3 _ _ 1 2 l1 l2 h') (fun e => Nat.noConfusion (Nat.succ.inj e))
  | 2, _, Or.inr hs3 =>
    rw [hs3] at h'
    exact absurd (Inf3mod8.uniq_rem 3 a (4 * b * q + b * 2 + 3 * q + 1) 1 2 l1 l2 (h'.trans (by lib_nat))) (fun e => Nat.noConfusion (Nat.succ.inj e))
  | 3, _, Or.inl hs1 =>
    rw [hs1] at h'; exact absurd (Inf3mod8.uniq_rem 3 _ _ 1 3 l1 l3 h') (fun e => Nat.noConfusion (Nat.succ.inj e))
  | 3, _, Or.inr hs3 => exact ⟨q, by rw [hq, hs3]; lib_nat⟩
  | ρ + 4, hρ, _ => exact absurd (Nat.lt_of_lt_of_le l3 (Nat.le_of_succ_le_succ (Nat.le_trans (Nat.succ_le_succ (Nat.le_add_left 4 ρ)) hρ))) (Nat.lt_irrefl 3)

def R4 (y F : Nat) : Prop := (∀ k, y = 4 * k + 2 → ∃ a, F = 4 * a + 3) ∧ (∀ k, y = 4 * k → ∃ a, F = 4 * a + 1)

theorem base4 (y j : Nat) : R4 y (gN y (j + 1 + 1)) := by
  refine ⟨fun k hk => ?_, fun k hk => ?_⟩
  · obtain ⟨a, ha⟩ := Cheb.g_mod4 k j
    exact ⟨a, by rw [hk, show 4 * k + 2 = 2 * (2 * k + 1) by lib_nat, ha]; lib_nat⟩
  · rw [show j + 1 + 1 = 1 + (j + 1) by lib_nat, gN_one, hk]
    exact ⟨k * gN (4 * k) (j + 1), by lib_nat⟩

theorem sqfree4 (j : Nat) (L : List Nat) : Good (j :: L) → ∀ y', AllDv (j :: L) (y' + 1 + 1) →
    ∃ F, Inv (pr (j :: L)) (y' + 1 + 1) F ∧ (y' + 1 + 1) ^ ph j L ≤ cs L * F ∧ F ≤ cs L * (y' + 1 + 1) ^ ph j L ∧
      R4 (y' + 1 + 1) F := by
  induction L generalizing j with
  | nil =>
    intro hG y' hA
    have hb := PhiSize.gN_bounds y' (j + 1)
    refine ⟨gN (y' + 1 + 1) (j + 1 + 1), ?_, Nat.le_trans hb.1 ?_, hb.2, base4 _ j⟩
    · show Inv (1 * (j + 1 + 1)) _ _
      rw [Nat.one_mul]; exact inv_base j _ hG.1 hA.1
    · show _ ≤ 2 * _
      exact Nat.le_trans (Nat.le_add_left _ _) (Nat.le_of_eq (Nat.two_mul _).symm)
  | cons k L ih =>
    intro hG y' hA
    obtain ⟨hq, hqn, hG'⟩ := hG
    have hy2 : 2 ≤ (y' + 1 + 1) ^ (j + 1 + 1) :=
      Nat.le_trans (Nat.le_add_left 2 y') (Nat.le_trans (Nat.le_of_eq (Nat.mul_one _).symm)
        (Nat.le_trans (Nat.mul_le_mul_left (y' + 1 + 1) (Cheb.pow_pos' _ (Nat.le_add_left 1 (y' + 1)) (j + 1)))
          (Nat.le_of_eq (Nat.mul_comm _ _))))
    obtain ⟨y'', hy''⟩ := Cheb.le_dest2 2 _ hy2
    have hA1 : AllDv (k :: L) (y'' + 1 + 1) := by
      rw [show y'' + 1 + 1 = y'' + 2 from rfl, hy'']; exact allDv_pow (j + 1) _ _ hA.2
    obtain ⟨F0, I0, l0, u0, r0⟩ := ih k hG' y' hA.2
    obtain ⟨F1, I1, l1, u1, r1⟩ := ih k hG' y'' hA1
    rw [show y'' + 1 + 1 = (y' + 1 + 1) ^ (j + 1 + 1) from hy''] at I1 l1 u1 r1
    rw [pow_mul', Nat.mul_comm (j + 1 + 1) (ph k L),
      show ph k L * (j + 1 + 1) = ph k L + ph k L * (j + 1) by lib_nat, pow_add'] at l1 u1
    have hY : 1 ≤ (y' + 1 + 1) ^ ph k L := Cheb.pow_pos' _ (Nat.le_add_left 1 (y' + 1)) _
    have hF0 : 1 ≤ F0 := by
      match F0, l0 with
      | 0, l0 => exact absurd (Nat.le_trans hY (Nat.le_trans l0 (Nat.le_of_eq (Nat.mul_zero _)))) (Nat.not_succ_le_zero 0)
      | f + 1, _ => exact Nat.le_add_left 1 f
    have hn := pr_pos (k :: L)
    have hD := step_dv j (pr (k :: L)) (y' + 1 + 1) F0 F1 hq hA.1 hqn hn hF0 I0 I1
    obtain ⟨F, hF⟩ := hD
    have hF' : F1 = F0 * F := hF.trans (Nat.mul_comm _ _)
    have hsz := PhiSize.size_step (cs L) _ _ F0 F1 F hY l1 u1 l0 u0 hF'
    -- residues
    have hR : R4 (y' + 1 + 1) F := by
      refine ⟨fun kk hk => ?_, fun kk hk => ?_⟩
      · obtain ⟨b, hb⟩ := r0.1 kk hk
        obtain ⟨a, ha⟩ := r1.2 ((y' + 1 + 1) ^ j * (4 * kk * kk + 4 * kk + 1)) (by
          show (y' + 1 + 1) ^ j * (y' + 1 + 1) * (y' + 1 + 1) = _
          rw [hk]; generalize (4 * kk + 2) ^ j = P; lib_nat)
        exact m4_div a b 3 F (Or.inr rfl) (by rw [← ha, hF', hb])
      · obtain ⟨b, hb⟩ := r0.2 kk hk
        obtain ⟨a, ha⟩ := r1.2 (kk * (y' + 1 + 1) ^ (j + 1)) (by
          show (y' + 1 + 1) ^ (j + 1) * (y' + 1 + 1) = _
          rw [hk]; generalize (4 * kk) ^ (j + 1) = P; lib_nat)
        exact m4_div a b 1 F (Or.inl rfl) (by rw [← ha, hF', hb])
    refine ⟨F, ?_, hsz.1, hsz.2, hR⟩
    have hn2 : 2 ≤ pr (k :: L) := Nat.le_trans (Nat.le_add_left 2 k) (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm)
      (Nat.mul_le_mul_right (k + 1 + 1) (pr_pos L)))
    obtain ⟨n', hn'⟩ := Cheb.le_dest2 2 _ hn2
    rw [← hn'] at I0 I1
    show Inv (pr (k :: L) * (j + 1 + 1)) _ F
    rw [← hn']
    exact step_inv j n' (y' + 1 + 1) F0 F1 F hq hA.1 hF0 I0 I1 hF'

end PhiSq

#print axioms PhiSq.lt4
#print axioms PhiSq.m4_div
#print axioms PhiSq.base4
#print axioms PhiSq.sqfree4

lang/PhiSq8.lean

lean_all/lang/PhiSq8.lean

/-
  lang/PhiSq8 — T43, cycle 536: the squarefree induction with the residue of F mod 8:
  y ≡ 4 (mod 8) ⇒ F ≡ 5 (mod 8), and 8 | y ⇒ F ≡ 1 (mod 8)   (Φ_n(y) ≡ 1 − μ(n)y mod y²).

    m8_div      8a + 1 = (8b + s)·F, s ∈ {1, 5} ⇒ F ≡ s (mod 8)      (5·5 ≡ 1)
    base8, sqfree8
  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.PhiSq4

namespace PhiSq

open FTAChain TwoPrime PhiInv

theorem m8_div (a b s F : Nat) (hs : s = 1 ∨ s = 5) (h : 8 * a + 1 = (8 * b + s) * F) : ∃ c, F = 8 * c + s := by
  obtain ⟨q, ρ, hq, hρ⟩ := Order.divmod 7 F
  have e : (8 * b + s) * F = (8 * b * q + b * ρ + s * q) * (7 + 1) + s * ρ := by rw [hq]; lib_nat
  rw [e] at h
  have h' : a * (7 + 1) + 1 = (8 * b * q + b * ρ + s * q) * (7 + 1) + s * ρ := by rw [← h]; lib_nat
  have lt8 : ∀ r, r + 1 ≤ 7 → r < 7 + 1 := fun r hr => Nat.lt_succ_of_le (Nat.le_trans (Nat.le_succ r) hr)
  have u1 : 1 < 7 + 1 := lt8 1 (Nat.le_add_left 2 5)
  -- s·ρ = 8·w + t with t the residue
  have key : ∀ w t, t < 7 + 1 → s * ρ = w * (7 + 1) + t → t = 1 := fun w t ht hst =>
    (Inf3mod8.uniq_rem 7 a (8 * b * q + b * ρ + s * q + w) 1 t u1 ht (h'.trans (by rw [hst]; lib_nat))).symm
  rcases hs with hs | hs <;> subst hs
  · match ρ, hρ with
    | 0, _ => exact absurd (key 0 0 (Nat.zero_lt_succ 7) (by lib_nat)) (fun e => Nat.noConfusion e)
    | 1, _ => exact ⟨q, by rw [hq]; lib_nat⟩
    | ρ + 2, hρ =>
      exact absurd (key 0 (ρ + 2) hρ (by lib_nat)) (fun e => Nat.noConfusion (Nat.succ.inj e))
  · match ρ, hρ with
    | 0, _ => exact absurd (key 0 0 (Nat.zero_lt_succ 7) (by lib_nat)) (fun e => Nat.noConfusion e)
    | 1, _ => exact absurd (key 0 5 (lt8 5 (Nat.le_add_left 6 1)) (by lib_nat)) (fun e => Nat.noConfusion (Nat.succ.inj e))
    | 2, _ => exact absurd (key 1 2 (lt8 2 (Nat.le_add_left 3 4)) (by lib_nat)) (fun e => Nat.noConfusion (Nat.succ.inj e))
    | 3, _ => exact absurd (key 1 7 (Nat.lt_succ_self 7) (by lib_nat)) (fun e => Nat.noConfusion (Nat.succ.inj e))
    | 4, _ => exact absurd (key 2 4 (lt8 4 (Nat.le_add_left 5 2)) (by lib_nat)) (fun e => Nat.noConfusion (Nat.succ.inj e))
    | 5, _ => exact ⟨q, by rw [hq]; lib_nat⟩
    | 6, _ => exact absurd (key 3 6 (lt8 6 (Nat.le_refl 7)) (by lib_nat)) (fun e => Nat.noConfusion (Nat.succ.inj e))
    | 7, _ => exact absurd (key 4 3 (lt8 3 (Nat.le_add_left 4 3)) (by lib_nat)) (fun e => Nat.noConfusion (Nat.succ.inj e))
    | ρ + 8, hρ => exact absurd (Nat.lt_of_lt_of_le (Nat.lt_succ_self 7) (Nat.le_of_succ_le_succ (Nat.le_trans (Nat.succ_le_succ (Nat.le_add_left 8 ρ)) hρ))) (Nat.lt_irrefl 7)

def R8 (y F : Nat) : Prop := (∀ k, y = 8 * k + 4 → ∃ a, F = 8 * a + 5) ∧ (∀ k, y = 8 * k → ∃ a, F = 8 * a + 1)

theorem base8 (y j : Nat) : R8 y (gN y (j + 1 + 1)) := by
  refine ⟨fun k hk => ?_, fun k hk => ?_⟩
  · rw [show j + 1 + 1 = 1 + (1 + j) by lib_nat, gN_one, gN_one, hk]
    generalize gN (8 * k + 4) j = X
    exact ⟨k + (8 * k * k + 8 * k + 2) * X, by lib_nat⟩
  · rw [show j + 1 + 1 = 1 + (j + 1) by lib_nat, gN_one, hk]
    exact ⟨k * gN (8 * k) (j + 1), by lib_nat⟩

theorem sqfree8 (j : Nat) (L : List Nat) : Good (j :: L) → ∀ y', AllDv (j :: L) (y' + 1 + 1) →
    ∃ F, Inv (pr (j :: L)) (y' + 1 + 1) F ∧ (y' + 1 + 1) ^ ph j L ≤ cs L * F ∧ F ≤ cs L * (y' + 1 + 1) ^ ph j L ∧
      R8 (y' + 1 + 1) F := by
  induction L generalizing j with
  | nil =>
    intro hG y' hA
    have hb := PhiSize.gN_bounds y' (j + 1)
    refine ⟨gN (y' + 1 + 1) (j + 1 + 1), ?_, Nat.le_trans hb.1 ?_, hb.2, base8 _ j⟩
    · show Inv (1 * (j + 1 + 1)) _ _
      rw [Nat.one_mul]; exact inv_base j _ hG.1 hA.1
    · show _ ≤ 2 * _
      exact Nat.le_trans (Nat.le_add_left _ _) (Nat.le_of_eq (Nat.two_mul _).symm)
  | cons k L ih =>
    intro hG y' hA
    obtain ⟨hq, hqn, hG'⟩ := hG
    have hy2 : 2 ≤ (y' + 1 + 1) ^ (j + 1 + 1) :=
      Nat.le_trans (Nat.le_add_left 2 y') (Nat.le_trans (Nat.le_of_eq (Nat.mul_one _).symm)
        (Nat.le_trans (Nat.mul_le_mul_left (y' + 1 + 1) (Cheb.pow_pos' _ (Nat.le_add_left 1 (y' + 1)) (j + 1)))
          (Nat.le_of_eq (Nat.mul_comm _ _))))
    obtain ⟨y'', hy''⟩ := Cheb.le_dest2 2 _ hy2
    have hA1 : AllDv (k :: L) (y'' + 1 + 1) := by
      rw [show y'' + 1 + 1 = y'' + 2 from rfl, hy'']; exact allDv_pow (j + 1) _ _ hA.2
    obtain ⟨F0, I0, l0, u0, r0⟩ := ih k hG' y' hA.2
    obtain ⟨F1, I1, l1, u1, r1⟩ := ih k hG' y'' hA1
    rw [show y'' + 1 + 1 = (y' + 1 + 1) ^ (j + 1 + 1) from hy''] at I1 l1 u1 r1
    rw [pow_mul', Nat.mul_comm (j + 1 + 1) (ph k L),
      show ph k L * (j + 1 + 1) = ph k L + ph k L * (j + 1) by lib_nat, pow_add'] at l1 u1
    have hY : 1 ≤ (y' + 1 + 1) ^ ph k L := Cheb.pow_pos' _ (Nat.le_add_left 1 (y' + 1)) _
    have hF0 : 1 ≤ F0 := by
      match F0, l0 with
      | 0, l0 => exact absurd (Nat.le_trans hY (Nat.le_trans l0 (Nat.le_of_eq (Nat.mul_zero _)))) (Nat.not_succ_le_zero 0)
      | f + 1, _ => exact Nat.le_add_left 1 f
    have hn := pr_pos (k :: L)
    have hD := step_dv j (pr (k :: L)) (y' + 1 + 1) F0 F1 hq hA.1 hqn hn hF0 I0 I1
    obtain ⟨F, hF⟩ := hD
    have hF' : F1 = F0 * F := hF.trans (Nat.mul_comm _ _)
    have hsz := PhiSize.size_step (cs L) _ _ F0 F1 F hY l1 u1 l0 u0 hF'
    -- residues
    have hR : R8 (y' + 1 + 1) F := by
      refine ⟨fun kk hk => ?_, fun kk hk => ?_⟩
      · obtain ⟨b, hb⟩ := r0.1 kk hk
        obtain ⟨a, ha⟩ := r1.2 ((y' + 1 + 1) ^ j * (8 * kk * kk + 8 * kk + 2)) (by
          show (y' + 1 + 1) ^ j * (y' + 1 + 1) * (y' + 1 + 1) = _
          rw [hk]; generalize (8 * kk + 4) ^ j = P; lib_nat)
        exact m8_div a b 5 F (Or.inr rfl) (by rw [← ha, hF', hb])
      · obtain ⟨b, hb⟩ := r0.2 kk hk
        obtain ⟨a, ha⟩ := r1.2 (kk * (y' + 1 + 1) ^ (j + 1)) (by
          show (y' + 1 + 1) ^ (j + 1) * (y' + 1 + 1) = _
          rw [hk]; generalize (8 * kk) ^ (j + 1) = P; lib_nat)
        exact m8_div a b 1 F (Or.inl rfl) (by rw [← ha, hF', hb])
    refine ⟨F, ?_, hsz.1, hsz.2, hR⟩
    have hn2 : 2 ≤ pr (k :: L) := Nat.le_trans (Nat.le_add_left 2 k) (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm)
      (Nat.mul_le_mul_right (k + 1 + 1) (pr_pos L)))
    obtain ⟨n', hn'⟩ := Cheb.le_dest2 2 _ hn2
    rw [← hn'] at I0 I1
    show Inv (pr (k :: L) * (j + 1 + 1)) _ F
    rw [← hn']
    exact step_inv j n' (y' + 1 + 1) F0 F1 F hq hA.1 hF0 I0 I1 hF'

end PhiSq

#print axioms PhiSq.m8_div
#print axioms PhiSq.base8
#print axioms PhiSq.sqfree8

lang/PhiSqInf.lean

lean_all/lang/PhiSqInf.lean

/-
  lang/PhiSqInf — T43, cycle 488: **p ≡ 1 (mod n) beyond every bound, for every squarefree n ≥ 2** (step I6).

  n = Π q over a list q :: L of distinct primes (PhiSq.Good), c = PhiSq.cs L, x = n·(B + c + 2)!.
  PhiSq.sqfree gives F with Inv(n, x, F) and x^φ ≤ c·F; x^φ ≥ x > c gives F ≥ 2 (PhiSize.two_le), so F has a prime factor r.
  Inv: x has order n in 𝔽_r and r | gN(x, n) = 1 + x·gN(x, n − 1), so r ∤ x, hence r > B + c + 2 and r odd;
  Fermat (Cyclo2l.ferm1) gives x^(r−1) = 1, so n | r − 1.
    allDv_mul, allDv_pr, ph_pos, pow_ge, dv_pow1   small facts
    inf_sq                                          ∀ B ∃ prime p + 1 > B with n | p
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.PhiSq
import lang.TwoPrimeInf
import lang.Cyclo2l

namespace PhiSq

open FTAChain ZMod Cheb TwoPrime PhiInv InfSplit

theorem allDv_mul (z : Nat) : ∀ L y, AllDv L y → AllDv L (z * y)
  | [], _, _ => trivial
  | _ :: L, y, ⟨h, hL⟩ => ⟨dv_mul_left _ _ _ h, allDv_mul z L y hL⟩

theorem allDv_pr : ∀ L, AllDv L (pr L)
  | [] => trivial
  | j :: L => ⟨⟨pr L, rfl⟩, by
      show AllDv L (pr L * (j + 1 + 1))
      rw [Nat.mul_comm]; exact allDv_mul _ L _ (allDv_pr L)⟩

theorem ph_pos (j : Nat) : ∀ L, 1 ≤ ph j L
  | [] => Nat.le_add_left 1 j
  | k :: L => Nat.le_trans (ph_pos k L) (Nat.le_trans (Nat.le_of_eq (Nat.mul_one _).symm)
      (Nat.mul_le_mul_left _ (Nat.le_add_left 1 j)))

theorem pow_ge (x : Nat) (hx : 1 ≤ x) (e : Nat) (he : 1 ≤ e) : x ≤ x ^ e := by
  obtain ⟨e', he'⟩ := le_dest2 1 e he
  rw [← he']
  show x ≤ x ^ e' * x
  exact Nat.le_trans (Nat.le_of_eq (Nat.one_mul x).symm) (Nat.mul_le_mul (pow_pos' x hx e') (Nat.le_refl x))

theorem dv_pow1 (r F : Nat) (h : Dv r F) : Dv (r ^ (0 + 1)) F := by
  obtain ⟨q, hq⟩ := h
  exact ⟨q, hq.trans (congrArg (q * ·) (Nat.one_mul r).symm)⟩

theorem inf_sq (j : Nat) (L : List Nat) (hG : Good (j :: L)) (B : Nat) :
    ∃ p, (∀ k, Dv k (p + 1) → k = 1 ∨ k = p + 1) ∧ B < p + 1 ∧ Dv (pr (j :: L)) p := by
  obtain ⟨f, hf⟩ := fac_pos (B + cs L + 2)
  have hxB : ∀ q, 1 ≤ q → q ≤ B + cs L + 2 → Dv q (pr (j :: L) * (f + 1)) := fun q h1 h2 => by
    rw [← hf]; exact dv_mul_left _ _ _ (dv_fac _ q h1 h2)
  have hxA : AllDv (j :: L) (pr (j :: L) * (f + 1)) := by
    rw [Nat.mul_comm]; exact allDv_mul _ _ _ (allDv_pr (j :: L))
  have hx1 : 1 ≤ pr (j :: L) * (f + 1) := Nat.mul_le_mul (pr_pos (j :: L)) (Nat.le_add_left 1 f)
  generalize hxdef : pr (j :: L) * (f + 1) = x at hxB hxA hx1
  -- c + 2 ≤ x
  have hcx : cs L + 2 ≤ x := by
    obtain ⟨x', hx'⟩ := le_dest2 1 x hx1
    have hd := hxB (cs L + 2) (Nat.le_add_left 1 (cs L + 1)) (Nat.le_add_left _ B)
    rw [← hx'] at hd
    obtain ⟨k, hk⟩ := dv_le _ x' hd
    rw [← hx', ← hk]; exact Nat.le_add_right _ _
  obtain ⟨x'', hx''⟩ := le_dest2 2 x (Nat.le_trans (Nat.le_add_left 2 (cs L)) hcx)
  rw [← hx''] at hxA
  obtain ⟨F, I, l, _⟩ := sqfree j L hG x'' hxA
  rw [show x'' + 1 + 1 = x from hx''] at I l
  have hC : cs L < x ^ ph j L :=
    Nat.le_trans (Nat.le_add_right (cs L + 1) 1) (Nat.le_trans hcx (pow_ge x hx1 _ (ph_pos j L)))
  have hF2 := PhiSize.two_le _ _ _ l hC
  obtain ⟨F', hF'⟩ := le_dest2 2 F hF2
  obtain ⟨r, hrF, hmin⟩ := lp_exists F'
  have hpr := lp_prime _ r hrF hmin
  rw [hF'] at hrF
  obtain ⟨hO, hg⟩ := (I r 0 hpr).1 (dv_pow1 _ _ hrF)
  -- r ∤ x
  have hn2 : 2 ≤ pr (j :: L) := Nat.le_trans (Nat.le_add_left 2 j) (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm)
    (Nat.mul_le_mul_right (j + 1 + 1) (pr_pos L)))
  obtain ⟨n', hn'⟩ := le_dest2 1 _ (Nat.le_trans (Nat.le_add_left 1 1) hn2)
  have hrx : Dv (r + 1 + 1) x → False := fun hd => by
    have hg1 : Dv (r + 1 + 1) (gN x (pr (j :: L))) := dv_base _ 0 _ hg
    rw [← hn', Nat.add_comm n' 1, gN_one] at hg1
    exact dv_one_absurd r (dv_sub (r + 1) (x * gN x n') 1 (dv_trans _ _ _ hd ⟨gN x n', Nat.mul_comm _ _⟩)
      (by rw [Nat.add_comm (x * gN x n') 1]; exact hg1))
  rcases EveryPrime.nat_even_odd r with ⟨i, hi⟩ | ⟨m, hm⟩
  · exfalso
    have h2 : r + 1 + 1 = 2 := by
      cases hpr 2 ⟨i + 1, by rw [hi]; lib_nat⟩ with
      | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
      | inr e => exact e.symm
    rw [h2] at hrx
    exact hrx (hxB 2 (Nat.le_add_left 1 1) (Nat.le_add_left 2 _))
  · subst hm
    have hX0 : emb (ring (2 * m + 1 + 1)) x ≠ (ring (2 * m + 1 + 1)).zero := fun e => hrx (dv_of_emb _ _ e)
    have hXq := Cyclo2l.ferm1 m hpr _ hX0
    refine ⟨2 * m + 1 + 1, hpr, ?_, hO.2 _ hXq⟩
    cases Nat.lt_or_ge B (2 * m + 1 + 1 + 1) with
    | inl h => exact h
    | inr h => exact absurd (hxB _ (Nat.le_add_left 1 _) (Nat.le_trans h (Nat.le_trans (Nat.le_add_right B (cs L)) (Nat.le_add_right _ 2)))) hrx

end PhiSq

#print axioms PhiSq.allDv_mul
#print axioms PhiSq.allDv_pr
#print axioms PhiSq.ph_pos
#print axioms PhiSq.pow_ge
#print axioms PhiSq.dv_pow1
#print axioms PhiSq.inf_sq

lang/PhzModN.lean

lean_all/lang/PhzModN.lean

/-
  lang/PhzModN — T49 (from A to α, step 1): **evaluation at u kills N**, so the element-level
  Stickelberger congruence A ≡ ε σ^j B (mod N) becomes an equation in E = R[C_ℓ]/(N):
    sumK_cmul    Σ c·f = c·Σ f
    sumK_addc    Σ (f + c)·g = Σ f g + c·Σ g
    phz_modn     Σ_b z^b = 0 and u ≡ v (mod N) ⇒ φ_z(u) = φ_z(v)
    phz_mono     φ_z(s·σ^a) = s·z^a
    rng_gs       Σ_(i<n) w^i over rng n is CycChar.gs w n
    sum_u        Σ_b u^b = 0 in R[C_ℓ]/(N) (ℓ prime)
    phz_u_modn   u ≡ v (mod N) ⇒ φ_u(u) = φ_u(v) in R[C_ℓ]/(N)
    phz_A_B      A ≡ ε σ^j B (mod N) ⇒ φ_u(A) = ε·u^j·φ_u(B)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.ModN
import lang.CycEis
import lang.CycUnit
import lang.PhzSum
import lib.RingTac
import lib.Semi

namespace PhzModN

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob
open LibRing (castZ castN)

variable {S : Type} (E : CRing S)

theorem sumK_cmul {A : Type} (c : S) (f : A → S) : ∀ l : List A,
    sumK E (fun a => E.mul c (f a)) l = E.mul c (sumK E f l)
  | [] => by show E.zero = E.mul c E.zero; lib_ring
  | a :: l => by
    show E.add (E.mul c (f a)) (sumK E (fun a => E.mul c (f a)) l) = E.mul c (E.add (f a) (sumK E f l))
    rw [sumK_cmul c f l]
    lib_ring

theorem sumK_addc {A : Type} (c : S) (f g : A → S) : ∀ l : List A,
    sumK E (fun a => E.mul (E.add (f a) c) (g a)) l
      = E.add (sumK E (fun a => E.mul (f a) (g a)) l) (E.mul c (sumK E g l))
  | [] => by show E.zero = E.add E.zero (E.mul c E.zero); lib_ring
  | a :: l => by
    show E.add (E.mul (E.add (f a) c) (g a)) (sumK E (fun a => E.mul (E.add (f a) c) (g a)) l)
      = E.add (E.add (E.mul (f a) (g a)) (sumK E (fun a => E.mul (f a) (g a)) l)) (E.mul c (E.add (g a) (sumK E g l)))
    rw [sumK_addc c f g l]
    generalize sumK E (fun a => E.mul (f a) (g a)) l = X
    generalize sumK E g l = Y
    lib_ring

theorem phz_eq (m : Nat) (z : S) (v : V Int (m + 1)) :
    CycEis.phz E m z v = sumK E (fun b => E.mul (castZ E (gt LibInt.intR m v b)) (pw E z b.val)) (L m) :=
  PhzSum.phz_sum E m z v

/-- **φ_z respects congruence mod N when Σ_b z^b = 0**. -/
theorem phz_modn (m : Nat) (z : S) (hs : sumK E (fun b : Zp m => pw E z b.val) (L m) = E.zero)
    (u v : V Int (m + 1)) (h : ModN.ModN m u v) : CycEis.phz E m z u = CycEis.phz E m z v := by
  obtain ⟨t, ht⟩ := h
  have e1 : sumK E (fun b => E.mul (castZ E (gt LibInt.intR m u b)) (pw E z b.val)) (L m)
      = sumK E (fun b => E.mul (E.add (castZ E (gt LibInt.intR m v b)) (castZ E t)) (pw E z b.val)) (L m) :=
    sumK_ext E _ _ (fun b => by rw [ht b, IntCast.castZ_add]) (L m)
  have e2 := sumK_addc E (castZ E t) (fun b => castZ E (gt LibInt.intR m v b)) (fun b : Zp m => pw E z b.val) (L m)
  rw [phz_eq, phz_eq, e1]
  refine e2.trans ?_
  show E.add (sumK E (fun b => E.mul (castZ E (gt LibInt.intR m v b)) (pw E z b.val)) (L m))
      (E.mul (castZ E t) (sumK E (fun b : Zp m => pw E z b.val) (L m))) = _
  rw [hs]
  lib_ring

/-- **φ_z(s·σ^a) = s·z^a**. -/
theorem phz_mono (m : Nat) (z : S) (a : Zp m) (s : Int) :
    CycEis.phz E m z (mono LibInt.intR m a s) = E.mul (castZ E s) (pw E z a.val) := by
  have e1 : sumK E (fun b => E.mul (castZ E (gt LibInt.intR m (mono LibInt.intR m a s) b)) (pw E z b.val)) (L m)
      = sumK E (fun b => E.mul (castZ E s) (E.mul (delta E b a) (pw E z b.val))) (L m) :=
    sumK_ext E _ _ (fun b => by
      rw [gt_mono]
      show E.mul (castZ E (s * delta LibInt.intR b a)) (pw E z b.val) = _
      rw [HasseDavenport.castZ_mul, CycPrim.cast_delta]
      lib_ring) (L m)
  rw [phz_eq, e1]
  refine (sumK_cmul E (castZ E s) (fun b => E.mul (delta E b a) (pw E z b.val)) (L m)).trans ?_
  exact congrArg (E.mul (castZ E s)) (pick E _ (CMInert.complete_allZ m E) (fun b => pw E z b.val) a)

theorem rng_gs (w : S) : ∀ n, sumK E (fun i => pw E w i) (CycUnit.rng n) = CycChar.gs E w n
  | 0 => rfl
  | n + 1 => by
    show sumK E (fun i => pw E w i) (CycUnit.rng n ++ [n]) = E.add (pw E w n) (CycChar.gs E w n)
    rw [sumK_append, rng_gs w n]
    show E.add (CycChar.gs E w n) (E.add (pw E w n) E.zero) = _
    lib_ring

variable {R : Type} (K : CRing R)

/-- **Σ_b u^b = 0 in R[C_ℓ]/(N)**, ℓ = n + 2 prime. -/
theorem sum_u (n : Nat) (hpr : ∀ d, Dv d (n + 1 + 1) → d = 1 ∨ d = n + 1 + 1) :
    sumK (CycRing.cyc K (n + 1)) (fun b : Zp (n + 1) => pw (CycRing.cyc K (n + 1)) (CycUnit.u K (n + 1)) b.val) (L (n + 1))
      = (CycRing.cyc K (n + 1)).zero := by
  show sumK (CycRing.cyc K (n + 1)) (fun b : Zp (n + 1) => pw (CycRing.cyc K (n + 1)) (CycUnit.u K (n + 1)) b.val)
    (CMInert.allZ (n + 1) (n + 1 + 1)) = _
  rw [CycUnit.sum_allZ (n + 1) (CycRing.cyc K (n + 1)) _ (n + 1 + 1)]
  have e1 : sumK (CycRing.cyc K (n + 1)) (fun i => pw (CycRing.cyc K (n + 1)) (CycUnit.u K (n + 1)) (md (n + 1) i))
        (CycUnit.rng (n + 1 + 1))
      = sumK (CycRing.cyc K (n + 1)) (fun i => pw (CycRing.cyc K (n + 1)) (CycUnit.u K (n + 1)) i) (CycUnit.rng (n + 1 + 1)) :=
    sumK_ext (CycRing.cyc K (n + 1)) _ _ (fun i =>
      GEval.pw_md (CycRing.cyc K (n + 1)) (n + 1) _ (CycUnit.u_ell K (n + 1)) i) (CycUnit.rng (n + 1 + 1))
  refine Eq.trans e1 ?_
  rw [rng_gs]
  have h1 : CycUnit.u K (n + 1) = pw (CycRing.cyc K (n + 1)) (CycUnit.u K (n + 1)) 1 := by
    show CycUnit.u K (n + 1) = (CycRing.cyc K (n + 1)).mul (CycRing.cyc K (n + 1)).one (CycUnit.u K (n + 1))
    exact ((CycRing.cyc K (n + 1)).one_mul _).symm
  rw [h1]
  apply CycUnit.gs_u K n hpr 1
  intro h0
  rw [CycUnit.nsm_mk (n + 1) 1] at h0
  have hv := congrArg Subtype.val h0
  have e1 : (mk (n + 1) 1).val = 1 := md_small (n + 1) 1 (Nat.succ_lt_succ (Nat.zero_lt_succ n))
  have e0 : ((ring (n + 1)).zero).val = 0 := md_small (n + 1) 0 (Nat.zero_lt_succ (n + 1))
  rw [e1, e0] at hv
  exact Nat.noConfusion hv

/-- **u ≡ v (mod N) ⇒ φ_u(u) = φ_u(v)** in R[C_ℓ]/(N). -/
theorem phz_u_modn (n : Nat) (hpr : ∀ d, Dv d (n + 1 + 1) → d = 1 ∨ d = n + 1 + 1) (x y : V Int (n + 1 + 1))
    (h : ModN.ModN (n + 1) x y) :
    CycEis.phz (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1)) x
      = CycEis.phz (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1)) y :=
  phz_modn (CycRing.cyc K (n + 1)) (n + 1) _ (sum_u K n hpr) x y h

/-- **A ≡ ε σ^j B (mod N) ⇒ φ_u(A) = ε·u^j·φ_u(B)** in R[C_ℓ]/(N). -/
theorem phz_A_B (n : Nat) (hpr : ∀ d, Dv d (n + 1 + 1) → d = 1 ∨ d = n + 1 + 1) (x y : V Int (n + 1 + 1))
    (jj : Zp (n + 1)) (ε : Int) (h : ModN.ModN (n + 1) x (gmul LibInt.intR (n + 1) (mono LibInt.intR (n + 1) jj ε) y)) :
    CycEis.phz (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1)) x
      = (CycRing.cyc K (n + 1)).mul
          ((CycRing.cyc K (n + 1)).mul (castZ (CycRing.cyc K (n + 1)) ε) (pw (CycRing.cyc K (n + 1)) (CycUnit.u K (n + 1)) jj.val))
          (CycEis.phz (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1)) y) := by
  rw [phz_u_modn K n hpr _ _ h]
  show CycEis.phz (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1))
      ((gring LibInt.intR (n + 1)).mul (mono LibInt.intR (n + 1) jj ε) y) = _
  rw [CycEis.phz_mul (CycRing.cyc K (n + 1)) (n + 1) _ (CycUnit.u_ell K (n + 1)), phz_mono]

end PhzModN

#print axioms PhzModN.sumK_cmul
#print axioms PhzModN.sumK_addc
#print axioms PhzModN.phz_eq
#print axioms PhzModN.phz_modn
#print axioms PhzModN.phz_mono
#print axioms PhzModN.rng_gs
#print axioms PhzModN.sum_u
#print axioms PhzModN.phz_u_modn
#print axioms PhzModN.phz_A_B

lang/PhzPw.lean

lean_all/lang/PhzPw.lean

/-
  lang/PhzPw — T49 (O3') (v3): **evaluation of powers and of the product B**: φ_z(v^n) = φ_z(v)^n, and
  φ_z(AbsPair.Bp entries) = Π φ_z(x)^a φ_z(y)^b (z^ℓ = 1).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.AbsPair
import lang.CycEis

namespace PhzPw

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing

variable {S : Type} (E : CRing S) (m : Nat) (z : S) (hz : pw E z (m + 1) = E.one)

include hz in
theorem phz_pw (v : V Int (m + 1)) : ∀ n, CycEis.phz E m z (pw (gring LibInt.intR m) v n) = pw E (CycEis.phz E m z v) n
  | 0 => CycEis.phz_one E m z
  | n + 1 => by
    show CycEis.phz E m z ((gring LibInt.intR m).mul (pw (gring LibInt.intR m) v n) v)
      = E.mul (pw E (CycEis.phz E m z v) n) (CycEis.phz E m z v)
    rw [CycEis.phz_mul E m z hz, phz_pw v n]

def evBp : List (V Int (m + 1) × V Int (m + 1) × Nat × Nat) → S
  | [] => E.one
  | e :: es => E.mul (E.mul (pw E (CycEis.phz E m z e.1) e.2.2.1) (pw E (CycEis.phz E m z e.2.1) e.2.2.2)) (evBp es)

include hz in
theorem phz_Bp : ∀ es, CycEis.phz E m z (AbsPair.Bp (gring LibInt.intR m) es) = evBp E m z es
  | [] => CycEis.phz_one E m z
  | e :: es => by
    show CycEis.phz E m z ((gring LibInt.intR m).mul ((gring LibInt.intR m).mul (pw (gring LibInt.intR m) e.1 e.2.2.1)
        (pw (gring LibInt.intR m) e.2.1 e.2.2.2)) (AbsPair.Bp (gring LibInt.intR m) es))
      = E.mul (E.mul (pw E (CycEis.phz E m z e.1) e.2.2.1) (pw E (CycEis.phz E m z e.2.1) e.2.2.2)) (evBp E m z es)
    rw [CycEis.phz_mul E m z hz, CycEis.phz_mul E m z hz, phz_pw E m z hz, phz_pw E m z hz, phz_Bp es]

end PhzPw

#print axioms PhzPw.phz_pw
#print axioms PhzPw.phz_Bp

lang/PhzSum.lean

lean_all/lang/PhzSum.lean

/-
  lang/PhzSum — T49 (O3') assembly: **φ_z(v) = Σ_b castZ(v(b))·z^b** over CMInert.allZ (the RootsConst form).
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.CycEis

namespace PhzSum

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing LibRing

theorem phz_sum {S : Type} (E : CRing S) (m : Nat) (z : S) (v : V Int (m + 1)) :
    CycEis.phz E m z v
      = sumK E (fun b => E.mul (castZ E (gt LibInt.intR m v b)) (Frobenius.pw E z b.val)) (CMInert.allZ m (m + 1)) := by
  show sumK E (fun b => E.mul (gt E m (GEval.gmap LibInt.intR m (castZ E) v) b) (Frobenius.pw E z b.val)) (L m) = _
  exact sumK_ext E _ _ (fun b => by
    show E.mul (gt E m (tb m (fun a => castZ E (gt LibInt.intR m v a))) b) (Frobenius.pw E z b.val) = _
    rw [gt_tb]) (L m)

end PhzSum

#print axioms PhzSum.phz_sum

lang/Pigeon.lean

lean_all/lang/Pigeon.lean

/-
  lang/Pigeon — T43, cycle 24: the pigeonhole principle, toward Thue's lemma and p = a² + b² for split p.

    Dist, rmv          pairwise distinct lists; removal of one value
    rmv_lt, rmv_dist   removing m from a list of values < m + 1 leaves values < m, still distinct
    len_rmv            a distinct list loses at most one element when m is removed
    pigeonhole         a distinct list of naturals below m has length ≤ m
    collision          for a list of distinct points and f with values < m: if the list is longer than m, two
                       different points have the same value
  Used next: Thue's lemma (pairs (a, b) ∈ [0, k]², (k+1)² > p, f(a, b) = (a + y b) mod p) and Fermat's two squares
  theorem, the norm form of the split primes of ℤ[i] (lang/GaussSplit).

  Lean 4 core only, proofs by hand, no axioms.
-/
import FiniteRecChain

namespace Pigeon

open YMChain FiniteRecChain

def Dist {A : Type} : List A → Prop
  | [] => True
  | r :: rs => (∀ s, memL s rs → s ≠ r) ∧ Dist rs

def rmv (m : Nat) : List Nat → List Nat
  | [] => []
  | a :: l => if a = m then rmv m l else a :: rmv m l

theorem mem_rmv (m x : Nat) : ∀ l, memL x (rmv m l) → memL x l ∧ x ≠ m
  | [], h => False.elim h
  | a :: l, h => by
    by_cases ha : a = m
    · have h' : memL x (rmv m l) := by
        have e : rmv m (a :: l) = rmv m l := ite_pos' _ _ ha
        rw [e] at h; exact h
      have := mem_rmv m x l h'
      exact ⟨Or.inr this.1, this.2⟩
    · have e : rmv m (a :: l) = a :: rmv m l := ite_neg' _ _ ha
      rw [e] at h
      cases h with
      | inl hx => exact ⟨Or.inl hx, fun e' => ha (hx ▸ e')⟩
      | inr hx =>
        have := mem_rmv m x l hx
        exact ⟨Or.inr this.1, this.2⟩

theorem rmv_dist (m : Nat) : ∀ l, Dist l → Dist (rmv m l)
  | [], _ => trivial
  | a :: l, hd => by
    by_cases ha : a = m
    · have e : rmv m (a :: l) = rmv m l := ite_pos' _ _ ha
      rw [e]; exact rmv_dist m l hd.2
    · have e : rmv m (a :: l) = a :: rmv m l := ite_neg' _ _ ha
      rw [e]
      exact ⟨fun s hs => hd.1 s (mem_rmv m s l hs).1, rmv_dist m l hd.2⟩

theorem rmv_absent (m : Nat) : ∀ l, (∀ s, memL s l → s ≠ m) → (rmv m l).length = l.length
  | [], _ => rfl
  | a :: l, h => by
    have ha : ¬ a = m := h a (Or.inl rfl)
    have e : rmv m (a :: l) = a :: rmv m l := ite_neg' _ _ ha
    rw [e]
    exact congrArg Nat.succ (rmv_absent m l (fun s hs => h s (Or.inr hs)))

theorem len_rmv (m : Nat) : ∀ l, Dist l → l.length ≤ (rmv m l).length + 1
  | [], _ => Nat.zero_le _
  | a :: l, hd => by
    by_cases ha : a = m
    · have e : rmv m (a :: l) = rmv m l := ite_pos' _ _ ha
      rw [e, rmv_absent m l (fun s hs e' => hd.1 s hs (e'.trans ha.symm))]
      exact Nat.le_refl _
    · have e : rmv m (a :: l) = a :: rmv m l := ite_neg' _ _ ha
      rw [e]
      exact Nat.succ_le_succ (len_rmv m l hd.2)

/-- **Pigeonhole.** -/
theorem pigeonhole : ∀ (m : Nat) (l : List Nat), Dist l → (∀ x, memL x l → x < m) → l.length ≤ m
  | 0, [], _, _ => Nat.le_refl 0
  | 0, a :: _, _, h => absurd (h a (Or.inl rfl)) (Nat.not_lt_zero a)
  | m + 1, l, hd, h => by
    have hr := pigeonhole m (rmv m l) (rmv_dist m l hd) (fun x hx => by
      have hx' := mem_rmv m x l hx
      cases Nat.lt_or_eq_of_le (Nat.le_of_lt_succ (h x hx'.1)) with
      | inl h' => exact h'
      | inr h' => exact absurd h' hx'.2)
    exact Nat.le_trans (len_rmv m l hd) (Nat.succ_le_succ hr)

theorem find_eq {A : Type} (f : A → Nat) (v : Nat) : ∀ l : List A, (∃ y, memL y l ∧ f y = v) ∨ (∀ y, memL y l → f y ≠ v)
  | [] => Or.inr (fun _ h => False.elim h)
  | a :: l => by
    by_cases ha : f a = v
    · exact Or.inl ⟨a, Or.inl rfl, ha⟩
    · cases find_eq f v l with
      | inl h =>
        match h with
        | ⟨y, hy, e⟩ => exact Or.inl ⟨y, Or.inr hy, e⟩
      | inr h =>
        exact Or.inr (fun y hy => by
          cases hy with
          | inl e => rw [e]; exact ha
          | inr e => exact h y e)

theorem mem_map' {A : Type} (f : A → Nat) (x : Nat) : ∀ l : List A, memL x (l.map f) → ∃ y, memL y l ∧ f y = x
  | [], h => False.elim h
  | a :: l, h => by
    cases h with
    | inl e => exact ⟨a, Or.inl rfl, e.symm⟩
    | inr e =>
      match mem_map' f x l e with
      | ⟨y, hy, ey⟩ => exact ⟨y, Or.inr hy, ey⟩

theorem len_map' {A B : Type} (f : A → B) : ∀ l : List A, (l.map f).length = l.length
  | [] => rfl
  | _ :: l => congrArg Nat.succ (len_map' f l)

/-- Either `f` is injective on the list, or two different points collide. -/
theorem dist_or_collide {A : Type} (f : A → Nat) : ∀ l : List A, Dist l →
    Dist (l.map f) ∨ ∃ x y, memL x l ∧ memL y l ∧ x ≠ y ∧ f x = f y
  | [], _ => Or.inl trivial
  | a :: l, hd => by
    cases find_eq f (f a) l with
    | inl h =>
      match h with
      | ⟨y, hy, e⟩ => exact Or.inr ⟨a, y, Or.inl rfl, Or.inr hy, fun e' => hd.1 y hy e'.symm, e.symm⟩
    | inr h =>
      cases dist_or_collide f l hd.2 with
      | inl hm =>
        exact Or.inl ⟨fun s hs => match mem_map' f s l hs with
          | ⟨y, hy, ey⟩ => fun e => h y hy (ey.trans e), hm⟩
      | inr hc =>
        match hc with
        | ⟨x, y, hx, hy, hxy, e⟩ => exact Or.inr ⟨x, y, Or.inr hx, Or.inr hy, hxy, e⟩

/-- **Collision.** More distinct points than values: two different points have the same value. -/
theorem collision {A : Type} (f : A → Nat) (m : Nat) (l : List A) (hd : Dist l) (hf : ∀ x, f x < m)
    (hlen : m < l.length) : ∃ x y, memL x l ∧ memL y l ∧ x ≠ y ∧ f x = f y := by
  cases dist_or_collide f l hd with
  | inl hm =>
    have := pigeonhole m (l.map f) hm (fun x hx => match mem_map' f x l hx with
      | ⟨y, _, ey⟩ => ey ▸ hf y)
    rw [len_map'] at this
    exact absurd hlen (Nat.not_lt_of_le this)
  | inr h => exact h

end Pigeon

#print axioms Pigeon.mem_rmv
#print axioms Pigeon.rmv_dist
#print axioms Pigeon.rmv_absent
#print axioms Pigeon.len_rmv
#print axioms Pigeon.pigeonhole
#print axioms Pigeon.find_eq
#print axioms Pigeon.mem_map'
#print axioms Pigeon.len_map'
#print axioms Pigeon.dist_or_collide
#print axioms Pigeon.collision

lang/PowerSum.lean

lean_all/lang/PowerSum.lean

/-
  lang/PowerSum — T43, cycle 27: power sums over 𝔽_p.

  p = J + 2 prime, ℤ/p = ring (J + 1), the residues listed once each by CMInert.allZ.
    nz, mem_nz, dist_nz, len_nz    the nonzero residues 1, …, p − 1
    exists_nonroot                 1 ≤ k ≤ p − 2 ⇒ some g ≠ 0 has g^k ≠ 1    (RootBound: x^k − 1 has ≤ k roots)
    power_sum_zero                 Σ_x x^k = 0 for 1 ≤ k ≤ p − 2             (reindex x ↦ g x)
    power_sum_top                  Σ_x x^(p−1) = −1                           (Fermat on p − 1 units)
  These give the point count of a curve mod p from one coefficient: for f of degree < 2(p − 1),
  Σ_x f(x)^((p−1)/2) ≡ −(coefficient of x^(p−1)) — the Hasse invariant, which determines a_p mod p
  (next: a_p(y² = x³ − x) ≡ ±2u mod p at split p = u² + v², with lang/TwoSquares).

  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.EulerHard
import lang.CMInert

namespace PowerSum

open FTAChain ZMod YMChain FiniteRecChain RootBound

variable (J : Nat)

def nz : Nat → List (Zp (J + 1))
  | 0 => []
  | n + 1 => mk (J + 1) (n + 1) :: nz n

theorem len_nz : ∀ n, (nz J n).length = n
  | 0 => rfl
  | n + 1 => congrArg Nat.succ (len_nz n)

theorem mem_nz (s : Zp (J + 1)) : ∀ n, memL s (nz J n) → ∃ i, 1 ≤ i ∧ i ≤ n ∧ s = mk (J + 1) i
  | 0, h => False.elim h
  | n + 1, h => by
    cases h with
    | inl e => exact ⟨n + 1, Nat.succ_le_succ (Nat.zero_le n), Nat.le_refl _, e⟩
    | inr e =>
      match mem_nz s n e with
      | ⟨i, h1, h2, hi⟩ => exact ⟨i, h1, Nat.le_succ_of_le h2, hi⟩

theorem dist_nz : ∀ n, n ≤ J + 1 → Dist (nz J n)
  | 0, _ => trivial
  | n + 1, hn => ⟨fun s hs e => by
      match mem_nz J s n hs with
      | ⟨i, _, h2, hi⟩ =>
        have hv := congrArg Subtype.val (hi.symm.trans e)
        have a : md (J + 1) i = i := md_small _ i (Nat.lt_of_le_of_lt h2 (Nat.lt_of_succ_le (Nat.le_succ_of_le hn)))
        have b : md (J + 1) (n + 1) = n + 1 := md_small _ (n + 1) (Nat.lt_succ_of_le hn)
        have c : i = n + 1 := a.symm.trans (hv.trans b)
        rw [c] at h2
        exact Nat.not_succ_le_self n h2,
    dist_nz n (Nat.le_of_succ_le hn)⟩

theorem mk_ne_zero (i : Nat) (h1 : 1 ≤ i) (h2 : i ≤ J + 1) : mk (J + 1) i ≠ (ring (J + 1)).zero := fun e => by
  have hv := congrArg Subtype.val e
  have a : md (J + 1) i = i := md_small _ i (Nat.lt_succ_of_le h2)
  have : i = 0 := a.symm.trans hv
  rw [this] at h1
  exact Nat.not_succ_le_zero 0 h1

theorem one_ne_zero' : (ring (J + 1)).one ≠ (ring (J + 1)).zero := fun e => by
  have := congrArg Subtype.val e
  rw [TwoPower.one_val] at this
  exact Nat.noConfusion this

theorem find_nonroot (k : Nat) : ∀ l : List (Zp (J + 1)),
    (∃ g, memL g l ∧ Frobenius.pw (ring (J + 1)) g (k + 1) ≠ (ring (J + 1)).one) ∨
    (∀ g, memL g l → Frobenius.pw (ring (J + 1)) g (k + 1) = (ring (J + 1)).one)
  | [] => Or.inr (fun _ h => False.elim h)
  | a :: l => by
    by_cases ha : Frobenius.pw (ring (J + 1)) a (k + 1) = (ring (J + 1)).one
    · cases find_nonroot k l with
      | inl h =>
        match h with
        | ⟨g, hg, e⟩ => exact Or.inl ⟨g, Or.inr hg, e⟩
      | inr h =>
        exact Or.inr (fun g hg => by
          cases hg with
          | inl e => rw [e]; exact ha
          | inr e => exact h g e)
    · exact Or.inl ⟨a, Or.inl rfl, ha⟩

/-- **A non-root of x^k − 1** for 1 ≤ k ≤ p − 2. -/
theorem exists_nonroot (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (k : Nat) (hk : k + 1 ≤ J) :
    ∃ g : Zp (J + 1), g ≠ (ring (J + 1)).zero ∧ Frobenius.pw (ring (J + 1)) g (k + 1) ≠ (ring (J + 1)).one := by
  cases find_nonroot J k (nz J (J + 1)) with
  | inl h =>
    match h with
    | ⟨g, hg, e⟩ =>
      match mem_nz J g (J + 1) hg with
      | ⟨i, h1, h2, hi⟩ => exact ⟨g, by rw [hi]; exact mk_ne_zero J i h1 h2, e⟩
  | inr h =>
    exfalso
    have := root_bound (ring (J + 1)) (nozd J hpr) (k + 1) (ring (J + 1)).one
      (EulerHard.binomL.zerosThen (ring (J + 1)) k) (one_ne_zero' J) (EulerHard.len_zerosThen _ k)
      (nz J (J + 1)) (dist_nz J (J + 1) (Nat.le_refl _))
      (fun r hr => by
        show evH (ring (J + 1)) r (EulerHard.binomL (ring (J + 1)) k) = _
        rw [EulerHard.ev_binom, h r hr, (ring (J + 1)).add_neg])
    rw [len_nz] at this
    exact Nat.not_succ_le_self J (Nat.le_trans this hk)

/-- **Power sums vanish**: `Σ_x x^k = 0` for `1 ≤ k ≤ p − 2`. -/
theorem power_sum_zero (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (k : Nat) (hk : k + 1 ≤ J) :
    sumK (ring (J + 1)) (fun x => Frobenius.pw (ring (J + 1)) x (k + 1)) (CMInert.allZ (J + 1) (J + 1 + 1))
      = (ring (J + 1)).zero := by
  match exists_nonroot J hpr k hk with
  | ⟨g, hg0, hg⟩ =>
    have hre := reindex (ring (J + 1)) (CMInert.allZ (J + 1) (J + 1 + 1)) (CMInert.complete_allZ (J + 1) (ring (J + 1)))
      ((ring (J + 1)).mul g) ((ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) g J))
      (fun x => by rw [← (ring (J + 1)).mul_assoc, inv_exists J hpr g hg0, (ring (J + 1)).one_mul])
      (fun y => by rw [← (ring (J + 1)).mul_assoc, (ring (J + 1)).mul_comm g, inv_exists J hpr g hg0, (ring (J + 1)).one_mul])
      (fun x => Frobenius.pw (ring (J + 1)) x (k + 1))
    have e : sumK (ring (J + 1)) (fun x => Frobenius.pw (ring (J + 1)) ((ring (J + 1)).mul g x) (k + 1))
        (CMInert.allZ (J + 1) (J + 1 + 1))
        = (ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) g (k + 1))
          (sumK (ring (J + 1)) (fun x => Frobenius.pw (ring (J + 1)) x (k + 1)) (CMInert.allZ (J + 1) (J + 1 + 1))) := by
      rw [mul_sumK]
      exact sumK_ext _ _ _ (fun x => Frobenius.pw_mul _ g x (k + 1)) _
    rw [e] at hre
    have e2 : (ring (J + 1)).mul ((ring (J + 1)).add (Frobenius.pw (ring (J + 1)) g (k + 1)) ((ring (J + 1)).neg (ring (J + 1)).one))
        (sumK (ring (J + 1)) (fun x => Frobenius.pw (ring (J + 1)) x (k + 1)) (CMInert.allZ (J + 1) (J + 1 + 1)))
        = (ring (J + 1)).zero := by
      rw [DetChain.right_distrib, hre, DetChain.neg_mul_left, (ring (J + 1)).one_mul, (ring (J + 1)).add_neg]
    cases nozd J hpr _ _ e2 with
    | inl h =>
      exfalso
      apply hg
      have h' := congrArg (fun z => (ring (J + 1)).add z (ring (J + 1)).one) h
      show Frobenius.pw (ring (J + 1)) g (k + 1) = (ring (J + 1)).one
      have := h'.trans ((ring (J + 1)).zero_add _)
      rw [(ring (J + 1)).add_assoc, DetChain.add_neg', DetChain.add_zero'] at this
      exact this
    | inr h => exact h

theorem sum_top_partial (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) : ∀ n, n ≤ J + 1 →
    sumK (ring (J + 1)) (fun x => Frobenius.pw (ring (J + 1)) x (J + 1)) (CMInert.allZ (J + 1) (n + 1)) = mk (J + 1) n
  | 0, _ => by
    show (ring (J + 1)).add ((ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) (mk (J + 1) 0) J) (mk (J + 1) 0))
      (ring (J + 1)).zero = mk (J + 1) 0
    rw [DetChain.add_zero']
    exact DetChain.mul_zero (ring (J + 1)) _
  | n + 1, hn => by
    show sumK (ring (J + 1)) _ (CMInert.allZ (J + 1) (n + 1) ++ [mk (J + 1) (n + 1)]) = _
    rw [CMInert.sumK_app', sum_top_partial hpr n (Nat.le_of_succ_le hn)]
    show (ring (J + 1)).add (mk (J + 1) n) ((ring (J + 1)).add (Frobenius.pw (ring (J + 1)) (mk (J + 1) (n + 1)) (J + 1))
      (ring (J + 1)).zero) = _
    rw [DetChain.add_zero', unit_pow J hpr _ (mk_ne_zero J (n + 1) (Nat.succ_le_succ (Nat.zero_le n)) hn),
      TwoPower.mk_add_one]

/-- **The top power sum**: `Σ_x x^(p−1) = −1`. -/
theorem power_sum_top (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) :
    sumK (ring (J + 1)) (fun x => Frobenius.pw (ring (J + 1)) x (J + 1)) (CMInert.allZ (J + 1) (J + 1 + 1))
      = (ring (J + 1)).neg (ring (J + 1)).one := by
  rw [sum_top_partial J hpr (J + 1) (Nat.le_refl _)]
  apply Subtype.ext
  rw [TwoPower.negone_val]
  exact md_small _ (J + 1) (Nat.lt_succ_self _)

end PowerSum

#print axioms PowerSum.len_nz
#print axioms PowerSum.mem_nz
#print axioms PowerSum.dist_nz
#print axioms PowerSum.mk_ne_zero
#print axioms PowerSum.one_ne_zero'
#print axioms PowerSum.find_nonroot
#print axioms PowerSum.exists_nonroot
#print axioms PowerSum.power_sum_zero
#print axioms PowerSum.sum_top_partial
#print axioms PowerSum.power_sum_top

lang/Prim11.lean

lean_all/lang/Prim11.lean

/-
  lang/Prim11 — T48/T50, cycle 649: **the primary class of 121b's Hecke character** in ℤ[θ], θ = (1 + √−11)/2:
  ρ(a + bθ) = a + 6b is the reduction mod √−11 (θ ≡ 6); H11 = {ρ ≡ 1, 3, 4, 5, 9 (mod 11)} (the squares).
  (Numerically π ∈ H11 of norm p gives 2a + b = a_p(121b) for split p < 120.)
    prim11_mul, prim11_cj, unit_pm11, normalize11, unit_one11
  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.Cop11
import lang.Sq2Prim8
import lang.W36Mul

namespace Prim11

open FTAChain DetChain LibInt Euc11

def rho (z : Int × Int) : Int := z.1 + 6 * z.2

def Prim11 (z : Int × Int) : Prop := ∃ e : Int, rho z = 11 * e + 1 ∨ rho z = 11 * e + 3 ∨ rho z = 11 * e + 4 ∨ rho z = 11 * e + 5 ∨ rho z = 11 * e + 9

theorem rho_mul (x y : Int × Int) : rho ((Ring11.ring11 LibInt.intR).mul x y) = rho x * rho y + -(33 * (x.2 * y.2)) := by
  show x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2) + 6 * (x.1 * y.2 + x.2 * y.1 + x.2 * y.2) = (x.1 + 6 * x.2) * (y.1 + 6 * y.2) + -(33 * (x.2 * y.2))
  lib_int

theorem rho_neg (z : Int × Int) : rho (-z.1, -z.2) = -rho z := by
  show -z.1 + 6 * -z.2 = -(z.1 + 6 * z.2); lib_int

theorem prim11_mul_0 (u v e f w : Int) (hx : u = 11 * e + 1) (hy : v = 11 * f + 1 ∨ v = 11 * f + 3 ∨ v = 11 * f + 4 ∨ v = 11 * f + 5 ∨ v = 11 * f + 9) :
    ∃ E : Int, u * v + -(33 * w) = 11 * E + 1 ∨ u * v + -(33 * w) = 11 * E + 3 ∨ u * v + -(33 * w) = 11 * E + 4 ∨ u * v + -(33 * w) = 11 * E + 5 ∨ u * v + -(33 * w) = 11 * E + 9 := by
  rcases hy with hy | hy | hy | hy | hy <;> rw [hx, hy]
  · exact ⟨e + (11) * e * f + f + (-3) * w, (Or.inl (by lib_int))⟩
  · exact ⟨(3) * e + (11) * e * f + f + (-3) * w, (Or.inr (Or.inl (by lib_int)))⟩
  · exact ⟨(4) * e + (11) * e * f + f + (-3) * w, (Or.inr (Or.inr (Or.inl (by lib_int))))⟩
  · exact ⟨(5) * e + (11) * e * f + f + (-3) * w, (Or.inr (Or.inr (Or.inr (Or.inl (by lib_int)))))⟩
  · exact ⟨(9) * e + (11) * e * f + f + (-3) * w, (Or.inr (Or.inr (Or.inr (Or.inr (by lib_int)))))⟩

theorem prim11_mul_1 (u v e f w : Int) (hx : u = 11 * e + 3) (hy : v = 11 * f + 1 ∨ v = 11 * f + 3 ∨ v = 11 * f + 4 ∨ v = 11 * f + 5 ∨ v = 11 * f + 9) :
    ∃ E : Int, u * v + -(33 * w) = 11 * E + 1 ∨ u * v + -(33 * w) = 11 * E + 3 ∨ u * v + -(33 * w) = 11 * E + 4 ∨ u * v + -(33 * w) = 11 * E + 5 ∨ u * v + -(33 * w) = 11 * E + 9 := by
  rcases hy with hy | hy | hy | hy | hy <;> rw [hx, hy]
  · exact ⟨e + (11) * e * f + (3) * f + (-3) * w, (Or.inr (Or.inl (by lib_int)))⟩
  · exact ⟨(3) * e + (11) * e * f + (3) * f + (-3) * w, (Or.inr (Or.inr (Or.inr (Or.inr (by lib_int)))))⟩
  · exact ⟨(1) + (4) * e + (11) * e * f + (3) * f + (-3) * w, (Or.inl (by lib_int))⟩
  · exact ⟨(1) + (5) * e + (11) * e * f + (3) * f + (-3) * w, (Or.inr (Or.inr (Or.inl (by lib_int))))⟩
  · exact ⟨(2) + (9) * e + (11) * e * f + (3) * f + (-3) * w, (Or.inr (Or.inr (Or.inr (Or.inl (by lib_int)))))⟩

theorem prim11_mul_2 (u v e f w : Int) (hx : u = 11 * e + 4) (hy : v = 11 * f + 1 ∨ v = 11 * f + 3 ∨ v = 11 * f + 4 ∨ v = 11 * f + 5 ∨ v = 11 * f + 9) :
    ∃ E : Int, u * v + -(33 * w) = 11 * E + 1 ∨ u * v + -(33 * w) = 11 * E + 3 ∨ u * v + -(33 * w) = 11 * E + 4 ∨ u * v + -(33 * w) = 11 * E + 5 ∨ u * v + -(33 * w) = 11 * E + 9 := by
  rcases hy with hy | hy | hy | hy | hy <;> rw [hx, hy]
  · exact ⟨e + (11) * e * f + (4) * f + (-3) * w, (Or.inr (Or.inr (Or.inl (by lib_int))))⟩
  · exact ⟨(1) + (3) * e + (11) * e * f + (4) * f + (-3) * w, (Or.inl (by lib_int))⟩
  · exact ⟨(1) + (4) * e + (11) * e * f + (4) * f + (-3) * w, (Or.inr (Or.inr (Or.inr (Or.inl (by lib_int)))))⟩
  · exact ⟨(1) + (5) * e + (11) * e * f + (4) * f + (-3) * w, (Or.inr (Or.inr (Or.inr (Or.inr (by lib_int)))))⟩
  · exact ⟨(3) + (9) * e + (11) * e * f + (4) * f + (-3) * w, (Or.inr (Or.inl (by lib_int)))⟩

theorem prim11_mul_3 (u v e f w : Int) (hx : u = 11 * e + 5) (hy : v = 11 * f + 1 ∨ v = 11 * f + 3 ∨ v = 11 * f + 4 ∨ v = 11 * f + 5 ∨ v = 11 * f + 9) :
    ∃ E : Int, u * v + -(33 * w) = 11 * E + 1 ∨ u * v + -(33 * w) = 11 * E + 3 ∨ u * v + -(33 * w) = 11 * E + 4 ∨ u * v + -(33 * w) = 11 * E + 5 ∨ u * v + -(33 * w) = 11 * E + 9 := by
  rcases hy with hy | hy | hy | hy | hy <;> rw [hx, hy]
  · exact ⟨e + (11) * e * f + (5) * f + (-3) * w, (Or.inr (Or.inr (Or.inr (Or.inl (by lib_int)))))⟩
  · exact ⟨(1) + (3) * e + (11) * e * f + (5) * f + (-3) * w, (Or.inr (Or.inr (Or.inl (by lib_int))))⟩
  · exact ⟨(1) + (4) * e + (11) * e * f + (5) * f + (-3) * w, (Or.inr (Or.inr (Or.inr (Or.inr (by lib_int)))))⟩
  · exact ⟨(2) + (5) * e + (11) * e * f + (5) * f + (-3) * w, (Or.inr (Or.inl (by lib_int)))⟩
  · exact ⟨(4) + (9) * e + (11) * e * f + (5) * f + (-3) * w, (Or.inl (by lib_int))⟩

theorem prim11_mul_4 (u v e f w : Int) (hx : u = 11 * e + 9) (hy : v = 11 * f + 1 ∨ v = 11 * f + 3 ∨ v = 11 * f + 4 ∨ v = 11 * f + 5 ∨ v = 11 * f + 9) :
    ∃ E : Int, u * v + -(33 * w) = 11 * E + 1 ∨ u * v + -(33 * w) = 11 * E + 3 ∨ u * v + -(33 * w) = 11 * E + 4 ∨ u * v + -(33 * w) = 11 * E + 5 ∨ u * v + -(33 * w) = 11 * E + 9 := by
  rcases hy with hy | hy | hy | hy | hy <;> rw [hx, hy]
  · exact ⟨e + (11) * e * f + (9) * f + (-3) * w, (Or.inr (Or.inr (Or.inr (Or.inr (by lib_int)))))⟩
  · exact ⟨(2) + (3) * e + (11) * e * f + (9) * f + (-3) * w, (Or.inr (Or.inr (Or.inr (Or.inl (by lib_int)))))⟩
  · exact ⟨(3) + (4) * e + (11) * e * f + (9) * f + (-3) * w, (Or.inr (Or.inl (by lib_int)))⟩
  · exact ⟨(4) + (5) * e + (11) * e * f + (9) * f + (-3) * w, (Or.inl (by lib_int))⟩
  · exact ⟨(7) + (9) * e + (11) * e * f + (9) * f + (-3) * w, (Or.inr (Or.inr (Or.inl (by lib_int))))⟩

theorem prim11_mul (x y : Int × Int) (hx : Prim11 x) (hy : Prim11 y) : Prim11 ((Ring11.ring11 LibInt.intR).mul x y) := by
  obtain ⟨e, hx⟩ := hx
  obtain ⟨f, hy⟩ := hy
  rw [Prim11, rho_mul]
  generalize x.2 * y.2 = w
  rcases hx with hx | hx | hx | hx | hx
  · exact prim11_mul_0 _ _ e f w hx hy
  · exact prim11_mul_1 _ _ e f w hx hy
  · exact prim11_mul_2 _ _ e f w hx hy
  · exact prim11_mul_3 _ _ e f w hx hy
  · exact prim11_mul_4 _ _ e f w hx hy

theorem prim11_cj (z : Int × Int) (hz : Prim11 z) : Prim11 (cj z) := by
  obtain ⟨e, h⟩ := hz
  have hr : rho (cj z) = rho z + -(11 * z.2) := by show z.1 + z.2 + 6 * -z.2 = z.1 + 6 * z.2 + -(11 * z.2); lib_int
  refine ⟨e + -z.2, ?_⟩
  rw [hr]
  rcases h with h | h | h | h | h
  · exact (Or.inl (by rw [h]; lib_int))
  · exact (Or.inr (Or.inl (by rw [h]; lib_int)))
  · exact (Or.inr (Or.inr (Or.inl (by rw [h]; lib_int))))
  · exact (Or.inr (Or.inr (Or.inr (Or.inl (by rw [h]; lib_int)))))
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (by rw [h]; lib_int)))))

theorem unit_pm11 (u : Int × Int) (h : en u = 1) : u = (Ring11.ring11 LibInt.intR).one ∨ u = (-1, 0) := by
  have h4 := four_nrm u
  have hn : nrm u = 1 := Int.ofNat.inj (by rw [nrm_cast, h]; rfl)
  rw [hn] at h4
  have hB : Int.natAbs u.2 * Int.natAbs u.2 = 0 := by
    cases hb : Int.natAbs u.2 * Int.natAbs u.2 with
    | zero => rfl
    | succ t => rw [hb, show Int.natAbs (2 * u.1 + u.2) * Int.natAbs (2 * u.1 + u.2) + 11 * (t + 1) = (Int.natAbs (2 * u.1 + u.2) * Int.natAbs (2 * u.1 + u.2) + 11 * t + 7) + 4 by lib_nat] at h4
                exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj h4))))
  have b0 : u.2 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ hB with | inl e => exact e | inr e => exact e)
  rw [hB] at h4
  have hT : Int.natAbs (2 * u.1 + u.2) * Int.natAbs (2 * u.1 + u.2) = 2 * 2 := h4.symm
  have hT2 : Int.natAbs (2 * u.1 + u.2) = 2 := by
    match hx : Int.natAbs (2 * u.1 + u.2) with
    | 0 => rw [hx] at hT; exact Nat.noConfusion hT
    | 1 => rw [hx] at hT; exact Nat.noConfusion (Nat.succ.inj hT)
    | 2 => rfl
    | t + 3 => rw [hx, show (t + 3) * (t + 3) = (t * t + 6 * t + 5) + 4 by lib_nat] at hT; exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj hT))))
  rw [b0] at hT2
  obtain ⟨a, b⟩ := u
  show (a, b) = (1, 0) ∨ (a, b) = (-1, 0)
  have b0' : b = 0 := b0
  rcases EisEuclid.sgn (2 * a + 0) with s | s <;> rw [hT2] at s
  · exact Or.inl (Prod.ext (W36Mul.two_cancel_int a 1 (by rw [show 2 * a = 2 * a + 0 by lib_int, s]; rfl)) b0')
  · exact Or.inr (Prod.ext (W36Mul.two_cancel_int a (-1) (by rw [show 2 * a = 2 * a + 0 by lib_int, s]; rfl)) b0')

theorem neg_mul11 (z : Int × Int) : (Ring11.ring11 LibInt.intR).mul (-1, 0) z = (-z.1, -z.2) :=
  Prod.ext (by show -1 * z.1 + -(0 * z.2 + 0 * z.2 + 0 * z.2) = -z.1; lib_int) (by show -1 * z.2 + 0 * z.1 + 0 * z.2 = -z.2; lib_int)

/-- 11 ∤ N(α) ⇒ ±α ∈ H11. -/
theorem normalize11 (α : Int × Int) (h11 : ∀ k, en α ≠ 11 * k) : ∃ u, en u = 1 ∧ Prim11 ((Ring11.ring11 LibInt.intR).mul u α) := by
  obtain ⟨q, r, hq, hr⟩ := GaussEuclid.rnd (rho α) 10
  have hq' : rho α = 11 * q + r := by rw [hq]; show q * 11 + r = 11 * q + r; lib_int
  have plus : ∀ e, (rho α = 11 * e + 1 ∨ rho α = 11 * e + 3 ∨ rho α = 11 * e + 4 ∨ rho α = 11 * e + 5 ∨ rho α = 11 * e + 9) → ∃ u, en u = 1 ∧ Prim11 ((Ring11.ring11 LibInt.intR).mul u α) :=
    fun e he => ⟨(Ring11.ring11 LibInt.intR).one, rfl, by rw [(Ring11.ring11 LibInt.intR).one_mul]; exact ⟨e, he⟩⟩
  have minus : ∀ e, (-rho α = 11 * e + 1 ∨ -rho α = 11 * e + 3 ∨ -rho α = 11 * e + 4 ∨ -rho α = 11 * e + 5 ∨ -rho α = 11 * e + 9) → ∃ u, en u = 1 ∧ Prim11 ((Ring11.ring11 LibInt.intR).mul u α) :=
    fun e he => ⟨(-1, 0), rfl, by rw [neg_mul11]; exact ⟨e, by rw [rho_neg]; exact he⟩⟩
  cases r with
  | ofNat t =>
    match t, hr with
    | 0, _ =>
      exfalso
      obtain ⟨a, b⟩ := α
      have ha : a = 11 * q + -(6 * b) := by
        have : a + 6 * b = 11 * q + Int.ofNat 0 := hq'
        calc a = (a + 6 * b) + -(6 * b) := by lib_int
          _ = _ := by rw [this]; show 11 * q + 0 + -(6 * b) = _; lib_int
      exact h11 (11 * q * q + -(11 * q * b) + 3 * b * b) (by show a * a + a * b + 3 * (b * b) = _; rw [ha]; lib_int)
    | 1, _ => exact plus q (Or.inl (hq'))
    | 2, _ => exact minus (-q + -1) (Or.inr (Or.inr (Or.inr (Or.inr (by rw [hq']; show -(11 * q + 2) = _; lib_int)))))
    | 3, _ => exact plus q (Or.inr (Or.inl (hq')))
    | 4, _ => exact plus q (Or.inr (Or.inr (Or.inl (hq'))))
    | 5, _ => exact plus q (Or.inr (Or.inr (Or.inr (Or.inl (hq')))))
    | t + 6, hr' =>
      exfalso
      have : 2 * (t + 6) ≤ 11 := hr'
      rw [show 2 * (t + 6) = 2 * t + 1 + 11 by lib_nat] at this
      exact Nat.not_succ_le_self 11 (Nat.le_trans (Nat.add_le_add_right (Nat.zero_le (2 * t)) 12) (Nat.le_trans (Nat.le_of_eq (by lib_nat)) this))
  | negSucc t =>
    match t, hr with
    | 0, _ => exact minus (-q) (Or.inl (by rw [hq']; show -(11 * q + -1) = _; lib_int))
    | 1, _ => exact plus (q + -1) (Or.inr (Or.inr (Or.inr (Or.inr (by rw [hq']; show 11 * q + -2 = _; lib_int)))))
    | 2, _ => exact minus (-q) (Or.inr (Or.inl (by rw [hq']; show -(11 * q + -3) = _; lib_int)))
    | 3, _ => exact minus (-q) (Or.inr (Or.inr (Or.inl (by rw [hq']; show -(11 * q + -4) = _; lib_int))))
    | 4, _ => exact minus (-q) (Or.inr (Or.inr (Or.inr (Or.inl (by rw [hq']; show -(11 * q + -5) = _; lib_int)))))
    | t + 5, hr' =>
      exfalso
      have : 2 * (t + 6) ≤ 11 := hr'
      rw [show 2 * (t + 6) = 2 * t + 1 + 11 by lib_nat] at this
      exact Nat.not_succ_le_self 11 (Nat.le_trans (Nat.add_le_add_right (Nat.zero_le (2 * t)) 12) (Nat.le_trans (Nat.le_of_eq (by lib_nat)) this))

/-- **A unit taking an H11-element to an H11-element is 1.** -/
theorem unit_one11 (u α : Int × Int) (hu : en u = 1) (pα : Prim11 α) (pu : Prim11 ((Ring11.ring11 LibInt.intR).mul u α)) : u = (Ring11.ring11 LibInt.intR).one := by
  rcases unit_pm11 u hu with e0 | e0
  · exact e0
  exfalso
  rw [e0, neg_mul11] at pu
  obtain ⟨e, h1⟩ := pα
  obtain ⟨f, h2⟩ := pu
  rw [rho_neg] at h2
  generalize rho α = x at h1 h2
  rcases h1 with h1 | h1 | h1 | h1 | h1 <;> rcases h2 with h2 | h2 | h2 | h2 | h2
  · exact Sq2Prim8.noM 10 (e + f + -(-1)) 8 1 (by calc (11 : Int) * (e + f + -(-1)) = (11 * e + 1) + (11 * f + 1) + 9 := by lib_int
        _ = x + -x + 9 := by rw [← h1, ← h2]
        _ = 9 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-1)) 6 3 (by calc (11 : Int) * (e + f + -(-1)) = (11 * e + 1) + (11 * f + 3) + 7 := by lib_int
        _ = x + -x + 7 := by rw [← h1, ← h2]
        _ = 7 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-1)) 5 4 (by calc (11 : Int) * (e + f + -(-1)) = (11 * e + 1) + (11 * f + 4) + 6 := by lib_int
        _ = x + -x + 6 := by rw [← h1, ← h2]
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-1)) 4 5 (by calc (11 : Int) * (e + f + -(-1)) = (11 * e + 1) + (11 * f + 5) + 5 := by lib_int
        _ = x + -x + 5 := by rw [← h1, ← h2]
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-1)) 0 9 (by calc (11 : Int) * (e + f + -(-1)) = (11 * e + 1) + (11 * f + 9) + 1 := by lib_int
        _ = x + -x + 1 := by rw [← h1, ← h2]
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-1)) 6 3 (by calc (11 : Int) * (e + f + -(-1)) = (11 * e + 3) + (11 * f + 1) + 7 := by lib_int
        _ = x + -x + 7 := by rw [← h1, ← h2]
        _ = 7 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-1)) 4 5 (by calc (11 : Int) * (e + f + -(-1)) = (11 * e + 3) + (11 * f + 3) + 5 := by lib_int
        _ = x + -x + 5 := by rw [← h1, ← h2]
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-1)) 3 6 (by calc (11 : Int) * (e + f + -(-1)) = (11 * e + 3) + (11 * f + 4) + 4 := by lib_int
        _ = x + -x + 4 := by rw [← h1, ← h2]
        _ = 4 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-1)) 2 7 (by calc (11 : Int) * (e + f + -(-1)) = (11 * e + 3) + (11 * f + 5) + 3 := by lib_int
        _ = x + -x + 3 := by rw [← h1, ← h2]
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-2)) 9 0 (by calc (11 : Int) * (e + f + -(-2)) = (11 * e + 3) + (11 * f + 9) + 10 := by lib_int
        _ = x + -x + 10 := by rw [← h1, ← h2]
        _ = 10 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-1)) 5 4 (by calc (11 : Int) * (e + f + -(-1)) = (11 * e + 4) + (11 * f + 1) + 6 := by lib_int
        _ = x + -x + 6 := by rw [← h1, ← h2]
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-1)) 3 6 (by calc (11 : Int) * (e + f + -(-1)) = (11 * e + 4) + (11 * f + 3) + 4 := by lib_int
        _ = x + -x + 4 := by rw [← h1, ← h2]
        _ = 4 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-1)) 2 7 (by calc (11 : Int) * (e + f + -(-1)) = (11 * e + 4) + (11 * f + 4) + 3 := by lib_int
        _ = x + -x + 3 := by rw [← h1, ← h2]
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-1)) 1 8 (by calc (11 : Int) * (e + f + -(-1)) = (11 * e + 4) + (11 * f + 5) + 2 := by lib_int
        _ = x + -x + 2 := by rw [← h1, ← h2]
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-2)) 8 1 (by calc (11 : Int) * (e + f + -(-2)) = (11 * e + 4) + (11 * f + 9) + 9 := by lib_int
        _ = x + -x + 9 := by rw [← h1, ← h2]
        _ = 9 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-1)) 4 5 (by calc (11 : Int) * (e + f + -(-1)) = (11 * e + 5) + (11 * f + 1) + 5 := by lib_int
        _ = x + -x + 5 := by rw [← h1, ← h2]
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-1)) 2 7 (by calc (11 : Int) * (e + f + -(-1)) = (11 * e + 5) + (11 * f + 3) + 3 := by lib_int
        _ = x + -x + 3 := by rw [← h1, ← h2]
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-1)) 1 8 (by calc (11 : Int) * (e + f + -(-1)) = (11 * e + 5) + (11 * f + 4) + 2 := by lib_int
        _ = x + -x + 2 := by rw [← h1, ← h2]
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-1)) 0 9 (by calc (11 : Int) * (e + f + -(-1)) = (11 * e + 5) + (11 * f + 5) + 1 := by lib_int
        _ = x + -x + 1 := by rw [← h1, ← h2]
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-2)) 7 2 (by calc (11 : Int) * (e + f + -(-2)) = (11 * e + 5) + (11 * f + 9) + 8 := by lib_int
        _ = x + -x + 8 := by rw [← h1, ← h2]
        _ = 8 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-1)) 0 9 (by calc (11 : Int) * (e + f + -(-1)) = (11 * e + 9) + (11 * f + 1) + 1 := by lib_int
        _ = x + -x + 1 := by rw [← h1, ← h2]
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-2)) 9 0 (by calc (11 : Int) * (e + f + -(-2)) = (11 * e + 9) + (11 * f + 3) + 10 := by lib_int
        _ = x + -x + 10 := by rw [← h1, ← h2]
        _ = 10 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-2)) 8 1 (by calc (11 : Int) * (e + f + -(-2)) = (11 * e + 9) + (11 * f + 4) + 9 := by lib_int
        _ = x + -x + 9 := by rw [← h1, ← h2]
        _ = 9 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-2)) 7 2 (by calc (11 : Int) * (e + f + -(-2)) = (11 * e + 9) + (11 * f + 5) + 8 := by lib_int
        _ = x + -x + 8 := by rw [← h1, ← h2]
        _ = 8 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-2)) 3 6 (by calc (11 : Int) * (e + f + -(-2)) = (11 * e + 9) + (11 * f + 9) + 4 := by lib_int
        _ = x + -x + 4 := by rw [← h1, ← h2]
        _ = 4 := by lib_int) rfl

end Prim11

#print axioms Prim11.rho_mul
#print axioms Prim11.rho_neg
#print axioms Prim11.prim11_mul_0
#print axioms Prim11.prim11_mul_1
#print axioms Prim11.prim11_mul_2
#print axioms Prim11.prim11_mul_3
#print axioms Prim11.prim11_mul_4
#print axioms Prim11.prim11_mul
#print axioms Prim11.prim11_cj
#print axioms Prim11.unit_pm11
#print axioms Prim11.neg_mul11
#print axioms Prim11.normalize11
#print axioms Prim11.unit_one11

lang/Prim163.lean

lean_all/lang/Prim163.lean

/-
  lang/Prim163 — T48/T50: **the primary class in ℤ[(1 + √−163)/2] through the Legendre symbol** (no residue enumeration):
  ρ(a + bθ) = a + 82b is the reduction mod √−163 (θ ≡ 82), and H163 = {z : χ₁₆₃(ρ z) = 1} (QChar.chi with p = 163).
    prime163      163 is prime (ZMod.md certificates)
    chi_m1       χ(−1) = −1 (163 ≡ 3 mod 4);  chi_neg  χ(−x) = −χ(x)
    cr_mul       ρ(xy) ≡ ρ(x)ρ(y) (mod 163)
    prim_mul, prim_cj, unit_pm, normalize, unit_one    the interface used by the Hecke files
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Euc163
import lang.GaussQR
import lang.CubicTwo
import lang.PencilClosed
import lang.W36Mul

set_option maxRecDepth 60000

namespace Prim163

open FTAChain DetChain LibInt Euc163 ZMod QChar
open LibRing (castZ castN)

theorem prime163 : ∀ d, Dv d (2 * 80 + 1 + 1 + 1) → d = 1 ∨ d = 2 * 80 + 1 + 1 + 1 := by
  intro d ⟨q, hq⟩
  match d with
  | 0 => exact absurd (hq.trans (Nat.mul_zero q)) (fun h => Nat.noConfusion h)
  | e + 1 =>
    match e with
    | 0 => exact Or.inl rfl
    | 1 => exact absurd (ZMod.dv_md_zero 1 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 2 => exact absurd (ZMod.dv_md_zero 2 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 3 => exact absurd (ZMod.dv_md_zero 3 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 4 => exact absurd (ZMod.dv_md_zero 4 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 5 => exact absurd (ZMod.dv_md_zero 5 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 6 => exact absurd (ZMod.dv_md_zero 6 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 7 => exact absurd (ZMod.dv_md_zero 7 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 8 => exact absurd (ZMod.dv_md_zero 8 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 9 => exact absurd (ZMod.dv_md_zero 9 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 10 => exact absurd (ZMod.dv_md_zero 10 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 11 => exact absurd (ZMod.dv_md_zero 11 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 12 => exact absurd (ZMod.dv_md_zero 12 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 13 => exact absurd (ZMod.dv_md_zero 13 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 14 => exact absurd (ZMod.dv_md_zero 14 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 15 => exact absurd (ZMod.dv_md_zero 15 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 16 => exact absurd (ZMod.dv_md_zero 16 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 17 => exact absurd (ZMod.dv_md_zero 17 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 18 => exact absurd (ZMod.dv_md_zero 18 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 19 => exact absurd (ZMod.dv_md_zero 19 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 20 => exact absurd (ZMod.dv_md_zero 20 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 21 => exact absurd (ZMod.dv_md_zero 21 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 22 => exact absurd (ZMod.dv_md_zero 22 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 23 => exact absurd (ZMod.dv_md_zero 23 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 24 => exact absurd (ZMod.dv_md_zero 24 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 25 => exact absurd (ZMod.dv_md_zero 25 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 26 => exact absurd (ZMod.dv_md_zero 26 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 27 => exact absurd (ZMod.dv_md_zero 27 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 28 => exact absurd (ZMod.dv_md_zero 28 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 29 => exact absurd (ZMod.dv_md_zero 29 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 30 => exact absurd (ZMod.dv_md_zero 30 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 31 => exact absurd (ZMod.dv_md_zero 31 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 32 => exact absurd (ZMod.dv_md_zero 32 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 33 => exact absurd (ZMod.dv_md_zero 33 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 34 => exact absurd (ZMod.dv_md_zero 34 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 35 => exact absurd (ZMod.dv_md_zero 35 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 36 => exact absurd (ZMod.dv_md_zero 36 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 37 => exact absurd (ZMod.dv_md_zero 37 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 38 => exact absurd (ZMod.dv_md_zero 38 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 39 => exact absurd (ZMod.dv_md_zero 39 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 40 => exact absurd (ZMod.dv_md_zero 40 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 41 => exact absurd (ZMod.dv_md_zero 41 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 42 => exact absurd (ZMod.dv_md_zero 42 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 43 => exact absurd (ZMod.dv_md_zero 43 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 44 => exact absurd (ZMod.dv_md_zero 44 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 45 => exact absurd (ZMod.dv_md_zero 45 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 46 => exact absurd (ZMod.dv_md_zero 46 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 47 => exact absurd (ZMod.dv_md_zero 47 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 48 => exact absurd (ZMod.dv_md_zero 48 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 49 => exact absurd (ZMod.dv_md_zero 49 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 50 => exact absurd (ZMod.dv_md_zero 50 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 51 => exact absurd (ZMod.dv_md_zero 51 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 52 => exact absurd (ZMod.dv_md_zero 52 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 53 => exact absurd (ZMod.dv_md_zero 53 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 54 => exact absurd (ZMod.dv_md_zero 54 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 55 => exact absurd (ZMod.dv_md_zero 55 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 56 => exact absurd (ZMod.dv_md_zero 56 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 57 => exact absurd (ZMod.dv_md_zero 57 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 58 => exact absurd (ZMod.dv_md_zero 58 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 59 => exact absurd (ZMod.dv_md_zero 59 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 60 => exact absurd (ZMod.dv_md_zero 60 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 61 => exact absurd (ZMod.dv_md_zero 61 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 62 => exact absurd (ZMod.dv_md_zero 62 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 63 => exact absurd (ZMod.dv_md_zero 63 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 64 => exact absurd (ZMod.dv_md_zero 64 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 65 => exact absurd (ZMod.dv_md_zero 65 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 66 => exact absurd (ZMod.dv_md_zero 66 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 67 => exact absurd (ZMod.dv_md_zero 67 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 68 => exact absurd (ZMod.dv_md_zero 68 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 69 => exact absurd (ZMod.dv_md_zero 69 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 70 => exact absurd (ZMod.dv_md_zero 70 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 71 => exact absurd (ZMod.dv_md_zero 71 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 72 => exact absurd (ZMod.dv_md_zero 72 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 73 => exact absurd (ZMod.dv_md_zero 73 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 74 => exact absurd (ZMod.dv_md_zero 74 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 75 => exact absurd (ZMod.dv_md_zero 75 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 76 => exact absurd (ZMod.dv_md_zero 76 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 77 => exact absurd (ZMod.dv_md_zero 77 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 78 => exact absurd (ZMod.dv_md_zero 78 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 79 => exact absurd (ZMod.dv_md_zero 79 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 80 => exact absurd (ZMod.dv_md_zero 80 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 81 => exact absurd (ZMod.dv_md_zero 81 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 82 => exact absurd (ZMod.dv_md_zero 82 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 83 => exact absurd (ZMod.dv_md_zero 83 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 84 => exact absurd (ZMod.dv_md_zero 84 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 85 => exact absurd (ZMod.dv_md_zero 85 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 86 => exact absurd (ZMod.dv_md_zero 86 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 87 => exact absurd (ZMod.dv_md_zero 87 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 88 => exact absurd (ZMod.dv_md_zero 88 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 89 => exact absurd (ZMod.dv_md_zero 89 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 90 => exact absurd (ZMod.dv_md_zero 90 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 91 => exact absurd (ZMod.dv_md_zero 91 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 92 => exact absurd (ZMod.dv_md_zero 92 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 93 => exact absurd (ZMod.dv_md_zero 93 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 94 => exact absurd (ZMod.dv_md_zero 94 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 95 => exact absurd (ZMod.dv_md_zero 95 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 96 => exact absurd (ZMod.dv_md_zero 96 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 97 => exact absurd (ZMod.dv_md_zero 97 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 98 => exact absurd (ZMod.dv_md_zero 98 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 99 => exact absurd (ZMod.dv_md_zero 99 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 100 => exact absurd (ZMod.dv_md_zero 100 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 101 => exact absurd (ZMod.dv_md_zero 101 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 102 => exact absurd (ZMod.dv_md_zero 102 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 103 => exact absurd (ZMod.dv_md_zero 103 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 104 => exact absurd (ZMod.dv_md_zero 104 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 105 => exact absurd (ZMod.dv_md_zero 105 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 106 => exact absurd (ZMod.dv_md_zero 106 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 107 => exact absurd (ZMod.dv_md_zero 107 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 108 => exact absurd (ZMod.dv_md_zero 108 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 109 => exact absurd (ZMod.dv_md_zero 109 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 110 => exact absurd (ZMod.dv_md_zero 110 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 111 => exact absurd (ZMod.dv_md_zero 111 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 112 => exact absurd (ZMod.dv_md_zero 112 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 113 => exact absurd (ZMod.dv_md_zero 113 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 114 => exact absurd (ZMod.dv_md_zero 114 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 115 => exact absurd (ZMod.dv_md_zero 115 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 116 => exact absurd (ZMod.dv_md_zero 116 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 117 => exact absurd (ZMod.dv_md_zero 117 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 118 => exact absurd (ZMod.dv_md_zero 118 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 119 => exact absurd (ZMod.dv_md_zero 119 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 120 => exact absurd (ZMod.dv_md_zero 120 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 121 => exact absurd (ZMod.dv_md_zero 121 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 122 => exact absurd (ZMod.dv_md_zero 122 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 123 => exact absurd (ZMod.dv_md_zero 123 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 124 => exact absurd (ZMod.dv_md_zero 124 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 125 => exact absurd (ZMod.dv_md_zero 125 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 126 => exact absurd (ZMod.dv_md_zero 126 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 127 => exact absurd (ZMod.dv_md_zero 127 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 128 => exact absurd (ZMod.dv_md_zero 128 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 129 => exact absurd (ZMod.dv_md_zero 129 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 130 => exact absurd (ZMod.dv_md_zero 130 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 131 => exact absurd (ZMod.dv_md_zero 131 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 132 => exact absurd (ZMod.dv_md_zero 132 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 133 => exact absurd (ZMod.dv_md_zero 133 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 134 => exact absurd (ZMod.dv_md_zero 134 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 135 => exact absurd (ZMod.dv_md_zero 135 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 136 => exact absurd (ZMod.dv_md_zero 136 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 137 => exact absurd (ZMod.dv_md_zero 137 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 138 => exact absurd (ZMod.dv_md_zero 138 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 139 => exact absurd (ZMod.dv_md_zero 139 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 140 => exact absurd (ZMod.dv_md_zero 140 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 141 => exact absurd (ZMod.dv_md_zero 141 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 142 => exact absurd (ZMod.dv_md_zero 142 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 143 => exact absurd (ZMod.dv_md_zero 143 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 144 => exact absurd (ZMod.dv_md_zero 144 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 145 => exact absurd (ZMod.dv_md_zero 145 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 146 => exact absurd (ZMod.dv_md_zero 146 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 147 => exact absurd (ZMod.dv_md_zero 147 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 148 => exact absurd (ZMod.dv_md_zero 148 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 149 => exact absurd (ZMod.dv_md_zero 149 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 150 => exact absurd (ZMod.dv_md_zero 150 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 151 => exact absurd (ZMod.dv_md_zero 151 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 152 => exact absurd (ZMod.dv_md_zero 152 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 153 => exact absurd (ZMod.dv_md_zero 153 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 154 => exact absurd (ZMod.dv_md_zero 154 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 155 => exact absurd (ZMod.dv_md_zero 155 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 156 => exact absurd (ZMod.dv_md_zero 156 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 157 => exact absurd (ZMod.dv_md_zero 157 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 158 => exact absurd (ZMod.dv_md_zero 158 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 159 => exact absurd (ZMod.dv_md_zero 159 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 160 => exact absurd (ZMod.dv_md_zero 160 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 161 => exact absurd (ZMod.dv_md_zero 161 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 162 => exact Or.inr rfl
    | e' + 163 =>
      exfalso
      cases q with
      | zero => exact Nat.noConfusion (hq.trans (Nat.zero_mul _))
      | succ q' =>
        have : 164 ≤ 163 := by
          calc 164 ≤ 164 + (e' + q' * (e' + 163 + 1)) := Nat.le_add_right _ _
            _ = (q' + 1) * (e' + 163 + 1) := by lib_nat
            _ = 163 := hq.symm
        exact Nat.not_succ_le_self 163 this

theorem pw_neg_one : ∀ k, Frobenius.pw (ZMod.ring (2 * 80 + 1 + 1)) ((ZMod.ring (2 * 80 + 1 + 1)).neg (ZMod.ring (2 * 80 + 1 + 1)).one) (2 * k + 1) = (ZMod.ring (2 * 80 + 1 + 1)).neg (ZMod.ring (2 * 80 + 1 + 1)).one
  | 0 => by show (ZMod.ring (2 * 80 + 1 + 1)).mul (ZMod.ring (2 * 80 + 1 + 1)).one ((ZMod.ring (2 * 80 + 1 + 1)).neg (ZMod.ring (2 * 80 + 1 + 1)).one) = _; lib_ring
  | k + 1 => by
    show (ZMod.ring (2 * 80 + 1 + 1)).mul ((ZMod.ring (2 * 80 + 1 + 1)).mul (Frobenius.pw (ZMod.ring (2 * 80 + 1 + 1)) ((ZMod.ring (2 * 80 + 1 + 1)).neg (ZMod.ring (2 * 80 + 1 + 1)).one) (2 * k + 1)) ((ZMod.ring (2 * 80 + 1 + 1)).neg (ZMod.ring (2 * 80 + 1 + 1)).one)) ((ZMod.ring (2 * 80 + 1 + 1)).neg (ZMod.ring (2 * 80 + 1 + 1)).one) = _
    rw [pw_neg_one k]; lib_ring

theorem chi_m1 : chi 80 ((ZMod.ring (2 * 80 + 1 + 1)).neg (ZMod.ring (2 * 80 + 1 + 1)).one) = -1 :=
  chi_neg_of 80 _ (PencilClosed.negnz 80 _ (PowerSum.one_ne_zero' (2 * 80 + 1))) (by rw [show 80 + 1 = 2 * 40 + 1 from rfl, pw_neg_one 40]; exact ne_pm 80)

theorem chi_neg (x : Zp (2 * 80 + 1 + 1)) : chi 80 ((ZMod.ring (2 * 80 + 1 + 1)).neg x) = -chi 80 x := by
  rw [show (ZMod.ring (2 * 80 + 1 + 1)).neg x = (ZMod.ring (2 * 80 + 1 + 1)).mul ((ZMod.ring (2 * 80 + 1 + 1)).neg (ZMod.ring (2 * 80 + 1 + 1)).one) x by lib_ring, chi_mul 80 prime163, chi_m1]; lib_int

def rho (z : Int × Int) : Int := z.1 + 82 * z.2

def cr (z : Int × Int) : Zp (2 * 80 + 1 + 1) := castZ (ZMod.ring (2 * 80 + 1 + 1)) (rho z)

theorem rho_mul (x y : Int × Int) : rho ((Ring163.ring163 LibInt.intR).mul x y) = rho x * rho y + -(163 * (41 * (x.2 * y.2))) := by
  show x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y  -- (+222 chars; full line in the Zenodo source)
  lib_int

theorem cz163 (w : Int) : castZ (ZMod.ring (2 * 80 + 1 + 1)) (163 * w) = (ZMod.ring (2 * 80 + 1 + 1)).zero := by
  rw [HasseDavenport.castZ_mul, show castZ (ZMod.ring (2 * 80 + 1 + 1)) 163 = (ZMod.ring (2 * 80 + 1 + 1)).zero from rfl]; lib_ring

theorem cr_mul (x y : Int × Int) : cr ((Ring163.ring163 LibInt.intR).mul x y) = (ZMod.ring (2 * 80 + 1 + 1)).mul (cr x) (cr y) := by
  unfold cr; rw [rho_mul, IntCast.castZ_add, HasseDavenport.castZ_mul, HasseDavenport.castZ_neg, cz163]; lib_ring

theorem cr_neg (z : Int × Int) : cr (-z.1, -z.2) = (ZMod.ring (2 * 80 + 1 + 1)).neg (cr z) := by
  unfold cr; rw [show rho (-z.1, -z.2) = -rho z by show -z.1 + 82 * -z.2 = -(z.1 + 82 * z.2); lib_int, HasseDavenport.castZ_neg]

def Prim163 (z : Int × Int) : Prop := chi 80 (cr z) = 1

instance (z : Int × Int) : Decidable (Prim163 z) := inferInstanceAs (Decidable (_ = _))

theorem prim_mul (x y : Int × Int) (hx : Prim163 x) (hy : Prim163 y) : Prim163 ((Ring163.ring163 LibInt.intR).mul x y) := by
  show chi 80 (cr _) = 1
  rw [cr_mul, chi_mul 80 prime163, show chi 80 (cr x) = 1 from hx, show chi 80 (cr y) = 1 from hy]; rfl

theorem prim_cj (z : Int × Int) (hz : Prim163 z) : Prim163 (cj z) := by
  have e : cr (cj z) = cr z := by
    unfold cr
    rw [show rho (cj z) = rho z + 163 * -z.2 by show z.1 + z.2 + 82 * -z.2 = z.1 + 82 * z.2 + 163 * -z.2; lib_int, IntCast.castZ_add, cz163]; lib_ring
  show chi 80 (cr (cj z)) = 1; rw [e]; exact hz

theorem unit_pm (u : Int × Int) (h : en u = 1) : u = (Ring163.ring163 LibInt.intR).one ∨ u = (-1, 0) := by
  have h4 := four_nrm u
  have hn : nrm u = 1 := Int.ofNat.inj (by rw [nrm_cast, h]; rfl)
  rw [hn] at h4
  have hB : Int.natAbs u.2 * Int.natAbs u.2 = 0 := by
    cases hb : Int.natAbs u.2 * Int.natAbs u.2 with
    | zero => rfl
    | succ t => rw [hb, show Int.natAbs (2 * u.1 + u.2) * Int.natAbs (2 * u.1 + u.2) + 163 * (t + 1) = (Int.natAbs (2 * u.1 + u.2) * Int.natAbs (2 * u.1 + u.2) + 163 * t + 159) + 4 by lib_nat] at h4
                exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj h4))))
  have b0 : u.2 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ hB with | inl e => exact e | inr e => exact e)
  rw [hB] at h4
  have hT : Int.natAbs (2 * u.1 + u.2) * Int.natAbs (2 * u.1 + u.2) = 2 * 2 := h4.symm
  have hT2 : Int.natAbs (2 * u.1 + u.2) = 2 := by
    match hx : Int.natAbs (2 * u.1 + u.2) with
    | 0 => rw [hx] at hT; exact Nat.noConfusion hT
    | 1 => rw [hx] at hT; exact Nat.noConfusion (Nat.succ.inj hT)
    | 2 => rfl
    | t + 3 => rw [hx, show (t + 3) * (t + 3) = (t * t + 6 * t + 5) + 4 by lib_nat] at hT; exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj hT))))
  rw [b0] at hT2
  obtain ⟨a, b⟩ := u
  show (a, b) = (1, 0) ∨ (a, b) = (-1, 0)
  have b0' : b = 0 := b0
  rcases EisEuclid.sgn (2 * a + 0) with s | s <;> rw [hT2] at s
  · exact Or.inl (Prod.ext (W36Mul.two_cancel_int a 1 (by rw [show 2 * a = 2 * a + 0 by lib_int, s]; rfl)) b0')
  · exact Or.inr (Prod.ext (W36Mul.two_cancel_int a (-1) (by rw [show 2 * a = 2 * a + 0 by lib_int, s]; rfl)) b0')

theorem neg_mul' (z : Int × Int) : (Ring163.ring163 LibInt.intR).mul (-1, 0) z = (-z.1, -z.2) :=
  Prod.ext (by show -1 * z.1 + -(0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2  -- (+113 chars; full line in the Zenodo source)

/-- Generic factorization: (a + c b)(a − c' b) + D t b² = a² + ab + t b² when c = c' + 1 and D t = c c' + t, with D | a + c b. -/
theorem fac_gen (a b c c' D t q : Int) (h1 : c = c' + 1) (h2 : D * t = c * c' + t) (hq : a + c * b = D * q) :
    a * a + a * b + t * (b * b) = D * (q * (a + -(c' * b)) + t * (b * b)) := by
  calc a * a + a * b + t * (b * b) = (a + c * b) * (a + -(c' * b)) + (c * c' + t) * (b * b) + (c' + 1 + -c) * (a * b) := by lib_int
    _ = D * q * (a + -(c' * b)) + D * t * (b * b) + (c' + 1 + -c) * (a * b) := by rw [hq, h2]
    _ = D * q * (a + -(c' * b)) + D * t * (b * b) := by rw [h1]; lib_int
    _ = _ := by lib_int

theorem cr_ne (α : Int × Int) (hd : ∀ k, en α ≠ 163 * k) : cr α ≠ (ZMod.ring (2 * 80 + 1 + 1)).zero := fun h0 => by
  obtain ⟨q, hq⟩ := CubicTwo.cast_zero_gen (2 * 80 + 1 + 1) (rho α) h0
  obtain ⟨a, b⟩ := α
  exact hd _ (fac_gen a b 82 81 163 41 q rfl rfl hq)

/-- 163 ∤ N(α) ⇒ ±α ∈ H163. -/
theorem normalize (α : Int × Int) (hd : ∀ k, en α ≠ 163 * k) : ∃ u, en u = 1 ∧ Prim163 ((Ring163.ring163 LibInt.intR).mul u α) := by
  rcases GaussQR.chi_pm 80 _ (cr_ne α hd) with h | h
  · exact ⟨(Ring163.ring163 LibInt.intR).one, rfl, by rw [(Ring163.ring163 LibInt.intR).one_mul]; exact h⟩
  · refine ⟨(-1, 0), rfl, ?_⟩
    show chi 80 (cr _) = 1
    rw [neg_mul', cr_neg, chi_neg, h]; rfl

theorem unit_one (u α : Int × Int) (hu : en u = 1) (pα : Prim163 α) (pu : Prim163 ((Ring163.ring163 LibInt.intR).mul u α)) : u = (Ring163.ring163 LibInt.intR).one := by
  rcases unit_pm u hu with e0 | e0
  · exact e0
  exfalso
  rw [e0, neg_mul'] at pu
  have h1 : chi 80 (cr α) = 1 := pα
  have h2 : chi 80 (cr (-α.1, -α.2)) = 1 := pu
  rw [cr_neg, chi_neg, h1] at h2
  exact Int.noConfusion h2

end Prim163

#print axioms Prim163.prime163
#print axioms Prim163.pw_neg_one
#print axioms Prim163.chi_m1
#print axioms Prim163.chi_neg
#print axioms Prim163.rho_mul
#print axioms Prim163.cz163
#print axioms Prim163.cr_mul
#print axioms Prim163.fac_gen
#print axioms Prim163.cr_neg
#print axioms Prim163.prim_mul
#print axioms Prim163.prim_cj
#print axioms Prim163.unit_pm
#print axioms Prim163.neg_mul'
#print axioms Prim163.fac_gen
#print axioms Prim163.cr_ne
#print axioms Prim163.normalize
#print axioms Prim163.unit_one

lang/Prim19.lean

lean_all/lang/Prim19.lean

/-
  lang/Prim19 — T48/T50: **the primary class** in ℤ[θ], θ = (1 + √−19)/2: ρ(a + bθ) = a + 10b is the reduction mod √−19;
  H19 = {ρ a nonzero square mod 19}. Closed under products and conjugation, ±α normalisation (19 ∤ N), unit uniqueness.
  (Generated, scratchpad genprim.py.)
  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.Cop19
import lang.Sq2Prim8
import lang.W36Mul

namespace Prim19

open FTAChain DetChain LibInt Euc19

def rho (z : Int × Int) : Int := z.1 + 10 * z.2

def Prim19 (z : Int × Int) : Prop := ∃ e : Int, rho z = 19 * e + 1 ∨ rho z = 19 * e + 4 ∨ rho z = 19 * e + 5 ∨ rho z = 19 * e + 6 ∨ rho z = 19 * e + 7 ∨ rho z = 19 * e + 9 ∨ rho z = 19 * e + 11 ∨ rho z = 19 * e + 16 ∨ rho z = 19 * e + 17

theorem rho_mul (x y : Int × Int) : rho ((Ring19.ring19 LibInt.intR).mul x y) = rho x * rho y + -(95 * (x.2 * y.2)) := by
  show x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2) + 10 * (x.1 * y.2 + x.2 * y.1 + x.2 * y.2) = (x.1 + 10 * x.2) * (y.1 + 10 * y.2) + -(95 * (x.2 * y.2))
  lib_int

theorem rho_neg (z : Int × Int) : rho (-z.1, -z.2) = -rho z := by
  show -z.1 + 10 * -z.2 = -(z.1 + 10 * z.2); lib_int

theorem mulgenD (u v e f w r s D : Int) (hx : u = D * e + r) (hy : v = D * f + s) :
    u * v + -(5 * D * w) = D * (D * e * f + e * s + r * f + -(5 * w)) + r * s := by
  rw [hx, hy]; lib_int

theorem mulgen (u v e f w r s : Int) (hx : u = 19 * e + r) (hy : v = 19 * f + s) :
    u * v + -(95 * w) = 19 * (19 * e * f + e * s + r * f + -(5 * w)) + r * s :=
  mulgenD u v e f w r s 19 hx hy

set_option maxHeartbeats 0 in
theorem mul_0 (u v e f w : Int) (hx : u = 19 * e + 1) (hy : v = 19 * f + 1 ∨ v = 19 * f + 4 ∨ v = 19 * f + 5 ∨ v = 19 * f + 6 ∨ v = 19 * f + 7 ∨ v = 19 * f + 9 ∨ v = 19 * f + 11 ∨ v = 19 * f + 16 ∨ v = 19 * f + 17) :
    ∃ E : Int, u * v + -(95 * w) = 19 * E + 1 ∨ u * v + -(95 * w) = 19 * E + 4 ∨ u * v + -(95 * w) = 19 * E + 5 ∨ u * v + -(95 * w) = 19 * E + 6 ∨ u * v + -(95 * w) = 19 * E + 7 ∨ u * v + -(95 * w) = 19 * E + 9 ∨ u * v + -(95 * w) = 19 * E + 11 ∨ u * v + -(95 * w) = 19 * E + 16 ∨ u * v + -(95 * w) = 19 * E + 17 := by
  rcases hy with hy | hy | hy | hy | hy | hy | hy | hy | hy
  · exact ⟨19 * e * f + e * 1 + 1 * f + -(5 * w) + 0, (Or.inl (by rw [mulgen u v e f w 1 1 hx hy]; generalize 19 * e * f + e * 1 + 1 * f + -(5 * w) = X; lib_int))⟩
  · exact ⟨19 * e * f + e * 4 + 1 * f + -(5 * w) + 0, (Or.inr (Or.inl (by rw [mulgen u v e f w 1 4 hx hy]; generalize 19 * e * f + e * 4 + 1 * f + -(5 * w) = X; lib_int)))⟩
  · exact ⟨19 * e * f + e * 5 + 1 * f + -(5 * w) + 0, (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 1 5 hx hy]; generalize 19 * e * f + e * 5 + 1 * f + -(5 * w) = X; lib_int))))⟩
  · exact ⟨19 * e * f + e * 6 + 1 * f + -(5 * w) + 0, (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 1 6 hx hy]; generalize 19 * e * f + e * 6 + 1 * f + -(5 * w) = X; lib_int)))))⟩
  · exact ⟨19 * e * f + e * 7 + 1 * f + -(5 * w) + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 1 7 hx hy]; generalize 19 * e * f + e * 7 + 1 * f + -(5 * w) = X; lib_int))))))⟩
  · exact ⟨19 * e * f + e * 9 + 1 * f + -(5 * w) + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 1 9 hx hy]; generalize 19 * e * f + e * 9 + 1 * f + -(5 * w) = X; lib_int)))))))⟩
  · exact ⟨19 * e * f + e * 11 + 1 * f + -(5 * w) + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 1 11 hx hy]; generalize 19 * e * f + e * 11 + 1 * f + -(5 * w) = X; lib_int))))))))⟩
  · exact ⟨19 * e * f + e * 16 + 1 * f + -(5 * w) + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 1 16 hx hy]; generalize 19 * e * f + e * 16 + 1 * f + -(5 * w) = X; lib_int)))))))))⟩
  · exact ⟨19 * e * f + e * 17 + 1 * f + -(5 * w) + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [mulgen u v e f w 1 17 hx hy]; generalize 19 * e * f + e * 17 + 1 * f + -(5 * w) = X; lib_int)))))))))⟩

set_option maxHeartbeats 0 in
theorem mul_1 (u v e f w : Int) (hx : u = 19 * e + 4) (hy : v = 19 * f + 1 ∨ v = 19 * f + 4 ∨ v = 19 * f + 5 ∨ v = 19 * f + 6 ∨ v = 19 * f + 7 ∨ v = 19 * f + 9 ∨ v = 19 * f + 11 ∨ v = 19 * f + 16 ∨ v = 19 * f + 17) :
    ∃ E : Int, u * v + -(95 * w) = 19 * E + 1 ∨ u * v + -(95 * w) = 19 * E + 4 ∨ u * v + -(95 * w) = 19 * E + 5 ∨ u * v + -(95 * w) = 19 * E + 6 ∨ u * v + -(95 * w) = 19 * E + 7 ∨ u * v + -(95 * w) = 19 * E + 9 ∨ u * v + -(95 * w) = 19 * E + 11 ∨ u * v + -(95 * w) = 19 * E + 16 ∨ u * v + -(95 * w) = 19 * E + 17 := by
  rcases hy with hy | hy | hy | hy | hy | hy | hy | hy | hy
  · exact ⟨19 * e * f + e * 1 + 4 * f + -(5 * w) + 0, (Or.inr (Or.inl (by rw [mulgen u v e f w 4 1 hx hy]; generalize 19 * e * f + e * 1 + 4 * f + -(5 * w) = X; lib_int)))⟩
  · exact ⟨19 * e * f + e * 4 + 4 * f + -(5 * w) + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 4 4 hx hy]; generalize 19 * e * f + e * 4 + 4 * f + -(5 * w) = X; lib_int)))))))))⟩
  · exact ⟨19 * e * f + e * 5 + 4 * f + -(5 * w) + 1, (Or.inl (by rw [mulgen u v e f w 4 5 hx hy]; generalize 19 * e * f + e * 5 + 4 * f + -(5 * w) = X; lib_int))⟩
  · exact ⟨19 * e * f + e * 6 + 4 * f + -(5 * w) + 1, (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 4 6 hx hy]; generalize 19 * e * f + e * 6 + 4 * f + -(5 * w) = X; lib_int))))⟩
  · exact ⟨19 * e * f + e * 7 + 4 * f + -(5 * w) + 1, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 4 7 hx hy]; generalize 19 * e * f + e * 7 + 4 * f + -(5 * w) = X; lib_int)))))))⟩
  · exact ⟨19 * e * f + e * 9 + 4 * f + -(5 * w) + 1, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [mulgen u v e f w 4 9 hx hy]; generalize 19 * e * f + e * 9 + 4 * f + -(5 * w) = X; lib_int)))))))))⟩
  · exact ⟨19 * e * f + e * 11 + 4 * f + -(5 * w) + 2, (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 4 11 hx hy]; generalize 19 * e * f + e * 11 + 4 * f + -(5 * w) = X; lib_int)))))⟩
  · exact ⟨19 * e * f + e * 16 + 4 * f + -(5 * w) + 3, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 4 16 hx hy]; generalize 19 * e * f + e * 16 + 4 * f + -(5 * w) = X; lib_int))))))⟩
  · exact ⟨19 * e * f + e * 17 + 4 * f + -(5 * w) + 3, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 4 17 hx hy]; generalize 19 * e * f + e * 17 + 4 * f + -(5 * w) = X; lib_int))))))))⟩

set_option maxHeartbeats 0 in
theorem mul_2 (u v e f w : Int) (hx : u = 19 * e + 5) (hy : v = 19 * f + 1 ∨ v = 19 * f + 4 ∨ v = 19 * f + 5 ∨ v = 19 * f + 6 ∨ v = 19 * f + 7 ∨ v = 19 * f + 9 ∨ v = 19 * f + 11 ∨ v = 19 * f + 16 ∨ v = 19 * f + 17) :
    ∃ E : Int, u * v + -(95 * w) = 19 * E + 1 ∨ u * v + -(95 * w) = 19 * E + 4 ∨ u * v + -(95 * w) = 19 * E + 5 ∨ u * v + -(95 * w) = 19 * E + 6 ∨ u * v + -(95 * w) = 19 * E + 7 ∨ u * v + -(95 * w) = 19 * E + 9 ∨ u * v + -(95 * w) = 19 * E + 11 ∨ u * v + -(95 * w) = 19 * E + 16 ∨ u * v + -(95 * w) = 19 * E + 17 := by
  rcases hy with hy | hy | hy | hy | hy | hy | hy | hy | hy
  · exact ⟨19 * e * f + e * 1 + 5 * f + -(5 * w) + 0, (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 5 1 hx hy]; generalize 19 * e * f + e * 1 + 5 * f + -(5 * w) = X; lib_int))))⟩
  · exact ⟨19 * e * f + e * 4 + 5 * f + -(5 * w) + 1, (Or.inl (by rw [mulgen u v e f w 5 4 hx hy]; generalize 19 * e * f + e * 4 + 5 * f + -(5 * w) = X; lib_int))⟩
  · exact ⟨19 * e * f + e * 5 + 5 * f + -(5 * w) + 1, (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 5 5 hx hy]; generalize 19 * e * f + e * 5 + 5 * f + -(5 * w) = X; lib_int)))))⟩
  · exact ⟨19 * e * f + e * 6 + 5 * f + -(5 * w) + 1, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 5 6 hx hy]; generalize 19 * e * f + e * 6 + 5 * f + -(5 * w) = X; lib_int))))))))⟩
  · exact ⟨19 * e * f + e * 7 + 5 * f + -(5 * w) + 1, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 5 7 hx hy]; generalize 19 * e * f + e * 7 + 5 * f + -(5 * w) = X; lib_int)))))))))⟩
  · exact ⟨19 * e * f + e * 9 + 5 * f + -(5 * w) + 2, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 5 9 hx hy]; generalize 19 * e * f + e * 9 + 5 * f + -(5 * w) = X; lib_int))))))⟩
  · exact ⟨19 * e * f + e * 11 + 5 * f + -(5 * w) + 2, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [mulgen u v e f w 5 11 hx hy]; generalize 19 * e * f + e * 11 + 5 * f + -(5 * w) = X; lib_int)))))))))⟩
  · exact ⟨19 * e * f + e * 16 + 5 * f + -(5 * w) + 4, (Or.inr (Or.inl (by rw [mulgen u v e f w 5 16 hx hy]; generalize 19 * e * f + e * 16 + 5 * f + -(5 * w) = X; lib_int)))⟩
  · exact ⟨19 * e * f + e * 17 + 5 * f + -(5 * w) + 4, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 5 17 hx hy]; generalize 19 * e * f + e * 17 + 5 * f + -(5 * w) = X; lib_int)))))))⟩

set_option maxHeartbeats 0 in
theorem mul_3 (u v e f w : Int) (hx : u = 19 * e + 6) (hy : v = 19 * f + 1 ∨ v = 19 * f + 4 ∨ v = 19 * f + 5 ∨ v = 19 * f + 6 ∨ v = 19 * f + 7 ∨ v = 19 * f + 9 ∨ v = 19 * f + 11 ∨ v = 19 * f + 16 ∨ v = 19 * f + 17) :
    ∃ E : Int, u * v + -(95 * w) = 19 * E + 1 ∨ u * v + -(95 * w) = 19 * E + 4 ∨ u * v + -(95 * w) = 19 * E + 5 ∨ u * v + -(95 * w) = 19 * E + 6 ∨ u * v + -(95 * w) = 19 * E + 7 ∨ u * v + -(95 * w) = 19 * E + 9 ∨ u * v + -(95 * w) = 19 * E + 11 ∨ u * v + -(95 * w) = 19 * E + 16 ∨ u * v + -(95 * w) = 19 * E + 17 := by
  rcases hy with hy | hy | hy | hy | hy | hy | hy | hy | hy
  · exact ⟨19 * e * f + e * 1 + 6 * f + -(5 * w) + 0, (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 6 1 hx hy]; generalize 19 * e * f + e * 1 + 6 * f + -(5 * w) = X; lib_int)))))⟩
  · exact ⟨19 * e * f + e * 4 + 6 * f + -(5 * w) + 1, (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 6 4 hx hy]; generalize 19 * e * f + e * 4 + 6 * f + -(5 * w) = X; lib_int))))⟩
  · exact ⟨19 * e * f + e * 5 + 6 * f + -(5 * w) + 1, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 6 5 hx hy]; generalize 19 * e * f + e * 5 + 6 * f + -(5 * w) = X; lib_int))))))))⟩
  · exact ⟨19 * e * f + e * 6 + 6 * f + -(5 * w) + 1, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [mulgen u v e f w 6 6 hx hy]; generalize 19 * e * f + e * 6 + 6 * f + -(5 * w) = X; lib_int)))))))))⟩
  · exact ⟨19 * e * f + e * 7 + 6 * f + -(5 * w) + 2, (Or.inr (Or.inl (by rw [mulgen u v e f w 6 7 hx hy]; generalize 19 * e * f + e * 7 + 6 * f + -(5 * w) = X; lib_int)))⟩
  · exact ⟨19 * e * f + e * 9 + 6 * f + -(5 * w) + 2, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 6 9 hx hy]; generalize 19 * e * f + e * 9 + 6 * f + -(5 * w) = X; lib_int)))))))))⟩
  · exact ⟨19 * e * f + e * 11 + 6 * f + -(5 * w) + 3, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 6 11 hx hy]; generalize 19 * e * f + e * 11 + 6 * f + -(5 * w) = X; lib_int)))))))⟩
  · exact ⟨19 * e * f + e * 16 + 6 * f + -(5 * w) + 5, (Or.inl (by rw [mulgen u v e f w 6 16 hx hy]; generalize 19 * e * f + e * 16 + 6 * f + -(5 * w) = X; lib_int))⟩
  · exact ⟨19 * e * f + e * 17 + 6 * f + -(5 * w) + 5, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 6 17 hx hy]; generalize 19 * e * f + e * 17 + 6 * f + -(5 * w) = X; lib_int))))))⟩

set_option maxHeartbeats 0 in
theorem mul_4 (u v e f w : Int) (hx : u = 19 * e + 7) (hy : v = 19 * f + 1 ∨ v = 19 * f + 4 ∨ v = 19 * f + 5 ∨ v = 19 * f + 6 ∨ v = 19 * f + 7 ∨ v = 19 * f + 9 ∨ v = 19 * f + 11 ∨ v = 19 * f + 16 ∨ v = 19 * f + 17) :
    ∃ E : Int, u * v + -(95 * w) = 19 * E + 1 ∨ u * v + -(95 * w) = 19 * E + 4 ∨ u * v + -(95 * w) = 19 * E + 5 ∨ u * v + -(95 * w) = 19 * E + 6 ∨ u * v + -(95 * w) = 19 * E + 7 ∨ u * v + -(95 * w) = 19 * E + 9 ∨ u * v + -(95 * w) = 19 * E + 11 ∨ u * v + -(95 * w) = 19 * E + 16 ∨ u * v + -(95 * w) = 19 * E + 17 := by
  rcases hy with hy | hy | hy | hy | hy | hy | hy | hy | hy
  · exact ⟨19 * e * f + e * 1 + 7 * f + -(5 * w) + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 7 1 hx hy]; generalize 19 * e * f + e * 1 + 7 * f + -(5 * w) = X; lib_int))))))⟩
  · exact ⟨19 * e * f + e * 4 + 7 * f + -(5 * w) + 1, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 7 4 hx hy]; generalize 19 * e * f + e * 4 + 7 * f + -(5 * w) = X; lib_int)))))))⟩
  · exact ⟨19 * e * f + e * 5 + 7 * f + -(5 * w) + 1, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 7 5 hx hy]; generalize 19 * e * f + e * 5 + 7 * f + -(5 * w) = X; lib_int)))))))))⟩
  · exact ⟨19 * e * f + e * 6 + 7 * f + -(5 * w) + 2, (Or.inr (Or.inl (by rw [mulgen u v e f w 7 6 hx hy]; generalize 19 * e * f + e * 6 + 7 * f + -(5 * w) = X; lib_int)))⟩
  · exact ⟨19 * e * f + e * 7 + 7 * f + -(5 * w) + 2, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 7 7 hx hy]; generalize 19 * e * f + e * 7 + 7 * f + -(5 * w) = X; lib_int))))))))⟩
  · exact ⟨19 * e * f + e * 9 + 7 * f + -(5 * w) + 3, (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 7 9 hx hy]; generalize 19 * e * f + e * 9 + 7 * f + -(5 * w) = X; lib_int)))))⟩
  · exact ⟨19 * e * f + e * 11 + 7 * f + -(5 * w) + 4, (Or.inl (by rw [mulgen u v e f w 7 11 hx hy]; generalize 19 * e * f + e * 11 + 7 * f + -(5 * w) = X; lib_int))⟩
  · exact ⟨19 * e * f + e * 16 + 7 * f + -(5 * w) + 5, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [mulgen u v e f w 7 16 hx hy]; generalize 19 * e * f + e * 16 + 7 * f + -(5 * w) = X; lib_int)))))))))⟩
  · exact ⟨19 * e * f + e * 17 + 7 * f + -(5 * w) + 6, (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 7 17 hx hy]; generalize 19 * e * f + e * 17 + 7 * f + -(5 * w) = X; lib_int))))⟩

set_option maxHeartbeats 0 in
theorem mul_5 (u v e f w : Int) (hx : u = 19 * e + 9) (hy : v = 19 * f + 1 ∨ v = 19 * f + 4 ∨ v = 19 * f + 5 ∨ v = 19 * f + 6 ∨ v = 19 * f + 7 ∨ v = 19 * f + 9 ∨ v = 19 * f + 11 ∨ v = 19 * f + 16 ∨ v = 19 * f + 17) :
    ∃ E : Int, u * v + -(95 * w) = 19 * E + 1 ∨ u * v + -(95 * w) = 19 * E + 4 ∨ u * v + -(95 * w) = 19 * E + 5 ∨ u * v + -(95 * w) = 19 * E + 6 ∨ u * v + -(95 * w) = 19 * E + 7 ∨ u * v + -(95 * w) = 19 * E + 9 ∨ u * v + -(95 * w) = 19 * E + 11 ∨ u * v + -(95 * w) = 19 * E + 16 ∨ u * v + -(95 * w) = 19 * E + 17 := by
  rcases hy with hy | hy | hy | hy | hy | hy | hy | hy | hy
  · exact ⟨19 * e * f + e * 1 + 9 * f + -(5 * w) + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 9 1 hx hy]; generalize 19 * e * f + e * 1 + 9 * f + -(5 * w) = X; lib_int)))))))⟩
  · exact ⟨19 * e * f + e * 4 + 9 * f + -(5 * w) + 1, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [mulgen u v e f w 9 4 hx hy]; generalize 19 * e * f + e * 4 + 9 * f + -(5 * w) = X; lib_int)))))))))⟩
  · exact ⟨19 * e * f + e * 5 + 9 * f + -(5 * w) + 2, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 9 5 hx hy]; generalize 19 * e * f + e * 5 + 9 * f + -(5 * w) = X; lib_int))))))⟩
  · exact ⟨19 * e * f + e * 6 + 9 * f + -(5 * w) + 2, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 9 6 hx hy]; generalize 19 * e * f + e * 6 + 9 * f + -(5 * w) = X; lib_int)))))))))⟩
  · exact ⟨19 * e * f + e * 7 + 9 * f + -(5 * w) + 3, (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 9 7 hx hy]; generalize 19 * e * f + e * 7 + 9 * f + -(5 * w) = X; lib_int)))))⟩
  · exact ⟨19 * e * f + e * 9 + 9 * f + -(5 * w) + 4, (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 9 9 hx hy]; generalize 19 * e * f + e * 9 + 9 * f + -(5 * w) = X; lib_int))))⟩
  · exact ⟨19 * e * f + e * 11 + 9 * f + -(5 * w) + 5, (Or.inr (Or.inl (by rw [mulgen u v e f w 9 11 hx hy]; generalize 19 * e * f + e * 11 + 9 * f + -(5 * w) = X; lib_int)))⟩
  · exact ⟨19 * e * f + e * 16 + 9 * f + -(5 * w) + 7, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 9 16 hx hy]; generalize 19 * e * f + e * 16 + 9 * f + -(5 * w) = X; lib_int))))))))⟩
  · exact ⟨19 * e * f + e * 17 + 9 * f + -(5 * w) + 8, (Or.inl (by rw [mulgen u v e f w 9 17 hx hy]; generalize 19 * e * f + e * 17 + 9 * f + -(5 * w) = X; lib_int))⟩

set_option maxHeartbeats 0 in
theorem mul_6 (u v e f w : Int) (hx : u = 19 * e + 11) (hy : v = 19 * f + 1 ∨ v = 19 * f + 4 ∨ v = 19 * f + 5 ∨ v = 19 * f + 6 ∨ v = 19 * f + 7 ∨ v = 19 * f + 9 ∨ v = 19 * f + 11 ∨ v = 19 * f + 16 ∨ v = 19 * f + 17) :
    ∃ E : Int, u * v + -(95 * w) = 19 * E + 1 ∨ u * v + -(95 * w) = 19 * E + 4 ∨ u * v + -(95 * w) = 19 * E + 5 ∨ u * v + -(95 * w) = 19 * E + 6 ∨ u * v + -(95 * w) = 19 * E + 7 ∨ u * v + -(95 * w) = 19 * E + 9 ∨ u * v + -(95 * w) = 19 * E + 11 ∨ u * v + -(95 * w) = 19 * E + 16 ∨ u * v + -(95 * w) = 19 * E + 17 := by
  rcases hy with hy | hy | hy | hy | hy | hy | hy | hy | hy
  · exact ⟨19 * e * f + e * 1 + 11 * f + -(5 * w) + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 11 1 hx hy]; generalize 19 * e * f + e * 1 + 11 * f + -(5 * w) = X; lib_int))))))))⟩
  · exact ⟨19 * e * f + e * 4 + 11 * f + -(5 * w) + 2, (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 11 4 hx hy]; generalize 19 * e * f + e * 4 + 11 * f + -(5 * w) = X; lib_int)))))⟩
  · exact ⟨19 * e * f + e * 5 + 11 * f + -(5 * w) + 2, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [mulgen u v e f w 11 5 hx hy]; generalize 19 * e * f + e * 5 + 11 * f + -(5 * w) = X; lib_int)))))))))⟩
  · exact ⟨19 * e * f + e * 6 + 11 * f + -(5 * w) + 3, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 11 6 hx hy]; generalize 19 * e * f + e * 6 + 11 * f + -(5 * w) = X; lib_int)))))))⟩
  · exact ⟨19 * e * f + e * 7 + 11 * f + -(5 * w) + 4, (Or.inl (by rw [mulgen u v e f w 11 7 hx hy]; generalize 19 * e * f + e * 7 + 11 * f + -(5 * w) = X; lib_int))⟩
  · exact ⟨19 * e * f + e * 9 + 11 * f + -(5 * w) + 5, (Or.inr (Or.inl (by rw [mulgen u v e f w 11 9 hx hy]; generalize 19 * e * f + e * 9 + 11 * f + -(5 * w) = X; lib_int)))⟩
  · exact ⟨19 * e * f + e * 11 + 11 * f + -(5 * w) + 6, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 11 11 hx hy]; generalize 19 * e * f + e * 11 + 11 * f + -(5 * w) = X; lib_int))))))⟩
  · exact ⟨19 * e * f + e * 16 + 11 * f + -(5 * w) + 9, (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 11 16 hx hy]; generalize 19 * e * f + e * 16 + 11 * f + -(5 * w) = X; lib_int))))⟩
  · exact ⟨19 * e * f + e * 17 + 11 * f + -(5 * w) + 9, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 11 17 hx hy]; generalize 19 * e * f + e * 17 + 11 * f + -(5 * w) = X; lib_int)))))))))⟩

set_option maxHeartbeats 0 in
theorem mul_7 (u v e f w : Int) (hx : u = 19 * e + 16) (hy : v = 19 * f + 1 ∨ v = 19 * f + 4 ∨ v = 19 * f + 5 ∨ v = 19 * f + 6 ∨ v = 19 * f + 7 ∨ v = 19 * f + 9 ∨ v = 19 * f + 11 ∨ v = 19 * f + 16 ∨ v = 19 * f + 17) :
    ∃ E : Int, u * v + -(95 * w) = 19 * E + 1 ∨ u * v + -(95 * w) = 19 * E + 4 ∨ u * v + -(95 * w) = 19 * E + 5 ∨ u * v + -(95 * w) = 19 * E + 6 ∨ u * v + -(95 * w) = 19 * E + 7 ∨ u * v + -(95 * w) = 19 * E + 9 ∨ u * v + -(95 * w) = 19 * E + 11 ∨ u * v + -(95 * w) = 19 * E + 16 ∨ u * v + -(95 * w) = 19 * E + 17 := by
  rcases hy with hy | hy | hy | hy | hy | hy | hy | hy | hy
  · exact ⟨19 * e * f + e * 1 + 16 * f + -(5 * w) + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 16 1 hx hy]; generalize 19 * e * f + e * 1 + 16 * f + -(5 * w) = X; lib_int)))))))))⟩
  · exact ⟨19 * e * f + e * 4 + 16 * f + -(5 * w) + 3, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 16 4 hx hy]; generalize 19 * e * f + e * 4 + 16 * f + -(5 * w) = X; lib_int))))))⟩
  · exact ⟨19 * e * f + e * 5 + 16 * f + -(5 * w) + 4, (Or.inr (Or.inl (by rw [mulgen u v e f w 16 5 hx hy]; generalize 19 * e * f + e * 5 + 16 * f + -(5 * w) = X; lib_int)))⟩
  · exact ⟨19 * e * f + e * 6 + 16 * f + -(5 * w) + 5, (Or.inl (by rw [mulgen u v e f w 16 6 hx hy]; generalize 19 * e * f + e * 6 + 16 * f + -(5 * w) = X; lib_int))⟩
  · exact ⟨19 * e * f + e * 7 + 16 * f + -(5 * w) + 5, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [mulgen u v e f w 16 7 hx hy]; generalize 19 * e * f + e * 7 + 16 * f + -(5 * w) = X; lib_int)))))))))⟩
  · exact ⟨19 * e * f + e * 9 + 16 * f + -(5 * w) + 7, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 16 9 hx hy]; generalize 19 * e * f + e * 9 + 16 * f + -(5 * w) = X; lib_int))))))))⟩
  · exact ⟨19 * e * f + e * 11 + 16 * f + -(5 * w) + 9, (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 16 11 hx hy]; generalize 19 * e * f + e * 11 + 16 * f + -(5 * w) = X; lib_int))))⟩
  · exact ⟨19 * e * f + e * 16 + 16 * f + -(5 * w) + 13, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 16 16 hx hy]; generalize 19 * e * f + e * 16 + 16 * f + -(5 * w) = X; lib_int)))))))⟩
  · exact ⟨19 * e * f + e * 17 + 16 * f + -(5 * w) + 14, (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 16 17 hx hy]; generalize 19 * e * f + e * 17 + 16 * f + -(5 * w) = X; lib_int)))))⟩

set_option maxHeartbeats 0 in
theorem mul_8 (u v e f w : Int) (hx : u = 19 * e + 17) (hy : v = 19 * f + 1 ∨ v = 19 * f + 4 ∨ v = 19 * f + 5 ∨ v = 19 * f + 6 ∨ v = 19 * f + 7 ∨ v = 19 * f + 9 ∨ v = 19 * f + 11 ∨ v = 19 * f + 16 ∨ v = 19 * f + 17) :
    ∃ E : Int, u * v + -(95 * w) = 19 * E + 1 ∨ u * v + -(95 * w) = 19 * E + 4 ∨ u * v + -(95 * w) = 19 * E + 5 ∨ u * v + -(95 * w) = 19 * E + 6 ∨ u * v + -(95 * w) = 19 * E + 7 ∨ u * v + -(95 * w) = 19 * E + 9 ∨ u * v + -(95 * w) = 19 * E + 11 ∨ u * v + -(95 * w) = 19 * E + 16 ∨ u * v + -(95 * w) = 19 * E + 17 := by
  rcases hy with hy | hy | hy | hy | hy | hy | hy | hy | hy
  · exact ⟨19 * e * f + e * 1 + 17 * f + -(5 * w) + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [mulgen u v e f w 17 1 hx hy]; generalize 19 * e * f + e * 1 + 17 * f + -(5 * w) = X; lib_int)))))))))⟩
  · exact ⟨19 * e * f + e * 4 + 17 * f + -(5 * w) + 3, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 17 4 hx hy]; generalize 19 * e * f + e * 4 + 17 * f + -(5 * w) = X; lib_int))))))))⟩
  · exact ⟨19 * e * f + e * 5 + 17 * f + -(5 * w) + 4, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 17 5 hx hy]; generalize 19 * e * f + e * 5 + 17 * f + -(5 * w) = X; lib_int)))))))⟩
  · exact ⟨19 * e * f + e * 6 + 17 * f + -(5 * w) + 5, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 17 6 hx hy]; generalize 19 * e * f + e * 6 + 17 * f + -(5 * w) = X; lib_int))))))⟩
  · exact ⟨19 * e * f + e * 7 + 17 * f + -(5 * w) + 6, (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 17 7 hx hy]; generalize 19 * e * f + e * 7 + 17 * f + -(5 * w) = X; lib_int))))⟩
  · exact ⟨19 * e * f + e * 9 + 17 * f + -(5 * w) + 8, (Or.inl (by rw [mulgen u v e f w 17 9 hx hy]; generalize 19 * e * f + e * 9 + 17 * f + -(5 * w) = X; lib_int))⟩
  · exact ⟨19 * e * f + e * 11 + 17 * f + -(5 * w) + 9, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 17 11 hx hy]; generalize 19 * e * f + e * 11 + 17 * f + -(5 * w) = X; lib_int)))))))))⟩
  · exact ⟨19 * e * f + e * 16 + 17 * f + -(5 * w) + 14, (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 17 16 hx hy]; generalize 19 * e * f + e * 16 + 17 * f + -(5 * w) = X; lib_int)))))⟩
  · exact ⟨19 * e * f + e * 17 + 17 * f + -(5 * w) + 15, (Or.inr (Or.inl (by rw [mulgen u v e f w 17 17 hx hy]; generalize 19 * e * f + e * 17 + 17 * f + -(5 * w) = X; lib_int)))⟩

theorem prim_mul (x y : Int × Int) (hx : Prim19 x) (hy : Prim19 y) : Prim19 ((Ring19.ring19 LibInt.intR).mul x y) := by
  obtain ⟨e, hx⟩ := hx
  obtain ⟨f, hy⟩ := hy
  rw [Prim19, rho_mul]
  generalize x.2 * y.2 = w
  rcases hx with hx | hx | hx | hx | hx | hx | hx | hx | hx
  · exact mul_0 _ _ e f w hx hy
  · exact mul_1 _ _ e f w hx hy
  · exact mul_2 _ _ e f w hx hy
  · exact mul_3 _ _ e f w hx hy
  · exact mul_4 _ _ e f w hx hy
  · exact mul_5 _ _ e f w hx hy
  · exact mul_6 _ _ e f w hx hy
  · exact mul_7 _ _ e f w hx hy
  · exact mul_8 _ _ e f w hx hy

theorem prim_cj (z : Int × Int) (hz : Prim19 z) : Prim19 (cj z) := by
  obtain ⟨e, h⟩ := hz
  have hr : rho (cj z) = rho z + -(19 * z.2) := by show z.1 + z.2 + 10 * -z.2 = z.1 + 10 * z.2 + -(19 * z.2); lib_int
  refine ⟨e + -z.2, ?_⟩
  rw [hr]
  rcases h with h | h | h | h | h | h | h | h | h
  · exact (Or.inl (by rw [h]; lib_int))
  · exact (Or.inr (Or.inl (by rw [h]; lib_int)))
  · exact (Or.inr (Or.inr (Or.inl (by rw [h]; lib_int))))
  · exact (Or.inr (Or.inr (Or.inr (Or.inl (by rw [h]; lib_int)))))
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [h]; lib_int))))))
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [h]; lib_int)))))))
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [h]; lib_int))))))))
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [h]; lib_int)))))))))
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [h]; lib_int)))))))))

theorem unit_pm (u : Int × Int) (h : en u = 1) : u = (Ring19.ring19 LibInt.intR).one ∨ u = (-1, 0) := by
  have h4 := four_nrm u
  have hn : nrm u = 1 := Int.ofNat.inj (by rw [nrm_cast, h]; rfl)
  rw [hn] at h4
  have hB : Int.natAbs u.2 * Int.natAbs u.2 = 0 := by
    cases hb : Int.natAbs u.2 * Int.natAbs u.2 with
    | zero => rfl
    | succ t => rw [hb, show Int.natAbs (2 * u.1 + u.2) * Int.natAbs (2 * u.1 + u.2) + 19 * (t + 1) = (Int.natAbs (2 * u.1 + u.2) * Int.natAbs (2 * u.1 + u.2) + 19 * t + 15) + 4 by lib_nat] at h4
                exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj h4))))
  have b0 : u.2 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ hB with | inl e => exact e | inr e => exact e)
  rw [hB] at h4
  have hT : Int.natAbs (2 * u.1 + u.2) * Int.natAbs (2 * u.1 + u.2) = 2 * 2 := h4.symm
  have hT2 : Int.natAbs (2 * u.1 + u.2) = 2 := by
    match hx : Int.natAbs (2 * u.1 + u.2) with
    | 0 => rw [hx] at hT; exact Nat.noConfusion hT
    | 1 => rw [hx] at hT; exact Nat.noConfusion (Nat.succ.inj hT)
    | 2 => rfl
    | t + 3 => rw [hx, show (t + 3) * (t + 3) = (t * t + 6 * t + 5) + 4 by lib_nat] at hT; exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj hT))))
  rw [b0] at hT2
  obtain ⟨a, b⟩ := u
  show (a, b) = (1, 0) ∨ (a, b) = (-1, 0)
  have b0' : b = 0 := b0
  rcases EisEuclid.sgn (2 * a + 0) with s | s <;> rw [hT2] at s
  · exact Or.inl (Prod.ext (W36Mul.two_cancel_int a 1 (by rw [show 2 * a = 2 * a + 0 by lib_int, s]; rfl)) b0')
  · exact Or.inr (Prod.ext (W36Mul.two_cancel_int a (-1) (by rw [show 2 * a = 2 * a + 0 by lib_int, s]; rfl)) b0')

theorem neg_mul' (z : Int × Int) : (Ring19.ring19 LibInt.intR).mul (-1, 0) z = (-z.1, -z.2) :=
  Prod.ext (by show -1 * z.1 + -(0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2) = -z.1; lib_int) (by show -1 * z.2 + 0 * z.1 + 0 * z.2 = -z.2; lib_int)

set_option maxHeartbeats 0 in
theorem normalize (α : Int × Int) (hd : ∀ k, en α ≠ 19 * k) : ∃ u, en u = 1 ∧ Prim19 ((Ring19.ring19 LibInt.intR).mul u α) := by
  obtain ⟨q, r, hq, hr⟩ := GaussEuclid.rnd (rho α) 18
  have hq' : rho α = 19 * q + r := by rw [hq]; show q * 19 + r = 19 * q + r; lib_int
  have plus : ∀ e, (rho α = 19 * e + 1 ∨ rho α = 19 * e + 4 ∨ rho α = 19 * e + 5 ∨ rho α = 19 * e + 6 ∨ rho α = 19 * e + 7 ∨ rho α = 19 * e + 9 ∨ rho α = 19 * e + 11 ∨ rho α = 19 * e + 16 ∨ rho α = 19 * e + 17) → ∃ u, en u = 1 ∧ Prim19 ((Ring19.ring19 LibInt.intR).mul u α) :=
    fun e he => ⟨(Ring19.ring19 LibInt.intR).one, rfl, by rw [(Ring19.ring19 LibInt.intR).one_mul]; exact ⟨e, he⟩⟩
  have minus : ∀ e, (-rho α = 19 * e + 1 ∨ -rho α = 19 * e + 4 ∨ -rho α = 19 * e + 5 ∨ -rho α = 19 * e + 6 ∨ -rho α = 19 * e + 7 ∨ -rho α = 19 * e + 9 ∨ -rho α = 19 * e + 11 ∨ -rho α = 19 * e + 16 ∨ -rho α = 19 * e + 17) → ∃ u, en u = 1 ∧ Prim19 ((Ring19.ring19 LibInt.intR).mul u α) :=
    fun e he => ⟨(-1, 0), rfl, by rw [neg_mul']; exact ⟨e, by rw [rho_neg]; exact he⟩⟩
  cases r with
  | ofNat t =>
    match t, hr with
    | 0, _ =>
      exfalso
      obtain ⟨a, b⟩ := α
      have ha : a = 19 * q + -(10 * b) := by
        have : a + 10 * b = 19 * q + Int.ofNat 0 := hq'
        calc a = (a + 10 * b) + -(10 * b) := by lib_int
          _ = _ := by rw [this]; show 19 * q + 0 + -(10 * b) = _; lib_int
      exact hd (19 * q * q + -(19 * q * b) + 5 * b * b) (by show a * a + a * b + 5 * (b * b) = _; rw [ha]; lib_int)
    | 1, _ => exact plus q (Or.inl (hq'))
    | 2, _ => exact minus (-q + -1) (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [hq']; show -(19 * q + 2) = _; lib_int)))))))))
    | 3, _ => exact minus (-q + -1) (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [hq']; show -(19 * q + 3) = _; lib_int)))))))))
    | 4, _ => exact plus q (Or.inr (Or.inl (hq')))
    | 5, _ => exact plus q (Or.inr (Or.inr (Or.inl (hq'))))
    | 6, _ => exact plus q (Or.inr (Or.inr (Or.inr (Or.inl (hq')))))
    | 7, _ => exact plus q (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (hq'))))))
    | 8, _ => exact minus (-q + -1) (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [hq']; show -(19 * q + 8) = _; lib_int))))))))
    | 9, _ => exact plus q (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (hq')))))))
    | t + 10, hr' =>
      exfalso
      have : 2 * (t + 10) ≤ 19 := hr'
      rw [show 2 * (t + 10) = 2 * t + 1 + 19 by lib_nat] at this
      exact Nat.not_succ_le_self 19 (Nat.le_trans (Nat.add_le_add_right (Nat.zero_le (2 * t)) 20) (Nat.le_trans (Nat.le_of_eq (by lib_nat)) this))
  | negSucc t =>
    match t, hr with
    | 0, _ => exact minus (-q) (Or.inl (by rw [hq']; show -(19 * q + -1) = _; lib_int))
    | 1, _ => exact plus (q + -1) (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [hq']; show 19 * q + -2 = _; lib_int)))))))))
    | 2, _ => exact plus (q + -1) (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [hq']; show 19 * q + -3 = _; lib_int)))))))))
    | 3, _ => exact minus (-q) (Or.inr (Or.inl (by rw [hq']; show -(19 * q + -4) = _; lib_int)))
    | 4, _ => exact minus (-q) (Or.inr (Or.inr (Or.inl (by rw [hq']; show -(19 * q + -5) = _; lib_int))))
    | 5, _ => exact minus (-q) (Or.inr (Or.inr (Or.inr (Or.inl (by rw [hq']; show -(19 * q + -6) = _; lib_int)))))
    | 6, _ => exact minus (-q) (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [hq']; show -(19 * q + -7) = _; lib_int))))))
    | 7, _ => exact plus (q + -1) (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [hq']; show 19 * q + -8 = _; lib_int))))))))
    | 8, _ => exact minus (-q) (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [hq']; show -(19 * q + -9) = _; lib_int)))))))
    | t + 9, hr' =>
      exfalso
      have : 2 * (t + 10) ≤ 19 := hr'
      rw [show 2 * (t + 10) = 2 * t + 1 + 19 by lib_nat] at this
      exact Nat.not_succ_le_self 19 (Nat.le_trans (Nat.add_le_add_right (Nat.zero_le (2 * t)) 20) (Nat.le_trans (Nat.le_of_eq (by lib_nat)) this))

set_option maxHeartbeats 0 in
theorem one_0 (x e f : Int) (h1 : x = 19 * e + 1) (h2 : -x = 19 * f + 1 ∨ -x = 19 * f + 4 ∨ -x = 19 * f + 5 ∨ -x = 19 * f + 6 ∨ -x = 19 * f + 7 ∨ -x = 19 * f + 9 ∨ -x = 19 * f + 11 ∨ -x = 19 * f + 16 ∨ -x = 19 * f + 17) : False := by
  rcases h2 with h2 | h2 | h2 | h2 | h2 | h2 | h2 | h2 | h2
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 16 1 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 1) + (19 * f + 1) + 17 := by lib_int
        _ = x + -x + 17 := by rw [← h1, ← h2]
        _ = 17 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 13 4 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 1) + (19 * f + 4) + 14 := by lib_int
        _ = x + -x + 14 := by rw [← h1, ← h2]
        _ = 14 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 12 5 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 1) + (19 * f + 5) + 13 := by lib_int
        _ = x + -x + 13 := by rw [← h1, ← h2]
        _ = 13 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 11 6 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 1) + (19 * f + 6) + 12 := by lib_int
        _ = x + -x + 12 := by rw [← h1, ← h2]
        _ = 12 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 10 7 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 1) + (19 * f + 7) + 11 := by lib_int
        _ = x + -x + 11 := by rw [← h1, ← h2]
        _ = 11 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 8 9 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 1) + (19 * f + 9) + 9 := by lib_int
        _ = x + -x + 9 := by rw [← h1, ← h2]
        _ = 9 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 6 11 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 1) + (19 * f + 11) + 7 := by lib_int
        _ = x + -x + 7 := by rw [← h1, ← h2]
        _ = 7 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 1 16 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 1) + (19 * f + 16) + 2 := by lib_int
        _ = x + -x + 2 := by rw [← h1, ← h2]
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 0 17 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 1) + (19 * f + 17) + 1 := by lib_int
        _ = x + -x + 1 := by rw [← h1, ← h2]
        _ = 1 := by lib_int) rfl

set_option maxHeartbeats 0 in
theorem one_1 (x e f : Int) (h1 : x = 19 * e + 4) (h2 : -x = 19 * f + 1 ∨ -x = 19 * f + 4 ∨ -x = 19 * f + 5 ∨ -x = 19 * f + 6 ∨ -x = 19 * f + 7 ∨ -x = 19 * f + 9 ∨ -x = 19 * f + 11 ∨ -x = 19 * f + 16 ∨ -x = 19 * f + 17) : False := by
  rcases h2 with h2 | h2 | h2 | h2 | h2 | h2 | h2 | h2 | h2
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 13 4 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 4) + (19 * f + 1) + 14 := by lib_int
        _ = x + -x + 14 := by rw [← h1, ← h2]
        _ = 14 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 10 7 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 4) + (19 * f + 4) + 11 := by lib_int
        _ = x + -x + 11 := by rw [← h1, ← h2]
        _ = 11 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 9 8 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 4) + (19 * f + 5) + 10 := by lib_int
        _ = x + -x + 10 := by rw [← h1, ← h2]
        _ = 10 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 8 9 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 4) + (19 * f + 6) + 9 := by lib_int
        _ = x + -x + 9 := by rw [← h1, ← h2]
        _ = 9 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 7 10 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 4) + (19 * f + 7) + 8 := by lib_int
        _ = x + -x + 8 := by rw [← h1, ← h2]
        _ = 8 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 5 12 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 4) + (19 * f + 9) + 6 := by lib_int
        _ = x + -x + 6 := by rw [← h1, ← h2]
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 3 14 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 4) + (19 * f + 11) + 4 := by lib_int
        _ = x + -x + 4 := by rw [← h1, ← h2]
        _ = 4 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 17 0 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 4) + (19 * f + 16) + 18 := by lib_int
        _ = x + -x + 18 := by rw [← h1, ← h2]
        _ = 18 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 16 1 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 4) + (19 * f + 17) + 17 := by lib_int
        _ = x + -x + 17 := by rw [← h1, ← h2]
        _ = 17 := by lib_int) rfl

set_option maxHeartbeats 0 in
theorem one_2 (x e f : Int) (h1 : x = 19 * e + 5) (h2 : -x = 19 * f + 1 ∨ -x = 19 * f + 4 ∨ -x = 19 * f + 5 ∨ -x = 19 * f + 6 ∨ -x = 19 * f + 7 ∨ -x = 19 * f + 9 ∨ -x = 19 * f + 11 ∨ -x = 19 * f + 16 ∨ -x = 19 * f + 17) : False := by
  rcases h2 with h2 | h2 | h2 | h2 | h2 | h2 | h2 | h2 | h2
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 12 5 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 5) + (19 * f + 1) + 13 := by lib_int
        _ = x + -x + 13 := by rw [← h1, ← h2]
        _ = 13 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 9 8 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 5) + (19 * f + 4) + 10 := by lib_int
        _ = x + -x + 10 := by rw [← h1, ← h2]
        _ = 10 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 8 9 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 5) + (19 * f + 5) + 9 := by lib_int
        _ = x + -x + 9 := by rw [← h1, ← h2]
        _ = 9 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 7 10 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 5) + (19 * f + 6) + 8 := by lib_int
        _ = x + -x + 8 := by rw [← h1, ← h2]
        _ = 8 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 6 11 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 5) + (19 * f + 7) + 7 := by lib_int
        _ = x + -x + 7 := by rw [← h1, ← h2]
        _ = 7 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 4 13 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 5) + (19 * f + 9) + 5 := by lib_int
        _ = x + -x + 5 := by rw [← h1, ← h2]
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 2 15 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 5) + (19 * f + 11) + 3 := by lib_int
        _ = x + -x + 3 := by rw [← h1, ← h2]
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 16 1 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 5) + (19 * f + 16) + 17 := by lib_int
        _ = x + -x + 17 := by rw [← h1, ← h2]
        _ = 17 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 15 2 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 5) + (19 * f + 17) + 16 := by lib_int
        _ = x + -x + 16 := by rw [← h1, ← h2]
        _ = 16 := by lib_int) rfl

set_option maxHeartbeats 0 in
theorem one_3 (x e f : Int) (h1 : x = 19 * e + 6) (h2 : -x = 19 * f + 1 ∨ -x = 19 * f + 4 ∨ -x = 19 * f + 5 ∨ -x = 19 * f + 6 ∨ -x = 19 * f + 7 ∨ -x = 19 * f + 9 ∨ -x = 19 * f + 11 ∨ -x = 19 * f + 16 ∨ -x = 19 * f + 17) : False := by
  rcases h2 with h2 | h2 | h2 | h2 | h2 | h2 | h2 | h2 | h2
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 11 6 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 6) + (19 * f + 1) + 12 := by lib_int
        _ = x + -x + 12 := by rw [← h1, ← h2]
        _ = 12 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 8 9 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 6) + (19 * f + 4) + 9 := by lib_int
        _ = x + -x + 9 := by rw [← h1, ← h2]
        _ = 9 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 7 10 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 6) + (19 * f + 5) + 8 := by lib_int
        _ = x + -x + 8 := by rw [← h1, ← h2]
        _ = 8 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 6 11 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 6) + (19 * f + 6) + 7 := by lib_int
        _ = x + -x + 7 := by rw [← h1, ← h2]
        _ = 7 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 5 12 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 6) + (19 * f + 7) + 6 := by lib_int
        _ = x + -x + 6 := by rw [← h1, ← h2]
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 3 14 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 6) + (19 * f + 9) + 4 := by lib_int
        _ = x + -x + 4 := by rw [← h1, ← h2]
        _ = 4 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 1 16 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 6) + (19 * f + 11) + 2 := by lib_int
        _ = x + -x + 2 := by rw [← h1, ← h2]
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 15 2 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 6) + (19 * f + 16) + 16 := by lib_int
        _ = x + -x + 16 := by rw [← h1, ← h2]
        _ = 16 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 14 3 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 6) + (19 * f + 17) + 15 := by lib_int
        _ = x + -x + 15 := by rw [← h1, ← h2]
        _ = 15 := by lib_int) rfl

set_option maxHeartbeats 0 in
theorem one_4 (x e f : Int) (h1 : x = 19 * e + 7) (h2 : -x = 19 * f + 1 ∨ -x = 19 * f + 4 ∨ -x = 19 * f + 5 ∨ -x = 19 * f + 6 ∨ -x = 19 * f + 7 ∨ -x = 19 * f + 9 ∨ -x = 19 * f + 11 ∨ -x = 19 * f + 16 ∨ -x = 19 * f + 17) : False := by
  rcases h2 with h2 | h2 | h2 | h2 | h2 | h2 | h2 | h2 | h2
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 10 7 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 7) + (19 * f + 1) + 11 := by lib_int
        _ = x + -x + 11 := by rw [← h1, ← h2]
        _ = 11 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 7 10 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 7) + (19 * f + 4) + 8 := by lib_int
        _ = x + -x + 8 := by rw [← h1, ← h2]
        _ = 8 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 6 11 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 7) + (19 * f + 5) + 7 := by lib_int
        _ = x + -x + 7 := by rw [← h1, ← h2]
        _ = 7 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 5 12 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 7) + (19 * f + 6) + 6 := by lib_int
        _ = x + -x + 6 := by rw [← h1, ← h2]
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 4 13 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 7) + (19 * f + 7) + 5 := by lib_int
        _ = x + -x + 5 := by rw [← h1, ← h2]
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 2 15 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 7) + (19 * f + 9) + 3 := by lib_int
        _ = x + -x + 3 := by rw [← h1, ← h2]
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 0 17 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 7) + (19 * f + 11) + 1 := by lib_int
        _ = x + -x + 1 := by rw [← h1, ← h2]
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 14 3 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 7) + (19 * f + 16) + 15 := by lib_int
        _ = x + -x + 15 := by rw [← h1, ← h2]
        _ = 15 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 13 4 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 7) + (19 * f + 17) + 14 := by lib_int
        _ = x + -x + 14 := by rw [← h1, ← h2]
        _ = 14 := by lib_int) rfl

set_option maxHeartbeats 0 in
theorem one_5 (x e f : Int) (h1 : x = 19 * e + 9) (h2 : -x = 19 * f + 1 ∨ -x = 19 * f + 4 ∨ -x = 19 * f + 5 ∨ -x = 19 * f + 6 ∨ -x = 19 * f + 7 ∨ -x = 19 * f + 9 ∨ -x = 19 * f + 11 ∨ -x = 19 * f + 16 ∨ -x = 19 * f + 17) : False := by
  rcases h2 with h2 | h2 | h2 | h2 | h2 | h2 | h2 | h2 | h2
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 8 9 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 9) + (19 * f + 1) + 9 := by lib_int
        _ = x + -x + 9 := by rw [← h1, ← h2]
        _ = 9 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 5 12 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 9) + (19 * f + 4) + 6 := by lib_int
        _ = x + -x + 6 := by rw [← h1, ← h2]
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 4 13 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 9) + (19 * f + 5) + 5 := by lib_int
        _ = x + -x + 5 := by rw [← h1, ← h2]
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 3 14 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 9) + (19 * f + 6) + 4 := by lib_int
        _ = x + -x + 4 := by rw [← h1, ← h2]
        _ = 4 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 2 15 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 9) + (19 * f + 7) + 3 := by lib_int
        _ = x + -x + 3 := by rw [← h1, ← h2]
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 0 17 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 9) + (19 * f + 9) + 1 := by lib_int
        _ = x + -x + 1 := by rw [← h1, ← h2]
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 17 0 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 9) + (19 * f + 11) + 18 := by lib_int
        _ = x + -x + 18 := by rw [← h1, ← h2]
        _ = 18 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 12 5 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 9) + (19 * f + 16) + 13 := by lib_int
        _ = x + -x + 13 := by rw [← h1, ← h2]
        _ = 13 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 11 6 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 9) + (19 * f + 17) + 12 := by lib_int
        _ = x + -x + 12 := by rw [← h1, ← h2]
        _ = 12 := by lib_int) rfl

set_option maxHeartbeats 0 in
theorem one_6 (x e f : Int) (h1 : x = 19 * e + 11) (h2 : -x = 19 * f + 1 ∨ -x = 19 * f + 4 ∨ -x = 19 * f + 5 ∨ -x = 19 * f + 6 ∨ -x = 19 * f + 7 ∨ -x = 19 * f + 9 ∨ -x = 19 * f + 11 ∨ -x = 19 * f + 16 ∨ -x = 19 * f + 17) : False := by
  rcases h2 with h2 | h2 | h2 | h2 | h2 | h2 | h2 | h2 | h2
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 6 11 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 11) + (19 * f + 1) + 7 := by lib_int
        _ = x + -x + 7 := by rw [← h1, ← h2]
        _ = 7 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 3 14 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 11) + (19 * f + 4) + 4 := by lib_int
        _ = x + -x + 4 := by rw [← h1, ← h2]
        _ = 4 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 2 15 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 11) + (19 * f + 5) + 3 := by lib_int
        _ = x + -x + 3 := by rw [← h1, ← h2]
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 1 16 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 11) + (19 * f + 6) + 2 := by lib_int
        _ = x + -x + 2 := by rw [← h1, ← h2]
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 0 17 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 11) + (19 * f + 7) + 1 := by lib_int
        _ = x + -x + 1 := by rw [← h1, ← h2]
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 17 0 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 11) + (19 * f + 9) + 18 := by lib_int
        _ = x + -x + 18 := by rw [← h1, ← h2]
        _ = 18 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 15 2 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 11) + (19 * f + 11) + 16 := by lib_int
        _ = x + -x + 16 := by rw [← h1, ← h2]
        _ = 16 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 10 7 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 11) + (19 * f + 16) + 11 := by lib_int
        _ = x + -x + 11 := by rw [← h1, ← h2]
        _ = 11 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 9 8 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 11) + (19 * f + 17) + 10 := by lib_int
        _ = x + -x + 10 := by rw [← h1, ← h2]
        _ = 10 := by lib_int) rfl

set_option maxHeartbeats 0 in
theorem one_7 (x e f : Int) (h1 : x = 19 * e + 16) (h2 : -x = 19 * f + 1 ∨ -x = 19 * f + 4 ∨ -x = 19 * f + 5 ∨ -x = 19 * f + 6 ∨ -x = 19 * f + 7 ∨ -x = 19 * f + 9 ∨ -x = 19 * f + 11 ∨ -x = 19 * f + 16 ∨ -x = 19 * f + 17) : False := by
  rcases h2 with h2 | h2 | h2 | h2 | h2 | h2 | h2 | h2 | h2
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 1 16 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 16) + (19 * f + 1) + 2 := by lib_int
        _ = x + -x + 2 := by rw [← h1, ← h2]
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 17 0 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 16) + (19 * f + 4) + 18 := by lib_int
        _ = x + -x + 18 := by rw [← h1, ← h2]
        _ = 18 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 16 1 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 16) + (19 * f + 5) + 17 := by lib_int
        _ = x + -x + 17 := by rw [← h1, ← h2]
        _ = 17 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 15 2 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 16) + (19 * f + 6) + 16 := by lib_int
        _ = x + -x + 16 := by rw [← h1, ← h2]
        _ = 16 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 14 3 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 16) + (19 * f + 7) + 15 := by lib_int
        _ = x + -x + 15 := by rw [← h1, ← h2]
        _ = 15 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 12 5 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 16) + (19 * f + 9) + 13 := by lib_int
        _ = x + -x + 13 := by rw [← h1, ← h2]
        _ = 13 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 10 7 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 16) + (19 * f + 11) + 11 := by lib_int
        _ = x + -x + 11 := by rw [← h1, ← h2]
        _ = 11 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 5 12 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 16) + (19 * f + 16) + 6 := by lib_int
        _ = x + -x + 6 := by rw [← h1, ← h2]
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 4 13 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 16) + (19 * f + 17) + 5 := by lib_int
        _ = x + -x + 5 := by rw [← h1, ← h2]
        _ = 5 := by lib_int) rfl

set_option maxHeartbeats 0 in
theorem one_8 (x e f : Int) (h1 : x = 19 * e + 17) (h2 : -x = 19 * f + 1 ∨ -x = 19 * f + 4 ∨ -x = 19 * f + 5 ∨ -x = 19 * f + 6 ∨ -x = 19 * f + 7 ∨ -x = 19 * f + 9 ∨ -x = 19 * f + 11 ∨ -x = 19 * f + 16 ∨ -x = 19 * f + 17) : False := by
  rcases h2 with h2 | h2 | h2 | h2 | h2 | h2 | h2 | h2 | h2
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 0 17 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 17) + (19 * f + 1) + 1 := by lib_int
        _ = x + -x + 1 := by rw [← h1, ← h2]
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 16 1 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 17) + (19 * f + 4) + 17 := by lib_int
        _ = x + -x + 17 := by rw [← h1, ← h2]
        _ = 17 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 15 2 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 17) + (19 * f + 5) + 16 := by lib_int
        _ = x + -x + 16 := by rw [← h1, ← h2]
        _ = 16 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 14 3 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 17) + (19 * f + 6) + 15 := by lib_int
        _ = x + -x + 15 := by rw [← h1, ← h2]
        _ = 15 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 13 4 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 17) + (19 * f + 7) + 14 := by lib_int
        _ = x + -x + 14 := by rw [← h1, ← h2]
        _ = 14 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 11 6 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 17) + (19 * f + 9) + 12 := by lib_int
        _ = x + -x + 12 := by rw [← h1, ← h2]
        _ = 12 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 9 8 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 17) + (19 * f + 11) + 10 := by lib_int
        _ = x + -x + 10 := by rw [← h1, ← h2]
        _ = 10 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 4 13 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 17) + (19 * f + 16) + 5 := by lib_int
        _ = x + -x + 5 := by rw [← h1, ← h2]
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 3 14 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 17) + (19 * f + 17) + 4 := by lib_int
        _ = x + -x + 4 := by rw [← h1, ← h2]
        _ = 4 := by lib_int) rfl

theorem unit_one (u α : Int × Int) (hu : en u = 1) (pα : Prim19 α) (pu : Prim19 ((Ring19.ring19 LibInt.intR).mul u α)) : u = (Ring19.ring19 LibInt.intR).one := by
  rcases unit_pm u hu with e0 | e0
  · exact e0
  exfalso
  rw [e0, neg_mul'] at pu
  obtain ⟨e, h1⟩ := pα
  obtain ⟨f, h2⟩ := pu
  rw [rho_neg] at h2
  generalize rho α = x at h1 h2
  rcases h1 with h1 | h1 | h1 | h1 | h1 | h1 | h1 | h1 | h1
  · exact one_0 x e f h1 h2
  · exact one_1 x e f h1 h2
  · exact one_2 x e f h1 h2
  · exact one_3 x e f h1 h2
  · exact one_4 x e f h1 h2
  · exact one_5 x e f h1 h2
  · exact one_6 x e f h1 h2
  · exact one_7 x e f h1 h2
  · exact one_8 x e f h1 h2

end Prim19

#print axioms Prim19.rho_mul
#print axioms Prim19.rho_neg
#print axioms Prim19.mulgenD
#print axioms Prim19.mulgen
#print axioms Prim19.mul_0
#print axioms Prim19.mul_1
#print axioms Prim19.mul_2
#print axioms Prim19.mul_3
#print axioms Prim19.mul_4
#print axioms Prim19.mul_5
#print axioms Prim19.mul_6
#print axioms Prim19.mul_7
#print axioms Prim19.mul_8
#print axioms Prim19.prim_mul
#print axioms Prim19.prim_cj
#print axioms Prim19.unit_pm
#print axioms Prim19.neg_mul'
#print axioms Prim19.normalize
#print axioms Prim19.one_0
#print axioms Prim19.one_1
#print axioms Prim19.one_2
#print axioms Prim19.one_3
#print axioms Prim19.one_4
#print axioms Prim19.one_5
#print axioms Prim19.one_6
#print axioms Prim19.one_7
#print axioms Prim19.one_8
#print axioms Prim19.unit_one

lang/Prim32.lean

lean_all/lang/Prim32.lean

/-
  lang/Prim32 — T47, cycle 563: the parametrization of primary Gaussian integers used for 32a:
    φ(e, j) = (s·(2j + 1), 2e),  s = (−1)^(|e| + j),   e ∈ ℤ, j ∈ ℕ.

    sg2, sg2_mod, sg2_pm, sg2_add, sg2_sq, sg2_odd    (−1)^k and its residues mod 4
    phi_prim          φ(e, j) is primary
    phi_norm          N(φ(e, j)) = 4e² + (2j + 1)²
    prim_param        every primary (a, b) is φ(e, j) with b = 2e, |a| = 2j + 1
    two_cancel, phi_inj    φ is injective
  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.HeckePrime
import lang.InertFq

namespace Hecke32

open GaussInt GaussUnique GaussEuclid LibInt LibGcd

def sg2 : Nat → Int
  | 0 => 1
  | k + 1 => -sg2 k

theorem sg2_mod : ∀ k, ∃ t, sg2 k - 1 + 2 * Int.ofNat k = 4 * t
  | 0 => ⟨0, rfl⟩
  | k + 1 => by
    obtain ⟨t, ht⟩ := sg2_mod k
    refine ⟨Int.ofNat k - t, ?_⟩
    show -sg2 k - 1 + 2 * (Int.ofNat k + 1) = _
    have e : sg2 k = 4 * t + 1 - 2 * Int.ofNat k := by
      calc sg2 k = (sg2 k - 1 + 2 * Int.ofNat k) + 1 - 2 * Int.ofNat k := by lib_int
        _ = _ := by rw [ht]
    rw [e]; lib_int

theorem sg2_pm : ∀ k, sg2 k = 1 ∨ sg2 k = -1
  | 0 => Or.inl rfl
  | k + 1 => by
    cases sg2_pm k with
    | inl h => exact Or.inr (by show -sg2 k = -1; rw [h])
    | inr h => exact Or.inl (by show -sg2 k = 1; rw [h]; rfl)

theorem sg2_add (a : Nat) : ∀ b, sg2 (a + b) = sg2 a * sg2 b
  | 0 => by show sg2 a = sg2 a * 1; lib_int
  | b + 1 => by show -sg2 (a + b) = sg2 a * -sg2 b; rw [sg2_add a b]; lib_int

theorem sg2_sq (k : Nat) : sg2 k * sg2 k = 1 := by
  cases sg2_pm k with
  | inl h => rw [h]; rfl
  | inr h => rw [h]; rfl

theorem sg2_odd (j : Nat) : ∃ t, sg2 j * (2 * Int.ofNat j + 1) - 1 = 4 * t := by
  obtain ⟨t, ht⟩ := sg2_mod j
  have e : sg2 j = 4 * t + 1 - 2 * Int.ofNat j := by
    calc sg2 j = (sg2 j - 1 + 2 * Int.ofNat j) + 1 - 2 * Int.ofNat j := by lib_int
      _ = _ := by rw [ht]
  exact ⟨2 * Int.ofNat j * t - Int.ofNat j * Int.ofNat j + t, by rw [e]; lib_int⟩

def phi (e : Int) (j : Nat) : Int × Int := (sg2 (Int.natAbs e + j) * (2 * Int.ofNat j + 1), 2 * e)

theorem phi_prim (e : Int) (j : Nat) : Prim (phi e j) := by
  refine ⟨⟨e, rfl⟩, ?_⟩
  show IDv 4 (sg2 (Int.natAbs e + j) * (2 * Int.ofNat j + 1) + 2 * e - 1)
  rw [sg2_add]
  obtain ⟨t, ht⟩ := sg2_odd j
  have eA : sg2 j * (2 * Int.ofNat j + 1) = 4 * t + 1 := by
    calc _ = (sg2 j * (2 * Int.ofNat j + 1) - 1) + 1 := by lib_int
      _ = _ := by rw [ht]
  obtain ⟨t', ht'⟩ := sg2_mod (Int.natAbs e)
  have eS : sg2 (Int.natAbs e) = 4 * t' + 1 - 2 * Int.ofNat (Int.natAbs e) := by
    calc _ = (sg2 (Int.natAbs e) - 1 + 2 * Int.ofNat (Int.natAbs e)) + 1 - 2 * Int.ofNat (Int.natAbs e) := by lib_int
      _ = _ := by rw [ht']
  have r : sg2 (Int.natAbs e) * sg2 j * (2 * Int.ofNat j + 1) + 2 * e - 1 =
      sg2 (Int.natAbs e) * (sg2 j * (2 * Int.ofNat j + 1)) + 2 * e - 1 := by lib_int
  rw [r, eA, eS]
  cases e with
  | ofNat k => exact ⟨4 * t * t' + t' - 2 * Int.ofNat k * t + t, by
      show (4 * t' + 1 - 2 * Int.ofNat k) * (4 * t + 1) + 2 * Int.ofNat k - 1 = _; lib_int⟩
  | negSucc k =>
    refine ⟨4 * t * t' + t' - t - 2 * Int.ofNat k * t - Int.ofNat k - 1, ?_⟩
    show (4 * t' + 1 - 2 * (Int.ofNat k + 1)) * (4 * t + 1) + 2 * -(Int.ofNat k + 1) - 1 = _
    lib_int

theorem phi_norm (e : Int) (j : Nat) : gn (phi e j) = 4 * (e * e) + (2 * Int.ofNat j + 1) * (2 * Int.ofNat j + 1) := by
  show sg2 (Int.natAbs e + j) * (2 * Int.ofNat j + 1) * (sg2 (Int.natAbs e + j) * (2 * Int.ofNat j + 1)) + 2 * e * (2 * e) = _
  have h := sg2_sq (Int.natAbs e + j)
  generalize sg2 (Int.natAbs e + j) = s at h
  calc _ = (s * s) * ((2 * Int.ofNat j + 1) * (2 * Int.ofNat j + 1)) + 4 * (e * e) := by lib_int
    _ = _ := by rw [h]; lib_int

theorem natAbs_sg2 (k j : Nat) : Int.natAbs (sg2 k * (2 * Int.ofNat j + 1)) = 2 * j + 1 := by
  cases sg2_pm k with
  | inl h => rw [h, show (1 : Int) * (2 * Int.ofNat j + 1) = 2 * Int.ofNat j + 1 by lib_int]; rfl
  | inr h => rw [h, show (-1 : Int) * (2 * Int.ofNat j + 1) = -(2 * Int.ofNat j + 1) by lib_int]; rfl

/-- Every primary (a, b) is φ(e, j) with b = 2e, |a| = 2j + 1. -/
theorem prim_param (a b : Int) (h : Prim (a, b)) : ∃ e j, phi e j = (a, b) := by
  obtain ⟨w, c, ha, hb⟩ := HeckePrime.odd_a a b h
  have hP : IDv 4 (a + b - 1) := h.2
  have key : ∀ j : Nat, (a = 2 * Int.ofNat j + 1 ∨ a = -(2 * Int.ofNat j + 1)) → ∃ e j, phi e j = (a, b) := by
    intro j hj
    refine ⟨c, j, ?_⟩
    have hX := (phi_prim c j).2
    obtain ⟨t1, h1⟩ := hX
    obtain ⟨t2, h2⟩ := hP
    have h1' : sg2 (Int.natAbs c + j) * (2 * Int.ofNat j + 1) + 2 * c - 1 = 4 * t1 := h1
    rw [hb] at h2
    show (sg2 (Int.natAbs c + j) * (2 * Int.ofNat j + 1), 2 * c) = (a, b)
    rw [hb]
    have hs := sg2_pm (Int.natAbs c + j)
    cases hs with
    | inl hs1 =>
      rw [hs1] at h1' ⊢
      cases hj with
      | inl ha1 => rw [ha1]; try exact congrArg (fun x => (x, 2 * c)) (by lib_int)
      | inr ha1 =>
        rw [ha1] at h2
        exact absurd (show 4 * Int.ofNat j + 3 - 1 = 4 * (t1 - t2) by
          calc _ = (1 * (2 * Int.ofNat j + 1) + 2 * c - 1) - (-(2 * Int.ofNat j + 1) + 2 * c - 1) := by lib_int
            _ = _ := by rw [h1', h2]; lib_int) (fun hh => InertFq.odd_ne_even4 _ _ hh)
    | inr hs1 =>
      rw [hs1] at h1' ⊢
      cases hj with
      | inr ha1 => rw [ha1]; try exact congrArg (fun x => (x, 2 * c)) (by lib_int)
      | inl ha1 =>
        rw [ha1] at h2
        exact absurd (show 4 * Int.ofNat j + 3 - 1 = 4 * (t2 - t1) by
          calc _ = (2 * Int.ofNat j + 1 + 2 * c - 1) - (-1 * (2 * Int.ofNat j + 1) + 2 * c - 1) := by lib_int
            _ = _ := by rw [h1', h2]; lib_int) (fun hh => InertFq.odd_ne_even4 _ _ hh)
  cases w with
  | ofNat t => exact key t (Or.inl (by rw [ha]))
  | negSucc t => exact key t (Or.inr (by rw [ha]; show 2 * -(Int.ofNat t + 1) + 1 = _; lib_int))

theorem two_cancel (c e : Int) (h : 2 * c = 2 * e) : c = e := by
  have hd : 2 * (c - e) = 0 := by rw [show 2 * (c - e) = 2 * c - 2 * e by lib_int, h]; lib_int
  have hz : c - e = 0 := by
    generalize c - e = d at hd
    cases d with
    | ofNat k =>
      cases k with
      | zero => rfl
      | succ k => exact absurd (Int.ofNat.inj (show Int.ofNat (2 * (k + 1)) = Int.ofNat 0 from hd)) (fun hh => Nat.noConfusion (show 2 * k + 1 + 1 = 0 by rw [← hh]; lib_nat))
    | negSucc k => exact absurd hd (fun hh => Int.noConfusion (show Int.negSucc (2 * k + 1) = Int.ofNat 0 from hh))
  calc c = (c - e) + e := by lib_int
    _ = e := by rw [hz]; lib_int

theorem phi_inj (e e' : Int) (j j' : Nat) (h : phi e j = phi e' j') : e = e' ∧ j = j' := by
  have h1 : sg2 (Int.natAbs e + j) * (2 * Int.ofNat j + 1) = sg2 (Int.natAbs e' + j') * (2 * Int.ofNat j' + 1) := congrArg Prod.fst h
  have h2 : 2 * e = 2 * e' := congrArg Prod.snd h
  refine ⟨two_cancel e e' h2, ?_⟩
  have h3 := congrArg Int.natAbs h1
  rw [natAbs_sg2, natAbs_sg2] at h3
  have h4 : 2 * j = 2 * j' := Nat.succ.inj h3
  exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ 1) h4

end Hecke32

#print axioms Hecke32.sg2_mod
#print axioms Hecke32.sg2_pm
#print axioms Hecke32.sg2_add
#print axioms Hecke32.sg2_sq
#print axioms Hecke32.sg2_odd
#print axioms Hecke32.phi_prim
#print axioms Hecke32.phi_norm
#print axioms Hecke32.natAbs_sg2
#print axioms Hecke32.prim_param
#print axioms Hecke32.two_cancel
#print axioms Hecke32.phi_inj

lang/Prim43.lean

lean_all/lang/Prim43.lean

/-
  lang/Prim43 — T48/T50: **the primary class in ℤ[(1 + √−43)/2] through the Legendre symbol** (no residue enumeration):
  ρ(a + bθ) = a + 22b is the reduction mod √−43 (θ ≡ 22), and H43 = {z : χ₄₃(ρ z) = 1} (QChar.chi with p = 43).
    prime43      43 is prime (ZMod.md certificates)
    chi_m1       χ(−1) = −1 (43 ≡ 3 mod 4);  chi_neg  χ(−x) = −χ(x)
    cr_mul       ρ(xy) ≡ ρ(x)ρ(y) (mod 43)
    prim_mul, prim_cj, unit_pm, normalize, unit_one    the interface used by the Hecke files
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Euc43
import lang.GaussQR
import lang.CubicTwo
import lang.PencilClosed
import lang.W36Mul

namespace Prim43

open FTAChain DetChain LibInt Euc43 ZMod QChar
open LibRing (castZ castN)

theorem prime43 : ∀ d, Dv d (2 * 20 + 1 + 1 + 1) → d = 1 ∨ d = 2 * 20 + 1 + 1 + 1 := by
  intro d ⟨q, hq⟩
  match d with
  | 0 => exact absurd (hq.trans (Nat.mul_zero q)) (fun h => Nat.noConfusion h)
  | e + 1 =>
    match e with
    | 0 => exact Or.inl rfl
    | 1 => exact absurd (ZMod.dv_md_zero 1 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 2 => exact absurd (ZMod.dv_md_zero 2 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 3 => exact absurd (ZMod.dv_md_zero 3 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 4 => exact absurd (ZMod.dv_md_zero 4 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 5 => exact absurd (ZMod.dv_md_zero 5 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 6 => exact absurd (ZMod.dv_md_zero 6 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 7 => exact absurd (ZMod.dv_md_zero 7 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 8 => exact absurd (ZMod.dv_md_zero 8 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 9 => exact absurd (ZMod.dv_md_zero 9 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 10 => exact absurd (ZMod.dv_md_zero 10 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 11 => exact absurd (ZMod.dv_md_zero 11 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 12 => exact absurd (ZMod.dv_md_zero 12 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 13 => exact absurd (ZMod.dv_md_zero 13 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 14 => exact absurd (ZMod.dv_md_zero 14 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 15 => exact absurd (ZMod.dv_md_zero 15 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 16 => exact absurd (ZMod.dv_md_zero 16 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 17 => exact absurd (ZMod.dv_md_zero 17 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 18 => exact absurd (ZMod.dv_md_zero 18 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 19 => exact absurd (ZMod.dv_md_zero 19 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 20 => exact absurd (ZMod.dv_md_zero 20 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 21 => exact absurd (ZMod.dv_md_zero 21 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 22 => exact absurd (ZMod.dv_md_zero 22 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 23 => exact absurd (ZMod.dv_md_zero 23 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 24 => exact absurd (ZMod.dv_md_zero 24 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 25 => exact absurd (ZMod.dv_md_zero 25 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 26 => exact absurd (ZMod.dv_md_zero 26 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 27 => exact absurd (ZMod.dv_md_zero 27 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 28 => exact absurd (ZMod.dv_md_zero 28 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 29 => exact absurd (ZMod.dv_md_zero 29 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 30 => exact absurd (ZMod.dv_md_zero 30 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 31 => exact absurd (ZMod.dv_md_zero 31 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 32 => exact absurd (ZMod.dv_md_zero 32 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 33 => exact absurd (ZMod.dv_md_zero 33 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 34 => exact absurd (ZMod.dv_md_zero 34 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 35 => exact absurd (ZMod.dv_md_zero 35 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 36 => exact absurd (ZMod.dv_md_zero 36 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 37 => exact absurd (ZMod.dv_md_zero 37 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 38 => exact absurd (ZMod.dv_md_zero 38 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 39 => exact absurd (ZMod.dv_md_zero 39 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 40 => exact absurd (ZMod.dv_md_zero 40 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 41 => exact absurd (ZMod.dv_md_zero 41 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 42 => exact Or.inr rfl
    | e' + 43 =>
      exfalso
      cases q with
      | zero => exact Nat.noConfusion (hq.trans (Nat.zero_mul _))
      | succ q' =>
        have : 44 ≤ 43 := by
          calc 44 ≤ 44 + (e' + q' * (e' + 43 + 1)) := Nat.le_add_right _ _
            _ = (q' + 1) * (e' + 43 + 1) := by lib_nat
            _ = 43 := hq.symm
        exact Nat.not_succ_le_self 43 this

theorem pw_neg_one : ∀ k, Frobenius.pw (ZMod.ring (2 * 20 + 1 + 1)) ((ZMod.ring (2 * 20 + 1 + 1)).neg (ZMod.ring (2 * 20 + 1 + 1)).one) (2 * k + 1) = (ZMod.ring (2 * 20 + 1 + 1)).neg (ZMod.ring (2 * 20 + 1 + 1)).one
  | 0 => by show (ZMod.ring (2 * 20 + 1 + 1)).mul (ZMod.ring (2 * 20 + 1 + 1)).one ((ZMod.ring (2 * 20 + 1 + 1)).neg (ZMod.ring (2 * 20 + 1 + 1)).one) = _; lib_ring
  | k + 1 => by
    show (ZMod.ring (2 * 20 + 1 + 1)).mul ((ZMod.ring (2 * 20 + 1 + 1)).mul (Frobenius.pw (ZMod.ring (2 * 20 + 1 + 1)) ((ZMod.ring (2 * 20 + 1 + 1)).neg (ZMod.ring (2 * 20 + 1 + 1)).one) (2 * k + 1)) ((ZMod.ring (2 * 20 + 1 + 1)).neg (ZMod.ring (2 * 20 + 1 + 1)).one)) ((ZMod.ring (2 * 20 + 1 + 1)).neg (ZMod.ring (2 * 20 + 1 + 1)).one) = _
    rw [pw_neg_one k]; lib_ring

theorem chi_m1 : chi 20 ((ZMod.ring (2 * 20 + 1 + 1)).neg (ZMod.ring (2 * 20 + 1 + 1)).one) = -1 :=
  chi_neg_of 20 _ (PencilClosed.negnz 20 _ (PowerSum.one_ne_zero' (2 * 20 + 1))) (by rw [show 20 + 1 = 2 * 10 + 1 from rfl, pw_neg_one 10]; exact ne_pm 20)

theorem chi_neg (x : Zp (2 * 20 + 1 + 1)) : chi 20 ((ZMod.ring (2 * 20 + 1 + 1)).neg x) = -chi 20 x := by
  rw [show (ZMod.ring (2 * 20 + 1 + 1)).neg x = (ZMod.ring (2 * 20 + 1 + 1)).mul ((ZMod.ring (2 * 20 + 1 + 1)).neg (ZMod.ring (2 * 20 + 1 + 1)).one) x by lib_ring, chi_mul 20 prime43, chi_m1]; lib_int

def rho (z : Int × Int) : Int := z.1 + 22 * z.2

def cr (z : Int × Int) : Zp (2 * 20 + 1 + 1) := castZ (ZMod.ring (2 * 20 + 1 + 1)) (rho z)

theorem rho_mul (x y : Int × Int) : rho ((Ring43.ring43 LibInt.intR).mul x y) = rho x * rho y + -(43 * (11 * (x.2 * y.2))) := by
  show x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2) + 22 * (x.1 * y.2 + x.2 * y.1 + x.2 * y.2) = (x.1 + 22 * x.2) * (y.1 + 22 * y.2) + -(43 * (11 * (x.2 * y.2)))
  lib_int

theorem cz43 (w : Int) : castZ (ZMod.ring (2 * 20 + 1 + 1)) (43 * w) = (ZMod.ring (2 * 20 + 1 + 1)).zero := by
  rw [HasseDavenport.castZ_mul, show castZ (ZMod.ring (2 * 20 + 1 + 1)) 43 = (ZMod.ring (2 * 20 + 1 + 1)).zero from rfl]; lib_ring

theorem cr_mul (x y : Int × Int) : cr ((Ring43.ring43 LibInt.intR).mul x y) = (ZMod.ring (2 * 20 + 1 + 1)).mul (cr x) (cr y) := by
  unfold cr; rw [rho_mul, IntCast.castZ_add, HasseDavenport.castZ_mul, HasseDavenport.castZ_neg, cz43]; lib_ring

theorem cr_neg (z : Int × Int) : cr (-z.1, -z.2) = (ZMod.ring (2 * 20 + 1 + 1)).neg (cr z) := by
  unfold cr; rw [show rho (-z.1, -z.2) = -rho z by show -z.1 + 22 * -z.2 = -(z.1 + 22 * z.2); lib_int, HasseDavenport.castZ_neg]

def Prim43 (z : Int × Int) : Prop := chi 20 (cr z) = 1

instance (z : Int × Int) : Decidable (Prim43 z) := inferInstanceAs (Decidable (_ = _))

theorem prim_mul (x y : Int × Int) (hx : Prim43 x) (hy : Prim43 y) : Prim43 ((Ring43.ring43 LibInt.intR).mul x y) := by
  show chi 20 (cr _) = 1
  rw [cr_mul, chi_mul 20 prime43, show chi 20 (cr x) = 1 from hx, show chi 20 (cr y) = 1 from hy]; rfl

theorem prim_cj (z : Int × Int) (hz : Prim43 z) : Prim43 (cj z) := by
  have e : cr (cj z) = cr z := by
    unfold cr
    rw [show rho (cj z) = rho z + 43 * -z.2 by show z.1 + z.2 + 22 * -z.2 = z.1 + 22 * z.2 + 43 * -z.2; lib_int, IntCast.castZ_add, cz43]; lib_ring
  show chi 20 (cr (cj z)) = 1; rw [e]; exact hz

theorem unit_pm (u : Int × Int) (h : en u = 1) : u = (Ring43.ring43 LibInt.intR).one ∨ u = (-1, 0) := by
  have h4 := four_nrm u
  have hn : nrm u = 1 := Int.ofNat.inj (by rw [nrm_cast, h]; rfl)
  rw [hn] at h4
  have hB : Int.natAbs u.2 * Int.natAbs u.2 = 0 := by
    cases hb : Int.natAbs u.2 * Int.natAbs u.2 with
    | zero => rfl
    | succ t => rw [hb, show Int.natAbs (2 * u.1 + u.2) * Int.natAbs (2 * u.1 + u.2) + 43 * (t + 1) = (Int.natAbs (2 * u.1 + u.2) * Int.natAbs (2 * u.1 + u.2) + 43 * t + 39) + 4 by lib_nat] at h4
                exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj h4))))
  have b0 : u.2 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ hB with | inl e => exact e | inr e => exact e)
  rw [hB] at h4
  have hT : Int.natAbs (2 * u.1 + u.2) * Int.natAbs (2 * u.1 + u.2) = 2 * 2 := h4.symm
  have hT2 : Int.natAbs (2 * u.1 + u.2) = 2 := by
    match hx : Int.natAbs (2 * u.1 + u.2) with
    | 0 => rw [hx] at hT; exact Nat.noConfusion hT
    | 1 => rw [hx] at hT; exact Nat.noConfusion (Nat.succ.inj hT)
    | 2 => rfl
    | t + 3 => rw [hx, show (t + 3) * (t + 3) = (t * t + 6 * t + 5) + 4 by lib_nat] at hT; exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj hT))))
  rw [b0] at hT2
  obtain ⟨a, b⟩ := u
  show (a, b) = (1, 0) ∨ (a, b) = (-1, 0)
  have b0' : b = 0 := b0
  rcases EisEuclid.sgn (2 * a + 0) with s | s <;> rw [hT2] at s
  · exact Or.inl (Prod.ext (W36Mul.two_cancel_int a 1 (by rw [show 2 * a = 2 * a + 0 by lib_int, s]; rfl)) b0')
  · exact Or.inr (Prod.ext (W36Mul.two_cancel_int a (-1) (by rw [show 2 * a = 2 * a + 0 by lib_int, s]; rfl)) b0')

theorem neg_mul' (z : Int × Int) : (Ring43.ring43 LibInt.intR).mul (-1, 0) z = (-z.1, -z.2) :=
  Prod.ext (by show -1 * z.1 + -(0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2) = -z.1; lib_int) (by show -1 * z.2 + 0 * z.1 + 0 * z.2 = -z.2; lib_int)

theorem cr_ne (α : Int × Int) (hd : ∀ k, en α ≠ 43 * k) : cr α ≠ (ZMod.ring (2 * 20 + 1 + 1)).zero := fun h0 => by
  obtain ⟨q, hq⟩ := CubicTwo.cast_zero_gen (2 * 20 + 1 + 1) (rho α) h0
  obtain ⟨a, b⟩ := α
  have hq' : a + 22 * b = 43 * q := hq
  exact hd (q * (a + -(21 * b)) + 11 * (b * b)) (by
    show a * a + a * b + 11 * (b * b) = _
    calc a * a + a * b + 11 * (b * b) = (a + 22 * b) * (a + -(21 * b)) + 43 * (11 * (b * b)) := by lib_int
      _ = 43 * q * (a + -(21 * b)) + 43 * (11 * (b * b)) := by rw [hq']
      _ = _ := by lib_int)

/-- 43 ∤ N(α) ⇒ ±α ∈ H43. -/
theorem normalize (α : Int × Int) (hd : ∀ k, en α ≠ 43 * k) : ∃ u, en u = 1 ∧ Prim43 ((Ring43.ring43 LibInt.intR).mul u α) := by
  rcases GaussQR.chi_pm 20 _ (cr_ne α hd) with h | h
  · exact ⟨(Ring43.ring43 LibInt.intR).one, rfl, by rw [(Ring43.ring43 LibInt.intR).one_mul]; exact h⟩
  · refine ⟨(-1, 0), rfl, ?_⟩
    show chi 20 (cr _) = 1
    rw [neg_mul', cr_neg, chi_neg, h]; rfl

theorem unit_one (u α : Int × Int) (hu : en u = 1) (pα : Prim43 α) (pu : Prim43 ((Ring43.ring43 LibInt.intR).mul u α)) : u = (Ring43.ring43 LibInt.intR).one := by
  rcases unit_pm u hu with e0 | e0
  · exact e0
  exfalso
  rw [e0, neg_mul'] at pu
  have h1 : chi 20 (cr α) = 1 := pα
  have h2 : chi 20 (cr (-α.1, -α.2)) = 1 := pu
  rw [cr_neg, chi_neg, h1] at h2
  exact Int.noConfusion h2

end Prim43

#print axioms Prim43.prime43
#print axioms Prim43.pw_neg_one
#print axioms Prim43.chi_m1
#print axioms Prim43.chi_neg
#print axioms Prim43.rho_mul
#print axioms Prim43.cz43
#print axioms Prim43.cr_mul
#print axioms Prim43.cr_neg
#print axioms Prim43.prim_mul
#print axioms Prim43.prim_cj
#print axioms Prim43.unit_pm
#print axioms Prim43.neg_mul'
#print axioms Prim43.cr_ne
#print axioms Prim43.normalize
#print axioms Prim43.unit_one

lang/Prim67.lean

lean_all/lang/Prim67.lean

/-
  lang/Prim67 — T48/T50: **the primary class in ℤ[(1 + √−67)/2] through the Legendre symbol** (no residue enumeration):
  ρ(a + bθ) = a + 34b is the reduction mod √−67 (θ ≡ 34), and H67 = {z : χ₆₇(ρ z) = 1} (QChar.chi with p = 67).
    prime67      67 is prime (ZMod.md certificates)
    chi_m1       χ(−1) = −1 (67 ≡ 3 mod 4);  chi_neg  χ(−x) = −χ(x)
    cr_mul       ρ(xy) ≡ ρ(x)ρ(y) (mod 67)
    prim_mul, prim_cj, unit_pm, normalize, unit_one    the interface used by the Hecke files
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Euc67
import lang.GaussQR
import lang.CubicTwo
import lang.PencilClosed
import lang.W36Mul

set_option maxRecDepth 20000

namespace Prim67

open FTAChain DetChain LibInt Euc67 ZMod QChar
open LibRing (castZ castN)

theorem prime67 : ∀ d, Dv d (2 * 32 + 1 + 1 + 1) → d = 1 ∨ d = 2 * 32 + 1 + 1 + 1 := by
  intro d ⟨q, hq⟩
  match d with
  | 0 => exact absurd (hq.trans (Nat.mul_zero q)) (fun h => Nat.noConfusion h)
  | e + 1 =>
    match e with
    | 0 => exact Or.inl rfl
    | 1 => exact absurd (ZMod.dv_md_zero 1 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 2 => exact absurd (ZMod.dv_md_zero 2 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 3 => exact absurd (ZMod.dv_md_zero 3 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 4 => exact absurd (ZMod.dv_md_zero 4 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 5 => exact absurd (ZMod.dv_md_zero 5 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 6 => exact absurd (ZMod.dv_md_zero 6 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 7 => exact absurd (ZMod.dv_md_zero 7 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 8 => exact absurd (ZMod.dv_md_zero 8 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 9 => exact absurd (ZMod.dv_md_zero 9 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 10 => exact absurd (ZMod.dv_md_zero 10 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 11 => exact absurd (ZMod.dv_md_zero 11 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 12 => exact absurd (ZMod.dv_md_zero 12 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 13 => exact absurd (ZMod.dv_md_zero 13 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 14 => exact absurd (ZMod.dv_md_zero 14 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 15 => exact absurd (ZMod.dv_md_zero 15 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 16 => exact absurd (ZMod.dv_md_zero 16 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 17 => exact absurd (ZMod.dv_md_zero 17 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 18 => exact absurd (ZMod.dv_md_zero 18 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 19 => exact absurd (ZMod.dv_md_zero 19 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 20 => exact absurd (ZMod.dv_md_zero 20 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 21 => exact absurd (ZMod.dv_md_zero 21 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 22 => exact absurd (ZMod.dv_md_zero 22 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 23 => exact absurd (ZMod.dv_md_zero 23 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 24 => exact absurd (ZMod.dv_md_zero 24 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 25 => exact absurd (ZMod.dv_md_zero 25 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 26 => exact absurd (ZMod.dv_md_zero 26 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 27 => exact absurd (ZMod.dv_md_zero 27 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 28 => exact absurd (ZMod.dv_md_zero 28 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 29 => exact absurd (ZMod.dv_md_zero 29 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 30 => exact absurd (ZMod.dv_md_zero 30 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 31 => exact absurd (ZMod.dv_md_zero 31 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 32 => exact absurd (ZMod.dv_md_zero 32 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 33 => exact absurd (ZMod.dv_md_zero 33 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 34 => exact absurd (ZMod.dv_md_zero 34 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 35 => exact absurd (ZMod.dv_md_zero 35 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 36 => exact absurd (ZMod.dv_md_zero 36 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 37 => exact absurd (ZMod.dv_md_zero 37 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 38 => exact absurd (ZMod.dv_md_zero 38 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 39 => exact absurd (ZMod.dv_md_zero 39 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 40 => exact absurd (ZMod.dv_md_zero 40 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 41 => exact absurd (ZMod.dv_md_zero 41 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 42 => exact absurd (ZMod.dv_md_zero 42 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 43 => exact absurd (ZMod.dv_md_zero 43 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 44 => exact absurd (ZMod.dv_md_zero 44 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 45 => exact absurd (ZMod.dv_md_zero 45 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 46 => exact absurd (ZMod.dv_md_zero 46 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 47 => exact absurd (ZMod.dv_md_zero 47 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 48 => exact absurd (ZMod.dv_md_zero 48 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 49 => exact absurd (ZMod.dv_md_zero 49 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 50 => exact absurd (ZMod.dv_md_zero 50 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 51 => exact absurd (ZMod.dv_md_zero 51 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 52 => exact absurd (ZMod.dv_md_zero 52 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 53 => exact absurd (ZMod.dv_md_zero 53 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 54 => exact absurd (ZMod.dv_md_zero 54 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 55 => exact absurd (ZMod.dv_md_zero 55 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 56 => exact absurd (ZMod.dv_md_zero 56 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 57 => exact absurd (ZMod.dv_md_zero 57 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 58 => exact absurd (ZMod.dv_md_zero 58 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 59 => exact absurd (ZMod.dv_md_zero 59 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 60 => exact absurd (ZMod.dv_md_zero 60 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 61 => exact absurd (ZMod.dv_md_zero 61 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 62 => exact absurd (ZMod.dv_md_zero 62 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 63 => exact absurd (ZMod.dv_md_zero 63 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 64 => exact absurd (ZMod.dv_md_zero 64 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 65 => exact absurd (ZMod.dv_md_zero 65 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 66 => exact Or.inr rfl
    | e' + 67 =>
      exfalso
      cases q with
      | zero => exact Nat.noConfusion (hq.trans (Nat.zero_mul _))
      | succ q' =>
        have : 68 ≤ 67 := by
          calc 68 ≤ 68 + (e' + q' * (e' + 67 + 1)) := Nat.le_add_right _ _
            _ = (q' + 1) * (e' + 67 + 1) := by lib_nat
            _ = 67 := hq.symm
        exact Nat.not_succ_le_self 67 this

theorem pw_neg_one : ∀ k, Frobenius.pw (ZMod.ring (2 * 32 + 1 + 1)) ((ZMod.ring (2 * 32 + 1 + 1)).neg (ZMod.ring (2 * 32 + 1 + 1)).one) (2 * k + 1) = (ZMod.ring (2 * 32 + 1 + 1)).neg (ZMod.ring (2 * 32 + 1 + 1)).one
  | 0 => by show (ZMod.ring (2 * 32 + 1 + 1)).mul (ZMod.ring (2 * 32 + 1 + 1)).one ((ZMod.ring (2 * 32 + 1 + 1)).neg (ZMod.ring (2 * 32 + 1 + 1)).one) = _; lib_ring
  | k + 1 => by
    show (ZMod.ring (2 * 32 + 1 + 1)).mul ((ZMod.ring (2 * 32 + 1 + 1)).mul (Frobenius.pw (ZMod.ring (2 * 32 + 1 + 1)) ((ZMod.ring (2 * 32 + 1 + 1)).neg (ZMod.ring (2 * 32 + 1 + 1)).one) (2 * k + 1)) ((ZMod.ring (2 * 32 + 1 + 1)).neg (ZMod.ring (2 * 32 + 1 + 1)).one)) ((ZMod.ring (2 * 32 + 1 + 1)).neg (ZMod.ring (2 * 32 + 1 + 1)).one) = _
    rw [pw_neg_one k]; lib_ring

theorem chi_m1 : chi 32 ((ZMod.ring (2 * 32 + 1 + 1)).neg (ZMod.ring (2 * 32 + 1 + 1)).one) = -1 :=
  chi_neg_of 32 _ (PencilClosed.negnz 32 _ (PowerSum.one_ne_zero' (2 * 32 + 1))) (by rw [show 32 + 1 = 2 * 16 + 1 from rfl, pw_neg_one 16]; exact ne_pm 32)

theorem chi_neg (x : Zp (2 * 32 + 1 + 1)) : chi 32 ((ZMod.ring (2 * 32 + 1 + 1)).neg x) = -chi 32 x := by
  rw [show (ZMod.ring (2 * 32 + 1 + 1)).neg x = (ZMod.ring (2 * 32 + 1 + 1)).mul ((ZMod.ring (2 * 32 + 1 + 1)).neg (ZMod.ring (2 * 32 + 1 + 1)).one) x by lib_ring, chi_mul 32 prime67, chi_m1]; lib_int

def rho (z : Int × Int) : Int := z.1 + 34 * z.2

def cr (z : Int × Int) : Zp (2 * 32 + 1 + 1) := castZ (ZMod.ring (2 * 32 + 1 + 1)) (rho z)

theorem rho_mul (x y : Int × Int) : rho ((Ring67.ring67 LibInt.intR).mul x y) = rho x * rho y + -(67 * (17 * (x.2 * y.2))) := by
  show x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2) + 34 * (x.1 * y.2 + x.2 * y.1 + x.2 * y.2) = (x.1 + 34 * x.2) * (y.1 + 34 * y.2) + -(67 * (17 * (x.2 * y.2)))
  lib_int

theorem cz67 (w : Int) : castZ (ZMod.ring (2 * 32 + 1 + 1)) (67 * w) = (ZMod.ring (2 * 32 + 1 + 1)).zero := by
  rw [HasseDavenport.castZ_mul, show castZ (ZMod.ring (2 * 32 + 1 + 1)) 67 = (ZMod.ring (2 * 32 + 1 + 1)).zero from rfl]; lib_ring

theorem cr_mul (x y : Int × Int) : cr ((Ring67.ring67 LibInt.intR).mul x y) = (ZMod.ring (2 * 32 + 1 + 1)).mul (cr x) (cr y) := by
  unfold cr; rw [rho_mul, IntCast.castZ_add, HasseDavenport.castZ_mul, HasseDavenport.castZ_neg, cz67]; lib_ring

theorem cr_neg (z : Int × Int) : cr (-z.1, -z.2) = (ZMod.ring (2 * 32 + 1 + 1)).neg (cr z) := by
  unfold cr; rw [show rho (-z.1, -z.2) = -rho z by show -z.1 + 34 * -z.2 = -(z.1 + 34 * z.2); lib_int, HasseDavenport.castZ_neg]

def Prim67 (z : Int × Int) : Prop := chi 32 (cr z) = 1

instance (z : Int × Int) : Decidable (Prim67 z) := inferInstanceAs (Decidable (_ = _))

theorem prim_mul (x y : Int × Int) (hx : Prim67 x) (hy : Prim67 y) : Prim67 ((Ring67.ring67 LibInt.intR).mul x y) := by
  show chi 32 (cr _) = 1
  rw [cr_mul, chi_mul 32 prime67, show chi 32 (cr x) = 1 from hx, show chi 32 (cr y) = 1 from hy]; rfl

theorem prim_cj (z : Int × Int) (hz : Prim67 z) : Prim67 (cj z) := by
  have e : cr (cj z) = cr z := by
    unfold cr
    rw [show rho (cj z) = rho z + 67 * -z.2 by show z.1 + z.2 + 34 * -z.2 = z.1 + 34 * z.2 + 67 * -z.2; lib_int, IntCast.castZ_add, cz67]; lib_ring
  show chi 32 (cr (cj z)) = 1; rw [e]; exact hz

theorem unit_pm (u : Int × Int) (h : en u = 1) : u = (Ring67.ring67 LibInt.intR).one ∨ u = (-1, 0) := by
  have h4 := four_nrm u
  have hn : nrm u = 1 := Int.ofNat.inj (by rw [nrm_cast, h]; rfl)
  rw [hn] at h4
  have hB : Int.natAbs u.2 * Int.natAbs u.2 = 0 := by
    cases hb : Int.natAbs u.2 * Int.natAbs u.2 with
    | zero => rfl
    | succ t => rw [hb, show Int.natAbs (2 * u.1 + u.2) * Int.natAbs (2 * u.1 + u.2) + 67 * (t + 1) = (Int.natAbs (2 * u.1 + u.2) * Int.natAbs (2 * u.1 + u.2) + 67 * t + 63) + 4 by lib_nat] at h4
                exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj h4))))
  have b0 : u.2 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ hB with | inl e => exact e | inr e => exact e)
  rw [hB] at h4
  have hT : Int.natAbs (2 * u.1 + u.2) * Int.natAbs (2 * u.1 + u.2) = 2 * 2 := h4.symm
  have hT2 : Int.natAbs (2 * u.1 + u.2) = 2 := by
    match hx : Int.natAbs (2 * u.1 + u.2) with
    | 0 => rw [hx] at hT; exact Nat.noConfusion hT
    | 1 => rw [hx] at hT; exact Nat.noConfusion (Nat.succ.inj hT)
    | 2 => rfl
    | t + 3 => rw [hx, show (t + 3) * (t + 3) = (t * t + 6 * t + 5) + 4 by lib_nat] at hT; exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj hT))))
  rw [b0] at hT2
  obtain ⟨a, b⟩ := u
  show (a, b) = (1, 0) ∨ (a, b) = (-1, 0)
  have b0' : b = 0 := b0
  rcases EisEuclid.sgn (2 * a + 0) with s | s <;> rw [hT2] at s
  · exact Or.inl (Prod.ext (W36Mul.two_cancel_int a 1 (by rw [show 2 * a = 2 * a + 0 by lib_int, s]; rfl)) b0')
  · exact Or.inr (Prod.ext (W36Mul.two_cancel_int a (-1) (by rw [show 2 * a = 2 * a + 0 by lib_int, s]; rfl)) b0')

theorem neg_mul' (z : Int × Int) : (Ring67.ring67 LibInt.intR).mul (-1, 0) z = (-z.1, -z.2) :=
  Prod.ext (by show -1 * z.1 + -(0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2) = -z.1; lib_int) (by show -1 * z.2 + 0 * z.1 + 0 * z.2 = -z.2; lib_int)

/-- Generic factorization: (a + c b)(a − c' b) + D t b² = a² + ab + t b² when c = c' + 1 and D t = c c' + t, with D | a + c b. -/
theorem fac_gen (a b c c' D t q : Int) (h1 : c = c' + 1) (h2 : D * t = c * c' + t) (hq : a + c * b = D * q) :
    a * a + a * b + t * (b * b) = D * (q * (a + -(c' * b)) + t * (b * b)) := by
  calc a * a + a * b + t * (b * b) = (a + c * b) * (a + -(c' * b)) + (c * c' + t) * (b * b) + (c' + 1 + -c) * (a * b) := by lib_int
    _ = D * q * (a + -(c' * b)) + D * t * (b * b) + (c' + 1 + -c) * (a * b) := by rw [hq, h2]
    _ = D * q * (a + -(c' * b)) + D * t * (b * b) := by rw [h1]; lib_int
    _ = _ := by lib_int

theorem cr_ne (α : Int × Int) (hd : ∀ k, en α ≠ 67 * k) : cr α ≠ (ZMod.ring (2 * 32 + 1 + 1)).zero := fun h0 => by
  obtain ⟨q, hq⟩ := CubicTwo.cast_zero_gen (2 * 32 + 1 + 1) (rho α) h0
  obtain ⟨a, b⟩ := α
  exact hd _ (fac_gen a b 34 33 67 17 q rfl rfl hq)

/-- 67 ∤ N(α) ⇒ ±α ∈ H67. -/
theorem normalize (α : Int × Int) (hd : ∀ k, en α ≠ 67 * k) : ∃ u, en u = 1 ∧ Prim67 ((Ring67.ring67 LibInt.intR).mul u α) := by
  rcases GaussQR.chi_pm 32 _ (cr_ne α hd) with h | h
  · exact ⟨(Ring67.ring67 LibInt.intR).one, rfl, by rw [(Ring67.ring67 LibInt.intR).one_mul]; exact h⟩
  · refine ⟨(-1, 0), rfl, ?_⟩
    show chi 32 (cr _) = 1
    rw [neg_mul', cr_neg, chi_neg, h]; rfl

theorem unit_one (u α : Int × Int) (hu : en u = 1) (pα : Prim67 α) (pu : Prim67 ((Ring67.ring67 LibInt.intR).mul u α)) : u = (Ring67.ring67 LibInt.intR).one := by
  rcases unit_pm u hu with e0 | e0
  · exact e0
  exfalso
  rw [e0, neg_mul'] at pu
  have h1 : chi 32 (cr α) = 1 := pα
  have h2 : chi 32 (cr (-α.1, -α.2)) = 1 := pu
  rw [cr_neg, chi_neg, h1] at h2
  exact Int.noConfusion h2

end Prim67

#print axioms Prim67.prime67
#print axioms Prim67.pw_neg_one
#print axioms Prim67.chi_m1
#print axioms Prim67.chi_neg
#print axioms Prim67.rho_mul
#print axioms Prim67.cz67
#print axioms Prim67.cr_mul
#print axioms Prim67.fac_gen
#print axioms Prim67.cr_neg
#print axioms Prim67.prim_mul
#print axioms Prim67.prim_cj
#print axioms Prim67.unit_pm
#print axioms Prim67.neg_mul'
#print axioms Prim67.fac_gen
#print axioms Prim67.cr_ne
#print axioms Prim67.normalize
#print axioms Prim67.unit_one

lang/Prim7.lean

lean_all/lang/Prim7.lean

/-
  lang/Prim7 — T48/T50, cycle 640: **the primary class of 49a's Hecke character** in ℤ[θ], θ = (1 + √−7)/2:
  ρ(a + bθ) = a + 4b is the reduction mod √−7 (θ ≡ 4), multiplicative mod 7; H7 = {ρ ≡ 1, 2, 4 (mod 7)} (the squares).
  (Numerically: π ∈ H7 of norm p gives 2a + b = a_p(y² = x³ − 35x − 98) for all split p < 140.)
    prim7_mul, prim7_cj      closed under products and conjugation
    unit_pm7                 N(u) = 1 ⇒ u = ±1
    normalize7               7 ∤ N(α) ⇒ ±α ∈ H7          (N ≡ ρ² mod 7, −1 is not a square mod 7)
    unit_one7                N(u) = 1, α, uα ∈ H7 ⇒ u = 1
  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.Cop7
import lang.Sq2Prim8

namespace Prim7

open FTAChain DetChain LibInt Euc7

def rho (z : Int × Int) : Int := z.1 + 4 * z.2

def Prim7 (z : Int × Int) : Prop := ∃ e : Int, rho z = 7 * e + 1 ∨ rho z = 7 * e + 2 ∨ rho z = 7 * e + 4

theorem rho_mul (x y : Int × Int) : rho ((Ring7.ring7 LibInt.intR).mul x y) = rho x * rho y + -(14 * (x.2 * y.2)) := by
  show x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2) + 4 * (x.1 * y.2 + x.2 * y.1 + x.2 * y.2) = (x.1 + 4 * x.2) * (y.1 + 4 * y.2) + -(14 * (x.2 * y.2))
  lib_int

theorem prim7_mul (x y : Int × Int) (hx : Prim7 x) (hy : Prim7 y) : Prim7 ((Ring7.ring7 LibInt.intR).mul x y) := by
  obtain ⟨e, hx⟩ := hx
  obtain ⟨f, hy⟩ := hy
  rw [Prim7, rho_mul]
  generalize x.2 * y.2 = w
  rcases hx with hx | hx | hx <;> rcases hy with hy | hy | hy <;> rw [hx, hy]
  · exact ⟨e + (7) * e * f + f + (-2) * w, Or.inl (by lib_int)⟩
  · exact ⟨(2) * e + (7) * e * f + f + (-2) * w, Or.inr (Or.inl (by lib_int))⟩
  · exact ⟨(4) * e + (7) * e * f + f + (-2) * w, Or.inr (Or.inr (by lib_int))⟩
  · exact ⟨e + (7) * e * f + (2) * f + (-2) * w, Or.inr (Or.inl (by lib_int))⟩
  · exact ⟨(2) * e + (7) * e * f + (2) * f + (-2) * w, Or.inr (Or.inr (by lib_int))⟩
  · exact ⟨(1) + (4) * e + (7) * e * f + (2) * f + (-2) * w, Or.inl (by lib_int)⟩
  · exact ⟨e + (7) * e * f + (4) * f + (-2) * w, Or.inr (Or.inr (by lib_int))⟩
  · exact ⟨(1) + (2) * e + (7) * e * f + (4) * f + (-2) * w, Or.inl (by lib_int)⟩
  · exact ⟨(2) + (4) * e + (7) * e * f + (4) * f + (-2) * w, Or.inr (Or.inl (by lib_int))⟩

theorem prim7_cj (z : Int × Int) (hz : Prim7 z) : Prim7 (cj z) := by
  obtain ⟨e, h⟩ := hz
  have hr : rho (cj z) = rho z + -(7 * z.2) := by show z.1 + z.2 + 4 * -z.2 = z.1 + 4 * z.2 + -(7 * z.2); lib_int
  refine ⟨e + -z.2, ?_⟩
  rw [hr]
  rcases h with h | h | h
  · exact Or.inl (by rw [h]; lib_int)
  · exact Or.inr (Or.inl (by rw [h]; lib_int))
  · exact Or.inr (Or.inr (by rw [h]; lib_int))

theorem unit_pm7 (u : Int × Int) (h : en u = 1) : u = (Ring7.ring7 LibInt.intR).one ∨ u = (-1, 0) := by
  have h4 := four_nrm u
  have hn : nrm u = 1 := Int.ofNat.inj (by rw [nrm_cast, h]; rfl)
  rw [hn] at h4
  have hB : Int.natAbs u.2 * Int.natAbs u.2 = 0 := by
    cases hb : Int.natAbs u.2 * Int.natAbs u.2 with
    | zero => rfl
    | succ t => rw [hb, show Int.natAbs (2 * u.1 + u.2) * Int.natAbs (2 * u.1 + u.2) + 7 * (t + 1) = (Int.natAbs (2 * u.1 + u.2) * Int.natAbs (2 * u.1 + u.2) + 7 * t + 3) + 4 by lib_nat] at h4
                exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj h4))))
  have b0 : u.2 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ hB with | inl e => exact e | inr e => exact e)
  rw [hB] at h4
  have hT : Int.natAbs (2 * u.1 + u.2) * Int.natAbs (2 * u.1 + u.2) = 2 * 2 := h4.symm
  have hT2 : Int.natAbs (2 * u.1 + u.2) = 2 := by
    match hx : Int.natAbs (2 * u.1 + u.2) with
    | 0 => rw [hx] at hT; exact Nat.noConfusion hT
    | 1 => rw [hx] at hT; exact Nat.noConfusion (Nat.succ.inj hT)
    | 2 => rfl
    | t + 3 => rw [hx, show (t + 3) * (t + 3) = (t * t + 6 * t + 5) + 4 by lib_nat] at hT; exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj hT))))
  rw [b0] at hT2
  obtain ⟨a, b⟩ := u
  show (a, b) = (1, 0) ∨ (a, b) = (-1, 0)
  have b0' : b = 0 := b0
  rcases EisEuclid.sgn (2 * a + 0) with s | s <;> rw [hT2] at s
  · exact Or.inl (Prod.ext (W36Mul.two_cancel_int a 1 (by rw [show 2 * a = 2 * a + 0 by lib_int, s]; rfl)) b0')
  · exact Or.inr (Prod.ext (W36Mul.two_cancel_int a (-1) (by rw [show 2 * a = 2 * a + 0 by lib_int, s]; rfl)) b0')

theorem neg_mul7 (z : Int × Int) : (Ring7.ring7 LibInt.intR).mul (-1, 0) z = (-z.1, -z.2) :=
  Prod.ext (by show -1 * z.1 + -(0 * z.2 + 0 * z.2) = -z.1; lib_int) (by show -1 * z.2 + 0 * z.1 + 0 * z.2 = -z.2; lib_int)

theorem rho_neg (z : Int × Int) : rho (-z.1, -z.2) = -rho z := by
  show -z.1 + 4 * -z.2 = -(z.1 + 4 * z.2); lib_int

/-- 7 ∤ N(α) ⇒ ±α ∈ H7. -/
theorem normalize7 (α : Int × Int) (h7 : ∀ k, en α ≠ 7 * k) : ∃ u, en u = 1 ∧ Prim7 ((Ring7.ring7 LibInt.intR).mul u α) := by
  obtain ⟨q, r, hq, hr⟩ := GaussEuclid.rnd (rho α) 6
  have hq' : rho α = 7 * q + r := by rw [hq]; show q * 7 + r = 7 * q + r; lib_int
  have plus : ∀ e, (rho α = 7 * e + 1 ∨ rho α = 7 * e + 2 ∨ rho α = 7 * e + 4) → ∃ u, en u = 1 ∧ Prim7 ((Ring7.ring7 LibInt.intR).mul u α) :=
    fun e he => ⟨(Ring7.ring7 LibInt.intR).one, rfl, by rw [(Ring7.ring7 LibInt.intR).one_mul]; exact ⟨e, he⟩⟩
  have minus : ∀ e, (-rho α = 7 * e + 1 ∨ -rho α = 7 * e + 2 ∨ -rho α = 7 * e + 4) → ∃ u, en u = 1 ∧ Prim7 ((Ring7.ring7 LibInt.intR).mul u α) :=
    fun e he => ⟨(-1, 0), rfl, by rw [neg_mul7]; exact ⟨e, by rw [rho_neg]; exact he⟩⟩
  cases r with
  | ofNat t =>
    match t, hr with
    | 0, _ =>
      exfalso
      obtain ⟨a, b⟩ := α
      have ha : a = 7 * q + -(4 * b) := by
        have : a + 4 * b = 7 * q + Int.ofNat 0 := hq'
        calc a = (a + 4 * b) + -(4 * b) := by lib_int
          _ = _ := by rw [this]; show 7 * q + 0 + -(4 * b) = _; lib_int
      exact h7 (7 * q * q + -(7 * q * b) + 2 * b * b) (by show a * a + a * b + 2 * (b * b) = _; rw [ha]; lib_int)
    | 1, _ => exact plus q (Or.inl hq')
    | 2, _ => exact plus q (Or.inr (Or.inl hq'))
    | 3, _ => exact minus (-q + -1) (Or.inr (Or.inr (by rw [hq']; show -(7 * q + 3) = _; lib_int)))
    | t + 4, hr' =>
      exfalso
      have : 2 * (t + 4) ≤ 7 := hr'
      rw [show 2 * (t + 4) = 2 * t + 1 + 7 by lib_nat] at this
      exact Nat.not_succ_le_self 7 (Nat.le_trans (Nat.add_le_add_right (Nat.zero_le (2 * t)) 8) (Nat.le_trans (Nat.le_of_eq (by lib_nat)) this))
  | negSucc t =>
    match t, hr with
    | 0, _ => exact minus (-q) (Or.inl (by rw [hq']; show -(7 * q + -1) = _; lib_int))
    | 1, _ => exact minus (-q) (Or.inr (Or.inl (by rw [hq']; show -(7 * q + -2) = _; lib_int)))
    | 2, _ => exact plus (q + -1) (Or.inr (Or.inr (by rw [hq']; show 7 * q + -3 = _; lib_int)))
    | t + 3, hr' =>
      exfalso
      have : 2 * (t + 4) ≤ 7 := hr'
      rw [show 2 * (t + 4) = 2 * t + 1 + 7 by lib_nat] at this
      exact Nat.not_succ_le_self 7 (Nat.le_trans (Nat.add_le_add_right (Nat.zero_le (2 * t)) 8) (Nat.le_trans (Nat.le_of_eq (by lib_nat)) this))

/-- **A unit taking an H7-element to an H7-element is 1.** -/
theorem unit_one7 (u α : Int × Int) (hu : en u = 1) (pα : Prim7 α) (pu : Prim7 ((Ring7.ring7 LibInt.intR).mul u α)) : u = (Ring7.ring7 LibInt.intR).one := by
  rcases unit_pm7 u hu with e0 | e0
  · exact e0
  exfalso
  rw [e0, neg_mul7] at pu
  obtain ⟨e, h1⟩ := pα
  obtain ⟨f, h2⟩ := pu
  rw [rho_neg] at h2
  generalize rho α = x at h1 h2
  rcases h1 with h1 | h1 | h1 <;> rcases h2 with h2 | h2 | h2
  · exact Sq2Prim8.noM 6 (e + f + -(-1)) 4 1 (by calc (7 : Int) * (e + f + -(-1)) = (7 * e + 1) + (7 * f + 1) + 5 := by lib_int
        _ = x + -x + 5 := by rw [← h1, ← h2]
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (e + f + -(-1)) 3 2 (by calc (7 : Int) * (e + f + -(-1)) = (7 * e + 1) + (7 * f + 2) + 4 := by lib_int
        _ = x + -x + 4 := by rw [← h1, ← h2]
        _ = 4 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (e + f + -(-1)) 1 4 (by calc (7 : Int) * (e + f + -(-1)) = (7 * e + 1) + (7 * f + 4) + 2 := by lib_int
        _ = x + -x + 2 := by rw [← h1, ← h2]
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (e + f + -(-1)) 3 2 (by calc (7 : Int) * (e + f + -(-1)) = (7 * e + 2) + (7 * f + 1) + 4 := by lib_int
        _ = x + -x + 4 := by rw [← h1, ← h2]
        _ = 4 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (e + f + -(-1)) 2 3 (by calc (7 : Int) * (e + f + -(-1)) = (7 * e + 2) + (7 * f + 2) + 3 := by lib_int
        _ = x + -x + 3 := by rw [← h1, ← h2]
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (e + f + -(-1)) 0 5 (by calc (7 : Int) * (e + f + -(-1)) = (7 * e + 2) + (7 * f + 4) + 1 := by lib_int
        _ = x + -x + 1 := by rw [← h1, ← h2]
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (e + f + -(-1)) 1 4 (by calc (7 : Int) * (e + f + -(-1)) = (7 * e + 4) + (7 * f + 1) + 2 := by lib_int
        _ = x + -x + 2 := by rw [← h1, ← h2]
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (e + f + -(-1)) 0 5 (by calc (7 : Int) * (e + f + -(-1)) = (7 * e + 4) + (7 * f + 2) + 1 := by lib_int
        _ = x + -x + 1 := by rw [← h1, ← h2]
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (e + f + -(-2)) 5 0 (by calc (7 : Int) * (e + f + -(-2)) = (7 * e + 4) + (7 * f + 4) + 6 := by lib_int
        _ = x + -x + 6 := by rw [← h1, ← h2]
        _ = 6 := by lib_int) rfl

end Prim7

#print axioms Prim7.rho_mul
#print axioms Prim7.prim7_mul
#print axioms Prim7.prim7_cj
#print axioms Prim7.unit_pm7
#print axioms Prim7.neg_mul7
#print axioms Prim7.rho_neg
#print axioms Prim7.normalize7
#print axioms Prim7.unit_one7

lang/PrimA.lean

lean_all/lang/PrimA.lean

/-
  lang/PrimA — T49 (general ℓ), cycle 830: **the ℓ-th power of the Gauss sum is primary**, G(χ)^ℓ ≡ −1 (mod (1 − ζ)²),
  in computational form. NthRecip writes G_χ^ℓ = A·(pζ⁰ − N) with A = (∏_{i ≤ k} J(χ^i, χ))·χ(ι(−1)), k = ℓ − 2,
  and the n-th power residue criterion reads χ(q⁻¹) = (A p)^m. Here, in a ring Q with π² = 0 and ℓ·1 = 0
  (think Q = ℤ[ζ_ℓ]/(1 − ζ)², π = ζ − 1), p − 1 = ℓ·2h, g a primitive root, u = (1 + π)^(dlog ·) (PrimJC.u):
    prim2      Σ_t u(t) w(1 − t) = −1 for two characters u, w of the PrimJ shape
    v_val, v_sum, v_sum_all, v_zero, v_one, v_lin   the powers u^c of u
    jac_c      J(u^(c+1), u) = −1          (every J(χ^i, χ) is primary)
    jpr_val    ∏_{i ≤ k} J(u^i, u) = (−1)^k
    iv_neg     ι(−1) = −1 in 𝔽_p
    u_neg      u(−1) = 1                   (g^(ℓh) = −1, dlog(−1) = ℓh, ℓ·1 = 0)
    pp_one     p·1 = 1 in Q
    gauss_prim A·p = −1 for k odd (k = ℓ − 2), i.e. G(χ)^ℓ ≡ −1 (mod (1 − ζ)²) — the general form of Quint18.
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.PrimJC
import lang.GaussCritN
import lang.Euler
import lib.RingTac
import lib.Semi
import FiniteRecChain

namespace PrimA

open ZMod Frobenius YMChain GroupRing
open FiniteRecChain (ite_pos' ite_neg')

variable {Q : Type} (K : DetChain.CRing Q)

/-- **Σ_t u(t) w(1 − t) = −1** for two maps u, w with u(0) = w(0) = 0, u(1) = w(1) = 1, (u_t − 1)(w_s − 1) = 0, Σu = Σw = 0. -/
theorem prim2 (m : Nat) (u w : Zp m → Q) (hu0 : u (ring m).zero = K.zero) (hu1 : u (ring m).one = K.one)
    (_hw0 : w (ring m).zero = K.zero) (hw1 : w (ring m).one = K.one)
    (hsq : ∀ t s, t ≠ (ring m).zero → s ≠ (ring m).zero → K.mul (K.add (u t) (K.neg K.one)) (K.add (w s) (K.neg K.one)) = K.zero)
    (hsu : sumK K u (L m) = K.zero) (hsw : sumK K w (L m) = K.zero) (hp1 : nsm K m K.one = K.zero) :
    sumK K (fun t => K.mul (u t) (w ((ring m).add (ring m).one ((ring m).neg t)))) (L m) = K.neg K.one := by
  have pt : ∀ t, K.mul (u t) (w ((ring m).add (ring m).one ((ring m).neg t))) =
      K.add (K.add (u t) (w ((ring m).add (ring m).one ((ring m).neg t)))) (K.neg K.one) := fun t => by
    have hz : K.mul (K.add (u t) (K.neg K.one)) (K.add (w ((ring m).add (ring m).one ((ring m).neg t))) (K.neg K.one)) = K.zero := by
      by_cases ht : t = (ring m).zero
      · rw [ht, hu0, show (ring m).add (ring m).one ((ring m).neg (ring m).zero) = (ring m).one by lib_ring, hw1]
        lib_ring
      · by_cases ht1 : t = (ring m).one
        · rw [ht1, hu1]; lib_ring
        · apply hsq t _ ht
          intro h
          apply ht1
          calc t = (ring m).add (ring m).zero t := by lib_ring
            _ = (ring m).add ((ring m).add (ring m).one ((ring m).neg t)) t := by rw [h]
            _ = (ring m).one := by lib_ring
    calc K.mul (u t) (w ((ring m).add (ring m).one ((ring m).neg t)))
        = K.add (K.add (K.add (u t) (w ((ring m).add (ring m).one ((ring m).neg t)))) (K.neg K.one))
            (K.mul (K.add (u t) (K.neg K.one)) (K.add (w ((ring m).add (ring m).one ((ring m).neg t))) (K.neg K.one))) := by lib_ring
      _ = _ := by rw [hz]; lib_ring
  rw [sumK_ext K _ _ pt, sumK_add, sumK_add, hsu, flip K m w (ring m).one, hsw, Stick1.sumK_negK K (fun _ => K.one)]
  show K.add (K.add K.zero K.zero) (K.neg (sumK K (fun _ : Zp m => K.one) (CMInert.allZ m (m + 1)))) = K.neg K.one
  rw [TeichChar.sum_one_allZ K m (m + 1)]
  show K.add (K.add K.zero K.zero) (K.neg (K.add (nsm K m K.one) K.one)) = K.neg K.one
  rw [hp1]
  lib_ring

theorem pw_one : ∀ c : Nat, pw K K.one c = K.one
  | 0 => rfl
  | c + 1 => by show K.mul (pw K K.one c) K.one = K.one; rw [pw_one c]; lib_ring

variable (π : Q) (hπ : K.mul π π = K.zero) (j : Nat) (g : Zp (j + 1 + 1))

/-- v_c = u^c = (1 + π)^(c·dlog ·). -/
def v (c : Nat) (a : Zp (j + 1 + 1)) : Q := pw K (PrimJC.u K π (j + 1) g a) c

include hπ in
theorem sq0_lin (c : Nat) (a : Q) :
    pw K (K.add K.one (K.mul a π)) c = K.add K.one (K.mul (K.mul (nsm K c K.one) a) π) := by
  have hx : K.mul (K.mul a π) (K.mul a π) = K.zero := by
    calc K.mul (K.mul a π) (K.mul a π) = K.mul (K.mul a a) (K.mul π π) := by lib_ring
      _ = K.zero := by rw [hπ]; lib_ring
  rw [Teich.sq0_pow K (K.mul a π) hx c, Teich.nsm_one_mul K c (K.mul a π)]
  lib_ring

include hπ in
/-- u(t) = 1 + dlog(t)·π and u^c(t) = 1 + c·dlog(t)·π for t ≠ 0. -/
theorem v_lin (c : Nat) (t : Zp (j + 1 + 1)) (ht : t ≠ (ring (j + 1 + 1)).zero) :
    v K π j g c t = K.add K.one (K.mul (K.mul (nsm K c K.one) (nsm K (DLogF.dlog (j + 1) g t) K.one)) π) := by
  unfold v PrimJC.u
  rw [ite_neg' _ _ ht, PrimJC.u_pow K π hπ, sq0_lin K π hπ]

include hπ in
theorem u_lin (t : Zp (j + 1 + 1)) (ht : t ≠ (ring (j + 1 + 1)).zero) :
    PrimJC.u K π (j + 1) g t = K.add K.one (K.mul (nsm K (DLogF.dlog (j + 1) g t) K.one) π) := by
  unfold PrimJC.u
  rw [ite_neg' _ _ ht, PrimJC.u_pow K π hπ]

theorem u_zero : PrimJC.u K π (j + 1) g (ring (j + 1 + 1)).zero = K.zero := by
  unfold PrimJC.u; rw [ite_pos' _ _ rfl]

theorem v_zero (c : Nat) : v K π j g (c + 1) (ring (j + 1 + 1)).zero = K.zero := by
  unfold v; rw [u_zero K π j g]; exact HasseCM.pw_zero_succ K c

theorem u_one (hpr : ∀ d, FTAChain.Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1)) :
    PrimJC.u K π (j + 1) g (ring (j + 1 + 1)).one = K.one := by
  have e : DLogF.dlog (j + 1) g (ring (j + 1 + 1)).one = 0 :=
    DLogF.dlog_pw (j + 1) hpr g hg (PrimJC.g_ne_zero (j + 1) g hg) 0 (Nat.zero_lt_succ (j + 1))
  unfold PrimJC.u; rw [ite_neg' _ _ (PowerSum.one_ne_zero' (j + 1)), e]; try rfl

theorem v_one (hpr : ∀ d, FTAChain.Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1)) (c : Nat) :
    v K π j g c (ring (j + 1 + 1)).one = K.one := by
  unfold v; rw [u_one K π j g hpr hg]; exact pw_one K c

include hπ in
theorem v_val (hpr : ∀ d, FTAChain.Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1)) (c k : Nat) (hk : k < j + 1 + 1) :
    v K π j g c (pw (ring (j + 1 + 1)) g k) = K.add K.one (K.mul (K.mul (nsm K c K.one) (nsm K k K.one)) π) := by
  rw [v_lin K π hπ j g c _ (DLog.pw_ne_zero (j + 1) hpr g (PrimJC.g_ne_zero (j + 1) g hg) k),
    DLogF.dlog_pw (j + 1) hpr g hg (PrimJC.g_ne_zero (j + 1) g hg) k hk]

include hπ in
theorem v_sum (hpr : ∀ d, FTAChain.Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1)) (c : Nat) :
    ∀ k, k ≤ j + 1 + 1 → sumK K (v K π j g c) (DLog.pows (j + 1) g k) =
      K.add (nsm K k K.one) (K.mul (K.mul (nsm K c K.one) (nsm K (PrimJC.tri k) K.one)) π)
  | 0, _ => by show K.zero = K.add K.zero (K.mul (K.mul (nsm K c K.one) K.zero) π); lib_ring
  | k + 1, hk => by
    show K.add (v K π j g c (pw (ring (j + 1 + 1)) g k)) (sumK K (v K π j g c) (DLog.pows (j + 1) g k)) =
      K.add (K.add (nsm K k K.one) K.one) (K.mul (K.mul (nsm K c K.one) (nsm K (PrimJC.tri k + k) K.one)) π)
    rw [v_val K π hπ j g hpr hg c k hk, v_sum hpr hg c k (Nat.le_of_succ_le hk), nsm_add]
    lib_ring

include hπ in
/-- **Σ_a u^(c+1)(a) = 0** when ℓ·1 = 0 and p − 1 = ℓ·2h. -/
theorem v_sum_all (hpr : ∀ d, FTAChain.Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1)) (l h : Nat) (hl : nsm K l K.one = K.zero)
    (hN : j + 1 + 1 = l * (2 * h)) (c : Nat) :
    sumK K (v K π j g (c + 1)) (L (j + 1 + 1)) = K.zero := by
  show sumK K (v K π j g (c + 1)) (CMInert.allZ (j + 1 + 1) (j + 1 + 1 + 1)) = K.zero
  rw [DLog.sum_split (j + 1) K hpr g hg, v_sum K π hπ j g hpr hg (c + 1) (j + 1 + 1) (Nat.le_refl _),
    PrimJC.tri_val l h (j + 1) hN, v_zero K π j g c, hN, nsm_mul_nat K l K.one (2 * h), nsm_mul_nat K l K.one (h * (j + 1)), hl,
    Teich.nsm_zero, Teich.nsm_zero]
  lib_ring

theorem hp1 (l h : Nat) (hl : nsm K l K.one = K.zero) (hN : j + 1 + 1 = l * (2 * h)) :
    nsm K (j + 1 + 1) K.one = K.zero := by
  rw [hN, nsm_mul_nat K l K.one (2 * h), hl, Teich.nsm_zero]

include hπ in
/-- **J(χ^(c+1), χ) ≡ −1 (mod (1 − ζ)²)**. -/
theorem jac_c (hpr : ∀ d, FTAChain.Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1)) (l h : Nat) (hl : nsm K l K.one = K.zero)
    (hN : j + 1 + 1 = l * (2 * h)) (c : Nat) :
    CharSumGen.J2 K j (CharSumGen.psp K j (PrimJC.u K π (j + 1) g) (c + 1)) (PrimJC.u K π (j + 1) g) = K.neg K.one := by
  show sumK K (fun t => K.mul (v K π j g (c + 1) t)
      (PrimJC.u K π (j + 1) g ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg t)))) (L (j + 1 + 1)) = K.neg K.one
  apply prim2 K (j + 1 + 1) (v K π j g (c + 1)) (PrimJC.u K π (j + 1) g)
  · exact v_zero K π j g c
  · exact v_one K π j g hpr hg (c + 1)
  · exact u_zero K π j g
  · exact u_one K π j g hpr hg
  · intro t s ht hs
    rw [v_lin K π hπ j g (c + 1) t ht, u_lin K π hπ j g s hs]
    calc K.mul (K.add (K.add K.one (K.mul (K.mul (nsm K (c + 1) K.one) (nsm K (DLogF.dlog (j + 1) g t) K.one)) π)) (K.neg K.one))
          (K.add (K.add K.one (K.mul (nsm K (DLogF.dlog (j + 1) g s) K.one) π)) (K.neg K.one))
        = K.mul (K.mul (K.mul (nsm K (c + 1) K.one) (nsm K (DLogF.dlog (j + 1) g t) K.one)) (nsm K (DLogF.dlog (j + 1) g s) K.one))
            (K.mul π π) := by lib_ring
      _ = K.zero := by rw [hπ]; lib_ring
  · exact v_sum_all K π hπ j g hpr hg l h hl hN c
  · exact PrimJC.sum_u K π hπ (j + 1) g hpr hg l h hl hN
  · exact hp1 K j l h hl hN

include hπ in
/-- **∏_{i ≤ k} J(χ^i, χ) ≡ (−1)^k (mod (1 − ζ)²)**. -/
theorem jpr_val (hpr : ∀ d, FTAChain.Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1)) (l h : Nat) (hl : nsm K l K.one = K.zero)
    (hN : j + 1 + 1 = l * (2 * h)) :
    ∀ k, CharSumGen.jpr K j (PrimJC.u K π (j + 1) g) k = pw K (K.neg K.one) k
  | 0 => rfl
  | k + 1 => by
    show K.mul (CharSumGen.jpr K j (PrimJC.u K π (j + 1) g) k)
        (CharSumGen.J2 K j (CharSumGen.psp K j (PrimJC.u K π (j + 1) g) (k + 1)) (PrimJC.u K π (j + 1) g)) =
      K.mul (pw K (K.neg K.one) k) (K.neg K.one)
    rw [jpr_val hpr hg l h hl hN k, jac_c K π hπ j g hpr hg l h hl hN k]

theorem neg_one_ne : (ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero := fun h => by
  apply PowerSum.one_ne_zero' (j + 1)
  calc (ring (j + 1 + 1)).one = (ring (j + 1 + 1)).neg ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) := by lib_ring
    _ = (ring (j + 1 + 1)).neg (ring (j + 1 + 1)).zero := by rw [h]
    _ = (ring (j + 1 + 1)).zero := by lib_ring

/-- **ι(−1) = −1** in 𝔽_p. -/
theorem iv_neg (hpr : ∀ d, FTAChain.Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) :
    CharSumGen.iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) = (ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one := by
  have e := CharSumGen.iv_mul j hpr _ (neg_one_ne j)
  calc CharSumGen.iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)
      = (ring (j + 1 + 1)).neg ((ring (j + 1 + 1)).mul (CharSumGen.iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))
          ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)) := by lib_ring
    _ = (ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one := by rw [e]

theorem lt_double : ∀ M : Nat, 0 < M → M < M + M
  | 0, h0 => absurd h0 (Nat.lt_irrefl 0)
  | M + 1, _ => Nat.lt_of_succ_le (Nat.add_le_add_left (Nat.succ_le_succ (Nat.zero_le M)) (M + 1))

theorem not_dvd_small (N M : Nat) (h0 : 0 < M) (hM : M < N) (hd : N ∣ M) : False := by
  obtain ⟨c, hc⟩ := hd
  cases c with
  | zero => rw [hc] at h0; exact Nat.lt_irrefl 0 h0
  | succ c =>
    have : N ≤ M := by
      rw [hc]
      exact Nat.le_trans (Nat.le_add_left N (N * c)) (Nat.le_of_eq (show N * c + N = N * (c + 1) from rfl))
    exact Nat.lt_irrefl M (Nat.lt_of_lt_of_le hM this)

include hπ in
/-- **χ(−1) ≡ 1**: g^(ℓh) = −1, so dlog(−1) = ℓh and (1 + π)^(ℓh) = 1 when ℓ·1 = 0. -/
theorem u_neg (hpr : ∀ d, FTAChain.Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1)) (l h : Nat) (hl : nsm K l K.one = K.zero)
    (hN : j + 1 + 1 = l * (2 * h)) :
    PrimJC.u K π (j + 1) g ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) = K.one := by
  have hM : j + 1 + 1 = l * h + l * h := hN.trans (by lib_nat)
  have hpos : 0 < l * h := by
    cases e : l * h with
    | zero => rw [e] at hM; exact Nat.noConfusion hM
    | succ M => exact Nat.zero_lt_succ M
  have hlt : l * h < j + 1 + 1 := by rw [hM]; exact lt_double (l * h) hpos
  have hsq : (ring (j + 1 + 1)).mul (pw (ring (j + 1 + 1)) g (l * h)) (pw (ring (j + 1 + 1)) g (l * h)) = (ring (j + 1 + 1)).one := by
    rw [← HasseCM.pw_add', ← hM]; exact hg.1
  have hy : pw (ring (j + 1 + 1)) g (l * h) = (ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one := by
    cases Euler.sq_one (ring (j + 1 + 1)) (ZMod.nozd (j + 1) hpr) _ hsq with
    | inl h1 => exact absurd (hg.2 (l * h) h1) (fun hd => not_dvd_small (j + 1 + 1) (l * h) hpos hlt hd)
    | inr h1 => exact h1
  rw [u_lin K π hπ j g _ (neg_one_ne j), ← hy, DLogF.dlog_pw (j + 1) hpr g hg (PrimJC.g_ne_zero (j + 1) g hg) (l * h) hlt,
    nsm_mul_nat K l K.one h, hl, Teich.nsm_zero]
  lib_ring

/-- p·1 = 1 in Q. -/
theorem pp_one (l h : Nat) (hl : nsm K l K.one = K.zero) (hN : j + 1 + 1 = l * (2 * h)) : CharSumGen.Pp K j = K.one := by
  show sumK K (fun _ : Zp (j + 1 + 1) => K.one) (CMInert.allZ (j + 1 + 1) (j + 1 + 1 + 1)) = K.one
  rw [TeichChar.sum_one_allZ K (j + 1 + 1) (j + 1 + 1 + 1)]
  show K.add (nsm K (j + 1 + 1) K.one) K.one = K.one
  rw [hp1 K j l h hl hN]
  lib_ring

theorem pw_neg_odd : ∀ r : Nat, pw K (K.neg K.one) (2 * r + 1) = K.neg K.one
  | 0 => by show K.mul K.one (K.neg K.one) = K.neg K.one; lib_ring
  | r + 1 => by
    show K.mul (K.mul (pw K (K.neg K.one) (2 * r + 1)) (K.neg K.one)) (K.neg K.one) = K.neg K.one
    rw [pw_neg_odd r]; lib_ring

include hπ in
/-- **A·p ≡ −1 (mod (1 − ζ)²)**, A = (∏_{i ≤ k} J(χ^i, χ))·χ(ι(−1)), k odd (k = ℓ − 2): the coefficient of
    G(χ)^ℓ = A·(pζ⁰ − N) (CharSumGen.gpow_n) and the base of the criterion χ(q⁻¹) = (A p)^m (NthRecip) is primary. -/
theorem gauss_prim (hpr : ∀ d, FTAChain.Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1)) (l h : Nat) (hl : nsm K l K.one = K.zero)
    (hN : j + 1 + 1 = l * (2 * h)) (r : Nat) :
    K.mul (K.mul (CharSumGen.jpr K j (PrimJC.u K π (j + 1) g) (2 * r + 1))
        (PrimJC.u K π (j + 1) g (CharSumGen.iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))))
      (CharSumGen.Pp K j) = K.neg K.one := by
  rw [jpr_val K π hπ j g hpr hg l h hl hN, pw_neg_odd K r, iv_neg j hpr, u_neg K π hπ j g hpr hg l h hl hN,
    pp_one K j l h hl hN]
  lib_ring

end PrimA

#print axioms PrimA.prim2
#print axioms PrimA.pw_one
#print axioms PrimA.sq0_lin
#print axioms PrimA.v_lin
#print axioms PrimA.u_lin
#print axioms PrimA.u_zero
#print axioms PrimA.v_zero
#print axioms PrimA.u_one
#print axioms PrimA.v_one
#print axioms PrimA.v_val
#print axioms PrimA.v_sum
#print axioms PrimA.v_sum_all
#print axioms PrimA.hp1
#print axioms PrimA.jac_c
#print axioms PrimA.jpr_val
#print axioms PrimA.neg_one_ne
#print axioms PrimA.iv_neg
#print axioms PrimA.lt_double
#print axioms PrimA.not_dvd_small
#print axioms PrimA.u_neg
#print axioms PrimA.pp_one
#print axioms PrimA.pw_neg_odd
#print axioms PrimA.gauss_prim

lang/PrimAlpha1.lean

lean_all/lang/PrimAlpha1.lean

/-
  lang/PrimAlpha1 — T49 (primary elements, step 2): **evaluation at ζ = 1 + π in D = 𝔽_ℓ[π]/(π²)**.
  ℓ = n + 2 prime, F = 𝔽_ℓ, D = F[π]/(π²), z = 1 + π, φ₁ = φ_z : ℤ[C_ℓ] → D.
    nsm_D        k·1 = (k·1, 0) in D
    hl_D         ℓ·1 = 0 in D
    z_ell        z^ℓ = 1
    z_pw         z^k = (1, k·1)
    sum_rng      Σ_(i<k) z^i = (k·1, tri(k)·1)
    sum_z        Σ_b z^b = 0 (ℓ odd)
    psi_z        z^c = ψ_c(z)
    phz_psi      φ₁(σ(d) v) = ψ_c(φ₁(v))      (c·d = 1)
    Prim         v is primary :⟺ the π-part of φ₁(v) is 0  (Σ_i i·v(i) ≡ 0 mod ℓ)
    prim_gsig    v primary ⇒ σ(d) v primary with the same constant part
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.DualL
import lang.CycTau
import lang.CycGalEv
import lang.PrimJC
import lib.RingTac
import lib.Semi

namespace PrimAlpha1

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

variable (n : Nat)

/-- D = 𝔽_ℓ[π]/(π²). -/
abbrev D := DualL.dring (ring (n + 1))
/-- z = 1 + π. -/
def z : Zp (n + 1) × Zp (n + 1) := (D n).add (D n).one (DualL.pi (ring (n + 1)))

theorem nsm_D : ∀ k, nsm (D n) k (D n).one = (nsm (ring (n + 1)) k (ring (n + 1)).one, (ring (n + 1)).zero)
  | 0 => rfl
  | k + 1 => by
    rw [show nsm (D n) (k + 1) (D n).one = (D n).add (nsm (D n) k (D n).one) (D n).one from rfl, nsm_D k]
    show ((ring (n + 1)).add (nsm (ring (n + 1)) k (ring (n + 1)).one) (ring (n + 1)).one,
        (ring (n + 1)).add (ring (n + 1)).zero (ring (n + 1)).zero)
      = ((ring (n + 1)).add (nsm (ring (n + 1)) k (ring (n + 1)).one) (ring (n + 1)).one, (ring (n + 1)).zero)
    exact DualL.dext rfl (by lib_ring)

theorem hl_D : nsm (D n) (n + 1 + 1) (D n).one = (D n).zero := by
  rw [nsm_D, ZMod.char_p (n + 1)]
  rfl

theorem z_ell : pw (D n) (z n) (n + 1 + 1) = (D n).one :=
  CycPrim.z_pow (D n) (DualL.pi (ring (n + 1))) (DualL.pi_sq (ring (n + 1))) (n + 1) (hl_D n)

theorem z_pw : ∀ k, pw (D n) (z n) k = ((ring (n + 1)).one, nsm (ring (n + 1)) k (ring (n + 1)).one)
  | 0 => rfl
  | k + 1 => by
    show (D n).mul (pw (D n) (z n) k) (z n) = _
    rw [z_pw k]
    show ((ring (n + 1)).mul (ring (n + 1)).one ((ring (n + 1)).add (ring (n + 1)).one (ring (n + 1)).zero),
        (ring (n + 1)).add ((ring (n + 1)).mul (ring (n + 1)).one ((ring (n + 1)).add (ring (n + 1)).zero (ring (n + 1)).one))
          ((ring (n + 1)).mul (nsm (ring (n + 1)) k (ring (n + 1)).one) ((ring (n + 1)).add (ring (n + 1)).one (ring (n + 1)).zero)))
      = ((ring (n + 1)).one, (ring (n + 1)).add (nsm (ring (n + 1)) k (ring (n + 1)).one) (ring (n + 1)).one)
    exact DualL.dext (by lib_ring) (by lib_ring)

theorem nu_add (a : Nat) : ∀ b, nsm (ring (n + 1)) (a + b) (ring (n + 1)).one
    = (ring (n + 1)).add (nsm (ring (n + 1)) a (ring (n + 1)).one) (nsm (ring (n + 1)) b (ring (n + 1)).one)
  | 0 => by
    show nsm (ring (n + 1)) a (ring (n + 1)).one = (ring (n + 1)).add (nsm (ring (n + 1)) a (ring (n + 1)).one) (ring (n + 1)).zero
    lib_ring
  | b + 1 => by
    show (ring (n + 1)).add (nsm (ring (n + 1)) (a + b) (ring (n + 1)).one) (ring (n + 1)).one
      = (ring (n + 1)).add (nsm (ring (n + 1)) a (ring (n + 1)).one)
          ((ring (n + 1)).add (nsm (ring (n + 1)) b (ring (n + 1)).one) (ring (n + 1)).one)
    rw [nu_add a b]
    lib_ring

theorem nu_mul (a : Nat) : ∀ b, nsm (ring (n + 1)) (a * b) (ring (n + 1)).one
    = (ring (n + 1)).mul (nsm (ring (n + 1)) a (ring (n + 1)).one) (nsm (ring (n + 1)) b (ring (n + 1)).one)
  | 0 => by
    show (ring (n + 1)).zero = (ring (n + 1)).mul (nsm (ring (n + 1)) a (ring (n + 1)).one) (ring (n + 1)).zero
    lib_ring
  | b + 1 => by
    show nsm (ring (n + 1)) (a * b + a) (ring (n + 1)).one
      = (ring (n + 1)).mul (nsm (ring (n + 1)) a (ring (n + 1)).one)
          ((ring (n + 1)).add (nsm (ring (n + 1)) b (ring (n + 1)).one) (ring (n + 1)).one)
    rw [nu_add n (a * b) a, nu_mul a b]
    lib_ring

theorem sum_rng : ∀ k, sumK (D n) (fun i => pw (D n) (z n) i) (CycUnit.rng k)
    = (nsm (ring (n + 1)) k (ring (n + 1)).one, nsm (ring (n + 1)) (PrimJC.tri k) (ring (n + 1)).one)
  | 0 => rfl
  | k + 1 => by
    show sumK (D n) (fun i => pw (D n) (z n) i) (CycUnit.rng k ++ [k]) = _
    rw [sumK_append, sum_rng k]
    show (D n).add _ ((D n).add (pw (D n) (z n) k) (D n).zero) = _
    rw [z_pw]
    show ((ring (n + 1)).add (nsm (ring (n + 1)) k (ring (n + 1)).one) ((ring (n + 1)).add (ring (n + 1)).one (ring (n + 1)).zero),
        (ring (n + 1)).add (nsm (ring (n + 1)) (PrimJC.tri k) (ring (n + 1)).one)
          ((ring (n + 1)).add (nsm (ring (n + 1)) k (ring (n + 1)).one) (ring (n + 1)).zero))
      = ((ring (n + 1)).add (nsm (ring (n + 1)) k (ring (n + 1)).one) (ring (n + 1)).one,
          nsm (ring (n + 1)) (PrimJC.tri k + k) (ring (n + 1)).one)
    rw [nu_add n (PrimJC.tri k) k]
    exact DualL.dext (by lib_ring) (by lib_ring)

/-- **Σ_b z^b = 0** in D (ℓ = 2h + 1). -/
theorem sum_z (h : Nat) (hh : n + 1 = h + h) :
    sumK (D n) (fun b : Zp (n + 1) => pw (D n) (z n) b.val) (L (n + 1)) = (D n).zero := by
  show sumK (D n) (fun b : Zp (n + 1) => pw (D n) (z n) b.val) (CMInert.allZ (n + 1) (n + 1 + 1)) = _
  rw [CycUnit.sum_allZ (n + 1) (D n) _ (n + 1 + 1)]
  have e1 : sumK (D n) (fun i => pw (D n) (z n) (md (n + 1) i)) (CycUnit.rng (n + 1 + 1))
      = sumK (D n) (fun i => pw (D n) (z n) i) (CycUnit.rng (n + 1 + 1)) :=
    sumK_ext (D n) _ _ (fun i => GEval.pw_md (D n) (n + 1) _ (z_ell n) i) _
  refine Eq.trans e1 ?_
  rw [sum_rng]
  have ht : PrimJC.tri (n + 1 + 1) = (n + 1 + 1) * h :=
    PrimJC.double_inj _ _ ((PrimJC.two_tri (n + 1)).trans (by rw [hh]; lib_nat))
  rw [ht, nu_mul, ZMod.char_p (n + 1)]
  exact DualL.dext rfl (by lib_ring)

/-- **z^c = ψ_c(z)**. -/
theorem psi_z (c : Zp (n + 1)) : pw (D n) (z n) c.val = DualL.psi (ring (n + 1)) c (z n) := by
  rw [z_pw, ZMod.every_elem]
  show _ = ((ring (n + 1)).add (ring (n + 1)).one (ring (n + 1)).zero,
      (ring (n + 1)).mul c ((ring (n + 1)).add (ring (n + 1)).zero (ring (n + 1)).one))
  exact DualL.dext (by lib_ring) (by lib_ring)

/-- **φ₁(σ(d) v) = ψ_c(φ₁(v))** for c·d = 1. -/
theorem phz_psi (c d : Zp (n + 1)) (hcd : (ring (n + 1)).mul c d = (ring (n + 1)).one) (v : V Int (n + 1 + 1)) :
    CycEis.phz (D n) (n + 1) (z n) (CycGal.gsig LibInt.intR (n + 1) d v)
      = DualL.psi (ring (n + 1)) c (CycEis.phz (D n) (n + 1) (z n) v) := by
  rw [CycGalEv.phz_gsig (D n) (n + 1) (z n) (z_ell n) c d hcd v, psi_z]
  exact (CycTau.hom_phz (D n) (D n) (DualL.psi (ring (n + 1)) c) (DualL.psi_add (ring (n + 1)) c)
    (DualL.psi_mul (ring (n + 1)) c) (DualL.psi_one (ring (n + 1)) c) (n + 1) (z n) v).symm

/-- v is primary: the π-part of φ₁(v) vanishes. -/
def Prim (v : V Int (n + 1 + 1)) : Prop := (CycEis.phz (D n) (n + 1) (z n) v).2 = (ring (n + 1)).zero

theorem prim_gsig (c d : Zp (n + 1)) (hcd : (ring (n + 1)).mul c d = (ring (n + 1)).one) (v : V Int (n + 1 + 1))
    (hv : Prim n v) :
    Prim n (CycGal.gsig LibInt.intR (n + 1) d v) ∧
      (CycEis.phz (D n) (n + 1) (z n) (CycGal.gsig LibInt.intR (n + 1) d v)).1 = (CycEis.phz (D n) (n + 1) (z n) v).1 := by
  unfold Prim at hv ⊢
  rw [phz_psi n c d hcd v]
  refine ⟨?_, rfl⟩
  show (ring (n + 1)).mul c (CycEis.phz (D n) (n + 1) (z n) v).2 = _
  rw [hv]
  lib_ring

end PrimAlpha1

#print axioms PrimAlpha1.nsm_D
#print axioms PrimAlpha1.hl_D
#print axioms PrimAlpha1.z_ell
#print axioms PrimAlpha1.z_pw
#print axioms PrimAlpha1.nu_add
#print axioms PrimAlpha1.nu_mul
#print axioms PrimAlpha1.sum_rng
#print axioms PrimAlpha1.sum_z
#print axioms PrimAlpha1.psi_z
#print axioms PrimAlpha1.phz_psi
#print axioms PrimAlpha1.prim_gsig

lang/PrimAlpha2.lean

lean_all/lang/PrimAlpha2.lean

/-
  lang/PrimAlpha2 — T49 (primary elements, step 3): **for primary α the root of unity in A ≡ ±σ^(j₀)B is ±1**.
  In D = 𝔽_ℓ[π]/(π²) with z = 1 + π: φ₁(A) = −1 (A primary, p ≡ 1 mod ℓ), φ₁(B) has π-part 0 when α is primary,
  and φ₁ kills N; comparing π-parts of φ₁(A) = ε·(1 + j₀π)·φ₁(B) gives j₀ = 0.
    castN_D, castZ_snd     integers map to D with π-part 0
    snd_pw                 π-part 0 is stable under powers
    B_snd                  α primary, all d ≠ 0 ⇒ φ₁(B) has π-part 0
    pp_one                 p ≡ 1 (mod ℓ) ⇒ p·1 = 1 in D
    A_val                  φ₁(A) = −1
    j0_zero                A ≡ ε σ^(j₀) B (mod N) ⇒ j₀ = 0
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.PrimAlpha1
import lang.PhzModN
import lang.SymB1
import lib.RingTac
import lib.Semi

namespace PrimAlpha2

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob
open LibRing (castZ castN)

variable (n : Nat)

theorem castN_D : ∀ k, castN (PrimAlpha1.D n) k = (castN (ring (n + 1)) k, (ring (n + 1)).zero)
  | 0 => rfl
  | k + 1 => by
    show (PrimAlpha1.D n).add (PrimAlpha1.D n).one (castN (PrimAlpha1.D n) k) = _
    rw [castN_D k]
    show ((ring (n + 1)).add (ring (n + 1)).one (castN (ring (n + 1)) k), (ring (n + 1)).add (ring (n + 1)).zero (ring (n + 1)).zero)
      = ((ring (n + 1)).add (ring (n + 1)).one (castN (ring (n + 1)) k), (ring (n + 1)).zero)
    exact DualL.dext rfl (by lib_ring)

theorem castZ_snd : ∀ ε : Int, (castZ (PrimAlpha1.D n) ε).2 = (ring (n + 1)).zero
  | .ofNat k => by
    show (castN (PrimAlpha1.D n) k).2 = _
    rw [castN_D]
  | .negSucc k => by
    show ((PrimAlpha1.D n).neg (castN (PrimAlpha1.D n) (k + 1))).2 = _
    rw [castN_D]
    show (ring (n + 1)).neg (ring (n + 1)).zero = (ring (n + 1)).zero
    lib_ring

theorem snd_pw (x : Zp (n + 1) × Zp (n + 1)) (hx : x.2 = (ring (n + 1)).zero) :
    ∀ e, (pw (PrimAlpha1.D n) x e).2 = (ring (n + 1)).zero
  | 0 => rfl
  | e + 1 => DualL.snd_mul_zero (ring (n + 1)) _ _ (snd_pw x hx e) hx

variable (hℓ : ∀ d, Dv d (n + 1 + 1) → d = 1 ∨ d = n + 1 + 1)

include hℓ in
/-- **φ₁(B) has π-part 0** for primary α. -/
theorem B_snd (α : V Int (n + 1 + 1)) (hα : PrimAlpha1.Prim n α) : ∀ qs, SymB1.NZ n qs →
    (PhzPw.evBp (PrimAlpha1.D n) (n + 1) (PrimAlpha1.z n) (StickBB.ents (n + 1) α qs)).2 = (ring (n + 1)).zero
  | [], _ => rfl
  | q :: qs, ⟨hq, hqs⟩ => by
    have h1 := (PrimAlpha1.prim_gsig n _ q.1 (SymB1.inv_ne n hℓ q.1 hq) α hα).1
    have h2 := (PrimAlpha1.prim_gsig n _ _ (SymB1.inv_ne n hℓ _ (SymB1.ed_ne n q.1 hq)) α hα).1
    exact DualL.snd_mul_zero (ring (n + 1)) _ _
      (DualL.snd_mul_zero (ring (n + 1)) _ _ (snd_pw n _ h1 q.2.1) (snd_pw n _ h2 q.2.2)) (B_snd α hα qs hqs)

/-- **p·1 = 1 in D** when p = ℓ·(2h) + 1. -/
theorem pp_one (j h : Nat) (hN : j + 1 + 1 = (n + 1 + 1) * (2 * h)) :
    CharSumGen.Pp (PrimAlpha1.D n) j = (PrimAlpha1.D n).one := by
  rw [NthEis.pp_nsm, PrimAlpha1.nsm_D]
  show (nsm (ring (n + 1)) (j + 1 + 1 + 1) (ring (n + 1)).one, (ring (n + 1)).zero) = ((ring (n + 1)).one, (ring (n + 1)).zero)
  rw [show j + 1 + 1 + 1 = (n + 1 + 1) * (2 * h) + 1 by rw [← hN]]
  show ((ring (n + 1)).add (nsm (ring (n + 1)) ((n + 1 + 1) * (2 * h)) (ring (n + 1)).one) (ring (n + 1)).one, (ring (n + 1)).zero) = _
  rw [PrimAlpha1.nu_mul, ZMod.char_p (n + 1)]
  exact DualL.dext (by lib_ring) rfl

/-- **φ₁(A) = −1** (A of odd degree k = 2r + 1, p − 1 = ℓ·2h). -/
theorem A_val (j : Nat) (g : Zp (j + 1 + 1)) (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1)) (h : Nat) (hN : j + 1 + 1 = (n + 1 + 1) * (2 * h))
    (k r : Nat) (hk : k = 2 * r + 1) :
    CycEis.phz (PrimAlpha1.D n) (n + 1) (PrimAlpha1.z n)
        (EisBridge.AA (gring LibInt.intR (n + 1)) j (CycPrim.chi (n + 1) j g) k) = (PrimAlpha1.D n).neg (PrimAlpha1.D n).one := by
  subst hk
  have e := CycPrim.cyc_primary (PrimAlpha1.D n) (DualL.pi (ring (n + 1))) (DualL.pi_sq (ring (n + 1))) (n + 1) j g r
    (PrimAlpha1.hl_D n) hpr hg h hN
  rw [pp_one n j h hN] at e
  rw [← e]
  show _ = (PrimAlpha1.D n).mul _ (PrimAlpha1.D n).one
  exact (((PrimAlpha1.D n).mul_comm _ _).trans ((PrimAlpha1.D n).one_mul _)).symm

/-- **j₀ = 0 for primary α**: φ₁(A) = −1, A ≡ ε σ^(j₀) B (mod N), φ₁(B) with π-part 0 ⇒ j₀ = 0. -/
theorem j0_zero (h : Nat) (hh : n + 1 = h + h) (A B : V Int (n + 1 + 1))
    (hA : CycEis.phz (PrimAlpha1.D n) (n + 1) (PrimAlpha1.z n) A = (PrimAlpha1.D n).neg (PrimAlpha1.D n).one)
    (hB : (CycEis.phz (PrimAlpha1.D n) (n + 1) (PrimAlpha1.z n) B).2 = (ring (n + 1)).zero)
    (jj : Zp (n + 1)) (ε : Int)
    (hmod : ModN.ModN (n + 1) A (gmul LibInt.intR (n + 1) (mono LibInt.intR (n + 1) jj ε) B)) :
    jj = (ring (n + 1)).zero := by
  have e := PhzModN.phz_modn (PrimAlpha1.D n) (n + 1) (PrimAlpha1.z n) (PrimAlpha1.sum_z n h hh) _ _ hmod
  rw [hA] at e
  have e2 : CycEis.phz (PrimAlpha1.D n) (n + 1) (PrimAlpha1.z n) (gmul LibInt.intR (n + 1) (mono LibInt.intR (n + 1) jj ε) B)
      = (PrimAlpha1.D n).mul ((PrimAlpha1.D n).mul (castZ (PrimAlpha1.D n) ε) ((ring (n + 1)).one, jj))
          (CycEis.phz (PrimAlpha1.D n) (n + 1) (PrimAlpha1.z n) B) := by
    show CycEis.phz (PrimAlpha1.D n) (n + 1) (PrimAlpha1.z n) ((gring LibInt.intR (n + 1)).mul (mono LibInt.intR (n + 1) jj ε) B) = _
    rw [CycEis.phz_mul (PrimAlpha1.D n) (n + 1) _ (PrimAlpha1.z_ell n), PhzModN.phz_mono, PrimAlpha1.z_pw, ZMod.every_elem]
  rw [e2] at e
  have hX := castZ_snd n ε
  generalize castZ (PrimAlpha1.D n) ε = X at e hX
  generalize CycEis.phz (PrimAlpha1.D n) (n + 1) (PrimAlpha1.z n) B = Y at e hB
  have hf := congrArg Prod.fst e
  have hs := congrArg Prod.snd e
  change (ring (n + 1)).neg (ring (n + 1)).one
      = (ring (n + 1)).mul ((ring (n + 1)).mul X.1 (ring (n + 1)).one) Y.1 at hf
  change (ring (n + 1)).neg (ring (n + 1)).zero
      = (ring (n + 1)).add ((ring (n + 1)).mul ((ring (n + 1)).mul X.1 (ring (n + 1)).one) Y.2)
          ((ring (n + 1)).mul ((ring (n + 1)).add ((ring (n + 1)).mul X.1 jj) ((ring (n + 1)).mul X.2 (ring (n + 1)).one)) Y.1) at hs
  rw [hX, hB] at hs
  calc jj = (ring (n + 1)).neg ((ring (n + 1)).mul jj ((ring (n + 1)).neg (ring (n + 1)).one)) := by lib_ring
    _ = (ring (n + 1)).neg ((ring (n + 1)).mul jj ((ring (n + 1)).mul ((ring (n + 1)).mul X.1 (ring (n + 1)).one) Y.1)) := by
        rw [← hf]
    _ = (ring (n + 1)).neg ((ring (n + 1)).add ((ring (n + 1)).mul ((ring (n + 1)).mul X.1 (ring (n + 1)).one) (ring (n + 1)).zero)
          ((ring (n + 1)).mul ((ring (n + 1)).add ((ring (n + 1)).mul X.1 jj) ((ring (n + 1)).mul (ring (n + 1)).zero (ring (n + 1)).one)) Y.1)) := by
        lib_ring
    _ = (ring (n + 1)).neg ((ring (n + 1)).neg (ring (n + 1)).zero) := by rw [← hs]
    _ = (ring (n + 1)).zero := by lib_ring

end PrimAlpha2

#print axioms PrimAlpha2.castN_D
#print axioms PrimAlpha2.castZ_snd
#print axioms PrimAlpha2.snd_pw
#print axioms PrimAlpha2.B_snd
#print axioms PrimAlpha2.pp_one
#print axioms PrimAlpha2.A_val
#print axioms PrimAlpha2.j0_zero

lang/PrimJ.lean

lean_all/lang/PrimJ.lean

/-
  lang/PrimJ — T49 (general ℓ), cycle 829: **the Jacobi sum is primary**, in computational form.
  In any ring Q (think Q = ℤ[ζ_ℓ]/(1 − ζ)², u = image of a character χ of 𝔽_p), if
    u(0) = 0, u(1) = 1, (u(t) − 1)(u(s) − 1) = 0 for t, s ≠ 0, Σ_t u(t) = 0, (p − 1)·1 = 0 in Q,
  then  Σ_t u(t) u(1 − t) = −1,   i.e.  J(χ, χ) ≡ −1 (mod (1 − ζ)²).
  Proof: u_t u_(1−t) = u_t + u_(1−t) − 1 + (u_t − 1)(u_(1−t) − 1); the last term is 0 for every t (t = 0, 1 by u(0) = 0, u(1) = 1),
  Σ u_(1−t) = Σ u_t = 0 (GroupRing.flip), Σ 1 = p.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.TeichChar3
import lib.RingTac

namespace PrimJ

open ZMod Frobenius YMChain GroupRing

variable {Q : Type} (K : DetChain.CRing Q) (m : Nat)

theorem prim (u : Zp m → Q) (h0 : u (ring m).zero = K.zero) (h1 : u (ring m).one = K.one)
    (hsq : ∀ t s, t ≠ (ring m).zero → s ≠ (ring m).zero → K.mul (K.add (u t) (K.neg K.one)) (K.add (u s) (K.neg K.one)) = K.zero)
    (hs : sumK K u (L m) = K.zero) (hp1 : nsm K m K.one = K.zero) :
    sumK K (fun t => K.mul (u t) (u ((ring m).add (ring m).one ((ring m).neg t)))) (L m) = K.neg K.one := by
  have pt : ∀ t, K.mul (u t) (u ((ring m).add (ring m).one ((ring m).neg t))) =
      K.add (K.add (u t) (u ((ring m).add (ring m).one ((ring m).neg t)))) (K.neg K.one) := fun t => by
    have hz : K.mul (K.add (u t) (K.neg K.one)) (K.add (u ((ring m).add (ring m).one ((ring m).neg t))) (K.neg K.one)) = K.zero := by
      by_cases ht : t = (ring m).zero
      · rw [ht, h0, show (ring m).add (ring m).one ((ring m).neg (ring m).zero) = (ring m).one by lib_ring, h1]
        lib_ring
      · by_cases ht1 : t = (ring m).one
        · rw [ht1, h1]; lib_ring
        · apply hsq t _ ht
          intro h
          apply ht1
          calc t = (ring m).add (ring m).zero t := by lib_ring
            _ = (ring m).add ((ring m).add (ring m).one ((ring m).neg t)) t := by rw [h]
            _ = (ring m).one := by lib_ring
    calc K.mul (u t) (u ((ring m).add (ring m).one ((ring m).neg t)))
        = K.add (K.add (K.add (u t) (u ((ring m).add (ring m).one ((ring m).neg t)))) (K.neg K.one))
            (K.mul (K.add (u t) (K.neg K.one)) (K.add (u ((ring m).add (ring m).one ((ring m).neg t))) (K.neg K.one))) := by lib_ring
      _ = _ := by rw [hz]; lib_ring
  rw [sumK_ext K _ _ pt, sumK_add, sumK_add, hs, flip K m u (ring m).one, hs, Stick1.sumK_negK K (fun _ => K.one)]
  show K.add (K.add K.zero K.zero) (K.neg (sumK K (fun _ : Zp m => K.one) (CMInert.allZ m (m + 1)))) = K.neg K.one
  rw [TeichChar.sum_one_allZ K m (m + 1)]
  show K.add (K.add K.zero K.zero) (K.neg (K.add (nsm K m K.one) K.one)) = K.neg K.one
  rw [hp1]
  lib_ring

end PrimJ

#print axioms PrimJ.prim

lang/PrimJC.lean

lean_all/lang/PrimJC.lean

/-
  lang/PrimJC — T49 (general ℓ), cycle 829: **the concrete character for primarity**. In a ring Q with π² = 0 and ℓ·1 = 0
  (think Q = ℤ[ζ_ℓ]/(1 − ζ)², π = ζ − 1), p − 1 = ℓ·2h, g a primitive root mod p:
    u(a) = (1 + π)^(dlog a)  (u(0) = 0)    satisfies the hypotheses of PrimJ.prim, hence  Σ_t u(t) u(1 − t) = −1,
  i.e. the Jacobi sum J(χ, χ) of χ(a) = ζ^(dlog a) is primary: J ≡ −1 (mod (1 − ζ)²).
    tri, double_inj, tri_val, u_pow, sum_pows, sum_u, prim_conc
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.PrimJ
import lang.DLogF
import lib.RingTac
import lib.Semi
import FiniteRecChain

namespace PrimJC

open ZMod Frobenius YMChain
open FiniteRecChain (ite_pos' ite_neg')

def tri : Nat → Nat
  | 0 => 0
  | k + 1 => tri k + k

theorem double_inj : ∀ a b : Nat, a + a = b + b → a = b
  | 0, 0, _ => rfl
  | 0, b + 1, h => by
    have h2 : b + b + 1 + 1 = 0 := (show b + b + 1 + 1 = b + 1 + (b + 1) by lib_nat).trans h.symm
    nomatch h2
  | a + 1, 0, h => by
    have h2 : a + a + 1 + 1 = 0 := (show a + a + 1 + 1 = a + 1 + (a + 1) by lib_nat).trans h
    nomatch h2
  | a + 1, b + 1, h => by
    have h' : a + a = b + b := Nat.succ.inj (Nat.succ.inj ((show a + a + 1 + 1 = a + 1 + (a + 1) by lib_nat).trans
      (h.trans (show b + 1 + (b + 1) = b + b + 1 + 1 by lib_nat))))
    rw [double_inj a b h']

theorem two_tri : ∀ k : Nat, tri (k + 1) + tri (k + 1) = (k + 1) * k
  | 0 => rfl
  | k + 1 => by
    show tri (k + 1) + (k + 1) + (tri (k + 1) + (k + 1)) = (k + 1 + 1) * (k + 1)
    calc tri (k + 1) + (k + 1) + (tri (k + 1) + (k + 1)) = (tri (k + 1) + tri (k + 1)) + (k + 1) + (k + 1) := by lib_nat
      _ = (k + 1) * k + (k + 1) + (k + 1) := by rw [two_tri k]
      _ = _ := by lib_nat

/-- tri(N) = ℓ·h·(N − 1) when N = ℓ·2h. -/
theorem tri_val (l h M' : Nat) (hN : M' + 1 = l * (2 * h)) : tri (M' + 1) = l * (h * M') :=
  double_inj _ _ (by rw [two_tri, hN]; lib_nat)

variable {Q : Type} (K : DetChain.CRing Q) (π : Q) (hπ : K.mul π π = K.zero)

include hπ in
theorem u_pow (k : Nat) : pw K (K.add K.one π) k = K.add K.one (K.mul (nsm K k K.one) π) := by
  rw [Teich.sq0_pow K π hπ k, Teich.nsm_one_mul K k π]

variable (J : Nat) (g : Zp (J + 1))

/-- u(a) = (1 + π)^(dlog a), u(0) = 0. -/
def u (a : Zp (J + 1)) : Q := if a = (ring (J + 1)).zero then K.zero else pw K (K.add K.one π) (DLogF.dlog J g a)

theorem g_ne_zero (hg : OrdMul.Ord (ring (J + 1)) g (J + 1)) : g ≠ (ring (J + 1)).zero := fun h => by
  have := hg.1; rw [h, HasseCM.pw_zero_succ] at this; exact PowerSum.one_ne_zero' J this.symm

include hπ in
theorem sum_pows (hpr : ∀ d, FTAChain.Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hg : OrdMul.Ord (ring (J + 1)) g (J + 1)) :
    ∀ k, k ≤ J + 1 → sumK K (u K π J g) (DLog.pows J g k) = K.add (nsm K k K.one) (K.mul (nsm K (tri k) K.one) π)
  | 0, _ => by show K.zero = K.add K.zero (K.mul K.zero π); lib_ring
  | k + 1, hk => by
    have hne := DLog.pw_ne_zero J hpr g (g_ne_zero J g hg) k
    have hu : u K π J g (pw (ring (J + 1)) g k) = K.add K.one (K.mul (nsm K k K.one) π) := by
      unfold u
      rw [ite_neg' _ _ hne, DLogF.dlog_pw J hpr g hg (g_ne_zero J g hg) k hk, u_pow K π hπ]
    show K.add (u K π J g (pw (ring (J + 1)) g k)) (sumK K (u K π J g) (DLog.pows J g k)) =
      K.add (K.add (nsm K k K.one) K.one) (K.mul (nsm K (tri k + k) K.one) π)
    rw [hu, sum_pows hpr hg k (Nat.le_of_succ_le hk), nsm_add]
    lib_ring

include hπ in
/-- **Σ_a u(a) = 0** when ℓ·1 = 0 and p − 1 = ℓ·2h. -/
theorem sum_u (hpr : ∀ d, FTAChain.Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hg : OrdMul.Ord (ring (J + 1)) g (J + 1))
    (l h : Nat) (hl : nsm K l K.one = K.zero) (hN : J + 1 = l * (2 * h)) :
    sumK K (u K π J g) (GroupRing.L (J + 1)) = K.zero := by
  show sumK K (u K π J g) (CMInert.allZ (J + 1) (J + 1 + 1)) = K.zero
  rw [DLog.sum_split J K hpr g hg, sum_pows K π hπ J g hpr hg (J + 1) (Nat.le_refl _), tri_val l h J hN]
  have h0 : u K π J g (ring (J + 1)).zero = K.zero := by unfold u; rw [ite_pos' _ _ rfl]
  rw [h0, hN, nsm_mul_nat K l K.one (2 * h), nsm_mul_nat K l K.one (h * J), hl, Teich.nsm_zero, Teich.nsm_zero]
  lib_ring

include hπ in
/-- **J(χ, χ) ≡ −1 (mod (1 − ζ)²)** for χ = ζ^(dlog ·). -/
theorem prim_conc (hpr : ∀ d, FTAChain.Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hg : OrdMul.Ord (ring (J + 1)) g (J + 1))
    (l h : Nat) (hl : nsm K l K.one = K.zero) (hN : J + 1 = l * (2 * h)) :
    sumK K (fun t => K.mul (u K π J g t) (u K π J g ((ring (J + 1)).add (ring (J + 1)).one ((ring (J + 1)).neg t))))
      (GroupRing.L (J + 1)) = K.neg K.one := by
  apply PrimJ.prim K (J + 1) (u K π J g)
  · unfold u; rw [ite_pos' _ _ rfl]
  · have e : DLogF.dlog J g (ring (J + 1)).one = 0 := DLogF.dlog_pw J hpr g hg (g_ne_zero J g hg) 0 (Nat.zero_lt_succ J)
    unfold u; rw [ite_neg' _ _ (PowerSum.one_ne_zero' J), e]; try rfl
  · intro t s ht hs
    unfold u
    rw [ite_neg' _ _ ht, ite_neg' _ _ hs, u_pow K π hπ, u_pow K π hπ]
    calc K.mul (K.add (K.add K.one (K.mul (nsm K (DLogF.dlog J g t) K.one) π)) (K.neg K.one))
          (K.add (K.add K.one (K.mul (nsm K (DLogF.dlog J g s) K.one) π)) (K.neg K.one))
        = K.mul (K.mul (nsm K (DLogF.dlog J g t) K.one) (nsm K (DLogF.dlog J g s) K.one)) (K.mul π π) := by lib_ring
      _ = K.zero := by rw [hπ]; lib_ring
  · exact sum_u K π hπ J g hpr hg l h hl hN
  · rw [hN, nsm_mul_nat K l K.one (2 * h), hl, Teich.nsm_zero]

end PrimJC

#print axioms PrimJC.double_inj
#print axioms PrimJC.two_tri
#print axioms PrimJC.tri_val
#print axioms PrimJC.u_pow
#print axioms PrimJC.g_ne_zero
#print axioms PrimJC.sum_pows
#print axioms PrimJC.sum_u
#print axioms PrimJC.prim_conc

lang/PrimRoot.lean

lean_all/lang/PrimRoot.lean

/-
  lang/PrimRoot — T43, cycle 33: 𝔽_p^× is cyclic, and Artin reciprocity for every cyclotomic field ℚ(ζ_N).

  p = J + 2 prime, ℤ/p = ring (J + 1), orders as in lang/OrdMul (Ord x m: x^m = 1 and m divides every r with x^r = 1).
    extract          n ≥ 1, ℓ prime ⇒ n = ℓ^e n' with ℓ ∤ n'
    coprime_pow      ℓ ∤ n' (n' ≥ 1) ⇒ ℓ^(s+1) and n' are coprime
    ord_of_dvd       every n | p − 1 is the order of some element  (prime powers by OrdMul.ord_prime_pow, glued by
                     OrdMul.ord_mul_coprime, strong induction on n)
    primitive_root   some g has order p − 1: 𝔽_p^× is cyclic
    cyclo_N          for N ≥ 1: 𝔽_p has an element of order N  ⇔  N | p − 1
  So p splits completely in ℚ(ζ_N) (p ∤ N) exactly when p ≡ 1 (mod N): the Frobenius of p in
  Gal(ℚ(ζ_N)/ℚ) = (ℤ/N)^× is trivial iff p ≡ 1 — GL1 reciprocity (Kronecker–Weber side) for all cyclotomic fields.

  Lean 4 core only, proofs by hand (lib/Gcd from lib_shared), no axioms.
-/
import lang.OrdMul

namespace PrimRoot

open FTAChain ZMod TwoPower Order PrimeOneModQ PrimePowerOne OrdMul

theorem dvd_of_dv {d f : Nat} (h : Dv d f) : d ∣ f := dv_to_dvd h

theorem dv_of_dvd {d f : Nat} (h : d ∣ f) : Dv d f :=
  match h with
  | ⟨c, e⟩ => ⟨c, by rw [e, nmul_comm]⟩

/-- **ℓ-part.** -/
theorem extract (l : Nat) : ∀ F n, n ≤ F → 1 ≤ n →
    ∃ e n', n = (l + 1 + 1) ^ e * n' ∧ ¬ ((l + 1 + 1) ∣ n')
  | 0, n, hF, h1 => absurd (Nat.le_trans h1 hF) (Nat.not_succ_le_zero 0)
  | F + 1, n, hF, h1 =>
    match dv_dec (l + 1) n with
    | Or.inr hnd => ⟨0, n, by rw [Nat.pow_zero, Nat.one_mul], fun h => hnd (dv_of_dvd h)⟩
    | Or.inl ⟨q, hq⟩ => by
      -- n = q ℓ, 1 ≤ q < n
      have hq1 : 1 ≤ q := by
        cases q with
        | zero => rw [hq, Nat.zero_mul] at h1; exact absurd h1 (Nat.not_succ_le_zero 0)
        | succ q' => exact Nat.succ_le_succ (Nat.zero_le _)
      have hlt : q < n := by
        rw [hq]
        have a : q * 2 ≤ q * (l + 1 + 1) := Nat.mul_le_mul_left q (Nat.succ_le_succ (Nat.succ_le_succ (Nat.zero_le l)))
        have b : q < q * 2 := by
          rw [Nat.mul_two]
          exact Nat.lt_add_of_pos_right hq1
        exact Nat.lt_of_lt_of_le b a
      match extract l F q (Nat.le_of_lt_succ (Nat.lt_of_lt_of_le hlt hF)) hq1 with
      | ⟨e, n', he, hn'⟩ =>
        refine ⟨e + 1, n', ?_, hn'⟩
        rw [hq, he, Nat.pow_succ, nmul_assoc, nmul_assoc, nmul_comm n' (l + 1 + 1)]

theorem coprime_pow (l s n' : Nat) (hl : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1)
    (hn1 : 1 ≤ n') (hn : ¬ ((l + 1 + 1) ∣ n')) :
    ∀ d, d ∣ (l + 1 + 1) ^ (s + 1) → d ∣ n' → d = 1
  | 0, _, h2 => by
    match h2 with
    | ⟨c, e⟩ => rw [e, Nat.zero_mul] at hn1; exact absurd hn1 (Nat.not_succ_le_zero 0)
  | 1, _, _ => rfl
  | k + 2, h1, h2 => by
    exfalso
    match lp_exists k with
    | ⟨r, hr, hmin⟩ =>
      have hrp := lp_prime _ r hr hmin
      have hr1 : Dv (r + 1 + 1) ((l + 1 + 1) ^ (s + 1)) := dv_trans _ _ _ hr (dv_of_dvd h1)
      have := prime_dvd_pow r l hrp hl (s + 1) hr1
      apply hn
      rw [← this]
      exact LibGcd.dvd_trans' (dvd_of_dv hr) h2

variable (J : Nat)

theorem ord_one : Ord (ring (J + 1)) (ring (J + 1)).one 1 :=
  ⟨pwone (ring (J + 1)) 1, fun r _ => LibGcd.one_dvd' r⟩

/-- **Every divisor of p − 1 is an order.** -/
theorem ord_of_dvd (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) :
    ∀ F n, n ≤ F → n ∣ J + 1 → ∃ g : Zp (J + 1), Ord (ring (J + 1)) g n
  | _, 0, _, h => by
    match h with
    | ⟨c, e⟩ => exact absurd (e.trans (Nat.zero_mul c)) (fun e' => Nat.noConfusion e')
  | _, 1, _, _ => ⟨(ring (J + 1)).one, ord_one J⟩
  | 0, k + 2, hF, _ => absurd hF (Nat.not_succ_le_zero _)
  | F + 1, k + 2, hF, hd => by
    match lp_exists k with
    | ⟨l, hl, hmin⟩ =>
      have hlp := lp_prime _ l hl hmin
      match extract l (k + 2) (k + 2) (Nat.le_refl _) (Nat.succ_le_succ (Nat.zero_le _)) with
      | ⟨0, n', he, hn'⟩ =>
        exfalso; apply hn'
        rw [Nat.pow_zero, Nat.one_mul] at he
        rw [← he]; exact dvd_of_dv hl
      | ⟨s + 1, n', he, hn'⟩ =>
        have hn1 : 1 ≤ n' := by
          cases n' with
          | zero => rw [Nat.mul_zero] at he; exact absurd he (fun e => Nat.noConfusion e)
          | succ _ => exact Nat.succ_le_succ (Nat.zero_le _)
        have hP2 : 2 ≤ (l + 1 + 1) ^ (s + 1) := by
          rw [Nat.pow_succ]
          have hp1 : 1 ≤ (l + 1 + 1) ^ s := match pow_succ_form (l + 1) s with
            | ⟨r, hr⟩ => by rw [hr]; exact Nat.succ_le_succ (Nat.zero_le r)
          have := Nat.mul_le_mul hp1
            (Nat.succ_le_succ (Nat.succ_le_succ (Nat.zero_le l)))
          rw [Nat.one_mul] at this
          exact this
        have hlt : n' < k + 2 := by
          rw [he]
          have a : 2 * n' ≤ (l + 1 + 1) ^ (s + 1) * n' := Nat.mul_le_mul_right n' hP2
          have b : n' < 2 * n' := by
            rw [Nat.two_mul]; exact Nat.lt_add_of_pos_right hn1
          exact Nat.lt_of_lt_of_le b a
        have hPd : (l + 1 + 1) ^ (s + 1) ∣ J + 1 := LibGcd.dvd_trans' ⟨n', he⟩ hd
        have hnd : n' ∣ J + 1 :=
          LibGcd.dvd_trans' ⟨(l + 1 + 1) ^ (s + 1), by rw [he, Nat.mul_comm]⟩ hd
        match ord_prime_pow J hpr l s hlp (dv_of_dvd hPd),
          ord_of_dvd hpr F n' (Nat.le_of_lt_succ (Nat.lt_of_lt_of_le hlt hF)) hnd with
        | ⟨y, hy⟩, ⟨z, hz⟩ =>
          refine ⟨(ring (J + 1)).mul y z, ?_⟩
          rw [he]
          exact ord_mul_coprime (ring (J + 1)) y z _ _ hy hz (coprime_pow l s n' hlp hn1 hn')

/-- **𝔽_p^× is cyclic.** -/
theorem primitive_root (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) :
    ∃ g : Zp (J + 1), Ord (ring (J + 1)) g (J + 1) :=
  ord_of_dvd J hpr (J + 1) (J + 1) (Nat.le_refl _) (LibGcd.dvd_refl' _)

/-- **Reciprocity for ℚ(ζ_N)**: an element of order N exists iff N | p − 1. -/
theorem cyclo_N (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (N : Nat) :
    (∃ y : Zp (J + 1), Ord (ring (J + 1)) y (N + 1)) ↔ (N + 1) ∣ J + 1 :=
  ⟨fun ⟨y, hy⟩ => by
    have hy0 : y ≠ (ring (J + 1)).zero := fun e => by
      have h := hy.1
      rw [e] at h
      exact PowerSum.one_ne_zero' J (h.symm.trans (HasseCM.pw_zero_succ (ring (J + 1)) N))
    exact hy.2 _ (unit_pow J hpr y hy0),
   fun hd => ord_of_dvd J hpr (N + 1) (N + 1) (Nat.le_refl _) hd⟩

end PrimRoot

#print axioms PrimRoot.dvd_of_dv
#print axioms PrimRoot.dv_of_dvd
#print axioms PrimRoot.extract
#print axioms PrimRoot.coprime_pow
#print axioms PrimRoot.ord_one
#print axioms PrimRoot.ord_of_dvd
#print axioms PrimRoot.primitive_root
#print axioms PrimRoot.cyclo_N

lang/PrimaryUnique.lean

lean_all/lang/PrimaryUnique.lean

/-
  lang/PrimaryUnique — T43, cycle 149: the primary Gaussian prime above a split p is unique up to conjugation.

  π = a + bi and π' = c + di primary (b, d even; a + b ≡ c + d ≡ 1 mod 4) with N(π) = N(π') = p prime:
    four_bad          4k ≠ 2
    primary_unique    c = a and d = ±b                        (FermatUnique + the congruences mod 4)
    hecke_unique      ψ(π') + ψ(π̄') = ψ(π) + ψ(π̄) = 2a        (the Hecke value at p does not depend on the choice)
  So a_p = 2 Re π of the CM form of y² = x³ − x is a function of p alone.

  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.FermatUnique
import lang.GaussInt

namespace PrimaryUnique

open FTAChain LibGcd GaussInt FermatUnique

theorem four_bad (k : Int) (h : 4 * k = 2) : False :=
  two_ne_one k (CubicTrace.cancel_N 2 (fun e => Nat.noConfusion e) (2 * k) 1
    (show (2 : Int) * (2 * k) = 2 * 1 by rw [show (2 : Int) * (2 * k) = 4 * k by lib_int, h]; rfl))

theorem settle (a b c d dd : Int) (hdd : dd = d ∨ dd = -d) (e f s t : Int)
    (hb : b = 2 * e) (hab : a + b - 1 = 4 * t) (hd : d = 2 * f) (hcd : c + d - 1 = 4 * s)
    (h : Four a b c dd) : c = a ∧ (d = b ∨ d = -b) := by
  rcases h with ⟨h1, h2⟩ | ⟨h1, h2⟩ | ⟨h1, h2⟩ | ⟨h1, h2⟩
  · refine ⟨h1, ?_⟩
    cases hdd with
    | inl q => left; rw [← q, h2]
    | inr q => right; rw [← h2, q]; lib_int
  · exfalso
    have hd' : d = -b ∨ d = b := by
      cases hdd with
      | inl q => left; rw [← q, h2]
      | inr q => right; rw [show d = -(-d) by lib_int, ← q, h2]; lib_int
    cases hd' with
    | inl q =>
      apply four_bad (-(s + t))
      rw [show 4 * -(s + t) = -(4 * s) + -(4 * t) by lib_int, ← hcd, ← hab, h1, q]; lib_int
    | inr q =>
      apply four_bad (e - s - t)
      rw [show 4 * (e - s - t) = 2 * (2 * e) + -(4 * s) + -(4 * t) by lib_int, ← hb, ← hcd, ← hab, h1, q]; lib_int
  · exfalso
    apply two_ne_one (f - e - 2 * s)
    rw [show 2 * (f - e - 2 * s) = 2 * f + -(2 * e) + -(4 * s) by lib_int, ← hd, ← hb, ← hcd, h1]; lib_int
  · exfalso
    apply two_ne_one (e + f - 2 * s)
    rw [show 2 * (e + f - 2 * s) = 2 * e + 2 * f + -(4 * s) by lib_int, ← hd, ← hb, ← hcd, h1]; lib_int

/-- **The primary π above p is unique up to conjugation.** -/
theorem primary_unique (p : Nat) (hp : ∀ d, Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (a b c d : Int)
    (hP1 : Primary ⟨a, b⟩) (hP2 : Primary ⟨c, d⟩)
    (h1 : a * a + b * b = Int.ofNat (p + 1 + 1)) (h2 : c * c + d * d = Int.ofNat (p + 1 + 1)) :
    c = a ∧ (d = b ∨ d = -b) := by
  match hP1, hP2 with
  | ⟨⟨e, hb⟩, ⟨t, hab⟩⟩, ⟨⟨f, hd⟩, ⟨s, hcd⟩⟩ =>
    cases fermat_unique p hp a b c d h1 h2 with
    | inl h => exact settle a b c d d (Or.inl rfl) e f s t hb hab hd hcd h
    | inr h => exact settle a b c d (-d) (Or.inr rfl) e f s t hb hab hd hcd h

/-- **The Hecke value at p is well defined.** -/
theorem hecke_unique (p : Nat) (hp : ∀ d, Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (x y : GI)
    (hx : Primary x) (hy : Primary y) (h1 : gnorm x = Int.ofNat (p + 1 + 1)) (h2 : gnorm y = Int.ofNat (p + 1 + 1)) :
    y.re + (gconj y).re = x.re + (gconj x).re := by
  rw [hecke_trace, hecke_trace, (primary_unique p hp x.re x.im y.re y.im hx hy h1 h2).1]

end PrimaryUnique

#print axioms PrimaryUnique.four_bad
#print axioms PrimaryUnique.settle
#print axioms PrimaryUnique.primary_unique
#print axioms PrimaryUnique.hecke_unique

lang/Prime11.lean

lean_all/lang/Prime11.lean

/-
  lang/Prime11 — T48/T50, cycle 652: ℤ[(1+√−11)/2] above a split prime: π of prime norm is prime, N(α) = p^k ⇒ α = uπ^jπ̄^l, and the
  H11-elements of norm p^k are the π^j π̄^l (H11 conjugation-stable).  (Port of EisPrime3.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.HeckeMult11

namespace Prime11

open FTAChain DetChain LibInt Euc11 Prim11 Uniq11 Frobenius

variable (P : Nat)

theorem nrm_mul (x y : Int × Int) : nrm ((Ring11.ring11 LibInt.intR).mul x y) = nrm x * nrm y :=
  Int.ofNat.inj (by show Int.ofNat _ = Int.ofNat (nrm x) * Int.ofNat (nrm y); rw [nrm_cast, nrm_cast, nrm_cast, en_mul])

theorem unit_inv (u : Int × Int) (h : nrm u = 1) : (Ring11.ring11 LibInt.intR).mul u (cj u) = (Ring11.ring11 LibInt.intR).one := by
  rw [mul_cj, ← nrm_cast, h]; rfl

/-- **An element of prime norm is prime.** -/
theorem eprime_div (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (x y : Int × Int)
    (h : Dvd' π ((Ring11.ring11 LibInt.intR).mul x y)) : Dvd' π x ∨ Dvd' π y := by
  match egcd_exists _ π x (Nat.le_refl _) with
  | ⟨d, X, Y, hd, ⟨c1, hc1⟩, ⟨c2, hc2⟩⟩ =>
    have hdc : nrm d * nrm c1 = P + 1 + 1 := by rw [← nrm_mul, ← hc1, hπ]
    cases hp (nrm d) ⟨nrm c1, by rw [← hdc]; lib_nat⟩ with
    | inl h1 =>
      refine Or.inr (eeuclid π x y ((Ring11.ring11 LibInt.intR).mul (cj d) X) ((Ring11.ring11 LibInt.intR).mul (cj d) Y) ?_ h)
      rw [show (Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul (cj d) X) π) ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul (cj d) Y) x)
          = (Ring11.ring11 LibInt.intR).mul (cj d) ((Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul X π) ((Ring11.ring11 LibInt.intR).mul Y x)) by lib_ring,
        ← hd, (Ring11.ring11 LibInt.intR).mul_comm, unit_inv d h1]
    | inr h1 =>
      have nc : nrm c1 = 1 := by
        have e : (P + 1 + 1) * nrm c1 = (P + 1 + 1) * 1 := by rw [← h1, hdc, h1]; lib_nat
        exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) e
      refine Or.inl ⟨(Ring11.ring11 LibInt.intR).mul (cj c1) c2, ?_⟩
      have hd' : d = (Ring11.ring11 LibInt.intR).mul π (cj c1) := by
        rw [hc1]
        calc d = (Ring11.ring11 LibInt.intR).mul d (Ring11.ring11 LibInt.intR).one := by lib_ring
          _ = _ := by rw [← unit_inv c1 nc]; lib_ring
      rw [hc2, hd']; lib_ring

theorem cj_mul (x y : Int × Int) : cj ((Ring11.ring11 LibInt.intR).mul x y) = (Ring11.ring11 LibInt.intR).mul (cj x) (cj y) := by
  apply Prod.ext
  · show x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2) + (x.1 * y.2 + x.2 * y.1 + x.2 * y.2) = (x.1 + x.2) * (y.1 + y.2) + -(-x.2 * -y.2 + -x.2 * -y.2 + -x.2 * -y.2)
    lib_int
  · show -(x.1 * y.2 + x.2 * y.1 + x.2 * y.2) = (x.1 + x.2) * -y.2 + -x.2 * (y.1 + y.2) + -x.2 * -y.2
    lib_int

theorem cj_cj (z : Int × Int) : cj (cj z) = z := by
  apply Prod.ext
  · show z.1 + z.2 + -z.2 = z.1; lib_int
  · show - -z.2 = z.2; lib_int

theorem nrm_cj (z : Int × Int) : nrm (cj z) = nrm z := by unfold nrm; rw [en_cj]

theorem nrm_one_eq (z : Int × Int) (h : nrm z = 1) : en z = 1 := by rw [← nrm_cast, h]; rfl

/-- **Factorisation above a split prime.** -/
theorem factor3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) : ∀ k (α : Int × Int),
    nrm α = (P + 1 + 1) ^ k → ∃ u j l, nrm u = 1 ∧ j + l = k ∧
      α = (Ring11.ring11 LibInt.intR).mul u ((Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) π j) (pw (Ring11.ring11 LibInt.intR) (cj π) l))
  | 0, α, h => ⟨α, 0, 0, h, rfl, by show α = (Ring11.ring11 LibInt.intR).mul α ((Ring11.ring11 LibInt.intR).mul (Ring11.ring11 LibInt.intR).one (Ring11.ring11 LibInt.intR).one); lib_ring⟩
  | k + 1, α, h => by
    -- π | α ᾱ
    have hdiv : Dvd' π ((Ring11.ring11 LibInt.intR).mul α (cj α)) := by
      refine ⟨(Ring11.ring11 LibInt.intR).mul (cj π) (Int.ofNat ((P + 1 + 1) ^ k), 0), ?_⟩
      rw [mul_cj, ← nrm_cast, h]
      rw [show (Ring11.ring11 LibInt.intR).mul π ((Ring11.ring11 LibInt.intR).mul (cj π) (Int.ofNat ((P + 1 + 1) ^ k), 0)) =
        (Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul π (cj π)) (Int.ofNat ((P + 1 + 1) ^ k), 0) by lib_ring, mul_cj, ← nrm_cast, hπ]
      apply Prod.ext
      · show Int.ofNat ((P + 1 + 1) ^ k * (P + 1 + 1)) = Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ k) + -(0 * 0)
        show Int.ofNat ((P + 1 + 1) ^ k) * Int.ofNat (P + 1 + 1) = _; lib_int
      · show (0 : Int) = Int.ofNat (P + 1 + 1) * 0 + 0 * Int.ofNat ((P + 1 + 1) ^ k) + -(0 * 0); lib_int
    have cancel : ∀ β : Int × Int, nrm ((Ring11.ring11 LibInt.intR).mul π β) = (P + 1 + 1) ^ (k + 1) → nrm β = (P + 1 + 1) ^ k := fun β hb => by
      rw [nrm_mul, hπ] at hb
      exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) (hb.trans (Nat.mul_comm ((P + 1 + 1) ^ k) (P + 1 + 1)))
    cases eprime_div P hp π hπ α (cj α) hdiv with
    | inl hα =>
      obtain ⟨β, hβ⟩ := hα
      obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 hp π hπ k β (cancel β (by rw [← hβ]; exact h))
      refine ⟨u, j + 1, l, hu, by rw [← hjl]; lib_nat, ?_⟩
      rw [hβ, he]; show _ = (Ring11.ring11 LibInt.intR).mul u ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) π j) π) _); lib_ring
    | inr hα =>
      obtain ⟨β, hβ⟩ := hα
      have hα' : α = (Ring11.ring11 LibInt.intR).mul (cj π) (cj β) := by rw [← cj_mul, ← hβ, cj_cj]
      have hb : nrm (cj β) = (P + 1 + 1) ^ k := by
        rw [nrm_cj]; apply cancel
        rw [← hβ, nrm_cj]; exact h
      obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 hp π hπ k (cj β) hb
      refine ⟨u, j, l + 1, hu, by rw [← hjl]; lib_nat, ?_⟩
      rw [hα', he]; show _ = (Ring11.ring11 LibInt.intR).mul u ((Ring11.ring11 LibInt.intR).mul _ ((Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) (cj π) l) (cj π))); lib_ring

theorem prim11_one : Prim11 (Ring11.ring11 LibInt.intR).one := ⟨0, Or.inl rfl⟩

theorem prim11_pow (z : Int × Int) (h : Prim11 z) : ∀ j, Prim11 (pw (Ring11.ring11 LibInt.intR) z j)
  | 0 => prim11_one
  | j + 1 => prim11_mul _ _ (prim11_pow z h j) h

/-- **The primary elements of norm p^k are the π^j π̄^l.** -/
theorem primary_only3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim11 π)
    (k : Nat) (α : Int × Int) (hα : nrm α = (P + 1 + 1) ^ k) (pα : Prim11 α) :
    ∃ j l, j + l = k ∧ α = (Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) π j) (pw (Ring11.ring11 LibInt.intR) (cj π) l) := by
  obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 P hp π hπ k α hα
  have pw' := prim11_mul _ _ (prim11_pow π pπ j) (prim11_pow (cj π) (prim11_cj π pπ) l)
  have u1 := unit_one11 u _ (nrm_one_eq u hu) pw' (by rw [← he]; exact pα)
  exact ⟨j, l, hjl, by rw [he, u1]; lib_ring⟩

end Prime11

#print axioms Prime11.nrm_mul
#print axioms Prime11.unit_inv
#print axioms Prime11.eprime_div
#print axioms Prime11.cj_mul
#print axioms Prime11.cj_cj
#print axioms Prime11.nrm_cj
#print axioms Prime11.nrm_one_eq
#print axioms Prime11.factor3
#print axioms Prime11.prim11_one
#print axioms Prime11.prim11_pow
#print axioms Prime11.primary_only3

lang/Prime163.lean

lean_all/lang/Prime163.lean

/-
  lang/Prime163 — T48/T50, T48: ℤ[(1+√−163)/2] above a split prime: π of prime norm is prime, N(α) = p^k ⇒ α = uπ^jπ̄^l, and the
  H163-elements of norm p^k are the π^j π̄^l (H163 conjugation-stable).  (Port of EisPrime3.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.HeckeMult163

namespace Prime163

open FTAChain DetChain LibInt Euc163 Prim163 Uniq163 Frobenius

variable (P : Nat)

theorem nrm_mul (x y : Int × Int) : nrm ((Ring163.ring163 LibInt.intR).mul x y) = nrm x * nrm y :=
  Int.ofNat.inj (by show Int.ofNat _ = Int.ofNat (nrm x) * Int.ofNat (nrm y); rw [nrm_cast, nrm_cast, nrm_cast, en_mul])

theorem unit_inv (u : Int × Int) (h : nrm u = 1) : (Ring163.ring163 LibInt.intR).mul u (cj u) = (Ring163.ring163 LibInt.intR).one := by
  rw [mul_cj, ← nrm_cast, h]; rfl

/-- **An element of prime norm is prime.** -/
theorem eprime_div (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (x y : Int × Int)
    (h : Dvd' π ((Ring163.ring163 LibInt.intR).mul x y)) : Dvd' π x ∨ Dvd' π y := by
  match egcd_exists π x with
  | ⟨d, X, Y, hd, ⟨c1, hc1⟩, ⟨c2, hc2⟩⟩ =>
    have hdc : nrm d * nrm c1 = P + 1 + 1 := by rw [← nrm_mul, ← hc1, hπ]
    cases hp (nrm d) ⟨nrm c1, by rw [← hdc]; lib_nat⟩ with
    | inl h1 =>
      refine Or.inr (eeuclid π x y ((Ring163.ring163 LibInt.intR).mul (cj d) X) ((Ring163.ring163 LibInt.intR).mul (cj d) Y) ?_ h)
      rw [show (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul (cj d) X) π) ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul (cj d) Y) x)
          = (Ring163.ring163 LibInt.intR).mul (cj d) ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul X π) ((Ring163.ring163 LibInt.intR).mul Y x)) by lib_ring,
        ← hd, (Ring163.ring163 LibInt.intR).mul_comm, unit_inv d h1]
    | inr h1 =>
      have nc : nrm c1 = 1 := by
        have e : (P + 1 + 1) * nrm c1 = (P + 1 + 1) * 1 := by rw [← h1, hdc, h1]; lib_nat
        exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) e
      refine Or.inl ⟨(Ring163.ring163 LibInt.intR).mul (cj c1) c2, ?_⟩
      have hd' : d = (Ring163.ring163 LibInt.intR).mul π (cj c1) := by
        rw [hc1]
        calc d = (Ring163.ring163 LibInt.intR).mul d (Ring163.ring163 LibInt.intR).one := by lib_ring
          _ = _ := by rw [← unit_inv c1 nc]; lib_ring
      rw [hc2, hd']; lib_ring

theorem cj_mul (x y : Int × Int) : cj ((Ring163.ring163 LibInt.intR).mul x y) = (Ring163.ring163 LibInt.intR).mul (cj x) (cj y) := by
  apply Prod.ext
  · show x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 *  -- (+756 chars; full line in the Zenodo source)
    lib_int
  · show -(x.1 * y.2 + x.2 * y.1 + x.2 * y.2) = (x.1 + x.2) * -y.2 + -x.2 * (y.1 + y.2) + -x.2 * -y.2
    lib_int

theorem cj_cj (z : Int × Int) : cj (cj z) = z := by
  apply Prod.ext
  · show z.1 + z.2 + -z.2 = z.1; lib_int
  · show - -z.2 = z.2; lib_int

theorem nrm_cj (z : Int × Int) : nrm (cj z) = nrm z := by unfold nrm; rw [en_cj]

theorem nrm_one_eq (z : Int × Int) (h : nrm z = 1) : en z = 1 := by rw [← nrm_cast, h]; rfl

/-- **Factorisation above a split prime.** -/
theorem factor3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) : ∀ k (α : Int × Int),
    nrm α = (P + 1 + 1) ^ k → ∃ u j l, nrm u = 1 ∧ j + l = k ∧
      α = (Ring163.ring163 LibInt.intR).mul u ((Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) π j) (pw (Ring163.ring163 LibInt.intR) (cj π) l))
  | 0, α, h => ⟨α, 0, 0, h, rfl, by show α = (Ring163.ring163 LibInt.intR).mul α ((Ring163.ring163 LibInt.intR).mul (Ring163.ring163 LibInt.intR).one (Ring163.ring163 LibInt.intR).one); lib_ring⟩
  | k + 1, α, h => by
    -- π | α ᾱ
    have hdiv : Dvd' π ((Ring163.ring163 LibInt.intR).mul α (cj α)) := by
      refine ⟨(Ring163.ring163 LibInt.intR).mul (cj π) (Int.ofNat ((P + 1 + 1) ^ k), 0), ?_⟩
      rw [mul_cj, ← nrm_cast, h]
      rw [show (Ring163.ring163 LibInt.intR).mul π ((Ring163.ring163 LibInt.intR).mul (cj π) (Int.ofNat ((P + 1 + 1) ^ k), 0)) =
        (Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul π (cj π)) (Int.ofNat ((P + 1 + 1) ^ k), 0) by lib_ring, mul_cj, ← nrm_cast, hπ]
      apply Prod.ext
      · show Int.ofNat ((P + 1 + 1) ^ k * (P + 1 + 1)) = Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ k) + -(0 * 0)
        show Int.ofNat ((P + 1 + 1) ^ k) * Int.ofNat (P + 1 + 1) = _; lib_int
      · show (0 : Int) = Int.ofNat (P + 1 + 1) * 0 + 0 * Int.ofNat ((P + 1 + 1) ^ k) + -(0 * 0); lib_int
    have cancel : ∀ β : Int × Int, nrm ((Ring163.ring163 LibInt.intR).mul π β) = (P + 1 + 1) ^ (k + 1) → nrm β = (P + 1 + 1) ^ k := fun β hb => by
      rw [nrm_mul, hπ] at hb
      exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) (hb.trans (Nat.mul_comm ((P + 1 + 1) ^ k) (P + 1 + 1)))
    cases eprime_div P hp π hπ α (cj α) hdiv with
    | inl hα =>
      obtain ⟨β, hβ⟩ := hα
      obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 hp π hπ k β (cancel β (by rw [← hβ]; exact h))
      refine ⟨u, j + 1, l, hu, by rw [← hjl]; lib_nat, ?_⟩
      rw [hβ, he]; show _ = (Ring163.ring163 LibInt.intR).mul u ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) π j) π) _); lib_ring
    | inr hα =>
      obtain ⟨β, hβ⟩ := hα
      have hα' : α = (Ring163.ring163 LibInt.intR).mul (cj π) (cj β) := by rw [← cj_mul, ← hβ, cj_cj]
      have hb : nrm (cj β) = (P + 1 + 1) ^ k := by
        rw [nrm_cj]; apply cancel
        rw [← hβ, nrm_cj]; exact h
      obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 hp π hπ k (cj β) hb
      refine ⟨u, j, l + 1, hu, by rw [← hjl]; lib_nat, ?_⟩
      rw [hα', he]; show _ = (Ring163.ring163 LibInt.intR).mul u ((Ring163.ring163 LibInt.intR).mul _ ((Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) (cj π) l) (cj π))); lib_ring

theorem prim163_one : Prim163 (Ring163.ring163 LibInt.intR).one := by
  show QChar.chi 80 (Prim163.cr (1, 0)) = 1
  rw [show Prim163.cr (1, 0) = (ZMod.ring (2 * 80 + 1 + 1)).mul (ZMod.ring (2 * 80 + 1 + 1)).one (ZMod.ring (2 * 80 + 1 + 1)).one from rfl]
  exact QChar.chi_sq 80 Prim163.prime163 _ (PowerSum.one_ne_zero' (2 * 80 + 1))

theorem prim163_pow (z : Int × Int) (h : Prim163 z) : ∀ j, Prim163 (pw (Ring163.ring163 LibInt.intR) z j)
  | 0 => prim163_one
  | j + 1 => prim_mul _ _ (prim163_pow z h j) h

/-- **The primary elements of norm p^k are the π^j π̄^l.** -/
theorem primary_only3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim163 π)
    (k : Nat) (α : Int × Int) (hα : nrm α = (P + 1 + 1) ^ k) (pα : Prim163 α) :
    ∃ j l, j + l = k ∧ α = (Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) π j) (pw (Ring163.ring163 LibInt.intR) (cj π) l) := by
  obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 P hp π hπ k α hα
  have pw' := prim_mul _ _ (prim163_pow π pπ j) (prim163_pow (cj π) (prim_cj π pπ) l)
  have u1 := unit_one u _ (nrm_one_eq u hu) pw' (by rw [← he]; exact pα)
  exact ⟨j, l, hjl, by rw [he, u1]; lib_ring⟩

end Prime163

#print axioms Prime163.nrm_mul
#print axioms Prime163.unit_inv
#print axioms Prime163.eprime_div
#print axioms Prime163.cj_mul
#print axioms Prime163.cj_cj
#print axioms Prime163.nrm_cj
#print axioms Prime163.nrm_one_eq
#print axioms Prime163.factor3
#print axioms Prime163.prim163_one
#print axioms Prime163.prim163_pow
#print axioms Prime163.primary_only3

lang/Prime19.lean

lean_all/lang/Prime19.lean

/-
  lang/Prime19 — T48/T50, cycle 663: ℤ[(1+√−19)/2] above a split prime: π of prime norm is prime, N(α) = p^k ⇒ α = uπ^jπ̄^l, and the
  H19-elements of norm p^k are the π^j π̄^l (H19 conjugation-stable).  (Port of EisPrime3.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.HeckeMult19

namespace Prime19

open FTAChain DetChain LibInt Euc19 Prim19 Uniq19 Frobenius

variable (P : Nat)

theorem nrm_mul (x y : Int × Int) : nrm ((Ring19.ring19 LibInt.intR).mul x y) = nrm x * nrm y :=
  Int.ofNat.inj (by show Int.ofNat _ = Int.ofNat (nrm x) * Int.ofNat (nrm y); rw [nrm_cast, nrm_cast, nrm_cast, en_mul])

theorem unit_inv (u : Int × Int) (h : nrm u = 1) : (Ring19.ring19 LibInt.intR).mul u (cj u) = (Ring19.ring19 LibInt.intR).one := by
  rw [mul_cj, ← nrm_cast, h]; rfl

/-- **An element of prime norm is prime.** -/
theorem eprime_div (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (x y : Int × Int)
    (h : Dvd' π ((Ring19.ring19 LibInt.intR).mul x y)) : Dvd' π x ∨ Dvd' π y := by
  match egcd_exists π x with
  | ⟨d, X, Y, hd, ⟨c1, hc1⟩, ⟨c2, hc2⟩⟩ =>
    have hdc : nrm d * nrm c1 = P + 1 + 1 := by rw [← nrm_mul, ← hc1, hπ]
    cases hp (nrm d) ⟨nrm c1, by rw [← hdc]; lib_nat⟩ with
    | inl h1 =>
      refine Or.inr (eeuclid π x y ((Ring19.ring19 LibInt.intR).mul (cj d) X) ((Ring19.ring19 LibInt.intR).mul (cj d) Y) ?_ h)
      rw [show (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul (cj d) X) π) ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul (cj d) Y) x)
          = (Ring19.ring19 LibInt.intR).mul (cj d) ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul X π) ((Ring19.ring19 LibInt.intR).mul Y x)) by lib_ring,
        ← hd, (Ring19.ring19 LibInt.intR).mul_comm, unit_inv d h1]
    | inr h1 =>
      have nc : nrm c1 = 1 := by
        have e : (P + 1 + 1) * nrm c1 = (P + 1 + 1) * 1 := by rw [← h1, hdc, h1]; lib_nat
        exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) e
      refine Or.inl ⟨(Ring19.ring19 LibInt.intR).mul (cj c1) c2, ?_⟩
      have hd' : d = (Ring19.ring19 LibInt.intR).mul π (cj c1) := by
        rw [hc1]
        calc d = (Ring19.ring19 LibInt.intR).mul d (Ring19.ring19 LibInt.intR).one := by lib_ring
          _ = _ := by rw [← unit_inv c1 nc]; lib_ring
      rw [hc2, hd']; lib_ring

theorem cj_mul (x y : Int × Int) : cj ((Ring19.ring19 LibInt.intR).mul x y) = (Ring19.ring19 LibInt.intR).mul (cj x) (cj y) := by
  apply Prod.ext
  · show x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2) + (x.1 * y.2 + x.2 * y.1 + x.2 * y.2) = (x.1 + x.2) * (y.1 + y.2) + -(-x.2 * -y.2 + -x.2 * -y.2 + -x.2 * -y.2 + -x.2 * -y.2 + -x.2 * -y.2)
    lib_int
  · show -(x.1 * y.2 + x.2 * y.1 + x.2 * y.2) = (x.1 + x.2) * -y.2 + -x.2 * (y.1 + y.2) + -x.2 * -y.2
    lib_int

theorem cj_cj (z : Int × Int) : cj (cj z) = z := by
  apply Prod.ext
  · show z.1 + z.2 + -z.2 = z.1; lib_int
  · show - -z.2 = z.2; lib_int

theorem nrm_cj (z : Int × Int) : nrm (cj z) = nrm z := by unfold nrm; rw [en_cj]

theorem nrm_one_eq (z : Int × Int) (h : nrm z = 1) : en z = 1 := by rw [← nrm_cast, h]; rfl

/-- **Factorisation above a split prime.** -/
theorem factor3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) : ∀ k (α : Int × Int),
    nrm α = (P + 1 + 1) ^ k → ∃ u j l, nrm u = 1 ∧ j + l = k ∧
      α = (Ring19.ring19 LibInt.intR).mul u ((Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) π j) (pw (Ring19.ring19 LibInt.intR) (cj π) l))
  | 0, α, h => ⟨α, 0, 0, h, rfl, by show α = (Ring19.ring19 LibInt.intR).mul α ((Ring19.ring19 LibInt.intR).mul (Ring19.ring19 LibInt.intR).one (Ring19.ring19 LibInt.intR).one); lib_ring⟩
  | k + 1, α, h => by
    -- π | α ᾱ
    have hdiv : Dvd' π ((Ring19.ring19 LibInt.intR).mul α (cj α)) := by
      refine ⟨(Ring19.ring19 LibInt.intR).mul (cj π) (Int.ofNat ((P + 1 + 1) ^ k), 0), ?_⟩
      rw [mul_cj, ← nrm_cast, h]
      rw [show (Ring19.ring19 LibInt.intR).mul π ((Ring19.ring19 LibInt.intR).mul (cj π) (Int.ofNat ((P + 1 + 1) ^ k), 0)) =
        (Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul π (cj π)) (Int.ofNat ((P + 1 + 1) ^ k), 0) by lib_ring, mul_cj, ← nrm_cast, hπ]
      apply Prod.ext
      · show Int.ofNat ((P + 1 + 1) ^ k * (P + 1 + 1)) = Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ k) + -(0 * 0)
        show Int.ofNat ((P + 1 + 1) ^ k) * Int.ofNat (P + 1 + 1) = _; lib_int
      · show (0 : Int) = Int.ofNat (P + 1 + 1) * 0 + 0 * Int.ofNat ((P + 1 + 1) ^ k) + -(0 * 0); lib_int
    have cancel : ∀ β : Int × Int, nrm ((Ring19.ring19 LibInt.intR).mul π β) = (P + 1 + 1) ^ (k + 1) → nrm β = (P + 1 + 1) ^ k := fun β hb => by
      rw [nrm_mul, hπ] at hb
      exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) (hb.trans (Nat.mul_comm ((P + 1 + 1) ^ k) (P + 1 + 1)))
    cases eprime_div P hp π hπ α (cj α) hdiv with
    | inl hα =>
      obtain ⟨β, hβ⟩ := hα
      obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 hp π hπ k β (cancel β (by rw [← hβ]; exact h))
      refine ⟨u, j + 1, l, hu, by rw [← hjl]; lib_nat, ?_⟩
      rw [hβ, he]; show _ = (Ring19.ring19 LibInt.intR).mul u ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) π j) π) _); lib_ring
    | inr hα =>
      obtain ⟨β, hβ⟩ := hα
      have hα' : α = (Ring19.ring19 LibInt.intR).mul (cj π) (cj β) := by rw [← cj_mul, ← hβ, cj_cj]
      have hb : nrm (cj β) = (P + 1 + 1) ^ k := by
        rw [nrm_cj]; apply cancel
        rw [← hβ, nrm_cj]; exact h
      obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 hp π hπ k (cj β) hb
      refine ⟨u, j, l + 1, hu, by rw [← hjl]; lib_nat, ?_⟩
      rw [hα', he]; show _ = (Ring19.ring19 LibInt.intR).mul u ((Ring19.ring19 LibInt.intR).mul _ ((Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) (cj π) l) (cj π))); lib_ring

theorem prim19_one : Prim19 (Ring19.ring19 LibInt.intR).one := ⟨0, Or.inl rfl⟩

theorem prim19_pow (z : Int × Int) (h : Prim19 z) : ∀ j, Prim19 (pw (Ring19.ring19 LibInt.intR) z j)
  | 0 => prim19_one
  | j + 1 => prim_mul _ _ (prim19_pow z h j) h

/-- **The primary elements of norm p^k are the π^j π̄^l.** -/
theorem primary_only3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim19 π)
    (k : Nat) (α : Int × Int) (hα : nrm α = (P + 1 + 1) ^ k) (pα : Prim19 α) :
    ∃ j l, j + l = k ∧ α = (Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) π j) (pw (Ring19.ring19 LibInt.intR) (cj π) l) := by
  obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 P hp π hπ k α hα
  have pw' := prim_mul _ _ (prim19_pow π pπ j) (prim19_pow (cj π) (prim_cj π pπ) l)
  have u1 := unit_one u _ (nrm_one_eq u hu) pw' (by rw [← he]; exact pα)
  exact ⟨j, l, hjl, by rw [he, u1]; lib_ring⟩

end Prime19

#print axioms Prime19.nrm_mul
#print axioms Prime19.unit_inv
#print axioms Prime19.eprime_div
#print axioms Prime19.cj_mul
#print axioms Prime19.cj_cj
#print axioms Prime19.nrm_cj
#print axioms Prime19.nrm_one_eq
#print axioms Prime19.factor3
#print axioms Prime19.prim19_one
#print axioms Prime19.prim19_pow
#print axioms Prime19.primary_only3

lang/Prime43.lean

lean_all/lang/Prime43.lean

/-
  lang/Prime43 — T48/T50, T48: ℤ[(1+√−43)/2] above a split prime: π of prime norm is prime, N(α) = p^k ⇒ α = uπ^jπ̄^l, and the
  H43-elements of norm p^k are the π^j π̄^l (H43 conjugation-stable).  (Port of EisPrime3.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.HeckeMult43

namespace Prime43

open FTAChain DetChain LibInt Euc43 Prim43 Uniq43 Frobenius

variable (P : Nat)

theorem nrm_mul (x y : Int × Int) : nrm ((Ring43.ring43 LibInt.intR).mul x y) = nrm x * nrm y :=
  Int.ofNat.inj (by show Int.ofNat _ = Int.ofNat (nrm x) * Int.ofNat (nrm y); rw [nrm_cast, nrm_cast, nrm_cast, en_mul])

theorem unit_inv (u : Int × Int) (h : nrm u = 1) : (Ring43.ring43 LibInt.intR).mul u (cj u) = (Ring43.ring43 LibInt.intR).one := by
  rw [mul_cj, ← nrm_cast, h]; rfl

/-- **An element of prime norm is prime.** -/
theorem eprime_div (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (x y : Int × Int)
    (h : Dvd' π ((Ring43.ring43 LibInt.intR).mul x y)) : Dvd' π x ∨ Dvd' π y := by
  match egcd_exists π x with
  | ⟨d, X, Y, hd, ⟨c1, hc1⟩, ⟨c2, hc2⟩⟩ =>
    have hdc : nrm d * nrm c1 = P + 1 + 1 := by rw [← nrm_mul, ← hc1, hπ]
    cases hp (nrm d) ⟨nrm c1, by rw [← hdc]; lib_nat⟩ with
    | inl h1 =>
      refine Or.inr (eeuclid π x y ((Ring43.ring43 LibInt.intR).mul (cj d) X) ((Ring43.ring43 LibInt.intR).mul (cj d) Y) ?_ h)
      rw [show (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul (cj d) X) π) ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul (cj d) Y) x)
          = (Ring43.ring43 LibInt.intR).mul (cj d) ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul X π) ((Ring43.ring43 LibInt.intR).mul Y x)) by lib_ring,
        ← hd, (Ring43.ring43 LibInt.intR).mul_comm, unit_inv d h1]
    | inr h1 =>
      have nc : nrm c1 = 1 := by
        have e : (P + 1 + 1) * nrm c1 = (P + 1 + 1) * 1 := by rw [← h1, hdc, h1]; lib_nat
        exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) e
      refine Or.inl ⟨(Ring43.ring43 LibInt.intR).mul (cj c1) c2, ?_⟩
      have hd' : d = (Ring43.ring43 LibInt.intR).mul π (cj c1) := by
        rw [hc1]
        calc d = (Ring43.ring43 LibInt.intR).mul d (Ring43.ring43 LibInt.intR).one := by lib_ring
          _ = _ := by rw [← unit_inv c1 nc]; lib_ring
      rw [hc2, hd']; lib_ring

theorem cj_mul (x y : Int × Int) : cj ((Ring43.ring43 LibInt.intR).mul x y) = (Ring43.ring43 LibInt.intR).mul (cj x) (cj y) := by
  apply Prod.ext
  · show x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2) + (x.1 * y.2 + x.2 * y.1 + x.2 * y.2) = (x.1 + x.2) * (y.1 + y.2) + -(-x.2 * -y.2 + -x.2 * -y.2 + -x.2 * -y.2 + -x.2 * -y.2 + -x.2 * -y.2 + -x.2 * -y.2 + -x.2 * -y.2 + -x.2 * -y.2 + -x.2 * -y.2 + -x.2 * -y.2 + -x.2 * -y.2)
    lib_int
  · show -(x.1 * y.2 + x.2 * y.1 + x.2 * y.2) = (x.1 + x.2) * -y.2 + -x.2 * (y.1 + y.2) + -x.2 * -y.2
    lib_int

theorem cj_cj (z : Int × Int) : cj (cj z) = z := by
  apply Prod.ext
  · show z.1 + z.2 + -z.2 = z.1; lib_int
  · show - -z.2 = z.2; lib_int

theorem nrm_cj (z : Int × Int) : nrm (cj z) = nrm z := by unfold nrm; rw [en_cj]

theorem nrm_one_eq (z : Int × Int) (h : nrm z = 1) : en z = 1 := by rw [← nrm_cast, h]; rfl

/-- **Factorisation above a split prime.** -/
theorem factor3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) : ∀ k (α : Int × Int),
    nrm α = (P + 1 + 1) ^ k → ∃ u j l, nrm u = 1 ∧ j + l = k ∧
      α = (Ring43.ring43 LibInt.intR).mul u ((Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) π j) (pw (Ring43.ring43 LibInt.intR) (cj π) l))
  | 0, α, h => ⟨α, 0, 0, h, rfl, by show α = (Ring43.ring43 LibInt.intR).mul α ((Ring43.ring43 LibInt.intR).mul (Ring43.ring43 LibInt.intR).one (Ring43.ring43 LibInt.intR).one); lib_ring⟩
  | k + 1, α, h => by
    -- π | α ᾱ
    have hdiv : Dvd' π ((Ring43.ring43 LibInt.intR).mul α (cj α)) := by
      refine ⟨(Ring43.ring43 LibInt.intR).mul (cj π) (Int.ofNat ((P + 1 + 1) ^ k), 0), ?_⟩
      rw [mul_cj, ← nrm_cast, h]
      rw [show (Ring43.ring43 LibInt.intR).mul π ((Ring43.ring43 LibInt.intR).mul (cj π) (Int.ofNat ((P + 1 + 1) ^ k), 0)) =
        (Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul π (cj π)) (Int.ofNat ((P + 1 + 1) ^ k), 0) by lib_ring, mul_cj, ← nrm_cast, hπ]
      apply Prod.ext
      · show Int.ofNat ((P + 1 + 1) ^ k * (P + 1 + 1)) = Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ k) + -(0 * 0)
        show Int.ofNat ((P + 1 + 1) ^ k) * Int.ofNat (P + 1 + 1) = _; lib_int
      · show (0 : Int) = Int.ofNat (P + 1 + 1) * 0 + 0 * Int.ofNat ((P + 1 + 1) ^ k) + -(0 * 0); lib_int
    have cancel : ∀ β : Int × Int, nrm ((Ring43.ring43 LibInt.intR).mul π β) = (P + 1 + 1) ^ (k + 1) → nrm β = (P + 1 + 1) ^ k := fun β hb => by
      rw [nrm_mul, hπ] at hb
      exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) (hb.trans (Nat.mul_comm ((P + 1 + 1) ^ k) (P + 1 + 1)))
    cases eprime_div P hp π hπ α (cj α) hdiv with
    | inl hα =>
      obtain ⟨β, hβ⟩ := hα
      obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 hp π hπ k β (cancel β (by rw [← hβ]; exact h))
      refine ⟨u, j + 1, l, hu, by rw [← hjl]; lib_nat, ?_⟩
      rw [hβ, he]; show _ = (Ring43.ring43 LibInt.intR).mul u ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) π j) π) _); lib_ring
    | inr hα =>
      obtain ⟨β, hβ⟩ := hα
      have hα' : α = (Ring43.ring43 LibInt.intR).mul (cj π) (cj β) := by rw [← cj_mul, ← hβ, cj_cj]
      have hb : nrm (cj β) = (P + 1 + 1) ^ k := by
        rw [nrm_cj]; apply cancel
        rw [← hβ, nrm_cj]; exact h
      obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 hp π hπ k (cj β) hb
      refine ⟨u, j, l + 1, hu, by rw [← hjl]; lib_nat, ?_⟩
      rw [hα', he]; show _ = (Ring43.ring43 LibInt.intR).mul u ((Ring43.ring43 LibInt.intR).mul _ ((Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) (cj π) l) (cj π))); lib_ring

theorem prim43_one : Prim43 (Ring43.ring43 LibInt.intR).one := rfl

theorem prim43_pow (z : Int × Int) (h : Prim43 z) : ∀ j, Prim43 (pw (Ring43.ring43 LibInt.intR) z j)
  | 0 => prim43_one
  | j + 1 => prim_mul _ _ (prim43_pow z h j) h

/-- **The primary elements of norm p^k are the π^j π̄^l.** -/
theorem primary_only3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim43 π)
    (k : Nat) (α : Int × Int) (hα : nrm α = (P + 1 + 1) ^ k) (pα : Prim43 α) :
    ∃ j l, j + l = k ∧ α = (Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) π j) (pw (Ring43.ring43 LibInt.intR) (cj π) l) := by
  obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 P hp π hπ k α hα
  have pw' := prim_mul _ _ (prim43_pow π pπ j) (prim43_pow (cj π) (prim_cj π pπ) l)
  have u1 := unit_one u _ (nrm_one_eq u hu) pw' (by rw [← he]; exact pα)
  exact ⟨j, l, hjl, by rw [he, u1]; lib_ring⟩

end Prime43

#print axioms Prime43.nrm_mul
#print axioms Prime43.unit_inv
#print axioms Prime43.eprime_div
#print axioms Prime43.cj_mul
#print axioms Prime43.cj_cj
#print axioms Prime43.nrm_cj
#print axioms Prime43.nrm_one_eq
#print axioms Prime43.factor3
#print axioms Prime43.prim43_one
#print axioms Prime43.prim43_pow
#print axioms Prime43.primary_only3

lang/Prime67.lean

lean_all/lang/Prime67.lean

/-
  lang/Prime67 — T48/T50, T48: ℤ[(1+√−67)/2] above a split prime: π of prime norm is prime, N(α) = p^k ⇒ α = uπ^jπ̄^l, and the
  H67-elements of norm p^k are the π^j π̄^l (H67 conjugation-stable).  (Port of EisPrime3.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.HeckeMult67

namespace Prime67

open FTAChain DetChain LibInt Euc67 Prim67 Uniq67 Frobenius

variable (P : Nat)

theorem nrm_mul (x y : Int × Int) : nrm ((Ring67.ring67 LibInt.intR).mul x y) = nrm x * nrm y :=
  Int.ofNat.inj (by show Int.ofNat _ = Int.ofNat (nrm x) * Int.ofNat (nrm y); rw [nrm_cast, nrm_cast, nrm_cast, en_mul])

theorem unit_inv (u : Int × Int) (h : nrm u = 1) : (Ring67.ring67 LibInt.intR).mul u (cj u) = (Ring67.ring67 LibInt.intR).one := by
  rw [mul_cj, ← nrm_cast, h]; rfl

/-- **An element of prime norm is prime.** -/
theorem eprime_div (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (x y : Int × Int)
    (h : Dvd' π ((Ring67.ring67 LibInt.intR).mul x y)) : Dvd' π x ∨ Dvd' π y := by
  match egcd_exists π x with
  | ⟨d, X, Y, hd, ⟨c1, hc1⟩, ⟨c2, hc2⟩⟩ =>
    have hdc : nrm d * nrm c1 = P + 1 + 1 := by rw [← nrm_mul, ← hc1, hπ]
    cases hp (nrm d) ⟨nrm c1, by rw [← hdc]; lib_nat⟩ with
    | inl h1 =>
      refine Or.inr (eeuclid π x y ((Ring67.ring67 LibInt.intR).mul (cj d) X) ((Ring67.ring67 LibInt.intR).mul (cj d) Y) ?_ h)
      rw [show (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul (cj d) X) π) ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul (cj d) Y) x)
          = (Ring67.ring67 LibInt.intR).mul (cj d) ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul X π) ((Ring67.ring67 LibInt.intR).mul Y x)) by lib_ring,
        ← hd, (Ring67.ring67 LibInt.intR).mul_comm, unit_inv d h1]
    | inr h1 =>
      have nc : nrm c1 = 1 := by
        have e : (P + 1 + 1) * nrm c1 = (P + 1 + 1) * 1 := by rw [← h1, hdc, h1]; lib_nat
        exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) e
      refine Or.inl ⟨(Ring67.ring67 LibInt.intR).mul (cj c1) c2, ?_⟩
      have hd' : d = (Ring67.ring67 LibInt.intR).mul π (cj c1) := by
        rw [hc1]
        calc d = (Ring67.ring67 LibInt.intR).mul d (Ring67.ring67 LibInt.intR).one := by lib_ring
          _ = _ := by rw [← unit_inv c1 nc]; lib_ring
      rw [hc2, hd']; lib_ring

theorem cj_mul (x y : Int × Int) : cj ((Ring67.ring67 LibInt.intR).mul x y) = (Ring67.ring67 LibInt.intR).mul (cj x) (cj y) := by
  apply Prod.ext
  · show x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2) + (x.1 * y.2 + x.2 * y.1 + x.2 * y.2) = (x.1 + x.2) * (y.1 + y.2) + -(-x.2 * -y.2 + -x.2 * -y.2 + -x.2 * -y.2 + -x.2 * -y.2 + -x.2 * -y.2 + -x.2 * -y.2 + -x.2 * -y.2 + -x.2 *  -- (+132 chars; full line in the Zenodo source)
    lib_int
  · show -(x.1 * y.2 + x.2 * y.1 + x.2 * y.2) = (x.1 + x.2) * -y.2 + -x.2 * (y.1 + y.2) + -x.2 * -y.2
    lib_int

theorem cj_cj (z : Int × Int) : cj (cj z) = z := by
  apply Prod.ext
  · show z.1 + z.2 + -z.2 = z.1; lib_int
  · show - -z.2 = z.2; lib_int

theorem nrm_cj (z : Int × Int) : nrm (cj z) = nrm z := by unfold nrm; rw [en_cj]

theorem nrm_one_eq (z : Int × Int) (h : nrm z = 1) : en z = 1 := by rw [← nrm_cast, h]; rfl

/-- **Factorisation above a split prime.** -/
theorem factor3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) : ∀ k (α : Int × Int),
    nrm α = (P + 1 + 1) ^ k → ∃ u j l, nrm u = 1 ∧ j + l = k ∧
      α = (Ring67.ring67 LibInt.intR).mul u ((Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) π j) (pw (Ring67.ring67 LibInt.intR) (cj π) l))
  | 0, α, h => ⟨α, 0, 0, h, rfl, by show α = (Ring67.ring67 LibInt.intR).mul α ((Ring67.ring67 LibInt.intR).mul (Ring67.ring67 LibInt.intR).one (Ring67.ring67 LibInt.intR).one); lib_ring⟩
  | k + 1, α, h => by
    -- π | α ᾱ
    have hdiv : Dvd' π ((Ring67.ring67 LibInt.intR).mul α (cj α)) := by
      refine ⟨(Ring67.ring67 LibInt.intR).mul (cj π) (Int.ofNat ((P + 1 + 1) ^ k), 0), ?_⟩
      rw [mul_cj, ← nrm_cast, h]
      rw [show (Ring67.ring67 LibInt.intR).mul π ((Ring67.ring67 LibInt.intR).mul (cj π) (Int.ofNat ((P + 1 + 1) ^ k), 0)) =
        (Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul π (cj π)) (Int.ofNat ((P + 1 + 1) ^ k), 0) by lib_ring, mul_cj, ← nrm_cast, hπ]
      apply Prod.ext
      · show Int.ofNat ((P + 1 + 1) ^ k * (P + 1 + 1)) = Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ k) + -(0 * 0)
        show Int.ofNat ((P + 1 + 1) ^ k) * Int.ofNat (P + 1 + 1) = _; lib_int
      · show (0 : Int) = Int.ofNat (P + 1 + 1) * 0 + 0 * Int.ofNat ((P + 1 + 1) ^ k) + -(0 * 0); lib_int
    have cancel : ∀ β : Int × Int, nrm ((Ring67.ring67 LibInt.intR).mul π β) = (P + 1 + 1) ^ (k + 1) → nrm β = (P + 1 + 1) ^ k := fun β hb => by
      rw [nrm_mul, hπ] at hb
      exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) (hb.trans (Nat.mul_comm ((P + 1 + 1) ^ k) (P + 1 + 1)))
    cases eprime_div P hp π hπ α (cj α) hdiv with
    | inl hα =>
      obtain ⟨β, hβ⟩ := hα
      obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 hp π hπ k β (cancel β (by rw [← hβ]; exact h))
      refine ⟨u, j + 1, l, hu, by rw [← hjl]; lib_nat, ?_⟩
      rw [hβ, he]; show _ = (Ring67.ring67 LibInt.intR).mul u ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) π j) π) _); lib_ring
    | inr hα =>
      obtain ⟨β, hβ⟩ := hα
      have hα' : α = (Ring67.ring67 LibInt.intR).mul (cj π) (cj β) := by rw [← cj_mul, ← hβ, cj_cj]
      have hb : nrm (cj β) = (P + 1 + 1) ^ k := by
        rw [nrm_cj]; apply cancel
        rw [← hβ, nrm_cj]; exact h
      obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 hp π hπ k (cj β) hb
      refine ⟨u, j, l + 1, hu, by rw [← hjl]; lib_nat, ?_⟩
      rw [hα', he]; show _ = (Ring67.ring67 LibInt.intR).mul u ((Ring67.ring67 LibInt.intR).mul _ ((Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) (cj π) l) (cj π))); lib_ring

theorem prim67_one : Prim67 (Ring67.ring67 LibInt.intR).one := rfl

theorem prim67_pow (z : Int × Int) (h : Prim67 z) : ∀ j, Prim67 (pw (Ring67.ring67 LibInt.intR) z j)
  | 0 => prim67_one
  | j + 1 => prim_mul _ _ (prim67_pow z h j) h

/-- **The primary elements of norm p^k are the π^j π̄^l.** -/
theorem primary_only3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim67 π)
    (k : Nat) (α : Int × Int) (hα : nrm α = (P + 1 + 1) ^ k) (pα : Prim67 α) :
    ∃ j l, j + l = k ∧ α = (Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) π j) (pw (Ring67.ring67 LibInt.intR) (cj π) l) := by
  obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 P hp π hπ k α hα
  have pw' := prim_mul _ _ (prim67_pow π pπ j) (prim67_pow (cj π) (prim_cj π pπ) l)
  have u1 := unit_one u _ (nrm_one_eq u hu) pw' (by rw [← he]; exact pα)
  exact ⟨j, l, hjl, by rw [he, u1]; lib_ring⟩

end Prime67

#print axioms Prime67.nrm_mul
#print axioms Prime67.unit_inv
#print axioms Prime67.eprime_div
#print axioms Prime67.cj_mul
#print axioms Prime67.cj_cj
#print axioms Prime67.nrm_cj
#print axioms Prime67.nrm_one_eq
#print axioms Prime67.factor3
#print axioms Prime67.prim67_one
#print axioms Prime67.prim67_pow
#print axioms Prime67.primary_only3

lang/Prime7.lean

lean_all/lang/Prime7.lean

/-
  lang/Prime7 — T48/T50, cycle 643: ℤ[(1+√−7)/2] above a split prime: π of prime norm is prime, N(α) = p^k ⇒ α = uπ^jπ̄^l, and the
  H7-elements of norm p^k are the π^j π̄^l (H7 conjugation-stable).  (Port of EisPrime3.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.HeckeMult7

namespace Prime7

open FTAChain DetChain LibInt Euc7 Prim7 Uniq7 Frobenius

variable (P : Nat)

theorem nrm_mul (x y : Int × Int) : nrm ((Ring7.ring7 LibInt.intR).mul x y) = nrm x * nrm y :=
  Int.ofNat.inj (by show Int.ofNat _ = Int.ofNat (nrm x) * Int.ofNat (nrm y); rw [nrm_cast, nrm_cast, nrm_cast, en_mul])

theorem unit_inv (u : Int × Int) (h : nrm u = 1) : (Ring7.ring7 LibInt.intR).mul u (cj u) = (Ring7.ring7 LibInt.intR).one := by
  rw [mul_cj, ← nrm_cast, h]; rfl

/-- **An element of prime norm is prime.** -/
theorem eprime_div (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (x y : Int × Int)
    (h : Dvd' π ((Ring7.ring7 LibInt.intR).mul x y)) : Dvd' π x ∨ Dvd' π y := by
  match egcd_exists _ π x (Nat.le_refl _) with
  | ⟨d, X, Y, hd, ⟨c1, hc1⟩, ⟨c2, hc2⟩⟩ =>
    have hdc : nrm d * nrm c1 = P + 1 + 1 := by rw [← nrm_mul, ← hc1, hπ]
    cases hp (nrm d) ⟨nrm c1, by rw [← hdc]; lib_nat⟩ with
    | inl h1 =>
      refine Or.inr (eeuclid π x y ((Ring7.ring7 LibInt.intR).mul (cj d) X) ((Ring7.ring7 LibInt.intR).mul (cj d) Y) ?_ h)
      rw [show (Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul (cj d) X) π) ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul (cj d) Y) x)
          = (Ring7.ring7 LibInt.intR).mul (cj d) ((Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul X π) ((Ring7.ring7 LibInt.intR).mul Y x)) by lib_ring,
        ← hd, (Ring7.ring7 LibInt.intR).mul_comm, unit_inv d h1]
    | inr h1 =>
      have nc : nrm c1 = 1 := by
        have e : (P + 1 + 1) * nrm c1 = (P + 1 + 1) * 1 := by rw [← h1, hdc, h1]; lib_nat
        exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) e
      refine Or.inl ⟨(Ring7.ring7 LibInt.intR).mul (cj c1) c2, ?_⟩
      have hd' : d = (Ring7.ring7 LibInt.intR).mul π (cj c1) := by
        rw [hc1]
        calc d = (Ring7.ring7 LibInt.intR).mul d (Ring7.ring7 LibInt.intR).one := by lib_ring
          _ = _ := by rw [← unit_inv c1 nc]; lib_ring
      rw [hc2, hd']; lib_ring

theorem cj_mul (x y : Int × Int) : cj ((Ring7.ring7 LibInt.intR).mul x y) = (Ring7.ring7 LibInt.intR).mul (cj x) (cj y) := by
  apply Prod.ext
  · show x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2) + (x.1 * y.2 + x.2 * y.1 + x.2 * y.2) = (x.1 + x.2) * (y.1 + y.2) + -(-x.2 * -y.2 + -x.2 * -y.2)
    lib_int
  · show -(x.1 * y.2 + x.2 * y.1 + x.2 * y.2) = (x.1 + x.2) * -y.2 + -x.2 * (y.1 + y.2) + -x.2 * -y.2
    lib_int

theorem cj_cj (z : Int × Int) : cj (cj z) = z := by
  apply Prod.ext
  · show z.1 + z.2 + -z.2 = z.1; lib_int
  · show - -z.2 = z.2; lib_int

theorem nrm_cj (z : Int × Int) : nrm (cj z) = nrm z := by unfold nrm; rw [en_cj]

theorem nrm_one_eq (z : Int × Int) (h : nrm z = 1) : en z = 1 := by rw [← nrm_cast, h]; rfl

/-- **Factorisation above a split prime.** -/
theorem factor3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) : ∀ k (α : Int × Int),
    nrm α = (P + 1 + 1) ^ k → ∃ u j l, nrm u = 1 ∧ j + l = k ∧
      α = (Ring7.ring7 LibInt.intR).mul u ((Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) π j) (pw (Ring7.ring7 LibInt.intR) (cj π) l))
  | 0, α, h => ⟨α, 0, 0, h, rfl, by show α = (Ring7.ring7 LibInt.intR).mul α ((Ring7.ring7 LibInt.intR).mul (Ring7.ring7 LibInt.intR).one (Ring7.ring7 LibInt.intR).one); lib_ring⟩
  | k + 1, α, h => by
    -- π | α ᾱ
    have hdiv : Dvd' π ((Ring7.ring7 LibInt.intR).mul α (cj α)) := by
      refine ⟨(Ring7.ring7 LibInt.intR).mul (cj π) (Int.ofNat ((P + 1 + 1) ^ k), 0), ?_⟩
      rw [mul_cj, ← nrm_cast, h]
      rw [show (Ring7.ring7 LibInt.intR).mul π ((Ring7.ring7 LibInt.intR).mul (cj π) (Int.ofNat ((P + 1 + 1) ^ k), 0)) =
        (Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul π (cj π)) (Int.ofNat ((P + 1 + 1) ^ k), 0) by lib_ring, mul_cj, ← nrm_cast, hπ]
      apply Prod.ext
      · show Int.ofNat ((P + 1 + 1) ^ k * (P + 1 + 1)) = Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ k) + -(0 * 0)
        show Int.ofNat ((P + 1 + 1) ^ k) * Int.ofNat (P + 1 + 1) = _; lib_int
      · show (0 : Int) = Int.ofNat (P + 1 + 1) * 0 + 0 * Int.ofNat ((P + 1 + 1) ^ k) + -(0 * 0); lib_int
    have cancel : ∀ β : Int × Int, nrm ((Ring7.ring7 LibInt.intR).mul π β) = (P + 1 + 1) ^ (k + 1) → nrm β = (P + 1 + 1) ^ k := fun β hb => by
      rw [nrm_mul, hπ] at hb
      exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) (hb.trans (Nat.mul_comm ((P + 1 + 1) ^ k) (P + 1 + 1)))
    cases eprime_div P hp π hπ α (cj α) hdiv with
    | inl hα =>
      obtain ⟨β, hβ⟩ := hα
      obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 hp π hπ k β (cancel β (by rw [← hβ]; exact h))
      refine ⟨u, j + 1, l, hu, by rw [← hjl]; lib_nat, ?_⟩
      rw [hβ, he]; show _ = (Ring7.ring7 LibInt.intR).mul u ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) π j) π) _); lib_ring
    | inr hα =>
      obtain ⟨β, hβ⟩ := hα
      have hα' : α = (Ring7.ring7 LibInt.intR).mul (cj π) (cj β) := by rw [← cj_mul, ← hβ, cj_cj]
      have hb : nrm (cj β) = (P + 1 + 1) ^ k := by
        rw [nrm_cj]; apply cancel
        rw [← hβ, nrm_cj]; exact h
      obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 hp π hπ k (cj β) hb
      refine ⟨u, j, l + 1, hu, by rw [← hjl]; lib_nat, ?_⟩
      rw [hα', he]; show _ = (Ring7.ring7 LibInt.intR).mul u ((Ring7.ring7 LibInt.intR).mul _ ((Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) (cj π) l) (cj π))); lib_ring

theorem prim7_one : Prim7 (Ring7.ring7 LibInt.intR).one := ⟨0, Or.inl rfl⟩

theorem prim7_pow (z : Int × Int) (h : Prim7 z) : ∀ j, Prim7 (pw (Ring7.ring7 LibInt.intR) z j)
  | 0 => prim7_one
  | j + 1 => prim7_mul _ _ (prim7_pow z h j) h

/-- **The primary elements of norm p^k are the π^j π̄^l.** -/
theorem primary_only3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim7 π)
    (k : Nat) (α : Int × Int) (hα : nrm α = (P + 1 + 1) ^ k) (pα : Prim7 α) :
    ∃ j l, j + l = k ∧ α = (Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) π j) (pw (Ring7.ring7 LibInt.intR) (cj π) l) := by
  obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 P hp π hπ k α hα
  have pw' := prim7_mul _ _ (prim7_pow π pπ j) (prim7_pow (cj π) (prim7_cj π pπ) l)
  have u1 := unit_one7 u _ (nrm_one_eq u hu) pw' (by rw [← he]; exact pα)
  exact ⟨j, l, hjl, by rw [he, u1]; lib_ring⟩

end Prime7

#print axioms Prime7.nrm_mul
#print axioms Prime7.unit_inv
#print axioms Prime7.eprime_div
#print axioms Prime7.cj_mul
#print axioms Prime7.cj_cj
#print axioms Prime7.nrm_cj
#print axioms Prime7.nrm_one_eq
#print axioms Prime7.factor3
#print axioms Prime7.prim7_one
#print axioms Prime7.prim7_pow
#print axioms Prime7.primary_only3

lang/PrimeOneModQ.lean

lean_all/lang/PrimeOneModQ.lean

/-
  lang/PrimeOneModQ — T43, cycle 15 (GL1 over ℚ): for every prime q a prime p ≡ 1 (mod q).

  N = 1 + q + … + q^(q−1); p = least prime factor of N.
    geomN          N = 1 + q · M  (so q ∤ N and p ≠ q)
    mk_pow, mk_geom   residues: (mk q)^i = mk (q^i), Σ_{i<q} (mk q)^i = mk N = 0 in ℤ/p
    geom_ring      (x − 1) Σ_{i<n} x^i = x^n − 1 in any commutative ring
    prime_one_mod_q   q prime ⇒ ∃ prime p with q | p − 1
  Checked first: least prime factors 3, 13, 11, 29, 15797, 53 for q = 2, 3, 5, 7, 11, 13, each ≡ 1 (mod q).

  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.Order

namespace PrimeOneModQ

open FTAChain ZMod TwoPower Order

/-- `Σ_{i<n} q^i` in ℕ. -/
def gs (q : Nat) : Nat → Nat
  | 0 => 0
  | n + 1 => gs q n + q ^ n

/-- `Σ_{i<n+1} q^i = 1 + q · Σ_{i<n} q^i`. -/
theorem geomN (q : Nat) : ∀ n, gs q (n + 1) = 1 + q * gs q n
  | 0 => by show 0 + q ^ 0 = 1 + q * 0; rfl
  | n + 1 => by
    show gs q (n + 1) + q ^ (n + 1) = 1 + q * (gs q n + q ^ n)
    rw [geomN q n, nleft_distrib, nadd_assoc, nmul_comm q (q ^ n)]
    rfl

theorem mk_pow (m a : Nat) : ∀ e, Frobenius.pw (ZMod.ring m) (mk m a) e = mk m (a ^ e)
  | 0 => rfl
  | e + 1 => by
    show (ZMod.ring m).mul (Frobenius.pw (ZMod.ring m) (mk m a) e) (mk m a) = mk m (a ^ e * a)
    rw [mk_pow m a e, mk_mul]

theorem mk_add (m a b : Nat) : (ZMod.ring m).add (mk m a) (mk m b) = mk m (a + b) :=
  Subtype.ext (by show md m (md m a + md m b) = md m (a + b); rw [← md_add2])

theorem mk_geom (m q : Nat) : ∀ n, YMChain.sumK (ZMod.ring m) (Frobenius.pw (ZMod.ring m) (mk m q)) (LinkIntegralZN.rng n)
    = mk m (gs q n)
  | 0 => rfl
  | n + 1 => by
    show YMChain.sumK (ZMod.ring m) (Frobenius.pw (ZMod.ring m) (mk m q)) (LinkIntegralZN.rng n ++ [n]) = mk m (gs q n + q ^ n)
    rw [LinkIntegralZN.sumK_app, mk_geom m q n]
    show (ZMod.ring m).add (mk m (gs q n)) ((ZMod.ring m).add (Frobenius.pw (ZMod.ring m) (mk m q) n) (ZMod.ring m).zero) = _
    rw [DetChain.add_zero', mk_pow, mk_add]

theorem gs_one : ∀ n, gs 1 n = n
  | 0 => rfl
  | n + 1 => by show gs 1 n + 1 ^ n = n + 1; rw [gs_one n, Nat.one_pow]

theorem geom_ring {R : Type} (K : DetChain.CRing R) (x : R) (n : Nat) :
    K.mul (K.add x (K.neg K.one)) (YMChain.sumK K (Frobenius.pw K x) (LinkIntegralZN.rng n))
      = K.add (Frobenius.pw K x n) (K.neg K.one) := by
  have e := LinkIntegralZN.geom_shift K x n
  rw [DetChain.right_distrib, e, DetChain.neg_mul_left, K.one_mul, K.add_comm (YMChain.sumK K _ _),
    K.add_assoc, K.add_neg, DetChain.add_zero']

theorem pow_succ_form (q' : Nat) : ∀ t, ∃ r, (q' + 1) ^ t = r + 1
  | 0 => ⟨0, rfl⟩
  | t + 1 => by
    obtain ⟨r, hr⟩ := pow_succ_form q' t
    refine ⟨r * (q' + 1) + q', ?_⟩
    show (q' + 1) ^ t * (q' + 1) = r * (q' + 1) + q' + 1
    rw [hr, nright_distrib, nmul_comm 1 (q' + 1), nmul_one, nadd_assoc]

/-- **Primes ≡ 1 (mod q)** for every prime q = t + 2. -/
theorem prime_one_mod_q (t : Nat) (hq : ∀ d, Dv d (t + 1 + 1) → d = 1 ∨ d = t + 1 + 1) :
    ∃ l, (∀ e, Dv e (l + 1 + 1) → e = 1 ∨ e = l + 1 + 1) ∧ Dv (t + 1 + 1) (l + 1) := by
  -- N = gs q q = 1 + q · M, M = gs q (t+1) = M' + 1
  obtain ⟨r, hr⟩ := pow_succ_form (t + 1) t
  have hM : gs (t + 1 + 1) (t + 1) = (gs (t + 1 + 1) t + r) + 1 := by
    show gs (t + 1 + 1) t + (t + 1 + 1) ^ t = _
    rw [hr]
    rfl
  have hN : gs (t + 1 + 1) (t + 1 + 1) = 1 + (t + 1 + 1) * ((gs (t + 1 + 1) t + r) + 1) := by
    rw [geomN, hM]
  -- N = i + 2 with i = q (M' ) + t + 1
  have hNi : gs (t + 1 + 1) (t + 1 + 1) = ((t + 1 + 1) * (gs (t + 1 + 1) t + r) + t + 1) + 1 + 1 := by
    rw [hN, nleft_distrib, nmul_one, Nat.add_comm 1]
    rfl
  obtain ⟨l, hl, hmin⟩ := lp_exists ((t + 1 + 1) * (gs (t + 1 + 1) t + r) + t + 1)
  rw [← hNi] at hl hmin
  have hpr := lp_prime _ l hl hmin
  refine ⟨l, hpr, ?_⟩
  -- in ℤ/p, x = residue of q
  let K := ZMod.ring (l + 1)
  let x := mk (l + 1) (t + 1 + 1)
  have hS : YMChain.sumK K (Frobenius.pw K x) (LinkIntegralZN.rng (t + 1 + 1)) = K.zero := by
    rw [mk_geom]; exact Subtype.ext (dv_md_zero (l + 1) _ hl)
  have hxq : Frobenius.pw K x (t + 1 + 1) = K.one := by
    have g := geom_ring K x (t + 1 + 1)
    rw [hS, DetChain.mul_zero] at g
    have := congrArg (fun z => K.add z K.one) g.symm
    have h' : K.add (K.add (Frobenius.pw K x (t + 1 + 1)) (K.neg K.one)) K.one = K.add K.zero K.one := this
    rw [K.add_assoc, DetChain.add_neg' K K.one, DetChain.add_zero', K.zero_add] at h'
    exact h'
  -- p ≠ q: p | N = 1 + q M and p | q would give p | 1
  have hx1 : x ≠ K.one := fun e => by
    have hS1 : YMChain.sumK K (Frobenius.pw K K.one) (LinkIntegralZN.rng (t + 1 + 1)) = K.zero := by
      rw [← e]; exact hS
    -- Σ_{i<q} 1 = mk q
    have hq1 : YMChain.sumK K (Frobenius.pw K K.one) (LinkIntegralZN.rng (t + 1 + 1)) = mk (l + 1) (t + 1 + 1) := by
      have := mk_geom (l + 1) 1 (t + 1 + 1)
      have one_eq : mk (l + 1) 1 = K.one := rfl
      rw [one_eq] at this
      rw [this]
      exact congrArg (mk (l + 1)) (gs_one (t + 1 + 1))
    rw [hq1] at hS1
    have hdq : Dv (l + 1 + 1) (t + 1 + 1) := md_zero_dv (l + 1) _ (congrArg Subtype.val hS1)
    cases hq (l + 1 + 1) hdq with
    | inl h1 => exact Nat.noConfusion (Nat.succ.inj h1)
    | inr hpq =>
      -- p = q divides N − 1 = q M, and N
      have hdN : Dv (l + 1 + 1) (1 + (t + 1 + 1) * ((gs (t + 1 + 1) t + r) + 1)) := by rw [← hN]; exact hl
      have hdqM : Dv (l + 1 + 1) ((t + 1 + 1) * ((gs (t + 1 + 1) t + r) + 1)) := by
        rw [hpq]; exact ⟨(gs (t + 1 + 1) t + r) + 1, nmul_comm _ _⟩
      rw [Nat.add_comm 1 ((t + 1 + 1) * ((gs (t + 1 + 1) t + r) + 1))] at hdN
      exact dv_one_absurd l (dv_sub (l + 1) _ 1 hdqM hdN)
  -- order of x is q
  obtain ⟨d, hd, hdmin⟩ := ord_exists K x (t + 1) hxq
  have hdq : Dv (d + 1) (t + 1 + 1) := ord_dvd K x d hd hdmin (t + 1 + 1) hxq
  have hd' : d + 1 = t + 1 + 1 := by
    cases hq (d + 1) hdq with
    | inr h => exact h
    | inl h =>
      have h0 : d = 0 := Nat.succ.inj h
      rw [h0] at hd
      exact absurd (by have := hd; show x = K.one; rw [← K.one_mul x]; exact this) hx1
  -- x ≠ 0, so x^(p−1) = 1, and the order divides p − 1
  have hx0 : x ≠ K.zero := fun e => by
    rw [e] at hxq
    have h0 : Frobenius.pw K K.zero (t + 1 + 1) = K.zero := DetChain.mul_zero K _
    rw [h0] at hxq
    have := congrArg Subtype.val hxq
    rw [one_val l] at this
    exact Nat.noConfusion this
  have hp1 := ZMod.unit_pow l hpr x hx0
  have := ord_dvd K x d hd hdmin (l + 1) hp1
  rw [hd'] at this
  exact this

end PrimeOneModQ

#print axioms PrimeOneModQ.geomN
#print axioms PrimeOneModQ.mk_pow
#print axioms PrimeOneModQ.mk_add
#print axioms PrimeOneModQ.mk_geom
#print axioms PrimeOneModQ.gs_one
#print axioms PrimeOneModQ.geom_ring
#print axioms PrimeOneModQ.pow_succ_form
#print axioms PrimeOneModQ.prime_one_mod_q

lang/PrimePowerOne.lean

lean_all/lang/PrimePowerOne.lean

/-
  lang/PrimePowerOne — T43, cycle 16 (GL1 over ℚ): for every prime q and e ≥ 1, a prime p ≡ 1 (mod q^e).

  y = q^(q^s) (e = s + 1), N = 1 + y + … + y^(q−1), p = least prime factor of N, x = residue of q in ℤ/p.
    prime_dvd_pow     r prime, r | q^e, q prime ⇒ r = q
    proper_div        d · k = q^(s+1), k ≥ 2 ⇒ d | q^s
    prime_one_mod_pow q prime ⇒ ∃ prime p with q^(s+1) | p − 1
  (x^(q^s))^q = 1 by the geometric sum, x^(q^s) ≠ 1 since N ≡ 1 (mod q), so ord(x) = q^(s+1) and it divides
  p − 1. Checked first for q = 2, 3, 5, 7, e ≤ 3 (e.g. 3597751 ≡ 1 mod 125).

  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.PrimeOneModQ

namespace PrimePowerOne

open FTAChain ZMod TwoPower Order PrimeOneModQ

/-- `r | q^e` with `r, q` prime gives `r = q`. -/
theorem prime_dvd_pow (j t : Nat) (hr : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1)
    (hq : ∀ d, Dv d (t + 1 + 1) → d = 1 ∨ d = t + 1 + 1) :
    ∀ e, Dv (j + 1 + 1) ((t + 1 + 1) ^ e) → j + 1 + 1 = t + 1 + 1
  | 0, h => absurd h (dv_one_absurd j)
  | e + 1, h => by
    -- q^(e+1) = (t+1+1) * q^e ... use Euclid with q^e * q
    have h' : Dv (j + 1 + 1) ((t + 1) + 1) ∨ Dv (j + 1 + 1) ((t + 1 + 1) ^ e) := by
      have hh : Dv (j + 1 + 1) (((t + 1) + 1) * (t + 1 + 1) ^ e) := by
        rw [nmul_comm]; exact h
      exact euclid_core j (t + 1) _ hr hh
    cases h' with
    | inl hq' =>
      cases hq (j + 1 + 1) hq' with
      | inl h1 => exact absurd (Nat.succ.inj h1) (Nat.noConfusion)
      | inr h2 => exact h2
    | inr he => exact prime_dvd_pow j t hr hq e he

theorem pow_succ' (a n : Nat) : a ^ (n + 1) = a ^ n * a := rfl

/-- A proper divisor of `q^(s+1)` divides `q^s`. -/
theorem proper_div (t s d k' : Nat) (hq : ∀ d, Dv d (t + 1 + 1) → d = 1 ∨ d = t + 1 + 1)
    (h : d * (k' + 1 + 1) = (t + 1 + 1) ^ (s + 1)) : Dv d ((t + 1 + 1) ^ s) := by
  obtain ⟨l, hl, hmin⟩ := lp_exists k'
  have hpr := lp_prime _ l hl hmin
  have hk : Dv (k' + 1 + 1) ((t + 1 + 1) ^ (s + 1)) := ⟨d, by rw [← h, nmul_comm]⟩
  have hrq : l + 1 + 1 = t + 1 + 1 := prime_dvd_pow l t hpr hq (s + 1) (dv_trans _ _ _ hl hk)
  obtain ⟨c, hc⟩ := hl
  rw [hrq] at hc
  -- d (c q) = q^s q  ⇒  d c = q^s
  have e : (t + 1 + 1) * (d * c) = (t + 1 + 1) * (t + 1 + 1) ^ s := by
    rw [nmul_comm (t + 1 + 1) ((t + 1 + 1) ^ s), ← pow_succ', ← h, hc, nmul_comm c, ← nmul_assoc,
      nmul_comm (t + 1 + 1) d, nmul_assoc]
  have e' := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (t + 1)) e
  exact ⟨c, by rw [← e', nmul_comm]⟩

/-- **Primes ≡ 1 (mod q^(s+1))** for every prime q = t + 2 and every s. -/
theorem prime_one_mod_pow (t s : Nat) (hq : ∀ d, Dv d (t + 1 + 1) → d = 1 ∨ d = t + 1 + 1) :
    ∃ l, (∀ e, Dv e (l + 1 + 1) → e = 1 ∨ e = l + 1 + 1) ∧ Dv ((t + 1 + 1) ^ (s + 1)) (l + 1) := by
  obtain ⟨w, hw⟩ := pow_succ_form (t + 1) s
  obtain ⟨Y, hY⟩ := pow_succ_form (t + 1) ((t + 1 + 1) ^ s)
  obtain ⟨r, hr⟩ := pow_succ_form Y t
  obtain ⟨v, hv⟩ := pow_succ_form (t + 1) w
  -- y = q^(q^s), M = gs y (t+1) = M' + 1
  have hM : gs ((t + 1 + 1) ^ (t + 1 + 1) ^ s) (t + 1) = (gs ((t + 1 + 1) ^ (t + 1 + 1) ^ s) t + r) + 1 := by
    show gs _ t + ((t + 1 + 1) ^ (t + 1 + 1) ^ s) ^ t = _
    rw [hY, hr]; rfl
  have hy : (t + 1 + 1) ^ (t + 1 + 1) ^ s = (t + 1 + 1) ^ w * (t + 1 + 1) := by rw [hw]; rfl
  -- N = 1 + q · Q, Q = q^w (M' + 1) = Q' + 1
  have hQ : (t + 1 + 1) ^ w * ((gs ((t + 1 + 1) ^ (t + 1 + 1) ^ s) t + r) + 1)
      = ((v + 1) * (gs ((t + 1 + 1) ^ (t + 1 + 1) ^ s) t + r) + v) + 1 := by
    rw [hv, nleft_distrib, nmul_one]
    rfl
  have hN : gs ((t + 1 + 1) ^ (t + 1 + 1) ^ s) (t + 1 + 1)
      = 1 + (t + 1 + 1) * (((v + 1) * (gs ((t + 1 + 1) ^ (t + 1 + 1) ^ s) t + r) + v) + 1) := by
    have hyM : ∀ Mx, (t + 1 + 1) ^ (t + 1 + 1) ^ s * Mx = (t + 1 + 1) * ((t + 1 + 1) ^ w * Mx) := fun Mx => by
      rw [hy, nmul_comm ((t + 1 + 1) ^ w) (t + 1 + 1), nmul_assoc]
    rw [geomN, hM, hyM, hQ]
  have hNi : gs ((t + 1 + 1) ^ (t + 1 + 1) ^ s) (t + 1 + 1)
      = ((t + 1 + 1) * ((v + 1) * (gs ((t + 1 + 1) ^ (t + 1 + 1) ^ s) t + r) + v) + t + 1) + 1 + 1 := by
    rw [hN, nleft_distrib, nmul_one, Nat.add_comm 1]
    rfl
  obtain ⟨l, hl, hmin⟩ := lp_exists ((t + 1 + 1) * ((v + 1) * (gs ((t + 1 + 1) ^ (t + 1 + 1) ^ s) t + r) + v) + t + 1)
  rw [← hNi] at hl hmin
  have hpr := lp_prime _ l hl hmin
  refine ⟨l, hpr, ?_⟩
  let K := ZMod.ring (l + 1)
  let x := mk (l + 1) (t + 1 + 1)
  have hX : Frobenius.pw K x ((t + 1 + 1) ^ s) = mk (l + 1) ((t + 1 + 1) ^ (t + 1 + 1) ^ s) := mk_pow _ _ _
  have hS : YMChain.sumK K (Frobenius.pw K (Frobenius.pw K x ((t + 1 + 1) ^ s))) (LinkIntegralZN.rng (t + 1 + 1)) = K.zero := by
    rw [hX, mk_geom]; exact Subtype.ext (dv_md_zero (l + 1) _ hl)
  have hXq : Frobenius.pw K (Frobenius.pw K x ((t + 1 + 1) ^ s)) (t + 1 + 1) = K.one := by
    have g := geom_ring K (Frobenius.pw K x ((t + 1 + 1) ^ s)) (t + 1 + 1)
    rw [hS, DetChain.mul_zero] at g
    have h' : K.add (K.add (Frobenius.pw K (Frobenius.pw K x ((t + 1 + 1) ^ s)) (t + 1 + 1)) (K.neg K.one)) K.one
        = K.add K.zero K.one := congrArg (fun z => K.add z K.one) g.symm
    rw [K.add_assoc, DetChain.add_neg' K K.one, DetChain.add_zero', K.zero_add] at h'
    exact h'
  have hxfull : Frobenius.pw K x ((t + 1 + 1) ^ (s + 1)) = K.one := by
    rw [pow_succ', ← pwpow K x]; exact hXq
  -- X ≠ 1
  have hX1 : Frobenius.pw K x ((t + 1 + 1) ^ s) ≠ K.one := fun e => by
    rw [e] at hS
    have hq1 : YMChain.sumK K (Frobenius.pw K K.one) (LinkIntegralZN.rng (t + 1 + 1)) = mk (l + 1) (t + 1 + 1) := by
      have := mk_geom (l + 1) 1 (t + 1 + 1)
      have one_eq : mk (l + 1) 1 = K.one := rfl
      rw [one_eq] at this
      rw [this]
      exact congrArg (mk (l + 1)) (gs_one (t + 1 + 1))
    rw [hq1] at hS
    have hdq : Dv (l + 1 + 1) (t + 1 + 1) := md_zero_dv (l + 1) _ (congrArg Subtype.val hS)
    cases hq (l + 1 + 1) hdq with
    | inl h1 => exact Nat.noConfusion (Nat.succ.inj h1)
    | inr hpq =>
      have hdN : Dv (l + 1 + 1) (1 + (t + 1 + 1) * (((v + 1) * (gs ((t + 1 + 1) ^ (t + 1 + 1) ^ s) t + r) + v) + 1)) := by
        rw [← hN]; exact hl
      have hdqM : Dv (l + 1 + 1) ((t + 1 + 1) * (((v + 1) * (gs ((t + 1 + 1) ^ (t + 1 + 1) ^ s) t + r) + v) + 1)) := by
        rw [hpq]; exact ⟨_, nmul_comm _ _⟩
      rw [Nat.add_comm 1 ((t + 1 + 1) * (((v + 1) * (gs ((t + 1 + 1) ^ (t + 1 + 1) ^ s) t + r) + v) + 1))] at hdN
      exact dv_one_absurd l (dv_sub (l + 1) _ 1 hdqM hdN)
  -- the order of x is q^(s+1)
  obtain ⟨n, hn⟩ := pow_succ_form (t + 1) (s + 1)
  rw [hn] at hxfull
  obtain ⟨d, hd, hdmin⟩ := ord_exists K x n hxfull
  obtain ⟨c, hc⟩ := ord_dvd K x d hd hdmin (n + 1) hxfull
  have hdq : d + 1 = (t + 1 + 1) ^ (s + 1) := by
    cases c with
    | zero => rw [nzero_mul] at hc; exact absurd hc (Nat.noConfusion)
    | succ c =>
      cases c with
      | zero => rw [hn, hc]; exact (nzero_add' (d + 1)).symm
      | succ c =>
        exfalso
        apply hX1
        have hpd := proper_div t s (d + 1) c hq (by rw [hn, hc, nmul_comm])
        obtain ⟨m', hm'⟩ := hpd
        rw [hm']
        exact pw_qe K x (d + 1) 0 hd m'
  have hx0 : x ≠ K.zero := fun e => by
    rw [e] at hd
    have h0 : Frobenius.pw K K.zero (d + 1) = K.zero := DetChain.mul_zero K _
    rw [h0] at hd
    have := congrArg Subtype.val hd
    rw [one_val l] at this
    exact Nat.noConfusion this
  have hp1 := ZMod.unit_pow l hpr x hx0
  have := ord_dvd K x d hd hdmin (l + 1) hp1
  rw [hdq] at this
  exact this
where
  nzero_add' (a : Nat) : 1 * a = a := by rw [nmul_comm, nmul_one]

end PrimePowerOne

#print axioms PrimePowerOne.prime_dvd_pow
#print axioms PrimePowerOne.pow_succ'
#print axioms PrimePowerOne.proper_div
#print axioms PrimePowerOne.prime_one_mod_pow

lang/ProdVal.lean

lean_all/lang/ProdVal.lean

/-
  lang/ProdVal — T49 (O3') (v3), abstract: **the valuation of a product of powers is the sum over the factors
  divisible by P**. Entries (x, n, f): f = true ⇒ x = P·U with U a unit, f = false ⇒ x a unit. Then
    pv        Π x^n = P^(Σ_(f = true) n) · U for a unit U
  With exactly one flagged factor (lang/NormEval, ExactOne) the valuation of φ_(z^c)(σ_(−1)B) is that factor's exponent.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.ValArith
import lang.FinChoice

namespace ProdVal

open Frobenius

variable {R : Type} (K : DetChain.CRing R) (P : R)

def prd : List (R × Nat × Bool) → R
  | [] => K.one
  | e :: es => K.mul (pw K e.1 e.2.1) (prd es)

def fexp : List (R × Nat × Bool) → Nat
  | [] => 0
  | e :: es => (if e.2.2 = true then e.2.1 else 0) + fexp es

def Ok : List (R × Nat × Bool) → Prop
  | [] => True
  | e :: es => (e.2.2 = true → ∃ U w, e.1 = K.mul P U ∧ K.mul U w = K.one) ∧
      (e.2.2 = false → ∃ w, K.mul e.1 w = K.one) ∧ Ok es

theorem unit_pw (x w : R) (h : K.mul x w = K.one) : ∀ n, K.mul (pw K x n) (pw K w n) = K.one
  | 0 => by show K.mul K.one K.one = K.one; lib_ring
  | n + 1 => by
    show K.mul (K.mul (pw K x n) x) (K.mul (pw K w n) w) = K.one
    calc K.mul (K.mul (pw K x n) x) (K.mul (pw K w n) w) = K.mul (K.mul (pw K x n) (pw K w n)) (K.mul x w) := by
          lib_ring
      _ = K.one := by rw [unit_pw x w h n, h]; lib_ring

/-- **Π x^n = P^(flagged exponent sum) · unit**. -/
theorem pv : ∀ es, Ok K P es → ∃ U w, prd K es = K.mul (pw K P (fexp es)) U ∧ K.mul U w = K.one
  | [], _ => ⟨K.one, K.one, by show K.one = K.mul K.one K.one; lib_ring, by lib_ring⟩
  | e :: es, ⟨h1, h2, h3⟩ => by
    obtain ⟨U, w, hU, hw⟩ := pv es h3
    cases hf : e.2.2 with
    | true =>
      obtain ⟨V, v, hV, hv⟩ := h1 hf
      refine ⟨K.mul (pw K V e.2.1) U, K.mul (pw K v e.2.1) w, ?_, ?_⟩
      · show K.mul (pw K e.1 e.2.1) (prd K es) = K.mul (pw K P ((if e.2.2 = true then e.2.1 else 0) + fexp es)) _
        rw [hf, FinChoice.ite_t _ _ rfl, hU, hV, pw_mul, SymProdW.pw_add]
        generalize pw K P e.2.1 = A
        generalize pw K P (fexp es) = B
        lib_ring
      · calc K.mul (K.mul (pw K V e.2.1) U) (K.mul (pw K v e.2.1) w)
            = K.mul (K.mul (pw K V e.2.1) (pw K v e.2.1)) (K.mul U w) := by lib_ring
          _ = K.one := by rw [unit_pw K V v hv, hw]; lib_ring
    | false =>
      obtain ⟨v, hv⟩ := h2 hf
      refine ⟨K.mul (pw K e.1 e.2.1) U, K.mul (pw K v e.2.1) w, ?_, ?_⟩
      · show K.mul (pw K e.1 e.2.1) (prd K es) = K.mul (pw K P ((if e.2.2 = true then e.2.1 else 0) + fexp es)) _
        rw [hf, FinChoice.ite_f _ _ (fun h => Bool.noConfusion h), hU, show 0 + fexp es = fexp es by lib_nat]
        lib_ring
      · calc K.mul (K.mul (pw K e.1 e.2.1) U) (K.mul (pw K v e.2.1) w)
            = K.mul (K.mul (pw K e.1 e.2.1) (pw K v e.2.1)) (K.mul U w) := by lib_ring
          _ = K.one := by rw [unit_pw K e.1 v hv, hw]; lib_ring

end ProdVal

#print axioms ProdVal.unit_pw
#print axioms ProdVal.pv

lang/PtsDist.lean

lean_all/lang/PtsDist.lean

/-
  lang/PtsDist — T49 (O3') (i'): **the points z^n, …, z^1 have pairwise unit differences** once every difference
  z^b − z^a (a < b ≤ n) is a unit (in ℤ/p^k: red(z) of order ℓ and UnitPk).
    pts        [z^n, …, z^1]
    mem_pts    s ∈ pts n ⇒ s = z^a with 1 ≤ a ≤ n
    udist_pts  UDist (pts n)       (UnitRoots.UDist: later minus earlier is a unit)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.UnitRoots

namespace PtsDist

open Frobenius YMChain UnitRoots

variable {S : Type} (E : DetChain.CRing S) (z : S)

def pts : Nat → List S
  | 0 => []
  | n + 1 => pw E z (n + 1) :: pts n

theorem len_pts : ∀ n, (pts E z n).length = n
  | 0 => rfl
  | n + 1 => congrArg Nat.succ (len_pts n)

theorem mem_pts : ∀ n s, memL s (pts E z n) → ∃ a, 1 ≤ a ∧ a ≤ n ∧ s = pw E z a
  | 0, _, h => h.elim
  | n + 1, s, h => by
    cases h with
    | inl h => exact ⟨n + 1, Nat.succ_le_succ (Nat.zero_le n), Nat.le_refl _, h⟩
    | inr h =>
      obtain ⟨a, h1, h2, h3⟩ := mem_pts n s h
      exact ⟨a, h1, Nat.le_trans h2 (Nat.le_succ n), h3⟩

theorem udist_pts (hU : ∀ a b, 1 ≤ a → a < b → ∃ w, E.mul (E.add (pw E z a) (E.neg (pw E z b))) w = E.one) :
    ∀ n, UDist E (pts E z n)
  | 0 => trivial
  | n + 1 => by
    refine ⟨fun s hs => ?_, udist_pts hU n⟩
    obtain ⟨a, h1, h2, h3⟩ := mem_pts E z n s hs
    rw [h3]
    exact hU a (n + 1) h1 (Nat.lt_succ_of_le h2)

end PtsDist

#print axioms PtsDist.len_pts
#print axioms PtsDist.mem_pts
#print axioms PtsDist.udist_pts

lang/Q256.lean

lean_all/lang/Q256.lean

/-
  lang/Q256 — T48, cycle 721: **the linear step of the genus-2 route for 256a** (OPEN 256a-split, step (i)).
  For σ ≠ 0 and 2h = 1 in 𝔽_p (h = 1/2):
    pt_id      (2X² − σ²)(2X − 2σ) = σ³h · z(z² + 4z + 2)  at X = σh(2 + z)
    quot_sub   Σ_X χ((2X² − σ²)(2X − 2σ)) = χ(σ³h) · Σ_z χ(z(z² + 4z + 2))
  i.e. the elliptic quotient C_σ of y² = x⁵ − x is 256a twisted by σ³h.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.CharSumGen
import lang.G4Gen

namespace Q256

open FTAChain ZMod YMChain FiniteRecChain DetChain QChar

variable (m : Nat)

theorem pt_id (σ h z : Zp (2 * m + 1 + 1)) (hh : (ring (2 * m + 1 + 1)).add h h = (ring (2 * m + 1 + 1)).one) :
    ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul σ h) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one z))) ((ring (2 * m + 1 + 1)).m  -- (+1237 chars; full line in the Zenodo source)
  have e0 : ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add h h) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [hh]; lib_ring
  have hq : ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul σ h) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one z))) ((ring (2 * m + 1  -- (+6543 chars; full line in the Zenodo source)
  rw [hq, e0]; lib_ring

theorem quot_sub (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (σ h : Zp (2 * m + 1 + 1))
    (hs : σ ≠ (ring (2 * m + 1 + 1)).zero) (hh : (ring (2 * m + 1 + 1)).add h h = (ring (2 * m + 1 + 1)).one) :
    sumK LibInt.intR (fun X => chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul X X)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul σ σ))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m +   -- (+214 chars; full line in the Zenodo source)
      chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul σ σ) σ) h) * sumK LibInt.intR (fun z => chi m ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)   -- (+203 chars; full line in the Zenodo source)
  have hh0 : h ≠ (ring (2 * m + 1 + 1)).zero := fun e => PowerSum.one_ne_zero' (2 * m + 1) (by rw [← hh, e]; lib_ring)
  have hsh : (ring (2 * m + 1 + 1)).mul σ h ≠ (ring (2 * m + 1 + 1)).zero := fun e => by
    rcases G4Gen.mul_eq_zero m hpr σ h e with e1 | e1
    · exact hs e1
    · exact hh0 e1
  rw [← CharSumGen.sum_scale LibInt.intR (2 * m) hpr _ ((ring (2 * m + 1 + 1)).mul σ h) hsh, ← CharSumGen.shift1 LibInt.intR (2 * m), ← CharSumGen.shift1 LibInt.intR (2 * m)]
  show _ = LibInt.intR.mul _ _
  rw [mul_sumK]
  exact sumK_ext LibInt.intR _ _ (fun z => by
    show chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul σ h) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one z))) ((ring (2 * m   -- (+691 chars; full line in the Zenodo source)
    rw [pt_id m σ h z hh, chi_mul m hpr]; rfl) _

end Q256

#print axioms Q256.pt_id
#print axioms Q256.quot_sub

lang/QBinom.lean

lean_all/lang/QBinom.lean

/-
  lang/QBinom — T47, cycle 543: Gaussian binomials and Cauchy's q-binomial theorem in coefficient form, in any commutative ring.

    gb q n k     [n; k]_q by Pascal: [n+1; k+1] = [n; k+1] + q^(n−k)[n; k], [n; 0] = 1, [0; k+1] = 0
    gb_big       [n; k] = 0 for k > n
    ec q n k     the coefficient of x^k in ∏_{j<n} (1 + x q^j):  e(n+1, k+1) = e(n, k+1) + q^n e(n, k)
    tri          k(k−1)/2 by recursion
    qbinom       e(n, k) = q^(tri k)·[n; k]_q
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.PSer
import lang.HasseCM
import lang.ChebAll
import lib.IntRing

namespace QBinom

open FTAChain

variable {R : Type} (K : DetChain.CRing R) (q : R)

def gb : Nat → Nat → R
  | _, 0 => K.one
  | 0, _ + 1 => K.zero
  | n + 1, k + 1 => K.add (gb n (k + 1)) (K.mul (Frobenius.pw K q (n - k)) (gb n k))

def ec : Nat → Nat → R
  | 0, 0 => K.one
  | 0, _ + 1 => K.zero
  | _ + 1, 0 => K.one
  | n + 1, k + 1 => K.add (ec n (k + 1)) (K.mul (Frobenius.pw K q n) (ec n k))

def tri : Nat → Nat
  | 0 => 0
  | k + 1 => tri k + k

theorem gb_big : ∀ n k, n < k → gb K q n k = K.zero
  | 0, 0, h => absurd h (Nat.lt_irrefl 0)
  | 0, k + 1, _ => rfl
  | n + 1, 0, h => absurd h (Nat.not_succ_le_zero _)
  | n + 1, k + 1, h => by
    show K.add (gb K q n (k + 1)) (K.mul (Frobenius.pw K q (n - k)) (gb K q n k)) = K.zero
    rw [gb_big n (k + 1) (Nat.lt_trans (Nat.lt_succ_self n) h), gb_big n k (Nat.lt_of_succ_lt_succ h)]
    lib_ring

theorem qbinom : ∀ n k, ec K q n k = K.mul (Frobenius.pw K q (tri k)) (gb K q n k)
  | 0, 0 => by show K.one = K.mul K.one K.one; lib_ring
  | 0, k + 1 => by show K.zero = K.mul _ K.zero; lib_ring
  | n + 1, 0 => by show K.one = K.mul K.one K.one; lib_ring
  | n + 1, k + 1 => by
    show K.add (ec K q n (k + 1)) (K.mul (Frobenius.pw K q n) (ec K q n k)) =
      K.mul (Frobenius.pw K q (tri k + k)) (K.add (gb K q n (k + 1)) (K.mul (Frobenius.pw K q (n - k)) (gb K q n k)))
    rw [qbinom n (k + 1), qbinom n k]
    show K.add (K.mul (Frobenius.pw K q (tri k + k)) (gb K q n (k + 1))) (K.mul (Frobenius.pw K q n) (K.mul (Frobenius.pw K q (tri k)) (gb K q n k))) = _
    cases Nat.lt_or_ge n k with
    | inl h =>
      rw [gb_big K q n k h]; lib_ring
    | inr h =>
      obtain ⟨d, hd⟩ := Cheb.le_dest2 k n h
      have hs : d + k - k = d := by
        have h' := LibInt.nsub_add_add d 0 k
        rw [Nat.zero_add] at h'; exact h'
      rw [← hd, hs]
      rw [HasseCM.pw_add', HasseCM.pw_add']
      generalize Frobenius.pw K q (tri k) = A
      generalize Frobenius.pw K q k = B
      generalize Frobenius.pw K q d = C
      lib_ring

end QBinom

#print axioms QBinom.gb_big
#print axioms QBinom.qbinom

lang/QBinom2.lean

lean_all/lang/QBinom2.lean

/-
  lang/QBinom2 — T47, cycle 544: the other Pascal rule and the double Pascal rule for Gaussian binomials (base Q).

    sym_rel     (1 − Q^(k+1))·[n; k+1] = (1 − Q^(n−k))·[n; k]
    pascalA     [n+1; k+1] = Q^(k+1)·[n; k+1] + [n; k]
    dpascal     [n+2; m+2] = Q^(m+2)·[n; m+2] + (1 + Q^(n+1))·[n; m+1] + Q^(n−m)·[n; m]     (m ≤ n)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.QBinom

namespace QBinom

open FTAChain

variable {R : Type} (K : DetChain.CRing R) (Q : R)

theorem sub_succ_le (n k : Nat) (h : k + 1 ≤ n) : ∃ d, n - k = d + 1 ∧ n = d + 1 + k := by
  obtain ⟨d, hd⟩ := Cheb.le_dest2 (k + 1) n h
  refine ⟨d, ?_, by rw [← hd]; lib_nat⟩
  rw [← hd, show d + (k + 1) = (d + 1) + k by lib_nat]
  have h' := LibInt.nsub_add_add (d + 1) 0 k
  rw [Nat.zero_add] at h'; exact h'

theorem gb_zero : ∀ m, gb K Q m 0 = K.one
  | 0 => rfl
  | _ + 1 => rfl

theorem sym_rel : ∀ n k, K.mul (K.add K.one (K.neg (Frobenius.pw K Q (k + 1)))) (gb K Q n (k + 1)) =
    K.mul (K.add K.one (K.neg (Frobenius.pw K Q (n - k)))) (gb K Q n k)
  | 0, k => by
    show K.mul (K.add K.one (K.neg (Frobenius.pw K Q (k + 1)))) K.zero = K.mul (K.add K.one (K.neg (Frobenius.pw K Q (0 - k)))) (gb K Q 0 k)
    rw [LibInt.nzero_sub]
    show _ = K.mul (K.add K.one (K.neg K.one)) _
    lib_ring
  | n + 1, 0 => by
    show K.mul (K.add K.one (K.neg (K.mul K.one Q))) (K.add (gb K Q n 1) (K.mul (Frobenius.pw K Q n) (gb K Q n 0))) =
      K.mul (K.add K.one (K.neg (K.mul (Frobenius.pw K Q n) Q))) (gb K Q (n + 1) 0)
    rw [gb_zero, gb_zero]
    have ih : K.mul (K.add K.one (K.neg (K.mul K.one Q))) (gb K Q n 1) = K.mul (K.add K.one (K.neg (Frobenius.pw K Q n))) (gb K Q n 0) :=
      sym_rel n 0
    rw [gb_zero] at ih
    calc _ = K.add (K.mul (K.add K.one (K.neg (K.mul K.one Q))) (gb K Q n 1))
          (K.mul (K.add K.one (K.neg Q)) (Frobenius.pw K Q n)) := by lib_ring
      _ = _ := by rw [ih]; lib_ring
  | n + 1, k + 1 => by
    -- [n+1; k+2] = [n; k+2] + Q^(n−k−1)[n; k+1],  [n+1; k+1] = [n; k+1] + Q^(n−k)[n; k]
    show K.mul (K.add K.one (K.neg (Frobenius.pw K Q (k + 1 + 1)))) (K.add (gb K Q n (k + 1 + 1)) (K.mul (Frobenius.pw K Q (n - (k + 1))) (gb K Q n (k + 1)))) =
      K.mul (K.add K.one (K.neg (Frobenius.pw K Q (n + 1 - (k + 1))))) (K.add (gb K Q n (k + 1)) (K.mul (Frobenius.pw K Q (n - k)) (gb K Q n k)))
    have i1 := sym_rel n (k + 1)
    have i0 := sym_rel n k
    have hs : n + 1 - (k + 1) = n - k := LibInt.nsub_succ_succ n k
    rw [hs]
    cases Nat.lt_or_ge n (k + 1) with
    | inl h =>
      -- n ≤ k: [n; k+1] = [n; k+2] = 0
      rw [gb_big K Q n (k + 1) h, gb_big K Q n (k + 1 + 1) (Nat.lt_succ_of_lt h)]
      rw [gb_big K Q n (k + 1) h] at i0
      rw [show K.mul (K.add K.one (K.neg (Frobenius.pw K Q (n - k)))) (K.add K.zero (K.mul (Frobenius.pw K Q (n - k)) (gb K Q n k))) =
        K.mul (Frobenius.pw K Q (n - k)) (K.mul (K.add K.one (K.neg (Frobenius.pw K Q (n - k)))) (gb K Q n k)) by lib_ring, ← i0]
      lib_ring
    | inr h =>
      obtain ⟨d, hd1, hd2⟩ := sub_succ_le n k h
      have hd3 : n - (k + 1) = d := by
        rw [hd2, show d + 1 + k = (d + k) + 1 by lib_nat, LibInt.nsub_succ_succ]
        have h' := LibInt.nsub_add_add d 0 k
        rw [Nat.zero_add] at h'; exact h'
      rw [hd3] at i1 ⊢
      rw [hd1] at i0 ⊢
      have e2 : Frobenius.pw K Q (k + 1 + 1) = K.mul (K.mul (Frobenius.pw K Q k) Q) Q := rfl
      have e1 : Frobenius.pw K Q (k + 1) = K.mul (Frobenius.pw K Q k) Q := rfl
      have ed : Frobenius.pw K Q (d + 1) = K.mul (Frobenius.pw K Q d) Q := rfl
      rw [e2] at i1 ⊢
      rw [e1, ed] at i0
      rw [ed]
      generalize Frobenius.pw K Q k = A at i0 i1 ⊢
      generalize Frobenius.pw K Q d = B at i0 i1 ⊢
      generalize gb K Q n (k + 1 + 1) = X2 at i1 ⊢
      generalize gb K Q n (k + 1) = X1 at i0 i1 ⊢
      generalize gb K Q n k = X0 at i0 ⊢
      calc _ = K.add (K.mul (K.add K.one (K.neg (K.mul (K.mul A Q) Q))) X2) (K.mul B (K.mul (K.add K.one (K.neg (K.mul (K.mul A Q) Q))) X1)) := by lib_ring
        _ = K.add (K.mul (K.add K.one (K.neg B)) X1) (K.mul B (K.mul (K.add K.one (K.neg (K.mul (K.mul A Q) Q))) X1)) := by rw [i1]
        _ = K.add (K.mul (K.add K.one (K.neg (K.mul B Q))) X1) (K.mul (K.mul B Q) (K.mul (K.add K.one (K.neg (K.mul A Q))) X1)) := by lib_ring
        _ = K.add (K.mul (K.add K.one (K.neg (K.mul B Q))) X1) (K.mul (K.mul B Q) (K.mul (K.add K.one (K.neg (K.mul B Q))) X0)) := by rw [i0]
        _ = _ := by lib_ring

theorem pascalA (n k : Nat) : gb K Q (n + 1) (k + 1) = K.add (K.mul (Frobenius.pw K Q (k + 1)) (gb K Q n (k + 1))) (gb K Q n k) := by
  have hs := sym_rel K Q n k
  calc gb K Q (n + 1) (k + 1) = K.add (gb K Q n (k + 1)) (K.mul (Frobenius.pw K Q (n - k)) (gb K Q n k)) := rfl
    _ = K.add (K.add (K.mul (Frobenius.pw K Q (k + 1)) (gb K Q n (k + 1))) (gb K Q n k))
          (K.add (K.mul (K.add K.one (K.neg (Frobenius.pw K Q (k + 1)))) (gb K Q n (k + 1)))
            (K.neg (K.mul (K.add K.one (K.neg (Frobenius.pw K Q (n - k)))) (gb K Q n k)))) := by lib_ring
    _ = _ := by rw [hs]; lib_ring

theorem dpascal (n m : Nat) (h : m ≤ n) : gb K Q (n + 2) (m + 2) =
    K.add (K.add (K.mul (Frobenius.pw K Q (m + 2)) (gb K Q n (m + 2)))
      (K.mul (K.add K.one (Frobenius.pw K Q (n + 1))) (gb K Q n (m + 1))))
      (K.mul (Frobenius.pw K Q (n - m)) (gb K Q n m)) := by
  rw [show n + 2 = (n + 1) + 1 from rfl, show m + 2 = (m + 1) + 1 from rfl, pascalA]
  show K.add (K.mul (Frobenius.pw K Q (m + 1 + 1)) (K.add (gb K Q n (m + 1 + 1)) (K.mul (Frobenius.pw K Q (n - (m + 1))) (gb K Q n (m + 1)))))
    (K.add (gb K Q n (m + 1)) (K.mul (Frobenius.pw K Q (n - m)) (gb K Q n m))) = _
  cases Nat.lt_or_ge m n with
  | inl hl =>
    obtain ⟨d, hd1, hd2⟩ := sub_succ_le n m hl
    have hd3 : n - (m + 1) = d := by
      rw [hd2, show d + 1 + m = (d + m) + 1 by lib_nat, LibInt.nsub_succ_succ]
      have h' := LibInt.nsub_add_add d 0 m
      rw [Nat.zero_add] at h'; exact h'
    rw [hd3, hd1, hd2, show d + 1 + m + 1 = (m + 1 + 1) + d by lib_nat, HasseCM.pw_add' K Q (m + 1 + 1) d,
      show Frobenius.pw K Q (d + 1) = K.mul (Frobenius.pw K Q d) Q from rfl]
    generalize Frobenius.pw K Q (m + 1 + 1) = A
    generalize Frobenius.pw K Q d = B
    lib_ring
  | inr hg =>
    have e : m = n := Nat.le_antisymm h hg
    subst e
    rw [gb_big K Q m (m + 1) (Nat.lt_succ_self m), gb_big K Q m (m + 1 + 1) (Nat.lt_succ_of_lt (Nat.lt_succ_self m))]
    lib_ring

end QBinom

#print axioms QBinom.sub_succ_le
#print axioms QBinom.gb_zero
#print axioms QBinom.sym_rel
#print axioms QBinom.pascalA
#print axioms QBinom.dpascal

lang/QBinom3.lean

lean_all/lang/QBinom3.lean

/-
  lang/QBinom3 — T47, cycle 547: [n; k]_Q·(Q; Q)_k·(Q; Q)_(n−k) = (Q; Q)_n in any commutative ring.

    pp Q n       (Q; Q)_n = ∏_{i=1}^{n} (1 − Q^i)
    gb_self      [n; n] = 1
    gb_pp        k ≤ n ⇒ [n; k]·(Q; Q)_k·(Q; Q)_(n−k) = (Q; Q)_n
  With Q nilpotent ((Q; Q)_n constant for n ≥ T) this gives [n; k]·P = 1 for k, n − k ≥ T: the N → ∞ step of the triple product.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.FinJTP2

namespace QBinom

open FTAChain

variable {R : Type} (K : DetChain.CRing R) (Q : R)

def pp : Nat → R
  | 0 => K.one
  | n + 1 => K.mul (pp n) (K.add K.one (K.neg (Frobenius.pw K Q (n + 1))))

theorem gb_self : ∀ n, gb K Q n n = K.one
  | 0 => rfl
  | n + 1 => by
    show K.add (gb K Q n (n + 1)) (K.mul (Frobenius.pw K Q (n - n)) (gb K Q n n)) = K.one
    rw [gb_big K Q n (n + 1) (Nat.lt_succ_self n), gb_self n, show n - n = 0 from (sub_add_r n 0)]
    show K.add K.zero (K.mul K.one K.one) = K.one; lib_ring

theorem gb_pp : ∀ n k, k ≤ n → K.mul (K.mul (gb K Q n k) (pp K Q k)) (pp K Q (n - k)) = pp K Q n
  | n, 0, _ => by rw [gb_zero]; show K.mul (K.mul K.one K.one) (pp K Q n) = _; lib_ring
  | 0, k + 1, h => absurd h (Nat.not_succ_le_zero k)
  | n + 1, k + 1, h => by
    show K.mul (K.mul (K.add (gb K Q n (k + 1)) (K.mul (Frobenius.pw K Q (n - k)) (gb K Q n k)))
      (K.mul (pp K Q k) (K.add K.one (K.neg (Frobenius.pw K Q (k + 1)))))) (pp K Q (n + 1 - (k + 1))) =
      K.mul (pp K Q n) (K.add K.one (K.neg (Frobenius.pw K Q (n + 1))))
    rw [LibInt.nsub_succ_succ]
    have hkn : k ≤ n := Nat.le_of_succ_le_succ h
    have i0 := gb_pp n k hkn
    cases Nat.lt_or_ge k n with
    | inl hl =>
      obtain ⟨d, hd1, hd2⟩ := sub_succ_le n k hl
      have hd3 : n - (k + 1) = d := by
        rw [hd2, show d + 1 + k = (d + k) + 1 by lib_nat, LibInt.nsub_succ_succ, sub_add_l' d k]
      have i1 := gb_pp n (k + 1) hl
      rw [hd3] at i1
      rw [hd1] at i0 ⊢
      have epp : pp K Q (d + 1) = K.mul (pp K Q d) (K.add K.one (K.neg (Frobenius.pw K Q (d + 1)))) := rfl
      have ek : Frobenius.pw K Q (k + 1) = K.mul (Frobenius.pw K Q k) Q := rfl
      have ed : Frobenius.pw K Q (d + 1) = K.mul (Frobenius.pw K Q d) Q := rfl
      have en : Frobenius.pw K Q (n + 1) = K.mul (K.mul (Frobenius.pw K Q d) (Frobenius.pw K Q k)) (Frobenius.pw K Q (1 + 1)) := by
        rw [hd2, show d + 1 + k + 1 = (d + k) + (1 + 1) by lib_nat, HasseCM.pw_add', HasseCM.pw_add']
      have epk1 : pp K Q (k + 1) = K.mul (pp K Q k) (K.add K.one (K.neg (Frobenius.pw K Q (k + 1)))) := rfl
      rw [epk1] at i1
      rw [epp] at i0 ⊢
      rw [en, show Frobenius.pw K Q (1 + 1) = K.mul (K.mul K.one Q) Q from rfl]
      rw [ek] at i1 ⊢
      rw [ed] at i0 ⊢
      generalize pp K Q n = Pn at i0 i1 ⊢
      generalize gb K Q n (k + 1) = G1 at i1 ⊢
      generalize gb K Q n k = G0 at i0 ⊢
      generalize pp K Q k = Pk at i0 i1 ⊢
      generalize pp K Q d = Pd at i0 i1 ⊢
      generalize Frobenius.pw K Q k = A at i0 i1 ⊢
      generalize Frobenius.pw K Q d = B at i0 i1 ⊢
      calc _ = K.add (K.mul (K.mul (K.mul G1 (K.mul Pk (K.add K.one (K.neg (K.mul A Q))))) Pd) (K.add K.one (K.neg (K.mul B Q))))
            (K.mul (K.mul (K.mul B Q) (K.add K.one (K.neg (K.mul A Q)))) (K.mul (K.mul G0 Pk) (K.mul Pd (K.add K.one (K.neg (K.mul B Q)))))) := by lib_ring
        _ = _ := by rw [i1, i0]; lib_ring
    | inr hg =>
      have e : k = n := Nat.le_antisymm hkn hg
      subst e
      rw [gb_big K Q k (k + 1) (Nat.lt_succ_self k), gb_self, show k - k = 0 from sub_add_r k 0]
      show K.mul (K.mul (K.add K.zero (K.mul K.one K.one)) (K.mul (pp K Q k) (K.add K.one (K.neg (Frobenius.pw K Q (k + 1)))))) K.one = _
      lib_ring
where
  sub_add_l' (s m : Nat) : s + m - m = s := by
    have h := LibInt.nsub_add_add s 0 m
    rw [Nat.zero_add] at h; exact h

end QBinom

#print axioms QBinom.gb_self
#print axioms QBinom.gb_pp

lang/QChar.lean

lean_all/lang/QChar.lean

/-
  lang/QChar — T43, cycle 41: the quadratic character of 𝔽_p with integer values.

  p = 2m + 3 prime, ℤ/p = ring (2m + 2), χ(x) = 0, 1, −1 ∈ ℤ (x = 0, x^((p−1)/2) = 1, otherwise).
    chi_zero, chi_unit     χ(0) = 0;  x ≠ 0 ⇒ χ(x) = ±1 with χ(x) = 1 ⇔ x^((p−1)/2) = 1   (Euler.euler_pm)
    chi_mul                χ(xy) = χ(x) χ(y)
    chi_sq                 χ(y²) = 1 for y ≠ 0
    exists_nonres          some n has χ(n) = −1                                   (PowerSum.exists_nonroot)
    chi_sum                Σ_x χ(x) = 0                                           (reindex x ↦ n x)
  Integer-valued, so that character sums can be compared with integers (Jacobsthal sums, a_p = ±2u for y² = x³ − x).

  Lean 4 core only, proofs by hand (LibInt.intR, lib_int from lib_shared), no axioms.
-/
import lang.Euler
import lang.PowerSum
import lib.IntRing

namespace QChar

open FTAChain ZMod YMChain FiniteRecChain
open FiniteRecChain (ite_pos' ite_neg')

variable (m : Nat)

def chi (x : Zp (2 * m + 1 + 1)) : Int :=
  if x = (ring (2 * m + 1 + 1)).zero then 0 else if Frobenius.pw (ring (2 * m + 1 + 1)) x (m + 1) = (ring (2 * m + 1 + 1)).one then 1 else -1

theorem chi_zero : chi m (ring (2 * m + 1 + 1)).zero = 0 := ite_pos' _ _ rfl

theorem chi_one_of (x : Zp (2 * m + 1 + 1)) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (h : Frobenius.pw (ring (2 * m + 1 + 1)) x (m + 1) = (ring (2 * m + 1 + 1)).one) :
    chi m x = 1 := by
  unfold chi; rw [ite_neg' _ _ hx, ite_pos' _ _ h]

theorem chi_neg_of (x : Zp (2 * m + 1 + 1)) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (h : Frobenius.pw (ring (2 * m + 1 + 1)) x (m + 1) ≠ (ring (2 * m + 1 + 1)).one) :
    chi m x = -1 := by
  unfold chi; rw [ite_neg' _ _ hx, ite_neg' _ _ h]

theorem ne_pm : (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).one :=
  TwoPower.neg_one_ne_one (2 * m + 1) (Nat.le_add_left 1 _)

theorem chi_mul (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1)
    (x y : Zp (2 * m + 1 + 1)) : chi m ((ring (2 * m + 1 + 1)).mul x y) = chi m x * chi m y := by
  by_cases hx : x = (ring (2 * m + 1 + 1)).zero
  · rw [hx, DetChain.zero_mul', chi_zero]; lib_int
  by_cases hy : y = (ring (2 * m + 1 + 1)).zero
  · rw [hy, DetChain.mul_zero, chi_zero]; lib_int
  have hxy : (ring (2 * m + 1 + 1)).mul x y ≠ (ring (2 * m + 1 + 1)).zero := fun e => by
    cases nozd (2 * m + 1) hpr _ _ e with
    | inl h => exact hx h
    | inr h => exact hy h
  have hp : Frobenius.pw (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).mul x y) (m + 1) = (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) x (m + 1)) (Frobenius.pw (ring (2 * m + 1 + 1)) y (m + 1)) :=
    Euler.legendre_mul m x y
  cases Euler.euler_pm m hpr x hx with
  | inl ex =>
    cases Euler.euler_pm m hpr y hy with
    | inl ey =>
      rw [chi_one_of m x hx ex, chi_one_of m y hy ey, chi_one_of m _ hxy (by rw [hp, ex, ey, (ring (2 * m + 1 + 1)).one_mul])]; rfl
    | inr ey =>
      have ey' : Frobenius.pw (ring (2 * m + 1 + 1)) y (m + 1) ≠ (ring (2 * m + 1 + 1)).one := fun e => ne_pm m (ey.symm.trans e)
      rw [chi_one_of m x hx ex, chi_neg_of m y hy ey', chi_neg_of m _ hxy (by rw [hp, ex, ey, (ring (2 * m + 1 + 1)).one_mul]; exact ne_pm m)]; rfl
  | inr ex =>
    have ex' : Frobenius.pw (ring (2 * m + 1 + 1)) x (m + 1) ≠ (ring (2 * m + 1 + 1)).one := fun e => ne_pm m (ex.symm.trans e)
    cases Euler.euler_pm m hpr y hy with
    | inl ey =>
      rw [chi_neg_of m x hx ex', chi_one_of m y hy ey,
        chi_neg_of m _ hxy (by rw [hp, ex, ey, (ring (2 * m + 1 + 1)).mul_comm, (ring (2 * m + 1 + 1)).one_mul]; exact ne_pm m)]; rfl
    | inr ey =>
      have ey' : Frobenius.pw (ring (2 * m + 1 + 1)) y (m + 1) ≠ (ring (2 * m + 1 + 1)).one := fun e => ne_pm m (ey.symm.trans e)
      rw [chi_neg_of m x hx ex', chi_neg_of m y hy ey',
        chi_one_of m _ hxy (by rw [hp, ex, ey]; exact TwoPower.negone_sq' (2 * m + 1))]; rfl

theorem chi_sq (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (y : Zp (2 * m + 1 + 1))
    (hy : y ≠ (ring (2 * m + 1 + 1)).zero) : chi m ((ring (2 * m + 1 + 1)).mul y y) = 1 := by
  have hyy : (ring (2 * m + 1 + 1)).mul y y ≠ (ring (2 * m + 1 + 1)).zero := fun e => by
    cases nozd (2 * m + 1) hpr _ _ e with
    | inl h => exact hy h
    | inr h => exact hy h
  exact chi_one_of m _ hyy (Euler.euler_square m hpr y hy)

theorem exists_nonres (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) :
    ∃ n : Zp (2 * m + 1 + 1), n ≠ (ring (2 * m + 1 + 1)).zero ∧ chi m n = -1 :=
  match PowerSum.exists_nonroot (2 * m + 1) hpr m (by rw [Nat.two_mul]; exact Nat.succ_le_succ (Nat.le_add_left m m)) with
  | ⟨n, hn0, hn⟩ => ⟨n, hn0, chi_neg_of m n hn0 hn⟩

theorem int_two_zero (S : Int) (h : S + S = 0) : S = 0 := by
  have e : (2 : Int) * S = 0 := by rw [← h]; lib_int
  cases LibInt.int_noZD _ _ e with
  | inl h2 =>
    have h2' : Int.ofNat 2 = Int.ofNat 0 := h2
    exact absurd (Int.ofNat.inj h2') (fun h => Nat.noConfusion h)
  | inr h' => exact h'

/-- **Σ_x χ(x) = 0.** -/
theorem chi_sum (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) :
    sumK LibInt.intR (chi m) (CMInert.allZ (2 * m + 1 + 1) (2 * m + 1 + 1 + 1)) = 0 := by
  match exists_nonres m hpr with
  | ⟨n, hn0, hn⟩ =>
    have hre := reindex LibInt.intR _ (CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR) ((ring (2 * m + 1 + 1)).mul n)
      ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) n (2 * m + 1)))
      (fun x => by rw [← (ring (2 * m + 1 + 1)).mul_assoc, inv_exists (2 * m + 1) hpr n hn0, (ring (2 * m + 1 + 1)).one_mul])
      (fun y => by rw [← (ring (2 * m + 1 + 1)).mul_assoc, (ring (2 * m + 1 + 1)).mul_comm n, inv_exists (2 * m + 1) hpr n hn0, (ring (2 * m + 1 + 1)).one_mul]) (chi m)
    have e : sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).mul n x)) (CMInert.allZ (2 * m + 1 + 1) (2 * m + 1 + 1 + 1))
        = LibInt.intR.mul (-1) (sumK LibInt.intR (chi m) (CMInert.allZ (2 * m + 1 + 1) (2 * m + 1 + 1 + 1))) := by
      rw [mul_sumK]
      exact sumK_ext LibInt.intR _ _ (fun x => by rw [chi_mul m hpr, hn]; rfl) _
    rw [e] at hre
    apply int_two_zero
    have : LibInt.intR.mul (-1) (sumK LibInt.intR (chi m) (CMInert.allZ (2 * m + 1 + 1) (2 * m + 1 + 1 + 1)))
        + sumK LibInt.intR (chi m) (CMInert.allZ (2 * m + 1 + 1) (2 * m + 1 + 1 + 1))
        = 0 := by
      show -1 * sumK LibInt.intR (chi m) _ + sumK LibInt.intR (chi m) _ = 0
      lib_int
    rw [hre] at this
    exact this

end QChar

#print axioms QChar.chi_zero
#print axioms QChar.chi_one_of
#print axioms QChar.chi_neg_of
#print axioms QChar.ne_pm
#print axioms QChar.chi_mul
#print axioms QChar.chi_sq
#print axioms QChar.exists_nonres
#print axioms QChar.int_two_zero
#print axioms QChar.chi_sum

lang/Quad.lean

lean_all/lang/Quad.lean

/-
  lang/Quad — T43, cycle 63: the quadratic extension R[√n] of a commutative ring, and Frobenius on 𝔽_p[√n].

  For a commutative ring K and n ∈ K, pairs (a, b) = a + b√n form a commutative ring (quad K n):
    (a, b)(c, d) = (ac + n bd, ad + bc).
    quad                the ring structure (all laws componentwise, lib_ring)
    qconj, qnorm        conjugation a − b√n and norm a² − n b²
    norm_mul            N(xy) = N(x) N(y)
    conj_mul            conj(xy) = conj(x) conj(y)
    sqrt_pow            (√n)^(2j+1) = n^j √n
  For K = 𝔽_p and n a non-residue (n^((p−1)/2) = −1), 𝔽_p[√n] = 𝔽_{p²} and
    frob_sqrt           (√n)^p = −√n
  (with Frobenius.frob_add: (a + b√n)^p = a + b (√n)^p = conj(a + b√n)) — the Frobenius of 𝔽_{p²}/𝔽_p is conjugation,
  the GL1 Artin map of the unramified quadratic extension; point counts of curves over 𝔽_{p²} come next.

  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lib.RingTac
import lang.Frobenius

namespace Quad

variable {R : Type} (K : DetChain.CRing R) (n : R)

def qmul (x y : R × R) : R × R := (K.add (K.mul x.1 y.1) (K.mul n (K.mul x.2 y.2)), K.add (K.mul x.1 y.2) (K.mul x.2 y.1))

theorem qmul_assoc (a b c : R × R) : qmul K n (qmul K n a b) c = qmul K n a (qmul K n b c) := by
  apply Prod.ext
  · show K.add (K.mul (K.add (K.mul a.1 b.1) (K.mul n (K.mul a.2 b.2))) c.1) (K.mul n (K.mul (K.add (K.mul a.1 b.2) (K.mul a.2 b.1)) c.2)) = K.add (K.mul a.1 (K.add (K.mul b.1 c.1) (K.mul n (K.mul b.2 c.2)))) (K.mul n (K.mul a.2 (K.add (K.mul b.1 c.2) (K.mul b.2 c.1))))
    lib_ring
  · show K.add (K.mul (K.add (K.mul a.1 b.1) (K.mul n (K.mul a.2 b.2))) c.2) (K.mul (K.add (K.mul a.1 b.2) (K.mul a.2 b.1)) c.1) = K.add (K.mul a.1 (K.add (K.mul b.1 c.2) (K.mul b.2 c.1))) (K.mul a.2 (K.add (K.mul b.1 c.1) (K.mul n (K.mul b.2 c.2))))
    lib_ring

theorem qmul_comm (a b : R × R) : qmul K n a b = qmul K n b a := by
  apply Prod.ext
  · show K.add (K.mul a.1 b.1) (K.mul n (K.mul a.2 b.2)) = K.add (K.mul b.1 a.1) (K.mul n (K.mul b.2 a.2))
    lib_ring
  · show K.add (K.mul a.1 b.2) (K.mul a.2 b.1) = K.add (K.mul b.1 a.2) (K.mul b.2 a.1)
    lib_ring

theorem qone_mul (a : R × R) : qmul K n (K.one, K.zero) a = a := by
  apply Prod.ext
  · show K.add (K.mul K.one a.1) (K.mul n (K.mul K.zero a.2)) = a.1
    lib_ring
  · show K.add (K.mul K.one a.2) (K.mul K.zero a.1) = a.2
    lib_ring

theorem qdistrib (a b c : R × R) :
    qmul K n a (K.add b.1 c.1, K.add b.2 c.2) = (K.add (qmul K n a b).1 (qmul K n a c).1, K.add (qmul K n a b).2 (qmul K n a c).2) := by
  apply Prod.ext
  · show K.add (K.mul a.1 (K.add b.1 c.1)) (K.mul n (K.mul a.2 (K.add b.2 c.2))) = K.add (K.add (K.mul a.1 b.1) (K.mul n (K.mul a.2 b.2))) (K.add (K.mul a.1 c.1) (K.mul n (K.mul a.2 c.2)))
    lib_ring
  · show K.add (K.mul a.1 (K.add b.2 c.2)) (K.mul a.2 (K.add b.1 c.1)) = K.add (K.add (K.mul a.1 b.2) (K.mul a.2 b.1)) (K.add (K.mul a.1 c.2) (K.mul a.2 c.1))
    lib_ring

def quad : DetChain.CRing (R × R) where
  add := fun x y => (K.add x.1 y.1, K.add x.2 y.2)
  mul := qmul K n
  neg := fun x => (K.neg x.1, K.neg x.2)
  zero := (K.zero, K.zero)
  one := (K.one, K.zero)
  add_assoc := fun _ _ _ => Prod.ext (K.add_assoc _ _ _) (K.add_assoc _ _ _)
  add_comm := fun _ _ => Prod.ext (K.add_comm _ _) (K.add_comm _ _)
  zero_add := fun _ => Prod.ext (K.zero_add _) (K.zero_add _)
  add_neg := fun _ => Prod.ext (K.add_neg _) (K.add_neg _)
  mul_assoc := qmul_assoc K n
  mul_comm := qmul_comm K n
  one_mul := qone_mul K n
  distrib := qdistrib K n

def qconj (x : R × R) : R × R := (x.1, K.neg x.2)
def qnorm (x : R × R) : R := K.add (K.mul x.1 x.1) (K.neg (K.mul n (K.mul x.2 x.2)))

theorem norm_mul (x y : R × R) : qnorm K n ((quad K n).mul x y) = K.mul (qnorm K n x) (qnorm K n y) := by
  show K.add (K.mul (K.add (K.mul x.1 y.1) (K.mul n (K.mul x.2 y.2))) (K.add (K.mul x.1 y.1) (K.mul n (K.mul x.2 y.2))))
      (K.neg (K.mul n (K.mul (K.add (K.mul x.1 y.2) (K.mul x.2 y.1)) (K.add (K.mul x.1 y.2) (K.mul x.2 y.1)))))
    = K.mul (K.add (K.mul x.1 x.1) (K.neg (K.mul n (K.mul x.2 x.2)))) (K.add (K.mul y.1 y.1) (K.neg (K.mul n (K.mul y.2 y.2))))
  lib_ring

theorem conj_mul (x y : R × R) : qconj K ((quad K n).mul x y) = (quad K n).mul (qconj K x) (qconj K y) := by
  apply Prod.ext
  · show K.add (K.mul x.1 y.1) (K.mul n (K.mul x.2 y.2)) = K.add (K.mul x.1 y.1) (K.mul n (K.mul (K.neg x.2) (K.neg y.2)))
    lib_ring
  · show K.neg (K.add (K.mul x.1 y.2) (K.mul x.2 y.1)) = K.add (K.mul x.1 (K.neg y.2)) (K.mul (K.neg x.2) y.1)
    lib_ring

theorem mul_self_norm (x : R × R) : (quad K n).mul x (qconj K x) = (qnorm K n x, K.zero) := by
  apply Prod.ext
  · show K.add (K.mul x.1 x.1) (K.mul n (K.mul x.2 (K.neg x.2))) = K.add (K.mul x.1 x.1) (K.neg (K.mul n (K.mul x.2 x.2)))
    lib_ring
  · show K.add (K.mul x.1 (K.neg x.2)) (K.mul x.2 x.1) = K.zero
    lib_ring

/-- `(√n)^(2j+1) = n^j √n`. -/
theorem sqrt_pow : ∀ j, Frobenius.pw (quad K n) (K.zero, K.one) (2 * j + 1) = (K.zero, Frobenius.pw K n j)
  | 0 => by
    apply Prod.ext
    · show K.add (K.mul K.one K.zero) (K.mul n (K.mul K.zero K.one)) = K.zero
      lib_ring
    · show K.add (K.mul K.one K.one) (K.mul K.zero K.zero) = K.one
      lib_ring
  | j + 1 => by
    show qmul K n (qmul K n (Frobenius.pw (quad K n) (K.zero, K.one) (2 * j + 1)) (K.zero, K.one)) (K.zero, K.one) = _
    rw [sqrt_pow j]
    apply Prod.ext
    · show K.add (K.mul (K.add (K.mul K.zero K.zero) (K.mul n (K.mul (Frobenius.pw K n j) K.one))) K.zero) (K.mul n (K.mul (K.add (K.mul K.zero K.one) (K.mul (Frobenius.pw K n j) K.zero)) K.one)) = K.zero
      lib_ring
    · show K.add (K.mul (K.add (K.mul K.zero K.zero) (K.mul n (K.mul (Frobenius.pw K n j) K.one))) K.one) (K.mul (K.add (K.mul K.zero K.one) (K.mul (Frobenius.pw K n j) K.zero)) K.zero) = K.mul (Frobenius.pw K n j) n
      lib_ring

/-- **(√n)^p = −√n** when n^((p−1)/2) = −1 (p = 2j + 1). -/
theorem frob_sqrt (j : Nat) (hn : Frobenius.pw K n j = K.neg K.one) :
    Frobenius.pw (quad K n) (K.zero, K.one) (2 * j + 1) = (quad K n).neg (K.zero, K.one) := by
  rw [sqrt_pow, hn]
  apply Prod.ext
  · show K.zero = K.neg K.zero
    lib_ring
  · rfl

end Quad

#print axioms Quad.quad
#print axioms Quad.qmul_assoc
#print axioms Quad.qmul_comm
#print axioms Quad.qone_mul
#print axioms Quad.qdistrib
#print axioms Quad.norm_mul
#print axioms Quad.conj_mul
#print axioms Quad.mul_self_norm
#print axioms Quad.sqrt_pow
#print axioms Quad.frob_sqrt

lang/QuadAll.lean

lean_all/lang/QuadAll.lean

/-
  lang/QuadAll — T43, cycle 36: Artin reciprocity for every quadratic field ℚ(√a), a ≥ 1.

  For odd primes p, p' not dividing a with p' ≡ p (mod 4a):  (a/p') = (a/p).
    leg_one        (1/p) = 1
    two_period     (2/p) depends only on p mod 8           (TwoSupp.two_sq_iff)
    odd_prime_form a prime q ≠ 2 is g + g + 1
    leg_period     (a/p) depends only on p mod 4a          (strong induction on a: least prime factor q, a = q a',
                   LegMul.legB_mul, QuadPeriod.legB_mod_4q for odd q, two_period for q = 2)
  So p ↦ (a/p) is a Dirichlet character mod 4a: the Frobenius of p in ℚ(√a)/ℚ depends only on p mod 4a — GL1
  reciprocity for all real quadratic fields (with GaussSplit for −1, all quadratic fields ℚ(√±a)).

  Lean 4 core only, proofs by hand (lib/Semi's lib_nat for Nat identities), no axioms.
-/
import lang.LegMul
import lang.TwoSupp
import lib.Semi

namespace QuadAll

open FTAChain QRChain

theorem bool_eq {a b : Bool} (h1 : a = true → b = true) (h2 : b = true → a = true) : a = b := by
  cases a <;> cases b
  · rfl
  · exact absurd (h2 rfl) (fun e => Bool.noConfusion e)
  · exact absurd (h1 rfl) (fun e => Bool.noConfusion e)
  · rfl

theorem leg_one (h : Nat) : legB (h + h + 1) 1 = true :=
  (legB_iff (h + h) 1).mpr ⟨1, rfl⟩

/-- **(2/p) depends only on p mod 8.** -/
theorem two_period (h K : Nat) (h1 : 1 ≤ h) (hp : Pr (h + h + 1)) (hp' : Pr ((h + K * 4) + (h + K * 4) + 1)) :
    legB ((h + K * 4) + (h + K * 4) + 1) 2 = legB (h + h + 1) 2 := by
  match (par_split h).1, (par_split h).2 with
  | f0, f1 =>
    -- h = n + (n + r)
    have rep : ∃ n r, r ≤ 1 ∧ h = n + (n + r) := by
      cases hpar : par h with
      | false => match f0 hpar with | ⟨k, hk⟩ => exact ⟨k, 0, Nat.zero_le 1, hk⟩
      | true => match f1 hpar with | ⟨k, hk⟩ => exact ⟨k, 1, Nat.le_refl 1, by rw [hk]; lib_nat⟩
    match rep with
    | ⟨n, r, hr, hn⟩ =>
      have hn' : h + K * 4 = (n + K * 2) + ((n + K * 2) + r) := by rw [hn]; lib_nat
      have i1 := TwoSupp.two_sq_iff n r hr (hn ▸ h1) (hn ▸ hp)
      have i2 := TwoSupp.two_sq_iff (n + K * 2) r hr (hn' ▸ Nat.le_trans h1 (Nat.le_add_right h _)) (hn' ▸ hp')
      have hpar : par (n + K * 2 + r) = par (n + r) := by
        rw [show n + K * 2 + r = (n + r) + (K + K) by lib_nat, par_add, par_double]
        cases par (n + r) <;> rfl
      rw [hpar] at i2
      apply bool_eq
      · intro t
        apply (legB_iff (h + h) 2).mpr
        have a1 := (legB_iff ((h + K * 4) + (h + K * 4)) 2).mp t
        rw [hn'] at a1
        have := i1.mpr (i2.mp a1)
        rw [← hn] at this
        exact this
      · intro t
        apply (legB_iff ((h + K * 4) + (h + K * 4)) 2).mpr
        have a1 := (legB_iff (h + h) 2).mp t
        rw [hn] at a1
        have := i2.mpr (i1.mp a1)
        rw [← hn'] at this
        exact this

theorem odd_prime_form (l : Nat) (hq : ∀ d, Dv d (l + 1 + 1 + 1) → d = 1 ∨ d = l + 1 + 1 + 1) :
    ∃ g, l + 1 + 1 + 1 = g + g + 1 := by
  cases hpar : par (l + 1 + 1 + 1) with
  | true => match (par_split _).2 hpar with | ⟨g, hg⟩ => exact ⟨g, hg⟩
  | false =>
    exfalso
    match (par_split _).1 hpar with
    | ⟨k, hk⟩ =>
      cases hq 2 ⟨k, by rw [hk]; lib_nat⟩ with
      | inl e => exact Nat.noConfusion (Nat.succ.inj e)
      | inr e =>
        have : l + 1 = 0 := Nat.succ.inj (Nat.succ.inj e.symm)
        exact Nat.noConfusion this

theorem not_dv_of_mul (p a b : Nat) (h : ¬ Dv p (a * b)) : ¬ Dv p a := fun ⟨c, e⟩ =>
  h ⟨c * b, by rw [e]; lib_nat⟩

/-- **(a/p) depends only on p mod 4a.** -/
theorem leg_period : ∀ F a, a ≤ F → 1 ≤ a → ∀ h h' k, Pr (h + h + 1) → Pr (h' + h' + 1) →
    ¬ Dv (h + h + 1) a → ¬ Dv (h' + h' + 1) a → h' + h' + 1 = h + h + 1 + 4 * a * k →
    legB (h' + h' + 1) a = legB (h + h + 1) a
  | _, 0, _, h1, _, _, _, _, _, _, _, _ => absurd h1 (Nat.not_succ_le_zero 0)
  | _, 1, _, _, h, h', _, _, _, _, _, _ => by rw [leg_one, leg_one]
  | 0, i + 2, hF, _, _, _, _, _, _, _, _, _ => absurd hF (Nat.not_succ_le_zero _)
  | F + 1, i + 2, hF, _, h, h', k, hp, hp', ha, ha', e => by
    match lp_exists i with
    | ⟨l, ⟨a', ea⟩, hmin⟩ =>
      have hq := lp_prime _ l ⟨a', ea⟩ hmin
      -- i + 2 = a' q
      have ha1 : 1 ≤ a' := by
        cases a' with
        | zero => rw [Nat.zero_mul] at ea; exact absurd ea (fun e => Nat.noConfusion e)
        | succ _ => exact Nat.succ_le_succ (Nat.zero_le _)
      have hlt : a' < i + 2 := by
        rw [ea]
        have b : a' < a' * 2 := by rw [Nat.mul_two]; exact Nat.lt_add_of_pos_right ha1
        exact Nat.lt_of_lt_of_le b (Nat.mul_le_mul_left a' (Nat.succ_le_succ (Nat.succ_le_succ (Nat.zero_le l))))
      have eq' : i + 2 = (l + 1 + 1) * a' := by rw [ea, nmul_comm]
      have hpq : ¬ Dv (h + h + 1) (l + 1 + 1) := not_dv_of_mul _ _ _ (eq' ▸ ha)
      have hpa : ¬ Dv (h + h + 1) a' := not_dv_of_mul _ _ _ ((show i + 2 = a' * (l + 1 + 1) from ea) ▸ ha)
      have hpq' : ¬ Dv (h' + h' + 1) (l + 1 + 1) := not_dv_of_mul _ _ _ (eq' ▸ ha')
      have hpa' : ¬ Dv (h' + h' + 1) a' := not_dv_of_mul _ _ _ ((show i + 2 = a' * (l + 1 + 1) from ea) ▸ ha')
      have ih := leg_period F a' (Nat.le_of_lt_succ (Nat.lt_of_lt_of_le hlt hF)) ha1 h h' ((l + 1 + 1) * k) hp hp' hpa hpa'
        (by rw [e, ea]; lib_nat)
      have hqq : legB (h' + h' + 1) (l + 1 + 1) = legB (h + h + 1) (l + 1 + 1) := by
        cases l with
        | zero =>
          -- q = 2: h' = h + 4 a' k
          have e2 : h' = h + (a' * k) * 4 := QuadPeriod.double_inj _ _ (by
            have e3 : h' + h' + 1 = (h + (a' * k) * 4) + (h + (a' * k) * 4) + 1 := by rw [e, ea]; lib_nat
            exact Nat.succ.inj e3)
          rw [e2] at hp' ⊢
          exact two_period h (a' * k) (h_pos h hp) hp hp'
        | succ l' =>
          match odd_prime_form l' hq with
          | ⟨g, hg⟩ =>
            rw [hg]
            have hne : h ≠ g := fun hh => hpq (by rw [hg, hh]; exact dv_refl _)
            have hne' : h' ≠ g := fun hh => hpq' (by rw [hg, hh]; exact dv_refl _)
            rw [hg] at hq
            exact QuadPeriod.legB_mod_4q h h' g (a' * k) hp hp' ⟨⟨l' + 1, hg.symm⟩, hq⟩ hne hne' (by rw [e, ea, hg]; lib_nat)
      rw [eq', LegMul.legB_mul h' _ _ hp' hpq' hpa', LegMul.legB_mul h _ _ hp hpq hpa, hqq, ih]

end QuadAll

#print axioms QuadAll.bool_eq
#print axioms QuadAll.leg_one
#print axioms QuadAll.two_period
#print axioms QuadAll.odd_prime_form
#print axioms QuadAll.not_dv_of_mul
#print axioms QuadAll.leg_period

lang/QuadArtin.lean

lean_all/lang/QuadArtin.lean

/-
  lang/QuadArtin — T43, cycle 20 (GL1 over ℚ for the quadratic field ℚ(√q*)): the Artin symbol is a Dirichlet
  character modulo q.

  p = h + h + 1, q = g + g + 1 distinct odd primes. The Frobenius of p in Gal(ℚ(√q*)/ℚ), q* = (−1)^g q, is
      F(p) = (q/p) · (−1/p)^g = (q/p) · (−1)^(h g)          (Euler: (−1/p) = (−1)^h)
  written with Booleans as bx (legB p q) (par (h g)). From quadratic reciprocity (QRChain.qr_law):
    artin_symbol    F(p) = (p/q)
    legB_periodic   (p/q) depends only on p mod q
    artin_periodic  p ≡ p' (mod q) ⇒ F(p) = F(p')
  So the eigen-system p ↦ F(p) is fixed by place ∘ read of Recip1 with N = q: GL1 reciprocity for ℚ(√q*).

  Lean 4 core only, proofs by hand, no axioms.
-/
import QRChain

namespace QuadArtin

open FTAChain FrobChain QRChain

theorem bx_solve (a b c : Bool) (h : bx a b = c) : bx a c = b := by
  cases a <;> cases b <;> cases c <;> first | rfl | exact Bool.noConfusion h

/-- **The Artin symbol of p in ℚ(√q*) is (p/q).** -/
theorem artin_symbol (h g : Nat) (hp : Pr (h + h + 1)) (hq : Pr (g + g + 1)) (hne : h ≠ g) :
    bx (legB (h + h + 1) (g + g + 1)) (par (h * g)) = legB (g + g + 1) (h + h + 1) :=
  bx_solve _ _ _ (qr_law h g hp hq hne)

theorem bool_eq_of_iff (a b : Bool) (h : a = true ↔ b = true) : a = b := by
  cases a <;> cases b
  · rfl
  · exact absurd (h.mpr rfl) (fun e => Bool.noConfusion e)
  · exact absurd (h.mp rfl) (fun e => Bool.noConfusion e)
  · rfl

/-- **The Legendre symbol (p/q) depends only on p mod q.** -/
theorem legB_periodic (g a k : Nat) : legB (g + g + 1) (a + (g + g + 1) * k) = legB (g + g + 1) a := by
  have hr : rem (g + g + 1) (a + (g + g + 1) * k) = rem (g + g + 1) a :=
    rem_add_dv (g + g) a _ ⟨k, nmul_comm _ _⟩
  apply bool_eq_of_iff
  exact ⟨fun h1 => (legB_iff (g + g) a).mpr (match (legB_iff (g + g) _).mp h1 with
          | ⟨x, hx⟩ => ⟨x, hx.trans hr⟩),
         fun h2 => (legB_iff (g + g) _).mpr (match (legB_iff (g + g) a).mp h2 with
          | ⟨x, hx⟩ => ⟨x, hx.trans hr.symm⟩)⟩

/-- **GL1 reciprocity for ℚ(√q*)**: primes congruent mod q have the same Frobenius. -/
theorem artin_periodic (h h' g k : Nat) (hp : Pr (h + h + 1)) (hp' : Pr (h' + h' + 1)) (hq : Pr (g + g + 1))
    (hne : h ≠ g) (hne' : h' ≠ g) (hcong : h' + h' + 1 = h + h + 1 + (g + g + 1) * k) :
    bx (legB (h' + h' + 1) (g + g + 1)) (par (h' * g)) = bx (legB (h + h + 1) (g + g + 1)) (par (h * g)) := by
  rw [artin_symbol h' g hp' hq hne', artin_symbol h g hp hq hne, hcong, legB_periodic]

end QuadArtin

#print axioms QuadArtin.bx_solve
#print axioms QuadArtin.artin_symbol
#print axioms QuadArtin.bool_eq_of_iff
#print axioms QuadArtin.legB_periodic
#print axioms QuadArtin.artin_periodic

lang/QuadDiagonal.lean

lean_all/lang/QuadDiagonal.lean

/-
  lang/QuadDiagonal — T43, cycle 255: the quadrics x₁² + … + x_k² = c over 𝔽_p for every odd prime p and every k.

  p = 2m + 3, χ the Legendre symbol (QChar, integer values), e = χ(−1). #{x² = a} = 1 + χ(a) (sq_count), and
  Σ_a χ(a)χ(c − a) = −e for c ≠ 0 (ConicSum.sum_xxd), so N_{k+1}(c) = Σ_x N_k(c − x²) keeps the shape:
      N_k(c) = A_k + B_k χ(c)  (c ≠ 0),   N_k(0) = C_k,
      A' = (p − 1)A − eB + C,   B' = C − A,   C' = (p − 1)(A + eB) + C,     A_0 = B_0 = 0, C_0 = 1.
      quad_diagonal   for every k (Gauss–Jordan: the number of representations by a sum of k squares over 𝔽_p)
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.ConicSum
import lang.DLog
import lang.PrimRoot
import lang.GaussSum

namespace QuadDiagonal

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius QChar ConicSum
open FiniteRecChain (ite_pos' ite_neg')

variable (m : Nat)

theorem sq_root (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a : Zp (2 * m + 1 + 1)) (ha0 : a ≠ (ring (2 * m + 1 + 1)).zero) (ha : pw (ring (2 * m + 1 + 1)) a (m + 1) = (ring (2 * m + 1 + 1)).one) : ∃ c, (ring (2 * m + 1 + 1)).mul c c = a := by
  match PrimRoot.primitive_root (2 * m + 1) hpr with
  | ⟨g, hg⟩ =>
    match DLog.mem_pows (2 * m + 1) g a _ (DLog.every_unit (2 * m + 1) hpr g hg a ha0) with
    | ⟨k, _, hk⟩ =>
      have h1 : pw (ring (2 * m + 1 + 1)) g (k * (m + 1)) = (ring (2 * m + 1 + 1)).one := by rw [← TwoPower.pwpow, ← hk, ha]
      match hg.2 _ h1 with
      | ⟨c, hc⟩ =>
        have h3 : k = 2 * c := by
          apply Nat.eq_of_mul_eq_mul_right (Nat.zero_lt_succ m)
          rw [hc]; lib_nat
        refine ⟨pw (ring (2 * m + 1 + 1)) g c, ?_⟩
        rw [hk, h3, show 2 * c = c * 2 by lib_nat, ← TwoPower.pwpow]
        show _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one _) _
        lib_ring

theorem sI_add {A : Type} (F G : A → Int) (l : List A) : sumK LibInt.intR (fun x => F x + G x) l = sumK LibInt.intR F l + sumK LibInt.intR G l := sumK_add LibInt.intR F G l
theorem sI_mul {A : Type} (c : Int) (F : A → Int) (l : List A) : sumK LibInt.intR (fun x => c * F x) l = c * sumK LibInt.intR F l := (mul_sumK LibInt.intR c F l).symm
theorem sI_mulr {A : Type} (c : Int) (F : A → Int) (l : List A) : sumK LibInt.intR (fun x => F x * c) l = sumK LibInt.intR F l * c := (sumK_mul LibInt.intR c F l).symm
theorem zsum {A : Type} : ∀ l : List A, sumK LibInt.intR (fun _ => (0 : Int)) l = 0
  | [] => rfl
  | _ :: as => by show 0 + sumK LibInt.intR (fun _ => (0 : Int)) as = 0; rw [zsum as]; rfl

theorem two_ne : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero := fun e => ne_pm m (by
  calc (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) := by rw [e]; lib_ring
    _ = (ring (2 * m + 1 + 1)).one := by lib_ring)

theorem nozd' (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1)) (h : (ring (2 * m + 1 + 1)).mul x y = (ring (2 * m + 1 + 1)).zero) : x = (ring (2 * m + 1 + 1)).zero ∨ y = (ring (2 * m + 1 + 1)).zero := nozd (2 * m + 1) hpr x y h

theorem two_roots (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (c : Zp (2 * m + 1 + 1)) (hc : c ≠ (ring (2 * m + 1 + 1)).zero) (x : Zp (2 * m + 1 + 1)) :
    delta LibInt.intR ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul c c) = delta LibInt.intR x c + delta LibInt.intR x ((ring (2 * m + 1 + 1)).neg c) := by
  have d : c ≠ (ring (2 * m + 1 + 1)).neg c := fun e => hc (by
    have e2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) c = (ring (2 * m + 1 + 1)).zero := by
      calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) c = (ring (2 * m + 1 + 1)).add c c := by lib_ring
        _ = (ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg c) := congrArg ((ring (2 * m + 1 + 1)).add c) e
        _ = (ring (2 * m + 1 + 1)).zero := by lib_ring
    cases nozd' m hpr _ _ e2 with
    | inl h => exact absurd h (two_ne m)
    | inr h => exact h)
  unfold delta
  by_cases hx : (ring (2 * m + 1 + 1)).mul x x = (ring (2 * m + 1 + 1)).mul c c
  · rw [ite_pos' _ _ hx]
    have e : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg c)) ((ring (2 * m + 1 + 1)).add x c) = (ring (2 * m + 1 + 1)).zero := by
      rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg c)) ((ring (2 * m + 1 + 1)).add x c) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul c c)) by lib_ring, hx]; lib_ring
    cases nozd' m hpr _ _ e with
    | inl h =>
      have ex : x = c := by calc x = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg c)) c := by lib_ring
                                _ = c := by rw [h]; lib_ring
      rw [ite_pos' _ _ ex, ite_neg' _ _ (by rw [ex]; exact d)]; rfl
    | inr h =>
      have ex : x = (ring (2 * m + 1 + 1)).neg c := by calc x = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add x c) ((ring (2 * m + 1 + 1)).neg c) := by lib_ring
                                      _ = (ring (2 * m + 1 + 1)).neg c := by rw [h]; lib_ring
      rw [ite_neg' _ _ (by rw [ex]; exact fun e => d e.symm), ite_pos' _ _ ex]; rfl
  · rw [ite_neg' _ _ hx, ite_neg' _ _ (fun e => hx (by rw [e])), ite_neg' _ _ (fun e => hx (by rw [e]; lib_ring))]; rfl

/-- **#{x² = a} = 1 + χ(a).** -/
theorem sq_count (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a : Zp (2 * m + 1 + 1)) : sumK LibInt.intR (fun x => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul x x) a) (L m) = 1 + chi m a := by
  have hcomp : ∀ z, sumK LibInt.intR (fun x => delta LibInt.intR x z) (L m) = 1 := fun z => CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR z
  by_cases ha : a = (ring (2 * m + 1 + 1)).zero
  · rw [ha, sumK_ext LibInt.intR _ (fun x => delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) (fun x => by
      unfold delta
      by_cases hx : x = (ring (2 * m + 1 + 1)).zero
      · rw [ite_pos' _ _ hx, ite_pos' _ _ (by rw [hx]; lib_ring)]
      · rw [ite_neg' _ _ hx, ite_neg' _ _ (fun e => hx (by
          cases nozd' m hpr _ _ e with
          | inl h => exact h
          | inr h => exact h))]), hcomp, chi_zero]; rfl
  · cases Euler.euler_pm m hpr a ha with
    | inl ep =>
      match sq_root m hpr a ha ep with
      | ⟨c, hc⟩ =>
        have hc0 : c ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => ha (by rw [← hc, e0]; lib_ring)
        rw [← hc, sumK_ext LibInt.intR _ _ (fun x => two_roots m hpr c hc0 x), sI_add, hcomp, hcomp, hc, chi_one_of m a ha ep]
    | inr en =>
      have en' : pw (ring (2 * m + 1 + 1)) a (m + 1) ≠ (ring (2 * m + 1 + 1)).one := by rw [en]; exact ne_pm m
      have hz : ∀ x, delta LibInt.intR ((ring (2 * m + 1 + 1)).mul x x) a = 0 := fun x => by
        unfold delta
        rw [ite_neg' _ _ (fun ex => by
          have hx0 : x ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => ha (by rw [← ex, e0]; lib_ring)
          have h1 := chi_sq m hpr x hx0
          rw [ex, chi_neg_of m a ha en'] at h1
          exact Int.noConfusion h1)]; rfl
      rw [sumK_ext LibInt.intR _ _ hz, zsum, chi_neg_of m a ha en']; rfl

/-- N_k(c) = #{x₁² + … + x_k² = c}. -/
def Nk : Nat → Zp (2 * m + 1 + 1) → Int
  | 0, c => delta LibInt.intR c (ring (2 * m + 1 + 1)).zero
  | k + 1, c => sumK LibInt.intR (fun x => Nk k ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x)))) (L m)

theorem hcomp : Complete LibInt.intR (L m) := CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR

theorem conv (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (F : Zp (2 * m + 1 + 1) → Int) : sumK LibInt.intR (fun x => F ((ring (2 * m + 1 + 1)).mul x x)) (L m) = sumK LibInt.intR (fun a => (1 + chi m a) * F a) (L m) := by
  rw [sumK_ext LibInt.intR _ (fun x => sumK LibInt.intR (fun a => delta LibInt.intR a ((ring (2 * m + 1 + 1)).mul x x) * F a) (L m)) (fun x => (FiniteRecChain.pick LibInt.intR (L m) (hcomp m) F _).symm), sumK_swap]
  exact sumK_ext LibInt.intR _ _ (fun a => by
    rw [← sq_count m hpr a, ← sI_mulr]
    exact sumK_ext LibInt.intR _ _ (fun x => by rw [delta_symm]) _) _

theorem e_val (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = 1 ∨ chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = -1 := by
  have h : (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero := fun e => PowerSum.one_ne_zero' (2 * m + 1) (by
    calc (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).zero := by rw [e]
      _ = (ring (2 * m + 1 + 1)).zero := by lib_ring)
  cases Euler.euler_pm m hpr _ h with
  | inl ep => exact Or.inl (chi_one_of m _ h ep)
  | inr en => exact Or.inr (chi_neg_of m _ h (by rw [en]; exact ne_pm m))


theorem T1 : sumK LibInt.intR (fun _ : Zp (2 * m + 1 + 1) => (1 : Int)) (L m) = (Int.ofNat (2 * m + 1 + 1 + 1)) := GaussSum.sum_one m _

theorem S0 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : sumK LibInt.intR (chi m) (L m) = 0 := chi_sum m hpr

theorem S1 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (c : Zp (2 * m + 1 + 1)) : sumK LibInt.intR (fun a => chi m ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a))) (L m) = 0 := by
  rw [FiniteRecChain.reindex LibInt.intR (L m) (hcomp m) (fun a => (ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a)) (fun a => (ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a)) (fun x => by lib_ring) (fun y => by lib_ring) (chi m)]
  exact S0 m hpr

theorem S2 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (c : Zp (2 * m + 1 + 1)) (hc : c ≠ (ring (2 * m + 1 + 1)).zero) : sumK LibInt.intR (fun a => chi m a * chi m ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a))) (L m) = -((chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))) := by
  have hnc : (ring (2 * m + 1 + 1)).neg c ≠ (ring (2 * m + 1 + 1)).zero := fun h => hc (by calc c = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).neg c) := by lib_ring
                                                        _ = (ring (2 * m + 1 + 1)).zero := by rw [h]; lib_ring)
  rw [sumK_ext LibInt.intR _ (fun a => (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * chi m ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg c)))) (fun a => by
    rw [show (ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg c)) by lib_ring, chi_mul m hpr, chi_mul m hpr]; lib_int),
    sI_mul, sum_xxd m hpr _ hnc]; lib_int

theorem sqchi (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a : Zp (2 * m + 1 + 1)) : chi m a * chi m a = 1 + -(delta LibInt.intR a (ring (2 * m + 1 + 1)).zero) := by
  unfold delta
  by_cases ha : a = (ring (2 * m + 1 + 1)).zero
  · rw [ha, chi_zero, ite_pos' _ _ rfl]; rfl
  · rw [ite_neg' _ _ ha]
    cases Euler.euler_pm m hpr a ha with
    | inl ep => rw [chi_one_of m a ha ep]; rfl
    | inr en => rw [chi_neg_of m a ha (by rw [en]; exact ne_pm m)]; rfl

theorem S3 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : sumK LibInt.intR (fun a => chi m a * chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).neg a))) (L m) = (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * ((Int.ofNat (2 * m + 1 + 1 + 1)) + -1) := by
  rw [sumK_ext LibInt.intR _ (fun a => (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (1 + -(delta LibInt.intR a (ring (2 * m + 1 + 1)).zero))) (fun a => by
    rw [show (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).neg a) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) a by lib_ring, chi_mul m hpr, ← sqchi m hpr a]; lib_int),
    sI_mul, sI_add, T1 m, show sumK LibInt.intR (fun a => -(delta LibInt.intR a (ring (2 * m + 1 + 1)).zero)) (L m) = -1 by
      rw [sumK_ext LibInt.intR _ (fun a => -1 * delta LibInt.intR a (ring (2 * m + 1 + 1)).zero) (fun a => by lib_int), sI_mul, hcomp m]; rfl]

theorem Psum (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : sumK LibInt.intR (fun a => 1 + chi m a) (L m) = (Int.ofNat (2 * m + 1 + 1 + 1)) := by
  rw [sI_add, T1 m, S0 m hpr]; lib_int

/-- **One step**. -/
theorem step (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (k : Nat) (A B Cc : Int) (hne : ∀ c, c ≠ (ring (2 * m + 1 + 1)).zero → Nk m k c = A + B * chi m c) (h0 : Nk m k (ring (2 * m + 1 + 1)).zero = Cc) :
    (∀ c, c ≠ (ring (2 * m + 1 + 1)).zero → Nk m (k + 1) c = ((Int.ofNat (2 * m + 1 + 1 + 1)) * A + -((chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * B) + Cc + -A) + (Cc + -A) * chi m c) ∧
    Nk m (k + 1) (ring (2 * m + 1 + 1)).zero = (Int.ofNat (2 * m + 1 + 1 + 1)) * A + (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * B * ((Int.ofNat (2 * m + 1 + 1 + 1)) + -1) + Cc + -A := by
  have split : ∀ c a, Nk m k ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a)) = A + B * chi m ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a)) + delta LibInt.intR a c * (Cc + -A) := fun c a => by
    by_cases ha : a = c
    · rw [ha, show (ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg c) = (ring (2 * m + 1 + 1)).zero by lib_ring, h0, chi_zero, show delta LibInt.intR c c = 1 from ite_pos' _ _ rfl]; lib_int
    · have h1 : (ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a) ≠ (ring (2 * m + 1 + 1)).zero := fun h => ha (by calc a = (ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a))) := by lib_ring
                                                                    _ = c := by rw [h]; lib_ring)
      rw [hne _ h1, show delta LibInt.intR a c = 0 from ite_neg' _ _ ha]; lib_int
  have gen : ∀ c, Nk m (k + 1) c = A * (Int.ofNat (2 * m + 1 + 1 + 1)) + B * (sumK LibInt.intR (fun a => chi m ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a))) (L m) + sumK LibInt.intR (fun a => chi m a * chi m ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a))) (L m))
      + (1 + chi m c) * (Cc + -A) := fun c => by
    show sumK LibInt.intR (fun x => Nk m k ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x)))) (L m) = _
    rw [conv m hpr (fun a => Nk m k ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a)))]
    rw [sumK_ext LibInt.intR _ (fun a => A * (1 + chi m a) + B * (chi m ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a)) + chi m a * chi m ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a))) + delta LibInt.intR a c * ((1 + chi m a) * (Cc + -A))) (fun a => by
      rw [split c a]; lib_int)]
    have hpk : sumK LibInt.intR (fun a => delta LibInt.intR a c * ((1 + chi m a) * (Cc + -A))) (L m) = (1 + chi m c) * (Cc + -A) :=
      FiniteRecChain.pick LibInt.intR (L m) (hcomp m) (fun a => (1 + chi m a) * (Cc + -A)) c
    rw [sI_add, sI_add, sI_mul, sI_mul, sI_add, sI_add, T1 m, S0 m hpr, hpk]
    lib_int
  constructor
  · intro c hc
    rw [gen c, S1 m hpr c, S2 m hpr c hc]; lib_int
  · rw [gen (ring (2 * m + 1 + 1)).zero, S1 m hpr (ring (2 * m + 1 + 1)).zero, S3 m hpr, chi_zero]; lib_int

def Sq : Nat → Int × Int × Int
  | 0 => (0, 0, 1)
  | k + 1 => ((Int.ofNat (2 * m + 1 + 1 + 1)) * (Sq k).1 + -((chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (Sq k).2.1) + (Sq k).2.2 + -(Sq k).1, (Sq k).2.2 + -(Sq k).1,
              (Int.ofNat (2 * m + 1 + 1 + 1)) * (Sq k).1 + (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (Sq k).2.1 * ((Int.ofNat (2 * m + 1 + 1 + 1)) + -1) + (Sq k).2.2 + -(Sq k).1)

/-- **Sums of k squares over 𝔽_p, every k.** -/
theorem quad_diagonal (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : ∀ k : Nat,
    (∀ c, c ≠ (ring (2 * m + 1 + 1)).zero → Nk m k c = (Sq m k).1 + (Sq m k).2.1 * chi m c) ∧ Nk m k (ring (2 * m + 1 + 1)).zero = (Sq m k).2.2
  | 0 => by
    constructor
    · intro c hc; show delta LibInt.intR c (ring (2 * m + 1 + 1)).zero = 0 + 0 * chi m c; rw [show delta LibInt.intR c (ring (2 * m + 1 + 1)).zero = 0 from ite_neg' _ _ hc]; lib_int
    · show delta LibInt.intR (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero = 1; unfold delta; exact ite_pos' _ _ rfl
  | k + 1 => by
    have ih := quad_diagonal hpr k
    exact step m hpr k _ _ _ ih.1 ih.2

/-! ## diagonal forms with coefficients -/

/-- N_as(c) = #{Σ a_i x_i² = c} for a list of coefficients. -/
def Nf : List (Zp (2 * m + 1 + 1)) → Zp (2 * m + 1 + 1) → Int
  | [], c => delta LibInt.intR c (ring (2 * m + 1 + 1)).zero
  | a :: as, c => sumK LibInt.intR (fun x => Nf as ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x))))) (L m)

theorem chi_sq_val (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a : Zp (2 * m + 1 + 1)) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) : chi m a * chi m a = 1 := by
  rw [sqchi m hpr a, show delta LibInt.intR a (ring (2 * m + 1 + 1)).zero = 0 from ite_neg' _ _ ha]; rfl

/-- #{a x² = u} = 1 + χ(a)χ(u). -/
theorem conv_a (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a : Zp (2 * m + 1 + 1)) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) (F : Zp (2 * m + 1 + 1) → Int) :
    sumK LibInt.intR (fun x => F ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x))) (L m) = sumK LibInt.intR (fun u => (1 + chi m a * chi m u) * F u) (L m) := by
  rw [conv m hpr (fun v => F ((ring (2 * m + 1 + 1)).mul a v))]
  have hi := ConicSum.iv_mul_self m hpr a ha
  rw [← FiniteRecChain.reindex LibInt.intR (L m) (hcomp m) ((ring (2 * m + 1 + 1)).mul a) ((ring (2 * m + 1 + 1)).mul (ConicSum.iv m a))
    (fun x => by rw [← (ring (2 * m + 1 + 1)).mul_assoc, hi]; lib_ring)
    (fun y => by rw [← (ring (2 * m + 1 + 1)).mul_assoc, (ring (2 * m + 1 + 1)).mul_comm a, hi]; lib_ring) (fun u => (1 + chi m a * chi m u) * F u)]
  exact sumK_ext LibInt.intR _ _ (fun v => by
    show (1 + chi m v) * F ((ring (2 * m + 1 + 1)).mul a v) = (1 + chi m a * chi m ((ring (2 * m + 1 + 1)).mul a v)) * F ((ring (2 * m + 1 + 1)).mul a v)
    rw [chi_mul m hpr, show chi m a * (chi m a * chi m v) = (chi m a * chi m a) * chi m v by lib_int, chi_sq_val m hpr a ha]; lib_int) _

/-- **One step for a coefficient a ≠ 0** (δ = χ(a)). -/
theorem step_a (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a : Zp (2 * m + 1 + 1)) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) (as : List (Zp (2 * m + 1 + 1))) (A B Cc : Int)
    (hne : ∀ c, c ≠ (ring (2 * m + 1 + 1)).zero → Nf m as c = A + B * chi m c) (h0 : Nf m as (ring (2 * m + 1 + 1)).zero = Cc) :
    (∀ c, c ≠ (ring (2 * m + 1 + 1)).zero → Nf m (a :: as) c = ((Int.ofNat (2 * m + 1 + 1 + 1)) * A + -(chi m a * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * B) + Cc + -A) + chi m a * (Cc + -A) * chi m c) ∧
    Nf m (a :: as) (ring (2 * m + 1 + 1)).zero = (Int.ofNat (2 * m + 1 + 1 + 1)) * A + chi m a * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * B * ((Int.ofNat (2 * m + 1 + 1 + 1)) + -1) + Cc + -A := by
  have split : ∀ c u, Nf m as ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg u)) = A + B * chi m ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg u)) + delta LibInt.intR u c * (Cc + -A) := fun c u => by
    by_cases hu : u = c
    · rw [hu, show (ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg c) = (ring (2 * m + 1 + 1)).zero by lib_ring, h0, chi_zero, show delta LibInt.intR c c = 1 from ite_pos' _ _ rfl]; lib_int
    · have h1 : (ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg u) ≠ (ring (2 * m + 1 + 1)).zero := fun h => hu (by calc u = (ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg u))) := by lib_ring
                                                                    _ = c := by rw [h]; lib_ring)
      rw [hne _ h1, show delta LibInt.intR u c = 0 from ite_neg' _ _ hu]; lib_int
  have gen : ∀ c, Nf m (a :: as) c = A * (Int.ofNat (2 * m + 1 + 1 + 1)) + chi m a * B * (sumK LibInt.intR (fun u => chi m u * chi m ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg u))) (L m))
      + B * sumK LibInt.intR (fun u => chi m ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg u))) (L m) + (1 + chi m a * chi m c) * (Cc + -A) := fun c => by
    show sumK LibInt.intR (fun x => Nf m as ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x))))) (L m) = _
    rw [conv_a m hpr a ha (fun u => Nf m as ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg u)))]
    rw [sumK_ext LibInt.intR _ (fun u => A * 1 + A * (chi m a * chi m u) + B * chi m ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg u)) + chi m a * B * (chi m u * chi m ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg u)))
        + delta LibInt.intR u c * ((1 + chi m a * chi m u) * (Cc + -A))) (fun u => by rw [split c u]; lib_int)]
    have hpk : sumK LibInt.intR (fun u => delta LibInt.intR u c * ((1 + chi m a * chi m u) * (Cc + -A))) (L m) = (1 + chi m a * chi m c) * (Cc + -A) :=
      FiniteRecChain.pick LibInt.intR (L m) (hcomp m) (fun u => (1 + chi m a * chi m u) * (Cc + -A)) c
    rw [sI_add, sI_add, sI_add, sI_add, sI_mul, sI_mul, sI_mul, sI_mul, sI_mul, T1 m, S0 m hpr, hpk]
    lib_int
  constructor
  · intro c hc
    rw [gen c, S1 m hpr c, S2 m hpr c hc]; lib_int
  · rw [gen (ring (2 * m + 1 + 1)).zero, S1 m hpr (ring (2 * m + 1 + 1)).zero, S3 m hpr, chi_zero]; lib_int

/-- (A, B, C) along a coefficient list. -/
def Sf : List (Zp (2 * m + 1 + 1)) → Int × Int × Int
  | [] => (0, 0, 1)
  | a :: as => ((Int.ofNat (2 * m + 1 + 1 + 1)) * (Sf as).1 + -(chi m a * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (Sf as).2.1) + (Sf as).2.2 + -(Sf as).1, chi m a * ((Sf as).2.2 + -(Sf as).1),
                (Int.ofNat (2 * m + 1 + 1 + 1)) * (Sf as).1 + chi m a * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (Sf as).2.1 * ((Int.ofNat (2 * m + 1 + 1 + 1)) + -1) + (Sf as).2.2 + -(Sf as).1)

/-- **Every nondegenerate diagonal quadratic form over 𝔽_p**: #{Σ a_i x_i² = c} = A + Bχ(c) (c ≠ 0), C (c = 0). -/
theorem diag_form (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : ∀ as : List (Zp (2 * m + 1 + 1)), (∀ a, memL a as → a ≠ (ring (2 * m + 1 + 1)).zero) →
    (∀ c, c ≠ (ring (2 * m + 1 + 1)).zero → Nf m as c = (Sf m as).1 + (Sf m as).2.1 * chi m c) ∧ Nf m as (ring (2 * m + 1 + 1)).zero = (Sf m as).2.2
  | [], _ => by
    constructor
    · intro c hc; show delta LibInt.intR c (ring (2 * m + 1 + 1)).zero = 0 + 0 * chi m c; rw [show delta LibInt.intR c (ring (2 * m + 1 + 1)).zero = 0 from ite_neg' _ _ hc]; lib_int
    · show delta LibInt.intR (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero = 1; unfold delta; exact ite_pos' _ _ rfl
  | a :: as, h => by
    have ih := diag_form hpr as (fun b hb => h b (Or.inr hb))
    have st := step_a m hpr a (h a (Or.inl rfl)) as _ _ _ ih.1 ih.2
    exact st

/-! ## closed forms (Gauss–Lebesgue–Jordan) -/

theorem Sq_succ (k : Nat) : Sq m (k + 1) = ((Int.ofNat (2 * m + 1 + 1 + 1)) * (Sq m k).1 + -((chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (Sq m k).2.1) + (Sq m k).2.2 + -(Sq m k).1, (Sq m k).2.2 + -(Sq m k).1,
    (Int.ofNat (2 * m + 1 + 1 + 1)) * (Sq m k).1 + (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (Sq m k).2.1 * ((Int.ofNat (2 * m + 1 + 1 + 1)) + -1) + (Sq m k).2.2 + -(Sq m k).1) := rfl

theorem ee (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) = 1 := by
  rcases e_val m hpr with h | h <;> rw [h] <;> rfl

/-- With Q = p^j and E = χ(−1)^j:  Sq(2j+1) = (Q², EQ, Q²),  Sq(2j+2) = (Q²p − Eχ(−1)Q, 0, Q²p + Eχ(−1)(Qp − Q)). -/
theorem closed (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : ∀ j : Nat,
    Sq m (2 * j + 1) = (pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j * pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j, pw LibInt.intR (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) j * pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j, pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j * pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j) ∧
    Sq m (2 * j + 2) = (pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j * pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j * (Int.ofNat (2 * m + 1 + 1 + 1)) + -(pw LibInt.intR (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) j * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j), 0,
      pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j * pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j * (Int.ofNat (2 * m + 1 + 1 + 1)) + pw LibInt.intR (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) j * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j * (Int.ofNat (2 * m + 1 + 1 + 1)) + -(pw LibInt.intR (Int.o  -- (+30 chars; full line in the Zenodo source)
  | 0 => by
    have h1 : Sq m (2 * 0 + 1) = (1, 1, 1) := by
      show Sq m (0 + 1) = _; rw [Sq_succ]; show ((Int.ofNat (2 * m + 1 + 1 + 1)) * 0 + -((chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * 0) + 1 + -0, 1 + -0, (Int.ofNat (2 * m + 1 + 1 + 1)) * 0 + (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * 0 * ((Int.ofNat (2 * m + 1 + 1 + 1)) + -1) + 1 + -0) = _
      apply Prod.ext; · show (Int.ofNat (2 * m + 1 + 1 + 1)) * 0 + -((chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * 0) + 1 + -0 = 1; lib_int
      apply Prod.ext; · show (1 : Int) + -0 = 1; lib_int
      · show (Int.ofNat (2 * m + 1 + 1 + 1)) * 0 + (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * 0 * ((Int.ofNat (2 * m + 1 + 1 + 1)) + -1) + 1 + -0 = 1; lib_int
    refine ⟨by rw [h1]; show ((1 : Int), (1 : Int), (1 : Int)) = (1 * 1, 1 * 1, 1 * 1); rfl, ?_⟩
    show Sq m (2 * 0 + 1 + 1) = _
    rw [Sq_succ, h1]
    show _ = (1 * 1 * (Int.ofNat (2 * m + 1 + 1 + 1)) + -(1 * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * 1), 0, 1 * 1 * (Int.ofNat (2 * m + 1 + 1 + 1)) + 1 * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (1 * (Int.ofNat (2 * m + 1 + 1 + 1)) + -1))
    apply Prod.ext; · show (Int.ofNat (2 * m + 1 + 1 + 1)) * 1 + -((chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * 1) + 1 + -1 = 1 * 1 * (Int.ofNat (2 * m + 1 + 1 + 1)) + -(1 * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * 1); lib_int
    apply Prod.ext; · show (1 : Int) + -1 = 0; rfl
    · show (Int.ofNat (2 * m + 1 + 1 + 1)) * 1 + (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * 1 * ((Int.ofNat (2 * m + 1 + 1 + 1)) + -1) + 1 + -1 = 1 * 1 * (Int.ofNat (2 * m + 1 + 1 + 1)) + 1 * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (1 * (Int.ofNat (2 * m + 1 + 1 + 1)) + -1); lib_int
  | j + 1 => by
    have ih := closed hpr j
    have he := ee m hpr
    have hQ : pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) (j + 1) = pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j * (Int.ofNat (2 * m + 1 + 1 + 1)) := rfl
    have hE : pw LibInt.intR (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (j + 1) = pw LibInt.intR (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) j * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := rfl
    rw [hQ, hE]
    generalize pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j = Q at ih ⊢
    generalize pw LibInt.intR (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) j = E at ih ⊢
    have odd : Sq m (2 * (j + 1) + 1) = (Q * (Int.ofNat (2 * m + 1 + 1 + 1)) * (Q * (Int.ofNat (2 * m + 1 + 1 + 1))), E * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (Q * (Int.ofNat (2 * m + 1 + 1 + 1))), Q * (Int.ofNat (2 * m + 1 + 1 + 1)) * (Q * (Int.ofNat (2 * m + 1 + 1 + 1)))) := by
      rw [show 2 * (j + 1) + 1 = (2 * j + 2) + 1 by lib_nat, Sq_succ, ih.2]
      apply Prod.ext; · show (Int.ofNat (2 * m + 1 + 1 + 1)) * (Q * Q * (Int.ofNat (2 * m + 1 + 1 + 1)) + -(E * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * Q)) + -((chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * 0) + (Q * Q * (Int.ofNat (2 * m + 1 + 1 + 1)) + E * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (Q * (Int.ofNat (2 * m + 1 + 1  -- (+222 chars; full line in the Zenodo source)
      apply Prod.ext; · show (Q * Q * (Int.ofNat (2 * m + 1 + 1 + 1)) + E * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (Q * (Int.ofNat (2 * m + 1 + 1 + 1)) + -Q)) + -(Q * Q * (Int.ofNat (2 * m + 1 + 1 + 1)) + -(E * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * Q)) = E * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (Q * (Int.ofNat (2 *  -- (+26 chars; full line in the Zenodo source)
      · show (Int.ofNat (2 * m + 1 + 1 + 1)) * (Q * Q * (Int.ofNat (2 * m + 1 + 1 + 1)) + -(E * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * Q)) + (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * 0 * ((Int.ofNat (2 * m + 1 + 1 + 1)) + -1) + (Q * Q * (Int.ofNat (2 * m + 1 + 1 + 1)) + E * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (Q * (In  -- (+244 chars; full line in the Zenodo source)
    refine ⟨odd, ?_⟩
    rw [show 2 * (j + 1) + 2 = (2 * (j + 1) + 1) + 1 by lib_nat, Sq_succ, odd]
    apply Prod.ext
    · show (Int.ofNat (2 * m + 1 + 1 + 1)) * (Q * (Int.ofNat (2 * m + 1 + 1 + 1)) * (Q * (Int.ofNat (2 * m + 1 + 1 + 1)))) + -((chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (E * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (Q * (Int.ofNat (2 * m + 1 + 1 + 1))))) + Q * (Int.ofNat (2 * m + 1 + 1 + 1)) * (Q * (Int.ofNat (2 * m + 1 + 1 + 1))) + -(Q * (Int.ofNat  -- (+353 chars; full line in the Zenodo source)
      lib_int
    apply Prod.ext
    · show Q * (Int.ofNat (2 * m + 1 + 1 + 1)) * (Q * (Int.ofNat (2 * m + 1 + 1 + 1))) + -(Q * (Int.ofNat (2 * m + 1 + 1 + 1)) * (Q * (Int.ofNat (2 * m + 1 + 1 + 1)))) = 0; lib_int
    · show (Int.ofNat (2 * m + 1 + 1 + 1)) * (Q * (Int.ofNat (2 * m + 1 + 1 + 1)) * (Q * (Int.ofNat (2 * m + 1 + 1 + 1)))) + (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (E * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (Q * (Int.ofNat (2 * m + 1 + 1 + 1)))) * ((Int.ofNat (2 * m + 1 + 1 + 1)) + -1) + Q * (Int.ofNat (2 * m + 1 + 1 + 1)) * (Q * (Int.ofNat (2  -- (+100 chars; full line in the Zenodo source)
        = Q * (Int.ofNat (2 * m + 1 + 1 + 1)) * (Q * (Int.ofNat (2 * m + 1 + 1 + 1))) * (Int.ofNat (2 * m + 1 + 1 + 1)) + E * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (Q * (Int.ofNat (2 * m + 1 + 1 + 1)) * (Int.ofNat (2 * m + 1 + 1 + 1)) + -(Q * (Int.ofNat (2 * m + 1 + 1 + 1))))
      lib_int

/-! ## closed forms for every diagonal form -/

def dprod : List (Zp (2 * m + 1 + 1)) → Int
  | [] => 1
  | a :: as => chi m a * dprod as

theorem Sf_cons (a : Zp (2 * m + 1 + 1)) (as : List (Zp (2 * m + 1 + 1))) : Sf m (a :: as) =
    ((Int.ofNat (2 * m + 1 + 1 + 1)) * (Sf m as).1 + -(chi m a * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (Sf m as).2.1) + (Sf m as).2.2 + -(Sf m as).1, chi m a * ((Sf m as).2.2 + -(Sf m as).1),
     (Int.ofNat (2 * m + 1 + 1 + 1)) * (Sf m as).1 + chi m a * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (Sf m as).2.1 * ((Int.ofNat (2 * m + 1 + 1 + 1)) + -1) + (Sf m as).2.2 + -(Sf m as).1) := rfl

/-- **Closed forms for Σ a_i x_i²** with d = Π (a_i/p): length 2j+1 gives (p^{2j}, e^j d p^j, p^{2j}); length 2j+2 gives
    (p^{2j+1} − e^{j+1} d p^j, 0, p^{2j+1} + e^{j+1} d (p^{j+1} − p^j)). -/
theorem form_closed : ∀ as : List (Zp (2 * m + 1 + 1)),
    (∀ j, as.length = 2 * j + 1 → Sf m as = (pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j * pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j, pw LibInt.intR (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) j * dprod m as * pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j, pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j * pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j)) ∧
    (∀ j, as.length = 2 * j + 2 → Sf m as = (pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j * pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j * (Int.ofNat (2 * m + 1 + 1 + 1)) + -(pw LibInt.intR (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) j * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * dprod m as * pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j), 0,
      pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j * pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j * (Int.ofNat (2 * m + 1 + 1 + 1)) + pw LibInt.intR (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) j * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * dprod m as * (pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j * (Int.ofNat (2 * m + 1 + 1 + 1)) + -(pw LibIn  -- (+44 chars; full line in the Zenodo source)
  | [] => ⟨fun j h => Nat.noConfusion h, fun j h => Nat.noConfusion h⟩
  | a :: as => by
    have ih := form_closed as
    constructor
    · intro j hj
      have hl : as.length = 2 * j := Nat.succ.inj hj
      cases j with
      | zero =>
        have he : as = [] := by
          cases as with
          | nil => rfl
          | cons b bs => exact Nat.noConfusion hl
        rw [he]
        show Sf m [a] = _
        rw [Sf_cons]
        show ((Int.ofNat (2 * m + 1 + 1 + 1)) * 0 + -(chi m a * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * 0) + 1 + -0, chi m a * (1 + -0), (Int.ofNat (2 * m + 1 + 1 + 1)) * 0 + chi m a * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * 0 * ((Int.ofNat (2 * m + 1 + 1 + 1)) + -1) + 1 + -0) = (1 * 1, 1 * (chi m a * 1) * 1, 1 * 1)
        apply Prod.ext; · show (Int.ofNat (2 * m + 1 + 1 + 1)) * 0 + -(chi m a * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * 0) + 1 + -0 = 1 * 1; lib_int
        apply Prod.ext; · show chi m a * (1 + -0) = 1 * (chi m a * 1) * 1; lib_int
        · show (Int.ofNat (2 * m + 1 + 1 + 1)) * 0 + chi m a * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * 0 * ((Int.ofNat (2 * m + 1 + 1 + 1)) + -1) + 1 + -0 = 1 * 1; lib_int
      | succ i =>
        have h2 := ih.2 i (by rw [hl]; lib_nat)
        rw [Sf_cons, h2]
        have hQ : pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) (i + 1) = pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) i * (Int.ofNat (2 * m + 1 + 1 + 1)) := rfl
        have hE : pw LibInt.intR (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (i + 1) = pw LibInt.intR (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) i * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := rfl
        rw [hQ, hE, show dprod m (a :: as) = chi m a * dprod m as from rfl]
        generalize pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) i = Q
        generalize pw LibInt.intR (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) i = E
        generalize dprod m as = d
        apply Prod.ext
        · show (Int.ofNat (2 * m + 1 + 1 + 1)) * (Q * Q * (Int.ofNat (2 * m + 1 + 1 + 1)) + -(E * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * d * Q)) + -(chi m a * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * 0) + (Q * Q * (Int.ofNat (2 * m + 1 + 1 + 1)) + E * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * d * (Q * (Int.ofNat (2 * m + 1  -- (+221 chars; full line in the Zenodo source)
          lib_int
        apply Prod.ext
        · show chi m a * ((Q * Q * (Int.ofNat (2 * m + 1 + 1 + 1)) + E * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * d * (Q * (Int.ofNat (2 * m + 1 + 1 + 1)) + -Q)) + -(Q * Q * (Int.ofNat (2 * m + 1 + 1 + 1)) + -(E * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * d * Q))) = E * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (chi m a * d)   -- (+39 chars; full line in the Zenodo source)
          lib_int
        · show (Int.ofNat (2 * m + 1 + 1 + 1)) * (Q * Q * (Int.ofNat (2 * m + 1 + 1 + 1)) + -(E * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * d * Q)) + chi m a * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * 0 * ((Int.ofNat (2 * m + 1 + 1 + 1)) + -1) + (Q * Q * (Int.ofNat (2 * m + 1 + 1 + 1)) + E * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).  -- (+259 chars; full line in the Zenodo source)
          lib_int
    · intro j hj
      have h1 := ih.1 j (Nat.succ.inj hj)
      rw [Sf_cons, h1, show dprod m (a :: as) = chi m a * dprod m as from rfl]
      generalize pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j = Q
      generalize pw LibInt.intR (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) j = E
      generalize dprod m as = d
      apply Prod.ext
      · show (Int.ofNat (2 * m + 1 + 1 + 1)) * (Q * Q) + -(chi m a * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (E * d * Q)) + Q * Q + -(Q * Q) = Q * Q * (Int.ofNat (2 * m + 1 + 1 + 1)) + -(E * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (chi m a * d) * Q); lib_int
      apply Prod.ext
      · show chi m a * (Q * Q + -(Q * Q)) = 0; lib_int
      · show (Int.ofNat (2 * m + 1 + 1 + 1)) * (Q * Q) + chi m a * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (E * d * Q) * ((Int.ofNat (2 * m + 1 + 1 + 1)) + -1) + Q * Q + -(Q * Q) = Q * Q * (Int.ofNat (2 * m + 1 + 1 + 1)) + E * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (chi m a * d) * (Q * (Int.ofNat (2 * m + 1 + 1 + 1)) + -Q); lib_int

end QuadDiagonal
#print axioms QuadDiagonal.sI_add
#print axioms QuadDiagonal.sI_mul
#print axioms QuadDiagonal.sI_mulr
#print axioms QuadDiagonal.zsum
#print axioms QuadDiagonal.sq_root
#print axioms QuadDiagonal.two_ne
#print axioms QuadDiagonal.nozd'
#print axioms QuadDiagonal.two_roots
#print axioms QuadDiagonal.sq_count
#print axioms QuadDiagonal.hcomp
#print axioms QuadDiagonal.conv
#print axioms QuadDiagonal.e_val
#print axioms QuadDiagonal.T1
#print axioms QuadDiagonal.S0
#print axioms QuadDiagonal.S1
#print axioms QuadDiagonal.S2
#print axioms QuadDiagonal.sqchi
#print axioms QuadDiagonal.S3
#print axioms QuadDiagonal.Psum
#print axioms QuadDiagonal.step
#print axioms QuadDiagonal.quad_diagonal
#print axioms QuadDiagonal.chi_sq_val
#print axioms QuadDiagonal.conv_a
#print axioms QuadDiagonal.step_a
#print axioms QuadDiagonal.diag_form
#print axioms QuadDiagonal.Sq_succ
#print axioms QuadDiagonal.ee
#print axioms QuadDiagonal.closed
#print axioms QuadDiagonal.Sf_cons
#print axioms QuadDiagonal.form_closed

lang/QuadExt.lean

lean_all/lang/QuadExt.lean

/-
  lang/QuadExt — T43, cycle 139: the quadratic extension F[√n] of any finite field F of the interface.

  F of order q = 2h + 1 (h = j + 1), n a non-residue of F. E = quad F n on pairs:
    pairsL, comp_pairs, card_pairs     the q² pairs, listed once each
    cnt_nzL                            counts in the list of nonzero pairs
    norm_ne                            N(x) = a² − n b² ≠ 0 for x ≠ 0     (n is not a square)
    ivE, ivE_mul, nozdE                inverses x̄ · N(x)⁻¹, no zero divisors
    fermatE                            x^(q²−1) = 1                        (FiniteField.unit_pow)
    quadExt                            the FF structure on E               (MkFF)
  So 𝔽_{q²} is an instance whenever 𝔽_q is: the towers 𝔽_{p^(2^k)} and all FF theorems over them.

  Lean 4 core only, proofs by hand (lib_ring, lib_nat, lib_int), no axioms.
-/
import lang.MkFF
import lang.FiniteField
import lang.Quad

namespace QuadExt

open FTAChain YMChain FiniteRecChain FinField Quad

variable {R : Type} [DecidableEq R]

abbrev Z := LibInt.intR

def pairsL (els : List R) : List R → List (R × R)
  | [] => []
  | a :: l => els.map (fun b => (a, b)) ++ pairsL els l

theorem sumK_map {A B : Type} (f : B → Int) (g : A → B) : ∀ l : List A, sumK Z f (l.map g) = sumK Z (fun a => f (g a)) l
  | [] => rfl
  | a :: l => by show f (g a) + sumK Z f (l.map g) = _; rw [sumK_map f g l]; rfl

omit [DecidableEq R] in
theorem sum_pairsL (els : List R) (G : R × R → Int) : ∀ l : List R,
    sumK Z G (pairsL els l) = sumK Z (fun a => sumK Z (fun b => G (a, b)) els) l
  | [] => rfl
  | a :: l => by
    show sumK Z G (els.map (fun b => (a, b)) ++ pairsL els l) = _
    rw [CMInert.sumK_app', sum_pairsL els G l, sumK_map]
    rfl

theorem delta_pair (y z : R × R) : delta Z y z = delta Z y.1 z.1 * delta Z y.2 z.2 := by
  unfold delta
  by_cases h1 : y.1 = z.1
  · by_cases h2 : y.2 = z.2
    · rw [ite_pos' _ _ (Prod.ext h1 h2), ite_pos' _ _ h1, ite_pos' _ _ h2]; rfl
    · rw [ite_neg' _ _ (fun e => h2 (congrArg Prod.snd e)), ite_pos' _ _ h1, ite_neg' _ _ h2]; rfl
  · rw [ite_neg' _ _ (fun e => h1 (congrArg Prod.fst e)), ite_neg' _ _ h1]; exact (DetChain.zero_mul' Z _).symm

theorem comp_pairs (els : List R) (hc : Complete Z els) : Complete Z (pairsL els els) := fun z => by
  rw [sum_pairsL]
  have pt : ∀ a, sumK Z (fun b => delta Z (a, b) z) els = delta Z a z.1 := fun a => by
    rw [sumK_ext Z _ (fun b => Z.mul (delta Z a z.1) (delta Z b z.2)) (fun b => delta_pair (a, b) z), ← mul_sumK, hc z.2]
    show delta Z a z.1 * 1 = _; lib_int
  rw [sumK_ext Z _ _ pt]
  exact hc z.1

omit [DecidableEq R] in
theorem card_pairs (els : List R) (q : Int) (hq : sumK Z (fun _ => (1 : Int)) els = q) :
    sumK Z (fun _ => (1 : Int)) (pairsL els els) = q * q := by
  rw [sum_pairsL, sumK_ext Z _ (fun _ => q) (fun _ => hq)]
  rw [show sumK Z (fun _ => q) els = Z.mul q (sumK Z (fun _ => (1 : Int)) els) by
    rw [mul_sumK]; exact sumK_ext Z _ _ (fun _ => by show _ = q * 1; lib_int) _, hq]
  rfl

theorem cnt_eq (z : R) : ∀ l : List R, FiniteField.cnt z l = FinList.cnt z l
  | [] => rfl
  | a :: l => by show (if a = z then 1 else 0) + _ = (if a = z then 1 else 0) + _; rw [cnt_eq z l]

theorem cnt_nzL (z w : R) : ∀ l : List R, FinList.cnt z (FinList.nzL w l) = if z = w then 0 else FinList.cnt z l
  | [] => by
    show 0 = if z = w then 0 else 0
    by_cases e : z = w
    · rw [ite_pos' _ _ e]
    · rw [ite_neg' _ _ e]
  | a :: l => by
    show FinList.cnt z (if a = w then FinList.nzL w l else a :: FinList.nzL w l) = _
    by_cases ea : a = w
    · rw [ite_pos' _ _ ea, cnt_nzL z w l]
      show _ = if z = w then 0 else (if a = z then 1 else 0) + FinList.cnt z l
      by_cases ez : z = w
      · rw [ite_pos' _ _ ez, ite_pos' _ _ ez]
      · rw [ite_neg' _ _ ez, ite_neg' _ _ ez, ite_neg' _ _ (fun (e : a = z) => ez (e ▸ ea)), Nat.zero_add]
    · rw [ite_neg' _ _ ea]
      show (if a = z then 1 else 0) + FinList.cnt z (FinList.nzL w l) = _
      rw [cnt_nzL z w l]
      by_cases ez : z = w
      · rw [ite_pos' _ _ ez, ite_pos' _ _ ez, ite_neg' _ _ (fun e => ea (e.trans ez))]
      · rw [ite_neg' _ _ ez, ite_neg' _ _ ez]; rfl

variable (F : FF R) (n : R)

theorem norm_ne (hn0 : n ≠ F.K.zero) (hnr : Frobenius.pw F.K n F.h ≠ F.K.one) (x : R × R) (hx : x ≠ (quad F.K n).zero) : qnorm F.K n x ≠ F.K.zero := fun hN => by
  by_cases hb : x.2 = F.K.zero
  · have ha : x.1 ≠ F.K.zero := fun ha => hx (Prod.ext ha hb)
    have : F.K.mul x.1 x.1 = F.K.zero := by
      rw [← hN]; show _ = F.K.add (F.K.mul x.1 x.1) (F.K.neg (F.K.mul n (F.K.mul x.2 x.2))); rw [hb]; lib_ring
    cases F.nozd _ _ this with
    | inl h => exact ha h
    | inr h => exact ha h
  · have hab : F.K.mul x.1 x.1 = F.K.mul n (F.K.mul x.2 x.2) := FinMoment.eq_of_sub_zero F _ _ hN
    have hbi := F.iv_mul x.2 hb
    have ht : F.K.mul (F.K.mul x.1 (F.iv x.2)) (F.K.mul x.1 (F.iv x.2)) = n := by
      rw [show F.K.mul (F.K.mul x.1 (F.iv x.2)) (F.K.mul x.1 (F.iv x.2)) = F.K.mul (F.K.mul x.1 x.1) (F.K.mul (F.iv x.2) (F.iv x.2)) by lib_ring,
        hab, show F.K.mul (F.K.mul n (F.K.mul x.2 x.2)) (F.K.mul (F.iv x.2) (F.iv x.2)) = F.K.mul n (F.K.mul (F.K.mul (F.iv x.2) x.2) (F.K.mul (F.iv x.2) x.2)) by lib_ring,
        hbi]
      lib_ring
    have ht0 : F.K.mul x.1 (F.iv x.2) ≠ F.K.zero := fun e => hn0 (by rw [← ht, e]; exact DetChain.mul_zero F.K _)
    exact hnr (by rw [← ht]; exact F.euler_sq _ ht0)

def ivE (x : R × R) : R × R := (quad F.K n).mul (qconj F.K x) (F.iv (qnorm F.K n x), F.K.zero)

theorem ivE_mul (hn0 : n ≠ F.K.zero) (hnr : Frobenius.pw F.K n F.h ≠ F.K.one) (x : R × R) (hx : x ≠ (quad F.K n).zero) : (quad F.K n).mul (ivE F n x) x = (quad F.K n).one := by
  have hN := F.iv_mul _ (norm_ne F n hn0 hnr x hx)
  rw [show (quad F.K n).mul (ivE F n x) x = (quad F.K n).mul ((quad F.K n).mul x (qconj F.K x)) (F.iv (qnorm F.K n x), F.K.zero) by
      unfold ivE; lib_ring, mul_self_norm]
  apply Prod.ext
  · show F.K.add (F.K.mul (qnorm F.K n x) (F.iv (qnorm F.K n x))) (F.K.mul n (F.K.mul F.K.zero F.K.zero)) = F.K.one
    rw [F.K.mul_comm (qnorm F.K n x), hN]; lib_ring
  · show F.K.add (F.K.mul (qnorm F.K n x) F.K.zero) (F.K.mul F.K.zero (F.iv (qnorm F.K n x))) = F.K.zero
    lib_ring

theorem nozdE (hn0 : n ≠ F.K.zero) (hnr : Frobenius.pw F.K n F.h ≠ F.K.one) : DetChain.NoZD (quad F.K n) := fun x y e => by
  by_cases hx : x = (quad F.K n).zero
  · exact Or.inl hx
  · right
    calc y = (quad F.K n).mul (quad F.K n).one y := ((quad F.K n).one_mul y).symm
      _ = (quad F.K n).mul ((quad F.K n).mul (ivE F n x) x) y := by rw [ivE_mul F n hn0 hnr x hx]
      _ = (quad F.K n).mul (ivE F n x) ((quad F.K n).mul x y) := (quad F.K n).mul_assoc _ _ _
      _ = (quad F.K n).zero := by rw [e]; exact DetChain.mul_zero (quad F.K n) _

theorem sq_form (j : Nat) : (2 * (j + 1) + 1) * (2 * (j + 1) + 1) = 2 * (2 * j * j + 6 * j + 3 + 1) + 1 := by lib_nat

theorem cardE (j : Nat) (hj : F.h = j + 1) :
    sumK Z (fun _ => (1 : Int)) (pairsL F.els F.els) = Int.ofNat (2 * (2 * j * j + 6 * j + 3 + 1) + 1) := by
  rw [card_pairs F.els _ F.card, hj, ← sq_form j]; rfl

theorem fermatE (j : Nat) (hj : F.h = j + 1) (hn0 : n ≠ F.K.zero) (hnr : Frobenius.pw F.K n F.h ≠ F.K.one)
    (x : R × R) (hx : x ≠ (quad F.K n).zero) :
    Frobenius.pw (quad F.K n) x (2 * (2 * j * j + 6 * j + 3 + 1)) = (quad F.K n).one := by
  have hc : ∀ z, FinList.cnt z (pairsL F.els F.els) = 1 := fun z =>
    Int.ofNat.inj ((FinList.cnt_int z _).symm.trans (comp_pairs F.els F.comp z))
  have hl := FinList.len_nzL (quad F.K n).zero (pairsL F.els F.els)
  have hlen : (pairsL F.els F.els).length = 2 * (2 * j * j + 6 * j + 3 + 1) + 1 :=
    Int.ofNat.inj ((FinList.len_int _).symm.trans (cardE F j hj))
  rw [hc, hlen] at hl
  have hus : (FinList.nzL (quad F.K n).zero (pairsL F.els F.els)).length = 2 * (2 * j * j + 6 * j + 3 + 1) := Nat.succ.inj hl
  rw [← hus]
  apply FiniteField.unit_pow (quad F.K n) (nozdE F n hn0 hnr) (ivE F n) (ivE_mul F n hn0 hnr)
  · intro z
    rw [cnt_eq, cnt_nzL]
    by_cases e : z = (quad F.K n).zero
    · rw [ite_pos' _ _ e, ite_pos' _ _ e]
    · rw [ite_neg' _ _ e, ite_neg' _ _ e, hc]
  · intro z hz e
    rw [cnt_eq, cnt_nzL, ite_pos' _ _ e] at hz
    exact Nat.lt_irrefl 0 hz
  · exact fun e => FinConic.one_ne_zero F (congrArg Prod.fst e)
  · exact hx

/-- **𝔽_{q²} = F[√n] as a finite field of the interface.** -/
def quadExt (j : Nat) (hj : F.h = j + 1) (hn0 : n ≠ F.K.zero) (hnr : Frobenius.pw F.K n F.h ≠ F.K.one) : FF (R × R) :=
  MkFF.mkFF (quad F.K n) (nozdE F n hn0 hnr) (pairsL F.els F.els) (comp_pairs F.els F.comp) (2 * j * j + 6 * j + 3)
    (cardE F j hj) (fun e => F.ne_pm (congrArg Prod.fst e)) (fermatE F n j hj hn0 hnr)

end QuadExt

#print axioms QuadExt.pairsL
#print axioms QuadExt.sumK_map
#print axioms QuadExt.sum_pairsL
#print axioms QuadExt.delta_pair
#print axioms QuadExt.comp_pairs
#print axioms QuadExt.card_pairs
#print axioms QuadExt.cnt_eq
#print axioms QuadExt.cnt_nzL
#print axioms QuadExt.norm_ne
#print axioms QuadExt.ivE
#print axioms QuadExt.ivE_mul
#print axioms QuadExt.nozdE
#print axioms QuadExt.sq_form
#print axioms QuadExt.cardE
#print axioms QuadExt.fermatE
#print axioms QuadExt.quadExt

lang/QuadFrob.lean

lean_all/lang/QuadFrob.lean

/-
  lang/QuadFrob — T43, cycle 64: Frobenius of 𝔽_{p²} = 𝔽_p[√n] is conjugation, and 𝔽_p[√n] is a field.

  p = J + 2, K = ℤ/p, n a non-residue (n^((p−1)/2) = −1), F = quad K n.
    nsm_quad, char_quad   p · 1 = 0 in F
    pw_embed              (a, 0)^k = (a^k, 0)
    frob_conj             (a + b√n)^p = a − b√n                      (Frobenius.frob_add, Fermat, Quad.frob_sqrt)
    norm_ne_zero          (a, b) ≠ 0 ⇒ a² − n b² ≠ 0                 (n not a square)
    quad_inv              (a, b) ≠ 0 ⇒ (a, b) · (ā / N) = 1
  So 𝔽_p[√n] is a field with p² elements whose Frobenius is the conjugation — the unramified quadratic extension.

  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Quad
import lang.ZMod
import lang.EulerHard
import lang.HasseCM
import lib.Semi

namespace QuadFrob

open FTAChain ZMod Quad

variable (J : Nat) (n : Zp (J + 1))

theorem nsm_quad (a b : Zp (J + 1)) : ∀ k, Frobenius.nsm (quad (ring (J + 1)) n) k (a, b) = (Frobenius.nsm (ring (J + 1)) k a, Frobenius.nsm (ring (J + 1)) k b)
  | 0 => rfl
  | k + 1 => by
    show (quad (ring (J + 1)) n).add (Frobenius.nsm (quad (ring (J + 1)) n) k (a, b)) (a, b) = _
    rw [nsm_quad a b k]; rfl

theorem nsm_zero' : ∀ k, Frobenius.nsm (ring (J + 1)) k (ring (J + 1)).zero = (ring (J + 1)).zero
  | 0 => rfl
  | k + 1 => by show (ring (J + 1)).add (Frobenius.nsm (ring (J + 1)) k (ring (J + 1)).zero) (ring (J + 1)).zero = _; rw [nsm_zero' k, (ring (J + 1)).zero_add]

theorem char_quad : Frobenius.nsm (quad (ring (J + 1)) n) (J + 1 + 1) (quad (ring (J + 1)) n).one = (quad (ring (J + 1)) n).zero := by
  show Frobenius.nsm (quad (ring (J + 1)) n) (J + 1 + 1) ((ring (J + 1)).one, (ring (J + 1)).zero) = ((ring (J + 1)).zero, (ring (J + 1)).zero)
  rw [nsm_quad, char_p, nsm_zero']

theorem pw_embed (a : Zp (J + 1)) : ∀ k, Frobenius.pw (quad (ring (J + 1)) n) (a, (ring (J + 1)).zero) k = (Frobenius.pw (ring (J + 1)) a k, (ring (J + 1)).zero)
  | 0 => rfl
  | k + 1 => by
    show qmul (ring (J + 1)) n (Frobenius.pw (quad (ring (J + 1)) n) (a, (ring (J + 1)).zero) k) (a, (ring (J + 1)).zero) = _
    rw [pw_embed a k]
    apply Prod.ext
    · show (ring (J + 1)).add ((ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) a k) a) ((ring (J + 1)).mul n ((ring (J + 1)).mul (ring (J + 1)).zero (ring (J + 1)).zero)) = (ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) a k) a
      lib_ring
    · show (ring (J + 1)).add ((ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) a k) (ring (J + 1)).zero) ((ring (J + 1)).mul (ring (J + 1)).zero a) = (ring (J + 1)).zero
      lib_ring

/-- **Frobenius is conjugation**: `(a + b√n)^p = a − b√n`. -/
theorem frob_conj (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (m : Nat) (hJ : J + 1 + 1 = 2 * m + 1)
    (hn : Frobenius.pw (ring (J + 1)) n m = (ring (J + 1)).neg (ring (J + 1)).one) (a b : Zp (J + 1)) :
    Frobenius.pw (quad (ring (J + 1)) n) (a, b) (J + 1 + 1) = (a, (ring (J + 1)).neg b) := by
  have split : (a, b) = (quad (ring (J + 1)) n).add (a, (ring (J + 1)).zero) ((quad (ring (J + 1)) n).mul (b, (ring (J + 1)).zero) ((ring (J + 1)).zero, (ring (J + 1)).one)) := by
    apply Prod.ext
    · show a = (ring (J + 1)).add a ((ring (J + 1)).add ((ring (J + 1)).mul b (ring (J + 1)).zero) ((ring (J + 1)).mul n ((ring (J + 1)).mul (ring (J + 1)).zero (ring (J + 1)).one)))
      lib_ring
    · show b = (ring (J + 1)).add (ring (J + 1)).zero ((ring (J + 1)).add ((ring (J + 1)).mul b (ring (J + 1)).one) ((ring (J + 1)).mul (ring (J + 1)).zero (ring (J + 1)).zero))
      lib_ring
  rw [split, Frobenius.frob_add (quad (ring (J + 1)) n) J hpr (char_quad J n), Frobenius.pw_mul, pw_embed, pw_embed,
    fermat J hpr a, fermat J hpr b, hJ, frob_sqrt (ring (J + 1)) n m hn]
  apply Prod.ext
  · show (ring (J + 1)).add a ((ring (J + 1)).add ((ring (J + 1)).mul b ((ring (J + 1)).neg (ring (J + 1)).zero)) ((ring (J + 1)).mul n ((ring (J + 1)).mul (ring (J + 1)).zero ((ring (J + 1)).neg (ring (J + 1)).one)))) = a
    lib_ring
  · show (ring (J + 1)).add (ring (J + 1)).zero ((ring (J + 1)).add ((ring (J + 1)).mul b ((ring (J + 1)).neg (ring (J + 1)).one)) ((ring (J + 1)).mul (ring (J + 1)).zero ((ring (J + 1)).neg (ring (J + 1)).zero))) = (ring (J + 1)).neg b
    lib_ring

theorem norm_ne_zero (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (m : Nat) (hJ : J + 1 = 2 * m) (h1 : 1 ≤ J)
    (hn : Frobenius.pw (ring (J + 1)) n m = (ring (J + 1)).neg (ring (J + 1)).one) (a b : Zp (J + 1)) (hab : (a, b) ≠ ((ring (J + 1)).zero, (ring (J + 1)).zero)) :
    qnorm (ring (J + 1)) n (a, b) ≠ (ring (J + 1)).zero := fun hN => by
  have hNe : (ring (J + 1)).mul a a = (ring (J + 1)).mul n ((ring (J + 1)).mul b b) := by
    have := congrArg (fun z => (ring (J + 1)).add z ((ring (J + 1)).mul n ((ring (J + 1)).mul b b))) hN
    have e : (ring (J + 1)).add (qnorm (ring (J + 1)) n (a, b)) ((ring (J + 1)).mul n ((ring (J + 1)).mul b b)) = (ring (J + 1)).mul a a := by
      show (ring (J + 1)).add ((ring (J + 1)).add ((ring (J + 1)).mul a a) ((ring (J + 1)).neg ((ring (J + 1)).mul n ((ring (J + 1)).mul b b)))) ((ring (J + 1)).mul n ((ring (J + 1)).mul b b)) = _
      lib_ring
    rw [e] at this
    exact this.trans ((ring (J + 1)).zero_add _)
  by_cases hb : b = (ring (J + 1)).zero
  · apply hab
    have ha : a = (ring (J + 1)).zero := by
      rw [hb, DetChain.zero_mul', DetChain.mul_zero] at hNe
      cases nozd J hpr _ _ hNe with
      | inl h => exact h
      | inr h => exact h
    rw [ha, hb]
  · -- c = a b⁻¹ has c² = n, so n^m = c^(p−1) = 1
    have hbb := inv_exists J hpr b hb
    let c := (ring (J + 1)).mul a (Frobenius.pw (ring (J + 1)) b J)
    have hc2 : (ring (J + 1)).mul c c = n := by
      have e : (ring (J + 1)).mul c c = (ring (J + 1)).mul ((ring (J + 1)).mul a a) ((ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) b J) (Frobenius.pw (ring (J + 1)) b J)) := by
        show (ring (J + 1)).mul ((ring (J + 1)).mul a (Frobenius.pw (ring (J + 1)) b J)) ((ring (J + 1)).mul a (Frobenius.pw (ring (J + 1)) b J)) = _; lib_ring
      rw [e, hNe]
      have e2 : (ring (J + 1)).mul ((ring (J + 1)).mul n ((ring (J + 1)).mul b b)) ((ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) b J) (Frobenius.pw (ring (J + 1)) b J))
          = (ring (J + 1)).mul n ((ring (J + 1)).mul ((ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) b J) b) ((ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) b J) b)) := by lib_ring
      rw [e2, hbb, (ring (J + 1)).one_mul, (ring (J + 1)).mul_comm, (ring (J + 1)).one_mul]
    have hm : m = (m - 1) + 1 := by
      cases m with
      | zero => exact absurd hJ (fun e' => Nat.noConfusion e')
      | succ m' => rfl
    have hn0 : n ≠ (ring (J + 1)).zero := fun e => by
      rw [e, hm, HasseCM.pw_zero_succ] at hn
      apply PowerSum.one_ne_zero' J
      have := congrArg (fun z => (ring (J + 1)).add z (ring (J + 1)).one) hn
      exact ((ring (J + 1)).zero_add (ring (J + 1)).one).symm.trans (this.trans (DetChain.add_neg' (ring (J + 1)) (ring (J + 1)).one))
    have hc0 : c ≠ (ring (J + 1)).zero := fun e => hn0 (by rw [← hc2, e, DetChain.zero_mul'])
    have hpc : Frobenius.pw (ring (J + 1)) n m = (ring (J + 1)).one := by
      rw [← hc2, Frobenius.pw_mul, ← HasseCM.pw_add', show m + m = J + 1 by rw [hJ]; lib_nat]
      exact unit_pow J hpr c hc0
    exact TwoPower.neg_one_ne_one J h1 (hn.symm.trans hpc)

/-- **𝔽_p[√n] is a field**: every nonzero element has an inverse. -/
theorem quad_inv (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (m : Nat) (hJ : J + 1 = 2 * m) (h1 : 1 ≤ J)
    (hn : Frobenius.pw (ring (J + 1)) n m = (ring (J + 1)).neg (ring (J + 1)).one) (x : Zp (J + 1) × Zp (J + 1)) (hx : x ≠ ((ring (J + 1)).zero, (ring (J + 1)).zero)) :
    (quad (ring (J + 1)) n).mul x ((quad (ring (J + 1)) n).mul (qconj (ring (J + 1)) x) (Frobenius.pw (ring (J + 1)) (qnorm (ring (J + 1)) n x) J, (ring (J + 1)).zero)) = (quad (ring (J + 1)) n).one := by
  have hN := norm_ne_zero J n hpr m hJ h1 hn x.1 x.2 hx
  rw [← (quad (ring (J + 1)) n).mul_assoc, mul_self_norm]
  apply Prod.ext
  · show (ring (J + 1)).add ((ring (J + 1)).mul (qnorm (ring (J + 1)) n x) (Frobenius.pw (ring (J + 1)) (qnorm (ring (J + 1)) n x) J)) ((ring (J + 1)).mul n ((ring (J + 1)).mul (ring (J + 1)).zero (ring (J + 1)).zero)) = (ring (J + 1)).one
    rw [(ring (J + 1)).mul_comm (qnorm (ring (J + 1)) n x), inv_exists J hpr _ hN]
    lib_ring
  · show (ring (J + 1)).add ((ring (J + 1)).mul (qnorm (ring (J + 1)) n x) (ring (J + 1)).zero) ((ring (J + 1)).mul (ring (J + 1)).zero (Frobenius.pw (ring (J + 1)) (qnorm (ring (J + 1)) n x) J)) = (ring (J + 1)).zero
    lib_ring

end QuadFrob

#print axioms QuadFrob.nsm_quad
#print axioms QuadFrob.nsm_zero'
#print axioms QuadFrob.char_quad
#print axioms QuadFrob.pw_embed
#print axioms QuadFrob.frob_conj
#print axioms QuadFrob.norm_ne_zero
#print axioms QuadFrob.quad_inv

lang/QuadPeriod.lean

lean_all/lang/QuadPeriod.lean

/-
  lang/QuadPeriod — T43, cycle 34 (GL1 for quadratic fields): (q/p) depends only on p mod 4q.

  q = g + g + 1 an odd prime, p = h + h + 1, p' = h' + h' + 1 odd primes ≠ q with p' = p + 4q k. Then
    double_inj        a + a = b + b ⇒ a = b
    half_shift        h' = h + 2 q k
    par_shift         (−1)^(h' g) = (−1)^(h g)
    legB_mod_4q       (q/p') = (q/p)
  by quadratic reciprocity (QuadArtin.artin_symbol): (q/p) = (p/q)(−1)^(hg), and (p/q) is periodic mod q
  (QuadArtin.legB_periodic). So the Artin symbol of p in ℚ(√q) is a Dirichlet character mod 4q — with GaussSplit
  (−1, mod 4) and TwoSupp (2, mod 8) these are the prime pieces of Artin reciprocity for every quadratic field.

  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.QuadArtin

namespace QuadPeriod

open FTAChain QRChain QuadArtin

theorem double_inj (a b : Nat) (h : a + a = b + b) : a = b := by
  cases Nat.lt_or_ge a b with
  | inl hl => exact absurd (h ▸ Nat.add_lt_add hl hl) (Nat.lt_irrefl _)
  | inr hg =>
    cases Nat.lt_or_ge b a with
    | inl hl => exact absurd (h ▸ Nat.add_lt_add hl hl) (Nat.lt_irrefl _)
    | inr hg' => exact Nat.le_antisymm hg' hg

theorem half_shift (h h' q k : Nat) (e : h' + h' + 1 = h + h + 1 + q * (2 * k + 2 * k)) :
    h' = h + q * (2 * k) := by
  apply double_inj
  have e' : h' + h' = h + h + q * (2 * k + 2 * k) := by
    have := Nat.succ.inj (e.trans (Nat.add_right_comm (h + h) 1 _))
    exact this
  rw [e', nleft_distrib, mid4]

theorem par_shift (h g q k : Nat) : par ((h + q * (2 * k)) * g) = par (h * g) := by
  rw [nright_distrib, par_add, nmul_assoc, par_mul q, nmul_assoc 2 k g, par_mul 2, show par 2 = false from rfl]
  cases par q <;> cases par (h * g) <;> rfl

/-- **(q/p) depends only on p mod 4q.** -/
theorem legB_mod_4q (h h' g k : Nat) (hp : Pr (h + h + 1)) (hp' : Pr (h' + h' + 1)) (hq : Pr (g + g + 1))
    (hne : h ≠ g) (hne' : h' ≠ g)
    (e : h' + h' + 1 = h + h + 1 + (g + g + 1) * (2 * k + 2 * k)) :
    legB (h' + h' + 1) (g + g + 1) = legB (h + h + 1) (g + g + 1) := by
  have a := artin_symbol h g hp hq hne
  have a' := artin_symbol h' g hp' hq hne'
  have hq' : legB (g + g + 1) (h' + h' + 1) = legB (g + g + 1) (h + h + 1) := by
    rw [e, legB_periodic]
  have hpar : par (h' * g) = par (h * g) := by
    rw [half_shift h h' (g + g + 1) k e, par_shift]
  rw [hpar, hq'] at a'
  have key : ∀ x y c : Bool, bx x c = bx y c → x = y := by
    intro x y c hxy
    cases x <;> cases y <;> cases c <;> first | rfl | exact Bool.noConfusion hxy
  exact key _ _ _ (a'.trans a.symm)

end QuadPeriod

#print axioms QuadPeriod.double_inj
#print axioms QuadPeriod.half_shift
#print axioms QuadPeriod.par_shift
#print axioms QuadPeriod.legB_mod_4q

lang/QuadTwist.lean

lean_all/lang/QuadTwist.lean

/-
  lang/QuadTwist — T43, cycle 294: quadratic twists of an arbitrary Weierstrass curve, every odd p.

  p = 2m + 3, χ the Legendre symbol (QChar), W(a, b) = Σ_x χ(x³ + ax + b) (so a_p(y² = x³ + ax + b) = −W(a, b)).
    quad_twist   t ≠ 0 ⇒ W(t²a, t³b) = χ(t) W(a, b)              (x ↦ t x, χ(t³) = χ(t))
    twist_sq     t ≠ 0 ⇒ W(t⁴a, t⁶b) = W(a, b)                     (isomorphic curves)
    sum_b        Σ_b W(a, b) = 0                                   (the a_p of the pencil b ↦ E_{a,b} average to 0)
  The twist by a non-square multiplies a_p by −1 — for every curve, not only the CM ones.
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.Jacobsthal

namespace QuadTwist

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum Jacobsthal

variable (m : Nat)

def W (a b : Zp (2 * m + 1 + 1)) : Int :=
  sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a x) b))) (L m)

/-- **Quadratic twist**: W(t²a, t³b) = χ(t) W(a, b). -/
theorem quad_twist (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b t : Zp (2 * m + 1 + 1)) (ht : t ≠ (ring (2 * m + 1 + 1)).zero) :
    W m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t t) a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)).mul t t)) b) = chi m t * W m a b := by
  unfold W
  rw [← reindex_mul m hpr t ht]
  have h : ∀ x, chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t x) ((ring (2 * m + 1 + 1)).mul t x)))
      ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t t) a) ((ring (2 * m + 1 + 1)).mul t x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)).mul t t)) b)))
      = chi m t * chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a x) b)) := fun x => by
    rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t x) ((ring (2 * m + 1 + 1)).mul t x)))
        ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t t) a) ((ring (2 * m + 1 + 1)).mul t x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)).mul t t)) b))
        = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)).mul t t)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a x) b)) by lib_ring,
      chi_mul m hpr, chi_cube m hpr]
  rw [sumK_ext LibInt.intR _ _ h, show chi m t * sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a x) b))) (L m)
      = LibInt.intR.mul (sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a x) b))) (L m)) (chi m t) by
        exact LibInt.intR.mul_comm _ _, sumK_mul]
  exact sumK_ext LibInt.intR _ _ (fun x => LibInt.intR.mul_comm _ _) _

/-- **Twisting by a square does nothing**: W(t⁴a, t⁶b) = W(a, b). -/
theorem twist_sq (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b t : Zp (2 * m + 1 + 1)) (ht : t ≠ (ring (2 * m + 1 + 1)).zero) :
    W m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t t) ((ring (2 * m + 1 + 1)).mul t t)) a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t t) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t t) ((ring (2 * m + 1 + 1)).mul t t))) b)
      = W m a b := by
  have htt : (ring (2 * m + 1 + 1)).mul t t ≠ (ring (2 * m + 1 + 1)).zero := fun e => by
    cases nozd (2 * m + 1) hpr _ _ e with
    | inl h => exact ht h
    | inr h => exact ht h
  have h := quad_twist m hpr a b ((ring (2 * m + 1 + 1)).mul t t) htt
  rw [chi_sq m hpr t ht] at h
  calc _ = 1 * W m a b := h
    _ = W m a b := by lib_int

theorem sum_zero : ∀ l : List (Zp (2 * m + 1 + 1)), sumK LibInt.intR (fun _ => (0 : Int)) l = 0
  | [] => rfl
  | _ :: l => by
    show LibInt.intR.add 0 (sumK LibInt.intR (fun _ => (0 : Int)) l) = 0
    rw [sum_zero l]; rfl

/-- **The traces of the curves y² = x³ + ax + b sum to zero over b**: Σ_b W(a, b) = 0. -/
theorem sum_b (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a : Zp (2 * m + 1 + 1)) : sumK LibInt.intR (fun b => W m a b) (L m) = 0 := by
  unfold W
  rw [← sumK_swap LibInt.intR (fun x b => chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a x) b))) (L m) (L m)]
  have h : ∀ x, sumK LibInt.intR (fun b => chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a x) b))) (L m) = 0 := fun x => by
    have e := shift_sum m (chi m) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul a x))
    rw [sumK_ext LibInt.intR _ (fun b => chi m ((ring (2 * m + 1 + 1)).add b ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul a x)))) (fun b => by
          rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a x) b) = (ring (2 * m + 1 + 1)).add b ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul a x)) by lib_ring]) _,
      e]
    exact chi_sum m hpr
  rw [sumK_ext LibInt.intR _ _ h]
  exact sum_zero m _

end QuadTwist

#print axioms QuadTwist.quad_twist
#print axioms QuadTwist.twist_sq
#print axioms QuadTwist.sum_zero
#print axioms QuadTwist.sum_b

lang/QuartChar.lean

lean_all/lang/QuartChar.lean

/-
  lang/QuartChar — G2 (T43): the quartic character of 𝔽_p with values in ℤ[i].

  p = 4n + 5 prime, N = (p − 1)/4 = n + 1, ι ∈ 𝔽_p with ι² = −1. For x ≠ 0, t = x^N has t⁴ = 1, so t ∈ {1, ι, −1, −ι}
  ((t − 1)(t + 1)(t − ι)(t + ι) = t⁴ − 1); χ(x) = 1, i, −1, −i accordingly (χ(0) = 0), in ℤ[i] = Cyclo4.cyc4 ℤ.
    four_cases, exists_j, val4, reduce4     t⁴ = 1 ⇒ t = ι^j (j < 4) and then χ(x) = i^j
    chi4_mul                                χ(xy) = χ(x)χ(y)
    chi4_neg1                               χ(−1) = i^{2N}
    chi4_sum                                Σ_x χ(x) = 0
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Cyclo4
import lang.CubicChar

namespace QuartChar

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius

variable (n : Nat)

def chi4 (ι : Zp (4 * n + 3 + 1)) (x : Zp (4 * n + 3 + 1)) : Int × Int :=
  if x = (ring (4 * n + 3 + 1)).zero then (0, 0) else if pw (ring (4 * n + 3 + 1)) x (n + 1) = (ring (4 * n + 3 + 1)).one then (1, 0) else if pw (ring (4 * n + 3 + 1)) x (n + 1) = ι then (0, 1)
  else if pw (ring (4 * n + 3 + 1)) x (n + 1) = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one then (-1, 0) else (0, -1)

def I : Int × Int := (0, 1)

theorem cancel (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (a b : Zp (4 * n + 3 + 1)) (ha : a ≠ (ring (4 * n + 3 + 1)).zero) (h : (ring (4 * n + 3 + 1)).mul a b = (ring (4 * n + 3 + 1)).zero) : b = (ring (4 * n + 3 + 1)).zero := by
  have hv := inv_exists (4 * n + 3) hpr a ha
  calc b = (ring (4 * n + 3 + 1)).mul (ring (4 * n + 3 + 1)).one b := ((ring (4 * n + 3 + 1)).one_mul b).symm
    _ = (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul (pw (ring (4 * n + 3 + 1)) a (4 * n + 3)) a) b := by rw [hv]
    _ = (ring (4 * n + 3 + 1)).mul (pw (ring (4 * n + 3 + 1)) a (4 * n + 3)) ((ring (4 * n + 3 + 1)).mul a b) := by lib_ring
    _ = (ring (4 * n + 3 + 1)).zero := by rw [h, mul_zero]

theorem sub_ne (a b : Zp (4 * n + 3 + 1)) (h : a ≠ b) : (ring (4 * n + 3 + 1)).add a ((ring (4 * n + 3 + 1)).neg b) ≠ (ring (4 * n + 3 + 1)).zero := fun e => h (by
  calc a = (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).add a ((ring (4 * n + 3 + 1)).neg b)) b := by lib_ring
    _ = b := by rw [e, (ring (4 * n + 3 + 1)).zero_add])

theorem eq_of_sub (a b : Zp (4 * n + 3 + 1)) (h : (ring (4 * n + 3 + 1)).add a ((ring (4 * n + 3 + 1)).neg b) = (ring (4 * n + 3 + 1)).zero) : a = b := by
  calc a = (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).add a ((ring (4 * n + 3 + 1)).neg b)) b := by lib_ring
    _ = b := by rw [h, (ring (4 * n + 3 + 1)).zero_add]

theorem n1 : (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one ≠ (ring (4 * n + 3 + 1)).one := TwoPower.neg_one_ne_one (4 * n + 3) (Nat.le_add_left 1 _)

theorem i_ne_one (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : ι ≠ (ring (4 * n + 3 + 1)).one := fun e => n1 n (by rw [← hi, e]; lib_ring)
theorem i_ne_neg (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : ι ≠ (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one := fun e => n1 n (by
  rw [← hi, e]; lib_ring)
theorem ni_ne_one (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : (ring (4 * n + 3 + 1)).neg ι ≠ (ring (4 * n + 3 + 1)).one := fun e => n1 n (by
  rw [← hi, show ι = (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).neg ι) by lib_ring, e]; lib_ring)
theorem ni_ne_i (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : (ring (4 * n + 3 + 1)).neg ι ≠ ι := fun e => by
  have h2 : (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one) ι = (ring (4 * n + 3 + 1)).zero := by
    rw [show (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one) ι = (ring (4 * n + 3 + 1)).add ι ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).neg ι)) by lib_ring, e]; lib_ring
  have h0 : ι = (ring (4 * n + 3 + 1)).zero := by
    by_cases h1 : (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).zero
    · exfalso; exact n1 n (by calc (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one) := by rw [h1]; lib_ring
        _ = (ring (4 * n + 3 + 1)).one := by lib_ring)
    · exact cancel n hpr _ _ h1 h2
  apply PowerSum.one_ne_zero' (4 * n + 3)
  calc (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by lib_ring
    _ = (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul ι ι) := by rw [hi]
    _ = (ring (4 * n + 3 + 1)).zero := by rw [h0]; lib_ring

theorem ni_ne_neg (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : (ring (4 * n + 3 + 1)).neg ι ≠ (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one := fun e => i_ne_one n ι hi (by
  rw [show ι = (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).neg ι) by lib_ring, e]; lib_ring)

theorem four_cases (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (t : Zp (4 * n + 3 + 1)) (ht : (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul t t) t) t = (ring (4 * n + 3 + 1)).one) :
    t = (ring (4 * n + 3 + 1)).one ∨ t = ι ∨ t = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one ∨ t = (ring (4 * n + 3 + 1)).neg ι := by
  have hP : (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).add t ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((ring (4 * n + 3 + 1)).add t (ring (4 * n + 3 + 1)).one)) ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).add t ((ring (4 * n + 3 + 1)).neg ι)) ((ring (4 * n + 3 + 1)).add t ι)) = (ring (4 * n + 3 + 1)).zero := by
    rw [show (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).add t ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((ring (4 * n + 3 + 1)).add t (ring (4 * n + 3 + 1)).one)) ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).add t ((ring (4 * n + 3 + 1)).neg ι)) ((ring (4 * n + 3 + 1)).add t ι))
        = (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul t t) t) t) ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).mul t t) ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((ring (4 * n + 3 + 1)).add ((ring (4  -- (+123 chars; full line in the Zenodo source)
      ht, hi]
    lib_ring
  by_cases h1 : t = (ring (4 * n + 3 + 1)).one
  · exact Or.inl h1
  by_cases h2 : t = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one
  · exact Or.inr (Or.inr (Or.inl h2))
  have hA : (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).add t ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((ring (4 * n + 3 + 1)).add t (ring (4 * n + 3 + 1)).one) ≠ (ring (4 * n + 3 + 1)).zero := fun e =>
    h2 (by
      have := cancel n hpr _ _ (sub_ne n t (ring (4 * n + 3 + 1)).one h1) e
      calc t = (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).add t (ring (4 * n + 3 + 1)).one) ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by lib_ring
        _ = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one := by rw [this]; lib_ring)
  have hB := cancel n hpr _ _ hA hP
  by_cases h3 : t = ι
  · exact Or.inr (Or.inl h3)
  · have := cancel n hpr _ _ (sub_ne n t ι h3) hB
    exact Or.inr (Or.inr (Or.inr (by calc t = (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).add t ι) ((ring (4 * n + 3 + 1)).neg ι) := by lib_ring
      _ = (ring (4 * n + 3 + 1)).neg ι := by rw [this]; lib_ring)))

theorem xN4 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (x : Zp (4 * n + 3 + 1)) (hx : x ≠ (ring (4 * n + 3 + 1)).zero) :
    (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul (pw (ring (4 * n + 3 + 1)) x (n + 1)) (pw (ring (4 * n + 3 + 1)) x (n + 1))) (pw (ring (4 * n + 3 + 1)) x (n + 1))) (pw (ring (4 * n + 3 + 1)) x (n + 1)) = (ring (4 * n + 3 + 1)).one := by
  have e : (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul (pw (ring (4 * n + 3 + 1)) x (n + 1)) (pw (ring (4 * n + 3 + 1)) x (n + 1))) (pw (ring (4 * n + 3 + 1)) x (n + 1))) (pw (ring (4 * n + 3 + 1)) x (n + 1)) = pw (ring (4 * n + 3 + 1)) (pw (ring (4 * n + 3 + 1)) x (n + 1)) 4 := by
    show _ = (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul (ring (4 * n + 3 + 1)).one _) _) _) _; rw [(ring (4 * n + 3 + 1)).one_mul]
  rw [e, TwoPower.pwpow, show (n + 1) * 4 = 4 * n + 3 + 1 by lib_nat]
  exact unit_pow (4 * n + 3) hpr x hx

theorem p2 (ι : Zp (4 * n + 3 + 1)) : pw (ring (4 * n + 3 + 1)) ι 2 = (ring (4 * n + 3 + 1)).mul ι ι := by show (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul (ring (4 * n + 3 + 1)).one ι) ι = _; rw [(ring (4 * n + 3 + 1)).one_mul]

theorem exists_j (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (x : Zp (4 * n + 3 + 1)) (hx : x ≠ (ring (4 * n + 3 + 1)).zero) : ∃ j, j < 4 ∧ pw (ring (4 * n + 3 + 1)) x (n + 1) = pw (ring (4 * n + 3 + 1)) ι j := by
  cases four_cases n hpr ι hi _ (xN4 n hpr x hx) with
  | inl h => exact ⟨0, Nat.zero_lt_succ 3, h⟩
  | inr h =>
    cases h with
    | inl h => exact ⟨1, Nat.le_of_ble_eq_true rfl, by rw [h]; exact ((ring (4 * n + 3 + 1)).one_mul ι).symm⟩
    | inr h =>
      cases h with
      | inl h => exact ⟨2, Nat.le_of_ble_eq_true rfl, by rw [h, p2, hi]⟩
      | inr h => exact ⟨3, Nat.lt_succ_self 3, by rw [h]; show _ = (ring (4 * n + 3 + 1)).mul (pw (ring (4 * n + 3 + 1)) ι 2) ι; rw [p2, hi]; lib_ring⟩

theorem val4 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (x : Zp (4 * n + 3 + 1)) (hx : x ≠ (ring (4 * n + 3 + 1)).zero) (j : Nat) (hj : j < 4) (h : pw (ring (4 * n + 3 + 1)) x (n + 1) = pw (ring (4 * n + 3 + 1)) ι j) :
    chi4 n ι x = pw (Cyclo4.cyc4 LibInt.intR) I j := by
  unfold chi4
  rw [ite_neg' _ _ hx]
  match j, hj with
  | 0, _ => rw [ite_pos' _ _ (show pw (ring (4 * n + 3 + 1)) x (n + 1) = (ring (4 * n + 3 + 1)).one from h)]; rfl
  | 1, _ =>
    have h1 : pw (ring (4 * n + 3 + 1)) x (n + 1) = ι := h.trans ((ring (4 * n + 3 + 1)).one_mul ι)
    rw [ite_neg' _ _ (by rw [h1]; exact i_ne_one n ι hi), ite_pos' _ _ h1]; rfl
  | 2, _ =>
    have h2 : pw (ring (4 * n + 3 + 1)) x (n + 1) = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one := h.trans (by rw [p2, hi])
    rw [ite_neg' _ _ (by rw [h2]; exact n1 n), ite_neg' _ _ (by rw [h2]; exact fun e => i_ne_neg n ι hi e.symm), ite_pos' _ _ h2]; rfl
  | 3, _ =>
    have h3 : pw (ring (4 * n + 3 + 1)) x (n + 1) = (ring (4 * n + 3 + 1)).neg ι := h.trans (by show (ring (4 * n + 3 + 1)).mul (pw (ring (4 * n + 3 + 1)) ι 2) ι = _; rw [p2, hi]; lib_ring)
    rw [ite_neg' _ _ (by rw [h3]; exact ni_ne_one n ι hi), ite_neg' _ _ (by rw [h3]; exact ni_ne_i n hpr ι hi),
      ite_neg' _ _ (by rw [h3]; exact ni_ne_neg n ι hi)]; rfl
  | k + 4, hk => exact absurd hk (fun h => Nat.lt_irrefl 4 (Nat.lt_of_le_of_lt (Nat.le_add_left 4 k) h))

theorem i4 (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : pw (ring (4 * n + 3 + 1)) ι 4 = (ring (4 * n + 3 + 1)).one := by
  show (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul (pw (ring (4 * n + 3 + 1)) ι 2) ι) ι = _
  rw [p2, show (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul ι ι) ι) ι = (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul ι ι) ((ring (4 * n + 3 + 1)).mul ι ι) by lib_ring, hi]; lib_ring

theorem reduce4 (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : ∀ k, ∃ k', k' < 4 ∧ pw (ring (4 * n + 3 + 1)) ι k = pw (ring (4 * n + 3 + 1)) ι k' ∧ pw (Cyclo4.cyc4 LibInt.intR) I k = pw (Cyclo4.cyc4 LibInt.intR) I k'
  | 0 => ⟨0, Nat.zero_lt_succ 3, rfl, rfl⟩
  | k + 1 => by
    match reduce4 ι hi k with
    | ⟨k', hk', e1, e2⟩ =>
      match k', hk' with
      | 0, _ => exact ⟨1, Nat.le_of_ble_eq_true rfl, by show (ring (4 * n + 3 + 1)).mul _ ι = (ring (4 * n + 3 + 1)).mul _ ι; rw [e1], by show (Cyclo4.cyc4 LibInt.intR).mul _ I = (Cyclo4.cyc4 LibInt.intR).mul _ I; rw [e2]⟩
      | 1, _ => exact ⟨2, Nat.le_of_ble_eq_true rfl, by show (ring (4 * n + 3 + 1)).mul _ ι = (ring (4 * n + 3 + 1)).mul _ ι; rw [e1], by show (Cyclo4.cyc4 LibInt.intR).mul _ I = (Cyclo4.cyc4 LibInt.intR).mul _ I; rw [e2]⟩
      | 2, _ => exact ⟨3, Nat.lt_succ_self 3, by show (ring (4 * n + 3 + 1)).mul _ ι = (ring (4 * n + 3 + 1)).mul _ ι; rw [e1], by show (Cyclo4.cyc4 LibInt.intR).mul _ I = (Cyclo4.cyc4 LibInt.intR).mul _ I; rw [e2]⟩
      | 3, _ => exact ⟨0, Nat.zero_lt_succ 3, by show (ring (4 * n + 3 + 1)).mul _ ι = _; rw [e1]; exact i4 n ι hi, by show (Cyclo4.cyc4 LibInt.intR).mul _ I = _; rw [e2]; rfl⟩
      | j + 4, hj => exact absurd hj (fun h => Nat.lt_irrefl 4 (Nat.lt_of_le_of_lt (Nat.le_add_left 4 j) h))

theorem chi4_zero (ι : Zp (4 * n + 3 + 1)) : chi4 n ι (ring (4 * n + 3 + 1)).zero = (0, 0) := by unfold chi4; rw [ite_pos' _ _ rfl]

theorem chi4_mul (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (x y : Zp (4 * n + 3 + 1)) : chi4 n ι ((ring (4 * n + 3 + 1)).mul x y) = (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι x) (chi4 n ι y) := by
  by_cases hx : x = (ring (4 * n + 3 + 1)).zero
  · rw [hx, zero_mul', chi4_zero]; exact (zero_mul' (Cyclo4.cyc4 LibInt.intR) _).symm
  by_cases hy : y = (ring (4 * n + 3 + 1)).zero
  · rw [hy, mul_zero, chi4_zero]; exact (mul_zero (Cyclo4.cyc4 LibInt.intR) _).symm
  have hxy : (ring (4 * n + 3 + 1)).mul x y ≠ (ring (4 * n + 3 + 1)).zero := fun e => hy (cancel n hpr x y hx e)
  match exists_j n hpr ι hi x hx, exists_j n hpr ι hi y hy with
  | ⟨i, hi', ei⟩, ⟨j, hj, ej⟩ =>
    match reduce4 n ι hi (i + j) with
    | ⟨k, hk, e1, e2⟩ =>
      have hxyN : pw (ring (4 * n + 3 + 1)) ((ring (4 * n + 3 + 1)).mul x y) (n + 1) = pw (ring (4 * n + 3 + 1)) ι k := by
        rw [pw_mul, ei, ej, ← HasseCM.pw_add', e1]
      rw [val4 n hpr ι hi _ hxy k hk hxyN, val4 n hpr ι hi x hx i hi' ei, val4 n hpr ι hi y hy j hj ej, ← e2, HasseCM.pw_add']

theorem chi4_neg1 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : ∃ k, k < 4 ∧ chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) = pw (Cyclo4.cyc4 LibInt.intR) I k ∧ pw (Cyclo4.cyc4 LibInt.intR) I (2 * (n + 1)) = pw (Cyclo4.cyc4 LibInt.intR)   -- (+9 chars; full line in the Zenodo source)
  have hne : (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one ≠ (ring (4 * n + 3 + 1)).zero := fun e => PowerSum.one_ne_zero' (4 * n + 3) (by
    calc (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := (neg_neg' (ring (4 * n + 3 + 1)) _).symm
      _ = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).zero := by rw [e]
      _ = (ring (4 * n + 3 + 1)).zero := by lib_ring)
  match reduce4 n ι hi (2 * (n + 1)) with
  | ⟨k, hk, e1, e2⟩ =>
    have h : pw (ring (4 * n + 3 + 1)) ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (n + 1) = pw (ring (4 * n + 3 + 1)) ι k := by
      rw [← hi, ← p2, TwoPower.pwpow, e1]
    exact ⟨k, hk, val4 n hpr ι hi _ hne k hk h, e2⟩

theorem int2 (b : Int) (h : b + b = 0) : b = 0 := QChar.int_two_zero b h

/-- (1 − i^j) S = 0 for j = 1, 2, 3 ⇒ S = 0 in ℤ[i]. -/
theorem kill4 (S c : Int × Int) (hc : c = pw (Cyclo4.cyc4 LibInt.intR) I 1 ∨ c = pw (Cyclo4.cyc4 LibInt.intR) I 2 ∨ c = pw (Cyclo4.cyc4 LibInt.intR) I 3) (h : (Cyclo4.cyc4 LibInt.intR).mul c S = S) : S = (Cyclo4.cyc4 LibInt.intR).zero := by
  match S, hc with
  | (a, b), Or.inl hc =>
    rw [hc] at h
    have h1 : (Cyclo4.c4mul LibInt.intR (0, 1) (a, b)).1 = a := congrArg Prod.fst h
    have h2 : (Cyclo4.c4mul LibInt.intR (0, 1) (a, b)).2 = b := congrArg Prod.snd h
    change 0 * a + -(1 * b) = a at h1
    change 0 * b + 1 * a = b at h2
    have hb' : b = a := by rw [← h2]; lib_int
    rw [hb'] at h1
    have ha : a = 0 := int2 a (by rw [show a + a = a - (0 * a + -(1 * a)) by lib_int, h1]; lib_int)
    have hb : b = 0 := by rw [hb', ha]
    rw [ha, hb]; rfl
  | (a, b), Or.inr (Or.inl hc) =>
    rw [hc] at h
    have h1 : (Cyclo4.c4mul LibInt.intR (-1, 0) (a, b)).1 = a := congrArg Prod.fst h
    have h2 : (Cyclo4.c4mul LibInt.intR (-1, 0) (a, b)).2 = b := congrArg Prod.snd h
    change -1 * a + -(0 * b) = a at h1
    change -1 * b + 0 * a = b at h2
    have ha : a = 0 := int2 a (by rw [show a + a = a - (-1 * a + -(0 * b)) by lib_int, h1]; lib_int)
    have hb : b = 0 := int2 b (by rw [show b + b = b - (-1 * b + 0 * a) by lib_int, h2]; lib_int)
    rw [ha, hb]; rfl
  | (a, b), Or.inr (Or.inr hc) =>
    rw [hc] at h
    have h1 : (Cyclo4.c4mul LibInt.intR (0, -1) (a, b)).1 = a := congrArg Prod.fst h
    have h2 : (Cyclo4.c4mul LibInt.intR (0, -1) (a, b)).2 = b := congrArg Prod.snd h
    change 0 * a + -(-1 * b) = a at h1
    change 0 * b + -1 * a = b at h2
    have ha' : a = b := by rw [← h1]; lib_int
    rw [ha'] at h2
    have hb : b = 0 := int2 b (by rw [show b + b = b - (0 * b + -1 * b) by lib_int, h2]; lib_int)
    have ha : a = 0 := by rw [ha', hb]
    rw [ha, hb]; rfl

def LL : List (Zp (4 * n + 3 + 1)) := CMInert.allZ (4 * n + 3 + 1) ((4 * n + 3 + 1) + 1)

/-- **Σ_x χ(x) = 0.** -/
theorem chi4_sum (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : sumK (Cyclo4.cyc4 LibInt.intR) (chi4 n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := by
  match PowerSum.exists_nonroot (4 * n + 3) hpr n (by rw [show 4 * n + 3 = (n + 1) + (3 * n + 2) by lib_nat]; exact Nat.le_add_right _ _) with
  | ⟨x0, hx0, hn⟩ =>
    have hre := reindex (Cyclo4.cyc4 LibInt.intR) _ (CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR)) ((ring (4 * n + 3 + 1)).mul x0) ((ring (4 * n + 3 + 1)).mul (pw (ring (4 * n + 3 + 1)) x0 (4 * n + 3)))
      (fun x => by rw [← (ring (4 * n + 3 + 1)).mul_assoc, inv_exists (4 * n + 3) hpr x0 hx0, (ring (4 * n + 3 + 1)).one_mul])
      (fun y => by rw [← (ring (4 * n + 3 + 1)).mul_assoc, (ring (4 * n + 3 + 1)).mul_comm x0, inv_exists (4 * n + 3) hpr x0 hx0, (ring (4 * n + 3 + 1)).one_mul]) (chi4 n ι)
    have e : sumK (Cyclo4.cyc4 LibInt.intR) (fun x => chi4 n ι ((ring (4 * n + 3 + 1)).mul x0 x)) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι x0) (sumK (Cyclo4.cyc4 LibInt.intR) (chi4 n ι) (LL n)) := by
      rw [mul_sumK]; exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun x => chi4_mul n hpr ι hi x0 x) _
    have hre' : (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι x0) (sumK (Cyclo4.cyc4 LibInt.intR) (chi4 n ι) (LL n)) = sumK (Cyclo4.cyc4 LibInt.intR) (chi4 n ι) (LL n) := e.symm.trans hre
    match exists_j n hpr ι hi x0 hx0 with
    | ⟨j, hj, ej⟩ =>
      rw [val4 n hpr ι hi x0 hx0 j hj ej] at hre'
      match j, hj with
      | 0, _ => exact absurd ej hn
      | 1, _ => exact kill4 _ _ (Or.inl rfl) hre'
      | 2, _ => exact kill4 _ _ (Or.inr (Or.inl rfl)) hre'
      | 3, _ => exact kill4 _ _ (Or.inr (Or.inr rfl)) hre'
      | k + 4, hk => exact absurd hk (fun h => Nat.lt_irrefl 4 (Nat.lt_of_le_of_lt (Nat.le_add_left 4 k) h))

end QuartChar

#print axioms QuartChar.cancel
#print axioms QuartChar.n1
#print axioms QuartChar.i_ne_one
#print axioms QuartChar.i_ne_neg
#print axioms QuartChar.ni_ne_one
#print axioms QuartChar.ni_ne_i
#print axioms QuartChar.ni_ne_neg
#print axioms QuartChar.four_cases
#print axioms QuartChar.xN4
#print axioms QuartChar.exists_j
#print axioms QuartChar.val4
#print axioms QuartChar.i4
#print axioms QuartChar.reduce4
#print axioms QuartChar.chi4_mul
#print axioms QuartChar.chi4_neg1
#print axioms QuartChar.kill4
#print axioms QuartChar.chi4_sum

lang/QuartCyclo.lean

lean_all/lang/QuartCyclo.lean

/-
  lang/QuartCyclo — T43, cycle 242: all sixteen cyclotomic numbers of order 4.

  p = 4n + 5, χ quartic, ρ = χ², J = J(χ,χ), K = J(ρ,χ) = χ(−1)J (QuartDup). For fourth roots of unity v, u:
      4[χ(x) = v] + [x = 0] = 1 + v̄χ(x) + v̄²ρ(x) + vχ̄(x)      (E4_val)
      cyclo4_vu   16·#{a : χ(a) = v, χ(1 − a) = u} = p + v̄ūJ + vuJ̄ − v̄²ū² − χ(−1)(v̄u + vū) + (v̄ū² + v̄²ū)K + (vū² + v̄²u)K̄
                                                     − (1 + v̄ + v̄² + v) − (1 + ū + ū² + u)
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.FermatQuartic

namespace QuartCyclo

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius QuartChar QuartJacobi QuartPrimary QuartDup FermatQuartic
open LibRing (castZ castN)

variable (n : Nat)

def Iv (ι : Zp (4 * n + 3 + 1)) (v : Int × Int) (x : Zp (4 * n + 3 + 1)) : Int := if chi4 n ι x = v then 1 else 0

def E4 (ι : Zp (4 * n + 3 + 1)) (v : Int × Int) (x : Zp (4 * n + 3 + 1)) : Int × Int :=
  (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (chi4 n ι x))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (gconj v)) (rho n ι x))) ((Cyclo4.cyc4 LibInt.intR).mul v (chib n ι x))

def R4 (v : Int × Int) : Prop := v = (1, 0) ∨ v = (0, 1) ∨ v = (-1, 0) ∨ v = (0, -1)

theorem E4_val (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (v : Int × Int) (hv : R4 v) (x : Zp (4 * n + 3 + 1)) :
    E4 n ι v x = (4 * Iv n ι v x + delta LibInt.intR x (ring (4 * n + 3 + 1)).zero, 0) := by
  unfold E4 Iv rho
  rw [chib_eq n hpr ι hi]
  by_cases hx : x = (ring (4 * n + 3 + 1)).zero
  · rw [hx, chi4_zero]; unfold delta; rw [ite_pos' _ _ rfl]
    rcases hv with e | e | e | e <;> rw [e] <;> rfl
  · unfold delta; rw [ite_neg' _ _ hx]
    rcases ResidueCriterion.chi4_cases n ι x with e | e | e | e | e
    · exact absurd e (chi4_ne n hpr ι hi x hx)
    all_goals (rw [e]; rcases hv with f | f | f | f <;> rw [f] <;> rfl)

theorem pair_sum {A : Type} (g : A → Int) : ∀ l : List A, sumK (Cyclo4.cyc4 LibInt.intR) (fun x => (g x, (0 : Int))) l = (sumK LibInt.intR g l, 0)
  | [] => rfl
  | a :: as => by show (Cyclo4.cyc4 LibInt.intR).add (g a, 0) (sumK (Cyclo4.cyc4 LibInt.intR) (fun x => (g x, (0 : Int))) as) = _; rw [pair_sum g as]; exact Prod.ext rfl (show (0 : Int) + 0 = 0 from rfl)

theorem sC_neg (f : Zp (4 * n + 3 + 1) → Int × Int) : ∀ l : List (Zp (4 * n + 3 + 1)), sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).neg (f a)) l = (Cyclo4.cyc4 LibInt.intR).neg (sumK (Cyclo4.cyc4 LibInt.intR) f l)
  | [] => by show (Cyclo4.cyc4 LibInt.intR).zero = (Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).zero; lib_ring
  | a :: as => by show (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).neg (f a)) (sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).neg (f a)) as) = (Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).add (f a) (sumK (Cyclo4.cyc4 LibInt.intR) f as)); rw [sC_neg f as]; lib_ring

/-- **Gauss's cyclotomic numbers of order 4.** -/
theorem cyclo4_vu (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (v u : Int × Int) (hv : R4 v) (hu : R4 u) :
    (16 * sumK LibInt.intR (fun a => Iv n ι v a * Iv n ι u (om n a)) (LL n), (0 : Int)) =
      (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (Int.ofNat (4 * n + 3 + 1 + 1), 0)
        ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (gconj u)) (Jq n (chi4 n ι)))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul v u) (gconj (Jq n (chi4 n ι)))))
        ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (gconj v)) ((Cyclo4.cyc4 LibInt.intR).mul (gconj u) (gconj u)))))
        ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) u) ((Cyclo4.cyc4 LibInt.intR).mul v (gconj u))) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one))))
        ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) ((Cyclo4.cyc4 LibInt.intR).mul (gconj u) (gconj u))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (gconj v)) (gconj u))) (J2 n ι)))
        ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul v ((Cyclo4.cyc4 LibInt.intR).mul (gconj u) (gconj u))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (gconj v)) u)) (gconj (J2 n ι))))
        ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one (gconj v)) ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (gconj v))) v)))
        ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one (gconj u)) ((Cyclo4.cyc4 LibInt.intR).mul (gconj u) (gconj u))) u)) := by
  have om0 : om n (ring (4 * n + 3 + 1)).zero = (ring (4 * n + 3 + 1)).one := by unfold om; lib_ring
  have om1' : om n (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).zero := by unfold om; lib_ring
  have hL : (16 * sumK LibInt.intR (fun a => Iv n ι v a * Iv n ι u (om n a)) (LL n), (0 : Int))
      = sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add (E4 n ι v a) ((Cyclo4.cyc4 LibInt.intR).neg (delta (Cyclo4.cyc4 LibInt.intR) a (ring (4 * n + 3 + 1)).zero))) ((Cyclo4.cyc4 LibInt.intR).add (E4 n ι u (om n a)) ((Cyclo4.cyc4 LibInt.intR).neg (delta (Cyclo4.cyc4 LibInt.intR) a (ring (4 * n + 3 + 1)).one)))) (LL n) := by
    rw [← QuartPrimary.sI_mul, ← pair_sum]
    exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by
      rw [E4_val n hpr ι hi v hv, E4_val n hpr ι hi u hu]
      have d1 : delta LibInt.intR (om n a) (ring (4 * n + 3 + 1)).zero = delta LibInt.intR a (ring (4 * n + 3 + 1)).one := by
        unfold delta
        by_cases h1 : a = (ring (4 * n + 3 + 1)).one
        · rw [ite_pos' _ _ h1, ite_pos' _ _ (by rw [h1]; exact om1')]
        · rw [ite_neg' _ _ h1, ite_neg' _ _ (fun e => h1 (by rw [← om_om n a, e]; exact om0))]
      rw [d1]
      have dC : ∀ z : Zp (4 * n + 3 + 1), delta (Cyclo4.cyc4 LibInt.intR) a z = (delta LibInt.intR a z, 0) := fun z => by
        unfold delta; by_cases hz : a = z
        · rw [ite_pos' _ _ hz, ite_pos' _ _ hz]; rfl
        · rw [ite_neg' _ _ hz, ite_neg' _ _ hz]; rfl
      rw [dC, dC]
      apply Prod.ext
      · show 16 * (Iv n ι v a * Iv n ι u (om n a)) = (4 * Iv n ι v a + delta LibInt.intR a (ring (4 * n + 3 + 1)).zero + -delta LibInt.intR a (ring (4 * n + 3 + 1)).zero) * (4 * Iv n ι u (om n a) + delta LibInt.intR a (ring (4 * n + 3 + 1)).one + -delta LibInt.intR a (ring (4 * n + 3 + 1)).one) + -((0 + -0) * (0 + -0))
        lib_int
      · show (0 : Int) = (4 * Iv n ι v a + delta LibInt.intR a (ring (4 * n + 3 + 1)).zero + -delta LibInt.intR a (ring (4 * n + 3 + 1)).zero) * (0 + -0) + (0 + -0) * (4 * Iv n ι u (om n a) + delta LibInt.intR a (ring (4 * n + 3 + 1)).one + -delta LibInt.intR a (ring (4 * n + 3 + 1)).one)
        lib_int) _
  rw [hL]
  -- expand into the sixteen sums and the boundary terms
  have hexp : ∀ a, (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add (E4 n ι v a) ((Cyclo4.cyc4 LibInt.intR).neg (delta (Cyclo4.cyc4 LibInt.intR) a (ring (4 * n + 3 + 1)).zero))) ((Cyclo4.cyc4 LibInt.intR).add (E4 n ι u (om n a)) ((Cyclo4.cyc4 LibInt.intR).neg (delta (Cyclo4.cyc4 LibInt.intR) a (ring (4 * n + 3 + 1)).one)))
      = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one
        ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (gconj u) (chi4 n ι (om n a))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj u) (gconj u)) (rho n ι (om n a)))) ((Cyclo4.cyc4 LibInt.intR).mul u (chib n ι (om n a)))))
        ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (chi4 n ι a)) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (gconj v)) (rho n ι a))) ((Cyclo4.cyc4 LibInt.intR).mul v (chib n ι a))))
        ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (gconj u)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (chi4 n ι (om n a)))))
        ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul v u) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (chib n ι (om n a)))))
        ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (gconj v)) ((Cyclo4.cyc4 LibInt.intR).mul (gconj u) (gconj u))) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (rho n ι (om n a)))))
        ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) u) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (chib n ι (om n a)))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul v (gconj u)) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (chi4 n ι (om n a))))))
        ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) ((Cyclo4.cyc4 LibInt.intR).mul (gconj u) (gconj u))) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (rho n ι (om n a)))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (gconj v)) (gconj u)) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (ch  -- (+20 chars; full line in the Zenodo source)
        ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul v ((Cyclo4.cyc4 LibInt.intR).mul (gconj u) (gconj u))) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (rho n ι (om n a)))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (gconj v)) u) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (chib n ι (om n a))  -- (+4 chars; full line in the Zenodo source)
        ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) a (ring (4 * n + 3 + 1)).zero) (E4 n ι u (om n a)))))
        ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) a (ring (4 * n + 3 + 1)).one) (E4 n ι v a))))
        ((Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) a (ring (4 * n + 3 + 1)).zero) (delta (Cyclo4.cyc4 LibInt.intR) a (ring (4 * n + 3 + 1)).one)) := fun a => by unfold E4; lib_ring
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ hexp]
  repeat rw [sumK_add]
  have hp : ∀ (c : Int × Int) (f : Zp (4 * n + 3 + 1) → Int × Int), sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul c (f a)) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul c (sumK (Cyclo4.cyc4 LibInt.intR) f (LL n)) := fun c f => (mul_sumK (Cyclo4.cyc4 LibInt.intR) c f (LL n)).symm
  have hcomp : Complete (Cyclo4.cyc4 LibInt.intR) (LL n) := CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR)
  have SA : sumK (Cyclo4.cyc4 LibInt.intR) (chi4 n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := chi4_sum n hpr ι hi
  have SR : sumK (Cyclo4.cyc4 LibInt.intR) (rho n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := rho_sum n hpr ι hi
  have SB : sumK (Cyclo4.cyc4 LibInt.intR) (chib n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := chib_sum n hpr ι hi
  have c1 : chi4 n ι (ring (4 * n + 3 + 1)).one = (Cyclo4.cyc4 LibInt.intR).one := chi_one n hpr ι hi
  have r1 : rho n ι (ring (4 * n + 3 + 1)).one = (Cyclo4.cyc4 LibInt.intR).one := rho_one n hpr ι hi
  have b1 : chib n ι (ring (4 * n + 3 + 1)).one = (Cyclo4.cyc4 LibInt.intR).one := chib_one n hpr ι hi
  have E1 : ∀ x : Int × Int, E4 n ι x (ring (4 * n + 3 + 1)).one = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one (gconj x)) ((Cyclo4.cyc4 LibInt.intR).mul (gconj x) (gconj x))) x := fun x => by unfold E4; rw [c1, r1, b1]; lib_ring
  have T1 : sumK (Cyclo4.cyc4 LibInt.intR) (fun _ : Zp (4 * n + 3 + 1) => (Cyclo4.cyc4 LibInt.intR).one) (LL n) = (Int.ofNat (4 * n + 3 + 1 + 1), 0) := sum_oneq n _
  have Tu1 : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (gconj u) (chi4 n ι (om n a))) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := by rw [hp, refl_om n (chi4 n ι), SA]; lib_ring
  have Tu2 : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj u) (gconj u)) (rho n ι (om n a))) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := by rw [hp, refl_om n (rho n ι), SR]; lib_ring
  have Tu3 : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul u (chib n ι (om n a))) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := by rw [hp, refl_om n (chib n ι), SB]; lib_ring
  have Tv1 : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (gconj v) (chi4 n ι a)) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := by rw [hp, SA]; lib_ring
  have Tv2 : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (gconj v)) (rho n ι a)) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := by rw [hp, SR]; lib_ring
  have Tv3 : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul v (chib n ι a)) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := by rw [hp, SB]; lib_ring
  have TAA : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (gconj u)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (chi4 n ι (om n a)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (gconj u)) (Jq n (chi4 n ι)) := by rw [hp]; rfl
  have TBB : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul v u) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (chib n ι (om n a)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul v u) (gconj (Jq n (chi4 n ι))) := by
    rw [hp, ← Jb_conj n hpr ι hi]; rfl
  have TRR : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (gconj v)) ((Cyclo4.cyc4 LibInt.intR).mul (gconj u) (gconj u))) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (rho n ι (om n a)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (gcon  -- (+64 chars; full line in the Zenodo source)
    rw [hp, J_rho n hpr ι hi]; lib_ring
  have SAB := FermatQuartic.T13 n hpr ι hi
  have SBA : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (chi4 n ι (om n a))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one) := by
    rw [← SAB, ← refl_om n]; exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by rw [om_om]; exact (Cyclo4.cyc4 LibInt.intR).mul_comm _ _) _
  have TAB : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) u) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (chib n ι (om n a)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) u) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR  -- (+56 chars; full line in the Zenodo source)
  have TBA : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul v (gconj u)) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (chi4 n ι (om n a)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul v (gconj u)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR  -- (+56 chars; full line in the Zenodo source)
  have SRA : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (chi4 n ι (om n a))) (LL n) = J2 n ι := rfl
  have SAR : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (rho n ι (om n a))) (LL n) = J2 n ι := by
    rw [← SRA, ← refl_om n]; exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by rw [om_om]; exact (Cyclo4.cyc4 LibInt.intR).mul_comm _ _) _
  have SRB : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (chib n ι (om n a))) (LL n) = gconj (J2 n ι) := by
    unfold J2; rw [gconj_sum]
    exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by rw [gconj_mul, gconj_rho n hpr ι hi, chib_eq n hpr ι hi]) _
  have SBR : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (rho n ι (om n a))) (LL n) = gconj (J2 n ι) := by
    rw [← SRB, ← refl_om n]; exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by rw [om_om]; exact (Cyclo4.cyc4 LibInt.intR).mul_comm _ _) _
  have TAR : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) ((Cyclo4.cyc4 LibInt.intR).mul (gconj u) (gconj u))) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (rho n ι (om n a)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) ((Cyclo4.cyc4 LibInt.intR).mul (gconj u) (gconj u))) (J2 n ι) := by rw [hp, S  -- (+3 chars; full line in the Zenodo source)
  have TRA : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (gconj v)) (gconj u)) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (chi4 n ι (om n a)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (gconj v)) (gconj u)) (J2 n ι) := by rw [hp];   -- (+3 chars; full line in the Zenodo source)
  have TBR : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul v ((Cyclo4.cyc4 LibInt.intR).mul (gconj u) (gconj u))) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (rho n ι (om n a)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul v ((Cyclo4.cyc4 LibInt.intR).mul (gconj u) (gconj u))) (gconj (J2 n ι)) := by rw [hp, SBR]
  have TRB : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (gconj v)) u) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (chib n ι (om n a)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (gconj v)) u) (gconj (J2 n ι)) := by rw [hp, SRB]
  have T10 : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) a (ring (4 * n + 3 + 1)).zero) (E4 n ι u (om n a)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).neg (E4 n ι u (ring (4 * n + 3 + 1)).one) := by
    rw [sC_neg, FiniteRecChain.pick (Cyclo4.cyc4 LibInt.intR) (LL n) hcomp (fun a => E4 n ι u (om n a)) (ring (4 * n + 3 + 1)).zero, om0]
  have T11 : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) a (ring (4 * n + 3 + 1)).one) (E4 n ι v a))) (LL n) = (Cyclo4.cyc4 LibInt.intR).neg (E4 n ι v (ring (4 * n + 3 + 1)).one) := by
    rw [sC_neg, FiniteRecChain.pick (Cyclo4.cyc4 LibInt.intR) (LL n) hcomp (E4 n ι v) (ring (4 * n + 3 + 1)).one]
  have T12 : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) a (ring (4 * n + 3 + 1)).zero) (delta (Cyclo4.cyc4 LibInt.intR) a (ring (4 * n + 3 + 1)).one)) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := by
    rw [FiniteRecChain.pick (Cyclo4.cyc4 LibInt.intR) (LL n) hcomp (fun a => delta (Cyclo4.cyc4 LibInt.intR) a (ring (4 * n + 3 + 1)).one) (ring (4 * n + 3 + 1)).zero]
    unfold delta; exact ite_neg' _ _ (fun e => PowerSum.one_ne_zero' (4 * n + 3) e.symm)
  rw [T1, Tu1, Tu2, Tu3, Tv1, Tv2, Tv3, TAA, TBB, TRR, TAB, TBA, TAR, TRA, TBR, TRB, T10, T11, T12, E1, E1]
  lib_ring

end QuartCyclo
#print axioms QuartCyclo.E4_val
#print axioms QuartCyclo.pair_sum
#print axioms QuartCyclo.sC_neg
#print axioms QuartCyclo.cyclo4_vu

lang/QuartDiagonal.lean

lean_all/lang/QuartDiagonal.lean

/-
  lang/QuartDiagonal — T43, cycle 254: the diagonal quartic hypersurfaces x₁⁴ + … + x_k⁴ = c in every dimension.

  N_0(c) = [c = 0], N_{k+1}(c) = Σ_a N(a)N_k(c − a), N = 1 + χ + ρ + χ̄. For every k, with s = χ(−1), K = J(ρ, χ):
      N_k(c) = A_k + B_k χ̄(c) + B2_k ρ(c) + B3_k χ(c)   (c ≠ 0),   N_k(0) = C_k,
      A' = (p − 1)A − sB − B2 − sB3 + C,  B' = (B2 + B3)K + C − A,  B2' = B J̄ + B3 J + C − A,  B3' = (B + B2)K̄ + C − A,
      C' = (p − 1)(A + sB + B2 + sB3) + C.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.QuartSurface

namespace QuartDiagonal

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius QuartChar QuartJacobi QuartPrimary QuartDup FermatQuartic QuartTwist QuartTwistCount QuartSurface
open LibRing (castZ castN)

variable (n : Nat)

def Nk (ι : Zp (4 * n + 3 + 1)) : Nat → Zp (4 * n + 3 + 1) → Int × Int
  | 0, c => delta (Cyclo4.cyc4 LibInt.intR) c (ring (4 * n + 3 + 1)).zero
  | k + 1, c => sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (Nk ι k ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a)))) (LL n)

theorem hp (k : Int × Int) (f : Zp (4 * n + 3 + 1) → Int × Int) : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul k (f a)) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul k (sumK (Cyclo4.cyc4 LibInt.intR) f (LL n)) := (mul_sumK (Cyclo4.cyc4 LibInt.intR) k f (LL n)).symm
theorem hq (k : Int × Int) (f : Zp (4 * n + 3 + 1) → Int × Int) : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (f a) k) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul (sumK (Cyclo4.cyc4 LibInt.intR) f (LL n)) k := (sumK_mul (Cyclo4.cyc4 LibInt.intR) k f (LL n)).symm

theorem SBA (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (chi4 n ι (om n a))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n +   -- (+81 chars; full line in the Zenodo source)
  rw [← FermatQuartic.T13 n hpr ι hi, ← refl_om n]; exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by rw [om_om]; exact (Cyclo4.cyc4 LibInt.intR).mul_comm _ _) _
theorem SAR (ι : Zp (4 * n + 3 + 1)) : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (rho n ι (om n a))) (LL n) = J2 n ι := by
  rw [← refl_om n]; exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by rw [om_om]; exact (Cyclo4.cyc4 LibInt.intR).mul_comm _ _) _
theorem SRB (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (chib n ι (om n a))) (LL n) = gconj (J2 n ι) := by
  unfold J2; rw [gconj_sum]
  exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by rw [gconj_mul, gconj_rho n hpr ι hi, chib_eq n hpr ι hi]) _
theorem SBR (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (rho n ι (om n a))) (LL n) = gconj (J2 n ι) := by
  rw [← SRB n hpr ι hi, ← refl_om n]; exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by rw [om_om]; exact (Cyclo4.cyc4 LibInt.intR).mul_comm _ _) _
theorem SBB (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (chib n ι (om n a))) (LL n) = gconj (Jq n (chi4 n ι)) := by rw [← Jb_conj n hpr ι hi]; rfl

theorem scale (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (c : Zp (4 * n + 3 + 1)) (hc : c ≠ (ring (4 * n + 3 + 1)).zero) (F : Zp (4 * n + 3 + 1) → Int × Int) :
    sumK (Cyclo4.cyc4 LibInt.intR) F (LL n) = sumK (Cyclo4.cyc4 LibInt.intR) (fun t => F ((ring (4 * n + 3 + 1)).mul c t)) (LL n) := (CharSumGen.sum_scale (Cyclo4.cyc4 LibInt.intR) (4 * n + 2) hpr F c hc).symm

theorem hct (c t : Zp (4 * n + 3 + 1)) : (ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul c t)) = (ring (4 * n + 3 + 1)).mul c (om n t) := by unfold om; lib_ring

theorem Q1c (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (c : Zp (4 * n + 3 + 1)) (hc : c ≠ (ring (4 * n + 3 + 1)).zero) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (chi4 n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a)))) (LL n)
      = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι c) (Jq n (chi4 n ι))) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι c) (J2 n ι))) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one)) := by
  obtain ⟨v1, _, _⟩ := vals n hpr ι hi c hc
  rw [scale n hpr c hc]
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun t => (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι c) (chi4 n ι (om n t))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι c) (chi4 n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι t) (chi4 n ι (om n t)))))
      ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι c) (chi4 n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι t) (chi4 n ι (om n t))))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι c) (chi4 n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι t) (chi4 n ι (om n t))))) (fun t => by
    rw [hct]; unfold g4; rw [chi4_mul n hpr ι hi, chi4_mul n hpr ι hi, rho_mul n hpr ι hi, chib_mul n hpr ι hi]; lib_ring)]
  rw [sumK_add, sumK_add, sumK_add, hp, hp, hp, hp, refl_om n (chi4 n ι), show sumK (Cyclo4.cyc4 LibInt.intR) (chi4 n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero from chi4_sum n hpr ι hi,
    show sumK (Cyclo4.cyc4 LibInt.intR) (fun t => (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι t) (chi4 n ι (om n t))) (LL n) = Jq n (chi4 n ι) from rfl,
    show sumK (Cyclo4.cyc4 LibInt.intR) (fun t => (Cyclo4.cyc4 LibInt.intR).mul (rho n ι t) (chi4 n ι (om n t))) (LL n) = J2 n ι from rfl, SBA n hpr ι hi,
    show (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι c) (chi4 n ι c) = rho n ι c from rfl, show (Cyclo4.cyc4 LibInt.intR).mul (rho n ι c) (chi4 n ι c) = chib n ι c by rw [(Cyclo4.cyc4 LibInt.intR).mul_comm]; exact chi_rho n hpr ι hi c,
    show (Cyclo4.cyc4 LibInt.intR).mul (chib n ι c) (chi4 n ι c) = (Cyclo4.cyc4 LibInt.intR).one by rw [(Cyclo4.cyc4 LibInt.intR).mul_comm]; exact v1]
  lib_ring

theorem Q2c (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (c : Zp (4 * n + 3 + 1)) (hc : c ≠ (ring (4 * n + 3 + 1)).zero) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (rho n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a)))) (LL n)
      = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι c) (J2 n ι)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι c) (gconj (J2 n ι))) := by
  obtain ⟨_, _, v3⟩ := vals n hpr ι hi c hc
  rw [scale n hpr c hc]
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun t => (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι c) (rho n ι (om n t))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι c) (rho n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι t) (rho n ι (om n t)))))
      ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι c) (rho n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι t) (rho n ι (om n t))))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι c) (rho n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι t) (rho n ι (om n t))))) (fun t => by
    rw [hct]; unfold g4; rw [chi4_mul n hpr ι hi, rho_mul n hpr ι hi, rho_mul n hpr ι hi, chib_mul n hpr ι hi]; lib_ring)]
  rw [sumK_add, sumK_add, sumK_add, hp, hp, hp, hp, refl_om n (rho n ι), show sumK (Cyclo4.cyc4 LibInt.intR) (rho n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero from rho_sum n hpr ι hi,
    SAR n ι, J_rho n hpr ι hi, SBR n hpr ι hi, chi_rho n hpr ι hi c, chib_rho n hpr ι hi c, v3]
  lib_ring

theorem Q3c (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (c : Zp (4 * n + 3 + 1)) (hc : c ≠ (ring (4 * n + 3 + 1)).zero) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (chib n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a)))) (LL n)
      = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι c) (gconj (J2 n ι)))) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι c) (gconj (Jq n (chi4 n ι)))) := by
  obtain ⟨v1, v2, _⟩ := vals n hpr ι hi c hc
  rw [scale n hpr c hc]
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun t => (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι c) (chib n ι (om n t))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι c) (chib n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι t) (chib n ι (om n t)))))
      ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι c) (chib n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι t) (chib n ι (om n t))))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι c) (chib n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι t) (chib n ι (om n t))))) (fun t => by
    rw [hct]; unfold g4; rw [chi4_mul n hpr ι hi, rho_mul n hpr ι hi, chib_mul n hpr ι hi, chib_mul n hpr ι hi]; lib_ring)]
  rw [sumK_add, sumK_add, sumK_add, hp, hp, hp, hp, refl_om n (chib n ι), show sumK (Cyclo4.cyc4 LibInt.intR) (chib n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero from chib_sum n hpr ι hi,
    FermatQuartic.T13 n hpr ι hi, SRB n hpr ι hi, SBB n hpr ι hi, v1, v2,
    show (Cyclo4.cyc4 LibInt.intR).mul (rho n ι c) (chib n ι c) = chi4 n ι c by rw [(Cyclo4.cyc4 LibInt.intR).mul_comm]; exact chib_rho n hpr ι hi c]
  lib_ring

theorem one_minus (_hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one ((Cyclo4.cyc4 LibInt.intR).neg (delta (Cyclo4.cyc4 LibInt.intR) a (ring (4 * n + 3 + 1)).zero))) (LL n) = (Cyclo4.cyc4 LibInt.intR).add (Int.ofNat (4 * n + 3 + 1 + 1), 0) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo  -- (+30 chars; full line in the Zenodo source)
  have T1 : sumK (Cyclo4.cyc4 LibInt.intR) (fun _ : Zp (4 * n + 3 + 1) => (Cyclo4.cyc4 LibInt.intR).one) (LL n) = (Int.ofNat (4 * n + 3 + 1 + 1), 0) := sum_oneq n _
  rw [sumK_add, QuartCyclo.sC_neg n, T1, show sumK (Cyclo4.cyc4 LibInt.intR) (fun a => delta (Cyclo4.cyc4 LibInt.intR) a (ring (4 * n + 3 + 1)).zero) (LL n) = (Cyclo4.cyc4 LibInt.intR).one from CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR) (ring (4 * n + 3 + 1)).zero]

theorem pt_vals (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (a : Zp (4 * n + 3 + 1)) :
    (Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (chi4 n ι a) = (Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one ((Cyclo4.cyc4 LibInt.intR).neg (delta (Cyclo4.cyc4 LibInt.intR) a (ring (4 * n + 3 + 1)).zero)) ∧
    (Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (rho n ι a) = (Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one ((Cyclo4.cyc4 LibInt.intR).neg (delta (Cyclo4.cyc4 LibInt.intR) a (ring (4 * n + 3 + 1)).zero)) ∧
    (Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (chib n ι a) = rho n ι a := by
  by_cases ha : a = (ring (4 * n + 3 + 1)).zero
  · rw [ha, chib_zero, rho_zero, show chi4 n ι (ring (4 * n + 3 + 1)).zero = (Cyclo4.cyc4 LibInt.intR).zero from chi4_zero n ι]; unfold delta; rw [ite_pos' _ _ rfl]
    exact ⟨by lib_ring, by lib_ring, by lib_ring⟩
  · obtain ⟨v1, v2, v3⟩ := vals n hpr ι hi a ha
    unfold delta; rw [ite_neg' _ _ ha]
    exact ⟨by rw [(Cyclo4.cyc4 LibInt.intR).mul_comm, v1]; lib_ring, by rw [v3]; lib_ring, v2⟩

theorem Zs (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (chi4 n ι ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).zero ((ring (4 * n + 3 + 1)).neg a)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).add (Int.ofNat (4 * n + 3 + 1 + 1), 0) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc  -- (+22 chars; full line in the Zenodo source)
    sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (rho n ι ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).zero ((ring (4 * n + 3 + 1)).neg a)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).add (Int.ofNat (4 * n + 3 + 1 + 1), 0) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one) ∧
    sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (chib n ι ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).zero ((ring (4 * n + 3 + 1)).neg a)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).add (Int.ofNat (4 * n + 3 + 1 + 1), 0) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc  -- (+26 chars; full line in the Zenodo source)
  have hm : ∀ a, (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).zero ((ring (4 * n + 3 + 1)).neg a) = (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) a := fun a => by lib_ring
  have SA : sumK (Cyclo4.cyc4 LibInt.intR) (chi4 n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := chi4_sum n hpr ι hi
  have SR : sumK (Cyclo4.cyc4 LibInt.intR) (rho n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := rho_sum n hpr ι hi
  have SB : sumK (Cyclo4.cyc4 LibInt.intR) (chib n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := chib_sum n hpr ι hi
  have breal : chib n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) = chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by
    rw [chib_eq n hpr ι hi]; rcases FermatQuartic.m1_real n hpr ι hi with e | e <;> rw [e] <;> rfl
  refine ⟨?_, ?_, ?_⟩
  · rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (chi4 n ι a) (rho n ι a)) (chib n ι a)) ((Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one ((Cyclo4.cyc4 LibInt.intR).neg (delta (Cyclo4.cyc4 Lib  -- (+57 chars; full line in the Zenodo source)
      rw [hm, chi4_mul n hpr ι hi]; unfold g4 rho; rw [chib_eq n hpr ι hi a]
      by_cases ha : a = (ring (4 * n + 3 + 1)).zero
      · rw [ha, show chi4 n ι (ring (4 * n + 3 + 1)).zero = (0, 0) from chi4_zero n ι]; unfold delta; rw [ite_pos' _ _ rfl]
        rcases FermatQuartic.m1_real n hpr ι hi with f | f <;> (try rw [f]) <;> rfl
      · unfold delta; rw [ite_neg' _ _ ha]
        rcases ResidueCriterion.chi4_cases n ι a with e | e | e | e | e
        · exact absurd e (chi4_ne n hpr ι hi a ha)
        all_goals (rw [e]; rcases FermatQuartic.m1_real n hpr ι hi with f | f <;> (try rw [f]) <;> rfl))]
    rw [hp, sumK_add, sumK_add, sumK_add, SA, SR, SB, one_minus n hpr]; lib_ring
  · rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun a => (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (rho n ι a) (chib n ι a)) (chi4 n ι a)) ((Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one ((Cyclo4.cyc4 LibInt.intR).neg (delta (Cyclo4.cyc4 LibInt.intR) a (ring (4 * n + 3 + 1)).zero)))) (fun a => by
      rw [hm, rho_mul n hpr ι hi, rho_neg1 n hpr ι hi]; unfold g4 rho; rw [chib_eq n hpr ι hi a]
      by_cases ha : a = (ring (4 * n + 3 + 1)).zero
      · rw [ha, show chi4 n ι (ring (4 * n + 3 + 1)).zero = (0, 0) from chi4_zero n ι]; unfold delta; rw [ite_pos' _ _ rfl]
        rcases FermatQuartic.m1_real n hpr ι hi with f | f <;> (try rw [f]) <;> rfl
      · unfold delta; rw [ite_neg' _ _ ha]
        rcases ResidueCriterion.chi4_cases n ι a with e | e | e | e | e
        · exact absurd e (chi4_ne n hpr ι hi a ha)
        all_goals (rw [e]; rcases FermatQuartic.m1_real n hpr ι hi with f | f <;> (try rw [f]) <;> rfl))]
    rw [sumK_add, sumK_add, sumK_add, SA, SR, SB, one_minus n hpr]; lib_ring
  · rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (chib n ι a) (chi4 n ι a)) (rho n ι a)) ((Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one ((Cyclo4.cyc4 LibInt.intR).neg (delta (Cyclo4.cyc4 Lib  -- (+57 chars; full line in the Zenodo source)
      rw [hm, chib_mul n hpr ι hi, breal]; unfold g4 rho; rw [chib_eq n hpr ι hi a]
      by_cases ha : a = (ring (4 * n + 3 + 1)).zero
      · rw [ha, show chi4 n ι (ring (4 * n + 3 + 1)).zero = (0, 0) from chi4_zero n ι]; unfold delta; rw [ite_pos' _ _ rfl]
        rcases FermatQuartic.m1_real n hpr ι hi with f | f <;> (try rw [f]) <;> rfl
      · unfold delta; rw [ite_neg' _ _ ha]
        rcases ResidueCriterion.chi4_cases n ι a with e | e | e | e | e
        · exact absurd e (chi4_ne n hpr ι hi a ha)
        all_goals (rw [e]; rcases FermatQuartic.m1_real n hpr ι hi with f | f <;> (try rw [f]) <;> rfl))]
    rw [hp, sumK_add, sumK_add, sumK_add, SA, SR, SB, one_minus n hpr]; lib_ring

def sP : Int × Int := (Cyclo4.cyc4 LibInt.intR).add (Int.ofNat (4 * n + 3 + 1 + 1), 0) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one)

theorem step (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (k : Nat) (A B B2 B3 Cc : Int × Int)
    (hne : ∀ c, c ≠ (ring (4 * n + 3 + 1)).zero → Nk n ι k c = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add A ((Cyclo4.cyc4 LibInt.intR).mul B (chib n ι c))) ((Cyclo4.cyc4 LibInt.intR).mul B2 (rho n ι c))) ((Cyclo4.cyc4 LibInt.intR).mul B3 (chi4 n ι c)))
    (h0 : Nk n ι k (ring (4 * n + 3 + 1)).zero = Cc) :
    (∀ c, c ≠ (ring (4 * n + 3 + 1)).zero → Nk n ι (k + 1) c = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add
        ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (sP n) A) ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) B) B2) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1))  -- (+17 chars; full line in the Zenodo source)
        ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add B2 B3) (J2 n ι)) ((Cyclo4.cyc4 LibInt.intR).add Cc ((Cyclo4.cyc4 LibInt.intR).neg A))) (chib n ι c)))
        ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul B (gconj (Jq n (chi4 n ι)))) ((Cyclo4.cyc4 LibInt.intR).mul B3 (Jq n (chi4 n ι)))) ((Cyclo4.cyc4 LibInt.intR).add Cc ((Cyclo4.cyc4 LibInt.intR).neg A))) (rho n ι c)))
        ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add B B2) (gconj (J2 n ι))) ((Cyclo4.cyc4 LibInt.intR).add Cc ((Cyclo4.cyc4 LibInt.intR).neg A))) (chi4 n ι c))) ∧
    Nk n ι (k + 1) (ring (4 * n + 3 + 1)).zero = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (sP n) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add A ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) B)) B2) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n   -- (+30 chars; full line in the Zenodo source)
  have split : ∀ c a, Nk n ι k ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a)) = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add A ((Cyclo4.cyc4 LibInt.intR).mul B (chib n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a)))))
      ((Cyclo4.cyc4 LibInt.intR).mul B2 (rho n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a))))) ((Cyclo4.cyc4 LibInt.intR).mul B3 (chi4 n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a)))))
      ((Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) a c) ((Cyclo4.cyc4 LibInt.intR).add Cc ((Cyclo4.cyc4 LibInt.intR).neg A))) := fun c a => by
    by_cases ha : a = c
    · rw [ha, show (ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg c) = (ring (4 * n + 3 + 1)).zero by lib_ring, h0, chib_zero, rho_zero, show chi4 n ι (ring (4 * n + 3 + 1)).zero = (Cyclo4.cyc4 LibInt.intR).zero from chi4_zero n ι]
      unfold delta; rw [ite_pos' _ _ rfl]; lib_ring
    · have h1 : (ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a) ≠ (ring (4 * n + 3 + 1)).zero := fun e => ha (by calc a = (ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a))) := by lib_ring
                                                                    _ = c := by rw [e]; lib_ring)
      rw [hne _ h1]; unfold delta; rw [ite_neg' _ _ ha]; lib_ring
  have gen : ∀ c, Nk n ι (k + 1) c = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (sumK (Cyclo4.cyc4 LibInt.intR) (g4 n ι) (LL n)) A)
      ((Cyclo4.cyc4 LibInt.intR).mul B (sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (chib n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a)))) (LL n))))
      ((Cyclo4.cyc4 LibInt.intR).mul B2 (sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (rho n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a)))) (LL n))))
      ((Cyclo4.cyc4 LibInt.intR).mul B3 (sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (chi4 n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a)))) (LL n))))
      ((Cyclo4.cyc4 LibInt.intR).mul (g4 n ι c) ((Cyclo4.cyc4 LibInt.intR).add Cc ((Cyclo4.cyc4 LibInt.intR).neg A))) := fun c => by
    show sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (Nk n ι k ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a)))) (LL n) = _
    rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by rw [split c a])]
    rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun a => (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) A) ((Cyclo4.cyc4 LibInt.intR).mul B ((Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (chib n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a))))))
        ((Cyclo4.cyc4 LibInt.intR).mul B2 ((Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (rho n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a)))))) ((Cyclo4.cyc4 LibInt.intR).mul B3 ((Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (chi4 n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a))))))
        ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) a c) (g4 n ι a)) ((Cyclo4.cyc4 LibInt.intR).add Cc ((Cyclo4.cyc4 LibInt.intR).neg A)))) (fun a => by lib_ring)]
    rw [sumK_add, sumK_add, sumK_add, sumK_add, hq, hp, hp, hp, hq, FiniteRecChain.pick (Cyclo4.cyc4 LibInt.intR) (LL n) (CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR)) (g4 n ι) c]
  constructor
  · intro c hc
    rw [gen c, QuartSurface.g4_sum n hpr ι hi, Q3c n hpr ι hi c hc, Q2c n hpr ι hi c hc, Q1c n hpr ι hi c hc]
    unfold g4 sP; lib_ring
  · obtain ⟨z1, z2, z3⟩ := Zs n hpr ι hi
    rw [gen (ring (4 * n + 3 + 1)).zero, QuartSurface.g4_sum n hpr ι hi, z1, z2, z3]
    have g0 : g4 n ι (ring (4 * n + 3 + 1)).zero = (Cyclo4.cyc4 LibInt.intR).one := by
      unfold g4; rw [chib_zero, rho_zero, show chi4 n ι (ring (4 * n + 3 + 1)).zero = (Cyclo4.cyc4 LibInt.intR).zero from chi4_zero n ι]; lib_ring
    rw [g0]; unfold sP; lib_ring

/-- (A, B, B2, B3, C). -/
def Sq (ι : Zp (4 * n + 3 + 1)) : Nat → (Int × Int) × (Int × Int) × (Int × Int) × (Int × Int) × (Int × Int)
  | 0 => ((Cyclo4.cyc4 LibInt.intR).zero, (Cyclo4.cyc4 LibInt.intR).zero, (Cyclo4.cyc4 LibInt.intR).zero, (Cyclo4.cyc4 LibInt.intR).zero, (Cyclo4.cyc4 LibInt.intR).one)
  | k + 1 =>
    let A := (Sq ι k).1
    let B := (Sq ι k).2.1
    let B2 := (Sq ι k).2.2.1
    let B3 := (Sq ι k).2.2.2.1
    let Cc := (Sq ι k).2.2.2.2
    ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (sP n) A) ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) B) B2) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one  -- (+13 chars; full line in the Zenodo source)
     (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add B2 B3) (J2 n ι)) ((Cyclo4.cyc4 LibInt.intR).add Cc ((Cyclo4.cyc4 LibInt.intR).neg A)),
     (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul B (gconj (Jq n (chi4 n ι)))) ((Cyclo4.cyc4 LibInt.intR).mul B3 (Jq n (chi4 n ι)))) ((Cyclo4.cyc4 LibInt.intR).add Cc ((Cyclo4.cyc4 LibInt.intR).neg A)),
     (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add B B2) (gconj (J2 n ι))) ((Cyclo4.cyc4 LibInt.intR).add Cc ((Cyclo4.cyc4 LibInt.intR).neg A)),
     (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (sP n) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add A ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) B)) B2) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) B3))) Cc)

/-- **Diagonal quartic hypersurfaces in every dimension.** -/
theorem diagonal (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : ∀ k : Nat,
    (∀ c, c ≠ (ring (4 * n + 3 + 1)).zero → Nk n ι k c = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (Sq n ι k).1 ((Cyclo4.cyc4 LibInt.intR).mul (Sq n ι k).2.1 (chib n ι c))) ((Cyclo4.cyc4 LibInt.intR).mul (Sq n ι k).2.2.1 (rho n ι c))) ((Cyclo4.cyc4 LibInt.intR).mul (Sq n ι k).2.2.2.1 (chi4 n ι c))) ∧
    Nk n ι k (ring (4 * n + 3 + 1)).zero = (Sq n ι k).2.2.2.2
  | 0 => by
    constructor
    · intro c hc
      show delta (Cyclo4.cyc4 LibInt.intR) c (ring (4 * n + 3 + 1)).zero = _
      unfold delta; rw [ite_neg' _ _ hc]; show (Cyclo4.cyc4 LibInt.intR).zero = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).zero ((Cyclo4.cyc4 LibInt.intR).mul (Cyclo4.cyc4 LibInt.intR).zero _)) ((Cyclo4.cyc4 LibInt.intR).mul (Cyclo4.cyc4 LibInt.intR).zero _)) ((Cyclo4.cyc4 LibInt.intR).mul (Cyclo4.cyc4 LibInt.intR).zero _);  -- (+9 chars; full line in the Zenodo source)
    · show delta (Cyclo4.cyc4 LibInt.intR) (ring (4 * n + 3 + 1)).zero (ring (4 * n + 3 + 1)).zero = (Cyclo4.cyc4 LibInt.intR).one
      unfold delta; exact ite_pos' _ _ rfl
  | k + 1 => by
    have ih := diagonal hpr ι hi k
    exact step n hpr ι hi k _ _ _ _ _ ih.1 ih.2

/-! ## diagonal quartic forms with coefficients: the nine basic sums -/

theorem PAA (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (c : Zp (4 * n + 3 + 1)) (hc : c ≠ (ring (4 * n + 3 + 1)).zero) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι u) (chi4 n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι c) (chi4 n ι c)) (Jq n (chi4 n ι)) := by
  rw [scale n hpr c hc, sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun t => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι c) (chi4 n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι t) (chi4 n ι (om n t)))) (fun t => by
    rw [hct, chi4_mul n hpr ι hi, chi4_mul n hpr ι hi]; lib_ring), hp]
  rfl

theorem PAR (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (c : Zp (4 * n + 3 + 1)) (hc : c ≠ (ring (4 * n + 3 + 1)).zero) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι u) (rho n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι c) (rho n ι c)) (J2 n ι) := by
  rw [scale n hpr c hc, sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun t => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι c) (rho n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι t) (rho n ι (om n t)))) (fun t => by
    rw [hct, chi4_mul n hpr ι hi, rho_mul n hpr ι hi]; lib_ring), hp]
  congr 1
  exact SAR n ι

theorem PAB (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (c : Zp (4 * n + 3 + 1)) (hc : c ≠ (ring (4 * n + 3 + 1)).zero) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι u) (chib n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι c) (chib n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4  -- (+25 chars; full line in the Zenodo source)
  rw [scale n hpr c hc, sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun t => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι c) (chib n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι t) (chib n ι (om n t)))) (fun t => by
    rw [hct, chi4_mul n hpr ι hi, chib_mul n hpr ι hi]; lib_ring), hp]
  congr 1
  exact FermatQuartic.T13 n hpr ι hi

theorem PRA (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (c : Zp (4 * n + 3 + 1)) (hc : c ≠ (ring (4 * n + 3 + 1)).zero) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (rho n ι u) (chi4 n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι c) (chi4 n ι c)) (J2 n ι) := by
  rw [scale n hpr c hc, sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun t => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι c) (chi4 n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι t) (chi4 n ι (om n t)))) (fun t => by
    rw [hct, rho_mul n hpr ι hi, chi4_mul n hpr ι hi]; lib_ring), hp]
  rfl

theorem PRR (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (c : Zp (4 * n + 3 + 1)) (hc : c ≠ (ring (4 * n + 3 + 1)).zero) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (rho n ι u) (rho n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι c) (rho n ι c)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one) := by
  rw [scale n hpr c hc, sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun t => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι c) (rho n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι t) (rho n ι (om n t)))) (fun t => by
    rw [hct, rho_mul n hpr ι hi, rho_mul n hpr ι hi]; lib_ring), hp]
  congr 1
  exact J_rho n hpr ι hi

theorem PRB (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (c : Zp (4 * n + 3 + 1)) (hc : c ≠ (ring (4 * n + 3 + 1)).zero) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (rho n ι u) (chib n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι c) (chib n ι c)) (gconj (J2 n ι)) := by
  rw [scale n hpr c hc, sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun t => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι c) (chib n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι t) (chib n ι (om n t)))) (fun t => by
    rw [hct, rho_mul n hpr ι hi, chib_mul n hpr ι hi]; lib_ring), hp]
  congr 1
  exact SRB n hpr ι hi

theorem PBA (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (c : Zp (4 * n + 3 + 1)) (hc : c ≠ (ring (4 * n + 3 + 1)).zero) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (chib n ι u) (chi4 n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι c) (chi4 n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4  -- (+25 chars; full line in the Zenodo source)
  rw [scale n hpr c hc, sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun t => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι c) (chi4 n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι t) (chi4 n ι (om n t)))) (fun t => by
    rw [hct, chib_mul n hpr ι hi, chi4_mul n hpr ι hi]; lib_ring), hp]
  congr 1
  exact SBA n hpr ι hi

theorem PBR (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (c : Zp (4 * n + 3 + 1)) (hc : c ≠ (ring (4 * n + 3 + 1)).zero) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (chib n ι u) (rho n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι c) (rho n ι c)) (gconj (J2 n ι)) := by
  rw [scale n hpr c hc, sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun t => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι c) (rho n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι t) (rho n ι (om n t)))) (fun t => by
    rw [hct, chib_mul n hpr ι hi, rho_mul n hpr ι hi]; lib_ring), hp]
  congr 1
  exact SBR n hpr ι hi

theorem PBB (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (c : Zp (4 * n + 3 + 1)) (hc : c ≠ (ring (4 * n + 3 + 1)).zero) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (chib n ι u) (chib n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι c) (chib n ι c)) (gconj (Jq n (chi4 n ι))) := by
  rw [scale n hpr c hc, sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun t => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι c) (chib n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι t) (chib n ι (om n t)))) (fun t => by
    rw [hct, chib_mul n hpr ι hi, chib_mul n hpr ι hi]; lib_ring), hp]
  congr 1
  exact SBB n hpr ι hi

/-! ## forms with coefficients -/

def Nf (ι : Zp (4 * n + 3 + 1)) : List (Zp (4 * n + 3 + 1)) → Zp (4 * n + 3 + 1) → Int × Int
  | [], c => delta (Cyclo4.cyc4 LibInt.intR) c (ring (4 * n + 3 + 1)).zero
  | a :: as, c => sumK (Cyclo4.cyc4 LibInt.intR) (fun x => Nf ι as ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul a (q4 n x))))) (LL n)

/-- #{a x⁴ = u} = 1 + χ̄(a)χ(u) + ρ(a)ρ(u) + χ(a)χ̄(u). -/
def wt (ι a u : Zp (4 * n + 3 + 1)) : Int × Int :=
  (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (chi4 n ι u))) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (rho n ι u))) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (chib n ι u))

theorem conv_a (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (a : Zp (4 * n + 3 + 1)) (ha : a ≠ (ring (4 * n + 3 + 1)).zero) (F : Zp (4 * n + 3 + 1) → Int × Int) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun x => F ((ring (4 * n + 3 + 1)).mul a (q4 n x))) (LL n) = sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (wt n ι a u) (F u)) (LL n) := by
  rw [FermatQuartic.step2 n hpr ι hi (fun v => F ((ring (4 * n + 3 + 1)).mul a v))]
  have hiv := CharSumGen.iv_mul (4 * n + 2) hpr a ha
  rw [← FiniteRecChain.reindex (Cyclo4.cyc4 LibInt.intR) (LL n) (CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR)) ((ring (4 * n + 3 + 1)).mul a) ((ring (4 * n + 3 + 1)).mul (CharSumGen.iv (4 * n + 2) a))
    (fun x => by rw [← (ring (4 * n + 3 + 1)).mul_assoc, hiv]; lib_ring) (fun y => by rw [← (ring (4 * n + 3 + 1)).mul_assoc, (ring (4 * n + 3 + 1)).mul_comm a, hiv]; lib_ring) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (wt n ι a u) (F u))]
  obtain ⟨v1, _, v3⟩ := vals n hpr ι hi a ha
  exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun v => by
    show (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι v) (F ((ring (4 * n + 3 + 1)).mul a v)) = (Cyclo4.cyc4 LibInt.intR).mul (wt n ι a ((ring (4 * n + 3 + 1)).mul a v)) (F ((ring (4 * n + 3 + 1)).mul a v))
    unfold wt g4
    rw [chi4_mul n hpr ι hi, rho_mul n hpr ι hi, chib_mul n hpr ι hi]
    rw [show (Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (chi4 n ι v)) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (chib n ι a)) (chi4 n ι v) by lib_ring,
      show (Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (rho n ι v)) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (rho n ι a)) (rho n ι v) by lib_ring,
      show (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (chib n ι v)) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (chib n ι a)) (chib n ι v) by lib_ring, v1, v3]; lib_ring) _

theorem wt_sum (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (a : Zp (4 * n + 3 + 1)) : sumK (Cyclo4.cyc4 LibInt.intR) (wt n ι a) (LL n) = (Int.ofNat (4 * n + 3 + 1 + 1), 0) := by
  unfold wt
  have T1 : sumK (Cyclo4.cyc4 LibInt.intR) (fun _ : Zp (4 * n + 3 + 1) => (Cyclo4.cyc4 LibInt.intR).one) (LL n) = (Int.ofNat (4 * n + 3 + 1 + 1), 0) := sum_oneq n _
  rw [sumK_add, sumK_add, sumK_add, T1, hp, hp, hp, show sumK (Cyclo4.cyc4 LibInt.intR) (chi4 n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero from chi4_sum n hpr ι hi,
    show sumK (Cyclo4.cyc4 LibInt.intR) (rho n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero from rho_sum n hpr ι hi, show sumK (Cyclo4.cyc4 LibInt.intR) (chib n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero from chib_sum n hpr ι hi]
  lib_ring

theorem idents (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (u : Zp (4 * n + 3 + 1)) :
    (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι u) (chib n ι u) = (Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one ((Cyclo4.cyc4 LibInt.intR).neg (delta (Cyclo4.cyc4 LibInt.intR) u (ring (4 * n + 3 + 1)).zero)) ∧
    (Cyclo4.cyc4 LibInt.intR).mul (rho n ι u) (rho n ι u) = (Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one ((Cyclo4.cyc4 LibInt.intR).neg (delta (Cyclo4.cyc4 LibInt.intR) u (ring (4 * n + 3 + 1)).zero)) ∧
    (Cyclo4.cyc4 LibInt.intR).mul (chib n ι u) (chib n ι u) = rho n ι u ∧ (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι u) (chi4 n ι u) = rho n ι u ∧
    (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι u) (rho n ι u) = chib n ι u ∧ (Cyclo4.cyc4 LibInt.intR).mul (rho n ι u) (chi4 n ι u) = chib n ι u ∧
    (Cyclo4.cyc4 LibInt.intR).mul (chib n ι u) (rho n ι u) = chi4 n ι u ∧ (Cyclo4.cyc4 LibInt.intR).mul (rho n ι u) (chib n ι u) = chi4 n ι u ∧
    (Cyclo4.cyc4 LibInt.intR).mul (chib n ι u) (chi4 n ι u) = (Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one ((Cyclo4.cyc4 LibInt.intR).neg (delta (Cyclo4.cyc4 LibInt.intR) u (ring (4 * n + 3 + 1)).zero)) := by
  obtain ⟨p1, p2, p3⟩ := pt_vals n hpr ι hi u
  exact ⟨by rw [(Cyclo4.cyc4 LibInt.intR).mul_comm]; exact p1, p2, p3, rfl, chi_rho n hpr ι hi u, by rw [(Cyclo4.cyc4 LibInt.intR).mul_comm]; exact chi_rho n hpr ι hi u,
    chib_rho n hpr ι hi u, by rw [(Cyclo4.cyc4 LibInt.intR).mul_comm]; exact chib_rho n hpr ι hi u, p1⟩

theorem ZW (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (a : Zp (4 * n + 3 + 1)) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (wt n ι a u) (chib n ι ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).zero ((ring (4 * n + 3 + 1)).neg u)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (sP n)) ∧
    sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (wt n ι a u) (rho n ι ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).zero ((ring (4 * n + 3 + 1)).neg u)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (sP n) ∧
    sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (wt n ι a u) (chi4 n ι ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).zero ((ring (4 * n + 3 + 1)).neg u)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (sP n)) := by
  have hm : ∀ u, (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).zero ((ring (4 * n + 3 + 1)).neg u) = (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) u := fun u => by lib_ring
  have SA : sumK (Cyclo4.cyc4 LibInt.intR) (chi4 n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := chi4_sum n hpr ι hi
  have SR : sumK (Cyclo4.cyc4 LibInt.intR) (rho n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := rho_sum n hpr ι hi
  have SB : sumK (Cyclo4.cyc4 LibInt.intR) (chib n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := chib_sum n hpr ι hi
  have breal : chib n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) = chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by
    rw [chib_eq n hpr ι hi]; rcases FermatQuartic.m1_real n hpr ι hi with e | e <;> rw [e] <;> rfl
  have OM := one_minus n hpr
  refine ⟨?_, ?_, ?_⟩
  · rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun u => (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (chib n ι u) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) ((Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one ((Cyclo4.cyc4 LibInt.intR).neg (delta  -- (+61 chars; full line in the Zenodo source)
        ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (chi4 n ι u))) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (rho n ι u)))) (fun u => by
      obtain ⟨i1, _, i3, _, _, _, _, i8, _⟩ := idents n hpr ι hi u
      rw [hm, chib_mul n hpr ι hi, breal]; unfold wt
      calc _ = (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (chib n ι u) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι u) (chib n ι u))))
                ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι u) (chib n ι u)))) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι u) (chib n ι u)))) := by lib_ring
        _ = _ := by rw [i1, i8, i3])]
    rw [hp, sumK_add, sumK_add, sumK_add, hp, hp, hp, SB, OM, SA, SR]; unfold sP; lib_ring
  · rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun u => (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (rho n ι u) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (chib n ι u))) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) ((Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one ((Cyclo4.cyc4 LibInt.intR).neg (delta (Cyclo4.cyc4 LibInt.intR) u (ring (4 * n  -- (+20 chars; full line in the Zenodo source)
        ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (chi4 n ι u))) (fun u => by
      obtain ⟨_, i2, _, _, i5, _, i7, _, _⟩ := idents n hpr ι hi u
      rw [hm, rho_mul n hpr ι hi, rho_neg1 n hpr ι hi]; unfold wt
      calc _ = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (rho n ι u) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι u) (rho n ι u))))
                ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι u) (rho n ι u)))) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι u) (rho n ι u))) := by lib_ring
        _ = _ := by rw [i5, i2, i7])]
    rw [sumK_add, sumK_add, sumK_add, hp, hp, hp, SB, OM, SA, SR]; unfold sP; lib_ring
  · rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun u => (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (chi4 n ι u) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (rho n ι u)))
        ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (chib n ι u))) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) ((Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one ((Cyclo4.cyc4 LibInt.intR).neg (delta (Cyclo4.cyc4 LibInt.intR) u (ring (4 * n + 3 + 1)).zero)))))) (fun u => by
      obtain ⟨_, _, _, i4, _, i6, _, _, i9⟩ := idents n hpr ι hi u
      rw [hm, chi4_mul n hpr ι hi]; unfold wt
      calc _ = (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (chi4 n ι u) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι u) (chi4 n ι u))))
                ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι u) (chi4 n ι u)))) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι u) (chi4 n ι u)))) := by lib_ring
        _ = _ := by rw [i4, i6, i9])]
    rw [hp, sumK_add, sumK_add, sumK_add, hp, hp, hp, SA, SR, SB, OM]; unfold sP; lib_ring

theorem shift0 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (c : Zp (4 * n + 3 + 1)) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun u => chi4 n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u))) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero ∧ sumK (Cyclo4.cyc4 LibInt.intR) (fun u => rho n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u))) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero ∧
    sumK (Cyclo4.cyc4 LibInt.intR) (fun u => chib n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u))) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := by
  have r : ∀ G : Zp (4 * n + 3 + 1) → Int × Int, sumK (Cyclo4.cyc4 LibInt.intR) (fun u => G ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u))) (LL n) = sumK (Cyclo4.cyc4 LibInt.intR) G (LL n) := fun G =>
    FiniteRecChain.reindex (Cyclo4.cyc4 LibInt.intR) (LL n) (CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR)) (fun u => (ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)) (fun u => (ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)) (fun x => by lib_ring) (fun y => by lib_ring) G
  exact ⟨(r _).trans (chi4_sum n hpr ι hi), (r _).trans (rho_sum n hpr ι hi), (r _).trans (chib_sum n hpr ι hi)⟩

theorem step_f (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (a : Zp (4 * n + 3 + 1)) (ha : a ≠ (ring (4 * n + 3 + 1)).zero) (as : List (Zp (4 * n + 3 + 1))) (A B B2 B3 Cc : Int × Int)
    (hne : ∀ c, c ≠ (ring (4 * n + 3 + 1)).zero → Nf n ι as c = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add A ((Cyclo4.cyc4 LibInt.intR).mul B (chib n ι c))) ((Cyclo4.cyc4 LibInt.intR).mul B2 (rho n ι c))) ((Cyclo4.cyc4 LibInt.intR).mul B3 (chi4 n ι c)))
    (h0 : Nf n ι as (ring (4 * n + 3 + 1)).zero = Cc) :
    (∀ c, c ≠ (ring (4 * n + 3 + 1)).zero → Nf n ι (a :: as) c = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add
        ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (Int.ofNat (4 * n + 3 + 1 + 1), 0) A) ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (chib n ι a)) B) ((Cyclo4.cyc4 LibInt.i  -- (+245 chars; full line in the Zenodo source)
        ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul B2 (chib n ι a)) (J2 n ι)) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul B3 (rho n ι a)) (J2 n ι))) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) ((Cyclo4.cyc4 LibInt.intR).add Cc ((Cyclo4.cyc4 LibInt.intR).neg A)))) (chib   -- (+8 chars; full line in the Zenodo source)
        ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul B (chi4 n ι a)) (gconj (Jq n (chi4 n ι)))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul B3 (chib n ι a)) (Jq n (chi4 n ι)))) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) ((Cyclo4.cyc4 LibInt.intR).add Cc ((Cyclo4.cyc4 LibI  -- (+32 chars; full line in the Zenodo source)
        ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul B (rho n ι a)) (gconj (J2 n ι))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul B2 (chi4 n ι a)) (gconj (J2 n ι)))) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) ((Cyclo4.cyc4 LibInt.intR).add Cc ((Cyclo4.cyc4 LibInt.intR).n  -- (+25 chars; full line in the Zenodo source)
    Nf n ι (a :: as) (ring (4 * n + 3 + 1)).zero = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (Int.ofNat (4 * n + 3 + 1 + 1), 0) A)
      ((Cyclo4.cyc4 LibInt.intR).mul B ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (sP n)))))
      ((Cyclo4.cyc4 LibInt.intR).mul B2 ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (sP n)))) ((Cyclo4.cyc4 LibInt.intR).mul B3 ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (sP n))))) ((Cyclo4.cyc4 LibInt.intR).add Cc ((Cyclo4.cyc4 LibInt.intR).neg A)) := by
  have split : ∀ c u, Nf n ι as ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)) = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add A ((Cyclo4.cyc4 LibInt.intR).mul B (chib n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))))
      ((Cyclo4.cyc4 LibInt.intR).mul B2 (rho n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u))))) ((Cyclo4.cyc4 LibInt.intR).mul B3 (chi4 n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))))
      ((Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) u c) ((Cyclo4.cyc4 LibInt.intR).add Cc ((Cyclo4.cyc4 LibInt.intR).neg A))) := fun c u => by
    by_cases hu : u = c
    · rw [hu, show (ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg c) = (ring (4 * n + 3 + 1)).zero by lib_ring, h0, chib_zero, rho_zero, show chi4 n ι (ring (4 * n + 3 + 1)).zero = (Cyclo4.cyc4 LibInt.intR).zero from chi4_zero n ι]
      unfold delta; rw [ite_pos' _ _ rfl]; lib_ring
    · have h1 : (ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u) ≠ (ring (4 * n + 3 + 1)).zero := fun e => hu (by calc u = (ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u))) := by lib_ring
                                                                    _ = c := by rw [e]; lib_ring)
      rw [hne _ h1]; unfold delta; rw [ite_neg' _ _ hu]; lib_ring
  have gen : ∀ c, Nf n ι (a :: as) c = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (sumK (Cyclo4.cyc4 LibInt.intR) (wt n ι a) (LL n)) A)
      ((Cyclo4.cyc4 LibInt.intR).mul B (sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (wt n ι a u) (chib n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))) (LL n))))
      ((Cyclo4.cyc4 LibInt.intR).mul B2 (sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (wt n ι a u) (rho n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))) (LL n))))
      ((Cyclo4.cyc4 LibInt.intR).mul B3 (sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (wt n ι a u) (chi4 n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))) (LL n))))
      ((Cyclo4.cyc4 LibInt.intR).mul (wt n ι a c) ((Cyclo4.cyc4 LibInt.intR).add Cc ((Cyclo4.cyc4 LibInt.intR).neg A))) := fun c => by
    show sumK (Cyclo4.cyc4 LibInt.intR) (fun x => Nf n ι as ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul a (q4 n x))))) (LL n) = _
    rw [conv_a n hpr ι hi a ha (fun u => Nf n ι as ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))]
    rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun u => by rw [split c u])]
    rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun u => (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (wt n ι a u) A) ((Cyclo4.cyc4 LibInt.intR).mul B ((Cyclo4.cyc4 LibInt.intR).mul (wt n ι a u) (chib n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u))))))
        ((Cyclo4.cyc4 LibInt.intR).mul B2 ((Cyclo4.cyc4 LibInt.intR).mul (wt n ι a u) (rho n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))))) ((Cyclo4.cyc4 LibInt.intR).mul B3 ((Cyclo4.cyc4 LibInt.intR).mul (wt n ι a u) (chi4 n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u))))))
        ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) u c) (wt n ι a u)) ((Cyclo4.cyc4 LibInt.intR).add Cc ((Cyclo4.cyc4 LibInt.intR).neg A)))) (fun u => by lib_ring)]
    rw [sumK_add, sumK_add, sumK_add, sumK_add, hq, hp, hp, hp, hq, FiniteRecChain.pick (Cyclo4.cyc4 LibInt.intR) (LL n) (CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR)) (wt n ι a) c]
  have expand : ∀ c (ψ : Zp (4 * n + 3 + 1) → Int × Int), sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (wt n ι a u) (ψ ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))) (LL n) =
      (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (sumK (Cyclo4.cyc4 LibInt.intR) (fun u => ψ ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u))) (LL n))
        ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι u) (ψ ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))) (LL n))))
        ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (rho n ι u) (ψ ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))) (LL n))))
        ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (chib n ι u) (ψ ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))) (LL n))) := fun c ψ => by
    unfold wt
    rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun u => (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (ψ ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u))) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι u) (ψ ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u))))))
        ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι u) (ψ ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))))) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι u) (ψ ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))))) (fun u => by lib_ring),
      sumK_add, sumK_add, sumK_add, hp, hp, hp]
  constructor
  · intro c hc
    obtain ⟨sA, sR, sB⟩ := shift0 n hpr ι hi c
    have dz : delta (Cyclo4.cyc4 LibInt.intR) c (ring (4 * n + 3 + 1)).zero = (Cyclo4.cyc4 LibInt.intR).zero := ite_neg' _ _ hc
    obtain ⟨i1, i2, i3, i4, i5, i6, i7, i8, i9⟩ := idents n hpr ι hi c
    rw [dz] at i1 i2 i9
    rw [gen c, wt_sum n hpr ι hi a, expand c (chib n ι), expand c (rho n ι), expand c (chi4 n ι), sA, sR, sB,
      PAB n hpr ι hi c hc, PRB n hpr ι hi c hc, PBB n hpr ι hi c hc, PAR n hpr ι hi c hc, PRR n hpr ι hi c hc, PBR n hpr ι hi c hc,
      PAA n hpr ι hi c hc, PRA n hpr ι hi c hc, PBA n hpr ι hi c hc, i1, i2, i3, i4, i5, i6, i7, i8, i9]
    unfold wt; lib_ring
  · obtain ⟨z1, z2, z3⟩ := ZW n hpr ι hi a
    rw [gen (ring (4 * n + 3 + 1)).zero, wt_sum n hpr ι hi a, z1, z2, z3]
    have w0 : wt n ι a (ring (4 * n + 3 + 1)).zero = (Cyclo4.cyc4 LibInt.intR).one := by
      unfold wt; rw [chib_zero, rho_zero, show chi4 n ι (ring (4 * n + 3 + 1)).zero = (Cyclo4.cyc4 LibInt.intR).zero from chi4_zero n ι]; lib_ring
    rw [w0]; lib_ring

def Sf (ι : Zp (4 * n + 3 + 1)) : List (Zp (4 * n + 3 + 1)) → (Int × Int) × (Int × Int) × (Int × Int) × (Int × Int) × (Int × Int)
  | [] => ((Cyclo4.cyc4 LibInt.intR).zero, (Cyclo4.cyc4 LibInt.intR).zero, (Cyclo4.cyc4 LibInt.intR).zero, (Cyclo4.cyc4 LibInt.intR).zero, (Cyclo4.cyc4 LibInt.intR).one)
  | a :: as =>
    let A := (Sf ι as).1
    let B := (Sf ι as).2.1
    let B2 := (Sf ι as).2.2.1
    let B3 := (Sf ι as).2.2.2.1
    let Cc := (Sf ι as).2.2.2.2
    ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (Int.ofNat (4 * n + 3 + 1 + 1), 0) A) ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (chib n ι a)) B) ((Cyclo4.cyc4 LibInt.intR)  -- (+241 chars; full line in the Zenodo source)
     (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul B2 (chib n ι a)) (J2 n ι)) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul B3 (rho n ι a)) (J2 n ι))) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) ((Cyclo4.cyc4 LibInt.intR).add Cc ((Cyclo4.cyc4 LibInt.intR).neg A))),
     (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul B (chi4 n ι a)) (gconj (Jq n (chi4 n ι)))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul B3 (chib n ι a)) (Jq n (chi4 n ι)))) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) ((Cyclo4.cyc4 LibInt.intR).add Cc ((Cyclo4.cyc4 LibInt.intR).neg A))),
     (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul B (rho n ι a)) (gconj (J2 n ι))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul B2 (chi4 n ι a)) (gconj (J2 n ι)))) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) ((Cyclo4.cyc4 LibInt.intR).add Cc ((Cyclo4.cyc4 LibInt.intR).neg A))),
     (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (Int.ofNat (4 * n + 3 + 1 + 1), 0) A)
      ((Cyclo4.cyc4 LibInt.intR).mul B ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (sP n)))))
      ((Cyclo4.cyc4 LibInt.intR).mul B2 ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (sP n)))) ((Cyclo4.cyc4 LibInt.intR).mul B3 ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (sP n))))) ((Cyclo4.cyc4 LibInt.intR).add Cc ((Cyclo4.cyc4 LibInt.intR).neg A)))

/-- **Every diagonal quartic form Σ a_i x_i⁴ over 𝔽_p** (a_i ≠ 0). -/
theorem diag_form (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : ∀ as : List (Zp (4 * n + 3 + 1)), (∀ a, memL a as → a ≠ (ring (4 * n + 3 + 1)).zero) →
    (∀ c, c ≠ (ring (4 * n + 3 + 1)).zero → Nf n ι as c = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (Sf n ι as).1 ((Cyclo4.cyc4 LibInt.intR).mul (Sf n ι as).2.1 (chib n ι c))) ((Cyclo4.cyc4 LibInt.intR).mul (Sf n ι as).2.2.1 (rho n ι c))) ((Cyclo4.cyc4 LibInt.intR).mul (Sf n ι as).2.2.2.1 (chi4 n ι c))) ∧
    Nf n ι as (ring (4 * n + 3 + 1)).zero = (Sf n ι as).2.2.2.2
  | [], _ => by
    constructor
    · intro c hc
      show delta (Cyclo4.cyc4 LibInt.intR) c (ring (4 * n + 3 + 1)).zero = _
      unfold delta; rw [ite_neg' _ _ hc]; show (Cyclo4.cyc4 LibInt.intR).zero = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).zero ((Cyclo4.cyc4 LibInt.intR).mul (Cyclo4.cyc4 LibInt.intR).zero _)) ((Cyclo4.cyc4 LibInt.intR).mul (Cyclo4.cyc4 LibInt.intR).zero _)) ((Cyclo4.cyc4 LibInt.intR).mul (Cyclo4.cyc4 LibInt.intR).zero _);  -- (+9 chars; full line in the Zenodo source)
    · show delta (Cyclo4.cyc4 LibInt.intR) (ring (4 * n + 3 + 1)).zero (ring (4 * n + 3 + 1)).zero = (Cyclo4.cyc4 LibInt.intR).one
      unfold delta; exact ite_pos' _ _ rfl
  | a :: as, h => by
    have ih := diag_form hpr ι hi as (fun b hb => h b (Or.inr hb))
    exact step_f n hpr ι hi a (h a (Or.inl rfl)) as _ _ _ _ _ ih.1 ih.2

end QuartDiagonal
#print axioms QuartDiagonal.hp
#print axioms QuartDiagonal.hq
#print axioms QuartDiagonal.SBA
#print axioms QuartDiagonal.SAR
#print axioms QuartDiagonal.SRB
#print axioms QuartDiagonal.SBR
#print axioms QuartDiagonal.SBB
#print axioms QuartDiagonal.scale
#print axioms QuartDiagonal.hct
#print axioms QuartDiagonal.Q1c
#print axioms QuartDiagonal.Q2c
#print axioms QuartDiagonal.Q3c
#print axioms QuartDiagonal.one_minus
#print axioms QuartDiagonal.pt_vals
#print axioms QuartDiagonal.Zs
#print axioms QuartDiagonal.step
#print axioms QuartDiagonal.diagonal
#print axioms QuartDiagonal.PAA
#print axioms QuartDiagonal.PAR
#print axioms QuartDiagonal.PAB
#print axioms QuartDiagonal.PRA
#print axioms QuartDiagonal.PRR
#print axioms QuartDiagonal.PRB
#print axioms QuartDiagonal.PBA
#print axioms QuartDiagonal.PBR
#print axioms QuartDiagonal.PBB
#print axioms QuartDiagonal.conv_a
#print axioms QuartDiagonal.wt_sum
#print axioms QuartDiagonal.idents
#print axioms QuartDiagonal.ZW
#print axioms QuartDiagonal.shift0
#print axioms QuartDiagonal.step_f
#print axioms QuartDiagonal.diag_form

lang/QuartDup.lean

lean_all/lang/QuartDup.lean

/-
  lang/QuartDup — T43, cycle 231: the duplication formula J(χ, χ) = χ(−1) J(ρ, χ) for the quartic character.

  p = 4n + 5, χ quartic, ρ = χ². #{x : x² = a} = 1 + ρ(a) (sq_root, two_roots, sq_count); with 4t(1 − t) = 1 − (2t − 1)² and the
  bijection t ↦ 2t − 1:  χ(4) J(χ, χ) = Σ_s χ(1 − s²) = Σ_v (1 + ρ(v)) χ(1 − v) = J(ρ, χ), and χ(4) = ρ(2) = χ(−1) = ±1:
      quart_dup    J(χ, χ) = χ(−1) · Σ_t ρ(t) χ(1 − t)
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.QuartPrimary
import lang.HeckePrime

namespace QuartDup

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius QuartChar QuartJacobi QuartPrimary
open LibRing (castZ castN)

variable (n : Nat)

/-- a^{(p−1)/2} = 1, a ≠ 0 ⇒ a is a square. -/
theorem sq_root (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (a : Zp (4 * n + 3 + 1)) (ha0 : a ≠ (ring (4 * n + 3 + 1)).zero) (ha : pw (ring (4 * n + 3 + 1)) a (2 * n + 2) = (ring (4 * n + 3 + 1)).one) : ∃ c, (ring (4 * n + 3 + 1)).mul c c = a := by
  match PrimRoot.primitive_root (4 * n + 3) hpr with
  | ⟨g, hg⟩ =>
    match DLog.mem_pows (4 * n + 3) g a _ (DLog.every_unit (4 * n + 3) hpr g hg a ha0) with
    | ⟨k, _, hk⟩ =>
      have h1 : pw (ring (4 * n + 3 + 1)) g (k * (2 * n + 2)) = (ring (4 * n + 3 + 1)).one := by rw [← TwoPower.pwpow, ← hk, ha]
      match hg.2 _ h1 with
      | ⟨c, hc⟩ =>
        have h3 : k = 2 * c := by
          apply Nat.eq_of_mul_eq_mul_right (Nat.zero_lt_succ (2 * n + 1))
          rw [hc]; lib_nat
        refine ⟨pw (ring (4 * n + 3 + 1)) g c, ?_⟩
        rw [hk, h3, show 2 * c = c * 2 by lib_nat, ← TwoPower.pwpow]
        show _ = (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul (ring (4 * n + 3 + 1)).one _) _
        lib_ring

def mtwo : Zp (4 * n + 3 + 1) := (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one

theorem mtwo_ne : mtwo n ≠ (ring (4 * n + 3 + 1)).zero := Eisen.two_ne_zero' (4 * n + 3) (by rw [show 4 * n + 3 = (4 * n + 1) + 2 by lib_nat]; exact Nat.le_add_left 2 _)

/-- the two square roots of c². -/
theorem two_roots (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (c : Zp (4 * n + 3 + 1)) (hc : c ≠ (ring (4 * n + 3 + 1)).zero) (x : Zp (4 * n + 3 + 1)) :
    delta (Cyclo4.cyc4 LibInt.intR) ((ring (4 * n + 3 + 1)).mul x x) ((ring (4 * n + 3 + 1)).mul c c) = (Cyclo4.cyc4 LibInt.intR).add (delta (Cyclo4.cyc4 LibInt.intR) x c) (delta (Cyclo4.cyc4 LibInt.intR) x ((ring (4 * n + 3 + 1)).neg c)) := by
  have d : c ≠ (ring (4 * n + 3 + 1)).neg c := fun e => hc (by
    have e2 : (ring (4 * n + 3 + 1)).mul (mtwo n) c = (ring (4 * n + 3 + 1)).zero := by
      calc (ring (4 * n + 3 + 1)).mul (mtwo n) c = (ring (4 * n + 3 + 1)).add c c := by unfold mtwo; lib_ring
        _ = (ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg c) := congrArg ((ring (4 * n + 3 + 1)).add c) e
        _ = (ring (4 * n + 3 + 1)).zero := by lib_ring
    cases nozd (4 * n + 3) hpr _ _ e2 with
    | inl h => exact absurd h (mtwo_ne n)
    | inr h => exact h)
  unfold delta
  by_cases hx : (ring (4 * n + 3 + 1)).mul x x = (ring (4 * n + 3 + 1)).mul c c
  · rw [ite_pos' _ _ hx]
    have e : (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).add x ((ring (4 * n + 3 + 1)).neg c)) ((ring (4 * n + 3 + 1)).add x c) = (ring (4 * n + 3 + 1)).zero := by
      rw [show (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).add x ((ring (4 * n + 3 + 1)).neg c)) ((ring (4 * n + 3 + 1)).add x c) = (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).mul x x) ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul c c)) by lib_ring, hx]; lib_ring
    cases nozd (4 * n + 3) hpr _ _ e with
    | inl h =>
      have ex : x = c := by calc x = (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).add x ((ring (4 * n + 3 + 1)).neg c)) c := by lib_ring
                                _ = c := by rw [h]; lib_ring
      rw [ite_pos' _ _ ex, ite_neg' _ _ (by rw [ex]; exact d)]; lib_ring
    | inr h =>
      have ex : x = (ring (4 * n + 3 + 1)).neg c := by calc x = (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).add x c) ((ring (4 * n + 3 + 1)).neg c) := by lib_ring
                                      _ = (ring (4 * n + 3 + 1)).neg c := by rw [h]; lib_ring
      rw [ite_neg' _ _ (by rw [ex]; exact fun e => d e.symm), ite_pos' _ _ ex]; lib_ring
  · rw [ite_neg' _ _ hx, ite_neg' _ _ (fun e => hx (by rw [e])), ite_neg' _ _ (fun e => hx (by rw [e]; lib_ring))]; lib_ring

theorem chi4_one_pw (ι x : Zp (4 * n + 3 + 1)) (h : chi4 n ι x = (1, 0)) : pw (ring (4 * n + 3 + 1)) x (n + 1) = (ring (4 * n + 3 + 1)).one := by
  unfold chi4 at h
  by_cases h0 : x = (ring (4 * n + 3 + 1)).zero
  · rw [ite_pos' _ _ h0] at h; exact absurd (congrArg Prod.fst h) (fun e => Nat.noConfusion (Int.ofNat.inj e))
  · rw [ite_neg' _ _ h0] at h
    by_cases h1 : pw (ring (4 * n + 3 + 1)) x (n + 1) = (ring (4 * n + 3 + 1)).one
    · exact h1
    · rw [ite_neg' _ _ h1] at h
      by_cases h2 : pw (ring (4 * n + 3 + 1)) x (n + 1) = ι
      · rw [ite_pos' _ _ h2] at h; exact absurd (congrArg Prod.fst h) (fun e => Nat.noConfusion (Int.ofNat.inj e))
      · rw [ite_neg' _ _ h2] at h
        by_cases h3 : pw (ring (4 * n + 3 + 1)) x (n + 1) = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one
        · rw [ite_pos' _ _ h3] at h; exact absurd (congrArg Prod.fst h) (fun e => Int.noConfusion e)
        · rw [ite_neg' _ _ h3] at h; exact absurd (congrArg Prod.fst h) (fun e => Nat.noConfusion (Int.ofNat.inj e))

theorem chi4_m1_pw (ι x : Zp (4 * n + 3 + 1)) (h : chi4 n ι x = (-1, 0)) : pw (ring (4 * n + 3 + 1)) x (n + 1) = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one := by
  unfold chi4 at h
  by_cases h0 : x = (ring (4 * n + 3 + 1)).zero
  · rw [ite_pos' _ _ h0] at h; exact absurd (congrArg Prod.fst h) (fun e => Int.noConfusion e)
  · rw [ite_neg' _ _ h0] at h
    by_cases h1 : pw (ring (4 * n + 3 + 1)) x (n + 1) = (ring (4 * n + 3 + 1)).one
    · rw [ite_pos' _ _ h1] at h; exact absurd (congrArg Prod.fst h) (fun e => Int.noConfusion e)
    · rw [ite_neg' _ _ h1] at h
      by_cases h2 : pw (ring (4 * n + 3 + 1)) x (n + 1) = ι
      · rw [ite_pos' _ _ h2] at h; exact absurd (congrArg Prod.fst h) (fun e => Int.noConfusion e)
      · rw [ite_neg' _ _ h2] at h
        by_cases h3 : pw (ring (4 * n + 3 + 1)) x (n + 1) = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one
        · exact h3
        · rw [ite_neg' _ _ h3] at h; exact absurd (congrArg Prod.fst h) (fun e => Int.noConfusion e)

/-- ρ(a) = 1 ⇒ a^{(p−1)/2} = 1. -/
theorem rho_one_pw (_hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (_hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (a : Zp (4 * n + 3 + 1)) (h : rho n ι a = (1, 0)) : pw (ring (4 * n + 3 + 1)) a (2 * n + 2) = (ring (4 * n + 3 + 1)).one := by
  rw [show 2 * n + 2 = (n + 1) * 2 by lib_nat, ← TwoPower.pwpow]
  unfold rho at h
  rcases ResidueCriterion.chi4_cases n ι a with e | e | e | e | e
  · rw [e] at h; exact absurd (congrArg Prod.fst h) (fun e => Nat.noConfusion (Int.ofNat.inj e))
  · rw [chi4_one_pw n ι a e]; show (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one) (ring (4 * n + 3 + 1)).one = _; lib_ring
  · rw [e] at h; exact absurd (congrArg Prod.fst h) (fun e => Int.noConfusion e)
  · rw [chi4_m1_pw n ι a e]; show (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul (ring (4 * n + 3 + 1)).one ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) = _; lib_ring
  · rw [e] at h; exact absurd (congrArg Prod.fst h) (fun e => Int.noConfusion e)

/-- **#{x : x² = a} = 1 + ρ(a).** -/
theorem sq_count (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (a : Zp (4 * n + 3 + 1)) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun x => delta (Cyclo4.cyc4 LibInt.intR) ((ring (4 * n + 3 + 1)).mul x x) a) (LL n) = (Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one (rho n ι a) := by
  by_cases ha : a = (ring (4 * n + 3 + 1)).zero
  · rw [ha, sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun x => delta (Cyclo4.cyc4 LibInt.intR) x (ring (4 * n + 3 + 1)).zero) (fun x => by
      unfold delta
      by_cases hx : x = (ring (4 * n + 3 + 1)).zero
      · rw [ite_pos' _ _ hx, ite_pos' _ _ (by rw [hx]; lib_ring)]
      · rw [ite_neg' _ _ hx, ite_neg' _ _ (fun e => hx (by
          cases nozd (4 * n + 3) hpr _ _ e with
          | inl h => exact h
          | inr h => exact h))]), show sumK (Cyclo4.cyc4 LibInt.intR) (fun x => delta (Cyclo4.cyc4 LibInt.intR) x (ring (4 * n + 3 + 1)).zero) (LL n) = (Cyclo4.cyc4 LibInt.intR).one from CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR) (ring (4 * n + 3 + 1)).zero, rho_zero]; lib_ring
  · rcases QuartPrimary.rho_real n hpr ι hi a ha with e | e
    · match sq_root n hpr a ha (rho_one_pw n hpr ι hi a e) with
      | ⟨c, hc⟩ =>
        have hc0 : c ≠ (ring (4 * n + 3 + 1)).zero := fun e0 => ha (by rw [← hc, e0]; lib_ring)
        rw [← hc, sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun x => two_roots n hpr c hc0 x), sumK_add, show sumK (Cyclo4.cyc4 LibInt.intR) (fun x => delta (Cyclo4.cyc4 LibInt.intR) x c) (LL n) = (Cyclo4.cyc4 LibInt.intR).one from CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR) c,
          show sumK (Cyclo4.cyc4 LibInt.intR) (fun x => delta (Cyclo4.cyc4 LibInt.intR) x ((ring (4 * n + 3 + 1)).neg c)) (LL n) = (Cyclo4.cyc4 LibInt.intR).one from CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR) ((ring (4 * n + 3 + 1)).neg c), hc, e]; rfl
    · have hz : ∀ x, delta (Cyclo4.cyc4 LibInt.intR) ((ring (4 * n + 3 + 1)).mul x x) a = (Cyclo4.cyc4 LibInt.intR).zero := fun x => by
        unfold delta
        rw [ite_neg' _ _ (fun ex => by
          have hx0 : x ≠ (ring (4 * n + 3 + 1)).zero := fun e0 => ha (by rw [← ex, e0]; lib_ring)
          have h1 := QuartPrimary.rho_sq n hpr ι hi x hx0
          rw [ex, e] at h1
          exact absurd (congrArg Prod.fst h1) (fun e' => Int.noConfusion e'))]
      rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ hz, HeckePrime.zsumR (Cyclo4.cyc4 LibInt.intR), e]; rfl

/-- J(ρ, χ) = Σ_t ρ(t) χ(1 − t). -/
def J2 (ι : Zp (4 * n + 3 + 1)) : Int × Int := sumK (Cyclo4.cyc4 LibInt.intR) (fun t => (Cyclo4.cyc4 LibInt.intR).mul (rho n ι t) (chi4 n ι (om n t))) (LL n)

def sh (t : Zp (4 * n + 3 + 1)) : Zp (4 * n + 3 + 1) := (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).add t t) ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)
def shi (u : Zp (4 * n + 3 + 1)) : Zp (4 * n + 3 + 1) := (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).add u (ring (4 * n + 3 + 1)).one) (h n)

theorem sh_shi (u : Zp (4 * n + 3 + 1)) : sh n (shi n u) = u := by
  unfold sh shi
  calc _ = (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).add u (ring (4 * n + 3 + 1)).one) ((ring (4 * n + 3 + 1)).add (h n) (h n))) ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by lib_ring
    _ = u := by rw [hh]; lib_ring

theorem shi_sh (t : Zp (4 * n + 3 + 1)) : shi n (sh n t) = t := by
  unfold sh shi
  calc _ = (ring (4 * n + 3 + 1)).mul t ((ring (4 * n + 3 + 1)).add (h n) (h n)) := by lib_ring
    _ = t := by rw [hh]; lib_ring

theorem four_id (t : Zp (4 * n + 3 + 1)) : (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul (mtwo n) (mtwo n)) ((ring (4 * n + 3 + 1)).mul t (om n t)) = om n ((ring (4 * n + 3 + 1)).mul (sh n t) (sh n t)) := by
  unfold mtwo om sh; lib_ring

/-- **Duplication**: J(χ, χ) = χ(−1) J(ρ, χ). -/
theorem quart_dup (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) :
    Jq n (chi4 n ι) = (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (J2 n ι) := by
  -- χ(4) J = Σ_s χ(1 − s²)
  have eA : (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).mul (mtwo n) (mtwo n))) (Jq n (chi4 n ι)) = sumK (Cyclo4.cyc4 LibInt.intR) (fun s => chi4 n ι (om n ((ring (4 * n + 3 + 1)).mul s s))) (LL n) := by
    rw [show Jq n (chi4 n ι) = sumK (Cyclo4.cyc4 LibInt.intR) (f n ι) (LL n) from rfl, mul_sumK]
    rw [← FiniteRecChain.reindex (Cyclo4.cyc4 LibInt.intR) (LL n) (CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR)) (sh n) (shi n) (shi_sh n) (sh_shi n) (fun s => chi4 n ι (om n ((ring (4 * n + 3 + 1)).mul s s)))]
    exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun t => by rw [f_eq n hpr ι hi, ← chi4_mul n hpr ι hi, four_id]) _
  -- Σ_s χ(1 − s²) = Σ_v (1 + ρ(v)) χ(1 − v)
  have eB : sumK (Cyclo4.cyc4 LibInt.intR) (fun s => chi4 n ι (om n ((ring (4 * n + 3 + 1)).mul s s))) (LL n) = sumK (Cyclo4.cyc4 LibInt.intR) (fun v => (Cyclo4.cyc4 LibInt.intR).mul (sumK (Cyclo4.cyc4 LibInt.intR) (fun s => delta (Cyclo4.cyc4 LibInt.intR) ((ring (4 * n + 3 + 1)).mul s s) v) (LL n)) (chi4 n ι (om n v))) (LL n) := by
    rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun s => sumK (Cyclo4.cyc4 LibInt.intR) (fun v => (Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) v ((ring (4 * n + 3 + 1)).mul s s)) (chi4 n ι (om n v))) (LL n))
      (fun s => (FiniteRecChain.pick (Cyclo4.cyc4 LibInt.intR) (LL n) (CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR)) (fun v => chi4 n ι (om n v)) _).symm), sumK_swap]
    exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun v => by
      rw [sumK_mul]; exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun s => by rw [delta_symm]) _) _
  have eC : sumK (Cyclo4.cyc4 LibInt.intR) (fun v => (Cyclo4.cyc4 LibInt.intR).mul (sumK (Cyclo4.cyc4 LibInt.intR) (fun s => delta (Cyclo4.cyc4 LibInt.intR) ((ring (4 * n + 3 + 1)).mul s s) v) (LL n)) (chi4 n ι (om n v))) (LL n) = J2 n ι := by
    rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun v => (Cyclo4.cyc4 LibInt.intR).add (chi4 n ι (om n v)) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι v) (chi4 n ι (om n v)))) (fun v => by rw [sq_count n hpr ι hi]; lib_ring), sumK_add]
    have e0 : sumK (Cyclo4.cyc4 LibInt.intR) (fun v => chi4 n ι (om n v)) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := by
      rw [FiniteRecChain.reindex (Cyclo4.cyc4 LibInt.intR) (LL n) (CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR)) (om n) (om n) (om_om n) (om_om n) (chi4 n ι)]
      exact chi4_sum n hpr ι hi
    rw [e0]; unfold J2; lib_ring
  have e4 : chi4 n ι ((ring (4 * n + 3 + 1)).mul (mtwo n) (mtwo n)) = chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by
    rw [chi4_mul n hpr ι hi]; exact QuartPrimary.rho_two n hpr ι hi
  have hs : (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) = (Cyclo4.cyc4 LibInt.intR).one := by
    rw [← chi4_mul n hpr ι hi, show (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) = (ring (4 * n + 3 + 1)).one by lib_ring]; exact chi_one n hpr ι hi
  rw [e4, eB, eC] at eA
  calc Jq n (chi4 n ι) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one))) (Jq n (chi4 n ι)) := by rw [hs]; lib_ring
    _ = (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (Jq n (chi4 n ι))) := by lib_ring
    _ = _ := by rw [eA]

end QuartDup

#print axioms QuartDup.sq_root
#print axioms QuartDup.mtwo_ne
#print axioms QuartDup.two_roots
#print axioms QuartDup.chi4_one_pw
#print axioms QuartDup.chi4_m1_pw
#print axioms QuartDup.rho_one_pw
#print axioms QuartDup.sq_count
#print axioms QuartDup.sh_shi
#print axioms QuartDup.shi_sh
#print axioms QuartDup.four_id
#print axioms QuartDup.quart_dup

lang/QuartFive.lean

lean_all/lang/QuartFive.lean

/-
  lang/QuartFive — T43: the quartic character of 5 (generated by work/gen_quart_table.py).

  p = 4(n + 1) + 5 (so p > 5), χ the quartic character of 𝔽_p, J = J(χ, χ) = a + bi, N(J) = a² + b² = p. QuartRecip at the split
  prime q = 5 (k = 0) gives χ̄(q) = (J²)^1 p^1 in 𝔽_5[i], and a 25-case table over 𝔽_5 (through the builtin Nat.mod, proved equal
  to ring q pointwise) shows that this is 1 iff b(b² − 2a²) ≡ 0 (when N(J) ≢ 0):
      5 is a quartic residue mod p  ⇔  5 | b(b² − 2a²).
    table, table'      the table (both directions)
    quart_five, quart_five_conv
  Lean 4 core only, proofs by hand (lib_nat; the table by rfl on closed terms), no axioms.
-/
import lang.QuartRecip
import lang.CubicThirteen

namespace QuartFive

open FTAChain ZMod DetChain Frobenius QuartChar QuartJacobi QuartRecip LibGcd CubicFive
open LibRing (castZ castN)

theorem lt_wrap : ∀ q a : Nat, a + 1 + q = q → False
  | 0, _, h => Nat.noConfusion h
  | q + 1, a, h => lt_wrap q a (Nat.succ.inj h)

theorem ndvN (N m' q : Nat) (h : N = q * (m' + 1)) (hr : rem (m' + 1) N = 0 → False) : False :=
  hr (by rw [h, show q * (m' + 1) = q * (m' + 1) + 0 from rfl, rem_add_mul]; rfl)

theorem q_prime : ∀ d, Dv d (4 * 0 + 3 + 1 + 1) → d = 1 ∨ d = 4 * 0 + 3 + 1 + 1 := fun d hd =>
  match dv_le d 4 hd, hd with
  | ⟨k, hk⟩, ⟨q', hq⟩ => by
    match d with
    | 0 => exact absurd hq (fun e => Nat.noConfusion e)
    | 1 => exact Or.inl rfl
    | 2 => exact (ndvN 5 1 q' hq (fun e => Nat.noConfusion e)).elim
    | 3 => exact (ndvN 5 2 q' hq (fun e => Nat.noConfusion e)).elim
    | 4 => exact (ndvN 5 3 q' hq (fun e => Nat.noConfusion e)).elim
    | 5 => exact Or.inr rfl
    | d + 6 => exact (lt_wrap 5 (d + k) ((show d + k + 1 + 5 = d + 6 + k by lib_nat).trans hk)).elim

variable (n : Nat)

theorem q_ne : nsm (ring (4 * (n + 1) + 3 + 1)) (4 * 0 + 3 + 1 + 1) (ring (4 * (n + 1) + 3 + 1)).one ≠ (ring (4 * (n + 1) + 3 + 1)).zero := fun e => by
  have h := congrArg Subtype.val e
  rw [nsm_one_val, md_small _ 5 (by rw [show 4 * (n + 1) + 3 + 1 + 1 = 5 + (4 * n + 3) + 1 by lib_nat]; exact Nat.lt_succ_of_le (Nat.le_add_right 5 _))] at h
  exact Nat.noConfusion h

theorem not_dvq (hpr : ∀ d, Dv d (4 * (n + 1) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (n + 1) + 3 + 1 + 1) (h : Dv (4 * 0 + 3 + 1 + 1) (4 * (n + 1) + 3 + 1 + 1)) : False :=
  match hpr 5 h with
  | Or.inl e => Nat.noConfusion (Nat.succ.inj e)
  | Or.inr e => lt_wrap 5 (4 * n + 3) ((show 4 * n + 3 + 1 + 5 = 4 * (n + 1) + 3 + 1 + 1 by lib_nat).trans e.symm)

abbrev F := ring (4 * 0 + 3 + 1)
abbrev C := Cyclo4.cyc4 F

def nrm (X Y : Zp (4 * 0 + 3 + 1)) : Zp (4 * 0 + 3 + 1) := F.add (F.mul X X) (F.mul Y Y)
def lhs (X Y : Zp (4 * 0 + 3 + 1)) : Zp (4 * 0 + 3 + 1) × Zp (4 * 0 + 3 + 1) :=
  C.mul (pw C (C.mul (C.mul (X, Y) (X, Y)) C.one) 1) (pw C (nrm X Y, F.zero) 1)
def rhs (X Y : Zp (4 * 0 + 3 + 1)) : Zp (4 * 0 + 3 + 1) := F.mul Y (F.add (F.mul Y Y) (F.neg (F.mul (castZ F 2) (F.mul X X))))

theorem md_red (a r k : Nat) (h : a = r + k * (4 * 0 + 3 + 1 + 1)) (hr : r < 4 * 0 + 3 + 1 + 1) : md (4 * 0 + 3 + 1) a = r := by
  rw [h, md_add_mul]; exact md_small _ r hr

def fm (a : Nat) : Zp (4 * 0 + 3 + 1) := ⟨a % 5, Nat.mod_lt _ (Nat.zero_lt_succ _)⟩
def fadd (x y : Zp (4 * 0 + 3 + 1)) : Zp (4 * 0 + 3 + 1) := fm (x.val + y.val)
def fmul (x y : Zp (4 * 0 + 3 + 1)) : Zp (4 * 0 + 3 + 1) := fm (x.val * y.val)
def fneg (x : Zp (4 * 0 + 3 + 1)) : Zp (4 * 0 + 3 + 1) := fm ((4 * 0 + 3 + 1) * x.val)

theorem add_nat : ∀ x y : Nat, x < 5 → y < 5 → md (4 * 0 + 3 + 1) (x + y) = (x + y) % 5
  | 0, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 4 : 0 < 4 * 0 + 3 + 1 + 1)
  | 0, 1, _, _ => md_red _ 1 0 rfl (Nat.le_add_right 2 3 : 1 < 4 * 0 + 3 + 1 + 1)
  | 0, 2, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 2 : 2 < 4 * 0 + 3 + 1 + 1)
  | 0, 3, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 1 : 3 < 4 * 0 + 3 + 1 + 1)
  | 0, 4, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 0 : 4 < 4 * 0 + 3 + 1 + 1)
  | 1, 0, _, _ => md_red _ 1 0 rfl (Nat.le_add_right 2 3 : 1 < 4 * 0 + 3 + 1 + 1)
  | 1, 1, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 2 : 2 < 4 * 0 + 3 + 1 + 1)
  | 1, 2, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 1 : 3 < 4 * 0 + 3 + 1 + 1)
  | 1, 3, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 0 : 4 < 4 * 0 + 3 + 1 + 1)
  | 1, 4, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 4 : 0 < 4 * 0 + 3 + 1 + 1)
  | 2, 0, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 2 : 2 < 4 * 0 + 3 + 1 + 1)
  | 2, 1, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 1 : 3 < 4 * 0 + 3 + 1 + 1)
  | 2, 2, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 0 : 4 < 4 * 0 + 3 + 1 + 1)
  | 2, 3, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 4 : 0 < 4 * 0 + 3 + 1 + 1)
  | 2, 4, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 3 : 1 < 4 * 0 + 3 + 1 + 1)
  | 3, 0, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 1 : 3 < 4 * 0 + 3 + 1 + 1)
  | 3, 1, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 0 : 4 < 4 * 0 + 3 + 1 + 1)
  | 3, 2, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 4 : 0 < 4 * 0 + 3 + 1 + 1)
  | 3, 3, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 3 : 1 < 4 * 0 + 3 + 1 + 1)
  | 3, 4, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 2 : 2 < 4 * 0 + 3 + 1 + 1)
  | 4, 0, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 0 : 4 < 4 * 0 + 3 + 1 + 1)
  | 4, 1, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 4 : 0 < 4 * 0 + 3 + 1 + 1)
  | 4, 2, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 3 : 1 < 4 * 0 + 3 + 1 + 1)
  | 4, 3, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 2 : 2 < 4 * 0 + 3 + 1 + 1)
  | 4, 4, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 1 : 3 < 4 * 0 + 3 + 1 + 1)
  | k + 5, _, h, _ => absurd h (fun e => Nat.lt_irrefl 5 (Nat.lt_of_le_of_lt (Nat.le_add_left 5 k) e))
  | _, k + 5, _, h => absurd h (fun e => Nat.lt_irrefl 5 (Nat.lt_of_le_of_lt (Nat.le_add_left 5 k) e))

theorem mul_nat : ∀ x y : Nat, x < 5 → y < 5 → md (4 * 0 + 3 + 1) (x * y) = (x * y) % 5
  | 0, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 4 : 0 < 4 * 0 + 3 + 1 + 1)
  | 0, 1, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 4 : 0 < 4 * 0 + 3 + 1 + 1)
  | 0, 2, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 4 : 0 < 4 * 0 + 3 + 1 + 1)
  | 0, 3, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 4 : 0 < 4 * 0 + 3 + 1 + 1)
  | 0, 4, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 4 : 0 < 4 * 0 + 3 + 1 + 1)
  | 1, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 4 : 0 < 4 * 0 + 3 + 1 + 1)
  | 1, 1, _, _ => md_red _ 1 0 rfl (Nat.le_add_right 2 3 : 1 < 4 * 0 + 3 + 1 + 1)
  | 1, 2, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 2 : 2 < 4 * 0 + 3 + 1 + 1)
  | 1, 3, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 1 : 3 < 4 * 0 + 3 + 1 + 1)
  | 1, 4, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 0 : 4 < 4 * 0 + 3 + 1 + 1)
  | 2, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 4 : 0 < 4 * 0 + 3 + 1 + 1)
  | 2, 1, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 2 : 2 < 4 * 0 + 3 + 1 + 1)
  | 2, 2, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 0 : 4 < 4 * 0 + 3 + 1 + 1)
  | 2, 3, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 3 : 1 < 4 * 0 + 3 + 1 + 1)
  | 2, 4, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 1 : 3 < 4 * 0 + 3 + 1 + 1)
  | 3, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 4 : 0 < 4 * 0 + 3 + 1 + 1)
  | 3, 1, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 1 : 3 < 4 * 0 + 3 + 1 + 1)
  | 3, 2, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 3 : 1 < 4 * 0 + 3 + 1 + 1)
  | 3, 3, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 0 : 4 < 4 * 0 + 3 + 1 + 1)
  | 3, 4, _, _ => md_red _ 2 2 rfl (Nat.le_add_right 3 2 : 2 < 4 * 0 + 3 + 1 + 1)
  | 4, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 4 : 0 < 4 * 0 + 3 + 1 + 1)
  | 4, 1, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 0 : 4 < 4 * 0 + 3 + 1 + 1)
  | 4, 2, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 1 : 3 < 4 * 0 + 3 + 1 + 1)
  | 4, 3, _, _ => md_red _ 2 2 rfl (Nat.le_add_right 3 2 : 2 < 4 * 0 + 3 + 1 + 1)
  | 4, 4, _, _ => md_red _ 1 3 rfl (Nat.le_add_right 2 3 : 1 < 4 * 0 + 3 + 1 + 1)
  | k + 5, _, h, _ => absurd h (fun e => Nat.lt_irrefl 5 (Nat.lt_of_le_of_lt (Nat.le_add_left 5 k) e))
  | _, k + 5, _, h => absurd h (fun e => Nat.lt_irrefl 5 (Nat.lt_of_le_of_lt (Nat.le_add_left 5 k) e))

theorem neg_nat : ∀ x : Nat, x < 5 → md (4 * 0 + 3 + 1) ((4 * 0 + 3 + 1) * x) = ((4 * 0 + 3 + 1) * x) % 5
  | 0, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 4 : 0 < 4 * 0 + 3 + 1 + 1)
  | 1, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 0 : 4 < 4 * 0 + 3 + 1 + 1)
  | 2, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 1 : 3 < 4 * 0 + 3 + 1 + 1)
  | 3, _ => md_red _ 2 2 rfl (Nat.le_add_right 3 2 : 2 < 4 * 0 + 3 + 1 + 1)
  | 4, _ => md_red _ 1 3 rfl (Nat.le_add_right 2 3 : 1 < 4 * 0 + 3 + 1 + 1)
  | k + 5, h => absurd h (fun e => Nat.lt_irrefl 5 (Nat.lt_of_le_of_lt (Nat.le_add_left 5 k) e))

theorem addE (x y : Zp (4 * 0 + 3 + 1)) : F.add x y = fadd x y := Subtype.ext (add_nat x.val y.val x.2 y.2)
theorem mulE (x y : Zp (4 * 0 + 3 + 1)) : F.mul x y = fmul x y := Subtype.ext (mul_nat x.val y.val x.2 y.2)
theorem negE (x : Zp (4 * 0 + 3 + 1)) : F.neg x = fneg x := Subtype.ext (by show md (4 * 0 + 3 + 1) ((4 * 0 + 3 + 1) * x.val) = ((4 * 0 + 3 + 1) * x.val) % 5; exact neg_nat x.val x.2)

def c4f (u v : Zp (4 * 0 + 3 + 1) × Zp (4 * 0 + 3 + 1)) : Zp (4 * 0 + 3 + 1) × Zp (4 * 0 + 3 + 1) :=
  (fadd (fmul u.1 v.1) (fneg (fmul u.2 v.2)), fadd (fmul u.1 v.2) (fmul u.2 v.1))
theorem c4E (u v : Zp (4 * 0 + 3 + 1) × Zp (4 * 0 + 3 + 1)) : C.mul u v = c4f u v := by
  show Cyclo4.c4mul F u v = _
  unfold Cyclo4.c4mul
  rw [mulE, mulE, mulE, mulE, negE, addE, addE]; rfl
def pwf (z : Zp (4 * 0 + 3 + 1) × Zp (4 * 0 + 3 + 1)) : Nat → Zp (4 * 0 + 3 + 1) × Zp (4 * 0 + 3 + 1)
  | 0 => C.one
  | k + 1 => c4f (pwf z k) z
theorem pwE (z : Zp (4 * 0 + 3 + 1) × Zp (4 * 0 + 3 + 1)) : ∀ k, pw C z k = pwf z k
  | 0 => rfl
  | k + 1 => by show C.mul (pw C z k) z = _; rw [c4E, pwE z k]; rfl

def nrmf (X Y : Zp (4 * 0 + 3 + 1)) : Zp (4 * 0 + 3 + 1) := fadd (fmul X X) (fmul Y Y)
def lhsf (X Y : Zp (4 * 0 + 3 + 1)) : Zp (4 * 0 + 3 + 1) × Zp (4 * 0 + 3 + 1) := c4f (pwf (c4f (c4f (X, Y) (X, Y)) C.one) 1) (pwf (nrmf X Y, F.zero) 1)
def rhsf (X Y : Zp (4 * 0 + 3 + 1)) : Zp (4 * 0 + 3 + 1) := fmul Y (fadd (fmul Y Y) (fneg (fmul (castZ F 2) (fmul X X))))

theorem nrmE (X Y : Zp (4 * 0 + 3 + 1)) : nrm X Y = nrmf X Y := by
  unfold nrm nrmf; rw [mulE, mulE, addE]
theorem lhsE (X Y : Zp (4 * 0 + 3 + 1)) : lhs X Y = lhsf X Y := by
  unfold lhs lhsf; rw [nrmE, c4E, c4E, pwE, pwE, c4E]
theorem rhsE (X Y : Zp (4 * 0 + 3 + 1)) : rhs X Y = rhsf X Y := by
  unfold rhs rhsf
  repeat (first | rw [mulE] | rw [addE] | rw [negE])

def code (z : Zp (4 * 0 + 3 + 1) × Zp (4 * 0 + 3 + 1)) : Nat := z.1.val + 5 * z.2.val
def chk (X Y : Zp (4 * 0 + 3 + 1)) : Bool := (!(Nat.beq (code (lhsf X Y)) 1) || Nat.beq (rhsf X Y).val 0) &&
  (!(Nat.beq (rhsf X Y).val 0) || Nat.beq (nrmf X Y).val 0 || Nat.beq (code (lhsf X Y)) 1)

theorem row0 : ∀ y : Nat, y < 5 → chk (mk (4 * 0 + 3 + 1) 0) (mk (4 * 0 + 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | k + 5, h => absurd h (fun e => Nat.lt_irrefl 5 (Nat.lt_of_le_of_lt (Nat.le_add_left 5 k) e))

theorem row1 : ∀ y : Nat, y < 5 → chk (mk (4 * 0 + 3 + 1) 1) (mk (4 * 0 + 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | k + 5, h => absurd h (fun e => Nat.lt_irrefl 5 (Nat.lt_of_le_of_lt (Nat.le_add_left 5 k) e))

theorem row2 : ∀ y : Nat, y < 5 → chk (mk (4 * 0 + 3 + 1) 2) (mk (4 * 0 + 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | k + 5, h => absurd h (fun e => Nat.lt_irrefl 5 (Nat.lt_of_le_of_lt (Nat.le_add_left 5 k) e))

theorem row3 : ∀ y : Nat, y < 5 → chk (mk (4 * 0 + 3 + 1) 3) (mk (4 * 0 + 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | k + 5, h => absurd h (fun e => Nat.lt_irrefl 5 (Nat.lt_of_le_of_lt (Nat.le_add_left 5 k) e))

theorem row4 : ∀ y : Nat, y < 5 → chk (mk (4 * 0 + 3 + 1) 4) (mk (4 * 0 + 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | k + 5, h => absurd h (fun e => Nat.lt_irrefl 5 (Nat.lt_of_le_of_lt (Nat.le_add_left 5 k) e))

theorem chk_nat : ∀ x y : Nat, x < 5 → y < 5 → chk (mk (4 * 0 + 3 + 1) x) (mk (4 * 0 + 3 + 1) y) = true
  | 0, y, _, h => row0 y h
  | 1, y, _, h => row1 y h
  | 2, y, _, h => row2 y h
  | 3, y, _, h => row3 y h
  | 4, y, _, h => row4 y h
  | k + 5, _, h, _ => absurd h (fun e => Nat.lt_irrefl 5 (Nat.lt_of_le_of_lt (Nat.le_add_left 5 k) e))

theorem chk_all (X Y : Zp (4 * 0 + 3 + 1)) : chk X Y = true := by
  rw [show X = mk (4 * 0 + 3 + 1) X.val from Subtype.ext (md_small _ _ X.2).symm, show Y = mk (4 * 0 + 3 + 1) Y.val from Subtype.ext (md_small _ _ Y.2).symm]
  exact chk_nat X.val Y.val X.2 Y.2

theorem table (X Y : Zp (4 * 0 + 3 + 1)) (h : lhs X Y = C.one) : rhs X Y = F.zero := by
  have hc := band_l _ _ (chk_all X Y)
  rw [← lhsE, h] at hc
  rw [rhsE]
  exact (Subtype.ext (beq_eq _ _ hc) : rhsf X Y = ⟨0, Nat.zero_lt_succ _⟩)

theorem table' (X Y : Zp (4 * 0 + 3 + 1)) (h : rhs X Y = F.zero) (hn : nrm X Y ≠ F.zero) : lhs X Y = C.one := by
  have hc := band_r _ _ (chk_all X Y)
  rw [← rhsE, h] at hc
  rw [lhsE]
  rw [nrmE] at hn
  cases hb : Nat.beq (nrmf X Y).val 0 with
  | true => exact absurd (Subtype.ext (beq_eq _ _ hb) : nrmf X Y = F.zero) hn
  | false =>
    rw [hb] at hc
    have e := beq_eq _ _ hc
    match lhsf X Y, e with
    | (u, v), e =>
      have e' : u.val + 5 * v.val = 1 := e
      have hv : v.val = 0 := by
        cases hv' : v.val with
        | zero => rfl
        | succ t => rw [hv', show 5 * (t + 1) = 5 * t + 4 + 1 by lib_nat, show u.val + (5 * t + 4 + 1) = u.val + 5 * t + 4 + 1 by lib_nat] at e'; exact Nat.noConfusion (Nat.succ.inj e')
      have hu : u.val = 1 := by rw [hv] at e'; exact e'
      exact Prod.ext (Subtype.ext hu) (Subtype.ext hv)

theorem cast_norm (x y : Int) : castZ F (x * x + y * y) = nrm (castZ F x) (castZ F y) := by
  rw [IntCast.castZ_add, HasseDavenport.castZ_mul, HasseDavenport.castZ_mul]; rfl

theorem cast_rhs (x y : Int) : castZ F (y * (y * y - 2 * (x * x))) = rhs (castZ F x) (castZ F y) := by
  rw [show y * (y * y - 2 * (x * x)) = y * (y * y + -(2 * (x * x))) from rfl]
  repeat (first | rw [HasseDavenport.castZ_mul] | rw [IntCast.castZ_add] | rw [HasseDavenport.castZ_neg])
  rfl

theorem hP (hpr : ∀ d, Dv d (4 * (n + 1) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (n + 1) + 3 + 1 + 1) (ι : Zp (4 * (n + 1) + 3 + 1)) (hi : (ring (4 * (n + 1) + 3 + 1)).mul ι ι = (ring (4 * (n + 1) + 3 + 1)).neg (ring (4 * (n + 1) + 3 + 1)).one) :
    ph4 F (Ppq (n + 1)) = (nrm (castZ F (Jq (n + 1) (chi4 (n + 1) ι)).1) (castZ F (Jq (n + 1) (chi4 (n + 1) ι)).2), F.zero) := by
  rw [show Ppq (n + 1) = (Int.ofNat (4 * (n + 1) + 3 + 1 + 1), 0) from sum_oneq (n + 1) _, ← quart_norm (n + 1) hpr ι hi]; unfold ph4; rw [cast_norm]; rfl

theorem chi4_cases (ι x : Zp (4 * (n + 1) + 3 + 1)) : chi4 (n + 1) ι x = (0, 0) ∨ chi4 (n + 1) ι x = (1, 0) ∨ chi4 (n + 1) ι x = (0, 1) ∨ chi4 (n + 1) ι x = (-1, 0) ∨ chi4 (n + 1) ι x = (0, -1) := by
  unfold chi4
  by_cases h1 : x = (ring (4 * (n + 1) + 3 + 1)).zero
  · exact Or.inl (ite_t _ _ h1)
  · rw [ite_f _ _ h1]
    by_cases h2 : pw (ring (4 * (n + 1) + 3 + 1)) x ((n + 1) + 1) = (ring (4 * (n + 1) + 3 + 1)).one
    · exact Or.inr (Or.inl (ite_t _ _ h2))
    · rw [ite_f _ _ h2]
      by_cases h3 : pw (ring (4 * (n + 1) + 3 + 1)) x ((n + 1) + 1) = ι
      · exact Or.inr (Or.inr (Or.inl (ite_t _ _ h3)))
      · rw [ite_f _ _ h3]
        by_cases h4 : pw (ring (4 * (n + 1) + 3 + 1)) x ((n + 1) + 1) = (ring (4 * (n + 1) + 3 + 1)).neg (ring (4 * (n + 1) + 3 + 1)).one
        · exact Or.inr (Or.inr (Or.inr (Or.inl (ite_t _ _ h4))))
        · exact Or.inr (Or.inr (Or.inr (Or.inr (ite_f _ _ h4))))

theorem lhs_one (hpr : ∀ d, Dv d (4 * (n + 1) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (n + 1) + 3 + 1 + 1) (ι : Zp (4 * (n + 1) + 3 + 1)) (hi : (ring (4 * (n + 1) + 3 + 1)).mul ι ι = (ring (4 * (n + 1) + 3 + 1)).neg (ring (4 * (n + 1) + 3 + 1)).one) :
    ph4 F (chib (n + 1) ι (nsm (ring (4 * (n + 1) + 3 + 1)) (4 * 0 + 3 + 1 + 1) (ring (4 * (n + 1) + 3 + 1)).one)) = lhs (castZ F (Jq (n + 1) (chi4 (n + 1) ι)).1) (castZ F (Jq (n + 1) (chi4 (n + 1) ι)).2) := by
  rw [quart_recip_split (n + 1) 0 hpr q_prime ι hi (q_ne n), hP n hpr ι hi, ph4_one]; rfl

/-- **5 a quartic residue mod p ⇒ 5 | b(b² − 2a²)**. -/
theorem quart_five (hpr : ∀ d, Dv d (4 * (n + 1) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (n + 1) + 3 + 1 + 1) (ι : Zp (4 * (n + 1) + 3 + 1)) (hi : (ring (4 * (n + 1) + 3 + 1)).mul ι ι = (ring (4 * (n + 1) + 3 + 1)).neg (ring (4 * (n + 1) + 3 + 1)).one)
    (hq : chi4 (n + 1) ι (nsm (ring (4 * (n + 1) + 3 + 1)) (4 * 0 + 3 + 1 + 1) (ring (4 * (n + 1) + 3 + 1)).one) = (1, 0)) : IDv (Int.ofNat 5) ((Jq (n + 1) (chi4 (n + 1) ι)).2 * ((Jq (n + 1) (chi4 (n + 1) ι)).2 * (Jq (n + 1) (chi4 (n + 1) ι)).2 - 2 * ((Jq (n + 1) (chi4 (n + 1) ι)).1 * (Jq (n + 1) (chi4 (n + 1) ι)).1))) := by
  have hc := lhs_one n hpr ι hi
  rw [show chib (n + 1) ι (nsm (ring (4 * (n + 1) + 3 + 1)) (4 * 0 + 3 + 1 + 1) (ring (4 * (n + 1) + 3 + 1)).one) = gconj (chi4 (n + 1) ι (nsm (ring (4 * (n + 1) + 3 + 1)) (4 * 0 + 3 + 1 + 1) (ring (4 * (n + 1) + 3 + 1)).one)) from chi_ivq (n + 1) hpr ι hi _, hq] at hc
  have hl : lhs (castZ F (Jq (n + 1) (chi4 (n + 1) ι)).1) (castZ F (Jq (n + 1) (chi4 (n + 1) ι)).2) = C.one := hc.symm.trans (Prod.ext (rfl : castZ F 1 = F.one) (rfl : castZ F (-0) = F.zero))
  exact CubicTwo.cast_zero_gen (4 * 0 + 3 + 1) _ ((cast_rhs _ _).trans (table _ _ hl))

/-- **5 | b(b² − 2a²) ⇒ 5 is a quartic residue mod p.** -/
theorem quart_five_conv (hpr : ∀ d, Dv d (4 * (n + 1) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (n + 1) + 3 + 1 + 1) (ι : Zp (4 * (n + 1) + 3 + 1)) (hi : (ring (4 * (n + 1) + 3 + 1)).mul ι ι = (ring (4 * (n + 1) + 3 + 1)).neg (ring (4 * (n + 1) + 3 + 1)).one)
    (hd : IDv (Int.ofNat 5) ((Jq (n + 1) (chi4 (n + 1) ι)).2 * ((Jq (n + 1) (chi4 (n + 1) ι)).2 * (Jq (n + 1) (chi4 (n + 1) ι)).2 - 2 * ((Jq (n + 1) (chi4 (n + 1) ι)).1 * (Jq (n + 1) (chi4 (n + 1) ι)).1)))) : chi4 (n + 1) ι (nsm (ring (4 * (n + 1) + 3 + 1)) (4 * 0 + 3 + 1 + 1) (ring (4 * (n + 1) + 3 + 1)).one) = (1, 0) := by
  have hc := lhs_one n hpr ι hi
  have hr : rhs (castZ F (Jq (n + 1) (chi4 (n + 1) ι)).1) (castZ F (Jq (n + 1) (chi4 (n + 1) ι)).2) = F.zero := by
    rw [← cast_rhs]
    match hd with
    | ⟨c, hc'⟩ =>
      rw [hc', HasseDavenport.castZ_mul]
      have h0 : castZ F (Int.ofNat 5) = F.zero := CubeAp.castP 3
      rw [h0, zero_mul']
  have hn : nrm (castZ F (Jq (n + 1) (chi4 (n + 1) ι)).1) (castZ F (Jq (n + 1) (chi4 (n + 1) ι)).2) ≠ F.zero := fun e => by
    have e1 : castZ F (Int.ofNat (4 * (n + 1) + 3 + 1 + 1)) = F.zero := by rw [← quart_norm (n + 1) hpr ι hi, cast_norm]; exact e
    have e2 : mk (4 * 0 + 3 + 1) (4 * (n + 1) + 3 + 1 + 1) = F.zero := (IntCast.castN_mk _ _).symm.trans e1
    have hdq : ∀ P' : Nat, mk (4 * 0 + 3 + 1) P' = F.zero → Dv (4 * 0 + 3 + 1 + 1) P' := fun P' h => md_zero_dv (4 * 0 + 3 + 1) P' (congrArg Subtype.val h)
    exact not_dvq n hpr (hdq _ e2)
  have h1 := hc.trans (table' _ _ hr hn)
  rw [show chib (n + 1) ι (nsm (ring (4 * (n + 1) + 3 + 1)) (4 * 0 + 3 + 1 + 1) (ring (4 * (n + 1) + 3 + 1)).one) = gconj (chi4 (n + 1) ι (nsm (ring (4 * (n + 1) + 3 + 1)) (4 * 0 + 3 + 1 + 1) (ring (4 * (n + 1) + 3 + 1)).one)) from chi_ivq (n + 1) hpr ι hi _] at h1
  rcases chi4_cases n ι (nsm (ring (4 * (n + 1) + 3 + 1)) (4 * 0 + 3 + 1 + 1) (ring (4 * (n + 1) + 3 + 1)).one) with e | e | e | e | e
  · rw [e] at h1; exact absurd (congrArg (fun z => z.1.val) h1) (fun e' => Nat.noConfusion e')
  · exact e
  · rw [e] at h1; exact absurd (congrArg (fun z => z.1.val) h1) (fun e' => Nat.noConfusion e')
  · rw [e] at h1; exact absurd (congrArg (fun z => z.1.val) h1) (fun e' => Nat.noConfusion (Nat.succ.inj e'))
  · rw [e] at h1; exact absurd (congrArg (fun z => z.1.val) h1) (fun e' => Nat.noConfusion e')

end QuartFive
#print axioms QuartFive.lt_wrap
#print axioms QuartFive.ndvN
#print axioms QuartFive.q_prime
#print axioms QuartFive.q_ne
#print axioms QuartFive.not_dvq
#print axioms QuartFive.md_red
#print axioms QuartFive.add_nat
#print axioms QuartFive.mul_nat
#print axioms QuartFive.neg_nat
#print axioms QuartFive.addE
#print axioms QuartFive.mulE
#print axioms QuartFive.negE
#print axioms QuartFive.c4E
#print axioms QuartFive.pwE
#print axioms QuartFive.nrmE
#print axioms QuartFive.lhsE
#print axioms QuartFive.rhsE
#print axioms QuartFive.row0
#print axioms QuartFive.row1
#print axioms QuartFive.row2
#print axioms QuartFive.row3
#print axioms QuartFive.row4
#print axioms QuartFive.chk_nat
#print axioms QuartFive.chk_all
#print axioms QuartFive.table
#print axioms QuartFive.table'
#print axioms QuartFive.cast_norm
#print axioms QuartFive.cast_rhs
#print axioms QuartFive.hP
#print axioms QuartFive.chi4_cases
#print axioms QuartFive.lhs_one
#print axioms QuartFive.quart_five
#print axioms QuartFive.quart_five_conv

lang/QuartJacobi.lean

lean_all/lang/QuartJacobi.lean

/-
  lang/QuartJacobi — G2 (T43): the quartic Jacobi sum J = Σ χ(t)χ(1 − t) ∈ ℤ[i] has norm p.

  p = 4n + 5, χ = QuartChar.chi4, χ̄(x) = χ(x⁻¹) = conj χ(x), ρ = χ² (the quadratic character). In ℤ[i][C_p] (CharSumGen):
    g ⋆ ḡ = χ(−1) X,  g ⋆ g = J g_ρ,  ḡ ⋆ ḡ = J̄ g_ρ,  g_ρ ⋆ g_ρ = X,  X ⋆ X = p X
  so J J̄ X = (g ḡ)² = p X and, at ζ⁰, J = a + bi has a² + b² = p:
      quart_norm
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.QuartChar
import lang.CharSumGen
import lang.JacobiNorm

namespace QuartJacobi

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius QuartChar GroupRing GroupRingFrob

variable (n : Nat)

def ivq (x : Zp (4 * n + 3 + 1)) : Zp (4 * n + 3 + 1) := pw (ring (4 * n + 3 + 1)) x (4 * n + 3)
def Jq (ψ : Zp (4 * n + 3 + 1) → Int × Int) : Int × Int := sumK (Cyclo4.cyc4 LibInt.intR) (fun t => (Cyclo4.cyc4 LibInt.intR).mul (ψ t) (ψ ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one ((ring (4 * n + 3 + 1)).neg t)))) (LL n)
def Ppq : Int × Int := sumK (Cyclo4.cyc4 LibInt.intR) (fun _ : Zp (4 * n + 3 + 1) => (Cyclo4.cyc4 LibInt.intR).one) (LL n)
def gconj (z : Int × Int) : Int × Int := (z.1, -z.2)

theorem ivq_mul (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (x : Zp (4 * n + 3 + 1)) (hx : x ≠ (ring (4 * n + 3 + 1)).zero) : (ring (4 * n + 3 + 1)).mul (ivq n x) x = (ring (4 * n + 3 + 1)).one := inv_exists (4 * n + 3) hpr x hx
theorem ivq_iv (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (x : Zp (4 * n + 3 + 1)) : ivq n (ivq n x) = x := CharSumGen.iv_iv (4 * n + 2) hpr x
theorem ivq_zero : ivq n (ring (4 * n + 3 + 1)).zero = (ring (4 * n + 3 + 1)).zero := HasseCM.pw_zero_succ (ring (4 * n + 3 + 1)) (4 * n + 2)
theorem ivq_one : ivq n (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).one := CubicChar.pw_one'' (ring (4 * n + 3 + 1)) _
theorem ivq_mulmul (x y : Zp (4 * n + 3 + 1)) : ivq n ((ring (4 * n + 3 + 1)).mul x y) = (ring (4 * n + 3 + 1)).mul (ivq n x) (ivq n y) := pw_mul (ring (4 * n + 3 + 1)) x y _

theorem S_ne' (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ψ : Zp (4 * n + 3 + 1) → Int × Int) (hm : ∀ x y, ψ ((ring (4 * n + 3 + 1)).mul x y) = (Cyclo4.cyc4 LibInt.intR).mul (ψ x) (ψ y)) (c : Zp (4 * n + 3 + 1)) (hc : c ≠ (ring (4 * n + 3 + 1)).zero) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (ψ a) (ψ ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (ψ c) (ψ c)) (Jq n ψ) :=
  CharSumGen.S_ne (Cyclo4.cyc4 LibInt.intR) (4 * n + 2) hpr ψ hm c hc
theorem S_zero' (ψ : Zp (4 * n + 3 + 1) → Int × Int) (hm : ∀ x y, ψ ((ring (4 * n + 3 + 1)).mul x y) = (Cyclo4.cyc4 LibInt.intR).mul (ψ x) (ψ y)) : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (ψ a) (ψ ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).zero ((ring (4 * n + 3 + 1)).neg a)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul (ψ ((ring (4 * n + 3 + 1)).neg (ring (4 * n  -- (+112 chars; full line in the Zenodo source)
  CharSumGen.S_zero (Cyclo4.cyc4 LibInt.intR) (4 * n + 2) ψ hm
theorem D_ne' (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ψ : Zp (4 * n + 3 + 1) → Int × Int) (hm : ∀ x y, ψ ((ring (4 * n + 3 + 1)).mul x y) = (Cyclo4.cyc4 LibInt.intR).mul (ψ x) (ψ y)) (h1 : ψ (ring (4 * n + 3 + 1)).one = (Cyclo4.cyc4 LibInt.intR).one) (h0 : ψ (ring (4 * n + 3 + 1)).zero = (Cyclo4.cyc4 LibInt.intR).zero) (hs : sumK (Cyclo4.cyc4 LibInt.intR) ψ (LL n) =   -- (+97 chars; full line in the Zenodo source)
    sumK (Cyclo4.cyc4 LibInt.intR) (fun b => (Cyclo4.cyc4 LibInt.intR).mul (ψ ((ring (4 * n + 3 + 1)).add c b)) (ψ (ivq n b))) (LL n) = (Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one :=
  CharSumGen.D_ne (Cyclo4.cyc4 LibInt.intR) (4 * n + 2) hpr ψ hm h1 h0 hs c hc
theorem D_zero' (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ψ : Zp (4 * n + 3 + 1) → Int × Int) (hm : ∀ x y, ψ ((ring (4 * n + 3 + 1)).mul x y) = (Cyclo4.cyc4 LibInt.intR).mul (ψ x) (ψ y)) (h1 : ψ (ring (4 * n + 3 + 1)).one = (Cyclo4.cyc4 LibInt.intR).one) (h0 : ψ (ring (4 * n + 3 + 1)).zero = (Cyclo4.cyc4 LibInt.intR).zero) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun b => (Cyclo4.cyc4 LibInt.intR).mul (ψ b) (ψ (ivq n b))) (LL n) = (Cyclo4.cyc4 LibInt.intR).add (Ppq n) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one) :=
  CharSumGen.D_zero (Cyclo4.cyc4 LibInt.intR) (4 * n + 2) hpr ψ hm h1 h0

theorem chi_one (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : chi4 n ι (ring (4 * n + 3 + 1)).one = (Cyclo4.cyc4 LibInt.intR).one := val4 n hpr ι hi (ring (4 * n + 3 + 1)).one (PowerSum.one_ne_zero' _) 0 (Nat.zero_lt_succ 3) (CubicChar.pw_one'' (ring (4 *  -- (+15 chars; full line in the Zenodo source)

/-- χ(x⁻¹) = conj χ(x). -/
theorem chi_ivq (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (x : Zp (4 * n + 3 + 1)) : chi4 n ι (ivq n x) = gconj (chi4 n ι x) := by
  by_cases hx : x = (ring (4 * n + 3 + 1)).zero
  · rw [hx, ivq_zero, chi4_zero]; rfl
  have h1 : (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι (ivq n x)) (chi4 n ι x) = (Cyclo4.cyc4 LibInt.intR).one := by rw [← chi4_mul n hpr ι hi, ivq_mul n hpr x hx, chi_one n hpr ι hi]
  have h2 : (Cyclo4.cyc4 LibInt.intR).mul (gconj (chi4 n ι x)) (chi4 n ι x) = (Cyclo4.cyc4 LibInt.intR).one := by
    match exists_j n hpr ι hi x hx with
    | ⟨j, hj, ej⟩ =>
      rw [val4 n hpr ι hi x hx j hj ej]
      match j, hj with
      | 0, _ => rfl
      | 1, _ => rfl
      | 2, _ => rfl
      | 3, _ => rfl
      | k + 4, hk => exact absurd hk (fun h => Nat.lt_irrefl 4 (Nat.lt_of_le_of_lt (Nat.le_add_left 4 k) h))
  calc chi4 n ι (ivq n x) = (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι (ivq n x)) ((Cyclo4.cyc4 LibInt.intR).mul (gconj (chi4 n ι x)) (chi4 n ι x)) := by rw [h2]; lib_ring
    _ = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι (ivq n x)) (chi4 n ι x)) (gconj (chi4 n ι x)) := by lib_ring
    _ = gconj (chi4 n ι x) := by rw [h1]; lib_ring

def rho (ι x : Zp (4 * n + 3 + 1)) : Int × Int := (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι x) (chi4 n ι x)
def chib (ι x : Zp (4 * n + 3 + 1)) : Int × Int := chi4 n ι (ivq n x)

theorem rho_mul (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (x y : Zp (4 * n + 3 + 1)) : rho n ι ((ring (4 * n + 3 + 1)).mul x y) = (Cyclo4.cyc4 LibInt.intR).mul (rho n ι x) (rho n ι y) := by
  unfold rho; rw [chi4_mul n hpr ι hi]; lib_ring
theorem chib_mul (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (x y : Zp (4 * n + 3 + 1)) : chib n ι ((ring (4 * n + 3 + 1)).mul x y) = (Cyclo4.cyc4 LibInt.intR).mul (chib n ι x) (chib n ι y) := by
  unfold chib; rw [ivq_mulmul, chi4_mul n hpr ι hi]

/-- ρ(x) is real: ρ(x⁻¹) = ρ(x). -/
theorem rho_iv (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (x : Zp (4 * n + 3 + 1)) : rho n ι (ivq n x) = rho n ι x := by
  unfold rho; rw [chi_ivq n hpr ι hi]
  by_cases hx : x = (ring (4 * n + 3 + 1)).zero
  · rw [hx, chi4_zero]; rfl
  match exists_j n hpr ι hi x hx with
  | ⟨j, hj, ej⟩ =>
    rw [val4 n hpr ι hi x hx j hj ej]
    match j, hj with
    | 0, _ => rfl
    | 1, _ => rfl
    | 2, _ => rfl
    | 3, _ => rfl
    | k + 4, hk => exact absurd hk (fun h => Nat.lt_irrefl 4 (Nat.lt_of_le_of_lt (Nat.le_add_left 4 k) h))

theorem rho_sum (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : sumK (Cyclo4.cyc4 LibInt.intR) (rho n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := by
  match PowerSum.exists_nonroot (4 * n + 3) hpr (2 * n + 1) (by rw [show 4 * n + 3 = (2 * n + 1 + 1) + (2 * n + 1) by lib_nat]; exact Nat.le_add_right _ _) with
  | ⟨x0, hx0, hn⟩ =>
    have hre := CharSumGen.sum_scale (Cyclo4.cyc4 LibInt.intR) (4 * n + 2) hpr (rho n ι) x0 hx0
    have e : sumK (Cyclo4.cyc4 LibInt.intR) (fun x => rho n ι ((ring (4 * n + 3 + 1)).mul x0 x)) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul (rho n ι x0) (sumK (Cyclo4.cyc4 LibInt.intR) (rho n ι) (LL n)) := by
      rw [mul_sumK]; exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun x => rho_mul n hpr ι hi x0 x) _
    have hre' : (Cyclo4.cyc4 LibInt.intR).mul (rho n ι x0) (sumK (Cyclo4.cyc4 LibInt.intR) (rho n ι) (LL n)) = sumK (Cyclo4.cyc4 LibInt.intR) (rho n ι) (LL n) := e.symm.trans hre
    match exists_j n hpr ι hi x0 hx0 with
    | ⟨j, hj, ej⟩ =>
      have hr : rho n ι x0 = (-1, 0) := by
        unfold rho; rw [val4 n hpr ι hi x0 hx0 j hj ej]
        match j, hj with
        | 0, _ => exact absurd (by rw [show 2 * n + 1 + 1 = (n + 1) * 2 by lib_nat, ← TwoPower.pwpow, ej]; rfl) hn
        | 1, _ => rfl
        | 2, _ =>
          exfalso; apply hn
          rw [show 2 * n + 1 + 1 = (n + 1) * 2 by lib_nat, ← TwoPower.pwpow, ej, p2, p2, hi]; lib_ring
        | 3, _ => rfl
        | k + 4, hk => exact absurd hk (fun h => Nat.lt_irrefl 4 (Nat.lt_of_le_of_lt (Nat.le_add_left 4 k) h))
      rw [hr] at hre'
      generalize sumK (Cyclo4.cyc4 LibInt.intR) (rho n ι) (LL n) = S at hre' ⊢
      match S with
      | (a, b) =>
        have h1 : (Cyclo4.c4mul LibInt.intR (-1, 0) (a, b)).1 = a := congrArg Prod.fst hre'
        have h2 : (Cyclo4.c4mul LibInt.intR (-1, 0) (a, b)).2 = b := congrArg Prod.snd hre'
        change -1 * a + -(0 * b) = a at h1
        change -1 * b + 0 * a = b at h2
        have ha : a = 0 := int2 a (by rw [show a + a = a - (-1 * a + -(0 * b)) by lib_int, h1]; lib_int)
        have hb : b = 0 := int2 b (by rw [show b + b = b - (-1 * b + 0 * a) by lib_int, h2]; lib_int)
        rw [ha, hb]; rfl

def Gq (ψ : Zp (4 * n + 3 + 1) → Int × Int) : V (Int × Int) ((4 * n + 3 + 1) + 1) := tb (4 * n + 3 + 1) ψ
def Nq : V (Int × Int) ((4 * n + 3 + 1) + 1) := tb (4 * n + 3 + 1) (fun _ => (Cyclo4.cyc4 LibInt.intR).one)
def Xq : V (Int × Int) ((4 * n + 3 + 1) + 1) := gadd (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Ppq n)) (gneg (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (Nq n))

theorem gt_Xq (c : Zp (4 * n + 3 + 1)) : gt (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (Xq n) c = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (Ppq n) (delta (Cyclo4.cyc4 LibInt.intR) c (ring (4 * n + 3 + 1)).zero)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one) := CharSumGen.gt_X (Cyclo4.cyc4 LibInt.intR) (4 * n + 2) c
theorem XXq : gmul (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (Xq n) (Xq n) = gmul (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Ppq n)) (Xq n) := CharSumGen.XX (Cyclo4.cyc4 LibInt.intR) (4 * n + 2)
theorem convq (ψ ψ' : Zp (4 * n + 3 + 1) → Int × Int) (hm' : ∀ x y, ψ' ((ring (4 * n + 3 + 1)).mul x y) = (Cyclo4.cyc4 LibInt.intR).mul (ψ' x) (ψ' y)) (c : Zp (4 * n + 3 + 1)) :
    gt (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (gmul (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (Gq n ψ) (Gq n ψ')) c = (Cyclo4.cyc4 LibInt.intR).mul (ψ' ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (sumK (Cyclo4.cyc4 LibInt.intR) (fun b => (Cyclo4.cyc4 LibInt.intR).mul (ψ ((ring (4 * n + 3 + 1)).add c b)) (ψ' b)) (LL n)) :=
  CharSumGen.conv_D (Cyclo4.cyc4 LibInt.intR) (4 * n + 2) ψ ψ' hm' c

theorem gt_sq (ψ : Zp (4 * n + 3 + 1) → Int × Int) (c : Zp (4 * n + 3 + 1)) :
    gt (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (gmul (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (Gq n ψ) (Gq n ψ)) c = sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (ψ a) (ψ ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a)))) (LL n) := by
  rw [gt_mul]; exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by unfold Gq; rw [gt_tb, gt_tb]) _

theorem chib_sum (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : sumK (Cyclo4.cyc4 LibInt.intR) (chib n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := by
  have hre := reindex (Cyclo4.cyc4 LibInt.intR) _ (CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR)) (ivq n) (ivq n) (ivq_iv n hpr) (ivq_iv n hpr) (chi4 n ι)
  exact hre.trans (chi4_sum n hpr ι hi)

/-- the X-shape of ψ ⋆ ψ' when ψ'(b) = ψ(b⁻¹). -/
theorem conv_X (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ψ ψ' : Zp (4 * n + 3 + 1) → Int × Int) (hm : ∀ x y, ψ ((ring (4 * n + 3 + 1)).mul x y) = (Cyclo4.cyc4 LibInt.intR).mul (ψ x) (ψ y)) (hm' : ∀ x y, ψ' ((ring (4 * n + 3 + 1)).mul x y) = (Cyclo4.cyc4 LibInt.intR).mul (ψ' x) (ψ' y))
    (h1 : ψ (ring (4 * n + 3 + 1)).one = (Cyclo4.cyc4 LibInt.intR).one) (h0 : ψ (ring (4 * n + 3 + 1)).zero = (Cyclo4.cyc4 LibInt.intR).zero) (hs : sumK (Cyclo4.cyc4 LibInt.intR) ψ (LL n) = (Cyclo4.cyc4 LibInt.intR).zero) (hiv : ∀ b, ψ' b = ψ (ivq n b)) :
    gmul (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (Gq n ψ) (Gq n ψ') = gmul (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (ψ' ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one))) (Xq n) :=
  ext (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) _ _ (fun c => by
    rw [convq n ψ ψ' hm' c, JacobiNorm.mono0_mulg, gt_Xq]
    rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun b => by rw [hiv b])]
    by_cases hc : c = (ring (4 * n + 3 + 1)).zero
    · rw [hc]
      rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun b => by rw [(ring (4 * n + 3 + 1)).zero_add]), D_zero' n hpr ψ hm h1 h0]
      unfold delta; rw [ite_pos' _ _ rfl]; lib_ring
    · rw [D_ne' n hpr ψ hm h1 h0 hs c hc]
      unfold delta; rw [ite_neg' _ _ hc]; lib_ring)

theorem chib_one (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : chib n ι (ring (4 * n + 3 + 1)).one = (Cyclo4.cyc4 LibInt.intR).one := by unfold chib; rw [ivq_one, chi_one n hpr ι hi]
theorem chib_zero (ι : Zp (4 * n + 3 + 1)) : chib n ι (ring (4 * n + 3 + 1)).zero = (Cyclo4.cyc4 LibInt.intR).zero := by unfold chib; rw [ivq_zero, chi4_zero]; rfl
theorem rho_one (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : rho n ι (ring (4 * n + 3 + 1)).one = (Cyclo4.cyc4 LibInt.intR).one := by unfold rho; rw [chi_one n hpr ι hi]; lib_ring
theorem rho_zero (ι : Zp (4 * n + 3 + 1)) : rho n ι (ring (4 * n + 3 + 1)).zero = (Cyclo4.cyc4 LibInt.intR).zero := by unfold rho; rw [chi4_zero]; rfl
theorem rho_neg1 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : rho n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) = (Cyclo4.cyc4 LibInt.intR).one := by
  unfold rho; rw [← chi4_mul n hpr ι hi, show (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) = (ring (4 * n + 3 + 1)).one by lib_ring, chi_one n hpr ι hi]

theorem E1 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : gmul (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (Gq n (chi4 n ι)) (Gq n (chib n ι)) = gmul (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 +  -- (+87 chars; full line in the Zenodo source)
  conv_X n hpr _ _ (chi4_mul n hpr ι hi) (chib_mul n hpr ι hi) (chi_one n hpr ι hi) (chi4_zero n ι) (chi4_sum n hpr ι hi) (fun _ => rfl)

theorem E4 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : gmul (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (Gq n (rho n ι)) (Gq n (rho n ι)) = gmul (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1  -- (+51 chars; full line in the Zenodo source)
  rw [conv_X n hpr _ _ (rho_mul n hpr ι hi) (rho_mul n hpr ι hi) (rho_one n hpr ι hi) (rho_zero n ι) (rho_sum n hpr ι hi) (fun b => (rho_iv n hpr ι hi b).symm), rho_neg1 n hpr ι hi]

theorem E2gen (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ψ : Zp (4 * n + 3 + 1) → Int × Int) (hm : ∀ x y, ψ ((ring (4 * n + 3 + 1)).mul x y) = (Cyclo4.cyc4 LibInt.intR).mul (ψ x) (ψ y)) (r : Zp (4 * n + 3 + 1) → Int × Int)
    (hr : ∀ c, (Cyclo4.cyc4 LibInt.intR).mul (ψ c) (ψ c) = r c) (hr0 : r (ring (4 * n + 3 + 1)).zero = (Cyclo4.cyc4 LibInt.intR).zero) (hsr : sumK (Cyclo4.cyc4 LibInt.intR) r (LL n) = (Cyclo4.cyc4 LibInt.intR).zero) :
    gmul (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (Gq n ψ) (Gq n ψ) = gmul (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Jq n ψ)) (Gq n r) :=
  ext (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) _ _ (fun c => by
    rw [gt_sq, JacobiNorm.mono0_mulg]
    unfold Gq; rw [gt_tb]
    by_cases hc : c = (ring (4 * n + 3 + 1)).zero
    · rw [hc, S_zero' n ψ hm, hr0, sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ hr, hsr]; lib_ring
    · rw [S_ne' n hpr ψ hm c hc, hr]; lib_ring)

theorem gconj_mul (x y : Int × Int) : gconj ((Cyclo4.cyc4 LibInt.intR).mul x y) = (Cyclo4.cyc4 LibInt.intR).mul (gconj x) (gconj y) := by
  apply Prod.ext
  · show x.1 * y.1 + -(x.2 * y.2) = x.1 * y.1 + -(-x.2 * -y.2)
    lib_int
  · show -(x.1 * y.2 + x.2 * y.1) = x.1 * -y.2 + -x.2 * y.1
    lib_int

theorem gconj_sum {A : Type} (f : A → Int × Int) : ∀ l : List A, gconj (sumK (Cyclo4.cyc4 LibInt.intR) f l) = sumK (Cyclo4.cyc4 LibInt.intR) (fun a => gconj (f a)) l
  | [] => rfl
  | a :: l => by
    show gconj ((Cyclo4.cyc4 LibInt.intR).add (f a) (sumK (Cyclo4.cyc4 LibInt.intR) f l)) = (Cyclo4.cyc4 LibInt.intR).add (gconj (f a)) (sumK (Cyclo4.cyc4 LibInt.intR) (fun a => gconj (f a)) l)
    rw [← gconj_sum f l]
    apply Prod.ext
    · rfl
    · show -((f a).2 + (sumK (Cyclo4.cyc4 LibInt.intR) f l).2) = -(f a).2 + -(sumK (Cyclo4.cyc4 LibInt.intR) f l).2
      lib_int

theorem Jb_conj (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : Jq n (chib n ι) = gconj (Jq n (chi4 n ι)) := by
  unfold Jq; rw [gconj_sum]
  exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun t => by unfold chib; rw [gconj_mul, chi_ivq n hpr ι hi, chi_ivq n hpr ι hi]) _

theorem sum_oneq : ∀ k, sumK (Cyclo4.cyc4 LibInt.intR) (fun _ : Zp (4 * n + 3 + 1) => (Cyclo4.cyc4 LibInt.intR).one) (CMInert.allZ (4 * n + 3 + 1) k) = (Int.ofNat k, 0)
  | 0 => rfl
  | k + 1 => by
    show sumK (Cyclo4.cyc4 LibInt.intR) (fun _ : Zp (4 * n + 3 + 1) => (Cyclo4.cyc4 LibInt.intR).one) (CMInert.allZ (4 * n + 3 + 1) k ++ [mk (4 * n + 3 + 1) k]) = _
    rw [sumK_append, sum_oneq k]
    rfl

/-- **N(J) = p**: the quartic Jacobi sum J = a + bi has a² + b² = p. -/
theorem quart_norm (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : (Jq n (chi4 n ι)).1 * (Jq n (chi4 n ι)).1 + (Jq n (chi4 n ι)).2 * (Jq n (chi4 n ι)).2 = Int.ofNat (4 * n + 3 + 1 + 1) := by
  have h2 := E2gen n hpr _ (chi4_mul n hpr ι hi) (rho n ι) (fun c => rfl) (rho_zero n ι) (rho_sum n hpr ι hi)
  have h3 := E2gen n hpr _ (chib_mul n hpr ι hi) (rho n ι) (fun c => rho_iv n hpr ι hi c) (rho_zero n ι) (rho_sum n hpr ι hi)
  have h1 := E1 n hpr ι hi
  have h4 := E4 n hpr ι hi
  have hcm : (Cyclo4.cyc4 LibInt.intR).mul (chib n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (chib n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) = (Cyclo4.cyc4 LibInt.intR).one := by
    unfold chib; rw [show (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι (ivq n ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one))) (chi4 n ι (ivq n ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one))) = rho n ι (ivq n ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) from rfl, rho_iv n hpr ι hi, rho_neg1 n hpr ι hi]
  let G := gring (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1)
  have key : gmul (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero ((Cyclo4.cyc4 LibInt.intR).mul (Jq n (chi4 n ι)) (Jq n (chib n ι)))) (Xq n) = gmul (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Ppq n)) (Xq n) := by
    have L : G.mul (G.mul (Gq n (chi4 n ι)) (Gq n (chi4 n ι))) (G.mul (Gq n (chib n ι)) (Gq n (chib n ι)))
        = G.mul (G.mul (Gq n (chi4 n ι)) (Gq n (chib n ι))) (G.mul (Gq n (chi4 n ι)) (Gq n (chib n ι))) := by lib_ring
    have h2' : G.mul (Gq n (chi4 n ι)) (Gq n (chi4 n ι)) = G.mul (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Jq n (chi4 n ι))) (Gq n (rho n ι)) := h2
    have h3' : G.mul (Gq n (chib n ι)) (Gq n (chib n ι)) = G.mul (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Jq n (chib n ι))) (Gq n (rho n ι)) := h3
    have h1' : G.mul (Gq n (chi4 n ι)) (Gq n (chib n ι)) = G.mul (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (chib n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one))) (Xq n) := h1
    have h4' : G.mul (Gq n (rho n ι)) (Gq n (rho n ι)) = G.mul (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Cyclo4.cyc4 LibInt.intR).one) (Xq n) := h4
    have hX' : G.mul (Xq n) (Xq n) = G.mul (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Ppq n)) (Xq n) := XXq n
    have m2 : ∀ s t, G.mul (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero s) (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero t) = mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero ((Cyclo4.cyc4 LibInt.intR).mul s t) := fun s t => by show gmul (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) _ _ = _; rw [mono_mul, show (ring  -- (+120 chars; full line in the Zenodo source)
    rw [h2', h3', h1'] at L
    rw [show G.mul (G.mul (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Jq n (chi4 n ι))) (Gq n (rho n ι))) (G.mul (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Jq n (chib n ι))) (Gq n (rho n ι)))
        = G.mul (G.mul (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Jq n (chi4 n ι))) (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Jq n (chib n ι)))) (G.mul (Gq n (rho n ι)) (Gq n (rho n ι))) by lib_ring,
      h4', m2, show G.mul (G.mul (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (chib n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one))) (Xq n)) (G.mul (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (chib n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one))) (Xq n))
        = G.mul (G.mul (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (chib n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one))) (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (chib n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)))) (G.mul (Xq n) (Xq n)) by lib_ring,
      hX', m2, hcm, show G.mul (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero ((Cyclo4.cyc4 LibInt.intR).mul (Jq n (chi4 n ι)) (Jq n (chib n ι)))) (G.mul (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Cyclo4.cyc4 LibInt.intR).one) (Xq n))
        = G.mul (G.mul (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero ((Cyclo4.cyc4 LibInt.intR).mul (Jq n (chi4 n ι)) (Jq n (chib n ι)))) (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Cyclo4.cyc4 LibInt.intR).one)) (Xq n) by lib_ring,
      m2, show G.mul (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Cyclo4.cyc4 LibInt.intR).one) (G.mul (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Ppq n)) (Xq n)) = G.mul (G.mul (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Cyclo4.cyc4 LibInt.intR).one) (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (r  -- (+55 chars; full line in the Zenodo source)
      m2, (Cyclo4.cyc4 LibInt.intR).mul_comm _ (Cyclo4.cyc4 LibInt.intR).one, (Cyclo4.cyc4 LibInt.intR).one_mul, (Cyclo4.cyc4 LibInt.intR).one_mul] at L
    exact L
  have k0 := congrArg (fun u => gt (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) u (ring (4 * n + 3 + 1)).zero) key
  have k1 : (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (Jq n (chi4 n ι)) (Jq n (chib n ι))) (gt (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (Xq n) (ring (4 * n + 3 + 1)).zero) = (Cyclo4.cyc4 LibInt.intR).mul (Ppq n) (gt (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (Xq n) (ring (4 * n + 3 + 1)).zero) := by
    have := k0
    rw [JacobiNorm.mono0_mulg, JacobiNorm.mono0_mulg] at this
    exact this
  have hP : Ppq n = (Int.ofNat (4 * n + 3 + 1 + 1), 0) := sum_oneq n _
  have hX0 : gt (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (Xq n) (ring (4 * n + 3 + 1)).zero = (Int.ofNat (4 * n + 3 + 1), 0) := by
    rw [gt_Xq]; unfold delta; rw [ite_pos' _ _ rfl, (Cyclo4.cyc4 LibInt.intR).mul_comm, (Cyclo4.cyc4 LibInt.intR).one_mul, hP]
    exact Prod.ext (by show Int.ofNat (4 * n + 3 + 1) + 1 + -1 = Int.ofNat (4 * n + 3 + 1); lib_int) (by show (0 : Int) + -0 = 0; rfl)
  have hN : (Cyclo4.cyc4 LibInt.intR).mul (Jq n (chi4 n ι)) (Jq n (chib n ι)) = ((Jq n (chi4 n ι)).1 * (Jq n (chi4 n ι)).1 + (Jq n (chi4 n ι)).2 * (Jq n (chi4 n ι)).2, 0) := by
    rw [Jb_conj n hpr ι hi]
    apply Prod.ext
    · show (Jq n (chi4 n ι)).1 * (Jq n (chi4 n ι)).1 + -((Jq n (chi4 n ι)).2 * -(Jq n (chi4 n ι)).2) = (Jq n (chi4 n ι)).1 * (Jq n (chi4 n ι)).1 + (Jq n (chi4 n ι)).2 * (Jq n (chi4 n ι)).2
      lib_int
    · show (Jq n (chi4 n ι)).1 * -(Jq n (chi4 n ι)).2 + (Jq n (chi4 n ι)).2 * (Jq n (chi4 n ι)).1 = 0
      lib_int
  rw [hX0, hN, hP] at k1
  have k2 := congrArg Prod.fst k1
  change _ * Int.ofNat (4 * n + 3 + 1) + -(0 * 0) = Int.ofNat (4 * n + 3 + 1 + 1) * Int.ofNat (4 * n + 3 + 1) + -(0 * 0) at k2
  apply JacobiNorm.int_cancel _ _ (4 * n + 3)
  rw [show ((Jq n (chi4 n ι)).1 * (Jq n (chi4 n ι)).1 + (Jq n (chi4 n ι)).2 * (Jq n (chi4 n ι)).2) * Int.ofNat (4 * n + 3 + 1)
      = ((Jq n (chi4 n ι)).1 * (Jq n (chi4 n ι)).1 + (Jq n (chi4 n ι)).2 * (Jq n (chi4 n ι)).2) * Int.ofNat (4 * n + 3 + 1) + -(0 * 0) by lib_int, k2]
  lib_int

end QuartJacobi

#print axioms QuartJacobi.ivq_mul
#print axioms QuartJacobi.ivq_iv
#print axioms QuartJacobi.S_ne'
#print axioms QuartJacobi.S_zero'
#print axioms QuartJacobi.D_ne'
#print axioms QuartJacobi.D_zero'
#print axioms QuartJacobi.chi_one
#print axioms QuartJacobi.chi_ivq
#print axioms QuartJacobi.rho_mul
#print axioms QuartJacobi.chib_mul
#print axioms QuartJacobi.rho_iv
#print axioms QuartJacobi.rho_sum
#print axioms QuartJacobi.gt_Xq
#print axioms QuartJacobi.XXq
#print axioms QuartJacobi.convq
#print axioms QuartJacobi.gt_sq
#print axioms QuartJacobi.chib_sum
#print axioms QuartJacobi.conv_X
#print axioms QuartJacobi.E1
#print axioms QuartJacobi.E4
#print axioms QuartJacobi.E2gen
#print axioms QuartJacobi.gconj_mul
#print axioms QuartJacobi.gconj_sum
#print axioms QuartJacobi.Jb_conj
#print axioms QuartJacobi.sum_oneq
#print axioms QuartJacobi.quart_norm

lang/QuartMoments.lean

lean_all/lang/QuartMoments.lean

/-
  lang/QuartMoments — T43, cycle 289: moments of the quartic twists of y² = x⁴ + D (p ≡ 1 mod 4).

    quartic_moment2   χ(v) = i, ρ(v) = −1 ⇒ Σ_{k=0}^{3} (1 + Σ_x ρ(x⁴ + v^k D))² = 8p
    quartic_moment4   … and Σ_{k=0}^{3} (1 + Σ_x ρ(x⁴ + v^k D))⁴ = 24p² + 4(J⁴ + J̄⁴)
    fermat4_moment2   Σ_{k=0}^{3} (#(x⁴ + y⁴ = v^k c) − (p − 1 − 2χ(−1)))² = 4(J + J̄)² + 32p
  (quartic_sum: 1 + Σ_x ρ(x⁴ + D) = ρ(D) Tr(χ(−D) J); the twist by v multiplies the trace argument by −i; |J|² = p).
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.QuartQuartic

namespace QuartMoments

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius QuartChar QuartJacobi QuartPrimary QuartDup FermatQuartic QuartTwist QuartQuartic
open LibRing (castZ castN)

variable (n : Nat)

/-- **Second moment over the quartic twists of y² = x⁴ + D**: Σ_{k=0}^{3} (1 + Σ_x ρ(x⁴ + v^k D))² = 8p. -/
theorem quartic_moment2 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (D v : Zp (4 * n + 3 + 1)) (hD : D ≠ (ring (4 * n + 3 + 1)).zero)
    (hv0 : v ≠ (ring (4 * n + 3 + 1)).zero) (hv : chi4 n ι v = (((0 : Int), (1 : Int)))) (hr : rho n ι v = (((-1 : Int)), ((0 : Int)))) :
    (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add (S4 n ι (D)) (Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).add (S4 n ι (D)) (Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add (S4 n ι ((ring (4 * n + 3 + 1)).mul v (D))) (Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 Li  -- (+1387 chars; full line in the Zenodo source)
  have nz : ∀ x y : Zp (4 * n + 3 + 1), x ≠ (ring (4 * n + 3 + 1)).zero → y ≠ (ring (4 * n + 3 + 1)).zero → (ring (4 * n + 3 + 1)).mul x y ≠ (ring (4 * n + 3 + 1)).zero := fun x y hx hy e => by
    cases nozd (4 * n + 3) hpr _ _ e with
    | inl h => exact hx h
    | inr h => exact hy h
  have n0 := hD
  have n1 : (ring (4 * n + 3 + 1)).mul v (D) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n0
  have n2 : (ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v (D)) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n1
  have n3 : (ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v (D))) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n2
  have hneg : ∀ x : Zp (4 * n + 3 + 1), x ≠ (ring (4 * n + 3 + 1)).zero → (ring (4 * n + 3 + 1)).neg x ≠ (ring (4 * n + 3 + 1)).zero := fun x hx e => hx (by
    calc x = (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).neg x) := by lib_ring
      _ = (ring (4 * n + 3 + 1)).zero := by rw [e]; lib_ring)
  have hcc : (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg D)) (gconj (chi4 n ι ((ring (4 * n + 3 + 1)).neg D))) = (Cyclo4.cyc4 LibInt.intR).one := by
    rw [show (chi4 n ι ((ring (4 * n + 3 + 1)).neg D)) = ((chi4 n ι ((ring (4 * n + 3 + 1)).neg D)).1, (chi4 n ι ((ring (4 * n + 3 + 1)).neg D)).2) from rfl, gnorm, (QuartTwistCount.unit_norm n hpr ι hi _ (hneg D hD)).2]; rfl
  have m1 : (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one ≠ (ring (4 * n + 3 + 1)).zero := fun e => by
    have : (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).zero := by
      calc (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by lib_ring
        _ = (ring (4 * n + 3 + 1)).zero := by rw [e]; lib_ring
    exact PowerSum.one_ne_zero' (4 * n + 3) this
  have hJJ : (Cyclo4.cyc4 LibInt.intR).mul (Jcr n ι) (gconj (Jcr n ι)) = (Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int)) := by
    rw [show (Jcr n ι) = ((Jcr n ι).1, (Jcr n ι).2) from rfl, gnorm, Jcr_eq n hpr ι hi, QuartTwistCount.norm_mul4,
      (QuartTwistCount.unit_norm n hpr ι hi _ m1).2, quart_norm n hpr ι hi]
    show (1 * Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int)) = _
    rw [Int.one_mul]
  have hrr : (Cyclo4.cyc4 LibInt.intR).mul (rho n ι D) (rho n ι D) = (Cyclo4.cyc4 LibInt.intR).one := by rw [← rho_mul n hpr ι hi]; exact QuartPrimary.rho_sq n hpr ι hi D hD
  rw [quartic_sum n hpr ι hi _ n0, quartic_sum n hpr ι hi _ n1, quartic_sum n hpr ι hi _ n2, quartic_sum n hpr ι hi _ n3]
  have hn : ∀ E : Zp (4 * n + 3 + 1), (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul v E) = (ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).neg E) := fun E => by lib_ring
  repeat rw [hn]
  have hcv : ∀ E : Zp (4 * n + 3 + 1), chi4 n ι ((ring (4 * n + 3 + 1)).mul v E) = (Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) (chi4 n ι E) := fun E => by rw [chi4_mul n hpr ι hi, hv]
  have hρ : ∀ E : Zp (4 * n + 3 + 1), rho n ι ((ring (4 * n + 3 + 1)).mul v E) = (Cyclo4.cyc4 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) (rho n ι E) := fun E => by rw [rho_mul n hpr ι hi, hr]
  repeat rw [hcv]
  repeat rw [hρ]
  repeat rw [gconj_mul]
  have E0 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((-1 : In  -- (+2045 chars; full line in the Zenodo source)
  have E1 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((-1 : In  -- (+2284 chars; full line in the Zenodo source)
  have E2 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((-1 : In  -- (+2141 chars; full line in the Zenodo source)
  calc _ = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι D) (rho n ι D)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg D)) (Jcr n ι)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg D)) (Jcr n ι))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.c  -- (+8090 chars; full line in the Zenodo source)
        generalize (gconj (((0 : Int), (1 : Int)))) = a2; generalize (((0 : Int), (1 : Int))) = a; generalize (((-1 : Int)), ((0 : Int))) = b; lib_ring
    _ = (Cyclo4.cyc4 LibInt.intR).add ((Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int))) ((Cyclo4.cyc4 LibInt.intR).add ((Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int))) ((Cyclo4.cyc4 LibInt.intR).add ((Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int))) ((Cyclo4.cyc4 LibInt.intR).add ((Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int))) ((Cyclo4.cyc4 LibInt.intR).add ((Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int))) ((Cyclo4.cyc4  -- (+195 chars; full line in the Zenodo source)
        rw [E0, E1, E2, hrr, hcc, hJJ]; generalize (Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int)) = q; lib_ring

/-- **Fourth moment over the quartic twists of y² = x⁴ + D**: Σ_{k=0}^{3} (1 + Σ_x ρ(x⁴ + v^k D))⁴ = 24p² + 4(J⁴ + J̄⁴). -/
theorem quartic_moment4 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (D v : Zp (4 * n + 3 + 1)) (hD : D ≠ (ring (4 * n + 3 + 1)).zero)
    (hv0 : v ≠ (ring (4 * n + 3 + 1)).zero) (hv : chi4 n ι v = (((0 : Int), (1 : Int)))) (hr : rho n ι v = (((-1 : Int)), ((0 : Int)))) :
    (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add (S4 n ι (D)) (Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).add (S4 n ι (D)) (Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add (S4 n ι (D)) (Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).add (S4 n ι (D)) (Cy  -- (+7103 chars; full line in the Zenodo source)
  have nz : ∀ x y : Zp (4 * n + 3 + 1), x ≠ (ring (4 * n + 3 + 1)).zero → y ≠ (ring (4 * n + 3 + 1)).zero → (ring (4 * n + 3 + 1)).mul x y ≠ (ring (4 * n + 3 + 1)).zero := fun x y hx hy e => by
    cases nozd (4 * n + 3) hpr _ _ e with
    | inl h => exact hx h
    | inr h => exact hy h
  have n0 := hD
  have n1 : (ring (4 * n + 3 + 1)).mul v (D) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n0
  have n2 : (ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v (D)) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n1
  have n3 : (ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v (D))) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n2
  have hneg : ∀ x : Zp (4 * n + 3 + 1), x ≠ (ring (4 * n + 3 + 1)).zero → (ring (4 * n + 3 + 1)).neg x ≠ (ring (4 * n + 3 + 1)).zero := fun x hx e => hx (by
    calc x = (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).neg x) := by lib_ring
      _ = (ring (4 * n + 3 + 1)).zero := by rw [e]; lib_ring)
  have hcc : (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg D)) (gconj (chi4 n ι ((ring (4 * n + 3 + 1)).neg D))) = (Cyclo4.cyc4 LibInt.intR).one := by
    rw [show (chi4 n ι ((ring (4 * n + 3 + 1)).neg D)) = ((chi4 n ι ((ring (4 * n + 3 + 1)).neg D)).1, (chi4 n ι ((ring (4 * n + 3 + 1)).neg D)).2) from rfl, gnorm, (QuartTwistCount.unit_norm n hpr ι hi _ (hneg D hD)).2]; rfl
  have m1 : (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one ≠ (ring (4 * n + 3 + 1)).zero := fun e => by
    have : (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).zero := by
      calc (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by lib_ring
        _ = (ring (4 * n + 3 + 1)).zero := by rw [e]; lib_ring
    exact PowerSum.one_ne_zero' (4 * n + 3) this
  have hJJ : (Cyclo4.cyc4 LibInt.intR).mul (Jcr n ι) (gconj (Jcr n ι)) = (Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int)) := by
    rw [show (Jcr n ι) = ((Jcr n ι).1, (Jcr n ι).2) from rfl, gnorm, Jcr_eq n hpr ι hi, QuartTwistCount.norm_mul4,
      (QuartTwistCount.unit_norm n hpr ι hi _ m1).2, quart_norm n hpr ι hi]
    show (1 * Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int)) = _
    rw [Int.one_mul]
  have hrr : (Cyclo4.cyc4 LibInt.intR).mul (rho n ι D) (rho n ι D) = (Cyclo4.cyc4 LibInt.intR).one := by rw [← rho_mul n hpr ι hi]; exact QuartPrimary.rho_sq n hpr ι hi D hD
  have hc4 : (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((chi4 n ι ((ring (4 * n + 3 + 1)).neg D))) ((chi4 n ι ((ring (4 * n + 3 + 1)).neg D)))) ((Cyclo4.cyc4 LibInt.intR).mul ((chi4 n ι ((ring (4 * n + 3 + 1)).neg D))) ((chi4 n ι ((ring (4 * n + 3 + 1)).neg D)))) = (Cyclo4.cyc4 LibInt.intR).one ∧ (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj (chi4 n ι ((rin  -- (+248 chars; full line in the Zenodo source)
    rcases ResidueCriterion.chi4_cases n ι ((ring (4 * n + 3 + 1)).neg D) with e | e | e | e | e
    · exact absurd e (chi4_ne n hpr ι hi _ (hneg D hD))
    all_goals (rw [e]; exact ⟨rfl, rfl⟩)
  rw [quartic_sum n hpr ι hi _ n0, quartic_sum n hpr ι hi _ n1, quartic_sum n hpr ι hi _ n2, quartic_sum n hpr ι hi _ n3]
  have hn : ∀ E : Zp (4 * n + 3 + 1), (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul v E) = (ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).neg E) := fun E => by lib_ring
  repeat rw [hn]
  have hcv : ∀ E : Zp (4 * n + 3 + 1), chi4 n ι ((ring (4 * n + 3 + 1)).mul v E) = (Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) (chi4 n ι E) := fun E => by rw [chi4_mul n hpr ι hi, hv]
  have hρ : ∀ E : Zp (4 * n + 3 + 1), rho n ι ((ring (4 * n + 3 + 1)).mul v E) = (Cyclo4.cyc4 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) (rho n ι E) := fun E => by rw [rho_mul n hpr ι hi, hr]
  repeat rw [hcv]
  repeat rw [hρ]
  repeat rw [gconj_mul]
  have E0 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.  -- (+5112 chars; full line in the Zenodo source)
  have E1 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.  -- (+4617 chars; full line in the Zenodo source)
  have E2 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.  -- (+4760 chars; full line in the Zenodo source)
  have E3 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.  -- (+4521 chars; full line in the Zenodo source)
  have E4 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.  -- (+4920 chars; full line in the Zenodo source)
  calc _ = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι D) (rho n ι D)) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι D) (rho n ι D))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj (chi4 n ι ((ring (4 * n + 3 + 1)).neg D))) (gconj (ch  -- (+33046 chars; full line in the Zenodo source)
        generalize (gconj (((0 : Int), (1 : Int)))) = a2; generalize (((0 : Int), (1 : Int))) = a; generalize (((-1 : Int)), ((0 : Int))) = b; lib_ring
    _ = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int)) (Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int)) (Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (I  -- (+4683 chars; full line in the Zenodo source)
        rw [E0, E1, E2, E3, E4, hrr, hcc, hJJ, hc4.1, hc4.2]; generalize (Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int)) = q; lib_ring

/-- **Second moment over the quartic twists of the Fermat quartic**: χ(v) = i, ρ(v) = −1 ⇒
    Σ_{k=0}^{3} (#(x⁴ + y⁴ = v^k c) − (p − 1 − 2χ(−1)))² = 4T² + 32p, T = J + J̄. -/
theorem fermat4_moment2 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (c v : Zp (4 * n + 3 + 1)) (hc : c ≠ (ring (4 * n + 3 + 1)).zero)
    (hv0 : v ≠ (ring (4 * n + 3 + 1)).zero) (hv : chi4 n ι v = (((0 : Int), (1 : Int)))) (hr : rho n ι v = (((-1 : Int)), ((0 : Int)))) :
    (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add (QuartTwistCount.Fqc n (c)) ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (Int.ofNat (4 * n + 3 + 1 + 1), 0) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n  -- (+8355 chars; full line in the Zenodo source)
  have nz : ∀ x y : Zp (4 * n + 3 + 1), x ≠ (ring (4 * n + 3 + 1)).zero → y ≠ (ring (4 * n + 3 + 1)).zero → (ring (4 * n + 3 + 1)).mul x y ≠ (ring (4 * n + 3 + 1)).zero := fun x y hx hy e => by
    cases nozd (4 * n + 3) hpr _ _ e with
    | inl h => exact hx h
    | inr h => exact hy h
  have n0 := hc
  have n1 : (ring (4 * n + 3 + 1)).mul v (c) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n0
  have n2 : (ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v (c)) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n1
  have n3 : (ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v (c))) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n2
  have m1 : (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one ≠ (ring (4 * n + 3 + 1)).zero := fun e => by
    have : (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).zero := by
      calc (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by lib_ring
        _ = (ring (4 * n + 3 + 1)).zero := by rw [e]; lib_ring
    exact PowerSum.one_ne_zero' (4 * n + 3) this
  have hJJ : (Cyclo4.cyc4 LibInt.intR).mul (J2 n ι) (gconj (J2 n ι)) = (Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int)) := by
    rw [show (J2 n ι) = ((J2 n ι).1, (J2 n ι).2) from rfl, gnorm, QuartTwistCount.J2_eq' n hpr ι hi, QuartTwistCount.norm_mul4,
      (QuartTwistCount.unit_norm n hpr ι hi _ m1).2, quart_norm n hpr ι hi]
    show (1 * Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int)) = _
    rw [Int.one_mul]
  have htb : (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι c) (chib n ι c) = (Cyclo4.cyc4 LibInt.intR).one := by
    rw [chib_eq n hpr ι hi, show (chi4 n ι c) = ((chi4 n ι c).1, (chi4 n ι c).2) from rfl, gnorm, (QuartTwistCount.unit_norm n hpr ι hi _ hc).2]; rfl
  have hrr : (Cyclo4.cyc4 LibInt.intR).mul (rho n ι c) (rho n ι c) = (Cyclo4.cyc4 LibInt.intR).one := by rw [← rho_mul n hpr ι hi]; exact QuartPrimary.rho_sq n hpr ι hi c hc
  rw [QuartTwistCount.quart_twist_count n hpr ι hi _ n0, QuartTwistCount.quart_twist_count n hpr ι hi _ n1, QuartTwistCount.quart_twist_count n hpr ι hi _ n2, QuartTwistCount.quart_twist_count n hpr ι hi _ n3]
  have hcv : ∀ E : Zp (4 * n + 3 + 1), chi4 n ι ((ring (4 * n + 3 + 1)).mul v E) = (Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) (chi4 n ι E) := fun E => by rw [chi4_mul n hpr ι hi, hv]
  have hbv : ∀ E : Zp (4 * n + 3 + 1), chib n ι ((ring (4 * n + 3 + 1)).mul v E) = (Cyclo4.cyc4 LibInt.intR).mul (((0 : Int)), ((-1 : Int))) (chib n ι E) := fun E => by
    rw [chib_mul n hpr ι hi, chib_eq n hpr ι hi v, hv]; rfl
  have hρ : ∀ E : Zp (4 * n + 3 + 1), rho n ι ((ring (4 * n + 3 + 1)).mul v E) = (Cyclo4.cyc4 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) (rho n ι E) := fun E => by rw [rho_mul n hpr ι hi, hr]
  repeat rw [hcv]
  repeat rw [hbv]
  repeat rw [hρ]
  have E0 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) ((Cyclo4.cyc4 LibInt.intR).one))) ((Cyclo4.cyc4 L  -- (+1040 chars; full line in the Zenodo source)
  have E1 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int)), ((-1 : Int))) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int)), ((-1 : Int))) ((Cyclo4.cyc4 LibInt.intR).one))) ((Cyclo4.cyc4 L  -- (+849 chars; full line in the Zenodo source)
  have E2 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) ((Cyclo4.cyc4 LibInt.intR).one))) ((Cyclo4.cyc4 LibInt.  -- (+813 chars; full line in the Zenodo source)
  have E3 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int)), ((-1 : Int))) ((Cyclo4.cyc4 LibInt.intR).one))) ((Cyclo4.cyc4 L  -- (+849 chars; full line in the Zenodo source)
  have E4 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) ((Cyclo4.cyc4 LibInt.intR).one))) ((Cyclo4.cyc4 LibI  -- (+831 chars; full line in the Zenodo source)
  have E5 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int)), ((-1 : Int))) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) ((Cyclo4.cyc4 LibInt.intR).one))) ((Cyclo4.cyc4 LibI  -- (+1022 chars; full line in the Zenodo source)
  calc _ = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι c) (rho n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add (Jq n (chi4 n ι)) (gconj (Jq n (chi4 n ι)))) ((Cyclo4.cyc4 LibInt.intR).add (Jq n (chi4 n ι)) (gconj (Jq n (chi4 n ι)))))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR  -- (+12022 chars; full line in the Zenodo source)
        generalize (((0 : Int), (1 : Int))) = a; generalize (((-1 : Int)), ((0 : Int))) = bb; generalize (((0 : Int)), ((-1 : Int))) = d; generalize (Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int)) = q; lib_ring
    _ = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add (Jq n (chi4 n ι)) (gconj (Jq n (chi4 n ι)))) ((Cyclo4.cyc4 LibInt.intR).add (Jq n (chi4 n ι)) (gconj (Jq n (chi4 n ι))))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add (Jq n (chi4 n ι)) (gconj (Jq n (chi4 n ι)))) ((Cyclo4.  -- (+2907 chars; full line in the Zenodo source)
        rw [E0, E1, E2, E3, E4, E5, hrr, htb, hJJ]; generalize (Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int)) = q; lib_ring

end QuartMoments

#print axioms QuartMoments.quartic_moment2

#print axioms QuartMoments.quartic_moment4
#print axioms QuartMoments.fermat4_moment2

lang/QuartPrimary.lean

lean_all/lang/QuartPrimary.lean

/-
  lang/QuartPrimary — T43, cycle 230: the quartic Jacobi sum mod 4 (primary normalisation).

  p = 4n + 5, χ quartic, J = J(χ, χ) = Σ_t f(t), f(t) = χ(t)χ(1 − t) = χ(t(1 − t)), J = a + bi. The involution t ↦ 1 − t fixes
  only h = 1/2 (Pairing), f(h) = χ(1/4) = ρ(h) is real, and Re f ≡ [f real], Im f ≡ [f imaginary] (mod 2). With
  Σ_t [f real] − [f imaginary] = Σ_t ρ(t(1 − t)) = −1 and Σ_t [f ≠ 0] = p − 2:
      quart_primary   a = Re ρ(h) + 2n + 4K,  b = 2n + 2 + 4K'      (so a ≡ −1, b ≡ (p − 1)/2 mod 4 once ρ(h) = χ(−1))
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.ResidueCriterion

namespace QuartPrimary

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius QuartChar QuartJacobi Pairing
open LibRing (castZ castN)

variable (n : Nat)

def om (t : Zp (4 * n + 3 + 1)) : Zp (4 * n + 3 + 1) := (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one ((ring (4 * n + 3 + 1)).neg t)
def f (ι t : Zp (4 * n + 3 + 1)) : Int × Int := (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι t) (chi4 n ι (om n t))
def h : Zp (4 * n + 3 + 1) := mk (4 * n + 3 + 1) (2 * n + 3)

theorem om_om (t : Zp (4 * n + 3 + 1)) : om n (om n t) = t := by unfold om; lib_ring

theorem f_om (ι t : Zp (4 * n + 3 + 1)) : f n ι (om n t) = f n ι t := by
  unfold f; rw [om_om]; exact (Cyclo4.cyc4 LibInt.intR).mul_comm _ _

theorem hh : (ring (4 * n + 3 + 1)).add (h n) (h n) = (ring (4 * n + 3 + 1)).one := Subtype.ext (by
  show md (4 * n + 3 + 1) (md (4 * n + 3 + 1) (2 * n + 3) + md (4 * n + 3 + 1) (2 * n + 3)) = md (4 * n + 3 + 1) 1
  rw [← md_add2, show 2 * n + 3 + (2 * n + 3) = 1 + (4 * n + 3 + 1 + 1) by lib_nat, md_add_p])

theorem om_h : om n (h n) = h n := by
  unfold om; rw [← hh n]; lib_ring

theorem fix_h (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (t : Zp (4 * n + 3 + 1)) (e : om n t = t) : t = h n := by
  have e2 : (ring (4 * n + 3 + 1)).add t t = (ring (4 * n + 3 + 1)).add (h n) (h n) := by
    rw [hh]; calc (ring (4 * n + 3 + 1)).add t t = (ring (4 * n + 3 + 1)).add t (om n t) := by rw [e]
      _ = (ring (4 * n + 3 + 1)).one := by unfold om; lib_ring
  have e3 : (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one) ((ring (4 * n + 3 + 1)).add t ((ring (4 * n + 3 + 1)).neg (h n))) = (ring (4 * n + 3 + 1)).zero := by
    calc _ = (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).add t t) ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).add (h n) (h n))) := by lib_ring
      _ = (ring (4 * n + 3 + 1)).zero := by rw [e2]; lib_ring
  cases nozd (4 * n + 3) hpr _ _ e3 with
  | inl e4 => exact absurd e4 (Eisen.two_ne_zero' (4 * n + 3) (by rw [show 4 * n + 3 = (4 * n + 1) + 2 by lib_nat]; exact Nat.le_add_left 2 _))
  | inr e4 => calc t = (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).add t ((ring (4 * n + 3 + 1)).neg (h n))) (h n) := by lib_ring
      _ = h n := by rw [e4]; lib_ring

theorem sum_fst {A : Type} (g : A → Int × Int) : ∀ l : List A, (sumK (Cyclo4.cyc4 LibInt.intR) g l).1 = sumK LibInt.intR (fun x => (g x).1) l
  | [] => rfl
  | a :: as => by show (g a).1 + (sumK (Cyclo4.cyc4 LibInt.intR) g as).1 = _; rw [sum_fst g as]; rfl

theorem sum_snd {A : Type} (g : A → Int × Int) : ∀ l : List A, (sumK (Cyclo4.cyc4 LibInt.intR) g l).2 = sumK LibInt.intR (fun x => (g x).2) l
  | [] => rfl
  | a :: as => by show (g a).2 + (sumK (Cyclo4.cyc4 LibInt.intR) g as).2 = _; rw [sum_snd g as]; rfl

/-- the fixed-point part of the pairing. -/
theorem fixed_sum (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (F : Zp (4 * n + 3 + 1) → Int) :
    sumK LibInt.intR (fun x => eqI (4 * n + 3) (om n x) x * F x) (LL n) = F (h n) := by
  rw [← FiniteRecChain.pick LibInt.intR (LL n) (CMInert.complete_allZ (4 * n + 3 + 1) LibInt.intR) F (h n)]
  exact sumK_ext LibInt.intR _ _ (fun x => by
    unfold eqI delta
    by_cases hx : x = h n
    · rw [ite_pos' _ _ (show om n x = x by rw [hx]; exact om_h n), ite_pos' _ _ hx]; rfl
    · rw [ite_neg' _ _ (fun e => hx (fix_h n hpr x e)), ite_neg' _ _ hx]; rfl) _

theorem pair (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (F : Zp (4 * n + 3 + 1) → Int) (hF : ∀ x, F (om n x) = F x) :
    sumK LibInt.intR F (LL n) = F (h n) + 2 * sumK LibInt.intR (fun x => ltI (4 * n + 3) x (om n x) * F x) (LL n) := by
  rw [Pairing.pairing (4 * n + 3) (om n) (om_om n) F hF (LL n) (CMInert.complete_allZ (4 * n + 3 + 1) LibInt.intR), fixed_sum n hpr]

/-! ## sums of integers -/

theorem sI_add {A : Type} (F G : A → Int) (l : List A) : sumK LibInt.intR (fun x => F x + G x) l = sumK LibInt.intR F l + sumK LibInt.intR G l :=
  sumK_add LibInt.intR F G l
theorem sI_mul {A : Type} (c : Int) (F : A → Int) (l : List A) : sumK LibInt.intR (fun x => c * F x) l = c * sumK LibInt.intR F l :=
  (mul_sumK LibInt.intR c F l).symm
theorem sI_neg {A : Type} (F : A → Int) : ∀ l : List A, sumK LibInt.intR (fun x => -F x) l = -sumK LibInt.intR F l
  | [] => rfl
  | a :: as => by show -F a + sumK LibInt.intR (fun x => -F x) as = -(F a + sumK LibInt.intR F as); rw [sI_neg F as]; lib_int

/-! ## the summand f(t) = χ(t(1 − t)) -/

theorem f_eq (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (t : Zp (4 * n + 3 + 1)) : f n ι t = chi4 n ι ((ring (4 * n + 3 + 1)).mul t (om n t)) :=
  (chi4_mul n hpr ι hi t (om n t)).symm

def Ri (ι t : Zp (4 * n + 3 + 1)) : Int := (f n ι t).1 * (f n ι t).1
def Ii (ι t : Zp (4 * n + 3 + 1)) : Int := (f n ι t).2 * (f n ι t).2
def c1 (ι t : Zp (4 * n + 3 + 1)) : Int := -(((f n ι t).1 * (f n ι t).1 - (f n ι t).1) / 2)
def c2 (ι t : Zp (4 * n + 3 + 1)) : Int := -(((f n ι t).2 * (f n ι t).2 - (f n ι t).2) / 2)

theorem dec1 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (t : Zp (4 * n + 3 + 1)) : (f n ι t).1 = Ri n ι t + 2 * c1 n ι t := by
  unfold Ri c1; rw [f_eq n hpr ι hi]
  rcases ResidueCriterion.chi4_cases n ι ((ring (4 * n + 3 + 1)).mul t (om n t)) with e | e | e | e | e <;> rw [e] <;> rfl

theorem dec2 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (t : Zp (4 * n + 3 + 1)) : (f n ι t).2 = Ii n ι t + 2 * c2 n ι t := by
  unfold Ii c2; rw [f_eq n hpr ι hi]
  rcases ResidueCriterion.chi4_cases n ι ((ring (4 * n + 3 + 1)).mul t (om n t)) with e | e | e | e | e <;> rw [e] <;> rfl

/-- Ri − Ii is the real part of f² = ρ(t)ρ(1 − t). -/
theorem RmI (ι t : Zp (4 * n + 3 + 1)) : Ri n ι t + -Ii n ι t = ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι t) (rho n ι (om n t))).1 := by
  have e : (Cyclo4.cyc4 LibInt.intR).mul (rho n ι t) (rho n ι (om n t)) = (Cyclo4.cyc4 LibInt.intR).mul (f n ι t) (f n ι t) := by unfold rho f; lib_ring
  rw [e]; rfl

/-- chi4 x ≠ 0 for x ≠ 0. -/
theorem chi4_ne (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (x : Zp (4 * n + 3 + 1)) (hx : x ≠ (ring (4 * n + 3 + 1)).zero) : chi4 n ι x ≠ (0, 0) := fun e => by
  have h1 : (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι x) (chi4 n ι (ivq n x)) = (Cyclo4.cyc4 LibInt.intR).one := by
    rw [← chi4_mul n hpr ι hi, (ring (4 * n + 3 + 1)).mul_comm, ivq_mul n hpr x hx]; exact chi_one n hpr ι hi
  rw [e] at h1
  have h2 : (Cyclo4.cyc4 LibInt.intR).zero = (Cyclo4.cyc4 LibInt.intR).one := (show (Cyclo4.cyc4 LibInt.intR).mul (Cyclo4.cyc4 LibInt.intR).zero (chi4 n ι (ivq n x)) = (Cyclo4.cyc4 LibInt.intR).zero by lib_ring).symm.trans h1
  exact absurd (congrArg Prod.fst h2) (fun e' => Nat.noConfusion (Int.ofNat.inj e'))

/-- Ri + Ii = [t ≠ 0][t ≠ 1]. -/
theorem RpI (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (t : Zp (4 * n + 3 + 1)) :
    Ri n ι t + Ii n ι t = 1 + -(delta LibInt.intR t (ring (4 * n + 3 + 1)).zero) + -(delta LibInt.intR t (ring (4 * n + 3 + 1)).one) := by
  unfold Ri Ii; rw [f_eq n hpr ι hi]
  by_cases h0 : t = (ring (4 * n + 3 + 1)).zero
  · rw [h0, show (ring (4 * n + 3 + 1)).mul (ring (4 * n + 3 + 1)).zero (om n (ring (4 * n + 3 + 1)).zero) = (ring (4 * n + 3 + 1)).zero by lib_ring, chi4_zero]
    unfold delta; rw [ite_pos' _ _ rfl, ite_neg' _ _ (fun e => PowerSum.one_ne_zero' (4 * n + 3) e.symm)]; rfl
  · by_cases h1 : t = (ring (4 * n + 3 + 1)).one
    · rw [h1, show (ring (4 * n + 3 + 1)).mul (ring (4 * n + 3 + 1)).one (om n (ring (4 * n + 3 + 1)).one) = (ring (4 * n + 3 + 1)).zero by unfold om; lib_ring, chi4_zero]
      unfold delta; rw [ite_neg' _ _ (fun e => PowerSum.one_ne_zero' (4 * n + 3) e), ite_pos' _ _ rfl]; rfl
    · have hx : (ring (4 * n + 3 + 1)).mul t (om n t) ≠ (ring (4 * n + 3 + 1)).zero := fun e => by
        cases nozd (4 * n + 3) hpr _ _ e with
        | inl e1 => exact h0 e1
        | inr e1 => exact h1 (by
            calc t = (ring (4 * n + 3 + 1)).add t (om n t) := by rw [e1]; lib_ring
              _ = (ring (4 * n + 3 + 1)).one := by unfold om; lib_ring)
      unfold delta; rw [ite_neg' _ _ h0, ite_neg' _ _ h1]
      rcases ResidueCriterion.chi4_cases n ι ((ring (4 * n + 3 + 1)).mul t (om n t)) with e | e | e | e | e
      · exact absurd e (chi4_ne n hpr ι hi _ hx)
      all_goals (rw [e]; rfl)

/-! ## the sums -/

theorem sum_one : sumK LibInt.intR (fun _ : Zp (4 * n + 3 + 1) => (1 : Int)) (LL n) = Int.ofNat (4 * n + 3 + 1 + 1) :=
  (sum_fst (fun _ : Zp (4 * n + 3 + 1) => (Cyclo4.cyc4 LibInt.intR).one) (LL n)).symm.trans (congrArg Prod.fst (sum_oneq n _))

theorem sum_delta (z : Zp (4 * n + 3 + 1)) : sumK LibInt.intR (fun t => delta LibInt.intR t z) (LL n) = 1 :=
  CMInert.complete_allZ (4 * n + 3 + 1) LibInt.intR z

theorem sum_RpI (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) :
    sumK LibInt.intR (Ri n ι) (LL n) + sumK LibInt.intR (Ii n ι) (LL n) = Int.ofNat (4 * n + 3 + 1 + 1) + -2 := by
  rw [← sI_add, sumK_ext LibInt.intR _ _ (RpI n hpr ι hi), sI_add, sI_add, sI_neg, sI_neg, sum_one, sum_delta, sum_delta]; lib_int

/-- J(ρ, ρ) = −1. -/
theorem J_rho (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun t => (Cyclo4.cyc4 LibInt.intR).mul (rho n ι t) (rho n ι (om n t))) (LL n) = (Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one := by
  rw [← FiniteRecChain.reindex (Cyclo4.cyc4 LibInt.intR) (LL n) (CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR)) ((ring (4 * n + 3 + 1)).neg) ((ring (4 * n + 3 + 1)).neg) (fun x => by lib_ring) (fun x => by lib_ring)]
  rw [← D_ne' n hpr (rho n ι) (rho_mul n hpr ι hi) (rho_one n hpr ι hi) (rho_zero n ι) (rho_sum n hpr ι hi) (ring (4 * n + 3 + 1)).one (fun e => PowerSum.one_ne_zero' (4 * n + 3) e)]
  exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun b => by
    show (Cyclo4.cyc4 LibInt.intR).mul (rho n ι ((ring (4 * n + 3 + 1)).neg b)) (rho n ι (om n ((ring (4 * n + 3 + 1)).neg b))) = (Cyclo4.cyc4 LibInt.intR).mul (rho n ι ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one b)) (rho n ι (ivq n b))
    rw [rho_iv n hpr ι hi, show om n ((ring (4 * n + 3 + 1)).neg b) = (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one b by unfold om; lib_ring,
      show (ring (4 * n + 3 + 1)).neg b = (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) b by lib_ring, rho_mul n hpr ι hi, rho_neg1 n hpr ι hi]
    lib_ring) _

theorem sum_RmI (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) :
    sumK LibInt.intR (Ri n ι) (LL n) + -sumK LibInt.intR (Ii n ι) (LL n) = -1 := by
  rw [← sI_neg, ← sI_add, sumK_ext LibInt.intR _ _ (RmI n ι), ← sum_fst, J_rho n hpr ι hi]; rfl

theorem two_cancel (x y : Int) (h : 2 * x = 2 * y) : x = y := by
  have h2 : 2 * (x + -y) = 0 := by
    calc 2 * (x + -y) = 2 * x + -(2 * y) := by lib_int
      _ = 2 * y + -(2 * y) := by rw [h]
      _ = 0 := by lib_int
  cases LibInt.int_noZD _ _ h2 with
  | inl e => exact absurd e (fun e' => Nat.noConfusion (Int.ofNat.inj e'))
  | inr e => calc x = (x + -y) + y := by lib_int
      _ = y := by rw [e]; exact LibInt.intR.zero_add y

theorem sum_Ri (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : sumK LibInt.intR (Ri n ι) (LL n) = 2 * Int.ofNat n + 1 := by
  apply two_cancel
  have e1 := sum_RpI n hpr ι hi
  have e2 := sum_RmI n hpr ι hi
  rw [show Int.ofNat (4 * n + 3 + 1 + 1) = 4 * Int.ofNat n + 5 from rfl] at e1
  calc 2 * sumK LibInt.intR (Ri n ι) (LL n) = (sumK LibInt.intR (Ri n ι) (LL n) + sumK LibInt.intR (Ii n ι) (LL n)) + (sumK LibInt.intR (Ri n ι) (LL n) + -sumK LibInt.intR (Ii n ι) (LL n)) := by lib_int
    _ = 2 * (2 * Int.ofNat n + 1) := by rw [e1, e2]; lib_int

theorem sum_Ii (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : sumK LibInt.intR (Ii n ι) (LL n) = 2 * Int.ofNat n + 2 := by
  apply two_cancel
  have e1 := sum_RpI n hpr ι hi
  have e2 := sum_RmI n hpr ι hi
  rw [show Int.ofNat (4 * n + 3 + 1 + 1) = 4 * Int.ofNat n + 5 from rfl] at e1
  calc 2 * sumK LibInt.intR (Ii n ι) (LL n) = (sumK LibInt.intR (Ri n ι) (LL n) + sumK LibInt.intR (Ii n ι) (LL n)) + -(sumK LibInt.intR (Ri n ι) (LL n) + -sumK LibInt.intR (Ii n ι) (LL n)) := by lib_int
    _ = 2 * (2 * Int.ofNat n + 2) := by rw [e1, e2]; lib_int

/-! ## the fixed point h = 1/2 -/

theorem h_ne (_hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) : h n ≠ (ring (4 * n + 3 + 1)).zero := fun e => PowerSum.one_ne_zero' (4 * n + 3) (by rw [← hh n, e]; lib_ring)

theorem f_h (ι : Zp (4 * n + 3 + 1)) : f n ι (h n) = rho n ι (h n) := by unfold f; rw [om_h]; rfl

theorem rho_real (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (x : Zp (4 * n + 3 + 1)) (hx : x ≠ (ring (4 * n + 3 + 1)).zero) :
    rho n ι x = (1, 0) ∨ rho n ι x = (-1, 0) := by
  unfold rho
  rcases ResidueCriterion.chi4_cases n ι x with e | e | e | e | e
  · exact absurd e (chi4_ne n hpr ι hi x hx)
  · rw [e]; exact Or.inl rfl
  · rw [e]; exact Or.inr rfl
  · rw [e]; exact Or.inl rfl
  · rw [e]; exact Or.inr rfl

/-- **The quartic Jacobi sum mod 4**: J = a + bi with a = Re ρ(1/2) + 2n + 4K and b = 2n + 2 + 4K' (p = 4n + 5). -/
theorem quart_mod4 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) :
    ∃ K K' : Int, (Jq n (chi4 n ι)).1 = (rho n ι (h n)).1 + 2 * Int.ofNat n + 4 * K ∧ (Jq n (chi4 n ι)).2 = 2 * Int.ofNat n + 2 + 4 * K' := by
  have hJ : Jq n (chi4 n ι) = sumK (Cyclo4.cyc4 LibInt.intR) (f n ι) (LL n) := rfl
  have hRi : ∀ x, Ri n ι (om n x) = Ri n ι x := fun x => by unfold Ri; rw [f_om]
  have hIi : ∀ x, Ii n ι (om n x) = Ii n ι x := fun x => by unfold Ii; rw [f_om]
  have pa := pair n hpr (fun t => (f n ι t).1) (fun x => by show (f n ι (om n x)).1 = _; rw [f_om])
  have pb := pair n hpr (fun t => (f n ι t).2) (fun x => by show (f n ι (om n x)).2 = _; rw [f_om])
  have pR := pair n hpr (Ri n ι) hRi
  have pI := pair n hpr (Ii n ι) hIi
  have da : sumK LibInt.intR (fun x => ltI (4 * n + 3) x (om n x) * (f n ι x).1) (LL n) = sumK LibInt.intR (fun x => ltI (4 * n + 3) x (om n x) * Ri n ι x) (LL n) + 2 * sumK LibInt.intR (fun x => ltI (4 * n + 3) x (om n x) * c1 n ι x) (LL n) := by
    rw [← sI_mul, ← sI_add]; exact sumK_ext LibInt.intR _ _ (fun x => by show _ * (f n ι x).1 = _; rw [dec1 n hpr ι hi]; lib_int) _
  have db : sumK LibInt.intR (fun x => ltI (4 * n + 3) x (om n x) * (f n ι x).2) (LL n) = sumK LibInt.intR (fun x => ltI (4 * n + 3) x (om n x) * Ii n ι x) (LL n) + 2 * sumK LibInt.intR (fun x => ltI (4 * n + 3) x (om n x) * c2 n ι x) (LL n) := by
    rw [← sI_mul, ← sI_add]; exact sumK_ext LibInt.intR _ _ (fun x => by show _ * (f n ι x).2 = _; rw [dec2 n hpr ι hi]; lib_int) _
  have hfh : (f n ι (h n)).1 * (f n ι (h n)).1 = 1 ∧ (f n ι (h n)).2 = 0 := by
    rw [f_h]; rcases rho_real n hpr ι hi (h n) (h_ne n hpr) with e | e <;> rw [e] <;> exact ⟨rfl, rfl⟩
  refine ⟨sumK LibInt.intR (fun x => ltI (4 * n + 3) x (om n x) * c1 n ι x) (LL n), sumK LibInt.intR (fun x => ltI (4 * n + 3) x (om n x) * c2 n ι x) (LL n), ?_, ?_⟩
  · rw [hJ, sum_fst, pa, da, ← f_h]
    have e1 : 2 * sumK LibInt.intR (fun x => ltI (4 * n + 3) x (om n x) * Ri n ι x) (LL n) = 2 * Int.ofNat n := by
      have := pR; rw [sum_Ri n hpr ι hi] at this
      have hr : Ri n ι (h n) = 1 := hfh.1
      rw [hr] at this
      calc _ = (1 + 2 * sumK LibInt.intR (fun x => ltI (4 * n + 3) x (om n x) * Ri n ι x) (LL n)) + -1 := by lib_int
        _ = 2 * Int.ofNat n := by rw [← this]; lib_int
    show (f n ι (h n)).1 + 2 * (sumK LibInt.intR (fun x => ltI (4 * n + 3) x (om n x) * Ri n ι x) (LL n) + 2 * sumK LibInt.intR (fun x => ltI (4 * n + 3) x (om n x) * c1 n ι x) (LL n)) = _
    rw [show ∀ u v w : Int, u + 2 * (v + 2 * w) = u + 2 * v + 4 * w from fun u v w => by lib_int, e1]
  · rw [hJ, sum_snd, pb, db, hfh.2]
    have e1 : 2 * sumK LibInt.intR (fun x => ltI (4 * n + 3) x (om n x) * Ii n ι x) (LL n) = 2 * Int.ofNat n + 2 := by
      have := pI; rw [sum_Ii n hpr ι hi] at this
      have hr : Ii n ι (h n) = 0 := by unfold Ii; rw [hfh.2]; rfl
      rw [hr] at this
      calc _ = 0 + 2 * sumK LibInt.intR (fun x => ltI (4 * n + 3) x (om n x) * Ii n ι x) (LL n) := by lib_int
        _ = 2 * Int.ofNat n + 2 := by rw [← this]
    show 0 + 2 * (sumK LibInt.intR (fun x => ltI (4 * n + 3) x (om n x) * Ii n ι x) (LL n) + 2 * sumK LibInt.intR (fun x => ltI (4 * n + 3) x (om n x) * c2 n ι x) (LL n)) = _
    rw [show ∀ v w : Int, 0 + 2 * (v + 2 * w) = 2 * v + 4 * w from fun v w => by lib_int, e1]

/-! ## ρ(1/2) = χ(−1) -/

theorem rho_sq (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (y : Zp (4 * n + 3 + 1)) (hy : y ≠ (ring (4 * n + 3 + 1)).zero) : rho n ι ((ring (4 * n + 3 + 1)).mul y y) = (Cyclo4.cyc4 LibInt.intR).one := by
  rw [rho_mul n hpr ι hi]
  rcases rho_real n hpr ι hi y hy with e | e <;> rw [e] <;> rfl

theorem two_eq (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) :
    (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg ι) ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one ι) ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one ι)) := by
  calc (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one))) ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).add ι ((ring (4 * n + 3 + 1)).mul   -- (+75 chars; full line in the Zenodo source)
    _ = (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul ι ι) ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one))) ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).add ι ((ring (4 * n + 3 + 1)).mul ι ((ring (4 * n + 3 + 1)).mul ι ι)))) := by rw [hi]
    _ = _ := by lib_ring

theorem rho_two (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : rho n ι ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one) = chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by
  have h1 : (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one ι ≠ (ring (4 * n + 3 + 1)).zero := fun e => Eisen.two_ne_zero' (4 * n + 3) (by rw [show 4 * n + 3 = (4 * n + 1) + 2 by lib_nat]; exact Nat.le_add_left 2 _) (by
    have ei : ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one := by calc ι = (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one ι) ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by lib_ring
                                              _ = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one := by rw [e]; lib_ring
    have := hi; rw [ei] at this
    calc (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (ring (4 * n + 3 + 1)).one := by lib_ring
      _ = (ring (4 * n + 3 + 1)).zero := by rw [this]; lib_ring)
  rw [two_eq n ι hi, rho_mul n hpr ι hi, rho_sq n hpr ι hi _ h1, show (ring (4 * n + 3 + 1)).neg ι = (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) ι by lib_ring, rho_mul n hpr ι hi, rho_neg1 n hpr ι hi]
  show (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (Cyclo4.cyc4 LibInt.intR).one ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ι) (chi4 n ι ι))) (Cyclo4.cyc4 LibInt.intR).one = _
  rw [← chi4_mul n hpr ι hi, hi]; lib_ring

theorem rho_h (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : rho n ι (h n) = chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by
  have h2 : (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one ≠ (ring (4 * n + 3 + 1)).zero := Eisen.two_ne_zero' (4 * n + 3) (by rw [show 4 * n + 3 = (4 * n + 1) + 2 by lib_nat]; exact Nat.le_add_left 2 _)
  have e1 : (Cyclo4.cyc4 LibInt.intR).mul (rho n ι (h n)) (rho n ι ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one)) = (Cyclo4.cyc4 LibInt.intR).one := by
    rw [← rho_mul n hpr ι hi, show (ring (4 * n + 3 + 1)).mul (h n) ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one) = (ring (4 * n + 3 + 1)).add (h n) (h n) by lib_ring, hh, rho_one n hpr ι hi]
  have e2 : (Cyclo4.cyc4 LibInt.intR).mul (rho n ι ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one)) (rho n ι ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one)) = (Cyclo4.cyc4 LibInt.intR).one := by
    rw [← rho_mul n hpr ι hi]; exact rho_sq n hpr ι hi _ h2
  rw [← rho_two n hpr ι hi]
  calc rho n ι (h n) = (Cyclo4.cyc4 LibInt.intR).mul (rho n ι (h n)) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one)) (rho n ι ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one))) := by rw [e2]; lib_ring
    _ = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι (h n)) (rho n ι ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one))) (rho n ι ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one)) := by lib_ring
    _ = _ := by rw [e1]; lib_ring

/-- **−χ(−1)J is primary**: J = a + bi with a = χ(−1) + 2n + 4K, b = 2n + 2 + 4K' (p = 4n + 5), i.e. a ≡ −1 and b ≡ (p − 1)/2 (mod 4). -/
theorem quart_primary (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) :
    ∃ K K' : Int, (Jq n (chi4 n ι)).1 = (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)).1 + 2 * Int.ofNat n + 4 * K ∧ (Jq n (chi4 n ι)).2 = 2 * Int.ofNat n + 2 + 4 * K' := by
  rw [← rho_h n hpr ι hi]; exact quart_mod4 n hpr ι hi

end QuartPrimary

#print axioms QuartPrimary.om_om
#print axioms QuartPrimary.f_om
#print axioms QuartPrimary.hh
#print axioms QuartPrimary.om_h
#print axioms QuartPrimary.fix_h
#print axioms QuartPrimary.sum_fst
#print axioms QuartPrimary.sum_snd
#print axioms QuartPrimary.fixed_sum
#print axioms QuartPrimary.pair
#print axioms QuartPrimary.sI_add
#print axioms QuartPrimary.sI_mul
#print axioms QuartPrimary.sI_neg
#print axioms QuartPrimary.f_eq
#print axioms QuartPrimary.dec1
#print axioms QuartPrimary.dec2
#print axioms QuartPrimary.RmI
#print axioms QuartPrimary.chi4_ne
#print axioms QuartPrimary.RpI
#print axioms QuartPrimary.sum_one
#print axioms QuartPrimary.sum_delta
#print axioms QuartPrimary.sum_RpI
#print axioms QuartPrimary.J_rho
#print axioms QuartPrimary.sum_RmI
#print axioms QuartPrimary.two_cancel
#print axioms QuartPrimary.sum_Ri
#print axioms QuartPrimary.sum_Ii
#print axioms QuartPrimary.h_ne
#print axioms QuartPrimary.f_h
#print axioms QuartPrimary.rho_real
#print axioms QuartPrimary.quart_mod4
#print axioms QuartPrimary.rho_sq
#print axioms QuartPrimary.two_eq
#print axioms QuartPrimary.rho_two
#print axioms QuartPrimary.rho_h
#print axioms QuartPrimary.quart_primary

lang/QuartQuartic.lean

lean_all/lang/QuartQuartic.lean

/-
  lang/QuartQuartic — T43, cycle 243: the trace of y² = x⁴ + D at p ≡ 1 (mod 4).

  p = 4n + 5. #{x⁴ = u} = 1 + χ(u) + ρ(u) + χ̄(u) (FermatQuartic.q4_count), and u = −Dt:
      quartic_sum   Σ_x ρ(x⁴ + D) = −1 + ρ(D)(z + z̄),  z = χ(−D) J(χ, ρ)        (D ≠ 0)
  (J(ρ, ρ) = −1 gives the constant; J(χ, ρ) = χ(−1)J by QuartDup.)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.QuartTwist
import lang.QuartTwistCount

namespace QuartQuartic

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius QuartChar QuartJacobi QuartPrimary QuartDup FermatQuartic QuartTwist
open LibRing (castZ castN)

variable (n : Nat)

def S4 (ι D : Zp (4 * n + 3 + 1)) : Int × Int := sumK (Cyclo4.cyc4 LibInt.intR) (fun x => rho n ι ((ring (4 * n + 3 + 1)).add (q4 n x) D)) (LL n)

theorem rho_unit (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (D : Zp (4 * n + 3 + 1)) (hD : D ≠ (ring (4 * n + 3 + 1)).zero) :
    (Cyclo4.cyc4 LibInt.intR).mul (rho n ι ((ring (4 * n + 3 + 1)).neg D)) (rho n ι D) = (Cyclo4.cyc4 LibInt.intR).one := by
  rw [show (ring (4 * n + 3 + 1)).neg D = (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) D by lib_ring, rho_mul n hpr ι hi, rho_neg1 n hpr ι hi]
  rcases QuartPrimary.rho_real n hpr ι hi D hD with e | e <;> rw [e] <;> rfl

/-- **Σ_x ρ(x⁴ + D) = −1 + ρ(D)(z + z̄), z = χ(−D) J(χ, ρ).** -/
theorem quartic_sum (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (D : Zp (4 * n + 3 + 1)) (hD : D ≠ (ring (4 * n + 3 + 1)).zero) :
    S4 n ι D = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι D) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg D)) (Jcr n ι)) (gconj ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg D)) (Jcr n ι))))) := by
  have hD' : (ring (4 * n + 3 + 1)).neg D ≠ (ring (4 * n + 3 + 1)).zero := fun e => hD (by calc D = (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).neg D) := by lib_ring
                                                         _ = (ring (4 * n + 3 + 1)).zero := by rw [e]; lib_ring)
  unfold S4
  rw [FermatQuartic.step2 n hpr ι hi (fun a => rho n ι ((ring (4 * n + 3 + 1)).add a D))]
  have hexp : ∀ a, (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (rho n ι ((ring (4 * n + 3 + 1)).add a D)) = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (rho n ι ((ring (4 * n + 3 + 1)).add a D))
      ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (rho n ι ((ring (4 * n + 3 + 1)).add a D)))) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (rho n ι ((ring (4 * n + 3 + 1)).add a D)))) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (rho n ι ((ring (4 * n + 3 + 1)).add a D))) := fun a => by unfold g4; lib_ring
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ hexp, sumK_add, sumK_add, sumK_add]
  have S0 : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => rho n ι ((ring (4 * n + 3 + 1)).add a D)) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := by
    rw [FiniteRecChain.reindex (Cyclo4.cyc4 LibInt.intR) (LL n) (CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR)) (fun a => (ring (4 * n + 3 + 1)).add a D) (fun a => (ring (4 * n + 3 + 1)).add a ((ring (4 * n + 3 + 1)).neg D)) (fun x => by lib_ring) (fun y => by lib_ring) (rho n ι)]
    exact rho_sum n hpr ι hi
  have scale : ∀ F : Zp (4 * n + 3 + 1) → Int × Int, sumK (Cyclo4.cyc4 LibInt.intR) F (LL n) = sumK (Cyclo4.cyc4 LibInt.intR) (fun t => F ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg D) t)) (LL n) := fun F =>
    (CharSumGen.sum_scale (Cyclo4.cyc4 LibInt.intR) (4 * n + 2) hpr F ((ring (4 * n + 3 + 1)).neg D) hD').symm
  have hsub : ∀ t, (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg D) t) D = (ring (4 * n + 3 + 1)).mul D (om n t) := fun t => by unfold om; lib_ring
  have S1 : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (rho n ι ((ring (4 * n + 3 + 1)).add a D))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg D)) (rho n ι D)) (Jcr n ι) := by
    rw [scale, Jcr, mul_sumK]
    exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun t => by rw [hsub, chi4_mul n hpr ι hi, rho_mul n hpr ι hi]; lib_ring) _
  have S2 : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (rho n ι ((ring (4 * n + 3 + 1)).add a D))) (LL n) = (Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one := by
    rw [scale, ← rho_unit n hpr ι hi D hD]
    rw [show (Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι ((ring (4 * n + 3 + 1)).neg D)) (rho n ι D)) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι ((ring (4 * n + 3 + 1)).neg D)) (rho n ι D)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one) by lib_ring,
      ← J_rho n hpr ι hi, mul_sumK]
    exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun t => by rw [hsub, rho_mul n hpr ι hi, rho_mul n hpr ι hi]; lib_ring) _
  have S3 : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (rho n ι ((ring (4 * n + 3 + 1)).add a D))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι ((ring (4 * n + 3 + 1)).neg D)) (rho n ι D)) (gconj (Jcr n ι)) := by
    rw [scale, Jcr, gconj_sum, mul_sumK]
    exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun t => by
      rw [hsub, chib_mul n hpr ι hi, rho_mul n hpr ι hi, gconj_mul, FermatQuartic.gconj_rho n hpr ι hi, ← chib_eq n hpr ι hi]; lib_ring) _
  rw [S0, S1, S2, S3, gconj_mul, ← gconj_chib n hpr ι hi ((ring (4 * n + 3 + 1)).neg D)]
  have hc : gconj (gconj (chi4 n ι ((ring (4 * n + 3 + 1)).neg D))) = chi4 n ι ((ring (4 * n + 3 + 1)).neg D) := Prod.ext rfl (by show -(-(chi4 n ι ((ring (4 * n + 3 + 1)).neg D)).2) = _; lib_int)
  rw [chib_eq n hpr ι hi ((ring (4 * n + 3 + 1)).neg D), hc]
  lib_ring

/-- **Hasse's bound for y² = x⁴ + D**: (Σ_x ρ(x⁴ + D) + 1)² ≤ 4p. -/
theorem quartic_hasse (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (D : Zp (4 * n + 3 + 1)) (hD : D ≠ (ring (4 * n + 3 + 1)).zero) :
    ∃ s : Nat, ((S4 n ι D).1 + 1) * ((S4 n ι D).1 + 1) + Int.ofNat s = 4 * Int.ofNat (4 * n + 3 + 1 + 1) := by
  have hD' : (ring (4 * n + 3 + 1)).neg D ≠ (ring (4 * n + 3 + 1)).zero := fun e => hD (by calc D = (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).neg D) := by lib_ring
                                                         _ = (ring (4 * n + 3 + 1)).zero := by rw [e]; lib_ring)
  rw [quartic_sum n hpr ι hi D hD, Jcr_eq n hpr ι hi]
  have hN := quart_norm n hpr ι hi
  have m1 : (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one ≠ (ring (4 * n + 3 + 1)).zero := fun e => PowerSum.one_ne_zero' (4 * n + 3) (by
    calc (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by lib_ring
      _ = (ring (4 * n + 3 + 1)).zero := by rw [e]; lib_ring)
  have h1 := (QuartTwistCount.unit_norm n hpr ι hi _ hD').2
  have h2 := (QuartTwistCount.unit_norm n hpr ι hi _ m1).2
  have hm := QuartTwistCount.norm_mul4 (chi4 n ι ((ring (4 * n + 3 + 1)).neg D)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (Jq n (chi4 n ι)))
  rw [QuartTwistCount.norm_mul4 (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (Jq n (chi4 n ι)), h1, h2, hN] at hm
  have hr : rho n ι D = (1, 0) ∨ rho n ι D = (-1, 0) := QuartPrimary.rho_real n hpr ι hi D hD
  generalize (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg D)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (Jq n (chi4 n ι))) = z at hm ⊢
  match StrictHasse.sq_nat z.2 with
  | ⟨k, hk⟩ =>
    refine ⟨4 * (k * k), ?_⟩
    have e1 : Int.ofNat (4 * (k * k)) = 4 * (z.2 * z.2) := by rw [hk]; rfl
    rw [e1, show 4 * Int.ofNat (4 * n + 3 + 1 + 1) = 4 * (1 * (1 * Int.ofNat (4 * n + 3 + 1 + 1))) by lib_int, ← hm]
    rcases hr with e | e <;> rw [e]
    · show (-1 + (1 * (z.1 + z.1) + -(0 * (z.2 + -z.2))) + 1) * (-1 + (1 * (z.1 + z.1) + -(0 * (z.2 + -z.2))) + 1) + 4 * (z.2 * z.2) = 4 * (z.1 * z.1 + z.2 * z.2)
      lib_int
    · show (-1 + (-1 * (z.1 + z.1) + -(0 * (z.2 + -z.2))) + 1) * (-1 + (-1 * (z.1 + z.1) + -(0 * (z.2 + -z.2))) + 1) + 4 * (z.2 * z.2) = 4 * (z.1 * z.1 + z.2 * z.2)
      lib_int

/-- **y² = x⁴ + D and y² = x³ − 4Dx have the same trace**: Σ_x ρ(x⁴ + D) + 1 = Σ_x ρ(x³ − 4Dx). -/
theorem quartic_cubic (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (D : Zp (4 * n + 3 + 1)) (hD : D ≠ (ring (4 * n + 3 + 1)).zero) :
    (Cyclo4.cyc4 LibInt.intR).add (S4 n ι D) (Cyclo4.cyc4 LibInt.intR).one = Sq n ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul (mtwo n) (mtwo n)) D) ι := by
  have h2 := mtwo_ne n
  have h4 : (ring (4 * n + 3 + 1)).mul (mtwo n) (mtwo n) ≠ (ring (4 * n + 3 + 1)).zero := fun e => by
    cases nozd (4 * n + 3) hpr _ _ e with
    | inl h => exact h2 h
    | inr h => exact h2 h
  have h4D : (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul (mtwo n) (mtwo n)) D ≠ (ring (4 * n + 3 + 1)).zero := fun e => by
    cases nozd (4 * n + 3) hpr _ _ e with
    | inl h => exact h4 h
    | inr h => exact hD h
  rw [quartic_sum n hpr ι hi D hD, quart_twist n hpr ι hi _ h4D]
  -- the two coefficients agree: ρ(D)χ(−D) = χ(−1)χ̄(D) = χ̄(4D)
  have hs4 : chi4 n ι ((ring (4 * n + 3 + 1)).mul (mtwo n) (mtwo n)) = chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by
    rw [chi4_mul n hpr ι hi]; exact QuartPrimary.rho_two n hpr ι hi
  have breal : chib n ι ((ring (4 * n + 3 + 1)).mul (mtwo n) (mtwo n)) = chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by
    rw [chib_eq n hpr ι hi, hs4]; rcases FermatQuartic.m1_real n hpr ι hi with e | e <;> rw [e] <;> rfl
  have hc : (Cyclo4.cyc4 LibInt.intR).mul (rho n ι D) (chi4 n ι ((ring (4 * n + 3 + 1)).neg D)) = chib n ι ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul (mtwo n) (mtwo n)) D) := by
    rw [chib_mul n hpr ι hi, breal, show (ring (4 * n + 3 + 1)).neg D = (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) D by lib_ring, chi4_mul n hpr ι hi]
    calc _ = (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι D) (rho n ι D)) := by lib_ring
      _ = _ := by rw [QuartTwist.chi_rho n hpr ι hi D]
  have rreal : gconj (rho n ι D) = rho n ι D := FermatQuartic.gconj_rho n hpr ι hi D
  rw [← hc]
  have g1 : gconj ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι D) (chi4 n ι ((ring (4 * n + 3 + 1)).neg D))) (Jcr n ι)) = (Cyclo4.cyc4 LibInt.intR).mul (rho n ι D) (gconj ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg D)) (Jcr n ι))) := by
    rw [gconj_mul, gconj_mul, gconj_mul, rreal]; lib_ring
  rw [g1]
  lib_ring

/-- **2-isogeny**: y² = x³ − Dx and y² = x³ + 4Dx have the same trace. -/
theorem isog2 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (D : Zp (4 * n + 3 + 1)) (hD : D ≠ (ring (4 * n + 3 + 1)).zero) :
    Sq n D ι = Sq n ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul (mtwo n) (mtwo n)) D)) ι := by
  have h2 := mtwo_ne n
  have nz : ∀ x y : Zp (4 * n + 3 + 1), x ≠ (ring (4 * n + 3 + 1)).zero → y ≠ (ring (4 * n + 3 + 1)).zero → (ring (4 * n + 3 + 1)).mul x y ≠ (ring (4 * n + 3 + 1)).zero := fun x y hx hy e => by
    cases nozd (4 * n + 3) hpr _ _ e with
    | inl h => exact hx h
    | inr h => exact hy h
  have h4D : (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul (mtwo n) (mtwo n)) D) ≠ (ring (4 * n + 3 + 1)).zero := fun e => nz _ _ (nz _ _ h2 h2) hD (by
    calc _ = (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul (mtwo n) (mtwo n)) D)) := by lib_ring
      _ = (ring (4 * n + 3 + 1)).zero := by rw [e]; lib_ring)
  rw [quart_twist n hpr ι hi D hD, quart_twist n hpr ι hi _ h4D]
  have hs4 : chi4 n ι ((ring (4 * n + 3 + 1)).mul (mtwo n) (mtwo n)) = chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by
    rw [chi4_mul n hpr ι hi]; exact QuartPrimary.rho_two n hpr ι hi
  have hss : (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) = (Cyclo4.cyc4 LibInt.intR).one := by
    rw [← chi4_mul n hpr ι hi, show (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) = (ring (4 * n + 3 + 1)).one by lib_ring]; exact chi_one n hpr ι hi
  have breal : ∀ x, chi4 n ι x = chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) → chib n ι x = chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := fun x hx => by
    rw [chib_eq n hpr ι hi, hx]; rcases FermatQuartic.m1_real n hpr ι hi with e | e <;> rw [e] <;> rfl
  have hc : chib n ι ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul (mtwo n) (mtwo n)) D)) = chib n ι D := by
    rw [show (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul (mtwo n) (mtwo n)) D) = (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) ((ring (4 * n + 3 + 1)).mul (mtwo n) (mtwo n))) D by lib_ring,
      chib_mul n hpr ι hi, chib_mul n hpr ι hi, breal _ rfl, breal _ hs4, hss]; lib_ring
  rw [hc]

/-- **The four quartic twists cancel**: if χ̄(v) = i then Σ_{k=0}^{3} Σ_x ρ(x³ − v^k D x) = 0. -/
theorem twist4_cancel (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (D v : Zp (4 * n + 3 + 1)) (hD : D ≠ (ring (4 * n + 3 + 1)).zero)
    (hv0 : v ≠ (ring (4 * n + 3 + 1)).zero) (hv : chib n ι v = ((0 : Int), (1 : Int))) :
    (Cyclo4.cyc4 LibInt.intR).add (Sq n D ι) ((Cyclo4.cyc4 LibInt.intR).add (Sq n ((ring (4 * n + 3 + 1)).mul v D) ι) ((Cyclo4.cyc4 LibInt.intR).add (Sq n ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul v v) D) ι)
      (Sq n ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul v v) v) D) ι))) = (Cyclo4.cyc4 LibInt.intR).zero := by
  have nz : ∀ x y : Zp (4 * n + 3 + 1), x ≠ (ring (4 * n + 3 + 1)).zero → y ≠ (ring (4 * n + 3 + 1)).zero → (ring (4 * n + 3 + 1)).mul x y ≠ (ring (4 * n + 3 + 1)).zero := fun x y hx hy e => by
    cases nozd (4 * n + 3) hpr _ _ e with
    | inl h => exact hx h
    | inr h => exact hy h
  have h2 := nz _ _ hv0 hv0
  have h3 := nz _ _ h2 hv0
  rw [quart_twist n hpr ι hi D hD, quart_twist n hpr ι hi _ (nz _ _ hv0 hD), quart_twist n hpr ι hi _ (nz _ _ h2 hD),
    quart_twist n hpr ι hi _ (nz _ _ h3 hD), chib_mul n hpr ι hi v D, chib_mul n hpr ι hi ((ring (4 * n + 3 + 1)).mul v v) D,
    chib_mul n hpr ι hi ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul v v) v) D, chib_mul n hpr ι hi ((ring (4 * n + 3 + 1)).mul v v) v, chib_mul n hpr ι hi v v, hv]
  have m1 : (Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) (((0 : Int), (1 : Int))) = (((-1 : Int)), ((0 : Int))) := rfl
  have m2 : (Cyclo4.cyc4 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) (((0 : Int), (1 : Int))) = (((0 : Int)), ((-1 : Int))) := rfl
  rw [m1, m2]
  generalize chib n ι D = c
  repeat rw [gconj_mul]
  have E : (Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one ((Cyclo4.cyc4 LibInt.intR).add (((0 : Int), (1 : Int))) ((Cyclo4.cyc4 LibInt.intR).add (((-1 : Int)), ((0 : Int))) (((0 : Int)), ((-1 : Int))))) = (Cyclo4.cyc4 LibInt.intR).zero := rfl
  have E' : (Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one ((Cyclo4.cyc4 LibInt.intR).add (gconj (((0 : Int), (1 : Int)))) ((Cyclo4.cyc4 LibInt.intR).add (gconj (((-1 : Int)), ((0 : Int)))) (gconj (((0 : Int)), ((-1 : Int)))))) = (Cyclo4.cyc4 LibInt.intR).zero := rfl
  calc _ = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul c (Jcr n ι)) ((Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one ((Cyclo4.cyc4 LibInt.intR).add (((0 : Int), (1 : Int))) ((Cyclo4.cyc4 LibInt.intR).add (((-1 : Int)), ((0 : Int))) (((0 : Int)), ((-1 : Int)))))))
          ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj c) (gconj (Jcr n ι))) ((Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one ((Cyclo4.cyc4 LibInt.intR).add (gconj (((0 : Int), (1 : Int)))) ((Cyclo4.cyc4 LibInt.intR).add (gconj (((-1 : Int)), ((0 : Int)))) (gconj (((0 : Int)), ((-1 : Int)))))))) := by
        generalize (((0 : Int), (1 : Int))) = a; generalize (((-1 : Int)), ((0 : Int))) = b; generalize (((0 : Int)), ((-1 : Int))) = d; lib_ring
    _ = (Cyclo4.cyc4 LibInt.intR).zero := by rw [E, E']; lib_ring

/-- **The four quartic twists of y² = x⁴ + D**: if χ(v) = i, ρ(v) = −1 then Σ_{k=0}^{3} Σ_x ρ(x⁴ + v^k D) = −4. -/
theorem quartic4_sum (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (D v : Zp (4 * n + 3 + 1)) (hD : D ≠ (ring (4 * n + 3 + 1)).zero)
    (hv0 : v ≠ (ring (4 * n + 3 + 1)).zero) (hv : chi4 n ι v = (((0 : Int), (1 : Int)))) (hr : rho n ι v = (((-1 : Int)), ((0 : Int)))) :
    (Cyclo4.cyc4 LibInt.intR).add (S4 n ι (D)) ((Cyclo4.cyc4 LibInt.intR).add (S4 n ι ((ring (4 * n + 3 + 1)).mul v (D))) ((Cyclo4.cyc4 LibInt.intR).add (S4 n ι ((ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v (D)))) (S4 n ι ((ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v (D))))))) = (((-4 : Int)), ((0 : Int))) := by
  have nz : ∀ x y : Zp (4 * n + 3 + 1), x ≠ (ring (4 * n + 3 + 1)).zero → y ≠ (ring (4 * n + 3 + 1)).zero → (ring (4 * n + 3 + 1)).mul x y ≠ (ring (4 * n + 3 + 1)).zero := fun x y hx hy e => by
    cases nozd (4 * n + 3) hpr _ _ e with
    | inl h => exact hx h
    | inr h => exact hy h
  have n0 := hD
  have n1 : (ring (4 * n + 3 + 1)).mul v (D) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n0
  have n2 : (ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v (D)) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n1
  have n3 : (ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v (D))) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n2
  rw [quartic_sum n hpr ι hi _ n0, quartic_sum n hpr ι hi _ n1, quartic_sum n hpr ι hi _ n2, quartic_sum n hpr ι hi _ n3]
  have hc : ∀ E : Zp (4 * n + 3 + 1), chi4 n ι ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul v E)) = (Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) (chi4 n ι ((ring (4 * n + 3 + 1)).neg E)) := fun E => by
    rw [show (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul v E) = (ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).neg E) by lib_ring, chi4_mul n hpr ι hi, hv]
  have hρ : ∀ E : Zp (4 * n + 3 + 1), rho n ι ((ring (4 * n + 3 + 1)).mul v E) = (Cyclo4.cyc4 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) (rho n ι E) := fun E => by rw [rho_mul n hpr ι hi, hr]
  repeat rw [hc]
  repeat rw [hρ]
  generalize chi4 n ι ((ring (4 * n + 3 + 1)).neg D) = c
  generalize rho n ι D = r
  repeat rw [gconj_mul]
  have EP : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) ((Cyclo4.cyc4 LibInt.intR).one))) ((Cyclo4.cyc4 LibI  -- (+831 chars; full line in the Zenodo source)
  have EQ : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul (gconj (((0 : Int), (1 : Int)))) ((Cyclo4.cyc4 LibInt.intR).one))) ((Cyclo4.c  -- (+879 chars; full line in the Zenodo source)
  calc _ = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one)))) ((Cyclo4.cyc4 L  -- (+1306 chars; full line in the Zenodo source)
          ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj c) (gconj (Jcr n ι))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibI  -- (+939 chars; full line in the Zenodo source)
        generalize (gconj (((0 : Int), (1 : Int)))) = a2; generalize (((0 : Int), (1 : Int))) = a; generalize (((-1 : Int)), ((0 : Int))) = b; lib_ring
    _ = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one))) := by rw [EP, EQ]; lib_ring
    _ = (((-4 : Int)), ((0 : Int))) := rfl

theorem gnorm (a b : Int) : (Cyclo4.cyc4 LibInt.intR).mul (a, b) (gconj (a, b)) = (a * a + b * b, 0) := by
  apply Prod.ext
  · show a * a + -(b * -b) = a * a + b * b
    lib_int
  · show a * -b + b * a = 0
    lib_int

/-- **Second moment over the quartic twists**: χ̄(v) = i ⇒ Σ_{k=0}^{3} (Σ_x ρ(x³ − v^k D x))² = 8p. -/
theorem quart_moment (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (D v : Zp (4 * n + 3 + 1)) (hD : D ≠ (ring (4 * n + 3 + 1)).zero)
    (hv0 : v ≠ (ring (4 * n + 3 + 1)).zero) (hv : chib n ι v = (((0 : Int), (1 : Int)))) :
    (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (Sq n (D) ι) (Sq n (D) ι)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (Sq n ((ring (4 * n + 3 + 1)).mul v (D)) ι) (Sq n ((ring (4 * n + 3 + 1)).mul v (D)) ι)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (Sq n ((ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v (D))) ι) (Sq n ((ring (4 * n   -- (+891 chars; full line in the Zenodo source)
  have nz : ∀ x y : Zp (4 * n + 3 + 1), x ≠ (ring (4 * n + 3 + 1)).zero → y ≠ (ring (4 * n + 3 + 1)).zero → (ring (4 * n + 3 + 1)).mul x y ≠ (ring (4 * n + 3 + 1)).zero := fun x y hx hy e => by
    cases nozd (4 * n + 3) hpr _ _ e with
    | inl h => exact hx h
    | inr h => exact hy h
  have n0 := hD
  have n1 : (ring (4 * n + 3 + 1)).mul v (D) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n0
  have n2 : (ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v (D)) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n1
  have n3 : (ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v (D))) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n2
  have hbb : (Cyclo4.cyc4 LibInt.intR).mul (chib n ι D) (gconj (chib n ι D)) = (Cyclo4.cyc4 LibInt.intR).one := by
    rw [show (chib n ι D) = ((chib n ι D).1, (chib n ι D).2) from rfl, gnorm, (QuartTwistCount.unit_norm n hpr ι hi D hD).1]; rfl
  have m1 : (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one ≠ (ring (4 * n + 3 + 1)).zero := fun e => by
    have : (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).zero := by
      calc (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by lib_ring
        _ = (ring (4 * n + 3 + 1)).zero := by rw [e]; lib_ring
    exact PowerSum.one_ne_zero' (4 * n + 3) this
  have hJJ : (Cyclo4.cyc4 LibInt.intR).mul (Jcr n ι) (gconj (Jcr n ι)) = (Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int)) := by
    rw [show (Jcr n ι) = ((Jcr n ι).1, (Jcr n ι).2) from rfl, gnorm, Jcr_eq n hpr ι hi, QuartTwistCount.norm_mul4,
      (QuartTwistCount.unit_norm n hpr ι hi _ m1).2, quart_norm n hpr ι hi]
    show (1 * Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int)) = _
    rw [Int.one_mul]
  rw [quart_twist n hpr ι hi _ n0, quart_twist n hpr ι hi _ n1, quart_twist n hpr ι hi _ n2, quart_twist n hpr ι hi _ n3]
  have hbv : ∀ E : Zp (4 * n + 3 + 1), chib n ι ((ring (4 * n + 3 + 1)).mul v E) = (Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) (chib n ι E) := fun E => by rw [chib_mul n hpr ι hi, hv]
  repeat rw [hbv]
  repeat rw [gconj_mul]
  have E1 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) ((Cyclo4.cyc4 LibInt.intR).one))) ((Cyclo4.cyc4 LibInt.  -- (+813 chars; full line in the Zenodo source)
  have E2 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul (gconj (((0 : Int), (1 : Int)))) ((Cyclo4.cyc4 LibInt.intR).one))) ((Cyclo4.cyc4  -- (+1052 chars; full line in the Zenodo source)
  have E3 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj (((0 : Int), (1 : Int)))) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul (gconj (((0 : Int), (1 : Int)))) ((Cyclo4.cyc4 LibInt.intR).one))) ((Cyc  -- (+909 chars; full line in the Zenodo source)
  calc _ = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι D) (chib n ι D)) ((Cyclo4.cyc4 LibInt.intR).mul (Jcr n ι) (Jcr n ι))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).m  -- (+978 chars; full line in the Zenodo source)
          ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι D) (gconj (chib n ι D))) ((Cyclo4.cyc4 LibInt.intR).mul (Jcr n ι) (gconj (Jcr n ι)))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι D) (gconj (chib n ι D))) ((Cyclo4.cyc4 LibInt.intR).mul (Jcr n ι) (  -- (+1232 chars; full line in the Zenodo source)
            ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj (chib n ι D)) (gconj (chib n ι D))) ((Cyclo4.cyc4 LibInt.intR).mul (gconj (Jcr n ι)) (gconj (Jcr n ι)))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR  -- (+1084 chars; full line in the Zenodo source)
        generalize (gconj (((0 : Int), (1 : Int)))) = a2; generalize (((0 : Int), (1 : Int))) = a; lib_ring
    _ = (Cyclo4.cyc4 LibInt.intR).add ((Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int))) ((Cyclo4.cyc4 LibInt.intR).add ((Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int))) ((Cyclo4.cyc4 LibInt.intR).add ((Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int))) ((Cyclo4.cyc4 LibInt.intR).add ((Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int))) ((Cyclo4.cyc4 LibInt.intR).add ((Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int))) ((Cyclo4.cyc4  -- (+195 chars; full line in the Zenodo source)
        rw [E1, E2, E3, hbb, hJJ]; generalize (Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int)) = q; lib_ring

/-- **Fourth moment over the quartic twists**: χ̄(v) = i ⇒ Σ_{k=0}^{3} a_k⁴ = 24p² + 4(J⁴ + J̄⁴), a_k = Σ_x ρ(x³ − v^k D x). -/
theorem quart_moment4 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (D v : Zp (4 * n + 3 + 1)) (hD : D ≠ (ring (4 * n + 3 + 1)).zero)
    (hv0 : v ≠ (ring (4 * n + 3 + 1)).zero) (hv : chib n ι v = (((0 : Int), (1 : Int)))) :
    (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (Sq n (D) ι) (Sq n (D) ι)) ((Cyclo4.cyc4 LibInt.intR).mul (Sq n (D) ι) (Sq n (D) ι))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (Sq n ((ring (4 * n + 3 + 1)).mul v (D)) ι) (Sq n ((ring (4 * n + 3 + 1)).mul v (D)) ι)) ((Cyclo4.cyc4 LibInt.intR).mul (Sq n  -- (+6111 chars; full line in the Zenodo source)
  have nz : ∀ x y : Zp (4 * n + 3 + 1), x ≠ (ring (4 * n + 3 + 1)).zero → y ≠ (ring (4 * n + 3 + 1)).zero → (ring (4 * n + 3 + 1)).mul x y ≠ (ring (4 * n + 3 + 1)).zero := fun x y hx hy e => by
    cases nozd (4 * n + 3) hpr _ _ e with
    | inl h => exact hx h
    | inr h => exact hy h
  have n0 := hD
  have n1 : (ring (4 * n + 3 + 1)).mul v (D) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n0
  have n2 : (ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v (D)) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n1
  have n3 : (ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v (D))) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n2
  have hbb : (Cyclo4.cyc4 LibInt.intR).mul (chib n ι D) (gconj (chib n ι D)) = (Cyclo4.cyc4 LibInt.intR).one := by
    rw [show (chib n ι D) = ((chib n ι D).1, (chib n ι D).2) from rfl, gnorm, (QuartTwistCount.unit_norm n hpr ι hi D hD).1]; rfl
  have m1 : (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one ≠ (ring (4 * n + 3 + 1)).zero := fun e => by
    have : (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).zero := by
      calc (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by lib_ring
        _ = (ring (4 * n + 3 + 1)).zero := by rw [e]; lib_ring
    exact PowerSum.one_ne_zero' (4 * n + 3) this
  have hJJ : (Cyclo4.cyc4 LibInt.intR).mul (Jcr n ι) (gconj (Jcr n ι)) = (Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int)) := by
    rw [show (Jcr n ι) = ((Jcr n ι).1, (Jcr n ι).2) from rfl, gnorm, Jcr_eq n hpr ι hi, QuartTwistCount.norm_mul4,
      (QuartTwistCount.unit_norm n hpr ι hi _ m1).2, quart_norm n hpr ι hi]
    show (1 * Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int)) = _
    rw [Int.one_mul]
  have hb4 : (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((chib n ι D)) ((chib n ι D))) ((Cyclo4.cyc4 LibInt.intR).mul ((chib n ι D)) ((chib n ι D))) = (Cyclo4.cyc4 LibInt.intR).one ∧ (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj (chib n ι D)) (gconj (chib n ι D))) ((Cyclo4.cyc4 LibInt.intR).mul (gconj (chib n ι D)) (gconj (chib n ι D))) = (Cyclo4.cyc4 LibInt  -- (+16 chars; full line in the Zenodo source)
    rw [chib_eq n hpr ι hi]
    rcases ResidueCriterion.chi4_cases n ι D with e | e | e | e | e
    · exact absurd e (chi4_ne n hpr ι hi D hD)
    all_goals (rw [e]; exact ⟨rfl, rfl⟩)
  rw [quart_twist n hpr ι hi _ n0, quart_twist n hpr ι hi _ n1, quart_twist n hpr ι hi _ n2, quart_twist n hpr ι hi _ n3]
  have hbv : ∀ E : Zp (4 * n + 3 + 1), chib n ι ((ring (4 * n + 3 + 1)).mul v E) = (Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) (chib n ι E) := fun E => by rw [chib_mul n hpr ι hi, hv]
  repeat rw [hbv]
  repeat rw [gconj_mul]
  have E0 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one))))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4  -- (+2648 chars; full line in the Zenodo source)
  have E1 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) ((Cyclo4.cyc4 Li  -- (+2153 chars; full line in the Zenodo source)
  have E2 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int  -- (+2296 chars; full line in the Zenodo source)
  have E3 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one))) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int  -- (+2057 chars; full line in the Zenodo source)
  have E4 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)))) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4  -- (+2456 chars; full line in the Zenodo source)
  calc _ = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj (chib n ι D)) (gconj (chib n ι D))) ((Cyclo4.cyc4 LibInt.intR).mul (gconj (chib n ι D)) (gconj (chib n ι D)))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj (Jcr n ι)) (gconj (Jcr n ι))) ((Cyclo4.cyc4 LibI  -- (+18694 chars; full line in the Zenodo source)
        generalize (gconj (((0 : Int), (1 : Int)))) = a2; generalize (((0 : Int), (1 : Int))) = a; lib_ring
    _ = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int)) (Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int)) (Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (I  -- (+4683 chars; full line in the Zenodo source)
        rw [E0, E1, E2, E3, E4, hbb, hJJ, hb4.1, hb4.2]; generalize (Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int)) = q; lib_ring

/-- **Third moment over the quartic twists vanishes**: χ̄(v) = i ⇒ Σ_{k=0}^{3} (Σ_x ρ(x³ − v^k D x))³ = 0. -/
theorem quart_moment3 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (D v : Zp (4 * n + 3 + 1)) (hD : D ≠ (ring (4 * n + 3 + 1)).zero)
    (hv0 : v ≠ (ring (4 * n + 3 + 1)).zero) (hv : chib n ι v = (((0 : Int), (1 : Int)))) :
    (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (Sq n (D) ι) (Sq n (D) ι)) (Sq n (D) ι)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (Sq n ((ring (4 * n + 3 + 1)).mul v (D)) ι) (Sq n ((ring (4 * n + 3 + 1)).mul v (D)) ι)) (Sq n ((ring (4 * n + 3 + 1)).mul v (D)) ι)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyc  -- (+706 chars; full line in the Zenodo source)
  have nz : ∀ x y : Zp (4 * n + 3 + 1), x ≠ (ring (4 * n + 3 + 1)).zero → y ≠ (ring (4 * n + 3 + 1)).zero → (ring (4 * n + 3 + 1)).mul x y ≠ (ring (4 * n + 3 + 1)).zero := fun x y hx hy e => by
    cases nozd (4 * n + 3) hpr _ _ e with
    | inl h => exact hx h
    | inr h => exact hy h
  have n0 := hD
  have n1 : (ring (4 * n + 3 + 1)).mul v (D) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n0
  have n2 : (ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v (D)) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n1
  have n3 : (ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v (D))) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n2
  rw [quart_twist n hpr ι hi _ n0, quart_twist n hpr ι hi _ n1, quart_twist n hpr ι hi _ n2, quart_twist n hpr ι hi _ n3]
  have hbv : ∀ E : Zp (4 * n + 3 + 1), chib n ι ((ring (4 * n + 3 + 1)).mul v E) = (Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) (chib n ι E) := fun E => by rw [chib_mul n hpr ι hi, hv]
  repeat rw [hbv]
  repeat rw [gconj_mul]
  have E0 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.  -- (+1811 chars; full line in the Zenodo source)
  have E1 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 Li  -- (+1507 chars; full line in the Zenodo source)
  have E2 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 Li  -- (+1459 chars; full line in the Zenodo source)
  have E3 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one))) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int  -- (+1667 chars; full line in the Zenodo source)
  calc _ = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((Cyclo4.cyc4 LibInt.intR).mul (gconj (chib n ι D)) (gconj (Jcr n ι)))) ((Cyclo4.cyc4 LibInt.intR).mul (((Cyclo4.cyc4 LibInt.intR).mul (gconj (chib n ι D)) (gconj (Jcr n ι)))) (((Cyclo4.cyc4 LibInt.intR).mul (gconj (chib n ι D)) (gconj (Jcr n ι)))))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4   -- (+9886 chars; full line in the Zenodo source)
        generalize (gconj (((0 : Int), (1 : Int)))) = a2; generalize (((0 : Int), (1 : Int))) = a; lib_ring
    _ = (Cyclo4.cyc4 LibInt.intR).zero := by rw [E0, E1, E2, E3]; lib_ring

end QuartQuartic
#print axioms QuartQuartic.rho_unit
#print axioms QuartQuartic.quartic_sum
#print axioms QuartQuartic.quartic_hasse
#print axioms QuartQuartic.quartic_cubic
#print axioms QuartQuartic.isog2
#print axioms QuartQuartic.twist4_cancel
#print axioms QuartQuartic.quartic4_sum
#print axioms QuartQuartic.gnorm
#print axioms QuartQuartic.quart_moment
#print axioms QuartQuartic.quart_moment4
#print axioms QuartQuartic.quart_moment3

lang/QuartRecip.lean

lean_all/lang/QuartRecip.lean

/-
  lang/QuartRecip — G2 (T43): quartic reciprocity for the primary-type prime J of ℤ[i] above p ≡ 1 (mod 4), at every rational prime q.

  p = 4n + 5, χ quartic character, J = Σ χ(t)χ(1 − t) (N(J) = p, QuartJacobi), φ : ℤ[i] → 𝔽_q[i] reduction. In 𝔽_q[i][C_p]:
    inert q = 4k + 3 (i^q = −i):  g^q = χ(q) ḡ,  g^{q+1} = χ(q)χ(−1) X,  g⁴ = J² X,  so
        quart_recip_inert    χ(q) χ(−1) = J^{2k+2} p^k       in 𝔽_q[i]
    split q = 4k + 5 (i^q = i):   g^q = χ̄(q) g,  g^q = (g⁴)^{k+1} g = J^{2k+2} p^{k+1} g, so
        quart_recip_split    χ̄(q) = J^{2k+2} p^{k+1}         in 𝔽_q[i]
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.QuartJacobi
import lang.CubicRecip

namespace QuartRecip

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius QuartChar QuartJacobi GroupRing GroupRingFrob
open LibRing (castZ castN)

variable (n : Nat)

section Gen
variable {R : Type} (T : CRing R)

def ph4 (z : Int × Int) : R × R := (castZ T z.1, castZ T z.2)

theorem ph4_add (x y : Int × Int) : ph4 T ((Cyclo4.cyc4 LibInt.intR).add x y) = (Cyclo4.cyc4 T).add (ph4 T x) (ph4 T y) := by
  apply Prod.ext
  · exact IntCast.castZ_add T x.1 y.1
  · exact IntCast.castZ_add T x.2 y.2

theorem ph4_mul (x y : Int × Int) : ph4 T ((Cyclo4.cyc4 LibInt.intR).mul x y) = (Cyclo4.cyc4 T).mul (ph4 T x) (ph4 T y) := by
  apply Prod.ext
  · show castZ T (x.1 * y.1 + -(x.2 * y.2)) = T.add (T.mul (castZ T x.1) (castZ T y.1)) (T.neg (T.mul (castZ T x.2) (castZ T y.2)))
    rw [IntCast.castZ_add, HasseDavenport.castZ_neg, HasseDavenport.castZ_mul, HasseDavenport.castZ_mul]
  · show castZ T (x.1 * y.2 + x.2 * y.1) = T.add (T.mul (castZ T x.1) (castZ T y.2)) (T.mul (castZ T x.2) (castZ T y.1))
    rw [IntCast.castZ_add, HasseDavenport.castZ_mul, HasseDavenport.castZ_mul]

theorem ph4_neg (x : Int × Int) : ph4 T ((Cyclo4.cyc4 LibInt.intR).neg x) = (Cyclo4.cyc4 T).neg (ph4 T x) := by
  apply Prod.ext
  · exact HasseDavenport.castZ_neg T x.1
  · exact HasseDavenport.castZ_neg T x.2

theorem ph4_one : ph4 T (Cyclo4.cyc4 LibInt.intR).one = (Cyclo4.cyc4 T).one := by
  apply Prod.ext
  · show castN T 1 = T.one
    exact add_zero' T _
  · rfl

theorem ph4_zero : ph4 T (Cyclo4.cyc4 LibInt.intR).zero = (Cyclo4.cyc4 T).zero := rfl

theorem ph4_sum {A : Type} (f : A → Int × Int) : ∀ l : List A, ph4 T (sumK (Cyclo4.cyc4 LibInt.intR) f l) = sumK (Cyclo4.cyc4 T) (fun a => ph4 T (f a)) l
  | [] => rfl
  | a :: l => by
    show ph4 T ((Cyclo4.cyc4 LibInt.intR).add (f a) (sumK _ f l)) = (Cyclo4.cyc4 T).add (ph4 T (f a)) (sumK (Cyclo4.cyc4 T) (fun a => ph4 T (f a)) l)
    rw [ph4_add, ph4_sum f l]

theorem ph4_delta {G : Type} [DecidableEq G] (x y : G) : ph4 T (delta (Cyclo4.cyc4 LibInt.intR) x y) = delta (Cyclo4.cyc4 T) x y := by
  unfold delta
  by_cases h : x = y
  · rw [ite_pos' _ _ h, ite_pos' _ _ h]; exact ph4_one T
  · rw [ite_neg' _ _ h, ite_neg' _ _ h]; exact ph4_zero T

theorem ph4_pow (z : Int × Int) : ∀ j, pw (Cyclo4.cyc4 T) (ph4 T z) j = ph4 T (pw (Cyclo4.cyc4 LibInt.intR) z j)
  | 0 => (ph4_one T).symm
  | j + 1 => by show (Cyclo4.cyc4 T).mul (pw _ (ph4 T z) j) (ph4 T z) = ph4 T ((Cyclo4.cyc4 LibInt.intR).mul (pw (Cyclo4.cyc4 LibInt.intR) z j) z); rw [ph4_pow z j, ph4_mul]

def mp4 (u : V (Int × Int) ((4 * n + 3 + 1) + 1)) : V (R × R) ((4 * n + 3 + 1) + 1) := tb (4 * n + 3 + 1) (fun a => ph4 T (gt (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) u a))

theorem mp4_mul (u v : V (Int × Int) ((4 * n + 3 + 1) + 1)) : gmul (Cyclo4.cyc4 T) (4 * n + 3 + 1) (mp4 n T u) (mp4 n T v) = mp4 n T (gmul (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) u v) :=
  ext (Cyclo4.cyc4 T) (4 * n + 3 + 1) _ _ (fun c => by
    unfold mp4
    rw [gt_tb, gt_mul, gt_mul, ph4_sum]
    exact sumK_ext (Cyclo4.cyc4 T) _ _ (fun a => by rw [gt_tb, gt_tb, ph4_mul]) _)

theorem mp4_mono (s : Int × Int) (a : Zp (4 * n + 3 + 1)) : mp4 n T (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) a s) = mono (Cyclo4.cyc4 T) (4 * n + 3 + 1) a (ph4 T s) :=
  ext (Cyclo4.cyc4 T) (4 * n + 3 + 1) _ _ (fun c => by unfold mp4; rw [gt_tb, gt_mono, gt_mono, ph4_mul, ph4_delta])

theorem coef4 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (Q : Zp (4 * n + 3 + 1)) (hQ : Q ≠ (ring (4 * n + 3 + 1)).zero) (s : Zp (4 * n + 3 + 1) → R × R) (c : Zp (4 * n + 3 + 1)) :
    sumK (Cyclo4.cyc4 T) (fun a => (Cyclo4.cyc4 T).mul (s a) (delta (Cyclo4.cyc4 T) c ((ring (4 * n + 3 + 1)).mul Q a))) (GroupRing.L (4 * n + 3 + 1)) = s ((ring (4 * n + 3 + 1)).mul (ivq n Q) c) := by
  have hQQ := ivq_mul n hpr Q hQ
  have e := reindex (Cyclo4.cyc4 T) _ (CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 T)) (fun a => (ring (4 * n + 3 + 1)).mul Q a) (fun b => (ring (4 * n + 3 + 1)).mul (ivq n Q) b)
    (fun x => by rw [show (ring (4 * n + 3 + 1)).mul (ivq n Q) ((ring (4 * n + 3 + 1)).mul Q x) = (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul (ivq n Q) Q) x by lib_ring, hQQ]; lib_ring)
    (fun y => by rw [show (ring (4 * n + 3 + 1)).mul Q ((ring (4 * n + 3 + 1)).mul (ivq n Q) y) = (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul (ivq n Q) Q) y by lib_ring, hQQ]; lib_ring)
    (fun b => (Cyclo4.cyc4 T).mul (delta (Cyclo4.cyc4 T) b c) (s ((ring (4 * n + 3 + 1)).mul (ivq n Q) b)))
  have e2 : sumK (Cyclo4.cyc4 T) (fun a => (Cyclo4.cyc4 T).mul (s a) (delta (Cyclo4.cyc4 T) c ((ring (4 * n + 3 + 1)).mul Q a))) (GroupRing.L (4 * n + 3 + 1))
      = sumK (Cyclo4.cyc4 T) (fun a => (fun b => (Cyclo4.cyc4 T).mul (delta (Cyclo4.cyc4 T) b c) (s ((ring (4 * n + 3 + 1)).mul (ivq n Q) b))) ((ring (4 * n + 3 + 1)).mul Q a)) (GroupRing.L (4 * n + 3 + 1)) :=
    sumK_ext (Cyclo4.cyc4 T) _ _ (fun a => by
      show _ = (Cyclo4.cyc4 T).mul (delta (Cyclo4.cyc4 T) ((ring (4 * n + 3 + 1)).mul Q a) c) (s ((ring (4 * n + 3 + 1)).mul (ivq n Q) ((ring (4 * n + 3 + 1)).mul Q a)))
      rw [show (ring (4 * n + 3 + 1)).mul (ivq n Q) ((ring (4 * n + 3 + 1)).mul Q a) = a by rw [show (ring (4 * n + 3 + 1)).mul (ivq n Q) ((ring (4 * n + 3 + 1)).mul Q a) = (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul (ivq n Q) Q) a by lib_ring, hQQ]; lib_ring,
        delta_symm, (Cyclo4.cyc4 T).mul_comm]) _
  rw [e2]
  exact e.trans (pick (Cyclo4.cyc4 T) _ (CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 T)) _ c)

theorem charq4 (j : Nat) (hchar : nsm T j T.one = T.zero) : nsm (Cyclo4.cyc4 T) j (Cyclo4.cyc4 T).one = (Cyclo4.cyc4 T).zero := by
  have hn : ∀ m, nsm (Cyclo4.cyc4 T) m (Cyclo4.cyc4 T).one = (nsm T m T.one, nsm T m T.zero) := fun m => by
    induction m with
    | zero => rfl
    | succ m ih => show (Cyclo4.cyc4 T).add (nsm (Cyclo4.cyc4 T) m (Cyclo4.cyc4 T).one) (Cyclo4.cyc4 T).one = _; rw [ih]; rfl
  rw [hn, hchar]
  apply Prod.ext
  · rfl
  · show nsm T j T.zero = T.zero
    rw [nsm_eq_mul, mul_zero]
end Gen

theorem chi4_4 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (x : Zp (4 * n + 3 + 1)) (hx : x ≠ (ring (4 * n + 3 + 1)).zero) : pw (Cyclo4.cyc4 LibInt.intR) (chi4 n ι x) 4 = (Cyclo4.cyc4 LibInt.intR).one ∧ pw (Cyclo4.cyc4 LibInt.intR) (chi4 n ι x) 3 = chib n  -- (+10 chars; full line in the Zenodo source)
  unfold chib; rw [chi_ivq n hpr ι hi]
  match exists_j n hpr ι hi x hx with
  | ⟨j, hj, ej⟩ =>
    rw [val4 n hpr ι hi x hx j hj ej]
    match j, hj with
    | 0, _ => exact ⟨rfl, rfl⟩
    | 1, _ => exact ⟨rfl, rfl⟩
    | 2, _ => exact ⟨rfl, rfl⟩
    | 3, _ => exact ⟨rfl, rfl⟩
    | k + 4, hk => exact absurd hk (fun h => Nat.lt_irrefl 4 (Nat.lt_of_le_of_lt (Nat.le_add_left 4 k) h))

theorem pow4 {S' : Type} (G : CRing S') (z : S') (h4 : pw G z 4 = G.one) (r : Nat) : ∀ m, pw G z (4 * m + r) = pw G z r
  | 0 => by rw [show 4 * 0 + r = r by lib_nat]
  | m + 1 => by rw [show 4 * (m + 1) + r = (4 * m + r) + 4 by lib_nat, HasseCM.pw_add', pow4 G z h4 r m, h4]; lib_ring


/-- **Quartic reciprocity at an inert q = 4k + 3**: χ(q) χ̄(−1) = (J² )^{k+1} p^k in 𝔽_q[i]. -/
theorem quart_recip_inert (k : Nat) (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (hq : ∀ d, Dv d (4 * k + 1 + 1 + 1) → d = 1 ∨ d = 4 * k + 1 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (hQ : nsm (ring (4 * n + 3 + 1)) (4 * k + 1 + 1 + 1) (ring (4 * n + 3 + 1)).one ≠ (ring (4 * n + 3 + 1)).z  -- (+6 chars; full line in the Zenodo source)
    (Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul (ph4 (ring (4 * k + 1 + 1)) (chi4 n ι (nsm (ring (4 * n + 3 + 1)) (4 * k + 1 + 1 + 1) (ring (4 * n + 3 + 1)).one))) (ph4 (ring (4 * k + 1 + 1)) (chib n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)))
      = (Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul (pw (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) ((Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul ((Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul (ph4 (ring (4 * k + 1 + 1)) (Jq n (chi4 n ι))) (ph4 (ring (4 * k + 1 + 1)) (Jq n (chi4 n ι)))) (ph4 (ring (4 * k + 1 + 1)) (Cyclo4.cyc4 LibInt.intR).one)) (k + 1)) (pw (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (ph4 (ring (4 * k + 1  -- (+24 chars; full line in the Zenodo source)

  have hP2 : pw (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)) (mp4 n (ring (4 * k + 1 + 1)) (Gq n (chi4 n ι))) 2 = gmul (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (mono (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (ph4 (ring (4 * k + 1 + 1)) (Jq n (chi4 n ι)))) (mp4 n (ring (4 * k + 1 + 1)) (Gq n (rho n ι))) := by
    show gmul (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (gmul (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (gone (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)) (mp4 n (ring (4 * k + 1 + 1)) (Gq n (chi4 n ι)))) (mp4 n (ring (4 * k + 1 + 1)) (Gq n (chi4 n ι))) = _
    rw [gm_one, mp4_mul, E2gen n hpr _ (chi4_mul n hpr ι hi) (rho n ι) (fun c => rfl) (rho_zero n ι) (rho_sum n hpr ι hi), ← mp4_mul, mp4_mono]
  have hRR : (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)).mul (mp4 n (ring (4 * k + 1 + 1)) (Gq n (rho n ι))) (mp4 n (ring (4 * k + 1 + 1)) (Gq n (rho n ι))) = (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (ph4 (ring (4 * k + 1 + 1)) (Cyclo4.cyc4 LibInt.intR).one)) (mp4 n (ring  -- (+31 chars; full line in the Zenodo source)
    show gmul (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) _ _ = gmul (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) _ _
    rw [mp4_mul, E4 n hpr ι hi, ← mp4_mul, mp4_mono]
  have hYY : (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)).mul (mp4 n (ring (4 * k + 1 + 1)) (Xq n)) (mp4 n (ring (4 * k + 1 + 1)) (Xq n)) = (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (ph4 (ring (4 * k + 1 + 1)) (Ppq n))) (mp4 n (ring (4 * k + 1 + 1)) (Xq n)) := by
    show gmul (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) _ _ = gmul (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) _ _
    rw [mp4_mul, XXq, ← mp4_mul, mp4_mono]
  have m2 : ∀ s t, (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero s) (mono (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero t) = mono (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero ((Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul s t) :  -- (+1 chars; full line in the Zenodo source)
    fun s t => CubicRecip.mono0_mul2 (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) s t
  have hP4 : pw (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)) (mp4 n (ring (4 * k + 1 + 1)) (Gq n (chi4 n ι))) 4 = (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero ((Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul ((Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul (ph4 (ring (4 * k + 1 + 1)) (Jq n   -- (+165 chars; full line in the Zenodo source)
    rw [(CubicRecip.pw_mul_split (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)) (mp4 n (ring (4 * k + 1 + 1)) (Gq n (chi4 n ι))) 2 2 : pw (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)) (mp4 n (ring (4 * k + 1 + 1)) (Gq n (chi4 n ι))) 4 = _), hP2]
    show (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)).mul ((gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)).mul (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)).one ((gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (ph4 (ring (4 * k + 1 + 1)) (Jq n (chi4 n   -- (+309 chars; full line in the Zenodo source)
    rw [show (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)).mul ((gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)).mul (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)).one ((gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (ph4 (ring (4 * k + 1 + 1)) (Jq n (chi  -- (+309 chars; full line in the Zenodo source)
        = (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)).mul ((gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (ph4 (ring (4 * k + 1 + 1)) (Jq n (chi4 n ι)))) (mono (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (ph4 (ring (4 * k + 1 + 1)) (Jq n (ch  -- (+187 chars; full line in the Zenodo source)
      hRR, m2, show (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero ((Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul (ph4 (ring (4 * k + 1 + 1)) (Jq n (chi4 n ι))) (ph4 (ring (4 * k + 1 + 1)) (Jq n (chi4 n ι))))) ((gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring  -- (+161 chars; full line in the Zenodo source)
        = (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)).mul ((gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero ((Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul (ph4 (ring (4 * k + 1 + 1)) (Jq n (chi4 n ι))) (ph4 (ring (4 * k + 1 + 1)) (Jq n (chi4 n ι))))) (mono (Cyclo4.cyc4 (ring (4 * k +   -- (+168 chars; full line in the Zenodo source)
  have hfv : ∀ x, pw (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (ph4 (ring (4 * k + 1 + 1)) (chi4 n ι x)) (4 * k + 1 + 1 + 1) = ph4 (ring (4 * k + 1 + 1)) (chib n ι x) := fun x => by
    by_cases hx : x = (ring (4 * n + 3 + 1)).zero
    · rw [hx, chi4_zero]
      show (Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul (pw (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (ph4 (ring (4 * k + 1 + 1)) (Cyclo4.cyc4 LibInt.intR).zero) (4 * k + 1 + 1)) (ph4 (ring (4 * k + 1 + 1)) (Cyclo4.cyc4 LibInt.intR).zero) = ph4 (ring (4 * k + 1 + 1)) (chib n ι (ring (4 * n + 3 + 1)).zero)
      rw [ph4_zero, mul_zero, chib_zero]; rfl
    · rw [ph4_pow, show (4 * k + 1 + 1 + 1) = 4 * k + 3 by lib_nat, pow4 (Cyclo4.cyc4 LibInt.intR) _ (chi4_4 n hpr ι hi x hx).1 3 k]
      rw [← ph4_pow, ph4_pow]
      exact congrArg (ph4 (ring (4 * k + 1 + 1))) (chi4_4 n hpr ι hi x hx).2
  have hfrob : pw (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)) (mp4 n (ring (4 * k + 1 + 1)) (Gq n (chi4 n ι))) (4 * k + 1 + 1 + 1) = gmul (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (mono (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (ph4 (ring (4 * k + 1 + 1)) (chi4 n ι (nsm (ring (4 * n + 3 + 1)) (4 * k + 1 + 1 + 1) (ring (4 * n + 3 +   -- (+63 chars; full line in the Zenodo source)
    ext (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) _ _ (fun c => by
      have hd := decomp (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (mp4 n (ring (4 * k + 1 + 1)) (Gq n (chi4 n ι)))
      refine (congrArg (fun u => gt (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (pw (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)) u (4 * k + 1 + 1 + 1)) c) hd).trans ?_
      show gt (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (pw (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)) (sumK (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)) _ (GroupRing.L (4 * n + 3 + 1))) (4 * k + 1 + 1 + 1)) c = _
      rw [frob_sum (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (4 * k + 1) hq (charq4 (ring (4 * k + 1 + 1)) _ (char_p _)), gt_sum, JacobiNorm.mono0_mulg]
      have e2 : sumK (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (fun a => gt (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (pw (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)) (mono (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) a (gt (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (mp4 n (ring (4 * k + 1 + 1)) (Gq n (chi4 n ι))) a)) (4 * k + 1 + 1 + 1)) c) (GroupRing.L (4   -- (+13 chars; full line in the Zenodo source)
          = sumK (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (fun a => (Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul (pw (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (ph4 (ring (4 * k + 1 + 1)) (chi4 n ι a)) (4 * k + 1 + 1 + 1)) (delta (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) c ((ring (4 * n + 3 + 1)).mul (nsm (ring (4 * n + 3 + 1)) (4 * k + 1 + 1 + 1) (ring (4 * n + 3 + 1)).one) a))) (GroupRing.L (4 * n + 3 + 1)) :=
        sumK_ext (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) _ _ (fun a => by
          rw [mono_pow, gt_mono, nsm_eq_mul]
          unfold mp4 Gq; rw [gt_tb, gt_tb]) _
      rw [e2, coef4 n (ring (4 * k + 1 + 1)) hpr _ hQ (fun a => pw (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (ph4 (ring (4 * k + 1 + 1)) (chi4 n ι a)) (4 * k + 1 + 1 + 1)) c, hfv]
      rw [chib_mul n hpr ι hi, show chib n ι (ivq n (nsm (ring (4 * n + 3 + 1)) (4 * k + 1 + 1 + 1) (ring (4 * n + 3 + 1)).one)) = chi4 n ι (nsm (ring (4 * n + 3 + 1)) (4 * k + 1 + 1 + 1) (ring (4 * n + 3 + 1)).one) by unfold chib; rw [ivq_iv n hpr], ph4_mul]
      unfold mp4 Gq; rw [gt_tb, gt_tb])

  have L1 : pw (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)) (mp4 n (ring (4 * k + 1 + 1)) (Gq n (chi4 n ι))) ((4 * k + 1 + 1 + 1) + 1) = gmul (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (mono (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero ((Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul (ph4 (ring (4 * k + 1 + 1)) (chi4 n ι (nsm (ring (4 * n + 3   -- (+197 chars; full line in the Zenodo source)
    show gmul (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (pw (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)) (mp4 n (ring (4 * k + 1 + 1)) (Gq n (chi4 n ι))) (4 * k + 1 + 1 + 1)) (mp4 n (ring (4 * k + 1 + 1)) (Gq n (chi4 n ι))) = _
    rw [hfrob, gm_assoc, mp4_mul, gm_comm (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (Gq n (chib n ι)), E1 n hpr ι hi, ← mp4_mul, mp4_mono, ← gm_assoc]
    exact congrArg (fun u => gmul (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) u (mp4 n (ring (4 * k + 1 + 1)) (Xq n))) (m2 _ _)
  have L2 : pw (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)) (mp4 n (ring (4 * k + 1 + 1)) (Gq n (chi4 n ι))) ((4 * k + 1 + 1 + 1) + 1) = gmul (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (mono (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero ((Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul (pw (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) ((Cyclo4.cyc4 (ring  -- (+355 chars; full line in the Zenodo source)
    rw [show (4 * k + 1 + 1 + 1) + 1 = 4 * (k + 1) by lib_nat, CubicRecip.pw_mul_split, hP4, CubicRecip.pow_SY (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)) _ _ _ hYY k]
    show gmul (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (gmul (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (pw (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)) (mono (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero _) (k + 1)) (pw (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)) (mono (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4  -- (+125 chars; full line in the Zenodo source)
    rw [CubicRecip.mono0_pow, CubicRecip.mono0_pow, CubicRecip.mono0_mul2]
  have key := L1.symm.trans L2
  have k1 := congrArg (fun u => gt (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) u (ring (4 * n + 3 + 1)).one) key
  have k2 := k1
  rw [JacobiNorm.mono0_mulg, JacobiNorm.mono0_mulg] at k2
  have hX1 : gt (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (mp4 n (ring (4 * k + 1 + 1)) (Xq n)) (ring (4 * n + 3 + 1)).one = (Cyclo4.cyc4 (ring (4 * k + 1 + 1))).neg (Cyclo4.cyc4 (ring (4 * k + 1 + 1))).one := by
    unfold mp4; rw [gt_tb, gt_Xq]; unfold delta; rw [ite_neg' _ _ (PowerSum.one_ne_zero' _)]
    rw [ph4_add, ph4_mul, ph4_zero, mul_zero, ph4_neg, ph4_one]; lib_ring
  rw [hX1] at k2
  calc _ = (Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul ((Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul ((Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul (ph4 (ring (4 * k + 1 + 1)) (chi4 n ι (nsm (ring (4 * n + 3 + 1)) (4 * k + 1 + 1 + 1) (ring (4 * n + 3 + 1)).one))) (ph4 (ring (4 * k + 1 + 1)) (chib n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)))) ((Cyclo4.cyc4 (ring (4 * k + 1 + 1))).neg (Cyclo4.c  -- (+132 chars; full line in the Zenodo source)
    _ = _ := by rw [k2]; lib_ring

/-- **Quartic reciprocity at a split q = 4k + 5**: χ̄(q) = (J²)^{k+1} p^{k+1} in 𝔽_q[i]. -/
theorem quart_recip_split (k : Nat) (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (hq : ∀ d, Dv d (4 * k + 3 + 1 + 1) → d = 1 ∨ d = 4 * k + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (hQ : nsm (ring (4 * n + 3 + 1)) (4 * k + 3 + 1 + 1) (ring (4 * n + 3 + 1)).one ≠ (ring (4 * n + 3 + 1)).z  -- (+6 chars; full line in the Zenodo source)
    ph4 (ring (4 * k + 3 + 1)) (chib n ι (nsm (ring (4 * n + 3 + 1)) (4 * k + 3 + 1 + 1) (ring (4 * n + 3 + 1)).one))
      = (Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul (pw (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) ((Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul ((Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul (ph4 (ring (4 * k + 3 + 1)) (Jq n (chi4 n ι))) (ph4 (ring (4 * k + 3 + 1)) (Jq n (chi4 n ι)))) (ph4 (ring (4 * k + 3 + 1)) (Cyclo4.cyc4 LibInt.intR).one)) (k + 1)) (pw (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (ph4 (ring (4 * k + 3  -- (+30 chars; full line in the Zenodo source)

  have hP2 : pw (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)) (mp4 n (ring (4 * k + 3 + 1)) (Gq n (chi4 n ι))) 2 = gmul (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (ph4 (ring (4 * k + 3 + 1)) (Jq n (chi4 n ι)))) (mp4 n (ring (4 * k + 3 + 1)) (Gq n (rho n ι))) := by
    show gmul (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (gmul (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (gone (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)) (mp4 n (ring (4 * k + 3 + 1)) (Gq n (chi4 n ι)))) (mp4 n (ring (4 * k + 3 + 1)) (Gq n (chi4 n ι))) = _
    rw [gm_one, mp4_mul, E2gen n hpr _ (chi4_mul n hpr ι hi) (rho n ι) (fun c => rfl) (rho_zero n ι) (rho_sum n hpr ι hi), ← mp4_mul, mp4_mono]
  have hRR : (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul (mp4 n (ring (4 * k + 3 + 1)) (Gq n (rho n ι))) (mp4 n (ring (4 * k + 3 + 1)) (Gq n (rho n ι))) = (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (ph4 (ring (4 * k + 3 + 1)) (Cyclo4.cyc4 LibInt.intR).one)) (mp4 n (ring  -- (+31 chars; full line in the Zenodo source)
    show gmul (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) _ _ = gmul (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) _ _
    rw [mp4_mul, E4 n hpr ι hi, ← mp4_mul, mp4_mono]
  have hYY : (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul (mp4 n (ring (4 * k + 3 + 1)) (Xq n)) (mp4 n (ring (4 * k + 3 + 1)) (Xq n)) = (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (ph4 (ring (4 * k + 3 + 1)) (Ppq n))) (mp4 n (ring (4 * k + 3 + 1)) (Xq n)) := by
    show gmul (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) _ _ = gmul (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) _ _
    rw [mp4_mul, XXq, ← mp4_mul, mp4_mono]
  have m2 : ∀ s t, (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero s) (mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero t) = mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero ((Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul s t) :  -- (+1 chars; full line in the Zenodo source)
    fun s t => CubicRecip.mono0_mul2 (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) s t
  have hP4 : pw (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)) (mp4 n (ring (4 * k + 3 + 1)) (Gq n (chi4 n ι))) 4 = (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero ((Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul ((Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul (ph4 (ring (4 * k + 3 + 1)) (Jq n   -- (+165 chars; full line in the Zenodo source)
    rw [(CubicRecip.pw_mul_split (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)) (mp4 n (ring (4 * k + 3 + 1)) (Gq n (chi4 n ι))) 2 2 : pw (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)) (mp4 n (ring (4 * k + 3 + 1)) (Gq n (chi4 n ι))) 4 = _), hP2]
    show (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul ((gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).one ((gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (ph4 (ring (4 * k + 3 + 1)) (Jq n (chi4 n   -- (+309 chars; full line in the Zenodo source)
    rw [show (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul ((gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).one ((gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (ph4 (ring (4 * k + 3 + 1)) (Jq n (chi  -- (+309 chars; full line in the Zenodo source)
        = (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul ((gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (ph4 (ring (4 * k + 3 + 1)) (Jq n (chi4 n ι)))) (mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (ph4 (ring (4 * k + 3 + 1)) (Jq n (ch  -- (+187 chars; full line in the Zenodo source)
      hRR, m2, show (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero ((Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul (ph4 (ring (4 * k + 3 + 1)) (Jq n (chi4 n ι))) (ph4 (ring (4 * k + 3 + 1)) (Jq n (chi4 n ι))))) ((gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring  -- (+161 chars; full line in the Zenodo source)
        = (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul ((gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero ((Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul (ph4 (ring (4 * k + 3 + 1)) (Jq n (chi4 n ι))) (ph4 (ring (4 * k + 3 + 1)) (Jq n (chi4 n ι))))) (mono (Cyclo4.cyc4 (ring (4 * k +   -- (+168 chars; full line in the Zenodo source)
  have hfv : ∀ x, pw (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (ph4 (ring (4 * k + 3 + 1)) (chi4 n ι x)) (4 * k + 3 + 1 + 1) = ph4 (ring (4 * k + 3 + 1)) (chi4 n ι x) := fun x => by
    by_cases hx : x = (ring (4 * n + 3 + 1)).zero
    · rw [hx, chi4_zero]
      show (Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul (pw (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (ph4 (ring (4 * k + 3 + 1)) (Cyclo4.cyc4 LibInt.intR).zero) (4 * k + 3 + 1)) (ph4 (ring (4 * k + 3 + 1)) (Cyclo4.cyc4 LibInt.intR).zero) = ph4 (ring (4 * k + 3 + 1)) (Cyclo4.cyc4 LibInt.intR).zero
      rw [ph4_zero, mul_zero]
    · rw [ph4_pow, show (4 * k + 3 + 1 + 1) = 4 * (k + 1) + 1 by lib_nat, pow4 (Cyclo4.cyc4 LibInt.intR) _ (chi4_4 n hpr ι hi x hx).1 1 (k + 1)]
      exact congrArg (ph4 (ring (4 * k + 3 + 1))) (((Cyclo4.cyc4 LibInt.intR)).one_mul _)
  have hfrob : pw (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)) (mp4 n (ring (4 * k + 3 + 1)) (Gq n (chi4 n ι))) (4 * k + 3 + 1 + 1) = gmul (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (ph4 (ring (4 * k + 3 + 1)) (chib n ι (nsm (ring (4 * n + 3 + 1)) (4 * k + 3 + 1 + 1) (ring (4 * n + 3 +   -- (+63 chars; full line in the Zenodo source)
    ext (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) _ _ (fun c => by
      have hd := decomp (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (mp4 n (ring (4 * k + 3 + 1)) (Gq n (chi4 n ι)))
      refine (congrArg (fun u => gt (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (pw (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)) u (4 * k + 3 + 1 + 1)) c) hd).trans ?_
      show gt (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (pw (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)) (sumK (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)) _ (GroupRing.L (4 * n + 3 + 1))) (4 * k + 3 + 1 + 1)) c = _
      rw [frob_sum (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (4 * k + 3) hq (charq4 (ring (4 * k + 3 + 1)) _ (char_p _)), gt_sum, JacobiNorm.mono0_mulg]
      have e2 : sumK (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (fun a => gt (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (pw (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)) (mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) a (gt (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (mp4 n (ring (4 * k + 3 + 1)) (Gq n (chi4 n ι))) a)) (4 * k + 3 + 1 + 1)) c) (GroupRing.L (4   -- (+13 chars; full line in the Zenodo source)
          = sumK (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (fun a => (Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul (pw (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (ph4 (ring (4 * k + 3 + 1)) (chi4 n ι a)) (4 * k + 3 + 1 + 1)) (delta (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) c ((ring (4 * n + 3 + 1)).mul (nsm (ring (4 * n + 3 + 1)) (4 * k + 3 + 1 + 1) (ring (4 * n + 3 + 1)).one) a))) (GroupRing.L (4 * n + 3 + 1)) :=
        sumK_ext (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) _ _ (fun a => by
          rw [mono_pow, gt_mono, nsm_eq_mul]
          unfold mp4 Gq; rw [gt_tb, gt_tb]) _
      rw [e2, coef4 n (ring (4 * k + 3 + 1)) hpr _ hQ (fun a => pw (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (ph4 (ring (4 * k + 3 + 1)) (chi4 n ι a)) (4 * k + 3 + 1 + 1)) c, hfv]
      rw [chi4_mul n hpr ι hi, ph4_mul]
      unfold mp4 Gq chib; rw [gt_tb, gt_tb])

  have hNg : gmul (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (Nq n) (Gq n (chi4 n ι)) = gzero (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) :=
    ext (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) _ _ (fun c => by
      rw [gt_mul]
      unfold gzero; rw [gt_tb]
      have e : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (gt (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (Nq n) a) (gt (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (Gq n (chi4 n ι)) ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a)))) (GroupRing.L (4 * n + 3 + 1))
          = sumK (Cyclo4.cyc4 LibInt.intR) (fun a => chi4 n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a))) (GroupRing.L (4 * n + 3 + 1)) :=
        sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by unfold Nq Gq; rw [gt_tb, gt_tb]; exact (Cyclo4.cyc4 LibInt.intR).one_mul _) _
      rw [e]
      exact (flip (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (chi4 n ι) c).trans (chi4_sum n hpr ι hi))
  have hXg : (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul (mp4 n (ring (4 * k + 3 + 1)) (Xq n)) (mp4 n (ring (4 * k + 3 + 1)) (Gq n (chi4 n ι))) = (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (ph4 (ring (4 * k + 3 + 1)) (Ppq n))) (mp4 n (ring (4 * k + 3 + 1)) (Gq n (chi4 n  -- (+11 chars; full line in the Zenodo source)
    show gmul (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) _ _ = gmul (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) _ _
    have hXgE : gmul (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (Xq n) (Gq n (chi4 n ι)) = gmul (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Ppq n)) (Gq n (chi4 n ι)) := by
      have h' : (gring (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1)).mul (Nq n) (Gq n (chi4 n ι)) = (gring (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1)).zero := hNg
      show (gring (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1)).mul ((gring (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1)).add (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Ppq n)) ((gring (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1)).neg (Nq n))) (Gq n (chi4 n ι)) = (gring (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4   -- (+45 chars; full line in the Zenodo source)
      rw [show (gring (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1)).mul ((gring (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1)).add (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Ppq n)) ((gring (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1)).neg (Nq n))) (Gq n (chi4 n ι))
          = (gring (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1)).add ((gring (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Ppq n)) (Gq n (chi4 n ι))) ((gring (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1)).neg ((gring (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1)).mul (Nq n) (Gq n (chi4 n ι)))) by lib_ring, h']
      lib_ring
    rw [mp4_mul, hXgE, ← mp4_mul, mp4_mono]
  have L2 : pw (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)) (mp4 n (ring (4 * k + 3 + 1)) (Gq n (chi4 n ι))) (4 * k + 3 + 1 + 1) = gmul (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero ((Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul (pw (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) ((Cyclo4.cyc4 (ring (4 *   -- (+366 chars; full line in the Zenodo source)
    rw [show (4 * k + 3 + 1 + 1) = 4 * (k + 1) + 1 by lib_nat]
    show (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul (pw (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)) (mp4 n (ring (4 * k + 3 + 1)) (Gq n (chi4 n ι))) (4 * (k + 1))) (mp4 n (ring (4 * k + 3 + 1)) (Gq n (chi4 n ι))) = _
    rw [CubicRecip.pw_mul_split, hP4, CubicRecip.pow_SY (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)) _ _ _ hYY k]
    rw [show (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul ((gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul ((gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul (pw (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)) (mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero ((Cyclo4.cyc4 (ring (4 * k + 3 + 1))).  -- (+492 chars; full line in the Zenodo source)
        = (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul ((gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul (pw (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)) (mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero ((Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul ((Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul (ph4 (ring (4 * k + 3 +  -- (+507 chars; full line in the Zenodo source)
    show gmul (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (gmul (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (pw (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)) (mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero _) (k + 1)) (pw (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)) (mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4  -- (+321 chars; full line in the Zenodo source)
    rw [CubicRecip.mono0_pow, CubicRecip.mono0_pow, CubicRecip.mono0_mul2, ← gm_assoc, CubicRecip.mono0_mul2]
    rw [show (Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul ((Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul (pw (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) ((Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul ((Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul (ph4 (ring (4 * k + 3 + 1)) (Jq n (chi4 n ι))) (ph4 (ring (4 * k + 3 + 1)) (Jq n (chi4 n ι)))) (ph4 (ring (4 * k + 3 + 1)) (Cyclo4.cyc4 LibInt.intR).one)) (k + 1)) (pw (Cyclo4.cyc  -- (+103 chars; full line in the Zenodo source)
        = (Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul (pw (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) ((Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul ((Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul (ph4 (ring (4 * k + 3 + 1)) (Jq n (chi4 n ι))) (ph4 (ring (4 * k + 3 + 1)) (Jq n (chi4 n ι)))) (ph4 (ring (4 * k + 3 + 1)) (Cyclo4.cyc4 LibInt.intR).one)) (k + 1)) ((Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul (pw (Cyclo4.cyc4 (  -- (+113 chars; full line in the Zenodo source)
    rfl
  have key := hfrob.symm.trans L2
  have k1 := congrArg (fun u => gt (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) u (ring (4 * n + 3 + 1)).one) key
  have k2 := k1
  rw [JacobiNorm.mono0_mulg, JacobiNorm.mono0_mulg] at k2
  have hg1 : gt (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (mp4 n (ring (4 * k + 3 + 1)) (Gq n (chi4 n ι))) (ring (4 * n + 3 + 1)).one = (Cyclo4.cyc4 (ring (4 * k + 3 + 1))).one := by
    unfold mp4 Gq; rw [gt_tb, gt_tb, chi_one n hpr ι hi, ph4_one]
  rw [hg1, (Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul_comm, (Cyclo4.cyc4 (ring (4 * k + 3 + 1))).one_mul, (Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul_comm, (Cyclo4.cyc4 (ring (4 * k + 3 + 1))).one_mul] at k2
  exact k2

end QuartRecip

#print axioms QuartRecip.ph4_add
#print axioms QuartRecip.ph4_mul
#print axioms QuartRecip.ph4_neg
#print axioms QuartRecip.ph4_one
#print axioms QuartRecip.ph4_zero
#print axioms QuartRecip.ph4_sum
#print axioms QuartRecip.ph4_delta
#print axioms QuartRecip.ph4_pow
#print axioms QuartRecip.mp4_mul
#print axioms QuartRecip.mp4_mono
#print axioms QuartRecip.coef4
#print axioms QuartRecip.charq4
#print axioms QuartRecip.chi4_4
#print axioms QuartRecip.pow4
#print axioms QuartRecip.quart_recip_inert
#print axioms QuartRecip.quart_recip_split

lang/QuartSeven1.lean

lean_all/lang/QuartSeven1.lean

/-
  lang/QuartSeven1 — T43: the quartic character of 7 for p ≡ 1 (mod 8) (generated by work/gen_quart_inert.py).

  p = 4N + 5 with N = (2 * m + 1) (p ≡ 1 mod 8, so χ(−1) = 1), χ the quartic character of 𝔽_p, J = J(χ, χ) = a + bi, N(J) = p.
  QuartRecip at the inert prime q = 7 (k = 1) gives χ(q) χ̄(−1) = (J²)^2 p^1 in 𝔽_7[i], and a 49-case table over 𝔽_7 (through the
  builtin Nat.mod, proved equal to ring q pointwise) shows (J²)^2 N(J)^1 = χ(−1) iff a * b ≡ 0 (when N(J) ≢ 0):
      7 is a quartic residue mod p  ⇔  7 | a * b.
    chi_m1             χ(−1) = 1
    table, table'      the table (both directions)
    quart_seven_1, quart_seven_1_conv
  Lean 4 core only, proofs by hand (lib_ring, lib_nat; the table by rfl on closed terms), no axioms.
-/
import lang.QuartFive

namespace QuartSeven1

open FTAChain ZMod DetChain Frobenius QuartChar QuartJacobi QuartRecip LibGcd CubicFive CubicRecip
open LibRing (castZ castN)

theorem lt_wrap : ∀ q a : Nat, a + 1 + q = q → False
  | 0, _, h => Nat.noConfusion h
  | q + 1, a, h => lt_wrap q a (Nat.succ.inj h)

theorem ndvN (N m' q : Nat) (h : N = q * (m' + 1)) (hr : rem (m' + 1) N = 0 → False) : False :=
  hr (by rw [h, show q * (m' + 1) = q * (m' + 1) + 0 from rfl, rem_add_mul]; rfl)

theorem q_prime : ∀ d, Dv d (4 * 1 + 1 + 1 + 1) → d = 1 ∨ d = 4 * 1 + 1 + 1 + 1 := fun d hd =>
  match dv_le d 6 hd, hd with
  | ⟨k, hk⟩, ⟨q', hq⟩ => by
    match d with
    | 0 => exact absurd hq (fun e => Nat.noConfusion e)
    | 1 => exact Or.inl rfl
    | 2 => exact (ndvN 7 1 q' hq (fun e => Nat.noConfusion e)).elim
    | 3 => exact (ndvN 7 2 q' hq (fun e => Nat.noConfusion e)).elim
    | 4 => exact (ndvN 7 3 q' hq (fun e => Nat.noConfusion e)).elim
    | 5 => exact (ndvN 7 4 q' hq (fun e => Nat.noConfusion e)).elim
    | 6 => exact (ndvN 7 5 q' hq (fun e => Nat.noConfusion e)).elim
    | 7 => exact Or.inr rfl
    | d + 8 => exact (lt_wrap 7 (d + k) ((show d + k + 1 + 7 = d + 8 + k by lib_nat).trans hk)).elim

variable (m : Nat)

theorem q_ne : nsm (ring (4 * (2 * m + 1) + 3 + 1)) (4 * 1 + 1 + 1 + 1) (ring (4 * (2 * m + 1) + 3 + 1)).one ≠ (ring (4 * (2 * m + 1) + 3 + 1)).zero := fun e => by
  have h := congrArg Subtype.val e
  rw [nsm_one_val, md_small _ 7 (by rw [show 4 * (2 * m + 1) + 3 + 1 + 1 = 7 + (8 * m + 1) + 1 by lib_nat]; exact Nat.lt_succ_of_le (Nat.le_add_right 7 _))] at h
  exact Nat.noConfusion h

theorem not_dvq (hpr : ∀ d, Dv d (4 * (2 * m + 1) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (2 * m + 1) + 3 + 1 + 1) (h : Dv (4 * 1 + 1 + 1 + 1) (4 * (2 * m + 1) + 3 + 1 + 1)) : False :=
  match hpr 7 h with
  | Or.inl e => Nat.noConfusion (Nat.succ.inj e)
  | Or.inr e => lt_wrap 7 (8 * m + 1) ((show 8 * m + 1 + 1 + 7 = 4 * (2 * m + 1) + 3 + 1 + 1 by lib_nat).trans e.symm)

/-- **χ(−1) = 1** for p ≡ 1 (mod 8). -/
theorem chi_m1 (hpr : ∀ d, Dv d (4 * (2 * m + 1) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (2 * m + 1) + 3 + 1 + 1) (ι : Zp (4 * (2 * m + 1) + 3 + 1)) (hi : (ring (4 * (2 * m + 1) + 3 + 1)).mul ι ι = (ring (4 * (2 * m + 1) + 3 + 1)).neg (ring (4 * (2 * m + 1) + 3 + 1)).one) : chi4 (2 * m + 1) ι ((ring (4 * (2 * m + 1) + 3 + 1)).neg (ring (4 * (2 * m + 1) + 3 + 1)).one) = (1, 0) := by
  have hm : ((ring (4 * (2 * m + 1) + 3 + 1)).neg (ring (4 * (2 * m + 1) + 3 + 1)).one) ≠ (ring (4 * (2 * m + 1) + 3 + 1)).zero := fun e => PowerSum.one_ne_zero' _ (by
    show (ring (4 * (2 * m + 1) + 3 + 1)).one = (ring (4 * (2 * m + 1) + 3 + 1)).zero
    calc (ring (4 * (2 * m + 1) + 3 + 1)).one = (ring (4 * (2 * m + 1) + 3 + 1)).neg ((ring (4 * (2 * m + 1) + 3 + 1)).neg (ring (4 * (2 * m + 1) + 3 + 1)).one) := by lib_ring
      _ = (ring (4 * (2 * m + 1) + 3 + 1)).zero := by rw [e]; lib_ring)
  have h : pw (ring (4 * (2 * m + 1) + 3 + 1)) ((ring (4 * (2 * m + 1) + 3 + 1)).neg (ring (4 * (2 * m + 1) + 3 + 1)).one) ((2 * m + 1) + 1) = pw (ring (4 * (2 * m + 1) + 3 + 1)) ι 0 := by
    rw [show (2 * m + 1) + 1 = 2 * (m + 1) by lib_nat, pw_mul_split, show pw (ring (4 * (2 * m + 1) + 3 + 1)) ((ring (4 * (2 * m + 1) + 3 + 1)).neg (ring (4 * (2 * m + 1) + 3 + 1)).one) 2 = (ring (4 * (2 * m + 1) + 3 + 1)).one by show (ring (4 * (2 * m + 1) + 3 + 1)).mul ((ring (4 * (2 * m + 1) + 3 + 1)).mul (ring (4 * (2 * m + 1) + 3 + 1)).one _) _ = _; lib_ring, ZMod.pw_one_r]; rfl
  exact val4 (2 * m + 1) hpr ι hi _ hm 0 (Nat.zero_lt_succ 3) h

abbrev F := ring (4 * 1 + 1 + 1)
abbrev C := Cyclo4.cyc4 F

def sC : Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1) := (castZ F 1, castZ F (-0))
def nrm (X Y : Zp (4 * 1 + 1 + 1)) : Zp (4 * 1 + 1 + 1) := F.add (F.mul X X) (F.mul Y Y)
def lhs (X Y : Zp (4 * 1 + 1 + 1)) : Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1) :=
  C.mul (pw C (C.mul (C.mul (X, Y) (X, Y)) C.one) 2) (pw C (nrm X Y, F.zero) 1)
def rhs (X Y : Zp (4 * 1 + 1 + 1)) : Zp (4 * 1 + 1 + 1) := F.mul X Y

theorem md_red (a r k : Nat) (h : a = r + k * (4 * 1 + 1 + 1 + 1)) (hr : r < 4 * 1 + 1 + 1 + 1) : md (4 * 1 + 1 + 1) a = r := by
  rw [h, md_add_mul]; exact md_small _ r hr

def fm (a : Nat) : Zp (4 * 1 + 1 + 1) := ⟨a % 7, Nat.mod_lt _ (Nat.zero_lt_succ _)⟩
def fadd (x y : Zp (4 * 1 + 1 + 1)) : Zp (4 * 1 + 1 + 1) := fm (x.val + y.val)
def fmul (x y : Zp (4 * 1 + 1 + 1)) : Zp (4 * 1 + 1 + 1) := fm (x.val * y.val)
def fneg (x : Zp (4 * 1 + 1 + 1)) : Zp (4 * 1 + 1 + 1) := fm ((4 * 1 + 1 + 1) * x.val)

theorem add_nat : ∀ x y : Nat, x < 7 → y < 7 → md (4 * 1 + 1 + 1) (x + y) = (x + y) % 7
  | 0, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 0, 1, _, _ => md_red _ 1 0 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 0, 2, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 0, 3, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 0, 4, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 0, 5, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 0, 6, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 1, 0, _, _ => md_red _ 1 0 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 1, 1, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 1, 2, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 1, 3, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 1, 4, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 1, 5, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 1, 6, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 2, 0, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 2, 1, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 2, 2, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 2, 3, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 2, 4, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 2, 5, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 2, 6, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 3, 0, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 3, 1, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 3, 2, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 3, 3, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 3, 4, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 3, 5, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 3, 6, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 4, 0, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 4, 1, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 4, 2, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 4, 3, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 4, 4, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 4, 5, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 4, 6, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 5, 0, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 5, 1, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 5, 2, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 5, 3, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 5, 4, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 5, 5, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 5, 6, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 6, 0, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 6, 1, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 6, 2, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 6, 3, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 6, 4, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 6, 5, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 6, 6, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | k + 7, _, h, _ => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))
  | _, k + 7, _, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem mul_nat : ∀ x y : Nat, x < 7 → y < 7 → md (4 * 1 + 1 + 1) (x * y) = (x * y) % 7
  | 0, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 0, 1, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 0, 2, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 0, 3, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 0, 4, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 0, 5, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 0, 6, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 1, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 1, 1, _, _ => md_red _ 1 0 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 1, 2, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 1, 3, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 1, 4, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 1, 5, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 1, 6, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 2, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 2, 1, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 2, 2, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 2, 3, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 2, 4, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 2, 5, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 2, 6, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 3, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 3, 1, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 3, 2, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 3, 3, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 3, 4, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 3, 5, _, _ => md_red _ 1 2 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 3, 6, _, _ => md_red _ 4 2 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 4, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 4, 1, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 4, 2, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 4, 3, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 4, 4, _, _ => md_red _ 2 2 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 4, 5, _, _ => md_red _ 6 2 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 4, 6, _, _ => md_red _ 3 3 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 5, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 5, 1, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 5, 2, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 5, 3, _, _ => md_red _ 1 2 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 5, 4, _, _ => md_red _ 6 2 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 5, 5, _, _ => md_red _ 4 3 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 5, 6, _, _ => md_red _ 2 4 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 6, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 6, 1, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 6, 2, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 6, 3, _, _ => md_red _ 4 2 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 6, 4, _, _ => md_red _ 3 3 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 6, 5, _, _ => md_red _ 2 4 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 6, 6, _, _ => md_red _ 1 5 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | k + 7, _, h, _ => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))
  | _, k + 7, _, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem neg_nat : ∀ x : Nat, x < 7 → md (4 * 1 + 1 + 1) ((4 * 1 + 1 + 1) * x) = ((4 * 1 + 1 + 1) * x) % 7
  | 0, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 1, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 2, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 3, _ => md_red _ 4 2 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 4, _ => md_red _ 3 3 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 5, _ => md_red _ 2 4 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 6, _ => md_red _ 1 5 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | k + 7, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem addE (x y : Zp (4 * 1 + 1 + 1)) : F.add x y = fadd x y := Subtype.ext (add_nat x.val y.val x.2 y.2)
theorem mulE (x y : Zp (4 * 1 + 1 + 1)) : F.mul x y = fmul x y := Subtype.ext (mul_nat x.val y.val x.2 y.2)
theorem negE (x : Zp (4 * 1 + 1 + 1)) : F.neg x = fneg x := Subtype.ext (by show md (4 * 1 + 1 + 1) ((4 * 1 + 1 + 1) * x.val) = ((4 * 1 + 1 + 1) * x.val) % 7; exact neg_nat x.val x.2)

def c4f (u v : Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1)) : Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1) :=
  (fadd (fmul u.1 v.1) (fneg (fmul u.2 v.2)), fadd (fmul u.1 v.2) (fmul u.2 v.1))
theorem c4E (u v : Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1)) : C.mul u v = c4f u v := by
  show Cyclo4.c4mul F u v = _
  unfold Cyclo4.c4mul
  rw [mulE, mulE, mulE, mulE, negE, addE, addE]; rfl
def pwf (z : Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1)) : Nat → Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1)
  | 0 => C.one
  | k + 1 => c4f (pwf z k) z
theorem pwE (z : Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1)) : ∀ k, pw C z k = pwf z k
  | 0 => rfl
  | k + 1 => by show C.mul (pw C z k) z = _; rw [c4E, pwE z k]; rfl

def nrmf (X Y : Zp (4 * 1 + 1 + 1)) : Zp (4 * 1 + 1 + 1) := fadd (fmul X X) (fmul Y Y)
def lhsf (X Y : Zp (4 * 1 + 1 + 1)) : Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1) := c4f (pwf (c4f (c4f (X, Y) (X, Y)) C.one) 2) (pwf (nrmf X Y, F.zero) 1)
def rhsf (X Y : Zp (4 * 1 + 1 + 1)) : Zp (4 * 1 + 1 + 1) := fmul X Y

theorem nrmE (X Y : Zp (4 * 1 + 1 + 1)) : nrm X Y = nrmf X Y := by
  unfold nrm nrmf; rw [mulE, mulE, addE]
theorem lhsE (X Y : Zp (4 * 1 + 1 + 1)) : lhs X Y = lhsf X Y := by
  unfold lhs lhsf; rw [nrmE, c4E, c4E, pwE, pwE, c4E]
theorem rhsE (X Y : Zp (4 * 1 + 1 + 1)) : rhs X Y = rhsf X Y := by
  unfold rhs rhsf
  repeat (first | rw [mulE] | rw [addE] | rw [negE])
  try rfl

def code (z : Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1)) : Nat := z.1.val + 7 * z.2.val
def chk (X Y : Zp (4 * 1 + 1 + 1)) : Bool := (!(Nat.beq (code (lhsf X Y)) 1) || Nat.beq (rhsf X Y).val 0) &&
  (!(Nat.beq (rhsf X Y).val 0) || Nat.beq (nrmf X Y).val 0 || Nat.beq (code (lhsf X Y)) 1)

theorem row0 : ∀ y : Nat, y < 7 → chk (mk (4 * 1 + 1 + 1) 0) (mk (4 * 1 + 1 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | k + 7, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem row1 : ∀ y : Nat, y < 7 → chk (mk (4 * 1 + 1 + 1) 1) (mk (4 * 1 + 1 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | k + 7, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem row2 : ∀ y : Nat, y < 7 → chk (mk (4 * 1 + 1 + 1) 2) (mk (4 * 1 + 1 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | k + 7, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem row3 : ∀ y : Nat, y < 7 → chk (mk (4 * 1 + 1 + 1) 3) (mk (4 * 1 + 1 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | k + 7, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem row4 : ∀ y : Nat, y < 7 → chk (mk (4 * 1 + 1 + 1) 4) (mk (4 * 1 + 1 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | k + 7, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem row5 : ∀ y : Nat, y < 7 → chk (mk (4 * 1 + 1 + 1) 5) (mk (4 * 1 + 1 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | k + 7, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem row6 : ∀ y : Nat, y < 7 → chk (mk (4 * 1 + 1 + 1) 6) (mk (4 * 1 + 1 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | k + 7, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem chk_nat : ∀ x y : Nat, x < 7 → y < 7 → chk (mk (4 * 1 + 1 + 1) x) (mk (4 * 1 + 1 + 1) y) = true
  | 0, y, _, h => row0 y h
  | 1, y, _, h => row1 y h
  | 2, y, _, h => row2 y h
  | 3, y, _, h => row3 y h
  | 4, y, _, h => row4 y h
  | 5, y, _, h => row5 y h
  | 6, y, _, h => row6 y h
  | k + 7, _, h, _ => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem chk_all (X Y : Zp (4 * 1 + 1 + 1)) : chk X Y = true := by
  rw [show X = mk (4 * 1 + 1 + 1) X.val from Subtype.ext (md_small _ _ X.2).symm, show Y = mk (4 * 1 + 1 + 1) Y.val from Subtype.ext (md_small _ _ Y.2).symm]
  exact chk_nat X.val Y.val X.2 Y.2

theorem ne_of_beq (a b : Nat) (h : Nat.beq a b = false) (e : a = b) : False := by
  rw [e, beq_self] at h; exact Bool.noConfusion h

theorem code_eq (z : Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1)) (h : code z = 1) : z = sC := by
  match z, h with
  | (u, v), h =>
    have e' : u.val + 7 * v.val = 1 := h
    have hv : v.val = 0 := by
      cases hv' : v.val with
      | zero => rfl
      | succ t =>
        rw [hv'] at e'
        exact (lt_wrap 7 (u.val + 7 * t + 5) ((show u.val + 7 * t + 5 + 1 + 7 = u.val + 7 * (t + 1) + 6 by lib_nat).trans
          (by rw [show u.val + 7 * (t + 1) + 6 = (u.val + 7 * (t + 1)) + 6 from rfl, e']))).elim
    have hu : u.val = 1 := by rw [hv] at e'; exact e'
    exact Prod.ext (Subtype.ext hu) (Subtype.ext hv)

theorem table (X Y : Zp (4 * 1 + 1 + 1)) (h : lhs X Y = sC) : rhs X Y = F.zero := by
  have hc := band_l _ _ (chk_all X Y)
  rw [← lhsE, h] at hc
  rw [rhsE]
  exact (Subtype.ext (beq_eq _ _ hc) : rhsf X Y = ⟨0, Nat.zero_lt_succ _⟩)

theorem table' (X Y : Zp (4 * 1 + 1 + 1)) (h : rhs X Y = F.zero) (hn : nrm X Y ≠ F.zero) : lhs X Y = sC := by
  have hc := band_r _ _ (chk_all X Y)
  rw [← rhsE, h] at hc
  rw [lhsE]
  rw [nrmE] at hn
  cases hb : Nat.beq (nrmf X Y).val 0 with
  | true => exact absurd (Subtype.ext (beq_eq _ _ hb) : nrmf X Y = F.zero) hn
  | false =>
    rw [hb] at hc
    exact code_eq _ (beq_eq _ _ hc)

theorem cast_norm (x y : Int) : castZ F (x * x + y * y) = nrm (castZ F x) (castZ F y) := by
  rw [IntCast.castZ_add, HasseDavenport.castZ_mul, HasseDavenport.castZ_mul]; rfl

theorem cast_rhs (x y : Int) : castZ F (x * y) = rhs (castZ F x) (castZ F y) := by
  rw [show x * y = x * y from rfl]
  repeat (first | rw [HasseDavenport.castZ_mul] | rw [IntCast.castZ_add] | rw [HasseDavenport.castZ_neg])
  rfl

theorem hP (hpr : ∀ d, Dv d (4 * (2 * m + 1) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (2 * m + 1) + 3 + 1 + 1) (ι : Zp (4 * (2 * m + 1) + 3 + 1)) (hi : (ring (4 * (2 * m + 1) + 3 + 1)).mul ι ι = (ring (4 * (2 * m + 1) + 3 + 1)).neg (ring (4 * (2 * m + 1) + 3 + 1)).one) :
    ph4 F (Ppq (2 * m + 1)) = (nrm (castZ F (Jq (2 * m + 1) (chi4 (2 * m + 1) ι)).1) (castZ F (Jq (2 * m + 1) (chi4 (2 * m + 1) ι)).2), F.zero) := by
  rw [show Ppq (2 * m + 1) = (Int.ofNat (4 * (2 * m + 1) + 3 + 1 + 1), 0) from sum_oneq (2 * m + 1) _, ← quart_norm (2 * m + 1) hpr ι hi]; unfold ph4; rw [cast_norm]; rfl

theorem chi4_cases (ι x : Zp (4 * (2 * m + 1) + 3 + 1)) : chi4 (2 * m + 1) ι x = (0, 0) ∨ chi4 (2 * m + 1) ι x = (1, 0) ∨ chi4 (2 * m + 1) ι x = (0, 1) ∨ chi4 (2 * m + 1) ι x = (-1, 0) ∨ chi4 (2 * m + 1) ι x = (0, -1) := by
  unfold chi4
  by_cases h1 : x = (ring (4 * (2 * m + 1) + 3 + 1)).zero
  · exact Or.inl (ite_t _ _ h1)
  · rw [ite_f _ _ h1]
    by_cases h2 : pw (ring (4 * (2 * m + 1) + 3 + 1)) x ((2 * m + 1) + 1) = (ring (4 * (2 * m + 1) + 3 + 1)).one
    · exact Or.inr (Or.inl (ite_t _ _ h2))
    · rw [ite_f _ _ h2]
      by_cases h3 : pw (ring (4 * (2 * m + 1) + 3 + 1)) x ((2 * m + 1) + 1) = ι
      · exact Or.inr (Or.inr (Or.inl (ite_t _ _ h3)))
      · rw [ite_f _ _ h3]
        by_cases h4 : pw (ring (4 * (2 * m + 1) + 3 + 1)) x ((2 * m + 1) + 1) = (ring (4 * (2 * m + 1) + 3 + 1)).neg (ring (4 * (2 * m + 1) + 3 + 1)).one
        · exact Or.inr (Or.inr (Or.inr (Or.inl (ite_t _ _ h4))))
        · exact Or.inr (Or.inr (Or.inr (Or.inr (ite_f _ _ h4))))

theorem recip (hpr : ∀ d, Dv d (4 * (2 * m + 1) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (2 * m + 1) + 3 + 1 + 1) (ι : Zp (4 * (2 * m + 1) + 3 + 1)) (hi : (ring (4 * (2 * m + 1) + 3 + 1)).mul ι ι = (ring (4 * (2 * m + 1) + 3 + 1)).neg (ring (4 * (2 * m + 1) + 3 + 1)).one) :
    C.mul (ph4 F (chi4 (2 * m + 1) ι (nsm (ring (4 * (2 * m + 1) + 3 + 1)) (4 * 1 + 1 + 1 + 1) (ring (4 * (2 * m + 1) + 3 + 1)).one))) sC = lhs (castZ F (Jq (2 * m + 1) (chi4 (2 * m + 1) ι)).1) (castZ F (Jq (2 * m + 1) (chi4 (2 * m + 1) ι)).2) := by
  have hc := quart_recip_inert (2 * m + 1) 1 hpr q_prime ι hi (q_ne m)
  rw [show chib (2 * m + 1) ι ((ring (4 * (2 * m + 1) + 3 + 1)).neg (ring (4 * (2 * m + 1) + 3 + 1)).one) = gconj (chi4 (2 * m + 1) ι ((ring (4 * (2 * m + 1) + 3 + 1)).neg (ring (4 * (2 * m + 1) + 3 + 1)).one)) from chi_ivq (2 * m + 1) hpr ι hi _, chi_m1 m hpr ι hi, hP m hpr ι hi, ph4_one] at hc
  exact hc

/-- **7 a quartic residue mod p ⇒ 7 | a * b**. -/
theorem quart_seven_1 (hpr : ∀ d, Dv d (4 * (2 * m + 1) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (2 * m + 1) + 3 + 1 + 1) (ι : Zp (4 * (2 * m + 1) + 3 + 1)) (hi : (ring (4 * (2 * m + 1) + 3 + 1)).mul ι ι = (ring (4 * (2 * m + 1) + 3 + 1)).neg (ring (4 * (2 * m + 1) + 3 + 1)).one)
    (hq : chi4 (2 * m + 1) ι (nsm (ring (4 * (2 * m + 1) + 3 + 1)) (4 * 1 + 1 + 1 + 1) (ring (4 * (2 * m + 1) + 3 + 1)).one) = (1, 0)) : IDv (Int.ofNat 7) ((Jq (2 * m + 1) (chi4 (2 * m + 1) ι)).1 * (Jq (2 * m + 1) (chi4 (2 * m + 1) ι)).2) := by
  have hc := recip m hpr ι hi
  rw [hq] at hc
  have hl : lhs (castZ F (Jq (2 * m + 1) (chi4 (2 * m + 1) ι)).1) (castZ F (Jq (2 * m + 1) (chi4 (2 * m + 1) ι)).2) = sC := hc.symm.trans (by rw [c4E]; rfl)
  exact CubicTwo.cast_zero_gen (4 * 1 + 1 + 1) _ ((cast_rhs _ _).trans (table _ _ hl))

/-- **7 | a * b ⇒ 7 is a quartic residue mod p.** -/
theorem quart_seven_1_conv (hpr : ∀ d, Dv d (4 * (2 * m + 1) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (2 * m + 1) + 3 + 1 + 1) (ι : Zp (4 * (2 * m + 1) + 3 + 1)) (hi : (ring (4 * (2 * m + 1) + 3 + 1)).mul ι ι = (ring (4 * (2 * m + 1) + 3 + 1)).neg (ring (4 * (2 * m + 1) + 3 + 1)).one)
    (hd : IDv (Int.ofNat 7) ((Jq (2 * m + 1) (chi4 (2 * m + 1) ι)).1 * (Jq (2 * m + 1) (chi4 (2 * m + 1) ι)).2)) : chi4 (2 * m + 1) ι (nsm (ring (4 * (2 * m + 1) + 3 + 1)) (4 * 1 + 1 + 1 + 1) (ring (4 * (2 * m + 1) + 3 + 1)).one) = (1, 0) := by
  have hc := recip m hpr ι hi
  have hr : rhs (castZ F (Jq (2 * m + 1) (chi4 (2 * m + 1) ι)).1) (castZ F (Jq (2 * m + 1) (chi4 (2 * m + 1) ι)).2) = F.zero := by
    rw [← cast_rhs]
    match hd with
    | ⟨c, hc'⟩ =>
      rw [hc', HasseDavenport.castZ_mul]
      have h0 : castZ F (Int.ofNat 7) = F.zero := CubeAp.castP 5
      rw [h0, zero_mul']
  have hn : nrm (castZ F (Jq (2 * m + 1) (chi4 (2 * m + 1) ι)).1) (castZ F (Jq (2 * m + 1) (chi4 (2 * m + 1) ι)).2) ≠ F.zero := fun e => by
    have e1 : castZ F (Int.ofNat (4 * (2 * m + 1) + 3 + 1 + 1)) = F.zero := by rw [← quart_norm (2 * m + 1) hpr ι hi, cast_norm]; exact e
    have e2 : mk (4 * 1 + 1 + 1) (4 * (2 * m + 1) + 3 + 1 + 1) = F.zero := (IntCast.castN_mk _ _).symm.trans e1
    have hdq : ∀ P' : Nat, mk (4 * 1 + 1 + 1) P' = F.zero → Dv (4 * 1 + 1 + 1 + 1) P' := fun P' h => md_zero_dv (4 * 1 + 1 + 1) P' (congrArg Subtype.val h)
    exact not_dvq m hpr (hdq _ e2)
  have h1 := hc.trans (table' _ _ hr hn)
  rcases chi4_cases m ι (nsm (ring (4 * (2 * m + 1) + 3 + 1)) (4 * 1 + 1 + 1 + 1) (ring (4 * (2 * m + 1) + 3 + 1)).one) with e | e | e | e | e
  · rw [e, c4E] at h1; exact absurd (congrArg code h1) (fun e' => Nat.noConfusion e')
  · exact e
  · rw [e, c4E] at h1; exact absurd (congrArg code h1) (ne_of_beq _ _ rfl)
  · rw [e, c4E] at h1; exact absurd (congrArg code h1) (ne_of_beq _ _ rfl)
  · rw [e, c4E] at h1; exact absurd (congrArg code h1) (ne_of_beq _ _ rfl)

end QuartSeven1
#print axioms QuartSeven1.lt_wrap
#print axioms QuartSeven1.ndvN
#print axioms QuartSeven1.q_prime
#print axioms QuartSeven1.q_ne
#print axioms QuartSeven1.not_dvq
#print axioms QuartSeven1.chi_m1
#print axioms QuartSeven1.md_red
#print axioms QuartSeven1.add_nat
#print axioms QuartSeven1.mul_nat
#print axioms QuartSeven1.neg_nat
#print axioms QuartSeven1.addE
#print axioms QuartSeven1.mulE
#print axioms QuartSeven1.negE
#print axioms QuartSeven1.c4E
#print axioms QuartSeven1.pwE
#print axioms QuartSeven1.nrmE
#print axioms QuartSeven1.lhsE
#print axioms QuartSeven1.rhsE
#print axioms QuartSeven1.row0
#print axioms QuartSeven1.row1
#print axioms QuartSeven1.row2
#print axioms QuartSeven1.row3
#print axioms QuartSeven1.row4
#print axioms QuartSeven1.row5
#print axioms QuartSeven1.row6
#print axioms QuartSeven1.chk_nat
#print axioms QuartSeven1.chk_all
#print axioms QuartSeven1.ne_of_beq
#print axioms QuartSeven1.code_eq
#print axioms QuartSeven1.table
#print axioms QuartSeven1.table'
#print axioms QuartSeven1.cast_norm
#print axioms QuartSeven1.cast_rhs
#print axioms QuartSeven1.hP
#print axioms QuartSeven1.chi4_cases
#print axioms QuartSeven1.recip
#print axioms QuartSeven1.quart_seven_1
#print axioms QuartSeven1.quart_seven_1_conv

lang/QuartSeven5.lean

lean_all/lang/QuartSeven5.lean

/-
  lang/QuartSeven5 — T43: the quartic character of 7 for p ≡ 5 (mod 8) (generated by work/gen_quart_inert.py).

  p = 4N + 5 with N = (2 * m + 2) (p ≡ 5 mod 8, so χ(−1) = -1), χ the quartic character of 𝔽_p, J = J(χ, χ) = a + bi, N(J) = p.
  QuartRecip at the inert prime q = 7 (k = 1) gives χ(q) χ̄(−1) = (J²)^2 p^1 in 𝔽_7[i], and a 49-case table over 𝔽_7 (through the
  builtin Nat.mod, proved equal to ring q pointwise) shows (J²)^2 N(J)^1 = χ(−1) iff a * a - b * b ≡ 0 (when N(J) ≢ 0):
      7 is a quartic residue mod p  ⇔  7 | a * a - b * b.
    chi_m1             χ(−1) = -1
    table, table'      the table (both directions)
    quart_seven_5, quart_seven_5_conv
  Lean 4 core only, proofs by hand (lib_ring, lib_nat; the table by rfl on closed terms), no axioms.
-/
import lang.QuartFive

namespace QuartSeven5

open FTAChain ZMod DetChain Frobenius QuartChar QuartJacobi QuartRecip LibGcd CubicFive CubicRecip
open LibRing (castZ castN)

theorem lt_wrap : ∀ q a : Nat, a + 1 + q = q → False
  | 0, _, h => Nat.noConfusion h
  | q + 1, a, h => lt_wrap q a (Nat.succ.inj h)

theorem ndvN (N m' q : Nat) (h : N = q * (m' + 1)) (hr : rem (m' + 1) N = 0 → False) : False :=
  hr (by rw [h, show q * (m' + 1) = q * (m' + 1) + 0 from rfl, rem_add_mul]; rfl)

theorem q_prime : ∀ d, Dv d (4 * 1 + 1 + 1 + 1) → d = 1 ∨ d = 4 * 1 + 1 + 1 + 1 := fun d hd =>
  match dv_le d 6 hd, hd with
  | ⟨k, hk⟩, ⟨q', hq⟩ => by
    match d with
    | 0 => exact absurd hq (fun e => Nat.noConfusion e)
    | 1 => exact Or.inl rfl
    | 2 => exact (ndvN 7 1 q' hq (fun e => Nat.noConfusion e)).elim
    | 3 => exact (ndvN 7 2 q' hq (fun e => Nat.noConfusion e)).elim
    | 4 => exact (ndvN 7 3 q' hq (fun e => Nat.noConfusion e)).elim
    | 5 => exact (ndvN 7 4 q' hq (fun e => Nat.noConfusion e)).elim
    | 6 => exact (ndvN 7 5 q' hq (fun e => Nat.noConfusion e)).elim
    | 7 => exact Or.inr rfl
    | d + 8 => exact (lt_wrap 7 (d + k) ((show d + k + 1 + 7 = d + 8 + k by lib_nat).trans hk)).elim

variable (m : Nat)

theorem q_ne : nsm (ring (4 * (2 * m + 2) + 3 + 1)) (4 * 1 + 1 + 1 + 1) (ring (4 * (2 * m + 2) + 3 + 1)).one ≠ (ring (4 * (2 * m + 2) + 3 + 1)).zero := fun e => by
  have h := congrArg Subtype.val e
  rw [nsm_one_val, md_small _ 7 (by rw [show 4 * (2 * m + 2) + 3 + 1 + 1 = 7 + (8 * m + 5) + 1 by lib_nat]; exact Nat.lt_succ_of_le (Nat.le_add_right 7 _))] at h
  exact Nat.noConfusion h

theorem not_dvq (hpr : ∀ d, Dv d (4 * (2 * m + 2) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (2 * m + 2) + 3 + 1 + 1) (h : Dv (4 * 1 + 1 + 1 + 1) (4 * (2 * m + 2) + 3 + 1 + 1)) : False :=
  match hpr 7 h with
  | Or.inl e => Nat.noConfusion (Nat.succ.inj e)
  | Or.inr e => lt_wrap 7 (8 * m + 5) ((show 8 * m + 5 + 1 + 7 = 4 * (2 * m + 2) + 3 + 1 + 1 by lib_nat).trans e.symm)

/-- **χ(−1) = -1** for p ≡ 5 (mod 8). -/
theorem chi_m1 (hpr : ∀ d, Dv d (4 * (2 * m + 2) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (2 * m + 2) + 3 + 1 + 1) (ι : Zp (4 * (2 * m + 2) + 3 + 1)) (hi : (ring (4 * (2 * m + 2) + 3 + 1)).mul ι ι = (ring (4 * (2 * m + 2) + 3 + 1)).neg (ring (4 * (2 * m + 2) + 3 + 1)).one) : chi4 (2 * m + 2) ι ((ring (4 * (2 * m + 2) + 3 + 1)).neg (ring (4 * (2 * m + 2) + 3 + 1)).one) = (-1, 0) := by
  have hm : ((ring (4 * (2 * m + 2) + 3 + 1)).neg (ring (4 * (2 * m + 2) + 3 + 1)).one) ≠ (ring (4 * (2 * m + 2) + 3 + 1)).zero := fun e => PowerSum.one_ne_zero' _ (by
    show (ring (4 * (2 * m + 2) + 3 + 1)).one = (ring (4 * (2 * m + 2) + 3 + 1)).zero
    calc (ring (4 * (2 * m + 2) + 3 + 1)).one = (ring (4 * (2 * m + 2) + 3 + 1)).neg ((ring (4 * (2 * m + 2) + 3 + 1)).neg (ring (4 * (2 * m + 2) + 3 + 1)).one) := by lib_ring
      _ = (ring (4 * (2 * m + 2) + 3 + 1)).zero := by rw [e]; lib_ring)
  have h : pw (ring (4 * (2 * m + 2) + 3 + 1)) ((ring (4 * (2 * m + 2) + 3 + 1)).neg (ring (4 * (2 * m + 2) + 3 + 1)).one) ((2 * m + 2) + 1) = pw (ring (4 * (2 * m + 2) + 3 + 1)) ι 2 := by
    rw [show (2 * m + 2) + 1 = 2 * (m + 1) + 1 by lib_nat]
    show (ring (4 * (2 * m + 2) + 3 + 1)).mul (pw (ring (4 * (2 * m + 2) + 3 + 1)) ((ring (4 * (2 * m + 2) + 3 + 1)).neg (ring (4 * (2 * m + 2) + 3 + 1)).one) (2 * (m + 1))) ((ring (4 * (2 * m + 2) + 3 + 1)).neg (ring (4 * (2 * m + 2) + 3 + 1)).one) = (ring (4 * (2 * m + 2) + 3 + 1)).mul ((ring (4 * (2 * m + 2) + 3 + 1)).mul (ring (4 * (2 * m + 2) + 3 + 1)).one ι) ι
    rw [pw_mul_split, show pw (ring (4 * (2 * m + 2) + 3 + 1)) ((ring (4 * (2 * m + 2) + 3 + 1)).neg (ring (4 * (2 * m + 2) + 3 + 1)).one) 2 = (ring (4 * (2 * m + 2) + 3 + 1)).one by show (ring (4 * (2 * m + 2) + 3 + 1)).mul ((ring (4 * (2 * m + 2) + 3 + 1)).mul (ring (4 * (2 * m + 2) + 3 + 1)).one _) _ = _; lib_ring, ZMod.pw_one_r]
    rw [show (ring (4 * (2 * m + 2) + 3 + 1)).mul ((ring (4 * (2 * m + 2) + 3 + 1)).mul (ring (4 * (2 * m + 2) + 3 + 1)).one ι) ι = (ring (4 * (2 * m + 2) + 3 + 1)).mul ι ι by lib_ring, hi]; lib_ring
  exact val4 (2 * m + 2) hpr ι hi _ hm 2 (Nat.lt_succ_of_le (Nat.le_succ 2)) h

abbrev F := ring (4 * 1 + 1 + 1)
abbrev C := Cyclo4.cyc4 F

def sC : Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1) := (castZ F (-1), castZ F (-0))
def nrm (X Y : Zp (4 * 1 + 1 + 1)) : Zp (4 * 1 + 1 + 1) := F.add (F.mul X X) (F.mul Y Y)
def lhs (X Y : Zp (4 * 1 + 1 + 1)) : Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1) :=
  C.mul (pw C (C.mul (C.mul (X, Y) (X, Y)) C.one) 2) (pw C (nrm X Y, F.zero) 1)
def rhs (X Y : Zp (4 * 1 + 1 + 1)) : Zp (4 * 1 + 1 + 1) := F.add (F.mul X X) (F.neg (F.mul Y Y))

theorem md_red (a r k : Nat) (h : a = r + k * (4 * 1 + 1 + 1 + 1)) (hr : r < 4 * 1 + 1 + 1 + 1) : md (4 * 1 + 1 + 1) a = r := by
  rw [h, md_add_mul]; exact md_small _ r hr

def fm (a : Nat) : Zp (4 * 1 + 1 + 1) := ⟨a % 7, Nat.mod_lt _ (Nat.zero_lt_succ _)⟩
def fadd (x y : Zp (4 * 1 + 1 + 1)) : Zp (4 * 1 + 1 + 1) := fm (x.val + y.val)
def fmul (x y : Zp (4 * 1 + 1 + 1)) : Zp (4 * 1 + 1 + 1) := fm (x.val * y.val)
def fneg (x : Zp (4 * 1 + 1 + 1)) : Zp (4 * 1 + 1 + 1) := fm ((4 * 1 + 1 + 1) * x.val)

theorem add_nat : ∀ x y : Nat, x < 7 → y < 7 → md (4 * 1 + 1 + 1) (x + y) = (x + y) % 7
  | 0, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 0, 1, _, _ => md_red _ 1 0 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 0, 2, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 0, 3, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 0, 4, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 0, 5, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 0, 6, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 1, 0, _, _ => md_red _ 1 0 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 1, 1, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 1, 2, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 1, 3, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 1, 4, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 1, 5, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 1, 6, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 2, 0, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 2, 1, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 2, 2, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 2, 3, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 2, 4, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 2, 5, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 2, 6, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 3, 0, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 3, 1, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 3, 2, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 3, 3, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 3, 4, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 3, 5, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 3, 6, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 4, 0, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 4, 1, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 4, 2, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 4, 3, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 4, 4, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 4, 5, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 4, 6, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 5, 0, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 5, 1, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 5, 2, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 5, 3, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 5, 4, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 5, 5, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 5, 6, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 6, 0, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 6, 1, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 6, 2, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 6, 3, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 6, 4, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 6, 5, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 6, 6, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | k + 7, _, h, _ => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))
  | _, k + 7, _, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem mul_nat : ∀ x y : Nat, x < 7 → y < 7 → md (4 * 1 + 1 + 1) (x * y) = (x * y) % 7
  | 0, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 0, 1, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 0, 2, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 0, 3, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 0, 4, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 0, 5, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 0, 6, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 1, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 1, 1, _, _ => md_red _ 1 0 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 1, 2, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 1, 3, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 1, 4, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 1, 5, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 1, 6, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 2, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 2, 1, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 2, 2, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 2, 3, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 2, 4, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 2, 5, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 2, 6, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 3, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 3, 1, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 3, 2, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 3, 3, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 3, 4, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 3, 5, _, _ => md_red _ 1 2 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 3, 6, _, _ => md_red _ 4 2 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 4, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 4, 1, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 4, 2, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 4, 3, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 4, 4, _, _ => md_red _ 2 2 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 4, 5, _, _ => md_red _ 6 2 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 4, 6, _, _ => md_red _ 3 3 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 5, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 5, 1, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 5, 2, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 5, 3, _, _ => md_red _ 1 2 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 5, 4, _, _ => md_red _ 6 2 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 5, 5, _, _ => md_red _ 4 3 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 5, 6, _, _ => md_red _ 2 4 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 6, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 6, 1, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 6, 2, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 6, 3, _, _ => md_red _ 4 2 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 6, 4, _, _ => md_red _ 3 3 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 6, 5, _, _ => md_red _ 2 4 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 6, 6, _, _ => md_red _ 1 5 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | k + 7, _, h, _ => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))
  | _, k + 7, _, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem neg_nat : ∀ x : Nat, x < 7 → md (4 * 1 + 1 + 1) ((4 * 1 + 1 + 1) * x) = ((4 * 1 + 1 + 1) * x) % 7
  | 0, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 1, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 2, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 3, _ => md_red _ 4 2 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 4, _ => md_red _ 3 3 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 5, _ => md_red _ 2 4 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 6, _ => md_red _ 1 5 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | k + 7, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem addE (x y : Zp (4 * 1 + 1 + 1)) : F.add x y = fadd x y := Subtype.ext (add_nat x.val y.val x.2 y.2)
theorem mulE (x y : Zp (4 * 1 + 1 + 1)) : F.mul x y = fmul x y := Subtype.ext (mul_nat x.val y.val x.2 y.2)
theorem negE (x : Zp (4 * 1 + 1 + 1)) : F.neg x = fneg x := Subtype.ext (by show md (4 * 1 + 1 + 1) ((4 * 1 + 1 + 1) * x.val) = ((4 * 1 + 1 + 1) * x.val) % 7; exact neg_nat x.val x.2)

def c4f (u v : Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1)) : Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1) :=
  (fadd (fmul u.1 v.1) (fneg (fmul u.2 v.2)), fadd (fmul u.1 v.2) (fmul u.2 v.1))
theorem c4E (u v : Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1)) : C.mul u v = c4f u v := by
  show Cyclo4.c4mul F u v = _
  unfold Cyclo4.c4mul
  rw [mulE, mulE, mulE, mulE, negE, addE, addE]; rfl
def pwf (z : Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1)) : Nat → Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1)
  | 0 => C.one
  | k + 1 => c4f (pwf z k) z
theorem pwE (z : Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1)) : ∀ k, pw C z k = pwf z k
  | 0 => rfl
  | k + 1 => by show C.mul (pw C z k) z = _; rw [c4E, pwE z k]; rfl

def nrmf (X Y : Zp (4 * 1 + 1 + 1)) : Zp (4 * 1 + 1 + 1) := fadd (fmul X X) (fmul Y Y)
def lhsf (X Y : Zp (4 * 1 + 1 + 1)) : Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1) := c4f (pwf (c4f (c4f (X, Y) (X, Y)) C.one) 2) (pwf (nrmf X Y, F.zero) 1)
def rhsf (X Y : Zp (4 * 1 + 1 + 1)) : Zp (4 * 1 + 1 + 1) := fadd (fmul X X) (fneg (fmul Y Y))

theorem nrmE (X Y : Zp (4 * 1 + 1 + 1)) : nrm X Y = nrmf X Y := by
  unfold nrm nrmf; rw [mulE, mulE, addE]
theorem lhsE (X Y : Zp (4 * 1 + 1 + 1)) : lhs X Y = lhsf X Y := by
  unfold lhs lhsf; rw [nrmE, c4E, c4E, pwE, pwE, c4E]
theorem rhsE (X Y : Zp (4 * 1 + 1 + 1)) : rhs X Y = rhsf X Y := by
  unfold rhs rhsf
  repeat (first | rw [mulE] | rw [addE] | rw [negE])
  try rfl

def code (z : Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1)) : Nat := z.1.val + 7 * z.2.val
def chk (X Y : Zp (4 * 1 + 1 + 1)) : Bool := (!(Nat.beq (code (lhsf X Y)) 6) || Nat.beq (rhsf X Y).val 0) &&
  (!(Nat.beq (rhsf X Y).val 0) || Nat.beq (nrmf X Y).val 0 || Nat.beq (code (lhsf X Y)) 6)

theorem row0 : ∀ y : Nat, y < 7 → chk (mk (4 * 1 + 1 + 1) 0) (mk (4 * 1 + 1 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | k + 7, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem row1 : ∀ y : Nat, y < 7 → chk (mk (4 * 1 + 1 + 1) 1) (mk (4 * 1 + 1 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | k + 7, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem row2 : ∀ y : Nat, y < 7 → chk (mk (4 * 1 + 1 + 1) 2) (mk (4 * 1 + 1 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | k + 7, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem row3 : ∀ y : Nat, y < 7 → chk (mk (4 * 1 + 1 + 1) 3) (mk (4 * 1 + 1 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | k + 7, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem row4 : ∀ y : Nat, y < 7 → chk (mk (4 * 1 + 1 + 1) 4) (mk (4 * 1 + 1 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | k + 7, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem row5 : ∀ y : Nat, y < 7 → chk (mk (4 * 1 + 1 + 1) 5) (mk (4 * 1 + 1 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | k + 7, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem row6 : ∀ y : Nat, y < 7 → chk (mk (4 * 1 + 1 + 1) 6) (mk (4 * 1 + 1 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | k + 7, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem chk_nat : ∀ x y : Nat, x < 7 → y < 7 → chk (mk (4 * 1 + 1 + 1) x) (mk (4 * 1 + 1 + 1) y) = true
  | 0, y, _, h => row0 y h
  | 1, y, _, h => row1 y h
  | 2, y, _, h => row2 y h
  | 3, y, _, h => row3 y h
  | 4, y, _, h => row4 y h
  | 5, y, _, h => row5 y h
  | 6, y, _, h => row6 y h
  | k + 7, _, h, _ => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem chk_all (X Y : Zp (4 * 1 + 1 + 1)) : chk X Y = true := by
  rw [show X = mk (4 * 1 + 1 + 1) X.val from Subtype.ext (md_small _ _ X.2).symm, show Y = mk (4 * 1 + 1 + 1) Y.val from Subtype.ext (md_small _ _ Y.2).symm]
  exact chk_nat X.val Y.val X.2 Y.2

theorem ne_of_beq (a b : Nat) (h : Nat.beq a b = false) (e : a = b) : False := by
  rw [e, beq_self] at h; exact Bool.noConfusion h

theorem code_eq (z : Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1)) (h : code z = 6) : z = sC := by
  match z, h with
  | (u, v), h =>
    have e' : u.val + 7 * v.val = 6 := h
    have hv : v.val = 0 := by
      cases hv' : v.val with
      | zero => rfl
      | succ t =>
        rw [hv'] at e'
        exact (lt_wrap 7 (u.val + 7 * t + 0) ((show u.val + 7 * t + 0 + 1 + 7 = u.val + 7 * (t + 1) + 1 by lib_nat).trans
          (by rw [show u.val + 7 * (t + 1) + 1 = (u.val + 7 * (t + 1)) + 1 from rfl, e']))).elim
    have hu : u.val = 6 := by rw [hv] at e'; exact e'
    exact Prod.ext (Subtype.ext hu) (Subtype.ext hv)

theorem table (X Y : Zp (4 * 1 + 1 + 1)) (h : lhs X Y = sC) : rhs X Y = F.zero := by
  have hc := band_l _ _ (chk_all X Y)
  rw [← lhsE, h] at hc
  rw [rhsE]
  exact (Subtype.ext (beq_eq _ _ hc) : rhsf X Y = ⟨0, Nat.zero_lt_succ _⟩)

theorem table' (X Y : Zp (4 * 1 + 1 + 1)) (h : rhs X Y = F.zero) (hn : nrm X Y ≠ F.zero) : lhs X Y = sC := by
  have hc := band_r _ _ (chk_all X Y)
  rw [← rhsE, h] at hc
  rw [lhsE]
  rw [nrmE] at hn
  cases hb : Nat.beq (nrmf X Y).val 0 with
  | true => exact absurd (Subtype.ext (beq_eq _ _ hb) : nrmf X Y = F.zero) hn
  | false =>
    rw [hb] at hc
    exact code_eq _ (beq_eq _ _ hc)

theorem cast_norm (x y : Int) : castZ F (x * x + y * y) = nrm (castZ F x) (castZ F y) := by
  rw [IntCast.castZ_add, HasseDavenport.castZ_mul, HasseDavenport.castZ_mul]; rfl

theorem cast_rhs (x y : Int) : castZ F (x * x - y * y) = rhs (castZ F x) (castZ F y) := by
  rw [show x * x - y * y = x * x + -(y * y) from rfl]
  repeat (first | rw [HasseDavenport.castZ_mul] | rw [IntCast.castZ_add] | rw [HasseDavenport.castZ_neg])
  rfl

theorem hP (hpr : ∀ d, Dv d (4 * (2 * m + 2) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (2 * m + 2) + 3 + 1 + 1) (ι : Zp (4 * (2 * m + 2) + 3 + 1)) (hi : (ring (4 * (2 * m + 2) + 3 + 1)).mul ι ι = (ring (4 * (2 * m + 2) + 3 + 1)).neg (ring (4 * (2 * m + 2) + 3 + 1)).one) :
    ph4 F (Ppq (2 * m + 2)) = (nrm (castZ F (Jq (2 * m + 2) (chi4 (2 * m + 2) ι)).1) (castZ F (Jq (2 * m + 2) (chi4 (2 * m + 2) ι)).2), F.zero) := by
  rw [show Ppq (2 * m + 2) = (Int.ofNat (4 * (2 * m + 2) + 3 + 1 + 1), 0) from sum_oneq (2 * m + 2) _, ← quart_norm (2 * m + 2) hpr ι hi]; unfold ph4; rw [cast_norm]; rfl

theorem chi4_cases (ι x : Zp (4 * (2 * m + 2) + 3 + 1)) : chi4 (2 * m + 2) ι x = (0, 0) ∨ chi4 (2 * m + 2) ι x = (1, 0) ∨ chi4 (2 * m + 2) ι x = (0, 1) ∨ chi4 (2 * m + 2) ι x = (-1, 0) ∨ chi4 (2 * m + 2) ι x = (0, -1) := by
  unfold chi4
  by_cases h1 : x = (ring (4 * (2 * m + 2) + 3 + 1)).zero
  · exact Or.inl (ite_t _ _ h1)
  · rw [ite_f _ _ h1]
    by_cases h2 : pw (ring (4 * (2 * m + 2) + 3 + 1)) x ((2 * m + 2) + 1) = (ring (4 * (2 * m + 2) + 3 + 1)).one
    · exact Or.inr (Or.inl (ite_t _ _ h2))
    · rw [ite_f _ _ h2]
      by_cases h3 : pw (ring (4 * (2 * m + 2) + 3 + 1)) x ((2 * m + 2) + 1) = ι
      · exact Or.inr (Or.inr (Or.inl (ite_t _ _ h3)))
      · rw [ite_f _ _ h3]
        by_cases h4 : pw (ring (4 * (2 * m + 2) + 3 + 1)) x ((2 * m + 2) + 1) = (ring (4 * (2 * m + 2) + 3 + 1)).neg (ring (4 * (2 * m + 2) + 3 + 1)).one
        · exact Or.inr (Or.inr (Or.inr (Or.inl (ite_t _ _ h4))))
        · exact Or.inr (Or.inr (Or.inr (Or.inr (ite_f _ _ h4))))

theorem recip (hpr : ∀ d, Dv d (4 * (2 * m + 2) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (2 * m + 2) + 3 + 1 + 1) (ι : Zp (4 * (2 * m + 2) + 3 + 1)) (hi : (ring (4 * (2 * m + 2) + 3 + 1)).mul ι ι = (ring (4 * (2 * m + 2) + 3 + 1)).neg (ring (4 * (2 * m + 2) + 3 + 1)).one) :
    C.mul (ph4 F (chi4 (2 * m + 2) ι (nsm (ring (4 * (2 * m + 2) + 3 + 1)) (4 * 1 + 1 + 1 + 1) (ring (4 * (2 * m + 2) + 3 + 1)).one))) sC = lhs (castZ F (Jq (2 * m + 2) (chi4 (2 * m + 2) ι)).1) (castZ F (Jq (2 * m + 2) (chi4 (2 * m + 2) ι)).2) := by
  have hc := quart_recip_inert (2 * m + 2) 1 hpr q_prime ι hi (q_ne m)
  rw [show chib (2 * m + 2) ι ((ring (4 * (2 * m + 2) + 3 + 1)).neg (ring (4 * (2 * m + 2) + 3 + 1)).one) = gconj (chi4 (2 * m + 2) ι ((ring (4 * (2 * m + 2) + 3 + 1)).neg (ring (4 * (2 * m + 2) + 3 + 1)).one)) from chi_ivq (2 * m + 2) hpr ι hi _, chi_m1 m hpr ι hi, hP m hpr ι hi, ph4_one] at hc
  exact hc

/-- **7 a quartic residue mod p ⇒ 7 | a * a - b * b**. -/
theorem quart_seven_5 (hpr : ∀ d, Dv d (4 * (2 * m + 2) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (2 * m + 2) + 3 + 1 + 1) (ι : Zp (4 * (2 * m + 2) + 3 + 1)) (hi : (ring (4 * (2 * m + 2) + 3 + 1)).mul ι ι = (ring (4 * (2 * m + 2) + 3 + 1)).neg (ring (4 * (2 * m + 2) + 3 + 1)).one)
    (hq : chi4 (2 * m + 2) ι (nsm (ring (4 * (2 * m + 2) + 3 + 1)) (4 * 1 + 1 + 1 + 1) (ring (4 * (2 * m + 2) + 3 + 1)).one) = (1, 0)) : IDv (Int.ofNat 7) ((Jq (2 * m + 2) (chi4 (2 * m + 2) ι)).1 * (Jq (2 * m + 2) (chi4 (2 * m + 2) ι)).1 - (Jq (2 * m + 2) (chi4 (2 * m + 2) ι)).2 * (Jq (2 * m + 2) (chi4 (2 * m + 2) ι)).2) := by
  have hc := recip m hpr ι hi
  rw [hq] at hc
  have hl : lhs (castZ F (Jq (2 * m + 2) (chi4 (2 * m + 2) ι)).1) (castZ F (Jq (2 * m + 2) (chi4 (2 * m + 2) ι)).2) = sC := hc.symm.trans (by rw [c4E]; rfl)
  exact CubicTwo.cast_zero_gen (4 * 1 + 1 + 1) _ ((cast_rhs _ _).trans (table _ _ hl))

/-- **7 | a * a - b * b ⇒ 7 is a quartic residue mod p.** -/
theorem quart_seven_5_conv (hpr : ∀ d, Dv d (4 * (2 * m + 2) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (2 * m + 2) + 3 + 1 + 1) (ι : Zp (4 * (2 * m + 2) + 3 + 1)) (hi : (ring (4 * (2 * m + 2) + 3 + 1)).mul ι ι = (ring (4 * (2 * m + 2) + 3 + 1)).neg (ring (4 * (2 * m + 2) + 3 + 1)).one)
    (hd : IDv (Int.ofNat 7) ((Jq (2 * m + 2) (chi4 (2 * m + 2) ι)).1 * (Jq (2 * m + 2) (chi4 (2 * m + 2) ι)).1 - (Jq (2 * m + 2) (chi4 (2 * m + 2) ι)).2 * (Jq (2 * m + 2) (chi4 (2 * m + 2) ι)).2)) : chi4 (2 * m + 2) ι (nsm (ring (4 * (2 * m + 2) + 3 + 1)) (4 * 1 + 1 + 1 + 1) (ring (4 * (2 * m + 2) + 3 + 1)).one) = (1, 0) := by
  have hc := recip m hpr ι hi
  have hr : rhs (castZ F (Jq (2 * m + 2) (chi4 (2 * m + 2) ι)).1) (castZ F (Jq (2 * m + 2) (chi4 (2 * m + 2) ι)).2) = F.zero := by
    rw [← cast_rhs]
    match hd with
    | ⟨c, hc'⟩ =>
      rw [hc', HasseDavenport.castZ_mul]
      have h0 : castZ F (Int.ofNat 7) = F.zero := CubeAp.castP 5
      rw [h0, zero_mul']
  have hn : nrm (castZ F (Jq (2 * m + 2) (chi4 (2 * m + 2) ι)).1) (castZ F (Jq (2 * m + 2) (chi4 (2 * m + 2) ι)).2) ≠ F.zero := fun e => by
    have e1 : castZ F (Int.ofNat (4 * (2 * m + 2) + 3 + 1 + 1)) = F.zero := by rw [← quart_norm (2 * m + 2) hpr ι hi, cast_norm]; exact e
    have e2 : mk (4 * 1 + 1 + 1) (4 * (2 * m + 2) + 3 + 1 + 1) = F.zero := (IntCast.castN_mk _ _).symm.trans e1
    have hdq : ∀ P' : Nat, mk (4 * 1 + 1 + 1) P' = F.zero → Dv (4 * 1 + 1 + 1 + 1) P' := fun P' h => md_zero_dv (4 * 1 + 1 + 1) P' (congrArg Subtype.val h)
    exact not_dvq m hpr (hdq _ e2)
  have h1 := hc.trans (table' _ _ hr hn)
  rcases chi4_cases m ι (nsm (ring (4 * (2 * m + 2) + 3 + 1)) (4 * 1 + 1 + 1 + 1) (ring (4 * (2 * m + 2) + 3 + 1)).one) with e | e | e | e | e
  · rw [e, c4E] at h1; exact absurd (congrArg code h1) (fun e' => Nat.noConfusion e')
  · exact e
  · rw [e, c4E] at h1; exact absurd (congrArg code h1) (ne_of_beq _ _ rfl)
  · rw [e, c4E] at h1; exact absurd (congrArg code h1) (ne_of_beq _ _ rfl)
  · rw [e, c4E] at h1; exact absurd (congrArg code h1) (ne_of_beq _ _ rfl)

end QuartSeven5
#print axioms QuartSeven5.lt_wrap
#print axioms QuartSeven5.ndvN
#print axioms QuartSeven5.q_prime
#print axioms QuartSeven5.q_ne
#print axioms QuartSeven5.not_dvq
#print axioms QuartSeven5.chi_m1
#print axioms QuartSeven5.md_red
#print axioms QuartSeven5.add_nat
#print axioms QuartSeven5.mul_nat
#print axioms QuartSeven5.neg_nat
#print axioms QuartSeven5.addE
#print axioms QuartSeven5.mulE
#print axioms QuartSeven5.negE
#print axioms QuartSeven5.c4E
#print axioms QuartSeven5.pwE
#print axioms QuartSeven5.nrmE
#print axioms QuartSeven5.lhsE
#print axioms QuartSeven5.rhsE
#print axioms QuartSeven5.row0
#print axioms QuartSeven5.row1
#print axioms QuartSeven5.row2
#print axioms QuartSeven5.row3
#print axioms QuartSeven5.row4
#print axioms QuartSeven5.row5
#print axioms QuartSeven5.row6
#print axioms QuartSeven5.chk_nat
#print axioms QuartSeven5.chk_all
#print axioms QuartSeven5.ne_of_beq
#print axioms QuartSeven5.code_eq
#print axioms QuartSeven5.table
#print axioms QuartSeven5.table'
#print axioms QuartSeven5.cast_norm
#print axioms QuartSeven5.cast_rhs
#print axioms QuartSeven5.hP
#print axioms QuartSeven5.chi4_cases
#print axioms QuartSeven5.recip
#print axioms QuartSeven5.quart_seven_5
#print axioms QuartSeven5.quart_seven_5_conv