Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : QYVORA7// File : Tests/QyvoraMLKEMTests/MLKEMACVPTests.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 QyvoraMath43import Testing4445@testable import QyvoraMLKEM4647/// Replays every group and case of the NIST ACVP ML-KEM files (see Vectors/README.txt).48@Suite struct MLKEMACVPTests {49 static func parameterSet(_ name: String) -> MLKEM.ParameterSet? {50 MLKEM.ParameterSet.allCases.first { $0.name == name }51 }5253 @Test func keyGen() throws {54 var passed = 0, total = 055 for group in try Vectors.groups("acvp-ML-KEM-keyGen-FIPS203.json.gz") {56 let ps = try #require(Self.parameterSet(group.string("parameterSet")))57 for t in group.tests {58 total += 159 let pair = try MLKEM.keyGenInternal(ps, d: t.bytes("d"), z: t.bytes("z"))60 let ok = try pair.encapsulationKey == t.bytes("ek") && pair.decapsulationKey == t.bytes("dk")61 #expect(ok, "keyGen tcId \(t.int("tcId"))")62 if ok { passed += 1 }63 }64 }65 print("ACVP ML-KEM keyGen: \(passed)/\(total) cases passed")66 #expect(total == 75)67 #expect(passed == total)68 }6970 @Test func encapDecap() throws {71 var passed = 0, total = 072 var perFunction: [String: (Int, Int)] = [:]73 for group in try Vectors.groups("acvp-ML-KEM-encapDecap-FIPS203.json.gz") {74 let ps = try #require(Self.parameterSet(group.string("parameterSet")))75 let function = group.string("function")76 for t in group.tests {77 total += 178 var ok = false79 switch function {80 case "encapsulation":81 let ek = try t.bytes("ek")82 #expect(MLKEM.isValidEncapsulationKey(ps, ek))83 let r = try MLKEM.encapsInternal(ps, encapsulationKey: ek, m: t.bytes("m"))84 ok = try r.ciphertext == t.bytes("c") && r.sharedSecret == t.bytes("k")85 // and the matching decapsulation key recovers the same secret86 ok = try ok && MLKEM.decapsulate(ps, decapsulationKey: t.bytes("dk"), ciphertext: r.ciphertext) == r.sharedSecret87 case "decapsulation":88 // "modified ciphertext" cases carry the implicit-rejection secret as expected k89 let k = try MLKEM.decapsulate(ps, decapsulationKey: t.bytes("dk"), ciphertext: t.bytes("c"))90 ok = try k == t.bytes("k")91 case "decapsulationKeyCheck":92 ok = try MLKEM.isValidDecapsulationKey(ps, t.bytes("dk")) == t.bool("testPassed")93 case "encapsulationKeyCheck":94 ok = try MLKEM.isValidEncapsulationKey(ps, t.bytes("ek")) == t.bool("testPassed")95 default:96 Issue.record("unknown ACVP function \(function)")97 }98 #expect(ok, "\(function) tcId \(t.int("tcId")) (\(t.string("reason")))")99 var c = perFunction[function] ?? (0, 0)100 c.1 += 1101 if ok {102 passed += 1103 c.0 += 1104 }105 perFunction[function] = c106 }107 }108 for (f, c) in perFunction.sorted(by: { $0.key < $1.key }) {109 print("ACVP ML-KEM encapDecap \(f): \(c.0)/\(c.1) cases passed")110 }111 #expect(total == 165)112 #expect(passed == total)113 }114}115