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: Bitmasks creation can be simplified #21

Open
code423n4 opened this issue Oct 13, 2021 · 2 comments
Open

Gas: Bitmasks creation can be simplified #21

code423n4 opened this issue Oct 13, 2021 · 2 comments
Assignees
Labels
bug Warden finding 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

cmichel

Vulnerability details

In DrawCalculator._createBitMasks, the bit masks can just be created by shifting the previous (potentially already shifted) masks by the bit range.
It saves on multiplications and, for me, this is also more intuitive than the current algorithm.

Some pseudocode:

function _createBitMasks(IPrizeDistributionBuffer.PrizeDistribution memory _prizeDistribution)
    internal
    pure
    returns (uint256[] memory)
{
    uint256[] memory masks = new uint256[](_prizeDistribution.matchCardinality);
    uint256 _bitRangeMaskValue = (2**_prizeDistribution.bitRangeSize) - 1; // get a decimal representation of bitRangeSize, for example 0xF for bitRangeSize = 4

    if(_prizeDistribution.matchCardinality == 0) return masks;

    masks[0] = _bitRangeMaskValue;
    for (uint8 maskIndex = 1; maskIndex < _prizeDistribution.matchCardinality; maskIndex++) {
        // shift by the "size" of the bit mask each time, 0xF, 0xF0, 0xF00, 0xF000, etc.
        masks[maskIndex] = masks[maskIndex - 1] << _prizeDistribution.bitRangeSize;
    }

    return masks;
}
code423n4 added a commit that referenced this issue Oct 13, 2021
@asselstine asselstine self-assigned this Oct 13, 2021
@asselstine asselstine added the sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity") label Oct 13, 2021
@asselstine asselstine added resolved Finding has been patched by sponsor (sponsor pls link to PR containing fix) and removed sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity") labels Oct 14, 2021
@PierrickGT
Copy link
Member

PR: pooltogether/v4-core#215

@PierrickGT PierrickGT added the sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity") label Oct 14, 2021
@GalloDaSballo
Copy link
Collaborator

The sponsor has implemented the optimization in a subsequent PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Warden finding 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