nmarshall commited on
Commit
bdf6c1f
·
1 Parent(s): d14d999

Recode Huffman updates and derive provisional seeded codebook

Browse files
docs/ASSET_PIPELINE_TODO.md CHANGED
@@ -222,3 +222,5 @@ Encoder follow-up: [supplied-tree symbol streams](research/HUFFMAN_ENCODING.md)
222
  Initialization follow-up: [native frequency tables](research/HUFFMAN_INITIALIZATION.md) preserves exact codec seeds and initial layouts. Recover adaptive training and actual codewords before real network/map/renderer integration; supplied-tree round trips alone remain insufficient (RE-356).
223
 
224
  Adaptive-codec follow-up: [update compositions](research/HUFFMAN_UPDATES.md) validates sampled supplied-tree insertion/count/list/block behavior. Actual seed-trained codebooks, real packets and runtime/platform validation remain required before network/map/renderer integration (RE-360).
 
 
 
222
  Initialization follow-up: [native frequency tables](research/HUFFMAN_INITIALIZATION.md) preserves exact codec seeds and initial layouts. Recover adaptive training and actual codewords before real network/map/renderer integration; supplied-tree round trips alone remain insufficient (RE-356).
223
 
224
  Adaptive-codec follow-up: [update compositions](research/HUFFMAN_UPDATES.md) validates sampled supplied-tree insertion/count/list/block behavior. Actual seed-trained codebooks, real packets and runtime/platform validation remain required before network/map/renderer integration (RE-360).
225
+
226
+ Codebook follow-up: [provisional modeled codewords](research/HUFFMAN_CODEBOOK.md) adds exact native seed training in a differentially checked recoding. Full native/runtime codeword and message/packet validation remains required before network/map/renderer integration (RE-364).
docs/FINDINGS.md CHANGED
@@ -396,3 +396,9 @@ Bounded qagame paths recover bomb-plant/inactivity correction requests, direct f
396
  **F-128 — Observed adaptive count compositions:** repeated/new sequences retain occurrence-count leaf weights, root total and child-weight sums across recursive increment and reorder instructions. Null updates preserve memory; sampled recursion reaches depth eight. Real seeded training/codewords and complete runtime/ABI/platform behavior remain open. [UPDATE-002](research/HUFFMAN_UPDATES.md).
397
 
398
  **F-129 — Observed rank/block reuse:** sampled rank lists cover all nodes with nondecreasing weights and reverse-link consistency; weight blocks share last-rank leader cells. Allocated cells partition into live heads and an acyclic free chain, with fifteen cells in the full-alphabet fixture. Complete swaps, second-object behavior and capacities remain open. [UPDATE-003](research/HUFFMAN_UPDATES.md).
 
 
 
 
 
 
 
396
  **F-128 — Observed adaptive count compositions:** repeated/new sequences retain occurrence-count leaf weights, root total and child-weight sums across recursive increment and reorder instructions. Null updates preserve memory; sampled recursion reaches depth eight. Real seeded training/codewords and complete runtime/ABI/platform behavior remain open. [UPDATE-002](research/HUFFMAN_UPDATES.md).
397
 
398
  **F-129 — Observed rank/block reuse:** sampled rank lists cover all nodes with nondecreasing weights and reverse-link consistency; weight blocks share last-rank leader cells. Allocated cells partition into live heads and an acyclic free chain, with fifteen cells in the full-alphabet fixture. Complete swaps, second-object behavior and capacities remain open. [UPDATE-003](research/HUFFMAN_UPDATES.md).
399
+
400
+ **F-130 — Observed sampled differential equivalence:** compact adaptive recoding matches complete prior and fresh native-instruction tree/list/head/freelist states, exercising tie and swap event classes. This supports the sampled valid-tree contract, not branch-complete/native-runtime conformance. [CODE-001](research/HUFFMAN_CODEBOOK.md).
401
+
402
+ **F-131 — Observed paired-object update samples:** first/second initialized halves match their respective recoding states after every fresh update, with the +24 pointer-slot asymmetry retained. Full initialization/training lifetime and runtime/platform equivalence remain open. [CODE-002](research/HUFFMAN_CODEBOOK.md).
403
+
404
+ **F-132 — Model-derived seeded codebook:** exact native counts produce a provisional 513-node tree, 2–11-bit prefix-free codewords and compatible bounded native-helper symbol round trips against supplied modeled memory. Full native seeded training/codeword/packet conformance remains unverified. [CODE-003](research/HUFFMAN_CODEBOOK.md).
docs/PROVISIONAL_TODO.md CHANGED
@@ -534,3 +534,5 @@ Research progress: [bounded Huffman encoding](research/HUFFMAN_ENCODING.md) comp
534
  Research progress: [Huffman initialization](research/HUFFMAN_INITIALIZATION.md) completes bounded RE-353–355. Broad network/runtime milestones remain open; RE-356 tracks adaptive updates, initialized codewords and actual message/packet/platform fixtures.
535
 
536
  Research progress: [adaptive Huffman updates](research/HUFFMAN_UPDATES.md) completes bounded RE-357–359. Broad network/runtime milestones remain open; RE-360 tracks complete swap/tie paths, second-object equivalence, actual trained codebooks and real packet/platform fixtures.
 
 
 
534
  Research progress: [Huffman initialization](research/HUFFMAN_INITIALIZATION.md) completes bounded RE-353–355. Broad network/runtime milestones remain open; RE-356 tracks adaptive updates, initialized codewords and actual message/packet/platform fixtures.
535
 
536
  Research progress: [adaptive Huffman updates](research/HUFFMAN_UPDATES.md) completes bounded RE-357–359. Broad network/runtime milestones remain open; RE-360 tracks complete swap/tie paths, second-object equivalence, actual trained codebooks and real packet/platform fixtures.
