Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 32 additions & 20 deletions lib/evmone_precompiles/ripemd160.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,39 @@ constexpr size_t N = R * B; ///< Number of steps.

using State = std::array<uint32_t, RIPEMD160_HASH_SIZE / sizeof(uint32_t)>;

using BinaryFunction = uint32_t (*)(uint32_t, uint32_t, uint32_t) noexcept;
/// Boolean function, numbered by the round index.
template <std::size_t I>
uint32_t boolean_fn(uint32_t x, uint32_t y, uint32_t z) noexcept = delete;

// TODO: Functions from the array of function pointers are not inlined by GCC:
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114452
// TODO(C++23): Mark these as [[always_inline]]
constexpr BinaryFunction binary_functions[R] = {
// f₁(x, y, z) = x ⊕ y ⊕ z
[](uint32_t x, uint32_t y, uint32_t z) noexcept { return x ^ y ^ z; },
template <> // f₁(x, y, z) = x ⊕ y ⊕ z
[[gnu::always_inline]] inline uint32_t boolean_fn<0>(uint32_t x, uint32_t y, uint32_t z) noexcept
{
return x ^ y ^ z;
}

// f₂(x, y, z) = (x ∧ y) ∨ (¬x ∧ z) ⇔ ((y ⊕ z) ∧ x) ⊕ z
[](uint32_t x, uint32_t y, uint32_t z) noexcept { return ((y ^ z) & x) ^ z; },
template <> // f₂(x, y, z) = (x ∧ y) ∨ (¬x ∧ z)
[[gnu::always_inline]] inline uint32_t boolean_fn<1>(uint32_t x, uint32_t y, uint32_t z) noexcept
{
return ((y ^ z) & x) ^ z;
}

// f₃(x, y, z) = (x ∨ ¬y) ⊕ z
[](uint32_t x, uint32_t y, uint32_t z) noexcept { return (x | ~y) ^ z; },
template <> // f₃(x, y, z) = (x ∨ ¬y) ⊕ z
[[gnu::always_inline]] inline uint32_t boolean_fn<2>(uint32_t x, uint32_t y, uint32_t z) noexcept
{
return (x | ~y) ^ z;
}

// f₄(x, y, z) = (x ∧ z) ∨ (y ∧ ¬z) ⇔ ((x ⊕ y) ∧ z) ⊕ y
[](uint32_t x, uint32_t y, uint32_t z) noexcept { return ((x ^ y) & z) ^ y; },
template <> // f₄(x, y, z) = (x ∧ z) ∨ (y ∧ ¬z)
[[gnu::always_inline]] inline uint32_t boolean_fn<3>(uint32_t x, uint32_t y, uint32_t z) noexcept
{
return ((x ^ y) & z) ^ y;
}

// f₅(x, y, z) = x ⊕ (y ∨ ¬z)
[](uint32_t x, uint32_t y, uint32_t z) noexcept { return x ^ (y | ~z); },
};
template <> // f₅(x, y, z) = x ⊕ (y ∨ ¬z)
[[gnu::always_inline]] inline uint32_t boolean_fn<4>(uint32_t x, uint32_t y, uint32_t z) noexcept
{
return x ^ (y | ~z);
}

/// Added constants.
constexpr uint32_t constants[L][R] = {
Expand Down Expand Up @@ -110,14 +122,12 @@ constexpr int rotate_amount[L][N] = {
};

template <size_t J>
inline void step(State z[L], const std::byte* block) noexcept
[[gnu::always_inline]] inline void step(State z[L], const std::byte* block) noexcept
{
static constexpr auto I = J / B; // round index
static constexpr BinaryFunction fs[]{binary_functions[I], binary_functions[R - 1 - I]};

for (size_t i = 0; i < L; ++i)
{
const auto f = fs[i];
const auto w = load_le<uint32_t>(&block[sizeof(uint32_t) * word_index[i][J]]);
const auto k = constants[i][I];
const auto s = rotate_amount[i][J];
Expand All @@ -128,8 +138,10 @@ inline void step(State z[L], const std::byte* block) noexcept
const auto d = z[i][3];
const auto e = z[i][4];

const auto f = (i == 0) ? boolean_fn<I>(b, c, d) : boolean_fn<R - 1 - I>(b, c, d);

z[i][0] = e;
z[i][1] = std::rotl(a + f(b, c, d) + w + k, s) + e;
z[i][1] = std::rotl(a + f + w + k, s) + e;
z[i][2] = b;
z[i][3] = std::rotl(c, 10);
z[i][4] = d;
Expand Down
Loading