Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : QYVORA7// File : Tests/QyvoraMLDSATests/TestHashOracle.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 CommonCrypto42import CryptoKit43import Foundation44import QyvoraKeccak45import QyvoraMath46import Testing4748@testable import QyvoraMLDSA4950/// Test-only hash oracle for the HashML-DSA vectors. QyvoraMLDSA does not depend on the SHA-251/// module, so the SHA-2 digests come from the system (CryptoKit, CommonCrypto) and, for the two52/// truncated SHA-512/t variants that no system API offers, from the small FIPS 180-453/// implementation below (checked against CryptoKit's SHA-512 and the NIST "abc" examples).54enum TestHashOracle {55 static func preHash(named name: String) -> MLDSA.PreHash? {56 switch name {57 case "SHA2-224": return .sha2_22458 case "SHA2-256": return .sha2_25659 case "SHA2-384": return .sha2_38460 case "SHA2-512": return .sha2_51261 case "SHA2-512/224": return .sha2_512_22462 case "SHA2-512/256": return .sha2_512_25663 case "SHA3-224": return .sha3_22464 case "SHA3-256": return .sha3_25665 case "SHA3-384": return .sha3_38466 case "SHA3-512": return .sha3_51267 case "SHAKE-128": return .shake12868 case "SHAKE-256": return .shake25669 default: return nil70 }71 }7273 static func keccak(_ hash: MLDSA.PreHash) -> MLDSA.KeccakPreHash? {74 MLDSA.KeccakPreHash.allCases.first { $0.preHash == hash }75 }7677 static func digest(_ hash: MLDSA.PreHash, _ message: Bytes) -> Bytes {78 switch hash {79 case .sha2_224:80 var out = Bytes(repeating: 0, count: Int(CC_SHA224_DIGEST_LENGTH))81 _ = CC_SHA224(message, CC_LONG(message.count), &out)82 return out83 case .sha2_256: return Bytes(SHA256.hash(data: message))84 case .sha2_384: return Bytes(SHA384.hash(data: message))85 case .sha2_512: return Bytes(SHA512.hash(data: message))86 case .sha2_512_224: return SHA512t.hash(message, t: 224)87 case .sha2_512_256: return SHA512t.hash(message, t: 256)88 default: return keccak(hash)!.digest(message)89 }90 }9192 /// SHA-512 and SHA-512/t (FIPS 180-4, sections 5.3.5, 5.3.6 and 6.4).93 enum SHA512t {94 static let k: [UInt64] = [95 0x428a_2f98_d728_ae22, 0x7137_4491_23ef_65cd, 0xb5c0_fbcf_ec4d_3b2f, 0xe9b5_dba5_8189_dbbc,96 0x3956_c25b_f348_b538, 0x59f1_11f1_b605_d019, 0x923f_82a4_af19_4f9b, 0xab1c_5ed5_da6d_8118,97 0xd807_aa98_a303_0242, 0x1283_5b01_4570_6fbe, 0x2431_85be_4ee4_b28c, 0x550c_7dc3_d5ff_b4e2,98 0x72be_5d74_f27b_896f, 0x80de_b1fe_3b16_96b1, 0x9bdc_06a7_25c7_1235, 0xc19b_f174_cf69_2694,99 0xe49b_69c1_9ef1_4ad2, 0xefbe_4786_384f_25e3, 0x0fc1_9dc6_8b8c_d5b5, 0x240c_a1cc_77ac_9c65,100 0x2de9_2c6f_592b_0275, 0x4a74_84aa_6ea6_e483, 0x5cb0_a9dc_bd41_fbd4, 0x76f9_88da_8311_53b5,101 0x983e_5152_ee66_dfab, 0xa831_c66d_2db4_3210, 0xb003_27c8_98fb_213f, 0xbf59_7fc7_beef_0ee4,102 0xc6e0_0bf3_3da8_8fc2, 0xd5a7_9147_930a_a725, 0x06ca_6351_e003_826f, 0x1429_2967_0a0e_6e70,103 0x27b7_0a85_46d2_2ffc, 0x2e1b_2138_5c26_c926, 0x4d2c_6dfc_5ac4_2aed, 0x5338_0d13_9d95_b3df,104 0x650a_7354_8baf_63de, 0x766a_0abb_3c77_b2a8, 0x81c2_c92e_47ed_aee6, 0x9272_2c85_1482_353b,105 0xa2bf_e8a1_4cf1_0364, 0xa81a_664b_bc42_3001, 0xc24b_8b70_d0f8_9791, 0xc76c_51a3_0654_be30,106 0xd192_e819_d6ef_5218, 0xd699_0624_5565_a910, 0xf40e_3585_5771_202a, 0x106a_a070_32bb_d1b8,107 0x19a4_c116_b8d2_d0c8, 0x1e37_6c08_5141_ab53, 0x2748_774c_df8e_eb99, 0x34b0_bcb5_e19b_48a8,108 0x391c_0cb3_c5c9_5a63, 0x4ed8_aa4a_e341_8acb, 0x5b9c_ca4f_7763_e373, 0x682e_6ff3_d6b2_b8a3,109 0x748f_82ee_5def_b2fc, 0x78a5_636f_4317_2f60, 0x84c8_7814_a1f0_ab72, 0x8cc7_0208_1a64_39ec,110 0x90be_fffa_2363_1e28, 0xa450_6ceb_de82_bde9, 0xbef9_a3f7_b2c6_7915, 0xc671_78f2_e372_532b,111 0xca27_3ece_ea26_619c, 0xd186_b8c7_21c0_c207, 0xeada_7dd6_cde0_eb1e, 0xf57d_4f7f_ee6e_d178,112 0x06f0_67aa_7217_6fba, 0x0a63_7dc5_a2c8_98a6, 0x113f_9804_bef9_0dae, 0x1b71_0b35_131c_471b,113 0x28db_77f5_2304_7d84, 0x32ca_ab7b_40c7_2493, 0x3c9e_be0a_15c9_bebc, 0x431d_67c4_9c10_0d4c,114 0x4cc5_d4be_cb3e_42b6, 0x597f_299c_fc65_7e2a, 0x5fcb_6fab_3ad6_faec, 0x6c44_198c_4a47_5817,115 ]116117 static let iv512: [UInt64] = [118 0x6a09_e667_f3bc_c908, 0xbb67_ae85_84ca_a73b, 0x3c6e_f372_fe94_f82b, 0xa54f_f53a_5f1d_36f1,119 0x510e_527f_ade6_82d1, 0x9b05_688c_2b3e_6c1f, 0x1f83_d9ab_fb41_bd6b, 0x5be0_cd19_137e_2179,120 ]121122 static func rotr(_ x: UInt64, _ n: UInt64) -> UInt64 { (x >> n) | (x << (64 - n)) }123124 static func run(_ message: Bytes, iv: [UInt64]) -> Bytes {125 var h = iv126 var m = message127 m.append(0x80)128 while m.count % 128 != 112 { m.append(0) }129 m += Bytes(repeating: 0, count: 8) + Endian.be64(UInt64(message.count) * 8)130 var w = [UInt64](repeating: 0, count: 80)131 for block in stride(from: 0, to: m.count, by: 128) {132 for t in 0..<16 {133 var v: UInt64 = 0134 for b in 0..<8 { v = v << 8 | UInt64(m[block + 8 * t + b]) }135 w[t] = v136 }137 for t in 16..<80 {138 let s0 = rotr(w[t - 15], 1) ^ rotr(w[t - 15], 8) ^ (w[t - 15] >> 7)139 let s1 = rotr(w[t - 2], 19) ^ rotr(w[t - 2], 61) ^ (w[t - 2] >> 6)140 w[t] = w[t - 16] &+ s0 &+ w[t - 7] &+ s1141 }142 var a = h[0], b = h[1], c = h[2], d = h[3], e = h[4], f = h[5], g = h[6], hh = h[7]143 for t in 0..<80 {144 let S1 = rotr(e, 14) ^ rotr(e, 18) ^ rotr(e, 41)145 let ch = (e & f) ^ (~e & g)146 let t1 = hh &+ S1 &+ ch &+ k[t] &+ w[t]147 let S0 = rotr(a, 28) ^ rotr(a, 34) ^ rotr(a, 39)148 let maj = (a & b) ^ (a & c) ^ (b & c)149 let t2 = S0 &+ maj150 hh = g; g = f; f = e; e = d &+ t1; d = c; c = b; b = a; a = t1 &+ t2151 }152 h[0] &+= a; h[1] &+= b; h[2] &+= c; h[3] &+= d; h[4] &+= e; h[5] &+= f; h[6] &+= g; h[7] &+= hh153 }154 return h.flatMap { Endian.be64($0) }155 }156157 /// SHA-512/t: the initial value is SHA-512, started from H(0) xor a5a5..., of "SHA-512/t".158 static func hash(_ message: Bytes, t: Int) -> Bytes {159 let seed = run(Array("SHA-512/\(t)".utf8), iv: iv512.map { $0 ^ 0xa5a5_a5a5_a5a5_a5a5 })160 var iv = [UInt64]()161 for i in 0..<8 {162 var v: UInt64 = 0163 for b in 0..<8 { v = v << 8 | UInt64(seed[8 * i + b]) }164 iv.append(v)165 }166 return Array(run(message, iv: iv)[0..<(t / 8)])167 }168 }169}170171@Suite struct TestHashOracleTests {172 @Test func sha512FamilyMatchesReferences() {173 for length in [0, 1, 55, 111, 112, 127, 128, 129, 1000] {174 let m = SystemRandom.bytes(length)175 #expect(TestHashOracle.SHA512t.run(m, iv: TestHashOracle.SHA512t.iv512) == Bytes(SHA512.hash(data: m)))176 }177 let abc = Array("abc".utf8)178 #expect(TestHashOracle.digest(.sha2_512_224, abc).hex == "4634270f707b6a54daae7530460842e20e37ed265ceee9a43e8924aa")179 #expect(TestHashOracle.digest(.sha2_512_256, abc).hex == "53048e2681941ef99b2e29b76b4c7dabe4c2d0c634fc6d46e0e2f13107e7af23")180 #expect(TestHashOracle.digest(.sha2_224, abc).hex == "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7")181 for h in MLDSA.PreHash.allCases {182 #expect(TestHashOracle.digest(h, abc).count == h.digestSize)183 }184 }185}186