Skip to content

Commit 045e615

Browse files
committed
Add CKey::ECDH
1 parent aa375e6 commit 045e615

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/key.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <secp256k1.h>
1414
#include <secp256k1_recovery.h>
15+
#include <secp256k1_ecdh.h>
1516

1617
static secp256k1_context* secp256k1_context_sign = nullptr;
1718

@@ -202,6 +203,15 @@ bool SigHasLowR(const secp256k1_ecdsa_signature* sig)
202203
return compact_sig[0] < 0x80;
203204
}
204205

206+
uint256 CKey::ECDH(const CPubKey& pubkey) const {
207+
assert(fValid);
208+
uint256 result;
209+
secp256k1_pubkey pkey;
210+
assert(secp256k1_ec_pubkey_parse(secp256k1_context_sign, &pkey, pubkey.begin(), pubkey.size()));
211+
assert(secp256k1_ecdh(secp256k1_context_sign, result.begin(), &pkey, begin(), NULL, NULL));
212+
return result;
213+
}
214+
205215
bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, bool grind, uint32_t test_case) const {
206216
if (!fValid)
207217
return false;

src/key.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ class CKey
110110
*/
111111
CPubKey GetPubKey() const;
112112

113+
/**
114+
* Compute the ECDH exchange result using this private key and another public key.
115+
*/
116+
uint256 ECDH(const CPubKey& pubkey) const;
117+
113118
/**
114119
* Create a DER-serialized signature.
115120
* The test_case parameter tweaks the deterministic nonce.

0 commit comments

Comments
 (0)