|
| 1 | +/* |
| 2 | + * Copyright (c) The mldsa-native project authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT |
| 4 | + */ |
| 5 | + |
| 6 | +/* |
| 7 | + * This file is derived from the public domain |
| 8 | + * AVX2 Dilithium implementation @[REF_AVX2]. |
| 9 | + */ |
| 10 | + |
| 11 | +#include "../../../common.h" |
| 12 | + |
| 13 | +#if defined(MLD_ARITH_BACKEND_X86_64_DEFAULT) && \ |
| 14 | + !defined(MLD_CONFIG_MULTILEVEL_NO_SHARED) |
| 15 | + |
| 16 | +#include <immintrin.h> |
| 17 | +#include <stdint.h> |
| 18 | +#include "../../../reduce.h" |
| 19 | +#include "arith_native_x86_64.h" |
| 20 | +#include "consts.h" |
| 21 | + |
| 22 | +/************************************************* |
| 23 | + * Name: mld_poly_reduce_avx2 |
| 24 | + * |
| 25 | + * Description: Inplace reduction of all coefficients of polynomial to |
| 26 | + * representative in [-6283009,6283008]. Assumes input |
| 27 | + * coefficients to be at most 2^31 - 2^22 - 1 in absolute value. |
| 28 | + * |
| 29 | + * Arguments: - int32_t *r: pointer to input/output polynomial |
| 30 | + **************************************************/ |
| 31 | +void mld_poly_reduce_avx2(int32_t *r) |
| 32 | +{ |
| 33 | + unsigned int i; |
| 34 | + __m256i f, g; |
| 35 | + const __m256i q = _mm256_set1_epi32(MLDSA_Q); |
| 36 | + const __m256i off = _mm256_set1_epi32(1 << 22); |
| 37 | + __m256i *rr = (__m256i *)r; |
| 38 | + |
| 39 | + for (i = 0; i < MLDSA_N / 8; i++) |
| 40 | + { |
| 41 | + f = _mm256_load_si256(&rr[i]); |
| 42 | + g = _mm256_add_epi32(f, off); |
| 43 | + g = _mm256_srai_epi32(g, 23); |
| 44 | + g = _mm256_mullo_epi32(g, q); |
| 45 | + f = _mm256_sub_epi32(f, g); |
| 46 | + _mm256_store_si256(&rr[i], f); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +/************************************************* |
| 51 | + * Name: mld_poly_caddq_avx2 |
| 52 | + * |
| 53 | + * Description: For all coefficients of in/out polynomial add Q if |
| 54 | + * coefficient is negative. |
| 55 | + * |
| 56 | + * Arguments: - int32_t *r: pointer to input/output polynomial |
| 57 | + **************************************************/ |
| 58 | +void mld_poly_caddq_avx2(int32_t *r) |
| 59 | +{ |
| 60 | + unsigned int i; |
| 61 | + __m256i f, g; |
| 62 | + const __m256i q = _mm256_set1_epi32(MLDSA_Q); |
| 63 | + const __m256i zero = _mm256_setzero_si256(); |
| 64 | + __m256i *rr = (__m256i *)r; |
| 65 | + |
| 66 | + for (i = 0; i < MLDSA_N / 8; i++) |
| 67 | + { |
| 68 | + f = _mm256_load_si256(&rr[i]); |
| 69 | + g = _mm256_cmpgt_epi32(zero, f); |
| 70 | + g = _mm256_and_si256(g, q); |
| 71 | + f = _mm256_add_epi32(f, g); |
| 72 | + _mm256_store_si256(&rr[i], f); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +#else /* MLD_ARITH_BACKEND_X86_64_DEFAULT && !MLD_CONFIG_MULTILEVEL_NO_SHARED \ |
| 77 | + */ |
| 78 | + |
| 79 | +MLD_EMPTY_CU(avx2_reduce) |
| 80 | + |
| 81 | +#endif /* !(MLD_ARITH_BACKEND_X86_64_DEFAULT && \ |
| 82 | + !MLD_CONFIG_MULTILEVEL_NO_SHARED) */ |
0 commit comments