<p><code>hash</code> is the SHA256 hash value (a large integer) calculated by the miner for the block. If the hash is less than or equal to the Bitcoin network target, the miner succeeded and the hash is a valid proof of work. Conversely, if the hash value is greater than the Bitcoin network target, the miner failed and the hash is not a valid proof of work.</p>
<p><code>bnTarget</code> is the current Bitcoin network target (inverse of difficulty). In the network packets it is sent in a Bitcoin specific compact form (<code>nbits</code>) but you need it expanded to an integer for use in numeric comparisons with calculated hashes.</p>
<p><code>fNegative</code> and <code>fOverflow</code> are error flag values returned by the <code>SetCompact()</code> method. They indicate if the conversion from the <code>nbits</code> value resulted in a negative number (bad) or an arithmetic overflow (bad)</p>
<p><code>params.powlimit</code> is the maximum possible value of a hash value representing proof of work (hence <code>pow</code>). This is less than the actual max value of the data type being used here. The value of <code>powLimit</code> is <code>0x00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff</code>, the value corresponding to difficulty 1.</p>

CC BY-SA 4.0
2022-10-09T09:22:57.473Z
What is `uint256 hash` and what does it do? I assumed it is the hash generated by a miner but then why the comparation is `>` against the `bnTarget` instead of `<`?
smartmouse
CC BY-SA 4.0
2022-10-09T09:50:53.467Z
You are right, `hash` is the value calculated by the miner. Note that `if (hash > bnTarget) FAIL` is the same as `if (hash <= bnTarget) SUCCEED`.
RedGrittyBrick
CC BY-SA 4.0
2022-10-05T10:46:34.140Z
2022-10-09T09:53:13.240Z
2022-10-09T09:53:13.240Z
RedGrittyBrick
<p>According to the <a href="https://github.com/bitcoin/bitcoin/blob/master/src/pow.cpp#L74" rel="nofollow noreferrer">source code</a> here is where the hash computed by a miner is compared with the target hash:</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>
<p>Can you fill the code above with comments to make me to understand what is the role of every variable used in this function?</p>
<p>About the hashes comparing I have read <a href="https://bitcoin.stackexchange.com/a/44580/76216">this</a>, anyway, can you make another example printing hash strings to show to me the output of every line of the code?</p>

<bitcoin-core-development><hash><difficulty><proof-of-work>
Proof of work: comparing computed hash with target hash
smartmouse
# Proof of work: comparing computed hash with target hash

(Asked by: username_0 on 2022-10-04)

According to the [source code](https://github.com/bitcoin/bitcoin/blob/master/src/pow.cpp#L74) here is where the hash computed by a miner is compared with the target hash:

```
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;
}

```

Can you fill the code above with comments to make me to understand what is the role of every variable used in this function?

About the hashes comparing I have read [this](https://bitcoin.stackexchange.com/a/44580/76216), anyway, can you make another example printing hash strings to show to me the output of every line of the code?

**Tags:** bitcoin-core-development, hash, difficulty, proof-of-work


---

## 1 answers

### Answer by username_1 on 2022-10-05. Score: 3

`hash` is the SHA256 hash value (a large integer) calculated by the miner for the block. If the hash is less than or equal to the Bitcoin network target, the miner succeeded and the hash is a valid proof of work. Conversely, if the hash value is greater than the Bitcoin network target, the miner failed and the hash is not a valid proof of work.

`bnTarget` is the current Bitcoin network target (inverse of difficulty). In the network packets it is sent in a Bitcoin specific compact form (`nbits`) but you need it expanded to an integer for use in numeric comparisons with calculated hashes.

`fNegative` and `fOverflow` are error flag values returned by the `SetCompact()` method. They indicate if the conversion from the `nbits` value resulted in a negative number (bad) or an arithmetic overflow (bad)

`params.powlimit` is the maximum possible value of a hash value representing proof of work (hence `pow`). This is less than the actual max value of the data type being used here. The value of `powLimit` is `0x00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff`, the value corresponding to difficulty 1.

> **Comments:**
> **username_0**: What is `uint256 hash` and what does it do? I assumed it is the hash generated by a miner but then why the comparation is `>` against the `bnTarget` instead of `<`?  
> **username_1**: You are right, `hash` is the value calculated by the miner. Note that `if (hash > bnTarget) FAIL` is the same as `if (hash <= bnTarget) SUCCEED`.  

---

