Botan 3.14.1
Crypto and TLS for C&
keccak_perm_round.h
Go to the documentation of this file.
1/*
2* (C) 2010,2016,2021 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#ifndef BOTAN_KECCAK_PERM_ROUND_H_
8#define BOTAN_KECCAK_PERM_ROUND_H_
9
10#include <botan/types.h>
11#include <botan/internal/rotate.h>
12#include <concepts>
13
14namespace Botan {
15
16alignas(64) inline constexpr uint64_t KECCAK_RC[24] = {
17 0x0000000000000001, 0x0000000000008082, 0x800000000000808A, 0x8000000080008000, 0x000000000000808B,
18 0x0000000080000001, 0x8000000080008081, 0x8000000000008009, 0x000000000000008A, 0x0000000000000088,
19 0x0000000080008009, 0x000000008000000A, 0x000000008000808B, 0x800000000000008B, 0x8000000000008089,
20 0x8000000000008003, 0x8000000000008002, 0x8000000000000080, 0x000000000000800A, 0x800000008000000A,
21 0x8000000080008081, 0x8000000000008080, 0x0000000080000001, 0x8000000080008008};
22
23/*
24* One Keccak-f[1600] round, on plain uint64_t words or on any of the
25* SIMD wrapper types, computing several states in parallel with one
26* Keccak word per SIMD lane
27*/
28
29template <size_t R, typename W>
31 if constexpr(std::unsigned_integral<W>) {
32 return rotl<R>(v);
33 } else {
34 return v.template rotl<R>();
35 }
36}
37
38template <typename W>
39BOTAN_FORCE_INLINE W keccak_chi(const W& x, const W& y, const W& z) {
40 if constexpr(std::unsigned_integral<W>) {
41 return x ^ (~y & z);
42 } else {
43 return W::chi(x, y, z);
44 }
45}
46
47template <typename W>
49 if constexpr(std::unsigned_integral<W>) {
50 return rc;
51 } else {
52 return W::splat(rc);
53 }
54}
55
56template <typename W>
57BOTAN_FORCE_INLINE void Keccak_Permutation_round(W T[25], const W A[25], uint64_t RC) {
58 const W C0 = A[0] ^ A[5] ^ A[10] ^ A[15] ^ A[20];
59 const W C1 = A[1] ^ A[6] ^ A[11] ^ A[16] ^ A[21];
60 const W C2 = A[2] ^ A[7] ^ A[12] ^ A[17] ^ A[22];
61 const W C3 = A[3] ^ A[8] ^ A[13] ^ A[18] ^ A[23];
62 const W C4 = A[4] ^ A[9] ^ A[14] ^ A[19] ^ A[24];
63
64 const W D0 = keccak_rotl<1>(C0) ^ C3;
65 const W D1 = keccak_rotl<1>(C1) ^ C4;
66 const W D2 = keccak_rotl<1>(C2) ^ C0;
67 const W D3 = keccak_rotl<1>(C3) ^ C1;
68 const W D4 = keccak_rotl<1>(C4) ^ C2;
69
70 const W B00 = A[0] ^ D1;
71 const W B01 = keccak_rotl<44>(A[6] ^ D2);
72 const W B02 = keccak_rotl<43>(A[12] ^ D3);
73 const W B03 = keccak_rotl<21>(A[18] ^ D4);
74 const W B04 = keccak_rotl<14>(A[24] ^ D0);
75 T[0] = keccak_chi(B00, B01, B02) ^ keccak_iota<W>(RC);
76 T[1] = keccak_chi(B01, B02, B03);
77 T[2] = keccak_chi(B02, B03, B04);
78 T[3] = keccak_chi(B03, B04, B00);
79 T[4] = keccak_chi(B04, B00, B01);
80
81 const W B05 = keccak_rotl<28>(A[3] ^ D4);
82 const W B06 = keccak_rotl<20>(A[9] ^ D0);
83 const W B07 = keccak_rotl<3>(A[10] ^ D1);
84 const W B08 = keccak_rotl<45>(A[16] ^ D2);
85 const W B09 = keccak_rotl<61>(A[22] ^ D3);
86 T[5] = keccak_chi(B05, B06, B07);
87 T[6] = keccak_chi(B06, B07, B08);
88 T[7] = keccak_chi(B07, B08, B09);
89 T[8] = keccak_chi(B08, B09, B05);
90 T[9] = keccak_chi(B09, B05, B06);
91
92 const W B10 = keccak_rotl<1>(A[1] ^ D2);
93 const W B11 = keccak_rotl<6>(A[7] ^ D3);
94 const W B12 = keccak_rotl<25>(A[13] ^ D4);
95 const W B13 = keccak_rotl<8>(A[19] ^ D0);
96 const W B14 = keccak_rotl<18>(A[20] ^ D1);
97 T[10] = keccak_chi(B10, B11, B12);
98 T[11] = keccak_chi(B11, B12, B13);
99 T[12] = keccak_chi(B12, B13, B14);
100 T[13] = keccak_chi(B13, B14, B10);
101 T[14] = keccak_chi(B14, B10, B11);
102
103 const W B15 = keccak_rotl<27>(A[4] ^ D0);
104 const W B16 = keccak_rotl<36>(A[5] ^ D1);
105 const W B17 = keccak_rotl<10>(A[11] ^ D2);
106 const W B18 = keccak_rotl<15>(A[17] ^ D3);
107 const W B19 = keccak_rotl<56>(A[23] ^ D4);
108 T[15] = keccak_chi(B15, B16, B17);
109 T[16] = keccak_chi(B16, B17, B18);
110 T[17] = keccak_chi(B17, B18, B19);
111 T[18] = keccak_chi(B18, B19, B15);
112 T[19] = keccak_chi(B19, B15, B16);
113
114 const W B20 = keccak_rotl<62>(A[2] ^ D3);
115 const W B21 = keccak_rotl<55>(A[8] ^ D4);
116 const W B22 = keccak_rotl<39>(A[14] ^ D0);
117 const W B23 = keccak_rotl<41>(A[15] ^ D1);
118 const W B24 = keccak_rotl<2>(A[21] ^ D2);
119 T[20] = keccak_chi(B20, B21, B22);
120 T[21] = keccak_chi(B21, B22, B23);
121 T[22] = keccak_chi(B22, B23, B24);
122 T[23] = keccak_chi(B23, B24, B20);
123 T[24] = keccak_chi(B24, B20, B21);
124}
125
126} // namespace Botan
127
128#endif
#define BOTAN_FORCE_INLINE
Definition compiler.h:86
BOTAN_FORCE_INLINE W keccak_iota(uint64_t rc)
BOTAN_FORCE_INLINE W keccak_rotl(const W &v)
BOTAN_FORCE_INLINE void Keccak_Permutation_round(W T[25], const W A[25], uint64_t RC)
constexpr uint64_t KECCAK_RC[24]
BOTAN_FORCE_INLINE constexpr T rotl(T input)
Definition rotate.h:23
BOTAN_FORCE_INLINE W keccak_chi(const W &x, const W &y, const W &z)