Created
April 17, 2020 18:29
-
-
Save drbh/574fc06fe3196ce393f62d4925b087fe to your computer and use it in GitHub Desktop.
typed-hash-tree-construction.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"source": [ | |
"import hashlib\n", | |
"import collections\n", | |
"import secrets\n", | |
"\n", | |
"_Node = collections.namedtuple('Node', 'Type Label')\n", | |
"\n", | |
"class Node(_Node):\n", | |
"\tdef _pre_label(self):\n", | |
"\t\treturn ''.join([str(self.Type), str(self.Label)]).encode()\n", | |
"\t\t\n", | |
"\tdef _label(self):\n", | |
"\t\tprelabel = self._pre_label()\n", | |
"\t\treturn hashlib.sha256(prelabel).hexdigest()\t\t\n", | |
"\n", | |
"\n", | |
"\n", | |
"def _build_pairs_parent(left_node, right_node = None):\n", | |
" pre_label = ''.join(\n", | |
" [\n", | |
" str(len(left_node.Type)), left_node.Type,\n", | |
" str(len(str(left_node.Label))), str(left_node.Label)\n", | |
" ]\n", | |
" )\n", | |
" internal_node = hashlib.sha256(pre_label.encode()).hexdigest()\t\n", | |
" return pre_label, internal_node\n", | |
"\n", | |
"def _construct_internals_from_bottom(nodes):\n", | |
" pre_lables = []\n", | |
" internal_nodes = []\n", | |
" it = iter(nodes)\n", | |
" for x in it:\n", | |
" left_node = x\n", | |
" right_node = next(it)\n", | |
" pre_label, internal = _build_pairs_parent(left_node, right_node = None)\n", | |
" pre_lables.append(pre_label)\n", | |
" internal_nodes.append(internal)\n", | |
" return pre_lables, internal_nodes" | |
], | |
"outputs": [], | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true, | |
"outputExpanded": false, | |
"jupyter": { | |
"source_hidden": false, | |
"outputs_hidden": false | |
}, | |
"nteract": { | |
"transient": { | |
"deleting": false | |
} | |
}, | |
"execution": { | |
"iopub.status.busy": "2020-04-17T18:27:19.756Z", | |
"iopub.execute_input": "2020-04-17T18:27:19.759Z", | |
"iopub.status.idle": "2020-04-17T18:27:19.760Z", | |
"shell.execute_reply": "2020-04-17T18:27:19.750Z" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"nodes = [\n", | |
" # typed node I - with entropy\n", | |
" Node(\"name\", \"David\"),\n", | |
" Node(\"\", secrets.token_hex(32)),\n", | |
" \n", | |
" # typed node II - with entropy\n", | |
" Node(\"age\", 40),\n", | |
" Node(\"\", secrets.token_hex(32)),\n", | |
"]\n", | |
"\n", | |
"leaves = [n._label() for n in nodes]\n", | |
"# build internal nodes, using left leaf type and label encoding\n", | |
"pre_lables, internal_nodes = _construct_internals_from_bottom(nodes)" | |
], | |
"outputs": [], | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": true, | |
"outputExpanded": false, | |
"jupyter": { | |
"source_hidden": false, | |
"outputs_hidden": false | |
}, | |
"nteract": { | |
"transient": { | |
"deleting": false | |
} | |
}, | |
"execution": { | |
"iopub.status.busy": "2020-04-17T18:27:19.763Z", | |
"iopub.execute_input": "2020-04-17T18:27:19.764Z", | |
"iopub.status.idle": "2020-04-17T18:27:19.766Z", | |
"shell.execute_reply": "2020-04-17T18:27:19.752Z" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"for x in [0,1]:\n", | |
" print(pre_lables[x])\n", | |
" print(internal_nodes[x])\n", | |
" print()\n", | |
" print(\"\\t\",nodes[x*2])\n", | |
" print(\"\\t\",leaves[x*2])\n", | |
" \n", | |
" print()\n", | |
" print(\"\\t\",nodes[x*2-1])\n", | |
" print(\"\\t\",leaves[x*2-1])\n", | |
" \n", | |
" print()\n", | |
" print()" | |
], | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"4name5David\n", | |
"9569a020d6e6631186d1de2457c98ea2ea7c58d6bf085bc9a4641f2501f715a0\n", | |
"\n", | |
"\t Node(Type='name', Label='David')\n", | |
"\t 571585e97c63d33d5147a9aa5e3c91b290105dacdbf991cda6833ddc86f383a1\n", | |
"\n", | |
"\t Node(Type='', Label='3d6871f01058837adebf3397772c98bddf979549ee31d8c47547cc2118ff8744')\n", | |
"\t 2e87277a6362a5f55787fa6e9d1cd027a804b0f18a1e9365ede4df5b4ca99073\n", | |
"\n", | |
"\n", | |
"3age240\n", | |
"fabe5d6d7f41ce6889346f54dbb0edcf320890e835f5dde4684dfcc395815ce4\n", | |
"\n", | |
"\t Node(Type='age', Label=40)\n", | |
"\t 15e55fa93a940fe2f7c700332397fefab86af5ea72794d38f7895a9b8b2a65fc\n", | |
"\n", | |
"\t Node(Type='', Label='328b71092efbc8569d9320c32d607e132fc05a17e112a9898cef9e7e30c8e2d5')\n", | |
"\t 61c0193b5b36f2673d83d032c2c7b16e7a6cc3ef3b963e3b8af076c9ade9adc7\n", | |
"\n", | |
"\n" | |
] | |
} | |
], | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": true, | |
"outputExpanded": false, | |
"jupyter": { | |
"source_hidden": false, | |
"outputs_hidden": false | |
}, | |
"nteract": { | |
"transient": { | |
"deleting": false | |
} | |
}, | |
"execution": { | |
"iopub.status.busy": "2020-04-17T18:27:19.770Z", | |
"iopub.execute_input": "2020-04-17T18:27:19.772Z", | |
"iopub.status.idle": "2020-04-17T18:27:19.777Z", | |
"shell.execute_reply": "2020-04-17T18:27:19.791Z" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [], | |
"outputs": [], | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true, | |
"outputExpanded": false, | |
"jupyter": { | |
"source_hidden": false, | |
"outputs_hidden": false | |
}, | |
"nteract": { | |
"transient": { | |
"deleting": false | |
} | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [], | |
"outputs": [], | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true, | |
"outputExpanded": false, | |
"jupyter": { | |
"source_hidden": false, | |
"outputs_hidden": false | |
}, | |
"nteract": { | |
"transient": { | |
"deleting": false | |
} | |
} | |
} | |
} | |
], | |
"metadata": { | |
"kernel_info": { | |
"name": "python3" | |
}, | |
"language_info": { | |
"name": "python", | |
"version": "3.7.3", | |
"mimetype": "text/x-python", | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"pygments_lexer": "ipython3", | |
"nbconvert_exporter": "python", | |
"file_extension": ".py" | |
}, | |
"kernelspec": { | |
"argv": [ | |
"/Applications/Xcode.app/Contents/Developer/usr/bin/python3", | |
"-m", | |
"ipykernel_launcher", | |
"-f", | |
"{connection_file}" | |
], | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment