Skip to content

Commit

Permalink
WIP: Introduce global config and reorganize backends
Browse files Browse the repository at this point in the history
Signed-off-by: Hanno Becker <[email protected]>
  • Loading branch information
hanno-becker committed Dec 16, 2024
1 parent 15aed95 commit a462f02
Show file tree
Hide file tree
Showing 83 changed files with 526 additions and 521 deletions.
3 changes: 1 addition & 2 deletions fips202/fips202.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@

#include <stddef.h>
#include <stdint.h>
#include "namespace.h"

#include "cbmc.h"
#include "common.h"

#define SHAKE128_RATE 168
#define SHAKE256_RATE 136
Expand Down
2 changes: 1 addition & 1 deletion fips202/fips202x4.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

#include <stddef.h>
#include <stdint.h>
#include "common.h"
#include "fips202.h"
#include "keccakf1600.h"
#include "namespace.h"

#include "cbmc.h"

Expand Down
2 changes: 1 addition & 1 deletion fips202/keccakf1600.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include <stdint.h>

#include "config.h"
#include "fips202_native.h"
#include "fips202_impl.h"

#include "cbmc.h"

Expand Down
5 changes: 2 additions & 3 deletions fips202/keccakf1600.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
#define KECCAKF1600_H

#include <stdint.h>
#include "fips202_native.h"
#include "namespace.h"

#include "cbmc.h"
#include "common.h"
#include "fips202_native.h"
#define KECCAK_LANES 25

/*
Expand Down
30 changes: 0 additions & 30 deletions fips202/namespace.h

This file was deleted.

6 changes: 1 addition & 5 deletions fips202/native/aarch64/fips202_native_aarch64.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@
#define FIPS202_AARCH64_NATIVE_H

#include <stdint.h>
#include "config.h"
#include "namespace.h"
#include "params.h"
#include "common.h"

#ifdef MLKEM_USE_NATIVE_AARCH64
#define keccak_f1600_x1_scalar_asm_opt \
FIPS202_NAMESPACE(keccak_f1600_x1_scalar_asm_opt)
void keccak_f1600_x1_scalar_asm_opt(uint64_t *state);
Expand Down Expand Up @@ -38,6 +35,5 @@ void keccak_f1600_x4_scalar_v84a_asm_hybrid_opt(uint64_t *state);
#define keccak_f1600_x4_scalar_v8a_v84a_hybrid_asm_opt \
FIPS202_NAMESPACE(keccak_f1600_x4_scalar_v8a_v84a_hybrid_asm_opt)
void keccak_f1600_x4_scalar_v8a_v84a_hybrid_asm_opt(uint64_t *state);
#endif /* MLKEM_USE_NATIVE_AARCH64 */

#endif /* FIPS202_AARCH64_NATIVE_H */
6 changes: 3 additions & 3 deletions fips202/native/aarch64/keccak_f1600_x1_scalar_asm_opt.S
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
// Author: Hanno Becker <[email protected]>
// Author: Matthias Kannwischer <[email protected]>

#include "config.h"
#if defined(MLKEM_USE_NATIVE_AARCH64)
#include "common.h"
#if defined(MLKEM_NATIVE_FIPS202_BACKEND_AARCH64_DEFAULT) || \
defined(MLKEM_NATIVE_FIPS202_BACKEND_AARCH64_A55)

// Needed to provide ASM_LOAD directive
#include "common.i"
#include "namespace.h"

/********************** CONSTANTS *************************/
.data
Expand Down
5 changes: 3 additions & 2 deletions fips202/native/aarch64/keccak_f1600_x1_v84a_asm_clean.S
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@
// during load and store, so that the caller need not do this.
//

#include "config.h"
#if defined(MLKEM_USE_NATIVE_AARCH64)
#include "common.h"
#if defined(MLKEM_NATIVE_FIPS202_BACKEND_AARCH64_DEFAULT) || \
defined(MLKEM_NATIVE_FIPS202_BACKEND_AARCH64_A55)

// Needed to provide ASM_LOAD directive
#include "common.i"
Expand Down
5 changes: 3 additions & 2 deletions fips202/native/aarch64/keccak_f1600_x2_v84a_asm_clean.S
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@
// during load and store, so that the caller need not do this.
//

#include "config.h"
#if defined(MLKEM_USE_NATIVE_AARCH64)
#include "common.h"
#if defined(MLKEM_NATIVE_FIPS202_BACKEND_AARCH64_DEFAULT) || \
defined(MLKEM_NATIVE_FIPS202_BACKEND_AARCH64_A55)

// Needed to provide ASM_LOAD directive
#include "common.i"
Expand Down
6 changes: 3 additions & 3 deletions fips202/native/aarch64/keccak_f1600_x2_v8a_v84a_asm_hybrid.S
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@
// during load and store, so that the caller need not do this.
//

#include "config.h"
#if defined(MLKEM_USE_NATIVE_AARCH64)
#include "common.h"
#if defined(MLKEM_NATIVE_FIPS202_BACKEND_AARCH64_DEFAULT) || \
defined(MLKEM_NATIVE_FIPS202_BACKEND_AARCH64_A55)

// Needed to provide ASM_LOAD directive
#include "common.i"
#include "namespace.h"

#if defined(__ARM_FEATURE_SHA3)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
*
*/

#include "config.h"
#if defined(MLKEM_USE_NATIVE_AARCH64)
#include "common.h"
#if defined(MLKEM_NATIVE_FIPS202_BACKEND_AARCH64_DEFAULT) || \
defined(MLKEM_NATIVE_FIPS202_BACKEND_AARCH64_A55)

// Needed to provide ASM_LOAD directive
#include "common.i"
#include "namespace.h"

#if defined(__ARM_FEATURE_SHA3)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
*
*/

#include "config.h"
#if defined(MLKEM_USE_NATIVE_AARCH64)
#include "common.h"
#if defined(MLKEM_NATIVE_FIPS202_BACKEND_AARCH64_DEFAULT) || \
defined(MLKEM_NATIVE_FIPS202_BACKEND_AARCH64_A55)

// Needed to provide ASM_LOAD directive
#include "common.i"
#include "namespace.h"

#define KECCAK_F1600_ROUNDS 24

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
*
*/

#include "config.h"
#if defined(MLKEM_USE_NATIVE_AARCH64)
#include "common.h"
#if defined(MLKEM_NATIVE_FIPS202_BACKEND_AARCH64_DEFAULT) || \
defined(MLKEM_NATIVE_FIPS202_BACKEND_AARCH64_A55)

// Needed to provide ASM_LOAD directive
#include "common.i"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@

/* FIPS202 assembly profile targeting Cortex-A55 */

#ifdef FIPS202_NATIVE_PROFILE_H
#ifdef FIPS202_NATIVE_PROFILE_IMPL_H
#error Only one FIPS202 assembly profile can be defined -- did you include multiple profiles?
#else
#define FIPS202_NATIVE_PROFILE_H

#include "../fips202_native_aarch64.h"
#define FIPS202_NATIVE_PROFILE_IMPL_H

/*
* On Cortex-A55, we use lazy rotation assembly for Keccak-x1,
Expand All @@ -22,4 +20,4 @@ static INLINE void keccak_f1600_x1_native(uint64_t *state)
keccak_f1600_x1_scalar_asm_opt(state);
}

#endif /* FIPS202_NATIVE_PROFILE_H */
#endif /* FIPS202_NATIVE_PROFILE_IMPL_H */
24 changes: 24 additions & 0 deletions fips202/native/aarch64/profiles/cortex_a55_metadata.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) 2024 The mlkem-native project authors
* SPDX-License-Identifier: Apache-2.0
*/

/* FIPS202 assembly profile targeting Cortex-A55 */

#ifdef FIPS202_NATIVE_PROFILE_H
#error Only one FIPS202 assembly profile can be defined -- did you include multiple profiles?
#else
#define FIPS202_NATIVE_PROFILE_H

/* Identifier for this backend so that source and assembly files
* in the build can be appropriately guarded. */
#define MLKEM_NATIVE_FIPS202_BACKEND_AARCH64_A55

#define MLKEM_NATIVE_FIPS202_BACKEND_NAME AARCH64_A55

/* Filename of the C backend implementation.
* This is not inlined here because this header is included in assembly
* files as well. */
#define MLKEM_NATIVE_FIPS202_BACKEND_IMPL "aarch64/profiles/cortex_a55_impl.h"

#endif /* FIPS202_NATIVE_PROFILE_H */
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

/* Default FIPS202 assembly profile for AArch64 systems */

#ifdef FIPS202_NATIVE_PROFILE_H
#ifdef FIPS202_NATIVE_PROFILE_IMPL_H
#error Only one FIPS202 assembly profile can be defined -- did you include multiple profiles?
#else
#define FIPS202_NATIVE_PROFILE_H
#define FIPS202_NATIVE_PROFILE_IMPL_H

#include "../fips202_native_aarch64.h"

Expand Down
24 changes: 24 additions & 0 deletions fips202/native/aarch64/profiles/default_metadata.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) 2024 The mlkem-native project authors
* SPDX-License-Identifier: Apache-2.0
*/

/* Default FIPS202 assembly profile for AArch64 systems */

#ifdef FIPS202_NATIVE_PROFILE_H
#error Only one FIPS202 assembly profile can be defined -- did you include multiple profiles?
#else
#define FIPS202_NATIVE_PROFILE_H

/* Identifier for this backend so that source and assembly files
* in the build can be appropriately guarded. */
#define MLKEM_NATIVE_FIPS202_BACKEND_AARCH64_DEFAULT

#define MLKEM_NATIVE_FIPS202_BACKEND_NAME AARCH64_DEFAULT

/* Filename of the C backend implementation.
* This is not inlined here because this header is included in assembly
* files as well. */
#define MLKEM_NATIVE_FIPS202_BACKEND_IMPL "aarch64/profiles/default_impl.h"

#endif /* FIPS202_NATIVE_PROFILE_H */
22 changes: 22 additions & 0 deletions fips202/native/default.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (c) 2024 The mlkem-native project authors
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef MLKEM_NATIVE_FIPS202_BACKEND_DEFAULT_H
#define MLKEM_NATIVE_FIPS202_BACKEND_DEFAULT_H

/*
* Default FIPS202 backend
*/
#include "common.h"

#if defined(SYS_AARCH64)
#include "aarch64/profiles/default_metadata.h"
#endif

#if defined(SYS_X86_64) && defined(SYS_X86_64_AVX2)
#include "x86_64/profiles/xkcp_metadata.h"
#endif

#endif /* MLKEM_NATIVE_FIPS202_BACKEND_DEFAULT_H */
15 changes: 15 additions & 0 deletions fips202/native/fips202_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright (c) 2024 The mlkem-native project authors
* SPDX-License-Identifier: Apache-2.0
*/

#ifdef MLKEM_NATIVE_FIPS202_IMPL_H
#error Only one FIPS202 assembly profile can be defined -- did you include multiple profiles?
#else
#define MLKEM_NATIVE_FIPS202_IMPL_H

#if defined(MLKEM_NATIVE_FIPS202_BACKEND_IMPL)
#include MLKEM_NATIVE_FIPS202_BACKEND_IMPL
#endif

#endif /* MLKEM_NATIVE_FIPS202_IMPL_H */
50 changes: 1 addition & 49 deletions fips202/native/fips202_native.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,54 +6,7 @@
#define FIPS202_NATIVE_H

#include <stdint.h>
#include "config.h"
#include "params.h"

#if defined(MLKEM_USE_NATIVE)

/*
* FIPS202 native profile
*
* The profile decides which implementation(s) of FIPS202 to use.
*
* If you don't change anything, the default profile will be used. This profile
* picks implementations based on characteristics of your system visible to
* the compiler.
*
* The default logic is not perfect, and you may want to pick a specific
* profile for your target. There are three ways to do so, in descending
* order of convenience to the user:
* 1. Pick one of the profiles shipped with this repository.
* 2. Provide your own profile and register it via FIPS202_ASM_PROFILE
* (which must be the profile's path relative to this directoru).
* 3. Set FIPS202_NATIVE_MANUAL and use an adhoc profile specified via CFLAGS.
*/

/* Option 2: Manually written profile */
#if defined(FIPS202_NATIVE_PROFILE)

#define STRINGIFY_(x) #x
#define STRINGIFY(x) STRINGIFY_(x)
#include STRINGIFY(FIPS202_NATIVE_PROFILE)

/* Option 1: Choose from shipped list of profiles */
#elif !defined(FIPS202_NATIVE_MANUAL)

#ifdef SYS_AARCH64
/* Pick exactly one profile from the following list */
#include "aarch64/profiles/default.h"
/* #include "aarch64/profiles/cortex_a55.h" */
#endif

#if defined(SYS_X86_64) && defined(SYS_X86_64_AVX2)
#include "x86_64/profiles/xkcp.h"
#endif

#else /* !FIPS202_NATIVE_PROFILE && FIPS202_NATIVE_MANUAL */

/* Option 3: Build your own profile here, or via CFLAGS */

#endif /* !FIPS202_NATIVE_PROFILE && !FIPS202_NATIVE_MANUAL */
#include "common.h"

/*
* FIPS202 native interface
Expand All @@ -77,5 +30,4 @@ static INLINE void keccak_f1600_x2_native(uint64_t *state);
static INLINE void keccak_f1600_x4_native(uint64_t *state);
#endif

#endif /* MLKEM_USE_NATIVE */
#endif /* FIPS202_NATIVE_H */
Loading

0 comments on commit a462f02

Please sign in to comment.