Example #1
0
def pubkey_to_pyelliptic(pubkey_hex):
    """
    Convert a hex BTC public key into a format suitable for pyelliptic.

    @param pubkey_hex: Uncompressed BTC public key in hex format.
    @type pubkey_hex: str

    @return: A pyelliptic-compatible binary key.
    @rtype: str
    """
    # Strip the '04' prefix.
    pubkey_hex_strip = pubkey_hex[2:]

    # Add curve ID and key length.
    pubkey_hex_fmt = "".join((
        BTC_CURVE_OPENSSL_ID_HEX,
        BTC_EC_POINT_LENGTH_HEX,
        pubkey_hex_strip[:2 * BTC_EC_POINT_LENGTH],
        BTC_EC_POINT_LENGTH_HEX,
        pubkey_hex_strip[2 * BTC_EC_POINT_LENGTH:],
    ))

    # Convert to binary and return.
    len_key_bin = len(pubkey_hex_fmt) // 2
    return arithmetic.changebase(pubkey_hex_fmt, 16, 256, minlen=len_key_bin)
    def test_privkey_to_pyelliptic(self):
        privkey_bin_fmt = crypto_util.privkey_to_pyelliptic(self.privkey_hex)

        header_bin = crypto_util.BTC_CURVE_OPENSSL_ID_HEX.decode('hex')
        length_bin = crypto_util.BTC_EC_POINT_LENGTH_HEX.decode('hex')
        privkey_bin = arithmetic.changebase(
            self.privkey_hex, 16, 256, minlen=crypto_util.BTC_EC_POINT_LENGTH)

        self.assertEqual(privkey_bin_fmt[:2], header_bin)
        self.assertEqual(privkey_bin_fmt[2:4], length_bin)
        self.assertEqual(privkey_bin_fmt[4:], privkey_bin)
Example #3
0
    def test_privkey_to_pyelliptic(self):
        privkey_bin_fmt = crypto_util.privkey_to_pyelliptic(self.privkey_hex)

        header_bin = crypto_util.BTC_CURVE_OPENSSL_ID_HEX.decode('hex')
        length_bin = crypto_util.BTC_EC_POINT_LENGTH_HEX.decode('hex')
        privkey_bin = arithmetic.changebase(
            self.privkey_hex, 16, 256, minlen=crypto_util.BTC_EC_POINT_LENGTH
        )

        self.assertEqual(privkey_bin_fmt[:2], header_bin)
        self.assertEqual(privkey_bin_fmt[2:4], length_bin)
        self.assertEqual(privkey_bin_fmt[4:], privkey_bin)
Example #4
0
    def test_pubkey_to_pyelliptic(self):
        pubkey_bin_fmt = crypto_util.pubkey_to_pyelliptic(self.pubkey_hex)

        point_len = crypto_util.BTC_EC_POINT_LENGTH
        header_bin = crypto_util.BTC_CURVE_OPENSSL_ID_HEX.decode('hex')
        length_bin = crypto_util.BTC_EC_POINT_LENGTH_HEX.decode('hex')
        pubkey_bin = arithmetic.changebase(
            self.pubkey_hex_strip, 16, 256, minlen=2*point_len
        )

        self.assertEqual(pubkey_bin_fmt[:2], header_bin)
        self.assertEqual(pubkey_bin_fmt[2:4], length_bin)
        self.assertEqual(pubkey_bin_fmt[4:4+point_len], pubkey_bin[:point_len])
        self.assertEqual(pubkey_bin_fmt[4+point_len:6+point_len], length_bin)
        self.assertEqual(pubkey_bin_fmt[6+point_len:], pubkey_bin[point_len:])
Example #5
0
def privkey_to_pyelliptic(privkey_hex):
    """
    Convert a hex BTC private key into a format suitable for pyelliptic.

    @param privkey_hex: Compressed BTC private key in hex format.
    @type privkey_hex: str

    @return: A pyelliptic-compatible binary key.
    @rtype: str
    """
    # Add curve ID and key length.
    privkey_hex_fmt = "".join(
        (BTC_CURVE_OPENSSL_ID_HEX, BTC_EC_POINT_LENGTH_HEX, privkey_hex))

    # Convert to binary and return.
    len_key_bin = len(privkey_hex_fmt) // 2
    return arithmetic.changebase(privkey_hex_fmt, 16, 256, minlen=len_key_bin)
    def test_pubkey_to_pyelliptic(self):
        pubkey_bin_fmt = crypto_util.pubkey_to_pyelliptic(self.pubkey_hex)

        point_len = crypto_util.BTC_EC_POINT_LENGTH
        header_bin = crypto_util.BTC_CURVE_OPENSSL_ID_HEX.decode('hex')
        length_bin = crypto_util.BTC_EC_POINT_LENGTH_HEX.decode('hex')
        pubkey_bin = arithmetic.changebase(self.pubkey_hex_strip,
                                           16,
                                           256,
                                           minlen=2 * point_len)

        self.assertEqual(pubkey_bin_fmt[:2], header_bin)
        self.assertEqual(pubkey_bin_fmt[2:4], length_bin)
        self.assertEqual(pubkey_bin_fmt[4:4 + point_len],
                         pubkey_bin[:point_len])
        self.assertEqual(pubkey_bin_fmt[4 + point_len:6 + point_len],
                         length_bin)
        self.assertEqual(pubkey_bin_fmt[6 + point_len:],
                         pubkey_bin[point_len:])
Example #7
0
def privkey_to_pyelliptic(privkey_hex):
    """
    Convert a hex BTC private key into a format suitable for pyelliptic.

    @param privkey_hex: Compressed BTC private key in hex format.
    @type privkey_hex: str

    @return: A pyelliptic-compatible binary key.
    @rtype: str
    """
    # Add curve ID and key length.
    privkey_hex_fmt = "".join((
        BTC_CURVE_OPENSSL_ID_HEX,
        BTC_EC_POINT_LENGTH_HEX,
        privkey_hex
    ))

    # Convert to binary and return.
    len_key_bin = len(privkey_hex_fmt) // 2
    return arithmetic.changebase(privkey_hex_fmt, 16, 256, minlen=len_key_bin)
Example #8
0
def pubkey_to_pyelliptic(pubkey_hex):
    """
    Convert a hex BTC public key into a format suitable for pyelliptic.

    @param pubkey_hex: Uncompressed BTC public key in hex format.
    @type pubkey_hex: str

    @return: A pyelliptic-compatible binary key.
    @rtype: str
    """
    # Strip the '04' prefix.
    pubkey_hex_strip = pubkey_hex[2:]

    # Add curve ID and key length.
    pubkey_hex_fmt = "".join((
        BTC_CURVE_OPENSSL_ID_HEX,
        BTC_EC_POINT_LENGTH_HEX, pubkey_hex_strip[:2 * BTC_EC_POINT_LENGTH],
        BTC_EC_POINT_LENGTH_HEX, pubkey_hex_strip[2 * BTC_EC_POINT_LENGTH:],
    ))

    # Convert to binary and return.
    len_key_bin = len(pubkey_hex_fmt) // 2
    return arithmetic.changebase(pubkey_hex_fmt, 16, 256, minlen=len_key_bin)