Skip to content

fuzz: minor updates to the Simplicity fuzz tests #1414

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 14, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions src/test/fuzz/simplicity_tx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <cstdio>
#include <span.h>
#include <primitives/transaction.h>
#include <script/sigcache.h>
Expand Down Expand Up @@ -65,14 +64,6 @@ void initialize_simplicity_tx()
INPUT_ASSET_CONF.vchCommitment[0] = 0x0a;
}

void write_u32(FILE *fh, uint32_t val) {
unsigned char buf[4];

val = htole32(val);
memcpy(buf, &val, 4);
assert(fwrite(buf, 1, 4, fh) == 4);
}

FUZZ_TARGET_INIT(simplicity_tx, initialize_simplicity_tx)
{
simplicity_err error;
Expand Down Expand Up @@ -122,6 +113,7 @@ FUZZ_TARGET_INIT(simplicity_tx, initialize_simplicity_tx)
// 3. Construct `nIn` and `spent_outs` arrays.
bool expect_simplicity = false;
std::vector<CTxOut> spent_outs{};
unsigned char last_cmr[32] = { 0 };
for (unsigned int i = 0; i < mtx.vin.size(); i++) {
// Null asset or value would assert in the interpreter, and are impossible
// to hit in real transactions. Nonces are not included in the UTXO set and
Expand Down Expand Up @@ -160,6 +152,15 @@ FUZZ_TARGET_INIT(simplicity_tx, initialize_simplicity_tx)
// Compute CMR and do some sanity checks on it (and the program)
std::vector<unsigned char> cmr(32, 0);
assert(simplicity_computeCmr(&error, cmr.data(), program.data(), program.size()));
if (error == SIMPLICITY_NO_ERROR) {
if (memcmp(last_cmr, cmr.data(), sizeof(last_cmr)) == 0) {
// If we have already seen this CMR this transaction, try mangling
// it to check that this produces a CMR error and not something worse.
cmr.data()[1] ^= 1;
}
memcpy(last_cmr, cmr.data(), sizeof(last_cmr));
}

const XOnlyPubKey internal{Span{control}.subspan(1, TAPROOT_CONTROL_BASE_SIZE - 1)};

const CScript leaf_script{cmr.begin(), cmr.end()};
Expand Down