Skip to content

feat(aead): a context exposes its parts as an AadPiece tree, not only its bytes - #318

Open
coderdan wants to merge 1 commit into
mainfrom
aad-pieces
Open

feat(aead): a context exposes its parts as an AadPiece tree, not only its bytes#318
coderdan wants to merge 1 commit into
mainfrom
aad-pieces

Conversation

@coderdan

@coderdan coderdan commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

Closes #317.

What

  • AadPiece<'a>: Text, Bytes, Unsigned { value, width }, Signed { value, width }, List(Vec<AadPiece>). Encodes through IntoAad to the bytes of the value it was built from (List is the PAE of its parts). Display for humans — ("users/email", 7u64) shows as (users/email, 7) — and leaves() walks the parts in encoding order for a consumer that renders or binds them itself. into_owned() detaches the borrow.
  • IntoAadPiece<'a>: into_aad_piece(self) -> AadPiece<'a>, implemented for every type IntoAad is, one for one: &str/StringText; slices, arrays, Vec<u8>, Cow<[u8]>, an Aad, ()Bytes; the ten integers with their width; (A, B) → two-element List; Option<T> → one- or zero-element List; NonEmpty<T> transparent; AadPiece itself.
  • Re-exported from the crate root; a paragraph in the README.

Additive. No encoding changes. A blanket IntoAad for T: IntoAadPiece is ruled out by coherence with the existing concrete impls, so the two traits stay separate and the contract between them is pinned by quickcheck for every built-in: x.into_aad_piece().into_aad() == x.into_aad().

Why

stack-encrypt now sends every leaf's context to ZeroKMS as the data key's descriptor (cipherstash-suite#2180), which ZeroKMS HMACs into the key tag and logs per retrieval. A &str context is readable there; a composite — nonempty!("users/email").with(record_id) — is one PAE blob, so the log shows b64:AgAAAAAAAAALAAAA…. With the parts in hand the descriptor renders users/email/7, and the next ZeroKMS's structured binding ({label, value} per part) maps one to one onto the leaves — from the same value that built the AAD, so the two commit to identical bytes.

Labels for tuple positions are a follow-up (a Labelled<T> wrapper), noted in #317.

https://claude.ai/code/session_019VRX1tXygj5bew3YhyK1B6

…ly its bytes

`IntoAad` hands a consumer the final, PAE-framed byte string of a context
and nothing else — right for the AEAD, useless for a consumer that has to
name what the bytes were built from: a key service logging the field a
data key was issued for, an audit trail, a structured binding built from
the same parts as the AAD. Recovering the parts from the bytes means
parsing PAE heuristically, which is neither injective nor a contract.

`IntoAadPiece` is the second view: the same context as an `AadPiece` tree
(`Text`, `Bytes`, `Unsigned`/`Signed` with their width, `List`) that
encodes through `IntoAad` to exactly the bytes the value does. Every
built-in context type implements both, one for one: `&str`/`String` are
`Text`; slices, arrays, `Vec<u8>`, `Cow<[u8]>`, an `Aad` and `()` are
`Bytes`; the integers keep their width; `(A, B)` is the two-element
`List`, `Option` the one- or zero-element one; `NonEmpty<T>` is
transparent. `Display` renders `("users/email", 7u64)` as
`(users/email, 7)`; `leaves()` walks the parts in encoding order for a
consumer building its own rendering or binding.

Additive. No encoding changes: quickcheck pins
`x.into_aad_piece().into_aad() == x.into_aad()` for every built-in.

Closes #317.

Claude-Session: https://claude.ai/code/session_019VRX1tXygj5bew3YhyK1B6
@github-actions

github-actions Bot commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

🧬 Mutation testing (cargo-mutants, --in-diff)

caught missed unviable timeout
6 0 16 0

✅ Every mutant in the changed lines was caught by a test.

@github-actions

github-actions Bot commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

✅ No CRAP threshold violations

589 function(s) analyzed · threshold 30

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

aead: let a context expose its parts (IntoAadPiece), not only its bytes

1 participant