From 714c0fd7c90cdb11742f7f2c91a65357a7bf5d5a Mon Sep 17 00:00:00 2001 From: Jeff Garzik Date: Thu, 3 Feb 2011 00:46:55 -0500 Subject: [PATCH] Continue scanhash, even if high 32 bits are zero. Previously, we would stop the scan if the high 32 bits of the hash were zero, as a quick shortcut for testing the full hash. If this quick test succeeded, we would pass the work to the server for full validation. Change this logic to perform full validation inside minerd, so that work may be resumed more quickly if hash > target. --- cpu-miner.c | 10 ++++++---- miner.h | 7 ++++++- sha256_4way.c | 11 ++++++----- sha256_cryptopp.c | 10 +++++----- sha256_generic.c | 5 ++--- sha256_via.c | 5 ++--- util.c | 45 ++++++++++++++++++++++++++++++++++++++------- 7 files changed, 65 insertions(+), 28 deletions(-) diff --git a/cpu-miner.c b/cpu-miner.c index f9be60cd..c3924a9d 100644 --- a/cpu-miner.c +++ b/cpu-miner.c @@ -308,7 +308,7 @@ static void *miner_thread(void *thr_id_int) switch (opt_algo) { case ALGO_C: rc = scanhash_c(work.midstate, work.data + 64, - work.hash1, work.hash, + work.hash1, work.hash, work.target, max_nonce, &hashes_done); break; @@ -317,6 +317,7 @@ static void *miner_thread(void *thr_id_int) unsigned int rc4 = ScanHash_4WaySSE2(work.midstate, work.data + 64, work.hash1, work.hash, + work.target, max_nonce, &hashes_done); rc = (rc4 == -1) ? false : true; } @@ -325,19 +326,20 @@ static void *miner_thread(void *thr_id_int) #ifdef WANT_VIA_PADLOCK case ALGO_VIA: - rc = scanhash_via(work.data, max_nonce, &hashes_done); + rc = scanhash_via(work.data, work.target, + max_nonce, &hashes_done); break; #endif case ALGO_CRYPTOPP: rc = scanhash_cryptopp(work.midstate, work.data + 64, - work.hash1, work.hash, + work.hash1, work.hash, work.target, max_nonce, &hashes_done); break; #ifdef WANT_CRYPTOPP_ASM32 case ALGO_CRYPTOPP_ASM32: rc = scanhash_asm32(work.midstate, work.data + 64, - work.hash1, work.hash, + work.hash1, work.hash, work.target, max_nonce, &hashes_done); break; #endif diff --git a/miner.h b/miner.h index 49bc07d0..539b5d6f 100644 --- a/miner.h +++ b/miner.h @@ -52,24 +52,29 @@ extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len); extern unsigned int ScanHash_4WaySSE2(const unsigned char *pmidstate, unsigned char *pdata, unsigned char *phash1, unsigned char *phash, + const unsigned char *ptarget, uint32_t max_nonce, unsigned long *nHashesDone); extern bool scanhash_via(unsigned char *data_inout, + const unsigned char *target, uint32_t max_nonce, unsigned long *hashes_done); extern bool scanhash_c(const unsigned char *midstate, unsigned char *data, unsigned char *hash1, unsigned char *hash, + const unsigned char *target, uint32_t max_nonce, unsigned long *hashes_done); extern bool scanhash_cryptopp(const unsigned char *midstate,unsigned char *data, unsigned char *hash1, unsigned char *hash, + const unsigned char *target, uint32_t max_nonce, unsigned long *hashes_done); extern bool scanhash_asm32(const unsigned char *midstate,unsigned char *data, unsigned char *hash1, unsigned char *hash, + const unsigned char *target, uint32_t max_nonce, unsigned long *hashes_done); extern int timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y); -extern void print_pow(const unsigned char *hash); +extern bool fulltest(const unsigned char *hash, const unsigned char *target); #endif /* __MINER_H__ */ diff --git a/sha256_4way.c b/sha256_4way.c index 556a07ad..fe9642bc 100644 --- a/sha256_4way.c +++ b/sha256_4way.c @@ -100,6 +100,7 @@ static const unsigned int pSHA256InitState[8] = unsigned int ScanHash_4WaySSE2(const unsigned char *pmidstate, unsigned char *pdata, unsigned char *phash1, unsigned char *phash, + const unsigned char *ptarget, uint32_t max_nonce, unsigned long *nHashesDone) { unsigned int *nNonce_p = (unsigned int*)(pdata + 12); @@ -124,11 +125,11 @@ unsigned int ScanHash_4WaySSE2(const unsigned char *pmidstate, unsigned char *pd for (i = 0; i < 32/4; i++) ((unsigned int*)phash)[i] = thash[i][j]; - print_pow(phash); - - *nHashesDone = nonce; - *nNonce_p = nonce + j; - return nonce + j; + if (fulltest(phash, ptarget)) { + *nHashesDone = nonce; + *nNonce_p = nonce + j; + return nonce + j; + } } } diff --git a/sha256_cryptopp.c b/sha256_cryptopp.c index 4ada4807..47d4921a 100644 --- a/sha256_cryptopp.c +++ b/sha256_cryptopp.c @@ -93,6 +93,7 @@ static void runhash(void *state, const void *input, const void *init) /* suspiciously similar to ScanHash* from bitcoin */ bool scanhash_cryptopp(const unsigned char *midstate, unsigned char *data, unsigned char *hash1, unsigned char *hash, + const unsigned char *target, uint32_t max_nonce, unsigned long *hashes_done) { uint32_t *hash32 = (uint32_t *) hash; @@ -109,9 +110,7 @@ bool scanhash_cryptopp(const unsigned char *midstate, unsigned char *data, stat_ctr++; - if (hash32[7] == 0) { - print_pow(hash); - + if ((hash32[7] == 0) && fulltest(hash, target)) { *hashes_done = stat_ctr; return true; } @@ -578,6 +577,7 @@ static void runhash32(void *state, const void *input, const void *init) /* suspiciously similar to ScanHash* from bitcoin */ bool scanhash_asm32(const unsigned char *midstate, unsigned char *data, unsigned char *hash1, unsigned char *hash, + const unsigned char *target, uint32_t max_nonce, unsigned long *hashes_done) { uint32_t *hash32 = (uint32_t *) hash; @@ -594,8 +594,8 @@ bool scanhash_asm32(const unsigned char *midstate, unsigned char *data, stat_ctr++; - if (hash32[7] == 0) { - print_pow(hash); + if ((hash32[7] == 0) && fulltest(hash, target)) { + fulltest(hash, target); *hashes_done = stat_ctr; return true; diff --git a/sha256_generic.c b/sha256_generic.c index 91fbbcf5..cc23fc18 100644 --- a/sha256_generic.c +++ b/sha256_generic.c @@ -239,6 +239,7 @@ const uint32_t sha256_init_state[8] = { /* suspiciously similar to ScanHash* from bitcoin */ bool scanhash_c(const unsigned char *midstate, unsigned char *data, unsigned char *hash1, unsigned char *hash, + const unsigned char *target, uint32_t max_nonce, unsigned long *hashes_done) { uint32_t *hash32 = (uint32_t *) hash; @@ -255,9 +256,7 @@ bool scanhash_c(const unsigned char *midstate, unsigned char *data, stat_ctr++; - if (hash32[7] == 0) { - print_pow(hash); - + if ((hash32[7] == 0) && fulltest(hash, target)) { *hashes_done = stat_ctr; return true; } diff --git a/sha256_via.c b/sha256_via.c index 011d854d..a15c6645 100644 --- a/sha256_via.c +++ b/sha256_via.c @@ -18,6 +18,7 @@ static void via_sha256(void *hash, void *buf, unsigned len) } bool scanhash_via(unsigned char *data_inout, + const unsigned char *target, uint32_t max_nonce, unsigned long *hashes_done) { unsigned char data[128] __attribute__((aligned(128))); @@ -56,9 +57,7 @@ bool scanhash_via(unsigned char *data_inout, stat_ctr++; - if (hash32[7] == 0) { - print_pow(tmp_hash); - + if ((hash32[7] == 0) && fulltest(tmp_hash, target)) { /* swap nonce'd data back into original storage area; * TODO: only swap back the nonce, rather than all data */ diff --git a/util.c b/util.c index 2a52a048..e37c0a0a 100644 --- a/util.c +++ b/util.c @@ -255,14 +255,45 @@ timeval_subtract ( return x->tv_sec < y->tv_sec; } -void print_pow(const unsigned char *hash) +bool fulltest(const unsigned char *hash, const unsigned char *target) { - char *hexstr; - unsigned char hash_swap[32]; + unsigned char hash_swap[32], target_swap[32]; + uint32_t *hash32 = (uint32_t *) hash_swap; + uint32_t *target32 = (uint32_t *) target_swap; + int i; + bool rc = true; + char *hash_str, *target_str; swap256(hash_swap, hash); - hexstr = bin2hex(hash_swap, 32); - fprintf(stderr, "PoW found: %s\n", hexstr); - free(hexstr); -} + swap256(target_swap, target); + + for (i = 0; i < 32/4; i++) { + uint32_t h32tmp = swab32(hash32[i]); + uint32_t t32tmp = target32[i]; + + target32[i] = swab32(target32[i]); /* for printing */ + + if (h32tmp > t32tmp) { + rc = false; + break; + } + if (h32tmp < t32tmp) { + rc = true; + break; + } + } + + hash_str = bin2hex(hash_swap, 32); + target_str = bin2hex(target_swap, 32); + fprintf(stderr, " Proof: %s\nTarget: %s\nTrgVal? %s\n", + hash_str, + target_str, + rc ? "YES (hash < target)" : + "no (false positive; hash > target)"); + + free(hash_str); + free(target_str); + + return rc; +}