Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
tpruvot
GitHub Repository: tpruvot/cpuminer-multi
Path: blob/linux/algo/lbry.c
1201 views
1
/**
2
* Lbry sph Implementation
3
* tpruvot@github July 2016
4
*/
5
6
#include "miner.h"
7
8
#include <string.h>
9
#include <stdint.h>
10
11
#include "sha3/sph_sha2.h"
12
#include "sha3/sph_ripemd.h"
13
14
#define A 64
15
16
typedef struct {
17
sph_sha256_context sha256;
18
sph_sha512_context sha512;
19
sph_ripemd160_context ripemd;
20
} lbryhash_context_holder;
21
22
static __thread lbryhash_context_holder ctx;
23
static __thread bool ctx_init = false;
24
25
static void lbry_initstate()
26
{
27
sph_sha256_init(&ctx.sha256);
28
sph_sha512_init(&ctx.sha512);
29
sph_ripemd160_init(&ctx.ripemd);
30
ctx_init = true;
31
}
32
33
void lbry_hash(void* output, const void* input)
34
{
35
uint32_t _ALIGN(A) hashA[16];
36
uint32_t _ALIGN(A) hashB[8];
37
uint32_t _ALIGN(A) hashC[8];
38
39
//memset(&hashA[8], 0, 32);
40
41
// sha256d
42
sph_sha256(&ctx.sha256, input, 112);
43
sph_sha256_close(&ctx.sha256, hashA);
44
sph_sha256(&ctx.sha256, hashA, 32);
45
sph_sha256_close(&ctx.sha256, hashA);
46
47
sph_sha512(&ctx.sha512, hashA, 32);
48
sph_sha512_close(&ctx.sha512, hashA);
49
50
sph_ripemd160(&ctx.ripemd, hashA, 32);
51
sph_ripemd160_close(&ctx.ripemd, hashB);
52
53
sph_ripemd160(&ctx.ripemd, &hashA[8], 32); // weird
54
sph_ripemd160_close(&ctx.ripemd, hashC);
55
56
sph_sha256(&ctx.sha256, hashB, 20);
57
sph_sha256(&ctx.sha256, hashC, 20);
58
sph_sha256_close(&ctx.sha256, hashA);
59
60
sph_sha256(&ctx.sha256, hashA, 32);
61
sph_sha256_close(&ctx.sha256, hashA);
62
63
memcpy(output, hashA, 32);
64
}
65
66
int scanhash_lbry(int thr_id, struct work *work, uint32_t max_nonce, uint64_t *hashes_done)
67
{
68
uint32_t _ALIGN(A) vhashcpu[8];
69
uint32_t _ALIGN(A) endiandata[28];
70
uint32_t *pdata = work->data;
71
uint32_t *ptarget = work->target;
72
73
const uint32_t Htarg = ptarget[7];
74
const uint32_t first_nonce = pdata[27];
75
76
uint32_t n = first_nonce;
77
78
for (int i=0; i < 27; i++) {
79
be32enc(&endiandata[i], pdata[i]);
80
}
81
82
if (!ctx_init) lbry_initstate();
83
84
do {
85
be32enc(&endiandata[27], n);
86
lbry_hash(vhashcpu, endiandata);
87
88
if (vhashcpu[7] <= Htarg && fulltest(vhashcpu, ptarget)) {
89
work_set_target_ratio(work, vhashcpu);
90
*hashes_done = n - first_nonce + 1;
91
work->resnonce = pdata[27] = n; // to check
92
return 1;
93
}
94
n++;
95
96
} while (n < max_nonce && !work_restart[thr_id].restart);
97
98
*hashes_done = n - first_nonce + 1;
99
pdata[27] = n;
100
101
return 0;
102
}
103
104