def BigIntToBytes(n):
    """Return a big-endian byte string representation of an arbitrary length n."""
    array = []
    while n > 0:
        array.append(n % 256)
        n = n >> 8
    array.reverse()
    barray = bytearray(array)
    if barray[0] & 128:
        return b'\x00' + bytes(barray)
    return bytes(barray)
