Skip to content

Commit

Permalink
Swift Format
Browse files Browse the repository at this point in the history
  • Loading branch information
pacu committed Jul 2, 2024
1 parent 9af36cd commit 4c4b403
Show file tree
Hide file tree
Showing 15 changed files with 307 additions and 344 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import XCTest

final class FrostCompanionTests: XCTestCase {

override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
}
Expand All @@ -31,5 +30,4 @@ final class FrostCompanionTests: XCTestCase {
// Put the code you want to measure the time of here.
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import XCTest

final class FrostCompanionUITests: XCTestCase {

override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import XCTest

final class FrostCompanionUITestsLaunchTests: XCTestCase {

override class var runsForEachTargetApplicationUIConfiguration: Bool {
true
}
Expand Down
71 changes: 34 additions & 37 deletions FrostSwift/Sources/FrostSwift/Coordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

import Foundation
import FrostSwiftFFI
enum FROSTCoordinatorError: Error {

enum FROSTCoordinatorError: Error {
case repeatedCommitmentFromIdentifier(Identifier)
case repeatedSignatureShareFromIdentifier(Identifier)
case incorrectNumberOfCommitments(min: UInt16, max: UInt16, found: UInt16)
Expand Down Expand Up @@ -41,7 +41,7 @@ enum FROSTCoordinatorError: Error {
self = .signatureShareDeserializationError
case .PublicKeyPackageDeserializationError:
self = .publicKeyPackageDeserializationError
case .SignatureShareAggregationFailed(message: let message):
case let .SignatureShareAggregationFailed(message: message):
self = .signatureShareAggregationFailed(message: message)
case .InvalidRandomizer:
self = .invalidRandomizer
Expand Down Expand Up @@ -111,44 +111,44 @@ public actor NonSigningCoordinator: FROSTCoordinator {
throw FROSTCoordinatorError.repeatedCommitmentFromIdentifier(commitment.identifier)
}

self.commitments[commitment.identifier] = commitment
commitments[commitment.identifier] = commitment
}

public func createSigningPackage() throws -> Round2Configuration {
guard self.round2Config?.signingPackage == nil else {
guard round2Config?.signingPackage == nil else {
throw FROSTCoordinatorError.signingPackageAlreadyCreated
}

try validateNumberOfCommitments()

let package = SigningPackage(
package: try newSigningPackage(
message: self.message,
commitments: self.commitments.values.map { $0.commitment }
let package = try SigningPackage(
package: newSigningPackage(
message: message,
commitments: commitments.values.map { $0.commitment }
)
)

let randomizedParams = try RandomizedParams(
publicKey: self.publicKeyPackage,
publicKey: publicKeyPackage,
signingPackage: package
)

let randomizer = try randomizedParams.randomizer()

let config = Round2Configuration(signingPackage: package, randomizer: randomizer)
self.round2Config = config
round2Config = config

return config
}

/// receives the signature share from a partipicant
public func receive(signatureShare: SignatureShare) throws {
// TODO: validate that the commitment belongs to a known identifier
guard self.signatureShares[signatureShare.identifier] == nil else {
guard signatureShares[signatureShare.identifier] == nil else {
throw FROSTCoordinatorError.repeatedSignatureShareFromIdentifier(signatureShare.identifier)
}

self.signatureShares[signatureShare.identifier] = signatureShare
signatureShares[signatureShare.identifier] = signatureShare
}

public func aggregate() throws -> Signature {
Expand All @@ -161,35 +161,35 @@ public actor NonSigningCoordinator: FROSTCoordinator {

let signature = try FrostSwiftFFI.aggregate(
signingPackage: round2config.signingPackage.package,
signatureShares: self.signatureShares.values.map { $0.share },
pubkeyPackage: self.publicKeyPackage.package,
signatureShares: signatureShares.values.map { $0.share },
pubkeyPackage: publicKeyPackage.package,
randomizer: randomizer
)

return Signature(signature: signature)
}

public func verify(signature: Signature) throws {
public func verify(signature _: Signature) throws {
throw FrostError.invalidSignature
}

func round2ConfigPresent() throws -> Round2Configuration {
guard let config = self.round2Config else {
guard let config = round2Config else {
throw FROSTCoordinatorError.signingPackageMissing
}

return config
}

func validateNumberOfCommitments() throws {
guard commitments.count >= configuration.minSigners &&
commitments.count <= configuration.maxSigners
guard commitments.count >= configuration.minSigners &&
commitments.count <= configuration.maxSigners
else {
throw FROSTCoordinatorError.incorrectNumberOfCommitments(
min: configuration.minSigners,
max: configuration.maxSigners,
found: UInt16(commitments.count)
)
min: configuration.minSigners,
max: configuration.maxSigners,
found: UInt16(commitments.count)
)
}
}
}
Expand All @@ -212,16 +212,15 @@ public actor SigningCoordinator: FROSTCoordinator {
self.configuration = configuration
self.publicKeyPackage = publicKeyPackage
self.message = message
self.nonSigningCoordinator = try NonSigningCoordinator(
nonSigningCoordinator = try NonSigningCoordinator(
configuration: configuration,
publicKeyPackage: publicKeyPackage,
message: message
)
self.signingParticipant = signingParticipant

}
public init(configuration: Configuration, publicKeyPackage: PublicKeyPackage, keyPackage: KeyPackage, message: Message) throws {

public init(configuration: Configuration, publicKeyPackage: PublicKeyPackage, keyPackage: KeyPackage, message: Message) throws {
let signingParticipant = SigningParticipant(
keyPackage: keyPackage,
publicKey: publicKeyPackage
Expand All @@ -233,7 +232,6 @@ public actor SigningCoordinator: FROSTCoordinator {
signingParticipant: signingParticipant,
message: message
)

}

public func receive(commitment: SigningCommitments) async throws {
Expand All @@ -242,36 +240,35 @@ public actor SigningCoordinator: FROSTCoordinator {

public func createSigningPackage() async throws -> Round2Configuration {
// sends its own commitment before creating the signign package
let commitment = try self.signingParticipant.commit()
try await self.nonSigningCoordinator.receive(commitment: commitment)
let commitment = try signingParticipant.commit()
try await nonSigningCoordinator.receive(commitment: commitment)

// create the signing package
let round2Config = try await self.nonSigningCoordinator.createSigningPackage()
let round2Config = try await nonSigningCoordinator.createSigningPackage()
self.round2Config = round2Config
return round2Config
}

public func receive(signatureShare: SignatureShare) async throws {
try await self.nonSigningCoordinator.receive(signatureShare: signatureShare)
try await nonSigningCoordinator.receive(signatureShare: signatureShare)
}

public func aggregate() async throws -> Signature {
let round2Config = try await self.nonSigningCoordinator.round2ConfigPresent()
let round2Config = try await nonSigningCoordinator.round2ConfigPresent()
// create own signature share
self.signingParticipant.receive(round2Config: round2Config)
let signatureShare = try self.signingParticipant.sign()
signingParticipant.receive(round2Config: round2Config)
let signatureShare = try signingParticipant.sign()

// sends its own share before creating the signature
try await self.nonSigningCoordinator.receive(signatureShare: signatureShare)
try await nonSigningCoordinator.receive(signatureShare: signatureShare)

// produce signature by aggregating all shares.
let signature = try await self.nonSigningCoordinator.aggregate()
let signature = try await nonSigningCoordinator.aggregate()

return signature

}

public func verify(signature: Signature) async throws {
try await self.nonSigningCoordinator.verify(signature: signature)
try await nonSigningCoordinator.verify(signature: signature)
}
}
3 changes: 1 addition & 2 deletions FrostSwift/Sources/FrostSwift/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
//
//
// Created by Pacu in 2024.
//
//

import Foundation
import FrostSwiftFFI

public enum FrostError: Error {

case invalidConfiguration
case invalidSignature
case malformedIdentifier
Expand Down
17 changes: 8 additions & 9 deletions FrostSwift/Sources/FrostSwift/FFIConversion.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// FFIConversion.swift
//
//
//
// Created by Pacu on 26-06-2024.
//
Expand All @@ -11,9 +11,9 @@ import FrostSwiftFFI
extension Configuration {
func intoFFIConfiguration() -> FrostSwiftFFI.Configuration {
FrostSwiftFFI.Configuration(
minSigners: self.minSigners,
maxSigners: self.maxSigners,
secret: self.secret ?? Data()
minSigners: minSigners,
maxSigners: maxSigners,
secret: secret ?? Data()
)
}
}
Expand All @@ -26,21 +26,20 @@ extension ParticipantIdentifier {

extension Identifier {
func toParticipantIdentifier() -> ParticipantIdentifier {
self.id
id
}
}

extension TrustedKeyGeneration {
func toKeyGeneration() -> TrustedDealerCoordinator.KeyGeneration {

var keys = [Identifier: SecretShare]()

self.secretShares.forEach {
keys[$0.key.toIdentifier()] = SecretShare(share: $0.value)
for secretShare in secretShares {
keys[secretShare.key.toIdentifier()] = SecretShare(share: secretShare.value)
}

return TrustedDealerCoordinator.KeyGeneration(
publicKeyPackage: PublicKeyPackage(package: self.publicKeyPackage),
publicKeyPackage: PublicKeyPackage(package: publicKeyPackage),
secretShares: keys
)
}
Expand Down
19 changes: 10 additions & 9 deletions FrostSwift/Sources/FrostSwift/FROST.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import FrostSwiftFFI
import Foundation
import FrostSwiftFFI

enum FrostSwift {
static func frost() -> String {
Expand Down Expand Up @@ -41,11 +41,12 @@ public struct PublicKeyPackage {
randomizer: randomizer.randomizer,
message: message,
signature: signature.signature,
pubkey: self.package
pubkey: package
)
}
}
}

/// Identifier of a signing participant of a FROST signature scheme.
/// an identifier is unique within a signature scheme.
/// - Note: a participant that is the same actor may (and most probably will) have
Expand All @@ -54,7 +55,7 @@ public struct Identifier: Hashable {
let id: ParticipantIdentifier

init(participant: ParticipantIdentifier) {
self.id = participant
id = participant
}

public init?(with scalar: UInt16) {
Expand Down Expand Up @@ -114,15 +115,15 @@ public struct RandomizedParams {
}

public init(publicKey: PublicKeyPackage, signingPackage: SigningPackage) throws {
self.params = try randomizedParamsFromPublicKeyAndSigningPackage(
params = try randomizedParamsFromPublicKeyAndSigningPackage(
publicKey: publicKey.package,
signingPackage: signingPackage.package
)
}

public func randomizer() throws -> Randomizer {
Randomizer(
randomizer: try FrostSwiftFFI.randomizerFromParams(
try Randomizer(
randomizer: FrostSwiftFFI.randomizerFromParams(
randomizedParams: params
)
)
Expand All @@ -141,7 +142,6 @@ public struct VerifyingKey {
}

public var asString: String { key }

}

/// _Secret_ key package of a given signing participant.
Expand All @@ -156,7 +156,7 @@ public struct KeyPackage {
}

public var identifier: Identifier {
self.package.identifier.toIdentifier()
package.identifier.toIdentifier()
}
}

Expand All @@ -168,7 +168,7 @@ public struct SecretShare {

/// Verifies the Secret share and creates a `KeyPackage`
public func verifyAndGetKeyPackage() throws -> KeyPackage {
let package = try verifyAndGetKeyPackageFrom(secretShare: self.share)
let package = try verifyAndGetKeyPackageFrom(secretShare: share)

return KeyPackage(package: package)
}
Expand Down Expand Up @@ -223,6 +223,7 @@ public struct SignatureShare: Equatable {
public struct SigningPackage: Equatable {
let package: FrostSigningPackage
}

/// Signature produced by aggregating the `SignatureShare`s of the
/// different _t_ participants of a threshold signature.
/// - note: to validate a signature use the `PublicKeyPackage` method.
Expand Down
Loading

0 comments on commit 4c4b403

Please sign in to comment.