Botan 3.14.1
Crypto and TLS for C&
hash_engine_sha2_64_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/hash_engine_sha2_64.h>
8
9#include <botan/internal/isa_extn.h>
10#include <botan/internal/sha2_64_f.h>
11#include <botan/internal/simd_8x64.h>
12
13namespace Botan {
14
15namespace {
16
17BOTAN_FN_ISA_SIMD_8X64 BOTAN_FORCE_INLINE void rnd(const SIMD_8x64& A,
18 const SIMD_8x64& B,
19 const SIMD_8x64& C,
20 SIMD_8x64& D,
21 const SIMD_8x64& E,
22 const SIMD_8x64& F,
23 const SIMD_8x64& G,
24 SIMD_8x64& H,
25 const SIMD_8x64& W,
26 uint64_t K) {
27 const auto T1 = H + E.sigma1() + SIMD_8x64::choose(E, F, G) + SIMD_8x64::splat(K) + W;
28 const auto T2 = A.sigma0() + SIMD_8x64::majority(A, B, C);
29 D += T1;
30 H = T1 + T2;
31}
32
33template <size_t I>
34BOTAN_FN_ISA_SIMD_8X64 BOTAN_FORCE_INLINE SIMD_8x64 next_w(SIMD_8x64 W[16]) {
35 const auto& w15 = W[(I + 1) % 16];
36 const auto& w2 = W[(I + 14) % 16];
37 const auto s0 = w15.rotr<1>() ^ w15.rotr<8>() ^ w15.shr<7>();
38 const auto s1 = w2.rotr<19>() ^ w2.rotr<61>() ^ w2.shr<6>();
39 W[I] += s0 + s1 + W[(I + 9) % 16];
40 return W[I];
41}
42
43} // namespace
44
45BOTAN_FN_ISA_SIMD_8X64 void sha2_64_mb_compress_x8(uint8_t* states, const uint8_t* const* blocks, size_t nblocks) {
46 SIMD_8x64 S[8];
47 for(size_t j = 0; j != 8; ++j) {
48 S[j] = SIMD_8x64::load_le(states + 64 * j);
49 }
50
51 for(size_t n = 0; n != nblocks; ++n) {
52 // Initialized directly from the loads, since default construction
53 // would zero the whole array first
54 SIMD_8x64 W[16] = {SIMD_8x64::load_be<128>(blocks, 0, n, 0),
55 SIMD_8x64::load_be<128>(blocks, 1, n, 0),
56 SIMD_8x64::load_be<128>(blocks, 2, n, 0),
57 SIMD_8x64::load_be<128>(blocks, 3, n, 0),
58 SIMD_8x64::load_be<128>(blocks, 4, n, 0),
59 SIMD_8x64::load_be<128>(blocks, 5, n, 0),
60 SIMD_8x64::load_be<128>(blocks, 6, n, 0),
61 SIMD_8x64::load_be<128>(blocks, 7, n, 0),
62 SIMD_8x64::load_be<128>(blocks, 0, n, 64),
63 SIMD_8x64::load_be<128>(blocks, 1, n, 64),
64 SIMD_8x64::load_be<128>(blocks, 2, n, 64),
65 SIMD_8x64::load_be<128>(blocks, 3, n, 64),
66 SIMD_8x64::load_be<128>(blocks, 4, n, 64),
67 SIMD_8x64::load_be<128>(blocks, 5, n, 64),
68 SIMD_8x64::load_be<128>(blocks, 6, n, 64),
69 SIMD_8x64::load_be<128>(blocks, 7, n, 64)};
70 SIMD_8x64::transpose(W[0], W[1], W[2], W[3], W[4], W[5], W[6], W[7]);
71 SIMD_8x64::transpose(W[8], W[9], W[10], W[11], W[12], W[13], W[14], W[15]);
72
73 SIMD_8x64 A = S[0];
74 SIMD_8x64 B = S[1];
75 SIMD_8x64 C = S[2];
76 SIMD_8x64 D = S[3];
77 SIMD_8x64 E = S[4];
78 SIMD_8x64 F = S[5];
79 SIMD_8x64 G = S[6];
80 SIMD_8x64 H = S[7];
81
82 // Every index into W is a constant so that it can stay in registers
83 rnd(A, B, C, D, E, F, G, H, W[0], SHA512_K[0]);
84 rnd(H, A, B, C, D, E, F, G, W[1], SHA512_K[1]);
85 rnd(G, H, A, B, C, D, E, F, W[2], SHA512_K[2]);
86 rnd(F, G, H, A, B, C, D, E, W[3], SHA512_K[3]);
87 rnd(E, F, G, H, A, B, C, D, W[4], SHA512_K[4]);
88 rnd(D, E, F, G, H, A, B, C, W[5], SHA512_K[5]);
89 rnd(C, D, E, F, G, H, A, B, W[6], SHA512_K[6]);
90 rnd(B, C, D, E, F, G, H, A, W[7], SHA512_K[7]);
91 rnd(A, B, C, D, E, F, G, H, W[8], SHA512_K[8]);
92 rnd(H, A, B, C, D, E, F, G, W[9], SHA512_K[9]);
93 rnd(G, H, A, B, C, D, E, F, W[10], SHA512_K[10]);
94 rnd(F, G, H, A, B, C, D, E, W[11], SHA512_K[11]);
95 rnd(E, F, G, H, A, B, C, D, W[12], SHA512_K[12]);
96 rnd(D, E, F, G, H, A, B, C, W[13], SHA512_K[13]);
97 rnd(C, D, E, F, G, H, A, B, W[14], SHA512_K[14]);
98 rnd(B, C, D, E, F, G, H, A, W[15], SHA512_K[15]);
99
100 for(size_t t = 16; t != 80; t += 16) {
101 rnd(A, B, C, D, E, F, G, H, next_w<0>(W), SHA512_K[t + 0]);
102 rnd(H, A, B, C, D, E, F, G, next_w<1>(W), SHA512_K[t + 1]);
103 rnd(G, H, A, B, C, D, E, F, next_w<2>(W), SHA512_K[t + 2]);
104 rnd(F, G, H, A, B, C, D, E, next_w<3>(W), SHA512_K[t + 3]);
105 rnd(E, F, G, H, A, B, C, D, next_w<4>(W), SHA512_K[t + 4]);
106 rnd(D, E, F, G, H, A, B, C, next_w<5>(W), SHA512_K[t + 5]);
107 rnd(C, D, E, F, G, H, A, B, next_w<6>(W), SHA512_K[t + 6]);
108 rnd(B, C, D, E, F, G, H, A, next_w<7>(W), SHA512_K[t + 7]);
109 rnd(A, B, C, D, E, F, G, H, next_w<8>(W), SHA512_K[t + 8]);
110 rnd(H, A, B, C, D, E, F, G, next_w<9>(W), SHA512_K[t + 9]);
111 rnd(G, H, A, B, C, D, E, F, next_w<10>(W), SHA512_K[t + 10]);
112 rnd(F, G, H, A, B, C, D, E, next_w<11>(W), SHA512_K[t + 11]);
113 rnd(E, F, G, H, A, B, C, D, next_w<12>(W), SHA512_K[t + 12]);
114 rnd(D, E, F, G, H, A, B, C, next_w<13>(W), SHA512_K[t + 13]);
115 rnd(C, D, E, F, G, H, A, B, next_w<14>(W), SHA512_K[t + 14]);
116 rnd(B, C, D, E, F, G, H, A, next_w<15>(W), SHA512_K[t + 15]);
117 }
118
119 S[0] += A;
120 S[1] += B;
121 S[2] += C;
122 S[3] += D;
123 S[4] += E;
124 S[5] += F;
125 S[6] += G;
126 S[7] += H;
127 }
128
129 for(size_t j = 0; j != 8; ++j) {
130 S[j].store_le(states + 64 * j);
131 }
132}
133
134BOTAN_FN_ISA_SIMD_8X64 void sha2_64_mb_extract_x8(const uint8_t* states, uint8_t* digests) {
135 SIMD_8x64 S[8];
136 for(size_t j = 0; j != 8; ++j) {
137 S[j] = SIMD_8x64::load_le(states + 64 * j);
138 }
139
140 // Each S[j] holds word j of all 8 lanes; the transpose turns that
141 // into the 8 words of each lane
142 SIMD_8x64::transpose(S[0], S[1], S[2], S[3], S[4], S[5], S[6], S[7]);
143
144 for(size_t l = 0; l != 8; ++l) {
145 S[l].store_be(digests + 64 * l);
146 }
147}
148
149} // namespace Botan
SIMD_8x64 BOTAN_FN_ISA_SIMD_8X64 sigma0() const
Definition simd_8x64.h:160
void store_le(uint64_t out[8]) const
Definition simd_8x64.h:82
static BOTAN_FN_ISA_SIMD_8X64 SIMD_8x64 choose(const SIMD_8x64 &mask, const SIMD_8x64 &a, const SIMD_8x64 &b)
Definition simd_8x64.h:175
static BOTAN_FN_ISA_SIMD_8X64 SIMD_8x64 load_le(const void *in)
Definition simd_8x64.h:51
static BOTAN_FN_ISA_SIMD_8X64 SIMD_8x64 majority(const SIMD_8x64 &x, const SIMD_8x64 &y, const SIMD_8x64 &z)
Definition simd_8x64.h:180
static BOTAN_FN_ISA_SIMD_8X64 SIMD_8x64 splat(uint64_t v)
Definition simd_8x64.h:319
BOTAN_FN_ISA_SIMD_8X64 SIMD_8x64 rotr() const
Definition simd_8x64.h:149
BOTAN_FN_ISA_SIMD_8X64 void store_be(uint8_t out[]) const
Definition simd_8x64.h:88
static BOTAN_FN_ISA_SIMD_8X64 SIMD_8x64 load_be(const void *in)
Definition simd_8x64.h:60
static BOTAN_FN_ISA_SIMD_8X64 void transpose(SIMD_8x64 &B0, SIMD_8x64 &B1, SIMD_8x64 &B2, SIMD_8x64 &B3, SIMD_8x64 &B4, SIMD_8x64 &B5, SIMD_8x64 &B6, SIMD_8x64 &B7)
Definition simd_8x64.h:185
SIMD_8x64 BOTAN_FN_ISA_SIMD_8X64 shr() const noexcept
Definition simd_8x64.h:222
#define BOTAN_FORCE_INLINE
Definition compiler.h:86
constexpr uint64_t SHA512_K[80]
Definition sha2_64_f.h:19
BOTAN_FN_ISA_SIMD_8X64 void sha2_64_mb_compress_x8(uint8_t *states, const uint8_t *const *blocks, size_t nblocks)
BOTAN_FN_ISA_SIMD_8X64 void sha2_64_mb_extract_x8(const uint8_t *states, uint8_t *digests)