witnet / elliptic-curve-solidity

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

perf: remove sqrt by squaring on both sides #23

Closed tmpolaczyk closed 4 years ago

tmpolaczyk commented 4 years ago

The only affected method is _decomposeScalar, where the improvement is from 937567 average gas used to 62303 (15x).

Since _sqrt is not used anymore, I removed it. Is that ok?

Output of yarn gas-analysis:

Old:

········································|···························|·············|·····························
|  Methods                              ·               20 gwei/gas               ·       213.25 usd/eth       │
··················|·····················|·············|·············|·············|··············|··············
|  Contract       ·  Method             ·  Min        ·  Max        ·  Avg        ·  # calls     ·  usd (avg)  │
··················|·····················|·············|·············|·············|··············|··············
|  EcGasHelper    ·  _decomposeScalar   ·     653611  ·    1078579  ·     937567  ·         134  ·       4.00  │
··················|·····················|·············|·············|·············|··············|··············

New:

········································|···························|·············|·····························
|  Methods                              ·               20 gwei/gas               ·       186.43 usd/eth       │
··················|·····················|·············|·············|·············|··············|··············
|  Contract       ·  Method             ·  Min        ·  Max        ·  Avg        ·  # calls     ·  usd (avg)  │
··················|·····················|·············|·············|·············|··············|··············
|  EcGasHelper    ·  _decomposeScalar   ·      56171  ·      65801  ·      62303  ·         134  ·       0.23  │
··················|·····················|·············|·············|·············|··············|··············