SPB Git

Repository Public

RustCLIHashDiffTests.swift 4.4 KB 91 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : QYVORA7// File             : security/tests/Tests/DifferentialAuditTests/RustCLIHashDiffTests.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 QyvoraKeccak44import QyvoraMath45import QyvoraSLHDSA46import QyvoraSymmetric47import Testing4849// The hash functions of the engine against the command-line tool `spbqc` of the Rust library50// spb-quantum-crypto (one process per case, message on stdin). This is the only differential51// oracle here for SHA-512/224 and SHA-512/256 (the HashSLH-DSA pre-hash functions of the engine).5253@Suite struct RustCLIHashDifferential {54    @Test func hashes() throws {55        let name = "rust-cli-hash"56        guard let cli = Oracle.spbqc else {57            AuditRecorder.notRun(area: "differential", name: name, reason: "spbqc binary not found (security/.tools/bin/spbqc or QY_SPBQC)")58            return59        }60        let version = (String(data: (try? Oracle.run(cli, ["version"])) ?? Data(), encoding: .utf8) ?? "").trimmingCharacters(in: .whitespacesAndNewlines)61        let functions: [(String, Int, (Bytes, Int) -> Bytes)] = [62            ("SHA2-224", 64, { m, _ in SHA224.hash(m) }),63            ("SHA2-256", 64, { m, _ in SHA256.hash(m) }),64            ("SHA2-384", 128, { m, _ in SHA384.hash(m) }),65            ("SHA2-512", 128, { m, _ in SHA512.hash(m) }),66            ("SHA2-512/224", 128, { m, _ in SLHDSA.PreHash.sha512_224.digest(m) }),67            ("SHA2-512/256", 128, { m, _ in SLHDSA.PreHash.sha512_256.digest(m) }),68            ("SHA3-224", 144, { m, _ in SHA3.sha3_224(m) }),69            ("SHA3-256", 136, { m, _ in SHA3.sha3_256(m) }),70            ("SHA3-384", 104, { m, _ in SHA3.sha3_384(m) }),71            ("SHA3-512", 72, { m, _ in SHA3.sha3_512(m) }),72            ("SHAKE-128", 168, { m, n in SHAKE.shake128(m, outputLength: n) }),73            ("SHAKE-256", 136, { m, n in SHAKE.shake256(m, outputLength: n) }),74        ]75        for (alg, block, ours) in functions {76            let seed = DiffSeed.make(name + alg)77            var rng = AuditRNG(seed: seed)78            var tally = DiffTally(name + "-" + alg.lowercased().replacingOccurrences(of: "/", with: "-"), algorithm: alg, oracle: version + " (Rust, command-line tool)", seed: seed)79            for _ in 0..<AuditMode.cases(quick: 25, full: 250) {80                let m = rng.bytes(rng.edgeLength(block: block, max: 400))81                let length = rng.pick([1, 32, 64, 200, 337])82                var args = ["hash", "--alg", alg]83                if alg.hasPrefix("SHAKE") { args += ["--len", String(length)] }84                let out = (try? Oracle.run(cli, args, input: Data(m))).flatMap { String(data: $0, encoding: .utf8) } ?? ""85                tally.check(Hex.decode(out.trimmingCharacters(in: .whitespacesAndNewlines)) == ours(m, length), "alg=\(alg) message=\(hex(m)) output_length=\(length) spbqc=\(out.prefix(140))")86            }87            #expect(tally.finish() == 0)88        }89    }90}91
Files main
Menu

Theme