Updated ethash

This commit is contained in:
obscuren 2015-04-07 13:17:27 +02:00
parent c756633fb7
commit 01b2c90179
16 changed files with 313 additions and 259 deletions

4
Godeps/Godeps.json generated
View File

@ -22,8 +22,8 @@
},
{
"ImportPath": "github.com/ethereum/ethash",
"Comment": "v23.1-73-g67a0e12",
"Rev": "67a0e12a091de035ef083186247e84be2d863c62"
"Comment": "v23.1-81-g4039fd0",
"Rev": "4039fd095084679fb0bf3feae91d02506b5d67aa"
},
{
"ImportPath": "github.com/ethereum/serpent-go",

View File

@ -34,13 +34,12 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
"github.com/ethereum/go-ethereum/pow"
)
var minDifficulty = new(big.Int).Exp(big.NewInt(2), big.NewInt(256), big.NewInt(0))
var powlogger = logger.NewLogger("POW")
type ParamsAndCache struct {
params *C.ethash_params
cache *C.ethash_cache
@ -92,10 +91,13 @@ func makeParamsAndCache(chainManager pow.ChainManager, blockNum uint64) (*Params
return nil, err
}
powlogger.Infoln("Making Cache")
glog.V(logger.Info).Infoln("Making cache")
start := time.Now()
C.ethash_mkcache(paramsAndCache.cache, paramsAndCache.params, (*C.uint8_t)(unsafe.Pointer(&seedHash[0])))
powlogger.Infoln("Took:", time.Since(start))
C.ethash_mkcache(paramsAndCache.cache, paramsAndCache.params, (*C.ethash_blockhash_t)(unsafe.Pointer(&seedHash[0])))
if glog.V(logger.Info) {
glog.Infoln("Took:", time.Since(start))
}
return paramsAndCache, nil
}
@ -131,9 +133,9 @@ func makeDAG(p *ParamsAndCache) *DAG {
for {
select {
case <-t.C:
powlogger.Infof("... still generating DAG (%v) ...\n", time.Since(tstart).Seconds())
glog.V(logger.Info).Infof("... still generating DAG (%v) ...\n", time.Since(tstart).Seconds())
case str := <-donech:
powlogger.Infof("... %s ...\n", str)
glog.V(logger.Info).Infof("... %s ...\n", str)
break done
}
}
@ -193,30 +195,30 @@ func (pow *Ethash) UpdateDAG() {
pow.paramsAndCache = paramsAndCache
path := path.Join("/", "tmp", "dag")
pow.dag = nil
powlogger.Infoln("Retrieving DAG")
glog.V(logger.Info).Infoln("Retrieving DAG")
start := time.Now()
file, err := os.Open(path)
if err != nil {
powlogger.Infof("No DAG found. Generating new DAG in '%s' (this takes a while)...\n", path)
glog.V(logger.Info).Infof("No DAG found. Generating new DAG in '%s' (this takes a while)...\n", path)
pow.dag = makeDAG(paramsAndCache)
file = pow.writeDagToDisk(pow.dag, thisEpoch)
pow.dag.file = true
} else {
data, err := ioutil.ReadAll(file)
if err != nil {
powlogger.Infof("DAG load err: %v\n", err)
glog.V(logger.Info).Infof("DAG load err: %v\n", err)
}
if len(data) < 8 {
powlogger.Infof("DAG in '%s' is less than 8 bytes, it must be corrupted. Generating new DAG (this takes a while)...\n", path)
glog.V(logger.Info).Infof("DAG in '%s' is less than 8 bytes, it must be corrupted. Generating new DAG (this takes a while)...\n", path)
pow.dag = makeDAG(paramsAndCache)
file = pow.writeDagToDisk(pow.dag, thisEpoch)
pow.dag.file = true
} else {
dataEpoch := binary.BigEndian.Uint64(data[0:8])
if dataEpoch < thisEpoch {
powlogger.Infof("DAG in '%s' is stale. Generating new DAG (this takes a while)...\n", path)
glog.V(logger.Info).Infof("DAG in '%s' is stale. Generating new DAG (this takes a while)...\n", path)
pow.dag = makeDAG(paramsAndCache)
file = pow.writeDagToDisk(pow.dag, thisEpoch)
pow.dag.file = true
@ -224,7 +226,7 @@ func (pow *Ethash) UpdateDAG() {
// FIXME
panic(fmt.Errorf("Saved DAG in '%s' reports to be from future epoch %v (current epoch is %v)\n", path, dataEpoch, thisEpoch))
} else if len(data) != (int(paramsAndCache.params.full_size) + 8) {
powlogger.Infof("DAG in '%s' is corrupted. Generating new DAG (this takes a while)...\n", path)
glog.V(logger.Info).Infof("DAG in '%s' is corrupted. Generating new DAG (this takes a while)...\n", path)
pow.dag = makeDAG(paramsAndCache)
file = pow.writeDagToDisk(pow.dag, thisEpoch)
pow.dag.file = true
@ -238,7 +240,7 @@ func (pow *Ethash) UpdateDAG() {
}
}
}
powlogger.Infoln("Took:", time.Since(start))
glog.V(logger.Info).Infoln("Took:", time.Since(start))
file.Close()
}
@ -319,7 +321,7 @@ func (pow *Ethash) Search(block pow.Block, stop <-chan struct{}) (uint64, []byte
start := time.Now().UnixNano()
nonce := uint64(r.Int63())
cMiningHash := (*C.uint8_t)(unsafe.Pointer(&miningHash[0]))
cMiningHash := (*C.ethash_blockhash_t)(unsafe.Pointer(&miningHash[0]))
target := new(big.Int).Div(minDifficulty, diff)
var ret C.ethash_return_value
@ -336,11 +338,11 @@ func (pow *Ethash) Search(block pow.Block, stop <-chan struct{}) (uint64, []byte
pow.HashRate = int64(hashes)
C.ethash_full(&ret, pow.dag.dag, pow.dag.paramsAndCache.params, cMiningHash, C.uint64_t(nonce))
result := common.Bytes2Big(C.GoBytes(unsafe.Pointer(&ret.result[0]), C.int(32)))
result := common.Bytes2Big(C.GoBytes(unsafe.Pointer(&ret.result), C.int(32)))
// TODO: disagrees with the spec https://github.com/ethereum/wiki/wiki/Ethash#mining
if result.Cmp(target) <= 0 {
mixDigest := C.GoBytes(unsafe.Pointer(&ret.mix_hash[0]), C.int(32))
mixDigest := C.GoBytes(unsafe.Pointer(&ret.mix_hash), C.int(32))
seedHash, err := GetSeedHash(block.NumberU64()) // This seedhash is useless
if err != nil {
panic(err)
@ -366,14 +368,14 @@ func (pow *Ethash) Verify(block pow.Block) bool {
func (pow *Ethash) verify(hash common.Hash, mixDigest common.Hash, difficulty *big.Int, blockNum uint64, nonce uint64) bool {
// Make sure the block num is valid
if blockNum >= epochLength*2048 {
powlogger.Infoln(fmt.Sprintf("Block number exceeds limit, invalid (value is %v, limit is %v)",
glog.V(logger.Info).Infoln(fmt.Sprintf("Block number exceeds limit, invalid (value is %v, limit is %v)",
blockNum, epochLength*2048))
return false
}
// First check: make sure header, mixDigest, nonce are correct without hitting the cache
// This is to prevent DOS attacks
chash := (*C.uint8_t)(unsafe.Pointer(&hash[0]))
chash := (*C.ethash_blockhash_t)(unsafe.Pointer(&hash[0]))
cnonce := C.uint64_t(nonce)
target := new(big.Int).Div(minDifficulty, difficulty)
@ -387,7 +389,7 @@ func (pow *Ethash) verify(hash common.Hash, mixDigest common.Hash, difficulty *b
// If we can't make the params for some reason, this block is invalid
pAc, err = makeParamsAndCache(pow.chainManager, blockNum+1)
if err != nil {
powlogger.Infoln("big fucking eror", err)
glog.V(logger.Info).Infoln("big fucking eror", err)
return false
}
} else {
@ -401,7 +403,7 @@ func (pow *Ethash) verify(hash common.Hash, mixDigest common.Hash, difficulty *b
C.ethash_light(ret, pAc.cache, pAc.params, chash, cnonce)
result := common.Bytes2Big(C.GoBytes(unsafe.Pointer(&ret.result[0]), C.int(32)))
result := common.Bytes2Big(C.GoBytes(unsafe.Pointer(&ret.result), C.int(32)))
return result.Cmp(target) <= 0
}
@ -417,7 +419,7 @@ func (pow *Ethash) FullHash(nonce uint64, miningHash []byte) []byte {
pow.UpdateDAG()
pow.dagMutex.Lock()
defer pow.dagMutex.Unlock()
cMiningHash := (*C.uint8_t)(unsafe.Pointer(&miningHash[0]))
cMiningHash := (*C.ethash_blockhash_t)(unsafe.Pointer(&miningHash[0]))
cnonce := C.uint64_t(nonce)
ret := new(C.ethash_return_value)
// pow.hash is the output/return of ethash_full
@ -427,7 +429,7 @@ func (pow *Ethash) FullHash(nonce uint64, miningHash []byte) []byte {
}
func (pow *Ethash) LightHash(nonce uint64, miningHash []byte) []byte {
cMiningHash := (*C.uint8_t)(unsafe.Pointer(&miningHash[0]))
cMiningHash := (*C.ethash_blockhash_t)(unsafe.Pointer(&miningHash[0]))
cnonce := C.uint64_t(nonce)
ret := new(C.ethash_return_value)
C.ethash_light(ret, pow.paramsAndCache.cache, pow.paramsAndCache.params, cMiningHash, cnonce)

View File

@ -25,9 +25,10 @@ setup (
author="Matthew Wampler-Doty",
author_email="matthew.wampler.doty@gmail.com",
license='GPL',
version = '23',
version='0.1.23',
url='https://github.com/ethereum/ethash',
download_url='https://github.com/ethereum/ethash/tarball/v23',
description = 'Python wrappers for ethash, the ethereum proof of work hashing function',
description=('Python wrappers for ethash, the ethereum proof of work'
'hashing function'),
ext_modules=[pyethash],
)

View File

@ -106,10 +106,11 @@ extern "C" int main(void)
//params.full_size = 8209 * 4096; // 8MBish;
//params.cache_size = 8209*4096;
//params.cache_size = 2053*4096;
uint8_t seed[32], previous_hash[32];
ethash_blockhash_t seed;
ethash_blockhash_t previous_hash;
memcpy(seed, hexStringToBytes("9410b944535a83d9adf6bbdcc80e051f30676173c16ca0d32d6f1263fc246466").data(), 32);
memcpy(previous_hash, hexStringToBytes("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470").data(), 32);
memcpy(&seed, hexStringToBytes("9410b944535a83d9adf6bbdcc80e051f30676173c16ca0d32d6f1263fc246466").data(), 32);
memcpy(&previous_hash, hexStringToBytes("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470").data(), 32);
// allocate page aligned buffer for dataset
#ifdef FULL
@ -128,9 +129,9 @@ extern "C" int main(void)
ethash_mkcache(&cache, &params, seed);
auto time = std::chrono::duration_cast<std::chrono::milliseconds>(high_resolution_clock::now() - startTime).count();
uint8_t cache_hash[32];
SHA3_256(cache_hash, (uint8_t const*)cache_mem, params.cache_size);
debugf("ethash_mkcache: %ums, sha3: %s\n", (unsigned)time, bytesToHexString(cache_hash,sizeof(cache_hash)).data());
ethash_blockhash_t cache_hash;
SHA3_256(&cache_hash, (uint8_t const*)cache_mem, params.cache_size);
debugf("ethash_mkcache: %ums, sha3: %s\n", (unsigned)((time*1000)/CLOCKS_PER_SEC), bytesToHexString(cache_hash,sizeof(cache_hash)).data());
// print a couple of test hashes
{

View File

@ -54,15 +54,7 @@ ethash_cl_miner::ethash_cl_miner()
{
}
void ethash_cl_miner::finish()
{
if (m_queue())
{
m_queue.finish();
}
}
bool ethash_cl_miner::init(ethash_params const& params, const uint8_t seed[32], unsigned workgroup_size)
bool ethash_cl_miner::init(ethash_params const& params, ethash_blockhash_t const *seed, unsigned workgroup_size)
{
// store params
m_params = params;

View File

@ -19,7 +19,7 @@ public:
public:
ethash_cl_miner();
bool init(ethash_params const& params, const uint8_t seed[32], unsigned workgroup_size = 64);
bool init(ethash_params const& params, ethash_blockhash_t const *seed, unsigned workgroup_size = 64);
void finish();
void hash(uint8_t* ret, uint8_t const* header, uint64_t nonce, unsigned count);

View File

@ -47,9 +47,26 @@ typedef struct ethash_params {
uint64_t cache_size; // Size of compute cache (in bytes, multiple of node size (64)).
} ethash_params;
/// Type of a blockhash
typedef struct ethash_blockhash { uint8_t b[32]; } ethash_blockhash_t;
static inline uint8_t ethash_blockhash_get(ethash_blockhash_t const* hash, unsigned int i)
{
return hash->b[i];
}
static inline void ethash_blockhash_set(ethash_blockhash_t *hash, unsigned int i, uint8_t v)
{
hash->b[i] = v;
}
static inline void ethash_blockhash_reset(ethash_blockhash_t *hash)
{
memset(hash, 0, 32);
}
typedef struct ethash_return_value {
uint8_t result[32];
uint8_t mix_hash[32];
ethash_blockhash_t result;
ethash_blockhash_t mix_hash;
} ethash_return_value;
uint64_t ethash_get_datasize(const uint32_t block_number);
@ -65,58 +82,68 @@ 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_mkcache(ethash_cache *cache, ethash_params const *params, ethash_blockhash_t const *seed);
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);
void ethash_get_seedhash(uint8_t seedhash[32], const uint32_t block_number);
void ethash_full(ethash_return_value *ret,
void const *full_mem,
ethash_params const *params,
ethash_blockhash_t const *header_hash,
const uint64_t nonce);
void ethash_light(ethash_return_value *ret,
ethash_cache const *cache,
ethash_params const *params,
ethash_blockhash_t const *header_hash,
const uint64_t nonce);
void ethash_get_seedhash(ethash_blockhash_t *seedhash, const uint32_t block_number);
static inline void ethash_prep_light(void *cache, ethash_params const *params, const uint8_t seed[32]) {
static inline void ethash_prep_light(void *cache, ethash_params const *params, ethash_blockhash_t const* seed)
{
ethash_cache c;
c.mem = cache;
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 void ethash_compute_light(ethash_return_value *ret, void const *cache, ethash_params const *params, ethash_blockhash_t const *header_hash, const uint64_t nonce)
{
ethash_cache c;
c.mem = (void *) cache;
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 void 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);
}
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) {
static inline void ethash_compute_full(ethash_return_value *ret,
void const *full,
ethash_params const *params,
ethash_blockhash_t const *header_hash,
const uint64_t nonce)
{
ethash_full(ret, full, params, header_hash, nonce);
}
/// @brief Compare two s256-bit big-endian values.
/// @returns 1 if @a a is less than or equal to @a b, 0 otherwise.
/// Both parameters are 256-bit big-endian values.
static inline int ethash_leq_be256(const uint8_t a[32], const uint8_t b[32]) {
// Boundary is big endian
// Returns if hash is less than or equal to difficulty
static inline int ethash_check_difficulty(ethash_blockhash_t const *hash,
ethash_blockhash_t const *difficulty)
{
// Difficulty is big endian
for (int i = 0; i < 32; i++) {
if (a[i] == b[i])
if (ethash_blockhash_get(hash, i) == ethash_blockhash_get(difficulty, i)) {
continue;
return a[i] < b[i];
}
return ethash_blockhash_get(hash, i) < ethash_blockhash_get(difficulty, i);
}
return 1;
}
/// 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],
int ethash_quick_check_difficulty(ethash_blockhash_t const *header_hash,
const uint64_t nonce,
const uint8_t mix_hash[32],
const uint8_t boundary[32]);
#define ethash_quick_check_difficulty ethash_preliminary_check_boundary
#define ethash_check_difficulty ethash_leq_be256
ethash_blockhash_t const *mix_hash,
ethash_blockhash_t const *difficulty);
#ifdef __cplusplus
}

View File

@ -50,14 +50,14 @@ uint64_t ethash_get_cachesize(const uint32_t block_number) {
// 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(
node *const nodes,
void static ethash_compute_cache_nodes(node *const nodes,
ethash_params const *params,
const uint8_t seed[32]) {
ethash_blockhash_t const* seed)
{
assert((params->cache_size % sizeof(node)) == 0);
uint32_t const num_nodes = (uint32_t) (params->cache_size / sizeof(node));
SHA3_512(nodes[0].bytes, seed, 32);
SHA3_512(nodes[0].bytes, (uint8_t*)seed, 32);
for (unsigned i = 1; i != num_nodes; ++i) {
SHA3_512(nodes[i].bytes, nodes[i - 1].bytes, 64);
@ -84,20 +84,19 @@ void static ethash_compute_cache_nodes(
#endif
}
void ethash_mkcache(
ethash_cache *cache,
void ethash_mkcache(ethash_cache *cache,
ethash_params const *params,
const uint8_t seed[32]) {
ethash_blockhash_t const* seed)
{
node *nodes = (node *) cache->mem;
ethash_compute_cache_nodes(nodes, params, seed);
}
void ethash_calculate_dag_item(
node *const ret,
void ethash_calculate_dag_item(node *const ret,
const unsigned node_index,
const struct ethash_params *params,
const struct ethash_cache *cache) {
const struct ethash_cache *cache)
{
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];
@ -161,13 +160,13 @@ void ethash_compute_full_data(
}
}
static void ethash_hash(
ethash_return_value *ret,
static void 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) {
ethash_blockhash_t const *header_hash,
const uint64_t nonce)
{
assert((params->full_size % MIX_WORDS) == 0);
@ -251,16 +250,16 @@ static void ethash_hash(
}
#endif
memcpy(ret->mix_hash, mix->bytes, 32);
memcpy(&ret->mix_hash, mix->bytes, 32);
// final Keccak hash
SHA3_256(ret->result, s_mix->bytes, 64 + 32); // Keccak-256(s + compressed_mix)
SHA3_256(&ret->result, s_mix->bytes, 64 + 32); // Keccak-256(s + compressed_mix)
}
void ethash_quick_hash(
uint8_t return_hash[32],
const uint8_t header_hash[32],
void ethash_quick_hash(ethash_blockhash_t *return_hash,
ethash_blockhash_t const *header_hash,
const uint64_t nonce,
const uint8_t mix_hash[32]) {
ethash_blockhash_t const *mix_hash)
{
uint8_t buf[64 + 32];
memcpy(buf, header_hash, 32);
@ -273,28 +272,39 @@ void ethash_quick_hash(
SHA3_256(return_hash, buf, 64 + 32);
}
void ethash_get_seedhash(uint8_t seedhash[32], const uint32_t block_number) {
memset(seedhash, 0, 32);
void ethash_get_seedhash(ethash_blockhash_t *seedhash, const uint32_t block_number)
{
ethash_blockhash_reset(seedhash);
const uint32_t epochs = block_number / EPOCH_LENGTH;
for (uint32_t i = 0; i < epochs; ++i)
SHA3_256(seedhash, seedhash, 32);
SHA3_256(seedhash, (uint8_t*)seedhash, 32);
}
int ethash_preliminary_check_boundary(
const uint8_t header_hash[32],
int ethash_quick_check_difficulty(ethash_blockhash_t const *header_hash,
const uint64_t nonce,
const uint8_t mix_hash[32],
const uint8_t difficulty[32]) {
ethash_blockhash_t const *mix_hash,
ethash_blockhash_t const *difficulty)
{
uint8_t return_hash[32];
ethash_quick_hash(return_hash, header_hash, nonce, mix_hash);
return ethash_leq_be256(return_hash, difficulty);
ethash_blockhash_t return_hash;
ethash_quick_hash(&return_hash, header_hash, nonce, mix_hash);
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);
void ethash_full(ethash_return_value *ret,
void const *full_mem,
ethash_params const *params,
ethash_blockhash_t const *header_hash,
const uint64_t nonce)
{
ethash_hash(ret, (node const *) full_mem, NULL, params, header_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);
void ethash_light(ethash_return_value *ret,
ethash_cache const *cache,
ethash_params const *params,
ethash_blockhash_t const *header_hash,
const uint64_t nonce)
{
ethash_hash(ret, NULL, cache, params, header_hash, nonce);
}

View File

@ -30,18 +30,15 @@ typedef union node {
} node;
void ethash_calculate_dag_item(
node *const ret,
void ethash_calculate_dag_item(node *const ret,
const unsigned node_index,
ethash_params const *params,
ethash_cache const *cache
);
ethash_cache const *cache);
void ethash_quick_hash(
uint8_t return_hash[32],
const uint8_t header_hash[32],
void ethash_quick_hash(ethash_blockhash_t *return_hash,
ethash_blockhash_t const *header_hash,
const uint64_t nonce,
const uint8_t mix_hash[32]);
ethash_blockhash_t const *mix_hash);
#ifdef __cplusplus
}

View File

@ -28,8 +28,6 @@
extern "C" {
#endif
typedef struct ethash_blockhash { uint8_t b[32]; } ethash_blockhash_t;
static const char DAG_FILE_NAME[] = "full";
static const char DAG_MEMO_NAME[] = "full.info";
// MSVC thinks that "static const unsigned int" is not a compile time variable. Sorry for the #define :(
@ -54,6 +52,7 @@ enum ethash_io_rc {
* @return For possible return values @see enum ethash_io_rc
*/
enum ethash_io_rc ethash_io_prepare(char const *dirname, ethash_blockhash_t seedhash);
/**
* Fully computes data and writes it to the file on disk.
*

View File

@ -8,17 +8,21 @@ extern "C" {
#include <stdint.h>
#include <stdlib.h>
struct ethash_blockhash;
#define decsha3(bits) \
int sha3_##bits(uint8_t*, size_t, const uint8_t*, size_t);
decsha3(256)
decsha3(512)
static inline void SHA3_256(uint8_t * const ret, uint8_t const *data, const size_t size) {
sha3_256(ret, 32, data, size);
static inline void SHA3_256(struct ethash_blockhash const* ret, uint8_t const *data, const size_t size)
{
sha3_256((uint8_t*)ret, 32, data, size);
}
static inline void SHA3_512(uint8_t * const ret, uint8_t const *data, const size_t size) {
static inline void SHA3_512(uint8_t *ret, uint8_t const *data, const size_t size)
{
sha3_512(ret, 64, data, size);
}

View File

@ -19,13 +19,14 @@
* @author Tim Hughes <tim@twistedfury.com>
* @date 2015
*/
#include <stdint.h>
#include <cryptopp/sha3.h>
extern "C" {
void SHA3_256(uint8_t *const ret, const uint8_t *data, size_t size) {
CryptoPP::SHA3_256().CalculateDigest(ret, data, size);
struct ethash_blockhash;
typedef struct ethash_blockhash ethash_blockhash_t;
void SHA3_256(ethash_blockhash_t const* ret, const uint8_t *data, size_t size) {
CryptoPP::SHA3_256().CalculateDigest((uint8_t*)ret, data, size);
}
void SHA3_512(uint8_t *const ret, const uint8_t *data, size_t size) {

View File

@ -2,12 +2,16 @@
#include "compiler.h"
#include <stdint.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
void SHA3_256(uint8_t *const ret, const uint8_t *data, size_t size);
struct ethash_blockhash;
typedef struct ethash_blockhash ethash_blockhash_t;
void SHA3_256(ethash_blockhash_t *const ret, const uint8_t *data, size_t size);
void SHA3_512(uint8_t *const ret, const uint8_t *data, size_t size);
#ifdef __cplusplus

View File

@ -69,7 +69,7 @@ mkcache_bytes(PyObject *self, PyObject *args) {
params.cache_size = (size_t) cache_size;
ethash_cache cache;
cache.mem = malloc(cache_size);
ethash_mkcache(&cache, &params, (uint8_t *) seed);
ethash_mkcache(&cache, &params, (ethash_blockhash_t *) seed);
PyObject * val = Py_BuildValue(PY_STRING_FORMAT, cache.mem, cache_size);
free(cache.mem);
return val;
@ -114,28 +114,25 @@ calc_dataset_bytes(PyObject *self, PyObject *args) {
// hashimoto_light(full_size, cache, header, nonce)
static PyObject *
hashimoto_light(PyObject *self, PyObject *args) {
char *cache_bytes, *header;
char *cache_bytes;
char *header;
unsigned long full_size;
unsigned long long nonce;
int cache_size, header_size;
if (!PyArg_ParseTuple(args, "k" PY_STRING_FORMAT PY_STRING_FORMAT "K", &full_size, &cache_bytes, &cache_size, &header, &header_size, &nonce))
return 0;
if (full_size % MIX_WORDS != 0) {
char error_message[1024];
sprintf(error_message, "The size of data set must be a multiple of %i bytes (was %lu)", MIX_WORDS, full_size);
PyErr_SetString(PyExc_ValueError, error_message);
return 0;
}
if (cache_size % HASH_BYTES != 0) {
char error_message[1024];
sprintf(error_message, "The size of the cache must be a multiple of %i bytes (was %i)", HASH_BYTES, cache_size);
PyErr_SetString(PyExc_ValueError, error_message);
return 0;
}
if (header_size != 32) {
char error_message[1024];
sprintf(error_message, "Seed must be 32 bytes long (was %i)", header_size);
@ -143,23 +140,23 @@ hashimoto_light(PyObject *self, PyObject *args) {
return 0;
}
ethash_return_value out;
ethash_params params;
params.cache_size = (size_t) cache_size;
params.full_size = (size_t) full_size;
ethash_cache cache;
cache.mem = (void *) cache_bytes;
ethash_light(&out, &cache, &params, (uint8_t *) header, nonce);
ethash_light(&out, &cache, &params, (ethash_blockhash_t *) header, nonce);
return Py_BuildValue("{" PY_CONST_STRING_FORMAT ":" PY_STRING_FORMAT "," PY_CONST_STRING_FORMAT ":" PY_STRING_FORMAT "}",
"mix digest", out.mix_hash, 32,
"result", out.result, 32);
"mix digest", &out.mix_hash, 32,
"result", &out.result, 32);
}
// hashimoto_full(dataset, header, nonce)
static PyObject *
hashimoto_full(PyObject *self, PyObject *args) {
char *full_bytes, *header;
char *full_bytes;
char *header;
unsigned long long nonce;
int full_size, header_size;
@ -184,16 +181,18 @@ hashimoto_full(PyObject *self, PyObject *args) {
ethash_return_value out;
ethash_params params;
params.full_size = (size_t) full_size;
ethash_full(&out, (void *) full_bytes, &params, (uint8_t *) header, nonce);
ethash_full(&out, (void *) full_bytes, &params, (ethash_blockhash_t *) header, nonce);
return Py_BuildValue("{" PY_CONST_STRING_FORMAT ":" PY_STRING_FORMAT ", " PY_CONST_STRING_FORMAT ":" PY_STRING_FORMAT "}",
"mix digest", out.mix_hash, 32,
"result", out.result, 32);
"mix digest", &out.mix_hash, 32,
"result", &out.result, 32);
}
// mine(dataset_bytes, header, difficulty_bytes)
static PyObject *
mine(PyObject *self, PyObject *args) {
char *full_bytes, *header, *difficulty;
char *full_bytes;
char *header;
char *difficulty;
srand(time(0));
uint64_t nonce = ((uint64_t) rand()) << 32 | rand();
int full_size, header_size, difficulty_size;
@ -228,13 +227,13 @@ mine(PyObject *self, PyObject *args) {
// TODO: Multi threading?
do {
ethash_full(&out, (void *) full_bytes, &params, (const uint8_t *) header, nonce++);
ethash_full(&out, (void *) full_bytes, &params, (const ethash_blockhash_t *) header, nonce++);
// TODO: disagrees with the spec https://github.com/ethereum/wiki/wiki/Ethash#mining
} while (!ethash_check_difficulty(out.result, (const uint8_t *) difficulty));
} while (!ethash_check_difficulty(&out.result, (const ethash_blockhash_t *) difficulty));
return Py_BuildValue("{" PY_CONST_STRING_FORMAT ":" PY_STRING_FORMAT ", " PY_CONST_STRING_FORMAT ":" PY_STRING_FORMAT ", " PY_CONST_STRING_FORMAT ":K}",
"mix digest", out.mix_hash, 32,
"result", out.result, 32,
"mix digest", &out.mix_hash, 32,
"result", &out.result, 32,
"nonce", nonce);
}
@ -251,9 +250,9 @@ get_seedhash(PyObject *self, PyObject *args) {
PyErr_SetString(PyExc_ValueError, error_message);
return 0;
}
uint8_t seedhash[32];
ethash_get_seedhash(seedhash, block_number);
return Py_BuildValue(PY_STRING_FORMAT, (char *) seedhash, 32);
ethash_blockhash_t seedhash;
ethash_get_seedhash(&seedhash, block_number);
return Py_BuildValue(PY_STRING_FORMAT, (char *) &seedhash, 32);
}
static PyMethodDef PyethashMethods[] =

View File

@ -37,6 +37,10 @@ std::string bytesToHexString(const uint8_t *str, const uint64_t s) {
return ret.str();
}
std::string blockhashToHexString(ethash_blockhash_t *hash) {
return bytesToHexString((uint8_t*)hash, 32);
}
BOOST_AUTO_TEST_CASE(fnv_hash_check) {
uint32_t x = 1235U;
const uint32_t
@ -52,12 +56,13 @@ BOOST_AUTO_TEST_CASE(fnv_hash_check) {
}
BOOST_AUTO_TEST_CASE(SHA256_check) {
uint8_t input[32], out[32];
memcpy(input, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~", 32);
SHA3_256(out, input, 32);
ethash_blockhash_t input;
ethash_blockhash_t out;
memcpy(&input, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~", 32);
SHA3_256(&out, (uint8_t*)&input, 32);
const std::string
expected = "2b5ddf6f4d21c23de216f44d5e4bdc68e044b71897837ea74c83908be7037cd7",
actual = bytesToHexString(out, 32);
actual = bytesToHexString((uint8_t*)&out, 32);
BOOST_REQUIRE_MESSAGE(expected == actual,
"\nexpected: " << expected.c_str() << "\n"
<< "actual: " << actual.c_str() << "\n");
@ -104,23 +109,26 @@ BOOST_AUTO_TEST_CASE(ethash_params_init_genesis_calcifide_check) {
BOOST_AUTO_TEST_CASE(light_and_full_client_checks) {
ethash_params params;
uint8_t seed[32], hash[32], difficulty[32];
ethash_return_value light_out, full_out;
memcpy(seed, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~", 32);
memcpy(hash, "~~~X~~~~~~~~~~~~~~~~~~~~~~~~~~~~", 32);
ethash_blockhash_t seed;
ethash_blockhash_t hash;
ethash_blockhash_t difficulty;
ethash_return_value light_out;
ethash_return_value full_out;
memcpy(&seed, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~", 32);
memcpy(&hash, "~~~X~~~~~~~~~~~~~~~~~~~~~~~~~~~~", 32);
// Set the difficulty
difficulty[0] = 197;
difficulty[1] = 90;
ethash_blockhash_set(&difficulty, 0, 197);
ethash_blockhash_set(&difficulty, 1, 90);
for (int i = 2; i < 32; i++)
difficulty[i] = (uint8_t) 255;
ethash_blockhash_set(&difficulty, i, 255);
ethash_params_init(&params, 0);
params.cache_size = 1024;
params.full_size = 1024 * 32;
ethash_cache cache;
cache.mem = our_alloca(params.cache_size);
ethash_mkcache(&cache, &params, seed);
ethash_mkcache(&cache, &params, &seed);
node *full_mem = (node *) our_alloca(params.full_size);
ethash_compute_full_data(full_mem, &params, &cache);
@ -164,78 +172,76 @@ BOOST_AUTO_TEST_CASE(light_and_full_client_checks) {
{
uint64_t nonce = 0x7c7c597c;
ethash_full(&full_out, full_mem, &params, hash, nonce);
ethash_light(&light_out, &cache, &params, hash, nonce);
ethash_full(&full_out, full_mem, &params, &hash, nonce);
ethash_light(&light_out, &cache, &params, &hash, nonce);
const std::string
light_result_string = bytesToHexString(light_out.result, 32),
full_result_string = bytesToHexString(full_out.result, 32);
light_result_string = blockhashToHexString(&light_out.result),
full_result_string = blockhashToHexString(&full_out.result);
BOOST_REQUIRE_MESSAGE(light_result_string == full_result_string,
"\nlight result: " << light_result_string.c_str() << "\n"
<< "full result: " << full_result_string.c_str() << "\n");
const std::string
light_mix_hash_string = bytesToHexString(light_out.mix_hash, 32),
full_mix_hash_string = bytesToHexString(full_out.mix_hash, 32);
light_mix_hash_string = blockhashToHexString(&light_out.mix_hash),
full_mix_hash_string = blockhashToHexString(&full_out.mix_hash);
BOOST_REQUIRE_MESSAGE(full_mix_hash_string == light_mix_hash_string,
"\nlight mix hash: " << light_mix_hash_string.c_str() << "\n"
<< "full mix hash: " << full_mix_hash_string.c_str() << "\n");
uint8_t check_hash[32];
ethash_quick_hash(check_hash, hash, nonce, full_out.mix_hash);
const std::string check_hash_string = bytesToHexString(check_hash, 32);
ethash_blockhash_t check_hash;
ethash_quick_hash(&check_hash, &hash, nonce, &full_out.mix_hash);
const std::string check_hash_string = blockhashToHexString(&check_hash);
BOOST_REQUIRE_MESSAGE(check_hash_string == full_result_string,
"\ncheck hash string: " << check_hash_string.c_str() << "\n"
<< "full result: " << full_result_string.c_str() << "\n");
}
{
ethash_full(&full_out, full_mem, &params, hash, 5);
ethash_full(&full_out, full_mem, &params, &hash, 5);
std::string
light_result_string = bytesToHexString(light_out.result, 32),
full_result_string = bytesToHexString(full_out.result, 32);
light_result_string = blockhashToHexString(&light_out.result),
full_result_string = blockhashToHexString(&full_out.result);
BOOST_REQUIRE_MESSAGE(light_result_string != full_result_string,
"\nlight result and full result should differ: " << light_result_string.c_str() << "\n");
ethash_light(&light_out, &cache, &params, hash, 5);
light_result_string = bytesToHexString(light_out.result, 32);
ethash_light(&light_out, &cache, &params, &hash, 5);
light_result_string = blockhashToHexString(&light_out.result);
BOOST_REQUIRE_MESSAGE(light_result_string == full_result_string,
"\nlight result and full result should be the same\n"
<< "light result: " << light_result_string.c_str() << "\n"
<< "full result: " << full_result_string.c_str() << "\n");
std::string
light_mix_hash_string = bytesToHexString(light_out.mix_hash, 32),
full_mix_hash_string = bytesToHexString(full_out.mix_hash, 32);
light_mix_hash_string = blockhashToHexString(&light_out.mix_hash),
full_mix_hash_string = blockhashToHexString(&full_out.mix_hash);
BOOST_REQUIRE_MESSAGE(full_mix_hash_string == light_mix_hash_string,
"\nlight mix hash: " << light_mix_hash_string.c_str() << "\n"
<< "full mix hash: " << full_mix_hash_string.c_str() << "\n");
BOOST_REQUIRE_MESSAGE(ethash_check_difficulty(full_out.result, difficulty),
BOOST_REQUIRE_MESSAGE(ethash_check_difficulty(&full_out.result, &difficulty),
"ethash_check_difficulty failed"
);
BOOST_REQUIRE_MESSAGE(ethash_quick_check_difficulty(hash, 5U, full_out.mix_hash, difficulty),
BOOST_REQUIRE_MESSAGE(ethash_quick_check_difficulty(&hash, 5U, &full_out.mix_hash, &difficulty),
"ethash_quick_check_difficulty failed"
);
}
}
BOOST_AUTO_TEST_CASE(ethash_check_difficulty_check) {
uint8_t hash[32], target[32];
memset(hash, 0, 32);
memset(target, 0, 32);
memcpy(hash, "11111111111111111111111111111111", 32);
memcpy(target, "22222222222222222222222222222222", 32);
ethash_blockhash_t hash;
ethash_blockhash_t target;
memcpy(&hash, "11111111111111111111111111111111", 32);
memcpy(&target, "22222222222222222222222222222222", 32);
BOOST_REQUIRE_MESSAGE(
ethash_check_difficulty(hash, target),
"\nexpected \"" << std::string((char *) hash, 32).c_str() << "\" to have the same or less difficulty than \"" << std::string((char *) target, 32).c_str() << "\"\n");
ethash_check_difficulty(&hash, &target),
"\nexpected \"" << std::string((char *) &hash, 32).c_str() << "\" to have the same or less difficulty than \"" << std::string((char *) &target, 32).c_str() << "\"\n");
BOOST_REQUIRE_MESSAGE(
ethash_check_difficulty(hash, hash),
"\nexpected \"" << hash << "\" to have the same or less difficulty than \"" << hash << "\"\n");
memcpy(target, "11111111111111111111111111111112", 32);
ethash_check_difficulty(&hash, &hash), "");
// "\nexpected \"" << hash << "\" to have the same or less difficulty than \"" << hash << "\"\n");
memcpy(&target, "11111111111111111111111111111112", 32);
BOOST_REQUIRE_MESSAGE(
ethash_check_difficulty(hash, target),
"\nexpected \"" << hash << "\" to have the same or less difficulty than \"" << target << "\"\n");
memcpy(target, "11111111111111111111111111111110", 32);
ethash_check_difficulty(&hash, &target), "");
// "\nexpected \"" << hash << "\" to have the same or less difficulty than \"" << target << "\"\n");
memcpy(&target, "11111111111111111111111111111110", 32);
BOOST_REQUIRE_MESSAGE(
!ethash_check_difficulty(hash, target),
"\nexpected \"" << hash << "\" to have more difficulty than \"" << target << "\"\n");
!ethash_check_difficulty(&hash, &target), "");
// "\nexpected \"" << hash << "\" to have more difficulty than \"" << target << "\"\n");
}
BOOST_AUTO_TEST_CASE(test_ethash_dir_creation) {
@ -256,7 +262,7 @@ BOOST_AUTO_TEST_CASE(test_ethash_dir_creation) {
BOOST_AUTO_TEST_CASE(test_ethash_io_write_files_are_created) {
ethash_blockhash_t seedhash;
static const int blockn = 0;
ethash_get_seedhash((uint8_t*)&seedhash, blockn);
ethash_get_seedhash(&seedhash, blockn);
BOOST_REQUIRE_EQUAL(
ETHASH_IO_MEMO_MISMATCH,
ethash_io_prepare("./test_ethash_directory/", seedhash)
@ -273,7 +279,7 @@ BOOST_AUTO_TEST_CASE(test_ethash_io_write_files_are_created) {
params.cache_size = 1024;
params.full_size = 1024 * 32;
cache.mem = our_alloca(params.cache_size);
ethash_mkcache(&cache, &params, (uint8_t*)&seedhash);
ethash_mkcache(&cache, &params, &seedhash);
BOOST_REQUIRE(
ethash_io_write("./test_ethash_directory/", &params, seedhash, &cache, &data, &size)
@ -290,7 +296,7 @@ BOOST_AUTO_TEST_CASE(test_ethash_io_write_files_are_created) {
BOOST_AUTO_TEST_CASE(test_ethash_io_memo_file_match) {
ethash_blockhash_t seedhash;
static const int blockn = 0;
ethash_get_seedhash((uint8_t*)&seedhash, blockn);
ethash_get_seedhash(&seedhash, blockn);
BOOST_REQUIRE_EQUAL(
ETHASH_IO_MEMO_MISMATCH,
ethash_io_prepare("./test_ethash_directory/", seedhash)
@ -307,7 +313,7 @@ BOOST_AUTO_TEST_CASE(test_ethash_io_memo_file_match) {
params.cache_size = 1024;
params.full_size = 1024 * 32;
cache.mem = our_alloca(params.cache_size);
ethash_mkcache(&cache, &params, (uint8_t*)&seedhash);
ethash_mkcache(&cache, &params, &seedhash);
BOOST_REQUIRE(
ethash_io_write("./test_ethash_directory/", &params, seedhash, &cache, &data, &size)
@ -344,7 +350,7 @@ static std::vector<char> readFileIntoVector(char const* filename)
BOOST_AUTO_TEST_CASE(test_ethash_io_memo_file_contents) {
ethash_blockhash_t seedhash;
static const int blockn = 0;
ethash_get_seedhash((uint8_t*)&seedhash, blockn);
ethash_get_seedhash(&seedhash, blockn);
BOOST_REQUIRE_EQUAL(
ETHASH_IO_MEMO_MISMATCH,
ethash_io_prepare("./test_ethash_directory/", seedhash)
@ -361,7 +367,7 @@ BOOST_AUTO_TEST_CASE(test_ethash_io_memo_file_contents) {
params.cache_size = 1024;
params.full_size = 1024 * 32;
cache.mem = our_alloca(params.cache_size);
ethash_mkcache(&cache, &params, (uint8_t*)&seedhash);
ethash_mkcache(&cache, &params, &seedhash);
BOOST_REQUIRE(
ethash_io_write("./test_ethash_directory/", &params, seedhash, &cache, &data, &size)

View File

@ -3,6 +3,15 @@
# Strict mode
set -e
if [ -x "$(which virtualenv2)" ] ; then
VIRTUALENV_EXEC=virtualenv2
elif [ -x "$(which virtualenv)" ] ; then
VIRTUALENV_EXEC=virtualenv
else
echo "Could not find a suitable version of virtualenv"
false
fi
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
@ -11,9 +20,11 @@ while [ -h "$SOURCE" ]; do
done
TEST_DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
[ -d $TEST_DIR/python-virtual-env ] || virtualenv --system-site-packages $TEST_DIR/python-virtual-env
[ -d $TEST_DIR/python-virtual-env ] || $VIRTUALENV_EXEC --system-site-packages $TEST_DIR/python-virtual-env
source $TEST_DIR/python-virtual-env/bin/activate
pip install -r $TEST_DIR/requirements.txt > /dev/null
# force installation of nose in virtualenv even if existing in thereuser's system
pip install nose -I
pip install --upgrade --no-deps --force-reinstall -e $TEST_DIR/../..
cd $TEST_DIR
nosetests --with-doctest -v --nocapture