Documentation
¶
Overview ¶
Package p256k is a signer interface that (by default) uses the bitcoin/libsecp256k1 library for fast signature creation and verification of the BIP-340 nostr X-only signatures and public keys, and ECDH.
Currently the ECDH is only implemented with the btcec library.
Index ¶
- func AssertLen(b []byte, length int, name string) (err error)
- func FromSecretBytes(skb []byte) (pkb []byte, sec *Sec, pub *XPublicKey, err error)
- func Generate() (skb, pkb []byte, sec *Sec, pub *XPublicKey, err error)
- func Negate(uskb []byte)
- func RandomizeContext(ctx *C.secp256k1_context)
- func Sign(msg *Uchar, sk *SecKey) (sig []byte, err error)
- func SignFromBytes(msg, sk []byte) (sig []byte, err error)
- func Verify(msg, sig *Uchar, pk *PubKey) (valid bool)
- func VerifyFromBytes(msg, sig, pk []byte) (err error)
- func Zero(sk *SecKey)
- type Cint
- type Context
- type ECPub
- type ECPubKey
- type Keygen
- type Pub
- type PubKey
- type Sec
- type SecKey
- type Signer
- func (s *Signer) ECDH(pubkeyBytes []byte) (secret []byte, err error)
- func (s *Signer) Generate() (err error)
- func (s *Signer) InitPub(pub []byte) (err error)
- func (s *Signer) InitSec(skb []byte) (err error)
- func (s *Signer) Pub() (b []byte)
- func (s *Signer) Sec() (b []byte)
- func (s *Signer) Sign(msg []byte) (sig []byte, err error)
- func (s *Signer) Verify(msg, sig []byte) (valid bool, err error)
- func (s *Signer) Zero()
- type Uchar
- type XPublicKey
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FromSecretBytes ¶
func FromSecretBytes(skb []byte) ( pkb []byte, sec *Sec, pub *XPublicKey, err error, )
FromSecretBytes parses and processes what should be a secret key. If it is a correct key within the curve order, but with a public key having an odd Y coordinate, it returns an error with the fixed key.
func Generate ¶
func Generate() ( skb, pkb []byte, sec *Sec, pub *XPublicKey, err error, )
Generate gathers entropy to generate a full set of bytes and CGO values of it and derived from it to perform signature and ECDH operations.
func Negate ¶
func Negate(uskb []byte)
Negate inverts a secret key so an odd prefix bit becomes even and vice versa.
func RandomizeContext ¶
func RandomizeContext(ctx *C.secp256k1_context)
func SignFromBytes ¶
SignFromBytes Signs a message using a provided secret key and message as raw bytes.
func VerifyFromBytes ¶
VerifyFromBytes a signature from the raw bytes of the message hash, signature and public key
Types ¶
type Context ¶
type Context = C.secp256k1_context
func CreateContext ¶
func CreateContext() *Context
func CreateRandomContext ¶
func CreateRandomContext() (c *Context)
type ECPub ¶
type ECPub struct {
Key ECPubKey
}
func ECPubFromSchnorrBytes ¶
ECPubFromSchnorrBytes converts a BIP-340 public key to its even standard 33 byte encoding.
This function is for the purpose of getting a key to do ECDH from an x-only key.
type ECPubKey ¶
type ECPubKey = C.secp256k1_pubkey
type Keygen ¶
type Keygen struct {
// contains filtered or unexported fields
}
Keygen is an implementation of a key miner designed to be used for vanity key generation with X-only BIP-340 keys.
func NewKeygen ¶
func NewKeygen() (k *Keygen)
NewKeygen allocates the required buffers for deriving a key. This should only be done once to avoid garbage and make the key mining as fast as possible.
This allocates everything and creates proper CGO variables needed for the generate function so they only need to be allocated once per thread.
func (*Keygen) Generate ¶
func (k *Keygen) Generate() ( sec *Sec, pub *XPublicKey, pubBytes []byte, err error, )
Generate takes a pair of buffers for the secret and ec pubkey bytes and gathers new entropy and returns a valid secret key and the compressed pubkey bytes for the partial collision search.
The first byte of pubBytes must be sliced off before deriving the hex/Bech32 forms of the nostr public key.
type Pub ¶
type Pub struct {
Key PubKey
}
Pub is a schnorr BIP-340 public key.
func PubFromBytes ¶
PubFromBytes creates a public key from raw bytes.
type PubKey ¶
type PubKey = C.secp256k1_xonly_pubkey
type SecKey ¶
type SecKey = C.secp256k1_keypair
type Signer ¶
type Signer struct {
// SecretKey is the secret key.
SecretKey *SecKey
// PublicKey is the public key.
PublicKey *PubKey
// BTCECSec is needed for ECDH as currently the CGO bindings don't include it
BTCECSec *btcec.SecretKey
// contains filtered or unexported fields
}
Signer implements the signer.I interface.
Either the Sec or Pub must be populated, the former is for generating signatures, the latter is for verifying them.
When using this library only for verification, a constructor that converts from bytes to PubKey is needed prior to calling Verify.
type Uchar ¶
type XPublicKey ¶
type XPublicKey struct {
Key *C.secp256k1_xonly_pubkey
}
func NewXPublicKey ¶
func NewXPublicKey() *XPublicKey