Skip to content

Commit d128869

Browse files
PatStilesuri-99
andauthored
feat(): Add docs to generating proofs for zkrust (#1063)
Co-authored-by: Urix <[email protected]>
1 parent 014bd4d commit d128869

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed

docs/3_guides/4_generating_proofs.md

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,136 @@ aligned submit \
284284
```
285285

286286
For more instructions on how to submit proofs, check the [Submitting proofs guide](../3_guides/0_submitting_proofs.md).
287+
288+
## ZkRust
289+
290+
`zkRust` is a CLI tool maintained by Aligned that aims to simplify the developing applications in Rust using zkVM's such as SP1 or Risc0.
291+
292+
zkRust can be installed directly by downloading the latest release binaries:
293+
294+
```sh
295+
curl -L https://raw.githubusercontent.com/yetanotherco/zkRust/main/install_zkrust.sh | bash
296+
```
297+
298+
Then, to get started you can create a workspace for your project in zkRust by running:
299+
300+
```sh
301+
cargo new <PROGRAM_DIRECTORY>
302+
```
303+
304+
It is that simple.
305+
306+
## Usage
307+
308+
To use zkRust, users specify a `fn main()` whose execution is proven within the zkVM. This function must be defined in a `main.rs` file in a directory with the following structure:
309+
310+
```
311+
.
312+
└── <PROGRAM_DIRECTORY>
313+
├── Cargo.toml
314+
└── src
315+
└── main.rs
316+
```
317+
318+
For using more complex programs you can import a separate lib/ crate into the `PROGRAM_DIRECTORY`
319+
320+
```
321+
.
322+
└── <PROGRAM_DIRECTORY>
323+
├── Cargo.toml
324+
├── lib/
325+
└── src
326+
└── lib
327+
```
328+
329+
### Inputs and Outputs
330+
331+
The user may also define a `input()` and `output()` functions in addition to `main()`, that define code that runs outside of the zkVM, before and after the VM executes
332+
333+
- The `input()` function executes before the zkVM code is executed and allows the user to define inputs passed to the vm such as a deserialized Tx or data fetched from an external source at runtime.
334+
- Within the `main()` (guest) function the user may write information from the computation performed in the zkVM to an output buffer to be used after proof generation.
335+
- The `output()` defines code that allows the user to read the information written to that buffer of the and perform post-processing of that data.
336+
337+
The user may specify inputs into the VM (guest) code using `zk_rust_io::write()` as long on the type of rust object they are writing implements `Serializable`.
338+
339+
Within the `main()` function (guest) the user may read in the inputs by specifying `zk_rust_io::read()` and output data computed during the execution phase of the code within the VM (guest) program by specifying `zk_rust_io::commit()`.
340+
341+
To read the output of the output of the VM (guest) program you declare `zk_rust_io::out()`. The `zk_rust_io` crate defines function headers that are not inlined and are purely used as compile time symbols to ensure a user can compile their rust code before running it within one of the zkVMs available in zkRust.
342+
343+
To use the I/O imports import the `zk_rust_io` crate by adding the following to the `Cargo.toml` in your project directory.
344+
345+
```sh
346+
zk_rust_io = { git = "https://github.com/yetanotherco/zkRust.git", version = "v0.1.0" }
347+
```
348+
349+
## Example
350+
351+
### input.rs
352+
353+
```rust
354+
use zk_rust_io;
355+
356+
pub fn input() {
357+
let pattern = "a+".to_string();
358+
let target_string = "an era of truth, not trust".to_string();
359+
360+
// Write in a simple regex pattern.
361+
zk_rust_io::write(&pattern);
362+
zk_rust_io::write(&target_string);
363+
}
364+
````
365+
366+
### main.rs
367+
368+
```rust
369+
use regex::Regex;
370+
use zk_rust_io;
371+
372+
pub fn main() {
373+
// Read two inputs from the prover: a regex pattern and a target string.
374+
let pattern: String = zk_rust_io::read();
375+
let target_string: String = zk_rust_io::read();
376+
377+
// Try to compile the regex pattern. If it fails, write `false` as output and return.
378+
let regex = match Regex::new(&pattern) {
379+
Ok(regex) => regex,
380+
Err(_) => {
381+
panic!("Invalid regex pattern");
382+
}
383+
};
384+
385+
// Perform the regex search on the target string.
386+
let result = regex.is_match(&target_string);
387+
388+
// Write the result (true or false) to the output.
389+
zk_rust_io::commit(&result);
390+
}
391+
```
392+
393+
### output.rs
394+
395+
```rust
396+
use zk_rust_io;
397+
398+
pub fn output() {
399+
// Read the output.
400+
let res: bool = zk_rust_io::out();
401+
println!("res: {}", res);
402+
}
403+
```
404+
405+
To generate a proof of the execution of your code run the following:
406+
407+
- **Sp1**:
408+
409+
```sh
410+
cargo run --release -- prove-sp1 <PROGRAM_DIRECTORY_PATH> .
411+
```
412+
413+
- **Risc0**:
414+
```sh
415+
cargo run --release -- prove-risc0 <PROGRAM_DIRECTORY_PATH> .
416+
```
417+
Make sure to have [Risc0](https://dev.risczero.com/api/zkvm/quickstart#1-install-the-risc-zero-toolchain) installed with version `v1.0.1`
418+
419+
For additional information on using zkRust and using it to submit proofs to Aligned see the [zkRust](https://github.com/yetanotherco/zkRust) Github Repository.

0 commit comments

Comments
 (0)