Repository navigation
Follow BIP32 in hardened derivation for keys with a leading zero byte - #21
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hardened derivation in
HDPrivateKeyhashed0x00 ‖ privateKey.toBuffer() ‖ index.PrivateKey.toBuffer()drops leading zero bytes, so when the parent private key starts with0x00(about 1 in 256 keys) the HMAC input is shorter than BIP32'sser256(k)and the child comes out different. The library fails BIP32 test vector 3 atm/0Hand test vector 4 atm/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
derivehashes the parent key as 32 bytes in hardened steps (a port of b79a9b2).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, andfromObjectrejected it.docs/hierarchical.mdsays when to usederiveNonCompliant.Tests
toObjectround 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-lib0.16.x or earlier will get a different child for about 1 in 256 parent keys after upgrading, and can usederiveNonCompliantto find those.Since
deriveoutput 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.