PureOne's picture
Add Nhais-DQ II certified autonomous RSI research release (part 2)
be823b7 verified
Raw History Blame Contribute Delete
3.4 kB
#!/usr/bin/env python3
"""Independent regression/adversarial tests; executable without pytest."""
from pathlib import Path
from itertools import product
from collections import deque
import copy,hashlib,json,random,time
from engine import Improver,Rule,Guard,family,cold_search,ref_eval,shortlex
from verifier import verify,evaluate
ROOT=Path(__file__).resolve().parent
def rehash(p):
d=p['guard'];d['sha256']=hashlib.sha256(json.dumps([d['transition'],d['reject']],separators=(',',':')).encode()).hexdigest()
def normalize(w,laws):
while True:
for law in laws:
for i in range(len(w)-len(law.lhs)+1):
if w[i:i+len(law.lhs)]==law.lhs:
new=w[:i]+law.rhs+w[i+len(law.lhs):]
assert shortlex(new)<shortlex(w);w=new;break
else:continue
break
else:return w
def main():
start=time.perf_counter();rng=random.Random(700091)
paths=sorted((ROOT/'snapshots').glob('*.json'))
certs=[json.loads(p.read_text()) for p in paths]
out={'accepted_snapshots':0,'rejected_false_laws':0,'rejected_forged_dfas':0,
'guard_word_checks':0,'exhaustive_word_checks':0,'identical_bfs_trajectories':0}
for p in certs:
assert verify(p)['accepted'];out['accepted_snapshots']+=1
final=[p for path,p in zip(paths,certs) if path.name.endswith('_g8.json')]
for i in range(512):
p=copy.deepcopy(final[i%len(final)]); maps=p['generators']
for law in p['rules']:
old=law['rhs']
for a in range(len(maps)):
bad=[a]
if evaluate(law['lhs'],maps)!=evaluate(bad,maps):
law['rhs']=bad;break
if law['rhs']!=old:break
assert not verify(p)['accepted'];out['rejected_false_laws']+=1
p=copy.deepcopy(final[i%len(final)]);d=p['guard']
if i%2:
j=rng.randrange(len(d['reject']));d['reject'][j]=not d['reject'][j]
else:
j=rng.randrange(len(d['transition']));a=rng.randrange(len(maps))
d['transition'][j][a]=(d['transition'][j][a]+1)%len(d['transition'])
rehash(p) # The adversary also forges the checksum; logical checks must reject.
assert not verify(p)['accepted'];out['rejected_forged_dfas']+=1
for p in final:
gs=tuple(map(tuple,p['generators']));laws=[Rule(tuple(r['lhs']),tuple(r['rhs'])) for r in p['rules']]
guard=Guard([r.lhs for r in laws],len(gs))
lefts={r.lhs for r in laws}
for j in range(100):
w=tuple(rng.randrange(len(gs)) for _ in range(rng.randrange(50)))
naive=any(w[i:i+len(l)]==l for l in lefts for i in range(len(w)-len(l)+1))
assert (guard.scan(w) is None)==naive;out['guard_word_checks']+=1
a=cold_search(gs,[],500,True)['best'];b=cold_search(gs,laws,500,True)['best']
assert a==b;out['identical_bfs_trajectories']+=1
gs=((1,2,0),(2,0,1),(1,0,2));e=Improver(gs,semantic_dedup=True)
e.advance(1000);e.install(1500)
least={}
for length in range(8):
for w in product(range(3),repeat=length):
sig=ref_eval(w,gs);least.setdefault(sig,w)
nw=normalize(w,e.rules)
assert ref_eval(nw,gs)==sig and e.guard.scan(nw) is not None
assert shortlex(nw)<=shortlex(w);out['exhaustive_word_checks']+=1
assert len(least)==6
for w in least.values():assert e.guard.scan(w) is not None
out['tiny_algebra_semantic_states']=len(least)
out['wall_seconds']=time.perf_counter()-start
out['verified_source_sha256']=hashlib.sha256((ROOT/'verifier.py').read_bytes()).hexdigest()
(ROOT/'results'/'TEST_RESULTS.json').write_text(json.dumps(out,indent=2)+'\n')
print(json.dumps(out,indent=2))
if __name__=='__main__':main()