SPB Git

Repository Public

HarnessSelfTests.swift 4.3 KB 93 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : QYVORA7// File             : security/tests/Tests/DifferentialAuditTests/HarnessSelfTests.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 Foundation43import QyvoraMath44import QyvoraSymmetric45import Testing4647// Negative controls of the differential harness itself: a comparison that can never fail proves48// nothing, so these tests feed deliberately wrong values through the same path (Swift → JSON →49// oracle → JSON → comparison → tally) and require the mismatch to be detected. They write no record.5051@Suite struct DifferentialHarnessSelfTests {52    @Test func tallyCountsMismatches() {53        var tally = DiffTally("self-test", algorithm: "none", oracle: "none", seed: 1)54        tally.check(true, "unused")55        tally.check(false, "wrong on purpose")56        tally.check(false, "wrong again")57        #expect(tally.cases == 3 && tally.failed == 2 && tally.failures == ["wrong on purpose", "wrong again"])58    }5960    /// One flipped bit in the message must make the oracle's digest differ from ours.61    @Test func oracleDetectsAWrongDigest() throws {62        guard Oracle.pythonUnavailable(["pycryptodome"]) == nil else { return }63        var rng = AuditRNG(seed: 42)64        var mismatches = 0, matches = 065        let messages = (0..<50).map { _ in rng.bytes(rng.int(1...100)) }66        let answers = try Py.batch("hash", messages.map { ["alg": "keccak256", "data": hex($0)] } + messages.map { ["alg": "ripemd160", "data": hex($0)] })67        for (i, m) in messages.enumerated() {68            var wrong = m69            wrong[0] ^= 170            if RIPEMD160.hash(m) == answers[50 + i].bytes("out") { matches += 1 }71            if RIPEMD160.hash(wrong) != answers[50 + i].bytes("out") { mismatches += 1 }72            if answers[i].bytes("out") != answers[50 + i].bytes("out") { mismatches += 1 }  // two different functions73        }74        #expect(matches == 50 && mismatches == 100)75    }7677    /// An error raised inside the oracle for one case is reported for that case only and never read as a match.78    @Test func oracleErrorsAreNotMatches() throws {79        guard Oracle.pythonUnavailable([]) == nil else { return }80        let answers = try Py.batch("hash", [["alg": "sha256", "data": "00"], ["alg": "no-such-hash", "data": "00"], ["alg": "sha256", "data": "zz"]])81        #expect(answers[0].bytes("out") == SHA256.hash([0]))82        #expect(answers[1].oracleError != nil && answers[1].bytes("out") == nil)83        #expect(answers[2].oracleError != nil && answers[2].bytes("out") == nil)84    }8586    /// Same seed, same cases: a recorded seed reproduces a campaign.87    @Test func seededGeneratorIsReproducible() {88        var a = AuditRNG(seed: 0x5eed), b = AuditRNG(seed: 0x5eed), c = AuditRNG(seed: 0x5eee)89        let x = a.bytes(64), y = b.bytes(64), z = c.bytes(64)90        #expect(x == y && x != z)91    }92}93
Files main
Menu

Theme