Skip to content

Commit c37874d

Browse files
committed
mlkem_native.h: Add PCT-aware MLK_TOTAL_ALLOC constants
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]>
1 parent dec97d5 commit c37874d

File tree

6 files changed

+38
-15
lines changed

6 files changed

+38
-15
lines changed

mlkem/mlkem_native.S

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,20 @@
177177
#undef MLK_TOTAL_ALLOC_1024_DECAPS
178178
#undef MLK_TOTAL_ALLOC_1024_ENCAPS
179179
#undef MLK_TOTAL_ALLOC_1024_KEYPAIR
180+
#undef MLK_TOTAL_ALLOC_1024_KEYPAIR_NO_PCT
181+
#undef MLK_TOTAL_ALLOC_1024_KEYPAIR_PCT
180182
#undef MLK_TOTAL_ALLOC_512
181183
#undef MLK_TOTAL_ALLOC_512_DECAPS
182184
#undef MLK_TOTAL_ALLOC_512_ENCAPS
183185
#undef MLK_TOTAL_ALLOC_512_KEYPAIR
186+
#undef MLK_TOTAL_ALLOC_512_KEYPAIR_NO_PCT
187+
#undef MLK_TOTAL_ALLOC_512_KEYPAIR_PCT
184188
#undef MLK_TOTAL_ALLOC_768
185189
#undef MLK_TOTAL_ALLOC_768_DECAPS
186190
#undef MLK_TOTAL_ALLOC_768_ENCAPS
187191
#undef MLK_TOTAL_ALLOC_768_KEYPAIR
192+
#undef MLK_TOTAL_ALLOC_768_KEYPAIR_NO_PCT
193+
#undef MLK_TOTAL_ALLOC_768_KEYPAIR_PCT
188194
#undef crypto_kem_check_pk
189195
#undef crypto_kem_check_sk
190196
#undef crypto_kem_dec

mlkem/mlkem_native.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,20 @@
166166
#undef MLK_TOTAL_ALLOC_1024_DECAPS
167167
#undef MLK_TOTAL_ALLOC_1024_ENCAPS
168168
#undef MLK_TOTAL_ALLOC_1024_KEYPAIR
169+
#undef MLK_TOTAL_ALLOC_1024_KEYPAIR_NO_PCT
170+
#undef MLK_TOTAL_ALLOC_1024_KEYPAIR_PCT
169171
#undef MLK_TOTAL_ALLOC_512
170172
#undef MLK_TOTAL_ALLOC_512_DECAPS
171173
#undef MLK_TOTAL_ALLOC_512_ENCAPS
172174
#undef MLK_TOTAL_ALLOC_512_KEYPAIR
175+
#undef MLK_TOTAL_ALLOC_512_KEYPAIR_NO_PCT
176+
#undef MLK_TOTAL_ALLOC_512_KEYPAIR_PCT
173177
#undef MLK_TOTAL_ALLOC_768
174178
#undef MLK_TOTAL_ALLOC_768_DECAPS
175179
#undef MLK_TOTAL_ALLOC_768_ENCAPS
176180
#undef MLK_TOTAL_ALLOC_768_KEYPAIR
181+
#undef MLK_TOTAL_ALLOC_768_KEYPAIR_NO_PCT
182+
#undef MLK_TOTAL_ALLOC_768_KEYPAIR_PCT
177183
#undef crypto_kem_check_pk
178184
#undef crypto_kem_check_sk
179185
#undef crypto_kem_dec

mlkem/mlkem_native.h

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -447,22 +447,38 @@ int MLK_API_NAMESPACE(check_sk)(
447447
* fixed-sized buffer and a simple allocator (e.g., bump allocator).
448448
*/
449449
/* check-magic: off */
450-
#define MLK_TOTAL_ALLOC_512_KEYPAIR 10048
450+
#define MLK_TOTAL_ALLOC_512_KEYPAIR_NO_PCT 5824
451+
#define MLK_TOTAL_ALLOC_512_KEYPAIR_PCT 10048
451452
#define MLK_TOTAL_ALLOC_512_ENCAPS 8384
452453
#define MLK_TOTAL_ALLOC_512_DECAPS 9152
453-
#define MLK_TOTAL_ALLOC_768_KEYPAIR 15552
454+
#define MLK_TOTAL_ALLOC_768_KEYPAIR_NO_PCT 10176
455+
#define MLK_TOTAL_ALLOC_768_KEYPAIR_PCT 15552
454456
#define MLK_TOTAL_ALLOC_768_ENCAPS 13248
455457
#define MLK_TOTAL_ALLOC_768_DECAPS 14336
456-
#define MLK_TOTAL_ALLOC_1024_KEYPAIR 22400
458+
#define MLK_TOTAL_ALLOC_1024_KEYPAIR_NO_PCT 15552
459+
#define MLK_TOTAL_ALLOC_1024_KEYPAIR_PCT 22400
457460
#define MLK_TOTAL_ALLOC_1024_ENCAPS 19136
458461
#define MLK_TOTAL_ALLOC_1024_DECAPS 20704
459462
/* check-magic: on */
460463

464+
/*
465+
* MLK_TOTAL_ALLOC_*_KEYPAIR adapts based on MLK_CONFIG_KEYGEN_PCT.
466+
* For legacy config, we don't know which options are used, so assume
467+
* the worst case (PCT enabled).
468+
*/
469+
#if defined(MLK_API_LEGACY_CONFIG) || defined(MLK_CONFIG_KEYGEN_PCT)
470+
#define MLK_TOTAL_ALLOC_512_KEYPAIR MLK_TOTAL_ALLOC_512_KEYPAIR_PCT
471+
#define MLK_TOTAL_ALLOC_768_KEYPAIR MLK_TOTAL_ALLOC_768_KEYPAIR_PCT
472+
#define MLK_TOTAL_ALLOC_1024_KEYPAIR MLK_TOTAL_ALLOC_1024_KEYPAIR_PCT
473+
#else
474+
#define MLK_TOTAL_ALLOC_512_KEYPAIR MLK_TOTAL_ALLOC_512_KEYPAIR_NO_PCT
475+
#define MLK_TOTAL_ALLOC_768_KEYPAIR MLK_TOTAL_ALLOC_768_KEYPAIR_NO_PCT
476+
#define MLK_TOTAL_ALLOC_1024_KEYPAIR MLK_TOTAL_ALLOC_1024_KEYPAIR_NO_PCT
477+
#endif
478+
461479
/*
462480
* `MLK_MAX_TOTAL_ALLOC_{KEYPAIR,ENCAPS,DECAPS}` is the maximum across all
463481
* parameter sets for each operation.
464-
* `MLK_MAX_TOTAL_ALLOC` is the maximum across all parameter sets and
465-
* operations.
466482
*/
467483
#define MLK_MAX_TOTAL_ALLOC_KEYPAIR MLK_TOTAL_ALLOC_1024_KEYPAIR
468484
#define MLK_MAX_TOTAL_ALLOC_ENCAPS MLK_TOTAL_ALLOC_1024_ENCAPS

test/configs/configs.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -434,11 +434,6 @@ configs:
434434
description: "Using custom allocation that can be made fail at specific invocation"
435435
defines:
436436
MLK_CONFIG_NAMESPACE_PREFIX: mlk
437-
MLK_CONFIG_KEYGEN_PCT:
438-
content: |
439-
#if !defined(MLK_CONFIG_KEYGEN_PCT)
440-
#define MLK_CONFIG_KEYGEN_PCT
441-
#endif
442437
MLK_CONFIG_CUSTOM_ALLOC_FREE:
443438
content: |
444439
#define MLK_CONFIG_CUSTOM_ALLOC_FREE

test/configs/test_alloc_config.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
* This configuration differs from the default mlkem/mlkem_native_config.h in
3232
* the following places:
3333
* - MLK_CONFIG_NAMESPACE_PREFIX
34-
* - MLK_CONFIG_KEYGEN_PCT
3534
* - MLK_CONFIG_CUSTOM_ALLOC_FREE
3635
*/
3736

@@ -632,10 +631,7 @@ void custom_free(void *p, size_t sz, const char *file, int line,
632631
* key generation.
633632
*
634633
*****************************************************************************/
635-
#if !defined(MLK_CONFIG_KEYGEN_PCT)
636-
#define MLK_CONFIG_KEYGEN_PCT
637-
#endif
638-
634+
/* #define MLK_CONFIG_KEYGEN_PCT */
639635

640636
/******************************************************************************
641637
* Name: MLK_CONFIG_KEYGEN_PCT_BREAKAGE_TEST

test/src/test_alloc.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
* Level-dependent allocation limit macros.
1717
* These expand to the right MLK_TOTAL_ALLOC_{512,768,1024}_* constant
1818
* based on MLK_CONFIG_API_PARAMETER_SET.
19+
*
20+
* Note: MLK_TOTAL_ALLOC_*_KEYPAIR in the header automatically adapts
21+
* based on MLK_CONFIG_KEYGEN_PCT.
1922
*/
2023
#define MLK_TOTAL_ALLOC_KEYPAIR__(LVL) MLK_TOTAL_ALLOC_##LVL##_KEYPAIR
2124
#define MLK_TOTAL_ALLOC_KEYPAIR_(LVL) MLK_TOTAL_ALLOC_KEYPAIR__(LVL)
@@ -470,6 +473,7 @@ int main(void)
470473
/*
471474
* For parameter set 1024, also check that the high watermarks match
472475
* the MLK_MAX_TOTAL_ALLOC_* constants (which are defined as the 1024 values).
476+
* MLK_MAX_TOTAL_ALLOC_KEYPAIR adapts based on MLK_CONFIG_KEYGEN_PCT.
473477
*/
474478
#if MLK_CONFIG_API_PARAMETER_SET == 1024
475479
CHECK_ALLOC_MATCH(global_bump_high_mark_keypair, MLK_MAX_TOTAL_ALLOC_KEYPAIR);

0 commit comments

Comments
 (0)