def compress(pubkey):
    """ Convert 65 bytes starting with 0x04... to compressed form starting
    with either 0x02 or 0x03.

    http://www.secg.org/sec1-v2.pdf#page=17 for large prime field """
    assert(len(pubkey) == 65 and pubkey[0] == 4)
    x = pubkey[1:33]
    ylast = pubkey[64]
    if ylast & 1:
        return b'\x03' + x
    else:
        return b'\x02' + x