asymmetric

package
v0.0.0-...-020e20f Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2022 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package asymmetric implements Asymmetric Encryption methods ported from btcd, Ethereum-go etc.

Package btcec implements support for the elliptic curves needed for bitcoin.

Bitcoin uses elliptic curve cryptography using koblitz curves (specifically secp256k1) for cryptographic functions. See http://www.secg.org/collateral/sec2_final.pdf for details on the standard.

This package provides the data structures and functions implementing the crypto/elliptic Curve interface in order to permit using these curves with the standard crypto/ecdsa package provided with go. Helper functionality is provided to parse signatures and public keys from standard formats. It was designed for use with btcd, but should be general enough for other uses of elliptic curve crypto. It was originally based on some initial work by ThePiachu, but has significantly diverged since then.

Index

Constants

View Source
const PrivateKeyBytesLen = ec.PrivKeyBytesLen

PrivateKeyBytesLen defines the length in bytes of a serialized private key.

View Source
const PublicKeyBytesLen = ec.PubKeyBytesLenCompressed

PublicKeyBytesLen defines the length in bytes of a serialized public key.

View Source
const PublicKeyFormatHeader byte = 0x2

PublicKeyFormatHeader is the default header of PublicKey.Serialize()

see: github.com/btcsuite/btcd/btcec/pubkey.go#L63

Variables

View Source
var (
	// BypassSignature is the flag indicate if bypassing signature sign & verify
	BypassSignature = false
)

Functions

func GenECDHSharedSecret

func GenECDHSharedSecret(privateKey *PrivateKey, publicKey *PublicKey) []byte

GenECDHSharedSecret is just a wrapper of ec.GenerateSharedSecret which generates a shared secret based on a private key and a public key using Diffie-Hellman key exchange (ECDH) (RFC 4753). RFC5903 Section 9 states we should only return x. Key Feature:

GenECDHSharedSecret(BPub, APriv) == GenECDHSharedSecret(APub, BPriv).

func GenSecp256k1KeyPair

func GenSecp256k1KeyPair() (
	privateKey *PrivateKey,
	publicKey *PublicKey,
	err error)

GenSecp256k1KeyPair generate Secp256k1(used by Bitcoin) key pair.

func GetPubKeyNonce

func GetPubKeyNonce(
	publicKey *PublicKey,
	difficulty int,
	timeThreshold time.Duration,
	quit chan struct{}) (nonce mine.NonceInfo)

GetPubKeyNonce will make his best effort to find a difficult enough nonce.

func PrivKeyFromBytes

func PrivKeyFromBytes(pk []byte) (*PrivateKey, *PublicKey)

PrivKeyFromBytes returns a private and public key for `curve' based on the private key passed as an argument as a byte slice.

Types

type PrivateKey

type PrivateKey ec.PrivateKey

PrivateKey wraps an ec.PrivateKey as a convenience mainly for signing things with the the private key without having to directly import the ecdsa package.

func (*PrivateKey) PubKey

func (private *PrivateKey) PubKey() *PublicKey

PubKey return the public key.

func (*PrivateKey) Serialize

func (private *PrivateKey) Serialize() []byte

Serialize returns the private key number d as a big-endian binary-encoded number, padded to a length of 32 bytes.

func (*PrivateKey) Sign

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

Sign generates an ECDSA signature for the provided hash (which should be the result of hashing a larger message) using the private key. Produced signature is deterministic (same message and same key yield the same signature) and canonical in accordance with RFC6979 and BIP0062.

type PublicKey

type PublicKey ec.PublicKey

PublicKey wraps an ec.PublicKey as a convenience mainly verifying signatures with the the public key without having to directly import the ecdsa package.

func ParsePubKey

func ParsePubKey(pubKeyStr []byte) (*PublicKey, error)

ParsePubKey recovers the public key from pubKeyStr.

func (*PublicKey) IsEqual

func (k *PublicKey) IsEqual(public *PublicKey) bool

IsEqual return true if two keys are equal.

func (*PublicKey) MarshalBinary

func (k *PublicKey) MarshalBinary() (keyBytes []byte, err error)

MarshalBinary does the serialization.

func (*PublicKey) MarshalHash

func (k *PublicKey) MarshalHash() (keyBytes []byte, err error)

MarshalHash marshals for hash.

func (PublicKey) MarshalYAML

func (k PublicKey) MarshalYAML() (interface{}, error)

MarshalYAML implements the yaml.Marshaler interface.

func (PublicKey) Msgsize

func (k PublicKey) Msgsize() (s int)

Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message.

func (*PublicKey) Serialize

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

Serialize is a function that converts a public key to uncompressed byte array

TODO(leventeliu): use SerializeUncompressed, which is about 40 times faster than SerializeCompressed.

BenchmarkParsePublicKey-12 50000 39819 ns/op 2401 B/op 35 allocs/op BenchmarkParsePublicKey-12 1000000 1039 ns/op 384 B/op 9 allocs/op.

func (*PublicKey) UnmarshalBinary

func (k *PublicKey) UnmarshalBinary(keyBytes []byte) (err error)

UnmarshalBinary does the deserialization.

func (*PublicKey) UnmarshalYAML

func (k *PublicKey) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML implements the yaml.Unmarshaler interface.

type Signature

type Signature struct {
	R *big.Int
	S *big.Int
}

Signature is a type representing an ecdsa signature.

func ParseDERSignature

func ParseDERSignature(sigStr []byte, curve elliptic.Curve) (*Signature, error)

ParseDERSignature recovers the signature from a sigStr.

func ParseSignature

func ParseSignature(sigStr []byte) (*Signature, error)

ParseSignature recovers the signature from a sigStr using koblitz curve.

func (*Signature) IsEqual

func (s *Signature) IsEqual(signature *Signature) bool

IsEqual return true if two signature is equal.

func (*Signature) MarshalBinary

func (s *Signature) MarshalBinary() (keyBytes []byte, err error)

MarshalBinary does the serialization.

func (*Signature) MarshalHash

func (s *Signature) MarshalHash() (keyBytes []byte, err error)

MarshalHash marshals for hash.

func (Signature) Msgsize

func (s Signature) Msgsize() (sz int)

Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message.

func (*Signature) Serialize

func (s *Signature) Serialize() []byte

Serialize converts a signature to stirng.

func (*Signature) UnmarshalBinary

func (s *Signature) UnmarshalBinary(keyBytes []byte) (err error)

UnmarshalBinary does the deserialization.

func (*Signature) Verify

func (s *Signature) Verify(hash []byte, signee *PublicKey) bool

Verify calls ecdsa.Verify to verify the signature of hash using the public key. It returns true if the signature is valid, false otherwise.

Jump to

Keyboard shortcuts

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