def compact_encode(i:int) -> bytes:
    """Encodes an integer as a compact int"""
    if i < 0:
        raise ValueError("integer can't be negative: {}".format(i))
    order = 0
    while (i>>(8*(2**order))):
        order += 1
    if order == 0:
        if i < 0xfd:
            return bytes([i])
        order = 1
    if order > 3:
        raise ValueError("integer too large: {}".format(i))
    return bytes([0xfc+order]) + i.to_bytes(2**order, 'little')