Skip to content

dancinlab/qrng

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

15 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

qrng

🎲 qrng

Quantum RNG β€” 5-source provider registry Β· CURBy + ANU + NIST Beacon + hardware + audit Β· NIST SP 800-22 tier-1+ audit

License CI Spec Backends Audit Pure HEXA DOI Family

quantum Β· entropy Β· CURBy Β· ANU Β· NIST Beacon Β· hardware QRNG Β· NIST SP 800-22 Β· audit Β· provider registry Β· n=6 Β· dancinlab


qrng is a provider-side registry of quantum random byte sources. It does not itself amplify entropy (no HMAC-DRBG, no NIST SP 800-90A constructions β€” those belong to consumer packages). It returns raw bytes from one of 5 backends with a uniform QrngBytes struct + provenance message.

Note

Sister package of qmirror (quantum mirror substrate Β· consumer-side HMAC-DRBG amplifier), and member of the dancinlab HEXA family (parent: dancinlab/echoes). Provider/consumer boundary deliberate β€” zero code overlap, struct-shape compatible. See Β§"Boundary: qrng (provider) vs qmirror.qrng (consumer)".

At a glance

let r = qrng_audited_bytes(1024, "tier1+", "anu_legacy")
// r.ok                       : 0/1
// r.bytes_hex                : hex string
// r.audit_pass               : 0/1 (1 iff every non-skipped test passed)
// r.tests_run                : [AuditTestResult] β€” 5 entries (name, p_value, passed, skipped, note)
// r.audit_level_requested    : "tier1+" | "none"
// r.audit_level_delivered    : "tier1+" | "tier1-partial" | "tier1-none" | "none"
// r.tier                     : "T0" | "T1" | "T3" | ...
// r.vendor                   : resolved backend label
// r.alpha                    : 0.01 (NIST default)
// r.message                  : provenance string

Five backends shipped today:

Tier Name Vendor is_quantum is_local Cost Throughput
T0 mock_qrng deterministic LCG 0 1 $0 1 GB/s
T1 curby NIST + CU Boulder (Bell-test) 1 0 $0 8.5 bps (60s/pulse)
T1 anu qrng.anu.edu.au 1 0 $0 1 KB/s (1 req/min throttled)
T1 nist_beacon NIST (ECDSA-signed mixed entropy) 0 0 $0 8.5 bps (60s/pulse)
T3 hardware_qrng IDQ Quantis PCIe / ESP32 serial 1 1 $5000 (PCIe) / $10 (ESP32) 240 MB/s (PCIe)

Honest C3 note: nist_beacon is is_quantum=0 because it's vendor- classified as mixed entropy (HSM + multiple RNGs, possibly QRNG-augmented). Use as US sovereign sibling to ANU (AU) when geographic diversity matters. hardware_qrng is is_quantum=1 by vendor assertion (IDQ's NIST SP 800-90B health checks + ESP32 ADC noise) β€” independent NIST validation NOT performed by this package.

Status

  • v1.1.0 (2026-05-07) β€” integrated NIST SP 800-22 tier-1+ statistical audit via single-stage qrng_audited_bytes(n_bytes, audit_level, vendor) API (Boltz-2 paradigm absorption β€” entropy + audit + verdict in one call). Five tests: monobit, frequency-block, runs, longest-run, DFT spectral. See audit/module/audit.hexa.
  • v1.0.0 (2026-05-04) β€” initial standalone release; 5 backends + 4 abstraction modules; tests scaffolded fresh; dual-home boundary doc published.
  • 5 backends (T0/T1Γ—3/T3) Β· 4 abstraction modules Β· 1 integrated audit
  • Pure HEXA β€” raw9 strict; no .py in the runnable surface
  • 6 external consumers (anima / anima-physics / anima-eeg / nexus stub) refactor staged
  • Parent: dancinlab/echoes (HEXA family canonical)

Registered backends

curby β€” Bell-test verified (NIST + CU Boulder)

Twine-blockchain anchored Bell-inequality-violation pulses (loophole-free Bell test protocol). Free, no auth, 8.5 bps sustained (512 bits per 60-second pulse). twine_anchor field MUST be non-empty on live pulses (falsifier F_CURBY_03).

QRNG_LIVE=1 qrng collect --source curby --bytes 64

anu β€” vacuum-fluctuation photodetector (Australian National University)

