From a27a0c862c5c7cfed61ccd1191615b9d20a01475 Mon Sep 17 00:00:00 2001 From: lordforever Date: Sat, 28 Sep 2024 22:36:20 +0530 Subject: [PATCH] unwrap and nits --- .../environmental_information.cairo | 1 - .../src/instructions/system_operations.cairo | 9 ++++---- crates/evm/src/memory.cairo | 3 --- .../precompiles/ec_operations/ec_add.cairo | 21 ++++--------------- .../precompiles/ec_operations/ec_mul.cairo | 16 +++----------- 5 files changed, 12 insertions(+), 38 deletions(-) diff --git a/crates/evm/src/instructions/environmental_information.cairo b/crates/evm/src/instructions/environmental_information.cairo index 59b40ed83..d675193ff 100644 --- a/crates/evm/src/instructions/environmental_information.cairo +++ b/crates/evm/src/instructions/environmental_information.cairo @@ -86,7 +86,6 @@ pub impl EnvironmentInformationImpl of EnvironmentInformationTrait { let bytes_len = core::cmp::min(32, calldata_len - offset); let sliced = calldata.slice(offset, bytes_len); - // Use from_be_bytes_partial to load the data let mut data_to_load: u256 = sliced .from_be_bytes_partial() .expect('Failed to parse calldata'); diff --git a/crates/evm/src/instructions/system_operations.cairo b/crates/evm/src/instructions/system_operations.cairo index 936a338d5..8d252e182 100644 --- a/crates/evm/src/instructions/system_operations.cairo +++ b/crates/evm/src/instructions/system_operations.cairo @@ -387,7 +387,6 @@ mod tests { use snforge_std::{test_address, start_mock_call}; use utils::constants::EMPTY_KECCAK; use utils::helpers::compute_starknet_address; - use utils::helpers::load_word; use utils::traits::bytes::{U8SpanExTrait, FromBytes}; use utils::traits::{EthAddressIntoU256}; @@ -404,9 +403,11 @@ mod tests { vm.stack.push(32).expect('push failed'); vm.stack.push(0).expect('push failed'); + assert(vm.exec_return().is_ok(), 'Exec return failed'); + let return_data = vm.return_data(); let parsed_return_data: u256 = return_data - .from_be_bytes_partial() + .from_be_bytes() .expect('Failed to parse return data'); assert(1000 == parsed_return_data, 'Wrong return_data'); assert(!vm.is_running(), 'vm should be stopped'); @@ -428,7 +429,7 @@ mod tests { let return_data = vm.return_data(); let parsed_return_data: u256 = return_data - .from_be_bytes_partial() + .from_be_bytes() .expect('Failed to parse return data'); assert(1000 == parsed_return_data, 'Wrong return_data'); assert(!vm.is_running(), 'vm should be stopped'); @@ -451,7 +452,7 @@ mod tests { let parsed_return_data: u256 = return_data .from_be_bytes_partial() .expect('Failed to parse return data'); - assert(1000 == parsed_return_data, 'Wrong return_data'); + assert(256 == parsed_return_data, 'Wrong return_data'); assert(!vm.is_running(), 'vm should be stopped'); assert_eq!(vm.error, false); } diff --git a/crates/evm/src/memory.cairo b/crates/evm/src/memory.cairo index 942ffca74..9659045c8 100644 --- a/crates/evm/src/memory.cairo +++ b/crates/evm/src/memory.cairo @@ -550,10 +550,8 @@ pub(crate) impl InternalMemoryMethods of InternalMemoryTrait { let word = self.items.get(chunk_index.into()); let word_high = (word.into() / start_mask); - // Calculate the number of bytes to read let bytes_to_read = 16 - start_offset_in_chunk; - // Slice the elements and convert to u128 using from_be_bytes_partial let word_low: u128 = elements .slice(0, bytes_to_read) .from_be_bytes_partial() @@ -592,7 +590,6 @@ pub(crate) impl InternalMemoryMethods of InternalMemoryTrait { let word = self.items.get(chunk_index.into()); let word_low = (word.into() % end_mask); - // Convert the elements to u128 using from_be_bytes_partial let low_bytes: u128 = elements .slice(0, end_offset_in_chunk) .from_be_bytes_partial() diff --git a/crates/evm/src/precompiles/ec_operations/ec_add.cairo b/crates/evm/src/precompiles/ec_operations/ec_add.cairo index 91e08cb3d..1d8b9422e 100644 --- a/crates/evm/src/precompiles/ec_operations/ec_add.cairo +++ b/crates/evm/src/precompiles/ec_operations/ec_add.cairo @@ -13,7 +13,6 @@ use crate::precompiles::ec_operations::{ eq_mod_p, eq_neg_mod_p, is_on_curve, double_ec_point_unchecked, BN254_PRIME_LIMBS, BN254_PRIME }; use garaga::core::circuit::AddInputResultTrait2; -// use utils::helpers::{load_word}; use utils::traits::bytes::{ToBytes, U8SpanExTrait, FromBytes}; @@ -31,25 +30,13 @@ pub impl EcAdd of Precompile { // Pad the input to 128 bytes to avoid out-of-bounds accesses let mut input = input.pad_right_with_zeroes(128); - let x1: u256 = match input.slice(0, 32).from_be_bytes() { - Option::Some(x1) => x1, - Option::None => { return Result::Ok((gas, [].span())); } - }; + let x1: u256 = input.slice(0, 32).from_be_bytes().unwrap(); - let y1: u256 = match input.slice(32, 32).from_be_bytes() { - Option::Some(y1) => y1, - Option::None => { return Result::Ok((gas, [].span())); } - }; + let y1: u256 = input.slice(32, 32).from_be_bytes().unwrap(); - let x2: u256 = match input.slice(64, 32).from_be_bytes() { - Option::Some(x2) => x2, - Option::None => { return Result::Ok((gas, [].span())); } - }; + let x2: u256 = input.slice(64, 32).from_be_bytes().unwrap(); - let y2: u256 = match input.slice(96, 32).from_be_bytes() { - Option::Some(y2) => y2, - Option::None => { return Result::Ok((gas, [].span())); } - }; + let y2: u256 = input.slice(96, 32).from_be_bytes().unwrap(); let (x, y) = match ec_add(x1, y1, x2, y2) { Option::Some((x, y)) => { (x, y) }, diff --git a/crates/evm/src/precompiles/ec_operations/ec_mul.cairo b/crates/evm/src/precompiles/ec_operations/ec_mul.cairo index a55f3188f..bfb60a7e5 100644 --- a/crates/evm/src/precompiles/ec_operations/ec_mul.cairo +++ b/crates/evm/src/precompiles/ec_operations/ec_mul.cairo @@ -6,7 +6,6 @@ use crate::errors::EVMError; use crate::precompiles::Precompile; use crate::precompiles::ec_operations::ec_add::ec_safe_add; use crate::precompiles::ec_operations::{is_on_curve, double_ec_point_unchecked, BN254_PRIME}; -// use utils::helpers::{load_word}; use utils::traits::bytes::{ToBytes, U8SpanExTrait, FromBytes}; const BASE_COST: u64 = 6000; @@ -23,20 +22,11 @@ pub impl EcMul of Precompile { // Pad the input to 128 bytes to avoid out-of-bounds accesses let mut input = input.pad_right_with_zeroes(96); - let x1: u256 = match input.slice(0, 32).from_be_bytes() { - Option::Some(x1) => x1, - Option::None => { return Result::Ok((gas, [].span())); } - }; + let x1: u256 = input.slice(0, 32).from_be_bytes().unwrap(); - let y1: u256 = match input.slice(32, 32).from_be_bytes() { - Option::Some(y1) => y1, - Option::None => { return Result::Ok((gas, [].span())); } - }; + let y1: u256 = input.slice(32, 32).from_be_bytes().unwrap(); - let s: u256 = match input.slice(64, 32).from_be_bytes() { - Option::Some(s) => s, - Option::None => { return Result::Ok((gas, [].span())); } - }; + let s: u256 = input.slice(64, 32).from_be_bytes().unwrap(); let (x, y) = match ec_mul(x1, y1, s) { Option::Some((x, y)) => { (x, y) },