Skip to content

Commit

Permalink
code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
metalurgical committed May 15, 2024
1 parent ff75355 commit 0b5e898
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 110 deletions.
47 changes: 23 additions & 24 deletions Sources/TorusUtils/Helpers/LangrangeInterpolatePoly.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ internal class Lagrange {
}
return key
}

public static func generateEmptyBNArray(length: Int) -> [BigInt] {
return Array(repeating: BigInt(0), count: length)
}

public static func denominator(i: Int, innerPoints: [Point]) -> BigInt {
var result = BigInt(1)
let xi = innerPoints[i].x
Expand All @@ -31,7 +31,7 @@ internal class Lagrange {
}
return result
}

public static func interpolationPoly(i: Int, innerPoints: [Point]) -> [BigInt] {
var coefficients = generateEmptyBNArray(length: innerPoints.count)
let d = denominator(i: i, innerPoints: innerPoints)
Expand Down Expand Up @@ -61,13 +61,13 @@ internal class Lagrange {
}
return coefficients
}

public static func pointSort(innerPoints: [Point]) -> [Point] {
var pointArrClone = innerPoints
pointArrClone.sort { $0.x < $1.x }
return pointArrClone
}

public static func lagrange(unsortedPoints: [Point]) -> Polynomial {
let sortedPoints = pointSort(innerPoints: unsortedPoints)
var polynomial = generateEmptyBNArray(length: sortedPoints.count)
Expand All @@ -81,27 +81,26 @@ internal class Lagrange {
}
return Polynomial(polynomial: polynomial)
}

public static func lagrangeInterpolatePolynomial(points: [Point]) -> Polynomial {
return lagrange(unsortedPoints: points)
}

