def hash_pair(first: Keccak256, second: Optional[Keccak256]) -> Keccak256:
    """ Computes the keccak hash of the elements ordered topologically.

    Since a merkle proof will not include all the elements, but only the path
    starting from the leaves up to the root, the order of the elements is not
    known by the proof checker. The topological order is used as a
    deterministic way of ordering the elements making sure the smart contract
    verification and the python code are compatible.
    """
    assert first is not None

    if second is None:
        return first

    if first > second:
        return sha3(second + first)

    return sha3(first + second)