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
- Variables
- func GenECDHSharedSecret(privateKey *PrivateKey, publicKey *PublicKey) []byte
- func GenSecp256k1KeyPair() (privateKey *PrivateKey, publicKey *PublicKey, err error)
- func GetPubKeyNonce(publicKey *PublicKey, difficulty int, timeThreshold time.Duration, ...) (nonce mine.NonceInfo)
- func PrivKeyFromBytes(pk []byte) (*PrivateKey, *PublicKey)
- type PrivateKey
- type PublicKey
- func (k *PublicKey) IsEqual(public *PublicKey) bool
- func (k *PublicKey) MarshalBinary() (keyBytes []byte, err error)
- func (k *PublicKey) MarshalHash() (keyBytes []byte, err error)
- func (k PublicKey) MarshalYAML() (interface{}, error)
- func (k PublicKey) Msgsize() (s int)
- func (k *PublicKey) Serialize() []byte
- func (k *PublicKey) UnmarshalBinary(keyBytes []byte) (err error)
- func (k *PublicKey) UnmarshalYAML(unmarshal func(interface{}) error) error
- type Signature
- func (s *Signature) IsEqual(signature *Signature) bool
- func (s *Signature) MarshalBinary() (keyBytes []byte, err error)
- func (s *Signature) MarshalHash() (keyBytes []byte, err error)
- func (s Signature) Msgsize() (sz int)
- func (s *Signature) Serialize() []byte
- func (s *Signature) UnmarshalBinary(keyBytes []byte) (err error)
- func (s *Signature) Verify(hash []byte, signee *PublicKey) bool
Constants ¶
const PrivateKeyBytesLen = ec.PrivKeyBytesLen
PrivateKeyBytesLen defines the length in bytes of a serialized private key.
const PublicKeyBytesLen = ec.PubKeyBytesLenCompressed
PublicKeyBytesLen defines the length in bytes of a serialized public key.
const PublicKeyFormatHeader byte = 0x2
PublicKeyFormatHeader is the default header of PublicKey.Serialize()
see: github.com/btcsuite/btcd/btcec/pubkey.go#L63
Variables ¶
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 ¶
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 ¶
ParsePubKey recovers the public key from pubKeyStr.
func (*PublicKey) MarshalBinary ¶
MarshalBinary does the serialization.
func (*PublicKey) MarshalHash ¶
MarshalHash marshals for hash.
func (PublicKey) MarshalYAML ¶
MarshalYAML implements the yaml.Marshaler interface.
func (PublicKey) Msgsize ¶
Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message.
func (*PublicKey) Serialize ¶
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 ¶
UnmarshalBinary does the deserialization.
func (*PublicKey) UnmarshalYAML ¶
UnmarshalYAML implements the yaml.Unmarshaler interface.
type Signature ¶
Signature is a type representing an ecdsa signature.
func ParseDERSignature ¶
ParseDERSignature recovers the signature from a sigStr.
func ParseSignature ¶
ParseSignature recovers the signature from a sigStr using koblitz curve.
func (*Signature) MarshalBinary ¶
MarshalBinary does the serialization.
func (*Signature) MarshalHash ¶
MarshalHash marshals for hash.
func (Signature) Msgsize ¶
Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message.
func (*Signature) UnmarshalBinary ¶
UnmarshalBinary does the deserialization.