Skip to content

Commit

Permalink
lbry sha/ripemd algo
Browse files Browse the repository at this point in the history
beware, different data size and different stratum protocol (notify)
  • Loading branch information
tpruvot committed Jul 10, 2016
1 parent 22dd788 commit f9345a5
Show file tree
Hide file tree
Showing 11 changed files with 1,276 additions and 15 deletions.
2 changes: 2 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ cpuminer_SOURCES = \
sha3/sph_hamsi.c \
sha3/sph_fugue.c \
sha3/sph_gost.c \
sha3/sph_ripemd.c \
sha3/sph_sha2.c \
sha3/sph_sha2big.c \
sha3/sph_shabal.c \
Expand Down Expand Up @@ -66,6 +67,7 @@ cpuminer_SOURCES = \
algo/groestl.c \
algo/heavy.c \
algo/ink.c \
algo/lbry.c \
algo/luffa.c \
algo/lyra2re.c \
algo/lyra2rev2.c \
Expand Down
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Version 1.3 (Tanguy Pruvot)
- Add decred algo
- Add lbry algo
- Add x11evo algo
- Enhance Blake2-S
- Stratum benchmarks support
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Algorithms
*__dmd-gr__ (Diamond-Groestl)
*__fresh__ (FreshCoin)
*__groestl__ (Groestlcoin)
*__lbry__ (LBRY Credits [LBC])
*__lyra2RE__ (Lyrabar, Cryptocoin)
*__lyra2REv2__ (VertCoin [VTC])
*__myr-gr__ (Myriad-Groestl)
Expand Down
102 changes: 102 additions & 0 deletions algo/lbry.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* Lbry sph Implementation
* tpruvot@github July 2016
*/

#include "miner.h"

#include <string.h>
#include <stdint.h>

#include "sha3/sph_sha2.h"
#include "sha3/sph_ripemd.h"

#define A 64

typedef struct {
sph_sha256_context sha256;
sph_sha512_context sha512;
sph_ripemd160_context ripemd;
} lbryhash_context_holder;

static __thread lbryhash_context_holder ctx;
static __thread bool ctx_init = false;

static void lbry_initstate()
{
sph_sha256_init(&ctx.sha256);
sph_sha512_init(&ctx.sha512);
sph_ripemd160_init(&ctx.ripemd);
ctx_init = true;
}

void lbry_hash(void* output, const void* input)
{
uint32_t _ALIGN(A) hashA[16];
uint32_t _ALIGN(A) hashB[8];
uint32_t _ALIGN(A) hashC[8];

//memset(&hashA[8], 0, 32);

// sha256d
sph_sha256(&ctx.sha256, input, 112);
sph_sha256_close(&ctx.sha256, hashA);
sph_sha256(&ctx.sha256, hashA, 32);
sph_sha256_close(&ctx.sha256, hashA);

sph_sha512(&ctx.sha512, hashA, 32);
sph_sha512_close(&ctx.sha512, hashA);

sph_ripemd160(&ctx.ripemd, hashA, 32);
sph_ripemd160_close(&ctx.ripemd, hashB);

sph_ripemd160(&ctx.ripemd, &hashA[8], 32); // weird
sph_ripemd160_close(&ctx.ripemd, hashC);

sph_sha256(&ctx.sha256, hashB, 20);
sph_sha256(&ctx.sha256, hashC, 20);
sph_sha256_close(&ctx.sha256, hashA);

sph_sha256(&ctx.sha256, hashA, 32);
sph_sha256_close(&ctx.sha256, hashA);

memcpy(output, hashA, 32);
}

