Browse Source

Fix code block flags

master
infogulch 2 months ago
parent
commit
77dbe92327
  1. 223
      2021-07-07-Merklist.ipynb

223
2021-07-07-Merklist.ipynb

@ -83,7 +83,7 @@
{
"cell_type": "code",
"execution_count": 2,
"id": "3ccc7fdc-fa6a-48e3-accb-3c1070b4559c",
"id": "7f76f998-94a9-4002-adb3-73ccf88223e2",
"metadata": {
"tags": []
},
@ -96,15 +96,12 @@
},
{
"cell_type": "markdown",
"id": "04132091-21b1-4fbb-99df-711ae5e0c819",
"id": "11c69415-fac2-46a5-97a3-5a7f58376664",
"metadata": {
"slideshow": {
"slide_type": "skip"
},
"tags": []
},
"source": [
"8×8 seems big compared to 3×3 or 4×4 matrixes. The values are as random as you might expect a cryptographic hash to be, and range from 0-255:"
"Lets see it in use:"
]
},
{
@ -112,9 +109,6 @@
"execution_count": 3,
"id": "65aa7c7a-25d5-4971-8780-661f367e45ab",
"metadata": {
"jupyter": {
"source_hidden": true
},
"slideshow": {
"slide_type": "skip"
},
@ -146,12 +140,24 @@
}
],
"source": [
"#collapse-hide\n",
"print(hash_m(b\"Hello A\"))\n",
"print()\n",
"print(hash_m(b\"Hello B\"))"
]
},
{
"cell_type": "markdown",
"id": "04132091-21b1-4fbb-99df-711ae5e0c819",
"metadata": {
"slideshow": {
"slide_type": "skip"
},
"tags": []
},
"source": [
"8×8 seems big compared to 3×3 or 4×4 matrixes, but otherwise everything looks normal. The values are as random as you might expect a cryptographic hash to be, and range from 0-255."
]
},
{
"cell_type": "markdown",
"id": "c0c37110-b38d-4420-adf9-11ff5c5cd590",
@ -182,7 +188,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 5,
"id": "eb84e6e1-b1c1-48f4-aa50-3ae0edfc78af",
"metadata": {},
"outputs": [],
@ -196,28 +202,15 @@
"list_hash1 = mul_m(mul_m(element_hashes[0], element_hashes[1]), element_hashes[2])\n",
"# an alternative way to write the reduction\n",
"list_hash2 = reduce(mul_m, element_hashes)\n",
"# check that these alternative spellings are equivalent\n",
"# check that these are equivalent\n",
"assert_equal(list_hash1, list_hash2)"
]
},
{
"cell_type": "markdown",
"id": "5632eb48-e5cd-4ec4-9bcd-1b59a5dc6042",
"metadata": {},
"source": [
"> Expand the sections below to see a comparison"
]
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 6,
"id": "694b4727-621e-4c1b-a2af-99296a8e664a",
"metadata": {
"collapsed": true,
"jupyter": {
"outputs_hidden": true,
"source_hidden": true
},
"tags": []
},
"outputs": [
@ -226,10 +219,10 @@
"output_type": "stream",
"text": [
"List of elements:\n",
"[b'A', b'Hello', b'World']\n",
" [b'A', b'Hello', b'World'] \n",
"\n",
"Hash of each element:\n",
"[array([[ 33, 180, 244, 189, 158, 100, 237, 53],\n",
"List of element hashes:\n",
" [array([[ 33, 180, 244, 189, 158, 100, 237, 53],\n",
" [ 92, 62, 182, 118, 162, 142, 190, 218],\n",
" [246, 216, 241, 123, 220, 54, 89, 149],\n",
" [179, 25, 9, 113, 83, 4, 64, 128],\n",
@ -250,32 +243,26 @@
" [187, 104, 46, 253, 214, 197, 221, 19],\n",
" [193, 23, 224, 139, 212, 170, 239, 113],\n",
" [ 41, 29, 138, 172, 226, 248, 144, 39],\n",
" [ 48, 129, 208, 103, 124, 22, 223, 15]], dtype=uint8)]\n",
" [ 48, 129, 208, 103, 124, 22, 223, 15]], dtype=uint8)] \n",
"\n",
"Hash of full list:\n",
"[[178 188 57 157 60 136 190 127]\n",
"Merklist hash of the list [b'A', b'Hello', b'World'] :\n",
" [[178 188 57 157 60 136 190 127]\n",
" [ 40 234 254 224 38 46 250 52]\n",
" [156 72 193 136 219 98 28 4]\n",
" [197 2 43 132 132 232 254 198]\n",
" [ 93 64 113 215 2 246 130 192]\n",
" [ 91 107 85 13 149 60 19 173]\n",
" [ 84 77 244 98 0 239 123 17]\n",
" [ 58 112 98 250 163 20 27 6]]\n"
" [ 58 112 98 250 163 20 27 6]] \n",
"\n"
]
}
],
"source": [
"#collapse-hide\n",
"#collapse-output\n",
"print(\"List of elements:\")\n",
"print(elements)\n",
"print()\n",
"print(\"Hash of each element:\")\n",
"print(element_hashes)\n",
"print()\n",
"print(\"Hash of full list:\")\n",
"print(list_hash1)\n",
"# Expand the section below to see the output"
"print(\"List of elements:\\n\", elements, \"\\n\")\n",
"print(\"List of element hashes:\\n\", element_hashes, \"\\n\")\n",
"print(\"Merklist hash of the list\", elements, \":\\n\", list_hash1, \"\\n\")"
]
},
{
@ -301,17 +288,7 @@
"\n",
"Upon consideration, these are the exact properties that one would want in order to define the hash of a list of items. Non-commutativity enables the order of elements in the list to be well defined, since swapping different elements produces a different hash. Associativity enables calculating the hash of the list by performing the reduction operations in any order, and you still get the same hash.\n",
"\n",
"Lets sanity-check that these properties can hold for the construction described above."
]
},
{
"cell_type": "markdown",
"id": "c6c8ef5e-99d2-4a7e-887f-54b93a7baf4a",
"metadata": {},
"source": [
"### Associativity\n",
"\n",
"If it's associative, we should get the same hash if we rearrange the parenthesis to indicate reduction in a different operation order. That is: $((e1 × e2) × e3) = (e1 × (e2 × e3))$"
"Lets sanity-check that these properties can hold for the construction described above. For setup, lets consider the hashes of three elements, $he_1$, $he_2$, and $he_3$:"
]
},
{
@ -323,9 +300,19 @@
},
"outputs": [],
"source": [
"e1 = hash_m(b\"Hello A\")\n",
"e2 = hash_m(b\"Hello B\")\n",
"e3 = hash_m(b\"Hello C\")"
"he1 = hash_m(b\"A\")\n",
"he2 = hash_m(b\"B\")\n",
"he3 = hash_m(b\"C\")"
]
},
{
"cell_type": "markdown",
"id": "c6c8ef5e-99d2-4a7e-887f-54b93a7baf4a",
"metadata": {},
"source": [
"### Associativity\n",
"\n",
"If it's associative, we should get the same hash if we rearrange the parenthesis to indicate reduction in a different operation order. $((he_1 × he_2) × he_3) = (he_1 × (he_2 × he_3))$"
]
},
{
@ -337,33 +324,18 @@
},
"outputs": [],
"source": [
"x = np.matmul(np.matmul(e1, e2), e3)\n",
"y = np.matmul(e1, np.matmul(e2, e3))\n",
"x = np.matmul(np.matmul(he1, he2), he3)\n",
"y = np.matmul(he1, np.matmul(he2, he3))\n",
"\n",
"# observe that they produce the same summary\n",
"assert_equal(x, y)"
]
},
{
"cell_type": "markdown",
"id": "bda2bacf-daf7-4f42-93cf-c422704dc067",
"metadata": {
"tags": []
},
"source": [
"> Expand the sections below to see a comparison"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "b7a1906d-524c-4339-920a-978a0385d6cc",
"metadata": {
"collapsed": true,
"jupyter": {
"outputs_hidden": true,
"source_hidden": true
},
"tags": []
},
"outputs": [
@ -371,32 +343,29 @@
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 58 12 144 134 100 158 159 51]\n",
" [ 73 206 202 190 87 79 223 2]\n",
" [210 122 142 117 37 148 106 45]\n",
" [175 146 187 223 235 171 64 226]\n",
" [149 85 203 87 92 251 243 206]\n",
" [ 18 252 160 103 125 251 181 133]\n",
" [191 132 220 104 213 154 34 154]\n",
" [127 197 95 87 166 3 22 3]]\n",
"[[236 186 154 122 127 118 202 95]\n",
" [132 196 196 78 80 160 68 166]\n",
" [208 62 184 219 85 118 143 111]\n",
" [ 7 176 219 162 243 25 29 54]\n",
" [233 49 39 237 233 209 201 221]\n",
" [187 196 20 155 17 241 207 101]\n",
" [132 186 67 153 231 160 68 62]\n",
" [ 8 184 160 149 100 160 189 56]] \n",
"\n",
"[[ 58 12 144 134 100 158 159 51]\n",
" [ 73 206 202 190 87 79 223 2]\n",
" [210 122 142 117 37 148 106 45]\n",
" [175 146 187 223 235 171 64 226]\n",
" [149 85 203 87 92 251 243 206]\n",
" [ 18 252 160 103 125 251 181 133]\n",
" [191 132 220 104 213 154 34 154]\n",
" [127 197 95 87 166 3 22 3]]\n"
" [[236 186 154 122 127 118 202 95]\n",
" [132 196 196 78 80 160 68 166]\n",
" [208 62 184 219 85 118 143 111]\n",
" [ 7 176 219 162 243 25 29 54]\n",
" [233 49 39 237 233 209 201 221]\n",
" [187 196 20 155 17 241 207 101]\n",
" [132 186 67 153 231 160 68 62]\n",
" [ 8 184 160 149 100 160 189 56]]\n"
]
}
],
"source": [
"#collapse-hide\n",
"#collapse-output\n",
"print(x)\n",
"print()\n",
"print(y)"
"print(x, \"\\n\\n\", y)"
]
},
{
@ -406,7 +375,7 @@
"source": [
"### Non-Commutativity\n",
"\n",
"If it's not commutative, then swapping different elements should produce a different hash. That is, $e1 × e2 \\ne e2 × e1$:"
"If it's not commutative, then swapping different elements should produce a different hash. That is, $he_1 × he_2 \\ne he_2 × he_1$:"
]
},
{
@ -418,33 +387,18 @@
},
"outputs": [],
"source": [
"x = np.matmul(e1, e2)\n",
"y = np.matmul(e2, e1)\n",
"x = np.matmul(he1, he2)\n",
"y = np.matmul(he2, he1)\n",
"\n",
"# observe that they produce different summaries\n",
"assert_not_equal(x, y)"
]
},
{
"cell_type": "markdown",
"id": "a8c05183-08db-44f1-b0cc-5fb4aed77b1b",
"metadata": {
"tags": []
},
"source": [
"> Expand the sections below to see a comparison"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "7f833e44-79d8-4c98-af41-0c915bee66ed",
"metadata": {
"collapsed": true,
"jupyter": {
"outputs_hidden": true,
"source_hidden": true
},
"tags": []
},
"outputs": [
@ -452,32 +406,29 @@
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 87 79 149 131 148 247 195 90]\n",
" [249 84 195 58 142 133 211 15]\n",
" [177 93 69 254 240 234 97 37]\n",
" [ 46 84 76 253 55 200 43 236]\n",
" [ 21 84 99 157 55 148 170 2]\n",
" [168 123 6 250 64 144 54 242]\n",
" [230 78 164 76 30 29 214 68]\n",
" [ 47 183 156 239 157 177 192 184]]\n",
"[[ 3 123 188 218 224 245 207 138]\n",
" [ 6 234 158 180 156 14 148 118]\n",
" [ 1 224 198 114 201 102 144 157]\n",
" [ 11 141 205 36 186 218 16 40]\n",
" [ 76 243 130 82 188 146 230 84]\n",
" [211 24 6 46 87 234 89 206]\n",
" [ 16 239 112 27 11 202 139 137]\n",
" [103 30 53 187 203 140 26 146]] \n",
"\n",
"[[149 18 239 238 84 188 191 109]\n",
" [239 150 214 235 59 161 9 133]\n",
" [ 89 174 59 14 70 113 124 243]\n",
" [ 66 113 176 124 227 247 17 25]\n",
" [247 138 152 181 177 143 184 97]\n",
" [113 249 199 153 154 75 45 105]\n",
" [121 201 225 42 249 213 180 244]\n",
" [ 85 31 72 28 181 182 140 176]]\n"
" [[253 223 41 178 183 140 102 18]\n",
" [111 185 54 143 153 96 117 15]\n",
" [109 111 168 203 253 0 102 84]\n",
" [106 74 245 252 178 202 226 201]\n",
" [ 12 105 29 102 167 152 161 226]\n",
" [253 244 27 149 227 247 235 21]\n",
" [121 15 36 5 9 32 10 92]\n",
" [ 42 189 91 117 135 176 52 152]]\n"
]
}
],
"source": [
"#collapse-hide\n",
"#collapse-output\n",
"print(x)\n",
"print()\n",
"print(y)"
"print(x, \"\\n\\n\", y)"
]
},
{
@ -560,9 +511,9 @@
" [ 19 135 80 115 75 242 242 5]\n",
" [244 165 250 28 76 43 188 254]\n",
" [233 46 187 39 151 241 175 130]\n",
" [132 138 6 215 20 132 89 33]]\n",
" [132 138 6 215 20 132 89 33]] \n",
"\n",
"[[ 68 252 159 3 14 52 199 199]\n",
" [[ 68 252 159 3 14 52 199 199]\n",
" [136 124 6 34 58 174 206 54]\n",
" [ 3 234 2 13 120 240 7 163]\n",
" [102 47 66 61 87 234 246 72]\n",
@ -574,9 +525,7 @@
}
],
"source": [
"print(hash1)\n",
"print()\n",
"print(hash2)"
"print(hash1, \"\\n\\n\", hash2)"
]
},
{

Loading…
Cancel
Save