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

Add STARK Verification Example #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions mimc-stark-verifier.yml

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions scripts/mimc-stark-verifier/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[package]
name = "phase2-stark-verifier"
version = "0.0.0"
authors = ["Jared Wasinger"]
license = "Apache-2.0"
repository = "https://github.com/ewasm/scout"
description = "Eth 2.0 Phase 2 execution script: MIMC STARK verifier"
publish = false
edition = "2018"

[dependencies]
ssz = "0.1.2"
ssz-derive = "0.1.2"
sha3 = "^0.6"
stark_verifier = { git = "https://github.com/jwasinger/stark-verifier", branch = "feat/lib" }
num-bigint = { git = "https://github.com/jwasinger/num-bigint", branch = "feature/pow"}
num-traits = "0.2.8"

[dependencies.ewasm_api]
git = "https://github.com/ewasm/ewasm-rust-api"
rev = "1c01982"
default-features = false
features = ["std", "eth2", "qimalloc"]

# [profile.release]
# lto = true
# debug = false

[lib]
crate-type = ["cdylib"]
8 changes: 8 additions & 0 deletions scripts/mimc-stark-verifier/chisel.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
helloworld:
file: "target/wasm32-unknown-unknown/release/phase2_stark_verifier.wasm"
trimexports:
preset: "ewasm"
verifyexports:
preset: "ewasm"
snip:
preset: "ewasm"
40 changes: 40 additions & 0 deletions scripts/mimc-stark-verifier/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
extern crate stark_verifier;
extern crate ewasm_api;
extern crate num_bigint;

use std::str::FromStr;
use num_traits::pow::Pow;

use ewasm_api::*;
use stark_verifier::deserializer;
use stark_verifier::{verify_mimc_proof, MODULUS};
use num_bigint::{BigInt, BigUint};

fn process_block(pre_state_root: types::Bytes32, block_data: &[u8]) -> types::Bytes32 {
let (stark_proof, _) = deserializer::from_bytes(&block_data).expect("couldn't deserialize");

// TODO: package subsequent parameters in the proof itself
const LOG_STEPS: usize = 13;
let mut constants: Vec<BigInt> = Vec::new();
let modulus: BigInt = num_bigint::BigInt::from_str(MODULUS).expect("modulus couldn't be deserialized into bigint");

for i in 0..64 {
let constant = BigInt::from(i as u8).pow(BigUint::from(7u8)) ^ BigInt::from(42u8);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These look like constants. You should precompute these and place them as data.

constants.push(constant);
}

let output = BigInt::from_str("95224774355499767951968048714566316597785297695903697235130434363122555476056").unwrap();

match verify_mimc_proof(BigInt::from(3u8), 2usize.pow(LOG_STEPS as u32), &constants, output, stark_proof, &modulus) {
true => types::Bytes32 { bytes: [0u8; 32] },
false => types::Bytes32 { bytes: [1u8; 32] }
}
}

#[no_mangle]
pub extern "C" fn main() {
let pre_state_root = eth2::load_pre_state_root();
let block_data = eth2::acquire_block_data();
let post_state_root = process_block(pre_state_root, &block_data);
eth2::save_post_state_root(post_state_root)
}