def subtract(lhs: list[int], rhs: list[int], base: int) -> list[int]:
    """
    Subtracts two arbitrary-precision values in some base.

    Given two arrays lhs and rhs of digits in some base 'base,' returns an
    array of the number lhs - rhs encoded in base 'base.'  It is assumed that
    lhs >= rhs; an error occurs if this is not the case.
    """

    # Pad the two inputs to be the same length.
    length = max(len(lhs), len(rhs))
    lhs = [0] * (length - len(lhs)) + lhs
    rhs = [0] * (length - len(rhs)) + rhs

    # Track the result.  We'll build the array up in reverse to avoid costly
    # prepend operations that aren't relevant.
    result = []

    # Iterate across the digits in reverse, subtracting the values.
    for i in range(1, len(lhs) + 1):
        # Compute the difference in this column.
        difference = lhs[-i] - rhs[-i]

        # If we can subtract without borrowing, do so.
        if difference >= 0:
            result.append(difference)
            continue

        # Otherwise, we have to borrow from previous columns.
        for j in range(i + 1, length + 1):
            # Drop the value of this digit by one.  We do this by adding
            # base - 1 and then modding by base, which is equivalent to
            # subtracting one modulo base.
            lhs[-j] = (lhs[-j] + (base - 1)) % base

            # If this value didn't wrap around (i.e. its value is not
            # base - 1), then we're done.  Otherwise, we have to go to the
            # previous digit.
            if lhs[-j] != base - 1:
                break

        # Finally, update the result by appending the correct value.
        # Since we just computed lhs - rhs, we actually want to compute
        # (base + lhs) - rhs.
        result.append(difference + base)

    # Reverse the order of the resulting digits; that's the more proper way
    # to hand them back.
    result.reverse()

    return result