def check_hash(hash: bytes, leading_zeros: int)-> bool:
    """Check that the provided hash is prefixed with at least `leading_zeros` zero bits"""
    if leading_zeros >= 32 * 8:
        raise Exception(f"Requirement of {leading_zeros} leading zero bits is impossible; max is {32 * 8}")
    
    # convert bits to bytes, flooring
    leading_zero_bytes = leading_zeros // 8
    # the bits that leak into the first non-zero byte
    remaining_bits = leading_zeros - leading_zero_bytes * 8
    # take 0b11111111 and shift `remaining_bits` leading 0s into it
    # if the byte at index `leading_zero_bytes` exceeds this, there are insufficient leading 0s
    max_first_nonzero_byte = (2 ** 8 - 1) >> remaining_bits
    
    for i in range(leading_zero_bytes):
        if hash[i] != 0:
            return False
    if hash[leading_zero_bytes] > max_first_nonzero_byte:
        return False
    
    return True