# `EEVM.Interpreter.Instructions.Arithmetic`
[🔗](https://github.com/mw2000/eevm/blob/v0.1.0/lib/eevm/interpreter/instructions/arithmetic.ex#L1)

EVM arithmetic opcodes: ADD, MUL, SUB, DIV, SDIV, MOD, SMOD, ADDMOD, MULMOD, EXP, SIGNEXTEND.

# `execute`

```elixir
@spec execute(non_neg_integer(), EEVM.Interpreter.MachineState.t()) ::
  {:ok, EEVM.Interpreter.MachineState.t()}
  | {:error, atom(), EEVM.Interpreter.MachineState.t()}
```

Executes EXP (0x0A), which charges `Gas.Dynamic.exp_dynamic_cost/2` for its
exponent on top of the static cost (EIP-160).

Returns `{:ok, new_state}` on success, `{:error, reason, state}` on failure.

# `stack_op`

```elixir
@spec stack_op(non_neg_integer(), [non_neg_integer()]) :: [non_neg_integer()]
```

Applies an arithmetic opcode to the stack (a list, top first) and returns the new stack.

| Byte | Mnemonic  | Operation                          |
|------|-----------|------------------------------------|
| 0x01 | ADD       | `(a + b) mod 2^256`                |
| 0x02 | MUL       | `(a * b) mod 2^256`                |
| 0x03 | SUB       | `(a - b) mod 2^256`                |
| 0x04 | DIV       | `a / b` (0 if b == 0)              |
| 0x05 | SDIV      | signed division (0 if b == 0)      |
| 0x06 | MOD       | `a mod b` (0 if b == 0)            |
| 0x07 | SMOD      | signed modulo (0 if b == 0)        |
| 0x08 | ADDMOD    | `(a + b) mod n` (0 if n == 0)      |
| 0x09 | MULMOD    | `(a * b) mod n` (0 if n == 0)      |
| 0x0B | SIGNEXTEND| sign-extend x from byte width b    |

The interpreter has already checked that the stack holds the opcode's inputs.
EXP is not here: its gas depends on the exponent, so it goes through `execute/2`.

    iex> EEVM.Interpreter.Instructions.Arithmetic.stack_op(0x03, [1, 3, 7])
    [115792089237316195423570985008687907853269984665640564039457584007913129639934, 7]

---

*Consult [api-reference.md](api-reference.md) for complete listing*
