Bitcoin Forum

Bitcoin => Bitcoin Discussion => Topic started by: Jaxkr on June 21, 2013, 04:42:28 PM



Title: From a low level, how does difficulty work?
Post by: Jaxkr on June 21, 2013, 04:42:28 PM
I'm having a really hard time wrapping my head around what the difficulty number means. I don't understand how it can be so high, and how it can affect the target. I have read the wiki page (https://en.bitcoin.it/wiki/Difficulty#What_is_.22difficulty.22.3F), and it didn't really help.

I understand how a rudimentary POW algorithm works. In fact, I've even written one (https://gist.github.com/Jaxkr/5745803). It hashes data, and adds a number on the end, and hopes that the resulting hash is prefixed with a certain amount of 0's (or any special character). I know that a hash that has a prefix of 5 zeroes is significantly more difficult than a hash that hash a prefix of 1 zero. Each nonce has a lower probability of producing that hash, therefore it's more difficult.
So why isn't the difficulty just a whole integer? A difficulty of 1 means that the hash needs to have one zero. A difficulty of 2 means that the hash needs two zeroes in front of it. And so on.
What does 19339258.272387 mean? And how does it affect the miner's target? Sorry for my cluelessness.


Title: Re: From a low level, how does difficulty work?
Post by: Birdy on June 21, 2013, 04:51:11 PM
Afaik the difficult number just means it's 19339258.272387 times harder to find a new block than the first one that was ever created.


Title: Re: From a low level, how does difficulty work?
Post by: nimda on June 21, 2013, 04:56:15 PM
Looking for n-zeroes in front isn't precise enough: difficulty jumps exponentially with every digit. Instead, the miner looks for hashes that are numerically less than the target (https://en.bitcoin.it/wiki/Target).

Here's the maximum target:
0x00000000FFFF0000000000000000000000000000000000000000000000000000

A maximum target means a minimum difficulty, because the higher the target, the more hashes are less than that target.

The difficulty is the maximum target divided by the current target.

As the current target goes down, it becomes harder to generate blocks. The current target is the denominator of the difficulty equation, so as current target goes down, "difficulty" goes up.

Make sense?


Title: Re: From a low level, how does difficulty work?
Post by: Jaxkr on June 21, 2013, 05:03:36 PM
Looking for n-zeroes in front isn't precise enough: difficulty jumps exponentially with every digit. Instead, the miner looks for hashes that are numerically less than the target (https://en.bitcoin.it/wiki/Target).

Here's the maximum target:
0x00000000FFFF0000000000000000000000000000000000000000000000000000

A maximum target means a minimum difficulty, because the higher the target, the more hashes are less than that target.

The difficulty is the maximum target divided by the current target.

As the current target goes down, it becomes harder to generate blocks. The current target is the denominator of the difficulty equation, so as current target goes down, "difficulty" goes up.

Make sense?
Kind of. So the target is a 64 bit number that represents what the hash of the block in numeric form needs to be less than. So, the hash of the block is treated as a hex number, and compared to the target, and it has to be less than the target?


Title: Re: From a low level, how does difficulty work?
Post by: nimda on June 21, 2013, 05:06:16 PM
Make sense?
Kind of. So the target is a 64 bit number that represents what the hash of the block in numeric form needs to be less than. So, the hash of the block is treated as a hex number, and compared to the target, and it has to be less than the target?
Kind of. A sha256 hash is always a number, no matter how you represent it (hex or not). The target is also a number, and it can be represented however you want.

Regardless of format, the hash of the block's header is treated as a number, and it has to be less than the target.


Title: Re: From a low level, how does difficulty work?
Post by: Stephen Gornick on June 21, 2013, 05:07:27 PM
So the target is a 64 bit number

A 256 bit number stored as a floating point and truncated:
- http://en.bitcoin.it/wiki/Target

Also more info here:

What is “difficulty” and how it relates to “target”?
 - http://bitcoin.stackexchange.com/q/8806/153


Title: Re: From a low level, how does difficulty work?
Post by: Jaxkr on June 21, 2013, 05:09:38 PM
Make sense?
Kind of. So the target is a 64 bit number that represents what the hash of the block in numeric form needs to be less than. So, the hash of the block is treated as a hex number, and compared to the target, and it has to be less than the target?
Kind of. A sha256 hash is always a number, no matter how you represent it (hex or not). The target is also a number, and it can be represented however you want.

Regardless of format, the hash of the block's header is treated as a number, and it has to be less than the target.
OK. The header contains a hash of the list of transaction in the block, so there is no computational incentive not to include transactions.
And the reason a large amount of zeros are needed prefixing the hash is because it make the value of the hash less than the target. Thank you for explaining that.  :)
Could a POW algorithm work just as well when the hash has to be greater than the target? Eg. It's prefixed with "fffffff" instead of "000000"?


