Skip to content

Commit

Permalink
P256.sol: support precompile as primary, use solidity as fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
nalinbhardwaj committed May 19, 2024
1 parent 76db52f commit 7df29d1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 26 deletions.
57 changes: 34 additions & 23 deletions lcov.info
Original file line number Diff line number Diff line change
@@ -1,38 +1,49 @@
TN:
SF:src/P256.sol
FN:10,P256.verifySignatureAllowMalleability
FN:11,P256.verifySignatureAllowMalleability
FNDA:6,P256.verifySignatureAllowMalleability
DA:17,6
DA:17,6
DA:17,6
DA:18,6
DA:18,6
DA:18,6
DA:19,6
DA:19,6
BRDA:19,0,0,-
BRDA:19,0,1,-
DA:21,6
DA:20,6
DA:20,6
DA:20,6
DA:21,6
DA:21,6
DA:21,6
FN:28,P256.verifySignature
BRDA:21,0,0,6
BRDA:21,0,1,-
DA:21,0
DA:21,0
DA:21,0
DA:23,6
DA:23,6
DA:23,6
DA:26,6
DA:26,6
BRDA:26,1,0,-
BRDA:26,1,1,-
DA:28,6
DA:28,6
DA:28,6
DA:28,6
FN:35,P256.verifySignature
FNDA:5,P256.verifySignature
DA:36,5
DA:36,5
BRDA:36,1,0,4
BRDA:36,1,1,1
DA:37,1
DA:37,1
DA:40,4
DA:40,4
DA:40,4
DA:43,5
DA:43,5
BRDA:43,2,0,4
BRDA:43,2,1,1
DA:44,1
DA:44,1
DA:47,4
DA:47,4
DA:47,4
FNF:2
FNH:2
LF:7
LH:7
BRF:4
BRH:2
LF:9
LH:9
BRF:6
BRH:3
end_of_record
TN:
SF:src/P256Verifier.sol
Expand Down
13 changes: 10 additions & 3 deletions src/P256.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pragma solidity 0.8.21;
* Helper library for external contracts to verify P256 signatures.
**/
library P256 {
address constant PRECOMPILE = address(0x100);
address constant VERIFIER = 0xc2b78104907F722DABAc4C69f826a522B2754De4;

function verifySignatureAllowMalleability(
Expand All @@ -15,10 +16,16 @@ library P256 {
uint256 y
) internal view returns (bool) {
bytes memory args = abi.encode(message_hash, r, s, x, y);
(bool success, bytes memory ret) = VERIFIER.staticcall(args);
assert(success); // never reverts, always returns 0 or 1

return abi.decode(ret, (uint256)) == 1;
(bool success, bytes memory ret) = PRECOMPILE.staticcall(args);
if (success && ret.length > 0) return abi.decode(ret, (uint256)) == 1;

(bool fallbackSuccess, bytes memory fallbackRet) = VERIFIER.staticcall(
args
);
assert(fallbackSuccess); // never reverts, always returns 0 or 1

return abi.decode(fallbackRet, (uint256)) == 1;
}

/// P256 curve order n/2 for malleability check
Expand Down

0 comments on commit 7df29d1

Please sign in to comment.