Skip to content

spike: one walk for records and values (not for merging) - #2043

Draft
coderdan wants to merge 42 commits into
nextfrom
spike/one-sink
Draft

coderdan wants to merge 42 commits into
nextfrom
spike/one-sink

Conversation

@coderdan

@coderdan coderdan commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Not for merging. This draft pull request has the one-sink spike, so you can read its history and check it out. The changes reach next through separate pull requests. #2040 lists them.

Summary

The spike tests the design in #2040. In that design, one sink walks a record or a value once. Every leaf is then sealed in one pass. It shows that the design works, and that it keeps the bytes that are stored today.

The base is the readability refactor in #2042, so this diff shows the spike's commits alone.

What the spike changes

  • The encrypt sink holds no context. One function derives each leaf's additional authenticated data (AAD), the data that is authenticated but not encrypted with the leaf.
  • A value staged for encryption is a Node<Plaintext>. It is sealed by a closure, as it is opened.
  • Cipher is the only cipher trait. AsyncCipher, SealLeaf, Opened, the hlist module, open_all, decrypt_to_frame, encrypt_native, open_native, ContextTag and LeafTypeAad are removed.
  • A record is walked once by a sink built from the plan. The record sink is in tests only, and covers only part of today's record path.
  • vitaminc-normalize is copied into the repository, first unchanged, then changed to use Cipher.
  • vitaminc-aead gets the AES-256-GCM cipher from vitaminc-encrypt, behind an aes feature. Nothing in the repository depends on vitaminc-encrypt 0.10 now.
  • Before, the AES backend freed a block of memory with plaintext still in it. The backend now wipes the plaintext first. The heap tests no longer exclude that free.

Verification

At bfaa79e93, on a Mac:

  • Tests: vitaminc-aead, vitaminc-normalize, recipher-frame and stack-encrypt, all features: 2,265 run, 2,264 pass. The one failure is the UI snapshot that the refactor pull request fixes.
  • vitaminc-aead backends: 404 tests pass on aws-lc-rs and 404 on the RustCrypto backend.
  • Heap tests: all 49 pass, and the EQL heap test passes 12 of 12.
  • Other checks: fmt, clippy with -D warnings, doc examples and rustdoc pass.
  • Go: the five wasm modules build and pass the import check. The guest tests pass, and go test passes for every package, including the tests that open records sealed before the spike.
  • EQL: eql-bindings and the EQL encryption tests pass 408 tests. The 63 tests that need PostgreSQL were not run.
  • Builds: the recipher-js test addon, the EQL timing generator and the three fuzz crates build.

Related

…'s AAD (not for merging)

The sink walks a value into a tree of plaintext leaves. seal_with and
open_with seal and open that tree with a closure, and both derive each
leaf's AAD from its place through map_under. EncryptSink::root(aad) and
Pending stay only because the published vitaminc-normalize 0.10.0 calls
them.
…cipher (not for merging)

encrypt_tree and the target layer share one path: walk, request one key
for each leaf, seal_tree. Opening is open_pending and open_tree.
PendingStackCipherText, MintedKeys, BoundKeys, KeyedLeaf and bind_keys
are removed.
…led in one pass (not for merging)

RecordSink sends each field to the sinks the plan names for it, and
settles each field's context when the walk ends. seal_record makes one
key request and one pass. open_record is the same in reverse.
vitaminc-aead gains an optional sink and a place a caller lends to the
encrypting sink.
…r (not for merging)

EncryptSink<O, E>, EncryptSeq<O, E> and EncryptMap<O, E>: the value
comes first and the error after it, as in Result. The slot has no
default now, because a default cannot come before a parameter without
one.
…rait (not for merging)

The sink's output is vitaminc_aead::pending::Pending, a type of its own
that cannot be built by hand or read. Pending::seal seals it with
anything that implements SealLeaf. Stack Encrypt's DataKeys implements
it, and any Cipher does through a reference. vitaminc-aead's own encrypt
goes through it too.
… merging)

One shape serves a value staged for encryption, sealed, and opened, so
the tree is not a ciphertext. Node<Leaf, P = PassthroughFrame> is the
type. CipherText stays as an alias for code that names it, the published
vitaminc-encrypt included.
…not for merging)

The crate lives in cipherstash/vitaminc and builds against the
vitaminc-aead in this repository. The spike changes vitaminc-aead in ways
that crate has to follow, so its source is copied here and patched in.
Every later commit to packages/vitaminc-normalize is a change the real
crate needs, released from cipherstash/vitaminc.

The source is as published. The manifest gains the test dependency on
vitaminc-aead that publishing removes. One test, known_answers, does not
build at this commit: it names EncryptSink::<()>, and an earlier commit
put the slot before the error.
…ot for merging)

- known_answers names the encrypt sink's parameters in their new order,
  the slot and then the error.
- a_refusal_names_the_field_only_on_request reads the reason from the
  term error. The walk's error has shown a placeholder for the error
  inside it since 8f823d9, which is older than the spike. The
  published test still expected the text.
encrypt_with_terms_async is removed, with the two tests that sealed
through an await and the futures-executor test dependency. Nothing in
this repository called it. This is the change the real crate needs
before vitaminc-aead can drop the trait.
Removed: the AsyncCipher trait with seal_many and open_many,
DEFAULT_CONCURRENCY, seal_all_async, open_all_async, encrypt_async,
decrypt_async, decrypt_into_async, the four async methods of ContextTag,
and the futures-util and futures-executor dependencies.

Nothing outside vitaminc-aead's own tests implemented the trait. Stack
Encrypt fetches its keys first, through target::Pending, and then seals
with the keys in hand (RFC 0002). The tests of a cipher's failure are
kept, against the sync cipher. The tests of batching, ordering and
leaf counts went with the code they tested.

Not changed: the ADR and the changelog in packages/vitaminc-aead still
describe the trait.
…rging)

Only target::Pending is called Pending now.

vitaminc-aead:
- Plaintext is the leaf that is not sealed. Opened is the same type.
- The sink's output is Node<Plaintext>. Node::seal seals it with anything
  that implements SealLeaf, and Node::leaf_count counts its leaves.
- Removed: the Pending struct added earlier in the spike; the old Pending
  leaf, which held a context; EncryptSink::root(aad) and the Under slot;
  seal_all; place, split, split_with_aad, fill and fill_from; LeafCount,
  SealError, OpenError::Count and EncryptError::Count, which only a batch
  could produce.
- plan(value) takes no context. The context is given to seal.

vitaminc-normalize (the copy):
- plan_with_terms(value) takes no context and returns Node<Plaintext>.
- encrypt_with_terms seals through Node::seal.

Stack Encrypt:
- seal_pending and the record sink take and make Node<Plaintext>.

Not changed: the ADR, the README's examples and the changelog in
packages/vitaminc-aead.
… for merging)

SealLeaf was added earlier in this spike. It is gone, and Cipher is the
one cipher trait. Node::seal takes a Cipher.

Stack Encrypt's DataKeys now implements Cipher. Two things come back
with that, both of which the spike had removed:
- Cipher::seal takes &self, and each call uses up one data key, so the
  keys are in a RefCell.
- Cipher also requires open. DataKeys cannot open, so its open always
  fails.

Opening is unchanged: Stack Encrypt opens with open_with and a closure.
…r merging)

Node::seal takes a closure that seals one leaf. The seal and the open
now have one form.

Stack Encrypt has a key for each leaf, so each leaf has its own cipher,
an Aes256Cipher built in seal_leaf. Nothing in Stack Encrypt is a cipher
for a whole tree. DataKeys is removed, with its RefCell and its open
that always failed. seal_tree is back in its place.

Cipher is still the only cipher trait, and only real ciphers implement
it. vitaminc-aead's encrypt passes the cipher's seal as the closure.

The two key streams are named for what they do: sealing keys, which
ZeroKMS generated, and opening keys, which it retrieved.
Nothing outside vitaminc-aead's own tests called it. Its doc named the
Go guest, which does not call it. A caller that wants a frame passes a
FrameWriter to decrypt_into, as the one test that used it now does.
…ing)

Only vitaminc-aead's tests used it. It stays for the unit tests, under
cfg(test). The two integration tests that used it only counted leaves,
and now call Node::leaf_count.
It was one line over open_with, and decrypt_into was its only caller.
decrypt_into now calls open_with with the cipher's open.
Two doc examples still called plan with a context, and one read the old
leaf's plaintext field. They broke at b99c0ea. The test runs until now
did not include doc examples, so nothing reported it.
…ing)