537
+
538
+ Research progress: [differential Huffman codebook](research/HUFFMAN_CODEBOOK.md) completes bounded RE-361–363. Broad network/runtime milestones remain open; RE-364 tracks full native-training/codeword conformance and complete real packet/platform contracts.
docs/RESEARCH_LEDGER.md CHANGED
@@ -221,3 +221,7 @@ Completed bounded RE-353–355 with `analyze_huffman_initialization.py`: eight v
221
  ## 2026-10-11 — Adaptive Huffman insertion and recursive updates
222
 
223
  Completed bounded RE-357–359 with `analyze_huffman_updates.py`: four verified graphs across the Linux ELF64 engines, 22 adaptive sequences, twelve low-byte existing-entry dispatch probes and two null-increment probes. UPDATE-001–003/F-127–129 distinguish byte normalization from transmit's signed lookup, new leaf/internal insertion from reuse, sampled Counter/subtree/rank/head/freelist invariants from exhaustive tie/swap coverage, and actual source-matched initial bytes from full game-seed training. Composed sequences include every byte once, yielding 513 reachable nodes and fifteen allocated pointer cells; recursion reaches depth eight. Prefix states are retained for sequences up to 24 steps, with complete final states for longer cases. increment PUSH omission, stopped POP/RET/CALL boundaries and explicit recursive caller-register restoration are documented; no original process or native callee executes. Eight independent tests verify allocation/lookup/counts, tree/parent/subtree invariants, rank/reverse links, shared leaders, live/free cell partition/reuse, aliases/dispatch, null/recursive traces and paired results. All 326 tests pass; new and preceding reports regenerate byte-identically, and storage/link/whitespace checks pass. RE-356 remains open; RE-360 tracks branch-complete tie/swaps, second-object equivalence, malformed/alias/capacity/wrap states, efficient exact seed-trained codebook reconstruction and actual message/packet/runtime/all-platform contracts.
 
 
 
 
 
221
  ## 2026-10-11 — Adaptive Huffman insertion and recursive updates
222
 
223
  Completed bounded RE-357–359 with `analyze_huffman_updates.py`: four verified graphs across the Linux ELF64 engines, 22 adaptive sequences, twelve low-byte existing-entry dispatch probes and two null-increment probes. UPDATE-001–003/F-127–129 distinguish byte normalization from transmit's signed lookup, new leaf/internal insertion from reuse, sampled Counter/subtree/rank/head/freelist invariants from exhaustive tie/swap coverage, and actual source-matched initial bytes from full game-seed training. Composed sequences include every byte once, yielding 513 reachable nodes and fifteen allocated pointer cells; recursion reaches depth eight. Prefix states are retained for sequences up to 24 steps, with complete final states for longer cases. increment PUSH omission, stopped POP/RET/CALL boundaries and explicit recursive caller-register restoration are documented; no original process or native callee executes. Eight independent tests verify allocation/lookup/counts, tree/parent/subtree invariants, rank/reverse links, shared leaders, live/free cell partition/reuse, aliases/dispatch, null/recursive traces and paired results. All 326 tests pass; new and preceding reports regenerate byte-identically, and storage/link/whitespace checks pass. RE-356 remains open; RE-360 tracks branch-complete tie/swaps, second-object equivalence, malformed/alias/capacity/wrap states, efficient exact seed-trained codebook reconstruction and actual message/packet/runtime/all-platform contracts.
224
+
225
+ ## 2026-10-11 — Differential Huffman recoding and provisional seeded codebook
226
+
227
+ Completed bounded RE-361–363 with `huffman_model.py` and `analyze_huffman_codebook.py`: compact observed update recoding, complete prior-state comparisons (152 retained prefixes plus 22 final cases), sixteen fresh first/second-object cases (288 native-prefix comparisons), exact native-frequency model training and eight native-helper symbol round trips against the modeled tree. CODE-001–003/F-130–132 distinguish sampled state equivalence from branch-complete/runtime equivalence, retained second-object slot asymmetry from normalization, and MODEL-DERIVED seeded trees/codewords from full native training or runtime captures. The 1,053,340-operation model schedule yields 513 nodes, 493 allocated cells (489 live/four free), 257 prefix-free leaf codes with lengths 2–11, and explicit state/codebook/source/report hashes. All 256 model symbol checkpoints validate tree/count/rank/leader/pool invariants. Native transmit/send/receive instruction compositions agree with independent modeled-codeword packing, including every byte value; no original process or native callee executes. Eight tests verify prior/fresh differentials, sampled event classes, modeled training counts/checkpoints, independent tree/codeword/Kraft checks and native-helper bytes/symbol/cursor/canary/paired results. All 334 tests pass; new and preceding reports regenerate byte-identically, and storage/link/whitespace checks pass. RE-360 remains open; RE-364 tracks full native-seed/codeword validation, authoritative/runtime comparison, complete tie/swap/alias/capacity/wrap/platform coverage and actual message/packet/snapshot/renderer contracts.
docs/REVERSE_ENGINEERING_TODO.md CHANGED
@@ -432,3 +432,8 @@ Purpose: turn the supplied installation into an evidence-backed specification of
432
  - [x] RE-358 Compose repeated/new symbol and recursive increment sequences with independent count/tree/subtree-weight invariants ([UPDATE-002](research/HUFFMAN_UPDATES.md)).
433
  - [x] RE-359 Recover sampled rank links, shared block leaders and live/free pointer-cell partition/reuse ([UPDATE-003](research/HUFFMAN_UPDATES.md)).
434
  - [ ] RE-360 Recover branch-complete swap/tie/parent/leader behavior and second-object update equivalence; validate malformed/alias/capacity/wrap states and legal callers, efficiently reconstruct actual seed-trained codebooks with equivalence checks, and compose real message/packet/original-runtime/all-platform fixtures.
 
 
 
 
 
 
432
  - [x] RE-358 Compose repeated/new symbol and recursive increment sequences with independent count/tree/subtree-weight invariants ([UPDATE-002](research/HUFFMAN_UPDATES.md)).
433
  - [x] RE-359 Recover sampled rank links, shared block leaders and live/free pointer-cell partition/reuse ([UPDATE-003](research/HUFFMAN_UPDATES.md)).
