Commit d88648dd3952f4ea2f9aad135710e9f106fe85c4

Jeff Garzik 2010-12-23T20:21:00

sha256_via: ensure that found nonce is copied back into output data buffer Also, some minor cleanups, removing unused args.

diff --git a/cpu-miner.c b/cpu-miner.c
index 138ec9c..a8f1372 100644
--- a/cpu-miner.c
+++ b/cpu-miner.c
@@ -319,9 +319,7 @@ static void *miner_thread(void *thr_id_int)
 
 #ifdef WANT_VIA_PADLOCK
 		case ALGO_VIA:
-			rc = scanhash_via(work.midstate, work.data,
-					  work.hash1, work.hash,
-					  &hashes_done);
+			rc = scanhash_via(work.data, &hashes_done);
 			break;
 #endif
 		case ALGO_CRYPTOPP:
diff --git a/miner.h b/miner.h
index b77bf0d..fc0fe5a 100644
--- a/miner.h
+++ b/miner.h
@@ -34,9 +34,7 @@ extern unsigned int ScanHash_4WaySSE2(const unsigned char *pmidstate,
 	unsigned char *pdata, unsigned char *phash1, unsigned char *phash,
 	unsigned long *nHashesDone);
 
-extern bool scanhash_via(const unsigned char *midstate, const unsigned char *data_in,
-	          unsigned char *hash1, unsigned char *hash,
-	          unsigned long *hashes_done);
+extern bool scanhash_via(unsigned char *data_inout, unsigned long *hashes_done);
 
 extern bool scanhash_c(const unsigned char *midstate, unsigned char *data,
 	      unsigned char *hash1, unsigned char *hash,
diff --git a/sha256_via.c b/sha256_via.c
index ef2c102..b4e2b91 100644
--- a/sha256_via.c
+++ b/sha256_via.c
@@ -28,13 +28,13 @@ static void via_sha256(void *hash, void *buf, unsigned len)
 		     :"memory");
 }
 
-bool scanhash_via(const unsigned char *midstate, const unsigned char *data_in,
-	          unsigned char *hash1, unsigned char *hash,
-	          unsigned long *hashes_done)
+bool scanhash_via(unsigned char *data_inout, unsigned long *hashes_done)
 {
 	unsigned char data[128] __attribute__((aligned(128)));
+	unsigned char tmp_hash[32] __attribute__((aligned(128)));
 	unsigned char tmp_hash1[32] __attribute__((aligned(128)));
-	uint32_t *hash32 = (uint32_t *) hash;
+	uint32_t *data32 = (uint32_t *) data;
+	uint32_t *hash32 = (uint32_t *) tmp_hash;
 	uint32_t *nonce = (uint32_t *)(data + 64 + 12);
 	uint32_t n = 0;
 	unsigned long stat_ctr = 0;
@@ -45,10 +45,8 @@ bool scanhash_via(const unsigned char *midstate, const unsigned char *data_in,
 	 * in order to permit the hardware to swap everything
 	 * back to BE again (extra work).
 	 */
-	for (i = 0; i < 128/4; i++) {
-		uint32_t *data32 = (uint32_t *) data;
-		data32[i] = swab32(((uint32_t *)data_in)[i]);
-	}
+	for (i = 0; i < 128/4; i++)
+		data32[i] = swab32(((uint32_t *)data_inout)[i]);
 
 	while (1) {
 		n++;
@@ -63,20 +61,28 @@ bool scanhash_via(const unsigned char *midstate, const unsigned char *data_in,
 				swab32(((uint32_t *)tmp_hash1)[i]);
 
 		/* second SHA256 transform */
-		memcpy(hash, sha256_init_state, 32);
-		via_sha256(hash, tmp_hash1, 32);
+		memcpy(tmp_hash, sha256_init_state, 32);
+		via_sha256(tmp_hash, tmp_hash1, 32);
 
 		stat_ctr++;
 
 		if (hash32[7] == 0) {
 			char *hexstr;
 
-			hexstr = bin2hex(hash, 32);
+			hexstr = bin2hex(tmp_hash, 32);
 			fprintf(stderr,
 				"DBG: found zeroes in hash:\n%s\n",
 				hexstr);
 			free(hexstr);
 
+			/* swap nonce'd data back into original storage area;
+			 * TODO: only swap back the nonce, rather than all data
+			 */
+			for (i = 0; i < 128/4; i++) {
+				uint32_t *dout32 = (uint32_t *) data_inout;
+				dout32[i] = swab32(data32[i]);
+			}
+
 			*hashes_done = stat_ctr;
 			return true;
 		}