Skip to content

Commit 4eb6e4d

Browse files
committed
fuzz: simplicity: use zero CMR for invalid programs
When we have an invalid program, use a zero CMR and create a valid control block/taptweak. Otherwise we fail to hit the Simplicity logic at all with bad programs. # # You are committing on CAMUS # # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # # On branch 2024-12--simple-fuzz # Changes to be committed: # modified: src/test/fuzz/simplicity_tx.cpp # # Untracked files: # reduced-corpus/ # run-fuzz-merge-dir-CPzq/ # run-fuzz-merge-dir-sscf/ #
1 parent 044db41 commit 4eb6e4d

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

src/test/fuzz/simplicity_tx.cpp

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ FUZZ_TARGET_INIT(simplicity_tx, initialize_simplicity_tx)
121121

122122
// 3. Construct `nIn` and `spent_outs` arrays.
123123
bool expect_simplicity = false;
124-
std::vector<unsigned char[32]> cmrs;
125124
std::vector<CTxOut> spent_outs{};
126125
for (unsigned int i = 0; i < mtx.vin.size(); i++) {
127126
// Null asset or value would assert in the interpreter, and are impossible
@@ -161,23 +160,20 @@ FUZZ_TARGET_INIT(simplicity_tx, initialize_simplicity_tx)
161160
// Compute CMR and do some sanity checks on it (and the program)
162161
std::vector<unsigned char> cmr(32, 0);
163162
assert(simplicity_computeCmr(&error, cmr.data(), program.data(), program.size()));
164-
if (error == SIMPLICITY_NO_ERROR) {
165-
const XOnlyPubKey internal{Span{control}.subspan(1, TAPROOT_CONTROL_BASE_SIZE - 1)};
166-
167-
const CScript leaf_script{cmr.begin(), cmr.end()};
168-
const uint256 tapleaf_hash = ComputeTapleafHash(0xbe, leaf_script);
169-
uint256 merkle_root = ComputeTaprootMerkleRoot(control, tapleaf_hash);
170-
auto ret = internal.CreateTapTweak(&merkle_root);
171-
if (ret.has_value()) {
172-
expect_simplicity = true;
173-
//assert(0); // useful for searching for a nontrivial fuzz target
174-
// Just drop the parity; it needs to match the one in the control block,
175-
// but we want to test that logic, so we allow them not to match.
176-
const XOnlyPubKey output_key = ret->first;
177-
// If we made it here, success (aside from parity maybe)
178-
current[top - 2] = std::move(cmr);
179-
scriptPubKey = CScript() << OP_1 << ToByteVector(output_key);
180-
}
163+
const XOnlyPubKey internal{Span{control}.subspan(1, TAPROOT_CONTROL_BASE_SIZE - 1)};
164+
165+
const CScript leaf_script{cmr.begin(), cmr.end()};
166+
const uint256 tapleaf_hash = ComputeTapleafHash(0xbe, leaf_script);
167+
uint256 merkle_root = ComputeTaprootMerkleRoot(control, tapleaf_hash);
168+
auto ret = internal.CreateTapTweak(&merkle_root);
169+
if (ret.has_value()) {
170+
expect_simplicity = (error == SIMPLICITY_NO_ERROR);
171+
// Just drop the parity; it needs to match the one in the control block,
172+
// but we want to test that logic, so we allow them not to match.
173+
const XOnlyPubKey output_key = ret->first;
174+
// If we made it here, success (aside from parity maybe)
175+
current[top - 2] = std::move(cmr);
176+
scriptPubKey = CScript() << OP_1 << ToByteVector(output_key);
181177
}
182178
}
183179
}

0 commit comments

Comments
 (0)