Skip to content

Commit de79727

Browse files
feat: update compiler tutorial
1 parent 46950b5 commit de79727

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed
Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Writing a MidenVM Program In Rust
22

3-
*Using the Miden compiler to write programs in Rust and generate a proof of computation using the MidenVM CLI*
3+
_Using the Miden compiler to write programs in Rust and generate a proof of computation using the MidenVM CLI_
44

55
## Overview
66

@@ -16,33 +16,46 @@ In this guide, we will write a simple Rust program that checks whether an intege
1616
## Limitations and Important Considerations
1717

1818
Please note these current limitations of the Miden compiler:
19+
1920
- **No Floating Point Support:** Only integer arithmetic is supported (e.g., `u32`, `u64`, etc.).
2021
- **No Standard Library:** Programs must be written with `#![no_std]`, limiting you to core library functionality.
2122
- **Entrypoint Constraints:** The `entrypoint` function can accept at most **16 inputs** on the stack and produces a single `u32` output.
2223

2324
## Step 1: Installing the Miden Compiler
2425

25-
Clone the repository and install the compiler:
26+
Ensure you are using the nightly release of the rust toolchain:
27+
28+
```bash
29+
rustup update nightly
30+
rustup default nightly
31+
```
32+
33+
Clone the compiler repository:
34+
2635
```bash
2736
git clone https://github.com/0xpolygonmiden/compiler
2837
cd compiler
2938
git checkout next
3039
```
3140

32-
Then install the Miden compiler and cargo-miden extension:
41+
Then install the compiler and cargo-miden extension:
42+
3343
```bash
34-
cargo make build
44+
cargo install --path tools/cargo-miden
45+
cargo install --path midenc --locked
3546
```
3647

3748
## Step 2: Writing the Rust Program
3849

39-
Outside of the compiler repository, create a new Miden project:
50+
In a new terminal outside of the compiler repository, create a new Miden project:
51+
4052
```bash
4153
cargo miden new is_prime
4254
cd is_prime
4355
```
4456

4557
Add the following Rust code to `is_prime/src/lib.rs`. This code checks whether a number is prime:
58+
4659
```rust
4760
#![no_std]
4861

@@ -84,26 +97,30 @@ fn entrypoint(n: u32) -> bool {
8497
Add this code into your project's `src/lib.rs` file.
8598

8699
Next, create an `is_prime/inputs.toml` file:
100+
87101
```toml
88102
[inputs]
89-
stack = [2147482583]
103+
stack = [29]
90104
```
91105

92106
This file sets the value that will be passed into our `entrypoint` function when the program runs.
93107

94108
## Step 3: Running the Program in the Miden VM
95109

96110
Compile your program with:
111+
97112
```bash
98113
cargo miden build --release
99114
```
100115

101116
Run your compiled Miden assembly program using:
117+
102118
```bash
103119
midenc run target/miden/release/is_prime.masp --inputs inputs.toml
104120
```
105121

106122
The output will look like this:
123+
107124
```
108125
Run program: target/miden/release/is_prime.masp
109126
-------------------------------------------------------------------------------
@@ -123,30 +140,27 @@ The program returns `1` if the integer passed to the `is_prime` function is prim
123140

124141
## Step 4: Generating a zk proof of the `is_prime` program execution
125142

126-
First install the Miden CLI by cloning the Miden VM repository and checking out the `next` branch:
143+
First cloning the Miden VM repository and install the Miden VM CLI:
144+
127145
```bash
128146
git clone git@github.com:0xPolygonMiden/miden-vm.git
129-
cd miden-vm
130-
git checkout next
131-
```
132-
133-
Build and install the Miden VM CLI:
134-
```
135-
cd miden
147+
cd miden-vm/miden
136148
cargo install --path . --features concurrent,executable
137149
```
138150

139151
After installation is complete, return to the `is_prime` directory.
140152

141153
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:
154+
142155
```json
143156
{
144-
"operand_stack": ["2147482583"]
157+
"operand_stack": ["29"]
145158
}
146159
```
147160

148161
Now, using the Miden VM CLI tool, we can prove our program by running the following:
149-
```
162+
163+
```bash
150164
miden prove target/miden/release/is_prime.masp -i is_prime.inputs
151165
```
152166

@@ -162,11 +176,13 @@ Output: [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
162176
```
163177

164178
To verify the proof generated in the previous step, run the following:
165-
```
179+
180+
```bash
166181
miden verify -p target/miden/release/is_prime.proof -i is_prime.inputs -x 79689b17ab6286cfde4651ef1f675cab19ad4efd9defd2c43001a06e7cbd8c40
167182
```
168183

169184
The output should look like this:
185+
170186
```
171187
===============================================================================
172188
Verifying proof: target/miden/release/is_prime.proof

0 commit comments

Comments
 (0)