Skip to content

Commit 8be9851

Browse files
committed
test: add tests for miniscript GetWitnessSize
1 parent 7ed2b2d commit 8be9851

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

src/test/fuzz/miniscript.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ struct TestData {
7070
sig.push_back(1); // SIGHASH_ALL
7171
dummy_sigs.insert({pubkey, {sig, i & 1}});
7272
assert(privkey.SignSchnorr(MESSAGE_HASH, schnorr_sig, nullptr, EMPTY_AUX));
73+
schnorr_sig.push_back(1); // Maximally-sized signature has sighash byte
7374
schnorr_sigs.emplace(XOnlyPubKey{pubkey}, std::make_pair(std::move(schnorr_sig), i & 1));
7475

7576
std::vector<unsigned char> hash;
@@ -1119,6 +1120,9 @@ void TestNode(const MsCtx script_ctx, const NodeRef& node, FuzzedDataProvider& p
11191120
// If a non-malleable satisfaction exists, the malleable one must also exist, and be identical to it.
11201121
assert(mal_success);
11211122
assert(stack_nonmal == stack_mal);
1123+
// Compute witness size (excluding script push, control block, and witness count encoding).
1124+
const size_t wit_size = GetSerializeSize(stack_nonmal, PROTOCOL_VERSION) - GetSizeOfCompactSize(stack_nonmal.size());
1125+
assert(wit_size <= *node->GetWitnessSize());
11221126

11231127
// Test non-malleable satisfaction.
11241128
witness_nonmal.stack.insert(witness_nonmal.stack.end(), std::make_move_iterator(stack_nonmal.begin()), std::make_move_iterator(stack_nonmal.end()));

src/test/miniscript_tests.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ struct TestData {
7777
sig.push_back(1); // sighash byte
7878
signatures.emplace(pubkey, sig);
7979
BOOST_CHECK(key.SignSchnorr(MESSAGE_HASH, schnorr_sig, nullptr, EMPTY_AUX));
80+
schnorr_sig.push_back(1); // Maximally sized Schnorr sigs have a sighash byte.
8081
schnorr_signatures.emplace(XOnlyPubKey{pubkey}, schnorr_sig);
8182

8283
// Compute various hashes
@@ -367,6 +368,8 @@ void TestSatisfy(const KeyConverter& converter, const std::string& testcase, con
367368
// Run non-malleable satisfaction algorithm.
368369
CScriptWitness witness_nonmal;
369370
const bool nonmal_success = node->Satisfy(satisfier, witness_nonmal.stack, true) == miniscript::Availability::YES;
371+
// Compute witness size (excluding script push, control block, and witness count encoding).
372+
const size_t wit_size = GetSerializeSize(witness_nonmal.stack, PROTOCOL_VERSION) - GetSizeOfCompactSize(witness_nonmal.stack.size());
370373
SatisfactionToWitness(converter.MsContext(), witness_nonmal, script, builder);
371374

372375
if (nonmal_success) {
@@ -378,6 +381,7 @@ void TestSatisfy(const KeyConverter& converter, const std::string& testcase, con
378381
// If a non-malleable satisfaction exists, the malleable one must also exist, and be identical to it.
379382
BOOST_CHECK(mal_success);
380383
BOOST_CHECK(witness_nonmal.stack == witness_mal.stack);
384+
assert(wit_size <= *node->GetWitnessSize());
381385

382386
// Test non-malleable satisfaction.
383387
ScriptError serror;

0 commit comments

Comments
 (0)