Skip to content
This repository was archived by the owner on Apr 24, 2022. It is now read-only.

Commit 9bf6ad8

Browse files
committedMay 6, 2015
subtree bump: ethash_reloaded
2 parents 808886a + 3f72ae8 commit 9bf6ad8

19 files changed

+2052
-1419
lines changed
 

‎libethash/CMakeLists.txt

+2-7
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ if (NOT MSVC)
1010
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99")
1111
endif()
1212

13-
set(FILES util.c
14-
util.h
13+
set(FILES util.h
1514
io.c
1615
internal.c
1716
ethash.h
@@ -21,7 +20,7 @@ set(FILES util.c
2120
data_sizes.h)
2221

2322
if (MSVC)
24-
list(APPEND FILES io_win32.c)
23+
list(APPEND FILES util_win32.c io_win32.c mmap_win32.c)
2524
else()
2625
list(APPEND FILES io_posix.c)
2726
endif()
@@ -43,7 +42,3 @@ add_library(${LIBRARY} ${FILES})
4342
if (CRYPTOPP_FOUND)
4443
TARGET_LINK_LIBRARIES(${LIBRARY} ${CRYPTOPP_LIBRARIES})
4544
endif()
46-
47-
if (NOT ETHASHCL)
48-
install( TARGETS ${LIBRARY} RUNTIME DESTINATION bin ARCHIVE DESTINATION lib LIBRARY DESTINATION lib )
49-
endif ()

‎libethash/data_sizes.h

+738-738
Large diffs are not rendered by default.

‎libethash/endian.h

+36-38
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,6 @@
33
#include <stdint.h>
44
#include "compiler.h"
55

6-
static const uint8_t BitReverseTable256[] =
7-
{
8-
0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0,
9-
0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8, 0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8,
10-
0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4, 0x14, 0x94, 0x54, 0xD4, 0x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0x3B, 0xBB, 0x7B, 0xFB,
22-
0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7, 0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7,
23-
0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF, 0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF
24-
};
25-
26-
static inline uint32_t bitfn_swap32(uint32_t a) {
27-
return (BitReverseTable256[a & 0xff] << 24) |
28-
(BitReverseTable256[(a >> 8) & 0xff] << 16) |
29-
(BitReverseTable256[(a >> 16) & 0xff] << 8) |
30-
(BitReverseTable256[(a >> 24) & 0xff]);
31-
}
32-
33-
static inline uint64_t bitfn_swap64(uint64_t a) {
34-
return ((uint64_t) bitfn_swap32((uint32_t) (a >> 32))) |
35-
(((uint64_t) bitfn_swap32((uint32_t) a)) << 32);
36-
}
37-
386
#if defined(__MINGW32__) || defined(_WIN32)
397
# define LITTLE_ENDIAN 1234
408
# define BYTE_ORDER LITTLE_ENDIAN
@@ -53,22 +21,52 @@ static inline uint64_t bitfn_swap64(uint64_t a) {
5321
# define BIG_ENDIAN 1234
5422
# define BYTE_ORDER BIG_ENDIAN
5523
#else
56-
5724
# include <endian.h>
25+
#endif
5826

27+
#if defined(_WIN32)
28+
#include <stdlib.h>
29+
#define ethash_swap_u32(input_) _byteswap_ulong(input_)
30+
#define ethash_swap_u64(input_) _byteswap_uint64(input_)
31+
#elif defined(__APPLE__)
32+
#include <libkern/OSByteOrder.h>
33+
#define ethash_swap_u32(input_) OSSwapInt32(input_)
34+
#define ethash_swap_u64(input_) OSSwapInt64(input_)
35+
#else // posix
36+
#include <byteswap.h>
37+
#define ethash_swap_u32(input_) __bswap_32(input_)
38+
#define ethash_swap_u64(input_) __bswap_64(input_)
5939
#endif
6040

6141

6242
#if LITTLE_ENDIAN == BYTE_ORDER
6343

64-
#define fix_endian32(x) (x)
65-
#define fix_endian64(x) (x)
44+
#define fix_endian32(dst_ ,src_) dst_ = src_
45+
#define fix_endian32_same(val_)
46+
#define fix_endian64(dst_, src_) dst_ = src_
47+
#define fix_endian64_same(val_)
48+
#define fix_endian_arr32(arr_, size_)
49+
#define fix_endian_arr64(arr_, size_)
6650

6751
#elif BIG_ENDIAN == BYTE_ORDER
6852

69-
#define fix_endian32(x) bitfn_swap32(x)
70-
#define fix_endian64(x) bitfn_swap64(x)
53+
#define fix_endian32(dst_, src_) dst_ = ethash_swap_u32(src_)
54+
#define fix_endian32_same(val_) val_ = ethash_swap_u32(val_)
55+
#define fix_endian64(dst_, src_) dst_ = ethash_swap_u64(src_
56+
#define fix_endian64_same(val_) val_ = ethash_swap_u64(val_)
57+
#define fix_endian_arr32(arr_, size_) \
58+
do { \
59+
for (unsigned i_ = 0; i_ < (size_), ++i_) { \
60+
arr_[i_] = ethash_swap_u32(arr_[i_]); \
61+
} \
62+
while (0)
63+
#define fix_endian_arr64(arr_, size_) \
64+
do { \
65+
for (unsigned i_ = 0; i_ < (size_), ++i_) { \
66+
arr_[i_] = ethash_swap_u64(arr_[i_]); \
67+
} \
68+
while (0) \
7169

7270
#else
7371
# error "endian not supported"
74-
#endif // BYTE_ORDER
72+
#endif // BYTE_ORDER

‎libethash/ethash.h

+96-88
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include <stdbool.h>
2525
#include <string.h>
2626
#include <stddef.h>
27-
#include <stdlib.h>
2827
#include "compiler.h"
2928

3029
#define ETHASH_REVISION 23
@@ -38,101 +37,110 @@
3837
#define ETHASH_DATASET_PARENTS 256
3938
#define ETHASH_CACHE_ROUNDS 3
4039
#define ETHASH_ACCESSES 64
40+
#define ETHASH_DAG_MAGIC_NUM_SIZE 8
41+
#define ETHASH_DAG_MAGIC_NUM 0xFEE1DEADBADDCAFE
4142

4243
#ifdef __cplusplus
4344
extern "C" {
4445
#endif
4546

46-
typedef struct ethash_params {
47-
uint64_t full_size; // Size of full data set (in bytes, multiple of mix size (128)).
48-
uint64_t cache_size; // Size of compute cache (in bytes, multiple of node size (64)).
49-
} ethash_params;
47+
/// Type of a seedhash/blockhash e.t.c.
48+
typedef struct ethash_h256 { uint8_t b[32]; } ethash_h256_t;
5049

51-
typedef struct ethash_return_value {
52-
uint8_t result[32];
53-
uint8_t mix_hash[32];
54-
} ethash_return_value;
55-
56-
uint64_t ethash_get_datasize(const uint32_t block_number);
57-
uint64_t ethash_get_cachesize(const uint32_t block_number);
58-
59-
// initialize the parameters
60-
static inline void ethash_params_init(ethash_params *params, const uint32_t block_number) {
61-
params->full_size = ethash_get_datasize(block_number);
62-
params->cache_size = ethash_get_cachesize(block_number);
63-
}
64-
65-
/***********************************
66-
* OLD API *************************
67-
***********************************
68-
******************** (deprecated) *
69-
***********************************/
50+
// convenience macro to statically initialize an h256_t
51+
// usage:
52+
// ethash_h256_t a = ethash_h256_static_init(1, 2, 3, ... )
53+
// have to provide all 32 values. If you don't provide all the rest
54+
// will simply be unitialized (not guranteed to be 0)
55+
#define ethash_h256_static_init(...) \
56+
{ {__VA_ARGS__} }
7057

71-
void ethash_get_seedhash(uint8_t seedhash[32], const uint32_t block_number);
72-
void ethash_mkcache(void *cache, ethash_params const *params, const uint8_t seed[32]);
73-
void ethash_light(ethash_return_value *ret, void const *cache, ethash_params const *params, const uint8_t header_hash[32], const uint64_t nonce);
74-
void ethash_compute_full_data(void *mem, ethash_params const *params, void const *cache);
75-
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);
58+
struct ethash_light;
59+
typedef struct ethash_light* ethash_light_t;
60+
struct ethash_full;
61+
typedef struct ethash_full* ethash_full_t;
62+
typedef int(*ethash_callback_t)(unsigned);
7663

77-
/***********************************
78-
* NEW API *************************
79-
***********************************/
80-
81-
// TODO: compute params and seed in ethash_new_light; it should take only block_number
82-
// TODO: store params in ethash_light_t/ethash_full_t to avoid having to repass into compute/new_full
83-
84-
typedef uint8_t const ethash_seedhash_t[32];
85-
86-
typedef void const* ethash_light_t;
87-
static inline ethash_light_t ethash_new_light(ethash_params const* params, ethash_seedhash_t seed) {
88-
void* ret = malloc((size_t)params->cache_size);
89-
ethash_mkcache(ret, params, seed);
90-
return ret;
91-
}
92-
static inline void ethash_compute_light(ethash_return_value *ret, ethash_light_t light, ethash_params const *params, const uint8_t header_hash[32], const uint64_t nonce) {
93-
ethash_light(ret, light, params, header_hash, nonce);
94-
}
95-
static inline void ethash_delete_light(ethash_light_t light) {
96-
free((void*)light);
97-
}
98-
99-
typedef void const* ethash_full_t;
100-
static inline ethash_full_t ethash_new_full(ethash_params const* params, ethash_light_t light) {
101-
void* ret = malloc((size_t)params->full_size);
102-
ethash_compute_full_data(ret, params, light);
103-
return ret;
104-
}
105-
static inline void ethash_prep_full(void *full, ethash_params const *params, void const *cache) {
106-
ethash_compute_full_data(full, params, cache);
107-
}
108-
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) {
109-
ethash_full(ret, full, params, header_hash, nonce);
110-
}
111-
112-
/// @brief Compare two s256-bit big-endian values.
113-
/// @returns 1 if @a a is less than or equal to @a b, 0 otherwise.
114-
/// Both parameters are 256-bit big-endian values.
115-
static inline int ethash_leq_be256(const uint8_t a[32], const uint8_t b[32]) {
116-
// Boundary is big endian
117-
for (int i = 0; i < 32; i++) {
118-
if (a[i] == b[i])
119-
continue;
120-
return a[i] < b[i];
121-
}
122-
return 1;
123-
}
124-
125-
/// Perofrms a cursory check on the validity of the nonce.
126-
/// @returns 1 if the nonce may possibly be valid for the given header_hash & boundary.
127-
/// @p boundary equivalent to 2 ^ 256 / block_difficulty, represented as a 256-bit big-endian.
128-
int ethash_preliminary_check_boundary(
129-
const uint8_t header_hash[32],
130-
const uint64_t nonce,
131-
const uint8_t mix_hash[32],
132-
const uint8_t boundary[32]);
133-
134-
#define ethash_quick_check_difficulty ethash_preliminary_check_boundary
135-
#define ethash_check_difficulty ethash_leq_be256
64+
typedef struct ethash_return_value {
65+
ethash_h256_t result;
66+
ethash_h256_t mix_hash;
67+
bool success;
68+
} ethash_return_value_t;
69+
70+
/**
71+
* Allocate and initialize a new ethash_light handler
72+
*
73+
* @param block_number The block number for which to create the handler
74+
* @return Newly allocated ethash_light handler or NULL in case of
75+
* ERRNOMEM or invalid parameters used for @ref ethash_compute_cache_nodes()
76+
*/
77+
ethash_light_t ethash_light_new(uint64_t block_number);
78+
/**
79+
* Frees a previously allocated ethash_light handler
80+
* @param light The light handler to free
81+
*/
82+
void ethash_light_delete(ethash_light_t light);
83+
/**
84+
* Calculate the light client data
85+
*
86+
* @param light The light client handler
87+
* @param header_hash The header hash to pack into the mix
88+
* @param nonce The nonce to pack into the mix
89+
* @return an object of ethash_return_value_t holding the return values
90+
*/
91+
ethash_return_value_t ethash_light_compute(
92+
ethash_light_t light,
93+
ethash_h256_t const header_hash,
94+
uint64_t nonce
95+
);
96+
97+
/**
98+
* Allocate and initialize a new ethash_full handler
99+
*
100+
* @param light The light handler containing the cache.
101+
* @param callback A callback function with signature of @ref ethash_callback_t
102+
* It accepts an unsigned with which a progress of DAG calculation
103+
* can be displayed. If all goes well the callback should return 0.
104+
* If a non-zero value is returned then DAG generation will stop.
105+
* Be advised. A progress value of 100 means that DAG creation is
106+
* almost complete and that this function will soon return succesfully.
107+
* It does not mean that the function has already had a succesfull return.
108+
* @return Newly allocated ethash_full handler or NULL in case of
109+
* ERRNOMEM or invalid parameters used for @ref ethash_compute_full_data()
110+
*/
111+
ethash_full_t ethash_full_new(ethash_light_t light, ethash_callback_t callback);
112+
113+
/**
114+
* Frees a previously allocated ethash_full handler
115+
* @param full The light handler to free
116+
*/
117+
void ethash_full_delete(ethash_full_t full);
118+
/**
119+
* Calculate the full client data
120+
*
121+
* @param full The full client handler
122+
* @param header_hash The header hash to pack into the mix
123+
* @param nonce The nonce to pack into the mix
124+
* @return An object of ethash_return_value to hold the return value
125+
*/
126+
ethash_return_value_t ethash_full_compute(
127+
ethash_full_t full,
128+
ethash_h256_t const header_hash,
129+
uint64_t nonce
130+
);
131+
/**
132+
* Get a pointer to the full DAG data
133+
*/
134+
void const* ethash_full_dag(ethash_full_t full);
135+
/**
136+
* Get the size of the DAG data
137+
*/
138+
uint64_t ethash_full_dag_size(ethash_full_t full);
139+
140+
/**
141+
* Calculate the seedhash for a given block number
142+
*/
143+
ethash_h256_t ethash_get_seedhash(uint64_t block_number);
136144

137145
#ifdef __cplusplus
138146
}

‎libethash/fnv.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ extern "C" {
2929

3030
#define FNV_PRIME 0x01000193
3131

32-
static inline uint32_t fnv_hash(const uint32_t x, const uint32_t y) {
33-
return x*FNV_PRIME ^ y;
32+
static inline uint32_t fnv_hash(uint32_t const x, uint32_t const y)
33+
{
34+
return x * FNV_PRIME ^ y;
3435
}
3536

3637
#ifdef __cplusplus
3738
}
38-
#endif
39+
#endif

0 commit comments

Comments
 (0)
This repository has been archived.