Public REST (https://qrng.anu.edu.au/API/jsonI.php) returning uint8 arrays sampled from quantum vacuum fluctuations. Free legacy tier 1 req/min; chunks > 1024 bytes require pacing (use qrng collect with chained chunks).

QRNG_LIVE=1 qrng collect --source anu --bytes 128

nist_beacon β€” Beacon 2.0 (ECDSA P-384 signed pulses)

NIST Randomness Beacon 2.0 (beacon.nist.gov/beacon/2.0/pulse/last). Mixed-entropy (HSM + RNG composite, possibly QRNG-augmented β€” vendor self-classifies). Use as US sovereignty mirror to ANU when geographic diversity proof is required. ECDSA signature MUST be present (F_NIST_03).

hardware_qrng β€” local PCIe/USB-serial

Probes /dev/quantis* (IDQ Quantis PCIe/USB SDK), then /dev/cu.usbmodem* / /dev/cu.usbserial* (ESP32 / FTDI bridges). Live path requires QRNG_HW_LIVE=1; default is mock-mode (deterministic LCG byte- identical to mock_qrng).

mock_qrng β€” deterministic LCG

s = (1664525 Γ— s + 1013904223) mod 2^32. Same --seed β†’ same bytes byte-identical across runs. CI default; safety net for the router chain.

Integrated audit β€” qrng_audited_bytes()

Single-stage API that pulls entropy + runs NIST SP 800-22 tier-1+ statistical audit + returns ok/fail in one call (Boltz-2 paradigm absorption). Eliminates the "did the caller actually audit?" failure mode.

Five tests (NIST SP 800-22 Β§2.1, Β§2.2, Β§2.3, Β§2.4, Β§2.6):

Test Min bits Notes
monobit 100 frequency / proportion of ones
frequency_block 2560 M=128 N>=20; chi-square upper tail
runs 100 gated by monobit pre-check
longest_run 6272 M=128 K=5 N=49; class boundaries [<=4..>=9]
dft_spectral 1000 O(n^2) DFT, capped at 1024 bits (FFT deferred β€” see caveat 3)

Audit downgrade is automatic and labelled: a 64-byte (512-bit) pull with audit_level="tier1+" returns audit_level_delivered="tier1-partial" with monobit + runs running and the other three tests skipped with reason notes. audit_pass aggregates only the tests that actually ran (alpha=0.01).

hexa run audit/module/audit.hexa
QRNG_LIVE=1 hexa run audit/module/audit.hexa   # +ANU legacy live exercise

Selftest evidence:

  • pathological all-zeros fixture: audit FAIL (monobit p β‰ˆ 0; distinction proof)
  • mock LCG (n=128 bytes = 1024 bits): audit PASS (3/5 tests run; LCGs are statistically OK on short windows β€” caveat 4)
  • ANU legacy live (n=64 bytes = 512 bits): audit PASS (monobit + runs run)

Boundary: qrng (provider) vs qmirror.qrng (consumer drop-in)

Two repositories share the qrng name across different roles. Zero code overlap, but the API surface is dual-homed.

Repo Surface Role
qrng (this) <feature>/module/<feature>.hexa for {anu,curby,nist_beacon,hardware_qrng,mock_qrng} + abstraction triplet {source,registry,router,qrng_main} Provider registry β€” 5 backends + dispatch + router
qmirror modules/qrng.hexa (single file) Consumer drop-in β€” HMAC-DRBG amplifier exposed as qmirror qrng <bits>

They share the QrngBytes and QrngSourceMeta struct shapes by convention (both packages declare them locally; no shared import). Sentinel namespaces are disjoint (__QRNG_* vs __QMIRROR_QRNG__). Env var namespaces are disjoint (QRNG_* / NEXUS_QRNG_* vs QMIRROR_* / NEXUS_QMIRROR_*).

Future qmirror v3.0.0 may declare qrng = "^1.0.0" as a runtime dependency to consolidate the entropy pipeline; until then, each operates independently. Full rationale + falsifiers in docs/dual_home_boundary.md.

Architecture β€” 4-abstraction + 5-backend split

The nexus/core/qrng/ Option D split is preserved verbatim. The router resolution order (default chain curby β†’ anu β†’ nist_beacon β†’ hardware_qrng β†’ mock_qrng) is overridable via env:

  • QRNG_SOURCE=<name> β€” pin to single backend
  • QRNG_FALLBACK_CHAIN=a,b,c β€” comma-sep custom chain

Both legacy (NEXUS_QRNG_*) and forward (QRNG_*) env namespaces are honoured; legacy is load-bearing for backward compat with the upstream nexus origin.

Install

# 1. Install hexa-lang (gives you `hexa` + `hx` package manager)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/dancinlab/hexa-lang/main/install.sh)"

# 2. Install qrng
hx install qrng

System deps (assumed present on macOS / Linux): curl, xxd, shasum/sha256sum, timeout. The mock_qrng backend (CI default) needs none of these.

Run

qrng status                                          # registry table + tier coverage
qrng collect [--bytes=N] [--seed=S] [--source=NAME]  # pull random bytes via router fallback
qrng selftest                                        # full provider sweep (12 sentinels: 9 backends + 3 abstraction)
qrng chain                                           # show resolved router fallback chain
qrng meta --backend=NAME                             # print backend metadata
qrng --version                                       # show version
qrng --help                                          # full usage

# integrated audit (entropy + NIST SP 800-22 + verdict in one call):
hexa run audit/module/audit.hexa
QRNG_LIVE=1 hexa run audit/module/audit.hexa         # +ANU legacy live exercise

# examples (examples/):
hexa run examples/01_quick_collect.hexa              # pull bytes via default router chain
hexa run examples/02_qrng_for_ml_seed.hexa           # quantum-seeded ML training pattern
hexa run examples/03_router_fallback_demo.hexa       # show router attempt/reason chain

Env vars:

QRNG_ROOT             override repo root (default: inferred from $0)
QRNG_LIVE             1 β†’ enable live network paths (default: gated mock)
QRNG_MOCK             1 β†’ force mock LCG fixture (CI-safe)
QRNG_SOURCE           pin to single backend (skips fallback chain)
QRNG_FALLBACK_CHAIN   comma-sep custom fallback chain
QRNG_HW_LIVE          1 β†’ hardware_qrng live serial/PCIe path
QRNG_HW_MOCK          1 β†’ hardware_qrng mock fixture
QRNG_HW_TIMEOUT_S     serial read timeout in seconds (default 5)

legacy aliases (load-bearing):
  NEXUS_QRNG_LIVE / NEXUS_QRNG_MOCK / NEXUS_QRNG_SOURCE
  NEXUS_QRNG_FALLBACK_CHAIN / NEXUS_QRNG_HW_*
  ANIMA_QRNG_MOCK   (anima-side consumer alias)

Repo layout

qrng/
β”œβ”€β”€ README.md                              # this file
β”œβ”€β”€ LICENSE                                # Apache-2.0
β”œβ”€β”€ CHANGELOG.md
β”œβ”€β”€ CITATION.cff
β”œβ”€β”€ AGENTS.tape                            # identity + governance (.tape v1.2)
β”œβ”€β”€ CLAUDE.md                              # β†’ AGENTS.tape (Claude Code auto-discovery)
β”œβ”€β”€ hexa.toml                              # package manifest
β”œβ”€β”€ install.hexa                           # hx install hook (system dep check + selftest)
β”œβ”€β”€ source/module/source.hexa              # abstract interface contract (QrngBytes / QrngSourceMeta)
β”œβ”€β”€ registry/module/registry.hexa          # 5-backend dispatch table + canonical names
β”œβ”€β”€ router/module/router.hexa              # config-driven backend selection + fallback chain
β”œβ”€β”€ qrng_main/module/qrng_main.hexa        # aggregator + selftest entry-point
β”œβ”€β”€ audit/module/audit.hexa                # NIST SP 800-22 tier-1+ statistical audit + qrng_audited_bytes()
β”œβ”€β”€ mock_qrng/module/mock_qrng.hexa        # T0 deterministic LCG
β”œβ”€β”€ anu/module/anu.hexa                    # T1 ANU vacuum-fluctuation REST
β”œβ”€β”€ curby/module/                           # T1 NIST + CU Boulder Bell-test
β”‚   β”œβ”€β”€ curby.hexa
β”‚   └── fixtures/curby_pulse_sample.json
β”œβ”€β”€ nist_beacon/module/                     # T1 NIST Beacon 2.0 ECDSA-signed
β”‚   β”œβ”€β”€ nist_beacon.hexa
β”‚   └── fixtures/nist_beacon_pulse_sample.json
β”œβ”€β”€ hardware_qrng/module/hardware_qrng.hexa # T3 IDQ Quantis / ESP32 serial
β”œβ”€β”€ cli/qrng.hexa                          # CLI router (5 subcmds)
β”œβ”€β”€ tests/                                 # 7 smoke tests (one per backend + router + registry)
β”œβ”€β”€ examples/                              # 3 example scripts
β”œβ”€β”€ docs/                                  # cross-cutting docs (dual_home_boundary.md, logo.svg)
└── state/                                 # gitignored runtime markers

# Layout: feature-grouped triplet β€” each <feature>/ has {core?, module/, doc/} (singular).
# core/ is optional and absent for module-only features.

Caveats

  1. Dual-home boundary risk with qmirror.qrng β€” qmirror's modules/qrng.hexa is a consumer-side HMAC-DRBG amplifier API drop-in (NO code overlap) while THIS package is the 5-source provider registry. Future qmirror v3+ may declare qrng as a runtime dependency to unify entropy sourcing. See docs/dual_home_boundary.md.
  2. 6 external consumers (anima/.roadmap.qrng + 3 anima-physics files + anima-eeg + nexus/core/qrng provider stub) require refactor to point at standalone path; refactor staged for user review (NOT auto-applied).
  3. Tests scaffolded fresh at extraction β€” phase 2 audit found NO tests/ subdir in nexus origin (modularity 2β†’1 demotion). Coverage is tier 1 (sentinel-pass per backend); deeper property-based tests deferred.
  4. ANU rate-limit + ToS evolution β€” public REST is throttled to 1 req/min on T1.a legacy tier; ToS / redistribution terms may evolve. Mock LCG fallback always available with QRNG_MOCK=1.
  5. License audit deferred to follow-up cycle β€” qrng core is Apache-2.0 (clean), but per-vendor data-rights for ANU / CURBy / NIST Beacon byte redistribution NOT formally audited. This package returns bytes to the caller; it does not redistribute. Callers re-publishing returned bytes should consult vendor ToS independently.

Sister packages (HEXA family)

qrng is the 6th publishable HEXA-family package. The full set:

Package Version Repo Description
qmirror 2.0.0 https://github.com/dancinlab/qmirror Quantum mirror substrate (CHSH/IIT/NIST)
sim-universe 1.0.0 https://github.com/dancinlab/sim-universe Virtual universe runtime + Bostrom test
hexa-bio 1.0.0 https://github.com/dancinlab/hexa-bio Molecular toolkit on n=6 lattice
honesty-monitor 1.0.0 https://github.com/dancinlab/honesty-monitor AI honesty-bit falsifier
anima-agent 1.0.0 https://github.com/dancinlab/anima-agent Ξ¦-gated autonomous agent runtime
qrng 1.1.0 https://github.com/dancinlab/qrng 5-source QRNG provider registry (this)

Provenance

Extracted from the nexus repo on 2026-05-04 after phase 2 audit verdict extract_standalone_qrng_repo_with_caveats (rank 7, score 7). Upstream provenance:

  • nexus/modules/qrng/ (5 backends, 1047 LoC)
  • nexus/core/qrng/ (4 abstraction modules, 546 LoC)
  • Total: 1593 LoC migrated

License

Apache-2.0 β€” Copyright 2026 λ°•λ―Όμš° nerve011235@gmail.com.

Per-vendor entropy sources carry their own data-rights regimes:

Backend Data rights Notes
mock_qrng Apache-2.0 (qrng core) LCG arithmetic β€” no third-party data
anu ANU ToS Free public REST; user-of-record is the caller
curby NIST + CU Boulder ToS Bell-test pulses; Twine anchors public
nist_beacon NIST public domain US-government work; ECDSA-signed
hardware_qrng Vendor SDK terms (IDQ libQuantis) Local hardware; no redistribution

Optional Python aux deps (none today) would ship under their own licenses; in-process safe (no copyleft). qrng core stays Apache-2.0 under FSF MereAggregation.

About

🎲 qrng β€” Quantum Random Number Generator (5-source provider registry: ANU + CURBy + NIST Beacon 2.0 + IDQ Quantis + LCG fallback). Pure-hexa raw#9 STRICT. Apache-2.0.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors