Skip to content

Commit

Permalink
zkm project template draft (#1)
Browse files Browse the repository at this point in the history
* draft template

---------

Co-authored-by: [gavin-ygy] <[[email protected]]>
  • Loading branch information
gavin-ygy and [gavin-ygy] authored Sep 1, 2024
1 parent 129fce1 commit 2a4bea0
Show file tree
Hide file tree
Showing 47 changed files with 4,184 additions and 0 deletions.
56 changes: 56 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Cargo Build & Test

on:
push:
branches:
- main
pull_request:
branches:
- main

env:
CARGO_TERM_COLOR: always

jobs:
build_and_test:
name: Rust project - latest
runs-on: ubuntu-latest
strategy:
matrix:
toolchain:
- nightly
steps:
- uses: actions/checkout@v2
- run: rustup update ${{ matrix.toolchain }} && rustup default ${{ matrix.toolchain }}
- name: Cargo build
run: cargo build --verbose --release
- name: Cargo test
run: cargo test --release --verbose
fmt:
name: Rustfmt
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- run: rustup component add rustfmt
- uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check
clippy:
name: Clippy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
override: true
- run: rustup component add clippy
- run: cargo clippy --all-targets -- -D warnings

4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/target
/verifier/data/test_circuit
/test-vectors/mips
push.sh
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "contracts/lib/forge-std"]
path = contracts/lib/forge-std
url = https://github.com/foundry-rs/forge-std.git
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[workspace]
members = [

"host-program"
]
resolver = "2"
24 changes: 24 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
CARGO = cargo

UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Darwin)
CARGO += --config 'build.rustdocflags = ["-C", "link-args=-framework CoreFoundation -framework Security"]'
endif

help: ## Display this help screen
@grep -h \
-E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \
awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

clippy: ## Run clippy checks over all workspace members and formatted correctly
@cargo check
@cargo fmt --all -- --check
@cargo clippy --all-targets -- -D warnings

fix: ## Automatically apply lint suggestions. This flag implies `--no-deps` and `--all-targets`
@cargo clippy --fix

test: ## Run tests for all the workspace members
@cargo test --release --all

.PHONY: clippy fmt test
192 changes: 192 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
# ZKM Project Template

This is a template for creating an end-to-end ZKM project which can generate the EVM-Compatible proof and the on chain verification contract.

There are two ways to prove the guest program:
* Use your local machine
* Use ZKM Proving network

## Local Proving

### Requirements
* Hardware: X86 CPU, 32 cores, 32G memory

* OS: Ubuntu22

* Rust: 1.81.0-nightly

* Go : 1.22.1

### Running the project

#### 0. Build guest program ELF

Please refer to : guest-program/README.md

#### 1. Generate plonky2 proof

> [!NOTE]
> If the program excutes succussfully, it will generate three files in the directory verifier/data/test_circuit/.(common_circuit_data.json proof_with_public_inputs.json verifier_only_circuit_data.json)
* Go program

```
mkdir -p /tmp/zkm
git clone https://github.com/zkMIPS/zkm-project-template.git
cd zkm-project-template
RUST_LOG=info SEG_OUTPUT=/tmp/zkm SEG_SIZE=262144 cargo run --release --bin add-go-prove
```

If the memory is insufficient, please reduce the SEG_SIZE to 131072 .

* Rust program

```
cd zkm-project-template
RUST_LOG=info SEG_OUTPUT=/tmp/zkm SEG_SIZE=262144 cargo run --release --bin revme-prove
```
If the memory is insufficient, please reduce the SEG_SIZE to 131072 .


#### 2. Convert plonky2 proof to groth16 proof

Copy the three files generated in the previous step to the testdata/mips directory.

```
cp verifier/data/test_circuit/* testdata/mips
./benchmark
```

If benchmark excutes succussfully, it will generate a groth16 proof and verifying.key in the directory testdata/mips/.
Then, copying the proof file and verifying.key to contracts/verifier

```
cp testdata/mips/snark_proof_with_public_inputs.json contracts/verifier/
cp testdata/mips/verifying.key contracts/verifier/
```

#### 3. Generate the on chain verification contract.

```
./gnark_sol_caller generate --outputDir contracts/src
```

#### 4. Deploy Verifier Contract.

If your system does not has Foundry,please install it.

```
curl -L https://foundry.paradigm.xyz | bash
```

Then, deploy the contract refering to "### Deploy" in the contracts/README.md .

## Network Proving

> [!NOTE]
> The proving network is demo at present. The production version is coming soon.
### Requirements
* CA certificate: ca.pem, ca.key
* Register in the https://www.zkm.io/apply (Let your key be in the whitelist)

### Running the project

#### 0. Build guest program ELF

Please refer to : guest-program/README.md

#### 1. Download the block

The block has your transaction.
We use the following tool to download the block.

* Compile the tool.

```
git clone https://github.com/zkMIPS/cannon-mips
cd cannon-mips && make
```

* Config the tool.

```
mkdir -p /tmp/cannon
export BASEDIR=/tmp/cannon;
export NODE=https://eth-sepolia.g.alchemy.com/v2/RH793ZL_pQkZb7KttcWcTlOjPrN0BjOW
```

* Download some block.

```
minigeth/go-ethereum 13284491
```
If it executes successfully, you will see the block information in the directory /tmp/cannon/0_13284491 .

#### 2. Config your CA certificate

Put the ca.key and ca.pem to some directory , such as , host-program/tool/ .

If you don't have a CA certificate, you can generate it using the certgen.sh in the director zkm-project-template/host-program/tool/.
```
cd zkm-project-template/host-program/tool/
chmod +x certgen.sh
./certgen.sh
```

#### 3. Generate the groth16 proof and verifier Contract

* Set the Environment parameters.

```
cd zkm-project-template
export CA_CERT_PATH=host-program/tool/ca.pem
export PRIVATE_KEY=df4bc5647fdb9600ceb4943d4adff3749956a8512e5707716357b13d5ee687d9 ##For testing, No changing the key!
export RUST_LOG=info
export ENDPOINT=https://152.32.186.45:20002 ##the test entry of zkm proving network
export SEG_SIZE=131072
export BLOCK_PATH=/tmp/cannon/0_13284491
export OUTPUT_DIR=/tmp/zkm
export ELF_PATH=guest-program/mips-elf/zkm-mips-elf-revme-rust
```

* Run the host program.

```
ARGS='12345678 654321' cargo run --release --bin revme-network-prove
```

If it executes successfully, it will output the similar message:
```
[2024-08-28T03:20:55Z INFO stage] request: "1509d5b6-a9e3-4b2f-85b8-5739c35a1310"
[2024-08-28T03:20:58Z INFO stage] generate_proof response: GenerateProofResponse { status: 2, error_message: "", proof_id: "1509d5b6-a9e3-4b2f-85b8-5739c35a1310", proof_url: "http://152.32.186.45:20001/1509d5b6-a9e3-4b2f-85b8-5739c35a1310/final/proof_with_public_inputs.json", stark_proof_url: "http://152.32.186.45:20001/1509d5b6-a9e3-4b2f-85b8-5739c35a1310/aggregate/proof_with_public_inputs.json", solidity_verifier_url: "http://152.32.186.45:20001/verifier.sol", output_stream: [] }
[2024-08-28T03:21:52Z INFO stage] generate_proof success public_inputs_size: 1546, output_size: 0
[2024-08-28T03:21:52Z INFO stage] Elapsed time: 56 secs
```

* Download the proof and contract

In the above output, we need the proof_url: "http://152.32.186.45:20001/1509d5b6-a9e3-4b2f-85b8-5739c35a1310/final/proof_with_public_inputs.json" and solidity_verifier_url: "http://152.32.186.45:20001/verifier.sol" .

```
wget http://152.32.186.45:20001/1509d5b6-a9e3-4b2f-85b8-5739c35a1310/final/proof_with_public_inputs.json
wget http://152.32.186.45:20001/verifier.sol
```
Then, move the proof and verifier.sol to contracts directory.
```
mv proof_with_public_inputs.json contracts/verifier/
mv verifier.sol contracts/src/
```

#### 4. Deploy Verifier Contract.

If your system does not has Foundry,please install it.

```
curl -L https://foundry.paradigm.xyz | bash
```

Then, deploy the contract refering to contracts/README.md


Binary file added benchmark
Binary file not shown.
1 change: 1 addition & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
large-error-threshold = 1000
15 changes: 15 additions & 0 deletions contracts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Compiler files
cache/
out/

# Ignores development broadcast logs
/broadcast
/broadcast/*/11155111/
/broadcast/*/31337/
/broadcast/**/dry-run/

# Docs
docs/

# Dotenv file
.env
66 changes: 66 additions & 0 deletions contracts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
## Foundry

**Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.**

Foundry consists of:

- **Forge**: Ethereum testing framework (like Truffle, Hardhat and DappTools).
- **Cast**: Swiss army knife for interacting with EVM smart contracts, sending transactions and getting chain data.
- **Anvil**: Local Ethereum node, akin to Ganache, Hardhat Network.
- **Chisel**: Fast, utilitarian, and verbose solidity REPL.

## Documentation

https://book.getfoundry.sh/

## Usage

### Build

```shell
$ forge build
```

### Test

```shell
$ forge test (TBD)
```

### Format

```shell
$ forge fmt
```

### Gas Snapshots

```shell
$ forge snapshot
```

### Anvil

```shell
$ anvil
```

### Deploy

```shell
$ forge script script/erc20.s.sol:ERC20TokenScript --rpc-url https://eth-sepolia.g.alchemy.com/v2/RH793ZL_pQkZb7KttcWcTlOjPrN0BjOW --private-key df4bc5647fdb9600ceb4943d4adff3749956a8512e5707716357b13d5ee687d9
```

### Cast

```shell
$ cast <subcommand>
```

### Help

```shell
$ forge --help
$ anvil --help
$ cast --help
```
7 changes: 7 additions & 0 deletions contracts/foundry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[profile.default]
src = "src"
out = "out"
libs = ["lib"]
fs_permissions = [{ access = "read-write", path = "./" }]

# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options
1 change: 1 addition & 0 deletions contracts/lib/forge-std
Submodule forge-std added at 4695fa
Empty file added contracts/remappings.txt
Empty file.
Loading

0 comments on commit 2a4bea0

Please sign in to comment.