|
| 1 | +/* |
| 2 | + * Copyright (c) The mlkem-native project authors |
| 3 | + * Copyright (c) The mldsa-native project authors |
| 4 | + * SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT |
| 5 | + */ |
| 6 | + |
| 7 | +#ifndef MLD_NATIVE_AARCH64_META_H |
| 8 | +#define MLD_NATIVE_AARCH64_META_H |
| 9 | + |
| 10 | +/* Set of primitives that this backend replaces */ |
| 11 | +#define MLD_USE_NATIVE_NTT |
| 12 | +#define MLD_USE_NATIVE_INTT |
| 13 | +#define MLD_USE_NATIVE_REJ_UNIFORM |
| 14 | +#define MLD_USE_NATIVE_REJ_UNIFORM_ETA2 |
| 15 | +#define MLD_USE_NATIVE_REJ_UNIFORM_ETA4 |
| 16 | +#define MLD_USE_NATIVE_POLY_DECOMPOSE_32 |
| 17 | +#define MLD_USE_NATIVE_POLY_DECOMPOSE_88 |
| 18 | +#define MLD_USE_NATIVE_POLY_CADDQ |
| 19 | +#define MLD_USE_NATIVE_POLY_USE_HINT_32 |
| 20 | +#define MLD_USE_NATIVE_POLY_USE_HINT_88 |
| 21 | +#define MLD_USE_NATIVE_POLY_CHKNORM |
| 22 | +#define MLD_USE_NATIVE_POLYZ_UNPACK_17 |
| 23 | +#define MLD_USE_NATIVE_POLYZ_UNPACK_19 |
| 24 | +#define MLD_USE_NATIVE_POINTWISE_MONTGOMERY |
| 25 | +#define MLD_USE_NATIVE_POLYVECL_POINTWISE_ACC_MONTGOMERY_L4 |
| 26 | +#define MLD_USE_NATIVE_POLYVECL_POINTWISE_ACC_MONTGOMERY_L5 |
| 27 | +#define MLD_USE_NATIVE_POLYVECL_POINTWISE_ACC_MONTGOMERY_L7 |
| 28 | + |
| 29 | +/* Identifier for this backend so that source and assembly files |
| 30 | + * in the build can be appropriately guarded. */ |
| 31 | +#define MLD_ARITH_BACKEND_AARCH64 |
| 32 | + |
| 33 | + |
| 34 | +#if !defined(__ASSEMBLER__) |
| 35 | +#include "src/arith_native_aarch64.h" |
| 36 | + |
| 37 | +static MLD_INLINE void mld_ntt_native(int32_t data[MLDSA_N]) |
| 38 | +{ |
| 39 | + mld_ntt_asm(data, mld_aarch64_ntt_zetas_layer123456, |
| 40 | + mld_aarch64_ntt_zetas_layer78); |
| 41 | +} |
| 42 | + |
| 43 | +static MLD_INLINE void mld_intt_native(int32_t data[MLDSA_N]) |
| 44 | +{ |
| 45 | + mld_intt_asm(data, mld_aarch64_intt_zetas_layer78, |
| 46 | + mld_aarch64_intt_zetas_layer123456); |
| 47 | +} |
| 48 | + |
| 49 | +static MLD_INLINE int mld_rej_uniform_native(int32_t *r, unsigned len, |
| 50 | + const uint8_t *buf, |
| 51 | + unsigned buflen) |
| 52 | +{ |
| 53 | + if (len != MLDSA_N || buflen % 24 != 0) |
| 54 | + { |
| 55 | + return -1; |
| 56 | + } |
| 57 | + |
| 58 | + /* Safety: outlen is at most MLDSA_N, hence, this cast is safe. */ |
| 59 | + return (int)mld_rej_uniform_asm(r, buf, buflen, mld_rej_uniform_table); |
| 60 | +} |
| 61 | + |
| 62 | +static MLD_INLINE int mld_rej_uniform_eta2_native(int32_t *r, unsigned len, |
| 63 | + const uint8_t *buf, |
| 64 | + unsigned buflen) |
| 65 | +{ |
| 66 | + unsigned int outlen; |
| 67 | + /* AArch64 implementation assumes specific buffer lengths */ |
| 68 | + if (len != MLDSA_N || buflen != MLD_AARCH64_REJ_UNIFORM_ETA2_BUFLEN) |
| 69 | + { |
| 70 | + return -1; |
| 71 | + } |
| 72 | + /* Constant time: Inputs and outputs to this function are secret. |
| 73 | + * It is safe to leak which coefficients are accepted/rejected. |
| 74 | + * The assembly implementation must not leak any other information about the |
| 75 | + * accepted coefficients. Constant-time testing cannot cover this, and we |
| 76 | + * hence have to manually verify the assembly. |
| 77 | + * We declassify prior the input data and mark the outputs as secret. |
| 78 | + */ |
| 79 | + MLD_CT_TESTING_DECLASSIFY(buf, buflen); |
| 80 | + outlen = mld_rej_uniform_eta2_asm(r, buf, buflen, mld_rej_uniform_eta_table); |
| 81 | + MLD_CT_TESTING_SECRET(r, sizeof(int32_t) * outlen); |
| 82 | + /* Safety: outlen is at most MLDSA_N and, hence, this cast is safe. */ |
| 83 | + return (int)outlen; |
| 84 | +} |
| 85 | + |
| 86 | +static MLD_INLINE int mld_rej_uniform_eta4_native(int32_t *r, unsigned len, |
| 87 | + const uint8_t *buf, |
| 88 | + unsigned buflen) |
| 89 | +{ |
| 90 | + unsigned int outlen; |
| 91 | + /* AArch64 implementation assumes specific buffer lengths */ |
| 92 | + if (len != MLDSA_N || buflen != MLD_AARCH64_REJ_UNIFORM_ETA4_BUFLEN) |
| 93 | + { |
| 94 | + return -1; |
| 95 | + } |
| 96 | + /* Constant time: Inputs and outputs to this function are secret. |
| 97 | + * It is safe to leak which coefficients are accepted/rejected. |
| 98 | + * The assembly implementation must not leak any other information about the |
| 99 | + * accepted coefficients. Constant-time testing cannot cover this, and we |
| 100 | + * hence have to manually verify the assembly. |
| 101 | + * We declassify prior the input data and mark the outputs as secret. |
| 102 | + */ |
| 103 | + MLD_CT_TESTING_DECLASSIFY(buf, buflen); |
| 104 | + outlen = mld_rej_uniform_eta4_asm(r, buf, buflen, mld_rej_uniform_eta_table); |
| 105 | + MLD_CT_TESTING_SECRET(r, sizeof(int32_t) * outlen); |
| 106 | + /* Safety: outlen is at most MLDSA_N and, hence, this cast is safe. */ |
| 107 | + return (int)outlen; |
| 108 | +} |
| 109 | + |
| 110 | +static MLD_INLINE void mld_poly_decompose_32_native(int32_t *a1, int32_t *a0, |
| 111 | + const int32_t *a) |
| 112 | +{ |
| 113 | + mld_poly_decompose_32_asm(a1, a0, a); |
| 114 | +} |
| 115 | + |
| 116 | +static MLD_INLINE void mld_poly_decompose_88_native(int32_t *a1, int32_t *a0, |
| 117 | + const int32_t *a) |
| 118 | +{ |
| 119 | + mld_poly_decompose_88_asm(a1, a0, a); |
| 120 | +} |
| 121 | + |
| 122 | +static MLD_INLINE void mld_poly_caddq_native(int32_t a[MLDSA_N]) |
| 123 | +{ |
| 124 | + mld_poly_caddq_asm(a); |
| 125 | +} |
| 126 | + |
| 127 | +static MLD_INLINE void mld_poly_use_hint_32_native(int32_t *b, const int32_t *a, |
| 128 | + const int32_t *h) |
| 129 | +{ |
| 130 | + mld_poly_use_hint_32_asm(b, a, h); |
| 131 | +} |
| 132 | + |
| 133 | +static MLD_INLINE void mld_poly_use_hint_88_native(int32_t *b, const int32_t *a, |
| 134 | + const int32_t *h) |
| 135 | +{ |
| 136 | + mld_poly_use_hint_88_asm(b, a, h); |
| 137 | +} |
| 138 | + |
| 139 | +static MLD_INLINE uint32_t mld_poly_chknorm_native(const int32_t *a, int32_t B) |
| 140 | +{ |
| 141 | + return mld_poly_chknorm_asm(a, B); |
| 142 | +} |
| 143 | + |
| 144 | +static MLD_INLINE void mld_polyz_unpack_17_native(int32_t *r, |
| 145 | + const uint8_t *buf) |
| 146 | +{ |
| 147 | + mld_polyz_unpack_17_asm(r, buf, mld_polyz_unpack_17_indices); |
| 148 | +} |
| 149 | + |
| 150 | +static MLD_INLINE void mld_polyz_unpack_19_native(int32_t *r, |
| 151 | + const uint8_t *buf) |
| 152 | +{ |
| 153 | + mld_polyz_unpack_19_asm(r, buf, mld_polyz_unpack_19_indices); |
| 154 | +} |
| 155 | + |
| 156 | +static MLD_INLINE void mld_poly_pointwise_montgomery_native( |
| 157 | + int32_t out[MLDSA_N], const int32_t in0[MLDSA_N], |
| 158 | + const int32_t in1[MLDSA_N]) |
| 159 | +{ |
| 160 | + mld_poly_pointwise_montgomery_asm(out, in0, in1); |
| 161 | +} |
| 162 | + |
| 163 | +static MLD_INLINE void mld_polyvecl_pointwise_acc_montgomery_l4_native( |
| 164 | + int32_t w[MLDSA_N], const int32_t u[4][MLDSA_N], |
| 165 | + const int32_t v[4][MLDSA_N]) |
| 166 | +{ |
| 167 | + mld_polyvecl_pointwise_acc_montgomery_l4_asm(w, (const int32_t *)u, |
| 168 | + (const int32_t *)v); |
| 169 | +} |
| 170 | + |
| 171 | +static MLD_INLINE void mld_polyvecl_pointwise_acc_montgomery_l5_native( |
| 172 | + int32_t w[MLDSA_N], const int32_t u[5][MLDSA_N], |
| 173 | + const int32_t v[5][MLDSA_N]) |
| 174 | +{ |
| 175 | + mld_polyvecl_pointwise_acc_montgomery_l5_asm(w, (const int32_t *)u, |
| 176 | + (const int32_t *)v); |
| 177 | +} |
| 178 | + |
| 179 | +static MLD_INLINE void mld_polyvecl_pointwise_acc_montgomery_l7_native( |
| 180 | + int32_t w[MLDSA_N], const int32_t u[7][MLDSA_N], |
| 181 | + const int32_t v[7][MLDSA_N]) |
| 182 | +{ |
| 183 | + mld_polyvecl_pointwise_acc_montgomery_l7_asm(w, (const int32_t *)u, |
| 184 | + (const int32_t *)v); |
| 185 | +} |
| 186 | + |
| 187 | +#endif /* !__ASSEMBLER__ */ |
| 188 | +#endif /* !MLD_NATIVE_AARCH64_META_H */ |
0 commit comments