<blockquote>
  <p>Where in the source code do we check whether
  <code>hash(blockheader*nonce) &lt; Difficulty</code></p>
</blockquote>

<p>The function you are looking for is in <a href="https://github.com/bitcoin/bitcoin/blob/master/src/pow.cpp#L74" rel="nofollow noreferrer"><code>pow.cpp</code></a>:</p>

<pre><code>bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params&amp; params)
{
    bool fNegative;
    bool fOverflow;
    arith_uint256 bnTarget;

    bnTarget.SetCompact(nBits, &amp;fNegative, &amp;fOverflow);

    // Check range
    if (fNegative || bnTarget == 0 || fOverflow || bnTarget &gt; UintToArith256(params.powLimit))
        return false;

    // Check proof of work matches claimed amount
    if (UintToArith256(hash) &gt; bnTarget)
        return false;

    return true;
}
</code></pre>

<blockquote>
  <p>a) this is not the code that miners use to perform the check; it probably is the validation on node-side ?</p>
</blockquote>

<p>This is on the node side. Miners do not use Bitcoin Core software for mining, this check is implemented as part of the ASIC design, so not even software at all. </p>

<blockquote>
  <p>b) here the "hash" refers to what exactly ?</p>
</blockquote>

<p>Here the hash is the function used to calculate the block hash. In Bitcoin, this is 2 rounds of sha256, sometimes called hash256. See <a href="https://en.bitcoin.it/wiki/Block_hashing_algorithm" rel="nofollow noreferrer">Bitcoin Wiki - Bitcoin Hashing Algorithm</a></p>

<blockquote>
  <p>c) why is this value tested against nTarget ? rather than Difficulty ? </p>
</blockquote>

<p>Difficulty is just an abstraction of the <code>nTarget</code>, which represents the actual 256 bit value that the header must be less than (or equal to). The header is a 256 bit value, so it must be compared to <code>nTarget</code>. The conversion between <code>nTarget</code> and difficulty is <code>difficulty = difficulty_1_target / nTarget</code>, where <code>difficulty_1_target</code> is the target of the genesis block. See <a href="https://en.bitcoin.it/wiki/Difficulty" rel="nofollow noreferrer">Bitcoin Wiki - Difficulty</a></p>

<blockquote>
  <p>d) this check is usually advertised in media as "&lt;"less rather than "&lt;=" less than equal. But seeing how this line is coded , it means HASH&lt;= nTarget is OK. </p>
</blockquote>

<p>correct.</p>

CC BY-SA 4.0
2019-02-21T15:22:19.413Z
@JBaczuk.Thank you. This helps.
But is the PoW definition of : Hash(blockheader + nonce) <= Difficulty incorrect ?  .... or is this correct on the miner client end. Where is that line of code in the source code ? I'd be interested to see if that's "<" or "<=".
  
Note that in your outlined answer, we are talking here about the node side. The process that "validates" the hash (which has been computed on the miner's client). This validation takes milliseconds. If on the miner's client "<" was set (rather than "<="), then it's irrelevant of to include the "=" on the node end.
user92452
CC BY-SA 4.0
2019-02-21T15:52:46.287Z
The nonce is part of the block header. The miners have their own code, they don't use Bitcoin Core to mine, in fact they probably do the check in the ASIC, so no software at all. This is the line in Bitcoin Core `if (UintToArith256(hash) > bnTarget) return false`, so if it's >=, then it will be valid. Also, there is virtually no chance the hash will ever be =, so it doesn't matter.
JBaczuk
CC BY-SA 4.0
2019-02-22T06:54:53.720Z
Ahh ... thanks. but I am now a bit confused. I thought that if I run a full node, I perform mining too ? That should be in the bitcoin core code, no ? There must have been a pre-implementation for CPU mining ... where can we find that ?
user92452
CC BY-SA 4.0
2019-02-22T12:58:11.080Z
See https://bitcoin.stackexchange.com/a/51170/60443
JBaczuk
CC BY-SA 4.0
2019-02-21T15:12:53.717Z
2019-02-21T15:54:40.743Z
2019-02-21T15:54:40.743Z
JBaczuk
<p>Where in the source code do we check whether</p>

<pre><code> hash(blockheader*nonce) &lt; Difficulty 
</code></pre>

<p>In fact, the line I found is as follows: 
<a href="https://github.com/bitcoin/bitcoin/blob/v0.17.0.1/src/pow.cpp#L87" rel="nofollow noreferrer">https://github.com/bitcoin/bitcoin/blob/v0.17.0.1/src/pow.cpp#L87</a></p>

<p>My problem with this is 3-fold</p>

<p>a) this is not the code that miners use to perform the check; it probably is the validation on node-side ?</p>

