flip the byte order of the midstate

This commit is contained in:
Skot Croshere 2023-05-19 22:47:10 -04:00 committed by johnny9
parent 4cf368864e
commit 3e3ba6d98e
2 changed files with 45 additions and 16 deletions

View File

@ -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;

View File

@ -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);
}