Skip to content

Commit

Permalink
Generate keys with GYB (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sajjon committed Mar 25, 2023
1 parent 94bc63e commit 53c5609
Show file tree
Hide file tree
Showing 18 changed files with 1,889 additions and 141 deletions.
2 changes: 1 addition & 1 deletion .swiftformat
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
--header strip

# file options
--exclude .build,Sources/secp256k1,Sources/K1/Support/ThirdyParty
--exclude .build,Sources/secp256k1,Sources/K1/Support/ThirdyParty,**/*.swift.gyb
3 changes: 3 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ let package = Package(
dependencies: [
"secp256k1",
],
exclude: [
"K1/Keys/Keys.swift.gyb",
],
swiftSettings: [
.define("CRYPTO_IN_SWIFTPM_FORCE_BUILD_API"),
]
Expand Down
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,30 @@ Stand in root and run

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


## `gyb`

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

`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.

You run `gyb` for a single file like so:

```bash
./scripts/gyb --line-directive "" Sources/Foobar.swift.gyb -o Sources/Foobar.swift
```

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.

**If you add a new `.gyb` file, you should append a `// MARK: - Generated file, do NOT edit` warning** inside it, e.g.

```swift
// MARK: - Generated file, do NOT edit
// any edits of this file WILL be overwritten and thus discarded
// see section `gyb` in `README` for details.
```


# Alternatives

- [GigaBitcoin/secp256k1.swift](https://github.com/GigaBitcoin/secp256k1.swift) (also using `libsecp256k1`, ⚠️ possibly unsafe, ✅ Schnorr support)
Expand Down
13 changes: 2 additions & 11 deletions Sources/K1/K1/ECDH/KeyAgreement.swift
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import struct CryptoKit.SharedSecret
import Foundation

// MARK: - K1Feature
public protocol K1Feature {
associatedtype PublicKey: K1PublicKeyProtocol
}

// MARK: - K1.KeyAgreement
extension K1 {
/// A mechanism used to create a shared secret between two users by performing `secp256k1` elliptic curve Diffie Hellman (ECDH) key exchange.
public enum KeyAgreement: K1Feature {
/// A `secp256k1` private key used for key agreement.
public typealias PrivateKey = PrivateKeyOf<Self>

/// A `secp256k1` public key used for key agreement.
public typealias PublicKey = PublicKeyOf<Self>
public enum KeyAgreement {
// Just a namespace
}
}

Expand Down
12 changes: 4 additions & 8 deletions Sources/K1/K1/ECDSA/ECDSASignatureNonRecoverable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,15 @@ import Foundation
// MARK: - K1.ECDSA.NonRecoverable
extension K1.ECDSA {
/// 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.
public enum NonRecoverable: K1Feature {
/// A `secp256k1` private key used to create cryptographic signatures,
/// more specifically ECDSA signatures, that do not offer recovery of the public key.
public typealias PrivateKey = PrivateKeyOf<Self>

/// A `secp256k1` public key used to verify cryptographic signatures,
/// more specifically ECDSA signatures, that do not offer recovery of the public key.
public typealias PublicKey = PublicKeyOf<Self>
public enum NonRecoverable {
// Just a namespace
}
}

// MARK: - K1.ECDSA.NonRecoverable.Signature
extension K1.ECDSA.NonRecoverable {
/// A `secp256k1` elliptic curve digital signature algorithm (ECDSA) signature,
/// from which users can recover a public key with the message that was signed.
public struct Signature: Sendable, Hashable, ContiguousBytes {
typealias Wrapped = FFI.ECDSA.NonRecovery.Wrapped
internal let wrapped: Wrapped
Expand Down
12 changes: 4 additions & 8 deletions Sources/K1/K1/ECDSA/ECDSASignatureRecoverable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,8 @@ import Foundation
// MARK: - K1.ECDSA.Recoverable
extension K1.ECDSA {
/// 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.
public enum Recoverable: K1Feature {
/// A `secp256k1` private key used to create cryptographic signatures,
/// more specifically ECDSA signatures that offers recovery of the public key.
public typealias PrivateKey = PrivateKeyOf<Self>

/// A `secp256k1` public key used to verify cryptographic signatures.
/// more specifically ECDSA signatures that offers recovery of the public key.
public typealias PublicKey = PublicKeyOf<Self>
public enum Recoverable {
// Just a namespace
}
}

Expand Down Expand Up @@ -137,6 +131,8 @@ extension K1.ECDSA.Recoverable.PublicKey {

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

0 comments on commit 53c5609

Please sign in to comment.