int scanhash_lbry(int thr_id, struct work *work, uint32_t max_nonce, uint64_t *hashes_done)
{
uint32_t _ALIGN(A) vhashcpu[8];
uint32_t _ALIGN(A) endiandata[28];
uint32_t *pdata = work->data;
uint32_t *ptarget = work->target;

const uint32_t Htarg = ptarget[7];
const uint32_t first_nonce = pdata[27];

uint32_t n = first_nonce;

for (int i=0; i < 27; i++) {
be32enc(&endiandata[i], pdata[i]);
}

if (!ctx_init) lbry_initstate();

do {
be32enc(&endiandata[27], n);
lbry_hash(vhashcpu, endiandata);

if (vhashcpu[7] <= Htarg && fulltest(vhashcpu, ptarget)) {
work_set_target_ratio(work, vhashcpu);
*hashes_done = n - first_nonce + 1;
work->resnonce = pdata[27] = n; // to check
return 1;
}
n++;

} while (n < max_nonce && !work_restart[thr_id].restart);

*hashes_done = n - first_nonce + 1;
pdata[27] = n;

return 0;
}
24 changes: 22 additions & 2 deletions cpu-miner.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ enum algos {
ALGO_DROP, /* Dropcoin */
ALGO_FRESH, /* Fresh */
ALGO_GROESTL, /* Groestl */
ALGO_LBRY, /* Lbry Sha Ripemd */
ALGO_LUFFA, /* Luffa (Joincoin, Doom) */
ALGO_LYRA2, /* Lyra2RE */
ALGO_LYRA2REV2, /* Lyra2REv2 (Vertcoin) */
Expand Down Expand Up @@ -141,6 +142,7 @@ static const char *algo_names[] = {
"drop",
"fresh",
"groestl",
"lbry",
"luffa",
"lyra2re",
"lyra2rev2",
Expand Down Expand Up @@ -529,18 +531,17 @@ static void calc_network_diff(struct work *work)
// sample for diff 43.281 : 1c05ea29
// todo: endian reversed on longpoll could be zr5 specific...
uint32_t nbits = have_longpoll ? work->data[18] : swab32(work->data[18]);
if (opt_algo == ALGO_LBRY) nbits = 0; // not found
if (opt_algo == ALGO_DECRED) nbits = work->data[29];
uint32_t bits = (nbits & 0xffffff);
int16_t shift = (swab32(nbits) & 0xff); // 0x1c = 28

double d = (double)0x0000ffff / (double)bits;

for (int m=shift; m < 29; m++) d *= 256.0;
for (int m=29; m < shift; m++) d /= 256.0;
if (opt_algo == ALGO_DECRED && shift == 28) d *= 256.0; // testnet
if (opt_debug_diff)
applog(LOG_DEBUG, "net diff: %f -> shift %u, bits %08x", d, shift, bits);

net_diff = d;
}

Expand Down Expand Up @@ -1084,6 +1085,10 @@ static bool submit_upstream_work(CURL *curl, struct work *work)
be32enc(&ntime, work->data[34]);
be32enc(&nonce, work->data[35]);
break;
case ALGO_LBRY:
le32enc(&ntime, work->data[25]);
le32enc(&nonce, work->data[27]);
break;
case ALGO_DROP:
case ALGO_NEOSCRYPT:
case ALGO_ZR5:
Expand Down Expand Up @@ -1692,6 +1697,13 @@ static void stratum_gen_work(struct stratum_ctx *sctx, struct work *work)
sctx->bloc_height = work->data[32];
//applog_hex(work->data, 180);
//applog_hex(&work->data[36], 36);
} else if (opt_algo == ALGO_LBRY) {
for (i = 0; i < 8; i++)
work->data[17 + i] = ((uint32_t*)sctx->job.claim)[i];
work->data[25] = le32dec(sctx->job.ntime);
work->data[26] = le32dec(sctx->job.nbits);
// required ?
work->data[28] = 0x80000000;
} else {
work->data[17] = le32dec(sctx->job.ntime);
work->data[18] = le32dec(sctx->job.nbits);
Expand Down Expand Up @@ -1730,6 +1742,7 @@ static void stratum_gen_work(struct stratum_ctx *sctx, struct work *work)
case ALGO_FRESH:
case ALGO_DMD_GR:
case ALGO_GROESTL:
case ALGO_LBRY:
case ALGO_LYRA2REV2:
work_set_target(work, sctx->job.diff / (256.0 * opt_diff_factor));
break;
Expand Down Expand Up @@ -1900,6 +1913,9 @@ static void *miner_thread(void *userdata)
} else if (opt_algo == ALGO_DECRED) {
wkcmp_sz = nonce_oft = 140; // 35 * 4
regen_work = true; // ntime not changed ?
} else if (opt_algo == ALGO_LBRY) {
wkcmp_sz = nonce_oft = 108; // 27
//regen_work = true;
}

