secp256k1

package
v0.0.0-...-84d53aa Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2019 License: GPL-3.0 Imports: 12 Imported by: 10

Documentation

Overview

Package secp256k1 wraps the bitcoin secp256k1 C library.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidMsgLen       = errors.New("invalid message length, need 32 bytes")
	ErrInvalidSignatureLen = errors.New("invalid signature length")
	ErrInvalidRecoveryID   = errors.New("invalid signature recovery id")
	ErrInvalidKey          = errors.New("invalid private key")
	ErrInvalidPubkey       = errors.New("invalid public key")
	ErrSignFailed          = errors.New("signing failed")
	ErrRecoverFailed       = errors.New("recovery failed")
)

Error types

Functions

func CompressPubkey

func CompressPubkey(x, y *big.Int) ([]byte, error)

CompressPubkey encodes a public key to 33-byte compressed format.

func DecompressPubkey

func DecompressPubkey(pubkey []byte) (x, y *big.Int, err error)

DecompressPubkey parses a public key in the 33-byte compressed format. It returns non-nil coordinates if the public key is valid.

func FromECDSAPrivateKey

func FromECDSAPrivateKey(priv *ecdsa.PrivateKey) []byte

FromECDSAPrivateKey exports a private key into a binary dump.

func FromECDSAPublicKey

func FromECDSAPublicKey(pub *ecdsa.PublicKey) ([]byte, error)

FromECDSAPublicKey exports a public key into a binary dump.

func HexToECDSA

func HexToECDSA(hexkey string) (*ecdsa.PrivateKey, error)

HexToECDSA gets a private key from hex string.

func NewECDSAPrivateKey

func NewECDSAPrivateKey() *ecdsa.PrivateKey

NewECDSAPrivateKey generate a ecdsa private key

func RecoverPubkey

func RecoverPubkey(msg []byte, sig []byte) ([]byte, error)

RecoverPubkey returns the the public key of the signer. msg must be the 32-byte hash of the message to be signed. sig must be a 65-byte compact ECDSA signature containing the recovery id as the last element.

func SeckeyVerify

func SeckeyVerify(seckey []byte) bool

SeckeyVerify check private is ok for secp256k1

func Sign

func Sign(msg []byte, seckey []byte) ([]byte, error)

Sign creates a recoverable ECDSA signature. The produced signature is in the 65-byte [R || S || V] format where V is 0 or 1.

The caller is responsible for ensuring that msg cannot be chosen directly by an attacker. It is usually preferable to use a cryptographic hash function on any input before handing it to this function.

func ToECDSAPrivateKey

func ToECDSAPrivateKey(d []byte) (*ecdsa.PrivateKey, error)

ToECDSAPrivateKey gets a private key from bytes.

func ToECDSAPrivateKeyUnsafe

func ToECDSAPrivateKeyUnsafe(d []byte) *ecdsa.PrivateKey

ToECDSAPrivateKeyUnsafe is ToECDSAPrivateKey's unsafe function.

func ToECDSAPublicKey

func ToECDSAPublicKey(pub []byte) (*ecdsa.PublicKey, error)

ToECDSAPublicKey creates a public key with the given data value.

func VerifySignature

func VerifySignature(pubkey, msg, signature []byte) bool

VerifySignature checks that the given pubkey created signature over message. The signature should be in [R || S] format.

Types

type BitCurve

type BitCurve struct {
	P       *big.Int // the order of the underlying field
	N       *big.Int // the order of the base point
	B       *big.Int // the constant of the BitCurve equation
	Gx, Gy  *big.Int // (x,y) of the base point
	BitSize int      //the size of the underlying field
}

A BitCurve represents a Koblitz Curve with a=0. See http://www.hyperelliptic.org/EFD/g1p/auto-shortw.html

func S256

func S256() *BitCurve

S256 returns a BitCurve which implements secp256k1.

func (*BitCurve) Add

func (BitCurve *BitCurve) Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int)

Add returns the sum of (x1,y1) and (x2,y2)

func (*BitCurve) Double

func (BitCurve *BitCurve) Double(x1, y1 *big.Int) (*big.Int, *big.Int)

Double returns 2*(x,y)

func (*BitCurve) IsOnCurve

func (BitCurve *BitCurve) IsOnCurve(x, y *big.Int) bool

