Skip to content
Trapdoor·Lab

Hierarchical deterministic keys (BIP32)

1Toy

One seed, a whole tree of keys

An application that needs many key pairs — one per account, per device or per single use — has to store and protect each of them. BIP-32 (Pieter Wuille, 2012) turns that into one seed: the whole tree of keys comes out of it deterministically, so a copy of the seed is a copy of every key. Each node of the tree is an extended key: a private key k with its public key K = k · G on the secp256k1 curve (point multiplication as in the Elliptic curves station) and a 32-byte chain code c. The master key is one HMAC-SHA512 of the seed; the HMAC key is the constant “Bitcoin seed”, because the scheme was written for Bitcoin, but the mechanism itself has nothing to do with cryptocurrency. A child with index i is another HMAC-SHA512, this time keyed with the parent’s chain code: the left half of the result, I_L, added to the parent key modulo n gives the child key, and the right half, I_R, is its chain code.

Children come in two kinds. A normal child (i < 2³¹) feeds the parent’s public key into the HMAC, and since k_i = I_L + k_parent, K_i = I_L · G + K_parent. Whoever holds the pair (K, c) — the extended public key, written as an “xpub…” string — derives the public keys of all normal children without any private key. A hardened child (i ≥ 2³¹, “H” in a path) feeds the parent’s private key into the HMAC, and the xpub cannot compute it. WebCrypto has no secp256k1, so the points come from the curves station’s toy arithmetic; HMAC-SHA512 and SHA-256 come from WebCrypto, and the whole of it is checked against BIP-32 test vectors 1–5. Switch to a hex seed and type the path m/0H/1/2H/2/1000000000 — you get the xpub and xprv published in vector 1.

Toy implementation — not for productionPoints on secp256k1 come from the curves station’s toy BigInt arithmetic (not constant time), since WebCrypto has no such curve; HMAC-SHA512 and SHA-256 come from WebCrypto. Do not type a real seed here. For real: an audited library such as @scure/bip32.

A tree of keys from one seed

Seed (32 B from crypto.getRandomValues)
32 B62562010a766eb1b08724bf25ec25448cd3eda8c222b98432729d1d190b27783

m is the master key, each “/i” one child. “H” after an index marks a hardened child (index i + 2³¹). At most 6 levels, indices 0 … 2³¹ − 1.

A path like m/0H/1/2H: it starts with m, indices 0 … 2147483647, at most 6 levels.

Computing HMAC-SHA512 and curve points…

2Break it

An xpub and one leaked key

The convenience of an xpub has a price, which BIP-32 spells out in its security section (“Implications”): the parent’s extended public key plus any private key of a normal descendant is equivalent to the parent’s extended private key. The arithmetic is short. For a normal child, I_L depends only on c_parent, K_parent and the index — all of them in the xpub — and k_i = I_L + k_parent mod n, so k_parent = k_i − I_L. Below, the victim gives a server the account’s xpub so it can hand out new public keys without access to private ones, and one child private key leaks: exported to another program or taken from a compromised device. From those two things you recover the account key and the keys of all its children. Switch the children to hardened: the same attacker code gives a number whose point is not the account key, because the victim’s HMAC was computed over data containing k_parent, which the attacker does not have. The price is that the xpub no longer derives the public keys of hardened children — which is why the account level is hardened and the children below it are normal. The attack stops at the first hardened level above the leak.

Toy implementation — not for productionPoints on secp256k1 come from the curves station’s toy BigInt arithmetic (not constant time), since WebCrypto has no such curve; HMAC-SHA512 and SHA-256 come from WebCrypto. Do not type a real seed here. For real: an audited library such as @scure/bip32.

An account xpub and one leaked key

You are on the attacker’s side. You have the extended public key of an account, which the victim gave to a server, and the private key of one child, which leaked. Recover the account’s private key and the keys of the other children.

The victim keeps a tree from a random seed. The account m/0H is a hardened child of the master key and its children are normal — that is why a server with only the account’s xpub derives their public keys.

KPrivate key of the account m/0H hidden

The victim derives a tree of keys…

The fix has a price: hardened children protect the parent from a leak, but then the xpub cannot derive their public keys. That is why BIP-32 hardens the account level and keeps normal children below it — a leak hits one account, not the master key.

The second attack on the map will come from a quantum computer. BIP-32 keys are ordinary keys on the secp256k1 curve, and Shor’s algorithm solves the discrete logarithm on a curve in polynomial time (the resource estimates are in the Elliptic curves station). Whoever has such a computer will recover the private key from any public key — and a single xpub reveals the public keys of all normal children at once. The algorithm itself is shown by the Shor’s algorithm station.

3In the real world

Where keys grow from one root

  • SLIP-0010The same scheme on other curves: NIST P-256, Ed25519 and Curve25519, with its own HMAC key constant for each (“Nist256p1 seed”, “ed25519 seed”). For Ed25519 and Curve25519 only hardened derivation works — CKDpub always fails. On P-256 an invalid key comes out with a probability of 2⁻³², so SLIP-0010 does not give up but hashes again until the key is valid.Source (SLIP-0010)
  • ARKG (IETF draft)Asynchronous Remote Key Generation from Yubico has the same goal as an xpub: it lets someone derive public keys on your behalf without access to the private keys. Examples from the draft: two paired authentication devices register public keys on behalf of each other, and the European digital identity is to get single-use keys without touching the device that holds the private key. BIP-32 is in the draft’s list of references.Source (ARKG (IETF draft))
  • Apple Find MyA related construction outside BIP-32: the device has a key pair on the P-224 curve — private d and public P — and a 256-bit secret SK₀. The chain SKᵢ = KDF(SKᵢ₋₁, “update”) gives numbers (uᵢ, vᵢ), and from them a short-lived key dᵢ = uᵢ · d + vᵢ with its public key Pᵢ = uᵢ · P + vᵢ · G — computed without d, like a child from an xpub. The broadcast public key changes about every 15 minutes, so the device cannot be tracked by a persistent identifier.Source (Apple Find My)
  • Tor — onion services v3An analogue with multiplication instead of addition. The onion services v3 specification (appendix A, key blinding): from a master Ed25519 key A, each period gets a blinded key A′ = h · A, with the private key a′ = h · a mod l, where h is SHA3-256 of A and the period number, among other inputs. Anyone who knows A (and the optional extra secret s) computes A′ without the private key, and an observer of many blinded keys cannot link them to one master key.Source (Tor — onion services v3)
  • Wallets (BIP-44)The best-known use is cryptocurrency wallets. BIP-44 lays out the tree as m / purpose′ / coin_type′ / account′ / change / address_index: the top three levels are hardened, the bottom two normal. So the account xpub derives the public keys of all addresses — and together with the private key of one of them it gives the account key, as in “Break it”, just two levels further down.Source (Wallets (BIP-44))
4Take it to your project

A hardened level above every shared xpub

Good practice

Use an audited library — in JavaScript, for example, @scure/bip32 on @noble/curves — and a seed from a CSPRNG; BIP-32 advises 256 bits. Make the level whose xpub you hand out (to a server, a service, a second device) a hardened child of its parent: then a leak of a key below it ends at that level, not at the master key. Derive keys that are to leave the system one by one as hardened. Treat an xpub as confidential: it signs nothing, but it reveals the public keys of the whole subtree, and with one private key of a normal child — all the private keys.

JavaScript
import { HDKey } from '@scure/bip32';

const root = HDKey.fromMasterSeed(crypto.getRandomValues(new Uint8Array(32)));
// the shared level is a hardened child of the root
const account = root.derive("m/0'");
const forServer = account.publicExtendedKey; // xpub: the server derives child public keys
// a key that leaves on its own is hardened — the account xpub cannot reach it
const exported = account.deriveChild(HDKey.HARDENED_OFFSET + 7).privateKey;
Anti-pattern

Do not give anyone the xpub of a level whose normal children leave the device as single private keys or sit somewhere less protected — one with the other rebuilds the parent key. Do not publish an xpub like an ordinary public key: anyone who knows it can link all the children’s keys to each other. Do not compute points with your own code like this toy — BigInt arithmetic does not run in constant time. Do not identify a parent by the 4-byte fingerprint in the serialization: BIP-32 notes that software must be willing to deal with collisions.

JavaScript
// ✕ the account xpub at a server and a normal child of the same account off the device
const account = root.derive("m/0'");
upload(account.publicExtendedKey);
exportKey(account.deriveChild(5).privateKey); // together: the key of the account and all its children