Commit c31e75b7c5fbd9039a21752e4c59f0cbb9fc9a52

Con Kolivas 2014-01-05T22:41:10

Import functions from nanofury fork for libbitfury

diff --git a/driver-bitfury.c b/driver-bitfury.c
index 34d779f..3678aa1 100644
--- a/driver-bitfury.c
+++ b/driver-bitfury.c
@@ -474,14 +474,14 @@ static bool nf1_spi_txrx(struct cgpu_info *bitfury, struct bitfury_info *info)
 	return true;
 }
 
-static void nf1_reinit(struct cgpu_info *bitfury, struct bitfury_info *info)
+static bool nf1_reinit(struct cgpu_info *bitfury, struct bitfury_info *info)
 {
 	spi_clear_buf(info);
 	spi_add_break(info);
 	nf1_set_freq(info);
 	nf1_send_conf(info);
 	nf1_send_init(info);
-	nf1_spi_txrx(bitfury, info);
+	return nf1_spi_txrx(bitfury, info);
 }
 
 static bool nf1_set_spi_settings(struct cgpu_info *bitfury, struct bitfury_info *info)
@@ -593,8 +593,7 @@ static bool nf1_detect_one(struct cgpu_info *bitfury, struct bitfury_info *info)
 		goto out;
 
 	info->osc6_bits = 50;
-	nf1_reinit(bitfury, info);
-	ret = true;
+	ret = nf1_reinit(bitfury, info);
 out:
 	if (!ret)
 		nf1_close(bitfury);
diff --git a/libbitfury.c b/libbitfury.c
index aa066b1..1d42090 100644
--- a/libbitfury.c
+++ b/libbitfury.c
@@ -9,7 +9,10 @@
  * any later version.  See COPYING for more details.
  */
 
+#include "miner.h"
+#include "libbitfury.h"
 #include "sha2.h"
+
 void ms3steps(uint32_t *p)
 {
 	uint32_t a, b, c, d, e, f, g, h, new_e, new_a;
@@ -70,3 +73,75 @@ uint32_t decnonce(uint32_t in)
 	out -= 0x800004;
 	return out;
 }
+
+/* Test vectors to calculate (using address-translated loads) */
+static unsigned int atrvec[] = {
+	0xb0e72d8e, 0x1dc5b862, 0xe9e7c4a6, 0x3050f1f5, 0x8a1a6b7e, 0x7ec384e8, 0x42c1c3fc, 0x8ed158a1, /* MIDSTATE */
+	0,0,0,0,0,0,0,0,
+	0x8a0bb7b7, 0x33af304f, 0x0b290c1a, 0xf0c4e61f, /* WDATA: hashMerleRoot[7], nTime, nBits, nNonce */
+
+	0x9c4dfdc0, 0xf055c9e1, 0xe60f079d, 0xeeada6da, 0xd459883d, 0xd8049a9d, 0xd49f9a96, 0x15972fed, /* MIDSTATE */
+	0,0,0,0,0,0,0,0,
+	0x048b2528, 0x7acb2d4f, 0x0b290c1a, 0xbe00084a, /* WDATA: hashMerleRoot[7], nTime, nBits, nNonce */
+
+	0x0317b3ea, 0x1d227d06, 0x3cca281e, 0xa6d0b9da, 0x1a359fe2, 0xa7287e27, 0x8b79c296, 0xc4d88274, /* MIDSTATE */
+	0,0,0,0,0,0,0,0,
+	0x328bcd4f, 0x75462d4f, 0x0b290c1a, 0x002c6dbc, /* WDATA: hashMerleRoot[7], nTime, nBits, nNonce */
+
+	0xac4e38b6, 0xba0e3b3b, 0x649ad6f8, 0xf72e4c02, 0x93be06fb, 0x366d1126, 0xf4aae554, 0x4ff19c5b, /* MIDSTATE */
+	0,0,0,0,0,0,0,0,
+	0x72698140, 0x3bd62b4f, 0x3fd40c1a, 0x801e43e9, /* WDATA: hashMerleRoot[7], nTime, nBits, nNonce */
+
+	0x9dbf91c9, 0x12e5066c, 0xf4184b87, 0x8060bc4d, 0x18f9c115, 0xf589d551, 0x0f7f18ae, 0x885aca59, /* MIDSTATE */
+	0,0,0,0,0,0,0,0,
+	0x6f3806c3, 0x41f82a4f, 0x3fd40c1a, 0x00334b39, /* WDATA: hashMerleRoot[7], nTime, nBits, nNonce */
+};
+
+static int rehash(unsigned char *midstate, unsigned m7, unsigned ntime, unsigned nbits,
+		  unsigned nnonce)
+{
+	unsigned char in[16];
+	unsigned int *in32 = (unsigned int *)in;
+	char *hex;
+	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);
+	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) {
+		hex = bin2hex(midstate, 32);
+		hex = bin2hex(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;
+}
+
+void bitfury_work_to_payload(struct bitfury_payload *p, struct work *w)
+{
+	unsigned char flipped_data[80];
+
+	memset(p, 0, sizeof(struct bitfury_payload));
+	flip80(flipped_data, w->data);
+
+	memcpy(p->midstate, w->midstate, 32);
+	p->m7 = bswap_32(*(unsigned *)(flipped_data + 64));
+	p->ntime = bswap_32(*(unsigned *)(flipped_data + 68));
+	p->nbits = bswap_32(*(unsigned *)(flipped_data + 72));
+	applog(LOG_INFO, "INFO nonc: %08x bitfury_scanHash MS0: %08x, ", p->nnonce, ((unsigned int *)w->midstate)[0]);
+	applog(LOG_INFO, "INFO merkle[7]: %08x, ntime: %08x, nbits: %08x", p->m7, p->ntime, p->nbits);
+}
diff --git a/libbitfury.h b/libbitfury.h
index e4cef89..562b942 100644
--- a/libbitfury.h
+++ b/libbitfury.h
@@ -9,6 +9,15 @@
 
 #ifndef LIBBITFURY_H
 #define LIBBITFURY_H
+struct bitfury_payload {
+	unsigned char midstate[32];
+	unsigned int junk[8];
+	unsigned m7;
+	unsigned ntime;
+	unsigned nbits;
+	unsigned nnonce;
+};
+
 void ms3steps(uint32_t *p);
 uint32_t decnonce(uint32_t in);