From 5996c3f6f50ce0924a0b65bd97ad0a2a2705c95b Mon Sep 17 00:00:00 2001 From: 0xlny Date: Mon, 17 Oct 2022 17:56:30 +0200 Subject: [PATCH] :sparkles: Implement SLT --- src/kakarot/instructions.cairo | 4 +- .../instructions/comparison_operations.cairo | 40 ++++++++++++++++++- tests/cases/003_slt.json | 6 +++ tests/units/kakarot/test_basic.cairo | 13 +++++- 4 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 tests/cases/003_slt.json diff --git a/src/kakarot/instructions.cairo b/src/kakarot/instructions.cairo index 8ea86fc2b..e82ace0f9 100644 --- a/src/kakarot/instructions.cairo +++ b/src/kakarot/instructions.cairo @@ -184,8 +184,10 @@ namespace EVMInstructions { // Comparison & bitwise logic operations // 0x10 - LT add_instruction(instructions, 0x10, ComparisonOperations.exec_lt); - // 0x1 - GT + // 0x11 - GT add_instruction(instructions, 0x11, ComparisonOperations.exec_gt); + // 0x12 - SLT + add_instruction(instructions, 0x12, ComparisonOperations.exec_slt); // 0x52 - MSTORE add_instruction(instructions, 0x52, MemoryOperations.exec_store); diff --git a/src/kakarot/instructions/comparison_operations.cairo b/src/kakarot/instructions/comparison_operations.cairo index 33039bcdc..5cecfef22 100644 --- a/src/kakarot/instructions/comparison_operations.cairo +++ b/src/kakarot/instructions/comparison_operations.cairo @@ -4,7 +4,7 @@ // Starkware dependencies from starkware.cairo.common.cairo_builtins import HashBuiltin -from starkware.cairo.common.uint256 import Uint256, uint256_lt +from starkware.cairo.common.uint256 import Uint256, uint256_lt, uint256_signed_lt // Internal dependencies from kakarot.model import model @@ -19,6 +19,7 @@ namespace ComparisonOperations { // Define constants. const GAS_COST_LT = 3; const GAS_COST_GT = 3; + const GAS_COST_SLT = 3; // @notice 0x10 - LT // @dev Comparison operation @@ -93,4 +94,41 @@ namespace ComparisonOperations { let ctx = ExecutionContext.increment_gas_used(ctx, GAS_COST_GT); return ctx; } + + // @notice 0x12 - SLT + // @dev Comparison operation + // @custom:since Frontier + // @custom:group Comparison & Bitwise Logic Operations + // @custom:gas 3 + // @custom:stack_consumed_elements 2 + // @custom:stack_produced_elements 1 + // @param ctx The pointer to the execution context. + // @return The pointer to the execution context. + func exec_slt{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + ctx: model.ExecutionContext* + ) -> model.ExecutionContext* { + alloc_locals; + %{ print("0x12 - SLT") %} + + let stack = ctx.stack; + + // Stack input: + // 0 - a: left side signed integer. + // 1 - b: right side signed integer. + let (stack, a) = Stack.pop(stack); + let (stack, b) = Stack.pop(stack); + + // Compute the comparison + let (result) = uint256_signed_lt(a, b); + + // Stack output: + // a < b: integer result of comparison a less than b + let stack: model.Stack* = Stack.push(stack, Uint256(result, 0)); + + // Update context stack. + let ctx = ExecutionContext.update_stack(ctx, stack); + // Increment gas used. + let ctx = ExecutionContext.increment_gas_used(ctx, GAS_COST_SLT); + return ctx; + } } diff --git a/tests/cases/003_slt.json b/tests/cases/003_slt.json new file mode 100644 index 000000000..b4f46f584 --- /dev/null +++ b/tests/cases/003_slt.json @@ -0,0 +1,6 @@ +{ + "name": "Comparison & bitwise logic operations - SLT", + "code": "60ff600112", + "calldata": "", + "expected_return_data": "" +} \ No newline at end of file diff --git a/tests/units/kakarot/test_basic.cairo b/tests/units/kakarot/test_basic.cairo index 58500ed53..f67f7d39f 100644 --- a/tests/units/kakarot/test_basic.cairo +++ b/tests/units/kakarot/test_basic.cairo @@ -67,7 +67,7 @@ func test_comparison_operations{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, // Assert value on the top of the stack test_utils.assert_top_stack(ctx, 0); - // Load test case for LT + // Load test case for GT let (evm_test_case: EVMTestCase) = test_utils.load_evm_test_case_from_file( './tests/cases/003_gt.json' ); @@ -75,6 +75,17 @@ func test_comparison_operations{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, // Run EVM execution let ctx: model.ExecutionContext* = Kakarot.execute(evm_test_case.code, evm_test_case.calldata); + // Assert value on the top of the stack + test_utils.assert_top_stack(ctx, 1); + + // Load test case for SLT + let (evm_test_case: EVMTestCase) = test_utils.load_evm_test_case_from_file( + './tests/cases/003_slt.json' + ); + + // Run EVM execution + let ctx: model.ExecutionContext* = Kakarot.execute(evm_test_case.code, evm_test_case.calldata); + // Assert value on the top of the stack test_utils.assert_top_stack(ctx, 1);