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

feat: add tests for shift instructions #1254

Merged
merged 8 commits into from
Apr 2, 2024
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
2 changes: 1 addition & 1 deletion bins/revm-test/src/bin/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
285 changes: 283 additions & 2 deletions crates/interpreter/src/instructions/bitwise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
Host, Interpreter,
};
use core::cmp::Ordering;
use revm_primitives::uint;

pub fn lt<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::VERYLOW);
Expand Down Expand Up @@ -103,19 +104,299 @@ pub fn sar<H: Host, SPEC: Spec>(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,
// value is <0, pushing -1
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),
Sign::Minus => two_compl(op2.wrapping_sub(ONE).wrapping_shr(shift).wrapping_add(ONE)),
}
};
}

#[cfg(test)]
mod tests {
use crate::instructions::bitwise::{sar, shl, shr};
use crate::{Contract, DummyHost, Interpreter};
use revm_primitives::{uint, Env, LatestSpec, U256};

#[test]
fn test_shift_left() {
let mut host = DummyHost::new(Env::default());
let mut interpreter = Interpreter::new(Contract::default(), u64::MAX, false);

struct TestCase {
value: U256,
shift: U256,
expected: U256,
}

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, test.value);
push!(interpreter, test.shift);
shl::<DummyHost, LatestSpec>(&mut interpreter, &mut host);
pop!(interpreter, res);
assert_eq!(res, test.expected);
}
}

#[test]
fn test_logical_shift_right() {
let mut host = DummyHost::new(Env::default());
let mut interpreter = Interpreter::new(Contract::default(), u64::MAX, false);

struct TestCase {
value: U256,
shift: U256,
expected: U256,
}

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, test.value);
push!(interpreter, test.shift);
shr::<DummyHost, LatestSpec>(&mut interpreter, &mut host);
pop!(interpreter, res);
assert_eq!(res, test.expected);
}
}

#[test]
fn test_arithmetic_shift_right() {
let mut host = DummyHost::new(Env::default());
let mut interpreter = Interpreter::new(Contract::default(), u64::MAX, false);

struct TestCase {
value: U256,
shift: U256,
expected: U256,
}

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: 0xc000000000000000000000000000000000000000000000000000000000000000_U256,
},
TestCase {
value: 0x8000000000000000000000000000000000000000000000000000000000000000_U256,
shift: 0xff_U256,
expected: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256,
},
TestCase {
value: 0x8000000000000000000000000000000000000000000000000000000000000000_U256,
shift: 0x0100_U256,
expected: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256,
},
TestCase {
value: 0x8000000000000000000000000000000000000000000000000000000000000000_U256,
shift: 0x0101_U256,
expected: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256,
},
TestCase {
value: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256,
shift: 0x00_U256,
expected: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256,
},
TestCase {
value: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256,
shift: 0x01_U256,
expected: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256,
},
TestCase {
value: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256,
shift: 0xff_U256,
expected: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256,
},
TestCase {
value: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256,
shift: 0x0100_U256,
expected: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256,
},
TestCase {
value: 0x0000000000000000000000000000000000000000000000000000000000000000_U256,
shift: 0x01_U256,
expected: 0x0000000000000000000000000000000000000000000000000000000000000000_U256,
},
TestCase {
value: 0x4000000000000000000000000000000000000000000000000000000000000000_U256,
shift: 0xfe_U256,
expected: 0x0000000000000000000000000000000000000000000000000000000000000001_U256,
},
TestCase {
value: 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256,
shift: 0xf8_U256,
expected: 0x000000000000000000000000000000000000000000000000000000000000007f_U256,
},
TestCase {
value: 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256,
shift: 0xfe_U256,
expected: 0x0000000000000000000000000000000000000000000000000000000000000001_U256,
},
TestCase {
value: 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256,
shift: 0xff_U256,
expected: 0x0000000000000000000000000000000000000000000000000000000000000000_U256,
},
TestCase {
value: 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256,
shift: 0x0100_U256,
expected: 0x0000000000000000000000000000000000000000000000000000000000000000_U256,
},
];
}

for test in test_cases {
host.clear();
push!(interpreter, test.value);
push!(interpreter, test.shift);
sar::<DummyHost, LatestSpec>(&mut interpreter, &mut host);
pop!(interpreter, res);
assert_eq!(res, test.expected);
}
}
}
Loading