Title: Re: From a low level, how does difficulty work?
Post by: Jaxkr on June 21, 2013, 05:12:42 PM
So the target is a 64 bit number

A 256 bit number stored as a floating point and truncated:
- http://en.bitcoin.it/wiki/Target

Also more info here:

What is “difficulty” and how it relates to “target”?
 - http://bitcoin.stackexchange.com/q/8806/153
Thanks. Can you link me to the formula used to convert difficulty to target? Is it simply the max target divided by the difficulty?


Title: Re: From a low level, how does difficulty work?
Post by: Rannasha on June 21, 2013, 05:17:44 PM
Make sense?
Kind of. So the target is a 64 bit number that represents what the hash of the block in numeric form needs to be less than. So, the hash of the block is treated as a hex number, and compared to the target, and it has to be less than the target?
Kind of. A sha256 hash is always a number, no matter how you represent it (hex or not). The target is also a number, and it can be represented however you want.

Regardless of format, the hash of the block's header is treated as a number, and it has to be less than the target.
OK. The header contains a hash of the list of transaction in the block, so there is no computational incentive not to include transactions.
And the reason a large amount of zeros are needed prefixing the hash is because it make the value of the hash less than the target. Thank you for explaining that.  :)
Yes. You can imagine it in base-10: Suppose a 5 digit number (with prefix zeroes) needs to be smaller than 100, that means that you need at least 3 prefix zeroes (00099 and below).

Quote
Could a POW algorithm work just as well when the hash has to be greater than the target? Eg. It's prefixed with "fffffff" instead of "000000"?
Sure, there's no reason why the hash needs to be smaller than a small target. Might as well require it to be larger than some large target.


Title: Re: From a low level, how does difficulty work?
Post by: nimda on June 21, 2013, 05:24:16 PM
Thanks. Can you link me to the formula used to convert difficulty to target? Is it simply the max target divided by the difficulty?
Yes.
maximum_target = 0x00000000FFFF0000000000000000000000000000000000000000000000000000

difficulty = maximum_target / current_target

multiply both sides by current_target:
current_target * difficulty = maximum_target

divide both sides by difficulty:
current_target = maximum_target / difficulty


Title: Re: From a low level, how does difficulty work?
Post by: Jaxkr on June 21, 2013, 05:53:05 PM
Thanks. Can you link me to the formula used to convert difficulty to target? Is it simply the max target divided by the difficulty?
Yes.
maximum_target = 0x00000000FFFF0000000000000000000000000000000000000000000000000000

difficulty = maximum_target / current_target

multiply both sides by current_target:
current_target * difficulty = maximum_target

divide both sides by difficulty:
current_target = maximum_target / difficulty

Thank you! Now I understand.


Title: Re: From a low level, how does difficulty work?
Post by: r3wt on June 21, 2013, 05:58:03 PM
fucking nerds. :P


Title: Re: From a low level, how does difficulty work?
Post by: Jaxkr on June 21, 2013, 06:02:21 PM
fucking nerds. :P
C'mon. If you're going to use this currency, might as well know how it works. :P