import Mathlib.Tactic.Ringimport Mathlib.Tactic.Linarithimport Mathlib.Data.ZMod.Basic/-!# Round 14: the Frey curve of `{3,4,p}` and its conductor at 2 and 3For a primitive solution of `A³ + B⁴ = Cᵖ`, `A³ + Cᵖ = B⁴` or `B⁴ + Cᵖ = A³` put `a = B²` and `b = A` (first order)or `b = -A` (other two), so that `a² + b³ = s·Cᵖ` with `s = 1, 1, -1`. The Frey curve is`E_{a,b} : Y² = X³ + 3bX - 2a`, i.e. `a₁ = a₂ = a₃ = 0`, `a₄ = 3b`, `a₆ = -2a`.This file checks the algebra behind the computation of the Serre level `N = 2^{f₂} 3^{f₃}`:* the invariants `c₄ = -144b`, `c₆ = 1728a`, `Δ = -1728(a² + b³)`;* `2 ∤ C`: `v₂(Δ) = 6`; Tate's algorithm stops at type II (`f₂ = 6`) for `B` odd or `b ≡ 3 (mod 4)`, and at type III(`f₂ = 5`) for `B` even, `b ≡ 1 (mod 4)`;* `3 ∤ C`: `v₃(Δ) = 3`; the singular point is `(-a, 0)`, and `f₃ = 2` or `3` according to `9 ∣ a(a² + 3b + 2)`;* `2 ∣ C`: Kraus' condition `c₆' ≡ -1 (mod 4)` for `(c₄', c₆') = (c₄/2⁴, c₆/2⁶)`, so `E` is multiplicative at 2;* `3 ∣ C`: `B ≡ ±1 (mod 9)` and `81 ∤ c₄`, so the model is minimal and the reduction is of type `Iₙ*`;* the Ramanujan bound used for level lowering: `p ∣ (ℓ+1)² - a_ℓ²` forces `p ≤ (√ℓ + 1)²`.Modularity, Ribet's level lowering, Mazur's isogeny theorem, Tate's algorithm itself, Ogg's formula and thecomputation of the newforms are cited, not formalized.-/namespace BealFrey/-! ### Weierstrass invariants (standard formulas) -/def b2 (a₁ a₂ : ℤ) : ℤ := a₁ ^ 2 + 4 * a₂def b4 (a₁ a₃ a₄ : ℤ) : ℤ := a₁ * a₃ + 2 * a₄def b6 (a₃ a₆ : ℤ) : ℤ := a₃ ^ 2 + 4 * a₆def b8 (a₁ a₂ a₃ a₄ a₆ : ℤ) : ℤ := a₁ ^ 2 * a₆ + 4 * a₂ * a₆ - a₁ * a₃ * a₄ + a₂ * a₃ ^ 2 - a₄ ^ 2def c4 (a₁ a₂ a₃ a₄ : ℤ) : ℤ := b2 a₁ a₂ ^ 2 - 24 * b4 a₁ a₃ a₄def c6 (a₁ a₂ a₃ a₄ a₆ : ℤ) : ℤ :=-b2 a₁ a₂ ^ 3 + 36 * b2 a₁ a₂ * b4 a₁ a₃ a₄ - 216 * b6 a₃ a₆def disc (a₁ a₂ a₃ a₄ a₆ : ℤ) : ℤ :=-b2 a₁ a₂ ^ 2 * b8 a₁ a₂ a₃ a₄ a₆ - 8 * b4 a₁ a₃ a₄ ^ 3 - 27 * b6 a₃ a₆ ^ 2+ 9 * b2 a₁ a₂ * b4 a₁ a₃ a₄ * b6 a₃ a₆/-- `c₄(E_{a,b}) = -144b`. -/theorem frey_c4 (b : ℤ) : c4 0 0 0 (3 * b) = -144 * b := bysimp only [c4, b2, b4]; ring/-- `c₆(E_{a,b}) = 1728a`. -/theorem frey_c6 (a b : ℤ) : c6 0 0 0 (3 * b) (-2 * a) = 1728 * a := bysimp only [c6, b2, b4, b6]; ring/-- `Δ(E_{a,b}) = -1728(a² + b³) = -1728·s·Cᵖ`. -/theorem frey_disc (a b : ℤ) : disc 0 0 0 (3 * b) (-2 * a) = -1728 * (a ^ 2 + b ^ 3) := bysimp only [disc, b2, b4, b6, b8]; ring/-- The invariants satisfy `c₄³ - c₆² = 1728Δ`, and `j = c₄³/Δ = 1728b³/(a² + b³)`. -/theorem frey_c4c6_disc (a b : ℤ) :(-144 * b) ^ 3 - (1728 * a) ^ 2 = 1728 * (-1728 * (a ^ 2 + b ^ 3)) ∧(-144 * b) ^ 3 = 1728 * b ^ 3 * (-1728) := byconstructor <;> ring/-! ### The prime 2, `2 ∤ C` (so `a² + b³` is odd and `B`, `b` have opposite parity) -//-- `v₂(Δ) = 6` when `c = a² + b³` is odd; the model is then minimal at 2 (`v₂(Δ) < 12`). -/theorem disc_val2 (c : ℤ) (hc : ¬ (2 : ℤ) ∣ c) : (64 : ℤ) ∣ -1728 * c ∧ ¬ (128 : ℤ) ∣ -1728 * c := byrefine ⟨⟨-27 * c, by ring⟩, ?_⟩omega/-- `B = 2k + 1` odd, `b = 2m` even: the singular point mod 2 is `(0,0)`, `2 ∣ a₄`, `2 ∣ a₆`, `b₂ = 0`, and`4 ∤ a₆ = -2B²`: type II, `f₂ = v₂(Δ) = 6`. -/theorem tate2_B_odd (k m : ℤ) :(2 : ℤ) ∣ 3 * (2 * m) ∧ (2 : ℤ) ∣ -2 * (2 * k + 1) ^ 2 ∧ ¬ (4 : ℤ) ∣ -2 * (2 * k + 1) ^ 2 := byrefine ⟨⟨3 * m, by ring⟩, ⟨-(2 * k + 1) ^ 2, by ring⟩, ?_⟩have e : -2 * (2 * k + 1) ^ 2 = 4 * (-2 * k ^ 2 - 2 * k - 1) + 2 := by ringrw [e]generalize -2 * k ^ 2 - 2 * k - 1 = womega/-- `b` odd: the singular point of `E mod 2` is `(1, 0)`; the shift `X = x + 1` gives`a₂ = 3`, `a₄ = 3(1 + b)`, `a₆ = 1 + 3b - 2a` (and `b₂ = 12`, so the reduction is additive). -/theorem tate2_shift (a b x : ℤ) :(x + 1) ^ 3 + 3 * b * (x + 1) - 2 * a = x ^ 3 + 3 * x ^ 2 + 3 * (1 + b) * x + (1 + 3 * b - 2 * a) := byring/-- `B = 2k` even, `b = 4m + 3`: after the shift `2 ∣ a₄`, `2 ∣ a₆` and `4 ∤ a₆`: type II, `f₂ = 6`. -/theorem tate2_b3 (k m : ℤ) :(2 : ℤ) ∣ 3 * (1 + (4 * m + 3)) ∧ (2 : ℤ) ∣ 1 + 3 * (4 * m + 3) - 2 * (2 * k) ^ 2 ∧¬ (4 : ℤ) ∣ 1 + 3 * (4 * m + 3) - 2 * (2 * k) ^ 2 := byrefine ⟨⟨6 * m + 6, by ring⟩, ⟨6 * m + 5 - 4 * k ^ 2, by ring⟩, ?_⟩have e : 1 + 3 * (4 * m + 3) - 2 * (2 * k) ^ 2 = 4 * (2 + 3 * m - 2 * k ^ 2) + 2 := by ringrw [e]generalize 2 + 3 * m - 2 * k ^ 2 = womega/-- `B = 2k` even, `b = 4m + 1`: `4 ∣ a₆`, and `b₈ = 4a₂a₆ - a₄² = 12a₆ - 9(1 + b)²` is `≡ 4 (mod 8)`:type III, `f₂ = v₂(Δ) - 1 = 5`. -/theorem tate2_b1 (k m : ℤ) :(4 : ℤ) ∣ 1 + 3 * (4 * m + 1) - 2 * (2 * k) ^ 2 ∧¬ (8 : ℤ) ∣ 12 * (1 + 3 * (4 * m + 1) - 2 * (2 * k) ^ 2) - 9 * (1 + (4 * m + 1)) ^ 2 := byrefine ⟨⟨1 + 3 * m - 2 * k ^ 2, by ring⟩, ?_⟩have e : 12 * (1 + 3 * (4 * m + 1) - 2 * (2 * k) ^ 2) - 9 * (1 + (4 * m + 1)) ^ 2= 8 * (1 - 12 * k ^ 2 - 18 * m ^ 2) + 4 := by ringrw [e]generalize 1 - 12 * k ^ 2 - 18 * m ^ 2 = womega/-! ### The prime 3, `3 ∤ C` -//-- `v₃(Δ) = 3` when `3 ∤ c`; the model is minimal at 3. -/theorem disc_val3 (c : ℤ) (hc : ¬ (3 : ℤ) ∣ c) : (27 : ℤ) ∣ -1728 * c ∧ ¬ (81 : ℤ) ∣ -1728 * c := byrefine ⟨⟨-64 * c, by ring⟩, ?_⟩omega/-- The singular point of `E mod 3` is `(-a, 0)`; the shift `X = x - a` gives `a₂ = -3a`, `a₄ = 3a² + 3b`,`a₆ = -a(a² + 3b + 2)`. -/theorem tate3_shift (a b x : ℤ) :(x - a) ^ 3 + 3 * b * (x - a) - 2 * a= x ^ 3 - 3 * a * x ^ 2 + (3 * a ^ 2 + 3 * b) * x - a * (a ^ 2 + 3 * b + 2) := byring/-- After the shift `3 ∣ a₆` always (so Tate's step 2 applies; `b₂ = -12a`, additive reduction). -/theorem tate3_a6 (a b : ℤ) : (3 : ℤ) ∣ a * (a ^ 2 + 3 * b + 2) := byhave h : (((a * (a ^ 2 + 3 * b + 2) : ℤ)) : ZMod 3) = 0 := bypush_castgeneralize (a : ZMod 3) = xgeneralize (b : ZMod 3) = yrevert x ydecideexact_mod_cast (ZMod.intCast_zmod_eq_zero_iff_dvd _ 3).mp h/-- Tate's step 3 at 3: `f₃ = 3` (type II) iff `9 ∤ a₆`, otherwise `f₃ = 2` (type III; type IV would give `f₃ = 1`).With `a = B²` and `3 ∤ B⁴ + b³`: `9 ∣ B²(B⁴ + 3b + 2)` iff `3 ∣ B`, or `B ≡ ±2 (mod 9)` and `3 ∣ b`, or`B ≡ ±4 (mod 9)` and `b ≡ 1 (mod 3)`. (For `B ≡ ±1 (mod 9)` it never holds.) -/theorem tate3_rule : ∀ B b : ZMod 9, 3 * (B ^ 4 + b ^ 3) ≠ 0 →(B ^ 2 * (B ^ 4 + 3 * b + 2) = 0 ↔(3 * B = 0 ∨ ((B = 2 ∨ B = 7) ∧ 3 * b = 0) ∨ ((B = 4 ∨ B = 5) ∧ 3 * (b - 1) = 0))) := bydecide/-! ### `2 ∣ C` and `3 ∣ C` -//-- `2 ∣ C` (`B`, `b` odd): the scaled invariants `c₄' = c₄/2⁴ = -9b`, `c₆' = c₆/2⁶ = 27a` have`c₄'³ - c₆'² = 1728Δ'` with `Δ' = Δ/2¹²`, integral since `2⁶ ∣ a² + b³`. -/theorem kraus2_scaling (a b : ℤ) :-144 * b = 2 ^ 4 * (-9 * b) ∧ 1728 * a = 2 ^ 6 * (27 * a) ∧64 * ((-9 * b) ^ 3 - (27 * a) ^ 2) = 1728 * (-27 * (a ^ 2 + b ^ 3)) := byrefine ⟨by ring, by ring, by ring⟩/-- Kraus' condition at 2 holds: `c₆' = 27B² ≡ 3 ≡ -1 (mod 4)` for `B` odd, so `(c₄', c₆')` come from a model over`ℤ₂`; as `c₄' = -9b` is odd, `E` has multiplicative reduction at 2 with `v₂(Δ_min) = p·v₂(C) - 6`, prime to `p`. -/theorem kraus2_c6 (k : ℤ) : (27 * (2 * k + 1) ^ 2) % 4 = 3 := byhave e : 27 * (2 * k + 1) ^ 2 = 4 * (27 * k ^ 2 + 27 * k + 6) + 3 := by ringrw [e]generalize 27 * k ^ 2 + 27 * k + 6 = womega/-- `3 ∣ C` forces `9 ∣ B⁴ + b³` with `3 ∤ B`, hence `B ≡ ±1 (mod 9)`. -/theorem cube_mod9 : ∀ B b : ZMod 9, 3 * B ≠ 0 → B ^ 4 + b ^ 3 = 0 → B = 1 ∨ B = 8 := bydecide/-- `3 ∣ C` (so `3 ∤ b`): `81 ∤ c₄ = -144b`, so no rescaling by 3 exists, the model is minimal, the reduction isadditive with `v₃(j) = 6 - v₃(Δ) < 0` (type `Iₙ*`, potentially multiplicative), and `f₃ = 2`. -/theorem not_scalable3 (b : ℤ) (hb : ¬ (3 : ℤ) ∣ b) : ¬ (81 : ℤ) ∣ -144 * b := byomega/-! ### The Ramanujan bound behind level lowering -//-- If `a² ≤ 4ℓ` (Hasse–Weil / Ramanujan–Petersson for a rational eigenform) and `ℓ ≥ 2`, then `ℓ + 1 ± a > 0`,so `(ℓ+1)² - a² ≠ 0`. -/theorem ramanujan_pos (l a : ℤ) (hl : 2 ≤ l) (ha : a ^ 2 ≤ 4 * l) : 0 < l + 1 - a ∧ 0 < l + 1 + a := byhave key : ∀ x : ℤ, x ^ 2 ≤ 4 * l → x < l + 1 := byintro x hxby_contra h'have h : l + 1 ≤ x := not_lt.mp h'have h0 : (0 : ℤ) ≤ l + 1 := by omegahave h1 : (l + 1) * (l + 1) ≤ x * x := by nlinarithnlinarithhave h1 := key a hahave h2 := key (-a) (by rw [neg_sq]; exact ha)constructor <;> linarith/-- Level lowering at `ℓ ∣ C`: `a_ℓ(f) ≡ ±(ℓ + 1) (mod p)`. For a rational form, `p ∣ ℓ + 1 - a` (`a = ±a_ℓ`) gives`p ≤ ℓ + 1` or `(p - ℓ - 1)² ≤ 4ℓ`, i.e. `p ≤ (√ℓ + 1)²`. -/theorem exponent_bound (l a p : ℤ) (hl : 2 ≤ l) (ha : a ^ 2 ≤ 4 * l) (hdiv : p ∣ l + 1 - a) :p ≤ l + 1 ∨ (p - (l + 1)) ^ 2 ≤ 4 * l := byhave hpos := (ramanujan_pos l a hl ha).1have hle : p ≤ l + 1 - a := Int.le_of_dvd hpos hdivby_cases h : p ≤ l + 1· exact Or.inl h· righthave h' : l + 1 < p := not_le.mp hhave h1 : 0 ≤ p - (l + 1) := by linarithhave h2 : p - (l + 1) ≤ -a := by linarithnlinarith/-- The same for a prime `p ∣ (ℓ + 1)² - a²`. -/theorem exponent_bound_sq (l a p : ℤ) (hl : 2 ≤ l) (ha : a ^ 2 ≤ 4 * l) (hp : Prime p)(hdiv : p ∣ (l + 1) ^ 2 - a ^ 2) : p ≤ l + 1 ∨ (p - (l + 1)) ^ 2 ≤ 4 * l := byhave e : (l + 1) ^ 2 - a ^ 2 = (l + 1 - a) * (l + 1 - -a) := by ringrw [e] at hdivrcases hp.dvd_or_dvd hdiv with h | h· exact exponent_bound l a p hl ha h· exact exponent_bound l (-a) p hl (by rw [neg_sq]; exact ha) hend BealFrey#print axioms BealFrey.frey_disc#print axioms BealFrey.tate2_b1#print axioms BealFrey.tate3_rule#print axioms BealFrey.tate3_a6#print axioms BealFrey.kraus2_c6#print axioms BealFrey.cube_mod9#print axioms BealFrey.exponent_bound_sq