BitMaker-hub / NerdMiner_v2

Improved version of first ESP32 NerdMiner
Other
1.37k stars 250 forks source link

Problem to test 'checkValid' function #291

Open AchcarLucas opened 6 months ago

AchcarLucas commented 6 months ago

Well, I was trying to validate the checkValid function, so I rewrote it in a C++ file and tried to pass the hash and target to try to do the test, however, the 'checkValid' function is not returning true '1', I tried several different hashes and targets but unsuccessfully. I don't know if I wrote the correct algorithm in my test example or if the function really isn't working.

I will leave the code used to do the test here

https://onlinegdb.com/lypx0_9Tx

test-check-hash.txt

/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <iostream>

using namespace std;

uint8_t hex(char ch) {
    uint8_t r = (ch > 57) ? (ch - 55) : (ch - 48);
    return r & 0x0F;
}

int to_byte_array(unsigned char *in, size_t in_size, uint8_t *out) {
    int count = 0;
    if (in_size % 2) {
        while (*in && out) {
            *out = hex(*in++);
            if (!*in)
                return count;
            *out = (*out << 4) | hex(*in++);
            *out++;
            count++;
        }
        return count;
    } else {
        while (*in && out) {
            *out++ = (hex(*in++) << 4) | hex(*in++);
            count++;
        }
        return count;
    }
}

void reverse_bytes(uint8_t * data, size_t len) {
    for (int i = 0; i < len / 2; ++i) {
        uint8_t temp = data[i];
        data[i] = data[len - 1 - i];
        data[len - 1 - i] = temp;
    }
}

bool checkValid(unsigned char* hash, unsigned char* target) {
      bool valid = true;
      unsigned char diff_target[32];
      memcpy(diff_target, &target, 32);
      //convert target to little endian for comparison
      reverse_bytes(diff_target, 32);

      for(uint8_t i=31; i>=0; i--) {
        if(hash[i] > diff_target[i]) {
          valid = false;
          break;
        }
      }

      return valid;
}

int main()
{
    unsigned char hash[] =   "0000000010000000000000000000d6b66fc0000000000000000000100000000";
    unsigned char target[] = "00010000000000000003d8690000000000000000000000000000000000001000";

    uint8_t bytearray_target[32];
    uint8_t bytearray_hash[32];

    {
        size_t size_target = to_byte_array(target, 32, bytearray_target);

        for (size_t j = 0; j < 8; j++) {
          bytearray_target[j] ^= bytearray_target[size_target - 1 - j];
          bytearray_target[size_target - 1 - j] ^= bytearray_target[j];
          bytearray_target[j] ^= bytearray_target[size_target - 1 - j];
        }
    }

    {
        size_t size_hash = to_byte_array(hash, 32, bytearray_hash);

        for (size_t j = 0; j < 8; j++) {
          bytearray_hash[j] ^= bytearray_hash[size_hash - 1 - j];
          bytearray_hash[size_hash - 1 - j] ^= bytearray_hash[j];
          bytearray_hash[j] ^= bytearray_hash[size_hash - 1 - j];
        }
    }

    cout << endl << "hash:   ";

    for(unsigned i = 0; i < 32; ++i)
    {
        printf("%02x", bytearray_hash[i]);
    }

    cout << endl << "target: ";

    for(unsigned i = 0; i < 32; ++i)
    {
        printf("%02x", bytearray_target[i]);
    }

    cout << endl;

    bool valid = checkValid(bytearray_hash, bytearray_target);
    cout << endl << "valid: " << valid;

    return 0;
}