Skip to content

Commit

Permalink
✨ Implement SGT
Browse files Browse the repository at this point in the history
  • Loading branch information
0xlny authored and AbdelStark committed Oct 17, 2022
1 parent 5996c3f commit 15705ec
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/kakarot/instructions.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ namespace EVMInstructions {
add_instruction(instructions, 0x11, ComparisonOperations.exec_gt);
// 0x12 - SLT
add_instruction(instructions, 0x12, ComparisonOperations.exec_slt);
// 0x13 - SGT
add_instruction(instructions, 0x13, ComparisonOperations.exec_sgt);

// 0x52 - MSTORE
add_instruction(instructions, 0x52, MemoryOperations.exec_store);
Expand Down
38 changes: 38 additions & 0 deletions src/kakarot/instructions/comparison_operations.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace ComparisonOperations {
const GAS_COST_LT = 3;
const GAS_COST_GT = 3;
const GAS_COST_SLT = 3;
const GAS_COST_SGT = 3;
// @notice 0x10 - LT
// @dev Comparison operation
Expand Down Expand Up @@ -131,4 +132,41 @@ namespace ComparisonOperations {
let ctx = ExecutionContext.increment_gas_used(ctx, GAS_COST_SLT);
return ctx;
}

// @notice 0x13 - SGT
// @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_sgt{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
ctx: model.ExecutionContext*
) -> model.ExecutionContext* {
alloc_locals;
%{ print("0x13 - SGT") %}

let stack = ctx.stack;

// Stack input:
// 0 - a: left side integer.
// 1 - b: right side integer.
let (stack, a) = Stack.pop(stack);
let (stack, b) = Stack.pop(stack);

// Compute the comparison
let (result) = uint256_signed_lt(b, a);

// 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_SGT);
return ctx;
}
}
6 changes: 6 additions & 0 deletions tests/cases/003_sgt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "Comparison & bitwise logic operations - SGT",
"code": "60ff600113",
"calldata": "",
"expected_return_data": ""
}
13 changes: 12 additions & 1 deletion tests/units/kakarot/test_basic.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,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, 1);

// Load test case for SLT
// Load test case for SLT
let (evm_test_case: EVMTestCase) = test_utils.load_evm_test_case_from_file(
'./tests/cases/003_slt.json'
);
Expand All @@ -89,6 +89,17 @@ func test_comparison_operations{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*,
// Assert value on the top of the stack
test_utils.assert_top_stack(ctx, 1);

// Load test case for SGT
let (evm_test_case: EVMTestCase) = test_utils.load_evm_test_case_from_file(
'./tests/cases/003_sgt.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, 0);


return ();
}
Expand Down

0 comments on commit 15705ec

Please sign in to comment.