You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
BIP32: Maximal length of derivation paths and depth of extended keys #32201
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+).
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.
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.
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.
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.
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?
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?
Activity
quapka commentedon Apr 2, 2025
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.
quapka commentedon Sep 15, 2025
Linking bitcoinj/bitcoinj#3683, to keep things in one place.
Prevent derivation of unserializable extended keys
Prevent derivation of unserializable extended keys
quapka commentedon Feb 4, 2026
@sipa any thoughts on this?
On a related note, the specification itself advices to increment
iin 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.
sipa commentedon Feb 4, 2026
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.
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.
quapka commentedon Feb 4, 2026
I agree, the potential interoperability issues motivated me in the first place.
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?
quapka commentedon Mar 20, 2026
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?BIP32: Maximal length of derivation paths and depth of extended keys
fametrano commentedon Oct 2, 2026
I opened bitcoin/bips#2322 with the note on the maximum depth of 255 discussed above.
fix(utxo-lib): prevent derivation of unserializable extended keys