|
| 1 | +/* |
| 2 | + * BLAKE implementation. |
| 3 | + * |
| 4 | + * ==========================(LICENSE BEGIN)============================ |
| 5 | + * |
| 6 | + * Copyright (c) 2007-2010 Projet RNRT SAPHIR |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining |
| 9 | + * a copy of this software and associated documentation files (the |
| 10 | + * "Software"), to deal in the Software without restriction, including |
| 11 | + * without limitation the rights to use, copy, modify, merge, publish, |
| 12 | + * distribute, sublicense, and/or sell copies of the Software, and to |
| 13 | + * permit persons to whom the Software is furnished to do so, subject to |
| 14 | + * the following conditions: |
| 15 | + * |
| 16 | + * The above copyright notice and this permission notice shall be |
| 17 | + * included in all copies or substantial portions of the Software. |
| 18 | + * |
| 19 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 20 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 21 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 22 | + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 23 | + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 24 | + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 25 | + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 26 | + * |
| 27 | + * ===========================(LICENSE END)============================= |
| 28 | + * |
| 29 | + * @author Thomas Pornin <[email protected]> |
| 30 | + * |
| 31 | + * Modified for more speed by BlueDragon747 for the Blakecoin project |
| 32 | + */ |
| 33 | + |
| 34 | +#include <stddef.h> |
| 35 | +#include <string.h> |
| 36 | +#include <limits.h> |
| 37 | +#include <stdint.h> |
| 38 | + |
| 39 | +#include "sph/sph_sha2.h" |
| 40 | +#include "algorithm/pascal.h" |
| 41 | + |
| 42 | +static const uint32_t diff1targ_pascal = 0x000000ff; |
| 43 | + |
| 44 | +void pascalhash(void *state, const void *input) |
| 45 | +{ |
| 46 | + sph_sha256_context ctx_sha; |
| 47 | + uint32_t hash[16]; |
| 48 | + |
| 49 | + sph_sha256_init(&ctx_sha); |
| 50 | + sph_sha256(&ctx_sha, input, 200); |
| 51 | + sph_sha256_close(&ctx_sha, hash); |
| 52 | + |
| 53 | + sph_sha256_init(&ctx_sha); |
| 54 | + sph_sha256(&ctx_sha, hash, 32); |
| 55 | + sph_sha256_close(&ctx_sha, hash); |
| 56 | + |
| 57 | + memcpy(state, hash, 32); |
| 58 | + |
| 59 | +} |
| 60 | + |
| 61 | +void pascal_midstate(struct work *work) |
| 62 | +{ |
| 63 | + sph_sha256_context ctx_sha; |
| 64 | + uint32_t data[48]; |
| 65 | + |
| 66 | + memcpy(data, work->data, 192); |
| 67 | + |
| 68 | + sph_sha256_init(&ctx_sha); |
| 69 | + sph_sha256 (&ctx_sha, (unsigned char *)data, 192); |
| 70 | + |
| 71 | + memcpy(work->midstate, ctx_sha.val, 32); |
| 72 | + endian_flip32(work->midstate, work->midstate); |
| 73 | + |
| 74 | +/* |
| 75 | + char *strdata, *strmidstate; |
| 76 | + strdata = bin2hex(work->data, 192); |
| 77 | + strmidstate = bin2hex(work->midstate, 32); |
| 78 | + applog(LOG_DEBUG, "data %s midstate %s", strdata, strmidstate); |
| 79 | +*/ |
| 80 | +} |
| 81 | + |
| 82 | +static const uint32_t diff1targ = 0x0000ffff; |
| 83 | + |
| 84 | +/* Used externally as confirmation of correct OCL code */ |
| 85 | +int pascal_test(unsigned char *pdata, const unsigned char *ptarget, uint32_t nonce) |
| 86 | +{ |
| 87 | + uint32_t tmp_hash7, Htarg = le32toh(((const uint32_t *)ptarget)[7]); |
| 88 | + uint32_t data[50], ohash[8]; |
| 89 | + |
| 90 | + memcpy(data, pdata, 200); |
| 91 | + data[49] = htobe32(nonce); |
| 92 | + pascalhash(ohash, data); |
| 93 | + tmp_hash7 = be32toh(ohash[7]); |
| 94 | + |
| 95 | + applog(LOG_DEBUG, "htarget %08lx diff1 %08lx hash %08lx", |
| 96 | + (long unsigned int)Htarg, |
| 97 | + (long unsigned int)diff1targ, |
| 98 | + (long unsigned int)tmp_hash7); |
| 99 | + if (tmp_hash7 > diff1targ) |
| 100 | + return -1; |
| 101 | + if (tmp_hash7 > Htarg) |
| 102 | + return 0; |
| 103 | + return 1; |
| 104 | +} |
| 105 | + |
| 106 | +void pascal_regenhash(struct work *work) |
| 107 | +{ |
| 108 | + uint32_t data[64]; |
| 109 | + uint32_t hash[16]; |
| 110 | + uint32_t *nonce = (uint32_t *)(work->data + 196); |
| 111 | + uint32_t *ohash = (uint32_t *)(work->hash); |
| 112 | + |
| 113 | + memcpy(data, work->data, 200); |
| 114 | + data[49] = htole32(*nonce); |
| 115 | + pascalhash(hash, data); |
| 116 | + swab256(ohash, hash); |
| 117 | +} |
0 commit comments