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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
diff --git a/cpu-miner.c b/cpu-miner.c
index f9be60c..c3924a9 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 49bc07d..539b5d6 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 556a07a..fe9642b 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 4ada480..47d4921 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 91fbbcf..cc23fc1 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 011d854..a15c664 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 2a52a04..e37c0a0 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;
+}