From 1c6f71823f1a13f4792f8c9f5aa3c06e8ddbefb6 Mon Sep 17 00:00:00 2001 From: Antoine Poinsot Date: Sun, 9 Oct 2022 15:50:27 +0200 Subject: [PATCH] qa: add a fuzz target ensuring the sighash behaviour for non APO keys was conserved This fuzz targets copied the SignatureHashSchnorr function for Bitcoin Core 23.0 and checks the output of the APO-ready SignatureHashSchnorr from this branch against it. This is to make sure the behaviour of the function was not changed for non ANYPREVOUT keys, which would make some previously valid signatures invalid and, even worse, some previously invalid signatures valid. --- src/Makefile.test.include | 1 + src/script/interpreter.cpp | 4 + src/test/fuzz/anyprevout.cpp | 187 +++++++++++++++++++++++++++++++++++ 3 files changed, 192 insertions(+) create mode 100644 src/test/fuzz/anyprevout.cpp diff --git a/src/Makefile.test.include b/src/Makefile.test.include index 2fe48c88b4e78b..ae5622101814d7 100644 --- a/src/Makefile.test.include +++ b/src/Makefile.test.include @@ -283,6 +283,7 @@ test_fuzz_fuzz_SOURCES = \ $(FUZZ_WALLET_SRC) \ test/fuzz/addition_overflow.cpp \ test/fuzz/addrman.cpp \ + test/fuzz/anyprevout.cpp \ test/fuzz/asmap.cpp \ test/fuzz/asmap_direct.cpp \ test/fuzz/autofile.cpp \ diff --git a/src/script/interpreter.cpp b/src/script/interpreter.cpp index 53df4248a841b0..7497ae8df10ae4 100644 --- a/src/script/interpreter.cpp +++ b/src/script/interpreter.cpp @@ -1671,9 +1671,11 @@ bool SignatureHashSchnorr(uint256& hash_out, ScriptExecutionData& execdata, cons ss << cache.m_spent_outputs[in_pos]; ss << tx_to.vin[in_pos].nSequence; } else if (input_type == SIGHASH_ANYPREVOUT) { + assert(keyversion == KeyVersion::ANYPREVOUT); ss << cache.m_spent_outputs[in_pos]; ss << tx_to.vin[in_pos].nSequence; } else if (input_type == SIGHASH_ANYPREVOUTANYSCRIPT) { + assert(keyversion == KeyVersion::ANYPREVOUT); ss << tx_to.vin[in_pos].nSequence; } else { ss << in_pos; @@ -1698,6 +1700,8 @@ bool SignatureHashSchnorr(uint256& hash_out, ScriptExecutionData& execdata, cons assert(execdata.m_tapleaf_hash_init); if (input_type != SIGHASH_ANYPREVOUTANYSCRIPT) { ss << execdata.m_tapleaf_hash; + } else { + assert(keyversion == KeyVersion::ANYPREVOUT); } ss << uint8_t(keyversion); assert(execdata.m_codeseparator_pos_init); diff --git a/src/test/fuzz/anyprevout.cpp b/src/test/fuzz/anyprevout.cpp new file mode 100644 index 00000000000000..a38a344ecb2004 --- /dev/null +++ b/src/test/fuzz/anyprevout.cpp @@ -0,0 +1,187 @@ +// Copyright (c) 2020 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include +#include +#include