Move sha256_generic into its own .o build output. Const-ify midstate param.

This commit is contained in:
Jeff Garzik 2010-11-27 04:31:32 -05:00 committed by Jeff Garzik
parent ad3fda8aba
commit 339ddf4d75
6 changed files with 68 additions and 63 deletions

View file

@ -11,9 +11,7 @@ INCLUDES = $(PTHREAD_FLAGS) -fno-strict-aliasing $(JANSSON_INCLUDES)
bin_PROGRAMS = minerd
EXTRA_DIST = sha256_generic.c
minerd_SOURCES = cpu-miner.c sha256_4way.c sha256_via.c \
minerd_SOURCES = cpu-miner.c sha256_generic.c sha256_4way.c sha256_via.c \
util.c miner.h compat.h
minerd_LDFLAGS = $(PTHREAD_FLAGS)
minerd_LDADD = @LIBCURL@ @JANSSON_LIBS@ @PTHREAD_LIBS@

View file

@ -119,8 +119,6 @@ struct work {
unsigned char hash[32];
};
#include "sha256_generic.c"
static bool jobj_binary(const json_t *obj, const char *key,
void *buf, size_t buflen)
{
@ -231,58 +229,6 @@ static void hashmeter(int thr_id, struct timeval *tv_start,
khashes / secs);
}
static void runhash(void *state, 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 */
static bool scanhash(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;
}
}
}
static void *miner_thread(void *thr_id_int)
{
int thr_id = (unsigned long) thr_id_int;
@ -318,7 +264,7 @@ static void *miner_thread(void *thr_id_int)
/* scan nonces for a proof-of-work hash */
switch (opt_algo) {
case ALGO_C:
rc = scanhash(work.midstate, work.data + 64,
rc = scanhash_c(work.midstate, work.data + 64,
work.hash1, work.hash, &hashes_done);
break;

View file

@ -26,13 +26,18 @@ extern json_t *json_rpc_call(const char *url, const char *userpass,
extern char *bin2hex(unsigned char *p, size_t len);
extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
extern unsigned int ScanHash_4WaySSE2(unsigned char *pmidstate,
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(unsigned char *midstate, const unsigned char *data_in,
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_c(const unsigned char *midstate, unsigned char *data,
unsigned char *hash1, unsigned char *hash,
unsigned long *hashes_done);
extern int
timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y);

View file

@ -97,7 +97,7 @@ static const unsigned int pSHA256InitState[8] =
{0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19};
unsigned int ScanHash_4WaySSE2(unsigned char *pmidstate, unsigned char *pdata,
unsigned int ScanHash_4WaySSE2(const unsigned char *pmidstate, unsigned char *pdata,
unsigned char *phash1, unsigned char *phash, unsigned long *nHashesDone)
{
unsigned int *nNonce_p = (unsigned int*)(pdata + 12);

View file

@ -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;
}
}
}

View file

@ -28,7 +28,7 @@ static void via_sha256(void *hash, void *buf, unsigned len)
:"memory");
}
bool scanhash_via(unsigned char *midstate, const unsigned char *data_in,
bool scanhash_via(const unsigned char *midstate, const unsigned char *data_in,
unsigned char *hash1, unsigned char *hash,
unsigned long *hashes_done)
{