Module risc0_zkvm::guest

source ·
Expand description

The RISC Zero ZKVM’s guest-side RISC-V API.

Code that is validated by the RISC Zero zkVM is run inside the guest. In the minimal case, an entrypoint (the guest’s “main” function) must be provided by using the entry! macro. In almost all practical cases, the guest will want to read private input data using env::read and commit public output data using env::commit; additional I/O functionality is also available in env.

For example1, the following guest code proves a number is composite by multiplying two unsigned integers, and panicking if either is 1 or if the multiplication overflows:

use risc0_zkvm::guest::env;

risc0_zkvm::entry!(main);

pub fn main() {
    // Load the first number from the host
    let a: u64 = env::read();
    // Load the second number from the host
    let b: u64 = env::read();
    // Verify that neither of them are 1 (i.e. nontrivial factors)
    if a == 1 || b == 1 {
        panic!("Trivial factors")
    }
    // Compute the product while being careful with integer overflow
    let product = a.checked_mul(b).expect("Integer overflow");
    env::commit(&product);
}

Notice how the entry! macro is used to indicate the entrypoint, env::read is used to load the two factors, and env::commit is used to make their composite product publically available.


  1. The example is based on the RISC Zero Rust Starter repository

Modules

Functions for interacting with the host environment.
Functions for computing SHA-256 hashes.

Macros

Used for defining a main entrypoint.

Functions

Aborts the guest with the given message.
Require that accesses to behind the given pointer before the memory barrier don’t get optimized away or reordered to after the memory barrier.