π
K1 Safer than K2
K1 is Swift wrapper around libsecp256k1 (bitcoin-core/secp256k1), offering ECDSA, Schnorr (BIP340) and ECDH features.
Features
ECDSA Signatures
let alice = try K1.PrivateKey.generateNew()
let message = "Send Bob 3 BTC".data(using: .utf8)!
let signature = try alice.ecdsaSign(unhashed: message)
let isSignatureValid = try alice.publicKey.isValidECDSASignature(signature, unhashed: message)
assert(isSignatureValid, "Signature should be valid.")
Schnorr Signatures
let alice = try K1.PrivateKey.generateNew()
let message = "Send Bob 3 BTC".data(using: .utf8)!
let signature = try alice.schnorrSign(unhashed: message)
let isSignatureValid = try alice.publicKey.isValidSchnorrSignature(signature, unhashed: message)
assert(isSignatureValid, "Signature should be valid.")
Schnorr Scheme
The Schnorr signature implementation is BIP340, since we use libsecp256k1 which only provides the BIP340 Schnorr scheme.
It is worth noting that some Schnorr implementations are incompatible with BIP340 and thus this library, e.g. Zilliqa's (kudelski report, libsecp256k1 proposal, Twitter thread).
ECDH
let alice = try K1.PrivateKey.generateNew()
let bob = try K1.PrivateKey.generateNew()
let ab = try alice.sharedSecret(with: bob.publicKey)
let ba = try bob.sharedSecret(with: alice.publicKey)
assert(ab == ba, "Alice and Bob should be able to agree on the same secret")
Alternatives
- GigaBitcoin/secp256k1.swift (also using
libsecp256k1
,β No Schnorr) - KevinVitale/WalletKit (also using
libsecp256k1
,β No Schnorr) - yenom/BitcoinKit (
π Discontinued, also usinglibsecp256k1
,β No Schnorr) - oleganza/CoreBitcoin (ObjC + Swift,
β No Schnorr) - Sajjon/EllipticCurveKit (mine,
β£οΈ unsafe,β Schnorr support)
Non-Swift but SPM support
greymass/secp256k1 (Fork of libsecp256k1
)