Skip to content

Commit

Permalink
precompiles: Use std::byte, drop "cpu ext" in SHA256
Browse files Browse the repository at this point in the history
Use `std::byte` instead of `uint8_t`
and drop the `use_cpu_extensions` parameter.
  • Loading branch information
chfast committed Jun 11, 2024
1 parent a34ae07 commit eadca78
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
36 changes: 15 additions & 21 deletions lib/evmone_precompiles/sha256.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,14 @@ static const uint32_t k[] = {0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3

struct BufferState
{
const uint8_t* p;
const std::byte* p;
size_t len;
size_t total_len;
bool single_one_delivered = false;
bool total_len_delivered = false;

constexpr BufferState(const uint8_t* input, size_t len) : p{input}, len{len}, total_len{len} {}
constexpr BufferState(const std::byte* input, size_t len) : p{input}, len{len}, total_len{len}
{}
};

static bool calc_chunk(uint8_t chunk[CHUNK_SIZE], struct BufferState* state)
Expand Down Expand Up @@ -134,7 +135,7 @@ static bool calc_chunk(uint8_t chunk[CHUNK_SIZE], struct BufferState* state)
}

[[gnu::always_inline, msvc::forceinline]] inline void sha_256_implementation(
uint32_t h[8], const uint8_t* input, size_t len)
uint32_t h[8], const std::byte* input, size_t len)
{
/*
* Note 1: All integers (expect indexes) are 32-bit unsigned integers and addition is calculated
Expand Down Expand Up @@ -234,17 +235,17 @@ static bool calc_chunk(uint8_t chunk[CHUNK_SIZE], struct BufferState* state)
}
}

static void sha_256_generic(uint32_t h[8], const uint8_t* input, size_t len)
static void sha_256_generic(uint32_t h[8], const std::byte* input, size_t len)
{
sha_256_implementation(h, input, len);
}

static void (*sha_256_best)(uint32_t h[8], const uint8_t* input, size_t len) = sha_256_generic;
static void (*sha_256_best)(uint32_t h[8], const std::byte* input, size_t len) = sha_256_generic;

#if defined(__x86_64__)

__attribute__((target("bmi,bmi2"))) static void sha_256_x86_bmi(
uint32_t h[8], const uint8_t* input, size_t len)
uint32_t h[8], const std::byte* input, size_t len)
{
sha_256_implementation(h, input, len);
}
Expand All @@ -259,7 +260,7 @@ __attribute__((target("bmi,bmi2"))) static void sha_256_x86_bmi(
/* Based on code from Intel, and by Sean Gulley for */
/* the miTLS project. */
__attribute__((target("sha,sse4.1"))) static void sha_256_x86_sha(
uint32_t h[8], const uint8_t* input, size_t len)
uint32_t h[8], const std::byte* input, size_t len)
{
// NOLINTBEGIN(readability-isolate-declaration)
__m128i STATE0, STATE1;
Expand Down Expand Up @@ -527,7 +528,7 @@ __attribute__((constructor)) static void select_sha256_implementation()
/* Written and placed in public domain by Jeffrey Walton */
/* Based on code from ARM, and by Johannes Schneiders, Skip */
/* Hovsmith and Barry O'Rourke for the mbedTLS project. */
static void sha_256_arm_v8(uint32_t h[8], const uint8_t* input, size_t len)
static void sha_256_arm_v8(uint32_t h[8], const std::byte* input, size_t len)
{
uint32x4_t STATE0, STATE1, ABEF_SAVE, CDGH_SAVE;
uint32x4_t MSG0, MSG1, MSG2, MSG3;
Expand Down Expand Up @@ -723,7 +724,7 @@ __attribute__((constructor)) static void select_sha256_implementation(void)
* support for bit string lengths that are not multiples of eight, and it really operates on arrays
* of bytes. In particular, the len parameter is a number of bytes.
*/
void sha256(uint8_t hash[32], const uint8_t* input, size_t len, bool use_cpu_extensions)
void sha256(std::byte hash[SHA256_HASH_SIZE], const std::byte* data, size_t size)
{
/*
* Initialize hash values:
Expand All @@ -732,22 +733,15 @@ void sha256(uint8_t hash[32], const uint8_t* input, size_t len, bool use_cpu_ext
uint32_t h[] = {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c,
0x1f83d9ab, 0x5be0cd19};

if (use_cpu_extensions)
{
sha_256_best(h, input, len);
}
else
{
sha_256_generic(h, input, len);
}
sha_256_best(h, data, size);

/* Produce the final hash value (big-endian): */
for (unsigned i = 0, j = 0; i < 8; i++)
{
hash[j++] = (uint8_t)(h[i] >> 24);
hash[j++] = (uint8_t)(h[i] >> 16);
hash[j++] = (uint8_t)(h[i] >> 8);
hash[j++] = (uint8_t)h[i];
hash[j++] = static_cast<std::byte>(h[i] >> 24);
hash[j++] = static_cast<std::byte>(h[i] >> 16);
hash[j++] = static_cast<std::byte>(h[i] >> 8);
hash[j++] = static_cast<std::byte>(h[i]);
}
}

Expand Down
13 changes: 10 additions & 3 deletions lib/evmone_precompiles/sha256.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@
#pragma once

#include <cstddef>
#include <cstdint>

namespace evmone::crypto
{
void sha256(uint8_t hash[32], const uint8_t* input, size_t len, bool use_cpu_extensions);
}
/// The size (32 bytes) of the SHA256 message digest.
static constexpr std::size_t SHA256_HASH_SIZE = 256 / 8;

/// Computes the SHA256 hash function.
///
/// @param[out] hash The result message digest is written to the provided memory.
/// @param data The input data.
/// @param size The size of the input data.
void sha256(std::byte hash[SHA256_HASH_SIZE], const std::byte* data, size_t size);
} // namespace evmone::crypto
3 changes: 2 additions & 1 deletion test/state/precompiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ ExecutionResult sha256_execute(const uint8_t* input, size_t input_size, uint8_t*
[[maybe_unused]] size_t output_size) noexcept
{
assert(output_size >= 32);
crypto::sha256(output, input, input_size, true);
crypto::sha256(reinterpret_cast<std::byte*>(output), reinterpret_cast<const std::byte*>(input),
input_size);
return {EVMC_SUCCESS, 32};
}

Expand Down

0 comments on commit eadca78

Please sign in to comment.