Browse Source

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.
nfactor-troky
Jeff Garzik 14 years ago committed by Jeff Garzik
parent
commit
714c0fd7c9
  1. 10
      cpu-miner.c
  2. 7
      miner.h
  3. 11
      sha256_4way.c
  4. 10
      sha256_cryptopp.c
  5. 5
      sha256_generic.c
  6. 5
      sha256_via.c
  7. 45
      util.c

10
cpu-miner.c

@ -308,7 +308,7 @@ static void *miner_thread(void *thr_id_int)
switch (opt_algo) { switch (opt_algo) {
case ALGO_C: case ALGO_C:
rc = scanhash_c(work.midstate, work.data + 64, rc = scanhash_c(work.midstate, work.data + 64,
work.hash1, work.hash, work.hash1, work.hash, work.target,
max_nonce, &hashes_done); max_nonce, &hashes_done);
break; break;
@ -317,6 +317,7 @@ static void *miner_thread(void *thr_id_int)
unsigned int rc4 = unsigned int rc4 =
ScanHash_4WaySSE2(work.midstate, work.data + 64, ScanHash_4WaySSE2(work.midstate, work.data + 64,
work.hash1, work.hash, work.hash1, work.hash,
work.target,
max_nonce, &hashes_done); max_nonce, &hashes_done);
rc = (rc4 == -1) ? false : true; rc = (rc4 == -1) ? false : true;
} }
@ -325,19 +326,20 @@ static void *miner_thread(void *thr_id_int)
#ifdef WANT_VIA_PADLOCK #ifdef WANT_VIA_PADLOCK
case ALGO_VIA: case ALGO_VIA:
rc = scanhash_via(work.data, max_nonce, &hashes_done); rc = scanhash_via(work.data, work.target,
max_nonce, &hashes_done);
break; break;
#endif #endif
case ALGO_CRYPTOPP: case ALGO_CRYPTOPP:
rc = scanhash_cryptopp(work.midstate, work.data + 64, rc = scanhash_cryptopp(work.midstate, work.data + 64,
work.hash1, work.hash, work.hash1, work.hash, work.target,
max_nonce, &hashes_done); max_nonce, &hashes_done);
break; break;
#ifdef WANT_CRYPTOPP_ASM32 #ifdef WANT_CRYPTOPP_ASM32
case ALGO_CRYPTOPP_ASM32: case ALGO_CRYPTOPP_ASM32:
rc = scanhash_asm32(work.midstate, work.data + 64, rc = scanhash_asm32(work.midstate, work.data + 64,
work.hash1, work.hash, work.hash1, work.hash, work.target,
max_nonce, &hashes_done); max_nonce, &hashes_done);
break; break;
#endif #endif

7
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, extern unsigned int ScanHash_4WaySSE2(const unsigned char *pmidstate,
unsigned char *pdata, unsigned char *phash1, unsigned char *phash, unsigned char *pdata, unsigned char *phash1, unsigned char *phash,
const unsigned char *ptarget,
uint32_t max_nonce, unsigned long *nHashesDone); uint32_t max_nonce, unsigned long *nHashesDone);
extern bool scanhash_via(unsigned char *data_inout, extern bool scanhash_via(unsigned char *data_inout,
const unsigned char *target,
uint32_t max_nonce, unsigned long *hashes_done); uint32_t max_nonce, unsigned long *hashes_done);
extern bool scanhash_c(const unsigned char *midstate, unsigned char *data, extern bool scanhash_c(const unsigned char *midstate, unsigned char *data,
unsigned char *hash1, unsigned char *hash, unsigned char *hash1, unsigned char *hash,
const unsigned char *target,
uint32_t max_nonce, unsigned long *hashes_done); uint32_t max_nonce, unsigned long *hashes_done);
extern bool scanhash_cryptopp(const unsigned char *midstate,unsigned char *data, extern bool scanhash_cryptopp(const unsigned char *midstate,unsigned char *data,
unsigned char *hash1, unsigned char *hash, unsigned char *hash1, unsigned char *hash,
const unsigned char *target,
uint32_t max_nonce, unsigned long *hashes_done); uint32_t max_nonce, unsigned long *hashes_done);
extern bool scanhash_asm32(const unsigned char *midstate,unsigned char *data, extern bool scanhash_asm32(const unsigned char *midstate,unsigned char *data,
unsigned char *hash1, unsigned char *hash, unsigned char *hash1, unsigned char *hash,
const unsigned char *target,
uint32_t max_nonce, unsigned long *hashes_done); uint32_t max_nonce, unsigned long *hashes_done);
extern int extern int
timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y); 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__ */ #endif /* __MINER_H__ */

11
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 int ScanHash_4WaySSE2(const unsigned char *pmidstate, unsigned char *pdata,
unsigned char *phash1, unsigned char *phash, unsigned char *phash1, unsigned char *phash,
const unsigned char *ptarget,
uint32_t max_nonce, unsigned long *nHashesDone) uint32_t max_nonce, unsigned long *nHashesDone)
{ {
unsigned int *nNonce_p = (unsigned int*)(pdata + 12); 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++) for (i = 0; i < 32/4; i++)
((unsigned int*)phash)[i] = thash[i][j]; ((unsigned int*)phash)[i] = thash[i][j];
print_pow(phash); if (fulltest(phash, ptarget)) {
*nHashesDone = nonce;
*nHashesDone = nonce; *nNonce_p = nonce + j;
*nNonce_p = nonce + j; return nonce + j;
return nonce + j; }
} }
} }

10
sha256_cryptopp.c

@ -93,6 +93,7 @@ static void runhash(void *state, const void *input, const void *init)
/* suspiciously similar to ScanHash* from bitcoin */ /* suspiciously similar to ScanHash* from bitcoin */
bool scanhash_cryptopp(const unsigned char *midstate, unsigned char *data, bool scanhash_cryptopp(const unsigned char *midstate, unsigned char *data,
unsigned char *hash1, unsigned char *hash, unsigned char *hash1, unsigned char *hash,
const unsigned char *target,
uint32_t max_nonce, unsigned long *hashes_done) uint32_t max_nonce, unsigned long *hashes_done)
{ {
uint32_t *hash32 = (uint32_t *) hash; uint32_t *hash32 = (uint32_t *) hash;
@ -109,9 +110,7 @@ bool scanhash_cryptopp(const unsigned char *midstate, unsigned char *data,
stat_ctr++; stat_ctr++;
if (hash32[7] == 0) { if ((hash32[7] == 0) && fulltest(hash, target)) {
print_pow(hash);
*hashes_done = stat_ctr; *hashes_done = stat_ctr;
return true; return true;
} }
@ -578,6 +577,7 @@ static void runhash32(void *state, const void *input, const void *init)
/* suspiciously similar to ScanHash* from bitcoin */ /* suspiciously similar to ScanHash* from bitcoin */
bool scanhash_asm32(const unsigned char *midstate, unsigned char *data, bool scanhash_asm32(const unsigned char *midstate, unsigned char *data,
unsigned char *hash1, unsigned char *hash, unsigned char *hash1, unsigned char *hash,
const unsigned char *target,
uint32_t max_nonce, unsigned long *hashes_done) uint32_t max_nonce, unsigned long *hashes_done)
{ {
uint32_t *hash32 = (uint32_t *) hash; uint32_t *hash32 = (uint32_t *) hash;
@ -594,8 +594,8 @@ bool scanhash_asm32(const unsigned char *midstate, unsigned char *data,
stat_ctr++; stat_ctr++;
if (hash32[7] == 0) { if ((hash32[7] == 0) && fulltest(hash, target)) {
print_pow(hash); fulltest(hash, target);
*hashes_done = stat_ctr; *hashes_done = stat_ctr;
return true; return true;

5
sha256_generic.c

@ -239,6 +239,7 @@ const uint32_t sha256_init_state[8] = {
/* suspiciously similar to ScanHash* from bitcoin */ /* suspiciously similar to ScanHash* from bitcoin */
bool scanhash_c(const unsigned char *midstate, unsigned char *data, bool scanhash_c(const unsigned char *midstate, unsigned char *data,
unsigned char *hash1, unsigned char *hash, unsigned char *hash1, unsigned char *hash,
const unsigned char *target,
uint32_t max_nonce, unsigned long *hashes_done) uint32_t max_nonce, unsigned long *hashes_done)
{ {
uint32_t *hash32 = (uint32_t *) hash; uint32_t *hash32 = (uint32_t *) hash;
@ -255,9 +256,7 @@ bool scanhash_c(const unsigned char *midstate, unsigned char *data,
stat_ctr++; stat_ctr++;
if (hash32[7] == 0) { if ((hash32[7] == 0) && fulltest(hash, target)) {
print_pow(hash);
*hashes_done = stat_ctr; *hashes_done = stat_ctr;
return true; return true;
} }

5
sha256_via.c

@ -18,6 +18,7 @@ static void via_sha256(void *hash, void *buf, unsigned len)
} }
bool scanhash_via(unsigned char *data_inout, bool scanhash_via(unsigned char *data_inout,
const unsigned char *target,
uint32_t max_nonce, unsigned long *hashes_done) uint32_t max_nonce, unsigned long *hashes_done)
{ {
unsigned char data[128] __attribute__((aligned(128))); unsigned char data[128] __attribute__((aligned(128)));
@ -56,9 +57,7 @@ bool scanhash_via(unsigned char *data_inout,
stat_ctr++; stat_ctr++;
if (hash32[7] == 0) { if ((hash32[7] == 0) && fulltest(tmp_hash, target)) {
print_pow(tmp_hash);
/* swap nonce'd data back into original storage area; /* swap nonce'd data back into original storage area;
* TODO: only swap back the nonce, rather than all data * TODO: only swap back the nonce, rather than all data
*/ */

45
util.c

@ -255,14 +255,45 @@ timeval_subtract (
return x->tv_sec < y->tv_sec; 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], target_swap[32];
unsigned char hash_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); swap256(hash_swap, hash);
hexstr = bin2hex(hash_swap, 32); swap256(target_swap, target);
fprintf(stderr, "PoW found: %s\n", hexstr);
free(hexstr); 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;
}

Loading…
Cancel
Save