434
  - [ ] RE-360 Recover branch-complete swap/tie/parent/leader behavior and second-object update equivalence; validate malformed/alias/capacity/wrap states and legal callers, efficiently reconstruct actual seed-trained codebooks with equivalence checks, and compose real message/packet/original-runtime/all-platform fixtures.
435
+
436
+ - [x] RE-361 Recode observed adaptive updates compactly and compare complete prior/fresh tie-heavy native-instruction states ([CODE-001](research/HUFFMAN_CODEBOOK.md)).
437
+ - [x] RE-362 Compare both initialized object halves after every sampled update, retaining slot asymmetry and relocated pointers ([CODE-002](research/HUFFMAN_CODEBOOK.md)).
438
+ - [x] RE-363 Train the provisional model with exact native seeds, export/check tree and prefix-free codewords, and compose native-helper symbol round trips against that supplied tree ([CODE-003](research/HUFFMAN_CODEBOOK.md)).
439
+ - [ ] RE-364 Validate complete native-seed training/codewords against an independent authoritative implementation or permitted original-runtime captures; prove remaining swap/tie/alias/error/capacity/wrap/platform paths and compose full message/real-packet/overflow/truncation/snapshot/renderer contracts.
docs/research/HUFFMAN_CODEBOOK.md ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Differential Huffman recoding and provisional seeded codebook
2
+
3
+ Reproduce with `python3 scripts/analyze_huffman_codebook.py`; validate with `python3 scripts/test_huffman_codebook.py`. [huffman-codebook.json](huffman-codebook.json) retains source/disassembly/report/model hashes, native graphs, differential checks, modeled training checkpoints, tree/codewords and bounded native-instruction symbol round trips. The compact recoding is [huffman_model.py](../../scripts/huffman_model.py).
4
+
5
+ The codebook is **model-derived**, not runtime captured or produced by evaluating every native training instruction. Differential checks compare complete sampled states against synthetic native instruction compositions, with the same stopped-call/frame/register-restoration limitations as [adaptive updates](HUFFMAN_UPDATES.md). Original processes and native callees never execute. Full branch completeness, native runtime/ABI, other platforms and complete message/packet conformance remain open.
6
+
7
+ ## CODE-001 — Compact update recoding and tie-heavy differentials (RE-361)
8
+
9
+ The provisional model represents nodes, shared leader cells and freelist links directly. It recodes the observed insertion/increment behavior: byte-normalized lookup, NYT insertion, equal-weight next-node leader selection, tree swaps unless the leader is the parent, rank-list swaps and adjacency repair, previous-node leader retention or cell freeing, new weight-block sharing/allocation, recursive parent updates and post-parent adjacency/head repair.
10
+
11
+ Complete model states match all 22 previously recorded native-instruction sequence fixtures, including 152 retained prefix comparisons across both engines and their final states. Four additional sequence patterns exercise alternation, ascending sixteen bytes, repeated boundary-byte mixtures and ascending/reversed eight-byte orders. Each is compared after every update for both initialized objects and both engines: sixteen fresh cases, 288 native-prefix comparisons. Comparisons include all nodes, tree/list/lookup pointers, weights, shared heads, every allocated pointer cell and freelist, rather than just counts or encoded output.
12
+
13
+ Model event counts record new/existing entries, equal-next ties, tree/list swaps, leader-is-parent skips, old-head previous reuse, cell release/reuse, new-head sharing and post-parent adjacency/head changes. The matched sample corpus exercises each of these event classes. These counters belong to the recoding; they are not a branch-complete native coverage report or proof that every malformed/alias/capacity/tie path is equivalent. The model intentionally covers supplied valid byte trees, not general corrupted-object behavior.
14
+
15
+ ## CODE-002 — Both initialized objects (RE-362)
16
+
17
+ Fresh compositions reconstruct the source-matched sparse seed bytes of both object halves. The second starts at first +0xc830 and retains its initially nonzero pointer slot +24. The model retains this slot too, so absolute state comparisons preserve the initializer asymmetry.
18
+
19
+ Within the fresh corpus, the first and second halves generate matching normalized tree/count/rank/cell invariants and model event counts. Every prefix matches its respective native-instruction state with relocated addresses and its own tail-slot value. Outside object canaries remain intact. This establishes sampled update equivalence despite the retained slot difference, not full paired initialization lifetime, million-step native training, concurrent use, or runtime equivalence.
20
+
21
+ ## CODE-003 — Exact seed schedule and provisional codewords (RE-363)
22
+
23
+ The collector re-extracts the 256 frequency words directly from each verified ELF image and checks them against the [frequency report](HUFFMAN_INITIALIZATION.md). It trains the compact first-object model in the exact ascending-symbol repetition order, totaling 1,053,340 add operations. At every completed symbol it records a complete-state hash and validates independent leaf counts, parent/child reachability, internal subtree weights, rank ordering, leader targets and live/free cell partition.
24
+
25
+ The resulting modeled tree has 513 nodes and leaves for symbols 0–256, including NYT 256 with weight zero. Root weight is 1,053,340. It allocates 493 pointer cells, with 489 live and four free at the final checkpoint. These are model outputs, not measurements of the game's running allocator. The exported codebook has lengths from two through eleven bits and SHA-256:
26
+
27
+ `a5a945498ab44f54d98b0b75346e03099e3adb212278f63e170b1a2eb57438c3`
28
+
29
+ That digest uses sorted compact JSON of symbol/bits/length records; it is not the frequency-table hash or a native packet checksum. Bits are listed in root-to-leaf transmission order. The codebook is prefix-free and its full binary leaf set satisfies exact Kraft equality. Tests independently derive every code from the exported tree and verify leaf weights against the actual native frequency table.
30
+
31
+ Eight paired native-instruction encoder/decoder compositions then use this modeled tree as supplied memory. Cases are all 256 bytes, ascending first sixteen at bit seven, and repeated boundary-byte mixtures at bits zero/seven. Transmit/send/receive instructions produce matching symbols, variable codeword lengths/cursors and independent LSB-packed bytes with buffer canaries intact. The buffer is explicitly oversized; no capacity/truncation claim is made. This checks those helpers against the supplied modeled tree, not the accuracy of native full training or complete MSG_WriteBits/MSG_ReadBits/network packets.
32
+
33
+ The exported tree and codebook are research artifacts for subsequent comparison. Do not treat their provisional hash as a compatibility guarantee or replace the original frequency-training contract without validation.
34
+
35
+ ## Validation and next work
36
+
37
+ Eight tests check prior complete-state differential equivalence, fresh first/second-object prefixes and invariants, sampled model event classes, full seed leaf/subtree/reachability/count checks, checkpoint/cell summaries, independent tree-derived prefix-free codewords/Kraft equality, native-helper symbol packing/cursors/canaries and paired helper outputs. No shared evaluator extension was needed.
38
+
39
+ RE-364: prove individual native swap/tie/alias/error/capacity/wrap paths, validate full native-seed training/codewords against an independent authoritative implementation or permitted original-runtime captures, establish all-platform equivalence, and compose complete message writers/readers with actual packets, overflow/truncation and snapshot/renderer integration.
docs/research/HUFFMAN_UPDATES.md CHANGED
@@ -35,3 +35,5 @@ The object-wide outside canaries are preserved for all sequences. This does not
35
  Eight independent tests verify node allocation/lookup/Counter counts, complete tree reachability/parent/subtree weights, rank ordering/reverse links, leader cells, live/free pool partition and reuse, dispatch/low-byte aliases, null/recursive stopped-call behavior and paired results. A fresh isolated root increment additionally verifies its weight change and call-free evaluated trace. No evaluator extension was needed.
