Skip to content

Commit 53c5609

Browse files
authored
Generate keys with GYB (#22)
1 parent 94bc63e commit 53c5609

18 files changed

+1889
-141
lines changed

.swiftformat

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313
--header strip
1414

1515
# file options
16-
--exclude .build,Sources/secp256k1,Sources/K1/Support/ThirdyParty
16+
--exclude .build,Sources/secp256k1,Sources/K1/Support/ThirdyParty,**/*.swift.gyb

Package.swift

+3
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ let package = Package(
7171
dependencies: [
7272
"secp256k1",
7373
],
74+
exclude: [
75+
"K1/Keys/Keys.swift.gyb",
76+
],
7477
swiftSettings: [
7578
.define("CRYPTO_IN_SWIFTPM_FORCE_BUILD_API"),
7679
]

README.md

+24
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,30 @@ Stand in root and run
250250

251251
To clone the dependency [libsecp256k1][lib], using commit [427bc3cdcfbc74778070494daab1ae5108c71368](https://github.com/bitcoin-core/secp256k1/commit/427bc3cdcfbc74778070494daab1ae5108c71368) (semver 0.3.0)
252252

253+
254+
## `gyb`
255+
256+
Some of the files in this project are autogenerated (metaprogramming) using the Swift Utils tools called [gyb](https://github.com/apple/swift/blob/main/utils/gyb.py) (_"generate your boilerplate"_). `gyb` is included in [`./scripts/gyb`](scripts/gyb).
257+
258+
`gyb` will generate some `Foobar.swift` Swift file from some `Foobar.swift.gyb` _template_ file. **You should not edit `Foobar.swift` directly**, since all manual edits in that generated file will be overwritten the next time `gyb` is run.
259+
260+
You run `gyb` for a single file like so:
261+
262+
```bash
263+
./scripts/gyb --line-directive "" Sources/Foobar.swift.gyb -o Sources/Foobar.swift
264+
```
265+
266+
More conveniently you can run the bash script `./scripts/generate_boilerplate_files_with_gyb.sh` to generate all Swift files from their corresponding gyb template.
267+
268+
**If you add a new `.gyb` file, you should append a `// MARK: - Generated file, do NOT edit` warning** inside it, e.g.
269+
270+
```swift
271+
// MARK: - Generated file, do NOT edit
272+
// any edits of this file WILL be overwritten and thus discarded
273+
// see section `gyb` in `README` for details.
274+
```
275+
276+
253277
# Alternatives
254278

255279
- [GigaBitcoin/secp256k1.swift](https://github.com/GigaBitcoin/secp256k1.swift) (also using `libsecp256k1`, ⚠️ possibly unsafe, ✅ Schnorr support)

Sources/K1/K1/ECDH/KeyAgreement.swift

+2-11
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,11 @@
11
import struct CryptoKit.SharedSecret
22
import Foundation
33

4-
// MARK: - K1Feature
5-
public protocol K1Feature {
6-
associatedtype PublicKey: K1PublicKeyProtocol
7-
}
8-
94
// MARK: - K1.KeyAgreement
105
extension K1 {
116
/// A mechanism used to create a shared secret between two users by performing `secp256k1` elliptic curve Diffie Hellman (ECDH) key exchange.
12-
public enum KeyAgreement: K1Feature {
13-
/// A `secp256k1` private key used for key agreement.
14-
public typealias PrivateKey = PrivateKeyOf<Self>
15-
16-
/// A `secp256k1` public key used for key agreement.
17-
public typealias PublicKey = PublicKeyOf<Self>
7+
public enum KeyAgreement {
8+
// Just a namespace
189
}
1910
}
2011

Sources/K1/K1/ECDSA/ECDSASignatureNonRecoverable.swift

+4-8
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,15 @@ import Foundation
55
// MARK: - K1.ECDSA.NonRecoverable
66
extension K1.ECDSA {
77
/// A mechanism used to create or verify a cryptographic signature using the `secp256k1` elliptic curve digital signature algorithm (ECDSA), signatures that do not offer recovery of the public key.
8-
public enum NonRecoverable: K1Feature {
9-
/// A `secp256k1` private key used to create cryptographic signatures,
10-
/// more specifically ECDSA signatures, that do not offer recovery of the public key.
11-
public typealias PrivateKey = PrivateKeyOf<Self>
12-
13-
/// A `secp256k1` public key used to verify cryptographic signatures,
14-
/// more specifically ECDSA signatures, that do not offer recovery of the public key.
15-
public typealias PublicKey = PublicKeyOf<Self>
8+
public enum NonRecoverable {
9+
// Just a namespace
1610
}
1711
}
1812

1913
// MARK: - K1.ECDSA.NonRecoverable.Signature
2014
extension K1.ECDSA.NonRecoverable {
15+
/// A `secp256k1` elliptic curve digital signature algorithm (ECDSA) signature,
16+
/// from which users can recover a public key with the message that was signed.
2117
public struct Signature: Sendable, Hashable, ContiguousBytes {
2218
typealias Wrapped = FFI.ECDSA.NonRecovery.Wrapped
2319
internal let wrapped: Wrapped

Sources/K1/K1/ECDSA/ECDSASignatureRecoverable.swift

+4-8
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,8 @@ import Foundation
55
// MARK: - K1.ECDSA.Recoverable
66
extension K1.ECDSA {
77
/// A mechanism used to create or verify a cryptographic signature using the `secp256k1` elliptic curve digital signature algorithm (ECDSA), signatures that do offers recovery of the public key.
8-
public enum Recoverable: K1Feature {
9-
/// A `secp256k1` private key used to create cryptographic signatures,
10-
/// more specifically ECDSA signatures that offers recovery of the public key.
11-
public typealias PrivateKey = PrivateKeyOf<Self>
12-
13-
/// A `secp256k1` public key used to verify cryptographic signatures.
14-
/// more specifically ECDSA signatures that offers recovery of the public key.
15-
public typealias PublicKey = PublicKeyOf<Self>
8+
public enum Recoverable {
9+
// Just a namespace
1610
}
1711
}
1812

@@ -137,6 +131,8 @@ extension K1.ECDSA.Recoverable.PublicKey {
137131

138132
// MARK: - K1.ECDSA.Recoverable.Signature
139133
extension K1.ECDSA.Recoverable {
134+
/// A `secp256k1` elliptic curve digital signature algorithm (ECDSA) signature,
135+
/// from which users **cannot** recover the public key, not without the `RecoveryID`.
140136
public struct Signature: Sendable, Hashable, ContiguousBytes {
141137
typealias Wrapped = FFI.ECDSA.Recovery.Wrapped
142138
private let wrapped: Wrapped

0 commit comments

Comments
 (0)