Browse Source

Started minor refactor - still API compatible though.

cl-refactor
Gav Wood 10 years ago
parent
commit
2ad558b9a9
  1. 7
      libethash/CMakeLists.txt
  2. 4
      libethash/data_sizes.h
  3. 33
      libethash/ethash.h
  4. 8
      libethash/internal.c
  5. 2
      libethash/internal.h

7
libethash/CMakeLists.txt

@ -12,6 +12,7 @@ endif()
set(FILES util.c set(FILES util.c
util.h util.h
io.c
internal.c internal.c
ethash.h ethash.h
endian.h endian.h
@ -19,6 +20,12 @@ set(FILES util.c
fnv.h fnv.h
data_sizes.h) data_sizes.h)
if (MSVC)
list(APPEND FILES io_win32.c)
else()
list(APPEND FILES io_posix.c)
endif()
if (NOT CRYPTOPP_FOUND) if (NOT CRYPTOPP_FOUND)
find_package(CryptoPP 5.6.2) find_package(CryptoPP 5.6.2)
endif() endif()

4
libethash/data_sizes.h

@ -48,7 +48,7 @@ extern "C" {
// Sow[i*HashBytes]; j++]]]][[2]][[1]] // Sow[i*HashBytes]; j++]]]][[2]][[1]]
static const size_t dag_sizes[2048] = { static const uint64_t dag_sizes[2048] = {
1073739904U, 1082130304U, 1090514816U, 1098906752U, 1107293056U, 1073739904U, 1082130304U, 1090514816U, 1098906752U, 1107293056U,
1115684224U, 1124070016U, 1132461952U, 1140849536U, 1149232768U, 1115684224U, 1124070016U, 1132461952U, 1140849536U, 1149232768U,
1157627776U, 1166013824U, 1174404736U, 1182786944U, 1191180416U, 1157627776U, 1166013824U, 1174404736U, 1182786944U, 1191180416U,
@ -477,7 +477,7 @@ static const size_t dag_sizes[2048] = {
// While[! PrimeQ[i], i--]; // While[! PrimeQ[i], i--];
// Sow[i*HashBytes]; j++]]]][[2]][[1]] // Sow[i*HashBytes]; j++]]]][[2]][[1]]
const size_t cache_sizes[2048] = { const uint64_t cache_sizes[2048] = {
16776896U, 16907456U, 17039296U, 17170112U, 17301056U, 17432512U, 17563072U, 16776896U, 16907456U, 17039296U, 17170112U, 17301056U, 17432512U, 17563072U,
17693888U, 17824192U, 17955904U, 18087488U, 18218176U, 18349504U, 18481088U, 17693888U, 17824192U, 17955904U, 18087488U, 18218176U, 18349504U, 18481088U,
18611392U, 18742336U, 18874304U, 19004224U, 19135936U, 19267264U, 19398208U, 18611392U, 18742336U, 18874304U, 19004224U, 19135936U, 19267264U, 19398208U,

33
libethash/ethash.h

@ -43,8 +43,8 @@ extern "C" {
#endif #endif
typedef struct ethash_params { typedef struct ethash_params {
size_t full_size; // Size of full data set (in bytes, multiple of mix size (128)). uint64_t full_size; // Size of full data set (in bytes, multiple of mix size (128)).
size_t cache_size; // Size of compute cache (in bytes, multiple of node size (64)). uint64_t cache_size; // Size of compute cache (in bytes, multiple of node size (64)).
} ethash_params; } ethash_params;
typedef struct ethash_return_value { typedef struct ethash_return_value {
@ -52,8 +52,8 @@ typedef struct ethash_return_value {
uint8_t mix_hash[32]; uint8_t mix_hash[32];
} ethash_return_value; } ethash_return_value;
size_t ethash_get_datasize(const uint32_t block_number); uint64_t ethash_get_datasize(const uint32_t block_number);
size_t ethash_get_cachesize(const uint32_t block_number); uint64_t ethash_get_cachesize(const uint32_t block_number);
// initialize the parameters // initialize the parameters
static inline void ethash_params_init(ethash_params *params, const uint32_t block_number) { static inline void ethash_params_init(ethash_params *params, const uint32_t block_number) {
@ -93,23 +93,30 @@ static inline void ethash_compute_full(ethash_return_value *ret, void const *ful
ethash_full(ret, full, params, header_hash, nonce); ethash_full(ret, full, params, header_hash, nonce);
} }
// Returns if hash is less than or equal to difficulty /// @brief Compare two s256-bit big-endian values.
static inline int ethash_check_difficulty( /// @returns 1 if @a a is less than or equal to @a b, 0 otherwise.
const uint8_t hash[32], /// Both parameters are 256-bit big-endian values.
const uint8_t difficulty[32]) { static inline int ethash_leq_be256(const uint8_t a[32], const uint8_t b[32]) {
// Difficulty is big endian // Boundary is big endian
for (int i = 0; i < 32; i++) { for (int i = 0; i < 32; i++) {
if (hash[i] == difficulty[i]) continue; if (a[i] == b[i])
return hash[i] < difficulty[i]; continue;
return a[i] < b[i];
} }
return 1; return 1;
} }
int ethash_quick_check_difficulty( /// Perofrms a cursory check on the validity of the nonce.
/// @returns 1 if the nonce may possibly be valid for the given header_hash & boundary.
/// @p boundary equivalent to 2 ^ 256 / block_difficulty, represented as a 256-bit big-endian.
int ethash_preliminary_check_boundary(
const uint8_t header_hash[32], const uint8_t header_hash[32],
const uint64_t nonce, const uint64_t nonce,
const uint8_t mix_hash[32], const uint8_t mix_hash[32],
const uint8_t difficulty[32]); const uint8_t boundary[32]);
#define ethash_quick_check_difficulty ethash_preliminary_check_boundary
#define ethash_check_difficulty ethash_leq_be256
#ifdef __cplusplus #ifdef __cplusplus
} }

8
libethash/internal.c

@ -37,12 +37,12 @@
#include "sha3.h" #include "sha3.h"
#endif // WITH_CRYPTOPP #endif // WITH_CRYPTOPP
size_t ethash_get_datasize(const uint32_t block_number) { uint64_t ethash_get_datasize(const uint32_t block_number) {
assert(block_number / EPOCH_LENGTH < 2048); assert(block_number / EPOCH_LENGTH < 2048);
return dag_sizes[block_number / EPOCH_LENGTH]; return dag_sizes[block_number / EPOCH_LENGTH];
} }
size_t ethash_get_cachesize(const uint32_t block_number) { uint64_t ethash_get_cachesize(const uint32_t block_number) {
assert(block_number / EPOCH_LENGTH < 2048); assert(block_number / EPOCH_LENGTH < 2048);
return cache_sizes[block_number / EPOCH_LENGTH]; return cache_sizes[block_number / EPOCH_LENGTH];
} }
@ -280,7 +280,7 @@ void ethash_get_seedhash(uint8_t seedhash[32], const uint32_t block_number) {
SHA3_256(seedhash, seedhash, 32); SHA3_256(seedhash, seedhash, 32);
} }
int ethash_quick_check_difficulty( int ethash_preliminary_check_boundary(
const uint8_t header_hash[32], const uint8_t header_hash[32],
const uint64_t nonce, const uint64_t nonce,
const uint8_t mix_hash[32], const uint8_t mix_hash[32],
@ -288,7 +288,7 @@ int ethash_quick_check_difficulty(
uint8_t return_hash[32]; uint8_t return_hash[32];
ethash_quick_hash(return_hash, header_hash, nonce, mix_hash); ethash_quick_hash(return_hash, header_hash, nonce, mix_hash);
return ethash_check_difficulty(return_hash, difficulty); return ethash_leq_be256(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) { 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) {

2
libethash/internal.h

@ -3,7 +3,7 @@
#include "endian.h" #include "endian.h"
#include "ethash.h" #include "ethash.h"
#define ENABLE_SSE 1 #define ENABLE_SSE 0
#if defined(_M_X64) && ENABLE_SSE #if defined(_M_X64) && ENABLE_SSE
#include <smmintrin.h> #include <smmintrin.h>

Loading…
Cancel
Save