Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestions for the new no-relay template #76

Merged
merged 11 commits into from
Feb 5, 2024
26 changes: 15 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 3 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,11 @@ bincode = { version = "1.3" }
bonsai-sdk = { version = "0.6.0" }
bytemuck = { version = "1.14" }
methods = { path = "./methods", package = "bonsai-starter-methods" }
risc0-build = { version = "0.20.0", features = ["guest-list"] }
risc0-build = { version = "0.20", features = ["guest-list"] }
risc0-ethereum-sdk = { version = "0.1.0", path = "sdk" }
risc0-zkvm = { version = "0.20.0", default-features = false }
serde = { version = "1.0", features = ["derive"] }

# Always optimize; building and running the guest takes much longer without optimization.
[profile.dev]
opt-level = 3

[profile.dev.build-override]
opt-level = 3
risc0-zkvm = { version = "0.20", default-features = false }
serde = { version = "1.0", features = ["derive", "std"] }

[profile.release]
debug = 1
lto = true

[profile.release.build-override]
opt-level = 3
149 changes: 85 additions & 64 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,103 @@

> **Note: This software is not production ready. Do not use in production.**

Starter template for writing an application using [Bonsai].
Starter template for writing an application using [RISC Zero] and Ethereum.

This repository implements an application on Ethereum utilizing Bonsai as a [coprocessor] to the smart contract application.
It provides a starting point for building powerful new applications on Ethereum that offload computationally intensive, or difficult to implement, tasks to be proven by the [RISC Zero zkVM], with verifiable results sent to your Ethereum contract.
This repository implements an application on Ethereum utilizing [Bonsai] as a [coprocessor] to the smart contract application.
It provides a starting point for building powerful new applications on Ethereum that offload computationally intensive (i.e. gas expensive), or difficult to implement, tasks to be proven by the [RISC Zero zkVM].
Verifiable results are sent to your Ethereum contract.

[RISC Zero]: https://www.risczero.com/
[Bonsai]: https://dev.bonsai.xyz/
[coprocessor]: https://twitter.com/RiscZero/status/1677316664772132864
[RISC Zero zkVM]: https://dev.risczero.com/zkvm

## Overview

The picture below shows a simplified overview of how users can integrate Bonsai into their Ethereum smart contracts:
The picture below shows a simplified overview of how users can integrate [Bonsai] into their Ethereum smart contracts:

![Bonsai Foundry Template Diagram](images/bonsai-foundry-template.png)

1. Delegate your smart contract's logic to the [RISC Zero zkVM] simply using a Command Line Interface (CLI) to initiate an off-chain proof request, which is sent to the Bonsai proving service.
2. Bonsai generates a SNARK proof and its result, encapsulated in a journal.
1. Run your application logic in the [RISC Zero zkVM]. The provided Command Line Interface (CLI) sends an off-chain proof request to the Bonsai proving service.
2. Bonsai generates the computation result, written to the [journal], and a SNARK proof of its correctness.
3. The CLI submits this proof and journal on-chain to your app contract for validation.
4. Your app contract calls the RISC Zero Verifier to validate the proof. If the verification is successful, the journal is deemed trustworthy and can be safely used.
4. Your app contract calls the [RISC Zero Verifier] to validate the proof. If the verification is successful, the journal is deemed trustworthy and can be safely used.

[journal]: https://dev.risczero.com/terminology#journal
[RISC Zero Verifier]: https://github.com/risc0/risc0/blob/release-0.20/bonsai/ethereum/contracts/IRiscZeroVerifier.sol

## Dependencies

First, [install Rust] and [Foundry], and then restart your terminal. Next, you will need to install the `cargo risczero` tool.

```sh
# Install Rust
curl https://sh.rustup.rs -sSf | sh
# Install Foundry
curl -L https://foundry.paradigm.xyz | bash
```

We'll use `cargo binstall` to get `cargo-risczero` installed. See [cargo-binstall] for more details.

```bash
```sh
cargo install cargo-binstall
cargo binstall cargo-risczero
```

Next we'll need to install the `risc0` toolchain with:

```
```sh
cargo risczero install
```

Now you have all the tools you need to develop and deploy an application with RISC Zero.

[install Rust]: https://doc.rust-lang.org/cargo/getting-started/installation.html
[Foundry]: https://getfoundry.sh/
[cargo-binstall]: https://github.com/cargo-bins/cargo-binstall#cargo-binaryinstall

## Quick Start
First, install the RISC Zero toolchain using the instructions above.

First, install the RISC Zero toolchain using the [instructions above].

Now, you can initialize a new Bonsai project at a location of your choosing:

```bash
```sh
forge init -t risc0/bonsai-foundry-template ./my-project
```
Congratulations! You've just built your first Bonsai project.

Congratulations! You've just started your first RISC Zero project.

Your new project consists of:

- a [zkVM program] (written in Rust), which specifies a computation that will be proven;
- a [app contract] (written in Solidity), which receives the response;
- a [guest interface] (written in Rust), which lets you define how to parse and serialize the guest input and calldata so that the [RISC Zero zkVM] and Bonsai can interact with your contract.
- a [guest interface] (written in Rust), which lets you define how to parse and serialize the guest input and calldata so that the [RISC Zero zkVM] and Bonsai can interact with your contract.

[instructions above]: #dependencies
[zkVM program]: ./methods/guest/src/bin
[app contract]: ./contracts
[guest interface]: ./cli

[install Rust]: https://doc.rust-lang.org/cargo/getting-started/installation.html
[Foundry]: https://getfoundry.sh/
[cargo-binstall]: https://github.com/cargo-bins/cargo-binstall#cargo-binaryinstall
[zkVM program]: https://github.com/risc0/bonsai-foundry-template/tree/main/methods/guest/src/bin
[app contract]: https://github.com/risc0/bonsai-foundry-template/tree/main/contracts
[guest interface]: https://github.com/risc0/bonsai-foundry-template/tree/main/cli
### Run the Tests

### Test Your Project
- Use `cargo build` to test compilation of your zkVM program.
- Use `cargo test` to run the tests in your zkVM program.
- Use `RISC0_DEV_MODE=false forge test -vvv` to test your Solidity contracts and their interaction with your zkVM program.
- Use `RISC0_DEV_MODE=true forge test -vvv` to test your Solidity contracts and their interaction with your zkVM program.

## Develop Your Application

To build your application, you'll need to make changes in three folders:

- write the code you want proven in the [methods] folder
- write the on-chain part of your project in the [contracts] folder
- write the guest interface in the [cli] folder

[methods]: ./methods
[cli]: ./cli
[contracts]: ./contracts

### Configuring Bonsai

***Note:*** *The Bonsai proving service is still in early Alpha. To request an API key [complete the form here](https://bonsai.xyz/apply).*

With the Bonsai proving service, you can produce a [Groth16 SNARK proof] that is verifiable on-chain.
Expand All @@ -71,63 +109,46 @@ export BONSAI_API_KEY="YOUR_API_KEY" # see form linked above
export BONSAI_API_URL="BONSAI_URL" # provided with your api key
```

<!-- TODO(victor): Rename the RiscZeroGroth16VerifierTest -->
Now if you run `forge test` with `RISC0_DEV_MODE=false`, the test will run as before, but will additionally use the fully verifying `RiscZeroGroth16Verifier` contract instead of `RiscZeroGroth16VerifierTest` and will request a SNARK receipt from Bonsai.

```bash
RISC0_DEV_MODE=false forge test -vvv
```

## Next Steps
To build your application, you'll need to make changes in three folders:
- write the code you want proven in the [methods] folder
- write the on-chain part of your project in the [contracts] folder
- write the guest interface in the [cli] folder
[Groth16 SNARK proof]: https://www.risczero.com/news/on-chain-verification

Then, you're ready to [deploy your project]. <br/>
## Deploy Your Application

When you're ready, follow the [deployment guide] to get your application running on [Sepolia].

[deployment guide]: /deployment-guide.md
[Sepolia]: https://www.alchemy.com/overviews/sepolia-testnet

## Project Structure

Below are the primary files in the project directory

```text
.
├── Cargo.toml // Definitions for cargo and rust
├── foundry.toml // Definitions for foundry
├── cli // CLI for interacting with your application
│ ├── Cargo.toml
│ └── src
│ ├── interface.rs // Interface for interacting with your contract
│ └── main.rs // CLI for interacting with your application
├── contracts // Your Ethereum contracts live here
│ └── BonsaiStarter.sol // Starter template for basic callback contract
├── tests // Your Ethereum contract tests live here
│ └── BonsaiStarter.t.sol // Tests for basic callback contract
└── methods // [zkVM guest programs] are built here
├── Cargo.toml // Configuration for Cargo and Rust
├── foundry.toml // Configuration for Foundry
├── contracts
│ └── EvenNumber.sol // Basic example contract for you to modify
├── tests
│ └── EvenNumber.t.sol // Tests for the basic example contract
├── methods
│ ├── Cargo.toml
│ ├── guest
│ │ ├── Cargo.toml
│ │ └── src
│ │ └── bin // You can add additionally guest prgrams to this folder
│ │ └── is_even.rs // Example guest program for cheking if a number is even
│ └── src
│ └── lib.rs // Compiled image IDs and tests for your guest programs
└── cli
├── Cargo.toml
├── build.rs // Instructions for the risc0-build rust crate
├── guest // A rust crate containing your [zkVM guest programs]
│ ├── Cargo.toml
│ └── src
│ └── bin // Your [zkVM guest programs] live here
│ └── is_even.rs // Example [guest program] for cheking if a number is even
└── src
└── lib.rs // Built RISC Zero guest programs are compiled into here
├── interface.rs // Interface for interacting with your contract
└── main.rs // CLI for interacting with your application
```


[methods]: /methods
[cli]: /cli
[contracts]: /contracts
[deploy your project]: /deployment-guide.md
[coprocessor]: https://twitter.com/RiscZero/status/1677316664772132864
[Bonsai]: https://dev.bonsai.xyz/
[Foundry]: https://getfoundry.sh/
[Groth16 SNARK proof]: https://www.risczero.com/news/on-chain-verification
[RISC Zero examples]: https://github.com/risc0/risc0/tree/main/examples
[RISC Zero]: https://www.risczero.com/
[RISC-V]: https://www.risczero.com/docs/reference-docs/about-risc-v
[https://book.getfoundry.sh/forge/tests]: https://book.getfoundry.sh/forge/tests
[receipt]: https://dev.risczero.com/zkvm/developer-guide/receipts
[zkVM guest program]: https://dev.risczero.com/zkvm/developer-guide/guest-code-101
[RISC Zero zkVM]: https://dev.risczero.com/zkvm
3 changes: 2 additions & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "bonsai-starter"
name = "bonsai-starter-cli"
version = { workspace = true }
edition = { workspace = true }

Expand All @@ -8,5 +8,6 @@ alloy-primitives = { workspace = true }
alloy-sol-types = { workspace = true }
anyhow = { workspace = true }
env_logger = { version = "0.10" }
hex = "0.4.3"
methods = { workspace = true }
risc0-ethereum-sdk = { workspace = true }
Loading
Loading