Search code examples
c#hashnumericbigintegerbitcoin

How do I compare hex representation of 2 BigIntegers


I need to compare sha256 hashes according to bitcoin proof of work algorithm. So the requirement is to create an upper boundary by setting a target difficulty and then create hashes of data + nonce and compare it with upper boundary. If this calculated value is less than upper boundary then it is a valid proof of work.

So that means our upper boundary i.e. target can be defined as

var target = new BigInteger(1) << 256 - 24

And when we create the hash of new block then we compare it with target. i.e.

var hashInt = new BigInteger(hashBytes)
if (hashInt.CompareTo(target) == -1) {
    //It is a valid hash
}

For this question let’s assume values would be

target:

6901746346790563787434755862277025452451108972170386555162524223799296

hashInt:

-50765282004551092829298895592266344483300201926640276338429566691241878315064

and their hex values would be (which are calculated by calling .ToString("x") on these variables)

target:

10000000000000000000000000000000000000000000000000000000000

hashInt:

8fc3de5d792f2e6b3118697d1e5baa405400e9f8f09625d7eed1481ee0b09fc8

Now when we do hashInt.CompareTo(target) == -1 then it returns true. I completely understand why it’s that, but what I want to do is to compare their hex values and if somehow we would be able to do that then on compare it will return false which is what I want.


Solution

  • You can compare two sequences of equal length with Zip:

    static int CompareSequenceTo<T>(this IEnumerable<T> a, IEnumerable<T> b) where T : IComparable<T> {
        return a
            .Zip(b, (x, y) => x.CompareTo(y))
            .FirstOrDefault(r => r != 0);
    }
    

    Applied to a difficulty a and hash b, it correctly reports that a is less than b:

    byte[] a = {0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
    byte[] b = {0x8f, 0xc3, 0xde, 0x5d, 0x79, 0x2f, 0x2e, 0x6b, 0x31, 0x18, 0x69, 0x7d, 0x1e, 0x5b, 0xaa, 0x40, 0x54, 0x00, 0xe9, 0xf8, 0xf0, 0x96, 0x25, 0xd7, 0xee, 0xd1, 0x48, 0x1e, 0xe0, 0xb0, 0x9f, 0xc8};
    
    Console.WriteLine(a.CompareSequenceTo(b));  // some value less than zero