get equihash out of the way, fix erroneous submit flag

This commit is contained in:
Brannon King committed 2019-10-22 13:48:56 -06:00
1 parent 626c62fc89
commit 6d60f7e6ea
13 files changed
+131 -2351

No files matched your search

-1
View File
@@ -83,7 +83,6 @@ sgminer_SOURCES += algorithm/blake256.c algorithm/blake256.h
sgminer_SOURCES += algorithm/blakecoin.c algorithm/blakecoin.h
sgminer_SOURCES += algorithm/ethash.c algorithm/ethgencache.c algorithm/ethash.h algorithm/eth-sha3.c algorithm/eth-sha3.h
sgminer_SOURCES += algorithm/cryptonight.c algorithm/cryptonight.h algorithm/cn-aes-tbls.h
sgminer_SOURCES += algorithm/equihash.c algorithm/equihash.h
sgminer_SOURCES += gbt-util.c gbt-util.h
sgminer_SOURCES += algorithm/lbry.c algorithm/lbry.h
-62
View File
@@ -42,7 +42,6 @@
#include "algorithm/blakecoin.h"
#include "algorithm/ethash.h"
#include "algorithm/cryptonight.h"
#include "algorithm/equihash.h"
#include "algorithm/lbry.h"
#include "compat.h"
@@ -78,7 +77,6 @@ const char *algorithm_type_str[] = {
"Vanilla",
"Ethash",
"Cryptonight",
"Equihash",
"LBRY"
};
@@ -1087,11 +1085,6 @@ static cl_int queue_ethash_kernel(_clState *clState, dev_blk_ctx *blk, __maybe_u
return status;
}
static void append_equihash_compiler_options(struct _build_kernel_data *data, struct cgpu_info *cgpu, struct _algorithm_t *algorithm)
{
strcat(data->compiler_options, "");
}
static cl_int queue_cryptonight_kernel(_clState *clState, dev_blk_ctx *blk, __maybe_unused cl_uint threads)
{
cl_kernel *kernel = &clState->kernel;
@@ -1168,58 +1161,6 @@ static cl_int queue_cryptonight_kernel(_clState *clState, dev_blk_ctx *blk, __ma
return(status);
}
#define WORKSIZE clState->wsize
static cl_int queue_equihash_kernel(_clState *clState, dev_blk_ctx *blk, __maybe_unused cl_uint threads)
{
cl_int status = 0;
size_t work_items = threads;
size_t worksize = clState->wsize;
uint64_t mid_hash[8];
equihash_calc_mid_hash(mid_hash, blk->work->equihash_data);
status = clEnqueueWriteBuffer(clState->commandQueue, clState->MidstateBuf, CL_TRUE, 0, sizeof(mid_hash), mid_hash, 0, NULL, NULL);
uint32_t dbg[2] = {0};
status |= clEnqueueWriteBuffer(clState->commandQueue, clState->padbuffer8, CL_TRUE, 0, sizeof(dbg), &dbg, 0, NULL, NULL);
cl_mem rowCounters[2] = {clState->buffer2, clState->buffer3};
for (int round = 0; round < PARAM_K; round++) {
size_t global_ws = RC_SIZE;
size_t local_ws = 256;
unsigned int num = 0;
cl_kernel *kernel = &clState->extra_kernels[0];
// Now on every round!!!!
CL_SET_ARG(clState->index_buf[round]);
CL_SET_ARG(rowCounters[round % 2]);
CL_SET_ARG(clState->outputBuffer);
CL_SET_ARG(clState->CLbuffer0);
status |= clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, NULL, &global_ws, &local_ws, 0, NULL, NULL);
kernel = &clState->extra_kernels[1 + round];
if (!round) {
worksize = LOCAL_WORK_SIZE_ROUND0;
work_items = NR_INPUTS / ROUND0_INPUTS_PER_WORK_ITEM;
}
else {
worksize = LOCAL_WORK_SIZE;
work_items = NR_ROWS * worksize;
}
status |= clEnqueueNDRangeKernel(clState->commandQueue, clState->extra_kernels[1 + round], 1, NULL, &work_items, &worksize, 0, NULL, NULL);
}
worksize = LOCAL_WORK_SIZE_POTENTIAL_SOLS;
work_items = NR_ROWS * worksize;
status |= clEnqueueNDRangeKernel(clState->commandQueue, clState->extra_kernels[1 + 9], 1, NULL, &work_items, &worksize, 0, NULL, NULL);
worksize = LOCAL_WORK_SIZE_SOLS;
work_items = MAX_POTENTIAL_SOLS * worksize;
status |= clEnqueueNDRangeKernel(clState->commandQueue, clState->kernel, 1, NULL, &work_items, &worksize, 0, NULL, NULL);
return status;
}
#undef WORKSIZE
static cl_int queue_lbry_kernel(struct __clState *clState, struct _dev_blk_ctx *blk, __maybe_unused cl_uint threads)
{
cl_kernel *kernel = &clState->kernel;
@@ -1342,8 +1283,6 @@ static algorithm_settings_t algos[] = {
{ "ethash-new", ALGO_ETHASH, "", 0x100010001LLU, 0x100010001LLU, 0x100010001LLU, 0, 0, 0xFF, 0xFFFF000000000000ULL, 72UL, 0, 128, 0, ethash_regenhash, NULL, queue_ethash_kernel, gen_hash, append_ethash_compiler_options },
{ "cryptonight", ALGO_CRYPTONIGHT, "", 1, 0x100010001LLU, 0x100010001LLU, 0, 0, 0xFF, 0xFFFFULL, 0x0000ffffUL, 6, 0, 0, cryptonight_regenhash, NULL, queue_cryptonight_kernel, gen_hash, NULL },
{ "equihash", ALGO_EQUIHASH, "", 1, (1ULL << 28), (1ULL << 28), 0, 0, 0x20000, 0xFFFF000000000000ULL, 0x00000000UL, 0, 128, 0, equihash_regenhash, NULL, queue_equihash_kernel, gen_hash, append_equihash_compiler_options },
{ "lbry", ALGO_LBRY, "", 1, 256, 256, 0, 0, 0xFF, 0xFFFFULL, 0x0000ffffUL, 2, 4 * 8 * 4194304, 0, lbry_regenhash, NULL, queue_lbry_kernel, gen_hash, NULL },
// Terminator (do not remove)
@@ -1422,7 +1361,6 @@ static const char *lookup_algorithm_alias(const char *lookup_alias, uint8_t *nfa
ALGO_ALIAS("lyra2v2", "lyra2rev2");
ALGO_ALIAS("blakecoin", "blake256r8");
ALGO_ALIAS("blake", "blake256r14");
ALGO_ALIAS("zcash", "equihash");
#undef ALGO_ALIAS
#undef ALGO_ALIAS_NF
-1
View File
@@ -39,7 +39,6 @@ typedef enum {
ALGO_VANILLA,
ALGO_ETHASH,
ALGO_CRYPTONIGHT,
ALGO_EQUIHASH,
ALGO_LBRY
} algorithm_type_t;
-285
View File
@@ -1,285 +0,0 @@
#include <stdint.h>
#include "config.h"
#include "miner.h"
#include "algorithm/equihash.h"
#include "algorithm.h"
#define N 200UL
#define K 9UL
#define COLLISION_BIT_LENGTH (N / (K + 1))
#define COLLISION_BYTE_LENGTH ((COLLISION_BIT_LENGTH + 7) / 8)
#define INIT_SIZE (1 << (COLLISION_BIT_LENGTH + 1))
#define HASH_LENGTH ((K + 1) * COLLISION_BYTE_LENGTH)
#define INDICES_PER_HASH_OUTPUT (512 / N)
#define HASH_OUTPUT (INDICES_PER_HASH_OUTPUT * N/8)
#define rotr64(x, n) (((x) >> (n)) | ((x) << (64 - (n))))
static const uint8_t blake2b_sigma[12][16] = {
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } ,
{ 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 } ,
{ 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 } ,
{ 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 } ,
{ 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 } ,
{ 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 } ,
{ 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 } ,
{ 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 } ,
{ 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 } ,
{ 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0 } ,
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } ,
{ 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 }
};
static const uint64_t blake2b_IV[8] = {
0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL,
0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL,
0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL
};
static const uint64_t blake2b_h[8] = {
0x6a09e667f2bdc93aULL, 0xbb67ae8584caa73bULL,
0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL,
0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
0x48ec89c38820de31ULL, 0x5be0cd10137e21b1ULL
};
// block_header_size: 108 byte
// nonce_size: 32 byte
// birthday_size: 4 byte
// nonce in high part of m[1]
#define G(r,i,a,b,c,d) \
a = a + b + m[blake2b_sigma[r][2*i]]; \
d = rotr64(d ^ a, 32); \
c = c + d; \
b = rotr64(b ^ c, 24); \
a = a + b + m[blake2b_sigma[r][2*i+1]]; \
d = rotr64(d ^ a, 16); \
c = c + d; \
b = rotr64(b ^ c, 63);
#define ROUND(r) \
G(r,0,v[ 0],v[ 4],v[ 8],v[12]); \
G(r,1,v[ 1],v[ 5],v[ 9],v[13]); \
G(r,2,v[ 2],v[ 6],v[10],v[14]); \
G(r,3,v[ 3],v[ 7],v[11],v[15]); \
G(r,4,v[ 0],v[ 5],v[10],v[15]); \
G(r,5,v[ 1],v[ 6],v[11],v[12]); \
G(r,6,v[ 2],v[ 7],v[ 8],v[13]); \
G(r,7,v[ 3],v[ 4],v[ 9],v[14]);
#define G_fast(r,i,a,b,c,d) \
a = a + b + (blake2b_sigma[r][2*i] == 1 ? m1 : 0); \
d = rotr64(d ^ a, 32); \
c = c + d; \
b = rotr64(b ^ c, 24); \
a = a + b + (blake2b_sigma[r][2*i+1] == 1 ? m1 : 0); \
d = rotr64(d ^ a, 16); \
c = c + d; \
b = rotr64(b ^ c, 63);
#define ROUND_fast(r) \
G_fast(r,0,v[ 0],v[ 4],v[ 8],v[12]); \
G_fast(r,1,v[ 1],v[ 5],v[ 9],v[13]); \
G_fast(r,2,v[ 2],v[ 6],v[10],v[14]); \
G_fast(r,3,v[ 3],v[ 7],v[11],v[15]); \
G_fast(r,4,v[ 0],v[ 5],v[10],v[15]); \
G_fast(r,5,v[ 1],v[ 6],v[11],v[12]); \
G_fast(r,6,v[ 2],v[ 7],v[ 8],v[13]); \
G_fast(r,7,v[ 3],v[ 4],v[ 9],v[14]);
void equihash_calc_mid_hash(uint64_t mid_hash[8], uint8_t* header) {
uint64_t v[16], *m = (uint64_t*)header;
for (int i = 0; i < 8; i++) {
v[i] = blake2b_h[i];
v[i + 8] = blake2b_IV[i];
}
v[12] ^= 128;
for (int r = 0; r < 12; r++) {
ROUND(r)
}
for (int i = 0; i < 8; i++)
mid_hash[i] = blake2b_h[i] ^ v[i] ^ v[i + 8];
}
void blake2b_hash(uint8_t *hash, uint64_t mid_hash[8], uint32_t bday) {
uint64_t v[16], tmp[8];
uint64_t m1 = (uint64_t)bday << 32;
for (int i = 0; i < 8; i++) {
v[i] = mid_hash[i];
v[i + 8] = blake2b_IV[i];
}
v[12] ^= 140 + sizeof(bday);
v[14] ^= (int64_t)-1;
for (int r = 0; r < 12; r++) {
ROUND_fast(r)
}
for (int i = 0; i < 8; i++)
tmp[i] = mid_hash[i] ^ v[i] ^ v[i + 8];
memcpy(hash, tmp, 50);
}
void equihash_calc_hash(uint8_t hash[25], uint64_t mid_hash[8], uint32_t bday) {
uint8_t tmp[50];
blake2b_hash(tmp, mid_hash, bday / 2);
memcpy(hash, tmp + (bday & 1 ? 25 : 0), 25);
}
// These two copied from the ref impl, for now.
void ExpandArray(const unsigned char* in, size_t in_len,
unsigned char* out, size_t out_len,
size_t bit_len)
{
size_t byte_pad = 0;
size_t out_width = ((bit_len + 7) / 8 + byte_pad);
uint32_t bit_len_mask = (((uint32_t)1 << bit_len) - 1);
// The acc_bits least-significant bits of acc_value represent a bit sequence
// in big-endian order.
size_t acc_bits = 0;
uint32_t acc_value = 0;
size_t j = 0;
for (size_t i = 0; i < in_len; i++) {
acc_value = (acc_value << 8) | in[i];
acc_bits += 8;
// When we have bit_len or more bits in the accumulator, write the next
// output element.
if (acc_bits >= bit_len) {
acc_bits -= bit_len;
for (size_t x = 0; x < byte_pad; x++) {
out[j + x] = 0;
}
for (size_t x = byte_pad; x < out_width; x++) {
out[j + x] = (
// Big-endian
acc_value >> (acc_bits + (8 * (out_width - x - 1)))
) & (
// Apply bit_len_mask across byte boundaries
(bit_len_mask >> (8 * (out_width - x - 1))) & 0xFF
);
}
j += out_width;
}
}
}
void CompressArray(const unsigned char* in, size_t in_len,
unsigned char* out, size_t out_len,
size_t bit_len, size_t byte_pad)
{
size_t in_width = ((bit_len + 7) / 8 + byte_pad);
uint32_t bit_len_mask = (((uint32_t)1 << bit_len) - 1);
// The acc_bits least-significant bits of acc_value represent a bit sequence
// in big-endian order.
size_t acc_bits = 0;
uint32_t acc_value = 0;
size_t j = 0;
for (size_t i = 0; i < out_len; i++) {
// When we have fewer than 8 bits left in the accumulator, read the next
// input element.
if (acc_bits < 8) {
acc_value = acc_value << bit_len;
for (size_t x = byte_pad; x < in_width; x++) {
acc_value = acc_value | (
(
// Apply bit_len_mask across byte boundaries
in[j + x] & ((bit_len_mask >> (8 * (in_width - x - 1))) & 0xFF)
) << (8 * (in_width - x - 1))
); // Big-endian
}
j += in_width;
acc_bits += bit_len;
}
acc_bits -= 8;
out[i] = (acc_value >> acc_bits) & 0xFF;
}
}
static inline void sort_pair(uint32_t *a, uint32_t len)
{
uint32_t *b = a + len;
uint32_t tmp, need_sorting = 0;
for (uint32_t i = 0; i < len; i++) {
if (need_sorting || a[i] > b[i]) {
need_sorting = 1;
tmp = a[i];
a[i] = b[i];
b[i] = tmp;
} else if (a[i] < b[i])
break;
}
}
bool submit_tested_work(struct thr_info *, struct work *);
uint32_t equihash_verify_sol(struct work *work, sols_t *sols, int sol_i)
{
uint32_t thr_id = work->thr->id;
uint32_t *inputs = sols->values[sol_i];
uint32_t seen_len = (1 << (PREFIX + 1)) / 8;
uint8_t seen[(1 << (PREFIX + 1)) / 8];
uint32_t i;
uint8_t tmp;
// look for duplicate inputs
memset(seen, 0, seen_len);
for (i = 0; i < (1 << PARAM_K); i++) {
if (inputs[i] / 8 >= seen_len) {
sols->valid[sol_i] = 0;
return 0;
}
tmp = seen[inputs[i] / 8];
seen[inputs[i] / 8] |= 1 << (inputs[i] & 7);
if (tmp == seen[inputs[i] / 8]) {
// at least one input value is a duplicate
sols->valid[sol_i] = 0;
return 0;
}
}
// the valid flag is already set by the GPU, but set it again because
// I plan to change the GPU code to not set it
sols->valid[sol_i] = 1;
// sort the pairs in place
for (uint32_t level = 0; level < PARAM_K; level++) {
for (i = 0; i < (1 << PARAM_K); i += (2 << level)) {
sort_pair(&inputs[i], 1 << level);
}
}
for (i = 0; i < (1 << PARAM_K); i++)
inputs[i] = htobe32(inputs[i]);
CompressArray((unsigned char*)inputs, 512 * 4, work->equihash_data + 143, 1344, 21, 1);
gen_hash(work->equihash_data, 1344 + 143, work->hash);
if (*(uint64_t*)(work->hash + 24) < *(uint64_t*)(work->target + 24)) {
submit_tested_work(work->thr, work);
}
return 1;
}
void equihash_regenhash(struct work *work)
{
}
-13
View File
@@ -1,13 +0,0 @@
#ifndef __EQUIHASH_H
#define __EQUIHASH_H
#include <stdint.h>
#include "miner.h"
#include "kernel/equihash-param.h"
uint32_t equihash_verify_sol(struct work *work, sols_t *sols, int sol_i);
void equihash_calc_mid_hash(uint64_t[8], uint8_t*);
void equihash_regenhash(struct work *work);
int64_t equihash_scanhash(struct thr_info *thr, struct work *work, int64_t *last_nonce, int64_t __maybe_unused max_nonce);
#endif // __EQUIHASH_H
+1
View File
@@ -19,6 +19,7 @@ AC_CANONICAL_SYSTEM
AC_CONFIG_MACRO_DIR([m4])
AC_CONFIG_SRCDIR([sgminer.c])
AC_CONFIG_HEADERS([config.h])
AH_TOP([#pragma once])
AM_INIT_AUTOMAKE([foreign subdir-objects])
m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])])
-51
View File
@@ -36,8 +36,6 @@
#include "util.h"
#include "sysfs-gpu-controls.h"
#include "algorithm/equihash.h"
/* TODO: cleanup externals ********************/
#ifdef HAVE_CURSES
@@ -1418,55 +1416,6 @@ static int64_t opencl_scanhash(struct thr_info *thr, struct work *work,
if (hashes > gpu->max_hashes)
gpu->max_hashes = hashes;
if (gpu->algorithm.type == ALGO_EQUIHASH) {
int64_t ret = 0;
size_t length = sizeof(sols_t);
uint64_t t0 = time(NULL);
uint8_t prev_hash[32];
size_t txns;
bool stale = true;
if (work->getwork_mode != GETWORK_MODE_STRATUM) {
cg_rlock(&work->pool->gbt_lock);
txns = work->pool->gbt_txns;
memcpy(prev_hash, work->pool->previousblockhash, 32);
cg_runlock(&work->pool->gbt_lock);
}
thrdata->res = realloc(thrdata->res, length);
sols_t *sols = (sols_t*) thrdata->res;
work->thr = thr;
do {
status = thrdata->queue_kernel_parameters(clState, &work->blk, globalThreads[0]);
status |= clEnqueueReadBuffer(clState->commandQueue, clState->outputBuffer, CL_TRUE, 0, length, thrdata->res, 0, NULL, NULL);
if (status == CL_SUCCESS) {
if (sols->nr > MAX_SOLS) {
applog(LOG_DEBUG, "equihash: %d (probably invalid) solutions were dropped!", sols->nr - MAX_SOLS);
sols->nr = MAX_SOLS;
}
for (int sol_i = 0; sol_i < sols->nr; sol_i++)
ret += equihash_verify_sol(work, sols, sol_i);
}
else {
applog(LOG_ERR, "Error %d: Reading result buffer for ALGO_EQUIHASH failed. (clEnqueueReadBuffer)", status);
return -1;
}
// increase nonce
work->blk.nonce++;
if (work->getwork_mode == GETWORK_MODE_STRATUM)
*(uint16_t*)(work->equihash_data + 108 + strlen(work->nonce1) / 2) += 1;
else {
*(uint64_t*)(work->equihash_data + 108) += 1;
cg_rlock(&work->pool->gbt_lock);
stale = (work->pool->gbt_txns != txns) || (memcmp(prev_hash, work->pool->previousblockhash, 32) != 0);
cg_runlock(&work->pool->gbt_lock);
}
} while (!stale && (time(NULL) - t0) < 2);
return ret;
}
status = thrdata->queue_kernel_parameters(clState, &work->blk, globalThreads[0]);
if (unlikely(status != CL_SUCCESS)) {
if (status > 0)
-404
View File
@@ -1,404 +0,0 @@
// Gateless Gate, a Zcash miner
// Copyright 2016 zawawa @ bitcointalk.org
//
// The initial version of this software was based on:
// SILENTARMY v5
// The MIT License (MIT) Copyright (c) 2016 Marc Bevand, Genoil
//
// This program is free software : you can redistribute it and / or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.If not, see <http://www.gnu.org/licenses/>.
#ifndef __OPENCL_VERSION__
#define uint uint32_t
#define uchar uint8_t
#endif
#ifdef cl_amd_fp64
#define AMD
#endif
#if (defined(__Tahiti__) || defined(__Pitcairn__) || defined(__Capeverde__) || defined(__Oland__)) && !defined(AMD_LEGACY)
#define AMD_LEGACY
#endif
#ifdef cl_nv_pragma_unroll
#define NVIDIA
#endif
//#define ENABLE_DEBUG
//
// Parameters for Hash Tables
//
// There are PARAM_K - 1 hash tables, and each hash table has NR_ROWS rows.
// Each row contains NR_SLOTS slots.
#define NR_ROWS_LOG 12 /* 12, 13, 14, 15, or 16. */
#define NR_SLOTS 684
#define LDS_COLL_SIZE (NR_SLOTS * 67 / 100)
#define LOCAL_WORK_SIZE WORKSIZE
#define LOCAL_WORK_SIZE_SOLS WORKSIZE
#define LOCAL_WORK_SIZE_ROUND0 WORKSIZE
#define LOCAL_WORK_SIZE_POTENTIAL_SOLS WORKSIZE
#define ROUND0_INPUTS_PER_WORK_ITEM 1
#if defined(AMD)
#define THREADS_PER_WRITE(round) (((round) <= 5) ? 2 : 1)
#else
#define THREADS_PER_WRITE(round) 1
#endif
#if defined(AMD) && !defined(AMD_LEGACY)
#define OPTIM_24BYTE_WRITES
#endif
#define OPTIM_16BYTE_WRITES
#if !defined(AMD_LEGACY)
#define OPTIM_8BYTE_WRITES
#endif
//#define OPTIM_FAST_INTEGER_DIVISION
//#define OPTIM_COMPACT_ROW_COUNTERS
#define ADJUSTED_LDS_ARRAY_SIZE(n) (n)
#define PARAM_N 200
#define PARAM_K 9
#define PREFIX (PARAM_N / (PARAM_K + 1))
#define NR_INPUTS (1 << PREFIX)
#define NR_ROWS (1 << NR_ROWS_LOG)
// Length of 1 element (slot) in byte
#define SLOT_LEN 32
#define ADJUSTED_SLOT_LEN(round) (((round) <= 5) ? SLOT_LEN : SLOT_LEN - 16)
// Total size of hash table
#define HT_SIZE (NR_ROWS * NR_SLOTS * SLOT_LEN)
// Length of Zcash block header, nonce (part of header)
#define ZCASH_BLOCK_HEADER_LEN 140
// Offset of nTime in header
#define ZCASH_BLOCK_OFFSET_NTIME (4 + 3 * 32)
// Length of nonce
#define ZCASH_NONCE_LEN 32
// Length of encoded representation of solution size
#define ZCASH_SOLSIZE_LEN 3
// Solution size (1344 = 0x540) represented as a compact integer, in hex
#define ZCASH_SOLSIZE_HEX "fd4005"
// Length of encoded solution (512 * 21 bits / 8 = 1344 bytes)
#define ZCASH_SOL_LEN ((1 << PARAM_K) * (PREFIX + 1) / 8)
// Last N_ZERO_BYTES of nonce must be zero due to my BLAKE2B optimization
#define N_ZERO_BYTES 12
// Number of bytes Zcash needs out of Blake
#define ZCASH_HASH_LEN 50
// Number of wavefronts per SIMD for the Blake kernel.
// Blake is ALU-bound (beside the atomic counter being incremented) so we need
// at least 2 wavefronts per SIMD to hide the 2-clock latency of integer
// instructions. 10 is the max supported by the hw.
#define BLAKE_WPS 10
// Maximum number of solutions reported by kernel to host
#define MAX_SOLS 11
#define MAX_POTENTIAL_SOLS 4096
// Length of SHA256 target
#define SHA256_TARGET_LEN (256 / 8)
#ifdef OPTIM_COMPACT_ROW_COUNTERS
#define BITS_PER_ROW ((NR_SLOTS < 3) ? 2 : \
(NR_SLOTS < 7) ? 3 : \
(NR_SLOTS < 15) ? 4 : \
(NR_SLOTS < 31) ? 5 : \
(NR_SLOTS < 63) ? 6 : \
(NR_SLOTS < 255) ? 8 : \
(NR_SLOTS < 1023) ? 10 : \
16)
#else
#define BITS_PER_ROW ((NR_SLOTS < 3) ? 2 : \
(NR_SLOTS < 15) ? 4 : \
(NR_SLOTS < 255) ? 8 : \
16)
#endif
#define ROWS_PER_UINT (32 / BITS_PER_ROW)
#define ROW_MASK ((1 << BITS_PER_ROW) - 1)
#define RC_SIZE ((NR_ROWS * 4 + ROWS_PER_UINT - 1) / ROWS_PER_UINT)
// An (uncompressed) solution stores (1 << PARAM_K) 32-bit values
#define SOL_SIZE ((1 << PARAM_K) * 4)
typedef struct sols_s
{
uint nr;
uint likely_invalids;
uchar valid[MAX_SOLS];
uint values[MAX_SOLS][(1 << PARAM_K)];
} sols_t;
typedef struct potential_sols_s
{
uint nr;
uint values[MAX_POTENTIAL_SOLS][2];
} potential_sols_t;
#if NR_ROWS_LOG <= 12 && NR_SLOTS <= (1 << 10)
#define ENCODE_INPUTS(row, slot0, slot1) \
((row << 20) | ((slot1 & 0x3ff) << 10) | (slot0 & 0x3ff))
#define DECODE_ROW(REF) (REF >> 20)
#define DECODE_SLOT1(REF) ((REF >> 10) & 0x3ff)
#define DECODE_SLOT0(REF) (REF & 0x3ff)
#elif NR_ROWS_LOG <= 14 && NR_SLOTS <= (1 << 9)
#define ENCODE_INPUTS(row, slot0, slot1) \
((row << 18) | ((slot1 & 0x1ff) << 9) | (slot0 & 0x1ff))
#define DECODE_ROW(REF) (REF >> 18)
#define DECODE_SLOT1(REF) ((REF >> 9) & 0x1ff)
#define DECODE_SLOT0(REF) (REF & 0x1ff)
#elif NR_ROWS_LOG <= 16 && NR_SLOTS <= (1 << 8)
#define ENCODE_INPUTS(row, slot0, slot1) \
((row << 16) | ((slot1 & 0xff) << 8) | (slot0 & 0xff))
#define DECODE_ROW(REF) (REF >> 16)
#define DECODE_SLOT1(REF) ((REF >> 8) & 0xff)
#define DECODE_SLOT0(REF) (REF & 0xff)
#elif NR_ROWS_LOG <= 18 && NR_SLOTS <= (1 << 7)
#define ENCODE_INPUTS(row, slot0, slot1) \
((row << 14) | ((slot1 & 0x7f) << 7) | (slot0 & 0x7f))
#define DECODE_ROW(REF) (REF >> 14)
#define DECODE_SLOT1(REF) ((REF >> 7) & 0x7f)
#define DECODE_SLOT0(REF) (REF & 0x7f)
#elif NR_ROWS_LOG == 19 && NR_SLOTS <= (1 << 6)
#define ENCODE_INPUTS(row, slot0, slot1) \
((row << 13) | ((slot1 & 0x3f) << 6) | (slot0 & 0x3f)) /* 1 spare bit */
#define DECODE_ROW(REF) (REF >> 13)
#define DECODE_SLOT1(REF) ((REF >> 6) & 0x3f)
#define DECODE_SLOT0(REF) (REF & 0x3f)
#elif NR_ROWS_LOG == 20 && NR_SLOTS <= (1 << 6)
#define ENCODE_INPUTS(row, slot0, slot1) \
((row << 12) | ((slot1 & 0x3f) << 6) | (slot0 & 0x3f))
#define DECODE_ROW(REF) (REF >> 12)
#define DECODE_SLOT1(REF) ((REF >> 6) & 0x3f)
#define DECODE_SLOT0(REF) (REF & 0x3f)
#else
#error "unsupported NR_ROWS_LOG"
#endif
#define NEXT_PRIME_NO(n) \
(((n) <= 2) ? 2 : \
((n) <= 3) ? 3 : \
((n) <= 5) ? 5 : \
((n) <= 7) ? 7 : \
((n) <= 11) ? 11 : \
((n) <= 13) ? 13 : \
((n) <= 17) ? 17 : \
((n) <= 19) ? 19 : \
((n) <= 23) ? 23 : \
((n) <= 29) ? 29 : \
((n) <= 31) ? 31 : \
((n) <= 37) ? 37 : \
((n) <= 41) ? 41 : \
((n) <= 43) ? 43 : \
((n) <= 47) ? 47 : \
((n) <= 53) ? 53 : \
((n) <= 59) ? 59 : \
((n) <= 61) ? 61 : \
((n) <= 67) ? 67 : \
((n) <= 71) ? 71 : \
((n) <= 73) ? 73 : \
((n) <= 79) ? 79 : \
((n) <= 83) ? 83 : \
((n) <= 89) ? 89 : \
((n) <= 97) ? 97 : \
((n) <= 101) ? 101 : \
((n) <= 103) ? 103 : \
((n) <= 107) ? 107 : \
((n) <= 109) ? 109 : \
((n) <= 113) ? 113 : \
((n) <= 127) ? 127 : \
((n) <= 131) ? 131 : \
((n) <= 137) ? 137 : \
((n) <= 139) ? 139 : \
((n) <= 149) ? 149 : \
((n) <= 151) ? 151 : \
((n) <= 157) ? 157 : \
((n) <= 163) ? 163 : \
((n) <= 167) ? 167 : \
((n) <= 173) ? 173 : \
((n) <= 179) ? 179 : \
((n) <= 181) ? 181 : \
((n) <= 191) ? 191 : \
((n) <= 193) ? 193 : \
((n) <= 197) ? 197 : \
((n) <= 199) ? 199 : \
((n) <= 211) ? 211 : \
((n) <= 223) ? 223 : \
((n) <= 227) ? 227 : \
((n) <= 229) ? 229 : \
((n) <= 233) ? 233 : \
((n) <= 239) ? 239 : \
((n) <= 241) ? 241 : \
((n) <= 251) ? 251 : \
((n) <= 257) ? 257 : \
((n) <= 263) ? 263 : \
((n) <= 269) ? 269 : \
((n) <= 271) ? 271 : \
((n) <= 277) ? 277 : \
((n) <= 281) ? 281 : \
((n) <= 283) ? 283 : \
((n) <= 293) ? 293 : \
((n) <= 307) ? 307 : \
((n) <= 311) ? 311 : \
((n) <= 313) ? 313 : \
((n) <= 317) ? 317 : \
((n) <= 331) ? 331 : \
((n) <= 337) ? 337 : \
((n) <= 347) ? 347 : \
((n) <= 349) ? 349 : \
((n) <= 353) ? 353 : \
((n) <= 359) ? 359 : \
((n) <= 367) ? 367 : \
((n) <= 373) ? 373 : \
((n) <= 379) ? 379 : \
((n) <= 383) ? 383 : \
((n) <= 389) ? 389 : \
((n) <= 397) ? 397 : \
((n) <= 401) ? 401 : \
((n) <= 409) ? 409 : \
((n) <= 419) ? 419 : \
((n) <= 421) ? 421 : \
((n) <= 431) ? 431 : \
((n) <= 433) ? 433 : \
((n) <= 439) ? 439 : \
((n) <= 443) ? 443 : \
((n) <= 449) ? 449 : \
((n) <= 457) ? 457 : \
((n) <= 461) ? 461 : \
((n) <= 463) ? 463 : \
((n) <= 467) ? 467 : \
((n) <= 479) ? 479 : \
((n) <= 487) ? 487 : \
((n) <= 491) ? 491 : \
((n) <= 499) ? 499 : \
((n) <= 503) ? 503 : \
((n) <= 509) ? 509 : \
((n) <= 521) ? 521 : \
((n) <= 523) ? 523 : \
((n) <= 541) ? 541 : \
((n) <= 547) ? 547 : \
((n) <= 557) ? 557 : \
((n) <= 563) ? 563 : \
((n) <= 569) ? 569 : \
((n) <= 571) ? 571 : \
((n) <= 577) ? 577 : \
((n) <= 587) ? 587 : \
((n) <= 593) ? 593 : \
((n) <= 599) ? 599 : \
((n) <= 601) ? 601 : \
((n) <= 607) ? 607 : \
((n) <= 613) ? 613 : \
((n) <= 617) ? 617 : \
((n) <= 619) ? 619 : \
((n) <= 631) ? 631 : \
((n) <= 641) ? 641 : \
((n) <= 643) ? 643 : \
((n) <= 647) ? 647 : \
((n) <= 653) ? 653 : \
((n) <= 659) ? 659 : \
((n) <= 661) ? 661 : \
((n) <= 673) ? 673 : \
((n) <= 677) ? 677 : \
((n) <= 683) ? 683 : \
((n) <= 691) ? 691 : \
((n) <= 701) ? 701 : \
((n) <= 709) ? 709 : \
((n) <= 719) ? 719 : \
((n) <= 727) ? 727 : \
((n) <= 733) ? 733 : \
((n) <= 739) ? 739 : \
((n) <= 743) ? 743 : \
((n) <= 751) ? 751 : \
((n) <= 757) ? 757 : \
((n) <= 761) ? 761 : \
((n) <= 769) ? 769 : \
((n) <= 773) ? 773 : \
((n) <= 787) ? 787 : \
((n) <= 797) ? 797 : \
((n) <= 809) ? 809 : \
((n) <= 811) ? 811 : \
((n) <= 821) ? 821 : \
((n) <= 823) ? 823 : \
((n) <= 827) ? 827 : \
((n) <= 829) ? 829 : \
((n) <= 839) ? 839 : \
((n) <= 853) ? 853 : \
((n) <= 857) ? 857 : \
((n) <= 859) ? 859 : \
((n) <= 863) ? 863 : \
((n) <= 877) ? 877 : \
((n) <= 881) ? 881 : \
((n) <= 883) ? 883 : \
((n) <= 887) ? 887 : \
((n) <= 907) ? 907 : \
((n) <= 911) ? 911 : \
((n) <= 919) ? 919 : \
((n) <= 929) ? 929 : \
((n) <= 937) ? 937 : \
((n) <= 941) ? 941 : \
((n) <= 947) ? 947 : \
((n) <= 953) ? 953 : \
((n) <= 967) ? 967 : \
((n) <= 971) ? 971 : \
((n) <= 977) ? 977 : \
((n) <= 983) ? 983 : \
((n) <= 991) ? 991 : \
((n) <= 997) ? 997 : \
((n) <= 1009) ? 1009 : \
(n))
#define NEXT_POWER_OF_TWO(n) \
(((n) <= 2) ? 2 : \
((n) <= 4) ? 4 : \
((n) <= 8) ? 8 : \
((n) <= 16) ? 16 : \
((n) <= 32) ? 32 : \
((n) <= 64) ? 64 : \
((n) <= 128) ? 128 : \
((n) <= 256) ? 256 : \
((n) <= 512) ? 512 : \
((n) <= 1024) ? 1024 : \
((n) <= 2048) ? 2048 : \
((n) <= 4096) ? 4096 : \
((n) <= 8192) ? 8192 : \
((n) <= 16384) ? 16384 : \
((n) <= 32768) ? 32768 : \
(n))
#if NR_SLOTS < 255
#define SLOT_INDEX_TYPE uchar
#elif NR_SLOTS < 65535
#define SLOT_INDEX_TYPE ushort
#else
#error "Unsupported NR_SLOTS"
#endif
-894
View File
@@ -1,894 +0,0 @@
// Gateless Gate, a Zcash miner
// Copyright 2016 zawawa @ bitcointalk.org
//
// The initial version of this software was based on:
// SILENTARMY v5
// The MIT License (MIT) Copyright (c) 2016 Marc Bevand, Genoil, eXtremal
//
// This program is free software : you can redistribute it and / or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "equihash-param.h"
#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable
#ifdef AMD
#pragma OPENCL EXTENSION cl_amd_vec3 : enable
#endif
/////////////////
// HASH TABLES //
/////////////////
/*
** With the new hash tables, each slot has this layout (length in bytes in parens):
**
** round 0, table 0: i(4) pad(0) Xi(24) pad(4)
** round 1, table 1: i(4) pad(3) Xi(20) pad(5)
** round 2, table 2: i(4) pad(0) Xi(19) pad(9)
** round 3, table 3: i(4) pad(3) Xi(15) pad(10)
** round 4, table 4: i(4) pad(0) Xi(14) pad(14)
** round 5, table 5: i(4) pad(3) Xi(10) pad(15)
** round 6, table 6: i(4) pad(0) Xi( 9) pad(19)
** round 7, table 7: i(4) pad(3) Xi( 5) pad(20)
** round 8, table 8: i(4) pad(0) Xi( 4) pad(24)
*/
typedef union {
struct {
uint xi[7];
uint padding;
} slot;
uint8 ui8;
uint4 ui4[2];
uint2 ui2[4];
uint ui[8];
#ifdef AMD
ulong3 ul3;
uint3 ui3[2];
#endif
} slot_t;
typedef __global slot_t *global_pointer_to_slot_t;
#define UINTS_IN_XI(round) (((round) == 0) ? 6 : \
((round) == 1) ? 6 : \
((round) == 2) ? 5 : \
((round) == 3) ? 5 : \
((round) == 4) ? 4 : \
((round) == 5) ? 4 : \
((round) == 6) ? 3 : \
((round) == 7) ? 2 : \
1)
/*
** OBSOLETE
** If xi0,xi1,xi2,xi3 are stored consecutively in little endian then they
** represent (hex notation, group of 5 hex digits are a group of PREFIX bits):
** aa aa ab bb bb cc cc cd dd... [round 0]
** --------------------
** ...ab bb bb cc cc cd dd... [odd round]
** --------------
** ...cc cc cd dd... [next even round]
** -----
** Bytes underlined are going to be stored in the slot. Preceding bytes
** (and possibly part of the underlined bytes, depending on NR_ROWS_LOG) are
** used to compute the row number.
**
** Round 0: xi0,xi1,xi2,xi3 is a 25-byte Xi (xi3: only the low byte matter)
** Round 1: xi0,xi1,xi2 is a 23-byte Xi (incl. the colliding PREFIX nibble)
** TODO: update lines below with padding nibbles
** Round 2: xi0,xi1,xi2 is a 20-byte Xi (xi2: only the low 4 bytes matter)
** Round 3: xi0,xi1,xi2 is a 17.5-byte Xi (xi2: only the low 1.5 bytes matter)
** Round 4: xi0,xi1 is a 15-byte Xi (xi1: only the low 7 bytes matter)
** Round 5: xi0,xi1 is a 12.5-byte Xi (xi1: only the low 4.5 bytes matter)
** Round 6: xi0,xi1 is a 10-byte Xi (xi1: only the low 2 bytes matter)
** Round 7: xi0 is a 7.5-byte Xi (xi0: only the low 7.5 bytes matter)
** Round 8: xi0 is a 5-byte Xi (xi0: only the low 5 bytes matter)
**
** Return 0 if successfully stored, or 1 if the row overflowed.
*/
__global char *get_slot_ptr(__global char *ht, uint round, uint row, uint slot)
{
return ht + (row * NR_SLOTS + slot) * ADJUSTED_SLOT_LEN(round);
}
__global uint *get_xi_ptr(__global char *ht, uint round, uint row, uint slot)
{
return (__global uint *)get_slot_ptr(ht, round, row, slot);
}
__global uint *get_ref_ptr(__global char *ht, uint round, uint row, uint slot)
{
return get_xi_ptr(ht, round, row, slot) + UINTS_IN_XI(round);
}
void get_row_counters_index(uint *rowIdx, uint *rowOffset, uint row)
{
if (ROWS_PER_UINT == 3) {
uint r = (0x55555555 * row + (row >> 1) - (row >> 3)) >> 30;
*rowIdx = (row - r) * 0xAAAAAAAB;
*rowOffset = BITS_PER_ROW * r;
} else if (ROWS_PER_UINT == 6) {
uint r = (0x55555555 * row + (row >> 1) - (row >> 3)) >> 29;
*rowIdx = (row - r) * 0xAAAAAAAB * 2;
*rowOffset = BITS_PER_ROW * r;
} else {
*rowIdx = row / ROWS_PER_UINT;
*rowOffset = BITS_PER_ROW * (row % ROWS_PER_UINT);
}
}
uint get_row(uint round, uint xi0)
{
uint row = 0;
if (NR_ROWS_LOG == 12) {
if (!(round % 2))
row = (xi0 & 0xfff);
else
row = ((xi0 & 0x0f0f00) >> 8) | ((xi0 & 0xf0000000) >> 24);
} else if (NR_ROWS_LOG == 13) {
if (!(round % 2))
row = (xi0 & 0x1fff);
else
row = ((xi0 & 0x1f0f00) >> 8) | ((xi0 & 0xf0000000) >> 24);
} else if (NR_ROWS_LOG == 14) {
if (!(round % 2))
row = (xi0 & 0x3fff);
else
row = ((xi0 & 0x3f0f00) >> 8) | ((xi0 & 0xf0000000) >> 24);
} else if (NR_ROWS_LOG == 15) {
if (!(round % 2))
row = (xi0 & 0x7fff);
else
row = ((xi0 & 0x7f0f00) >> 8) | ((xi0 & 0xf0000000) >> 24);
} else if (NR_ROWS_LOG == 16) {
if (!(round % 2))
row = (xi0 & 0xffff);
else
row = ((xi0 & 0xff0f00) >> 8) | ((xi0 & 0xf0000000) >> 24);
}
return row;
}
uint get_nr_slots(__global uint *row_counters, uint row_index)
{
uint rowIdx, rowOffset, nr_slots;
get_row_counters_index(&rowIdx, &rowOffset, row_index);
nr_slots = (row_counters[rowIdx] >> rowOffset) & ROW_MASK;
nr_slots = min(nr_slots, (uint)NR_SLOTS); // handle possible overflow in last round
return nr_slots;
}
uint inc_row_counter(__global uint *rowCounters, uint row)
{
uint rowIdx, rowOffset;
get_row_counters_index(&rowIdx, &rowOffset, row);
uint nr_slots = atomic_add(rowCounters + rowIdx, 1U << rowOffset);
nr_slots = (nr_slots >> rowOffset) & ROW_MASK;
if (nr_slots >= NR_SLOTS) {
// avoid overflows
atomic_sub(rowCounters + rowIdx, 1 << rowOffset);
}
return nr_slots;
}
/*
** Reset counters in a hash table.
*/
__kernel
void kernel_init_ht(__global char *ht, __global uint *rowCounters, __global sols_t *sols, __global potential_sols_t *potential_sols)
{
if (!get_global_id(0))
sols->nr = sols->likely_invalids = potential_sols->nr = 0;
if (get_global_id(0) < RC_SIZE / 4)
rowCounters[get_global_id(0)] = 0;
}
/////////////
// ROUND 0 //
/////////////
__constant ulong blake_iv[] =
{
0x6a09e667f3bcc908, 0xbb67ae8584caa73b,
0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1,
0x510e527fade682d1, 0x9b05688c2b3e6c1f,
0x1f83d9abfb41bd6b, 0x5be0cd19137e2179,
};
#define mix(va, vb, vc, vd, x, y) \
va = (va + vb + (x)); \
vd = rotate((vd ^ va), (ulong)64 - 32); \
vc = (vc + vd); \
vb = rotate((vb ^ vc), (ulong)64 - 24); \
va = (va + vb + (y)); \
vd = rotate((vd ^ va), (ulong)64 - 16); \
vc = (vc + vd); \
vb = rotate((vb ^ vc), (ulong)64 - 63);
/*
** Execute round 0 (blake).
**
** Note: making the work group size less than or equal to the wavefront size
** allows the OpenCL compiler to remove the barrier() calls, see "2.2 Local
** Memory (LDS) Optimization 2-10" in:
** http://developer.amd.com/tools-and-sdks/opencl-zone/amd-accelerated-parallel-processing-app-sdk/opencl-optimization-guide/
*/
#if ZCASH_HASH_LEN != 50
#error "unsupported ZCASH_HASH_LEN"
#endif
__kernel __attribute__((reqd_work_group_size(LOCAL_WORK_SIZE_ROUND0, 1, 1)))
void kernel_round0(__constant ulong *blake_state, __global char *ht,
__global uint *rowCounters, __global uint *debug)
{
uint tid = get_global_id(0);
#if defined(AMD) && !defined(AMD_LEGACY)
volatile ulong v[16];
uint xi0, xi1, xi2, xi3, xi4, xi5, xi6;
slot_t slot;
#else
ulong v[16];
uint xi0, xi1, xi2, xi3, xi4, xi5, xi6;
slot_t slot;
#endif
ulong h[7];
uint inputs_per_thread = (NR_INPUTS + get_global_size(0) - 1) / get_global_size(0);
uint dropped = 0;
for (uint chunk = 0; chunk < inputs_per_thread; ++chunk) {
uint input = tid + get_global_size(0) * chunk;
if (input < NR_INPUTS) {
// shift "i" to occupy the high 32 bits of the second ulong word in the
// message block
ulong word1 = (ulong)input << 32;
// init vector v
v[0] = blake_state[0];
v[1] = blake_state[1];
v[2] = blake_state[2];
v[3] = blake_state[3];
v[4] = blake_state[4];
v[5] = blake_state[5];
v[6] = blake_state[6];
v[7] = blake_state[7];
v[8] = blake_iv[0];
v[9] = blake_iv[1];
v[10] = blake_iv[2];
v[11] = blake_iv[3];
v[12] = blake_iv[4];
v[13] = blake_iv[5];
v[14] = blake_iv[6];
v[15] = blake_iv[7];
// mix in length of data
v[12] ^= ZCASH_BLOCK_HEADER_LEN + 4 /* length of "i" */;
// last block
v[14] ^= (ulong)-1;
#if defined(AMD) && !defined(AMD_LEGACY)
#pragma unroll 1
for (uint blake_round = 1; blake_round <= 9; ++blake_round) {
#else
#pragma unroll 9
for (uint blake_round = 1; blake_round <= 9; ++blake_round) {
#endif
mix(v[0], v[4], v[8], v[12], 0, (blake_round == 1) ? word1 : 0);
mix(v[1], v[5], v[9], v[13], (blake_round == 7) ? word1 : 0, (blake_round == 4) ? word1 : 0);
mix(v[2], v[6], v[10], v[14], 0, (blake_round == 8) ? word1 : 0);
mix(v[3], v[7], v[11], v[15], 0, 0);
mix(v[0], v[5], v[10], v[15], (blake_round == 2) ? word1 : 0, (blake_round == 5) ? word1 : 0);
mix(v[1], v[6], v[11], v[12], 0, 0);
mix(v[2], v[7], v[8], v[13], (blake_round == 9) ? word1 : 0, (blake_round == 3) ? word1 : 0);
mix(v[3], v[4], v[9], v[14], (blake_round == 6) ? word1 : 0, 0);
}
// round 10
mix(v[0], v[4], v[8], v[12], 0, 0);
mix(v[1], v[5], v[9], v[13], 0, 0);
mix(v[2], v[6], v[10], v[14], 0, 0);
mix(v[3], v[7], v[11], v[15], word1, 0);
mix(v[0], v[5], v[10], v[15], 0, 0);
mix(v[1], v[6], v[11], v[12], 0, 0);
mix(v[2], v[7], v[8], v[13], 0, 0);
mix(v[3], v[4], v[9], v[14], 0, 0);
// round 11
mix(v[0], v[4], v[8], v[12], 0, word1);
mix(v[1], v[5], v[9], v[13], 0, 0);
mix(v[2], v[6], v[10], v[14], 0, 0);
mix(v[3], v[7], v[11], v[15], 0, 0);
mix(v[0], v[5], v[10], v[15], 0, 0);
mix(v[1], v[6], v[11], v[12], 0, 0);
mix(v[2], v[7], v[8], v[13], 0, 0);
mix(v[3], v[4], v[9], v[14], 0, 0);
// round 12
mix(v[0], v[4], v[8], v[12], 0, 0);
mix(v[1], v[5], v[9], v[13], 0, 0);
mix(v[2], v[6], v[10], v[14], 0, 0);
mix(v[3], v[7], v[11], v[15], 0, 0);
mix(v[0], v[5], v[10], v[15], word1, 0);
mix(v[1], v[6], v[11], v[12], 0, 0);
mix(v[2], v[7], v[8], v[13], 0, 0);
mix(v[3], v[4], v[9], v[14], 0, 0);
// compress v into the blake state; this produces the 50-byte hash
// (two Xi values)
h[0] = blake_state[0] ^ v[0] ^ v[8];
h[1] = blake_state[1] ^ v[1] ^ v[9];
h[2] = blake_state[2] ^ v[2] ^ v[10];
h[3] = blake_state[3] ^ v[3] ^ v[11];
h[4] = blake_state[4] ^ v[4] ^ v[12];
h[5] = blake_state[5] ^ v[5] ^ v[13];
h[6] = (blake_state[6] ^ v[6] ^ v[14]) & 0xffff;
}
if (input < NR_INPUTS) {
// store the two Xi values in the hash table
#pragma unroll 1
for (uint index = 0; index < 2; ++index) {
if (!index) {
xi0 = h[0] & 0xffffffff; xi1 = h[0] >> 32;
xi2 = h[1] & 0xffffffff; xi3 = h[1] >> 32;
xi4 = h[2] & 0xffffffff; xi5 = h[2] >> 32;
xi6 = h[3] & 0xffffffff;
} else {
xi0 = ((h[3] >> 8) | (h[4] << (64 - 8))) & 0xffffffff; xi1 = ((h[3] >> 8) | (h[4] << (64 - 8))) >> 32;
xi2 = ((h[4] >> 8) | (h[5] << (64 - 8))) & 0xffffffff; xi3 = ((h[4] >> 8) | (h[5] << (64 - 8))) >> 32;
xi4 = ((h[5] >> 8) | (h[6] << (64 - 8))) & 0xffffffff; xi5 = ((h[5] >> 8) | (h[6] << (64 - 8))) >> 32;
xi6 = (h[6] >> 8) & 0xffffffff;
}
uint row = get_row(0, xi0);
uint nr_slots = inc_row_counter(rowCounters, row);
if (nr_slots >= NR_SLOTS) {
++dropped;
} else {
slot.slot.xi[0] = ((xi1 << 24) | (xi0 >> 8));
slot.slot.xi[1] = ((xi2 << 24) | (xi1 >> 8));
slot.slot.xi[2] = ((xi3 << 24) | (xi2 >> 8));
slot.slot.xi[3] = ((xi4 << 24) | (xi3 >> 8));
slot.slot.xi[4] = ((xi5 << 24) | (xi4 >> 8));
slot.slot.xi[5] = ((xi6 << 24) | (xi5 >> 8));
slot.slot.xi[UINTS_IN_XI(0)] = input * 2 + index;
__global char *p = get_slot_ptr(ht, 0, row, nr_slots);
*(__global uint8 *)p = slot.ui8;
}
}
}
}
#ifdef ENABLE_DEBUG
debug[tid * 2] = 0;
debug[tid * 2 + 1] = dropped;
#endif
}
/*
** XOR a pair of Xi values computed at "round - 1" and store the result in the
** hash table being built for "round". Note that when building the table for
** even rounds we need to skip 1 padding byte present in the "round - 1" table
** (the "0xAB" byte mentioned in the description at the top of this file.) But
** also note we can't load data directly past this byte because this would
** cause an unaligned memory access which is undefined per the OpenCL spec.
**
** Return 0 if successfully stored, or 1 if the row overflowed.
*/
uint xor_and_store(uint round, __global char *ht_src, __global char *ht_dst, uint row,
uint slot_a, uint slot_b, __local uint *ai, __local uint *bi,
__global uint *rowCounters) {
uint ret = 0;
uint xi0, xi1, xi2, xi3, xi4, xi5;
#if NR_ROWS_LOG < 8 && NR_ROWS_LOG > 20
#error "unsupported NR_ROWS_LOG"
#endif
slot_t slot;
__global slot_t *p = 0;
if (slot_a < NR_SLOTS && slot_b < NR_SLOTS) {
xi0 = *ai;
xi1 = *(ai += NR_SLOTS);
if (round <= 7) xi2 = *(ai += NR_SLOTS);
if (round <= 6) xi3 = *(ai += NR_SLOTS);
if (round <= 4) xi4 = *(ai += NR_SLOTS);
if (round <= 2) xi5 = *(ai += NR_SLOTS);
xi0 ^= *bi;
xi1 ^= *(bi += NR_SLOTS);
if (round <= 7) xi2 ^= *(bi += NR_SLOTS);
if (round <= 6) xi3 ^= *(bi += NR_SLOTS);
if (round <= 4) xi4 ^= *(bi += NR_SLOTS);
if (round <= 2) xi5 ^= *(bi += NR_SLOTS);
if (!(round & 0x1)) {
// skip padding bytes
xi0 = (xi0 >> 24) | (xi1 << (32 - 24));
slot.slot.xi[0] = xi1;
slot.slot.xi[1] = xi2;
slot.slot.xi[2] = xi3;
slot.slot.xi[3] = xi4;
slot.slot.xi[4] = xi5;
} else {
slot.slot.xi[0] = ((xi1 << 24) | (xi0 >> 8));
if (round <= 7) slot.slot.xi[1] = ((xi2 << 24) | (xi1 >> 8));
if (round <= 6) slot.slot.xi[2] = ((xi3 << 24) | (xi2 >> 8));
if (round <= 5) slot.slot.xi[3] = ((xi4 << 24) | (xi3 >> 8));
if (round <= 3) slot.slot.xi[4] = ((xi5 << 24) | (xi4 >> 8));
if (round <= 1) slot.slot.xi[5] = ((xi5 >> 8));
}
slot.slot.xi[UINTS_IN_XI(round)] = ENCODE_INPUTS(row, slot_a, slot_b);
// invalid solutions (which start happenning in round 5) have duplicate
// inputs and xor to zero, so discard them
if (xi0 || xi1) {
uint new_row = get_row(round, xi0);
uint new_slot_index = inc_row_counter(rowCounters, new_row);
if (new_slot_index >= NR_SLOTS) {
ret = 1;
} else {
p = (__global slot_t *)get_slot_ptr(ht_dst, round, new_row, new_slot_index);
}
}
}
if (p) {
#ifdef OPTIM_8BYTE_WRITES
if (round >= 8)
*(__global uint2 *)p = slot.ui2[0];
else
#endif
#ifdef OPTIM_12BYTE_WRITES
if (round >= 7)
*(__global uint3 *)p = slot.ui3[0];
else
#endif
#ifdef OPTIM_16BYTE_WRITES
if (round >= 6)
*(__global uint4 *)p = slot.ui4[0];
else
#endif
#ifdef OPTIM_24BYTE_WRITES
if (round >= 2)
*(__global ulong3 *)p = slot.ul3;
else
#endif
*(__global uint8 *)p = slot.ui8;
}
return ret;
}
uint parallel_xor_and_store(uint round, __global char *ht_src, __global char *ht_dst, uint row,
uint slot_a, uint slot_b, __local uint *ai, __local uint *bi,
__global uint *rowCounters,
__local SLOT_INDEX_TYPE *new_slot_indexes) {
uint ret = 0;
uint xi0, xi1, xi2, xi3, xi4, xi5;
uint write_index = get_local_id(0) / THREADS_PER_WRITE(round);
uint write_thread_index = get_local_id(0) % THREADS_PER_WRITE(round);
//uint write_index = get_local_id(0) % (get_local_size(0) / THREADS_PER_WRITE(round));
//uint write_thread_index = get_local_id(0) / (get_local_size(0) / THREADS_PER_WRITE(round));
#if NR_ROWS_LOG < 8 && NR_ROWS_LOG > 20
#error "unsupported NR_ROWS_LOG"
#endif
slot_t slot;
uint new_slot_index;
uint new_row;
if (!write_thread_index)
new_slot_indexes[write_index] = NR_SLOTS;
barrier(CLK_LOCAL_MEM_FENCE);
if (slot_a < NR_SLOTS && slot_b < NR_SLOTS) {
xi0 = *ai;
xi1 = *(ai += NR_SLOTS);
if (round <= 7) xi2 = *(ai += NR_SLOTS);
if (round <= 6) xi3 = *(ai += NR_SLOTS);
if (round <= 4) xi4 = *(ai += NR_SLOTS);
if (round <= 2) xi5 = *(ai += NR_SLOTS);
xi0 ^= *bi;
xi1 ^= *(bi += NR_SLOTS);
if (round <= 7) xi2 ^= *(bi += NR_SLOTS);
if (round <= 6) xi3 ^= *(bi += NR_SLOTS);
if (round <= 4) xi4 ^= *(bi += NR_SLOTS);
if (round <= 2) xi5 ^= *(bi += NR_SLOTS);
if (!(round & 0x1)) {
// skip padding bytes
xi0 = (xi0 >> 24) | (xi1 << (32 - 24));
slot.slot.xi[0] = xi1;
slot.slot.xi[1] = xi2;
slot.slot.xi[2] = xi3;
slot.slot.xi[3] = xi4;
slot.slot.xi[4] = xi5;
} else {
slot.slot.xi[0] = ((xi1 << 24) | (xi0 >> 8));
if (round <= 7) slot.slot.xi[1] = ((xi2 << 24) | (xi1 >> 8));
if (round <= 6) slot.slot.xi[2] = ((xi3 << 24) | (xi2 >> 8));
if (round <= 5) slot.slot.xi[3] = ((xi4 << 24) | (xi3 >> 8));
if (round <= 3) slot.slot.xi[4] = ((xi5 << 24) | (xi4 >> 8));
if (round <= 1) slot.slot.xi[5] = ((xi5 >> 8));
}
slot.slot.xi[UINTS_IN_XI(round)] = ENCODE_INPUTS(row, slot_a, slot_b);
new_row = get_row(round, xi0);
// invalid solutions (which start happenning in round 5) have duplicate
// inputs and xor to zero, so discard them
if ((xi0 || xi1) && !write_thread_index) {
new_slot_indexes[write_index] = inc_row_counter(rowCounters, new_row);
#ifdef ENABLE_DEBUG
if (new_slot_index >= NR_SLOTS)
ret = 1;
#endif
}
}
barrier(CLK_LOCAL_MEM_FENCE);
if (new_slot_indexes[write_index] < NR_SLOTS) {
__global slot_t *p = (__global slot_t *)get_slot_ptr(ht_dst, round, new_row, new_slot_indexes[write_index]);
*(((__global uint4 *)p) + write_thread_index) = slot.ui4[write_thread_index];
}
//barrier(CLK_LOCAL_MEM_FENCE);
return ret;
}
/*
** Execute one Equihash round. Read from ht_src, XOR colliding pairs of Xi,
** store them in ht_dst. Each work group processes only one row at a time.
*/
void equihash_round(uint round,
__global char *ht_src,
__global char *ht_dst,
__global uint *debug,
__local uint *slot_cache,
__local SLOT_INDEX_TYPE *collision_array_a,
__local SLOT_INDEX_TYPE *collision_array_b,
__local uint *nr_collisions,
__global uint *rowCountersSrc,
__global uint *rowCountersDst,
__local uint *bin_first_slots,
__local SLOT_INDEX_TYPE *bin_next_slots,
__local SLOT_INDEX_TYPE *new_slot_indexes)
{
uint i, j;
#ifdef ENABLE_DEBUG
uint dropped_coll = 0;
uint dropped_stor = 0;
#endif
// the mask is also computed to read data from the previous round
#define BIN_MASK(round) ((((round) + 1) % 2) ? 0xf000 : 0xf0000)
#define BIN_MASK_OFFSET(round) ((((round) + 1) % 2) ? 3 * 4 : 4 * 4)
#define BIN_MASK2(round) ((NR_ROWS_LOG == 12) ? ((((round) + 1) % 2) ? 0x00f0 : 0xf000) : \
(NR_ROWS_LOG == 13) ? ((((round) + 1) % 2) ? 0x00e0 : 0xe000) : \
(NR_ROWS_LOG == 14) ? ((((round) + 1) % 2) ? 0x00c0 : 0xc000) : \
(NR_ROWS_LOG == 15) ? ((((round) + 1) % 2) ? 0x0080 : 0x8000) : \
0)
#define BIN_MASK2_OFFSET(round) ((NR_ROWS_LOG == 12) ? ((((round) + 1) % 2) ? 0 : 8) : \
(NR_ROWS_LOG == 13) ? ((((round) + 1) % 2) ? 1 : 9) : \
(NR_ROWS_LOG == 14) ? ((((round) + 1) % 2) ? 2 : 10) : \
(NR_ROWS_LOG == 15) ? ((((round) + 1) % 2) ? 3 : 11) : \
0)
#define NR_BINS_LOG (20 - NR_ROWS_LOG)
#define NR_BINS (1 << NR_BINS_LOG)
uint nr_slots = 0;
uint assigned_row_index = get_group_id(0);
if (assigned_row_index >= NR_ROWS)
return;
for (i = get_local_id(0); i < NR_BINS; i += get_local_size(0))
bin_first_slots[i] = NR_SLOTS;
for (i = get_local_id(0); i < NR_SLOTS; i += get_local_size(0))
bin_next_slots[i] = NR_SLOTS;
if (get_local_id(0) == 0)
*nr_collisions = nr_slots = get_nr_slots(rowCountersSrc, assigned_row_index);
barrier(CLK_LOCAL_MEM_FENCE);
if (get_local_id(0))
nr_slots = *nr_collisions;
barrier(CLK_LOCAL_MEM_FENCE);
for (uint phase = 0; phase < 1; ++phase) {
// Perform a radix sort as slots get loaded into LDS.
// Make sure all the work items in the work group enter the loop.
for (i = get_local_id(0); i < nr_slots; i += get_local_size(0)) {
uint slot_index = i;
uint slot_cache_index = i;
#ifdef NVIDIA
uint2 slot_data0, slot_data1, slot_data2;
if (UINTS_IN_XI(round - 1) >= 1) slot_data0 = *((__global uint2 *)get_slot_ptr(ht_src, round - 1, assigned_row_index, slot_cache_index) + 0);
if (UINTS_IN_XI(round - 1) >= 3) slot_data1 = *((__global uint2 *)get_slot_ptr(ht_src, round - 1, assigned_row_index, slot_cache_index) + 1);
if (UINTS_IN_XI(round - 1) >= 5) slot_data2 = *((__global uint2 *)get_slot_ptr(ht_src, round - 1, assigned_row_index, slot_cache_index) + 2);
if (UINTS_IN_XI(round - 1) >= 1) slot_cache[0 * NR_SLOTS + slot_cache_index] = slot_data0.s0;
if (UINTS_IN_XI(round - 1) >= 2) slot_cache[1 * NR_SLOTS + slot_cache_index] = slot_data0.s1;
if (UINTS_IN_XI(round - 1) >= 3) slot_cache[2 * NR_SLOTS + slot_cache_index] = slot_data1.s0;
if (UINTS_IN_XI(round - 1) >= 4) slot_cache[3 * NR_SLOTS + slot_cache_index] = slot_data1.s1;
if (UINTS_IN_XI(round - 1) >= 5) slot_cache[4 * NR_SLOTS + slot_cache_index] = slot_data2.s0;
if (UINTS_IN_XI(round - 1) >= 6) slot_cache[5 * NR_SLOTS + slot_cache_index] = slot_data2.s1;
uint xi0 = slot_data0.s0;
#else
for (j = 0; j < UINTS_IN_XI(round - 1); ++j)
slot_cache[j * NR_SLOTS + slot_cache_index] = *((__global uint *)get_xi_ptr(ht_src, round - 1, assigned_row_index, slot_index) + j);
uint xi0 = slot_cache[0 * NR_SLOTS + slot_cache_index];
#endif
uint bin_to_use =
((xi0 & BIN_MASK(round - 1)) >> BIN_MASK_OFFSET(round - 1))
| ((xi0 & BIN_MASK2(round - 1)) >> BIN_MASK2_OFFSET(round - 1));
bin_next_slots[i] = atomic_xchg(&bin_first_slots[bin_to_use], i);
}
if (!get_local_id(0))
*nr_collisions = 0;
uint max_slot_a_index = NR_SLOTS + (get_local_size(0) - NR_SLOTS % get_local_size(0)) - 1;
barrier(CLK_LOCAL_MEM_FENCE);
for (uint slot_a_index = get_local_id(0); slot_a_index <= max_slot_a_index; slot_a_index += get_local_size(0)) {
uint slot_b_index = (slot_a_index < NR_SLOTS) ? bin_next_slots[slot_a_index] : NR_SLOTS;
while (slot_b_index < NR_SLOTS) {
uint coll_index = atomic_inc(nr_collisions);
if (coll_index < LDS_COLL_SIZE) {
collision_array_a[coll_index] = slot_a_index;
collision_array_b[coll_index] = slot_b_index;
} else {
atomic_dec(nr_collisions);
#ifdef ENABLE_DEBUG
++dropped_coll;
#endif
}
slot_b_index = bin_next_slots[slot_b_index];
}
barrier(CLK_LOCAL_MEM_FENCE);
uint nr_collisions_copy = *nr_collisions;
//barrier(CLK_LOCAL_MEM_FENCE);
while (nr_collisions_copy > 0) {
uint collision, slot_index_a = NR_SLOTS, slot_index_b = NR_SLOTS;
__local uint *slot_cache_a, *slot_cache_b;
uint write_index = get_local_id(0) / THREADS_PER_WRITE(round);
if (write_index < nr_collisions_copy) {
slot_index_a = collision_array_a[nr_collisions_copy - 1 - write_index];
slot_index_b = collision_array_b[nr_collisions_copy - 1 - write_index];
slot_cache_a = (__local uint *)&slot_cache[slot_index_a];
slot_cache_b = (__local uint *)&slot_cache[slot_index_b];
}
//barrier(CLK_LOCAL_MEM_FENCE);
if (THREADS_PER_WRITE(round) > 1) {
#ifdef ENABLE_DEBUG
//dropped_stor +=
#endif
parallel_xor_and_store(round, ht_src, ht_dst, assigned_row_index, slot_index_a, slot_index_b, slot_cache_a, slot_cache_b, rowCountersDst, new_slot_indexes);
} else {
#ifdef ENABLE_DEBUG
dropped_stor +=
#endif
xor_and_store(round, ht_src, ht_dst, assigned_row_index, slot_index_a, slot_index_b, slot_cache_a, slot_cache_b, rowCountersDst);
}
if (!get_local_id(0))
*nr_collisions -= min(*nr_collisions, (uint)get_local_size(0) / THREADS_PER_WRITE(round));
barrier(CLK_LOCAL_MEM_FENCE);
nr_collisions_copy = *nr_collisions;
barrier(CLK_LOCAL_MEM_FENCE);
}
barrier(CLK_LOCAL_MEM_FENCE);
}
}
#ifdef ENABLE_DEBUG
debug[get_global_id(0) * 2] = dropped_coll;
debug[get_global_id(0) * 2 + 1] = dropped_stor;
#endif
}
/*
** This defines kernel_round1, kernel_round2, ..., kernel_round8.
*/
#define KERNEL_ROUND(kernel_name, N) \
__kernel __attribute__((reqd_work_group_size(LOCAL_WORK_SIZE, 1, 1))) \
void kernel_name(__global char *ht_src, __global char *ht_dst, \
__global uint *rowCountersSrc, __global uint *rowCountersDst, \
__global uint *debug) \
{ \
__local uint slot_cache[ADJUSTED_LDS_ARRAY_SIZE(UINTS_IN_XI(N - 1) * NR_SLOTS)]; \
__local SLOT_INDEX_TYPE collision_array_a[ADJUSTED_LDS_ARRAY_SIZE(LDS_COLL_SIZE)]; \
__local SLOT_INDEX_TYPE collision_array_b[ADJUSTED_LDS_ARRAY_SIZE(LDS_COLL_SIZE)]; \
__local uint nr_collisions; \
__local uint bin_first_slots[ADJUSTED_LDS_ARRAY_SIZE(NR_BINS)]; \
__local SLOT_INDEX_TYPE bin_next_slots[ADJUSTED_LDS_ARRAY_SIZE(NR_SLOTS)]; \
__local SLOT_INDEX_TYPE new_slot_indexes[ADJUSTED_LDS_ARRAY_SIZE((THREADS_PER_WRITE(N) > 1) ? LOCAL_WORK_SIZE / THREADS_PER_WRITE(N) : 0)]; \
equihash_round((N), ht_src, ht_dst, debug, slot_cache, collision_array_a, collision_array_b, \
&nr_collisions, rowCountersSrc, rowCountersDst, bin_first_slots, bin_next_slots, new_slot_indexes); \
}
KERNEL_ROUND(kernel_round1, 1)
KERNEL_ROUND(kernel_round2, 2)
KERNEL_ROUND(kernel_round3, 3)
KERNEL_ROUND(kernel_round4, 4)
KERNEL_ROUND(kernel_round5, 5)
KERNEL_ROUND(kernel_round6, 6)
KERNEL_ROUND(kernel_round7, 7)
KERNEL_ROUND(kernel_round8, 8)
void mark_potential_sol(__global potential_sols_t *potential_sols, uint ref0, uint ref1)
{
uint sol_i = atomic_inc(&potential_sols->nr);
if (sol_i >= MAX_POTENTIAL_SOLS)
return;
potential_sols->values[sol_i][0] = ref0;
potential_sols->values[sol_i][1] = ref1;
}
/*
** Scan the hash tables to find Equihash solutions.
*/
__kernel __attribute__((reqd_work_group_size(LOCAL_WORK_SIZE_POTENTIAL_SOLS, 1, 1)))
void kernel_potential_sols(
__global char *ht_src,
__global potential_sols_t *potential_sols,
__global uint *rowCountersSrc)
{
__local uint refs[ADJUSTED_LDS_ARRAY_SIZE(NR_SLOTS)];
__local uint data[ADJUSTED_LDS_ARRAY_SIZE(NR_SLOTS)];
uint nr_slots;
uint i, j;
__global char *p;
uint ref_i, ref_j;
__local uint bin_first_slots[ADJUSTED_LDS_ARRAY_SIZE(NR_BINS)];
__local SLOT_INDEX_TYPE bin_next_slots[ADJUSTED_LDS_ARRAY_SIZE(NR_SLOTS)];
if (!get_global_id(0))
potential_sols->nr = 0;
barrier(CLK_GLOBAL_MEM_FENCE);
uint assigned_row_index = (get_global_id(0) / get_local_size(0));
if (assigned_row_index >= NR_ROWS)
return;
__local uint nr_slots_shared;
for (i = get_local_id(0); i < NR_BINS; i += get_local_size(0))
bin_first_slots[i] = NR_SLOTS;
for (i = get_local_id(0); i < NR_SLOTS; i += get_local_size(0))
bin_next_slots[i] = NR_SLOTS;
if (get_local_id(0) == 0)
nr_slots_shared = nr_slots = get_nr_slots(rowCountersSrc, assigned_row_index);
barrier(CLK_LOCAL_MEM_FENCE);
if (get_local_id(0))
nr_slots = nr_slots_shared;
barrier(CLK_LOCAL_MEM_FENCE);
// in the final hash table, we are looking for a match on both the bits
// part of the previous PREFIX colliding bits, and the last PREFIX bits.
for (i = get_local_id(0); i < nr_slots; i += get_local_size(0)) {
ulong slot_first_8bytes = *(__global ulong *) get_slot_ptr(ht_src, PARAM_K - 1, assigned_row_index, i);
uint ref_i = refs[i] = slot_first_8bytes >> 32;
uint xi_first_4bytes = data[i] = slot_first_8bytes & 0xffffffff;
uint bin_to_use =
((xi_first_4bytes & BIN_MASK(PARAM_K - 1)) >> BIN_MASK_OFFSET(PARAM_K - 1))
| ((xi_first_4bytes & BIN_MASK2(PARAM_K - 1)) >> BIN_MASK2_OFFSET(PARAM_K - 1));
bin_next_slots[i] = atomic_xchg(&bin_first_slots[bin_to_use], i);
}
barrier(CLK_LOCAL_MEM_FENCE);
for (i = get_local_id(0); i < nr_slots; i += get_local_size(0)) {
uint data_i = data[i];
j = bin_next_slots[i];
while (j < NR_SLOTS) {
if (data_i == data[j]) {
mark_potential_sol(potential_sols, refs[i], refs[j]);
return;
}
j = bin_next_slots[j];
}
}
}
__kernel __attribute__((reqd_work_group_size(LOCAL_WORK_SIZE_SOLS, 1, 1)))
void kernel_sols(__global char *ht0,
__global char *ht1,
__global sols_t *sols,
__global uint *rowCountersSrc,
__global uint *rowCountersDst,
__global char *ht2,
__global char *ht3,
__global char *ht4,
__global char *ht5,
__global char *ht6,
__global char *ht7,
__global char *ht8,
__global potential_sols_t *potential_sols)
{
__local uint inputs_a[ADJUSTED_LDS_ARRAY_SIZE(1 << PARAM_K)], inputs_b[ADJUSTED_LDS_ARRAY_SIZE(1 << (PARAM_K - 1))];
__global char *htabs[] = { ht0, ht1, ht2, ht3, ht4, ht5, ht6, ht7, ht8 };
if ((get_global_id(0) / get_local_size(0)) < potential_sols->nr && (get_global_id(0) / get_local_size(0)) < MAX_POTENTIAL_SOLS) {
__local uint dup_counter;
if (get_local_id(0) == 0) {
dup_counter = 0;
inputs_a[0] = potential_sols->values[(get_global_id(0) / get_local_size(0))][0];
inputs_a[1] = potential_sols->values[(get_global_id(0) / get_local_size(0))][1];
}
barrier(CLK_LOCAL_MEM_FENCE);
for (int round = 7; round >= 0; --round) {
if (round % 2) {
for (uint i = get_local_id(0); i < (1 << (8 - round)); i += get_local_size(0)) {
inputs_b[i * 2 + 1] = *get_ref_ptr(htabs[round], round, DECODE_ROW(inputs_a[i]), DECODE_SLOT1(inputs_a[i]));
inputs_b[i * 2] = *get_ref_ptr(htabs[round], round, DECODE_ROW(inputs_a[i]), DECODE_SLOT0(inputs_a[i]));
}
} else {
for (uint i = get_local_id(0); i < (1 << (8 - round)); i += get_local_size(0)) {
inputs_a[i * 2 + 1] = *get_ref_ptr(htabs[round], round, DECODE_ROW(inputs_b[i]), DECODE_SLOT1(inputs_b[i]));
inputs_a[i * 2] = *get_ref_ptr(htabs[round], round, DECODE_ROW(inputs_b[i]), DECODE_SLOT0(inputs_b[i]));
}
}
barrier(CLK_LOCAL_MEM_FENCE);
}
//barrier(CLK_LOCAL_MEM_FENCE);
int dup_to_watch = inputs_a[256 * 2 - 1];
for (uint j = 3 + get_local_id(0); j < 256 * 2 - 2; j += get_local_size(0))
if (inputs_a[j] == dup_to_watch)
atomic_inc(&dup_counter);
barrier(CLK_LOCAL_MEM_FENCE);
// solution appears valid, copy it to sols
__local uint sol_i;
if (get_local_id(0) == 0 && !dup_counter)
sol_i = atomic_inc(&sols->nr);
barrier(CLK_LOCAL_MEM_FENCE);
if (sol_i < MAX_SOLS && !dup_counter) {
for (uint i = get_local_id(0); i < (1 << PARAM_K); i += get_local_size(0))
sols->values[sol_i][i] = inputs_a[i];
if (get_local_id(0) == 0)
sols->valid[sol_i] = 1;
}
barrier(CLK_LOCAL_MEM_FENCE);
}
}
+1 -3
View File
@@ -1505,7 +1505,7 @@ struct pool {
size_t gbt_txns;
size_t coinbase_len;
/* equihash GBT or LBRY claim hash */
/* LBRY claim hash */
unsigned char reserved[32];
/* Shared by both stratum & GBT */
@@ -1544,8 +1544,6 @@ struct work {
uint32_t XMRBlobLen;
bool is_monero;
unsigned char equihash_data[1487];
int rolls;
int drv_rolllimit; /* How much the driver can roll ntime */
+15 -117
View File
@@ -38,7 +38,6 @@
#include "algorithm/pluck.h"
#include "algorithm/yescrypt.h"
#include "algorithm/lyra2rev2.h"
#include "algorithm/equihash.h"
/* FIXME: only here for global config vars, replace with configuration.h
* or similar as soon as config is in a struct instead of littered all
@@ -793,126 +792,25 @@ _clState *initCl(unsigned int gpu, char *name, size_t nameSize, algorithm_t *alg
filename, algorithm->nfactor, algorithm->n);
/* get a kernel object handle for a kernel with the given name */
if (algorithm->type == ALGO_EQUIHASH) {
clState->kernel = clCreateKernel(clState->program, "kernel_sols", &status);
if (status != CL_SUCCESS) {
applog(LOG_ERR, "Error %d: Creating Kernel \"kernel_sols\" from program. (clCreateKernel)", status);
return NULL;
}
char *kernel_names[] = {"kernel_init_ht",
"kernel_round0", "kernel_round1", "kernel_round2",
"kernel_round3", "kernel_round4", "kernel_round5",
"kernel_round6", "kernel_round7", "kernel_round8",
"kernel_potential_sols"};
clState->n_extra_kernels = 1 + 9 + 1;
clState->extra_kernels = (cl_kernel *)malloc(sizeof(cl_kernel) * clState->n_extra_kernels);
for (int i = 0; i < clState->n_extra_kernels; i++) {
clState->extra_kernels[i] = clCreateKernel(clState->program, kernel_names[i], &status);
if (status != CL_SUCCESS) {
applog(LOG_ERR, "Error %d: Creating Kernel \"%s\" from program. (clCreateKernel)", status, kernel_names[i]);
return NULL;
}
}
char buffer[32];
clState->CLbuffer0 = clCreateBuffer(clState->context, CL_MEM_READ_WRITE, sizeof(potential_sols_t), NULL, &status);
snprintf(buffer, sizeof(buffer), "CLbuffer0");
if (status != CL_SUCCESS)
goto out;
clState->buffer1 = NULL;
for (int i = 0; i < 9; i++) {
snprintf(buffer, sizeof(buffer), "index_buf[%d]", i);
clState->index_buf[i] = clCreateBuffer(clState->context, CL_MEM_READ_WRITE, HT_SIZE, NULL, &status);
if (status != CL_SUCCESS)
goto out;
}
clState->buffer2 = clCreateBuffer(clState->context, CL_MEM_READ_WRITE, RC_SIZE, NULL, &status);
snprintf(buffer, sizeof(buffer), "buffer2");
if (status != CL_SUCCESS)
goto out;
clState->buffer3 = clCreateBuffer(clState->context, CL_MEM_READ_WRITE, RC_SIZE, NULL, &status);
snprintf(buffer, sizeof(buffer), "buffer3");
if (status != CL_SUCCESS)
goto out;
clState->padbuffer8 = clCreateBuffer(clState->context, CL_MEM_READ_WRITE, 2 * sizeof(uint32_t), NULL, &status);
snprintf(buffer, sizeof(buffer), "padbuffer8");
if (status != CL_SUCCESS)
goto out;
clState->MidstateBuf = clCreateBuffer(clState->context, CL_MEM_READ_ONLY, 140, NULL, &status); // TODO: decrease buffer size to 64 bytes
snprintf(buffer, sizeof(buffer), "MidstateBuf");
if (status != CL_SUCCESS)
goto out;
clState->outputBuffer = clCreateBuffer(clState->context, CL_MEM_READ_WRITE, MAX(sizeof(sols_t), BUFFERSIZE), NULL, &status);
snprintf(buffer, sizeof(buffer), "outputBuffer");
if (status != CL_SUCCESS)
goto out;
cl_mem rowCounters[] = {clState->buffer2, clState->buffer3};
for (int round = 0; round < PARAM_K; round++) {
unsigned int num = 0;
cl_kernel *kernel = &clState->extra_kernels[1 + round];
if (!round) {
CL_SET_ARG(clState->MidstateBuf);
CL_SET_ARG(clState->index_buf[round]);
CL_SET_ARG(rowCounters[round % 2]);
}
else {
CL_SET_ARG(clState->index_buf[round - 1]);
CL_SET_ARG(clState->index_buf[round]);
CL_SET_ARG(rowCounters[(round - 1) % 2]);
CL_SET_ARG(rowCounters[round % 2]);
}
CL_SET_ARG(clState->padbuffer8);
}
unsigned int num = 0;
cl_kernel *kernel = &clState->extra_kernels[1 + 9];
CL_SET_ARG(clState->index_buf[8]);
CL_SET_ARG(clState->CLbuffer0);
CL_SET_ARG(rowCounters[0]);
num = 0;
kernel = &clState->kernel;
CL_SET_ARG(clState->index_buf[0]);
CL_SET_ARG(clState->index_buf[1]);
CL_SET_ARG(clState->outputBuffer);
CL_SET_ARG(rowCounters[0]);
CL_SET_ARG(rowCounters[1]);
for (int i = 2; i < 9; i++)
CL_SET_ARG(clState->index_buf[i]);
CL_SET_ARG(clState->CLbuffer0);
if (status != CL_SUCCESS) {
applog(LOG_ERR, "Error %d: Setting Kernel arguments for ALGO_EQUIHASH failed. (clSetKernelArg)", status);
return NULL;
}
clState->devid = cgpu->device_id;
return clState;
out:
applog(LOG_ERR, "Error %d: Creating Buffer \"%s\" failed. (clCreateBuffer)", status, buffer);
clState->kernel = clCreateKernel(clState->program, "search", &status);
if (status != CL_SUCCESS) {
applog(LOG_ERR, "Error %d: Creating Kernel from program. (clCreateKernel)", status);
return NULL;
}
else {
clState->kernel = clCreateKernel(clState->program, "search", &status);
if (status != CL_SUCCESS) {
applog(LOG_ERR, "Error %d: Creating Kernel from program. (clCreateKernel)", status);
return NULL;
}
clState->n_extra_kernels = algorithm->n_extra_kernels;
if (clState->n_extra_kernels > 0) {
unsigned int i;
char kernel_name[9]; // max: search99 + 0x0
clState->n_extra_kernels = algorithm->n_extra_kernels;
if (clState->n_extra_kernels > 0) {
unsigned int i;
char kernel_name[9]; // max: search99 + 0x0
clState->extra_kernels = (cl_kernel *)malloc(sizeof(cl_kernel)* clState->n_extra_kernels);
clState->extra_kernels = (cl_kernel *)malloc(sizeof(cl_kernel)* clState->n_extra_kernels);
for (i = 0; i < clState->n_extra_kernels; i++) {
snprintf(kernel_name, 9, "%s%d", "search", i + 1);
clState->extra_kernels[i] = clCreateKernel(clState->program, kernel_name, &status);
if (status != CL_SUCCESS) {
applog(LOG_ERR, "Error %d: Creating ExtraKernel #%d from program. (clCreateKernel)", status, i);
return NULL;
}
for (i = 0; i < clState->n_extra_kernels; i++) {
snprintf(kernel_name, 9, "%s%d", "search", i + 1);
clState->extra_kernels[i] = clCreateKernel(clState->program, kernel_name, &status);
if (status != CL_SUCCESS) {
applog(LOG_ERR, "Error %d: Creating ExtraKernel #%d from program. (clCreateKernel)", status, i);
return NULL;
}
}
}
@@ -1106,7 +1004,7 @@ out:
clState->devid = cgpu->device_id;
size_t buffersize = MAX(sizeof(sols_t), BUFFERSIZE);
size_t buffersize = BUFFERSIZE;
applog(LOG_DEBUG, "Using output buffer sized %lu", buffersize);
clState->outputBuffer = clCreateBuffer(clState->context, CL_MEM_WRITE_ONLY, buffersize, NULL, &status);
if (status != CL_SUCCESS) {
+111 -339
View File
@@ -536,9 +536,8 @@ static void sharelog(const char*disposition, const struct work*work)
static char *getwork_req = "{\"method\": \"getwork\", \"params\": [], \"id\":0}\n";
static char *gbt_req = "{\"id\": 0, \"method\": \"getblocktemplate\", \"params\": [{\"capabilities\": [\"coinbasetxn\", \"workid\", \"coinbase/append\"]}]}\n";
static char *gbt_req_equihash = "{\"id\": 0, \"method\": \"getblocktemplate\", \"params\": [{\"capabilities\": [\"coinbasevalue\", \"workid\", \"longpoll\"]}]}\n";
static char *gbt_req = "{\"id\": 0, \"method\": \"getblocktemplate\", \"params\": [{\"rules\": [\"segwit\"], "
"\"capabilities\": [\"coinbasetxn\", \"workid\", \"coinbase/append\"]}]}\n";
/* Adjust all the pools' quota to the greatest common denominator after a pool
* has been added or the quotas changed. */
@@ -2053,26 +2052,6 @@ static bool __build_gbt_txns(struct pool *pool, json_t *res_val)
if (unlikely(!pool->txn_hashes))
quit(1, "Failed to calloc txn_hashes in __build_gbt_txns");
if (pool->algorithm.type == ALGO_EQUIHASH) {
pool->coinbasetxn = realloc(pool->coinbasetxn, 1 << 22); // reuse coinbasetxn
size_t len = 0;
for (i = 0; i < pool->gbt_txns; i++) {
json_t *array_elem = json_array_get(txn_array, i);
json_t *txn_hash_val = json_object_get(array_elem, "hash");
const char *txn_hash = json_string_value(txn_hash_val);
hex2bin(bin, txn_hash, 32);
swab256(pool->txn_hashes + 32 * i, bin);
json_t *txn_val = json_object_get(array_elem, "data");
const char *txn = json_string_value(txn_val);
size_t txn_len = strlen(txn);
memcpy(pool->coinbasetxn + len, txn, txn_len + 1);
len += txn_len;
}
applog(LOG_DEBUG, "gbt_txns: %s", pool->coinbasetxn);
goto out;
}
if (!pool->gbt_txns)
goto out;
@@ -2227,23 +2206,7 @@ static void gen_gbt_work(struct pool *pool, struct work *work)
offsetMerkleRoot = 4 + 32;
/* Equihash is Version (4) + PrevblockHash (32) + Merkleroot (32) + Reserved (32) + CurTime (4) + nBits (4) + Nonce (32) + SolutionSize(3) + Solution (1344) */
if (pool->algorithm.type == ALGO_EQUIHASH) {
offsetTime = offsetMerkleRoot + 32 + 32;
headerLen = 143;
nonceLen = 32;
lenPadding = 0; //no padding
offsetBits = offsetTime + 4;
offsetNonce = offsetBits + 4;
//reserved - Not sure exactly what this is used for...
memcpy(work->data + offsetMerkleRoot + 32, pool->reserved, 32);
//set solutionsize - supposed to always be 1344
add_var_int(work->data + offsetNonce + 32, 1344);
}
else if (pool->algorithm.type == ALGO_LBRY) {
if (pool->algorithm.type == ALGO_LBRY) {
headerLen = 112;
offsetTime = 100;
offsetBits = 104;
@@ -2288,16 +2251,6 @@ static void gen_gbt_work(struct pool *pool, struct work *work)
free(merkleroot);
memset(work->data + offsetNonce, 0, nonceLen); /* nonce */
if (pool->algorithm.type == ALGO_EQUIHASH) {
//add entropy to nonce
memcpy(work->data + offsetNonce + 8, entropy, 12);
// Equihash is little endian -> swab32 first 140 bytes
for (int i = 0; i < 140; i += 4)
for (int j = 0; j < 4; j++)
work->equihash_data[i + j] = work->data[i + 3-j];
add_var_int(work->equihash_data + offsetNonce + 32, 1344);
}
if (lenPadding > 0) {
hex2bin(work->data + offsetPadding, workpadding, lenPadding);
}
@@ -2389,16 +2342,7 @@ static bool gbt_decode(struct pool *pool, json_t *res_val)
flags = json_string_value(json_object_get(json_object_get(res_val, "coinbaseaux"), "flags"));
coinbasevalue = json_integer_value(json_object_get(res_val, "coinbasevalue"));
bool invalid = false;
if (pool->algorithm.type == ALGO_EQUIHASH) {
applog(LOG_DEBUG, "USING EQUIHASH");
coinbasefrvalue = json_integer_value(json_object_get(res_val, "coinbasefrvalue"));
coinbasefrscript = json_string_value(json_object_get(res_val, "coinbasefrscript"));
invalid = (!previousblockhash || !target || !coinbasevalue || !height || !longpollid || !version || !curtime || !bits);
}
else {
invalid = (!previousblockhash || !target || !coinbasetxn || !longpollid || !version || !curtime || !bits);
}
bool invalid = (!previousblockhash || !target || !coinbasetxn || !longpollid || !version || !curtime || !bits);
if (previousblockhash)
applog(LOG_DEBUG, "previousblockhash: %s", previousblockhash);
@@ -2441,113 +2385,104 @@ static bool gbt_decode(struct pool *pool, json_t *res_val)
}
}
if (pool->algorithm.type == ALGO_EQUIHASH) {
int offset = 42, len;
unsigned char script_size, bin_value[44];
if (strncmp(coinbasetxn, "01000000", 8) && strncmp(coinbasetxn + 8, "0001", 4) == 0)
offset += 2;
free(pool->coinbase);
free(pool->coinbasetxn);
pool->coinbasetxn = strdup(coinbasetxn);
hex2bin(bin_value, pool->coinbasetxn, offset);
extra_len = (uint8_t *)(bin_value + offset - 1);
if (have_segwit) {
uint64_t *u64;
uint32_t *u32;
int txout_size, witnessdata_size, txout_start, witnessdata_start, ofs = 0;
len = offset + *extra_len + 4 + 1 + 8; // sequence, out counter, coinbase value
hex2bin(&script_size, pool->coinbasetxn + (len * 2), 1);
txout_start = len + 1;
txout_size = script_size;
len += txout_size + 8 + 1;
hex2bin(&script_size, pool->coinbasetxn + (len * 2), 1);
witnessdata_size = script_size;
witnessdata_start = len + 1;
unsigned char scriptsig_base[64] = {0};
ofs++; // Leave room for template length
/* Put block height at start of template. */
ofs += ser_number(scriptsig_base + ofs, height); // max 5
/* Followed by flags */
len = strlen(flags) / 2;
scriptsig_base[ofs++] = len;
hex2bin(scriptsig_base + ofs, flags, len);
ofs += len;
/* Followed by timestamp */
cgtime(&now);
scriptsig_base[ofs++] = 0xfe; // Encode seconds as u32
u32 = (uint32_t *)&scriptsig_base[ofs];
*u32 = htole32(now.tv_sec);
ofs += 4; // sizeof uint32_t
scriptsig_base[ofs++] = 0xfe; // Encode usecs as u32
u32 = (uint32_t *)&scriptsig_base[ofs];
*u32 = htole32(now.tv_usec);
ofs += 4; // sizeof uint32_t
memcpy(scriptsig_base + ofs, "\x09\x63\x67\x6d\x69\x6e\x65\x72\x34\x32", 10);
ofs += 10;
/* Followed by extranonce size, fixed at 8 */
scriptsig_base[ofs++] = 8;
pool->nonce2_offset = 41 + ofs;
ofs += 8;
scriptsig_base[0] = ofs++; // Template length
pool->n1_len = ofs;
len = 41 // prefix
+ ofs // Template length
+ 4 // txin sequence no
+ 1 // txouts
+ 8 // value
+ 1 + txout_size // txout
+ 4 // lock
+ 8 //value
+ 1 + witnessdata_size; // total scriptPubKey size + OP_RETURN + push size + data
pool->coinbase = calloc(len, 1);
hex2bin(pool->coinbase, scriptsig_header, 41);
pool->coinbase[41 + pool->n1_len + 4 + 1 + 8] = txout_size;
hex2bin(pool->coinbase + 41 + pool->n1_len + 4 + 1 + 8 + 1, pool->coinbasetxn + (txout_start * 2), txout_size);
memcpy(pool->coinbase + 41, scriptsig_base, ofs);
memcpy(pool->coinbase + 41 + ofs, "\xff\xff\xff\xff", 4);
pool->coinbase[41 + ofs + 4] = 2;
u64 = (uint64_t *)&(pool->coinbase[41 + ofs + 4 + 1]);
*u64 = htole64(coinbasevalue);
ofs += 41 + 4 + 1 + 8 + 1 + txout_size;
memset(pool->coinbase + ofs, 0, 8);
pool->coinbase[ofs + 8] = witnessdata_size;
hex2bin(pool->coinbase + ofs + 8 + 1, pool->coinbasetxn + (witnessdata_start * 2), witnessdata_size);
witnessdata_size += 9;
pool->nonce2 = 0;
pool->n2size = 4;
pool->coinbase_len = ofs + witnessdata_size + 4;
} else {
cbt_len = strlen(pool->coinbasetxn) / 2;
/* We add 8 bytes of extra data corresponding to nonce2 */
pool->n2size = 8;
if (!set_coinbasetxn(pool, height, coinbasevalue, coinbasefrvalue, coinbasefrscript)) {
cg_wunlock(&pool->gbt_lock);
return false;
}
}
else {
int offset = 42, len;
unsigned char script_size, bin_value[44];
if (strncmp(coinbasetxn, "01000000", 8) && strncmp(coinbasetxn + 8, "0001", 4) == 0)
offset += 2;
free(pool->coinbase);
free(pool->coinbasetxn);
pool->coinbasetxn = strdup(coinbasetxn);
hex2bin(bin_value, pool->coinbasetxn, offset);
extra_len = (uint8_t *)(bin_value + offset - 1);
if (have_segwit) {
uint64_t *u64;
uint32_t *u32;
int txout_size, witnessdata_size, txout_start, witnessdata_start, ofs = 0;
len = offset + *extra_len + 4 + 1 + 8; // sequence, out counter, coinbase value
hex2bin(&script_size, pool->coinbasetxn + (len * 2), 1);
txout_start = len + 1;
txout_size = script_size;
len += txout_size + 8 + 1;
hex2bin(&script_size, pool->coinbasetxn + (len * 2), 1);
witnessdata_size = script_size;
witnessdata_start = len + 1;
unsigned char scriptsig_base[64] = {0};
ofs++; // Leave room for template length
/* Put block height at start of template. */
ofs += ser_number(scriptsig_base + ofs, height); // max 5
/* Followed by flags */
len = strlen(flags) / 2;
scriptsig_base[ofs++] = len;
hex2bin(scriptsig_base + ofs, flags, len);
ofs += len;
/* Followed by timestamp */
cgtime(&now);
scriptsig_base[ofs++] = 0xfe; // Encode seconds as u32
u32 = (uint32_t *)&scriptsig_base[ofs];
*u32 = htole32(now.tv_sec);
ofs += 4; // sizeof uint32_t
scriptsig_base[ofs++] = 0xfe; // Encode usecs as u32
u32 = (uint32_t *)&scriptsig_base[ofs];
*u32 = htole32(now.tv_usec);
ofs += 4; // sizeof uint32_t
memcpy(scriptsig_base + ofs, "\x09\x63\x67\x6d\x69\x6e\x65\x72\x34\x32", 10);
ofs += 10;
/* Followed by extranonce size, fixed at 8 */
scriptsig_base[ofs++] = 8;
pool->nonce2_offset = 41 + ofs;
ofs += 8;
scriptsig_base[0] = ofs++; // Template length
pool->n1_len = ofs;
len = 41 // prefix
+ ofs // Template length
+ 4 // txin sequence no
+ 1 // txouts
+ 8 // value
+ 1 + txout_size // txout
+ 4 // lock
+ 8 //value
+ 1 + witnessdata_size; // total scriptPubKey size + OP_RETURN + push size + data
pool->coinbase = calloc(len, 1);
hex2bin(pool->coinbase, scriptsig_header, 41);
pool->coinbase[41 + pool->n1_len + 4 + 1 + 8] = txout_size;
hex2bin(pool->coinbase + 41 + pool->n1_len + 4 + 1 + 8 + 1, pool->coinbasetxn + (txout_start * 2), txout_size);
memcpy(pool->coinbase + 41, scriptsig_base, ofs);
memcpy(pool->coinbase + 41 + ofs, "\xff\xff\xff\xff", 4);
pool->coinbase[41 + ofs + 4] = 2;
u64 = (uint64_t *)&(pool->coinbase[41 + ofs + 4 + 1]);
*u64 = htole64(coinbasevalue);
ofs += 41 + 4 + 1 + 8 + 1 + txout_size;
memset(pool->coinbase + ofs, 0, 8);
pool->coinbase[ofs + 8] = witnessdata_size;
hex2bin(pool->coinbase + ofs + 8 + 1, pool->coinbasetxn + (witnessdata_start * 2), witnessdata_size);
witnessdata_size += 9;
pool->nonce2 = 0;
pool->n2size = 4;
pool->coinbase_len = ofs + witnessdata_size + 4;
} else {
cbt_len = strlen(pool->coinbasetxn) / 2;
/* We add 8 bytes of extra data corresponding to nonce2 */
pool->n2size = 8;
pool->coinbase_len = cbt_len + pool->n2size;
cal_len = pool->coinbase_len + 1;
align_len(&cal_len);
pool->coinbase = (unsigned char *)calloc(cal_len, 1);
memcpy(pool->coinbase, bin_value, offset);
extra_len = (uint8_t *)(pool->coinbase + offset - 1);
orig_len = *extra_len;
hex2bin(pool->coinbase + offset, pool->coinbasetxn + (offset * 2), orig_len);
*extra_len += pool->n2size;
hex2bin(pool->coinbase + offset + *extra_len, pool->coinbasetxn + (offset * 2) + (orig_len * 2),
cbt_len - orig_len - offset);
pool->nonce2_offset = orig_len + offset;
}
pool->coinbase_len = cbt_len + pool->n2size;
cal_len = pool->coinbase_len + 1;
align_len(&cal_len);
pool->coinbase = (unsigned char *)calloc(cal_len, 1);
memcpy(pool->coinbase, bin_value, offset);
extra_len = (uint8_t *)(pool->coinbase + offset - 1);
orig_len = *extra_len;
hex2bin(pool->coinbase + offset, pool->coinbasetxn + (offset * 2), orig_len);
*extra_len += pool->n2size;
hex2bin(pool->coinbase + offset + *extra_len, pool->coinbasetxn + (offset * 2) + (orig_len * 2),
cbt_len - orig_len - offset);
pool->nonce2_offset = orig_len + offset;
}
free(pool->longpollid);
@@ -2561,7 +2496,6 @@ static bool gbt_decode(struct pool *pool, json_t *res_val)
hex2bin(hash_swap, previousblockhash, 32);
swap256(pool->previousblockhash, hash_swap);
//equihash reserved currently all zeros but assuming it's supposed to be a reversed hash...
if (reserved) {
hex2bin(hash_swap, reserved, 32);
swap256(pool->reserved, hash_swap);
@@ -3449,36 +3383,6 @@ static bool submit_upstream_work(struct work *work, CURL *curl, char *curl_err_s
free(ASCIIMixHash);
free(ASCIIPoWHash);
}
else if(work->pool->algorithm.type == ALGO_EQUIHASH) {
/* equihash block: header (108) + nonce (32) + numbersolutions (3) + solution (1344) - total of 1487 , times 2 for hex string (2974) + json string (50) */
const int bin_len = sizeof(work->equihash_data);
char *result = bin2hex(work->equihash_data, bin_len); // block header
const int len = 2*bin_len;
char workid[256];
workid[0] = 0;
/*
// not supported
if (work->job_id) {
snprintf(workid, sizeof(workid), ", {\"workid\": \"%s\"}", work->job_id);
str_len += strlen(workid);
}
*/
uint8_t txn_cnt_bin[9];
cg_rlock(&pool->gbt_lock);
int txn_cnt_len = add_var_int(txn_cnt_bin, pool->gbt_txns + 1);
char *txn_cnt = bin2hex(txn_cnt_bin, txn_cnt_len);
int str_len = len + 512 + strlen(pool->coinbasetxn);
s = (char *)malloc(sizeof(char) * str_len);
snprintf(s, str_len, "{\"id\": 0, \"method\": \"submitblock\", "
"\"params\": [\"%s%s%s%s\"%s]}", result, txn_cnt, work->coinbase, pool->coinbasetxn, workid);
cg_runlock(&pool->gbt_lock);
free(result);
free(txn_cnt);
applog(LOG_DEBUG, "submitblock: %s", s);
}
else {
if (work->pool->algorithm.type == ALGO_CRE)
endian_flip168(work->data, work->data);
@@ -6151,31 +6055,6 @@ static void *stratum_sthread(void *userdata)
free(ASCIINonce);
free(ASCIIResult);
}
else if(pool->algorithm.type == ALGO_EQUIHASH) {
char *nonce;
char *solution;
sshare->sshare_time = time(NULL);
/* This work item is freed in parse_stratum_response */
sshare->work = work;
applog(LOG_DEBUG, "stratum_sthread() algorithm = %s", pool->algorithm.name);
//get nonce minus extranonce set by server
nonce = bin2hex(work->equihash_data+108, 32);
solution = bin2hex(work->equihash_data+140, 1347);
//applog(LOG_DEBUG, "%s: Nonce set to %s", __func__, nonce+strlen(work->nonce1));
mutex_lock(&sshare_lock);
/* Give the stratum share a unique id */
sshare->id = swork_id++;
mutex_unlock(&sshare_lock);
snprintf(s, s_size, "{\"id\": %d, \"method\": \"mining.submit\", \"params\": [\"%s\", \"%s\", \"%s\", \"%s\", \"%s\"]}", sshare->id, pool->rpc_user, work->job_id, work->ntime, nonce+strlen(work->nonce1), solution);
free(nonce);
free(solution);
}
else {
if (unlikely(work->nonce2_len > 8)) {
applog(LOG_ERR, "%s asking for inappropriately long nonce2 length %d", get_pool_name(pool), (int)work->nonce2_len);
@@ -6366,13 +6245,11 @@ retry_stratum:
if (!pool->probed) {
char *_gbt_req = gbt_req;
if (pool->algorithm.type == ALGO_EQUIHASH)
_gbt_req = gbt_req_equihash;
applog(LOG_DEBUG, "Probing for GBT support");
val = json_rpc_call(curl, curl_err_str, pool->rpc_url, pool->rpc_userpass,
_gbt_req, true, false, &rolltime, pool, false);
if (val) {
bool append = false, submit = false;
bool append = false;
json_t *res_val, *mutables;
int i, mutsize = 0;
@@ -6390,16 +6267,11 @@ retry_stratum:
if (!strncasecmp(mut, "coinbase/append", 15))
append = true;
else if (!strncasecmp(mut, "submit/coinbase", 15))
submit = true;
}
}
json_decref(val);
/* Only use GBT if it supports coinbase append and
* submit coinbase */
if ((append && submit) || pool->algorithm.type == ALGO_EQUIHASH) {
if (append) {
pool->has_gbt = true;
pool->rpc_req = _gbt_req;
}
@@ -6460,10 +6332,6 @@ retry_stratum:
copy_time(&work->tv_getwork_reply, &tv_getwork_reply);
work->getwork_mode = GETWORK_MODE_TESTPOOL;
if (pool->algorithm.type == ALGO_EQUIHASH) {
memcpy(work->target, pool->gbt_target, 32);
}
calc_diff(work, 0);
applog(LOG_DEBUG, "Pushing pooltest work to base pool");
@@ -6791,70 +6659,8 @@ static void gen_stratum_work_cn(struct pool *pool, struct work *work)
applog(LOG_DEBUG, "gen_stratum_work_cn() done.");
}
static void gen_stratum_work_equihash(struct pool *pool, struct work *work)
{
cg_wlock(&pool->data_lock);
work->nonce2 = pool->nonce2++;
work->nonce2_len = 2;
/* Downgrade to a read lock to read off the pool variables */
cg_dwlock(&pool->data_lock);
/* equihash already has the merkle root in the header no need to change it */
memset(work->equihash_data, 0, 1487);
memcpy(work->equihash_data, pool->header_bin, 128);
//add pool extra nonce
hex2bin(work->equihash_data + 108, pool->nonce1, strlen(pool->nonce1) / 2);
memcpy(work->equihash_data + 108 + 20 - work->nonce2_len, &work->nonce2, work->nonce2_len);
//add solutionsize
add_var_int(work->equihash_data + 140, 1344);
/* Store the stratum work diff to check it still matches the pool's
* stratum diff when submitting shares */
work->sdiff = pool->swork.diff;
/* Copy parameters required for share submission */
work->job_id = strdup(pool->swork.job_id);
work->nonce1 = strdup(pool->nonce1);
work->ntime = strdup(pool->swork.ntime);
cg_runlock(&pool->data_lock);
if (opt_debug) {
char *header;
header = bin2hex(work->equihash_data, 143);
applog(LOG_DEBUG, "[THR%d] Generated stratum header %s", work->thr_id, header);
applog(LOG_DEBUG, "[THR%d] job_id %s, nonce1 %s, nonce2 %"PRIu64", ntime %s", work->thr_id, work->job_id, work->nonce1, work->nonce2, work->ntime);
free(header);
}
/* equihash validates on the network target not share target... */
memcpy(work->target, pool->Target, 32);
local_work++;
work->pool = pool;
work->stratum = true;
work->blk.nonce = 0;
work->id = total_work++;
work->longpoll = false;
work->getwork_mode = GETWORK_MODE_STRATUM;
work->work_block = work_block;
/* Nominally allow a driver to ntime roll 60 seconds */
work->drv_rolllimit = 60;
calc_diff(work, work->sdiff);
cgtime(&work->tv_staged);
}
static void gen_stratum_work(struct pool *pool, struct work *work)
{
if (pool->algorithm.type == ALGO_EQUIHASH) {
gen_stratum_work_equihash(pool, work);
return;
}
unsigned char merkle_root[32], merkle_sha[64];
uint32_t *data32, *swap32;
uint64_t nonce2le;
@@ -7718,13 +7524,6 @@ bool submit_tested_work(struct thr_info *thr, struct work *work)
if (work->pool->algorithm.type == ALGO_CRYPTONIGHT) {
}
else if (work->pool->algorithm.type == ALGO_EQUIHASH) {
applog(LOG_DEBUG, "equihash target: %.16llx", *(uint64_t*) (work->target + 24));
if (*(uint64_t*) (work->hash + 24) > *(uint64_t*) (work->target + 24))
return false;
if (work->getwork_mode == GETWORK_MODE_GBT)
applog(LOG_WARNING, "Found zcash block!");
}
else if (!fulltest(work->hash, work->target)) {
applog(LOG_INFO, "%s %d: Share above target", thr->cgpu->drv->name,
thr->cgpu->device_id);
@@ -7738,15 +7537,6 @@ bool submit_tested_work(struct thr_info *thr, struct work *work)
/* Returns true if nonce for work was a valid share */
bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce)
{
//temporary
if (work->pool->algorithm.type == ALGO_EQUIHASH) {
struct work *work_out;
update_work_stats(thr, work);
work_out = copy_work(work);
submit_work_async(work_out);
return true;
}
if (test_nonce(work, nonce)) {
submit_tested_work(thr, work);
return true;
@@ -7836,11 +7626,6 @@ static void hash_sole_work(struct thr_info *mythr)
if (work->pool->algorithm.type == ALGO_NEOSCRYPT)
set_target_neoscrypt(work->device_target, work->device_diff, work->thr_id);
else if (work->pool->algorithm.type != ALGO_EQUIHASH) {
if (work->pool->algorithm.type == ALGO_ETHASH)
work->device_diff = MIN(work->sdiff, 60e6);
set_target(work->device_target, work->device_diff, work->pool->algorithm.diff_multiplier2, work->thr_id);
}
do {
cgtime(&tv_start);
@@ -8144,16 +7929,10 @@ retry_pool:
* avoid races */
if (pool->has_gbt) {
cg_rlock(&pool->gbt_lock);
if (pool->algorithm.type == ALGO_EQUIHASH)
snprintf(lpreq, sizeof(lpreq),
"{\"id\": 0, \"method\": \"getblocktemplate\", \"params\": "
"[{\"capabilities\": [\"coinbasevalue\", \"workid\", \"longpoll\"], "
"\"longpollid\": \"%s\"}]}\n", pool->longpollid);
else
snprintf(lpreq, sizeof(lpreq),
"{\"id\": 0, \"method\": \"getblocktemplate\", \"params\": "
"[{\"capabilities\": [\"coinbasetxn\", \"workid\", \"coinbase/append\"], "
"\"rules\": [\"segwit\"], \"longpollid\": \"%s\"}]}\n", pool->longpollid);
snprintf(lpreq, sizeof(lpreq),
"{\"id\": 0, \"method\": \"getblocktemplate\", \"params\": "
"[{\"capabilities\": [\"coinbasetxn\", \"workid\", \"coinbase/append\"], "
"\"rules\": [\"segwit\"], \"longpollid\": \"%s\"}]}\n", pool->longpollid);
cg_runlock(&pool->gbt_lock);
}
@@ -8161,8 +7940,6 @@ retry_pool:
* and any number of issues could have come up in the meantime
* so always establish a fresh connection instead of relying on
* a persistent one. */
if (pool->algorithm.type != ALGO_EQUIHASH)
curl_easy_setopt(curl, CURLOPT_FRESH_CONNECT, 1);
val = json_rpc_call(curl, curl_err_str, lp_url, pool->rpc_userpass,
lpreq, false, true, &rolltime, pool, false);
@@ -9281,8 +9058,9 @@ int main(int argc, char *argv[])
int fd = open("/dev/urandom", O_RDONLY);
if (fd < 0)
fd = open("/dev/random", O_RDONLY);
read(fd, &eth_nonce, 4);
read(fd, entropy, sizeof(entropy));
size_t r = read(fd, &eth_nonce, 4);
r += read(fd, entropy, sizeof(entropy));
assert(r);
close(fd);
#endif
mutex_init(&hash_lock);
@@ -9528,13 +9306,7 @@ int main(int argc, char *argv[])
quit(1, "Failed to malloc userpass");
char *point_chr = strchr(pool->rpc_user, '.');
if (pool->algorithm.type == ALGO_EQUIHASH && point_chr != NULL) {
*point_chr = '\0';
snprintf(pool->rpc_userpass, siz, "%s:%s", pool->rpc_user, pool->rpc_pass);
*point_chr = '.';
}
else
snprintf(pool->rpc_userpass, siz, "%s:%s", pool->rpc_user, pool->rpc_pass);
snprintf(pool->rpc_userpass, siz, "%s:%s", pool->rpc_user, pool->rpc_pass);
}
}
/* Set the currentpool to pool 0 */
+3 -181
View File
@@ -1532,143 +1532,8 @@ static char *json_array_string(json_t *val, unsigned int entry)
static char *blank_merkel = "0000000000000000000000000000000000000000000000000000000000000000";
static bool parse_notify_equihash(struct pool *pool, json_t *val)
{
char *job_id, *prev_hash, *reserved, *bbversion, *nbit, *ntime, *header, *merkle;
size_t cb1_len, cb2_len, alloc_len;
unsigned char *cb1, *cb2;
bool clean, invalid, ret = false;
int merkles, i;
json_t *arr = NULL;
job_id = json_array_string(val, 0);
merkles = 1;
bbversion = json_array_string(val, 1);
prev_hash = json_array_string(val, 2);
merkle = json_array_string(val, 3);
reserved = json_array_string(val, 4);
ntime = json_array_string(val, 5);
nbit = json_array_string(val, 6);
clean = json_is_true(json_array_get(val, 7));
if (!job_id || !prev_hash || !merkle || !reserved || !bbversion || !nbit || !ntime) {
goto out;
}
applog(LOG_DEBUG, "Valid Notify");
cg_wlock(&pool->data_lock);
free(pool->swork.job_id);
free(pool->swork.prev_hash);
free(pool->swork.bbversion);
free(pool->swork.nbit);
free(pool->swork.ntime);
pool->swork.job_id = job_id;
pool->swork.prev_hash = prev_hash;
cb1_len = 0;
cb2_len = 0;
pool->swork.bbversion = bbversion;
pool->swork.nbit = nbit;
pool->swork.ntime = ntime;
pool->swork.clean = clean;
if (pool->next_diff > 0) {
pool->swork.diff = pool->next_diff;
}
if (clean) {
pool->nonce2 = 0;
}
pool->merkle_offset = strlen(pool->swork.bbversion) + strlen(pool->swork.prev_hash);
pool->swork.header_len = (pool->merkle_offset / 2) +
/* merkle_hash */ 32 +
/* reserved */ 32 +
(strlen(pool->swork.ntime) / 2) +
(strlen(pool->swork.nbit) / 2) +
/* partial nonce */ 20;
pool->merkle_offset /= 2;
pool->swork.header_len = pool->swork.header_len * 2 + 1;
applog(LOG_DEBUG, "%s: pool->swork.header_len = %d", __func__, pool->swork.header_len);
align_len(&pool->swork.header_len);
if ((header = (char *)malloc(pool->swork.header_len)) == NULL) {
quithere(1, "%s: Failed to malloc header.", __func__);
}
snprintf(header, pool->swork.header_len,
"%s%s%s%s%s%s%s",
pool->swork.bbversion,
pool->swork.prev_hash,
merkle,
reserved,
pool->swork.ntime,
pool->swork.nbit,
"0000000000000000000000000000000000000000" /* partial empty nonce just to pad */
);
if (unlikely(!hex2bin(pool->header_bin, header, 128))) {
applog(LOG_WARNING, "%s: Failed to convert header to header_bin, got %s", __func__, header);
cg_wunlock(&pool->data_lock);
pool_failed(pool);
goto out;
}
/* A notify message is the closest stratum gets to a getwork */
pool->getwork_requested++;
total_getworks++;
cg_wunlock(&pool->data_lock);
ret = true;
if (pool == current_pool()) {
opt_work_update = true;
}
if (opt_protocol) {
applog(LOG_DEBUG, "job_id: %s", job_id);
applog(LOG_DEBUG, "version: %s", bbversion);
applog(LOG_DEBUG, "prev_hash: %s", prev_hash);
applog(LOG_DEBUG, "merkle: %s", merkle);
applog(LOG_DEBUG, "reserved: %s", reserved);
applog(LOG_DEBUG, "nbit: %s", nbit);
applog(LOG_DEBUG, "ntime: %s", ntime);
applog(LOG_DEBUG, "clean: %s", clean ? "yes" : "no");
}
out:
/* Annoying but we must not leak memory */
//only free these if we failed
if (!ret) {
free(job_id);
free(prev_hash);
free(reserved);
free(merkle);
free(bbversion);
free(nbit);
free(ntime);
}
free(header);
return ret;
}
static bool parse_notify(struct pool *pool, json_t *val)
{
if (pool->algorithm.type == ALGO_EQUIHASH) {
return parse_notify_equihash(pool, val);
}
char *job_id = NULL, *prev_hash = NULL, *coinbase1 = NULL,
*coinbase2 = NULL, *bbversion = NULL, *nbit = NULL,
*ntime = NULL, *header = NULL, *trie = NULL;
@@ -2095,34 +1960,6 @@ static bool parse_target(struct pool *pool, json_t *val)
return true;
}
static bool parse_extranonce_equihash(struct pool *pool, json_t *val)
{
char *n1str;
if (!(n1str = json_array_string(val, 1))) {
return false;
}
cg_wlock(&pool->data_lock);
free(pool->nonce1);
pool->nonce1 = n1str;
pool->n1_len = strlen(n1str) / 2; //size in bytes of nonce1 in the header
free(pool->nonce1bin);
if (unlikely(!(pool->nonce1bin = (unsigned char *)calloc(pool->n1_len, 1)))) {
quithere(1, "%s: Failed to calloc pool->nonce1bin", __func__);
}
hex2bin(pool->nonce1bin, pool->nonce1, pool->n1_len);
pool->n2size = 64 - pool->n1_len; //size in bytes of nonce2 in the header
pool->nonce2 = 0; //reset nonce 2 to 0
cg_wunlock(&pool->data_lock);
applog(LOG_NOTICE, "%s extranonce set to %s", get_pool_name(pool), n1str);
return true;
}
static bool parse_extranonce_ethash(struct pool *pool, json_t *val)
{
char *n1str;
@@ -2155,10 +1992,7 @@ static bool parse_extranonce_ethash(struct pool *pool, json_t *val)
static bool parse_extranonce(struct pool *pool, json_t *val)
{
if (pool->algorithm.type == ALGO_EQUIHASH) {
return parse_extranonce_equihash(pool, val);
}
else if (pool->algorithm.type == ALGO_ETHASH) {
if (pool->algorithm.type == ALGO_ETHASH) {
return parse_extranonce_ethash(pool, val);
}
@@ -3018,12 +2852,7 @@ resend:
sprintf(s, "{\"id\": %d, \"method\": \"mining.subscribe\", \"params\": [\""PACKAGE"/"CGMINER_VERSION"\", \"%s\"]}", swork_id++, pool->sessionid);
}
else {
if (pool->algorithm.type == ALGO_EQUIHASH) {
sprintf(s, "{\"id\":%d, \"method\":\"mining.subscribe\", \"params\":[\""PACKAGE"/"CGMINER_VERSION"\", null, \"%s\", \"%s\"]}", swork_id++, pool->sockaddr_url, pool->stratum_port);
}
else {
sprintf(s, "{\"id\": %d, \"method\": \"mining.subscribe\", \"params\": [\""PACKAGE"/"CGMINER_VERSION"\"]}", swork_id++);
}
sprintf(s, "{\"id\": %d, \"method\": \"mining.subscribe\", \"params\": [\""PACKAGE"/"CGMINER_VERSION"\"]}", swork_id++);
}
}
@@ -3088,14 +2917,7 @@ resend:
json_decref(val);
return true;
}
else if (pool->algorithm.type == ALGO_EQUIHASH) {
if (!(ret = parse_extranonce(pool, res_val))) {
applog(LOG_DEBUG, "%s: Failed to get parse extranonce.", __func__);
}
goto out;
}
sessionid = get_sessionid(res_val);
if (!sessionid) {
applog(LOG_DEBUG, "Failed to get sessionid in initiate_stratum");