Skip to content

Commit 67d9741

Browse files
author
sammyne
committed
perf(PrivateKey): some benchmarkings
1 parent bd491c9 commit 67d9741

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

bench_test.go

+22-20
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1+
// Copyright (c) 2018-2019 sammyne
12
// Copyright (c) 2014 The btcsuite developers
23
// Use of this source code is governed by an ISC
34
// license that can be found in the LICENSE file.
45

56
package bip32_test
67

7-
/*
88
import (
99
"testing"
1010

11-
"github.com/btcsuite/btcutil/hdkeychain"
11+
"github.com/sammyne/bip32"
1212
)
1313

1414
// bip0032MasterPriv1 is the master private extended key from the first set of
@@ -20,22 +20,23 @@ const bip0032MasterPriv1 = "xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbP" +
2020
// child from a master private extended key.
2121
func BenchmarkDeriveHardened(b *testing.B) {
2222
b.StopTimer()
23-
masterKey, err := hdkeychain.NewKeyFromString(bip0032MasterPriv1)
23+
masterKey, err := bip32.ParsePrivateKey(bip0032MasterPriv1)
2424
if err != nil {
2525
b.Errorf("Failed to decode master seed: %v", err)
2626
}
2727
b.StartTimer()
2828

2929
for i := 0; i < b.N; i++ {
30-
masterKey.Child(hdkeychain.HardenedKeyStart)
30+
masterKey.Child(bip32.HardenedKeyStart)
3131
}
3232
}
3333

34-
// BenchmarkDeriveNormal benchmarks how long it takes to derive a normal
34+
// BenchmarkDeriveNonhardened benchmarks how long it takes to derive a normal
3535
// (non-hardened) child from a master private extended key.
36-
func BenchmarkDeriveNormal(b *testing.B) {
36+
func BenchmarkDeriveNonhardened(b *testing.B) {
3737
b.StopTimer()
38-
masterKey, err := hdkeychain.NewKeyFromString(bip0032MasterPriv1)
38+
//masterKey, err := hdkeychain.NewKeyFromString(bip0032MasterPriv1)
39+
masterKey, err := bip32.ParsePrivateKey(bip0032MasterPriv1)
3940
if err != nil {
4041
b.Errorf("Failed to decode master seed: %v", err)
4142
}
@@ -46,34 +47,36 @@ func BenchmarkDeriveNormal(b *testing.B) {
4647
}
4748
}
4849

49-
// BenchmarkPrivToPub benchmarks how long it takes to convert a private extended
50-
// key to a public extended key.
51-
func BenchmarkPrivToPub(b *testing.B) {
50+
// BenchmarkPrivateKey_Public benchmarks how long it takes to convert a private
51+
// extended key to a public extended key.
52+
func BenchmarkPrivateKey_Public(b *testing.B) {
5253
b.StopTimer()
53-
masterKey, err := hdkeychain.NewKeyFromString(bip0032MasterPriv1)
54+
masterKey, err := bip32.ParsePrivateKey(bip0032MasterPriv1)
5455
if err != nil {
5556
b.Errorf("Failed to decode master seed: %v", err)
5657
}
5758
b.StartTimer()
5859

5960
for i := 0; i < b.N; i++ {
6061
masterKey.Neuter()
62+
// invalid the cached public key
63+
masterKey.PublicKey.Data = nil
6164
}
6265
}
6366

64-
// BenchmarkDeserialize benchmarks how long it takes to deserialize a private
65-
// extended key.
66-
func BenchmarkDeserialize(b *testing.B) {
67+
// BenchmarkParsePrivateKey benchmarks how long it takes to deserialize a
68+
// private extended key.
69+
func BenchmarkParsePrivateKey(b *testing.B) {
6770
for i := 0; i < b.N; i++ {
68-
hdkeychain.NewKeyFromString(bip0032MasterPriv1)
71+
bip32.ParsePrivateKey(bip0032MasterPriv1)
6972
}
7073
}
7174

72-
// BenchmarkSerialize benchmarks how long it takes to serialize a private
73-
// extended key.
74-
func BenchmarkSerialize(b *testing.B) {
75+
// BenchmarkPrivateKey_String benchmarks how long it takes to serialize a
76+
// private extended key.
77+
func BenchmarkPrivateKey_String(b *testing.B) {
7578
b.StopTimer()
76-
masterKey, err := hdkeychain.NewKeyFromString(bip0032MasterPriv1)
79+
masterKey, err := bip32.ParsePrivateKey(bip0032MasterPriv1)
7780
if err != nil {
7881
b.Errorf("Failed to decode master seed: %v", err)
7982
}
@@ -83,4 +86,3 @@ func BenchmarkSerialize(b *testing.B) {
8386
_ = masterKey.String()
8487
}
8588
}
86-
*/

private_key.go

+2
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ func (priv *PrivateKey) publicKeyData() []byte {
175175
// be a convenience method used to create a populated struct. This function
176176
// should only by used by applications that need to create custom PrivateKey.
177177
// All other applications should just use NewMasterKey, Child, or Neuter.
178+
// **The public key part is left uninitialized yet**
178179
func NewPrivateKey(version []byte, depth uint8, parentFP []byte, index uint32,
179180
chainCode, data []byte) *PrivateKey {
180181
pub := PublicKey{
@@ -195,6 +196,7 @@ func NewPrivateKey(version []byte, depth uint8, parentFP []byte, index uint32,
195196

196197
// ParsePrivateKey a new extended private key instance out of a base58-encoded
197198
// extended key.
199+
// **The public key part is left uninitialized yet**
198200
func ParsePrivateKey(data58 string) (*PrivateKey, error) {
199201
// decodePublicKey is applicable here too !!!
200202
pub, err := decodePublicKey(data58)

0 commit comments

Comments
 (0)