if (jsonrpc_2) {
Expand Down Expand Up @@ -2052,6 +2068,7 @@ static void *miner_thread(void *userdata)
case ALGO_X14:
max64 = 0x3ffff;
break;
case ALGO_LBRY:
case ALGO_X15:
case ALGO_ZR5:
max64 = 0x1ffff;
Expand Down Expand Up @@ -2135,6 +2152,9 @@ static void *miner_thread(void *userdata)
case ALGO_HEAVY:
rc = scanhash_heavy(thr_id, &work, max_nonce, &hashes_done);
break;
case ALGO_LBRY:
rc = scanhash_lbry(thr_id, &work, max_nonce, &hashes_done);
break;
case ALGO_LUFFA:
rc = scanhash_luffa(thr_id, &work, max_nonce, &hashes_done);
break;
Expand Down
2 changes: 2 additions & 0 deletions cpuminer.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@
<ClCompile Include="algo\heavy.c" />
<ClCompile Include="algo\keccak.c" />
<ClCompile Include="algo\ink.c" />
<ClCompile Include="algo\lbry.c" />
<ClCompile Include="algo\luffa.c" />
<ClCompile Include="algo\lyra2re.c" />
<ClCompile Include="algo\lyra2rev2.c" />
Expand Down Expand Up @@ -259,6 +260,7 @@
<ClCompile Include="sha3\sph_jh.c" />
<ClCompile Include="sha3\sph_keccak.c" />
<ClCompile Include="sha3\sph_luffa.c" />
<ClCompile Include="sha3\sph_ripemd.c" />
<ClCompile Include="sha3\sph_sha2.c" />
<ClCompile Include="sha3\sph_sha2big.c" />
<ClCompile Include="sha3\sph_shabal.c" />
Expand Down
6 changes: 6 additions & 0 deletions cpuminer.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
<ClCompile Include="sha3\sph_luffa.c">
<Filter>sph</Filter>
</ClCompile>
<ClCompile Include="sha3\sph_ripemd.c">
<Filter>sph</Filter>
</ClCompile>
<ClCompile Include="sha3\sph_sha2.c">
<Filter>sph</Filter>
</ClCompile>
Expand Down Expand Up @@ -204,6 +207,9 @@
<ClCompile Include="algo\keccak.c">
<Filter>algo</Filter>
</ClCompile>
<ClCompile Include="algo\lbry.c">
<Filter>algo</Filter>
</ClCompile>
<ClCompile Include="algo\luffa.c">
<Filter>algo</Filter>
</ClCompile>
Expand Down
4 changes: 4 additions & 0 deletions miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ int scanhash_groestl(int thr_id, struct work *work, uint32_t max_nonce, uint64_t
int scanhash_heavy(int thr_id, struct work *work, uint32_t max_nonce, uint64_t *hashes_done);
int scanhash_ink(int thr_id, struct work *work, uint32_t max_nonce, uint64_t *hashes_done);
int scanhash_keccak(int thr_id, struct work *work, uint32_t max_nonce, uint64_t *hashes_done);
int scanhash_lbry(int thr_id, struct work *work, uint32_t max_nonce, uint64_t *hashes_done);
int scanhash_luffa(int thr_id, struct work *work, uint32_t max_nonce, uint64_t *hashes_done);
int scanhash_lyra2(int thr_id, struct work *work, uint32_t max_nonce, uint64_t *hashes_done);
int scanhash_lyra2rev2(int thr_id, struct work *work, uint32_t max_nonce, uint64_t *hashes_done);
Expand Down Expand Up @@ -381,6 +382,7 @@ struct work {
double targetdiff;
double shareratio;
double sharediff;
uint32_t resnonce;

int height;
char *txs;
Expand All @@ -402,6 +404,7 @@ struct stratum_job {
unsigned char version[4];
unsigned char nbits[4];
unsigned char ntime[4];
unsigned char claim[32]; // lbry
bool clean;
double diff;
};
Expand Down Expand Up @@ -494,6 +497,7 @@ void quarkhash(void *state, const void *input);
void freshhash(void* output, const void* input, uint32_t len);
void keccakhash(void *state, const void *input);
void inkhash(void *state, const void *input); /* shavite */
void lbry_hash(void *output, const void *input);
void luffahash(void *output, const void *input);
void lyra2_hash(void *state, const void *input);
void lyra2rev2_hash(void *state, const void *input);
Expand Down
Loading

0 comments on commit f9345a5

Please sign in to comment.