SPB Git

Repository Public

MLKEMTimingTests.swift 4.0 KB 93 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : QYVORA7// File             : Tests/QyvoraMLKEMTests/MLKEMTimingTests.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/// Prints indicative timings (meaningful with `-c release` only). Not a pass/fail benchmark:48/// the only assertion is functional.49@Suite(.serialized) struct MLKEMTimingTests {50    static func microseconds(_ iterations: Int, _ body: () throws -> Void) rethrows -> Double {51        let clock = ContinuousClock()52        var samples = [Double]()53        for _ in 0..<5 {54            let start = clock.now55            for _ in 0..<iterations { try body() }56            let d = clock.now - start57            samples.append((Double(d.components.seconds) * 1e6 + Double(d.components.attoseconds) / 1e12) / Double(iterations))58        }59        return samples.sorted()[2]60    }6162    @Test func timings() throws {63        #if DEBUG64            let n = 2065        #else66            let n = 200067        #endif68        for ps in MLKEM.ParameterSet.allCases {69            let d = SystemRandom.bytes(32), z = SystemRandom.bytes(32), m = SystemRandom.bytes(32)70            let pair = try MLKEM.keyGenInternal(ps, d: d, z: z)71            let enc = try MLKEM.encapsInternal(ps, encapsulationKey: pair.encapsulationKey, m: m)72            let keygen = try Self.microseconds(n) { _ = try MLKEM.keyGenInternal(ps, d: d, z: z) }73            let encaps = try Self.microseconds(n) { _ = try MLKEM.encapsulate(ps, encapsulationKey: pair.encapsulationKey) }74            let decaps = try Self.microseconds(n) {75                _ = try MLKEM.decapsulate(ps, decapsulationKey: pair.decapsulationKey, ciphertext: enc.ciphertext)76            }77            #expect(try MLKEM.decapsulate(ps, decapsulationKey: pair.decapsulationKey, ciphertext: enc.ciphertext) == enc.sharedSecret)78            print(String(format: "TIMING %@: keygen %.1f us, encaps %.1f us, decaps %.1f us", ps.name, keygen, encaps, decaps))79        }80        var poly = (0..<256).map { Int16($0 * 13 % 3329) }81        let ntt = poly.withUnsafeMutableBufferPointer { p in82            Self.microseconds(n * 50) {83                MLKEM.NTT.forward(p.baseAddress!)84                for i in 0..<256 { p[i] = MLKEM.Field.barrettReduce(p[i]) }85            }86        }87        let intt = poly.withUnsafeMutableBufferPointer { p in88            Self.microseconds(n * 50) { MLKEM.NTT.inverseToMontgomery(p.baseAddress!) }89        }90        print(String(format: "TIMING ML-KEM NTT+reduce %.3f us, inverse NTT %.3f us", ntt, intt))91    }92}93
Files main
Menu

Theme