mirror of
https://github.com/skot/ESP-Miner.git
synced 2025-04-10 04:49:16 +02:00
mining: swap endianess of prevblockhash when computing midstate
This commit is contained in:
parent
40cb7fac61
commit
bad72d638a
@ -4,13 +4,13 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
int hex2char(uint8_t x, char *c);
|
||||
int hex2char(uint8_t x, char * c);
|
||||
|
||||
size_t bin2hex(const uint8_t *buf, size_t buflen, char *hex, size_t hexlen);
|
||||
size_t bin2hex(const uint8_t * buf, size_t buflen, char * hex, size_t hexlen);
|
||||
|
||||
uint8_t hex2val(char c);
|
||||
|
||||
size_t hex2bin(const char *hex, uint8_t *bin, size_t bin_len);
|
||||
size_t hex2bin(const char * hex, uint8_t * bin, size_t bin_len);
|
||||
|
||||
void print_hex(const uint8_t * b, size_t len,
|
||||
const size_t in_line, const char * prefix);
|
||||
@ -21,4 +21,6 @@ uint8_t * double_sha256_bin(const uint8_t * data, const size_t data_len);
|
||||
|
||||
void single_sha256_bin(const uint8_t * data, const size_t data_len, uint8_t * dest);
|
||||
|
||||
void swap_endian_words(const char * hex, uint8_t * output);
|
||||
|
||||
#endif // STRATUM_UTILS_H
|
@ -59,7 +59,7 @@ bm_job construct_bm_job(uint32_t version, const char * prev_block_hash, const ch
|
||||
|
||||
uint8_t midstate_data[64];
|
||||
memcpy(midstate_data, &version, 4);
|
||||
hex2bin(prev_block_hash, midstate_data + 4, 32);
|
||||
swap_endian_words(prev_block_hash, midstate_data + 4);
|
||||
memcpy(midstate_data + 36, merkle_root_bin, 28);
|
||||
single_sha256_bin(midstate_data, 64, new_job.midstate);
|
||||
|
||||
|
@ -50,6 +50,6 @@ TEST_CASE("Validate bm job construction", "[mining]")
|
||||
TEST_ASSERT_EQUAL_UINT32(job.merkle_root_end, 0x93a899f0);
|
||||
|
||||
uint8_t expected_midstate_bin[32];
|
||||
hex2bin("ce9aa3d9ff4cf2c5897fe7caaacf56bfd7fc7ac01d89f8d88f6501d4597344b9", expected_midstate_bin, 32);
|
||||
hex2bin("ac9643f83aa1e1d8de56d0f59a41ea3b3eac45b62ae043bd933a7413c0a1fe58", expected_midstate_bin, 32);
|
||||
TEST_ASSERT_EQUAL_UINT8_ARRAY(expected_midstate_bin, job.midstate, 32);
|
||||
}
|
@ -1,7 +1,19 @@
|
||||
import hashlib
|
||||
import binascii
|
||||
|
||||
version = b'\x04\x00\x00\x20' # little-endian encoding of version
|
||||
prev_block_hash = bytes.fromhex('ef4b9a48c7986466de4adc002f7337a6e121bc43000376ea0000000000000000')
|
||||
|
||||
def swap_endian_words(hex_words):
|
||||
'''Swaps the endianness of a hexadecimal string of words and returns as another hexadecimal string.'''
|
||||
|
||||
message = binascii.unhexlify(hex_words)
|
||||
if len(message) % 4 != 0:
|
||||
raise ValueError('Must be 4-byte word aligned')
|
||||
swapped_bytes = [message[4 * i: 4 * i + 4][::-1] for i in range(0, len(message) // 4)]
|
||||
return ''.join([binascii.hexlify(word).decode('utf-8') for word in swapped_bytes])
|
||||
|
||||
|
||||
version = b'\x04\x00\x00\x20' # little-endian encoding of version 1
|
||||
prev_block_hash = bytes.fromhex(swap_endian_words("ef4b9a48c7986466de4adc002f7337a6e121bc43000376ea0000000000000000"))
|
||||
merkle_root = bytes.fromhex('adbcbc21e20388422198a55957aedfa0e61be0b8f2b87d7c08510bb9f099a893')
|
||||
timestamp = b'\x64\x49\x55\x22' # little-endian encoding of Unix timestamp 1683385928
|
||||
difficulty_target = b'\x39\xc7\x05\x17' # little-endian encoding of difficulty
|
||||
|
@ -127,3 +127,21 @@ void single_sha256_bin( const uint8_t * data, const size_t data_len, uint8_t * d
|
||||
{
|
||||
mbedtls_sha256(data, data_len, dest, 0);
|
||||
}
|
||||
|
||||
void swap_endian_words(const char * hex_words, uint8_t * output) {
|
||||
size_t hex_length = strlen(hex_words);
|
||||
if (hex_length % 8 != 0) {
|
||||
fprintf(stderr, "Must be 4-byte word aligned\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
size_t binary_length = hex_length / 2;
|
||||
|
||||
for (size_t i = 0; i < binary_length; i += 4) {
|
||||
for (int j = 0; j < 4; j++) {
|
||||
unsigned int byte_val;
|
||||
sscanf(hex_words + (i + j) * 2, "%2x", &byte_val);
|
||||
output[i + (3 - j)] = byte_val;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user