|
1 | 1 | // SPDX-License-Identifier: MIT |
2 | 2 | pragma solidity ^0.8.30; |
3 | 3 |
|
| 4 | +import {EllipticCurve} from "@elliptic-curve-solidity/contracts/EllipticCurve.sol"; |
4 | 5 | import {Test} from "forge-std/Test.sol"; |
5 | 6 |
|
6 | 7 | import {Delta} from "../../src/libs/proving/Delta.sol"; |
@@ -302,6 +303,66 @@ contract DeltaProofTest is Test { |
302 | 303 | DeltaFuzzing.verify({proof: proof, instance: deltaAcc, verifyingKey: verifyingKey}); |
303 | 304 | } |
304 | 305 |
|
| 306 | + function test_add_reverts_if_the_second_point_is_not_on_the_curve(uint8 k) public { |
| 307 | + // Generate a random point on the curve. |
| 308 | + Delta.Point memory p1 = _mul({p: Delta.Point({x: Delta._GX, y: Delta._GY}), k: k}); |
| 309 | + assertTrue( |
| 310 | + EllipticCurve.isOnCurve({_x: p1.x, _y: p1.y, _aa: Delta._AA, _bb: Delta._BB, _pp: Delta._PP}), |
| 311 | + "Point 1 must be on the curve." |
| 312 | + ); |
| 313 | + |
| 314 | + Delta.Point memory p2 = Delta.zero(); |
| 315 | + |
| 316 | + // Attempt to add a point being not on the curve |
| 317 | + vm.expectRevert(abi.encodeWithSelector(Delta.PointNotOnCurve.selector, p2), address(this)); |
| 318 | + p1.add(p2); |
| 319 | + } |
| 320 | + |
| 321 | + function test_add_adding_two_curve_points_produces_a_point_on_the_curve(uint8 k1, uint8 k2) public pure { |
| 322 | + // Generate two random points on the curve. |
| 323 | + Delta.Point memory p1 = _mul({p: Delta.Point({x: Delta._GX, y: Delta._GY}), k: k1}); |
| 324 | + assertTrue( |
| 325 | + EllipticCurve.isOnCurve({_x: p1.x, _y: p1.y, _aa: Delta._AA, _bb: Delta._BB, _pp: Delta._PP}), |
| 326 | + "Point 1 must be on the curve." |
| 327 | + ); |
| 328 | + |
| 329 | + Delta.Point memory p2 = _mul({p: Delta.Point({x: Delta._GX, y: Delta._GY}), k: k2}); |
| 330 | + assertTrue( |
| 331 | + EllipticCurve.isOnCurve({_x: p2.x, _y: p2.y, _aa: Delta._AA, _bb: Delta._BB, _pp: Delta._PP}), |
| 332 | + "Point 2 must be on the curve." |
| 333 | + ); |
| 334 | + |
| 335 | + // Add the two points. |
| 336 | + Delta.Point memory sum = Delta.add(p1, p2); |
| 337 | + assertTrue( |
| 338 | + EllipticCurve.isOnCurve({_x: sum.x, _y: sum.y, _aa: Delta._AA, _bb: Delta._BB, _pp: Delta._PP}), |
| 339 | + "Sum must be on the curve." |
| 340 | + ); |
| 341 | + } |
| 342 | + |
| 343 | + function test_add_adding_a_curve_point_to_zero_produces_a_point_on_the_curve(uint8 k) public pure { |
| 344 | + Delta.Point memory zero = Delta.zero(); |
| 345 | + |
| 346 | + // Generate a random point on the curve. |
| 347 | + Delta.Point memory p = _mul({p: Delta.Point({x: Delta._GX, y: Delta._GY}), k: k}); |
| 348 | + assertTrue( |
| 349 | + EllipticCurve.isOnCurve({_x: p.x, _y: p.y, _aa: Delta._AA, _bb: Delta._BB, _pp: Delta._PP}), |
| 350 | + "The point must be on the curve." |
| 351 | + ); |
| 352 | + |
| 353 | + // Add the two points. |
| 354 | + Delta.Point memory sum = zero.add(p); |
| 355 | + assertTrue( |
| 356 | + EllipticCurve.isOnCurve({_x: sum.x, _y: sum.y, _aa: Delta._AA, _bb: Delta._BB, _pp: Delta._PP}), |
| 357 | + "Sum must be on the curve." |
| 358 | + ); |
| 359 | + } |
| 360 | + |
| 361 | + function test_zero_is_not_on_the_curve() public pure { |
| 362 | + Delta.Point memory p2 = Delta.zero(); |
| 363 | + assertFalse(EllipticCurve.isOnCurve({_x: p2.x, _y: p2.y, _aa: Delta._AA, _bb: Delta._BB, _pp: Delta._PP})); |
| 364 | + } |
| 365 | + |
305 | 366 | function test_verify_example_delta_proof() public pure { |
306 | 367 | Transaction memory txn = TransactionExample.transaction(); |
307 | 368 |
|
|
0 commit comments