3

I do a sort of bitcoin 101 presentation for folks where I work. In one of the recent sessions, someone asked why the proof of work difficulty requires a minimum number of leading 0s. Why not 1s or something else?

Not understanding the math behind it in-depth, the best answer I can come up with is that the protocol requires that the proof of work hash (hash of nonce and block data) should be equal to or lower than the difficulty target. Hence, you can interpret it as having to contain a certain number of leading 0s.

Is there a better explanation?

Thanks!

septerr
  • 173
  • 5

1 Answers1

7

Not understanding the math behind it in-depth, the best answer I can come up with is that the protocol requires that the proof of work hash (hash of nonce and block data) should be equal to or lower than the difficulty target. Hence, you can interpret it as having to contain a certain number of leading 0s.

This is exactly what it is. The actual calculation has nothing to do with leading 0s. It's entirely just a comparison of integers, which has the effect of producing leading 0s.

Ava Chow
  • 70,382
  • 5
  • 81
  • 161
  • 6
    I think the explanation with "number of zeroes" has a historical reason: hashcash, the original PoW system, had a "difficulty" that was actually the number of zero bits up front in the hash. Bitcoin's proof of work is based on it, but generalized to a big integer comparison. – Pieter Wuille Aug 08 '21 at 22:45
  • @andrew-chow thank you for your answer. Say if difficulty target was 0F in hexadecimal. Then the PoW hash could be anything between 00 to 0E. In this case the number of leading 0s required is 1, but that is not sufficient. The second number needs to be between 0 and E inclusive. Is this correct? – septerr Aug 08 '21 at 22:59
  • 2
    Correct. Even the earliest instances of the code pay no attention to the number of leading 0s and really have the requirement of being smaller than a given target.

    Here's a code snippet of how hash target was adjusted (it was effectively multiplied by the ratio of actual time taken over the time desired by satoshi) -- https://github.com/bitcoin/bitcoin/blob/4405b78d6059e536c36974088a8ed4d9f0f29898/main.cpp#L716.

    Here's a code snippet where the comparison was actually made -- https://github.com/bitcoin/bitcoin/blob/4405b78d6059e536c36974088a8ed4d9f0f29898/main.cpp#L2345

    – Randy Aug 09 '21 at 00:59