From a943121a74b080308cec6fc903dad301f1c534f5 Mon Sep 17 00:00:00 2001 From: Artem Chystiakov Date: Mon, 4 Nov 2024 13:08:07 +0200 Subject: [PATCH 1/2] small optimization --- contracts/curves/EllipticCurve.sol | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/contracts/curves/EllipticCurve.sol b/contracts/curves/EllipticCurve.sol index 6e798af8..fa3f4f14 100644 --- a/contracts/curves/EllipticCurve.sol +++ b/contracts/curves/EllipticCurve.sol @@ -297,7 +297,7 @@ contract EllipticCurve { uint base2Y = y0; uint base2Z = 1; - for(uint i = 0; i < exp; i++) { + for(uint i = 0; i < exp; ++i) { (base2X, base2Y, base2Z) = twiceProj(base2X, base2Y, base2Z); } @@ -327,7 +327,7 @@ contract EllipticCurve { x1 = x0; y1 = y0; - if(scalar%2 == 0) { + if(scalar & 1 == 0) { x1 = y1 = 0; } @@ -336,7 +336,7 @@ contract EllipticCurve { while(scalar > 0) { (base2X, base2Y, base2Z) = twiceProj(base2X, base2Y, base2Z); - if(scalar%2 == 1) { + if(scalar & 1 == 1) { (x1, y1, z1) = addProj(base2X, base2Y, base2Z, x1, y1, z1); } @@ -374,19 +374,14 @@ contract EllipticCurve { uint x2; uint y1; uint y2; + uint px; + uint py; uint sInv = inverseMod(rs[1], n); (x1, y1) = multiplyScalar(gx, gy, mulmod(uint(message), sInv, n)); (x2, y2) = multiplyScalar(Q[0], Q[1], mulmod(rs[0], sInv, n)); - uint[3] memory P = addAndReturnProjectivePoint(x1, y1, x2, y2); + (px, py) = add(x1, y1, x2, y2); - if (P[2] == 0) { - return false; - } - - uint Px = inverseMod(P[2], p); - Px = mulmod(P[0], mulmod(Px, Px, p), p); - - return Px % n == rs[0]; + return px == rs[0]; } -} \ No newline at end of file +} From 5cf8fa546ef3d188883ac478ce9ef4f14d84e58f Mon Sep 17 00:00:00 2001 From: Artem Chystiakov Date: Thu, 7 Nov 2024 18:53:49 +0200 Subject: [PATCH 2/2] fix zero proj --- contracts/curves/EllipticCurve.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/curves/EllipticCurve.sol b/contracts/curves/EllipticCurve.sol index fa3f4f14..cfebfa10 100644 --- a/contracts/curves/EllipticCurve.sol +++ b/contracts/curves/EllipticCurve.sol @@ -95,7 +95,7 @@ contract EllipticCurve { function zeroProj() public pure returns (uint x, uint y, uint z) { - return (0, 1, 0); + return (0, 0, 1); } /**