Skip to content

Commit fc4994c

Browse files
authored
Merge pull request #1560 from evoskuil/master
Single block shani.
2 parents 5480d11 + 3182c22 commit fc4994c

7 files changed

Lines changed: 313 additions & 134 deletions

File tree

include/bitcoin/system/hash/sha/algorithm.hpp

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ class algorithm
166166
template <typename xWord, if_extended<xWord> = true>
167167
using wstate_t = std_array<xWord, sizeof(state_t) / sizeof(xWord)>;
168168

169+
////template <typename xWord, if_extended<xWord> = true>
170+
////using wblock_t = std_array<xWord, sizeof(block_t) / sizeof(xWord)>;
171+
169172
/// Other types.
170173
/// -----------------------------------------------------------------------
171174

@@ -236,17 +239,23 @@ class algorithm
236239
/// Padding.
237240
/// -----------------------------------------------------------------------
238241

242+
/// Scheduled padding (new and existing buffer objects).
239243
template <size_t Blocks>
240244
static CONSTEVAL buffer_t scheduled_pad() NOEXCEPT;
241-
static CONSTEVAL chunk_t chunk_pad() NOEXCEPT;
242-
static CONSTEVAL pad_t stream_pad() NOEXCEPT;
243-
244245
template <size_t Blocks>
245246
static constexpr void schedule_n(buffer_t& buffer) NOEXCEPT;
246247
static constexpr void schedule_n(buffer_t& buffer, size_t blocks) NOEXCEPT;
247248
static constexpr void schedule_1(buffer_t& buffer) NOEXCEPT;
248-
static constexpr void pad_half(buffer_t& buffer) NOEXCEPT;
249-
static constexpr void pad_n(buffer_t& buffer, count_t blocks) NOEXCEPT;
249+
250+
/// Unscheduled padding (new objects).
251+
static words_t pad_block() NOEXCEPT;
252+
static words_t pad_blocks(count_t blocks) NOEXCEPT;
253+
static CONSTEVAL chunk_t chunk_pad() NOEXCEPT;
254+
static CONSTEVAL pad_t stream_pad() NOEXCEPT;
255+
256+
/// Unscheduled padding (update block or buffer object).
257+
static constexpr void pad_half(auto& buffer) NOEXCEPT;
258+
static constexpr void pad_n(auto& buffer, count_t blocks) NOEXCEPT;
250259

251260
/// Double hashing.
252261
/// -----------------------------------------------------------------------
@@ -363,6 +372,8 @@ class algorithm
363372
/// Native SHA optimizations (single blocks).
364373
/// -----------------------------------------------------------------------
365374

375+
template <bool Swap>
376+
INLINE static xint128_t bytes(xint128_t message) NOEXCEPT;
366377
INLINE static void shuffle(xint128_t& state0, xint128_t& state1) NOEXCEPT;
367378
INLINE static void unshuffle(xint128_t& state0, xint128_t& state1) NOEXCEPT;
368379
INLINE static void prepare(xint128_t& message0, xint128_t message1) NOEXCEPT;
@@ -373,7 +384,14 @@ class algorithm
373384
INLINE static void round_4(xint128_t& state0, xint128_t& state1,
374385
xint128_t message) NOEXCEPT;
375386

376-
static void native_rounds(state_t& state, iblocks_t& blocks) NOEXCEPT;
387+
template <bool Swap>
388+
INLINE static void native_rounds(xint128_t& lo, xint128_t& hi,
389+
const block_t& block) NOEXCEPT;
390+
391+
static void native_(state_t& state, iblocks_t& blocks) NOEXCEPT;
392+
static void native_(state_t& state, const block_t& block) NOEXCEPT;
393+
INLINE static void native_preswapped(state_t& state,
394+
const words_t& block) NOEXCEPT;
377395

378396
public:
379397
/// Summary public values.

include/bitcoin/system/impl/hash/sha/algorithm_double.ipp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ double_hash(const ablocks_t<Size>& blocks) NOEXCEPT
6565
{
6666
static_assert(is_same_type<state_t, chunk_t>);
6767

68-
buffer_t buffer{};
6968
auto state = H::get;
7069
iterate(state, blocks);
70+
71+
buffer_t buffer{};
7172
schedule_n<Size>(buffer);
7273
compress(state, buffer);
7374

@@ -77,6 +78,7 @@ double_hash(const ablocks_t<Size>& blocks) NOEXCEPT
7778
schedule(buffer);
7879
state = H::get;
7980
compress(state, buffer);
81+
8082
return output(state);
8183
}
8284