<p>b) here the "hash" refers to what exactly ?</p>

<p>c) why is this value tested against nTarget ? rather than Difficulty ? </p>

<p>d) this check is usually advertised in media as "&lt;"less rather than "&lt;=" less than equal. But seeing how this line
is coded , it means 
     HASH&lt;= nTarget 
is OK. </p>

<hash><difficulty><proof-of-work><source-code>
where in the source code is the the proof of work checking the blockheader hash and nonce?
user92452
# where in the source code is the the proof of work checking the blockheader hash and nonce?

(Asked by: username_0 on 2019-02-21)

Where in the source code do we check whether

```
 hash(blockheader*nonce) < Difficulty 

```

In fact, the line I found is as follows:
<https://github.com/bitcoin/bitcoin/blob/v0.17.0.1/src/pow.cpp#L87>

My problem with this is 3-fold

a) this is not the code that miners use to perform the check; it probably is the validation on node-side ?

b) here the "hash" refers to what exactly ?

c) why is this value tested against nTarget ? rather than Difficulty ?

d) this check is usually advertised in media as "<"less rather than "<=" less than equal. But seeing how this line
is coded , it means
HASH<= nTarget
is OK.

**Tags:** hash, difficulty, proof-of-work, source-code


---

## 1 answers

### Answer by username_1 on 2019-02-21. Score: 3

> Where in the source code do we check whether
> `hash(blockheader*nonce) < Difficulty`

The function you are looking for is in [`pow.cpp`](https://github.com/bitcoin/bitcoin/blob/master/src/pow.cpp#L74):

```
bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params& params)
{
    bool fNegative;
    bool fOverflow;
    arith_uint256 bnTarget;

    bnTarget.SetCompact(nBits, &fNegative, &fOverflow);

    // Check range
    if (fNegative || bnTarget == 0 || fOverflow || bnTarget > UintToArith256(params.powLimit))
        return false;

    // Check proof of work matches claimed amount
    if (UintToArith256(hash) > bnTarget)
        return false;

    return true;
}

```

> a) this is not the code that miners use to perform the check; it probably is the validation on node-side ?

This is on the node side. Miners do not use Bitcoin Core software for mining, this check is implemented as part of the ASIC design, so not even software at all.

> b) here the "hash" refers to what exactly ?

Here the hash is the function used to calculate the block hash. In Bitcoin, this is 2 rounds of sha256, sometimes called hash256. See [Bitcoin Wiki - Bitcoin Hashing Algorithm](https://en.bitcoin.it/wiki/Block_hashing_algorithm)

> c) why is this value tested against nTarget ? rather than Difficulty ?

Difficulty is just an abstraction of the `nTarget`, which represents the actual 256 bit value that the header must be less than (or equal to). The header is a 256 bit value, so it must be compared to `nTarget`. The conversion between `nTarget` and difficulty is `difficulty = difficulty_1_target / nTarget`, where `difficulty_1_target` is the target of the genesis block. See [Bitcoin Wiki - Difficulty](https://en.bitcoin.it/wiki/Difficulty)

> d) this check is usually advertised in media as "<"less rather than "<=" less than equal. But seeing how this line is coded , it means HASH<= nTarget is OK.

correct.

> **Comments:**
> **username_0**: @username_1.Thank you. This helps. But is the PoW definition of : Hash(blockheader + nonce) <= Difficulty incorrect ?  .... or is this correct on the miner client end. Where is that line of code in the source code ? I'd be interested to see if that's "<" or "<=".    Note that in your outlined answer, we are talking here about the node side. The process that "validates" the hash (which has been computed on the miner's client). This validation takes milliseconds. If on the miner's client "<" was set (rather than "<="), then it's irrelevant of to include the "=" on the node end.  
> **username_1**: The nonce is part of the block header. The miners have their own code, they don't use Bitcoin Core to mine, in fact they probably do the check in the ASIC, so no software at all. This is the line in Bitcoin Core `if (UintToArith256(hash) > bnTarget) return false`, so if it's >=, then it will be valid. Also, there is virtually no chance the hash will ever be =, so it doesn't matter.  
> **username_0**: Ahh ... thanks. but I am now a bit confused. I thought that if I run a full node, I perform mining too ? That should be in the bitcoin core code, no ? There must have been a pre-implementation for CPU mining ... where can we find that ?  
> **username_1**: See https://bitcoin.stackexchange.com/a/51170/60443  

---

