Skip to content

Commit

Permalink
fix: ec_precompiles input padding (#960)
Browse files Browse the repository at this point in the history
  • Loading branch information
enitrat authored Sep 18, 2024
1 parent 4b4139b commit a4442a6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
7 changes: 5 additions & 2 deletions crates/evm/src/precompiles/ec_add.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,9 +24,12 @@ pub impl EcAdd of Precompile {
0x6.try_into().unwrap()
}

fn exec(mut input: Span<u8>) -> Result<(u64, Span<u8>), EVMError> {
fn exec(input: Span<u8>) -> Result<(u64, Span<u8>), 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());

Expand Down
5 changes: 4 additions & 1 deletion crates/evm/src/precompiles/ec_mul.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -20,6 +20,9 @@ pub impl EcMul of Precompile {
fn exec(mut input: Span<u8>) -> Result<(u64, Span<u8>), 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());

Expand Down

0 comments on commit a4442a6

Please sign in to comment.