crates

entropy_contract

check proofs license minimum Rust version

The words a client and an entropy service exchange to move random bytes, and the rules for reading what comes back. no_std, no dependencies, no system calls of its own: the caller supplies the CALL. A CALL is the kernel's synchronous message call, one word in and two words back, sent to an endpoint: a capability that names the service. The service, its clients and any library that wraps the call share this one definition, so they cannot drift. A backend is the driver for one kind of random-number device.

The contract

A CALL carries one request word and returns two reply words.

EXAMPLES

use entropy_contract::{GET, MAXIMUM_BYTES, delivered_bytes, request, copy_delivered_bytes, requested_bytes};

// Stands in for the service: it answers with a count and a word of bytes.
fn service(request_word: u64) -> (u64, u64) {
    (requested_bytes(request_word), 0x0807_0605_0403_0201)
}

let mut key = [0u8; 20];
let mut filled = 0;
while filled < key.len() {
    let (first_reply_word, reply_word) = service(request(GET, (key.len() - filled) as u64).expect("GET fits"));
    let byte_count = delivered_bytes(first_reply_word).expect("the service answered");
    assert_ne!(byte_count, 0, "a zero count is NO_ENTROPY: fail, do not retry");
    filled += copy_delivered_bytes(byte_count, reply_word, &mut key[filled..]);
}
assert_eq!(MAXIMUM_BYTES, 8);

fill_with(out, call) runs a stricter form of that loop, given a closure that performs the CALL. It refuses a partial count because a caller filling a key must never hold fewer random bytes than it asked for and believe otherwise. It returns Ok(()) for an empty slice without calling the closure. It refuses any reply whose count differs from the one it asked for. It returns Err(Refusal::NoService) when a reply is not a count, and Err(Refusal::ShortReply { asked, delivered }) when a reply carries a different count than the request asked for, a larger one included. A dry device causes the second, so a caller can tell a missing grant from an empty source. Refusal is #[non_exhaustive], so a match on it needs a wildcard arm.

A service answers one request by checking that the word is well formed, checking the opcode, clamping the count and building the reply. Here draw stands in for the device:

use entropy_contract::{
    GET, NO_ENTROPY, MAXIMUM_BYTES, operation, reply, request, request_is_well_formed,
    requested_bytes,
};

fn handle(request_word: u64, draw: &mut impl FnMut(&mut [u8])) -> (u64, u64) {
    if !request_is_well_formed(request_word) || operation(request_word) != GET {
        return (NO_ENTROPY, 0);
    }
    let mut bytes = [0u8; MAXIMUM_BYTES as usize];
    let count = requested_bytes(request_word) as usize; // already at most MAXIMUM_BYTES
    draw(&mut bytes[..count]);
    reply(&bytes[..count])
}

let mut device = |out: &mut [u8]| out.fill(0xab);
assert_eq!(handle(request(GET, 3).expect("GET fits"), &mut device), (3, 0x00ab_abab));
assert_eq!(handle(request(2, 3).expect("2 fits"), &mut device), (NO_ENTROPY, 0));

What is proven

Seven Kani harnesses in src/lib.rs prove these properties, each within the stated bounds:

The table gives each harness's full path, the property it proves and the falsification patch that turns it red. cargo kani list prints the harnesses. A patch in falsifications/ takes the name verification.<harness>.patch.

Harness Property Falsification patch
verification::a_request_word_carries_its_opcode_and_clamped_count a request reads back as built verification.a_request_word_carries_its_opcode_and_clamped_count.patch
verification::an_opcode_or_step_past_one_byte_is_refused an opcode or a step past one byte is refused verification.an_opcode_or_step_past_one_byte_is_refused.patch
verification::copying_writes_exactly_the_delivered_bytes a copy writes only the delivered bytes verification.copying_writes_exactly_the_delivered_bytes.patch
verification::a_fill_succeeds_exactly_when_every_reply_is_full a fill succeeds exactly when every reply is full verification.a_fill_succeeds_exactly_when_every_reply_is_full.patch
verification::a_reply_reads_back_as_built a reply reads back as built verification.a_reply_reads_back_as_built.patch
verification::readiness_refuses_a_repeated_first_draw_of_at_least_eight_bytes readiness refuses a repeated first draw verification.readiness_refuses_a_repeated_first_draw_of_at_least_eight_bytes.patch
verification::readiness_is_reported_exactly_when_a_first_byte_is_nonzero readiness is READY exactly when a byte is nonzero verification.readiness_is_reported_exactly_when_a_first_byte_is_nonzero.patch

