Skip to content

Commit

Permalink
Demonstrate multi-level build in single CU
Browse files Browse the repository at this point in the history
This commit extends `examples/monolithic_build` to include
two instances of mlkem-native in the same compilation unit,
with different configurations. This is enabled through the
previous commits, 1) #undef'ing all #define's, 2) namespacing
statics, 3) namespacing structs.

Signed-off-by: Hanno Becker <[email protected]>
  • Loading branch information
hanno-becker committed Jan 5, 2025
1 parent db99555 commit 38fc271
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 2 deletions.
5 changes: 3 additions & 2 deletions examples/monolithic_build/config_a.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,16 @@
* Description: The macros to use to namespace global symbols
* from mlkem/.
*****************************************************************************/
#define MLKEM_NAMESPACE(sym) MLKEM_DEFAULT_NAMESPACE(sym)
#define CONCAT(a, b) a##b
#define MLKEM_NAMESPACE(sym) CONCAT(namespace_a_, sym)

/******************************************************************************
* Name: FIPS202_NAMESPACE
*
* Description: The macros to use to namespace global symbols
* from mlkem/fips202/.
*****************************************************************************/
#define FIPS202_NAMESPACE(sym) FIPS202_DEFAULT_NAMESPACE(sym)
#define FIPS202_NAMESPACE(sym) CONCAT(namespace_a_, sym)

/******************************************************************************
* Name: MLKEM_USE_NATIVE
Expand Down
97 changes: 97 additions & 0 deletions examples/monolithic_build/config_b.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright (c) 2024 The mlkem-native project authors
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef MLKEM_NATIVE_CONFIG_H
#define MLKEM_NATIVE_CONFIG_H

/******************************************************************************
* Name: MLKEM_K
*
* Description: Determines the security level for ML-KEM
* - MLKEM_K=2 corresponds to ML-KEM-512
* - MLKEM_K=3 corresponds to ML-KEM-768
* - MLKEM_K=4 corresponds to ML-KEM-1024
*
* This can also be set using CFLAGS.
*
*****************************************************************************/
#ifndef MLKEM_K
#define MLKEM_K 4 /* Change this for different security strengths */
#endif

/******************************************************************************
* Name: MLKEM_NATIVE_CONFIG_FILE
*
* Description: If defined, this is a header that will be included instead
* of mlkem/config.h.
*
* This _must_ be set on the command line using
* `-DMLKEM_NATIVE_CONFIG_FILE="..."`.
*
* When you need to build mlkem-native in multiple configurations,
* using varying MLKEM_NATIE_CONFIG_FILE can be more convenient
* then configuring everything through CFLAGS.
*
*****************************************************************************/
/* #define MLKEM_NATIVE_CONFIG_FILE "config.h" */

/******************************************************************************
* Name: MLKEM_NAMESPACE
*
* Description: The macros to use to namespace global symbols
* from mlkem/.
*****************************************************************************/
#define CONCAT(a, b) a##b
#define MLKEM_NAMESPACE(sym) CONCAT(namespace_b_, sym)

/******************************************************************************
* Name: FIPS202_NAMESPACE
*
* Description: The macros to use to namespace global symbols
* from mlkem/fips202/.
*****************************************************************************/
#define FIPS202_NAMESPACE(sym) CONCAT(namespace_b_, sym)

/******************************************************************************
* Name: MLKEM_USE_NATIVE
*
* Description: Determines whether a native backend should
* be used, if available.
*
* This can also be set using CFLAGS.
*
*****************************************************************************/
/* #define MLKEM_USE_NATIVE */

/******************************************************************************
* Name: MLKEM_NATIVE_ARITH_BACKEND
*
* Description: The arithmetic backend to use.
*
* This must be the filename of an arithmetic backend.
* See the existing backends for examples.
*
* This can be set using CFLAGS.
*
*****************************************************************************/
#if defined(MLKEM_USE_NATIVE) && !defined(MLKEM_NATIVE_ARITH_BACKEND)
#define MLKEM_NATIVE_ARITH_BACKEND "native/default.h"
#endif /* MLKEM_NATIVE_ARITH_BACKEND */

/******************************************************************************
* Name: MLKEM_NATIVE_FIPS202_BACKEND
*
* Description: The FIPS-202 backend to use.
*
* This must be the filename of an FIPS-202 backend.
*
* This can be set using CFLAGS.
*
*****************************************************************************/
#if defined(MLKEM_USE_NATIVE) && !defined(MLKEM_NATIVE_FIPS202_BACKEND)
#define MLKEM_NATIVE_FIPS202_BACKEND "fips202/native/default.h"
#endif /* MLKEM_NATIVE_FIPS202_BACKEND */

#endif /* MLkEM_NATIVE_CONFIG_H */
4 changes: 4 additions & 0 deletions examples/monolithic_build/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
#include "mlkem_native_all.c"
#undef MLKEM_NATIVE_CONFIG_FILE

#define MLKEM_NATIVE_CONFIG_FILE "config_b.h"
#include "mlkem_native_all.c"
#undef MLKEM_NATIVE_CONFIG_FILE

int main(void)
{
/* Nothing to do here -- only test that it builds */
Expand Down

0 comments on commit 38fc271

Please sign in to comment.