Cet article décrit une implémentation en python du minage du bitcoin qui s’appuie sur un algorithme basé sur un double hash SHA-256.

Introduction – Principe de l’agorithme de minage du bitcoin
Les mineurs du réseau bitcoin doivent rechercher le nonce qui est un un nombre de 32 bits. Le mineur va tester successivement plusieurs NONCE (1,2,3 ….10^32-1), pour chacun des nonce il crée l’entête suivante et le hasher 2 fois avec une fonction de hachage SHA-256 bits.
Field | Description | Size |
---|---|---|
version | Version | 4 |
Hash du bloc précédent | 256-bit hash du block précédent | 32 |
Merkle root | Il s’agit d’un hash sur les données du bloc. Il est fournit au mineur et il contient un résumé des transactions qui sont contenues dans le bloc. | 32 |
time | Un timestamp numérique qui représente le nombre de seconde depuis 1970-01-01T00:00 UTC | 4 |
bits | La cible actuelle (Current target) en format compacté | 4 |
nonce | 32-bit number (starts at 0) | 4 |
Une fois le hash obtenu le mineur doit ensuite vérifier que le hash obtenu est inférieur au facteur de difficulté cible du bloc. Si le hash obtenu est supérieur alors le nonce n’est pas le bon il faut en tester un autre.
Exemple sur le bloc 671712
Si vous utilisez un explotateur de blochain bitoin (par exemple blocstream info), si on prends par exemple le bloc 671712 :

Dans ce cas voici un algorithme en python qui permet de miner ce bloc :
Dans cette simulation j’affiche le header et le hash calculé ainsi que le hash rate.
Le nonce a trouver pour le bloc 671712 était 4107802144
Soit en hexa : 0xf4d81620
Cette algorithme démarre avec un nonce = 4107802144 – 400 nous allons faire comme si on était à très proche de trouver le bloc (il manque 400 double hash à effectuer pour trouver le bloc) :
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() header_hex = (binascii.hexlify(struct.Struct('<L').pack(int('0x2fffe000',16))).decode() + 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()) header_bin = unhexlify(header_hex) dt1 = datetime.now().strftime("%H:%M:%S.%f") hash = hashlib.sha256(hashlib.sha256(header_bin).digest()).digest() 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('HEADER=' + header_hex) print('HASH=' + hash) print('NONCE=' + str(inonc)) print('NONCE (hex)=' + hex(inonc)) print('###########BLOC MINED###################') break
La sortie est la suivante :
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################### HEADER=00e0ff2f21d250f04bee45ceb12f53eae1b2979f7afa56d20889070000000000000000009a10af13b10da83515b9f9e64180294e6f91507fda98b5c5c7116f403afc04c53ebc3360e3f40c172016d8f4 HASH=000000000000000000062a949bc297739a12e639ba9e2107638b469afe11d0f8 NONCE=4107802144 NONCE (hex)=0xf4d81620 ###########BLOC MINED###################
Lien externe – minage de bitcoin en python
https://en.bitcoin.it/wiki/Block_hashing_algorithm