Opened was a second name for Plaintext. Outside vitaminc-aead only
Stack Encrypt's cipher.rs named it, in two places. The other types
called Opened in this repository are local types of their own.
361 lines, behind the hlist feature. No manifest in this repository
turned the feature on, so nothing here built it. It had its own copy of
the rule for a map entry's context.

The published vitaminc-encrypt has an hlist feature of its own that
needs this module. Stack Encrypt still builds, because it leaves that
feature off. Anyone who turns it on against this vitaminc-aead gets an
error, so vitaminc-encrypt has to drop the feature in its next release.
…(not for merging)

Each was one line that called encrypt_opaque or open_opaque. Their three
callers, in target/operations.rs, already pass a context that cannot be
empty, so the rule they stated is kept by the callers' own types.

The module doc of cipher.rs described PendingStackCipherText, MintedKeys
and BoundKeys, which are gone. It now describes the path as it is: one
walk, one request for the keys, one pass with the keys in hand.
ContextTag put a context in a value's type, for vitaminc-aead's own
encrypt and decrypt. Stack Encrypt has its own way to give a value its
context, and no published crate used ContextTag.

- vitaminc-aead: src/context.rs is deleted, 568 lines with its tests.
- Stack Encrypt: the public re-export is removed. This is a change to
  Stack Encrypt's public API.
- Two tests in tests/roundtrip.rs are removed. They passed
  ContextTag::aad as a context. scalar_roundtrips_with_matching_aad and
  decrypt_fails_with_wrong_aad cover the same with a plain context.
LeafTypeAad was an optional context that bound a leaf's expected type.
No code used it. Stack Encrypt checks a value's type after it opens it
and binds no type into a context.

- vitaminc-aead: src/aad/leaf_type.rs is deleted, 548 lines with its
  tests.
- The shared test cases lose their leaf_type_aad section, in both copies
  of value-conformance.json. Only Rust read that section: its README
  said that neither language binding has the feature.
- recipher-frame's conformance test for that section is removed.

Not changed: ADR 0001 of vitaminc-aead, section 3, still lists
LeafTypeAad among the cases the shared set has.

Found while doing this: KnownLogical and UnknownLogicalId are now named
only inside vitaminc-aead.
…::Count (not for merging)

The variant went at b99c0ea, and the link to it broke the rustdoc
build, which this repository runs with warnings as errors. The test runs
until now did not build the rustdoc.
…pt (not for merging)

Copied from the published vitaminc-encrypt 0.10.0, behind a new `aes`
feature, as `vitaminc_aead::aes::{Aes256Cipher, Key}`. It sits next to
the leaf format it writes (`LocalCipherText`, the nonce and the wire
version), which is already in this crate.

Only what this repository uses came across: `Aes256Cipher::new`, its
`Cipher` impl, `Key::from([u8; 32])`, and the two backends (aws-lc-rs on
native targets, RustCrypto's aes-gcm on wasm32 or with the
`_test-rust-crypto-backend` feature). Left behind: `opening_version_1`
and `OpensVersion1` (reading wire version 1 leaves), `Key::wrap` and
`Key::from_wrapped`, `Generatable for Key`, `encrypt_with_key`,
`decrypt_with_key`, `AesCipherText`, and the `hlist` static cipher.
Their tests stayed behind with them.

With aws-lc-rs in the graph, `(): From<_>` has two impls, so three
existing tests of the ciphertext builder name their error type.
…t for merging)

stack-encrypt, the EQL encryption tests and the recipher-js test addon
use `vitaminc_aead::aes` instead, with its `aes` feature on. The
workspace's `vitaminc` umbrella no longer turns on its `encrypt` feature:
nothing here names `vitaminc::encrypt`.

The two heap-hygiene tests exclude the aws-lc seal's free by its new
path, `vitaminc_aead::aes::backend::aws_lc::CipherKey::seal`.

vitaminc-encrypt 0.5.1 is still in the graph, through cts-common and
vitaminc 0.5.1. That is the older line and is not changed here.
…c-aead (not for merging)

`CipherTextBuilder`, `Nonce`, `NonceGenerator`, `RandomNonceGenerator`
and `LocalCipherText::into_reader` were public so that a cipher in
another crate could write and read the leaf layout. The only such cipher
is now `aes::Aes256Cipher`, in this crate, so they are crate-private and
built only with the `aes` feature. `LocalCipherText` and `WIRE_VERSION`
stay public: stack-encrypt stores the leaf, and eql-bindings reads the
version.

`read_version_in`, which accepted older wire versions, is folded into
`read_version`: nothing opens version 1 leaves any more.
`append_target_plaintext_array` had no caller and is removed.
…ging)

They were renamed to `Context` and `ContextPiece` in vitaminc 0.5 and kept
for one transition. The published vitaminc-encrypt was the last thing
that named `Aad`; nothing in the build depends on it now. stack-encrypt's
re-export of both goes with them.
…p tests exclude nothing (not for merging)

aws-lc-rs's `seal_in_place_append_tag` appends the tag's space to the
buffer before it encrypts (aws-lc-rs 1.18.0, `aead/unbound_key.rs:403`).
A buffer with no spare room moves at that point, and the block it leaves
holds the plaintext. The backend now makes the room first and wipes the
old block before it is freed.

Both heap-hygiene tests excluded that free by name, as a fault in a crate
outside this repository. The backend is in vitaminc-aead now, so the
exclusion goes. Before the fix, 14 of stack-encrypt's 49 heap-hygiene
tests fail without the exclusion; with it, all 49 pass.
… types (not for merging)

The nonce types are private now, so the README's nonce example no longer compiled. It names the cipher in the aes module where it named vitaminc-encrypt's.
The spike had not been formatted or linted. Five tests in vitaminc-aead's encrypt sink and vitaminc-normalize's known answers called .err().expect(): clippy flags that when the success type is Debug, which Node<Plaintext> is and the old pending types were not. They call expect_err now.
…tes build against the spike (not for merging)

Each of these is its own Cargo workspace. The five that resolve
vitaminc-normalize patch it to the spike's copy, as the root workspace
does. Their lock files drop vitaminc-encrypt 0.10.

The Go guest's native test sealed a value with
`PendingStackCipherText::plan`, which the spike removed; it calls
`KeysetCipher::encrypt_tree` instead. The recipher-js test addon matched
`EncryptError::Count`, which the spike removed.

Checked: all five Go guest modules build for wasm32-wasip1 and pass the
import-surface check; the guest's tests pass with and without the EQL
types; `go test` passes for every package of the Go module; the EQL
crates' tests pass except the 63 that need PostgreSQL, which were not
run; the test addon, recipher-js and the three fuzz crates build.
@changeset-bot

changeset-bot Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 08d109f

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderabbitai

coderabbitai Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true

…inator (not for merging)

A proof for one target, a ciphertext with its equality term, which the
combinators declare as indexed(Equality).

SinkTarget has a sink, which stages what one walk of the value leaves,
and a finishing step, which is handed the staged parts, the cipher and
one context. The three combinators this target uses each have a form as
a type: zip is the pair (A, B) with Tee as its sink, accepting is
Accepting<A, C>, and map is an impl that calls another target's steps.

What the tests show, against the combinators on the same value:
- the equality term is the same bytes;
- the same number of keys is requested, under the same contexts;
- today's decrypt opens the ciphertext, and another context does not;
- a value with no equality term is refused by both;
- the sink target walks the value once, and the combinators walk it
  twice, once for each operation.

One context for each target is still checked by the compiler. A pair is
a target only when both sides name the same context type. The pair
(StackCipherText, EqualityTerm) does not compile: E0271, the term's
context is CallerContext and the ciphertext's is AeadContext.

Not tried: what the derive emits, a field under a context of its own
(under, extend, context_from), the other terms, decrypt, contexts()
without a run, and the order of the context check. The combinators
check the context before they read the plaintext. Here the walk comes
first, so a refused context drops a staged value.
…xt (not for merging)

The proof grows from one value to a record of two fields: an email with
an equality term under users/email, and an age under users/age.

Under<T, O> is the type form of a.under(own): T's finishing step is
handed the record's context under the context O names. The record's
target is written by hand, as a derive would write it. Its sink is a map
sink that hands each entry to the sink of that field's target, through
vitaminc-aead's Either. Its finishing step runs each field's finishing
step under the one context it was handed. vitaminc-aead's derive
already writes that kind of sink for a struct it decrypts into.

What the tests show, against the combinators on the same struct:
- the equality term is the same bytes;
- the same number of keys is requested, under the same contexts;
- today's decrypt opens each field under its own context, and a field
  does not open under another field's context;
- a frame from a binding goes through the same target as the struct,
  with no field copied first, and gives the same term;
- a record with a missing field or an unknown field is refused.

Not tried: a derive that emits this, a context read from a field of the
record, a passthrough field, nulls, a batch, the other terms, decrypt,
and contexts() without a run.
…rs are (not for merging)

The sink target of the last two commits composed targets with types
and trait impls, so a record needed marker types and an impl written
out by hand. This is the same design with the combinators kept as the
way a declaration is written.

Declaration is the trait: a sink for the one walk of the value, and a
finishing step. ciphertext() and equality() are the two operations.
zip, map, accepting, under, at and record are provided methods, and
each returns a small struct that is a Declaration too, as Iterator's
adapters do. indexed_equality() is composed as today's indexed is:
ciphertext().accepting().zip(equality()).map(..).

at(key) takes the place of project. A field is the entry of that name,
taken as the record is walked, and not a borrow of a struct's field.
record() refuses an entry that no field takes. So what a derive would
write for a record is one expression again:

  indexed_equality().under(users/email).at("email")
      .zip(ciphertext().under(users/age).at("age"))
      .record()
      .map(|(email, age)| EncryptedUser { email, age })

The 15 tests are the sink target's tests on this form, and all pass:
the same term, key requests and contexts as today's combinators, one
walk where today's make two, today's decrypt opens each field, a frame
from a binding goes through the same declaration as a struct, and a
missing field or an unknown field is refused.

The three things this commit set out to check:
- at beside zip works. Each side takes its own entry and ignores the
  rest, through the Option sink. record() counts the entries.
- The return type is impl Declaration<Output = T, Context = C>, for a
  function and for a trait method (Declared::declaration). No type is
  boxed.
- One context for each target is still checked by the compiler:
  ciphertext().zip(equality()) is E0271, at the call to zip.

Not tried: contexts() without a run, a context read from a field, a
passthrough field, nulls, a batch, the other terms, decrypt, the owned
mode's wiping, and the order of the context check.
…(not for merging)

Three more pieces over the Declaration trait:

- clear() keeps a value as a frame, in the clear. It is what a
  passthrough field declares.
- a.within(identity) runs a field under the record's context, then the
  field's identity.
- fields.context_from(key) reads the record's context from the entry
  key, and keeps that entry in the clear.

The context field can be the last entry the walk reaches. So the
context is settled in the finishing step, when the walk is over, as
RecordSink settles it in end.

The tests write the record of plan_with_a_context_field as one
expression, and map its output to the tree a record is stored in:

  indexed_equality().within("email").at("email")
      .zip(ciphertext().within("age").at("age"))
      .context_from("tenant")
      .record()
      .map(..)

Against today's dynamic record path, with the tenant last in the row:
- the equality term is the same bytes;
- today's decrypt opens the record to the same plaintext;
- the record does not open when the stored tenant is changed;
- a record with no tenant is refused.

This is what a plan would build at run time. Nothing here builds a
declaration from a plan yet: the field names are in the test.

Not tried: a plan that builds the declaration, the plan's scopes and
bindings, nulls, a batch, the other terms, decrypt, contexts() without
a run, the owned mode's wiping, and the order of the context check.
…t (not for merging)

The declarations of the last commits took any value that can be walked,
so a plaintext of the wrong type was refused in the walk, at run time.
Today's combinators refuse it at compile time, because an Encryption
names its plaintext type.

The check comes back where the standard library has it for collect:

- EncryptTarget<S> is FromIterator<A>. A target's impl says which
  plaintext types it takes: any walkable S for a ciphertext, S:
  AsRef<str> for a target that takes text.
- plaintext.encrypt_into::<T>(..) is iter.collect::<B>(). It is a
  method on every plaintext, and it asks T: EncryptTarget<Self>.
- field(|user: &User| &user.email) carries the check to a field of a
  record. The closure is never called. It gives the compiler the
  field's type, and the field's target must take that type.

EncryptTarget<S> also says how a plaintext is walked (source). A scalar
is walked as it is. A record names its fields there, so what a derive
would write is the walk of the plaintext struct and the declaration,
both from the same field names. The plaintext struct needs no Encrypt
impl of its own.

Tried by hand, and both are E0277, u32: AsRef<str> is not satisfied:
- 36u32.encrypt_into::<TextOnly, _>(..), at the call;
- a record whose u32 field has the target TextOnly, at field(..).

A frame from a binding still goes through the same declaration, with
run, and its checks are in the walk, as they always were.

Not checked by the compiler: that a field's name in at(..) is the field
the closure names. A derive writes both from one identifier.
A plan is data, so its fields are known only at run time, and zip
cannot join them: each zip makes a new type. Two pieces close that gap.

- Option<A> is a declaration when A is one. Whether a plan's field has
  a ciphertext, an equality term or a clear value is three Options, so
  every field of a plan has one type.
- Fields<A> is the fields of a record, collected from their entries as
  a Vec is collected from an iterator. Its sink gives each entry of the
  map to the field of that name.

encrypt_declared builds the declaration from the plan and runs it:

  plan.fields().iter()
      .map(|field| planned(field)?.under(own).entry(name))
      .collect::<Result<Fields<_>, _>>()?
      .record()
      .try_map(stored_record)

For a plan whose context is a field, under(own) is under_field(identity)
and the fields are followed by context_from(of).

Against today's dynamic path, for a plan with a context field, a plan
with a label and a plan with nothing to seal:
- the record has the same fields and the same outputs;
- the equality term is the same bytes;
- the same number of keys is requested;
- today's decrypt opens the record to the same plaintext.
A field the plan does not name and a missing field are refused.

Two adapters are renamed: at is entry, and within is under_field. Their
names are now owned strings or static ones, so a plan can pass its own.
try_map is added beside map.

Not tried: the plan's scopes and bindings with values, nulls, a batch,
the other terms, decrypt, contexts() without a run, the owned mode's
wiping, and the order of the context check. The kind of an error is not
kept: every refusal here is Error::Aead.
…ing)

json_document(options) is a declaration beside ciphertext() and
equality(). A document is walked as one scalar, its JSON text. The sink
keeps the text. The finishing step parses it and calls the JSON index's
own seal_pending, unchanged: its walk of the document, its terms under
the keyset, one data key, the entries sealed from the leaves up.

Against today's json_document, for one document:
- the selector set is the same;
- the number of entries is the same;
- today's decrypt opens the document to the same value.
A record from a frame, with an indexed email and a JSON field, seals in
one walk. The field's document has today's selectors, today's decrypt
opens it, and the email beside it opens.

The JSON index needs nothing the trait does not have. It needs the
whole document before any keyed work, the keyset, both contexts and the
cipher's randomness, and the finishing step has all four.

The index's own walk derives under the keyset as it goes, so it cannot
be the walk into the sink, which has no key. A sink that took a
document as maps and sequences would stage a tree, and the keyed walk
would run over that tree in the finishing step.

One cost: a typed JsonPlaintext is written to text and parsed again
here. Today's operation reads it in place.

Not tried: the JSON queries, and decrypt of a document.
The document in the test has five nodes. The declaration stores six
entries, one for the root and one for each node, so it is the JSON
tree and not one ciphertext of the whole text.
…for merging)

json_document took JSON text alone. Its sink already keeps whatever is
walked, so the finishing step now reads the document from either form:
JSON text, parsed as before, or a map or a sequence, read from its
structure. Then the JSON index's own walk and seal run, unchanged.

So the sink walk can feed the JSON tree. A Rust struct walked as a map,
and a map of frames from a binding, each give the selectors of the same
document written as JSON, six entries for five nodes, and a document
today's decrypt opens to the same value.

Not tried: the third form, where the sink stages each node's path, kind
and canonical bytes and the finishing step does only the keyed work.
That splits the JSON index's walk (json/derive.rs) into a half with no
key and a half with one. Also not tried: a scalar the JSON model has no
kind for, such as a date or bytes, inside a walked value.
…ging)

Open is a second trait on a declaration. It says how what the
declaration stored is opened again, so a record is declared once for
encrypt and for decrypt.

