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 | /-! |
| 9 | # Kernel-reducible definitions |
| 10 | |
| 11 | The kernel-reducible `Bool` checkers and the `Nat`/`Int`/`List` arithmetic they fold over. The |
| 12 | correctness proofs and the abstract-typed spec definitions live in `ECCompute.Soundness.*`. |
| 13 | |
| 14 | Currently the `Bool` folds `allBelow`/`allList`, the small-prime trial-division checkers, the monic |
| 15 | residue search, the descent label and character checks, the 𝔽₂ matrix-inverse checker, the |
| 16 | aggregate descent-matrix check, and the point-on-curve check. |
| 17 | -/ |
| 18 | |
| 19 | @[expose] public section |
| 20 | |
| 21 | namespace ECCompute |
| 22 | |
| 23 | /-- `true` iff `p m = true` for every `m < n`. -/ |
| 24 | noncomputable def allBelow (n : Nat) (p : Nat → Bool) : Bool := n.rec true fun m r ↦ (p m).and r |
| 25 | |
| 26 | /-- `true` iff `p a = true` for every `a ∈ l`. -/ |
| 27 | noncomputable def allList {α : Type} (p : α → Bool) : List α → Bool := |
| 28 | List.rec true fun a _ r ↦ (p a).and r |
| 29 | |
| 30 | /-! ## Primes -/ |
| 31 | |
| 32 | /-- `true` iff no `i ∈ L` with `i < x` divides `x`. -/ |
| 33 | noncomputable def passes (x : Nat) : List Nat → Bool := |
| 34 | List.rec true (fun i _ r ↦ ((Nat.ble 1 (x.mod i)).or (x.ble i)).and r) |
| 35 | |
| 36 | /-- `true` iff `p` is a prime below `529 = 23²`, certified by trial division by the primes below |
| 37 | `23` (`ECCompute.passes`). -/ |
| 38 | noncomputable def checkPrime (p : Nat) : Bool := |
| 39 | (Nat.ble 2 p).and ((p.ble 528).and (passes p [2, 3, 5, 7, 11, 13, 17, 19])) |
| 40 | |
| 41 | /-- `true` iff every label's prime component passes `checkPrime`. -/ |
| 42 | noncomputable def checkPrimes (labels : List (Nat × Int)) : Bool := |
| 43 | allList (fun l ↦ checkPrime l.1) labels |
| 44 | |
| 45 | /-! ## Polynomial residue search -/ |
| 46 | |
| 47 | /-- The integer polynomial with coefficients `cs` (constant term first, leading coefficient last), |
| 48 | evaluated at `u`. -/ |
| 49 | noncomputable def polyEval (cs : List Int) (u : Int) : Int := |
| 50 | cs.rec 0 fun c _ acc ↦ c.add (u.mul acc) |
| 51 | |
| 52 | /-- `polyEval` at `r` reduced mod `ℓ` in `Nat`, each coefficient taken to its residue as the Horner |
| 53 | fold reaches it. -/ |
| 54 | noncomputable def polyModL (cs : List Int) (ℓ r : Nat) : Nat := |
| 55 | cs.rec 0 fun c _ acc ↦ ((c.emod ℓ).toNat.add (r.mul acc)).mod ℓ |
| 56 | |
| 57 | /-- The residue mod `ℓ` of the polynomial with coefficients `cs` (constant term first), evaluated at |
| 58 | `r`. -/ |
| 59 | noncomputable def polyModK (cs : List Nat) (ℓ r : Nat) : Nat := |
| 60 | cs.rec 0 fun c _ acc ↦ ((c.mod ℓ).add (r.mul acc)).mod ℓ |
| 61 | |
| 62 | /-- `true` iff the monic integer polynomial with lower coefficients `cs` |
| 63 | (implicit leading coefficient `1`) has no root modulo `ℓ`, trying every residue `0, …, ℓ - 1`. -/ |
| 64 | noncomputable def monicHasNoRootMod (cs : List Int) (ℓ : Nat) : Bool := |
| 65 | allBelow ℓ fun r ↦ ((polyModL (cs ++ [1]) ℓ r).beq 0).not |
| 66 | |
| 67 | /-! ## Descent label check |
| 68 | |
| 69 | For each label `(p, θ)`, `checkLabel` tests `p ∤ 6`, `p ∤ Δ`, and `f(θ) ≡ 0 (mod p)`, reading the |
| 70 | coefficient residues `a₂r, a₄r, a₆r` and the discriminant `Δ` from the certificate. |
| 71 | `descentHyp_of_checkLabels` proves a passing check gives the descent hypotheses. -/ |
| 72 | |
| 73 | /-- The integer discriminant of `y² = x³ + a₂x² + a₄x + a₆`; `discrIntK_eq` identifies it with |
| 74 | `discrInt`. -/ |
| 75 | def discrIntK (a₂ a₄ a₆ : Int) : Int := |
| 76 | let b2 := a₂.mul 4 |
| 77 | let b4 := a₄.mul 2 |
| 78 | let b6 := a₆.mul 4 |
| 79 | ((((b2.mul b2).mul (((a₂.mul 4).mul a₆).sub (a₄.mul a₄))).neg.sub |
| 80 | (((b4.mul b4).mul b4).mul 8)).sub ((b6.mul b6).mul 27)).add |
| 81 | (((b2.mul 9).mul b4).mul b6) |
| 82 | |
| 83 | /-- `true` iff `p ∤ 6`, `p ∤ Δ`, and `f(θ) ≡ 0 (mod p)`, computed from the coefficient residues |
| 84 | `a₂r, a₄r, a₆r`, the discriminant `Δ`, and the root `θ`, reducing each mod `p`. Here |
| 85 | `f(θ) = θ³ + a₂θ² + a₄θ + a₆`. The residues' agreement with the curve mod `p` is `checkLabels`. -/ |
| 86 | noncomputable def checkLabel (a₂r a₄r a₆r : Nat) (Δ : Int) (p : Nat) (θ : Int) : Bool := |
| 87 | let r₂ := a₂r.mod p |
| 88 | let r₄ := a₄r.mod p |
| 89 | let r₆ := a₆r.mod p |
| 90 | ((Nat.mod 6 p).beq 0).not.and |
| 91 | (((Δ.emod p).beq' 0).not.and |
| 92 | ((polyModK [r₆, r₄, r₂, 1] p (θ.emod p).toNat).beq 0)) |
| 93 | |
| 94 | /-- `true` iff `P` is positive, each of `a₂r, a₄r, a₆r` equals the corresponding coefficient mod |
| 95 | `P`, the discriminant `Δ` equals `discrIntK a₂ a₄ a₆`, every label prime divides `P`, and every |
| 96 | label passes `checkLabel`. Sound by `descentHyp_of_checkLabels`. -/ |
| 97 | noncomputable def checkLabels (a₂ a₄ a₆ : Int) (P a₂r a₄r a₆r : Nat) (Δ : Int) |
| 98 | (labels : List (Nat × Int)) : Bool := |
| 99 | (Nat.ble 1 P).and |
| 100 | ((a₂r.beq (a₂.emod (Int.ofNat P)).toNat).and |
| 101 | ((a₄r.beq (a₄.emod (Int.ofNat P)).toNat).and |
| 102 | ((a₆r.beq (a₆.emod (Int.ofNat P)).toNat).and |
| 103 | ((Δ.beq' (discrIntK a₂ a₄ a₆)).and |
| 104 | (allList (fun l ↦ ((P.mod l.1).beq 0).and (checkLabel a₂r a₄r a₆r Δ l.1 l.2)) |
| 105 | labels))))) |
| 106 | |
| 107 | /-! ## Descent character -/ |
| 108 | |
| 109 | /-- Reference quadratic-residue-mask builder: OR together `1 <<< (j² % p)` for `j = 1 .. fuel`. With |
| 110 | `fuel = (p-1)/2` this sets exactly the bits at the nonzero quadratic residues mod an odd prime `p`, |
| 111 | using `Nat` primitives only. -/ |
| 112 | noncomputable def qrMaskGo (p : Nat) : Nat → Nat := |
| 113 | Nat.rec 0 (fun k ih ↦ ih.lor (Nat.shiftLeft 1 ((k.succ.mul k.succ).mod p))) |
| 114 | |
| 115 | /-- The quadratic-residue bitmask mod `p`: bit `a` is set iff `a` is a nonzero square mod `p`. -/ |
| 116 | noncomputable def qrMask (p : Nat) : Nat := qrMaskGo p ((p.sub 1).div 2) |
| 117 | |
| 118 | /-- Residue in `[0, p)` of `x.num - θ·x.den`, for the kernel. -/ |
| 119 | noncomputable def alphaResK (p tval xp xm xden : Nat) : Nat := |
| 120 | ((xp.mod p).add (p.sub ((xm.add (tval.mul xden)).mod p))).mod p |
| 121 | |
| 122 | /-- Residue in `[0, p)` of `f'(θ) = 3θ² + 2a₂θ + a₄`, for the kernel. -/ |
| 123 | noncomputable def fderivResK (a₂ a₄ : Int) (p tval : Nat) : Nat := |
| 124 | polyModL [a₄, Int.mul 2 a₂, 3] p tval |
| 125 | |
| 126 | /-- The descent character `λ_{p,θ}` at a point as a `Nat` bit, `0` or `1`. Spec: `lambdaBitK_eq`. -/ |
| 127 | noncomputable def lambdaBitK (a₂ a₄ : Int) (p qm tval xp xm xden : Nat) : Nat := |
| 128 | ((xden.mod p).beq 0).rec |
| 129 | (((alphaResK p tval xp xm xden).beq 0).rec |
| 130 | (((qm.shiftRight (alphaResK p tval xp xm xden)).land 1).xor 1) |
| 131 | (((qm.shiftRight (fderivResK a₂ a₄ p tval)).land 1).xor 1)) |
| 132 | 0 |
| 133 | |
| 134 | /-! ## 𝔽₂ matrix inverse -/ |
| 135 | |
| 136 | namespace F2Invert |
| 137 | |
| 138 | /-- The XOR of the rows of `M` selected by the set bits of `bi`, as a `Nat` bitmask (`bi` times `M` |
| 139 | over `𝔽₂`). -/ |
| 140 | noncomputable def invRowK (bi : Nat) (M : List Nat) : Nat := |
| 141 | M.rec (motive := fun _ ↦ Nat → Nat) (fun _ ↦ 0) |
| 142 | (fun m _ ih b ↦ (m.mul (b.land 1)).xor (ih (b.shiftRight 1))) bi |
| 143 | |
| 144 | /-- Fold over the rows of `B`: row `b` at index `i` must give `invRowK b M = 1 <<< i`. -/ |
| 145 | noncomputable def checkInvGo (M : List Nat) (i : Nat) (B : List Nat) : Bool := |
| 146 | B.rec (fun _ ↦ true) |
| 147 | (fun b _ ih i ↦ ((invRowK b M).beq (Nat.shiftLeft 1 i)).and (ih i.succ)) i |
| 148 | |
| 149 | /-- `true` iff `B * M = I` over `𝔽₂`, with `B` and `M` given by rows (each a `Nat` bitmask). -/ |
| 150 | noncomputable def checkInv (B M : List Nat) : Bool := checkInvGo M 0 B |
| 151 | |
| 152 | end F2Invert |
| 153 | |
| 154 | /-! ## Descent matrix -/ |
| 155 | |
| 156 | /-- The `Nat` label triples `(p, (θ % p).toNat, m)`, one for each label `(p, θ)` in `ls` paired |
| 157 | with its quadratic-residue mask `m` in `q`. -/ |
| 158 | noncomputable def toLs (ls : List (Nat × Int)) (q : List Nat) : List (Nat × Nat × Nat) := |
| 159 | List.zipWith (fun l m ↦ (l.1, (l.2.emod l.1).toNat, m)) ls q |
| 160 | |
| 161 | /-- The expected row word: bit `j` is label `ls[j]`'s descent character at point |
| 162 | `(xnp - xnm) / xden`, LSB-first (head label at bit `0`). Spec: `testBit_checkBRowWord`. -/ |
| 163 | noncomputable def checkBRowWord (a₂ a₄ : Int) (xnp xnm xden : Nat) |
| 164 | (ls : List (Nat × Nat × Nat)) : Nat := |
| 165 | ls.rec 0 (fun l _ ih ↦ (lambdaBitK a₂ a₄ l.1 l.2.2 l.2.1 xnp xnm xden).lor (ih.shiftLeft 1)) |
| 166 | |
| 167 | /-- `true` iff `b` equals `checkBRowWord`: bit `j` of `b` is label `ls[j]`'s descent character at |
| 168 | point `(xnp - xnm) / xden` for every `j < ls.length`, and every higher bit of `b` is `0`. -/ |
| 169 | noncomputable def checkBRow (a₂ a₄ : Int) (xnp xnm xden b : Nat) (ls : List (Nat × Nat × Nat)) : |
| 170 | Bool := |
| 171 | (checkBRowWord a₂ a₄ xnp xnm xden ls).beq b |
| 172 | |
| 173 | /-- `true` iff every row of `B` passes `checkBRow` against its point at the same index in `pt`. -/ |
| 174 | noncomputable def checkBGo (a₂ a₄ : Int) (ls : List (Nat × Nat × Nat)) (B : List Nat) |
| 175 | (pt : List (Rat × Rat)) : Bool := |
| 176 | B.rec (fun _ ↦ true) |
| 177 | (fun b _ ih pt ↦ pt.rec true |
| 178 | (fun p ps _ ↦ (checkBRow a₂ a₄ p.1.num.toNat (-p.1.num).toNat p.1.den b ls).and |
| 179 | (ih ps))) pt |
| 180 | |
| 181 | /-- `true` iff each triple's mask equals `qrMask p` for its prime `p`. -/ |
| 182 | noncomputable def checkMaskList (ls : List (Nat × Nat × Nat)) : Bool := |
| 183 | allList (fun l ↦ (qrMask l.1).beq l.2.2) ls |
| 184 | |
| 185 | /-- `true` iff every entry of `B` equals the descent character at its point in `pt`, with each |
| 186 | mask in `q` checked against `qrMask`. Spec: `checkB_true`. -/ |
| 187 | noncomputable def checkB (a₂ a₄ : Int) (ls : List (Nat × Int)) (q B : List Nat) |
| 188 | (pt : List (Rat × Rat)) : Bool := |
| 189 | (checkMaskList (toLs ls q)).and |
| 190 | (checkBGo a₂ a₄ (toLs ls q) B pt) |
| 191 | |
| 192 | /-! ## Point on curve -/ |
| 193 | |
| 194 | /-- `checkPoint a₂ a₄ a₆ x y` tests the cleared short-model Weierstrass identity |
| 195 | `yn²·xd³ = xn³·yd² + a₂·xn²·xd·yd² + a₄·xn·xd²·yd² + a₆·xd³·yd²` at `x = xn/xd`, `y = yn/yd`. |
| 196 | Spec: `checkPoint_iff`. -/ |
| 197 | noncomputable def checkPoint (a₂ a₄ a₆ : Int) (x y : Rat) : Bool := |
| 198 | let xn := x.num; let xd := x.den |
| 199 | let yn := y.num; let yd := y.den |
| 200 | let xd2 := xd.mul xd; let xd3 := xd2.mul xd |
| 201 | let yd2 := yd.mul yd |
| 202 | let xn2 := xn.mul xn; let xn3 := xn2.mul xn |
| 203 | let yn2 := yn.mul yn |
| 204 | (yn2.mul xd3).beq' |
| 205 | ((((xn3.mul yd2).add (((a₂.mul xn2).mul xd).mul yd2)).add |
| 206 | (((a₄.mul xn).mul xd2).mul yd2)).add ((a₆.mul xd3).mul yd2)) |
| 207 | |
| 208 | /-- `true` iff every point in `pts` lies on the short model `⟨0, a₂, 0, a₄, a₆⟩`. -/ |
| 209 | noncomputable def checkPoints (a₂ a₄ a₆ : Int) (pts : List (Rat × Rat)) : Bool := |
| 210 | allList (fun p ↦ checkPoint a₂ a₄ a₆ p.1 p.2) pts |
| 211 | |
| 212 | end ECCompute |