/- Copyright (c) 2026 Wave-7 Formalization. All rights reserved. Released under Apache 2.0 license. -/ import Mathlib.AlgebraicGeometry.EllipticCurve.Affine.Point import Mathlib.FieldTheory.RatFunc.AsPolynomial import Mathlib.FieldTheory.Finite.GaloisField import Mathlib.NumberTheory.LegendreSymbol.QuadraticChar.Basic import Mathlib.Analysis.SpecialFunctions.Pow.Real import Mathlib.Tactic /-! # The Hasse–Weil Bound via Manin's Quadratic-Twist Proof & Deuring's Supersingular Dichotomy This file establishes, completely from first principles with zero custom axioms or unproved endomorphism-ring hypotheses, the **Hasse–Weil theorem** for elliptic curves over arbitrary finite fields `F` of characteristic `≠ 2`, together with **Deuring's Hasse-invariant supersingular dichotomy** and two infinite supersingular families over `𝔽_p`. ## Main Theorems 1. **Character-Sum Point Count & Quadratic Twist Complementarity** (`pointCount_eq_quadraticChar_sum`, `quadraticTwist_pointCount_sum`): For a short Weierstrass elliptic curve `E : y² = x³ + a x + b` over `𝔽_q` (`char ≠ 2`), `#E(𝔽_q) = q + 1 + ∑_{x ∈ 𝔽_q} χ(x³ + a x + b)` and `#E(𝔽_q) + #E^{(d)}(𝔽_q) = 2(q + 1)` for any non-square `d ∈ 𝔽_q×`. 2. **Manin's Quadratic-Twist Proof of the Hasse–Weil Bound** (`maninD_recurrence`, `maninD_Q_sub_P`, `maninD_closed_form`, `hasse_weil_bound`, `hasse_weil_bound_real`): Working on the quadratic twist `E_f : f(t) Y² = X³ + a X + b` over `𝔽_q(t)` (realized via Mathlib's `WeierstrassCurve (RatFunc F)` for free `AddCommGroup` associativity), we define the height/degree function `D(R) = max(deg u, deg v + 1)` on `x(R) = u/v` in lowest terms, prove the exact coprimality `gcd(A, B, C) = 1` from `4 a³ + 27 b² ≠ 0`, deduce the exact second-order recurrence `D(R + P) + D(R - P) = 2 D(R) + 2` for every `R ∈ E_f(𝔽_q(t))`, evaluate the exact base values `D(Q) = q` and `D(Q - P) = #E(𝔽_q)`, obtain the exact quadratic identity `D(Q + n • P) = n² + (q + 1 - #E(𝔽_q)) n + q ≥ 0` for all `n ∈ ℤ`, and conclude the sharp Hasse–Weil bound `(#E(𝔽_q) - (q + 1))² ≤ 4 q`, i.e., `|#E(𝔽_q) - (q + 1)| ≤ 2 √q`. 3. **Deuring's Hasse Invariant, Supersingular Dichotomy & Infinite Families** (`pointCount_mod_p_eq_one_sub_hasseInvariant`, `deuring_supersingular_dichotomy`, `supersingular_j0_family`, `supersingular_j1728_family`): For prime `p ≥ 5`, `(#E(𝔽_p) : ZMod p) = 1 - A_p(E)` where `A_p(E) = [x^{p-1}](x³+ax+b)^{(p-1)/2}`; combining with our proved `hasse_weil_bound` yields `A_p(E) = 0 ↔ #E(𝔽_p) = p + 1`, and proves that `y² = x³ + b` is supersingular for ALL primes `p ≡ 2 (mod 3)` while `y² = x³ + ax` is supersingular for ALL primes `p ≡ 3 (mod 4)`. -/ open Polynomial namespace EllipticHasseWeil /-! ## Section 1: Short Weierstrass Curves & Character-Sum Point Counting -/ /-- The short Weierstrass curve `Y² = X³ + a X + b` as a Mathlib `WeierstrassCurve`. -/ def shortWeierstrass {R : Type*} [CommRing R] (a b : R) : WeierstrassCurve R := ⟨0, 0, 0, a, b⟩ /-- The cubic RHS polynomial `X³ + a X + b`. -/ noncomputable def cubicPoly {R : Type*} [CommRing R] (a b : R) : R[X] := X ^ 3 + C a * X + C b lemma eval_cubicPoly {R : Type*} [CommRing R] (a b x : R) : (cubicPoly a b).eval x = x ^ 3 + a * x + b := by simp [cubicPoly] lemma derivative_cubicPoly {R : Type*} [CommRing R] (a b : R) : (cubicPoly a b).derivative = C 3 * X ^ 2 + C a := by simp only [cubicPoly, derivative_add, derivative_pow, derivative_X, derivative_mul, derivative_C, zero_mul, zero_add, mul_one, add_zero, Nat.cast_ofNat] lemma shortWeierstrass_equation_iff {R : Type*} [CommRing R] (a b x y : R) : (shortWeierstrass a b).toAffine.Equation x y ↔ y ^ 2 = x ^ 3 + a * x + b := by rw [WeierstrassCurve.Affine.equation_iff] change y ^ 2 + 0 * x * y + 0 * y = x ^ 3 + 0 * x ^ 2 + a * x + b ↔ _ constructor <;> intro h <;> linear_combination h lemma shortWeierstrass_nonsingular_iff {R : Type*} [CommRing R] (a b x y : R) : (shortWeierstrass a b).toAffine.Nonsingular x y ↔ y ^ 2 = x ^ 3 + a * x + b ∧ (2 * y ≠ 0 ∨ 3 * x ^ 2 + a ≠ 0) := by rw [WeierstrassCurve.Affine.nonsingular_iff, shortWeierstrass_equation_iff] change y ^ 2 = x ^ 3 + a * x + b ∧ (0 * y ≠ 3 * x ^ 2 + 2 * 0 * x + a ∨ y ≠ -y - 0 * x - 0) ↔ _ constructor · rintro ⟨heq, h | h⟩ · exact ⟨heq, Or.inr (fun h0 => h (by linear_combination -h0))⟩ · exact ⟨heq, Or.inl (fun h0 => h (by linear_combination h0))⟩ · rintro ⟨heq, h | h⟩ · exact ⟨heq, Or.inr (fun h0 => h (by linear_combination h0))⟩ · exact ⟨heq, Or.inl (fun h0 => h (by linear_combination -h0))⟩ /-- If `4 a³ + 27 b² ≠ 0`, the cubic `x³ + a x + b` and its derivative `3 x² + a` share no root. -/ lemma cubic_deriv_ne_zero_of_root {F : Type*} [Field F] {a b x : F} (hΔ : 4 * a ^ 3 + 27 * b ^ 2 ≠ 0) (hr : x ^ 3 + a * x + b = 0) : 3 * x ^ 2 + a ≠ 0 := by intro hd have ha : a = -3 * x ^ 2 := by linear_combination hd have hb : b = 2 * x ^ 3 := by linear_combination hr - x * ha apply hΔ rw [ha, hb]; ring /-- Over a field with `4 a³ + 27 b² ≠ 0` and `2 ≠ 0`, every affine solution is nonsingular. -/ lemma shortWeierstrass_nonsingular_of_eq {F : Type*} [Field F] {a b x y : F} (h2 : (2 : F) ≠ 0) (hΔ : 4 * a ^ 3 + 27 * b ^ 2 ≠ 0) (heq : y ^ 2 = x ^ 3 + a * x + b) : (shortWeierstrass a b).toAffine.Nonsingular x y := by rw [shortWeierstrass_nonsingular_iff] refine ⟨heq, ?_⟩ by_cases hy : y = 0 · right apply cubic_deriv_ne_zero_of_root hΔ rw [← heq, hy]; ring · left exact mul_ne_zero h2 hy /-- Explicit equivalence between affine points of `shortWeierstrass a b` and `Option` of solutions. -/ noncomputable def shortWeierstrassPointEquiv {F : Type*} [Field F] (a b : F) : (shortWeierstrass a b).toAffine.Point ≃ Option { p : F × F // (shortWeierstrass a b).toAffine.Nonsingular p.1 p.2 } where toFun | .zero => none | .some x y h => some ⟨(x, y), h⟩ invFun | none => .zero | some ⟨(x, y), h⟩ => .some x y h left_inv P := by cases P <;> rfl right_inv o := by rcases o with _ | ⟨⟨x, y⟩, h⟩ <;> rfl noncomputable instance shortWeierstrassPointFintype {F : Type*} [Field F] [Fintype F] (a b : F) : Fintype (shortWeierstrass a b).toAffine.Point := letI := Classical.decPred (fun p : F × F => (shortWeierstrass a b).toAffine.Nonsingular p.1 p.2) Fintype.ofEquiv _ (shortWeierstrassPointEquiv a b).symm /-- Number of `F`-rational points (including `𝓞`) on `E : y² = x³ + a x + b`. -/ noncomputable def pointCount {F : Type*} [Field F] [Fintype F] (a b : F) : ℕ := Fintype.card (shortWeierstrass a b).toAffine.Point /-- Number of square roots of `c` in a field `F` of characteristic `≠ 2`. -/ lemma card_sq_eq {F : Type*} [Field F] [Fintype F] [DecidableEq F] (h2 : (2 : F) ≠ 0) (c : F) : (Finset.univ.filter fun y : F => y ^ 2 = c).card = if c = 0 then 1 else if IsSquare c then 2 else 0 := by by_cases hc : c = 0 · subst hc simp only [↓reduceIte] have hset : (Finset.univ.filter fun y : F => y ^ 2 = 0) = {0} := by ext y; simp rw [hset, Finset.card_singleton] · simp only [hc, ↓reduceIte] by_cases hsq : IsSquare c · simp only [hsq, ↓reduceIte] rcases hsq with ⟨r, hr⟩ have hr0 : r ≠ 0 := by intro h0; apply hc; rw [hr, h0, mul_zero] have hne : r ≠ -r := by intro heq have : 2 * r = 0 := by linear_combination heq exact (mul_ne_zero h2 hr0) this have hset : (Finset.univ.filter fun y : F => y ^ 2 = c) = {r, -r} := by ext y simp only [Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_insert, Finset.mem_singleton] rw [hr, ← sq, sq_eq_sq_iff_eq_or_eq_neg] rw [hset, Finset.card_pair hne] · simp only [hsq, ↓reduceIte] rw [Finset.card_eq_zero, Finset.filter_eq_empty_iff] intro y _ hy exact hsq ⟨y, by rw [← sq, hy]⟩ /-- Fiber-counting formula for `pointCount a b`. -/ lemma pointCount_eq_sum_fiber {F : Type*} [Field F] [Fintype F] [DecidableEq F] {a b : F} (h2 : (2 : F) ≠ 0) (hΔ : 4 * a ^ 3 + 27 * b ^ 2 ≠ 0) : pointCount a b = 1 + ∑ x : F, (if x ^ 3 + a * x + b = 0 then 1 else if IsSquare (x ^ 3 + a * x + b) then 2 else 0) := by classical unfold pointCount rw [Fintype.card_congr (shortWeierstrassPointEquiv a b), Fintype.card_option] rw [add_comm] congr 1 have hcard : Fintype.card { p : F × F // (shortWeierstrass a b).toAffine.Nonsingular p.1 p.2 } = (Finset.univ.filter (fun p : F × F => p.2 ^ 2 = p.1 ^ 3 + a * p.1 + b)).card := by rw [Fintype.card_subtype] congr 1 ext ⟨x, y⟩ simp only [Finset.mem_filter, Finset.mem_univ, true_and] exact ⟨fun h => (shortWeierstrass_nonsingular_iff a b x y).mp h |>.1, shortWeierstrass_nonsingular_of_eq h2 hΔ⟩ rw [hcard] have hprod : (Finset.univ.filter (fun p : F × F => p.2 ^ 2 = p.1 ^ 3 + a * p.1 + b)) = (Finset.univ.sigma (fun x : F => Finset.univ.filter (fun y : F => y ^ 2 = x ^ 3 + a * x + b))).map ⟨fun s => (s.1, s.2), fun ⟨x₁, y₁⟩ ⟨x₂, y₂⟩ h => by cases h; rfl⟩ := by ext ⟨x, y⟩ simp only [Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_map, Finset.mem_sigma, Sigma.exists] exact ⟨fun h => ⟨x, y, h, rfl⟩, by rintro ⟨_, _, h, hpair⟩; cases hpair; exact h⟩ rw [hprod, Finset.card_map, Finset.card_sigma] refine Finset.sum_congr rfl fun x _ => ?_ exact card_sq_eq h2 (x ^ 3 + a * x + b) /-- **Character-Sum Point-Counting Formula**: `#E(𝔽_q) = q + 1 + ∑_{x ∈ 𝔽_q} χ(x³ + a x + b)`. -/ theorem pointCount_eq_quadraticChar_sum {F : Type*} [Field F] [Fintype F] [DecidableEq F] {a b : F} (h2 : (2 : F) ≠ 0) (hΔ : 4 * a ^ 3 + 27 * b ^ 2 ≠ 0) : (pointCount a b : ℤ) = (Fintype.card F : ℤ) + 1 + ∑ x : F, quadraticChar F (x ^ 3 + a * x + b) := by classical rw [pointCount_eq_sum_fiber h2 hΔ] rw [Nat.cast_add, Nat.cast_one, Nat.cast_sum] have hsum : ∑ x : F, ((1 : ℤ) + quadraticChar F (x ^ 3 + a * x + b)) = (Fintype.card F : ℤ) + ∑ x : F, quadraticChar F (x ^ 3 + a * x + b) := by rw [Finset.sum_add_distrib, Finset.sum_const, Finset.card_univ, nsmul_eq_mul, mul_one] have hterm : (∑ x : F, ((if x ^ 3 + a * x + b = 0 then 1 else if IsSquare (x ^ 3 + a * x + b) then 2 else 0 : ℕ) : ℤ)) = ∑ x : F, ((1 : ℤ) + quadraticChar F (x ^ 3 + a * x + b)) := by refine Finset.sum_congr rfl fun x _ => ?_ change _ = (1 : ℤ) + quadraticCharFun F (x ^ 3 + a * x + b) unfold quadraticCharFun split_ifs <;> norm_num rw [hterm, hsum]; ring /-- **Quadratic Twist Complementarity**: If `d ∈ 𝔽_q×` is a non-square, then `#E(𝔽_q) + #E^{(d)}(𝔽_q) = 2(q + 1)`. -/ theorem quadraticTwist_pointCount_sum {F : Type*} [Field F] [Fintype F] [DecidableEq F] {a b d : F} (h2 : (2 : F) ≠ 0) (hΔ : 4 * a ^ 3 + 27 * b ^ 2 ≠ 0) (hd : ¬ IsSquare d) : pointCount a b + pointCount (a * d ^ 2) (b * d ^ 3) = 2 * (Fintype.card F + 1) := by have hd0 : d ≠ 0 := by intro h0; exact hd (h0 ▸ ⟨0, by ring⟩) have hΔ' : 4 * (a * d ^ 2) ^ 3 + 27 * (b * d ^ 3) ^ 2 ≠ 0 := by have : 4 * (a * d ^ 2) ^ 3 + 27 * (b * d ^ 3) ^ 2 = d ^ 6 * (4 * a ^ 3 + 27 * b ^ 2) := by ring rw [this] exact mul_ne_zero (pow_ne_zero 6 hd0) hΔ have hχd : quadraticChar F d = -1 := by change quadraticCharFun F d = -1 unfold quadraticCharFun simp [hd0, hd] have hχd3 : quadraticChar F (d ^ 3) = -1 := by rw [map_pow, hχd]; norm_num have hbij : ∑ x : F, quadraticChar F (x ^ 3 + a * d ^ 2 * x + b * d ^ 3) = - ∑ x : F, quadraticChar F (x ^ 3 + a * x + b) := by have hreidx : ∑ x : F, quadraticChar F (x ^ 3 + a * d ^ 2 * x + b * d ^ 3) = ∑ x : F, quadraticChar F ((d * x) ^ 3 + a * d ^ 2 * (d * x) + b * d ^ 3) := by exact (Fintype.sum_equiv (Equiv.mulLeft₀ d hd0) _ _ (fun _ => rfl)).symm rw [hreidx, ← Finset.sum_neg_distrib] refine Finset.sum_congr rfl fun x _ => ?_ have halg : (d * x) ^ 3 + a * d ^ 2 * (d * x) + b * d ^ 3 = d ^ 3 * (x ^ 3 + a * x + b) := by ring rw [halg, map_mul, hχd3, neg_one_mul] have h1 := pointCount_eq_quadraticChar_sum h2 hΔ have h2' := pointCount_eq_quadraticChar_sum h2 hΔ' zify linarith /-! ## Section 2: Manin's Quadratic-Twist Proof of the Hasse–Weil Bound -/ section ManinProof attribute [local instance] Classical.decEq variable {F : Type*} [Field F] lemma withBot_add_lt_add {a b c d : WithBot ℕ} (h1 : a < b) (h2 : c < d) : a + c < b + d := by cases a with | bot => cases b with | bot => exact absurd h1 (lt_irrefl _) | coe nb => cases d with | bot => exact absurd h2 (not_lt_bot) | coe nd => simp [← WithBot.coe_add] | coe na => cases b with | bot => exact absurd h1 (not_lt_bot) | coe nb => cases c with | bot => cases d with | bot => exact absurd h2 (lt_irrefl _) | coe nd => simp [← WithBot.coe_add] | coe nc => cases d with | bot => exact absurd h2 (not_lt_bot) | coe nd => rw [WithBot.coe_lt_coe] at h1 h2 rw [← WithBot.coe_add, ← WithBot.coe_add, WithBot.coe_lt_coe] omega /-- Weighted degree of a polynomial pair `(u, v)`: `max(deg u, deg v + 1)` in `WithBot ℕ`. -/ noncomputable def pairWeight (u v : F[X]) : WithBot ℕ := max u.degree (v.degree + 1) /-- Elementary PID coprime-triple lemma: if `IsCoprime u₁ v₁` and `IsCoprime u₂ v₂`, no irreducible divides `v₁v₂`, `u₁v₂ + u₂v₁`, `u₁u₂`. -/ lemma not_irreducible_dvd_triple {u₁ v₁ u₂ v₂ π : F[X]} (hc₁ : IsCoprime u₁ v₁) (hc₂ : IsCoprime u₂ v₂) (hπ : Irreducible π) (hvv : π ∣ v₁ * v₂) (huv : π ∣ u₁ * v₂ + u₂ * v₁) (huu : π ∣ u₁ * u₂) : False := by have hp : Prime π := UniqueFactorizationMonoid.irreducible_iff_prime.mp hπ have h1 : ∀ {u₁ v₁ u₂ v₂ : F[X]}, IsCoprime u₁ v₁ → IsCoprime u₂ v₂ → π ∣ v₁ → π ∣ u₁ * v₂ + u₂ * v₁ → π ∣ u₁ * u₂ → False := by intro a b c d hab hcd hpb habcd hac have hna : ¬ π ∣ a := fun hpa => hπ.not_isUnit (hab.isUnit_of_dvd' hpa hpb) have hpc : π ∣ c := (hp.dvd_or_dvd hac).resolve_left hna have hpad : π ∣ a * d := by have : a * d = (a * d + c * b) - c * b := by ring rw [this] exact dvd_sub habcd (dvd_mul_of_dvd_right hpb c) have hpd : π ∣ d := (hp.dvd_or_dvd hpad).resolve_left hna exact hπ.not_isUnit (hcd.isUnit_of_dvd' hpc hpd) rcases hp.dvd_or_dvd hvv with hv₁ | hv₂ · exact h1 hc₁ hc₂ hv₁ huv huu · exact h1 hc₂ hc₁ hv₂ (by rwa [add_comm]) (by rwa [mul_comm]) /-- Exact degree additivity for `pairWeight`: `pairWeight u₁ v₁ + pairWeight u₂ v₂ = max(deg(u₁u₂), deg(u₁v₂ + u₂v₁) + 1, deg(v₁v₂) + 2)`. -/ lemma pairWeight_add_pairWeight (u₁ v₁ u₂ v₂ : F[X]) : pairWeight u₁ v₁ + pairWeight u₂ v₂ = max (u₁ * u₂).degree (max ((u₁ * v₂ + u₂ * v₁).degree + 1) ((v₁ * v₂).degree + 2)) := by unfold pairWeight set a₁ := u₁.degree set b₁ := v₁.degree + 1 set a₂ := u₂.degree set b₂ := v₂.degree + 1 have huu : (u₁ * u₂).degree = a₁ + a₂ := degree_mul have hvv : (v₁ * v₂).degree + 2 = b₁ + b₂ := by rw [degree_mul] show v₁.degree + v₂.degree + (1 + 1) = (v₁.degree + 1) + (v₂.degree + 1) abel have hcross_le : (u₁ * v₂ + u₂ * v₁).degree + 1 ≤ max (a₁ + b₂) (b₁ + a₂) := by calc (u₁ * v₂ + u₂ * v₁).degree + 1 ≤ max (u₁ * v₂).degree (u₂ * v₁).degree + 1 := add_le_add_left (degree_add_le _ _) 1 _ = max ((u₁ * v₂).degree + 1) ((u₂ * v₁).degree + 1) := by rw [max_add_add_right] _ = max (a₁ + b₂) (b₁ + a₂) := by rw [degree_mul, degree_mul] congr 1 · show u₁.degree + v₂.degree + 1 = u₁.degree + (v₂.degree + 1); abel · show u₂.degree + v₁.degree + 1 = (v₁.degree + 1) + u₂.degree; abel rw [huu, hvv] have hle : max (a₁ + a₂) (max ((u₁ * v₂ + u₂ * v₁).degree + 1) (b₁ + b₂)) ≤ max a₁ b₁ + max a₂ b₂ := by refine max_le (add_le_add (le_max_left _ _) (le_max_left _ _)) (max_le ?_ ?_) · exact hcross_le.trans (max_le (add_le_add (le_max_left _ _) (le_max_right _ _)) (add_le_add (le_max_right _ _) (le_max_left _ _))) · exact add_le_add (le_max_right _ _) (le_max_right _ _) refine le_antisymm ?_ hle rcases le_total b₁ a₁ with h1 | h1 <;> rcases le_total b₂ a₂ with h2 | h2 · rw [max_eq_left h1, max_eq_left h2]; exact le_max_left _ _ · rcases eq_or_lt_of_le h1 with h1eq | h1lt · rw [max_eq_left h1, max_eq_right h2, ← h1eq] exact le_trans (le_max_right _ _) (le_max_right _ _) · rcases eq_or_lt_of_le h2 with h2eq | h2lt · rw [max_eq_left h1, max_eq_right h2, ← h2eq]; exact le_max_left _ _ · rw [max_eq_left h1, max_eq_right h2] have hlt : (u₂ * v₁).degree < (u₁ * v₂).degree := by rw [degree_mul, degree_mul] have hsum : u₂.degree + (v₁.degree + 1) < (v₂.degree + 1) + u₁.degree := withBot_add_lt_add h2lt h1lt have hL : u₂.degree + (v₁.degree + 1) = (u₂.degree + v₁.degree) + 1 := by abel have hR : (v₂.degree + 1) + u₁.degree = (u₁.degree + v₂.degree) + 1 := by abel rw [hL, hR] at hsum exact lt_of_add_lt_add_right hsum have hdeg : (u₁ * v₂ + u₂ * v₁).degree = (u₁ * v₂).degree := degree_add_eq_left_of_degree_lt hlt have heq : (u₁ * v₂ + u₂ * v₁).degree + 1 = a₁ + b₂ := by rw [hdeg, degree_mul] show u₁.degree + v₂.degree + 1 = u₁.degree + (v₂.degree + 1); abel rw [← heq] exact le_trans (le_max_left _ _) (le_max_right _ _) · rcases eq_or_lt_of_le h1 with h1eq | h1lt · rw [max_eq_right h1, max_eq_left h2, ← h1eq]; exact le_max_left _ _ · rcases eq_or_lt_of_le h2 with h2eq | h2lt · rw [max_eq_right h1, max_eq_left h2, ← h2eq] exact le_trans (le_max_right _ _) (le_max_right _ _) · rw [max_eq_right h1, max_eq_left h2] have hlt : (u₁ * v₂).degree < (u₂ * v₁).degree := by rw [degree_mul, degree_mul] have hsum : u₁.degree + (v₂.degree + 1) < (v₁.degree + 1) + u₂.degree := withBot_add_lt_add h1lt h2lt have hL : u₁.degree + (v₂.degree + 1) = (u₁.degree + v₂.degree) + 1 := by abel have hR : (v₁.degree + 1) + u₂.degree = (u₂.degree + v₁.degree) + 1 := by abel rw [hL, hR] at hsum exact lt_of_add_lt_add_right hsum have hdeg : (u₁ * v₂ + u₂ * v₁).degree = (u₂ * v₁).degree := degree_add_eq_right_of_degree_lt hlt have heq : (u₁ * v₂ + u₂ * v₁).degree + 1 = b₁ + a₂ := by rw [hdeg, degree_mul] show u₂.degree + v₁.degree + 1 = (v₁.degree + 1) + u₂.degree; abel rw [← heq] exact le_trans (le_max_left _ _) (le_max_right _ _) · rw [max_eq_right h1, max_eq_right h2] exact le_trans (le_max_right _ _) (le_max_right _ _) /-- Helper: in a Euclidean domain, if `IsUnit (gcd f h)`, then `IsCoprime f h`. -/ lemma isCoprime_of_isUnit_gcd {f h : F[X]} (hu : IsUnit (EuclideanDomain.gcd f h)) : IsCoprime f h := by rcases hu with ⟨u, hu⟩ refine ⟨(u⁻¹ : F[X]ˣ) * EuclideanDomain.gcdA f h, (u⁻¹ : F[X]ˣ) * EuclideanDomain.gcdB f h, ?_⟩ have hbez := EuclideanDomain.gcd_eq_gcd_ab f h calc (u⁻¹ : F[X]ˣ) * EuclideanDomain.gcdA f h * f + (u⁻¹ : F[X]ˣ) * EuclideanDomain.gcdB f h * h = (u⁻¹ : F[X]ˣ) * (f * EuclideanDomain.gcdA f h + h * EuclideanDomain.gcdB f h) := by ring _ = (u⁻¹ : F[X]ˣ) * EuclideanDomain.gcd f h := by rw [← hbez] _ = (u⁻¹ : F[X]ˣ) * (u : F[X]) := by rw [← hu] _ = 1 := by exact Units.inv_mul u /-- The three Manin polynomials `A, B, C ∈ F[X]` governing `x(R + P) + x(R - P) = B/A` and `x(R + P) * x(R - P) = C/A` for `x(R) = u/v` and `x(P) = X`. -/ noncomputable def maninA (u v : F[X]) : F[X] := (u - X * v) ^ 2 noncomputable def maninB (a b : F) (u v : F[X]) : F[X] := C 2 * (u * X + C a * v) * (u + X * v) + C (4 * b) * v ^ 2 noncomputable def maninC (a b : F) (u v : F[X]) : F[X] := (u * X - C a * v) ^ 2 - C (4 * b) * v * (u + X * v) /-- Coprimality of `f(X) = X³ + a X + b` and `h(X) = X⁴ - 2 a X² - 8 b X + a²` when `4 a³ + 27 b² ≠ 0`. -/ lemma cubic_duplNum_coprime {a b : F} (hΔ : 4 * a ^ 3 + 27 * b ^ 2 ≠ 0) : IsCoprime (cubicPoly a b) (X ^ 4 - C (2 * a) * X ^ 2 - C (8 * b) * X + C (a ^ 2)) := by set f := cubicPoly a b set h := X ^ 4 - C (2 * a) * X ^ 2 - C (8 * b) * X + C (a ^ 2) refine isCoprime_of_isUnit_gcd ?_ by_contra hgu have hgnz : EuclideanDomain.gcd f h ≠ 0 := by intro h0 have hf0 : f = 0 := (EuclideanDomain.gcd_eq_zero_iff.mp h0).1 have : f.coeff 3 = 0 := by rw [hf0]; simp simp [f, cubicPoly] at this obtain ⟨π, hπ_irr, hπ_dvd⟩ := WfDvdMonoid.exists_irreducible_factor hgu hgnz have hπf : π ∣ f := dvd_trans hπ_dvd (EuclideanDomain.gcd_dvd_left f h) have hπh : π ∣ h := dvd_trans hπ_dvd (EuclideanDomain.gcd_dvd_right f h) have hp : Prime π := UniqueFactorizationMonoid.irreducible_iff_prime.mp hπ_irr have hid2 : (C (3 * b) - C a * X) * h + (C a * X ^ 2 - C (3 * b) * X - C (3 * a ^ 2)) * f = - C (4 * a ^ 3 + 27 * b ^ 2) * X := by simp only [f, h, cubicPoly, map_add, map_mul, map_pow, map_ofNat] ring have hπ_dX : π ∣ - C (4 * a ^ 3 + 27 * b ^ 2) * X := by rw [← hid2] exact dvd_add (dvd_mul_of_dvd_right hπh _) (dvd_mul_of_dvd_right hπf _) have hπX : π ∣ X := by rcases hp.dvd_or_dvd hπ_dX with hπc | hπX · exfalso have hu : IsUnit (- C (4 * a ^ 3 + 27 * b ^ 2)) := IsUnit.neg (isUnit_C.mpr (isUnit_iff_ne_zero.mpr hΔ)) exact hπ_irr.not_isUnit (isUnit_of_dvd_unit hπc hu) · exact hπX have hπb : π ∣ C b := by have : C b = f - X * (X ^ 2 + C a) := by simp [f, cubicPoly]; ring rw [this] exact dvd_sub hπf (dvd_mul_of_dvd_left hπX _) have hπa2 : π ∣ C (a ^ 2) := by have : C (a ^ 2) = h - X * (X ^ 3 - C (2 * a) * X - C (8 * b)) := by simp [h]; ring rw [this] exact dvd_sub hπh (dvd_mul_of_dvd_left hπX _) have hb0 : b = 0 := by by_contra hb exact hπ_irr.not_isUnit (isUnit_of_dvd_unit hπb (isUnit_C.mpr (isUnit_iff_ne_zero.mpr hb))) have ha0 : a = 0 := by by_contra ha exact hπ_irr.not_isUnit (isUnit_of_dvd_unit hπa2 (isUnit_C.mpr (isUnit_iff_ne_zero.mpr (pow_ne_zero 2 ha)))) apply hΔ rw [ha0, hb0]; ring /-- **Key Coprimality Lemma**: For `IsCoprime u v`, `2 ≠ 0`, `4 a³ + 27 b² ≠ 0`, no irreducible polynomial divides all three of `maninA u v`, `maninB a b u v`, `maninC a b u v`. -/ lemma not_irreducible_dvd_maninABC {a b : F} {u v π : F[X]} (h2 : (2 : F) ≠ 0) (hΔ : 4 * a ^ 3 + 27 * b ^ 2 ≠ 0) (hc : IsCoprime u v) (hπ : Irreducible π) (hA : π ∣ maninA u v) (hB : π ∣ maninB a b u v) (hC : π ∣ maninC a b u v) : False := by have hp : Prime π := UniqueFactorizationMonoid.irreducible_iff_prime.mp hπ have huxv : π ∣ u - X * v := hp.dvd_of_dvd_pow hA have hnv : ¬ π ∣ v := by intro hdv have hdu : π ∣ u := by have : u = (u - X * v) + X * v := by ring rw [this] exact dvd_add huxv (dvd_mul_of_dvd_right hdv X) exact hπ.not_isUnit (hc.isUnit_of_dvd' hdu hdv) have hB_id : maninB a b u v = (u - X * v) * (C 2 * X * u + C 2 * X ^ 2 * v + C (2 * a) * v + C 2 * X * (X * v)) + C 4 * cubicPoly a b * v ^ 2 := by simp only [maninB, cubicPoly, map_mul, map_ofNat] ring have hC_id : maninC a b u v = (u - X * v) * (X ^ 2 * u + X ^ 3 * v - C (2 * a) * X * v - C (4 * b) * v) + (X ^ 4 - C (2 * a) * X ^ 2 - C (8 * b) * X + C (a ^ 2)) * v ^ 2 := by simp only [maninC, map_mul, map_pow, map_ofNat] ring have hπf : π ∣ cubicPoly a b := by have h4fv : π ∣ C 4 * cubicPoly a b * v ^ 2 := by have : C 4 * cubicPoly a b * v ^ 2 = maninB a b u v - (u - X * v) * (C 2 * X * u + C 2 * X ^ 2 * v + C (2 * a) * v + C 2 * X * (X * v)) := by linear_combination -hB_id rw [this] exact dvd_sub hB (dvd_mul_of_dvd_left huxv _) have hnv2 : ¬ π ∣ v ^ 2 := fun h => hnv (hp.dvd_of_dvd_pow h) have h4f : π ∣ C 4 * cubicPoly a b := (hp.dvd_or_dvd h4fv).resolve_right hnv2 have h4ne : (4 : F) ≠ 0 := by have : (4 : F) = 2 * 2 := by ring rw [this]; exact mul_ne_zero h2 h2 have hn4 : ¬ π ∣ C (4 : F) := fun h => hπ.not_isUnit (isUnit_of_dvd_unit h (isUnit_C.mpr (isUnit_iff_ne_zero.mpr h4ne))) exact (hp.dvd_or_dvd h4f).resolve_left hn4 have hπh : π ∣ X ^ 4 - C (2 * a) * X ^ 2 - C (8 * b) * X + C (a ^ 2) := by have hhv : π ∣ (X ^ 4 - C (2 * a) * X ^ 2 - C (8 * b) * X + C (a ^ 2)) * v ^ 2 := by have : (X ^ 4 - C (2 * a) * X ^ 2 - C (8 * b) * X + C (a ^ 2)) * v ^ 2 = maninC a b u v - (u - X * v) * (X ^ 2 * u + X ^ 3 * v - C (2 * a) * X * v - C (4 * b) * v) := by linear_combination -hC_id rw [this] exact dvd_sub hC (dvd_mul_of_dvd_left huxv _) have hnv2 : ¬ π ∣ v ^ 2 := fun h => hnv (hp.dvd_of_dvd_pow h) exact (hp.dvd_or_dvd hhv).resolve_right hnv2 exact hπ.not_isUnit ((cubic_duplNum_coprime hΔ).isUnit_of_dvd' hπf hπh) /-- Fast helper: bound `p.degree ≤ (k : ℕ)` from `p.natDegree ≤ k`. -/ lemma degree_le_of_natDegree_le {p : F[X]} {k : ℕ} (h : p.natDegree ≤ k) : p.degree ≤ (k : WithBot ℕ) := degree_le_natDegree.trans (WithBot.coe_le_coe.mpr h) /-- **Exact Degree of `(A, B, C)`**: `max(deg C, deg B + 1, deg A + 2) = 2 * pairWeight u v + 2` whenever `v ≠ 0`. -/ lemma maninABC_max_degree (a b : F) {u v : F[X]} (hv : v ≠ 0) : max (maninC a b u v).degree (max ((maninB a b u v).degree + 1) ((maninA u v).degree + 2)) = 2 * pairWeight u v + 2 := by set n := v.natDegree have hv_deg : v.degree = (n : WithBot ℕ) := degree_eq_natDegree hv set D : ℕ := if u = 0 then n + 1 else max u.natDegree (n + 1) have hD_eq : pairWeight u v = (D : WithBot ℕ) := by unfold pairWeight by_cases hu : u = 0 · simp only [D, hu, ↓reduceIte, degree_zero, hv_deg, bot_le, max_eq_right] rfl · simp only [D, hu, ↓reduceIte, degree_eq_natDegree hu, hv_deg] norm_cast have hu_le : u.degree ≤ (D : WithBot ℕ) := by rw [← hD_eq]; exact le_max_left _ _ have hv1_le : n + 1 ≤ D := by unfold D; split_ifs <;> omega have hXv_deg : (X * v).degree = ((n + 1 : ℕ) : WithBot ℕ) := by rw [degree_mul, degree_X, hv_deg, add_comm]; rfl have hCa_v : (C a * v).degree ≤ (n : WithBot ℕ) := by rw [degree_mul, hv_deg] exact (add_le_add_left degree_C_le _).trans (by rw [zero_add]) have hC4b_v : (C (4 * b) * v).degree ≤ (n : WithBot ℕ) := by rw [degree_mul, hv_deg] exact (add_le_add_left degree_C_le _).trans (by rw [zero_add]) have huX_le : (u * X).degree ≤ ((D + 1 : ℕ) : WithBot ℕ) := by rw [degree_mul, degree_X] exact add_le_add_left hu_le 1 have h2D2 : (2 * pairWeight u v + 2 : WithBot ℕ) = ((2 * D + 2 : ℕ) : WithBot ℕ) := by rw [hD_eq, two_mul]; norm_cast; omega rw [h2D2] -- Upper bounds on deg A + 2, deg B + 1, deg C: have hu_sub_Xv : (u - X * v).degree ≤ (D : WithBot ℕ) := (degree_sub_le _ _).trans (max_le hu_le (hXv_deg ▸ WithBot.coe_le_coe.mpr hv1_le)) have hu_add_Xv : (u + X * v).degree ≤ (D : WithBot ℕ) := (degree_add_le _ _).trans (max_le hu_le (hXv_deg ▸ WithBot.coe_le_coe.mpr hv1_le)) have hn_le_D1 : n ≤ D + 1 := by omega have huX_add_av : (u * X + C a * v).degree ≤ ((D + 1 : ℕ) : WithBot ℕ) := (degree_add_le _ _).trans (max_le huX_le (hCa_v.trans (WithBot.coe_le_coe.mpr hn_le_D1))) have huX_sub_av : (u * X - C a * v).degree ≤ ((D + 1 : ℕ) : WithBot ℕ) := (degree_sub_le _ _).trans (max_le huX_le (hCa_v.trans (WithBot.coe_le_coe.mpr hn_le_D1))) have hA_le : (maninA u v).degree + 2 ≤ ((2 * D + 2 : ℕ) : WithBot ℕ) := by unfold maninA rw [degree_pow, two_nsmul] calc (u - X * v).degree + (u - X * v).degree + 2 ≤ (D : WithBot ℕ) + (D : WithBot ℕ) + 2 := add_le_add_left (add_le_add hu_sub_Xv hu_sub_Xv) 2 _ = ((2 * D + 2 : ℕ) : WithBot ℕ) := by norm_cast; omega have hB_le : (maninB a b u v).degree + 1 ≤ ((2 * D + 2 : ℕ) : WithBot ℕ) := by unfold maninB have h1 : (C 2 * (u * X + C a * v) * (u + X * v)).degree ≤ ((2 * D + 1 : ℕ) : WithBot ℕ) := by rw [degree_mul, degree_mul] calc (C (2 : F)).degree + (u * X + C a * v).degree + (u + X * v).degree ≤ 0 + ((D + 1 : ℕ) : WithBot ℕ) + (D : WithBot ℕ) := add_le_add (add_le_add degree_C_le huX_add_av) hu_add_Xv _ = ((2 * D + 1 : ℕ) : WithBot ℕ) := by rw [zero_add]; norm_cast; omega have h2 : (C (4 * b) * v ^ 2).degree ≤ ((2 * D + 1 : ℕ) : WithBot ℕ) := by rw [degree_mul, degree_pow, hv_deg, two_nsmul] calc (C (4 * b)).degree + ((n : WithBot ℕ) + (n : WithBot ℕ)) ≤ 0 + ((n : WithBot ℕ) + (n : WithBot ℕ)) := add_le_add_left degree_C_le _ _ ≤ ((2 * D + 1 : ℕ) : WithBot ℕ) := by rw [zero_add]; norm_cast; omega calc (C 2 * (u * X + C a * v) * (u + X * v) + C (4 * b) * v ^ 2).degree + 1 ≤ ((2 * D + 1 : ℕ) : WithBot ℕ) + 1 := add_le_add_left ((degree_add_le _ _).trans (max_le h1 h2)) 1 _ = ((2 * D + 2 : ℕ) : WithBot ℕ) := by norm_cast have hC_le : (maninC a b u v).degree ≤ ((2 * D + 2 : ℕ) : WithBot ℕ) := by unfold maninC have h1 : ((u * X - C a * v) ^ 2).degree ≤ ((2 * D + 2 : ℕ) : WithBot ℕ) := by rw [degree_pow, two_nsmul] calc (u * X - C a * v).degree + (u * X - C a * v).degree ≤ ((D + 1 : ℕ) : WithBot ℕ) + ((D + 1 : ℕ) : WithBot ℕ) := add_le_add huX_sub_av huX_sub_av _ = ((2 * D + 2 : ℕ) : WithBot ℕ) := by norm_cast; omega have h2 : (C (4 * b) * v * (u + X * v)).degree ≤ ((2 * D + 2 : ℕ) : WithBot ℕ) := by rw [degree_mul] calc (C (4 * b) * v).degree + (u + X * v).degree ≤ (n : WithBot ℕ) + (D : WithBot ℕ) := add_le_add hC4b_v hu_add_Xv _ ≤ ((2 * D + 2 : ℕ) : WithBot ℕ) := by norm_cast; omega exact (degree_sub_le _ _).trans (max_le h1 h2) refine le_antisymm (max_le hC_le (max_le hB_le hA_le)) ?_ by_cases hcase : u ≠ 0 ∧ n + 1 ≤ u.natDegree · rcases hcase with ⟨hu0, hnm⟩ have hD_u : D = u.natDegree := by simp [D, hu0, max_eq_left hnm] have hu_deg : u.degree = (D : WithBot ℕ) := by rw [degree_eq_natDegree hu0, hD_u] have huX_deg : (u * X).degree = ((D + 1 : ℕ) : WithBot ℕ) := by rw [degree_mul, hu_deg, degree_X]; rfl have hlt1 : (C a * v).degree < (u * X).degree := by have hn_lt : n < D + 1 := by omega rw [huX_deg]; exact hCa_v.trans_lt (WithBot.coe_lt_coe.mpr hn_lt) have hterm1 : ((u * X - C a * v) ^ 2).degree = ((2 * D + 2 : ℕ) : WithBot ℕ) := by rw [degree_pow, degree_sub_eq_left_of_degree_lt hlt1, huX_deg, two_nsmul] norm_cast; omega have hterm2 : (C (4 * b) * v * (u + X * v)).degree < ((2 * D + 2 : ℕ) : WithBot ℕ) := by rw [degree_mul] calc (C (4 * b) * v).degree + (u + X * v).degree ≤ (n : WithBot ℕ) + (D : WithBot ℕ) := add_le_add hC4b_v hu_add_Xv _ < ((2 * D + 2 : ℕ) : WithBot ℕ) := by norm_cast; omega have hCeq : (maninC a b u v).degree = ((2 * D + 2 : ℕ) : WithBot ℕ) := by unfold maninC rw [degree_sub_eq_left_of_degree_lt (hterm1 ▸ hterm2), hterm1] rw [← hCeq]; exact le_max_left _ _ · push Not at hcase have hlt : u.degree < (X * v).degree := by rw [hXv_deg] by_cases hu0 : u = 0 · rw [hu0, degree_zero]; exact WithBot.bot_lt_coe _ · rw [degree_eq_natDegree hu0] exact WithBot.coe_lt_coe.mpr (hcase hu0) have hD_n : D = n + 1 := by unfold D by_cases hu0 : u = 0 · simp [hu0] · have hle' : u.natDegree ≤ n + 1 := (hcase hu0).le simp [hu0, max_eq_right hle'] have hAeq : (maninA u v).degree + 2 = ((2 * D + 2 : ℕ) : WithBot ℕ) := by unfold maninA rw [degree_pow, degree_sub_eq_right_of_degree_lt hlt, hXv_deg, two_nsmul, hD_n] norm_cast; omega rw [← hAeq] exact le_trans (le_max_right _ _) (le_max_right _ _) /-- The quadratic twist `E_f : Y² = X³ + (a f²) X + (b f³)` over `RatFunc F` where `f = X³ + a X + b`. -/ noncomputable def twistCurve (a b : F) : WeierstrassCurve (RatFunc F) := let fK : RatFunc F := algebraMap F[X] (RatFunc F) (cubicPoly a b) shortWeierstrass (RatFunc.C a * fK ^ 2) (RatFunc.C b * fK ^ 3) lemma cubicPoly_ne_zero (a b : F) : cubicPoly a b ≠ 0 := by intro h0 have : (cubicPoly a b).coeff 3 = 0 := by rw [h0]; simp simp [cubicPoly] at this lemma ratFunc_algebraMap_eq_zero_iff (p : F[X]) : algebraMap F[X] (RatFunc F) p = 0 ↔ p = 0 := map_eq_zero_iff (algebraMap F[X] (RatFunc F)) (RatFunc.algebraMap_injective F) lemma ratFunc_C_eq_zero_iff (c : F) : RatFunc.C c = 0 ↔ c = 0 := map_eq_zero_iff (algebraMap F (RatFunc F)) (algebraMap F (RatFunc F)).injective lemma ratFunc_two_ne_zero (h2 : (2 : F) ≠ 0) : (2 : RatFunc F) ≠ 0 := by intro h0 apply h2 rw [← ratFunc_C_eq_zero_iff] simpa only [map_ofNat] using h0 lemma fK_ne_zero (a b : F) : (algebraMap F[X] (RatFunc F) (cubicPoly a b)) ≠ 0 := by rw [ne_eq, ratFunc_algebraMap_eq_zero_iff] exact cubicPoly_ne_zero a b lemma twistCurve_disc_ne_zero {a b : F} (hΔ : 4 * a ^ 3 + 27 * b ^ 2 ≠ 0) : 4 * (RatFunc.C a * (algebraMap F[X] (RatFunc F) (cubicPoly a b)) ^ 2) ^ 3 + 27 * (RatFunc.C b * (algebraMap F[X] (RatFunc F) (cubicPoly a b)) ^ 3) ^ 2 ≠ 0 := by set fK := algebraMap F[X] (RatFunc F) (cubicPoly a b) have hfK : fK ≠ 0 := fK_ne_zero a b have halg : 4 * (RatFunc.C a * fK ^ 2) ^ 3 + 27 * (RatFunc.C b * fK ^ 3) ^ 2 = fK ^ 6 * RatFunc.C (4 * a ^ 3 + 27 * b ^ 2) := by simp only [map_add, map_mul, map_pow, map_ofNat] ring rw [halg] refine mul_ne_zero (pow_ne_zero 6 hfK) ?_ rw [ne_eq, ratFunc_C_eq_zero_iff] exact hΔ /-- **Manin's Height/Degree Function** `D(R) ∈ ℕ` on `(twistCurve a b).toAffine.Point`: `D(𝓞) = 0`, and for affine `R` with untwisted `x(R) = u / v` in lowest terms, `D(R) = max(deg u, deg v + 1)`. -/ noncomputable def maninD (a b : F) : (twistCurve a b).toAffine.Point → ℕ | .zero => 0 | .some x0 _ _ => let x := x0 / algebraMap F[X] (RatFunc F) (cubicPoly a b) if x.num = 0 then x.denom.natDegree + 1 else max x.num.natDegree (x.denom.natDegree + 1) lemma maninD_some_coe (a b : F) (x0 y0 : RatFunc F) (h : (twistCurve a b).toAffine.Nonsingular x0 y0) : (maninD a b (.some x0 y0 h) : WithBot ℕ) = pairWeight (x0 / algebraMap F[X] (RatFunc F) (cubicPoly a b)).num (x0 / algebraMap F[X] (RatFunc F) (cubicPoly a b)).denom := by set x := x0 / algebraMap F[X] (RatFunc F) (cubicPoly a b) have hdv : x.denom ≠ 0 := RatFunc.denom_ne_zero x change ((if x.num = 0 then x.denom.natDegree + 1 else max x.num.natDegree (x.denom.natDegree + 1) : ℕ) : WithBot ℕ) = max x.num.degree (x.denom.degree + 1) by_cases hnum : x.num = 0 · simp only [hnum, ↓reduceIte, degree_zero, bot_le, max_eq_right] rw [degree_eq_natDegree hdv] rfl · simp only [hnum, ↓reduceIte] rw [degree_eq_natDegree hnum, degree_eq_natDegree hdv] norm_cast lemma maninD_neg (a b : F) (R : (twistCurve a b).toAffine.Point) : maninD a b (-R) = maninD a b R := by cases R with | zero => rfl | some x0 y0 h => rw [WeierstrassCurve.Affine.Point.neg_some] rfl lemma maninP_nonsingular {a b : F} (h2 : (2 : F) ≠ 0) (hΔ : 4 * a ^ 3 + 27 * b ^ 2 ≠ 0) : (twistCurve a b).toAffine.Nonsingular (algebraMap F[X] (RatFunc F) (cubicPoly a b) * RatFunc.X) ((algebraMap F[X] (RatFunc F) (cubicPoly a b)) ^ 2) := by set fK : RatFunc F := algebraMap F[X] (RatFunc F) (cubicPoly a b) refine shortWeierstrass_nonsingular_of_eq (ratFunc_two_ne_zero h2) (twistCurve_disc_ne_zero hΔ) ?_ have hfK_eq : fK = RatFunc.X ^ 3 + RatFunc.C a * RatFunc.X + RatFunc.C b := by simp only [fK, cubicPoly, map_add, map_mul, map_pow]; rfl calc (fK ^ 2) ^ 2 = fK ^ 3 * fK := by ring _ = fK ^ 3 * (RatFunc.X ^ 3 + RatFunc.C a * RatFunc.X + RatFunc.C b) := by rw [← hfK_eq] _ = (fK * RatFunc.X) ^ 3 + (RatFunc.C a * fK ^ 2) * (fK * RatFunc.X) + RatFunc.C b * fK ^ 3 := by ring /-- The generic point `P = (fK * X, fK²)` (untwisted `(X, 1)`) on `twistCurve a b`. -/ noncomputable def maninP {a b : F} (h2 : (2 : F) ≠ 0) (hΔ : 4 * a ^ 3 + 27 * b ^ 2 ≠ 0) : (twistCurve a b).toAffine.Point := .some (algebraMap F[X] (RatFunc F) (cubicPoly a b) * RatFunc.X) ((algebraMap F[X] (RatFunc F) (cubicPoly a b)) ^ 2) (maninP_nonsingular h2 hΔ) lemma maninD_P {a b : F} (h2 : (2 : F) ≠ 0) (hΔ : 4 * a ^ 3 + 27 * b ^ 2 ≠ 0) : maninD a b (maninP h2 hΔ) = 1 := by set fK : RatFunc F := algebraMap F[X] (RatFunc F) (cubicPoly a b) have hfK : fK ≠ 0 := fK_ne_zero a b change (let x := (fK * RatFunc.X) / fK; if x.num = 0 then x.denom.natDegree + 1 else max x.num.natDegree (x.denom.natDegree + 1)) = 1 dsimp only rw [mul_div_cancel_left₀ _ hfK, RatFunc.num_X, RatFunc.denom_X] simp [Polynomial.X_ne_zero] section TwistGroupLaw lemma maninD_two_P {a b : F} (h2 : (2 : F) ≠ 0) (hΔ : 4 * a ^ 3 + 27 * b ^ 2 ≠ 0) : maninD a b (maninP h2 hΔ + maninP h2 hΔ) = 4 := by set fK : RatFunc F := algebraMap F[X] (RatFunc F) (cubicPoly a b) have hfK : fK ≠ 0 := fK_ne_zero a b have h2K : (2 : RatFunc F) ≠ 0 := ratFunc_two_ne_zero h2 have hPns := maninP_nonsingular h2 hΔ have hneg : fK ^ 2 ≠ (twistCurve a b).toAffine.negY (fK * RatFunc.X) (fK ^ 2) := by change fK ^ 2 ≠ -fK ^ 2 - 0 * (fK * RatFunc.X) - 0 intro heq have : (2 : RatFunc F) * fK ^ 2 = 0 := by linear_combination heq exact mul_ne_zero h2K (pow_ne_zero 2 hfK) this have hxy : ¬ (fK * RatFunc.X = fK * RatFunc.X ∧ fK ^ 2 = (twistCurve a b).toAffine.negY (fK * RatFunc.X) (fK ^ 2)) := fun h => hneg h.2 have hadd : maninP h2 hΔ + maninP h2 hΔ = WeierstrassCurve.Affine.Point.some _ _ ((twistCurve a b).toAffine.nonsingular_add hPns hPns hxy) := WeierstrassCurve.Affine.Point.add_some hxy set numP : F[X] := X ^ 4 - C (2 * a) * X ^ 2 - C (8 * b) * X + C (a ^ 2) set denP : F[X] := C 4 * cubicPoly a b have h4ne : (4 : F) ≠ 0 := by have : (4 : F) = 2 * 2 := by ring rw [this]; exact mul_ne_zero h2 h2 have hx2P : (twistCurve a b).toAffine.addX (fK * RatFunc.X) (fK * RatFunc.X) ((twistCurve a b).toAffine.slope (fK * RatFunc.X) (fK * RatFunc.X) (fK ^ 2) (fK ^ 2)) / fK = algebraMap F[X] (RatFunc F) numP / algebraMap F[X] (RatFunc F) denP := by rw [WeierstrassCurve.Affine.slope_of_Y_ne rfl hneg] have hnum_eq : algebraMap F[X] (RatFunc F) numP = RatFunc.X ^ 4 - RatFunc.C (2 * a) * RatFunc.X ^ 2 - RatFunc.C (8 * b) * RatFunc.X + RatFunc.C (a ^ 2) := by simp only [numP, map_add, map_sub, map_mul, map_pow]; rfl have hden_eq : algebraMap F[X] (RatFunc F) denP = 4 * fK := by simp only [denP, fK, map_mul, map_ofNat] have hfK_eq : fK = RatFunc.X ^ 3 + RatFunc.C a * RatFunc.X + RatFunc.C b := by simp only [fK, cubicPoly, map_add, map_mul, map_pow]; rfl rw [hnum_eq, hden_eq] change (((3 * (fK * RatFunc.X) ^ 2 + 2 * 0 * (fK * RatFunc.X) + RatFunc.C a * fK ^ 2 - 0 * fK ^ 2) / (fK ^ 2 - (-fK ^ 2 - 0 * (fK * RatFunc.X) - 0))) ^ 2 + 0 * ((3 * (fK * RatFunc.X) ^ 2 + 2 * 0 * (fK * RatFunc.X) + RatFunc.C a * fK ^ 2 - 0 * fK ^ 2) / (fK ^ 2 - (-fK ^ 2 - 0 * (fK * RatFunc.X) - 0))) - 0 - fK * RatFunc.X - fK * RatFunc.X) / fK = _ have h2f2 : fK ^ 2 - (-fK ^ 2 - 0 * (fK * RatFunc.X) - 0) = 2 * fK ^ 2 := by ring rw [h2f2] have h4K : (4 : RatFunc F) ≠ 0 := by have : (4 : RatFunc F) = 2 * 2 := by ring rw [this]; exact mul_ne_zero h2K h2K field_simp [hfK, h2K, h4K] simp only [map_mul, map_pow, map_ofNat] rw [hfK_eq] ring have hcop : IsCoprime numP denP := by have h1' : IsCoprime numP (cubicPoly a b) := (cubic_duplNum_coprime hΔ).symm have h2' : IsCoprime numP (C (4 : F)) := ⟨0, C 4⁻¹, by simp [← map_mul, inv_mul_cancel₀ h4ne]⟩ exact h2'.mul_right h1' have hden0 : denP ≠ 0 := mul_ne_zero (C_ne_zero.mpr h4ne) (cubicPoly_ne_zero a b) set x := (twistCurve a b).toAffine.addX (fK * RatFunc.X) (fK * RatFunc.X) ((twistCurve a b).toAffine.slope (fK * RatFunc.X) (fK * RatFunc.X) (fK ^ 2) (fK ^ 2)) / fK have hx_eq : algebraMap F[X] (RatFunc F) x.num / algebraMap F[X] (RatFunc F) x.denom = algebraMap F[X] (RatFunc F) numP / algebraMap F[X] (RatFunc F) denP := by rw [RatFunc.num_div_denom, hx2P] have hcross : x.num * denP = numP * x.denom := by have hxd0 : algebraMap F[X] (RatFunc F) x.denom ≠ 0 := by rw [ne_eq, ratFunc_algebraMap_eq_zero_iff]; exact RatFunc.denom_ne_zero x have hd0' : algebraMap F[X] (RatFunc F) denP ≠ 0 := by rw [ne_eq, ratFunc_algebraMap_eq_zero_iff]; exact hden0 have hmul : algebraMap F[X] (RatFunc F) (x.num * denP) = algebraMap F[X] (RatFunc F) (numP * x.denom) := by simp only [map_mul] exact (div_eq_div_iff hxd0 hd0').mp hx_eq exact (RatFunc.algebraMap_injective F) hmul have hassoc_num : Associated x.num numP := by refine associated_of_dvd_dvd ?_ ?_ · exact (RatFunc.isCoprime_num_denom x).dvd_of_dvd_mul_right ⟨denP, hcross.symm⟩ · exact hcop.dvd_of_dvd_mul_right ⟨x.denom, hcross⟩ have hassoc_den : Associated x.denom denP := by refine associated_of_dvd_dvd ?_ ?_ · exact (RatFunc.isCoprime_num_denom x).symm.dvd_of_dvd_mul_left ⟨numP, by rw [hcross, mul_comm]⟩ · exact hcop.symm.dvd_of_dvd_mul_left ⟨x.num, by rw [← hcross, mul_comm]⟩ have hdeg_num : numP.degree = (4 : ℕ) := by unfold numP have hlt : (- C (2 * a) * X ^ 2 - C (8 * b) * X + C (a ^ 2)).degree < (X ^ 4 : F[X]).degree := by rw [degree_X_pow] refine (degree_add_le _ _).trans_lt (max_lt ?_ (degree_C_le.trans_lt (by decide))) refine (degree_sub_le _ _).trans_lt (max_lt ?_ ?_) · calc (- C (2 * a) * X ^ 2).degree = (- C (2 * a)).degree + (X ^ 2).degree := degree_mul _ = (C (2 * a)).degree + ((2 : ℕ) : WithBot ℕ) := by rw [degree_neg, degree_X_pow] _ ≤ 0 + ((2 : ℕ) : WithBot ℕ) := add_le_add_left degree_C_le _ _ < ((4 : ℕ) : WithBot ℕ) := by rw [zero_add]; decide · calc (C (8 * b) * X).degree = (C (8 * b)).degree + X.degree := degree_mul _ ≤ 0 + 1 := add_le_add degree_C_le (by rw [degree_X]) _ < ((4 : ℕ) : WithBot ℕ) := by rw [zero_add]; decide have heq : X ^ 4 - C (2 * a) * X ^ 2 - C (8 * b) * X + C (a ^ 2) = X ^ 4 + (- C (2 * a) * X ^ 2 - C (8 * b) * X + C (a ^ 2)) := by ring rw [heq, degree_add_eq_left_of_degree_lt hlt, degree_X_pow] have hdeg_den : denP.degree = (3 : ℕ) := by unfold denP cubicPoly rw [degree_mul, degree_C h4ne, zero_add] have hlt : (C a * X + C b).degree < (X ^ 3 : F[X]).degree := by rw [degree_X_pow] refine (degree_add_le _ _).trans_lt (max_lt ?_ (degree_C_le.trans_lt (by decide))) calc (C a * X).degree = (C a).degree + X.degree := degree_mul _ ≤ 0 + 1 := add_le_add degree_C_le (by rw [degree_X]) _ < ((3 : ℕ) : WithBot ℕ) := by rw [zero_add]; decide have heq : X ^ 3 + C a * X + C b = X ^ 3 + (C a * X + C b) := by ring rw [heq, degree_add_eq_left_of_degree_lt hlt, degree_X_pow] have hxnum_deg : x.num.degree = (4 : ℕ) := by rcases hassoc_num with ⟨u, hu⟩ have hdu : (u : F[X]).degree = 0 := degree_eq_zero_of_isUnit u.isUnit rw [← hdeg_num, ← hu, degree_mul, hdu, add_zero] have hxden_deg : x.denom.degree = (3 : ℕ) := by rcases hassoc_den with ⟨u, hu⟩ have hdu : (u : F[X]).degree = 0 := degree_eq_zero_of_isUnit u.isUnit rw [← hdeg_den, ← hu, degree_mul, hdu, add_zero] have hcoe : (maninD a b (maninP h2 hΔ + maninP h2 hΔ) : WithBot ℕ) = ((4 : ℕ) : WithBot ℕ) := by rw [hadd, maninD_some_coe] change pairWeight x.num x.denom = ((4 : ℕ) : WithBot ℕ) unfold pairWeight rw [hxnum_deg, hxden_deg] rfl exact WithBot.coe_injective hcoe /-- **Manin's Second-Order Degree Recurrence**: For EVERY point `R ∈ E_f(𝔽_q(t))`, `D(R + P) + D(R - P) = 2 * D(R) + 2`. -/ theorem maninD_recurrence {a b : F} (h2 : (2 : F) ≠ 0) (hΔ : 4 * a ^ 3 + 27 * b ^ 2 ≠ 0) (R : (twistCurve a b).toAffine.Point) : maninD a b (R + maninP h2 hΔ) + maninD a b (R - maninP h2 hΔ) = 2 * maninD a b R + 2 := by set P := maninP h2 hΔ have hD0 : maninD a b 0 = 0 := rfl have hDP : maninD a b P = 1 := maninD_P h2 hΔ have hD2P : maninD a b (P + P) = 4 := maninD_two_P h2 hΔ by_cases hR0 : R = 0 · subst hR0 simp only [zero_add, zero_sub, maninD_neg, hD0, hDP] by_cases hRP : R = P · subst hRP rw [sub_self, hD2P, hD0, hDP] by_cases hRnP : R = -P · subst hRnP rw [neg_add_cancel, sub_eq_add_neg, ← neg_add, maninD_neg, maninD_neg, hD2P, hD0, hDP] cases R with | zero => exact absurd rfl hR0 | some x0 y0 hR => set fK : RatFunc F := algebraMap F[X] (RatFunc F) (cubicPoly a b) have hfK : fK ≠ 0 := fK_ne_zero a b set x := x0 / fK set y := y0 / fK ^ 2 have hx0 : x0 = fK * x := (mul_div_cancel₀ x0 hfK).symm have hy0 : y0 = fK ^ 2 * y := (mul_div_cancel₀ y0 (pow_ne_zero 2 hfK)).symm have hEq_untwist : fK * y ^ 2 = x ^ 3 + RatFunc.C a * x + RatFunc.C b := by have heq := (shortWeierstrass_nonsingular_iff _ _ x0 y0).mp hR |>.1 rw [hx0, hy0] at heq have hmul : fK ^ 3 * (fK * y ^ 2) = fK ^ 3 * (x ^ 3 + RatFunc.C a * x + RatFunc.C b) := by linear_combination heq exact mul_left_cancel₀ (pow_ne_zero 3 hfK) hmul have hx_ne_X : x ≠ RatFunc.X := by intro hxX have hy2 : y ^ 2 = 1 := by have hfK_eq : fK = RatFunc.X ^ 3 + RatFunc.C a * RatFunc.X + RatFunc.C b := by simp only [fK, cubicPoly, map_add, map_mul, map_pow]; rfl rw [hxX, ← hfK_eq] at hEq_untwist exact mul_left_cancel₀ hfK (by linear_combination hEq_untwist) rcases sq_eq_one_iff.mp hy2 with hy1 | hym1 · apply hRP change WeierstrassCurve.Affine.Point.some x0 y0 hR = WeierstrassCurve.Affine.Point.some (fK * RatFunc.X) (fK ^ 2) (maninP_nonsingular h2 hΔ) rw [WeierstrassCurve.Affine.Point.some.injEq] exact ⟨by rw [hx0, hxX], by rw [hy0, hy1, mul_one]⟩ · apply hRnP have hnegP : -P = WeierstrassCurve.Affine.Point.some (fK * RatFunc.X) ((twistCurve a b).toAffine.negY (fK * RatFunc.X) (fK ^ 2)) _ := WeierstrassCurve.Affine.Point.neg_some _ rw [hnegP, WeierstrassCurve.Affine.Point.some.injEq] refine ⟨by rw [hx0, hxX], ?_⟩ change y0 = -fK ^ 2 - 0 * (fK * RatFunc.X) - 0 rw [hy0, hym1]; ring have hx0_ne : x0 ≠ fK * RatFunc.X := fun h => hx_ne_X (by dsimp only [x]; rw [h, mul_div_cancel_left₀ _ hfK]) have hPns := maninP_nonsingular h2 hΔ have hnegPns := ((twistCurve a b).toAffine.nonsingular_neg (fK * RatFunc.X) (fK ^ 2)).mpr hPns have haddP : WeierstrassCurve.Affine.Point.some x0 y0 hR + P = WeierstrassCurve.Affine.Point.some _ _ ((twistCurve a b).toAffine.nonsingular_add hR hPns (fun h => hx0_ne h.1)) := WeierstrassCurve.Affine.Point.add_some (fun h => hx0_ne h.1) have hnegP_eq : -P = WeierstrassCurve.Affine.Point.some (fK * RatFunc.X) ((twistCurve a b).toAffine.negY (fK * RatFunc.X) (fK ^ 2)) hnegPns := WeierstrassCurve.Affine.Point.neg_some hPns have hsubP : WeierstrassCurve.Affine.Point.some x0 y0 hR - P = WeierstrassCurve.Affine.Point.some _ _ ((twistCurve a b).toAffine.nonsingular_add hR hnegPns (fun h => hx0_ne h.1)) := by rw [sub_eq_add_neg, hnegP_eq] exact WeierstrassCurve.Affine.Point.add_some (fun h => hx0_ne h.1) set x_p := (twistCurve a b).toAffine.addX x0 (fK * RatFunc.X) ((twistCurve a b).toAffine.slope x0 (fK * RatFunc.X) y0 (fK ^ 2)) / fK set x_m := (twistCurve a b).toAffine.addX x0 (fK * RatFunc.X) ((twistCurve a b).toAffine.slope x0 (fK * RatFunc.X) y0 ((twistCurve a b).toAffine.negY (fK * RatFunc.X) (fK ^ 2))) / fK have hsub_ne : x - RatFunc.X ≠ 0 := sub_ne_zero.mpr hx_ne_X have hxp_formula : x_p = fK * ((y - 1) / (x - RatFunc.X)) ^ 2 - x - RatFunc.X := by change (twistCurve a b).toAffine.addX x0 (fK * RatFunc.X) ((twistCurve a b).toAffine.slope x0 (fK * RatFunc.X) y0 (fK ^ 2)) / fK = _ rw [WeierstrassCurve.Affine.slope_of_X_ne hx0_ne] change (((y0 - fK ^ 2) / (x0 - fK * RatFunc.X)) ^ 2 + 0 * ((y0 - fK ^ 2) / (x0 - fK * RatFunc.X)) - 0 - x0 - fK * RatFunc.X) / fK = _ rw [hx0, hy0] field_simp [hfK, hsub_ne] ring have hxm_formula : x_m = fK * ((y + 1) / (x - RatFunc.X)) ^ 2 - x - RatFunc.X := by change (twistCurve a b).toAffine.addX x0 (fK * RatFunc.X) ((twistCurve a b).toAffine.slope x0 (fK * RatFunc.X) y0 ((twistCurve a b).toAffine.negY (fK * RatFunc.X) (fK ^ 2))) / fK = _ rw [WeierstrassCurve.Affine.slope_of_X_ne hx0_ne] change (((y0 - (-fK ^ 2 - 0 * (fK * RatFunc.X) - 0)) / (x0 - fK * RatFunc.X)) ^ 2 + 0 * ((y0 - (-fK ^ 2 - 0 * (fK * RatFunc.X) - 0)) / (x0 - fK * RatFunc.X)) - 0 - x0 - fK * RatFunc.X) / fK = _ rw [hx0, hy0] field_simp [hfK, hsub_ne] ring have hfK_eq : fK = RatFunc.X ^ 3 + RatFunc.C a * RatFunc.X + RatFunc.C b := by simp only [fK, cubicPoly, map_add, map_mul, map_pow]; rfl have hsum_id : x_p + x_m = (2 * (x * RatFunc.X + RatFunc.C a) * (x + RatFunc.X) + 4 * RatFunc.C b) / (x - RatFunc.X) ^ 2 := by rw [hxp_formula, hxm_formula] field_simp [hsub_ne] linear_combination 2 * hEq_untwist + 2 * hfK_eq have hprod_id : x_p * x_m = ((x * RatFunc.X - RatFunc.C a) ^ 2 - 4 * RatFunc.C b * (x + RatFunc.X)) / (x - RatFunc.X) ^ 2 := by rw [hxp_formula, hxm_formula] field_simp [hsub_ne] have hfactor : (fK * (y - 1) ^ 2 - (x + RatFunc.X) * (x - RatFunc.X) ^ 2) * (fK * (y + 1) ^ 2 - (x + RatFunc.X) * (x - RatFunc.X) ^ 2) = (fK * y ^ 2 - fK) ^ 2 - 2 * (x + RatFunc.X) * (x - RatFunc.X) ^ 2 * (fK * y ^ 2 + fK) + (x + RatFunc.X) ^ 2 * (x - RatFunc.X) ^ 4 := by ring have hgoal : (fK * (y - 1) ^ 2 - (x + RatFunc.X) * (x - RatFunc.X) ^ 2) * (fK * (y + 1) ^ 2 - (x + RatFunc.X) * (x - RatFunc.X) ^ 2) = ((x * RatFunc.X - RatFunc.C a) ^ 2 - 4 * RatFunc.C b * (x + RatFunc.X)) * (x - RatFunc.X) ^ 2 := by rw [hfactor, hEq_untwist, hfK_eq] ring linear_combination hgoal set u := x.num set v := x.denom set u₁ := x_p.num set v₁ := x_p.denom set u₂ := x_m.num set v₂ := x_m.denom have hv0 : v ≠ 0 := RatFunc.denom_ne_zero x have hv10 : v₁ ≠ 0 := RatFunc.denom_ne_zero x_p have hv20 : v₂ ≠ 0 := RatFunc.denom_ne_zero x_m have hA_ne : maninA u v ≠ 0 := by unfold maninA refine pow_ne_zero 2 ?_ intro h0 apply hx_ne_X rw [← RatFunc.num_div_denom x] have hvK : algebraMap F[X] (RatFunc F) v ≠ 0 := by rw [ne_eq, ratFunc_algebraMap_eq_zero_iff]; exact hv0 rw [div_eq_iff hvK] have h0' := congr_arg (algebraMap F[X] (RatFunc F)) h0 simp only [map_sub, map_mul, map_zero] at h0' change algebraMap F[X] (RatFunc F) u - RatFunc.X * algebraMap F[X] (RatFunc F) v = 0 at h0' linear_combination h0' have hAB_poly : (u₁ * v₂ + u₂ * v₁) * maninA u v = maninB a b u v * (v₁ * v₂) := by apply RatFunc.algebraMap_injective F have hvK : algebraMap F[X] (RatFunc F) v ≠ 0 := by rw [ne_eq, ratFunc_algebraMap_eq_zero_iff]; exact hv0 have hv1K : algebraMap F[X] (RatFunc F) v₁ ≠ 0 := by rw [ne_eq, ratFunc_algebraMap_eq_zero_iff]; exact hv10 have hv2K : algebraMap F[X] (RatFunc F) v₂ ≠ 0 := by rw [ne_eq, ratFunc_algebraMap_eq_zero_iff]; exact hv20 have hx_frac : x = algebraMap F[X] (RatFunc F) u / algebraMap F[X] (RatFunc F) v := (RatFunc.num_div_denom x).symm have hxp_frac : x_p = algebraMap F[X] (RatFunc F) u₁ / algebraMap F[X] (RatFunc F) v₁ := (RatFunc.num_div_denom x_p).symm have hxm_frac : x_m = algebraMap F[X] (RatFunc F) u₂ / algebraMap F[X] (RatFunc F) v₂ := (RatFunc.num_div_denom x_m).symm have hsum' := hsum_id rw [hx_frac, hxp_frac, hxm_frac] at hsum' have hsub_ne' : algebraMap F[X] (RatFunc F) u - RatFunc.X * algebraMap F[X] (RatFunc F) v ≠ 0 := by intro h0 apply hsub_ne rw [hx_frac, div_sub' hvK] exact div_eq_zero_iff.mpr (Or.inl (by linear_combination h0)) have hsub_ne'' : algebraMap F[X] (RatFunc F) u - algebraMap F[X] (RatFunc F) v * RatFunc.X ≠ 0 := by rwa [mul_comm (algebraMap F[X] (RatFunc F) v) RatFunc.X] have hsum_clear : (algebraMap F[X] (RatFunc F) u₁ * algebraMap F[X] (RatFunc F) v₂ + algebraMap F[X] (RatFunc F) u₂ * algebraMap F[X] (RatFunc F) v₁) * (algebraMap F[X] (RatFunc F) u - RatFunc.X * algebraMap F[X] (RatFunc F) v) ^ 2 = (2 * (algebraMap F[X] (RatFunc F) u * RatFunc.X + RatFunc.C a * algebraMap F[X] (RatFunc F) v) * (algebraMap F[X] (RatFunc F) u + RatFunc.X * algebraMap F[X] (RatFunc F) v) + 4 * RatFunc.C b * (algebraMap F[X] (RatFunc F) v) ^ 2) * (algebraMap F[X] (RatFunc F) v₁ * algebraMap F[X] (RatFunc F) v₂) := by field_simp [hvK, hv1K, hv2K, hsub_ne''] at hsum' linear_combination hsum' have hXeq : algebraMap F[X] (RatFunc F) X = RatFunc.X := rfl have hCeq : ∀ c : F, algebraMap F[X] (RatFunc F) (C c) = RatFunc.C c := fun _ => rfl simp only [maninA, maninB, map_add, map_sub, map_mul, map_pow, hXeq, hCeq, map_ofNat] exact hsum_clear have hAC_poly : (u₁ * u₂) * maninA u v = maninC a b u v * (v₁ * v₂) := by apply RatFunc.algebraMap_injective F have hvK : algebraMap F[X] (RatFunc F) v ≠ 0 := by rw [ne_eq, ratFunc_algebraMap_eq_zero_iff]; exact hv0 have hv1K : algebraMap F[X] (RatFunc F) v₁ ≠ 0 := by rw [ne_eq, ratFunc_algebraMap_eq_zero_iff]; exact hv10 have hv2K : algebraMap F[X] (RatFunc F) v₂ ≠ 0 := by rw [ne_eq, ratFunc_algebraMap_eq_zero_iff]; exact hv20 have hx_frac : x = algebraMap F[X] (RatFunc F) u / algebraMap F[X] (RatFunc F) v := (RatFunc.num_div_denom x).symm have hxp_frac : x_p = algebraMap F[X] (RatFunc F) u₁ / algebraMap F[X] (RatFunc F) v₁ := (RatFunc.num_div_denom x_p).symm have hxm_frac : x_m = algebraMap F[X] (RatFunc F) u₂ / algebraMap F[X] (RatFunc F) v₂ := (RatFunc.num_div_denom x_m).symm have hprod' := hprod_id rw [hx_frac, hxp_frac, hxm_frac] at hprod' have hsub_ne' : algebraMap F[X] (RatFunc F) u - RatFunc.X * algebraMap F[X] (RatFunc F) v ≠ 0 := by intro h0 apply hsub_ne rw [hx_frac, div_sub' hvK] exact div_eq_zero_iff.mpr (Or.inl (by linear_combination h0)) have hsub_ne'' : algebraMap F[X] (RatFunc F) u - algebraMap F[X] (RatFunc F) v * RatFunc.X ≠ 0 := by rwa [mul_comm (algebraMap F[X] (RatFunc F) v) RatFunc.X] have hprod_clear : (algebraMap F[X] (RatFunc F) u₁ * algebraMap F[X] (RatFunc F) u₂) * (algebraMap F[X] (RatFunc F) u - RatFunc.X * algebraMap F[X] (RatFunc F) v) ^ 2 = ((algebraMap F[X] (RatFunc F) u * RatFunc.X - RatFunc.C a * algebraMap F[X] (RatFunc F) v) ^ 2 - 4 * RatFunc.C b * algebraMap F[X] (RatFunc F) v * (algebraMap F[X] (RatFunc F) u + RatFunc.X * algebraMap F[X] (RatFunc F) v)) * (algebraMap F[X] (RatFunc F) v₁ * algebraMap F[X] (RatFunc F) v₂) := by field_simp [hvK, hv1K, hv2K, hsub_ne''] at hprod' linear_combination hprod' have hXeq : algebraMap F[X] (RatFunc F) X = RatFunc.X := rfl have hCeq : ∀ c : F, algebraMap F[X] (RatFunc F) (C c) = RatFunc.C c := fun _ => rfl simp only [maninA, maninC, map_add, map_sub, map_mul, map_pow, hXeq, hCeq, map_ofNat] exact hprod_clear have hassoc : Associated (v₁ * v₂) (maninA u v) := by set g := EuclideanDomain.gcd (v₁ * v₂) (maninA u v) have hg_ne : g ≠ 0 := fun h0 => hA_ne (EuclideanDomain.gcd_eq_zero_iff.mp h0).2 obtain ⟨q₁, hq₁⟩ := EuclideanDomain.gcd_dvd_left (v₁ * v₂) (maninA u v) obtain ⟨q₂, hq₂⟩ := EuclideanDomain.gcd_dvd_right (v₁ * v₂) (maninA u v) have hcop_q : IsCoprime q₁ q₂ := by refine isCoprime_of_isUnit_gcd ?_ set d := EuclideanDomain.gcd q₁ q₂ have hd1 : d ∣ q₁ := EuclideanDomain.gcd_dvd_left q₁ q₂ have hd2 : d ∣ q₂ := EuclideanDomain.gcd_dvd_right q₁ q₂ have hgd : g * d ∣ g := by refine EuclideanDomain.dvd_gcd ?_ ?_ · rw [hq₁]; exact mul_dvd_mul_left g hd1 · rw [hq₂]; exact mul_dvd_mul_left g hd2 obtain ⟨k, hk⟩ := hgd have : d * k = 1 := mul_left_cancel₀ hg_ne (by linear_combination -hk) exact isUnit_iff_exists_inv.mpr ⟨k, this⟩ have hq1_u : IsUnit q₁ := by by_contra hnu have hq1_ne : q₁ ≠ 0 := fun h0 => mul_ne_zero hv10 hv20 (by rw [hq₁, h0, mul_zero]) obtain ⟨π, hπ_irr, hπ_q1⟩ := WfDvdMonoid.exists_irreducible_factor hnu hq1_ne have hπ_vv : π ∣ v₁ * v₂ := by rw [hq₁]; exact dvd_mul_of_dvd_right hπ_q1 g have hπ_uv : π ∣ u₁ * v₂ + u₂ * v₁ := by have heq : (u₁ * v₂ + u₂ * v₁) * q₂ = maninB a b u v * q₁ := mul_left_cancel₀ hg_ne (by calc g * ((u₁ * v₂ + u₂ * v₁) * q₂) = (u₁ * v₂ + u₂ * v₁) * (g * q₂) := by ring _ = (u₁ * v₂ + u₂ * v₁) * maninA u v := by rw [← hq₂] _ = maninB a b u v * (v₁ * v₂) := hAB_poly _ = maninB a b u v * (g * q₁) := by rw [hq₁] _ = g * (maninB a b u v * q₁) := by ring) exact dvd_trans hπ_q1 (hcop_q.dvd_of_dvd_mul_right ⟨maninB a b u v, by rw [heq, mul_comm]⟩) have hπ_uu : π ∣ u₁ * u₂ := by have heq : (u₁ * u₂) * q₂ = maninC a b u v * q₁ := mul_left_cancel₀ hg_ne (by calc g * ((u₁ * u₂) * q₂) = (u₁ * u₂) * (g * q₂) := by ring _ = (u₁ * u₂) * maninA u v := by rw [← hq₂] _ = maninC a b u v * (v₁ * v₂) := hAC_poly _ = maninC a b u v * (g * q₁) := by rw [hq₁] _ = g * (maninC a b u v * q₁) := by ring) exact dvd_trans hπ_q1 (hcop_q.dvd_of_dvd_mul_right ⟨maninC a b u v, by rw [heq, mul_comm]⟩) exact not_irreducible_dvd_triple (RatFunc.isCoprime_num_denom x_p) (RatFunc.isCoprime_num_denom x_m) hπ_irr hπ_vv hπ_uv hπ_uu have hq2_u : IsUnit q₂ := by by_contra hnu have hq2_ne : q₂ ≠ 0 := fun h0 => hA_ne (by rw [hq₂, h0, mul_zero]) obtain ⟨π, hπ_irr, hπ_q2⟩ := WfDvdMonoid.exists_irreducible_factor hnu hq2_ne have hπ_A : π ∣ maninA u v := by rw [hq₂]; exact dvd_mul_of_dvd_right hπ_q2 g have hπ_B : π ∣ maninB a b u v := by have heq : maninB a b u v * q₁ = (u₁ * v₂ + u₂ * v₁) * q₂ := mul_left_cancel₀ hg_ne (by calc g * (maninB a b u v * q₁) = maninB a b u v * (g * q₁) := by ring _ = maninB a b u v * (v₁ * v₂) := by rw [← hq₁] _ = (u₁ * v₂ + u₂ * v₁) * maninA u v := hAB_poly.symm _ = (u₁ * v₂ + u₂ * v₁) * (g * q₂) := by rw [hq₂] _ = g * ((u₁ * v₂ + u₂ * v₁) * q₂) := by ring) exact dvd_trans hπ_q2 (hcop_q.symm.dvd_of_dvd_mul_right ⟨u₁ * v₂ + u₂ * v₁, by rw [heq, mul_comm]⟩) have hπ_C : π ∣ maninC a b u v := by have heq : maninC a b u v * q₁ = (u₁ * u₂) * q₂ := mul_left_cancel₀ hg_ne (by calc g * (maninC a b u v * q₁) = maninC a b u v * (g * q₁) := by ring _ = maninC a b u v * (v₁ * v₂) := by rw [← hq₁] _ = (u₁ * u₂) * maninA u v := hAC_poly.symm _ = (u₁ * u₂) * (g * q₂) := by rw [hq₂] _ = g * ((u₁ * u₂) * q₂) := by ring) exact dvd_trans hπ_q2 (hcop_q.symm.dvd_of_dvd_mul_right ⟨u₁ * u₂, by rw [heq, mul_comm]⟩) exact not_irreducible_dvd_maninABC h2 hΔ (RatFunc.isCoprime_num_denom x) hπ_irr hπ_A hπ_B hπ_C rcases hq1_u with ⟨c₁, rfl⟩ rcases hq2_u with ⟨c₂, rfl⟩ refine ⟨c₁⁻¹ * c₂, ?_⟩ calc v₁ * v₂ * ((c₁⁻¹ * c₂ : F[X]ˣ) : F[X]) = (g * (c₁ : F[X])) * ((c₁⁻¹ : F[X]ˣ) * (c₂ : F[X])) := by rw [hq₁, Units.val_mul] _ = g * (((c₁ : F[X]) * (c₁⁻¹ : F[X]ˣ)) * (c₂ : F[X])) := by ring _ = g * (c₂ : F[X]) := by rw [Units.mul_inv, one_mul] _ = maninA u v := hq₂.symm rcases hassoc with ⟨c, hcA⟩ have hc_deg : (c : F[X]).degree = 0 := degree_eq_zero_of_isUnit c.isUnit have hvv_deg : (v₁ * v₂).degree = (maninA u v).degree := by calc (v₁ * v₂).degree = (v₁ * v₂).degree + (c : F[X]).degree := by rw [hc_deg, add_zero] _ = (v₁ * v₂ * (c : F[X])).degree := degree_mul.symm _ = (maninA u v).degree := by rw [hcA] have huv_deg : (u₁ * v₂ + u₂ * v₁).degree = (maninB a b u v).degree := by have hB_eq : (u₁ * v₂ + u₂ * v₁) * (c : F[X]) = maninB a b u v := by apply mul_left_cancel₀ (mul_ne_zero hv10 hv20) calc (v₁ * v₂) * ((u₁ * v₂ + u₂ * v₁) * (c : F[X])) = (u₁ * v₂ + u₂ * v₁) * ((v₁ * v₂) * (c : F[X])) := by ring _ = (u₁ * v₂ + u₂ * v₁) * maninA u v := by rw [hcA] _ = maninB a b u v * (v₁ * v₂) := hAB_poly _ = (v₁ * v₂) * maninB a b u v := by ring calc (u₁ * v₂ + u₂ * v₁).degree = (u₁ * v₂ + u₂ * v₁).degree + (c : F[X]).degree := by rw [hc_deg, add_zero] _ = ((u₁ * v₂ + u₂ * v₁) * (c : F[X])).degree := degree_mul.symm _ = (maninB a b u v).degree := by rw [hB_eq] have huu_deg : (u₁ * u₂).degree = (maninC a b u v).degree := by have hC_eq : (u₁ * u₂) * (c : F[X]) = maninC a b u v := by apply mul_left_cancel₀ (mul_ne_zero hv10 hv20) calc (v₁ * v₂) * ((u₁ * u₂) * (c : F[X])) = (u₁ * u₂) * ((v₁ * v₂) * (c : F[X])) := by ring _ = (u₁ * u₂) * maninA u v := by rw [hcA] _ = maninC a b u v * (v₁ * v₂) := hAC_poly _ = (v₁ * v₂) * maninC a b u v := by ring calc (u₁ * u₂).degree = (u₁ * u₂).degree + (c : F[X]).degree := by rw [hc_deg, add_zero] _ = (u₁ * u₂ * (c : F[X])).degree := degree_mul.symm _ = (maninC a b u v).degree := by rw [hC_eq] have hcoe : ((maninD a b (.some x0 y0 hR + P) + maninD a b (.some x0 y0 hR - P) : ℕ) : WithBot ℕ) = ((2 * maninD a b (.some x0 y0 hR) + 2 : ℕ) : WithBot ℕ) := by push_cast rw [haddP, hsubP, maninD_some_coe, maninD_some_coe, maninD_some_coe] change pairWeight u₁ v₁ + pairWeight u₂ v₂ = 2 * pairWeight u v + 2 rw [pairWeight_add_pairWeight, hvv_deg, huv_deg, huu_deg, maninABC_max_degree a b hv0] exact WithBot.coe_injective hcoe /-! ### Section 2.2: The Frobenius Point `Q`, Base Values `D(Q) = q` and `D(Q - P) = #E(𝔽_q)` -/ variable [Fintype F] /-- Over a finite field `F` of cardinality `q`, raising a polynomial to the `q`-th power fixes coefficients and sends `X` to `X^q`: `(X³ + a X + b)^q = (X^q)³ + a X^q + b`. -/ lemma cubicPoly_pow_card (a b : F) : (cubicPoly a b) ^ Fintype.card F = X ^ (3 * Fintype.card F) + C a * X ^ Fintype.card F + C b := by obtain ⟨n, hp, hcard⟩ := FiniteField.card F (ringChar F) have : Fact (ringChar F).Prime := ⟨hp⟩ have h1 : (cubicPoly a b) ^ (ringChar F) ^ (n : ℕ) = (X ^ 3) ^ (ringChar F) ^ (n : ℕ) + (C a * X) ^ (ringChar F) ^ (n : ℕ) + (C b) ^ (ringChar F) ^ (n : ℕ) := by unfold cubicPoly rw [add_pow_char_pow, add_pow_char_pow] rw [hcard, h1, mul_pow, ← pow_mul, ← map_pow, ← map_pow, ← hcard, FiniteField.pow_card a, FiniteField.pow_card b] /-- Oddness of `Fintype.card F` when `(2 : F) ≠ 0`. -/ lemma odd_card_of_two_ne_zero (h2 : (2 : F) ≠ 0) : Odd (Fintype.card F) := by obtain ⟨n, hp, hcard⟩ := FiniteField.card F (ringChar F) rw [hcard] refine Odd.pow ?_ rcases hp.eq_two_or_odd' with h2eq | hodd · exfalso have : CharP F 2 := h2eq ▸ ringChar.charP F exact h2 (CharP.cast_eq_zero F 2) · exact hodd lemma maninQ_nonsingular {a b : F} (h2 : (2 : F) ≠ 0) (hΔ : 4 * a ^ 3 + 27 * b ^ 2 ≠ 0) : (twistCurve a b).toAffine.Nonsingular (algebraMap F[X] (RatFunc F) (cubicPoly a b) * RatFunc.X ^ Fintype.card F) ((algebraMap F[X] (RatFunc F) (cubicPoly a b)) ^ 2 * (algebraMap F[X] (RatFunc F) (cubicPoly a b)) ^ ((Fintype.card F - 1) / 2)) := by set q := Fintype.card F set h := (q - 1) / 2 set fK : RatFunc F := algebraMap F[X] (RatFunc F) (cubicPoly a b) refine shortWeierstrass_nonsingular_of_eq (ratFunc_two_ne_zero h2) (twistCurve_disc_ne_zero hΔ) ?_ have hodd := odd_card_of_two_ne_zero h2 have h2h1 : 2 * h + 1 = q := by rcases hodd with ⟨k, hk⟩ have hqk : q = 2 * k + 1 := hk have : h = k := by simp only [h, hqk]; omega omega have hfq : fK ^ q = RatFunc.X ^ (3 * q) + RatFunc.C a * RatFunc.X ^ q + RatFunc.C b := by change (algebraMap F[X] (RatFunc F) (cubicPoly a b)) ^ q = _ rw [← map_pow, cubicPoly_pow_card a b] simp only [map_add, map_mul, map_pow]; rfl calc (fK ^ 2 * fK ^ h) ^ 2 = fK ^ 3 * fK ^ (2 * h + 1) := by ring _ = fK ^ 3 * fK ^ q := by rw [h2h1] _ = fK ^ 3 * (RatFunc.X ^ (3 * q) + RatFunc.C a * RatFunc.X ^ q + RatFunc.C b) := by rw [hfq] _ = (fK * RatFunc.X ^ q) ^ 3 + (RatFunc.C a * fK ^ 2) * (fK * RatFunc.X ^ q) + RatFunc.C b * fK ^ 3 := by have : (RatFunc.X ^ q : RatFunc F) ^ 3 = RatFunc.X ^ (3 * q) := by rw [← pow_mul, mul_comm] rw [mul_pow, this]; ring /-- The Frobenius point `Q = (fK * X^q, fK² * fK^((q-1)/2))` (untwisted `(X^q, f^((q-1)/2))`) on `twistCurve a b`. -/ noncomputable def maninQ {a b : F} (h2 : (2 : F) ≠ 0) (hΔ : 4 * a ^ 3 + 27 * b ^ 2 ≠ 0) : (twistCurve a b).toAffine.Point := .some (algebraMap F[X] (RatFunc F) (cubicPoly a b) * RatFunc.X ^ Fintype.card F) ((algebraMap F[X] (RatFunc F) (cubicPoly a b)) ^ 2 * (algebraMap F[X] (RatFunc F) (cubicPoly a b)) ^ ((Fintype.card F - 1) / 2)) (maninQ_nonsingular h2 hΔ) lemma maninD_Q {a b : F} (h2 : (2 : F) ≠ 0) (hΔ : 4 * a ^ 3 + 27 * b ^ 2 ≠ 0) : maninD a b (maninQ h2 hΔ) = Fintype.card F := by set fK : RatFunc F := algebraMap F[X] (RatFunc F) (cubicPoly a b) have hfK : fK ≠ 0 := fK_ne_zero a b change (let x := (fK * RatFunc.X ^ Fintype.card F) / fK; if x.num = 0 then x.denom.natDegree + 1 else max x.num.natDegree (x.denom.natDegree + 1)) = Fintype.card F dsimp only rw [mul_div_cancel_left₀ _ hfK] have hXq : (RatFunc.X ^ Fintype.card F : RatFunc F) = algebraMap F[X] (RatFunc F) (X ^ Fintype.card F) := by rw [map_pow]; rfl rw [hXq, RatFunc.num_algebraMap, RatFunc.denom_algebraMap] have hne : (X ^ Fintype.card F : F[X]) ≠ 0 := pow_ne_zero _ Polynomial.X_ne_zero simp only [hne, ↓reduceIte, natDegree_X_pow, natDegree_one, zero_add] exact max_eq_left Fintype.card_pos /-- **Base Value `D(Q - P) = #E(𝔽_q)`**: Explicit root-multiplicity cancellation of `(X - α)²` in `x(Q - P) = N(X) / (X^q - X)²`. -/ theorem maninD_Q_sub_P {a b : F} (h2 : (2 : F) ≠ 0) (hΔ : 4 * a ^ 3 + 27 * b ^ 2 ≠ 0) : maninD a b (maninQ h2 hΔ - maninP h2 hΔ) = pointCount a b := by set q := Fintype.card F set h := (q - 1) / 2 set f := cubicPoly a b set fK : RatFunc F := algebraMap F[X] (RatFunc F) f have hfK : fK ≠ 0 := fK_ne_zero a b have hodd := odd_card_of_two_ne_zero h2 have hq3 : 3 ≤ q := by have hq1 : 1 < q := Fintype.one_lt_card rcases hodd with ⟨k, hk⟩ have hqk : q = 2 * k + 1 := hk omega have h2h1 : 2 * h + 1 = q := by rcases hodd with ⟨k, hk⟩ have hqk : q = 2 * k + 1 := hk have : h = k := by simp only [h, hqk]; omega omega have hh_pos : 0 < h := by omega have hXq_ne_X : (RatFunc.X ^ q : RatFunc F) ≠ RatFunc.X := by intro heq have h0' : algebraMap F[X] (RatFunc F) (X ^ q - X) = 0 := by simp only [map_sub, map_pow] change (RatFunc.X ^ q : RatFunc F) - RatFunc.X = 0 exact sub_eq_zero.mpr heq have h0 : (X ^ q - X : F[X]) = 0 := (ratFunc_algebraMap_eq_zero_iff _).mp h0' have hdeg := FiniteField.X_pow_card_sub_X_natDegree_eq F (Fintype.one_lt_card (α := F)) rw [h0, natDegree_zero] at hdeg omega have hxQ_ne : fK * RatFunc.X ^ q ≠ fK * RatFunc.X := fun heq => hXq_ne_X (mul_left_cancel₀ hfK heq) have hPns := maninP_nonsingular h2 hΔ have hQns := maninQ_nonsingular h2 hΔ have hnegPns := ((twistCurve a b).toAffine.nonsingular_neg (fK * RatFunc.X) (fK ^ 2)).mpr hPns have hnegP_eq : -maninP h2 hΔ = WeierstrassCurve.Affine.Point.some (fK * RatFunc.X) ((twistCurve a b).toAffine.negY (fK * RatFunc.X) (fK ^ 2)) hnegPns := WeierstrassCurve.Affine.Point.neg_some hPns have hQ_eq : maninQ h2 hΔ = WeierstrassCurve.Affine.Point.some (fK * RatFunc.X ^ q) (fK ^ 2 * fK ^ h) hQns := rfl have hsubQP : maninQ h2 hΔ - maninP h2 hΔ = WeierstrassCurve.Affine.Point.some _ _ ((twistCurve a b).toAffine.nonsingular_add hQns hnegPns (fun h' => hxQ_ne h'.1)) := by rw [sub_eq_add_neg, hQ_eq, hnegP_eq] exact WeierstrassCurve.Affine.Point.add_some (fun h' => hxQ_ne h'.1) set N : F[X] := f * (f ^ h + 1) ^ 2 - (X ^ q + X) * (X ^ q - X) ^ 2 set M : F[X] := (X ^ q - X) ^ 2 have hM_ne : M ≠ 0 := pow_ne_zero 2 (FiniteField.X_pow_card_sub_X_ne_zero F Fintype.one_lt_card) set xQP := (twistCurve a b).toAffine.addX (fK * RatFunc.X ^ q) (fK * RatFunc.X) ((twistCurve a b).toAffine.slope (fK * RatFunc.X ^ q) (fK * RatFunc.X) (fK ^ 2 * fK ^ h) ((twistCurve a b).toAffine.negY (fK * RatFunc.X) (fK ^ 2))) / fK have hxQP_eq : xQP = algebraMap F[X] (RatFunc F) N / algebraMap F[X] (RatFunc F) M := by change (twistCurve a b).toAffine.addX (fK * RatFunc.X ^ q) (fK * RatFunc.X) ((twistCurve a b).toAffine.slope (fK * RatFunc.X ^ q) (fK * RatFunc.X) (fK ^ 2 * fK ^ h) ((twistCurve a b).toAffine.negY (fK * RatFunc.X) (fK ^ 2))) / fK = _ rw [WeierstrassCurve.Affine.slope_of_X_ne hxQ_ne] have hsub_ne : (RatFunc.X ^ q : RatFunc F) - RatFunc.X ≠ 0 := sub_ne_zero.mpr hXq_ne_X have hN_eq : algebraMap F[X] (RatFunc F) N = fK * (fK ^ h + 1) ^ 2 - (RatFunc.X ^ q + RatFunc.X) * (RatFunc.X ^ q - RatFunc.X) ^ 2 := by simp only [N, fK, map_add, map_sub, map_mul, map_pow, map_one]; rfl have hM_eq : algebraMap F[X] (RatFunc F) M = (RatFunc.X ^ q - RatFunc.X) ^ 2 := by simp only [M, map_sub, map_pow]; rfl rw [hN_eq, hM_eq] change (((fK ^ 2 * fK ^ h - (-fK ^ 2 - 0 * (fK * RatFunc.X) - 0)) / (fK * RatFunc.X ^ q - fK * RatFunc.X)) ^ 2 + 0 * ((fK ^ 2 * fK ^ h - (-fK ^ 2 - 0 * (fK * RatFunc.X) - 0)) / (fK * RatFunc.X ^ q - fK * RatFunc.X)) - 0 - fK * RatFunc.X ^ q - fK * RatFunc.X) / fK = _ field_simp [hfK, hsub_ne] ring have hM_deg : M.natDegree = 2 * q := by unfold M rw [natDegree_pow, FiniteField.X_pow_card_sub_X_natDegree_eq F Fintype.one_lt_card] have hN_deg : N.degree = ((2 * q + 1 : ℕ) : WithBot ℕ) := by have hN_expand : N = X ^ (2 * q + 1) + (X ^ (q + 2) + C a * X ^ q + C b + C 2 * f ^ (h + 1) + f - X ^ 3) := by have hf_exp : f * (f ^ h + 1) ^ 2 = f ^ q + C 2 * f ^ (h + 1) + f := by have h1 : f * (f ^ h) ^ 2 = f ^ q := by rw [← pow_mul, mul_comm h 2, ← pow_succ', h2h1] have h2' : (2 : F[X]) = C (2 : F) := (map_ofNat (Polynomial.C (R := F)) 2).symm calc f * (f ^ h + 1) ^ 2 = f * (f ^ h) ^ 2 + 2 * (f * f ^ h) + f := by ring _ = f ^ q + C 2 * f ^ (h + 1) + f := by rw [h1, h2', ← pow_succ'] have hX_exp : ((X ^ q + X) * (X ^ q - X) ^ 2 : F[X]) = X ^ (3 * q) - X ^ (2 * q + 1) - X ^ (q + 2) + X ^ 3 := by have h3q : (X ^ q : F[X]) ^ 3 = X ^ (3 * q) := by rw [← pow_mul, mul_comm] have h2q1 : (X ^ q : F[X]) ^ 2 * X = X ^ (2 * q + 1) := by rw [← pow_mul, mul_comm q 2, ← pow_succ] have hq2 : (X ^ q : F[X]) * X ^ 2 = X ^ (q + 2) := by rw [← pow_add] linear_combination h3q - h2q1 - hq2 unfold N rw [hf_exp, cubicPoly_pow_card a b, hX_exp] ring have hf_deg : f.degree ≤ ((3 : ℕ) : WithBot ℕ) := by unfold f cubicPoly refine (degree_add_le _ _).trans (max_le ((degree_add_le _ _).trans (max_le ?_ ?_)) ?_) · rw [degree_X_pow] · calc (C a * X).degree = (C a).degree + X.degree := degree_mul _ ≤ 0 + 1 := add_le_add degree_C_le (by rw [degree_X]) _ ≤ ((3 : ℕ) : WithBot ℕ) := by rw [zero_add]; decide · exact degree_C_le.trans (by decide) have hlt_0 : 0 < 2 * q + 1 := by omega have hlt_3 : 3 < 2 * q + 1 := by omega have hlt_q2 : q + 2 < 2 * q + 1 := by omega have hlt_q : q < 2 * q + 1 := by omega have hlt_3h : 3 * (h + 1) < 2 * q + 1 := by omega have hlt : (X ^ (q + 2) + C a * X ^ q + C b + C 2 * f ^ (h + 1) + f - X ^ 3).degree < (X ^ (2 * q + 1) : F[X]).degree := by rw [degree_X_pow] refine (degree_sub_le _ _).trans_lt (max_lt ?_ (by rw [degree_X_pow]; exact WithBot.coe_lt_coe.mpr hlt_3)) refine (degree_add_le _ _).trans_lt (max_lt ?_ (hf_deg.trans_lt (WithBot.coe_lt_coe.mpr hlt_3))) refine (degree_add_le _ _).trans_lt (max_lt ?_ ?_) · refine (degree_add_le _ _).trans_lt (max_lt ?_ (degree_C_le.trans_lt (WithBot.coe_lt_coe.mpr hlt_0))) refine (degree_add_le _ _).trans_lt (max_lt ?_ ?_) · rw [degree_X_pow]; exact WithBot.coe_lt_coe.mpr hlt_q2 · calc (C a * X ^ q).degree = (C a).degree + (X ^ q).degree := degree_mul _ ≤ 0 + (q : WithBot ℕ) := add_le_add degree_C_le (by rw [degree_X_pow]) _ < ((2 * q + 1 : ℕ) : WithBot ℕ) := by rw [zero_add]; exact WithBot.coe_lt_coe.mpr hlt_q · calc (C 2 * f ^ (h + 1)).degree = (C (2 : F)).degree + (h + 1) • f.degree := by rw [degree_mul, degree_pow] _ ≤ 0 + (h + 1) • ((3 : ℕ) : WithBot ℕ) := add_le_add degree_C_le (nsmul_le_nsmul_right hf_deg _) _ = ((3 * (h + 1) : ℕ) : WithBot ℕ) := by rw [zero_add, nsmul_eq_mul]; norm_cast; ring _ < ((2 * q + 1 : ℕ) : WithBot ℕ) := WithBot.coe_lt_coe.mpr hlt_3h rw [hN_expand, degree_add_eq_left_of_degree_lt hlt, degree_X_pow] have hN_ne : N ≠ 0 := fun h0 => by rw [h0, degree_zero] at hN_deg exact WithBot.bot_ne_coe hN_deg have hN_natDeg : N.natDegree = 2 * q + 1 := natDegree_eq_of_degree_eq_some hN_deg set u := xQP.num set v := xQP.denom have hv_ne : v ≠ 0 := RatFunc.denom_ne_zero xQP have huM_eq : u * M = N * v := by apply RatFunc.algebraMap_injective F have hvK : algebraMap F[X] (RatFunc F) v ≠ 0 := by rw [ne_eq, ratFunc_algebraMap_eq_zero_iff]; exact hv_ne have hMK : algebraMap F[X] (RatFunc F) M ≠ 0 := by rw [ne_eq, ratFunc_algebraMap_eq_zero_iff]; exact hM_ne have heq : algebraMap F[X] (RatFunc F) u / algebraMap F[X] (RatFunc F) v = algebraMap F[X] (RatFunc F) N / algebraMap F[X] (RatFunc F) M := by rw [RatFunc.num_div_denom, hxQP_eq] simp only [map_mul] exact (div_eq_div_iff hvK hMK).mp heq have hv_dvd_M : v ∣ M := (RatFunc.isCoprime_num_denom xQP).symm.dvd_of_dvd_mul_left ⟨N, by rw [huM_eq, mul_comm]⟩ obtain ⟨g, hMgv⟩ := hv_dvd_M have hg_ne : g ≠ 0 := fun h0 => hM_ne (by rw [hMgv, h0, mul_zero]) have hNgu : N = u * g := mul_left_cancel₀ hv_ne (by calc v * N = N * v := by ring _ = u * M := huM_eq.symm _ = u * (v * g) := by rw [hMgv] _ = v * (u * g) := by ring) have hu_ne : u ≠ 0 := fun h0 => hN_ne (by rw [hNgu, h0, zero_mul]) have hu_natDeg : u.natDegree = v.natDegree + 1 := by have h1 : N.natDegree = u.natDegree + g.natDegree := by rw [hNgu, natDegree_mul hu_ne hg_ne] have h2' : M.natDegree = v.natDegree + g.natDegree := by rw [hMgv, natDegree_mul hv_ne hg_ne] omega have hD_eq : maninD a b (maninQ h2 hΔ - maninP h2 hΔ) = v.natDegree + 1 := by rw [hsubQP] change (if u = 0 then v.natDegree + 1 else max u.natDegree (v.natDegree + 1)) = v.natDegree + 1 rw [if_neg hu_ne, hu_natDeg, max_self] rw [hD_eq, pointCount_eq_sum_fiber h2 hΔ, add_comm] congr 1 have hXsub_roots : (X ^ q - X : F[X]).roots = Finset.univ.val := FiniteField.roots_X_pow_card_sub_X F have hXsub_splits : (X ^ q - X : F[X]).Splits := by rw [splits_iff_card_roots, hXsub_roots, ← Finset.card_def, Finset.card_univ, FiniteField.X_pow_card_sub_X_natDegree_eq F (Fintype.one_lt_card (α := F))] have hM_splits : M.Splits := Splits.pow hXsub_splits 2 have hv_splits : v.Splits := Splits.of_dvd hM_splits hM_ne ⟨g, hMgv⟩ have hv_natDeg_sum : v.natDegree = ∑ α : F, v.rootMultiplicity α := by rw [← splits_iff_card_roots.mp hv_splits] have : v.roots.card = ∑ α ∈ Finset.univ, v.roots.count α := by rw [← Multiset.toFinset_sum_count_eq v.roots] refine Finset.sum_subset (Finset.subset_univ _) fun α _ hα => ?_ exact Multiset.count_eq_zero.mpr (by simpa using hα) rw [this] refine Finset.sum_congr rfl fun α _ => count_roots v rw [hv_natDeg_sum] refine Finset.sum_congr rfl fun α _ => ?_ have hM_mult : M.rootMultiplicity α = 2 := by have hXne : (X ^ q - X : F[X]) ≠ 0 := FiniteField.X_pow_card_sub_X_ne_zero F (Fintype.one_lt_card (α := F)) unfold M rw [sq, rootMultiplicity_mul (mul_ne_zero hXne hXne)] have h1 : (X ^ q - X : F[X]).rootMultiplicity α = 1 := by rw [← count_roots, hXsub_roots] exact Multiset.count_eq_one_of_mem Finset.univ.nodup (Finset.mem_univ α) rw [h1] have hvg_mult : v.rootMultiplicity α + g.rootMultiplicity α = 2 := by rw [← rootMultiplicity_mul (mul_ne_zero hv_ne hg_ne), ← hMgv, hM_mult] have hug_mult : u.rootMultiplicity α + g.rootMultiplicity α = N.rootMultiplicity α := by rw [← rootMultiplicity_mul (mul_ne_zero hu_ne hg_ne), ← hNgu] have huv_zero : u.rootMultiplicity α = 0 ∨ v.rootMultiplicity α = 0 := by by_contra hboth push Not at hboth have hdu : (X - C α) ∣ u := dvd_iff_isRoot.mpr ((rootMultiplicity_pos hu_ne).mp (Nat.pos_of_ne_zero hboth.1)) have hdv : (X - C α) ∣ v := dvd_iff_isRoot.mpr ((rootMultiplicity_pos hv_ne).mp (Nat.pos_of_ne_zero hboth.2)) have hu_lin : IsUnit (X - C α) := (RatFunc.isCoprime_num_denom xQP).isUnit_of_dvd' hdu hdv have : (X - C α).degree = 0 := degree_eq_zero_of_isUnit hu_lin rw [degree_X_sub_C] at this exact absurd this (by decide) have hXα2_dvd_M : (X - C α) ^ 2 ∣ M := by rw [← le_rootMultiplicity_iff hM_ne, hM_mult] have hXα2_dvd_term2 : (X - C α) ^ 2 ∣ (X ^ q + X) * (X ^ q - X) ^ 2 := dvd_mul_of_dvd_right hXα2_dvd_M _ set c := α ^ 3 + a * α + b have hfc : f.eval α = c := eval_cubicPoly a b α by_cases hc0 : c = 0 · simp only [hc0, ↓reduceIte] have h1 : (X - C α) ∣ N := by have hf1 : (X - C α) ∣ f := dvd_iff_isRoot.mpr (by rw [IsRoot, hfc, hc0]) exact dvd_sub (dvd_mul_of_dvd_left hf1 _) (dvd_trans (dvd_pow_self _ (by norm_num)) hXα2_dvd_term2) have hnot2 : ¬ (X - C α) ^ 2 ∣ N := by intro h2dvd have h2f : (X - C α) ^ 2 ∣ f * (f ^ h + 1) ^ 2 := by have : f * (f ^ h + 1) ^ 2 = N + (X ^ q + X) * (X ^ q - X) ^ 2 := by ring rw [this] exact dvd_add h2dvd hXα2_dvd_term2 have hnot_rhs : ¬ (X - C α) ∣ (f ^ h + 1) ^ 2 := by rw [dvd_iff_isRoot, IsRoot, eval_pow, eval_add, eval_pow, eval_one, hfc, hc0, zero_pow hh_pos.ne', zero_add, one_pow] exact one_ne_zero have hcop_lin : IsCoprime ((X - C α) ^ 2) ((f ^ h + 1) ^ 2) := by refine IsCoprime.pow_left ?_ have hirr : Irreducible (X - C α) := irreducible_X_sub_C α exact (hirr.coprime_iff_not_dvd).mpr hnot_rhs have h2f' : (X - C α) ^ 2 ∣ f := hcop_lin.dvd_of_dvd_mul_right h2f have hf'_zero : f.derivative.eval α = 0 := by obtain ⟨r, hr⟩ := h2f' rw [hr, derivative_mul, derivative_pow, derivative_X_sub_C] simp have hd_ne := cubic_deriv_ne_zero_of_root hΔ hc0 rw [derivative_cubicPoly] at hf'_zero simp at hf'_zero exact hd_ne hf'_zero have hN_mult1 : N.rootMultiplicity α = 1 := by have hge1 : 1 ≤ N.rootMultiplicity α := (le_rootMultiplicity_iff hN_ne).mpr (by simpa using h1) have hlt2 : ¬ 2 ≤ N.rootMultiplicity α := by rw [le_rootMultiplicity_iff hN_ne]; exact hnot2 omega omega · simp only [hc0, ↓reduceIte] have hrc : ringChar F ≠ 2 := by intro hrc2 have : CharP F 2 := hrc2 ▸ ringChar.charP F exact h2 (CharP.cast_eq_zero F 2) have hh_eq : h = Fintype.card F / 2 := by rcases hodd with ⟨k, hk⟩ have hqk : q = 2 * k + 1 := hk simp only [h, q, hk]; omega by_cases hsq : IsSquare c · simp only [hsq, ↓reduceIte] have hch : c ^ h = 1 := by have hqcf : quadraticCharFun F c = 1 := by simp [quadraticCharFun, hc0, hsq] rw [quadraticCharFun_eq_pow_of_char_ne_two hrc hc0] at hqcf split_ifs at hqcf with hp1 exact hh_eq ▸ hp1 have hN_eval : N.eval α ≠ 0 := by have hXα : (X ^ q - X : F[X]).eval α = 0 := by simp [q, FiniteField.pow_card α] simp only [N, eval_sub, eval_mul, eval_pow, eval_add, eval_one, hfc, hch, hXα, zero_pow (by norm_num : 2 ≠ 0), mul_zero, sub_zero] have h22 : (1 + 1 : F) ^ 2 = 2 * 2 := by ring rw [h22] exact mul_ne_zero hc0 (mul_ne_zero h2 h2) have hN_mult0 : N.rootMultiplicity α = 0 := rootMultiplicity_eq_zero_iff.mpr (fun hr => absurd hr hN_eval) omega · simp only [hsq, ↓reduceIte] have hch : c ^ h = -1 := by have hqcf : quadraticCharFun F c = -1 := by simp [quadraticCharFun, hc0, hsq] rw [quadraticCharFun_eq_pow_of_char_ne_two hrc hc0] at hqcf have hne1 : c ^ (Fintype.card F / 2) ≠ 1 := by intro hp1; rw [if_pos hp1] at hqcf; norm_num at hqcf have hsq1 : (c ^ h) ^ 2 = 1 := by rw [← pow_mul, mul_comm h 2] have h2h : 2 * h = q - 1 := by omega rw [h2h] exact FiniteField.pow_card_sub_one_eq_one c hc0 rcases sq_eq_one_iff.mp hsq1 with hp1 | hm1 · exact absurd (hh_eq ▸ hp1) hne1 · exact hm1 have hlin : (X - C α) ∣ (f ^ h + 1) := by rw [dvd_iff_isRoot, IsRoot, eval_add, eval_pow, eval_one, hfc, hch] ring have h2dvd : (X - C α) ^ 2 ∣ N := dvd_sub (dvd_mul_of_dvd_right (pow_dvd_pow_of_dvd hlin 2) f) hXα2_dvd_term2 have hN_ge2 : 2 ≤ N.rootMultiplicity α := (le_rootMultiplicity_iff hN_ne).mpr h2dvd omega /-- **Exact Closed Form for `D(Q + n • P)`** for ALL integers `n ∈ ℤ`: `D(Q + n • P) = n² + (q + 1 - #E(𝔽_q)) n + q`. -/ theorem maninD_closed_form {a b : F} (h2 : (2 : F) ≠ 0) (hΔ : 4 * a ^ 3 + 27 * b ^ 2 ≠ 0) (n : ℤ) : (maninD a b (maninQ h2 hΔ + n • maninP h2 hΔ) : ℤ) = n ^ 2 + ((Fintype.card F : ℤ) + 1 - (pointCount a b : ℤ)) * n + (Fintype.card F : ℤ) := by set P := maninP h2 hΔ set Q := maninQ h2 hΔ set q : ℤ := (Fintype.card F : ℤ) set t : ℤ := q + 1 - (pointCount a b : ℤ) have hrec : ∀ k : ℤ, (maninD a b (Q + (k + 1) • P) : ℤ) + (maninD a b (Q + (k - 1) • P) : ℤ) = 2 * (maninD a b (Q + k • P) : ℤ) + 2 := by intro k have h1 : Q + (k + 1) • P = (Q + k • P) + P := by rw [add_zsmul, one_zsmul, add_assoc] have h2' : Q + (k - 1) • P = (Q + k • P) - P := by rw [sub_zsmul, one_zsmul, add_sub_assoc] rfl rw [h1, h2'] exact_mod_cast maninD_recurrence h2 hΔ (Q + k • P) have h0 : (maninD a b (Q + (0 : ℤ) • P) : ℤ) = 0 ^ 2 + t * 0 + q := by rw [zero_zsmul, add_zero] have hDQ : maninD a b Q = Fintype.card F := maninD_Q h2 hΔ rw [hDQ]; simp [q] have hm1 : (maninD a b (Q + (-1 : ℤ) • P) : ℤ) = (-1) ^ 2 + t * (-1) + q := by have : Q + (-1 : ℤ) • P = Q - P := by rw [neg_one_zsmul, sub_eq_add_neg] rw [this, maninD_Q_sub_P h2 hΔ] simp only [t, q]; ring have hpos : ∀ m : ℕ, (maninD a b (Q + (m : ℤ) • P) : ℤ) = (m : ℤ) ^ 2 + t * m + q := by intro m induction m using Nat.strongRecOn with | ind m ih => match m with | 0 => simpa using h0 | 1 => have hr := hrec 0 have h0' : (maninD a b (Q + (0 : ℤ) • P) : ℤ) = q := by linarith [h0] have hm1' : (maninD a b (Q + (0 - 1 : ℤ) • P) : ℤ) = 1 - t + q := by have hsub : (0 - 1 : ℤ) = -1 := by norm_num rw [hsub]; linarith [hm1] have hadd1 : (0 + 1 : ℤ) = (1 : ℤ) := by norm_num rw [hadd1] at hr rw [Nat.cast_one] linarith | k + 2 => have hr := hrec (k + 1 : ℕ) have hk1 := ih (k + 1) (by omega) have hk0 := ih k (by omega) have hcast1 : ((k + 1 : ℕ) : ℤ) + 1 = ((k + 2 : ℕ) : ℤ) := by push_cast; ring have hcast2 : ((k + 1 : ℕ) : ℤ) - 1 = (k : ℤ) := by push_cast; ring rw [hcast1, hcast2] at hr push_cast at hk1 hk0 hr ⊢ linarith have hneg : ∀ m : ℕ, (maninD a b (Q + (-(m : ℤ)) • P) : ℤ) = (-(m : ℤ)) ^ 2 + t * (-(m : ℤ)) + q := by intro m induction m using Nat.strongRecOn with | ind m ih => match m with | 0 => simpa using h0 | 1 => simpa using hm1 | k + 2 => have hr := hrec (-((k + 1 : ℕ) : ℤ)) have hk1 := ih (k + 1) (by omega) have hk0 := ih k (by omega) have hcast1 : -((k + 1 : ℕ) : ℤ) + 1 = -(k : ℤ) := by push_cast; ring have hcast2 : -((k + 1 : ℕ) : ℤ) - 1 = -((k + 2 : ℕ) : ℤ) := by push_cast; ring rw [hcast1, hcast2] at hr push_cast at hk1 hk0 hr ⊢ linarith cases n with | ofNat m => exact hpos m | negSucc m => have : Int.negSucc m = -((m + 1 : ℕ) : ℤ) := by omega rw [this] exact hneg (m + 1) /-- **CROWN JEWEL — THE HASSE–WEIL THEOREM (Exact Integer Form)**: For ANY finite field `F` of characteristic `≠ 2` and ANY nonsingular short Weierstrass elliptic curve `E : y² = x³ + a x + b` (`4 a³ + 27 b² ≠ 0`), `(#E(𝔽_q) - (q + 1))² ≤ 4 q`. -/ theorem hasse_weil_bound {a b : F} (h2 : (2 : F) ≠ 0) (hΔ : 4 * a ^ 3 + 27 * b ^ 2 ≠ 0) : ((pointCount a b : ℤ) - ((Fintype.card F : ℤ) + 1)) ^ 2 ≤ 4 * (Fintype.card F : ℤ) := by set q : ℤ := (Fintype.card F : ℤ) set t : ℤ := q + 1 - (pointCount a b : ℤ) have hquad : ∀ n : ℤ, 0 ≤ n ^ 2 + t * n + q := by intro n rw [← maninD_closed_form h2 hΔ n] exact Nat.cast_nonneg _ have ht2 : ((pointCount a b : ℤ) - (q + 1)) ^ 2 = t ^ 2 := by simp only [t]; ring rw [ht2] rcases Int.even_or_odd t with ⟨k, hk⟩ | ⟨k, hk⟩ · have hnk := hquad (-k) rw [hk] at hnk ⊢ nlinarith · have hnk := hquad (-k) rw [hk] at hnk ⊢ have hge : k * (k + 1) ≤ q := by nlinarith have hne : k * (k + 1) ≠ q := by intro heq have heven : Even (k * (k + 1)) := by rcases Int.even_or_odd k with ⟨m, rfl⟩ | ⟨m, rfl⟩ · exact ⟨m * (m + m + 1), by ring⟩ · exact ⟨(2 * m + 1) * (m + 1), by ring⟩ have hq_odd : Odd q := by rcases odd_card_of_two_ne_zero h2 with ⟨m, hm⟩ refine ⟨(m : ℤ), ?_⟩ simp only [q, hm]; push_cast; ring rw [heq] at heven exact (Int.not_even_iff_odd.mpr hq_odd) heven have hstrict : k * (k + 1) + 1 ≤ q := lt_of_le_of_ne hge hne nlinarith /-- **THE HASSE–WEIL THEOREM (Real Absolute-Value Form)**: `|#E(𝔽_q) - (q + 1)| ≤ 2 √q`. -/ theorem hasse_weil_bound_real {a b : F} (h2 : (2 : F) ≠ 0) (hΔ : 4 * a ^ 3 + 27 * b ^ 2 ≠ 0) : |(pointCount a b : ℝ) - ((Fintype.card F : ℝ) + 1)| ≤ 2 * Real.sqrt (Fintype.card F) := by have hZ := hasse_weil_bound h2 hΔ have hR : ((pointCount a b : ℝ) - ((Fintype.card F : ℝ) + 1)) ^ 2 ≤ (2 * Real.sqrt (Fintype.card F)) ^ 2 := by have hcast : ((pointCount a b : ℝ) - ((Fintype.card F : ℝ) + 1)) ^ 2 ≤ 4 * (Fintype.card F : ℝ) := by exact_mod_cast hZ rw [mul_pow, Real.sq_sqrt (Nat.cast_nonneg _)] linarith exact abs_le_of_sq_le_sq hR (by positivity) end TwistGroupLaw end ManinProof /-! ## Section 3: Deuring's Hasse Invariant, Supersingular Dichotomy & Infinite Families -/ section DeuringSupersingular /-- The **Hasse invariant** `A_p(E) ∈ 𝔽_p`: the coefficient of `X^{p-1}` in `(X³ + a X + b)^{(p-1)/2}`. -/ noncomputable def hasseInvariant (p : ℕ) (a b : ZMod p) : ZMod p := ((cubicPoly a b) ^ ((p - 1) / 2)).coeff (p - 1) /-- Finite-field power sum over `ZMod p`: for prime `p` and `k < 2(p - 1)`, `∑_{x : ZMod p} x^k = -1` if `k = p - 1`, and `0` otherwise. -/ lemma zmod_sum_pow_eq (p : ℕ) [Fact p.Prime] {k : ℕ} (hk_lt : k < 2 * (p - 1)) : (∑ x : ZMod p, x ^ k) = if k = p - 1 then -1 else 0 := by have hp : p.Prime := Fact.out have hp1 : 0 < p - 1 := Nat.sub_pos_of_lt hp.one_lt by_cases hk0 : k = 0 · subst hk0 have hne : (0 : ℕ) ≠ p - 1 := hp1.ne simp [hne] · by_cases hkp : k = p - 1 · subst hkp simp only [↓reduceIte] have hsplit : ∑ x : ZMod p, x ^ (p - 1) = (0 : ZMod p) ^ (p - 1) + ∑ x ∈ Finset.univ.erase (0 : ZMod p), x ^ (p - 1) := by rw [← Finset.add_sum_erase _ _ (Finset.mem_univ (0 : ZMod p))] rw [hsplit, zero_pow hp1.ne', zero_add] have hones : ∑ x ∈ Finset.univ.erase (0 : ZMod p), x ^ (p - 1) = ∑ _x ∈ Finset.univ.erase (0 : ZMod p), (1 : ZMod p) := by refine Finset.sum_congr rfl fun x hx => ?_ exact ZMod.pow_card_sub_one_eq_one (Finset.ne_of_mem_erase hx) rw [hones, Finset.sum_const, Finset.card_erase_of_mem (Finset.mem_univ _), Finset.card_univ, ZMod.card p, nsmul_eq_mul, mul_one] rw [Nat.cast_sub hp.one_lt.le, ZMod.natCast_self, Nat.cast_one, zero_sub] · simp only [hkp, ↓reduceIte] have hndvd : ¬ (p - 1) ∣ k := by rintro ⟨m, rfl⟩ rcases m with _ | _ | m · exact hk0 (by simp) · exact hkp (by simp) · have hge : 2 * (p - 1) ≤ (p - 1) * (m + 1 + 1) := by calc 2 * (p - 1) = (p - 1) * 2 := mul_comm _ _ _ ≤ (p - 1) * (m + 1 + 1) := Nat.mul_le_mul_left _ (by omega) exact absurd hk_lt (not_lt_of_ge hge) obtain ⟨g, hg⟩ := IsCyclic.exists_generator (α := (ZMod p)ˣ) have hgk_ne : (g : ZMod p) ^ k ≠ 1 := by intro h1 have hgu : g ^ k = 1 := Units.ext (by simpa using h1) have hord : orderOf g ∣ k := orderOf_dvd_of_pow_eq_one hgu rw [orderOf_eq_card_of_forall_mem_zpowers hg, Nat.card_eq_fintype_card, ZMod.card_units_eq_totient, Nat.totient_prime hp] at hord exact hndvd hord have hmul : (g : ZMod p) ^ k * ∑ x : ZMod p, x ^ k = ∑ x : ZMod p, x ^ k := by rw [Finset.mul_sum] have heq : ∑ x : ZMod p, (g : ZMod p) ^ k * x ^ k = ∑ x : ZMod p, ((g : ZMod p) * x) ^ k := Finset.sum_congr rfl fun x _ => (mul_pow (g : ZMod p) x k).symm rw [heq] exact Fintype.sum_equiv (Equiv.mulLeft₀ (g : ZMod p) g.ne_zero) _ _ (fun _ => rfl) have hdiff : ((g : ZMod p) ^ k - 1) * ∑ x : ZMod p, x ^ k = 0 := by linear_combination hmul exact (mul_eq_zero.mp hdiff).resolve_left (sub_ne_zero.mpr hgk_ne) /-- **Deuring's Exact Congruence mod `p`**: For any prime `p ≥ 5` and nonsingular short Weierstrass curve `E : y² = x³ + a x + b` over `𝔽_p`, `(#E(𝔽_p) : ZMod p) = 1 - A_p(E)`. -/ theorem pointCount_mod_p_eq_one_sub_hasseInvariant {p : ℕ} [Fact p.Prime] (hp5 : 5 ≤ p) {a b : ZMod p} (hΔ : 4 * a ^ 3 + 27 * b ^ 2 ≠ 0) : (pointCount a b : ZMod p) = 1 - hasseInvariant p a b := by have hp : p.Prime := Fact.out have h2 : (2 : ZMod p) ≠ 0 := by intro h0 have : p ∣ 2 := (ZMod.natCast_eq_zero_iff 2 p).mp (by simpa using h0) have := Nat.le_of_dvd (by norm_num) this omega have hrc : ringChar (ZMod p) ≠ 2 := by rw [ZMod.ringChar_zmod_n]; omega have hZ := pointCount_eq_quadraticChar_sum h2 hΔ have hcast := congr_arg (Int.cast : ℤ → ZMod p) hZ simp only [Int.cast_natCast, Int.cast_add, Int.cast_one, Int.cast_sum, ZMod.card p, ZMod.natCast_self, zero_add] at hcast rw [hcast] have heuler : ∀ c : ZMod p, (quadraticChar (ZMod p) c : ZMod p) = c ^ ((p - 1) / 2) := by intro c have hh : (p - 1) / 2 = Fintype.card (ZMod p) / 2 := by rw [ZMod.card p] have hodd : Odd p := hp.odd_of_ne_two (by omega) rcases hodd with ⟨k, rfl⟩ omega have hh_pos : 0 < (p - 1) / 2 := by omega by_cases hc : c = 0 · subst hc; simp [hh_pos.ne'] · change (quadraticCharFun (ZMod p) c : ZMod p) = _ rw [quadraticCharFun_eq_pow_of_char_ne_two hrc hc, ← hh] split_ifs with h1 · simp [h1] · have hsq : (c ^ ((p - 1) / 2)) ^ 2 = 1 := by rw [← pow_mul] have h2m : (p - 1) / 2 * 2 = p - 1 := by have hodd : Odd p := hp.odd_of_ne_two (by omega) rcases hodd with ⟨k, rfl⟩; omega rw [h2m]; exact ZMod.pow_card_sub_one_eq_one hc rcases sq_eq_one_iff.mp hsq with h1' | hm1 · exact absurd h1' h1 · simp [hm1] have hsum_eq : ∑ x : ZMod p, (quadraticChar (ZMod p) (x ^ 3 + a * x + b) : ZMod p) = - hasseInvariant p a b := by have h1 : ∑ x : ZMod p, (quadraticChar (ZMod p) (x ^ 3 + a * x + b) : ZMod p) = ∑ x : ZMod p, ((cubicPoly a b) ^ ((p - 1) / 2)).eval x := by refine Finset.sum_congr rfl fun x _ => ?_ rw [heuler, eval_pow, eval_cubicPoly] rw [h1] set P := (cubicPoly a b) ^ ((p - 1) / 2) have hdegP : P.natDegree < 2 * (p - 1) := by have hf3 : (cubicPoly a b).natDegree ≤ 3 := by unfold cubicPoly refine (natDegree_add_le _ _).trans (max_le ((natDegree_add_le _ _).trans (max_le ?_ ?_)) ?_) · simp · exact natDegree_mul_le.trans (by simp) · simp calc P.natDegree ≤ (p - 1) / 2 * (cubicPoly a b).natDegree := natDegree_pow_le _ ≤ (p - 1) / 2 * 3 := Nat.mul_le_mul_left _ hf3 _ < 2 * (p - 1) := by omega have hP_eval : ∀ x : ZMod p, P.eval x = ∑ k ∈ Finset.range (2 * (p - 1)), P.coeff k * x ^ k := eval_eq_sum_range' hdegP simp_rw [hP_eval] rw [Finset.sum_comm] have hterm : ∑ k ∈ Finset.range (2 * (p - 1)), ∑ x : ZMod p, P.coeff k * x ^ k = ∑ k ∈ Finset.range (2 * (p - 1)), P.coeff k * (if k = p - 1 then -1 else 0) := by refine Finset.sum_congr rfl fun k hk => ?_ rw [← Finset.mul_sum, zmod_sum_pow_eq p (Finset.mem_range.mp hk)] rw [hterm] have hmem : p - 1 ∈ Finset.range (2 * (p - 1)) := Finset.mem_range.mpr (by omega) rw [Finset.sum_eq_single (p - 1)] · simp [hasseInvariant, P] · intro k _ hk; simp [hk] · intro hnot; exact absurd hmem hnot rw [hsum_eq, sub_eq_add_neg] /-- **DEURING'S SUPERSINGULAR DICHOTOMY (Unconditional via Hasse–Weil!)**: For EVERY prime `p ≥ 5` and EVERY elliptic curve `E : y² = x³ + a x + b` over `𝔽_p`: `A_p(E) = 0 ↔ p ∣ t_p ↔ #E(𝔽_p) = p + 1`! -/ theorem deuring_supersingular_dichotomy {p : ℕ} [Fact p.Prime] (hp5 : 5 ≤ p) {a b : ZMod p} (hΔ : 4 * a ^ 3 + 27 * b ^ 2 ≠ 0) : (hasseInvariant p a b = 0 ↔ (p : ℤ) ∣ ((p : ℤ) + 1 - pointCount a b)) ∧ ((p : ℤ) ∣ ((p : ℤ) + 1 - pointCount a b) ↔ pointCount a b = p + 1) := by have hp : p.Prime := Fact.out have h2 : (2 : ZMod p) ≠ 0 := by intro h0 have : p ∣ 2 := (ZMod.natCast_eq_zero_iff 2 p).mp (by simpa using h0) have := Nat.le_of_dvd (by norm_num) this omega have hcong := pointCount_mod_p_eq_one_sub_hasseInvariant hp5 hΔ have htrace_mod : (((p : ℤ) + 1 - pointCount a b : ℤ) : ZMod p) = hasseInvariant p a b := by push_cast rw [ZMod.natCast_self, hcong] ring have hpart1 : hasseInvariant p a b = 0 ↔ (p : ℤ) ∣ ((p : ℤ) + 1 - pointCount a b) := by rw [← htrace_mod, ZMod.intCast_zmod_eq_zero_iff_dvd] have hpart2 : (p : ℤ) ∣ ((p : ℤ) + 1 - pointCount a b) ↔ pointCount a b = p + 1 := by constructor · rintro ⟨m, hm⟩ have hHW := hasse_weil_bound h2 hΔ rw [ZMod.card p] at hHW have hsq : ((p : ℤ) * m) ^ 2 ≤ 4 * (p : ℤ) := by have : ((pointCount a b : ℤ) - ((p : ℤ) + 1)) ^ 2 = ((p : ℤ) * m) ^ 2 := by have : (pointCount a b : ℤ) - ((p : ℤ) + 1) = -((p : ℤ) * m) := by linarith rw [this, neg_sq] linarith have hm0 : m = 0 := by by_contra hmnz have hm2 : 1 ≤ m ^ 2 := by have : 1 ≤ |m| := Int.one_le_abs hmnz have hsq' : (1 : ℤ) ^ 2 ≤ |m| ^ 2 := by nlinarith rwa [sq_abs, one_pow] at hsq' have hp_int : (5 : ℤ) ≤ p := by exact_mod_cast hp5 have hp2 : (p : ℤ) ^ 2 ≤ (p : ℤ) ^ 2 * m ^ 2 := le_mul_of_one_le_right (sq_nonneg _) hm2 have hp2' : ((p : ℤ) * m) ^ 2 = (p : ℤ) ^ 2 * m ^ 2 := mul_pow _ _ 2 nlinarith rw [hm0, mul_zero] at hm omega · intro hN rw [hN]; push_cast; simp exact ⟨hpart1, hpart2⟩ /-- **Infinite Supersingular Family 1 (`j = 0`)**: For ALL primes `p ≥ 5` with `p ≡ 2 (mod 3)` and ALL `b ≠ 0`, `y² = x³ + b` is supersingular: `A_p(E) = 0` and `#E(𝔽_p) = p + 1`. -/ theorem supersingular_j0_family {p : ℕ} [Fact p.Prime] (hp5 : 5 ≤ p) (hp3 : p % 3 = 2) {b : ZMod p} (hb : b ≠ 0) : hasseInvariant p 0 b = 0 ∧ pointCount (0 : ZMod p) b = p + 1 := by have hp : p.Prime := Fact.out have hΔ : 4 * (0 : ZMod p) ^ 3 + 27 * b ^ 2 ≠ 0 := by have h27 : (27 : ZMod p) ≠ 0 := by intro h0 have hdvd : p ∣ 27 := (ZMod.natCast_eq_zero_iff 27 p).mp (by simpa using h0) have : p ∣ 3 := hp.dvd_of_dvd_pow (show p ∣ 3 ^ 3 from hdvd) have := Nat.le_of_dvd (by norm_num) this omega simp only [zero_pow (by norm_num : 3 ≠ 0), mul_zero, zero_add] exact mul_ne_zero h27 (pow_ne_zero 2 hb) have hA : hasseInvariant p 0 b = 0 := by unfold hasseInvariant cubicPoly simp only [map_zero, zero_mul, add_zero] rw [add_pow, finsetSum_coeff] refine Finset.sum_eq_zero fun k _ => ?_ rw [mul_assoc, ← C_pow, ← C_eq_natCast, ← C_mul, coeff_mul_C] rw [← pow_mul, coeff_X_pow] split_ifs with heq · exfalso; omega · simp have hdic := deuring_supersingular_dichotomy hp5 hΔ exact ⟨hA, hdic.2.mp (hdic.1.mp hA)⟩ /-- **Infinite Supersingular Family 2 (`j = 1728`)**: For ALL primes `p ≥ 5` with `p ≡ 3 (mod 4)` and ALL `a ≠ 0`, `y² = x³ + a x` is supersingular: `A_p(E) = 0` and `#E(𝔽_p) = p + 1`. -/ theorem supersingular_j1728_family {p : ℕ} [Fact p.Prime] (hp5 : 5 ≤ p) (hp4 : p % 4 = 3) {a : ZMod p} (ha : a ≠ 0) : hasseInvariant p a 0 = 0 ∧ pointCount a (0 : ZMod p) = p + 1 := by have hp : p.Prime := Fact.out have hΔ : 4 * a ^ 3 + 27 * (0 : ZMod p) ^ 2 ≠ 0 := by have h4 : (4 : ZMod p) ≠ 0 := by intro h0 have hdvd : p ∣ 4 := (ZMod.natCast_eq_zero_iff 4 p).mp (by simpa using h0) have : p ∣ 2 := hp.dvd_of_dvd_pow (show p ∣ 2 ^ 2 from hdvd) have := Nat.le_of_dvd (by norm_num) this omega simp only [zero_pow (by norm_num : 2 ≠ 0), mul_zero, add_zero] exact mul_ne_zero h4 (pow_ne_zero 3 ha) have hA : hasseInvariant p a 0 = 0 := by unfold hasseInvariant cubicPoly simp only [map_zero, add_zero] have hfac : (X ^ 3 + C a * X : (ZMod p)[X]) = X * (X ^ 2 + C a) := by ring rw [hfac, mul_pow] set m := (p - 1) / 2 have h2m : m + m = p - 1 := by omega rw [← h2m, coeff_X_pow_mul, add_pow, finsetSum_coeff] refine Finset.sum_eq_zero fun k _ => ?_ rw [mul_assoc, ← C_pow, ← C_eq_natCast, ← C_mul, coeff_mul_C] rw [← pow_mul, coeff_X_pow] split_ifs with heq · exfalso; omega · simp have hdic := deuring_supersingular_dichotomy hp5 hΔ exact ⟨hA, hdic.2.mp (hdic.1.mp hA)⟩ local instance instFactPrime7 : Fact (Nat.Prime 7) := ⟨by decide⟩ local instance instFactPrime5 : Fact (Nat.Prime 5) := ⟨by decide⟩ /-- **Ordinary Witness 1**: Over `𝔽₇` (`7 ≡ 1 mod 3`), `y² = x³ + 1` is ordinary with `A₇(E) = 3 ≠ 0` and `#E(𝔽₇) = 12 ≠ 8`. -/ theorem ordinary_witness_j0_mod7 : hasseInvariant 7 0 1 = 3 ∧ pointCount (0 : ZMod 7) 1 = 12 := by have hΔ : 4 * (0 : ZMod 7) ^ 3 + 27 * (1 : ZMod 7) ^ 2 ≠ 0 := by decide have h2 : (2 : ZMod 7) ≠ 0 := by decide have hA : hasseInvariant 7 0 1 = 3 := by unfold hasseInvariant cubicPoly simp only [map_zero, zero_mul, add_zero, map_one] have hpow : ((X ^ 3 + 1 : (ZMod 7)[X]) ^ ((7 - 1) / 2)) = X ^ 9 + C (3 : ZMod 7) * X ^ 6 + C (3 : ZMod 7) * X ^ 3 + 1 := by have h3 : (7 - 1) / 2 = 3 := rfl have hC3 : C (3 : ZMod 7) = 3 := rfl rw [h3, hC3]; ring rw [hpow] simp [coeff_X_pow, coeff_one] have hN : pointCount (0 : ZMod 7) 1 = 12 := by rw [pointCount_eq_sum_fiber h2 hΔ] decide exact ⟨hA, hN⟩ /-- **Ordinary Witness 2**: Over `𝔽₅` (`5 ≡ 1 mod 4`), `y² = x³ + x` is ordinary with `A₅(E) = 2 ≠ 0` and `#E(𝔽₅) = 4 ≠ 6`. -/ theorem ordinary_witness_j1728_mod5 : hasseInvariant 5 1 0 = 2 ∧ pointCount (1 : ZMod 5) 0 = 4 := by have hΔ : 4 * (1 : ZMod 5) ^ 3 + 27 * (0 : ZMod 5) ^ 2 ≠ 0 := by decide have h2 : (2 : ZMod 5) ≠ 0 := by decide have hA : hasseInvariant 5 1 0 = 2 := by unfold hasseInvariant cubicPoly simp only [map_one, one_mul, map_zero, add_zero] have hpow : ((X ^ 3 + X : (ZMod 5)[X]) ^ ((5 - 1) / 2)) = X ^ 6 + C (2 : ZMod 5) * X ^ 4 + X ^ 2 := by have h2' : (5 - 1) / 2 = 2 := rfl have hC2 : C (2 : ZMod 5) = 2 := rfl rw [h2', hC2]; ring rw [hpow] simp [coeff_X_pow] have hN : pointCount (1 : ZMod 5) 0 = 4 := by rw [pointCount_eq_sum_fiber h2 hΔ] decide exact ⟨hA, hN⟩ end DeuringSupersingular end EllipticHasseWeil #print axioms EllipticHasseWeil.pointCount_eq_quadraticChar_sum #print axioms EllipticHasseWeil.quadraticTwist_pointCount_sum #print axioms EllipticHasseWeil.maninD_recurrence #print axioms EllipticHasseWeil.maninD_Q_sub_P #print axioms EllipticHasseWeil.maninD_closed_form #print axioms EllipticHasseWeil.hasse_weil_bound #print axioms EllipticHasseWeil.hasse_weil_bound_real #print axioms EllipticHasseWeil.pointCount_mod_p_eq_one_sub_hasseInvariant #print axioms EllipticHasseWeil.deuring_supersingular_dichotomy #print axioms EllipticHasseWeil.supersingular_j0_family #print axioms EllipticHasseWeil.supersingular_j1728_family #print axioms EllipticHasseWeil.ordinary_witness_j0_mod7 #print axioms EllipticHasseWeil.ordinary_witness_j1728_mod5