diff --git a/crates/evm/src/instructions.cairo b/crates/evm/src/instructions.cairo index 10a364e18..1c92b2be3 100644 --- a/crates/evm/src/instructions.cairo +++ b/crates/evm/src/instructions.cairo @@ -18,6 +18,7 @@ mod logging_operations; mod memory_operations; mod push_operations; +use push_operations::PushOperationsTrait; mod sha3; diff --git a/crates/evm/src/instructions/push_operations.cairo b/crates/evm/src/instructions/push_operations.cairo index c8183144d..ade1fa3a6 100644 --- a/crates/evm/src/instructions/push_operations.cairo +++ b/crates/evm/src/instructions/push_operations.cairo @@ -1,227 +1,273 @@ //! Push Operations. // Internal imports -use evm::context::ExecutionContext; -use evm::context::ExecutionContextTrait; +use evm::context::{ExecutionContext, ExecutionContextTrait, BoxDynamicExecutionContextDestruct}; +use evm::errors::EVMError; +use evm::stack::StackTrait; mod internal { - use evm::context::ExecutionContext; - use evm::context::ExecutionContextTrait; + use evm::context::{ExecutionContext, ExecutionContextTrait, BoxDynamicExecutionContextDestruct}; + use evm::errors::EVMError; + use evm::stack::StackTrait; + use utils::helpers::load_word; /// Place i bytes items on stack. - fn exec_push_i(ref context: ExecutionContext, i: u8) {} -} - -/// 5F - PUSH0 operation -/// # Specification: https://www.evm.codes/#5f?fork=shanghai -fn exec_push0(ref context: ExecutionContext) {} - - -/// 0x60 - PUSH1 operation -/// # Specification: https://www.evm.codes/#60?fork=shanghai -fn exec_push1(ref context: ExecutionContext) { - internal::exec_push_i(ref context, 1); -} - - -/// 0x61 - PUSH2 operation -/// # Specification: https://www.evm.codes/#61?fork=shanghai -fn exec_push2(ref context: ExecutionContext) { - internal::exec_push_i(ref context, 2); -} - - -/// 0x62 - PUSH3 operation -/// # Specification: https://www.evm.codes/#62?fork=shanghai -fn exec_push3(ref context: ExecutionContext) { - internal::exec_push_i(ref context, 3); -} - -/// 0x63 - PUSH4 operation -/// # Specification: https://www.evm.codes/#63?fork=shanghai -fn exec_push4(ref context: ExecutionContext) { - internal::exec_push_i(ref context, 4); -} - -/// 0x64 - PUSH5 operation -/// # Specification: https://www.evm.codes/#64?fork=shanghai -fn exec_push5(ref context: ExecutionContext) { - internal::exec_push_i(ref context, 5); -} - -/// 0x65 - PUSH6 operation -/// # Specification: https://www.evm.codes/#65?fork=shanghai -fn exec_push6(ref context: ExecutionContext) { - internal::exec_push_i(ref context, 6); -} - -/// 0x66 - PUSH7 operation -/// # Specification: https://www.evm.codes/#66?fork=shanghai -fn exec_push7(ref context: ExecutionContext) { - internal::exec_push_i(ref context, 7); -} - -/// 0x67 - PUSH8 operation -/// # Specification: https://www.evm.codes/#67?fork=shanghai -fn exec_push8(ref context: ExecutionContext) { - internal::exec_push_i(ref context, 8); -} - - -/// 0x68 - PUSH9 operation -/// # Specification: https://www.evm.codes/#68?fork=shanghai -fn exec_push9(ref context: ExecutionContext) { - internal::exec_push_i(ref context, 9); -} - -/// 0x69 - PUSH10 operation -/// # Specification: https://www.evm.codes/#69?fork=shanghai -fn exec_push10(ref context: ExecutionContext) { - internal::exec_push_i(ref context, 10); -} - -/// 0x6A - PUSH11 operation -/// # Specification: https://www.evm.codes/#6a?fork=shanghai -fn exec_push11(ref context: ExecutionContext) { - internal::exec_push_i(ref context, 11); -} - -/// 0x6B - PUSH12 operation -/// # Specification: https://www.evm.codes/#6b?fork=shanghai -fn exec_push12(ref context: ExecutionContext) { - internal::exec_push_i(ref context, 12); -} - - -/// 0x6C - PUSH13 operation -/// # Specification: https://www.evm.codes/#6c?fork=shanghai -fn exec_push13(ref context: ExecutionContext) { - internal::exec_push_i(ref context, 13); -} - -/// 0x6D - PUSH14 operation -/// # Specification: https://www.evm.codes/#6d?fork=shanghai -fn exec_push14(ref context: ExecutionContext) { - internal::exec_push_i(ref context, 14); -} - - -/// 0x6E - PUSH15 operation -/// # Specification: https://www.evm.codes/#6e?fork=shanghai -fn exec_push15(ref context: ExecutionContext) { - internal::exec_push_i(ref context, 15); -} - -/// 0x6F - PUSH16 operation -/// # Specification: https://www.evm.codes/#6f?fork=shanghai -fn exec_push16(ref context: ExecutionContext) { - internal::exec_push_i(ref context, 16); -} - -/// 0x70 - PUSH17 operation -/// # Specification: https://www.evm.codes/#70?fork=shanghai -fn exec_push17(ref context: ExecutionContext) { - internal::exec_push_i(ref context, 17); -} - -/// 0x71 - PUSH18 operation -/// # Specification: https://www.evm.codes/#71?fork=shanghai -fn exec_push18(ref context: ExecutionContext) { - internal::exec_push_i(ref context, 18); -} - - -/// 0x72 - PUSH19 operation -/// # Specification: https://www.evm.codes/#72?fork=shanghai -fn exec_push19(ref context: ExecutionContext) { - internal::exec_push_i(ref context, 19); -} - -/// 0x73 - PUSH20 operation -/// # Specification: https://www.evm.codes/#73?fork=shanghai -fn exec_push20(ref context: ExecutionContext) { - internal::exec_push_i(ref context, 20); -} - - -/// 0x74 - PUSH21 operation -/// # Specification: https://www.evm.codes/#74?fork=shanghai -fn exec_push21(ref context: ExecutionContext) { - internal::exec_push_i(ref context, 21); -} - - -/// 0x75 - PUSH22 operation -/// # Specification: https://www.evm.codes/#75?fork=shanghai -fn exec_push22(ref context: ExecutionContext) { - internal::exec_push_i(ref context, 22); -} - - -/// 0x76 - PUSH23 operation -/// # Specification: https://www.evm.codes/#76?fork=shanghai -fn exec_push23(ref context: ExecutionContext) { - internal::exec_push_i(ref context, 23); -} - - -/// 0x77 - PUSH24 operation -/// # Specification: https://www.evm.codes/#77?fork=shanghai -fn exec_push24(ref context: ExecutionContext) { - internal::exec_push_i(ref context, 24); -} - - -/// 0x78 - PUSH21 operation -/// # Specification: https://www.evm.codes/#78?fork=shanghai -fn exec_push25(ref context: ExecutionContext) { - internal::exec_push_i(ref context, 25); -} - - -/// 0x79 - PUSH26 operation -/// # Specification: https://www.evm.codes/#79?fork=shanghai -fn exec_push26(ref context: ExecutionContext) { - internal::exec_push_i(ref context, 26); -} - - -/// 0x7A - PUSH27 operation -/// # Specification: https://www.evm.codes/#7a?fork=shanghai -fn exec_push27(ref context: ExecutionContext) { - internal::exec_push_i(ref context, 27); -} - -/// 0x7B - PUSH28 operation -/// # Specification: https://www.evm.codes/#7b?fork=shanghai -fn exec_push28(ref context: ExecutionContext) { - internal::exec_push_i(ref context, 28); -} - - -/// 0x7C - PUSH29 operation -/// # Specification: https://www.evm.codes/#7c?fork=shanghai -fn exec_push29(ref context: ExecutionContext) { - internal::exec_push_i(ref context, 29); -} - - -/// 0x7D - PUSH30 operation -/// # Specification: https://www.evm.codes/#7d?fork=shanghai -fn exec_push30(ref context: ExecutionContext) { - internal::exec_push_i(ref context, 31); -} - - -/// 0x7E - PUSH31 operation -/// # Specification: https://www.evm.codes/#7e?fork=shanghai -fn exec_push31(ref context: ExecutionContext) { - internal::exec_push_i(ref context, 31); -} - - -/// 0x7F - PUSH32 operation -/// # Specification: https://www.evm.codes/#7f?fork=shanghai -fn exec_push32(ref context: ExecutionContext) { - internal::exec_push_i(ref context, 32); + #[inline(always)] + fn exec_push_i(ref context: ExecutionContext, i: u8) -> Result<(), EVMError> { + let i = i.into(); + let data = context.read_code(i); + context.stack.push(load_word(i, data)) + } +} + +#[generate_trait] +impl PushOperations of PushOperationsTrait { + /// 5F - PUSH0 operation + /// # Specification: https://www.evm.codes/#5f?fork=shanghai + #[inline(always)] + fn exec_push0(ref self: ExecutionContext) -> Result<(), EVMError> { + self.stack.push(0) + } + + + /// 0x60 - PUSH1 operation + /// # Specification: https://www.evm.codes/#60?fork=shanghai + #[inline(always)] + fn exec_push1(ref self: ExecutionContext) -> Result<(), EVMError> { + internal::exec_push_i(ref self, 1) + } + + + /// 0x61 - PUSH2 operation + /// # Specification: https://www.evm.codes/#61?fork=shanghai + #[inline(always)] + fn exec_push2(ref self: ExecutionContext) -> Result<(), EVMError> { + internal::exec_push_i(ref self, 2) + } + + + /// 0x62 - PUSH3 operation + /// # Specification: https://www.evm.codes/#62?fork=shanghai + #[inline(always)] + fn exec_push3(ref self: ExecutionContext) -> Result<(), EVMError> { + internal::exec_push_i(ref self, 3) + } + + /// 0x63 - PUSH4 operation + /// # Specification: https://www.evm.codes/#63?fork=shanghai + #[inline(always)] + fn exec_push4(ref self: ExecutionContext) -> Result<(), EVMError> { + internal::exec_push_i(ref self, 4) + } + + /// 0x64 - PUSH5 operation + /// # Specification: https://www.evm.codes/#64?fork=shanghai + #[inline(always)] + fn exec_push5(ref self: ExecutionContext) -> Result<(), EVMError> { + internal::exec_push_i(ref self, 5) + } + + /// 0x65 - PUSH6 operation + /// # Specification: https://www.evm.codes/#65?fork=shanghai + #[inline(always)] + fn exec_push6(ref self: ExecutionContext) -> Result<(), EVMError> { + internal::exec_push_i(ref self, 6) + } + + /// 0x66 - PUSH7 operation + /// # Specification: https://www.evm.codes/#66?fork=shanghai + #[inline(always)] + fn exec_push7(ref self: ExecutionContext) -> Result<(), EVMError> { + internal::exec_push_i(ref self, 7) + } + + /// 0x67 - PUSH8 operation + /// # Specification: https://www.evm.codes/#67?fork=shanghai + #[inline(always)] + fn exec_push8(ref self: ExecutionContext) -> Result<(), EVMError> { + internal::exec_push_i(ref self, 8) + } + + + /// 0x68 - PUSH9 operation + /// # Specification: https://www.evm.codes/#68?fork=shanghai + #[inline(always)] + fn exec_push9(ref self: ExecutionContext) -> Result<(), EVMError> { + internal::exec_push_i(ref self, 9) + } + + /// 0x69 - PUSH10 operation + /// # Specification: https://www.evm.codes/#69?fork=shanghai + #[inline(always)] + fn exec_push10(ref self: ExecutionContext) -> Result<(), EVMError> { + internal::exec_push_i(ref self, 10) + } + + /// 0x6A - PUSH11 operation + /// # Specification: https://www.evm.codes/#6a?fork=shanghai + #[inline(always)] + fn exec_push11(ref self: ExecutionContext) -> Result<(), EVMError> { + internal::exec_push_i(ref self, 11) + } + + /// 0x6B - PUSH12 operation + /// # Specification: https://www.evm.codes/#6b?fork=shanghai + #[inline(always)] + fn exec_push12(ref self: ExecutionContext) -> Result<(), EVMError> { + internal::exec_push_i(ref self, 12) + } + + + /// 0x6C - PUSH13 operation + /// # Specification: https://www.evm.codes/#6c?fork=shanghai + #[inline(always)] + fn exec_push13(ref self: ExecutionContext) -> Result<(), EVMError> { + internal::exec_push_i(ref self, 13) + } + + /// 0x6D - PUSH14 operation + /// # Specification: https://www.evm.codes/#6d?fork=shanghai + #[inline(always)] + fn exec_push14(ref self: ExecutionContext) -> Result<(), EVMError> { + internal::exec_push_i(ref self, 14) + } + + + /// 0x6E - PUSH15 operation + /// # Specification: https://www.evm.codes/#6e?fork=shanghai + #[inline(always)] + fn exec_push15(ref self: ExecutionContext) -> Result<(), EVMError> { + internal::exec_push_i(ref self, 15) + } + + /// 0x6F - PUSH16 operation + /// # Specification: https://www.evm.codes/#6f?fork=shanghai + #[inline(always)] + fn exec_push16(ref self: ExecutionContext) -> Result<(), EVMError> { + internal::exec_push_i(ref self, 16) + } + + /// 0x70 - PUSH17 operation + /// # Specification: https://www.evm.codes/#70?fork=shanghai + #[inline(always)] + fn exec_push17(ref self: ExecutionContext) -> Result<(), EVMError> { + internal::exec_push_i(ref self, 17) + } + + /// 0x71 - PUSH18 operation + /// # Specification: https://www.evm.codes/#71?fork=shanghai + #[inline(always)] + fn exec_push18(ref self: ExecutionContext) -> Result<(), EVMError> { + internal::exec_push_i(ref self, 18) + } + + + /// 0x72 - PUSH19 operation + /// # Specification: https://www.evm.codes/#72?fork=shanghai + #[inline(always)] + fn exec_push19(ref self: ExecutionContext) -> Result<(), EVMError> { + internal::exec_push_i(ref self, 19) + } + + /// 0x73 - PUSH20 operation + /// # Specification: https://www.evm.codes/#73?fork=shanghai + #[inline(always)] + fn exec_push20(ref self: ExecutionContext) -> Result<(), EVMError> { + internal::exec_push_i(ref self, 20) + } + + + /// 0x74 - PUSH21 operation + /// # Specification: https://www.evm.codes/#74?fork=shanghai + #[inline(always)] + fn exec_push21(ref self: ExecutionContext) -> Result<(), EVMError> { + internal::exec_push_i(ref self, 21) + } + + + /// 0x75 - PUSH22 operation + /// # Specification: https://www.evm.codes/#75?fork=shanghai + #[inline(always)] + fn exec_push22(ref self: ExecutionContext) -> Result<(), EVMError> { + internal::exec_push_i(ref self, 22) + } + + + /// 0x76 - PUSH23 operation + /// # Specification: https://www.evm.codes/#76?fork=shanghai + #[inline(always)] + fn exec_push23(ref self: ExecutionContext) -> Result<(), EVMError> { + internal::exec_push_i(ref self, 23) + } + + + /// 0x77 - PUSH24 operation + /// # Specification: https://www.evm.codes/#77?fork=shanghai + #[inline(always)] + fn exec_push24(ref self: ExecutionContext) -> Result<(), EVMError> { + internal::exec_push_i(ref self, 24) + } + + + /// 0x78 - PUSH21 operation + /// # Specification: https://www.evm.codes/#78?fork=shanghai + #[inline(always)] + fn exec_push25(ref self: ExecutionContext) -> Result<(), EVMError> { + internal::exec_push_i(ref self, 25) + } + + + /// 0x79 - PUSH26 operation + /// # Specification: https://www.evm.codes/#79?fork=shanghai + #[inline(always)] + fn exec_push26(ref self: ExecutionContext) -> Result<(), EVMError> { + internal::exec_push_i(ref self, 26) + } + + + /// 0x7A - PUSH27 operation + /// # Specification: https://www.evm.codes/#7a?fork=shanghai + #[inline(always)] + fn exec_push27(ref self: ExecutionContext) -> Result<(), EVMError> { + internal::exec_push_i(ref self, 27) + } + + /// 0x7B - PUSH28 operation + /// # Specification: https://www.evm.codes/#7b?fork=shanghai + #[inline(always)] + fn exec_push28(ref self: ExecutionContext) -> Result<(), EVMError> { + internal::exec_push_i(ref self, 28) + } + + + /// 0x7C - PUSH29 operation + /// # Specification: https://www.evm.codes/#7c?fork=shanghai + #[inline(always)] + fn exec_push29(ref self: ExecutionContext) -> Result<(), EVMError> { + internal::exec_push_i(ref self, 29) + } + + + /// 0x7D - PUSH30 operation + /// # Specification: https://www.evm.codes/#7d?fork=shanghai + #[inline(always)] + fn exec_push30(ref self: ExecutionContext) -> Result<(), EVMError> { + internal::exec_push_i(ref self, 30) + } + + + /// 0x7E - PUSH31 operation + /// # Specification: https://www.evm.codes/#7e?fork=shanghai + #[inline(always)] + fn exec_push31(ref self: ExecutionContext) -> Result<(), EVMError> { + internal::exec_push_i(ref self, 31) + } + + + /// 0x7F - PUSH32 operation + /// # Specification: https://www.evm.codes/#7f?fork=shanghai + #[inline(always)] + fn exec_push32(ref self: ExecutionContext) -> Result<(), EVMError> { + internal::exec_push_i(ref self, 32) + } } diff --git a/crates/evm/src/interpreter.cairo b/crates/evm/src/interpreter.cairo index 0cb4278a0..bb309ea97 100644 --- a/crates/evm/src/interpreter.cairo +++ b/crates/evm/src/interpreter.cairo @@ -13,9 +13,9 @@ use utils::{helpers::u256_to_bytes_array}; use evm::errors::{EVMError, PC_OUT_OF_BOUNDS}; use evm::instructions::{ duplication_operations, environmental_information, exchange_operations, logging_operations, - memory_operations, push_operations, sha3, StopAndArithmeticOperationsTrait, - ComparisonAndBitwiseOperationsTrait, system_operations, BlockInformationTrait, - DuplicationOperationsTrait, EnvironmentInformationTrait + memory_operations, sha3, StopAndArithmeticOperationsTrait, ComparisonAndBitwiseOperationsTrait, + system_operations, BlockInformationTrait, DuplicationOperationsTrait, + EnvironmentInformationTrait, PushOperationsTrait }; use result::ResultTrait; @@ -343,135 +343,135 @@ impl EVMInterpreterImpl of EVMInterpreterTrait { } if opcode == 95 { // PUSH0 - push_operations::exec_push0(ref context); + return context.exec_push0(); } if opcode == 96 { // PUSH1 - push_operations::exec_push1(ref context); + return context.exec_push1(); } if opcode == 97 { // PUSH2 - push_operations::exec_push2(ref context); + return context.exec_push2(); } if opcode == 98 { // PUSH3 - push_operations::exec_push3(ref context); + return context.exec_push3(); } if opcode == 99 { // PUSH4 - push_operations::exec_push4(ref context); + return context.exec_push4(); } if opcode == 100 { // PUSH5 - push_operations::exec_push5(ref context); + return context.exec_push5(); } if opcode == 101 { // PUSH6 - push_operations::exec_push6(ref context); + return context.exec_push6(); } if opcode == 102 { // PUSH7 - push_operations::exec_push7(ref context); + return context.exec_push7(); } if opcode == 103 { // PUSH8 - push_operations::exec_push8(ref context); + return context.exec_push8(); } if opcode == 104 { // PUSH9 - push_operations::exec_push9(ref context); + return context.exec_push9(); } if opcode == 105 { // PUSH10 - push_operations::exec_push10(ref context); + return context.exec_push10(); } if opcode == 106 { // PUSH11 - push_operations::exec_push11(ref context); + return context.exec_push11(); } if opcode == 107 { // PUSH12 - push_operations::exec_push12(ref context); + return context.exec_push12(); } if opcode == 108 { // PUSH13 - push_operations::exec_push13(ref context); + return context.exec_push13(); } if opcode == 109 { // PUSH14 - push_operations::exec_push14(ref context); + return context.exec_push14(); } if opcode == 110 { // PUSH15 - push_operations::exec_push15(ref context); + return context.exec_push15(); } if opcode == 111 { // PUSH16 - push_operations::exec_push16(ref context); + return context.exec_push16(); } if opcode == 112 { // PUSH17 - push_operations::exec_push17(ref context); + return context.exec_push17(); } if opcode == 113 { // PUSH18 - push_operations::exec_push18(ref context); + return context.exec_push18(); } if opcode == 114 { // PUSH19 - push_operations::exec_push19(ref context); + return context.exec_push19(); } if opcode == 115 { // PUSH20 - push_operations::exec_push20(ref context); + return context.exec_push20(); } if opcode == 116 { // PUSH21 - push_operations::exec_push21(ref context); + return context.exec_push21(); } if opcode == 117 { // PUSH22 - push_operations::exec_push22(ref context); + return context.exec_push22(); } if opcode == 118 { // PUSH23 - push_operations::exec_push23(ref context); + return context.exec_push23(); } if opcode == 119 { // PUSH24 - push_operations::exec_push24(ref context); + return context.exec_push24(); } if opcode == 120 { // PUSH25 - push_operations::exec_push25(ref context); + return context.exec_push25(); } if opcode == 121 { // PUSH26 - push_operations::exec_push26(ref context); + return context.exec_push26(); } if opcode == 122 { // PUSH27 - push_operations::exec_push27(ref context); + return context.exec_push27(); } if opcode == 123 { // PUSH28 - push_operations::exec_push28(ref context); + return context.exec_push28(); } if opcode == 124 { // PUSH29 - push_operations::exec_push29(ref context); + return context.exec_push29(); } if opcode == 125 { // PUSH30 - push_operations::exec_push30(ref context); + return context.exec_push30(); } if opcode == 126 { // PUSH31 - push_operations::exec_push31(ref context); + return context.exec_push31(); } if opcode == 127 { // PUSH32 - push_operations::exec_push32(ref context); + return context.exec_push32(); } if opcode == 128 { // DUP1 diff --git a/crates/evm/src/tests/test_instructions.cairo b/crates/evm/src/tests/test_instructions.cairo index b1e15d62b..1a1048b11 100644 --- a/crates/evm/src/tests/test_instructions.cairo +++ b/crates/evm/src/tests/test_instructions.cairo @@ -3,3 +3,4 @@ mod test_comparison_operations; mod test_duplication_operations; mod test_block_information; mod test_environment_information; +mod test_push_operations; diff --git a/crates/evm/src/tests/test_instructions/test_push_operations.cairo b/crates/evm/src/tests/test_instructions/test_push_operations.cairo new file mode 100644 index 000000000..59a999574 --- /dev/null +++ b/crates/evm/src/tests/test_instructions/test_push_operations.cairo @@ -0,0 +1,460 @@ +use evm::instructions::PushOperationsTrait; +use evm::context::BoxDynamicExecutionContextDestruct; +use evm::stack::StackTrait; +use evm::tests::test_utils::setup_execution_context_with_bytecode; +use array::ArrayTrait; +use evm::context::ExecutionContextTrait; + +fn get_n_0xFF(mut n: u8) -> Span { + let mut array: Array = ArrayTrait::new(); + loop { + if n == 0 { + break; + } + array.append(0xFF); + n -= 1; + }; + array.span() +} + +#[test] +#[available_gas(20000000)] +fn test_push0() { + // Given + let mut ctx = setup_execution_context_with_bytecode(get_n_0xFF(0)); + // When + ctx.exec_push0(); + // Then + assert(ctx.stack.len() == 1, 'stack should have one element'); + assert(ctx.stack.peek().unwrap() == 0, 'invalid stack top'); +} + +#[test] +#[available_gas(20000000)] +fn test_push1() { + // Given + let mut ctx = setup_execution_context_with_bytecode(get_n_0xFF(1)); + // When + ctx.exec_push1(); + // Then + assert(ctx.stack.len() == 1, 'stack should have one element'); + assert(ctx.stack.peek().unwrap() == 0xFF, 'invalid stack top'); +} + +#[test] +#[available_gas(20000000)] +fn test_push2() { + // Given + let mut ctx = setup_execution_context_with_bytecode(get_n_0xFF(2)); + // When + ctx.exec_push2(); + // Then + assert(ctx.stack.len() == 1, 'stack should have one element'); + assert(ctx.stack.peek().unwrap() == 0xFFFF, 'invalid stack top'); +} + +#[test] +#[available_gas(20000000)] +fn test_push3() { + // Given + let mut ctx = setup_execution_context_with_bytecode(get_n_0xFF(3)); + // When + ctx.exec_push3(); + // Then + assert(ctx.stack.len() == 1, 'stack should have one element'); + assert(ctx.stack.peek().unwrap() == 0xFFFFFF, 'invalid stack top'); +} + +#[test] +#[available_gas(20000000)] +fn test_push4() { + // Given + let mut ctx = setup_execution_context_with_bytecode(get_n_0xFF(4)); + // When + ctx.exec_push4(); + // Then + assert(ctx.stack.len() == 1, 'stack should have one element'); + assert(ctx.stack.peek().unwrap() == 0xFFFFFFFF, 'invalid stack top'); +} + +#[test] +#[available_gas(20000000)] +fn test_push5() { + // Given + let mut ctx = setup_execution_context_with_bytecode(get_n_0xFF(5)); + // When + ctx.exec_push5(); + // Then + assert(ctx.stack.len() == 1, 'stack should have one element'); + assert(ctx.stack.peek().unwrap() == 0xFFFFFFFFFF, 'invalid stack top'); +} + +#[test] +#[available_gas(20000000)] +fn test_push6() { + // Given + let mut ctx = setup_execution_context_with_bytecode(get_n_0xFF(6)); + // When + ctx.exec_push6(); + // Then + assert(ctx.stack.len() == 1, 'stack should have one element'); + assert(ctx.stack.peek().unwrap() == 0xFFFFFFFFFFFF, 'invalid stack top'); +} + +#[test] +#[available_gas(20000000)] +fn test_push7() { + // Given + let mut ctx = setup_execution_context_with_bytecode(get_n_0xFF(7)); + // When + ctx.exec_push7(); + // Then + assert(ctx.stack.len() == 1, 'stack should have one element'); + assert(ctx.stack.peek().unwrap() == 0xFFFFFFFFFFFFFF, 'invalid stack top'); +} + + +#[test] +#[available_gas(20000000)] +fn test_push8() { + // Given + let mut ctx = setup_execution_context_with_bytecode(get_n_0xFF(8)); + // When + ctx.exec_push8(); + // Then + assert(ctx.stack.len() == 1, 'stack should have one element'); + assert(ctx.stack.peek().unwrap() == 0xFFFFFFFFFFFFFFFF, 'invalid stack top'); +} + +#[test] +#[available_gas(20000000)] +fn test_push9() { + // Given + let mut ctx = setup_execution_context_with_bytecode(get_n_0xFF(9)); + // When + ctx.exec_push9(); + // Then + assert(ctx.stack.len() == 1, 'stack should have one element'); + assert(ctx.stack.peek().unwrap() == 0xFFFFFFFFFFFFFFFFFF, 'invalid stack top'); +} + +#[test] +#[available_gas(20000000)] +fn test_push10() { + // Given + let mut ctx = setup_execution_context_with_bytecode(get_n_0xFF(10)); + // When + ctx.exec_push10(); + // Then + assert(ctx.stack.len() == 1, 'stack should have one element'); + assert(ctx.stack.peek().unwrap() == 0xFFFFFFFFFFFFFFFFFFFF, 'invalid stack top'); +} + +#[test] +#[available_gas(20000000)] +fn test_push11() { + // Given + let mut ctx = setup_execution_context_with_bytecode(get_n_0xFF(11)); + // When + ctx.exec_push11(); + // Then + assert(ctx.stack.len() == 1, 'stack should have one element'); + assert(ctx.stack.peek().unwrap() == 0xFFFFFFFFFFFFFFFFFFFFFF, 'invalid stack top'); +} + +#[test] +#[available_gas(20000000)] +fn test_push12() { + // Given + let mut ctx = setup_execution_context_with_bytecode(get_n_0xFF(12)); + // When + ctx.exec_push12(); + // Then + assert(ctx.stack.len() == 1, 'stack should have one element'); + assert(ctx.stack.peek().unwrap() == 0xFFFFFFFFFFFFFFFFFFFFFFFF, 'invalid stack top'); +} + +#[test] +#[available_gas(20000000)] +fn test_push13() { + // Given + let mut ctx = setup_execution_context_with_bytecode(get_n_0xFF(13)); + // When + ctx.exec_push13(); + // Then + assert(ctx.stack.len() == 1, 'stack should have one element'); + assert(ctx.stack.peek().unwrap() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFF, 'invalid stack top'); +} + +#[test] +#[available_gas(20000000)] +fn test_push14() { + // Given + let mut ctx = setup_execution_context_with_bytecode(get_n_0xFF(14)); + // When + ctx.exec_push14(); + // Then + assert(ctx.stack.len() == 1, 'stack should have one element'); + assert(ctx.stack.peek().unwrap() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFF, 'invalid stack top'); +} + +#[test] +#[available_gas(20000000)] +fn test_push15() { + // Given + let mut ctx = setup_execution_context_with_bytecode(get_n_0xFF(15)); + // When + ctx.exec_push15(); + // Then + assert(ctx.stack.len() == 1, 'stack should have one element'); + assert(ctx.stack.peek().unwrap() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, 'invalid stack top'); +} + +#[test] +#[available_gas(20000000)] +fn test_push16() { + // Given + let mut ctx = setup_execution_context_with_bytecode(get_n_0xFF(16)); + // When + ctx.exec_push16(); + // Then + assert(ctx.stack.len() == 1, 'stack should have one element'); + assert(ctx.stack.peek().unwrap() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, 'invalid stack top'); +} + +#[test] +#[available_gas(20000000)] +fn test_push17() { + // Given + let mut ctx = setup_execution_context_with_bytecode(get_n_0xFF(17)); + // When + ctx.exec_push17(); + // Then + assert(ctx.stack.len() == 1, 'stack should have one element'); + assert(ctx.stack.peek().unwrap() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, 'invalid stack top'); +} + +#[test] +#[available_gas(20000000)] +fn test_push18() { + // Given + let mut ctx = setup_execution_context_with_bytecode(get_n_0xFF(18)); + // When + ctx.exec_push18(); + // Then + assert(ctx.stack.len() == 1, 'stack should have one element'); + assert( + ctx.stack.peek().unwrap() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, 'invalid stack top' + ); +} +#[test] +#[available_gas(20000000)] +fn test_push19() { + // Given + let mut ctx = setup_execution_context_with_bytecode(get_n_0xFF(19)); + // When + ctx.exec_push19(); + // Then + assert(ctx.stack.len() == 1, 'stack should have one element'); + assert( + ctx.stack.peek().unwrap() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, 'invalid stack top' + ); +} + +#[test] +#[available_gas(20000000)] +fn test_push20() { + // Given + let mut ctx = setup_execution_context_with_bytecode(get_n_0xFF(20)); + // When + ctx.exec_push20(); + // Then + assert(ctx.stack.len() == 1, 'stack should have one element'); + assert( + ctx.stack.peek().unwrap() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, 'invalid stack top' + ); +} + +#[test] +#[available_gas(20000000)] +fn test_push21() { + // Given + let mut ctx = setup_execution_context_with_bytecode(get_n_0xFF(21)); + // When + ctx.exec_push21(); + // Then + assert(ctx.stack.len() == 1, 'stack should have one element'); + assert( + ctx.stack.peek().unwrap() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + 'invalid stack top' + ); +} + +#[test] +#[available_gas(20000000)] +fn test_push22() { + // Given + let mut ctx = setup_execution_context_with_bytecode(get_n_0xFF(22)); + // When + ctx.exec_push22(); + // Then + assert(ctx.stack.len() == 1, 'stack should have one element'); + assert( + ctx.stack.peek().unwrap() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + 'invalid stack top' + ); +} +#[test] +#[available_gas(20000000)] +fn test_push23() { + // Given + let mut ctx = setup_execution_context_with_bytecode(get_n_0xFF(23)); + // When + ctx.exec_push23(); + // Then + assert(ctx.stack.len() == 1, 'stack should have one element'); + assert( + ctx.stack.peek().unwrap() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + 'invalid stack top' + ); +} +#[test] +#[available_gas(20000000)] +fn test_push24() { + // Given + let mut ctx = setup_execution_context_with_bytecode(get_n_0xFF(24)); + // When + ctx.exec_push24(); + // Then + assert(ctx.stack.len() == 1, 'stack should have one element'); + assert( + ctx.stack.peek().unwrap() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + 'invalid stack top' + ); +} + +#[test] +#[available_gas(20000000)] +fn test_push25() { + // Given + let mut ctx = setup_execution_context_with_bytecode(get_n_0xFF(25)); + // When + ctx.exec_push25(); + // Then + assert(ctx.stack.len() == 1, 'stack should have one element'); + assert( + ctx.stack.peek().unwrap() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + 'invalid stack top' + ); +} + +#[test] +#[available_gas(20000000)] +fn test_push26() { + // Given + let mut ctx = setup_execution_context_with_bytecode(get_n_0xFF(26)); + // When + ctx.exec_push26(); + // Then + assert(ctx.stack.len() == 1, 'stack should have one element'); + assert( + ctx.stack.peek().unwrap() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + 'invalid stack top' + ); +} + +#[test] +#[available_gas(20000000)] +fn test_push27() { + // Given + let mut ctx = setup_execution_context_with_bytecode(get_n_0xFF(27)); + // When + ctx.exec_push27(); + // Then + assert(ctx.stack.len() == 1, 'stack should have one element'); + assert( + ctx.stack.peek().unwrap() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + 'invalid stack top' + ); +} + +#[test] +#[available_gas(20000000)] +fn test_push28() { + // Given + let mut ctx = setup_execution_context_with_bytecode(get_n_0xFF(28)); + // When + ctx.exec_push28(); + // Then + assert(ctx.stack.len() == 1, 'stack should have one element'); + assert( + ctx.stack.peek().unwrap() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + 'invalid stack top' + ); +} + +#[test] +#[available_gas(20000000)] +fn test_push29() { + // Given + let mut ctx = setup_execution_context_with_bytecode(get_n_0xFF(29)); + // When + ctx.exec_push29(); + // Then + assert(ctx.stack.len() == 1, 'stack should have one element'); + assert( + ctx.stack.peek().unwrap() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + 'invalid stack top' + ); +} + +#[test] +#[available_gas(20000000)] +fn test_push30() { + // Given + let mut ctx = setup_execution_context_with_bytecode(get_n_0xFF(30)); + // When + ctx.exec_push30(); + // Then + assert(ctx.stack.len() == 1, 'stack should have one element'); + assert( + ctx.stack.peek().unwrap() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + 'invalid stack top' + ); +} + +#[test] +#[available_gas(20000000)] +fn test_push31() { + // Given + let mut ctx = setup_execution_context_with_bytecode(get_n_0xFF(31)); + // When + ctx.exec_push31(); + // Then + assert(ctx.stack.len() == 1, 'stack should have one element'); + assert( + ctx + .stack + .peek() + .unwrap() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + 'invalid stack top' + ); +} + +#[test] +#[available_gas(20000000)] +fn test_push32() { + // Given + let mut ctx = setup_execution_context_with_bytecode(get_n_0xFF(32)); + // When + ctx.exec_push32(); + // Then + assert(ctx.stack.len() == 1, 'stack should have one element'); + assert( + ctx + .stack + .peek() + .unwrap() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + 'invalid stack top' + ); +} diff --git a/crates/evm/src/tests/test_utils.cairo b/crates/evm/src/tests/test_utils.cairo index 094457fd0..a11cbe782 100644 --- a/crates/evm/src/tests/test_utils.cairo +++ b/crates/evm/src/tests/test_utils.cairo @@ -42,6 +42,27 @@ fn setup_execution_context() -> ExecutionContext { ) } +fn setup_call_context_with_bytecode(bytecode: Span) -> CallContext { + let call_data: Span = array![4, 5, 6].span(); + let value: u256 = 100; + + CallContextTrait::new(bytecode, call_data, value) +} + +fn setup_execution_context_with_bytecode(bytecode: Span) -> ExecutionContext { + let call_context = setup_call_context_with_bytecode(bytecode); + let starknet_address: ContractAddress = starknet_address(); + let evm_address: EthAddress = evm_address(); + let gas_limit: u64 = 1000; + let gas_price: u64 = 10; + let read_only: bool = false; + let returned_data = Default::default(); + + ExecutionContextTrait::new( + call_context, starknet_address, evm_address, gas_limit, gas_price, returned_data, read_only + ) +} + impl CallContextPartialEq of PartialEq { fn eq(lhs: @CallContext, rhs: @CallContext) -> bool { lhs.bytecode() == rhs.bytecode() && lhs.call_data == rhs.call_data && lhs.value == rhs.value