Skip to content

Commit

Permalink
Merge pull request #186 from chfast/memcmp_cleanup
Browse files Browse the repository at this point in the history
Clean up equal() / less_equal()
  • Loading branch information
chfast authored Sep 30, 2021
2 parents 5ef7e05 + bf4ddd5 commit 54f425a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 36 deletions.
21 changes: 10 additions & 11 deletions lib/ethash/ethash-internal.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// ethash: C/C++ implementation of Ethash, the Ethereum Proof of Work algorithm.
// Copyright 2018-2019 Pawel Bylica.
// Copyright 2018 Pawel Bylica.
// Licensed under the Apache License, Version 2.0.

/// @file
Expand All @@ -8,28 +8,25 @@

#pragma once

#include <ethash/ethash.hpp>

#include "endianness.hpp"
#include <ethash/ethash.hpp>

#include <memory>
#include <vector>

extern "C" struct ethash_epoch_context_full : ethash_epoch_context
{
ethash_hash1024* full_dataset;

constexpr ethash_epoch_context_full(int epoch, int light_num_items,
const ethash_hash512* light, const uint32_t* l1, int dataset_num_items,
ethash_hash1024* dataset) noexcept
constexpr ethash_epoch_context_full(int epoch, int light_num_items, const ethash_hash512* light,
const uint32_t* l1, int dataset_num_items, ethash_hash1024* dataset) noexcept
: ethash_epoch_context{epoch, light_num_items, light, l1, dataset_num_items},
full_dataset{dataset}
{}
};

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 @@ -41,9 +38,11 @@ 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 std::memcmp(a.bytes, b.bytes, sizeof(a)) == 0;
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]);
}

void build_light_cache(hash512 cache[], int num_items, const hash256& seed) noexcept;
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 54f425a

Please sign in to comment.