secp256k1

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2025 License: BSD-3-Clause, BSD-3-Clause Imports: 15 Imported by: 59

Documentation

Overview

Package secp256k1 wraps the bitcoin secp256k1 C library.

Index

Constants

View Source
const (
	// SignatureLen is the number of bytes in a secp256k1 recoverable signature
	SignatureLen = 65

	// PrivateKeyLen is the number of bytes in a secp256k1 private key
	PrivateKeyLen = 32

	// PublicKeyLen is the number of bytes in a secp256k1 public key
	PublicKeyLen = 33

	PrivateKeyPrefix = "PrivateKey-"
)

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")
)
View Source
var (
	ErrInvalidSig = errors.New("invalid signature")
)
View Source
var RecoverCache = cache.NewLRU[string, *PublicKey](2048)

RecoverCache is a cache for recovered public keys

Functions

func CompressPubkey

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

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

func DecompressPubkey

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

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

func Keccak256 added in v1.1.2

func Keccak256(data ...[]byte) []byte

Keccak256 calculates and returns the Keccak256 hash of the input data.

func PaddedBigBytes added in v1.1.2

func PaddedBigBytes(bigint *big.Int, n int) []byte

PaddedBigBytes encodes a big integer as a big-endian byte slice. The byte slice is padded with zeros.

func PubkeyBytesToAddress added in v1.1.2

func PubkeyBytesToAddress(pubkey []byte) []byte

PubkeyBytesToAddress converts public key bytes to an address using SHA256 + RIPEMD160

func PubkeyToAddress added in v1.1.2

func PubkeyToAddress(p ecdsa.PublicKey) common.Address

PubkeyToAddress returns the Ethereum address for the given public key

func RecoverPubkey

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

RecoverPubkey returns 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 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 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

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)

func (*BitCurve) Unmarshal

func (bitCurve *BitCurve) Unmarshal(data []byte) (x, y *big.Int)

Unmarshal converts a point, serialised by Marshal, into an x, y pair. On error, x = nil.

type PrivateKey added in v1.1.2

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

PrivateKey wraps an ecdsa.PrivateKey

func NewPrivateKey added in v1.1.2

func NewPrivateKey() (*PrivateKey, error)

NewPrivateKey generates a new private key

func TestKeys added in v1.1.2

func TestKeys() []*PrivateKey

TestKeys returns a set of test keys for testing purposes

func ToPrivateKey added in v1.1.2

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

ToPrivateKey converts bytes to a private key

func (*PrivateKey) Address added in v1.1.2

func (k *PrivateKey) Address() ids.ShortID

Address returns the address of the private key (via its public key)

func (*PrivateKey) Bytes added in v1.1.2

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

Bytes returns the private key bytes

func (*PrivateKey) MarshalText added in v1.1.2

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

MarshalText implements encoding.TextMarshaler

func (*PrivateKey) PublicKey added in v1.1.2

func (k *PrivateKey) PublicKey() *PublicKey

PublicKey returns the public key

func (*PrivateKey) Sign added in v1.1.2

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

Sign signs a message with the private key

func (*PrivateKey) SignArray added in v1.1.2

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

SignArray signs a message and returns a fixed-size array

func (*PrivateKey) SignHash added in v1.1.2

func (k *PrivateKey) SignHash(hash []byte) ([]byte, error)

SignHash signs a hash with the private key

func (*PrivateKey) SignHashArray added in v1.1.2

func (k *PrivateKey) SignHashArray(hash []byte) ([SignatureLen]byte, error)

SignHashArray signs a hash and returns a fixed-size array

func (*PrivateKey) String added in v1.1.2

func (k *PrivateKey) String() string

String returns the string representation of the private key

func (*PrivateKey) UnmarshalText added in v1.1.2

func (k *PrivateKey) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler

type PublicKey added in v1.1.2

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

PublicKey wraps an ecdsa.PublicKey

func RecoverPublicKey added in v1.1.2

func RecoverPublicKey(msg, sig []byte) (*PublicKey, error)

RecoverPublicKey recovers the public key from a message and signature

func RecoverPublicKeyFromHash added in v1.1.2

func RecoverPublicKeyFromHash(hash, sig []byte) (*PublicKey, error)

RecoverPublicKeyFromHash recovers the public key from a hash and signature

func ToPublicKey added in v1.1.2

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

ToPublicKey converts bytes to a public key

func (*PublicKey) Address added in v1.1.2

func (k *PublicKey) Address() ids.ShortID

Address returns the address of the public key as an ids.ShortID

func (*PublicKey) Bytes added in v1.1.2

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

Bytes returns the public key bytes

func (*PublicKey) CompressedBytes added in v1.1.2

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

CompressedBytes returns the compressed public key bytes (33 bytes)

func (*PublicKey) String added in v1.1.2

func (k *PublicKey) String() string

String returns the string representation of the public key

func (*PublicKey) ToECDSA added in v1.1.2

func (k *PublicKey) ToECDSA() *ecdsa.PublicKey

ToECDSA returns the underlying ECDSA public key

func (*PublicKey) Verify added in v1.1.2

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

Verify verifies a signature against a message

func (*PublicKey) VerifyHash added in v1.1.2

func (k *PublicKey) VerifyHash(hash, sig []byte) bool

VerifyHash verifies a signature against a hash

type RecoverCacheType added in v1.1.2

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

RecoverCacheType provides a cache for public key recovery with methods

func NewRecoverCache added in v1.1.2

func NewRecoverCache(size int) RecoverCacheType

NewRecoverCache creates a new recover cache

func (RecoverCacheType) RecoverPublicKey added in v1.1.2

func (r RecoverCacheType) RecoverPublicKey(msg, sig []byte) (*PublicKey, error)

RecoverPublicKey recovers a public key from a message and signature

func (RecoverCacheType) RecoverPublicKeyFromHash added in v1.1.2

func (r RecoverCacheType) RecoverPublicKeyFromHash(hash, sig []byte) (*PublicKey, error)

RecoverPublicKeyFromHash recovers a public key from a hash and signature

Jump to

Keyboard shortcuts

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