Skip to content

Commit

Permalink
add cryptonight-lite support
Browse files Browse the repository at this point in the history
  • Loading branch information
iamsmooth committed May 17, 2015
1 parent 231be03 commit bfbfdc5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
17 changes: 13 additions & 4 deletions cpu-miner.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ enum algos {
ALGO_X14, /* X14 */
ALGO_X15, /* X15 Whirlpool */
ALGO_CRYPTONIGHT, /* CryptoNight */
ALGO_CRYPTONIGHT_LITE, /* CryptoNight-Lite */
};

static const char *algo_names[] = {
Expand All @@ -131,6 +132,7 @@ static const char *algo_names[] = {
[ALGO_X14] = "x14",
[ALGO_X15] = "x15",
[ALGO_CRYPTONIGHT] = "cryptonight",
[ALGO_CRYPTONIGHT_LITE] = "cryptonight-lite"
};

bool opt_debug = false;
Expand Down Expand Up @@ -560,6 +562,7 @@ static void share_result(int result, struct work *work, const char *reason) {

switch (opt_algo) {
case ALGO_CRYPTONIGHT:
case ALGO_CRYPTONIGHT_LITE:
applog(LOG_INFO, "accepted: %lu/%lu (%.2f%%), %.2f H/s at diff %g %s",
accepted_count, accepted_count + rejected_count,
100. * accepted_count / (accepted_count + rejected_count), hashrate,
Expand Down Expand Up @@ -602,8 +605,9 @@ static bool submit_upstream_work(CURL *curl, struct work *work) {
char hash[32];
switch(opt_algo) {
case ALGO_CRYPTONIGHT:
case ALGO_CRYPTONIGHT_LITE:
default:
cryptonight_hash(hash, work->data, 76);
cryptonight_hash(hash, work->data, 76,opt_algo==ALGO_CRYPTONIGHT_LITE);
}
char *hashhex = bin2hex(hash, 32);
snprintf(s, JSON_BUF_LEN,
Expand Down Expand Up @@ -635,8 +639,9 @@ static bool submit_upstream_work(CURL *curl, struct work *work) {
char hash[32];
switch(opt_algo) {
case ALGO_CRYPTONIGHT:
case ALGO_CRYPTONIGHT_LITE:
default:
cryptonight_hash(hash, work->data, 76);
cryptonight_hash(hash, work->data, 76,opt_algo==ALGO_CRYPTONIGHT_LITE);
}
char *hashhex = bin2hex(hash, 32);
snprintf(s, JSON_BUF_LEN,
Expand Down Expand Up @@ -1128,6 +1133,7 @@ static void *miner_thread(void *userdata) {
max64 = opt_scrypt_n < 16 ? 0x3ffff : 0x3fffff / opt_scrypt_n;
break;
case ALGO_CRYPTONIGHT:
case ALGO_CRYPTONIGHT_LITE:
max64 = 0x40LL;
break;
case ALGO_FRESH:
Expand Down Expand Up @@ -1215,8 +1221,9 @@ static void *miner_thread(void *userdata) {
&hashes_done);
break;
case ALGO_CRYPTONIGHT:
case ALGO_CRYPTONIGHT_LITE:
rc = scanhash_cryptonight(thr_id, work.data, work.target,
max_nonce, &hashes_done);
max_nonce, &hashes_done,opt_algo==ALGO_CRYPTONIGHT_LITE);
break;

default:
Expand All @@ -1236,6 +1243,7 @@ static void *miner_thread(void *userdata) {
if (!opt_quiet) {
switch(opt_algo) {
case ALGO_CRYPTONIGHT:
case ALGO_CRYPTONIGHT_LITE:
applog(LOG_INFO, "thread %d: %lu hashes, %.2f H/s", thr_id,
hashes_done, thr_hashrates[thr_id]);
break;
Expand All @@ -1254,6 +1262,7 @@ static void *miner_thread(void *userdata) {
if (i == opt_n_threads) {
switch(opt_algo) {
case ALGO_CRYPTONIGHT:
case ALGO_CRYPTONIGHT_LITE:
applog(LOG_INFO, "Total: %s H/s", hashrate);
break;
default:
Expand Down Expand Up @@ -1846,7 +1855,7 @@ int main(int argc, char *argv[]) {
init_quarkhash_contexts();
} else if (opt_algo == ALGO_BLAKE) {
init_blakehash_contexts();
} else if(opt_algo == ALGO_CRYPTONIGHT) {
} else if(opt_algo == ALGO_CRYPTONIGHT||opt_algo == ALGO_CRYPTONIGHT_LITE) {
jsonrpc_2 = true;
aes_ni_supported = has_aes_ni();
applog(LOG_INFO, "Using JSON-RPC 2.0");
Expand Down
26 changes: 13 additions & 13 deletions cryptonight.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,14 @@ struct cryptonight_ctx {
oaes_ctx* aes_ctx;
};

void cryptonight_hash_ctx(void* output, const void* input, size_t len, struct cryptonight_ctx* ctx) {
void cryptonight_hash_ctx(void* output, const void* input, size_t len, struct cryptonight_ctx* ctx, int lite) {
hash_process(&ctx->state.hs, (const uint8_t*) input, len);
ctx->aes_ctx = (oaes_ctx*) oaes_alloc();
size_t i, j;
memcpy(ctx->text, ctx->state.init, INIT_SIZE_BYTE);

oaes_key_import_data(ctx->aes_ctx, ctx->state.hs.b, AES_KEY_SIZE);
for (i = 0; likely(i < MEMORY); i += INIT_SIZE_BYTE) {
for (i = 0; likely(i < MEMORY/(lite?2:1)); i += INIT_SIZE_BYTE) {
aesb_pseudo_round_mut(&ctx->text[AES_BLOCK_SIZE * 0], ctx->aes_ctx->key->exp_data);
aesb_pseudo_round_mut(&ctx->text[AES_BLOCK_SIZE * 1], ctx->aes_ctx->key->exp_data);
aesb_pseudo_round_mut(&ctx->text[AES_BLOCK_SIZE * 2], ctx->aes_ctx->key->exp_data);
Expand All @@ -134,7 +134,7 @@ void cryptonight_hash_ctx(void* output, const void* input, size_t len, struct cr
xor_blocks_dst(&ctx->state.k[0], &ctx->state.k[32], ctx->a);
xor_blocks_dst(&ctx->state.k[16], &ctx->state.k[48], ctx->b);

for (i = 0; likely(i < ITER / 4); ++i) {
for (i = 0; likely(i < ITER / 4 / (lite?2:1)); ++i) {
/* Dependency chain: address -> read value ------+
* written value <-+ hard function (AES or MUL) <+
* next address <-+
Expand All @@ -155,7 +155,7 @@ void cryptonight_hash_ctx(void* output, const void* input, size_t len, struct cr

memcpy(ctx->text, ctx->state.init, INIT_SIZE_BYTE);
oaes_key_import_data(ctx->aes_ctx, &ctx->state.hs.b[32], AES_KEY_SIZE);
for (i = 0; likely(i < MEMORY); i += INIT_SIZE_BYTE) {
for (i = 0; likely(i < MEMORY/(lite?2:1)); i += INIT_SIZE_BYTE) {
xor_blocks(&ctx->text[0 * AES_BLOCK_SIZE], &ctx->long_state[i + 0 * AES_BLOCK_SIZE]);
aesb_pseudo_round_mut(&ctx->text[0 * AES_BLOCK_SIZE], ctx->aes_ctx->key->exp_data);
xor_blocks(&ctx->text[1 * AES_BLOCK_SIZE], &ctx->long_state[i + 1 * AES_BLOCK_SIZE]);
Expand All @@ -180,20 +180,20 @@ void cryptonight_hash_ctx(void* output, const void* input, size_t len, struct cr
oaes_free((OAES_CTX **) &ctx->aes_ctx);
}

void cryptonight_hash(void* output, const void* input, size_t len) {
void cryptonight_hash(void* output, const void* input, size_t len, int lite) {
struct cryptonight_ctx *ctx = (struct cryptonight_ctx*)malloc(sizeof(struct cryptonight_ctx));
cryptonight_hash_ctx(output, input, len, ctx);
cryptonight_hash_ctx(output, input, len, ctx, lite);
free(ctx);
}

void cryptonight_hash_ctx_aes_ni(void* output, const void* input, size_t len, struct cryptonight_ctx* ctx) {
void cryptonight_hash_ctx_aes_ni(void* output, const void* input, size_t len, struct cryptonight_ctx* ctx, int lite) {
hash_process(&ctx->state.hs, (const uint8_t*) input, len);
ctx->aes_ctx = (oaes_ctx*) oaes_alloc();
size_t i, j;
memcpy(ctx->text, ctx->state.init, INIT_SIZE_BYTE);

oaes_key_import_data(ctx->aes_ctx, ctx->state.hs.b, AES_KEY_SIZE);
for (i = 0; likely(i < MEMORY); i += INIT_SIZE_BYTE) {
for (i = 0; likely(i < MEMORY/(lite?2:1)); i += INIT_SIZE_BYTE) {
fast_aesb_pseudo_round_mut(&ctx->text[AES_BLOCK_SIZE * 0], ctx->aes_ctx->key->exp_data);
fast_aesb_pseudo_round_mut(&ctx->text[AES_BLOCK_SIZE * 1], ctx->aes_ctx->key->exp_data);
fast_aesb_pseudo_round_mut(&ctx->text[AES_BLOCK_SIZE * 2], ctx->aes_ctx->key->exp_data);
Expand All @@ -208,7 +208,7 @@ void cryptonight_hash_ctx_aes_ni(void* output, const void* input, size_t len, st
xor_blocks_dst(&ctx->state.k[0], &ctx->state.k[32], ctx->a);
xor_blocks_dst(&ctx->state.k[16], &ctx->state.k[48], ctx->b);

for (i = 0; likely(i < ITER / 4); ++i) {
for (i = 0; likely(i < ITER / 4 / (lite?2:1)); ++i) {
/* Dependency chain: address -> read value ------+
* written value <-+ hard function (AES or MUL) <+
* next address <-+
Expand All @@ -229,7 +229,7 @@ void cryptonight_hash_ctx_aes_ni(void* output, const void* input, size_t len, st

memcpy(ctx->text, ctx->state.init, INIT_SIZE_BYTE);
oaes_key_import_data(ctx->aes_ctx, &ctx->state.hs.b[32], AES_KEY_SIZE);
for (i = 0; likely(i < MEMORY); i += INIT_SIZE_BYTE) {
for (i = 0; likely(i < MEMORY/(lite?2:1)); i += INIT_SIZE_BYTE) {
xor_blocks(&ctx->text[0 * AES_BLOCK_SIZE], &ctx->long_state[i + 0 * AES_BLOCK_SIZE]);
fast_aesb_pseudo_round_mut(&ctx->text[0 * AES_BLOCK_SIZE], ctx->aes_ctx->key->exp_data);
xor_blocks(&ctx->text[1 * AES_BLOCK_SIZE], &ctx->long_state[i + 1 * AES_BLOCK_SIZE]);
Expand All @@ -255,7 +255,7 @@ void cryptonight_hash_ctx_aes_ni(void* output, const void* input, size_t len, st
}

int scanhash_cryptonight(int thr_id, uint32_t *pdata, const uint32_t *ptarget,
uint32_t max_nonce, uint64_t *hashes_done) {
uint32_t max_nonce, uint64_t *hashes_done, int lite) {
uint32_t *nonceptr = (uint32_t*) (((char*)pdata) + 39);
uint32_t n = *nonceptr - 1;
const uint32_t first_nonce = n + 1;
Expand All @@ -267,7 +267,7 @@ int scanhash_cryptonight(int thr_id, uint32_t *pdata, const uint32_t *ptarget,
if (aes_ni_supported) {
do {
*nonceptr = ++n;
cryptonight_hash_ctx_aes_ni(hash, pdata, 76, ctx);
cryptonight_hash_ctx_aes_ni(hash, pdata, 76, ctx, lite);
if (unlikely(hash[7] < ptarget[7])) {
*hashes_done = n - first_nonce + 1;
free(ctx);
Expand All @@ -277,7 +277,7 @@ int scanhash_cryptonight(int thr_id, uint32_t *pdata, const uint32_t *ptarget,
} else {
do {
*nonceptr = ++n;
cryptonight_hash_ctx(hash, pdata, 76, ctx);
cryptonight_hash_ctx(hash, pdata, 76, ctx, lite);
if (unlikely(hash[7] < ptarget[7])) {
*hashes_done = n - first_nonce + 1;
free(ctx);
Expand Down
4 changes: 2 additions & 2 deletions miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,10 @@ extern int scanhash_x14(int thr_id, uint32_t *pdata, const uint32_t *ptarget,
extern int scanhash_x15(int thr_id, uint32_t *pdata, const uint32_t *ptarget,
uint32_t max_nonce, uint64_t *hashes_done);

extern void cryptonight_hash(void* output, const void* input, size_t input_len);
extern void cryptonight_hash(void* output, const void* input, size_t input_len, int lite);

extern int scanhash_cryptonight(int thr_id, uint32_t *pdata, const uint32_t *ptarget,
uint32_t max_nonce, uint64_t *hashes_done);
uint32_t max_nonce, uint64_t *hashes_done, int lite);

struct thr_info {
int id;
Expand Down

0 comments on commit bfbfdc5

Please sign in to comment.