Esempio n. 1
0
def test_single_element_merkle_proof():
    leaves = (b"1", )
    tree = calc_merkle_tree(leaves)
    root = get_root(tree)
    assert get_merkle_proof(tree, 0) == ()
    assert verify_merkle_proof(root, b"1", 0, ())
    assert not verify_merkle_proof(b"\x00" * 32, b"1", 0, ())
    assert not verify_merkle_proof(root, b"2", 0, ())
    assert not verify_merkle_proof(root, b"1", 0, (b"\x00" * 32, ))
Esempio n. 2
0
def test_merkle_proofs(leaves, index, proof):
    tree = calc_merkle_tree(leaves)
    root = get_root(tree)
    item = leaves[index]
    calculated_proof = get_merkle_proof(tree, index)
    assert calculated_proof == proof
    assert verify_merkle_proof(root, item, index, calculated_proof)

    assert not verify_merkle_proof(b"\x00" * 32, item, index, proof)
    assert not verify_merkle_proof(root, b"\x00" * 32, index, proof)
    assert not verify_merkle_proof(root, item,
                                   (index + 1) % len(leaves), proof)
    for replaced_index in range(len(proof)):
        altered_proof = proof[:replaced_index] + (
            b"\x00" * 32, ) + proof[replaced_index + 1:]
        assert not verify_merkle_proof(root, item, index, altered_proof)
Esempio n. 3
0
def test_merkle_tree_calculation(leaves, tree):
    calculated_tree = calc_merkle_tree(leaves)
    assert calculated_tree == tree
    assert get_root(tree) == tree[0][0]
    assert calc_merkle_root(leaves) == get_root(tree)
Esempio n. 4
0
def test_proof_generation_index_validation(leaves):
    tree = calc_merkle_tree(leaves)
    for invalid_index in [-1, len(leaves)]:
        with pytest.raises(ValidationError):
            get_merkle_proof(tree, invalid_index)