Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : QYVORA7// File : Tests/QyvoraSymmetricTests/VectorSupport.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 Foundation42import QyvoraMath4344/// Test-only helpers: gzip-compressed JSON vector files of the `Vectors` resource directory.45enum VectorFile {46 enum Failure: Error { case missing(String), notGzip(String), inflate(String), json(String) }4748 /// Removes the gzip framing (RFC 1952) and inflates the raw DEFLATE stream with the system zlib codec.49 static func gunzip(_ data: Data, name: String) throws -> Data {50 let b = [UInt8](data)51 guard b.count >= 18, b[0] == 0x1f, b[1] == 0x8b, b[2] == 8 else { throw Failure.notGzip(name) }52 let flags = b[3]53 var i = 1054 if flags & 0x04 != 0 { // FEXTRA55 guard i + 2 <= b.count else { throw Failure.notGzip(name) }56 i += 2 + Int(b[i]) + Int(b[i + 1]) << 857 }58 if flags & 0x08 != 0 { // FNAME59 while i < b.count, b[i] != 0 { i += 1 }60 i += 161 }62 if flags & 0x10 != 0 { // FCOMMENT63 while i < b.count, b[i] != 0 { i += 1 }64 i += 165 }66 if flags & 0x02 != 0 { i += 2 } // FHCRC67 guard i < b.count - 8 else { throw Failure.notGzip(name) }68 let raw = Data(b[i..<(b.count - 8)])69 guard let out = try? (raw as NSData).decompressed(using: .zlib) as Data else { throw Failure.inflate(name) }70 let t = b.count - 471 let isize = UInt32(b[t]) | UInt32(b[t + 1]) << 8 | UInt32(b[t + 2]) << 16 | UInt32(b[t + 3]) << 2472 guard UInt32(truncatingIfNeeded: out.count) == isize else { throw Failure.inflate(name) }73 return out74 }7576 /// Loads `Vectors/<name>` (a `.json.gz` file) and returns the decoded JSON object.77 static func load(_ name: String) throws -> [String: Any] {78 guard let dir = Bundle.module.url(forResource: "Vectors", withExtension: nil) else { throw Failure.missing("Vectors") }79 let url = dir.appendingPathComponent(name)80 guard let gz = try? Data(contentsOf: url) else { throw Failure.missing(name) }81 let json = try gunzip(gz, name: name)82 guard let obj = try JSONSerialization.jsonObject(with: json) as? [String: Any] else { throw Failure.json(name) }83 return obj84 }85}8687/// Loose accessors for JSON test fields (ACVP encodes some integers as strings).88extension Dictionary where Key == String, Value == Any {89 func int(_ key: String) -> Int {90 if let v = self[key] as? Int { return v }91 if let s = self[key] as? String, let v = Int(s) { return v }92 if let n = self[key] as? NSNumber { return n.intValue }93 fatalError("missing integer field \(key)")94 }9596 func str(_ key: String) -> String {97 guard let s = self[key] as? String else { fatalError("missing string field \(key)") }98 return s99 }100101 func bytes(_ key: String) -> Bytes {102 guard let b = Bytes(hex: str(key)) else { fatalError("field \(key) is not hexadecimal") }103 return b104 }105106 func list(_ key: String) -> [[String: Any]] {107 guard let l = self[key] as? [[String: Any]] else { fatalError("missing array field \(key)") }108 return l109 }110}111