Fix code block flags

This commit is contained in:
infogulch 2021-07-09 01:31:25 +00:00
parent adc0dcb270
commit 77dbe92327

View File

@ -83,7 +83,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 2,
"id": "3ccc7fdc-fa6a-48e3-accb-3c1070b4559c", "id": "7f76f998-94a9-4002-adb3-73ccf88223e2",
"metadata": { "metadata": {
"tags": [] "tags": []
}, },
@ -96,15 +96,12 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "04132091-21b1-4fbb-99df-711ae5e0c819", "id": "11c69415-fac2-46a5-97a3-5a7f58376664",
"metadata": { "metadata": {
"slideshow": {
"slide_type": "skip"
},
"tags": [] "tags": []
}, },
"source": [ "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, "execution_count": 3,
"id": "65aa7c7a-25d5-4971-8780-661f367e45ab", "id": "65aa7c7a-25d5-4971-8780-661f367e45ab",
"metadata": { "metadata": {
"jupyter": {
"source_hidden": true
},
"slideshow": { "slideshow": {
"slide_type": "skip" "slide_type": "skip"
}, },
@ -146,12 +140,24 @@
} }
], ],
"source": [ "source": [
"#collapse-hide\n",
"print(hash_m(b\"Hello A\"))\n", "print(hash_m(b\"Hello A\"))\n",
"print()\n", "print()\n",
"print(hash_m(b\"Hello B\"))" "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", "cell_type": "markdown",
"id": "c0c37110-b38d-4420-adf9-11ff5c5cd590", "id": "c0c37110-b38d-4420-adf9-11ff5c5cd590",
@ -182,7 +188,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 17, "execution_count": 5,
"id": "eb84e6e1-b1c1-48f4-aa50-3ae0edfc78af", "id": "eb84e6e1-b1c1-48f4-aa50-3ae0edfc78af",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -196,28 +202,15 @@
"list_hash1 = mul_m(mul_m(element_hashes[0], element_hashes[1]), element_hashes[2])\n", "list_hash1 = mul_m(mul_m(element_hashes[0], element_hashes[1]), element_hashes[2])\n",
"# an alternative way to write the reduction\n", "# an alternative way to write the reduction\n",
"list_hash2 = reduce(mul_m, element_hashes)\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)" "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", "cell_type": "code",
"execution_count": 18, "execution_count": 6,
"id": "694b4727-621e-4c1b-a2af-99296a8e664a", "id": "694b4727-621e-4c1b-a2af-99296a8e664a",
"metadata": { "metadata": {
"collapsed": true,
"jupyter": {
"outputs_hidden": true,
"source_hidden": true
},
"tags": [] "tags": []
}, },
"outputs": [ "outputs": [
@ -228,7 +221,7 @@
"List of elements:\n", "List of elements:\n",
" [b'A', b'Hello', b'World'] \n", " [b'A', b'Hello', b'World'] \n",
"\n", "\n",
"Hash of each element:\n", "List of element hashes:\n",
" [array([[ 33, 180, 244, 189, 158, 100, 237, 53],\n", " [array([[ 33, 180, 244, 189, 158, 100, 237, 53],\n",
" [ 92, 62, 182, 118, 162, 142, 190, 218],\n", " [ 92, 62, 182, 118, 162, 142, 190, 218],\n",
" [246, 216, 241, 123, 220, 54, 89, 149],\n", " [246, 216, 241, 123, 220, 54, 89, 149],\n",
@ -252,7 +245,7 @@
" [ 41, 29, 138, 172, 226, 248, 144, 39],\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", "\n",
"Hash of full list:\n", "Merklist hash of the list [b'A', b'Hello', b'World'] :\n",
" [[178 188 57 157 60 136 190 127]\n", " [[178 188 57 157 60 136 190 127]\n",
" [ 40 234 254 224 38 46 250 52]\n", " [ 40 234 254 224 38 46 250 52]\n",
" [156 72 193 136 219 98 28 4]\n", " [156 72 193 136 219 98 28 4]\n",
@ -260,22 +253,16 @@
" [ 93 64 113 215 2 246 130 192]\n", " [ 93 64 113 215 2 246 130 192]\n",
" [ 91 107 85 13 149 60 19 173]\n", " [ 91 107 85 13 149 60 19 173]\n",
" [ 84 77 244 98 0 239 123 17]\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": [ "source": [
"#collapse-hide\n",
"#collapse-output\n", "#collapse-output\n",
"print(\"List of elements:\")\n", "print(\"List of elements:\\n\", elements, \"\\n\")\n",
"print(elements)\n", "print(\"List of element hashes:\\n\", element_hashes, \"\\n\")\n",
"print()\n", "print(\"Merklist hash of the list\", elements, \":\\n\", list_hash1, \"\\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"
] ]
}, },
{ {
@ -301,17 +288,7 @@
"\n", "\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", "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", "\n",
"Lets sanity-check that these properties can hold for the construction described above." "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$:"
]
},
{
"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))$"
] ]
}, },
{ {
@ -323,9 +300,19 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"e1 = hash_m(b\"Hello A\")\n", "he1 = hash_m(b\"A\")\n",
"e2 = hash_m(b\"Hello B\")\n", "he2 = hash_m(b\"B\")\n",
"e3 = hash_m(b\"Hello C\")" "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": [], "outputs": [],
"source": [ "source": [
"x = np.matmul(np.matmul(e1, e2), e3)\n", "x = np.matmul(np.matmul(he1, he2), he3)\n",
"y = np.matmul(e1, np.matmul(e2, e3))\n", "y = np.matmul(he1, np.matmul(he2, he3))\n",
"\n", "\n",
"# observe that they produce the same summary\n", "# observe that they produce the same summary\n",
"assert_equal(x, y)" "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", "cell_type": "code",
"execution_count": 9, "execution_count": 9,
"id": "b7a1906d-524c-4339-920a-978a0385d6cc", "id": "b7a1906d-524c-4339-920a-978a0385d6cc",
"metadata": { "metadata": {
"collapsed": true,
"jupyter": {
"outputs_hidden": true,
"source_hidden": true
},
"tags": [] "tags": []
}, },
"outputs": [ "outputs": [
@ -371,32 +343,29 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"[[ 58 12 144 134 100 158 159 51]\n", "[[236 186 154 122 127 118 202 95]\n",
" [ 73 206 202 190 87 79 223 2]\n", " [132 196 196 78 80 160 68 166]\n",
" [210 122 142 117 37 148 106 45]\n", " [208 62 184 219 85 118 143 111]\n",
" [175 146 187 223 235 171 64 226]\n", " [ 7 176 219 162 243 25 29 54]\n",
" [149 85 203 87 92 251 243 206]\n", " [233 49 39 237 233 209 201 221]\n",
" [ 18 252 160 103 125 251 181 133]\n", " [187 196 20 155 17 241 207 101]\n",
" [191 132 220 104 213 154 34 154]\n", " [132 186 67 153 231 160 68 62]\n",
" [127 197 95 87 166 3 22 3]]\n", " [ 8 184 160 149 100 160 189 56]] \n",
"\n", "\n",
"[[ 58 12 144 134 100 158 159 51]\n", " [[236 186 154 122 127 118 202 95]\n",
" [ 73 206 202 190 87 79 223 2]\n", " [132 196 196 78 80 160 68 166]\n",
" [210 122 142 117 37 148 106 45]\n", " [208 62 184 219 85 118 143 111]\n",
" [175 146 187 223 235 171 64 226]\n", " [ 7 176 219 162 243 25 29 54]\n",
" [149 85 203 87 92 251 243 206]\n", " [233 49 39 237 233 209 201 221]\n",
" [ 18 252 160 103 125 251 181 133]\n", " [187 196 20 155 17 241 207 101]\n",
" [191 132 220 104 213 154 34 154]\n", " [132 186 67 153 231 160 68 62]\n",
" [127 197 95 87 166 3 22 3]]\n" " [ 8 184 160 149 100 160 189 56]]\n"
] ]
} }
], ],
"source": [ "source": [
"#collapse-hide\n",
"#collapse-output\n", "#collapse-output\n",
"print(x)\n", "print(x, \"\\n\\n\", y)"
"print()\n",
"print(y)"
] ]
}, },
{ {
@ -406,7 +375,7 @@
"source": [ "source": [
"### Non-Commutativity\n", "### Non-Commutativity\n",
"\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": [], "outputs": [],
"source": [ "source": [
"x = np.matmul(e1, e2)\n", "x = np.matmul(he1, he2)\n",
"y = np.matmul(e2, e1)\n", "y = np.matmul(he2, he1)\n",
"\n", "\n",
"# observe that they produce different summaries\n", "# observe that they produce different summaries\n",
"assert_not_equal(x, y)" "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", "cell_type": "code",
"execution_count": 11, "execution_count": 11,
"id": "7f833e44-79d8-4c98-af41-0c915bee66ed", "id": "7f833e44-79d8-4c98-af41-0c915bee66ed",
"metadata": { "metadata": {
"collapsed": true,
"jupyter": {
"outputs_hidden": true,
"source_hidden": true
},
"tags": [] "tags": []
}, },
"outputs": [ "outputs": [
@ -452,32 +406,29 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"[[ 87 79 149 131 148 247 195 90]\n", "[[ 3 123 188 218 224 245 207 138]\n",
" [249 84 195 58 142 133 211 15]\n", " [ 6 234 158 180 156 14 148 118]\n",
" [177 93 69 254 240 234 97 37]\n", " [ 1 224 198 114 201 102 144 157]\n",
" [ 46 84 76 253 55 200 43 236]\n", " [ 11 141 205 36 186 218 16 40]\n",
" [ 21 84 99 157 55 148 170 2]\n", " [ 76 243 130 82 188 146 230 84]\n",
" [168 123 6 250 64 144 54 242]\n", " [211 24 6 46 87 234 89 206]\n",
" [230 78 164 76 30 29 214 68]\n", " [ 16 239 112 27 11 202 139 137]\n",
" [ 47 183 156 239 157 177 192 184]]\n", " [103 30 53 187 203 140 26 146]] \n",
"\n", "\n",
"[[149 18 239 238 84 188 191 109]\n", " [[253 223 41 178 183 140 102 18]\n",
" [239 150 214 235 59 161 9 133]\n", " [111 185 54 143 153 96 117 15]\n",
" [ 89 174 59 14 70 113 124 243]\n", " [109 111 168 203 253 0 102 84]\n",
" [ 66 113 176 124 227 247 17 25]\n", " [106 74 245 252 178 202 226 201]\n",
" [247 138 152 181 177 143 184 97]\n", " [ 12 105 29 102 167 152 161 226]\n",
" [113 249 199 153 154 75 45 105]\n", " [253 244 27 149 227 247 235 21]\n",
" [121 201 225 42 249 213 180 244]\n", " [121 15 36 5 9 32 10 92]\n",
" [ 85 31 72 28 181 182 140 176]]\n" " [ 42 189 91 117 135 176 52 152]]\n"
] ]
} }
], ],
"source": [ "source": [
"#collapse-hide\n",
"#collapse-output\n", "#collapse-output\n",
"print(x)\n", "print(x, \"\\n\\n\", y)"
"print()\n",
"print(y)"
] ]
}, },
{ {
@ -574,9 +525,7 @@
} }
], ],
"source": [ "source": [
"print(hash1)\n", "print(hash1, \"\\n\\n\", hash2)"
"print()\n",
"print(hash2)"
] ]
}, },
{ {