Skip to content

Commit

Permalink
feat: remove extension
Browse files Browse the repository at this point in the history
  • Loading branch information
metalurgical committed Jan 22, 2024
1 parent 41ea557 commit 2f0a235
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 132 deletions.
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

0 comments on commit 2f0a235

Please sign in to comment.