go get p8k.mleku.dev
Install libsecp256k1 on your system:
Ubuntu/Debian:
sudo apt-get install libsecp256k1-dev
macOS:
brew install libsecp256k1
From source:
make install-secp256k1
import "p8k.mleku.dev"
// Generate key pair
privKey, _ := secp.GeneratePrivateKey()
pubKey, _ := secp.PublicKeyFromPrivate(privKey, true) // compressed
// Sign message
msgHash := sha256.Sum256([]byte("Hello"))
sig, _ := secp.SignMessage(msgHash[:], privKey)
// Verify signature
valid, _ := secp.VerifyMessage(msgHash[:], sig, pubKey)
// Generate x-only public key
xonly, _, _ := secp.XOnlyPubKeyFromPrivate(privKey)
// Sign with Schnorr
auxRand, _ := secp.GeneratePrivateKey() // 32 random bytes
sig, _ := secp.SchnorrSign(msgHash[:], privKey, auxRand)
// Verify
valid, _ := secp.SchnorrVerifyWithPubKey(msgHash[:], sig, xonly)
// Compute shared secret
sharedSecret, _ := secp.ComputeECDH(theirPubKey, myPrivKey)
// Sign with recovery
sig, recID, _ := secp.SignRecoverableCompact(msgHash[:], privKey)
// Recover public key
recoveredPubKey, _ := secp.RecoverPubKey(msgHash[:], sig, recID, true)
For more control, use the context-based API:
ctx, _ := secp.NewContext(secp.ContextSign | secp.ContextVerify)
defer ctx.Destroy()
// Use ctx methods directly
pubKey, _ := ctx.CreatePublicKey(privKey)
sig, _ := ctx.Sign(msgHash[:], privKey)
valid, _ := ctx.Verify(msgHash[:], sig, pubKey)
secp.PrivateKeySize // 32 bytes
secp.PublicKeySize // 64 bytes (internal format)
secp.CompressedPublicKeySize // 33 bytes (serialized)
secp.UncompressedPublicKeySize // 65 bytes (serialized)
secp.SignatureSize // 64 bytes (internal format)
secp.CompactSignatureSize // 64 bytes (serialized)
secp.SchnorrSignatureSize // 64 bytes
secp.SharedSecretSize // 32 bytes
secp.RecoverableSignatureSize // 65 bytes
secp.ContextNone // No flags
secp.ContextVerify // For verification operations
secp.ContextSign // For signing operations
secp.ContextDeclassify // For declassification
# Run tests
make test
# Run benchmarks
make bench
# Run examples
make run-examples
Not all modules may be available in your libsecp256k1 build:
--enable-module-schnorrsig--enable-module-ecdh--enable-module-recoveryFunctions will return an error if the required module is not available.
All functions return errors. Always check them:
sig, err := secp.SignMessage(msgHash[:], privKey)
if err != nil {
log.Fatal(err)
}
Context objects are NOT thread-safe. Each goroutine should have its own context.
// BAD: Sharing context across goroutines
ctx, _ := secp.NewContext(secp.ContextSign)
go func() { ctx.Sign(...) }()
go func() { ctx.Sign(...) }() // Race condition!
// GOOD: Each goroutine gets its own context
go func() {
ctx, _ := secp.NewContext(secp.ContextSign)
defer ctx.Destroy()
ctx.Sign(...)
}()
MIT License