Skip to content

Commit c61f152

Browse files
authored
Fixed bindings and added recovery module (#165)
1 parent 75ff85a commit c61f152

File tree

2 files changed

+83
-7
lines changed

2 files changed

+83
-7
lines changed

Package.swift

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,13 @@ let package = Package(
4444
.headerSearchPath("secp256k1"),
4545
// Basic config values that are universal and require no dependencies.
4646
// https://github.com/bitcoin-core/secp256k1/blob/master/src/basic-config.h#L12-L13
47-
.define("ECMULT_WINDOW_SIZE", to: "15"),
4847
.define("ECMULT_GEN_PREC_BITS", to: "4"),
48+
.define("ECMULT_WINDOW_SIZE", to: "15"),
4949
// Enabling additional secp256k1 modules.
50-
.define("SECP256K1_ECDH_H"),
51-
.define("SECP256K1_MODULE_ECDH_MAIN_H"),
52-
.define("SECP256K1_EXTRAKEYS_H"),
53-
.define("SECP256K1_MODULE_EXTRAKEYS_MAIN_H"),
54-
.define("SECP256K1_SCHNORRSIG_H"),
55-
.define("SECP256K1_MODULE_SCHNORRSIG_MAIN_H"),
50+
.define("ENABLE_MODULE_ECDH"),
51+
.define("ENABLE_MODULE_EXTRAKEYS"),
52+
.define("ENABLE_MODULE_RECOVERY"),
53+
.define("ENABLE_MODULE_SCHNORRSIG"),
5654
]
5755
),
5856
// Only include select utility extensions because most of Swift Crypto is not required

Tests/secp256k1Tests/secp256k1Tests.swift

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,80 @@ final class secp256k1Tests: XCTestCase {
5959
// Verify the generated public key matches the expected public key
6060
XCTAssertEqual(expectedPublicKey, publicKey)
6161
}
62+
63+
func testECDHBindings() {
64+
// Initialize context
65+
let context = secp256k1_context_create(UInt32(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY))!
66+
67+
// Destroy context after execution
68+
defer { secp256k1_context_destroy(context) }
69+
70+
var point = secp256k1_pubkey()
71+
var res = [UInt8](repeating: 0, count: 32)
72+
var s_one = [UInt8](repeating: 0, count: 32)
73+
74+
s_one[31] = 1;
75+
76+
XCTAssertEqual(secp256k1_ec_pubkey_create(context, &point, s_one), 1)
77+
XCTAssertEqual(secp256k1_ecdh(context, &res, &point, s_one, nil, nil), 1)
78+
}
79+
80+
func testExtraKeysBindings() {
81+
// Initialize context
82+
let context = secp256k1_context_create(UInt32(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY))!
83+
84+
// Destroy context after execution
85+
defer { secp256k1_context_destroy(context) }
86+
87+
var pubKey = secp256k1_pubkey()
88+
var xOnlyPubKey = secp256k1_xonly_pubkey()
89+
var pk_parity = Int32()
90+
91+
let privateKey = try! "14E4A74438858920D8A35FB2D88677580B6A2EE9BE4E711AE34EC6B396D87B5C".byteArray()
92+
93+
XCTAssertEqual(secp256k1_ec_pubkey_create(context, &pubKey, privateKey), 1)
94+
XCTAssertEqual(secp256k1_xonly_pubkey_from_pubkey(context, &xOnlyPubKey, &pk_parity, &pubKey), 1)
95+
}
96+
97+
func testRecoveryBindings() {
98+
// Initialize context
99+
let context = secp256k1_context_create(UInt32(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY))!
100+
101+
// Destroy context after execution
102+
defer { secp256k1_context_destroy(context) }
103+
104+
var pubKey = secp256k1_pubkey()
105+
var recsig = secp256k1_ecdsa_recoverable_signature()
106+
var message = [UInt8](repeating: 0, count: 32)
107+
108+
let privateKey = try! "14E4A74438858920D8A35FB2D88677580B6A2EE9BE4E711AE34EC6B396D87B5C".byteArray()
109+
110+
XCTAssertEqual(secp256k1_ec_seckey_verify(context, privateKey), 1)
111+
XCTAssertEqual(secp256k1_ec_pubkey_create(context, &pubKey, privateKey), 1)
112+
XCTAssertEqual(secp256k1_ecdsa_sign_recoverable(context, &recsig, &message, privateKey, nil, nil), 1)
113+
}
114+
115+
func testSchnorrBindings() {
116+
// Initialize context
117+
let context = secp256k1_context_create(UInt32(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY))!
118+
119+
// Destroy context after execution
120+
defer { secp256k1_context_destroy(context) }
121+
122+
var keypair = secp256k1_keypair()
123+
var xpubKey = secp256k1_xonly_pubkey()
124+
var xpubKeyBytes = [UInt8](repeating: 0, count: 32)
125+
126+
let privateKey = try! "14E4A74438858920D8A35FB2D88677580B6A2EE9BE4E711AE34EC6B396D87B5C".byteArray()
127+
128+
XCTAssertEqual(secp256k1_keypair_create(context, &keypair, privateKey), 1)
129+
XCTAssertEqual(secp256k1_keypair_xonly_pub(context, &xpubKey, nil, &keypair), 1)
130+
XCTAssertEqual(secp256k1_xonly_pubkey_serialize(context, &xpubKeyBytes, &xpubKey), 1)
131+
132+
let expectedXPubKey = "734b3511150a60fc8cac329cd5ff804555728740f2f2e98bc4242135ef5d5e4e"
133+
134+
XCTAssertEqual(String(byteArray: xpubKeyBytes), expectedXPubKey)
135+
}
62136

63137
/// Compressed Key pair test
64138
func testCompressedKeypairImplementationWithRaw() {
@@ -219,6 +293,10 @@ final class secp256k1Tests: XCTestCase {
219293
static var allTests = [
220294
("testUncompressedKeypairCreation", testUncompressedKeypairCreation),
221295
("testCompressedKeypairCreation", testCompressedKeypairCreation),
296+
("testECDHBindings", testECDHBindings),
297+
("testExtraKeysBindings", testExtraKeysBindings),
298+
("testRecoveryBindings", testRecoveryBindings),
299+
("testSchnorrBindings", testSchnorrBindings),
222300
("testCompressedKeypairImplementationWithRaw", testCompressedKeypairImplementationWithRaw),
223301
("testSha256", testSha256),
224302
("testSigning", testSigning),

0 commit comments

Comments
 (0)