back to table · edit · history · where entries came from · files · download
6178 bytes, as of the version from 2026-11-27 22:06 (current). Recorded here, not run.
"""Independent exhaustive integer digit lifting; never imports generate.py.
Usage: python3 independent_check.py checkpoint.json check-results.json
Use --stored to read a NumberDB full-document JSON instead of a checkpoint.
"""
import argparse, hashlib, json, math, re, sys
from pathlib import Path
def prime(n):
return n >= 2 and all(n%q for q in range(2, math.isqrt(n)+1))
def squarefree(n):
return all(n%(q*q) for q in range(2, math.isqrt(abs(n))+1))
def precision(p):
n=0
while p**n < 10**100: n+=1
assert p**(n-1) < 10**100 <= p**n
return n
def digit_roots(d,p,n):
"""All roots mod p^n by literal exhaustive extension of each finite root."""
roots=[0];modulus=1
for j in range(n):
newmod=modulus*p
roots=[r+t*modulus for r in roots for t in range(p)
if ((r+t*modulus)**2-d)%newmod==0]
modulus=newmod
if not roots:break
return sorted(roots)
def parse_record(record):
params=record['params'];d,p,k,a=(int(params[s]) for s in ('d','p','k','a'))
m=re.fullmatch(r'(\d+) \+ O\((\d+)\^(\d+)\)',str(record['number']))
if not m:raise ValueError('Noncanonical or missing p-adic precision')
u,base,n=map(int,m.groups())
return d,p,k,a,u,base,n
def validate_one(record,computed):
d,p,k,a,u,base,n=parse_record(record)
assert prime(p) and squarefree(d) and abs(d)>1
assert base==p and n==precision(p) and 0<u<p**n and u%p!=0
assert k==(2 if p==2 else 1) and 0<=a<p**k and u%(p**k)==a
assert (u*u-d)%(p**(n+(p==2)))==0
matching={r%(p**n) for r in computed if r%(p**k)==a}
assert matching=={u}, 'Digit lift disagrees or branch not unique'
def main():
ap=argparse.ArgumentParser();ap.add_argument('input');ap.add_argument('output');ap.add_argument('--stored',action='store_true');args=ap.parse_args()
inp=Path(args.input);j=json.loads(inp.read_text());failures=[]
if args.stored:
# The client's format adapter does not share root arithmetic.
from numberdb._generate import _stored_entries
document=j.get('document',j)
assert list(document['Parameters'])==['d','p','k','a']
records=[{'params':dict(zip(document['Parameters'],identity.split(','))),
'number':value} for identity,value in _stored_entries(document).items()]
else:records=j['records']
known=(digit_roots(1,5,10)==[1,5**10-1] and digit_roots(2,3,8)==[] and
digit_roots(7,3,5)==[68,175] and digit_roots(17,2,8)==[23,105,151,233])
assert known,'Known-answer checker control failed'
primes=[p for p in range(2,30) if prime(p)]
ds=[s*m for m in range(2,31) if squarefree(m) for s in (1,-1)]
expected=[];computed={};census=[]
for d in ds:
for p in primes:
n=precision(p)
if d%p==0:
assert (d//p)%p!=0
roots=[];reason='v_p(d)=1 is odd'
elif p==2 and d%8!=1:
roots=[];reason='odd squares are 1 modulo 8'
else:
roots=digit_roots(d,p,n+(p==2));reason='all finite lifts enumerated'
computed[d,p]=roots
images=sorted(set(r%(p**n) for r in roots))
k=2 if p==2 else 1
residues=sorted(set(r%(p**k) for r in images))
assert len(images)==len(residues)
assert len(images) in (0,2)
expected.extend((d,p,k,a) for a in residues)
census.append({'d':d,'p':p,'N':n,'branches':residues,'finite_roots':len(roots),'reason':reason})
identities=[];paired={};comparisons=[]
for i,r in enumerate(records):
try:
d,p,k,a,u,base,n=parse_record(r);identities.append((d,p,k,a))
validate_one(r,computed[d,p]);paired.setdefault((d,p,n),[]).append(u)
comparisons.append({'params':r['params'],'N':n,'residue_sha256':hashlib.sha256(str(u).encode()).hexdigest(),'match':True})
except Exception as e:failures.append({'entry':i,'error':type(e).__name__+': '+str(e)})
if identities!=expected:failures.append({'selection':'Stored order or identities differ from exhaustive enumeration'})
for (d,p,n),us in paired.items():
if len(us)!=2 or sum(us)%(p**n)!=0:failures.append({'opposite_roots':[d,p,n]})
rejected=[]
for p0 in [2,3]:
r=next(r for r in records if int(r['params']['p'])==p0)
d,p,k,a,u,base,n=parse_record(r)
wrong=dict(r,number='%s + O(%s^%s)'%((u+p**(n-1))%(p**n),p,n))
try:validate_one(wrong,computed[d,p]);rejected.append(False)
except (AssertionError,ValueError):rejected.append(True)
if p==2:
bad=parse_record(wrong)[4]
assert (bad*bad-d)%(2**n)==0 and (bad*bad-d)%(2**(n+1))!=0
r=records[0];wrong=dict(r,params=dict(r['params'],a='0'))
try:validate_one(wrong,computed[int(r['params']['d']),int(r['params']['p'])]);rejected.append(False)
except (AssertionError,ValueError):rejected.append(True)
result={'method':'Exhaustive one-digit integer lift trees, independent of Newton generator',
'input_sha256':hashlib.sha256(inp.read_bytes()).hexdigest(),
'checked_entries':len(records),'total_entries':len(expected),'failures':failures,
'known_answer_control':known,'negative_control_rejected':all(rejected),
'controls':{'known':['x^2-1 at p=5','x^2-2 nonsquare at p=3','x^2-7 modulo 243','x^2-17 modulo 256'],
'negative':['alter final odd-prime digit','two-adic spurious finite solution passes mod 2^N but fails mod 2^(N+1)','wrong branch label'], 'rejected':rejected},
'counts_by_prime':{str(p):sum(t[1]==p for t in expected) for p in primes},
'precision_exponents':{str(p):precision(p) for p in primes},
'census':census,'comparisons':comparisons,'python':sys.version}
Path(args.output).write_text(json.dumps(result,indent=2)+'\n')
print(json.dumps({k:result[k] for k in ['checked_entries','total_entries','failures','known_answer_control','negative_control_rejected','counts_by_prime','precision_exponents']}))
return bool(failures) or not all(rejected)
if __name__=='__main__':sys.exit(main())