5
5
#include < array>
6
6
#include < cstdint>
7
7
#include < cstring>
8
+ #include < limits>
8
9
#include < span>
9
10
10
11
// Keccak family of sponge functions
@@ -21,7 +22,7 @@ namespace sponge {
21
22
constexpr bool
22
23
check_domain_separator (const size_t dom_sep_bit_len)
23
24
{
24
- return (dom_sep_bit_len == 2 ) | (dom_sep_bit_len == 4 );
25
+ return (dom_sep_bit_len == 2u ) | (dom_sep_bit_len == 4u );
25
26
}
26
27
27
28
// Pad10*1 - generates a padding, while also considering domain separator bits (
@@ -37,17 +38,18 @@ check_domain_separator(const size_t dom_sep_bit_len)
37
38
// This function implementation collects motivation from
38
39
// https://github.com/itzmeanjan/turboshake/blob/e1a6b950/src/sponge.rs#L70-L72
39
40
template <uint8_t domain_separator, size_t ds_bits, size_t rate>
40
- static inline constexpr std::array<uint8_t , rate / 8 >
41
+ static inline constexpr std::array<uint8_t ,
42
+ rate / std::numeric_limits<uint8_t >::digits>
41
43
pad10x1 (const size_t offset)
42
44
requires (check_domain_separator(ds_bits))
43
45
{
44
- std::array<uint8_t , rate / 8 > res{};
46
+ std::array<uint8_t , rate / std::numeric_limits< uint8_t >::digits > res{};
45
47
46
- constexpr uint8_t mask = (1 << ds_bits) - 1 ;
47
- constexpr uint8_t pad_byte = (1 << ds_bits) | (domain_separator & mask);
48
+ constexpr uint8_t mask = (1u << ds_bits) - 1u ;
49
+ constexpr uint8_t pad_byte = (1u << ds_bits) | (domain_separator & mask);
48
50
49
51
res[offset] = pad_byte;
50
- res[(rate / 8 ) - 1 ] ^= 0x80 ;
52
+ res[res. size ( ) - 1 ] ^= 0x80u ;
51
53
52
54
return res;
53
55
}
@@ -67,14 +69,14 @@ absorb(uint64_t state[keccak::LANE_CNT],
67
69
size_t & offset,
68
70
std::span<const uint8_t > msg)
69
71
{
70
- constexpr size_t rbytes = rate >> 3 ; // # -of bytes
71
- constexpr size_t rwords = rbytes >> 3 ; // # -of 64 -bit words
72
+ constexpr size_t rbytes = rate >> 3u ; // # -of bytes
73
+ constexpr size_t rwords = rbytes >> 3u ; // # -of 64 -bit words
72
74
73
75
std::array<uint8_t , rbytes> blk_bytes{};
74
76
std::array<uint64_t , rwords> blk_words{};
75
77
76
- auto _blk_bytes = std::span (blk_bytes);
77
- auto _blk_words = std::span (blk_words);
78
+ auto blk_bytes_span = std::span (blk_bytes);
79
+ auto blk_words_span = std::span (blk_words);
78
80
79
81
const size_t mlen = msg.size ();
80
82
const size_t blk_cnt = (offset + mlen) / rbytes;
@@ -84,14 +86,14 @@ absorb(uint64_t state[keccak::LANE_CNT],
84
86
for (size_t i = 0 ; i < blk_cnt; i++) {
85
87
const size_t readable = rbytes - offset;
86
88
87
- auto _msg = msg.subspan (moff, readable);
88
- auto _blk = _blk_bytes .subspan (offset, readable);
89
+ auto msg_span = msg.subspan (moff, readable);
90
+ auto blk_span = blk_bytes_span .subspan (offset, readable);
89
91
90
- std::copy (_msg .begin (), _msg .end (), _blk .begin ());
91
- sha3_utils::le_bytes_to_u64_words<rate>(_blk_bytes, _blk_words );
92
+ std::copy (msg_span .begin (), msg_span .end (), blk_span .begin ());
93
+ sha3_utils::le_bytes_to_u64_words<rate>(blk_bytes_span, blk_words_span );
92
94
93
95
for (size_t j = 0 ; j < rwords; j++) {
94
- state[j] ^= _blk_words [j];
96
+ state[j] ^= blk_words_span [j];
95
97
}
96
98
97
99
keccak::permute (state);
@@ -102,15 +104,15 @@ absorb(uint64_t state[keccak::LANE_CNT],
102
104
103
105
const size_t rm_bytes = mlen - moff;
104
106
105
- auto _msg = msg.subspan (moff, rm_bytes);
106
- auto _blk = _blk_bytes .subspan (offset, rm_bytes);
107
+ auto msg_span = msg.subspan (moff, rm_bytes);
108
+ auto blk_span = blk_bytes_span .subspan (offset, rm_bytes);
107
109
108
110
blk_bytes.fill (0x00 );
109
- std::copy (_msg .begin (), _msg .end (), _blk .begin ());
110
- sha3_utils::le_bytes_to_u64_words<rate>(_blk_bytes, _blk_words );
111
+ std::copy (msg_span .begin (), msg_span .end (), blk_span .begin ());
112
+ sha3_utils::le_bytes_to_u64_words<rate>(blk_bytes_span, blk_words_span );
111
113
112
114
for (size_t j = 0 ; j < rwords; j++) {
113
- state[j] ^= _blk_words [j];
115
+ state[j] ^= blk_words_span [j];
114
116
}
115
117
116
118
offset += rm_bytes;
@@ -132,19 +134,19 @@ static inline constexpr void
132
134
finalize (uint64_t state[keccak::LANE_CNT], size_t & offset)
133
135
requires (check_domain_separator(ds_bits))
134
136
{
135
- constexpr size_t rbytes = rate >> 3 ; // # -of bytes
136
- constexpr size_t rwords = rbytes >> 3 ; // # -of 64 -bit words
137
+ constexpr size_t rbytes = rate >> 3u ; // # -of bytes
138
+ constexpr size_t rwords = rbytes >> 3u ; // # -of 64 -bit words
137
139
138
140
const auto padb = pad10x1<domain_separator, ds_bits, rate>(offset);
139
141
std::array<uint64_t , rwords> padw{};
140
142
141
- auto _padb = std::span (padb);
142
- auto _padw = std::span (padw);
143
+ auto padb_span = std::span (padb);
144
+ auto padw_span = std::span (padw);
143
145
144
- sha3_utils::le_bytes_to_u64_words<rate>(_padb, _padw );
146
+ sha3_utils::le_bytes_to_u64_words<rate>(padb_span, padw_span );
145
147
146
148
for (size_t j = 0 ; j < rwords; j++) {
147
- state[j] ^= _padw [j];
149
+ state[j] ^= padw_span [j];
148
150
}
149
151
150
152
keccak::permute (state);
@@ -168,14 +170,14 @@ squeeze(uint64_t state[keccak::LANE_CNT],
168
170
size_t & squeezable,
169
171
std::span<uint8_t > out)
170
172
{
171
- constexpr size_t rbytes = rate >> 3 ; // # -of bytes
172
- constexpr size_t rwords = rbytes >> 3 ; // # -of 64 -bit words
173
+ constexpr size_t rbytes = rate >> 3u ; // # -of bytes
174
+ constexpr size_t rwords = rbytes >> 3u ; // # -of 64 -bit words
173
175
174
176
std::array<uint8_t , rbytes> blk_bytes{};
175
- auto _blk_bytes = std::span (blk_bytes);
177
+ auto blk_bytes_span = std::span (blk_bytes);
176
178
177
179
auto swords = std::span{ state, keccak::LANE_CNT };
178
- auto _swords = swords.template subspan <0 , rwords>();
180
+ auto swords_span = swords.template subspan <0 , rwords>();
179
181
180
182
const size_t olen = out.size ();
181
183
size_t off = 0 ;
@@ -184,12 +186,12 @@ squeeze(uint64_t state[keccak::LANE_CNT],
184
186
const size_t read = std::min (squeezable, olen - off);
185
187
const size_t soff = rbytes - squeezable;
186
188
187
- sha3_utils::u64_words_to_le_bytes<rate>(_swords, _blk_bytes );
189
+ sha3_utils::u64_words_to_le_bytes<rate>(swords_span, blk_bytes_span );
188
190
189
- auto _blk = _blk_bytes .subspan (soff, read);
190
- auto _out = out.subspan (off, read);
191
+ auto blk_span = blk_bytes_span .subspan (soff, read);
192
+ auto out_span = out.subspan (off, read);
191
193
192
- std::copy (_blk .begin (), _blk .end (), _out .begin ());
194
+ std::copy (blk_span .begin (), blk_span .end (), out_span .begin ());
193
195
194
196
squeezable -= read;
195
197
off += read;
0 commit comments