aboutsummaryrefslogtreecommitdiff
path: root/sha256_generic.c
diff options
context:
space:
mode:
authorJeff Garzik <jeff@garzik.org>2010-11-27 04:31:32 -0500
committerJeff Garzik <jgarzik@redhat.com>2010-11-27 04:31:32 -0500
commit339ddf4d757e17687d18d59e8ded57a71f186121 (patch)
tree8bdcd2f8bfee2b5ffdd8ee5c5cb2cd7fa93967b8 /sha256_generic.c
parentad3fda8aba5f3f1f4820a20a925c6387347876f9 (diff)
Move sha256_generic into its own .o build output. Const-ify midstate param.
Diffstat (limited to 'sha256_generic.c')
-rw-r--r--sha256_generic.c56
1 files changed, 56 insertions, 0 deletions
diff --git a/sha256_generic.c b/sha256_generic.c
index d622353..e778113 100644
--- a/sha256_generic.c
+++ b/sha256_generic.c
@@ -19,6 +19,10 @@
*/
#include <stdint.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <string.h>
+#include "miner.h"
typedef uint32_t u32;
typedef uint8_t u8;
@@ -221,3 +225,55 @@ static void sha256_transform(u32 *state, const u8 *input)
#endif
}
+static void runhash(void *state, const void *input, const void *init)
+{
+ memcpy(state, init, 32);
+ sha256_transform(state, input);
+}
+
+const uint32_t sha256_init_state[8] = {
+ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
+ 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
+};
+
+/* suspiciously similar to ScanHash* from bitcoin */
+bool scanhash_c(const unsigned char *midstate, unsigned char *data,
+ unsigned char *hash1, unsigned char *hash,
+ unsigned long *hashes_done)
+{
+ uint32_t *hash32 = (uint32_t *) hash;
+ uint32_t *nonce = (uint32_t *)(data + 12);
+ uint32_t n = 0;
+ unsigned long stat_ctr = 0;
+
+ while (1) {
+ n++;
+ *nonce = n;
+
+ runhash(hash1, data, midstate);
+ runhash(hash, hash1, sha256_init_state);
+
+ stat_ctr++;
+
+ if (hash32[7] == 0) {
+ char *hexstr;
+
+ hexstr = bin2hex(hash, 32);
+ fprintf(stderr,
+ "DBG: found zeroes in hash:\n%s\n",
+ hexstr);
+ free(hexstr);
+
+ *hashes_done = stat_ctr;
+ return true;
+ }
+
+ if ((n & 0xffffff) == 0) {
+ if (opt_debug)
+ fprintf(stderr, "DBG: end of nonce range\n");
+ *hashes_done = stat_ctr;
+ return false;
+ }
+ }
+}
+