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

Standardization #2

Merged
merged 3 commits into from
Jul 18, 2023
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
102 changes: 32 additions & 70 deletions src/core_module/op_codes/arithmetic/signed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,96 +9,58 @@ use ethers::types::U256;
// Colored output
use colored::*;

/// Divides the second item on the stack by the topmost item, treating both values as signed.
/// If the divisor is zero, the result is zero.
///
/// # Example
///
/// ```
/// use bytecode_rs::core_module::op_codes::math::unsigned::sdiv;
/// use bytecode_rs::core_module::runner::Runner;
///
/// let mut runner = Runner::new([0xaa; 20], None, None, None);
/// runner.stack.push([0x01, 0x00, 0x00, 0x00].to_vec()).unwrap();
/// runner.stack.push([0x00, 0x00, 0x00, 0x00].to_vec()).unwrap();
///
/// sdiv(&mut runner).unwrap();
///
/// assert_eq!(runner.stack.pop().unwrap(), [0x00, 0x00, 0x00, 0x00].to_vec());
/// ```
pub fn sdiv(runner: &mut Runner) -> Result<(), ExecutionError> {
unsafe {
let pop1 = runner.stack.pop()?;
let pop2 = runner.stack.pop()?;
let pop1 = runner.stack.pop()?;
let pop2 = runner.stack.pop()?;

let a = I256::from_raw(U256::from_big_endian(&pop1));
let b = I256::from_raw(U256::from_big_endian(&pop2));
let a = I256::from_raw(U256::from_big_endian(&pop1));
let b = I256::from_raw(U256::from_big_endian(&pop2));

let result = a.checked_div(b);
let result = a.checked_div(b);

let mut result_bytes = [0u8; 32];
result
.unwrap_or(I256::from(0))
.to_big_endian(&mut result_bytes);
let mut result_bytes = [0u8; 32];
result
.unwrap_or(I256::from(0))
.to_big_endian(&mut result_bytes);

let result = runner.stack.push(result_bytes);
let result = runner.stack.push(result_bytes);

if result.is_err() {
return Err(result.unwrap_err());
}
if result.is_err() {
return Err(result.unwrap_err());
}

if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 {
let hex: String = utils::debug::to_hex_string(result_bytes);
runner.print_debug(&format!("{:<14} 👉 [ {} ]", "SDIV".bright_blue(), hex));
}
if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 {
let hex: String = utils::debug::to_hex_string(result_bytes);
runner.print_debug(&format!("{:<14} 👉 [ {} ]", "SDIV".bright_blue(), hex));
}

// Increment PC
runner.increment_pc(1)
}

/// Computes the remainder of the signed division of the two topmost items on the stack.
/// If the divisor is zero, the result is zero.
///
/// # Example
///
/// ```
/// use bytecode_rs::core_module::op_codes::math::unsigned::smodulo;
/// use bytecode_rs::core_module::runner::Runner;
///
/// let mut runner = Runner::new([0xaa; 20], None, None, None);
/// runner.stack.push([0x01, 0x00, 0x00, 0x00].to_vec()).unwrap();
/// runner.stack.push([0x00, 0x00, 0x00, 0x00].to_vec()).unwrap();
///
/// smodulo(&mut runner).unwrap();
///
/// assert_eq!(runner.stack.pop().unwrap(), [0x00, 0x00, 0x00, 0x00].to_vec());
/// ```
pub fn smodulo(runner: &mut Runner) -> Result<(), ExecutionError> {
unsafe {
let pop1 = runner.stack.pop()?;
let pop2 = runner.stack.pop()?;
let pop1 = runner.stack.pop()?;
let pop2 = runner.stack.pop()?;

let a = I256::from_raw(U256::from_big_endian(&pop1));
let b = I256::from_raw(U256::from_big_endian(&pop2));
let a = I256::from_raw(U256::from_big_endian(&pop1));
let b = I256::from_raw(U256::from_big_endian(&pop2));

let result = a.checked_rem(b);
let result = a.checked_rem(b);

let mut result_bytes = [0u8; 32];
result
.unwrap_or(I256::from(0))
.to_big_endian(&mut result_bytes);
let mut result_bytes = [0u8; 32];
result
.unwrap_or(I256::from(0))
.to_big_endian(&mut result_bytes);

let result = runner.stack.push(result_bytes);
let result = runner.stack.push(result_bytes);

if result.is_err() {
return Err(result.unwrap_err());
}
if result.is_err() {
return Err(result.unwrap_err());
}

if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 {
let hex: String = utils::debug::to_hex_string(result_bytes);
runner.print_debug(&format!("{:<14} 👉 [ {} ]", "SMOD".bright_blue(), hex));
}
if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 {
let hex: String = utils::debug::to_hex_string(result_bytes);
runner.print_debug(&format!("{:<14} 👉 [ {} ]", "SMOD".bright_blue(), hex));
}

// Increment PC
Expand Down
Loading