Skip to content

Commit ced77f3

Browse files
committed
Create x1 bit interleaving functions with MVE acceleration
1 parent 9416118 commit ced77f3

File tree

7 files changed

+511
-119
lines changed

7 files changed

+511
-119
lines changed

mlkem/src/fips202/keccakf1600.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ void mlk_keccakf1600_extract_bytes(uint64_t *state, unsigned char *data,
3838
unsigned offset, unsigned length)
3939
{
4040
unsigned i;
41+
#if defined(MLK_USE_FIPS202_X1_EXTRACT_BYTES_NATIVE)
42+
if(mlk_keccakf1600_extract_bytes_x1_native(state, data, offset, length) == MLK_NATIVE_FUNC_SUCCESS)
43+
{
44+
return;
45+
}
46+
#endif
4147
#if defined(MLK_SYS_LITTLE_ENDIAN)
4248
uint8_t *state_ptr = (uint8_t *)state + offset;
4349
for (i = 0; i < length; i++)
@@ -46,6 +52,7 @@ void mlk_keccakf1600_extract_bytes(uint64_t *state, unsigned char *data,
4652
data[i] = state_ptr[i];
4753
}
4854
#else /* MLK_SYS_LITTLE_ENDIAN */
55+
unsigned i;
4956
/* Portable version */
5057
for (i = 0; i < length; i++)
5158
__loop__(invariant(i <= length))
@@ -59,6 +66,11 @@ void mlk_keccakf1600_xor_bytes(uint64_t *state, const unsigned char *data,
5966
unsigned offset, unsigned length)
6067
{
6168
unsigned i;
69+
#if defined(MLK_USE_FIPS202_X1_XOR_BYTES_NATIVE)
70+
if (mlk_keccakf1600_xor_bytes_x1_native(state, data, offset, length) == MLK_NATIVE_FUNC_SUCCESS) {
71+
return;
72+
}
73+
#endif
6274
#if defined(MLK_SYS_LITTLE_ENDIAN)
6375
uint8_t *state_ptr = (uint8_t *)state + offset;
6476
for (i = 0; i < length; i++)

mlkem/src/fips202/native/armv81m/mve.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
/* Part of backend API */
1212
#define MLK_USE_FIPS202_X1_NATIVE
1313
#define MLK_USE_FIPS202_X4_NATIVE
14+
#define MLK_USE_FIPS202_X1_XOR_BYTES_NATIVE
15+
#define MLK_USE_FIPS202_X1_EXTRACT_BYTES_NATIVE
16+
1417
/* Guard for assembly files */
1518
#define MLK_FIPS202_ARMV81M_NEED_X1
1619
#define MLK_FIPS202_ARMV81M_NEED_X4
@@ -39,6 +42,41 @@ static MLK_INLINE int mlk_keccak_f1600_x4_native(uint64_t *state)
3942
return mlk_keccak_f1600_x4_native_impl(state);
4043
}
4144

45+
/*
46+
* Native x1 XOR bytes (with on-the-fly bit interleaving)
47+
*/
48+
#define mlk_keccak_f1600_x1_state_xor_bytes_impl \
49+
MLK_NAMESPACE(mlk_keccak_f1600_x1_state_xor_bytes_impl)
50+
void mlk_keccak_f1600_x1_state_xor_bytes_impl(uint64_t *state, const uint8_t *data,
51+
unsigned offset,
52+
unsigned length);
53+
54+
MLK_MUST_CHECK_RETURN_VALUE
55+
static MLK_INLINE int mlk_keccakf1600_xor_bytes_x1_native(
56+
uint64_t *state, const uint8_t *data, unsigned offset,
57+
unsigned length)
58+
{
59+
mlk_keccak_f1600_x1_state_xor_bytes_impl(state, data, offset, length);
60+
return MLK_NATIVE_FUNC_SUCCESS;
61+
}
62+
63+
/*
64+
* Native x1 extract bytes (with on-the-fly bit de-interleaving)
65+
*/
66+
#define mlk_keccak_f1600_x1_state_extract_bytes_impl \
67+
MLK_NAMESPACE(mlk_keccak_f1600_x1_state_extract_bytes_impl)
68+
void mlk_keccak_f1600_x1_state_extract_bytes_impl(uint64_t *state, uint8_t *data,
69+
unsigned offset,
70+
unsigned length);
71+
72+
MLK_MUST_CHECK_RETURN_VALUE
73+
static MLK_INLINE int mlk_keccakf1600_extract_bytes_x1_native(
74+
uint64_t *state, uint8_t *data, unsigned offset, unsigned length)
75+
{
76+
mlk_keccak_f1600_x1_state_extract_bytes_impl(state, data, offset, length);
77+
return MLK_NATIVE_FUNC_SUCCESS;
78+
}
79+
4280
#endif /* !__ASSEMBLER__ */
4381

4482
#endif /* !MLK_FIPS202_NATIVE_ARMV81M_MVE_H */

mlkem/src/fips202/native/armv81m/src/fips202_native_armv81m.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,15 @@ void mlk_keccak_f1600_x4_mve_asm(uint64_t state[100], uint64_t tmpstate[100],
2020
#define mlk_keccak_f1600_x1_armv7m_asm MLK_NAMESPACE(keccak_f1600_x1_armv7m_asm)
2121
void mlk_keccak_f1600_x1_armv7m_asm(uint32_t state[50], const uint32_t rc[49]);
2222

23+
#define mlk_keccak_f1600_x1_state_xor_bytes_asm MLK_NAMESPACE(keccak_f1600_x1_state_xor_bytes_asm)
24+
void mlk_keccak_f1600_x1_state_xor_bytes_asm(
25+
uint64_t *state, const uint8_t *data, unsigned offset,
26+
unsigned length);
27+
28+
#define mlk_keccak_f1600_x1_state_extract_bytes_asm MLK_NAMESPACE(keccak_f1600_x1_state_extract_bytes_asm)
29+
void mlk_keccak_f1600_x1_state_extract_bytes_asm(
30+
uint64_t *state, const uint8_t *data, unsigned offset,
31+
unsigned length);
32+
33+
2334
#endif /* !MLK_FIPS202_NATIVE_ARMV81M_SRC_FIPS202_NATIVE_ARMV81M_H */

mlkem/src/fips202/native/armv81m/src/keccak_f1600_x1_armv7m.c

Lines changed: 0 additions & 119 deletions
This file was deleted.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) The mlkem-native project authors
3+
* SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
4+
*/
5+
6+
#include "../../../../common.h"
7+
#include "../../../../verify.h"
8+
9+
#if defined(MLK_FIPS202_ARMV81M_NEED_X1) && \
10+
!defined(MLK_CONFIG_MULTILEVEL_NO_SHARED)
11+
12+
#include <stdint.h>
13+
#include "fips202_native_armv81m.h"
14+
15+
void mlk_keccak_f1600_x1_state_extract_bytes_impl(
16+
uint64_t *state, uint8_t *data, unsigned offset,
17+
unsigned length)
18+
{
19+
mlk_keccak_f1600_x1_state_extract_bytes_asm(state, data, offset, length);
20+
}
21+
22+
void mlk_keccak_f1600_x1_state_xor_bytes_impl(
23+
uint64_t *state, const uint8_t *data, unsigned offset,
24+
unsigned length)
25+
{
26+
mlk_keccak_f1600_x1_state_xor_bytes_asm(state, data, offset, length);
27+
}
28+
29+
30+
#define mlk_keccak_f1600_x1_native_impl \
31+
MLK_NAMESPACE(keccak_f1600_x1_native_impl)
32+
int mlk_keccak_f1600_x1_native_impl(uint64_t *state)
33+
{
34+
/* Run the permutation */
35+
mlk_keccak_f1600_x1_armv7m_asm((void*)state, mlk_keccakf1600_round_constants);
36+
return MLK_NATIVE_FUNC_SUCCESS;
37+
}
38+
39+
#else /* MLK_FIPS202_ARMV81M_NEED_X1 && !MLK_CONFIG_MULTILEVEL_NO_SHARED */
40+
41+
MLK_EMPTY_CU(keccak_f1600_x1_armv7m)
42+
43+
#endif /* !(MLK_FIPS202_ARMV81M_NEED_X1 && !MLK_CONFIG_MULTILEVEL_NO_SHARED) \
44+
*/
45+
46+
/* To facilitate single-compilation-unit (SCU) builds, undefine all macros.
47+
* Don't modify by hand -- this is auto-generated by scripts/autogen. */
48+
/* Some macros are kept because they are also defined in a header. */
49+
/* Keep: mlk_keccak_f1600_x1_native_impl (mve.h) */

0 commit comments

Comments
 (0)