SchnorrSignature.sol 1.28 KB
Newer Older
John Doe's avatar
John Doe committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
// SPDX-License-Identifier: MIT
pragma solidity =0.8.4;

import "./RP/RangeProofMath.sol";

contract SchnorrSignature is RangeProofMath {
	struct SlotSchnorrSignature {
		string message;
		PointEC publicKey;
		PointEC ecR;
		uint256 s;
	}

	/**
	 * @dev Returns the result of schnorr signature verify..
	 * @param message signed message.
	 * @param publicKey public key with which message was signed.
	 * @param ecR signature part R.
	 * @param s signature part s.
	 * @return verifying result.
	 */
	function SchnorrSignatureVerify(
		string memory message,
		PointEC memory publicKey,
		PointEC memory ecR,
		uint256 s
	) public pure returns (bool) {
		uint256 messageHash;
		PointEC memory ecG;
		PointEC memory ecLeft;
		PointEC memory ecRight;

		require(
			eIsOnCurve(publicKey.x, publicKey.y) && eIsOnCurve(ecR.x, ecR.y),
			"Invalid input parametrs to verify the Schnorr signature"
		);

		// c = H (X, R, m)
		messageHash = uint256(
			sha256(abi.encodePacked(publicKey.x, publicKey.y, ecR.x, ecR.y, message))
		);
		//s*G
		ecG.x = gx;
		ecG.y = gy;

		(ecLeft.x, ecLeft.y) = eMul(s, ecG.x, ecG.y);
		//R + c*X
		(ecRight.x, ecRight.y) = eMul(messageHash, publicKey.x, publicKey.y);
		(ecRight.x, ecRight.y) = eAdd(ecRight.x, ecRight.y, ecR.x, ecR.y);
		return _equalPointEC(ecLeft, ecRight);
	}
}