Implementation of a blockchain cycle

This commit is contained in:
daviddoji 2021-04-13 21:18:10 +02:00
parent 1a4ca51660
commit a113d5d1a2

487
Blockchain.ipynb Normal file
View File

@ -0,0 +1,487 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"A = \"6a09e667\"\n",
"B = \"bb67ae85\"\n",
"C = \"3c6ef372\"\n",
"D = \"a54ff53a\"\n",
"E = \"510e527f\"\n",
"F = \"9b05688c\"\n",
"G = \"1f83d9ab\"\n",
"H = \"5be0cd19\"\n",
"W = \"02000000\"\n",
"K = \"428a2f98\""
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def hex2bin(text):\n",
" \"\"\"\n",
" Converts a hex string into binary\n",
" \n",
" Parameters\n",
" ----------\n",
" text: str\n",
" A string of hexadecimal characters\n",
" \n",
" Return\n",
" ------\n",
" ret: str\n",
" A string of the hexadecimal characters converted\n",
" to binary using 0 padding when needed\n",
" \"\"\"\n",
" formatting = f\"0{len(text) * 4}b\"\n",
" ret = format(int(text, 16), formatting)\n",
" return ret"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'01101010000010011110011001100111'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"hex2bin(A)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def majority(str1, str2, str3):\n",
" \"\"\"\n",
" Outputs the majority of bits for each position of the inputs\n",
" \n",
" Parameters\n",
" ----------\n",
" str1, str2, str3: str\n",
" A string of hexadecimal characters\n",
" \n",
" Return\n",
" ------\n",
" ret: str\n",
" A string of binary characters using padding when needed\n",
" \"\"\"\n",
" bin_str1 = hex2bin(str1)\n",
" bin_str2 = hex2bin(str2)\n",
" bin_str3 = hex2bin(str3)\n",
"\n",
" ret = str(''.join(str(max(set(bit), key=bit.count))\n",
" for bit in zip(bin_str1, bin_str2, bin_str3)))\n",
" return ret"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'00111010011011111110011001100111'"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"majority(A, B, C)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"def bin2hex(text):\n",
" \"\"\"\n",
" Converts a binary string into hex\n",
" \n",
" Parameters\n",
" ----------\n",
" text: str\n",
" A string of binary characters\n",
" \n",
" Return\n",
" ------\n",
" ret: str\n",
" A string of the binary characters converted to hexadecimal \n",
" \"\"\"\n",
" formatting = f\"0x\"\n",
" ret = format(int(text, 2), formatting)\n",
" return ret"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'3a6fe667'"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bin2hex(majority(A, B, C))"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"def rightShift(text,n):\n",
" \"\"\"\n",
" Right shifts a string 'n' times\n",
" \n",
" Parameters\n",
" ----------\n",
" text: str\n",
" A string of characters\n",
" \n",
" Return\n",
" ------\n",
" ret: str\n",
" A string of the characters rightshifted 'n' times \n",
" \"\"\"\n",
" return text[-n:] + text[:-n]\n",
"\n",
"def sigma_0(text):\n",
" \"\"\"\n",
" Rotates the bits of text and sums them all modulo 2\n",
" \n",
" Parameters\n",
" ----------\n",
" text: str\n",
" A string of characters\n",
" \n",
" Return\n",
" ------\n",
" ret: str\n",
" A string of the characters converted to hexadecimal \n",
" \"\"\"\n",
" tmp_2 = rightShift(hex2bin(text), 2)\n",
" tmp_13 = rightShift(hex2bin(text), 13)\n",
" tmp_22 = rightShift(hex2bin(text), 22)\n",
" \n",
" tmp = []\n",
" for bit in zip(tmp_2, tmp_13, tmp_22):\n",
" if list(bit).count(\"1\") % 2:\n",
" tmp.append(\"1\")\n",
" else:\n",
" tmp.append(\"0\")\n",
" \n",
" ret = str(''.join(tmp))\n",
" return ret"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'ce20b47e'"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bin2hex(sigma_0(A))"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"def sigma_1(text):\n",
" \"\"\"\n",
" Rotates the bits of text and sums them all modulo 2\n",
" \n",
" Parameters\n",
" ----------\n",
" text: str\n",
" A string of characters\n",
" \n",
" Return\n",
" ------\n",
" ret: str\n",
" A string of the characters converted to hexadecimal \n",
" \"\"\"\n",
" tmp_6 = rightShift(hex2bin(text), 6)\n",
" tmp_11 = rightShift(hex2bin(text), 11)\n",
" tmp_25 = rightShift(hex2bin(text), 25)\n",
" \n",
" tmp = []\n",
" for bit in zip(tmp_6, tmp_11, tmp_25):\n",
" if list(bit).count(\"1\") % 2:\n",
" tmp.append(\"1\")\n",
" else:\n",
" tmp.append(\"0\")\n",
" \n",
" ret = str(''.join(tmp))\n",
" return ret"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'3587272b'"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bin2hex(sigma_1(E))"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"def choose(str1, str2, str3):\n",
" \"\"\"\n",
" Chooses bits of str2 or str3 according to str1\n",
" \n",
" Parameters\n",
" ----------\n",
" str1, str2, str3: str\n",
" A string of hexadecimal characters\n",
" \n",
" Return\n",
" ------\n",
" ret: str\n",
" A string of the characters converted to hexadecimal \n",
" \"\"\" \n",
" bin_str1 = hex2bin(str1)\n",
" bin_str2 = hex2bin(str2)\n",
" bin_str3 = hex2bin(str3)\n",
" \n",
" tmp = []\n",
" for idx, bit in enumerate(bin_str1):\n",
" if bit == \"1\":\n",
" tmp.append(bin_str2[idx])\n",
" else:\n",
" tmp.append(bin_str3[idx])\n",
" \n",
" ret = str(''.join(tmp))\n",
" return ret"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'1f85c98c'"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bin2hex(choose(E, F, G))"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"def sum_partial(W, K, H, E, F, G):\n",
" W_ = int(W, 16)\n",
" K_ = int(K, 16)\n",
" H_ = int(H, 16)\n",
" Ch_ = int(bin2hex(choose(E, F, G)), 16)\n",
" S1_ = int(bin2hex(sigma_1(E)), 16)\n",
" \n",
" ret = format(W_ + K_ + H_ + Ch_ + S1_, 'x')\n",
" \n",
" if len(ret) > 8:\n",
" ret = ret[1:]\n",
" return ret"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"partial = sum_partial(W, K, H, E, F, G)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"def new_A(A, B, C, partial):\n",
" S0_ = int(bin2hex(sigma_0(A)), 16)\n",
" maj_ = int(bin2hex(majority(A, B, C)), 16)\n",
" part_ = int(partial, 16)\n",
"\n",
" ret = format(S0_ + maj_ + part_, 'x')\n",
"\n",
" if len(ret) > 8:\n",
" ret = ret[1:]\n",
" return ret"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"new_A = new_A(A, B, C, partial)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"def new_E(D, partial):\n",
" D_ = int(D, 16)\n",
" part_ = int(partial, 16)\n",
" \n",
" ret = format(D_ + part_, 'x')\n",
" \n",
" if len(ret) > 8:\n",
" ret = ret[1:]\n",
" return ret"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"new_E = new_E(D, partial)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Values for next round of SHA-216 are:\n",
" A: fe08884d\n",
" B: 6a09e667\n",
" C: bb67ae85\n",
" D: 3c6ef372\n",
" E: 9ac7e2a2\n",
" F: 510e527f\n",
" G: 9b05688c\n",
" H: 1f83d9ab\n"
]
}
],
"source": [
"print(\n",
" f'''Values for next round of SHA-216 are:\n",
" A: {new_A}\n",
" B: {A}\n",
" C: {B}\n",
" D: {C}\n",
" E: {new_E}\n",
" F: {E}\n",
" G: {F}\n",
" H: {G}''')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}