ecc

package module
v0.0.0-...-107dfd8 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2022 License: MIT Imports: 11 Imported by: 4

Documentation

Overview

Package ecc provides an implementation of elliptic curves of short Weierstass form over finite fields, used for cryptography.

Index

Constants

This section is empty.

Variables

Curve is the secp256k1 curve modeled as an elliptic.Curve interface.

View Source
var ErrPointNotOnCurve = errors.New("failed to deserialize point not on secp256k1 curve")

ErrPointNotOnCurve is returned upon deserializing an invalid point (one which does not satisfy the secp256k1 curve equation).

Functions

func CompressPublicKey

func CompressPublicKey(publicKey []byte) ([]byte, error)

CompressPublicKey takes a given public key of any format, deserializes it, and re-encodes it in compressed format. Returns ErrPointNotOnCurve if the key is invalid.

func DeserializePoint

func DeserializePoint(serialized []byte) (x, y *big.Int, err error)

DeserializePoint decodes the given serialized curve point, which should either be length 65 (uncompressed), 33 (compressed), or 32 (BIP-340 schnorr). Returns ErrPointNotOnCurve if the resulting point is not on the secp256k1 curve.

func GetPublicKey

func GetPublicKey(privateKey []byte, compressed bool) []byte

GetPublicKey returns the encoded public key for the given private key, with a boolean parameter to decide whether the output public key will be compressed or not.

func GetPublicKeyCompressed

func GetPublicKeyCompressed(privateKey []byte) []byte

GetPublicKeyCompressed returns the 33-byte compressed public key of a given private key.

func GetPublicKeySchnorr

func GetPublicKeySchnorr(privateKey []byte) []byte

GetPublicKeySchnorr returns the 32-byte encoded x coordinate of the public key belonging to the given private key.

func GetPublicKeyUncompressed

func GetPublicKeyUncompressed(privateKey []byte) []byte

GetPublicKeyUncompressed returns the 65-byte uncompressed public key of a given private key.

func IsCompressedPublicKey

func IsCompressedPublicKey(key []byte) bool

IsCompressedPublicKey returns true if the given byte slice appears to be a 33-byte compressed public key. It does not check whether the key encodes a valid secp256k1 point.

func NewPrivateKey

func NewPrivateKey(random io.Reader) ([]byte, error)

NewPrivateKey generates a private key by reading data from a random source. This source should come from a secure high-entropy RNG like crypto/rand.Reader.

func SerializePoint

func SerializePoint(x, y *big.Int, compressed bool) []byte

SerializePoint serializes the given curve point in compressed format if compressed is true, otherwise it returns the uncompressed serialization.

func SerializePointCompressed

func SerializePointCompressed(x, y *big.Int) []byte

SerializePointCompressed serializes the given curve point in 33-byte compressed format.

func SerializePointUncompressed

func SerializePointUncompressed(x, y *big.Int) []byte

SerializePointUncompressed serializes the given curve point in 65-byte uncompressed format.

func SharedSecret

func SharedSecret(priv, pubX, pubY *big.Int) []byte

SharedSecret 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 SignECDSA

func SignECDSA(privateKey, messageHash []byte) (r, s *big.Int)

SignECDSA signs the given hash with the given private key d and returns the two components of the signature: r and s.

SignECDSA calculates the secret signature nonce value k deterministically using RFC6979.

func SignSchnorr

func SignSchnorr(privateKey, messageHash, auxRand []byte) []byte

SignSchnorr signs a given 32-byte messageHash with the given private key, using auxRand as the seed to derive a nonce value.

func SumPrivateKeys

func SumPrivateKeys(privateKeys ...[]byte) ([]byte, error)

SumPrivateKeys combines any number of private keys together into one key which can form an aggregate signature.

Returns an error if any of the keys are not valid secp256k1 scalar values.

func SumPublicKeys

func SumPublicKeys(publicKeys ...[]byte) ([]byte, error)

SumPublicKeys combines any number of schnorr public keys together into one key which can be used to verify signatures aggregated from the component keys.

Returns an error if any of the keys are not schnorr public keys.

func UncompressPublicKey

func UncompressPublicKey(publicKey []byte) ([]byte, error)

UncompressPublicKey takes a given public key of any format, deserializes it, and re-encodes it in uncompressed format. Returns ErrPointNotOnCurve if the key is invalid.

func VerifyECDSA

func VerifyECDSA(pubBytes, messageHash []byte, r, s *big.Int) bool

VerifyECDSA calculates if the given signature (r, s) is a valid ECDSA signature on messageHash from the given public key. Note that non-canonical ECDSA signatures (where s > N/2) are acceptable.

func VerifySchnorr

func VerifySchnorr(pubBytes, messageHash, sig []byte) bool

VerifySchnorr returns true if the given signature was made by the owner of the given public key on the given message hash.

Types

This section is empty.

Jump to

Keyboard shortcuts

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