// SPDX-License-Identifier: MIT pragma solidity ^0.8.28; /// @title Secp256k1 /// @notice What the contract needs of secp256k1: point validation, the address of a /// point, and the computation of `z*G - c*Q`. /// @dev The EVM has no scalar multiplication on secp256k1, but it has `ecrecover`. /// {linearCombAddress} calls `ecrecover` with inputs chosen so that it returns the /// address of `z*G - c*Q`, which is the only curve computation the proof verifier /// needs. This works because `ecrecover` is defined on secp256k1; on another curve /// the same value would have to be computed with explicit point arithmetic. library Secp256k1 { /// @dev Base field prime p = 2^256 - 2^32 - 977. uint256 internal constant P = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F; /// @dev Prime group order N. uint256 internal constant N = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141; /// @dev Generator G. uint256 internal constant G_X = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798; uint256 internal constant G_Y = 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8; /// @notice An affine point `(x, y)`; `(0, 0)` denotes the point at infinity. struct Point { uint256 x; uint256 y; } /// @notice True iff `(x, y)` is a finite point on `y^2 = x^3 + 7` in canonical form. /// @dev Requires `0 < x < P` and `y < P` so non-canonical representatives `(x, y + P)` /// cannot spoof parity bits or address hashes. Since 7 is a quadratic non-residue /// mod P, no on-curve point has `x = 0`, so `(0, 0)` is an unambiguous infinity. function isOnCurve(Point memory pt) internal pure returns (bool) { if (pt.x == 0 || pt.x >= P || pt.y >= P) return false; uint256 lhs = mulmod(pt.y, pt.y, P); uint256 rhs = addmod(mulmod(mulmod(pt.x, pt.x, P), pt.x, P), 7, P); return lhs == rhs; } /// @notice True iff `pt` is on-curve and `0 < x < N`, so its x-coordinate is a valid /// `r` input to `ecrecover` (whose domain is `[1, N-1]`). function isValidRecoverablePoint(Point memory pt) internal pure returns (bool) { return pt.x < N && isOnCurve(pt); } /// @notice 20-byte Ethereum address of a point: `keccak256(x || y)[12:]`. function toAddress(Point memory pt) internal pure returns (address) { return address(uint160(uint256(keccak256(abi.encode(pt.x, pt.y))))); } /// @notice Returns `addr(z*G - c*Q)` using one `ecrecover` call, or `address(0)` on bad input. /// @dev `ecrecover(h, v, r, s)` returns `addr( r^-1 * (s*R - h*G) )`, where `R` is the /// point with x-coordinate `r` and y-parity `v - 27`. Choosing the inputs /// r = Q.x, v = 27 + (Q.y & 1), h = -z*Q.x mod N, s = -c*Q.x mod N /// makes `R = Q` and gives /// Q.x^-1 * ( (-c*Q.x)*Q - (-z*Q.x)*G ) = z*G - c*Q. /// Caller must pass a `Q` already checked with `isValidRecoverablePoint`. function linearCombAddress(Point memory Q, uint256 z, uint256 c) internal pure returns (address) { if (z == 0 || z >= N || c == 0 || c >= N || Q.x == 0 || Q.x >= N) { return address(0); } uint8 v = uint8(27 + (Q.y & 1)); bytes32 h = bytes32(mulmod(N - z, Q.x, N)); bytes32 s = bytes32(mulmod(N - c, Q.x, N)); return ecrecover(h, v, bytes32(Q.x), s); } }