Skip to content
This repository has been archived by the owner on Jan 9, 2025. It is now read-only.

Commit

Permalink
Add OOG to DUP ops
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementWalter committed Nov 3, 2023
1 parent babe8d4 commit 6348379
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 900 deletions.
32 changes: 16 additions & 16 deletions src/kakarot/evm.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -349,37 +349,37 @@ namespace EVM {
ret;
call PushOperations.exec_push32; // 0x7f
ret;
call DuplicationOperations.exec_dup1; // 0x80
call DuplicationOperations.exec_dup; // 0x80
ret;
call DuplicationOperations.exec_dup2; // 0x81
call DuplicationOperations.exec_dup; // 0x81
ret;
call DuplicationOperations.exec_dup3; // 0x82
call DuplicationOperations.exec_dup; // 0x82
ret;
call DuplicationOperations.exec_dup4; // 0x83
call DuplicationOperations.exec_dup; // 0x83
ret;
call DuplicationOperations.exec_dup5; // 0x84
call DuplicationOperations.exec_dup; // 0x84
ret;
call DuplicationOperations.exec_dup6; // 0x85
call DuplicationOperations.exec_dup; // 0x85
ret;
call DuplicationOperations.exec_dup7; // 0x86
call DuplicationOperations.exec_dup; // 0x86
ret;
call DuplicationOperations.exec_dup8; // 0x87
call DuplicationOperations.exec_dup; // 0x87
ret;
call DuplicationOperations.exec_dup9; // 0x88
call DuplicationOperations.exec_dup; // 0x88
ret;
call DuplicationOperations.exec_dup10; // 0x89
call DuplicationOperations.exec_dup; // 0x89
ret;
call DuplicationOperations.exec_dup11; // 0x8a
call DuplicationOperations.exec_dup; // 0x8a
ret;
call DuplicationOperations.exec_dup12; // 0x8b
call DuplicationOperations.exec_dup; // 0x8b
ret;
call DuplicationOperations.exec_dup13; // 0x8c
call DuplicationOperations.exec_dup; // 0x8c
ret;
call DuplicationOperations.exec_dup14; // 0x8d
call DuplicationOperations.exec_dup; // 0x8d
ret;
call DuplicationOperations.exec_dup15; // 0x8e
call DuplicationOperations.exec_dup; // 0x8e
ret;
call DuplicationOperations.exec_dup16; // 0x8f
call DuplicationOperations.exec_dup; // 0x8f
ret;
call ExchangeOperations.exec_swap1; // 0x90
ret;
Expand Down
315 changes: 21 additions & 294 deletions src/kakarot/instructions/duplication_operations.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,38 @@ namespace DuplicationOperations {
// @dev Duplicate the top i-th stack item to the top of the stack.
// @param ctx The pointer to the execution context.
// @return ExecutionContext Updated execution context.
func exec_dup_i{range_check_ptr}(
ctx: model.ExecutionContext*, i: felt
) -> model.ExecutionContext* {
func exec_dup{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
}(ctx: model.ExecutionContext*) -> model.ExecutionContext* {
alloc_locals;

let stack_underflow = is_le(ctx.stack.size, i - 1);
if (stack_underflow != 0) {
let (revert_reason_len, revert_reason) = Errors.stackUnderflow();
let out_of_gas = is_le(ctx.call_context.gas_limit, ctx.gas_used + GAS_COST_DUP - 1);
if (out_of_gas != 0) {
let (revert_reason_len, revert_reason) = Errors.outOfGas();
let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE);
return ctx;
}

if (ctx.stack.size == Constants.STACK_MAX_DEPTH) {
let (revert_reason_len, revert_reason) = Errors.stackOverflow();
let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE);
return ctx;
}

// See evm.cairo, pc is increased before entering the opcode
let opcode_number = [ctx.call_context.bytecode + ctx.program_counter - 1];
let i = opcode_number - 0x7F;

let stack_underflow = is_le(ctx.stack.size, i - 1);
if (stack_underflow != 0) {
let (revert_reason_len, revert_reason) = Errors.stackUnderflow();
let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE);
return ctx;
}

let (stack, element) = Stack.peek(ctx.stack, i - 1);
let stack = Stack.push(stack, element);

Expand All @@ -49,292 +64,4 @@ namespace DuplicationOperations {

return ctx;
}

// @notice DUP1 operation
// @dev Duplicate the top stack item to the top of the stack.
// @custom:since Frontier
// @custom:gas 3
// @custom:stack_consumed_elements 0
// @custom:stack_produced_elements 1
// @param ctx The pointer to the execution context.
// @return ExecutionContext Updated execution context.
func exec_dup1{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
}(ctx: model.ExecutionContext*) -> model.ExecutionContext* {
let ctx = exec_dup_i(ctx=ctx, i=1);
return ctx;
}

// @notice DUP2 operation
// @dev Duplicate the top 2nd stack item to the top of the stack.
// @custom:since Frontier
// @custom:gas 3
// @custom:stack_consumed_elements 0
// @custom:stack_produced_elements 1
// @param ctx The pointer to the execution context.
// @return ExecutionContext Updated execution context.
func exec_dup2{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
}(ctx: model.ExecutionContext*) -> model.ExecutionContext* {
let ctx = exec_dup_i(ctx=ctx, i=2);
return ctx;
}

// @notice DUP3 operation
// @dev Duplicate the top 3rd stack item to the top of the stack.
// @custom:since Frontier
// @custom:gas 3
// @custom:stack_consumed_elements 0
// @custom:stack_produced_elements 1
// @param ctx The pointer to the execution context.
// @return ExecutionContext Updated execution context.
func exec_dup3{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
}(ctx: model.ExecutionContext*) -> model.ExecutionContext* {
let ctx = exec_dup_i(ctx=ctx, i=3);
return ctx;
}

// @notice DUP4 operation
// @dev Duplicate the top 4th stack item to the top of the stack.
// @custom:since Frontier
// @custom:gas 3
// @custom:stack_consumed_elements 0
// @custom:stack_produced_elements 1
// @param ctx The pointer to the execution context.
// @return ExecutionContext Updated execution context.
func exec_dup4{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
}(ctx: model.ExecutionContext*) -> model.ExecutionContext* {
let ctx = exec_dup_i(ctx=ctx, i=4);
return ctx;
}

// @notice DUP5 operation
// @dev Duplicate the top 5th stack item to the top of the stack.
// @custom:since Frontier
// @custom:gas 3
// @custom:stack_consumed_elements 0
// @custom:stack_produced_elements 1
// @param ctx The pointer to the execution context.
// @return ExecutionContext Updated execution context.
func exec_dup5{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
}(ctx: model.ExecutionContext*) -> model.ExecutionContext* {
let ctx = exec_dup_i(ctx=ctx, i=5);
return ctx;
}

// @notice DUP6 operation
// @dev Duplicate the top 6th stack item to the top of the stack.
// @custom:since Frontier
// @custom:gas 3
// @custom:stack_consumed_elements 0
// @custom:stack_produced_elements 1
// @param ctx The pointer to the execution context.
// @return ExecutionContext Updated execution context.
func exec_dup6{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
}(ctx: model.ExecutionContext*) -> model.ExecutionContext* {
let ctx = exec_dup_i(ctx=ctx, i=6);
return ctx;
}

// @notice DUP7 operation
// @dev Duplicate the top 7th stack item to the top of the stack.
// @custom:since Frontier
// @custom:gas 3
// @custom:stack_consumed_elements 0
// @custom:stack_produced_elements 1
// @param ctx The pointer to the execution context.
// @return ExecutionContext Updated execution context.
func exec_dup7{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
}(ctx: model.ExecutionContext*) -> model.ExecutionContext* {
let ctx = exec_dup_i(ctx=ctx, i=7);
return ctx;
}

// @notice DUP8 operation
// @dev Duplicate the top 8th stack item to the top of the stack.
// @custom:since Frontier
// @custom:gas 3
// @custom:stack_consumed_elements 0
// @custom:stack_produced_elements 1
// @param ctx The pointer to the execution context.
// @return ExecutionContext Updated execution context.
func exec_dup8{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
}(ctx: model.ExecutionContext*) -> model.ExecutionContext* {
let ctx = exec_dup_i(ctx=ctx, i=8);
return ctx;
}

// @notice DUP9 operation
// @dev Duplicate the top 9th stack item to the top of the stack.
// @custom:since Frontier
// @custom:gas 3
// @custom:stack_consumed_elements 0
// @custom:stack_produced_elements 1
// @param ctx The pointer to the execution context.
// @return ExecutionContext Updated execution context.
func exec_dup9{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
}(ctx: model.ExecutionContext*) -> model.ExecutionContext* {
let ctx = exec_dup_i(ctx=ctx, i=9);
return ctx;
}

// @notice DUP10 operation
// @dev Duplicate the top 10th stack item to the top of the stack.
// @custom:since Frontier
// @custom:gas 3
// @custom:stack_consumed_elements 0
// @custom:stack_produced_elements 1
// @param ctx The pointer to the execution context.
// @return ExecutionContext Updated execution context.
func exec_dup10{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
}(ctx: model.ExecutionContext*) -> model.ExecutionContext* {
let ctx = exec_dup_i(ctx=ctx, i=10);
return ctx;
}

// @notice DUP11 operation
// @dev Duplicate the top 11th stack item to the top of the stack.
// @custom:since Frontier
// @custom:gas 3
// @custom:stack_consumed_elements 0
// @custom:stack_produced_elements 1
// @param ctx The pointer to the execution context.
// @return ExecutionContext Updated execution context.
func exec_dup11{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
}(ctx: model.ExecutionContext*) -> model.ExecutionContext* {
let ctx = exec_dup_i(ctx=ctx, i=11);
return ctx;
}

// @notice DUP12 operation
// @dev Duplicate the top 12th stack item to the top of the stack.
// @custom:since Frontier
// @custom:gas 3
// @custom:stack_consumed_elements 0
// @custom:stack_produced_elements 1
// @param ctx The pointer to the execution context.
// @return ExecutionContext Updated execution context.
func exec_dup12{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
}(ctx: model.ExecutionContext*) -> model.ExecutionContext* {
let ctx = exec_dup_i(ctx=ctx, i=12);
return ctx;
}

// @notice DUP13 operation
// @dev Duplicate the top 13th stack item to the top of the stack.
// @custom:since Frontier
// @custom:gas 3
// @custom:stack_consumed_elements 0
// @custom:stack_produced_elements 1
// @param ctx The pointer to the execution context.
// @return ExecutionContext Updated execution context.
func exec_dup13{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
}(ctx: model.ExecutionContext*) -> model.ExecutionContext* {
let ctx = exec_dup_i(ctx=ctx, i=13);
return ctx;
}

// @notice DUP14 operation
// @dev Duplicate the top 14th stack item to the top of the stack.
// @custom:since Frontier
// @custom:gas 3
// @custom:stack_consumed_elements 0
// @custom:stack_produced_elements 1
// @param ctx The pointer to the execution context.
// @return ExecutionContext Updated execution context.
func exec_dup14{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
}(ctx: model.ExecutionContext*) -> model.ExecutionContext* {
let ctx = exec_dup_i(ctx=ctx, i=14);
return ctx;
}

// @notice DUP15 operation
// @dev Duplicate the top 15th stack item to the top of the stack.
// @custom:since Frontier
// @custom:gas 3
// @custom:stack_consumed_elements 0
// @custom:stack_produced_elements 1
// @param ctx The pointer to the execution context.
// @return ExecutionContext Updated execution context.
func exec_dup15{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
}(ctx: model.ExecutionContext*) -> model.ExecutionContext* {
let ctx = exec_dup_i(ctx=ctx, i=15);
return ctx;
}

// @notice DUP16 operation
// @dev Duplicate the top 16th stack item to the top of the stack.
// @custom:since Frontier
// @custom:gas 3
// @custom:stack_consumed_elements 0
// @custom:stack_produced_elements 1
// @param ctx The pointer to the execution context.
// @return ExecutionContext Updated execution context.
func exec_dup16{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
}(ctx: model.ExecutionContext*) -> model.ExecutionContext* {
let ctx = exec_dup_i(ctx=ctx, i=16);
return ctx;
}
}
Loading

0 comments on commit 6348379

Please sign in to comment.