From ee8c903b0b02efec8ff5cc2abbf0fc8ad4c366a2 Mon Sep 17 00:00:00 2001 From: debris Date: Tue, 22 Dec 2015 20:05:14 +0100 Subject: [PATCH] sha3 && log --- rust-evmjit/Cargo.toml | 1 + rust-evmjit/src/lib.rs | 43 ++++++++++++++++++++++++++++++------------ 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/rust-evmjit/Cargo.toml b/rust-evmjit/Cargo.toml index 54c4437f4..4fabe4f97 100644 --- a/rust-evmjit/Cargo.toml +++ b/rust-evmjit/Cargo.toml @@ -5,3 +5,4 @@ authors = ["debris "] [dependencies] libc = "0.2.2" +tiny-keccak = "1.0" diff --git a/rust-evmjit/src/lib.rs b/rust-evmjit/src/lib.rs index 42d30ec3f..8958f2b2f 100644 --- a/rust-evmjit/src/lib.rs +++ b/rust-evmjit/src/lib.rs @@ -1,4 +1,4 @@ -//! Bare rust wrapper around evmjit +//! Bare rust wrapper around evmjit. //! //! Requires latest version of Ethereum EVM JIT. https://github.com/debris/evmjit //! @@ -14,6 +14,8 @@ //! ``` extern crate libc; +extern crate tiny_keccak; + use std::ops::{Deref, DerefMut}; use self::ffi::*; @@ -102,6 +104,14 @@ pub trait Env { out_size: *mut u64, code_address: JitI256) -> bool; + fn log(&mut self, + beg: *const u8, + size: *const u64, + topic1: *const JitI256, + topic2: *const JitI256, + topic3: *const JitI256, + topic4: *const JitI256); + fn extcode(&self, address: *const JitI256, size: *mut u64) -> *const u8; } @@ -145,8 +155,10 @@ impl DerefMut for EnvHandle { /// ffi functions pub mod ffi { - use super::*; + use std::slice; use libc; + use tiny_keccak::Keccak; + use super::*; /// Jit context struct declaration. pub enum JitContext {} @@ -243,8 +255,14 @@ pub mod ffi { } #[no_mangle] - pub unsafe extern fn env_sha3(_begin: *const u8, _size: *const u64, _out_hash: *const JitI256) { - unimplemented!() + pub unsafe extern fn env_sha3(begin: *const u8, size: *const u64, out_hash: *mut JitI256) { + let out_hash = &mut *out_hash; + let input = slice::from_raw_parts(begin, *size as usize); + let outlen = out_hash.words.len() * 8; + let output = slice::from_raw_parts_mut(out_hash.words.as_mut_ptr() as *mut u8, outlen); + let mut sha3 = Keccak::new_sha3_256(); + sha3.update(input); + sha3.finalize(output); } #[no_mangle] @@ -254,14 +272,15 @@ pub mod ffi { } #[no_mangle] - pub unsafe extern fn env_log(_env: *mut EnvHandle, - _beg: *const u8, - _size: *const u64, - _topic1: *const JitI256, - _topic2: *const JitI256, - _topic3: *const JitI256, - _topic4: *const JitI256) { - unimplemented!() + pub unsafe extern fn env_log(env: *mut EnvHandle, + beg: *const u8, + size: *const u64, + topic1: *const JitI256, + topic2: *const JitI256, + topic3: *const JitI256, + topic4: *const JitI256) { + let env = &mut *env; + env.log(beg, size, topic1, topic2, topic3, topic4); }