From de237e1d3449e2282f5d70548ff058f78ee77aa1 Mon Sep 17 00:00:00 2001 From: Skot Croshere Date: Thu, 25 May 2023 21:49:12 -0400 Subject: [PATCH] added nonce checking fixes from @johnny9 --- components/stratum/include/utils.h | 2 ++ components/stratum/mining.c | 47 +++++++-------------------- components/stratum/test/test_mining.c | 27 +++++++++++---- components/stratum/utils.c | 42 ++++++++++++++++-------- 4 files changed, 63 insertions(+), 55 deletions(-) diff --git a/components/stratum/include/utils.h b/components/stratum/include/utils.h index cd5c9616..b21c41ae 100644 --- a/components/stratum/include/utils.h +++ b/components/stratum/include/utils.h @@ -36,4 +36,6 @@ void reverse_bytes(uint8_t * data, size_t len); double le256todouble(const void *target); +void prettyHex(unsigned char * buf, int len); + #endif // STRATUM_UTILS_H \ No newline at end of file diff --git a/components/stratum/mining.c b/components/stratum/mining.c index 03c5bb4d..97145577 100644 --- a/components/stratum/mining.c +++ b/components/stratum/mining.c @@ -105,59 +105,36 @@ static const double truediffone = 2695953529101130949315647634472399133601089873 double test_nonce_value(bm_job * job, uint32_t nonce) { double d64, s64, ds; unsigned char header[80]; - uint32_t flipped_nonce; + uint8_t flipped_prev_block_hash[32]; + uint8_t flipped_merkle_root[32]; + + flip_bytes(flipped_prev_block_hash, job->prev_block_hash, 32); + flip_bytes(flipped_merkle_root, job->merkle_root, 32); //copy data from job to header memcpy(header, &job->version, 4); - memcpy(header + 4, job->prev_block_hash, 32); - memcpy(header + 36, job->merkle_root, 32); + memcpy(header + 4, flipped_prev_block_hash, 32); + memcpy(header + 36, flipped_merkle_root, 32); memcpy(header + 68, &job->ntime, 4); memcpy(header + 72, &job->target, 4); - flipped_nonce = bswap32(nonce); - memcpy(header + 76, &flipped_nonce, 4); + memcpy(header + 76, &nonce, 4); - //unsigned char swapped_header[80]; - // unsigned char hash_buffer[32]; + unsigned char hash_buffer[32]; unsigned char hash_result[32]; - //endian flip 80 bytes. - //version (4 bytes), prevhash (32 bytes), merkle (32 bytes), time (4 bytes), bits (4 bytes), nonce (4 bytes) = 80 bytes - // flip_bytes((uint32_t *)swapped_header, header, 80); - printf("data32: "); prettyHex(header, 80); - mbedtls_sha256_context midstate, ctx; - - //Calcular midstate - mbedtls_sha256_init(&midstate); - mbedtls_sha256_starts_ret(&midstate, 0); - mbedtls_sha256_update_ret(&midstate, header, 64); - - unsigned char *header64 = header + 64; - - mbedtls_sha256_clone(&ctx, &midstate); //Clonamos el contexto anterior para continuar el SHA desde allĂ­ - mbedtls_sha256_update_ret(&ctx, header64, 16); - mbedtls_sha256_finish_ret(&ctx, hash_result); - - // Segundo SHA-256 - mbedtls_sha256_starts_ret(&ctx, 0); - mbedtls_sha256_update_ret(&ctx, hash_result, 32); - mbedtls_sha256_finish_ret(&ctx, hash_result); + //double hash the header + mbedtls_sha256(header, 80, hash_buffer, 0); + mbedtls_sha256(hash_buffer, 32, hash_result, 0); printf("hash: "); prettyHex(hash_result, 32); - // //check that the last 4 bytes are 0 - // if (*hash_32 != 0) { - // return 0.0; - // } d64 = truediffone; s64 = le256todouble(hash_result); ds = d64 / s64; - mbedtls_sha256_free(&ctx); - mbedtls_sha256_free(&midstate); - return ds; } \ No newline at end of file diff --git a/components/stratum/test/test_mining.c b/components/stratum/test/test_mining.c index cc3d3609..27ab268a 100644 --- a/components/stratum/test/test_mining.c +++ b/components/stratum/test/test_mining.c @@ -60,13 +60,13 @@ TEST_CASE("Validate another merkle root calculation", "[mining]") // Values calculated from esp-miner/components/stratum/test/verifiers/bm1397.py TEST_CASE("Validate bm job construction", "[mining]") { + mining_notify notify_message; + notify_message.prev_block_hash = "bf44fd3513dc7b837d60e5c628b572b448d204a8000007490000000000000000"; + notify_message.version = 0x20000004; + notify_message.target = 0x1705dd01; + notify_message.ntime = 0x64658bd8; const char * merkle_root = "cd1be82132ef0d12053dcece1fa0247fcfdb61d4dbd3eb32ea9ef9b4c604a846"; - const char * prev_block_hash = "bf44fd3513dc7b837d60e5c628b572b448d204a8000007490000000000000000"; - uint32_t version = 0x20000004; - uint32_t target = 0x1705dd01; - uint32_t ntime = 0x64658bd8; - bm_job job = construct_bm_job(version, prev_block_hash, merkle_root, ntime, target); - TEST_ASSERT_EQUAL_UINT32(job.merkle_root_end, 0x46a804c6); + bm_job job = construct_bm_job(¬ify_message, merkle_root); uint8_t expected_midstate_bin[32]; hex2bin("91DFEA528A9F73683D0D495DD6DD7415E1CA21CB411759E3E05D7D5FF285314D", expected_midstate_bin, 32); @@ -96,4 +96,19 @@ TEST_CASE("Test extranonce 2 generation", "[mining extranonce2]") char * fifth = extranonce_2_generate(UINT_MAX / 2, 6); TEST_ASSERT_EQUAL_STRING("ffffff7f0000", fifth); free(fifth); +} + +TEST_CASE("Test nonce diff checking", "[mining test_nonce]") +{ + mining_notify notify_message; + notify_message.prev_block_hash = "d02b10fc0d4711eae1a805af50a8a83312a2215e00017f2b0000000000000000"; + notify_message.version = 0x20000004; + notify_message.target = 0x1705ae3a; + notify_message.ntime = 0x646ff1a9; + const char * merkle_root = "C459036D054643519C5A2AC50B3474E0632C7CE4F93107843FDBF1EDD9CDB126646FF1A9"; + bm_job job = construct_bm_job(¬ify_message, merkle_root); + + uint32_t nonce = 0x276E8947; + double diff = test_nonce_value(&job, nonce); + TEST_ASSERT_EQUAL_INT(18, (int) diff); } \ No newline at end of file diff --git a/components/stratum/utils.c b/components/stratum/utils.c index 34942893..5e471386 100644 --- a/components/stratum/utils.c +++ b/components/stratum/utils.c @@ -214,23 +214,37 @@ void reverse_bytes(uint8_t * data, size_t len) { } } +//static const double truediffone = 26959535291011309493156476344723991336010898738574164086137773096960.0; + static const double bits192 = 6277101735386680763835789423207666416102355444464034512896.0; + static const double bits128 = 340282366920938463463374607431768211456.0; + static const double bits64 = 18446744073709551616.0; + /* Converts a little endian 256 bit value to a double */ -double le256todouble(const void *target) -{ - uint64_t *data64; - double dcut64; + double le256todouble(const void *target) + { + uint64_t *data64; + double dcut64; - data64 = (uint64_t *)(target + 24); - dcut64 = bswap64(*data64) * 6277101735386680763835789423207666416102355444464034512896.0; + data64 = (uint64_t *)(target + 24); + dcut64 = *data64 * bits192; - data64 = (uint64_t *)(target + 16); - dcut64 += bswap64(*data64) * 340282366920938463463374607431768211456.0; + data64 = (uint64_t *)(target + 16); + dcut64 += *data64 * bits128; - data64 = (uint64_t *)(target + 8); - dcut64 += bswap64(*data64) * 18446744073709551616.0; + data64 = (uint64_t *)(target + 8); + dcut64 += *data64 * bits64; - data64 = (uint64_t *)(target); - dcut64 += bswap64(*data64); + data64 = (uint64_t *)(target); + dcut64 += *data64; - return dcut64; -} + return dcut64; + } + + void prettyHex(unsigned char * buf, int len) { + int i; + printf("["); + for (i = 0; i < len-1; i++) { + printf("%02X ", buf[i]); + } + printf("%02X]\n", buf[len-1]); + }