|
| 1 | +/*- |
| 2 | + * Copyright 2009 Colin Percival, 2011 ArtForz |
| 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 | + * This file was originally written by Colin Percival as part of the Tarsnap |
| 27 | + * online backup system. |
| 28 | + */ |
| 29 | + |
| 30 | +#include "config.h" |
| 31 | +#include "miner.h" |
| 32 | + |
| 33 | +#include <stdlib.h> |
| 34 | +#include <stdint.h> |
| 35 | +#include <string.h> |
| 36 | + |
| 37 | + |
| 38 | +#include "sph/sph_blake.h" |
| 39 | +#include "sph/sph_bmw.h" |
| 40 | +#include "sph/sph_groestl.h" |
| 41 | +#include "sph/sph_jh.h" |
| 42 | +#include "sph/sph_keccak.h" |
| 43 | +#include "sph/sph_skein.h" |
| 44 | +#include "sph/sph_luffa.h" |
| 45 | +#include "sph/sph_cubehash.h" |
| 46 | +#include "sph/sph_shavite.h" |
| 47 | +#include "sph/sph_simd.h" |
| 48 | +#include "sph/sph_echo.h" |
| 49 | + |
| 50 | +/* Move init out of loop, so init once externally, and then use one single memcpy with that bigger memory block */ |
| 51 | +typedef struct { |
| 52 | + sph_blake512_context blake1; |
| 53 | + sph_bmw512_context bmw1; |
| 54 | + sph_groestl512_context groestl1; |
| 55 | + sph_skein512_context skein1; |
| 56 | + sph_jh512_context jh1; |
| 57 | + sph_keccak512_context keccak1; |
| 58 | + sph_luffa512_context luffa1; |
| 59 | + sph_cubehash512_context cubehash1; |
| 60 | + sph_shavite512_context shavite1; |
| 61 | + sph_simd512_context simd1; |
| 62 | + sph_echo512_context echo1; |
| 63 | +} Xhash_context_holder; |
| 64 | + |
| 65 | +static Xhash_context_holder base_contexts; |
| 66 | + |
| 67 | + |
| 68 | +static void init_Xhash_contexts() |
| 69 | +{ |
| 70 | + sph_blake512_init(&base_contexts.blake1); |
| 71 | + sph_skein512_init(&base_contexts.skein1); |
| 72 | + sph_bmw512_init(&base_contexts.bmw1); |
| 73 | + sph_groestl512_init(&base_contexts.groestl1); |
| 74 | + sph_jh512_init(&base_contexts.jh1); |
| 75 | + sph_luffa512_init(&base_contexts.luffa1); |
| 76 | + sph_keccak512_init(&base_contexts.keccak1); |
| 77 | + sph_cubehash512_init(&base_contexts.cubehash1); |
| 78 | + sph_simd512_init(&base_contexts.simd1); |
| 79 | + sph_shavite512_init(&base_contexts.shavite1); |
| 80 | + sph_echo512_init(&base_contexts.echo1); |
| 81 | +} |
| 82 | + |
| 83 | +static void xhash(void *state, const void *input) |
| 84 | +{ |
| 85 | + init_Xhash_contexts(); |
| 86 | + |
| 87 | + Xhash_context_holder ctx; |
| 88 | + |
| 89 | + uint32_t hashA[16], hashB[16]; |
| 90 | + //blake-bmw-groestl-sken-jh-meccak-luffa-cubehash-shivite-simd-echo |
| 91 | + memcpy(&ctx, &base_contexts, sizeof(base_contexts)); |
| 92 | + |
| 93 | + sph_blake512 (&ctx.blake1, input, 80); |
| 94 | + sph_blake512_close (&ctx.blake1, hashA); |
| 95 | + |
| 96 | + sph_skein512 (&ctx.skein1, hashA, 64); |
| 97 | + sph_skein512_close(&ctx.skein1, hashB); |
| 98 | + |
| 99 | + sph_bmw512 (&ctx.bmw1, hashB, 64); |
| 100 | + sph_bmw512_close(&ctx.bmw1, hashA); |
| 101 | + |
| 102 | + sph_groestl512 (&ctx.groestl1, hashA, 64); |
| 103 | + sph_groestl512_close(&ctx.groestl1, hashB); |
| 104 | + |
| 105 | + sph_jh512 (&ctx.jh1, hashB, 64); |
| 106 | + sph_jh512_close(&ctx.jh1, hashA); |
| 107 | + |
| 108 | + sph_luffa512 (&ctx.luffa1, hashA, 64); |
| 109 | + sph_luffa512_close (&ctx.luffa1, hashB); |
| 110 | + |
| 111 | + sph_keccak512 (&ctx.keccak1, hashB, 64); |
| 112 | + sph_keccak512_close(&ctx.keccak1, hashA); |
| 113 | + |
| 114 | + sph_cubehash512 (&ctx.cubehash1, hashA, 64); |
| 115 | + sph_cubehash512_close(&ctx.cubehash1, hashB); |
| 116 | + |
| 117 | + sph_simd512 (&ctx.simd1, hashB, 64); |
| 118 | + sph_simd512_close(&ctx.simd1, hashA); |
| 119 | + |
| 120 | + sph_shavite512 (&ctx.shavite1, hashA, 64); |
| 121 | + sph_shavite512_close(&ctx.shavite1, hashB); |
| 122 | + |
| 123 | + sph_echo512 (&ctx.echo1, hashB, 64); |
| 124 | + sph_echo512_close(&ctx.echo1, hashA); |
| 125 | + |
| 126 | + memcpy(state, hashA, 32); |
| 127 | + |
| 128 | +} |
| 129 | + |
| 130 | +static const uint32_t diff1targ = 0x0000ffff; |
| 131 | + |
| 132 | + |
| 133 | +/* Used externally as confirmation of correct OCL code */ |
| 134 | +int chainox_test(unsigned char *pdata, const unsigned char *ptarget, uint32_t nonce) |
| 135 | +{ |
| 136 | + uint32_t tmp_hash7, Htarg = le32toh(((const uint32_t *)ptarget)[7]); |
| 137 | + uint32_t data[20], ohash[8]; |
| 138 | + |
| 139 | + be32enc_vect(data, (const uint32_t *)pdata, 19); |
| 140 | + data[19] = htobe32(nonce); |
| 141 | + xhash(ohash, data); |
| 142 | + tmp_hash7 = be32toh(ohash[7]); |
| 143 | + |
| 144 | + applog(LOG_DEBUG, "htarget %08lx diff1 %08lx hash %08lx", |
| 145 | + (long unsigned int)Htarg, |
| 146 | + (long unsigned int)diff1targ, |
| 147 | + (long unsigned int)tmp_hash7); |
| 148 | + if (tmp_hash7 > diff1targ) |
| 149 | + return -1; |
| 150 | + if (tmp_hash7 > Htarg) |
| 151 | + return 0; |
| 152 | + return 1; |
| 153 | +} |
| 154 | + |
| 155 | +void chainox_regenhash(struct work *work) |
| 156 | +{ |
| 157 | + uint32_t data[20]; |
| 158 | + uint32_t *nonce = (uint32_t *)(work->data + 76); |
| 159 | + uint32_t *ohash = (uint32_t *)(work->hash); |
| 160 | + |
| 161 | + be32enc_vect(data, (const uint32_t *)work->data, 19); |
| 162 | + data[19] = htobe32(*nonce); |
| 163 | + xhash(ohash, data); |
| 164 | +} |
| 165 | + |
| 166 | +bool scanhash_chainox(struct thr_info *thr, const unsigned char __maybe_unused *pmidstate, |
| 167 | + unsigned char *pdata, unsigned char __maybe_unused *phash1, |
| 168 | + unsigned char __maybe_unused *phash, const unsigned char *ptarget, |
| 169 | + uint32_t max_nonce, uint32_t *last_nonce, uint32_t n) |
| 170 | +{ |
| 171 | + uint32_t *nonce = (uint32_t *)(pdata + 76); |
| 172 | + uint32_t data[20]; |
| 173 | + uint32_t tmp_hash7; |
| 174 | + uint32_t Htarg = le32toh(((const uint32_t *)ptarget)[7]); |
| 175 | + bool ret = false; |
| 176 | + |
| 177 | + be32enc_vect(data, (const uint32_t *)pdata, 19); |
| 178 | + |
| 179 | + while(1) { |
| 180 | + uint32_t ostate[8]; |
| 181 | + |
| 182 | + *nonce = ++n; |
| 183 | + data[19] = (n); |
| 184 | + xhash(ostate, data); |
| 185 | + tmp_hash7 = (ostate[7]); |
| 186 | + |
| 187 | + applog(LOG_INFO, "data7 %08lx", |
| 188 | + (long unsigned int)data[7]); |
| 189 | + |
| 190 | + if (unlikely(tmp_hash7 <= Htarg)) { |
| 191 | + ((uint32_t *)pdata)[19] = htobe32(n); |
| 192 | + *last_nonce = n; |
| 193 | + ret = true; |
| 194 | + break; |
| 195 | + } |
| 196 | + |
| 197 | + if (unlikely((n >= max_nonce) || thr->work_restart)) { |
| 198 | + *last_nonce = n; |
| 199 | + break; |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + return ret; |
| 204 | +} |
| 205 | + |
| 206 | + |
| 207 | + |
0 commit comments