Skip to content
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
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ members = [
"tools/interactor-delegation-func-calls/",
"tools/interactor-governance-func-calls/",
"tools/gas-schedule-generator",
"tools/op-test-gen",
"tools/wat-gen",

"contracts/modules",
Expand Down
195 changes: 122 additions & 73 deletions chain/vm/src/host/vm_hooks/vh_handler/vh_managed_types/vh_big_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,77 +11,9 @@ use core::{
ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Neg, Rem, Shl, Shr, Sub},
};
use multiversx_chain_vm_executor::VMHooksEarlyExit;
use num_traits::{pow, sign::Signed};
use num_traits::{pow, sign::Signed, Zero};
use std::convert::TryInto;

macro_rules! binary_op_method {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These guys just got moved further below, no changes

($method_name:ident, $rust_op_name:ident, $gas_cost_field:ident) => {
pub fn $method_name(
&mut self,
dest: RawHandle,
x: RawHandle,
y: RawHandle,
) -> Result<(), VMHooksEarlyExit> {
self.use_gas(self.gas_schedule().big_int_api_cost.$gas_cost_field)?;

let bi_x = self.context.m_types_lock().bi_get(x);
let bi_y = self.context.m_types_lock().bi_get(y);
let result = bi_x.$rust_op_name(bi_y);
self.context.m_types_lock().bi_overwrite(dest, result);

Ok(())
}
};
}

macro_rules! binary_bitwise_op_method {
($method_name:ident, $rust_op_name:ident, $gas_cost_field:ident) => {
pub fn $method_name(
&mut self,
dest: RawHandle,
x: RawHandle,
y: RawHandle,
) -> Result<(), VMHooksEarlyExit> {
self.use_gas(self.gas_schedule().big_int_api_cost.$gas_cost_field)?;

let bi_x = self.context.m_types_lock().bi_get(x);
if bi_x.sign() == num_bigint::Sign::Minus {
return Err(early_exit_vm_error(
vm_err_msg::BIG_INT_BITWISE_OPERATION_NEGATIVE,
));
}
let bi_y = self.context.m_types_lock().bi_get(y);
if bi_y.sign() == num_bigint::Sign::Minus {
return Err(early_exit_vm_error(
vm_err_msg::BIG_INT_BITWISE_OPERATION_NEGATIVE,
));
}
let result = bi_x.$rust_op_name(bi_y);
self.context.m_types_lock().bi_overwrite(dest, result);

Ok(())
}
};
}

macro_rules! unary_op_method {
($method_name:ident, $rust_op_name:ident, $gas_cost_field:ident) => {
pub fn $method_name(
&mut self,
dest: RawHandle,
x: RawHandle,
) -> Result<(), VMHooksEarlyExit> {
self.use_gas(self.gas_schedule().big_int_api_cost.$gas_cost_field)?;

let bi_x = self.context.m_types_lock().bi_get(x);
let result = bi_x.$rust_op_name();
self.context.m_types_lock().bi_overwrite(dest, result);

Ok(())
}
};
}

/// Provides VM hook implementations for methods that deal big ints.
impl<C: VMHooksContext> VMHooksHandler<C> {
pub fn bi_new(&mut self, value: i64) -> Result<RawHandle, VMHooksEarlyExit> {
Expand Down Expand Up @@ -209,16 +141,101 @@ impl<C: VMHooksContext> VMHooksHandler<C> {
)),
}
}
}

macro_rules! binary_op_method {
($method_name:ident, $rust_op_name:ident, $gas_cost_field:ident) => {
pub fn $method_name(
&mut self,
dest: RawHandle,
x: RawHandle,
y: RawHandle,
) -> Result<(), VMHooksEarlyExit> {
self.use_gas(self.gas_schedule().big_int_api_cost.$gas_cost_field)?;

let bi_x = self.context.m_types_lock().bi_get(x);
let bi_y = self.context.m_types_lock().bi_get(y);
let result = bi_x.$rust_op_name(bi_y);
self.context.m_types_lock().bi_overwrite(dest, result);

Ok(())
}
};
}

impl<C: VMHooksContext> VMHooksHandler<C> {
binary_op_method! {bi_add, add, big_int_add}
binary_op_method! {bi_sub, sub, big_int_sub}
binary_op_method! {bi_mul, mul, big_int_mul}
binary_op_method! {bi_t_div, div, big_int_t_div}
binary_op_method! {bi_t_mod, rem, big_int_t_mod}

pub fn bi_t_div(
&mut self,
dest: RawHandle,
x: RawHandle,
y: RawHandle,
) -> Result<(), VMHooksEarlyExit> {
self.use_gas(self.gas_schedule().big_int_api_cost.big_int_t_div)?;

let bi_x = self.context.m_types_lock().bi_get(x);
let bi_y = self.context.m_types_lock().bi_get(y);

if bi_y.is_zero() {
return Err(early_exit_vm_error(vm_err_msg::DIVISION_BY_0));
}

let result = bi_x.div(bi_y);
self.context.m_types_lock().bi_overwrite(dest, result);

Ok(())
}

pub fn bi_t_mod(
&mut self,
dest: RawHandle,
x: RawHandle,
y: RawHandle,
) -> Result<(), VMHooksEarlyExit> {
self.use_gas(self.gas_schedule().big_int_api_cost.big_int_t_mod)?;

let bi_x = self.context.m_types_lock().bi_get(x);
let bi_y = self.context.m_types_lock().bi_get(y);

if bi_y.is_zero() {
return Err(early_exit_vm_error(vm_err_msg::DIVISION_BY_0));
}

let result = bi_x.rem(bi_y);
self.context.m_types_lock().bi_overwrite(dest, result);

Ok(())
}
}

macro_rules! unary_op_method {
($method_name:ident, $rust_op_name:ident, $gas_cost_field:ident) => {
pub fn $method_name(
&mut self,
dest: RawHandle,
x: RawHandle,
) -> Result<(), VMHooksEarlyExit> {
self.use_gas(self.gas_schedule().big_int_api_cost.$gas_cost_field)?;

let bi_x = self.context.m_types_lock().bi_get(x);
let result = bi_x.$rust_op_name();
self.context.m_types_lock().bi_overwrite(dest, result);

Ok(())
}
};
}

impl<C: VMHooksContext> VMHooksHandler<C> {
unary_op_method! {bi_abs, abs, big_int_abs}
unary_op_method! {bi_neg, neg, big_int_neg}
unary_op_method! {bi_sqrt, sqrt, big_int_sqrt}
}

impl<C: VMHooksContext> VMHooksHandler<C> {
pub fn bi_sign(&mut self, x: RawHandle) -> Result<i32, VMHooksEarlyExit> {
self.use_gas(self.gas_schedule().big_int_api_cost.big_int_sign)?;

Expand All @@ -242,8 +259,6 @@ impl<C: VMHooksContext> VMHooksHandler<C> {
}
}

unary_op_method! {bi_sqrt, sqrt, big_int_sqrt}

pub fn bi_pow(
&mut self,
dest: RawHandle,
Expand All @@ -267,11 +282,45 @@ impl<C: VMHooksContext> VMHooksHandler<C> {
let bi_x = self.context.m_types_lock().bi_get(x);
Ok(bi_x.bits() as i32 - 1)
}
}

macro_rules! binary_bitwise_op_method {
($method_name:ident, $rust_op_name:ident, $gas_cost_field:ident) => {
pub fn $method_name(
&mut self,
dest: RawHandle,
x: RawHandle,
y: RawHandle,
) -> Result<(), VMHooksEarlyExit> {
self.use_gas(self.gas_schedule().big_int_api_cost.$gas_cost_field)?;

let bi_x = self.context.m_types_lock().bi_get(x);
if bi_x.sign() == num_bigint::Sign::Minus {
return Err(early_exit_vm_error(
vm_err_msg::BIG_INT_BITWISE_OPERATION_NEGATIVE,
));
}
let bi_y = self.context.m_types_lock().bi_get(y);
if bi_y.sign() == num_bigint::Sign::Minus {
return Err(early_exit_vm_error(
vm_err_msg::BIG_INT_BITWISE_OPERATION_NEGATIVE,
));
}
let result = bi_x.$rust_op_name(bi_y);
self.context.m_types_lock().bi_overwrite(dest, result);

Ok(())
}
};
}

impl<C: VMHooksContext> VMHooksHandler<C> {
binary_bitwise_op_method! {bi_and, bitand, big_int_and}
binary_bitwise_op_method! {bi_or, bitor, big_int_or}
binary_bitwise_op_method! {bi_xor, bitxor, big_int_xor}
}

impl<C: VMHooksContext> VMHooksHandler<C> {
pub fn bi_shr(
&mut self,
dest: RawHandle,
Expand Down
Loading
Loading