Using the Rust Toolchain

For basic instructions on using the Rust to Valida compiler toolchain, see QUICK START: Valida Compiler Toolchain. This page covers more advanced usage instructions for the Rust to Valida toolchain.

Input and output

Valida programs can interact with the environment using the input and output tapes. In the Valida zk-VM, the input tape is mapped to stdin, or the input file specified on the command line. The output tape is mapped to stdout, and the output file specified on the command line for valida run.

The Rust toolchain provides functions for line-based interaction with the input and output tapes:

  • valida_rs::io::read_line<T>() -> Result<T, Box<dyn Error>>

    • Reads a line from the input tape and returns the result

    • T: std::str::FromStr

    • <T as std::str::FromStr>::Err: std::error::Error + 'static

  • valida_rs::io::read() -> Result<Vec<u8>, Box<dyn Error>>: reads all of the input from the input tape and returns it

  • valida_rs::io::read_until(stop_char: u8) -> Result<Vec<u8>, Box<dyn Error>>: reads from the input tape until reaching the end of input or stop_char

  • valida_rs::io::read_n(n: usize) -> Result<Vec<u8>, Box<dyn Error>>: reads n many bytes from the input tape

  • valida_rs::io::write_vec(v: impl AsRef<[u8]>) -> Result<(), Box<dyn Error>>: writes the contents of v to the output tape

  • valida_rs::io::read_and_deserialize<T: DeserializeOwned>() -> Result<T, Box<dyn Error>>: reads and deserializes an object from the input tape

  • valida_rs::io::write<T: Serialize>(value: &T) -> Result<(), Box<dyn Error>>: serializes and writes value to the output tape

Pseudo-random numbers

The Rust toolchain provides the following function for generating pseudo-random numbers:

  • valida_rand(s: &mut [u8]) - > Result<(), getrandom::Error>: populates s with pseudo-randomly generated bytes

Note that currently this function uses a static seed and thus behaves deterministically, producing the same result on each run of a program.

Last updated