lambdaclass / zksync_era_precompiles

Yul precompile library to speedup elliptic curves operations.
Apache License 2.0
51 stars 19 forks source link

`g2ProjectiveFromAffine` does not have to make final check. #241

Closed IAvecilla closed 6 months ago

IAvecilla commented 7 months ago

Context: EcPairing.yul#L383

Description:

g2ProjectiveFromAffine forQ point from G2 is used in only one place in millerLoop. Q point is never infinity because it’s checked before pair function call here Checking infinity again in g2ProjectiveFromAffine is not necessary. Moreover the function name is wrong because assuming the way it works for infinity point it should return point (0,1,0) for protective coordinates and (l^2, l^3, 0) for Jacobian coordinates.

Recommendation:

Remove unnecessary check in the end of this function.

diff --git a/precompiles/EcPairing.yul b/precompiles/EcPairing.yul
index d9e08c5..0961c95 100644
--- a/precompiles/EcPairing.yul
+++ b/precompiles/EcPairing.yul
@@ -413,16 +413,6 @@ object "EcPairing" {
                yr1 := yp1
                zr0 := MONTGOMERY_ONE()
                zr1 := 0
-               if and(eq(xp0, 0), eq(xp1, 0)) {
-                   if and(eq(yp0, 0), eq(yp1, 0)) {
-                       xr0 := MONTGOMERY_ONE()
-                       // xr1 is already 0
-                       yr0 := MONTGOMERY_ONE()
-                       // yr1 is already 0
-                       zr0 := 0
-                       // zr1 is already 0
-                   }
-               }
            }

             /// @notice Checks if a G2 point in affine coordinates is the point at infinity.

GAS savings for ecpairing_two_point_match_3: before: 9179324 after: 9177304

zkSync:

Spearbit: