## Preamble EIP: 145 Title: Bitwise shifting instructions in EVM Author: Alex Beregszaszi, Paweł Bylica Type: Standard Track Category Core Status: Draft Created: 2017-02-13 ## Simple Summary To provide native bitwise shifting with cost on par with other arithmetic operations. ## Abstract Native bitwise shifting instructions are introduced, which are more efficient processing wise on the host and are cheaper to user by a contract. ## Motivation EVM is lacking bitwise shifting operators, but supports other logical and arithmetic operators. Shift operations can be implemented via arithmetic operators, but that has a higher cost and requires more processing time from the host. Implementing `SHL` and `SHR` using arithmetics cost each 35 gas, while the proposed instructions take 3 gas. ## Specification The following instructions are introduced: ### `0x1b`: `SHL` (shift left) The `SHL` instruction (shift left) pops 2 values from the stack, `arg1` and `arg2`, and pushes on the stack the first popped value `arg1` shifted to the left by the number of bits in the second popped value `arg2`. The result is equal to ``` (arg1 * 2^arg2) mod 2^256 ``` Notes: - If the shift amount is greater or equal 256 the result is 0. ### `0x1c`: `SHR` (logical shift right) The `SHR` instruction (logical shift right) pops 2 values from the stack, `arg1` and `arg2`, and pushes on the stack the first popped value `arg1` shifted to the right by the number of bits in the second popped value `arg2` with zero fill. The result is equal to ``` arg1 udiv 2^arg2 ``` Notes: - If the shift amount is greater or equal 256 the result is 0. ### `0x1d`: `SAR` (arithmetic shift right) The `SAR` instruction (arithmetic shift right) pops 2 values from the stack, `arg1` and `arg2`, and pushes on the stack the first popped value `arg1` shifted to the right by the number of bits in the second popped value `arg2` with sign extension. The result is equal to ``` arg1 sdiv 2^arg2 ``` Notes: - `arg2` is interpreted as unsigned number. - If the shift amount (`arg2`) is greater or equal 256 the result is 0 if `arg1` is non-negative or -1 if `arg1` is negative. The cost of the shift instructions is set at `verylow` tier (3 gas). ## Rationale Instruction operands were chosen to match the other logical and arithmetic instructions. ## Backwards Compatibility The newly introduced instructions have no effect on bytecode created in the past. ## Test Cases TBA ## Implementation Client support: - cpp-ethereum: https://github.com/ethereum/cpp-ethereum/pull/4054 Compiler support: - Solidity: https://github.com/ethereum/solidity/tree/asm-bitshift ## Copyright Copyright and related rights waived via [CC0](https://creativecommons.org/publicdomain/zero/1.0/).