Birch and Swinnerton-Dyer: certified ranks in Lean
ECCompute: Lean that certifies rank lower bounds for 480 elliptic curves, up to rank ≥ 31. Certify more, rerun it, read what 2026 claimed about BSD

| 1 | /- |
| 2 | Copyright (c) 2026 Bhavik Mehta. All rights reserved. |
| 3 | Released under the GNU General Public License version 3.0 as described in the file LICENSE. |
| 4 | Authors: Bhavik Mehta |
| 5 | -/ |
| 6 | module |
| 7 | |
| 8 | public import Mathlib.AlgebraicGeometry.EllipticCurve.Affine.Point |
| 9 | public import Mathlib.Data.ZMod.Basic |
| 10 | public import ECCompute.Theory.Model |
| 11 | |
| 12 | import Mathlib.Algebra.Field.ZMod |
| 13 | import Mathlib.NumberTheory.LegendreSymbol.QuadraticChar.Basic |
| 14 | |
| 15 | /-! |
| 16 | # The descent character |
| 17 | |
| 18 | For an elliptic curve `E : y² = f(x)` with `f = x³ + a₂x² + a₄x + a₆` a monic integral cubic |
| 19 | of non-zero discriminant, a prime `p ∤ 6Δ`, and a root `θ ∈ 𝔽ₚ` of `f`, this file defines the |
| 20 | *descent character* `λ_{p,θ} : E(ℚ) → ZMod 2` as a raw function, together with the arithmetic |
| 21 | hypotheses `DescentHyp`, the collinearity identity behind its additivity, and the arithmetic of |
| 22 | the Legendre symbol `ψ_p`. |
| 23 | |
| 24 | For a point `P = (x, y) = (u/w², v/w³)` on `E`, set `α := u - θ·w² = x.num - θ·x.den` in |
| 25 | `ZMod p` (when `p ∤ w`, i.e. `(x.den : ZMod p) ≠ 0`). Then `λ(O) = 0`; `λ(P) = 0` if `p ∣ w`; |
| 26 | `λ(P) = ψ_p(f'(θ))` if `α = 0` (the tangent case); and `λ(P) = ψ_p(α)` otherwise, where |
| 27 | `ψ_p : ZMod p → ZMod 2` is the Legendre symbol (`0` on squares, `1` on non-squares). |
| 28 | |
| 29 | ## Main declarations |
| 30 | |
| 31 | * `ECCompute.psi`: the Legendre symbol into `ZMod 2`; `ECCompute.lambda`: the raw character. |
| 32 | * `ECCompute.DescentHyp`: the arithmetic hypotheses `p ∤ 6Δ`, `f(θ) ≡ 0`. |
| 33 | * `ECCompute.prod_sub_theta_eq_lineSq`: the collinearity identity `(x₁-θ)(x₂-θ)(x₃-θ) = (ℓθ+m)²`. |
| 34 | * `ECCompute.vieta_of_roots`, `ECCompute.vieta_of_double_root`, `ECCompute.fderiv_eq_prod`: |
| 35 | the Vieta relations of the secant line and the derivative-as-product identity at a root. |
| 36 | * `ECCompute.psi_mul`: `ψ_p` turns products into sums on nonzero elements. |
| 37 | * `ECCompute.psi_collinear`, `ECCompute.fderiv_ne_zero`: the collinear-sum and simple-root facts. |
| 38 | -/ |
| 39 | |
| 40 | open WeierstrassCurve |
| 41 | |
| 42 | namespace ECCompute |
| 43 | |
| 44 | section |
| 45 | |
| 46 | open Classical in |
| 47 | /-- The Legendre symbol pushed into `(ZMod 2, +)`: `0` on squares (including `0`), `1` on |
| 48 | non-squares. -/ |
| 49 | @[expose] public noncomputable def psi (p : ℕ) (a : ZMod p) : ZMod 2 := if IsSquare a then 0 else 1 |
| 50 | |
| 51 | variable {a₂ a₄ a₆ : ℤ} {p : ℕ} |
| 52 | |
| 53 | /-- The value `f(θ) = θ³ + a₂θ² + a₄θ + a₆`. -/ |
| 54 | @[expose] public def fval {R : Type*} [CommRing R] (a₂ a₄ a₆ θ : R) : R := |
| 55 | θ ^ 3 + a₂ * θ ^ 2 + a₄ * θ + a₆ |
| 56 | |
| 57 | /-- The value `f'(θ) = 3θ² + 2a₂θ + a₄`. -/ |
| 58 | @[expose] public def fderiv {R : Type*} [CommRing R] (a₂ a₄ θ : R) : R := |
| 59 | 3 * θ ^ 2 + 2 * a₂ * θ + a₄ |
| 60 | |
| 61 | /-- The descent character as a raw function. -/ |
| 62 | @[expose] public noncomputable def lambda (θ : ZMod p) : |
| 63 | (curveQ a₂ a₄ a₆).toAffine.Point → ZMod 2 |
| 64 | | .zero => 0 |
| 65 | | .some x _ _ => |
| 66 | if (x.den : ZMod p) = 0 then 0 |
| 67 | else |
| 68 | let α : ZMod p := x.num - θ * x.den |
| 69 | if α = 0 then psi p (fderiv (a₂ : ZMod p) a₄ θ) else psi p α |
| 70 | |
| 71 | @[simp, grind =] |
| 72 | public theorem lambda_zero {θ : ZMod p} : lambda θ (0 : (curveQ a₂ a₄ a₆).toAffine.Point) = 0 := rfl |
| 73 | |
| 74 | /-! ### The hypotheses of the descent lemma |
| 75 | |
| 76 | `p ∤ 6Δ` is expressed as `p` prime, `p ∤ 6` (so `p ≠ 2, 3`), and the integer discriminant a |
| 77 | unit mod `p` (the coefficients being integers, `Δ` is an integer, so `(curve …).Δ.num` is it). -/ |
| 78 | |
| 79 | /-- Arithmetic hypotheses of the descent lemma for the label `(p, θ)`. -/ |
| 80 | public structure DescentHyp (a₂ a₄ a₆ : ℤ) (p : ℕ) (θ : ZMod p) : Prop where |
| 81 | /-- `p` is prime. -/ |
| 82 | prime : p.Prime |
| 83 | /-- `p ∤ 6` (equivalently `p ≠ 2` and `p ≠ 3`). -/ |
| 84 | ne_six : ¬ p ∣ 6 |
| 85 | /-- `p ∤ Δ`: the (integer) discriminant is invertible mod `p`. -/ |
| 86 | discr : ((curveQ a₂ a₄ a₆).Δ.num : ZMod p) ≠ 0 |
| 87 | /-- `θ` is a root of `f` mod `p`, i.e. `f(θ) ≡ 0`. -/ |
| 88 | root : fval (a₂ : ZMod p) a₄ a₆ θ = 0 |
| 89 | |
| 90 | attribute [grind →] DescentHyp.discr DescentHyp.root |
| 91 | |
| 92 | end |
| 93 | |
| 94 | /-! ### The collinearity identity |
| 95 | |
| 96 | A line `y = ℓx + m` meeting `E` in three points with `x`-coordinates `x₁, x₂, x₃` gives |
| 97 | `f(x) - (ℓx + m)² = (x - x₁)(x - x₂)(x - x₃)`. At a root `θ` of `f` this yields |
| 98 | `(x₁ - θ)(x₂ - θ)(x₃ - θ) = (ℓθ + m)²`, a perfect square, used in the additivity proof. -/ |
| 99 | |
| 100 | section CommRing |
| 101 | |
| 102 | variable {R : Type*} [CommRing R] {a₂ a₄ a₆ ℓ m x₁ x₂ x₃ θ : R} |
| 103 | |
| 104 | /-- The Vieta relations of the line `y = ℓx + m` meeting the cubic `f = x³ + a₂x² + a₄x + a₆` |
| 105 | at `x₁, x₂, x₃`. -/ |
| 106 | public structure Vieta (a₂ a₄ a₆ ℓ m x₁ x₂ x₃ : R) : Prop where |
| 107 | σ₁ : x₁ + x₂ + x₃ = ℓ ^ 2 - a₂ |
| 108 | σ₂ : x₁ * x₂ + x₁ * x₃ + x₂ * x₃ = a₄ - 2 * ℓ * m |
| 109 | σ₃ : x₁ * x₂ * x₃ = m ^ 2 - a₆ |
| 110 | |
| 111 | attribute [grind →] Vieta.σ₁ Vieta.σ₂ Vieta.σ₃ |
| 112 | |
| 113 | /-- If `x₁, x₂, x₃` and the line `y = ℓx + m` satisfy the Vieta relations, the cubic |
| 114 | `x³ + a₂x² + a₄x + a₆ - (ℓx + m)²` factors as `(x - x₁)(x - x₂)(x - x₃)` at every `x`. -/ |
| 115 | theorem cubic_sub_lineSq_eq_prod (hv : Vieta a₂ a₄ a₆ ℓ m x₁ x₂ x₃) {x : R} : |
| 116 | fval a₂ a₄ a₆ x - (ℓ * x + m) ^ 2 = (x - x₁) * (x - x₂) * (x - x₃) := by |
| 117 | grind [fval] |
| 118 | |
| 119 | /-- Evaluating the collinearity identity at a root `θ` of `f(x) = x³ + a₂x² + a₄x + a₆` gives |
| 120 | `(x₁ - θ)(x₂ - θ)(x₃ - θ) = (ℓθ + m)²`. -/ |
| 121 | public theorem prod_sub_theta_eq_lineSq (hv : Vieta a₂ a₄ a₆ ℓ m x₁ x₂ x₃) |
| 122 | (hθ : fval a₂ a₄ a₆ θ = 0) : |
| 123 | (x₁ - θ) * (x₂ - θ) * (x₃ - θ) = (ℓ * θ + m) ^ 2 := by |
| 124 | grind [fval, cubic_sub_lineSq_eq_prod hv] |
| 125 | |
| 126 | /-- If the line `y = ℓx + m` is tangent to `E` at `(x₁, ℓx₁ + m)` (point on curve `hpt`, |
| 127 | slope condition `f'(x₁) = 2ℓ(ℓx₁ + m)` as `htan`), then `x₁` is a double root and the Vieta |
| 128 | relations hold for the triple `x₁, x₁, x₃` with `x₃ = ℓ² - a₂ - 2x₁`. Doubling analogue of |
| 129 | `vieta_of_roots`. -/ |
| 130 | public theorem vieta_of_double_root |
| 131 | (hpt : (ℓ * x₁ + m) ^ 2 = fval a₂ a₄ a₆ x₁) |
| 132 | (htan : fderiv a₂ a₄ x₁ = 2 * ℓ * (ℓ * x₁ + m)) |
| 133 | (hx₃ : x₃ = ℓ ^ 2 - a₂ - 2 * x₁) : |
| 134 | Vieta a₂ a₄ a₆ ℓ m x₁ x₁ x₃ := by |
| 135 | refine ⟨?_, ?_, ?_⟩ <;> grind [fval, fderiv] |
| 136 | |
| 137 | end CommRing |
| 138 | |
| 139 | section Field |
| 140 | |
| 141 | variable {F : Type*} [Field F] {a₂ a₄ a₆ ℓ m x₁ x₂ x₃ θ : F} |
| 142 | |
| 143 | /-- The Vieta relations for the line `y = ℓx + m` meeting `E` at `x₁, x₂, x₃`, recovered from |
| 144 | the two points `(x₁, ℓx₁ + m)` and `(x₂, ℓx₂ + m)` lying on the curve (with `x₁ ≠ x₂`) and the |
| 145 | group-law value `x₃ = ℓ² - a₂ - x₁ - x₂` for the third `x`-coordinate. -/ |
| 146 | public theorem vieta_of_roots (hne : x₁ ≠ x₂) |
| 147 | (hx₃ : x₃ = ℓ ^ 2 - a₂ - x₁ - x₂) |
| 148 | (h₁ : (ℓ * x₁ + m) ^ 2 = fval a₂ a₄ a₆ x₁) |
| 149 | (h₂ : (ℓ * x₂ + m) ^ 2 = fval a₂ a₄ a₆ x₂) : |
| 150 | Vieta a₂ a₄ a₆ ℓ m x₁ x₂ x₃ := by |
| 151 | refine ⟨?_, ?_, ?_⟩ <;> grind (ringSteps := 200000) [fval] |
| 152 | |
| 153 | /-- If `θ` is a root of `f` and one collinear `x`-coordinate equals `θ` (here `x₁ = θ`), then |
| 154 | `f'(θ) = (x₂ - θ)(x₃ - θ)`. Analogue of `prod_sub_theta_eq_lineSq` for the tangent (`2`-torsion |
| 155 | mod `p`) branch, where the factor `X_i - θ` is replaced by `f'(θ)`. -/ |
| 156 | public theorem fderiv_eq_prod (ℓ m : F) (hv : Vieta a₂ a₄ a₆ ℓ m x₁ x₂ x₃) |
| 157 | (hθ : fval a₂ a₄ a₆ θ = 0) (h1 : x₁ = θ) : |
| 158 | fderiv a₂ a₄ θ = (x₂ - θ) * (x₃ - θ) := by |
| 159 | grind [fval, fderiv, cubic_sub_lineSq_eq_prod hv] |
| 160 | |
| 161 | end Field |
| 162 | |
| 163 | /-! ### The Legendre character `ψ_p` is a homomorphism away from zero |
| 164 | |
| 165 | On the nonzero elements of `ZMod p` (`p` an odd prime), `ψ_p` is the quadratic-residue character |
| 166 | transported along `{±1} ≅ ZMod 2`, hence additive: `ψ_p(ab) = ψ_p a + ψ_p b`. The simple-root |
| 167 | fact `fderiv_ne_zero` closes the section. -/ |
| 168 | |
| 169 | variable {a₂ a₄ a₆ : ℤ} {p : ℕ} |
| 170 | |
| 171 | section Psi |
| 172 | |
| 173 | variable {a b : ZMod p} |
| 174 | |
| 175 | /-- `ψ_p` vanishes on squares. -/ |
| 176 | public theorem psi_of_isSquare (ha : IsSquare a) : psi p a = 0 := ite_eq_left ha |
| 177 | |
| 178 | /-- On the nonzero elements of `ZMod p` (`p` prime), `ψ_p` turns products into sums: |
| 179 | `ψ_p(ab) = ψ_p a + ψ_p b`. -/ |
| 180 | public theorem psi_mul (hp : p.Prime) (ha : a ≠ 0) (hb : b ≠ 0) : |
| 181 | psi p (a * b) = psi p a + psi p b := by |
| 182 | have : Fact p.Prime := ⟨hp⟩ |
| 183 | -- `IsSquare (a*b) ↔ (IsSquare a ↔ IsSquare b)` on nonzero elements, via `quadraticChar`. |
| 184 | have key : IsSquare (a * b) ↔ (IsSquare a ↔ IsSquare b) := by |
| 185 | rw [← quadraticChar_one_iff_isSquare (by grind), ← quadraticChar_one_iff_isSquare ha, |
| 186 | ← quadraticChar_one_iff_isSquare hb, map_mul] |
| 187 | grind [quadraticChar_dichotomy] |
| 188 | grind [psi] |
| 189 | |
| 190 | /-- Multiplying by a nonzero square does not change `ψ_p`. -/ |
| 191 | public theorem psi_mul_sq (hp : p.Prime) (hb : b ≠ 0) : psi p (b ^ 2 * a) = psi p a := by |
| 192 | have : Fact p.Prime := ⟨hp⟩ |
| 193 | rcases eq_or_ne a 0 with rfl | ha |
| 194 | · rw [mul_zero] |
| 195 | · rw [psi_mul hp (pow_ne_zero 2 hb) ha, psi_of_isSquare ⟨b, by ring⟩, zero_add] |
| 196 | |
| 197 | end Psi |
| 198 | |
| 199 | variable {θ : ZMod p} |
| 200 | |
| 201 | /-- If `x₁, x₂, x₃` are the reduced `x`-coordinates of three collinear points on `E` (via the |
| 202 | Vieta relations of `y = ℓx + m`), all distinct from the root `θ`, then |
| 203 | `ψ_p (x₁ - θ) + ψ_p (x₂ - θ) + ψ_p (x₃ - θ) = 0`. -/ |
| 204 | public theorem psi_collinear (hp : p.Prime) {ℓ m x₁ x₂ x₃ : ZMod p} |
| 205 | (hv : Vieta (a₂ : ZMod p) a₄ a₆ ℓ m x₁ x₂ x₃) |
| 206 | (hroot : fval (a₂ : ZMod p) a₄ a₆ θ = 0) |
| 207 | (hx₁ : x₁ ≠ θ) (hx₂ : x₂ ≠ θ) (hx₃ : x₃ ≠ θ) : |
| 208 | psi p (x₁ - θ) + psi p (x₂ - θ) + psi p (x₃ - θ) = 0 := by |
| 209 | have : Fact p.Prime := ⟨hp⟩ |
| 210 | have hpm : psi p ((x₁ - θ) * (x₂ - θ) * (x₃ - θ)) = 0 := by |
| 211 | rw [prod_sub_theta_eq_lineSq hv hroot]; exact psi_of_isSquare ⟨ℓ * θ + m, by ring⟩ |
| 212 | rwa [psi_mul hp (by grind) (by grind), psi_mul hp (by grind) (by grind)] at hpm |
| 213 | |
| 214 | /-- The root `θ` of `f` is simple, so `f'(θ) ≠ 0`. Uses the descent hypotheses `DescentHyp` |
| 215 | (`p ∤ 6Δ`). -/ |
| 216 | public theorem fderiv_ne_zero (h : DescentHyp a₂ a₄ a₆ p θ) : fderiv (a₂ : ZMod p) a₄ θ ≠ 0 := by |
| 217 | intro hfd |
| 218 | apply h.discr |
| 219 | simp only [curveQ_Δ_num, discrInt] |
| 220 | grind [fderiv, fval] |
| 221 | |
| 222 | end ECCompute |