"""Reproduce the cyclotomic factor examples using Python 3.8+ and no packages. Polynomials are coefficient tuples in ascending degree; () denotes zero. All arithmetic in the refinement routine takes place over F_p. Supplied factors are inputs, and root lists are used only for the independent tests. """ from itertools import combinations from math import isqrt ONE = (1,) X = (0, 1) def canonical(coefficients, p): a = [c % p for c in coefficients] while a and not a[-1]: a.pop() return tuple(a) def subtract(a, b, p): c = [0] * max(len(a), len(b)) for i, x in enumerate(a): c[i] += x for i, x in enumerate(b): c[i] -= x return canonical(c, p) def multiply(a, b, p): if not a or not b: return () c = [0] * (len(a) + len(b) - 1) for i, x in enumerate(a): for j, y in enumerate(b): c[i+j] += x*y return canonical(c, p) def product(factors, p): value = ONE for factor in factors: value = multiply(value, factor, p) return value def divide(a, b, p): if not b: raise ZeroDivisionError("zero polynomial divisor") rem = list(a) quot = [0] * max(0, len(a) - len(b) + 1) lead_inverse = pow(b[-1], -1, p) while len(rem) >= len(b): offset = len(rem) - len(b) ratio = (rem[-1] * lead_inverse) % p quot[offset] = ratio for j, coeff in enumerate(b): rem[offset+j] = (rem[offset+j] - ratio*coeff) % p while rem and not rem[-1]: rem.pop() return tuple(quot), tuple(rem) def poly_gcd(a, b, p): while b: a, b = b, divide(a, b, p)[1] if not a: return () inv = pow(a[-1], -1, p) return tuple(c*inv % p for c in a) def pow_mod(a, n, modulus, p): value = ONE while n: if n & 1: value = divide(multiply(value, a, p), modulus, p)[1] a = divide(multiply(a, a, p), modulus, p)[1] n //= 2 return value def prime(n): return n >= 2 and all(n%d for d in range(2, isqrt(n)+1)) def valuation(n, r): exponent = 0 while n % r == 0: n //= r exponent += 1 return exponent def power_translate(f, unit, ell, p): """Monic polynomial on the set {s^unit : f(s)=0}, for f dividing Phi_ell.""" inverse = pow(unit, -1, ell) # f(T^inverse) evaluated modulo T^ell-1 suffices on the primitive roots. folded = [0] * ell for j, coeff in enumerate(f): folded[j*inverse % ell] += coeff return poly_gcd((1,)*ell, canonical(folded, p), p) def translated_atoms(p, ell, f): if not prime(ell) or not prime(p) or ell == p: raise ValueError("distinct prime characteristic and conductor required") phi = (1,)*ell if not f or f[-1] != 1 or divide(phi, f, p)[1]: raise ValueError("expected a monic divisor of the cyclotomic polynomial") atoms = [phi] for u in range(1, ell): translated = power_translate(f, u, ell, p) refined = [] for block in atoms: inside = poly_gcd(block, translated, p) if inside == ONE or inside == block: refined.append(block) else: outside, rem = divide(block, inside, p) assert not rem refined.extend([inside, outside]) atoms = refined assert product(atoms, p) == phi return sorted(atoms) def quotient_coset(p, ell, r, atom, depth=0): """From one atom recover a coset of the subgroup of order h*r^depth. Return None exactly if the atom is larger in r-primary depth and the requested coset cannot be formed as a union of atoms. """ if not prime(r) or (ell-1) % r: raise ValueError("expected a prime r dividing ell-1") b = valuation(ell-1, r) if not 0 <= depth <= b: raise ValueError("invalid primary depth") index = r**(b-depth) subgroup = {pow(u, index, ell) for u in range(1, ell)} orbit = {power_translate(atom, u, ell, p) for u in subgroup} factor = product(sorted(orbit), p) if len(factor)-1 != len(subgroup): return None return factor def roots_poly(roots, p): return product([((-x) % p, 1) for x in roots], p) def support(f): return tuple(i for i, a in enumerate(f) if a) def test_two_cubics(): p, ell, r = 127, 7, 3 good = roots_poly([2,4,8], p) symmetric = roots_poly([2,4,16], p) assert good == (63,56,113,1) and symmetric == (126,104,105,1) common = poly_gcd(good, power_translate(good, 2, ell, p), p) assert common == ((-4) % p, 1) atoms = translated_atoms(p, ell, good) assert len(atoms) == 6 and common in atoms recovered = quotient_coset(p, ell, r, common) assert recovered == (1,91,1) == roots_poly([4,32], p) other_atoms = translated_atoms(p, ell, symmetric) assert len(other_atoms) == 2 and all(len(a)==4 for a in other_atoms) assert power_translate(symmetric, 2, ell, p) == symmetric assert all(quotient_coset(p, ell, r, a) is None for a in other_atoms) print("p=127, ell=7: translating the cubic with roots 2,4,8 by squaring") print("and taking its gcd gives T-4. The inversion orbit then gives T^2-36T+1.") print("The cubic with roots 2,4,16 is fixed by squaring; its two atoms have degree 3.") def test_characteristic_two(): p, ell, r = 2, 43, 3 exponents = {0,2,4,7,10,12,14} f = tuple(int(i in exponents) for i in range(15)) atoms = translated_atoms(p, ell, f) assert len(atoms) == 3 and all(len(a)==15 for a in atoms) assert all(quotient_coset(p, ell, r, a) == a for a in atoms) for a in atoms: # Rabin's irreducibility criterion, since 14 has prime divisors 2 and 7. assert pow_mod(X, 2**14, a, p) == X for d in (2,7): assert poly_gcd(a, subtract(pow_mod(X, 2**d, a, p), X, p), p) == ONE # A rank-two scalar extension sends this prime-field polynomial to a # norm a^2. The two nested gcd layers both recover a in characteristic 2. norm = multiply(a, a, p) for _ in range(2): layer = poly_gcd(norm, (1,)*ell, p) assert layer == a norm, rem = divide(norm, layer, p) assert not rem assert norm == ONE H = {pow(u, 3, ell) for u in range(1, ell)} cosets = sorted({tuple(sorted({u*x % ell for x in H})) for u in range(1,ell)}) period_polys = [tuple(int(i in coset) for i in range(ell)) for coset in cosets] evaluations = [] for atom in atoms: remainders = [divide(v,atom,p)[1] for v in period_polys] assert all(v in ((),ONE) for v in remainders) evaluations.append(tuple(int(bool(v)) for v in remainders)) assert set(evaluations) == {(1,0,0),(0,1,0),(0,0,1)} assert product(atoms,p) == (1,)*ell and len(H) == 14 print("p=2, ell=43: irreducible factor supports (nonzero coefficient indices) and period values") for a, e in zip(atoms,evaluations): print(f" {support(a)} gives {e}") print("All three degree-14 factors are recovered although 2 divides the subgroup order.") def test_binomials(): p = 41 for r,ell,h in [(2,97,96),(3,37,18),(5,61,10)]: phi = (1,)*ell assert pow(p, h, ell) == 1 for prime_factor in (a for a in range(2,h+1) if prime(a) and not h%a): assert pow(p,h//prime_factor,ell) != 1 assert (ell-1)//h % r and h%r == 0 assert pow(p,(ell-1)//r,ell) != 1 binomial = ((-1)%p,)+(0,)*(ell-1)+(1,) assert multiply(((-1)%p,1),phi,p) == binomial assert poly_gcd(binomial,canonical([i*c for i,c in enumerate(binomial) if i],p),p)==ONE # All primitive roots have degree h because Frobenius iterates as p^j mod ell. print(f"p=41, r={r}, ell={ell}: X^{ell}-1 has one linear factor and {(ell-1)//h} of degree {h}.") def test_all_seven_subsets(): p,ell,r = 127,7,3 group = set(range(1,ell)) for size in range(ell): for choice in combinations(sorted(group),size): selected = set(choice) stab = {u for u in group if {x*u%ell for x in selected}==selected} f = roots_poly([pow(2,x,p) for x in selected],p) atoms = translated_atoms(p,ell,f) assert len(atoms)*len(stab)==ell-1 and all(len(a)-1==len(stab) for a in atoms) expected = {roots_poly([pow(2,(u*j)%ell,p) for j in stab],p) for u in group} assert set(atoms)==expected for depth in (0,1): result = quotient_coset(p,ell,r,atoms[0],depth) assert (result is None) == (valuation(len(stab),r)>depth) print("All 64 subsets for ell=7 agree with the independent stabilizer oracle at both depths.") def test_intermediate_depth(): p, ell, r, z = 191, 19, 3, 5 assert prime(p) and pow(z,ell,p) == 1 and z != 1 selected = (1,7,11) f = roots_poly([pow(z,i,p) for i in selected],p) atoms = translated_atoms(p,ell,f) assert len(atoms)==6 and f in atoms and all(len(a)==4 for a in atoms) assert quotient_coset(p,ell,r,f,0) is None middle = quotient_coset(p,ell,r,f,1) assert middle == roots_poly([pow(z,i,p) for i in (1,7,8,11,12,18)],p) assert quotient_coset(p,ell,r,f,2) == (1,)*ell print("p=191, r=3, ell=19, z=5: the factor on exponents {1,7,11} isolates") print("a degree-6 factor on {1,7,8,11,12,18}. No degree-2 coset is a union of atoms.") if __name__ == "__main__": test_two_cubics() test_characteristic_two() test_binomials() test_all_seven_subsets() test_intermediate_depth()