#!/usr/bin/env python3 """Check every new certificate; do not rerun LP search or the old counting census.""" import hashlib import importlib.util import json import math import resource import sys import time from fractions import Fraction from pathlib import Path STARTS = [10007,20011,30011,40009,50021,60013,70001,80021, 90007,100003,110017,120011,130003,140009,150011,160001] QS = [101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193] KERNEL_SHA = '2526ee028e38059ac4507382eb3839fe941dc09044b2641cc084e0b6636ed787' T97 = math.prod(p for p in range(2,98) if all(p%d for d in range(2,math.isqrt(p)+1))) def exact(cert,D): rule = cert['rule'] if rule=='weighted': S,W = cert['slots'],cert['weights'] assert len(S)==len(set(S))==len(W)>0 assert all(type(w) is int and w>0 for w in W) assert set(S)<=set(D) caps = [] for q in QS: # Residue buckets avoid importing the producer's LP matrix or kernel. buckets = [0]*q for s,w in zip(S,W): buckets[s%q] += w caps.append(max(buckets[r]+buckets[(r+2)%q] for r in range(q))) ratio = Fraction(sum(caps),sum(W)) assert ratio<1 and cert['ratio']==[ratio.numerator,ratio.denominator] elif rule=='weighted_exhausted': mu,den = cert['mu'],cert['denominator'] assert type(den) is int and den>0 and set(mu)==set(map(str,QS)) for q in QS: v = mu[str(q)] assert len(v)==q and all(type(x) is int and x>=0 for x in v) assert sum(v)==den cov = [sum(mu[str(q)][(-s)%q]+mu[str(q)][(-s-2)%q] for q in QS) for s in D] ratio = Fraction(min(cov),den) assert ratio>=1 and cert['min_probability']==[ratio.numerator,ratio.denominator] else: assert rule=='none' and cert['kind']=='undecided' return False return True def input_slots(a): out = [] for s in range(a,a+131072): if math.gcd(s,T97)==math.gcd(s+2,T97)==1: out.append(s) if len(out)==72: return out raise AssertionError('input prefix missing') def uniform_capacity(D): caps = [] for q in QS: hist = [0]*q for s in D: hist[s%q] += 1 caps.append(max(hist[r]+hist[(r+2)%q] for r in range(q))) return caps def main(): t,cpu0 = time.monotonic(),time.process_time() resource.setrlimit(resource.RLIMIT_CPU,(40,45)) resource.setrlimit(resource.RLIMIT_FSIZE,(20_000_000,20_000_000)) path = Path(sys.argv[1]) if len(sys.argv)>1 else Path('frontier1052.json') kp = Path(sys.argv[2]) if len(sys.argv)>2 else Path('tightcert-original.py') assert hashlib.sha256(kp.read_bytes()).hexdigest()==KERNEL_SHA spec = importlib.util.spec_from_file_location('kernel1052',kp) tc = importlib.util.module_from_spec(spec);spec.loader.exec_module(tc) x = json.loads(path.read_text()) assert x['job']==1052 and x['p']==97 and x['Q']==QS and x['starts']==STARTS assert x['kernel_sha256']==KERNEL_SHA assert [r['a'] for r in x['rows']]==(STARTS[:1] if x['mode']=='pilot' else STARTS) receipt,counts = [],{'weighted':0,'weighted_exhausted':0,'none':0} for row in x['rows']: D = row['slots'];a = row['a'];assert D==input_slots(a) begin = 55 if a==10007 else 71 assert [t['N'] for t in row['trials']]==list(range(begin,begin-len(row['trials']),-1)) upper,lower = (56 if a==10007 else 72),1 for j,tr in enumerate(row['trials']): n,cert = tr['N'],tr['certificate'];rule = cert['rule'] decisive = exact(cert,D[:n]);counts[rule] += 1 # Also satisfy the assignment's requirement to check the original kernel. assert tc.verify(cert,D[:n],QS)[0] if rule=='weighted': upper = n else: assert j==len(row['trials'])-1 if rule=='weighted_exhausted': lower = n+1 assert row['stop']=='exact_exhaustion' else: assert not decisive and row['stop']=='undecided' assert (lower,upper)==(row['N_lower'],row['N_upper']) assert row['L_upper']==D[upper-1]-a+1 assert row['upper_external']==(upper==(56 if a==10007 else 72)) caps = uniform_capacity(D[:upper]) # This is ONE new comparison at the new weighted frontier, not an NF search. receipt.append({'a':a,'N_lower':lower,'N_upper':upper, 'L_upper':row['L_upper'],'exact_frontier':lower==upper, 'uniform_caps_at_upper':caps,'F1_at_upper':upper-sum(caps)}) trans = x['transfer'] if trans is not None: assert trans['source_a']==10007 and trans['target_N']==51 source = next(t for t in x['rows'][0]['trials'] if t['certificate']['rule']=='weighted_exhausted') assert trans['certificate']==source['certificate'] and trans['source_N']==source['N'] cert = trans['certificate'];mu,den = cert['mu'],cert['denominator'] assert [r['a'] for r in trans['evaluations']]==STARTS for r in trans['evaluations']: D = input_slots(r['a'])[:51] cov = [sum(mu[str(q)][(-s)%q]+mu[str(q)][(-s-2)%q] for q in QS) for s in D] i = cov.index(min(cov));f = Fraction(cov[i],den) assert r['minimum']==[f.numerator,f.denominator] assert r['witness_slot']==D[i] and r['exhausts']==(f>=1) print(json.dumps({'job':1052,'pass':True,'artifact_sha256':hashlib.sha256(path.read_bytes()).hexdigest(), 'certificate_counts':counts,'rows':receipt, 'transfer_new_supports_exhausted':None if trans is None else sum(r['exhausts'] for r in trans['evaluations'][1:])},sort_keys=True,separators=(',',':'))) print(json.dumps({'cpu_seconds':time.process_time()-cpu0,'wall_seconds':time.monotonic()-t,'max_rss_native':resource.getrusage(resource.RUSAGE_SELF).ru_maxrss,'platform':sys.platform},sort_keys=True),file=sys.stderr) if __name__=='__main__': main()