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 ECCompute.Soundness.Fold |
| 9 | public import Mathlib.Data.ZMod.Basic |
| 10 | public import Mathlib.Data.Matrix.Basic |
| 11 | |
| 12 | import Mathlib.Data.Nat.Bitwise |
| 13 | import Mathlib.Data.List.GetD |
| 14 | import Mathlib.Data.Matrix.Mul |
| 15 | import Mathlib.Algebra.BigOperators.Fin |
| 16 | import ECCompute.ForLean |
| 17 | |
| 18 | /-! |
| 19 | # Soundness of the kernel-reducible 𝔽₂ matrix invertibility certificate |
| 20 | |
| 21 | Correctness of the `Bool` checker `ECCompute.F2Invert.checkInv` (defined in `ECCompute.Kernel`): |
| 22 | a passing `checkInv B M`, with a claimed inverse `M` by rows, certifies that the square matrix over |
| 23 | `𝔽₂ = ZMod 2` interpreted from the rows of `B` is invertible. |
| 24 | |
| 25 | ## Main results |
| 26 | |
| 27 | * `checkInv_isUnit` : for `B`, `M` of length `n`, `checkInv B M → IsUnit (toMat B n)`. |
| 28 | -/ |
| 29 | |
| 30 | namespace ECCompute.F2Invert |
| 31 | |
| 32 | open Finset |
| 33 | |
| 34 | variable {a b : Bool} |
| 35 | |
| 36 | /-- `ZMod 2` indicator of a `Bool`: `true ↦ 1`, `false ↦ 0`. -/ |
| 37 | def bId (b : Bool) : ZMod 2 := if b then 1 else 0 |
| 38 | |
| 39 | @[simp, grind =] lemma bId_false : bId false = 0 := rfl |
| 40 | @[simp, grind =] lemma bId_true : bId true = 1 := rfl |
| 41 | @[simp] lemma bId_xor : bId (a ^^ b) = bId a + bId b := by decide +revert +kernel |
| 42 | @[simp] lemma bId_and : bId (a && b) = bId a * bId b := by decide +revert +kernel |
| 43 | |
| 44 | @[simp, grind =] theorem invRowK_nil {b : ℕ} : invRowK b [] = 0 := rfl |
| 45 | |
| 46 | @[simp, grind =] theorem invRowK_cons {b m : ℕ} {ms : List ℕ} : |
| 47 | invRowK b (m :: ms) = m * (b &&& 1) ^^^ invRowK (b >>> 1) ms := rfl |
| 48 | |
| 49 | /-- Bit `j` of one selected term `m * (b &&& 1)`, as a `ZMod 2` product: the low bit of `b` times |
| 50 | bit `j` of `m`. -/ |
| 51 | theorem bId_testBit_select {m b j : ℕ} : |
| 52 | bId ((m * (b &&& 1)).testBit j) = bId (b.testBit 0) * bId (m.testBit j) := by |
| 53 | rcases Nat.mod_two_eq_zero_or_one b with h | h <;> simp [Nat.and_one_is_mod, h] |
| 54 | |
| 55 | /-- Bit `j` of `invRowK`, over `𝔽₂`: over each row `k`, the selector bit `b.testBit k` times |
| 56 | bit `j` of row `k`. -/ |
| 57 | theorem bId_invRowK_testBit {ms : List ℕ} {b j : ℕ} : |
| 58 | bId ((invRowK b ms).testBit j) |
| 59 | = ∑ k ∈ range ms.length, bId (b.testBit k) * bId ((ms.getD k 0).testBit j) := by |
| 60 | induction ms generalizing b with |
| 61 | | nil => simp |
| 62 | | cons m ms ih => |
| 63 | simp only [invRowK_cons, Nat.testBit_xor, bId_xor, bId_testBit_select, List.length_cons, |
| 64 | sum_range_succ'] |
| 65 | grind |
| 66 | |
| 67 | variable {n i i' : ℕ} {B M : List ℕ} |
| 68 | |
| 69 | @[simp, grind =] theorem checkInvGo_cons {b : ℕ} {bs : List ℕ} : |
| 70 | checkInvGo M i (b :: bs) = ((invRowK b M).beq (1 <<< i)).and (checkInvGo M i.succ bs) := rfl |
| 71 | |
| 72 | /-- Interpret a `List Nat` of row bitmasks as an `n × n` matrix over `𝔽₂`. -/ |
| 73 | public def toMat (B : List ℕ) (n : ℕ) : Matrix (Fin n) (Fin n) (ZMod 2) := |
| 74 | Matrix.of fun i j ↦ if (B.getD i 0).testBit j then 1 else 0 |
| 75 | |
| 76 | /-- Entry `(i, j)` of `toMat B n`, for a row index in range: bit `j` of row `i` of `B`. -/ |
| 77 | public theorem toMat_apply {B : List ℕ} {n : ℕ} {i j : Fin n} (h : i.val < B.length) : |
| 78 | toMat B n i j = if B[i].testBit j then 1 else 0 := by |
| 79 | rw [toMat, Matrix.of_apply, List.getD_eq_getElem _ _ h, Fin.getElem_fin] |
| 80 | |
| 81 | /-- Entry `(i, j)` of `toMat B n` as the `𝔽₂` indicator of bit `j` of row `i`. -/ |
| 82 | theorem toMat_eq_bId {B : List ℕ} {n : ℕ} {i j : Fin n} : |
| 83 | toMat B n i j = bId ((B.getD i 0).testBit j) := rfl |
| 84 | |
| 85 | /-- From a passing `checkInvGo` (started at row index `i`), the row `invRowK B[i'] M` equals the |
| 86 | unit vector `1 <<< (i + i')` for every row of `B`. -/ |
| 87 | theorem checkInvGo_true (hc : checkInvGo M i B) (hi' : i' < B.length) : |
| 88 | invRowK B[i'] M = 1 <<< (i + i') := by |
| 89 | induction B generalizing i i' with |
| 90 | | nil => simp at hi' |
| 91 | | cons b bs ih => cases i' <;> grind [ih (i := i + 1)] |
| 92 | |
| 93 | /-- If the kernel-reducible checker `checkInv B M` returns `true` (and `B`, `M` have length `n`), |
| 94 | then the matrix `toMat B n` interpreted over `𝔽₂` is invertible (a unit). -/ |
| 95 | public theorem checkInv_isUnit (hBlen : B.length = n) (hMlen : M.length = n) (h : checkInv B M) : |
| 96 | IsUnit (toMat B n) := by |
| 97 | have key : toMat B n * toMat M n = 1 := by |
| 98 | ext i k |
| 99 | have hi : i.val < B.length := by grind |
| 100 | have hrow : invRowK B[i.val] M = 1 <<< i.val := by simpa using checkInvGo_true h hi |
| 101 | subst hMlen |
| 102 | rw [Matrix.mul_apply, Matrix.one_apply] |
| 103 | simp only [toMat_eq_bId] |
| 104 | rw [Fin.sum_univ_eq_sum_range |
| 105 | (fun x ↦ bId ((B.getD i 0).testBit x) * bId ((M.getD x 0).testBit k)), ← bId_invRowK_testBit, |
| 106 | List.getD_eq_getElem _ _ hi, hrow, Nat.one_shiftLeft] |
| 107 | grind |
| 108 | exact .of_mul_eq_one (toMat M n) key |
| 109 | |
| 110 | end ECCompute.F2Invert |