ml-dsa: lower stack usage of KeyPair::from_seed by up to 112kb#1259
Merged
tarcieri merged 1 commit intoRustCrypto:masterfrom Mar 20, 2026
Merged
ml-dsa: lower stack usage of KeyPair::from_seed by up to 112kb#1259tarcieri merged 1 commit intoRustCrypto:masterfrom
tarcieri merged 1 commit intoRustCrypto:masterfrom
Conversation
When using the KeyGen::from_seed is used, the A Matrix is computed (using `expand_a`). By design, the matrix is kept on the stack, and two copies end up being held (One in the `SigningKey` and another one in the `VerifyingKey`). That matrix takes up to 56kB for ML-DSA-87. Because two `SigningKey::new`/`VerifyingKey::new` code paths maybe used without the A matrix (when decoding from an expanded key), the code paths for both builder end up having to store two copies of the matrix and increases the stack size. This is all private API and does not break any of the public API. This only affects the debug/test builds as the release builds appears to optimize the unwrap_or_else and not use extraneous stack space.
ad3bf8e to
2c62aab
Compare
tarcieri
approved these changes
Mar 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When using the KeyGen::from_seed is used, the A Matrix is computed (using
expand_a).By design, the matrix is kept on the stack, and two copies end up being held (One in the
SigningKeyand another one in theVerifyingKey). That matrix takes up to 56kB for ML-DSA-87.Because two
SigningKey::new/VerifyingKey::newcode paths maybe used without the A matrix (when decoding from an expanded key), the code paths for both builder end up having to store two copies of the matrix and increases the stack size.This is all private API and does not break any of the public API.
This only affects the debug/test builds as the release builds appears to optimize the unwrap_or_else and not use extraneous stack space.
See #1024