native 0.0.1
Vectors, masks and wide register packs for C++26
Loading...
Searching...
No Matches
aes_constant.h
1// SPDX-License-Identifier: BSD-2-Clause OR Apache-2.0
2#pragma once
3
5#include "native/x86/detail/constant_lanes.h"
6
7// Intel AES round ordering: the round key is added after substitution,
8// shifting and (except in the last round) mixing. ARM AESE/AESD add it first.
9namespace native::detail::x86_aes_constant {
10 using aes_constant::field_inverse;
11 using aes_constant::field_product;
12 using aes_constant::substitute;
13 using aes_constant::mix;
14 using x86_constant::lanes;
15
16 template<bool Inverse, bool Last, class V>
17 constexpr V round(V state, V key) noexcept {
18 // Every 128-bit lane is a separate AES state with its own round key.
19 static_assert(V::lanes % 16 == 0);
20 auto input = lanes(state);
21 auto result = input;
22 auto round_key = lanes(key);
23 for (unsigned base = 0; base < V::lanes; base += 16) {
24 std::array<std::uint8_t, 16> block{};
25 for (unsigned i = 0; i < 16; ++i) block[i] = input[base + i];
26 block = aes_constant::shift_substitute<Inverse>(block);
27 if constexpr (!Last) block = mix<Inverse>(block);
28 for (unsigned i = 0; i < 16; ++i) result[base + i] = block[i] ^ round_key[base + i];
29 }
30 return V::load(result.data());
31 }
32
33 template<class V>
34 constexpr V inverse_mix(V state) noexcept {
35 auto result = mix<true>(lanes(state));
36 return V::load(result.data());
37 }
38
39 template<unsigned Imm8, class V>
40 constexpr V keygen(V state) noexcept {
41 auto input = lanes(state);
42 auto result = input;
43 for (unsigned half = 0; half < 2; ++half) {
44 auto source = 8 * half + 4;
45 for (unsigned i = 0; i < 4; ++i) {
46 result[8 * half + i] = substitute<false>(input[source + i]);
47 result[8 * half + 4 + i] = substitute<false>(input[source + (i + 1) % 4]);
48 }
49 result[8 * half + 4] ^= Imm8;
50 }
51 return V::load(result.data());
52 }
53}
Shared AES byte operations for constant evaluation on ARM and x86.