From fe92c15f0c44d1405b9048306736bd0eae868506 Mon Sep 17 00:00:00 2001 From: Sebastian Falbesoner Date: Tue, 21 Nov 2023 16:47:35 +0100 Subject: [PATCH] script/sign: avoid duplicated signature verification after signing `ProduceSignature` already calls `VerifyScript` internally as last step in order to check whether the signature data is complete. If and only if that is the case, the `complete` field of the `SignatureData` is set accordingly and there is no need then to verify the script after again, as we already know that it would succeed. This leads to a rough ~20% speed-up for `SignTransaction` for single-input ECDSA or Taproot inputs, according to the `SignTransaction{ECDSA,Taproot}` benchmarks. --- src/script/sign.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/script/sign.cpp b/src/script/sign.cpp index 22ac062a633..6e26ec11e05 100644 --- a/src/script/sign.cpp +++ b/src/script/sign.cpp @@ -831,7 +831,7 @@ bool SignTransaction(CMutableTransaction& mtx, const SigningProvider* keystore, } ScriptError serror = SCRIPT_ERR_OK; - if (!VerifyScript(txin.scriptSig, prevPubKey, &txin.scriptWitness, STANDARD_SCRIPT_VERIFY_FLAGS, TransactionSignatureChecker(&txConst, i, amount, txdata, MissingDataBehavior::FAIL), &serror)) { + if (!sigdata.complete && !VerifyScript(txin.scriptSig, prevPubKey, &txin.scriptWitness, STANDARD_SCRIPT_VERIFY_FLAGS, TransactionSignatureChecker(&txConst, i, amount, txdata, MissingDataBehavior::FAIL), &serror)) { if (serror == SCRIPT_ERR_INVALID_STACK_OPERATION) { // Unable to sign input and verification failed (possible attempt to partially sign). input_errors[i] = Untranslated("Unable to sign input, invalid stack size (possibly missing key)");