Skip to content

Commit

Permalink
add snark builtin
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Gancher committed Jul 28, 2016
1 parent ce00c13 commit b820abf
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 0 deletions.
64 changes: 64 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ ethcore-logger = { path = "logger" }
json-ipc-server = { git = "https://github.com/ethcore/json-ipc-server.git" }
ethcore-dapps = { path = "dapps", optional = true }
clippy = { version = "0.0.79", optional = true}
ethabi = { git = "https://github.com/gancherj/ethabi.git" }

[target.'cfg(windows)'.dependencies]
winapi = "0.2"
Expand Down
2 changes: 2 additions & 0 deletions ethcore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ rayon = "0.3.1"
ethstore = { path = "../ethstore" }
semver = "0.2"
ethcore-ipc-nano = { path = "../ipc/nano" }
ethabi = { git = "https://github.com/gancherj/ethabi.git" }
tinysnark = { git = "https://github.com/ebfull/tinysnark.git" }

[dependencies.hyper]
git = "https://github.com/ethcore/hyper"
Expand Down
27 changes: 27 additions & 0 deletions ethcore/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ use crypto::sha2::Sha256;
use crypto::ripemd160::Ripemd160;
use crypto::digest::Digest;
use ethjson;
use ethabi;
use tinysnark;
use ethabi::spec::ParamType;
use ethabi::Token;

/// Definition of a contract whose implementation is built-in.
pub struct Builtin {
Expand Down Expand Up @@ -85,6 +89,29 @@ pub fn copy_to(src: &[u8], dest: &mut[u8]) {
/// TODO: turn in to a factory with dynamic registration.
pub fn new_builtin_exec(name: &str) -> Box<Fn(&[u8], &mut [u8])> {
match name {
"zkSNARK" => Box::new(move|input: &[u8], output: &mut[u8]| {
let outlen = output.len();
for i in 0..output.len() {
output[i] = 0;
}
let abitype = [ParamType::Bytes, ParamType::Bytes, ParamType::Bytes];
let v = input[4..].to_vec();
let decode = ethabi::Decoder::decode(&abitype, v);
if let Ok(tokens) = decode {
if tokens.len() == 3 {
if let Token::Bytes(ref v1) = tokens[0] {
if let Token::Bytes(ref v2) = tokens[1] {
if let Token::Bytes(ref v3) = tokens[2] {
let res = tinysnark::snark_verify(v1, v2, v3);
if res {
output[outlen - 1] = 1;
}
}
}
}
}
}
}),
"identity" => Box::new(move|input: &[u8], output: &mut[u8]| {
for i in 0..min(input.len(), output.len()) {
output[i] = input[i];
Expand Down
2 changes: 2 additions & 0 deletions ethcore/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,12 @@ extern crate num_cpus;
extern crate crossbeam;
extern crate ethjson;
extern crate bloomchain;
extern crate ethabi;
#[macro_use] extern crate ethcore_ipc as ipc;
extern crate rayon;
extern crate hyper;
extern crate ethash;
extern crate tinysnark;
pub extern crate ethstore;
extern crate semver;
extern crate ethcore_ipc_nano as nanoipc;
Expand Down

0 comments on commit b820abf

Please sign in to comment.