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 sha1 precompile #33

Merged
merged 1 commit into from
Dec 21, 2018
Merged
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ members = [
"identity",
"keccak256",
"ripemd160",
"sha1",
"sha256",
]

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
15 changes: 15 additions & 0 deletions sha1/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "ewasm-precompile-sha1"
version = "0.1.0"
authors = ["Alex Beregszaszi <alex@rtfs.hu>"]
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"]
3 changes: 3 additions & 0 deletions sha1/README.md
Original file line number Diff line number Diff line change
@@ -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)
21 changes: 21 additions & 0 deletions sha1/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
extern crate ewasm_api;
extern crate sha1;

#[cfg(not(test))]
#[no_mangle]
pub extern "C" fn main() {
Copy link
Member

Choose a reason for hiding this comment

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

can you split the body of this function out into a helper, so that we could unit-test it without duplicating code?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'd rather not. Will unit test these when ewasm/ewasm-rust-api#5 is finished.

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)
}