Skip to content

Commit

Permalink
swift lint
Browse files Browse the repository at this point in the history
  • Loading branch information
pacu committed Jul 2, 2024
1 parent f69e755 commit 9af36cd
Show file tree
Hide file tree
Showing 13 changed files with 292 additions and 470 deletions.
72 changes: 72 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# By default, SwiftLint uses a set of sensible default rules you can adjust:
disabled_rules: # rule identifiers turned on by default to exclude from running
- colon
- comma
- control_statement
opt_in_rules: # some rules are turned off by default, so you need to opt-in
- empty_count # find all the available rules by running: `swiftlint rules`

# Alternatively, specify all rules explicitly by uncommenting this option:
# only_rules: # delete `disabled_rules` & `opt_in_rules` if using this
# - empty_parameters
# - vertical_whitespace

analyzer_rules: # rules run by `swiftlint analyze`
- explicit_self

# Case-sensitive paths to include during linting. Directory paths supplied on the
# command line will be ignored.
included:
- FrostSwift/Sources/FrostSwift
excluded: # case-sensitive paths to ignore during linting. Takes precedence over `included`
- Carthage
- Pods
- .build
- Sources/FrostSwiftFFI

# If true, SwiftLint will not fail if no lintable files are found.
allow_zero_lintable_files: false

# If true, SwiftLint will treat all warnings as errors.
strict: false

# The path to a baseline file, which will be used to filter out detected violations.
baseline: Baseline.json

# The path to save detected violations to as a new baseline.
write_baseline: Baseline.json

