Skip to content

Commit

Permalink
Merge branch 'main' into CODESIZE
Browse files Browse the repository at this point in the history
  • Loading branch information
khaeljy committed Aug 29, 2023
2 parents 1b7a7d2 + 36c3f37 commit 03d48b9
Show file tree
Hide file tree
Showing 10 changed files with 192 additions and 109 deletions.
6 changes: 5 additions & 1 deletion crates/evm/src/instructions/block_information.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use traits::Into;
use evm::context::{ExecutionContext, ExecutionContextTrait, BoxDynamicExecutionContextDestruct};
use evm::stack::StackTrait;
use evm::errors::EVMError;
use utils::constants::CHAIN_ID;

#[generate_trait]
impl BlockInformation of BlockInformationTrait {
Expand Down Expand Up @@ -57,7 +58,10 @@ impl BlockInformation of BlockInformationTrait {
/// Get the chain ID.
/// # Specification: https://www.evm.codes/#46?fork=shanghai
fn exec_chainid(ref self: ExecutionContext) -> Result<(), EVMError> {
Result::Ok(())
// CHAIN_ID = KKRT (0x4b4b5254) in ASCII
// TODO: Replace the hardcoded value by a value set in kakarot main contract constructor
// Push the chain ID to stack
self.stack.push(CHAIN_ID)
}

/// 0x47 - SELFBALANCE
Expand Down
25 changes: 14 additions & 11 deletions crates/evm/src/instructions/comparison_operations.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ impl ComparisonAndBitwiseOperations of ComparisonAndBitwiseOperationsTrait {
/// 0x11 - GT
/// # Specification: https://www.evm.codes/#11?fork=shanghai
fn exec_gt(ref self: ExecutionContext) -> Result<(), EVMError> {
Result::Ok(())
let popped = self.stack.pop_n(2)?;
let a = *popped[0];
let b = *popped[1];
let result = if (a > b) {
1
} else {
0
};
self.stack.push(result)
}


Expand Down Expand Up @@ -56,8 +64,7 @@ impl ComparisonAndBitwiseOperations of ComparisonAndBitwiseOperationsTrait {
let a = *popped[0];
let b = *popped[1];
let result = a & b;
self.stack.push(result)?;
Result::Ok(())
self.stack.push(result)
}

/// 0x17 - OR
Expand All @@ -73,8 +80,7 @@ impl ComparisonAndBitwiseOperations of ComparisonAndBitwiseOperationsTrait {
let a = *popped[0];
let b = *popped[1];
let result = a ^ b;
self.stack.push(result)?;
Result::Ok(())
self.stack.push(result)
}

/// 0x19 - NOT
Expand All @@ -83,8 +89,7 @@ impl ComparisonAndBitwiseOperations of ComparisonAndBitwiseOperationsTrait {
fn exec_not(ref self: ExecutionContext) -> Result<(), EVMError> {
let a = self.stack.pop()?;
let result = ~a;
self.stack.push(result)?;
Result::Ok(())
self.stack.push(result)
}

/// 0x1A - BYTE
Expand All @@ -97,14 +102,12 @@ impl ComparisonAndBitwiseOperations of ComparisonAndBitwiseOperationsTrait {

/// If the byte offset is out of range, we early return with 0.
if i > 31 {
self.stack.push(0)?;
return Result::Ok(());
return self.stack.push(0);
}

// Right shift value by offset bits and then take the least significant byte by applying modulo 256.
let result = (x / 2.pow((31 - i) * 8)) % 256;
self.stack.push(result)?;
Result::Ok(())
self.stack.push(result)
}

/// 0x1B - SHL
Expand Down
3 changes: 1 addition & 2 deletions crates/evm/src/instructions/duplication_operations.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ mod internal {
let i: u8 = i.into();

let item = context.stack.peek_at((i - 1).into())?;
context.stack.push(item)?;
Result::Ok(())
context.stack.push(item)
}
}

Expand Down
10 changes: 5 additions & 5 deletions crates/evm/src/instructions/environmental_information.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
use starknet::{EthAddressIntoFelt252};
use result::ResultTrait;
use evm::stack::StackTrait;
use evm::context::ExecutionContext;
use evm::context::ExecutionContextTrait;
use evm::context::CallContextTrait;
use evm::context::BoxDynamicExecutionContextDestruct;
use evm::context::{
ExecutionContext, ExecutionContextTrait, BoxDynamicExecutionContextDestruct, CallContextTrait
};
use evm::errors::EVMError;
use utils::helpers::EthAddressIntoU256;

Expand Down Expand Up @@ -57,7 +56,8 @@ impl EnvironmentInformationImpl of EnvironmentInformationTrait {
/// Get the size of return data.
/// # Specification: https://www.evm.codes/#36?fork=shanghai
fn exec_calldatasize(ref self: ExecutionContext) -> Result<(), EVMError> {
Result::Ok(())
let result: u256 = self.call_context().call_data().len().into();
self.stack.push(result)
}

/// 0x37 - CALLDATACOPY operation
Expand Down
33 changes: 11 additions & 22 deletions crates/evm/src/instructions/stop_and_arithmetic_operations.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ impl StopAndArithmeticOperations of StopAndArithmeticOperationsTrait {
// Compute the addition
let (result, _) = u256_overflowing_add(*popped[0], *popped[1]);

self.stack.push(result)?;
Result::Ok(())
self.stack.push(result)
}

/// 0x02 - MUL
Expand All @@ -51,8 +50,7 @@ impl StopAndArithmeticOperations of StopAndArithmeticOperationsTrait {
// Compute the multiplication
let (result, _) = u256_overflow_mul(*popped[0], *popped[1]);

self.stack.push(result)?;
Result::Ok(())
self.stack.push(result)
}

/// 0x03 - SUB
Expand All @@ -65,8 +63,7 @@ impl StopAndArithmeticOperations of StopAndArithmeticOperationsTrait {
// Compute the substraction
let (result, _) = u256_overflow_sub(*popped[0], *popped[1]);

self.stack.push(result)?;
Result::Ok(())
self.stack.push(result)
}

/// 0x04 - DIV
Expand All @@ -87,8 +84,7 @@ impl StopAndArithmeticOperations of StopAndArithmeticOperationsTrait {
Option::None => 0,
};

self.stack.push(result)?;
Result::Ok(())
self.stack.push(result)
}

/// 0x05 - SDIV
Expand All @@ -109,8 +105,7 @@ impl StopAndArithmeticOperations of StopAndArithmeticOperationsTrait {
Option::None => 0,
};

self.stack.push(result)?;
Result::Ok(())
self.stack.push(result)
}

/// 0x06 - MOD
Expand All @@ -131,8 +126,7 @@ impl StopAndArithmeticOperations of StopAndArithmeticOperationsTrait {
Option::None => 0,
};

self.stack.push(result)?;
Result::Ok(())
self.stack.push(result)
}

/// 0x07 - SMOD
Expand All @@ -153,8 +147,7 @@ impl StopAndArithmeticOperations of StopAndArithmeticOperationsTrait {
},
Option::None => 0,
};
self.stack.push(result)?;
Result::Ok(())
self.stack.push(result)
}

/// 0x08 - ADDMOD
Expand All @@ -179,8 +172,7 @@ impl StopAndArithmeticOperations of StopAndArithmeticOperationsTrait {
Option::None => 0,
};

self.stack.push(result)?;
Result::Ok(())
self.stack.push(result)
}

/// 0x09 - MULMOD operation.
Expand All @@ -205,8 +197,7 @@ impl StopAndArithmeticOperations of StopAndArithmeticOperationsTrait {
Option::None => 0,
};

self.stack.push(result)?;
Result::Ok(())
self.stack.push(result)
}

/// 0x0A - EXP
Expand All @@ -220,8 +211,7 @@ impl StopAndArithmeticOperations of StopAndArithmeticOperationsTrait {

let result = a.pow_mod(b);

self.stack.push(result)?;
Result::Ok(())
self.stack.push(result)
}

/// 0x0B - SIGNEXTEND
Expand Down Expand Up @@ -262,7 +252,6 @@ impl StopAndArithmeticOperations of StopAndArithmeticOperationsTrait {
x
};

self.stack.push(result)?;
Result::Ok(())
self.stack.push(result)
}
}
Loading

0 comments on commit 03d48b9

Please sign in to comment.