Skip to content

BIP32: Maximal length of derivation paths and depth of extended keys #32201

Open

Description

@quapka

Hi folks,
I have started to notice and report an overflow of the depth of extended keys (as described in BIP32) and some confusion regarding the depth came up. On one hand, the depth is, I'd say indirectly, limited to the range [0 , 255] due to the serialization into a single byte. On the other hand, I don't see an inherent technical reason(s) that would prevent deriving even lower keys (with depth 255+).

The list of libraries, where I see the depth overflowing is increasing: libbitcoin/libbitcoin-system#1624, bitcoinj/bitcoinj#3693, https://github.com/tyler-smith/go-bip32/issues/27, MetacoSA/NBitcoin#1260, piotrnar/gocoin#71,...

In bitcoin repository, the overflow is caught here (and also here) and there is also a test case for it. But the derivation paths themselves aren't restricted in the size. Due to the variety of implementations and libraries APIs' for the derivation paths and key derivation (priv -> priv, priv -> pub, pub -> pub), there are also a few different ways how to possibly hit the overflow.

Keys that overflow could be hiding coins (as they would appear to live at different levels), but the valid parents are likely still identifiable via the fingerprints. (Possibly, in some edge-cases, overflown keys could be invalid in such a way that the coins would get locked.) I am not familiar with how the depth is used later on, e.g., after key deserialization, and if it could cause other issues.

Are there any legitimate reasons to allow longer paths and keys with depth > 255? Or shall any such overflow be considered a bug? Not sure, if I can tag Pieter Wuille, to get his thoughts regarding the depth validity and if there is more to it.

Activity

  1. quapka commented on Apr 2, 2025

    @quapka
    Author

    I would ask about this in the bips repository, but I don't see the option to raise issues there. And since I don't have a definitive answer, I wasn't sure how to maybe clarify or highlight this in BIP32 itself via a PR.

  2. quapka commented on Sep 15, 2025

    @quapka
    Author

    Linking bitcoinj/bitcoinj#3683, to keep things in one place.

  3. quapka commented on Feb 4, 2026

    @quapka
    Author

    @sipa any thoughts on this?

    On a related note, the specification itself advices to increment i in certain edge-cases, which in very rare cases could lead to overflow of the index from non-hardened variant to the hardened one (and vice versa).

    Treating of the index as an integer that can be incremented lead to buggy API in NBitcoin.

  4. sipa commented on Feb 4, 2026

    @sipa
    Member

    You're right that there is no inherent reason why the depth should be limited to 255, but for interoperability I think it's better to set a hard limit, and treat deriving to a higher depth as incorrect usage. I'll amend the BIP to include a note about this.

    On a related note, the specification itself advices to increment i in certain edge-cases, which in very rare cases could lead to overflow of the index from non-hardened variant to the hardened one (and vice versa).

    I'm not worried about this, as the increment case is practically unreachable (for secp256k1, it has a chance of 0.63538*2^-127 per derivation). If you're doing enough derivations that you have a non-negligible probability of hitting this, you also have enough computation power to break ECDLP.

  5. quapka commented on Feb 4, 2026

    @quapka
    Author

    for interoperability I think it's better to set a hard limit

    I agree, the potential interoperability issues motivated me in the first place.

    I'll amend the BIP to include a note about this.

    Thanks. While many implementors have updated their code anyway, there are some that would do it only if the specs were updated. Can you also add (failing) test vectors?

  6. quapka commented on Mar 20, 2026

    @quapka
    Author

    One more inconsistency across the libraries regarding the parsing of derivation paths. We are seeing differences, e.g., due to how the child indices are parsed; see here. Some libraries parse the index as "an integer", allowing for +/- prefix and such, while others reject it. Do you think, @sipa, this is something where the parsing should be stricter and more consistent across implementations?

  7. added a commit that references this issue on Apr 5, 2026
    13ee2c2
  8. fametrano commented on Oct 2, 2026

    @fametrano

    I opened bitcoin/bips#2322 with the note on the maximum depth of 255 discussed above.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions