/- Copyright (c) 2026 Bhavik Mehta. All rights reserved. Released under the GNU General Public License version 3.0 as described in the file LICENSE. Authors: Bhavik Mehta -/ module public import ECCompute.Theory.Descent.Character public import ECCompute.Theory.Model public import Mathlib.Algebra.Field.ZMod import ECCompute.ForMathlib.WeierstrassCurve /-! # The finite-field descent character `εpFinite` and its additivity For a good prime `p` and a root `θ ∈ 𝔽ₚ` of `f = x³ + a₂x² + a₄x + a₆`, Cremona's descent character on the reduced curve `E/𝔽ₚ` is `εpFinite θ : E(𝔽ₚ) → ZMod 2`, `εpFinite θ O = 0`, `εpFinite θ (X, Y) = ψ_p(f'(θ))` if `X = θ`, `ψ_p(X - θ)` otherwise, where `ψ_p` is the Legendre symbol pushed into `(ZMod 2, +)`. This file proves that `εpFinite θ` is additive, packaged as `εpHom : E(𝔽ₚ) →+ ZMod 2`; vanishing on `2·E(𝔽ₚ)` is then automatic. ## Main declarations * `ECCompute.εpFinite`: the finite-field descent character `E(𝔽ₚ) → ZMod 2`. * `ECCompute.εpHom`: `εpFinite` packaged as an `AddMonoidHom`. -/ open WeierstrassCurve Affine namespace ECCompute variable {a₂ a₄ a₆ : ℤ} {p : ℕ} /-- The finite-field descent character. On `O` it is `0`; on an affine point `(X, Y)` it is `ψ_p(f'(θ))` in the tangent case `X = θ` and `ψ_p(X - θ)` otherwise. -/ @[expose] public noncomputable def εpFinite (a₂ a₄ a₆ : ℤ) (p : ℕ) (θ : ZMod p) : (curveZMod a₂ a₄ a₆ p).toAffine.Point → ZMod 2 | .zero => 0 | .some X _ _ => if X = θ then psi p (fderiv a₂ a₄ θ) else psi p (X - θ) variable {θ x y x₁ y₁ x₂ y₂ : ZMod p} @[simp] theorem εpFinite_zero : εpFinite a₂ a₄ a₆ p θ 0 = 0 := rfl public theorem εpFinite_some (h : (curveZMod a₂ a₄ a₆ p).toAffine.Nonsingular x y) : εpFinite a₂ a₄ a₆ p θ (.some x y h) = if x = θ then psi p (fderiv a₂ a₄ θ) else psi p (x - θ) := rfl /-- A point `(X, Y)` on the reduced curve satisfies the Weierstrass equation in expanded form. -/ @[local grind →] theorem equation_curveZMod (h : (curveZMod a₂ a₄ a₆ p).toAffine.Nonsingular x y) : y ^ 2 = fval (R := ZMod p) a₂ a₄ a₆ x := by simpa [fval] using curve_baseChange_equation h.1 /-- `p ≠ 2` under the descent hypotheses (from `p ∤ 6`). -/ theorem DescentHyp.ne_two (h : DescentHyp a₂ a₄ a₆ p θ) : p ≠ 2 := fun hp ↦ h.ne_six (hp ▸ ⟨3, rfl⟩) /-- `εpFinite` on an affine point depends only on its `x`-coordinate. -/ theorem εp_x_indep (h₁ : (curveZMod a₂ a₄ a₆ p).toAffine.Nonsingular x₁ y₁) (h₂ : (curveZMod a₂ a₄ a₆ p).toAffine.Nonsingular x₂ y₂) (hx : x₁ = x₂) : εpFinite a₂ a₄ a₆ p θ (.some x₁ y₁ h₁) = εpFinite a₂ a₄ a₆ p θ (.some x₂ y₂ h₂) := by subst hx; rfl /-- For a collinear triple `x₁, x₂, x₃` with the given Vieta relations of the secant line `y = ℓx + m`, the descent-character value at `x₃` equals the sum of the values at `x₁` and `x₂`. -/ theorem εp_sum_of_vieta (h : DescentHyp a₂ a₄ a₆ p θ) {ℓ m x₃ : ZMod p} (hv : Vieta (a₂ : ZMod p) a₄ a₆ ℓ m x₁ x₂ x₃) : (if x₃ = θ then psi p (fderiv a₂ a₄ θ) else psi p (x₃ - θ)) = (if x₁ = θ then psi p (fderiv a₂ a₄ θ) else psi p (x₁ - θ)) + (if x₂ = θ then psi p (fderiv a₂ a₄ θ) else psi p (x₂ - θ)) := by have : Fact p.Prime := ⟨h.prime⟩ have hθroot := h.root have hfd_ne : fderiv (a₂ : ZMod p) a₄ θ ≠ 0 := fderiv_ne_zero h have hfd1 : x₁ = θ → fderiv (a₂ : ZMod p) a₄ θ = (x₂ - θ) * (x₃ - θ) := fderiv_eq_prod ℓ m hv hθroot have hfd2 : x₂ = θ → fderiv (a₂ : ZMod p) a₄ θ = (x₁ - θ) * (x₃ - θ) := fderiv_eq_prod ℓ m ⟨by grind, by grind, by grind⟩ hθroot have hfd3 : x₃ = θ → fderiv (a₂ : ZMod p) a₄ θ = (x₁ - θ) * (x₂ - θ) := fderiv_eq_prod ℓ m ⟨by grind, by grind, by grind⟩ hθroot obtain rfl | c1 := eq_or_ne x₁ θ · rw [ite_eq_right (by grind), ite_eq_left rfl, ite_eq_right (by grind), hfd1 rfl, psi_mul h.prime (by grind) (by grind)] grind obtain rfl | c2 := eq_or_ne x₂ θ · rw [ite_eq_right (by grind), ite_eq_right c1, ite_eq_left rfl, hfd2 rfl, psi_mul h.prime (by grind) (by grind)] grind obtain rfl | c3 := eq_or_ne x₃ θ · rw [ite_eq_left rfl, ite_eq_right c1, ite_eq_right c2, hfd3 rfl, psi_mul h.prime (by grind) (by grind)] · rw [ite_eq_right c3, ite_eq_right c1, ite_eq_right c2] grind [psi_collinear h.prime hv h.root c1 c2 c3] /-- Additivity of `εpFinite` in the secant case: `P = (x₁, y₁)` and `Q = (x₂, y₂)` have distinct `x`-coordinates over `𝔽ₚ`. -/ theorem εpFinite_map_add_of_X_ne [Fact p.Prime] (h : DescentHyp a₂ a₄ a₆ p θ) (h₁ : (curveZMod a₂ a₄ a₆ p).toAffine.Nonsingular x₁ y₁) (h₂ : (curveZMod a₂ a₄ a₆ p).toAffine.Nonsingular x₂ y₂) (hne : x₁ ≠ x₂) : εpFinite a₂ a₄ a₆ p θ (.some x₁ y₁ h₁ + .some x₂ y₂ h₂) = εpFinite a₂ a₄ a₆ p θ (.some x₁ y₁ h₁) + εpFinite a₂ a₄ a₆ p θ (.some x₂ y₂ h₂) := by simp only [Point.add_of_X_ne hne, εpFinite_some] set ℓ := (curveZMod a₂ a₄ a₆ p).toAffine.slope x₁ x₂ y₁ y₂ with hℓdef set x₃ := (curveZMod a₂ a₄ a₆ p).toAffine.addX x₁ x₂ ℓ with hx3def have hℓmul : ℓ * (x₁ - x₂) = y₁ - y₂ := by rw [hℓdef, slope_of_X_ne hne, div_mul_cancel₀ _ (by grind)] have hx3 : x₃ = ℓ ^ 2 - a₂ - x₁ - x₂ := by rw [hx3def, curve_baseChange_addX] exact εp_sum_of_vieta (m := y₁ - ℓ * x₁) h (vieta_of_roots hne hx3 (by grind) (by grind)) /-- For the double-root triple `x, x, x₃` with the given Vieta relations at a root `θ ≠ x`, the descent-character value at `x₃` is `0`. -/ theorem εp_double_of_vieta (h : DescentHyp a₂ a₄ a₆ p θ) {ℓ m x x₃ : ZMod p} (hXθ : x ≠ θ) (hv : Vieta (a₂ : ZMod p) a₄ a₆ ℓ m x x x₃) : (if x₃ = θ then psi p (fderiv a₂ a₄ θ) else psi p (x₃ - θ)) = 0 := by have : Fact p.Prime := ⟨h.prime⟩ have hθroot := h.root obtain rfl | c3 := eq_or_ne x₃ θ · rw [fderiv_eq_prod ℓ m (x₂ := x) (x₃ := x) ⟨by grind, by grind, by grind⟩ hθroot rfl] simp [psi_of_isSquare] rw [ite_eq_right c3] have hpm : psi p ((x - θ) * (x - θ) * (x₃ - θ)) = 0 := by rw [prod_sub_theta_eq_lineSq hv hθroot] exact psi_of_isSquare ⟨ℓ * θ + m, by ring⟩ rwa [← sq, psi_mul_sq h.prime (by grind)] at hpm /-- Additivity of `εpFinite` in the doubling case: `εpFinite` vanishes on `2P` for a point `P = (x, y)` that is not `2`-torsion (`y ≠ 0`). -/ theorem εpFinite_double [Fact p.Prime] (h : DescentHyp a₂ a₄ a₆ p θ) {x y : ZMod p} (hP : (curveZMod a₂ a₄ a₆ p).toAffine.Nonsingular x y) (hy0 : y ≠ 0) : εpFinite a₂ a₄ a₆ p θ (.some x y hP + .some x y hP) = 0 := by have h2 : (2 : ZMod p) ≠ 0 := Ring.two_ne_zero (by simp [ZMod.ringChar_zmod_n, h.ne_two]) have hyne : y ≠ (curveZMod a₂ a₄ a₆ p).toAffine.negY x y := by grind simp only [Point.add_self_of_Y_ne hyne, εpFinite_some] set ℓ := (curveZMod a₂ a₄ a₆ p).toAffine.slope x x y y with hℓdef set x₃ := (curveZMod a₂ a₄ a₆ p).toAffine.addX x x ℓ with hx3def have hℓ : ℓ * (2 * y) = fderiv (a₂ : ZMod p) a₄ x := by rw [hℓdef, slope_of_Y_ne rfl hyne] simp only [curve_baseChange_eq] grind [fderiv] have hx3 : x₃ = ℓ ^ 2 - a₂ - 2 * x := by grind exact εp_double_of_vieta (m := y - ℓ * x) h (by grind) (vieta_of_double_root (by grind) (by grind) hx3) /-- Additivity of `εpFinite`: the finite-field descent character is a homomorphism `(E(𝔽ₚ), +) → (ZMod 2, +)`. -/ theorem εpFinite_map_add [Fact p.Prime] (h : DescentHyp a₂ a₄ a₆ p θ) (P Q : (curveZMod a₂ a₄ a₆ p).toAffine.Point) : εpFinite a₂ a₄ a₆ p θ (P + Q) = εpFinite a₂ a₄ a₆ p θ P + εpFinite a₂ a₄ a₆ p θ Q := by rcases P with _ | ⟨x₁, y₁, h₁⟩ · rw [← Point.zero_def, zero_add, εpFinite_zero, zero_add] rcases Q with _ | ⟨x₂, y₂, h₂⟩ · rw [← Point.zero_def, add_zero, εpFinite_zero, add_zero] by_cases hxy : x₁ = x₂ ∧ y₁ = (curveZMod a₂ a₄ a₆ p).toAffine.negY x₂ y₂ · -- `Q = -P`: the sum is `O`, and both summands share the `x`-coordinate, so `εpP + εpQ = 0`. rw [Point.add_of_Y_eq hxy.1 hxy.2, εpFinite_zero, εp_x_indep h₁ h₂ hxy.1, CharTwo.add_self_eq_zero] · obtain rfl | hne := eq_or_ne x₁ x₂ · -- Doubling: `x₁ = x₂` forces `y₁ = y₂` (not the `-P` case), so `P = Q`; `εp(2P) = 0`. rw [εp_x_indep h₂ h₁ rfl, CharTwo.add_self_eq_zero] grind [εpFinite_double] · -- Secant: `x₁ ≠ x₂`. exact εpFinite_map_add_of_X_ne h h₁ h₂ hne /-- The finite-field descent character `εpFinite θ` as an `AddMonoidHom E(𝔽ₚ) → ZMod 2`. -/ @[expose, simps] public noncomputable def εpHom [Fact p.Prime] (h : DescentHyp a₂ a₄ a₆ p θ) : (curveZMod a₂ a₄ a₆ p).toAffine.Point →+ ZMod 2 where toFun := εpFinite a₂ a₄ a₆ p θ map_zero' := by exact εpFinite_zero map_add' := by exact εpFinite_map_add h end ECCompute