Skip to content

refactor(stack-encrypt)!: make Encrypt and Decrypt Stack Encrypt's own derives - #2016

Open
coderdan wants to merge 19 commits into
fix/encrypt-derived-newtypefrom
feat/stack-encrypt-own-derives
Open

coderdan wants to merge 19 commits into
fix/encrypt-derived-newtypefrom
feat/stack-encrypt-own-derives

Conversation

@coderdan

Copy link
Copy Markdown
Contributor

Summary

#[derive(Encrypt)] and #[derive(Decrypt)] are now Stack Encrypt's own derives. A crate whose only dependency is stack-encrypt can derive both traits, or write them by hand. Before this PR, the user's Cargo.toml also needed vitaminc-aead, and the compile error did not say so.

The vitaminc-aead-derive crate is deleted. vitaminc-aead keeps the traits and has no derives of its own.

Closes #2013. This PR is stacked on #2014. Both change the same derive code.

What a user writes

Before:

stack-encrypt = "…"
vitaminc-aead = "0.10"   # only so that the derive compiles

After, stack-encrypt alone. The derive code is the same:

use stack_encrypt::{Decrypt, Encrypt};

#[derive(Encrypt, Decrypt)]
struct Email(String);

A hand-written impl imports from stack_encrypt:

use stack_encrypt::{plaintext::{depth, Sink}, Scalar};

Changes

  1. The derive code moved into stack-encrypt-derive. Its source, unit tests, and attribute docs moved with git mv, so they keep their history. There is one place to fix a derive bug, and one set of expansion tests.

  2. Generated code names stack_encrypt. It writes ::stack_encrypt::__derive::X. __derive is a hidden module that lists only the 23 items the generated code uses. #[aead(crate = "...")] still overrides the path, for crates in this repository that cannot name stack_encrypt.

  3. New public paths for a hand-written impl:

    • at the crate root: Scalar and FromScalar, beside Encrypt, EncryptRef, Decrypt, and DecodeError;
    • a new stack_encrypt::plaintext module: Sink, SeqSink, MapSink, and the type descriptions such as FixedType;
    • stack_encrypt::plaintext::depth: SCALAR, MAX_DEPTH, and the other depth helpers.

    The whole vitaminc_aead crate is not re-exported.

  4. Compile errors name Stack Encrypt paths, such as stack_encrypt::plaintext::depth::MAX_DEPTH.

  5. A crate that proves it: packages/stack-encrypt-downstream. It is a workspace member that CI builds, with publish = false. Its only dependency is stack-encrypt.

    • It derives both traits for a newtype, a struct, a fieldless enum, and two generic types.
    • It writes Encrypt, Scalar, and FromScalar by hand for one more type.
    • Its tests encrypt and decrypt through keyset.encrypt and encrypt_tree.
    • tests/manifest.rs fails if the crate lists any dependency but stack-encrypt, or names a vitaminc crate.
  6. vitaminc-aead's own derive tests stay where they are. They use stack-encrypt-derive as a dev-dependency, with #[aead(crate = "::vitaminc_aead")]. So mutation testing of vitaminc-aead still runs them.

  7. Every other Cargo workspace drops its patch entry for the deleted crate. None of them used the derives. They are:

    • both Go guests
    • recipher-js, and its test addon
    • EQL, and its generator
    • three fuzz crates
  8. Docs and config: CHANGELOGs, AGENTS.md, SECURITY.md, CODEOWNERS, release-plz.toml, the mutation-testing config, and the path filters of ten workflows.

Breaking changes

  • #[derive(vitaminc_aead::Encrypt)], or importing the derive macros from vitaminc_aead, no longer compiles. Import them from stack_encrypt.
  • #[aead(crate = "...")] now names the path to stack_encrypt. A path to vitaminc_aead still works, because that crate has the same hidden module.
  • The attributes and the stored values do not change.

Verification

