Skip to content

Commit

Permalink
unwrap and nits
Browse files Browse the repository at this point in the history
  • Loading branch information
lordshashank committed Sep 28, 2024
1 parent e17ecd9 commit dd8a4f4
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
9 changes: 5 additions & 4 deletions crates/evm/src/instructions/system_operations.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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);
}
Expand Down
3 changes: 0 additions & 3 deletions crates/evm/src/memory.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
21 changes: 4 additions & 17 deletions crates/evm/src/precompiles/ec_operations/ec_add.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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};


Expand All @@ -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) },
Expand Down
16 changes: 3 additions & 13 deletions crates/evm/src/precompiles/ec_operations/ec_mul.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) },
Expand Down

0 comments on commit dd8a4f4

Please sign in to comment.