SPB Git

Repository Public

MLDSATimingTests.swift 4.0 KB 93 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : QYVORA7// File             : Tests/QyvoraMLDSATests/MLDSATimingTests.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 QyvoraMLDSA4647/// Prints indicative timings (meaningful with `-c release` only). Not a pass/fail benchmark:48/// the only assertion is functional.49@Suite(.serialized) struct MLDSATimingTests {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 = 365        #else66            let n = 30067        #endif68        let message = SystemRandom.bytes(64)69        for ps in MLDSA.ParameterSet.allCases {70            let seed = SystemRandom.bytes(32)71            let pair = try MLDSA.keyGenInternal(ps, seed: seed)72            let sig = try MLDSA.sign(ps, privateKey: pair.privateKey, message: message)73            let keygen = try Self.microseconds(n) { _ = try MLDSA.keyGenInternal(ps, seed: seed) }74            // hedged signing: the number of rejection rounds varies, the figure is an average75            let sign = try Self.microseconds(n) { _ = try MLDSA.sign(ps, privateKey: pair.privateKey, message: message) }76            let verify = Self.microseconds(n) { _ = MLDSA.verify(ps, publicKey: pair.publicKey, message: message, signature: sig) }77            #expect(MLDSA.verify(ps, publicKey: pair.publicKey, message: message, signature: sig))78            print(String(format: "TIMING %@: keygen %.1f us, sign (hedged, mean) %.1f us, verify %.1f us", ps.name, keygen, sign, verify))79        }80        var poly = (0..<256).map { Int32($0 * 32749 % 8_380_417) }81        let ntt = poly.withUnsafeMutableBufferPointer { p in82            Self.microseconds(n * 100) {83                MLDSA.NTT.forward(p.baseAddress!)84                for i in 0..<256 { p[i] = MLDSA.Field.reduce32(p[i]) }85            }86        }87        let intt = poly.withUnsafeMutableBufferPointer { p in88            Self.microseconds(n * 100) { MLDSA.NTT.inverseToMontgomery(p.baseAddress!) }89        }90        print(String(format: "TIMING ML-DSA NTT+reduce %.3f us, inverse NTT %.3f us", ntt, intt))91    }92}93
Files main
Menu

Theme