Bitcoin Forum
October 22, 2019, 05:38:01 PM *
News: Latest Bitcoin Core release: 0.18.1 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: can't get the same publickey for c and nodejs  (Read 89 times)
jscode017
Newbie
*
Offline Offline

Activity: 22
Merit: 1


View Profile
October 23, 2018, 02:12:01 PM
 #1

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!!!


PLAY NOW
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
1571765881
Hero Member
*
Offline Offline

Posts: 1571765881

View Profile Personal Message (Offline)

Ignore
1571765881
Reply with quote  #2

1571765881
Report to moderator
ETFbitcoin
Legendary
*
Offline Offline

Activity: 1792
Merit: 2056

Use SegWit and enjoy lower fees.


View Profile WWW
October 23, 2018, 02:29:36 PM
Last edit: October 23, 2018, 03:10:19 PM by ETFbitcoin
 #2

I only can guess both secp256k1 libraries use different standard/method. In https://www.npmjs.com/package/secp256k1 mention that it follow Bitcoin Core's secp256k1.

But when i tried your code (Node.js), i got different result which is
Code:
<Buffer 02 c0 ad 47 44 dd f9 c6 e7 3d 3e 1b f3 e2 80 75 c3 81 46 8c 4e 3e fa c9 21 fe a2 d4 74 d0 ab 19 bb>

sorry stupid question again

There's no stupid question

jscode017
Newbie
*
Offline Offline

Activity: 22
Merit: 1


View Profile
October 23, 2018, 02:50:53 PM
 #3

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');
achow101
Moderator
Legendary
*
expert
Offline Offline

Activity: 1918
Merit: 2862


bc1qshxkrpe4arppq89fpzm6c0tpdvx5cfkve2c8kl


View Profile WWW
October 23, 2018, 03:07:09 PM
Merited by bones261 (2), bob123 (2), ETFbitcoin (1), HeRetiK (1)
 #4

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.

jscode017
Newbie
*
Offline Offline

Activity: 22
Merit: 1


View Profile
October 23, 2018, 03:28:09 PM
 #5

Thanks a lot .  Smiley
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!