Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : QYVORA7// File : Tests/QyvoraMLKEMTests/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/// Loads the gzip-compressed NIST ACVP JSON files of `Vectors/` without any dependency:45/// the gzip container (RFC 1952) is parsed here and the DEFLATE stream is handed to Foundation.46enum Vectors {47 struct LoadError: Error, CustomStringConvertible {48 let description: String49 }5051 static func gunzip(_ data: Data) throws -> Data {52 let b = [UInt8](data)53 guard b.count > 18, b[0] == 0x1f, b[1] == 0x8b, b[2] == 8 else { throw LoadError(description: "not a gzip file") }54 let flags = b[3]55 var i = 1056 if flags & 0x04 != 0 {57 i += 2 + Int(b[i]) + Int(b[i + 1]) << 858 }59 if flags & 0x08 != 0 {60 while b[i] != 0 { i += 1 }61 i += 162 }63 if flags & 0x10 != 0 {64 while b[i] != 0 { i += 1 }65 i += 166 }67 if flags & 0x02 != 0 { i += 2 }68 let deflate = Data(b[i..<(b.count - 8)])69 let out = try (deflate as NSData).decompressed(using: .zlib) as Data70 let expected = Int(b[b.count - 4]) | Int(b[b.count - 3]) << 8 | Int(b[b.count - 2]) << 16 | Int(b[b.count - 1]) << 2471 guard out.count & 0xffff_ffff == expected else { throw LoadError(description: "gzip length mismatch") }72 return out73 }7475 /// The `testGroups` array of an ACVP `internalProjection.json.gz` file.76 static func groups(_ name: String) throws -> [[String: Any]] {77 guard let dir = Bundle.module.url(forResource: "Vectors", withExtension: nil) else {78 throw LoadError(description: "Vectors resource directory missing")79 }80 let json = try gunzip(Data(contentsOf: dir.appendingPathComponent(name)))81 guard let root = try JSONSerialization.jsonObject(with: json) as? [String: Any],82 let groups = root["testGroups"] as? [[String: Any]]83 else { throw LoadError(description: "unexpected JSON layout in \(name)") }84 return groups85 }86}8788extension Dictionary where Key == String, Value == Any {89 func bytes(_ key: String) throws -> Bytes {90 guard let s = self[key] as? String, let b = Hex.decode(s) else {91 throw Vectors.LoadError(description: "missing or malformed hex field \(key)")92 }93 return b94 }9596 func string(_ key: String) -> String { self[key] as? String ?? "" }97 func bool(_ key: String) -> Bool { self[key] as? Bool ?? false }98 func int(_ key: String) -> Int { self[key] as? Int ?? -1 }99 var tests: [[String: Any]] { self["tests"] as? [[String: Any]] ?? [] }100}101