witnet / elliptic-curve-solidity

Elliptic Curve arithmetic operations written in Solidity
MIT License
168 stars 41 forks source link

refactor: reduce gas cost in multiple functions #20

Closed aesedepece closed 4 years ago

aesedepece commented 4 years ago

as suggested by red4sec

Before

·---------------------------------------|---------------------------|-------------|----------------------------·
|  Solc version: 0.6.4+commit.1dca32f3  ·  Optimizer enabled: true  ·  Runs: 200  ·  Block limit: 6721975 gas  │
········································|···························|·············|·····························
|  Methods                              ·               20 gwei/gas               ·       213.17 usd/eth       │
··················|·····················|·············|·············|·············|··············|··············
|  Contract       ·  Method             ·  Min        ·  Max        ·  Avg        ·  # calls     ·  usd (avg)  │
··················|·····················|·············|·············|·············|··············|··············
|  EcGasHelper    ·  _decomposeScalar   ·     653611  ·    1078579  ·     937567  ·         134  ·       4.00  │
··················|·····················|·············|·············|·············|··············|··············
|  EcGasHelper    ·  _deriveY           ·      45275  ·      55545  ·      50410  ·           4  ·       0.21  │
··················|·····················|·············|·············|·············|··············|··············
|  EcGasHelper    ·  _ecAdd             ·      42354  ·      56310  ·      49321  ·         468  ·       0.21  │
··················|·····················|·············|·············|·············|··············|··············
|  EcGasHelper    ·  _ecInv             ·      22906  ·      23074  ·      22990  ·           2  ·       0.10  │
··················|·····················|·············|·············|·············|··············|··············
|  EcGasHelper    ·  _ecMul             ·      25312  ·     687334  ·     385930  ·         561  ·       1.65  │
··················|·····················|·············|·············|·············|··············|··············
|  EcGasHelper    ·  _ecSimMul          ·      76873  ·     511133  ·     256045  ·         125  ·       1.09  │
··················|·····················|·············|·············|·············|··············|··············
|  EcGasHelper    ·  _ecSub             ·      42621  ·      56223  ·      49704  ·         228  ·       0.21  │
··················|·····················|·············|·············|·············|··············|··············
|  EcGasHelper    ·  _invMod            ·      22158  ·      49260  ·      39632  ·          12  ·       0.17  │
··················|·····················|·············|·············|·············|··············|··············
|  EcGasHelper    ·  _isOnCurve         ·      23587  ·      24065  ·      23829  ·           8  ·       0.10  │
··················|·····················|·············|·············|·············|··············|··············
|  EcGasHelper    ·  _toAffine          ·      50150  ·      50855  ·      50503  ·           4  ·       0.22  │
··················|·····················|·············|·············|·············|··············|··············
|  Deployments                          ·                                         ·  % of limit  ·             │
········································|·············|·············|·············|··············|··············
|  EcGasHelper                          ·          -  ·          -  ·    1565698  ·      23.3 %  ·       6.68  │
·---------------------------------------|-------------|-------------|-------------|--------------|-------------·

After

·---------------------------------------|---------------------------|-------------|----------------------------·
|  Solc version: 0.6.4+commit.1dca32f3  ·  Optimizer enabled: true  ·  Runs: 200  ·  Block limit: 6721975 gas  │
········································|···························|·············|·····························
|  Methods                              ·               20 gwei/gas               ·       185.78 usd/eth       │
··················|·····················|·············|·············|·············|··············|··············
|  Contract       ·  Method             ·  Min        ·  Max        ·  Avg        ·  # calls     ·  usd (avg)  │
··················|·····················|·············|·············|·············|··············|··············
|  EcGasHelper    ·  _decomposeScalar   ·     653251  ·    1078177  ·     937203  ·         134  ·       3.48  │
··················|·····················|·············|·············|·············|··············|··············
|  EcGasHelper    ·  _deriveY           ·      45275  ·      55545  ·      50410  ·           4  ·       0.19  │
··················|·····················|·············|·············|·············|··············|··············
|  EcGasHelper    ·  _ecAdd             ·      24308  ·      56306  ·      49103  ·         472  ·       0.18  │
··················|·····················|·············|·············|·············|··············|··············
|  EcGasHelper    ·  _ecInv             ·      22906  ·      23074  ·      22990  ·           2  ·       0.09  │
··················|·····················|·············|·············|·············|··············|··············
|  EcGasHelper    ·  _ecMul             ·      24911  ·     619772  ·     349212  ·         561  ·       1.30  │
··················|·····················|·············|·············|·············|··············|··············
|  EcGasHelper    ·  _ecSimMul          ·      76346  ·     486397  ·     243105  ·         125  ·       0.90  │
··················|·····················|·············|·············|·············|··············|··············
|  EcGasHelper    ·  _ecSub             ·      42617  ·      56219  ·      49700  ·         228  ·       0.18  │
··················|·····················|·············|·············|·············|··············|··············
|  EcGasHelper    ·  _invMod            ·      22153  ·      49255  ·      39627  ·          12  ·       0.15  │
··················|·····················|·············|·············|·············|··············|··············
|  EcGasHelper    ·  _isOnCurve         ·      23587  ·      24065  ·      23829  ·           8  ·       0.09  │
··················|·····················|·············|·············|·············|··············|··············
|  EcGasHelper    ·  _toAffine          ·      50145  ·      50850  ·      50498  ·           4  ·       0.19  │
··················|·····················|·············|·············|·············|··············|··············
|  Deployments                          ·                                         ·  % of limit  ·             │
········································|·············|·············|·············|··············|··············
|  EcGasHelper                          ·          -  ·          -  ·    1541666  ·      22.9 %  ·       5.73  │
·---------------------------------------|-------------|-------------|-------------|--------------|-------------·
aesedepece commented 4 years ago

4 new commits have been added.