Botan 3.14.1
Crypto and TLS for C&
keccak_perm_avx512.cpp
Go to the documentation of this file.
1/*
2* (C) 2025 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#include <botan/internal/keccak_perm.h>
8
9#include <botan/internal/isa_extn.h>
10#include <botan/internal/keccak_perm_round.h>
11#include <immintrin.h>
12
13namespace Botan {
14
15namespace {
16
17class SIMD_5x64 final {
18 public:
19 explicit BOTAN_FN_ISA_AVX512 SIMD_5x64() : SIMD_5x64(_mm512_setzero_si512()) {}
20
21 static BOTAN_FN_ISA_AVX512 SIMD_5x64 rc(uint64_t RC) {
22 return SIMD_5x64(_mm512_maskz_set1_epi64(0b00000001, RC));
23 }
24
25 static BOTAN_FN_ISA_AVX512 SIMD_5x64 load(const uint64_t v[5]) {
26 return SIMD_5x64(_mm512_maskz_loadu_epi64(0b00011111, v));
27 }
28
29 template <size_t I0, size_t I1, size_t I2, size_t I3, size_t I4>
30 inline BOTAN_FN_ISA_AVX512 SIMD_5x64 permute() const {
31 static_assert(I0 < 5 && I1 < 5 && I2 < 5 && I3 < 5 && I4 < 5);
32 const __m512i tbl = _mm512_setr_epi64(I0, I1, I2, I3, I4, 0, 0, 0);
33 return SIMD_5x64(_mm512_permutexvar_epi64(tbl, m_v));
34 }
35
36 static BOTAN_FN_ISA_AVX512 void transpose5(
37 SIMD_5x64& i0, SIMD_5x64& i1, SIMD_5x64& i2, SIMD_5x64& i3, SIMD_5x64& i4) {
38 // 5x5 u64 transpose using 7 permutex2var, 4 unpack, 1 blend, 5 constants
39
40 const auto lo_01 = _mm512_unpacklo_epi64(i0.m_v, i1.m_v);
41 const auto lo_23 = _mm512_unpacklo_epi64(i2.m_v, i3.m_v);
42
43 const auto hi_01 = _mm512_unpackhi_epi64(i0.m_v, i1.m_v);
44 const auto hi_23 = _mm512_unpackhi_epi64(i2.m_v, i3.m_v);
45
46 // Insert the relevant words from i4 into the i0/i1 data
47 const auto i4_lo_idx = _mm512_setr_epi64(0, 1, 2, 3, 4, 5, 8, 10);
48 const auto i4_hi_idx = _mm512_setr_epi64(0, 1, 2, 3, -1, -1, 9, 11);
49
50 const auto t0 = _mm512_permutex2var_epi64(lo_01, i4_lo_idx, i4.m_v);
51 const auto t2 = _mm512_permutex2var_epi64(hi_01, i4_hi_idx, i4.m_v);
52
53 // Now merge the 0/1/4 and 2/3 vectors using permutes
54 const auto idx0 = _mm512_setr_epi64(0, 1, 8, 9, 6, -1, -1, -1);
55 const auto idx1 = _mm512_setr_epi64(2, 3, 10, 11, 7, -1, -1, -1);
56 const auto idx4 = _mm512_setr_epi64(4, 5, 12, 13, -1, -1, -1, -1);
57
58 i0.m_v = _mm512_permutex2var_epi64(t0, idx0, lo_23);
59 i1.m_v = _mm512_permutex2var_epi64(t2, idx0, hi_23);
60 i2.m_v = _mm512_permutex2var_epi64(t0, idx1, lo_23);
61 i3.m_v = _mm512_permutex2var_epi64(t2, idx1, hi_23);
62 i4.m_v = _mm512_mask_blend_epi64(0b00010000, _mm512_permutex2var_epi64(t0, idx4, lo_23), i4.m_v);
63 }
64
65 static BOTAN_FN_ISA_AVX512 SIMD_5x64 chi(const SIMD_5x64& x, const SIMD_5x64& y, const SIMD_5x64& z) {
66 constexpr uint8_t xor_not_and = 0b11010010; // (x ^ (~y & z))
67 return SIMD_5x64(_mm512_ternarylogic_epi64(x.m_v, y.m_v, z.m_v, xor_not_and));
68 }
69
70 friend BOTAN_FN_ISA_AVX512 SIMD_5x64 operator^(const SIMD_5x64& x, const SIMD_5x64& y) {
71 return SIMD_5x64(_mm512_xor_epi64(x.m_v, y.m_v));
72 }
73
74 static BOTAN_FN_ISA_AVX512 SIMD_5x64
75 xor5(const SIMD_5x64& i0, const SIMD_5x64& i1, const SIMD_5x64& i2, const SIMD_5x64& i3, const SIMD_5x64& i4) {
76 constexpr uint8_t tern_xor = 0b10010110;
77 const auto t = _mm512_ternarylogic_epi64(i0.m_v, i1.m_v, i2.m_v, tern_xor);
78 return SIMD_5x64(_mm512_ternarylogic_epi64(i3.m_v, i4.m_v, t, tern_xor));
79 }
80
81 BOTAN_FN_ISA_AVX512 SIMD_5x64 rol1() const { return SIMD_5x64(_mm512_rol_epi64(m_v, 1)); }
82
83 template <size_t R0, size_t R1, size_t R2, size_t R3, size_t R4>
84 BOTAN_FN_ISA_AVX512 SIMD_5x64 rolv() const {
85 static_assert(R0 < 64 && R1 < 64 && R2 < 64 && R3 < 64 && R4 < 64);
86 const __m512i rot = _mm512_setr_epi64(R0, R1, R2, R3, R4, 0, 0, 0);
87 return SIMD_5x64(_mm512_rolv_epi64(m_v, rot));
88 }
89
90 BOTAN_FN_ISA_AVX512 void store(uint64_t v[5]) const { _mm512_mask_storeu_epi64(v, 0b00011111, m_v); }
91
92 private:
93 explicit BOTAN_FN_ISA_AVX512 SIMD_5x64(__m512i v) : m_v(v) {}
94
95 __m512i m_v;
96};
97
98inline void BOTAN_FN_ISA_AVX512 Keccak_Permutation_round_avx512(SIMD_5x64 A[5], uint64_t RC) {
99 const auto C = SIMD_5x64::xor5(A[0], A[1], A[2], A[3], A[4]);
100
101 const auto D = C.permute<4, 0, 1, 2, 3>() ^ C.permute<1, 2, 3, 4, 0>().rol1();
102
103 const auto B0 = (A[0] ^ D).permute<0, 3, 1, 4, 2>().rolv<0, 28, 1, 27, 62>();
104 const auto B1 = (A[1] ^ D).permute<1, 4, 2, 0, 3>().rolv<44, 20, 6, 36, 55>();
105 const auto B2 = (A[2] ^ D).permute<2, 0, 3, 1, 4>().rolv<43, 3, 25, 10, 39>();
106 const auto B3 = (A[3] ^ D).permute<3, 1, 4, 2, 0>().rolv<21, 45, 8, 15, 41>();
107 const auto B4 = (A[4] ^ D).permute<4, 2, 0, 3, 1>().rolv<14, 61, 18, 56, 2>();
108
109 auto T0 = SIMD_5x64::chi(B0, B1, B2) ^ SIMD_5x64::rc(RC);
110 auto T1 = SIMD_5x64::chi(B1, B2, B3);
111 auto T2 = SIMD_5x64::chi(B2, B3, B4);
112 auto T3 = SIMD_5x64::chi(B3, B4, B0);
113 auto T4 = SIMD_5x64::chi(B4, B0, B1);
114
115 SIMD_5x64::transpose5(T0, T1, T2, T3, T4);
116
117 A[0] = T0;
118 A[1] = T1;
119 A[2] = T2;
120 A[3] = T3;
121 A[4] = T4;
122}
123
124} // namespace
125
126void BOTAN_FN_ISA_AVX512 Keccak_Permutation::permute_avx512() {
127 auto& S = state();
128
129 std::array<SIMD_5x64, 5> X{
130 SIMD_5x64::load(&S[0]), // NOLINT(*container-data-pointer)
131 SIMD_5x64::load(&S[5]),
132 SIMD_5x64::load(&S[10]),
133 SIMD_5x64::load(&S[15]),
134 SIMD_5x64::load(&S[20]),
135 };
136
137 // NOLINTNEXTLINE(modernize-loop-convert)
138 for(size_t i = 0; i != 24; ++i) {
139 Keccak_Permutation_round_avx512(X.data(), KECCAK_RC[i]);
140 }
141
142 for(size_t i = 0; i != 5; ++i) {
143 X[i].store(&S[5 * i]);
144 }
145}
146
147} // namespace Botan
constexpr auto & state()
Definition sponge.h:63
OctetString operator^(const OctetString &k1, const OctetString &k2)
Definition symkey.cpp:109
constexpr uint64_t KECCAK_RC[24]
void R2(uint32_t A, uint32_t &B, uint32_t C, uint32_t &D, uint32_t E, uint32_t &F, uint32_t G, uint32_t &H, uint32_t TJ, uint32_t Wi, uint32_t Wj)
Definition sm3_fn.h:43
void R1(uint32_t A, uint32_t &B, uint32_t C, uint32_t &D, uint32_t E, uint32_t &F, uint32_t G, uint32_t &H, uint32_t TJ, uint32_t Wi, uint32_t Wj)
Definition sm3_fn.h:21