From 54f92095101ef294b48949404d0e57559526f464 Mon Sep 17 00:00:00 2001 From: Justin Traglia Date: Tue, 2 Apr 2024 09:57:39 -0500 Subject: [PATCH 1/8] feat: add tests for shift instructions --- bins/revm-test/src/bin/analysis.rs | 2 +- .../interpreter/src/instructions/bitwise.rs | 561 +++++++++++++++++- 2 files changed, 560 insertions(+), 3 deletions(-) diff --git a/bins/revm-test/src/bin/analysis.rs b/bins/revm-test/src/bin/analysis.rs index 49fdbc57a7..b8557b6926 100644 --- a/bins/revm-test/src/bin/analysis.rs +++ b/bins/revm-test/src/bin/analysis.rs @@ -25,7 +25,7 @@ fn main() { .with_db(BenchmarkDB::new_bytecode(bytecode_raw)) .build(); - // just to spead up processor. + // Just to warm up the processor. for _ in 0..10000 { let _ = evm.transact().unwrap(); } diff --git a/crates/interpreter/src/instructions/bitwise.rs b/crates/interpreter/src/instructions/bitwise.rs index 3f49dc57eb..cba9b86e8c 100644 --- a/crates/interpreter/src/instructions/bitwise.rs +++ b/crates/interpreter/src/instructions/bitwise.rs @@ -5,6 +5,7 @@ use crate::{ Host, Interpreter, }; use core::cmp::Ordering; +use revm_primitives::uint; pub fn lt(interpreter: &mut Interpreter, _host: &mut H) { gas!(interpreter, gas::VERYLOW); @@ -103,7 +104,11 @@ pub fn sar(interpreter: &mut Interpreter, _host: &mut H) { let value_sign = i256_sign_compl(op2); - *op2 = if value_sign == Sign::Zero || op1 >= U256::from(256) { + // If the shift count is 255+, we can short-circuit. This is because shifting by 255 bits is the + // maximum shift that still leaves 1 bit in the original 256-bit number. Shifting by 256 bits or + // more would mean that no original bits remain. The result depends on what the highest bit of + // the value is. + *op2 = if value_sign == Sign::Zero || op1 >= U256::from(255) { match value_sign { // value is 0 or >=1, pushing 0 Sign::Plus | Sign::Zero => U256::ZERO, @@ -111,7 +116,8 @@ pub fn sar(interpreter: &mut Interpreter, _host: &mut H) { Sign::Minus => U256::MAX, } } else { - const ONE: U256 = U256::from_limbs([1, 0, 0, 0]); + const ONE: U256 = uint!{1_U256}; + // SAFETY: shift count is checked above; it's less than 255. let shift = usize::try_from(op1).unwrap(); match value_sign { Sign::Plus | Sign::Zero => op2.wrapping_shr(shift), @@ -119,3 +125,554 @@ pub fn sar(interpreter: &mut Interpreter, _host: &mut H) { } }; } + +#[cfg(test)] +mod tests { + use crate::instructions::bitwise::{sar, shl, shr}; + use crate::{BytecodeLocked, Contract, DummyHost, Interpreter}; + use core::str::FromStr; + use revm_primitives::{Bytes, CancunSpec, Env, U256}; + + #[test] + fn test_shift_left() { + let contract = Contract { + input: Bytes::default(), + bytecode: BytecodeLocked::default(), + ..Default::default() + }; + let mut host = DummyHost::new(Env::default()); + let mut interpreter = Interpreter::new(contract.clone(), u64::MAX, false); + + struct TestCase { + value: &'static str, + shift: &'static str, + expected: &'static str, + } + + let test_cases = [ + /* + PUSH 0x0000000000000000000000000000000000000000000000000000000000000001 + PUSH 0x00 + SHL + --- + 0x0000000000000000000000000000000000000000000000000000000000000001 + */ + TestCase { + value: "0x0000000000000000000000000000000000000000000000000000000000000001", + shift: "0x00", + expected: "0x0000000000000000000000000000000000000000000000000000000000000001", + }, + /* + PUSH 0x0000000000000000000000000000000000000000000000000000000000000001 + PUSH 0x01 + SHL + --- + 0x0000000000000000000000000000000000000000000000000000000000000002 + */ + TestCase { + value: "0x0000000000000000000000000000000000000000000000000000000000000001", + shift: "0x01", + expected: "0x0000000000000000000000000000000000000000000000000000000000000002", + }, + /* + PUSH 0x0000000000000000000000000000000000000000000000000000000000000001 + PUSH 0xff + SHL + --- + 0x8000000000000000000000000000000000000000000000000000000000000000 + */ + TestCase { + value: "0x0000000000000000000000000000000000000000000000000000000000000001", + shift: "0xff", + expected: "0x8000000000000000000000000000000000000000000000000000000000000000", + }, + /* + PUSH 0x0000000000000000000000000000000000000000000000000000000000000001 + PUSH 0x0100 + SHL + --- + 0x0000000000000000000000000000000000000000000000000000000000000000 + */ + TestCase { + value: "0x0000000000000000000000000000000000000000000000000000000000000001", + shift: "0x0100", + expected: "0x0000000000000000000000000000000000000000000000000000000000000000", + }, + /* + PUSH 0x0000000000000000000000000000000000000000000000000000000000000001 + PUSH 0x0101 + SHL + --- + 0x0000000000000000000000000000000000000000000000000000000000000000 + */ + TestCase { + value: "0x0000000000000000000000000000000000000000000000000000000000000001", + shift: "0x0101", + expected: "0x0000000000000000000000000000000000000000000000000000000000000000", + }, + /* + PUSH 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + PUSH 0x00 + SHL + --- + 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + */ + TestCase { + value: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + shift: "0x00", + expected: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + }, + /* + PUSH 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + PUSH 0x01 + SHL + --- + 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe + */ + TestCase { + value: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + shift: "0x01", + expected: "0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe", + }, + /* + PUSH 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + PUSH 0xff + SHL + --- + 0x8000000000000000000000000000000000000000000000000000000000000000 + */ + TestCase { + value: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + shift: "0xff", + expected: "0x8000000000000000000000000000000000000000000000000000000000000000", + }, + /* + PUSH 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + PUSH 0x0100 + SHL + --- + 0x0000000000000000000000000000000000000000000000000000000000000000 + */ + TestCase { + value: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + shift: "0x0100", + expected: "0x0000000000000000000000000000000000000000000000000000000000000000", + }, + /* + PUSH 0x0000000000000000000000000000000000000000000000000000000000000000 + PUSH 0x01 + SHL + --- + 0x0000000000000000000000000000000000000000000000000000000000000000 + */ + TestCase { + value: "0x0000000000000000000000000000000000000000000000000000000000000000", + shift: "0x01", + expected: "0x0000000000000000000000000000000000000000000000000000000000000000", + }, + /* + PUSH 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + PUSH 0x01 + SHL + --- + 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe + */ + TestCase { + value: "0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + shift: "0x01", + expected: "0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe", + }, + ]; + + for test in test_cases { + host.clear(); + push!(interpreter, U256::from_str(test.value).unwrap()); + push!(interpreter, U256::from_str(test.shift).unwrap()); + shl::(&mut interpreter, &mut host); + pop!(interpreter, res); + assert_eq!(res, U256::from_str(test.expected).unwrap()); + } + } + + #[test] + fn test_logical_shift_right() { + let contract = Contract { + input: Bytes::default(), + bytecode: BytecodeLocked::default(), + ..Default::default() + }; + let mut host = DummyHost::new(Env::default()); + let mut interpreter = Interpreter::new(contract.clone(), u64::MAX, false); + + struct TestCase { + value: &'static str, + shift: &'static str, + expected: &'static str, + } + + let test_cases = [ + /* + PUSH 0x0000000000000000000000000000000000000000000000000000000000000001 + PUSH 0x00 + SHR + --- + 0x0000000000000000000000000000000000000000000000000000000000000001 + */ + TestCase { + value: "0x0000000000000000000000000000000000000000000000000000000000000001", + shift: "0x00", + expected: "0x0000000000000000000000000000000000000000000000000000000000000001", + }, + /* + PUSH 0x0000000000000000000000000000000000000000000000000000000000000001 + PUSH 0x01 + SHR + --- + 0x0000000000000000000000000000000000000000000000000000000000000000 + */ + TestCase { + value: "0x0000000000000000000000000000000000000000000000000000000000000001", + shift: "0x01", + expected: "0x0000000000000000000000000000000000000000000000000000000000000000", + }, + /* + PUSH 0x8000000000000000000000000000000000000000000000000000000000000000 + PUSH 0x01 + SHR + --- + 0x4000000000000000000000000000000000000000000000000000000000000000 + */ + TestCase { + value: "0x8000000000000000000000000000000000000000000000000000000000000000", + shift: "0x01", + expected: "0x4000000000000000000000000000000000000000000000000000000000000000", + }, + /* + PUSH 0x8000000000000000000000000000000000000000000000000000000000000000 + PUSH 0xff + SHR + --- + 0x0000000000000000000000000000000000000000000000000000000000000001 + */ + TestCase { + value: "0x8000000000000000000000000000000000000000000000000000000000000000", + shift: "0xff", + expected: "0x0000000000000000000000000000000000000000000000000000000000000001", + }, + /* + PUSH 0x8000000000000000000000000000000000000000000000000000000000000000 + PUSH 0x0100 + SHR + --- + 0x0000000000000000000000000000000000000000000000000000000000000000 + */ + TestCase { + value: "0x8000000000000000000000000000000000000000000000000000000000000000", + shift: "0x0100", + expected: "0x0000000000000000000000000000000000000000000000000000000000000000", + }, + /* + PUSH 0x8000000000000000000000000000000000000000000000000000000000000000 + PUSH 0x0101 + SHR + --- + 0x0000000000000000000000000000000000000000000000000000000000000000 + */ + TestCase { + value: "0x8000000000000000000000000000000000000000000000000000000000000000", + shift: "0x0101", + expected: "0x0000000000000000000000000000000000000000000000000000000000000000", + }, + /* + PUSH 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + PUSH 0x00 + SHR + --- + 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + */ + TestCase { + value: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + shift: "0x00", + expected: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + }, + /* + PUSH 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + PUSH 0x01 + SHR + --- + 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + */ + TestCase { + value: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + shift: "0x01", + expected: "0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + }, + /* + PUSH 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + PUSH 0xff + SHR + --- + 0x0000000000000000000000000000000000000000000000000000000000000001 + */ + TestCase { + value: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + shift: "0xff", + expected: "0x0000000000000000000000000000000000000000000000000000000000000001", + }, + /* + PUSH 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + PUSH 0x0100 + SHR + --- + 0x0000000000000000000000000000000000000000000000000000000000000000 + */ + TestCase { + value: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + shift: "0x0100", + expected: "0x0000000000000000000000000000000000000000000000000000000000000000", + }, + /* + PUSH 0x0000000000000000000000000000000000000000000000000000000000000000 + PUSH 0x01 + SHR + --- + 0x0000000000000000000000000000000000000000000000000000000000000000 + */ + TestCase { + value: "0x0000000000000000000000000000000000000000000000000000000000000000", + shift: "0x01", + expected: "0x0000000000000000000000000000000000000000000000000000000000000000", + }, + ]; + + for test in test_cases { + host.clear(); + push!(interpreter, U256::from_str(test.value).unwrap()); + push!(interpreter, U256::from_str(test.shift).unwrap()); + shr::(&mut interpreter, &mut host); + pop!(interpreter, res); + assert_eq!(res, U256::from_str(test.expected).unwrap()); + } + } + + #[test] + fn test_arithmetic_shift_right() { + let contract = Contract { + input: Bytes::default(), + bytecode: BytecodeLocked::default(), + ..Default::default() + }; + let mut host = DummyHost::new(Env::default()); + let mut interpreter = Interpreter::new(contract.clone(), u64::MAX, false); + + struct TestCase { + value: &'static str, + shift: &'static str, + expected: &'static str, + } + + let test_cases = [ + /* + PUSH 0x0000000000000000000000000000000000000000000000000000000000000001 + PUSH 0x00 + SAR + --- + 0x0000000000000000000000000000000000000000000000000000000000000001 + */ + TestCase { + value: "0x0000000000000000000000000000000000000000000000000000000000000001", + shift: "0x00", + expected: "0x0000000000000000000000000000000000000000000000000000000000000001", + }, + /* + PUSH 0x0000000000000000000000000000000000000000000000000000000000000001 + PUSH 0x01 + SAR + --- + 0x0000000000000000000000000000000000000000000000000000000000000000 + */ + TestCase { + value: "0x0000000000000000000000000000000000000000000000000000000000000001", + shift: "0x01", + expected: "0x0000000000000000000000000000000000000000000000000000000000000000", + }, + /* + PUSH 0x8000000000000000000000000000000000000000000000000000000000000000 + PUSH 0x01 + SAR + --- + 0xc000000000000000000000000000000000000000000000000000000000000000 + */ + TestCase { + value: "0x8000000000000000000000000000000000000000000000000000000000000000", + shift: "0x01", + expected: "0xc000000000000000000000000000000000000000000000000000000000000000", + }, + /* + PUSH 0x8000000000000000000000000000000000000000000000000000000000000000 + PUSH 0xff + SAR + --- + 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + */ + TestCase { + value: "0x8000000000000000000000000000000000000000000000000000000000000000", + shift: "0xff", + expected: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + }, + /* + PUSH 0x8000000000000000000000000000000000000000000000000000000000000000 + PUSH 0x0100 + SAR + --- + 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + */ + TestCase { + value: "0x8000000000000000000000000000000000000000000000000000000000000000", + shift: "0x0100", + expected: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + }, + /* + PUSH 0x8000000000000000000000000000000000000000000000000000000000000000 + PUSH 0x0101 + SAR + --- + 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + */ + TestCase { + value: "0x8000000000000000000000000000000000000000000000000000000000000000", + shift: "0x0101", + expected: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + }, + /* + PUSH 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + PUSH 0x00 + SAR + --- + 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + */ + TestCase { + value: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + shift: "0x00", + expected: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + }, + /* + PUSH 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + PUSH 0x01 + SAR + --- + 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + */ + TestCase { + value: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + shift: "0x01", + expected: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + }, + /* + PUSH 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + PUSH 0xff + SAR + --- + 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + */ + TestCase { + value: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + shift: "0xff", + expected: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + }, + /* + PUSH 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + PUSH 0x0100 + SAR + --- + 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + */ + TestCase { + value: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + shift: "0x0100", + expected: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + }, + /* + PUSH 0x0000000000000000000000000000000000000000000000000000000000000000 + PUSH 0x01 + SAR + --- + 0x0000000000000000000000000000000000000000000000000000000000000000 + */ + TestCase { + value: "0x0000000000000000000000000000000000000000000000000000000000000000", + shift: "0x01", + expected: "0x0000000000000000000000000000000000000000000000000000000000000000", + }, + /* + PUSH 0x4000000000000000000000000000000000000000000000000000000000000000 + PUSH 0xfe + SAR + --- + 0x0000000000000000000000000000000000000000000000000000000000000001 + */ + TestCase { + value: "0x4000000000000000000000000000000000000000000000000000000000000000", + shift: "0xfe", + expected: "0x0000000000000000000000000000000000000000000000000000000000000001", + }, + /* + PUSH 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + PUSH 0xf8 + SAR + --- + 0x000000000000000000000000000000000000000000000000000000000000007f + */ + TestCase { + value: "0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + shift: "0xf8", + expected: "0x000000000000000000000000000000000000000000000000000000000000007f", + }, + /* + PUSH 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + PUSH 0xfe + SAR + --- + 0x0000000000000000000000000000000000000000000000000000000000000001 + */ + TestCase { + value: "0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + shift: "0xfe", + expected: "0x0000000000000000000000000000000000000000000000000000000000000001", + }, + /* + PUSH 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + PUSH 0xff + SAR + --- + 0x0000000000000000000000000000000000000000000000000000000000000000 + */ + TestCase { + value: "0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + shift: "0xff", + expected: "0x0000000000000000000000000000000000000000000000000000000000000000", + }, + /* + PUSH 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + PUSH 0x0100 + SAR + --- + 0x0000000000000000000000000000000000000000000000000000000000000000 + */ + TestCase { + value: "0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + shift: "0x0100", + expected: "0x0000000000000000000000000000000000000000000000000000000000000000", + }, + ]; + + for test in test_cases { + host.clear(); + push!(interpreter, U256::from_str(test.value).unwrap()); + push!(interpreter, U256::from_str(test.shift).unwrap()); + sar::(&mut interpreter, &mut host); + pop!(interpreter, res); + assert_eq!(res, U256::from_str(test.expected).unwrap()); + } + } +} From 7b102f42e94ada2404d5c4816c41d1432d937d31 Mon Sep 17 00:00:00 2001 From: Justin Traglia Date: Tue, 2 Apr 2024 10:13:52 -0500 Subject: [PATCH 2/8] Use LatestSpec instead of CancunSpec --- crates/interpreter/src/instructions/bitwise.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/interpreter/src/instructions/bitwise.rs b/crates/interpreter/src/instructions/bitwise.rs index cba9b86e8c..a7ffa16bbc 100644 --- a/crates/interpreter/src/instructions/bitwise.rs +++ b/crates/interpreter/src/instructions/bitwise.rs @@ -131,7 +131,7 @@ mod tests { use crate::instructions::bitwise::{sar, shl, shr}; use crate::{BytecodeLocked, Contract, DummyHost, Interpreter}; use core::str::FromStr; - use revm_primitives::{Bytes, CancunSpec, Env, U256}; + use revm_primitives::{Bytes, LatestSpec, Env, U256}; #[test] fn test_shift_left() { @@ -288,7 +288,7 @@ mod tests { host.clear(); push!(interpreter, U256::from_str(test.value).unwrap()); push!(interpreter, U256::from_str(test.shift).unwrap()); - shl::(&mut interpreter, &mut host); + shl::(&mut interpreter, &mut host); pop!(interpreter, res); assert_eq!(res, U256::from_str(test.expected).unwrap()); } @@ -449,7 +449,7 @@ mod tests { host.clear(); push!(interpreter, U256::from_str(test.value).unwrap()); push!(interpreter, U256::from_str(test.shift).unwrap()); - shr::(&mut interpreter, &mut host); + shr::(&mut interpreter, &mut host); pop!(interpreter, res); assert_eq!(res, U256::from_str(test.expected).unwrap()); } @@ -670,7 +670,7 @@ mod tests { host.clear(); push!(interpreter, U256::from_str(test.value).unwrap()); push!(interpreter, U256::from_str(test.shift).unwrap()); - sar::(&mut interpreter, &mut host); + sar::(&mut interpreter, &mut host); pop!(interpreter, res); assert_eq!(res, U256::from_str(test.expected).unwrap()); } From d3f3c6cd7bae49033e03462ca4321b2a994695d1 Mon Sep 17 00:00:00 2001 From: Justin Traglia Date: Tue, 2 Apr 2024 10:15:38 -0500 Subject: [PATCH 3/8] Run cargo fmt --- crates/interpreter/src/instructions/bitwise.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/interpreter/src/instructions/bitwise.rs b/crates/interpreter/src/instructions/bitwise.rs index a7ffa16bbc..1a3c7c77ba 100644 --- a/crates/interpreter/src/instructions/bitwise.rs +++ b/crates/interpreter/src/instructions/bitwise.rs @@ -116,7 +116,7 @@ pub fn sar(interpreter: &mut Interpreter, _host: &mut H) { Sign::Minus => U256::MAX, } } else { - const ONE: U256 = uint!{1_U256}; + const ONE: U256 = uint! {1_U256}; // SAFETY: shift count is checked above; it's less than 255. let shift = usize::try_from(op1).unwrap(); match value_sign { @@ -131,7 +131,7 @@ mod tests { use crate::instructions::bitwise::{sar, shl, shr}; use crate::{BytecodeLocked, Contract, DummyHost, Interpreter}; use core::str::FromStr; - use revm_primitives::{Bytes, LatestSpec, Env, U256}; + use revm_primitives::{Bytes, Env, LatestSpec, U256}; #[test] fn test_shift_left() { From 13715ca428047f93605bdd37090061d92c88095f Mon Sep 17 00:00:00 2001 From: Justin Traglia Date: Tue, 2 Apr 2024 10:17:32 -0500 Subject: [PATCH 4/8] Use parans instead of brackets --- crates/interpreter/src/instructions/bitwise.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/interpreter/src/instructions/bitwise.rs b/crates/interpreter/src/instructions/bitwise.rs index 1a3c7c77ba..49123c1424 100644 --- a/crates/interpreter/src/instructions/bitwise.rs +++ b/crates/interpreter/src/instructions/bitwise.rs @@ -116,7 +116,7 @@ pub fn sar(interpreter: &mut Interpreter, _host: &mut H) { Sign::Minus => U256::MAX, } } else { - const ONE: U256 = uint! {1_U256}; + const ONE: U256 = uint!(1_U256); // SAFETY: shift count is checked above; it's less than 255. let shift = usize::try_from(op1).unwrap(); match value_sign { From b912ea30e49b7ea31ab03bfa63cddea89627f5a2 Mon Sep 17 00:00:00 2001 From: Justin Traglia Date: Tue, 2 Apr 2024 11:46:49 -0500 Subject: [PATCH 5/8] Simplify host/interpreter initialization --- .../interpreter/src/instructions/bitwise.rs | 21 +++---------------- 1 file changed, 3 insertions(+), 18 deletions(-) diff --git a/crates/interpreter/src/instructions/bitwise.rs b/crates/interpreter/src/instructions/bitwise.rs index 49123c1424..e58d4ae0d6 100644 --- a/crates/interpreter/src/instructions/bitwise.rs +++ b/crates/interpreter/src/instructions/bitwise.rs @@ -135,13 +135,8 @@ mod tests { #[test] fn test_shift_left() { - let contract = Contract { - input: Bytes::default(), - bytecode: BytecodeLocked::default(), - ..Default::default() - }; let mut host = DummyHost::new(Env::default()); - let mut interpreter = Interpreter::new(contract.clone(), u64::MAX, false); + let mut interpreter = Interpreter::new(Contract::default(), u64::MAX, false); struct TestCase { value: &'static str, @@ -296,13 +291,8 @@ mod tests { #[test] fn test_logical_shift_right() { - let contract = Contract { - input: Bytes::default(), - bytecode: BytecodeLocked::default(), - ..Default::default() - }; let mut host = DummyHost::new(Env::default()); - let mut interpreter = Interpreter::new(contract.clone(), u64::MAX, false); + let mut interpreter = Interpreter::new(Contract::default(), u64::MAX, false); struct TestCase { value: &'static str, @@ -457,13 +447,8 @@ mod tests { #[test] fn test_arithmetic_shift_right() { - let contract = Contract { - input: Bytes::default(), - bytecode: BytecodeLocked::default(), - ..Default::default() - }; let mut host = DummyHost::new(Env::default()); - let mut interpreter = Interpreter::new(contract.clone(), u64::MAX, false); + let mut interpreter = Interpreter::new(Contract::default(), u64::MAX, false); struct TestCase { value: &'static str, From 938c8874e59384f11ecd8ceda48a31352a1042a9 Mon Sep 17 00:00:00 2001 From: Justin Traglia Date: Tue, 2 Apr 2024 12:28:04 -0500 Subject: [PATCH 6/8] Remove unused imports --- crates/interpreter/src/instructions/bitwise.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/interpreter/src/instructions/bitwise.rs b/crates/interpreter/src/instructions/bitwise.rs index e58d4ae0d6..94f799871a 100644 --- a/crates/interpreter/src/instructions/bitwise.rs +++ b/crates/interpreter/src/instructions/bitwise.rs @@ -129,9 +129,9 @@ pub fn sar(interpreter: &mut Interpreter, _host: &mut H) { #[cfg(test)] mod tests { use crate::instructions::bitwise::{sar, shl, shr}; - use crate::{BytecodeLocked, Contract, DummyHost, Interpreter}; + use crate::{Contract, DummyHost, Interpreter}; use core::str::FromStr; - use revm_primitives::{Bytes, Env, LatestSpec, U256}; + use revm_primitives::{Env, LatestSpec, U256}; #[test] fn test_shift_left() { From 61142442dd93d58971e8439980d74e7ee8bfd3df Mon Sep 17 00:00:00 2001 From: Justin Traglia Date: Tue, 2 Apr 2024 13:35:26 -0500 Subject: [PATCH 7/8] Remove test case comments --- .../interpreter/src/instructions/bitwise.rs | 266 ------------------ 1 file changed, 266 deletions(-) diff --git a/crates/interpreter/src/instructions/bitwise.rs b/crates/interpreter/src/instructions/bitwise.rs index 94f799871a..c6384831b3 100644 --- a/crates/interpreter/src/instructions/bitwise.rs +++ b/crates/interpreter/src/instructions/bitwise.rs @@ -145,133 +145,56 @@ mod tests { } let test_cases = [ - /* - PUSH 0x0000000000000000000000000000000000000000000000000000000000000001 - PUSH 0x00 - SHL - --- - 0x0000000000000000000000000000000000000000000000000000000000000001 - */ TestCase { value: "0x0000000000000000000000000000000000000000000000000000000000000001", shift: "0x00", expected: "0x0000000000000000000000000000000000000000000000000000000000000001", }, - /* - PUSH 0x0000000000000000000000000000000000000000000000000000000000000001 - PUSH 0x01 - SHL - --- - 0x0000000000000000000000000000000000000000000000000000000000000002 - */ TestCase { value: "0x0000000000000000000000000000000000000000000000000000000000000001", shift: "0x01", expected: "0x0000000000000000000000000000000000000000000000000000000000000002", }, - /* - PUSH 0x0000000000000000000000000000000000000000000000000000000000000001 - PUSH 0xff - SHL - --- - 0x8000000000000000000000000000000000000000000000000000000000000000 - */ TestCase { value: "0x0000000000000000000000000000000000000000000000000000000000000001", shift: "0xff", expected: "0x8000000000000000000000000000000000000000000000000000000000000000", }, - /* - PUSH 0x0000000000000000000000000000000000000000000000000000000000000001 - PUSH 0x0100 - SHL - --- - 0x0000000000000000000000000000000000000000000000000000000000000000 - */ TestCase { value: "0x0000000000000000000000000000000000000000000000000000000000000001", shift: "0x0100", expected: "0x0000000000000000000000000000000000000000000000000000000000000000", }, - /* - PUSH 0x0000000000000000000000000000000000000000000000000000000000000001 - PUSH 0x0101 - SHL - --- - 0x0000000000000000000000000000000000000000000000000000000000000000 - */ TestCase { value: "0x0000000000000000000000000000000000000000000000000000000000000001", shift: "0x0101", expected: "0x0000000000000000000000000000000000000000000000000000000000000000", }, - /* - PUSH 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - PUSH 0x00 - SHL - --- - 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - */ TestCase { value: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", shift: "0x00", expected: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", }, - /* - PUSH 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - PUSH 0x01 - SHL - --- - 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe - */ TestCase { value: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", shift: "0x01", expected: "0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe", }, - /* - PUSH 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - PUSH 0xff - SHL - --- - 0x8000000000000000000000000000000000000000000000000000000000000000 - */ TestCase { value: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", shift: "0xff", expected: "0x8000000000000000000000000000000000000000000000000000000000000000", }, - /* - PUSH 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - PUSH 0x0100 - SHL - --- - 0x0000000000000000000000000000000000000000000000000000000000000000 - */ TestCase { value: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", shift: "0x0100", expected: "0x0000000000000000000000000000000000000000000000000000000000000000", }, - /* - PUSH 0x0000000000000000000000000000000000000000000000000000000000000000 - PUSH 0x01 - SHL - --- - 0x0000000000000000000000000000000000000000000000000000000000000000 - */ TestCase { value: "0x0000000000000000000000000000000000000000000000000000000000000000", shift: "0x01", expected: "0x0000000000000000000000000000000000000000000000000000000000000000", }, - /* - PUSH 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - PUSH 0x01 - SHL - --- - 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe - */ TestCase { value: "0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", shift: "0x01", @@ -301,133 +224,56 @@ mod tests { } let test_cases = [ - /* - PUSH 0x0000000000000000000000000000000000000000000000000000000000000001 - PUSH 0x00 - SHR - --- - 0x0000000000000000000000000000000000000000000000000000000000000001 - */ TestCase { value: "0x0000000000000000000000000000000000000000000000000000000000000001", shift: "0x00", expected: "0x0000000000000000000000000000000000000000000000000000000000000001", }, - /* - PUSH 0x0000000000000000000000000000000000000000000000000000000000000001 - PUSH 0x01 - SHR - --- - 0x0000000000000000000000000000000000000000000000000000000000000000 - */ TestCase { value: "0x0000000000000000000000000000000000000000000000000000000000000001", shift: "0x01", expected: "0x0000000000000000000000000000000000000000000000000000000000000000", }, - /* - PUSH 0x8000000000000000000000000000000000000000000000000000000000000000 - PUSH 0x01 - SHR - --- - 0x4000000000000000000000000000000000000000000000000000000000000000 - */ TestCase { value: "0x8000000000000000000000000000000000000000000000000000000000000000", shift: "0x01", expected: "0x4000000000000000000000000000000000000000000000000000000000000000", }, - /* - PUSH 0x8000000000000000000000000000000000000000000000000000000000000000 - PUSH 0xff - SHR - --- - 0x0000000000000000000000000000000000000000000000000000000000000001 - */ TestCase { value: "0x8000000000000000000000000000000000000000000000000000000000000000", shift: "0xff", expected: "0x0000000000000000000000000000000000000000000000000000000000000001", }, - /* - PUSH 0x8000000000000000000000000000000000000000000000000000000000000000 - PUSH 0x0100 - SHR - --- - 0x0000000000000000000000000000000000000000000000000000000000000000 - */ TestCase { value: "0x8000000000000000000000000000000000000000000000000000000000000000", shift: "0x0100", expected: "0x0000000000000000000000000000000000000000000000000000000000000000", }, - /* - PUSH 0x8000000000000000000000000000000000000000000000000000000000000000 - PUSH 0x0101 - SHR - --- - 0x0000000000000000000000000000000000000000000000000000000000000000 - */ TestCase { value: "0x8000000000000000000000000000000000000000000000000000000000000000", shift: "0x0101", expected: "0x0000000000000000000000000000000000000000000000000000000000000000", }, - /* - PUSH 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - PUSH 0x00 - SHR - --- - 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - */ TestCase { value: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", shift: "0x00", expected: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", }, - /* - PUSH 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - PUSH 0x01 - SHR - --- - 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - */ TestCase { value: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", shift: "0x01", expected: "0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", }, - /* - PUSH 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - PUSH 0xff - SHR - --- - 0x0000000000000000000000000000000000000000000000000000000000000001 - */ TestCase { value: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", shift: "0xff", expected: "0x0000000000000000000000000000000000000000000000000000000000000001", }, - /* - PUSH 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - PUSH 0x0100 - SHR - --- - 0x0000000000000000000000000000000000000000000000000000000000000000 - */ TestCase { value: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", shift: "0x0100", expected: "0x0000000000000000000000000000000000000000000000000000000000000000", }, - /* - PUSH 0x0000000000000000000000000000000000000000000000000000000000000000 - PUSH 0x01 - SHR - --- - 0x0000000000000000000000000000000000000000000000000000000000000000 - */ TestCase { value: "0x0000000000000000000000000000000000000000000000000000000000000000", shift: "0x01", @@ -457,193 +303,81 @@ mod tests { } let test_cases = [ - /* - PUSH 0x0000000000000000000000000000000000000000000000000000000000000001 - PUSH 0x00 - SAR - --- - 0x0000000000000000000000000000000000000000000000000000000000000001 - */ TestCase { value: "0x0000000000000000000000000000000000000000000000000000000000000001", shift: "0x00", expected: "0x0000000000000000000000000000000000000000000000000000000000000001", }, - /* - PUSH 0x0000000000000000000000000000000000000000000000000000000000000001 - PUSH 0x01 - SAR - --- - 0x0000000000000000000000000000000000000000000000000000000000000000 - */ TestCase { value: "0x0000000000000000000000000000000000000000000000000000000000000001", shift: "0x01", expected: "0x0000000000000000000000000000000000000000000000000000000000000000", }, - /* - PUSH 0x8000000000000000000000000000000000000000000000000000000000000000 - PUSH 0x01 - SAR - --- - 0xc000000000000000000000000000000000000000000000000000000000000000 - */ TestCase { value: "0x8000000000000000000000000000000000000000000000000000000000000000", shift: "0x01", expected: "0xc000000000000000000000000000000000000000000000000000000000000000", }, - /* - PUSH 0x8000000000000000000000000000000000000000000000000000000000000000 - PUSH 0xff - SAR - --- - 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - */ TestCase { value: "0x8000000000000000000000000000000000000000000000000000000000000000", shift: "0xff", expected: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", }, - /* - PUSH 0x8000000000000000000000000000000000000000000000000000000000000000 - PUSH 0x0100 - SAR - --- - 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - */ TestCase { value: "0x8000000000000000000000000000000000000000000000000000000000000000", shift: "0x0100", expected: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", }, - /* - PUSH 0x8000000000000000000000000000000000000000000000000000000000000000 - PUSH 0x0101 - SAR - --- - 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - */ TestCase { value: "0x8000000000000000000000000000000000000000000000000000000000000000", shift: "0x0101", expected: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", }, - /* - PUSH 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - PUSH 0x00 - SAR - --- - 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - */ TestCase { value: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", shift: "0x00", expected: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", }, - /* - PUSH 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - PUSH 0x01 - SAR - --- - 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - */ TestCase { value: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", shift: "0x01", expected: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", }, - /* - PUSH 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - PUSH 0xff - SAR - --- - 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - */ TestCase { value: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", shift: "0xff", expected: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", }, - /* - PUSH 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - PUSH 0x0100 - SAR - --- - 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - */ TestCase { value: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", shift: "0x0100", expected: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", }, - /* - PUSH 0x0000000000000000000000000000000000000000000000000000000000000000 - PUSH 0x01 - SAR - --- - 0x0000000000000000000000000000000000000000000000000000000000000000 - */ TestCase { value: "0x0000000000000000000000000000000000000000000000000000000000000000", shift: "0x01", expected: "0x0000000000000000000000000000000000000000000000000000000000000000", }, - /* - PUSH 0x4000000000000000000000000000000000000000000000000000000000000000 - PUSH 0xfe - SAR - --- - 0x0000000000000000000000000000000000000000000000000000000000000001 - */ TestCase { value: "0x4000000000000000000000000000000000000000000000000000000000000000", shift: "0xfe", expected: "0x0000000000000000000000000000000000000000000000000000000000000001", }, - /* - PUSH 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - PUSH 0xf8 - SAR - --- - 0x000000000000000000000000000000000000000000000000000000000000007f - */ TestCase { value: "0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", shift: "0xf8", expected: "0x000000000000000000000000000000000000000000000000000000000000007f", }, - /* - PUSH 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - PUSH 0xfe - SAR - --- - 0x0000000000000000000000000000000000000000000000000000000000000001 - */ TestCase { value: "0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", shift: "0xfe", expected: "0x0000000000000000000000000000000000000000000000000000000000000001", }, - /* - PUSH 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - PUSH 0xff - SAR - --- - 0x0000000000000000000000000000000000000000000000000000000000000000 - */ TestCase { value: "0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", shift: "0xff", expected: "0x0000000000000000000000000000000000000000000000000000000000000000", }, - /* - PUSH 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - PUSH 0x0100 - SAR - --- - 0x0000000000000000000000000000000000000000000000000000000000000000 - */ TestCase { value: "0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", shift: "0x0100", From fee0b08937e01d64e3498c9b671398340dd686cd Mon Sep 17 00:00:00 2001 From: Justin Traglia Date: Tue, 2 Apr 2024 13:44:04 -0500 Subject: [PATCH 8/8] Use U256 types --- .../interpreter/src/instructions/bitwise.rs | 369 +++++++++--------- 1 file changed, 187 insertions(+), 182 deletions(-) diff --git a/crates/interpreter/src/instructions/bitwise.rs b/crates/interpreter/src/instructions/bitwise.rs index c6384831b3..d832964214 100644 --- a/crates/interpreter/src/instructions/bitwise.rs +++ b/crates/interpreter/src/instructions/bitwise.rs @@ -130,8 +130,7 @@ pub fn sar(interpreter: &mut Interpreter, _host: &mut H) { mod tests { use crate::instructions::bitwise::{sar, shl, shr}; use crate::{Contract, DummyHost, Interpreter}; - use core::str::FromStr; - use revm_primitives::{Env, LatestSpec, U256}; + use revm_primitives::{uint, Env, LatestSpec, U256}; #[test] fn test_shift_left() { @@ -139,76 +138,78 @@ mod tests { let mut interpreter = Interpreter::new(Contract::default(), u64::MAX, false); struct TestCase { - value: &'static str, - shift: &'static str, - expected: &'static str, + value: U256, + shift: U256, + expected: U256, } - let test_cases = [ - TestCase { - value: "0x0000000000000000000000000000000000000000000000000000000000000001", - shift: "0x00", - expected: "0x0000000000000000000000000000000000000000000000000000000000000001", - }, - TestCase { - value: "0x0000000000000000000000000000000000000000000000000000000000000001", - shift: "0x01", - expected: "0x0000000000000000000000000000000000000000000000000000000000000002", - }, - TestCase { - value: "0x0000000000000000000000000000000000000000000000000000000000000001", - shift: "0xff", - expected: "0x8000000000000000000000000000000000000000000000000000000000000000", - }, - TestCase { - value: "0x0000000000000000000000000000000000000000000000000000000000000001", - shift: "0x0100", - expected: "0x0000000000000000000000000000000000000000000000000000000000000000", - }, - TestCase { - value: "0x0000000000000000000000000000000000000000000000000000000000000001", - shift: "0x0101", - expected: "0x0000000000000000000000000000000000000000000000000000000000000000", - }, - TestCase { - value: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", - shift: "0x00", - expected: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", - }, - TestCase { - value: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", - shift: "0x01", - expected: "0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe", - }, - TestCase { - value: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", - shift: "0xff", - expected: "0x8000000000000000000000000000000000000000000000000000000000000000", - }, - TestCase { - value: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", - shift: "0x0100", - expected: "0x0000000000000000000000000000000000000000000000000000000000000000", - }, - TestCase { - value: "0x0000000000000000000000000000000000000000000000000000000000000000", - shift: "0x01", - expected: "0x0000000000000000000000000000000000000000000000000000000000000000", - }, - TestCase { - value: "0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", - shift: "0x01", - expected: "0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe", - }, - ]; + uint! { + let test_cases = [ + TestCase { + value: 0x0000000000000000000000000000000000000000000000000000000000000001_U256, + shift: 0x00_U256, + expected: 0x0000000000000000000000000000000000000000000000000000000000000001_U256, + }, + TestCase { + value: 0x0000000000000000000000000000000000000000000000000000000000000001_U256, + shift: 0x01_U256, + expected: 0x0000000000000000000000000000000000000000000000000000000000000002_U256, + }, + TestCase { + value: 0x0000000000000000000000000000000000000000000000000000000000000001_U256, + shift: 0xff_U256, + expected: 0x8000000000000000000000000000000000000000000000000000000000000000_U256, + }, + TestCase { + value: 0x0000000000000000000000000000000000000000000000000000000000000001_U256, + shift: 0x0100_U256, + expected: 0x0000000000000000000000000000000000000000000000000000000000000000_U256, + }, + TestCase { + value: 0x0000000000000000000000000000000000000000000000000000000000000001_U256, + shift: 0x0101_U256, + expected: 0x0000000000000000000000000000000000000000000000000000000000000000_U256, + }, + TestCase { + value: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256, + shift: 0x00_U256, + expected: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256, + }, + TestCase { + value: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256, + shift: 0x01_U256, + expected: 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe_U256, + }, + TestCase { + value: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256, + shift: 0xff_U256, + expected: 0x8000000000000000000000000000000000000000000000000000000000000000_U256, + }, + TestCase { + value: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256, + shift: 0x0100_U256, + expected: 0x0000000000000000000000000000000000000000000000000000000000000000_U256, + }, + TestCase { + value: 0x0000000000000000000000000000000000000000000000000000000000000000_U256, + shift: 0x01_U256, + expected: 0x0000000000000000000000000000000000000000000000000000000000000000_U256, + }, + TestCase { + value: 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256, + shift: 0x01_U256, + expected: 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe_U256, + }, + ]; + } for test in test_cases { host.clear(); - push!(interpreter, U256::from_str(test.value).unwrap()); - push!(interpreter, U256::from_str(test.shift).unwrap()); + push!(interpreter, test.value); + push!(interpreter, test.shift); shl::(&mut interpreter, &mut host); pop!(interpreter, res); - assert_eq!(res, U256::from_str(test.expected).unwrap()); + assert_eq!(res, test.expected); } } @@ -218,76 +219,78 @@ mod tests { let mut interpreter = Interpreter::new(Contract::default(), u64::MAX, false); struct TestCase { - value: &'static str, - shift: &'static str, - expected: &'static str, + value: U256, + shift: U256, + expected: U256, } - let test_cases = [ - TestCase { - value: "0x0000000000000000000000000000000000000000000000000000000000000001", - shift: "0x00", - expected: "0x0000000000000000000000000000000000000000000000000000000000000001", - }, - TestCase { - value: "0x0000000000000000000000000000000000000000000000000000000000000001", - shift: "0x01", - expected: "0x0000000000000000000000000000000000000000000000000000000000000000", - }, - TestCase { - value: "0x8000000000000000000000000000000000000000000000000000000000000000", - shift: "0x01", - expected: "0x4000000000000000000000000000000000000000000000000000000000000000", - }, - TestCase { - value: "0x8000000000000000000000000000000000000000000000000000000000000000", - shift: "0xff", - expected: "0x0000000000000000000000000000000000000000000000000000000000000001", - }, - TestCase { - value: "0x8000000000000000000000000000000000000000000000000000000000000000", - shift: "0x0100", - expected: "0x0000000000000000000000000000000000000000000000000000000000000000", - }, - TestCase { - value: "0x8000000000000000000000000000000000000000000000000000000000000000", - shift: "0x0101", - expected: "0x0000000000000000000000000000000000000000000000000000000000000000", - }, - TestCase { - value: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", - shift: "0x00", - expected: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", - }, - TestCase { - value: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", - shift: "0x01", - expected: "0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", - }, - TestCase { - value: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", - shift: "0xff", - expected: "0x0000000000000000000000000000000000000000000000000000000000000001", - }, - TestCase { - value: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", - shift: "0x0100", - expected: "0x0000000000000000000000000000000000000000000000000000000000000000", - }, - TestCase { - value: "0x0000000000000000000000000000000000000000000000000000000000000000", - shift: "0x01", - expected: "0x0000000000000000000000000000000000000000000000000000000000000000", - }, - ]; + uint! { + let test_cases = [ + TestCase { + value: 0x0000000000000000000000000000000000000000000000000000000000000001_U256, + shift: 0x00_U256, + expected: 0x0000000000000000000000000000000000000000000000000000000000000001_U256, + }, + TestCase { + value: 0x0000000000000000000000000000000000000000000000000000000000000001_U256, + shift: 0x01_U256, + expected: 0x0000000000000000000000000000000000000000000000000000000000000000_U256, + }, + TestCase { + value: 0x8000000000000000000000000000000000000000000000000000000000000000_U256, + shift: 0x01_U256, + expected: 0x4000000000000000000000000000000000000000000000000000000000000000_U256, + }, + TestCase { + value: 0x8000000000000000000000000000000000000000000000000000000000000000_U256, + shift: 0xff_U256, + expected: 0x0000000000000000000000000000000000000000000000000000000000000001_U256, + }, + TestCase { + value: 0x8000000000000000000000000000000000000000000000000000000000000000_U256, + shift: 0x0100_U256, + expected: 0x0000000000000000000000000000000000000000000000000000000000000000_U256, + }, + TestCase { + value: 0x8000000000000000000000000000000000000000000000000000000000000000_U256, + shift: 0x0101_U256, + expected: 0x0000000000000000000000000000000000000000000000000000000000000000_U256, + }, + TestCase { + value: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256, + shift: 0x00_U256, + expected: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256, + }, + TestCase { + value: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256, + shift: 0x01_U256, + expected: 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256, + }, + TestCase { + value: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256, + shift: 0xff_U256, + expected: 0x0000000000000000000000000000000000000000000000000000000000000001_U256, + }, + TestCase { + value: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256, + shift: 0x0100_U256, + expected: 0x0000000000000000000000000000000000000000000000000000000000000000_U256, + }, + TestCase { + value: 0x0000000000000000000000000000000000000000000000000000000000000000_U256, + shift: 0x01_U256, + expected: 0x0000000000000000000000000000000000000000000000000000000000000000_U256, + }, + ]; + } for test in test_cases { host.clear(); - push!(interpreter, U256::from_str(test.value).unwrap()); - push!(interpreter, U256::from_str(test.shift).unwrap()); + push!(interpreter, test.value); + push!(interpreter, test.shift); shr::(&mut interpreter, &mut host); pop!(interpreter, res); - assert_eq!(res, U256::from_str(test.expected).unwrap()); + assert_eq!(res, test.expected); } } @@ -297,101 +300,103 @@ mod tests { let mut interpreter = Interpreter::new(Contract::default(), u64::MAX, false); struct TestCase { - value: &'static str, - shift: &'static str, - expected: &'static str, + value: U256, + shift: U256, + expected: U256, } + uint! { let test_cases = [ TestCase { - value: "0x0000000000000000000000000000000000000000000000000000000000000001", - shift: "0x00", - expected: "0x0000000000000000000000000000000000000000000000000000000000000001", + value: 0x0000000000000000000000000000000000000000000000000000000000000001_U256, + shift: 0x00_U256, + expected: 0x0000000000000000000000000000000000000000000000000000000000000001_U256, }, TestCase { - value: "0x0000000000000000000000000000000000000000000000000000000000000001", - shift: "0x01", - expected: "0x0000000000000000000000000000000000000000000000000000000000000000", + value: 0x0000000000000000000000000000000000000000000000000000000000000001_U256, + shift: 0x01_U256, + expected: 0x0000000000000000000000000000000000000000000000000000000000000000_U256, }, TestCase { - value: "0x8000000000000000000000000000000000000000000000000000000000000000", - shift: "0x01", - expected: "0xc000000000000000000000000000000000000000000000000000000000000000", + value: 0x8000000000000000000000000000000000000000000000000000000000000000_U256, + shift: 0x01_U256, + expected: 0xc000000000000000000000000000000000000000000000000000000000000000_U256, }, TestCase { - value: "0x8000000000000000000000000000000000000000000000000000000000000000", - shift: "0xff", - expected: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + value: 0x8000000000000000000000000000000000000000000000000000000000000000_U256, + shift: 0xff_U256, + expected: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256, }, TestCase { - value: "0x8000000000000000000000000000000000000000000000000000000000000000", - shift: "0x0100", - expected: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + value: 0x8000000000000000000000000000000000000000000000000000000000000000_U256, + shift: 0x0100_U256, + expected: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256, }, TestCase { - value: "0x8000000000000000000000000000000000000000000000000000000000000000", - shift: "0x0101", - expected: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + value: 0x8000000000000000000000000000000000000000000000000000000000000000_U256, + shift: 0x0101_U256, + expected: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256, }, TestCase { - value: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", - shift: "0x00", - expected: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + value: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256, + shift: 0x00_U256, + expected: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256, }, TestCase { - value: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", - shift: "0x01", - expected: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + value: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256, + shift: 0x01_U256, + expected: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256, }, TestCase { - value: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", - shift: "0xff", - expected: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + value: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256, + shift: 0xff_U256, + expected: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256, }, TestCase { - value: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", - shift: "0x0100", - expected: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + value: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256, + shift: 0x0100_U256, + expected: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256, }, TestCase { - value: "0x0000000000000000000000000000000000000000000000000000000000000000", - shift: "0x01", - expected: "0x0000000000000000000000000000000000000000000000000000000000000000", + value: 0x0000000000000000000000000000000000000000000000000000000000000000_U256, + shift: 0x01_U256, + expected: 0x0000000000000000000000000000000000000000000000000000000000000000_U256, }, TestCase { - value: "0x4000000000000000000000000000000000000000000000000000000000000000", - shift: "0xfe", - expected: "0x0000000000000000000000000000000000000000000000000000000000000001", + value: 0x4000000000000000000000000000000000000000000000000000000000000000_U256, + shift: 0xfe_U256, + expected: 0x0000000000000000000000000000000000000000000000000000000000000001_U256, }, TestCase { - value: "0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", - shift: "0xf8", - expected: "0x000000000000000000000000000000000000000000000000000000000000007f", + value: 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256, + shift: 0xf8_U256, + expected: 0x000000000000000000000000000000000000000000000000000000000000007f_U256, }, TestCase { - value: "0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", - shift: "0xfe", - expected: "0x0000000000000000000000000000000000000000000000000000000000000001", + value: 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256, + shift: 0xfe_U256, + expected: 0x0000000000000000000000000000000000000000000000000000000000000001_U256, }, TestCase { - value: "0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", - shift: "0xff", - expected: "0x0000000000000000000000000000000000000000000000000000000000000000", + value: 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256, + shift: 0xff_U256, + expected: 0x0000000000000000000000000000000000000000000000000000000000000000_U256, }, TestCase { - value: "0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", - shift: "0x0100", - expected: "0x0000000000000000000000000000000000000000000000000000000000000000", + value: 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256, + shift: 0x0100_U256, + expected: 0x0000000000000000000000000000000000000000000000000000000000000000_U256, }, ]; + } for test in test_cases { host.clear(); - push!(interpreter, U256::from_str(test.value).unwrap()); - push!(interpreter, U256::from_str(test.shift).unwrap()); + push!(interpreter, test.value); + push!(interpreter, test.shift); sar::(&mut interpreter, &mut host); pop!(interpreter, res); - assert_eq!(res, U256::from_str(test.expected).unwrap()); + assert_eq!(res, test.expected); } } }