Skip to content

Instantly share code, notes, and snippets.

@s1na
Created June 14, 2019 14:28
Show Gist options
  • Save s1na/34d9743318b1da440fae977e6c3f7fa5 to your computer and use it in GitHub Desktop.
Save s1na/34d9743318b1da440fae977e6c3f7fa5 to your computer and use it in GitHub Desktop.
ZoKrates -> scout stateless script

ZoKrates -> scout stateless script

Background info:

Setup

Clone a fork of zokrates with scout support:

git clone -b scout https://github.com/s1na/ZoKrates.git

Compile ZoKrates with the scout feature enabled (from zokrates_cli directory):

cd ZoKrates/zokrates_cli
cargo +nightly build --release --features scout
cd target/release

Clone scout:

git clone https://github.com/ewasm/scout.git

Add a directory under scout/scripts for the stateless contract we'll be creating.

Circuit & Verifier

You'll first need to implement a circuit of your choice, compile it, set it up and export witnesses for it. To do this, follow the ZoKrates documentation.

As an example let's imagine we have the following circuit (as mul.code) which verifies result of multiplication of a private number and a public one:

def main(private field a, field b) -> (field):
  return a * b

Now run the following commands:

# compile
./zokrates compile -i mul.code
# perform the setup phase
./zokrates setup
# execute the program (32 is private, 8 is public, result will be 256)
./zokrates compute-witness -a 32 8

Now to generate a verifier which runs as a stateless contract via scout:

# export a solidity verifier
./zokrates scout-verifier -o lib.rs

Note the result is a rust file which you can copy to scout/scripts/zkmul/src/lib.rs. This generated verifier assumes there are two public inputs, the first one should equal to pre_state_root and if the verification succeeds, it'll set post_state_root to the second public input. Feel free to change the rust file to implement your custom logic.

This gist contains also an example Cargo.toml file for the script.

Try it out

To test that your verifier works, you need to generate a proof:

./zokrates scout-proof -j proof

The proof file will contain a hex string which you can add to a new scout yaml test file, like the example provided.

Build scout and the script:

cargo build --release
cd scripts/zkmul && cargo build --release && chisel run --config chisel.toml

And test it via:

target/release/phase2-scout zkmul.yaml

There shouldn't be an error if the verification succeeds.

[package]
name = "zkmul"
version = "0.0.0"
license = "Apache-2.0"
repository = "https://github.com/ewasm/scout"
description = "zero knowledge multiplication"
publish = false
edition = "2018"
[lib]
crate-type = ["cdylib"]
[dependencies]
bellman_ce = "0.3"
pairing_ce = "0.17"
byteorder = "1"
[dependencies.ewasm_api]
git = "https://github.com/ewasm/ewasm-rust-api"
rev = "1c01982"
default-features = false
features = ["std", "eth2", "wee_alloc"]
[profile.release]
lto = true
debug = false
beacon_state:
execution_scripts:
- scripts/zkmul/target/wasm32-unknown-unknown/release/zkmul.wasm
shard_pre_state:
exec_env_states:
- "0000000000000000000000000000000000000000000000000000000000000008"
shard_blocks:
- env: 0
data: "<PROOF>"
shard_post_state:
exec_env_states:
- "0000000000000000000000000000000000000000000000000000000000000100"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment