Skip to content

Conversation

@mkannwischer
Copy link
Contributor

@mkannwischer mkannwischer commented Jan 10, 2026

For consumers implementing custom allocations (in cases where the stack is not large enough), it may be important to know how much space has to be reserved for allocations of large structures.

This commit adds per-operation allocation constants:

  • MLK_TOTAL_ALLOC_{512,768,1024}_{KEYPAIR,ENCAPS,DECAPS} for each parameter set and operation, indicating peak memory consumption.
  • MLK_MAX_TOTAL_ALLOC_{KEYPAIR,ENCAPS,DECAPS} for maximum across parameter sets per operation.
  • MLK_TOTAL_ALLOC_{512,768,1024} for maximum across operations per parameter set.
  • MLK_MAX_TOTAL_ALLOC for maximum across all parameter sets and operations.

The alloc_test is extended to check that the constants indeed match the peak memory consumption of the corresponding functions.

I included the extension from #869 that differentiates between keygen w/ and w/o PCT. The commits are kept separate though to follow the same structure as in mldsa-native.

@mkannwischer
Copy link
Contributor Author

mkannwischer commented Jan 10, 2026

Note that the keypair numbers include the pair-wise consistency test (PCT) - keypair without PCT will be considerably lower.
We probably want to provide the numbers both w/ and w/o PCT, but that needs more changes (also in mldsa-native).
I prefer to do that as a follow-up.

I have included the extension as a separate commit.

@mkannwischer mkannwischer marked this pull request as ready for review January 11, 2026 03:47
@mkannwischer mkannwischer requested a review from a team as a code owner January 11, 2026 03:47
Copy link
Contributor

@hanno-becker hanno-becker left a comment

Choose a reason for hiding this comment

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

I think it is slightly confusing to expose MLK_MAX_TOTAL_ALLOC as the maximum across all security levels, even in a single-level build. I'd prefer to only export constants explicitly labeled by a security level.

For consumers implementing custom allocations (in cases where the stack is
not large enough), it may be important to know how much space has to be
reserved for allocations of large structures.

This commit adds per-operation allocation constants:
 - MLK_TOTAL_ALLOC_{512,768,1024}_{KEYPAIR,ENCAPS,DECAPS} for each parameter
   set and operation, indicating peak memory consumption.
 - MLK_MAX_TOTAL_ALLOC_{KEYPAIR,ENCAPS,DECAPS} for maximum across parameter
   sets per operation.
 - MLK_TOTAL_ALLOC_{512,768,1024} for maximum across operations per parameter
   set.
 - MLK_MAX_TOTAL_ALLOC for maximum across all parameter sets and operations.

The alloc_test is extended to check that the constants indeed match the peak
memory consumption of the corresponding functions.

 - Ported from pq-code-package/mldsa-native#850
 - Resolves #1442

Signed-off-by: Matthias J. Kannwischer <[email protected]>
Multilevel builds require MLK_CONFIG_NO_SUPERCOP since the SUPERCOP
naming convention (crypto_kem_xxx) does not disambiguate between
parameter sets.

Add MLK_CONFIG_NO_SUPERCOP to all multilevel build configs and
propagate it to the API layer in the legacy config path.

This also fixes problem with mlkem_native.h: On multiple inclusions of
mlkem_native.h without NO_SUPERCOP set, mlkem_native.h would not undef the
API_CONFIG macros in the end of the file causing the second inclusion of
mlkem_native.h to set the LEGACY_CONFIG option.

 - Ported from pq-code-package/mldsa-native#869

Signed-off-by: Matthias J. Kannwischer <[email protected]>
Split KEYPAIR allocation constants into _NO_PCT and _PCT variants to
accurately reflect memory usage with and without pairwise consistency
testing (PCT). MLK_TOTAL_ALLOC_*_KEYPAIR now automatically selects
the appropriate value based on MLK_CONFIG_KEYGEN_PCT.

For legacy configs, assume PCT is enabled (conservative).

The default alloc test config no longer enables PCT; PCT is tested
via config-variations.

 - Ported from pq-code-package/mldsa-native#869

Signed-off-by: Matthias J. Kannwischer <[email protected]>
Remove MLK_MAX_TOTAL_ALLOC and MLK_MAX_TOTAL_ALLOC_{KEYPAIR,ENCAPS,DECAPS}
constants. These exposed the maximum allocation across all security
levels even in single-level builds, which was confusing. Users should
instead use the explicitly security-level-labeled constants such as
MLK_TOTAL_ALLOC_1024_KEYPAIR.

The per-level MLK_TOTAL_ALLOC_{512,768,1024}_{KEYPAIR,ENCAPS,DECAPS} and
aggregate MLK_TOTAL_ALLOC_{512,768,1024} constants remain available.

Signed-off-by: Matthias J. Kannwischer <[email protected]>
@mkannwischer
Copy link
Contributor Author

I think it is slightly confusing to expose MLK_MAX_TOTAL_ALLOC as the maximum across all security levels, even in a single-level build. I'd prefer to only export constants explicitly labeled by a security level.

Okay, yes, makes sense. I have removed the macros. Also removed it in mldsa-native in pq-code-package/mldsa-native#869.
I kept it as a separate commit, so that each change has a corresponding one in mldsa-native.

@mkannwischer mkannwischer merged commit 7ecb152 into main Jan 12, 2026
779 of 781 checks passed
@mkannwischer mkannwischer deleted the mlk-max-alloc branch January 12, 2026 08:42
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.

Provide macro indicidating upper bound of memory allocations via MLK_ALLOC

3 participants