Skip to content

Commit

Permalink
pluck: earlz based volatile sha2 func
Browse files Browse the repository at this point in the history
increase gcc/gw perf, now better than vstudio binary
  • Loading branch information
tpruvot committed Mar 5, 2015
1 parent 580d68e commit ffa395e
Showing 1 changed file with 145 additions and 10 deletions.
155 changes: 145 additions & 10 deletions algo/pluck.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@
#define htobe32(x) ((uint32_t)htonl((uint32_t)(x)))
#endif

#define ROTL(a, b) _rotl(a,b)// (((a) << (b)) | ((a) >> (32 - (b))))

#ifdef _MSC_VER
//#include "scryptjane/scrypt-jane-portable-x86.h"
#define ROTL(a, b) _rotl(a,b)
#define ROTR(a, b) _rotr(a,b)
#else
#define ROTL(a, b) (((a) << b) | ((a) >> (32 - b)))
#define ROTR(a, b) ((a >> b) | (a << (32 - b)))
#endif

#if defined(_MSC_VER) && defined(_M_X64)
Expand Down Expand Up @@ -196,12 +198,143 @@ static inline void xor_salsa8(uint32_t B[16], const uint32_t Bx[16], int i)

#endif

static const uint32_t 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
};

/* Elementary functions used by SHA256 */
#define Ch(x, y, z) ((x & (y ^ z)) ^ z)
#define Maj(x, y, z) ((x & (y | z)) | (y & z))
#define S0(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22))
#define S1(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25))
#define s0(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ (x >> 3))
#define s1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ (x >> 10))

/* SHA256 round function */
#define RND(a, b, c, d, e, f, g, h, k) \
do { \
t0 = h + S1(e) + Ch(e, f, g) + k; \
t1 = S0(a) + Maj(a, b, c); \
d += t0; \
h = t0 + t1; \
} while (0)

/* Adjusted round function for rotating state */
#define RNDr(S, W, i) \
RND(S[(64 - i) % 8], S[(65 - i) % 8], \
S[(66 - i) % 8], S[(67 - i) % 8], \
S[(68 - i) % 8], S[(69 - i) % 8], \
S[(70 - i) % 8], S[(71 - i) % 8], \
W[i] + sha256_k[i])


static void sha256_transform_volatile(uint32_t *state, uint32_t *block)
{
uint32_t* W=block; //note: block needs to be a mutable 64 int32_t
uint32_t S[8];
uint32_t t0, t1;
int i;

for (i = 16; i < 64; i += 2) {
W[i] = s1(W[i - 2]) + W[i - 7] + s0(W[i - 15]) + W[i - 16];
W[i+1] = s1(W[i - 1]) + W[i - 6] + s0(W[i - 14]) + W[i - 15];
}

/* 2. Initialize working variables. */
memcpy(S, state, 32);

/* 3. Mix. */
RNDr(S, W, 0);
RNDr(S, W, 1);
RNDr(S, W, 2);
RNDr(S, W, 3);
RNDr(S, W, 4);
RNDr(S, W, 5);
RNDr(S, W, 6);
RNDr(S, W, 7);
RNDr(S, W, 8);
RNDr(S, W, 9);
RNDr(S, W, 10);
RNDr(S, W, 11);
RNDr(S, W, 12);
RNDr(S, W, 13);
RNDr(S, W, 14);
RNDr(S, W, 15);
RNDr(S, W, 16);
RNDr(S, W, 17);
RNDr(S, W, 18);
RNDr(S, W, 19);
RNDr(S, W, 20);
RNDr(S, W, 21);
RNDr(S, W, 22);
RNDr(S, W, 23);
RNDr(S, W, 24);
RNDr(S, W, 25);
RNDr(S, W, 26);
RNDr(S, W, 27);
RNDr(S, W, 28);
RNDr(S, W, 29);
RNDr(S, W, 30);
RNDr(S, W, 31);
RNDr(S, W, 32);
RNDr(S, W, 33);
RNDr(S, W, 34);
RNDr(S, W, 35);
RNDr(S, W, 36);
RNDr(S, W, 37);
RNDr(S, W, 38);
RNDr(S, W, 39);
RNDr(S, W, 40);
RNDr(S, W, 41);
RNDr(S, W, 42);
RNDr(S, W, 43);
RNDr(S, W, 44);
RNDr(S, W, 45);
RNDr(S, W, 46);
RNDr(S, W, 47);
RNDr(S, W, 48);
RNDr(S, W, 49);
RNDr(S, W, 50);
RNDr(S, W, 51);
RNDr(S, W, 52);
RNDr(S, W, 53);
RNDr(S, W, 54);
RNDr(S, W, 55);
RNDr(S, W, 56);
RNDr(S, W, 57);
RNDr(S, W, 58);
RNDr(S, W, 59);
RNDr(S, W, 60);
RNDr(S, W, 61);
RNDr(S, W, 62);
RNDr(S, W, 63);

/* 4. Mix local working variables into global state */
for (i = 0; i < 8; i++)
state[i] += S[i];
}

// standard sha256 hash
#if 0
#if 1
static void sha256_hash(unsigned char *hash, const unsigned char *data, int len)
{
uint32_t _ALIGN(64) S[16];
uint32_t _ALIGN(64) T[16];
uint32_t _ALIGN(64) T[64];
int i, r;

sha256_init(S);
Expand All @@ -215,7 +348,8 @@ static void sha256_hash(unsigned char *hash, const unsigned char *data, int len)
T[i] = be32dec(T + i);
if (r < 56)
T[15] = 8 * len;
sha256_transform(S, T, 0);
//sha256_transform(S, T, 0);
sha256_transform_volatile(S, T);
}
for (i = 0; i < 8; i++)
be32enc((uint32_t *)hash + i, S[i]);
Expand All @@ -235,18 +369,19 @@ static void sha256_hash(unsigned char *hash, const unsigned char *data, int len)
static void sha256_hash512(uint32_t *hash, const uint32_t *data)
{
uint32_t _ALIGN(64) S[16];
uint32_t _ALIGN(64) T[16];
uchar _ALIGN(64) E[64] = { 0 };
uint32_t _ALIGN(64) T[64];
uchar _ALIGN(64) E[64*4] = { 0 };
int i;

sha256_init(S);

for (i = 0; i < 16; i++)
T[i] = be32dec(&data[i]);
sha256_transform(S, T, 0);
sha256_transform_volatile(S, T);

E[3] = 0x80;
E[61] = 0x02; // T[15] = 8 * 64 => 0x200;
sha256_transform(S, (uint32_t*) E, 0);
sha256_transform_volatile(S, (uint32_t*)E);

for (i = 0; i < 8; i++)
be32enc(&hash[i], S[i]);
Expand Down

0 comments on commit ffa395e

Please sign in to comment.