p256k

package
v1.0.14 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2024 License: CC0-1.0 Imports: 9 Imported by: 0

README

p256k1

This is a library that uses the bitcoin-core optimized secp256k1 elliptic curve signatures library for nostr schnorr signatures.

If you don't want to use cgo or can't use cgo you need to set the btcec build tag, which will set an override to use the code from the btcsuite/decred, the decred is actually where the schnorr signatures are (ikr?) - this repo uses my fork of this mess of shitcoinery and bad, slow Go code is cleaned up and unified in github.com/mleku/btcec and includes the bech32 and base58check libraries. And the messy precomputed values are upgraded to use the modern "embed" enabling a faster app startup for initialising this array (at the cost of a little more binary size).

For ubuntu, you need these

sudo apt -y install build-essential autoconf libtool  

The directory pkg/libsecp256k1/secp256k1 needs to be initialized and built and installed, like so:

cd crypto/p256k
git clone https://github.com/bitcoin-core/secp256k1.git
cd secp256k1
git submodule init
git submodule update

Then to build, you can refer to the instructions or just use the default autotools:

./autogen.sh
./configure --enable-module-schnorrsig --prefix=/usr
make
sudo make install

On WSL2 you may have to attend to various things to make this work, setting up your basic locale (uncomment one or more in /etc/locale.gen, and run locale-gen), installing the basic build tools (build-essential or base-devel) and of course git, curl, wget, libtool and autoconf.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertLen

func AssertLen(b B, length int, name string) (err E)

func ECPubFromBytes

func ECPubFromBytes(pkb B) (pub *ECPub, err E)

ECPubFromBytes parses a pubkey from 33 bytes to the bitcoin-core/secp256k1 struct.

func ECPubFromSchnorrBytes

func ECPubFromSchnorrBytes(xkb B) (pub *ECPub, err E)

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.

func FromSecretBytes

func FromSecretBytes(skb B) (pkb B, sec *Sec, pub *XPublicKey, ecPub *PublicKey, 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 GenSec

func GenSec() (sec *Sec, err E)

func Generate

func Generate() (skb, pkb B, sec *Sec, pub *XPublicKey, ecpub *PublicKey, err E)

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.

Note that the pubkey bytes are the 33 byte form with the sign prefix, slice it off for X-only use.

func Msg

func Msg(b B) (id *Uchar, err E)

func Negate

func Negate(uskb B)

func PubFromBytes

func PubFromBytes(pk B) (pub *Pub, err E)

func RandomizeContext

func RandomizeContext(ctx *C.secp256k1_context)

func SecFromBytes

func SecFromBytes(sk B) (sec *Sec, err E)

func Sig

func Sig(b B) (sig *Uchar, err E)

func Sign

func Sign(msg *Uchar, sk *SecKey) (sig B, err E)

func SignFromBytes

func SignFromBytes(msg, sk B) (sig B, err E)

func Verify

func Verify(msg, sig *Uchar, pk *PubKey) (valid bool)

func VerifyFromBytes

func VerifyFromBytes(msg, sig, pk B) (err error)

func Zero

func Zero(sk *SecKey)

Types

type Cint

type Cint = C.int

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
}

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() (pubBytes B, err E)

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.

func (*Keygen) KeyPairBytes

func (k *Keygen) KeyPairBytes() (secBytes, cmprPubBytes B)

func (*Keygen) Negate

func (k *Keygen) Negate()

Negate should be called when the pubkey's X coordinate is a match but the prefix is a 3. The X coordinate will not change but this ensures that when the X-only key has a 2 prefix added for ECDH and other purposes that it works correctly. This can be done after a match is found as it does not impact anything except the first byte.

type Pub

type Pub struct {
	Key PubKey
}

func (*Pub) Pub

func (p *Pub) Pub() *PubKey

func (*Pub) PubB

func (p *Pub) PubB() (b B)

func (*Pub) ToBytes

func (p *Pub) ToBytes() (b B, err E)

type PubKey

type PubKey = C.secp256k1_xonly_pubkey

type PublicKey

type PublicKey struct {
	Key *C.secp256k1_pubkey
}

func NewPublicKey

func NewPublicKey() *PublicKey

type Sec

type Sec struct {
	Key SecKey
}

func (*Sec) Pub

func (s *Sec) Pub() (p *Pub, err E)

func (*Sec) Sec

func (s *Sec) Sec() *SecKey

type SecKey

type SecKey = C.secp256k1_keypair

type Signer

type Signer struct {
	SecretKey   *SecKey
	PublicKey   *PubKey
	ECPublicKey *ECPubKey // not sure what this is useful for yet.
	BTCECSec    *btcec.SecretKey
	// contains filtered or unexported fields
}

Signer implements the nostr.Signer 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.

func (*Signer) ECDH

func (s *Signer) ECDH(xkb B) (secret B, err error)

func (*Signer) ECPub

func (s *Signer) ECPub() (b B)

func (*Signer) Generate

func (s *Signer) Generate() (err E)

func (*Signer) InitPub

func (s *Signer) InitPub(pub B) (err error)

func (*Signer) InitSec

func (s *Signer) InitSec(skb B) (err error)

func (*Signer) Negate

func (s *Signer) Negate()

func (*Signer) Pub

func (s *Signer) Pub() (b B)

func (*Signer) Sec

func (s *Signer) Sec() (b B)

func (*Signer) Sign

func (s *Signer) Sign(msg B) (sig B, err error)

func (*Signer) Verify

func (s *Signer) Verify(msg, sig B) (valid bool, err error)

func (*Signer) Zero

func (s *Signer) Zero()

type Uchar

type Uchar = C.uchar

func GetRandom

func GetRandom() (u *Uchar)

func ToUchar

func ToUchar(b B) (u *Uchar)

type XPublicKey

type XPublicKey struct {
	Key *C.secp256k1_xonly_pubkey
}

func NewXPublicKey

func NewXPublicKey() *XPublicKey

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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