def toAsn1IntBytes(b):
    """Return a bytearray containing ASN.1 integer based on input bytearray.
    
    An ASN.1 integer is a big-endian sequence of bytes, with excess zero bytes
    at the beginning removed.  However, if the high bit of the first byte 
    would be set, a zero byte is prepended.  Note that the ASN.1 type/len
    fields are NOT added by this function.
    """
    while b[0] == 0 and len(b) > 1:
        b = b[1:]
    if b[0] & 128:
        b = bytearray([0]) + b
    return b
