-
Notifications
You must be signed in to change notification settings - Fork 14
fix(low): check if points are on the curve before addition #396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
28c21ba
fix: check if points are on the curve before addition
heueristik b72fa3f
fix: name clash
heueristik 0573ec6
refactor: simplify check following the recommendation by Informal Sys…
heueristik f5096be
refactor: rename struct
heueristik fe5cdbe
feat: add tests
heueristik 89b149c
refactor: docs, variable names, and tests
heueristik 9260e53
style: harmonize test names
heueristik 995ef2e
chore: add author
heueristik 16c9b68
doc: improve comment
heueristik ee556a8
refactor: fix fuzz test name prefixes
heueristik 6d7c7ac
feat: add tests
heueristik 95f23e1
refactor: increase fuzzing space
heueristik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,43 +10,62 @@ import {EfficientHashLib} from "@solady/utils/EfficientHashLib.sol"; | |
| /// @notice A library containing methods of the delta proving system. | ||
| /// @custom:security-contact [email protected] | ||
| library Delta { | ||
| using Delta for CurvePoint; | ||
| using Delta for Point; | ||
|
|
||
| /// @notice An elliptic curve point representing a delta value. | ||
| /// @param x The x component of the point. | ||
| /// @param y The y component of the point. | ||
| struct CurvePoint { | ||
| struct Point { | ||
| uint256 x; | ||
| uint256 y; | ||
| } | ||
|
|
||
| /// @notice The constant of the secp256k1 (K-256) elliptic curve. | ||
| /// @notice The x-coordinate of the curve generator point. | ||
| uint256 internal constant _GX = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798; | ||
|
|
||
| /// @notice The y-coordinate of the curve generator point. | ||
| uint256 internal constant _GY = 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8; | ||
|
|
||
| // @notice The coefficient a of th secp256k1 (K-256) elliptic curve (y² = x³ + ax + b). | ||
| uint256 internal constant _AA = 0; | ||
|
|
||
| /// @notice The modulus of the secp256k1 (K-256) elliptic curve. | ||
| // @notice The coefficient b of th secp256k1 (K-256) elliptic curve (y² = x³ + ax + b). | ||
| uint256 internal constant _BB = 7; | ||
|
|
||
| /// @notice The field prime modulus (2^256 - 2^32 - 977) of the secp256k1 (K-256) elliptic curve (y² = x³ + ax + b). | ||
| uint256 internal constant _PP = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F; | ||
|
|
||
| /// @notice Thrown if the recovered delta public key doesn't match the delta instance. | ||
| error DeltaMismatch(address expected, address actual); | ||
|
|
||
| /// @notice Thrown when a provided point is not on the curve. | ||
| error PointNotOnCurve(Point point); | ||
|
|
||
| /// @notice Returns the elliptic curve point representing the zero delta. | ||
| /// @return zeroDelta The zero delta. | ||
| function zero() internal pure returns (CurvePoint memory zeroDelta) { | ||
| zeroDelta = CurvePoint({x: 0, y: 0}); | ||
| function zero() internal pure returns (Point memory zeroDelta) { | ||
| zeroDelta = Point({x: 0, y: 0}); | ||
| } | ||
|
|
||
| /// @notice Adds two elliptic curve points and returns the resulting value. | ||
| /// @param p1 The first curve point. | ||
| /// @param p2 The second curve point. | ||
| /// @notice Adds two delta points and returns the sum. | ||
| /// @param lhs The left-hand side point that can also be the zero delta. | ||
| /// @param rhs The right-hand side point that must be a curve point. | ||
| /// @return sum The resulting curve point. | ||
agureev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| function add(CurvePoint memory p1, CurvePoint memory p2) internal pure returns (CurvePoint memory sum) { | ||
| (sum.x, sum.y) = EllipticCurve.ecAdd({_x1: p1.x, _y1: p1.y, _x2: p2.x, _y2: p2.y, _aa: _AA, _pp: _PP}); | ||
| /// @dev Note that only the right-hand side point is checked to allow adding the zero delta from the left. This is | ||
| /// done due to the delta points being added sequentially starting from the zero delta in the | ||
| /// `ProtocolAdapter.execute()` function. | ||
| function add(Point memory lhs, Point memory rhs) internal pure returns (Point memory sum) { | ||
| if (!EllipticCurve.isOnCurve({_x: rhs.x, _y: rhs.y, _aa: _AA, _bb: _BB, _pp: _PP})) { | ||
| revert PointNotOnCurve(rhs); | ||
| } | ||
|
|
||
| (sum.x, sum.y) = EllipticCurve.ecAdd({_x1: lhs.x, _y1: lhs.y, _x2: rhs.x, _y2: rhs.y, _aa: _AA, _pp: _PP}); | ||
| } | ||
|
|
||
| /// @notice Converts an elliptic curve point to an Ethereum account address. | ||
| /// @param delta The elliptic curve point. | ||
| /// @return account The associated account. | ||
| function toAccount(CurvePoint memory delta) internal pure returns (address account) { | ||
| function toAccount(Point memory delta) internal pure returns (address account) { | ||
| // Hash the public key with Keccak-256. | ||
| bytes32 hashedKey = EfficientHashLib.hash(delta.x, delta.y); | ||
|
|
||
|
|
@@ -66,7 +85,7 @@ library Delta { | |
| /// @param proof The delta proof. | ||
| /// @param instance The transaction delta. | ||
| /// @param verifyingKey The Keccak-256 hash of all nullifiers and commitments as ordered in the compliance units. | ||
| function verify(bytes memory proof, CurvePoint memory instance, bytes32 verifyingKey) internal pure { | ||
| function verify(bytes memory proof, Point memory instance, bytes32 verifyingKey) internal pure { | ||
| // Verify the delta proof using the ECDSA.recover API to obtain the address | ||
| address recovered = ECDSA.recover({hash: verifyingKey, signature: proof}); | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.