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