Skip to content

Commit a98da3d

Browse files
committed
Rename secp256k1 enum to P256K
1 parent 8e3a5a4 commit a98da3d

29 files changed

+289
-289
lines changed

Projects/Sources/P256K/P256K.swift

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../Sources/ZKP/P256K.swift

Projects/Sources/P256K/secp256k1.swift

-1
This file was deleted.

Projects/Sources/XCFrameworkApp/ContentView.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public struct ContentView: View {
55
public init() {}
66

77
public var body: some View {
8-
let privateKey = try! secp256k1.Signing.PrivateKey()
8+
let privateKey = try! P256K.Signing.PrivateKey()
99
let stringKey = String(bytes: privateKey.dataRepresentation)
1010

1111
Text("KEY: \(stringKey)")

README.md

+27-27
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Swift package for elliptic curve public key cryptography, ECDSA, and Schnorr Sig
88

99
- Provide lightweight ECDSA & Schnorr Signatures functionality
1010
- 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
1212
- Offer a familiar API design inspired by [Swift Crypto](https://github.com/apple/swift-crypto)
1313
- Maintain automatic updates for Swift and libsecp256k1
1414
- Ensure availability for Linux and Apple platform ecosystems
@@ -28,14 +28,14 @@ This package uses Swift Package Manager. To add it to your project:
2828
Add the following to your `Package.swift` file:
2929

3030
```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"),
3232
```
3333

3434
Then, include `secp256k1` as a dependency in your target:
3535

3636
```swift
3737
.target(name: "<target>", dependencies: [
38-
.product(name: "secp256k1", package: "swift-secp256k1")
38+
.product(name: "P256K", package: "swift-secp256k1")
3939
]),
4040
```
4141

@@ -54,11 +54,11 @@ arena 21-DOT-DEV/swift-secp256k1
5454

5555
### ECDSA
5656
```swift
57-
import secp256k1
57+
import P256K
5858

5959
// Private key
6060
let privateBytes = try! "14E4A74438858920D8A35FB2D88677580B6A2EE9BE4E711AE34EC6B396D87B5C".bytes
61-
let privateKey = try! secp256k1.Signing.PrivateKey(rawRepresentation: privateBytes)
61+
let privateKey = try! P256K.Signing.PrivateKey(rawRepresentation: privateBytes)
6262

6363
// Public key
6464
print(String(bytes: privateKey.publicKey.rawRepresentation))
@@ -74,7 +74,7 @@ print(try! signature.derRepresentation.base64EncodedString())
7474
### Schnorr
7575
```swift
7676
// 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()
7878

7979
// Extra params for custom signing
8080
var auxRand = try! "C87AA53824B4D7AE2EB035A2B5BBBCCC080E76CDC6D1692C4B0B62D798E6D906".bytes
@@ -87,7 +87,7 @@ let signature = try! privateKey.signature(message: &messageDigest, auxiliaryRand
8787
## Tweak
8888

8989
```swift
90-
let privateKey = try! secp256k1.Signing.PrivateKey()
90+
let privateKey = try! P256K.Signing.PrivateKey()
9191

9292
// Adding a tweak to the private key and public key
9393
let tweak = try! "5f0da318c6e02f653a789950e55756ade9f194e1ec228d7f368de1bd821322b6".bytes
@@ -98,8 +98,8 @@ let tweakedPublicKeyKey = try! privateKey.publicKey.add(tweak)
9898
## Elliptic Curve Diffie Hellman
9999

100100
```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
103103

104104
// Create a compressed shared secret with a private key from only a public key
105105
let sharedSecret = try! privateKey.sharedSecretFromKeyAgreement(with: publicKey, format: .compressed)
@@ -111,17 +111,17 @@ let symmetricKey = SHA256.hash(data: sharedSecret.bytes)
111111
## Silent Payments Scheme
112112

113113
```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()
116116

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)
119119

120120
let sharedSecret1 = try! privateKey1.sharedSecretFromKeyAgreement(with: privateKey2.publicKey)
121121
let sharedSecret2 = try! privateKey2.sharedSecretFromKeyAgreement(with: privateKey1.publicKey)
122122

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)
125125

126126
// Payable Silent Payment public key
127127
let xonlyTweak2 = try! sharedSecretSign2.publicKey.xonly.add(privateSign1.publicKey.xonly.bytes)
@@ -133,14 +133,14 @@ let privateTweak1 = try! sharedSecretSign1.add(xonly: privateSign1.publicKey.xon
133133
## Recovery
134134

135135
```swift
136-
let privateKey = try! secp256k1.Recovery.PrivateKey()
136+
let privateKey = try! P256K.Recovery.PrivateKey()
137137
let messageData = "We're all Satoshi.".data(using: .utf8)!
138138

139139
// Create a recoverable ECDSA signature
140140
let recoverySignature = try! privateKey.signature(for: messageData)
141141

142142
// 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)
144144

145145
// Convert a recoverable signature into a normal signature
146146
let signature = try! recoverySignature.normalize
@@ -149,8 +149,8 @@ let signature = try! recoverySignature.normalize
149149
## Combine Public Keys
150150

151151
```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
154154

155155
// The Combine API arguments are an array of PublicKey objects and an optional format
156156
publicKey.combine([privateKey.publicKey], format: .uncompressed)
@@ -168,38 +168,38 @@ oUQDQgAEt2uDn+2GqqYs/fmkBr5+rCQ3oiFSIJMAcjHIrTDS6HEELgguOatmFBOp
168168
"""
169169

170170
// Import keys generated from OpenSSL
171-
let privateKey = try! secp256k1.Signing.PrivateKey(pemRepresentation: privateKeyString)
171+
let privateKey = try! P256K.Signing.PrivateKey(pemRepresentation: privateKeyString)
172172
```
173173

174174
## MuSig2
175175

176176
```swift
177177
// 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()
180180

181181
// 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])
183183

184184
// Message to be signed
185185
let message = "Vires in Numeris.".data(using: .utf8)!
186186
let messageHash = SHA256.hash(data: message)
187187

188188
// Generate nonces for each signer
189-
let firstNonce = try secp256k1.MuSig.Nonce.generate(
189+
let firstNonce = try P256K.MuSig.Nonce.generate(
190190
secretKey: firstPrivateKey,
191191
publicKey: firstPrivateKey.publicKey,
192192
msg32: Array(messageHash)
193193
)
194194

195-
let secondNonce = try secp256k1.MuSig.Nonce.generate(
195+
let secondNonce = try P256K.MuSig.Nonce.generate(
196196
secretKey: secondPrivateKey,
197197
publicKey: secondPrivateKey.publicKey,
198198
msg32: Array(messageHash)
199199
)
200200

201201
// 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])
203203

204204
// Create partial signatures
205205
let firstPartialSignature = try firstPrivateKey.partialSignature(
@@ -219,7 +219,7 @@ let secondPartialSignature = try secondPrivateKey.partialSignature(
219219
)
220220

221221
// 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])
223223

224224
// Verify the aggregate signature
225225
let isValid = aggregateKey.isValidSignature(

Sources/P256K/P256K.swift

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../ZKP/P256K.swift

Sources/P256K/secp256k1.swift

-1
This file was deleted.

Sources/ZKP/Asymmetric.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import Foundation
1212

1313
/// An elliptic curve that enables secp256k1 signatures and key agreement.
14-
public extension secp256k1 {
14+
public extension P256K {
1515
/// A mechanism used to create or verify a cryptographic signature using the secp256k1
1616
/// elliptic curve digital signature algorithm (ECDSA).
1717
enum Signing {
@@ -49,7 +49,7 @@ public extension secp256k1 {
4949
///
5050
/// - Parameter format: The key format, default is .compressed.
5151
/// - Throws: An error if the private key cannot be generated.
52-
public init(format: secp256k1.Format = .compressed) throws {
52+
public init(format: P256K.Format = .compressed) throws {
5353
self.baseKey = try PrivateKeyImplementation(format: format)
5454
}
5555

@@ -58,7 +58,7 @@ public extension secp256k1 {
5858
/// - Parameter data: A data representation of the key.
5959
/// - Parameter format: The key format, default is .compressed.
6060
/// - Throws: An error if the raw representation does not create a private key for signing.
61-
public init<D: ContiguousBytes>(dataRepresentation data: D, format: secp256k1.Format = .compressed) throws {
61+
public init<D: ContiguousBytes>(dataRepresentation data: D, format: P256K.Format = .compressed) throws {
6262
self.baseKey = try PrivateKeyImplementation(dataRepresentation: data, format: format)
6363
}
6464

@@ -135,7 +135,7 @@ public extension secp256k1 {
135135
}
136136

137137
/// The key format representation of the public key.
138-
public var format: secp256k1.Format {
138+
public var format: P256K.Format {
139139
baseKey.format
140140
}
141141

@@ -170,7 +170,7 @@ public extension secp256k1 {
170170
/// - Parameter data: A data representation of the key.
171171
/// - Parameter format: The key format.
172172
/// - Throws: An error if the data representation does not create a public key.
173-
public init<D: ContiguousBytes>(dataRepresentation data: D, format: secp256k1.Format) throws {
173+
public init<D: ContiguousBytes>(dataRepresentation data: D, format: P256K.Format) throws {
174174
self.baseKey = try PublicKeyImplementation(dataRepresentation: data, format: format)
175175
}
176176

@@ -205,7 +205,7 @@ public extension secp256k1 {
205205
let length = x963Representation.withUnsafeBytes { $0.count }
206206

207207
switch length {
208-
case (2 * secp256k1.ByteLength.dimension) + 1:
208+
case (2 * P256K.ByteLength.dimension) + 1:
209209
self.baseKey = try PublicKeyImplementation(dataRepresentation: x963Representation, format: .uncompressed)
210210

211211
default:

Sources/ZKP/Combine.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ import Foundation
1616
@_implementationOnly import libsecp256k1
1717
#endif
1818

19-
public extension secp256k1.Signing.PublicKey {
19+
public extension P256K.Signing.PublicKey {
2020
/// Create a new `PublicKey` by combining the current public key with an array of public keys.
2121
/// - Parameters:
2222
/// - pubkeys: the array of public key objects to be combined with
2323
/// - format: the format of the combined `PublicKey` object
2424
/// - Returns: combined `PublicKey` object
25-
func combine(_ pubkeys: [Self], format: secp256k1.Format = .compressed) throws -> Self {
26-
let context = secp256k1.Context.rawRepresentation
25+
func combine(_ pubkeys: [Self], format: P256K.Format = .compressed) throws -> Self {
26+
let context = P256K.Context.rawRepresentation
2727
let allPubKeys = [self] + pubkeys
2828
var pubKeyLen = format.length
2929
var combinedKey = secp256k1_pubkey()

Sources/ZKP/Context.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
/// A constructed context can safely be used from multiple threads simultaneously, but API calls that take a non-const
2424
/// pointer to a context need exclusive access to it. In particular this is the case for `secp256k1_context_destroy`,
2525
/// `secp256k1_context_preallocated_destroy`, and `secp256k1_context_randomize`.
26-
public extension secp256k1 {
26+
public extension P256K {
2727
/// A structure that represents the context flags for `secp256k1` operations.
2828
///
2929
/// This structure conforms to the `OptionSet` protocol, allowing you to combine different context flags.
@@ -34,7 +34,7 @@ public extension secp256k1 {
3434
/// memory for the context.
3535
struct Context: OptionSet {
3636
/// The raw representation of `secp256k1.Context`
37-
public static let rawRepresentation = try! secp256k1.Context.create()
37+
public static let rawRepresentation = try! P256K.Context.create()
3838

3939
/// The raw value of the context flags.
4040
public let rawValue: UInt32
@@ -63,7 +63,7 @@ public extension secp256k1 {
6363
/// the `Context` structure. The method throws an error if the context creation or randomization fails. If the
6464
/// context creation is successful, the method returns an opaque pointer to the created context.
6565
public static func create(_ context: Self = .none) throws -> OpaquePointer {
66-
var randomBytes = SecureBytes(count: secp256k1.ByteLength.privateKey).bytes
66+
var randomBytes = SecureBytes(count: P256K.ByteLength.privateKey).bytes
6767
guard let context = secp256k1_context_create(context.rawValue),
6868
secp256k1_context_randomize(context, &randomBytes).boolValue else {
6969
throw secp256k1Error.underlyingCryptoError

Sources/ZKP/DH.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public struct SharedSecret: ContiguousBytes {
5353
var ss: SecureBytes
5454

5555
// An enum that represents the format of the shared secret
56-
let format: secp256k1.Format
56+
let format: P256K.Format
5757

5858
public func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
5959
try ss.withUnsafeBytes(body)

0 commit comments

Comments
 (0)