Skip to content

Commit

Permalink
Config; Configure namespace prefix instead of namespace macro
Browse files Browse the repository at this point in the history
Previously, the user could customize namespacing by providing
macros `MLKEM_NAMESPACE(sym)` and `FIPS202_NAMESPACE(sym)`.

This mode of configuration is slightly inconvenient in that it
the namespace cannot be controlled from CFLAGS.

This commit addresses this, as follows: Instead of making the namespace
macro configurable, make the namespace _prefix_ configurable, and derive
the namespace macro: The config provides MLKEM_NAMESPACE_PREFIX and
FIPS202_NAMESPACE_PREIFX, and then the derived MLKEM_NAMESPACE(sym)
and FIPS202_NAMESPACE(sym) are the concatenation of that, an underscore `_`,
and the symbol name `sym`.

All configs and examples are adjusted accordingly.
  • Loading branch information
hanno-becker committed Jan 10, 2025
1 parent 81cb722 commit dd588fe
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 86 deletions.
17 changes: 6 additions & 11 deletions examples/custom_backend/mlkem_native/custom_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,20 @@
/* #define MLKEM_NATIVE_CONFIG_FILE "config.h" */

/******************************************************************************
* Name: MLKEM_NAMESPACE
* _MLKEM_NAMESPACE
* Name: MLKEM_NAMESPACE_PREFIX
*
* Description: The macros to use to namespace global symbols
* Description: The prefix to use to namespace global symbols
* from mlkem/.
*****************************************************************************/
#define __CONC(a, b) a##b
#define CONC(a, b) __CONC(a, b)

#define MLKEM_NAMESPACE(sym) CONC(CUSTOM_TINY_SHA3_, sym)
#define MLKEM_NAMESPACE_PREFIX CUSTOM_TINY_SHA3

/******************************************************************************
* Name: FIPS202_NAMESPACE
* _FIPS202_NAMESPACE
* Name: FIPS202_NAMESPACE_PREFIX
*
* Description: The macros to use to namespace global symbols
* Description: The prefix to use to namespace global symbols
* from mlkem/fips202/.
*****************************************************************************/
#define FIPS202_NAMESPACE(sym) CONC(CUSTOM_TINY_SHA3_, sym)
#define FIPS202_NAMESPACE_PREFIX CUSTOM_TINY_SHA3

/******************************************************************************
* Name: MLKEM_USE_NATIVE
Expand Down
19 changes: 12 additions & 7 deletions examples/monolithic_build/config_1024.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,26 @@
/* #define MLKEM_NATIVE_CONFIG_FILE "config.h" */

/******************************************************************************
* Name: MLKEM_NAMESPACE
* Name: MLKEM_NAMESPACE_PREFIX
*
* Description: The macros to use to namespace global symbols
* Description: The prefix to use to namespace global symbols
* from mlkem/.
*
* This can also be set using CFLAGS.
*
*****************************************************************************/
#define CONCAT(a, b) a##b
#define MLKEM_NAMESPACE(sym) CONCAT(mlkem1024_, sym)
#define MLKEM_NAMESPACE_PREFIX mlkem1024

/******************************************************************************
* Name: FIPS202_NAMESPACE
* Name: FIPS202_NAMESPACE_PREFIX
*
* Description: The macros to use to namespace global symbols
* Description: The prefix to use to namespace global symbols
* from mlkem/fips202/.
*
* This can also be set using CFLAGS.
*
*****************************************************************************/
#define FIPS202_NAMESPACE(sym) CONCAT(fips202_, sym)
#define FIPS202_NAMESPACE_PREFIX fips202

/******************************************************************************
* Name: MLKEM_USE_NATIVE
Expand Down
19 changes: 12 additions & 7 deletions examples/monolithic_build/config_512.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,26 @@
/* #define MLKEM_NATIVE_CONFIG_FILE "config.h" */