Run locally on Rust 1.94.1.

  • Root workspace:

    • fmt, and clippy with -D warnings for all features and for stack-encrypt with no default features
    • tests of stack-encrypt-derive, vitaminc-aead (all features), stack-encrypt (all features, with the compile tests), stack-encrypt-downstream and recipher-frame
    • doctests for the whole workspace
    • vitaminc-aead tests with no features, and with each of jiff, rust_decimal, uuid, and hlist
  • Rustdoc: public and private items, with -D warnings, for the changed crates.

  • Mutation testing (--in-diff): 17 mutants, 14 caught, 3 that do not compile, 0 missed.

  • Supply chain: cargo vet --locked, and cargo deny for vitaminc-aead and recipher-frame.

  • Lockfiles: cargo metadata --locked passes in all 10 workspaces whose lockfile changed.

  • Other workspaces:

    • EQL: eql-bindings with all features, and the encryption tests
    • both Go guests, for wasm32, under their feature sets
    • recipher-js: fmt, clippy, and tests

    These ran before the merge of fix(stack-encrypt): encrypt a derived newtype with keyset.encrypt #2014's latest commits. Those commits changed only messages and docs.

  • Test counts: no test was removed. The 93 derive unit tests are the old crate's 61 plus 32. The derive doctests went from 43 to 29, because the old crate included its attribute docs three times; the 8 distinct examples each run once.

  • Not run: the nightly fuzz builds, the release-only jiff test, and the script tests, because the worktree has no node_modules.

Related

Review notes

Open questions:

  1. Publishing. A published stack-encrypt depends on vitaminc-aead from crates.io, so vitaminc-aead must be published from this repository first. Removing its derive macros is a breaking change. 0.11.0 is the semver-correct version, but the vitaminc crates that depend on it would need new releases too.
  2. Licence. The moved files were MIT-licensed. They keep that licence in src/value/LICENSE-MIT. The crate itself is licensed PolyForm Internal Use. In git, the only authors of these files are CipherStash staff and release bots, so they could be relicensed.
  3. The attribute name stays #[aead(...)]. It does not name vitaminc. It also keeps these attributes apart from the record derives' #[stash(...)]: both have a passthrough key with a different meaning.
  4. A rustc note a user may see. A refused trait bound can print "there are multiple different versions of crate vitaminc_aead in the dependency graph". It comes from vitaminc-aead 0.5.1. That version is a dependency of cts-common. This PR does not cause it.
  5. Each commit builds on its own, except one gap. Between the second and third commits, the other Cargo workspaces still name the deleted crate. The root workspace builds at every commit.

… names

A type can implement `Encrypt` and `Decrypt` by hand instead of deriving
them. Such an impl names the sink traits, the `depth` constants, the types
of a scalar, and the `Scalar` and `FromScalar` traits. `stack-encrypt`
re-exported none of these, so a crate that wrote such an impl had to
depend on `vitaminc-aead` too.

The new `stack_encrypt::plaintext` module re-exports the sink traits and
the scalar types. It also has a `depth` module of its own. That module
adds `MAX_DEPTH` to the depth constants. `Scalar` and `FromScalar` are
re-exported at the crate root, next to `Encrypt`, and `Decrypt`. The
module docs show a complete hand-written `Encrypt` and `FromScalar` impl,
run against the in-memory key source.

The `OpaqueEncode` docs and doctests now import from `stack_encrypt`. The
compile error for a `Scalar` type with the wrong `DEPTH` names
`stack_encrypt::plaintext::depth::SCALAR`. A refused `FromScalar` bound
now prints as `FromScalar`, through the root re-export, so one UI
snapshot changes in that line only.
…pt's derives

`stack_encrypt::Encrypt` and `stack_encrypt::Decrypt` re-exported the
derives of vitaminc-aead-derive. Their code named every item through
`::vitaminc_aead`. Rust resolves that path only in a crate that lists
`vitaminc-aead` as a dependency of its own. So a crate that depended on
`stack-encrypt` alone could not derive either trait. The error did not
say why.

