/
keccak.hpp
261 lines (230 loc) · 7.21 KB
/
keccak.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Copyright © 2023 ZeroPass <zeropass@pm.me>
// Author: Crt Vavros
#pragma once
#include <array>
#include <cstdint>
#include <vector>
#include <ack/types.hpp>
namespace ack {
namespace internal_do_not_use {
// Implementation in this scope was taken from:
// https://github.com/XKCP/XKCP/blob/8f447eb59d43fef72297f5f1560e2cefea093b32/Standalone/CompactFIPS202/C/TweetableFIPS202.c
typedef unsigned long long U;
typedef byte_t C;
inline constexpr U rot( U a, U n ) {
return a << n ^ a >> ( 64 - n );
}
constexpr void F(U *s)
{
C R = 1;
U t = 0;
U B[5] = { 0 };
for( U n = 0; n < 24; n++ ) {
for( U i = 0; i < 5; i++ ) {
B[i] = 0;
for( U j = 0; j < 5; j++ ) {
B[i] ^= s[ i + 5 * j ];
}
}
for( U i = 0; i < 5; i++ ) {
t = B[ (i + 4) % 5 ] ^ rot( B[ (i + 1) % 5 ], 1 );
for( U j = 0; j < 5; j++ ) {
s[ i + 5 * j ] ^= t;
}
}
t = s[1];
C x = 1;
C y = 0;
C r = 0;
U Y = 0;
for( U j = 0; j < 24; j++ ) {
r += j + 1;
Y = 2 * x + 3 * y;
x = y;
y = Y % 5;
Y = s[ x + 5 * y ];
s[ x + 5 * y ] = rot( t, r % 64 );
t = Y;
}
for( U j = 0; j < 5; j++ ) {
for( U i = 0; i < 5; i++ ) {
B[i] = s[ i + 5 * j ];
}
for( U i = 0; i < 5; i++ ) {
s[ i + 5 * j ] = B[i] ^ ( ~B[ (i + 1) % 5 ] & B[ (i + 2) % 5 ] );
}
}
for( U j = 0; j < 7; j++ ) {
if( ( R = (R << 1) ^ ( 113 * (R >> 7) ) ) & 2 ) {
*s ^= 1ULL << ( (1 << j) - 1 );
}
}
}
}
template<C r, C p>
constexpr void keccak(const C* m, U n, C* h, U d)
{
U s[25] = { 0 };
C t[200] = { 0 };
while ( n >= r ) {
for( U i = 0; i < r; i++ ) {
s[ i/8 ] ^= static_cast<U>( m[i] ) << i % 8 * 8;
}
F(s);
n -= r;
m += r;
}
for( U i = 0; i < n; i++ ) {
t[i] = m[i];
}
t[n] = p;
t[r-1] |= 128;
for( U i = 0; i < r; i++ ) {
s[i/8] ^= static_cast<U>( t[i] ) << i % 8 * 8;
}
for( U i = 0; i < d; i++ ) {
if(0 == i % r) {
F(s);
}
h[i] = s[ i % r/8 ] >> 8 * (i % 8);
}
}
} // internal_use_do_not_use
/**
* SHA3-256 hash function.
* @note Implementation parameters are based on the NIST FIPS 202 standard.
* https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.202.pdf
*
* @param data - data to hash
* @return 256-bit hash
*/
[[nodiscard]] inline hash256 sha3_256(const bytes_view data)
{
std::array<byte_t, 32> h;
internal_do_not_use::keccak<17 * 8, 6>(
reinterpret_cast<const internal_do_not_use::C*>( data.data() ),
data.size(),
h.data(),
h.size()
);
return h;
}
/**
* SHA3-384 hash function.
* @note Implementation parameters are based on the NIST FIPS 202 standard.
* https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.202.pdf
*
* @param data - data to hash
* @return 384-bit hash
*/
[[nodiscard]] inline hash384 sha3_384(const bytes_view data)
{
std::array<byte_t, 48> h;
internal_do_not_use::keccak<13 * 8, 6>(
reinterpret_cast<const internal_do_not_use::C*>( data.data() ),
data.size(),
h.data(),
h.size()
);
return h;
}
/**
* SHA3-512 hash function.
* @note Implementation parameters are based on the NIST FIPS 202 standard.
* https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.202.pdf
*
* @param data - data to hash
* @return 512-bit hash
*/
[[nodiscard]] inline hash512 sha3_512(const bytes_view data)
{
std::array<byte_t, 64> h;
internal_do_not_use::keccak<9 * 8, 6>(
reinterpret_cast<const internal_do_not_use::C*>( data.data() ),
data.size(),
h.data(),
h.size()
);
return h;
}
/**
* SHAKE-128 hash function.
* @tparam - Size - output hash size in bytes.
* @param data - data to hash
* @return Size long hash
*/
template<std::size_t Size>
[[nodiscard]] constexpr inline hash_t<Size> shake128_fixed(const bytes_view data)
{
using hash = hash_t<Size>;
using word_t = typename hash::word_t;
static_assert( Size % sizeof(word_t) == 0,
"the size of digest is not divisible by the size of word"
);
fixed_bytes<Size> h;
internal_do_not_use::keccak<21 * 8, 31>(
reinterpret_cast<const internal_do_not_use::C*>( data.data() ),
data.size(),
h.data(),
Size
);
return h;
}
/**
* SHAKE-128 hash function.
* @param data - data to hash
* @param hash_len - output hash length
* @return hash_len long hash
*/
[[nodiscard]] inline bytes shake128(const bytes_view data, std::size_t hash_len)
{
bytes h( hash_len );
internal_do_not_use::keccak<21 * 8, 31>(
reinterpret_cast<const internal_do_not_use::C*>( data.data() ),
data.size(),
h.data(),
h.size()
);
return h;
}
/**
* SHAKE-256 hash function.
* @tparam - Size - output hash size in bytes.
* @param data - data to hash
* @return Size long hash
*/
template<std::size_t Size>
[[nodiscard]] constexpr inline hash_t<Size> shake256_fixed(const bytes_view data)
{
using hash = hash_t<Size>;
using word_t = typename hash::word_t;
static_assert( Size % sizeof(word_t) == 0,
"the size of digest is not divisible by the size of word"
);
fixed_bytes<Size> h;
internal_do_not_use::keccak<17 * 8, 31>(
reinterpret_cast<const internal_do_not_use::C*>( data.data() ),
data.size(),
h.data(),
h.size()
);
return h;
}
/**
* SHAKE-256 hash function.
* @param data - data to hash
* @param hash_len - output hash length
* @return hash_len long hash
*/
[[nodiscard]] inline bytes shake256(const bytes_view data, std::size_t out_len)
{
bytes h( out_len );
internal_do_not_use::keccak<17 * 8, 31>(
data.data(),
data.size(),
h.data(),
h.size()
);
return h;
}
}