Skip to content

Depth overflow when deriving the 256th extended keys and onwards #71

@quapka

Description

@quapka

Hi folks,
The serialization format described in BIP32 indirectly restricts valid keys to depth in the range [0, 255], because the depth is serialized as a single byte. The Depth of the keys on lower levels overflows the byte and wraps around.

There might be other derivation code paths in gocoin that achieve the overlow, right now I see at least this one. To see this happening, consider the following test:

diff --git a/lib/btc/wallethd_test.go b/lib/btc/wallethd_test.go
index 27a29e24..bd0bbff7 100644
--- a/lib/btc/wallethd_test.go
+++ b/lib/btc/wallethd_test.go
@@ -6,6 +6,7 @@ This code is taken from:
 package btc

 import (
+    "fmt"
        "bytes"
        "encoding/hex"
        "testing"
@@ -210,6 +211,15 @@ func TestChildren(t *testing.T) {
        }
 }

+func TestDepthOverflow(t *testing.T) {
+       hdwal := MasterKey([]byte("Random seed"), false)
+
+       for i := 0; i < 1000; i++ {
+               hdwal = hdwal.Child(uint32(i | 0x80000000))
+        fmt.Println(hdwal.Depth);
+       }
+}
+
 // benchmarks

 func BenchmarkStringChildPub(b *testing.B) {

Check this bitcoin test case for a similar situations.

While internal "deeper" keys might work fine, their serialization and sharing across other implementation might cause problems.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions