I've written this working NodeJS/Javascript function:
/*
* Calculate the Symmetric Key from the Public and Secret keys from two
* different key pairs.
*
* Note: symmetric_key(publicA, secretB) == symmetric_key(publicB, secretA)
*/
function symmetric_key(pkey, skey) {
const ephemeral = crypto.createECDH('secp256k1');
ephemeral.setPublicKey(pkey);
const centre = crypto.createECDH('secp256k1');
centre.setPrivateKey(skey);
return centre.computeSecret(ephemeral.getPublicKey());
}
I'm trying to understand how I can use the Rust secp256k1
library to do the same.
Looking at secp256k1::SecretKey's methods I cannot see a direct equivalent.
computeSecret()
called?Found it: ECDH SharedSecret
Struct secp256k1::ecdh::SharedSecret
pub struct SharedSecret(_);
Enables two parties to create a shared secret without revealing their own secrets.
Examples
let s = Secp256k1::new();
let (sk1, pk1) = s.generate_keypair(&mut rand::thread_rng());
let (sk2, pk2) = s.generate_keypair(&mut rand::thread_rng());
let sec1 = SharedSecret::new(&pk2, &sk1);
let sec2 = SharedSecret::new(&pk1, &sk2);
assert_eq!(sec1, sec2);