def base256_encode(n, minwidth=0):
    """
    Encode the input with base256.

    Args:
        n (int): input value.
        minwidth: minimum return value length.

    Raises:
        ValueError: if a negative number is provided.

    Returns:
        bytearray:
    """
    if n > 0:
        arr = []
        while n:
            n, rem = divmod(n, 256)
            arr.append(rem)
        b = bytearray(reversed(arr))
    elif n == 0:
        b = bytearray(b'\x00')
    else:
        raise ValueError('Negative numbers not supported')
    if minwidth > 0 and len(b) < minwidth:
        padding = (minwidth - len(b)) * b'\x00'
        b = bytearray(padding) + b
    b.reverse()
    return b
