diff --git a/cbmc/Makefile_params.common b/cbmc/Makefile_params.common index 438cbb5a0..d3df04182 100644 --- a/cbmc/Makefile_params.common +++ b/cbmc/Makefile_params.common @@ -7,13 +7,13 @@ endif MLKEM_K ?= 3 ifeq ($(MLKEM_K),2) - MLKEM_NAMESPACE=PQCP_MLKEM_NATIVE_MLKEM512_C_ + MLKEM_NAMESPACE=PQCP_MLKEM_NATIVE_MLKEM512_ else ifeq ($(MLKEM_K),3) - MLKEM_NAMESPACE=PQCP_MLKEM_NATIVE_MLKEM768_C_ + MLKEM_NAMESPACE=PQCP_MLKEM_NATIVE_MLKEM768_ else ifeq ($(MLKEM_K),4) - MLKEM_NAMESPACE=PQCP_MLKEM_NATIVE_MLKEM1024_C_ + MLKEM_NAMESPACE=PQCP_MLKEM_NATIVE_MLKEM1024_ else $(error Invalid value of MLKEM_K) endif -FIPS202_NAMESPACE=PQCP_MLKEM_NATIVE_FIPS202_C_ +FIPS202_NAMESPACE=PQCP_MLKEM_NATIVE_FIPS202_ diff --git a/examples/monolithic_build/mlkem_native_all.c b/examples/monolithic_build/mlkem_native_all.c index 17b80ee27..c90dfab76 100644 --- a/examples/monolithic_build/mlkem_native_all.c +++ b/examples/monolithic_build/mlkem_native_all.c @@ -224,32 +224,42 @@ #undef MLKEM_NATIVE_COMMON_H #endif +/* mlkem/common.h:28 */ +#if defined(MLKEM_NATIVE_ARITH_BACKEND_NAME) +#undef MLKEM_NATIVE_ARITH_BACKEND_NAME +#endif + /* mlkem/common.h:32 */ +#if defined(MLKEM_NATIVE_FIPS202_BACKEND_NAME) +#undef MLKEM_NATIVE_FIPS202_BACKEND_NAME +#endif + +/* mlkem/common.h:39 */ #if defined(MLKEM_ASM_NAMESPACE) #undef MLKEM_ASM_NAMESPACE #endif -/* mlkem/common.h:33 */ +/* mlkem/common.h:40 */ #if defined(FIPS202_ASM_NAMESPACE) #undef FIPS202_ASM_NAMESPACE #endif -/* mlkem/common.h:35 */ +/* mlkem/common.h:42 */ #if defined(_PREFIX_UNDERSCORE) #undef _PREFIX_UNDERSCORE #endif -/* mlkem/common.h:36 */ +/* mlkem/common.h:43 */ #if defined(PREFIX_UNDERSCORE) #undef PREFIX_UNDERSCORE #endif -/* mlkem/common.h:37 */ +/* mlkem/common.h:44 */ #if defined(MLKEM_ASM_NAMESPACE) #undef MLKEM_ASM_NAMESPACE #endif -/* mlkem/common.h:38 */ +/* mlkem/common.h:45 */ #if defined(FIPS202_ASM_NAMESPACE) #undef FIPS202_ASM_NAMESPACE #endif @@ -599,17 +609,17 @@ #undef shake256x4 #endif -/* mlkem/fips202/keccakf1600.c:21 */ +/* mlkem/fips202/keccakf1600.c:19 */ #if defined(NROUNDS) #undef NROUNDS #endif -/* mlkem/fips202/keccakf1600.c:22 */ +/* mlkem/fips202/keccakf1600.c:20 */ #if defined(ROL) #undef ROL #endif -/* mlkem/fips202/keccakf1600.c:28 */ +/* mlkem/fips202/keccakf1600.c:26 */ #if defined(KeccakF_RoundConstants) #undef KeccakF_RoundConstants #endif @@ -1274,57 +1284,47 @@ #undef MLKEM_NATIVE_NAMESPACE_H #endif -/* mlkem/namespace.h:8 */ -#if defined(MLKEM_NATIVE_ARITH_BACKEND_NAME) -#undef MLKEM_NATIVE_ARITH_BACKEND_NAME -#endif - -/* mlkem/namespace.h:13 */ +/* mlkem/namespace.h:9 */ #if defined(MLKEM_PARAM_NAME) #undef MLKEM_PARAM_NAME #endif -/* mlkem/namespace.h:15 */ +/* mlkem/namespace.h:11 */ #if defined(MLKEM_PARAM_NAME) #undef MLKEM_PARAM_NAME #endif -/* mlkem/namespace.h:17 */ +/* mlkem/namespace.h:13 */ #if defined(MLKEM_PARAM_NAME) #undef MLKEM_PARAM_NAME #endif -/* mlkem/namespace.h:22 */ +/* mlkem/namespace.h:18 */ #if defined(___MLKEM_DEFAULT_NAMESPACE) #undef ___MLKEM_DEFAULT_NAMESPACE #endif -/* mlkem/namespace.h:23 */ +/* mlkem/namespace.h:19 */ #if defined(__MLKEM_DEFAULT_NAMESPACE) #undef __MLKEM_DEFAULT_NAMESPACE #endif -/* mlkem/namespace.h:30 */ +/* mlkem/namespace.h:26 */ #if defined(MLKEM_DEFAULT_NAMESPACE) #undef MLKEM_DEFAULT_NAMESPACE #endif -/* mlkem/namespace.h:35 */ -#if defined(MLKEM_NATIVE_FIPS202_BACKEND_NAME) -#undef MLKEM_NATIVE_FIPS202_BACKEND_NAME -#endif - -/* mlkem/namespace.h:38 */ +/* mlkem/namespace.h:29 */ #if defined(___FIPS202_DEFAULT_NAMESPACE) #undef ___FIPS202_DEFAULT_NAMESPACE #endif -/* mlkem/namespace.h:39 */ +/* mlkem/namespace.h:30 */ #if defined(__FIPS202_DEFAULT_NAMESPACE) #undef __FIPS202_DEFAULT_NAMESPACE #endif -/* mlkem/namespace.h:46 */ +/* mlkem/namespace.h:36 */ #if defined(FIPS202_DEFAULT_NAMESPACE) #undef FIPS202_DEFAULT_NAMESPACE #endif @@ -2059,142 +2059,142 @@ #undef PARAMS_H #endif -/* mlkem/params.h:13 */ +/* mlkem/params.h:17 */ #if defined(MLKEM_N) #undef MLKEM_N #endif -/* mlkem/params.h:14 */ +/* mlkem/params.h:18 */ #if defined(MLKEM_Q) #undef MLKEM_Q #endif -/* mlkem/params.h:15 */ +/* mlkem/params.h:19 */ #if defined(UINT12_MAX) #undef UINT12_MAX #endif -/* mlkem/params.h:17 */ +/* mlkem/params.h:21 */ #if defined(MLKEM_SYMBYTES) #undef MLKEM_SYMBYTES #endif -/* mlkem/params.h:18 */ +/* mlkem/params.h:22 */ #if defined(MLKEM_SSBYTES) #undef MLKEM_SSBYTES #endif -/* mlkem/params.h:20 */ +/* mlkem/params.h:24 */ #if defined(MLKEM_POLYBYTES) #undef MLKEM_POLYBYTES #endif -/* mlkem/params.h:21 */ +/* mlkem/params.h:25 */ #if defined(MLKEM_POLYVECBYTES) #undef MLKEM_POLYVECBYTES #endif -/* mlkem/params.h:24 */ +/* mlkem/params.h:28 */ #if defined(MLKEM_ETA1) #undef MLKEM_ETA1 #endif -/* mlkem/params.h:25 */ +/* mlkem/params.h:29 */ #if defined(MLKEM_POLYCOMPRESSEDBYTES_DV) #undef MLKEM_POLYCOMPRESSEDBYTES_DV #endif -/* mlkem/params.h:26 */ +/* mlkem/params.h:30 */ #if defined(MLKEM_POLYCOMPRESSEDBYTES_DU) #undef MLKEM_POLYCOMPRESSEDBYTES_DU #endif -/* mlkem/params.h:27 */ +/* mlkem/params.h:31 */ #if defined(MLKEM_POLYVECCOMPRESSEDBYTES_DU) #undef MLKEM_POLYVECCOMPRESSEDBYTES_DU #endif -/* mlkem/params.h:29 */ +/* mlkem/params.h:33 */ #if defined(MLKEM_ETA1) #undef MLKEM_ETA1 #endif -/* mlkem/params.h:30 */ +/* mlkem/params.h:34 */ #if defined(MLKEM_POLYCOMPRESSEDBYTES_DV) #undef MLKEM_POLYCOMPRESSEDBYTES_DV #endif -/* mlkem/params.h:31 */ +/* mlkem/params.h:35 */ #if defined(MLKEM_POLYCOMPRESSEDBYTES_DU) #undef MLKEM_POLYCOMPRESSEDBYTES_DU #endif -/* mlkem/params.h:32 */ +/* mlkem/params.h:36 */ #if defined(MLKEM_POLYVECCOMPRESSEDBYTES_DU) #undef MLKEM_POLYVECCOMPRESSEDBYTES_DU #endif -/* mlkem/params.h:34 */ +/* mlkem/params.h:38 */ #if defined(MLKEM_ETA1) #undef MLKEM_ETA1 #endif -/* mlkem/params.h:35 */ +/* mlkem/params.h:39 */ #if defined(MLKEM_POLYCOMPRESSEDBYTES_DV) #undef MLKEM_POLYCOMPRESSEDBYTES_DV #endif -/* mlkem/params.h:36 */ +/* mlkem/params.h:40 */ #if defined(MLKEM_POLYCOMPRESSEDBYTES_DU) #undef MLKEM_POLYCOMPRESSEDBYTES_DU #endif -/* mlkem/params.h:37 */ +/* mlkem/params.h:41 */ #if defined(MLKEM_POLYVECCOMPRESSEDBYTES_DU) #undef MLKEM_POLYVECCOMPRESSEDBYTES_DU #endif -/* mlkem/params.h:40 */ +/* mlkem/params.h:44 */ #if defined(MLKEM_ETA2) #undef MLKEM_ETA2 #endif -/* mlkem/params.h:42 */ +/* mlkem/params.h:46 */ #if defined(MLKEM_INDCPA_MSGBYTES) #undef MLKEM_INDCPA_MSGBYTES #endif -/* mlkem/params.h:43 */ +/* mlkem/params.h:47 */ #if defined(MLKEM_INDCPA_PUBLICKEYBYTES) #undef MLKEM_INDCPA_PUBLICKEYBYTES #endif -/* mlkem/params.h:44 */ +/* mlkem/params.h:48 */ #if defined(MLKEM_INDCPA_SECRETKEYBYTES) #undef MLKEM_INDCPA_SECRETKEYBYTES #endif -/* mlkem/params.h:45 */ +/* mlkem/params.h:49 */ #if defined(MLKEM_INDCPA_BYTES) #undef MLKEM_INDCPA_BYTES #endif -/* mlkem/params.h:48 */ +/* mlkem/params.h:52 */ #if defined(MLKEM_PUBLICKEYBYTES) #undef MLKEM_PUBLICKEYBYTES #endif -/* mlkem/params.h:50 */ +/* mlkem/params.h:54 */ #if defined(MLKEM_SECRETKEYBYTES) #undef MLKEM_SECRETKEYBYTES #endif -/* mlkem/params.h:53 */ +/* mlkem/params.h:57 */ #if defined(MLKEM_CIPHERTEXTBYTES) #undef MLKEM_CIPHERTEXTBYTES #endif -/* mlkem/params.h:55 */ +/* mlkem/params.h:59 */ #if defined(KECCAK_WAY) #undef KECCAK_WAY #endif diff --git a/mlkem/common.h b/mlkem/common.h index 8177b0b50..c71468a69 100644 --- a/mlkem/common.h +++ b/mlkem/common.h @@ -7,8 +7,11 @@ #if defined(MLKEM_NATIVE_CONFIG_FILE) #include MLKEM_NATIVE_CONFIG_FILE +#else +#include "config.h" #endif /* MLKEM_NATIVE_CONFIG_FILE */ +#include "namespace.h" #include "params.h" #include "sys.h" @@ -22,9 +25,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 diff --git a/mlkem/fips202/keccakf1600.c b/mlkem/fips202/keccakf1600.c index 7c7a2d71d..7c04efcea 100644 --- a/mlkem/fips202/keccakf1600.c +++ b/mlkem/fips202/keccakf1600.c @@ -10,14 +10,12 @@ * from https://twitter.com/tweetfips202 * by Gilles Van Assche, Daniel J. Bernstein, and Peter Schwabe */ -#include "keccakf1600.h" #include #include -#include "config.h" -#include "fips202_backend.h" - #include "cbmc.h" +#include "fips202_backend.h" +#include "keccakf1600.h" #define NROUNDS 24 #define ROL(a, offset) ((a << offset) ^ (a >> (64 - offset))) diff --git a/mlkem/kem.h b/mlkem/kem.h index 2ba4af066..6d84a75d0 100644 --- a/mlkem/kem.h +++ b/mlkem/kem.h @@ -7,7 +7,7 @@ #include #include "cbmc.h" -#include "params.h" +#include "common.h" #define CRYPTO_SECRETKEYBYTES MLKEM_SECRETKEYBYTES #define CRYPTO_PUBLICKEYBYTES MLKEM_PUBLICKEYBYTES diff --git a/mlkem/namespace.h b/mlkem/namespace.h index 8d56503d9..f1490b8e7 100644 --- a/mlkem/namespace.h +++ b/mlkem/namespace.h @@ -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 @@ -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___ - * 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__ - * 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 */ diff --git a/mlkem/params.h b/mlkem/params.h index 586c31d33..656758475 100644 --- a/mlkem/params.h +++ b/mlkem/params.h @@ -5,7 +5,11 @@ #ifndef PARAMS_H #define PARAMS_H +#if defined(MLKEM_NATIVE_CONFIG_FILE) +#include MLKEM_NATIVE_CONFIG_FILE +#else #include "config.h" +#endif /* MLKEM_NATIVE_CONFIG_FILE */ #if !defined(MLKEM_K) #error MLKEM_K is not defined diff --git a/mlkem/polyvec.c b/mlkem/polyvec.c index 72277a626..d1150ca85 100644 --- a/mlkem/polyvec.c +++ b/mlkem/polyvec.c @@ -5,7 +5,6 @@ #include "polyvec.h" #include #include "arith_backend.h" -#include "config.h" #include "ntt.h" #include "poly.h" diff --git a/mlkem/verify.h b/mlkem/verify.h index 126eeb279..4398eafe7 100644 --- a/mlkem/verify.h +++ b/mlkem/verify.h @@ -9,7 +9,7 @@ #include #include #include "cbmc.h" -#include "params.h" +#include "common.h" /* Static namespacing * This is to facilitate building multiple instances diff --git a/scripts/ci/check-namespace b/scripts/ci/check-namespace index 2a69a2f5e..2b498bbcf 100755 --- a/scripts/ci/check-namespace +++ b/scripts/ci/check-namespace @@ -67,17 +67,13 @@ def check_folder(folder, namespace): def list_mlkem_namespaces(lvl): return [ - f"PQCP_MLKEM_NATIVE_MLKEM{lvl}_C", - f"PQCP_MLKEM_NATIVE_MLKEM{lvl}_AARCH64", - f"PQCP_MLKEM_NATIVE_MLKEM{lvl}_X86_64", + f"PQCP_MLKEM_NATIVE_MLKEM{lvl}", ] def list_fips202_namespaces(): return [ - f"PQCP_MLKEM_NATIVE_FIPS202_C", - f"PQCP_MLKEM_NATIVE_FIPS202_AARCH64", - f"PQCP_MLKEM_NATIVE_FIPS202_X86_64", + f"PQCP_MLKEM_NATIVE_FIPS202", ]