diff --git a/crates/evm/src/precompiles/ec_add.cairo b/crates/evm/src/precompiles/ec_add.cairo index 58c49037b..ff604c8b5 100644 --- a/crates/evm/src/precompiles/ec_add.cairo +++ b/crates/evm/src/precompiles/ec_add.cairo @@ -12,7 +12,7 @@ use evm::errors::EVMError; use evm::precompiles::Precompile; use garaga::core::circuit::AddInputResultTrait2; use utils::helpers::ToBytes; -use utils::helpers::load_word; +use utils::helpers::{load_word, U8SpanExTrait}; const BASE_COST: u64 = 150; @@ -24,9 +24,12 @@ pub impl EcAdd of Precompile { 0x6.try_into().unwrap() } - fn exec(mut input: Span) -> Result<(u64, Span), EVMError> { + fn exec(input: Span) -> Result<(u64, Span), EVMError> { let gas = BASE_COST; + // Pad the input to 128 bytes to avoid out-of-bounds accesses + let mut input = input.pad_right_with_zeroes(128); + let x1_bytes = *(input.multi_pop_front::<32>().unwrap()); let x1: u256 = load_word(U256_BYTES_LEN, x1_bytes.unbox().span()); diff --git a/crates/evm/src/precompiles/ec_mul.cairo b/crates/evm/src/precompiles/ec_mul.cairo index 3adbddbd2..955d77fbc 100644 --- a/crates/evm/src/precompiles/ec_mul.cairo +++ b/crates/evm/src/precompiles/ec_mul.cairo @@ -5,7 +5,7 @@ use evm::errors::EVMError; use evm::precompiles::Precompile; use evm::precompiles::ec_add::{is_on_curve, double_ec_point_unchecked, ec_safe_add}; -use utils::helpers::{load_word, ToBytes}; +use utils::helpers::{load_word, ToBytes, U8SpanExTrait}; // const BN254_ORDER: u256 = 0x30644E72E131A029B85045B68181585D2833E84879B9709143E1F593F0000001; @@ -20,6 +20,9 @@ pub impl EcMul of Precompile { fn exec(mut input: Span) -> Result<(u64, Span), EVMError> { let gas = BASE_COST; + // Pad the input to 128 bytes to avoid out-of-bounds accesses + let mut input = input.pad_right_with_zeroes(96); + let x1_bytes = *(input.multi_pop_front::<32>().unwrap()); let x1: u256 = load_word(U256_BYTES_LEN, x1_bytes.unbox().span());