Browse Source

Rework test_nonce to share common logic for scrypt and SHA256d

Luke Dashjr 12 years ago
parent
commit
b2fdf76a26
3 changed files with 31 additions and 47 deletions
  1. 26 20
      miner.c
  2. 4 18
      scrypt.c
  3. 1 9
      scrypt.h

+ 26 - 20
miner.c

@@ -9035,22 +9035,21 @@ void inc_hw_errors3(struct thr_info *thr, const struct work *work, const uint32_
 		thr->cgpu->drv->hw_error(thr);
 		thr->cgpu->drv->hw_error(thr);
 }
 }
 
 
-enum test_nonce2_result hashtest2(struct work *work, bool checktarget)
+static
+bool test_hash(const void * const phash, const float diff)
 {
 {
-	uint32_t *hash2_32 = (uint32_t *)&work->hash[0];
-
-	hash_data(work->hash, work->data);
-
-	if (hash2_32[7] != 0)
-		return TNR_BAD;
-
-	if (!checktarget)
-		return TNR_GOOD;
-
-	if (!hash_target_check_v(work->hash, work->target))
-		return TNR_HIGH;
-
-	return TNR_GOOD;
+	const uint32_t * const hash = phash;
+	if (diff >= 1.)
+		// FIXME: > 1 should check more
+		return !hash[7];
+	
+	const uint32_t Htarg = (uint32_t)(0x100000000 * diff) - 1;
+	const uint32_t tmp_hash7 = le32toh(hash[7]);
+	
+	applog(LOG_DEBUG, "htarget %08lx hash %08lx",
+				(long unsigned int)Htarg,
+				(long unsigned int)tmp_hash7);
+	return (tmp_hash7 <= Htarg);
 }
 }
 
 
 enum test_nonce2_result _test_nonce2(struct work *work, uint32_t nonce, bool checktarget)
 enum test_nonce2_result _test_nonce2(struct work *work, uint32_t nonce, bool checktarget)
@@ -9060,11 +9059,18 @@ enum test_nonce2_result _test_nonce2(struct work *work, uint32_t nonce, bool che
 
 
 #ifdef USE_SCRYPT
 #ifdef USE_SCRYPT
 	if (opt_scrypt)
 	if (opt_scrypt)
-		// NOTE: Depends on scrypt_test return matching enum values
-		return scrypt_test(work->data, work->target, nonce);
+		scrypt_hash_data(work->hash, work->data);
+	else
 #endif
 #endif
-
-	return hashtest2(work, checktarget);
+		hash_data(work->hash, work->data);
+	
+	if (!test_hash(work->hash, work->nonce_diff))
+		return TNR_BAD;
+	
+	if (checktarget && !hash_target_check_v(work->hash, work->target))
+		return TNR_HIGH;
+	
+	return TNR_GOOD;
 }
 }
 
 
 /* Returns true if nonce for work was a valid share */
 /* Returns true if nonce for work was a valid share */
@@ -9095,7 +9101,7 @@ bool submit_noffset_nonce(struct thr_info *thr, struct work *work_in, uint32_t n
 	work->thr_id = thr->id;
 	work->thr_id = thr->id;
 
 
 	/* Do one last check before attempting to submit the work */
 	/* Do one last check before attempting to submit the work */
-	/* Side effect: sets work->data for us */
+	/* Side effect: sets work->data and work->hash for us */
 	res = test_nonce2(work, nonce);
 	res = test_nonce2(work, nonce);
 	
 	
 	if (unlikely(res == TNR_BAD))
 	if (unlikely(res == TNR_BAD))

+ 4 - 18
scrypt.c

@@ -420,30 +420,16 @@ void scrypt_regenhash(struct work *work)
 	flip32(ohash, ohash);
 	flip32(ohash, ohash);
 }
 }
 
 
-static const uint32_t diff1targ = 0x0000ffff;
-
-/* Used externally as confirmation of correct OCL code */
-int scrypt_test(unsigned char *pdata, const unsigned char *ptarget, uint32_t nonce)
+/* Used by test_nonce functions */
+void scrypt_hash_data(unsigned char * const out_hash, const unsigned char * const pdata)
 {
 {
-	uint32_t tmp_hash7, Htarg = le32toh(((const uint32_t *)ptarget)[7]);
 	uint32_t data[20], ohash[8];
 	uint32_t data[20], ohash[8];
 	char *scratchbuf;
 	char *scratchbuf;
 
 
-	be32enc_vect(data, (const uint32_t *)pdata, 19);
-	data[19] = htobe32(nonce);
+	be32enc_vect(data, (const uint32_t *)pdata, 20);
 	scratchbuf = alloca(SCRATCHBUF_SIZE);
 	scratchbuf = alloca(SCRATCHBUF_SIZE);
 	scrypt_1024_1_1_256_sp(data, scratchbuf, ohash);
 	scrypt_1024_1_1_256_sp(data, scratchbuf, ohash);
-	tmp_hash7 = be32toh(ohash[7]);
-
-	applog(LOG_DEBUG, "htarget %08lx diff1 %08lx hash %08lx",
-				(long unsigned int)Htarg,
-				(long unsigned int)diff1targ,
-				(long unsigned int)tmp_hash7);
-	if (tmp_hash7 > diff1targ)
-		return -1;
-	if (tmp_hash7 > Htarg)
-		return 0;
-	return 1;
+	swap32yes(out_hash, ohash, 32/4);
 }
 }
 
 
 bool scanhash_scrypt(struct thr_info *thr, const unsigned char __maybe_unused *pmidstate,
 bool scanhash_scrypt(struct thr_info *thr, const unsigned char __maybe_unused *pmidstate,

+ 1 - 9
scrypt.h

@@ -6,18 +6,10 @@
 #include "miner.h"
 #include "miner.h"
 
 
 #ifdef USE_SCRYPT
 #ifdef USE_SCRYPT
-extern int scrypt_test(unsigned char *pdata, const unsigned char *ptarget,
-			uint32_t nonce);
+extern void scrypt_hash_data(unsigned char *out_hash, const unsigned char *data);
 extern void scrypt_regenhash(struct work *work);
 extern void scrypt_regenhash(struct work *work);
 
 
 #else /* USE_SCRYPT */
 #else /* USE_SCRYPT */
-static inline int scrypt_test(__maybe_unused unsigned char *pdata,
-			       __maybe_unused const unsigned char *ptarget,
-			       __maybe_unused uint32_t nonce)
-{
-	return 0;
-}
-
 static inline void scrypt_regenhash(__maybe_unused struct work *work)
 static inline void scrypt_regenhash(__maybe_unused struct work *work)
 {
 {
 }
 }