Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/baremetal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ jobs:
func: true
kat: true
acvp: true
alloc: true
- runner: ubuntu-latest
name: 'AVR ATmega128RFR2 (modified for 32K RAM)'
makefile: test/baremetal/platform/avr/platform.mk
nix-shell: avr
func: true
kat: true
acvp: true
alloc: false
runs-on: ${{ matrix.target.runner }}
steps:
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
Expand All @@ -46,4 +48,4 @@ jobs:
acvp: ${{ matrix.target.acvp }}
examples: false
stack: false
alloc: false
alloc: ${{ matrix.target.alloc }}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
*
* This configuration differs from the default mlkem/mlkem_native_config.h in
* the following places:
* - MLK_CONFIG_NO_SUPERCOP
* - MLK_CONFIG_MULTILEVEL_BUILD
* - MLK_CONFIG_NAMESPACE_PREFIX
* - MLK_CONFIG_INTERNAL_API_QUALIFIER
Expand Down Expand Up @@ -156,7 +157,7 @@
* naming does not disambiguate between the parameter sets.
*
*****************************************************************************/
/* #define MLK_CONFIG_NO_SUPERCOP */
#define MLK_CONFIG_NO_SUPERCOP

/******************************************************************************
* Name: MLK_CONFIG_CONSTANTS_ONLY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
*
* This configuration differs from the default mlkem/mlkem_native_config.h in
* the following places:
* - MLK_CONFIG_NO_SUPERCOP
* - MLK_CONFIG_MULTILEVEL_BUILD
* - MLK_CONFIG_NAMESPACE_PREFIX
* - MLK_CONFIG_USE_NATIVE_BACKEND_ARITH
Expand Down Expand Up @@ -160,7 +161,7 @@
* naming does not disambiguate between the parameter sets.
*
*****************************************************************************/
/* #define MLK_CONFIG_NO_SUPERCOP */
#define MLK_CONFIG_NO_SUPERCOP

/******************************************************************************
* Name: MLK_CONFIG_CONSTANTS_ONLY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
*
* This configuration differs from the default mlkem/mlkem_native_config.h in
* the following places:
* - MLK_CONFIG_NO_SUPERCOP
* - MLK_CONFIG_MULTILEVEL_BUILD
* - MLK_CONFIG_NAMESPACE_PREFIX
*/
Expand Down Expand Up @@ -155,7 +156,7 @@
* naming does not disambiguate between the parameter sets.
*
*****************************************************************************/
/* #define MLK_CONFIG_NO_SUPERCOP */
#define MLK_CONFIG_NO_SUPERCOP

/******************************************************************************
* Name: MLK_CONFIG_CONSTANTS_ONLY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
*
* This configuration differs from the default mlkem/mlkem_native_config.h in
* the following places:
* - MLK_CONFIG_NO_SUPERCOP
* - MLK_CONFIG_MULTILEVEL_BUILD
* - MLK_CONFIG_NAMESPACE_PREFIX
* - MLK_CONFIG_USE_NATIVE_BACKEND_ARITH
Expand Down Expand Up @@ -157,7 +158,7 @@
* naming does not disambiguate between the parameter sets.
*
*****************************************************************************/
/* #define MLK_CONFIG_NO_SUPERCOP */
#define MLK_CONFIG_NO_SUPERCOP

/******************************************************************************
* Name: MLK_CONFIG_CONSTANTS_ONLY
Expand Down
19 changes: 19 additions & 0 deletions mlkem/mlkem_native.S
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,25 @@
#undef MLK_ERR_FAIL
#undef MLK_ERR_OUT_OF_MEMORY
#undef MLK_H
#undef MLK_MAX3_
#undef MLK_TOTAL_ALLOC_1024
#undef MLK_TOTAL_ALLOC_1024_DECAPS
#undef MLK_TOTAL_ALLOC_1024_ENCAPS
#undef MLK_TOTAL_ALLOC_1024_KEYPAIR
#undef MLK_TOTAL_ALLOC_1024_KEYPAIR_NO_PCT
#undef MLK_TOTAL_ALLOC_1024_KEYPAIR_PCT
#undef MLK_TOTAL_ALLOC_512
#undef MLK_TOTAL_ALLOC_512_DECAPS
#undef MLK_TOTAL_ALLOC_512_ENCAPS
#undef MLK_TOTAL_ALLOC_512_KEYPAIR
#undef MLK_TOTAL_ALLOC_512_KEYPAIR_NO_PCT
#undef MLK_TOTAL_ALLOC_512_KEYPAIR_PCT
#undef MLK_TOTAL_ALLOC_768
#undef MLK_TOTAL_ALLOC_768_DECAPS
#undef MLK_TOTAL_ALLOC_768_ENCAPS
#undef MLK_TOTAL_ALLOC_768_KEYPAIR
#undef MLK_TOTAL_ALLOC_768_KEYPAIR_NO_PCT
#undef MLK_TOTAL_ALLOC_768_KEYPAIR_PCT
#undef crypto_kem_check_pk
#undef crypto_kem_check_sk
#undef crypto_kem_dec
Expand Down
19 changes: 19 additions & 0 deletions mlkem/mlkem_native.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,25 @@
#undef MLK_ERR_FAIL
#undef MLK_ERR_OUT_OF_MEMORY
#undef MLK_H
#undef MLK_MAX3_
#undef MLK_TOTAL_ALLOC_1024
#undef MLK_TOTAL_ALLOC_1024_DECAPS
#undef MLK_TOTAL_ALLOC_1024_ENCAPS
#undef MLK_TOTAL_ALLOC_1024_KEYPAIR
#undef MLK_TOTAL_ALLOC_1024_KEYPAIR_NO_PCT
#undef MLK_TOTAL_ALLOC_1024_KEYPAIR_PCT
#undef MLK_TOTAL_ALLOC_512
#undef MLK_TOTAL_ALLOC_512_DECAPS
#undef MLK_TOTAL_ALLOC_512_ENCAPS
#undef MLK_TOTAL_ALLOC_512_KEYPAIR
#undef MLK_TOTAL_ALLOC_512_KEYPAIR_NO_PCT
#undef MLK_TOTAL_ALLOC_512_KEYPAIR_PCT
#undef MLK_TOTAL_ALLOC_768
#undef MLK_TOTAL_ALLOC_768_DECAPS
#undef MLK_TOTAL_ALLOC_768_ENCAPS
#undef MLK_TOTAL_ALLOC_768_KEYPAIR
#undef MLK_TOTAL_ALLOC_768_KEYPAIR_NO_PCT
#undef MLK_TOTAL_ALLOC_768_KEYPAIR_PCT
#undef crypto_kem_check_pk
#undef crypto_kem_check_sk
#undef crypto_kem_dec
Expand Down
65 changes: 65 additions & 0 deletions mlkem/mlkem_native.h
Original file line number Diff line number Diff line change
Expand Up @@ -465,4 +465,69 @@ int MLK_API_NAMESPACE(check_sk)(
#endif /* MLK_CONFIG_API_NO_SUPERCOP */
#endif /* !MLK_CONFIG_API_CONSTANTS_ONLY */


/***************************** Memory Usage **********************************/

/*
* By default mlkem-native performs all memory allocations on the stack.
* Alternatively, mlkem-native supports custom allocation of large structures
* through the `MLK_CONFIG_CUSTOM_ALLOC_FREE` configuration option.
* See mlkem_native_config.h for details.
*
* `MLK_TOTAL_ALLOC_{512,768,1024}_{KEYPAIR,ENCAPS,DECAPS}` indicates the
* maximum (accumulative) allocation via MLK_ALLOC for each parameter set and
* operation. Note that some stack allocation remains even when using custom
* allocators, so these values are lower than total stack usage with the default
* stack-only allocation.
*
* These constants may be used to implement custom allocations using a
* fixed-sized buffer and a simple allocator (e.g., bump allocator).
*/
/* check-magic: off */
#define MLK_TOTAL_ALLOC_512_KEYPAIR_NO_PCT 5824
#define MLK_TOTAL_ALLOC_512_KEYPAIR_PCT 10048
#define MLK_TOTAL_ALLOC_512_ENCAPS 8384
#define MLK_TOTAL_ALLOC_512_DECAPS 9152
#define MLK_TOTAL_ALLOC_768_KEYPAIR_NO_PCT 10176
#define MLK_TOTAL_ALLOC_768_KEYPAIR_PCT 15552
#define MLK_TOTAL_ALLOC_768_ENCAPS 13248
#define MLK_TOTAL_ALLOC_768_DECAPS 14336
#define MLK_TOTAL_ALLOC_1024_KEYPAIR_NO_PCT 15552
#define MLK_TOTAL_ALLOC_1024_KEYPAIR_PCT 22400
#define MLK_TOTAL_ALLOC_1024_ENCAPS 19136
#define MLK_TOTAL_ALLOC_1024_DECAPS 20704
/* check-magic: on */

/*
* MLK_TOTAL_ALLOC_*_KEYPAIR adapts based on MLK_CONFIG_KEYGEN_PCT.
* For legacy config, we don't know which options are used, so assume
* the worst case (PCT enabled).
*/
#if defined(MLK_API_LEGACY_CONFIG) || defined(MLK_CONFIG_KEYGEN_PCT)
#define MLK_TOTAL_ALLOC_512_KEYPAIR MLK_TOTAL_ALLOC_512_KEYPAIR_PCT
#define MLK_TOTAL_ALLOC_768_KEYPAIR MLK_TOTAL_ALLOC_768_KEYPAIR_PCT
#define MLK_TOTAL_ALLOC_1024_KEYPAIR MLK_TOTAL_ALLOC_1024_KEYPAIR_PCT
#else
#define MLK_TOTAL_ALLOC_512_KEYPAIR MLK_TOTAL_ALLOC_512_KEYPAIR_NO_PCT
#define MLK_TOTAL_ALLOC_768_KEYPAIR MLK_TOTAL_ALLOC_768_KEYPAIR_NO_PCT
#define MLK_TOTAL_ALLOC_1024_KEYPAIR MLK_TOTAL_ALLOC_1024_KEYPAIR_NO_PCT
#endif

