Botan 3.9.0
Crypto and TLS for C&
simd_avx2_gfni.h
Go to the documentation of this file.
1/*
2* (C) 2024 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#ifndef BOTAN_SIMD_AVX2_GFNI_H_
8#define BOTAN_SIMD_AVX2_GFNI_H_
9
10#include <botan/internal/simd_avx2.h>
11
12#include <botan/internal/isa_extn.h>
13#include <stdexcept>
14#include <string_view>
15
16namespace Botan {
17
18// Helper for defining GFNI constants
19consteval uint64_t gfni_matrix(std::string_view s) {
20 uint64_t matrix = 0;
21 size_t bit_cnt = 0;
22 uint8_t row = 0;
23
24 for(char c : s) {
25 if(c == ' ' || c == '\n') {
26 continue;
27 }
28 if(c != '0' && c != '1') {
29 throw std::runtime_error("gfni_matrix: invalid bit value");
30 }
31
32 if(c == '1') {
33 row |= 0x80 >> (7 - bit_cnt % 8);
34 }
35 bit_cnt++;
36
37 if(bit_cnt % 8 == 0) {
38 matrix <<= 8;
39 matrix |= row;
40 row = 0;
41 }
42 }
43
44 if(bit_cnt != 64) {
45 throw std::runtime_error("gfni_matrix: invalid bit count");
46 }
47
48 return matrix;
49}
50
51template <uint64_t A, uint8_t B>
52BOTAN_FORCE_INLINE BOTAN_FN_ISA_AVX2_GFNI SIMD_8x32 gf2p8affine(const SIMD_8x32& x) {
53 return SIMD_8x32(_mm256_gf2p8affine_epi64_epi8(x.raw(), _mm256_set1_epi64x(A), B));
54}
55
56template <uint64_t A, uint8_t B>
57BOTAN_FORCE_INLINE BOTAN_FN_ISA_AVX2_GFNI SIMD_8x32 gf2p8affineinv(const SIMD_8x32& x) {
58 return SIMD_8x32(_mm256_gf2p8affineinv_epi64_epi8(x.raw(), _mm256_set1_epi64x(A), B));
59}
60
61BOTAN_FORCE_INLINE BOTAN_FN_ISA_AVX2_GFNI SIMD_8x32 gf2p8mul(const SIMD_8x32& a, const SIMD_8x32& b) {
62 return SIMD_8x32(_mm256_gf2p8mul_epi8(a.raw(), b.raw()));
63}
64
65} // namespace Botan
66
67#endif
__m256i BOTAN_FN_ISA_AVX2 raw() const noexcept
Definition simd_avx2.h:322
#define BOTAN_FORCE_INLINE
Definition compiler.h:87
consteval uint64_t gfni_matrix(std::string_view s)
BOTAN_FORCE_INLINE BOTAN_FN_ISA_AVX2_GFNI SIMD_8x32 gf2p8affineinv(const SIMD_8x32 &x)
BOTAN_FORCE_INLINE BOTAN_FN_ISA_AVX2_GFNI SIMD_8x32 gf2p8mul(const SIMD_8x32 &a, const SIMD_8x32 &b)
BOTAN_FORCE_INLINE BOTAN_FN_ISA_AVX2_GFNI SIMD_8x32 gf2p8affine(const SIMD_8x32 &x)