Comparing hex representation of 2 BigIntegers

0 votes

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.

Aug 27, 2018 in Blockchain by slayer
• 29,050 points
23 views

1 answer to this question.

0 votes

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
answered Aug 27, 2018 by digger
• 26,550 points

Related Questions In Blockchain

0 votes
1 answer

How can i read headers of blockchain in multichain?

You could use a recent feature known ...READ MORE

answered Mar 26, 2018 in Blockchain by Perry
• 17,020 points
40 views
+1 vote
1 answer

Security Properties of Hash Functions

The difference is indeed subtle. I understood ...READ MORE

answered Mar 26, 2018 in Blockchain by Johnathon
• 9,080 points
283 views
+1 vote
1 answer
+1 vote
1 answer

I am unable to change port of composer-rest-server. Please help!

Use full format for running a business ...READ MORE

answered Mar 27, 2018 in Blockchain by Christine
• 15,790 points
283 views
0 votes
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured by TLS encryption in hashgraph architecture-hashgraph, ...READ MORE

answered Aug 6, 2018 in Blockchain by ariaholic
• 7,340 points
76 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
141 views
0 votes
1 answer
+1 vote
1 answer

What problem does pow(proof of work) solves in blockchain?

Proof of Work: When a miner gets a ...READ MORE

answered Aug 6, 2018 in Blockchain by digger
• 26,550 points
57 views
0 votes
1 answer