/** * PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, c, buf, dkLen): * Compute PBKDF2(passwd, salt, c, dkLen) using HMAC-SHA256 as the PRF, and * write the output to buf. The value dkLen must be at most 32 * (2^32 - 1). */ static inline void PBKDF2_SHA256_80_128(const uint32_t * passwd, uint32_t * buf) { SHA256_CTX PShictx, PShoctx; uint32_t tstate[8]; uint32_t ihash[8]; uint32_t i; uint32_t pad[16]; static const uint32_t innerpad[11] = {0x00000080, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xa0040000}; /* If Klen > 64, the key is really SHA256(K). */ SHA256_InitState(tstate); SHA256_Transform(tstate, passwd, 1); memcpy(pad, passwd+16, 16); memcpy(pad+4, passwdpad, 48); SHA256_Transform(tstate, pad, 1); memcpy(ihash, tstate, 32); SHA256_InitState(PShictx.state); for (i = 0; i < 8; i++) pad[i] = ihash[i] ^ 0x36363636; for (; i < 16; i++) pad[i] = 0x36363636; SHA256_Transform(PShictx.state, pad, 0); SHA256_Transform(PShictx.state, passwd, 1); be32enc_vect(PShictx.buf, passwd+16, 4); be32enc_vect(PShictx.buf+5, innerpad, 11); SHA256_InitState(PShoctx.state); for (i = 0; i < 8; i++) pad[i] = ihash[i] ^ 0x5c5c5c5c; for (; i < 16; i++) pad[i] = 0x5c5c5c5c; SHA256_Transform(PShoctx.state, pad, 0); memcpy(PShoctx.buf+8, outerpad, 32); /* Iterate through the blocks. */ for (i = 0; i < 4; i++) { uint32_t istate[8]; uint32_t ostate[8]; memcpy(istate, PShictx.state, 32); PShictx.buf[4] = i + 1; SHA256_Transform(istate, PShictx.buf, 0); memcpy(PShoctx.buf, istate, 32); memcpy(ostate, PShoctx.state, 32); SHA256_Transform(ostate, PShoctx.buf, 0); be32enc_vect(buf+i*8, ostate, 8); } }
void blake256_regenhash(struct work *work) { uint32_t data[45]; uint32_t *nonce = (uint32_t *)(work->data + 140); uint32_t *ohash = (uint32_t *)(work->hash); be32enc_vect(data, (const uint32_t *)work->data, 45); data[35] = *nonce; //htobe32(*nonce); applog(LOG_DEBUG, "timestamp %x", data[34]); applog(LOG_DEBUG, "lucky nonce %x", data[35]); 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]); applog(LOG_DEBUG, "Dat1: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x", data[20], data[21], data[22], data[23], data[24], data[25], data[26], data[27], data[28], data[29], data[30], data[31], data[32], data[33], data[34], data[35], data[36], data[37], data[38], data[39]); applog(LOG_DEBUG, "Dat2: %x %x %x %x %x\n", data[40], data[41], data[42], data[43], data[44]); uint32_t swap[45]; flip180(swap, data); int i; for (i=0; i<45; ++i) { data[i] = swap[i]; } sph_blake256_context ctx_blake; sph_blake256_init(&ctx_blake); sph_blake256 (&ctx_blake, (unsigned char *)data, 180); sph_blake256_close(&ctx_blake, (unsigned char *)ohash); applog(LOG_DEBUG, "Hash produced: %x %x %x %x %x %x %x %x", ohash[0], ohash[1], ohash[2], ohash[3], ohash[4], ohash[5], ohash[6], ohash[7]); uint32_t *o = ohash; }
/* Used externally as confirmation of correct OCL code */ 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] = 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; static unsigned char pblank[1]; 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]); tmp_hash7 = ohash[7]; applog(LOG_DEBUG, "Nonce %x harget %08lx diff1 %08lx hash %08lx", nonce, (long unsigned int)Htarg, (long unsigned int)diff1targ_blake256, (long unsigned int)tmp_hash7); if (tmp_hash7 > diff1targ_blake256) return -1; if (tmp_hash7 > Htarg) return 0; return 1; }
/* Used externally as confirmation of correct OCL code */ int fresh_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]; //char *scratchbuf; be32enc_vect(data, (const uint32_t *)pdata, 19); data[19] = htobe32(nonce); //scratchbuf = alloca(SCRATCHBUF_SIZE); freshHash(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 advsha3_regenhash(struct work *work) { uint32_t data[22]; uint32_t *nonce = (uint32_t *)(work->data + 76); uint32_t *ohash = (uint32_t *)(work->hash); be32enc_vect(data, (const uint32_t *)work->data, 22); data[19] = htobe32(*nonce); advsha3_hash(ohash, data); }
void sibcoin_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); xhash(ohash, data); }
void myriadcoin_groestl_regenhash(struct work *work) { uint32_t data[20]; //char *scratchbuf; 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); mghash(ohash, data); }
void X11_RegenHash(struct work *work) { uint32_t data[20]; char *scratchbuf; 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); X11_Hash(ohash, data); }
void scrypt_outputhash(struct work *work) { uint32_t data[20]; char *scratchbuf; uint32_t *nonce = (uint32_t *)(work->data + 76); be32enc_vect(data, (const uint32_t *)work->data, 19); data[19] = htobe32(*nonce); scratchbuf = alloca(131584); work->outputhash = scrypt_1024_1_1_256_sp(data, scratchbuf); }
void yescrypt_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); yescrypt_hash((unsigned char*)data, (unsigned char*)ohash); }
void xcoin_regenhash(struct work *work) { uint32_t data[20]; char *scratchbuf; 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); xhash(ohash, data); flip32(ohash, ohash); }
void scrypt_regenhash(struct work *work) { uint32_t data[20]; char *scratchbuf; 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); scratchbuf = (char *)alloca(SCRATCHBUF_SIZE); scrypt_1024_1_1_256_sp(data, scratchbuf, ohash); flip32(ohash, ohash); }
/* Used externally as confirmation of correct OCL code */ bool scrypt_test(unsigned char *pdata, const unsigned char *ptarget, uint32_t nonce) { uint32_t tmp_hash7, Htarg = ((const uint32_t *)ptarget)[7]; char *scratchbuf; uint32_t data[20]; be32enc_vect(data, (const uint32_t *)pdata, 19); data[19] = byteswap(nonce); scratchbuf = (char*)alloca(131584); tmp_hash7 = scrypt_1024_1_1_256_sp(data, scratchbuf); return (tmp_hash7 <= Htarg); }
/* * SHA-256 finalization. Pads the input data, exports the hash value, * and clears the context state. */ void SHA256_Final(unsigned char digest[32], SHA256_CTX * ctx) { /* Add padding */ SHA256_Pad(ctx); /* Write the hash */ be32enc_vect(digest, ctx->state, 32); /* Clear the context state */ memset((void *)ctx, 0, sizeof(*ctx)); }
void blakecoin_midstate(struct work *work) { sph_blake256_context ctx_blake; uint32_t data[16]; be32enc_vect(data, (const uint32_t *)work->data, 19); sph_blake256_init(&ctx_blake); sph_blake256r8 (&ctx_blake, (unsigned char *)data, 64); memcpy(work->midstate, ctx_blake.H, 32); endian_flip32(work->midstate, work->midstate); char *strdata, *strmidstate; strdata = bin2hex(work->data, 80); strmidstate = bin2hex(work->midstate, 32); applog(LOG_DEBUG, "data %s midstate %s", strdata, strmidstate); }
bool scanhash_scrypt(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); char *scratchbuf; 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); scratchbuf = (char *)malloc(SCRATCHBUF_SIZE); if (unlikely(!scratchbuf)) { applog(LOG_ERR, "Failed to malloc scratchbuf in scanhash_scrypt"); return ret; } while(1) { uint32_t ostate[8]; *nonce = ++n; data[19] = htobe32(n); scrypt_1024_1_1_256_sp(data, scratchbuf, ostate); tmp_hash7 = be32toh(ostate[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; } } free(scratchbuf);; return ret; }
bool scanhash_fresh(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); char *scratchbuf; 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); freshHash(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; }
/* Used externally as confirmation of correct OCL code */ int sibcoin_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); xhash(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 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); 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]); 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); uint32_t *o = ohash; }
void scrypt_regenhash(struct work *work) { uint32_t data[20]; char *scratchbuf; uint32_t timestamp; cl_uint nfactor = 10; // scrypt default uint32_t *nonce = (uint32_t *)(work->data + 76); uint32_t *ohash = (uint32_t *)(work->hash); //if (opt_scrypt_vert) { timestamp = bswap_32(*((uint32_t *)(work->data + 17*4))); nfactor = alt_GetNfactor(timestamp) + 1; //} be32enc_vect(data, (const uint32_t *)work->data, 19); data[19] = htobe32(*nonce); //scratchbuf = alloca(SCRATCHBUF_SIZE); scratchbuf = alloca((1 << nfactor) * 128 + 512); scrypt_1024_1_1_256_sp(data, scratchbuf, ohash, (1 << nfactor)); flip32(ohash, ohash); }
void scrypt_regenhash(struct work *work) { uint32_t data[20]; char *scratchbuf; uint32_t timestamp; cl_uint nfactor = 10; // scrypt default uint32_t *nonce = (uint32_t *)(work->data + 76); uint32_t *ohash = (uint32_t *)(work->hash); if (opt_nscrypt) { timestamp = bswap_32(*((uint32_t *)(work->data + 17*4))); // find a way to implement r/m learning -- simulate it for the miners // nfactor = vert_GetNfactor(timestamp) + 1; nfactor = hin_getNFactor(work->work_block,work->work_difficulty) + 1; } be32enc_vect(data, (const uint32_t *)work->data, 19); data[19] = htobe32(*nonce); //scratchbuf = alloca(SCRATCHBUF_SIZE); scratchbuf = alloca((1 << nfactor) * 128 + 512); scrypt_1024_1_1_256_sp(data, scratchbuf, ohash, (1 << nfactor)); flip32(ohash, ohash); }
static bool epiphany_scrypt(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 i; e_epiphany_t *dev = &thr->cgpu->epiphany_dev; e_mem_t *emem = &thr->cgpu->epiphany_emem; unsigned rows = thr->cgpu->epiphany_rows; unsigned cols = thr->cgpu->epiphany_cols; uint8_t *core_working = calloc(rows*cols, sizeof(uint8_t)); uint32_t *core_nonce = calloc(rows*cols, sizeof(uint32_t)); uint32_t cores_working = 0; uint32_t *nonce = (uint32_t *)(pdata + 76); uint32_t ostate; uint32_t data[20]; const uint8_t core_go = 1; uint32_t tmp_hash7; uint32_t Htarg = ((const uint32_t *)ptarget)[7]; bool ret = false; be32enc_vect(data, (const uint32_t *)pdata, 19); if (e_start_group(dev) == E_ERR) { applog(LOG_ERR, "Error: Could not start Epiphany cores."); return false; } off_t offdata = offsetof(shared_buf_t, data); off_t offostate = offsetof(shared_buf_t, ostate); off_t offcorego = offsetof(shared_buf_t, go); off_t offcoreend = offsetof(shared_buf_t, working); off_t offcore; #ifdef EPIPHANY_DEBUG #define SCRATCHBUF_SIZE (131584) uint32_t ostate2[8]; char *scratchbuf = malloc(SCRATCHBUF_SIZE); uint32_t *ostatearm = calloc(rows*cols, sizeof(uint32_t)); #endif i = 0; while(1) { offcore = i * sizeof(shared_buf_t); if ((!core_working[i]) && (n < max_nonce)) { *nonce = ++n; data[19] = n; core_working[i] = 1; cores_working++; core_nonce[i] = n; #ifdef EPIPHANY_DEBUG scrypt_1024_1_1_256_sp(data, scratchbuf, ostate2); ostatearm[i] = ostate2[7]; #endif e_write(emem, 0, 0, offcore + offdata, (void *) data, sizeof(data)); e_write(emem, 0, 0, offcore + offcoreend, (void *) &core_working[i], sizeof(core_working[i])); e_write(emem, 0, 0, offcore + offcorego, (void *) &core_go, sizeof(core_go)); } e_read(emem, 0, 0, offcore + offcoreend, (void *) &(core_working[i]), sizeof(core_working[i])); if (!core_working[i]) { e_read(emem, 0, 0, offcore + offostate, (void *) &(ostate), sizeof(ostate)); #ifdef EPIPHANY_DEBUG applog(LOG_DEBUG, "CORE %u - EPI HASH %u - ARM HASH %u - %s", i, ostate, ostatearm[i], ((ostate==ostatearm[i])? "OK":"FAIL")); #endif tmp_hash7 = be32toh(ostate); cores_working--; if (unlikely(tmp_hash7 <= Htarg)) { ((uint32_t *)pdata)[19] = htobe32(core_nonce[i]); *last_nonce = core_nonce[i]; #ifdef EPIPHANY_DEBUG free(scratchbuf); #endif return true; } } if (unlikely(((n >= max_nonce) && !cores_working) || thr->work_restart)) { *last_nonce = n; #ifdef EPIPHANY_DEBUG free(scratchbuf); #endif return false; } i++; i %= rows * cols; } #ifdef EPIPHANY_DEBUG free(scratchbuf); #endif return false; }