|
| 1 | +/* |
| 2 | + * Copyright (c) The mldsa-native project authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT |
| 4 | + */ |
| 5 | + |
| 6 | +/* References |
| 7 | + * ========== |
| 8 | + * |
| 9 | + * - [REF_AVX2] |
| 10 | + * CRYSTALS-Dilithium optimized AVX2 implementation |
| 11 | + * Bai, Ducas, Kiltz, Lepoint, Lyubashevsky, Schwabe, Seiler, Stehlé |
| 12 | + * https://github.com/pq-crystals/dilithium/tree/master/avx2 |
| 13 | + */ |
| 14 | + |
| 15 | +/* |
| 16 | + * This file is derived from the public domain |
| 17 | + * AVX2 Dilithium implementation @[REF_AVX2]. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "../../../common.h" |
| 21 | + |
| 22 | +#if defined(MLD_ARITH_BACKEND_X86_64_DEFAULT) && \ |
| 23 | + !defined(MLD_CONFIG_MULTILEVEL_NO_SHARED) |
| 24 | + |
| 25 | +#include <immintrin.h> |
| 26 | +#include <stdint.h> |
| 27 | +#include "arith_native_x86_64.h" |
| 28 | + |
| 29 | +void mld_polyz_unpack_19_avx2(__m256i *r, const uint8_t *a) |
| 30 | +{ |
| 31 | + unsigned int i; |
| 32 | + __m256i f; |
| 33 | + const __m256i shufbidx = |
| 34 | + _mm256_set_epi8(-1, 11, 10, 9, -1, 9, 8, 7, -1, 6, 5, 4, -1, 4, 3, 2, -1, |
| 35 | + 9, 8, 7, -1, 7, 6, 5, -1, 4, 3, 2, -1, 2, 1, 0); |
| 36 | + /* Equivalent to _mm256_set_epi32(4, 0, 4, 0, 4, 0, 4, 0) */ |
| 37 | + const __m256i srlvdidx = _mm256_set1_epi64x((uint64_t)4 << 32); |
| 38 | + const __m256i mask = _mm256_set1_epi32(0xFFFFF); |
| 39 | + const __m256i gamma1 = _mm256_set1_epi32(MLDSA_GAMMA1); |
| 40 | + |
| 41 | + for (i = 0; i < MLDSA_N / 8; i++) |
| 42 | + { |
| 43 | + f = _mm256_loadu_si256((__m256i *)&a[20 * i]); |
| 44 | + |
| 45 | + /* Permute 64-bit lanes |
| 46 | + * 0x94 = 10010100b rearranges 64-bit lanes as: [3,2,1,0] -> [2,1,1,0] |
| 47 | + * |
| 48 | + * ╔═══════════════════════════════════════════════════════════════════════╗ |
| 49 | + * ║ Original Layout ║ |
| 50 | + * ╚═══════════════════════════════════════════════════════════════════════╝ |
| 51 | + * ┌─────────────────┬─────────────────┬─────────────────┬─────────────────┐ |
| 52 | + * │ Lane 0 │ Lane 1 │ Lane 2 │ Lane 3 │ |
| 53 | + * │ bytes 0..7 │ bytes 8..15 │ bytes 16..23 │ bytes 24..31 │ |
| 54 | + * └─────────────────┴─────────────────┴─────────────────┴─────────────────┘ |
| 55 | + * |
| 56 | + * ╔═══════════════════════════════════════════════════════════════════════╗ |
| 57 | + * ║ Layout after permute ║ |
| 58 | + * ║ Byte indices in high half shifted down by 8 positions ║ |
| 59 | + * ╚═══════════════════════════════════════════════════════════════════════╝ |
| 60 | + * ┌───────────────┬─────────────────┐ ┌─────────────────┬─────────────────┐ |
| 61 | + * │ Lane 0 │ Lane 1 │ │ Lane 2 │ Lane 3 │ |
| 62 | + * │ bytes 0..7 │ bytes 8..15 │ │ bytes 8..15 │ bytes 16..23 │ |
| 63 | + * └───────────────┴─────────────────┘ └─────────────────┴─────────────────┘ |
| 64 | + * Lower 128-bit lane (bytes 0-15) Upper 128-bit lane (bytes 16-31) |
| 65 | + */ |
| 66 | + f = _mm256_permute4x64_epi64(f, 0x94); |
| 67 | + |
| 68 | + /* Shuffling 8-bit lanes |
| 69 | + * |
| 70 | + * ┌─ Indices 0-9 into low 128-bit half of permuted vector ────────────────┐ |
| 71 | + * │ Shuffle: [-1, 9, 8, 7, -1, 7, 6, 5, -1, 4, 3, 2, -1, 2, 1, 0] │ |
| 72 | + * │ Result: [0, byte9, byte8, byte7, ..., 0, byte2, byte1, byte0] │ |
| 73 | + * └───────────────────────────────────────────────────────────────────────┘ |
| 74 | + * |
| 75 | + * ┌─ Indices 2-11 into high 128-bit half of permuted vector ──────────────┐ |
| 76 | + * │ Shuffle: [-1, 11, 9, 8, -1, 9, 8, 7, -1, 6, 5, 4, -1, 4, 3, 2] │ |
| 77 | + * │ Result: [0, byte19, byte18, byte17, ..., 0, byte12, byte11, byte10] │ |
| 78 | + * └───────────────────────────────────────────────────────────────────────┘ |
| 79 | + */ |
| 80 | + f = _mm256_shuffle_epi8(f, shufbidx); |
| 81 | + |
| 82 | + /* Keep only 20 out of 24 bits in each 32-bit lane */ |
| 83 | + /* Bits 0..23 16..39 40..63 56..79 |
| 84 | + * 80..103 96..119 120..143 136..159 */ |
| 85 | + f = _mm256_srlv_epi32(f, srlvdidx); |
| 86 | + /* Bits 0..23 20..39 40..63 60..79 |
| 87 | + * 80..103 100..119 120..143 140..159 */ |
| 88 | + f = _mm256_and_si256(f, mask); |
| 89 | + /* Bits 0..19 20..39 40..59 60..79 |
| 90 | + * 80..99 100..119 120..139 140..159 */ |
| 91 | + |
| 92 | + /* Map [0, 1, ..., 2^20-1] to [2^19, 2^19-1, ..., -2^19+1] */ |
| 93 | + f = _mm256_sub_epi32(gamma1, f); |
| 94 | + |
| 95 | + _mm256_store_si256(&r[i], f); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +#else /* MLD_ARITH_BACKEND_X86_64_DEFAULT && !MLD_CONFIG_MULTILEVEL_NO_SHARED \ |
| 100 | + */ |
| 101 | + |
| 102 | +MLD_EMPTY_CU(avx2_polyz_unpack) |
| 103 | + |
| 104 | +#endif /* !(MLD_ARITH_BACKEND_X86_64_DEFAULT && \ |
| 105 | + !MLD_CONFIG_MULTILEVEL_NO_SHARED) */ |
0 commit comments