The two derives now live in `stack-encrypt-derive`, in `src/value/`, and
`stack-encrypt` re-exports them. The generated code names every item
through `<crate>::__derive`. `<crate>` is `::stack_encrypt` unless
`#[aead(crate = "...")]` names another path. `vitaminc_aead::__derive` is
a hidden module that lists exactly the items the expansion names, and
`stack_encrypt::__derive` re-exports it. No other part of vitaminc-aead
is re-exported for the derives.

vitaminc-aead-derive is removed rather than kept as a second public
derive. So a derive bug has one place to fix, and one set of expansion
tests. Its source, unit tests, and attribute docs move with their
history. The moved files keep their MIT licence, in `LICENSE-MIT` beside
them. vitaminc-aead loses its derive dependency and its re-export of the
macros. Its own tests and doctests use Stack Encrypt's derive, with
`#[aead(crate = "::vitaminc_aead")]`, since stack-encrypt depends on it.

The attributes keep the name `aead`. That name does not name vitaminc.
It also keeps them apart from the `#[stash(...)]` attributes of the
record derives. So neither family of derives refuses the other's keys.

The wire shape does not change. The UI snapshots change only in line
numbers and in the depth error. That error now names
`stack_encrypt::plaintext::depth::MAX_DEPTH`.

These move with the crate:

- the root workspace member and patch;
- the mise task include, with a rustdoc task for stack-encrypt-derive in
  place of the old crate's;
- the release-plz entry;
- the mutation-testing config, now for the value derives only;
- the workflow path filters and CODEOWNERS;
- the vitaminc-aead supply-chain config.

cargo deny now leaves stack-encrypt-derive out of vitaminc-aead's graph,
as cargo vet's filter already did. The derive's own tests reach the
whole workspace.

BREAKING CHANGE: `vitaminc_aead::Encrypt` and `vitaminc_aead::Decrypt`
are traits only. Import the derives from `stack_encrypt`.
`#[aead(crate = "...")]` now names the path to `stack_encrypt`. A path to
`vitaminc_aead` still works.
…paces

Every Cargo workspace that resolves vitaminc-aead 0.10 patched both
vitaminc-aead and vitaminc-aead-derive to the copies in this repository.
The derive crate is gone, and vitaminc-aead no longer depends on it, so
nothing in these workspaces resolves vitaminc-aead-derive 0.10 any more.
A patch that names a missing path fails every cargo command in the
workspace.

These workspaces now patch vitaminc-aead alone:

- the two Go guests;
- recipher-js, and its test addon;
- EQL, and its timing generator;
- the three fuzz crates.

Each `Cargo.lock` loses the vitaminc-aead-derive 0.10 entry. The 0.5.1
copy that cts-common reaches through vitaminc 0.5.1 still comes from
crates.io. No code in these workspaces used the derives.
…ncrypt alone

Every crate in this repository that derived `Encrypt` or `Decrypt`
already depended on vitaminc-aead. So nothing noticed that the derives
did not compile without it. The new crate is a member of the root
workspace. Its only dependency is `stack-encrypt`, as an application's
would be. `tests-crates.yml` builds and tests it with the rest.

The crate derives both traits for these types:

- a newtype;
- a struct with named fields and a passthrough field;
- a fieldless enum;
- a generic newtype;
- a generic struct.

It also writes `Encrypt`, `Scalar`, and `FromScalar` by hand for one more
type. Its tests send each type through `keyset.encrypt` or
`keyset.encrypt_tree` and back, under the in-memory key source.

`tests/manifest.rs` fails if the manifest names a dependency other than
`stack-encrypt`. It also fails if any table names a vitaminc crate. Two
trybuild cases pin what a user reads when a derive is refused. The trait
bounds print as `stack_encrypt` paths. The depth error names
`stack_encrypt::plaintext::depth::MAX_DEPTH`.

The crate implements `Zeroize` by hand, through `stack_encrypt`'s
re-export of the trait, so that it needs no zeroize dependency either.
It is `publish = false`, with `release = false` in release-plz.toml. The
live jobs of tests.yml skip a change that touches only this crate.
The stack-encrypt CHANGELOG has two new entries:

- Under Fixed: the derives now compile in a crate that depends only on
  stack-encrypt. The entry says what stays the same, what
  `crate = "..."` now names, and how to migrate.
- Under Added: the `plaintext` module, and the `Scalar` and `FromScalar`
  re-exports, for a hand-written impl.

Two entries of the same release named `vitaminc_aead::plaintext::depth`
and "Vitamin C's `Scalar`". They now name the stack-encrypt paths.

The vitaminc-aead CHANGELOG lists two changes under Breaking. The crate
has no derives of its own, and the entry says how to import them instead.
The hidden `plaintext::__private` module is replaced by `__derive`.

AGENTS.md's repository layout and SECURITY.md's package list name the
two AEAD crates that remain. They say where the value derives went, and
describe stack-encrypt-downstream.
…derives

Brings in the latest review fixes of the base branch. Conflicts, and how
they are resolved:

- The derive's `encrypt.rs` moved to
  `packages/stack-encrypt-derive/src/value/encrypt.rs` here. The base
  branch decides once whether a type has a fixed depth. Its logic is kept
  as it is, with the generated paths renamed to `#aead::...` as in the
  rest of the moved file.
- `vitaminc-aead-derive/src/lib.rs` is deleted here. Its new paragraph,
  that `#[derive(Zeroize)]` does not wipe a fieldless enum, moves to
  `stack-encrypt-derive/docs/value-derives.md`, with its doctest importing
  `stack_encrypt::Encrypt`.
- `scalar.rs` keeps the base branch's new text and its wording of the
  `DEPTH` error, with the `stack_encrypt::plaintext::depth::SCALAR` path.
- The stack-encrypt CHANGELOG keeps the base branch's entry, names
  `stack_encrypt::plaintext::depth`, and no longer says that a crate
  needs vitaminc-aead to use the derive.
- The vitaminc-aead CHANGELOG has one Breaking section with the entries
  of both branches.
@changeset-bot

changeset-bot Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 44de1d0

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

This PR includes no changesets

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

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

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

@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI; Enforced overrides: Organization
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 917a73d9-ee98-4a30-a93a-13132b955a05

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review

@github-actions

github-actions Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Mutation testing (cargo-mutants, --in-diff, stack-auth, stack-encrypt, stack-encrypt-jsonpath, stack-encrypt-derive, vitaminc-aead, recipher-frame)

caught missed unviable timeout
16 0 3 0

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

@coderdan coderdan left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review at medium effort. Five findings: one pre-existing problem that this PR widens, and four nits. I checked each one against the branch. For the first, I also compared the registry copy of vitaminc-aead 0.10.0 with the copy in this repository.

Comment thread packages/stack-encrypt/src/lib.rs Resolved
Comment thread supply-chain/vitaminc-aead/filter-graph Resolved
The code that writes every derived `Encrypt` and `Decrypt` impl moved from
vitaminc-aead-derive to stack-encrypt-derive. The supply-chain checks left
the new crate out, so a change to it, or a bump of its syn, was no longer
vetted. The workflow also did not start when the derive changed.

The derive's dev-dependencies include stack-encrypt, and stack-encrypt
reaches the rest of the workspace. So each tool now leaves out those dev-dependencies
alone, and keeps the derive's normal dependencies:

- cargo deny checks the derive as the root of its own graph, with
  `--exclude-dev`. The other two crates' checks leave the derive out with
  `--exclude`. A `[graph] exclude` entry cannot do this, because cargo
  deny cannot use an excluded crate as a root.
- cargo vet keeps the derive and stack-encrypt-jsonpath in the filter.
  The derive's policy is `safe-to-deploy`, with `dev-criteria = []`.
- stack-encrypt-jsonpath's policy asks nothing of miette. miette comes
  only with the `diagnostics` feature. The derive does not turn it on.

The derive's graph brings in thiserror 1.0.69 and thiserror-impl 1.0.69,
through stack-encrypt-jsonpath. No imported registry audits them, so
they are exemptions at `safe-to-deploy`, as thiserror 2.0.18 is. The
reviewer decides whether an audit should replace them. The licence
check allows the PolyForm Small Business licence for the two
first-party crates only.
The too-deep compile error named
`stack_encrypt::plaintext::depth::MAX_DEPTH` as fixed text. Every other
path in the expansion follows `#[aead(crate = "...")]`. A type derived
with `crate = "::vitaminc_aead"`, as vitaminc-aead's tests and docs do,
was told about a path that does not resolve there.

The message now builds the path from the crate path. vitaminc-aead
re-exports `MAX_DEPTH` from `plaintext::depth`, as stack-encrypt does, so
the path resolves under both crates. An expansion test checks the message
for the default path and for `::vitaminc_aead`. vitaminc-aead's
`sink_too_deep` snapshot now names `vitaminc_aead::plaintext::depth::MAX_DEPTH`.
The manifest guard compared only the table named `[dependencies]`. A
dependency written as `[dependencies.serde]`, or listed under
`[target.'cfg(unix)'.dependencies]` or `[build-dependencies]`, was never
read. The guard still passed, and the crate no longer showed what its
docs say.

The guard now parses the manifest with the `toml` crate, as a new
dev-dependency. trybuild already brings `toml` 1.1.8 into the lockfile.
Parsing reads every form Cargo accepts, including dotted keys under a
target. A table other than a dev-dependency table can list only
stack-encrypt, at the top level or under a target. No table names a
vitaminc crate, under its own name or through `package`. Unit tests feed
the checks a manifest for each form.
…ture set

rustc lists a trait's other implementors in the errors that
`field_type_not_encryptable.stderr` records. A stack-encrypt feature such
as `dynamic` adds implementors. So the recording looked as if it could
match only one feature set, and as if the re-record command picked the
wrong one.

It matches every feature set. trybuild builds the ui cases as a separate
project, from this crate's dev-dependencies and features.
This crate has no features, so CI's `--workspace --all-features` run
records the same errors as a plain `cargo test`. Both pass with the
committed recording.

Limiting the test with `required-features`, as stack-encrypt's own ui
test does, would need a feature here that turns on `stack-encrypt/dynamic`.
That feature would add `&Frame<'_>` and `&Lent<'_, T>` to the lists, and
so make the recording depend on features after all.

The ui test's docs now say why the recording matches every feature set.
A manifest test fails if a `[features]` table is added to this crate.
The comment above the two licence exceptions said the crates are under a PolyForm Small Business licence. Their LICENSE file is the PolyForm Internal Use License. cargo-deny reads that text as PolyForm-Small-Business-1.0.0, so the exception has to name that identifier. The comment now says so.
@coderdan
coderdan marked this pull request as ready for review November 28, 2026 11:34
@coderdan
coderdan requested a review from a team as a code owner November 28, 2026 11:34

@cipherstash-agent cipherstash-agent left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review attestation

Reviewed 6e5cda5 · posted from Claude Code · claude-opus-5-5 · effort medium
Sources: code-review · claude-opus-5-5; mattpocock-skills:code-review agent · claude-opus-5-5; rust-security agent · claude-opus-5-5
Skills: code-review, mattpocock-skills:code-review, company-skills:rust-security, company-skills:review-cross-check

Findings (5): 2 major, 3 minor — 3 inline, 1 below, 1 in reply.

  • AGENTS.md:817 (outside diff) minor Two sentences still say there are three AEAD crates: AGENTS.md:817-818 ("for the three AEAD crates kani.yml, …") and AGENTS.md:878 ("All three #[deny(unsafe_code)]"), and so does the root Cargo.toml:5 comment. Only vitaminc-aead and recipher-frame remain, so change "three" to "two" at all three sites.

    Source: mattpocock-skills:code-review agent · claude-opus-5-5


