Skip to content

BIP-32 path parse - #423

Draft
kuliq23 wants to merge 1 commit into
bitcoinfuzz:v2from
kuliq23:bip32_path_parse
Draft

kuliq23 wants to merge 1 commit into
bitcoinfuzz:v2from
kuliq23:bip32_path_parse

Conversation

@kuliq23

@kuliq23 kuliq23 commented Feb 5, 2026 •

Copy link
Copy Markdown
Contributor

DRAFT PR
This is a WIP on the custom mutator for path parsing.
I am trying to find an equilibrium between tying the path generation to the input bytes (maximalize libFuzzer coverage and utilization) and targeting edgecases (results in repeated paths).
Added the target for RUST_BITCOIN and BITCOIN_CORE so far.

@erickcestari
erickcestari self-requested a review February 5, 2026 18:08

@erickcestari erickcestari left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on it! I don't think we need a custom mutator for BIP-32 paths.

BIP-32 paths are just plain text. LibFuzzer handles them well natively. However, the current approach breaks the coverage feedback loop because GeneratePathFromBytes replaces LibFuzzer's mutations entirely. This prevents LibFuzzer from learning the actual input structure.

By the way, we generally use the custom mutator for cases where we first need to make checksums that would make it almost impossible for the fuzzer to generate a valid one by itself or decode something (like base58), so that we can then mutate the raw bytes. Mutating a base58 string will not help the fuzzer understand the input structure.

https://github.com/google/fuzzing/blob/master/docs/structure-aware-fuzzing.md

@kuliq23

kuliq23 commented Feb 6, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for working on it! I don't think we need a custom mutator for BIP-32 paths.

BIP-32 paths are just plain text. LibFuzzer handles them well natively. However, the current approach breaks the coverage feedback loop because GeneratePathFromBytes replaces LibFuzzer's mutations entirely. This prevents LibFuzzer from learning the actual input structure.

By the way, we generally use the custom mutator for cases where we first need to make checksums that would make it almost impossible for the fuzzer to generate a valid one by itself or decode something (like base58), so that we can then mutate the raw bytes. Mutating a base58 string will not help the fuzzer understand the input structure.

https://github.com/google/fuzzing/blob/master/docs/structure-aware-fuzzing.md

Thank you so much, makes sense, ill make sure to check this out:)

@kuliq23

kuliq23 commented Feb 10, 2026

Copy link
Copy Markdown
Contributor Author

LibFuzzer alone explores the BIP-32 path dimension effectively according to code-coverage metrics, consistently reaching derivation depths above 255. This directly exercises the depth-byte overflow case I want to test. I will extend the same approach to other modules before finalizing the PR.

@quapka

quapka commented Feb 11, 2026

Copy link
Copy Markdown
Contributor

LibFuzzer alone explores the BIP-32 path dimension effectively according to code-coverage metrics, consistently reaching derivation depths above 255. This directly exercises the depth-byte overflow case I want to test. I will extend the same approach to other modules before finalizing the PR.

Great to hear that. Sorry for misguiding you towards the custom mutators approach.

Comment thread custommutator/mutators/bip32_path.h Resolved
@kuliq23

kuliq23 commented Feb 16, 2026 •

Copy link
Copy Markdown
Contributor Author

Due to the inconsistencies in internal representations of BIP-32 paths in different modules, I plan to expand the target to "derive_from_path". This is a less granular but more practical approach, as deriving new keys from a path is the typical use case.

Another obstacle is the sanitization of Path string input. Some modules expect an exact format while others accept more general forms. (e.g. "h","H"," ' ") .... Currently, I am planning a custommutator/sanitizer. That would convert the path strings to a form acceptable for most modules. Different layers of this mutator could be "turned on and off" using e.g. env vars to help fuzz different aspects of the paths.

Motivation: If we want to test depth-byte overflows, failing on "H" instead of "h" would be inconvenient.

(just a WIP for rust and core pushed)

@kuliq23
kuliq23 force-pushed the bip32_path_parse branch 2 times, most recently from e326af3 to e64a59b Compare March 11, 2026 11:05
Comment thread driver.cpp Resolved
@kuliq23

kuliq23 commented Mar 11, 2026

Copy link
Copy Markdown
Contributor Author

WIP - Still draft PR - working on it

@kuliq23

kuliq23 commented Mar 19, 2026

Copy link
Copy Markdown
Contributor Author

Left only RUST_BITCOIN and BITCOIN_CORE to make the contribution bite-sized as I am not sure about it's mergeability.

