From 573b3a7e816b9f73bc995e24a7b9f32a2397ddc7 Mon Sep 17 00:00:00 2001 From: Dan Draper Date: Tue, 1 Dec 2026 23:27:36 +1100 Subject: [PATCH 01/15] docs(plans): one walk for a record, and host-built stored types A draft plan in two phases. Phase 1 encrypts a record in one walk, adds a term node to the ciphertext tree, and moves the building of a stored type into Go and TypeScript, so the resolver can be removed. Phase 2 returns a query as a tree of terms. --- ...1-one-walk-records-and-host-built-types.md | 623 ++++++++++++++++++ 1 file changed, 623 insertions(+) create mode 100644 docs/plans/2026-12-01-one-walk-records-and-host-built-types.md diff --git a/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md b/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md new file mode 100644 index 000000000..2db3b4e80 --- /dev/null +++ b/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md @@ -0,0 +1,623 @@ +# One walk for a record, and host-built stored types + +Status: draft, 1 December 2026. Nothing in this plan is built. Decisions by +Dan Draper: + +- Encryption works as serde does. Any plaintext structure gives one + `StackCipherText`. A record is a `Map`, and a batch of records is a + `Sequence` of `Map`. +- One walk of a record reproduces what is stored today. No stored format + changes. +- A record's plan implements `EncryptTarget`. The plan builds an interim + step with the combinators, and the cipher seals that step. +- The tree crosses to a language binding in the transport codec. +- Each language defines its stored types, and builds them from the tree with + a deserializer and a visitor. The resolvers are removed. +- A term is a node of its own in the tree. The node contains bytes only. The + map key above the node says what kind of term it is. +- A query comes back as a `StackCipherText` too. That is the second phase. +- The public API may change. + +Stored data fixes one format, so this plan does not choose it. An EQL +value's `c` is the text `stack-encrypt:1:` followed by base64 of the sealed +leaf. Production data has that form. + +Code is cited as it stands on `next` at `54e647124`. + +## Contents + +- [Summary](#summary) +- [Why](#why) +- [The design](#the-design) +- [What was checked](#what-was-checked) +- [Checks to make first](#checks-to-make-first) +- [Phase 1: the record path](#phase-1-the-record-path) +- [Phase 2: queries](#phase-2-queries) +- [What does not change](#what-does-not-change) +- [Checks that must move with the code](#checks-that-must-move-with-the-code) +- [What this changes elsewhere](#what-this-changes-elsewhere) +- [Not in this plan](#not-in-this-plan) +- [Open questions](#open-questions) + +## Summary + +Today Stack Encrypt seals a record one field at a time and builds the +record's tree by hand afterwards. A field that is stored as an EQL type is +formatted in Rust by a resolver, for every language. A query comes back in a +form of its own. + +After this plan: + +- A record is walked once. Each field's ciphertext and terms come from that + walk, and the result is one `StackCipherText`. +- The tree has a node for a term. +- The tree crosses to Go and TypeScript in the transport codec. Each + language reads the tree with one deserializer. The deserializer calls a + visitor. +- A stored type, such as EQL's `TextEq`, is defined in the language that + uses it. Its visitor builds the stored value from the tree. `eql-codegen` + writes these types from the EQL catalog. +- Stack Encrypt has no resolver. Stack Encrypt does not name EQL. The Go SDK + has one encryption guest, not two. +- A query is a tree of terms, read by the same deserializer. + +Rust still does the sealing, the term derivation, and the context binding. +The format of a stored type is written once for each language. + +## Why + +### A record is sealed field by field + +A whole value is already one walk: `StackCipherText` implements +`EncryptTarget` for any source +(`packages/stack-encrypt/src/target/operations.rs:1717`). A record is not +one walk. The dynamic record path builds one operation for each field, and +`shape_record` builds the `Map` by hand. Three costs follow: + +- **The source is copied.** `into_raw_fields` writes each field's value into + a frame of its own, so that each field can be walked alone. +- **Types are erased and recovered.** `lift` boxes each field's output, and + `shape_record` uses `FieldShape` to take the output back out. +- **Terms lose their type.** `shape_record` stores each term as a + passthrough node of bytes (`bytes_node`, + `packages/stack-encrypt/src/dynamic/record.rs:2385`). + +The pieces for one walk exist in `vitaminc-aead`: `Sink`, `Tee` +(`packages/vitaminc-aead/src/plaintext/tee.rs`), `EncryptSink::root` +(`packages/vitaminc-aead/src/encrypt_sink.rs:131`), and `Either` +(`packages/vitaminc-aead/src/typed.rs:856`). The first decision record of +`vitaminc-aead` says "Encrypt-plus-terms is `Tee(EncryptSink, Terms)` with +no special wrapper". Stack Encrypt runs each piece alone, once for each +field. + +### A stored type is built in Rust for every language + +A plan field can name a type such as `TextEq`. A resolver then runs the +type's Rust code and returns finished JSON bytes +(`packages/stack-encrypt/src/dynamic/target.rs:560`, +`packages/eql/crates/eql-bindings/src/encryption/targets.rs:887`). Three +things follow from the resolver: + +- every entry point has a second form that takes a resolver; +- the Go SDK builds, embeds, and releases a second guest with the EQL types; +- Stack Encrypt names EQL. + +A language cannot define a stored type of its own. A language can only name +a type that Rust has. + +### A query has its own output form + +`dynamic::query_term` returns a `QueryTerm` in one of four fixed shapes +(`packages/stack-encrypt/src/dynamic/query.rs:290`). The answer crosses +through serde. Each binding has a second reader for the answer, such as Go's +`parseQueryTerm` (`languages/golang/encrypt/query.go:177`). A query with +more structure than those four shapes has no form to use. + +## The design + +| Part | Where it runs | What it does | +|---|---|---| +| The walk | Rust, `stack-encrypt` | Visits the record once. A record sink gives each field the sinks its plan asks for | +| Sealing | Rust | One key request for the whole call, then one sealed tree | +| The tree | `vitaminc-aead` | `Single`, `Sequence`, `Map`, the two empty markers, `Passthrough`, and a new term node | +| The crossing | `recipher-frame`, with its Go and TypeScript halves | The transport codec. The codec has no stability guarantee, so it can change | +| The deserializer | Go and TypeScript | Reads the transport bytes and calls a visitor for each node | +| The stored type | Go and TypeScript | A visitor that builds the stored value, and a function that returns the sealed leaf to decrypt | + +A stored type is three things in the host language: + +1. The outputs the type asks for. They become the field's entry in the + plan. For `TextEq` the outputs are `c` and `eq`. +2. A visitor that reads the field's map and builds the value. +3. A function that returns the sealed leaf from a stored value, for decrypt. + +The field's context is the plan's, as it is today. The resolver uses the +plan's parts under the field's label +(`packages/eql/crates/eql-bindings/src/encryption/targets.rs:448`). So a +plain field `email` under `users` derives the same term as the EQL type +`TextEq` for `users.email`. A plan with no resolver therefore reads and +writes the data stored today. + +## What was checked + +Two experiments ran. Both are on local branches that are not for merging. + +**One walk of a record.** Four tests, with no change to a trait in +`vitaminc-aead`: + +| Test | Result | +|---|---| +| A field planned in `Tee(EncryptSink, Terms)` under its own root | The equality term is today's term, byte for byte. Today's `record::decrypt` opens the result | +| One walk of a whole record through a record sink | Every field is planned. No field is copied into a frame first. Today's decrypt opens the result | +| One walk of a batch | Each row is planned as its own root, and each row opens alone | +| A control | A field planned under another field's root does not open | + +**Stored types built in Go and TypeScript.** The types are `TextEq`, +`TextMatch`, and their two query types, each written in both languages: + +| Check | Result | +|---|---| +| Go's and TypeScript's stored values against Rust's | The same bytes, once `c` is set aside. `c` is sealed under a new key each time | +| Rust's decryption of a value Go built | The value opens | +| A plan with no EQL type, on a value Rust built | The value opens, from the leaf inside `c` | +| Query values against Rust's | The same bytes, with nothing set aside | +| TypeScript on a tree the Go guest recorded | The same bytes, under the same test seed | +| Every truncation of a recorded tree | Refused in both languages. None panics or throws | + +The Go half needed no change in Rust. A plan field that lists +`outputs: ["c", "eq"]` already comes back as a map with those two keys. + +Neither experiment checked these: + +- The JSON search types, the order terms, and `TextSearch`. +- Null fields, scopes, bindings, and a context field. +- One seal of a tree whose fields have their own key descriptors. +- The conversion of a value to its declared type inside the walk. +- The host-built types on the tree that one walk builds. +- Speed, and fuzzing of the two new deserializers. + +## Checks to make first + +Three checks decide whether step 1.6 can remove the whole resolver. Each +check needs only today's tree and the experiment's deserializer. So the +checks can run before any step below, and steps 1.1 to 1.5 do not wait for +them. + +| Check | Why it is first | +|---|---| +| A JSON search type built in Go or TypeScript | The hard case: 1,050 hand-written lines in `packages/eql/crates/eql-bindings/src/v4/json_tree.rs`. Nobody has looked at what the tree contains for a JSON index today, or whether that is enough to build the stored value | +| The order types: `ob`, `op`, and `between` with its two terms | A second kind of term, and a query of two terms | +| `TextSearch` | Its filter differs from the filter of `TextMatch` | + +Each check is done when the host type builds the bytes Rust builds. If the +JSON check fails, it must report what else has to cross. Step 1.6 then keeps +the resolver for the JSON types until that is settled. + +## Phase 1: the record path + +| Step | What | Depends on | +|---:|---|---| +| 1.1 | Go's plan carries match options | Nothing | +| 1.2 | The tree codec takes any leaf | Nothing | +| 1.3 | A term node, and a length on a passthrough node | #2003, 1.2 | +| 1.4 | One walk of a record | Nothing. The walk writes term nodes once 1.3 is in | +| 1.5 | A deserializer and a visitor in Go and TypeScript | #2003, 1.3 | +| 1.6 | The stored types in each language, and the resolver removed | 1.1, 1.5, the checks above | + +PR #2003 moves vitaminc's Go codec into this repository as +`languages/golang/internal/frame`. Every Go step here is written against +that package. vitaminc's `vcencrypt` is to be deleted. After that, nothing +outside this repository reads the tree. + +### 1.1 Go's plan carries match options + +The match options decide the filter, and the tree does not record them. EQL +fixes its own: 3-grams, downcase, short words, `k` = 6, and `m` = 2048 +(`packages/eql/crates/eql-bindings/src/encryption.rs:398`). A bare `"match"` +in a plan means `k` = 3 and `m` = 256. For the text "Ada Lovelace" that is +59 positions against 27. So the type that reads a filter must be the type +that asks for it. + +Rust's plan accepts the options in an object form +(`packages/stack-encrypt/src/dynamic/record.rs:1206`). Go's plan cannot send +them. `encrypt.Match` takes options and drops them +(`languages/golang/encrypt/term.go:63`). `Plan.Wire()` writes each output as +a string (`languages/golang/internal/record/record.go:544`). + +- Go's plan model keeps the options and writes the object form. +- The comment on `encrypt.Match` says the guest derives a query under the + defaults only. The experiment showed that the query call uses the plan's + options. The comment is corrected. + +Issue #1673 proposes one analyzer pipeline in place of the fixed option +struct. This step sends whatever form the plan has when the step is built. + +Done when a Go plan with EQL's options gives the filter Rust gives, under +one test seed, with no test-only call. + +### 1.2 The tree codec takes any leaf + +`encode_ciphertext` asks for `Leaf: AsRef<[u8]>` +(`packages/recipher-frame/src/transport.rs:200`). A `SealedValue` is a +struct of four fields. So the Go guest first copies the whole tree into one +whose leaves are byte strings (`map_leaves`, +`languages/golang/encrypt/guest/src/ops.rs:702`). The guest also works out +the encoded length a second time (`:743`). + +| Part | Today | After | +|---|---|---| +| Leaf bound | `AsRef<[u8]>` | A trait with two methods: the leaf's length, and writing the leaf. Every `AsRef<[u8]>` type has the trait, so callers compile unchanged | +| `SealedValue` | Copied to bytes in a second tree | Writes its four fields into the output | +| Encoded length | Worked out again in the guest | Exported by the codec | +| Decode | Leaves as byte strings, then converted in a second step | The caller gives a function that reads each leaf | + +A prototype exists. The codec's tests and the cipher module's tests pass +with the prototype, and the bytes are unchanged. + +One part needs design. In the prototype, a leaf that the caller's function +refuses comes back as the codec's single error. The guest reports the leaf's +own error today (the `leaf` class in +`conformance/stack-encrypt/raw-frames-contract.md`). The real version needs +an error with two cases. + +Done when the Go guest has no `map_leaves` and no length function of its +own, and the raw-frame conformance cases pass unchanged. + +### 1.3 A term node, and a passthrough length + +**The term node.** `CipherText` +(`packages/vitaminc-aead/src/container.rs:32`) has a new variant for a +term's bytes. In the transport codec a term is a new kind, `0x08`. That is +the next free kind. A `u32` length and the bytes follow the kind. + +The node does not record what kind of term it is. The map key above the +node, such as `eq`, says that. This is the rule today (`IndexSpec::key`, +`packages/stack-encrypt/src/target/index.rs:127`). + +What the node gives every reader: + +- A term stops looking like plaintext. Today a term and a passthrough field + are the same kind of node. Only a passthrough can contain plaintext. +- Decrypt skips a term by its kind. Today decrypt works out from the plan + that terms are one-way + (`packages/stack-encrypt/src/dynamic/record.rs:1852`). +- A binding reads a length and takes the bytes. + +A new kind of term needs no change to the codec in any language. The limit +is that nothing in the tree refuses a type that reads the wrong entry. The +stored types are generated from one catalog and compared with Rust's bytes. +That comparison is the check that catches a wrong read. + +**The passthrough length (proposed, not yet agreed).** A passthrough node is +the frame of its value, written as it is, with no length +(`packages/recipher-frame/src/transport.rs:16`). The frame format has 26 +tags. A reader has to walk that format to find the end of the node. + +Go can use its own value decoder for that once the codec is in +`internal/frame`. TypeScript has no frame decoder in the host language, +because values cross through Rust. A `u32` length before the frame lets any +language skip a passthrough node. + +Changes: + +- `vitaminc-aead`: the variant, and what each sink and reader does with it. +- `recipher-frame`: the new kind and the length, in both directions. The + encoder still refuses what the decoder would refuse. +- `stack-encrypt`: `shape_record` writes a term node in place of + `bytes_node`. The Rust `Visitor` + (`packages/stack-encrypt/src/target/transcode.rs:96`) has a new method + that the tree calls for a term. +- `languages/golang/internal/frame/ciphertext.go`: the same two changes. The + SDK's hand-written mapping reads the new node until step 1.5 replaces + that mapping. + +No stored data changes. The transport codec's own documentation says "This +encoding has no stability guarantee". The storage profile encodes values, +not trees. + +Done when Rust and Go agree on the new bytes in the shared corpora, and +every existing record test passes with terms as term nodes. + +### 1.4 One walk of a record + +The record's plan becomes an `EncryptTarget`. Encrypting a record is one +walk of the source into a record sink, one key request, and one seal. + +How the walk works, as the first experiment did it: + +- **A record sink in `stack-encrypt`.** For each entry of the source's map, + the record sink gives out the sink for that plan field. That sink is + `EncryptSink::root` with the field's label, in a `Tee` with one term sink + for each index. `Either` lets one map sink give out a different sink for + each field. +- **No trait changes.** `MapSink` and `SeqSink` stay as they are. The + experiment needed one small sink of its own, about 70 lines. That sink + passes an inner sink's result to its parent. +- **A batch.** Each row is planned as its own root. So a row is sealed as if + it were alone, as today. + +What has to be built beyond the experiment: + +| Piece | Today | In the walk | +|---|---|---| +| Equality terms | `terms(value)` alone, for one scalar (`packages/stack-encrypt/src/sem/mod.rs:664`) | The `Terms` sink of `vitaminc-normalize`, in the `Tee` | +| Order terms | `order_terms(target, value)` alone (`sem/mod.rs:1935`) | `Terms::by(Order::new(target))`, in the `Tee` | +| Match terms | No sink. `MatchTerms` takes text | A small sink that keeps a wiped copy of the text, as `match_deferred` does (`sem/mod.rs:1450`) | +| The declared type | `check_field` reads the whole value and converts it before sealing | A sink in front of the others | +| Key descriptors | One descriptor for every key in a tree (`packages/stack-encrypt/src/target/core.rs:105`, and `:161` on open) | A descriptor for each field's part of the tree. The ZeroKMS protocol already takes one for each key (`packages/stack-kms/src/payload.rs:7`) | +| Nulls, scopes, bindings, a context field, passthrough | Handled for each field | Handled by the record sink. The experiment had none of them | + +The walk needs no key for any term. A sink produces a normalised encoding, +and the keyed step runs afterwards with the keyset. + +Two parts stay as they are and join the record's one key request: + +- **The JSON index.** Its walk needs the keyset, because it derives + selectors and tokens under the index key + (`packages/stack-encrypt/src/json/derive.rs:848`). To the record's walk, a + JSON document is one scalar. +- **A field that names a stored type**, until step 1.6 removes the resolver. + +Decrypt is not changed in this step. Today's `record::decrypt` opened every +tree the experiment built. + +Removed: `into_raw_fields`, `lift`, `FieldShape`, and the hand-building in +`shape_record`. + +Done when: + +- the record conformance suites pass with their expected files unchanged; +- a record sealed before the change opens after it, and the reverse; +- the tests that count key requests still count one for a batch; +- no field's value is copied into a frame of its own. + +### 1.5 A deserializer and a visitor in Go and TypeScript + +Each language gets one reader of the tree's transport bytes. The reader +calls a visitor and builds no tree of its own. + +The visitor has one method for each of the seven kinds of node. The kinds +are a sealed leaf, a sequence, a map, the two empty markers, a passthrough, +and a term. A type embeds a default that refuses every kind, and writes only +the methods for the shapes it stores. + +A visitor reads a map's entries in order. The reader skips an entry that the +visitor does not read. The reader refuses a repeated key. + +| | Go | TypeScript | +|---|---|---| +| Where | `languages/golang/internal/frame`, on that package's reader | `languages/typescript/packages/recipher-js/src`, in TypeScript | +| Who can use it | The SDK and `encrypt/eql`. Generated code reaches it through `gensupport` | The package and the EQL extension | +| Failures | Two errors: malformed bytes, and a shape the type does not store | The same two, as a result value. Nothing throws | +| The way back | `MarshalCipherText` exists | A small encoder, for the tree a decrypt sends | + +Rules for both: + +- **One parser for each language.** In Go, `UnmarshalCipherText` has one + caller outside tests (`languages/golang/encrypt/records.go:93`). + `UnmarshalCipherText` becomes a visitor on the new reader, or it is + removed when that caller moves. +- **Hostile input.** The bytes come from the guest. Both directions are + still treated as input an attacker can reach, as the Rust codec treats + them. The reader refuses every truncation, a repeated key, a tree deeper + than `MAX_DEPTH`, an unknown kind, and bytes left over. +- **No plaintext in an error.** An error names the failure. An error never + contains a value or the kind of a decrypted value. +- **TypeScript runs everywhere the SDK does.** The reader is plain + TypeScript, so the native build and the wasm build share it. The reader + does not use Node's `Buffer`. + +Done when the three codecs pass one shared corpus of trees and refused +inputs, and the Go and TypeScript readers are fuzzed. + +### 1.6 Stored types in each language, and no resolver + +`eql-codegen` already writes Go's EQL types from the catalog +(`languages/golang/encrypt/eql/eql_gen.go`). For each language and each +type, `eql-codegen` now also writes: + +- the outputs the type asks for, with their options; +- the visitor that builds the stored value; +- the function that returns the sealed leaf. + +A small hand-written part in each language has what every type shares: + +- the prefix on `c`, and its base64; +- the hex of an equality term; +- the list of positions in a match filter. A match term is its positions as + little-endian `u16` values + (`packages/stack-encrypt/src/sem/mod.rs:933`). + +Two rules move to the host type, because the guest no longer has the type: + +- **Empty match text.** Rust's `TextMatch` refuses it in the guest. A plain + plan seals it and returns an empty term, so the host type refuses it. +- **The stored `i`.** Rust's `TextEq` refuses a stored value whose `i` names + another column. See the open questions. + +Rust's own types in `eql-bindings` stay. Rust programs use them, and they +are the reference. A conformance suite compares each language's value with +Rust's, for every type, under the test key source. The suite compares `c` by +opening it. + +The order of work is the scalar text types, the order types, `TextSearch`, +then the JSON search types. The experiment covered two of the scalar text +types. + +Then the resolver is removed: + +- the second form of every dynamic entry point, and `Verb::Target`; +- about 730 lines of `packages/stack-encrypt/src/dynamic/target.rs`; +- the three resolver files of `eql-bindings`, about 3,000 lines; +- the Go guest built with the EQL types, and its test build. A Go release + then carries three guests, not five; +- every mention of EQL in `stack-encrypt`. + +Done when all of these are true: + +- every EQL v4 type has a generated type in Go and TypeScript that passes + the conformance suite; +- the Go PostgreSQL tests pass on one guest; +- `stack-encrypt` builds with no resolver. + +## Phase 2: queries + +| Step | What | +|---:|---| +| 2.1 | The dynamic query path returns a `StackCipherText` | +| 2.2 | Query types read it with a visitor, in each language | +| 2.3 | `QueryTerm` and each binding's second reader are removed | + +Phase 2 needs steps 1.3 and 1.5. Phase 2 does not need step 1.4. + +### 2.1 The dynamic query path returns a tree + +`dynamic::query_term` +(`packages/stack-encrypt/src/dynamic/query.rs:1093`) returns a tree of +terms. A query has no sealed leaf, so its tree is term nodes, maps, and +sequences. + +| Today's operand | As a tree | +|---|---| +| One term | A map with one entry: the index's key, and a term | +| A range, `lo` and `hi` | The same map, with a sequence of two terms | +| Text, and a list of text (the JSON operators) | Not settled. See the open questions | + +The operator, the fidelity, and the index leave the answer. They describe +the plan, not one query. Go's checker already reads them from the catalog +(`languages/golang/encrypt/checker.go:295`). The SQL domain and the `eql` +encoding leave the answer too. They exist because a resolver returns +finished EQL JSON. + +The form allows a query far larger than today's. A query of 131,072 tokens +was built by hand and read in TypeScript in two forms: + +| Form | Size | Time to read | +|---|---:|---:| +| One node for each token | 1.84 MB | 40 ms | +| One term that contains every token | 1.05 MB | 0.3 ms | + +Both forms pass through the same deserializer. Which form an index uses is a +choice for that index's design, not for this plan. + +### 2.2 Query types read it with a visitor + +A query type is a visitor, as a stored type is. In Rust a query type is +already a target: `TextEqQuery` derives `EncryptTarget` +(`packages/eql/crates/eql-bindings/src/v4/text.rs:457`). `eql-codegen` +writes the query types beside the stored types. Two checks that use today's +answer stay as calls: `target_limit` and `verify`. + +### 2.3 The fixed shapes are removed + +Removed: + +- `QueryTerm`; +- `Operand` and its four shapes; +- Go's `parseQueryTerm`; +- the generated code that takes only a term's bytes + (`languages/golang/encrypt/gensupport/field.go:121`). + +This is not a small change. `QueryTerm` and its operand are public. Ten +source files use them, across `stack-encrypt`, `eql-bindings`, and the Go +guest. Three conformance suites read the operand. + +Done when each binding has one reader for records and queries, and the +query conformance suites pass on the new form. + +## What does not change + +- **Stored bytes.** The sealed leaf, every EQL value, every term's bytes, + and the prefix on `c`. A change to stored bytes during this work is a + defect. +- **Where the security is.** Rust does the sealing, the term derivation, + and the context binding. A host type formats bytes that Rust gave it. +- **The number of key requests.** A call makes one, for any number of rows + and fields. +- **What a stored value authenticates.** A map's membership and a + sequence's order are still not bound. A passthrough field is still not + authenticated. +- **Rust's typed API.** The derive emits combinators only, and the + `eql-bindings` types stay. + +## Checks that must move with the code + +| Check | Step | What happens | +|---|---|---| +| `conformance/stack-encrypt/v1/record-raw-frames-v1.cases.json` | 1.3 | Its cases are transport bytes, so they are regenerated | +| The `ciphertext_malformed` section of `conformance/vitaminc-aead/value-conformance.json` | 1.3 | Has new cases for the new kind and the length | +| Go's `FuzzUnmarshalCipherText` and its seeds | 1.3, 1.5 | Covers the new reader | +| The record suites' expected files | 1.4 | Must not change | +| The tests that count key requests | 1.4 | Must not change | +| A shared corpus of trees and refused inputs | 1.5 | New. Run by Rust, Go, and TypeScript | +| A stored-type conformance suite against Rust | 1.6 | New | +| The suites that read a query's operand | 2.3 | Move to the tree form | +| The mutation gate on `stack-encrypt` | 1.4 | New sinks need tests that fail under each mutant | + +Issue #2004 is the differential fuzzing of the Go codec against the Rust +codec. After step 1.3, that fuzzing covers the new node. + +## What this changes elsewhere + +- **The TypeScript SDK plan** + (`docs/plans/2026-11-22-recipher-typescript-sdk.md`). Its EQL extension + becomes TypeScript types with visitors, with no extension inside the + cipher. A query value comes from the query type, not from the `domain` of + a `QueryTerm`. The plan says that a `match` index takes no options. That + is no longer true for a type that fixes them. +- **The Go SDK.** `encrypt/eql` no longer embeds a guest. The release + workflow's list of guests changes in step 1.6, and so does the + description of the guests in `AGENTS.md`. +- **Decision records.** This plan needs a record of its own. That record + changes what three others describe: + - the target model (0003); + - how a binding enters (0007, with its amendment for the EQL guest); + - the query term (0009). +- **Draft PR #2031.** That PR builds typed records that cross through + serde. This plan did not choose that form. + +## Not in this plan + +- The move of `vitaminc-normalize`, `vitaminc-prf`, and `vitaminc-ore` into + this repository. The steps above use their sinks as published. The match sink + can go in `stack-encrypt`, as the order sink is in `vitaminc-ore`. +- A plan for a nested record. One walk makes it possible. Today a plan + names top-level fields only. +- The developer API of the TypeScript SDK. A sketch exists, and it belongs + to the TypeScript SDK plan. In the sketch: + - a record is an object of fields; + - an EQL type goes by its catalog name; + - a field that is not an EQL type is made with a builder. +- A new name for `"null": "sql"`, and the naming of errors and refusals. +- The Python SDK. + +## Open questions + +1. Context rules: do a record's context rules belong in `stack-encrypt` or + in the cipher's data model? The experiment put them in a record sink in + `stack-encrypt` and changed no trait. The other way changes `MapSink` + and `SeqSink`, so that any source and any cipher can give an entry a + context. This plan assumes the first. +2. Passthrough: does a passthrough node get a length? Step 1.3 proposes + one. +3. A term outside a record: what does opening a tree do with a term node + when no plan says to skip it? +4. The stored `i`: does the host type check it on decrypt, or is the check + dropped? Authenticated encryption still binds the leaf to its field. +5. `eql-codegen`: where does it read each type's outputs, its match + options, and the prefix on `c`? Today the options are a Rust constant in + `eql-bindings`. +6. The derive: does a static record move to one walk too, after phase 1? A + static record also emits one operation for each field + (`packages/stack-encrypt-derive/src/owned_encrypt.rs:447`). +7. Text in a query: what is the tree form of a JSON operator's answer? The + JSON operators return text, not bytes. Nobody has tried them as a tree. +8. Go's visitor: which package makes it public? Generated code cannot + import an `internal` package. This plan assumes `gensupport` does it. + PR #2003 asks a related question about exported methods that take + internal types. +9. The crates of the term sinks: when do they move? No step here needs the + move. +10. Draft PR #2031: does it close? +11. The readability refactor of the dynamic record path: does it merge + before step 1.4? Step 1.4 rewrites much of that code. From 76cb8efef72e8273970ca8e5fdedb6aa28d4426b Mon Sep 17 00:00:00 2001 From: Dan Draper Date: Wed, 2 Dec 2026 00:07:53 +1100 Subject: [PATCH 02/15] docs(plans): correct the one-walk plan after review Phase 2 depends on step 1.6, because the resolver's query form goes there. Step 1.4 checks the single seal first and can keep today's merge of pendings. Step 1.3 says how the new term method relates to the typed term methods. Step 1.1 defines Go's match options as well as sending them. Two citations and two size figures are corrected, and names that arrive with #2003 say so. --- ...1-one-walk-records-and-host-built-types.md | 104 ++++++++++++------ 1 file changed, 72 insertions(+), 32 deletions(-) diff --git a/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md b/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md index 2db3b4e80..0394bd4d3 100644 --- a/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md +++ b/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md @@ -84,7 +84,7 @@ one walk. The dynamic record path builds one operation for each field, and The pieces for one walk exist in `vitaminc-aead`: `Sink`, `Tee` (`packages/vitaminc-aead/src/plaintext/tee.rs`), `EncryptSink::root` -(`packages/vitaminc-aead/src/encrypt_sink.rs:131`), and `Either` +(`packages/vitaminc-aead/src/encrypt_sink.rs:273`), and `Either` (`packages/vitaminc-aead/src/typed.rs:856`). The first decision record of `vitaminc-aead` says "Encrypt-plus-terms is `Tee(EncryptSink, Terms)` with no special wrapper". Stack Encrypt runs each piece alone, once for each @@ -107,11 +107,12 @@ a type that Rust has. ### A query has its own output form -`dynamic::query_term` returns a `QueryTerm` in one of four fixed shapes -(`packages/stack-encrypt/src/dynamic/query.rs:290`). The answer crosses -through serde. Each binding has a second reader for the answer, such as Go's -`parseQueryTerm` (`languages/golang/encrypt/query.go:177`). A query with -more structure than those four shapes has no form to use. +`dynamic::query_term` returns a `QueryTerm` +(`packages/stack-encrypt/src/dynamic/query.rs:281`). Its operand has one of +four fixed shapes (`:290`). The answer crosses through serde. Each binding +has a second reader for the answer, such as Go's `parseQueryTerm` +(`languages/golang/encrypt/query.go:177`). A query with more structure than +those four shapes has no form to use. ## The design @@ -200,7 +201,7 @@ the resolver for the JSON types until that is settled. | 1.1 | Go's plan carries match options | Nothing | | 1.2 | The tree codec takes any leaf | Nothing | | 1.3 | A term node, and a length on a passthrough node | #2003, 1.2 | -| 1.4 | One walk of a record | Nothing. The walk writes term nodes once 1.3 is in | +| 1.4 | One walk of a record | Nothing. Its first task is a check of the seal. The walk writes term nodes once 1.3 is in | | 1.5 | A deserializer and a visitor in Go and TypeScript | #2003, 1.3 | | 1.6 | The stored types in each language, and the resolver removed | 1.1, 1.5, the checks above | @@ -220,14 +221,21 @@ that asks for it. Rust's plan accepts the options in an object form (`packages/stack-encrypt/src/dynamic/record.rs:1206`). Go's plan cannot send -them. `encrypt.Match` takes options and drops them -(`languages/golang/encrypt/term.go:63`). `Plan.Wire()` writes each output as -a string (`languages/golang/internal/record/record.go:544`). +them, for two reasons: +- Go has no option to give. `MatchOption` is an interface with no + implementation, and `encrypt.Match` ignores its arguments + (`languages/golang/encrypt/term.go:53` and `:63`). +- `Plan.Wire()` writes each output as a string + (`languages/golang/internal/record/record.go:544`). + +The step makes three changes: + +- Go's public API defines the match options. - Go's plan model keeps the options and writes the object form. -- The comment on `encrypt.Match` says the guest derives a query under the - defaults only. The experiment showed that the query call uses the plan's - options. The comment is corrected. +- The comment on `MatchOption` is corrected. The comment says the guest + derives a query under the defaults only. The experiment showed that the + query call uses the plan's options. Issue #1673 proposes one analyzer pipeline in place of the fixed option struct. This step sends whatever form the plan has when the step is built. @@ -304,13 +312,25 @@ Changes: - `recipher-frame`: the new kind and the length, in both directions. The encoder still refuses what the decoder would refuse. - `stack-encrypt`: `shape_record` writes a term node in place of - `bytes_node`. The Rust `Visitor` - (`packages/stack-encrypt/src/target/transcode.rs:96`) has a new method - that the tree calls for a term. + `bytes_node`. The Rust `Visitor` has a new method for a term's bytes, + described below. - `languages/golang/internal/frame/ciphertext.go`: the same two changes. The SDK's hand-written mapping reads the new node until step 1.5 replaces that mapping. +**The Rust `Visitor`.** The visitor already has five methods that each take +a typed term: `equality`, `matching`, `ore`, `ope`, and `ore_block` +(`packages/stack-encrypt/src/target/transcode.rs:96`). The typed term +readers call those methods (`:194` to `:215`). Nothing in the tree calls +them, and they stay as they are. + +The new method takes a term's bytes. Only the tree's reader calls it +(`:182`). Its default refuses, as the default of every other method does. + +This plan adds no conversion from a tree's bytes to a typed term. The types +that read a term from a tree are in Go and TypeScript. A Rust type that has +to read one needs that conversion first. + No stored data changes. The transport codec's own documentation says "This encoding has no stability guarantee". The storage profile encodes values, not trees. @@ -350,6 +370,15 @@ What has to be built beyond the experiment: The walk needs no key for any term. A sink produces a normalised encoding, and the keyed step runs afterwards with the keyset. +**The seal is checked first.** The experiment did not seal one tree. It +sealed each field through its own pending and merged the pendings, as +today's code does. That merge already makes one key request. + +One seal of the whole tree needs the descriptor change in the table above. +Nobody has tried that change, so step 1.4 starts by trying it. If the change +does not work, step 1.4 keeps the merge. The result is still one tree and +one key request. + Two parts stay as they are and join the record's one key request: - **The JSON index.** Its walk needs the keyset, because it derives @@ -393,10 +422,11 @@ visitor does not read. The reader refuses a repeated key. Rules for both: -- **One parser for each language.** In Go, `UnmarshalCipherText` has one - caller outside tests (`languages/golang/encrypt/records.go:93`). - `UnmarshalCipherText` becomes a visitor on the new reader, or it is - removed when that caller moves. +- **One parser for each language.** Outside tests, Go decodes the tree in + one place (`languages/golang/encrypt/records.go:93`, through a wrapper). + After #2003, the function that call reaches is `UnmarshalCipherText` in + `internal/frame`. That function becomes a visitor on the new reader, or it + is removed when the caller moves. - **Hostile input.** The bytes come from the guest. Both directions are still treated as input an attacker can reach, as the Rust codec treats them. The reader refuses every truncation, a repeated key, a tree deeper @@ -418,7 +448,10 @@ type, `eql-codegen` now also writes: - the outputs the type asks for, with their options; - the visitor that builds the stored value; -- the function that returns the sealed leaf. +- the function that returns the sealed leaf; +- the query types. In this step a query type is a function of a term's + bytes, because a query still comes back as a `QueryTerm`. Phase 2 makes it + a visitor. A small hand-written part in each language has what every type shares: @@ -448,7 +481,10 @@ Then the resolver is removed: - the second form of every dynamic entry point, and `Verb::Target`; - about 730 lines of `packages/stack-encrypt/src/dynamic/target.rs`; -- the three resolver files of `eql-bindings`, about 3,000 lines; +- the resolver files of `eql-bindings`, about 2,300 lines: `resolver.rs`, + `targets.rs`, and `query.rs` under `src/encryption/`; +- the SQL domain and the `eql` encoding in a query's answer. They exist + because a resolver returns finished EQL JSON; - the Go guest built with the EQL types, and its test build. A Go release then carries three guests, not five; - every mention of EQL in `stack-encrypt`. @@ -468,7 +504,9 @@ Done when all of these are true: | 2.2 | Query types read it with a visitor, in each language | | 2.3 | `QueryTerm` and each binding's second reader are removed | -Phase 2 needs steps 1.3 and 1.5. Phase 2 does not need step 1.4. +Phase 2 needs steps 1.3, 1.5, and 1.6. After step 1.6 no resolver builds a +query, so the answer has one form to change. Phase 2 does not need step +1.4. ### 2.1 The dynamic query path returns a tree @@ -486,8 +524,7 @@ sequences. The operator, the fidelity, and the index leave the answer. They describe the plan, not one query. Go's checker already reads them from the catalog (`languages/golang/encrypt/checker.go:295`). The SQL domain and the `eql` -encoding leave the answer too. They exist because a resolver returns -finished EQL JSON. +encoding left the answer in step 1.6, with the resolver. The form allows a query far larger than today's. A query of 131,072 tokens was built by hand and read in TypeScript in two forms: @@ -504,9 +541,10 @@ choice for that index's design, not for this plan. A query type is a visitor, as a stored type is. In Rust a query type is already a target: `TextEqQuery` derives `EncryptTarget` -(`packages/eql/crates/eql-bindings/src/v4/text.rs:457`). `eql-codegen` -writes the query types beside the stored types. Two checks that use today's -answer stay as calls: `target_limit` and `verify`. +(`packages/eql/crates/eql-bindings/src/v4/text.rs:457`). Step 1.6 generated +each query type as a function of a term's bytes. `eql-codegen` now writes +each one as a visitor. Two checks that use today's answer stay as calls: +`target_limit` and `verify`. ### 2.3 The fixed shapes are removed @@ -518,9 +556,11 @@ Removed: - the generated code that takes only a term's bytes (`languages/golang/encrypt/gensupport/field.go:121`). -This is not a small change. `QueryTerm` and its operand are public. Ten -source files use them, across `stack-encrypt`, `eql-bindings`, and the Go -guest. Three conformance suites read the operand. +This is not a small change. `QueryTerm` and its operand are public. Outside +tests, 12 Rust source files name `QueryTerm` today: seven in +`stack-encrypt`, two in `eql-bindings`, and three in the Go guest. +`packages/eql/crates/eql-codegen/src/targets.rs` names it too, and so do six +files of the Go SDK. Three conformance suites read the operand. Done when each binding has one reader for records and queries, and the query conformance suites pass on the new form. @@ -546,7 +586,7 @@ query conformance suites pass on the new form. |---|---|---| | `conformance/stack-encrypt/v1/record-raw-frames-v1.cases.json` | 1.3 | Its cases are transport bytes, so they are regenerated | | The `ciphertext_malformed` section of `conformance/vitaminc-aead/value-conformance.json` | 1.3 | Has new cases for the new kind and the length | -| Go's `FuzzUnmarshalCipherText` and its seeds | 1.3, 1.5 | Covers the new reader | +| Go's `FuzzUnmarshalCipherText` and its seeds, which #2003 adds | 1.3, 1.5 | Covers the new reader | | The record suites' expected files | 1.4 | Must not change | | The tests that count key requests | 1.4 | Must not change | | A shared corpus of trees and refused inputs | 1.5 | New. Run by Rust, Go, and TypeScript | From 0c4766ae6d39f9c2ae56e3748bc4677c7ab67480 Mon Sep 17 00:00:00 2001 From: Dan Draper Date: Wed, 2 Dec 2026 00:24:06 +1100 Subject: [PATCH 03/15] docs(plans): split the resolver's removal into its own step A second review found that the plan could not finish if the JSON check failed, because one step both built the stored types and removed the resolver. Step 1.6 now builds the types one by one, and step 1.7 removes the resolver when every type is done. The plan also says what the JSON check has to change first, where the type catalog comes from once no resolver answers, which mentions of EQL stay in stack-encrypt, and that TypeScript needs a call that returns a record's tree. Step 1.4 has two refusal tests. Refs #2033. --- ...1-one-walk-records-and-host-built-types.md | 223 ++++++++++++++---- 1 file changed, 173 insertions(+), 50 deletions(-) diff --git a/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md b/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md index 0394bd4d3..b15fd6b10 100644 --- a/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md +++ b/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md @@ -57,8 +57,8 @@ After this plan: - A stored type, such as EQL's `TextEq`, is defined in the language that uses it. Its visitor builds the stored value from the tree. `eql-codegen` writes these types from the EQL catalog. -- Stack Encrypt has no resolver. Stack Encrypt does not name EQL. The Go SDK - has one encryption guest, not two. +- Stack Encrypt has no resolver. Its record form and its query form do not + name EQL. The Go SDK has one encryption guest, not two. - A query is a tree of terms, read by the same deserializer. Rust still does the sealing, the term derivation, and the context binding. @@ -100,7 +100,7 @@ things follow from the resolver: - every entry point has a second form that takes a resolver; - the Go SDK builds, embeds, and releases a second guest with the EQL types; -- Stack Encrypt names EQL. +- Stack Encrypt's record form and query form name EQL. A language cannot define a stored type of its own. A language can only name a type that Rust has. @@ -109,9 +109,9 @@ a type that Rust has. `dynamic::query_term` returns a `QueryTerm` (`packages/stack-encrypt/src/dynamic/query.rs:281`). Its operand has one of -four fixed shapes (`:290`). The answer crosses through serde. Each binding -has a second reader for the answer, such as Go's `parseQueryTerm` -(`languages/golang/encrypt/query.go:177`). A query with more structure than +four fixed shapes (`:290`). The answer crosses through serde. Each language +SDK has a second reader for the answer, such as Go's `parseQueryTerm` +(`languages/golang/encrypt/query.go:184`). A query with more structure than those four shapes has no form to use. ## The design @@ -135,9 +135,13 @@ A stored type is three things in the host language: The field's context is the plan's, as it is today. The resolver uses the plan's parts under the field's label (`packages/eql/crates/eql-bindings/src/encryption/targets.rs:448`). So a -plain field `email` under `users` derives the same term as the EQL type -`TextEq` for `users.email`. A plan with no resolver therefore reads and -writes the data stored today. +plain field `email` under `users` has the same context as the EQL type +`TextEq` for `users.email`. Its equality term is therefore the same. + +A match term is the same only when the plan field asks for the options that +the type fixes. That is why step 1.1 comes first. The experiments checked +equality and match only. The order types, `TextSearch`, and the JSON types +are in the checks to make first. ## What was checked @@ -158,7 +162,7 @@ Two experiments ran. Both are on local branches that are not for merging. | Check | Result | |---|---| -| Go's and TypeScript's stored values against Rust's | The same bytes, once `c` is set aside. `c` is sealed under a new key each time | +| Go's and TypeScript's stored values against Rust's | The same bytes, once `c` is set aside. `c` is sealed under a new key each time. For `TextMatch`, Go sent EQL's match options through a test-only call, because Go's plan cannot send them yet (step 1.1) | | Rust's decryption of a value Go built | The value opens | | A plan with no EQL type, on a value Rust built | The value opens, from the leaf inside `c` | | Query values against Rust's | The same bytes, with nothing set aside | @@ -179,20 +183,31 @@ Neither experiment checked these: ## Checks to make first -Three checks decide whether step 1.6 can remove the whole resolver. Each -check needs only today's tree and the experiment's deserializer. So the -checks can run before any step below, and steps 1.1 to 1.5 do not wait for -them. +Three checks decide which types step 1.6 covers, and whether step 1.7 can +remove the resolver. Steps 1.1 to 1.5 do not wait for them. -| Check | Why it is first | +| Check | What is known | |---|---| -| A JSON search type built in Go or TypeScript | The hard case: 1,050 hand-written lines in `packages/eql/crates/eql-bindings/src/v4/json_tree.rs`. Nobody has looked at what the tree contains for a JSON index today, or whether that is enough to build the stored value | -| The order types: `ob`, `op`, and `between` with its two terms | A second kind of term, and a query of two terms | -| `TextSearch` | Its filter differs from the filter of `TextMatch` | +| A JSON search type built in Go or TypeScript | The hard case. `packages/eql/crates/eql-bindings/src/v4/json_tree.rs` is 1,050 lines, about 880 of them outside tests. Today's tree has nothing to build the type from. See below | +| The order types: `ob`, `op`, and `between` with its two terms | A second kind of term, and a query of two terms. The check needs only today's tree and the experiment's deserializer | +| `TextSearch` | Its filter differs from the filter of `TextMatch`. The check needs only today's tree and the experiment's deserializer | + +The JSON check has to change Stack Encrypt before a host type can be tried: + +- A plan that lists the JSON index as an output is refused today + (`JsonIndexPending`, `packages/stack-encrypt/src/refusal.rs:923`). The + index is reached only through a target that stores a document. +- A JSON operator is answered only through a target + (`packages/stack-encrypt/src/dynamic/query.rs:833`). -Each check is done when the host type builds the bytes Rust builds. If the -JSON check fails, it must report what else has to cross. Step 1.6 then keeps -the resolver for the JSON types until that is settled. +So the check first admits the JSON index on a plain field, and returns the +document in the tree. The check then has to say what form the document has +there. + +Each check is done when the host type builds the bytes Rust builds. + +If the JSON check fails, step 1.6 still covers every other type. Step 1.7 +and phase 2 then wait, and this plan is revised with what the check found. ## Phase 1: the record path @@ -200,10 +215,11 @@ the resolver for the JSON types until that is settled. |---:|---|---| | 1.1 | Go's plan carries match options | Nothing | | 1.2 | The tree codec takes any leaf | Nothing | -| 1.3 | A term node, and a length on a passthrough node | #2003, 1.2 | +| 1.3 | A term node, and a length on a passthrough node | #2003, 1.2, and an answer to open question 3 | | 1.4 | One walk of a record | Nothing. Its first task is a check of the seal. The walk writes term nodes once 1.3 is in | | 1.5 | A deserializer and a visitor in Go and TypeScript | #2003, 1.3 | -| 1.6 | The stored types in each language, and the resolver removed | 1.1, 1.5, the checks above | +| 1.6 | The stored types in each language | 1.1, 1.5, and each type's check above | +| 1.7 | The resolver removed | 1.6 done for every EQL v4 type | PR #2003 moves vitaminc's Go codec into this repository as `languages/golang/internal/frame`. Every Go step here is written against @@ -286,10 +302,10 @@ What the node gives every reader: - A term stops looking like plaintext. Today a term and a passthrough field are the same kind of node. Only a passthrough can contain plaintext. -- Decrypt skips a term by its kind. Today decrypt works out from the plan - that terms are one-way - (`packages/stack-encrypt/src/dynamic/record.rs:1852`). -- A binding reads a length and takes the bytes. +- A reader can tell a term by its kind. Record decrypt does not change in + this plan. It takes the `c` and `passthrough` outputs by the plan's keys + today (`packages/stack-encrypt/src/dynamic/record.rs:1852`). +- A reader takes a length and then the bytes. A new kind of term needs no change to the codec in any language. The limit is that nothing in the tree refuses a type that reads the wrong entry. The @@ -309,6 +325,16 @@ language skip a passthrough node. Changes: - `vitaminc-aead`: the variant, and what each sink and reader does with it. + Two parts need care: + - A term is not a sealed leaf. Two rules count every node that is not a + passthrough as sealed: `check_shape` + (`packages/vitaminc-aead/src/seal.rs:293`) and `sealed_any` + (`packages/vitaminc-aead/src/walk.rs:146`). Both have to leave out a + term too. Otherwise a container of terms alone passes the rule that + refuses a container with no sealed leaf. + - `walk` (`packages/vitaminc-aead/src/walk.rs:162`) has to do something + with a term node. Open question 3 asks what. This step cannot start + until that question has an answer. - `recipher-frame`: the new kind and the length, in both directions. The encoder still refuses what the decoder would refuse. - `stack-encrypt`: `shape_record` writes a term node in place of @@ -317,6 +343,10 @@ Changes: - `languages/golang/internal/frame/ciphertext.go`: the same two changes. The SDK's hand-written mapping reads the new node until step 1.5 replaces that mapping. +- `languages/typescript/packages/recipher-js/value-napi/src/ciphertext.rs`: + `node_to_js` matches every variant of `CipherText` (`:193` to `:225`), so + it has to handle the new one. The package's CI runs when `vitaminc-aead` + changes. **The Rust `Visitor`.** The visitor already has five methods that each take a typed term: `equality`, `matching`, `ore`, `ope`, and `ore_block` @@ -385,7 +415,7 @@ Two parts stay as they are and join the record's one key request: selectors and tokens under the index key (`packages/stack-encrypt/src/json/derive.rs:848`). To the record's walk, a JSON document is one scalar. -- **A field that names a stored type**, until step 1.6 removes the resolver. +- **A field that names a stored type**, until step 1.7 removes the resolver. Decrypt is not changed in this step. Today's `record::decrypt` opened every tree the experiment built. @@ -398,7 +428,14 @@ Done when: - the record conformance suites pass with their expected files unchanged; - a record sealed before the change opens after it, and the reverse; - the tests that count key requests still count one for a batch; -- no field's value is copied into a frame of its own. +- no field's value is copied into a frame of its own; +- in a record of several fields, a leaf moved to another field's position + does not open; +- a field sealed under one descriptor or lock context does not open under + another. + +The last two are refusals. Without them, a field sealed and opened under the +wrong field's context passes every other item on the list. ### 1.5 A deserializer and a visitor in Go and TypeScript @@ -420,6 +457,19 @@ visitor does not read. The reader refuses a repeated key. | Failures | Two errors: malformed bytes, and a shape the type does not store | The same two, as a result value. Nothing throws | | The way back | `MarshalCipherText` exists | A small encoder, for the tree a decrypt sends | +**TypeScript needs the tree first.** Today the package gives TypeScript one +opaque `Ciphertext`, for a single value +(`languages/typescript/packages/recipher-js/crate/src/ciphertext.rs:34`). No +call returns a record's tree. + +So the TypeScript half of this step starts with two native calls: + +- one that returns a record's tree as transport bytes; +- one that takes a tree back, for decrypt. + +PR #1776 adds records to the package in another form. See the open +questions. + Rules for both: - **One parser for each language.** Outside tests, Go decodes the tree in @@ -440,7 +490,7 @@ Rules for both: Done when the three codecs pass one shared corpus of trees and refused inputs, and the Go and TypeScript readers are fuzzed. -### 1.6 Stored types in each language, and no resolver +### 1.6 Stored types in each language `eql-codegen` already writes Go's EQL types from the catalog (`languages/golang/encrypt/eql/eql_gen.go`). For each language and each @@ -477,17 +527,59 @@ The order of work is the scalar text types, the order types, `TextSearch`, then the JSON search types. The experiment covered two of the scalar text types. -Then the resolver is removed: +**The type catalog.** Today the list of EQL types comes from the resolver +(`packages/stack-encrypt/src/dynamic/target.rs:563`). Go's +`Checker.Targets` asks the guest for the list +(`languages/golang/encrypt/checker.go:73`), and `stashgen` reads it from +there (`languages/golang/stashgen/engine.go:102`). + +After step 1.7, no resolver answers. This plan proposes that `eql-codegen` +writes the catalog into each language, beside the types. See the open +questions. + +A type is done when its generated Go and TypeScript types pass the +conformance suite. The step is done when every type whose check passed is +done. + +### 1.7 The resolver removed + +This step starts when step 1.6 is done for every EQL v4 type. + +Removed from `stack-encrypt`: + +- `TargetResolver`, the second form of every dynamic entry point, and + `Verb::Target`. That is about 730 lines of + `packages/stack-encrypt/src/dynamic/target.rs`. +- The `resolver` parameter of `field_operators`, `check_query`, + `query_term`, `verify`, and `target_limit` + (`packages/stack-encrypt/src/dynamic/query.rs:534`, `:815`, `:1093`, + `:1196`, and `:1248`). The branch of `target_limit` for an EQL type's + match options is removed too. +- The record's `eql` output key (`EQL_KEY`, + `packages/stack-encrypt/src/dynamic/record.rs:177`), and the refusal + `NoEqlNode`. +- The `"eql"` form of a query's answer (`query.rs:392`), and the SQL domain + in the answer. Both exist because a resolver returns finished EQL JSON. + +Issue #2033 asks for the `eql` key, the refusal, and the query form to be +renamed. This step removes all three. Under this plan the TypeScript SDK +never reads the `eql` key, because its types ask for plain outputs. + +Two mentions of EQL stay in `stack-encrypt`. This plan does not change them, +and #2033 says to leave both: + +- The JSON query operands. They serialize to EQL's `eql_v4.query_json_*` + forms (`packages/stack-encrypt/src/json/query.rs:403` and `:434`). +- The module `eql_simulator` (`packages/stack-encrypt/src/lib.rs:739`), a + test copy of EQL's read functions. + +Removed elsewhere: -- the second form of every dynamic entry point, and `Verb::Target`; -- about 730 lines of `packages/stack-encrypt/src/dynamic/target.rs`; -- the resolver files of `eql-bindings`, about 2,300 lines: `resolver.rs`, - `targets.rs`, and `query.rs` under `src/encryption/`; -- the SQL domain and the `eql` encoding in a query's answer. They exist - because a resolver returns finished EQL JSON; -- the Go guest built with the EQL types, and its test build. A Go release - then carries three guests, not five; -- every mention of EQL in `stack-encrypt`. +- The resolver files of `eql-bindings`, about 2,300 lines: `resolver.rs`, + `targets.rs`, and `query.rs` under `src/encryption/`. Two of them are + public modules. See "What this changes elsewhere". +- The Go guest built with the EQL types, and its test build. A Go release + then carries three guests, not five. Done when all of these are true: @@ -502,9 +594,9 @@ Done when all of these are true: |---:|---| | 2.1 | The dynamic query path returns a `StackCipherText` | | 2.2 | Query types read it with a visitor, in each language | -| 2.3 | `QueryTerm` and each binding's second reader are removed | +| 2.3 | `QueryTerm` and each SDK's second reader are removed | -Phase 2 needs steps 1.3, 1.5, and 1.6. After step 1.6 no resolver builds a +Phase 2 needs steps 1.3, 1.5, and 1.7. After step 1.7 no resolver builds a query, so the answer has one form to change. Phase 2 does not need step 1.4. @@ -522,9 +614,10 @@ sequences. | Text, and a list of text (the JSON operators) | Not settled. See the open questions | The operator, the fidelity, and the index leave the answer. They describe -the plan, not one query. Go's checker already reads them from the catalog +the plan, not one query. An SDK already asks for them in a separate call, +`field_operators`. Go parses that answer in `parseOperators` (`languages/golang/encrypt/checker.go:295`). The SQL domain and the `eql` -encoding left the answer in step 1.6, with the resolver. +encoding left the answer in step 1.7, with the resolver. The form allows a query far larger than today's. A query of 131,072 tokens was built by hand and read in TypeScript in two forms: @@ -562,8 +655,8 @@ tests, 12 Rust source files name `QueryTerm` today: seven in `packages/eql/crates/eql-codegen/src/targets.rs` names it too, and so do six files of the Go SDK. Three conformance suites read the operand. -Done when each binding has one reader for records and queries, and the -query conformance suites pass on the new form. +Done when each SDK has one reader for records and queries, and the query +conformance suites pass on the new form. ## What does not change @@ -590,7 +683,12 @@ query conformance suites pass on the new form. | The record suites' expected files | 1.4 | Must not change | | The tests that count key requests | 1.4 | Must not change | | A shared corpus of trees and refused inputs | 1.5 | New. Run by Rust, Go, and TypeScript | -| A stored-type conformance suite against Rust | 1.6 | New | +| Refusal tests for one field of a record of several fields | 1.4 | New: a leaf under another field's root, and a wrong descriptor or lock context | +| A stored-type conformance suite against Rust | 1.6 | New. It covers the two refusals that move to the host type | +| A drift check for the generated TypeScript types | 1.6 | New, like `types:check` for Go (`.github/workflows/test-eql.yml:768`) | +| `scripts/__tests__/go-release-artifact.test.mjs` | 1.7 | It expects five guests (`:166`). The number becomes three | +| `scripts/__tests__/stack-exchange-conformance.test.mjs` and `scripts/__tests__/go-binding-test.test.mjs` | 1.7 | Both require the task `wasm:guest:build:eql:deterministic` (`:378` and `:124`) | +| The EQL guest builds in `.github/workflows/tests-golang.yml` | 1.7 | Removed (`:242` and `:245`) | | The suites that read a query's operand | 2.3 | Move to the tree form | | The mutation gate on `stack-encrypt` | 1.4 | New sinks need tests that fail under each mutant | @@ -606,10 +704,27 @@ codec. After step 1.3, that fuzzing covers the new node. a `QueryTerm`. The plan says that a `match` index takes no options. That is no longer true for a type that fixes them. - **The Go SDK.** `encrypt/eql` no longer embeds a guest. The release - workflow's list of guests changes in step 1.6, and so does the + workflow's list of guests changes in step 1.7, and so does the description of the guests in `AGENTS.md`. +- **The `eql-bindings` crate.** Step 1.7 removes its public modules + `encryption::query` and `encryption::targets`. `targets` is public on + `main` too. The crate is published at one version with the other four EQL + artefacts. See the open questions. +- **The SDK design principles** (`docs/sdk-design-principles.md`). + Principle 1 says that nothing is rebuilt from parts, and that an SDK + "holds no copy of the engine's rules". The same principle lets an SDK + assemble a wire format in the host language "only when a cross-language + test compares the bytes". This plan uses that for every stored type. The + stored-type conformance suite is the test. + + Two refusals also move from Rust into generated host code: empty match + text (`EmptyTermText`, + `packages/eql/crates/eql-bindings/src/encryption.rs:420`) and the stored + `i`. They are rules of the EQL types. Each language then has a copy, so + the conformance suite has to cover both. - **Decision records.** This plan needs a record of its own. That record - changes what three others describe: + changes what three records of `stack-encrypt` describe + (`packages/stack-encrypt/docs/adr`): - the target model (0003); - how a binding enters (0007, with its amendment for the EQL guest); - the query term (0009). @@ -641,7 +756,8 @@ codec. After step 1.3, that fuzzing covers the new node. 2. Passthrough: does a passthrough node get a length? Step 1.3 proposes one. 3. A term outside a record: what does opening a tree do with a term node - when no plan says to skip it? + when no plan says to skip it? Step 1.3 cannot start until this has an + answer. 4. The stored `i`: does the host type check it on decrypt, or is the check dropped? Authenticated encryption still binds the leaf to its field. 5. `eql-codegen`: where does it read each type's outputs, its match @@ -661,3 +777,10 @@ codec. After step 1.3, that fuzzing covers the new node. 10. Draft PR #2031: does it close? 11. The readability refactor of the dynamic record path: does it merge before step 1.4? Step 1.4 rewrites much of that code. +12. The type catalog: where does an SDK read the list of EQL types once no + resolver answers? Step 1.6 proposes the generated code. +13. `eql-bindings`: is the removal of its public modules `encryption::query` + and `encryption::targets` a major release of the five EQL artefacts? +14. PR #1776: does the TypeScript half of step 1.5 build on its record path + or replace it? That PR adds records to the TypeScript package through + `dynamic::Planned`. From 4b7ba1050f69c03f7d78657c5c9a05b3feef64a0 Mon Sep 17 00:00:00 2001 From: Dan Draper Date: Wed, 2 Dec 2026 14:44:07 +1100 Subject: [PATCH 04/15] docs(plans): say how the TextSearch filter differs, and explain the JSON check The plan said the filter of TextSearch differs from the filter of TextMatch. The two are built the same way, under the same match options. They differ only for text that cuts no token: TextMatch refuses it, and TextSearch stores an empty filter. The plan now says so, with the lines that do it, and names the one rule the two host types differ by. The JSON check gave two facts and no reason. It now says why a JSON document has no route through the tree today: a plan cannot list the JSON index as an output, the target builds the stored value in Rust from the index's document, and a JSON operator reads its options from the target. --- ...1-one-walk-records-and-host-built-types.md | 73 ++++++++++++++++--- 1 file changed, 62 insertions(+), 11 deletions(-) diff --git a/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md b/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md index b15fd6b10..a38dd5fe6 100644 --- a/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md +++ b/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md @@ -190,25 +190,74 @@ remove the resolver. Steps 1.1 to 1.5 do not wait for them. |---|---| | A JSON search type built in Go or TypeScript | The hard case. `packages/eql/crates/eql-bindings/src/v4/json_tree.rs` is 1,050 lines, about 880 of them outside tests. Today's tree has nothing to build the type from. See below | | The order types: `ob`, `op`, and `between` with its two terms | A second kind of term, and a query of two terms. The check needs only today's tree and the experiment's deserializer | -| `TextSearch` | Its filter differs from the filter of `TextMatch`. The check needs only today's tree and the experiment's deserializer | +| `TextSearch` | Its filter is the filter of `TextMatch`, with one difference: text that cuts no token. See below. The check needs only today's tree and the experiment's deserializer | -The JSON check has to change Stack Encrypt before a host type can be tried: +Each check is done when the host type builds the bytes Rust builds. -- A plan that lists the JSON index as an output is refused today - (`JsonIndexPending`, `packages/stack-encrypt/src/refusal.rs:923`). The - index is reached only through a target that stores a document. -- A JSON operator is answered only through a target +### The JSON check + +The JSON check has to change Stack Encrypt before a host type can be tried. +The reason is the route a JSON document takes to a stored type today. + +A scalar type has a route through the tree. The type's plan field lists +the outputs the type is made of. `TextEq` asks for `outputs: ["c", "eq"]`, and +the field comes back as a map with those two keys. The host type reads the +map. + +A JSON document has no such route: + +- **A plan cannot list the JSON index as an output.** A `json` field has to + name a target that stores a document, such as `JsonSearch`. A plan that + lists the index as an output is refused with `JsonIndexPending` + (`packages/stack-encrypt/src/refusal.rs:923`, + `packages/stack-encrypt/src/dynamic/kind.rs:116`). +- **The target builds the stored value in Rust.** The JSON index returns one + document: a key header `h`, the entries `sv`, and the containment + projection `q`. Rust's EQL type is that document's JSON with `v` and `i` + added (`packages/eql/crates/eql-bindings/src/v4/json_tree.rs:481`). The + resolver returns the result as finished JSON bytes. The document is never + a node of the tree. +- **A JSON operator needs the target too.** A query reads the index's + options from the field's target, and a field with no target has none (`packages/stack-encrypt/src/dynamic/query.rs:833`). -So the check first admits the JSON index on a plain field, and returns the -document in the tree. The check then has to say what form the document has -there. +So the check has two parts, in this order: -Each check is done when the host type builds the bytes Rust builds. +1. Stack Encrypt admits the JSON index as an output of a plain `json` field, + and returns the document in the tree. The check has to say what form the + document has there. No node of today's tree fits a document. A document + is not one sealed leaf with terms beside it: each entry in `sv` has a + ciphertext of its own (`json_tree.rs:125`). +2. A Go or TypeScript type builds the stored value from that form, and the + check compares the value with Rust's. If the JSON check fails, step 1.6 still covers every other type. Step 1.7 and phase 2 then wait, and this plan is revised with what the check found. +### The `TextSearch` check + +`TextSearch` and `TextMatch` build a filter the same way. Both use EQL's +match options (`packages/eql/crates/eql-bindings/src/encryption.rs:472` and +`:517`), and both write the positions as the same list (`:443`). The two +filters have one JSON schema +(`packages/eql/crates/eql-bindings/src/v4/mod.rs:126`). + +The types differ only for text that cuts no token: empty text, one letter, +or separators alone. + +| Type | Text that cuts no token | +|---|---| +| `TextMatch` | Refused with `EmptyTermText` (`encryption.rs:434`) | +| `TextSearch` and `TextSearchOre` | Stored with an empty filter (`encryption.rs:498`) | + +The rustdoc gives a reason (`v4/mod.rs:104`). `=` and `<` still find a +search value through `hm` and its order term. A `TextMatch` value with an +empty filter is a row that no operator can find. + +So the two host types differ by one rule. `TextMatch` refuses a match term +that has no positions, and `TextSearch` accepts it. The check covers that +rule, and a value that is built from three terms. + ## Phase 1: the record path | Step | What | Depends on | @@ -514,7 +563,9 @@ A small hand-written part in each language has what every type shares: Two rules move to the host type, because the guest no longer has the type: - **Empty match text.** Rust's `TextMatch` refuses it in the guest. A plain - plan seals it and returns an empty term, so the host type refuses it. + plan seals it and returns an empty term, so the host's `TextMatch` refuses + it. The host's `TextSearch` stores it with an empty filter, as Rust's + does. - **The stored `i`.** Rust's `TextEq` refuses a stored value whose `i` names another column. See the open questions. From cbec34605ff37a84d997f9af21ef359ba892dd06 Mon Sep 17 00:00:00 2001 From: Dan Draper Date: Wed, 2 Dec 2026 15:56:20 +1100 Subject: [PATCH 05/15] docs(plans): Go's reader and visitor are in a public package A stored type implements the visitor, so a type outside the Go module has to be able to import it. A package under internal cannot be imported from another module, and gensupport is for generated code alone. The open question on the package now asks for its name, and whether a type must embed the default. --- ...1-one-walk-records-and-host-built-types.md | 38 ++++++++++++++----- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md b/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md index a38dd5fe6..97bbe65dc 100644 --- a/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md +++ b/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md @@ -272,7 +272,8 @@ rule, and a value that is built from three terms. PR #2003 moves vitaminc's Go codec into this repository as `languages/golang/internal/frame`. Every Go step here is written against -that package. vitaminc's `vcencrypt` is to be deleted. After that, nothing +that package. Step 1.5 adds a public package beside it, for the reader and +the visitor. vitaminc's `vcencrypt` is to be deleted. After that, nothing outside this repository reads the tree. ### 1.1 Go's plan carries match options @@ -501,11 +502,29 @@ visitor does not read. The reader refuses a repeated key. | | Go | TypeScript | |---|---|---| -| Where | `languages/golang/internal/frame`, on that package's reader | `languages/typescript/packages/recipher-js/src`, in TypeScript | -| Who can use it | The SDK and `encrypt/eql`. Generated code reaches it through `gensupport` | The package and the EQL extension | +| Where | A public package of the Go module. This plan proposes `languages/golang/encrypt/transcode` | `languages/typescript/packages/recipher-js/src`, in TypeScript | +| Who can use it | Any Go module: the SDK, `encrypt/eql`, and a type that is written outside this repository | The package and the EQL extension | | Failures | Two errors: malformed bytes, and a shape the type does not store | The same two, as a result value. Nothing throws | | The way back | `MarshalCipherText` exists | A small encoder, for the tree a decrypt sends | +**Go's reader and visitor are public.** A stored type implements the +visitor, so the visitor is the interface that a new type is written +against. `encrypt/eql` is the first package to use it. Other packages are +likely to use it later. + +- **Why not `internal`.** Go lets only code inside `languages/golang` import + a package under `languages/golang/internal`. `encrypt/eql` is inside. A + type in another module is not. +- **Why not `gensupport`.** That package is for code that `stashgen` writes. + The package's documentation says that a program never imports it + (`languages/golang/encrypt/gensupport/gensupport.go:3`). +- **No internal type in an exported name.** A type outside the module has to + name everything that the visitor's methods take. The experiment's package + was public and imported the standard library alone. +- **The cost.** The visitor becomes public API of the Go module. A new kind + of node adds a method to the interface. That change breaks a type that + does not embed the default. See the open questions. + **TypeScript needs the tree first.** Today the package gives TypeScript one opaque `Ciphertext`, for a single value (`languages/typescript/packages/recipher-js/crate/src/ciphertext.rs:34`). No @@ -524,8 +543,8 @@ Rules for both: - **One parser for each language.** Outside tests, Go decodes the tree in one place (`languages/golang/encrypt/records.go:93`, through a wrapper). After #2003, the function that call reaches is `UnmarshalCipherText` in - `internal/frame`. That function becomes a visitor on the new reader, or it - is removed when the caller moves. + `internal/frame`. That function becomes a visitor on the public reader, or + it is removed when the caller moves. - **Hostile input.** The bytes come from the guest. Both directions are still treated as input an attacker can reach, as the Rust codec treats them. The reader refuses every truncation, a repeated key, a tree deeper @@ -819,10 +838,11 @@ codec. After step 1.3, that fuzzing covers the new node. (`packages/stack-encrypt-derive/src/owned_encrypt.rs:447`). 7. Text in a query: what is the tree form of a JSON operator's answer? The JSON operators return text, not bytes. Nobody has tried them as a tree. -8. Go's visitor: which package makes it public? Generated code cannot - import an `internal` package. This plan assumes `gensupport` does it. - PR #2003 asks a related question about exported methods that take - internal types. +8. Go's visitor: what is its public package called? Step 1.5 proposes + `encrypt/transcode`, after the Rust module. Does the interface also make + every type embed the default? With that rule, a new kind of node does + not break a type that is written outside this repository. PR #2003 asks + a related question about exported methods that take internal types. 9. The crates of the term sinks: when do they move? No step here needs the move. 10. Draft PR #2031: does it close? From 242f795a90767ea0fac0fde659dc9d1028e82b78 Mon Sep 17 00:00:00 2001 From: Dan Draper Date: Wed, 2 Dec 2026 16:58:13 +1100 Subject: [PATCH 06/15] docs(plans): one name for each part of the reader, and a QueryTerm shim to consider The reader and the visitor had different names in Rust, in the experiment and in the Go codec. The plan now proposes one name for each part, written in each language's style. Step 2.3 records the option of a deprecated QueryTerm in Go, with the facts that decide how much it helps. --- ...1-one-walk-records-and-host-built-types.md | 50 ++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md b/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md index 97bbe65dc..9ab370f44 100644 --- a/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md +++ b/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md @@ -505,7 +505,35 @@ visitor does not read. The reader refuses a repeated key. | Where | A public package of the Go module. This plan proposes `languages/golang/encrypt/transcode` | `languages/typescript/packages/recipher-js/src`, in TypeScript | | Who can use it | Any Go module: the SDK, `encrypt/eql`, and a type that is written outside this repository | The package and the EQL extension | | Failures | Two errors: malformed bytes, and a shape the type does not store | The same two, as a result value. Nothing throws | -| The way back | `MarshalCipherText` exists | A small encoder, for the tree a decrypt sends | +| The way back | #2003 has the function. See the names below | A small encoder, for the tree a decrypt sends | + +**Names.** Each part has one name in every language. Each language writes +the name in its own style. Rust has every part today but the method for a +term node, so Rust's words are used +(`packages/stack-encrypt/src/target/transcode.rs:36` to `:156`, and +`packages/recipher-frame/src/transport.rs:198` and `:261`). The Go names, +the TypeScript names, and the name `term` are proposed. + +| Part | Rust | Go | TypeScript | +|---|---|---|---| +| The interface that a type implements | `Visitor` | `Visitor` | `Visitor` | +| The methods for the six kinds of node that exist today | `sealed`, `sequence`, `map`, `empty_sequence`, `empty_map`, `passthrough` | `Sealed`, `Sequence`, `Map`, `EmptySequence`, `EmptyMap`, `Passthrough` | `sealed`, `sequence`, `map`, `emptySequence`, `emptyMap`, `passthrough` | +| The method for a term node (new in step 1.3) | `term` | `Term` | `term` | +| The elements of a sequence | `SequenceReader` | `SequenceReader` | `SequenceReader` | +| The entries of a map | `MapReader` | `MapReader` | `MapReader` | +| From bytes to a visitor | `decode_ciphertext` | `DecodeCipherText` | `decodeCipherText` | +| From a tree to bytes (the way back) | `encode_ciphertext` | `EncodeCipherText` | `encodeCipherText` | + +The names in the experiment and in #2003 differ from these in three places: + +- The experiment named the two readers `SeqAccess` and `MapAccess`, in Go + and in TypeScript. +- #2003 names Go's two functions `MarshalCipherText` and + `UnmarshalCipherText`. +- The experiment named the TypeScript functions `decode` and `encode`. + +The names of the two errors are not in the table. The naming of errors is +a separate piece of work. **Go's reader and visitor are public.** A stored type implements the visitor, so the visitor is the interface that a new type is written @@ -719,6 +747,24 @@ Removed: - the generated code that takes only a term's bytes (`languages/golang/encrypt/gensupport/field.go:121`). +**A deprecated `QueryTerm` in Go (to consider).** The Go SDK is the only +SDK that programs use today. So Go could keep its `QueryTerm` for a time, +marked deprecated, and build it from the new tree. Three facts apply: + +- A program cannot call `Cipher.QueryTerm` itself. The method takes a plan + type from an `internal` package + (`languages/golang/encrypt/query.go:140`). +- A generated file calls `Term`, `Range`, and `NullTerm` in `gensupport` + (`languages/golang/encrypt/gensupport/field.go:121`, `:131`, and `:143`). + Each returns a term's bytes. +- A generated file names the version of its layout + (`languages/golang/encrypt/gensupport/gensupport.go:35`). A file from + another layout does not compile. + +So the shim matters most for a generated file that was written before the +change. Nobody has checked whether steps 1.6 and 1.7 already change that +layout. See the open questions. + This is not a small change. `QueryTerm` and its operand are public. Outside tests, 12 Rust source files name `QueryTerm` today: seven in `stack-encrypt`, two in `eql-bindings`, and three in the Go guest. @@ -855,3 +901,5 @@ codec. After step 1.3, that fuzzing covers the new node. 14. PR #1776: does the TypeScript half of step 1.5 build on its record path or replace it? That PR adds records to the TypeScript package through `dynamic::Planned`. +15. `QueryTerm` in Go: does Go keep a deprecated `QueryTerm`, built from the + new tree, after step 2.3? Step 2.3 gives the facts. From 132a2a3230a912ce2e2e780bf0f48ac3d6207f66 Mon Sep 17 00:00:00 2001 From: Dan Draper Date: Wed, 2 Dec 2026 17:52:26 +1100 Subject: [PATCH 07/15] docs(plans): Go's codec is the public encrypt/frame, and the reader goes there The frame-codec pull request now moves vitaminc's Go packages as two public packages, encrypt/frame and encrypt/value, and adds no internal/frame. The plan's Go steps name the new path. With the codec public, the reader and the visitor go beside it, so the plan no longer proposes a package of its own. A new open question asks what happens to the Cipher methods that take an internal plan type. --- ...1-one-walk-records-and-host-built-types.md | 57 ++++++++++++------- 1 file changed, 37 insertions(+), 20 deletions(-) diff --git a/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md b/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md index 9ab370f44..9049f7587 100644 --- a/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md +++ b/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md @@ -270,11 +270,16 @@ rule, and a value that is built from three terms. | 1.6 | The stored types in each language | 1.1, 1.5, and each type's check above | | 1.7 | The resolver removed | 1.6 done for every EQL v4 type | -PR #2003 moves vitaminc's Go codec into this repository as -`languages/golang/internal/frame`. Every Go step here is written against -that package. Step 1.5 adds a public package beside it, for the reader and -the visitor. vitaminc's `vcencrypt` is to be deleted. After that, nothing -outside this repository reads the tree. +PR #2003 moves vitaminc's two Go packages into this repository. Both are +public: + +- `languages/golang/encrypt/frame` is the codec. It has + `MarshalCipherText`, `UnmarshalCipherText`, and the storage profile. +- `languages/golang/encrypt/value` is the value types. + +Every Go step here is written against `encrypt/frame`. Step 1.5 adds the +reader and the visitor to it. vitaminc's `vcencrypt` is to be deleted. +After that, nothing outside this repository reads the tree. ### 1.1 Go's plan carries match options @@ -368,7 +373,7 @@ the frame of its value, written as it is, with no length tags. A reader has to walk that format to find the end of the node. Go can use its own value decoder for that once the codec is in -`internal/frame`. TypeScript has no frame decoder in the host language, +`encrypt/frame`. TypeScript has no frame decoder in the host language, because values cross through Rust. A `u32` length before the frame lets any language skip a passthrough node. @@ -390,7 +395,7 @@ Changes: - `stack-encrypt`: `shape_record` writes a term node in place of `bytes_node`. The Rust `Visitor` has a new method for a term's bytes, described below. -- `languages/golang/internal/frame/ciphertext.go`: the same two changes. The +- `languages/golang/encrypt/frame/ciphertext.go`: the same two changes. The SDK's hand-written mapping reads the new node until step 1.5 replaces that mapping. - `languages/typescript/packages/recipher-js/value-napi/src/ciphertext.rs`: @@ -502,7 +507,7 @@ visitor does not read. The reader refuses a repeated key. | | Go | TypeScript | |---|---|---| -| Where | A public package of the Go module. This plan proposes `languages/golang/encrypt/transcode` | `languages/typescript/packages/recipher-js/src`, in TypeScript | +| Where | `languages/golang/encrypt/frame`, the public package of the codec | `languages/typescript/packages/recipher-js/src`, in TypeScript | | Who can use it | Any Go module: the SDK, `encrypt/eql`, and a type that is written outside this repository | The package and the EQL extension | | Failures | Two errors: malformed bytes, and a shape the type does not store | The same two, as a result value. Nothing throws | | The way back | #2003 has the function. See the names below | A small encoder, for the tree a decrypt sends | @@ -540,15 +545,19 @@ visitor, so the visitor is the interface that a new type is written against. `encrypt/eql` is the first package to use it. Other packages are likely to use it later. -- **Why not `internal`.** Go lets only code inside `languages/golang` import - a package under `languages/golang/internal`. `encrypt/eql` is inside. A - type in another module is not. +- **Why `encrypt/frame`.** The codec for the tree is in that public + package. With the reader beside the codec, one package reads the tree. + The experiment had a package of its own, `encrypt/transcode`. A second + package is not needed now. See the open questions. +- **Why not an `internal` package.** Go lets only code inside + `languages/golang` import a package under `languages/golang/internal`. + `encrypt/eql` is inside. A type in another module is not. - **Why not `gensupport`.** That package is for code that `stashgen` writes. The package's documentation says that a program never imports it (`languages/golang/encrypt/gensupport/gensupport.go:3`). - **No internal type in an exported name.** A type outside the module has to - name everything that the visitor's methods take. The experiment's package - was public and imported the standard library alone. + name everything that the visitor's methods take. `encrypt/frame` imports + the standard library and `encrypt/value` alone. - **The cost.** The visitor becomes public API of the Go module. A new kind of node adds a method to the interface. That change breaks a type that does not embed the default. See the open questions. @@ -571,8 +580,8 @@ Rules for both: - **One parser for each language.** Outside tests, Go decodes the tree in one place (`languages/golang/encrypt/records.go:93`, through a wrapper). After #2003, the function that call reaches is `UnmarshalCipherText` in - `internal/frame`. That function becomes a visitor on the public reader, or - it is removed when the caller moves. + `encrypt/frame`. That function becomes a visitor on the new reader, or it + is removed when the caller moves. - **Hostile input.** The bytes come from the guest. Both directions are still treated as input an attacker can reach, as the Rust codec treats them. The reader refuses every truncation, a repeated key, a tree deeper @@ -884,11 +893,10 @@ codec. After step 1.3, that fuzzing covers the new node. (`packages/stack-encrypt-derive/src/owned_encrypt.rs:447`). 7. Text in a query: what is the tree form of a JSON operator's answer? The JSON operators return text, not bytes. Nobody has tried them as a tree. -8. Go's visitor: what is its public package called? Step 1.5 proposes - `encrypt/transcode`, after the Rust module. Does the interface also make - every type embed the default? With that rule, a new kind of node does - not break a type that is written outside this repository. PR #2003 asks - a related question about exported methods that take internal types. +8. Go's visitor: is it in `encrypt/frame`, beside the codec, or in a + package of its own? Step 1.5 proposes `encrypt/frame`. Does the interface + also make every type embed the default? With that rule, a new kind of + node does not break a type that is written outside this repository. 9. The crates of the term sinks: when do they move? No step here needs the move. 10. Draft PR #2031: does it close? @@ -903,3 +911,12 @@ codec. After step 1.3, that fuzzing covers the new node. `dynamic::Planned`. 15. `QueryTerm` in Go: does Go keep a deprecated `QueryTerm`, built from the new tree, after step 2.3? Step 2.3 gives the facts. +16. `Cipher`'s methods that take a plan: do they change in step 1.4? Six + methods beside `QueryTerm` take a `*record.Plan` from `internal/record`: + `Verify`, `TargetLimit`, `Contexts`, `Seal`, `Open`, and `JSONOpen` + (`languages/golang/encrypt/query.go:292`, + `languages/golang/encrypt/records.go:47`). A program outside the module + cannot name that type, so only generated code calls them. Step 1.4 + rewrites the path behind `Seal` and `Open`. The Go SDK's principle 7 + says that no method that a user touches is called a plan or returns + one. This question came from #2003. From ad8b6dfbff322f8b6adfebb4d9473c09f457fd49 Mon Sep 17 00:00:00 2001 From: Dan Draper Date: Thu, 3 Dec 2026 23:42:14 +1100 Subject: [PATCH 08/15] docs(plans): the one-sink design for step 1.4, and its pull requests 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. --- ...1-one-walk-records-and-host-built-types.md | 178 ++++++++++++++---- 1 file changed, 138 insertions(+), 40 deletions(-) diff --git a/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md b/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md index 9049f7587..5894999cf 100644 --- a/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md +++ b/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md @@ -1,7 +1,8 @@ # One walk for a record, and host-built stored types -Status: draft, 1 December 2026. Nothing in this plan is built. Decisions by -Dan Draper: +Status: draft, 1 December 2026, updated 3 December 2026. A spike of step +1.4 is in draft PR #2043. That PR is not for merging. Nothing else in this +plan is built. Decisions by Dan Draper: - Encryption works as serde does. Any plaintext structure gives one `StackCipherText`. A record is a `Map`, and a batch of records is a @@ -18,6 +19,23 @@ Dan Draper: - A query comes back as a `StackCipherText` too. That is the second phase. - The public API may change. +Decisions from the spike, made on 2 December 2026 and 3 December 2026: + +- A record is walked once, by one sink that `stack-encrypt` builds from the + plan. The caller adds no adapter sink. Where the record sink needs + something from `vitaminc-aead`, `vitaminc-aead` changes. +- The encrypt sink holds no context. One function derives each leaf's + additional authenticated data (AAD) from the leaf's place in the tree. +- A value staged for encryption is a `Node`. Only + `target::Pending` is called `Pending`. +- `Cipher` is the only cipher trait. A tree is sealed with a closure, as it + is opened. `AsyncCipher` is deleted. +- The AES-256-GCM cipher moves into `vitaminc-aead`, and + `vitaminc-normalize` moves into this repository. Nothing here then depends on + `vitaminc-encrypt` 0.10. +- The readability refactor of the dynamic record path (#2042) merges first. + The pull requests of step 1.4 stack on it. + Stored data fixes one format, so this plan does not choose it. An EQL value's `c` is the text `stack-encrypt:1:` followed by base64 of the sealed leaf. Production data has that form. @@ -145,7 +163,8 @@ are in the checks to make first. ## What was checked -Two experiments ran. Both are on local branches that are not for merging. +Three experiments ran. The first two are on local branches. The third is +draft PR #2043. None of them is for merging. **One walk of a record.** Four tests, with no change to a trait in `vitaminc-aead`: @@ -172,7 +191,39 @@ Two experiments ran. Both are on local branches that are not for merging. The Go half needed no change in Rust. A plan field that lists `outputs: ["c", "eq"]` already comes back as a map with those two keys. -Neither experiment checked these: +**One sink for a record, sealed in one pass (#2043).** The spike is built on +#2042. + +A value and a record follow the same three steps. The walk gives a tree of +plaintext leaves. One key request follows. Then one pass seals the leaves +and keys the terms. + +| Check | Result | +|---|---| +| A record walked by `RecordSink`, with the context field last in the row | Every field has its context when the walk ends | +| One key request for the record, then one seal | Today's `record::decrypt` opens the result | +| The spike's `open_record` on a record that today's path stored | The record opens. Each path opens the other's records | +| A record sealed for one tenant, opened as another | Refused | +| A field the plan does not name, a missing field, and a scalar in place of a record | Each one refused | +| The value path, after `PendingStackCipherText` and the adapter cipher were removed | The fixture tests pass with their expected bytes unchanged. The Go tests that open records sealed before the spike pass | + +The record sink needed two additions to `vitaminc-aead`, and no change to +`Sink`, `MapSink`, or `SeqSink`: + +- `Option<S>` is a sink when `S` is one, so a field can have a sink or none. +- `EncryptSink::into(place)` writes its tree into a place the caller lends. + +Each field gets its own key descriptor, from the one function that states a +field's context (`field_context`). That is the change the first experiment +did not try. + +The record sink is in tests only. It does not yet cover: + +- type checks, nulls, absent fields, and batches; +- match terms, order terms, EQL targets, and the JSON index; +- queries, the expected context, and the kinds of error. + +Neither of the first two experiments checked these: - The JSON search types, the order terms, and `TextSearch`. - Null fields, scopes, bindings, and a context field. @@ -265,7 +316,7 @@ rule, and a value that is built from three terms. | 1.1 | Go's plan carries match options | Nothing | | 1.2 | The tree codec takes any leaf | Nothing | | 1.3 | A term node, and a length on a passthrough node | #2003, 1.2, and an answer to open question 3 | -| 1.4 | One walk of a record | Nothing. Its first task is a check of the seal. The walk writes term nodes once 1.3 is in | +| 1.4 | One walk of a record | #2042. The walk writes term nodes once 1.3 is in | | 1.5 | A deserializer and a visitor in Go and TypeScript | #2003, 1.3 | | 1.6 | The stored types in each language | 1.1, 1.5, and each type's check above | | 1.7 | The resolver removed | 1.6 done for every EQL v4 type | @@ -428,20 +479,28 @@ every existing record test passes with terms as term nodes. The record's plan becomes an `EncryptTarget`. Encrypting a record is one walk of the source into a record sink, one key request, and one seal. -How the walk works, as the first experiment did it: - -- **A record sink in `stack-encrypt`.** For each entry of the source's map, - the record sink gives out the sink for that plan field. That sink is - `EncryptSink::root` with the field's label, in a `Tee` with one term sink - for each index. `Either` lets one map sink give out a different sink for - each field. -- **No trait changes.** `MapSink` and `SeqSink` stay as they are. The - experiment needed one small sink of its own, about 70 lines. That sink - passes an inner sink's result to its parent. -- **A batch.** Each row is planned as its own root. So a row is sealed as if - it were alone, as today. - -What has to be built beyond the experiment: +The spike (#2043) does this with one sink. Open question 17 asks whether +the plan still has to be an `EncryptTarget` as well. + +How the walk works, as the spike does it: + +- **A record sink in `stack-encrypt`, built from the plan.** The record sink + sends each field's value to the sinks the plan names for the field. Those + are the encrypt sink and a term sink for each index. When the walk ends, + the record sink settles each field's context. +- **The encrypt sink holds no context.** It gives a tree of plaintext + leaves, a `Node<Plaintext>`. One function derives each leaf's AAD from its + place when the tree is sealed or opened. +- **One key request, then one pass.** The request has a key descriptor for + each field. The pass seals every leaf and keys every term. +- **No trait changes.** `Sink`, `MapSink`, and `SeqSink` stay as they are. + The caller has no adapter sink. The first experiment needed one, about 70 + lines. The spike needed two small additions to `vitaminc-aead` in its + place (see [What was checked](#what-was-checked)). +- **A batch.** Each row is its own root, as in the first experiment. So a + row is sealed as if it were alone, as today. The spike has no batches yet. + +What has to be built beyond the spike: | Piece | Today | In the walk | |---|---|---| @@ -449,20 +508,16 @@ What has to be built beyond the experiment: | Order terms | `order_terms(target, value)` alone (`sem/mod.rs:1935`) | `Terms::by(Order::new(target))`, in the `Tee` | | Match terms | No sink. `MatchTerms` takes text | A small sink that keeps a wiped copy of the text, as `match_deferred` does (`sem/mod.rs:1450`) | | The declared type | `check_field` reads the whole value and converts it before sealing | A sink in front of the others | -| Key descriptors | One descriptor for every key in a tree (`packages/stack-encrypt/src/target/core.rs:105`, and `:161` on open) | A descriptor for each field's part of the tree. The ZeroKMS protocol already takes one for each key (`packages/stack-kms/src/payload.rs:7`) | -| Nulls, scopes, bindings, a context field, passthrough | Handled for each field | Handled by the record sink. The experiment had none of them | +| Key descriptors | One descriptor for every key in a tree (`packages/stack-encrypt/src/target/core.rs:105`, and `:161` on open) | A descriptor for each field's part of the tree, as the spike has. The ZeroKMS protocol already takes one for each key (`packages/stack-kms/src/payload.rs:7`) | +| Nulls, scopes, bindings, passthrough | Handled for each field | Handled by the record sink. The spike has a context field and passthrough, and none of the others | The walk needs no key for any term. A sink produces a normalised encoding, and the keyed step runs afterwards with the keyset. -**The seal is checked first.** The experiment did not seal one tree. It -sealed each field through its own pending and merged the pendings, as -today's code does. That merge already makes one key request. - -One seal of the whole tree needs the descriptor change in the table above. -Nobody has tried that change, so step 1.4 starts by trying it. If the change -does not work, step 1.4 keeps the merge. The result is still one tree and -one key request. +**The seal.** The first experiment sealed each field through its own +pending and merged the pendings, as today's code does. The spike seals the +whole tree in one pass, with a descriptor for each field. So step 1.4 does +not need to keep the merge. Two parts stay as they are and join the record's one key request: @@ -472,12 +527,49 @@ Two parts stay as they are and join the record's one key request: JSON document is one scalar. - **A field that names a stored type**, until step 1.7 removes the resolver. -Decrypt is not changed in this step. Today's `record::decrypt` opened every -tree the experiment built. +Decrypt does not have to change in this step. Today's `record::decrypt` +opens every tree the spike builds. The spike also has `open_record`. It makes +one key request and one pass, and it opens what today's path stores. Removed: `into_raw_fields`, `lift`, `FieldShape`, and the hand-building in `shape_record`. +**The pull requests.** Step 1.4 reaches `next` in this order. Each pull +request stacks on the one before it. + +1. **The readability refactor (#2042).** Renames and moves in the dynamic + record path, with no change in behaviour. +2. **The AES cipher moves into `vitaminc-aead`.** A copy of the parts of + `vitaminc-encrypt` 0.10 that this repository calls, behind an `aes` + feature. The leaf format types become private. The deprecated `Aad` and + `AadPiece` names go. The AES backend wipes the plaintext before it + frees the buffer. It does not depend on the new design. +3. **`vitaminc-normalize` moves into this repository.** First an unchanged + copy, then its changes, as with `vitaminc-aead`. +4. **One walk for a value.** The encrypt sink holds no context. A staged + value is a `Node<Plaintext>`. A tree is sealed and opened by closure. + `AsyncCipher` and the other APIs the spike removed go here. This changes + `stack-encrypt`'s public API, and `eql-bindings` names a `stack-encrypt` + version. So that version reaches crates.io before `eql-bindings` is + published with it. +5. **The record sink takes over the record path.** It is given what the + table above lists. Then today's record path is deleted. This may take two or + three pull requests. +6. **Clean-ups.** The `CipherText` alias, the `E` parameter, whether + `decrypt_through` can merge with `open_opaque`, `KnownLogical`, + `UnknownLogicalId`, `vitaminc-aead`'s first decision record, and the + changelogs. + +Every one of these pull requests: + +- keeps stored bytes unchanged. The fixture tests, and the Go tests that + open records sealed before the change, pass unchanged; +- runs the full check list, not only the tests. The list is `cargo fmt`, + clippy, doc examples, and rustdoc. It also has the Go guests with + `go test`, the EQL crates, the fuzz builds, and mutation testing on the + changed lines; +- runs the AES tests on the RustCrypto backend as well as on aws-lc-rs. + Done when: - the record conformance suites pass with their expected files unchanged; @@ -858,9 +950,10 @@ codec. After step 1.3, that fuzzing covers the new node. ## Not in this plan -- The move of `vitaminc-normalize`, `vitaminc-prf`, and `vitaminc-ore` into - this repository. The steps above use their sinks as published. The match sink - can go in `stack-encrypt`, as the order sink is in `vitaminc-ore`. +- The move of `vitaminc-prf` and `vitaminc-ore` into this repository. The + steps above use their sinks as published. The match sink can go in + `stack-encrypt`, as the order sink is in `vitaminc-ore`. `vitaminc-normalize` + does move, in step 1.4. - A plan for a nested record. One walk makes it possible. Today a plan names top-level fields only. - The developer API of the TypeScript SDK. A sketch exists, and it belongs @@ -874,10 +967,9 @@ codec. After step 1.3, that fuzzing covers the new node. ## Open questions 1. Context rules: do a record's context rules belong in `stack-encrypt` or - in the cipher's data model? The experiment put them in a record sink in - `stack-encrypt` and changed no trait. The other way changes `MapSink` - and `SeqSink`, so that any source and any cipher can give an entry a - context. This plan assumes the first. + in the cipher's data model? Answered: in the record sink in + `stack-encrypt`, with no adapter sink in the caller. The spike changed no + sink trait. 2. Passthrough: does a passthrough node get a length? Step 1.3 proposes one. 3. A term outside a record: what does opening a tree do with a term node @@ -897,11 +989,12 @@ codec. After step 1.3, that fuzzing covers the new node. package of its own? Step 1.5 proposes `encrypt/frame`. Does the interface also make every type embed the default? With that rule, a new kind of node does not break a type that is written outside this repository. -9. The crates of the term sinks: when do they move? No step here needs the +9. The crates of the term sinks: when do they move? `vitaminc-normalize` + moves in step 1.4. No step here needs `vitaminc-prf` or `vitaminc-ore` to move. 10. Draft PR #2031: does it close? 11. The readability refactor of the dynamic record path: does it merge - before step 1.4? Step 1.4 rewrites much of that code. + before step 1.4? Answered: yes, as #2042, and step 1.4 stacks on it. 12. The type catalog: where does an SDK read the list of EQL types once no resolver answers? Step 1.6 proposes the generated code. 13. `eql-bindings`: is the removal of its public modules `encryption::query` @@ -920,3 +1013,8 @@ codec. After step 1.3, that fuzzing covers the new node. rewrites the path behind `Seal` and `Open`. The Go SDK's principle 7 says that no method that a user touches is called a plan or returns one. This question came from #2003. +17. `EncryptTarget`: does the record's plan still implement it? The first + decision says the plan builds an interim step with the combinators and + the cipher seals it. The spike walks the record with `RecordSink` and + seals with `seal_record`, and the plan implements no trait. Step 1.4 + needs an answer before its fifth pull request. From e0f53c02805cadcbc66d11f4ef3daf70326bc30b Mon Sep 17 00:00:00 2001 From: Dan Draper <dan@cipherstash.com> Date: Fri, 4 Dec 2026 18:55:34 +1100 Subject: [PATCH 09/15] docs(plans): declarations, the crate layout and the groundwork 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. --- ...1-one-walk-records-and-host-built-types.md | 762 ++++++++++++++---- 1 file changed, 627 insertions(+), 135 deletions(-) diff --git a/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md b/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md index 5894999cf..8b467fcb2 100644 --- a/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md +++ b/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md @@ -1,8 +1,8 @@ # One walk for a record, and host-built stored types -Status: draft, 1 December 2026, updated 3 December 2026. A spike of step -1.4 is in draft PR #2043. That PR is not for merging. Nothing else in this -plan is built. Decisions by Dan Draper: +Status: draft, 1 December 2026, updated 5 December 2026. A spike of the +encrypt, decrypt, and query paths is in draft PR #2043. That PR is not for +merging. Nothing else in this plan is built. Decisions by Dan Draper: - Encryption works as serde does. Any plaintext structure gives one `StackCipherText`. A record is a `Map`, and a batch of records is a @@ -19,22 +19,35 @@ plan is built. Decisions by Dan Draper: - A query comes back as a `StackCipherText` too. That is the second phase. - The public API may change. -Decisions from the spike, made on 2 December 2026 and 3 December 2026: - -- A record is walked once, by one sink that `stack-encrypt` builds from the - plan. The caller adds no adapter sink. Where the record sink needs - something from `vitaminc-aead`, `vitaminc-aead` changes. -- The encrypt sink holds no context. One function derives each leaf's +Decisions from the spike, made between 2 December 2026 and 5 December 2026: + +- A target says how it is encrypted with a declaration: a sink for one walk + of the value, and a finishing step. The combinators become adapters over + that one trait, as the standard library's iterator adapters are. This is + the interim step of the third decision above. +- One declaration of a field seals the field, opens it, and gives its query + terms. +- A record's plan builds a declaration at run time. The derive writes one + at compile time. Both use the same adapters. +- `plaintext.encrypt_into::<T>()` is to `EncryptTarget<S>` what `collect` is + to `FromIterator`. The impl of `EncryptTarget<S>` says which plaintext + types a target takes. +- The JSON index is one operation in a declaration. +- Today's combinators are removed when every target has moved: + `Encryption`, `Decryption`, and `KeysetCipher::run`. +- The encrypt sink has no context. One function derives each leaf's additional authenticated data (AAD) from the leaf's place in the tree. - A value staged for encryption is a `Node<Plaintext>`. Only `target::Pending` is called `Pending`. - `Cipher` is the only cipher trait. A tree is sealed with a closure, as it is opened. `AsyncCipher` is deleted. - The AES-256-GCM cipher moves into `vitaminc-aead`, and - `vitaminc-normalize` moves into this repository. Nothing here then depends on - `vitaminc-encrypt` 0.10. + `vitaminc-normalize` moves into this repository. Nothing here then depends + on `vitaminc-encrypt` 0.10. +- The two crates get the layout in [Crate layout](#crate-layout). +- `encrypt_tree` and `decrypt_tree` are removed. `run_decryption` is + private. - The readability refactor of the dynamic record path (#2042) merges first. - The pull requests of step 1.4 stack on it. Stored data fixes one format, so this plan does not choose it. An EQL value's `c` is the text `stack-encrypt:1:` followed by base64 of the sealed @@ -49,6 +62,8 @@ Code is cited as it stands on `next` at `54e647124`. - [The design](#the-design) - [What was checked](#what-was-checked) - [Checks to make first](#checks-to-make-first) +- [Crate layout](#crate-layout) +- [Groundwork](#groundwork) - [Phase 1: the record path](#phase-1-the-record-path) - [Phase 2: queries](#phase-2-queries) - [What does not change](#what-does-not-change) @@ -78,6 +93,13 @@ After this plan: - Stack Encrypt has no resolver. Its record form and its query form do not name EQL. The Go SDK has one encryption guest, not two. - A query is a tree of terms, read by the same deserializer. +- A target is declared once. That one declaration encrypts it, decrypts it, + and gives its query terms. +- A value with an index is walked once. Today each operation walks it. +- A Rust struct and a record from a language binding go through the same + declaration. +- The two crates have a layout a reader can follow: the three calls at the + top, and one place for each index. Rust still does the sealing, the term derivation, and the context binding. The format of a stored type is written once for each language. @@ -108,6 +130,23 @@ The pieces for one walk exist in `vitaminc-aead`: `Sink`, `Tee` no special wrapper". Stack Encrypt runs each piece alone, once for each field. +### A field is described three times, and its value is walked for each operation + +A field with an index has three descriptions today: + +- its encryption, an `Encryption` built from the combinators + (`packages/stack-encrypt/src/target/operations.rs:92`); +- its decryption, a `Decryption` (`operations.rs:108`); +- its query, the index's `query_operation` + (`packages/stack-encrypt/src/target/index.rs:292`). + +Only tests check that the three agree. + +Each operation also walks the value for itself. `indexed` joins a ciphertext +operation with one operation for each index (`index.rs:1019`), and each of +them reads the plaintext. The spike counted the walks of one value with an +equality index. There were two. + ### A stored type is built in Rust for every language A plan field can name a type such as `TextEq`. A resolver then runs the @@ -136,7 +175,7 @@ those four shapes has no form to use. | Part | Where it runs | What it does | |---|---|---| -| The walk | Rust, `stack-encrypt` | Visits the record once. A record sink gives each field the sinks its plan asks for | +| The walk | Rust, `stack-encrypt` | Visits the record once. The record's declaration gives each field the sinks its plan asks for | | Sealing | Rust | One key request for the whole call, then one sealed tree | | The tree | `vitaminc-aead` | `Single`, `Sequence`, `Map`, the two empty markers, `Passthrough`, and a new term node | | The crossing | `recipher-frame`, with its Go and TypeScript halves | The transport codec. The codec has no stability guarantee, so it can change | @@ -161,6 +200,91 @@ the type fixes. That is why step 1.1 comes first. The experiments checked equality and match only. The order types, `TextSearch`, and the JSON types are in the checks to make first. +### A declaration + +A declaration is a value that says how a plaintext becomes a target. It has +two parts: + +1. **A sink.** The plaintext is walked into it once. The sink stages what + the target needs: the plaintext tree to seal, and the normalized scalar + of each term. Nothing is sealed or keyed. The sink has no context. +2. **A finishing step.** It is handed what the walk staged, the cipher, and + one context. It requests the keys, seals the leaves, keys the terms, and + builds the target. + +A declaration is built as an iterator is built. An operation starts one. +Each adapter returns a small struct that is a declaration too. + +| Kind | Name | What it declares | +|---|---|---| +| Operation | `ciphertext()` | Seal the value | +| Operation | `equality()`, and one for each other index | Derive the index's term | +| Operation | `passthrough()` | Keep the value in the clear | +| Operation | `json_document(options)` | Seal a document under the JSON index | +| Adapter | `a.zip(b)` | Both, from one walk, under one context | +| Adapter | `a.map(f)`, `a.try_map(f)` | Convert the output | +| Adapter | `a.convert(to, from)` | Convert the output, with the inverse beside it, so that the target still opens | +| Adapter | `a.accepting::<C>()` | Take a context that converts to the one `a` needs | +| Adapter | `a.under(own)` | Run under a context of its own | +| Adapter | `a.entry("email")` | Read the entry of that name from a record | +| Adapter | `a.under_field("email")`, `fields.context_from("tenant")` | For a record whose context is the value of one of its fields | +| Adapter | `fields.record()` | Refuse an entry that no field takes | +| Collection | `Option<A>` | A part that a field has or does not have | +| Collection | `Fields<A>` | The fields of a record, collected from an iterator | + +A record of two fields is one expression. The derive writes it: + +```rust +indexed_equality().under(label("users/email")).entry("email") + .zip(ciphertext().under(label("users/age")).entry("age")) + .record() + .convert( + |(email, age)| EncryptedUser { email, age }, + |stored: EncryptedUser| (stored.email, stored.age), + ) +``` + +A plan builds the same declaration at run time: + +```rust +plan.fields().iter() + .map(|field| planned(field)?.under(own).entry(name)) + .collect::<Result<Fields<_>, _>>()? + .record() +``` + +`zip` cannot join a plan's fields. Each `zip` makes a new type, and the +number of a plan's fields is known only at run time. So every field of a +plan has one type, with an `Option` for each part a field may lack. The +fields are then collected into a `Fields`, as a `Vec` is collected from an +iterator. + +**One declaration serves three calls.** + +| Call | Trait | What the declaration gives | +|---|---|---| +| Encrypt | `Declaration` | The sink and the finishing step | +| Decrypt | `Open` | The stored target is taken apart under the one context, and one key request follows. Each tree is then opened, and its plaintext is walked into the sink of the output. That sink is a frame for a binding, or a Rust type | +| Query | `Answers<Op>` | `field.query::<Eq, _>()` is the declaration of the term that a query sends for `Eq`, under the field's own context | + +`map` and `try_map` are the two adapters that do not open, because a +function has no inverse. A target that has to open ends with `convert`. + +**What the compiler checks.** + +- **One context for each target.** `zip` asks both sides to name the same + context type. +- **The plaintext's type.** `EncryptTarget<S>` is this design's + `FromIterator<A>`. `plaintext.encrypt_into::<T>()` is its `collect::<B>()`. A target's impl says + which plaintext types it takes. For a field of a record, + `field(|user: &User| &user.email)` gives the compiler the field's type. +- **The operator.** A declaration with no index that answers `Eq` has no + `query::<Eq, _>()`. Two indexes that both answer one operator do not + compile either. Today that second case is refused at run time. + +A record from a language binding has no Rust type, so its checks are made +during the walk, as they are today. + ## What was checked Three experiments ran. The first two are on local branches. The third is @@ -191,37 +315,70 @@ draft PR #2043. None of them is for merging. The Go half needed no change in Rust. A plan field that lists `outputs: ["c", "eq"]` already comes back as a map with those two keys. -**One sink for a record, sealed in one pass (#2043).** The spike is built on -#2042. +**One declaration for encrypt, decrypt, and query (#2043).** The spike is +built on #2042. Its code is in four files of `packages/stack-encrypt/src`: +`target/declaration.rs`, `target/declaration/open.rs`, +`target/declaration/query.rs`, and `dynamic/record/one_sink.rs`. -A value and a record follow the same three steps. The walk gives a tree of -plaintext leaves. One key request follows. Then one pass seals the leaves -and keys the terms. - -| Check | Result | -|---|---| -| A record walked by `RecordSink`, with the context field last in the row | Every field has its context when the walk ends | -| One key request for the record, then one seal | Today's `record::decrypt` opens the result | -| The spike's `open_record` on a record that today's path stored | The record opens. Each path opens the other's records | -| A record sealed for one tenant, opened as another | Refused | -| A field the plan does not name, a missing field, and a scalar in place of a record | Each one refused | -| The value path, after `PendingStackCipherText` and the adapter cipher were removed | The fixture tests pass with their expected bytes unchanged. The Go tests that open records sealed before the spike pass | +Each row compares the declaration with today's code, on the same input. -The record sink needed two additions to `vitaminc-aead`, and no change to -`Sink`, `MapSink`, or `SeqSink`: +| Call | Case | Result | +|---|---|---| +| Encrypt | A ciphertext with an equality term | The same term, key requests, and contexts. One walk of the value. Today's combinators make two | +| Encrypt | A Rust struct of two fields | The same term. Each field is under its own context | +| Encrypt | A frame from a binding, through the struct's declaration | The same term. No field is copied first | +| Encrypt | A record of a plan: with a context field, with a label, and with nothing to seal | The same fields, outputs, term, and number of keys. Today's decrypt opens it | +| Encrypt | A JSON document: as JSON text, as a Rust struct, and as a map from a binding | The same selectors and number of entries. Today's decrypt opens it | +| Decrypt | A ciphertext with its term | Opens, with the same key requests and contexts | +| Decrypt | A stored struct | Opens into a Rust struct that derives `Decrypt`, and into a frame | +| Decrypt | A record of a plan, for the same three plans | The same plaintext and number of keys as today's decrypt | +| Query | A field of a struct | The term the record stores, and today's query term | +| Query | A field of a plan | Today's `query_term`, and the term the record stores | + +Each of these is refused, in every call it applies to: + +- a field the declaration does not name, and a missing field; +- a changed context field, and another context; +- a ciphertext moved to another field's place; +- an operator that the field's index has no term for; +- a query of a plan whose context is a field. Today's code refuses it too. + +The compiler refuses each of these. Each was tried by hand: + +- a pair whose sides name two context types; +- a number, for a target that takes text; +- a record field whose type does not fit the field's target; +- a query of a declaration with no index; +- an operator that the index does not answer; +- two indexes that both answer one operator. + +The spike needed two additions to `vitaminc-aead`, and no change to `Sink`, +`MapSink`, or `SeqSink`: - `Option<S>` is a sink when `S` is one, so a field can have a sink or none. - `EncryptSink::into(place)` writes its tree into a place the caller lends. -Each field gets its own key descriptor, from the one function that states a -field's context (`field_context`). That is the change the first experiment -did not try. +The spike does not cover: + +- match terms, order terms, and an operator whose query term is not the + stored term; +- nulls, batches, and a plan's scopes and bindings with values; +- the JSON queries, and the opening of a JSON document; +- a derive that writes declarations; +- `contexts()` without a run, and the kinds of error. Every refusal in the + spike is `Error::Aead`. -The record sink is in tests only. It does not yet cover: +The spike differs from today's code in four ways. Each is in the open +questions: -- type checks, nulls, absent fields, and batches; -- match terms, order terms, EQL targets, and the JSON index; -- queries, the expected context, and the kinds of error. +- Decrypt refuses a stored record that has no term. Today's decrypt ignores + terms. +- The value is walked before the context is checked. Today the context is + checked first. +- A typed `JsonPlaintext` is written to text and parsed again. Today's JSON + operation reads it in place. +- A function that returns a declaration has to state the type of its query + term in its return type. Neither of the first two experiments checked these: @@ -309,6 +466,304 @@ So the two host types differ by one rule. `TextMatch` refuses a match term that has no positions, and `TextSearch` accepts it. The check covers that rule, and a value that is built from three terms. +## Crate layout + +`stack-encrypt` and `vitaminc-aead` are hard to follow today. The numbers +are from branch `spike/one-sink`. + +| | `stack-encrypt` | `vitaminc-aead` | +|---|---:|---:| +| Source files | 68 | 35 | +| Lines of code and docs | 45,700 | 9,400 | +| Public top-level modules | 18 | 8 | +| Names re-exported at the crate root | 97 | 63 | +| Pairs of top-level modules that use each other | 14 | 5 | + +In `stack-encrypt`, `target` uses 12 of the other top-level modules, so +there is no order to read the crate in. One word names several places: +`contexts.rs`, `target/context.rs`, and `dynamic/context.rs`. One index is +in three modules: the equality index is in `target/index.rs`, `op/mod.rs`, +and `sem/mod.rs`. + +### The rules + +Each rule is a test that can be applied to any file. + +1. **The three calls are top-level modules:** `encrypt`, `decrypt`, and + `query`. The first file of each has the call a reader is following. +2. **A type is with its data. Its methods are with the call they + provide.** The `KeysetCipher` struct is in `cipher/`. Its encrypt methods + are in `encrypt/mod.rs`, its decrypt methods in `decrypt/mod.rs`, and its + query methods in `query/mod.rs`. Today its methods are in seven `impl` + blocks in six files. +3. **The declaration design is laid out as `core::iter` is:** a `traits/` + folder, an `operations/` folder, and an `adapters/` folder with one file + for each adapter. +4. **One index is in one place:** its operation, its term, and the + operators it answers. +5. **A module uses only the modules below it.** +6. **A test block longer than about 300 lines moves to a sibling + `tests.rs`.** +7. **The derive's output names one module, `__private`.** The derive has 78 + paths into `target::` today. With one module, a later move does not + change the derive. + +### `stack-encrypt` + +``` +lib.rs the crate docs: the three calls. 29 names at the root. + +encrypt/ a plaintext becomes a target +decrypt/ a stored target becomes a plaintext +query/ a typed query becomes a query term + +index/ each index: its operation, its term, what it answers +operator/ the operators a query can ask (Eq, Lt, Matches, ..) +cipher/ the client, its keysets, and the key requests of one call +sealed/ what is stored: Ciphertext, StackCipherText, the sealed leaf +context/ every context type +value/ the plaintext types this crate adds + +dynamic/ what a language binding calls: frames in, frames out, + and the plan that describes a record as data + +error/ Error, and the refusals of a caller's input +__private the names the derive's output uses (hidden) +``` + +That is 11 public modules. + +``` +encrypt/ + mod.rs the keyset's encrypt methods, EncryptInto, run + traits/ + declaration.rs Declaration + target.rs EncryptTarget<S>, and field() + operations/ + ciphertext.rs ciphertext(), with its finishing step + passthrough.rs passthrough() + adapters/ one file each: zip, map, try_map, convert, accepting, under, + entry, record, fields, option, under_field, context_from + targets.rs EncryptTarget for Ciphertext and StackCipherText + batch.rs Vec<S> into Vec<T>, and Option<S> into Option<T> + +decrypt/ + mod.rs the keyset's and the cipher's decrypt methods, DecryptFrom + traits.rs Open, DecryptSource + targets.rs DecryptSource for Ciphertext and StackCipherText + batch.rs Vec<T> into Vec<P>, and Option<T> into Option<P> + +query/ + mod.rs FieldQuery and its encrypt call, Answers, + the keyset's JSON query methods + checks.rs what a query is refused for, before any term exists + field_reference/ mod.rs, json.rs, predicate_checks.rs + +index/ + mod.rs Index, Indexes, IndexSpec, Encrypted<Terms>, indexed() + supports.rs which operators an index answers + equality.rs Equality, equality(), EqualityTerm + matching/ Match, matching(), MatchTerms; tokenize.rs + order.rs Ore, Ope, OreBlock, their operations and terms + json/ the JSON index: json_document(), its walk, its seal, its queries + term_error.rs + +cipher/ + mod.rs builder.rs + keyset.rs KeysetCipher: the struct, how it is made, its state + keyset_cache.rs + pending/ Pending: mod.rs, combine.rs, request.rs, dispatch.rs, future.rs + audit.rs credential.rs randomness.rs + +sealed/ ciphertext.rs, tree.rs, leaves.rs, transcode.rs +context/ mod.rs, input.rs, descriptor.rs, label.rs, display.rs, testing.rs +value/ mod.rs, json.rs, blob.rs + +dynamic/ + mod.rs record.rs + plan/ mod.rs, kind.rs, target.rs, declaration.rs, check.rs, + source.rs, error.rs + query/ mod.rs, json.rs, field_handle.rs + term.rs json.rs frame_context.rs cipher_directed.rs + +error/ mod.rs, refusal.rs, found.rs +``` + +Three points about `encrypt/`: + +- **The entry points are in `mod.rs`.** They are `keyset.encrypt`, + `keyset.encrypt_with_context`, `value.encrypt_into`, and + `value.encrypt_into_with_context`. `encrypt_tree` is the same call as + `encrypt_into_with_context::<StackCipherText>`, so it is removed. It is + used in 80 places. Its removal is a pull request of its own. +- **An index's operation is with its index.** `equality()` and + `json_document()` are in `index/`. So `encrypt` names no index, and a new + index adds no file to it. The standard library does the same: the + iterator over a string's characters is in `core::str`. +- **The impls for the built-in targets are in `targets.rs`.** The + standard library keeps `FromIterator for Vec` with `Vec`, because `Vec` + is above `iter`. Here `Ciphertext` is below `encrypt`: the `ciphertext()` + operation names the type it builds. An impl beside the type would make + `sealed` and `encrypt` use each other. + +`decrypt/` is small, because the adapters are shared. Each adapter's file in +`encrypt/adapters/` has its `Open` impl too. + +The operators are a top-level module, renamed from `op`. They cannot be +inside `query/`. Each index says which operators it answers, so `index` uses +the operators, and `query` uses `index`. + +### `vitaminc-aead` + +``` +lib.rs the verbs and the Cipher trait. About 26 names at the root. + +plaintext/ how a value describes itself + mod.rs Encrypt, EncryptRef, Scalar, Sink, SeqSink, MapSink + kinds.rs value_kind.rs path.rs depth.rs + sources/ std.rs, date.rs, decimal.rs, timestamp.rs, uuid.rs + adapters/ tee.rs, lent.rs, either.rs, option.rs +encrypt/ mod.rs: encrypt(); sink.rs: EncryptSink; shape.rs +seal/ mod.rs: Cipher, Node::seal, open_with; node.rs: Node; + local/: the stored layout of a leaf +decrypt/ mod.rs: decrypt(); walk.rs; target.rs: Decrypt, FromScalar; + sinks/: one file for each sink +leaf/ mod.rs: scalars as bytes; tags.rs +frame/ as today +aes/ the AES-256-GCM cipher, with nonce.rs +aad.rs +``` + +`typed.rs` (1,647 lines) is the decrypt side. It is split into `decrypt/`. + +### The order of the modules + +A module uses only the modules to its left: + +``` +context, value < sealed < cipher < operator < index < encrypt, decrypt < query < dynamic +``` + +`error` is the exception: every module may use it. + +| | Today | After | +|---|---:|---:| +| `stack-encrypt`: pairs of modules that use each other | 14 | 1 | +| `vitaminc-aead` | 5 | 2 | + +The pair that stays in `stack-encrypt` is `error` and `index`. + +### The crate root + +A name is at the root of `stack-encrypt` if a typical program needs it for +one of five things: + +- to build a client; +- to call encrypt or decrypt; +- to name what it encrypts into; +- to pass a context; +- to handle an error. + +| Group | Names | +|---|---| +| The client | `StackCipher`, `StackCipherBuilder`, `KeysetCipher`, `kms` | +| The calls | `EncryptInto`, `DecryptFrom`, `Pending` | +| What a program encrypts into | `Ciphertext`, `StackCipherText`, `Encrypted`, `OpaqueBlob` | +| Traits for a program's own types | `EncryptTarget`, `DecryptSource`, `Encrypt`, `Decrypt` | +| Contexts | `Label`, `CallerContext`, `IntoContext`, `NonEmpty`, `nonempty!` | +| Secrets | `Protected`, `Controlled` | +| Audit and credentials | `AuditMetadata`, `Audited`, `WithCredential` | +| Errors | `Error`, `ErrorPayload`, `diagnostic` | +| Other | `VERSION` | + +That is 29 names. The other 68 of today's 97 are reached through a module: + +| Goes to | Names | +|---|---| +| `index::` | `Equality`, `Match`, `Ope`, `Ore`, `OreBlock`, `Json`, `Index`, `Indexes`, `IndexSpec`, `TermSet`, `Orderable` | +| `query::` | `FieldReference`, `FieldQuery`, `BetweenQuery`, `Needs`, `QueryInput`, `Ready`, `ScopeInput` | +| `context::` | `Descriptor`, `Describe`, `Description`, `LabelError`, `Context`, `ContextPiece`, `IntoAad`, `IntoPrfContext`, `PrfContext`, `MaybeEmpty`, `EmptyError`, `nonempty_bytes!`, and the nine types that show contexts | +| `cipher::` | `NoKms`, `WithKeySource`, `WithAuthStrategy`, `KeySource`, `OwnIdentity`, `CipherRef`, `PendingFuture`, `Request`, `Responses`, `AuditValue` | +| `sealed::` | `SealedValue`, `LeafBytesError`, `CipherText`, `Node`, `PassthroughFrame` | +| `decrypt::` | `DecryptField`, `Decryptable` | +| `value::` | `BlobError`, `OpaqueEncode`, `OpaqueDecode` | +| `dynamic::` | `PlanError`, and the re-exports `Buffer`, `ProtectedRef`, and `AsProtectedRef` | +| No re-export | `EncryptRef`, `ScalarTarget`, `DecodeError`, `Unspecified`. A program takes them from `vitaminc-aead` | +| Deleted with today's combinators | `Encryption`, `Decryption` | + +`Buffer`, `ProtectedRef`, and `AsProtectedRef` come from +`vitaminc-protected`. The functions a binding calls take them, so `dynamic` +re-exports them. A binding's author then gets the matching version with no +second dependency. + +### Renames + +| Old | New | Reason | +|---|---|---| +| The `target` module | Removed | "Target" is a glossary word for a type. The module has 13 files on about six subjects | +| `sem` | `index` | The glossary has "index" and "term". It has no "SEM" | +| `op` | `operator` | The whole word | +| `contexts` | `context::display` | It shows contexts | +| `json/derive.rs` | Four files in `index/json/` | In this crate "derive" means the macro | +| `typed`, `encrypt_sink`, `container`, and `walk`, in `vitaminc-aead` | `decrypt`, `encrypt::sink`, `seal::node`, and `decrypt::walk` | Each new name says what the file has | + +### What the layout costs + +- **Public paths change.** `sem::*` becomes `index::*`. `json::*` becomes + `index::json::*`. `field_reference` becomes `query::field_reference`. + `eql-bindings` uses all three. +- **The root loses 68 names.** +- **Both designs exist for a time.** Today's combinators stay in + `target/operations.rs` until they are deleted. Only new code goes into + `encrypt/`. + +## Groundwork + +These pull requests do not change how a record is encrypted. They can start +now. Each one stacks on #2042. + +| | What | State | +|---|---|---| +| G1 | The readability refactor of the dynamic record path | #2042, ready to merge | +| G2 | The AES-256-GCM cipher moves into `vitaminc-aead` | #2044, draft | +| G3 | `vitaminc-normalize` moves into this repository | Not started | +| G4 | Long test blocks move to `tests.rs` files | Not started | +| G5 | The layout of `vitaminc-aead` | Not started | +| G6 | The layout of the shared modules of `stack-encrypt` | Not started | + +**G2, the AES cipher.** `vitaminc-aead` gets the parts of `vitaminc-encrypt` +0.10 that this repository calls, behind an `aes` feature. The pull request +also makes these changes: + +- The leaf format types become private to `vitaminc-aead`. The deprecated + `Aad` and `AadPiece` names are removed. +- The AES backend no longer frees a copy of the plaintext without wiping + it. A seal that fails wipes its buffer. +- A compiler flag chooses the RustCrypto backend for tests on a native + target. A Cargo feature did this before. CI runs the workspace's tests + with every feature on, so a feature would have changed the backend for + every test. +- CI runs the tests of `vitaminc-aead` on `wasm32-wasip1`. That is the + target of the Go guests. +- `aws-lc-rs` gets an audit in the `cargo vet` store, and the publisher of + `aws-lc-sys` is trusted. + +**G3, `vitaminc-normalize`.** First an unchanged copy of the published +crate, then its changes, as with `vitaminc-aead`. Every user of the API that +changes is in this repository. + +**G4 to G6, the layout.** Each is a move with no change in behaviour: + +1. The long test blocks. +2. `vitaminc-aead`. +3. `error`, `context`, `value`, and `sealed`. +4. `cipher/`. +5. `index/`, renamed from `sem`. +6. `__private`, with the derive. + +The rest of the layout comes with step 1.4: `encrypt/`, `decrypt/`, +`query/`, and `dynamic/plan/`. + ## Phase 1: the record path | Step | What | Depends on | @@ -316,7 +771,7 @@ rule, and a value that is built from three terms. | 1.1 | Go's plan carries match options | Nothing | | 1.2 | The tree codec takes any leaf | Nothing | | 1.3 | A term node, and a length on a passthrough node | #2003, 1.2, and an answer to open question 3 | -| 1.4 | One walk of a record | #2042. The walk writes term nodes once 1.3 is in | +| 1.4 | One walk: declarations | The groundwork. A plan's declaration writes term nodes once 1.3 is in | | 1.5 | A deserializer and a visitor in Go and TypeScript | #2003, 1.3 | | 1.6 | The stored types in each language | 1.1, 1.5, and each type's check above | | 1.7 | The resolver removed | 1.6 done for every EQL v4 type | @@ -474,91 +929,75 @@ not trees. Done when Rust and Go agree on the new bytes in the shared corpora, and every existing record test passes with terms as term nodes. -### 1.4 One walk of a record - -The record's plan becomes an `EncryptTarget`. Encrypting a record is one -walk of the source into a record sink, one key request, and one seal. - -The spike (#2043) does this with one sink. Open question 17 asks whether -the plan still has to be an `EncryptTarget` as well. - -How the walk works, as the spike does it: - -- **A record sink in `stack-encrypt`, built from the plan.** The record sink - sends each field's value to the sinks the plan names for the field. Those - are the encrypt sink and a term sink for each index. When the walk ends, - the record sink settles each field's context. -- **The encrypt sink holds no context.** It gives a tree of plaintext - leaves, a `Node<Plaintext>`. One function derives each leaf's AAD from its - place when the tree is sealed or opened. -- **One key request, then one pass.** The request has a key descriptor for - each field. The pass seals every leaf and keys every term. -- **No trait changes.** `Sink`, `MapSink`, and `SeqSink` stay as they are. - The caller has no adapter sink. The first experiment needed one, about 70 - lines. The spike needed two small additions to `vitaminc-aead` in its - place (see [What was checked](#what-was-checked)). -- **A batch.** Each row is its own root, as in the first experiment. So a - row is sealed as if it were alone, as today. The spike has no batches yet. - -What has to be built beyond the spike: - -| Piece | Today | In the walk | +### 1.4 One walk: declarations + +Every target is declared with a `Declaration` +([A declaration](#a-declaration)). A record's plan builds one, and the +derive writes one. Encrypting a record is three steps: one walk of the +source into the declaration's sink, one key request, and one pass. The pass +seals the leaves and keys the terms. + +**The pull requests.** Each one stacks on the one before it. + +1. **One walk for a value.** + - The encrypt sink has no context. + - A staged value is a `Node<Plaintext>`. + - A tree is sealed and opened by closure. + - `AsyncCipher` and the other APIs that the spike removed go here. + - This changes the public API of `stack-encrypt`. `eql-bindings` names a + `stack-encrypt` version, so that version reaches crates.io before + `eql-bindings` is published with it. +2. **The trait and its adapters.** `Declaration`, `ciphertext()`, + `passthrough()`, and the adapters, in `encrypt/`. Today's combinators + stay where they are. +3. **Decrypt.** `Open`, and `decrypt/`. +4. **Each index as a declaration:** its operation, and the operators it + answers (`Answers`). The order is equality, order, match, and the JSON + index. +5. **The targets.** The built-in targets and the 19 targets of + `eql-bindings` return a declaration from `EncryptTarget<S>`. +6. **The derive** writes a declaration for a record, and the walk of the + plaintext struct. +7. **The plan** builds a declaration. The dynamic record path encrypts, + decrypts, and queries through it. This may take two or three pull + requests. +8. **Removals and clean-ups.** + - Today's combinators are deleted. + - `encrypt_tree` and `decrypt_tree` are removed, and `run_decryption` + becomes private. + - The root has its 29 names. + - The smaller clean-ups: the `CipherText` alias, the `E` parameter, + `KnownLogical`, and `UnknownLogicalId`. + - `decrypt_through` and `open_opaque`, merged if they are one function. + - The first decision record of `vitaminc-aead`, and the changelogs. + +**What a plan's declaration needs beyond the spike:** + +| Piece | Today | In a declaration | |---|---|---| -| Equality terms | `terms(value)` alone, for one scalar (`packages/stack-encrypt/src/sem/mod.rs:664`) | The `Terms` sink of `vitaminc-normalize`, in the `Tee` | -| Order terms | `order_terms(target, value)` alone (`sem/mod.rs:1935`) | `Terms::by(Order::new(target))`, in the `Tee` | -| Match terms | No sink. `MatchTerms` takes text | A small sink that keeps a wiped copy of the text, as `match_deferred` does (`sem/mod.rs:1450`) | +| Equality terms | `terms(value)` alone, for one scalar (`packages/stack-encrypt/src/sem/mod.rs:664`) | `equality()`. The spike has it | +| Order terms | `order_terms(target, value)` alone (`sem/mod.rs:1935`) | An operation with the sink `Terms::by(Order::new(target))` | +| Match terms | No sink. `MatchTerms` takes text | An operation whose sink keeps a wiped copy of the text, as `match_deferred` does (`sem/mod.rs:1450`) | +| A query term that is not the stored term | The index's `query_operation` | The `Query` type of `Answers`. `between` sends two terms | | The declared type | `check_field` reads the whole value and converts it before sealing | A sink in front of the others | -| Key descriptors | One descriptor for every key in a tree (`packages/stack-encrypt/src/target/core.rs:105`, and `:161` on open) | A descriptor for each field's part of the tree, as the spike has. The ZeroKMS protocol already takes one for each key (`packages/stack-kms/src/payload.rs:7`) | -| Nulls, scopes, bindings, passthrough | Handled for each field | Handled by the record sink. The spike has a context field and passthrough, and none of the others | - -The walk needs no key for any term. A sink produces a normalised encoding, -and the keyed step runs afterwards with the keyset. - -**The seal.** The first experiment sealed each field through its own -pending and merged the pendings, as today's code does. The spike seals the -whole tree in one pass, with a descriptor for each field. So step 1.4 does -not need to keep the merge. +| Key descriptors | One descriptor for every key in a tree (`packages/stack-encrypt/src/target/core.rs:105`, and `:161` on open) | A descriptor for each field. The spike has it. The ZeroKMS protocol already takes one for each key (`packages/stack-kms/src/payload.rs:7`) | +| Nulls, and a field with no entry | Handled for each field | A rule of the field's declaration | +| A batch | A sequence of records | A declaration for a sequence, with each row its own root | +| A plan's scopes and bindings | In the context of each field | The context the declaration is handed. The spike passes the plan's, with no values | +| The kinds of error | A refusal names its field and its reason | The walk's errors have to keep both. The spike has one error | Two parts stay as they are and join the record's one key request: -- **The JSON index.** Its walk needs the keyset, because it derives - selectors and tokens under the index key - (`packages/stack-encrypt/src/json/derive.rs:848`). To the record's walk, a - JSON document is one scalar. -- **A field that names a stored type**, until step 1.7 removes the resolver. - -Decrypt does not have to change in this step. Today's `record::decrypt` -opens every tree the spike builds. The spike also has `open_record`. It makes -one key request and one pass, and it opens what today's path stores. - -Removed: `into_raw_fields`, `lift`, `FieldShape`, and the hand-building in -`shape_record`. - -**The pull requests.** Step 1.4 reaches `next` in this order. Each pull -request stacks on the one before it. - -1. **The readability refactor (#2042).** Renames and moves in the dynamic - record path, with no change in behaviour. -2. **The AES cipher moves into `vitaminc-aead`.** A copy of the parts of - `vitaminc-encrypt` 0.10 that this repository calls, behind an `aes` - feature. The leaf format types become private. The deprecated `Aad` and - `AadPiece` names go. The AES backend wipes the plaintext before it - frees the buffer. It does not depend on the new design. -3. **`vitaminc-normalize` moves into this repository.** First an unchanged - copy, then its changes, as with `vitaminc-aead`. -4. **One walk for a value.** The encrypt sink holds no context. A staged - value is a `Node<Plaintext>`. A tree is sealed and opened by closure. - `AsyncCipher` and the other APIs the spike removed go here. This changes - `stack-encrypt`'s public API, and `eql-bindings` names a `stack-encrypt` - version. So that version reaches crates.io before `eql-bindings` is - published with it. -5. **The record sink takes over the record path.** It is given what the - table above lists. Then today's record path is deleted. This may take two or - three pull requests. -6. **Clean-ups.** The `CipherText` alias, the `E` parameter, whether - `decrypt_through` can merge with `open_opaque`, `KnownLogical`, - `UnknownLogicalId`, `vitaminc-aead`'s first decision record, and the - changelogs. +- **The JSON index's own walk.** It derives selectors and tokens under the + index key (`packages/stack-encrypt/src/json/derive.rs:848`), so it needs + the keyset. It runs in the finishing step of `json_document()`, over the + document that the sink staged. +- **A field that names a stored type**, until step 1.7 removes the + resolver. + +Removed from the dynamic record path: `into_raw_fields`, `lift`, +`FieldShape`, the hand-building in `shape_record`, `compose`, and +`FieldValues`. Every one of these pull requests: @@ -568,7 +1007,7 @@ Every one of these pull requests: clippy, doc examples, and rustdoc. It also has the Go guests with `go test`, the EQL crates, the fuzz builds, and mutation testing on the changed lines; -- runs the AES tests on the RustCrypto backend as well as on aws-lc-rs. +- runs the AES tests on both backends. Done when: @@ -576,6 +1015,9 @@ Done when: - a record sealed before the change opens after it, and the reverse; - the tests that count key requests still count one for a batch; - no field's value is copied into a frame of its own; +- a value with an index is walked once; +- every target has one declaration, and nothing uses `Encryption` or + `Decryption`; - in a record of several fields, a leaf moved to another field's position does not open; - a field sealed under one descriptor or lock context does not open under @@ -797,7 +1239,8 @@ Done when all of these are true: Phase 2 needs steps 1.3, 1.5, and 1.7. After step 1.7 no resolver builds a query, so the answer has one form to change. Phase 2 does not need step -1.4. +1.4. Step 1.4 changes how a query term is derived: from the field's own +declaration. Phase 2 changes the form the answer crosses in. ### 2.1 The dynamic query path returns a tree @@ -887,8 +1330,10 @@ conformance suites pass on the new form. - **What a stored value authenticates.** A map's membership and a sequence's order are still not bound. A passthrough field is still not authenticated. -- **Rust's typed API.** The derive emits combinators only, and the - `eql-bindings` types stay. +- **How a Rust target is written.** The derive still writes one expression + of combinators for a record, and the `eql-bindings` types stay. The + combinators are declarations after step 1.4, and their paths change with + the layout. ## Checks that must move with the code @@ -907,7 +1352,11 @@ conformance suites pass on the new form. | `scripts/__tests__/stack-exchange-conformance.test.mjs` and `scripts/__tests__/go-binding-test.test.mjs` | 1.7 | Both require the task `wasm:guest:build:eql:deterministic` (`:378` and `:124`) | | The EQL guest builds in `.github/workflows/tests-golang.yml` | 1.7 | Removed (`:242` and `:245`) | | The suites that read a query's operand | 2.3 | Move to the tree form | -| The mutation gate on `stack-encrypt` | 1.4 | New sinks need tests that fail under each mutant | +| The mutation gate on `stack-encrypt` | 1.4 | New sinks and adapters need tests that fail under each mutant | +| The compiler-message snapshots in `packages/stack-encrypt/tests/ui` | 1.4 | Regenerated. A declaration gives other messages than an `Encryption` | +| A compile-fail test for each rule the compiler checks | 1.4 | New: one context for each target, the plaintext's type, and the operator. The spike tried each by hand | +| The heap tests of `stack-encrypt` and EQL | G2, 1.4 | Must pass on both AES backends, with no free excluded | +| `supply-chain/vitaminc-aead` | G2 | The `cargo vet` store and `deny.toml` cover the AES backends | Issue #2004 is the differential fuzzing of the Go codec against the Rust codec. After step 1.3, that fuzzing covers the new node. @@ -940,11 +1389,22 @@ codec. After step 1.3, that fuzzing covers the new node. `i`. They are rules of the EQL types. Each language then has a copy, so the conformance suite has to cover both. - **Decision records.** This plan needs a record of its own. That record - changes what three records of `stack-encrypt` describe + changes what four records of `stack-encrypt` describe (`packages/stack-encrypt/docs/adr`): - the target model (0003); + - one context for each target, threaded through the declaration tree + (0004). The rule stays. The context becomes an associated type of a + declaration, where it is a type parameter of `Encryption` today; - how a binding enters (0007, with its amendment for the EQL guest); - the query term (0009). + + The first decision record of `vitaminc-aead` describes `AsyncCipher` and + the sink that has a context. Both go in step 1.4. +- **The targets of `eql-bindings`, and the derive.** 19 types in + `eql-bindings` implement `EncryptTarget`, and the derive's output names 78 + paths in `target::`. Step 1.4 changes both. +- **Public paths.** The layout moves `sem`, `json`, and `field_reference`, + and takes 68 names off the root. See [Crate layout](#crate-layout). - **Draft PR #2031.** That PR builds typed records that cross through serde. This plan did not choose that form. @@ -953,7 +1413,7 @@ codec. After step 1.3, that fuzzing covers the new node. - The move of `vitaminc-prf` and `vitaminc-ore` into this repository. The steps above use their sinks as published. The match sink can go in `stack-encrypt`, as the order sink is in `vitaminc-ore`. `vitaminc-normalize` - does move, in step 1.4. + does move: it is G3 of the groundwork. - A plan for a nested record. One walk makes it possible. Today a plan names top-level fields only. - The developer API of the TypeScript SDK. A sketch exists, and it belongs @@ -967,9 +1427,9 @@ codec. After step 1.3, that fuzzing covers the new node. ## Open questions 1. Context rules: do a record's context rules belong in `stack-encrypt` or - in the cipher's data model? Answered: in the record sink in - `stack-encrypt`, with no adapter sink in the caller. The spike changed no - sink trait. + in the cipher's data model? Answered: in `stack-encrypt`, in the + adapters of a declaration (`under`, `under_field`, and `context_from`). + The spike changed no sink trait. 2. Passthrough: does a passthrough node get a length? Step 1.3 proposes one. 3. A term outside a record: what does opening a tree do with a term node @@ -980,9 +1440,9 @@ codec. After step 1.3, that fuzzing covers the new node. 5. `eql-codegen`: where does it read each type's outputs, its match options, and the prefix on `c`? Today the options are a Rust constant in `eql-bindings`. -6. The derive: does a static record move to one walk too, after phase 1? A - static record also emits one operation for each field - (`packages/stack-encrypt-derive/src/owned_encrypt.rs:447`). +6. The derive: does a static record move to one walk too, after phase 1? + Answered: yes, in step 1.4. The derive writes a declaration, as a plan + builds one. 7. Text in a query: what is the tree form of a JSON operator's answer? The JSON operators return text, not bytes. Nobody has tried them as a tree. 8. Go's visitor: is it in `encrypt/frame`, beside the codec, or in a @@ -1013,8 +1473,40 @@ codec. After step 1.3, that fuzzing covers the new node. rewrites the path behind `Seal` and `Open`. The Go SDK's principle 7 says that no method that a user touches is called a plan or returns one. This question came from #2003. -17. `EncryptTarget`: does the record's plan still implement it? The first - decision says the plan builds an interim step with the combinators and - the cipher seals it. The spike walks the record with `RecordSink` and - seals with `seal_record`, and the plan implements no trait. Step 1.4 - needs an answer before its fifth pull request. +17. `EncryptTarget`: does the record's plan still implement it? Answered: + the third decision stands. The plan builds a declaration from the + combinators, and the cipher runs it. +18. The main calls: how are the keyset's `encrypt` and `decrypt` overloaded + through traits, so that one method serves a scalar, a tree, and a + record? `EncryptTarget<S>` chooses the declaration for encrypt. Decrypt + has no trait like it yet. +19. Decrypt's type check: what tells the compiler that a target opens to a + given plaintext type? Encrypt has `EncryptTarget<S>`. +20. A stored record with no term: does decrypt ignore the missing term? + Today's decrypt ignores terms. The spike's decrypt refuses a record + that lacks one, so step 1.4 has to change that. +21. The order of the checks: does the context have to be checked before the + plaintext is read? It is today. In the spike the walk comes first, so a + refused context drops a staged value. The staged value is wiped. +22. `contexts()`: how does a declaration report its contexts without a run, + as `Encryption::contexts` does? The spike does not have it. +23. The owned mode: how does a declaration wipe a plaintext it is given by + value? Today `SourceMode` does this. +24. Return types: is a long return type acceptable for a declaration written + by hand? A function that returns a declaration has to state its query + term's type, as in + `Answers<Eq, Right<Here>, Query: Declaration<Output = EqualityTerm>>`. + A derive writes that. +25. `field(..)` and `entry(..)`: is it enough that a derive writes both + from one name? The compiler does not check that the closure and the + name mean the same field. +26. The JSON index's walk: does it become a sink? In the spike the sink + stages the document, and the index's own walk runs in the finishing + step. A sink could stage each node's path, kind, and canonical bytes, + and leave only the keyed work to the finishing step. No other part of + the design depends on the answer. +27. JSON from a walked value: what does the JSON index do with a scalar + that JSON has no kind for? A date and bytes are two examples. +28. A typed `JsonPlaintext`: can the declaration read it in place, as + today's operation does? The spike writes it to text and parses it + again. From 90b1c10c29bf50924ddf5e9ebd002fee8a64aea0 Mon Sep 17 00:00:00 2001 From: Dan Draper <dan@cipherstash.com> Date: Fri, 4 Dec 2026 18:56:52 +1100 Subject: [PATCH 10/15] docs(plans): the update and the spike decisions are dated 4 December The last commit dated both a day late. --- .../plans/2026-12-01-one-walk-records-and-host-built-types.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md b/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md index 8b467fcb2..ab0538179 100644 --- a/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md +++ b/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md @@ -1,6 +1,6 @@ # One walk for a record, and host-built stored types -Status: draft, 1 December 2026, updated 5 December 2026. A spike of the +Status: draft, 1 December 2026, updated 4 December 2026. A spike of the encrypt, decrypt, and query paths is in draft PR #2043. That PR is not for merging. Nothing else in this plan is built. Decisions by Dan Draper: @@ -19,7 +19,7 @@ merging. Nothing else in this plan is built. Decisions by Dan Draper: - A query comes back as a `StackCipherText` too. That is the second phase. - The public API may change. -Decisions from the spike, made between 2 December 2026 and 5 December 2026: +Decisions from the spike, made between 2 December 2026 and 4 December 2026: - A target says how it is encrypted with a declaration: a sink for one walk of the value, and a finishing step. The combinators become adapters over From daf68ff9684b5d89e2b5e808824325a85de9ea63 Mon Sep 17 00:00:00 2001 From: Dan Draper <dan@cipherstash.com> Date: Fri, 4 Dec 2026 20:02:40 +1100 Subject: [PATCH 11/15] docs(plans): vitaminc-normalize moves with its history, in #2052 The plan said the move was an unchanged copy of the published crate, followed by the crate's changes. The move in #2052 follows the vitaminc-aead move: the crate comes with its git history, at the code of its 0.10.0 release. The crate's own changes follow from the vitaminc-aead changes in step 1.4, so they go there. The supply-chain store does not cover the crate yet, and the plan now says what that needs. --- ...01-one-walk-records-and-host-built-types.md | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md b/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md index ab0538179..843857323 100644 --- a/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md +++ b/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md @@ -726,7 +726,7 @@ now. Each one stacks on #2042. |---|---|---| | G1 | The readability refactor of the dynamic record path | #2042, ready to merge | | G2 | The AES-256-GCM cipher moves into `vitaminc-aead` | #2044, draft | -| G3 | `vitaminc-normalize` moves into this repository | Not started | +| G3 | `vitaminc-normalize` moves into this repository | #2052, draft | | G4 | Long test blocks move to `tests.rs` files | Not started | | G5 | The layout of `vitaminc-aead` | Not started | | G6 | The layout of the shared modules of `stack-encrypt` | Not started | @@ -748,9 +748,19 @@ also makes these changes: - `aws-lc-rs` gets an audit in the `cargo vet` store, and the publisher of `aws-lc-sys` is trusted. -**G3, `vitaminc-normalize`.** First an unchanged copy of the published -crate, then its changes, as with `vitaminc-aead`. Every user of the API that -changes is in this repository. +**G3, `vitaminc-normalize`.** The crate comes with its git history, at the +code of its 0.10.0 release, as `vitaminc-aead` did +(`docs/plans/2026-11-14-move-vitaminc-aead-into-the-stack.md`). The move +changes no library code. One test changes, because it read the Debug output +of an error that `vitaminc-aead` prints differently here. + +The crate's own changes are not in this step. They follow from the +`vitaminc-aead` changes in the first pull request of step 1.4, so they go +there. Every user of the API that changes is in this repository. + +The `cargo vet` and `cargo deny` store does not cover the crate yet. That +needs three decisions: a build script to allow, and two crates to audit or +exempt. #2052 lists them. **G4 to G6, the layout.** Each is a move with no change in behaviour: From a3eacac507f708f92d60026ad3ca9b53e52671b2 Mon Sep 17 00:00:00 2001 From: Dan Draper <dan@cipherstash.com> Date: Sat, 5 Dec 2026 15:40:49 +1100 Subject: [PATCH 12/15] docs(plans): re-pin the citations to next after #2042 The plan's file:line citations now point at next at b939de404, the merge of #2042. They were read at 54e647124. #2042 moved the dynamic record code. The match options' object form and EQL_KEY are now in dynamic/record/plan.rs, and bytes_node and the record decrypt doc are further up dynamic/record.rs. #2003 shifted lines in the Go SDK's query.go and records.go, and the EQL guest builds in tests-golang.yml. Every citation was updated. None could not be. --- ...1-one-walk-records-and-host-built-types.md | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md b/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md index 843857323..5dad142c1 100644 --- a/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md +++ b/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md @@ -53,7 +53,7 @@ Stored data fixes one format, so this plan does not choose it. An EQL value's `c` is the text `stack-encrypt:1:` followed by base64 of the sealed leaf. Production data has that form. -Code is cited as it stands on `next` at `54e647124`. +Code is cited as it stands on `next` at `b939de404`. ## Contents @@ -120,7 +120,7 @@ one walk. The dynamic record path builds one operation for each field, and `shape_record` uses `FieldShape` to take the output back out. - **Terms lose their type.** `shape_record` stores each term as a passthrough node of bytes (`bytes_node`, - `packages/stack-encrypt/src/dynamic/record.rs:2385`). + `packages/stack-encrypt/src/dynamic/record.rs:713`). The pieces for one walk exist in `vitaminc-aead`: `Sink`, `Tee` (`packages/vitaminc-aead/src/plaintext/tee.rs`), `EncryptSink::root` @@ -168,7 +168,7 @@ a type that Rust has. (`packages/stack-encrypt/src/dynamic/query.rs:281`). Its operand has one of four fixed shapes (`:290`). The answer crosses through serde. Each language SDK has a second reader for the answer, such as Go's `parseQueryTerm` -(`languages/golang/encrypt/query.go:184`). A query with more structure than +(`languages/golang/encrypt/query.go:197`). A query with more structure than those four shapes has no form to use. ## The design @@ -807,7 +807,7 @@ in a plan means `k` = 3 and `m` = 256. For the text "Ada Lovelace" that is that asks for it. Rust's plan accepts the options in an object form -(`packages/stack-encrypt/src/dynamic/record.rs:1206`). Go's plan cannot send +(`packages/stack-encrypt/src/dynamic/record/plan.rs:1055`). Go's plan cannot send them, for two reasons: - Go has no option to give. `MatchOption` is an interface with no @@ -875,7 +875,7 @@ What the node gives every reader: are the same kind of node. Only a passthrough can contain plaintext. - A reader can tell a term by its kind. Record decrypt does not change in this plan. It takes the `c` and `passthrough` outputs by the plan's keys - today (`packages/stack-encrypt/src/dynamic/record.rs:1852`). + today (`packages/stack-encrypt/src/dynamic/record.rs:381`). - A reader takes a length and then the bytes. A new kind of term needs no change to the codec in any language. The limit @@ -1122,7 +1122,7 @@ questions. Rules for both: - **One parser for each language.** Outside tests, Go decodes the tree in - one place (`languages/golang/encrypt/records.go:93`, through a wrapper). + one place (`languages/golang/encrypt/records.go:94`, through a wrapper). After #2003, the function that call reaches is `UnmarshalCipherText` in `encrypt/frame`. That function becomes a visitor on the new reader, or it is removed when the caller moves. @@ -1207,7 +1207,7 @@ Removed from `stack-encrypt`: `:1196`, and `:1248`). The branch of `target_limit` for an EQL type's match options is removed too. - The record's `eql` output key (`EQL_KEY`, - `packages/stack-encrypt/src/dynamic/record.rs:177`), and the refusal + `packages/stack-encrypt/src/dynamic/record/plan.rs:25`), and the refusal `NoEqlNode`. - The `"eql"` form of a query's answer (`query.rs:392`), and the SQL domain in the answer. Both exist because a resolver returns finished EQL JSON. @@ -1307,7 +1307,7 @@ marked deprecated, and build it from the new tree. Three facts apply: - A program cannot call `Cipher.QueryTerm` itself. The method takes a plan type from an `internal` package - (`languages/golang/encrypt/query.go:140`). + (`languages/golang/encrypt/query.go:153`). - A generated file calls `Term`, `Range`, and `NullTerm` in `gensupport` (`languages/golang/encrypt/gensupport/field.go:121`, `:131`, and `:143`). Each returns a term's bytes. @@ -1360,7 +1360,7 @@ conformance suites pass on the new form. | A drift check for the generated TypeScript types | 1.6 | New, like `types:check` for Go (`.github/workflows/test-eql.yml:768`) | | `scripts/__tests__/go-release-artifact.test.mjs` | 1.7 | It expects five guests (`:166`). The number becomes three | | `scripts/__tests__/stack-exchange-conformance.test.mjs` and `scripts/__tests__/go-binding-test.test.mjs` | 1.7 | Both require the task `wasm:guest:build:eql:deterministic` (`:378` and `:124`) | -| The EQL guest builds in `.github/workflows/tests-golang.yml` | 1.7 | Removed (`:242` and `:245`) | +| The EQL guest builds in `.github/workflows/tests-golang.yml` | 1.7 | Removed (`:246` and `:249`) | | The suites that read a query's operand | 2.3 | Move to the tree form | | The mutation gate on `stack-encrypt` | 1.4 | New sinks and adapters need tests that fail under each mutant | | The compiler-message snapshots in `packages/stack-encrypt/tests/ui` | 1.4 | Regenerated. A declaration gives other messages than an `Encryption` | @@ -1477,8 +1477,8 @@ codec. After step 1.3, that fuzzing covers the new node. 16. `Cipher`'s methods that take a plan: do they change in step 1.4? Six methods beside `QueryTerm` take a `*record.Plan` from `internal/record`: `Verify`, `TargetLimit`, `Contexts`, `Seal`, `Open`, and `JSONOpen` - (`languages/golang/encrypt/query.go:292`, - `languages/golang/encrypt/records.go:47`). A program outside the module + (`languages/golang/encrypt/query.go:305`, + `languages/golang/encrypt/records.go:48`). A program outside the module cannot name that type, so only generated code calls them. Step 1.4 rewrites the path behind `Seal` and `Open`. The Go SDK's principle 7 says that no method that a user touches is called a plan or returns From 157df8502e924f849e89c7a1a2192ffafed251da Mon Sep 17 00:00:00 2001 From: Dan Draper <dan@cipherstash.com> Date: Sat, 5 Dec 2026 15:41:21 +1100 Subject: [PATCH 13/15] docs(plans): wrap the line the new plan.rs path made long --- .../plans/2026-12-01-one-walk-records-and-host-built-types.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md b/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md index 5dad142c1..15ee08ec3 100644 --- a/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md +++ b/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md @@ -807,8 +807,8 @@ in a plan means `k` = 3 and `m` = 256. For the text "Ada Lovelace" that is that asks for it. Rust's plan accepts the options in an object form -(`packages/stack-encrypt/src/dynamic/record/plan.rs:1055`). Go's plan cannot send -them, for two reasons: +(`packages/stack-encrypt/src/dynamic/record/plan.rs:1055`). Go's plan +cannot send them, for two reasons: - Go has no option to give. `MatchOption` is an interface with no implementation, and `encrypt.Match` ignores its arguments From a36f9060ce30dd8d995ee1237a73309194e9ba7a Mon Sep 17 00:00:00 2001 From: Dan Draper <dan@cipherstash.com> Date: Sat, 5 Dec 2026 16:01:25 +1100 Subject: [PATCH 14/15] docs(plans): the AES cipher and the leaf layout move into stack-encrypt #2044 moved the AES-256-GCM cipher into vitaminc-aead. Its only caller outside tests is stack-encrypt, which builds a one-key Aes256Cipher for each leaf inside its own key-per-leaf cipher, and seeds a random number generator for each leaf while doing so. Step 1.4's first pull request now moves the cipher into stack-encrypt as crate-private functions, with the stored layout of a leaf. vitaminc-aead then has no encryption primitive and no key. Before that, cargo deny and cargo vet cover stack-encrypt (#1842), so that aws-lc-rs, aws-lc-sys and aes-gcm stay checked. Two open questions are new: where the leaf layout sits in stack-encrypt, and what the two tests outside stack-encrypt that build an Aes256Cipher seal with afterwards. --- ...1-one-walk-records-and-host-built-types.md | 68 ++++++++++++++++--- 1 file changed, 59 insertions(+), 9 deletions(-) diff --git a/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md b/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md index 15ee08ec3..5764a2793 100644 --- a/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md +++ b/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md @@ -1,6 +1,6 @@ # One walk for a record, and host-built stored types -Status: draft, 1 December 2026, updated 4 December 2026. A spike of the +Status: draft, 1 December 2026, updated 5 December 2026. A spike of the encrypt, decrypt, and query paths is in draft PR #2043. That PR is not for merging. Nothing else in this plan is built. Decisions by Dan Draper: @@ -19,7 +19,7 @@ merging. Nothing else in this plan is built. Decisions by Dan Draper: - A query comes back as a `StackCipherText` too. That is the second phase. - The public API may change. -Decisions from the spike, made between 2 December 2026 and 4 December 2026: +Decisions from the spike, made between 2 December 2026 and 5 December 2026: - A target says how it is encrypted with a declaration: a sink for one walk of the value, and a finishing step. The combinators become adapters over @@ -44,6 +44,13 @@ Decisions from the spike, made between 2 December 2026 and 4 December 2026: - The AES-256-GCM cipher moves into `vitaminc-aead`, and `vitaminc-normalize` moves into this repository. Nothing here then depends on `vitaminc-encrypt` 0.10. +- The AES-256-GCM cipher then moves into `stack-encrypt`, as crate-private + functions, not a trait. The stored layout of a leaf moves with it. + `vitaminc-aead` then has no encryption primitive and no key. It has the + tree format, the walk, and the `Cipher` trait. This was decided on + 5 December 2026, after #2044 had moved the cipher into `vitaminc-aead`. +- `cargo deny` and `cargo vet` cover `stack-encrypt` before the cipher moves + there, so that its AES dependencies stay checked. - The two crates get the layout in [Crate layout](#crate-layout). - `encrypt_tree` and `decrypt_tree` are removed. `run_decryption` is private. @@ -519,8 +526,10 @@ query/ a typed query becomes a query term index/ each index: its operation, its term, what it answers operator/ the operators a query can ask (Eq, Lt, Matches, ..) -cipher/ the client, its keysets, and the key requests of one call +cipher/ the client, its keysets, the key requests of one call, and + the AES-256-GCM functions that seal and open a leaf sealed/ what is stored: Ciphertext, StackCipherText, the sealed leaf + and its stored layout context/ every context type value/ the plaintext types this crate adds @@ -574,8 +583,10 @@ cipher/ keyset_cache.rs pending/ Pending: mod.rs, combine.rs, request.rs, dispatch.rs, future.rs audit.rs credential.rs randomness.rs + aes/ seal() and open() for one leaf, the nonce, and the two + backends: aws_lc.rs and rust_crypto.rs -sealed/ ciphertext.rs, tree.rs, leaves.rs, transcode.rs +sealed/ ciphertext.rs, tree.rs, leaves.rs, transcode.rs, leaf_layout.rs context/ mod.rs, input.rs, descriptor.rs, label.rs, display.rs, testing.rs value/ mod.rs, json.rs, blob.rs @@ -624,18 +635,19 @@ plaintext/ how a value describes itself sources/ std.rs, date.rs, decimal.rs, timestamp.rs, uuid.rs adapters/ tee.rs, lent.rs, either.rs, option.rs encrypt/ mod.rs: encrypt(); sink.rs: EncryptSink; shape.rs -seal/ mod.rs: Cipher, Node::seal, open_with; node.rs: Node; - local/: the stored layout of a leaf +seal/ mod.rs: Cipher, Node::seal, open_with; node.rs: Node decrypt/ mod.rs: decrypt(); walk.rs; target.rs: Decrypt, FromScalar; sinks/: one file for each sink leaf/ mod.rs: scalars as bytes; tags.rs frame/ as today -aes/ the AES-256-GCM cipher, with nonce.rs aad.rs ``` `typed.rs` (1,647 lines) is the decrypt side. It is split into `decrypt/`. +The AES cipher and the stored layout of a leaf are not in this tree. They +move to `stack-encrypt` in step 1.4. + ### The order of the modules A module uses only the modules to its left: @@ -724,8 +736,8 @@ now. Each one stacks on #2042. | | What | State | |---|---|---| -| G1 | The readability refactor of the dynamic record path | #2042, ready to merge | -| G2 | The AES-256-GCM cipher moves into `vitaminc-aead` | #2044, draft | +| G1 | The readability refactor of the dynamic record path | #2042, merged | +| G2 | The AES-256-GCM cipher moves into `vitaminc-aead` | #2044, merged | | G3 | `vitaminc-normalize` moves into this repository | #2052, draft | | G4 | Long test blocks move to `tests.rs` files | Not started | | G5 | The layout of `vitaminc-aead` | Not started | @@ -748,6 +760,16 @@ also makes these changes: - `aws-lc-rs` gets an audit in the `cargo vet` store, and the publisher of `aws-lc-sys` is trusted. +The cipher does not stay in `vitaminc-aead`. Its only caller outside tests +is `stack-encrypt`. For each leaf, `stack-encrypt` builds an `Aes256Cipher` +from that leaf's data key (`leaf_cipher`, +`packages/stack-encrypt/src/cipher.rs`). So `stack-encrypt` wraps a one-key +cipher inside a cipher of its own. Its own cipher picks a key for each +leaf. + +Each new `Aes256Cipher` also seeds a random number generator from system +entropy, once for each leaf. Step 1.4 moves the cipher into `stack-encrypt`. + **G3, `vitaminc-normalize`.** The crate comes with its git history, at the code of its 0.10.0 release, as `vitaminc-aead` did (`docs/plans/2026-11-14-move-vitaminc-aead-into-the-stack.md`). The move @@ -954,6 +976,24 @@ seals the leaves and keys the terms. - A staged value is a `Node<Plaintext>`. - A tree is sealed and opened by closure. - `AsyncCipher` and the other APIs that the spike removed go here. + - The AES-256-GCM cipher moves into `stack-encrypt` as crate-private + functions. One seals a leaf under a key and an AAD. The other opens + one. The backend is still chosen by the target, so there is no trait. + `vitaminc-aead` loses its `aes` feature and its `aes` module. + - The stored layout of a leaf moves with it. That is `LocalCipherText`, + the writer, the reader, `WIRE_VERSION`, the AAD derivation `for_leaf`, + and the nonce generator. The stored bytes do not change. + - The nonce generator is seeded once for each call, not once for each + leaf. + - The JSON index (`packages/stack-encrypt/src/json/derive.rs`) moves to + the new functions. Two tests outside `stack-encrypt` build an + `Aes256Cipher` too: the EQL test `opaque_json_bytes.rs`, and the + recipher-js test addon. They cannot call crate-private functions. See + the open questions. + - Before this pull request, `cargo deny` and `cargo vet` cover + `stack-encrypt` (#1842). Otherwise aws-lc-rs, aws-lc-sys, and + aes-gcm leave the only graph the two tools check, and the audits + recorded in G2 apply to nothing. - This changes the public API of `stack-encrypt`. `eql-bindings` names a `stack-encrypt` version, so that version reaches crates.io before `eql-bindings` is published with it. @@ -1367,6 +1407,8 @@ conformance suites pass on the new form. | A compile-fail test for each rule the compiler checks | 1.4 | New: one context for each target, the plaintext's type, and the operator. The spike tried each by hand | | The heap tests of `stack-encrypt` and EQL | G2, 1.4 | Must pass on both AES backends, with no free excluded | | `supply-chain/vitaminc-aead` | G2 | The `cargo vet` store and `deny.toml` cover the AES backends | +| The supply-chain checks of `stack-encrypt` (#1842) | 1.4 | New, before the AES cipher moves there. They cover aws-lc-rs, aws-lc-sys, and aes-gcm | +| The tests of the AES cipher, the wasm32 job, and the backend flag `vitaminc_aead_rust_crypto` | 1.4 | Move to `stack-encrypt` with the cipher. The flag is renamed | Issue #2004 is the differential fuzzing of the Go codec against the Rust codec. After step 1.3, that fuzzing covers the new node. @@ -1520,3 +1562,11 @@ codec. After step 1.3, that fuzzing covers the new node. 28. A typed `JsonPlaintext`: can the declaration read it in place, as today's operation does? The spike writes it to text and parses it again. +29. The stored layout of a leaf: is `sealed/` the right module for it in + `stack-encrypt`, or does it sit with the AES functions in `cipher/aes/`? + Only the AES functions write it. The other stored types are in + `sealed/`. +30. The two tests outside `stack-encrypt` that build an `Aes256Cipher`: + what do they seal with once the cipher is crate-private? A cipher for + tests in `vitaminc-aead`, or a `stack-encrypt` feature for tests, are + two options. From 3b704db44270b7ec107b135231997bb5a48ee2e9 Mon Sep 17 00:00:00 2001 From: Dan Draper <dan@cipherstash.com> Date: Sat, 5 Dec 2026 16:48:05 +1100 Subject: [PATCH 15/15] docs(plans): summary first, a terms section, and clearer pronouns The plan opened with about 25 decision bullets before its summary, and used terms such as spike, sink, declaration, resolver, and visitor before saying what they mean. The summary now comes straight after the contents. A terms section defines the words a reader needs, and the decisions follow in a section of their own. The groundwork labels say that G stands for groundwork. Fifteen sentences that began with a pronoun that could point at more than one thing now name what they mean. No decision, step, or question changed. --- ...1-one-walk-records-and-host-built-types.md | 218 ++++++++++-------- 1 file changed, 127 insertions(+), 91 deletions(-) diff --git a/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md b/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md index 5764a2793..42240cbc6 100644 --- a/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md +++ b/docs/plans/2026-12-01-one-walk-records-and-host-built-types.md @@ -1,8 +1,88 @@ # One walk for a record, and host-built stored types -Status: draft, 1 December 2026, updated 5 December 2026. A spike of the -encrypt, decrypt, and query paths is in draft PR #2043. That PR is not for -merging. Nothing else in this plan is built. Decisions by Dan Draper: +Status: draft, 1 December 2026, updated 5 December 2026. The decisions in +[Decisions](#decisions) are Dan Draper's. The steps are a proposal. + +A spike of the encrypt, decrypt, and query paths is in draft PR #2043. That +PR is not for merging. Nothing else in this plan is built. Code is cited as +it stands on `next` at `b939de404`. + +## Contents + +- [Summary](#summary) +- [Terms](#terms) +- [Decisions](#decisions) +- [Why](#why) +- [The design](#the-design) +- [What was checked](#what-was-checked) +- [Checks to make first](#checks-to-make-first) +- [Crate layout](#crate-layout) +- [Groundwork](#groundwork) +- [Phase 1: the record path](#phase-1-the-record-path) +- [Phase 2: queries](#phase-2-queries) +- [What does not change](#what-does-not-change) +- [Checks that must move with the code](#checks-that-must-move-with-the-code) +- [What this changes elsewhere](#what-this-changes-elsewhere) +- [Not in this plan](#not-in-this-plan) +- [Open questions](#open-questions) + +## Summary + +Today Stack Encrypt seals a record one field at a time and builds the +record's tree by hand afterwards. A field that is stored as an EQL type is +formatted in Rust by a resolver, for every language. A query comes back in a +form of its own. + +After this plan: + +- A record is walked once. Each field's ciphertext and terms come from that + walk, and the result is one `StackCipherText`. +- The tree has a node for a term. +- The tree crosses to Go and TypeScript in the transport codec. Each + language reads the tree with one deserializer. The deserializer calls a + visitor. +- A stored type, such as EQL's `TextEq`, is defined in the language that + uses it. Its visitor builds the stored value from the tree. `eql-codegen` + writes these types from the EQL catalog. +- Stack Encrypt has no resolver. Its record form and its query form do not + name EQL. The Go SDK has one encryption guest, not two. +- A query is a tree of terms, read by the same deserializer. +- A target is declared once. That one declaration encrypts it, decrypts it, + and gives its query terms. +- A value with an index is walked once. Today each operation walks it. +- A Rust struct and a record from a language binding go through the same + declaration. +- The two crates have a layout a reader can follow: the three calls at the + top, and one place for each index. + +Rust still does the sealing, the term derivation, and the context binding. +The format of a stored type is written once for each language. + +## Terms + +| Term | Meaning | +|---|---| +| Stack Encrypt, `stack-encrypt` | The Rust library that encrypts values and derives search terms | +| EQL | Encrypt Query Language: the SQL library that stores and queries encrypted values in PostgreSQL | +| Language binding | The interface between the Rust code and Go or TypeScript | +| Record | A set of named fields encrypted together, such as one database row | +| Plan | The description of a record that a language binding sends as data: its fields, and each field's indexes and context | +| Target | The type a plaintext is encrypted into, such as `Ciphertext` or an EQL type | +| Walk | One pass over a value. It reports each scalar, map, and sequence, in order, to a sink | +| Sink | What a walk reports to. A sink stages what an operation needs from the value | +| Declaration | A value that says how a plaintext becomes a target: a sink for the walk, and a finishing step. See [A declaration](#a-declaration) | +| Term | The bytes an index derives from a value, such as an equality term. A record stores terms beside the ciphertext, and a query sends them | +| Leaf | One sealed scalar in the encrypted tree | +| `StackCipherText` | The encrypted tree: leaves, maps, sequences, and passthrough values. Step 1.3 adds a node for a term | +| AAD | Additional authenticated data: bytes that a leaf is sealed under and must be opened under. Here they come from the leaf's context and its place in the tree | +| Resolver | The code in `eql-bindings` that builds an EQL type's stored form in Rust today, for every language | +| Deserializer and visitor | In Go or TypeScript, the deserializer reads the tree and calls the visitor for each node. The visitor builds the value | +| Spike | Code written to test the design, in draft PR #2043. It is not for merging | +| G1 to G6 | The six groundwork pull requests. See [Groundwork](#groundwork) | + +## Decisions + +Decisions by Dan Draper: - Encryption works as serde does. Any plaintext structure gives one `StackCipherText`. A record is a `Map`, and a batch of records is a @@ -23,8 +103,8 @@ Decisions from the spike, made between 2 December 2026 and 5 December 2026: - A target says how it is encrypted with a declaration: a sink for one walk of the value, and a finishing step. The combinators become adapters over - that one trait, as the standard library's iterator adapters are. This is - the interim step of the third decision above. + that one trait, as the standard library's iterator adapters are. A + declaration is the interim step that the third decision names. - One declaration of a field seals the field, opens it, and gives its query terms. - A record's plan builds a declaration at run time. The derive writes one @@ -47,8 +127,9 @@ Decisions from the spike, made between 2 December 2026 and 5 December 2026: - The AES-256-GCM cipher then moves into `stack-encrypt`, as crate-private functions, not a trait. The stored layout of a leaf moves with it. `vitaminc-aead` then has no encryption primitive and no key. It has the - tree format, the walk, and the `Cipher` trait. This was decided on - 5 December 2026, after #2044 had moved the cipher into `vitaminc-aead`. + tree format, the walk, and the `Cipher` trait. The move into + `stack-encrypt` was decided on 5 December 2026, after #2044 had moved the + cipher into `vitaminc-aead`. - `cargo deny` and `cargo vet` cover `stack-encrypt` before the cipher moves there, so that its AES dependencies stay checked. - The two crates get the layout in [Crate layout](#crate-layout). @@ -60,57 +141,6 @@ Stored data fixes one format, so this plan does not choose it. An EQL value's `c` is the text `stack-encrypt:1:` followed by base64 of the sealed leaf. Production data has that form. -Code is cited as it stands on `next` at `b939de404`. - -## Contents - -- [Summary](#summary) -- [Why](#why) -- [The design](#the-design) -- [What was checked](#what-was-checked) -- [Checks to make first](#checks-to-make-first) -- [Crate layout](#crate-layout) -- [Groundwork](#groundwork) -- [Phase 1: the record path](#phase-1-the-record-path) -- [Phase 2: queries](#phase-2-queries) -- [What does not change](#what-does-not-change) -- [Checks that must move with the code](#checks-that-must-move-with-the-code) -- [What this changes elsewhere](#what-this-changes-elsewhere) -- [Not in this plan](#not-in-this-plan) -- [Open questions](#open-questions) - -## Summary - -Today Stack Encrypt seals a record one field at a time and builds the -record's tree by hand afterwards. A field that is stored as an EQL type is -formatted in Rust by a resolver, for every language. A query comes back in a -form of its own. - -After this plan: - -- A record is walked once. Each field's ciphertext and terms come from that - walk, and the result is one `StackCipherText`. -- The tree has a node for a term. -- The tree crosses to Go and TypeScript in the transport codec. Each - language reads the tree with one deserializer. The deserializer calls a - visitor. -- A stored type, such as EQL's `TextEq`, is defined in the language that - uses it. Its visitor builds the stored value from the tree. `eql-codegen` - writes these types from the EQL catalog. -- Stack Encrypt has no resolver. Its record form and its query form do not - name EQL. The Go SDK has one encryption guest, not two. -- A query is a tree of terms, read by the same deserializer. -- A target is declared once. That one declaration encrypts it, decrypts it, - and gives its query terms. -- A value with an index is walked once. Today each operation walks it. -- A Rust struct and a record from a language binding go through the same - declaration. -- The two crates have a layout a reader can follow: the three calls at the - top, and one place for each index. - -Rust still does the sealing, the term derivation, and the context binding. -The format of a stored type is written once for each language. - ## Why ### A record is sealed field by field @@ -172,11 +202,11 @@ a type that Rust has. ### A query has its own output form `dynamic::query_term` returns a `QueryTerm` -(`packages/stack-encrypt/src/dynamic/query.rs:281`). Its operand has one of -four fixed shapes (`:290`). The answer crosses through serde. Each language -SDK has a second reader for the answer, such as Go's `parseQueryTerm` -(`languages/golang/encrypt/query.go:197`). A query with more structure than -those four shapes has no form to use. +(`packages/stack-encrypt/src/dynamic/query.rs:281`). A `QueryTerm`'s operand +has one of four fixed shapes (`:290`). The answer crosses through serde. +Each language SDK has a second reader for the answer, such as Go's +`parseQueryTerm` (`languages/golang/encrypt/query.go:197`). A query with +more structure than those four shapes has no form to use. ## The design @@ -200,7 +230,7 @@ The field's context is the plan's, as it is today. The resolver uses the plan's parts under the field's label (`packages/eql/crates/eql-bindings/src/encryption/targets.rs:448`). So a plain field `email` under `users` has the same context as the EQL type -`TextEq` for `users.email`. Its equality term is therefore the same. +`TextEq` for `users.email`. So the two have the same equality term. A match term is the same only when the plan field asks for the options that the type fixes. That is why step 1.1 comes first. The experiments checked @@ -282,9 +312,10 @@ function has no inverse. A target that has to open ends with `convert`. - **One context for each target.** `zip` asks both sides to name the same context type. - **The plaintext's type.** `EncryptTarget<S>` is this design's - `FromIterator<A>`. `plaintext.encrypt_into::<T>()` is its `collect::<B>()`. A target's impl says - which plaintext types it takes. For a field of a record, - `field(|user: &User| &user.email)` gives the compiler the field's type. + `FromIterator<A>`. `plaintext.encrypt_into::<T>()` is its + `collect::<B>()`. A target's impl says which plaintext types it takes. For + a field of a record, `field(|user: &User| &user.email)` gives the compiler + the field's type. - **The operator.** A declaration with no index that answers `Eq` has no `query::<Eq, _>()`. Two indexes that both answer one operator do not compile either. Today that second case is refused at run time. @@ -323,9 +354,10 @@ The Go half needed no change in Rust. A plan field that lists `outputs: ["c", "eq"]` already comes back as a map with those two keys. **One declaration for encrypt, decrypt, and query (#2043).** The spike is -built on #2042. Its code is in four files of `packages/stack-encrypt/src`: -`target/declaration.rs`, `target/declaration/open.rs`, -`target/declaration/query.rs`, and `dynamic/record/one_sink.rs`. +built on #2042. The spike's code is in four files of +`packages/stack-encrypt/src`: `target/declaration.rs`, +`target/declaration/open.rs`, `target/declaration/query.rs`, and +`dynamic/record/one_sink.rs`. Each row compares the declaration with today's code, on the same input. @@ -605,8 +637,9 @@ Three points about `encrypt/`: - **The entry points are in `mod.rs`.** They are `keyset.encrypt`, `keyset.encrypt_with_context`, `value.encrypt_into`, and `value.encrypt_into_with_context`. `encrypt_tree` is the same call as - `encrypt_into_with_context::<StackCipherText>`, so it is removed. It is - used in 80 places. Its removal is a pull request of its own. + `encrypt_into_with_context::<StackCipherText>`, so it is removed. + `encrypt_tree` is used in 80 places, so removing it is a pull request of + its own. - **An index's operation is with its index.** `equality()` and `json_document()` are in `index/`. So `encrypt` names no index, and a new index adds no file to it. The standard library does the same: the @@ -732,7 +765,8 @@ second dependency. ## Groundwork These pull requests do not change how a record is encrypted. They can start -now. Each one stacks on #2042. +now. Each is labelled G, for groundwork, and a number. G1 and G2 have +merged into `next`. The others are based on it. | | What | State | |---|---|---| @@ -948,7 +982,8 @@ readers call those methods (`:194` to `:215`). Nothing in the tree calls them, and they stay as they are. The new method takes a term's bytes. Only the tree's reader calls it -(`:182`). Its default refuses, as the default of every other method does. +(`:182`). The new method's default refuses, as the default of every other +method does. This plan adds no conversion from a tree's bytes to a typed term. The types that read a term from a tree are in Go and TypeScript. A Rust type that has @@ -994,9 +1029,9 @@ seals the leaves and keys the terms. `stack-encrypt` (#1842). Otherwise aws-lc-rs, aws-lc-sys, and aes-gcm leave the only graph the two tools check, and the audits recorded in G2 apply to nothing. - - This changes the public API of `stack-encrypt`. `eql-bindings` names a - `stack-encrypt` version, so that version reaches crates.io before - `eql-bindings` is published with it. + - The pull request changes the public API of `stack-encrypt`. + `eql-bindings` names a `stack-encrypt` version, so that version reaches + crates.io before `eql-bindings` is published with it. 2. **The trait and its adapters.** `Declaration`, `ciphertext()`, `passthrough()`, and the adapters, in `encrypt/`. Today's combinators stay where they are. @@ -1009,7 +1044,7 @@ seals the leaves and keys the terms. 6. **The derive** writes a declaration for a record, and the walk of the plaintext struct. 7. **The plan** builds a declaration. The dynamic record path encrypts, - decrypts, and queries through it. This may take two or three pull + decrypts, and queries through it. Step 7 may take two or three pull requests. 8. **Removals and clean-ups.** - Today's combinators are deleted. @@ -1054,7 +1089,7 @@ Every one of these pull requests: - keeps stored bytes unchanged. The fixture tests, and the Go tests that open records sealed before the change, pass unchanged; - runs the full check list, not only the tests. The list is `cargo fmt`, - clippy, doc examples, and rustdoc. It also has the Go guests with + clippy, doc examples, and rustdoc. The list also has the Go guests with `go test`, the EQL crates, the fuzz builds, and mutation testing on the changed lines; - runs the AES tests on both backends. @@ -1305,7 +1340,7 @@ sequences. | A range, `lo` and `hi` | The same map, with a sequence of two terms | | Text, and a list of text (the JSON operators) | Not settled. See the open questions | -The operator, the fidelity, and the index leave the answer. They describe +The operator, the fidelity, and the index leave the answer. All three describe the plan, not one query. An SDK already asks for them in a separate call, `field_operators`. Go parses that answer in `parseOperators` (`languages/golang/encrypt/checker.go:295`). The SQL domain and the `eql` @@ -1359,8 +1394,8 @@ So the shim matters most for a generated file that was written before the change. Nobody has checked whether steps 1.6 and 1.7 already change that layout. See the open questions. -This is not a small change. `QueryTerm` and its operand are public. Outside -tests, 12 Rust source files name `QueryTerm` today: seven in +Removing `QueryTerm` is not a small change. `QueryTerm` and its operand are +public. Outside tests, 12 Rust source files name `QueryTerm` today: seven in `stack-encrypt`, two in `eql-bindings`, and three in the Go guest. `packages/eql/crates/eql-codegen/src/targets.rs` names it too, and so do six files of the Go SDK. Three conformance suites read the operand. @@ -1416,11 +1451,11 @@ codec. After step 1.3, that fuzzing covers the new node. ## What this changes elsewhere - **The TypeScript SDK plan** - (`docs/plans/2026-11-22-recipher-typescript-sdk.md`). Its EQL extension - becomes TypeScript types with visitors, with no extension inside the - cipher. A query value comes from the query type, not from the `domain` of - a `QueryTerm`. The plan says that a `match` index takes no options. That - is no longer true for a type that fixes them. + (`docs/plans/2026-11-22-recipher-typescript-sdk.md`). That plan's EQL + extension becomes TypeScript types with visitors, with no extension inside + the cipher. A query value comes from the query type, not from the `domain` + of a `QueryTerm`. The plan says that a `match` index takes no options. + That is no longer true for a type that fixes them. - **The Go SDK.** `encrypt/eql` no longer embeds a guest. The release workflow's list of guests changes in step 1.7, and so does the description of the guests in `AGENTS.md`. @@ -1438,8 +1473,8 @@ codec. After step 1.3, that fuzzing covers the new node. Two refusals also move from Rust into generated host code: empty match text (`EmptyTermText`, `packages/eql/crates/eql-bindings/src/encryption.rs:420`) and the stored - `i`. They are rules of the EQL types. Each language then has a copy, so - the conformance suite has to cover both. + `i`. Both refusals are rules of the EQL types. Each language then has a + copy, so the conformance suite has to cover both. - **Decision records.** This plan needs a record of its own. That record changes what four records of `stack-encrypt` describe (`packages/stack-encrypt/docs/adr`): @@ -1538,8 +1573,9 @@ codec. After step 1.3, that fuzzing covers the new node. Today's decrypt ignores terms. The spike's decrypt refuses a record that lacks one, so step 1.4 has to change that. 21. The order of the checks: does the context have to be checked before the - plaintext is read? It is today. In the spike the walk comes first, so a - refused context drops a staged value. The staged value is wiped. + plaintext is read? Today's code checks the context first. In the spike + the walk comes first, so a refused context drops a staged value. The + staged value is wiped. 22. `contexts()`: how does a declaration report its contexts without a run, as `Encryption::contexts` does? The spike does not have it. 23. The owned mode: how does a declaration wipe a plaintext it is given by