Skip to content

Commit accbad9

Browse files
authored
Added Schnorr (#28)
* Updating submodule * Added schnorr
1 parent 449d609 commit accbad9

File tree

8 files changed

+55
-53
lines changed

8 files changed

+55
-53
lines changed

Package.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@ let package = Package(
2626
"secp256k1/src/bench_ecmult.c",
2727
"secp256k1/src/bench_internal.c",
2828
"secp256k1/src/bench_recover.c",
29+
"secp256k1/src/bench_schnorrsig.c",
2930
"secp256k1/src/bench_sign.c",
3031
"secp256k1/src/bench_verify.c",
3132
"secp256k1/src/gen_context.c",
33+
"secp256k1/src/modules/extrakeys/tests_impl.h",
34+
"secp256k1/src/modules/schnorrsig/tests_impl.h",
3235
"secp256k1/src/tests_exhaustive.c",
3336
"secp256k1/src/tests.c",
3437
"secp256k1/src/valgrind_ctime_test.c"
@@ -37,12 +40,16 @@ let package = Package(
3740
.headerSearchPath("secp256k1"),
3841
// Basic config values that are universal and require no dependencies.
3942
// https://github.com/bitcoin-core/secp256k1/blob/master/src/basic-config.h#L27-L31
43+
.define("ECMULT_WINDOW_SIZE", to: "15", nil),
44+
.define("ECMULT_GEN_PREC_BITS", to: "4", nil),
45+
.define("SECP256K1_EXTRAKEYS_H"),
46+
.define("SECP256K1_SCHNORRSIG_H"),
47+
.define("_SECP256K1_MODULE_EXTRAKEYS_MAIN_"),
48+
.define("_SECP256K1_MODULE_SCHNORRSIG_MAIN_"),
4049
.define("USE_NUM_NONE"),
4150
.define("USE_FIELD_INV_BUILTIN"),
4251
.define("USE_SCALAR_INV_BUILTIN"),
43-
.define("USE_WIDEMUL_64"),
44-
.define("ECMULT_WINDOW_SIZE", to: "15", nil),
45-
.define("ECMULT_GEN_PREC_BITS", to: "4", nil)
52+
.define("USE_WIDEMUL_64")
4653
]
4754
),
4855
// Only include select utility extensions because most of Swift Crypto is not required
@@ -54,8 +61,7 @@ let package = Package(
5461
],
5562
sources: [
5663
"swift-crypto/Tests/CryptoTests/Utils/BytesUtil.swift",
57-
"extensions/Array.swift",
58-
"extensions/Data.swift"
64+
"extensions/String.swift"
5965
]
6066
),
6167
.testTarget(
@@ -66,6 +72,6 @@ let package = Package(
6672
]
6773
)
6874
],
69-
swiftLanguageVersions: [.v4, .v5],
75+
swiftLanguageVersions: [.v5],
7076
cLanguageStandard: .c89
7177
)

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ In your `Package.swift`:
1212

1313
```swift
1414
dependencies: [
15-
.package(name: "secp256k1", url: "https://github.com/GigaBitcoin/secp256k1.swift.git", from: "0.0.14"),
15+
.package(
16+
name: "secp256k1",
17+
url: "https://github.com/GigaBitcoin/secp256k1.swift.git",
18+
from: "0.1.0"
19+
),
1620
]
1721
```
1822

@@ -31,7 +35,7 @@ let context = secp256k1_context_create(UInt32(SECP256K1_CONTEXT_SIGN | SECP256K1
3135
var pubkeyLen = 33
3236
var cPubkey = secp256k1_pubkey()
3337
var pubkey = [UInt8](repeating: 0, count: pubkeyLen)
34-
let privkey = try! Array(hex: "14e4a74438858920d8a35fb2d88677580b6a2ee9be4e711ae34ec6b396d87b5c")
38+
let privkey = try! "14E4A74438858920D8A35FB2D88677580B6A2EE9BE4E711AE34EC6B396D87B5C".byteArray()
3539

3640
// Verify the context and keys are setup correctly
3741
guard secp256k1_context_randomize(context, privkey) == 1,
@@ -42,7 +46,7 @@ guard secp256k1_context_randomize(context, privkey) == 1,
4246
return
4347
}
4448

45-
print(pubkey.hex) // 02734b3511150a60fc8cac329cd5ff804555728740f2f2e98bc4242135ef5d5e4e
49+
print(String(byteArray: pubKey)) // 02734b3511150a60fc8cac329cd5ff804555728740f2f2e98bc4242135ef5d5e4e
4650

4751
// Destory context after creation
4852
secp256k1_context_destroy(context)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#include "../secp256k1/include/secp256k1_ecdh.h"
2+
#include "../secp256k1/include/secp256k1_extrakeys.h"
23
#include "../secp256k1/include/secp256k1_preallocated.h"
34
#include "../secp256k1/include/secp256k1_recovery.h"
5+
#include "../secp256k1/include/secp256k1_schnorrsig.h"
46
#include "../secp256k1/include/secp256k1.h"

Sources/swift-crypto/extensions/Array.swift

Lines changed: 0 additions & 20 deletions
This file was deleted.

Sources/swift-crypto/extensions/Data.swift

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// String.swift
3+
// GigaBitcoin/secp256k1.swift
4+
//
5+
// Copyright (c) 2020 GigaBitcoin LLC
6+
// Distributed under the MIT software license
7+
//
8+
// See the accompanying file LICENSE for information
9+
//
10+
11+
import Foundation
12+
13+
public extension String {
14+
/// Public initializer backed by the `BytesUtil.swift` DataProtocol extension property `hexString`
15+
/// - Parameter byteArray: byte array to initialize
16+
init<T: DataProtocol>(byteArray: T) {
17+
self.init()
18+
self = byteArray.hexString
19+
}
20+
21+
/// Public convenience function backed by the `BytesUtil.swift` Array extension initializer
22+
/// - Throws: `ByteHexEncodingErrors` for invalid string or hex value
23+
/// - Returns: initialized byte array
24+
func byteArray() throws -> [UInt8] {
25+
// The `BytesUtil.swift` Array extension expects lowercase strings
26+
try Array(hexString: self.lowercased())
27+
}
28+
}

Tests/secp256k1Tests/secp256k1Tests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ final class secp256k1Tests: XCTestCase {
1616
var cPubkey = secp256k1_pubkey()
1717
var publicKey = [UInt8](repeating: 0, count: pubkeyLen)
1818

19-
let privateKey = try! Array(hex: "14E4A74438858920D8A35FB2D88677580B6A2EE9BE4E711AE34EC6B396D87B5C")
19+
let privateKey = try! "14E4A74438858920D8A35FB2D88677580B6A2EE9BE4E711AE34EC6B396D87B5C".byteArray()
2020

2121
// Verify the context and keys are setup correctly
2222
XCTAssertEqual(secp256k1_context_randomize(context, privateKey), 1)
@@ -28,11 +28,11 @@ final class secp256k1Tests: XCTestCase {
2828
"""
2929

3030
// Define the expected public key
31-
let expectedPublicKey = try! Array(hex: hexString)
31+
let expectedPublicKey = try! hexString.byteArray()
3232

3333
// Verify the generated public key matches the expected public key
3434
XCTAssertEqual(expectedPublicKey, publicKey)
35-
XCTAssertEqual(hexString.lowercased(), publicKey.hex)
35+
XCTAssertEqual(hexString.lowercased(), String(byteArray: publicKey))
3636
}
3737

3838
/// Compressed Keypair test
@@ -48,15 +48,15 @@ final class secp256k1Tests: XCTestCase {
4848
var cPubkey = secp256k1_pubkey()
4949
var publicKey = [UInt8](repeating: 0, count: pubkeyLen)
5050

51-
let privateKey = try! Array(hex: "B035FCFC6ABF660856C5F3A6F9AC51FCA897BB4E76AD9ACA3EFD40DA6B9C864B")
51+
let privateKey = try! "B035FCFC6ABF660856C5F3A6F9AC51FCA897BB4E76AD9ACA3EFD40DA6B9C864B".byteArray()
5252

5353
// Verify the context and keys are setup correctly
5454
XCTAssertEqual(secp256k1_context_randomize(context, privateKey), 1)
5555
XCTAssertEqual(secp256k1_ec_pubkey_create(context, &cPubkey, privateKey), 1)
5656
XCTAssertEqual(secp256k1_ec_pubkey_serialize(context, &publicKey, &pubkeyLen, &cPubkey, UInt32(SECP256K1_EC_COMPRESSED)), 1)
5757

5858
// Define the expected public key
59-
let expectedPublicKey = try! Array(hex: "02EA724B70B48B61FB87E4310871A48C65BF38BF3FDFEFE73C2B90F8F32F9C1794")
59+
let expectedPublicKey = try! "02EA724B70B48B61FB87E4310871A48C65BF38BF3FDFEFE73C2B90F8F32F9C1794".byteArray()
6060

6161
// Verify the generated public key matches the expected public key
6262
XCTAssertEqual(expectedPublicKey, publicKey)

0 commit comments

Comments
 (0)