Skip to content

Commit

Permalink
Add CKey::ECDH
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenroose committed Mar 20, 2019
1 parent aa375e6 commit 045e615
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include <secp256k1.h>
#include <secp256k1_recovery.h>
#include <secp256k1_ecdh.h>

static secp256k1_context* secp256k1_context_sign = nullptr;

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

uint256 CKey::ECDH(const CPubKey& pubkey) const {
assert(fValid);
uint256 result;
secp256k1_pubkey pkey;
assert(secp256k1_ec_pubkey_parse(secp256k1_context_sign, &pkey, pubkey.begin(), pubkey.size()));
assert(secp256k1_ecdh(secp256k1_context_sign, result.begin(), &pkey, begin(), NULL, NULL));
return result;
}

bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, bool grind, uint32_t test_case) const {
if (!fValid)
return false;
Expand Down
5 changes: 5 additions & 0 deletions src/key.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ class CKey
*/
CPubKey GetPubKey() const;

/**
* Compute the ECDH exchange result using this private key and another public key.
*/
uint256 ECDH(const CPubKey& pubkey) const;

/**
* Create a DER-serialized signature.
* The test_case parameter tweaks the deterministic nonce.
Expand Down

0 comments on commit 045e615

Please sign in to comment.