bitcoin

package
v1.2.35 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 29, 2026 License: AGPL-3.0 Imports: 10 Imported by: 0

Documentation

Overview

Package bitcoin implements the basic components for Bitcoin processing

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBtcBase58Decoding = errors.New("base58 decoding error -- unknown character")
)

Error codes

View Source
var (
	ErrSigInvalid = errors.New("invalid ECDSA signature")
)

Inf is the point at "infinity"

Functions

func Base58Decode

func Base58Decode(s string) (data []byte, err error)

Base58Decode converts a base58 representation into byte array

func Base58Encode

func Base58Encode(in []byte) string

Base58Encode converts byte array to base58 string representation

func ConvertHash added in v1.2.30

func ConvertHash(hash []byte) *math.Int

ConvertHash to integer [http://www.secg.org/download/aid-780/sec1-v2.pdf]

func ExportPrivateKey

func ExportPrivateKey(k *PrivateKey, testnet bool) string

ExportPrivateKey returns a private key in SIPA format

func Hash160

func Hash160(data []byte) []byte

Hash160 computes RIPEMD-160(SHA-256(data))

func Hash256

func Hash256(data []byte) []byte

Hash256 computes SHA-256(SHA-256(data))

func RipeMD160

func RipeMD160(data []byte) []byte

RipeMD160 computes RIPEMD160(data)

func Sha1

func Sha1(data []byte) []byte

Sha1 computes SHA1(data)

func Sha256

func Sha256(data []byte) []byte

Sha256 computes SHA-256(data)

func SignY

func SignY(p *Point) (int, error)

SignY of a point (y-coordinate) on the Bitcoin curve: +1 if above x-axis, -1 if below.

func Solve

func Solve(x *math.Int) (*math.Int, bool)

Solve the curve equation for given x-ccordinate (returns positive y)

func Verify

func Verify(key *PublicKey, hash []byte, sig *Signature) bool

Verify a hash value with public key. [http://www.nsa.gov/ia/_files/ecdsa.pdf, page 15f]

Types

type Curve

type Curve struct {
	// P is the generator of the underlying field "F_p"
	// = 2^256 - 2^32 - 2^9 - 2^8 - 2^7 - 2^6 - 2^4 - 1
	P *math.Int
	// Gx is the x-coord of the base point
	Gx *math.Int
	// Gy is the y-coord of the base point
	Gy *math.Int
	// N is the order of G
	N *math.Int
	// curve parameter (=7)
	B *math.Int
}

Curve is the elliptic curve used for Bitcoin (Secp256k1)

func GetCurve

func GetCurve() *Curve

GetCurve returns the singleton curve instance

type Point

type Point struct {
	// contains filtered or unexported fields
}

Point (x,y) on the curve

func GetBasePoint

func GetBasePoint() *Point

GetBasePoint returns the base Point of the curve

func MultBase

func MultBase(k *math.Int) *Point

MultBase multiplies the base Point of the curve with a scalar value k

func NewPoint

func NewPoint(a, b *math.Int) *Point

NewPoint instaniates a new Point

func NewPointFromBytes

func NewPointFromBytes(b []byte) (p *Point, compr bool, err error)

NewPointFromBytes reconstructs a Point from binary representation.

func RndPoint added in v1.2.35

func RndPoint() (d *math.Int, p *Point)

RndPoint creates a random point p with scalar d

func RndPointParity added in v1.2.35

func RndPointParity(even bool) (d *math.Int, p *Point)

RndPointParity creates a random point p with scalar d of given parity

func (*Point) Add

func (p *Point) Add(q *Point) *Point

Add two Points on the curve

func (*Point) Bytes

func (p *Point) Bytes(compressed bool) []byte

Bytes returns a byte representation of Point (compressed or uncompressed).

func (*Point) Double

func (p *Point) Double() *Point

Double a Point on the curve

func (*Point) Equals

func (p *Point) Equals(q *Point) bool

Equals checks if two Points are equal

func (*Point) IsInf

func (p *Point) IsInf() bool

IsInf checks if a Point is at infinity

func (*Point) IsNull added in v1.2.35

func (p *Point) IsNull() bool

IsNull returns true if the point is Inf

func (*Point) IsOnCurve

func (p *Point) IsOnCurve() bool

IsOnCurve checks if a Point (x,y) is on the curve

func (*Point) Mult

func (p *Point) Mult(k *math.Int) *Point

Mult multiplies a Point on the curve with a scalar value k using a Montgomery multiplication approach

func (*Point) Neg

func (p *Point) Neg() *Point

Neg returns -P for the point P.

func (*Point) String

func (p *Point) String() string

String returns a human-readable representation of a point.

func (*Point) X

func (p *Point) X() *math.Int

X returns the x-coordinate of a point.

func (*Point) Y

func (p *Point) Y() *math.Int

Y returns the y-coordinate of a point.

type PrivateKey

type PrivateKey struct {
	PublicKey
	D *math.Int
}

PrivateKey is a random factor 'd' for the base Point that yields the associated PublicKey (Point on the curve (x,y) = d*G)

func GenerateKeys

func GenerateKeys(compr bool) *PrivateKey

GenerateKeys creates a new set of keys. [http://www.nsa.gov/ia/_files/ecdsa.pdf] page 19f but with a different range (value 1 and 2 for exponent are not allowed)

func ImportPrivateKey

func ImportPrivateKey(keydata string, testnet bool) (*PrivateKey, error)

ImportPrivateKey imports a private key in SIPA format

func PrivateKeyFromBytes

func PrivateKeyFromBytes(b []byte) (*PrivateKey, error)

PrivateKeyFromBytes returns a private key from a byte representation.

func (*PrivateKey) Bytes

func (k *PrivateKey) Bytes() []byte

Bytes returns a byte representation of private key.

type PublicKey

type PublicKey struct {
	Q            *Point
	IsCompressed bool
}

PublicKey is a Point on the elliptic curve: (x,y) = d*G, where 'G' is the base Point of the curve and 'd' is the secret private factor (private key)

func PublicKeyFromBytes

func PublicKeyFromBytes(b []byte) (*PublicKey, error)

PublicKeyFromBytes returns a public key from a byte representation.

func (*PublicKey) Bytes

func (k *PublicKey) Bytes() []byte

Bytes returns the byte representation of public key.

type Signature

type Signature struct {
	R, S *math.Int
}

Signature is a Bitcoin signature in scripts.

func NewSignatureFromASN1

func NewSignatureFromASN1(b []byte) (*Signature, error)

NewSignatureFromASN1 returns a signature from an ASN.1-encoded sequence.

func NewSignatureFromBytes added in v1.2.30

func NewSignatureFromBytes(b []byte) (sig *Signature, err error)

func Sign

func Sign(key *PrivateKey, hash []byte) *Signature

Sign a hash value with private key. [http://www.nsa.gov/ia/_files/ecdsa.pdf, page 13f]

func (*Signature) Bytes

func (s *Signature) Bytes() ([]byte, error)

Bytes returns an ASN.1-encoded sequence of the signature.

Directories

Path Synopsis
Package script handles Bitcoin scripts
Package script handles Bitcoin scripts
tools
passphrase2seed command
vanityaddress command
Package wallet handles wallets for cryptocoins
Package wallet handles wallets for cryptocoins

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL