ecdsa

package
v2.3.3-0...-e7751c3 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2024 License: ISC Imports: 5 Imported by: 0

Documentation

Overview

Example (SignMessage)

This example demonstrates signing a message with a secp256k1 private key that is first parsed form raw bytes and serializing the generated signature.

// Decode a hex-encoded private key.
pkBytes, err := hex.DecodeString("22a47fa09a223f2aa079edf85a7c2d4f87" +
	"20ee63e502ee2869afab7de234b80c")
if err != nil {
	fmt.Println(err)
	return
}
privKey, pubKey := btcec.PrivKeyFromBytes(pkBytes)

// Sign a message using the private key.
message := "test message"
messageHash := chainhash.DoubleHashB([]byte(message))
signature := ecdsa.Sign(privKey, messageHash)

// Serialize and display the signature.
fmt.Printf("Serialized Signature: %x\n", signature.Serialize())

// Verify the signature for the message using the public key.
verified := signature.Verify(messageHash, pubKey)
fmt.Printf("Signature Verified? %v\n", verified)
Output:

Serialized Signature: 304402201008e236fa8cd0f25df4482dddbb622e8a8b26ef0ba731719458de3ccd93805b022032f8ebe514ba5f672466eba334639282616bb3c2f0ab09998037513d1f9e3d6d
Signature Verified? true
Example (VerifySignature)

This example demonstrates verifying a secp256k1 signature against a public key that is first parsed from raw bytes. The signature is also parsed from raw bytes.

// Decode hex-encoded serialized public key.
pubKeyBytes, err := hex.DecodeString("02a673638cb9587cb68ea08dbef685c" +
	"6f2d2a751a8b3c6f2a7e9a4999e6e4bfaf5")
if err != nil {
	fmt.Println(err)
	return
}
pubKey, err := btcec.ParsePubKey(pubKeyBytes)
if err != nil {
	fmt.Println(err)
	return
}

// Decode hex-encoded serialized signature.
sigBytes, err := hex.DecodeString("30450220090ebfb3690a0ff115bb1b38b" +
	"8b323a667b7653454f1bccb06d4bbdca42c2079022100ec95778b51e707" +
	"1cb1205f8bde9af6592fc978b0452dafe599481c46d6b2e479")

if err != nil {
	fmt.Println(err)
	return
}
signature, err := ecdsa.ParseSignature(sigBytes)
if err != nil {
	fmt.Println(err)
	return
}

// Verify the signature for the message using the public key.
message := "test message"
messageHash := chainhash.DoubleHashB([]byte(message))
verified := signature.Verify(messageHash, pubKey)
fmt.Println("Signature Verified?", verified)
Output:

Signature Verified? true

Index

Examples

Constants

View Source
const MinSigLen = 8

MinSigLen is the minimum length of a DER encoded signature and is when both R and S are 1 byte each. 0x30 + <1-byte> + 0x02 + 0x01 + <byte> + 0x2 + 0x01 + <byte>

Variables

This section is empty.

Functions

func RecoverCompact

func RecoverCompact(signature, hash []byte) (*btcec.PublicKey, bool, error)

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 boolean if the original key was compressed or not, else an error will be returned.

func SignCompact

func SignCompact(key *btcec.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 Error

type Error = secp_ecdsa.ErrorKind

Error identifies an error related to an ECDSA signature. It has full support for errors.Is and errors.As, so the caller can ascertain the specific reason for the error by checking the underlying error.

type ErrorKind

type ErrorKind = secp_ecdsa.ErrorKind

ErrorKind identifies a kind of error. It has full support for errors.Is and errors.As, so the caller can directly check against an error kind when determining the reason for an error.

type Signature

type Signature = secp_ecdsa.Signature

Signature is a type representing an ecdsa signature.

func NewSignature

func NewSignature(r, s *btcec.ModNScalar) *Signature

NewSignature instantiates a new signature given some r and s values.

func ParseDERSignature

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

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

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

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 Sign

func Sign(key *btcec.PrivateKey, hash []byte) *Signature

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

Jump to

Keyboard shortcuts

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