"""Exact finite checks; not a proof-assistant formalization. Run from any directory: python tests/verify_release.py """ from pathlib import Path import sys, json, csv, math, random, time, hashlib from collections import Counter from itertools import permutations, product ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT/'src')) from osec import ErasureSystem, sign, dnf_value, is_subsequence RESULTS=[] def record(name,detail): RESULTS.append({'test':name,'status':'PASS',**detail}) print(f'PASS {name}: {detail}',flush=True) def verify_axioms(): for n in (2,3,4): s=ErasureSystem(n);checks=0 for state in range(s.full+1): for a in s.actions: out=s.erase(state,a) assert out & ~state == 0 assert s.erase(out,a)==out checks+=2 absent=s.full ^ state while absent: bit=absent & -absent assert out & ~s.erase(state|bit,a)==0 absent-=bit;checks+=1 record('operator_axioms',{'n':n,'all_states':s.full+1,'assertions':checks}) def verify_protocols(): rows=[] for n in range(2,9): s=ErasureSystem(n);seen=[set() for _ in range(n+1)];counts=[0]*(n+1) def visit(state,word,left): k=len(word);counts[k]+=1;seen[k].add(state) position={i:j for j,i in enumerate(word)};expected=s.full for i in word:expected &= ~s.marker(0,i) for i in word: for j in word: if position[j]>k&1] terms=[states[k] for k in range(len(pp)) if subset>>k&1] for k in range(4): for word in permutations(range(3),k): state=s.run([(0,i) for i in word])[-1] assert dnf_value(state,terms)==any(is_subsequence(word,p) for p in accepted) record('universal_classifier_and_subsequence_formula',{'n':3,'all_classifiers':64,'partial_words_per_classifier':16}) def verify_guard_optimum(): rows=[] for n in (2,3,4): s=ErasureSystem(n);pp=list(permutations(range(n))) ends=[s.saturated([p]) >> n for p in pp] plus=[a for a,p in zip(ends,pp) if sign(p)==1] minus=[a for a,p in zip(ends,pp) if sign(p)==-1] feasible=[];universal=[];r=n*(n-1) for mask in range(1<=2 data=[] for p in permutations(range(3)): states=[init] for i in p:states.append(s.erase(states[-1],(0,i))) states.append(0);obs=[int(f(x)) for x in states] assert obs==[1,1,1,int(sign(p)==1),0] data.append({'order':''.join('ABC'[i] for i in p),'sign':sign(p), 'cell_counts':[x.bit_count() for x in states],'observation':obs,'extinction':obs.index(0)}) (ROOT/'results/sharp_triad.json').write_text(json.dumps(data,indent=2)+'\n') record('six_cell_sharp_triad',{'complete_orders':6,'initial_cells':6}) def verify_tensor_guard_optimum(): for n,m in [(2,2),(2,3),(3,2)]: s=ErasureSystem(n,m);pp=list(permutations(range(n))) tuples=list(product(pp,repeat=m)) rb=[s.rail(b,i,j) for b in range(m) for i in range(n) for j in range(n) if i!=j] ends=[s.saturated(ps) for ps in tuples] compressed=[sum((1<>k&1) matched=[i for i,state in enumerate(states) if state&mask==mask] if matched and all(signs[i]==1 for i in matched): assert len(matched)==1;implicants[small]=matched[0] if all((small^(1<>j&1):minimal.append(small) assert len(minimal)==math.factorial(n)**m//2 assert all(x.bit_count()==m*(n-1) for x in minimal) for ps,eps,state in zip(tuples,signs,states): if eps==1: chain=s.chain_certificate(ps) assert chain.bit_count()==m*(n-1) and state&chain==chain assert sum(other&chain==chain for other in states)==1 detail={'n':n,'blocks':m,'all_positive_terms_checked':1<>full&1):continue if all(not (table>>x&1) or all(table>>(x|1<>i&1) for x in range(full+1)): funcs.append(table) for table in funcs: hist={1:Counter(),-1:Counter()} for word in permutations(range(n)): state=full;death=n+1 for k,i in enumerate(word,1): state &= ~(1<>state&1:death=k;break hist[sign(word)][death]+=1 assert hist[1]==hist[-1] record('commuting_parity_histogram_no_go',{'n':n,'all_nonconstant_monotone_observers':len(funcs),'orders_per_observer':math.factorial(n)}) def verify_tensor(): for n,m in [(2,2),(2,3),(3,2)]: s=ErasureSystem(n,m);counts=[0]*(s.action_count+1) def visit(state,word,left): k=len(word);counts[k]+=1;rails=s.parity_rails(state) if left:assert rails==(True,True) else: eps=s.induced_sign(word);assert rails==(eps==1,eps==-1) for a in left:visit(s.erase(state,a),word+(a,),tuple(z for z in left if z!=a)) visit(s.full,(),s.actions) record('tensor_all_interleavings',{'n':n,'blocks':m,'partial_protocols':sum(counts),'full_interleavings':counts[-1]}) rng=random.Random(20261007) for n,m in [(3,3),(3,9),(3,27),(4,9),(5,3)]: s=ErasureSystem(n,m) for trial in range(100): word=list(s.actions);rng.shuffle(word);trace=s.run(word) assert all(s.parity_rails(x)==(True,True) for x in trace[:-1]) eps=s.induced_sign(word) assert s.parity_rails(trace[-1])==(eps==1,eps==-1) assert s.parity_rails(0)==(False,False) record('tensor_seeded_larger_cases',{'n':n,'blocks':m,'random_full_interleavings':100,'seed':20261007}) def verify_failures_and_boundaries(): s=ErasureSystem(3);a,b,c=s.actions try:s.run([a,b,a]) except ValueError:pass else:raise AssertionError('Repeated actions must be rejected.') assert s.erase(s.erase(s.full,a),b)!=s.erase(s.erase(s.full,b),a) assert {s.run(w,flush=True)[-1] for w in permutations(s.actions)}=={0} terms=[s.chain_certificate([p]) for p in permutations(range(3)) if sign(p)==1] counterexample=next((x for x in range(s.full+1) if dnf_value(x,terms)!=s.observe(x)),None) assert counterexample is not None for k in range(4): for w in permutations(s.actions,k): x=s.run(w)[-1];assert dnf_value(x,terms)==s.observe(x) record('adversarial_boundaries',{'repetition_guard':'PASS','noncommuting_example':'PASS', 'common_terminal_state':0,'off_protocol_readout_counterexample':counterexample, 'counterexample_tokens':s.describe(counterexample),'chain_DNF':dnf_value(counterexample,terms), 'parity_circuit':s.observe(counterexample)}) def main(): t=time.time();(ROOT/'results').mkdir(exist_ok=True) error=None try: for test in [verify_axioms,verify_protocols,verify_universal,verify_guard_optimum, verify_tensor_guard_optimum,verify_dnf,verify_commuting_no_go, verify_tensor,verify_failures_and_boundaries]:test() except Exception as exc: error=exc RESULTS.append({'test':test.__name__,'status':'FAIL','error':repr(exc)}) report={'release':'MIND-QX OSEC 1.0.0', 'verification_type':'exact finite enumeration plus labelled seeded tests', 'proof_assistant_checked':False,'independent_peer_review':False,'novelty_verified_absent':False, 'test_groups':len(RESULTS),'passed':sum(r['status']=='PASS' for r in RESULTS), 'failed':sum(r['status']=='FAIL' for r in RESULTS), 'elapsed_seconds':round(time.time()-t,3), 'input_sha256':{str(p.relative_to(ROOT)):hashlib.sha256(p.read_bytes()).hexdigest() for p in [ROOT/'src/osec.py',ROOT/'tests/verify_release.py']}, 'tests':RESULTS} (ROOT/'results/verification_report.json').write_text(json.dumps(report,indent=2)+'\n') if error is not None:raise error print(f'ALL {len(RESULTS)} TEST GROUPS PASSED',flush=True) if __name__=='__main__':main()