Skip to content

Commit 326e73b

Browse files
authored
Adding utility module (#16)
1 parent 2a6af20 commit 326e73b

File tree

5 files changed

+76
-33
lines changed

5 files changed

+76
-33
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
[submodule "Sources/secp256k1/secp256k1"]
22
path = Sources/secp256k1/secp256k1
33
url = https://github.com/bitcoin-core/secp256k1
4+
[submodule "Sources/swift-crypto/swift-crypto"]
5+
path = Sources/swift-crypto/swift-crypto
6+
url = https://github.com/apple/swift-crypto

Package.swift

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ let package = Package(
1010
// WARNING: These APIs should not be considered stable and may change at any time.
1111
.library(
1212
name: "secp256k1",
13-
targets: ["secp256k1"]
13+
targets: [
14+
"secp256k1",
15+
"secp256k1_utils"
16+
]
1417
)
1518
],
1619
targets: [
@@ -42,9 +45,23 @@ let package = Package(
4245
.define("ECMULT_GEN_PREC_BITS", to: "4", nil)
4346
]
4447
),
48+
// Only include select utility extensions because most of Swift Crypto is not required
49+
.target(
50+
name: "secp256k1_utils",
51+
path: "Sources/swift-crypto",
52+
exclude: [
53+
"swift-crypto/Sources",
54+
],
55+
sources: [
56+
"swift-crypto/Tests/CryptoTests/Utils/BytesUtil.swift"
57+
]
58+
),
4559
.testTarget(
4660
name: "secp256k1Tests",
47-
dependencies: ["secp256k1"]
61+
dependencies: [
62+
"secp256k1",
63+
"secp256k1_utils"
64+
]
4865
)
4966
],
5067
swiftLanguageVersions: [.v5],

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
# 🔐 secp256k1.swift [![Build Status](https://app.bitrise.io/app/ef44aebd8443b33b/status.svg?token=oDGzN3bMEwseXF_5MQUsTg&branch=main)](https://app.bitrise.io/app/ef44aebd8443b33b)
2-
Swift bindings library for ECDSA signatures and secret/public key operations using the [libsecp256k1](https://github.com/bitcoin-core/secp256k1) C library.
2+
Swift bindings library for ECDSA signatures and secret/public key operations using [libsecp256k1](https://github.com/bitcoin-core/secp256k1).
33

44
# Objective
55
This library aims to be a lightweight dependency for clients and wrapper libraries to include ECDSA functionality.
66

7-
This package targets the default git branch of secp256k1 and aims to stay up-to-date without using a mirrored repository.
7+
This package is set to the default git branch of secp256k1 and aims to stay up-to-date without using a mirrored repository. An extra module is available for convenience functionality.
88

99
# Getting Started
1010

1111
In your `Package.swift`:
1212

1313
```swift
1414
dependencies: [
15-
.package(name: "secp256k1", url: "https://github.com/GigaBitcoin/secp256k1.swift.git", from: "0.0.1"),
15+
.package(name: "secp256k1", url: "https://github.com/GigaBitcoin/secp256k1.swift.git", from: "0.0.11"),
1616
]
1717
```
1818

@@ -22,26 +22,27 @@ Currently, this Swift package only provides a single product library built using
2222

2323
```swift
2424
import secp256k1
25+
import secp256k1_utils
2526

2627
// Initialize context
2728
let context = secp256k1_context_create(UInt32(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY))!
2829

2930
// Setup private and public key variables
30-
var pubkeyLen = 65
31+
var pubkeyLen = 33
3132
var cPubkey = secp256k1_pubkey()
3233
var pubkey = [UInt8](repeating: 0, count: pubkeyLen)
33-
let privkey: [UInt8] = [0,1,0,0,1,0,1,0,1,0,1,0,1,0,0,1,1,1,0,0,1,0,0,1,1,0,0,1,0,0,32,0]
34+
let privkey = try! Array(hexString: "14e4a74438858920d8a35fb2d88677580b6a2ee9be4e711ae34ec6b396d87b5c")
3435

3536
// Verify the context and keys are setup correctly
3637
guard secp256k1_context_randomize(context, privkey) == 1,
3738
secp256k1_ec_pubkey_create(context, &cPubkey, privkey) == 1,
38-
secp256k1_ec_pubkey_serialize(context, &pubkey, &pubkeyLen, &cPubkey, UInt32(SECP256K1_EC_UNCOMPRESSED)) == 1 else {
39+
secp256k1_ec_pubkey_serialize(context, &pubkey, &pubkeyLen, &cPubkey, UInt32(SECP256K1_EC_COMPRESSED)) == 1 else {
3940
// Destory context after creation
4041
secp256k1_context_destroy(context)
4142
return
4243
}
4344

44-
print(pubkey) // [4,96,104, 212, 128, 165, 213, 207, 134, 132, 22, 247, 38, 114, 82, 108, 77, 43, 6, 56, ... ]
45+
print(pubkey.hexString) // 02734b3511150a60fc8cac329cd5ff804555728740f2f2e98bc4242135ef5d5e4e
4546

4647
// Destory context after creation
4748
secp256k1_context_destroy(context)

Sources/swift-crypto/swift-crypto

Submodule swift-crypto added at afe4c9b
Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,68 @@
11
import XCTest
22
@testable import secp256k1
3+
@testable import secp256k1_utils
34

45
final class secp256k1Tests: XCTestCase {
5-
/// Basic Keypair test
6-
func testKeypairCreation() {
6+
/// Uncompressed Keypair test
7+
func testUncompressedKeypairCreation() {
78
// Initialize context
89
let context = secp256k1_context_create(UInt32(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY))!
910

1011
// Destory context after execution
11-
defer {
12-
secp256k1_context_destroy(context)
13-
}
12+
defer { secp256k1_context_destroy(context) }
1413

1514
// Setup private and public key variables
1615
var pubkeyLen = 65
1716
var cPubkey = secp256k1_pubkey()
18-
var pubkey = [UInt8](repeating: 0, count: pubkeyLen)
19-
let privkey: [UInt8] = [0,1,0,0,1,0,1,0,1,0,1,0,1,0,0,1,1,1,0,0,1,0,0,1,1,0,0,1,0,0,32,0]
17+
var publicKey = [UInt8](repeating: 0, count: pubkeyLen)
18+
19+
let privateKey = try! Array(hexString: "14E4A74438858920D8A35FB2D88677580B6A2EE9BE4E711AE34EC6B396D87B5C".lowercased())
20+
21+
// Verify the context and keys are setup correctly
22+
XCTAssertEqual(secp256k1_context_randomize(context, privateKey), 1)
23+
XCTAssertEqual(secp256k1_ec_pubkey_create(context, &cPubkey, privateKey), 1)
24+
XCTAssertEqual(secp256k1_ec_pubkey_serialize(context, &publicKey, &pubkeyLen, &cPubkey, UInt32(SECP256K1_EC_UNCOMPRESSED)), 1)
2025

26+
let hexString = """
27+
04734B3511150A60FC8CAC329CD5FF804555728740F2F2E98BC4242135EF5D5E4E6C4918116B0866F50C46614F3015D8667FBFB058471D662A642B8EA2C9C78E8A
28+
"""
29+
30+
// Define the expected public key
31+
let expectedPublicKey = try! Array(hexString: hexString.lowercased())
32+
33+
// Verify the generated public key matches the expected public key
34+
XCTAssertEqual(expectedPublicKey, publicKey)
35+
}
36+
37+
/// Compressed Keypair test
38+
func testCompressedKeypairCreation() {
39+
// Initialize context
40+
let context = secp256k1_context_create(UInt32(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY))!
41+
42+
// Destory context after execution
43+
defer { secp256k1_context_destroy(context) }
44+
45+
// Setup private and public key variables
46+
var pubkeyLen = 33
47+
var cPubkey = secp256k1_pubkey()
48+
var publicKey = [UInt8](repeating: 0, count: pubkeyLen)
49+
50+
let privateKey = try! Array(hexString: "B035FCFC6ABF660856C5F3A6F9AC51FCA897BB4E76AD9ACA3EFD40DA6B9C864B".lowercased())
51+
2152
// Verify the context and keys are setup correctly
22-
XCTAssertEqual(secp256k1_context_randomize(context, privkey), 1)
23-
XCTAssertEqual(secp256k1_ec_pubkey_create(context, &cPubkey, privkey), 1)
24-
XCTAssertEqual(secp256k1_ec_pubkey_serialize(context, &pubkey, &pubkeyLen, &cPubkey, UInt32(SECP256K1_EC_UNCOMPRESSED)), 1)
53+
XCTAssertEqual(secp256k1_context_randomize(context, privateKey), 1)
54+
XCTAssertEqual(secp256k1_ec_pubkey_create(context, &cPubkey, privateKey), 1)
55+
XCTAssertEqual(secp256k1_ec_pubkey_serialize(context, &publicKey, &pubkeyLen, &cPubkey, UInt32(SECP256K1_EC_COMPRESSED)), 1)
2556

2657
// Define the expected public key
27-
let expectedPublicKey: [UInt8] = [
28-
4,96,104, 212, 128, 165, 213,
29-
207, 134, 132, 22, 247, 38,
30-
114, 82, 108, 77, 43, 6, 56,
31-
80, 113, 12, 11, 119, 7, 240,
32-
188, 73, 170, 44, 202, 33, 225,
33-
30, 248, 53, 138, 34, 22, 100,
34-
96, 31, 76, 64, 125, 71, 127,
35-
62, 155, 108, 243, 17, 222, 97,
36-
234, 75, 247, 187, 83, 151, 206,
37-
27, 38, 228
38-
]
58+
let expectedPublicKey = try! Array(hexString: "02EA724B70B48B61FB87E4310871A48C65BF38BF3FDFEFE73C2B90F8F32F9C1794".lowercased())
3959

4060
// Verify the generated public key matches the expected public key
41-
XCTAssertEqual(expectedPublicKey, pubkey)
61+
XCTAssertEqual(expectedPublicKey, publicKey)
4262
}
4363

4464
static var allTests = [
45-
("testKeypairCreation", testKeypairCreation),
65+
("testUncompressedKeypairCreation", testUncompressedKeypairCreation),
66+
("testCompressedKeypairCreation", testCompressedKeypairCreation),
4667
]
4768
}

0 commit comments

Comments
 (0)