import Thesis.Iwasawa

/-!
# The two Lemmas A and C of Theorem 4.3, machine-checked

`source/chapter-4-proof-reconstruction.md` reduces Theorem 4.3 of Bhattacharya's
thesis to three ingredients, of which two are missing from Mathlib (obligations O21
and O23 in that document's ledger):

**Lemma A (O21).** Reduction modulo the maximal ideal `𝔭` of the ring of integers of
`ℂ_[p]` is injective on roots of unity of order prime to `p`; consequently two
Dirichlet characters modulo `p` whose values are congruent mod `𝔭` are equal.  This
is what makes the hypothesis `χ₁ ≡ χ₂ (mod 𝔭)` of Theorem 4.3 force `θ₁ = θ₂` for
the tame parts.

**Lemma C (O23).** If `F ∈ ℤ_p⟦T⟧` and `x, y` lie in the open unit ball (the maximal
ideal of `𝒪`), then `F(x) − F(y) ∈ (x − y)𝒪`; quantitatively `‖F(x) − F(y)‖ ≤ ‖x − y‖`.
That is the continuity step the printed proof of Theorem 4.3 omits.

Everything here is proved from Mathlib + `Thesis/Prelude.lean`; there are no axioms
and no `sorry`.  The proofs are phrased so that they can be pasted into
`Thesis/Iwasawa.lean` (the declarations are `Thesis`-namespaced and use the project's
`padicIntToCp`, `evalPowerSeries` and `ResidueModEq`).
-/

namespace Thesis

noncomputable section

open scoped BigOperators Topology

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

/-! ## Ultrametric bookkeeping in `ℂ_[p]` -/

/-- A finite sum in an ultrametric normed field is bounded by the maximum of the
norms of its terms. -/
lemma norm_sum_le_of_ultrametric {ι : Type*} (s : Finset ι) (f : ι → ℂ_[p]) {r : ℝ}
    (hr : 0 ≤ r) (h : ∀ i ∈ s, ‖f i‖ ≤ r) : ‖∑ i ∈ s, f i‖ ≤ r := by
  classical
  induction s using Finset.induction_on with
  | empty => simpa using hr
  | insert a s ha ih =>
    rw [Finset.sum_insert ha]
    refine (PadicComplex.isNonarchimedean p (f a) (∑ i ∈ s, f i)).trans (max_le ?_ ?_)
    · exact h a (Finset.mem_insert_self a s)
    · exact ih fun i hi => h i (Finset.mem_insert_of_mem hi)

/-- In an ultrametric normed field, a strictly smaller summand does not change the
norm of a sum. -/
lemma norm_add_eq_of_norm_lt {x y : ℂ_[p]} (h : ‖y‖ < ‖x‖) : ‖x + y‖ = ‖x‖ := by
  have hne : dist (x + y) y ≠ dist y 0 := by
    rw [dist_eq_norm, dist_eq_norm]
    simp only [add_sub_cancel_right, sub_zero]
    exact h.ne'
  have hmain := IsUltrametricDist.dist_eq_max_of_dist_ne_dist (x := x + y) (y := y) (z := 0) hne
  rw [dist_eq_norm, dist_eq_norm, add_sub_cancel_right, sub_zero, dist_zero_right] at hmain
  rw [hmain, max_eq_left h.le]

/-- A root of unity in `ℂ_[p]` has norm one. -/
lemma norm_eq_one_of_pow_eq_one {ζ : ℂ_[p]} {n : ℕ} (hn : n ≠ 0) (h : ζ ^ n = 1) :
    ‖ζ‖ = 1 := by
  have hpow : ‖ζ‖ ^ n = 1 := by rw [← norm_pow, h, norm_one]
  have hpow' : ‖ζ‖ ^ n = (1 : ℝ) ^ n := by rw [one_pow, hpow]
  rcases (norm_nonneg ζ).lt_or_eq with hpos | hzero
  · exact pow_left_inj₀ hpos.le zero_le_one hn |>.mp hpow'
  · exfalso
    rw [← hzero, zero_pow hn] at hpow
    exact zero_ne_one hpow

/-- The norm of `n` inside `ℂ_[p]`, for `p ∤ n`. -/
lemma norm_natCast_eq_one_of_not_dvd {n : ℕ} (hn : ¬ p ∣ n) : ‖(n : ℂ_[p])‖ = 1 := by
  have hnat : ‖(n : ℚ_[p])‖ = 1 := by
    rw [Padic.norm_natCast_eq_one_iff]
    exact (Fact.out : p.Prime).coprime_iff_not_dvd.mpr hn
  rw [show (n : ℂ_[p]) = ((n : ℚ_[p]) : ℂ_[p]) by norm_cast,
    PadicComplex.norm_extends', hnat]

/-! ## Lemma A: reduction mod `𝔭` is injective on prime-to-`p` roots of unity -/

/-- **Lemma A.**  A root of unity of order prime to `p` in `ℂ_[p]` which is congruent
to `1` modulo the maximal ideal is equal to `1`. -/
theorem eq_one_of_pow_eq_one_of_not_dvd {ζ : ℂ_[p]} {n : ℕ} (hn : ¬ p ∣ n)
    (hζ : ζ ^ n = 1) (h : ‖ζ - 1‖ < 1) : ζ = 1 := by
  have hn0 : n ≠ 0 := fun h0 => hn (h0 ▸ dvd_zero p)
  have hζnorm : ‖ζ‖ = 1 := norm_eq_one_of_pow_eq_one hn0 hζ
  by_contra hne
  have hpos : 0 < ‖ζ - 1‖ := norm_pos_iff.mpr (sub_ne_zero.mpr hne)
  have hfac : (∑ i ∈ Finset.range n, ζ ^ i) * (ζ - 1) = 0 := by
    rw [geom_sum_mul, hζ, sub_self]
  have hS : (∑ i ∈ Finset.range n, ζ ^ i) = 0 := by
    rcases mul_eq_zero.mp hfac with h' | h'
    · exact h'
    · exact absurd h' (sub_ne_zero.mpr hne)
  have hterm : ∀ i ∈ Finset.range n, ‖ζ ^ i - 1‖ ≤ ‖ζ - 1‖ := by
    intro i _
    rcases Nat.eq_zero_or_pos i with h0 | hpos'
    · subst h0
      simp
    · have hgeom : ‖∑ j ∈ Finset.range i, ζ ^ j‖ ≤ 1 := by
        refine norm_sum_le_of_ultrametric _ _ zero_le_one ?_
        intro j _
        rw [norm_pow, hζnorm, one_pow]
      have hfac' : ζ ^ i - 1 = (ζ - 1) * ∑ j ∈ Finset.range i, ζ ^ j := by
        rw [mul_comm, geom_sum_mul]
      rw [hfac', norm_mul]
      calc ‖ζ - 1‖ * ‖∑ j ∈ Finset.range i, ζ ^ j‖ ≤ ‖ζ - 1‖ * 1 :=
            mul_le_mul_of_nonneg_left hgeom (norm_nonneg _)
        _ = ‖ζ - 1‖ := mul_one _
  have hdiff : ‖(∑ i ∈ Finset.range n, ζ ^ i) - (n : ℂ_[p])‖ < 1 := by
    have hsplit : (∑ i ∈ Finset.range n, ζ ^ i) - (n : ℂ_[p]) =
        ∑ i ∈ Finset.range n, (ζ ^ i - 1) := by
      simp [Finset.sum_sub_distrib, Finset.sum_const, Finset.card_range]
    rw [hsplit]
    exact (norm_sum_le_of_ultrametric _ _ hpos.le hterm).trans_lt h
  rw [hS] at hdiff
  have hnormn : ‖(n : ℂ_[p])‖ = 1 := norm_natCast_eq_one_of_not_dvd hn
  rw [zero_sub, norm_neg, hnormn] at hdiff
  exact lt_irrefl 1 hdiff

/-- Two roots of unity of the same order prime to `p` that are congruent modulo the
maximal ideal are equal. -/
theorem eq_of_pow_eq_one_of_norm_sub_lt_one {ζ₁ ζ₂ : ℂ_[p]} {k : ℕ} (hk : ¬ p ∣ k)
    (h₁ : ζ₁ ^ k = 1) (h₂ : ζ₂ ^ k = 1) (h : ‖ζ₁ - ζ₂‖ < 1) : ζ₁ = ζ₂ := by
  have hk0 : k ≠ 0 := fun h0 => hk (h0 ▸ dvd_zero p)
  have hζ₂0 : ζ₂ ≠ 0 := by
    rintro rfl
    rw [zero_pow hk0] at h₂
    exact zero_ne_one h₂
  have hζ₂ : ‖ζ₂‖ = 1 := norm_eq_one_of_pow_eq_one hk0 h₂
  have hratio : (ζ₁ / ζ₂) ^ k = 1 := by
    rw [div_pow, h₁, h₂, div_one]
  have hdist : ‖ζ₁ / ζ₂ - 1‖ < 1 := by
    have hrewrite : ζ₁ / ζ₂ - 1 = (ζ₁ - ζ₂) / ζ₂ := by
      field_simp
    rw [hrewrite, norm_div, hζ₂]
    simpa using h
  have hone : ζ₁ / ζ₂ = 1 := eq_one_of_pow_eq_one_of_not_dvd hk hratio hdist
  rw [div_eq_one_iff_eq hζ₂0] at hone
  exact hone

/-- **Lemma A, character form.**  Two Dirichlet characters modulo `p` whose values are
congruent modulo the maximal ideal are equal; in particular reduction modulo the
maximal ideal is injective on the character group of `(ℤ/p)^×`. -/
theorem dirichletCharacter_eq_of_residueModEq (θ₁ θ₂ : DirichletCharacter ℂ_[p] p)
    (h : ∀ a : ZMod p, ResidueModEq p (θ₁ a) (θ₂ a)) : θ₁ = θ₂ := by
  classical
  have hcard : Fintype.card (ZMod p)ˣ = p - 1 := by
    rw [ZMod.card_units_eq_totient, Nat.totient_prime (Fact.out : p.Prime)]
  have hvalues : ∀ u : (ZMod p)ˣ, θ₁ (u : ZMod p) = θ₂ (u : ZMod p) := by
    intro u
    have hpow₁ : θ₁ (u : ZMod p) ^ (p - 1) = 1 := by
      have hpow := congrArg (fun ψ : DirichletCharacter ℂ_[p] p => ψ u) θ₁.pow_card_eq_one
      rw [MulChar.pow_apply_coe, MulChar.one_apply_coe, hcard] at hpow
      exact hpow
    have hpow₂ : θ₂ (u : ZMod p) ^ (p - 1) = 1 := by
      have hpow := congrArg (fun ψ : DirichletCharacter ℂ_[p] p => ψ u) θ₂.pow_card_eq_one
      rw [MulChar.pow_apply_coe, MulChar.one_apply_coe, hcard] at hpow
      exact hpow
    have hnotdvd : ¬ p ∣ (p - 1) := by
      intro hd
      have hp := (Fact.out : p.Prime).two_le
      exact absurd (Nat.le_of_dvd (by omega) hd) (by omega)
    exact eq_of_pow_eq_one_of_norm_sub_lt_one hnotdvd hpow₁ hpow₂ (h (u : ZMod p))
  exact MulChar.ext hvalues

/-! ## Lemma C: evaluation of an integral power series is `1`-Lipschitz -/

lemma norm_padicIntToCp_le_one (x : ℤ_[p]) : ‖padicIntToCp x‖ ≤ 1 := by
  rw [padicIntToCp, PadicComplex.norm_extends', ← PadicInt.norm_def]
  exact PadicInt.norm_le_one x

/-- **Ultrametric bound for an infinite sum**: a summable sequence in `ℂ_[p]` whose
terms are all bounded by `C` has `tsum` bounded by `C`. -/
lemma norm_tsum_le_of_forall_le {f : ℕ → ℂ_[p]} {C : ℝ}
    (hs : Summable f) (hC : 0 ≤ C) (h : ∀ i, ‖f i‖ ≤ C) : ‖∑' i, f i‖ ≤ C := by
  have hpartial : ∀ s : Finset ℕ, ‖∑ i ∈ s, f i‖ ≤ C := fun s =>
    norm_sum_le_of_ultrametric s f hC fun i _ => h i
  have htend : Filter.Tendsto (fun n : ℕ => ‖∑ i ∈ Finset.range n, f i‖) Filter.atTop
      (𝓝 ‖∑' i, f i‖) :=
    (continuous_norm.tendsto _).comp hs.hasSum.tendsto_sum_nat
  exact le_of_tendsto_of_tendsto htend tendsto_const_nhds (Filter.Eventually.of_forall fun n => hpartial _)

/-- The defining series of `evalPowerSeries F a` is summable for `‖a‖ < 1`. -/
lemma summable_evalPowerSeries (F : PowerSeries ℤ_[p]) {a : ℂ_[p]} (ha : ‖a‖ < 1) :
    Summable fun k : ℕ => padicIntToCp (PowerSeries.coeff k F) * a ^ k := by
  refine Summable.of_norm_bounded (g := fun k : ℕ => ‖a‖ ^ k)
    (summable_geometric_of_norm_lt_one (x := (‖a‖ : ℝ)) (by simpa using ha)) ?_
  intro k
  rw [norm_mul, norm_pow]
  calc ‖padicIntToCp (PowerSeries.coeff k F)‖ * ‖a‖ ^ k ≤ 1 * ‖a‖ ^ k :=
        mul_le_mul_of_nonneg_right (norm_padicIntToCp_le_one _) (by positivity)
    _ = ‖a‖ ^ k := one_mul _

/-- The divided-difference sum `∑_{i<k} x^i y^{k-1-i}` is bounded by
`(max ‖x‖ ‖y‖)^(k-1)`. -/
lemma norm_geomDiff_le {x y : ℂ_[p]} (hx : ‖x‖ ≤ 1) (hy : ‖y‖ ≤ 1) (k : ℕ) :
    ‖∑ i ∈ Finset.range k, x ^ i * y ^ (k - 1 - i)‖ ≤ (max ‖x‖ ‖y‖) ^ (k - 1) := by
  have hR0 : (0 : ℝ) ≤ max ‖x‖ ‖y‖ := le_trans (norm_nonneg x) (le_max_left _ _)
  have hR1 : max ‖x‖ ‖y‖ ≤ 1 := max_le hx hy
  rcases Nat.eq_zero_or_pos k with hk | hk
  · subst hk
    simp
  · have hterm : ∀ i ∈ Finset.range k, ‖x ^ i * y ^ (k - 1 - i)‖ ≤ (max ‖x‖ ‖y‖) ^ (k - 1) := by
      intro i hi
      have hik : i < k := Finset.mem_range.mp hi
      rw [norm_mul, norm_pow, norm_pow]
      calc ‖x‖ ^ i * ‖y‖ ^ (k - 1 - i)
          ≤ (max ‖x‖ ‖y‖) ^ i * (max ‖x‖ ‖y‖) ^ (k - 1 - i) :=
            mul_le_mul (pow_le_pow_left₀ (norm_nonneg _) (le_max_left _ _) _)
              (pow_le_pow_left₀ (norm_nonneg _) (le_max_right _ _) _)
              (by positivity) (by positivity)
        _ = (max ‖x‖ ‖y‖) ^ (i + (k - 1 - i)) := by rw [← pow_add]
        _ = (max ‖x‖ ‖y‖) ^ (k - 1) := by rw [show i + (k - 1 - i) = k - 1 by omega]
    exact norm_sum_le_of_ultrametric _ _ (by positivity) hterm

/-- **Lemma C.**  Evaluation of an integral power series is `1`-Lipschitz on the open
unit ball. -/
theorem evalPowerSeries_sub_norm_le (F : PowerSeries ℤ_[p]) {x y : ℂ_[p]}
    (hx : ‖x‖ < 1) (hy : ‖y‖ < 1) :
    ‖evalPowerSeries F x - evalPowerSeries F y‖ ≤ ‖x - y‖ := by
  classical
  set a : ℕ → ℂ_[p] := fun k => padicIntToCp (PowerSeries.coeff k F) with ha
  have hSx : Summable fun k : ℕ => a k * x ^ k := summable_evalPowerSeries F hx
  have hSy : Summable fun k : ℕ => a k * y ^ k := summable_evalPowerSeries F hy
  have hgeom1 : ∀ k : ℕ, ‖∑ i ∈ Finset.range k, x ^ i * y ^ (k - 1 - i)‖ ≤ 1 := by
    intro k
    refine (norm_geomDiff_le hx.le hy.le k).trans ?_
    exact pow_le_one₀ (le_trans (norm_nonneg _) (le_max_left _ _)) (max_le hx.le hy.le)
  have hterm : ∀ k : ℕ, ‖a k * x ^ k - a k * y ^ k‖ ≤ ‖x - y‖ := by
    intro k
    have hfac : a k * x ^ k - a k * y ^ k =
        a k * ((∑ i ∈ Finset.range k, x ^ i * y ^ (k - 1 - i)) * (x - y)) := by
      rw [geom_sum₂_mul]
      ring
    rw [hfac, norm_mul, norm_mul]
    calc ‖a k‖ * (‖∑ i ∈ Finset.range k, x ^ i * y ^ (k - 1 - i)‖ * ‖x - y‖)
        ≤ 1 * (1 * ‖x - y‖) := by
          refine mul_le_mul (norm_padicIntToCp_le_one _) ?_ (by positivity) zero_le_one
          exact mul_le_mul (hgeom1 k) le_rfl (norm_nonneg _) zero_le_one
      _ = ‖x - y‖ := by ring
  have hkey : evalPowerSeries F x - evalPowerSeries F y =
      ∑' k : ℕ, (a k * x ^ k - a k * y ^ k) := by
    have h₁ : evalPowerSeries F x = ∑' k : ℕ, a k * x ^ k := rfl
    have h₂ : evalPowerSeries F y = ∑' k : ℕ, a k * y ^ k := rfl
    rw [h₁, h₂, hSx.tsum_sub hSy]
  rw [hkey]
  exact norm_tsum_le_of_forall_le (hSx.sub hSy) (norm_nonneg _) hterm

/-! ## `p`-power roots of unity are congruent to `1` modulo `𝔭`

This is the second half of the tame-part collapse: it lets one strip the `p`-power
order part `ψ` of a character from a congruence `χ₁ ≡ χ₂ (mod 𝔭)`.
-/

lemma norm_intCast_le_one (z : ℤ) : ‖(z : ℂ_[p])‖ ≤ 1 := by
  rw [show (z : ℂ_[p]) = ((z : ℚ_[p]) : ℂ_[p]) from
      (map_intCast (algebraMap ℚ_[p] ℂ_[p]) z).symm,
    PadicComplex.norm_extends']
  exact Padic.norm_int_le_one z

/-- If all coefficients of `P : ℤ[X]` are divisible by `p`, then `P` is `p` times an
integral polynomial, whence its value at a point of the closed unit ball has norm at
most `‖p‖`. -/
lemma eval₂_norm_le_of_coeff_dvd {P : Polynomial ℤ} (hdiv : ∀ n, (p : ℤ) ∣ P.coeff n)
    {ζ : ℂ_[p]} (hζ : ‖ζ‖ ≤ 1) :
    ‖P.eval₂ (Int.castRingHom ℂ_[p]) ζ‖ ≤ ‖(p : ℂ_[p])‖ := by
  choose e he using hdiv
  rw [Polynomial.eval₂_eq_sum, Polynomial.sum_def]
  have hfac : ∀ n : ℕ, ((P.coeff n : ℤ) : ℂ_[p]) = (p : ℂ_[p]) * ((e n : ℤ) : ℂ_[p]) := by
    intro n
    rw [he n]
    push_cast
    ring
  have hterm : ∀ n ∈ P.support, ‖((e n : ℤ) : ℂ_[p]) * ζ ^ n‖ ≤ 1 := by
    intro n _
    rw [norm_mul, norm_pow]
    calc ‖((e n : ℤ) : ℂ_[p])‖ * ‖ζ‖ ^ n ≤ 1 * 1 :=
          mul_le_mul (norm_intCast_le_one _) (pow_le_one₀ (norm_nonneg _) hζ) (by positivity)
            zero_le_one
      _ = 1 := by norm_num
  have hinner : ‖∑ n ∈ P.support, ((e n : ℤ) : ℂ_[p]) * ζ ^ n‖ ≤ 1 :=
    norm_sum_le_of_ultrametric _ _ zero_le_one hterm
  have hsum : (∑ n ∈ P.support, (Int.castRingHom ℂ_[p]) (P.coeff n) * ζ ^ n) =
      (p : ℂ_[p]) * ∑ n ∈ P.support, ((e n : ℤ) : ℂ_[p]) * ζ ^ n := by
    rw [Finset.mul_sum]
    refine Finset.sum_congr rfl ?_
    intro n _
    rw [show (Int.castRingHom ℂ_[p]) (P.coeff n) = ((P.coeff n : ℤ) : ℂ_[p]) from rfl, hfac n]
    ring
  rw [hsum, norm_mul]
  calc ‖(p : ℂ_[p])‖ * ‖∑ n ∈ P.support, ((e n : ℤ) : ℂ_[p]) * ζ ^ n‖ ≤ ‖(p : ℂ_[p])‖ * 1 :=
        mul_le_mul_of_nonneg_left hinner (norm_nonneg _)
    _ = ‖(p : ℂ_[p])‖ := mul_one _

/-- The coefficients of `X ^ (p ^ k) - 1 - (X - 1) ^ (p ^ k)` are all divisible by `p`
(for odd `p`): modulo `p` the two terms coincide by the Frobenius identity. -/
lemma coeff_dvd_xpow_sub_X_sub_one_pow (hp2 : p ≠ 2) (k : ℕ) :
    ∀ n, (p : ℤ) ∣ (Polynomial.X ^ (p ^ k) - 1 - (Polynomial.X - 1) ^ (p ^ k) :
      Polynomial ℤ).coeff n := by
  set P : Polynomial ℤ := Polynomial.X ^ (p ^ k) - 1 - (Polynomial.X - 1) ^ (p ^ k) with hP
  have hmap : P.map (Int.castRingHom (ZMod p)) = 0 := by
    rw [hP]
    have hpodd : Odd p := (Fact.out : p.Prime).odd_of_ne_two hp2
    have hodd : Odd (p ^ k) := hpodd.pow
    have hchar : (Polynomial.X - 1 : Polynomial (ZMod p)) ^ (p ^ k) =
        Polynomial.X ^ (p ^ k) - 1 := by
      have h := add_pow_char_pow (Polynomial.X : Polynomial (ZMod p)) (-1) p k
      rw [show (Polynomial.X + (-1) : Polynomial (ZMod p)) = Polynomial.X - 1 by ring,
        Odd.neg_one_pow hodd] at h
      simpa [sub_eq_add_neg] using h
    simp only [Polynomial.map_sub, Polynomial.map_pow, Polynomial.map_one, Polynomial.map_X]
    rw [hchar]
    ring
  intro n
  have h := congrArg (fun Q : Polynomial (ZMod p) => Q.coeff n) hmap
  simp only [Polynomial.coeff_map, Polynomial.coeff_zero] at h
  exact (ZMod.intCast_zmod_eq_zero_iff_dvd (P.coeff n) p).mp h

/-- **`p`-power roots of unity are `≡ 1 (mod 𝔭)`** in `ℂ_[p]`, for `p` odd. -/
theorem norm_sub_one_lt_one_of_pow_prime_pow_eq_one (hp2 : p ≠ 2) {ζ : ℂ_[p]} {k : ℕ}
    (hζ : ζ ^ (p ^ k) = 1) : ‖ζ - 1‖ < 1 := by
  have hpne0 : p ^ k ≠ 0 := pow_ne_zero _ (Fact.out : p.Prime).ne_zero
  have hζnorm : ‖ζ‖ = 1 := norm_eq_one_of_pow_eq_one hpne0 hζ
  have hval : (Polynomial.X ^ (p ^ k) - 1 - (Polynomial.X - 1) ^ (p ^ k) :
      Polynomial ℤ).eval₂ (Int.castRingHom ℂ_[p]) ζ = -((ζ - 1) ^ (p ^ k)) := by
    simp only [Polynomial.eval₂_sub, Polynomial.eval₂_pow, Polynomial.eval₂_one,
      Polynomial.eval₂_X]
    rw [hζ]
    ring
  have hb := eval₂_norm_le_of_coeff_dvd (p := p) (coeff_dvd_xpow_sub_X_sub_one_pow hp2 k)
    (ζ := ζ) hζnorm.le
  rw [hval, norm_neg, norm_pow] at hb
  have hnormp : ‖(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 : p.Prime).one_lt)
  have hpow : ‖ζ - 1‖ ^ (p ^ k) < 1 := lt_of_le_of_lt hb hnormp
  by_contra hcon
  push Not at hcon
  have : 1 ≤ ‖ζ - 1‖ ^ (p ^ k) := one_le_pow₀ hcon
  exact absurd hpow (not_lt.mpr this)

/-- **Tame-part collapse.**  If a product `θψ` with `θ` of order dividing `p−1` and `ψ`
of `p`-power order is congruent modulo `𝔭` to another such product `θ'ψ'`, then the tame
parts `θ` and `θ'` are equal.  This is the step that removes the (unnecessary, and as
printed incorrect) Lemmas 4.9–4.10 from the proof of Theorem 4.3. -/
theorem tame_part_eq_of_mul_residueModEq (hp2 : p ≠ 2) {θ₁ θ₂ ψ₁ ψ₂ : ℂ_[p]} {k₁ k₂ : ℕ}
    (hθ₁ : θ₁ ^ (p - 1) = 1) (hθ₂ : θ₂ ^ (p - 1) = 1)
    (hψ₁ : ψ₁ ^ (p ^ k₁) = 1) (hψ₂ : ψ₂ ^ (p ^ k₂) = 1)
    (h : ‖θ₁ * ψ₁ - θ₂ * ψ₂‖ < 1) : θ₁ = θ₂ := by
  have hpne : p ≠ 0 := (Fact.out : p.Prime).ne_zero
  have hn : ¬ p ∣ (p - 1) := by
    intro hd
    have hp := (Fact.out : p.Prime).two_le
    exact absurd (Nat.le_of_dvd (by omega) hd) (by omega)
  have hθnorm₁ : ‖θ₁‖ = 1 := norm_eq_one_of_pow_eq_one (Nat.sub_ne_zero_of_lt (Fact.out : p.Prime).one_lt) hθ₁
  have hθnorm₂ : ‖θ₂‖ = 1 := norm_eq_one_of_pow_eq_one (Nat.sub_ne_zero_of_lt (Fact.out : p.Prime).one_lt) hθ₂
  have hψ₁' : ‖ψ₁ - 1‖ < 1 := norm_sub_one_lt_one_of_pow_prime_pow_eq_one hp2 hψ₁
  have hψ₂' : ‖ψ₂ - 1‖ < 1 := norm_sub_one_lt_one_of_pow_prime_pow_eq_one hp2 hψ₂
  have hdecomp : θ₁ - θ₂ =
      (θ₁ * ψ₁ - θ₂ * ψ₂) + θ₂ * (ψ₂ - 1) - θ₁ * (ψ₁ - 1) := by ring
  have hb₂ : ‖θ₂ * (ψ₂ - 1)‖ < 1 := by
    rw [norm_mul, hθnorm₂, one_mul]
    exact hψ₂'
  have hb₁ : ‖θ₁ * (ψ₁ - 1)‖ < 1 := by
    rw [norm_mul, hθnorm₁, one_mul]
    exact hψ₁'
  have hXY : ‖(θ₁ * ψ₁ - θ₂ * ψ₂) + θ₂ * (ψ₂ - 1)‖ < 1 :=
    lt_of_le_of_lt (PadicComplex.isNonarchimedean p _ _) (max_lt h hb₂)
  have hfinal : ‖θ₁ - θ₂‖ < 1 := by
    rw [hdecomp, sub_eq_add_neg]
    refine lt_of_le_of_lt (PadicComplex.isNonarchimedean p _ _) (max_lt hXY ?_)
    rw [norm_neg]
    exact hb₁
  exact eq_of_pow_eq_one_of_norm_sub_lt_one hn hθ₁ hθ₂ hfinal

/-- **Lemma C, congruence form.**  Congruent points in the open unit ball have
congruent values under an integral power series. -/
theorem evalPowerSeries_residueModEq (F : PowerSeries ℤ_[p]) {x y : ℂ_[p]}
    (hx : ‖x‖ < 1) (hy : ‖y‖ < 1) (h : ResidueModEq p x y) :
    ResidueModEq p (evalPowerSeries F x) (evalPowerSeries F y) :=
  lt_of_le_of_lt (evalPowerSeries_sub_norm_le F hx hy) h

/-- **Lemma C at the thesis's shifted series `F_n` of equation (4.3).** -/
theorem shiftPowerSeries_eval_residueModEq {n : ℕ} (F : ShiftPowerSeries p n) {x y : ℂ_[p]}
    (hx : ‖x‖ < 1) (hy : ‖y‖ < 1) (h : ResidueModEq p x y) :
    ResidueModEq p (F.eval x) (F.eval y) :=
  evalPowerSeries_residueModEq F.series hx hy h

/-! ## Bridge to the project's decomposition `χ = combineFirstSecondKind m θ ψ` -/

/-- Evaluation of the project's `combineFirstSecondKind` at a unit: it is the product of
the tame part (evaluated at the residue class mod `p`) and the `p`-power part. -/
lemma combineFirstSecondKind_apply {m : ℕ} (θ : DirichletCharacter ℂ_[p] p)
    (ψ : DirichletCharacter ℂ_[p] (p ^ (m + 1))) (u : (ZMod (p ^ (m + 1)))ˣ) :
    combineFirstSecondKind m θ ψ ((u : ZMod (p ^ (m + 1)))) =
      θ (ZMod.cast ((u : ZMod (p ^ (m + 1))))) * ψ ((u : ZMod (p ^ (m + 1)))) := by
  rw [combineFirstSecondKind, MulChar.mul_apply, liftFirstKind,
    DirichletCharacter.changeLevel_eq_cast_of_dvd (χ := θ)
      (hm := dvd_pow_self p (Nat.add_one_ne_zero m)) (a := u)]

end

end Thesis
