# The algorithm for mining bitcoin in Python

This article describes a python implementation of bitcoin mining that relies on an algorithm based on a double hash SHA-256.

## Introduction – Principle of the bitcoin mining agorithm

Miners on the bitcoin network should look for the nonce which is a 32-bit number. The miner will successively test several NONCEs (1,2,3 …. 10 ^ 32-1), for each of the nonce he creates the following header and hasher twice with a SHA-256 bit hash function.

Once the hash obtained, the miner must then check that the hash obtained is lower than the target difficulty factor of the block. If the hash obtained is greater then the nonce is not the correct one, another must be tested.

## Example on block 671712

If you are using a blockhain bitoin explorer (for example blocstream info), if we take for example block 671712:

https://blockstream.info/block/000000000000000000062a949bc297739a12e639ba9e2107638b469afe11d0f8?expand

In this case here is an algorithm in python which allows to mine this block:

In this simulation I display the header and the calculated hash as well as the hash rate.

The nonce to find for block 671712 was 4107802144

This algorithm starts with a nonce = 4107802144 – 400 we will act as if we were very close to finding the block (400 double hashes are missing to find the block):

```import hashlib
from hashlib import sha256
import time
import struct
import binascii
import datetime
from binascii import unhexlify, hexlify
from dateutil import parser
from datetime import datetime
from datetime import timedelta

nontrouve = True
dtprec = datetime.now()
inonc = 4107802134 - 400 #Starting 400 before the good nonce
resultat = []

while(nontrouve):
inonc +=1
if(inonc%50==0):
print(str(round(timedelta(seconds=1)/(datetime.now() - dtprec))) + ' H/s')
dtprec = datetime.now()
binascii.hexlify(binascii.unhexlify('000000000000000000078908d256fa7a9f97b2e1ea532fb1ce45ee4bf050d221')[::-1]).decode()+
binascii.hexlify(binascii.unhexlify('c504fc3a406f11c7c5b598da7f50916f4e298041e6f9b91535a80db113af109a')[::-1]).decode() +
binascii.hexlify(struct.Struct('<L').pack(int(hex(int(parser.parse('2021-02-22 15:14:22 GMT +1').timestamp())-3600),16))).decode() +
binascii.hexlify(struct.Struct('<L').pack(int("0x170cf4e3",16))).decode() +
binascii.hexlify(struct.Struct('<L').pack(int(hex(inonc),16))).decode())
dt1 = datetime.now().strftime("%H:%M:%S.%f")
hexlify(hash).decode("utf-8")
hexlify(hash[::-1]).decode("utf-8")
hash=hexlify(hash[::-1]).decode("utf-8")
resultat.append([round(int(hash,16)/10**65)])

MAX_TARGET = int("00000000FFFF0000000000000000000000000000000000000000000000000000", 16)
Difficulty = 21724134900047.27
target = int(MAX_TARGET / Difficulty)
target32 = '{:0>64x}'.format(target)
if(int(hash,16) < int(target32,16)):
print('###########BLOC MINED###################')
print('HASH=' + hash)
print('NONCE=' + str(inonc))
print('NONCE (hex)=' + hex(inonc))
print('###########BLOC MINED###################')
break```

The output is :

```1149 H/s
4405 H/s
4115 H/s
1534 H/s
3831 H/s
2392 H/s
4386 H/s
3165 H/s
###########BLOC MINED###################