mirror of https://github.com/GOSTSec/sgminer
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
188 lines
5.3 KiB
188 lines
5.3 KiB
/*- |
|
* Copyright 2009 Colin Percival, 2014 phm |
|
* All rights reserved. |
|
* |
|
* Redistribution and use in source and binary forms, with or without |
|
* modification, are permitted provided that the following conditions |
|
* are met: |
|
* 1. Redistributions of source code must retain the above copyright |
|
* notice, this list of conditions and the following disclaimer. |
|
* 2. Redistributions in binary form must reproduce the above copyright |
|
* notice, this list of conditions and the following disclaimer in the |
|
* documentation and/or other materials provided with the distribution. |
|
* |
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
|
* SUCH DAMAGE. |
|
* |
|
* This file was originally written by Colin Percival as part of the Tarsnap |
|
* online backup system. |
|
*/ |
|
|
|
#include "config.h" |
|
#include "miner.h" |
|
|
|
#include <stdlib.h> |
|
#include <stdint.h> |
|
#include <string.h> |
|
|
|
|
|
#include "sph/sph_luffa.h" |
|
#include "sph/sph_cubehash.h" |
|
#include "sph/sph_shavite.h" |
|
#include "sph/sph_simd.h" |
|
#include "sph/sph_echo.h" |
|
|
|
/* Move init out of loop, so init once externally, and then use one single memcpy with that bigger memory block */ |
|
typedef struct { |
|
sph_luffa512_context luffa1; |
|
sph_cubehash512_context cubehash1; |
|
sph_shavite512_context shavite1; |
|
sph_simd512_context simd1; |
|
sph_echo512_context echo1; |
|
} Qhash_context_holder; |
|
|
|
Qhash_context_holder base_contexts; |
|
|
|
|
|
void init_Qhash_contexts() |
|
{ |
|
sph_luffa512_init(&base_contexts.luffa1); |
|
sph_cubehash512_init(&base_contexts.cubehash1); |
|
sph_shavite512_init(&base_contexts.shavite1); |
|
sph_simd512_init(&base_contexts.simd1); |
|
sph_echo512_init(&base_contexts.echo1); |
|
} |
|
|
|
|
|
/* |
|
* Encode a length len/4 vector of (uint32_t) into a length len vector of |
|
* (unsigned char) in big-endian form. Assumes len is a multiple of 4. |
|
*/ |
|
static inline void |
|
be32enc_vect(uint32_t *dst, const uint32_t *src, uint32_t len) |
|
{ |
|
uint32_t i; |
|
|
|
for (i = 0; i < len; i++) |
|
dst[i] = htobe32(src[i]); |
|
} |
|
|
|
|
|
#ifdef __APPLE_CC__ |
|
static |
|
#endif |
|
void qhash(void *state, const void *input) |
|
{ |
|
init_Qhash_contexts(); |
|
|
|
Qhash_context_holder ctx; |
|
|
|
uint32_t hashA[16], hashB[16]; |
|
//luffa-cubehash-shivite-simd-echo |
|
memcpy(&ctx, &base_contexts, sizeof(base_contexts)); |
|
|
|
sph_luffa512 (&ctx.luffa1, input, 80); |
|
sph_luffa512_close (&ctx.luffa1, hashA); |
|
|
|
sph_cubehash512 (&ctx.cubehash1, hashA, 64); |
|
sph_cubehash512_close(&ctx.cubehash1, hashB); |
|
|
|
sph_shavite512 (&ctx.shavite1, hashB, 64); |
|
sph_shavite512_close(&ctx.shavite1, hashA); |
|
|
|
sph_simd512 (&ctx.simd1, hashA, 64); |
|
sph_simd512_close(&ctx.simd1, hashB); |
|
|
|
sph_echo512 (&ctx.echo1, hashB, 64); |
|
sph_echo512_close(&ctx.echo1, hashA); |
|
|
|
memcpy(state, hashA, 32); |
|
} |
|
|
|
static const uint32_t diff1targ = 0x0000ffff; |
|
|
|
|
|
/* Used externally as confirmation of correct OCL code */ |
|
int qubitcoin_test(unsigned char *pdata, const unsigned char *ptarget, uint32_t nonce) |
|
{ |
|
uint32_t tmp_hash7, Htarg = le32toh(((const uint32_t *)ptarget)[7]); |
|
uint32_t data[20], ohash[8]; |
|
|
|
be32enc_vect(data, (const uint32_t *)pdata, 19); |
|
data[19] = htobe32(nonce); |
|
qhash(ohash, data); |
|
tmp_hash7 = be32toh(ohash[7]); |
|
|
|
applog(LOG_DEBUG, "htarget %08lx diff1 %08lx hash %08lx", |
|
(long unsigned int)Htarg, |
|
(long unsigned int)diff1targ, |
|
(long unsigned int)tmp_hash7); |
|
if (tmp_hash7 > diff1targ) |
|
return -1; |
|
if (tmp_hash7 > Htarg) |
|
return 0; |
|
return 1; |
|
} |
|
|
|
void qubitcoin_regenhash(struct work *work) |
|
{ |
|
uint32_t data[20]; |
|
uint32_t *nonce = (uint32_t *)(work->data + 76); |
|
uint32_t *ohash = (uint32_t *)(work->hash); |
|
|
|
be32enc_vect(data, (const uint32_t *)work->data, 19); |
|
data[19] = htobe32(*nonce); |
|
qhash(ohash, data); |
|
} |
|
|
|
bool scanhash_qubitcoin(struct thr_info *thr, const unsigned char __maybe_unused *pmidstate, |
|
unsigned char *pdata, unsigned char __maybe_unused *phash1, |
|
unsigned char __maybe_unused *phash, const unsigned char *ptarget, |
|
uint32_t max_nonce, uint32_t *last_nonce, uint32_t n) |
|
{ |
|
uint32_t *nonce = (uint32_t *)(pdata + 76); |
|
uint32_t data[20]; |
|
uint32_t tmp_hash7; |
|
uint32_t Htarg = le32toh(((const uint32_t *)ptarget)[7]); |
|
bool ret = false; |
|
|
|
be32enc_vect(data, (const uint32_t *)pdata, 19); |
|
|
|
while(1) { |
|
uint32_t ostate[8]; |
|
|
|
*nonce = ++n; |
|
data[19] = (n); |
|
qhash(ostate, data); |
|
tmp_hash7 = (ostate[7]); |
|
|
|
applog(LOG_INFO, "data7 %08lx", |
|
(long unsigned int)data[7]); |
|
|
|
if (unlikely(tmp_hash7 <= Htarg)) { |
|
((uint32_t *)pdata)[19] = htobe32(n); |
|
*last_nonce = n; |
|
ret = true; |
|
break; |
|
} |
|
|
|
if (unlikely((n >= max_nonce) || thr->work_restart)) { |
|
*last_nonce = n; |
|
break; |
|
} |
|
} |
|
|
|
return ret; |
|
} |
|
|
|
|
|
|
|
|