paulmillr / noble-secp256k1

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

Is it possible to derive a sharedSecret? #28

Closed carlosnufe closed 2 years ago

carlosnufe commented 2 years ago

Hello,

We want to change if possible the elliptic implementation we have for our browser app with your library, which is better in different areas. However, either we didn't get how to do it (probably lack of knowledge) or it's not possible.

We do have this

const sharedPublicKeyPair = ecInstance.keyFromPublic(sharedPublicKey, 'hex');
const derivedKey = keyPair.derive(sharedPublicKeyPair.getPublic()).toString(16);
const derivedKeySliceBase64 = hexadecimalToBase64(derivedKey.substring(0, 48));

After reading the documentation you provide we need help to achieve our goal. So we would have

// previously
let privateKey = utils.randomPrivateKey() or window.crypto.getRandomValues(new Uint8Array(32))
const publicKey = getPublicKey(privateKey) 

const sharedPublic = getSharedSecret(privateKey, sharedPublicKey) // 'hex' 04...
// next step

After getting the hex of 130 chars / ArrayBuffer of 65 bytes, we tried to import the key using the WebCrypto API, but the length is above 256bits.

Could we get some help?

Thanks

paulmillr commented 2 years ago

Do sharedPublic.slice(1)

carlosnufe commented 2 years ago

@paulmillr it worked, thanks for your fast response. We are able to use the library.