This add ComputeECDHSecret(const CPubKey& pubkey, CPrivKey& secret_out) to CKey (including a test).
This is a subset of #14032 and a pre-requirement for BIP324.
This add ComputeECDHSecret(const CPubKey& pubkey, CPrivKey& secret_out) to CKey (including a test).
This is a subset of #14032 and a pre-requirement for BIP324.
156+ CPrivKey secret2;
157+ BOOST_CHECK(key1.ComputeECDHSecret(pubkey2, secret1));
158+ BOOST_CHECK(key2.ComputeECDHSecret(pubkey1, secret2));
159+ BOOST_CHECK(secret1.size() == 32);
160+ BOOST_CHECK(secret1 == secret2);
161 }
pubkeydata[9] = 0xFF; line checks that, right? Maybe add a comment to clarify?
292+ if (!secp256k1_ec_pubkey_parse(secp256k1_context_sign, &pubkey_internal, pubkey.data(), pubkey.size())) {
293+ return false;
294+ }
295+
296+ secret_out.resize(32);
297+ return secp256k1_ecdh(secp256k1_context_sign, &secret_out[0], &pubkey_internal, &keydata[0]) == 1;
SECP256K1_CONTEXT_NONE context, given the context is not currently used by these functions?
&keydata[0] argument with keydata.data()?
secp256k1_context_sign to secp256k1_context?
127@@ -128,6 +128,9 @@ class CKey
128 //! Derive BIP32 child key.
129 bool Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const;
130
131+ // computes an EC Diffie-Hellman 256bit secret
132+ bool ComputeECDHSecret(const CPubKey& pubkey, CPrivKey& secret_out) const;
Extract ComputeECDHSecret from CKey to make it a freestanding function?
0bool ComputeECDHSecret(const CKey& key, const CPubKey& pubkey, CPrivKey& secret_out);
Although it is convenient to add new BIP responsibilities directly to CKey, I would recommend against adding more responsibilities to CKey than it already has.
Derive and other member functions of CKey).
“A function that can be simply, elegantly, and efficiently implemented as a freestanding function (that is, as a nonmember function) should be implemented outside the class.” (Stroustrup) is a very commonly used and well documented best practice in C++ development.
See C.4. Make a function a member only if it needs direct access to the representation of a class for more info and guidelines.
0bool ComputeECDHSecret(const CKey& key, const CPubKey& pubkey, CPrivKey& secret_out)
1{
2 secp256k1_pubkey pubkey_internal;
3 if (!secp256k1_ec_pubkey_parse(secp256k1_context_sign, &pubkey_internal, pubkey.data(), pubkey.size())) {
4 return false;
5 }
6
7 secret_out.resize(32);
8 return secp256k1_ecdh(secp256k1_context_sign, &secret_out[0], &pubkey_internal, key.begin()) == 1;
9}
1431@@ -1432,7 +1432,7 @@ if test x$need_bundled_univalue = xyes; then
1432 AC_CONFIG_SUBDIRS([src/univalue])
1433 fi
1434
1435-ac_configure_args="${ac_configure_args} --disable-shared --with-pic --with-bignum=no --enable-module-recovery --disable-jni"
1436+ac_configure_args="${ac_configure_args} --disable-shared --with-pic --with-bignum=no --enable-module-recovery --enable-experimental --enable-module-ecdh --disable-jni"
--enable-experimental “permanent”? Perhaps this change should be behind a ./configure --enable-experimental-ecdh, or is overly pedantic?
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
Reviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.