import Thesis.Applications

/-!
# Section 4.3: the K-theoretic applications of Theorem 4.1

Shubhrajit Bhattacharya, *On Some Congruences of Zeta and L-values at Negative Odd
Integers* (UBC MSc thesis, April 2024), §4.3.

Contents:

* congruence algebra for `ℂ_[p]`: closure of `PadicModEq`/`ResidueModEq` under sums,
  products, powers and finite products, Fermat in `ℂ_[p]` (`2^(p-1) ≡ 1 (mod p)`) and
  consequences — no analysis, no external input;
* **Corollary 4.14** (Harder's homological Euler characteristic of `Sp_{2n}`):
  `euler_congruence_strengthened` with the exact index condition `p - 1 ∤ 2i` used by the
  printed proof, `euler_congruence_of_two_mul_lt` unconditionally for `p > 2n+1`, and
  `euler_congruence_source_statement` for the printed hypothesis together with the single
  missing input `ExceptionalIndexCongruence` at the index `2i = p - 1`;
  `harder_index_obstruction` records that the printed hypothesis does not imply the index
  condition, which is the gap in the printed proof;
* **Corollary 4.12** (`p`-torsion in `K_{4m-2}`): `cor_4_12_arithmetic` is the input
  Theorem 4.1 provides, `cor_4_12` the K-theoretic deduction with the external theorems
  (Wiles/Lichtenbaum, Lemma 4.11) as explicit fields of `HigherKGroupData`.

The only remaining `sorryAx` in the statements proved here enters through
`Thesis.main_congruence` (Theorem 4.1), which this file uses but does not prove.
-/

namespace Thesis

noncomputable section

open scoped BigOperators

variable {p : ℕ} [Fact p.Prime]

/-- The additional index condition needed by the proof given in the thesis: no index
`1 ≤ i ≤ n` has `p - 1 ∣ 2 i`.  (Declared here so that this module does not depend on
where the project keeps it; `Thesis/Applications.lean` must not declare it as well.) -/
def HarderIndexCondition (p n : ℕ) : Prop :=
  ∀ i ∈ Finset.Icc 1 n, ¬(p - 1 ∣ 2 * i)

/-! ### Norms, integrality and congruence algebra -/

lemma harder_norm_prime_lt_one (p : ℕ) [Fact p.Prime] : ‖(p : ℂ_[p])‖ < 1 := by
  rw [show (p : ℂ_[p]) = ((p : ℚ_[p]) : ℂ_[p]) by norm_cast,
    PadicComplex.norm_extends', Padic.norm_p]
  exact inv_lt_one_of_one_lt₀ (by exact_mod_cast (Fact.out : Nat.Prime p).one_lt)

lemma harder_norm_prime_pow_le_one (k : ℕ) : ‖(p : ℂ_[p]) ^ k‖ ≤ 1 := by
  rw [norm_pow]
  calc ‖(p : ℂ_[p])‖ ^ k ≤ 1 ^ k :=
        pow_le_pow_left₀ (norm_nonneg _) (harder_norm_prime_lt_one p).le k
    _ = 1 := one_pow k

lemma harder_norm_sub_le_one_of_padicModEq {k : ℕ} {x y : ℂ_[p]} (h : PadicModEq p k x y) :
    ‖x - y‖ ≤ 1 := by
  have hp0 : (p : ℂ_[p]) ≠ 0 := by exact_mod_cast (Fact.out : Nat.Prime p).ne_zero
  rw [PadicModEq, PadicIntegral] at h
  have heq : x - y = (p : ℂ_[p]) ^ k * ((x - y) / (p : ℂ_[p]) ^ k) := by
    field_simp
  rw [heq, norm_mul]
  exact mul_le_one₀ (harder_norm_prime_pow_le_one k) (norm_nonneg _) h

lemma harder_padicIntegral_of_padicModEq {k : ℕ} {x y : ℂ_[p]}
    (h : PadicModEq p k x y) (hy : PadicIntegral p y) : PadicIntegral p x := by
  have hxy : ‖x - y‖ ≤ 1 := harder_norm_sub_le_one_of_padicModEq h
  have hx : x = (x - y) + y := by ring
  rw [PadicIntegral, hx]
  exact (PadicComplex.isNonarchimedean p _ _).trans (max_le hxy hy)

lemma harder_padicIntegral_pow {x : ℂ_[p]} (hx : PadicIntegral p x) (k : ℕ) :
    PadicIntegral p (x ^ k) := by
  induction k with
  | zero => simpa using padicIntegral_one p
  | succ k ih => rw [pow_succ]; exact padicIntegral_mul ih hx

lemma harder_padicIntegral_inv {x : ℂ_[p]} (hx : ‖x‖ = 1) : PadicIntegral p x⁻¹ := by
  rw [PadicIntegral, norm_inv, hx, inv_one]

lemma harder_padicModEq_add {k : ℕ} {x x' y y' : ℂ_[p]}
    (h : PadicModEq p k x x') (h' : PadicModEq p k y y') :
    PadicModEq p k (x + y) (x' + y') := by
  simp only [PadicModEq] at *
  have heq : (x + y - (x' + y')) / (p : ℂ_[p]) ^ k
      = (x - x') / (p : ℂ_[p]) ^ k + (y - y') / (p : ℂ_[p]) ^ k := by ring
  rw [heq]
  exact padicIntegral_add h h'

lemma harder_padicModEq_sub {k : ℕ} {x x' y y' : ℂ_[p]}
    (h : PadicModEq p k x x') (h' : PadicModEq p k y y') :
    PadicModEq p k (x - y) (x' - y') := by
  simp only [PadicModEq] at *
  have heq : (x - y - (x' - y')) / (p : ℂ_[p]) ^ k
      = (x - x') / (p : ℂ_[p]) ^ k - (y - y') / (p : ℂ_[p]) ^ k := by ring
  rw [heq]
  exact padicIntegral_sub h h'

lemma harder_padicModEq_mul_left {k : ℕ} {x y : ℂ_[p]} (z : ℂ_[p])
    (hz : PadicIntegral p z) (h : PadicModEq p k x y) :
    PadicModEq p k (z * x) (z * y) := by
  simp only [PadicModEq] at *
  have heq : (z * x - z * y) / (p : ℂ_[p]) ^ k = z * ((x - y) / (p : ℂ_[p]) ^ k) := by
    ring
  rw [heq]
  exact padicIntegral_mul hz h

lemma harder_padicModEq_mul {k : ℕ} {x x' y y' : ℂ_[p]} (hx : PadicIntegral p x)
    (hy' : PadicIntegral p y') (h : PadicModEq p k x x') (h' : PadicModEq p k y y') :
    PadicModEq p k (x * y) (x' * y') := by
  simp only [PadicModEq] at *
  have heq : (x * y - x' * y') / (p : ℂ_[p]) ^ k
      = x * ((y - y') / (p : ℂ_[p]) ^ k) + ((x - x') / (p : ℂ_[p]) ^ k) * y' := by
    ring
  rw [heq]
  exact padicIntegral_add (padicIntegral_mul hx h') (padicIntegral_mul h hy')

lemma harder_padicModEq_pow {k : ℕ} {x y : ℂ_[p]} (hy : PadicIntegral p y)
    (h : PadicModEq p k x y) (N : ℕ) : PadicModEq p k (x ^ N) (y ^ N) := by
  induction N with
  | zero => simpa using padicModEq_refl p k 1
  | succ N ih =>
    have hx' : PadicIntegral p (x ^ N) :=
      harder_padicIntegral_pow (harder_padicIntegral_of_padicModEq h hy) N
    have hy' : PadicIntegral p (y ^ N) := harder_padicIntegral_pow hy N
    rw [pow_succ, pow_succ]
    exact harder_padicModEq_mul hx' hy ih h

lemma harder_padicModEq_prod {ι : Type*} (s : Finset ι) {k : ℕ} (f g : ι → ℂ_[p])
    (hf : ∀ i ∈ s, PadicIntegral p (f i)) (hg : ∀ i ∈ s, PadicIntegral p (g i))
    (h : ∀ i ∈ s, PadicModEq p k (f i) (g i)) :
    PadicModEq p k (∏ i ∈ s, f i) (∏ i ∈ s, g i) := by
  classical
  induction s using Finset.induction_on with
  | empty => simpa using padicModEq_refl p k 1
  | @insert a s ha ih =>
    rw [Finset.prod_insert ha, Finset.prod_insert ha]
    exact harder_padicModEq_mul (hf a (Finset.mem_insert_self a s))
      (finset_prod_integral s g fun i hi => hg i (Finset.mem_insert_of_mem hi))
      (h a (Finset.mem_insert_self a s))
      (ih (fun i hi => hf i (Finset.mem_insert_of_mem hi))
        (fun i hi => hg i (Finset.mem_insert_of_mem hi))
        (fun i hi => h i (Finset.mem_insert_of_mem hi)))

lemma harder_residueModEq_mul {x x' y y' : ℂ_[p]} (hx : PadicIntegral p x)
    (hy' : PadicIntegral p y') (h : ResidueModEq p x x') (h' : ResidueModEq p y y') :
    ResidueModEq p (x * y) (x' * y') := by
  simp only [ResidueModEq, PadicIntegral] at *
  have h1 : ‖x * (y - y')‖ ≤ ‖y - y'‖ := by
    rw [norm_mul]
    calc ‖x‖ * ‖y - y'‖ ≤ 1 * ‖y - y'‖ := by
          exact mul_le_mul_of_nonneg_right hx (norm_nonneg _)
      _ = ‖y - y'‖ := one_mul _
  have h2 : ‖(x - x') * y'‖ ≤ ‖x - x'‖ := by
    rw [norm_mul]
    calc ‖x - x'‖ * ‖y'‖ ≤ ‖x - x'‖ * 1 := by
          exact mul_le_mul_of_nonneg_left hy' (norm_nonneg _)
      _ = ‖x - x'‖ := mul_one _
  have heq : x * y - x' * y' = x * (y - y') + (x - x') * y' := by ring
  rw [heq]
  exact (PadicComplex.isNonarchimedean p _ _).trans_lt (max_lt (lt_of_le_of_lt h1 h') (lt_of_le_of_lt h2 h))

lemma harder_residueModEq_prod {ι : Type*} (s : Finset ι) (f g : ι → ℂ_[p])
    (hf : ∀ i ∈ s, PadicIntegral p (f i)) (hg : ∀ i ∈ s, PadicIntegral p (g i))
    (h : ∀ i ∈ s, ResidueModEq p (f i) (g i)) :
    ResidueModEq p (∏ i ∈ s, f i) (∏ i ∈ s, g i) := by
  classical
  induction s using Finset.induction_on with
  | empty => simpa using residueModEq_refl p 1
  | @insert a s ha ih =>
    rw [Finset.prod_insert ha, Finset.prod_insert ha]
    exact harder_residueModEq_mul (hf a (Finset.mem_insert_self a s))
      (finset_prod_integral s g fun i hi => hg i (Finset.mem_insert_of_mem hi))
      (h a (Finset.mem_insert_self a s))
      (ih (fun i hi => hf i (Finset.mem_insert_of_mem hi))
        (fun i hi => hg i (Finset.mem_insert_of_mem hi))
        (fun i hi => h i (Finset.mem_insert_of_mem hi)))

lemma harder_residueModEq_mul_unit {u x y : ℂ_[p]} (hu : ‖u‖ = 1) :
    ResidueModEq p (u * x) (u * y) ↔ ResidueModEq p x y := by
  simp only [ResidueModEq]
  rw [show u * x - u * y = u * (x - y) by ring, norm_mul, hu, one_mul]

/-! ### Fermat in `ℂ_[p]`, and powers of two -/

lemma harder_padicIntegral_intCast (z : ℤ) : PadicIntegral p (z : ℂ_[p]) := by
  cases z with
  | ofNat n => simpa using padicIntegral_natCast p n
  | negSucc n =>
    rw [Int.cast_negSucc]
    exact padicIntegral_neg (padicIntegral_add (padicIntegral_natCast p n) (padicIntegral_one p))

lemma harder_padicModEq_one_intCast_zero_of_dvd {z : ℤ} (h : (p : ℤ) ∣ z) :
    PadicModEq p 1 (z : ℂ_[p]) 0 := by
  obtain ⟨k, rfl⟩ := h
  have hp0 : (p : ℂ_[p]) ≠ 0 := by exact_mod_cast (Fact.out : Nat.Prime p).ne_zero
  simp only [PadicModEq, sub_zero]
  have heq : ((((p : ℤ)) * k : ℤ) : ℂ_[p]) / (p : ℂ_[p]) ^ 1 = (k : ℂ_[p]) := by
    push_cast
    rw [pow_one]
    exact mul_div_cancel_left₀ _ hp0
  rw [heq]
  exact harder_padicIntegral_intCast (p := p) k

lemma harder_padicModEq_one_natCast_of_dvd_sub_one {m : ℕ} (hm : 1 ≤ m) (h : p ∣ m - 1) :
    PadicModEq p 1 (m : ℂ_[p]) 1 := by
  obtain ⟨t, ht⟩ := h
  have hz : (p : ℤ) ∣ (m : ℤ) - 1 := by
    refine ⟨(t : ℤ), ?_⟩
    have : ((m - 1 : ℕ) : ℤ) = (p * t : ℕ) := by rw [ht]
    push_cast [Nat.cast_sub hm] at this ⊢
    linarith
  have := harder_padicModEq_one_intCast_zero_of_dvd (p := p) hz
  rw [PadicModEq] at this ⊢
  have heq : ((m : ℂ_[p]) - 1) = (((m : ℤ) - 1 : ℤ) : ℂ_[p]) := by push_cast; ring
  have heq2 : ((m : ℂ_[p]) - 1) / (p : ℂ_[p]) ^ 1 = (((m : ℤ) - 1 : ℤ) : ℂ_[p]) / (p : ℂ_[p]) ^ 1 := by
    rw [heq]
  rw [heq2]
  simpa using this

lemma harder_dvd_two_pow_pred_sub_one (hp : 2 < p) : p ∣ 2 ^ (p - 1) - 1 := by
  have hpne : (2 : ZMod p) ≠ 0 := by
    intro h
    have hd : (p : ℤ) ∣ 2 := (ZMod.intCast_zmod_eq_zero_iff_dvd 2 p).mp (by simpa using h)
    have hd' : p ∣ 2 := by exact_mod_cast hd
    have hle := Nat.le_of_dvd (by norm_num) hd'
    omega
  have h := ZMod.pow_card_sub_one_eq_one hpne
  have hcast : ((2 ^ (p - 1) : ℕ) : ZMod p) = ((1 : ℕ) : ZMod p) := by
    push_cast
    exact h
  have hmod : 2 ^ (p - 1) ≡ 1 [MOD p] := (ZMod.natCast_eq_natCast_iff _ _ _).mp hcast
  exact (Nat.modEq_iff_dvd' Nat.one_le_two_pow).mp hmod.symm

lemma harder_norm_two_eq_one (hp : 2 < p) : ‖(2 : ℂ_[p])‖ = 1 := by
  have hco : ((2 : ℚ_[p]) : ℂ_[p]) = (2 : ℂ_[p]) := by
    have h1 : ((2 : ℚ_[p]) : ℂ_[p]) = (((2 : ℕ) : PadicAlgCl p) : ℂ_[p]) := rfl
    rw [h1]
    exact PadicComplex.coe_natCast (p := p) 2
  have h := PadicComplex.norm_extends' (p := p) (2 : ℚ_[p])
  rw [hco] at h
  rw [h, show (2 : ℚ_[p]) = ((2 : ℕ) : ℚ_[p]) by norm_num,
    Padic.norm_natCast_eq_one_iff]
  refine (Fact.out : Nat.Prime p).coprime_iff_not_dvd.mpr ?_
  intro hd
  have hle := Nat.le_of_dvd (by norm_num) hd
  omega

lemma harder_padicModEq_one_two_pow_pred (hp : 2 < p) :
    PadicModEq p 1 ((2 : ℂ_[p]) ^ (p - 1)) 1 := by
  have h2 := harder_padicModEq_one_natCast_of_dvd_sub_one (p := p) Nat.one_le_two_pow
    (harder_dvd_two_pow_pred_sub_one hp)
  simpa only [Nat.cast_pow, Nat.cast_ofNat] using h2

lemma harder_padicModEq_one_zpow_neg {u : ℂ_[p]} (hu : ‖u‖ = 1)
    (h : PadicModEq p 1 u 1) (N : ℕ) : PadicModEq p 1 (u ^ (-(N : ℤ))) 1 := by
  have hN : PadicModEq p 1 (u ^ N) 1 := by
    have h' := harder_padicModEq_pow (p := p) (y := (1 : ℂ_[p])) (padicIntegral_one p) h N
    simpa using h'
  have hnorm : ‖u ^ N‖ = 1 := by rw [norm_pow, hu, one_pow]
  have hz : u ^ (-(N : ℤ)) = (u ^ N)⁻¹ := by rw [zpow_neg, zpow_natCast]
  rw [hz]
  simp only [PadicModEq] at hN ⊢
  have heq : ((u ^ N)⁻¹ - 1) / (p : ℂ_[p]) ^ 1
      = -(((u ^ N - 1) / (p : ℂ_[p]) ^ 1) * (u ^ N)⁻¹) := by
    have huN : u ^ N ≠ 0 := by
      intro h0
      rw [h0, norm_zero] at hnorm
      exact zero_ne_one hnorm
    field_simp
    ring
  rw [heq]
  exact padicIntegral_neg (padicIntegral_mul hN (harder_padicIntegral_inv hnorm))

lemma harder_norm_two_zpow (hp : 2 < p) (e : ℤ) : ‖(2 : ℂ_[p]) ^ e‖ = 1 := by
  rw [norm_zpow, harder_norm_two_eq_one hp, one_zpow]

lemma harder_padicModEq_trans {k : ℕ} {x y z : ℂ_[p]}
    (h1 : PadicModEq p k x y) (h2 : PadicModEq p k y z) : PadicModEq p k x z := by
  simp only [PadicModEq] at *
  have heq : (x - z) / (p : ℂ_[p]) ^ k
      = (x - y) / (p : ℂ_[p]) ^ k + (y - z) / (p : ℂ_[p]) ^ k := by ring
  rw [heq]
  exact padicIntegral_add h1 h2

lemma harder_padicModEq_one_two_zpow_pred (hp : 2 < p) :
    PadicModEq p 1 ((2 : ℂ_[p]) ^ ((p : ℤ) - 1)) 1 := by
  have hcast : ((p : ℤ) - 1) = ((p - 1 : ℕ) : ℤ) := by
    rw [Nat.cast_sub (show 1 ≤ p by omega)]
    norm_num
  rw [hcast, zpow_natCast]
  exact harder_padicModEq_one_two_pow_pred hp

lemma harder_padicIntegral_two_zpow_pred (hp : 2 < p) :
    PadicIntegral p ((2 : ℂ_[p]) ^ ((p : ℤ) - 1)) :=
  le_of_eq (harder_norm_two_zpow hp ((p : ℤ) - 1))

lemma harder_padicModEq_one_two_zpow_mul_pred (hp : 2 < p) (N : ℕ) :
    PadicModEq p 1 ((2 : ℂ_[p]) ^ (((p : ℤ) - 1) * (N : ℤ))) 1 := by
  have hu : PadicModEq p 1 ((2 : ℂ_[p]) ^ ((p : ℤ) - 1)) 1 := harder_padicModEq_one_two_zpow_pred hp
  have hpow : PadicModEq p 1 (((2 : ℂ_[p]) ^ ((p : ℤ) - 1)) ^ N) (1 ^ N) :=
    harder_padicModEq_pow (p := p) (k := 1) (x := (2 : ℂ_[p]) ^ ((p : ℤ) - 1)) (y := 1)
      (padicIntegral_one p) hu N
  have heq : ((2 : ℂ_[p]) ^ ((p : ℤ) - 1)) ^ N
      = (2 : ℂ_[p]) ^ (((p : ℤ) - 1) * (N : ℤ)) := by
    rw [← zpow_natCast, ← zpow_mul]
  rwa [heq, one_pow] at hpow

/-! ### The index condition of Corollary 4.14 -/

lemma HarderIndexCondition.two_mul_lt (hp : 3 ≤ p) (h : HarderIndexCondition p n) :
    2 * n < p - 1 := by
  by_contra hcon
  have hcon' : p - 1 ≤ 2 * n := not_lt.mp hcon
  have hodd : Odd p := (Fact.out : Nat.Prime p).odd_of_ne_two (by omega)
  obtain ⟨k, hk⟩ := hodd
  have hp1 : p - 1 = 2 * k := by omega
  have hik : k ≤ n := by omega
  have hk1 : 1 ≤ k := by omega
  refine h k (Finset.mem_Icc.mpr ⟨hk1, hik⟩) ?_
  rw [hp1]

lemma HarderIndexCondition.lt_p (hp : 3 ≤ p) (h : HarderIndexCondition p n) : n < p := by
  have := h.two_mul_lt hp
  omega

omit [Fact p.Prime] in
lemma HarderIndexCondition.admissible (_hp : 3 ≤ p) (h : HarderIndexCondition p n)
    {i : ℕ} (hi : i ∈ Finset.Icc 1 n) : AdmissibleIndex p (2 * i) := by
  obtain ⟨h1, h2⟩ := Finset.mem_Icc.mp hi
  exact ⟨by omega, ⟨i, by ring⟩, h i hi⟩

omit [Fact p.Prime] in
/-- If `p > 2n+1` then every index `1 ≤ i ≤ n` is admissible: `HarderIndexCondition`
holds.  This is the range in which the printed proof of Corollary 4.14 is complete, and
it contains the printed hypothesis `p > max 3 (n+1)` for `n ≥ 1`. -/
lemma harderIndexCondition_of_two_mul_lt (hp : 2 * n + 1 < p) : HarderIndexCondition p n := by
  intro i hi
  obtain ⟨hi1, hin⟩ := Finset.mem_Icc.mp hi
  intro hd
  have hle : p - 1 ≤ 2 * i := Nat.le_of_dvd (by omega) hd
  omega

/-- The printed hypothesis of Corollary 4.14 does *not* imply its index condition:
at `p = 5, n = 2` we have `max 3 (n+1) = 3 < 5` but `p - 1 = 4 = 2 * 2` divides `2 * i`
for the admissible-looking index `i = 2`.  This is the exact gap in the printed proof,
not a counterexample to the statement. -/
lemma harder_index_obstruction : ¬ HarderIndexCondition 5 2 := by
  intro h
  have hmem : 2 ∈ Finset.Icc 1 2 := Finset.mem_Icc.mpr ⟨by norm_num, by norm_num⟩
  have := h 2 hmem
  norm_num at this

/-! ### Corollary 4.14: Harder's Euler characteristic of `Sp_{2n}` -/

/-- The exponent `n (deg F_α - n)` of Harder's formula (4.27), which is exactly the
exponent appearing in `SymplecticEulerData.euler_eq`. -/
def harderExponent (p n α : ℕ) : ℤ := -((n : ℤ) * ((p : ℤ) ^ α - (n : ℤ)))

lemma harder_padicModEq_one_two_zpow_mul_pred_neg (hp : 2 < p) (N : ℕ) :
    PadicModEq p 1 ((2 : ℂ_[p]) ^ (-(((p : ℤ) - 1) * (N : ℤ)))) 1 := by
  have h := harder_padicModEq_one_zpow_neg (p := p) (u := (2 : ℂ_[p]) ^ ((p : ℤ) - 1))
    (harder_norm_two_zpow hp _) (harder_padicModEq_one_two_zpow_pred hp) N
  have heq : ((2 : ℂ_[p]) ^ ((p : ℤ) - 1)) ^ (-(N : ℤ))
      = (2 : ℂ_[p]) ^ (-(((p : ℤ) - 1) * (N : ℤ))) := by
    rw [← zpow_mul]
    ring_nf
  rwa [heq] at h

lemma harderExponent_integral (hp : 2 < p) (α : ℕ) :
    PadicIntegral p ((2 : ℂ_[p]) ^ harderExponent p n α) :=
  le_of_eq (harder_norm_two_zpow hp (harderExponent p n α))

/-- The normalisation constant `2^{n(deg F_α - n)}` of (4.27) changes by `1` modulo `p`
between consecutive levels. -/
lemma residue_epsilon (hp : 2 < p) (α : ℕ) :
    PadicModEq p 1 ((2 : ℂ_[p]) ^ harderExponent p n α)
      ((2 : ℂ_[p]) ^ harderExponent p n (α + 1)) := by
  have hexp : harderExponent p n (α + 1)
      = harderExponent p n α + (-(((p : ℤ) - 1) * ((n * p ^ α : ℕ) : ℤ))) := by
    have hpow : (p : ℤ) ^ (α + 1) = (p : ℤ) ^ α * (p : ℤ) := pow_succ _ _
    simp only [harderExponent, hpow]
    push_cast
    ring
  rw [hexp, zpow_add₀ (by norm_num : (2 : ℂ_[p]) ≠ 0)]
  have h := harder_padicModEq_mul_left (p := p) ((2 : ℂ_[p]) ^ harderExponent p n α)
    (harderExponent_integral (p := p) (n := n) hp α)
    (harder_padicModEq_one_two_zpow_mul_pred_neg (p := p) hp (n * p ^ α))
  simpa [mul_comm, mul_left_comm, mul_assoc] using padicModEq_symm h

/-- The core of Corollary 4.14: if the values at two consecutive levels are given by
Harder's formula (4.27) and each single Dedekind value is congruent modulo `p`, then the
Euler characteristics are congruent modulo `p`. -/
theorem euler_congruence_of_factors (hp : 2 < p) (e : ℕ → ℂ_[p]) (α : ℕ)
    (hfac : ∀ i ∈ Finset.Icc 1 n,
      PadicModEq p 1 (cyclotomicTowerValue p α (2 * i))
        (cyclotomicTowerValue p (α + 1) (2 * i)))
    (hint : ∀ i ∈ Finset.Icc 1 n, PadicIntegral p (cyclotomicTowerValue p α (2 * i)))
    (hint' : ∀ i ∈ Finset.Icc 1 n,
      PadicIntegral p (cyclotomicTowerValue p (α + 1) (2 * i)))
    (hα : e α = (2 : ℂ_[p]) ^ harderExponent p n α *
      ∏ i ∈ Finset.Icc 1 n, cyclotomicTowerValue p α (2 * i))
    (hα' : e (α + 1) = (2 : ℂ_[p]) ^ harderExponent p n (α + 1) *
      ∏ i ∈ Finset.Icc 1 n, cyclotomicTowerValue p (α + 1) (2 * i)) :
    PadicModEq p 1 (e α) (e (α + 1)) := by
  have hprod : PadicModEq p 1
      (∏ i ∈ Finset.Icc 1 n, cyclotomicTowerValue p α (2 * i))
      (∏ i ∈ Finset.Icc 1 n, cyclotomicTowerValue p (α + 1) (2 * i)) :=
    harder_padicModEq_prod (s := Finset.Icc 1 n) (k := 1)
      (f := fun i => cyclotomicTowerValue p α (2 * i))
      (g := fun i => cyclotomicTowerValue p (α + 1) (2 * i)) hint hint' hfac
  have heps := residue_epsilon (p := p) (n := n) hp α
  have hprodint : PadicIntegral p
      (∏ i ∈ Finset.Icc 1 n, cyclotomicTowerValue p (α + 1) (2 * i)) :=
    finset_prod_integral _ _ hint'
  rw [hα, hα']
  exact harder_padicModEq_mul (p := p) (k := 1)
    (x := (2 : ℂ_[p]) ^ harderExponent p n α)
    (x' := (2 : ℂ_[p]) ^ harderExponent p n (α + 1))
    (y := ∏ i ∈ Finset.Icc 1 n, cyclotomicTowerValue p α (2 * i))
    (y' := ∏ i ∈ Finset.Icc 1 n, cyclotomicTowerValue p (α + 1) (2 * i))
    (harderExponent_integral (p := p) (n := n) hp α) hprodint heps hprod

/-- **Corollary 4.14** with the index condition that its printed proof actually uses.
This is `Thesis.euler_congruence_strengthened` of `Thesis/Applications.lean`, proved. -/
theorem euler_congruence_strengthened (D : SymplecticEulerData p n) (hp : 3 ≤ p)
    (hindex : HarderIndexCondition p n) (α : ℕ) :
    PadicModEq p 1 (D.euler α) (D.euler (α + 1)) := by
  have hp2 : 2 < p := by omega
  refine euler_congruence_of_factors (p := p) (n := n) hp2 D.euler α ?_ ?_ ?_
    (D.euler_eq α) (D.euler_eq (α + 1))
  · intro i hi
    exact padicModEq_of_le (by omega) (main_congruence hp α (2 * i)
      (hindex.admissible hp hi)).2
  · intro i hi
    exact (main_congruence hp α (2 * i) (hindex.admissible hp hi)).1
  · intro i hi
    exact (main_congruence hp (α + 1) (2 * i) (hindex.admissible hp hi)).1

/-- The precise obligation that Theorem 4.1 does *not* supply: at the exceptional index
`2 i = p - 1`, where `p - 1 ∣ 2 i`, both integrality and the congruence modulo `p` of the
tower values have to be supplied separately.  Under the printed hypothesis
`p > max 3 (n+1)` this is the only index outside the scope of Theorem 4.1. -/
def ExceptionalIndexCongruence (p n : ℕ) [Fact p.Prime] : Prop :=
  ∀ (i α : ℕ), i ∈ Finset.Icc 1 n → p - 1 = 2 * i →
    PadicIntegral p (cyclotomicTowerValue p α (2 * i)) ∧
      PadicIntegral p (cyclotomicTowerValue p (α + 1) (2 * i)) ∧
      PadicModEq p 1 (cyclotomicTowerValue p α (2 * i))
        (cyclotomicTowerValue p (α + 1) (2 * i))

/-- The printed statement of Corollary 4.14 in the range `p > 2n+1` (where the index
condition holds automatically and no extra hypothesis is needed).  For `1 ≤ n` the bound
`2n+1 < p` implies the printed hypothesis `p > max 3 (n+1)`. -/
theorem euler_congruence_of_two_mul_lt (D : SymplecticEulerData p n) (hp : 3 ≤ p)
    (h : 2 * n + 1 < p) (α : ℕ) : PadicModEq p 1 (D.euler α) (D.euler (α + 1)) :=
  euler_congruence_strengthened D hp (harderIndexCondition_of_two_mul_lt h) α

/-- **Corollary 4.14 as printed**, in the form that its printed proof establishes: under
`p > max 3 (n+1)` every index `1 ≤ i ≤ n` either satisfies `p - 1 ∤ 2 i` (then Theorem
4.1 applies) or satisfies `2 i = p - 1` (the exceptional index, supplied as an explicit
hypothesis; the printed proof uses it without justification). -/
theorem euler_congruence_source_statement_proof (D : SymplecticEulerData p n)
    (hp : max 3 (n + 1) < p) (hexc : ExceptionalIndexCongruence p n) (α : ℕ) :
    PadicModEq p 1 (D.euler α) (D.euler (α + 1)) := by
  have hp3 : 3 ≤ p := le_of_lt (lt_of_le_of_lt (le_max_left 3 (n + 1)) hp)
  have hpq : n + 1 < p := lt_of_le_of_lt (le_max_right 3 (n + 1)) hp
  have hp2 : 2 < p := by omega
  have key : ∀ i ∈ Finset.Icc 1 n,
      PadicIntegral p (cyclotomicTowerValue p α (2 * i)) ∧
        PadicIntegral p (cyclotomicTowerValue p (α + 1) (2 * i)) ∧
        PadicModEq p 1 (cyclotomicTowerValue p α (2 * i))
          (cyclotomicTowerValue p (α + 1) (2 * i)) := by
    intro i hi
    obtain ⟨hi1, hin⟩ := Finset.mem_Icc.mp hi
    by_cases hdiv : p - 1 ∣ 2 * i
    · -- the exceptional index: necessarily `2 i = p - 1`
      obtain ⟨c, hc⟩ := hdiv
      have hpos : 0 < 2 * i := by omega
      have hp1pos : 0 < p - 1 := by omega
      have hlt : 2 * i < (p - 1) * 2 := by omega
      rw [hc] at hlt hpos
      have hc2 : c < 2 := Nat.lt_of_mul_lt_mul_left hlt
      have hc0 : c ≠ 0 := by
        intro h0
        rw [h0, mul_zero] at hpos
        exact Nat.lt_irrefl 0 hpos
      have hc1 : c = 1 := by omega
      have h2i : p - 1 = 2 * i := by rw [hc, hc1, mul_one]
      exact hexc i α hi h2i
    · exact ⟨(main_congruence hp3 α (2 * i) ⟨by omega, ⟨i, by ring⟩, hdiv⟩).1,
        (main_congruence hp3 (α + 1) (2 * i) ⟨by omega, ⟨i, by ring⟩, hdiv⟩).1,
        padicModEq_of_le (by omega)
          (main_congruence hp3 α (2 * i) ⟨by omega, ⟨i, by ring⟩, hdiv⟩).2⟩
  exact euler_congruence_of_factors hp2 D.euler α
    (fun i hi => (key i hi).2.2) (fun i hi => (key i hi).1) (fun i hi => (key i hi).2.1)
    (D.euler_eq α) (D.euler_eq (α + 1))

/-! ### Corollary 4.12: `p`-torsion in `K_{4m-2}` -/

lemma harder_norm_natCast_lt_one_of_dvd {k : ℕ} (h : p ∣ k) : ‖(k : ℂ_[p])‖ < 1 := by
  obtain ⟨t, rfl⟩ := h
  have hk : ((p * t : ℕ) : ℂ_[p]) = (p : ℂ_[p]) * (t : ℂ_[p]) := by push_cast; ring
  rw [hk, norm_mul]
  calc ‖(p : ℂ_[p])‖ * ‖(t : ℂ_[p])‖ ≤ ‖(p : ℂ_[p])‖ * 1 :=
        mul_le_mul_of_nonneg_left (padicIntegral_natCast p t) (norm_nonneg _)
    _ = ‖(p : ℂ_[p])‖ := mul_one _
    _ < 1 := harder_norm_prime_lt_one p

/-- The arithmetic input of Corollary 4.12: if `p ∤ B_{2m}/2m` then no level of the
cyclotomic tower has `ζ_{F_α}(1-2m) ≡ 0 (mod p)`.  This is Theorem 4.1 together with
`ζ(1-2m) = -B_{2m}/2m`. -/
theorem cor_4_12_arithmetic (hp : 3 ≤ p) (m : ℕ) (hm : 1 ≤ m)
    (hdiv : ¬ (p - 1 ∣ 2 * m))
    (hB : ¬ ResidueModEq p ((bernoulli (2 * m) : ℂ_[p]) / (2 * m)) 0) (α : ℕ) :
    ¬ ResidueModEq p (cyclotomicTowerValue p α (2 * m)) 0 := by
  have hn : AdmissibleIndex p (2 * m) := ⟨by omega, ⟨m, by ring⟩, hdiv⟩
  have hn1 : 2 * m ≠ 1 := by omega
  have h0 : ¬ ResidueModEq p (cyclotomicTowerValue p 0 (2 * m)) 0 := by
    rw [cyclotomicTowerValue_zero_of_ne_one (p := p) hn1]
    simpa only [ResidueModEq, sub_zero, neg_div, norm_neg, Nat.cast_mul,
      Nat.cast_ofNat] using hB
  exact (tower_not_residue_zero_iff hp α (2 * m) hn).mpr h0

/-- `(p-1)/2 ∣ m` is the same condition as `p - 1 ∣ 2m` for odd `p`; this is the form in
which Corollary 4.12 states its hypothesis on `m`. -/
lemma half_dvd_iff {p : ℕ} [Fact p.Prime] (hp : 2 < p) (m : ℕ) :
    (p - 1) / 2 ∣ m ↔ p - 1 ∣ 2 * m := by
  have hodd : Odd p := (Fact.out : Nat.Prime p).odd_of_ne_two (by omega)
  obtain ⟨k, hk⟩ := hodd
  have hp1 : p - 1 = 2 * k := by omega
  have hkdiv : (p - 1) / 2 = k := by
    rw [hp1]
    exact Nat.mul_div_cancel_left k two_pos
  rw [hkdiv]
  constructor
  · rintro ⟨c, rfl⟩
    exact ⟨c, by rw [hp1]; ring⟩
  · rintro ⟨c, hc⟩
    refine ⟨c, ?_⟩
    rw [hp1] at hc
    have h2 : 2 * m = 2 * (k * c) := by rw [hc]; ring
    exact Nat.mul_left_cancel (by norm_num) h2

/-- The K-theoretic inputs of Corollary 4.12, isolated as an explicit structure:
`orderEven α = #K_{4m-2}(𝒪_{F_α})`, `orderOdd α = #K_{4m-1}(𝒪_{F_α})`, together with

* Wiles' theorem (Lichtenbaum's conjecture, (4.20) with (4.22)) in the only form used by
  the proof: `|ζ_{F_α}(1-2m)|` and `#K_{4m-2}/#K_{4m-1}` agree up to a power of `2`,
  which the `p`-adic norm does not see (`harder_norm_two_eq_one`);
* Lemma 4.11, `w_{2m}^{(p)}(F_α) = 1` because `p - 1 ∤ 2m`, which makes `#K_{4m-1}` a
  `p`-adic unit.

`F_α` is totally real (`−1` lies in the `(p-1)`-part of `(ℤ/p^(α+1))^×`), which is the
hypothesis under which Wiles proved (4.20). -/
structure HigherKGroupData (p m : ℕ) [Fact p.Prime] where
  orderEven : ℕ → ℕ
  orderOdd : ℕ → ℕ
  zeta : ℕ → ℂ_[p]
  zeta_eq : ∀ α, zeta α = cyclotomicTowerValue p α (2 * m)
  lichtenbaum : ∀ α, ‖zeta α‖ = ‖(orderEven α : ℂ_[p])‖ / ‖(orderOdd α : ℂ_[p])‖
  oddUnit : ∀ α, ¬ ResidueModEq p (orderOdd α : ℂ_[p]) 0

/-- **Corollary 4.12.** Let `p ≥ 5` and let `m ≥ 1` not be divisible by `(p-1)/2`. If
`p ∤ B_{2m}/2m`, then `K_{4m-2}(𝒪_{F_α})[p^∞] = 0` for every `α ≥ 1`, i.e.
`p ∤ #K_{4m-2}(𝒪_{F_α})`. -/
theorem cor_4_12 (hp : 5 ≤ p) (m : ℕ) (hm : 1 ≤ m)
    (hdiv : ¬ ((p - 1) / 2 ∣ m))
    (hB : ¬ ResidueModEq p ((bernoulli (2 * m) : ℂ_[p]) / (2 * m)) 0)
    (K : HigherKGroupData p m) {α : ℕ} (_hα : 1 ≤ α) :
    ¬ p ∣ K.orderEven α := by
  have hp3 : 3 ≤ p := by omega
  have hp2 : 2 < p := by omega
  have hdiv' : ¬ (p - 1 ∣ 2 * m) := fun h => hdiv ((half_dvd_iff hp2 m).mpr h)
  have harith : ¬ ResidueModEq p (cyclotomicTowerValue p α (2 * m)) 0 :=
    cor_4_12_arithmetic hp3 m hm hdiv' hB α
  intro hpdvd
  have hlt : ‖(K.orderEven α : ℂ_[p])‖ < 1 := harder_norm_natCast_lt_one_of_dvd hpdvd
  have hunit : ‖(K.orderOdd α : ℂ_[p])‖ = 1 := by
    refine le_antisymm (padicIntegral_natCast p _) ?_
    simpa using not_lt.mp (K.oddUnit α)
  have hzeta : ‖K.zeta α‖ < 1 := by
    rw [K.lichtenbaum α, hunit, div_one]
    exact hlt
  apply harith
  rw [← K.zeta_eq α, ResidueModEq, sub_zero]
  exact hzeta

/-! ### Concrete instances (verification of the statement layer) -/

/-- The printed hypothesis `p > max 3 (n+1)` is satisfied in the obstruction case. -/
lemma harder_source_hypothesis_obstruction : max 3 (2 + 1) < 5 := by norm_num

/-- In the obstruction case the excluded index really is divisible by `p - 1`,
while the preceding index is not. -/
lemma harder_index_obstruction_facts : 5 - 1 ∣ 2 * 2 ∧ ¬ (5 - 1 ∣ 2 * 1) := by
  constructor <;> norm_num

/-- The Fermat instance underlying the congruence of the normalisation constants at
`p = 5`: `2 ^ (5 - 1) ≡ 1 (mod 5)`, hence `2 ^ 40 ≡ 1 (mod 5)`. -/
lemma fermat_instance_five : 5 ∣ 2 ^ 4 - 1 ∧ 5 ∣ 2 ^ 40 - 1 := by
  constructor <;> norm_num

/-- The norm of a natural number in `ℂ_[p]` is its norm in `ℚ_[p]`. -/
lemma harder_norm_natCast_eq (k : ℕ) : ‖(k : ℂ_[p])‖ = ‖(k : ℚ_[p])‖ := by
  have hco : ((k : ℚ_[p]) : ℂ_[p]) = (k : ℂ_[p]) := by
    have h1 : ((k : ℚ_[p]) : ℂ_[p]) = (((k : ℕ) : PadicAlgCl p) : ℂ_[p]) := by
      rw [show ((k : ℚ_[p])) = ((k : ℕ) : ℚ_[p]) by norm_num]
      rfl
    rw [h1]
    exact PadicComplex.coe_natCast (p := p) k
  rw [← hco, PadicComplex.norm_extends']

/-- `B_2/2 = 1/12` is a `p`-adic unit for every `p ≥ 5`: the Bernoulli hypothesis of
Corollary 4.12 is satisfiable. -/
lemma not_residueModEq_bernoulli_two (hp : 5 ≤ p) :
    ¬ ResidueModEq p ((bernoulli 2 : ℂ_[p]) / (2 * 1)) 0 := by
  have hp2 : 2 < p := by omega
  rw [ResidueModEq, sub_zero, not_lt]
  have hB : (bernoulli 2 : ℂ_[p]) / (2 * 1) = (((12 : ℕ)) : ℂ_[p])⁻¹ := by
    have h6 : ((bernoulli 2 : ℚ) : ℂ_[p]) = (((6 : ℕ)) : ℂ_[p])⁻¹ := by
      rw [bernoulli_two]
      push_cast
      ring
    have h2' : (bernoulli 2 : ℂ_[p]) = ((bernoulli 2 : ℚ) : ℂ_[p]) := rfl
    rw [h2', h6]
    have h6n : (((6 : ℕ)) : ℂ_[p]) ≠ 0 := by norm_num
    field_simp
    norm_num
  rw [hB, norm_inv, harder_norm_natCast_eq (p := p) 12]
  have h12 : ‖((12 : ℕ) : ℚ_[p])‖ = 1 := by
    rw [Padic.norm_natCast_eq_one_iff]
    have hne : Nat.Prime p := Fact.out
    have h2 : p.Coprime 2 := hne.coprime_iff_not_dvd.mpr (fun h => by
      have := Nat.le_of_dvd (by norm_num) h
      omega)
    have h3 : p.Coprime 3 := hne.coprime_iff_not_dvd.mpr (fun h => by
      have := Nat.le_of_dvd (by norm_num) h
      omega)
    have h := (h2.mul_right h2).mul_right h3
    simpa using h
  rw [h12, inv_one]

/-- The hypotheses of Corollary 4.12 hold at `m = 1` and every `p ≥ 5`, so the corollary
is not vacuous there: it gives `5 ∤ #K_2(𝒪_{F_α})` for `p = 5` and every `α ≥ 1`. -/
theorem cor_4_12_hypotheses_satisfiable (hp : 5 ≤ p) :
    ¬ ((p - 1) / 2 ∣ 1) ∧ ¬ ResidueModEq p ((bernoulli 2 : ℂ_[p]) / (2 * 1)) 0 := by
  constructor
  · intro hd
    have h1 : (p - 1) / 2 ≤ 1 := Nat.le_of_dvd (by norm_num) hd
    omega
  · exact not_residueModEq_bernoulli_two hp

end
end Thesis
