Skip to content

Commit c77e6c2

Browse files
authored
Upgrade libsecp256k1 to 0.6.0 (0cdc758a56360bf58a851fe91085a327ec97685a) (#44)
1 parent 9faf050 commit c77e6c2

File tree

4 files changed

+30
-17
lines changed

4 files changed

+30
-17
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
66
_K1_ is Swift wrapper around [libsecp256k1 (bitcoin-core/secp256k1)][lib], offering ECDSA, Schnorr ([BIP340][bip340]) and ECDH features.
77

8+
> [!NOTE]
9+
> Current `libsecp256k1` version is [0.6.0 (0cdc758a56360bf58a851fe91085a327ec97685a)](https://github.com/bitcoin-core/secp256k1/commit/0cdc758a56360bf58a851fe91085a327ec97685a)
10+
811
# Documentation
912
Read full documentation [here on SwiftPackageIndex][doc].
1013

@@ -246,15 +249,12 @@ assert(ab.count == 65) // pass
246249

247250
# Development
248251

249-
Stand in root and run
252+
Stand in root and run to setup submodules
250253

251254
```sh
252-
./scripts/bootstrap.sh
255+
make submodules
253256
```
254257

255-
To clone the dependency [libsecp256k1][lib], using commit [427bc3cdcfbc74778070494daab1ae5108c71368](https://github.com/bitcoin-core/secp256k1/commit/427bc3cdcfbc74778070494daab1ae5108c71368) (semver 0.3.0)
256-
257-
258258
## `gyb`
259259

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

Sources/K1/Support/FFI/API/ECDH/FFI+ECDH.swift

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ extension FFI.ECDH {
3030
///
3131
case noHashWholePoint
3232

33-
func hashfp() -> (Optional< @convention(c) (UnsafeMutablePointer<UInt8>?, UnsafePointer<UInt8>?, UnsafePointer<UInt8>?, UnsafeMutableRawPointer?) -> Int32>) {
33+
func hashfp() -> ((@convention(c) (UnsafeMutablePointer<UInt8>?, UnsafePointer<UInt8>?, UnsafePointer<UInt8>?, UnsafeMutableRawPointer?) -> Int32)?) {
3434
switch self {
3535
case .libsecp256kDefault: return secp256k1_ecdh_hash_function_default
3636
case .ansiX963: return ecdh_asn1_x963
@@ -59,7 +59,7 @@ extension FFI.ECDH {
5959
repeating: 0,
6060
count: hashFp.outputByteCount
6161
)
62-
var arbitraryData: [UInt8]? = {
62+
let arbitraryData: [UInt8]? = {
6363
switch hashFp {
6464
case let .libsecp256kDefault(arbitraryData?): return [UInt8](arbitraryData)
6565
case .libsecp256kDefault(.none): return nil
@@ -70,14 +70,27 @@ extension FFI.ECDH {
7070
try FFI.call(
7171
ifFailThrow: .ecdh
7272
) { context in
73-
secp256k1_ecdh(
74-
context,
75-
&sharedPublicPointBytes, // output
76-
&publicKeyRaw, // pubkey
77-
privateKey.secureBytes.backing.bytes, // seckey
78-
hashFp.hashfp(), // hashfp
79-
&arbitraryData // arbitrary data pointer that is passed through to hashfp
80-
)
73+
if var arbitraryData {
74+
arbitraryData.withUnsafeMutableBytes { ptr in
75+
secp256k1_ecdh(
76+
context,
77+
&sharedPublicPointBytes, // output
78+
&publicKeyRaw, // pubkey
79+
privateKey.secureBytes.backing.bytes, // seckey
80+
hashFp.hashfp(), // hashfp
81+
ptr.baseAddress // properly formed pointer
82+
)
83+
}
84+
} else {
85+
secp256k1_ecdh(
86+
context,
87+
&sharedPublicPointBytes,
88+
&publicKeyRaw,
89+
privateKey.secureBytes.backing.bytes,
90+
hashFp.hashfp(),
91+
nil // No arbitrary data
92+
)
93+
}
8194
}
8295

8396
return Data(sharedPublicPointBytes)

Sources/secp256k1/libsecp256k1

Submodule libsecp256k1 updated 105 files

Tests/K1Tests/TestCases/PublicKeyRecovery/ECDASignaturePublicKeyRecoveryTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ struct RecoveryTestVector: Decodable, Equatable {
121121
}
122122

123123
// MARK: - K1.ECDSAWithKeyRecovery.Signature.RecoveryID + ExpressibleByIntegerLiteral
124-
extension K1.ECDSAWithKeyRecovery.Signature.RecoveryID: ExpressibleByIntegerLiteral {
124+
extension K1.ECDSAWithKeyRecovery.Signature.RecoveryID: @retroactive ExpressibleByIntegerLiteral {
125125
public init(integerLiteral value: UInt8) {
126126
self.init(rawValue: value)!
127127
}

0 commit comments

Comments
 (0)