Modules deal with path strings differently, as I mentioned before:

Some modules expect an exact format while others accept more general forms. (e.g. "h","H"," ' ") ....

Due to this, this target fails after a few inputs.
This behavior is not unexpected and not something that I would consider as a real issue...

But we can also observe some interesting results:

BIP32 path parse failed for +22
Module: Rustbitcoin
Result: CORRECT
Module: Bitcoin
Result: INVALID

Do you think we should add this target?
Thanks

@erickcestari

Copy link
Copy Markdown
Member

Some modules expect an exact format while others accept more general forms. (e.g. "h","H"," ' ") ....

Hmmm, interesting! Sorry for the late reply. I think an easy way to avoid the fuzzer crashing all the time is to make those more restrictive implementations skip those specific errors in module.cpp instead of returning "INVALID", so that the driver.cpp doesn't compare the outputs. Since we know it's going to fail anyway.

I'll take a closer look later in the PR. Thanks for working on this!

@erickcestari erickcestari left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IndexOutOfRange(IndexOutOfRangeError { index: 3709551615 })
BIP32 path parse failed for 00000003709551615
Module: Rustbitcoin
Result: INVALID
Module: Bitcoin
Result: CORRECT

Bitcoin Core's ParseHDKeypath accepts unhardened path components with values ≥ 2^31 (e.g. 3709551615) without error, silently treating them as hardened keys since bit 31 is already set in the raw integer. This means m/3709551615 and m/1562068223' parse identically. The function should reject unhardened components where the parsed value exceeds 2^31 - 1, as rust-bitcoin does. Might be worth reporting upstream.

Found this by adding some skips to be able to found more meaningful discrepancies.

  1. skip these inputs in driver.cpp since we know that core will not be able to parse it:
 if (path.find('+') != std::string::npos) {
   return;
 }

 if (path.find('h') != std::string::npos) {
   return;
 }
  1. skip skip cases where bitcoin core accepts33/ and rust-bitcoin rejects because it expects an integer after the "/" in rustbitcoin lib.rs.
match bitcoin::bip32::DerivationPath::from_str(s) {
       Ok(_) => str_to_c_string("CORRECT"),
       Err(bitcoin::bip32::ParseChildNumberError::ParseInt(e)) => {
           if e.kind() == &std::num::IntErrorKind::Empty {
               return std::ptr::null_mut();
           }
           str_to_c_string("INVALID")
       }
       Err(_) => str_to_c_string("INVALID"),
   }

return "INVALID";
}

return "CORRECT";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could return a formatted string to see if the implementations produced the same uint32 numbers.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, the return value is simplified here to showcase just the constraints for accepting 'pathstrings' as valid and because again, each implementation has it's own path_to_str equivalent, if any.

Populating a formatted string from the internal path representation makes sense and I will incorporate it.

@quapka

quapka commented Mar 21, 2026

Copy link
Copy Markdown
Contributor

skip these inputs in driver.cpp since we know that core will not be able to parse it

You previously suggested skipping the troublesome inputs in module.cpp, which seems to provide more information. Say bitcoin does not parse +, but two other modules do; then fuzzing all three together could identify a discrepancy in how the other modules interpret +.

@quapka

quapka commented Mar 21, 2026

Copy link
Copy Markdown
Contributor

This means m/3709551615 and m/1562068223' parse identically. The function should reject unhardened components where the parsed value exceeds 2^31 - 1, as rust-bitcoin does. Might be worth reporting upstream.

I would be interested in hearing their take on that - I've mentioned some of the discrepancies in bitcoin/bitcoin#32201 (comment) already. However, after re-reading parts of the spec (emphasis mine):

Each extended key has 231 normal child keys, and 231 hardened child keys. Each of these child keys has an index. The normal child keys use indices 0 through 231-1. The hardened child keys use indices 231 through 232-1. To ease notation for hardened key indices, a number iH represents i+231.
...
The algorithm to do so depends on whether the child is a hardened key or not (or, equivalently, whether i ≥ 231), and whether we're talking about private or public keys.

I think it's just under- or poorly specified in the spec.

@kuliq23

kuliq23 commented Mar 22, 2026 •

Copy link
Copy Markdown
Contributor Author

Bitcoin Core's ParseHDKeypath accepts unhardened path components with values ≥ 2^31 (e.g. 3709551615) without error, silently treating them as hardened keys since bit 31 is already set in the raw integer. This means m/3709551615 and m/1562068223' parse identically. The function should reject unhardened components where the parsed value exceeds 2^31 - 1, as rust-bitcoin does. Might be worth reporting upstream.

I noticed this behavior earlier and assumed it might be an intentional safeguard. While this basically introduces three ways to denote the same path ( m/3709551615 , m/1562068223' , m/3709551615' ), I assumed it is more beneficial to a user than outright rejecting such inputs, as it tolerates mistakes and still results in a valid derivation.

However, this still underlines the under-specificiation of derivation paths as a whole in BIP32.
I am currently working on a behavior table comparing handling of this and other 'path cases' between implemented modules:

  • can start with m
  • can start with M
  • must start with m
  • must start with M
  • allows H for hardened index
  • allows h for hardened index
  • allows ' for hardened index
  • allows index > 0x7FFFFFFF
  • allows index > 0x7FFFFFFF with '|H|h
  • allows + in index
  • allows alphabetic chars (excl. m,M,handH)
  • allows depth > 255

I'd be happy to to document more edgecases if you can think of any!

Thank you both for exploring this!

@quapka

quapka commented Mar 22, 2026 •

Copy link
Copy Markdown
Contributor
  • allows ´ for hardened index
  • allows index > 0x7FFFFFFF with ´|H|h

Where have you seen ´? I thought it was the single quote character '.

  • allows H for hardened index

Also, I wonder what the source of truth should be for the hardening indicators. After skimming the BIP32, I can't find it. There are mentions in BIP380.

Interestingly, the situation with H changed a few times last year. Originally, it was supported, but in conflict with that, it was also part of an invalid test vector. This changed with bitcoin/bips#1803. But then it got reverted in bitcoin/bips#1888 while bitcoin/bitcoin#32788 was being implemented...

  • allows +,- in index

From what I have seen, this is due to the index being parsed via a function for parsing (unsigned) integers, e.g., NBitcoin (see some docs). Point being whatever is parseable as an unsigned int could be potentially seen as a valid item in a path for certain languages.

@kuliq23

kuliq23 commented Mar 22, 2026 •

Copy link
Copy Markdown
Contributor Author
  • allows ´ for hardened index
  • allows index > 0x7FFFFFFF with ´|H|h

Where have you seen ´? I thought it was the single quote character '.

Mistake(s) fixed, thanks.

(Also removed - from "+,-")

Thank you for all the comments!

@kuliq23

kuliq23 commented Mar 24, 2026

Copy link
Copy Markdown
Contributor Author

@erickcestari I think the approach with skipping in individual module.cpps is the most logical. I plan using this approach on a derive_from_path target and leave the path_parse target as is. Reasons:

  • derive_from_path is more use-case realistic - makes more sense to test it
  • some modules (e.g. rustbitcoin) parse paths of depth > 255 successfully, but later refuse to derive using them - testing parsing only does not show the actual behavior
  • the discrepancies in path_parse are expected as there is no single strict source of truth for BIP32 path formatting

So overall, I’d treat path_parse more as a diagnostic/behavior comparison target, and focus consistency checks on derive_from_path, where differences are more meaningful in practice.

Does this approach make sense to you? Do you think path_parse should be merged in its current form, or do we prefer to drop it? Thanks

@erickcestari

Copy link
Copy Markdown
Member

@erickcestari I think the approach with skipping in individual module.cpps is the most logical. I plan using this approach on a derive_from_path target and leave the path_parse target as is. Reasons:

  • derive_from_path is more use-case realistic - makes more sense to test it
  • some modules (e.g. rustbitcoin) parse paths of depth > 255 successfully, but later refuse to derive using them - testing parsing only does not show the actual behavior
  • the discrepancies in path_parse are expected as there is no single strict source of truth for BIP32 path formatting

So overall, I’d treat path_parse more as a diagnostic/behavior comparison target, and focus consistency checks on derive_from_path, where differences are more meaningful in practice.

Does this approach make sense to you? Do you think path_parse should be merged in its current form, or do we prefer to drop it? Thanks

Good question! I think we can keep this is as draft for now. derive_from_path target will provide us more meaningful results and and also cover the code that path_parse is covering. I think when we have derive_from_path working and we have sure that path_parse doesn't cover any new code we can close this PR.

You previously suggested skipping the troublesome inputs in module.cpp, which seems to provide more information. Say bitcoin does not parse +, but two other modules do; then fuzzing all three together could identify a discrepancy in how the other modules interpret +.

Indeed! It does make more sense to skip it directly in the module.cpp

@kuliq23
kuliq23 force-pushed the bip32_path_parse branch from 58a2a26 to ab85cb5 Compare May 16, 2026 19:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants