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.Data.Nat.Bitwise |
| 9 | |
| 10 | /-! |
| 11 | ## Evaluator-side helpers |
| 12 | |
| 13 | These functions reproduce the descent character and the `𝔽₂` matrix inverse using plain |
| 14 | `Int`/`Nat` arithmetic, written for the compiler/interpreter. The `certify_curve` elaborator |
| 15 | calls them to produce a certificate's character matrix `B` and its inverse `M`. |
| 16 | -/ |
| 17 | |
| 18 | namespace ECCompute.CertifyEval |
| 19 | |
| 20 | /-- The quadratic-residue bitmask mod an odd prime `p`: bit `a` set iff `a` is a nonzero square mod |
| 21 | `p`, computed as the OR of `1 <<< (j² % p)` for `j = 1 .. (p-1)/2`. Matches `ECCompute.qrMask`. -/ |
| 22 | public def qrMaskEval (p : Nat) : Nat := |
| 23 | (List.range ((p - 1) / 2)).foldl (fun acc k ↦ acc ||| (1 <<< ((k + 1) * (k + 1) % p))) 0 |
| 24 | |
| 25 | /-- Evaluator-side value of the descent character `λ_{p,θ}` on a point whose `x`-coordinate is |
| 26 | `xnum / xden`, mirroring `ECCompute.lambdaBitK` (`true` = bit `1`). `a₂ a₄` are the short-model |
| 27 | coefficients and `qrMask` is `qrMaskEval p`. The character is nontrivial exactly when the relevant |
| 28 | value is a non-residue (or zero) mod `p`, i.e. its bit in `qrMask` is clear. -/ |
| 29 | public def lambdaEval (a₂ a₄ : Int) (p : Nat) (qrMask : Nat) (θ xnum : Int) (xden : Nat) : Bool := |
| 30 | if (xden : Int) % (p : Int) == 0 then false |
| 31 | else |
| 32 | let α := (xnum - θ * (xden : Int)) % (p : Int) |
| 33 | -- tangent case `x.num ≡ θ·x.den`: evaluate at the derivative `3θ² + 2a₂θ + a₄` |
| 34 | let a := if α == 0 then 3 * θ ^ 2 + 2 * a₂ * θ + a₄ else α |
| 35 | let am := (((a % (p : Int)) + p) % (p : Int)).toNat |
| 36 | ((qrMask >>> am) &&& 1) == 0 |
| 37 | |
| 38 | /-- Pack booleans `p 0, …, p (n-1)` into a `Nat` bitmask: bit `j` set iff `p j`. -/ |
| 39 | public def bitmaskOf (n : Nat) (p : Nat → Bool) : Nat := |
| 40 | (List.range n).foldl (fun acc j ↦ if p j then acc ||| (1 <<< j) else acc) 0 |
| 41 | |
| 42 | /-- The descent-character matrix `B` as `Nat` row bitmasks: row `i` has bit `j` set iff the |
| 43 | character of label `ls[j]` on the point with `x`-coordinate `xs[i] = (num, den)` is nontrivial. The |
| 44 | quadratic-residue bitmask of each label's prime is computed once. -/ |
| 45 | public def computeB (a₂ a₄ : Int) (xs : List (Int × Nat)) (ls : List (Nat × Int)) : List Nat := |
| 46 | let qs := ls.map fun l ↦ qrMaskEval l.1 |
| 47 | xs.map fun x ↦ |
| 48 | bitmaskOf ls.length (fun j ↦ let l := ls[j]!; lambdaEval a₂ a₄ l.1 qs[j]! l.2 x.1 x.2) |
| 49 | |
| 50 | /-- Invert an `n × n` matrix over `𝔽₂` given as `Nat` row bitmasks, returning the inverse as `Nat` |
| 51 | row bitmasks (so it feeds `checkInv` as the row-major `M`). Returns `none` if the matrix is |
| 52 | singular. -/ |
| 53 | public def invF2 (B : Array Nat) (n : Nat) : Option (List Nat) := Id.run do |
| 54 | let mut a := B |
| 55 | let mut inv : Array Nat := (Array.range n).map (fun i ↦ (1 <<< i : Nat)) |
| 56 | for col in [0:n] do |
| 57 | let mut piv : Option Nat := none |
| 58 | for r in [col:n] do |
| 59 | if a[r]!.testBit col then |
| 60 | piv := some r |
| 61 | break |
| 62 | match piv with |
| 63 | | none => return none |
| 64 | | some r => |
| 65 | if r != col then |
| 66 | a := a.swapIfInBounds col r |
| 67 | inv := inv.swapIfInBounds col r |
| 68 | for r2 in [0:n] do |
| 69 | if r2 != col && a[r2]!.testBit col then |
| 70 | a := a.set! r2 (a[r2]! ^^^ a[col]!) |
| 71 | inv := inv.set! r2 (inv[r2]! ^^^ inv[col]!) |
| 72 | return some inv.toList |
| 73 | |
| 74 | end ECCompute.CertifyEval |