Skip to content

verify(storage): extract the key codecs into a dependency-free crate and prove order, injection and range coverage #1244

Backlog

Description

@chappie-daemon

Part of the formal-verification plan in docs/infra/nostream-formal-verification.md (workspace).

Why

A key's byte order is the sort order LMDB and PostgreSQL both seek with. An arithmetic slip in
crates/nostream-storage/src/keys.rs or lmdb/encoding.rs is not a wrong value on screen: it is a
cursor that seeks to the wrong page, a row that disappears after a migration, or a tag query that
silently omits events. hex_ranges builds its bounds in two different shapes depending on whether
the hex criterion has an odd or even length, and extends the key suffix only on the upper bound —
correct today as far as reading it goes, and exactly the reasoning a refactor breaks with no test
failing.

Unit tests sample this. The properties are total, so a bounded model checker can exhaust them.

Scope

Extract into a new crates/nostream-keys (no dependencies; chrono — used only by micros — and
serde_json stay in nostream-storage):

  • keys.rs: i32_key, i32_from_key, i64_key, i64_from_key
  • lmdb/encoding.rs: text_part (with refuse_nul), hex_bytes, hex_digit, hex_ranges,
    tag_ranges, disjoint, replaceable_key, event_header, EVENT_HEADER_LEN, MAX_KEY_LEN

replaceable_key currently takes &Pubkey, &Kind and &[serde_json::Value]; the extracted form
takes ([u8; 32], i32, &[DedupPart]) where DedupPart is Text(&str) | Json(&[u8]), and
nostream-storage keeps the type conversions. nostream-storage depends on the new crate, so
there is no copy to drift — the same rule nostream-scan follows.

Properties to prove

Function Property
i32_key, i64_key order-preserving: a < b ⟺ key(a) < key(b) for all i32/i64, and from_key inverts key for every value
text_part injective over NUL-free inputs; no value can forge the terminator
replaceable_key injective over well-formed inputs (two distinct triples never share a key); every result is <= MAX_KEY_LEN or an error
hex_bytes no panic and no out-of-bounds read for any byte string, including a trailing odd nibble
hex_ranges coverage: for every 32-byte key whose hex prefix matches the criterion, the key lies in [lower, upper] (upper as extended by suffix); both the odd- and even-length branches
tag_ranges the same coverage statement for name ‖ value ‖ created_at ‖ id
disjoint output ranges are pairwise disjoint and their union equals the input's union
event_header decode(encode(h)) == h for every header; any 54-byte value decodes without panic and with every field in range

Each harness gets a documented failing variant, the way nostream-scan/src/proofs.rs does — the
pre-fix ElementEnd is what proved those proofs can fail.

Acceptance

  • cargo kani -p nostream-keys verifies every harness (target: under 2 minutes, the scan
    crate does 5 harnesses in 46 s)
  • nostream-storage calls the extracted crate; no logic left duplicated in encoding.rs
  • the kani-proofs CI job runs both crates — a single step looping over the crates with
    proofs, so the next crate does not need a bespoke job
  • CLAUDE.md states the convention: verified code lives in a zero-dependency crate, the proof
    names its failing variant, and the unwinding budget is in the harness comment

Notes

hex_ranges first: it is the smallest function here with the largest silent-blast radius, and its
odd-length branch is the one a proof earns its keep on.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions