`ECDSA`: Use unchecked arithmetic for the `tryRecover` function (#4301)

Signed-off-by: Pascal Marco Caversaccio <pascal.caversaccio@hotmail.ch>
pull/4329/head
Pascal Marco Caversaccio 2 years ago committed by GitHub
parent d9474327a4
commit 1d5bcd04e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      .changeset/four-adults-knock.md
  2. 3
      contracts/utils/cryptography/ECDSA.sol

@ -0,0 +1,5 @@
---
'openzeppelin-solidity': patch
---
`ECDSA`: Use unchecked arithmetic for the `tryRecover` function that receives the `r` and `vs` short-signature fields separately.

@ -98,10 +98,13 @@ library ECDSA {
* _Available since v4.3._
*/
function tryRecover(bytes32 hash, bytes32 r, bytes32 vs) internal pure returns (address, RecoverError) {
unchecked {
bytes32 s = vs & bytes32(0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff);
// We do not check for an overflow here since the shift operation results in 0 or 1.
uint8 v = uint8((uint256(vs) >> 255) + 27);
return tryRecover(hash, v, r, s);
}
}
/**
* @dev Overload of {ECDSA-recover} that receives the `r and `vs` short-signature fields separately.

Loading…
Cancel
Save