Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
enitrat committed Jan 3, 2025
1 parent 7d7039d commit 5e4b226
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 18 deletions.
1 change: 1 addition & 0 deletions cairo/ethereum/cancun/vm.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ namespace EvmImpl {
code=evm.value.code,
gas_left=evm.value.gas_left,
env=evm.value.env,
valid_jump_destinations=evm.value.valid_jump_destinations,
logs=evm.value.logs,
refund_counter=evm.value.refund_counter,
running=evm.value.running,
Expand Down
16 changes: 10 additions & 6 deletions cairo/ethereum/cancun/vm/gas.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,12 @@ func charge_gas{range_check_ptr, evm: Evm}(amount: Uint) -> ExceptionalHalt* {
return err;
}

// @dev: assumption: not called with size_in_bytes > 2**64
// only used by calculate_gas_extend_memory
const MAX_MEMORY_COST = 0x20000000000017f7fffffffffffd;
const MAX_MEMORY_SIZE = 2 ** 64 - 32;

// @dev: assumption: not called with size_in_bytes >= 2**64
// only used by calculate_gas_extend_memory which saturates at 2**64-32
// @dev: max output value given this saturation is MAX_MEMORY_COST
func calculate_memory_gas_cost{range_check_ptr}(size_in_bytes: Uint) -> Uint {
let size = ceil32(size_in_bytes);
let (size_in_words, _) = divmod(size.value, 32);
Expand All @@ -146,7 +150,7 @@ func calculate_memory_gas_cost{range_check_ptr}(size_in_bytes: Uint) -> Uint {
return total_gas_cost;
}

// @dev: saturates extensions at 2**64 (Uint size)
// @dev: saturates extensions at (MAX_MEMORY_SIZE, MAX_MEMORY_COST)
func calculate_gas_extend_memory{range_check_ptr}(
memory: Memory, extensions: ListTupleU256U256
) -> ExtendMemory {
Expand Down Expand Up @@ -179,12 +183,12 @@ func _max_offset{range_check_ptr}(

let (max_offset, carry) = uint256_add([offset.value], [size.value]);
if (carry != 0) {
tempvar res = Uint(Constants.UINT64_MAX);
tempvar res = Uint(MAX_MEMORY_SIZE);
return _max_offset(res, extensions, idx + 1);
}
let (is_saturated) = uint256_le(max_offset, U256Struct(Constants.UINT64_MAX, 0));
let (is_saturated) = uint256_le(U256Struct(MAX_MEMORY_SIZE + 1, 0), max_offset);
if (is_saturated != 0) {
tempvar res = Uint(Constants.UINT64_MAX);
tempvar res = Uint(MAX_MEMORY_SIZE);
return _max_offset(res, extensions, idx + 1);
}

Expand Down
1 change: 1 addition & 0 deletions cairo/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def pytest_addoption(parser):
max_examples=30,
phases=[Phase.explicit, Phase.reuse, Phase.generate, Phase.target],
derandomize=True,
print_blob=True,
)
settings.register_profile(
"debug",
Expand Down
21 changes: 11 additions & 10 deletions cairo/tests/ethereum/cancun/vm/test_gas.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from ethereum_types.numeric import U256, Uint
from hypothesis import assume, given
from hypothesis import strategies as st
from hypothesis.strategies import composite

from ethereum.cancun.blocks import Header
from ethereum.cancun.transactions import BlobTransaction
Expand All @@ -24,6 +25,14 @@
from tests.utils.args_gen import Evm, Memory


@composite
def extensions_strategy(draw):
offset = draw(st.integers(min_value=0, max_value=2**64 - 32))
max_size = (2**64 - 32) - offset
size = draw(st.integers(min_value=0, max_value=max_size))
return (U256(offset), U256(size))


class TestGas:
@given(evm=..., amount=...)
def test_charge_gas(self, cairo_run, evm: Evm, amount: Uint):
Expand All @@ -43,16 +52,8 @@ def test_calculate_memory_gas_cost(self, cairo_run, size_in_bytes: Uint):
"calculate_memory_gas_cost", size_in_bytes
)

# Saturate the memory (offsets + size) at 2**64-1
@given(
memory=...,
extensions=st.lists(
st.tuples(
st.integers(min_value=0, max_value=2**64 - 1).map(U256),
st.integers(min_value=0, max_value=2**64 - 1).map(U256),
)
),
)
# We saturate the memory (offsets + size) at 2**64-32
@given(memory=..., extensions=st.lists(extensions_strategy()))
def test_calculate_gas_extend_memory(
self, cairo_run, memory: Memory, extensions: List[Tuple[U256, U256]]
):
Expand Down
13 changes: 11 additions & 2 deletions cairo/tests/utils/strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,12 @@ def tuple_strategy(thing):
# Generating up to 2**13 bytes of memory is enough for most tests as more would take too long
# in the test runner.
# 2**32 bytes would be the value at which the memory expansion would trigger an OOG
memory = st.binary(min_size=0, max_size=2**13).map(Memory)
# memory size must be a multiple of 32
memory = (
st.binary(min_size=0, max_size=2**13)
.map(lambda x: x + b"\x00" * ((32 - len(x) % 32) % 32))
.map(Memory)
)

evm = st.fixed_dictionaries(
{
Expand Down Expand Up @@ -171,7 +176,11 @@ def tuple_strategy(thing):

# Versions strategies with less data in collections

memory_lite = st.binary(min_size=0, max_size=128).map(Memory)
memory_lite = (
st.binary(min_size=0, max_size=128)
.map(lambda x: x + b"\x00" * ((32 - len(x) % 32) % 32))
.map(Memory)
)

message_lite = st.fixed_dictionaries(
{
Expand Down

0 comments on commit 5e4b226

Please sign in to comment.