Skip to content

Commit

Permalink
feat(levm): implement precompile ripemd160 (#1521)
Browse files Browse the repository at this point in the history
**Motivation**

The goal is to implement ripemd160 precompile.

**Description**

The implementation is quite similar to the sha2-256. There is one test
named `static_Call50000_rip160` that fails, but I think that fails due
to other reasons.

---------

Co-authored-by: ilitteri <[email protected]>
Co-authored-by: Jeremías Salomón <[email protected]>
  • Loading branch information
3 people authored Dec 18, 2024
1 parent 30976d3 commit 4b7c965
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 10 deletions.
1 change: 1 addition & 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 crates/vm/levm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ keccak-hash = "0.11.0"
thiserror = "2.0.3"
libsecp256k1 = "0.7.1"
sha2 = "0.10.8"
ripemd = "0.1.3"

[dev-dependencies]
hex = "0.4.3"
Expand Down
25 changes: 18 additions & 7 deletions crates/vm/levm/src/precompiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use sha3::Digest;
use crate::{
call_frame::CallFrame,
errors::{InternalError, PrecompileError, VMError},
gas_cost::{sha2_256 as sha2_256_cost, ECRECOVER_COST},
gas_cost::{ripemd_160 as ripemd_160_cost, sha2_256 as sha2_256_cost, ECRECOVER_COST},
};

pub const ECRECOVER_ADDRESS: H160 = H160([
Expand Down Expand Up @@ -199,7 +199,7 @@ fn identity(
Ok(Bytes::new())
}

fn sha2_256(
pub fn sha2_256(
calldata: &Bytes,
gas_for_call: U256,
consumed_gas: &mut U256,
Expand All @@ -213,12 +213,23 @@ fn sha2_256(
Ok(Bytes::from(result))
}

fn ripemd_160(
_calldata: &Bytes,
_gas_for_call: U256,
_consumed_gas: &mut U256,
pub fn ripemd_160(
calldata: &Bytes,
gas_for_call: U256,
consumed_gas: &mut U256,
) -> Result<Bytes, VMError> {
Ok(Bytes::new())
let gas_cost = ripemd_160_cost(calldata.len())?;

increase_precompile_consumed_gas(gas_for_call, gas_cost, consumed_gas)?;

let mut hasher = ripemd::Ripemd160::new();
hasher.update(calldata);
let result = hasher.finalize();

let mut output = vec![0; 12];
output.extend_from_slice(&result);

Ok(Bytes::from(output))
}

fn modexp(
Expand Down
49 changes: 46 additions & 3 deletions crates/vm/levm/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ use ethrex_levm::{
constants::*,
db::{cache, CacheDB, Db},
errors::{OutOfGasError, TxResult, VMError},
gas_cost, memory,
gas_cost::{
self, ECRECOVER_COST, RIPEMD_160_DYNAMIC_BASE, RIPEMD_160_STATIC_COST,
SHA2_256_DYNAMIC_BASE, SHA2_256_STATIC_COST,
},
memory,
operations::Operation,
precompiles::ecrecover,
precompiles::{ecrecover, ripemd_160, sha2_256},
utils::{new_vm_with_ops, new_vm_with_ops_addr_bal_db, new_vm_with_ops_db, ops_to_bytecode},
vm::{word_to_address, Storage, VM},
Environment,
Expand Down Expand Up @@ -4504,5 +4508,44 @@ fn recover_test() {
hex::decode("0000000000000000000000007156526fbd7a3c72969b54f64e42c10fbb768c8a").unwrap(),
);

assert_eq!(result, expected_result)
assert_eq!(result, expected_result);
assert_eq!(consumed_gas, ECRECOVER_COST.into());
}

#[test]
fn sha2_256_test() {
let calldata = hex::decode("ff").unwrap();
let calldata = Bytes::from(calldata);

let mut consumed_gas = U256::zero();
let result = sha2_256(&calldata, 10000.into(), &mut consumed_gas).unwrap();

let expected_result = Bytes::from(
hex::decode("a8100ae6aa1940d0b663bb31cd466142ebbdbd5187131b92d93818987832eb89").unwrap(),
);

assert_eq!(result, expected_result);
assert_eq!(
consumed_gas,
(SHA2_256_STATIC_COST + SHA2_256_DYNAMIC_BASE).into()
);
}

#[test]
fn ripemd_160_test() {
let calldata = hex::decode("ff").unwrap();
let calldata = Bytes::from(calldata);

let mut consumed_gas = U256::zero();
let result = ripemd_160(&calldata, 10000.into(), &mut consumed_gas).unwrap();

let expected_result = Bytes::from(
hex::decode("0000000000000000000000002c0c45d3ecab80fe060e5f1d7057cd2f8de5e557").unwrap(),
);

assert_eq!(result, expected_result);
assert_eq!(
consumed_gas,
(RIPEMD_160_STATIC_COST + RIPEMD_160_DYNAMIC_BASE).into()
);
}

0 comments on commit 4b7c965

Please sign in to comment.