THAMER
Sovereign Bitcoin Wallet

WALLET AI BITCOIN

Your Bitcoin Wallet. Your Keys. Your Engine.

Self-hosted Bitcoin software built for developers, applications, and AI agents.

CREATE WALLET

Hardened Bitcoin Child Key Derivation in Go

permalink THAMER
#bip32#bitcoin#bitcoin-script#golang#hd-wallet

Hardened CKDpriv hashes a zero byte, the 32-byte parent private key and a four-byte big-endian index using the parent chain code as HMAC key. The left half is added to the parent scalar modulo the curve order. The right half becomes the child chain code.

Go implementation

Go · 22 lines
41func (p ExtendedPrivate) ChildHardened(index uint32) (ExtendedPrivate, error) {
42	var out ExtendedPrivate
43	if index < Hardened || !valid(p.Key[:]) {
44		return out, errors.New("invalid hardened derivation input")
45	}
46	var data [37]byte
47	copy(data[1:33], p.Key[:])
48	binary.BigEndian.PutUint32(data[33:], index)
49	i := mac(p.Chain[:], data[:])
50	left := new(big.Int).SetBytes(i[:32])
51	if left.Cmp(order) >= 0 {
52		return out, errors.New("invalid child tweak")
53	}
54	left.Add(left, new(big.Int).SetBytes(p.Key[:]))
55	left.Mod(left, order)
56	if left.Sign() == 0 {
57		return out, errors.New("zero child scalar")
58	}
59	left.FillBytes(out.Key[:])
60	copy(out.Chain[:], i[32:])
61	return out, nil
62}
Source: /repo/golang-bitcoin/004-hd-wallets/hd.go · Raw file · Read-only excerpt

Practical exercise

Use index 2147483648 for the first hardened child. A normal index is rejected by this deliberately restricted implementation.

Local verification

From the repository directory golang-bitcoin/004-hd-wallets, run:

go test -race -v ./...
Go · 56 lines
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-0032.mediawiki

https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki

https://thamer.org/repo/golang-bitcoin/004-hd-wallets/

Questions & Answers

Explore all questions →

Latest from the Lab

THAMER illustrated developer portrait and signature

ABOUT THE DEVELOPER

Independent Software Developer specializing in Bitcoin Script, Golang, and AI Agents.

Software engineering · Independent research · Open-source projects