crates

jh7110_entropy

check proofs license minimum Rust version

The logic of a driver for the true random number generator (TRNG) in the StarFive JH7110, the RISC-V system-on-chip of the VisionFive 2. It holds the register layout and the decision of whether a generation succeeded, failed or is still running. It also holds a small pool that serves the 32 bytes of one generation without serving any byte twice, and what each of the chip's two device trees calls the block. It never touches a register: you map the window and do the volatile reads, and this crate decides what they mean. It is no_std and tested on the host. It has no dependencies and holds no unsafe code.

EXAMPLES

use jh7110_entropy::{GenerationOutcome, generation_outcome, is_status_okay, INTERRUPT_STATUS_RANDOM_NUMBER_READY, STATUS_256_BIT_OUTPUT, STATUS_SEEDED};

// Firmware that says nothing about `status` means "okay".
assert!(is_status_okay(None));
assert!(!is_status_okay(Some(b"disabled\0")));

// A seeded core in 256-bit mode with RAND_RDY set: the eight words, little-endian, word 0 first.
let random_numbers = [0x0403_0201, 0, 0, 0, 0, 0, 0, 0];
let status = STATUS_SEEDED | STATUS_256_BIT_OUTPUT;
match generation_outcome(status, INTERRUPT_STATUS_RANDOM_NUMBER_READY, random_numbers) {
    GenerationOutcome::Ready(bytes) => assert_eq!(&bytes[..4], &[1, 2, 3, 4]),
    other => panic!("expected Ready, got {other:?}"),
}

// The same snapshot from a core left in 128-bit mode is refused, not served.
assert_eq!(
    generation_outcome(STATUS_SEEDED, INTERRUPT_STATUS_RANDOM_NUMBER_READY, random_numbers),
    GenerationOutcome::WrongOutputWidth
);

A driver writes MODE_256_BIT_OUTPUT during bring-up. Otherwise only four words carry the answer, and generation_outcome answers WrongOutputWidth until the driver writes it. The driver then clears ISTAT with INTERRUPT_STATUS_DOCUMENTED_BITS_MASK, asks for a reseed with CONTROL_COMMAND_RESEED, then for each request writes CONTROL_COMMAND_GENERATE, polls STAT and ISTAT, and hands them to generation_outcome. Once the outcome is Ready and the eight words are read, the driver acknowledges the request by writing INTERRUPT_STATUS_RANDOM_NUMBER_READY (RAND_RDY) to INTERRUPT_STATUS_REGISTER. The bit is write-1-to-clear, so without that write it stays latched and the next request sees it on its first poll. On Lockup or Unseeded it reseeds and retries, bounded. On NotReady, which also covers a generation or a reseed still in flight, it polls again rather than issuing a command. The crate sets no bound, no poll interval and no timeout: you pick them, ideally from a measurement on your board. On the one board measured under Hardware evidence, a generation took at most 6.25 microseconds and a reseed at most 50.75 microseconds, so a bound well above those leaves margin; one board is not a datasheet figure. Wrap Pool and the register access in one lock; Pool does no locking of its own.

What is proven

Six Kani harnesses exhaust generation_outcome, the function that turns one snapshot of the status register (STAT) and the interrupt status register (ISTAT), plus the eight random-output words (RAND), into an answer:

Two more cover Pool::take, over every pool state and every request size, and through it Pool::take_into, which take calls with an eight-byte slice:

Each harness has a falsification record at falsifications/<module.path>.<harness>.patch; applying it with git apply -p1 from the crate root makes that harness fail at its own assertion, which shows the proof checks something. The patches need a git checkout, and you revert each one with git checkout -- src before the next.

Not proven: the quality of the randomness, which is a property of the silicon; that dropping a Pool zeroes its buffer, which safe code cannot observe; and the device-tree decoders, which have unit tests. take_into with a slice longer than 8 bytes has unit tests, not a proof, including one that crosses more than one refill. Pool::take serves at most WORD_BYTES, 8 bytes, per call, and its doc comment carries a runnable example.

Build, test and prove

You need a Rust toolchain at or above the minimum version at the foot of this page (rustup is the usual way to get one) and a git checkout. From the crate root:

The two device trees

Mainline Linux names the block starfive,jh7110-trng. The vendor U-Boot's control tree, which a VisionFive 2 boots with, names the same block starfive,trng, at the same window (0x1600_C000, 0x4000 bytes) and interrupt (30), and marks it status = "disabled" because U-Boot has no driver for it. COMPATIBLES lists both, mainline's first. Walk your tree for a node carrying one, read its reg, decode interrupts with interrupt_line and status with is_status_okay, and keep the result in a Discovered. A disabled node is the first suspect when the window reads all zeros. The crate has no helper that walks a tree for you; it decodes the properties and you do the walking with your own parser.

Hardware evidence

Version 0.3.0-pre.1 (tag v0.3.0-pre.1, commit 497d3ec) has run on a VisionFive 2 (JH7110): it served real TRNG output on two cold boots with the chip in 256-bit mode, the draws differing within each boot and across the boots. The clock and reset must be ungated and released first, as the first item under BUGS says.

The record below is of an earlier commit, ee0bde8, which a register-level check ran on its own.

Run on 2026-11-09 (UTC), on one VisionFive 2, against commit ee0bde8feb9f78a67da011c928f9a2a697f3bbc1 of this repository. A small bare-metal RISC-V payload built on that commit's constants and its generation_outcome performed the register sequences below and printed one checksummed line per result; a script judged the log. The payload came over the network, and the person running it powered the board on from off. The payload's SHA-256 is e421000bc0b66d35ac60315cbe618ba1c8e15fb46d9bf8aac44f3a27b82054e8; the console log's is 540996ad74682bd9fbe350e897028af2310113a9d210df37590847b40a2c960e and the 1,310,720-byte capture's is 97fc5325be5ca81d31cfb85b73aaea73191efc474d24d25a0af82f0817079263. The log holds two boots (the first fell back to the card's own kernel); the judged run is the second. The checker printed PASS, 0 problem(s).

What passed, with the board as the judge:

What the board answered, recorded and not judged:

Statistics, on one 1,310,720-byte capture (40,960 generations, every record's checksum matched): 2 of 524 Federal Information Processing Standard 140-2 blocks failed. A good source fails about one in 1,250, and the check's bar was at most 3. Shannon entropy was 7.999854 bits per byte, and the chi-square over byte values was 264.17 (upper-tail p 0.3333). The mean byte was 127.5159, the lag-1 serial correlation -0.000949, and no 32-byte generation repeated. No outside statistical suite ran.

One capture showing no gross defect says nothing about unpredictability. A stream that looks good from one boot is also what a weak source produces. A health test and a measurement in the manner of National Institute of Standards and Technology Special Publication 800-90B are still absent.

Second run, 2026-11-10 (UTC): the same commit and the same payload, on the same board. The person ran it after the board sat unpowered for about an hour, which is their statement, since nothing reports the plug state. The payload came over the network on the first boot this time, so the log holds one boot. The log's SHA-256 is fde9661f55f814865cb6f843b76f93ff5d3717a1c6290ebbd072db48500c8a92 and the capture's is 208c42b41ae4118ae000ee2839cc62ecd182e3d4f760b20694395f8b2529637f. The checker printed PASS, 0 problem(s).

Two runs show that the reading repeats. They remain two measurements. They do not show that the output is unpredictable.

Not established:

BUGS

Minimum Rust version

1.85, the manifest's rust-version.

License

MIT OR Apache-2.0, at your option: see LICENSE-MIT and LICENSE-APACHE.

What the crate takes from Linux, NetBSD, the vendor trees and the JH7110 manual, and why it owes them no notice, is in docs/the-crate-carries-facts-not-source-text.md.