Repository navigation
Description
How Stack Encrypt handles plaintext today
Stack Encrypt (packages/stack-encrypt) is the Rust engine that every CipherStash language SDK runs on. It encrypts values with Vitamin C (cipherstash/vitaminc), CipherStash's Rust cryptography library.
A plaintext type in Vitamin C is deliberately not Clone. A copy that nobody tracks is a copy nobody wipes. Vitamin C 0.10 gives Stack Encrypt what it needs to read a plaintext by reference instead of copying it:
EncryptRefencrypts a value through a borrow (aead: generic code cannot bound on&T: Encrypt, so every plaintext Stack Encrypt is generic over gets copied vitaminc#614).SchemeVisitorkeys an order scheme from the PRF (pseudorandom function) without copying the value (ore / prf: no way to key an order scheme from the PRF, so every order term Stack Encrypt makes copies its plaintext vitaminc#615).Date,Timestamp, andDecimalare wipeable plaintext types, and vitaminc-ore'sOrderablelists every type an order index takes (feat(aead, ore): protected wrappers for dates, decimals and uuids with order targets, andOwnTargetrenamed toOrderablewithout bytes vitaminc#461).
Stack Encrypt moves to Vitamin C 0.10 in #1732. It does not use these yet.
Problem: Stack Encrypt still copies plaintext, and keeps its own copies of Vitamin C's types
Stack Encrypt still needs a plaintext to be Clone wherever one value feeds more than one output. A date field with a ciphertext and an order term is one example. These are the places, at the top of the stack that ends in #1687:
target/source.rs:ShareSource for Ownedclones a value for every output but the last.ConsumeSource for Borrowedclones before each output.op/mod.rs: an equality query copies its value to check it, and again to derive the term.dynamic/term.rs: the term the Go SDK asks for copies its value for two checks.eql-bindings: theCiphertextandHmac256targets (encryption.rs), and the query targets (encryption/query.rs), bound their plaintext onClone.
#1666 removes the plan builder and chain, where most of the bounds were. The list above is what remains.
Because of that bound, Stack Encrypt keeps its own value::Date, value::Timestamp, and value::Decimal. They duplicate Vitamin C's types, only so that they can be Clone. value.rs says so. That has two effects:
- Each copy of a plaintext is one more thing to wipe. Some of these copies are wiped, but the design depends on every clone being wiped correctly.
- vitaminc-ore's
Orderableis sealed. It covers Vitamin C'sDate,Timestamp, andDecimal, but not Stack Encrypt's copies. So fix(stack-encrypt)!: a data plan refuses an order index on bytes #1611 cannot use it, and keeps two traits of its own:sem::Orderableandop::Ordered.
Proposal: read every plaintext by reference
- No public bound in Stack Encrypt or
eql-bindingsrequires a plaintext to beClone. This covers the typed path, field markers, the EQL types, and the dynamic engine. - One value that feeds several outputs is read in place, or taken once and wiped once. It is not copied.
- Stack Encrypt's
Date,Timestamp, andDecimalare removed. Callers use Vitamin C's types. - An order index is bounded by vitaminc-ore's
Orderable. Stack Encrypt's ownsem::Orderableandop::Orderedare removed. - The stored format does not change. The frozen vectors, the stored-row fixtures, and the conformance corpus pass unchanged.
- A test shows that a value with a ciphertext and several terms is not copied.
Relationship to other work
- Builds on build(stack-encrypt)!: move to vitaminc 0.10, with dates from jiff #1732 (Vitamin C 0.10). It also builds on refactor(stack-encrypt,eql-bindings)!: remove the plan chain, the builder and the old traits #1666 and its stack, because refactor(stack-encrypt,eql-bindings)!: remove the plan chain, the builder and the old traits #1666 removes most of the
Clonebounds. - Blocks fix(stack-encrypt)!: a data plan refuses an order index on bytes #1611. It needs item 4.
- stack-encrypt: a string literal, a
serde_json::Value, or a newtype needs a copy or hand-written traits — accept borrowed values, addJsonPlaintextand#[derive(Scalar)]#1569, item 1 (encrypting a borrowed value), is the same change on the single-value API. - aead: generic code cannot bound on
&T: Encrypt, so every plaintext Stack Encrypt is generic over gets copied vitaminc#614 lists the copies as they were before refactor(stack-encrypt,eql-bindings)!: remove the plan chain, the builder and the old traits #1666, and states the goal: no plaintext is ever copied, and no plaintext type needsClone.
Activity
refactor(stack-encrypt,eql-bindings)!: use vitaminc's Date, Timestamp…
coderdan commentedon Nov 21, 2026
A design for this is proposed as ADR-0014 in #1739 (draft, stacked on #1687).
In short: an operation reads the plaintext through a reference (a "reader"), at most one operation takes it by value (a "taker"), and the compiler checks that. Nothing in the library turns "take" into "clone", so a declaration that would copy the plaintext does not compile. The idea comes from how axum checks the order of a handler's extractors.
Three places that copy are not in the list above. They were found while reviewing #1664 and #1687:
EncryptTarget::owned_encryptiondefaults to "guard the value, then run the borrowed form over a reference". So every target that writes onlyencryption()clones on eachencrypt_into, and nothing in its declaration says so. This is how the same copy reached three different types.JsonTreeDocument, the storage-onlyJson, and a GoJsonSearchwrite (encrypt_documentineql-bindings) each hold two copies of the whole document while it is sealed. The other ten storage-only EQL types (Integer,Text, and so on) hold two copies of a small value. feat(stack-encrypt,eql-bindings): typed JSON queries from a field reference #1687 fixes these one type at a time, with owned overrides.encryptfield of a data plan.Verb::Encryptindynamic/planned.rsrunsciphertext::<ValueFrame, K, Borrowed>, which clones the field's value. feat(stack-encrypt,eql-bindings): typed JSON queries from a field reference #1687 does not fix this one. It falls under item 1 of the proposal here ("the dynamic engine").The ADR lists four questions for a prototype to answer before it is accepted.
docs(stack-encrypt): propose ADR-0014, operations read the plaintext …
docs(stack-encrypt): propose ADR-0014, operations read the plaintext …
docs(stack-encrypt): propose ADR-0014, operations read the plaintext …
docs(stack-encrypt): propose ADR-0014, operations read the plaintext …
docs(stack-encrypt): propose ADR-0014, operations read the plaintext …
refactor(stack-encrypt)!: every operation reads its plaintext in place
refactor(stack-encrypt,eql-bindings)!: use vitaminc's Date, Timestamp…
docs(stack-encrypt): propose ADR-0014, operations read the plaintext …
4 remaining items
refactor(stack-encrypt,eql-bindings)!: use vitaminc's Date, Timestamp…
refactor(stack-encrypt)!: every operation reads its plaintext in place
refactor(stack-encrypt,eql-bindings)!: use vitaminc's Date, Timestamp…
docs(stack-encrypt): revise ADR-0014 to the position mechanism and se…
docs(stack-encrypt): accept ADR-0014, and stop deriving an order term…
refactor(stack-encrypt)!: every operation reads its plaintext in place
refactor(stack-encrypt,eql-bindings)!: use vitaminc's Date, Timestamp…
fix(stack-encrypt)!: an operation in owned mode wipes the value it is…
refactor(stack-encrypt)!: bound order indexes on vitaminc-ore's Order…
refactor(stack-encrypt)!: bound order indexes on vitaminc-ore's Order…
test(stack-encrypt): close the three mutants the gate found
fix(stack-encrypt): address the review of the plaintext read in place
fix(stack-encrypt): address the agent review of the plaintext read in…