Skip to content

crypto-common: add MaybeBox#2406

Closed
tarcieri wants to merge 1 commit intomasterfrom
crypto-common/maybe-box
Closed

crypto-common: add MaybeBox#2406
tarcieri wants to merge 1 commit intomasterfrom
crypto-common/maybe-box

Conversation

@tarcieri
Copy link
Copy Markdown
Member

@tarcieri tarcieri commented May 9, 2026

Adds a type which is always available, but when the newly added alloc feature is enabled, provides opportunistic heap allocation with Box, falling back on stack allocation if it is not.

Originally added to the ml-dsa crate in RustCrypto/signatures#1320 to address the large size of post-quantum keys and signatures while still retaining no_alloc support.

However, it is generally useful anywhere we work with secret values to e.g. prevent moves from making copies of them on the stack, even if they aren't excessively large.

@tarcieri tarcieri requested a review from newpavlov May 9, 2026 23:37
Adds a type which is always available, but when the newly added `alloc`
feature is enabled, provides opportunistic heap allocation with `Box`,
falling back on stack allocation if it is not.

Originally added to the `ml-dsa` crate in RustCrypto/signatures#1320 to
address the large size of post-quantum keys and signatures while still
retaining `no_alloc` support.

However, it is generally useful anywhere we work with secret values to
e.g. prevent moves from making copies of them on the stack, even if they
aren't excessively large.
@tarcieri tarcieri force-pushed the crypto-common/maybe-box branch from 661cbb5 to 5402e71 Compare May 9, 2026 23:39
@tarcieri
Copy link
Copy Markdown
Member Author

tarcieri commented May 9, 2026

Note: trying to find somewhere to extract this. If crypto-common doesn't make sense I can start with module-lattice I guess, as my main goal is using it with ml-kem in addition to ml-dsa.

Also not wild about the name, but I can't think of a better one.

Copy link
Copy Markdown
Member

@newpavlov newpavlov left a comment

Choose a reason for hiding this comment

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

The type itself looks fine to me, but I am not sure it's worth to keep it as part of crypto-common. It would've been a bit better if we already had the alloc feature. So I would prefer to keep it elsewhere for now.

Also not wild about the name, but I can't think of a better one.

How about CondBox (i.e. conditionally Box)?

@tarcieri
Copy link
Copy Markdown
Member Author

I can start with module-lattice for now, though there are definitely many more places I think this could be used. Pretty much anywhere we have secret key material, I think it would make sense.

I do agree adding an alloc feature to crypto-common just for this is a little annoying, though since it's off-by-default I thought that also minimized the impact of the change.

@tarcieri tarcieri closed this May 10, 2026
@tarcieri tarcieri deleted the crypto-common/maybe-box branch May 10, 2026 14:14
@newpavlov
Copy link
Copy Markdown
Member

We also could put it into a separate utils crate.

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