Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove extension #16

Merged
merged 2 commits into from
Jan 23, 2024
Merged
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.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"package": "curvelib.swift",
"repositoryURL": "https://github.com/tkey/curvelib.swift",
"state": {
"branch": "refactor",
"revision": "3477abff071170d005f05397c6049478fa6ecaf3",
"branch": "extension",
"revision": "d5acae05bfd832393524ec8e0bf8820a745e5331",
"version": null
}
},
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let package = Package(
dependencies: [
.package(name: "BigInt", url: "https://github.com/attaswift/BigInt.git", from: "5.3.0"),
.package(name: "CryptoSwift", url: "https://github.com/krzyzanowskim/CryptoSwift.git",from: "1.7.2"),
.package(name: "curvelib.swift", url: "https://github.com/tkey/curvelib.swift", .branch("refactor")),
.package(name: "curvelib.swift", url: "https://github.com/tkey/curvelib.swift", .branch("extension")),
.package(name: "SocketIO", url: "https://github.com/socketio/socket.io-client-swift", .upToNextMajor(from: "16.0.1")),
],
targets: [
Expand Down
116 changes: 0 additions & 116 deletions Sources/tss-client-swift/CurveSecp256k1.swift

This file was deleted.

16 changes: 9 additions & 7 deletions Sources/tss-client-swift/Helpers.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import BigInt
import curvelib_swift
import CryptoKit
import CryptoSwift
import Foundation

public class TSSHelpers {
Expand Down Expand Up @@ -78,7 +79,8 @@ public class TSSHelpers {
throw TSSClientError("Invalid base64 encoded hash")
}
do {
let pk = try CurveSecp256k1.recoverPublicKey(hash: msgB64.hexString, signature: sigString, compressed: false)
let signature = try Signature(hex: sigString)
let pk = try ECDSA.recover(signature: signature, hash: msgB64.hexString).serialize(compressed: false)
return pk
} catch (_) {
throw TSSClientError("Public key recover failed")
Expand Down Expand Up @@ -241,18 +243,18 @@ public class TSSHelpers {
let serverLagrangeCoeff = try TSSHelpers.getLagrangeCoefficient(parties: [BigInt(1), userTssIndex], party: BigInt(1))
let userLagrangeCoeff = try TSSHelpers.getLagrangeCoefficient(parties: [BigInt(1), userTssIndex], party: userTssIndex)

let serverTermUnprocessed = try CurveSecp256k1.parsePublicKey(serializedKey: dkgPubKey.toHexString())
let userTermUnprocessed = try CurveSecp256k1.parsePublicKey(serializedKey: userSharePubKey.toHexString())
let serverTermUnprocessed = try PublicKey(hex: dkgPubKey.toHexString())
let userTermUnprocessed = try PublicKey(hex: userSharePubKey.toHexString())

var serverTerm = serverTermUnprocessed
var userTerm = userTermUnprocessed

let serverLagrangeCoeffData = try Data.ensureDataLengthIs32Bytes(serverLagrangeCoeff.serialize())
let userLagrangeCoeffData = try Data.ensureDataLengthIs32Bytes(userLagrangeCoeff.serialize())

let serverTermProcessed = try CurveSecp256k1.ecdh(pubKey: serverTerm, privateKey: try SecretKey(hex: serverLagrangeCoeffData.toHexString()))
let serverTermProcessed = try PublicKey(hex: ECDH.ecdhStandard(sk: SecretKey(hex: serverLagrangeCoeffData.toHexString()), pk: serverTerm))

let userTermProcessed = try CurveSecp256k1.ecdh(pubKey: userTerm, privateKey: SecretKey(hex: userLagrangeCoeffData.toHexString()))
let userTermProcessed = try PublicKey(hex: ECDH.ecdhStandard(sk: SecretKey(hex: userLagrangeCoeffData.toHexString()), pk: userTerm))

serverTerm = serverTermProcessed
userTerm = userTermProcessed
Expand All @@ -261,9 +263,9 @@ public class TSSHelpers {
try collection.insert(key: serverTermProcessed)
try collection.insert(key: userTermProcessed)

let combination = try CurveSecp256k1.combineSerializedPublicKeys(keys: collection)
let combination = try PublicKey.combine(collection: collection)

return Data(hexString: combination)!
return Data(hexString: try combination.serialize(compressed: false))!
}

internal static func getAdditiveCoefficient(isUser: Bool, participatingServerIndexes: [BigInt], userTSSIndex: BigInt, serverIndex: BigInt?) throws -> BigInt {
Expand Down
3 changes: 2 additions & 1 deletion Sources/tss-client-swift/dkls/ChaChaRng.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import curvelib_swift
#if canImport(dkls)
import dkls
#endif
Expand All @@ -7,7 +8,7 @@ internal final class ChaChaRng {
private(set) var pointer: OpaquePointer?

public init() throws {
let stateBytes = try CurveSecp256k1.generatePrivateKey()
let stateBytes = try SecretKey().serialize()
let state = Data(hexString: stateBytes)!.base64EncodedString()

var errorCode: Int32 = -1
Expand Down
16 changes: 8 additions & 8 deletions Tests/tss-client-swiftTests/tss_client_swiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ final class tss_client_swiftTests: XCTestCase {
let hash = TSSHelpers.hashMessage(message: token)
let data = hash.data(using: .utf8)!
let msgB64 = Data(base64Encoded: data)!
let serializedNodeSig = try CurveSecp256k1.signForRecovery(hash: msgB64.hexString, privateKey: SecretKey(hex: item)).serialize()
let serializedNodeSig = try ECDSA.signRecoverable(key: SecretKey(hex: item), hash: msgB64.hexString).serialize()
let msg: [String: Any] = [
"data": token,
"sig": serializedNodeSig,
Expand Down Expand Up @@ -75,7 +75,7 @@ final class tss_client_swiftTests: XCTestCase {
var additiveShares: [BigInt] = []
var shareSum = BigInt.zero
for _ in 0 ..< (parties.count - 1) {
let shareBigUint = BigUInt(try CurveSecp256k1.generatePrivateKey(), radix: 16)
let shareBigUint = BigUInt(try SecretKey().serialize(), radix: 16)
let shareBigInt = BigInt(sign: .plus, magnitude: shareBigUint!)
additiveShares.append(shareBigInt)
shareSum += shareBigInt
Expand Down Expand Up @@ -144,12 +144,12 @@ final class tss_client_swiftTests: XCTestCase {

private func setupMockShares(endpoints: [String?], parties: [Int32], localClientIndex: Int32, session: String) throws -> (Data, Data)
{
let privKey = try CurveSecp256k1.generatePrivateKey()
let privKeyBigUInt = BigUInt(privKey, radix: 16)
let privKey = SecretKey()
let privKeyBigUInt = BigUInt(try privKey.serialize(), radix: 16)
let privKeyBigInt = BigInt(sign: .plus, magnitude: privKeyBigUInt!)
let publicKey = try CurveSecp256k1.privateToPublic(privateKey: SecretKey(hex: privKey), compressed: false)
let publicKey = try privKey.toPublic()
try distributeShares(privKey: privKeyBigInt, parties: parties, endpoints: endpoints, localClientIndex: localClientIndex, session: session)
return (Data(hexString: privKey)!, Data(hexString: publicKey)!)
return (Data(hexString: try privKey.serialize())!, Data(hexString: try publicKey.serialize(compressed: false))!)
}

private func generateEndpoints(parties: Int, clientIndex: Int32) -> ([String?], [String?], [Int32]) {
Expand Down Expand Up @@ -177,7 +177,7 @@ final class tss_client_swiftTests: XCTestCase {
let msg = "hello world"
let msgHash = TSSHelpers.hashMessage(message: msg)
let clientIndex = Int32(parties - 1)
let randomKey = BigUInt(try CurveSecp256k1.generatePrivateKey(), radix: 16)
let randomKey = BigUInt(try SecretKey().serialize(), radix: 16)
let random = BigInt(sign: .plus, magnitude: randomKey!) + BigInt(Date().timeIntervalSince1970)
let randomNonce = TSSHelpers.hashMessage(message: String(random))
let testingRouteIdentifier = "testingShares"
Expand Down Expand Up @@ -207,7 +207,7 @@ final class tss_client_swiftTests: XCTestCase {
let pk = try! TSSHelpers.recoverPublicKey(msgHash: msgHash, s: s, r: r, v: v)
_ = try! TSSHelpers.hexUncompressedPublicKey(pubKey: Data(hexString:pk)!, return64Bytes: true)
let pkHex65 = try! TSSHelpers.hexUncompressedPublicKey(pubKey: Data(hexString:pk)!, return64Bytes: false)
let skToPkHex = try CurveSecp256k1.privateToPublic(privateKey: SecretKey(hex: privateKey.hexString))
let skToPkHex = try SecretKey(hex: privateKey.hexString).toPublic().serialize(compressed: false)
XCTAssert(pkHex65 == skToPkHex)

print(try! TSSHelpers.hexSignature(s: s, r: r, v: v))
Expand Down