Skip to content

Commit

Permalink
Rename is_equal() -> equal(), is_less_or_equal() -> less_equal()
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Sep 30, 2021
1 parent 3afa883 commit bf4ddd5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 27 deletions.
6 changes: 4 additions & 2 deletions lib/ethash/ethash-internal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ extern "C" struct ethash_epoch_context_full : ethash_epoch_context

namespace ethash
{
inline bool is_less_or_equal(const hash256& a, const hash256& b) noexcept
/// Returns true if a <= b in byte-wise comparison (i.e. as big-endian numbers).
inline bool less_equal(const hash256& a, const hash256& b) noexcept
{
for (size_t i = 0; i < (sizeof(a) / sizeof(a.word64s[0])); ++i)
{
Expand All @@ -37,7 +38,8 @@ inline bool is_less_or_equal(const hash256& a, const hash256& b) noexcept
return true;
}

inline bool is_equal(const hash256& a, const hash256& b) noexcept
/// Returns true if a == b in byte-wise comparison.
inline bool equal(const hash256& a, const hash256& b) noexcept
{
return (a.word64s[0] == b.word64s[0]) & (a.word64s[1] == b.word64s[1]) &
(a.word64s[2] == b.word64s[2]) & (a.word64s[3] == b.word64s[3]);
Expand Down
18 changes: 6 additions & 12 deletions lib/ethash/ethash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@

#include "ethash-internal.hpp"

#include "../support/attributes.h"
#include "bit_manipulation.h"
#include "endianness.hpp"
#include "primes.h"
#include <ethash/keccak.hpp>
#include <ethash/progpow.hpp>

#include <cassert>
#include <cstdlib>
#include <cstring>
#include <limits>

namespace ethash
{
Expand Down Expand Up @@ -305,8 +300,7 @@ inline hash256 hash_kernel(

result hash(const epoch_context_full& context, const hash256& header_hash, uint64_t nonce) noexcept
{
static const auto lazy_lookup = [](const epoch_context& ctx, uint32_t index) noexcept
{
static const auto lazy_lookup = [](const epoch_context& ctx, uint32_t index) noexcept {
auto full_dataset = static_cast<const epoch_context_full&>(ctx).full_dataset;
hash1024& item = full_dataset[index];
if (item.word64s[0] == 0)
Expand All @@ -330,7 +324,7 @@ search_result search_light(const epoch_context& context, const hash256& header_h
for (uint64_t nonce = start_nonce; nonce < end_nonce; ++nonce)
{
result r = hash(context, header_hash, nonce);
if (is_less_or_equal(r.final_hash, boundary))
if (less_equal(r.final_hash, boundary))
return {r, nonce};
}
return {};
Expand All @@ -343,7 +337,7 @@ search_result search(const epoch_context_full& context, const hash256& header_ha
for (uint64_t nonce = start_nonce; nonce < end_nonce; ++nonce)
{
result r = hash(context, header_hash, nonce);
if (is_less_or_equal(r.final_hash, boundary))
if (less_equal(r.final_hash, boundary))
return {r, nonce};
}
return {};
Expand Down Expand Up @@ -425,18 +419,18 @@ bool ethash_verify_final_hash(const hash256* header_hash, const hash256* mix_has
const hash256* boundary) noexcept
{
const hash512 seed = hash_seed(*header_hash, nonce);
return is_less_or_equal(hash_final(seed, *mix_hash), *boundary);
return less_equal(hash_final(seed, *mix_hash), *boundary);
}

bool ethash_verify(const epoch_context* context, const hash256* header_hash,
const hash256* mix_hash, uint64_t nonce, const hash256* boundary) noexcept
{
const hash512 seed = hash_seed(*header_hash, nonce);
if (!is_less_or_equal(hash_final(seed, *mix_hash), *boundary))
if (!less_equal(hash_final(seed, *mix_hash), *boundary))
return false;

const hash256 expected_mix_hash = hash_kernel(*context, seed, calculate_dataset_item_1024);
return is_equal(expected_mix_hash, *mix_hash);
return equal(expected_mix_hash, *mix_hash);
}

} // extern "C"
11 changes: 5 additions & 6 deletions lib/ethash/progpow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,7 @@ result hash(const epoch_context& context, int block_number, const hash256& heade
result hash(const epoch_context_full& context, int block_number, const hash256& header_hash,
uint64_t nonce) noexcept
{
static const auto lazy_lookup = [](const epoch_context& ctx, uint32_t index) noexcept
{
static const auto lazy_lookup = [](const epoch_context& ctx, uint32_t index) noexcept {
auto* full_dataset_1024 = static_cast<const epoch_context_full&>(ctx).full_dataset;
auto* full_dataset_2048 = reinterpret_cast<hash2048*>(full_dataset_1024);
hash2048& item = full_dataset_2048[index];
Expand All @@ -321,12 +320,12 @@ bool verify(const epoch_context& context, int block_number, const hash256& heade
{
const uint64_t seed = keccak_progpow_64(header_hash, nonce);
const hash256 final_hash = keccak_progpow_256(header_hash, seed, mix_hash);
if (!is_less_or_equal(final_hash, boundary))
if (!less_equal(final_hash, boundary))
return false;

const hash256 expected_mix_hash =
hash_mix(context, block_number, seed, calculate_dataset_item_2048);
return is_equal(expected_mix_hash, mix_hash);
return equal(expected_mix_hash, mix_hash);
}

search_result search_light(const epoch_context& context, int block_number,
Expand All @@ -337,7 +336,7 @@ search_result search_light(const epoch_context& context, int block_number,
for (uint64_t nonce = start_nonce; nonce < end_nonce; ++nonce)
{
result r = hash(context, block_number, header_hash, nonce);
if (is_less_or_equal(r.final_hash, boundary))
if (less_equal(r.final_hash, boundary))
return {r, nonce};
}
return {};
Expand All @@ -351,7 +350,7 @@ search_result search(const epoch_context_full& context, int block_number,
for (uint64_t nonce = start_nonce; nonce < end_nonce; ++nonce)
{
result r = hash(context, block_number, header_hash, nonce);
if (is_less_or_equal(r.final_hash, boundary))
if (less_equal(r.final_hash, boundary))
return {r, nonce};
}
return {};
Expand Down
14 changes: 7 additions & 7 deletions test/unittests/test_ethash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ hash512 copy(const hash512& h) noexcept
{
return h;
}
}
} // namespace

TEST(ethash, revision)
{
Expand Down Expand Up @@ -773,14 +773,14 @@ TEST(ethash, create_context_oom)

namespace
{
struct is_less_or_equal_test_case
struct less_equal_test_case
{
const char* a_hex;
const char* b_hex;
bool excected_result;
bool expected_result;
};

is_less_or_equal_test_case is_less_or_equal_test_cases[] = {
less_equal_test_case less_equal_test_cases[] = {
{"0000000000000000000000000000000000000000000000000000000000000000",
"0000000000000000000000000000000000000000000000000000000000000000", true},
{"0000000000000000000000000000000000000000000000000000000000000001",
Expand Down Expand Up @@ -826,12 +826,12 @@ is_less_or_equal_test_case is_less_or_equal_test_cases[] = {
};
} // namespace

TEST(ethash, is_less_or_equal)
TEST(ethash, less_equal)
{
for (const auto& t : is_less_or_equal_test_cases)
for (const auto& t : less_equal_test_cases)
{
auto a = to_hash256(t.a_hex);
auto b = to_hash256(t.b_hex);
EXPECT_EQ(is_less_or_equal(a, b), t.excected_result);
EXPECT_EQ(less_equal(a, b), t.expected_result);
}
}

0 comments on commit bf4ddd5

Please sign in to comment.