Example #1
0
    def from_point(cls, x: int, y: int, context: Context = GLOBAL_CONTEXT):
        """
        Derive a public key from a coordinate point in the form `(x, y)`.

        :param x:
        :param y:
        :param context:
        :return: The public key.
        :rtype: PublicKey
        """
        return PublicKey(
            b'\x04' + int_to_bytes_padded(x) + int_to_bytes_padded(y), context)
    def test_transform(self):
        x = urandom(32)
        k = urandom(32)
        point = G.multiply(x)

        assert point.add(k) == G.multiply(
            int_to_bytes_padded((bytes_to_int(x) + bytes_to_int(k)) % n))
Example #3
0
def getPubkey(new_prvkey, flag_compress):
    if flag_gmpy2:
        Ptmp = mul_ka(new_prvkey)
        Xcoord = Ptmp.x
        Ycoord = Ptmp.y
    elif flag_coincurve:
        Ptmp = PublicKey.from_valid_secret(int_to_bytes_padded(new_prvkey))
        if flag_cffi:
            tmp_pubkey = ffi.buffer(Ptmp.public_key, 64)[:]
            Xcoord = bytes_to_int(tmp_pubkey[31::-1])
            Ycoord = bytes_to_int(tmp_pubkey[:31:-1])
        else:
            Xcoord, Ycoord = Ptmp.point()
    else:
        Ptmp = mul_ka(new_prvkey)
        Xcoord = Ptmp.x
        Ycoord = Ptmp.y

    if flag_compress:
        if (Ycoord % 2) == 0:
            new_pubkey = '02%064x' % int(hex(Xcoord)[2:66], 16)
        else:
            new_pubkey = '03%064x' % int(hex(Xcoord)[2:66], 16)
    else:
        new_pubkey = '04%064x%064x' % (int(hex(Xcoord)[2:66],
                                           16), int(hex(Ycoord)[2:66], 16))

    return new_pubkey
Example #4
0
 def from_int(cls, num: int, context: Context = GLOBAL_CONTEXT):
     """
     :param num: The private key as an integer.
     :param context:
     :return: The private key.
     :rtype: PrivateKey
     """
     return PrivateKey(int_to_bytes_padded(num), context)
Example #5
0
 def from_der(cls, der: bytes, context: Context = GLOBAL_CONTEXT):
     """
     :param der: The private key encoded in DER format.
     :param context:
     :return: The private key.
     :rtype: PrivateKey
     """
     return PrivateKey(
         int_to_bytes_padded(
             PrivateKeyInfo.load(der).native['private_key']['private_key']),
         context)
Example #6
0
 def from_pem(cls, pem: bytes, context: Context = GLOBAL_CONTEXT):
     """
     :param pem: The private key encoded in PEM format.
     :param context:
     :return: The private key.
     :rtype: PrivateKey
     """
     return PrivateKey(
         int_to_bytes_padded(
             PrivateKeyInfo.load(
                 pem_to_der(pem)).native['private_key']['private_key']),
         context)
Example #7
0
 def from_point(cls, x, y, context=GLOBAL_CONTEXT):
     return PublicKey(
         b'\x04' + int_to_bytes_padded(x) + int_to_bytes_padded(y), context)
Example #8
0
 def from_der(cls, der, context=GLOBAL_CONTEXT):
     return PrivateKey(
         int_to_bytes_padded(
             PrivateKeyInfo.load(der).native['private_key']['private_key']),
         context)
Example #9
0
 def from_pem(cls, pem, context=GLOBAL_CONTEXT):
     return PrivateKey(
         int_to_bytes_padded(
             PrivateKeyInfo.load(
                 pem_to_der(pem)).native['private_key']['private_key']),
         context)
Example #10
0
 def from_int(cls, num, context=GLOBAL_CONTEXT):
     return PrivateKey(int_to_bytes_padded(num), context)
Example #11
0
def test_bytes_int_conversion_padded():
    bytestr = b'\x00' + urandom(31)
    assert int_to_bytes_padded(bytes_to_int(bytestr)) == bytestr