#!/usr/bin/env python3 """Bounded adaptive Huffman insertion and recursive increment compositions.""" import copy import gzip import hashlib import json from pathlib import Path from analyze_bsp_references import trace_fields from analyze_collision_composition import evaluate,get_word,put_word from analyze_engine_control import control_flow from analyze_qvm import write_json from analyze_vm_extensions import native_functions,verified BASE=0x10000 def run(g,start,stops,m,regs):return evaluate(g,start,stops,m,regs,limit=10000,capture_registers=True) def increment(g,m,obj,node,depth=1): if depth>64:raise ValueError('synthetic increment recursion bound') graph=copy.deepcopy(g) for x in graph['instructions']: if x['opcode']=='pushq':x['opcode']='nop';x['operands']='' rows=graph['instructions'];ends=[x['address'] for x in rows if x['opcode']=='popq'];idx=next(i for i,x in enumerate(rows) if x['opcode']=='callq');call=rows[idx]['address'];start=rows[0]['address'];regs={'%rdi':obj,'%rsi':node};trace=[];maximum=depth;frames=1 while True: t=run(graph,start,ends+[call],m,regs);m=t['memory'];regs=t['registers'];trace+=t['trace'] if t['stop'] in ends:break child=increment(g,m,regs['rdi'],regs['rsi'],depth+1);m=child['memory'];trace+=child['trace'];frames+=child['frames'];maximum=max(maximum,child['maximum_depth']);start=rows[idx+1]['address'] return {'memory':m,'trace':trace,'frames':frames,'maximum_depth':maximum} def state(m,obj=BASE): count=get_word(m,obj);nodes=[] if count>513:raise ValueError('fixture node capacity') for i in range(count): addr=obj+0x830+56*i;nodes.append({'index':i,'address':addr,'left':get_word(m,addr,8),'right':get_word(m,addr+8,8),'parent':get_word(m,addr+16,8),'next':get_word(m,addr+24,8),'previous':get_word(m,addr+32,8),'head_cell':get_word(m,addr+40,8),'weight':get_word(m,addr+48),'symbol':get_word(m,addr+52)}) heads=sorted({n['head_cell'] for n in nodes if n['head_cell']});pool=get_word(m,obj+4) return {'node_count':count,'pointer_cell_count':pool,'root':get_word(m,obj+8,8),'list_start':get_word(m,obj+16,8),'tail_slot':get_word(m,obj+24,8),'free_head':get_word(m,obj+0x828,8),'nodes':nodes,'heads':[[p,get_word(m,p,8)] for p in heads],'pointer_cells':[get_word(m,obj+0xb030+8*i,8) for i in range(pool)],'lookup':[[i,get_word(m,obj+32+8*i,8)] for i in range(257) if get_word(m,obj+32+8*i,8)]} def main(): seeds=json.loads(Path('docs/research/huffman-initialization.json').read_text());out={'native':{},'limitations':'Two Linux ELF64 engines, bounded synthetic adaptive instruction compositions. Initial object bytes supplied from hash-matched prior initialization report; original memset/process/native callees never execute. increment PUSH omitted, POP/RET stopped; recursive callers restored explicitly. Full real million-repetition seed training, actual game codewords/messages, malformed/cyclic/overflow/all-platform/ABI/runtime invariants remain open.'} sequences=[[],[0],[255],[0]*16,[0,1],[0,1]*8,list(range(8)),list(reversed(range(8))),list(range(16))+list(range(16)),[255,0,128,127,1,254]*4,list(range(256))] for label in ['linux--Quake3-UrT-Ded.x86_64','linux--Quake3-UrT.x86_64']: r=json.loads(Path(f'docs/research/native/{label}.json').read_text());verified((Path('.cache/UrbanTerror43')/r['source']['path']).read_bytes(),r['source']['sha256']);a=r['artifacts']['disassembly.txt'];fs=native_functions(verified(gzip.open(a['path'],'rb').read(),a['uncompressed_sha256']).decode());graphs={n:control_flow(fs[n]) for n in ['Huff_addRef','increment']};g=graphs['Huff_addRef'];end=graphs['increment']['instructions'][0]['address'] prior=seeds['native'][label] if prior['source']!=r['source'] or prior['disassembly_sha256']!=a['uncompressed_sha256']:raise ValueError('initialization provenance mismatch') seed=prior['initialization_probes'][0]['objects'][0];memory={BASE+i:0 for i in range(0xc830)} for offset,value in seed['nonzero_bytes']:memory[BASE+offset]=value memory[BASE-1]=memory[BASE+0xc830]=0x7b;records=[] for sequence in sequences: m=dict(memory);trace=[];steps=[];maximum=0;frames=0;dispatch=[] for symbol in sequence: before=get_word(m,BASE+32+8*(symbol&255),8);t=run(g,g['instructions'][0]['address'],[end],m,{'%rdi':BASE,'%rsi':symbol});m=t['memory'];trace+=t['trace'];regs=t['registers'];dispatch.append([symbol,'existing' if before else 'new',regs['rsi']]) c=increment(graphs['increment'],m,regs['rdi'],regs['rsi']);m=c['memory'];trace+=c['trace'];maximum=max(maximum,c['maximum_depth']);frames+=c['frames'] if len(sequence)<=24:steps.append(state(m)) records.append({'symbols':sequence,'dispatch':dispatch,'steps':steps,'final_state':state(m),'increment_frames':frames,'maximum_increment_depth':maximum,'canaries':[m[BASE-1],m[BASE+0xc830]],**trace_fields({'trace':trace},False)}) # Existing-entry normalization is isolated from increment effects. aliases=[] for word in [0,1,255,256,257,0xffffffff]: m=dict(memory);target=BASE+0x830;put_word(m,BASE+32+8*(word&255),target,8);before=dict(m);t=run(g,g['instructions'][0]['address'],[end],m,{'%rdi':BASE,'%rsi':word}) aliases.append({'input_word':word,'normalized_byte':word&255,'increment_node':t['registers']['rsi'],'memory_unchanged':t['memory']==before,**trace_fields(t,True)}) null=increment(graphs['increment'],memory,BASE,0) out['native'][label]={'source':r['source'],'disassembly_sha256':a['uncompressed_sha256'],'seed_report_sha256':hashlib.sha256(Path('docs/research/huffman-initialization.json').read_bytes()).hexdigest(),'graphs':graphs,'sequences':records,'existing_alias_probes':aliases,'null_increment_probe':{'memory_unchanged':null['memory']==memory,**trace_fields(null,True)}} write_json(Path('docs/research/huffman-updates.json'),out);print('Recovered 22 adaptive sequences, twelve byte-normalized dispatch and two null-increment probes') if __name__=='__main__':main()