#define MLK_MAX3_(a, b, c) \
((a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c)))

/*
* `MLK_TOTAL_ALLOC_{512,768,1024}` is the maximum across all operations for
* each parameter set.
*/
#define MLK_TOTAL_ALLOC_512 \
MLK_MAX3_(MLK_TOTAL_ALLOC_512_KEYPAIR, MLK_TOTAL_ALLOC_512_ENCAPS, \
MLK_TOTAL_ALLOC_512_DECAPS)
#define MLK_TOTAL_ALLOC_768 \
MLK_MAX3_(MLK_TOTAL_ALLOC_768_KEYPAIR, MLK_TOTAL_ALLOC_768_ENCAPS, \
MLK_TOTAL_ALLOC_768_DECAPS)
#define MLK_TOTAL_ALLOC_1024 \
MLK_MAX3_(MLK_TOTAL_ALLOC_1024_KEYPAIR, MLK_TOTAL_ALLOC_1024_ENCAPS, \
MLK_TOTAL_ALLOC_1024_DECAPS)

#endif /* !MLK_H */
4 changes: 4 additions & 0 deletions scripts/autogen
Original file line number Diff line number Diff line change
Expand Up @@ -2025,6 +2025,10 @@ def check_macro_typos():
if m in macros:
return True

# Ignore alloc macros only defined in mlkem_native.h
if m.startswith("MLK_TOTAL_ALLOC") or m.startswith("MLK_MAX_TOTAL_ALLOC"):
return True

is_autogen = filename == "scripts/autogen"

# Exclude system capabilities, which are enum values
Expand Down
9 changes: 4 additions & 5 deletions test/configs/configs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ configs:
- path: examples/monolithic_build_multilevel/mlkem_native/mlkem_native_config.h
description: "Multilevel monolithic build config"
defines:
MLK_CONFIG_NO_SUPERCOP: true
MLK_CONFIG_MULTILEVEL_BUILD: true
MLK_CONFIG_NAMESPACE_PREFIX: mlkem
MLK_CONFIG_INTERNAL_API_QUALIFIER: static
Expand All @@ -310,6 +311,7 @@ configs:
- path: examples/multilevel_build/mlkem_native/mlkem_native_config.h
description: "Multilevel build config"
defines:
MLK_CONFIG_NO_SUPERCOP: true
MLK_CONFIG_MULTILEVEL_BUILD: true
MLK_CONFIG_NAMESPACE_PREFIX: mlkem
MLK_CONFIG_FILE:
Expand All @@ -318,6 +320,7 @@ configs:
- path: examples/multilevel_build_native/mlkem_native/mlkem_native_config.h
description: "Multilevel build config"
defines:
MLK_CONFIG_NO_SUPERCOP: true
MLK_CONFIG_MULTILEVEL_BUILD: true
MLK_CONFIG_NAMESPACE_PREFIX: mlkem
MLK_CONFIG_USE_NATIVE_BACKEND_ARITH: true
Expand All @@ -328,6 +331,7 @@ configs:
- path: examples/monolithic_build_multilevel_native/mlkem_native/mlkem_native_config.h
description: "Multilevel monolithic build config with native backends"
defines:
MLK_CONFIG_NO_SUPERCOP: true
MLK_CONFIG_MULTILEVEL_BUILD: true
MLK_CONFIG_NAMESPACE_PREFIX: mlkem
MLK_CONFIG_USE_NATIVE_BACKEND_ARITH: true
Expand Down Expand Up @@ -430,11 +434,6 @@ configs:
description: "Using custom allocation that can be made fail at specific invocation"
defines:
MLK_CONFIG_NAMESPACE_PREFIX: mlk
MLK_CONFIG_KEYGEN_PCT:
content: |
#if !defined(MLK_CONFIG_KEYGEN_PCT)
#define MLK_CONFIG_KEYGEN_PCT
#endif
MLK_CONFIG_CONTEXT_PARAMETER: true
MLK_CONFIG_CONTEXT_PARAMETER_TYPE: struct test_ctx_t *
MLK_CONFIG_CUSTOM_ALLOC_FREE:
Expand Down
6 changes: 1 addition & 5 deletions test/configs/test_alloc_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
* This configuration differs from the default mlkem/mlkem_native_config.h in
* the following places:
* - MLK_CONFIG_NAMESPACE_PREFIX
* - MLK_CONFIG_KEYGEN_PCT
* - MLK_CONFIG_CONTEXT_PARAMETER
* - MLK_CONFIG_CONTEXT_PARAMETER_TYPE
* - MLK_CONFIG_CUSTOM_ALLOC_FREE
Expand Down Expand Up @@ -635,10 +634,7 @@ void custom_free(struct test_ctx_t *ctx, void *p, size_t sz, const char *file,
* key generation.
*
*****************************************************************************/
#if !defined(MLK_CONFIG_KEYGEN_PCT)
#define MLK_CONFIG_KEYGEN_PCT
#endif

/* #define MLK_CONFIG_KEYGEN_PCT */

/******************************************************************************
* Name: MLK_CONFIG_KEYGEN_PCT_BREAKAGE_TEST
Expand Down
Loading