Generated by Claude Code

Comment thread AGENTS.md Resolved
Comment thread .github/workflows/udeps.yml Resolved
stack-encrypt-downstream is a member of the root Cargo workspace, so the
counts in AGENTS.md were one short. The Repository Layout bullet now
lists the crate in its opening paths and says two crates were added here.
The workspace note now says eight stack-* crates.

SECURITY.md lists the unpublished test harness `eql-encryption-tests`
and says that it adds no published package. stack-encrypt-downstream is
also an unpublished test crate, so SECURITY.md now says the same of it.
The AEAD crates moved from vitaminc are now vitaminc-aead and
recipher-frame. The derive crate that came with them is gone. Its code
is in stack-encrypt-derive. Two sentences in AGENTS.md and the comment
above the root workspace members still counted three. The AGENTS.md
sentence about `#![deny(unsafe_code)]` is still true of both crates.
cargo udeps runs over the whole root workspace, and stack-encrypt-downstream
is a member. But neither path list named the crate. A change that stops
using one of its dev-dependencies, such as `toml`, did not start the job.
Both lists now name it, as tests-crates.yml does.

The other workflows that list stack-encrypt-derive do so because their
builds depend on stack-encrypt, or because they check named crates.
Nothing depends on stack-encrypt-downstream, so they do not list it.
The other stack-* crates warn on `dbg!`, `mem::forget`, printing,
`unwrap`, `expect`, and `panic!`. Those crates also deny unsafe code.
CI's clippy turns each warning into an error.

The downstream crate had none of these lints. Clippy does not run most of
them by default, because they are restriction lints. So a
`dbg!(&self.cents)` in the hand-written `Encrypt` for `Price` passed CI.

The crate now carries the same block as stack-encrypt, with the same
allowances for unit tests. Nothing in the crate breaks the lints.
The CHANGELOG said that `#[aead(crate = "...")]` still works with a path
to `vitaminc_aead`. The derives' code names `<crate>::__derive`. Only the
`vitaminc-aead` source in this repository has that module. crates.io's
0.10.0 does not. So the line was true only inside this repository, and
the CHANGELOG now says so.

The entry for the too-deep error also says that its path starts at the
`crate` path, when a type names one.
@coderdan

Copy link
Copy Markdown
Contributor Author

The finding outside the diff is fixed in afdfcff. AGENTS.md now says two AEAD crates in both places, and "Both" deny unsafe_code, which I checked for vitaminc-aead and recipher-frame. The root Cargo.toml comment says the same. The CHANGELOG line about a vitaminc_aead crate path is qualified in be75de1.

Brings in #2014 and, through it, next with #2011.

Conflicts:

- stack-encrypt's lib.rs: this branch re-exports `FromScalar` and
  `Scalar`, and #2011 re-exports `ScalarTarget`. The merge re-exports
  all three.
- stack-encrypt's CHANGELOG: both sides added an entry at the top of
  Added. The merge keeps both entries.
- owned_collection_elements.stderr: the merge takes the snapshot of
  #2014. Recording it again with TRYBUILD=overwrite gave the same file.

#2011 added tests and a doc example to vitaminc-aead that use the
derives from vitaminc_aead. On this branch vitaminc-aead has no derives.
So these now use stack_encrypt_derive with
`#[aead(crate = "::vitaminc_aead")]`, as the other vitaminc-aead tests
do:

- tests/scalar_target.rs
- four compile-fail tests in tests/ui/scalar_target/
- the example on `ScalarTarget` in typed.rs

The four snapshots of those compile-fail tests change only in their line
numbers. Two doc comments in typed.rs now say "Stack Encrypt's
`#[derive(Decrypt)]`", as the rest of the file does.
The merge of #2014 added packages/mn-meta.err and packages/eql/tests/timing/mn-meta.err. Both are empty logs from a local lockfile check, and are not part of the change.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants