"""Check BSD data for elliptic curves over Q, one JSON object per line in curves.jsonl. For each curve: recompute the discriminant from the a-invariants, require the conductor's primes to be exactly the primes of bad reduction, recount points mod small good primes and compare a_p, then put the submitted analytic values into the BSD formula Sha_an = L*(E,1) * #T^2 / (Omega * R * prod c_p) and require a perfect square equal to sha_an. Standard library only.""" import json, math, sys from pathlib import Path TOL = 1e-6 # relative tolerance on Sha_an from the submitted decimals PRIMES = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] def fail(msg): print(f"rejected: {msg}") sys.exit(1) def invariants(a1, a2, a3, a4, a6): b2 = a1 * a1 + 4 * a2 b4 = 2 * a4 + a1 * a3 b6 = a3 * a3 + 4 * a6 b8 = a1 * a1 * a6 + 4 * a2 * a6 - a1 * a3 * a4 + a2 * a3 * a3 - a4 * a4 disc = -b2 * b2 * b8 - 8 * b4 ** 3 - 27 * b6 * b6 + 9 * b2 * b4 * b6 return disc def primes_of(n): n, out, p = abs(n), set(), 2 while p * p <= n: while n % p == 0: out.add(p); n //= p p += 1 if n > 1: out.add(n) return out def ap(ainvs, p): a1, a2, a3, a4, a6 = ainvs count = 1 # point at infinity for x in range(p): for y in range(p): if (y * y + a1 * x * y + a3 * y - (x ** 3 + a2 * x * x + a4 * x + a6)) % p == 0: count += 1 return p + 1 - count def check(n, c): label = c.get("label", f"line {n}") ainvs = c.get("ainvs") if not (isinstance(ainvs, list) and len(ainvs) == 5 and all(isinstance(a, int) for a in ainvs)): fail(f"{label}: ainvs must be 5 integers") disc = invariants(*ainvs) if disc == 0: fail(f"{label}: discriminant 0, not an elliptic curve") N = c.get("conductor") if not (isinstance(N, int) and N >= 1): fail(f"{label}: conductor must be a positive integer") if primes_of(N) != primes_of(disc): fail(f"{label}: conductor {N} has primes {sorted(primes_of(N))}, bad primes of the curve are {sorted(primes_of(disc))}") for p, want in (c.get("ap") or {}).items(): p = int(p) if N % p == 0: continue got = ap(ainvs, p) if got != want: fail(f"{label}: a_{p} = {got} from the a-invariants, submitted {want}; the data is not this curve") for k in ("rank", "torsion", "tamagawa", "sha_an"): if not (isinstance(c.get(k), int) and c[k] >= (0 if k == "rank" else 1)): fail(f"{label}: {k} must be an integer") for k in ("omega", "regulator", "lvalue"): if not (isinstance(c.get(k), (int, float)) and c[k] > 0): fail(f"{label}: {k} must be a positive number") if c["rank"] == 0 and abs(c["regulator"] - 1) > TOL: fail(f"{label}: rank 0 needs regulator 1") sha = c["lvalue"] * c["torsion"] ** 2 / (c["omega"] * c["regulator"] * c["tamagawa"]) nearest = round(sha) if nearest < 1 or abs(sha - nearest) > TOL * max(1, nearest): fail(f"{label}: Sha = {sha:.6f} from the BSD formula is not an integer") r = math.isqrt(nearest) if r * r != nearest: fail(f"{label}: Sha = {nearest} is not a perfect square") if nearest != c["sha_an"]: fail(f"{label}: BSD formula gives Sha = {nearest}, submitted sha_an {c['sha_an']}") return N def main(): if len(sys.argv) != 2: fail("usage: python3 verify.py ") f = Path(sys.argv[1]) / "curves.jsonl" if not f.exists(): fail("curves.jsonl missing") seen, maxN = set(), 0 lines = [l for l in f.read_text(encoding="utf-8").splitlines() if l.strip()] if not lines: fail("no curves") for n, line in enumerate(lines, 1): try: c = json.loads(line) except ValueError as e: fail(f"line {n}: not JSON: {e}") key = tuple(c.get("ainvs") or []) if key in seen: fail(f"line {n}: curve listed twice") seen.add(key) maxN = max(maxN, check(n, c)) print(f"bsd: {len(lines)} curves checked, sha all squares, max conductor {maxN}") main()