From 36de35b25d5941ac257e1877696e894223445c30 Mon Sep 17 00:00:00 2001 From: "Tung Bui (Leo)" <85242618+tungbq@users.noreply.github.com> Date: Mon, 10 Jul 2023 15:12:53 +0700 Subject: [PATCH] feat: Rename all SHA3 opcodes to KECCAK256 (#514) * feat: Rename all SHA3 opcodes to KECCAK256 * feat: renaming sha3 function * feat: renaming sha3 params * revert: library * revert: library.1 * revert: library.2 * revert: library.3 * revert: library.4 * fix: failing test cases --- crates/interpreter/src/gas/calc.rs | 6 +++--- crates/interpreter/src/gas/constants.rs | 4 ++-- crates/interpreter/src/instructions.rs | 2 +- crates/interpreter/src/instructions/opcode.rs | 4 ++-- crates/interpreter/src/instructions/system.rs | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/crates/interpreter/src/gas/calc.rs b/crates/interpreter/src/gas/calc.rs index d255cee967..ae7d7c4b5f 100644 --- a/crates/interpreter/src/gas/calc.rs +++ b/crates/interpreter/src/gas/calc.rs @@ -62,7 +62,7 @@ pub fn create2_cost(len: usize) -> Option { // ceil(len / 32.0) let len = len as u64; let sha_addup_base = (len / 32) + u64::from((len % 32) != 0); - let sha_addup = SHA3WORD.checked_mul(sha_addup_base)?; + let sha_addup = KECCAK256WORD.checked_mul(sha_addup_base)?; let gas = base.checked_add(sha_addup)?; Some(gas) @@ -146,10 +146,10 @@ pub fn log_cost(n: u8, len: u64) -> Option { .checked_add(LOGTOPIC * n as u64) } -pub fn sha3_cost(len: u64) -> Option { +pub fn keccak256_cost(len: u64) -> Option { let wordd = len / 32; let wordr = len % 32; - SHA3.checked_add(SHA3WORD.checked_mul(if wordr == 0 { wordd } else { wordd + 1 })?) + KECCAK256.checked_add(KECCAK256WORD.checked_mul(if wordr == 0 { wordd } else { wordd + 1 })?) } /// EIP-3860: Limit and meter initcode diff --git a/crates/interpreter/src/gas/constants.rs b/crates/interpreter/src/gas/constants.rs index 6cca72ad22..b8bd7d0544 100644 --- a/crates/interpreter/src/gas/constants.rs +++ b/crates/interpreter/src/gas/constants.rs @@ -14,8 +14,8 @@ pub const MEMORY: u64 = 3; pub const LOG: u64 = 375; pub const LOGDATA: u64 = 8; pub const LOGTOPIC: u64 = 375; -pub const SHA3: u64 = 30; -pub const SHA3WORD: u64 = 6; +pub const KECCAK256: u64 = 30; +pub const KECCAK256WORD: u64 = 6; pub const COPY: u64 = 3; pub const BLOCKHASH: u64 = 20; pub const CODEDEPOSIT: u64 = 200; diff --git a/crates/interpreter/src/instructions.rs b/crates/interpreter/src/instructions.rs index 63b5c46094..ffd187ee3e 100644 --- a/crates/interpreter/src/instructions.rs +++ b/crates/interpreter/src/instructions.rs @@ -55,7 +55,7 @@ pub fn eval(opcode: u8, interp: &mut Interpreter, host: &mut H opcode::SHL => bitwise::shl::(interp, host), opcode::SHR => bitwise::shr::(interp, host), opcode::SAR => bitwise::sar::(interp, host), - opcode::SHA3 => system::sha3(interp, host), + opcode::KECCAK256 => system::calculate_keccak256(interp, host), opcode::ADDRESS => system::address(interp, host), opcode::BALANCE => host::balance::(interp, host), opcode::SELFBALANCE => host::selfbalance::(interp, host), diff --git a/crates/interpreter/src/instructions/opcode.rs b/crates/interpreter/src/instructions/opcode.rs index be739d20cb..3953c17f86 100644 --- a/crates/interpreter/src/instructions/opcode.rs +++ b/crates/interpreter/src/instructions/opcode.rs @@ -38,7 +38,7 @@ pub const CODECOPY: u8 = 0x39; pub const SHL: u8 = 0x1b; pub const SHR: u8 = 0x1c; pub const SAR: u8 = 0x1d; -pub const SHA3: u8 = 0x20; +pub const KECCAK256: u8 = 0x20; pub const POP: u8 = 0x50; pub const MLOAD: u8 = 0x51; pub const MSTORE: u8 = 0x52; @@ -551,7 +551,7 @@ macro_rules! gas_opcodee { }), /* 0x1e */ OpInfo::none(), /* 0x1f */ OpInfo::none(), - /* 0x20 SHA3 */ OpInfo::dynamic_gas(), + /* 0x20 KECCAK256 */ OpInfo::dynamic_gas(), /* 0x21 */ OpInfo::none(), /* 0x22 */ OpInfo::none(), /* 0x23 */ OpInfo::none(), diff --git a/crates/interpreter/src/instructions/system.rs b/crates/interpreter/src/instructions/system.rs index bfef1465f7..a40a6c6cf5 100644 --- a/crates/interpreter/src/instructions/system.rs +++ b/crates/interpreter/src/instructions/system.rs @@ -6,10 +6,10 @@ use crate::{ }; use core::cmp::min; -pub fn sha3(interpreter: &mut Interpreter, _host: &mut dyn Host) { +pub fn calculate_keccak256(interpreter: &mut Interpreter, _host: &mut dyn Host) { pop!(interpreter, from, len); let len = as_usize_or_fail!(interpreter, len, InstructionResult::InvalidOperandOOG); - gas_or_fail!(interpreter, gas::sha3_cost(len as u64)); + gas_or_fail!(interpreter, gas::keccak256_cost(len as u64)); let hash = if len == 0 { KECCAK_EMPTY } else {