Browse Source

Add Crypto++ sha256 implementation (C only, ASM elided for now)

nfactor-troky
Jeff Garzik 14 years ago committed by Jeff Garzik
parent
commit
74bb196296
  1. 2
      Makefile.am
  2. 10
      cpu-miner.c
  3. 3
      miner.h
  4. 133
      sha256_cryptopp.c

2
Makefile.am

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

10
cpu-miner.c

@ -39,6 +39,7 @@ enum sha256_algos { @@ -39,6 +39,7 @@ enum sha256_algos {
ALGO_C, /* plain C */
ALGO_4WAY, /* parallel SSE2 */
ALGO_VIA, /* VIA padlock */
ALGO_CRYPTOPP, /* Crypto++ */
};
static const char *algo_names[] = {
@ -49,6 +50,7 @@ static const char *algo_names[] = { @@ -49,6 +50,7 @@ static const char *algo_names[] = {
#ifdef WANT_VIA_PADLOCK
[ALGO_VIA] = "via",
#endif
[ALGO_CRYPTOPP] = "cryptopp",
};
bool opt_debug = false;
@ -74,11 +76,12 @@ static struct option_help options_help[] = { @@ -74,11 +76,12 @@ static struct option_help options_help[] = {
"(-a XXX) Specify sha256 implementation:\n"
"\tc\t\tLinux kernel sha256, implemented in C (default)"
#ifdef WANT_SSE2_4WAY
"\n\t4way\t\ttcatm's 4-way SSE2 implementation (EXPERIMENTAL)"
"\n\t4way\t\ttcatm's 4-way SSE2 implementation"
#endif
#ifdef WANT_VIA_PADLOCK
"\n\tvia\t\tVIA padlock implementation (EXPERIMENTAL)"
#endif
"\n\tcryptopp\tCrypto++ library implementation (EXPERIMENTAL)"
},
{ "debug",
@ -286,6 +289,11 @@ static void *miner_thread(void *thr_id_int) @@ -286,6 +289,11 @@ static void *miner_thread(void *thr_id_int)
&hashes_done);
break;
#endif
case ALGO_CRYPTOPP:
rc = scanhash_cryptopp(work.midstate, work.data + 64,
work.hash1, work.hash, &hashes_done);
break;
}
hashmeter(thr_id, &tv_start, hashes_done);

3
miner.h

@ -37,6 +37,9 @@ extern bool scanhash_via(const unsigned char *midstate, const unsigned char *dat @@ -37,6 +37,9 @@ extern bool scanhash_via(const unsigned char *midstate, const unsigned char *dat
extern bool scanhash_c(const unsigned char *midstate, unsigned char *data,
unsigned char *hash1, unsigned char *hash,
unsigned long *hashes_done);
extern bool scanhash_cryptopp(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);

133
sha256_cryptopp.c

@ -0,0 +1,133 @@ @@ -0,0 +1,133 @@
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "miner.h"
typedef uint32_t word32;
static word32 rotrFixed(word32 word, unsigned int shift)
{
return (word >> shift) | (word << (32 - shift));
}
#define blk0(i) (W[i] = data[i])
static const word32 SHA256_K[64] = {
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
};
#define blk2(i) (W[i&15]+=s1(W[(i-2)&15])+W[(i-7)&15]+s0(W[(i-15)&15]))
#define Ch(x,y,z) (z^(x&(y^z)))
#define Maj(x,y,z) (y^((x^y)&(y^z)))
#define a(i) T[(0-i)&7]
#define b(i) T[(1-i)&7]
#define c(i) T[(2-i)&7]
#define d(i) T[(3-i)&7]
#define e(i) T[(4-i)&7]
#define f(i) T[(5-i)&7]
#define g(i) T[(6-i)&7]
#define h(i) T[(7-i)&7]
#define R(i) h(i)+=S1(e(i))+Ch(e(i),f(i),g(i))+SHA256_K[i+j]+(j?blk2(i):blk0(i));\
d(i)+=h(i);h(i)+=S0(a(i))+Maj(a(i),b(i),c(i))
// for SHA256
#define S0(x) (rotrFixed(x,2)^rotrFixed(x,13)^rotrFixed(x,22))
#define S1(x) (rotrFixed(x,6)^rotrFixed(x,11)^rotrFixed(x,25))
#define s0(x) (rotrFixed(x,7)^rotrFixed(x,18)^(x>>3))
#define s1(x) (rotrFixed(x,17)^rotrFixed(x,19)^(x>>10))
static void SHA256_Transform(word32 *state, const word32 *data)
{
word32 W[16];
word32 T[8];
unsigned int j;
/* Copy context->state[] to working vars */
memcpy(T, state, sizeof(T));
/* 64 operations, partially loop unrolled */
for (j=0; j<64; j+=16)
{
R( 0); R( 1); R( 2); R( 3);
R( 4); R( 5); R( 6); R( 7);
R( 8); R( 9); R(10); R(11);
R(12); R(13); R(14); R(15);
}
/* Add the working vars back into context.state[] */
state[0] += a(0);
state[1] += b(0);
state[2] += c(0);
state[3] += d(0);
state[4] += e(0);
state[5] += f(0);
state[6] += g(0);
state[7] += h(0);
}
static void runhash(void *state, const void *input, const void *init)
{
memcpy(state, init, 32);
SHA256_Transform(state, input);
}
/* suspiciously similar to ScanHash* from bitcoin */
bool scanhash_cryptopp(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;
}
}
}
Loading…
Cancel
Save