<p>Assume we have a <strong>transaction</strong> in hex (<strong>tx_hex</strong>) and we want to find a <strong>nonce</strong> such that</p>
<p>SHA-256(SHA-256(nonce|| tx_hex)) = new_transaction_id   ('||' denotes concatenation)</p>
<p>first bits consist of <strong>32 zeros</strong>. I understand that this nonce can be found only by brute force.</p>
<p>However, I cannot figure out how you check this condition in practice. new_transaction_id is in hex, so is that equivalent to checking that its first 8 digits are 0?</p>
<p>And if so to find the nonce, I just have to try every possible number (let's say up to 32 bits) for the first 8 digits of new_transaction_id to be 0?</p>
<p>That is my code BTW</p>
<pre><code>def mine(self, tx_hex):
    for i in range(1,4294967295):
        nonce = hex(i)[2:]
        new_tx = self.add_nonce(nonce, tx_hex)
        sha = self.get_tx_id(new_tx)
        print(sha[:2])
        if sha[:8] == '00000000':
            print('Nonce:', i)
            break
</code></pre>

CC BY-SA 4.0
2021-02-18T12:16:08.203Z
Does this answer your question? [How does solving a block work in relation to the first letter/number after the 0's?](https://bitcoin.stackexchange.com/questions/102414/how-does-solving-a-block-work-in-relation-to-the-first-letter-number-after-the-0)
RedGrittyBrick
CC BY-SA 4.0
2021-02-18T12:54:18.653Z
@RedGrittyBrick I don't know if I fully understood your answer, but here I specifically want the new hash to have zeros at its first 32 bits. Can you help for this case?
Paris
CC BY-SA 4.0
2021-02-18T13:37:56.587Z
Ignore the count of leading zeroes. Test whether the calculated hash is numerically **less than** the target value (`hash < target`).
RedGrittyBrick
CC BY-SA 4.0
2021-02-19T07:39:18.657Z
@RedGrittyBrick Ok, that makes sense in practice, but I still want to understand what we mean by the first 32 bits to be zero. Do we examine the 256 bits of SHA-256(SHA-256(nonce|| tx_hex)) (binary) output and expect the first 32 to be zero?
Paris
CC BY-SA 4.0
2021-02-19T12:18:46.863Z
See  https://www.oreilly.com/library/view/mastering-bitcoin/9781491902639/ch08.html#difficulty_bits  you  should probably view the [difficulty](https://en.bitcoin.it/wiki/Difficulty) as simply a compact encoding of an inverse of the block [target](https://en.bitcoin.it/wiki/Target). You get the target then you compare the hash with the target, not with any aspect of the compact encoding of it's inverse. The 32 bits of zeroes is just an aspect of the encoding - it isn't applied to the hash.
RedGrittyBrick
<mining-theory><development><python><nonce>
Process for finding nonce
Paris
# Process for finding nonce

(Asked by: username_0 on 2021-02-18)

Assume we have a transaction in hex (tx_hex) and we want to find a nonce such that
SHA-256(SHA-256(nonce|| tx_hex)) = new_transaction_id   ('||' denotes concatenation)
first bits consist of 32 zeros. I understand that this nonce can be found only by brute force.
However, I cannot figure out how you check this condition in practice. new_transaction_id is in hex, so is that equivalent to checking that its first 8 digits are 0?
And if so to find the nonce, I just have to try every possible number (let's say up to 32 bits) for the first 8 digits of new_transaction_id to be 0?
That is my code BTW
def mine(self, tx_hex):
    for i in range(1,4294967295):
        nonce = hex(i)[2:]
        new_tx = self.add_nonce(nonce, tx_hex)
        sha = self.get_tx_id(new_tx)
        print(sha[:2])
        if sha[:8] == '00000000':
            print('Nonce:', i)
            break

**Tags:** mining-theory, development, python, nonce

> **Comments:**
> **username_1**: Does this answer your question? [How does solving a block work in relation to the first letter/number after the 0's?](https://bitcoin.stackexchange.com/questions/102414/how-does-solving-a-block-work-in-relation-to-the-first-letter-number-after-the-0)  
> **username_0**: @username_1 I don't know if I fully understood your answer, but here I specifically want the new hash to have zeros at its first 32 bits. Can you help for this case?  
> **username_1**: Ignore the count of leading zeroes. Test whether the calculated hash is numerically **less than** the target value (`hash < target`).  
> **username_0**: @username_1 Ok, that makes sense in practice, but I still want to understand what we mean by the first 32 bits to be zero. Do we examine the 256 bits of SHA-256(SHA-256(nonce|| tx_hex)) (binary) output and expect the first 32 to be zero?  
> **username_1**: See  https://www.oreilly.com/library/view/mastering-bitcoin/9781491902639/ch08.html#difficulty_bits  you  should probably view the [difficulty](https://en.bitcoin.it/wiki/Difficulty) as simply a compact encoding of an inverse of the block [target](https://en.bitcoin.it/wiki/Target). You get the target then you compare the hash with the target, not with any aspect of the compact encoding of it's inverse. The 32 bits of zeroes is just an aspect of the encoding - it isn't applied to the hash.  

---

## 0 answers

