Skip to content

Commit 1601cb6

Browse files
committed
FIPS202: Add native x4 XOR/extract bytes interface
Extend the FIPS202 native backend API to support implementing XORBytes and ExtractBytes steps in native code. This is essential for backends using custom state representations (e.g., bit-interleaved state), where these functions handle conversion to/from the internal format on-the-fly. In such cases, they also account for a significant amount of processing time. New flags: - MLD_USE_FIPS202_X4_XOR_BYTES_NATIVE: Backend provides native XOR bytes - MLD_USE_FIPS202_X4_EXTRACT_BYTES_NATIVE: Backend provides native extract bytes When set, backends provide native implementations for: - mld_keccakf1600_xor_bytes_x4_native: XOR input data into state - mld_keccakf1600_extract_bytes_x4_native: Extract output from state Signed-off-by: Matthias J. Kannwischer <matthias@kannwischer.eu>
1 parent a61e9cd commit 1601cb6

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

  • mldsa/src/fips202/native

mldsa/src/fips202/native/api.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,64 @@ __contract__(
6666
);
6767
#endif /* MLD_USE_FIPS202_X4_NATIVE */
6868

69+
/*
70+
* Native x4 XOR bytes and extract bytes interface.
71+
*
72+
* These functions allow backends to provide optimized implementations for
73+
* XORing input data into the state and extracting output data from the state.
74+
* This is particularly useful for backends that use a different internal state
75+
* representation (e.g., bit-interleaved), as conversion can happen during
76+
* XOR/extract rather than before/after each permutation.
77+
*
78+
* NOTE: We assume that the custom representation of the zero state is the
79+
* all-zero state.
80+
*
81+
* MLD_USE_FIPS202_X4_XOR_BYTES_NATIVE: Backend provides native XOR bytes
82+
* MLD_USE_FIPS202_X4_EXTRACT_BYTES_NATIVE: Backend provides native extract
83+
* bytes
84+
*/
85+
86+
#if defined(MLD_USE_FIPS202_X4_XOR_BYTES_NATIVE)
87+
MLD_MUST_CHECK_RETURN_VALUE
88+
static MLD_INLINE int mld_keccakf1600_xor_bytes_x4_native(
89+
uint64_t *state, const unsigned char *data0, const unsigned char *data1,
90+
const unsigned char *data2, const unsigned char *data3, unsigned offset,
91+
unsigned length)
92+
__contract__(
93+
requires(0 <= offset && offset <= 25 * sizeof(uint64_t) &&
94+
0 <= length && length <= 25 * sizeof(uint64_t) - offset)
95+
requires(memory_no_alias(state, sizeof(uint64_t) * 25 * 4))
96+
requires(memory_no_alias(data0, length))
97+
requires((data0 == data1 &&
98+
data0 == data2 &&
99+
data0 == data3) ||
100+
(memory_no_alias(data1, length) &&
101+
memory_no_alias(data2, length) &&
102+
memory_no_alias(data3, length)))
103+
assigns(memory_slice(state, sizeof(uint64_t) * 25 * 4))
104+
ensures(return_value == MLD_NATIVE_FUNC_FALLBACK || return_value == MLD_NATIVE_FUNC_SUCCESS)
105+
ensures((return_value == MLD_NATIVE_FUNC_FALLBACK) ==> array_unchanged_u64(state, 25 * 4)));
106+
#endif /* MLD_USE_FIPS202_X4_XOR_BYTES_NATIVE */
107+
108+
#if defined(MLD_USE_FIPS202_X4_EXTRACT_BYTES_NATIVE)
109+
MLD_MUST_CHECK_RETURN_VALUE
110+
static MLD_INLINE int mld_keccakf1600_extract_bytes_x4_native(
111+
uint64_t *state, unsigned char *data0, unsigned char *data1,
112+
unsigned char *data2, unsigned char *data3, unsigned offset,
113+
unsigned length)
114+
__contract__(
115+
requires(0 <= offset && offset <= 25 * sizeof(uint64_t) &&
116+
0 <= length && length <= 25 * sizeof(uint64_t) - offset)
117+
requires(memory_no_alias(state, sizeof(uint64_t) * 25 * 4))
118+
requires(memory_no_alias(data0, length))
119+
requires(memory_no_alias(data1, length))
120+
requires(memory_no_alias(data2, length))
121+
requires(memory_no_alias(data3, length))
122+
assigns(memory_slice(data0, length))
123+
assigns(memory_slice(data1, length))
124+
assigns(memory_slice(data2, length))
125+
assigns(memory_slice(data3, length))
126+
ensures(return_value == MLD_NATIVE_FUNC_FALLBACK || return_value == MLD_NATIVE_FUNC_SUCCESS));
127+
#endif /* MLD_USE_FIPS202_X4_EXTRACT_BYTES_NATIVE */
128+
69129
#endif /* !MLD_FIPS202_NATIVE_API_H */

0 commit comments

Comments
 (0)