Skip to content

stack-encrypt: #[derive(Encrypt, Decrypt)] does not compile in a crate that depends only on stack-encrypt #2013

Todo

Description

@coderdan

Background

Stack Encrypt is the Rust engine in packages/stack-encrypt. It is built on vitaminc, a cryptography library, whose crates vitaminc-aead and vitaminc-aead-derive are in this repository. Stack Encrypt re-exports vitaminc's Encrypt and Decrypt, each of which is both a trait and a derive macro. A Rust program derives them for a type of its own, such as struct Email(String), so that the engine can encrypt and decrypt it.

Problem

A crate that depends only on stack-encrypt cannot use the derives that stack-encrypt re-exports.

use stack_encrypt::{Decrypt, Encrypt};

#[derive(Encrypt, Decrypt)]
pub struct Email(String);
// error[E0433]: failed to resolve: could not find `vitaminc_aead` in the list of imported crates

Why it happens:

  1. The derives write paths that start at the crate ::vitaminc_aead, for example impl ::vitaminc_aead::Encrypt for Email. That is the default in packages/vitaminc-aead-derive/src/attrs.rs (krate.unwrap_or_else(|| syn::parse_quote!(::vitaminc_aead))).
  2. Rust resolves ::vitaminc_aead only when the crate being compiled lists vitaminc-aead as a dependency of its own. A re-export from stack-encrypt does not count.
  3. #[aead(crate = "...")] can point the derive at another path, but stack-encrypt does not re-export the vitaminc_aead crate, so there is no path through stack_encrypt to give it.

So every user of the derives has to add vitaminc-aead to their Cargo.toml, at a version that matches the one stack-encrypt was built with. The error does not say so. A developer who uses Stack Encrypt has no reason to know what vitaminc is.

The comment above the re-export says the opposite: callers "don't have to depend on vitaminc-aead directly for the common path" (packages/stack-encrypt/src/lib.rs, above pub use vitaminc_aead::{…}). Nothing in this repository catches the problem, because every test and example that uses the derives is in a crate that already depends on vitaminc-aead.

Verified by compiling the example above in a new crate whose only dependency is stack-encrypt, with Rust 1.94.1.

Proposal

A crate that depends only on stack-encrypt can derive Encrypt and Decrypt, with no attribute that names vitaminc.

Approaches to evaluate:

  1. Stack Encrypt has derives of its own. stack-encrypt-derive gains Encrypt and Decrypt macros that produce the same code with paths through stack_encrypt, and stack-encrypt exports those in place of vitaminc's. This needs the expansion code of vitaminc-aead-derive to be usable as a library, and a hidden re-export of vitaminc_aead in stack-encrypt for the generated paths.
  2. A hidden re-export and a documented attribute, such as #[aead(crate = "stack_encrypt::__private::vitaminc_aead")]. This is cheaper, but the developer writes an attribute on every type, and it names vitaminc.

Also:

  • A test crate that depends only on stack-encrypt and derives both traits for a newtype, a struct and an enum, built in CI, so the problem cannot return unnoticed.
  • Correct the comment above the re-export, and the docs that tell a user to add vitaminc-aead.

Relationship to other work

Activity

  1. added
    bug
    Rust
    on Nov 28, 2026
  2. coderdan commented on Nov 28, 2026

    @coderdan
    ContributorAuthor

    One more case for this issue, from the review of #2014.

    A hand-written Encrypt impl has the same problem as the derive. It needs three items that stack-encrypt does not re-export:

    • vitaminc_aead::plaintext::Sink, for the visit method;
    • vitaminc_aead::Scalar, to make the type usable with keyset.encrypt;
    • vitaminc_aead::plaintext::depth, for const DEPTH: Option<usize> = depth::SCALAR;.

    The OpaqueEncode docs describe this path, and its doctests import all three from vitaminc_aead. So the fix here should let a crate that depends only on stack-encrypt write an Encrypt impl by hand as well as derive one.

  3. self-assigned this
    on Nov 28, 2026
  4. coderdan commented on Nov 28, 2026

    @coderdan
    ContributorAuthor

    I don't just think Stack Encrypt should have its own derives. I think the Stack Encrypt derives should replace the vitaminc-aead derives entirely.

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

Metadata

Metadata

Assignees

Labels

RustSDKbug

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions