A pure Haskell implementation of BIP0340 Schnorr signatures, deterministic RFC6979 ECDSA (with BIP0146-style "low-S" signatures), ECDH, and various primitives on the elliptic curve secp256k1.
(See also ppad-csecp256k1 for FFI bindings to bitcoin-core/secp256k1.)
A sample GHCi session:
> -- pragmas and b16 import for illustration only; not required
> :set -XOverloadedStrings
> :set -XBangPatterns
> import qualified Data.ByteString.Base16 as B16
>
> -- import qualified
> import qualified Crypto.Curve.Secp256k1 as Secp256k1
>
> -- secret, public keys
> let sec = 0xB7E151628AED2A6ABF7158809CF4F3C762E7160F38B4DA56A784D9045190CFEF
:{
ghci| let Just pub = Secp256k1.parse_point . B16.decodeLenient $
ghci| "DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659"
ghci| :}
>
> let msg = "i approve of this message"
>
> -- create and verify a schnorr signature for the message
> let Just sig0 = Secp256k1.sign_schnorr sec msg mempty
> Secp256k1.verify_schnorr msg pub sig0
True
>
> -- create and verify a low-S ECDSA signature for the message
> let Just sig1 = Secp256k1.sign_ecdsa sec msg
> Secp256k1.verify_ecdsa msg pub sig1
True
>
> -- for faster signs (especially w/ECDSA) and verifies, use a context
> let !tex = Secp256k1.precompute
> Secp256k1.verify_schnorr' tex msg pub sig0
True
Haddocks (API documentation, etc.) are hosted at docs.ppad.tech/secp256k1.
The aim is best-in-class performance for pure Haskell code.
Current benchmark figures on an M4 MacBook Air look like (use cabal bench to run the benchmark suite):
benchmarking schnorr/sign_schnorr' (large)
time 42.04 μs (42.00 μs .. 42.11 μs)
1.000 R² (1.000 R² .. 1.000 R²)
mean 42.08 μs (42.05 μs .. 42.13 μs)
std dev 139.4 ns (100.1 ns .. 198.1 ns)
benchmarking schnorr/verify_schnorr'
time 88.88 μs (88.69 μs .. 89.17 μs)
1.000 R² (1.000 R² .. 1.000 R²)
mean 88.85 μs (88.70 μs .. 89.25 μs)
std dev 747.4 ns (346.3 ns .. 1.409 μs)
benchmarking ecdsa/sign_ecdsa' (large)
time 25.76 μs (25.73 μs .. 25.80 μs)
1.000 R² (1.000 R² .. 1.000 R²)
mean 25.76 μs (25.74 μs .. 25.81 μs)
std dev 105.5 ns (49.05 ns .. 205.4 ns)
benchmarking ecdsa/verify_ecdsa'
time 85.66 μs (85.60 μs .. 85.72 μs)
1.000 R² (1.000 R² .. 1.000 R²)
mean 85.65 μs (85.59 μs .. 85.71 μs)
std dev 190.2 ns (155.3 ns .. 247.4 ns)
benchmarking ecdh/ecdh (large)
time 88.01 μs (87.80 μs .. 88.30 μs)
1.000 R² (1.000 R² .. 1.000 R²)
mean 87.94 μs (87.81 μs .. 88.13 μs)
std dev 492.2 ns (361.8 ns .. 701.2 ns)
Ensure you compile with the 'llvm' flag (and that ppad-fixed and ppad-sha256 have been compiled with the 'llvm' flag) for maximum performance.
The Schnorr implementation within has been tested against the official BIP0340 vectors, and ECDSA and ECDH have been tested against the relevant Wycheproof vectors (with the former also being tested against noble-secp256k1's vectors), so their implementations are likely to be accurate and safe from attacks targeting e.g. faulty nonce generation or malicious inputs for signature or public key parameters.
All timing-sensitive operations execute in constant time on x86-64 and aarch64 under GHC's LLVM and NCG backends.
If you discover any vulnerabilities, please disclose them via [email protected].
You'll require Nix with flake support enabled. Enter a development shell with:
$ nix develop
Then do e.g.:
$ cabal repl ppad-secp256k1
to get a REPL for the main library.