Add WOLFSSL_MLKEM_DYNAMIC_KEYS for ML-KEM key memory optimization#4
Closed
Add WOLFSSL_MLKEM_DYNAMIC_KEYS for ML-KEM key memory optimization#4
Conversation
When enabled, the MlKemKey struct uses dynamically allocated pointers instead of static arrays for priv, pub, and a (CACHE_A) buffers. This right-sizes buffers to the actual ML-KEM level and only allocates the needed key parts (e.g., no private key buffer for encapsulate-only use). Saves ~2-4KB per key object (up to ~12KB with CACHE_A) when using smaller key levels or public-key-only operations. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Frauschi
commented
Apr 8, 2026
- Indent preprocessor directives in Init to match surrounding code style - Remove unnecessary block nesting in Free; move k declaration inside the if (key->priv != NULL) clause where it is actually needed - Make mlkemkey_get_k() unconditionally available and reuse it in MakeKeyWithRandom to replace the duplicate type-to-k switch Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.
Summary
WOLFSSL_MLKEM_DYNAMIC_KEYSmacro that makespriv,pub, anda(whenWOLFSSL_MLKEM_CACHE_A) buffers inMlKemKeydynamically allocated pointers instead of static arraysDecodePublicKeyonly allocates the public key buffer, saving private key memory for encapsulate-only use cases (e.g., TLS key exchange)WOLFSSL_NO_MALLOC(compile-time error)Memory savings (when ML-KEM-1024 is compiled in)
Test plan
WOLFSSL_MLKEM_DYNAMIC_KEYSalone: ML-KEM tests passWOLFSSL_MLKEM_DYNAMIC_KEYS+WOLFSSL_MLKEM_CACHE_A: ML-KEM tests passWOLFSSL_MLKEM_DYNAMIC_KEYS+WOLFSSL_NO_MALLOC: compile-time error as expected🤖 Generated with Claude Code