IsOnCurve returns true if the given (x,y) lies on the BitCurve.

func (*BitCurve) Marshal

func (BitCurve *BitCurve) Marshal(x, y *big.Int) []byte

Marshal converts a point into the form specified in section 4.3.6 of ANSI X9.62.

func (*BitCurve) Params

func (BitCurve *BitCurve) Params() *elliptic.CurveParams

Params returns elliptic curve params.

func (*BitCurve) ScalarBaseMult

func (BitCurve *BitCurve) ScalarBaseMult(k []byte) (*big.Int, *big.Int)

ScalarBaseMult returns k*G, where G is the base point of the group and k is an integer in big-endian form.

func (*BitCurve) ScalarMult

func (BitCurve *BitCurve) ScalarMult(Bx, By *big.Int, scalar []byte) (*big.Int, *big.Int)

ScalarMult multiply scalar.

type PrivateKey

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

PrivateKey ecdsa privatekey

func GeneratePrivateKey

func GeneratePrivateKey() *PrivateKey

GeneratePrivateKey generate a new private key

func NewPrivateKey

func NewPrivateKey(key *ecdsa.PrivateKey) *PrivateKey

NewPrivateKey new a private key with ecdsa.PrivateKey

func NewPrivateKeyFromHex

func NewPrivateKeyFromHex(b string) (*PrivateKey, error)

NewPrivateKeyFromHex gets new private key from hex string.

func (*PrivateKey) Algorithm

func (k *PrivateKey) Algorithm() algorithm.CryptoAlgorithm

Algorithm returns algorithm name.

func (*PrivateKey) Clear

func (k *PrivateKey) Clear()

Clear clear key content.

func (*PrivateKey) Decode

func (k *PrivateKey) Decode(b []byte) error

Decode decode data to key.

func (*PrivateKey) Encoded

func (k *PrivateKey) Encoded() ([]byte, error)

Encoded encodes to bytes.

func (*PrivateKey) PublicKey

func (k *PrivateKey) PublicKey() signature.PublicKey

PublicKey returns public key.

func (*PrivateKey) Sign

func (k *PrivateKey) Sign(msg []byte) ([]byte, error)

Sign sing bytes with private key.

type PublicKey

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

PublicKey ecdsa publickey.

func NewPublicKey

func NewPublicKey(pub ecdsa.PublicKey) *PublicKey

NewPublicKey generate PublicKey.

func (*PublicKey) Algorithm

func (k *PublicKey) Algorithm() algorithm.CryptoAlgorithm

Algorithm algorithm name.

func (*PublicKey) Clear

func (k *PublicKey) Clear()

Clear clear key content.

func (*PublicKey) Compressed

func (k *PublicKey) Compressed() ([]byte, error)

Compressed encodes a public key to 33-byte compressed format.

func (*PublicKey) Decode

func (k *PublicKey) Decode(b []byte) error

Decode decode data to key.

func (*PublicKey) Decompress

func (k *PublicKey) Decompress(data []byte) error

Decompress parses a public key in the 33-byte compressed format.

func (*PublicKey) Encoded

func (k *PublicKey) Encoded() ([]byte, error)

Encoded encoded to byte.

func (*PublicKey) Verify

func (k *PublicKey) Verify(msg []byte, sig []byte) (bool, error)

Verify verify ecdsa publickey.

type Signature

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

Signature signature ecdsa

func (*Signature) Algorithm

func (s *Signature) Algorithm() algorithm.CryptoAlgorithm

Algorithm secp256k1 algorithm

func (*Signature) InitSign

func (s *Signature) InitSign(priv signature.PrivateKey)

InitSign ecdsa init sign

func (*Signature) InitVerify

func (s *Signature) InitVerify(pub signature.PublicKey)

InitVerify ecdsa verify init

func (*Signature) RecoverPublic

func (s *Signature) RecoverPublic(data []byte, sig []byte) (signature.PublicKey, error)

RecoverPublic returns a public key

func (*Signature) Sign

func (s *Signature) Sign(data []byte) ([]byte, error)

Sign ecdsa sign

func (*Signature) Verify

func (s *Signature) Verify(data []byte, sig []byte) (bool, error)

Verify ecdsa verify

Jump to

Keyboard shortcuts

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