From 6450c058c5765f18e42fd45c4d1fc5825d653c3c Mon Sep 17 00:00:00 2001 From: Brandon Black Date: Sun, 7 Jan 2024 07:31:40 -0800 Subject: [PATCH] Add OP_INTERNALKEY for Tapscript Testing is minimal, but so is the code. --- src/policy/policy.h | 3 ++- src/script/interpreter.cpp | 20 ++++++++++++++++++-- src/script/interpreter.h | 10 ++++++++++ src/script/script.cpp | 2 ++ src/script/script.h | 1 + src/test/data/tx_valid.json | 8 ++++++++ src/test/transaction_tests.cpp | 2 ++ test/functional/test_framework/script.py | 2 +- 8 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/policy/policy.h b/src/policy/policy.h index 6a7980c312c9bd..938f823fbc4c63 100644 --- a/src/policy/policy.h +++ b/src/policy/policy.h @@ -113,7 +113,8 @@ static constexpr unsigned int STANDARD_SCRIPT_VERIFY_FLAGS{MANDATORY_SCRIPT_VERI SCRIPT_VERIFY_CONST_SCRIPTCODE | SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_TAPROOT_VERSION | SCRIPT_VERIFY_DISCOURAGE_OP_SUCCESS | - SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_PUBKEYTYPE}; + SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_PUBKEYTYPE | + SCRIPT_VERIFY_INTERNALKEY}; /** For convenience, standard but not mandatory verify flags. */ static constexpr unsigned int STANDARD_NOT_MANDATORY_VERIFY_FLAGS{STANDARD_SCRIPT_VERIFY_FLAGS & ~MANDATORY_SCRIPT_VERIFY_FLAGS}; diff --git a/src/script/interpreter.cpp b/src/script/interpreter.cpp index c969ce45f12496..a693d43374c1a9 100644 --- a/src/script/interpreter.cpp +++ b/src/script/interpreter.cpp @@ -1213,6 +1213,15 @@ bool EvalScript(std::vector >& stack, const CScript& } break; + case OP_INTERNALKEY: { + // OP_INTERNALKEY is only available in Tapscript + if (sigversion == SigVersion::BASE || sigversion == SigVersion::WITNESS_V0) return set_error(serror, SCRIPT_ERR_BAD_OPCODE); + // Always present in Tapscript + assert(execdata.m_internal_key); + stack.emplace_back(execdata.m_internal_key->begin(), execdata.m_internal_key->end()); + break; + } + default: return set_error(serror, SCRIPT_ERR_BAD_OPCODE); } @@ -1798,6 +1807,12 @@ static bool ExecuteWitnessScript(const Span& stack_span, const CS // Note how this condition would not be reached if an unknown OP_SUCCESSx was found return set_error(serror, SCRIPT_ERR_BAD_OPCODE); } + if (opcode == OP_INTERNALKEY) { + if (flags & SCRIPT_VERIFY_DISCOURAGE_INTERNALKEY) + return set_error(serror, SCRIPT_ERR_DISCOURAGE_OP_SUCCESS); + if (flags & SCRIPT_VERIFY_INTERNALKEY) continue; + return set_success(serror); + } // New opcodes will be listed here. May use a different sigversion to modify existing opcodes. if (IsOpSuccess(opcode)) { if (flags & SCRIPT_VERIFY_DISCOURAGE_OP_SUCCESS) { @@ -1856,12 +1871,13 @@ uint256 ComputeTaprootMerkleRoot(Span control, const uint25 return k; } -static bool VerifyTaprootCommitment(const std::vector& control, const std::vector& program, const uint256& tapleaf_hash) +static bool VerifyTaprootCommitment(const std::vector& control, const std::vector& program, const uint256& tapleaf_hash, std::optional& internal_key) { assert(control.size() >= TAPROOT_CONTROL_BASE_SIZE); assert(program.size() >= uint256::size()); //! The internal pubkey (x-only, so no Y coordinate parity). const XOnlyPubKey p{Span{control}.subspan(1, TAPROOT_CONTROL_BASE_SIZE - 1)}; + internal_key = p; //! The output pubkey (taken from the scriptPubKey). const XOnlyPubKey q{program}; // Compute the Merkle root from the leaf and the provided path. @@ -1927,7 +1943,7 @@ static bool VerifyWitnessProgram(const CScriptWitness& witness, int witversion, return set_error(serror, SCRIPT_ERR_TAPROOT_WRONG_CONTROL_SIZE); } execdata.m_tapleaf_hash = ComputeTapleafHash(control[0] & TAPROOT_LEAF_MASK, script); - if (!VerifyTaprootCommitment(control, program, execdata.m_tapleaf_hash)) { + if (!VerifyTaprootCommitment(control, program, execdata.m_tapleaf_hash, execdata.m_internal_key)) { return set_error(serror, SCRIPT_ERR_WITNESS_PROGRAM_MISMATCH); } execdata.m_tapleaf_hash_init = true; diff --git a/src/script/interpreter.h b/src/script/interpreter.h index 836c2e7982a20a..c354bbf968f340 100644 --- a/src/script/interpreter.h +++ b/src/script/interpreter.h @@ -11,6 +11,7 @@ #include #include