Skip to content

Commit

Permalink
feat: remove cryptoswift
Browse files Browse the repository at this point in the history
  • Loading branch information
metalurgical committed Apr 17, 2024
1 parent 9590a1d commit 30e7ec8
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 66 deletions.
13 changes: 2 additions & 11 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,13 @@
"version": "5.3.0"
}
},
{
"package": "CryptoSwift",
"repositoryURL": "https://github.com/krzyzanowskim/CryptoSwift.git",
"state": {
"branch": null,
"revision": "32f641cf24fc7abc1c591a2025e9f2f572648b0f",
"version": "1.7.2"
}
},
{
"package": "curvelib.swift",
"repositoryURL": "https://github.com/tkey/curvelib.swift",
"state": {
"branch": null,
"revision": "7dad3bf1793de263f83406c08c18c9316abf082f",
"version": "0.1.2"
"revision": "2df9f638fd121b445ce3b410cd79ac4d0cfa94ed",
"version": "1.0.0"
}
},
{
Expand Down
5 changes: 2 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,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", from: "0.1.1"),
.package(name: "curvelib.swift", url: "https://github.com/tkey/curvelib.swift", from: "1.0.0"),
.package(name: "SocketIO", url: "https://github.com/socketio/socket.io-client-swift", .upToNextMajor(from: "16.0.1")),
],
targets: [
Expand All @@ -25,7 +24,7 @@ let package = Package(
),
.target(
name: "tss-client-swift",
dependencies: ["BigInt", "CryptoSwift", .product(name: "curveSecp256k1", package: "curvelib.swift"), "SocketIO", "dkls"]),
dependencies: ["BigInt", .product(name: "curveSecp256k1", package: "curvelib.swift"), "SocketIO", "dkls"]),
.testTarget(
name: "tss-client-swiftTests",
dependencies: ["tss-client-swift", "BigInt"]),
Expand Down
38 changes: 19 additions & 19 deletions Sources/tss-client-swift/Helpers.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import BigInt
import CryptoKit
import CryptoSwift
import Foundation
import curveSecp256k1

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

Expand Down Expand Up @@ -96,16 +96,16 @@ public class TSSHelpers {
///
/// - Throws: `TSSClientError`
public static func base64PublicKey(pubKey: Data) throws -> String {
if pubKey.bytes.count == 65 { // first byte is 04 prefix indicating uncompressed format, must be dropped for dkls
if pubKey.bytes.first == 04 {
return Data(pubKey.bytes.dropFirst()).base64EncodedString()
if pubKey.count == 65 { // first byte is 04 prefix indicating uncompressed format, must be dropped for dkls
if pubKey.first == 04 {
return Data(pubKey.dropFirst()).base64EncodedString()
} else {
throw TSSClientError("Invalid public key bytes")
}
}

if pubKey.bytes.count == 64 {
return Data(pubKey.bytes).base64EncodedString()
if pubKey.count == 64 {
return Data(pubKey).base64EncodedString()
}

throw TSSClientError("Invalid public key bytes")
Expand All @@ -121,21 +121,21 @@ public class TSSHelpers {
///
/// - Throws: `TSSClientError`
public static func hexUncompressedPublicKey(pubKey: Data, return64Bytes: Bool) throws -> String {
if pubKey.bytes.count == 65 {
if pubKey.count == 65 {
if return64Bytes {
if pubKey.bytes.first == 04 {
return Data(pubKey.bytes.dropFirst()).hexString
if pubKey.first == 04 {
return Data(pubKey.dropFirst()).hexString
} else {
throw TSSClientError("Invalid public key bytes")
}
} else {
return Data(pubKey.bytes).hexString
return Data(pubKey).hexString
}
}

if pubKey.bytes.count == 64 {
if pubKey.count == 64 {
if return64Bytes {
return Data(pubKey.bytes).hexString
return Data(pubKey).hexString
} else { // first byte should be 04 prefix
let prefix: UInt8 = 4
var pk = Data(pubKey)
Expand Down Expand Up @@ -210,7 +210,7 @@ public class TSSHelpers {
public static func getClientCoefficients(participatingServerDKGIndexes: [BigInt], userTssIndex: BigInt) throws -> String {
let coeff = try getDKLSCoefficient(isUser: true, participatingServerIndexes: participatingServerDKGIndexes, userTssIndex: userTssIndex, serverIndex: nil)

return coeff.magnitude.serialize().toHexString()
return coeff.magnitude.serialize().hexString
}

/// Calculates client(user) denormalise Share based on the distributed key generation indexes and the user tss index
Expand Down Expand Up @@ -243,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 PublicKey(hex: dkgPubKey.toHexString())
let userTermUnprocessed = try PublicKey(hex: userSharePubKey.toHexString())
let serverTermUnprocessed = try PublicKey(hex: dkgPubKey.hexString)
let userTermUnprocessed = try PublicKey(hex: userSharePubKey.hexString)

var serverTerm = serverTermUnprocessed
var userTerm = userTermUnprocessed

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

let serverTermProcessed = try PublicKey(hex: ECDH.ecdhStandard(sk: SecretKey(hex: serverLagrangeCoeffData.toHexString()), pk: serverTerm))
let serverTermProcessed = try PublicKey(hex: ECDH.ecdhStandard(sk: SecretKey(hex: serverLagrangeCoeffData.hexString), pk: serverTerm))

let userTermProcessed = try PublicKey(hex: ECDH.ecdhStandard(sk: SecretKey(hex: userLagrangeCoeffData.toHexString()), pk: userTerm))
let userTermProcessed = try PublicKey(hex: ECDH.ecdhStandard(sk: SecretKey(hex: userLagrangeCoeffData.hexString), pk: userTerm))

serverTerm = serverTermProcessed
userTerm = userTermProcessed
Expand Down
6 changes: 3 additions & 3 deletions Sources/tss-client-swift/TSSClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public class TSSClient {

if !hashOnly {
if let original_message = original_message {
if TSSHelpers.hashMessage(message: original_message) != message {
if try TSSHelpers.hashMessage(message: original_message) != message {
throw TSSClientError("hash of original message does not match message")
}
} else {
Expand Down Expand Up @@ -367,10 +367,10 @@ public class TSSClient {
let precompute_r = try precompute.getR()
let decoded_r = try Data(base64Encoded: precompute_r) ?? { throw TSSClientError("R from precompute could not be decoded") }()
let decoded = try Data(base64Encoded: signature) ?? { throw TSSClientError("Signature could not be decoded") }()
let sighex = decoded.toHexString()
let sighex = decoded.hexString
let r = try BigInt(sighex.prefix(64), radix: 16) ?? { throw TSSClientError("R component for signature is not valid") }()
var s = try BigInt(sighex.suffix(from: sighex.index(sighex.startIndex, offsetBy: 64)), radix: 16) ?? { throw TSSClientError("S component for signature is not valid") }()
let v = try decoded_r.bytes.last ?? { throw TSSClientError("V component for signature is not valid") }()
let v = try decoded_r.last ?? { throw TSSClientError("V component for signature is not valid") }()
var recoveryParam = UInt8(v % 2)

if _sLessThanHalf {
Expand Down
20 changes: 0 additions & 20 deletions Sources/tss-client-swift/extension/DataExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,6 @@ enum DataPaddingError: Error {
}

public extension Data {
init?(hexString: String) {
let length = hexString.count / 2
var data = Data(capacity: length)
for i in 0 ..< length {
let j = hexString.index(hexString.startIndex, offsetBy: i * 2)
let k = hexString.index(j, offsetBy: 2)
let bytes = hexString[j ..< k]
if var byte = UInt8(bytes, radix: 16) {
data.append(&byte, count: 1)
} else {
return nil
}
}
self = data
}

var hexString: String {
return map { String(format: "%02x", $0) }.joined()
}

static func ensureDataLengthIs32Bytes(_ data: Data) throws -> Data {
if data.count < 32 {
let paddingCount = 32 - data.count
Expand Down
12 changes: 6 additions & 6 deletions Tests/tss-client-swiftTests/helperTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import BigInt

final class helpersTests: XCTestCase {
func testGetLangrange () throws {
let result = try! TSSHelpers.getLagrangeCoefficient(parties: [BigInt(50), BigInt(100)], party: BigInt(10)).serialize().suffix(32).toHexString()
let result = try! TSSHelpers.getLagrangeCoefficient(parties: [BigInt(50), BigInt(100)], party: BigInt(10)).serialize().suffix(32).hexString
let expected = "f1c71c71c71c71c71c71c71c71c71c7093de09848919ecaa352a3cda52dde84d".addLeading0sForLength64()
XCTAssertEqual(result, expected)
}

func testGetAdditiveCoefficient () throws {
let result = try TSSHelpers.getAdditiveCoefficient(isUser: true, participatingServerIndexes: [BigInt(100), BigInt(200), BigInt(300)], userTSSIndex: BigInt(10), serverIndex: nil)
let expected = "71c71c71c71c71c71c71c71c71c71c7136869b1131759c8c55410d93eac2c7ab".addLeading0sForLength64()
XCTAssertEqual(result.serialize().suffix(32).toHexString(), expected)
XCTAssertEqual(result.serialize().suffix(32).hexString, expected)

let coeff = try TSSHelpers.getAdditiveCoefficient(isUser: false, participatingServerIndexes: [BigInt(1), BigInt(4), BigInt(5)], userTSSIndex: BigInt(3), serverIndex: BigInt(1))
let compare = BigInt("7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a3", radix: 16)
Expand All @@ -23,13 +23,13 @@ final class helpersTests: XCTestCase {
func testGetDenormaliseCoefficient () throws {
let result = try TSSHelpers.getDenormalizedCoefficient(party: BigInt(100), parties: [BigInt(100), BigInt(200)])
let expected = "7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a1".addLeading0sForLength64()
XCTAssertEqual(result.serialize().suffix(32).toHexString(), expected)
XCTAssertEqual(result.serialize().suffix(32).hexString, expected)
}

func testGetDKLSCoeff () throws {
let result = try TSSHelpers.getDKLSCoefficient(isUser: true, participatingServerIndexes: [BigInt(100), BigInt(200)], userTssIndex: BigInt(100), serverIndex: nil)
let expected = "a57eb50295fad40a57eb50295fad40a4ac66b301bc4dfafaaa8d2b05b28fae1".addLeading0sForLength64()
XCTAssertEqual(result.serialize().suffix(32).toHexString(), expected)
XCTAssertEqual(result.serialize().suffix(32).hexString, expected)

let dklsCoeff = try TSSHelpers.getDKLSCoefficient(isUser: true, participatingServerIndexes: [BigInt(1), BigInt(4), BigInt(5)], userTssIndex: BigInt(3), serverIndex: nil)
let compare = BigInt("7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a1", radix: 16)
Expand Down Expand Up @@ -60,7 +60,7 @@ final class helpersTests: XCTestCase {
}

func testDenormalizeShare () throws {
let share = BigUInt(Data(hex: "18db3574e4217154769ad9cd88900e7f1c198aa60a1379f3869ba8a7699e6b53"))
let share = BigUInt(Data(hexString: "18db3574e4217154769ad9cd88900e7f1c198aa60a1379f3869ba8a7699e6b53")!)
let denormalize2 = try TSSHelpers.denormalizeShare(participatingServerDKGIndexes: [BigInt(1), BigInt(2), BigInt(3) ], userTssIndex: BigInt(2), userTssShare: BigInt(sign: .plus, magnitude: share))
let denormalize3 = try TSSHelpers.denormalizeShare(participatingServerDKGIndexes: [BigInt(1), BigInt(2), BigInt(3) ], userTssIndex: BigInt(3), userTssShare: BigInt(sign: .plus, magnitude: share))

Expand All @@ -81,7 +81,7 @@ final class helpersTests: XCTestCase {

let tssPub = try TSSHelpers.getFinalTssPublicKey(dkgPubKey: dkgpub, userSharePubKey: userpub, userTssIndex: BigInt(2))

XCTAssertEqual(tssPub.toHexString(), "04dd1619c7e99eb665e37c74828762e6a677511d4c52656ddc6499a57d486bddb8c0dc63b229ec9a31f4216138c3fbb67ac2630831135aecbaf0aafa095e439c61")
XCTAssertEqual(tssPub.hexString, "04dd1619c7e99eb665e37c74828762e6a677511d4c52656ddc6499a57d486bddb8c0dc63b229ec9a31f4216138c3fbb67ac2630831135aecbaf0aafa095e439c61")
}

func testRemoveZeroTest() throws{
Expand Down
8 changes: 4 additions & 4 deletions Tests/tss-client-swiftTests/tss_client_swiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ final class tss_client_swiftTests: XCTestCase {

var sigs: [String] = []
for item in privateKeys {
let hash = TSSHelpers.hashMessage(message: token)
let hash = try TSSHelpers.hashMessage(message: token)
let data = hash.data(using: .utf8)!
let msgB64 = Data(base64Encoded: data)!
let serializedNodeSig = try ECDSA.signRecoverable(key: SecretKey(hex: item), hash: msgB64.hexString).serialize()
Expand Down Expand Up @@ -87,7 +87,7 @@ final class tss_client_swiftTests: XCTestCase {
let reduced = additiveShares.reduce(0) {
($0 + $1).modulus(TSSClient.modulusValueSigned)
}
XCTAssert(reduced.serialize().toHexString() == privKey.serialize().toHexString())
XCTAssert(reduced.serialize().hexString == privKey.serialize().hexString)

// denormalize shares
var shares: [BigInt] = []
Expand Down Expand Up @@ -175,11 +175,11 @@ final class tss_client_swiftTests: XCTestCase {
func testClientLocal() throws {
let parties = 4
let msg = "hello world"
let msgHash = TSSHelpers.hashMessage(message: msg)
let msgHash = try TSSHelpers.hashMessage(message: msg)
let clientIndex = Int32(parties - 1)
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 randomNonce = try TSSHelpers.hashMessage(message: String(random))
let testingRouteIdentifier = "testingShares"
let vid = "test_verifier_name" + Delimiters.Delimiter1 + "test_verifier_id"
let session = testingRouteIdentifier +
Expand Down

0 comments on commit 30e7ec8

Please sign in to comment.