@@ -8,7 +8,7 @@ Swift package for elliptic curve public key cryptography, ECDSA, and Schnorr Sig
8
8
9
9
- Provide lightweight ECDSA & Schnorr Signatures functionality
10
10
- Support simple and advanced usage, including BIP-327 and BIP-340
11
- - Expose C bindings for full control of the secp256k1 implementation
11
+ - Expose libsecp256k1 bindings for full control of the implementation
12
12
- Offer a familiar API design inspired by [ Swift Crypto] ( https://github.com/apple/swift-crypto )
13
13
- Maintain automatic updates for Swift and libsecp256k1
14
14
- Ensure availability for Linux and Apple platform ecosystems
@@ -28,14 +28,14 @@ This package uses Swift Package Manager. To add it to your project:
28
28
Add the following to your ` Package.swift ` file:
29
29
30
30
``` swift
31
- .package (name : " swift-secp256k1" , url : " https://github.com/21-DOT-DEV/swift-secp256k1" , exact : " 0.18 .0" ),
31
+ .package (name : " swift-secp256k1" , url : " https://github.com/21-DOT-DEV/swift-secp256k1" , exact : " 0.19 .0" ),
32
32
```
33
33
34
34
Then, include ` secp256k1 ` as a dependency in your target:
35
35
36
36
``` swift
37
37
.target (name : " <target>" , dependencies : [
38
- .product (name : " secp256k1 " , package : " swift-secp256k1" )
38
+ .product (name : " P256K " , package : " swift-secp256k1" )
39
39
]),
40
40
```
41
41
@@ -54,11 +54,11 @@ arena 21-DOT-DEV/swift-secp256k1
54
54
55
55
### ECDSA
56
56
``` swift
57
- import secp256k1
57
+ import P256K
58
58
59
59
// Private key
60
60
let privateBytes = try ! " 14E4A74438858920D8A35FB2D88677580B6A2EE9BE4E711AE34EC6B396D87B5C" .bytes
61
- let privateKey = try ! secp256k1 .Signing .PrivateKey (rawRepresentation : privateBytes)
61
+ let privateKey = try ! P256K .Signing .PrivateKey (rawRepresentation : privateBytes)
62
62
63
63
// Public key
64
64
print (String (bytes : privateKey.publicKey .rawRepresentation ))
@@ -74,7 +74,7 @@ print(try! signature.derRepresentation.base64EncodedString())
74
74
### Schnorr
75
75
``` swift
76
76
// Strict BIP340 mode is disabled by default for Schnorr signatures with variable length messages
77
- let privateKey = try ! secp256k1 .Schnorr .PrivateKey ()
77
+ let privateKey = try ! P256K .Schnorr .PrivateKey ()
78
78
79
79
// Extra params for custom signing
80
80
var auxRand = try ! " C87AA53824B4D7AE2EB035A2B5BBBCCC080E76CDC6D1692C4B0B62D798E6D906" .bytes
@@ -87,7 +87,7 @@ let signature = try! privateKey.signature(message: &messageDigest, auxiliaryRand
87
87
## Tweak
88
88
89
89
``` swift
90
- let privateKey = try ! secp256k1 .Signing .PrivateKey ()
90
+ let privateKey = try ! P256K .Signing .PrivateKey ()
91
91
92
92
// Adding a tweak to the private key and public key
93
93
let tweak = try ! " 5f0da318c6e02f653a789950e55756ade9f194e1ec228d7f368de1bd821322b6" .bytes
@@ -98,8 +98,8 @@ let tweakedPublicKeyKey = try! privateKey.publicKey.add(tweak)
98
98
## Elliptic Curve Diffie Hellman
99
99
100
100
``` swift
101
- let privateKey = try ! secp256k1 .KeyAgreement .PrivateKey ()
102
- let publicKey = try ! secp256k1 .KeyAgreement .PrivateKey ().publicKey
101
+ let privateKey = try ! P256K .KeyAgreement .PrivateKey ()
102
+ let publicKey = try ! P256K .KeyAgreement .PrivateKey ().publicKey
103
103
104
104
// Create a compressed shared secret with a private key from only a public key
105
105
let sharedSecret = try ! privateKey.sharedSecretFromKeyAgreement (with : publicKey, format : .compressed )
@@ -111,17 +111,17 @@ let symmetricKey = SHA256.hash(data: sharedSecret.bytes)
111
111
## Silent Payments Scheme
112
112
113
113
``` swift
114
- let privateSign1 = try ! secp256k1 .Signing .PrivateKey ()
115
- let privateSign2 = try ! secp256k1 .Signing .PrivateKey ()
114
+ let privateSign1 = try ! P256K .Signing .PrivateKey ()
115
+ let privateSign2 = try ! P256K .Signing .PrivateKey ()
116
116
117
- let privateKey1 = try ! secp256k1 .KeyAgreement .PrivateKey (rawRepresentation : privateSign1.rawRepresentation )
118
- let privateKey2 = try ! secp256k1 .KeyAgreement .PrivateKey (rawRepresentation : privateSign2.rawRepresentation )
117
+ let privateKey1 = try ! P256K .KeyAgreement .PrivateKey (rawRepresentation : privateSign1.rawRepresentation )
118
+ let privateKey2 = try ! P256K .KeyAgreement .PrivateKey (rawRepresentation : privateSign2.rawRepresentation )
119
119
120
120
let sharedSecret1 = try ! privateKey1.sharedSecretFromKeyAgreement (with : privateKey2.publicKey )
121
121
let sharedSecret2 = try ! privateKey2.sharedSecretFromKeyAgreement (with : privateKey1.publicKey )
122
122
123
- let sharedSecretSign1 = try ! secp256k1 .Signing .PrivateKey (rawRepresentation : sharedSecret1.bytes )
124
- let sharedSecretSign2 = try ! secp256k1 .Signing .PrivateKey (rawRepresentation : sharedSecret2.bytes )
123
+ let sharedSecretSign1 = try ! P256K .Signing .PrivateKey (rawRepresentation : sharedSecret1.bytes )
124
+ let sharedSecretSign2 = try ! P256K .Signing .PrivateKey (rawRepresentation : sharedSecret2.bytes )
125
125
126
126
// Payable Silent Payment public key
127
127
let xonlyTweak2 = try ! sharedSecretSign2.publicKey .xonly .add (privateSign1.publicKey .xonly .bytes )
@@ -133,14 +133,14 @@ let privateTweak1 = try! sharedSecretSign1.add(xonly: privateSign1.publicKey.xon
133
133
## Recovery
134
134
135
135
``` swift
136
- let privateKey = try ! secp256k1 .Recovery .PrivateKey ()
136
+ let privateKey = try ! P256K .Recovery .PrivateKey ()
137
137
let messageData = " We're all Satoshi." .data (using : .utf8 )!
138
138
139
139
// Create a recoverable ECDSA signature
140
140
let recoverySignature = try ! privateKey.signature (for : messageData)
141
141
142
142
// Recover an ECDSA public key from a signature
143
- let publicKey = try ! secp256k1 .Recovery .PublicKey (messageData, signature : recoverySignature)
143
+ let publicKey = try ! P256K .Recovery .PublicKey (messageData, signature : recoverySignature)
144
144
145
145
// Convert a recoverable signature into a normal signature
146
146
let signature = try ! recoverySignature.normalize
@@ -149,8 +149,8 @@ let signature = try! recoverySignature.normalize
149
149
## Combine Public Keys
150
150
151
151
``` swift
152
- let privateKey = try ! secp256k1 .Signing .PrivateKey ()
153
- let publicKey = try ! secp256k1 .Signing .PrivateKey ().public
152
+ let privateKey = try ! P256K .Signing .PrivateKey ()
153
+ let publicKey = try ! P256K .Signing .PrivateKey ().public
154
154
155
155
// The Combine API arguments are an array of PublicKey objects and an optional format
156
156
publicKey.combine ([privateKey.publicKey ], format : .uncompressed )
@@ -168,38 +168,38 @@ oUQDQgAEt2uDn+2GqqYs/fmkBr5+rCQ3oiFSIJMAcjHIrTDS6HEELgguOatmFBOp
168
168
"""
169
169
170
170
// Import keys generated from OpenSSL
171
- let privateKey = try ! secp256k1 .Signing .PrivateKey (pemRepresentation : privateKeyString)
171
+ let privateKey = try ! P256K .Signing .PrivateKey (pemRepresentation : privateKeyString)
172
172
```
173
173
174
174
## MuSig2
175
175
176
176
``` swift
177
177
// Initialize private keys for two signers
178
- let firstPrivateKey = try secp256k1 .Schnorr .PrivateKey ()
179
- let secondPrivateKey = try secp256k1 .Schnorr .PrivateKey ()
178
+ let firstPrivateKey = try P256K .Schnorr .PrivateKey ()
179
+ let secondPrivateKey = try P256K .Schnorr .PrivateKey ()
180
180
181
181
// Aggregate the public keys using MuSig
182
- let aggregateKey = try secp256k1 .MuSig .aggregate ([firstPrivateKey.publicKey , secondPrivateKey.publicKey ])
182
+ let aggregateKey = try P256K .MuSig .aggregate ([firstPrivateKey.publicKey , secondPrivateKey.publicKey ])
183
183
184
184
// Message to be signed
185
185
let message = " Vires in Numeris." .data (using : .utf8 )!
186
186
let messageHash = SHA256.hash (data : message)
187
187
188
188
// Generate nonces for each signer
189
- let firstNonce = try secp256k1 .MuSig .Nonce .generate (
189
+ let firstNonce = try P256K .MuSig .Nonce .generate (
190
190
secretKey : firstPrivateKey,
191
191
publicKey : firstPrivateKey.publicKey ,
192
192
msg32 : Array (messageHash)
193
193
)
194
194
195
- let secondNonce = try secp256k1 .MuSig .Nonce .generate (
195
+ let secondNonce = try P256K .MuSig .Nonce .generate (
196
196
secretKey : secondPrivateKey,
197
197
publicKey : secondPrivateKey.publicKey ,
198
198
msg32 : Array (messageHash)
199
199
)
200
200
201
201
// Aggregate nonces
202
- let aggregateNonce = try secp256k1 .MuSig .Nonce (aggregating : [firstNonce.pubnonce , secondNonce.pubnonce ])
202
+ let aggregateNonce = try P256K .MuSig .Nonce (aggregating : [firstNonce.pubnonce , secondNonce.pubnonce ])
203
203
204
204
// Create partial signatures
205
205
let firstPartialSignature = try firstPrivateKey.partialSignature (
@@ -219,7 +219,7 @@ let secondPartialSignature = try secondPrivateKey.partialSignature(
219
219
)
220
220
221
221
// Aggregate partial signatures into a full signature
222
- let aggregateSignature = try secp256k1 .MuSig .aggregateSignatures ([firstPartialSignature, secondPartialSignature])
222
+ let aggregateSignature = try P256K .MuSig .aggregateSignatures ([firstPartialSignature, secondPartialSignature])
223
223
224
224
// Verify the aggregate signature
225
225
let isValid = aggregateKey.isValidSignature (
0 commit comments