Skip to content

Commit ed487fc

Browse files
committed
Merge e343610 into merged_master (Elements PR #1414)
2 parents 13c2a2b + e343610 commit ed487fc

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/test/fuzz/simplicity_tx.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5-
#include <cstdio>
65
#include <span.h>
76
#include <kernel/validation_cache_sizes.h>
87
#include <primitives/transaction.h>
@@ -67,14 +66,6 @@ void initialize_simplicity_tx()
6766
INPUT_ASSET_CONF.vchCommitment[0] = 0x0a;
6867
}
6968

70-
void write_u32(FILE *fh, uint32_t val) {
71-
unsigned char buf[4];
72-
73-
val = htole32(val);
74-
memcpy(buf, &val, 4);
75-
assert(fwrite(buf, 1, 4, fh) == 4);
76-
}
77-
7869
FUZZ_TARGET_INIT(simplicity_tx, initialize_simplicity_tx)
7970
{
8071
simplicity_err error;
@@ -124,6 +115,7 @@ FUZZ_TARGET_INIT(simplicity_tx, initialize_simplicity_tx)
124115
// 3. Construct `nIn` and `spent_outs` arrays.
125116
bool expect_simplicity = false;
126117
std::vector<CTxOut> spent_outs{};
118+
unsigned char last_cmr[32] = { 0 };
127119
for (unsigned int i = 0; i < mtx.vin.size(); i++) {
128120
// Null asset or value would assert in the interpreter, and are impossible
129121
// to hit in real transactions. Nonces are not included in the UTXO set and
@@ -162,6 +154,15 @@ FUZZ_TARGET_INIT(simplicity_tx, initialize_simplicity_tx)
162154
// Compute CMR and do some sanity checks on it (and the program)
163155
std::vector<unsigned char> cmr(32, 0);
164156
assert(simplicity_computeCmr(&error, cmr.data(), program.data(), program.size()));
157+
if (error == SIMPLICITY_NO_ERROR) {
158+
if (memcmp(last_cmr, cmr.data(), sizeof(last_cmr)) == 0) {
159+
// If we have already seen this CMR this transaction, try mangling
160+
// it to check that this produces a CMR error and not something worse.
161+
cmr.data()[1] ^= 1;
162+
}
163+
memcpy(last_cmr, cmr.data(), sizeof(last_cmr));
164+
}
165+
165166
const XOnlyPubKey internal{Span{control}.subspan(1, TAPROOT_CONTROL_BASE_SIZE - 1)};
166167

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

0 commit comments

Comments
 (0)