From 3e3ba6d98e979bb8610a7cfa907e6fac2ce27cd3 Mon Sep 17 00:00:00 2001 From: Skot Croshere Date: Fri, 19 May 2023 22:47:10 -0400 Subject: [PATCH] flip the byte order of the midstate --- components/stratum/mining.c | 25 ++++++++++--------------- components/stratum/utils.c | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 16 deletions(-) diff --git a/components/stratum/mining.c b/components/stratum/mining.c index 3ab5449a..730c7a05 100644 --- a/components/stratum/mining.c +++ b/components/stratum/mining.c @@ -60,23 +60,18 @@ bm_job construct_bm_job(uint32_t version, const char * prev_block_hash, const ch uint8_t midstate_data[64]; //print the header - printf("header: %08x%s%s%08x%08x000000000000008000000000000000000000000000000000000000000000000000000000\n", version, prev_block_hash, merkle_root, ntime, target); + //printf("header: %08x%s%s%08x%08x000000000000008000000000000000000000000000000000000000000000000000000000\n", version, prev_block_hash, merkle_root, ntime, target); memcpy(midstate_data, &version, 4); //copy version - // midstate_data[3] = version & 0xff; - // midstate_data[2] = (version >> 8) & 0xff; - // midstate_data[1] = (version >> 16) & 0xff; - // midstate_data[0] = (version >> 24) & 0xff; - swap_endian_words(prev_block_hash, midstate_data + 4); - //memcpy(midstate_data + 4, prev_block_bin, 32); - swap_endian_words(merkle_root, midstate_data + 36); - //memcpy(midstate_data + 36, merkle_root_bin, 28); - printf("midstate_data: "); - prettyHex(midstate_data, 64); - printf("\n"); - //single_sha256_bin(midstate_data, 64, new_job.midstate); - midstate_sha256_bin(midstate_data, 64, new_job.midstate); - reverse_bytes(new_job.midstate, 32); + swap_endian_words(prev_block_hash, midstate_data + 4); //copy prev_block_hash + swap_endian_words(merkle_root, midstate_data + 36); //copy merkle_root + // printf("midstate_data: "); + // prettyHex(midstate_data, 64); + // printf("\n"); + + midstate_sha256_bin(midstate_data, 64, new_job.midstate); //make the midstate hash + reverse_bytes(new_job.midstate, 32); //reverse the midstate bytes for the BM job packet + memcpy(&new_job.merkle_root_end, merkle_root_bin + 28, 4); return new_job; diff --git a/components/stratum/utils.c b/components/stratum/utils.c index 2fd8e0ad..c4b516d1 100644 --- a/components/stratum/utils.c +++ b/components/stratum/utils.c @@ -5,6 +5,39 @@ #include "mbedtls/sha256.h" +#ifndef bswap_16 +#define bswap_16(a) ((((uint16_t) (a) << 8) & 0xff00) | (((uint16_t) (a) >> 8) & 0xff)) +#endif + +#ifndef bswap_32 +#define bswap_32(a) ((((uint32_t) (a) << 24) & 0xff000000) | \ + (((uint32_t) (a) << 8) & 0xff0000) | \ + (((uint32_t) (a) >> 8) & 0xff00) | \ + (((uint32_t) (a) >> 24) & 0xff)) +#endif + +uint32_t swab32(uint32_t v) { + return bswap_32(v); +} + +void flip64bytes(void *dest_p, const void *src_p) { + uint32_t *dest = dest_p; + const uint32_t *src = src_p; + int i; + + for (i = 0; i < 16; i++) + dest[i] = swab32(src[i]); +} + +void flip32bytes(void *dest_p, const void *src_p) { + uint32_t *dest = dest_p; + const uint32_t *src = src_p; + int i; + + for (i = 0; i < 8; i++) + dest[i] = swab32(src[i]); +} + int hex2char(uint8_t x, char *c) { @@ -151,7 +184,8 @@ void midstate_sha256_bin( const uint8_t * data, const size_t data_len, uint8_t * mbedtls_sha256_starts_ret(&midstate, 0); mbedtls_sha256_update_ret(&midstate, data, 64); - memcpy(dest, midstate.state, 32); + //memcpy(dest, midstate.state, 32); + flip32bytes(dest, midstate.state); }