SPB Git

Repository Public

Board.swift 5.4 KB 122 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : QYVORA7// File             : security/tests/Tests/ConformanceAuditTests/Board.swift8// Module           : Tests9// 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 AuditSupport42import CommonCrypto43import CryptoKit44import Foundation45import QyvoraMath46import Testing4748/// Pass/fail counters keyed by (algorithm, parameter set, check); written to the audit records at the end.49struct Board {50    struct Key: Hashable {51        let algorithm: String, parameterSet: String, name: String52    }5354    var rows: [Key: (cases: Int, failed: Int, skipped: Int)] = [:]55    var failures: [String] = []56    let source: String5758    init(source: String) { self.source = source }5960    mutating func add(_ algorithm: String, _ parameterSet: String, _ name: String, ok: Bool, id: Any? = nil) {61        var r = rows[Key(algorithm: algorithm, parameterSet: parameterSet, name: name)] ?? (0, 0, 0)62        r.cases += 163        if !ok {64            r.failed += 165            if failures.count < 25 { failures.append("\(algorithm) \(parameterSet) \(name) tcId \(id ?? "?")") }66        }67        rows[Key(algorithm: algorithm, parameterSet: parameterSet, name: name)] = r68    }6970    mutating func skip(_ algorithm: String, _ parameterSet: String, _ name: String) {71        var r = rows[Key(algorithm: algorithm, parameterSet: parameterSet, name: name)] ?? (0, 0, 0)72        r.skipped += 173        rows[Key(algorithm: algorithm, parameterSet: parameterSet, name: name)] = r74    }7576    var cases: Int { rows.values.reduce(0) { $0 + $1.cases } }77    var failed: Int { rows.values.reduce(0) { $0 + $1.failed } }7879    /// Records every row and asserts that nothing failed and that at least `minimum` cases ran.80    func finish(minimum: Int = 1, note: String = "") {81        for (k, r) in rows.sorted(by: { ($0.key.algorithm, $0.key.parameterSet, $0.key.name) < ($1.key.algorithm, $1.key.parameterSet, $1.key.name) }) {82            AuditRecorder.record(83                area: "conformance", name: k.name, algorithm: k.algorithm, parameterSet: k.parameterSet, oracle: source,84                cases: r.cases, failed: r.failed, skipped: r.skipped, note: note)85        }86        #expect(failed == 0, "\(source): \(failures.joined(separator: " | "))")87        #expect(cases >= minimum, "\(source): only \(cases) cases replayed, expected at least \(minimum)")88    }89}9091/// Digests used only to prepare inputs of HashML-DSA vectors (the SHA-2 pre-hash is computed by the caller in the92/// engine's API). They come from system libraries, or from Python's hashlib for SHA-512/t which neither CryptoKit93/// nor CommonCrypto provides.94enum ReferenceDigest {95    static func sha2(_ name: String, _ m: Bytes) -> Bytes? {96        switch name {97        case "SHA2-224":98            var out = Bytes(repeating: 0, count: Int(CC_SHA224_DIGEST_LENGTH))99            _ = CC_SHA224(m, CC_LONG(m.count), &out)100            return out101        case "SHA2-256": return Bytes(CryptoKit.SHA256.hash(data: m))102        case "SHA2-384": return Bytes(CryptoKit.SHA384.hash(data: m))103        case "SHA2-512": return Bytes(CryptoKit.SHA512.hash(data: m))104        default: return nil105        }106    }107108    /// hashlib digests for a batch of messages (`sha512_224`, `sha512_256`); nil when python3 is unavailable.109    static func hashlib(_ algorithm: String, _ messages: [Bytes]) -> [Bytes]? {110        let script = """111            import sys, json, hashlib112            a = sys.argv[1]113            print(json.dumps([hashlib.new(a, bytes.fromhex(m)).hexdigest() for m in json.load(sys.stdin)]))114            """115        guard let input = try? JSONSerialization.data(withJSONObject: messages.map { Hex.encode($0) }),116            let out = try? Oracle.run(URL(fileURLWithPath: "/usr/bin/env"), ["python3", "-I", "-c", script, algorithm], input: input),117            let list = try? JSONSerialization.jsonObject(with: out) as? [String], list.count == messages.count118        else { return nil }119        return list.compactMap { Hex.decode($0) }120    }121}122
Files main
Menu

Theme