Skip to content

Commit

Permalink
Merge pull request #1560 from evoskuil/master
Browse files Browse the repository at this point in the history
Single block shani.
  • Loading branch information
evoskuil authored Dec 6, 2024
2 parents 5480d11 + 3182c22 commit fc4994c
Show file tree
Hide file tree
Showing 7 changed files with 313 additions and 134 deletions.
30 changes: 24 additions & 6 deletions include/bitcoin/system/hash/sha/algorithm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ class algorithm
template <typename xWord, if_extended<xWord> = true>
using wstate_t = std_array<xWord, sizeof(state_t) / sizeof(xWord)>;

////template <typename xWord, if_extended<xWord> = true>
////using wblock_t = std_array<xWord, sizeof(block_t) / sizeof(xWord)>;

/// Other types.
/// -----------------------------------------------------------------------

Expand Down Expand Up @@ -236,17 +239,23 @@ class algorithm
/// Padding.
/// -----------------------------------------------------------------------

/// Scheduled padding (new and existing buffer objects).
template <size_t Blocks>
static CONSTEVAL buffer_t scheduled_pad() NOEXCEPT;
static CONSTEVAL chunk_t chunk_pad() NOEXCEPT;
static CONSTEVAL pad_t stream_pad() NOEXCEPT;

template <size_t Blocks>
static constexpr void schedule_n(buffer_t& buffer) NOEXCEPT;
static constexpr void schedule_n(buffer_t& buffer, size_t blocks) NOEXCEPT;
static constexpr void schedule_1(buffer_t& buffer) NOEXCEPT;
static constexpr void pad_half(buffer_t& buffer) NOEXCEPT;
static constexpr void pad_n(buffer_t& buffer, count_t blocks) NOEXCEPT;

/// Unscheduled padding (new objects).
static words_t pad_block() NOEXCEPT;
static words_t pad_blocks(count_t blocks) NOEXCEPT;
static CONSTEVAL chunk_t chunk_pad() NOEXCEPT;
static CONSTEVAL pad_t stream_pad() NOEXCEPT;

/// Unscheduled padding (update block or buffer object).
static constexpr void pad_half(auto& buffer) NOEXCEPT;
static constexpr void pad_n(auto& buffer, count_t blocks) NOEXCEPT;

/// Double hashing.
/// -----------------------------------------------------------------------
Expand Down Expand Up @@ -363,6 +372,8 @@ class algorithm
/// Native SHA optimizations (single blocks).
/// -----------------------------------------------------------------------

template <bool Swap>
INLINE static xint128_t bytes(xint128_t message) NOEXCEPT;
INLINE static void shuffle(xint128_t& state0, xint128_t& state1) NOEXCEPT;
INLINE static void unshuffle(xint128_t& state0, xint128_t& state1) NOEXCEPT;
INLINE static void prepare(xint128_t& message0, xint128_t message1) NOEXCEPT;
Expand All @@ -373,7 +384,14 @@ class algorithm
INLINE static void round_4(xint128_t& state0, xint128_t& state1,
xint128_t message) NOEXCEPT;

static void native_rounds(state_t& state, iblocks_t& blocks) NOEXCEPT;
template <bool Swap>
INLINE static void native_rounds(xint128_t& lo, xint128_t& hi,
const block_t& block) NOEXCEPT;

static void native_(state_t& state, iblocks_t& blocks) NOEXCEPT;
static void native_(state_t& state, const block_t& block) NOEXCEPT;
INLINE static void native_preswapped(state_t& state,
const words_t& block) NOEXCEPT;

public:
/// Summary public values.
Expand Down
21 changes: 15 additions & 6 deletions include/bitcoin/system/impl/hash/sha/algorithm_double.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ double_hash(const ablocks_t<Size>& blocks) NOEXCEPT
{
static_assert(is_same_type<state_t, chunk_t>);

buffer_t buffer{};
auto state = H::get;
iterate(state, blocks);

buffer_t buffer{};
schedule_n<Size>(buffer);
compress(state, buffer);

Expand All @@ -77,6 +78,7 @@ double_hash(const ablocks_t<Size>& blocks) NOEXCEPT
schedule(buffer);
state = H::get;
compress(state, buffer);

return output(state);
}

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

buffer_t buffer{};
auto state = H::get;
iterate(state, blocks);

buffer_t buffer{};
schedule_n(buffer, count);
compress(state, buffer);

Expand All @@ -101,6 +104,7 @@ double_hash(iblocks_t&& blocks) NOEXCEPT
schedule(buffer);
state = H::get;
compress(state, buffer);

return output(state);
}

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

buffer_t buffer{};

auto state = H::get;

buffer_t buffer{};
input(buffer, block);
schedule(buffer);
compress(state, buffer);
Expand All @@ -125,6 +129,7 @@ double_hash(const block_t& block) NOEXCEPT
schedule(buffer);
state = H::get;
compress(state, buffer);

return output(state);
}

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

buffer_t buffer{};
auto state = H::get;

buffer_t buffer{};
input_left(buffer, half);
pad_half(buffer);
schedule(buffer);
Expand All @@ -147,6 +153,7 @@ double_hash(const half_t& half) NOEXCEPT
schedule(buffer);
state = H::get;
compress(state, buffer);

return output(state);
}

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

buffer_t buffer{};
auto state = H::get;

buffer_t buffer{};
input_left(buffer, left);
input_right(buffer, right);
schedule(buffer);
Expand All @@ -171,6 +179,7 @@ double_hash(const half_t& left, const half_t& right) NOEXCEPT
schedule(buffer);
state = H::get;
compress(state, buffer);

return output(state);
}

Expand Down
12 changes: 6 additions & 6 deletions include/bitcoin/system/impl/hash/sha/algorithm_iterate.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -243,19 +243,19 @@ iterate_vector(state_t& state, const ablocks_t<Size>& blocks) NOEXCEPT
// intel-sha-extensions-white-paper-402097.pdf

TEMPLATE
template <size_t Size>
INLINE void CLASS::
iterate_native(state_t& state, iblocks_t& blocks) NOEXCEPT
iterate_native(state_t& state, const ablocks_t<Size>& blocks) NOEXCEPT
{
native_rounds(state, blocks);
iblocks_t iblocks{ array_cast<byte_t>(blocks) };
native_(state, iblocks);
}

TEMPLATE
template <size_t Size>
INLINE void CLASS::
iterate_native(state_t& state, const ablocks_t<Size>& blocks) NOEXCEPT
iterate_native(state_t& state, iblocks_t& blocks) NOEXCEPT
{
iblocks_t iblocks{ array_cast<byte_t>(blocks) };
native_rounds(state, iblocks);
native_(state, blocks);
}

// Dispatch and normal forms.
Expand Down
Loading

0 comments on commit fc4994c

Please sign in to comment.