Skip to content
/ cbip32 Public

A fast, secure, low-dependency C implementation of BIP32

Notifications You must be signed in to change notification settings

jamesob/cbip32

Folders and files

NameName
Last commit message
Last commit date

Latest commit

a4ceec1 · Mar 27, 2025

History

26 Commits
Feb 3, 2025
Mar 27, 2025
Mar 27, 2025
Jan 27, 2025
Mar 27, 2025
Mar 5, 2025
Mar 5, 2025
Jan 27, 2025
Mar 27, 2025
Mar 27, 2025
Jan 27, 2025
Jan 27, 2025

Repository files navigation

cbip32

test workflow

A fast, secure, low-dependency implementation of BIP32

Installation from source

Install libsecp256k1

git clone https://github.com/bitcoin-core/secp256k1.git && \
  cd secp256k1 && ./autogen.sh && ./configure && make && sudo make install

# NOTE: in production, you should verify the associated GPG sigs and/or pin hashes.

Install libsodium

git clone https://github.com/jedisct1/libsodium.git && \
  cd libsodium && ./autogen.sh -sb && ./configure && make && sudo make install

# NOTE: in production, you should verify the associated GPG sigs and/or pin hashes.

Install this library

git clone https://github.com/jamesob/cbip32.git && \
  make && sudo make install

Tests

Performance

The Python bindings for this implementation have been shown to be

  • ~2x faster than python-bip32, an implementation based on coincurve which itself wraps libsecp256k1
  • >100x faster than verystable, which is a "dumb" pure Python implementation.

From fuzzing:

  - Highest target scores [time per derivation]:
     0.0162932  (label='ours')
     0.0379755  (label='python-bip32')

  - Highest target scores [time per derivation]:
     0.00222896  (label='ours')
     0.315636    (label='verystable')

Example bindings included

>>> from bindings import derive
>>> derive('0' * 32, 'm/1/2h').get_public().serialize()
'xpub69s9RVsS4kfK2VFed2giqFm9gQ4VmCWuWcPHFJ51Rj6dHvBjCicCZm2HR88Z6J5zRYyHkt7W9LPygBc57RCCPp2t1AxCNa1VtvSq4qWYLqK'
package cbip32

// #cgo CFLAGS: -I.
// #cgo LDFLAGS: -lbip32
// #include "bip32.h"
import "C"
import (
    "unsafe"
)

type BIP32Key struct {
    cKey C.bip32_key
}

func NewKey() *BIP32Key {
    key := &BIP32Key{}
    C.bip32_init(&key.cKey)
    return key
}

func Derive(source, path string) (*BIP32Key, bool) {
    key := NewKey()
    cSource := C.CString(source)
    cPath := C.CString(path)
    defer C.free(unsafe.Pointer(cSource))
    defer C.free(unsafe.Pointer(cPath))

    result := C.bip32_derive(&key.cKey, cSource, cPath)
    return key, result == 1
}

About

A fast, secure, low-dependency C implementation of BIP32

Resources

Stars

Watchers

Forks

Packages

No packages published