# verify_wilson_artin_schreier.py -- ancillary file of # "Infinitely many Wilson primes on the curves y**2 = x**p - x + a" # Pure Python 3, no dependency, no argument. # Ring A = F_p[x,y]/(y**2 - x**p + x - a). For P in F_p[y] irreducible, pi = P(y). # Part 1: for all monic irreducible P of small degree: primality of P(y) from the trace criterion, # W_d = -1 mod pi for the primes, R_0 in V_d (x) A (so K_pi = 0), and # pi Wilson (from the definition, W_d mod pi**2) <=> P'' = 0. # Part 2: members of the infinite families: irreducibility (Rabin test), primality, P'' = 0, # and the Wilson property from the definition. import itertools, time PART1 = [(3, 1, 5), (3, 2, 5), (5, 1, 3), (5, 3, 3), (7, 2, 2), (11, 1, 1)] # (p, a, max degree of P) PART2 = [(3, 1, {4: 1, 1: 1, 0: -1}), (3, 2, {4: 1, 1: 1, 0: -1}), # y**4 + y - 1 (5, 1, {16: 1, 0: -2}), (5, 2, {16: 1, 0: -3}), (5, 4, {16: 1, 0: -2})] # y**16 - c BRUTE_LIMIT = 300 class Curve: def __init__(s, p, h, f): s.p = p; s.h = s.T(h); s.f = s.T(f); s.gy = len(s.f) - 1; s.g = (s.gy - 1) // 2 s.fp = s.der(s.f); s.hp = s.der(s.h); s.f2 = s.has2(s.f); s.hh2 = s.has2(s.h) s.Delta = s.pa(s.pm(s.h, s.h), s.ps(4, s.f)) s.Z = ((), ()); s.ONE = ((1,), ()); s.X = ((0, 1), ()); s.Y = ((), (1,)) s.cache = {} # ---- polynomials over F_p : tuples, lowest degree first, no trailing zero def T(s, c): c = [v % s.p for v in c] while c and c[-1] == 0: c.pop() return tuple(c) def pa(s, a, b): n = max(len(a), len(b)) return s.T([(a[i] if i < len(a) else 0) + (b[i] if i < len(b) else 0) for i in range(n)]) def ps(s, c, a): return s.T([c * v for v in a]) def pm(s, a, b): if not a or not b: return () r = [0] * (len(a) + len(b) - 1) for i, u in enumerate(a): if u: for j, v in enumerate(b): r[i + j] += u * v return s.T(r) def pdm(s, a, b): a = list(a); p = s.p; q = [0] * max(len(a) - len(b) + 1, 0); il = pow(b[-1], p - 2, p) while len(a) >= len(b): c = a[-1] * il % p; t = len(a) - len(b); q[t] = c if c: for i, v in enumerate(b): a[t + i] = (a[t + i] - c * v) % p a.pop() return s.T(q), s.T(a) def pmod(s, a, b): return s.pdm(a, b)[1] def gcd(s, a, b): while b: a, b = b, s.pmod(a, b) return a def der(s, a): return s.T([i * a[i] for i in range(1, len(a))]) def has2(s, a): return s.T([(i * (i - 1) // 2) * a[i] for i in range(2, len(a))]) def c(s, a, i): return a[i] if 0 <= i < len(a) else 0 def monics(s, n): return (tuple(t) + (1,) for t in itertools.product(range(s.p), repeat=n)) def upto(s, n): return (s.T(t) for t in itertools.product(range(s.p), repeat=n + 1)) if n >= 0 else [()] def irreducible(s, a): if a in s.cache: return s.cache[a] d = len(a) - 1; ok = d >= 1 for e in range(1, d // 2 + 1): if not ok: break for b in s.monics(e): if s.pmod(a, b) == (): ok = False; break s.cache[a] = ok; return ok def ppow(s, b, n, m): r = (1,) while n: if n & 1: r = s.pmod(s.pm(r, b), m) b = s.pmod(s.pm(b, b), m); n >>= 1 return r def smooth(s): if s.p > 2: return len(s.gcd(s.Delta, s.der(s.Delta))) == 1 t = s.pa(s.pm(s.pm(s.hp, s.hp), s.f), s.pm(s.fp, s.fp)) return s.h != () and len(s.gcd(s.h, t)) == 1 # ---- the ring A : pairs (a, b) = a + b y def add(s, u, v): return (s.pa(u[0], v[0]), s.pa(u[1], v[1])) def sc(s, c, u): return (s.ps(c, u[0]), s.ps(c, u[1])) def sub(s, u, v): return s.add(u, s.sc(-1, v)) def mul(s, u, v): bb = s.pm(u[1], v[1]) return (s.pa(s.pm(u[0], v[0]), s.pm(bb, s.f)), s.pa(s.pa(s.pm(u[0], v[1]), s.pm(u[1], v[0])), s.ps(-1, s.pm(bb, s.h)))) def D(s, e): # invariant derivation: Dx = 2y+h, Dy = f' - h'y a, b = e; ap = s.der(a); bp = s.der(b); pm = s.pm; pa = s.pa return (pa(pa(pm(ap, s.h), s.ps(2, pm(bp, s.f))), pm(b, s.fp)), pa(pa(s.ps(2, ap), s.ps(-1, pm(bp, s.h))), s.ps(-1, pm(b, s.hp)))) def E(s, e): # the operator E of the note, explicit formula a, b = e; ap = s.der(a); bp = s.der(b); a2 = s.has2(a); b2 = s.has2(b); pm = s.pm; pa = s.pa; ps = s.ps hhp = pm(s.h, s.hp) c0 = pa(pa(pm(s.Delta, a2), pm(pa(s.fp, hhp), ap)), pa(pm(bp, pa(pm(s.h, s.fp), ps(-1, pm(s.hp, s.f)))), pm(b, pa(pm(s.h, s.f2), ps(-2, pm(s.hh2, s.f)))))) c1 = pa(pa(pm(s.hp, ap), pm(s.Delta, b2)), pa(pm(bp, pa(ps(3, s.fp), hhp)), pm(b, pa(ps(2, s.f2), pm(s.h, s.hh2))))) return (c0, c1) def deg(s, e): return max(2 * (len(e[0]) - 1) if e[0] else -1, 2 * (len(e[1]) - 1) + s.gy if e[1] else -1) def inS(s, j): return j >= 0 and (j % 2 == 0 or j >= s.gy) def mono(s, j): return ((0,) * (j // 2) + (1,), ()) if j % 2 == 0 else ((), (0,) * ((j - s.gy) // 2) + (1,)) def coef(s, e, j): if not s.inS(j): return 0 return s.c(e[0], j // 2) if j % 2 == 0 else s.c(e[1], (j - s.gy) // 2) def Vbasis(s, d): return [j for j in range(d + 2 * s.gy) if s.inS(j) and not s.inS(j - d)] def norm(s, e): a, b = e return s.pa(s.pa(s.pm(a, a), s.ps(-1, s.pm(s.pm(a, b), s.h))), s.ps(-1, s.pm(s.pm(b, b), s.f))) def inert(s, a): # is the irreducible polynomial a(x) a prime element of A ? e = len(a) - 1; p = s.p if p > 2: dl = s.pmod(s.Delta, a) return dl != () and s.ppow(dl, (p ** e - 1) // 2, a) == s.pmod((p - 1,), a) hh = s.pmod(s.h, a) if hh == (): return False inv = s.ppow(s.pm(hh, hh), 2 ** e - 2, a); v = s.pmod(s.pm(s.f, inv), a); t = () for _ in range(e): t = s.pa(t, v); v = s.pmod(s.pm(v, v), a) return t == (1,) def primes(s, d): # prime elements of degree d, coefficient 1 on m_d out = [] if d % 2 == 0: mb = (d - s.gy - 1) // 2 if d > s.gy else -1 gen = ((a, b) for a in s.monics(d // 2) for b in s.upto(mb)) elif d >= s.gy: gen = ((a, b) for b in s.monics((d - s.gy) // 2) for a in s.upto((d - 1) // 2)) else: gen = [] for e in gen: if e[1] == (): if s.irreducible(e[0]) and s.inert(e[0]): out.append(e) continue N = s.norm(e) if len(N) - 1 == d and s.irreducible(s.ps(pow(N[-1], s.p - 2, s.p), N)): out.append(e) return out def ideal(s, g): # Hermite basis of g*A as a module over F_p[x] a, b = g; r1 = [a, b]; r2 = [s.pm(b, s.f), s.pa(a, s.ps(-1, s.pm(b, s.h)))] while r2[1] != (): q = s.pdm(r1[1], r2[1])[0] r1 = [s.pa(r1[0], s.ps(-1, s.pm(q, r2[0]))), s.pa(r1[1], s.ps(-1, s.pm(q, r2[1])))]; r1, r2 = r2, r1 return (r1[0], r1[1], r2[0]) def red(s, e, I): s0, g2, g1 = I; a, b = e if g2 != (): q, b = s.pdm(b, g2); a = s.pa(a, s.ps(-1, s.pm(q, s0))) return (s.pmod(a, g1), b) def power(s, e, n, I): r = s.ONE while n: if n & 1: r = s.red(s.mul(r, e), I) e = s.red(s.mul(e, e), I); n >>= 1 return r def W_mod(s, pi, d): # W_d mod pi**2 = coefficient of z in prod_{v in V_d} (z - v) q = s.p; I = s.ideal(s.mul(pi, pi)); Ev = [s.red(s.mono(j), I) for j in s.Vbasis(d)]; l = s.ONE for i in range(d): c = s.power(Ev[i], q - 1, I); l = s.red(s.sc(-1, s.mul(l, c)), I) for j in range(i + 1, d): Ev[j] = s.sub(s.power(Ev[j], q, I), s.red(s.mul(c, Ev[j]), I)) return l def W_brute(s, pi, d): I = s.ideal(s.mul(pi, pi)); B = [s.mono(j) for j in s.Vbasis(d)]; w = s.ONE for cs in itertools.product(range(s.p), repeat=d): if any(cs): v = s.Z for c, b in zip(cs, B): if c: v = s.add(v, s.sc(c, b)) w = s.red(s.mul(w, v), I) return w def R0(s, pi): # R_0 = (pi_1 - pi_2)(y_1 + y_2 + h(x_1)) / (x_1 - x_2), as {j: r_j} a, b = pi; n = max(len(a), len(b), 1) + max(len(s.h), 1) + s.gy + 2 cst = lambda v: ((v % s.p,), ()) if v % s.p else s.Z ah = s.pa(s.pm(a, s.h), s.pm(b, s.f)); C0 = []; C1 = [] for i in range(n): C0.append(s.sub(s.add(cst(s.c(ah, i)), s.sc(s.c(a, i), s.Y)), s.sc(s.c(s.h, i), pi))) C1.append(s.add(cst(s.c(a, i)), s.sc(s.c(b, i), s.Y))) C0[0] = s.sub(C0[0], s.mul(pi, s.Y)); C1[0] = s.sub(C1[0], pi) def divx(Pl): q = [s.Z] * len(Pl) for k in range(len(Pl) - 2, -1, -1): q[k] = s.add(Pl[k + 1], s.mul(s.X, q[k + 1])) assert s.add(Pl[0], s.mul(s.X, q[0])) == s.Z return q R = {} for i, c in enumerate(divx(C0)): if c != s.Z: R[2 * i] = c for i, c in enumerate(divx(C1)): if c != s.Z: R[2 * i + s.gy] = c return R def K_recipe(s, pi, d): # Proposition 9 (needs d >= gamma - 1); pi_d = 1 rho = s.mul(pi, (s.h, (1,))); g = s.g; cs = {} for a in range(g - 1, -1, -1): r = () for i in range(0, s.gy): # r_{d+2a} = sum_i x**i rho_{d+2a+2+2i} v = s.coef(rho, d + 2 * a + 2 + 2 * i) if v: r = s.pa(r, (0,) * i + (v,)) for a2 in range(a + 1, g): r = s.pa(r, s.ps(-s.coef(pi, d - 2 * (a2 - a)), cs[a2])) cs[a] = r K = () for a in range(g): K = s.pa(K, s.ps(-1, s.pm((0,) * a + (1,), cs[a]))) return (K, ()) def curve(p, a): return Curve(p, [0], [a % p, p - 1] + [0] * (p - 2) + [1]) def P_of_y(C, co): # co: tuple of coefficients of P, lowest first r = C.Z; pw = C.ONE for c in co: if c: r = C.add(r, C.sc(c, pw)) pw = C.mul(pw, C.Y) return r def rabin(C, P): # irreducibility of P over F_p n = len(P) - 1; p = C.p; x = (0, 1) def frob(k): v = x for _ in range(k): v = C.ppow(v, p, P) return v if frob(n) != C.pmod(x, P): return False for r in set(q for q in range(2, n + 1) if n % q == 0 and all(q % s for s in range(2, q))): if len(C.gcd(P, C.pa(frob(n // r), C.ps(-1, x)))) != 1: return False return True def trace_test(C, P, a): # Tr_{F_p(theta)/F_p}(theta**2 - a) != 0 n = len(P) - 1; v = C.pmod(C.T([-a, 0, 1]), P); t = () for _ in range(n): t = C.pa(t, v); v = C.ppow(v, C.p, P) assert len(t) <= 1 return t != () def analyse(C, P, a): pi = P_of_y(C, P); d = C.deg(pi); n = len(P) - 1; assert d == C.p * n prime = trace_test(C, P, a); out = {"prime": prime, "ok": True} if not prime: return out I = C.ideal(pi); w = C.W_mod(pi, d) if C.red(C.add(w, C.ONE), I) != C.Z: out["ok"] = False if C.p ** d <= BRUTE_LIMIT and C.W_brute(pi, d) != w: out["ok"] = False out["wilson"] = (w == C.sc(-1, C.ONE)) R = C.R0(pi) if not all(j in C.Vbasis(d) for j in R): out["ok"] = False # R_0 in V_d (x) A, K_pi = 0 out["P2zero"] = (C.der(C.der(P)) == ()) E = C.E(pi); DD = C.D(C.D(pi)) if C.ps(2, E[0]) != DD[0] or C.ps(2, E[1]) != DD[1]: out["ok"] = False # E = D**2 / 2 if (C.red(E, I) == C.Z) != out["wilson"]: out["ok"] = False if out["wilson"] != out["P2zero"]: out["ok"] = False return out if __name__ == "__main__": T0 = time.time(); allok = True print("Part 1", flush=True) for (p, a, nmax) in PART1: C = curve(p, a); assert C.smooth(); cnt = {"irreducible": 0, "prime": 0, "wilson": 0}; ok = True for n in range(1, nmax + 1): for cs in itertools.product(range(p), repeat=n): P = tuple(cs) + (1,) if not C.irreducible(P): continue assert rabin(C, P) cnt["irreducible"] += 1; o = analyse(C, P, a); ok = ok and o["ok"] if o["prime"]: cnt["prime"] += 1; cnt["wilson"] += o["wilson"] allok = allok and ok print(" p=%d a=%d deg P<=%d: irreducible P: %d, P(y) prime: %d, Wilson: %d, consistent: %s (%.0f s)" % (p, a, nmax, cnt["irreducible"], cnt["prime"], cnt["wilson"], ok, time.time() - T0), flush=True) print("Part 2", flush=True) for (p, a, mon) in PART2: C = curve(p, a); n = max(mon); P = C.T([mon.get(i, 0) for i in range(n + 1)]) irr = rabin(C, P); o = analyse(C, P, a) good = irr and o["prime"] and o["ok"] and o.get("wilson") and o.get("P2zero") allok = allok and bool(good) print(" p=%d a=%d P=%s degree of pi=%d: irreducible=%s prime=%s P''=0: %s Wilson=%s (%.0f s)" % (p, a, mon, p * n, irr, o["prime"], o.get("P2zero"), o.get("wilson"), time.time() - T0), flush=True) print("VERDICT: %s (%.0f s)" % ("ALL CHECKS PASSED" if allok else "FAILURE", time.time() - T0), flush=True) input("Press Enter to close...")