Categorygithub.com/decred/dcrd/dcrec/secp256k1/v4
modulepackage
4.4.0
Repository: https://github.com/decred/dcrd.git
Documentation: pkg.go.dev

# README

secp256k1

Build Status ISC License Doc

Package secp256k1 implements optimized secp256k1 elliptic curve operations.

This package provides an optimized pure Go implementation of elliptic curve cryptography operations over the secp256k1 curve as well as data structures and functions for working with public and private secp256k1 keys. See https://www.secg.org/sec2-v2.pdf for details on the standard.

In addition, sub packages are provided to produce, verify, parse, and serialize ECDSA signatures and EC-Schnorr-DCRv0 (a custom Schnorr-based signature scheme specific to Decred) signatures. See the README.md files in the relevant sub packages for more details about those aspects.

An overview of the features provided by this package are as follows:

  • Private key generation, serialization, and parsing
  • Public key generation, serialization and parsing per ANSI X9.62-1998
    • Parses uncompressed, compressed, and hybrid public keys
    • Serializes uncompressed and compressed public keys
  • Specialized types for performing optimized and constant time field operations
    • FieldVal type for working modulo the secp256k1 field prime
    • ModNScalar type for working modulo the secp256k1 group order
  • Elliptic curve operations in Jacobian projective coordinates
    • Point addition
    • Point doubling
    • Scalar multiplication with an arbitrary point
    • Scalar multiplication with the base point (group generator)
  • Point decompression from a given x coordinate
  • Nonce generation via RFC6979 with support for extra data and version information that can be used to prevent nonce reuse between signing algorithms

It also provides an implementation of the Go standard library crypto/elliptic Curve interface via the S256 function so that it may be used with other packages in the standard library such as crypto/tls, crypto/x509, and crypto/ecdsa. However, in the case of ECDSA, it is highly recommended to use the ecdsa sub package of this package instead since it is optimized specifically for secp256k1 and is significantly faster as a result.

Although this package was primarily written for dcrd, it has intentionally been designed so it can be used as a standalone package for any projects needing to use optimized secp256k1 elliptic curve cryptography.

Finally, a comprehensive suite of tests is provided to provide a high level of quality assurance.

secp256k1 use in Decred

At the time of this writing, the primary public key cryptography in widespread use on the Decred network used to secure coins is based on elliptic curves defined by the secp256k1 domain parameters.

Installation and Updating

This package is part of the github.com/decred/dcrd/dcrec/secp256k1/v4 module. Use the standard go tooling for working with modules to incorporate it.

Examples

  • Encryption Demonstrates encrypting and decrypting a message using a shared key derived through ECDHE.

License

Package secp256k1 is licensed under the copyfree ISC License.

# Packages

Package ecdsa provides secp256k1-optimized ECDSA signing and verification.
Package schnorr provides custom Schnorr signing and verification via secp256k1.

# Functions

AddNonConst adds the passed Jacobian points together and stores the result in the provided result param in *non-constant* time.
DecompressY attempts to calculate the Y coordinate for the given X coordinate such that the result pair is a point on the secp256k1 curve.
DoubleNonConst doubles the passed Jacobian point and stores the result in the provided result parameter in *non-constant* time.
GeneratePrivateKey generates and returns a new cryptographically secure private key that is suitable for use with secp256k1.
GeneratePrivateKeyFromRand generates a private key that is suitable for use with secp256k1 using the provided reader as a source of entropy.
GenerateSharedSecret generates a shared secret based on a private key and a public key using Diffie-Hellman key exchange (ECDH) (RFC 5903).
MakeJacobianPoint returns a Jacobian point with the provided X, Y, and Z coordinates.
NewPrivateKey instantiates a new private key from a scalar encoded as a big integer.
NewPublicKey instantiates a new public key with the given x and y coordinates.
NonceRFC6979 generates a nonce deterministically according to RFC 6979 using HMAC-SHA256 for the hashing function.
Params returns the secp256k1 curve parameters for convenience.
ParsePubKey parses a secp256k1 public key encoded according to the format specified by ANSI X9.62-1998, which means it is also compatible with the SEC (Standards for Efficient Cryptography) specification which is a subset of the former.
PrivKeyFromBytes returns a private based on the provided byte slice which is interpreted as an unsigned 256-bit big-endian integer in the range [0, N-1], where N is the order of the curve.
S256 returns an elliptic.Curve which implements secp256k1.
ScalarBaseMultNonConst multiplies k*G where k is a scalar modulo the curve order and G is the base point of the group and stores the result in the provided Jacobian point.
ScalarMultNonConst multiplies k*P where k is a scalar modulo the curve order and P is a point in Jacobian projective coordinates and stores the result in the provided Jacobian point.

# Constants

ErrPubKeyInvalidFormat indicates an attempt was made to parse a public key that does not specify one of the supported formats.
ErrPubKeyInvalidLen indicates that the length of a serialized public key is not one of the allowed lengths.
ErrPubKeyMismatchedOddness indicates that a hybrid public key specified an oddness of the y coordinate that does not match the actual oddness of the provided y coordinate.
ErrPubKeyNotOnCurve indicates that a public key is not a point on the secp256k1 curve.
ErrPubKeyXTooBig indicates that the x coordinate for a public key is greater than or equal to the prime of the field underlying the group.
ErrPubKeyYTooBig indicates that the y coordinate for a public key is greater than or equal to the prime of the field underlying the group.
PrivKeyBytesLen defines the length in bytes of a serialized private key.
PubKeyBytesLenCompressed is the number of bytes of a serialized compressed public key.
PubKeyBytesLenUncompressed is the number of bytes of a serialized uncompressed public key.
PubKeyFormatCompressedEven is the identifier prefix byte for a public key whose Y coordinate is even when serialized in the compressed format per section 2.3.4 of [SEC1](https://secg.org/sec1-v2.pdf#subsubsection.2.3.4).
PubKeyFormatCompressedOdd is the identifier prefix byte for a public key whose Y coordinate is odd when serialized in the compressed format per section 2.3.4 of [SEC1](https://secg.org/sec1-v2.pdf#subsubsection.2.3.4).
PubKeyFormatHybridEven is the identifier prefix byte for a public key whose Y coordinate is even when serialized according to the hybrid format per section 4.3.6 of [ANSI X9.62-1998].
PubKeyFormatHybridOdd is the identifier prefix byte for a public key whose Y coordingate is odd when serialized according to the hybrid format per section 4.3.6 of [ANSI X9.62-1998].
PubKeyFormatUncompressed is the identifier prefix byte for a public key when serialized according in the uncompressed format per section 2.3.3 of [SEC1](https://secg.org/sec1-v2.pdf#subsubsection.2.3.3).

# Structs

CurveParams contains the parameters for the secp256k1 curve.
Error identifies an error related to public key cryptography using a sec256k1 curve.
FieldVal implements optimized fixed-precision arithmetic over the secp256k1 finite field.
JacobianPoint is an element of the group formed by the secp256k1 curve in Jacobian projective coordinates and thus represents a point on the curve.
KoblitzCurve provides an implementation for secp256k1 that fits the ECC Curve interface from crypto/elliptic.
ModNScalar implements optimized 256-bit constant-time fixed-precision arithmetic over the secp256k1 group order.
PrivateKey provides facilities for working with secp256k1 private keys within this package and includes functionality such as serializing and parsing them as well as computing their associated public key.
PublicKey provides facilities for efficiently working with secp256k1 public keys within this package and includes functions to serialize in both uncompressed and compressed SEC (Standards for Efficient Cryptography) formats.

# Type aliases

ErrorKind identifies a kind of error.