def test_two_element_valid(self): mt = merkle_tree.MT(data.TWO_DATA) expected_digest = Encode.sha256((Encode.sha256(data.TWO_DATA[0]) + Encode.sha256(data.TWO_DATA[1]))) self.assertEqual(expected_digest, mt.digest) self.assertEqual(1, mt.max_height) self.assertEqual(3, len(mt.nodes.keys()))
def __generate_next_hash_by_hash(self, pair, given_hash): if pair.child.parent_a.hash_value == given_hash: hash_next_height = Encode.sha256(given_hash + pair.hash_value) elif pair.child.parent_b.hash_value == given_hash: hash_next_height = Encode.sha256(pair.hash_value + given_hash) else: return None return hash_next_height
def test_three_elements_merkle_proof(self): mt = merkle_tree.MT(data.THREE_DATA) last_post = Encode.sha256(data.THREE_DATA[-1]) path = mt.get_branch_by_hash(last_post) self.assertTrue( mt.merkle_proof(Encode.sha256(data.THREE_DATA[2]), [ Encode.sha256( Encode.sha256(data.THREE_DATA[0]) + Encode.sha256(data.THREE_DATA[1])) ])) self.assertTrue(mt.merkle_proof(last_post, path))
def test_all_data_merkle_proof_and_valid_tree(self): for data_row in data.ALL_DATA: mt = merkle_tree.MT(data_row) secure_random = random.SystemRandom() post = Encode.sha256(secure_random.choice(data_row)) path = mt.get_branch_by_hash(post) self.assertTrue(mt.merkle_proof(post, path)) path = mt.get_branch_by_hash(post) false_post = "Not in the tree data" false_post = Encode.sha256(false_post) self.assertFalse(mt.merkle_proof(false_post, path)) number_of_nodes = pow(2, math.log(len(data_row), 2) + 1) - 1 self.assertEqual(round(number_of_nodes), len(mt.nodes))
def test_six_elements_merkle_proof(self): mt = merkle_tree.MT(data.SIX_DATA) post_valid_hash = Encode.sha256(data.SIX_DATA[3]) path = mt.get_branch_by_hash(post_valid_hash) list_nodes = list(mt.nodes) self.assertTrue( mt.merkle_proof( list_nodes[3], [list_nodes[10], list_nodes[9], list_nodes[2], list_nodes[4]])) # Correct case where the path is corresponding with the hash of the data self.assertTrue(mt.merkle_proof(post_valid_hash, path)) path = mt.get_branch_by_hash(post_valid_hash) # Error case where the path is not corresponding with the hash of the data post_error_hash = Encode.sha256(data.SIX_DATA[1]) self.assertFalse(mt.merkle_proof(post_error_hash, path))
def __create_child(self, parent_a, parent_b): child_hash = Encode.sha256(parent_a.hash_value + parent_b.hash_value) # print("Child Hash :" + child_hash + " parent_a: " + str(parent_a.hash_value) + # "parent_b: " + str(parent_b.hash_value)) # Inserting of the new Node as a child of the two parents child = Node(parent_a, parent_b, child_hash) self.nodes[child_hash] = child parent_a.child = child parent_b.child = child return child
from src.encryption import Encode from src import merkle_tree if __name__ == '__main__': print("Running the Merkle Tree application") posts = ["Hello", "World", "Nice", "To", "Meet", "You", "Friend"] mt = merkle_tree.MT(posts) mt.print_merkle_tree() last_post = Encode.sha256(posts[-1]) path = mt.get_branch_by_hash(last_post) print("Path:"+str(path)) if mt.merkle_proof(last_post, path) is True: print("Hash:"+str(last_post)+ "is in the tree !") else: print("Hash:" + str(last_post) + " is NOT in the tree !")
def __add_leaves(self, items): for item in items: self.leaves.append(Node(None, None, Encode.sha256(item)))
def test_one_elem_valid(self): mt = merkle_tree.MT(data.ONE_DATA) expected_digest = Encode.sha256(data.ONE_DATA[0]) self.assertTrue(mt.merkle_proof(expected_digest, [])) self.assertEqual(0, mt.max_height) self.assertEqual(mt.digest, expected_digest)