Skip to content

Instantly share code, notes, and snippets.

@nickyreinert
Last active March 20, 2022 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickyreinert/9f520b090d1a56cc90074c41fc4750df to your computer and use it in GitHub Desktop.
Save nickyreinert/9f520b090d1a56cc90074c41fc4750df to your computer and use it in GitHub Desktop.
Find a nonce for given set of header fields, utilizing overt ASIC boost
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Found this nonce 3371194464 after 10,000 iterations and 0.31 seconds.\n",
"Result hash is\n",
"\tb'000000000000000000078c7ad45ab182796bbb419c361aa3eaffaa8ddd00d4f0'\n"
]
}
],
"source": [
"import hashlib\n",
"import binascii\n",
"import datetime\n",
"import time\n",
"\n",
"def find_nonce(version, soft_forks, hashPrevBlock, merkle_tree, datetime_str, bits, nonce_start = 0, version_roller_start = 0):\n",
"\n",
" start = time.time()\n",
"\n",
" mantissa = int(bits[-6:], 16)\n",
" exponent = int(bits[:2], 16) - 3\n",
" target = mantissa * pow(256, (exponent))\n",
"\n",
" version_limit = pow(2, 16) \n",
" nonce_limit = pow(2, 32)\n",
"\n",
" for version_roller in range(version_roller_start, version_limit):\n",
"\n",
" version_hex = hex(int(str(bin(version)[:-1]) + format(version_roller, '016b') + soft_forks, 2))[2:]\n",
" \n",
" for nonce in range(nonce_start, nonce_limit):\n",
"\n",
" if nonce % 100000 == 0: \n",
" print(str(round(100 * nonce / nonce_limit, 2)) + '% ')\n",
" \n",
" nonce = nonce + 1\n",
" nonce_hex = hex(nonce)[-8:] # 9546a142\n",
"\n",
" datetime_int = int(datetime.datetime.strptime(datetime_str, \"%Y-%m-%d %H:%M:%S%z\").timestamp())\n",
" datetime_hex = hex(datetime_int)[-8:]\n",
"\n",
" # convert to little endians\n",
" nonce_hex_le = binascii.hexlify(binascii.unhexlify(nonce_hex)[::-1]) # 42a14695\n",
" version_hex_le = binascii.hexlify(binascii.unhexlify(version_hex)[::-1])\n",
" hashPrevBlock_hex_le = binascii.hexlify(binascii.unhexlify(hashPrevBlock)[::-1])\n",
" hashMerkleRoot_hex_le = binascii.hexlify(binascii.unhexlify(merkle_tree)[::-1])\n",
" datetime_hex_le = binascii.hexlify(binascii.unhexlify(datetime_hex)[::-1]) # c7f5d74d\n",
" bits_hex_le = binascii.hexlify(binascii.unhexlify(bits)[::-1]) # f2b9441a\n",
"\n",
" # concatenate to header message\n",
" header_hex = version_hex_le + hashPrevBlock_hex_le + hashMerkleRoot_hex_le + datetime_hex_le + bits_hex_le + nonce_hex_le\n",
" \n",
" # create binary data\n",
" header_bin = binascii.unhexlify(header_hex)\n",
"\n",
" # double hash\n",
" hash = hashlib.sha256(hashlib.sha256(header_bin).digest()).digest()\n",
"\n",
" # check if below target\n",
" if int.from_bytes(hash, 'little') <= target:\n",
"\n",
" duration = time.time() - start\n",
"\n",
" message = 'Found this nonce %s after %s iterations and %s seconds.\\r\\nResult hash is\\r\\n\\t%s' % (\n",
" nonce,\n",
" '{:0,.0f}'.format(nonce - nonce_start),\n",
" '{:.2f}'.format(duration),\n",
" binascii.hexlify(hash[::-1])\n",
" )\n",
" \n",
" return message\n",
"\n",
"# ref https://blockstream.info/block/00000000000000000006b9bf976b004581c19d800948d2eab77c8f9e07b9f088\n",
"print(find_nonce(\n",
" version = 2,\n",
" soft_forks = '0000000000000',\n",
" hashPrevBlock = \"00000000000000000004136135b2e0cd367b56ea6c0dd5b8f79964a4cd7d2718\",\n",
" merkle_tree = \"0d14fac91555d6337b10b2f20de231858fb5225f2ff685cd9b487c235d6e8307\",\n",
" datetime_str = \"2022-03-13 14:20:57+01:00\",\n",
" bits = \"170a3773\",\n",
" nonce_start = 3371184464,\n",
" version_roller_start = 1\n",
"))"
]
}
],
"metadata": {
"interpreter": {
"hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
},
"kernelspec": {
"display_name": "Python 3.9.7 64-bit",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.10"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment