Skip to content

miniscript_parse: Core vs rust-miniscript asserts in seconds, rust skips sanity checks #670

Open

Description

@amannx21

RUNNING.md:227-228 recommends pairing Bitcoin Core and rust-miniscript for miniscript_parse because they have a similar permissiveness level (#480). I tried it: MODULES="BITCOIN_CORE,RUST_MINISCRIPT" with the Dockerfile flags (-max_len=10000 -timeout=300), seeded from miniscript_parse_clean, 16 independent libFuzzer seeds. 13 hit the assert after 8-43s (median 13s), the other 3 survived 60-90s. All 13 go the same way: Core rejects, rust-miniscript accepts. None the other way round.

I looked at what explains the 13:

  • 9 are sanity. Core's wrapper requires IsSane() (modules/bitcoin/module.cpp:362-369), the rust one uses from_str_insane (modules/rustminiscript/rust_miniscript_lib/src/lib.rs:31-32), which is documented to accept malleable scripts and scripts without a signature. So older(1), after(1), a lone sha256(<64 hex>), andor(ripemd160(..),..) or or_d(or_d(l:1,l:1),..) are rejected by Core and accepted by rust. If I parse these with rust-miniscript's own from_str (same pinned rev), it agrees with Core on all 9.
  • 2 are key text, the contract KY-U described in feat(bitcoinerlab_miniscript): add bitcoinerlab miniscript module #480: Core only resolves 1-2 hex char keys (module.cpp:224, :239) and the rust side uses String, so pkh(0*2) and pkh(2:::::::::::::::::0) are rejected by Core and accepted by rust. String is also the hash type, so sha256(212) goes through too.
  • 2 are an embedded NUL. The wrapper passes str.c_str() to Rust (modules/rustminiscript/module.cpp:21), so multi_a(1,09)\0junk reaches rust as multi_a(1,09). Cut at the NUL, both modules agree.

Minimal inputs through the driver (rust=1, Core=0): pk(0*2), pkh(099), sha256(212), older(1), after(1). pk(02), and_v(v:pk(01),pk(02)) and and_v(v:pk(01),older(1)) agree. Repairing only the key text (0*2 to 02) removes that disagreement.

This isn't visible in CI because miniscript_parse_clean only keeps inputs that already exit 0, and driver.cpp:161-164 skips the strings "0" and "1", which looks like the same thing for two inputs. corpora#9 mentions miniscript_parse has many inputs that crash for known issues. My guess is that a good part of that is this and not differences between the libraries.

Is that something you want to change? I can send a PR where the rust side uses from_str, follows the key contract from #480, and passes the length instead of c_str(), or only a note in RUNNING.md. I haven't tried the change, so I don't know which other mismatches it would show (#73 suggests rust's sanity check differs from Core's in some cases).

Activity

  1. jascodes7 commented on Oct 28, 2026

    @jascodes7

    Thanks for the detailed write-up @amannx21. I went through the three causes against the pinned rust-miniscript rev (698b97f) and the pinned Core source, and I agree with all of them. A few things to add:

    Sanity checks. At this rev, rust-miniscript's from_str validates with Ctx::SANE: non-malleable, requires a signature, top-level B, no duplicate keys, no mixed timelocks, plus resource limits. That's close to Core's IsSane(). from_str_insane only applies consensus rules.

    Key contract. Beyond 0*2 / 099, switching to rust-miniscript's from_str alone doesn't catch two more cases. Both need a key type that mirrors Core's ParserContext::FromString (1–2 hex chars, so 8 == 08):

    • pk(g): Core rejects it because it isn't hex.
    • and_v(v:pk(8),pk(08)): Core sees the same dummy key twice and rejects it as a duplicate. With String keys, rust sees two different keys and accepts.

    With real hash types (sha256::Hash etc.), sha256(212) is also rejected for its length.

    What I checked. i also ran the issue's inputs plus some edge cases through a standalone crate against the pinned rev. The "Core" results come from reading Core's source at the pinned commit; I haven't run them through the harness yet.

    mismatches out of 23
    current (from_str_insane, String, c_str()) 15
    from_str only 7
    from_str + dummy key type + pass length 3

    The remaining 3 are a real library difference. Core parses the numbers in older/after/thresh with ToIntegral (std::from_chars), which accepts leading zeros, so and_v(v:pk(01),older(010)) is sane for Core. rust-miniscript's parse_num rejects leading zeros. I think this is the same class as #285. Once the wrapper bugs are fixed, it will probably be the first thing Core + rust-miniscript hits.

    @brunoerg @erickcestari, how would you like that case handled?

    1. Leave it compared and report it upstream (to Core or to rust-miniscript?).
    2. Skip it in the rust wrapper with a TODO linking an issue.
    3. Something else.

    @amannx21, are you already working on the PR? If not, I'm happy to open it, or we can co-author it. The plan, one commit each so commit-by-commit CI stays green:

    1. rust-miniscript: apply sanity checks in miniscript_parse: use from_str for Segwitv0 and then Tap.
    2. rust-miniscript: mirror Core's dummy key contract in miniscript_parse: a u8-backed key type plus real hash types.
    3. rust-miniscript: pass input length instead of c_str(): also replaces the duplicated extern "C" declarations in module.cpp with the header. descriptor_parse has the same NUL truncation; should I fix it in the same PR or keep it separate?
    4. Handle the leading-zero case, depending on the answer above.
  2. amannx21 commented on Oct 29, 2026

    @amannx21
    ContributorAuthor

    Thanks @jascodes7, good points. I'm already working on the PR and have it mostly done locally, so I'll open it soon. Happy to have your review on it. I checked your cases on current v2 and they match what I see: pk(g) and and_v(v:pk(8),pk(08)) need the key type, not just from_str. With the three fixes (sanity checks, a key type like Core's, passing the length), the only cases still differing for me are the leading zero ones like older(010) and thresh(02,...). I'll leave those out until @brunoerg / @erickcestari decide.
    I'm fixing the NUL truncation in descriptor_parse in the same commit, since it's the same change.

  3. jascodes7 commented on Oct 30, 2026

    @jascodes7

    Sounds good @amannx21 , thanks for picking this up. I'll review it once it's open. Two things to check in the meantime:

    1. pk(+1): if the key type parses with u8::from_str_radix, note that it accepts a leading + ("+1" gives Ok(1)), while Core's ParseHex rejects it. Checking that every character is a hex digit first avoids a new mismatch.

    2. NUL truncation in other modules: miniscript_parse in NBitcoin, both targets in embit (UntilNul) and descriptor_parse in tinyminiscript still cut at the first NUL. After your PR, Core and rust-miniscript will see the full string and these won't. The comment on embit's UntilNul also says it cuts "as the modules that take a C string do", which may need updating. This probably belongs in a follow-up rather than your PR. If you're not planning to cover it, I can take it.

  4. bitcoisas commented on Nov 10, 2026

    @bitcoisas

    Same pattern shows up in descriptor_parse with embit. Running only Core and embit, it asserts within seconds on wsh(u:1): Core rejects it as not sane, embit accepts it. The difference is that embit has no sane parser to switch to, since its miniscript doesn’t track the s/m properties (diybitcoinhardware/embit#144). Until that’s fixed upstream, would you rather skip non-sane descriptors for embit in the module, or leave it as a known mismatch?

    Separately, the same run also hit a real type bug in embit’s andor/and_n, which already has a fix up in diybitcoinhardware/embit#167.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions