Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gas optimization in XDEFIDistribution.sol - shifting instead of multiplying or dividing by power of 2 #122

Open
code423n4 opened this issue Jan 6, 2022 · 3 comments
Labels
bug Something isn't working G (Gas Optimization) resolved Finding has been patched by sponsor (sponsor pls link to PR containing fix) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")

Comments

@code423n4
Copy link
Contributor

Handle

OriDabush

Vulnerability details

XDEFIDistribution.sol lines 151, 338-344

Instead of multiplying by _pointsMultiplier, which is 2 ** 128, it is more efficient to shift by 128 (x * (2 ** 128) = x << 128), same for dividing (x / (2 ** 128) = x >> 128)

// line 151 - old
_pointsPerUnit += ((newXDEFI * _pointsMultiplier) / totalUnitsCached);

// line 151 - new
_pointsPerUnit += ((newXDEFI << 128) / totalUnitsCached);


// lines 338-344 - old
return
(
    _toUint256Safe(
        _toInt256Safe(_pointsPerUnit * uint256(units_)) +
        pointsCorrection_
    ) / _pointsMultiplier
) + uint256(depositedXDEFI_);

// lines 338-344 - new
return
(
    _toUint256Safe(
        _toInt256Safe(_pointsPerUnit * uint256(units_)) +
        pointsCorrection_
    ) >> 128
) + uint256(depositedXDEFI_);
@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Jan 6, 2022
code423n4 added a commit that referenced this issue Jan 6, 2022
@deluca-mike deluca-mike added the sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity") label Jan 8, 2022
@deluca-mike
Copy link
Collaborator

Yup, this is valid. Will do.

@deluca-mike
Copy link
Collaborator

In release candidate contract, _pointsMultiplier (now POINTS_MULTIPLIER_BITS) is defined as a number of bits to shift, and shifting is occuring (instead of multiplication) in updateDistribution, and instead of division in _withdrawableGiven.

@deluca-mike deluca-mike added the resolved Finding has been patched by sponsor (sponsor pls link to PR containing fix) label Jan 14, 2022
@Ivshti
Copy link
Member

Ivshti commented Jan 16, 2022

that's a good one!

@Ivshti Ivshti closed this as completed Jan 16, 2022
@CloudEllie CloudEllie reopened this Jan 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working G (Gas Optimization) resolved Finding has been patched by sponsor (sponsor pls link to PR containing fix) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")
Projects
None yet
Development

No branches or pull requests

4 participants