Using the Rust Toolchain
Last updated
Last updated
For basic instructions on using the Rust to Valida compiler toolchain, see . This page covers more advanced usage instructions for the Rust to Valida toolchain.
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 println
macro is known to work. The following methods are known to work on stdin
: read
, read_line
, read_until
, lines
, read_to_string
, and read_to_end
. There may be some issues with signaling end of input via ctrl+D on the command line. Sometimes it may have to be signaled more than once.
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
The Valida VM has the capability of accelerated Keccak hash proving. To use this capability in Rust, you can simply import sha3::Keccak256
and use the Keccak hasher in the sha3
crate in the usual way. The keccak-crate
example located at /valida-toolchain/examples/rust/keccak-crate
exemplifies this usage. You must use Lita's forked version of the Keccak crate, using a line in your Cargo.toml
such as:
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.