Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : QYVORA7// File : Tests/QyvoraMathTests/DataIndependentTimingTests.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 Testing42@testable import QyvoraMath4344/// Regression tests of QYV-CRY-004 and QYV-AUD-003: the processor's data-independent-timing flag is set around the45/// operations on secrets and restored afterwards. (The other modules check their own entry points; the statistical46/// effect is measured by `qytiming`, target `ct-equal-hamming`.)47@Suite struct DataIndependentTimingTests {48 @Test func theFlagIsSetInsideAScopeAndRestoredAfterIt() throws {49 #if arch(arm64)50 // every Apple silicon Mac and every iPhone since the A12 implements the flag51 try #require(DataIndependentTiming.isSupported, "this ARM64 processor does not report FEAT_DIT")52 #expect(!DataIndependentTiming.isEnabled)53 let inside = DataIndependentTiming.withEnabled { DataIndependentTiming.isEnabled }54 #expect(inside)55 #expect(!DataIndependentTiming.isEnabled)5657 // the two-line form used by the function bodies58 let token = DataIndependentTiming.enable()59 #expect(token == 0 && DataIndependentTiming.isEnabled)60 DataIndependentTiming.restore(token)61 #expect(!DataIndependentTiming.isEnabled)62 #else63 #expect(!DataIndependentTiming.isSupported && !DataIndependentTiming.isEnabled)64 #expect(DataIndependentTiming.enable() == 1)65 #endif66 }6768 @Test func nestedScopesLeaveTheFlagToTheOutermostOne() throws {69 #if arch(arm64)70 try #require(DataIndependentTiming.isSupported)71 let outer = DataIndependentTiming.enable()72 #expect(outer == 0)73 let inner = DataIndependentTiming.enable()74 #expect(inner == 1, "a nested scope has nothing to restore")75 DataIndependentTiming.restore(inner)76 #expect(DataIndependentTiming.isEnabled, "leaving the inner scope must not clear the flag of the outer one")77 // an operation of the engine inside a scope behaves the same way78 #expect(ConstantTime.equal([1, 2, 3], [1, 2, 3]))79 #expect(DataIndependentTiming.isEnabled)80 DataIndependentTiming.restore(outer)81 #expect(!DataIndependentTiming.isEnabled)82 #endif83 }8485 @Test func aThrowingScopeRestoresTheFlag() throws {86 struct Failure: Error {}87 #expect(throws: Failure.self) {88 try DataIndependentTiming.withEnabled { () throws(Failure) -> Void in throw Failure() }89 }90 #expect(!DataIndependentTiming.isEnabled)91 }9293 @Test func constantTimeComparisonStillAnswersAndRestoresTheFlag() {94 #expect(ConstantTime.equal([], []))95 #expect(ConstantTime.equal(Bytes(repeating: 7, count: 64), Bytes(repeating: 7, count: 64)))96 #expect(!ConstantTime.equal(Bytes(repeating: 7, count: 64), Bytes(repeating: 7, count: 63) + [6]))97 #expect(!ConstantTime.equal([1], [1, 2]))98 #expect(!DataIndependentTiming.isEnabled)99 }100}101102/// The one generator of the wallet also serves the choices that are not secret (the positions of the backup quiz):103/// nothing in the sources draws from another one (audit check `system-rng-only`).104@Suite struct SystemRandomSelectionTests {105 @Test func uniformStaysInRangeAndReachesEveryValue() {106 #expect(SystemRandom.uniform(below: 1) == 0 && SystemRandom.uniform(below: 0) == 0 && SystemRandom.uniform(below: -3) == 0)107 for bound in [2, 3, 10, 24, 2048] {108 var seen = Set<Int>()109 for _ in 0..<(bound * 40) {110 let v = SystemRandom.uniform(below: bound)111 #expect(v >= 0 && v < bound)112 seen.insert(v)113 }114 // 40 draws per value: missing one of them has a probability below 1e-13 for these bounds115 #expect(seen.count == bound, "\(bound - seen.count) value(s) of \(bound) never drawn")116 }117 }118119 @Test func aPermutationKeepsEveryElementAndIsNotTheIdentityEveryTime() {120 let items = Array(0..<24)121 var moved = 0122 for _ in 0..<50 {123 let s = SystemRandom.permutation(of: items)124 #expect(s.sorted() == items)125 if s != items { moved += 1 }126 }127 #expect(moved >= 49)128 #expect(SystemRandom.permutation(of: [Int]()).isEmpty && SystemRandom.permutation(of: [7]) == [7])129 }130}131