Skip to content

Commit

Permalink
precompiles: Add basic SHA256 unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Jun 25, 2024
1 parent 857513d commit b697c4f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ target_sources(
instructions_test.cpp
precompiles_blake2b_test.cpp
precompiles_ripemd160_test.cpp
precompiles_sha256_test.cpp
state_block_test.cpp
state_bloom_filter_test.cpp
state_difficulty_test.cpp
Expand Down
32 changes: 32 additions & 0 deletions test/unittests/precompiles_sha256_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// evmone: Fast Ethereum Virtual Machine implementation
// Copyright 2024 The evmone Authors.
// SPDX-License-Identifier: Apache-2.0

#include <evmc/hex.hpp>
#include <evmone_precompiles/sha256.hpp>
#include <gtest/gtest.h>

using evmone::crypto::sha256;

TEST(sha256, test_vectors)
{
// Some test vectors from https://www.di-mgt.com.au/sha_testvectors.html.

const std::pair<std::string_view, std::string_view> test_cases[] = {
{"", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"},
{"abc", "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"},
{"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
"248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"},
{"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
"cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1"},
};

for (const auto& [input, expected_hash_hex] : test_cases)
{
std::byte hash[evmone::crypto::SHA256_HASH_SIZE];
sha256(hash, reinterpret_cast<const std::byte*>(input.data()), input.size());
const auto hash_hex = evmc::hex({reinterpret_cast<const uint8_t*>(hash), std::size(hash)});
EXPECT_EQ(hash_hex, expected_hash_hex);
}
}

0 comments on commit b697c4f

Please sign in to comment.