@@ -89,9 +91,10 @@ double_hash(iblocks_t&& blocks) NOEXCEPT
8991
// Save block count, as iterable decrements.
9092
const auto count = blocks.size();
9193

92-
buffer_t buffer{};
9394
auto state = H::get;
9495
iterate(state, blocks);
96+
97+
buffer_t buffer{};
9598
schedule_n(buffer, count);
9699
compress(state, buffer);
97100

@@ -101,6 +104,7 @@ double_hash(iblocks_t&& blocks) NOEXCEPT
101104
schedule(buffer);
102105
state = H::get;
103106
compress(state, buffer);
107+
104108
return output(state);
105109
}
106110

@@ -110,9 +114,9 @@ double_hash(const block_t& block) NOEXCEPT
110114
{
111115
static_assert(is_same_type<state_t, chunk_t>);
112116

113-
buffer_t buffer{};
114-
115117
auto state = H::get;
118+
119+
buffer_t buffer{};
116120
input(buffer, block);
117121
schedule(buffer);
118122
compress(state, buffer);
@@ -125,6 +129,7 @@ double_hash(const block_t& block) NOEXCEPT
125129
schedule(buffer);
126130
state = H::get;
127131
compress(state, buffer);
132+
128133
return output(state);
129134
}
130135

@@ -134,8 +139,9 @@ double_hash(const half_t& half) NOEXCEPT
134139
{
135140
static_assert(is_same_type<state_t, chunk_t>);
136141

137-
buffer_t buffer{};
138142
auto state = H::get;
143+
144+
buffer_t buffer{};
139145
input_left(buffer, half);
140146
pad_half(buffer);
141147
schedule(buffer);
@@ -147,6 +153,7 @@ double_hash(const half_t& half) NOEXCEPT
147153
schedule(buffer);
148154
state = H::get;
149155
compress(state, buffer);
156+
150157
return output(state);
151158
}
152159

@@ -156,8 +163,9 @@ double_hash(const half_t& left, const half_t& right) NOEXCEPT
156163
{
157164
static_assert(is_same_type<state_t, chunk_t>);
158165

159-
buffer_t buffer{};
160166
auto state = H::get;
167+
168+
buffer_t buffer{};
161169
input_left(buffer, left);
162170
input_right(buffer, right);
163171
schedule(buffer);
@@ -171,6 +179,7 @@ double_hash(const half_t& left, const half_t& right) NOEXCEPT
171179
schedule(buffer);
172180
state = H::get;
173181
compress(state, buffer);
182+
174183
return output(state);
175184
}
176185

include/bitcoin/system/impl/hash/sha/algorithm_iterate.ipp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,19 +243,19 @@ iterate_vector(state_t& state, const ablocks_t<Size>& blocks) NOEXCEPT
243243
// intel-sha-extensions-white-paper-402097.pdf
244244

245245
TEMPLATE
246+
template <size_t Size>
246247
INLINE void CLASS::
247-
iterate_native(state_t& state, iblocks_t& blocks) NOEXCEPT
248+
iterate_native(state_t& state, const ablocks_t<Size>& blocks) NOEXCEPT
248249
{
249-
native_rounds(state, blocks);
250+
iblocks_t iblocks{ array_cast<byte_t>(blocks) };
251+
native_(state, iblocks);
250252
}
251253

252254
TEMPLATE
253-
template <size_t Size>
254255
INLINE void CLASS::
255-
iterate_native(state_t& state, const ablocks_t<Size>& blocks) NOEXCEPT
256+
iterate_native(state_t& state, iblocks_t& blocks) NOEXCEPT
256257
{
257-
iblocks_t iblocks{ array_cast<byte_t>(blocks) };
258-
native_rounds(state, iblocks);
258+
native_(state, blocks);
259259
}
260260

261261
// Dispatch and normal forms.

0 commit comments

Comments
 (0)