Skip to content

Commit eb455b2

Browse files
feat: update compiler & CLI tutorial
1 parent 2003501 commit eb455b2

File tree

1 file changed

+192
-0
lines changed

1 file changed

+192
-0
lines changed

docs/compiler/is_prime_rust.md

+192
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
# Writing a MidenVM Program In Rust
2+
3+
*Using the Miden compiler to write programs in Rust and generate a proof of computation using the MidenVM CLI*
4+
5+
## Overview
6+
7+
In this guide, we will write a simple Rust program that checks whether an integer is prime. We will compile the Rust program into a Miden package and run it in the Miden VM. We will also see how to use the Miden CLI to generate a STARK proof that the computation was performed correctly.
8+
9+
## What we'll cover
10+
11+
- Installing the Miden Rust compiler
12+
- Writing a basic `is_prime` program in Rust
13+
- Proving execution of the `is_prime` program
14+
- Verifying the STARK proof of the `is_prime` program execution
15+
16+
## Limitations and Important Considerations
17+
18+
Please note these current limitations of the Miden compiler:
19+
- **No Floating Point Support:** Only integer arithmetic is supported (e.g., `u32`, `u64`, etc.).
20+
- **No Standard Library:** Programs must be written with `#![no_std]`, limiting you to core library functionality.
21+
- **Entrypoint Constraints:** The `entrypoint` function can accept at most **16 inputs** on the stack and produces a single `u32` output.
22+
23+
## What We'll Cover
24+
25+
- Writing basic programs in Rust using the Miden compiler.
26+
- Running programs in the Miden VM.
27+
- Generating proofs for program execution using the Miden CLI.
28+
29+
## Step 1: Installing the Miden Compiler
30+
31+
Clone the repository and install the compiler:
32+
```bash
33+
git clone https://github.com/0xpolygonmiden/compiler
34+
cd compiler
35+
git checkout next
36+
```
37+
38+
Then install the Miden compiler:
39+
```bash
40+
cargo install --path midenc --locked
41+
```
42+
43+
and the cargo-miden toolchain:
44+
```bash
45+
cargo install --path tools/cargo-miden --locked
46+
```
47+
48+
## Step 2: Writing the Rust Program
49+
50+
Outside of the compiler repository, create a new Miden project:
51+
```bash
52+
cargo miden new is_prime
53+
cd is_prime
54+
```
55+
56+
Add the following Rust code to `is_prime/src/lib.rs`. This code checks whether a number is prime:
57+
```rust
58+
#![no_std]
59+
60+
// Custom panic handler since we don't have the standard library.
61+
#[cfg(not(test))]
62+
#[panic_handler]
63+
fn my_panic(_info: &core::panic::PanicInfo) -> ! {
64+
loop {}
65+
}
66+
67+
/// Returns true if the integer is prime.
68+
fn is_prime(n: u32) -> bool {
69+
if n <= 1 {
70+
return false;
71+
}
72+
if n <= 3 {
73+
return true;
74+
}
75+
if n % 2 == 0 || n % 3 == 0 {
76+
return false;
77+
}
78+
let mut i = 5;
79+
while i * i <= n {
80+
if n % i == 0 || n % (i + 2) == 0 {
81+
return false;
82+
}
83+
i += 6;
84+
}
85+
true
86+
}
87+
88+
/// The entry point to the Miden program.
89+
/// Due to current limitations, it accepts only a single `u32` input (from up to 16 stack inputs)
90+
/// and produces a single `u32` output (1 for true, 0 for false).
91+
#[no_mangle]
92+
fn entrypoint(n: u32) -> bool {
93+
is_prime(n)
94+
}
95+
```
96+
97+
Add this code into your project's `src/lib.rs` file.
98+
99+
Next, create an `is_prime/inputs.toml` file:
100+
```toml
101+
[inputs]
102+
stack = [2147482583]
103+
```
104+
105+
This file sets the value that will be passed into our `entrypoint` function when the program runs.
106+
107+
## Step 3: Running the Program in the Miden VM
108+
109+
Compile your program with:
110+
```bash
111+
cargo miden build --release
112+
```
113+
114+
Run your compiled Miden assembly program using:
115+
```bash
116+
midenc run target/miden/release/is_prime.masp --inputs inputs.toml
117+
```
118+
119+
The output will look like this:
120+
```
121+
Run program: target/miden/release/is_prime.masp
122+
-------------------------------------------------------------------------------
123+
Executed program with hash 0x79689b17ab6286cfde4651ef1f675cab19ad4efd9defd2c43001a06e7cbd8c40 in 2 seconds
124+
Output: [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
125+
VM cycles: 2039234 extended to 2097152 steps (2% padding).
126+
├── Stack rows: 1668454
127+
├── Range checker rows: 61329
128+
└── Chiplets rows: 2039234
129+
├── Hash chiplet rows: 1792040
130+
├── Bitwise chiplet rows: 247192
131+
├── Memory chiplet rows: 1
132+
└── Kernel ROM rows: 0
133+
```
134+
135+
The program returns `1` if the integer passed to the `is_prime` function is prime and `0` if it is not.
136+
137+
## Step 4: Generating a zk proof of the `is_prime` program execution
138+
139+
First install the Miden CLI by cloning the Miden VM repository and checking out the `next` branch:
140+
```bash
141+
git clone [email protected]:0xPolygonMiden/miden-vm.git
142+
cd miden-vm
143+
git checkout next
144+
```
145+
146+
Build and install the Miden VM CLI:
147+
```
148+
cd miden
149+
cargo install --path . --features concurrent,executable
150+
```
151+
152+
After installation is complete, return to the `is_prime` directory.
153+
154+
The current input file format for the Miden VM differs slightly from that of the compiler. This means we need to create an `is_prime.inputs` file at the root of the `is_prime` directory:
155+
```json
156+
{
157+
"operand_stack": ["2147482583"]
158+
}
159+
```
160+
161+
Now, using the Miden VM CLI tool, we can prove our program by running the following:
162+
```
163+
miden prove target/miden/release/is_prime.masp -i is_prime.inputs
164+
```
165+
166+
The output should look like this:
167+
168+
```
169+
===============================================================================
170+
Prove program: target/miden/release/is_prime.masp
171+
-------------------------------------------------------------------------------
172+
Proving program with hash 79689b17ab6286cfde4651ef1f675cab19ad4efd9defd2c43001a06e7cbd8c40...
173+
Program proved in 85558 ms
174+
Output: [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
175+
```
176+
177+
To verify the proof generated in the previous step, run the following:
178+
```
179+
miden verify -p target/miden/release/is_prime.proof -i is_prime.inputs -x 79689b17ab6286cfde4651ef1f675cab19ad4efd9defd2c43001a06e7cbd8c40
180+
```
181+
182+
The output should look like this:
183+
```
184+
===============================================================================
185+
Verifying proof: target/miden/release/is_prime.proof
186+
-------------------------------------------------------------------------------
187+
Verification complete in 5 ms
188+
```
189+
190+
## Conclusion
191+
192+
This tutorial demonstrated how to write a basic program using the Miden compiler and how to prove and verify the execution of the program using the Miden CLI.

0 commit comments

Comments
 (0)