"""Directly evaluate the restricted invariant derivatives on two Jacobians. Only Taylor series through order p-1 are needed. The degree p-1 term of dx_i/dz still gives the pth derivative of x_i in characteristic p. """ from math import comb, factorial def flow_at_prime(p, degree, points, direction): genus = (degree - 1) // 2 x = [[t] for t, y in points] y = [[v] for t, v in points] one = [1] # All coefficient arithmetic here is ordinary Taylor arithmetic. for n in range(p): def add(a, b): return [((a[i] if i < len(a) else 0) + (b[i] if i < len(b) else 0)) % p for i in range(n+1)] def neg(a): return [(-v) % p for v in a] def mul(a, b): c = [0] * (n+1) for i, v in enumerate(a): for j in range(min(n+1-i, len(b))): c[i+j] += v * b[j] return [v % p for v in c] def inv(a): b = [pow(a[0], -1, p)] for k in range(1, n+1): b.append(-b[0] * sum(a[i]*b[k-i] for i in range(1, k+1)) % p) return b def root_polynomial(roots): coefs = [one] for root in roots: coefs = [add(a, b) for a, b in zip([[]] + coefs, [mul(neg(root), c) for c in coefs] + [[]])] return coefs dx, dy = [], [] for i in range(genus): others = [x[j] for j in range(genus) if i != j] den = one for r in others: den = mul(den, add(x[i], neg(r))) lagrange = mul(root_polynomial(others)[direction-1], inv(den)) dx.append(mul(y[i], lagrange)[n]) xpower = one for _ in range(degree-1): xpower = mul(xpower, x[i]) dy.append(degree * pow(2, -1, p) * mul(xpower, lagrange)[n] % p) if n == 0: first = dx f0 = -root_polynomial(x)[direction-1][0] % p if n == p-1: factor = factorial(p-1) % p fp = -factor * root_polynomial(x)[direction-1][n] % p return first, [factor*v % p for v in dx], f0, fp step = pow(n+1, -1, p) for i in range(genus): x[i].append(dx[i] * step % p) y[i].append(dy[i] * step % p) def check(p, degree, directions): genus = (degree-1)//2 points = [] for x in range(p): y = next((y for y in range(1, p) if (y*y-x**degree+1) % p == 0), None) if y is not None: points.append((x, y)) if len(points) == genus: break assert len(points) == genus data = [flow_at_prime(p, degree, points, j) for j in directions] def coefficient(i, j): k, rem = divmod(p*j-i, degree) assert not rem and 0 <= k <= (p-1)//2 return (-1)**((p-1)//2-k) * comb((p-1)//2, k) % p links = [coefficient(b, a) for a, b in zip(directions, directions[1:])] last = coefficient(genus+1, directions[-1]) for i, link in enumerate(links): assert data[i][1] == [link*v % p for v in data[i+1][0]] assert data[i][3] == link*data[i+1][2] % p assert all(t == 0 for t in data[-1][1]) assert data[-1][3] == -last % p print(f'genus {genus}, p={p}, directions {directions}, divisor {points}') print(f' Dᵖ links {links}, last Dᵖ zero, last D^(p-1)F = {data[-1][3]} mod {p}') if __name__ == '__main__': check(11, 7, [2, 1]) check(23, 7, [1, 2]) check(17, 15, [1, 2, 4])