ERC-8499: Hash-Based Key Derivation from BIP-39

I’d like feedback on a draft ERC that says how a wallet derives hash-based post-quantum keys (leanSPHINCS, SLH-DSA, WOTS, FORS, XMSS) from a BIP-39 phrase.

Draft: Add ERC: Hash-Based Key Derivation from BIP-39 by johba37 · Pull Request #2172 · ethereum/ERCs · GitHub

Why

Contracts already verify hash-based signatures through ERC-1271 and account abstraction, and EIP-8288 would aggregate leanSPHINCS. Users back their accounts up with BIP-39 phrases, but no standard says how a phrase becomes a hash-based key, and implementations have diverged:

  • from the BIP-39 entropy, with Keccak-256 over application labels (the Winterlock signer I work on does this today);
  • through a second PBKDF2 over the phrase with its own salt (WOTS-39);
  • through HMAC-SHA512 over the BIP-39 seed under its own label (a hardware wallet in development).

So the same phrase gives different keys in different wallets, and restoring it elsewhere does not restore the keys.

A wallet that already has BIP-32 might derive these keys through it. That would bring a problem of its own. Every BIP-32 node has an elliptic-curve public key. Given an account-level xpub, which wallets export to watch-only software all the time, a quantum adversary computes that node’s private key and then every hardened child below it.

What

Every secret is derived with HMAC-SHA512: in BIP-39’s own PBKDF2, then in HKDF (RFC 5869). No BIP-32, no curve arithmetic.

S    = BIP39-Seed(mnemonic, passphrase)            64 bytes, as BIP-39
R    = HKDF-Extract(salt = "HBKD/v1", S)
seed = HKDF-Expand(R, info, L)
info = purpose || be32(account) || be16(L) || len8(name) || name || tail

purpose 0x01  stateless key                  name = key type
purpose 0x02  stateful key (optional)        name = key type, tail = context || instance
purpose 0x03  application secret (optional)  name = the application's label

The core is small. A wallet conforms if it derives the root, and a seed for each stateless key, named by an account and a key type. The draft defines the twelve SLH-DSA key types of FIPS 205 itself, for example SLH-DSA-SHA2-128s: the seed is cut into SK.seed, SK.prf and PK.seed. So two conforming wallets get the same SLH-DSA public key from the same phrase. When a fork fixes a new parameter set, the wallet derives a new key from the same phrase and the old key certifies it. No new backup.

Three parts are optional, because no two wallets need to agree on them:

  • Stateful keys (WOTS, FORS, XMSS) are never restored. Each instance takes 32 fresh random bytes, stored with its state, so a restored wallet cannot sign twice with a one-time key.
  • Application secrets, one per label.
  • Random roots for devices and automated signers, which hold no phrase. The phrase’s stateless key certifies and revokes their keys.

The draft has test vectors and a short Python reference implementation. The vectors cover three phrases (two of them BIP-39’s own TREZOR vectors), a random root, accounts 0, 1 and 2^32 − 1, stateful instances, app secrets, inputs that must be rejected, and three SLH-DSA keys from phrase to public key, which two independent libraries reproduce. A second implementation, in no_std Rust for browsers and microcontrollers, reproduces every value.

Questions

  1. Seed or entropy? The draft derives from the BIP-39 seed, so passphrases and hidden wallets keep working and wallets that keep only the seed can implement it. The cost: a mistyped passphrase gives valid but wrong keys, so wallets should show a fingerprint after a restore. Should this ERC fix the fingerprint’s format, so that two wallets show the same one?
  2. The info layout. Every field has a fixed width or a length byte, and the output length L is part of info, as in TLS 1.3. Is anything missing, or in the wrong place?
  3. Key types. The draft defines SLH-DSA’s. Other schemes, such as leanSPHINCS once its parameters settle, would define theirs in their own specs. Is that the right split, or should this ERC keep a registry?
  4. Stateful keys. Is “a random instance, never restored” right, or is there a case for a deterministic counter, for example on devices without reliable storage?
  5. Hardware wallets. Is anything here hard on a device that stores only the BIP-39 seed?
  6. Agents and other automated signers. The draft says a signer that can be copied (a VM snapshot, replicas, an agent started several times from one image) must not hold one-time keys, because two copies would sign with the same key. It asks automated signers to hold their own random root, not the phrase, and to use key types whose signature limit they cannot reach. If you build agent wallets: does that match how your agents hold keys, and what is missing?
1 Like

Suppose I migrate an account to a new SLH-DSA parameter set and later restore it in another wallet. The same phrase can derive both the old and new keys. How does the new wallet discover which one the account currently accepts? Is the rotation certificate discoverable from the account or does the user need to keep a separate record?

Good question. The two parameter sets are two key types, so the same phrase gives two independent keys under the same account number. A restoring wallet derives the key of every type it supports and checks which one the account accepts. It needs no separate record and no rotation certificate, because it reads the account’s current state.

The harder part is finding the account when its address came from the old key: the wallet has to keep deriving key types it has retired. I’ve added that to the restore section:

How an account exposes its current key is outside this ERC, but two drafts already give a wallet that read: ERC-8403 (authorityRoot() and its recovery scheme) and EIP-8130 (getActorConfig). Does that cover your case, or do you have an account in mind where the current key can’t be read or tested?

That covers my case. I’d missed that the wallet may still need the old key to find the account before it can check which key the account accepts now. Thanks for adding that to the restore section.

On the fingerprint, I’d fix the format, and compute it from a public key, say account 0’s SLH-DSA-SHA2-128s key, the way BIP-32 takes its fingerprint from the master public key. Users will write it next to the phrase. If it comes straight from R or the seed, anyone holding that paper can test passphrase guesses offline at PBKDF2 speed, and hidden wallets are exactly the ones with nothing on chain to test against otherwise. From a public key, every guess also costs a full key generation.

On stateful keys, random looks right, and a counter fails on the devices you mention: without reliable storage, a reset or a rolled back flash hands out the same counter value twice, and the same one time key signs twice. With random instances, the 32 bytes are the only thing separating two stateful keys under one root, so a device whose RNG repeats after a cold boot recreates an old key with a fresh state. I’d add that instances need a seeded CSPRNG, and that a device that can’t keep state reliably shouldn’t hold stateful keys at all.

Thanks Carlos, both are in the draft now.

It’s been a long time since I used anything but ECDSA recovery phrases, where the address does that job. Fingerprints I only know from pass and email encryption, so I had left that part loose. It is fixed now, as you suggest: the first 4 bytes of SHA-256 of account 0’s SLH-DSA-SHA2-128s public key, as 8 hex digits, and never computed from the root or a seed directly. On a server CPU that key generation took 72 ms against 3.6 ms for the PBKDF2, so each guess against a written-down fingerprint costs about 20 times as much as the PBKDF2 alone. Showing it stays a SHOULD, since it costs a wallet one key generation per restore.

On stateful keys, agreed. The Security Considerations now ask for a generator seeded from a source that doesn’t repeat across restarts, and say that a device that can’t keep state reliably must not hold a stateful instance.

Commit: Add ERC: Hash-Based Key Derivation from BIP-39 by johba37 · Pull Request #2172 · ethereum/ERCs · GitHub

The signature-budget guidance says wallets SHOULD count signatures per key, but restoring from the phrase alone recreates the same stateless key without its usage count. Importing the phrase on a second device also lets both wallets consume the same budget. On-chain history would miss signatures released off chain. For parameter sets with a realistically reachable limit, a local counter therefore seems insufficient. What should the restore guidance require when the wallet cannot establish the key’s previous usage?

From the wallet support side, the problem this solves is very real: “I imported my phrase and my balance is empty” is one of the most common worries, and it’s usually two wallets deriving keys differently, not lost funds. It would help if the restore guidance also asked wallets to say when a phrase might hold keys they don’t support, so people know to try another wallet instead of assuming the worst.

Thanks Mikhail, that was a gap. The draft said “count” and left out that nothing restores the count.

A signature count is state, and a phrase restores none. The draft said that for stateful keys only. A counter can’t fix it, for the reasons you give, so the draft now changes what such a key is used for:

  • A wallet must not assume that a restored key has not signed.
  • Where a key type’s limit is one that use could reach, a key of that type should come from a phrase only to certify other keys. Each device signs with a key of its own, and a restore costs the phrase’s key one signature, on the new device’s certificate. Two devices then never share a budget, and each device’s count is true, because its key was made there.
  • A wallet that signs transactions with such a key anyway, and can’t establish the count after a restore, signs only what replaces the key.

For the FIPS 205 sets, with 2^64 signatures, nothing changes. The arrangement isn’t new: NIST SP 800-208 (section 7) describes it for a stateful key spread over several modules, with a top-level key that only signs the roots of trees made inside each module. What a certificate says is left to the account, outside this ERC.

The fallback is affordable because these keys weaken gradually past their limit. For SLH-DSA-SHA2-128-24 I get about 5 bits lost at twice the limit and about 21 at sixteen times, with the formula from the SPHINCS+ submission.

My own wallet works this way: the phrase’s key signs device certificates and revocations only, against a budget of about 210,000.

Commit: Add ERC: Hash-Based Key Derivation from BIP-39 by johba37 · Pull Request #2172 · ethereum/ERCs · GitHub

1 Like

Thanks Anzus, that is the case the draft exists for, and it didn’t say what a wallet tells the user when it comes up empty. It does now.

The restore section says that finding no key in use doesn’t show the phrase holds none, and that the wallet should say so, name the key types it tried, and say that another wallet may find keys it didn’t. It lists the causes you’d see in support: a key type this wallet doesn’t support, a wallet that derived its keys another way, accounts used out of order, and a mistyped passphrase, which the fingerprint catches.

Commit: Add ERC: Hash-Based Key Derivation from BIP-39 by johba37 · Pull Request #2172 · ethereum/ERCs · GitHub

A question back, since you see that screen from the support side: would naming the key types the wallet tried help people, or is “try the wallet that created this phrase” all they can use?

1 Like

Thanks for adding that. For most people, “try the wallet that created this phrase” is the part they can act on, so I’d put that first. Key type names mean little to them, but they help a lot when someone contacts support, so I’d still show them as details rather than leave them out.

that settles the order. The draft now has the wallet say first what the user can do, try the wallet that created the phrase, and name the key types it tried as details for whoever helps them.

Commit: Add ERC: Hash-Based Key Derivation from BIP-39 by johba37 · Pull Request #2172 · ethereum/ERCs · GitHub

1 Like

Thanks, the device-key split makes sense. One remaining point: the phrase-derived certification key still shares a budget across restores, without recovering its usage count. Could the draft make the budget assumption for that key explicit?

Yes, it was only implied. The draft now states it: either the key signs only when a person acts, and its limit exceeds that use by a wide margin, or the wallet has to treat it as a key that signs transactions. On restore, a wallet can only get a lower bound, from the certificates the account has recorded.

Commit: ethereum/ERCs#2172 (commits)

1 Like

Independent recomputation of the Test Cases, against PR #2172 head 76de577 (ERCS/erc-8499.md).

I wrote my own code for this: HMAC and HKDF with SHA-512, BIP-39’s PBKDF2, and SLH-DSA key generation from FIPS 205 for the SHA2 and SHAKE sets (pure Python on hashlib, no library from the draft). I parsed the vectors from the markdown instead of retyping them.

What matched, 31 of 31 values:

  • the BIP-39 seeds S of all three mnemonics, including both TREZOR vectors, and the roots R, and the random root;
  • the stateless seeds for accounts 0 and 1, the stateful seeds, the application secrets (also L = 48), and the max-account values of the random root;
  • the 47-byte info of the first stateless seed;
  • the seeds and public keys of SLH-DSA-SHA2-128s, SLH-DSA-SHA2-192s and SLH-DSA-SHAKE-256s, and the fingerprint 35d10f26.

Not checked: I read the Python reference implementation against the Specification but did not run it. I did not check the mnemonic checksum rejection (it needs the wordlist). Nine of the twelve SLH-DSA key types have no public key in the vectors (the f sets, SHA2-256, SHAKE-128 and SHAKE-192), so nothing here says anything about them.

One suggestion: a vector for one f set (different d and h') would cover tree shapes that the current three do not. I can compute one with my code if that is useful, but it would be a second implementation only, not a published reference value.

Thanks, a second implementation of all 31 values is useful to have.
I added the vector you suggest: SLH-DSA-SHAKE-128f, same mnemonic and account. Its key generation builds a top tree of height 3 at layer 21, and it is the first SHAKE vector with n = 16. Two libraries agree on the public key here. Does your code give the same? Any chance to have a look at it?
Commit: Add ERC: Hash-Based Key Derivation from BIP-39 by johba37 · Pull Request #2172 · ethereum/ERCs · GitHub

Checked against PR #2172 head 69d3d04, with the same code as before, only its parameter table extended with the f shapes (my own FIPS 205 key generation, no library from the draft).

For SLH-DSA-SHAKE-128f it gives the same public key as the draft: b5deb0a4a71c25b9dbaf6f870b09470e6a193927b7519f45abde3cc062c9751b, from the seed shown. So a second implementation agrees here too. With the new vector all 33 values in Test Cases match (the 31 from before plus this seed and public key).

Control: if I build the same key with the shape of the s sets (d = 7 and h’ = 9 instead of 22 and 3), the public key differs from the draft’s; so does d = 7 with h’ = 3. So this vector does depend on the f shape (22 layers, top tree of height 3), which the earlier three did not.

Still not covered by any published public key: SHA2-128f, the 192 and 256 f sets, SHA2-256s, SHAKE-128s and SHAKE-192s. I only compared the one f set above.

@johba one thing in the stateful section I’d tighten. A key type may give context to its key generation as a public input, for example as the public seed of a tree, and context is suggested as a hash of the chain, the account and the purpose. Then every instance made for the same use shares one public seed. In XMSS and SPHINCS+ the public seed is what keeps the hash calls of one key pair apart from another’s, so instances sharing it hand an attacker more targets per hash query, and one-time keys get created a lot.

I’d say a key type that takes context as a public input still makes the public seed unique per instance, for example a hash of context and instance, or bytes cut from the seed, and keeps context as the binding to its use. The 32 random bytes of instance are already there for that.

Thanks Carlos, you’re right. In my implementation the context names the tree’s position (device, generation, first nonce), so each tree got its own public seed, and I didn’t see that the text allowed a shared one.

The draft now requires that the public seed, or whatever else separates one key pair’s hash calls from another’s, differs between instances. Where instances can share a context, the key type computes it from context and instance, or cuts it from the seed. Security Considerations says why.

Commit: Add ERC: Hash-Based Key Derivation from BIP-39 by johba37 · Pull Request #2172 · ethereum/ERCs · GitHub

ERC-8499 is merged as a Draft. @it44 @Zealynx, one change touches your checks: the two example key types have new names (example.org/stateless/v1 and example.org/one-time/v1), so eleven seed values in Test Cases changed. The SLH-DSA vectors and the fingerprint are the same. Update ERC-8499: Key type names and the signature limit by johba37 · Pull Request #2233 · ethereum/ERCs · GitHub