Skip to content

Gas: Shift Right instead of Dividing by 2 #131

@code423n4

Description

@code423n4

Handle

Dravee

Vulnerability details

Impact

A division by 2 can be calculated by shifting one to the right.

While the DIV opcode uses 5 gas, the SHR opcode only uses 3 gas. Furthermore, Solidity's division operation also includes a division-by-0 prevention which is bypassed using shifting.

POC

Instances include:

contracts\XOLE.sol:230:            uint center = upper - (upper - lower) / 2;
contracts\XOLE.sol:418:            uint32 center = upper - (upper - lower) / 2;

Tools Used

VS Code

Recommended Mitigation Steps

Replace / 2 with >> 1

Metadata

Metadata

Assignees

No one assigned

    Labels

    G (Gas Optimization)bugSomething isn't workingsponsor acknowledgedTechnically the issue is correct, but we're not going to resolve it for XYZ reasons

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions