From df22fef24fa8aea64a43e6342b6c0999bf923153 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Fri, 21 Dec 2018 21:49:08 +0000 Subject: [PATCH] Add sha1 precompile --- Cargo.toml | 1 + Makefile | 2 +- sha1/Cargo.toml | 15 +++++++++++++++ sha1/README.md | 3 +++ sha1/src/lib.rs | 21 +++++++++++++++++++++ 5 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 sha1/Cargo.toml create mode 100644 sha1/README.md create mode 100644 sha1/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index c733c31..9a50699 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ members = [ "identity", "keccak256", "ripemd160", + "sha1", "sha256", ] diff --git a/Makefile b/Makefile index a501986..26075c1 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -TARGETS=bls12pairing ecadd ecmul ecpairing ed25519 identity keccak256 ripemd160 sha256 +TARGETS=bls12pairing ecadd ecmul ecpairing ed25519 identity keccak256 ripemd160 sha1 sha256 all: @cargo build --release --target wasm32-unknown-unknown diff --git a/sha1/Cargo.toml b/sha1/Cargo.toml new file mode 100644 index 0000000..40e3839 --- /dev/null +++ b/sha1/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "ewasm-precompile-sha1" +version = "0.1.0" +authors = ["Alex Beregszaszi "] +license = "Apache-2.0" +repository = "https://github.com/ewasm/ewasm-precompiles" +description = "Ethereum SHA1 precompile in Rust" +publish = false + +[dependencies] +sha1 = "0.6" +ewasm_api = "0.5" + +[lib] +crate-type = ["cdylib"] diff --git a/sha1/README.md b/sha1/README.md new file mode 100644 index 0000000..2f1a9cc --- /dev/null +++ b/sha1/README.md @@ -0,0 +1,3 @@ +# sha1 + +This is the precompile for enabling SHA-1 hashing according to [EIP-180](https://github.com/ethereum/EIPs/issues/180) diff --git a/sha1/src/lib.rs b/sha1/src/lib.rs new file mode 100644 index 0000000..55399f0 --- /dev/null +++ b/sha1/src/lib.rs @@ -0,0 +1,21 @@ +extern crate ewasm_api; +extern crate sha1; + +#[cfg(not(test))] +#[no_mangle] +pub extern "C" fn main() { + let length = ewasm_api::calldata_size(); + + // charge a base fee plus a word fee for every 256-bit word + let base_fee = 62; + let word_fee = 8; + let total_cost = base_fee + ((length + 31) / 32) * word_fee; + + ewasm_api::consume_gas(total_cost as u64); + + let data = ewasm_api::unsafe_calldata_copy(0, length); + + let hash = sha1::Sha1::from(data).digest().bytes(); + + ewasm_api::finish_data(&hash) +}