def verify_merkle_branch(leaf, branch, depth, index, root): value = leaf for i in range(depth): if index // (2**i) % 2: value = hash(branch[i] + value) else: value = hash(value + branch[i]) return value == root
def compute_merkle_root(leaf_nodes): assert len(leaf_nodes) >= 1 empty_node = b'\x00' * 32 child_nodes = leaf_nodes[:] for i in range(DEPOSIT_CONTRACT_TREE_DEPTH): parent_nodes = [] if len(child_nodes) % 2 == 1: child_nodes.append(empty_node) for j in range(0, len(child_nodes), 2): parent_nodes.append(hash(child_nodes[j] + child_nodes[j + 1])) child_nodes = parent_nodes empty_node = hash(empty_node + empty_node) return child_nodes[0]