Skip to content

Follow BIP32 in hardened derivation for keys with a leading zero byte - #21

Merged
TheTrunk merged 1 commit into
masterfrom
fix/bip32-hardened-derivation
Dec 4, 2026
Merged

TheTrunk merged 1 commit into
masterfrom
fix/bip32-hardened-derivation

Conversation

@TheTrunk

@TheTrunk TheTrunk commented Dec 4, 2026

Copy link
Copy Markdown
Member

Hardened derivation in HDPrivateKey hashed 0x00 ‖ privateKey.toBuffer() ‖ index. PrivateKey.toBuffer() drops leading zero bytes, so when the parent private key starts with 0x00 (about 1 in 256 keys) the HMAC input is shorter than BIP32's ser256(k) and the child comes out different. The library fails BIP32 test vector 3 at m/0H and test vector 4 at m/0H/1H.

The bug came from upstream. BitPay fixed it in September 2016 (bitpay/bitcore-lib@b79a9b2, released in 0.14.0). The Zcash fork this library descends from was taken from 0.13.19 a month earlier, so the fix never came through.

Changes

  • derive hashes the parent key as 32 bytes in hardened steps (a port of b79a9b2).
  • New deriveNonCompliant(arg, hardened) returns what 0.16.x and earlier derived, so funds sent to addresses from old derivations can still be found. The HD key cache stores compliant and non-compliant children under different keys.
  • toObject() writes the private key as 32 bytes. A key starting with a zero byte used to come out as 62 hex characters, and fromObject rejected it.
  • docs/hierarchical.md says when to use deriveNonCompliant.

Tests

  • BIP32 test vectors 3 and 4, BitPay's two tests for this, a recovery test against the 0.16.x outputs, a cache test and a toObject round trip. All nine fail on master and pass here.
  • npm test: 2162 passing, 831 failing as listed, 0 unlisted (master: 2153 passing).

Who is affected

Flux's own packages are unaffected: bitcore-node, insight-api and bitcore-message don't use HD keys, and Flux's wallets derive keys with other libraries. Anyone who derived hardened keys with @runonflux/bitcore-lib 0.16.x or earlier will get a different child for about 1 in 256 parent keys after upgrading, and can use deriveNonCompliant to find those.

Since derive output changes, I'd release this as 0.17.0. The dependents ask for ^0.16.0, so they won't pick it up until someone bumps them, which is fine because none of them derive HD keys.

Reported through the Flux bug bounty.

Hardened derivation hashed the parent private key without its leading zero
bytes. For about 1 in 256 parent keys the child then differed from BIP32 and
from other wallets (test vectors 3 and 4). BitPay fixed this upstream in 2016
(bitpay/bitcore-lib b79a9b2, v0.14.0), a month after the Zcash fork this
library comes from was taken.

derive now uses the key as 32 bytes. deriveNonCompliant returns what 0.16.x
derived, so keys made with it can still be recovered, and the HD key cache
keeps the two apart. toObject also writes the key as 32 bytes; before,
fromObject rejected a key that starts with a zero byte.

Adds BIP32 test vectors 3 and 4 and BitPay's two tests for this.
@TheTrunk
TheTrunk merged commit fac5175 into master Dec 4, 2026
2 checks passed
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.

1 participant