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

Dev: replace all *_div_rem by DivRem::div_rem #381

Merged
merged 2 commits into from
Oct 2, 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
13 changes: 6 additions & 7 deletions crates/evm/src/memory.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use integer::{
u32_safe_divmod, u32_as_non_zero, u128_safe_divmod, u128_as_non_zero, u256_safe_div_rem,
u256_as_non_zero
u32_safe_divmod, u32_as_non_zero, u128_safe_divmod, u128_as_non_zero, u256_as_non_zero
};
use utils::constants::{
POW_256_0_U128, POW_256_1_U128, POW_256_2_U128, POW_256_3_U128, POW_256_4_U128, POW_256_5_U128,
Expand Down Expand Up @@ -296,8 +295,8 @@ impl InternalMemoryMethods of InternalMemoryTrait {
let mask_c: u256 = POW_256_16_U256 / mask;

// Split the 2 input bytes16 chunks at offset_in_chunk.
let (el_hh, el_hl) = u256_safe_div_rem(element.high.into(), u256_as_non_zero(mask_c));
let (el_lh, el_ll) = u256_safe_div_rem(element.low.into(), u256_as_non_zero(mask_c));
let (el_hh, el_hl) = DivRem::div_rem(element.high.into(), u256_as_non_zero(mask_c));
let (el_lh, el_ll) = DivRem::div_rem(element.low.into(), u256_as_non_zero(mask_c));

// Read the words at chunk_index, chunk_index + 2.
let w0: u128 = self.items.get(chunk_index.into());
Expand Down Expand Up @@ -336,8 +335,8 @@ impl InternalMemoryMethods of InternalMemoryTrait {
ref self: Memory, initial_chunk: usize, mask_i: u256, mask_f: u256, elements: Span<u8>
) {
let word: u128 = self.items.get(initial_chunk.into());
let (word_high, word_low) = u256_safe_div_rem(word.into(), u256_as_non_zero(mask_i));
let (_, word_low_l) = u256_safe_div_rem(word_low, u256_as_non_zero(mask_f));
let (word_high, word_low) = DivRem::div_rem(word.into(), u256_as_non_zero(mask_i));
let (_, word_low_l) = DivRem::div_rem(word_low, u256_as_non_zero(mask_f));
let bytes_as_word = helpers::load_word(elements.len(), elements);
let new_w: u128 = (word_high * mask_i + bytes_as_word.into() * mask_f + word_low_l)
.try_into()
Expand Down Expand Up @@ -461,7 +460,7 @@ impl InternalMemoryMethods of InternalMemoryTrait {

// Compute element words
let w0_l: u256 = w0.into() % mask;
let (w1_h, w1_l): (u256, u256) = u256_safe_div_rem(w1.into(), u256_as_non_zero(mask));
let (w1_h, w1_l): (u256, u256) = DivRem::div_rem(w1.into(), u256_as_non_zero(mask));
let w2_h: u256 = w2.into() / mask;
let el_h: u128 = (w0_l * mask_c + w1_h).try_into().unwrap();
let el_l: u128 = (w1_l * mask_c + w2_h).try_into().unwrap();
Expand Down
4 changes: 2 additions & 2 deletions crates/utils/src/i256.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use utils::constants::POW_2_127;
use utils::math::{Bitshift, Exponentiation};
use integer::{u256_try_as_non_zero, u256_safe_div_rem, BoundedInt};
use integer::{u256_try_as_non_zero, BoundedInt};

#[derive(Copy, Drop, PartialEq)]
struct i256 {
Expand Down Expand Up @@ -107,7 +107,7 @@ fn i256_signed_div_rem(a: i256, div: NonZero<u256>) -> (i256, i256) {

// Compute the quotient and remainder.
// Can't panic as zero case is handled in the first instruction
let (quot, rem) = u256_safe_div_rem(a.value, div.value.try_into().unwrap());
let (quot, rem) = DivRem::div_rem(a.value, div.value.try_into().unwrap());

// Restore remainder sign.
let rem = if a_positive {
Expand Down