Skip to content

Commit

Permalink
Scrypt, SHA256 and CryptoNight optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasjones committed May 10, 2014
1 parent fc812b4 commit 04f4829
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 27 deletions.
18 changes: 10 additions & 8 deletions crypto/oaes_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ static const char _NR[] = {
0x4e,0x61,0x62,0x69,0x6c,0x20,0x53,0x2e,0x20,
0x41,0x6c,0x20,0x52,0x61,0x6d,0x6c,0x69,0x00 };

#include "miner.h"

#include <stddef.h>
#include <time.h>
#include <sys/timeb.h>
Expand Down Expand Up @@ -261,7 +263,7 @@ static OAES_RET oaes_sub_byte( uint8_t * byte )
{
size_t _x, _y;

if( NULL == byte )
if( unlikely(NULL == byte) )
return OAES_RET_ARG1;

_x = _y = *byte;
Expand Down Expand Up @@ -321,7 +323,7 @@ static OAES_RET oaes_shift_rows( uint8_t block[OAES_BLOCK_SIZE] )
{
uint8_t _temp[OAES_BLOCK_SIZE];

if( NULL == block )
if( unlikely(NULL == block) )
return OAES_RET_ARG1;

_temp[0x00] = block[0x00];
Expand Down Expand Up @@ -412,7 +414,7 @@ static OAES_RET oaes_mix_cols( uint8_t word[OAES_COL_LEN] )
{
uint8_t _temp[OAES_COL_LEN];

if( NULL == word )
if( unlikely(NULL == word) )
return OAES_RET_ARG1;

_temp[0] = oaes_gf_mul(word[0], 0x02) ^ oaes_gf_mul( word[1], 0x03 ) ^
Expand Down Expand Up @@ -1438,10 +1440,10 @@ OAES_API OAES_RET oaes_encryption_round( const uint8_t * key, uint8_t * c )
{
size_t _i;

if( NULL == key )
if( unlikely(NULL == key) )
return OAES_RET_ARG1;

if( NULL == c )
if( unlikely(NULL == c) )
return OAES_RET_ARG2;

// SubBytes(state)
Expand Down Expand Up @@ -1469,13 +1471,13 @@ OAES_API OAES_RET oaes_pseudo_encrypt_ecb( OAES_CTX * ctx, uint8_t * c )
size_t _i;
oaes_ctx * _ctx = (oaes_ctx *) ctx;

if( NULL == _ctx )
if( unlikely(NULL == _ctx) )
return OAES_RET_ARG1;

if( NULL == c )
if( unlikely(NULL == c) )
return OAES_RET_ARG2;

if( NULL == _ctx->key )
if( unlikely(NULL == _ctx->key) )
return OAES_RET_NOKEY;

for ( _i = 0; _i < 10; ++_i )
Expand Down
29 changes: 14 additions & 15 deletions cryptonight.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ void do_groestl_hash(const void* input, size_t len, char* output) {

static void do_jh_hash(const void* input, size_t len, char* output) {
int r = jh_hash(HASH_SIZE * 8, input, 8 * len, (uint8_t*)output);
assert(SUCCESS == r);
assert(likely(SUCCESS == r));
}

static void do_skein_hash(const void* input, size_t len, char* output) {
int r = skein_hash(8 * HASH_SIZE, input, 8 * len, (uint8_t*)output);
assert(SKEIN_SUCCESS == r);
assert(likely(SKEIN_SUCCESS == r));
}

static void (* const extra_hashes[4])(const void *, size_t, char *) = {
Expand Down Expand Up @@ -145,7 +145,6 @@ static void cryptonight_hash(void* output, const void* input) {
xor_blocks(b, c);
swap_blocks(b, c);
copy_block(&long_state[j * AES_BLOCK_SIZE], c);
assert(j == e2i(a, MEMORY / AES_BLOCK_SIZE));
swap_blocks(a, b);
/* Iteration 2 */
j = e2i(a, MEMORY / AES_BLOCK_SIZE);
Expand Down Expand Up @@ -198,63 +197,63 @@ int scanhash_cryptonight(int thr_id, uint32_t *pdata, const uint32_t *ptarget,
pdata[19] = ++n;
be32enc(&endiandata[19], n);
cryptonight_hash(hash64, &endiandata);
if (((hash64[7] & 0xFFFFFFFF) == 0) && fulltest(hash64, ptarget)) {
if (unlikely((hash64[7] & 0xFFFFFFFF) == 0) && fulltest(hash64, ptarget)) {
*hashes_done = n - first_nonce + 1;
return true;
}
} while (n < max_nonce && !work_restart[thr_id].restart);
} while (likely(n < max_nonce && !work_restart[thr_id].restart));
} else if (ptarget[7] <= 0xF) {
do {
pdata[19] = ++n;
be32enc(&endiandata[19], n);
cryptonight_hash(hash64, &endiandata);
if (((hash64[7] & 0xFFFFFFF0) == 0) && fulltest(hash64, ptarget)) {
if (unlikely((hash64[7] & 0xFFFFFFF0) == 0) && fulltest(hash64, ptarget)) {
*hashes_done = n - first_nonce + 1;
return true;
}
} while (n < max_nonce && !work_restart[thr_id].restart);
} while (likely(n < max_nonce && !work_restart[thr_id].restart));
} else if (ptarget[7] <= 0xFF) {
do {
pdata[19] = ++n;
be32enc(&endiandata[19], n);
cryptonight_hash(hash64, &endiandata);
if (((hash64[7] & 0xFFFFFF00) == 0) && fulltest(hash64, ptarget)) {
if (unlikely((hash64[7] & 0xFFFFFF00) == 0) && fulltest(hash64, ptarget)) {
*hashes_done = n - first_nonce + 1;
return true;
}
} while (n < max_nonce && !work_restart[thr_id].restart);
} while (likely(n < max_nonce && !work_restart[thr_id].restart));
} else if (ptarget[7] <= 0xFFF) {
do {
pdata[19] = ++n;
be32enc(&endiandata[19], n);
cryptonight_hash(hash64, &endiandata);
if (((hash64[7] & 0xFFFFF000) == 0) && fulltest(hash64, ptarget)) {
if (unlikely((hash64[7] & 0xFFFFF000) == 0) && fulltest(hash64, ptarget)) {
*hashes_done = n - first_nonce + 1;
return true;
}
} while (n < max_nonce && !work_restart[thr_id].restart);
} while (likely(n < max_nonce && !work_restart[thr_id].restart));

} else if (ptarget[7] <= 0xFFFF) {
do {
pdata[19] = ++n;
be32enc(&endiandata[19], n);
cryptonight_hash(hash64, &endiandata);
if (((hash64[7] & 0xFFFF0000) == 0) && fulltest(hash64, ptarget)) {
if (unlikely((hash64[7] & 0xFFFF0000) == 0) && fulltest(hash64, ptarget)) {
*hashes_done = n - first_nonce + 1;
return true;
}
} while (n < max_nonce && !work_restart[thr_id].restart);
} while (likely(n < max_nonce && !work_restart[thr_id].restart));

} else {
do {
pdata[19] = ++n;
be32enc(&endiandata[19], n);
cryptonight_hash(hash64, &endiandata);
if (fulltest(hash64, ptarget)) {
if (unlikely(fulltest(hash64, ptarget))) {
*hashes_done = n - first_nonce + 1;
return true;
}
} while (n < max_nonce && !work_restart[thr_id].restart);
} while (likely(n < max_nonce && !work_restart[thr_id].restart));
}

*hashes_done = n - first_nonce + 1;
Expand Down
4 changes: 2 additions & 2 deletions scrypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -744,13 +744,13 @@ int scanhash_scrypt(int thr_id, uint32_t *pdata,
scrypt_1024_1_1_256(data, hash, midstate, scratchbuf);

for (i = 0; i < throughput; i++) {
if (hash[i * 8 + 7] <= Htarg && fulltest(hash + i * 8, ptarget)) {
if (unlikely(hash[i * 8 + 7] <= Htarg && fulltest(hash + i * 8, ptarget))) {
*hashes_done = n - pdata[19] + 1;
pdata[19] = data[i * 20 + 19];
return 1;
}
}
} while (n < max_nonce && !work_restart[thr_id].restart);
} while (likely(n < max_nonce && !work_restart[thr_id].restart));

*hashes_done = n - pdata[19] + 1;
pdata[19] = n;
Expand Down
4 changes: 2 additions & 2 deletions sha2.c
Original file line number Diff line number Diff line change
Expand Up @@ -614,15 +614,15 @@ int scanhash_sha256d(int thr_id, uint32_t *pdata, const uint32_t *ptarget,
do {
data[3] = ++n;
sha256d_ms(hash, data, midstate, prehash);
if (swab32(hash[7]) <= Htarg) {
if (unlikely(swab32(hash[7]) <= Htarg)) {
pdata[19] = data[3];
sha256d_80_swap(hash, pdata);
if (fulltest(hash, ptarget)) {
*hashes_done = n - first_nonce + 1;
return 1;
}
}
} while (n < max_nonce && !work_restart[thr_id].restart);
} while (likely(n < max_nonce && !work_restart[thr_id].restart));

*hashes_done = n - first_nonce + 1;
pdata[19] = n;
Expand Down

0 comments on commit 04f4829

Please sign in to comment.