36
 
37
  RE-360: audit individual tree/list swaps and parent/leader tie gates with branch-complete fixtures, second-object initialization/update equivalence, malformed/aliased/capacity/wrap states and legal callers; reconstruct actual initialized codebooks efficiently using the exact seed schedule with native-equivalence checks; compose message encoders/decoders and real packets and validate original-runtime/all-platform contracts.
 
 
 
35
  Eight independent tests verify node allocation/lookup/Counter counts, complete tree reachability/parent/subtree weights, rank ordering/reverse links, leader cells, live/free pool partition and reuse, dispatch/low-byte aliases, null/recursive stopped-call behavior and paired results. A fresh isolated root increment additionally verifies its weight change and call-free evaluated trace. No evaluator extension was needed.
36
 
37
  RE-360: audit individual tree/list swaps and parent/leader tie gates with branch-complete fixtures, second-object initialization/update equivalence, malformed/aliased/capacity/wrap states and legal callers; reconstruct actual initialized codebooks efficiently using the exact seed schedule with native-equivalence checks; compose message encoders/decoders and real packets and validate original-runtime/all-platform contracts.
38
+
39
+ Codebook follow-up: [differential recoding and modeled training](HUFFMAN_CODEBOOK.md) checks both object halves and exports provisional seed-trained codewords (RE-361–363). Actual full native training/runtime/platform/packet validation remains open.
docs/research/README.md CHANGED
@@ -135,3 +135,5 @@ The extension collector adds five focused tests for native tail calls, command-r
135
  [Huffman frequencies and initialization](HUFFMAN_INITIALIZATION.md) adds RE-353–355: verified native frequencies, empty-tree layout and bounded paired seed requests. Reproduce with `python3 scripts/analyze_huffman_initialization.py`; check with `python3 scripts/test_huffman_initialization.py`. Adaptive updates, trained codewords and actual message/runtime/platform contracts remain open.
136
 
137
  [Adaptive Huffman update compositions](HUFFMAN_UPDATES.md) adds RE-357–359: insertion/dispatch, recursive count/tree invariants and rank/block/freelist reuse. Reproduce with `python3 scripts/analyze_huffman_updates.py`; check with `python3 scripts/test_huffman_updates.py`. Full branch/tie/second-object/seeded-codebook/message/runtime/platform contracts remain open.
 
 
 
135
  [Huffman frequencies and initialization](HUFFMAN_INITIALIZATION.md) adds RE-353–355: verified native frequencies, empty-tree layout and bounded paired seed requests. Reproduce with `python3 scripts/analyze_huffman_initialization.py`; check with `python3 scripts/test_huffman_initialization.py`. Adaptive updates, trained codewords and actual message/runtime/platform contracts remain open.
136
 
137
  [Adaptive Huffman update compositions](HUFFMAN_UPDATES.md) adds RE-357–359: insertion/dispatch, recursive count/tree invariants and rank/block/freelist reuse. Reproduce with `python3 scripts/analyze_huffman_updates.py`; check with `python3 scripts/test_huffman_updates.py`. Full branch/tie/second-object/seeded-codebook/message/runtime/platform contracts remain open.
138
+
139
+ [Differential recoding and provisional Huffman codebook](HUFFMAN_CODEBOOK.md) adds RE-361–363: full sampled state comparisons, both object halves and modeled seed training/codewords with native-helper symbol compositions. Reproduce with `python3 scripts/analyze_huffman_codebook.py`; check with `python3 scripts/test_huffman_codebook.py`. Native full-training, runtime/platform and complete packet compatibility remain open.
docs/research/huffman-codebook.json ADDED
The diff for this file is too large to render. See raw diff
 
