Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : QYVORA7// File : Sources/QyvoraBenchmarks/Benchmark.swift8// Module : Qyvora Benchmarks9// Repository : https://git.spboucher.ai/qyvora10// Project Website : Not specified11// Created : 2026-12-0412// Last Modified : 2026-12-0413//14// Author : Simon-Pierre Boucher15// Position : Professor of Finance16// Institution : Université du Québec en Outaouais (UQO)17// Organization : SP Boucher18// Website : https://spboucher.ai19// Git : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development : dev@spboucher.ai23// Support : support@spboucher.ai24// Security/Admin : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License : None specified (no license file in this repository)33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import Foundation4243/// Family of a benchmark; the application shows one section per family.44public enum BenchmarkFamily: String, Codable, Sendable, CaseIterable, Identifiable {45 case mlkem46 case mldsa47 case slhdsa48 case hash49 case symmetric50 case kdf51 case ntt52 case secp256k15354 public var id: String { rawValue }5556 public var title: String {57 switch self {58 case .mlkem: return "ML-KEM (FIPS 203)"59 case .mldsa: return "ML-DSA (FIPS 204)"60 case .slhdsa: return "SLH-DSA (FIPS 205)"61 case .hash: return "Hash functions"62 case .symmetric: return "Symmetric encryption and MAC"63 case .kdf: return "Key derivation"64 case .ntt: return "Number-theoretic transforms"65 case .secp256k1: return "secp256k1"66 }67 }68}6970/// How the timing of one operation is presented.71public enum BenchmarkMetric: String, Codable, Sendable {72 /// Time of one operation (key generation, signature, ...).73 case time74 /// Bytes processed per second; `unitsPerOperation` is the number of bytes of one operation.75 case throughput76 /// Items per second; `unitsPerOperation` is the number of items of one operation.77 case rate78 /// Time per item; `unitsPerOperation` is the number of items of one operation.79 case timePerItem80}8182/// Sizes in bytes taken from the parameter set, shown next to the timings.83public struct BenchmarkSizes: Codable, Sendable, Equatable {84 public var publicKey: Int?85 public var privateKey: Int?86 /// Signature or ciphertext size.87 public var output: Int?88 /// "signature" or "ciphertext".89 public var outputLabel: String?9091 public init(publicKey: Int? = nil, privateKey: Int? = nil, output: Int? = nil, outputLabel: String? = nil) {92 self.publicKey = publicKey93 self.privateKey = privateKey94 self.output = output95 self.outputLabel = outputLabel96 }97}9899/// Relative cost of one operation; decides how many samples the runner insists on.100public enum BenchmarkCost: String, Codable, Sendable {101 /// Microseconds to a few milliseconds: the runner fills the time budget.102 case standard103 /// Hundreds of milliseconds or more (SLH-DSA "s" signatures, Argon2id): few samples, and the104 /// calibration run counts as a sample when it already exceeds the time budget.105 case slow106}107108/// The measured closure. Created by `Benchmark.prepare` on the thread that runs it; not Sendable.109public typealias BenchmarkBody = () throws -> Void110111/// Descriptor of one benchmark. `prepare` builds the inputs (keys, messages, buffers) outside the112/// timed region and returns the closure to time.113public struct Benchmark: Sendable, Identifiable {114 /// Stable identifier, e.g. `ml-kem-768.encaps`.115 public let id: String116 public let family: BenchmarkFamily117 /// Algorithm or parameter set, e.g. `ML-KEM-768`.118 public let algorithm: String119 /// Operation, e.g. `encaps`.120 public let operation: String121 public let metric: BenchmarkMetric122 /// Bytes (throughput) or items (rate, timePerItem) processed by one call of the body; 1 for `time`.123 public let unitsPerOperation: Double124 /// Label of the item for `rate` and `timePerItem`, e.g. "permutation", "transform".125 public let unitLabel: String?126 public let sizes: BenchmarkSizes?127 public let cost: BenchmarkCost128 public let prepare: @Sendable () throws -> BenchmarkBody129130 public var displayName: String { "\(algorithm) \(operation)" }131132 public init(133 id: String, family: BenchmarkFamily, algorithm: String, operation: String,134 metric: BenchmarkMetric = .time, unitsPerOperation: Double = 1, unitLabel: String? = nil,135 sizes: BenchmarkSizes? = nil, cost: BenchmarkCost = .standard,136 prepare: @escaping @Sendable () throws -> BenchmarkBody137 ) {138 self.id = id139 self.family = family140 self.algorithm = algorithm141 self.operation = operation142 self.metric = metric143 self.unitsPerOperation = unitsPerOperation144 self.unitLabel = unitLabel145 self.sizes = sizes146 self.cost = cost147 self.prepare = prepare148 }149150 /// Case-insensitive match on the identifier, the display name and the family title.151 public func matches(_ filter: String) -> Bool {152 let needle = filter.lowercased()153 if needle.isEmpty { return true }154 return id.lowercased().contains(needle) || displayName.lowercased().contains(needle)155 || family.rawValue.contains(needle) || family.title.lowercased().contains(needle)156 }157}158159public extension Array where Element == Benchmark {160 /// Benchmarks matching `filter` (nil or empty keeps everything). See `Benchmark.matches`.161 func filtered(by filter: String?) -> [Benchmark] {162 guard let filter, !filter.isEmpty else { return self }163 return self.filter { $0.matches(filter) }164 }165}166