randomness

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 14, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package randomness implements a battery of statistical randomness tests drawn from NIST SP 800-22 (the "STS" suite), specialised for the question an RF analyst actually asks of a captured payload or a recovered keystream: is this stream indistinguishable from random (strong keyed encryption — no weak structure to exploit), or does it carry exploitable structure (a keyless scrambler, a short LFSR, a biased generator)?

Each test returns a p-value in [0,1]; a stream "passes" a test at significance level alpha when p >= alpha. A truly random stream passes all tests; a structured one fails one or more (most diagnostically the spectral and linear-complexity tests for RF scramblers). The toolkit's existing entropy / index-of-coincidence triage (engine/stats) answers the same question coarsely; this battery answers it rigorously.

Bit sequences are represented as a []byte holding one 0/1 value per element (the same convention as engine/lfsr), so callers convert packed capture bytes with lfsr.BitsFromBytes. The incomplete-gamma p-values use gonum's mathext.GammaIncRegComp and the spectral test uses gonum's real FFT, so the suite stays pure-Go with no new dependencies.

Index

Constants

View Source
const DefaultAlpha = 0.01

DefaultAlpha is the conventional NIST significance level.

Variables

This section is empty.

Functions

This section is empty.

Types

type Report

type Report struct {
	Bits    int          `json:"bits"`
	Alpha   float64      `json:"alpha"`
	Tests   []TestResult `json:"tests"`
	Passed  int          `json:"passed"`
	Failed  int          `json:"failed"`
	Skipped int          `json:"skipped"`
}

Report is the full battery outcome over one bit stream.

func Battery

func Battery(bits []byte, alpha float64) Report

Battery runs the full suite over the bit stream, choosing block sizes from the stream length. alpha <= 0 selects DefaultAlpha.

func Quick

func Quick(bits []byte, alpha float64) Report

Quick runs the fast, low-data subset suitable for short captures.

func (Report) LooksRandom

func (r Report) LooksRandom() bool

LooksRandom reports whether every applicable test passed — i.e. the stream is statistically indistinguishable from random at the report's alpha.

func (Report) WeakestTests

func (r Report) WeakestTests() []TestResult

WeakestTests returns the applicable tests sorted by ascending p-value (the strongest evidence of non-randomness first).

type TestResult

type TestResult struct {
	Name string `json:"name"`
	// PValue is the test statistic's p-value in [0,1]; only meaningful when
	// Applicable is true.
	PValue float64 `json:"p_value"`
	// Passed reports p >= alpha. Only meaningful when Applicable is true.
	Passed bool `json:"passed"`
	// Applicable is false when the stream is too short for the test's
	// preconditions; Note explains what is missing.
	Applicable bool   `json:"applicable"`
	Note       string `json:"note,omitempty"`
}

TestResult is the outcome of one randomness test.

func ApproximateEntropy

func ApproximateEntropy(bits []byte, m int) TestResult

ApproximateEntropy: compares the frequency of overlapping m- and (m+1)-bit patterns; structure lowers the apparent entropy.

func BinaryMatrixRank

func BinaryMatrixRank(bits []byte) TestResult

BinaryMatrixRank: do the ranks of disjoint 32×32 sub-matrices follow the expected distribution? Detects linear dependence among bit positions.

func BlockFrequency

func BlockFrequency(bits []byte, m int) TestResult

BlockFrequency: is the proportion of ones uniform across M-bit blocks?

func CumulativeSums

func CumulativeSums(bits []byte) TestResult

CumulativeSums (forward): treats the ±1 sequence as a random walk and tests whether the maximum excursion from zero is too large.

func LinearComplexity

func LinearComplexity(bits []byte, m int) TestResult

LinearComplexity: blocks the stream and tests the distribution of each block's shortest-LFSR length (via Berlekamp–Massey). Low linear complexity is the defining weakness of an LFSR-based scrambler, so this is the most RF-diagnostic test in the suite.

func LongestRun

func LongestRun(bits []byte) TestResult

LongestRun: is the longest run of ones within a block consistent with randomness? Uses the NIST block size / category table selected by length.

func Monobit

func Monobit(bits []byte) TestResult

Monobit (frequency) test: are roughly half the bits one? The most basic test; everything else assumes this passes.

func Runs

func Runs(bits []byte) TestResult

Runs: is the number of uninterrupted runs of identical bits what a random stream would produce? Detects oscillation that is too fast or too slow.

func Serial

func Serial(bits []byte, m int) TestResult

Serial: are all overlapping m-bit patterns equally likely? Returns a single result that passes only when both NIST sub-p-values pass.

func Spectral

func Spectral(bits []byte) TestResult

Spectral (DFT): does the discrete Fourier transform of the ±1 sequence show periodic features (spikes) inconsistent with randomness? This is the sharpest test for periodic RF scramblers and rolling codes.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL