Lita Docs
  • Introduction
    • Getting Started
  • Quick Start
    • Tutorial: Rust via Docker
    • Installation & System Requirements
    • Valida Compiler Toolchain
      • Rust Usage
      • C Usage
      • WASM Usage
      • Rust API
      • Client-side API
    • Valida zk-VM
  • ADVANCED USAGE
    • zk-VM: Advanced Usage
    • Using the Rust Toolchain
    • Using LLVM libc
  • Architecture
    • Overview
    • Proving System: Plonky3
      • Future Directions
    • Valida zk-VM
      • Technical Design: VM
      • Technical Design: Prover
      • GitHub Link
    • LLVM-Valida Compiler
      • Technical Design
      • GitHub Link
    • Benchmarks
      • Fibonacci (vs. RISC Zero)
      • Fibonacci (vs. SP1)
      • Fibonacci (vs. Jolt)
      • Fibonacci (Rust vs. C)
      • SHA-256 (vs. RISC Zero)
      • SHA-256 (vs. SP1)
      • SHA-256 (vs. Jolt)
  • Core Concepts
    • zk-VM
    • Proofs: Classical, Probabilistic, Succinct, and ZK
    • Evaluating zk-VMs
    • ZK-VM Design Tradeoffs
    • Valida Design Considerations
  • Contributing
    • Overview
    • Early Access Program
Powered by GitBook
On this page
  • Input and output
  • Keccak acceleration
  • Pseudo-random numbers
  1. ADVANCED USAGE

Using the Rust Toolchain

Previouszk-VM: Advanced UsageNextUsing LLVM libc

Last updated 2 months ago

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.

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 printlnmacro 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

Keccak acceleration

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:

[dependencies]
sha3 = { git = "https://github.com/lita-xyz/hashes", default-features = false }

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.

QUICK START: Valida Compiler Toolchain