/******************************************************************************
* Name: MLKEM_NAMESPACE
* Name: MLKEM_NAMESPACE_PREFIX
*
* Description: The macros to use to namespace global symbols
* Description: The prefix to use to namespace global symbols
* from mlkem/.
*
* This can also be set using CFLAGS.
*
*****************************************************************************/
#define CONCAT(a, b) a##b
#define MLKEM_NAMESPACE(sym) CONCAT(mlkem512_, sym)
#define MLKEM_NAMESPACE_PREFIX mlkem512

/******************************************************************************
* Name: FIPS202_NAMESPACE
* Name: FIPS202_NAMESPACE_PREFIX
*
* Description: The macros to use to namespace global symbols
* Description: The prefix to use to namespace global symbols
* from mlkem/fips202/.
*
* This can also be set using CFLAGS.
*
*****************************************************************************/
#define FIPS202_NAMESPACE(sym) CONCAT(fips202_, sym)
#define FIPS202_NAMESPACE_PREFIX fips202

/******************************************************************************
* Name: MLKEM_USE_NATIVE
Expand Down
19 changes: 12 additions & 7 deletions examples/monolithic_build/config_768.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,26 @@
/* #define MLKEM_NATIVE_CONFIG_FILE "config.h" */

/******************************************************************************
* Name: MLKEM_NAMESPACE
* Name: MLKEM_NAMESPACE_PREFIX
*
* Description: The macros to use to namespace global symbols
* Description: The prefix to use to namespace global symbols
* from mlkem/.
*
* This can also be set using CFLAGS.
*
*****************************************************************************/
#define CONCAT(a, b) a##b
#define MLKEM_NAMESPACE(sym) CONCAT(mlkem768_, sym)
#define MLKEM_NAMESPACE_PREFIX mlkem768

/******************************************************************************
* Name: FIPS202_NAMESPACE
* Name: FIPS202_NAMESPACE_PREFIX
*
* Description: The macros to use to namespace global symbols
* Description: The prefix to use to namespace global symbols
* from mlkem/fips202/.
*
* This can also be set using CFLAGS.
*
*****************************************************************************/
#define FIPS202_NAMESPACE(sym) CONCAT(fips202_, sym)
#define FIPS202_NAMESPACE_PREFIX fips202

/******************************************************************************
* Name: MLKEM_USE_NATIVE
Expand Down
74 changes: 42 additions & 32 deletions examples/monolithic_build/mlkem_native_monobuild.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,26 @@
#undef MLKEM_NATIVE_INTERNAL_API
#endif

/* mlkem/common.h */
#if defined(MLKEM_NATIVE_MAKE_NAMESPACE_)
#undef MLKEM_NATIVE_MAKE_NAMESPACE_
#endif

/* mlkem/common.h */
#if defined(MLKEM_NATIVE_MAKE_NAMESPACE)
#undef MLKEM_NATIVE_MAKE_NAMESPACE
#endif

/* mlkem/common.h */
#if defined(FIPS202_NAMESPACE)
#undef FIPS202_NAMESPACE
#endif

/* mlkem/common.h */
#if defined(MLKEM_NAMESPACE)
#undef MLKEM_NAMESPACE
#endif

/* mlkem/common.h */
#if defined(MLKEM_ASM_NAMESPACE)
#undef MLKEM_ASM_NAMESPACE
Expand Down Expand Up @@ -289,13 +309,13 @@
#endif

/* mlkem/config.h */
#if defined(MLKEM_NAMESPACE)
#undef MLKEM_NAMESPACE
#if defined(MLKEM_NAMESPACE_PREFIX)
#undef MLKEM_NAMESPACE_PREFIX
#endif

/* mlkem/config.h */
#if defined(FIPS202_NAMESPACE)
#undef FIPS202_NAMESPACE
#if defined(FIPS202_NAMESPACE_PREFIX)
#undef FIPS202_NAMESPACE_PREFIX
#endif

/* mlkem/config.h */
Expand All @@ -309,43 +329,23 @@
#endif

/* mlkem/config.h */
#if defined(FIPS202_DEFAULT_NAMESPACE___)
#undef FIPS202_DEFAULT_NAMESPACE___
#endif

