check_stored.py

back to table · edit · history · where entries came from · files · download

1339 bytes, as of the version from 2026-12-01 14:15 (current). Recorded here, not run.

"""Read-only reproducibility, stored-value and displayed-program checks."""
import sys,json,io,contextlib,runpy
from pathlib import Path
import numberdb.sage as numberdb
from generate import Backhouse
D=Path(__file__).resolve().parent
key=sys.stdin.read().strip()
if '=' in key and key.split('=',1)[0].replace('_','').isalnum():key=key.split('=',1)[1].strip().strip('\"\'')
numberdb.configure(api_key=key)
g=Backhouse();report=g.verify(sample=None)
print('VERIFY',report,flush=True)
assert report.matched==1 and not report.differing
stored=numberdb.table('T893');cached=json.loads((D/'stored-document.json').read_text())
assert stored==cached
# Check the exact stored document through the independent coefficient route.
sys.argv=[str(D/'check_coefficients.py'),str(D/'stored-document.json')]
runpy.run_path(str(D/'check_coefficients.py'),run_name='__main__')
program=stored['Programs']['coefficient-ratio-approximation']['code']
output=io.StringIO()
with contextlib.redirect_stdout(output): exec(program,{})
text=output.getvalue().strip()
from fractions import Fraction as F
value=F(text);ulp=F(1,10**len(text.split('.')[1]))
root=json.loads((D/'root-certificate.json').read_text())
assert value-ulp<=F(root['B'][0])<=F(root['B'][1])<=value+ulp
print('PROGRAM_JSON '+json.dumps({'output':text,'root_enclosure_contained':True}),flush=True)