#!/usr/bin/env python3
import base64,hashlib,json,sys
from pathlib import Path
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey
H=Path(__file__).resolve().parent
sha=lambda b: hashlib.sha256(b if isinstance(b,bytes) else b.encode()).hexdigest()
canon=lambda o: json.dumps(o,sort_keys=True,separators=(',',':'),ensure_ascii=False).encode()
ev=json.loads((H/'E410RR_INDEPENDENT_REPLICATION.json').read_text()); rep=(H/'E408-R_RUN_REPORT.json').read_bytes(); pre=(H/'E408-R_PREREGISTRATION.json').read_bytes(); salts=json.loads((H/'E408-R_SALT_REVEALED.json').read_text())['salts']
body={k:v for k,v in ev.items() if k not in ('signature_b64','public_key_b64','key_fingerprint')}
ok=[]
def c(n,v):
    ok.append(bool(v)); print(('PASS' if v else 'FAIL'),n)
try:
    Ed25519PublicKey.from_public_bytes(base64.b64decode(ev['public_key_b64'])).verify(bytes.fromhex(ev['signature_b64']),canon(body)); c('signature',True)
except Exception: c('signature',False)
c('report hash',sha(rep)==ev['report_sha256']); c('prereg hash',sha(pre)==ev['preregistration_sha256'])
c('salt commitments',all(sha(v.encode())==ev['salt_commitment_sha256'][k] for k,v in salts.items()))
c('primary > 0.99',ev['result']['primary_governed_acc']>0.99); c('replication > 0.99',ev['result']['replication_governed_acc']>0.99)
c('poison governed 0',ev['result']['governed_poison_admitted']==0); c('status',ev['status']=='AGI: NOT ESTABLISHED · RSI: EXPERIMENTAL')
print(f'{sum(ok)}/{len(ok)} checks passed'); print(ev['status']); sys.exit(0 if all(ok) else 1)
