Skip to content

Commit

Permalink
refactor(levm): simplify U256 conversion logic (#1392)
Browse files Browse the repository at this point in the history
**Motivation**

- This PR aims to reduce the logic in some opcodes.

**Description**

- Add a new function `u256_from_bool(value: bool)` to convert boolean
values into U256. Function return `U256::one()` if value is `True` and
`U256::zero()` if value is `False`.
- This change simplifies existing code by removing repetitive conversion
in `ISZERO`, `EQ`, `SGT`, `SLT`, `GT` and `LT`.

Closes [#1379](#1379)
  • Loading branch information
damiramirez authored Dec 3, 2024
1 parent 2944de1 commit 1c32950
Showing 1 changed file with 14 additions and 32 deletions.
46 changes: 14 additions & 32 deletions crates/vm/levm/src/opcode_handlers/bitwise_comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl VM {
self.increase_consumed_gas(current_call_frame, gas_cost::LT)?;
let lho = current_call_frame.stack.pop()?;
let rho = current_call_frame.stack.pop()?;
let result = if lho < rho { U256::one() } else { U256::zero() };
let result = u256_from_bool(lho < rho);
current_call_frame.stack.push(result)?;

Ok(OpcodeSuccess::Continue)
Expand All @@ -27,7 +27,7 @@ impl VM {
self.increase_consumed_gas(current_call_frame, gas_cost::GT)?;
let lho = current_call_frame.stack.pop()?;
let rho = current_call_frame.stack.pop()?;
let result = if lho > rho { U256::one() } else { U256::zero() };
let result = u256_from_bool(lho > rho);
current_call_frame.stack.push(result)?;

Ok(OpcodeSuccess::Continue)
Expand All @@ -42,18 +42,10 @@ impl VM {
let rho_is_negative = rho.bit(255);
let result = if lho_is_negative == rho_is_negative {
// Compare magnitudes if signs are the same
if lho < rho {
U256::one()
} else {
U256::zero()
}
u256_from_bool(lho < rho)
} else {
// Negative is smaller if signs differ
if lho_is_negative {
U256::one()
} else {
U256::zero()
}
u256_from_bool(lho_is_negative)
};
current_call_frame.stack.push(result)?;

Expand All @@ -69,18 +61,10 @@ impl VM {
let rho_is_negative = rho.bit(255);
let result = if lho_is_negative == rho_is_negative {
// Compare magnitudes if signs are the same
if lho > rho {
U256::one()
} else {
U256::zero()
}
u256_from_bool(lho > rho)
} else {
// Positive is bigger if signs differ
if rho_is_negative {
U256::one()
} else {
U256::zero()
}
u256_from_bool(rho_is_negative)
};
current_call_frame.stack.push(result)?;

Expand All @@ -92,11 +76,8 @@ impl VM {
self.increase_consumed_gas(current_call_frame, gas_cost::EQ)?;
let lho = current_call_frame.stack.pop()?;
let rho = current_call_frame.stack.pop()?;
let result = if lho == rho {
U256::one()
} else {
U256::zero()
};
let result = u256_from_bool(lho == rho);

current_call_frame.stack.push(result)?;

Ok(OpcodeSuccess::Continue)
Expand All @@ -110,11 +91,8 @@ impl VM {
self.increase_consumed_gas(current_call_frame, gas_cost::ISZERO)?;

let operand = current_call_frame.stack.pop()?;
let result = if operand == U256::zero() {
U256::one()
} else {
U256::zero()
};
let result = u256_from_bool(operand.is_zero());

current_call_frame.stack.push(result)?;

Ok(OpcodeSuccess::Continue)
Expand Down Expand Up @@ -321,3 +299,7 @@ fn checked_shift_right(value: U256, shift: U256) -> Result<U256, VMError> {

Ok(result)
}

fn u256_from_bool(value: bool) -> U256 {
U256::from(u8::from(value))
}

0 comments on commit 1c32950

Please sign in to comment.