@@ -10,43 +10,62 @@ import {EfficientHashLib} from "@solady/utils/EfficientHashLib.sol";
1010/// @notice A library containing methods of the delta proving system.
1111/// @custom:security-contact [email protected] 1212library Delta {
13- using Delta for CurvePoint ;
13+ using Delta for Point ;
1414
1515 /// @notice An elliptic curve point representing a delta value.
1616 /// @param x The x component of the point.
1717 /// @param y The y component of the point.
18- struct CurvePoint {
18+ struct Point {
1919 uint256 x;
2020 uint256 y;
2121 }
2222
23- /// @notice The constant of the secp256k1 (K-256) elliptic curve.
23+ /// @notice The x-coordinate of the curve generator point.
24+ uint256 internal constant _GX = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798 ;
25+
26+ /// @notice The y-coordinate of the curve generator point.
27+ uint256 internal constant _GY = 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8 ;
28+
29+ // @notice The coefficient a of th secp256k1 (K-256) elliptic curve (y² = x³ + ax + b).
2430 uint256 internal constant _AA = 0 ;
2531
26- /// @notice The modulus of the secp256k1 (K-256) elliptic curve.
32+ // @notice The coefficient b of th secp256k1 (K-256) elliptic curve (y² = x³ + ax + b).
33+ uint256 internal constant _BB = 7 ;
34+
35+ /// @notice The field prime modulus (2^256 - 2^32 - 977) of the secp256k1 (K-256) elliptic curve (y² = x³ + ax + b).
2736 uint256 internal constant _PP = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F ;
2837
2938 /// @notice Thrown if the recovered delta public key doesn't match the delta instance.
3039 error DeltaMismatch (address expected , address actual );
3140
41+ /// @notice Thrown when a provided point is not on the curve.
42+ error PointNotOnCurve (Point point );
43+
3244 /// @notice Returns the elliptic curve point representing the zero delta.
3345 /// @return zeroDelta The zero delta.
34- function zero () internal pure returns (CurvePoint memory zeroDelta ) {
35- zeroDelta = CurvePoint ({x: 0 , y: 0 });
46+ function zero () internal pure returns (Point memory zeroDelta ) {
47+ zeroDelta = Point ({x: 0 , y: 0 });
3648 }
3749
38- /// @notice Adds two elliptic curve points and returns the resulting value .
39- /// @param p1 The first curve point.
40- /// @param p2 The second curve point.
50+ /// @notice Adds two delta points and returns the sum .
51+ /// @param lhs The left-hand side point that can also be the zero delta .
52+ /// @param rhs The right-hand side point that must be a curve point.
4153 /// @return sum The resulting curve point.
42- function add (CurvePoint memory p1 , CurvePoint memory p2 ) internal pure returns (CurvePoint memory sum ) {
43- (sum.x, sum.y) = EllipticCurve.ecAdd ({_x1: p1.x, _y1: p1.y, _x2: p2.x, _y2: p2.y, _aa: _AA, _pp: _PP});
54+ /// @dev Note that only the right-hand side point is checked to allow adding the zero delta from the left. This is
55+ /// done due to the delta points being added sequentially starting from the zero delta in the
56+ /// `ProtocolAdapter.execute()` function.
57+ function add (Point memory lhs , Point memory rhs ) internal pure returns (Point memory sum ) {
58+ if (! EllipticCurve.isOnCurve ({_x: rhs.x, _y: rhs.y, _aa: _AA, _bb: _BB, _pp: _PP})) {
59+ revert PointNotOnCurve (rhs);
60+ }
61+
62+ (sum.x, sum.y) = EllipticCurve.ecAdd ({_x1: lhs.x, _y1: lhs.y, _x2: rhs.x, _y2: rhs.y, _aa: _AA, _pp: _PP});
4463 }
4564
4665 /// @notice Converts an elliptic curve point to an Ethereum account address.
4766 /// @param delta The elliptic curve point.
4867 /// @return account The associated account.
49- function toAccount (CurvePoint memory delta ) internal pure returns (address account ) {
68+ function toAccount (Point memory delta ) internal pure returns (address account ) {
5069 // Hash the public key with Keccak-256.
5170 bytes32 hashedKey = EfficientHashLib.hash (delta.x, delta.y);
5271
@@ -66,7 +85,7 @@ library Delta {
6685 /// @param proof The delta proof.
6786 /// @param instance The transaction delta.
6887 /// @param verifyingKey The Keccak-256 hash of all nullifiers and commitments as ordered in the compliance units.
69- function verify (bytes memory proof , CurvePoint memory instance , bytes32 verifyingKey ) internal pure {
88+ function verify (bytes memory proof , Point memory instance , bytes32 verifyingKey ) internal pure {
7089 // Verify the delta proof using the ECDSA.recover API to obtain the address
7190 address recovered = ECDSA.recover ({hash: verifyingKey, signature: proof});
7291
0 commit comments