Skip to main content

Module timing

Module timing 

Source
Expand description

Constant-time operation wrapper.

Enforces a fixed wall-clock duration for wrapped operations to prevent timing side-channels from leaking information about internal control flow. The configured target_duration_ns must exceed the worst-case execution time of the protected function; if the operation overruns the target the call returns after its natural duration (time cannot be compressed).

The wrapper uses compiler_fence(SeqCst) and core::hint::black_box to prevent the compiler from eliding the busy-wait or reordering the result past the timing barrier.

§Platform semantics

  • Native + std: std::time::Instant provides monotonic nanosecond resolution for TimingProtection::target_duration_ns.
  • wasm32 + wasm feature (browser or worker with Web APIs): time is read from Performance::now on globalThis.performance (sub-millisecond resolution; values are converted to nanoseconds for the same target_duration_ns field).
  • Other no_std / bare-metal, or wasm32 without wasm: there is no portable monotonic wall clock. The implementation falls back to an atomic call counter, so target_duration_ns is not wall nanoseconds and sub-microsecond padding is not meaningful. Prefer disabling the wrapper (TimingProtection::permissive) on those targets unless you accept tick-based (non wall-clock) pacing only.

This layer does not make non-constant-time algorithms constant-time; it only pads elapsed time when a real clock (or explicit tick fallback) is used.

Structs§

TimingProtection
Constant-time wrapper configuration.

Functions§

get_timing_protection
Get the global timing protection configuration.
protect_timing
Apply global constant-time protection to func.
protect_timing_async
Async variant of protect_timing.
protect_timing_with_timing
Apply global constant-time protection and return (result, elapsed).
protect_timing_with_timing_async
Async variant of protect_timing_with_timing.
set_timing_protection
Set the global timing protection configuration.