Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : QYVORA7// File : Sources/QyvoraMath/DataIndependentTiming.swift8// Module : Qyvora Math9// 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 QyvoraCSupport4243/// The processor's data-independent-timing flag (PSTATE.DIT on ARM64), set around the operations of the engine that44/// process secrets (QYV-CRY-004, QYV-AUD-003).45///46/// With the flag set, the processor guarantees that the duration of the instructions listed by ARM (and by Apple for47/// its cores) does not depend on the values they process. It does not make a program constant-time: a branch or a48/// memory index that depends on a secret still leaks. The engine avoids those at the source level and the audit49/// counts the branches of the compiled code; the flag covers what neither can see.50///51/// The flag belongs to the thread. Every use here is a synchronous scope that restores the previous state on exit, so52/// nested scopes are harmless and nothing leaks into unrelated code. Do not hold a scope across an `await`.53///54/// Where the processor has no such flag (Intel Macs, a simulator running on one), the calls do nothing.55public enum DataIndependentTiming {56 /// True when the processor implements the flag.57 public static var isSupported: Bool { qy_dit_supported() != 0 }5859 /// True when the flag is set on the calling thread.60 public static var isEnabled: Bool { qy_dit_enabled() != 0 }6162 /// Sets the flag; pass the returned value to `restore`. For a function body:63 /// `let dit = DataIndependentTiming.enable(); defer { DataIndependentTiming.restore(dit) }`.64 public static func enable() -> Int32 { qy_dit_enable() }6566 /// Puts the flag back to what it was before the matching `enable`.67 public static func restore(_ previous: Int32) { qy_dit_restore(previous) }6869 /// Runs `body` with the flag set.70 public static func withEnabled<T, E: Error>(_ body: () throws(E) -> T) throws(E) -> T {71 let previous = qy_dit_enable()72 defer { qy_dit_restore(previous) }73 return try body()74 }75}76