Continuous health tests for entropy noise sources, per NIST SP 800-90B:
- Repetition Count Test (RCT), section 4.4.1.
Cutoff C = 1 + Ceil(-log2(alpha) / H); the test fails when the same
sample value is observed C or more times in a row.
- Adaptive Proportion Test (APT), section 4.4.2.
Non-overlapping windows of W samples (512 for non-binary sources); the
first sample of each window is the comparator, and the test fails when
the comparator value occurs C or more times within the window, with
C = 1 + CRITBINOM(W, 2^-H, 1 - alpha) (footnote 10 of 4.4.2).
H is the assessed (or claimed) min-entropy per sample in bits and alpha the
acceptable false-positive probability, expressed here as an exponent E with
alpha = 2^-E (SP 800-90B recommends E = 20).
tEntropyHealthMonitor packages both tests plus the startup-test requirement
of section 4.3: each channel (independent copy of the noise source, e.g.
one collector thread) must deliver at least StartupSamples (default 1024)
consecutive samples with no test failure before the monitor reports
Healthy. The caller MUST inhibit entropy output while Healthy is False.
The monitor is a passive observer: feed it copies of the raw noise samples
AFTER they have been collected (e.g. from the controller thread while the
collectors are suspended). It never touches the collection path, holds no
locks, and does not allocate after construction, so it cannot disturb the
timing behavior of the noise source being measured.
Samples are bytes. For noise sources whose raw samples are wider (such as
TSC deltas), use ProcessDelta, which reduces a sample to its 8 least
significant bits. The reduction applies to health testing only - the full
raw sample should still be fed to the conditioning function - but the same
reduction must be used when preparing datasets for the SP 800-90B entropy
assessment tools, so that the assessed H and the tested samples refer to
the same alphabet.
All tests run on a single thread (the thread calling ProcessSample); the
Healthy/State properties may be polled from the same thread. If another
thread must observe the state, the caller provides its own synchronization.
SEPARATION FROM stEntropyEstimators (why this unit does not use it): the
SP 800-90B section 6.3 non-IID min-entropy estimators live in a separate,
offline unit (stEntropyEstimators) and this always-on health unit deliberately
does NOT depend on them. The two mechanisms run at different times and are
coupled only by a number, never a call. The estimators run ONCE, offline, over
a large captured dataset (>= 1e6 samples; suffix arrays, Markov dictionaries,
Generics.Collections, tens of seconds) to produce an assessed min-entropy H.
That H - or a more conservative claimed value - is then handed to
tEntropyHealthMonitor.Create as aMinEntropyPerSample; the continuous RCT and
APT derive their cutoffs from it (see CalcRctCutoff / CalcAptCutoff) and verify
at runtime that the source has not degraded below it. Calling the heavyweight
estimators from this hot, per-sample health path would be nonsensical and would
drag the estimator machinery into the runtime entropy path, so the split is
intentional: keep the runtime health monitor lean (uses only SysUtils) and the
offline assessment suite separate. If anything the dependency would point the
other way, and even that only in tooling: the unit that legitimately uses both
is an assessment program, which computes H with the estimators (as the
EntropyEstimation demo does) AND replays this monitor at candidate H values.Feature notes: DRBG and entropy source (SP 800-90) notes
inherits from Exception
A caller-supplied parameter to the SP 800-90B health tests is outside the range the tests are defined for, or a channel index is out of range. It is always a programming error, never a health-test verdict: a FAILED health test is reported through tHealthMonitorState (hmsFailed), not raised.
(no public members)
fRctA : Byte;
fRctB : Cardinal;
fRctInit : Boolean;
fAptA : Byte;
fAptB : Cardinal;
fAptI : Cardinal;
fSampleCount: UInt64;
constructor Create(aChannelCount : Cardinal; aMinEntropyPerSample: Double; aAptWindow : Cardinal = 512; aAlphaExponent : Cardinal = 20; aStartupSamples : UInt64 = 1024);
destructor Destroy; override;
procedure ProcessSample(aChannel: Cardinal; aSample: Byte);
procedure ProcessDelta(aChannel: Cardinal; const aDelta: Int64);
procedure Restart;
function Healthy: Boolean;
property State: tHealthMonitorState read fState;
property ChannelCount: Cardinal read GetChannelCount;
property MinEntropyPerSample: Double read fMinEntropy;
property RctCutoff: Cardinal read fRctCutoff;
property AptCutoff: Cardinal read fAptCutoff;
property AptWindow: Cardinal read fAptWindow;
property StartupSamples: UInt64 read fStartupSamples;
property FailureCount: Cardinal read fFailureCount;
property LastFailure: tHealthTestKind read fLastFailure;
property LastFailureChannel: Cardinal read fLastFailureChannel;
property SampleCount[aChannel: Cardinal]: UInt64 read GetSampleCount;
class function SelfTest: Boolean;
Records the first TargetPerChannel raw deltas of each channel in memory (fed from the controller thread, like the health tests, so collection is not disturbed) and writes them to files on demand:
<prefix>_ch<N>.bin - 8-LSB reduced samples, one byte each: the dataset format for `ea_non_iid <file> 8` (must match the reduction the health tests and the claimed H refer to) <prefix>_ch<N>_raw.bin - full Int64 little-endian deltas, for offline re-reduction experiments Normally call SaveToFiles from the application thread once Complete is True, at which point the recorder is no longer written to. Saving earlier is supported (it snapshots each channel's count once and writes only that many samples) and is what a stalled capture should do, but the tail of a partial file is then whatever the noise source had produced so far.
constructor Create(aChannelCount : Cardinal; aTargetPerChannel : Cardinal; const aFilePrefix : string);
destructor Destroy; override;
procedure CaptureDelta(aChannel: Cardinal; const aDelta: Int64);
function Complete: Boolean;
function MinCaptured: Cardinal;
function CopyReduced(aChannel : Cardinal; var aDest : TBytes; aDestOfs : Cardinal; aCount : Cardinal): Cardinal;
procedure SaveToFiles;
property TargetPerChannel: Cardinal read fTarget;
property FilePrefix: string read fPrefix;