Every harness carries a falsification patch in falsifications/, a small defect that turns the harness red. Unit tests and doctests cover the rest, among them that no byte count collides with a kernel error.

Building and testing

You need git, a Rust toolchain installed with rustup and a C linker. The proofs also need patch. Get the source and build from its top directory:

git clone https://github.com/nifeos/entropy_contract
cd entropy_contract
cargo test
rustup target add x86_64-unknown-none aarch64-unknown-none-softfloat riscv64imac-unknown-none-elf
cargo build --lib --target x86_64-unknown-none
cargo build --lib --target aarch64-unknown-none-softfloat
cargo build --lib --target riscv64imac-unknown-none-elf

The crate has no features and no dependencies, so a plain cargo test runs every unit test and doctest.

Running the proofs

Install Kani first: run cargo install --locked kani-verifier --version 0.68.0, then cargo kani setup. Both need network access. cargo install builds only Kani's small installer, about ten crates. Setup downloads Kani's release bundle, about 140 megabytes, and the nightly Rust toolchain Kani is pinned to. It then holds about 485 megabytes in ~/.kani and 1.5 gigabytes in ~/.rustup. On the host measured below, from empty Cargo and rustup directories, the install took 5 seconds and setup 30. Release bundles exist for x86_64 and aarch64 Linux and macOS, and not for Windows. Then run every harness:

cargo kani

The seven harnesses take about one minute and peak near 2.2 gigabytes of memory, as /usr/bin/time -v cargo kani (Linux only; macOS has no -v) measured on a shared 80-core x86_64 host. Almost all of the time is a_fill_succeeds_exactly_when_every_reply_is_full; each other harness takes seconds. To run one harness, give its full path, because --exact refuses a bare name:

cargo kani --harness verification::a_reply_reads_back_as_built --exact

A passing run prints lines that look alarming and mean nothing. They are the use of an unstable feature warning for register_tool, the Found the following unsupported constructs warning, and many aborting path on assume(false) lines, which are paths Kani discards on purpose. The answer is the last line, Complete - 7 successfully verified harnesses, 0 failures, 7 total., and cargo kani exits 0.

Running one falsification patch

Each file in falsifications/ applies a small defect that turns its harness red. Apply one, confirm the proof fails, then undo it with the same patch reversed:

patch -p1 --no-backup-if-mismatch < falsifications/verification.readiness_is_reported_exactly_when_a_first_byte_is_nonzero.patch
cargo kani --harness verification::readiness_is_reported_exactly_when_a_first_byte_is_nonzero --exact
patch -R -p1 --no-backup-if-mismatch < falsifications/verification.readiness_is_reported_exactly_when_a_first_byte_is_nonzero.patch

patch may report Hunk #1 succeeded at a line number with an offset; that is harmless. The failing run prints Failed Checks: assertion failed: word == READY and VERIFICATION:- FAILED. It ends with Complete - 0 successfully verified harnesses, 1 failures, 1 total., and cargo kani exits 1. Each patch's opening lines name the check it expects to fail.

Using it

The project has not published this crate, and publishing is on hold. No release tag exists yet, so there is no tag to name; the tag is pending. Depend on it by git, pinned to a commit of this repository's main and never to a branch:

[dependencies]
entropy_contract = { git = "https://github.com/nifeos/entropy_contract", rev = "<full commit hash>" }

Once a pre-release tag exists, use tag = "<that tag>" in place of rev.

The service holds the random-number device. A client holds an endpoint that names no device, sends a request word naming a byte count, and gets the bytes back in the reply words. The service transforms no byte on the way, so the claim stays "these are the device's bytes".

BUGS

Minimum Rust version

Rust 1.88. To check it, install the toolchain and its bare-metal targets, then run the tests and all three bare-metal builds with +1.88:

rustup toolchain install 1.88 --profile minimal
rustup target add --toolchain 1.88 x86_64-unknown-none aarch64-unknown-none-softfloat riscv64imac-unknown-none-elf
cargo +1.88 test
cargo +1.88 build --lib --target x86_64-unknown-none
cargo +1.88 build --lib --target aarch64-unknown-none-softfloat
cargo +1.88 build --lib --target riscv64imac-unknown-none-elf

License

Licensed under either of MIT (LICENSE-MIT) or Apache-2.0 (LICENSE-APACHE), at your option.