ecdsa

package
v1.28.0 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2023 License: LGPL-3.0 Imports: 19 Imported by: 13

Documentation

Index

Constants

View Source
const (
	//SignatureLength indicates the byte length required to carry a signature with recovery id.
	SignatureLength = 64 + 1 // 64 bytes ECDSA signature + 1 byte recovery id

	// RecoveryIDOffset points to the byte offset within the signature that contains the recovery id.
	RecoveryIDOffset = 64

	// DigestLength sets the signature digest exact length
	DigestLength = 32
)

Variables

View Source
var (
	Big1 = big.NewInt(1)
)

Functions

func CompressPubkey added in v1.0.1

func CompressPubkey(pubkey *ecdsa.PublicKey) []byte

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

func DecompressPubkey added in v1.0.1

func DecompressPubkey(pubkey []byte) (*ecdsa.PublicKey, error)

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

func Ecrecover added in v1.0.1

func Ecrecover(hash, sig []byte) ([]byte, error)

Ecrecover returns the uncompressed public key that created the given signature.

func FromECDSA added in v1.0.1

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

FromECDSA exports a private key into a binary dump.

func FromECDSAPub added in v1.0.1

func FromECDSAPub(pub *ecdsa.PublicKey) []byte

func GenerateKey

func GenerateKey() (*ecdsa.PrivateKey, error)

GenerateKey generates a new private key.

func HexToECDSA added in v1.0.1

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

HexToECDSA parses a secp256k1 private key.

func Keccak256 added in v1.0.1

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

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

func LoadECDSA added in v1.0.1

func LoadECDSA(file string) (*ecdsa.PrivateKey, error)

LoadECDSA loads a secp256k1 private key from the given file.

func New added in v1.0.1

func New(opt crypto.KeyType) (crypto.PrivateKey, error)

New generate a ecdsa private key,input is algorithm type

func NewWithCryptoKey added in v1.0.1

func NewWithCryptoKey(priv *ecdsa.PrivateKey) (crypto.PrivateKey, error)

func PaddedBigBytes added in v1.0.1

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

PaddedBigBytes encodes a big integer as a big-endian byte slice. The length of the slice is at least n bytes.

func ReadBits added in v1.0.1

func ReadBits(bigint *big.Int, buf []byte)

ReadBits encodes the absolute value of bigint as big-endian bytes. Callers must ensure that buf has enough space. If buf is too short the result will be incomplete.

func RecoverPlain added in v1.20.0

func RecoverPlain(hash []byte, R, S, Vb *big.Int, homestead bool) ([]byte, error)

func S256 added in v1.0.1

func S256() elliptic.Curve

S256 returns an instance of the secp256k1 curve.

func SaveECDSA added in v1.0.1

func SaveECDSA(file string, key *ecdsa.PrivateKey) error

SaveECDSA saves a secp256k1 private key to the given file with restrictive permissions. The key data is saved hex-encoded.

func SigToPub added in v1.0.1

func SigToPub(hash, sig []byte) (*ecdsa.PublicKey, error)

SigToPub returns the public key that created the given signature.

func Sign added in v1.0.1

func Sign(digestHash []byte, prv *ecdsa.PrivateKey) (sig []byte, err error)

Sign calculates an ECDSA signature.

This function is susceptible to chosen plaintext attacks that can leak information about the private key that is used for signing. Callers must be aware that the given digest cannot be chosen by an adversery. Common solution is to hash any input before calculating the signature.

The produced signature is in the [R || S || V] format where V is 0 or 1.

func ToECDSA added in v1.0.1

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

ToECDSA creates a private key with the given D value.

func ToECDSAUnsafe added in v1.0.1

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

ToECDSAUnsafe blindly converts a binary blob to a private key. It should almost never be used unless you are sure the input is valid and want to avoid hitting errors due to bad origin encoding (0 prefixes cut off).

func Unmarshal

func Unmarshal(data []byte) (crypto.PrivateKey, error)

func UnmarshalPubkey added in v1.0.1

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

UnmarshalPubkey converts bytes to a secp256k1 public key.

func UnmarshalPublicKey

func UnmarshalPublicKey(data []byte, opt crypto.KeyType) (crypto.PublicKey, error)

func ValidateSignatureValues added in v1.20.0

func ValidateSignatureValues(v byte, r, s *big.Int, homestead bool) bool

ValidateSignatureValues verifies whether the signature values are valid with the given chain rules. The v value is assumed to be either 0 or 1.

func VerifySignature added in v1.0.1

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

VerifySignature checks that the given public key created signature over digest. The public key should be in compressed (33 bytes) or uncompressed (65 bytes) format. The signature should have the 64 byte [R || S] format.

Types

type KeccakState added in v1.0.1

type KeccakState interface {
	hash.Hash
	Read([]byte) (int, error)
}

KeccakState wraps sha3.state. In addition to the usual hash methods, it also supports Read to get a variable amount of data from the hash state. Read is faster than Sum because it doesn't copy the internal state, but also modifies the internal state.

type PrivateKey

type PrivateKey struct {
	K *ecdsa.PrivateKey
	// contains filtered or unexported fields
}

PrivateKey ECDSA private key. never new(PrivateKey), use NewPrivateKey()

func UnmarshalPrivateKey

func UnmarshalPrivateKey(data []byte, opt crypto.KeyType) (*PrivateKey, error)

func (*PrivateKey) Bytes

func (priv *PrivateKey) Bytes() ([]byte, error)

Bytes returns a serialized, storable representation of this key

func (*PrivateKey) PublicKey

func (priv *PrivateKey) PublicKey() crypto.PublicKey

func (*PrivateKey) Sign

func (priv *PrivateKey) Sign(digest []byte) ([]byte, error)

func (*PrivateKey) Type added in v1.0.1

func (priv *PrivateKey) Type() crypto.KeyType

type PublicKey

type PublicKey struct {
	K *ecdsa.PublicKey
}

PublicKey ECDSA public key. never new(PublicKey), use NewPublicKey()

func NewPublicKey added in v1.0.1

func NewPublicKey(k ecdsa.PublicKey) (*PublicKey, error)

func (*PublicKey) Address

func (pub *PublicKey) Address() (*types.Address, error)

func (*PublicKey) Bytes

func (pub *PublicKey) Bytes() ([]byte, error)

func (*PublicKey) Type added in v1.0.1

func (pub *PublicKey) Type() crypto.KeyType

func (*PublicKey) Verify

func (pub *PublicKey) Verify(digest []byte, sig []byte) (bool, error)

type Sig

type Sig struct {
	Pub []byte   `json:"pub"`
	R   *big.Int `json:"r"`
	S   *big.Int `json:"s"`
}

ECDSASig holds the r and s values of an ECDSA signature

Directories

Path Synopsis
Package secp256k1 wraps the bitcoin secp256k1 C library.
Package secp256k1 wraps the bitcoin secp256k1 C library.

Jump to

Keyboard shortcuts

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