Skip to content
This repository has been archived by the owner on Dec 17, 2024. It is now read-only.

Commit

Permalink
Replacing asserts with returns.
Browse files Browse the repository at this point in the history
  • Loading branch information
xcthulhu committed Mar 15, 2015
1 parent cec28eb commit 0ac096d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 36 deletions.
35 changes: 21 additions & 14 deletions src/libethash/ethash.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
53 changes: 35 additions & 18 deletions src/libethash/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
* @date 2015
*/

#include <assert.h>
#include <inttypes.h>
#include <stddef.h>
#include "ethash.h"
Expand All @@ -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);
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -145,34 +154,41 @@ 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,
ethash_params const *params,
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);

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion src/libethash/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 5 additions & 3 deletions test/c/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(&params, 0);
const uint32_t expected_full_size = 1073739904;
const uint32_t expected_cache_size = 16776896;
BOOST_REQUIRE_MESSAGE(ethash_params_init(&params, 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");
Expand Down

0 comments on commit 0ac096d

Please sign in to comment.