Documentation ¶
Overview ¶
Package secp256k1 implements support for the elliptic curves needed for Decred.
Decred uses elliptic curve cryptography using koblitz curves (specifically secp256k1) for cryptographic functions. See http://www.secg.org/sec2-v2.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 dcrd, 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 Decrypt(priv *PrivateKey, in []byte) ([]byte, error)
- func Encrypt(pubkey *PublicKey, in []byte) ([]byte, error)
- func GenerateKey(rand io.Reader) (priv []byte, x, y *big.Int, err error)
- func GenerateSharedSecret(privkey *PrivateKey, pubkey *PublicKey) []byte
- func NAF(k []byte) ([]byte, []byte)
- func NonceRFC6979(privkey *big.Int, hash []byte, extra []byte, version []byte) *big.Int
- func PrivKeyFromBytes(pk []byte) (*PrivateKey, *PublicKey)
- func PrivKeyFromScalar(s []byte) (*PrivateKey, *PublicKey)
- func SignCompact(key *PrivateKey, hash []byte, isCompressedKey bool) ([]byte, error)
- type KoblitzCurve
- func (curve *KoblitzCurve) Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int)
- func (curve *KoblitzCurve) Double(x1, y1 *big.Int) (*big.Int, *big.Int)
- func (curve *KoblitzCurve) IsOnCurve(x, y *big.Int) bool
- func (curve *KoblitzCurve) Params() *elliptic.CurveParams
- func (curve *KoblitzCurve) QPlus1Div4() *big.Int
- func (curve *KoblitzCurve) ScalarBaseMult(k []byte) (*big.Int, *big.Int)
- func (curve *KoblitzCurve) ScalarMult(Bx, By *big.Int, k []byte) (*big.Int, *big.Int)
- type PrivateKey
- func (p PrivateKey) GetD() *big.Int
- func (p PrivateKey) GetType() int
- func (p *PrivateKey) PubKey() *PublicKey
- func (p PrivateKey) Public() (*big.Int, *big.Int)
- func (p PrivateKey) Serialize() []byte
- func (p PrivateKey) SerializeSecret() []byte
- func (p *PrivateKey) Sign(hash []byte) (*Signature, error)
- func (p *PrivateKey) ToECDSA() *ecdsa.PrivateKey
- type PublicKey
- func (p PublicKey) GetCurve() interface{}
- func (p PublicKey) GetType() int
- func (p PublicKey) GetX() *big.Int
- func (p PublicKey) GetY() *big.Int
- func (p *PublicKey) IsEqual(otherPubKey *PublicKey) bool
- func (p PublicKey) Serialize() []byte
- func (p PublicKey) SerializeCompressed() []byte
- func (p PublicKey) SerializeUncompressed() []byte
- func (p PublicKey) ToECDSA() *ecdsa.PublicKey
- type Signature
Constants ¶
const ( PubKeyBytesLenCompressed = 33 PubKeyBytesLenUncompressed = 65 )
These constants define the lengths of serialized public keys.
const PrivKeyBytesLen = 32
PrivKeyBytesLen defines the length in bytes of a serialized private key.
Variables ¶
var ( // ErrInvalidMAC occurs when Message Authentication Check (MAC) fails // during decryption. This happens because of either invalid private key or // corrupt ciphertext. ErrInvalidMAC = errors.New("invalid mac hash") )
Functions ¶
func Decrypt ¶
func Decrypt(priv *PrivateKey, in []byte) ([]byte, error)
Decrypt decrypts data that was encrypted using the Encrypt function.
func Encrypt ¶
Encrypt encrypts data for the target public key using AES-256-CBC. It also generates a private key (the pubkey of which is also in the output). The only supported curve is secp256k1. The `structure' that it encodes everything into is:
struct { // Initialization Vector used for AES-256-CBC IV [16]byte // Public Key: curve(2) + len_of_pubkeyX(2) + pubkeyX + // len_of_pubkeyY(2) + pubkeyY (curve = 714) PublicKey [70]byte // Cipher text Data []byte // HMAC-SHA-256 Message Authentication Code HMAC [32]byte }
The primary aim is to ensure byte compatibility with Pyelliptic. Also, refer to section 5.8.1 of ANSI X9.63 for rationale on this format.
func GenerateKey ¶
GenerateKey generates a key using a random number generator, returning the private scalar and the corresponding public key points.
func GenerateSharedSecret ¶
func GenerateSharedSecret(privkey *PrivateKey, pubkey *PublicKey) []byte
GenerateSharedSecret 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.
func NAF ¶
NAF takes a positive integer k and returns the Non-Adjacent Form (NAF) as two byte slices. The first is where 1s will be. The second is where -1s will be. NAF is convenient in that on average, only 1/3rd of its values are non-zero. This is algorithm 3.30 from [GECC].
Essentially, this makes it possible to minimize the number of operations since the resulting ints returned will be at least 50% 0s.
func NonceRFC6979 ¶
NonceRFC6979 generates an ECDSA nonce (`k`) deterministically according to RFC 6979. It takes a 32-byte hash as an input and returns 32-byte nonce to be used in ECDSA algorithm.
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.
func PrivKeyFromScalar ¶
func PrivKeyFromScalar(s []byte) (*PrivateKey, *PublicKey)
PrivKeyFromScalar is the same as PrivKeyFromBytes in secp256k1.
func SignCompact ¶
func SignCompact(key *PrivateKey, hash []byte, isCompressedKey bool) ([]byte, error)
SignCompact produces a compact signature of the data in hash with the given private key on the given koblitz curve. The isCompressed parameter should be used to detail if the given signature should reference a compressed public key or not. If successful the bytes of the compact signature will be returned in the format: <(byte of 27+public key solution)+4 if compressed >< padded bytes for signature R><padded bytes for signature S> where the R and S parameters are padde up to the bitlengh of the curve.
Types ¶
type KoblitzCurve ¶
type KoblitzCurve struct { *elliptic.CurveParams H int // cofactor of the curve. // contains filtered or unexported fields }
KoblitzCurve supports a koblitz curve implementation that fits the ECC Curve interface from crypto/elliptic.
func (*KoblitzCurve) Add ¶
Add returns the sum of (x1,y1) and (x2,y2). Part of the elliptic.Curve interface.
func (*KoblitzCurve) IsOnCurve ¶
func (curve *KoblitzCurve) IsOnCurve(x, y *big.Int) bool
IsOnCurve returns boolean if the point (x,y) is on the curve. Part of the elliptic.Curve interface. This function differs from the crypto/elliptic algorithm since a = 0 not -3.
func (*KoblitzCurve) Params ¶
func (curve *KoblitzCurve) Params() *elliptic.CurveParams
Params returns the parameters for the curve.
func (*KoblitzCurve) QPlus1Div4 ¶
func (curve *KoblitzCurve) QPlus1Div4() *big.Int
QPlus1Div4 returns the Q+1/4 constant for the curve for use in calculating square roots via exponention.
func (*KoblitzCurve) ScalarBaseMult ¶
ScalarBaseMult returns k*G where G is the base point of the group and k is a big endian integer. Part of the elliptic.Curve interface.
func (*KoblitzCurve) ScalarMult ¶
ScalarMult returns k*(Bx, By) where k is a big endian integer. Part of the elliptic.Curve interface.
type PrivateKey ¶
type PrivateKey ecdsa.PrivateKey
PrivateKey wraps an ecdsa.PrivateKey as a convenience mainly for signing things with the the private key without having to directly import the ecdsa package.
func GeneratePrivateKey ¶
func GeneratePrivateKey() (*PrivateKey, error)
GeneratePrivateKey is a wrapper for ecdsa.GenerateKey that returns a PrivateKey instead of the normal ecdsa.PrivateKey.
func NewPrivateKey ¶
func NewPrivateKey(d *big.Int) *PrivateKey
NewPrivateKey instantiates a new private key from a scalar encoded as a big integer.
func (PrivateKey) GetD ¶
func (p PrivateKey) GetD() *big.Int
GetD satisfies the chainec PrivateKey interface.
func (PrivateKey) GetType ¶
func (p PrivateKey) GetType() int
GetType satisfies the chainec PrivateKey interface.
func (*PrivateKey) PubKey ¶
func (p *PrivateKey) PubKey() *PublicKey
PubKey returns the PublicKey corresponding to this private key.
func (PrivateKey) Public ¶
func (p PrivateKey) Public() (*big.Int, *big.Int)
Public returns the PublicKey corresponding to this private key.
func (PrivateKey) Serialize ¶
func (p 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) SerializeSecret ¶
func (p PrivateKey) SerializeSecret() []byte
SerializeSecret satisfies the chainec PrivateKey interface.
func (*PrivateKey) Sign ¶
func (p *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.
func (*PrivateKey) ToECDSA ¶
func (p *PrivateKey) ToECDSA() *ecdsa.PrivateKey
ToECDSA returns the private key as a *ecdsa.PrivateKey.
type PublicKey ¶
PublicKey is an ecdsa.PublicKey with additional functions to serialize in uncompressed and compressed formats.
func NewPublicKey ¶
NewPublicKey instantiates a new public key with the given X,Y coordinates.
func ParsePubKey ¶
ParsePubKey parses a public key for a koblitz curve from a bytestring into a ecdsa.Publickey, verifying that it is valid. It supports compressed and uncompressed signature formats, but not the hybrid format.
func RecoverCompact ¶
RecoverCompact verifies the compact signature "signature" of "hash" for the Koblitz curve in "curve". If the signature matches then the recovered public key will be returned as well as a boolen if the original key was compressed or not, else an error will be returned.
func (PublicKey) GetCurve ¶
func (p PublicKey) GetCurve() interface{}
GetCurve satisfies the chainec PublicKey interface.
func (*PublicKey) IsEqual ¶
IsEqual compares this PublicKey instance to the one passed, returning true if both PublicKeys are equivalent. A PublicKey is equivalent to another, if they both have the same X and Y coordinate.
func (PublicKey) Serialize ¶
Serialize serializes a public key in a 33-byte compressed format. It is the default serialization method.
func (PublicKey) SerializeCompressed ¶
SerializeCompressed serializes a public key in a 33-byte compressed format.
func (PublicKey) SerializeUncompressed ¶
SerializeUncompressed serializes a public key in a 65-byte uncompressed format.
type Signature ¶
Signature is a type representing an ecdsa signature.
func NewSignature ¶
NewSignature instantiates a new signature given some R,S values.
func ParseDERSignature ¶
ParseDERSignature parses a signature in DER format for the curve type `curve` into a Signature type. If parsing according to the less strict BER format is needed, use ParseSignature.
func ParseSignature ¶
ParseSignature parses a signature in BER format for the curve type `curve' into a Signature type, perfoming some basic sanity checks. If parsing according to the more strict DER format is needed, use ParseDERSignature.
func (*Signature) IsEqual ¶
IsEqual compares this Signature instance to the one passed, returning true if both Signatures are equivalent. A signature is equivalent to another, if they both have the same scalar value for R and S.
func (*Signature) Serialize ¶
Serialize returns the ECDSA signature in the more strict DER format. Note that the serialized bytes returned do not include the appended hash type used in Decred signature scripts.
encoding/asn1 is broken so we hand roll this output:
0x30 <length> 0x02 <length r> r 0x02 <length s> s