Awesome
🔐 secp256k1.swift
Swift package with elliptic curve public key cryptography, ECDSA, Schnorr Signatures for Bitcoin and C bindings from libsecp256k1.
Objectives
Long-term goals are:
- Lightweight ECDSA & Schnorr Signatures functionality
- Built for simple or advance usage with things like BIP340
- Exposed C bindings to take full control of the secp256k1 implementation
- Familiar API design by modeling after Swift Crypto
- Automatic updates for Swift and libsecp256k1
- Availability for Linux and Apple platform ecosystems
Example Usage
ECDSA
import secp256k1
// Private key
let privateBytes = try! "14E4A74438858920D8A35FB2D88677580B6A2EE9BE4E711AE34EC6B396D87B5C".bytes
let privateKey = try! secp256k1.Signing.PrivateKey(rawRepresentation: privateBytes)
// Public key
print(String(bytes: privateKey.publicKey.rawRepresentation))
// ECDSA
let messageData = "We're all Satoshi.".data(using: .utf8)!
let signature = try! privateKey.ecdsa.signature(for: messageData)
// DER signature
print(try! signature.derRepresentation.base64EncodedString())
Schnorr
let privateBytes = try! "C90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B14E5C9".bytes
let privateKey = try! secp256k1.Signing.PrivateKey(rawRepresentation: privateBytes)
// Extra params for custom signing
var auxRand = try! "C87AA53824B4D7AE2EB035A2B5BBBCCC080E76CDC6D1692C4B0B62D798E6D906".bytes
var messageDigest = try! "7E2D58D8B3BCDF1ABADEC7829054F90DDA9805AAB56C77333024B9D0A508B75C".bytes
// API allows for signing variable length messages
let signature = try! privateKey.schnorr.signature(message: &messageDigest, auxiliaryRand: &auxRand)
Getting Started
In your Package.swift
:
.package(url: "https://github.com/GigaBitcoin/secp256k1.swift.git", .upToNextMajor(from: "0.5.0"))
Try in a playground using the SPI Playgrounds app or 🏟 Arena
arena GigaBitcoin/secp256k1.swift
Danger
These APIs should not be considered stable and may change at any time, libsecp256k1 is still experimental and has not been formally released.