Skip to content

Commit 8a20fbe

Browse files
apoelstrasanket1729
authored andcommitted
Add XOnlyPubKey::CreateTapTweak
bitcoin/bitcoin#22051 (6/9) Modified to use the old-style Optional rather than std::optional
1 parent 68961ca commit 8a20fbe

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/pubkey.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,24 @@ bool CPubKey::TweakMulVerify(const CPubKey& untweaked, const uint256& tweak) con
222222
return *this == CPubKey(out_pk, out_pk + out_len);
223223
}
224224

225+
Optional<std::pair<XOnlyPubKey, bool>> XOnlyPubKey::CreateTapTweak(const uint256* merkle_root) const
226+
{
227+
secp256k1_xonly_pubkey base_point;
228+
if (!secp256k1_xonly_pubkey_parse(secp256k1_context_verify, &base_point, data())) return nullopt;
229+
secp256k1_pubkey out;
230+
uint256 tweak = ComputeTapTweakHash(merkle_root);
231+
if (!secp256k1_xonly_pubkey_tweak_add(secp256k1_context_verify, &out, &base_point, tweak.data())) return nullopt;
232+
int parity = -1;
233+
std::pair<XOnlyPubKey, bool> ret;
234+
secp256k1_xonly_pubkey out_xonly;
235+
if (!secp256k1_xonly_pubkey_from_pubkey(secp256k1_context_verify, &out_xonly, &parity, &out)) return nullopt;
236+
secp256k1_xonly_pubkey_serialize(secp256k1_context_verify, ret.first.data(), &out_xonly);
237+
assert(parity == 0 || parity == 1);
238+
ret.second = parity;
239+
return ret;
240+
}
241+
242+
225243
bool CPubKey::Verify(const uint256 &hash, const std::vector<unsigned char>& vchSig) const {
226244
if (!IsValid())
227245
return false;

src/pubkey.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define BITCOIN_PUBKEY_H
99

1010
#include <hash.h>
11+
#include <optional.h>
1112
#include <serialize.h>
1213
#include <span.h>
1314
#include <uint256.h>
@@ -248,13 +249,17 @@ class XOnlyPubKey
248249
* Merkle root, and parity. */
249250
bool CheckTapTweak(const XOnlyPubKey& internal, const uint256& merkle_root, bool parity) const;
250251

252+
/** Construct a Taproot tweaked output point with this point as internal key. */
253+
Optional<std::pair<XOnlyPubKey, bool>> CreateTapTweak(const uint256* merkle_root) const;
254+
251255
const unsigned char& operator[](int pos) const { return *(m_keydata.begin() + pos); }
252256
const unsigned char* data() const { return m_keydata.begin(); }
253257
static constexpr size_t size() { return decltype(m_keydata)::size(); }
254258
const unsigned char* begin() const { return m_keydata.begin(); }
255259
const unsigned char* end() const { return m_keydata.end(); }
256260
unsigned char* begin() { return m_keydata.begin(); }
257261
unsigned char* end() { return m_keydata.end(); }
262+
unsigned char* data() { return m_keydata.begin(); }
258263
bool operator==(const XOnlyPubKey& other) const { return m_keydata == other.m_keydata; }
259264
bool operator!=(const XOnlyPubKey& other) const { return m_keydata != other.m_keydata; }
260265
bool operator<(const XOnlyPubKey& other) const { return m_keydata < other.m_keydata; }

0 commit comments

Comments
 (0)