Skip to content

Commit

Permalink
refactor: remove duplicate combination code
Browse files Browse the repository at this point in the history
  • Loading branch information
metalurgical committed Oct 14, 2023
1 parent 828e3c6 commit 57da155
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 36 deletions.
2 changes: 1 addition & 1 deletion Sources/TorusUtils/Extensions/TorusUtils+extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ extension TorusUtils {

private func reconstructKey(decryptedShares: [Int: String], thresholdPublicKey: KeyAssignment.PublicKey) throws -> String? {
// run lagrange interpolation on all subsets, faster in the optimistic scenario than berlekamp-welch due to early exit
let allCombis = kCombinations(s: decryptedShares.count, k: 3)
let allCombis = combinations(elements: Array(0..<decryptedShares.count), k: 3)
var returnedKey: String?

for j in 0 ..< allCombis.count {
Expand Down
35 changes: 0 additions & 35 deletions Sources/TorusUtils/Helpers/Common.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,3 @@ func generateAddressFromPubKey(publicKeyX: String, publicKeyY: String) -> String
let ethAddrlower = "0x" + ethAddrData.toHexString()
return ethAddrlower.toChecksumAddress()
}

func kCombinations(s: Any, k: Int) -> [[Int]] {
var set: [Int]
if let number = s as? Int {
set = Array(0 ..< number)
} else if let numbers = s as? [Int] {
set = numbers
} else {
return []
}

if k > set.count || k <= 0 {
return []
}

if k == set.count {
return [set]
}

if k == 1 {
return set.map { [$0] }
}

var combs: [[Int]] = []
var tailCombs: [[Int]] = []

for i in 0 ... set.count - k {
tailCombs = kCombinations(s: Array(set[(i + 1)...]), k: k - 1)
for j in 0 ..< tailCombs.count {
combs.append([set[i]] + tailCombs[j])
}
}

return combs
}

0 comments on commit 57da155

Please sign in to comment.