Skip to content

Commit 2f4200d

Browse files
committed
mlkem_native.h: Introduce MLK_TOTAL_ALLOC constants
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 <matthias@kannwischer.eu>
1 parent aaacec6 commit 2f4200d

File tree

6 files changed

+261
-83
lines changed

6 files changed

+261
-83
lines changed

.github/workflows/baremetal.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ jobs:
4646
acvp: ${{ matrix.target.acvp }}
4747
examples: false
4848
stack: false
49-
alloc: false
49+
alloc: true

mlkem/mlkem_native.S

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,23 @@
168168
#undef MLK_ERR_FAIL
169169
#undef MLK_ERR_OUT_OF_MEMORY
170170
#undef MLK_H
171+
#undef MLK_MAX3_
172+
#undef MLK_MAX_TOTAL_ALLOC
173+
#undef MLK_MAX_TOTAL_ALLOC_DECAPS
174+
#undef MLK_MAX_TOTAL_ALLOC_ENCAPS
175+
#undef MLK_MAX_TOTAL_ALLOC_KEYPAIR
176+
#undef MLK_TOTAL_ALLOC_1024
177+
#undef MLK_TOTAL_ALLOC_1024_DECAPS
178+
#undef MLK_TOTAL_ALLOC_1024_ENCAPS
179+
#undef MLK_TOTAL_ALLOC_1024_KEYPAIR
180+
#undef MLK_TOTAL_ALLOC_512
181+
#undef MLK_TOTAL_ALLOC_512_DECAPS
182+
#undef MLK_TOTAL_ALLOC_512_ENCAPS
183+
#undef MLK_TOTAL_ALLOC_512_KEYPAIR
184+
#undef MLK_TOTAL_ALLOC_768
185+
#undef MLK_TOTAL_ALLOC_768_DECAPS
186+
#undef MLK_TOTAL_ALLOC_768_ENCAPS
187+
#undef MLK_TOTAL_ALLOC_768_KEYPAIR
171188
#undef crypto_kem_check_pk
172189
#undef crypto_kem_check_sk
173190
#undef crypto_kem_dec

mlkem/mlkem_native.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,23 @@
157157
#undef MLK_ERR_FAIL
158158
#undef MLK_ERR_OUT_OF_MEMORY
159159
#undef MLK_H
160+
#undef MLK_MAX3_
161+
#undef MLK_MAX_TOTAL_ALLOC
162+
#undef MLK_MAX_TOTAL_ALLOC_DECAPS
163+
#undef MLK_MAX_TOTAL_ALLOC_ENCAPS
164+
#undef MLK_MAX_TOTAL_ALLOC_KEYPAIR
165+
#undef MLK_TOTAL_ALLOC_1024
166+
#undef MLK_TOTAL_ALLOC_1024_DECAPS
167+
#undef MLK_TOTAL_ALLOC_1024_ENCAPS
168+
#undef MLK_TOTAL_ALLOC_1024_KEYPAIR
169+
#undef MLK_TOTAL_ALLOC_512
170+
#undef MLK_TOTAL_ALLOC_512_DECAPS
171+
#undef MLK_TOTAL_ALLOC_512_ENCAPS
172+
#undef MLK_TOTAL_ALLOC_512_KEYPAIR
173+
#undef MLK_TOTAL_ALLOC_768
174+
#undef MLK_TOTAL_ALLOC_768_DECAPS
175+
#undef MLK_TOTAL_ALLOC_768_ENCAPS
176+
#undef MLK_TOTAL_ALLOC_768_KEYPAIR
160177
#undef crypto_kem_check_pk
161178
#undef crypto_kem_check_sk
162179
#undef crypto_kem_dec

mlkem/mlkem_native.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,4 +428,65 @@ int MLK_API_NAMESPACE(check_sk)(
428428
#endif /* MLK_CONFIG_API_NO_SUPERCOP */
429429
#endif /* !MLK_CONFIG_API_CONSTANTS_ONLY */
430430

431+
432+
/***************************** Memory Usage **********************************/
433+
434+
/*
435+
* By default mlkem-native performs all memory allocations on the stack.
436+
* Alternatively, mlkem-native supports custom allocation of large structures
437+
* through the `MLK_CONFIG_CUSTOM_ALLOC_FREE` configuration option.
438+
* See mlkem_native_config.h for details.
439+
*
440+
* `MLK_TOTAL_ALLOC_{512,768,1024}_{KEYPAIR,ENCAPS,DECAPS}` indicates the
441+
* maximum (accumulative) allocation via MLK_ALLOC for each parameter set and
442+
* operation. Note that some stack allocation remains even when using custom
443+
* allocators, so these values are lower than total stack usage with the default
444+
* stack-only allocation.
445+
*
446+
* These constants may be used to implement custom allocations using a
447+
* fixed-sized buffer and a simple allocator (e.g., bump allocator).
448+
*/
449+
/* check-magic: off */
450+
#define MLK_TOTAL_ALLOC_512_KEYPAIR 10048
451+
#define MLK_TOTAL_ALLOC_512_ENCAPS 8384
452+
#define MLK_TOTAL_ALLOC_512_DECAPS 9152
453+
#define MLK_TOTAL_ALLOC_768_KEYPAIR 15552
454+
#define MLK_TOTAL_ALLOC_768_ENCAPS 13248
455+
#define MLK_TOTAL_ALLOC_768_DECAPS 14336
456+
#define MLK_TOTAL_ALLOC_1024_KEYPAIR 22400
457+
#define MLK_TOTAL_ALLOC_1024_ENCAPS 19136
458+
#define MLK_TOTAL_ALLOC_1024_DECAPS 20704
459+
/* check-magic: on */
460+
461+
/*
462+
* `MLK_MAX_TOTAL_ALLOC_{KEYPAIR,ENCAPS,DECAPS}` is the maximum across all
463+
* parameter sets for each operation.
464+
* `MLK_MAX_TOTAL_ALLOC` is the maximum across all parameter sets and
465+
* operations.
466+
*/
467+
#define MLK_MAX_TOTAL_ALLOC_KEYPAIR MLK_TOTAL_ALLOC_1024_KEYPAIR
468+
#define MLK_MAX_TOTAL_ALLOC_ENCAPS MLK_TOTAL_ALLOC_1024_ENCAPS
469+
#define MLK_MAX_TOTAL_ALLOC_DECAPS MLK_TOTAL_ALLOC_1024_DECAPS
470+
471+
#define MLK_MAX3_(a, b, c) \
472+
((a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c)))
473+
474+
/*
475+
* `MLK_TOTAL_ALLOC_{512,768,1024}` is the maximum across all operations for
476+
* each parameter set.
477+
* `MLK_MAX_TOTAL_ALLOC` is the maximum across all parameter sets and
478+
* operations.
479+
*/
480+
#define MLK_TOTAL_ALLOC_512 \
481+
MLK_MAX3_(MLK_TOTAL_ALLOC_512_KEYPAIR, MLK_TOTAL_ALLOC_512_ENCAPS, \
482+
MLK_TOTAL_ALLOC_512_DECAPS)
483+
#define MLK_TOTAL_ALLOC_768 \
484+
MLK_MAX3_(MLK_TOTAL_ALLOC_768_KEYPAIR, MLK_TOTAL_ALLOC_768_ENCAPS, \
485+
MLK_TOTAL_ALLOC_768_DECAPS)
486+
#define MLK_TOTAL_ALLOC_1024 \
487+
MLK_MAX3_(MLK_TOTAL_ALLOC_1024_KEYPAIR, MLK_TOTAL_ALLOC_1024_ENCAPS, \
488+
MLK_TOTAL_ALLOC_1024_DECAPS)
489+
490+
#define MLK_MAX_TOTAL_ALLOC MLK_TOTAL_ALLOC_1024
491+
431492
#endif /* !MLK_H */

scripts/autogen

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2019,6 +2019,10 @@ def check_macro_typos():
20192019
if m in macros:
20202020
return True
20212021

2022+
# Ignore alloc macros only defined in mlkem_native.h
2023+
if m.startswith("MLK_TOTAL_ALLOC") or m.startswith("MLK_MAX_TOTAL_ALLOC"):
2024+
return True
2025+
20222026
is_autogen = filename == "scripts/autogen"
20232027

20242028
# Exclude system capabilities, which are enum values

0 commit comments

Comments
 (0)