21class WhirlpoolState final {
24 WhirlpoolState() : m_v(_mm512_setzero_si512()) {}
27 explicit WhirlpoolState(__m512i v) : m_v(v) {}
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;
37 static WhirlpoolState load_bytes(
const uint8_t src[64]) {
return WhirlpoolState(_mm512_loadu_si512(src)); }
40 static WhirlpoolState
load_be(
const uint64_t src[8]) {
return WhirlpoolState(_mm512_loadu_si512(src)).bswap(); }
43 void store_be(uint64_t dst[8])
const { _mm512_storeu_si512(dst, bswap().m_v); }
46 inline friend WhirlpoolState
operator^(WhirlpoolState a, WhirlpoolState b) {
47 return WhirlpoolState(_mm512_xor_si512(a.m_v, b.m_v));
51 inline WhirlpoolState&
operator^=(WhirlpoolState other) {
52 m_v = _mm512_xor_si512(m_v, other.m_v);
61 inline WhirlpoolState sub_bytes()
const {
63 _mm512_broadcast_i32x4(_mm_setr_epi8(1, 11, 9, 12, 13, 6, 15, 3, 14, 8, 7, 4, 10, 2, 5, 0));
65 _mm512_broadcast_i32x4(_mm_setr_epi8(15, 0, 13, 7, 11, 14, 5, 10, 9, 2, 12, 1, 3, 4, 8, 6));
67 _mm512_broadcast_i32x4(_mm_setr_epi8(7, 12, 11, 13, 14, 4, 9, 15, 6, 3, 8, 10, 2, 5, 1, 0));
69 const __m512i lo_mask = _mm512_set1_epi8(0x0F);
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);
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));
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));
83 return WhirlpoolState(_mm512_or_si512(_mm512_slli_epi16(out_hi, 4), _mm512_and_si512(out_lo, lo_mask)));
93 inline WhirlpoolState shift_columns()
const {
96 alignas(64)
static constexpr uint8_t perm[64] = {
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,
108 return WhirlpoolState(_mm512_permutexvar_epi8(_mm512_load_si512(perm), m_v));
118 inline WhirlpoolState mix_rows()
const {
127 _mm512_broadcast_i32x4(_mm_setr_epi8(7, 0, 1, 2, 3, 4, 5, 6, 15, 8, 9, 10, 11, 12, 13, 14));
129 _mm512_broadcast_i32x4(_mm_setr_epi8(6, 7, 0, 1, 2, 3, 4, 5, 14, 15, 8, 9, 10, 11, 12, 13));
131 _mm512_broadcast_i32x4(_mm_setr_epi8(5, 6, 7, 0, 1, 2, 3, 4, 13, 14, 15, 8, 9, 10, 11, 12));
133 _mm512_broadcast_i32x4(_mm_setr_epi8(4, 5, 6, 7, 0, 1, 2, 3, 12, 13, 14, 15, 8, 9, 10, 11));
135 _mm512_broadcast_i32x4(_mm_setr_epi8(3, 4, 5, 6, 7, 0, 1, 2, 11, 12, 13, 14, 15, 8, 9, 10));
137 _mm512_broadcast_i32x4(_mm_setr_epi8(2, 3, 4, 5, 6, 7, 0, 1, 10, 11, 12, 13, 14, 15, 8, 9));
139 _mm512_broadcast_i32x4(_mm_setr_epi8(1, 2, 3, 4, 5, 6, 7, 0, 9, 10, 11, 12, 13, 14, 15, 8));
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);
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));
152 return WhirlpoolState(_mm512_xor_si512(_mm512_xor_si512(t01, t23), _mm512_xor_si512(t45, t67)));
159 inline WhirlpoolState round()
const {
return sub_bytes().shift_columns().mix_rows(); }
163 static inline WhirlpoolState rc(uint64_t v) {
return WhirlpoolState(_mm512_set_epi64(0, 0, 0, 0, 0, 0, 0, v)); }
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));
170 return WhirlpoolState(_mm512_shuffle_epi8(m_v, tbl));
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);
179 return _mm512_mask_blend_epi8(top_bits, shifted, _mm512_xor_si512(shifted, poly));