independent_check.py

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

5982 bytes, as of the version from 2026-11-27 21:16 (current). Recorded here, not run.

"""Independent integer digit-tree checks for T838; no generator imports.

python3 independent_check.py checkpoint.json check-results.json
python3 independent_check.py stored-document.json stored-check-results.json
A full tree enumerates all candidate next digits, unlike modular Newton.
Checks also independently lift x^2-5 and transform its roots, and test known
answers, absent roots, bad digits, wrong branch labels and missing rows.
"""
import copy
import json
from pathlib import Path
import re
import sys


def digit_tree(p, n, coefficients, initial=None):
    def f(x):
        result = 0
        for c in coefficients:
            result = result*x+c
        return result
    roots = [a for a in range(p) if f(a) % p == 0] if initial is None else list(initial)
    modulus = p
    for exponent in range(2, n+1):
        next_modulus = modulus*p
        roots = [u+t*modulus for u in roots for t in range(p)
                 if f(u+t*modulus) % next_modulus == 0]
        modulus = next_modulus
    return sorted(roots)


def primes_through(n):
    return [p for p in range(2,n+1) if all(p%d for d in range(2,p))]


def exponent_for(p):
    n = 1
    while p**n < 10**100:
        n += 1
    return n


def records_from(data):
    if 'entries' in data:
        return [{'params':r['params'],'number':r['number']} for r in data['entries']]
    block=data['Numbers']
    if isinstance(block,list):
        return block
    records=[]
    for p,ks in block.items():
        for k,aas in ks.items():
            for a,v in aas.items():
                records.append({'params':{'p':int(p),'k':int(k),'a':int(a)},'number':v['number'] if isinstance(v,dict) else v})
    return records


def check_records(records):
    expected=[]; prime_census={}; expected_residues={}
    for p in primes_through(29):
        roots=digit_tree(p,2,[1,-1,-1])
        simple=[a for a in range(p) if (a*a-a-1)%p==0 and (2*a-1)%p]
        assert sorted(u%p for u in roots)==simple
        prime_census[str(p)]={'roots_mod_p':[a for a in range(p) if (a*a-a-1)%p==0], 'roots_mod_p2':roots,'simple_branches':simple}
        n=exponent_for(p)
        full=digit_tree(p,n,[1,-1,-1])
        assert len(full)==len(simple)
        for a in simple:
            expected.append((p,1,a))
            branch=[u for u in full if u%p==a]
            assert len(branch)==1
            expected_residues[(p,1,a)]=branch[0]
    observed=[];comparisons=[];by_prime={}
    for r in records:
        assert set(r['params'])=={'p','k','a'}
        p,k,a=(int(r['params'][name]) for name in ['p','k','a'])
        observed.append((p,k,a))
        m=re.fullmatch(r'(\d+) \+ O\((\d+)\^(\d+)\)',r['number'])
        assert m, 'Noncanonical p-adic serialization'
        u,p_value,n=map(int,m.groups());modulus=p**n
        assert p_value==p and n==exponent_for(p)
        assert 0<u<modulus and u%p==a and k==1
        assert (u*u-u-1)%modulus==0 and (2*u-1)%p!=0
        assert (p,k,a) in expected_residues and u==expected_residues[(p,k,a)]
        assert (2*u-1)**2%modulus==5
        by_prime.setdefault(p,[]).append(u)
        comparisons.append({'params':r['params'],'N':n,'modulus':str(modulus),'Newton_stored_residue':str(u),'independent_digit_residue':str(expected_residues[(p,k,a)]),'same_full_modulus':True,'unique_simple_lift':True})
    assert observed==expected, 'Missing, extra, duplicated or reordered branch'
    for p,roots in by_prime.items():
        modulus=p**exponent_for(p)
        assert len(roots)==2 and sum(roots)%modulus==1 and roots[0]*roots[1]%modulus==modulus-1
        square_roots=digit_tree(p,exponent_for(p),[1,0,-5])
        transformed=sorted((1+s)*pow(2,-1,modulus)%modulus for s in square_roots)
        assert sorted(roots)==transformed
    return comparisons,prime_census


def run(data):
    records=records_from(data)
    controls={}
    n=12;m=7**n
    controls['x2_minus_1_both_roots']=digit_tree(7,n,[1,0,-1])==[1,m-1]
    controls['x2_minus_3_no_roots_mod7']=digit_tree(7,2,[1,0,-3])==[]
    controls['golden_mod11_cubed']=digit_tree(11,3,[1,-1,-1])==[37,1295]
    controls['double_root_mod5_cannot_lift']=digit_tree(5,1,[1,-1,-1])==[3] and digit_tree(5,2,[1,-1,-1])==[]
    assert all(controls.values())
    comparisons,census=check_records(records)
    mutations={}
    bad=copy.deepcopy(records);text=bad[0]['number'];match=re.fullmatch(r'(\d+) \+ O\((\d+)\^(\d+)\)',text);u,p,n=map(int,match.groups());bad[0]['number']=str((u+p**(n-1))%(p**n))+' + O('+str(p)+'^'+str(n)+')';mutations['last_padic_digit']=bad
    bad=copy.deepcopy(records);bad[0]['params']['a']=8;mutations['wrong_branch']=bad
    mutations['missing_branch']=records[1:]
    rejected=[]
    for name,bad in mutations.items():
        try:check_records(bad)
        except AssertionError:rejected.append(name)
        else:raise AssertionError('Checker accepted deliberate error: '+name)
    return {'checked_entries':len(records),'total_entries':len(records),'failures':[],'known_answer_control':all(controls.values()),'negative_control_rejected':len(rejected)==len(mutations),'controls':controls,'rejected_mutations':rejected,'comparisons':comparisons,'prime_census':census,'method':'Exhaustive one-digit lift trees, exact modular identities and independent square-root transform; no generator code imported. All branches and all no-root cases at all primes <=29.','limitations':'Builder check evidence only; a fresh critic must independently assess this revision.','skill_manifest':'agents/runs/20261127T210641Z-build-skills/manifest.json'}


if __name__=='__main__':
    data=json.loads(Path(sys.argv[1]).read_text())
    try:result=run(data)
    except Exception as e:
        Path(sys.argv[2]).write_text(json.dumps({'failures':[type(e).__name__+': '+str(e)]},indent=2)+'\n')
        raise
    Path(sys.argv[2]).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']}))