scripts/analyze_huffman_codebook.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Native-fixture differential recoding, second-object checks and modeled seed codebook."""
3
+ from collections import Counter
4
+ import gzip
5
+ import hashlib
6
+ import json
7
+ from pathlib import Path
8
+ import struct
9
+ from analyze_collision_composition import get_word,put_word
10
+ from analyze_engine_control import control_flow
11
+ from analyze_frame_timing import read_address
12
+ from analyze_huffman_updates import increment,run,state
13
+ from analyze_native import elf
14
+ from analyze_qvm import write_json
15
+ from analyze_vm_extensions import native_functions,verified
16
+ from huffman_model import Huffman
17
+ from analyze_huffman_encoding import transmit,DATA,CURSOR,TABLE,OUT
18
+
19
+
20
+ def digest(value):return hashlib.sha256(json.dumps(value,sort_keys=True,separators=(',',':')).encode()).hexdigest()
21
+
22
+
23
+ def invariants(h,counts):
24
+ s=h.state();nodes={x['address']:x for x in s['nodes']};seen=set()
25
+ def walk(addr,parent):
26
+ if addr in seen:raise ValueError('tree cycle')
27
+ seen.add(addr);n=nodes[addr]
28
+ if n['parent']!=parent:raise ValueError('parent mismatch')
29
+ if n['symbol']!=257:
30
+ expected=counts.get(n['symbol'],0)
31
+ if n['weight']!=expected or n['left'] or n['right']:raise ValueError('leaf mismatch')
32
+ return expected
33
+ weight=walk(n['left'],addr)+walk(n['right'],addr)
34
+ if n['weight']!=weight:raise ValueError('subtree mismatch')
35
+ return weight
36
+ total=walk(s['root'],0)
37
+ if total!=sum(counts.values()) or seen!=set(nodes):raise ValueError('tree coverage')
38
+ chain=[];cur=s['list_start'];prev=0
39
+ while cur:
40
+ if cur in chain or nodes[cur]['previous']!=prev:raise ValueError('rank links')
41
+ chain.append(cur);prev=cur;cur=nodes[cur]['next']
42
+ if set(chain)!=seen or [nodes[a]['weight'] for a in chain]!=sorted(nodes[a]['weight'] for a in chain):raise ValueError('rank order')
43
+ heads=dict(s['heads']);pool={0x10000+0xb030+8*i:v for i,v in enumerate(s['pointer_cells'])};free=set();cur=s['free_head']
44
+ while cur:
45
+ if cur in free or cur not in pool:raise ValueError('free chain')
46
+ free.add(cur);cur=pool[cur]
47
+ if set(heads)&free or set(heads)|free!=set(pool):raise ValueError('pool partition')
48
+ for addr in chain:
49
+ node=nodes[addr]
50
+ if node['weight'] and heads[node['head_cell']]!=next(a for a in reversed(chain) if nodes[a]['weight']==node['weight']):raise ValueError('block leader')
51
+ return {'total_weight':total,'nodes':len(nodes),'rank_nodes':len(chain),'live_cells':len(heads),'free_cells':len(free),'allocated_cells':len(pool)}
52
+
53
+
54
+ def main():
55
+ init_path=Path('docs/research/huffman-initialization.json');updates_path=Path('docs/research/huffman-updates.json');init=json.loads(init_path.read_text());updates=json.loads(updates_path.read_text());out={'native':{},'limitations':'Compact recoding is provisional. Differential checks compare complete sampled states to synthetic native instruction compositions, not original executions. Full seed training/codebook is MODEL-DERIVED, not an evaluated million-step native trace or runtime capture. No packet codec/original process/native callee executes. Branch completeness, actual codewords/runtime/ABI and other-platform conformance remain open.'}
56
+ fixtures=[[0,1]*8,list(range(16)),[255,0,128,127]*6,list(range(8))+list(reversed(range(8)))]
57
+ frequency=None;native_graphs={}
58
+ for label in ['linux--Quake3-UrT-Ded.x86_64','linux--Quake3-UrT.x86_64']:
59
+ r=json.loads(Path(f'docs/research/native/{label}.json').read_text());data=verified((Path('.cache/UrbanTerror43')/r['source']['path']).read_bytes(),r['source']['sha256']);meta,_=elf(data);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','send','Huff_offsetTransmit','Huff_offsetReceive']};native_graphs[label]=graphs;p=init['native'][label];u=updates['native'][label]
60
+ if p['source']!=r['source'] or u['source']!=r['source'] or p['disassembly_sha256']!=a['uncompressed_sha256'] or u['disassembly_sha256']!=a['uncompressed_sha256']:raise ValueError('native report provenance')
61
+ raw,_=read_address(data,meta['sections'],p['frequency_table']['address'],1024);counts=list(struct.unpack('<256I',raw))
62
+ if counts!=p['frequency_table']['counts'] or hashlib.sha256(raw).hexdigest()!=p['frequency_table']['sha256']:raise ValueError('frequency mismatch')
63
+ if frequency is not None and frequency!=counts:raise ValueError('paired seeds differ')
64
+ frequency=counts;prior=[]
65
+ for case in u['sequences']:
66
+ h=Huffman();checked=0
67
+ for i,symbol in enumerate(case['symbols']):
68
+ h.add(symbol)
69
+ if case['steps']:
70
+ if h.state()!=case['steps'][i]:raise ValueError('prior prefix mismatch')
71
+ checked+=1
72
+ if h.state()!=case['final_state']:raise ValueError('prior final mismatch')
73
+ prior.append({'symbols':case['symbols'],'checked_prefixes':checked,'final_state_sha256':digest(h.state()),'events':h.events,'invariants':invariants(h,Counter(case['symbols']))})
74
+ differential=[]
75
+ for second in [False,True]:
76
+ base=p['initialization_probes'][0]['base']+int(second)*0xc830;seed=p['initialization_probes'][0]['objects'][int(second)]
77
+ for sequence in fixtures:
78
+ m={base+i:0 for i in range(0xc830)}
79
+ for offset,value in seed['nonzero_bytes']:m[base+offset]=value
80
+ m[base-1]=m[base+0xc830]=0x7b;h=Huffman(second);trace=[];checks=[]
81
+ for i,symbol in enumerate(sequence):
82
+ g=graphs['Huff_addRef'];t=run(g,g['instructions'][0]['address'],[graphs['increment']['instructions'][0]['address']],m,{'%rdi':base,'%rsi':symbol});trace+=t['trace'];c=increment(graphs['increment'],t['memory'],base,t['registers']['rsi']);trace+=c['trace'];m=c['memory'];h.add(symbol);actual=state(m,base)
83
+ if actual!=h.state(base):raise ValueError('fresh differential mismatch')
84
+ checks.append(digest(actual))
85
+ differential.append({'second_object':second,'base':base,'symbols':sequence,'matched_prefix_sha256':checks,'events':h.events,'invariants':invariants(h,Counter(sequence)),'canaries':[m[base-1],m[base+0xc830]],'final_state_sha256':digest(h.state(base)),'trace_length':len(trace),'trace_sha256':digest(trace)})
86
+ out['native'][label]={'source':r['source'],'disassembly_sha256':a['uncompressed_sha256'],'graphs':graphs,'prior_cases':prior,'fresh_differential_cases':differential}
87
+ h=Huffman();checkpoints=[];trained={}
88
+ for symbol,count in enumerate(frequency):
89
+ for _ in range(count):h.add(symbol)
90
+ trained[symbol]=count;checkpoints.append({'through_symbol':symbol,'repetitions':sum(trained.values()),'state_sha256':digest(h.state()),'invariants':invariants(h,trained)})
91
+ words=h.codewords();codebook=[{'symbol':s,'bits':bits,'length':len(bits)} for s,bits in sorted(words.items())]
92
+ out['model_derived_seed_training']={'frequencies':frequency,'repetitions':sum(frequency),'checkpoints':checkpoints,'final_state':h.state(),'final_state_sha256':digest(h.state()),'codebook':codebook,'codebook_sha256':digest(codebook),'events':h.events,'invariants':invariants(h,dict(enumerate(frequency))),'origin':'Compact native-derived model; not runtime captured or full native-instruction training.'}
93
+ roundtrips=[]
94
+ for label,graphs in native_graphs.items():
95
+ for sequence,start in [(list(range(256)),0),(list(range(16)),7),([0,255,128,1]*8,0),([0,255,128,1]*8,7)]:
96
+ st=h.state(TABLE);m={TABLE+i:0 for i in range(0xc830)}
97
+ for slot,value in [(0,st['node_count']),(4,st['pointer_cell_count'])]:put_word(m,TABLE+slot,value)
98
+ for slot,value in [(8,st['root']),(16,st['list_start']),(24,st['tail_slot']),(0x828,st['free_head'])]:put_word(m,TABLE+slot,value,8)
99
+ for sym,ptr in st['lookup']:put_word(m,TABLE+32+8*sym,ptr,8)
100
+ for i,value in enumerate(st['pointer_cells']):put_word(m,TABLE+0xb030+8*i,value,8)
101
+ for node in st['nodes']:
102
+ for slot,key in [(0,'left'),(8,'right'),(16,'parent'),(24,'next'),(32,'previous'),(40,'head_cell')]:put_word(m,node['address']+slot,node[key],8)
103
+ put_word(m,node['address']+48,node['weight']);put_word(m,node['address']+52,node['symbol'])
104
+ for i in range(4096):m[DATA+i]=0
105
+ m[DATA-1]=m[DATA+4096]=0x7b;put_word(m,CURSOR,start);trace=[];end=start
106
+ for symbol in sequence:m,part,_=transmit(graphs,m,symbol,None);trace+=part
107
+ end=get_word(m,CURSOR);encoded=[m[DATA+i] for i in range((end+7)//8)];put_word(m,CURSOR,start);decoded=[];readcursors=[]
108
+ g=graphs['Huff_offsetReceive'];stops=[x['address'] for x in g['instructions'] if x['opcode']=='retq']
109
+ for _ in sequence:
110
+ t=run(g,g['instructions'][0]['address'],stops,m,{'%rdi':st['root'],'%rsi':OUT,'%rdx':DATA,'%rcx':CURSOR});m=t['memory'];trace+=t['trace'];decoded.append(get_word(m,OUT));readcursors.append(get_word(m,CURSOR))
111
+ roundtrips.append({'native_label':label,'symbols':sequence,'initial_bit':start,'encoded_bytes':encoded,'transmit_cursor':end,'decoded_symbols':decoded,'receive_cursors':readcursors,'canaries':[m[DATA-1],m[DATA+4096]],'trace_length':len(trace),'trace_sha256':digest(trace),'tree_origin':'model-derived seed-trained tree, supplied to native instruction compositions'})
112
+ out['modeled_tree_native_symbol_roundtrips']=roundtrips
113
+ out['model_source_sha256']=hashlib.sha256(Path('scripts/huffman_model.py').read_bytes()).hexdigest()
114
+ out['input_report_sha256']={str(path):hashlib.sha256(path.read_bytes()).hexdigest() for path in [init_path,updates_path]};write_json(Path('docs/research/huffman-codebook.json'),out);print('Verified prior/fresh differential states and generated provisional seed-trained model codebook')
115
+
116
+
117
+ if __name__=='__main__':main()
scripts/huffman_model.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Provisional node-level recoding of observed Linux Huff_addRef/increment semantics."""
3
+ class Node:
4
+ __slots__=('index','left','right','parent','next','previous','head','weight','symbol')
5
+ def __init__(self,index,symbol=0,weight=0):
6
+ self.index=index;self.symbol=symbol;self.weight=weight
7
+ self.left=self.right=self.parent=self.next=self.previous=self.head=None
8
+
9
+
10
+ class Huffman:
11
+ def __init__(self,second=False):
12
+ self.nodes=[Node(0,256)];self.root=self.nyt=self.nodes[0];self.lookup={256:self.nyt};self.cells=[];self.free=None;self.second=second;self.events={}
13
+ def event(self,name):self.events[name]=self.events.get(name,0)+1
14
+ def allocate_cell(self,node):
15
+ if self.free is None:i=len(self.cells);self.cells.append(node);self.event('cell_allocate')
16
+ else:i=self.free;self.free=self.cells[i];self.cells[i]=node;self.event('cell_reuse')
17
+ return i
18
+ def free_cell(self,i):self.cells[i]=self.free;self.free=i;self.event('cell_free')
19
+ def swap_tree(self,a,b):
20
+ pa,pb=a.parent,b.parent
21
+ if pa:
22
+ if pa.left is a:pa.left=b
23
+ else:pa.right=b
24
+ else:self.root=b
25
+ if pb:
26
+ if pb.left is b:pb.left=a
27
+ else:pb.right=a
28
+ else:self.root=a
29
+ a.parent,b.parent=pb,pa;self.event('tree_swap')
30
+ def swap_list(self,a,b):
31
+ a.next,b.next=b.next,a.next;a.previous,b.previous=b.previous,a.previous
32
+ if a.next is a:a.next=b
33
+ if b.next is b:b.next=a
34
+ for n in [a,b]:
35
+ if n.next:n.next.previous=n
36
+ if n.previous:n.previous.next=n
37
+ self.event('list_swap')
38
+ def increment(self,node):
39
+ if node is None:self.event('null_increment');return
40
+ if node.next and node.next.weight==node.weight:
41
+ self.event('equal_next');leader=self.cells[node.head]
42
+ if leader is not node.parent:self.swap_tree(leader,node)
43
+ else:self.event('leader_is_parent')
44
+ self.swap_list(leader,node)
45
+ if node.previous and node.previous.weight==node.weight:self.cells[node.head]=node.previous;self.event('old_head_previous')
46
+ else:self.free_cell(node.head)
47
+ node.weight=(node.weight+1)&0xffffffff
48
+ if node.next and node.next.weight==node.weight:node.head=node.next.head;self.event('new_head_shared')
49
+ else:node.head=self.allocate_cell(node)
50
+ if node.parent:
51
+ self.increment(node.parent)
52
+ if node.previous is node.parent:
53
+ self.event('post_parent_adjacent');parent=node.parent;self.swap_list(node,parent)
54
+ if self.cells[node.head] is node:self.cells[node.head]=parent;self.event('post_parent_head')
55
+ def add(self,symbol):
56
+ symbol&=255
57
+ if symbol in self.lookup:self.event('existing');self.increment(self.lookup[symbol]);return
58
+ self.event('new');leaf=Node(len(self.nodes),symbol,1);internal=Node(len(self.nodes)+1,257,1);self.nodes.extend([leaf,internal]);nyt=self.nyt
59
+ internal.next=nyt.next
60
+ if internal.next:
61
+ internal.next.previous=internal
62
+ if internal.next.weight==1:internal.head=internal.next.head
63
+ else:internal.head=self.allocate_cell(internal)
64
+ else:internal.head=self.allocate_cell(internal)
65
+ nyt.next=internal;internal.previous=nyt
66
+ leaf.next=nyt.next;leaf.next.previous=leaf;leaf.head=leaf.next.head;nyt.next=leaf;leaf.previous=nyt
67
+ parent=nyt.parent
68
+ if parent:
69
+ if parent.left is nyt:parent.left=internal
70
+ else:parent.right=internal
71
+ else:self.root=internal
72
+ internal.left=nyt;internal.right=leaf;internal.parent=parent;leaf.parent=nyt.parent=internal;self.lookup[symbol]=leaf;self.increment(parent)
73
+ def codewords(self):
74
+ out={}
75
+ def walk(n,bits):
76
+ if n.symbol!=257:out[n.symbol]=bits;return
77
+ walk(n.left,bits+[0]);walk(n.right,bits+[1])
78
+ walk(self.root,[]);return out
79
+ def state(self,base=0x10000):
80
+ def addr(n):return 0 if n is None else base+0x830+56*n.index
81
+ def cell(i):return 0 if i is None else base+0xb030+8*i
82
+ nodes=[{'index':n.index,'address':addr(n),'left':addr(n.left),'right':addr(n.right),'parent':addr(n.parent),'next':addr(n.next),'previous':addr(n.previous),'head_cell':cell(n.head),'weight':n.weight,'symbol':n.symbol} for n in self.nodes]
83
+ heads=sorted({n.head for n in self.nodes if n.head is not None})
84
+ values=[addr(v) if isinstance(v,Node) else cell(v) for v in self.cells]
85
+ return {'node_count':len(nodes),'pointer_cell_count':len(self.cells),'root':addr(self.root),'list_start':addr(self.nyt),'tail_slot':addr(self.nyt) if self.second else 0,'free_head':cell(self.free),'nodes':nodes,'heads':[[cell(i),addr(self.cells[i])] for i in heads],'pointer_cells':values,'lookup':[[s,addr(n)] for s,n in sorted(self.lookup.items())]}
scripts/test_huffman_codebook.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Differential states, branch events, seeded invariants and independent packing."""
3
+ from collections import Counter
4
+ from fractions import Fraction
5
+ import json
6
+ from pathlib import Path
7
+ import unittest
8
+ from analyze_huffman_codebook import digest
9
+ from huffman_model import Huffman
10
+
11
+
12
+ class HuffmanCodebookTests(unittest.TestCase):
13
+ @classmethod
14
+ def setUpClass(cls):
15
+ cls.r=json.loads(Path('docs/research/huffman-codebook.json').read_text());cls.u=json.loads(Path('docs/research/huffman-updates.json').read_text())
16
+
17
+ def test_recoding_matches_prior_native_complete_states(self):
18
+ for label,n in self.u['native'].items():
19
+ for p in n['sequences']:
20
+ h=Huffman()
21
+ for i,symbol in enumerate(p['symbols']):
22
+ h.add(symbol)
23
+ if p['steps']:self.assertEqual(h.state(),p['steps'][i])
24
+ self.assertEqual(h.state(),p['final_state'])
25
+
26
+ def test_fresh_both_object_prefix_differentials(self):
27
+ for n in self.r['native'].values():
28
+ self.assertEqual(len(n['fresh_differential_cases']),8)
29
+ for p in n['fresh_differential_cases']:
30
+ h=Huffman(p['second_object']);actual=[]
31
+ for symbol in p['symbols']:h.add(symbol);actual.append(digest(h.state(p['base'])))
32
+ self.assertEqual(actual,p['matched_prefix_sha256']);self.assertEqual(digest(h.state(p['base'])),p['final_state_sha256']);self.assertEqual(p['canaries'],[0x7b]*2)
33
+ for a,b in zip(n['fresh_differential_cases'][:4],n['fresh_differential_cases'][4:]):
34
+ self.assertEqual(a['symbols'],b['symbols']);self.assertEqual(a['events'],b['events']);self.assertEqual(a['invariants'],b['invariants'])
35
+
36
+ def test_sampled_tie_swap_and_cell_branches_occur(self):
37
+ for n in self.r['native'].values():
38
+ totals=Counter()
39
+ for p in n['prior_cases']+n['fresh_differential_cases']:totals.update(p['events'])
40
+ for name in ['new','existing','equal_next','tree_swap','leader_is_parent','list_swap','old_head_previous','cell_free','cell_reuse','new_head_shared','post_parent_adjacent','post_parent_head']:self.assertGreater(totals[name],0,name)
41
+
42
+ def test_full_seed_counts_tree_reachability_and_subtree_sums(self):
43
+ t=self.r['model_derived_seed_training'];s=t['final_state'];nodes={n['address']:n for n in s['nodes']};seen=set();counts=t['frequencies']
44
+ def walk(addr,parent):
45
+ self.assertNotIn(addr,seen);seen.add(addr);n=nodes[addr];self.assertEqual(n['parent'],parent)
46
+ if n['symbol']==257:v=walk(n['left'],addr)+walk(n['right'],addr)
47
+ else:v=counts[n['symbol']] if n['symbol']<256 else 0;self.assertEqual([n['left'],n['right']],[0,0])
48
+ self.assertEqual(n['weight'],v);return v
49
+ self.assertEqual(walk(s['root'],0),1053340);self.assertEqual(seen,set(nodes));self.assertEqual(len(nodes),513)
50
+ self.assertEqual(t['repetitions'],1053340);self.assertEqual(t['events']['new'],256);self.assertEqual(t['events']['existing'],1053340-256)
51
+
52
+ def test_training_checkpoint_totals_and_pool_partition_summary(self):
53
+ t=self.r['model_derived_seed_training'];self.assertEqual(len(t['checkpoints']),256)
54
+ for i,p in enumerate(t['checkpoints']):
55
+ self.assertEqual(p['through_symbol'],i);self.assertEqual(p['repetitions'],sum(t['frequencies'][:i+1]));self.assertEqual(p['invariants']['total_weight'],p['repetitions']);self.assertEqual(p['invariants']['nodes'],2*(i+1)+1)
56
+ self.assertEqual(p['invariants']['nodes'],p['invariants']['rank_nodes']);self.assertEqual(p['invariants']['allocated_cells'],p['invariants']['live_cells']+p['invariants']['free_cells'])
57
+ self.assertEqual(t['invariants']['allocated_cells'],493);self.assertEqual(t['invariants']['free_cells'],4)
58
+
59
+ def test_codewords_match_tree_and_are_prefix_free_complete(self):
60
+ t=self.r['model_derived_seed_training'];s=t['final_state'];nodes={n['address']:n for n in s['nodes']};expected={}
61
+ def walk(addr,bits):
62
+ n=nodes[addr]
63
+ if n['symbol']==257:walk(n['left'],bits+[0]);walk(n['right'],bits+[1])
64
+ else:expected[n['symbol']]=bits
65
+ walk(s['root'],[]);words={w['symbol']:w['bits'] for w in t['codebook']};self.assertEqual(words,expected);self.assertEqual(set(words),set(range(257)))
66
+ strings=[''.join(map(str,bits)) for bits in words.values()]
67
+ for i,a in enumerate(strings):
68
+ for j,b in enumerate(strings):
69
+ if i!=j:self.assertFalse(b.startswith(a))
70
+ self.assertEqual(sum(Fraction(1,2**len(bits)) for bits in words.values()),1);self.assertEqual(min(map(len,words.values())),2);self.assertEqual(max(map(len,words.values())),11)
71
+ self.assertEqual(digest(t['codebook']),t['codebook_sha256']);self.assertEqual(digest(s),t['final_state_sha256'])
72
+
73
+ def test_native_symbol_roundtrips_match_independent_codeword_packing(self):
74
+ words={w['symbol']:w['bits'] for w in self.r['model_derived_seed_training']['codebook']};rows=self.r['modeled_tree_native_symbol_roundtrips'];self.assertEqual(len(rows),8)
75
+ for p in rows:
76
+ bits=[bit for symbol in p['symbols'] for bit in words[symbol]];end=p['initial_bit']+len(bits);expected=[0]*((end+7)//8)
77
+ for i,bit in enumerate(bits):position=p['initial_bit']+i;expected[position//8]|=bit<<(position%8)
78
+ self.assertEqual(p['encoded_bytes'],expected);self.assertEqual(p['transmit_cursor'],end);self.assertEqual(p['decoded_symbols'],p['symbols']);self.assertEqual(p['canaries'],[0x7b]*2)
79
+ cursor=p['initial_bit'];expected_cursors=[]
80
+ for symbol in p['symbols']:cursor+=len(words[symbol]);expected_cursors.append(cursor)
81
+ self.assertEqual(p['receive_cursors'],expected_cursors)
82
+
83
+ def test_paired_native_symbol_results_match(self):
84
+ rows=self.r['modeled_tree_native_symbol_roundtrips'];a,b=rows[:4],rows[4:]
85
+ def clean(p):return {k:v for k,v in p.items() if k not in ['native_label','trace_sha256']}
86
+ self.assertEqual([clean(p) for p in a],[clean(p) for p in b])
87
+
88
+
89
+ if __name__=='__main__':unittest.main()