Skip to content

Commit

Permalink
feat: 0x1B - SHL opcode (#241)
Browse files Browse the repository at this point in the history
* passing test

* tweaks

* seperated tests

* fixed conflict resolution mistake

* moved both assertions back to wrapping shl test, added test case for shift > 255

* Update crates/evm/src/tests/test_instructions/test_comparison_operations.cairo

* Update crates/evm/src/tests/test_instructions/test_comparison_operations.cairo

---------

Co-authored-by: Mathieu <60658558+enitrat@users.noreply.github.com>
  • Loading branch information
trbutler4 and enitrat authored Sep 4, 2023
1 parent 1306fd5 commit aef3f5d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
12 changes: 11 additions & 1 deletion crates/evm/src/instructions/comparison_operations.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,17 @@ impl ComparisonAndBitwiseOperations of ComparisonAndBitwiseOperationsTrait {
/// 0x1B - SHL
/// # Specification: https://www.evm.codes/#1b?fork=shanghai
fn exec_shl(ref self: ExecutionContext) -> Result<(), EVMError> {
Result::Ok(())
let popped = self.stack.pop_n(2)?;
let shift = *popped[0];
let val = *popped[1];

// if shift is bigger than 255 return 0
if shift > 255 {
return self.stack.push(0);
}

let result = val.wrapping_shl(shift);
self.stack.push(result)
}

/// 0x1C - SHR
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,50 @@ fn test_exec_gt_true() {
assert(ctx.stack.peek().unwrap() == 1, 'stack top should be 1');
}

#[test]
#[available_gas(20000000)]
fn test_exec_shl() {
// Given
let mut ctx = setup_execution_context();
ctx.stack.push(0xff00000000000000000000000000000000000000000000000000000000000000).unwrap();
ctx.stack.push(4_u256).unwrap();

// When
ctx.exec_shl();

// Then
assert(ctx.stack.len() == 1, 'stack should have one element');
assert(
ctx
.stack
.peek()
.unwrap() == 0xf000000000000000000000000000000000000000000000000000000000000000,
'stack top should be 0xf00000...'
);
}

#[test]
#[available_gas(20000000)]
fn test_exec_shl_wrapping() {
// Given
let mut ctx = setup_execution_context();
ctx.stack.push(0xff00000000000000000000000000000000000000000000000000000000000000).unwrap();
ctx.stack.push(256_u256).unwrap();

// When
ctx.exec_shl();

// Then
assert(ctx.stack.len() == 1, 'stack should have one element');
assert(
ctx
.stack
.peek()
.unwrap() == 0,
'if shift > 255 should return 0'
);
}

#[test]
#[available_gas(20000000)]
fn test_exec_gt_false() {
Expand Down

0 comments on commit aef3f5d

Please sign in to comment.