Bitcoin BIP39 Passphrases and Unicode Normalization
BIP39 requires UTF-8 NFKD normalization of both mnemonic and passphrase. Every passphrase produces a seed, so a typo may silently select a different wallet. ASCII inputs are already normalized; the example rejects non-ASCII rather than pretending to implement Unicode normalization.
Go implementation
94func SeedASCII(mnemonic, passphrase string) ([64]byte, error) {
95 var out [64]byte
96 for _, s := range []string{mnemonic, passphrase} {
97 for _, r := range s {
98 if r > 127 {
99 return out, errors.New("ASCII only; full BIP39 requires NFKD")
100 }
101 }
102 }
103 salt := append([]byte("mnemonic"+passphrase), 0, 0, 0, 1)
104 u := mac([]byte(mnemonic), salt)
105 copy(out[:], u)
106 for j := 1; j < 2048; j++ {
107 u = mac([]byte(mnemonic), u)
108 for k := range out {
109 out[k] ^= u[k]
110 }
111 }
112 return out, nil
113}Practical exercise
Change the public fixture passphrase and observe a different seed. Try a non-ASCII input and confirm rejection.
Local verification
From the repository directory golang-bitcoin/004-hd-wallets, run:
go test -race -v ./...
1package hd
2
3import (
4 "encoding/hex"
5 "reflect"
6 "testing"
7)
8
9func TestMaster(t *testing.T) {
10 seed, _ := hex.DecodeString("000102030405060708090a0b0c0d0e0f")
11 m, e := Master(seed)
12 if e != nil {
13 t.Fatal(e)
14 }
15 if hex.EncodeToString(m.Key[:]) != "e8f32e723decf4051aefac8e2c93c9c5b214313817cdb01a1494b917c8436b35" || hex.EncodeToString(m.Chain[:]) != "873dff81c02f525623fd1fe5167eac3a55a049de3d314bb42ee227ffed37d508" {
16 t.Fatal("master vector")
17 }
18 c, e := m.ChildHardened(Hardened)
19 if e != nil {
20 t.Fatal(e)
21 }
22 if hex.EncodeToString(c.Chain[:]) != "47fdacbd0f1097043b78c63c20c34ef4ed9a111d980047ad16282c7ae6236141" {
23 t.Fatal("child chain")
24 }
25 if _, e = m.ChildHardened(0); e == nil {
26 t.Fatal("normal index accepted")
27 }
28}
29func TestSeed(t *testing.T) {
30 s, e := SeedASCII("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about", "TREZOR")
31 if e != nil || hex.EncodeToString(s[:]) != "c55257c360c07c72029aebc1b53c05ed0362ada38ead3e3e9efa3708e53495531f09a6987599d18264c1e1c92f2cf141630c7a3c4ab7c81b2f001698e7463b04" {
32 t.Fatal("seed vector", e)
33 }
34 if _, e = SeedASCII("é", ""); e == nil {
35 t.Fatal("non ASCII accepted")
36 }
37}
38func TestPath(t *testing.T) {
39 p, e := ParsePath("m/84'/0'/0'/0/7")
40 if e != nil || !reflect.DeepEqual(p, []uint32{Hardened + 84, Hardened, Hardened, 0, 7}) {
41 t.Fatal(p, e)
42 }
43 for _, s := range []string{"M/0", "m/", "m/-1", "m/2147483648", "m/+1"} {
44 if _, e := ParsePath(s); e == nil {
45 t.Fatal(s)
46 }
47 }
48}
49func TestInvalid(t *testing.T) {
50 if _, e := Master(nil); e == nil {
51 t.Fatal("short seed")
52 }
53 if _, e := (ExtendedPrivate{}).ChildHardened(Hardened); e == nil {
54 t.Fatal("zero parent")
55 }
56}Scope and security
This is an offline educational implementation, not a production wallet. Private scalar arithmetic uses math/big, which is not constant-time. No real secrets, mnemonic generation, extended-key serialization, normal child derivation, public child derivation or transaction signing are provided.
References
https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki
https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
https://thamer.org/repo/golang-bitcoin/004-hd-wallets/