# configurable rules can be customized from this configuration file
# binary rules can set their severity level
force_cast: warning # implicitly
force_try:
severity: warning # explicitly
# rules that have both warning and error levels, can set just the warning level
# implicitly
line_length: 110
# they can set both implicitly with an array
type_body_length:
- 300 # warning
- 400 # error
# or they can set both explicitly
file_length:
warning: 500
error: 1200
# naming rules can set warnings/errors for min_length and max_length
# additionally they can set excluded names
type_name:
min_length: 4 # only warning
max_length: # warning and error
warning: 50
error: 50
excluded: iPhone # excluded via string
allowed_symbols: ["_"] # these are allowed in type names
identifier_name:
min_length: # only min_length
error: 3 # only error
excluded: # excluded via string array
- i
- id
- URL
- GlobalAPIKey
reporter: "xcode" # reporter type (xcode, json, csv, checkstyle, codeclimate, junit, html, emoji, sonarqube, markdown, github-actions-logging, summary)
19 changes: 9 additions & 10 deletions FrostSwift/Sources/FrostSwift/Coordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import Foundation
import FrostSwiftFFI
enum FROSTCoordinatorError: Error {

case alreadyReceivedCommitmentFromIdentifier(Identifier)
case alreadyReceivedSignatureShareFromIdentifier(Identifier)
case repeatedCommitmentFromIdentifier(Identifier)
case repeatedSignatureShareFromIdentifier(Identifier)
case incorrectNumberOfCommitments(min: UInt16, max: UInt16, found: UInt16)
case signingPackageAlreadyCreated
case signingPackageMissing
Expand Down Expand Up @@ -78,27 +78,26 @@ public protocol FROSTCoordinator: Actor {
/// - throws: ``FROSTCoordinatorError.signingPackageAlreadyCreated`` if this function was
/// called already or ``FROSTCoordinatorError.incorrectNumberOfCommitments``
/// when the number of commitments gathered is less or more than the specified by the ``Configuration``
func createSigningPackage() async throws -> Round2Configuration
func createSigningPackage() async throws -> Round2Configuration
/// Receives a ``SignatureShare`` from a ``SigningParticipant``.
/// - throws: ``FROSTCoordinatorError.alreadyReceivedSignatureShareFromIdentifier``
/// when the same participant sends the same share repeatedly
func receive(signatureShare: SignatureShare) async throws
/// Aggregates all shares and creates the FROST ``Signature``
/// - throws: several things can go wrong here. 🥶
func aggregate() async throws -> Signature
func aggregate() async throws -> Signature
/// Verify a ``Signature`` with the ``PublicKeyPackage`` of this coordinator.
func verify(signature: Signature) async throws
}


/// A signature scheme coordinator that does not participate in the signature scheme
public actor NonSigningCoordinator: FROSTCoordinator {
public let configuration: Configuration
public let publicKeyPackage: PublicKeyPackage
public let message: Message
public var round2Config: Round2Configuration?
var commitments: [Identifier : SigningCommitments] = [:]
var signatureShares: [Identifier : SignatureShare] = [:]
var commitments: [Identifier: SigningCommitments] = [:]
var signatureShares: [Identifier: SignatureShare] = [:]

public init(configuration: Configuration, publicKeyPackage: PublicKeyPackage, message: Message) throws {
self.configuration = configuration
Expand All @@ -109,7 +108,7 @@ public actor NonSigningCoordinator: FROSTCoordinator {
public func receive(commitment: SigningCommitments) throws {
// TODO: validate that the commitment belongs to a known identifier
guard commitments[commitment.identifier] == nil else {
throw FROSTCoordinatorError.alreadyReceivedCommitmentFromIdentifier(commitment.identifier)
throw FROSTCoordinatorError.repeatedCommitmentFromIdentifier(commitment.identifier)
}

self.commitments[commitment.identifier] = commitment
Expand Down Expand Up @@ -146,7 +145,7 @@ public actor NonSigningCoordinator: FROSTCoordinator {
public func receive(signatureShare: SignatureShare) throws {
// TODO: validate that the commitment belongs to a known identifier
guard self.signatureShares[signatureShare.identifier] == nil else {
throw FROSTCoordinatorError.alreadyReceivedSignatureShareFromIdentifier(signatureShare.identifier)
throw FROSTCoordinatorError.repeatedSignatureShareFromIdentifier(signatureShare.identifier)
}

self.signatureShares[signatureShare.identifier] = signatureShare
Expand Down Expand Up @@ -222,7 +221,7 @@ public actor SigningCoordinator: FROSTCoordinator {

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

let signingParticipant = SigningParticipant(
keyPackage: keyPackage,
publicKey: publicKeyPackage
Expand Down
3 changes: 0 additions & 3 deletions FrostSwift/Sources/FrostSwift/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
//
// Created by Pacu in 2024.
//


import Foundation
import FrostSwiftFFI
Expand All @@ -16,5 +15,3 @@ public enum FrostError: Error {
case malformedIdentifier
case otherError(FrostSwiftFFI.FrostError)
}


20 changes: 14 additions & 6 deletions FrostSwift/Sources/FrostSwift/FFIConversion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
import Foundation
import FrostSwiftFFI


extension Configuration {
func intoFFIConfiguration() -> FrostSwiftFFI.Configuration {
FrostSwiftFFI.Configuration(minSigners: self.minSigners, maxSigners: self.maxSigners, secret: self.secret ?? Data())
FrostSwiftFFI.Configuration(
minSigners: self.minSigners,
maxSigners: self.maxSigners,
secret: self.secret ?? Data()
)
}
}

Expand All @@ -29,11 +32,16 @@ extension Identifier {

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

var keys = [Identifier : SecretShare]()

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

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

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



enum FrostSwift {

static func frost() -> String {
"❄️"
}
Expand All @@ -24,7 +21,7 @@ public struct PublicKeyPackage {

/// All the participants involved in this KeyPackage
public var participants: [Identifier] {
package.verifyingShares.keys.map{ $0.toIdentifier() }
package.verifyingShares.keys.map { $0.toIdentifier() }
}

public func verifyingShare(for participant: Identifier) -> VerifyingShare? {
Expand All @@ -34,12 +31,19 @@ public struct PublicKeyPackage {
return VerifyingShare(share: share)
}

public func verify(message: Message, signature: Signature, randomizer: Randomizer?) throws {

public func verify(
message: Message,
signature: Signature,
randomizer: Randomizer?
) throws {
if let randomizer = randomizer {
try verifyRandomizedSignature(randomizer: randomizer.randomizer, message: message, signature: signature.signature, pubkey: self.package)
try verifyRandomizedSignature(
randomizer: randomizer.randomizer,
message: message,
signature: signature.signature,
pubkey: self.package
)
}

}
}
/// Identifier of a signing participant of a FROST signature scheme.
Expand All @@ -60,7 +64,7 @@ public struct Identifier: Hashable {
return nil
}
}

/// constructs a JSON-formatted string from the given string to create an identifier
public init?(identifier: String) {
if let id = try? identifierFromString(string: identifier) {
Expand All @@ -80,7 +84,10 @@ public struct Identifier: Hashable {

public func toString() throws -> String {
do {
let json = try JSONDecoder().decode(String.self, from: id.data.data(using: .utf8) ?? Data())
let json = try JSONDecoder().decode(
String.self,
from: id.data.data(using: .utf8) ?? Data()
)

return json
} catch {
Expand All @@ -102,7 +109,6 @@ public struct VerifyingShare {
public struct RandomizedParams {
let params: FrostSwiftFFI.FrostRandomizedParams


init(params: FrostSwiftFFI.FrostRandomizedParams) {
self.params = params
}
Expand All @@ -125,10 +131,6 @@ public struct RandomizedParams {

public struct Randomizer {
let randomizer: FrostRandomizer

init(randomizer: FrostRandomizer) {
self.randomizer = randomizer
}
}

public struct VerifyingKey {
Expand Down Expand Up @@ -164,10 +166,6 @@ public struct KeyPackage {
public struct SecretShare {
let share: FrostSecretKeyShare

init(share: FrostSecretKeyShare) {
self.share = share
}

/// Verifies the Secret share and creates a `KeyPackage`
public func verifyAndGetKeyPackage() throws -> KeyPackage {
let package = try verifyAndGetKeyPackageFrom(secretShare: self.share)
Expand Down Expand Up @@ -198,10 +196,6 @@ public struct SigningCommitments {
/// memory for a later use
public struct SigningNonces {
let nonces: FrostSigningNonces

init(nonces: FrostSigningNonces) {
self.nonces = nonces
}
}

/// Signature share produced by a given participant of the signature scheme
Expand All @@ -228,10 +222,6 @@ public struct SignatureShare: Equatable {
/// authenticated and encrypted channel.
public struct SigningPackage: Equatable {
let package: FrostSigningPackage

init(package: FrostSigningPackage) {
self.package = package
}
}
/// Signature produced by aggregating the `SignatureShare`s of the
/// different _t_ participants of a threshold signature.
Expand All @@ -241,4 +231,3 @@ public struct Signature: Equatable, Hashable {

public var data: Data { signature.data }
}

4 changes: 2 additions & 2 deletions FrostSwift/Sources/FrostSwift/SigningParticipant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ public class SigningParticipant {
var round2Config: Round2Configuration?

public var identifier: Identifier { keyPackage.identifier }

public init(keyPackage: KeyPackage, publicKey: PublicKeyPackage) {
self.keyPackage = keyPackage
self.publicKey = publicKey
}

public func commit() throws -> SigningCommitments {
let commitments = try generateNoncesAndCommitments(keyPackage: keyPackage.package)

let nonces = SigningNonces(nonces: commitments.nonces)
self.signingNonces = nonces

Expand Down
21 changes: 9 additions & 12 deletions FrostSwift/Sources/FrostSwift/TrustedDealer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,7 @@ public struct TrustedDealerCoordinator {

public struct KeyGeneration {
public let publicKeyPackage: PublicKeyPackage
public let secretShares: [Identifier : SecretShare]

init(publicKeyPackage: PublicKeyPackage, secretShares: [Identifier : SecretShare]) {
self.publicKeyPackage = publicKeyPackage
self.secretShares = secretShares
}
public let secretShares: [Identifier: SecretShare]
}

public let configuration: Configuration
Expand All @@ -52,20 +47,22 @@ public struct TrustedDealerCoordinator {

public func generateKeys() throws -> KeyGeneration {
let keys = try trustedDealerKeygenFrom(configuration: configuration.intoFFIConfiguration())

return keys.toKeyGeneration()
}

public func generateKeys(with identifiers: [Identifier]) throws -> KeyGeneration {
public func generateKeys(with identifiers: [Identifier]) throws -> KeyGeneration {
guard identifiers.count == configuration.maxSigners else {
throw FrostError.invalidConfiguration
}

let keys = try trustedDealerKeygenWithIdentifiers(configuration: self.configuration.intoFFIConfiguration(), participants: ParticipantList(identifiers: identifiers.map({ $0.toParticipantIdentifier()})))
let keys = try trustedDealerKeygenWithIdentifiers(
configuration: self.configuration.intoFFIConfiguration(),
participants: ParticipantList(
identifiers: identifiers.map { $0.toParticipantIdentifier()}
)
)

return keys.toKeyGeneration()
}
}



Loading

0 comments on commit 9af36cd

Please sign in to comment.