Skip to content

Commit

Permalink
✨ implement isOnCurve function
Browse files Browse the repository at this point in the history
This function checks if a point is on the p256r1 curve.
  • Loading branch information
qd-qd committed Mar 7, 2024
1 parent d6e84c7 commit ca83ad2
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/ECDSA256r1.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.19 <0.9.0;

import { ECDSA, Curve, p, gx, gy, n, MINUS_2, MINUS_1, MODEXP_PRECOMPILE } from "./utils/ECDSA.sol";
import { ECDSA, Curve, p, gx, gy, n, MINUS_2, MINUS_1, MODEXP_PRECOMPILE, a, b } from "./utils/ECDSA.sol";

/// @title ECDSA256r1
/// @notice A library to verify ECDSA signatures made on the secp256r1 curve
Expand All @@ -14,6 +14,27 @@ import { ECDSA, Curve, p, gx, gy, n, MINUS_2, MINUS_1, MODEXP_PRECOMPILE } from
library ECDSA256r1 {
using { Curve.nModInv } for uint256;

/// @notice Verifies that a point is on the secp256r1 curve
/// @param x The x-coordinate of the point
/// @param y The y-coordinate of the point
/// @return bool True if the point is on the curve, false otherwise
function isPointValid(uint256 x, uint256 y) internal pure returns (bool) {
if (((0 == x) && (0 == y)) || x == p || y == p) {
return false;
}

unchecked {
// y^2
uint256 lhs = mulmod(y, y, p);
// x^3+ax
uint256 rhs = addmod(mulmod(mulmod(x, x, p), x, p), mulmod(x, a, p), p);
// x^3 + a*x + b
rhs = addmod(rhs, b, p);

return lhs == rhs;
}
}

//// @notice Computes uG + vQ using Strauss-Shamir's trick on the secp256r1 elliptic curve, where G is the basepoint
/// and Q is the public key.
/// @param Q0 x-coordinate of the input point Q
Expand Down

0 comments on commit ca83ad2

Please sign in to comment.