Bitcoin Forum
February 29, 2020, 07:04:29 AM *
News: Latest Bitcoin Core release: 0.19.0.1 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Creating a public key from a private key using secp256k1 library in C++  (Read 90 times)
chizisch
Newbie
*
Offline Offline

Activity: 3
Merit: 7


View Profile
February 13, 2020, 06:54:44 PM
Merited by bones261 (4), vapourminer (1), ETFbitcoin (1), pooya87 (1)
 #1

Hello, 

I'm trying to generate a public key from a private one using the bitcoin library secp256k1 https://github.com/bitcoin-core/secp256k1 in C++:
Code:
    // private key
    std::string privateKey = "baca891f5f0285e043496843d82341d15533f016c223d114e1e4dfd39e60ecb0";
    const char* cPrivateKey = privateKey.c_str();

    // creating public key from it
    secp256k1_context  *signingContext = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
    secp256k1_pubkey pkey;

    unsigned char *seckey = (unsigned char*) malloc(privateKey.size() * sizeof(unsigned char));
    std::copy(cPrivateKey, cPrivateKey + privateKey.size(), seckey);

    if (secp256k1_ec_pubkey_create(signingContext, &pkey, seckey) == 0) throw "Creation error";

    // print the result in hex
    std::stringstream ss;
    for (int i = 0; i < 64; ++i)
    {
        ss <<  std::setw(2) << std::hex << (0xff & (unsigned int)pkey.data[i]);
    }

    std::cout << ss.str() << std::endl;

    // parsing the result
    std::string pkey_data  = ss.str();
    secp256k1_context *noneContext = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
    secp256k1_pubkey pubkey;

    if (secp256k1_ec_pubkey_parse(noneContext, &pubkey, pkey.data, 64) == 0) std::cout << "Couldnt parse using pkey.data" << std::endl;
    if (secp256k1_ec_pubkey_parse(noneContext, &pubkey, pkay_data, pkey_data.size()) == 0) std::cout << "Couldnt parse using hex public key " << std::endl;

The output:

Code:
1b9e55408c5141414e8337adef57ead18f62444fdf5f8c897d0dc812696a6141a919254d3e750075a2a9ba32dc4ed30c84e65f27e431b59b94a2aafe3e80a974
Couldnt parse using pkey.data
Couldnt parse using hex public key

The idea is to parse the public key from the generated one to see if it is really correct. Also, i tried using openssl with the same private key and i get a different public key from the private one using ecdsa secp256k1..

Any help or example on how to use the library in C++ is more than welcome.

Thank you  Grin Grin
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
1582959869
Hero Member
*
Offline Offline

Posts: 1582959869

View Profile Personal Message (Offline)

Ignore
1582959869
Reply with quote  #2

1582959869
Report to moderator
1582959869
Hero Member
*
Offline Offline

Posts: 1582959869

View Profile Personal Message (Offline)

Ignore
1582959869
Reply with quote  #2

1582959869
Report to moderator
1582959869
Hero Member
*
Offline Offline

Posts: 1582959869

View Profile Personal Message (Offline)

Ignore
1582959869
Reply with quote  #2

1582959869
Report to moderator
A-Bolt
Legendary
*
Offline Offline

Activity: 1517
Merit: 1367


View Profile
February 13, 2020, 10:01:52 PM
Last edit: February 13, 2020, 10:18:15 PM by A-Bolt
Merited by bones261 (4), ETFbitcoin (1)
 #2

Private key must be a 32-byte char array, not 64-byte hex string.

pkey.data is an internal representation of pubkey object. Don't use it.

To get serialized pubkey use secp256k1_ec_pubkey_serialize function.

He xoтитe видeть этy пoдпиcь?
Oтключитe пoкaз пoдпиceй!
chizisch
Newbie
*
Offline Offline

Activity: 3
Merit: 7


View Profile
February 14, 2020, 12:12:47 AM
 #3

Hello,
Thank you so much for your answer.

Also, I have an additional newbie question as i am still not so good with data representations in C++ and how to deal with them.
how do i convert a hex string to a 32bytes char  array?
I mean, if i convert that private key to ascii format it is gonna be 32-bytes, however it is just garbage non printable symbols most of time.

Also, if you could guide me for some ressources i could use to well understand these conversions etc.. i will be more than grateful.


Thank you again.
pooya87
Legendary
*
Offline Offline

Activity: 1932
Merit: 2390


Remember tonight for it's the beginning of forever


View Profile
February 14, 2020, 03:04:26 AM
Merited by vapourminer (1)
 #4

how do i convert a hex string to a 32bytes char  array?

the stackoverflow website is where you should go for these basic programming questions. here is two links that would answer your question:
https://stackoverflow.com/questions/17261798/converting-a-hex-string-to-a-byte-array
https://stackoverflow.com/questions/3221170/how-to-turn-a-hex-string-into-an-unsigned-char-array

Also, if you could guide me for some ressources i could use to well understand these conversions etc.. i will be more than grateful.
just use google to find out what each encoding is. for example for hexadecimal or base-16 encoding you can read the wikipedia article: https://en.wikipedia.org/wiki/Hexadecimal

chizisch
Newbie
*
Offline Offline

Activity: 3
Merit: 7


View Profile
February 14, 2020, 09:26:01 AM
 #5

Thank you so much for your help  Grin Grin Grin Grin
Pages: [1]
  Print  
 
Jump to:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!