Opening is the encrypt steps in reverse. The stored target is taken
apart under the one context, and each sealed tree gives its key
requests. With the keys, each tree is opened and its plaintext is
walked into a sink: a frame writer for a binding, or the sink of a Rust
type. A record's fields are written as the entries of one map, so no
field is copied into a value of its own first.

Every adapter that arranges or names things opens what it stored: zip,
accepting, under, entry, record, under_field, context_from, Option and
Fields. map and try_map do not, because a function has no inverse.
convert is map with the inverse beside it, and indexed_equality now
uses it.

A plan builds one declaration for both directions. encrypt_declared
ends it with try_map, to make the stored tree. decrypt_declared takes
the stored tree apart and opens it with the same declaration.

What the tests show, against today's decrypt:
- a ciphertext with its term opens, whether today's combinators or the
  declaration sealed it, with the same key requests and the same
  reported contexts;
- a static record opens into a Rust struct that derives vitaminc's
  Decrypt, and into a frame with no entry for the term;
- a record of a plan opens to today's plaintext with the same number of
  keys, for a plan with a context field, a plan with a label and a plan
  with nothing to seal;
- a changed tenant, another context, and a ciphertext moved to another
  field's place are each refused.

Not tried: opening a JSON document, a stored record with no term (today
a term is ignored, and here its absence is refused), the typed check
that a target opens to a given plaintext type, nulls, a batch, the
other terms, and the kinds of error.
…(not for merging)

A query sends the term an index stores. A field's declaration already
says which index the field has and which context its term is derived
under. Answers<Op> reads the query from it: field.query::<Eq, _>() is
the declaration of the term a query sends for Eq, under the field's own
context. So one declaration of a field seals it, opens it and gives its
query term.

The adapters pass the query through. under and under_field keep the
context. accepting keeps the conversion. convert and entry drop out,
because a query's value is the value itself and not a stored target or
an entry of a record. Option gives None for a field without the index.

Whether a field answers an operator is checked by the compiler. A
second type parameter says where in the declaration the index is, and
the compiler works it out. A ciphertext answers nothing (E0277). An
equality index does not answer Lt (E0277). Two indexes that both answer
Eq do not compile, because the query cannot mean both (E0284).

A function that returns an opaque declaration has to state its query
term's type: Answers<Eq, Right<Here>, Query: Declaration<Output =
EqualityTerm>>. Without that the caller cannot use the term.

What the tests show:
- a query of a static record's field sends the term the record stores,
  and today's query operation gives the same term;
- query_declared, for a plan's field, gives today's query_term, and the
  term the record stores;
- a field with no equality index, and a plan whose context is a field,
  are refused, as today.

Not tried: an operator whose query term is not the stored term (match,
and between with its two terms), the JSON queries, a typed field
reference in place of today's, and the kinds of error.
coderdan added a commit that referenced this pull request Dec 4, 2026
The spike in #2043 walks a record with one sink built from the plan and
seals it in one pass, with no adapter sink in the caller. The plan now
records the decisions from the spike, what it checked, the order of the
pull requests for step 1.4 and the rules each one follows. Questions 1,
9 and 11 have answers, and question 17 asks whether the plan still
implements EncryptTarget.
coderdan added a commit that referenced this pull request Dec 4, 2026
The spike in #2043 showed that a target can be declared once, as a sink
for one walk of the value and a finishing step, and that the one
declaration encrypts the target, decrypts it and gives its query terms.
The combinators become adapters over that trait, as the standard
library's iterator adapters are. This is how the plan's third decision
is built, so the question of whether it still stands is answered.

The plan now has:
- the decisions made during the spike;
- the declaration design, and what the compiler checks;
- what the spike checked for encrypt, decrypt and query, what it did not
  cover, and the four ways it differs from today's code;
- the layout of stack-encrypt and vitaminc-aead: the three calls as
  top-level modules, one place for each index, the order of the
  modules, and the crate root's 29 names;
- the groundwork that can start now: #2042, the AES cipher in #2044,
  vitaminc-normalize and the layout moves;
- step 1.4 as eight pull requests;
- twelve new open questions, and answers to questions 1, 6 and 17.

It replaces the record-sink design of the last commit, which the
declaration design superseded.
Base automatically changed from refactor/dynamic-record-readability to next December 5, 2026 03:40

This branch has not been deployed

No deployments
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.

1 participant