public static func lagrangeInterpolation(shares: [String], nodeIndex: [Int]) throws -> String {

let CurveSecp256k1N = KeyUtils.getOrderOfCurve()

// Convert shares to BigInt(Shares)
let sharesList: [BigInt] = shares.map({ BigInt($0.addLeading0sForLength64(), radix: 16)}).filter({ $0 != nil }).map({ $0! })

let sharesList: [BigInt] = shares.map({ BigInt($0.addLeading0sForLength64(), radix: 16) }).filter({ $0 != nil }).map({ $0! })
let indexList: [BigInt] = nodeIndex.map({ BigInt($0) })

if sharesList.count != indexList.count {
throw TorusUtilError.runtime("sharesList not equal to indexList length in lagrangeInterpolation")
}

var secret = BigUInt("0")
var sharesDecrypt = 0

for i in 0 ..< sharesList.count {
var upper = BigInt(1)
var lower = BigInt(1)
Expand All @@ -110,7 +109,7 @@ internal class Lagrange {
let negatedJ = indexList[j] * BigInt(-1)
upper = upper * negatedJ
upper = upper.modulus(CurveSecp256k1N)

var temp = indexList[i] - indexList[j]
temp = temp.modulus(CurveSecp256k1N)
lower = (lower * temp).modulus(CurveSecp256k1N)
Expand All @@ -133,37 +132,37 @@ internal class Lagrange {
throw TorusUtilError.interpolationFailed
}
}

public static func generateRandomPolynomial(degree: Int, secret: BigInt? = nil, deterministicShares: [Share]? = nil) throws -> Polynomial {
var actualS = secret
if secret == nil {
actualS = try generatePrivateExcludingIndexes(shareIndexes: [BigInt(0)])
}

if deterministicShares == nil {
var poly = [actualS!]
for _ in 0 ..< degree {
let share = try generatePrivateExcludingIndexes(shareIndexes: poly)
poly.append(share)
}

return Polynomial(polynomial: poly)
}

guard let deterministicShares = deterministicShares else {
throw NSError(domain: "", code: 0, userInfo: [NSLocalizedDescriptionKey: "Deterministic shares in generateRandomPolynomial should be an array"])
}

if deterministicShares.count > degree {
throw NSError(domain: "", code: 0, userInfo: [NSLocalizedDescriptionKey: "Deterministic shares in generateRandomPolynomial should be less or equal than degree to ensure an element of randomness"])
}

var points = [String: Point]()
for share in deterministicShares {
points[String(share.shareIndex, radix: 16).addLeading0sForLength64()] =
Point(x: share.shareIndex, y: share.share)
Point(x: share.shareIndex, y: share.share)
}

let remainingDegree = degree - deterministicShares.count
for _ in 0 ..< remainingDegree {
var shareIndex = try generatePrivateExcludingIndexes(shareIndexes: [BigInt(0)])
Expand All @@ -172,7 +171,7 @@ internal class Lagrange {
}
points[String(shareIndex, radix: 16).addLeading0sForLength64()] = Point(x: shareIndex, y: BigInt(Data(hex: try SecretKey().serialize().addLeading0sForLength64())))
}

points["0"] = Point(x: BigInt(0), y: actualS!)
return lagrangeInterpolatePolynomial(points: Array(points.values))
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/TorusUtils/VerifierParams.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Foundation
public struct VerifyParams: Codable {
public var verifier_id: String?
public var idtoken: String?

public init(verifier_id: String?, idtoken: String?) {
self.verifier_id = verifier_id
self.idtoken = idtoken
Expand Down
34 changes: 17 additions & 17 deletions Tests/TorusUtilsTests/AquaTest.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import BigInt
import FetchNodeDetails
import JWTKit
import XCTest
import TorusUtils
import XCTest

class AquaTest: XCTestCase {
var TORUS_TEST_EMAIL = "[email protected]"
Expand All @@ -22,14 +22,14 @@ class AquaTest: XCTestCase {
let verifierID: String = TORUS_TEST_EMAIL
let nodeDetails = try await fnd.getNodeDetails(verifier: verifier, verifierID: verifierID)
let val = try await torus.getPublicAddress(endpoints: nodeDetails.getTorusNodeEndpoints(), verifier: verifier, verifierId: verifierID)

XCTAssertEqual(val.finalKeyData!.evmAddress, "0xDfA967285AC699A70DA340F60d00DB19A272639d")
XCTAssertLessThan(val.metadata!.serverTimeOffset, 20)

XCTAssertEqual(val.oAuthKeyData!.evmAddress, "0xDfA967285AC699A70DA340F60d00DB19A272639d")
XCTAssertEqual(val.oAuthKeyData!.X, "4fc8db5d3fe164a3ab70fd6348721f2be848df2cc02fd2db316a154855a7aa7d")
XCTAssertEqual(val.oAuthKeyData!.Y, "f76933cbf5fe2916681075bb6cb4cde7d5f6b6ce290071b1b7106747d906457c")

XCTAssertEqual(val.finalKeyData!.evmAddress, "0xDfA967285AC699A70DA340F60d00DB19A272639d")
XCTAssertEqual(val.finalKeyData!.X, "4fc8db5d3fe164a3ab70fd6348721f2be848df2cc02fd2db316a154855a7aa7d")
XCTAssertEqual(val.finalKeyData!.Y, "f76933cbf5fe2916681075bb6cb4cde7d5f6b6ce290071b1b7106747d906457c")
Expand All @@ -45,7 +45,7 @@ class AquaTest: XCTestCase {
let nodeDetails = try await fnd.getNodeDetails(verifier: verifier, verifierID: verifierID)
let result1 = try await torus.getUserTypeAndAddress(endpoints: nodeDetails.getTorusNodeEndpoints(), verifier: verifier, verifierId: verifierID)
XCTAssertLessThan(result1.metadata!.serverTimeOffset, 20)

XCTAssertEqual(result1.oAuthKeyData!.evmAddress, "0xDfA967285AC699A70DA340F60d00DB19A272639d")
XCTAssertEqual(result1.oAuthKeyData!.X, "4fc8db5d3fe164a3ab70fd6348721f2be848df2cc02fd2db316a154855a7aa7d")
XCTAssertEqual(result1.oAuthKeyData!.Y, "f76933cbf5fe2916681075bb6cb4cde7d5f6b6ce290071b1b7106747d906457c")
Expand All @@ -63,9 +63,9 @@ class AquaTest: XCTestCase {
verifier = "tkey-google-aqua"
verifierID = "[email protected]"
let result2 = try await torus.getUserTypeAndAddress(endpoints: nodeDetails.getTorusNodeEndpoints(), verifier: verifier, verifierId: verifierID)

XCTAssertLessThan(result2.metadata!.serverTimeOffset, 20)

XCTAssertEqual(result2.oAuthKeyData!.evmAddress, "0x4ea5260fF85678A2a326D08DF9C44d1f559a5828")
XCTAssertEqual(result2.oAuthKeyData!.X, "0e6febe33a9d4eeb680cc6b63ff6237ad1971f27adcd7f104a3b1de18eda9337")
XCTAssertEqual(result2.oAuthKeyData!.Y, "a5a915561f3543688e71281a850b9ee10b9690f305d9e79028dfc8359192b82d")
Expand All @@ -79,13 +79,13 @@ class AquaTest: XCTestCase {
XCTAssertEqual(result2.metadata?.upgraded, false)
XCTAssertEqual(result2.metadata?.typeOfUser, .v2)
XCTAssertNotNil(result2.nodesData)

// 2/n user
verifierID = "[email protected]"
let result3 = try await torus.getUserTypeAndAddress(endpoints: nodeDetails.getTorusNodeEndpoints(), verifier: verifier, verifierId: verifierID)

XCTAssertLessThan(result2.metadata!.serverTimeOffset, 20)

XCTAssertEqual(result3.oAuthKeyData!.evmAddress, "0x4ce0D09C3989eb3cC9372cC27fa022D721D737dD")
XCTAssertEqual(result3.oAuthKeyData!.X, "e76d2f7fa2c0df324b4ab74629c3af47aa4609c35f1d2b6b90b77a47ab9a1281")
XCTAssertEqual(result3.oAuthKeyData!.Y, "b33b35148d72d357070f66372e07fec436001bdb15c098276b120b9ed64c1e5f")
Expand All @@ -106,7 +106,7 @@ class AquaTest: XCTestCase {
let verifierID: String = fakeEmail
let nodeDetails = try await fnd.getNodeDetails(verifier: verifier, verifierID: verifierID)
let data = try await torus.getPublicAddress(endpoints: nodeDetails.getTorusNodeEndpoints(), verifier: verifier, verifierId: verifierID)
//note: duplicated checks on next two lines in javascript
// note: duplicated checks on next two lines in javascript
XCTAssertNotNil(data.finalKeyData?.evmAddress)
XCTAssertNotNil(data.oAuthKeyData?.evmAddress)
XCTAssertEqual(data.metadata?.typeOfUser, .v1)
Expand All @@ -121,9 +121,9 @@ class AquaTest: XCTestCase {
let jwt = try! generateIdToken(email: verifierID)
let nodeDetails = try await fnd.getNodeDetails(verifier: verifier, verifierID: verifierID)
let data = try await torus.retrieveShares(endpoints: nodeDetails.getTorusNodeEndpoints(), indexes: nodeDetails.getTorusIndexes(), verifier: verifier, verifierParams: verifierParams, idToken: jwt)

XCTAssertLessThan(data.metadata.serverTimeOffset, 20)

XCTAssertEqual(data.finalKeyData.evmAddress, "0x9EBE51e49d8e201b40cAA4405f5E0B86d9D27195")
XCTAssertEqual(data.finalKeyData.X, "c7bcc239f0957bb05bda94757eb4a5f648339424b22435da5cf7a0f2b2323664")
XCTAssertEqual(data.finalKeyData.Y, "63795690a33e575ee12d832935d563c2b5f2e1b1ffac63c32a4674152f68cb3f")
Expand All @@ -145,11 +145,11 @@ class AquaTest: XCTestCase {
let verifierID: String = TORUS_TEST_EMAIL
let jwt = try! generateIdToken(email: TORUS_TEST_EMAIL)
let hashedIDToken = try KeyUtils.keccak256Data(jwt)

let nodeDetails = try await fnd.getNodeDetails(verifier: verifier, verifierID: verifierID)
let verifierParams = VerifierParams(verifier_id: verifierID, sub_verifier_ids: [TORUS_TEST_VERIFIER], verify_params: [VerifyParams(verifier_id: TORUS_TEST_EMAIL, idtoken: jwt)])

let verifierParams = VerifierParams(verifier_id: verifierID, sub_verifier_ids: [TORUS_TEST_VERIFIER], verify_params: [VerifyParams(verifier_id: TORUS_TEST_EMAIL, idtoken: jwt)])

let data = try await torus.retrieveShares(endpoints: nodeDetails.getTorusNodeEndpoints(), indexes: nodeDetails.getTorusIndexes(), verifier: verifier, verifierParams: verifierParams, idToken: hashedIDToken)
XCTAssertEqual(data.finalKeyData.evmAddress, "0x5b58d8a16fDA79172cd42Dc3068d5CEf26a5C81D")
XCTAssertEqual(data.finalKeyData.X, "37a4ac8cbef68e88bcec5909d9b6fffb539187365bb723f3d7bffe56ae80e31d")
Expand Down
4 changes: 2 additions & 2 deletions Tests/TorusUtilsTests/BaseTests/Lagrange.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import BigInt
import Foundation
import XCTest
@testable import TorusUtils
import XCTest

class LagrangeTest: XCTestCase {
var tu: TorusUtils!
Expand All @@ -27,6 +27,6 @@ class LagrangeTest: XCTestCase {
XCTAssertEqual(y, expectedY)
}
}

// TODO: Test other methods
}
27 changes: 13 additions & 14 deletions Tests/TorusUtilsTests/Celeste.swift
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import Foundation
import BigInt
import FetchNodeDetails
import XCTest
import Foundation
import TorusUtils
import BigInt
import XCTest

class CelesteTest: XCTestCase {
var TORUS_TEST_EMAIL = "[email protected]"
var TORUS_TEST_VERIFIER = "torus-test-health"
var TORUS_TEST_AGGREGATE_VERIFIER = "torus-test-health-aggregate"
var fnd: NodeDetailManager!
var torus: TorusUtils!

override func setUp() {
super.setUp()
fnd = NodeDetailManager(network: .legacy(.CELESTE))
torus = TorusUtils(params: TorusOptions(clientId: "YOUR_CLIENT_ID", network: .legacy(.CELESTE)))
}

func test_should_fetch_public_address() async throws {
let verifier = "tkey-google-celeste"
let verifierID = TORUS_TEST_EMAIL
let nodeDetails = try await fnd.getNodeDetails(verifier: verifier, verifierID: verifierID)
let val = try await torus.getPublicAddress(endpoints: nodeDetails.torusNodeEndpoints, verifier: verifier, verifierId: verifierID)

XCTAssertEqual(val.finalKeyData!.evmAddress, "0xC3115b9d6FaB99739b23DA9dfcBA47A4Ec4Cd113")
XCTAssertLessThan(val.metadata!.serverTimeOffset, 20)

XCTAssertEqual(val.finalKeyData!.evmAddress, "0xC3115b9d6FaB99739b23DA9dfcBA47A4Ec4Cd113")
XCTAssertEqual(val.finalKeyData!.X, "b89b9d66b247d7294a98616b95b7bfa1675aa85a1df4d89f2780283864f1b6e9")
XCTAssertEqual(val.finalKeyData!.Y, "65422a8ccd66e638899fc53497e468a9a0bf50d45c9cb85ae0ffcfc13f433ffb")
Expand All @@ -47,7 +47,7 @@ class CelesteTest: XCTestCase {

XCTAssertEqual(val.finalKeyData!.evmAddress, "0xC3115b9d6FaB99739b23DA9dfcBA47A4Ec4Cd113")
XCTAssertLessThan(val.metadata!.serverTimeOffset, 20)

XCTAssertEqual(val.oAuthKeyData!.evmAddress, "0xC3115b9d6FaB99739b23DA9dfcBA47A4Ec4Cd113")
XCTAssertEqual(val.oAuthKeyData!.X, "b89b9d66b247d7294a98616b95b7bfa1675aa85a1df4d89f2780283864f1b6e9")
XCTAssertEqual(val.oAuthKeyData!.Y, "65422a8ccd66e638899fc53497e468a9a0bf50d45c9cb85ae0ffcfc13f433ffb")
Expand All @@ -64,7 +64,7 @@ class CelesteTest: XCTestCase {

XCTAssertEqual(val.finalKeyData!.evmAddress, "0x8d69CE354DA39413f205FdC8680dE1F3FBBb36e2")
XCTAssertLessThan(val.metadata!.serverTimeOffset, 20)

XCTAssertEqual(val.oAuthKeyData!.evmAddress, "0xda4afB35493094Dd2C05b186Ca0FABAD96491B21")
XCTAssertEqual(val.oAuthKeyData!.X, "cfa646a2949ebe559205c5c407d734d1b6927f2ea5fbeabfcbc31ab9a985a336")
XCTAssertEqual(val.oAuthKeyData!.Y, "8f988eb8b59515293820aa38af172b153e8d25307db8d5f410407c20e062b6e6")
Expand All @@ -86,7 +86,7 @@ class CelesteTest: XCTestCase {

XCTAssertEqual(val.finalKeyData!.evmAddress, "0x8108c29976C458e76f797AD55A3715Ce80a3fe78")
XCTAssertLessThan(val.metadata!.serverTimeOffset, 20)

XCTAssertEqual(val.oAuthKeyData!.evmAddress, "0xc8c4748ec135196fb482C761da273C31Ec48B099")
XCTAssertEqual(val.oAuthKeyData!.X, "0cc857201e6c304dd893b243e323fe95982e5a99c0994cf902efa2432a672eb4")
XCTAssertEqual(val.oAuthKeyData!.Y, "37a2f53c250b3e1186e38ece3dfcbcb23e325913038703531831b96d3e7b54cc")
Expand Down Expand Up @@ -121,10 +121,9 @@ class CelesteTest: XCTestCase {
let nodeDetails = try await fnd.getNodeDetails(verifier: verifier, verifierID: verifierID)
let data = try await torus.retrieveShares(endpoints: nodeDetails.getTorusNodeEndpoints(), indexes: nodeDetails.getTorusIndexes(), verifier: verifier, verifierParams: verifierParams, idToken: jwt)


XCTAssertEqual(data.finalKeyData.evmAddress, "0x58420FB83971C4490D8c9B091f8bfC890D716617")
XCTAssertLessThan(data.metadata.serverTimeOffset, 20)

XCTAssertEqual(data.oAuthKeyData.evmAddress, "0x58420FB83971C4490D8c9B091f8bfC890D716617")
XCTAssertEqual(data.oAuthKeyData.X, "73b82ce0f8201a962636d404fe7a683f37c2267a9528576e1dac9964940add74")
XCTAssertEqual(data.oAuthKeyData.Y, "6d28c46c5385b90322bde74d6c5096e154eae2838399f4d6e8d752f7b0c449c1")
Expand All @@ -147,13 +146,13 @@ class CelesteTest: XCTestCase {
let jwt = try! generateIdToken(email: TORUS_TEST_EMAIL)
let hashedIDToken = try KeyUtils.keccak256Data(jwt)
let nodeDetails = try await fnd.getNodeDetails(verifier: verifier, verifierID: verifierID)

let verifierParams = VerifierParams(verifier_id: verifierID, sub_verifier_ids: [TORUS_TEST_VERIFIER], verify_params: [VerifyParams(verifier_id: verifierID, idtoken: jwt)])
let data = try await torus.retrieveShares(endpoints: nodeDetails.torusNodeEndpoints, indexes: nodeDetails.getTorusIndexes(), verifier: verifier, verifierParams: verifierParams, idToken: hashedIDToken)

XCTAssertEqual(data.finalKeyData.evmAddress, "0x535Eb1AefFAc6f699A2a1A5846482d7b5b2BD564")
XCTAssertLessThan(data.metadata.serverTimeOffset, 20)

XCTAssertEqual(data.oAuthKeyData.evmAddress, "0x535Eb1AefFAc6f699A2a1A5846482d7b5b2BD564")
XCTAssertEqual(data.oAuthKeyData.X, "df6eb11d52e76b388a44896e9442eda17096c2b67b0be957a4ba0b68a70111ca")
XCTAssertEqual(data.oAuthKeyData.Y, "bfd29ab1e97b3f7c444bb3e7ad0acb39d72589371387436c7d623d1e83f3d6eb")
Expand Down
Loading

0 comments on commit 0b5e898

Please sign in to comment.