def test_three(): def sort_join(first, second): return ''.join(sorted([first, second])) hash_0 = 'a' * 32 hash_1 = 'b' * 32 hash_2 = 'c' * 32 leaves = [hash_0, hash_1, hash_2] tree = Merkletree(leaves) merkle_root = tree.merkleroot hash_01 = ( b'me\xef\x9c\xa9=5\x16\xa4\xd3\x8a\xb7\xd9\x89\xc2\xb5\x00' b'\xe2\xfc\x89\xcc\xdc\xf8x\xf9\xc4m\xaa\xf6\xad\r[' ) assert keccak(hash_0 + hash_1) == hash_01 calculated_root = keccak(hash_2 + hash_01) merkle_proof0 = tree.make_proof(hash_0) assert merkle_proof0 == [hash_1, hash_2] assert merkle_root == calculated_root assert check_proof(merkle_proof0, merkle_root, hash_0) merkle_proof1 = tree.make_proof(hash_1) assert merkle_proof1 == [hash_0, hash_2] assert merkle_root == calculated_root assert check_proof(merkle_proof1, merkle_root, hash_1) # with an odd number of values, the last value wont appear by itself in the # proof since it isn't hashed with another value merkle_proof2 = tree.make_proof(hash_2) assert merkle_proof2 == [keccak(hash_0 + hash_1)] assert merkle_root == calculated_root assert check_proof(merkle_proof2, merkle_root, hash_2)
def hash_pair(first, second): if second is None: return first if first is None: return second if first > second: return keccak(second + first) return keccak(first + second)
def test_duplicates(): hash_0 = keccak('x') hash_1 = keccak('y') assert merkleroot([hash_0, hash_0]) == hash_0, 'duplicates should be removed' assert merkleroot([hash_0, hash_1, hash_0 ]) == merkleroot([hash_0, hash_1 ]), 'duplicates should be removed'
def test_duplicates(): hash_0 = keccak('x') hash_1 = keccak('y') with pytest.raises(ValueError): Merkletree([hash_0, hash_0]) with pytest.raises(ValueError): Merkletree([hash_0, hash_1, hash_0])
def test_duplicates(): hash_0 = keccak('x') hash_1 = keccak('y') assert merkleroot([hash_0, hash_0]) == hash_0, 'duplicates should be removed' result0 = merkleroot([hash_0, hash_1, hash_0]) result1 = merkleroot([hash_0, hash_1]) assert result0 == result1, 'duplicates should be removed'
def test_many(num=10): for nummi in range(1, num + 1): values = [keccak(str(i)) for i in range(nummi)] rvalues = list(reversed(values)) r = do_test_many(values) r0 = do_test_many(rvalues) assert r == r0
def do_test_speed(rounds=100, num_hashes=1000): import time values = [keccak(str(i)) for i in range(num_hashes)] st = time.time() for i in range(rounds): merkleroot(values) elapsed = time.time() - st print '%d additions per second' % (num_hashes * rounds / elapsed)
def test_two(): hash_0 = 'a' * 32 hash_1 = 'b' * 32 merkle_tree = [hash_0, hash_1] merkle_proof = get_proof(merkle_tree, hash_0) merkle_root = merkleroot(merkle_tree) assert merkle_proof == [hash_1] assert merkle_root == keccak(hash_0 + hash_1) assert check_proof(merkle_proof, merkle_root, hash_0) merkle_proof = get_proof(merkle_tree, hash_1) merkle_root = merkleroot(merkle_tree) assert merkle_proof == [hash_0] assert merkle_root == keccak(hash_0 + hash_1) assert check_proof(merkle_proof, merkle_root, hash_1)
def test_two(): hash_0 = 'a' * 32 hash_1 = 'b' * 32 merkle_tree = [hash_0, hash_1] merkle_proof = [hash_0] # modified in place merkle_root = merkleroot(merkle_tree, merkle_proof) assert merkle_proof == [hash_1] assert merkle_root == keccak(hash_0 + hash_1) assert check_proof(merkle_proof, merkle_root, hash_0) merkle_proof = [hash_1] # modified in place merkle_root = merkleroot(merkle_tree, merkle_proof) assert merkle_proof == [hash_0] assert merkle_root == keccak(hash_0 + hash_1) assert check_proof(merkle_proof, merkle_root, hash_1)
def test_two(): hash_0 = 'a' * 32 hash_1 = 'b' * 32 leaves = [hash_0, hash_1] tree = Merkletree(leaves) merkle_root = tree.merkleroot merkle_proof0 = tree.make_proof(hash_0) assert merkle_proof0 == [hash_1] assert merkle_root == keccak(hash_0 + hash_1) assert check_proof(merkle_proof0, merkle_root, hash_0) merkle_proof1 = tree.make_proof(hash_1) assert merkle_proof1 == [hash_0] assert merkle_root == keccak(hash_0 + hash_1) assert check_proof(merkle_proof1, merkle_root, hash_1)
def do_test_speed(rounds=100, num_hashes=1000): values = [keccak(str(i)) for i in range(num_hashes)] start_time = time.time() for __ in range(rounds): Merkletree(values).merkleroot elapsed = time.time() - start_time print '%d additions per second' % (num_hashes * rounds / elapsed)
def test_three(): def sort_join(first, second): return ''.join(sorted([first, second])) hash_0 = 'a' * 32 hash_1 = 'b' * 32 hash_2 = 'c' * 32 merkle_tree = [hash_0, hash_1, hash_2] hash_01 = ( b'me\xef\x9c\xa9=5\x16\xa4\xd3\x8a\xb7\xd9\x89\xc2\xb5\x00' b'\xe2\xfc\x89\xcc\xdc\xf8x\xf9\xc4m\xaa\xf6\xad\r[' ) assert keccak(hash_0 + hash_1) == hash_01 calculated_root = keccak(hash_2 + hash_01) merkle_proof = get_proof(merkle_tree, hash_0) merkle_root = merkleroot(merkle_tree) assert merkle_proof == [hash_1, hash_2] assert merkle_root == calculated_root assert check_proof(merkle_proof, merkle_root, hash_0) merkle_proof = get_proof(merkle_tree, hash_1) merkle_root = merkleroot(merkle_tree) assert merkle_proof == [hash_0, hash_2] assert merkle_root == calculated_root assert check_proof(merkle_proof, merkle_root, hash_1) merkle_proof = get_proof(merkle_tree, hash_2) merkle_root = merkleroot(merkle_tree) # with an odd number of values, the last value wont appear by itself in the # proof since it isn't hashed with another value assert merkle_proof == [keccak(hash_0 + hash_1)] assert merkle_root == calculated_root assert check_proof(merkle_proof, merkle_root, hash_2)
def test_many(tree_up_to=10): for number_of_leaves in range(tree_up_to): merkle_tree = [keccak(str(value)) for value in range(number_of_leaves)] for value in merkle_tree: merkle_proof = [value] merkle_root = merkleroot(merkle_tree, merkle_proof) second_proof = get_proof(merkle_tree, value, merkle_root) assert check_proof(merkle_proof, merkle_root, value) is True assert check_proof(second_proof, merkle_root, value) is True assert merkleroot(merkle_tree) == merkleroot(reversed(merkle_tree))
def test_many(tree_up_to=10): for number_of_leaves in range(tree_up_to): leaves = [ keccak(str(value)) for value in range(number_of_leaves) ] tree = Merkletree(leaves) merkleroot = tree.merkleroot for value in leaves: merkle_proof = tree.make_proof(value) assert check_proof(merkle_proof, merkleroot, value) assert merkleroot == Merkletree(reversed(leaves)).merkleroot
def test_three(): def sort_join(first, second): return ''.join(sorted([first, second])) hash_0 = 'a' * 32 hash_1 = 'b' * 32 hash_2 = 'c' * 32 merkle_tree = [hash_0, hash_1, hash_2] hash_01 = b'me\xef\x9c\xa9=5\x16\xa4\xd3\x8a\xb7\xd9\x89\xc2\xb5\x00\xe2\xfc\x89\xcc\xdc\xf8x\xf9\xc4m\xaa\xf6\xad\r[' assert keccak(hash_0 + hash_1) == hash_01 calculated_root = keccak(hash_2 + hash_01) merkle_proof = [hash_0] # modified in place merkle_root = merkleroot(merkle_tree, merkle_proof) assert merkle_proof == [hash_1, hash_2] assert merkle_root == calculated_root assert check_proof(merkle_proof, merkle_root, hash_0) merkle_proof = [hash_1] # modified in place merkle_root = merkleroot(merkle_tree, merkle_proof) assert merkle_proof == [hash_0, hash_2] assert merkle_root == calculated_root assert check_proof(merkle_proof, merkle_root, hash_1) merkle_proof = [hash_2] # modified in place merkle_root = merkleroot(merkle_tree, merkle_proof) # with an odd number of values, the last value wont appear by itself in the # proof since it isn't hashed with another value assert merkle_proof == [keccak(hash_0 + hash_1)] assert merkle_root == calculated_root assert check_proof(merkle_proof, merkle_root, hash_2)
def test_many(tree_up_to=10): for number_of_leaves in range(tree_up_to): merkle_tree = [ keccak(str(value)) for value in range(number_of_leaves) ] for value in merkle_tree: merkle_proof = get_proof(merkle_tree, value) merkle_root = merkleroot(merkle_tree) second_proof = get_proof(merkle_tree, value, merkle_root) assert check_proof(merkle_proof, merkle_root, value) is True assert check_proof(second_proof, merkle_root, value) is True assert merkleroot(merkle_tree) == merkleroot(reversed(merkle_tree))
def test_duplicates(): h = keccak('x') h1 = keccak('y') assert merkleroot([h, h]) == h assert merkleroot([h, h1, h]) == merkleroot([h, h1])
def hash_pair(first, second): if first > second: return keccak(second + first) return keccak(first + second)
def test_single(): h = keccak('x') assert merkleroot([h]) == h
def test_single(): hash_0 = keccak('x') assert merkleroot([hash_0]) == hash_0
def test_single(): hash_0 = keccak('x') assert Merkletree([hash_0]).merkleroot == hash_0