/* mlkem/config.h */
#if defined(FIPS202_DEFAULT_NAMESPACE__)
#undef FIPS202_DEFAULT_NAMESPACE__
#endif

/* mlkem/config.h */
#if defined(FIPS202_DEFAULT_NAMESPACE)
#undef FIPS202_DEFAULT_NAMESPACE
#endif

/* mlkem/config.h */
#if defined(MLKEM_DEFAULT_NAMESPACE___)
#undef MLKEM_DEFAULT_NAMESPACE___
#if defined(FIPS202_DEFAULT_NAMESPACE_PREFIX)
#undef FIPS202_DEFAULT_NAMESPACE_PREFIX
#endif

/* mlkem/config.h */
#if defined(MLKEM_DEFAULT_NAMESPACE__)
#undef MLKEM_DEFAULT_NAMESPACE__
#if defined(MLKEM_DEFAULT_NAMESPACE_PREFIX)
#undef MLKEM_DEFAULT_NAMESPACE_PREFIX
#endif

/* mlkem/config.h */
#if defined(MLKEM_DEFAULT_NAMESPACE)
#undef MLKEM_DEFAULT_NAMESPACE
#if defined(MLKEM_DEFAULT_NAMESPACE_PREFIX)
#undef MLKEM_DEFAULT_NAMESPACE_PREFIX
#endif

/* mlkem/config.h */
#if defined(MLKEM_DEFAULT_NAMESPACE)
#undef MLKEM_DEFAULT_NAMESPACE
#endif

/* mlkem/config.h */
#if defined(MLKEM_DEFAULT_NAMESPACE)
#undef MLKEM_DEFAULT_NAMESPACE
#if defined(MLKEM_DEFAULT_NAMESPACE_PREFIX)
#undef MLKEM_DEFAULT_NAMESPACE_PREFIX
#endif

/* mlkem/debug/debug.c */
Expand Down Expand Up @@ -638,6 +638,16 @@
#undef BUILD_INFO_LVL
#endif

/* mlkem/mlkem_native.h */
#if defined(BUILD_INFO_CONCAT_)
#undef BUILD_INFO_CONCAT_
#endif

/* mlkem/mlkem_native.h */
#if defined(BUILD_INFO_CONCAT)
#undef BUILD_INFO_CONCAT
#endif

/* mlkem/mlkem_native.h */
#if defined(BUILD_INFO_NAMESPACE)
#undef BUILD_INFO_NAMESPACE
Expand Down
9 changes: 9 additions & 0 deletions mlkem/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@
#define MLKEM_NATIVE_INTERNAL_API static
#endif

#define MLKEM_NATIVE_MAKE_NAMESPACE_(x1, x2) x1##_##x2
#define MLKEM_NATIVE_MAKE_NAMESPACE(x1, x2) MLKEM_NATIVE_MAKE_NAMESPACE_(x1, x2)

#define FIPS202_NAMESPACE(s) \
MLKEM_NATIVE_MAKE_NAMESPACE(FIPS202_NAMESPACE_PREFIX, s)

#define MLKEM_NAMESPACE(s) \
MLKEM_NATIVE_MAKE_NAMESPACE(MLKEM_NAMESPACE_PREFIX, s)

/* On Apple platforms, we need to emit leading underscore
* in front of assembly symbols. We thus introducee a separate
* namespace wrapper for ASM symbols. */
Expand Down
37 changes: 18 additions & 19 deletions mlkem/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,28 @@
/******************************************************************************
* Name: MLKEM_NAMESPACE
*
* Description: The macros to use to namespace global symbols
* Description: The prefix to use to namespace global symbols
* from mlkem/.
*
* This can also be set using CFLAGS.
*
*****************************************************************************/
#define MLKEM_NAMESPACE(sym) MLKEM_DEFAULT_NAMESPACE(sym)
#if !defined(MLKEM_NAMESPACE_PREFIX)
#define MLKEM_NAMESPACE_PREFIX MLKEM_DEFAULT_NAMESPACE_PREFIX
#endif

/******************************************************************************
* Name: FIPS202_NAMESPACE
*
* Description: The macros to use to namespace global symbols
* Description: The prefix to use to namespace global symbols
* from mlkem/fips202/.
*
* This can also be set using CFLAGS.
*
*****************************************************************************/
#define FIPS202_NAMESPACE(sym) FIPS202_DEFAULT_NAMESPACE(sym)
#if !defined(FIPS202_NAMESPACE_PREFIX)
#define FIPS202_NAMESPACE_PREFIX FIPS202_DEFAULT_NAMESPACE_PREFIX
#endif

/******************************************************************************
* Name: MLKEM_USE_NATIVE
Expand Down Expand Up @@ -113,11 +123,7 @@
* e.g., PQCP_MLKEM_NATIVE_FIPS202_C_
*/

#define FIPS202_DEFAULT_NAMESPACE___(x1, x2) x1##_##x2
#define FIPS202_DEFAULT_NAMESPACE__(x1, x2) FIPS202_DEFAULT_NAMESPACE___(x1, x2)

#define FIPS202_DEFAULT_NAMESPACE(s) \
FIPS202_DEFAULT_NAMESPACE__(PQCP_MLKEM_NATIVE_FIPS202, s)
#define FIPS202_DEFAULT_NAMESPACE_PREFIX PQCP_MLKEM_NATIVE_FIPS202

/*
* The default MLKEM namespace is
Expand All @@ -127,19 +133,12 @@
* e.g., PQCP_MLKEM_NATIVE_MLKEM512_AARCH64_OPT_
*/

#define MLKEM_DEFAULT_NAMESPACE___(x1, x2, x3) x1##_##x2##_##x3
#define MLKEM_DEFAULT_NAMESPACE__(x1, x2, x3) \
MLKEM_DEFAULT_NAMESPACE___(x1, x2, x3)

#if MLKEM_K == 2
#define MLKEM_DEFAULT_NAMESPACE(s) \
MLKEM_DEFAULT_NAMESPACE__(PQCP_MLKEM_NATIVE, MLKEM512, s)
#define MLKEM_DEFAULT_NAMESPACE_PREFIX PQCP_MLKEM_NATIVE_MLKEM512
#elif MLKEM_K == 3
#define MLKEM_DEFAULT_NAMESPACE(s) \
MLKEM_DEFAULT_NAMESPACE__(PQCP_MLKEM_NATIVE, MLKEM768, s)
#define MLKEM_DEFAULT_NAMESPACE_PREFIX PQCP_MLKEM_NATIVE_MLKEM768
#elif MLKEM_K == 4
#define MLKEM_DEFAULT_NAMESPACE(s) \
MLKEM_DEFAULT_NAMESPACE__(PQCP_MLKEM_NATIVE, MLKEM1024, s)
#define MLKEM_DEFAULT_NAMESPACE_PREFIX PQCP_MLKEM_NATIVE_MLKEM1024
#endif

#endif /* MLkEM_NATIVE_CONFIG_H */
8 changes: 5 additions & 3 deletions mlkem/mlkem_native.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@
#error MLKEM_K not set by config file
#endif

#ifndef MLKEM_NAMESPACE
#error MLKEM_NAMESPACE not set by config file
#ifndef MLKEM_NAMESPACE_PREFIX
#error MLKEM_NAMESPACE_PREFIX not set by config file
#endif

#define BUILD_INFO_NAMESPACE(sym) MLKEM_NAMESPACE(sym)
#define BUILD_INFO_CONCAT_(x, y) x##_##y
#define BUILD_INFO_CONCAT(x, y) BUILD_INFO_CONCAT_(x, y)
#define BUILD_INFO_NAMESPACE(sym) BUILD_INFO_CONCAT(MLKEM_NAMESPACE_PREFIX, sym)

#endif /* BUILD_INFO_LVL */

Expand Down

0 comments on commit dd588fe

Please sign in to comment.