Elixir NIF bindings for the bitcoin-core/secp256k1 cryptographic library. Used extensively in Bitcoin, Ethereum, Nostr, and other blockchain/cryptocurrency applications.
- Keypair generation - secure random secret keys with compressed, uncompressed, or x-only public keys
- ECDSA signatures - sign and verify using the traditional Bitcoin signature scheme
- Schnorr signatures - BIP-340 compatible, used in Taproot and Nostr
- MuSig2 - BIP-327 multi-party Schnorr signatures (experimental)
- ECDH - Diffie-Hellman shared secret computation
The library compiles the underlying C library automatically, but requires build tools:
Linux (Ubuntu/Debian)
sudo apt-get install build-essential automake libtool autoconfmacOS
brew install make gcc autoconf automake libtoolAdd to your mix.exs:
def deps do
[
{:lib_secp256k1, "~> 0.7"}
]
end# Compressed pubkey (33 bytes) - standard Bitcoin format
{seckey, pubkey} = Secp256k1.keypair(:compressed)
# X-only pubkey (32 bytes) - for Schnorr/Taproot/Nostr
{seckey, pubkey} = Secp256k1.keypair(:xonly)
# Derive pubkey from existing secret key
pubkey = Secp256k1.pubkey(seckey, :compressed){seckey, pubkey} = Secp256k1.keypair(:compressed)
# Sign a message hash
msg_hash = :crypto.hash(:sha256, "Hello Bitcoin!")
signature = Secp256k1.ecdsa_sign(msg_hash, seckey)
# Verify
Secp256k1.ecdsa_valid?(signature, msg_hash, pubkey)
#=> true{seckey, pubkey} = Secp256k1.keypair(:xonly)
# Sign (works with 32-byte hash or arbitrary message)
msg_hash = :crypto.hash(:sha256, "Hello Nostr!")
signature = Secp256k1.schnorr_sign(msg_hash, seckey)
# Verify
Secp256k1.schnorr_valid?(signature, msg_hash, pubkey)
#=> trueFor multi-party signing where multiple parties create a single aggregated signature. See the MuSig Guide for the complete protocol.
# Aggregate public keys from multiple signers
{:ok, agg_pubkey, cache} = Secp256k1.MuSig.pubkey_agg([alice_pubkey, bob_pubkey])
# ... nonce generation, aggregation, signing rounds ...
# Final signature verifies as standard Schnorr
Secp256k1.schnorr_valid?(final_sig, msg_hash, agg_pubkey)- HexDocs - API reference
- Usage Guide - detailed examples
- MuSig Guide - multi-signature protocol
- Linux - fully supported, primary development platform
- macOS - supported with Homebrew dependencies
- Windows - not tested, contributions welcome
WTFPL - Do What The Fuck You Want To Public License
The underlying secp256k1 C library is MIT licensed.