paulmillr / noble-secp256k1

Fastest 4KB JS implementation of secp256k1 signatures and ECDH
https://paulmillr.com/noble
MIT License
757 stars 114 forks source link

Getting SharedSecret using a privateKey and other publicKey value. #20

Closed cowboy9 closed 3 years ago

cowboy9 commented 3 years ago

Hi @paulmillr Thanks for creating a great module. By the way, I'm having some issues using this module. Could you help me? The following is the code to calculate sharedSecret using "secp256k1-native" npm module.

const secp256k1 = require('secp256k1-native');
const ctx = secp256k1.secp256k1_context_create(secp256k1.secp256k1_context_SIGN);
const sharedSecrect = Buffer.alloc(32);
const point = Buffer.from('A8Nd1J1A+akKMfHe7j8oZkfLf1QDOkTynN27zr9nBgK3', 'base64'); // B- public key
const scalar = Buffer.from('88e5c2a323f371a43abee6ff28966f7f0cd267e65063aeba9033037c7d93d87d', 'hex'); // A- private key
const pubkey64 = Buffer.alloc(64);
secp256k1.secp256k1_ec_pubkey_parse(ctx, pubkey64, point);
secp256k1.secp256k1_ecdh(ctx, sharedSecrect, pubkey64, scalar, null);
console.log('shared secret:', sharedSecrect); // 028c3425493529dddc51a5d2d2de8af9e68e5e835e883d687b4f2ec421a2640c

The result is "028c3425493529dddc51a5d2d2de8af9e68e5e835e883d687b4f2ec421a2640c", but it's different when I use "noble-secp256k1" module.

import * as secp from 'noble-secp256k1';
  const point = Buffer.from('A8Nd1J1A+akKMfHe7j8oZkfLf1QDOkTynN27zr9nBgK3', 'base64'); // B-JS public key

  const scalar = Buffer.from('88e5c2a323f371a43abee6ff28966f7f0cd267e65063aeba9033037c7d93d87d', 'hex'); // A-JS private key

  const sharedSecret = secp.getSharedSecret(scalar, point);
  console.log("sharedSecret:", Buffer.from(sharedSecret).toString('hex')); //04602586145e1e0aeb17289e68e06bb672a03b708cb2c0ce907678903656f181b666c9eada9d630fefab93e02e131b7f28046fc970cdf5e86f640b100851593137

The above is my code. What's wrong in my code? My result length is 130. Could you help me? Thanks

paulmillr commented 3 years ago

don't use secp256k1-native, it's been downloaded like 5 times. Use other popular libraries and compare the result.

cowboy9 commented 3 years ago

Which one do you prefer? @paulmillr

cowboy9 commented 3 years ago

Hi @paulmillr , do you think the point and scalar value in my code are in right format to be used in secp.getSharedSecret function? point is base64 value: A8Nd1J1A+akKMfHe7j8oZkfLf1QDOkTynN27zr9nBgK3 - public key B scalar is hex value: 88e5c2a323f371a43abee6ff28966f7f0cd267e65063aeba9033037c7d93d87d - private key A

const point = Buffer.from('A8Nd1J1A+akKMfHe7j8oZkfLf1QDOkTynN27zr9nBgK3', 'base64'); // B-JS public key
const scalar = Buffer.from('88e5c2a323f371a43abee6ff28966f7f0cd267e65063aeba9033037c7d93d87d', 'hex'); // A-JS private key

can these values used for getSharedSecret function as params? Are these values in right format? const sharedSecret = secp.getSharedSecret(scalar, point);

paulmillr commented 3 years ago

Any hex string or Uint8Array is valid. Buffers are not valid. You need to do Uint8Array.from(buffer).

cowboy9 commented 3 years ago

Hi @paulmillr , I mean the point variable is the base64 value which is 33 bytes and scalar variable is hex value which is 32 bytes in above code. What are the byte size of params used in secp.getSharedSecret function?

paulmillr commented 3 years ago

Sizes are correct. See noble-secp tests.

cowboy9 commented 3 years ago

Hi @paulmillr , how can I change Point object to Buffer hex value?

paulmillr commented 3 years ago

See readme, Point.toHex(). Sorry, I don't really have time to be tech support if you don't know what you're doing! Compare outputs to other libraries and make it work.