Fix P256 corner cases (#5218)

Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
Co-authored-by: Ernesto García <ernestognw@gmail.com>
This commit is contained in:
cairo
2024-09-30 09:05:44 -07:00
committed by GitHub
parent d3ca1d1f00
commit e3cfe1c5dd
4 changed files with 88 additions and 30 deletions

View File

@ -9,8 +9,8 @@ import {Math} from "@openzeppelin/contracts/utils/math/Math.sol";
contract P256Test is Test {
/// forge-config: default.fuzz.runs = 512
function testVerify(uint256 seed, bytes32 digest) public {
uint256 privateKey = bound(uint256(keccak256(abi.encode(seed))), 1, P256.N - 1);
function testVerify(bytes32 digest, uint256 seed) public {
uint256 privateKey = _asPrivateKey(seed);
(bytes32 x, bytes32 y) = P256PublicKey.getPublicKey(privateKey);
(bytes32 r, bytes32 s) = vm.signP256(privateKey, digest);
@ -20,8 +20,8 @@ contract P256Test is Test {
}
/// forge-config: default.fuzz.runs = 512
function testRecover(uint256 seed, bytes32 digest) public {
uint256 privateKey = bound(uint256(keccak256(abi.encode(seed))), 1, P256.N - 1);
function testRecover(bytes32 digest, uint256 seed) public {
uint256 privateKey = _asPrivateKey(seed);
(bytes32 x, bytes32 y) = P256PublicKey.getPublicKey(privateKey);
(bytes32 r, bytes32 s) = vm.signP256(privateKey, digest);
@ -31,6 +31,10 @@ contract P256Test is Test {
assertTrue((qx0 == x && qy0 == y) || (qx1 == x && qy1 == y));
}
function _asPrivateKey(uint256 seed) private pure returns (uint256) {
return bound(seed, 1, P256.N - 1);
}
function _ensureLowerS(bytes32 s) private pure returns (bytes32) {
uint256 _s = uint256(s);
unchecked {