Skip to content

Commit

Permalink
Remove backend from default namespace
Browse files Browse the repository at this point in the history
Previously, the backend type would enter the default namespace
used by mlkem-native.

This means that before the MLKEM_NAMESPACE/FIPS202_NAMESPACE macros
can be used, the backend header has to be included.

This is inconvenient for the introduction of a public API header which
solely depends on the config file, since that header would need to also
include the backend header, and hence be compiled with the same include
paths as the mlkem-native build itself. This is unintuitive and unnecessarily
complicated for the customer.

This commit removes the backend type from the default namespace. Customers
who need the backend type, or at least the target architecture, to be
reflected in the namespace, can overwrite the default namespace as desired.

Signed-off-by: Hanno Becker <[email protected]>
  • Loading branch information
hanno-becker committed Jan 6, 2025
1 parent 8868f4e commit f3029dd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
11 changes: 8 additions & 3 deletions mlkem/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "params.h"
#include "sys.h"
#include "namespace.h"

/* Include backend metadata */
#if defined(MLKEM_USE_NATIVE)
Expand All @@ -22,9 +23,13 @@
#endif
#endif

/* This must come after the inclusion of the backend metadata
* since the backend choice may be part of the namespace. */
#include "namespace.h"
#if !defined(MLKEM_NATIVE_ARITH_BACKEND_NAME)
#define MLKEM_NATIVE_ARITH_BACKEND_NAME C
#endif

#if !defined(MLKEM_NATIVE_FIPS202_BACKEND_NAME)
#define MLKEM_NATIVE_FIPS202_BACKEND_NAME C
#endif

/* On Apple platforms, we need to emit leading underscore
* in front of assembly symbols. We thus introducee a separate
Expand Down
33 changes: 11 additions & 22 deletions mlkem/namespace.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
#ifndef MLKEM_NATIVE_NAMESPACE_H
#define MLKEM_NATIVE_NAMESPACE_H

#if !defined(MLKEM_NATIVE_ARITH_BACKEND_NAME)
#define MLKEM_NATIVE_ARITH_BACKEND_NAME C
#endif

/* Don't change parameters below this line */
#if (MLKEM_K == 2)
#define MLKEM_PARAM_NAME MLKEM512
Expand All @@ -20,32 +16,25 @@
#error "MLKEM_K must be in {2,3,4}"
#endif

#define ___MLKEM_DEFAULT_NAMESPACE(x1, x2, x3, x4) x1##_##x2##_##x3##_##x4
#define __MLKEM_DEFAULT_NAMESPACE(x1, x2, x3, x4) \
___MLKEM_DEFAULT_NAMESPACE(x1, x2, x3, x4)
#define ___MLKEM_DEFAULT_NAMESPACE(x1, x2, x3) x1##_##x2##_##x3
#define __MLKEM_DEFAULT_NAMESPACE(x1, x2, x3) \
___MLKEM_DEFAULT_NAMESPACE(x1, x2, x3)

/*
* NAMESPACE is PQCP_MLKEM_NATIVE_<PARAM_NAME>_<BACKEND>_
* e.g., PQCP_MLKEM_NATIVE_MLKEM512_AARCH64_OPT_
* e.g., PQCP_MLKEM_NATIVE_MLKEM512_
*/
#define MLKEM_DEFAULT_NAMESPACE(s) \
__MLKEM_DEFAULT_NAMESPACE(PQCP_MLKEM_NATIVE, MLKEM_PARAM_NAME, \
MLKEM_NATIVE_ARITH_BACKEND_NAME, s)

#if !defined(MLKEM_NATIVE_FIPS202_BACKEND_NAME)
#define MLKEM_NATIVE_FIPS202_BACKEND_NAME C
#endif
#define MLKEM_DEFAULT_NAMESPACE(s) \
__MLKEM_DEFAULT_NAMESPACE(PQCP_MLKEM_NATIVE, MLKEM_PARAM_NAME, s)

#define ___FIPS202_DEFAULT_NAMESPACE(x1, x2, x3) x1##_##x2##_##x3
#define __FIPS202_DEFAULT_NAMESPACE(x1, x2, x3) \
___FIPS202_DEFAULT_NAMESPACE(x1, x2, x3)
#define ___FIPS202_DEFAULT_NAMESPACE(x1, x2) x1##_##x2
#define __FIPS202_DEFAULT_NAMESPACE(x1, x2) ___FIPS202_DEFAULT_NAMESPACE(x1, x2)

/*
* NAMESPACE is PQCP_MLKEM_NATIVE_FIPS202_<BACKEND>_
* e.g., PQCP_MLKEM_NATIVE_FIPS202_X86_64_XKCP_
* e.g., PQCP_MLKEM_NATIVE_FIPS202_
*/
#define FIPS202_DEFAULT_NAMESPACE(s) \
__FIPS202_DEFAULT_NAMESPACE(PQCP_MLKEM_NATIVE_FIPS202, \
MLKEM_NATIVE_FIPS202_BACKEND_NAME, s)
#define FIPS202_DEFAULT_NAMESPACE(s) \
__FIPS202_DEFAULT_NAMESPACE(PQCP_MLKEM_NATIVE_FIPS202, s)

#endif /* MLKEM_NATIVE_NAMESPACE_H */

0 comments on commit f3029dd

Please sign in to comment.