code-423n4 / 2021-05-fairside-findings

0 stars 0 forks source link

Gas optimization for the `rootPows` function in `FairSideFormula` #71

Open code423n4 opened 3 years ago

code423n4 commented 3 years ago

Handle

shw

Vulnerability details

Impact

Gas optimization is possible for the current rootPows implementation.

Proof of Concept

The original implementation of rootPows requires 4 mul and 2 sqrt:

function rootPows(bytes16 x) private pure returns (bytes16, bytes16) {
    // fourth root
    x = x.sqrt().sqrt();
    // to the power of 3
    x = _pow3(x);
    // we offset the root on the second arg
    return (x, x.mul(x));
}

However, the calculation process can be simplified to be more gas-efficient than the original with only 1 mul and 2 sqrt requried:

function rootPows(bytes16 x) private pure returns (bytes16, bytes16) {
    bytes16 x1_2 = x.sqrt();
    bytes16 x3_2 = x.mul(x1_2);
    bytes16 x3_4 = x3_2.sqrt();
    return (x3_4, x3_2);
}

Referenced code: FairSideFormula.sol#L67-L75

Recommended Mitigation Steps

To save gas, change the implementation of rootPows as mentioned above.

fairside-core commented 3 years ago

Optimization is confirmed (basically constructs x^3/2 then applies root on it). Given that this is a gas optimization, perhaps the severity should be noted down to 1? I'll leave this up to the judges.

fairside-core commented 3 years ago

Fixed in PR#6.

cemozerr commented 3 years ago

Labeling this as gas optimization.