code-423n4 / 2022-01-insure-findings

2 stars 0 forks source link

uint8 is cheaper than uint128 #316

Closed code423n4 closed 2 years ago

code423n4 commented 2 years ago

Handle

defsec

Vulnerability details

Impact

In the vault contract, there is only loop is forwarded only for two times. Therefore, uint8 can be used for the loop.

Proof of Concept

  1. Navigate to the following contract.

https://github.com/code-423n4/2022-01-insure/blob/19d1a7819fe7ce795e6d4814e7ddf8b8e1323df3/contracts/Vault.sol#L109

        totalAttributions += _attributions;
        for (uint128 i = 0; i < 2; i++) {
            uint256 _allocation = (_shares[i] * _attributions) / MAGIC_SCALE_1E6;
            attributions[_beneficiaries[i]] += _allocation;
            _allocations[i] = _allocation;
        }

Tools Used

None

Recommended Mitigation Steps

Replace the uint128 with uint8 for the gas optimization.

oishun1112 commented 2 years ago
function test_1()external pure{
        for (uint256 i = 0; i < 100; i++) {
            uint256 a;
            a+=1;
        }
    }

    function test_2()external pure{
        for (uint128 i = 0; i < 100; i++) {
            uint256 a;
            a+=1;
        }
    }

    function test_3()external pure{
        for (uint8 i = 0; i < 100; i++) {
            uint256 a;
            a+=1;
        }
    }

test_1 = 59818 gas test_2 = 61046 gas test_3 = 61068 gas

oishun1112 commented 2 years ago

https://github.com/code-423n4/2022-01-insure-findings/issues/62

0xean commented 2 years ago

closing as invalid.