#!/usr/bin/env python3 """Scalar delta boundaries and modeled-tree compressed compositions.""" import gzip import hashlib import itertools import json from pathlib import Path from analyze_bsp_references import global_address, trace_fields from analyze_collision_composition import get_word, put_word from analyze_engine_control import control_flow from analyze_message_byte_codec import MSG, DATA, install_tree from analyze_message_numeric import frame_graph from analyze_message_widths import reader, run, writer from analyze_message_wrappers import WRAPPER_STACK from analyze_qvm import write_json from analyze_vm_extensions import native_functions, verified INTEGER_PAIRS=[(0,0),(0,1),(1,0),(0x1234,0x1234),(0x1234,0xffffffff),(0xffffffff,0)] FLOAT_PAIRS=[(0,0),(0,0x80000000),(0x80000000,0),(0x3f800000,0x3f800000),(0x3f800000,0x3fc00000),(0xbf800000,0x3f800000),(0x3f800000,0),(0x7f7fffff,0x7f7fffff)] def write_delta(graphs,m,kind,baseline,value,width): name='MSG_WriteDeltaFloat' if kind=='float' else 'MSG_WriteDelta';g,omitted=frame_graph(graphs[name]);rows=g['instructions'];calls={x['address']:i for i,x in enumerate(rows) if x['opcode']=='callq'};tails={x['address'] for x in rows if x['opcode']=='jmp' and '' in x['operands']};ends={x['address'] for x in rows if x['opcode']=='retq'} regs={'%rdi':MSG,'%rsi':baseline,'%rdx':value,'%rcx':width&0xffffffff,'%xmm0':baseline,'%xmm1':value,'%rsp':WRAPPER_STACK};pc=rows[0]['address'];requests=[];trace=[] while True: t=run(g,pc,list(calls)+list(tails)+list(ends),m,regs);m=t['memory'];regs=t['registers'];trace+=t['trace'] if t['stop'] in ends:break request=[regs[k] for k in ['rdi','rsi','rdx']];requests.append(request) if request[0]!=MSG:raise ValueError('delta writer pointer') word_width=request[2];signed_width=word_width if word_width<2**31 else word_width-2**32 m,_,error,child=writer(graphs,m,signed_width,request[1]);trace+=child if error:raise ValueError('delta writer fixture error') if t['stop'] in tails:break pc=rows[calls[t['stop']]+1]['address'] if len(requests)>2:raise ValueError('delta writer bound') return m,requests,trace,omitted def read_delta(graphs,m,kind,baseline,width,supplied=None): name='MSG_ReadDeltaFloat' if kind=='float' else 'MSG_ReadDelta';g,omitted=frame_graph(graphs[name]);rows=g['instructions'];calls={x['address']:i for i,x in enumerate(rows) if x['opcode']=='callq'};tails={x['address'] for x in rows if x['opcode']=='jmp' and '' in x['operands']};ends={x['address'] for x in rows if x['opcode']=='retq'} regs={'%rdi':MSG,'%rsi':baseline,'%rdx':width&0xffffffff,'%xmm0':baseline,'%rsp':WRAPPER_STACK};pc=rows[0]['address'];requests=[];trace=[] while True: t=run(g,pc,list(calls)+list(tails)+list(ends),m,regs);m=t['memory'];regs=t['registers'];trace+=t['trace'] if t['stop'] in ends:break request=[regs[k] for k in ['rdi','rsi']];requests.append(request) if request[0]!=MSG:raise ValueError('delta reader pointer') word_width=request[1];signed_width=word_width if word_width<2**31 else word_width-2**32 if supplied is None: m,value,error,child=reader(graphs,m,signed_width);trace+=child if error:raise ValueError('delta reader fixture error') else:value=supplied[len(requests)-1] regs['rax']=value if t['stop'] in tails:break pc=rows[calls[t['stop']]+1]['address'] if len(requests)>2:raise ValueError('delta reader bound') return m,regs['xmm0' if kind=='float' else 'rax']&0xffffffff,requests,trace,omitted def main(): cp=Path('docs/research/huffman-codebook.json');ip=Path('docs/research/huffman-initialization.json');code=json.loads(cp.read_text());initial=json.loads(ip.read_text()) out={'native':{},'codebook_report_sha256':hashlib.sha256(cp.read_bytes()).hexdigest(),'initialization_report_sha256':hashlib.sha256(ip.read_bytes()).hexdigest(),'limitations':'Synthetic Linux ELF64 instruction compositions only; original processes/native callees never execute. Compressed tree is MODEL-DERIVED, not complete native training/runtime capture. Save/restore/PUSH/POP omitted; disjoint virtual frames and caller-register restoration explicit. Float writer comparisons are finite only; signed zeros compare equal. Separate supplied-reader flag/return probes do not compose actual bits or cursor effects. Ample zero-initialized buffers, sampled integer widths and pairs only. No overflow/truncation/key/usercmd/entity/player/snapshot/real-packet/all-platform conformance.'} 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());names=['MSG_WriteDelta','MSG_ReadDelta','MSG_WriteDeltaFloat','MSG_ReadDeltaFloat','MSG_WriteBits','MSG_ReadBits','Huff_putBit','Huff_getBit','Huff_offsetTransmit','Huff_offsetReceive','send'];graphs={n:control_flow(fs[n]) for n in names} if code['native'][label]['source']!=r['source'] or code['native'][label]['disassembly_sha256']!=a['uncompressed_sha256']:raise ValueError('codebook provenance') if initial['native'][label]['source']!=r['source'] or initial['native'][label]['disassembly_sha256']!=a['uncompressed_sha256']:raise ValueError('initialization provenance') template={};base=initial['native'][label]['entry_request']['huff_init_pointer'];state=code['model_derived_seed_training']['final_state'];install_tree(template,state,base);root=install_tree(template,state,base+0xc830) for x in graphs['MSG_ReadBits']['instructions']: if x['opcode']=='movq' and '(%rip)' in x['operands']:put_word(template,global_address(x),root,8) g=graphs['MSG_WriteBits'];old=global_address(next(x for x in g['instructions'] if x['opcode']=='addl' and '(%rip)' in x['operands']));over=global_address(next(x for x in g['instructions'] if x['opcode']=='addl' and '$0x1,' in x['operands'] and '(%rip)' in x['operands'])) streams=[] fixtures=[('integer',w,b,v,bit) for w,(b,v),bit in itertools.product([1,7,8,9,16,24,31,32,-8,-9,-31],INTEGER_PAIRS,[0,7])]+[('float',32,b,v,bit) for (b,v),bit in itertools.product(FLOAT_PAIRS,[0,7])] for kind,width,baseline,value,bit in fixtures: m=dict(template) for i in range(256):m[DATA+i]=0 m[DATA-1]=m[DATA+256]=0x7b for offset,v in [(4,0),(8,0),(16,DATA),(24,256),(28,bit//8+1),(32,0),(36,bit)]:put_word(m,MSG+offset,v,8 if offset==16 else 4) put_word(m,old,0);put_word(m,over,0) m,wrequests,trace,womitted=write_delta(graphs,m,kind,baseline,value,width);write_bit=get_word(m,MSG+36);size=get_word(m,MSG+28);payload=bytes(m[DATA+i] for i in range(size)).hex();account=get_word(m,old);counter=get_word(m,over) put_word(m,MSG+36,bit);put_word(m,MSG+32,0);m,result,rrequests,child,romitted=read_delta(graphs,m,kind,baseline,width);trace+=child streams.append({'kind':kind,'width':width,'baseline_word':baseline,'value_word':value,'initial_bit':bit,'write_requests':wrequests,'read_requests':rrequests,'payload_hex':payload,'write_bit':write_bit,'write_size':size,'read_bit':get_word(m,MSG+36),'read_count':get_word(m,MSG+32),'result_word':result,'oldsize_word':account,'range_counter':counter,'overflow_flag':get_word(m,MSG+4),'canaries':[m[DATA-1],m[DATA+256]],'writer_omitted':womitted,'reader_omitted':romitted,**trace_fields({'trace':trace},False)}) probes=[] for kind,flag,baseline,payload in itertools.product(['integer','float'],[0,1,2,0xffffffff],[0,0x80000000,0x1234],[0x8196a5ff]): _,result,requests,trace,omitted=read_delta(graphs,{},kind,baseline,13,[flag,payload]);probes.append({'kind':kind,'supplied_flag_word':flag,'baseline_word':baseline,'supplied_payload_word':payload,'width':13,'result_word':result,'requests':requests,'omitted':omitted,**trace_fields({'trace':trace},False)}) out['native'][label]={'source':r['source'],'disassembly_sha256':a['uncompressed_sha256'],'graphs':graphs,'compressed_streams':streams,'supplied_reader_probes':probes} write_json(Path('docs/research/message-delta.json'),out) print('Recovered scalar delta flag/payload boundaries and modeled-tree streams') if __name__=='__main__':main()