// SPDX-License-Identifier: MIT pragma solidity ^0.8.28; import {Secp256k1} from "./Secp256k1.sol"; /// @title CurveArithmetic /// @notice Point addition and scalar multiplication written out with field operations. /// @dev This is the curve-independent way to compute `z*G - c*Q`. The contract does not /// use it: {Secp256k1-linearCombAddress} obtains the same value from one `ecrecover` /// call, for about 3k gas instead of about 1.2M. It /// is kept as the reference the `ecrecover` version is tested against, and as the /// code to switch to if the curve changes. /// /// The formulas are those of a curve `y^2 = x^3 + A x + B` over F_P. Moving to /// another curve of this shape means changing `P`, `A` and the generator. Points are affine, with `(0, 0)` for the point at infinity. /// Internally a point is kept as `(X, Y, Z)` with `x = X / Z^2`, `y = Y / Z^3` /// (Jacobian coordinates), which postpones every division to a single one at the end. library CurveArithmetic { uint256 private constant P = Secp256k1.P; uint256 private constant A = 0; struct Jacobian { uint256 x; uint256 y; uint256 z; // z = 0 is the point at infinity } /// @notice `addr(z*G - c*Q)`, or `address(0)` if the result is the point at infinity. /// @dev Same contract as {Secp256k1-linearCombAddress}, computed with two scalar /// multiplications and one addition. function linearCombAddress(Secp256k1.Point memory Q, uint256 z, uint256 c) internal pure returns (address) { Secp256k1.Point memory G = Secp256k1.Point({x: Secp256k1.G_X, y: Secp256k1.G_Y}); Secp256k1.Point memory R = add(mul(G, z), neg(mul(Q, c))); if (R.x == 0 && R.y == 0) return address(0); return Secp256k1.toAddress(R); } /// @notice The point `[k] pt`, by double-and-add from the lowest bit of `k`. function mul(Secp256k1.Point memory pt, uint256 k) internal pure returns (Secp256k1.Point memory) { Jacobian memory result = Jacobian(0, 0, 0); Jacobian memory power = _toJacobian(pt); // [2^i] pt at step i while (k != 0) { if (k & 1 == 1) result = _add(result, power); power = _double(power); k >>= 1; } return _toAffine(result); } /// @notice The point `a + b`. function add(Secp256k1.Point memory a, Secp256k1.Point memory b) internal pure returns (Secp256k1.Point memory) { return _toAffine(_add(_toJacobian(a), _toJacobian(b))); } /// @notice The point `-pt`. function neg(Secp256k1.Point memory pt) internal pure returns (Secp256k1.Point memory) { if (pt.y == 0) return pt; return Secp256k1.Point({x: pt.x, y: P - pt.y}); } function _toJacobian(Secp256k1.Point memory pt) private pure returns (Jacobian memory) { if (pt.x == 0 && pt.y == 0) return Jacobian(0, 0, 0); return Jacobian(pt.x, pt.y, 1); } function _toAffine(Jacobian memory pt) private pure returns (Secp256k1.Point memory) { if (pt.z == 0) return Secp256k1.Point({x: 0, y: 0}); uint256 zInv = _inverse(pt.z); uint256 zInv2 = mulmod(zInv, zInv, P); return Secp256k1.Point({x: mulmod(pt.x, zInv2, P), y: mulmod(pt.y, mulmod(zInv2, zInv, P), P)}); } /// @dev Tangent rule. In affine terms the slope is `(3 x^2 + A) / (2 y)`. function _double(Jacobian memory a) private pure returns (Jacobian memory) { if (a.z == 0 || a.y == 0) return Jacobian(0, 0, 0); uint256 yy = mulmod(a.y, a.y, P); uint256 zz = mulmod(a.z, a.z, P); uint256 s = mulmod(4, mulmod(a.x, yy, P), P); uint256 m = addmod(mulmod(3, mulmod(a.x, a.x, P), P), mulmod(A, mulmod(zz, zz, P), P), P); uint256 x3 = addmod(mulmod(m, m, P), P - mulmod(2, s, P), P); uint256 y3 = addmod(mulmod(m, addmod(s, P - x3, P), P), P - mulmod(8, mulmod(yy, yy, P), P), P); return Jacobian(x3, y3, mulmod(2, mulmod(a.y, a.z, P), P)); } /// @dev Chord rule. In affine terms the slope is `(y2 - y1) / (x2 - x1)`. function _add(Jacobian memory a, Jacobian memory b) private pure returns (Jacobian memory) { if (a.z == 0) return b; if (b.z == 0) return a; // Bring both points to the common denominator Z1 * Z2. uint256 z1z1 = mulmod(a.z, a.z, P); uint256 z2z2 = mulmod(b.z, b.z, P); uint256 u1 = mulmod(a.x, z2z2, P); uint256 u2 = mulmod(b.x, z1z1, P); uint256 s1 = mulmod(a.y, mulmod(b.z, z2z2, P), P); uint256 s2 = mulmod(b.y, mulmod(a.z, z1z1, P), P); if (u1 == u2) { // Same x: either the same point (double it) or opposite points (infinity). return s1 == s2 ? _double(a) : Jacobian(0, 0, 0); } uint256 h = addmod(u2, P - u1, P); // x2 - x1 uint256 r = addmod(s2, P - s1, P); // y2 - y1 uint256 hh = mulmod(h, h, P); uint256 hhh = mulmod(h, hh, P); uint256 v = mulmod(u1, hh, P); uint256 x3 = addmod(addmod(mulmod(r, r, P), P - hhh, P), P - mulmod(2, v, P), P); uint256 y3 = addmod(mulmod(r, addmod(v, P - x3, P), P), P - mulmod(s1, hhh, P), P); return Jacobian(x3, y3, mulmod(h, mulmod(a.z, b.z, P), P)); } /// @dev `a^-1 = a^(P-2) mod P` (Fermat), by square-and-multiply. function _inverse(uint256 a) private pure returns (uint256 result) { result = 1; uint256 exponent = P - 2; while (exponent != 0) { if (exponent & 1 == 1) result = mulmod(result, a, P); a = mulmod(a, a, P); exponent >>= 1; } } }