Skip to content

stack-encrypt: keyset.encrypt refuses a derived newtype, and the error names a vitaminc trait #2012

Backlog

Description

@coderdan

Background

Stack Encrypt is the Rust engine in packages/stack-encrypt. Its simplest call encrypts one value into one ciphertext: keyset.encrypt(value). A Rust program often wraps a scalar in a type of its own, such as struct Email(String), and derives the engine's traits for it with #[derive(Encrypt, Decrypt)]. The derive comes from vitaminc, the cryptography library the engine is built on. Its crates vitaminc-aead and vitaminc-aead-derive are in this repository.

Problem

keyset.encrypt does not accept a derived newtype, and the error names a trait the developer has never seen.

use stack_encrypt::{Decrypt, Encrypt};
use zeroize::Zeroize;

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

keyset.encrypt(Email("ada@example.com".into()))
// error[E0277]: the trait bound `for<'a> &'a Email: vitaminc_aead::Scalar` is not satisfied

Why it happens:

  1. keyset.encrypt accepts a type only when a borrow of it implements vitaminc's Scalar marker (OpaqueEncode in packages/stack-encrypt/src/scalar.rs).
  2. #[derive(Encrypt)] on a newtype generates impl Encrypt for Email and impl Encrypt for &Email. Each copies DEPTH from the inner type, so the compiler already knows an Email is one scalar (DEPTH is Some(0)). The derive does not generate impl Scalar.
  3. Scalar repeats what DEPTH says. It exists because stable Rust cannot use a constant in a trait bound (packages/vitaminc-aead/src/plaintext/mod.rs).

So the developer has to write impl Scalar for &Email {} by hand. To do that they must add vitaminc-aead as a dependency, because stack-encrypt does not re-export Scalar. Nothing checks that a hand-written impl is honest: one on a struct with several fields compiles, and the encrypt then fails at run time.

Verified by compiling the example above against next. The decrypt side has the same gap, and #2011 closes it there.

Proposal

A newtype that derives Encrypt over a scalar works with keyset.encrypt with no other code:

  1. No second trait for the developer to implement, and no extra derive attribute.
  2. Nothing in the developer's code, or in the error they see for a real mistake, requires knowing vitaminc.
  3. A newtype over a value that is not one scalar, such as struct Tags(Vec<String>), still derives Encrypt for use in a record, and is still refused by keyset.encrypt at compile time.
  4. A false claim cannot be written by hand, or is caught at compile time.

Approaches to evaluate, cheapest first:

  • The derive emits the marker. For a newtype, emit impl<'r> Scalar for &'r Email where &'r Inner: Scalar {}. The bound names a lifetime parameter, so it is checked where the impl is used, not where it is written, and a newtype over a Vec still compiles. Check whether the owned form, impl Scalar for Email where Inner: Scalar {}, is needed, and whether it compiles for a newtype over a type that is not a scalar.
  • An associated type on Encrypt that states the same fact as DEPTH, with one blanket impl of Scalar over it. This changes every hand-written Encrypt impl.
  • A compile-time assertion on DEPTH in the engine, in place of the Scalar bound. The error then appears when the program is built, not when it is checked.

Also:

  • When a type is refused, the error should say in Stack Encrypt's terms that keyset.encrypt takes one value, and what to use for a collection or a record (#[diagnostic::on_unimplemented]).
  • Compile tests: a derived newtype over a String and over an integer is accepted; a derived newtype over a Vec<String> and a derived struct with named fields are refused.
  • Update the OpaqueEncode docs and the CHANGELOG.

Relationship to other work

Activity

  1. added
    bug
    Rust
    on Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    RustSDKbug

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions