Botan 3.14.1
Crypto and TLS for C&
whirlpool_avx512.cpp
Go to the documentation of this file.
1/*
2* (C) 2026 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#include <botan/internal/whirlpool.h>
8
9#include <botan/internal/isa_extn.h>
10#include <botan/internal/whirlpool_consts.h>
11#include <immintrin.h>
12
13namespace Botan {
14
15namespace WhirlpoolAVX512 {
16
17namespace {
18
19// NOLINTBEGIN(portability-simd-intrinsics)
20
21class WhirlpoolState final {
22 public:
23 BOTAN_FN_ISA_AVX512
24 WhirlpoolState() : m_v(_mm512_setzero_si512()) {}
25
26 BOTAN_FN_ISA_AVX512
27 explicit WhirlpoolState(__m512i v) : m_v(v) {}
28
29 WhirlpoolState(const WhirlpoolState& other) = default;
30 WhirlpoolState(WhirlpoolState&& other) = default;
31 WhirlpoolState& operator=(const WhirlpoolState& other) = default;
32 WhirlpoolState& operator=(WhirlpoolState&& other) = default;
33 ~WhirlpoolState() = default;
34
35 // Load 64 bytes of message data
36 BOTAN_FN_ISA_AVX512
37 static WhirlpoolState load_bytes(const uint8_t src[64]) { return WhirlpoolState(_mm512_loadu_si512(src)); }
38
39 BOTAN_FN_ISA_AVX512
40 static WhirlpoolState load_be(const uint64_t src[8]) { return WhirlpoolState(_mm512_loadu_si512(src)).bswap(); }
41
42 BOTAN_FN_ISA_AVX512
43 void store_be(uint64_t dst[8]) const { _mm512_storeu_si512(dst, bswap().m_v); }
44
45 BOTAN_FN_ISA_AVX512
46 inline friend WhirlpoolState operator^(WhirlpoolState a, WhirlpoolState b) {
47 return WhirlpoolState(_mm512_xor_si512(a.m_v, b.m_v));
48 }
49
50 BOTAN_FN_ISA_AVX512
51 inline WhirlpoolState& operator^=(WhirlpoolState other) {
52 m_v = _mm512_xor_si512(m_v, other.m_v);
53 return *this;
54 }
55
56 /*
57 * The Whirlpool 8-bit Sbox is built out of 4-bit sboxes, which can be
58 * individually computed using pshufb-style shuffles.
59 */
60 BOTAN_FN_ISA_AVX512
61 inline WhirlpoolState sub_bytes() const {
62 const __m512i Ebox =
63 _mm512_broadcast_i32x4(_mm_setr_epi8(1, 11, 9, 12, 13, 6, 15, 3, 14, 8, 7, 4, 10, 2, 5, 0));
64 const __m512i Eibox =
65 _mm512_broadcast_i32x4(_mm_setr_epi8(15, 0, 13, 7, 11, 14, 5, 10, 9, 2, 12, 1, 3, 4, 8, 6));
66 const __m512i Rbox =
67 _mm512_broadcast_i32x4(_mm_setr_epi8(7, 12, 11, 13, 14, 4, 9, 15, 6, 3, 8, 10, 2, 5, 1, 0));
68
69 const __m512i lo_mask = _mm512_set1_epi8(0x0F);
70
71 const __m512i lo_nib = _mm512_and_si512(m_v, lo_mask);
72 const __m512i hi_nib = _mm512_and_si512(_mm512_srli_epi16(m_v, 4), lo_mask);
73
74 // L = Ebox[hi], R = Eibox[lo], T = Rbox[L ^ R]
75 const __m512i L = _mm512_shuffle_epi8(Ebox, hi_nib);
76 const __m512i R = _mm512_shuffle_epi8(Eibox, lo_nib);
77 const __m512i T = _mm512_shuffle_epi8(Rbox, _mm512_xor_si512(L, R));
78
79 // result = (Ebox[L ^ T] << 4) | Eibox[R ^ T]
80 const __m512i out_hi = _mm512_shuffle_epi8(Ebox, _mm512_xor_si512(L, T));
81 const __m512i out_lo = _mm512_shuffle_epi8(Eibox, _mm512_xor_si512(R, T));
82
83 return WhirlpoolState(_mm512_or_si512(_mm512_slli_epi16(out_hi, 4), _mm512_and_si512(out_lo, lo_mask)));
84 }
85
86 /*
87 * ShiftColumns: column j is cyclically shifted down by j positions.
88 *
89 * For output row r, column c: source = row (r - c + 8) % 8, column c.
90 * Implemented as a single vpermb with a fixed 64-byte permutation.
91 */
92 BOTAN_FN_ISA_AVX512
93 inline WhirlpoolState shift_columns() const {
94 // Register byte for (row r, col c) = r*8 + c
95 // Source byte = ((r - c + 8) % 8) * 8 + c
96 alignas(64) static constexpr uint8_t perm[64] = {
97 // clang-format off
98 0*8+0, 7*8+1, 6*8+2, 5*8+3, 4*8+4, 3*8+5, 2*8+6, 1*8+7,
99 1*8+0, 0*8+1, 7*8+2, 6*8+3, 5*8+4, 4*8+5, 3*8+6, 2*8+7,
100 2*8+0, 1*8+1, 0*8+2, 7*8+3, 6*8+4, 5*8+5, 4*8+6, 3*8+7,
101 3*8+0, 2*8+1, 1*8+2, 0*8+3, 7*8+4, 6*8+5, 5*8+6, 4*8+7,
102 4*8+0, 3*8+1, 2*8+2, 1*8+3, 0*8+4, 7*8+5, 6*8+6, 5*8+7,
103 5*8+0, 4*8+1, 3*8+2, 2*8+3, 1*8+4, 0*8+5, 7*8+6, 6*8+7,
104 6*8+0, 5*8+1, 4*8+2, 3*8+3, 2*8+4, 1*8+5, 0*8+6, 7*8+7,
105 7*8+0, 6*8+1, 5*8+2, 4*8+3, 3*8+4, 2*8+5, 1*8+6, 0*8+7,
106 // clang-format on
107 };
108 return WhirlpoolState(_mm512_permutexvar_epi8(_mm512_load_si512(perm), m_v));
109 }
110
111 /*
112 * MixRows: MDS circulant [1, 1, 4, 1, 8, 5, 2, 9] over GF(2^8) mod 0x11D
113 *
114 * Since the MDS coefficients are so small we can easily compute them using
115 * a few xtimes plus additions (aka XOR)
116 */
117 BOTAN_FN_ISA_AVX512
118 inline WhirlpoolState mix_rows() const {
119 /*
120 Constants for quadword rotations by X bytes.
121
122 Could use _mm512_rol_epi64 for this, but it's oddly slower even though
123 all documentation suggests that both instructions have the same latency
124 and throughput.
125 */
126 const __m512i rot1 =
127 _mm512_broadcast_i32x4(_mm_setr_epi8(7, 0, 1, 2, 3, 4, 5, 6, 15, 8, 9, 10, 11, 12, 13, 14));
128 const __m512i rot2 =
129 _mm512_broadcast_i32x4(_mm_setr_epi8(6, 7, 0, 1, 2, 3, 4, 5, 14, 15, 8, 9, 10, 11, 12, 13));
130 const __m512i rot3 =
131 _mm512_broadcast_i32x4(_mm_setr_epi8(5, 6, 7, 0, 1, 2, 3, 4, 13, 14, 15, 8, 9, 10, 11, 12));
132 const __m512i rot4 =
133 _mm512_broadcast_i32x4(_mm_setr_epi8(4, 5, 6, 7, 0, 1, 2, 3, 12, 13, 14, 15, 8, 9, 10, 11));
134 const __m512i rot5 =
135 _mm512_broadcast_i32x4(_mm_setr_epi8(3, 4, 5, 6, 7, 0, 1, 2, 11, 12, 13, 14, 15, 8, 9, 10));
136 const __m512i rot6 =
137 _mm512_broadcast_i32x4(_mm_setr_epi8(2, 3, 4, 5, 6, 7, 0, 1, 10, 11, 12, 13, 14, 15, 8, 9));
138 const __m512i rot7 =
139 _mm512_broadcast_i32x4(_mm_setr_epi8(1, 2, 3, 4, 5, 6, 7, 0, 9, 10, 11, 12, 13, 14, 15, 8));
140
141 const __m512i x2 = xtime(m_v);
142 const __m512i x4 = xtime(x2);
143 const __m512i x8 = xtime(x4);
144 const __m512i x5 = _mm512_xor_si512(x4, m_v);
145 const __m512i x9 = _mm512_xor_si512(x8, m_v);
146
147 const __m512i t01 = _mm512_xor_si512(m_v, _mm512_shuffle_epi8(m_v, rot1));
148 const __m512i t23 = _mm512_xor_si512(_mm512_shuffle_epi8(x4, rot2), _mm512_shuffle_epi8(m_v, rot3));
149 const __m512i t45 = _mm512_xor_si512(_mm512_shuffle_epi8(x8, rot4), _mm512_shuffle_epi8(x5, rot5));
150 const __m512i t67 = _mm512_xor_si512(_mm512_shuffle_epi8(x2, rot6), _mm512_shuffle_epi8(x9, rot7));
151
152 return WhirlpoolState(_mm512_xor_si512(_mm512_xor_si512(t01, t23), _mm512_xor_si512(t45, t67)));
153 }
154
155 /*
156 * Whirlpool round: SubBytes -> ShiftColumns -> MixRows
157 */
158 BOTAN_FN_ISA_AVX512
159 inline WhirlpoolState round() const { return sub_bytes().shift_columns().mix_rows(); }
160
161 // Round constant
162 BOTAN_FN_ISA_AVX512
163 static inline WhirlpoolState rc(uint64_t v) { return WhirlpoolState(_mm512_set_epi64(0, 0, 0, 0, 0, 0, 0, v)); }
164
165 private:
166 BOTAN_FN_ISA_AVX512
167 WhirlpoolState bswap() const {
168 const __m512i tbl = _mm512_broadcast_i32x4(_mm_set_epi8(8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7));
169
170 return WhirlpoolState(_mm512_shuffle_epi8(m_v, tbl));
171 }
172
173 // Packed 16-wide doubling in GF(2^8) mod 0x11D
174 BOTAN_FN_ISA_AVX512
175 static __m512i xtime(__m512i a) {
176 const __m512i poly = _mm512_set1_epi8(0x1D);
177 const __mmask64 top_bits = _mm512_movepi8_mask(a);
178 const __m512i shifted = _mm512_add_epi8(a, a); // no 8-bit shift in AVX512
179 return _mm512_mask_blend_epi8(top_bits, shifted, _mm512_xor_si512(shifted, poly));
180 }
181
182 __m512i m_v;
183};
184
185// NOLINTEND(portability-simd-intrinsics)
186
187} // namespace
188
189} // namespace WhirlpoolAVX512
190
191BOTAN_FN_ISA_AVX512
192void Whirlpool::compress_n_avx512(digest_type& digest, std::span<const uint8_t> input, size_t blocks) {
193 using WhirlpoolAVX512::WhirlpoolState;
194
195 auto H = WhirlpoolState::load_be(digest.data());
196
197 constexpr auto WHIRL_RC = whirlpool_rc<true>();
198
199 for(size_t i = 0; i != blocks; ++i) {
200 const auto M = WhirlpoolState::load_bytes(input.data() + i * 64);
201
202 auto K = H;
203 H ^= M;
204 auto B = H; // B = M ^ K
205
206 for(size_t r = 0; r != 10; ++r) {
207 K = K.round() ^ WhirlpoolState::rc(WHIRL_RC[r]);
208 B = B.round() ^ K;
209 }
210
211 H ^= B;
212 }
213
214 H.store_be(digest.data());
215}
216
217} // namespace Botan
OctetString operator^(const OctetString &k1, const OctetString &k2)
Definition symkey.cpp:109
std::vector< uint8_t, Alloc > & operator^=(std::vector< uint8_t, Alloc > &out, const std::vector< uint8_t, Alloc2 > &in)
Definition mem_ops.h:532
consteval std::array< uint64_t, 10 > whirlpool_rc() noexcept
constexpr auto store_be(ParamTs &&... params)
Definition loadstor.h:745
constexpr auto load_be(ParamTs &&... params)
Definition loadstor.h:504