The Birch and Swinnerton-Dyer conjecture
Certify ranks of elliptic curves exactly, check what 2026 claimed about BSD, and push the range where the full formula is proved past conductor 5000

| 1 | #!/usr/bin/env python3 |
| 2 | """Verify the rank certificate a route handed in: exact arithmetic, standard library only. |
| 3 | |
| 4 | Usage: |
| 5 | verify.py FOLDER [--metrics PATH] |
| 6 | |
| 7 | FOLDER/CERTIFICATE.json: |
| 8 | {"target": "E-1" | "C-1" | "C-2", |
| 9 | "curves": [{"label": "389a1", "ainvs": [a1, a2, a3, a4, a6], |
| 10 | "points": [["x", "y"], ...], "torsion": [["x", "y"], ...], "chars": [[p, theta], ...]}, ...]} |
| 11 | with x, y rationals written as strings ("3/4") and every other number an integer. |
| 12 | |
| 13 | For each curve the check verifies, with no floating point anywhere: |
| 14 | * the curve is nonsingular and every point lies on it; |
| 15 | * every torsion point has order at most 12 (Mazur), and their number is dim E(Q)[2], which the check computes itself |
| 16 | from the rational roots of 4x^3 + b2 x^2 + 2 b4 x + b6; |
| 17 | * every (p, theta) has p an odd prime not dividing the discriminant and theta a root of that cubic mod p; |
| 18 | * the matrix of the characters chi_(p,theta)(P) = [x(P) - theta is not a square mod p] on the points and the |
| 19 | torsion points has F_2-rank #points + #torsion. |
| 20 | Each chi is the reduction E(Q) -> E(F_p) followed by the 2-descent map at the root theta, so it is a homomorphism to F_2 |
| 21 | that kills 2E(Q) (Cremona, "Algorithms for modular elliptic curves", 3.6; the ICARM leaderboard checks ranks this way). |
| 22 | E(Q)/2E(Q) has F_2-dimension rank + dim E(Q)[2], so independent images prove rank E(Q) >= #points. |
| 23 | |
| 24 | Targets: |
| 25 | E-1 any curves: the example's target, and a way to certify any curve; |
| 26 | C-1 the curves of rank >= 2 and conductor below 10,000 in Cremona's tables (checks/c1-curves.tsv.gz: label, |
| 27 | a-invariants, rank); a curve counts when its label and a-invariants are listed and it has as many points as |
| 28 | its listed rank; |
| 29 | C-2 the three curves of rank >= 32 on the ICARM leaderboard (checks/records.json), as "icarm-<id>", 32 points each. |
| 30 | A certificate for another piece's target exits 0 saying no verifier exists for it: a maintainer reads it. |
| 31 | |
| 32 | Exit 0 with the last line "E-1: …", "C-1: <k> of 2389 curves, rank >= r certified exactly" or |
| 33 | "C-2: <k> of 3 records, rank >= 32 certified exactly"; with no CERTIFICATE.json, "Nothing to check yet". |
| 34 | Exit 1 with the first problem otherwise. With --metrics PATH, writes {"score": k, "subtasks": {"<target>": k}}. |
| 35 | """ |
| 36 | |
| 37 | from __future__ import annotations |
| 38 | |
| 39 | import gzip |
| 40 | import json |
| 41 | import math |
| 42 | import sys |
| 43 | from fractions import Fraction |
| 44 | from pathlib import Path |
| 45 | |
| 46 | HERE = Path(__file__).resolve().parent |
| 47 | MAX_BYTES = 8_000_000 # the whole of C-1 is about 340 KB |
| 48 | REVIEWED = {"C-3", "C-4", "V-1", "V-2", "V-3", "V-4", "R-1", "L-1"} |
| 49 | |
| 50 | |
| 51 | class Wrong(Exception): |
| 52 | """A certificate the check refuses; its text is what the author reads.""" |
| 53 | |
| 54 | |
| 55 | class NoVerifier(Exception): |
| 56 | """A target this check does not verify; a maintainer reads the certificate instead.""" |
| 57 | |
| 58 | |
| 59 | def shown(text, limit=60): |
| 60 | clean = "".join(c if c.isprintable() else "?" for c in str(text)) |
| 61 | return clean if len(clean) <= limit else clean[:limit] + "..." |
| 62 | |
| 63 | |
| 64 | def is_prime(n: int) -> bool: |
| 65 | if n < 2: |
| 66 | return False |
| 67 | for q in (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37): |
| 68 | if n % q == 0: |
| 69 | return n == q |
| 70 | if n >= 3_317_044_064_679_887_385_961_981: # the bases below are a proof only up to here |
| 71 | raise Wrong(f"prime {n} is too large for this check") |
| 72 | d, s = n - 1, 0 |
| 73 | while d % 2 == 0: |
| 74 | d, s = d // 2, s + 1 |
| 75 | for a in (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37): |
| 76 | x = pow(a, d, n) |
| 77 | if x in (1, n - 1): |
| 78 | continue |
| 79 | for _ in range(s - 1): |
| 80 | x = x * x % n |
| 81 | if x == n - 1: |
| 82 | break |
| 83 | else: |
| 84 | return False |
| 85 | return True |
| 86 | |
| 87 | |
| 88 | class Curve: |
| 89 | def __init__(self, a): |
| 90 | self.a1, self.a2, self.a3, self.a4, self.a6 = a |
| 91 | a1, a2, a3, a4, a6 = a |
| 92 | self.b2, self.b4, self.b6 = a1 * a1 + 4 * a2, 2 * a4 + a1 * a3, a3 * a3 + 4 * a6 |
| 93 | b8 = a1 * a1 * a6 + 4 * a2 * a6 - a1 * a3 * a4 + a2 * a3 * a3 - a4 * a4 |
| 94 | self.disc = -self.b2 ** 2 * b8 - 8 * self.b4 ** 3 - 27 * self.b6 ** 2 + 9 * self.b2 * self.b4 * self.b6 |
| 95 | if self.disc == 0: |
| 96 | raise Wrong("the curve is singular") |
| 97 | |
| 98 | def on(self, P): |
| 99 | x, y = P |
| 100 | return y * y + self.a1 * x * y + self.a3 * y == x ** 3 + self.a2 * x * x + self.a4 * x + self.a6 |
| 101 | |
| 102 | def add(self, P, Q): |
| 103 | if P is None: |
| 104 | return Q |
| 105 | if Q is None: |
| 106 | return P |
| 107 | (x1, y1), (x2, y2) = P, Q |
| 108 | if x1 == x2: |
| 109 | if y1 + y2 + self.a1 * x2 + self.a3 == 0: |
| 110 | return None |
| 111 | lam = (3 * x1 * x1 + 2 * self.a2 * x1 + self.a4 - self.a1 * y1) / (2 * y1 + self.a1 * x1 + self.a3) |
| 112 | else: |
| 113 | lam = (y2 - y1) / (x2 - x1) |
| 114 | nu = y1 - lam * x1 |
| 115 | x3 = lam * lam + self.a1 * lam - self.a2 - x1 - x2 |
| 116 | return (x3, -(lam + self.a1) * x3 - nu - self.a3) |
| 117 | |
| 118 | def cubic(self, x, p): |
| 119 | return (4 * x ** 3 + self.b2 * x * x + 2 * self.b4 * x + self.b6) % p |
| 120 | |
| 121 | def two_torsion_dim(self): |
| 122 | """dim E(Q)[2]: the rational roots of 4x^3 + b2 x^2 + 2 b4 x + b6 are x = u/4 with u an integer root of the |
| 123 | monic g(u) = u^3 + b2 u^2 + 8 b4 u + 16 b6. g is monotone between its critical points, so each sign change |
| 124 | on such a piece is bisected down to the two integers around it.""" |
| 125 | c2, c1, c0 = self.b2, 8 * self.b4, 16 * self.b6 |
| 126 | g = lambda u: ((u + c2) * u + c1) * u + c0 |
| 127 | bound = 1 + max(abs(c2), abs(c1), abs(c0)) # Cauchy: every real root lies inside |
| 128 | cuts = {-bound, bound} |
| 129 | d = c2 * c2 - 3 * c1 |
| 130 | if d >= 0: # the critical points (-c2 -+ sqrt(d)) / 3, rounded both ways |
| 131 | r = math.isqrt(d) |
| 132 | for centre in (-c2 - r, -c2 + r): |
| 133 | for s in range(centre - 2, centre + 3): |
| 134 | cuts.update((s // 3, s // 3 + 1)) |
| 135 | cuts = sorted(min(max(c, -bound), bound) for c in cuts) |
| 136 | roots = {u for u in cuts if g(u) == 0} |
| 137 | for lo, hi in zip(cuts, cuts[1:]): |
| 138 | if (g(lo) < 0) != (g(hi) < 0): |
| 139 | while hi - lo > 1: |
| 140 | m = (lo + hi) // 2 |
| 141 | if (g(m) < 0) == (g(lo) < 0): |
| 142 | lo = m |
| 143 | else: |
| 144 | hi = m |
| 145 | roots.update(u for u in (lo, hi) if g(u) == 0) |
| 146 | return {0: 0, 1: 1, 3: 2}[len(roots)] |
| 147 | |
| 148 | def finite_order(self, P): |
| 149 | Q = P |
| 150 | for _ in range(12): |
| 151 | if Q is None: |
| 152 | return True |
| 153 | Q = self.add(Q, P) |
| 154 | return Q is None |
| 155 | |
| 156 | def chi(self, P, p, theta): |
| 157 | x = P[0] |
| 158 | if x.denominator % p == 0: |
| 159 | return 0 # P reduces to the point at infinity |
| 160 | v = (x.numerator - theta * x.denominator) % p # the denominator is a square, so it does not change the class |
| 161 | if v == 0: # P reduces to the 2-torsion point (theta, *): the map takes the derivative there |
| 162 | v = (12 * theta * theta + 2 * self.b2 * theta + 2 * self.b4) % p |
| 163 | if v == 0: |
| 164 | raise Wrong(f"p = {p} is a bad prime") |
| 165 | return 0 if pow(v, (p - 1) // 2, p) == 1 else 1 |
| 166 | |
| 167 | |
| 168 | def f2_rank(rows): |
| 169 | basis = {} |
| 170 | for row in rows: |
| 171 | while row: |
| 172 | h = row.bit_length() - 1 |
| 173 | if h not in basis: |
| 174 | basis[h] = row |
| 175 | break |
| 176 | row ^= basis[h] |
| 177 | return len(basis) |
| 178 | |
| 179 | |
| 180 | def rational(s, where): |
| 181 | if not isinstance(s, str): |
| 182 | raise Wrong(f"{where}: write coordinates as strings, such as \"3/4\"") |
| 183 | try: |
| 184 | return Fraction(s) |
| 185 | except (ValueError, ZeroDivisionError): |
| 186 | raise Wrong(f"{where}: {shown(s)} is not a rational number") from None |
| 187 | |
| 188 | |
| 189 | def ainvs_of(c, label): |
| 190 | a = c.get("ainvs") |
| 191 | if not (isinstance(a, list) and len(a) == 5 and all(type(v) is int for v in a)): |
| 192 | raise Wrong(f"{label}: ainvs must be five integers") |
| 193 | return a |
| 194 | |
| 195 | |
| 196 | def certified(c) -> int: |
| 197 | """The rank lower bound one curve's entry proves: its number of points, once every condition holds.""" |
| 198 | label = shown(c.get("label", "?"), 40) |
| 199 | E = Curve(ainvs_of(c, label)) |
| 200 | for key in ("points", "torsion", "chars"): |
| 201 | if not isinstance(c.get(key, []), list) or not all(isinstance(P, list) and len(P) == 2 for P in c.get(key, [])): |
| 202 | raise Wrong(f"{label}: {key} must be a list of pairs") |
| 203 | pts = [tuple(rational(v, label) for v in P) for P in c.get("points", [])] |
| 204 | tors = [tuple(rational(v, label) for v in P) for P in c.get("torsion", [])] |
| 205 | for P in pts + tors: |
| 206 | if not E.on(P): |
| 207 | raise Wrong(f"{label}: ({', '.join(shown(v, 30) for v in P)}) is not on the curve") |
| 208 | for T in tors: |
| 209 | if not E.finite_order(T): |
| 210 | raise Wrong(f"{label}: the torsion point ({shown(T[0], 30)}, {shown(T[1], 30)}) has no order up to 12") |
| 211 | t2 = E.two_torsion_dim() |
| 212 | if len(tors) != t2: |
| 213 | raise Wrong(f"{label}: give {t2} torsion point(s) whose images span E(Q)_tors/2, found {len(tors)}") |
| 214 | rows = [0] * (len(pts) + len(tors)) |
| 215 | for j, ch in enumerate(c.get("chars", [])): |
| 216 | if not all(type(v) is int for v in ch): |
| 217 | raise Wrong(f"{label}: a character is [p, theta], two integers") |
| 218 | p, theta = ch |
| 219 | if not (p > 2 and is_prime(p)): |
| 220 | raise Wrong(f"{label}: the character {ch} needs an odd prime p") |
| 221 | if E.disc % p == 0: |
| 222 | raise Wrong(f"{label}: p = {p} divides the discriminant") |
| 223 | if E.cubic(theta, p) != 0: |
| 224 | raise Wrong(f"{label}: {theta} is not a root of 4x^3 + b2 x^2 + 2 b4 x + b6 mod {p}") |
| 225 | for i, P in enumerate(pts + tors): |
| 226 | rows[i] |= E.chi(P, p, theta) << j |
| 227 | got = f2_rank(rows) |
| 228 | if got != len(rows): |
| 229 | raise Wrong(f"{label}: the characters' matrix has F_2-rank {got}, not {len(rows)}: the points are not shown independent") |
| 230 | return len(pts) |
| 231 | |
| 232 | |
| 233 | def listed(target): |
| 234 | """The curves a target counts: label -> (a-invariants, the rank a certificate must reach).""" |
| 235 | if target == "C-1": |
| 236 | with gzip.open(HERE / "c1-curves.tsv.gz", "rt", encoding="utf-8") as f: |
| 237 | rows = [line.split("\t") for line in f if line.strip()] |
| 238 | return {r[0]: ([int(v) for v in r[1].split(",")], int(r[2])) for r in rows} |
| 239 | records = json.loads((HERE / "records.json").read_text(encoding="utf-8"))["records"] |
| 240 | return {f"icarm-{r['id']}": ([int(v) for v in r["ainvs"]], r["rank"]) for r in records} |
| 241 | |
| 242 | |
| 243 | def judge(cert): |
| 244 | target = cert.get("target") |
| 245 | if target in REVIEWED: |
| 246 | raise NoVerifier(target) |
| 247 | if target not in ("E-1", "C-1", "C-2"): |
| 248 | raise Wrong(f"unknown target {shown(target)}: E-1, C-1 and C-2 are checked here (docs/submission.md)") |
| 249 | curves = cert.get("curves") |
| 250 | if not isinstance(curves, list) or not curves: |
| 251 | raise Wrong("curves must be a list of at least one curve") |
| 252 | labels = [c.get("label") if isinstance(c, dict) else None for c in curves] |
| 253 | if any(not isinstance(l, str) for l in labels): |
| 254 | raise Wrong("every curve needs a label") |
| 255 | seen = set() |
| 256 | for l in labels: |
| 257 | if l in seen: |
| 258 | raise Wrong(f"{shown(l, 40)} is listed twice") |
| 259 | seen.add(l) |
| 260 | if target == "E-1": |
| 261 | ranks = [certified(c) for c in curves] |
| 262 | hist = ", ".join(f"{ranks.count(r)} with r = {r}" for r in sorted(set(ranks))) |
| 263 | return len(ranks), f"E-1: {len(ranks)} curve{'s' if len(ranks) != 1 else ''}, rank >= r certified exactly for {hist}" |
| 264 | want, k = listed(target), 0 |
| 265 | for c in curves: |
| 266 | label = c["label"] |
| 267 | if label not in want: |
| 268 | raise Wrong(f"{shown(label, 40)} is not one of {target}'s curves (checks/{'c1-curves.tsv.gz' if target == 'C-1' else 'records.json'})") |
| 269 | a, rank = want[label] |
| 270 | if ainvs_of(c, shown(label, 40)) != a: |
| 271 | raise Wrong(f"{shown(label, 40)}: its a-invariants are not the listed ones") |
| 272 | if certified(c) < rank: |
| 273 | raise Wrong(f"{shown(label, 40)}: {target} needs rank >= {rank}, that is {rank} points") |
| 274 | k += 1 |
| 275 | if target == "C-1": |
| 276 | return k, f"C-1: {k} of {len(want)} curves, rank >= r certified exactly" |
| 277 | return k, f"C-2: {k} of {len(want)} records, rank >= 32 certified exactly" |
| 278 | |
| 279 | |
| 280 | def main(argv): |
| 281 | args = argv[1:] |
| 282 | metrics = None |
| 283 | if "--metrics" in args: |
| 284 | i = args.index("--metrics") |
| 285 | metrics = args[i + 1] if i + 1 < len(args) else None |
| 286 | del args[i:i + 2] |
| 287 | if len(args) != 1 or (metrics is None and "--metrics" in argv): |
| 288 | print("usage: verify.py FOLDER [--metrics PATH]", file=sys.stderr) |
| 289 | return 2 |
| 290 | path = Path(args[0]) / "CERTIFICATE.json" |
| 291 | if not path.is_file(): |
| 292 | print("Nothing to check yet: no CERTIFICATE.json in the route's folder") |
| 293 | return 0 |
| 294 | if path.stat().st_size > MAX_BYTES: |
| 295 | print(f"FAILED CERTIFICATE.json is over {MAX_BYTES:,} bytes", file=sys.stderr) |
| 296 | return 1 |
| 297 | try: |
| 298 | cert = json.loads(path.read_text(encoding="utf-8")) |
| 299 | if not isinstance(cert, dict): |
| 300 | raise Wrong("CERTIFICATE.json must hold an object") |
| 301 | k, line = judge(cert) |
| 302 | except NoVerifier as e: |
| 303 | print(f"no automatic verifier for {e} yet: a maintainer reads this certificate") |
| 304 | return 0 |
| 305 | except (Wrong, ValueError) as e: |
| 306 | print(f"FAILED {e}", file=sys.stderr) |
| 307 | return 1 |
| 308 | print(line) |
| 309 | if metrics: |
| 310 | Path(metrics).write_text(json.dumps({"score": k, "subtasks": {cert["target"]: k}}) + "\n") |
| 311 | return 0 |
| 312 | |
| 313 | |
| 314 | if __name__ == "__main__": |
| 315 | sys.exit(main(sys.argv)) |