Skip to content

Commit

Permalink
implement OP_TWEAKADD
Browse files Browse the repository at this point in the history
  • Loading branch information
reardencode committed Jul 31, 2024
1 parent 0cbdc6b commit 309e045
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/pubkey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,11 @@ bool XOnlyPubKey::CheckTapTweak(const XOnlyPubKey& internal, const uint256& merk
return secp256k1_xonly_pubkey_tweak_add_check(secp256k1_context_static, m_keydata.begin(), parity, &internal_key, tweak.begin());
}

std::optional<std::pair<XOnlyPubKey, bool>> XOnlyPubKey::CreateTapTweak(const uint256* merkle_root) const
std::optional<std::pair<XOnlyPubKey, bool>> XOnlyPubKey::TweakAdd(const uint256& tweak) const
{
secp256k1_xonly_pubkey base_point;
if (!secp256k1_xonly_pubkey_parse(secp256k1_context_static, &base_point, data())) return std::nullopt;
secp256k1_pubkey out;
uint256 tweak = ComputeTapTweakHash(merkle_root);
if (!secp256k1_xonly_pubkey_tweak_add(secp256k1_context_static, &out, &base_point, tweak.data())) return std::nullopt;
int parity = -1;
std::pair<XOnlyPubKey, bool> ret;
Expand All @@ -263,6 +262,11 @@ std::optional<std::pair<XOnlyPubKey, bool>> XOnlyPubKey::CreateTapTweak(const ui
return ret;
}

std::optional<std::pair<XOnlyPubKey, bool>> XOnlyPubKey::CreateTapTweak(const uint256* merkle_root) const
{
return TweakAdd(ComputeTapTweakHash(merkle_root));
}


bool CPubKey::Verify(const uint256 &hash, const std::vector<unsigned char>& vchSig) const {
if (!IsValid())
Expand Down
3 changes: 3 additions & 0 deletions src/pubkey.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,9 @@ class XOnlyPubKey
* Merkle root, and parity. */
bool CheckTapTweak(const XOnlyPubKey& internal, const uint256& merkle_root, bool parity) const;

/** Add the specified tweak to this xonly pubkey. */
std::optional<std::pair<XOnlyPubKey, bool>> TweakAdd(const uint256& tweak) const;

/** Construct a Taproot tweaked output point with this point as internal key. */
std::optional<std::pair<XOnlyPubKey, bool>> CreateTapTweak(const uint256* merkle_root) const;

Expand Down
19 changes: 19 additions & 0 deletions src/script/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1213,6 +1213,25 @@ bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript&
}
break;

case OP_TWEAKADD:
{
// pubkey tweak -- tweaked_pubkey parity(0|1)
if (stack.size() < 2)
return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION);

const valtype& pubkey = stacktop(-2);
const valtype& tweak = stacktop(-1);
const XOnlyPubKey p{Span{pubkey}};
const std::optional<std::pair<XOnlyPubKey, bool>> ret = p.TweakAdd(uint256(tweak));
if (!ret.has_value())
return set_error(serror, SCRIPT_ERR_UNKNOWN_ERROR);
popstack(stack);
popstack(stack);
stack.emplace_back(ret.value().first.begin(), ret.value().first.end());
stack.push_back(ret.value().second ? vchTrue : vchFalse);
}
break;

default:
return set_error(serror, SCRIPT_ERR_BAD_OPCODE);
}
Expand Down
1 change: 1 addition & 0 deletions src/script/script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ std::string GetOpName(opcodetype opcode)

// Opcode added by BIP 342 (Tapscript)
case OP_CHECKSIGADD : return "OP_CHECKSIGADD";
case OP_TWEAKADD : return "OP_TWEAKADD";

case OP_INVALIDOPCODE : return "OP_INVALIDOPCODE";

Expand Down
1 change: 1 addition & 0 deletions src/script/script.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ enum opcodetype

// Opcode added by BIP 342 (Tapscript)
OP_CHECKSIGADD = 0xba,
OP_TWEAKADD = 0xcd,

OP_INVALIDOPCODE = 0xff,
};
Expand Down

0 comments on commit 309e045

Please sign in to comment.