"""Reconstructed exact CLIMB certificates; all polynomial arithmetic is integral. Independent constructions: full injection products (reference), and overlap matchings with labelled-slot multiplicities (primary). No historical certificate is trusted. """ import argparse from collections import Counter, defaultdict from functools import lru_cache from hashlib import sha256 from itertools import combinations, permutations from math import factorial from pathlib import Path import json import platform import time import flint from flint import fmpz_mpoly_ctx, Ordering def context(n): return fmpz_mpoly_ctx.get([f"x{i+1}" for i in range(n)], Ordering.lex) def injection(ctx, n, parts): out = Counter() for labels in permutations(range(n), len(parts)): e = [0] * n for i, b in zip(labels, parts): e[i] += b out[tuple(e)] += 1 return ctx.from_dict(dict(out)) def euler(f, x, ell): return sum((x[i] * f.derivative(i) for i in range(ell)), x[0] * 0) def direct(n, capacity, ell, primitive=False): ctx = context(n) x = ctx.gens() b = tuple(range(1, capacity+1)) z = injection(ctx, n, b) f = z * sum(x) g = injection(ctx, n, b+(1,)) r = g*euler(f,x,ell) - f*euler(g,x,ell) if primitive: return ctx.from_dict({tuple(v-1 for v in e): c for e,c in r.terms() if min(e) > 0}) return r @lru_cache(None) def orbit_shapes(capacity, n): """shape -> per-exponent scores including exact equal-exponent multiplicities.""" h = 2*capacity+1-n assert 0 <= h <= capacity result = defaultdict(Counter) blue = tuple(range(1,capacity+1))+(1,) for r in range(capacity): red = list(range(1,capacity+1)); red[r] += 1 for ri in combinations(range(capacity),h): for bj in permutations(range(capacity+1),h): matched = dict(zip(ri,bj)) components = [] for i,e in enumerate(red): if i in matched: f=blue[matched[i]]; components.append((e+f-1,e-f)) else: components.append((e-1,e)) components += [(f-1,-f) for j,f in enumerate(blue) if j not in bj] shape = tuple(sorted((t for t,s in components),reverse=True)) counts = Counter(shape) multiplicity = 1 for c in counts.values(): multiplicity *= factorial(c) scores = Counter() for t,s in components: scores[t] += s for t,s in scores.items(): result[shape][t] += s*(multiplicity//counts[t]) return {shape:dict(scores) for shape,scores in result.items() if any(scores.values())} def multiset_permutations(shape): counts=Counter(shape); values=sorted(counts,reverse=True); out=[0]*len(shape) def rec(i): if i==len(out): yield tuple(out); return for v in values: if counts[v]: counts[v]-=1;out[i]=v yield from rec(i+1) counts[v]+=1 yield from rec(0) def kernels(capacity,n): polynomials=[{} for _ in range(n-1)] for shape,scores in orbit_shapes(capacity,n).items(): for e in multiset_permutations(shape): score=0 for ell in range(1,n): score+=scores.get(e[ell-1],0) if score: polynomials[ell-1][e]=score ctx=context(n) return [ctx.from_dict(d) for d in polynomials] def gap(f,n): ctx=context(n);x=ctx.gens() return f.compose(*(sum(x[i:]) for i in range(n)),ctx=ctx) def digest(f): h=sha256() # FLINT lex order is deterministic; this serialization is documented in README. for e,c in f.terms(): h.update((','.join(map(str,e))+':'+str(c)+'\n').encode('ascii')) return h.hexdigest() def certify(max_capacity,output): records=[] for capacity in range(1,max_capacity+1): for n in range(capacity+1,2*capacity+2): start=time.monotonic() for ell,q in enumerate(kernels(capacity,n),1): independent=capacity<=3 or (capacity==4 and n<=6) if independent: assert q==direct(n,capacity,ell,True),(capacity,n,ell,'reference mismatch') g=gap(q,n) coeff=list(map(int,g.coeffs())) assert coeff and min(coeff)>0,(capacity,n,ell,'negative coefficient') d=capacity*(capacity+1)+2-n # Independently check smallest-support linear coefficients (strictness). if n==capacity+1: gd=g.to_dict() for j in range(1,n): e=[0]*n;e[j-1]=1;e[-1]=d-1 assert gd.get(tuple(e),0)==factorial(n)**2*(n*min(ell,j)-ell*j) rec={'capacity':capacity,'support':n,'split':ell,'terms':len(coeff), 'minimum_nonzero':min(coeff),'negative':0,'sha256':digest(g), 'independent_product_check':independent} records.append(rec) print(json.dumps({'capacity':capacity,'support':n,'seconds':round(time.monotonic()-start,3), 'terms':sum(r['terms'] for r in records if r['capacity']==capacity and r['support']==n)}),flush=True) Path(output).write_text(json.dumps({'python':platform.python_version(),'python_flint':flint.__version__, 'records':records},indent=2)+'\n') totals={c:sum(r['terms'] for r in records if r['capacity']==c) for c in range(1,max_capacity+1)} print(json.dumps({'total_kernels':len(records),'total_terms':sum(totals.values()),'by_capacity':totals}),flush=True) if __name__=='__main__': parser=argparse.ArgumentParser() parser.add_argument('--max-capacity',type=int,default=4) parser.add_argument('--output',default='results/certificates.json') args=parser.parse_args() certify(args.max_capacity,args.output)