Swift package with elliptic curve public key cryptography, ECDSA, Schnorr Signatures for Bitcoin and C bindings from libsecp256k1.
Long-term goals are:
This repository primarily uses Swift package manager as its build tool, so we recommend using that as well. Xcode comes with built-in support for Swift packages. From the menu bar, goto: File > Add Packages...
If you manage packages via a Package.swift
file, simply add secp256k1.swift
as a dependencies' clause in your Swift manifest:
.package(name: "secp256k1.swift", url: "https://github.com/GigaBitcoin/secp256k1.swift.git", exact: "0.13.0"),
Include secp256k1
as a dependency for your executable target:
.target(name: "<target>", dependencies: [
.product(name: "secp256k1", package: "secp256k1.swift")
]),
Try in a playground using the SPI Playgrounds app or 🏟 Arena
arena GigaBitcoin/secp256k1.swift
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.signature(for: messageData)
// DER signature
print(try! signature.derRepresentation.base64EncodedString())
// Strict BIP340 mode is disabled by default for Schnorr signatures with variable length messages
let privateKey = try! secp256k1.Schnorr.PrivateKey()
// 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.signature(message: &messageDigest, auxiliaryRand: &auxRand)
let privateKey = try! secp256k1.Signing.PrivateKey()
// Adding a tweak to the private key and public key
let tweak = try! "5f0da318c6e02f653a789950e55756ade9f194e1ec228d7f368de1bd821322b6".bytes
let tweakedPrivateKey = try! privateKey.add(tweak)
let tweakedPublicKeyKey = try! privateKey.publicKey.add(tweak)
let privateKey = try! secp256k1.KeyAgreement.PrivateKey()
let publicKey = try! secp256k1.KeyAgreement.PrivateKey().publicKey
// Create a compressed shared secret with a private key from only a public key
let sharedSecret = try! privateKey.sharedSecretFromKeyAgreement(with: publicKey, format: .compressed)
// By default, libsecp256k1 hashes the x-coordinate with version information.
let symmetricKey = SHA256.hash(data: sharedSecret.bytes)
let privateSign1 = try! secp256k1.Signing.PrivateKey()
let privateSign2 = try! secp256k1.Signing.PrivateKey()
let privateKey1 = try! secp256k1.KeyAgreement.PrivateKey(rawRepresentation: privateSign1.rawRepresentation)
let privateKey2 = try! secp256k1.KeyAgreement.PrivateKey(rawRepresentation: privateSign2.rawRepresentation)
let sharedSecret1 = try! privateKey1.sharedSecretFromKeyAgreement(with: privateKey2.publicKey)
let sharedSecret2 = try! privateKey2.sharedSecretFromKeyAgreement(with: privateKey1.publicKey)
let sharedSecretSign1 = try! secp256k1.Signing.PrivateKey(rawRepresentation: sharedSecret1.bytes)
let sharedSecretSign2 = try! secp256k1.Signing.PrivateKey(rawRepresentation: sharedSecret2.bytes)
// Payable Silent Payment public key
let xonlyTweak2 = try! sharedSecretSign2.publicKey.xonly.add(privateSign1.publicKey.xonly.bytes)
// Spendable Silent Payment private key
let privateTweak1 = try! sharedSecretSign1.add(xonly: privateSign1.publicKey.xonly.bytes)
let privateKey = try! secp256k1.Recovery.PrivateKey()
let messageData = "We're all Satoshi.".data(using: .utf8)!
// Create a recoverable ECDSA signature
let recoverySignature = try! privateKey.signature(for: messageData)
// Recover an ECDSA public key from a signature
let publicKey = try! secp256k1.Recovery.PublicKey(messageData, signature: recoverySignature)
// Convert a recoverable signature into a normal signature
let signature = try! recoverySignature.normalize
These APIs should not be considered stable and may change at any time.
link |
Stars: 78 |
Last commit: 34 minutes ago |
83186db
to 5f7903c
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/3655f7903c
to d373a72
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/366d373a72
to 20a5da5
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/36720a5da5
to 908e02d
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/368908e02d
to bf29f8d
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/369bf29f8d
to 60556c9
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/370dcf0815
to 940a631
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/37260556c9
to cb1a592
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/373cb1a592
to 67214f5
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/374940a631
to c433cd3
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/37567214f5
to 30574f2
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/37730574f2
to 0702ecb
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/3780702ecb
to 3c1a0fd
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/3793c1a0fd
to fd491ea
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/380fd491ea
to 0fa84f8
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/381c433cd3
to cba358d
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/3820fa84f8
to 3fc1de5
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/3833fc1de5
to 332af31
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/384332af31
to afd7eb4
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/385afd7eb4
to c9ebca9
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/386c9ebca9
to cc55757
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/389cc55757
to 0f7657d
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/3910f7657d
to 907a672
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/393ff33018
to 30fc9d6
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/394cba358d
to 5ac5632
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/395907a672
to b40e2d3
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/39630fc9d6
to 58e27bc
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/398b40e2d3
to c545fdc
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/3995ac5632
to 9c1b699
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/40058e27bc
to 53bc63f
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/40153bc63f
to bfeae12
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/402c545fdc
to 2bd5f3e
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/4032bd5f3e
to 0e00fc7
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/4040e00fc7
to 26392da
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/405bfeae12
to b2ccc8d
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/40626392da
to 374e2b5
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/4079c1b699
to a234f8a
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/408b2ccc8d
to b911b3c
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/409374e2b5
to 8d2960c
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/410a234f8a
to 710deac
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/4118d2960c
to ce765a5
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/412ce765a5
to eedd781
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/413eedd781
to 3d05c86
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/4143d05c86
to 6b9507a
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/415710deac
to 953b34a
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/4166b9507a
to 6ee1455
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/4176ee1455
to 4d7fe60
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/4184d7fe60
to ea26b71
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/419ea26b71
to 65c79fe
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/42065c79fe
to 727bec5
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/422727bec5
to b10ddd2
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/424b10ddd2
to 4fd00f4
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/4284fd00f4
to ba9cb6f
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/430953b34a
to 629f0b6
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/429ba9cb6f
to ee7aaf2
by @dependabot in https://github.com/GigaBitcoin/secp256k1.swift/pull/432Full Changelog: https://github.com/GigaBitcoin/secp256k1.swift/compare/0.12.2...0.13.0
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics