Skip to content

Commit

Permalink
Add stark verification example scout script
Browse files Browse the repository at this point in the history
  • Loading branch information
jwasinger authored and axic committed Jan 23, 2020
1 parent 287d37d commit 0f43f99
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
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);
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)
}

0 comments on commit 0f43f99

Please sign in to comment.