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.Theory.Descent.Character |
| 9 | public import ECCompute.Soundness.RootMod |
| 10 | |
| 11 | import Mathlib.Data.Nat.Bitwise |
| 12 | import ECCompute.ForLean |
| 13 | |
| 14 | /-! |
| 15 | # Soundness of the kernel-reducible descent character |
| 16 | |
| 17 | The kernel `Nat` builders that evaluate the descent character `λ_{p,θ}` |
| 18 | (`qrMask`, `lambdaBitK`, …) live in `ECCompute.Kernel`; this file |
| 19 | proves them correct. |
| 20 | |
| 21 | ## Main declarations |
| 22 | |
| 23 | * `ECCompute.qrLookupBool_iff`: the mask bit test decides whether `a` is a nonzero square mod `p`. |
| 24 | * `ECCompute.lambdaBitK_eq`: the fully-`Nat` kernel mirror, read into `ZMod 2`, |
| 25 | agrees with the abstract character `lambda`. |
| 26 | -/ |
| 27 | |
| 28 | namespace ECCompute |
| 29 | |
| 30 | variable {p a : ℕ} {a₂ a₄ a₆ : ℤ} |
| 31 | |
| 32 | /-! ### Quadratic-residue bitmask: kernel evaluation of the Legendre character |
| 33 | |
| 34 | `qrMask` is the reference builder the certificate's supplied mask is checked against; |
| 35 | `qrLookupBool_iff` shows the bit test decides whether `a` is a nonzero square mod `p`. |
| 36 | -/ |
| 37 | |
| 38 | /-- `true` iff bit `a` of the quadratic-residue mask `qm` is |
| 39 | set, i.e. (for `qm = qrMask p`, `a < p`, `p` odd prime) iff `a` is a nonzero square mod `p`. -/ |
| 40 | def qrLookupBool (qm a : ℕ) : Bool := ((qm.shiftRight a).land 1).beq 1 |
| 41 | |
| 42 | /-- The kernel bit test `(m >>> a) &&& 1 = 1` is `m.testBit a`. -/ |
| 43 | theorem shiftRight_land_one_eq_one_iff {m : ℕ} : (m >>> a) &&& 1 = 1 ↔ m.testBit a := by |
| 44 | grind [Nat.shiftRight_eq_div_pow] |
| 45 | |
| 46 | /-- One-step unfolding of the quadratic-residue mask fold. -/ |
| 47 | @[simp, grind =] theorem qrMaskGo_succ {k : ℕ} : |
| 48 | qrMaskGo p (k + 1) = qrMaskGo p k ||| 1 <<< ((k + 1) * (k + 1) % p) := rfl |
| 49 | |
| 50 | /-- Bit `a` of the fold is set iff some `1 ≤ j ≤ fuel` has `j² % p = a`. -/ |
| 51 | @[grind =] theorem testBit_qrMaskGo {f : ℕ} : |
| 52 | (qrMaskGo p f).testBit a ↔ ∃ j, 1 ≤ j ∧ j ≤ f ∧ j * j % p = a := by |
| 53 | induction f with |
| 54 | | zero => simp [qrMaskGo] |
| 55 | | succ k ih => |
| 56 | rw [qrMaskGo_succ, Nat.testBit_lor, Nat.shiftLeft_eq, Nat.one_mul, Nat.testBit_two_pow, |
| 57 | Bool.or_eq_true, ih, decide_eq_true_eq] |
| 58 | constructor |
| 59 | · rintro (⟨j, hj1, hjk, hja⟩ | hb) |
| 60 | · exact ⟨j, by lia⟩ |
| 61 | · exact ⟨k + 1, by lia⟩ |
| 62 | · rintro ⟨j, hj1, hjk, hja⟩ |
| 63 | obtain h | rfl : j < k + 1 ∨ j = k + 1 := by lia |
| 64 | · exact Or.inl ⟨j, by lia⟩ |
| 65 | · grind |
| 66 | |
| 67 | /-- For `a < p`, `p` an odd prime: `a` is a residue witnessed in the lower half `[1, (p-1)/2]` |
| 68 | iff it is a nonzero square in `ZMod p`. -/ |
| 69 | theorem exists_sq_iff (hp : p.Prime) (hp2 : p ≠ 2) (ha : a < p) : |
| 70 | (∃ j, 1 ≤ j ∧ j ≤ (p - 1) / 2 ∧ j * j % p = a) ↔ a ≠ 0 ∧ IsSquare (a : ZMod p) := by |
| 71 | have hodd : p % 2 = 1 := hp.eq_two_or_odd.resolve_left hp2 |
| 72 | constructor |
| 73 | · rintro ⟨j, hj1, hjk, hja⟩ |
| 74 | have hjp : j < p := by lia |
| 75 | have hcast : (a : ZMod p) = j * j := by |
| 76 | rw [← Nat.cast_mul, ← hja, ZMod.natCast_mod, Nat.cast_mul] |
| 77 | refine ⟨?_, ⟨j, hcast⟩⟩ |
| 78 | rintro rfl |
| 79 | exact (Nat.le_of_dvd (by lia) (by grind [hp.dvd_mul, Nat.dvd_of_mod_eq_zero hja])).not_gt hjp |
| 80 | · rintro ⟨ha0, ⟨x, hx⟩⟩ |
| 81 | have hxne : x ≠ 0 := by |
| 82 | rintro rfl |
| 83 | exact ha0 (Nat.eq_zero_of_dvd_of_lt (by grind [ZMod.natCast_eq_zero_iff]) ha) |
| 84 | have : NeZero p := ⟨by lia⟩ |
| 85 | wlog hx' : x.val ≤ (p - 1) / 2 generalizing x |
| 86 | · exact this (-x) (by grind) (by grind) (by grind [ZMod.neg_val]) |
| 87 | have haeq : x.val * x.val % p = a := by |
| 88 | have hc : (a : ZMod p) = (x.val * x.val : ℕ) := by rw [Nat.cast_mul, x.natCast_zmod_val, hx] |
| 89 | grind [Nat.mod_eq_of_lt ha, ZMod.natCast_eq_natCast_iff'] |
| 90 | exact ⟨x.val, by grind [ZMod.val_eq_zero], hx', haeq⟩ |
| 91 | |
| 92 | /-- Bit `a` of `qrMask p` is set iff `a` is a nonzero square mod the odd prime `p` (for `a < p`). -/ |
| 93 | theorem qrMask_testBit (hp2 : p ≠ 2) (hp : p.Prime) (ha : a < p) : |
| 94 | qrMask p >>> a &&& 1 = 1 ↔ a ≠ 0 ∧ IsSquare (a : ZMod p) := by |
| 95 | rw [shiftRight_land_one_eq_one_iff, qrMask, testBit_qrMaskGo] |
| 96 | exact exists_sq_iff hp hp2 ha |
| 97 | |
| 98 | /-- The mask bit test decides whether `a` is a nonzero square mod `p` (for `a < p`, `p` an odd |
| 99 | prime). -/ |
| 100 | theorem qrLookupBool_iff (hp2 : p ≠ 2) (hp : p.Prime) (ha : a < p) : |
| 101 | qrLookupBool (qrMask p) a ↔ (a ≠ 0 ∧ IsSquare (a : ZMod p)) := by |
| 102 | grind [qrLookupBool, qrMask_testBit] |
| 103 | |
| 104 | variable {θ : ZMod p} |
| 105 | |
| 106 | /-- For `p` an odd prime and `a ≠ 0`, reading the residue mask of `p` into `ZMod 2` gives the |
| 107 | abstract Legendre character `psi`. -/ |
| 108 | theorem mask_eq_psi (hp : p.Prime) (hp2 : p ≠ 2) {a : ZMod p} (ha : a ≠ 0) : |
| 109 | (if qrLookupBool (qrMask p) a.val then 0 else 1) = psi p a := by |
| 110 | have : NeZero p := ⟨hp.ne_zero⟩ |
| 111 | have ha0 : a.val ≠ 0 := by rwa [ne_eq, ZMod.val_eq_zero] |
| 112 | simp [qrLookupBool_iff hp2 hp (ZMod.val_lt a), ha0, psi] |
| 113 | |
| 114 | /-! ### Fully `Nat` mirror |
| 115 | |
| 116 | `lambdaBitK_eq` proves the kernel-reducible `Nat` builder `lambdaBitK`, |
| 117 | read into `ZMod 2`, agrees with the abstract character `lambda`. -/ |
| 118 | |
| 119 | section |
| 120 | variable {tval xp xm xden : ℕ} |
| 121 | |
| 122 | /-- `alphaResK` casts back to `x.num - θ·x.den` in `ZMod p`. -/ |
| 123 | theorem alphaResK_cast (hp : p ≠ 0) : |
| 124 | (alphaResK p tval xp xm xden : ZMod p) = xp - (xm + tval * xden) := by |
| 125 | simp only [alphaResK, Nat.mod_eq_mod, Nat.add_eq, Nat.sub_eq, Nat.mul_eq, ZMod.natCast_mod, |
| 126 | Nat.cast_add, Nat.cast_sub (Nat.mod_lt _ hp.bot_lt).le, ZMod.natCast_self, Nat.cast_mul] |
| 127 | ring |
| 128 | |
| 129 | /-- `alphaResK` is the `ZMod p`-value of `x.num - θ·x.den`. -/ |
| 130 | theorem alphaResK_eq_val (hp : p ≠ 0) {x : ℚ} |
| 131 | (htval : tval = θ) (hxnum : x.num = xp - xm) (hxden : xden = x.den) : |
| 132 | alphaResK p tval xp xm xden = (x.num - θ * x.den).val := by |
| 133 | have hcast : (alphaResK p tval xp xm xden : ZMod p) = x.num - θ * x.den := by |
| 134 | rw [alphaResK_cast hp, ← htval, ← hxden] |
| 135 | have : (x.num : ZMod p) = xp - xm := by rw [hxnum]; push_cast; ring |
| 136 | grind |
| 137 | have hlt : alphaResK p tval xp xm xden < p := Nat.mod_lt _ hp.bot_lt |
| 138 | rw [← hcast, ZMod.val_cast_of_lt hlt] |
| 139 | |
| 140 | /-- `fderivResK` casts back to `f'(θ) = 3θ² + 2a₂θ + a₄` in `ZMod p`. -/ |
| 141 | theorem fderivResK_cast (hp : p ≠ 0) (htval : tval = θ) : |
| 142 | (fderivResK a₂ a₄ p tval : ZMod p) = fderiv (a₂ : ZMod p) a₄ θ := by |
| 143 | simp only [fderivResK] |
| 144 | rw [polyModL_cast hp] |
| 145 | simp only [polyEval, Int.add_def, Int.mul_def] |
| 146 | subst htval |
| 147 | rw [fderiv] |
| 148 | push_cast |
| 149 | ring |
| 150 | |
| 151 | /-- `fderivResK` is the `ZMod p`-value of `f'(θ)`. -/ |
| 152 | theorem fderivResK_eq_val (hp : p ≠ 0) (htval : tval = θ) : |
| 153 | fderivResK a₂ a₄ p tval = (fderiv (a₂ : ZMod p) a₄ θ).val := by |
| 154 | have hlt : fderivResK a₂ a₄ p tval < p := by simp only [fderivResK]; exact polyModL_lt hp |
| 155 | rw [← fderivResK_cast hp htval, ZMod.val_cast_of_lt hlt] |
| 156 | |
| 157 | /-- `lambdaBitK` is a bit: `0` or `1`. -/ |
| 158 | public theorem lambdaBitK_eq_zero_or_one {qm : ℕ} : |
| 159 | lambdaBitK a₂ a₄ p qm tval xp xm xden = 0 ∨ lambdaBitK a₂ a₄ p qm tval xp xm xden = 1 := by |
| 160 | simp only [lambdaBitK, Bool.rec_eq, Nat.shiftRight_eq', Nat.land_eq, Nat.xor_eq, |
| 161 | Nat.mod_eq_mod, Nat.beq_eq_beq, Nat.and_one_is_mod] |
| 162 | grind [Nat.mod_two_eq_zero_or_one] |
| 163 | |
| 164 | /-- `lambdaBitK` with the mask `qrMask p`, read into `ZMod 2`, equals the abstract |
| 165 | character `lambda` at the affine point, provided its `Nat` inputs encode the arguments: `θ = tval`, |
| 166 | and `x` has numerator `xp - xm` and denominator `xden`. -/ |
| 167 | public theorem lambdaBitK_eq (hyp : DescentHyp a₂ a₄ a₆ p θ) {x y : ℚ} |
| 168 | (h : (curveQ a₂ a₄ a₆).toAffine.Nonsingular x y) |
| 169 | (htval : tval = θ) (hxnum : x.num = xp - xm) (hxden : xden = x.den) : |
| 170 | (lambdaBitK a₂ a₄ p (qrMask p) tval xp xm xden : ZMod 2) = lambda θ (.some x y h) := by |
| 171 | have hp : p ≠ 0 := hyp.prime.ne_zero |
| 172 | have hp2 : p ≠ 2 := fun hp ↦ hyp.ne_six (hp ▸ ⟨3, rfl⟩) |
| 173 | have halpha := alphaResK_eq_val hp htval hxnum hxden |
| 174 | have hden : xden % p = 0 ↔ (x.den : ZMod p) = 0 := by |
| 175 | rw [hxden, ← Nat.dvd_iff_mod_eq_zero, ZMod.natCast_eq_zero_iff] |
| 176 | have key (a : ℕ) : ((qrMask p >>> a % 2 ^^^ 1 : ℕ) : ZMod 2) = |
| 177 | if qrLookupBool (qrMask p) a then 0 else 1 := by |
| 178 | have hbit : (qrMask p >>> a % 2 ^^^ 1 : ℕ) = if qrLookupBool (qrMask p) a then 0 else 1 := by |
| 179 | grind [qrLookupBool, Nat.mod_two_eq_zero_or_one] |
| 180 | rw [hbit] |
| 181 | simp only [apply_ite (Nat.cast : ℕ → ZMod 2), Nat.cast_zero, Nat.cast_one] |
| 182 | simp only [lambdaBitK, Bool.rec_eq, Nat.shiftRight_eq', Nat.land_eq, Nat.xor_eq, |
| 183 | Nat.mod_eq_mod, Nat.beq_eq_beq, Nat.and_one_is_mod, apply_ite (Nat.cast : ℕ → ZMod 2), |
| 184 | Nat.cast_zero, key] |
| 185 | rw [lambda] |
| 186 | grind [mask_eq_psi, hyp.prime, ZMod.val_eq_zero, fderivResK_eq_val, fderiv_ne_zero] |
| 187 | |
| 188 | end |
| 189 | |
| 190 | end ECCompute |