Skip to content

Commit c2c5865

Browse files
committed
support for lyra2h
Add support for lyra2h for HPP coin
1 parent d7898b9 commit c2c5865

File tree

13 files changed

+1031
-17
lines changed

13 files changed

+1031
-17
lines changed

Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ sgminer_SOURCES += algorithm/ethash.c algorithm/ethgencache.c algorithm/ethash.h
8484
sgminer_SOURCES += algorithm/cryptonight.c algorithm/cryptonight.h algorithm/cn-aes-tbls.h
8585
sgminer_SOURCES += algorithm/equihash.c algorithm/equihash.h
8686
sgminer_SOURCES += algorithm/lyra2Z.c algorithm/lyra2Z.h
87+
sgminer_SOURCES += algorithm/lyra2h.c algorithm/lyra2h.h
8788
sgminer_SOURCES += gbt-util.c gbt-util.h
8889

8990
bin_SCRIPTS = $(top_srcdir)/kernel/*.cl

algorithm.c

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include "algorithm/cryptonight.h"
4545
#include "algorithm/equihash.h"
4646
#include "algorithm/lyra2Z.h"
47+
#include "algorithm/lyra2h.h"
4748
#include "compat.h"
4849

4950
#include <inttypes.h>
@@ -78,7 +79,8 @@ const char *algorithm_type_str[] = {
7879
"Ethash",
7980
"Cryptonight",
8081
"Equihash",
81-
"Lyra2Z"
82+
"Lyra2Z",
83+
"Lyra2h"
8284
};
8385

8486
void sha256(const unsigned char *message, unsigned int len, unsigned char *digest)
@@ -954,6 +956,45 @@ static cl_int queue_lyra2z_kernel(struct __clState *clState, struct _dev_blk_ctx
954956
}
955957

956958

959+
static cl_int queue_lyra2h_kernel(struct __clState *clState, struct _dev_blk_ctx *blk, __maybe_unused cl_uint threads)
960+
{
961+
cl_kernel *kernel;
962+
unsigned int num;
963+
cl_int status = 0;
964+
cl_ulong le_target;
965+
966+
// le_target = *(cl_uint *)(blk->work->device_target + 28);
967+
le_target = *(cl_ulong *)(blk->work->device_target + 24);
968+
flip80(clState->cldata, blk->work->data);
969+
status = clEnqueueWriteBuffer(clState->commandQueue, clState->CLbuffer0, true, 0, 80, clState->cldata, 0, NULL, NULL);
970+
971+
// blake - search
972+
kernel = &clState->kernel;
973+
num = 0;
974+
// CL_SET_ARG(clState->CLbuffer0);
975+
CL_SET_ARG(clState->buffer1);
976+
CL_SET_ARG(blk->work->blk.ctx_a);
977+
CL_SET_ARG(blk->work->blk.ctx_b);
978+
CL_SET_ARG(blk->work->blk.ctx_c);
979+
CL_SET_ARG(blk->work->blk.ctx_d);
980+
CL_SET_ARG(blk->work->blk.ctx_e);
981+
CL_SET_ARG(blk->work->blk.ctx_f);
982+
CL_SET_ARG(blk->work->blk.ctx_g);
983+
CL_SET_ARG(blk->work->blk.ctx_h);
984+
CL_SET_ARG(blk->work->blk.cty_a);
985+
CL_SET_ARG(blk->work->blk.cty_b);
986+
CL_SET_ARG(blk->work->blk.cty_c);
987+
num = 0;
988+
// keccak - search1
989+
kernel = clState->extra_kernels;
990+
CL_SET_ARG(clState->buffer1);
991+
CL_SET_ARG(clState->Scratchpads);
992+
CL_SET_ARG(clState->outputBuffer);
993+
CL_SET_ARG(le_target);
994+
return status;
995+
}
996+
997+
957998
static cl_int queue_pluck_kernel(_clState *clState, dev_blk_ctx *blk, __maybe_unused cl_uint threads)
958999
{
9591000
cl_kernel *kernel = &clState->kernel;
@@ -1317,6 +1358,7 @@ static algorithm_settings_t algos[] = {
13171358
{ "lyra2re", ALGO_LYRA2RE, "", 1, 128, 128, 0, 0, 0xFF, 0xFFFFULL, 0x0000ffffUL, 4, 2 * 8 * 4194304, 0, lyra2re_regenhash, precalc_hash_blake256, queue_lyra2re_kernel, gen_hash, NULL },
13181359
{ "lyra2rev2", ALGO_LYRA2REV2, "", 1, 256, 256, 0, 0, 0xFF, 0xFFFFULL, 0x0000ffffUL, 6, -1, CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE, lyra2rev2_regenhash, precalc_hash_blake256, queue_lyra2rev2_kernel, gen_hash, append_neoscrypt_compiler_options },
13191360
{ "lyra2Z" , ALGO_LYRA2Z , "", 1, 256, 256, 0, 0, 0xFF, 0xFFFFULL, 0x0000ffffUL, 1, 0,0, lyra2Z_regenhash , precalc_hash_blake256, queue_lyra2z_kernel , gen_hash, NULL },
1361+
{ "lyra2h" , ALGO_LYRA2H , "", 1, 256, 256, 0, 0, 0xFF, 0xFFFFULL, 0x0000ffffUL, 1, 0,0, lyra2h_regenhash , precalc_hash_blake256, queue_lyra2h_kernel , gen_hash, NULL },
13201362

13211363
// kernels starting from this will have difficulty calculated by using fuguecoin algorithm
13221364
#define A_FUGUE(a, b, c) \

algorithm.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ typedef enum {
4141
ALGO_CRYPTONIGHT,
4242
ALGO_EQUIHASH,
4343
ALGO_LBRY,
44-
ALGO_LYRA2Z
44+
ALGO_LYRA2Z,
45+
ALGO_LYRA2H,
4546
} algorithm_type_t;
4647

4748
extern const char *algorithm_type_str[];

algorithm/lyra2h.c

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/*-
2+
* Copyright 2017 djm34
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
* 1. Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in the
12+
* documentation and/or other materials provided with the distribution.
13+
*
14+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17+
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24+
* SUCH DAMAGE.
25+
*/
26+
27+
#include "config.h"
28+
#include "miner.h"
29+
30+
#include <stdlib.h>
31+
#include <stdint.h>
32+
#include <string.h>
33+
34+
#include "sph/sph_blake.h"
35+
#include "sph/sph_groestl.h"
36+
#include "sph/sph_skein.h"
37+
#include "sph/sph_keccak.h"
38+
#include "sph/sph_bmw.h"
39+
#include "sph/sph_cubehash.h"
40+
#include "lyra2.h"
41+
42+
/*
43+
* Encode a length len/4 vector of (uint32_t) into a length len vector of
44+
* (unsigned char) in big-endian form. Assumes len is a multiple of 4.
45+
*/
46+
static inline void
47+
be32enc_vect(uint32_t *dst, const uint32_t *src, uint32_t len)
48+
{
49+
uint32_t i;
50+
51+
for (i = 0; i < len; i++)
52+
dst[i] = htobe32(src[i]);
53+
}
54+
55+
56+
inline void lyra2hhash(void *state, const void *input)
57+
{
58+
sph_blake256_context ctx_blake;
59+
60+
uint32_t hashA[8], hashB[8];
61+
62+
sph_blake256_init(&ctx_blake);
63+
sph_blake256 (&ctx_blake, input, 80);
64+
sph_blake256_close (&ctx_blake, hashA);
65+
66+
// printf("cpu hashA %08x %08x %08x %08x %08x %08x %08x %08x\n",
67+
// hashA[0], hashA[1], hashA[2], hashA[3], hashA[4], hashA[5], hashA[6], hashA[7]);
68+
69+
LYRA2(hashB, 32, hashA, 32, hashA, 32, 16, 16, 16);
70+
71+
//printf("cpu hashB %08x %08x %08x %08x %08x %08x %08x %08x\n",
72+
//hashB[0],hashB[1],hashB[2],hashB[3], hashB[4], hashB[5], hashB[6], hashB[7]);
73+
74+
memcpy(state, hashB, 32);
75+
}
76+
77+
static const uint32_t diff1targ = 0x0000ffff;
78+
79+
80+
/* Used externally as confirmation of correct OCL code */
81+
int lyra2h_test(unsigned char *pdata, const unsigned char *ptarget, uint32_t nonce)
82+
{
83+
uint32_t tmp_hash7, Htarg = le32toh(((const uint32_t *)ptarget)[7]);
84+
uint32_t data[20], ohash[8];
85+
86+
be32enc_vect(data, (const uint32_t *)pdata, 19);
87+
data[19] = htobe32(nonce);
88+
lyra2hhash(ohash, data);
89+
tmp_hash7 = be32toh(ohash[7]);
90+
91+
applog(LOG_DEBUG, "htarget %08lx diff1 %08lx hash %08lx",
92+
(long unsigned int)Htarg,
93+
(long unsigned int)diff1targ,
94+
(long unsigned int)tmp_hash7);
95+
if (tmp_hash7 > diff1targ)
96+
return -1;
97+
if (tmp_hash7 > Htarg)
98+
return 0;
99+
return 1;
100+
}
101+
102+
void lyra2h_regenhash(struct work *work)
103+
{
104+
uint32_t data[20];
105+
uint32_t *nonce = (uint32_t *)(work->data + 76);
106+
uint32_t *ohash = (uint32_t *)(work->hash);
107+
108+
be32enc_vect(data, (const uint32_t *)work->data, 19);
109+
data[19] = htobe32(*nonce);
110+
lyra2hhash(ohash, data);
111+
}
112+
113+
bool scanhash_lyra2h(struct thr_info *thr, const unsigned char __maybe_unused *pmidstate,
114+
unsigned char *pdata, unsigned char __maybe_unused *phash1,
115+
unsigned char __maybe_unused *phash, const unsigned char *ptarget,
116+
uint32_t max_nonce, uint32_t *last_nonce, uint32_t n)
117+
{
118+
uint32_t *nonce = (uint32_t *)(pdata + 76);
119+
uint32_t data[20];
120+
uint32_t tmp_hash7;
121+
uint32_t Htarg = le32toh(((const uint32_t *)ptarget)[7]);
122+
bool ret = false;
123+
124+
be32enc_vect(data, (const uint32_t *)pdata, 19);
125+
126+
while(1) {
127+
uint32_t ostate[8];
128+
129+
*nonce = ++n;
130+
data[19] = (n);
131+
lyra2hhash(ostate, data);
132+
tmp_hash7 = (ostate[7]);
133+
134+
applog(LOG_INFO, "data7 %08lx",
135+
(long unsigned int)data[7]);
136+
137+
if (unlikely(tmp_hash7 <= Htarg)) {
138+
((uint32_t *)pdata)[19] = htobe32(n);
139+
*last_nonce = n;
140+
ret = true;
141+
break;
142+
}
143+
144+
if (unlikely((n >= max_nonce) || thr->work_restart)) {
145+
*last_nonce = n;
146+
break;
147+
}
148+
}
149+
150+
return ret;
151+
}
152+
153+
154+

algorithm/lyra2h.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef LYRA2H_H
2+
#define LYRA2H_H
3+
4+
#include "miner.h"
5+
//#define LYRA2H_SCRATCHBUF_SIZE (24576) // matrix size [12][16][16] uint64_t or equivalent
6+
#define LYRA2H_SCRATCHBUF_SIZE (12*16*16)
7+
// #define LYRA_SCRATCHBUF_SIZE (1536)
8+
#define LYRA_SECBUF_SIZE (4) // (not used)
9+
extern int lyra2h_test(unsigned char *pdata, const unsigned char *ptarget,
10+
uint32_t nonce);
11+
extern void lyra2h_regenhash(struct work *work);
12+
13+
#endif /* LYRA2H_H */

0 commit comments

Comments
 (0)