Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/attaswift/BigInt.git", .upToNextMajor(from: "5.0.0")),
.package(url: "https://github.com/horizontalsystems/HsExtensions.Swift.git", .upToNextMajor(from: "1.0.0")),
.package(url: "https://github.com/horizontalsystems/HsExtensions.Swift.git", .upToNextMinor(from: "1.0.0")),
.package(url: "https://github.com/apple/swift-crypto.git", "1.0.0" ..< "3.0.0"),
.package(url: "https://github.com/GigaBitcoin/secp256k1.swift.git", exact: .init(0, 10, 0))
.package(url: "https://github.com/GigaBitcoin/secp256k1.swift.git", exact: .init(0, 12, 2))
],
targets: [
.target(
Expand Down
8 changes: 4 additions & 4 deletions Sources/HsCryptoKit/Crypto.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public enum Crypto {
public static func publicKey(_ publicKey: secp256k1_pubkey, compressed: Bool) -> Data {
var outputLen: Int = compressed ? 33 : 65

let context = secp256k1.Context.raw
let context = secp256k1.Context.rawRepresentation

var publicKey = publicKey
var output = Data(count: outputLen)
Expand All @@ -93,7 +93,7 @@ public enum Crypto {
case .secp256k1:
var pubKeyPoint = secp256k1_pubkey()

let context = secp256k1.Context.raw
let context = secp256k1.Context.rawRepresentation
_ = SecpResult(secp256k1_ec_pubkey_create(context, &pubKeyPoint, privateKey))

return publicKey(pubKeyPoint, compressed: compressed)
Expand All @@ -110,7 +110,7 @@ public enum Crypto {
precondition(data.count > 0, "Data must be non-zero size")
precondition(privateKey.count > 0, "PrivateKey must be non-zero size")

let ctx = secp256k1.Context.raw
let ctx = secp256k1.Context.rawRepresentation

let signature = UnsafeMutablePointer<secp256k1_ecdsa_signature>.allocate(capacity: 1)
let status = data.withUnsafeBytes { ptr in
Expand Down Expand Up @@ -182,7 +182,7 @@ public enum Crypto {
// Combine to points to found new point (new public Key)
var combinedKey = secp256k1_pubkey()
if withUnsafeMutablePointer(to: &combinedKey, { (combinedKeyPtr: UnsafeMutablePointer<secp256k1_pubkey>) -> Int32 in
secp256k1_ec_pubkey_combine(secp256k1.Context.raw, combinedKeyPtr, immutablePointer, 2)
secp256k1_ec_pubkey_combine(secp256k1.Context.rawRepresentation, combinedKeyPtr, immutablePointer, 2)
}) == 0 {
throw SignError.additionError
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/HsCryptoKit/CryptoSwift/BatchedCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ struct BatchedCollectionIndex<Base: Collection> {
}

extension BatchedCollectionIndex: Comparable {
static func == <Base>(lhs: BatchedCollectionIndex<Base>, rhs: BatchedCollectionIndex<Base>) -> Bool {
static func == (lhs: BatchedCollectionIndex<Base>, rhs: BatchedCollectionIndex<Base>) -> Bool {
return lhs.range.lowerBound == rhs.range.lowerBound
}

static func < <Base>(lhs: BatchedCollectionIndex<Base>, rhs: BatchedCollectionIndex<Base>) -> Bool {
static func <(lhs: BatchedCollectionIndex<Base>, rhs: BatchedCollectionIndex<Base>) -> Bool {
return lhs.range.lowerBound < rhs.range.lowerBound
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/HsCryptoKit/EllipticCurveEncrypterSecp256k1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ final class EllipticCurveEncrypterSecp256k1 {
private let context: OpaquePointer

init() {
context = secp256k1.Context.raw
context = secp256k1.Context.rawRepresentation
}

/// Signs the hash with the private key. Produces signature data structure that can be exported with
Expand Down
38 changes: 20 additions & 18 deletions Sources/HsCryptoKit/SchnorrHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ public struct SchnorrHelper {
}

public static func hashTweak(data: Data, tag: String) throws -> Data {
let tagBytes = tag.data(using: .utf8)!.bytes

guard let tagBytes = tag.data(using: .utf8) else {
throw SchnorrError.hashTweakError
}
return try Data(SHA256.taggedHash(tag: tagBytes, data: data).bytes)
}

Expand All @@ -78,8 +79,8 @@ public struct SchnorrHelper {

// int(hashTapTweak(bytes(P)))G
var tweakedPublicKey = secp256k1_pubkey()
guard secp256k1_ec_seckey_verify(secp256k1.Context.raw, tweakedHash.bytes) == 1,
secp256k1_ec_pubkey_create(secp256k1.Context.raw, &tweakedPublicKey, tweakedHash.bytes) == 1
guard secp256k1_ec_seckey_verify(secp256k1.Context.rawRepresentation, tweakedHash.bytes) == 1,
secp256k1_ec_pubkey_create(secp256k1.Context.rawRepresentation, &tweakedPublicKey, tweakedHash.bytes) == 1
else {
throw SchnorrError.keyTweakError
}
Expand All @@ -88,7 +89,7 @@ public struct SchnorrHelper {
var internalKey = secp256k1_pubkey()
guard internalKeyBytes.withUnsafeBytes({ rawBytes -> Int32 in
guard let rawPointer = rawBytes.bindMemory(to: UInt8.self).baseAddress else { return 0 }
return secp256k1_ec_pubkey_parse(secp256k1.Context.raw, &internalKey, rawPointer, internalKeyBytes.count)
return secp256k1_ec_pubkey_parse(secp256k1.Context.rawRepresentation, &internalKey, rawPointer, internalKeyBytes.count)
}) == 1 else {
throw SchnorrError.keyTweakError
}
Expand All @@ -97,7 +98,7 @@ public struct SchnorrHelper {
var pubKeyLen = 33
var outputKeyBytes = [UInt8](repeating: 0, count: pubKeyLen)

guard secp256k1_ec_pubkey_serialize(secp256k1.Context.raw, &outputKeyBytes, &pubKeyLen, &outputKey, secp256k1.Format.compressed.rawValue) == 1 else {
guard secp256k1_ec_pubkey_serialize(secp256k1.Context.rawRepresentation, &outputKeyBytes, &pubKeyLen, &outputKey, secp256k1.Format.compressed.rawValue) == 1 else {
throw SchnorrError.keyTweakError
}

Expand All @@ -112,8 +113,8 @@ public struct SchnorrHelper {

// int(hashTapTweak(bytes(P)))G
var tweakedPublicKey = secp256k1_pubkey()
guard secp256k1_ec_seckey_verify(secp256k1.Context.raw, tweakedHash.bytes) == 1,
secp256k1_ec_pubkey_create(secp256k1.Context.raw, &tweakedPublicKey, tweakedHash.bytes) == 1
guard secp256k1_ec_seckey_verify(secp256k1.Context.rawRepresentation, tweakedHash.bytes) == 1,
secp256k1_ec_pubkey_create(secp256k1.Context.rawRepresentation, &tweakedPublicKey, tweakedHash.bytes) == 1
else {
throw SchnorrError.privateKeyTweakError
}
Expand All @@ -122,37 +123,37 @@ public struct SchnorrHelper {
var internalKey = secp256k1_pubkey()
guard internalKeyBytes.withUnsafeBytes({ rawBytes -> Int32 in
guard let rawPointer = rawBytes.bindMemory(to: UInt8.self).baseAddress else { return 0 }
return secp256k1_ec_pubkey_parse(secp256k1.Context.raw, &internalKey, rawPointer, internalKeyBytes.count)
return secp256k1_ec_pubkey_parse(secp256k1.Context.rawRepresentation, &internalKey, rawPointer, internalKeyBytes.count)
}) == 1 else {
throw SchnorrError.privateKeyTweakError
}

let outputKey = try Crypto.addEllipticCurvePoints(a: internalKey, b: tweakedPublicKey)
var privateBytes = privateKey.bytes
guard secp256k1_ec_seckey_tweak_add(secp256k1.Context.raw, &privateBytes, tweakedHash.bytes) == 1,
secp256k1_ec_seckey_verify(secp256k1.Context.raw, privateBytes) == 1 else {
guard secp256k1_ec_seckey_tweak_add(secp256k1.Context.rawRepresentation, &privateBytes, tweakedHash.bytes) == 1,
secp256k1_ec_seckey_verify(secp256k1.Context.rawRepresentation, privateBytes) == 1 else {
throw SchnorrError.privateKeyTweakError
}

var _outputKey = secp256k1_pubkey()
guard secp256k1_ec_pubkey_create(secp256k1.Context.raw, &_outputKey, privateBytes) == 1 else {
guard secp256k1_ec_pubkey_create(secp256k1.Context.rawRepresentation, &_outputKey, privateBytes) == 1 else {
throw SchnorrError.privateKeyTweakError
}

let keysEqual = withUnsafePointer(to: outputKey) { outputKeyPointer in
withUnsafePointer(to: _outputKey) { _outputKeyPointer in
secp256k1_ec_pubkey_cmp(secp256k1.Context.raw, outputKeyPointer, _outputKeyPointer)
secp256k1_ec_pubkey_cmp(secp256k1.Context.rawRepresentation, outputKeyPointer, _outputKeyPointer)
}
}

if keysEqual != 0 {
privateBytes = privateKey.bytes
guard secp256k1_ec_seckey_negate(secp256k1.Context.raw, &privateBytes) == 1 else {
guard secp256k1_ec_seckey_negate(secp256k1.Context.rawRepresentation, &privateBytes) == 1 else {
throw SchnorrError.privateKeyTweakError
}

guard secp256k1_ec_seckey_tweak_add(secp256k1.Context.raw, &privateBytes, tweakedHash.bytes) == 1,
secp256k1_ec_seckey_verify(secp256k1.Context.raw, privateBytes) == 1 else {
guard secp256k1_ec_seckey_tweak_add(secp256k1.Context.rawRepresentation, &privateBytes, tweakedHash.bytes) == 1,
secp256k1_ec_seckey_verify(secp256k1.Context.rawRepresentation, privateBytes) == 1 else {
throw SchnorrError.privateKeyTweakError
}
}
Expand All @@ -173,8 +174,8 @@ public struct SchnorrHelper {
var signature = [UInt8](repeating: 0, count: 64)
var extraParams = secp256k1_schnorrsig_extraparams(magic: magic, noncefp: nil, ndata: auxRandPointer)

guard secp256k1_keypair_create(secp256k1.Context.raw, &keypair, tweakedPrivateKey.bytes) == 1,
secp256k1_schnorrsig_sign_custom(secp256k1.Context.raw, &signature, &message, message.count, &keypair, &extraParams) == 1
guard secp256k1_keypair_create(secp256k1.Context.rawRepresentation, &keypair, tweakedPrivateKey.bytes) == 1,
secp256k1_schnorrsig_sign_custom(secp256k1.Context.rawRepresentation, &signature, &message, message.count, &keypair, &extraParams) == 1
else {
throw SchnorrError.signError
}
Expand All @@ -187,6 +188,7 @@ public struct SchnorrHelper {
case privateKeyTweakError
case keyTweakError
case signError
case hashTweakError
}

}