├── Bitcoin_example.png ├── MerkleTrees.py ├── README.md ├── SHA_256.png ├── Simple_explanation_about_Merkle_tree.docx └── merkle_tree_root.png /Bitcoin_example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JaeDukSeo/Simple-Merkle-Tree-in-Python/2baea737d5924e5a95e4c87c63dc13800c9d2fca/Bitcoin_example.png -------------------------------------------------------------------------------- /MerkleTrees.py: -------------------------------------------------------------------------------- 1 | # 0. Import the needed library 2 | import hashlib,json 3 | from collections import OrderedDict 4 | 5 | # 1. Declare the class trees 6 | class Jae_MerkTree: 7 | 8 | # 2. Initiate the class object 9 | def __init__(self,listoftransaction=None): 10 | self.listoftransaction = listoftransaction 11 | self.past_transaction = OrderedDict() 12 | 13 | # 3. Create the Merkle Tree 14 | def create_tree(self): 15 | 16 | # 3.0 Continue on the declaration 17 | listoftransaction = self.listoftransaction 18 | past_transaction = self.past_transaction 19 | temp_transaction = [] 20 | 21 | # 3.1 Loop until the list finishes 22 | for index in range(0,len(listoftransaction),2): 23 | 24 | # 3.2 Get the most left element 25 | current = listoftransaction[index] 26 | 27 | # 3.3 If there is still index left get the right of the left most element 28 | if index+1 != len(listoftransaction): 29 | current_right = listoftransaction[index+1] 30 | 31 | # 3.4 If we reached the limit of the list then make a empty string 32 | else: 33 | current_right = '' 34 | 35 | # 3.5 Apply the Hash 256 function to the current values 36 | current_hash = hashlib.sha256(current) 37 | 38 | # 3.6 If the current right hash is not a '' <- empty string 39 | if current_right != '': 40 | current_right_hash = hashlib.sha256(current_right) 41 | 42 | # 3.7 Add the Transaction to the dictionary 43 | past_transaction[listoftransaction[index]] = current_hash.hexdigest() 44 | 45 | # 3.8 If the next right is not empty 46 | if current_right != '': 47 | past_transaction[listoftransaction[index+1]] = current_right_hash.hexdigest() 48 | 49 | # 3.9 Create the new list of transaction 50 | if current_right != '': 51 | temp_transaction.append(current_hash.hexdigest() + current_right_hash.hexdigest()) 52 | 53 | # 3.01 If the left most is an empty string then only add the current value 54 | else: 55 | temp_transaction.append(current_hash.hexdigest()) 56 | 57 | # 3.02 Update the variables and rerun the function again 58 | if len(listoftransaction) != 1: 59 | self.listoftransaction = temp_transaction 60 | self.past_transaction = past_transaction 61 | 62 | # 3.03 Call the function repeatly again and again until we get the root 63 | self.create_tree() 64 | 65 | # 4. Return the past Transaction 66 | def Get_past_transacion(self): 67 | return self.past_transaction 68 | 69 | # 5. Get the root of the transaction 70 | def Get_Root_leaf(self): 71 | last_key = self.past_transaction.keys()[-1] 72 | return self.past_transaction[last_key] 73 | 74 | # Declare the main part of the function to run 75 | if __name__ == "__main__": 76 | 77 | # a) Create the new class of Jae_MerkTree 78 | Jae_Tree = Jae_MerkTree() 79 | 80 | # b) Give list of transaction 81 | transaction = ['a','b','c','d'] 82 | 83 | # c) pass on the transaction list 84 | Jae_Tree.listoftransaction = transaction 85 | 86 | # d) Create the Merkle Tree transaction 87 | Jae_Tree.create_tree() 88 | 89 | # e) Retrieve the transaction 90 | past_transaction = Jae_Tree.Get_past_transacion() 91 | 92 | # f) Get the last transaction and print all 93 | print "First Example - Even number of transaction Merkel Tree" 94 | print 'Final root of the tree : ',Jae_Tree.Get_Root_leaf() 95 | print(json.dumps(past_transaction, indent=4)) 96 | print "-" * 50 97 | 98 | # h) Second example 99 | print "Second Example - Odd number of transaction Merkel Tree" 100 | Jae_Tree = Jae_MerkTree() 101 | transaction = ['a','b','c','d','e'] 102 | Jae_Tree.listoftransaction = transaction 103 | Jae_Tree.create_tree() 104 | past_transaction = Jae_Tree.Get_past_transacion() 105 | print 'Final root of the tree : ',Jae_Tree.Get_Root_leaf() 106 | print(json.dumps(past_transaction, indent=4)) 107 | print "-" * 50 108 | 109 | # i) Actual Use Case 110 | print "Final Example - Actuall use case of the Merkle Tree" 111 | 112 | # i-1) Declare a transaction - the ground truth 113 | ground_truth_Tree = Jae_MerkTree() 114 | ground_truth_transaction = ['a','b','c','d','e'] 115 | ground_truth_Tree.listoftransaction = ground_truth_transaction 116 | ground_truth_Tree.create_tree() 117 | ground_truth_past_transaction = ground_truth_Tree.Get_past_transacion() 118 | ground_truth_root = ground_truth_Tree.Get_Root_leaf() 119 | 120 | # i-2) Declare a tampered transaction 121 | tampered_Tree = Jae_MerkTree() 122 | tampered_Tree_transaction = ['a','b','c','d','f'] 123 | tampered_Tree.listoftransaction = tampered_Tree_transaction 124 | tampered_Tree.create_tree() 125 | tampered_Tree_past_transaction = tampered_Tree.Get_past_transacion() 126 | tampered_Tree_root = tampered_Tree.Get_Root_leaf() 127 | 128 | # i-3) The three company share all of the transaction 129 | print 'Company A - my final transaction hash : ',ground_truth_root 130 | print 'Company B - my final transaction hash : ',ground_truth_root 131 | print 'Company C - my final transaction hash : ',tampered_Tree_root 132 | 133 | # i-4) Print out all of the past transaction 134 | print "\n\nGround Truth past Transaction " 135 | print(json.dumps(ground_truth_past_transaction, indent=4)) 136 | 137 | print "\n\nTamper Truth past Transaction " 138 | print(json.dumps(tampered_Tree_past_transaction, indent=4)) 139 | 140 | 141 | 142 | 143 | 144 | # ---- END OF THE CODE ------ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Simple-Merkle-Tree-in-Python 2 | This folder contains a short implementation of Merkle Tree. Using the SHA-256 hash function. 3 | 4 | # For video explanation please visit my youtube channel. 5 | https://www.youtube.com/playlist?list=PLz3iwtnWFin9eCMLr5ecphy7rHQNeMheE 6 | 7 | # If my explanation was not good (lol it is bad) please go to this URL for more info. 8 | https://brilliant.org/wiki/merkle-tree/ 9 | 10 | ## Getting Started 11 | To run the code please type 12 | python MerkleTrees.py 13 | 14 | or for python 3 15 | 16 | python3 MerkleTrees.py 17 | 18 | ## Deployment 19 | 20 | Add additional notes about how to deploy this on a live system 21 | 22 | ## Built With 23 | *python2 24 | 25 | 26 | ## Authors 27 | 28 | * Jae Duk Seo 29 | 30 | ## License 31 | 32 | This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details 33 | 34 | ## Acknowledgments 35 | 36 | 37 | * Inspiration - Dr.Ali Miri 38 | 39 | 40 | -------------------------------------------------------------------------------- /SHA_256.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JaeDukSeo/Simple-Merkle-Tree-in-Python/2baea737d5924e5a95e4c87c63dc13800c9d2fca/SHA_256.png -------------------------------------------------------------------------------- /Simple_explanation_about_Merkle_tree.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JaeDukSeo/Simple-Merkle-Tree-in-Python/2baea737d5924e5a95e4c87c63dc13800c9d2fca/Simple_explanation_about_Merkle_tree.docx -------------------------------------------------------------------------------- /merkle_tree_root.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JaeDukSeo/Simple-Merkle-Tree-in-Python/2baea737d5924e5a95e4c87c63dc13800c9d2fca/merkle_tree_root.png --------------------------------------------------------------------------------