Avoid unnecessary rehashing in nanofury nonce checking.
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
diff --git a/libbitfury.c b/libbitfury.c
index 38284cb..a8abe00 100644
--- a/libbitfury.c
+++ b/libbitfury.c
@@ -83,38 +83,21 @@ static unsigned int atrvec[] = {
};
static bool atrvec_set;
-static int rehash(unsigned char *midstate, unsigned m7, unsigned ntime, unsigned nbits,
- unsigned nnonce)
+static bool rehash(unsigned int *mid32, unsigned char *in)
{
- unsigned char in[16];
- unsigned int *in32 = (unsigned int *)in;
- unsigned int *mid32 = (unsigned int *)midstate;
unsigned out32[8];
unsigned char *out = (unsigned char *) out32;
sha256_ctx ctx;
- memset( &ctx, 0, sizeof( sha256_ctx ) );
- memcpy(ctx.h, mid32, 8*4);
+ memset(&ctx, 0, sizeof( sha256_ctx ));
+ memcpy(ctx.h, mid32, 8 * 4);
ctx.tot_len = 64;
- nnonce = bswap_32(nnonce);
- in32[0] = bswap_32(m7);
- in32[1] = bswap_32(ntime);
- in32[2] = bswap_32(nbits);
- in32[3] = nnonce;
-
sha256_update(&ctx, in, 16);
sha256_final(&ctx, out);
sha256(out, 32, out);
- if (out32[7] == 0) {
- char hex[68];
-
- __bin2hex(hex, out, 32);
- applog(LOG_INFO, "! MS0: %08x, m7: %08x, ntime: %08x, nbits: %08x, nnonce: %08x\n\t\t\t out: %s\n", mid32[0], m7, ntime, nbits, nnonce, hex);
- return 1;
- }
- return 0;
+ return (out32[7] == 0);
}
void bitfury_work_to_payload(struct bitfury_payload *p, struct work *work)
@@ -364,15 +347,38 @@ bool libbitfury_sendHashData(struct cgpu_info *bf)
for (i = 0; i < 16; i++) {
if (oldbuf[i] != newbuf[i]) {
- unsigned pn; //possible nonce
- unsigned int s = 0; //TODO zero may be solution
+ unsigned char in[16];
+ unsigned int *in32 = (unsigned int *)in;
+ unsigned int *mid32;
+ uint32_t nonce, pn; //possible nonce
+ bool found = false;
+ mid32 = (unsigned int *)op->midstate;
pn = decnonce(newbuf[i]);
- s |= rehash(op->midstate, op->m7, op->ntime, op->nbits, pn) ? pn : 0;
- s |= rehash(op->midstate, op->m7, op->ntime, op->nbits, pn-0x400000) ? pn - 0x400000 : 0;
- s |= rehash(op->midstate, op->m7, op->ntime, op->nbits, pn-0x800000) ? pn - 0x800000 : 0;
- if (s)
- results[results_num++] = bswap_32(s);
+ in32[0] = bswap_32(op->m7);
+ in32[1] = bswap_32(op->ntime);
+ in32[2] = bswap_32(op->nbits);
+
+ nonce = pn - 0x800000;
+ in32[3] = bswap_32(nonce);
+ if (rehash(mid32, in)) {
+ found = true;
+ goto out_found;
+ }
+ nonce = pn;
+ in32[3] = bswap_32(nonce);
+ if (rehash(mid32, in)) {
+ found = true;
+ goto out_found;
+
+ }
+ nonce = pn - 0x400000;
+ in32[3] = bswap_32(nonce);
+ if (rehash(mid32, in))
+ found = true;
+out_found:
+ if (found)
+ results[results_num++] = bswap_32(nonce);
}
}
info->results_n = results_num;