Skip to content
Closed
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
22 changes: 11 additions & 11 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ 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: "secp256k1", url: "https://github.com/GigaBitcoin/secp256k1.swift", .exact("0.12.2")),
.package(name: "SocketIO", url: "https://github.com/socketio/socket.io-client-swift", .upToNextMajor(from: "16.0.1")),
.package(name: "curvelib", url: "https://github.com/tkey/curvelib.swift", .branch("main")),
],
targets: [
.binaryTarget(name: "libdkls",
Expand All @@ -25,7 +25,10 @@ let package = Package(
),
.target(
name: "tss-client-swift",
dependencies: ["BigInt", "CryptoSwift", "secp256k1", "SocketIO", "dkls"]),
dependencies: ["BigInt", "CryptoSwift", "SocketIO", "dkls",
"curvelib",
// .product(name: "curvelib", package: "curvelib.swift")
]),
.testTarget(
name: "tss-client-swiftTests",
dependencies: ["tss-client-swift", "BigInt"]),
Expand Down
135 changes: 74 additions & 61 deletions Sources/tss-client-swift/Helpers.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import BigInt
import CryptoKit
import Foundation
import CryptoSwift

import curvelib

public class TSSHelpers {
// singleton class
Expand All @@ -12,9 +14,9 @@ public class TSSHelpers {
/// - message: The message to be hashed.
///
/// - Returns: `String`
public static func hashMessage(message: String) -> String {
public static func hashMessage(message: String) -> Data {
let hash = Data(message.utf8).sha3(.keccak256)
return hash.base64EncodedString()
return hash
}

/// Converts a share to base64
Expand Down Expand Up @@ -43,7 +45,7 @@ public class TSSHelpers {
/// - pubKey: The public key to be checked against, 65 byte representation
///
/// - Returns: `Bool`
public static func verifySignature(msgHash: String, s: BigInt, r: BigInt, v: UInt8, pubKey: Data) -> Bool {
public static func verifySignature(msgHash: Data, s: Data, r: Data, v: UInt8, pubKey: Data) -> Bool {
do {
let pk = try TSSHelpers.recoverPublicKey(msgHash: msgHash, s: s, r: r, v: v)
if pk == pubKey {
Expand All @@ -66,22 +68,14 @@ public class TSSHelpers {
/// - Returns: `Data`
///
/// - Throws: `TSSClientError`
public static func recoverPublicKey(msgHash: String, s: BigInt, r: BigInt, v: UInt8) throws -> Data {
if let secpSigMarshalled = SECP256K1.marshalSignature(v: v, r: r.serialize().suffix(32), s: s.serialize().suffix(32))
{
guard let msgData = msgHash.data(using: .utf8),
let msgB64 = Data(base64Encoded: msgData)
else {
throw TSSClientError("Invalid base64 encoded hash")
}
if let pk = SECP256K1.recoverPublicKey(hash: msgB64, signature: secpSigMarshalled, compressed: false) {
return pk
} else {
throw TSSClientError("Public key recover failed")
}
} else {
throw TSSClientError("Problem with signature")
}
public static func recoverPublicKey(msgHash: Data, s: Data, r: Data, v: UInt8) throws -> Data {

let rSign = try curvelib.Secp256k1.RecoverableSignature.init(r: r, s: s, v: v)

let rPKey = try curvelib.Secp256k1.recoverPublicKey(rSignature: rSign, message: msgHash)

return try rPKey.getSec1Full()

}

/// Converts a public key to base64.
Expand Down Expand Up @@ -168,13 +162,9 @@ public class TSSHelpers {
/// - Returns: `String`
///
/// - Throws: `TSSClientError`
public static func hexSignature(s: BigInt, r: BigInt, v: UInt8) throws -> String {
if let secpSigMarshalled = SECP256K1.marshalSignature(v: v, r: r.serialize().suffix(32), s: s.serialize().suffix(32))
{
return secpSigMarshalled.toHexString()
} else {
throw TSSClientError("Problem with signature components")
}
public static func hexSignature(s: Data, r: Data, v: UInt8) throws -> String {
let marshallSig = r + s + Data([v])
return marshallSig.hexString
}

/// Calculates server coefficients based on the distributed key generation indexes and the user tss index
Expand Down Expand Up @@ -228,6 +218,10 @@ public class TSSHelpers {
return denormalizeShare
}


//
// Use TssModule.get_tss_pubkey instead

/// Calculates the public key that will be used for TSS signing.
///
/// - Parameters:
Expand All @@ -238,40 +232,59 @@ public class TSSHelpers {
/// - Returns: `Data`
///
/// - Throws: `TSSClientError`
public static func getFinalTssPublicKey(dkgPubKey: Data, userSharePubKey: Data, userTssIndex: BigInt) throws -> Data {
let serverLagrangeCoeff = try TSSHelpers.getLagrangeCoefficient(parties: [BigInt(1), userTssIndex], party: BigInt(1))
let userLagrangeCoeff = try TSSHelpers.getLagrangeCoefficient(parties: [BigInt(1), userTssIndex], party: userTssIndex)

guard let serverTermUnprocessed = SECP256K1.parsePublicKey(serializedKey: dkgPubKey),
let userTermUnprocessed = SECP256K1.parsePublicKey(serializedKey: userSharePubKey) else {
throw TSSClientError("InvalidPublicKey")
}

var serverTerm = serverTermUnprocessed
var userTerm = userTermUnprocessed

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

guard let serverTermProcessed = SECP256K1.ecdh(pubKey: serverTerm, privateKey: serverLagrangeCoeffData),
let userTermProcessed = SECP256K1.ecdh(pubKey: userTerm, privateKey: userLagrangeCoeffData) else {
throw TSSClientError("Failed to process server term")
}

serverTerm = serverTermProcessed
userTerm = userTermProcessed

guard let serializedServerTerm = SECP256K1.serializePublicKey(publicKey: &serverTerm),
let serializedUserTerm = SECP256K1.serializePublicKey(publicKey: &userTerm) else {
throw TSSClientError("Failed to process client term")
}

guard let combination = SECP256K1.combineSerializedPublicKeys(keys: [serializedServerTerm, serializedUserTerm]) else {
throw TSSClientError("Failed to combine keys")
}

return combination
}
// public static func getFinalTssPublicKey(dkgPubKey: Data, userSharePubKey: Data, userTssIndex: BigInt) throws -> Data {
// 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 curvelib.Secp256k1.PublicKey(input: dkgPubKey)
// let _userTermUnprocessed = try curvelib.Secp256k1.PublicKey(input: userSharePubKey)
//
// guard let serverTermUnprocessed = SECP256K1.parsePublicKey(serializedKey: dkgPubKey),
// let userTermUnprocessed = SECP256K1.parsePublicKey(serializedKey: userSharePubKey) else {
// throw TSSClientError("InvalidPublicKey")
// }
//
// var serverTerm = serverTermUnprocessed
// var userTerm = userTermUnprocessed
//
// let serverLagrangeCoeffData = try Data.ensureDataLengthIs32Bytes(serverLagrangeCoeff.serialize())
// let userLagrangeCoeffData = try Data.ensureDataLengthIs32Bytes(userLagrangeCoeff.serialize())
//
// let serverLarganceCoeffPkey = try curvelib.Secp256k1.PrivateKey(input: serverLagrangeCoeffData)
//
// let userLagrangeCoeffPkey = try curvelib.Secp256k1.PrivateKey(input: userLagrangeCoeffData)
//
// let serverEcdh = try curvelib.Secp256k1.ecdh(privateKey: serverLarganceCoeffPkey, publicKey: _serverTermUnprocessed)
//
// let userEcdh = try curvelib.Secp256k1.ecdh(privateKey: userLagrangeCoeffPkey, publicKey: _userTermUnprocessed)
//
// let serverEcdhPkey = try curvelib.Secp256k1.PublicKey.fromPrivateKey(privateKey: serverEcdh)
// let userEcdhPkey = try curvelib.Secp256k1.PublicKey.fromPrivateKey(privateKey: userEcdh)
//
//
// let combined = try curvelib.Secp256k1.PublicKey.combine(publicKeys: [serverEcdhPkey, userEcdhPkey])
//
// guard let serverTermProcessed = SECP256K1.ecdh(pubKey: serverTerm, privateKey: serverLagrangeCoeffData),
// let userTermProcessed = SECP256K1.ecdh(pubKey: userTerm, privateKey: userLagrangeCoeffData) else {
// throw TSSClientError("Failed to process server term")
// }
//
// serverTerm = serverTermProcessed
// userTerm = userTermProcessed
//
// guard let serializedServerTerm = SECP256K1.serializePublicKey(publicKey: &serverTerm),
// let serializedUserTerm = SECP256K1.serializePublicKey(publicKey: &userTerm) else {
// throw TSSClientError("Failed to process client term")
// }
// print(serializedServerTerm.hexString)
//
//
//
// guard let combination = SECP256K1.combineSerializedPublicKeys(keys: [serializedServerTerm, serializedUserTerm]) else {
// throw TSSClientError("Failed to combine keys")
// }
// return combination
// }

internal static func getAdditiveCoefficient(isUser: Bool, participatingServerIndexes: [BigInt], userTSSIndex: BigInt, serverIndex: BigInt?) throws -> BigInt {
if isUser {
Expand Down
Loading