# verify_wilson_general.py # Ancillary file of the note # "Wilson primes for the ring of functions regular outside a rational point of a curve" # David Niedbala Giraudin # # Pure Python 3, no external module, no argument. Run it as it is. # It prints as it goes and stops on the first failed check (AssertionError). # # Test rings (for g >= 2 the semigroup at P0 is not generated by two elements): # C : y**2 = f(x) over F_p, p odd, deg f = 2g+1, f squarefree, P0 = (0, y0), # A = ring of the functions on C regular outside P0 (so "infinity" is P0). # An element of A is a pair (a, b) of Laurent polynomials in x, meaning a + b*y. # The basis (m_s) is computed by linear algebra from the Riemann-Roch spaces L(s P0). # # For every prime element pi of degree d treated: # - W_d is computed modulo pi**2 from the definition (plain product when # p**d <= 3000, otherwise through the linear polynomial e(z)); # - THEOREM 3: the dual basis (nu_j) of the monomials of V_d for the residue # pairing is computed, and W_d + 1 = pi * Theta/(d pi)**2 mod pi**2 is checked, # Theta = sum_j d(m_j) nu_j ; # - THEOREM 7: with the explicit kernel kappa of the note, R_0 = (pi_1 - pi_2) kappa is # expanded in the second variable, decomposed as R + pi_1 * kappa', the support of # kappa' (monomials of degree <= g) is checked, and W_d + 1 = pi * (E(pi) + K d pi)/(d pi)**2 mod pi**2 is checked. import itertools, time T0 = time.time() def say(*a): print(*a, flush=True) assert 2 ** 10 == 1024 and 7 // 2 == 3 and 7 % 2 == 1 say("Self-test passed.") class Ring: def __init__(s,p,f,y0,nmax,xm): s.p=p; s.f=[c%p for c in f]; s.g=(len(f)-1)//2; s.y0=y0%p assert (y0*y0-f[0])%p==0 and f[0]%p s.inv=[0]+[pow(i,p-2,p) for i in range(1,p)] s.fd={i:c for i,c in enumerate(s.f) if c} s.fp={i-1:(i*c)%p for i,c in enumerate(s.f) if i and (i*c)%p} s.OFF=nmax+6; s.XM=xm; s.L=s.OFF+s.XM # series of y at P0 n=s.L+5; ys=[0]*n; ys[0]=s.y0; i2=s.inv[(2*s.y0)%p] for k in range(1,n): t=(s.f[k] if kprev: # element with exact pole order n for u in B: e=s.exp(u) if e[s.OFF-n]%p: s.m[n]=s.scal(u,s.inv[e[s.OFF-n]%p]); s.S.append(n); break else: raise Exception("no element") prev=len(B) s.Sset=set(s.S) # ---- Laurent polys def ladd(s,a,b): r=dict(a) for e,c in b.items(): v=(r.get(e,0)+c)%s.p if v: r[e]=v else: r.pop(e,None) return r def lscal(s,a,c): c%=s.p return {e:(v*c)%s.p for e,v in a.items()} if c else {} def lmul(s,a,b): r={} for e1,c1 in a.items(): for e2,c2 in b.items(): r[e1+e2]=(r.get(e1+e2,0)+c1*c2)%s.p return {e:v for e,v in r.items() if v} def lder(s,a): return {e-1:(e*c)%s.p for e,c in a.items() if (e*c)%s.p} # ---- elements def add(s,u,v): return (s.ladd(u[0],v[0]),s.ladd(u[1],v[1])) def scal(s,u,c): return (s.lscal(u[0],c),s.lscal(u[1],c)) def sub(s,u,v): return s.add(u,s.scal(v,-1)) def mul(s,u,v): return (s.ladd(s.lmul(u[0],v[0]),s.lmul(s.lmul(u[1],v[1]),s.fd)), s.ladd(s.lmul(u[0],v[1]),s.lmul(u[1],v[0]))) def power(s,u,e): r=({0:1},{}) while e: if e%2: r=s.mul(r,u) u=s.mul(u,u); e//=2 return r def iszero(s,u): return not u[0] and not u[1] def exp(s,u,sign=1): # expansion of a + sign*b*yhat at x=0, window [-OFF, XM) r=[0]*s.L for e,c in u[0].items(): assert e>=-s.OFF if e=-s.OFF for k in range(0,s.XM-e): r[e+k+s.OFF]=(r[e+k+s.OFF]+sign*c*s.ys[k])%s.p return r def deglead(s,u): if s.iszero(u): return (-1,0) e=s.exp(u) for i in range(s.OFF): if e[i]: return (s.OFF-i,e[i]) assert not u[1] and set(u[0])=={0}, "non-constant element without pole" return (0,u[0][0]) def nullspace(s,rows,nv): p=s.p; M=[r[:] for r in rows]; piv=[]; rk=0 for c in range(nv): pr=None for r in range(rk,len(M)): if M[r][c]%p: pr=r;break if pr is None: continue M[rk],M[pr]=M[pr],M[rk]; iv=s.inv[M[rk][c]%p]; M[rk]=[(x*iv)%p for x in M[rk]] for r in range(len(M)): if r!=rk and M[r][c]%p: f=M[r][c]; M[r]=[(x-f*y)%p for x,y in zip(M[r],M[rk])] piv.append(c); rk+=1 free=[c for c in range(nv) if c not in piv]; out=[] for fc in free: v=[0]*nv; v[fc]=1 for i,pc in enumerate(piv): v[pc]=(-M[i][fc])%p out.append(v) return out def space(s,n,amax,bmax): # pairs (a,b): a exps in [-n,amax], b exps in [-n,bmax], a - b*yhat regular at x=0 ea=list(range(-n,amax+1)); eb=list(range(-n,bmax+1)); nv=len(ea)+len(eb) rows=[[0]*nv for _ in range(n)] # coefficient of x**(-n+i), i=0..n-1 for j,e in enumerate(ea): if e<0: rows[e+n][j]=1 for j,e in enumerate(eb): for k in range(0,-e): rows[e+k+n][len(ea)+j]=(-s.ys[k])%s.p out=[] for v in s.nullspace(rows,nv): a={e:v[j] for j,e in enumerate(ea) if v[j]}; b={e:v[len(ea)+j] for j,e in enumerate(eb) if v[len(ea)+j]} out.append((a,b)) return out def Lspace(s,n): return s.space(n,0,-(s.g+1)) # L(n P0) def Ospace(s,n): return s.space(n,s.g-1,-2) # h with h*dx/y regular outside P0, pole of h <= n def Zspace(s,n): return s.space(n,-(s.g+1),-(s.g+1)-s.g) # L(nP0) with zero of order >= 2g+1 at infinity def inS(s,n): return n in s.Sset def Vd(s,d): return [n for n in s.S if n<=d+2*s.g-1 and (n-d) not in s.Sset] def reducer(s,w): D,lw=s.deglead(w); cache={} def sh(k): if k not in cache: pr=s.mul(w,s.m[k]); dd,ll=s.deglead(pr); assert dd==D+k cache[k]=s.scal(pr,s.inv[ll]) return cache[k] def red(u): coords={} while not s.iszero(u): n,c=s.deglead(u) if n-D in s.Sset: u=s.sub(u,s.scal(sh(n-D),c)) else: assert n in s.Sset coords[n]=c; u=s.sub(u,s.scal(s.m[n],c)) return coords return red def lift(s,coords): u=({},{}) for n,c in coords.items(): u=s.add(u,s.scal(s.m[n],c)) return u class Fpi: # residue field A/pi, vectors of coordinates on V_d def __init__(s,R,pi,d): s.R=R; s.pi=pi; s.d=d; s.V=R.Vd(d); assert len(s.V)==d; s.red=R.reducer(pi); s.p=R.p def vec(s,u): c=s.red(u); return tuple(c.get(n,0) for n in s.V) def el(s,v): return s.R.lift({n:c for n,c in zip(s.V,v) if c}) def mul(s,v,w): return s.vec(s.R.mul(s.el(v),s.el(w))) def add(s,v,w): return tuple((a+b)%s.p for a,b in zip(v,w)) def scal(s,v,c): return tuple((a*c)%s.p for a in v) def pw(s,v,e): r=s.vec(({0:1},{})) while e: if e%2: r=s.mul(r,v) v=s.mul(v,v); e//=2 return r def inv(s,v): return s.pw(v,s.p**s.d-2) def isprime(s): p=s.p; d=s.d M=[list(s.pw(tuple(1 if i==j else 0 for j in range(d)),p)) for i in range(d)] def rank(M): M=[r[:] for r in M]; rk=0 for c in range(d): pr=None for r in range(rk,d): if M[r][c]%p: pr=r;break if pr is None: continue M[rk],M[pr]=M[pr],M[rk]; iv=pow(M[rk][c],p-2,p); M[rk]=[(x*iv)%p for x in M[rk]] for r in range(d): if r!=rk and M[r][c]%p: f=M[r][c]; M[r]=[(x-f*y)%p for x,y in zip(M[r],M[rk])] rk+=1 return rk if rank(M)=1 else ({},{}) if i=0 else F.pw(xi,-e) r=F.add(r,F.scal(t,c)) return r fx=evl(R.fd); assert F.mul(yb,yb)==fx, "y*y differs from f(x) in F_pi" fpx=evl(R.fp) def ev(u): return F.add(evl(u[0]),F.mul(evl(u[1]),yb)) def evD(u): # D = 2y d/dx : D(a+by) = 2y a' + 2f b' + f' b ; du = D(u) * dx/(2y) return F.add(F.add(F.scal(F.mul(evl(R.lder(u[0])),yb),2),F.scal(F.mul(evl(R.lder(u[1])),fx),2)),F.mul(evl(u[1]),fpx)) assert not any(ev(pi)), "evaluation map wrong" for n in V: assert ev(R.m[n])==F.vec(R.m[n]) Th=tuple([0]*d) for j,n in enumerate(V): hj=({},{}) for l,c in enumerate(N[j]): if c: hj=R.add(hj,R.scal(H[l],c)) Th=F.add(Th,F.mul(evD(R.m[n]),ev(hj))) dp=evD(pi); assert any(dp) # nu_j = h_j dx/y = 2 h_j * dx/(2y) Q=F.mul(F.scal(Th,2),F.inv(F.mul(dp,dp))) red2=R.reducer(R.mul(pi,pi)) lhs=R.sub(R.add(W,one),R.mul(pi,F.el(Q))) assert not red2(lhs), "THEOREM FAILS" wil=not red2(R.add(W,one)) assert wil==(not any(Q)) return wil,W,Q def primes(R,d): low=[n for n in R.S if n=g: a={j-n-1:ys[j] for j in range(n+1) if ys[j]} u=R.add(u,(a,{-(n+1):1})) return u redq=reducer_q(R,pi) Nlo=-d-1; Nhi=2*g+3 gam={} # s -> {N: coef} for N in range(Nlo,Nhi+1): rho=({},{}) for n in range(-1,N+d+2): j=N-n # coefficient of x2**j in s and in e_pi*s sj=sser[j+R.OFF] if -R.OFF<=j=0 else F.pw(xi,-e) r=F.add(r,F.scal(t,c)) return r def ev(u): return F.add(evl(u[0]),F.mul(evl(u[1]),yb)) def Dop(u): a,b=u return (R.ladd(R.lscal(R.lmul(R.lder(b),R.fd),2),R.lmul(b,R.fp)), R.lscal(R.lder(a),2)) Kterm=tuple([0]*d) for s_,ser in gam.items(): rows=[[Hexp[l][N+R.OFF] for l in range(len(H))]+[ser.get(N,0)] for N in range(Nlo,Nhi+1)] # solve nh=len(H); rk=0; piv=[] for c in range(nh): pr=None for r in range(rk,len(rows)): if rows[r][c]%p: pr=r;break if pr is None: continue rows[rk],rows[pr]=rows[pr],rows[rk]; iv=pow(rows[rk][c],p-2,p); rows[rk]=[(x*iv)%p for x in rows[rk]] for r in range(len(rows)): if r!=rk and rows[r][c]%p: f=rows[r][c]; rows[r]=[(x-f*y)%p for x,y in zip(rows[r],rows[rk])] piv.append(c); rk+=1 for r in range(rk,len(rows)): assert rows[r][nh]%p==0, "c_s not a differential of the expected space" hs=({},{}) for i,c in enumerate(piv): if rows[i][nh]: hs=R.add(hs,R.scal(H[c],rows[i][nh])) Kterm=F.add(Kterm,F.mul(F.vec(R.m[s_]),ev(hs))) Dpi=Dop(pi); D2=Dop(Dpi); dp=ev(Dpi); idp=F.inv(dp) # B = [y0 - (g-1) y - sum_{i= len(b): c = (r[-1] * iv) % p for i in range(len(b)): r[len(r) - len(b) + i] = (r[len(r) - len(b) + i] - c * b[i]) % p r.pop() a, b = b, trim(r) return len(a) == 1 CASES = [ ("g=1 p=3 y**2 = x**3+x+1, P0=(0,1)", 3, [1, 1, 0, 1], 1, 6, None), ("g=2 p=3 y**2 = x**5+x**2+1, P0=(0,1)", 3, [1, 0, 1, 0, 0, 1], 1, 7, None), ("g=2 p=5 y**2 = x**5+2x+1, P0=(0,1)", 5, [1, 2, 0, 0, 0, 1], 1, 5, None), ("g=2 p=7 y**2 = x**5+x+4, P0=(0,2)", 7, [4, 1, 0, 0, 0, 1], 2, 3, None), ("g=3 p=3 y**2 = x**7+x+1, P0=(0,1)", 3, [1, 1, 0, 0, 0, 0, 0, 1], 1, 6, None), ] GT = 0 GW = 0 for name, p, f, y0, dm, extra in CASES: g = (len(f) - 1) // 2 assert squarefree(f, p) dtop = extra if extra else dm R = Ring(p, f, y0, 4 * dtop + 4 * g + 2, 2 * dtop + 2 * g + 8) say("") say("%s semigroup at P0: %s ..." % (name, R.S[:7])) tot = 0 wl = 0 wdeg = [] for d in range(2, dtop + 1): if d not in R.Sset: continue n = 0 w = 0 for pi in primes(R, d): wil, W, QA_ = check(R, pi, d) QB = checkB(R, pi, d) assert QB == QA_, "Theorem 3 and Theorem 7 disagree" n += 1 w += wil tot += n wl += w if w: wdeg.append(d) say(" d=%d : %d prime elements, %d Wilson [%.0f s]" % (d, n, w, time.time() - T0)) GT += tot GW += wl say(" total %d prime elements, %d Wilson primes, degrees %s" % (tot, wl, wdeg)) say("") say("SUMMARY : %d prime elements on %d rings, %d Wilson primes;" % (GT, len(CASES), GW)) say(" Theorem 3 and Theorem 7 checked for each of them, no failure.") say("All checks passed. Total time %.0f s." % (time.time() - T0)) try: input("Press Enter to close...") except EOFError: pass