Skip to content

Commit a9213bb

Browse files
author
MarcoFalke
committed
Merge #18422: [consensus] MOVEONLY: Move single-sig checking EvalScript code to EvalChecksig
14e8cf9 [consensus] MOVEONLY: Move single-sig checking EvalScript code to EvalChecksig (Pieter Wuille) Pull request description: This is another small refactor pulled out of the Schnorr/Taproot PR #17977. This is in preparation for adding different signature verification rules, specifically tapscript (BIP 342), which interprets opcode 0xac and 0xad as Schnorr signature verifications. ACKs for top commit: sipa: ACK 14e8cf9, verified move-only. MarcoFalke: ACK 14e8cf9, reviewed with "git show 14e8cf9 --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space -W" 👆 fjahr: Code-review ACK 14e8cf9, verified that it's move-only. instagibbs: code review ACK bitcoin/bitcoin@14e8cf9, verified move-only theStack: Code-Review ACK bitcoin/bitcoin@14e8cf9 jonatack: ACK 14e8cf9 Tree-SHA512: af2efce9ae39d5ec01db5b9ef0ff383fe252ef5f33b3483927308ae17d91a619266cb45951f32ea1ce54807a4c0f052bcdefb47e244465d3a726393221c227b1
2 parents 99d6a5b + 14e8cf9 commit a9213bb

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

src/script/interpreter.cpp

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,35 @@ class ConditionStack {
342342
};
343343
}
344344

345+
/** Helper for OP_CHECKSIG and OP_CHECKSIGVERIFY
346+
*
347+
* A return value of false means the script fails entirely. When true is returned, the
348+
* fSuccess variable indicates whether the signature check itself succeeded.
349+
*/
350+
static bool EvalChecksig(const valtype& vchSig, const valtype& vchPubKey, CScript::const_iterator pbegincodehash, CScript::const_iterator pend, unsigned int flags, const BaseSignatureChecker& checker, SigVersion sigversion, ScriptError* serror, bool& fSuccess)
351+
{
352+
// Subset of script starting at the most recent codeseparator
353+
CScript scriptCode(pbegincodehash, pend);
354+
355+
// Drop the signature in pre-segwit scripts but not segwit scripts
356+
if (sigversion == SigVersion::BASE) {
357+
int found = FindAndDelete(scriptCode, CScript() << vchSig);
358+
if (found > 0 && (flags & SCRIPT_VERIFY_CONST_SCRIPTCODE))
359+
return set_error(serror, SCRIPT_ERR_SIG_FINDANDDELETE);
360+
}
361+
362+
if (!CheckSignatureEncoding(vchSig, flags, serror) || !CheckPubKeyEncoding(vchPubKey, flags, sigversion, serror)) {
363+
//serror is set
364+
return false;
365+
}
366+
fSuccess = checker.CheckSig(vchSig, vchPubKey, scriptCode, sigversion);
367+
368+
if (!fSuccess && (flags & SCRIPT_VERIFY_NULLFAIL) && vchSig.size())
369+
return set_error(serror, SCRIPT_ERR_SIG_NULLFAIL);
370+
371+
return true;
372+
}
373+
345374
bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, unsigned int flags, const BaseSignatureChecker& checker, SigVersion sigversion, ScriptError* serror)
346375
{
347376
static const CScriptNum bnZero(0);
@@ -985,25 +1014,8 @@ bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript&
9851014
valtype& vchSig = stacktop(-2);
9861015
valtype& vchPubKey = stacktop(-1);
9871016

988-
// Subset of script starting at the most recent codeseparator
989-
CScript scriptCode(pbegincodehash, pend);
990-
991-
// Drop the signature in pre-segwit scripts but not segwit scripts
992-
if (sigversion == SigVersion::BASE) {
993-
int found = FindAndDelete(scriptCode, CScript() << vchSig);
994-
if (found > 0 && (flags & SCRIPT_VERIFY_CONST_SCRIPTCODE))
995-
return set_error(serror, SCRIPT_ERR_SIG_FINDANDDELETE);
996-
}
997-
998-
if (!CheckSignatureEncoding(vchSig, flags, serror) || !CheckPubKeyEncoding(vchPubKey, flags, sigversion, serror)) {
999-
//serror is set
1000-
return false;
1001-
}
1002-
bool fSuccess = checker.CheckSig(vchSig, vchPubKey, scriptCode, sigversion);
1003-
1004-
if (!fSuccess && (flags & SCRIPT_VERIFY_NULLFAIL) && vchSig.size())
1005-
return set_error(serror, SCRIPT_ERR_SIG_NULLFAIL);
1006-
1017+
bool fSuccess = true;
1018+
if (!EvalChecksig(vchSig, vchPubKey, pbegincodehash, pend, flags, checker, sigversion, serror, fSuccess)) return false;
10071019
popstack(stack);
10081020
popstack(stack);
10091021
stack.push_back(fSuccess ? vchTrue : vchFalse);

0 commit comments

Comments
 (0)