Botan 3.14.1
Crypto and TLS for C&
polyval_fn.h
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#ifndef BOTAN_POLYVAL_FN_H_
8#define BOTAN_POLYVAL_FN_H_
9
10#include <botan/internal/simd_4x32.h>
11
12#if defined(BOTAN_SIMD_USE_SSSE3)
13 #include <wmmintrin.h>
14#endif
15
16namespace Botan {
17
18// NOLINTBEGIN(portability-simd-intrinsics)
19
20template <int M>
21BOTAN_FORCE_INLINE BOTAN_FN_ISA_CLMUL SIMD_4x32 clmul(const SIMD_4x32& H, const SIMD_4x32& x) {
22 static_assert(M == 0x00 || M == 0x01 || M == 0x10 || M == 0x11, "Valid clmul mode");
23
24#if defined(BOTAN_SIMD_USE_SSSE3)
25 return SIMD_4x32(_mm_clmulepi64_si128(x.raw(), H.raw(), M));
26#elif defined(BOTAN_SIMD_USE_NEON)
27 const uint64_t a = vgetq_lane_u64(vreinterpretq_u64_u32(x.raw()), M & 0x01);
28 const uint64_t b = vgetq_lane_u64(vreinterpretq_u64_u32(H.raw()), (M & 0x10) >> 4);
29
30 #if defined(BOTAN_BUILD_COMPILER_IS_MSVC)
31 __n64 a1 = {a}, b1 = {b};
32 return SIMD_4x32(vmull_p64(a1, b1));
33 #else
34 return SIMD_4x32(reinterpret_cast<uint32x4_t>(vmull_p64(a, b)));
35 #endif
36
37#elif defined(BOTAN_SIMD_USE_VSX)
38 const SIMD_4x32 mask_lo = SIMD_4x32(0, 0, 0xFFFFFFFF, 0xFFFFFFFF);
39 constexpr uint8_t flip = (std::endian::native == std::endian::big) ? 0x11 : 0x00;
40
41 SIMD_4x32 i1 = x;
42 SIMD_4x32 i2 = H;
43
44 if constexpr(std::endian::native == std::endian::big) {
45 i1 = i1.reverse_all_bytes().bswap();
46 i2 = i2.reverse_all_bytes().bswap();
47 }
48
49 if constexpr(M == (0x11 ^ flip)) {
50 i1 &= mask_lo;
51 i2 &= mask_lo;
52 } else if constexpr(M == (0x10 ^ flip)) {
53 i1 = i1.shift_elems_left<2>();
54 } else if constexpr(M == (0x01 ^ flip)) {
55 i2 = i2.shift_elems_left<2>();
56 } else if constexpr(M == (0x00 ^ flip)) {
57 i1 = mask_lo.andc(i1);
58 i2 = mask_lo.andc(i2);
59 }
60
61 auto i1v = reinterpret_cast<__vector unsigned long long>(i1.raw());
62 auto i2v = reinterpret_cast<__vector unsigned long long>(i2.raw());
63
64 #if BOTAN_COMPILER_HAS_BUILTIN(__builtin_crypto_vpmsumd)
65 auto rv = __builtin_crypto_vpmsumd(i1v, i2v);
66 #else
67 auto rv = __builtin_altivec_crypto_vpmsumd(i1v, i2v);
68 #endif
69
70 auto z = SIMD_4x32(reinterpret_cast<__vector unsigned int>(rv));
71
72 if constexpr(std::endian::native == std::endian::big) {
73 z = z.reverse_all_bytes().bswap();
74 }
75
76 return z;
77#endif
78}
79
80// NOLINTEND(portability-simd-intrinsics)
81
82/**
83* Load a block in the reflected GHASH convention (BSWAP == true) or the
84* natural POLYVAL convention (BSWAP == false)
85*/
86template <bool BSWAP>
87BOTAN_FORCE_INLINE BOTAN_FN_ISA_SIMD_4X32 SIMD_4x32 load_block(const uint8_t in[]) {
88 if constexpr(BSWAP) {
90 } else {
91 return SIMD_4x32::load_le(in);
92 }
93}
94
95template <bool BSWAP>
96BOTAN_FORCE_INLINE BOTAN_FN_ISA_SIMD_4X32 void store_block(const SIMD_4x32& b, uint8_t out[]) {
97 if constexpr(BSWAP) {
99 } else {
100 b.store_le(out);
101 }
102}
103
104BOTAN_FORCE_INLINE SIMD_4x32 BOTAN_FN_ISA_SIMD_4X32 mulx_polyval(const SIMD_4x32& h) {
105 const auto V = SIMD_4x32(0x00000001, 0x00000000, 0x00000000, 0xc2000000);
106
107 // Bitmask set iff the top bit of h is set
108 const auto mask = h.top_bit_mask();
109
110 // Extract the top bits of the words and move them into place as the low bit of the next word
111 const auto top_bits = h.shr<31>().shift_elems_left<1>();
112
113 // The main shift, adding back in the top bits that are otherwise lost
114 const auto shifted_h = h.shl<1>() | top_bits;
115
116 return shifted_h ^ (mask & V);
117}
118
119BOTAN_FORCE_INLINE SIMD_4x32 BOTAN_FN_ISA_CLMUL polyval_reduce(const SIMD_4x32& hi, const SIMD_4x32& lo) {
120 const SIMD_4x32 V(0, 0xC2000000, 0, 0);
121
122 /*
123 Montgomery reduction
124 Input: 256-bit operand [X3 : X2 : X1 : X0]
125 [A1 : A0] = X0 * 0xc200000000000000
126 [B1 : B0] = [X0 + A1 : X1 + A0]
127 [C1 : C0] = B0 * 0xc200000000000000
128 [D1 : D0] = [B0 + C1 : B1 + C0]
129 Output: [D1 + X3 : D0 + X2]
130 */
131
132 const auto A = clmul<0x00>(lo, V);
133 const auto B = A ^ lo.swap_halves();
134 const auto C = clmul<0x00>(B, V);
135 const auto D = C ^ B.swap_halves();
136
137 return D ^ hi;
138}
139
140BOTAN_FORCE_INLINE SIMD_4x32 BOTAN_FN_ISA_CLMUL polyval_multiply(const SIMD_4x32& H, const SIMD_4x32& x) {
141 SIMD_4x32 hi = clmul<0x11>(H, x);
142 const SIMD_4x32 mid = clmul<0x10>(H, x) ^ clmul<0x01>(H, x);
143 SIMD_4x32 lo = clmul<0x00>(H, x);
144
145 hi ^= mid.shift_elems_right<2>();
146 lo ^= mid.shift_elems_left<2>();
147
148 return polyval_reduce(hi, lo);
149}
150
151} // namespace Botan
152
153#endif
void BOTAN_FN_ISA_SIMD_4X32 store_le(uint32_t out[4]) const noexcept
Definition simd_4x32.h:210
SIMD_4x32 BOTAN_FN_ISA_SIMD_4X32 top_bit_mask() const
Definition simd_4x32.h:905
SIMD_4x32 BOTAN_FN_ISA_SIMD_4X32 shr() const noexcept
Definition simd_4x32.h:485
static SIMD_4x32 BOTAN_FN_ISA_SIMD_4X32 load_le(const void *in) noexcept
Definition simd_4x32.h:183
SIMD_4x32 BOTAN_FN_ISA_SIMD_4X32 shl() const noexcept
Definition simd_4x32.h:467
BOTAN_FN_ISA_SIMD_4X32 SIMD_4x32 reverse_all_bytes() const
Definition simd_4x32.h:536
native_simd_type BOTAN_FN_ISA_SIMD_4X32 raw() const noexcept
Definition simd_4x32.h:964
SIMD_4x32 BOTAN_FN_ISA_SIMD_4X32 shift_elems_right() const noexcept
Definition simd_4x32.h:599
SIMD_4x32 BOTAN_FN_ISA_SIMD_4X32 andc(const SIMD_4x32 &other) const noexcept
Definition simd_4x32.h:515
BOTAN_FN_ISA_SIMD_4X32 SIMD_4x32 bswap() const noexcept
Definition simd_4x32.h:544
SIMD_4x32 BOTAN_FN_ISA_SIMD_4X32 shift_elems_left() const noexcept
Definition simd_4x32.h:562
SIMD_4x32 BOTAN_FN_ISA_SIMD_4X32 swap_halves() const
Definition simd_4x32.h:930
#define BOTAN_FORCE_INLINE
Definition compiler.h:86
BOTAN_FORCE_INLINE SIMD_4x32 BOTAN_FN_ISA_SIMD_4X32 mulx_polyval(const SIMD_4x32 &h)
Definition polyval_fn.h:104
BOTAN_FORCE_INLINE BOTAN_FN_ISA_SIMD_4X32 SIMD_4x32 load_block(const uint8_t in[])
Definition polyval_fn.h:87
BOTAN_FORCE_INLINE SIMD_4x32 BOTAN_FN_ISA_CLMUL polyval_multiply(const SIMD_4x32 &H, const SIMD_4x32 &x)
Definition polyval_fn.h:140
BOTAN_FORCE_INLINE SIMD_4x32 BOTAN_FN_ISA_CLMUL polyval_reduce(const SIMD_4x32 &hi, const SIMD_4x32 &lo)
Definition polyval_fn.h:119
BOTAN_FORCE_INLINE BOTAN_FN_ISA_CLMUL SIMD_4x32 clmul(const SIMD_4x32 &H, const SIMD_4x32 &x)
Definition polyval_fn.h:21
BOTAN_FORCE_INLINE BOTAN_FN_ISA_SIMD_4X32 void store_block(const SIMD_4x32 &b, uint8_t out[])
Definition polyval_fn.h:96