# A matching zlib dictionary checksum can still restore different bytes

## Useful result

A deliberately substituted dictionary produced three wrong bytes in a completed
1,049-byte decode while both the dictionary Adler-32 and the decoded-content
Adler-32 matched. The 45-byte compressed stream was **not modified**. Direct
zlib 1.3 C calls returned Z_NEED_DICT, Z_OK from inflateSetDictionary, then
Z_STREAM_END. Python and Node replay the same frozen stream.

This is a designed counterexample to an acceptance rule, not a newly discovered
zlib defect, a natural collision observation, or an estimate of real-world
corruption risk. The API requires the original dictionary. Our experiment
intentionally violates that requirement to test what its checks can detect.

## What this adds to Hesela

The existing [archive analysis](https://hesela.com/analyses/compressed-archives-dictionary-dependencies/#dictionary-identity)
already demonstrates Zstandard dictionary-ID reuse and wrong output without a
verified content checksum. Its checked control detects that particular mismatch.
The [compression-dictionary record](https://hesela.dev/terms/compression-dictionary/)
also explains LZ4 selection and dictionary lifetime.

The new contribution is narrower: in zlib, a checksum-derived dictionary
identifier and a verified payload checksum can **both** pass the same constructed
dependency substitution. Turning checks on remains necessary, but the two
checks are not an independent proof of dictionary and output identity.
Do not reinterpret the Zstandard/LZ4 observations as tests of zlib.

## Reproduce

Requirements: Python 3 with its zlib module, a C compiler, zlib headers/library
and Node with its built-in zlib. No pip or npm dependencies and no network are
needed to replay the retained fixtures.

From this directory:

```sh
gcc -std=c11 -Wall -Wextra -Werror -O2 verify_native.c -lz -o /tmp/hesela-zlib-native
python3 -m unittest -v
python3 -O -m unittest -v
node replay_node.cjs evidence
python3 observe.py --capture /tmp/hesela-zlib-fresh-capture --native /tmp/hesela-zlib-native
python3 summarize.py
```

The capture directory must not exist. Tests replay frozen inputs; a fresh capture
recompresses them and is a separate operation. Compressor versions may choose
different encodings, so reuse the frozen stream for cross-decoder comparisons.
The C harness handles only bounded trusted fixtures, not arbitrary archives.
Source receipts can be refreshed separately with source_receipts.py.

## Designed data and observed controls

The original dictionary is 1,024 deterministic SHAKE-256-derived bytes, with
offsets 700..702 set to 100,100,100. The alternate changes just those bytes to
101,98,101. Both have Adler-32 0x232eee3b but different SHA-256 digests.

The full payload is the 13-byte prefix record-begin plus a newline, the original
dictionary, and the 12-byte newline/record-end/newline suffix. Both decoded
variants have Adler-32 0xb24bf6ed. The wrong output differs at zero-based offsets
713,714,715. The dictionary and payload checksums are distinct values.

See generated [table.md](table.md) and [summary.json](summary.json). Eight controls
produce 24 Python observations at input chunks of 1,7,65536 bytes, eight direct
C observations and eight Node observations. These are correlated controls, not
40 independent trials. The retained environment is Python 3.12.3 / system
zlib 1.3 and Node v24.22.1 / bundled zlib 1.3.2.1-motley-285e94b. C and Python
use the same system library; Node is a second library build, not an independent
implementation of the compression format.

The wrong-ID dictionary is rejected during dictionary setup. No dictionary
leaves the stream needing one. A second payload includes dictionary bytes
512..701, omitting the third balancing change: the same aliased dictionary is
accepted at setup but the output check rejects it. A damaged trailer rejects
otherwise exact bytes. A truncated trailer never reaches stream completion.

Python's exception API may expose a different prefix before failure depending
on input chunking; the C buffer can retain bytes even on error. Neither emitted
length nor successful cleanup counts as a successful decode. In the C report,
finalResult is the terminal operation status: it is the dictionary-setup error
when setup fails, otherwise the last inflate result. The generated summary
names that operation explicitly.

## Why the two checks can fail together

RFC1950 defines DICTID from the dictionary's Adler-32, and the trailer from
uncompressed content rather than separately including the preset dictionary.
The checksum maintains a byte sum and a sum of cumulative sums modulo 65521.
RFC1951's backward references allow dictionary bytes to become decoded bytes.
[Source map](source-review.md) gives exact sections and pinned implementation.

For three adjacent positions, changes (+1,-2,+1) preserve the byte sum:
1 - 2 + 1 = 0. Their weighted contribution is also zero:
w - 2(w-1) + (w-2) = 0. No byte wraps in this construction.

If decoding copies those changed bytes once, in the same order and positions
relative to each other, the same cancellation survives in the payload checksum.
Adding unchanged prefix/suffix bytes preserves it. This is an algebraic
derivation checked by a separate simple Adler implementation and by native
decoders. Other references, omissions or repetition patterns can break the
cancellation, as the partial-copy control demonstrates.

A naive 2^-32 times 2^-32 estimate is therefore unjustified for this error
mechanism: the check failures are structurally coupled, not independent
uniform random events. We do not assign an alternative probability without a
specified population and corruption process.

## Recovery implication

Keep the exact dictionary bytes, their version and an externally trusted
cryptographic digest in the recovery manifest; also verify intended payload
identity and completion. A digest stored beside mutable data still needs a
trusted reference, and authenticity needs an authenticated trust mechanism.
Hashing only the compressed stream cannot distinguish this witness: its bytes
remain identical while the supplied dependency changes.

Retain ordinary format checks. The rejected controls show that they remain
useful. The lesson is to bind external dependencies and expected output, not
to disable checks or to claim all wrong dictionaries will pass.

## Scope and status

This packet contains synthetic data created by Hesela; no user data or
third-party datasets are redistributed. Primary text is linked and hashed,
not republished. Upstream source is inspected at the resolved zlib v1.3 commit;
we do not claim the distribution binary is byte-identical to that source.

There was no hardware fault injection, crash/power-loss test, throughput study,
adversarial service test, or population sampling. One host and the named library
builds establish the witness, not all possible implementations.

This evidence package accompanies the existing archive-recovery analysis and
compression-dictionary record. The original frozen measurements remain unchanged.
Publication review repaired runtime-only replay rejection and bound generated
stream metadata to the retained bytes. Tests still require the recorded behavior.
A passing replay is scoped to the local versions, not all decoder implementations.
