extend tests

Signed-off-by: Uncle Fatso <uncle.fatso@ghostchain.io>
This commit is contained in:
Uncle Fatso 2025-10-28 21:31:05 +03:00
parent ee2eb2c805
commit 98031ae4c5
Signed by: f4ts0
GPG Key ID: 565F4F2860226EBB
3 changed files with 362 additions and 45 deletions

View File

@ -3,6 +3,7 @@ pragma solidity ^0.8.0;
import {EllipticCurve} from "./libraries/ECMath.sol";
import {EllipticCurveProjective} from "./libraries/ECMathProjective.sol";
import {GhostEllipticCurves} from "./libraries/GhostEllipticCurves.sol";
contract MathTester {
// Constants are taken from https://en.bitcoin.it/wiki/Secp256k1
@ -29,6 +30,12 @@ contract MathTester {
return EllipticCurveProjective.projectiveAdd(x1, y1, 1, x2, y2, 1);
}
function addGhost(
uint256 x1, uint256 y1, uint256 x2, uint256 y2
) public pure returns (uint256, uint256, uint256) {
return GhostEllipticCurves.projectiveAdd(x1, y1, 1, x2, y2, 1);
}
function doubleJacobian(uint256 x1, uint256 y1) public pure returns (uint256, uint256, uint256) {
return EllipticCurve.jacDouble(x1, y1, 1, A, P);
}
@ -37,7 +44,11 @@ contract MathTester {
return EllipticCurveProjective.projectiveDouble(x1, y1, 1);
}
function addProjectiveMixed(
function doubleGhost(uint256 x1, uint256 y1) public pure returns (uint256, uint256, uint256) {
return GhostEllipticCurves.projectiveDouble(x1, y1, 1);
}
function addMixedProjective(
uint256 x1,
uint256 y1,
uint256 x2,
@ -46,16 +57,16 @@ contract MathTester {
return EllipticCurveProjective.projectiveAddMixed(x1, y1, 1, x2, y2);
}
function mulEcTriplet(
uint256 x1,
uint256 y1,
uint256 k1,
uint256 x2,
uint256 y2,
uint256 k2,
uint256 x3,
uint256 y3,
uint256 k3
function addMixedGhost(
uint256 x1, uint256 y1, uint256 x2, uint256 y2
) public pure returns (uint256, uint256, uint256) {
return GhostEllipticCurves.projectiveAddMixed(x1, y1, 1, x2, y2);
}
function mulTripletEc(
uint256 x1, uint256 y1, uint256 k1,
uint256 x2, uint256 y2, uint256 k2,
uint256 x3, uint256 y3, uint256 k3
) public pure returns(uint256, uint256) {
(x1, y1) = EllipticCurve.ecMul(k1, x1, y1, A, P);
(x2, y2) = EllipticCurve.ecMul(k2, x2, y2, A, P);
@ -67,27 +78,17 @@ contract MathTester {
return (x1, y1);
}
function mulProjectiveTriplet(
uint256 x1,
uint256 y1,
uint256 k1,
uint256 x2,
uint256 y2,
uint256 k2,
uint256 x3,
uint256 y3,
uint256 k3
function mulTripletProjective(
uint256 x1, uint256 y1, uint256 k1,
uint256 x2, uint256 y2, uint256 k2,
uint256 x3, uint256 y3, uint256 k3
) public pure returns(uint256, uint256, uint256) {
return EllipticCurveProjective.mulAddProjectiveTriplet(x1, y1, k1, x2, y2, k2, x3, y3, k3);
}
function mulEcPair(
uint256 x1,
uint256 y1,
uint256 k1,
uint256 x2,
uint256 y2,
uint256 k2
function mulPairEc(
uint256 x1, uint256 y1, uint256 k1,
uint256 x2, uint256 y2, uint256 k2
) public pure returns(uint256, uint256) {
(x1, y1) = EllipticCurve.ecMul(k1, x1, y1, A, P);
(x2, y2) = EllipticCurve.ecMul(k2, x2, y2, A, P);
@ -95,18 +96,39 @@ contract MathTester {
return (x1, y1);
}
function mulProjectivePair(
uint256 x1,
uint256 y1,
uint256 k1,
uint256 x2,
uint256 y2,
uint256 k2
function mulPairProjective(
uint256 x1, uint256 y1, uint256 k1,
uint256 x2, uint256 y2, uint256 k2
) public pure returns(uint256, uint256, uint256) {
return EllipticCurveProjective.mulAddProjectivePair(x1, y1, k1, x2, y2, k2);
return EllipticCurveProjective.mulAddProjectivePair(x1, y1, 1, k1, x2, y2, 1, k2);
}
function mulEcQuartet(
function mulPairGhost(
uint256 x1, uint256 y1, uint256 k1,
uint256 x2, uint256 y2, uint256 k2
) public pure returns(uint256, uint256, uint256) {
return GhostEllipticCurves.mulAddAffinePair(x1, y1, k1, x2, y2, k2);
}
function mulSingleEc(
uint256 x1, uint256 y1, uint256 k1
) public pure returns (uint256, uint256) {
return EllipticCurve.ecMul(k1, x1, y1, A, P);
}
function mulSingleProjective(
uint256 x1, uint256 y1, uint256 k1
) public pure returns (uint256, uint256, uint256) {
return EllipticCurveProjective.mulAddProjectiveSingle(x1, y1, k1);
}
function mulSingleGhost(
uint256 x1, uint256 y1, uint256 k1
) public pure returns (uint256, uint256, uint256) {
return GhostEllipticCurves.mulAddAffineSingle(x1, y1, k1);
}
function mulQuartetEc(
uint256 x1, uint256 y1, uint256 k1,
uint256 x2, uint256 y2, uint256 k2,
uint256 x3, uint256 y3, uint256 k3,
@ -124,7 +146,7 @@ contract MathTester {
return (x1, y1);
}
function mulProjectiveQuartet(
function mulQuartetProjective(
uint256 x1, uint256 y1, uint256 k1,
uint256 x2, uint256 y2, uint256 k2,
uint256 x3, uint256 y3, uint256 k3,
@ -146,7 +168,15 @@ contract MathTester {
return EllipticCurveProjective.toAffine(x, y, z);
}
function toAffineGhost(uint256 x, uint256 y, uint256 z) public pure returns (uint256, uint256) {
return GhostEllipticCurves.toAffine(x, y, z);
}
function isOnCurve(uint256 x, uint256 y) public pure returns (bool) {
return EllipticCurveProjective.isOnCurve(x, y);
}
function isOnCurveGhost(uint256 x, uint256 y) public pure returns (bool) {
return GhostEllipticCurves.isOnCurve(x, y);
}
}

209
test/GhostVerifier.t.sol Normal file

File diff suppressed because one or more lines are too long

View File

@ -22,10 +22,44 @@ contract MathTesterTest is Test {
points = abi.decode(data, (Point[]));
}
function test_single() public view {
uint256 len = points.length - 1;
for (uint256 i; i < len;) {
(uint256 x_p, uint256 y_p, uint256 z_p) = math.mulSingleProjective(
uint256(points[i].x),
uint256(points[i].y),
uint256(points[i].k)
);
(x_p, y_p) = math.toAffineProjective(x_p, y_p, z_p);
(uint256 x_g, uint256 y_g, uint256 z_g) = math.mulSingleGhost(
uint256(points[i].x),
uint256(points[i].y),
uint256(points[i].k)
);
(x_g, y_g) = math.toAffineGhost(x_g, y_g, z_g);
(uint256 x_j, uint256 y_j) = math.mulSingleEc(
uint256(points[i].x),
uint256(points[i].y),
uint256(points[i].k)
);
assertEq(x_p, x_j);
assertEq(x_p, x_g);
assertEq(y_p, y_j);
assertEq(y_p, y_g);
assertEq(math.isOnCurve(x_p, y_p), true);
assertEq(math.isOnCurveGhost(x_p, y_p), true);
unchecked { ++i; }
}
}
function test_quartet() public view {
uint256 len = points.length - 3;
for (uint256 i; i < len;) {
(uint256 x_p, uint256 y_p, uint256 z_p) = math.mulProjectiveQuartet(
(uint256 x_p, uint256 y_p, uint256 z_p) = math.mulQuartetProjective(
uint256(points[i].x),
uint256(points[i].y),
uint256(points[i].k),
@ -41,7 +75,7 @@ contract MathTesterTest is Test {
);
(x_p, y_p) = math.toAffineProjective(x_p, y_p, z_p);
(uint256 x_j, uint256 y_j) = math.mulEcQuartet(
(uint256 x_j, uint256 y_j) = math.mulQuartetEc(
uint256(points[i].x),
uint256(points[i].y),
uint256(points[i].k),
@ -59,6 +93,7 @@ contract MathTesterTest is Test {
assertEq(x_p, x_j);
assertEq(y_p, y_j);
assertEq(math.isOnCurve(x_p, y_p), true);
assertEq(math.isOnCurveGhost(x_p, y_p), true);
unchecked { ++i; }
}
@ -67,7 +102,7 @@ contract MathTesterTest is Test {
function test_pair() public view {
uint256 len = points.length - 1;
for (uint256 i; i < len;) {
(uint256 x_p, uint256 y_p, uint256 z_p) = math.mulProjectivePair(
(uint256 x_p, uint256 y_p, uint256 z_p) = math.mulPairProjective(
uint256(points[i].x),
uint256(points[i].y),
uint256(points[i].k),
@ -77,7 +112,17 @@ contract MathTesterTest is Test {
);
(x_p, y_p) = math.toAffineProjective(x_p, y_p, z_p);
(uint256 x_j, uint256 y_j) = math.mulEcPair(
(uint256 x_g, uint256 y_g, uint256 z_g) = math.mulPairGhost(
uint256(points[i].x),
uint256(points[i].y),
uint256(points[i].k),
uint256(points[i+1].x),
uint256(points[i+1].y),
uint256(points[i+1].k)
);
(x_g, y_g) = math.toAffineGhost(x_g, y_g, z_g);
(uint256 x_j, uint256 y_j) = math.mulPairEc(
uint256(points[i].x),
uint256(points[i].y),
uint256(points[i].k),
@ -87,8 +132,11 @@ contract MathTesterTest is Test {
);
assertEq(x_p, x_j);
assertEq(x_p, x_g);
assertEq(y_p, y_j);
assertEq(y_p, y_g);
assertEq(math.isOnCurve(x_p, y_p), true);
assertEq(math.isOnCurveGhost(x_p, y_p), true);
unchecked { ++i; }
}
@ -97,7 +145,7 @@ contract MathTesterTest is Test {
function test_triplet() public view {
uint256 len = points.length - 2;
for (uint256 i; i < len;) {
(uint256 x_p, uint256 y_p, uint256 z_p) = math.mulProjectiveTriplet(
(uint256 x_p, uint256 y_p, uint256 z_p) = math.mulTripletProjective(
uint256(points[i].x),
uint256(points[i].y),
uint256(points[i].k),
@ -110,7 +158,7 @@ contract MathTesterTest is Test {
);
(x_p, y_p) = math.toAffineProjective(x_p, y_p, z_p);
(uint256 x_j, uint256 y_j) = math.mulEcTriplet(
(uint256 x_j, uint256 y_j) = math.mulTripletEc(
uint256(points[i].x),
uint256(points[i].y),
uint256(points[i].k),
@ -125,6 +173,7 @@ contract MathTesterTest is Test {
assertEq(x_p, x_j);
assertEq(y_p, y_j);
assertEq(math.isOnCurve(x_p, y_p), true);
assertEq(math.isOnCurveGhost(x_p, y_p), true);
unchecked { ++i; }
}
@ -141,6 +190,14 @@ contract MathTesterTest is Test {
);
(x_p, y_p) = math.toAffineProjective(x_p, y_p, z_p);
(uint256 x_g, uint256 y_g, uint256 z_g) = math.addGhost(
uint256(points[i].x),
uint256(points[i].y),
uint256(points[i+1].x),
uint256(points[i+1].y)
);
(x_g, y_g) = math.toAffineGhost(x_g, y_g, z_g);
(uint256 x_j, uint256 y_j, uint256 z_j) = math.addJacobian(
uint256(points[i].x),
uint256(points[i].y),
@ -150,8 +207,11 @@ contract MathTesterTest is Test {
(x_j, y_j) = math.toAffineJacobian(x_j, y_j, z_j);
assertEq(x_p, x_j);
assertEq(x_p, x_g);
assertEq(y_p, y_j);
assertEq(y_p, y_g);
assertEq(math.isOnCurve(x_p, y_p), true);
assertEq(math.isOnCurveGhost(x_p, y_p), true);
unchecked { ++i; }
}
@ -160,7 +220,7 @@ contract MathTesterTest is Test {
function test_mixedAddition() public view {
uint256 len = points.length - 1;
for (uint256 i; i < len;) {
(uint256 x_p, uint256 y_p, uint256 z_p) = math.addProjectiveMixed(
(uint256 x_p, uint256 y_p, uint256 z_p) = math.addMixedProjective(
uint256(points[i].x),
uint256(points[i].y),
uint256(points[i+1].x),
@ -168,6 +228,14 @@ contract MathTesterTest is Test {
);
(x_p, y_p) = math.toAffineProjective(x_p, y_p, z_p);
(uint256 x_g, uint256 y_g, uint256 z_g) = math.addMixedGhost(
uint256(points[i].x),
uint256(points[i].y),
uint256(points[i+1].x),
uint256(points[i+1].y)
);
(x_g, y_g) = math.toAffineGhost(x_g, y_g, z_g);
(uint256 x_j, uint256 y_j, uint256 z_j) = math.addJacobian(
uint256(points[i].x),
uint256(points[i].y),
@ -179,6 +247,7 @@ contract MathTesterTest is Test {
assertEq(x_p, x_j);
assertEq(y_p, y_j);
assertEq(math.isOnCurve(x_p, y_p), true);
assertEq(math.isOnCurveGhost(x_p, y_p), true);
unchecked { ++i; }
}
@ -193,6 +262,12 @@ contract MathTesterTest is Test {
);
(x_p, y_p) = math.toAffineProjective(x_p, y_p, z_p);
(uint256 x_g, uint256 y_g, uint256 z_g) = math.doubleGhost(
uint256(points[i].x),
uint256(points[i].y)
);
(x_g, y_g) = math.toAffineProjective(x_g, y_g, z_g);
(uint256 x_j, uint256 y_j, uint256 z_j) = math.doubleJacobian(
uint256(points[i].x),
uint256(points[i].y)
@ -200,8 +275,11 @@ contract MathTesterTest is Test {
(x_j, y_j) = math.toAffineJacobian(x_j, y_j, z_j);
assertEq(x_p, x_j);
assertEq(x_p, x_g);
assertEq(y_p, y_j);
assertEq(y_p, y_g);
assertEq(math.isOnCurve(x_p, y_p), true);
assertEq(math.isOnCurveGhost(x_p, y_p), true);
unchecked { ++i; }
}