ecdsa

package
v0.0.0-...-6a7e83e Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2022 License: LGPL-3.0 Imports: 19 Imported by: 1

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

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

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

func DecompressPubkey

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

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

func Ecrecover

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

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

func FromECDSA

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

FromECDSA exports a private key into a binary dump.

func FromECDSAPub

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

func GenerateKey

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

GenerateKey generates a new private key.

func HexToECDSA

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

HexToECDSA parses a secp256k1 private key.

func Keccak256

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

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

func LoadECDSA

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

LoadECDSA loads a secp256k1 private key from the given file.

func New

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

New generate a ecdsa private key,input is algorithm type

func NewWithCryptoKey

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

func PaddedBigBytes

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

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

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

func S256

func S256() elliptic.Curve

S256 returns an instance of the secp256k1 curve.

func SaveECDSA

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

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

SigToPub returns the public key that created the given signature.

func Sign

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

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

ToECDSA creates a private key with the given D value.

func ToECDSAUnsafe

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

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

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

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

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

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

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

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

Jump to

Keyboard shortcuts

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