API.md raw

API Documentation - p8k.mleku.dev

Complete API reference for the libsecp256k1 Go bindings.

Table of Contents

  1. Context Management
  2. Public Key Operations
  3. ECDSA Signatures
  4. Schnorr Signatures
  5. ECDH
  6. Recovery
  7. Utility Functions
  8. Constants
  9. Types

Context Management

NewContext

Creates a new secp256k1 context.

func NewContext(flags uint32) (c *Context, err error)

Parameters:

Returns:

Example:

ctx, err := secp.NewContext(secp.ContextSign | secp.ContextVerify)
if err != nil {
    log.Fatal(err)
}
defer ctx.Destroy()

Context.Destroy

Destroys the context and frees resources.

func (c *Context) Destroy()

Note: Contexts are automatically destroyed via finalizer, but explicit cleanup is recommended.

Context.Randomize

Randomizes the context with entropy for additional security.

func (c *Context) Randomize(seed32 []byte) (err error)

Parameters:

Returns:


Public Key Operations

Context.CreatePublicKey

Creates a public key from a private key.

func (c *Context) CreatePublicKey(seckey []byte) (pubkey []byte, err error)

Parameters:

Returns:

Context.SerializePublicKey

Serializes a public key to compressed or uncompressed format.

func (c *Context) SerializePublicKey(pubkey []byte, compressed bool) (output []byte, err error)

Parameters:

Returns:

Context.ParsePublicKey

Parses a serialized public key.

func (c *Context) ParsePublicKey(input []byte) (pubkey []byte, err error)

Parameters:

Returns:


ECDSA Signatures

Context.Sign

Creates an ECDSA signature.

func (c *Context) Sign(msg32 []byte, seckey []byte) (sig []byte, err error)

Parameters:

Returns:

Context.Verify

Verifies an ECDSA signature.

func (c *Context) Verify(msg32 []byte, sig []byte, pubkey []byte) (valid bool, err error)

Parameters:

Returns:

Context.SerializeSignatureDER

Serializes a signature to DER format.

func (c *Context) SerializeSignatureDER(sig []byte) (output []byte, err error)

Parameters:

Returns:

Context.ParseSignatureDER

Parses a DER-encoded signature.

func (c *Context) ParseSignatureDER(input []byte) (sig []byte, err error)

Parameters:

Returns:

Context.SerializeSignatureCompact

Serializes a signature to compact format (64 bytes).

func (c *Context) SerializeSignatureCompact(sig []byte) (output []byte, err error)

Parameters:

Returns:

Context.ParseSignatureCompact

Parses a compact (64-byte) signature.

func (c *Context) ParseSignatureCompact(input64 []byte) (sig []byte, err error)

Parameters:

Returns:

Context.NormalizeSignature

Normalizes a signature to lower-S form.

func (c *Context) NormalizeSignature(sig []byte) (normalized []byte, wasNormalized bool, err error)

Parameters:

Returns:


Schnorr Signatures

Context.CreateKeypair

Creates a keypair for Schnorr signatures.

func (c *Context) CreateKeypair(seckey []byte) (keypair Keypair, err error)

Parameters:

Returns:

Context.KeypairXOnlyPub

Extracts the x-only public key from a keypair.

func (c *Context) KeypairXOnlyPub(keypair Keypair) (xonly XOnlyPublicKey, pkParity int32, err error)

Parameters:

Returns:

Context.SchnorrSign

Creates a Schnorr signature (BIP-340).

func (c *Context) SchnorrSign(msg32 []byte, keypair Keypair, auxRand32 []byte) (sig []byte, err error)

Parameters:

Returns:

Context.SchnorrVerify

Verifies a Schnorr signature (BIP-340).

func (c *Context) SchnorrVerify(sig64 []byte, msg []byte, xonlyPubkey []byte) (valid bool, err error)

Parameters:

Returns:

Context.ParseXOnlyPublicKey

Parses a 32-byte x-only public key.

func (c *Context) ParseXOnlyPublicKey(input32 []byte) (xonly []byte, err error)

Parameters:

Returns:

Context.SerializeXOnlyPublicKey

Serializes an x-only public key to 32 bytes.

func (c *Context) SerializeXOnlyPublicKey(xonly []byte) (output32 []byte, err error)

Parameters:

Returns:

Context.XOnlyPublicKeyFromPublicKey

Converts a regular public key to an x-only public key.

func (c *Context) XOnlyPublicKeyFromPublicKey(pubkey []byte) (xonly []byte, pkParity int32, err error)

Parameters:

Returns:


ECDH

Context.ECDH

Computes an EC Diffie-Hellman shared secret.

func (c *Context) ECDH(pubkey []byte, seckey []byte) (output []byte, err error)

Parameters:

Returns:


Recovery

Context.SignRecoverable

Creates a recoverable ECDSA signature.

func (c *Context) SignRecoverable(msg32 []byte, seckey []byte) (sig []byte, err error)

Parameters:

Returns:

Context.SerializeRecoverableSignatureCompact

Serializes a recoverable signature.

func (c *Context) SerializeRecoverableSignatureCompact(sig []byte) (output64 []byte, recid int32, err error)

Parameters:

Returns:

Context.ParseRecoverableSignatureCompact

Parses a compact recoverable signature.

func (c *Context) ParseRecoverableSignatureCompact(input64 []byte, recid int32) (sig []byte, err error)

Parameters:

Returns:

Context.Recover

Recovers a public key from a recoverable signature.

func (c *Context) Recover(sig []byte, msg32 []byte) (pubkey []byte, err error)

Parameters:

Returns:


Utility Functions

Convenience functions that manage contexts automatically.

GeneratePrivateKey

func GeneratePrivateKey() (privKey []byte, err error)

Generates a random 32-byte private key.

PublicKeyFromPrivate

func PublicKeyFromPrivate(privKey []byte, compressed bool) (pubKey []byte, err error)

Generates a serialized public key from a private key.

SignMessage

func SignMessage(msgHash []byte, privKey []byte) (sig []byte, err error)

Signs a message and returns compact signature (64 bytes).

VerifyMessage

func VerifyMessage(msgHash []byte, compactSig []byte, serializedPubKey []byte) (valid bool, err error)

Verifies a compact signature.

SignMessageDER

func SignMessageDER(msgHash []byte, privKey []byte) (derSig []byte, err error)

Signs a message and returns DER-encoded signature.

VerifyMessageDER

func VerifyMessageDER(msgHash []byte, derSig []byte, serializedPubKey []byte) (valid bool, err error)

Verifies a DER-encoded signature.

SchnorrSign

func SchnorrSign(msgHash []byte, privKey []byte, auxRand []byte) (sig []byte, err error)

Creates a Schnorr signature (64 bytes).

SchnorrVerifyWithPubKey

func SchnorrVerifyWithPubKey(msgHash []byte, sig []byte, xonlyPubKey []byte) (valid bool, err error)

Verifies a Schnorr signature.

XOnlyPubKeyFromPrivate

func XOnlyPubKeyFromPrivate(privKey []byte) (xonly []byte, pkParity int32, err error)

Generates x-only public key from private key.

ComputeECDH

func ComputeECDH(serializedPubKey []byte, privKey []byte) (secret []byte, err error)

Computes ECDH shared secret.

SignRecoverableCompact

func SignRecoverableCompact(msgHash []byte, privKey []byte) (sig []byte, recID int32, err error)

Signs with recovery information.

RecoverPubKey

func RecoverPubKey(msgHash []byte, compactSig []byte, recID int32, compressed bool) (pubKey []byte, err error)

Recovers public key from signature.

ValidatePrivateKey

func ValidatePrivateKey(privKey []byte) (valid bool, err error)

Checks if a private key is valid.

IsPublicKeyValid

func IsPublicKeyValid(serializedPubKey []byte) (valid bool, err error)

Checks if a serialized public key is valid.


Constants

Context Flags

const (
    ContextNone       = 1
    ContextVerify     = 257
    ContextSign       = 513
    ContextDeclassify = 1025
)

EC Flags

const (
    ECCompressed   = 258
    ECUncompressed = 2
)

Size Constants

const (
    PublicKeySize              = 64
    CompressedPublicKeySize    = 33
    UncompressedPublicKeySize  = 65
    SignatureSize              = 64
    CompactSignatureSize       = 64
    PrivateKeySize             = 32
    SharedSecretSize           = 32
    SchnorrSignatureSize       = 64
    RecoverableSignatureSize   = 65
)

Types

Context

type Context struct {
    ctx uintptr
}

Opaque context handle.

Keypair

type Keypair [96]byte

Schnorr keypair structure.

XOnlyPublicKey

type XOnlyPublicKey [64]byte

64-byte x-only public key (internal representation).


Error Handling

All functions return errors. Common error conditions:

Always check returned errors:

result, err := secp.SomeFunction(...)
if err != nil {
    // Handle error
    return err
}

Thread Safety

Context objects are NOT thread-safe. Each goroutine should create its own context.

Utility functions are safe to use concurrently as they create temporary contexts.

Memory Management

Contexts are automatically cleaned up via finalizers, but explicit cleanup with Destroy() is recommended:

ctx, _ := secp.NewContext(secp.ContextSign)
defer ctx.Destroy()

All byte slices returned by the library are copies and safe to use/modify.