"""Certify squarefreeness throughout Zhang's range by reduction modulo one prime. Run with: uv run --with python-flint==0.9.0 verify_simple.py """ from flint import fmpz, nmod_poly from verify import TYPES PRIME = 2**31 - 1 X = nmod_poly([0, 1], PRIME) def reduce_generator(poly): return nmod_poly([int(c) % PRIME for c in poly.coeffs()], PRIME) def certify_length(q, b, step, ratio, first, second, length): """Return deg P after verifying degree preservation and gcd(P,P') = 1.""" if length >= PRIME: raise ValueError("The certificate requires a prime larger than the code length.") exponent = length // step terms = exponent // ratio distance = b * (terms + 1) degree = length + 2 - 2*distance # A triangular Gleason basis reconstructs W(1,y) in Y = y^b. basis = enumerator = first**exponent divisor = first**ratio for j in range(1, terms + 1): quotient, remainder = divmod(basis, divisor) if remainder: raise ArithmeticError("Inexact division in the Gleason basis.") basis = quotient * second if basis[j] != 1: raise ArithmeticError("Unexpected leading coefficient in the Gleason basis.") enumerator -= enumerator[j] * basis # Form W_y(1+v,1) over the prime field. derivative = [0] * length for w in range(distance, length + 1, b): derivative[length-w] = w * int(enumerator[w//b]) % PRIME shifted = nmod_poly(derivative, PRIME)(1 + X) # Multiplying the coefficient formula for P/(1-qT) by (q-1)n! # clears all denominators. This scalar is a unit in our prime field. factorial = [1] * length for j in range(1, length): factorial[j] = j * factorial[j-1] % PRIME values = [] for i in range(degree + 1): power = length - distance - i value = int(shifted[power]) * factorial[power] * factorial[distance+i-1] values.append(value % PRIME) p = (nmod_poly(values, PRIME) * (1 - q*X)).truncate(degree+1) if p.degree() != degree: raise ArithmeticError(f"Degree dropped modulo {PRIME} at q={q}, b={b}, n={length}.") if not p.gcd(p.derivative()).is_one(): raise ArithmeticError(f"Repeated factor modulo {PRIME} at q={q}, b={b}, n={length}.") return degree def main(): if not fmpz(PRIME).is_prime(): raise ArithmeticError("The reduction modulus must be prime.") total_pairs = total_degree = 0 for kind, (q, b, step, ratio, first, second, bounds) in TYPES.items(): first = reduce_generator(first) second = reduce_generator(second) pairs = degree_sum = 0 for residue, bound in enumerate(bounds): for index in range(bound): length = step * (ratio*index + residue) if length: degree_sum += certify_length(q, b, step, ratio, first, second, length) pairs += 1 print(f"Type {kind}: {pairs} squarefree polynomials, total degree {degree_sum}.") total_pairs += pairs total_degree += degree_sum if (total_pairs, total_degree) != (777, 654930): raise ArithmeticError("Unexpected number of candidate pairs or total degree.") print(f"All {total_pairs} pairs certified modulo {PRIME}, {total_degree} simple zeros.") if __name__ == "__main__": main()