diff --git a/Makefile.am b/Makefile.am index f6694fe5..c36ea1db 100644 --- a/Makefile.am +++ b/Makefile.am @@ -78,6 +78,7 @@ sgminer_SOURCES += algorithm/pluck.c algorithm/pluck.h sgminer_SOURCES += algorithm/credits.c algorithm/credits.h sgminer_SOURCES += algorithm/yescrypt.h algorithm/yescrypt.c algorithm/yescrypt_core.h algorithm/yescrypt-opt.c algorithm/yescryptcommon.c algorithm/sysendian.h sgminer_SOURCES += algorithm/blake256.c algorithm/blake256.h +sgminer_SOURCES += algorithm/blakecoin.c algorithm/blakecoin.h bin_SCRIPTS = $(top_srcdir)/kernel/*.cl diff --git a/algorithm.c b/algorithm.c index 7580d6a4..406a84c6 100644 --- a/algorithm.c +++ b/algorithm.c @@ -38,6 +38,7 @@ #include "algorithm/yescrypt.h" #include "algorithm/credits.h" #include "algorithm/blake256.h" +#include "algorithm/blakecoin.h" #include "compat.h" @@ -1035,9 +1036,9 @@ static algorithm_settings_t algos[] = { { "whirlcoin", ALGO_WHIRL, "", 1, 1, 1, 0, 0, 0xFF, 0xFFFFULL, 0x0000ffffUL, 3, 8 * 16 * 4194304, CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE, whirlcoin_regenhash, queue_whirlcoin_kernel, sha256, NULL }, { "whirlpoolx", ALGO_WHIRLPOOLX, "", 1, 1, 1, 0, 0, 0xFF, 0xFFFFULL, 0x0000FFFFUL, 0, 0, 0, whirlpoolx_regenhash, queue_whirlpoolx_kernel, gen_hash, NULL }, - { "blake256r8", ALGO_BLAKECOIN, "", 1, 1, 1, 0, 0, 0xFF, 0xFFFFULL, 0x000000ffUL, 0, 128, 0, blake_regenhash, queue_blake_kernel, sha256, NULL }, - { "blake256r14", ALGO_BLAKE, "", 1, 1, 1, 0, 0, 0xFF, 0xFFFFULL, 0x00000000UL, 0, 128, 0, blake_regenhash, queue_blake_kernel, gen_hash, NULL }, - { "vanilla", ALGO_VANILLA, "", 1, 1, 1, 0, 0, 0xFF, 0xFFFFULL, 0x000000ffUL, 0, 128, 0, blake_regenhash, queue_blake_kernel, gen_hash, NULL }, + { "blake256r8", ALGO_BLAKECOIN, "", 1, 1, 1, 0, 0, 0xFF, 0xFFFFULL, 0x000000ffUL, 0, 128, 0, blakecoin_regenhash, queue_blake_kernel, sha256, NULL }, + { "blake256r14", ALGO_BLAKE, "", 1, 1, 1, 0, 0, 0xFF, 0xFFFFULL, 0x00000000UL, 0, 128, 0, blake256_regenhash, queue_blake_kernel, gen_hash, NULL }, + { "vanilla", ALGO_VANILLA, "", 1, 1, 1, 0, 0, 0xFF, 0xFFFFULL, 0x000000ffUL, 0, 128, 0, blakecoin_regenhash, queue_blake_kernel, gen_hash, NULL }, // Terminator (do not remove) { NULL, ALGO_UNK, "", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL } diff --git a/algorithm/blake256.c b/algorithm/blake256.c index 0c2a91dd..e347bc23 100644 --- a/algorithm/blake256.c +++ b/algorithm/blake256.c @@ -46,85 +46,62 @@ static inline void be32enc_vect(uint32_t *dst, const uint32_t *src, uint32_t len) { - uint32_t i; + uint32_t i; - for (i = 0; i < len; i++) - dst[i] = htobe32(src[i]); + for (i = 0; i < len; i++) + dst[i] = htobe32(src[i]); } static const uint32_t diff1targ_blake256 = 0x000000ff; -inline void blakehash(void *state, const void *input) +inline void blake256hash(void *state, const void *input) { - sph_blake256_context ctx_blake; - sph_blake256_init(&ctx_blake); - sph_blake256(&ctx_blake, input, 80); - sph_blake256_close(&ctx_blake, state); + sph_blake256_context ctx_blake; + sph_blake256_init(&ctx_blake); + sph_blake256(&ctx_blake, input, 80); + sph_blake256_close(&ctx_blake, state); } -void blake_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); - - applog(LOG_DEBUG, "timestamp %d", data[17]); - - applog(LOG_DEBUG, "Dat0: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x", - data[ 0], data[ 1], data[ 2], data[ 3], data[ 4], data[ 5], data[ 6], data[ 7], data[ 8], data[ 9], - data[10], data[11], data[12], data[13], data[14], data[15], data[16], data[17], data[18], data[19]); - - if (work->pool->algorithm.type == ALGO_BLAKE) - blake256_rounds = 14; - else if (work->pool->algorithm.type == ALGO_BLAKECOIN || work->pool->algorithm.type == ALGO_VANILLA) - blake256_rounds = 8; - - blakehash(ohash, data); -} +static const uint32_t diff1targ = 0x0000ffff; + /* Used externally as confirmation of correct OCL code */ -int blake_test(unsigned char *pdata, const unsigned char *ptarget, uint32_t nonce) +int blake256_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); - - applog(LOG_DEBUG, "Dat0: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x", - data[ 0], data[ 1], data[ 2], data[ 3], data[ 4], data[ 5], data[ 6], data[ 7], data[ 8], data[ 9], - data[10], data[11], data[12], data[13], data[14], data[15], data[16], data[17], data[18], data[19]); - - sph_blake256_context ctx_blake; - sph_blake256_init(&ctx_blake); - sph_blake256(&ctx_blake, (unsigned char *)data, 80); - sph_blake256_close(&ctx_blake, (unsigned char *)ohash); - - flip32(ohash, ohash); // Not needed for scrypt-chacha - mikaelh - uint32_t *o = ohash; - applog(LOG_DEBUG, "Nonce: %x, Output buffe0: %x %x %x %x %x %x %x %x", nonce, o[0], o[1], o[2], o[3], o[4], o[5], o[6], o[7]); - + blake256hash(ohash, data); tmp_hash7 = be32toh(ohash[7]); - applog(LOG_DEBUG, "Nonce %x harget %08lx diff1 %08lx hash %08lx", - nonce, + applog(LOG_DEBUG, "htarget %08lx diff1 %08lx hash %08lx", (long unsigned int)Htarg, - (long unsigned int)diff1targ_blake256, + (long unsigned int)diff1targ, (long unsigned int)tmp_hash7); - if (tmp_hash7 > diff1targ_blake256) + if (tmp_hash7 > diff1targ) return -1; if (tmp_hash7 > Htarg) return 0; return 1; } -bool scanhash_blake(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) +void blake256_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); + blake256hash(ohash, data); +} + +bool scanhash_blake256(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]; @@ -134,16 +111,16 @@ bool scanhash_blake(struct thr_info *thr, const unsigned char __maybe_unused *pm be32enc_vect(data, (const uint32_t *)pdata, 19); - while (1) { + while(1) { uint32_t ostate[8]; *nonce = ++n; data[19] = (n); - blakehash(ostate, data); + blake256hash(ostate, data); tmp_hash7 = (ostate[7]); applog(LOG_INFO, "data7 %08lx", - (long unsigned int)data[7]); + (long unsigned int)data[7]); if (unlikely(tmp_hash7 <= Htarg)) { ((uint32_t *)pdata)[19] = htobe32(n); diff --git a/algorithm/blake256.h b/algorithm/blake256.h index 39698b6e..535a7399 100644 --- a/algorithm/blake256.h +++ b/algorithm/blake256.h @@ -1,9 +1,9 @@ -#ifndef BLAKE_H -#define BLAKE_H +#ifndef BLAKE256_H +#define BLAKE256_H #include "miner.h" -extern int blake_test(unsigned char *pdata, const unsigned char *ptarget, uint32_t nonce); -extern void blake_regenhash(struct work *work); +extern int blake256_test(unsigned char *pdata, const unsigned char *ptarget, uint32_t nonce); +extern void blake256_regenhash(struct work *work); -#endif /* BLAKE_H */ \ No newline at end of file +#endif /* BLAKE256_H */ \ No newline at end of file diff --git a/algorithm/blakecoin.c b/algorithm/blakecoin.c new file mode 100644 index 00000000..58974c91 --- /dev/null +++ b/algorithm/blakecoin.c @@ -0,0 +1,139 @@ +/* + * BLAKE implementation. + * + * ==========================(LICENSE BEGIN)============================ + * + * Copyright (c) 2007-2010 Projet RNRT SAPHIR + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * ===========================(LICENSE END)============================= + * + * @author Thomas Pornin + * + * Modified for more speed by BlueDragon747 for the Blakecoin project + */ + +#include +#include +#include +#include + +#include "sph/sph_blake.h" +#include "algorithm/blakecoin.h" + +/* +* 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]); +} + +static const uint32_t diff1targ_blake256 = 0x000000ff; + +inline void blakecoinhash(void *state, const void *input) +{ + sph_blake256_context ctx_blake; + sph_blake256_init(&ctx_blake); + sph_blake256r8(&ctx_blake, input, 80); + sph_blake256r8_close(&ctx_blake, state); +} + +static const uint32_t diff1targ = 0x0000ffff; + + +/* Used externally as confirmation of correct OCL code */ +int blakecoin_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); + blakecoinhash(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 blakecoin_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); + blakecoinhash(ohash, data); +} + +bool scanhash_blakecoin(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); + blakecoinhash(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; +} diff --git a/algorithm/blakecoin.h b/algorithm/blakecoin.h new file mode 100644 index 00000000..28b9b3d7 --- /dev/null +++ b/algorithm/blakecoin.h @@ -0,0 +1,9 @@ +#ifndef BLAKECOIN_H +#define BLAKECOIN_H + +#include "miner.h" + +extern int blakecoin_test(unsigned char *pdata, const unsigned char *ptarget, uint32_t nonce); +extern void blakecoin_regenhash(struct work *work); + +#endif /* BLAKECOIN_H */ \ No newline at end of file diff --git a/sph/blake.c b/sph/blake.c index 2ce7f71f..c7b7b14a 100644 --- a/sph/blake.c +++ b/sph/blake.c @@ -52,8 +52,6 @@ #pragma warning (disable: 4146) #endif -int blake256_rounds; - static const sph_u32 IV224[8] = { SPH_C32(0xC1059ED8), SPH_C32(0x367CD507), SPH_C32(0x3070DD17), SPH_C32(0xF70E5939), @@ -509,6 +507,55 @@ static const sph_u64 CB[16] = { #if SPH_COMPACT_BLAKE_32 +#define COMPRESS32r8 do { \ + sph_u32 M[16]; \ + sph_u32 V0, V1, V2, V3, V4, V5, V6, V7; \ + sph_u32 V8, V9, VA, VB, VC, VD, VE, VF; \ + unsigned r; \ + V0 = H0; \ + V1 = H1; \ + V2 = H2; \ + V3 = H3; \ + V4 = H4; \ + V5 = H5; \ + V6 = H6; \ + V7 = H7; \ + V8 = S0 ^ CS0; \ + V9 = S1 ^ CS1; \ + VA = S2 ^ CS2; \ + VB = S3 ^ CS3; \ + VC = T0 ^ CS4; \ + VD = T0 ^ CS5; \ + VE = T1 ^ CS6; \ + VF = T1 ^ CS7; \ + M[0x0] = sph_dec32be_aligned(buf + 0); \ + M[0x1] = sph_dec32be_aligned(buf + 4); \ + M[0x2] = sph_dec32be_aligned(buf + 8); \ + M[0x3] = sph_dec32be_aligned(buf + 12); \ + M[0x4] = sph_dec32be_aligned(buf + 16); \ + M[0x5] = sph_dec32be_aligned(buf + 20); \ + M[0x6] = sph_dec32be_aligned(buf + 24); \ + M[0x7] = sph_dec32be_aligned(buf + 28); \ + M[0x8] = sph_dec32be_aligned(buf + 32); \ + M[0x9] = sph_dec32be_aligned(buf + 36); \ + M[0xA] = sph_dec32be_aligned(buf + 40); \ + M[0xB] = sph_dec32be_aligned(buf + 44); \ + M[0xC] = sph_dec32be_aligned(buf + 48); \ + M[0xD] = sph_dec32be_aligned(buf + 52); \ + M[0xE] = sph_dec32be_aligned(buf + 56); \ + M[0xF] = sph_dec32be_aligned(buf + 60); \ + for (r = 0; r < 8; r ++) \ + ROUND_S(r); \ + H0 ^= S0 ^ V0 ^ V8; \ + H1 ^= S1 ^ V1 ^ V9; \ + H2 ^= S2 ^ V2 ^ VA; \ + H3 ^= S3 ^ V3 ^ VB; \ + H4 ^= S0 ^ V4 ^ VC; \ + H5 ^= S1 ^ V5 ^ VD; \ + H6 ^= S2 ^ V6 ^ VE; \ + H7 ^= S3 ^ V7 ^ VF; \ + } while (0) + #define COMPRESS32 do { \ sph_u32 M[16]; \ sph_u32 V0, V1, V2, V3, V4, V5, V6, V7; \ @@ -560,6 +607,61 @@ static const sph_u64 CB[16] = { #else +#define COMPRESS32r8 do { \ + sph_u32 M0, M1, M2, M3, M4, M5, M6, M7; \ + sph_u32 M8, M9, MA, MB, MC, MD, ME, MF; \ + sph_u32 V0, V1, V2, V3, V4, V5, V6, V7; \ + sph_u32 V8, V9, VA, VB, VC, VD, VE, VF; \ + V0 = H0; \ + V1 = H1; \ + V2 = H2; \ + V3 = H3; \ + V4 = H4; \ + V5 = H5; \ + V6 = H6; \ + V7 = H7; \ + V8 = S0 ^ CS0; \ + V9 = S1 ^ CS1; \ + VA = S2 ^ CS2; \ + VB = S3 ^ CS3; \ + VC = T0 ^ CS4; \ + VD = T0 ^ CS5; \ + VE = T1 ^ CS6; \ + VF = T1 ^ CS7; \ + M0 = sph_dec32be_aligned(buf + 0); \ + M1 = sph_dec32be_aligned(buf + 4); \ + M2 = sph_dec32be_aligned(buf + 8); \ + M3 = sph_dec32be_aligned(buf + 12); \ + M4 = sph_dec32be_aligned(buf + 16); \ + M5 = sph_dec32be_aligned(buf + 20); \ + M6 = sph_dec32be_aligned(buf + 24); \ + M7 = sph_dec32be_aligned(buf + 28); \ + M8 = sph_dec32be_aligned(buf + 32); \ + M9 = sph_dec32be_aligned(buf + 36); \ + MA = sph_dec32be_aligned(buf + 40); \ + MB = sph_dec32be_aligned(buf + 44); \ + MC = sph_dec32be_aligned(buf + 48); \ + MD = sph_dec32be_aligned(buf + 52); \ + ME = sph_dec32be_aligned(buf + 56); \ + MF = sph_dec32be_aligned(buf + 60); \ + ROUND_S(0); \ + ROUND_S(1); \ + ROUND_S(2); \ + ROUND_S(3); \ + ROUND_S(4); \ + ROUND_S(5); \ + ROUND_S(6); \ + ROUND_S(7); \ + H0 ^= S0 ^ V0 ^ V8; \ + H1 ^= S1 ^ V1 ^ V9; \ + H2 ^= S2 ^ V2 ^ VA; \ + H3 ^= S3 ^ V3 ^ VB; \ + H4 ^= S0 ^ V4 ^ VC; \ + H5 ^= S1 ^ V5 ^ VD; \ + H6 ^= S2 ^ V6 ^ VE; \ + H7 ^= S3 ^ V7 ^ VF; \ + } while (0) + #define COMPRESS32 do { \ sph_u32 M0, M1, M2, M3, M4, M5, M6, M7; \ sph_u32 M8, M9, MA, MB, MC, MD, ME, MF; \ @@ -605,14 +707,12 @@ static const sph_u64 CB[16] = { ROUND_S(5); \ ROUND_S(6); \ ROUND_S(7); \ - if (blake256_rounds > 8) { \ - ROUND_S(8); \ - ROUND_S(9); \ - ROUND_S(0); \ - ROUND_S(1); \ - ROUND_S(2); \ - ROUND_S(3); \ - } \ + ROUND_S(8); \ + ROUND_S(9); \ + ROUND_S(0); \ + ROUND_S(1); \ + ROUND_S(2); \ + ROUND_S(3); \ H0 ^= S0 ^ V0 ^ V8; \ H1 ^= S1 ^ V1 ^ V9; \ H2 ^= S2 ^ V2 ^ VA; \ @@ -835,6 +935,44 @@ blake32(sph_blake_small_context *sc, const void *data, size_t len) sc->ptr = ptr; } +static void +blake32r8(sph_blake_small_context *sc, const void *data, size_t len) +{ + unsigned char *buf; + size_t ptr; + DECL_STATE32 + + buf = sc->buf; + ptr = sc->ptr; + if (len < (sizeof sc->buf) - ptr) { + memcpy(buf + ptr, data, len); + ptr += len; + sc->ptr = ptr; + return; + } + + READ_STATE32(sc); + while (len > 0) { + size_t clen; + + clen = (sizeof sc->buf) - ptr; + if (clen > len) + clen = len; + memcpy(buf + ptr, data, clen); + ptr += clen; + data = (const unsigned char *)data + clen; + len -= clen; + if (ptr == sizeof sc->buf) { + if ((T0 = SPH_T32(T0 + 512)) < 512) + T1 = SPH_T32(T1 + 1); + COMPRESS32r8; + ptr = 0; + } + } + WRITE_STATE32(sc); + sc->ptr = ptr; +} + static void blake32_close(sph_blake_small_context *sc, unsigned ub, unsigned n, void *dst, size_t out_size_w32) @@ -888,6 +1026,59 @@ blake32_close(sph_blake_small_context *sc, sph_enc32be(out + (k << 2), sc->H[k]); } +static void +blake32r8_close(sph_blake_small_context *sc, + unsigned ub, unsigned n, void *dst, size_t out_size_w32) +{ + union { + unsigned char buf[64]; + sph_u32 dummy; + } u; + size_t ptr, k; + unsigned bit_len; + unsigned z; + sph_u32 th, tl; + unsigned char *out; + + ptr = sc->ptr; + bit_len = ((unsigned)ptr << 3) + n; + z = 0x80 >> n; + u.buf[ptr] = ((ub & -z) | z) & 0xFF; + tl = sc->T0 + bit_len; + th = sc->T1; + if (ptr == 0 && n == 0) { + sc->T0 = SPH_C32(0xFFFFFE00); + sc->T1 = SPH_C32(0xFFFFFFFF); + } else if (sc->T0 == 0) { + sc->T0 = SPH_C32(0xFFFFFE00) + bit_len; + sc->T1 = SPH_T32(sc->T1 - 1); + } else { + sc->T0 -= 512 - bit_len; + } + if (bit_len <= 446) { + memset(u.buf + ptr + 1, 0, 55 - ptr); + if (out_size_w32 == 8) + u.buf[55] |= 1; + sph_enc32be_aligned(u.buf + 56, th); + sph_enc32be_aligned(u.buf + 60, tl); + blake32r8(sc, u.buf + ptr, 64 - ptr); + } else { + memset(u.buf + ptr + 1, 0, 63 - ptr); + blake32r8(sc, u.buf + ptr, 64 - ptr); + sc->T0 = SPH_C32(0xFFFFFE00); + sc->T1 = SPH_C32(0xFFFFFFFF); + memset(u.buf, 0, 56); + if (out_size_w32 == 8) + u.buf[55] = 1; + sph_enc32be_aligned(u.buf + 56, th); + sph_enc32be_aligned(u.buf + 60, tl); + blake32r8(sc, u.buf, 64); + } + out = (unsigned char *)dst; + for (k = 0; k < out_size_w32; k ++) + sph_enc32be(out + (k << 2), sc->H[k]); +} + #if SPH_64 static const sph_u64 salt_zero_big[4] = { 0, 0, 0, 0 }; @@ -1038,6 +1229,13 @@ sph_blake256(void *cc, const void *data, size_t len) blake32((sph_blake_small_context *)cc, data, len); } +/* see sph_blake.h */ +void +sph_blake256r8(void *cc, const void *data, size_t len) +{ + blake32r8((sph_blake_small_context *)cc, data, len); +} + /* see sph_blake.h */ void sph_blake256_close(void *cc, void *dst) @@ -1045,6 +1243,13 @@ sph_blake256_close(void *cc, void *dst) sph_blake256_addbits_and_close(cc, 0, 0, dst); } +/* see sph_blake.h */ +void +sph_blake256r8_close(void *cc, void *dst) +{ + sph_blake256r8_addbits_and_close(cc, 0, 0, dst); +} + /* see sph_blake.h */ void sph_blake256_addbits_and_close(void *cc, unsigned ub, unsigned n, void *dst) @@ -1053,6 +1258,14 @@ sph_blake256_addbits_and_close(void *cc, unsigned ub, unsigned n, void *dst) sph_blake256_init(cc); } +/* see sph_blake.h */ +void +sph_blake256r8_addbits_and_close(void *cc, unsigned ub, unsigned n, void *dst) +{ + blake32r8_close((sph_blake_small_context *)cc, ub, n, dst, 8); + sph_blake256_init(cc); +} + #if SPH_64 /* see sph_blake.h */ diff --git a/sph/sph_blake.h b/sph/sph_blake.h index b8803a7b..8dea65ca 100644 --- a/sph/sph_blake.h +++ b/sph/sph_blake.h @@ -132,9 +132,6 @@ typedef sph_blake_big_context sph_blake512_context; #endif -// Number of rounds for COMPRESS32 -extern int blake256_rounds; - /** * Initialize a BLAKE-224 context. This process performs no memory allocation. * @@ -197,6 +194,7 @@ void sph_blake256_init(void *cc); * @param len the input data length (in bytes) */ void sph_blake256(void *cc, const void *data, size_t len); +void sph_blake256r8(void *cc, const void *data, size_t len); /** * Terminate the current BLAKE-256 computation and output the result into @@ -208,6 +206,7 @@ void sph_blake256(void *cc, const void *data, size_t len); * @param dst the destination buffer */ void sph_blake256_close(void *cc, void *dst); +void sph_blake256r8_close(void *cc, void *dst); /** * Add a few additional bits (0 to 7) to the current computation, then @@ -224,6 +223,8 @@ void sph_blake256_close(void *cc, void *dst); */ void sph_blake256_addbits_and_close( void *cc, unsigned ub, unsigned n, void *dst); +void sph_blake256r8_addbits_and_close( + void *cc, unsigned ub, unsigned n, void *dst); #if SPH_64