Skip to content

Commit 115220c

Browse files
authored
♻️ Tidy P256 (#1396)
1 parent 1e9ad83 commit 115220c

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

foundry.toml

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ evm_version = "cancun"
2929
skip = []
3030

3131
[profile.ithaca]
32-
match_path = "*/ext/ithaca/*"
3332
evm_version = "cancun"
3433
odyssey = true
3534
skip = []

src/utils/P256.sol

+4-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ library P256 {
2222
/// See: https://gist.github.com/Vectorized/3c69dcf4604b9e1216525cabcd06ee34
2323
/// This is to enable the optimization to skip the `VERIFIER` entirely
2424
/// when the `RIP_PRECOMPILE` returns empty returndata for an invalid signature.
25-
address internal constant CANARY = 0x0000000000009D47E8d483936dc4B6b4bf7bbFe6;
25+
address internal constant CANARY = 0x0000000000001Ab2e8006Fd8B71907bf06a5BDEE;
2626

2727
/// @dev Address of the RIP-7212 P256 verifier precompile.
2828
/// Currently, we don't support EIP-7212's precompile at 0x0b as it has not been finalized.
@@ -115,14 +115,15 @@ library P256 {
115115
assembly {
116116
let m := mload(0x40)
117117
// These values are taken from the standard Wycheproof test vectors.
118+
// https://github.com/C2SP/wycheproof/blob/aca47066256c167f0ce04d611d718cc85654341e/testvectors/ecdsa_webcrypto_test.json#L1197
118119
mstore(m, 0x532eaabd9574880dbf76b9b8cc00832c20a6ec113d682299550d7a6e0f345e25) // `hash`.
119120
mstore(add(m, 0x20), 0x5) // `r`.
120121
mstore(add(m, 0x40), 0x1) // `s`.
121122
mstore(add(m, 0x60), 0x4a03ef9f92eb268cafa601072489a56380fa0dc43171d7712813b3a19a1eb5e5) // `x`.
122123
mstore(add(m, 0x80), 0x3e213e28a608ce9a2f4a17fd830c6654018a79b3e0263d91a8ba90622df6f2f0) // `y`.
123124
// The `invalid` upon `staticcall` failure is solely for gas estimation.
124-
if iszero(staticcall(gas(), RIP_PRECOMPILE, m, 0xa0, 0x00, 0x00)) { invalid() }
125-
result := iszero(iszero(returndatasize()))
125+
if iszero(staticcall(gas(), RIP_PRECOMPILE, m, 0xa0, m, 0x20)) { invalid() }
126+
result := eq(1, mload(m))
126127
}
127128
}
128129

test/P256.t.sol

+14
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,27 @@ contract P256VerifierEtcher is SoladyTest {
1212
bytes internal constant _PASSTHROUGH_BYTECODE = hex"600160005260206000f3";
1313

1414
function _etchBytecode(address target, bytes memory bytecode, bool active) internal {
15+
if (target == P256.RIP_PRECOMPILE) {
16+
if (active && _hasNativeRIPPrecompile()) return;
17+
if (!active && _hasNativeRIPPrecompile()) {
18+
/// @solidity memory-safe-assembly
19+
assembly {
20+
return(0x00, 0x00)
21+
}
22+
}
23+
}
24+
1525
if (active) {
1626
if (target.code.length == 0) vm.etch(target, bytecode);
1727
} else {
1828
if (target.code.length != 0) vm.etch(target, "");
1929
}
2030
}
2131

32+
function _hasNativeRIPPrecompile() internal view returns (bool) {
33+
return P256.hasPrecompile() && P256.RIP_PRECOMPILE.code.length == 0;
34+
}
35+
2236
function _etchPassthroughBytecode(address target, bool active) internal {
2337
_etchBytecode(target, _PASSTHROUGH_BYTECODE, active);
2438
}

0 commit comments

Comments
 (0)