Skip to content

Commit 901d095

Browse files
authored
refactor(contracts): specify gas cost in SemaphoreVerifier precompile calls (#883)
* chore(contracts): remove sub() from gas() in staticcall at SemaphoreVerifier re #871 * refactor(contracts): using fixed gas cost on precompile calls re #871 * refactor(contracts): increease fixed gas cost with safety buffer
1 parent caebc1a commit 901d095

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

packages/contracts/contracts/base/SemaphoreVerifier.sol

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ contract SemaphoreVerifier {
5959
mstore(add(mIn, 32), y)
6060
mstore(add(mIn, 64), s)
6161

62-
success := staticcall(sub(gas(), 2000), 7, mIn, 96, mIn, 64)
62+
// ecMul gas cost is fixed at 6000. Add 33.3% gas for safety buffer.
63+
// Last checked in 2024 Oct, evm codename Cancun
64+
// ref: https://www.evm.codes/precompiled?fork=cancun#0x07
65+
success := staticcall(8000, 7, mIn, 96, mIn, 64)
6366

6467
if iszero(success) {
6568
mstore(0, 0)
@@ -69,7 +72,10 @@ contract SemaphoreVerifier {
6972
mstore(add(mIn, 64), mload(pR))
7073
mstore(add(mIn, 96), mload(add(pR, 32)))
7174

72-
success := staticcall(sub(gas(), 2000), 6, mIn, 128, pR, 64)
75+
// ecAdd gas cost is fixed at 150. Add 33.3% gas for safety buffer.
76+
// Last checked in 2024 Oct, evm codename Cancun
77+
// ref: https://www.evm.codes/precompiled?fork=cancun#0x06
78+
success := staticcall(200, 6, mIn, 128, pR, 64)
7379

7480
if iszero(success) {
7581
mstore(0, 0)
@@ -149,7 +155,10 @@ contract SemaphoreVerifier {
149155
mstore(add(_pPairing, 704), mload(add(vkPoints, 64)))
150156
mstore(add(_pPairing, 736), mload(add(vkPoints, 96)))
151157

152-
let success := staticcall(sub(gas(), 2000), 8, _pPairing, 768, _pPairing, 0x20)
158+
// ecPairing gas cost at 181000 given 768 bytes input. Add 33.3% gas for safety buffer.
159+
// Last checked in 2024 Oct, evm codename Cancun
160+
// ref: https://www.evm.codes/precompiled?fork=cancun#0x08
161+
let success := staticcall(241333, 8, _pPairing, 768, _pPairing, 0x20)
153162

154163
isOk := and(success, mload(_pPairing))
155164
}

0 commit comments

Comments
 (0)