Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: jscode017 on October 23, 2018, 02:12:01 PM



Title: can't get the same publickey for c and nodejs
Post by: jscode017 on October 23, 2018, 02:12:01 PM
sorry stupid question again

i try to derive a public key from a private key in both node js and c but seems can't get the same result of public key

for my c code (use secp256k1 directly from bitcoin core) 

 unsigned char ctmp[]={0x79,0x54,0x55,0x9D,0xE4,0x26,0xCB,0x23,0x8C,0x9A,0x8A,0x2E,0x45,0x3C,0x75,0x8C,0xCA,0xD9,0x17,0xE1,0xF4,0x66,0x79,0x2A,0x51,0x66,0xEB,0xED,0x59,0x32,0x0F,0x6F};

     secp256k1_context *vrfy = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN);
secp256k1_ec_pubkey_create(vrfy, &pubkey, &ctmp)
and print it out get  0xBB,0x19,0xAB,0xD0,0x74,0xD4,0xA2,0xFE,0x21,0xC9,0xFA,0x3E,0x4E,0x8C,0x46,0x81,0xC3,0x75,0x80,0xE2,0xF3,0x1B,0x3E,0x3D,0xE7,0xC6,0xF9,0xDD,0x44,0x47,0xAD,0xC0,0x36,0xC5,0x03,0x7E,0x3D,0x29,0x2B,0x10,0xBC,0x1B,0x11,0x73,0x55,0xF8,0xE4,0x69,0xB9,0xB6,0x4B,0xCD,0xC9,0xD9,0xD1,0x5D,0xE9,0x13,0x20,0x65,0xF2,0x72,0x3C,0xD6


and for my node js code

i use const secp256k1 = require('secp256k1');

then
PrivateKey="7954559de426cb238c9a8a2e453c758ccad917e1f466792a5166ebed59320f6f";
const pubKey = secp256k1.publicKeyCreate(Buffer.from(PrivateKey,'hex'));
but get c0ad4744ddf9c6e73d3e1bf3e28075c381468c4e3efac921fea2d474d0ab19bbd63c72f2652013e 95dd1d9c9cd4bb6b969e4f85573111bbc102b293d7e03c536


and strangely, when they sign the same message they will result in different signature too!
is it for the format that i used incorrectly?
can some one explain why there is this difference?

help please!!!




Title: Re: can't get the same publickey for c and nodejs
Post by: jscode017 on October 23, 2018, 02:50:53 PM
Thanks for the reply
Sorry i forget to mention when i print out my public key in node js i strip the prefix which is 02 (or sometomes 04)

won't it be weird that node js and c use different standard since secp256k1 is kind of a standard library?

p.s. get same publickey when use
elliptic in and specify secp256k1 in node js
var EC = require('elliptic').ec;
var ec = new EC('secp256k1');


Title: Re: can't get the same publickey for c and nodejs
Post by: achow101 on October 23, 2018, 03:07:09 PM
for my c code (use secp256k1 directly from bitcoin core)  

 unsigned char ctmp[]={0x79,0x54,0x55,0x9D,0xE4,0x26,0xCB,0x23,0x8C,0x9A,0x8A,0x2E,0x45,0x3C,0x75,0x8C,0xCA,0xD9,0x17,0xE1,0xF4,0x66,0x79,0x2A,0x51,0x66,0xEB,0xED,0x59,0x32,0x0F,0x6F};

     secp256k1_context *vrfy = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN);
secp256k1_ec_pubkey_create(vrfy, &pubkey, &ctmp)
and print it out get  0xBB,0x19,0xAB,0xD0,0x74,0xD4,0xA2,0xFE,0x21,0xC9,0xFA,0x3E,0x4E,0x8C,0x46,0x81,0xC3,0x75,0x80,0xE2,0xF3,0x1B,0x3E,0x3D,0xE7,0xC6,0xF9,0xDD,0x44,0x47,0xAD,0xC0,0x36,0xC5,0x03,0x7E,0x3D,0x29,0x2B,0x10,0xBC,0x1B,0x11,0x73,0x55,0xF8,0xE4,0x69,0xB9,0xB6,0x4B,0xCD,0xC9,0xD9,0xD1,0x5D,0xE9,0x13,0x20,0x65,0xF2,0x72,0x3C,0xD6
How are you printing this out? If you are just printing the pubkey object without serializing it, then that is obviously wrong.

If you look closely, you'll notice that 0xBB,0x19,0xAB,0xD0,0x74,0xD4,0xA2,0xFE,0x21,0xC9,0xFA,0x3E,0x4E,0x8C,0x46,0x81,0xC3,0x75,0x80,0xE2,0xF3,0x1B,0x3E,0x3D,0xE7,0xC6,0xF9,0xDD,0x44,0x47,0xAD,0xC0 is the first half of the pubkey that you got from node.js, but with the bytes reversed. Further, 0x36,0xC5,0x03,0x7E,0x3D,0x29,0x2B,0x10,0xBC,0x1B,0x11,0x73,0x55,0xF8,0xE4,0x69,0xB9,0xB6,0x4B,0xCD,0xC9,0xD9,0xD1,0x5D,0xE9,0x13,0x20,0x65,0xF2,0x72,0x3C,0xD6 is the second half of the pubkey, again with the bytes reversed. This will happen if you are just inspecting (or printing) the secp pubkey object itself since internal byte order is not the same as the byte order when actually serialized.


Title: Re: can't get the same publickey for c and nodejs
Post by: jscode017 on October 23, 2018, 03:28:09 PM
Thanks a lot .  :)