Tutorial: Rust via Docker

This is a tutorial on setting up and using the Dockerized Valida zk-VM stack to write a Rust program, compile it to Valida, and provide its execution. The Valida Compiler Toolchain and Valida zk-VM pages provide the same information about the stack, in a more concise and complete reference-style format. You can safely skip this tutorial if you prefer that style of presentation, since there is no information about the stack which is contained in this tutorial and nowhere else.

Installation and cloning the project template

This section covers the following steps:

  1. Install the toolchain via Docker.

  2. Clone the Rust project template.

  3. Validate that the installation is working correctly.

You can see all of the steps in this section performed in this video demo:

Prerequisites

First make sure that your system meets the requirements for following this tutorial. You will need a system which can run Docker containers. Your user will need to be part of the docker group. You will need at least 10 GB of free space on your filesystem.

Installation

To install the toolchain as a Docker image, run:

Cloning the template project

To clone the Rust project template, run:

Validating the installation

Follow the steps in this section to ensure that the toolchain is working correctly on your machine. After performing the above steps, cd into the template project:

Next, enter the Docker container.

For x86_64 systems:

For ARM64 systems:

From within the Docker container, build the template project:

Next, run the template project:

You should see a prompt that looks like this:

Enter a number between 0 and 46, and press enter. At this point, the program should print the answer and exit. For example:

At this point, you have validated that the toolchain is working on your system. You have also demonstrated how to compile a Rust program to a Valida executable, and run it on the zk-VM.

Looking at the template project

In the previous steps, you cloned the fibonacci template project. This contains a single Rust source file, src/main.rs. This is roughly what it should look like if you open it up in a text editor:

This program computes an element of the Fibonacci sequence based on user input.

Let's delete the application-specific logic from this file, in order to come up with a generic starting point for the project:

Application logic

Let's create a program which proves knowledge of the prime factorization of a 32-bit unsigned integer.

This tutorial demonstrates that multi-file Rust projects are supported by the Valida toolchain. We create a new file, call it src/lib.rs, which we fill with the following contents:

This module defines three functions: read_number, check_prime_factorization, and is_prime. Here is what each of these functions does:

  • The read_number function attempts to read an unsigned 32-bit integer from the input tape. If it fails to do so, it outputs an error message and again attempts to read an unsigned integer, continuing until it succeeds.

  • The check_prime_factorization function checks that ys is a prime factorization of x, outputting true if this is the case and false otherwise.

  • The is_prime function outputs true if x is a prime number, and false otherwise.

Next, we'll edit the src/main.rs file so that it looks like this:

This modifies the template src/main.rs file by adding imports from src/lib.rs and populating the main function. The new main function reads a 32-bit number x, followed by its number of prime factors, and then each of its prime factors. It then checks whether the claimed prime factors are in fact the prime factors of x, providing output which indicates whether or not this is the case.

Renaming the project

Let's rename the project so that it is no longer called fibonacci. First open the Cargo.toml file in the root folder of the project. You should see something like this:

Edit the line name = "fibonacci" to read instead name = "prime_factorization".

Building, running, proving, and verifying

To build the program to run on Valida, simply run:

To run the program:

You will get a series of prompts. Here is an example interaction with this program:

At this point, the output of the program will have been written to log. To prove an execution of the program, run:

You will again get a series of prompts, which you should respond to in the same way. The result should be that a proof of that execution is written to proof. You can verify this proof as follows:

Last updated