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 almost all practical cases, the guest will want to read private input data from the host and write public data to the journal. This can be done with env::read and env::commit, respectively; additional I/O functionality is also available in env.
§Installation
To build and run RISC Zero zkVM code, you will need to install the RISC Zero
toolchain, which can be done using the
cargo-risczero
tool:
cargo install cargo-binstall
cargo binstall cargo-risczero
cargo risczero install
§Example
The following guest code1 proves a number is
composite by multiplying two unsigned integers, and panicking if either is
1
or if the multiplication overflows:
#![no_main]
#![no_std]
use risc0_zkvm::guest::env;
risc0_zkvm::guest::entry!(main);
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 env::read is used to load the two factors, and env::commit is used to make their composite product publically available. All input an output of your guest is private except for what is written to the journal with env::commit.
By default, the guest only has the Rust core
libraries and not std
. A partial
implementation of the Rust standard libraries can be enabled with the std
feature on this crate.
When this feature is not enabled, the lines including #![no_std]
and #![no_main]
are
required, as well as the use of the crate::guest::entry macro. When std
is enabled, these
three lines can be omitted and many features of std
can be used.
If you encounter problems building zkVM guest code, you can see if we have a known workaround for your issue by looking in our rust guest workarounds tag on GitHub.
The example is based on the Factors example. ↩
Modules§
- Functions for interacting with the host environment.
- Functions for computing SHA-256 hashes.
Macros§
- Used for defining the guest’s entrypoint and main function.
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.