rdubois-crypto / FreshCryptoLib

Cryptographic Primitives for Blockchain Systems (solidity, cairo, C and rust)
MIT License
124 stars 22 forks source link

Reduced gas cost ( -3167gas ) #15

Closed jayden-sudo closed 11 months ago

jayden-sudo commented 11 months ago

With the code equivalent, executing ecZZ_mulmuladd_S_asm reduced gas cost by 3167 gas:

Gas
Before 207633 gas
After 204466 gas

list:

  1. (uint[2] calldata rs,uint[2] calldata Q) -> (uint256 r,uint256 s,uint256 Qx,uint256 Qy) save 393 gas

  2. if (rs[0] == 0 || rs[0] >= n || rs[1] == 0||rs[1]>=n) -> iszero(mul(mul(r, s), and(gt(n, r), gt(n, s)))) save 118 gas

  3. Update ecAff_isOnCurve save 176 gas

  4. uint[6] memory -> let pointer := mload(0x40) save 135 gas

  5. if eq(y2,0) -> if iszero(y2) save 1170 gas

    ... and more

jayden-sudo commented 11 months ago

Additionally, Why did I submit code in the test path: The current code does not have a code format. In order to make it easier to review the code diffs in the submitted code, I did not format the code. However, for the sake of maintainability, I hope we can unify the style of the formal code (https://github.com/rdubois-crypto/FreshCryptoLib/blob/master/solidity) before making a formal code PR, for example, by using the "solidity.formatter": "forge" plugin. I hope this suggestion can be considered. Thank you.

rdubois-crypto commented 11 months ago

Those lines seems false:

Please do not modify API, as the function are integrated elsewhere.

Could you use forge environment please ? There are tests vectors not assesed in hardhat (hardhat deprecated now).

jayden-sudo commented 11 months ago

got! thanks