Skip to content

Commit

Permalink
Merge branch 'main' into MLOAD-impl
Browse files Browse the repository at this point in the history
  • Loading branch information
khaeljy authored Sep 7, 2023
2 parents 82fbb5d + fe7d349 commit 262c0ef
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
15 changes: 15 additions & 0 deletions crates/evm/src/memory.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ trait MemoryTrait {

impl MemoryImpl of MemoryTrait {
/// Initializes a new `Memory` instance.
#[inline(always)]
fn new() -> Memory {
Memory { items: Default::default(), bytes_len: 0, }
}
Expand All @@ -42,6 +43,7 @@ impl MemoryImpl of MemoryTrait {
///
/// If the offset is aligned with the 16-bytes words in memory, the element is stored directly.
/// Otherwise, the element is split and stored in multiple words.
#[inline(always)]
fn store(ref self: Memory, element: u256, offset: usize) {
let new_min_bytes_len = helpers::ceil_bytes_len_to_next_32_bytes_word(offset + 32);

Expand Down Expand Up @@ -78,6 +80,7 @@ impl MemoryImpl of MemoryTrait {
/// * `self` - A mutable reference to the `Memory` instance to store the bytes in.
/// * `elements` - A span of bytes to store in memory.
/// * `offset` - The offset within memory to store the bytes at.
#[inline(always)]
fn store_n(ref self: Memory, elements: Span<u8>, offset: usize) {
if elements.len() == 0 {
return;
Expand Down Expand Up @@ -126,6 +129,7 @@ impl MemoryImpl of MemoryTrait {
/// Ensures that the memory is at least `length` bytes long. Expands if necessary.
/// # Returns
/// The gas cost of expanding the memory.
#[inline(always)]
fn ensure_length(ref self: Memory, length: usize) -> usize {
if self.bytes_len < length {
self.expand(length - self.bytes_len)
Expand All @@ -138,6 +142,7 @@ impl MemoryImpl of MemoryTrait {
/// # Returns
/// * `u256` - The loaded value.
/// * `usize` - The gas cost of expanding the memory.
#[inline(always)]
fn load(ref self: Memory, offset: usize) -> (u256, usize) {
let gas_cost = self.ensure_length(32 + offset);
let loaded_element = self.load_internal(offset);
Expand All @@ -147,6 +152,7 @@ impl MemoryImpl of MemoryTrait {
/// Expands memory if necessary, then load elements_len bytes from the memory at given offset inside elements.
/// # Returns
/// * `usize` - The gas cost of expanding the memory.
#[inline(always)]
fn load_n(
ref self: Memory, elements_len: usize, ref elements: Array<u8>, offset: usize
) -> usize {
Expand All @@ -171,6 +177,7 @@ impl InternalMemoryMethods of InternalMemoryTrait {
/// * `element` - The `u256` element to store in memory.
/// * `chunk_index` - The index of the memory chunk to start storing the element in.
/// * `offset_in_chunk` - The offset within the memory chunk to store the element at.
#[inline(always)]
fn store_element(ref self: Memory, element: u256, chunk_index: usize, offset_in_chunk: u32) {
let mask: u256 = helpers::pow256_rev(offset_in_chunk);
// explicit conversion to felt252 to compute the mask is way cheaper
Expand Down Expand Up @@ -213,6 +220,7 @@ impl InternalMemoryMethods of InternalMemoryTrait {
/// * `mask_i` - The mask for the high part of the word.
/// * `mask_f` - The mask for the low part of the word.
/// * `elements` - A span of bytes to store in memory.
#[inline(always)]
fn store_bytes_in_single_chunk(
ref self: Memory, initial_chunk: usize, mask_i: u256, mask_f: u256, elements: Span<u8>
) {
Expand Down Expand Up @@ -310,6 +318,7 @@ impl InternalMemoryMethods of InternalMemoryTrait {
/// # Returns
///
/// The `u256` element at the specified offset in the memory chunk.
#[inline(always)]
fn load_internal(ref self: Memory, offset: usize) -> u256 {
let (chunk_index, offset_in_chunk) = u32_safe_divmod(offset, u32_as_non_zero(16));

Expand Down Expand Up @@ -357,6 +366,7 @@ impl InternalMemoryMethods of InternalMemoryTrait {
/// * `elements_len` - The length of the array of bytes to load.
/// * `elements` - A reference to the array of bytes to load.
/// * `offset` - The chunk memory offset to load the bytes from.
#[inline(always)]
fn load_n_internal(
ref self: Memory, elements_len: usize, ref elements: Array<u8>, offset: usize
) {
Expand Down Expand Up @@ -412,6 +422,7 @@ impl InternalMemoryMethods of InternalMemoryTrait {
/// # Returns
///
/// The cost of the expansion.
#[inline(always)]
fn expand(ref self: Memory, length: usize) -> usize {
if (length == 0) {
return 0;
Expand Down Expand Up @@ -453,6 +464,7 @@ impl InternalMemoryMethods of InternalMemoryTrait {
/// # Panics
///
/// This function panics if the resulting word cannot be converted to a `u128` - which should never happen.
#[inline(always)]
fn store_first_word(
ref self: Memory,
chunk_index: usize,
Expand Down Expand Up @@ -484,6 +496,7 @@ impl InternalMemoryMethods of InternalMemoryTrait {
/// # Panics
///
/// This function panics if the resulting word cannot be converted to a `u128` - which should never happen.
#[inline(always)]
fn store_last_word(
ref self: Memory,
chunk_index: usize,
Expand All @@ -510,6 +523,7 @@ impl Felt252DictExtensionImpl of Felt252DictExtension {
/// * `self` - A mutable reference to the `Felt252Dict` instance.
/// * `element` - The element to store, of type `u256`.
/// * `index` - The `usize` index at which to store the element.
#[inline(always)]
fn store_u256(ref self: Felt252Dict<u128>, element: u256, index: usize) {
let index: felt252 = index.into();
self.insert(index, element.high.into());
Expand All @@ -527,6 +541,7 @@ impl Felt252DictExtensionImpl of Felt252DictExtension {
///
/// # Returns
/// * The element read, of type `u256`.
#[inline(always)]
fn read_u256(ref self: Felt252Dict<u128>, index: usize) -> u256 {
let index: felt252 = index.into();
let high: u128 = self.get(index);
Expand Down
6 changes: 6 additions & 0 deletions crates/evm/src/stack.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ trait NullableTraitExt<T> {
}

impl NullableTraitExtImpl of NullableTraitExt<u256> {
#[inline(always)]
fn new(value: u256) -> Nullable<u256> {
let nullable = nullable_from_box(BoxTrait::new(value));
nullable
Expand Down Expand Up @@ -65,6 +66,7 @@ impl StackImpl of StackTrait {

/// Pushes a new bytes32 word onto the stack.
/// If the stack is full, returns with a StackOverflow error.
#[inline(always)]
fn push(ref self: Stack, item: u256) -> Result<(), EVMError> {
// we can store at most 1024 256-bits words
if self.len() == constants::STACK_MAX_DEPTH {
Expand All @@ -77,6 +79,7 @@ impl StackImpl of StackTrait {

/// Pops the top item off the stack. If the stack is empty,
/// returns with a StackOverflow error.
#[inline(always)]
fn pop(ref self: Stack) -> Result<u256, EVMError> {
if self.len() == 0 {
return Result::Err(EVMError::StackError(STACK_UNDERFLOW));
Expand Down Expand Up @@ -106,6 +109,7 @@ impl StackImpl of StackTrait {

/// Peeks at the top item on the stack.
/// If the stack is empty, returns None.
#[inline(always)]
fn peek(ref self: Stack) -> Option<u256> {
if self.len() == 0 {
Option::None(())
Expand All @@ -119,6 +123,7 @@ impl StackImpl of StackTrait {
/// Peeks at the item at the given index on the stack.
/// index is 0-based, 0 being the top of the stack.
/// If the index is greather than the stack length, returns with a StackUnderflow error.
#[inline(always)]
fn peek_at(ref self: Stack, index: usize) -> Result<u256, EVMError> {
if index >= self.len() {
return Result::Err(EVMError::StackError(STACK_UNDERFLOW));
Expand All @@ -132,6 +137,7 @@ impl StackImpl of StackTrait {

/// Swaps the item at the given index with the item on top of the stack.
/// index is 0-based, 0 being the top of the stack (unallocated).
#[inline(always)]
fn swap_i(ref self: Stack, index: usize) -> Result<(), EVMError> {
if index >= self.len() {
return Result::Err(EVMError::StackError(STACK_UNDERFLOW));
Expand Down
2 changes: 1 addition & 1 deletion scripts/compare_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def get_previous_snapshot():

try:
# Fetch the list of workflow runs
cmd = f"curl -s -H 'Authorization: token {GITHUB_TOKEN}' -H 'Accept: application/vnd.github.v3+json' 'https://api.github.com/repos/{REPO}/actions/runs'"
cmd = f"curl -s -H 'Authorization: token {GITHUB_TOKEN}' -H 'Accept: application/vnd.github.v3+json' 'https://api.github.com/repos/{REPO}/actions/runs?branch=main&per_page=100'"
result = subprocess.check_output(cmd, shell=True)
runs = json.loads(result)

Expand Down

0 comments on commit 262c0ef

Please sign in to comment.