# XOR-window accounting

This is an exact, synthetic value-payload ledger, not a codec implementation,
benchmark, paper replication or interoperable file format.

Run with Node 22.22.3 or newer:

```sh
node model.mjs
node --test model.test.mjs
```

## Constructed result

Three finite binary64 bit patterns are printed in the JSON output. The first
sets a 64-bit initial state; the second creates a wide XOR residual; the third
has just one changed bit inside that old window.

| Value-payload policy | First | Wide change | Narrow change | Total bits |
| --- | ---: | ---: | ---: | ---: |
| Containment-only reuse | 64 | 77 | 66 | 207 |
| Cost-guarded reuse | 64 | 77 | 14 | 155 |

For an old width W and a newly described width w, reuse costs 2 + W and a
new window costs 13 + w. Containment alone does not choose the cheaper branch.
The guarded model chooses fresh metadata on a tie, matching the decision
in the pinned Beringei appendValue code. Its first-value rule here is normalized
to a raw 64-bit word: these totals are NOT Beringei stream lengths.

Sixty-four identical words cost 127 bits under the first policy and 190 under
the Chimp value rules. Splitting into two independent 32-value blocks raises
those totals to 190 and 252. A local compression win is not a universal ranking.

For a Chimp residual with 16 leading and 7 trailing zero bits, the pinned
encoder takes the tail-trimming branch: 11 + 41 = 52 bits. An already reusable
16-bit leading bucket would permit 2 + 48 = 50 bits. This is a single-decision
counterexample, not an alternative complete codec or a claim about later state.

## Accounting contract

Inputs are exact unsigned 64-bit words. Both models start each block with a
64-bit word. Gorilla-like leading counts are capped at 31. Chimp buckets follow
the pinned encoder; repeated and tail-trimmed values invalidate its reuse state.
No floating-point arithmetic is performed. Tests cover all 2080 possible nonzero
leading/trailing geometries, state boundaries and malformed parameters.

Totals omit timestamp encoding, stream terminators, framing, byte alignment,
checksums, indexes and allocated buffers. Byte size is not bits rounded separately
per value. Full-stream size and decoding fidelity require a real implementation.
A restart also requires appropriate format metadata; this ledger counts only
the chosen first-value convention.

## Primary references

- [Gorilla, PVLDB 8(12), 2015, section 4.1](https://www.vldb.org/pvldb/vol8/p1816-teller.pdf).
- [Chimp, PVLDB 15(11), 2022, sections 3-5](https://www.vldb.org/pvldb/vol15/p3058-liakos.pdf).
- [Beringei appendValue, commit 75c3002](https://github.com/facebookarchive/beringei/blob/75c3002b179d99c8709323d605e7d4b53484035c/beringei/lib/TimeSeriesStream.cpp).
- [Chimp encoder, commit 320d397](https://github.com/panagiotisl/chimp/blob/320d397157c7e0696b3c64dc1711fc17a3add3da/src/main/java/gr/aueb/delorean/chimp/Chimp.java).

Hesela-authored model and tests. Source receipt: [sources.json](sources.json).
