From 0ac096db1c7098d0c6d740bc65b11646864b53e2 Mon Sep 17 00:00:00 2001 From: Matthew Wampler-Doty Date: Sat, 14 Mar 2015 20:46:38 -0700 Subject: [PATCH] Replacing asserts with returns. --- src/libethash/ethash.h | 35 +++++++++++++++----------- src/libethash/internal.c | 53 ++++++++++++++++++++++++++-------------- src/libethash/internal.h | 2 +- test/c/test.cpp | 8 +++--- 4 files changed, 62 insertions(+), 36 deletions(-) diff --git a/src/libethash/ethash.h b/src/libethash/ethash.h index a7159de6..809b7cce 100644 --- a/src/libethash/ethash.h +++ b/src/libethash/ethash.h @@ -55,42 +55,49 @@ typedef struct ethash_return_value { size_t ethash_get_datasize(const uint32_t block_number); size_t ethash_get_cachesize(const uint32_t block_number); -// initialize the parameters -static inline void ethash_params_init(ethash_params *params, const uint32_t block_number) { +// Initialize the Parameters +static inline int ethash_params_init(ethash_params *params, const uint32_t block_number) { params->full_size = ethash_get_datasize(block_number); + if (params->full_size == 0) + return 0; + params->cache_size = ethash_get_cachesize(block_number); + if (params->cache_size == 0) + return 0; + + return 1; } typedef struct ethash_cache { void *mem; } ethash_cache; -void ethash_mkcache(ethash_cache *cache, ethash_params const *params, const uint8_t seed[32]); -void ethash_compute_full_data(void *mem, ethash_params const *params, ethash_cache const *cache); -void ethash_full(ethash_return_value *ret, void const *full_mem, ethash_params const *params, const uint8_t header_hash[32], const uint64_t nonce); -void ethash_light(ethash_return_value *ret, ethash_cache const *cache, ethash_params const *params, const uint8_t header_hash[32], const uint64_t nonce); +int ethash_mkcache(ethash_cache *cache, ethash_params const *params, const uint8_t seed[32]); +int ethash_compute_full_data(void *mem, ethash_params const *params, ethash_cache const *cache); +int ethash_full(ethash_return_value *ret, void const *full_mem, ethash_params const *params, const uint8_t header_hash[32], const uint64_t nonce); +int ethash_light(ethash_return_value *ret, ethash_cache const *cache, ethash_params const *params, const uint8_t header_hash[32], const uint64_t nonce); void ethash_get_seedhash(uint8_t seedhash[32], const uint32_t block_number); -static inline void ethash_prep_light(void *cache, ethash_params const *params, const uint8_t seed[32]) { +static inline int ethash_prep_light(void *cache, ethash_params const *params, const uint8_t seed[32]) { ethash_cache c; c.mem = cache; - ethash_mkcache(&c, params, seed); + return ethash_mkcache(&c, params, seed); } -static inline void ethash_compute_light(ethash_return_value *ret, void const *cache, ethash_params const *params, const uint8_t header_hash[32], const uint64_t nonce) { +static inline int ethash_compute_light(ethash_return_value *ret, void const *cache, ethash_params const *params, const uint8_t header_hash[32], const uint64_t nonce) { ethash_cache c; c.mem = (void *) cache; - ethash_light(ret, &c, params, header_hash, nonce); + return ethash_light(ret, &c, params, header_hash, nonce); } -static inline void ethash_prep_full(void *full, ethash_params const *params, void const *cache) { +static inline int ethash_prep_full(void *full, ethash_params const *params, void const *cache) { ethash_cache c; c.mem = (void *) cache; - ethash_compute_full_data(full, params, &c); + return ethash_compute_full_data(full, params, &c); } -static inline void ethash_compute_full(ethash_return_value *ret, void const *full, ethash_params const *params, const uint8_t header_hash[32], const uint64_t nonce) { - ethash_full(ret, full, params, header_hash, nonce); +static inline int ethash_compute_full(ethash_return_value *ret, void const *full, ethash_params const *params, const uint8_t header_hash[32], const uint64_t nonce) { + return ethash_full(ret, full, params, header_hash, nonce); } // Returns if hash is less than or equal to difficulty diff --git a/src/libethash/internal.c b/src/libethash/internal.c index 0a7e767e..9b6629f7 100644 --- a/src/libethash/internal.c +++ b/src/libethash/internal.c @@ -20,7 +20,6 @@ * @date 2015 */ -#include #include #include #include "ethash.h" @@ -38,23 +37,28 @@ #endif // WITH_CRYPTOPP size_t ethash_get_datasize(const uint32_t block_number) { - assert(block_number / EPOCH_LENGTH < 2048); + if (block_number / EPOCH_LENGTH >= 2048) + return 0; return dag_sizes[block_number / EPOCH_LENGTH]; } size_t ethash_get_cachesize(const uint32_t block_number) { - assert(block_number / EPOCH_LENGTH < 2048); + if (block_number / EPOCH_LENGTH >= 2048) + return 0; return cache_sizes[block_number / EPOCH_LENGTH]; } // Follows Sergio's "STRICT MEMORY HARD HASHING FUNCTIONS" (2014) // https://bitslog.files.wordpress.com/2013/12/memohash-v0-3.pdf // SeqMemoHash(s, R, N) -void static ethash_compute_cache_nodes( +int static ethash_compute_cache_nodes( node *const nodes, ethash_params const *params, const uint8_t seed[32]) { - assert((params->cache_size % sizeof(node)) == 0); + + if ((params->cache_size % sizeof(node)) != 0) + return 0; + uint32_t const num_nodes = (uint32_t) (params->cache_size / sizeof(node)); SHA3_512(nodes[0].bytes, seed, 32); @@ -82,22 +86,27 @@ void static ethash_compute_cache_nodes( nodes->words[w] = fix_endian32(nodes->words[w]); } #endif + + return 1; } -void ethash_mkcache( +int ethash_mkcache( ethash_cache *cache, ethash_params const *params, const uint8_t seed[32]) { node *nodes = (node *) cache->mem; - ethash_compute_cache_nodes(nodes, params, seed); + return ethash_compute_cache_nodes(nodes, params, seed); } -void ethash_calculate_dag_item( +int ethash_calculate_dag_item( node *const ret, const unsigned node_index, const struct ethash_params *params, const struct ethash_cache *cache) { + if (params->cache_size % sizeof(node) != 0) + return 0; + uint32_t num_parent_nodes = (uint32_t) (params->cache_size / sizeof(node)); node const *cache_nodes = (node const *) cache->mem; node const *init = &cache_nodes[node_index % num_parent_nodes]; @@ -145,23 +154,30 @@ void ethash_calculate_dag_item( } SHA3_512(ret->bytes, ret->bytes, sizeof(node)); + return 1; } -void ethash_compute_full_data( +int ethash_compute_full_data( void *mem, ethash_params const *params, ethash_cache const *cache) { - assert((params->full_size % (sizeof(uint32_t) * MIX_WORDS)) == 0); - assert((params->full_size % sizeof(node)) == 0); + + if ((params->full_size % (sizeof(uint32_t) * MIX_WORDS)) != 0) + return 0; + + if ((params->full_size % sizeof(node)) != 0) + return 0; + node *full_nodes = mem; // now compute full nodes for (unsigned n = 0; n != (params->full_size / sizeof(node)); ++n) { ethash_calculate_dag_item(&(full_nodes[n]), n, params, cache); } + return 1; } -static void ethash_hash( +static int ethash_hash( ethash_return_value *ret, node const *full_nodes, ethash_cache const *cache, @@ -169,10 +185,10 @@ static void ethash_hash( const uint8_t header_hash[32], const uint64_t nonce) { - assert((params->full_size % MIX_WORDS) == 0); + if ((params->full_size % MIX_WORDS) != 0) + return 0; // pack hash and nonce together into first 40 bytes of s_mix - assert(sizeof(node) * 8 == 512); node s_mix[MIX_NODES + 1]; memcpy(s_mix[0].bytes, header_hash, 32); @@ -254,6 +270,7 @@ static void ethash_hash( memcpy(ret->mix_hash, mix->bytes, 32); // final Keccak hash SHA3_256(ret->result, s_mix->bytes, 64 + 32); // Keccak-256(s + compressed_mix) + return 1; } void ethash_quick_hash( @@ -291,10 +308,10 @@ int ethash_quick_check_difficulty( return ethash_check_difficulty(return_hash, difficulty); } -void ethash_full(ethash_return_value *ret, void const *full_mem, ethash_params const *params, const uint8_t previous_hash[32], const uint64_t nonce) { - ethash_hash(ret, (node const *) full_mem, NULL, params, previous_hash, nonce); +int ethash_full(ethash_return_value *ret, void const *full_mem, ethash_params const *params, const uint8_t previous_hash[32], const uint64_t nonce) { + return ethash_hash(ret, (node const *) full_mem, NULL, params, previous_hash, nonce); } -void ethash_light(ethash_return_value *ret, ethash_cache const *cache, ethash_params const *params, const uint8_t previous_hash[32], const uint64_t nonce) { - ethash_hash(ret, NULL, cache, params, previous_hash, nonce); +int ethash_light(ethash_return_value *ret, ethash_cache const *cache, ethash_params const *params, const uint8_t previous_hash[32], const uint64_t nonce) { + return ethash_hash(ret, NULL, cache, params, previous_hash, nonce); } \ No newline at end of file diff --git a/src/libethash/internal.h b/src/libethash/internal.h index bcbacdaa..19d6ccd2 100644 --- a/src/libethash/internal.h +++ b/src/libethash/internal.h @@ -30,7 +30,7 @@ typedef union node { } node; -void ethash_calculate_dag_item( +int ethash_calculate_dag_item( node *const ret, const unsigned node_index, ethash_params const *params, diff --git a/test/c/test.cpp b/test/c/test.cpp index 21cb251f..9c0213d9 100644 --- a/test/c/test.cpp +++ b/test/c/test.cpp @@ -80,9 +80,11 @@ BOOST_AUTO_TEST_CASE(ethash_params_init_genesis_check) { BOOST_AUTO_TEST_CASE(ethash_params_init_genesis_calcifide_check) { ethash_params params; - ethash_params_init(¶ms, 0); - const uint32_t expected_full_size = 1073739904; - const uint32_t expected_cache_size = 16776896; + BOOST_REQUIRE_MESSAGE(ethash_params_init(¶ms, 0), + "Params could not be initialized"); + const uint32_t + expected_full_size = 1073739904, + expected_cache_size = 16776896; BOOST_REQUIRE_MESSAGE(params.full_size == expected_full_size, "\nexpected: " << expected_cache_size << "\n" << "actual: " << params.full_size << "\n");